Added VertexBufferLayout and associated structs, preparing to support independent layouts for each mesh primitive.
Some checks failed
CMake on multiple platforms / build (Release, cl, cl, windows-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, clang, clang++, macos-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, clang, clang++, ubuntu-latest) (push) Has been cancelled
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Has been cancelled

This commit is contained in:
2026-06-11 01:08:06 -07:00
parent f148fddeea
commit fd2c213aec
21 changed files with 1187 additions and 701 deletions

View File

@@ -41,7 +41,7 @@
using namespace hydra; using namespace hydra;
KRPipeline::KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, const KRRenderPass* renderPass, Vector2i viewport_size, Vector2i scissor_size, const PipelineInfo& info, const char* szKey, const std::vector<KRShader*>& shaders, uint32_t vertexAttributes, Topology topology) KRPipeline::KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, const KRRenderPass* renderPass, Vector2i viewport_size, Vector2i scissor_size, const PipelineInfo& info, const char* szKey, const std::vector<KRShader*>& shaders, const VertexBufferLayout* layout)
: KRContextObject(context) : KRContextObject(context)
, m_deviceHandle(deviceHandle) , m_deviceHandle(deviceHandle)
{ {
@@ -72,43 +72,7 @@ KRPipeline::KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, const KR
std::vector<VkDescriptorSetLayoutBinding> uboLayoutBindings; std::vector<VkDescriptorSetLayoutBinding> uboLayoutBindings;
// TODO - Refactor this... These lookup tables should be in KRMesh... int attribute_locations[kMaxAttributes] = {};
static const KRMesh::vertex_attrib_t attribute_mapping[KRMesh::KRENGINE_NUM_ATTRIBUTES] = {
KRMesh::KRENGINE_ATTRIB_POSITION,
KRMesh::KRENGINE_ATTRIB_NORMAL,
KRMesh::KRENGINE_ATTRIB_TANGENT,
KRMesh::KRENGINE_ATTRIB_COLOR0,
KRMesh::KRENGINE_ATTRIB_COLOR1,
KRMesh::KRENGINE_ATTRIB_COLOR2,
KRMesh::KRENGINE_ATTRIB_COLOR3,
KRMesh::KRENGINE_ATTRIB_COLOR4,
KRMesh::KRENGINE_ATTRIB_COLOR5,
KRMesh::KRENGINE_ATTRIB_COLOR6,
KRMesh::KRENGINE_ATTRIB_COLOR7,
KRMesh::KRENGINE_ATTRIB_TEXCOORD0,
KRMesh::KRENGINE_ATTRIB_TEXCOORD1,
KRMesh::KRENGINE_ATTRIB_TEXCOORD2,
KRMesh::KRENGINE_ATTRIB_TEXCOORD3,
KRMesh::KRENGINE_ATTRIB_TEXCOORD4,
KRMesh::KRENGINE_ATTRIB_TEXCOORD5,
KRMesh::KRENGINE_ATTRIB_TEXCOORD6,
KRMesh::KRENGINE_ATTRIB_TEXCOORD7,
KRMesh::KRENGINE_ATTRIB_BONEINDEXES,
KRMesh::KRENGINE_ATTRIB_BONEWEIGHTS,
KRMesh::KRENGINE_ATTRIB_POSITION,
KRMesh::KRENGINE_ATTRIB_NORMAL,
KRMesh::KRENGINE_ATTRIB_TANGENT,
KRMesh::KRENGINE_ATTRIB_TEXCOORD0,
KRMesh::KRENGINE_ATTRIB_TEXCOORD1,
KRMesh::KRENGINE_ATTRIB_TEXCOORD2,
KRMesh::KRENGINE_ATTRIB_TEXCOORD3,
KRMesh::KRENGINE_ATTRIB_TEXCOORD4,
KRMesh::KRENGINE_ATTRIB_TEXCOORD5,
KRMesh::KRENGINE_ATTRIB_TEXCOORD6,
KRMesh::KRENGINE_ATTRIB_TEXCOORD7,
};
uint32_t attribute_locations[KRMesh::KRENGINE_NUM_ATTRIBUTES] = {};
uint32_t layout_binding_count = 0; uint32_t layout_binding_count = 0;
for (KRShader* shader : shaders) { for (KRShader* shader : shaders) {
@@ -141,51 +105,43 @@ KRPipeline::KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, const KR
stageInfo.stage = shader->getShaderStageFlagBits(); stageInfo.stage = shader->getShaderStageFlagBits();
if (stageInfo.stage == VK_SHADER_STAGE_VERTEX_BIT) { if (stageInfo.stage == VK_SHADER_STAGE_VERTEX_BIT) {
for (uint32_t i = 0; i < reflection->input_variable_count; i++) { for (int i = 0; i < reflection->input_variable_count; i++) {
// TODO - We should have an interface to allow classes such as KRMesh to expose bindings attribute_locations[i] = -1;
SpvReflectInterfaceVariable& input_var = *reflection->input_variables[i]; SpvReflectInterfaceVariable& input_var = *reflection->input_variables[i];
if (strcmp(input_var.name, "vertex_position") == 0) { for (int i = 0; i < kMaxAttributes; i++) {
attribute_locations[KRMesh::KRENGINE_ATTRIB_POSITION] = input_var.location + 1; const VertexAttributeInfo& attrib = info.layout->attributes[i];
} else if (strcmp(input_var.name, "vertex_normal") == 0) { if (attrib.component == ComponentType::empty) {
attribute_locations[KRMesh::KRENGINE_ATTRIB_NORMAL] = input_var.location + 1; break;
} else if (strcmp(input_var.name, "vertex_tangent") == 0) { }
attribute_locations[KRMesh::KRENGINE_ATTRIB_TANGENT] = input_var.location + 1;
} else if (strcmp(input_var.name, "vertex_texcoord0") == 0) { const char* attribBaseName = VertexAttributeNames[(int)attrib.attribute];
attribute_locations[KRMesh::KRENGINE_ATTRIB_TEXCOORD0] = input_var.location + 1; int len = strlen(input_var.name);
} else if (strcmp(input_var.name, "vertex_texcoord1") == 0) { int baseNameLen = strlen(attribBaseName);
attribute_locations[KRMesh::KRENGINE_ATTRIB_TEXCOORD1] = input_var.location + 1; if (len == baseNameLen) {
} else if (strcmp(input_var.name, "vertex_texcoord2") == 0) { if (strcmp(attribBaseName, input_var.name) == 0) {
attribute_locations[KRMesh::KRENGINE_ATTRIB_TEXCOORD2] = input_var.location + 1; attribute_locations[i] = input_var.location;
} else if (strcmp(input_var.name, "vertex_texcoord3") == 0) { }
attribute_locations[KRMesh::KRENGINE_ATTRIB_TEXCOORD3] = input_var.location + 1; break;
} else if (strcmp(input_var.name, "vertex_texcoord4") == 0) { }
attribute_locations[KRMesh::KRENGINE_ATTRIB_TEXCOORD4] = input_var.location + 1; if (len + 1 != baseNameLen) {
} else if (strcmp(input_var.name, "vertex_texcoord5") == 0) { continue;
attribute_locations[KRMesh::KRENGINE_ATTRIB_TEXCOORD5] = input_var.location + 1; }
} else if (strcmp(input_var.name, "vertex_texcoord6") == 0) { char lastChar = input_var.name[len - 1];
attribute_locations[KRMesh::KRENGINE_ATTRIB_TEXCOORD6] = input_var.location + 1; if (lastChar < '0' || lastChar > '9') {
} else if (strcmp(input_var.name, "vertex_texcoord7") == 0) { continue;
attribute_locations[KRMesh::KRENGINE_ATTRIB_TEXCOORD7] = input_var.location + 1; }
} else if (strcmp(input_var.name, "vertex_color0") == 0) {
attribute_locations[KRMesh::KRENGINE_ATTRIB_COLOR0] = input_var.location + 1; int set = lastChar - '0';
} else if (strcmp(input_var.name, "vertex_color1") == 0) { for (int j = 0; j < i; j++) {
attribute_locations[KRMesh::KRENGINE_ATTRIB_COLOR1] = input_var.location + 1; if (info.layout->attributes[j].attribute == attrib.attribute) {
} else if (strcmp(input_var.name, "vertex_color2") == 0) { --set;
attribute_locations[KRMesh::KRENGINE_ATTRIB_COLOR2] = input_var.location + 1; }
} else if (strcmp(input_var.name, "vertex_color3") == 0) { }
attribute_locations[KRMesh::KRENGINE_ATTRIB_COLOR3] = input_var.location + 1; if (set != 0) {
} else if (strcmp(input_var.name, "vertex_color4") == 0) { continue;
attribute_locations[KRMesh::KRENGINE_ATTRIB_COLOR4] = input_var.location + 1; }
} else if (strcmp(input_var.name, "vertex_color5") == 0) { attribute_locations[i] = input_var.location;
attribute_locations[KRMesh::KRENGINE_ATTRIB_COLOR5] = input_var.location + 1; break;
} else if (strcmp(input_var.name, "vertex_color6") == 0) {
attribute_locations[KRMesh::KRENGINE_ATTRIB_COLOR6] = input_var.location + 1;
} else if (strcmp(input_var.name, "vertex_color7") == 0) {
attribute_locations[KRMesh::KRENGINE_ATTRIB_COLOR7] = input_var.location + 1;
} else if (strcmp(input_var.name, "bone_indexes") == 0) {
attribute_locations[KRMesh::KRENGINE_ATTRIB_BONEINDEXES] = input_var.location + 1;
} else if (strcmp(input_var.name, "bone_weights") == 0) {
attribute_locations[KRMesh::KRENGINE_ATTRIB_BONEWEIGHTS] = input_var.location + 1;
} }
} }
} }
@@ -198,22 +154,25 @@ KRPipeline::KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, const KR
VkVertexInputBindingDescription bindingDescription{}; VkVertexInputBindingDescription bindingDescription{};
bindingDescription.binding = 0; bindingDescription.binding = 0;
bindingDescription.stride = (uint32_t)KRMesh::VertexSizeForAttributes(vertexAttributes); bindingDescription.stride = (uint32_t)layout->vertexSize;
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
uint32_t vertexAttributeCount = 0; uint32_t vertexAttributeCount = 0;
VkVertexInputAttributeDescription vertexAttributeDescriptions[KRMesh::KRENGINE_NUM_ATTRIBUTES]{}; VkVertexInputAttributeDescription vertexAttributeDescriptions[kMaxAttributes]{};
for (int i = KRMesh::KRENGINE_ATTRIB_POSITION; i < KRMesh::KRENGINE_NUM_ATTRIBUTES; i++) { for (int i = 0; i < kMaxAttributes; i++) {
KRMesh::vertex_attrib_t mesh_attrib = static_cast<KRMesh::vertex_attrib_t>(i); VertexAttributeInfo attrib = layout->attributes[i];
int location_attrib = attribute_mapping[i]; if (attrib.component == ComponentType::empty) {
if (KRMesh::has_vertex_attribute(vertexAttributes, (KRMesh::vertex_attrib_t)i) && attribute_locations[location_attrib]) { break;
}
if (attribute_locations[i] == -1) {
continue;
}
VkVertexInputAttributeDescription& desc = vertexAttributeDescriptions[vertexAttributeCount++]; VkVertexInputAttributeDescription& desc = vertexAttributeDescriptions[vertexAttributeCount++];
desc.binding = 0; desc.binding = 0;
desc.location = attribute_locations[location_attrib] - 1; desc.location = attribute_locations[i];
desc.format = KRMesh::AttributeVulkanFormat(mesh_attrib); desc.format = KRMesh::AttributeVulkanFormat(attrib);
desc.offset = (uint32_t)KRMesh::AttributeOffset(mesh_attrib, vertexAttributes); desc.offset = (uint32_t)info.layout->offsets[i];
}
} }
VkPipelineVertexInputStateCreateInfo vertexInputInfo{}; VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
@@ -225,7 +184,7 @@ KRPipeline::KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, const KR
VkPipelineInputAssemblyStateCreateInfo inputAssembly{}; VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
switch (topology) { switch (info.layout->topology) {
case Topology::Points: case Topology::Points:
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
break; break;

View File

@@ -44,6 +44,9 @@ class KRShader;
class KRRenderPass; class KRRenderPass;
class KRUniformBuffer; class KRUniformBuffer;
class KRTexture; class KRTexture;
class KRMesh;
struct VertexBufferLayout;
struct SpvReflectShaderModule; struct SpvReflectShaderModule;
@@ -205,8 +208,8 @@ public:
bool bAlphaTest : 1; bool bAlphaTest : 1;
RasterMode rasterMode; RasterMode rasterMode;
CullMode cullMode; CullMode cullMode;
uint32_t vertexAttributes;
Topology topology; const VertexBufferLayout* layout;
const KRRenderPass* renderPass; const KRRenderPass* renderPass;
}; };
@@ -214,7 +217,7 @@ class KRPipeline : public KRContextObject
{ {
public: public:
KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, const KRRenderPass* renderPass, hydra::Vector2i viewport_size, hydra::Vector2i scissor_size, const PipelineInfo& info, const char* szKey, const std::vector<KRShader*>& shaders, uint32_t vertexAttributes, Topology topology); KRPipeline(KRContext& context, KrDeviceHandle deviceHandle, const KRRenderPass* renderPass, hydra::Vector2i viewport_size, hydra::Vector2i scissor_size, const PipelineInfo& info, const char* szKey, const std::vector<KRShader*>& shaders, const VertexBufferLayout* layout);
virtual ~KRPipeline(); virtual ~KRPipeline();
const char* getKey() const; const char* getKey() const;

View File

@@ -73,8 +73,7 @@ KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, const PipelineInf
key.second.push_back(surface.m_swapChain->m_imageFormat); key.second.push_back(surface.m_swapChain->m_imageFormat);
key.second.push_back(surface.m_swapChain->m_extent.width); key.second.push_back(surface.m_swapChain->m_extent.width);
key.second.push_back(surface.m_swapChain->m_extent.height); key.second.push_back(surface.m_swapChain->m_extent.height);
key.second.push_back(info.vertexAttributes); // key.second.push_back(info.layout); // FINDME!! KIPG!! HACK!!! Need to add a unique key generated from info.layout
key.second.push_back((int)info.topology);
// TODO - Add renderPass unique identifier to key // TODO - Add renderPass unique identifier to key
PipelineMap::iterator itr = m_pipelines.find(key); PipelineMap::iterator itr = m_pipelines.find(key);
if (itr != m_pipelines.end()) { if (itr != m_pipelines.end()) {
@@ -95,7 +94,7 @@ KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, const PipelineInf
shaders.push_back(shader); shaders.push_back(shader);
} }
KRPipeline* pipeline = new KRPipeline(*m_pContext, surface.m_deviceHandle, info.renderPass, surface.getDimensions(), surface.getDimensions(), info, info.shader_name->c_str(), shaders, info.vertexAttributes, info.topology); KRPipeline* pipeline = new KRPipeline(*m_pContext, surface.m_deviceHandle, info.renderPass, surface.getDimensions(), surface.getDimensions(), info, info.shader_name->c_str(), shaders, info.layout);
m_pipelines[key] = pipeline; m_pipelines[key] = pipeline;

View File

@@ -130,8 +130,7 @@ void KRAmbientZone::render(RenderInfo& ri)
info.spot_lights = &ri.spot_lights; info.spot_lights = &ri.spot_lights;
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditive; info.rasterMode = RasterMode::kAdditive;
info.topology = sphereModel->getTopology(); info.layout = sphereModel->getLayout(0);
info.vertexAttributes = sphereModel->getVertexAttributes();
KRPipeline* pPipeline = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pPipeline = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
if (pPipeline && pPipeline->bind(ri, sphereModelMatrix)) { if (pPipeline && pPipeline->bind(ri, sphereModelMatrix)) {

View File

@@ -152,8 +152,7 @@ void KRAudioSource::render(RenderInfo& ri)
info.spot_lights = &ri.spot_lights; info.spot_lights = &ri.spot_lights;
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditive; info.rasterMode = RasterMode::kAdditive;
info.topology = sphereModel->getTopology(); info.layout = sphereModel->getLayout(0);
info.vertexAttributes = sphereModel->getVertexAttributes();
KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
if (pShader && pShader->bind(ri, sphereModelMatrix)) { if (pShader && pShader->bind(ri, sphereModelMatrix)) {

View File

@@ -92,8 +92,7 @@ void KRBone::render(RenderInfo& ri)
info.spot_lights = &ri.spot_lights; info.spot_lights = &ri.spot_lights;
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditiveNoTest; info.rasterMode = RasterMode::kAdditiveNoTest;
info.topology = sphereModel->getTopology(); info.layout = sphereModel->getLayout(0);
info.vertexAttributes = sphereModel->getVertexAttributes();
KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
if (pShader && pShader->bind(ri, sphereModelMatrix)) { if (pShader && pShader->bind(ri, sphereModelMatrix)) {

View File

@@ -91,7 +91,15 @@ KRCamera::KRCamera(KRScene& scene, std::string name)
m_fade_color = Vector4::Zero(); m_fade_color = Vector4::Zero();
m_debug_text_vbo_data.init(m_pContext->getMeshManager(), &m_debug_text_vertices, nullptr, (1 << KRMesh::KRENGINE_ATTRIB_POSITION) | (1 << KRMesh::KRENGINE_ATTRIB_TEXCOORD0), true, KRMeshManager::KRVBOData::IMMEDIATE m_debug_text_vbo_layout = { };
m_debug_text_vbo_layout.topology = Topology::Triangles;
m_debug_text_vbo_layout.vertexSize = sizeof(DebugTextVertexData);
m_debug_text_vbo_layout.offsets[0] = offsetof(DebugTextVertexData, x);
m_debug_text_vbo_layout.offsets[1] = offsetof(DebugTextVertexData, u);
m_debug_text_vbo_layout.attributes[0] = { ComponentType::float32, DataType::vec3, Normalization::none, VertexAttribute::position };
m_debug_text_vbo_layout.attributes[1] = { ComponentType::float32, DataType::vec2, Normalization::none, VertexAttribute::texcoord };
m_debug_text_vbo_data.init(m_pContext->getMeshManager(), &m_debug_text_vertices, nullptr, &m_debug_text_vbo_layout, true, KRMeshManager::KRVBOData::IMMEDIATE
#if KRENGINE_DEBUG_GPU_LABELS #if KRENGINE_DEBUG_GPU_LABELS
, "Debug Text" , "Debug Text"
#endif #endif
@@ -161,6 +169,7 @@ void KRCamera::render(KRNode::RenderInfo& ri)
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kOpaqueNoDepthWrite; info.rasterMode = RasterMode::kOpaqueNoDepthWrite;
info.cullMode = CullMode::kCullNone; info.cullMode = CullMode::kCullNone;
info.layout = getContext().getMeshManager()->KRENGINE_VBO_DATA_2D_SQUARE_VERTICES.getLayout();
KRPipeline* pPipeline = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pPipeline = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
if (pPipeline && pPipeline->bind(ri, Matrix4())) { if (pPipeline && pPipeline->bind(ri, Matrix4())) {
@@ -616,8 +625,7 @@ void KRCamera::renderDebug(RenderInfo& ri)
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAlphaBlendNoTest; info.rasterMode = RasterMode::kAlphaBlendNoTest;
info.cullMode = CullMode::kCullNone; info.cullMode = CullMode::kCullNone;
info.vertexAttributes = (1 << KRMesh::KRENGINE_ATTRIB_POSITION) | (1 << KRMesh::KRENGINE_ATTRIB_TEXCOORD0); info.layout = m_debug_text_vbo_data.getLayout();;
info.topology = Topology::Triangles;
KRPipeline* fontShader = m_pContext->getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* fontShader = m_pContext->getPipelineManager()->getPipeline(*ri.surface, info);
if (fontShader && fontShader->bind(ri, Matrix4())) { if (fontShader && fontShader->bind(ri, Matrix4())) {

View File

@@ -121,6 +121,7 @@ private:
mimir::Block m_debug_text_vertices; mimir::Block m_debug_text_vertices;
KRMeshManager::KRVBOData m_debug_text_vbo_data; KRMeshManager::KRVBOData m_debug_text_vbo_data;
VertexBufferLayout m_debug_text_vbo_layout;
KRTextureBinding m_fontTexture; KRTextureBinding m_fontTexture;
KRNODE_PROPERTY(KRTextureBinding, m_skyBox, KRTexture::TEXTURE_USAGE_SKY_CUBE, "skybox"); KRNODE_PROPERTY(KRTextureBinding, m_skyBox, KRTexture::TEXTURE_USAGE_SKY_CUBE, "skybox");

View File

@@ -217,8 +217,7 @@ void KRCollider::render(RenderInfo& ri)
info.spot_lights = &ri.spot_lights; info.spot_lights = &ri.spot_lights;
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditive; info.rasterMode = RasterMode::kAdditive;
info.topology = m_model.val.get()->getTopology(); info.layout = m_model.val.get()->getLayout(0);
info.vertexAttributes = m_model.val.get()->getVertexAttributes();
KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info);

View File

@@ -159,9 +159,7 @@ void KRDirectionalLight::render(RenderInfo& ri)
info.directional_lights = &this_light; info.directional_lights = &this_light;
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditiveNoTest; info.rasterMode = RasterMode::kAdditiveNoTest;
info.layout = vertices.getLayout();
info.vertexAttributes = vertices.getVertexAttributes();
info.topology = Topology::TriangleStrips;
KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
if (pShader && pShader->bind(ri, getModelMatrix())) { // TODO: Need to pass in the light index to the shader if (pShader && pShader->bind(ri, getModelMatrix())) { // TODO: Need to pass in the light index to the shader

View File

@@ -238,8 +238,7 @@ void KRLight::render(RenderInfo& ri)
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditive; info.rasterMode = RasterMode::kAdditive;
info.cullMode = CullMode::kCullNone; info.cullMode = CullMode::kCullNone;
info.vertexAttributes = (1 << KRMesh::KRENGINE_ATTRIB_POSITION) | (1 << KRMesh::KRENGINE_ATTRIB_TEXCOORD0); info.layout = m_pContext->getMeshManager()->KRENGINE_VBO_DATA_RANDOM_PARTICLES.getLayout();
info.topology = Topology::Triangles;
KRPipeline* pParticleShader = m_pContext->getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pParticleShader = m_pContext->getPipelineManager()->getPipeline(*ri.surface, info);
if (pParticleShader && pParticleShader->bind(ri, getParticleModelMatrix(*ri.viewport))) { // TODO: Pass light index to shader if (pParticleShader && pParticleShader->bind(ri, getParticleModelMatrix(*ri.viewport))) { // TODO: Pass light index to shader
m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &m_pContext->getMeshManager()->KRENGINE_VBO_DATA_RANDOM_PARTICLES, 1.0f); m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &m_pContext->getMeshManager()->KRENGINE_VBO_DATA_RANDOM_PARTICLES, 1.0f);
@@ -278,8 +277,7 @@ void KRLight::render(RenderInfo& ri)
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditive; info.rasterMode = RasterMode::kAdditive;
info.cullMode = CullMode::kCullNone; info.cullMode = CullMode::kCullNone;
info.vertexAttributes = (1 << KRMesh::KRENGINE_ATTRIB_POSITION); info.layout = m_pContext->getMeshManager()->KRENGINE_VBO_DATA_VOLUMETRIC_LIGHTING.getLayout();
info.topology = Topology::Triangles;
KRPipeline* pFogShader = m_pContext->getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pFogShader = m_pContext->getPipelineManager()->getPipeline(*ri.surface, info);
if (pFogShader) { if (pFogShader) {
@@ -313,8 +311,7 @@ void KRLight::render(RenderInfo& ri)
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditive; info.rasterMode = RasterMode::kAdditive;
info.cullMode = CullMode::kCullNone; info.cullMode = CullMode::kCullNone;
info.topology = sphereModel->getTopology(); info.layout = sphereModel->getLayout(0);
info.vertexAttributes = sphereModel->getVertexAttributes();
KRPipeline* pPipeline = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pPipeline = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
if (pPipeline && pPipeline->bind(ri, occlusion_test_sphere_matrix)) { if (pPipeline && pPipeline->bind(ri, occlusion_test_sphere_matrix)) {
@@ -363,9 +360,7 @@ void KRLight::render(RenderInfo& ri)
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditiveNoTest; info.rasterMode = RasterMode::kAdditiveNoTest;
info.cullMode = CullMode::kCullNone; info.cullMode = CullMode::kCullNone;
info.vertexAttributes = vertices.getVertexAttributes(); info.layout = vertices.getLayout();
info.topology = Topology::TriangleStrips;
KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
if (pShader && pShader->bind(ri, getModelMatrix())) { if (pShader && pShader->bind(ri, getModelMatrix())) {

View File

@@ -98,8 +98,7 @@ void KRParticleSystemNewtonian::render(RenderInfo& ri)
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditive; info.rasterMode = RasterMode::kAdditive;
info.cullMode = CullMode::kCullNone; info.cullMode = CullMode::kCullNone;
info.vertexAttributes = (1 << KRMesh::KRENGINE_ATTRIB_POSITION) | (1 << KRMesh::KRENGINE_ATTRIB_TEXCOORD0); info.layout = m_pContext->getMeshManager()->KRENGINE_VBO_DATA_RANDOM_PARTICLES.getLayout();
info.topology = Topology::Triangles;
KRPipeline* pParticleShader = m_pContext->getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pParticleShader = m_pContext->getPipelineManager()->getPipeline(*ri.surface, info);
if (pParticleShader && pParticleShader->bind(ri, getModelMatrix())) { if (pParticleShader && pParticleShader->bind(ri, getModelMatrix())) {

View File

@@ -101,6 +101,13 @@ void KRPointLight::render(RenderInfo& ri)
bool bInsideLight = view_light_position.sqrMagnitude() <= (influence_radius + ri.camera->settings.getPerspectiveNearZ()) * (influence_radius + ri.camera->settings.getPerspectiveNearZ()); bool bInsideLight = view_light_position.sqrMagnitude() <= (influence_radius + ri.camera->settings.getPerspectiveNearZ()) * (influence_radius + ri.camera->settings.getPerspectiveNearZ());
KRMeshManager::KRVBOData* vboData = &m_pContext->getMeshManager()->KRENGINE_VBO_DATA_2D_SQUARE_VERTICES;
if (!bInsideLight) {
// FINDME!!! KIPG!!! HACK!! Create a KRVBOData for the sphere vertices...
// 1 << KRMesh::KRENGINE_ATTRIB_POSITION;
// bInsideLight ? Topology::TriangleStrips : Topology::Triangles;
}
std::string shader_name(bVisualize ? "visualize_overlay" : (bInsideLight ? "light_point_inside" : "light_point")); std::string shader_name(bVisualize ? "visualize_overlay" : (bInsideLight ? "light_point_inside" : "light_point"));
PipelineInfo info{}; PipelineInfo info{};
info.shader_name = &shader_name; info.shader_name = &shader_name;
@@ -112,8 +119,7 @@ void KRPointLight::render(RenderInfo& ri)
} else { } else {
info.rasterMode = bVisualize ? RasterMode::kAdditive : RasterMode::kAlphaBlend; info.rasterMode = bVisualize ? RasterMode::kAdditive : RasterMode::kAlphaBlend;
} }
info.vertexAttributes = bInsideLight ? m_pContext->getMeshManager()->KRENGINE_VBO_DATA_2D_SQUARE_VERTICES.getVertexAttributes() : 1 << KRMesh::KRENGINE_ATTRIB_POSITION; info.layout = vboData->getLayout();
info.topology = bInsideLight ? Topology::TriangleStrips : Topology::Triangles;
KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
if (pShader && pShader->bind(ri, sphereModelMatrix)) { // TODO: Pass light index to shader if (pShader && pShader->bind(ri, sphereModelMatrix)) { // TODO: Pass light index to shader

View File

@@ -127,8 +127,7 @@ void KRReverbZone::render(RenderInfo& ri)
info.spot_lights = &ri.spot_lights; info.spot_lights = &ri.spot_lights;
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAlphaBlend; info.rasterMode = RasterMode::kAlphaBlend;
info.topology = sphereModel->getTopology(); info.layout = sphereModel->getLayout(0);
info.vertexAttributes = sphereModel->getVertexAttributes();
KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
if (pShader && pShader->bind(ri, sphereModelMatrix)) { if (pShader && pShader->bind(ri, sphereModelMatrix)) {

View File

@@ -132,8 +132,7 @@ void KRSprite::render(RenderInfo& ri)
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.rasterMode = RasterMode::kAdditive; info.rasterMode = RasterMode::kAdditive;
info.cullMode = CullMode::kCullNone; info.cullMode = CullMode::kCullNone;
info.vertexAttributes = vertices.getVertexAttributes(); info.layout = vertices.getLayout();
info.topology = Topology::TriangleStrips;
KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
if (pShader && pShader->bind(ri, getModelMatrix())) { if (pShader && pShader->bind(ri, getModelMatrix())) {

View File

@@ -743,7 +743,7 @@ kraken_stream_level KRMaterial::getStreamLevel()
return stream_level; return stream_level;
} }
bool KRMaterial::bind(KRNode::RenderInfo& ri, Topology topology, __uint32_t vertexAttributes, CullMode cullMode, const std::vector<KRBone*>& bones, const std::vector<Matrix4>& bind_poses, const Matrix4& matModel, KRTexture* pLightMap, float lod_coverage) bool KRMaterial::bind(KRNode::RenderInfo& ri, const VertexBufferLayout* layout, CullMode cullMode, const std::vector<KRBone*>& bones, const std::vector<Matrix4>& bind_poses, const Matrix4& matModel, KRTexture* pLightMap, float lod_coverage)
{ {
bool bLightMap = pLightMap && ri.camera->settings.bEnableLightMap; bool bLightMap = pLightMap && ri.camera->settings.bEnableLightMap;
@@ -784,8 +784,7 @@ bool KRMaterial::bind(KRNode::RenderInfo& ri, Topology topology, __uint32_t vert
info.bAlphaTest = bAlphaTest; info.bAlphaTest = bAlphaTest;
info.rasterMode = bAlphaBlend ? RasterMode::kAlphaBlend : RasterMode::kOpaque; info.rasterMode = bAlphaBlend ? RasterMode::kAlphaBlend : RasterMode::kOpaque;
info.renderPass = ri.renderPass; info.renderPass = ri.renderPass;
info.topology = topology; info.layout = layout;
info.vertexAttributes = vertexAttributes;
info.cullMode = cullMode; info.cullMode = cullMode;
KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info);
if (pShader == nullptr) { if (pShader == nullptr) {

View File

@@ -52,6 +52,7 @@ enum class Topology : __uint8_t;
class KRTextureManager; class KRTextureManager;
class KRContext; class KRContext;
class KRSurface; class KRSurface;
class VertexBufferLayout;
class KRMaterial class KRMaterial
: public KRResource : public KRResource
@@ -131,7 +132,7 @@ public:
bool isTransparent(); bool isTransparent();
bool bind(KRNode::RenderInfo& ri, Topology topology, __uint32_t vertexAttributes, CullMode cullMode, const std::vector<KRBone*>& bones, const std::vector<hydra::Matrix4>& bind_poses, const hydra::Matrix4& matModel, KRTexture* pLightMap, float lod_coverage = 0.0f); bool bind(KRNode::RenderInfo& ri, const VertexBufferLayout* layout, CullMode cullMode, const std::vector<KRBone*>& bones, const std::vector<hydra::Matrix4>& bind_poses, const hydra::Matrix4& matModel, KRTexture* pLightMap, float lod_coverage = 0.0f);
bool needsVertexTangents(); bool needsVertexTangents();

File diff suppressed because it is too large Load Diff

View File

@@ -59,81 +59,6 @@ class KRMaterial;
class KRNode; class KRNode;
class KRRenderPass; class KRRenderPass;
enum class Topology : uint8_t
{
Points = 0,
LineStrips,
Lines,
Triangles,
TriangleStrips,
TriangleFans
};
enum class ComponentType : uint8_t
{
empty = 0,
int8,
uint8,
int16,
uint16,
int32,
uint32,
int64,
uint64,
float16,
float32,
float64
};
static constexpr std::array<int, 12> ComponentSize =
{
0, // empty
1, // int8
1, // uint8
2, // int16
2, // uint16
4, // int32
4, // uint32
8, // int64
8, // uint64
2, // float16
4, // float32
8 // float64
};
enum class DataType : uint8_t
{
scalar = 0,
vec2,
vec3,
vec4,
mat2,
mat3,
mat4
};
enum class VertexAttribute : uint8_t
{
position = 0,
normal,
tangent,
texcoord,
color,
joints,
weights
};
static constexpr const std::initializer_list<VertexAttribute> VertexAttributeList
{
VertexAttribute::position,
VertexAttribute::normal,
VertexAttribute::tangent,
VertexAttribute::texcoord,
VertexAttribute::color,
VertexAttribute::joints,
VertexAttribute::weights
};
class KRMesh : public KRResource class KRMesh : public KRResource
{ {
@@ -149,62 +74,14 @@ public:
bool hasTransparency(); bool hasTransparency();
typedef struct struct PrimitiveInfo
{ {
ComponentType component; VertexBufferLayout layout;
DataType type;
VertexAttribute attribute;
bool normalized : 1;
} AttributeInfo;
static_assert(sizeof(AttributeInfo) == 4);
static const int kMaxAttributes = 32;
typedef struct
{
AttributeInfo attributes[kMaxAttributes];
int64_t vertexCount; int64_t vertexCount;
int64_t indexCount; int64_t indexCount;
Topology topology; };
} PrimitiveInfo;
typedef enum struct mesh_info
{
KRENGINE_ATTRIB_POSITION = 0,
KRENGINE_ATTRIB_NORMAL,
KRENGINE_ATTRIB_TANGENT,
KRENGINE_ATTRIB_COLOR0,
KRENGINE_ATTRIB_COLOR1,
KRENGINE_ATTRIB_COLOR2,
KRENGINE_ATTRIB_COLOR3,
KRENGINE_ATTRIB_COLOR4,
KRENGINE_ATTRIB_COLOR5,
KRENGINE_ATTRIB_COLOR6,
KRENGINE_ATTRIB_COLOR7,
KRENGINE_ATTRIB_TEXCOORD0,
KRENGINE_ATTRIB_TEXCOORD1,
KRENGINE_ATTRIB_TEXCOORD2,
KRENGINE_ATTRIB_TEXCOORD3,
KRENGINE_ATTRIB_TEXCOORD4,
KRENGINE_ATTRIB_TEXCOORD5,
KRENGINE_ATTRIB_TEXCOORD6,
KRENGINE_ATTRIB_TEXCOORD7,
KRENGINE_ATTRIB_BONEINDEXES,
KRENGINE_ATTRIB_BONEWEIGHTS,
KRENGINE_ATTRIB_VERTEX_SHORT,
KRENGINE_ATTRIB_NORMAL_SHORT,
KRENGINE_ATTRIB_TANGENT_SHORT,
KRENGINE_ATTRIB_TEXCOORD0_SHORT,
KRENGINE_ATTRIB_TEXCOORD1_SHORT,
KRENGINE_ATTRIB_TEXCOORD2_SHORT,
KRENGINE_ATTRIB_TEXCOORD3_SHORT,
KRENGINE_ATTRIB_TEXCOORD4_SHORT,
KRENGINE_ATTRIB_TEXCOORD5_SHORT,
KRENGINE_ATTRIB_TEXCOORD6_SHORT,
KRENGINE_ATTRIB_TEXCOORD7_SHORT,
KRENGINE_NUM_ATTRIBUTES
} vertex_attrib_t;
typedef struct
{ {
Topology format; Topology format;
std::vector<hydra::Vector3> vertices; std::vector<hydra::Vector3> vertices;
@@ -221,7 +98,7 @@ public:
std::vector<std::vector<int> > bone_indexes; std::vector<std::vector<int> > bone_indexes;
std::vector<hydra::Matrix4> bone_bind_poses; std::vector<hydra::Matrix4> bone_bind_poses;
std::vector<std::vector<float> > bone_weights; std::vector<std::vector<float> > bone_weights;
} mesh_info; };
void render(KRNode::RenderInfo& ri, const std::string& object_name, const hydra::Matrix4& matModel, KRTexture* pLightMap, const std::vector<KRBone*>& bones, float lod_coverage = 0.0f); void render(KRNode::RenderInfo& ri, const std::string& object_name, const hydra::Matrix4& matModel, KRTexture* pLightMap, const std::vector<KRBone*>& bones, float lod_coverage = 0.0f);
@@ -298,13 +175,11 @@ public:
static bool lod_sort_predicate(const KRMesh* m1, const KRMesh* m2); static bool lod_sort_predicate(const KRMesh* m1, const KRMesh* m2);
bool has_vertex_attribute(vertex_attrib_t attribute_type) const;
static bool has_vertex_attribute(int vertex_attrib_flags, vertex_attrib_t attribute_type);
int getSubmeshCount() const; int getSubmeshCount() const;
int getVertexCount(int submesh) const; int getVertexCount(int submesh) const;
int getIndexCount(int submesh) const; int getIndexCount(int submesh) const;
__uint32_t getVertexAttributes() const; const VertexBufferLayout* getLayout(int submesh) const;
int getVertexIndex(int submesh, int index) const; int getVertexIndex(int submesh, int index) const;
hydra::Vector3 getVertexPosition(int index) const; hydra::Vector3 getVertexPosition(int index) const;
@@ -312,20 +187,34 @@ public:
hydra::Vector3 getVertexTangent(int index) const; hydra::Vector3 getVertexTangent(int index) const;
hydra::Vector2 getVertexTexCoord(int set, int index) const; hydra::Vector2 getVertexTexCoord(int set, int index) const;
hydra::Vector4 getVertexColor(int set, int index) const; hydra::Vector4 getVertexColor(int set, int index) const;
int getBoneIndex(int index, int weight_index) const;
float getBoneWeight(int index, int weight_index) const; static int getAttributeIndex(const PrimitiveInfo& primitive, VertexAttribute attribute, int index);
void setVertexAttribute(int vertexIndex, int attributeIndex, float val);
void setVertexAttribute(int vertexIndex, int attributeIndex, hydra::Vector2 val);
void setVertexAttribute(int vertexIndex, int attributeIndex, hydra::Vector3 val);
void setVertexAttribute(int vertexIndex, int attributeIndex, hydra::Vector4 val);
void setVertexAttribute(int vertexIndex, int attributeIndex, hydra::Matrix2 val);
void setVertexAttribute(int vertexIndex, int attributeIndex, hydra::Matrix4 val);
void getVertexAttribute(int vertexIndex, int attributeIndex, float* val) const;
void getVertexAttribute(int vertexIndex, int attributeIndex, hydra::Vector2* val) const;
void getVertexAttribute(int vertexIndex, int attributeIndex, hydra::Vector3* val) const;
void getVertexAttribute(int vertexIndex, int attributeIndex, hydra::Vector4* val) const;
void getVertexAttribute(int vertexIndex, int attributeIndex, hydra::Matrix2* val) const;
void getVertexAttribute(int vertexIndex, int attributeIndex, hydra::Matrix4* val) const;
void setVertexPosition(int index, const hydra::Vector3& v); void setVertexPosition(int index, const hydra::Vector3& v);
void setVertexNormal(int index, const hydra::Vector3& v); void setVertexNormal(int index, const hydra::Vector3& v);
void setVertexTangent(int index, const hydra::Vector3& v); void setVertexTangent(int index, const hydra::Vector3& v);
void setVertexTexCoord(int set, int index, const hydra::Vector2& v); void setVertexTexCoord(int index, int set, const hydra::Vector2& v);
void setVertexColor(int set, int index, const hydra::Vector4& v); void setVertexColor(int index, int set, const hydra::Vector4& v);
void setBoneIndex(int index, int weight_index, int bone_index);
void setBoneWeight(int index, int weight_index, float bone_weight);
static size_t VertexSizeForAttributes(__int32_t vertex_attrib_flags); int getBoneIndex(int index, int weight_index) const;
static size_t AttributeOffset(__int32_t vertex_attrib, __int32_t vertex_attrib_flags); void setBoneIndex(int index, int weight_index, int bone_index);
static VkFormat AttributeVulkanFormat(__int32_t vertex_attrib);
float getBoneWeight(int index, int weight_index) const;
void setBoneWeight(int index, int weight_index, float weight);
static VkFormat AttributeVulkanFormat(const VertexAttributeInfo& attribute);
int getBoneCount(); int getBoneCount();
char* getBoneName(int bone_index); char* getBoneName(int bone_index);
@@ -367,7 +256,6 @@ private:
{ {
char szTag[16]; char szTag[16];
PrimitiveInfo primitive; PrimitiveInfo primitive;
int32_t vertex_attrib_flags;
int32_t submesh_count; int32_t submesh_count;
int32_t bone_count; int32_t bone_count;
hydra::AABB extents; // Axis aligned bounding box, in model's coordinate space hydra::AABB extents; // Axis aligned bounding box, in model's coordinate space
@@ -378,14 +266,9 @@ private:
static_assert(sizeof(pack_header) == 512); static_assert(sizeof(pack_header) == 512);
vector<Submesh> m_submeshes; vector<Submesh> m_submeshes;
int m_vertex_attribute_offset[KRENGINE_NUM_ATTRIBUTES];
int m_vertex_size;
void updateAttributeOffsets();
void setName(const std::string name); void setName(const std::string name);
pack_material* getSubmesh(int mesh_index) const; pack_material* getSubmesh(int mesh_index) const;
unsigned char* getVertexData() const; unsigned char* getVertexData() const;
size_t getVertexDataOffset() const; size_t getVertexDataOffset() const;
@@ -399,7 +282,7 @@ private:
void getIndexedRange(int index_group, int& start_index_offset, int& start_vertex_offset, int& index_count, int& vertex_count) const; void getIndexedRange(int index_group, int& start_index_offset, int& start_vertex_offset, int& index_count, int& vertex_count) const;
void releaseData(); void releaseData(bool includeMainDatablock = true);
void createDataBlocks(KRMeshManager::KRVBOData::vbo_type t); void createDataBlocks(KRMeshManager::KRVBOData::vbo_type t);

View File

@@ -76,14 +76,18 @@ void KRMeshManager::init()
1.0, 1.0,-1.0, 1.0, 1.0,-1.0,
-1.0, 1.0,-1.0 -1.0, 1.0,-1.0
}; };
KRENGINE_VBO_3D_CUBE_LAYOUT = { };
KRENGINE_VBO_3D_CUBE_LAYOUT.topology = Topology::Triangles;
KRENGINE_VBO_3D_CUBE_LAYOUT.vertexSize = sizeof(float) * 3;
KRENGINE_VBO_3D_CUBE_LAYOUT.offsets[0] = 0;
KRENGINE_VBO_3D_CUBE_LAYOUT.attributes[0] = { ComponentType::float32, DataType::vec3, Normalization::none, VertexAttribute::position };
KRENGINE_VBO_3D_CUBE_ATTRIBS = (1 << KRMesh::KRENGINE_ATTRIB_POSITION);
KRENGINE_VBO_3D_CUBE_VERTICES.expand(sizeof(float) * 3 * 14); KRENGINE_VBO_3D_CUBE_VERTICES.expand(sizeof(float) * 3 * 14);
KRENGINE_VBO_3D_CUBE_VERTICES.lock(); KRENGINE_VBO_3D_CUBE_VERTICES.lock();
memcpy(KRENGINE_VBO_3D_CUBE_VERTICES.getStart(), _KRENGINE_VBO_3D_CUBE_VERTEX_DATA, sizeof(float) * 3 * 14); memcpy(KRENGINE_VBO_3D_CUBE_VERTICES.getStart(), _KRENGINE_VBO_3D_CUBE_VERTEX_DATA, sizeof(float) * 3 * 14);
KRENGINE_VBO_3D_CUBE_VERTICES.unlock(); KRENGINE_VBO_3D_CUBE_VERTICES.unlock();
KRENGINE_VBO_DATA_3D_CUBE_VERTICES.init(this, &KRENGINE_VBO_3D_CUBE_VERTICES, nullptr, KRENGINE_VBO_3D_CUBE_ATTRIBS, false, KRVBOData::CONSTANT KRENGINE_VBO_DATA_3D_CUBE_VERTICES.init(this, &KRENGINE_VBO_3D_CUBE_VERTICES, nullptr, &KRENGINE_VBO_3D_CUBE_LAYOUT, false, KRVBOData::CONSTANT
#if KRENGINE_DEBUG_GPU_LABELS #if KRENGINE_DEBUG_GPU_LABELS
, "Cube Mesh [built-in]" , "Cube Mesh [built-in]"
#endif #endif
@@ -98,13 +102,20 @@ void KRMeshManager::init()
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f 1.0f, 1.0f, 0.0f, 1.0f, 1.0f
}; };
KRENGINE_VBO_2D_SQUARE_ATTRIBS = (1 << KRMesh::KRENGINE_ATTRIB_POSITION) | (1 << KRMesh::KRENGINE_ATTRIB_TEXCOORD0); KRENGINE_VBO_2D_SQUARE_LAYOUT = { };
KRENGINE_VBO_2D_SQUARE_LAYOUT.topology = Topology::TriangleStrips;
KRENGINE_VBO_2D_SQUARE_LAYOUT.vertexSize = sizeof(float) * 5;
KRENGINE_VBO_2D_SQUARE_LAYOUT.offsets[0] = 0;
KRENGINE_VBO_2D_SQUARE_LAYOUT.offsets[1] = sizeof(float) * 3;
KRENGINE_VBO_2D_SQUARE_LAYOUT.attributes[0] = { ComponentType::float32, DataType::vec3, Normalization::none, VertexAttribute::position };
KRENGINE_VBO_2D_SQUARE_LAYOUT.attributes[1] = { ComponentType::float32, DataType::vec2, Normalization::none, VertexAttribute::texcoord };
KRENGINE_VBO_2D_SQUARE_VERTICES.expand(sizeof(float) * 5 * 4); KRENGINE_VBO_2D_SQUARE_VERTICES.expand(sizeof(float) * 5 * 4);
KRENGINE_VBO_2D_SQUARE_VERTICES.lock(); KRENGINE_VBO_2D_SQUARE_VERTICES.lock();
memcpy(KRENGINE_VBO_2D_SQUARE_VERTICES.getStart(), _KRENGINE_VBO_2D_SQUARE_VERTEX_DATA, sizeof(float) * 5 * 4); memcpy(KRENGINE_VBO_2D_SQUARE_VERTICES.getStart(), _KRENGINE_VBO_2D_SQUARE_VERTEX_DATA, sizeof(float) * 5 * 4);
KRENGINE_VBO_2D_SQUARE_VERTICES.unlock(); KRENGINE_VBO_2D_SQUARE_VERTICES.unlock();
KRENGINE_VBO_DATA_2D_SQUARE_VERTICES.init(this, &KRENGINE_VBO_2D_SQUARE_VERTICES, nullptr, KRENGINE_VBO_2D_SQUARE_ATTRIBS, false, KRVBOData::CONSTANT KRENGINE_VBO_DATA_2D_SQUARE_VERTICES.init(this, &KRENGINE_VBO_2D_SQUARE_VERTICES, nullptr, &KRENGINE_VBO_2D_SQUARE_LAYOUT, false, KRVBOData::CONSTANT
#if KRENGINE_DEBUG_GPU_LABELS #if KRENGINE_DEBUG_GPU_LABELS
, "Square Mesh [built-in]" , "Square Mesh [built-in]"
#endif #endif
@@ -344,10 +355,15 @@ void KRMeshManager::initVolumetricLightingVertexes()
vertex_data[iVertex].vertex.y = 1.0f; vertex_data[iVertex].vertex.y = 1.0f;
vertex_data[iVertex].vertex.z = (float)iPlane; vertex_data[iVertex].vertex.z = (float)iPlane;
iVertex++; iVertex++;
} }
KRENGINE_VBO_DATA_VOLUMETRIC_LIGHTING.init(this, &m_volumetricLightingVertexData, nullptr, (1 << KRMesh::KRENGINE_ATTRIB_POSITION), false, KRVBOData::CONSTANT m_volumetricLightingVertexLayout = { };
m_volumetricLightingVertexLayout.topology = Topology::Triangles;
m_volumetricLightingVertexLayout.vertexSize = sizeof(VolumetricLightingVertexData);
m_volumetricLightingVertexLayout.offsets[0] = offsetof(VolumetricLightingVertexData, vertex);
m_volumetricLightingVertexLayout.attributes[0] = { ComponentType::float32, DataType::vec3, Normalization::none, VertexAttribute::position };
KRENGINE_VBO_DATA_VOLUMETRIC_LIGHTING.init(this, &m_volumetricLightingVertexData, nullptr, &m_volumetricLightingVertexLayout, false, KRVBOData::CONSTANT
#if KRENGINE_DEBUG_GPU_LABELS #if KRENGINE_DEBUG_GPU_LABELS
, "Volumetric Lighting Planes [built-in]" , "Volumetric Lighting Planes [built-in]"
#endif #endif
@@ -393,7 +409,15 @@ void KRMeshManager::initRandomParticles()
iVertex++; iVertex++;
} }
KRENGINE_VBO_DATA_RANDOM_PARTICLES.init(this, &m_randomParticleVertexData, nullptr, (1 << KRMesh::KRENGINE_ATTRIB_POSITION) | (1 << KRMesh::KRENGINE_ATTRIB_TEXCOORD0), false, KRVBOData::CONSTANT m_randomParticleVertexLayout = { };
m_randomParticleVertexLayout.topology = Topology::Triangles;
m_randomParticleVertexLayout.vertexSize = sizeof(RandomParticleVertexData);
m_randomParticleVertexLayout.offsets[0] = offsetof(RandomParticleVertexData, vertex);
m_randomParticleVertexLayout.offsets[1] = offsetof(RandomParticleVertexData, texcoord0);
m_randomParticleVertexLayout.attributes[0] = { ComponentType::float32, DataType::vec3, Normalization::none, VertexAttribute::position };
m_randomParticleVertexLayout.attributes[1] = { ComponentType::float32, DataType::vec2, Normalization::none, VertexAttribute::texcoord };
KRENGINE_VBO_DATA_RANDOM_PARTICLES.init(this, &m_randomParticleVertexData, nullptr, &m_randomParticleVertexLayout, false, KRVBOData::CONSTANT
#if KRENGINE_DEBUG_GPU_LABELS #if KRENGINE_DEBUG_GPU_LABELS
, "Random Particles [built-in]" , "Random Particles [built-in]"
#endif #endif
@@ -441,7 +465,7 @@ KRMeshManager::KRVBOData::KRVBOData()
m_type = STREAMING; m_type = STREAMING;
m_data = NULL; m_data = NULL;
m_index_data = NULL; m_index_data = NULL;
m_vertex_attrib_flags = 0; m_layout = NULL;
m_size = 0; m_size = 0;
m_last_frame_used = 0; m_last_frame_used = 0;
@@ -450,7 +474,7 @@ KRMeshManager::KRVBOData::KRVBOData()
memset(m_allocations, 0, sizeof(AllocationInfo) * KRENGINE_MAX_GPU_COUNT); memset(m_allocations, 0, sizeof(AllocationInfo) * KRENGINE_MAX_GPU_COUNT);
} }
KRMeshManager::KRVBOData::KRVBOData(KRMeshManager* manager, Block* data, Block* index_data, int vertex_attrib_flags, bool static_vbo, vbo_type t KRMeshManager::KRVBOData::KRVBOData(KRMeshManager* manager, Block* data, Block* index_data, const VertexBufferLayout* layout, bool static_vbo, vbo_type t
#if KRENGINE_DEBUG_GPU_LABELS #if KRENGINE_DEBUG_GPU_LABELS
, const char* debug_label , const char* debug_label
#endif #endif
@@ -460,14 +484,14 @@ KRMeshManager::KRVBOData::KRVBOData(KRMeshManager* manager, Block* data, Block*
memset(m_allocations, 0, sizeof(AllocationInfo) * KRENGINE_MAX_GPU_COUNT); memset(m_allocations, 0, sizeof(AllocationInfo) * KRENGINE_MAX_GPU_COUNT);
m_is_vbo_loaded = false; m_is_vbo_loaded = false;
m_is_vbo_ready = false; m_is_vbo_ready = false;
init(manager, data, index_data, vertex_attrib_flags, static_vbo, t init(manager, data, index_data, layout, static_vbo, t
#if KRENGINE_DEBUG_GPU_LABELS #if KRENGINE_DEBUG_GPU_LABELS
, debug_label , debug_label
#endif #endif
); );
} }
void KRMeshManager::KRVBOData::init(KRMeshManager* manager, Block* data, Block* index_data, int vertex_attrib_flags, bool static_vbo, vbo_type t void KRMeshManager::KRVBOData::init(KRMeshManager* manager, Block* data, Block* index_data, const VertexBufferLayout* layout, bool static_vbo, vbo_type t
#if KRENGINE_DEBUG_GPU_LABELS #if KRENGINE_DEBUG_GPU_LABELS
, const char* debug_label , const char* debug_label
#endif #endif
@@ -481,7 +505,7 @@ void KRMeshManager::KRVBOData::init(KRMeshManager* manager, Block* data, Block*
m_static_vbo = static_vbo; m_static_vbo = static_vbo;
m_data = data; m_data = data;
m_index_data = index_data; m_index_data = index_data;
m_vertex_attrib_flags = vertex_attrib_flags; m_layout = layout;
m_size = m_data->getSize(); m_size = m_data->getSize();
if (m_index_data != NULL) { if (m_index_data != NULL) {
@@ -681,7 +705,7 @@ VkBuffer& KRMeshManager::KRVBOData::getIndexBuffer()
} }
uint32_t KRMeshManager::KRVBOData::getVertexAttributes() const VertexBufferLayout* KRMeshManager::KRVBOData::getLayout() const
{ {
return m_vertex_attrib_flags; return m_layout;
} }

View File

@@ -40,8 +40,132 @@
class KRContext; class KRContext;
class KRMesh; class KRMesh;
class VertexBufferLayout;
enum RenderPassType : uint8_t; enum RenderPassType : uint8_t;
enum class Topology : uint8_t
{
Points = 0,
LineStrips,
Lines,
Triangles,
TriangleStrips,
TriangleFans
};
enum class ComponentType : uint8_t
{
empty = 0,
int8,
uint8,
int16,
uint16,
int32,
uint32,
int64,
uint64,
float16,
float32,
float64
};
static constexpr std::array<int, 12> ComponentSize =
{
0, // empty
1, // int8
1, // uint8
2, // int16
2, // uint16
4, // int32
4, // uint32
8, // int64
8, // uint64
2, // float16
4, // float32
8 // float64
};
enum class DataType : uint8_t
{
scalar = 0,
vec2,
vec3,
vec4,
mat2,
mat3,
mat4
};
static constexpr std::array<int, 7> DataTypeComponentCount =
{
1, // scalar
2, // vec2
3, // vec3
4, // vec4
2 * 2, // mat2
3 * 3, // mat3
4 * 4 // mat4
};
enum class Normalization : uint8_t
{
none = 0,
scaled,
normalized,
srgb
};
enum class VertexAttribute : uint8_t
{
position = 0,
normal,
tangent,
texcoord,
color,
joints,
weights
};
static constexpr std::array<const char*, 7> VertexAttributeNames =
{
"vertex_position",
"vertex_normal",
"vertex_tangent",
"vertex_texcoord",
"vertex_color",
"vertex_joints",
"vertex_weights"
};
static constexpr const std::initializer_list<VertexAttribute> VertexAttributeList
{
VertexAttribute::position,
VertexAttribute::normal,
VertexAttribute::tangent,
VertexAttribute::texcoord,
VertexAttribute::color,
VertexAttribute::joints,
VertexAttribute::weights
};
struct VertexAttributeInfo
{
ComponentType component;
DataType type;
Normalization normalization;
VertexAttribute attribute;
};
static_assert(sizeof(VertexAttributeInfo) == 4);
static const int kMaxAttributes = 32;
struct VertexBufferLayout
{
VertexAttributeInfo attributes[kMaxAttributes];
int16_t offsets[kMaxAttributes];
int16_t vertexSize;
Topology topology;
};
class KRMeshManager : public KRResourceManager class KRMeshManager : public KRResourceManager
{ {
public: public:
@@ -84,12 +208,12 @@ public:
} vbo_type; } vbo_type;
KRVBOData(); KRVBOData();
KRVBOData(KRMeshManager* manager, mimir::Block* data, mimir::Block* index_data, int vertex_attrib_flags, bool static_vbo, vbo_type t KRVBOData(KRMeshManager* manager, mimir::Block* data, mimir::Block* index_data, const VertexBufferLayout* layout, bool static_vbo, vbo_type t
#if KRENGINE_DEBUG_GPU_LABELS #if KRENGINE_DEBUG_GPU_LABELS
, const char* debug_label , const char* debug_label
#endif #endif
); );
void init(KRMeshManager* manager, mimir::Block* data, mimir::Block* index_data, int vertex_attrib_flags, bool static_vbo, vbo_type t void init(KRMeshManager* manager, mimir::Block* data, mimir::Block* index_data, const VertexBufferLayout* layout, bool static_vbo, vbo_type t
#if KRENGINE_DEBUG_GPU_LABELS #if KRENGINE_DEBUG_GPU_LABELS
, const char* debug_label , const char* debug_label
#endif #endif
@@ -99,6 +223,7 @@ public:
mimir::Block* m_data; mimir::Block* m_data;
mimir::Block* m_index_data; mimir::Block* m_index_data;
const VertexBufferLayout* m_layout;
bool isVBOLoaded() bool isVBOLoaded()
{ {
@@ -140,11 +265,10 @@ public:
VkBuffer& getVertexBuffer(); VkBuffer& getVertexBuffer();
VkBuffer& getIndexBuffer(); VkBuffer& getIndexBuffer();
uint32_t getVertexAttributes(); const VertexBufferLayout* getLayout() const;
private: private:
KRMeshManager* m_manager; KRMeshManager* m_manager;
int m_vertex_attrib_flags;
long m_size; long m_size;
long m_last_frame_used; long m_last_frame_used;
@@ -212,9 +336,13 @@ public:
private: private:
mimir::Block KRENGINE_VBO_3D_CUBE_VERTICES; mimir::Block KRENGINE_VBO_3D_CUBE_VERTICES;
__int32_t KRENGINE_VBO_3D_CUBE_ATTRIBS; VertexBufferLayout KRENGINE_VBO_3D_CUBE_LAYOUT;
mimir::Block KRENGINE_VBO_2D_SQUARE_VERTICES; mimir::Block KRENGINE_VBO_2D_SQUARE_VERTICES;
__int32_t KRENGINE_VBO_2D_SQUARE_ATTRIBS; VertexBufferLayout KRENGINE_VBO_2D_SQUARE_LAYOUT;
mimir::Block m_randomParticleVertexData;
VertexBufferLayout m_randomParticleVertexLayout;
mimir::Block m_volumetricLightingVertexData;
VertexBufferLayout m_volumetricLightingVertexLayout;
unordered_map<std::string, KRMesh*> m_meshes; unordered_map<std::string, KRMesh*> m_meshes;
@@ -225,9 +353,6 @@ private:
std::vector<std::pair<float, KRVBOData*> > m_activeVBOs_streamer; std::vector<std::pair<float, KRVBOData*> > m_activeVBOs_streamer;
std::vector<std::pair<float, KRVBOData*> > m_activeVBOs_streamer_copy; std::vector<std::pair<float, KRVBOData*> > m_activeVBOs_streamer_copy;
mimir::Block m_randomParticleVertexData;
mimir::Block m_volumetricLightingVertexData;
long m_memoryTransferredThisFrame; long m_memoryTransferredThisFrame;
std::vector<draw_call_info> m_draw_calls; std::vector<draw_call_info> m_draw_calls;