From 00eeb8bcb4a6457b83880028ab661194f504c256 Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Mon, 28 Feb 2022 22:04:24 -0800 Subject: [PATCH] KRPipeline's are now created with the passed in KRMesh::model_format_t --- kraken/KRPipeline.cpp | 38 +++++++++++++++++------- kraken/KRPipeline.h | 4 ++- kraken/KRPipelineManager.cpp | 5 ++-- kraken/KRPipelineManager.h | 3 +- kraken/KRPresentationThread.cpp | 4 +-- standard_assets/shaders/vulkan_test.vert | 11 ++----- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/kraken/KRPipeline.cpp b/kraken/KRPipeline.cpp index 354bf04..c102cd2 100644 --- a/kraken/KRPipeline.cpp +++ b/kraken/KRPipeline.cpp @@ -109,7 +109,7 @@ const char *KRPipeline::KRENGINE_UNIFORM_NAMES[] = { "fade_color", // KRENGINE_UNIFORM_FADE_COLOR }; -KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const char* szKey, const std::vector& shaders, uint32_t vertexAttributes) +KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const char* szKey, const std::vector& shaders, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat) : KRContextObject(context) , m_iProgram(0) // not used for Vulkan { @@ -160,24 +160,32 @@ KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const char* szKey SpvReflectInterfaceVariable& input_var = *reflection->input_variables[i]; if (strcmp(input_var.name, "vertex_position") == 0) { attribute_locations[KRMesh::KRENGINE_ATTRIB_VERTEX] = input_var.location + 1; - } else if (strcmp(input_var.name, "vertex_normal") == 0) { + } + else if (strcmp(input_var.name, "vertex_normal") == 0) { attribute_locations[KRMesh::KRENGINE_ATTRIB_NORMAL] = input_var.location + 1; - } else if (strcmp(input_var.name, "vertex_tangent") == 0) { + } + 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_uv") == 0) { + } + else if (strcmp(input_var.name, "vertex_uv") == 0) { attribute_locations[KRMesh::KRENGINE_ATTRIB_TEXUVA] = input_var.location + 1; - } else if (strcmp(input_var.name, "vertex_lightmap_uv") == 0) { + } + else if (strcmp(input_var.name, "vertex_lightmap_uv") == 0) { attribute_locations[KRMesh::KRENGINE_ATTRIB_TEXUVB] = input_var.location + 1; - } else if (strcmp(input_var.name, "bone_indexes") == 0) { + } + 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) { + } + else if (strcmp(input_var.name, "bone_weights") == 0) { attribute_locations[KRMesh::KRENGINE_ATTRIB_BONEWEIGHTS] = input_var.location + 1; } } - } else if (shader->getSubExtension().compare("frag") == 0) { + } + else if (shader->getSubExtension().compare("frag") == 0) { stageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT; - } else { + } + else { // failed! TODO - Error handling } stageInfo.module = shaderModule; @@ -213,7 +221,17 @@ KRPipeline::KRPipeline(KRContext& context, KRSurface& surface, const char* szKey VkPipelineInputAssemblyStateCreateInfo inputAssembly{}; inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; - inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + switch (modelFormat) { + case KRMesh::model_format_t::KRENGINE_MODEL_FORMAT_INDEXED_TRIANGLES: + case KRMesh::model_format_t::KRENGINE_MODEL_FORMAT_TRIANGLES: + inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + break; + case KRMesh::model_format_t::KRENGINE_MODEL_FORMAT_INDEXED_STRIP: + case KRMesh::model_format_t::KRENGINE_MODEL_FORMAT_STRIP: + inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; + break; + } + inputAssembly.primitiveRestartEnable = VK_FALSE; VkViewport viewport{}; diff --git a/kraken/KRPipeline.h b/kraken/KRPipeline.h index cc801b8..7328c58 100644 --- a/kraken/KRPipeline.h +++ b/kraken/KRPipeline.h @@ -39,13 +39,15 @@ #include "KRCamera.h" #include "KRNode.h" #include "KRViewport.h" +#include "KRMesh.h" class KRShader; class KRSurface; + class KRPipeline : public KRContextObject { public: - KRPipeline(KRContext& context, KRSurface& surface, const char* szKey, const std::vector& shaders, uint32_t vertexAttributes); + KRPipeline(KRContext& context, KRSurface& surface, const char* szKey, const std::vector& shaders, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat); KRPipeline(KRContext &context, char *szKey, std::string options, std::string vertShaderSource, const std::string fragShaderSource); virtual ~KRPipeline(); const char *getKey() const; diff --git a/kraken/KRPipelineManager.cpp b/kraken/KRPipelineManager.cpp index 28b1b95..ab378bf 100644 --- a/kraken/KRPipelineManager.cpp +++ b/kraken/KRPipelineManager.cpp @@ -62,7 +62,7 @@ KRPipelineManager::~KRPipelineManager() { #endif // ANDROID } -KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, const std::string& shader_name, uint32_t vertexAttributes) +KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, const std::string& shader_name, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat) { std::pair > key; key.first = shader_name; @@ -71,6 +71,7 @@ KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, const std::string key.second.push_back(surface.m_swapChainExtent.width); key.second.push_back(surface.m_swapChainExtent.height); key.second.push_back(vertexAttributes); + key.second.push_back(modelFormat); PipelineMap::iterator itr = m_pipelines.find(key); if (itr != m_pipelines.end()) { return itr->second; @@ -79,7 +80,7 @@ KRPipeline* KRPipelineManager::getPipeline(KRSurface& surface, const std::string std::vector shaders; shaders.push_back(m_pContext->getShaderManager()->get(shader_name + ".vert", "spv")); shaders.push_back(m_pContext->getShaderManager()->get(shader_name + ".frag", "spv")); - KRPipeline* pipeline = new KRPipeline(*m_pContext, surface, shader_name.c_str(), shaders, vertexAttributes); + KRPipeline* pipeline = new KRPipeline(*m_pContext, surface, shader_name.c_str(), shaders, vertexAttributes, modelFormat); m_pipelines[key] = pipeline; diff --git a/kraken/KRPipelineManager.h b/kraken/KRPipelineManager.h index dee8b9b..4ad692b 100644 --- a/kraken/KRPipelineManager.h +++ b/kraken/KRPipelineManager.h @@ -36,6 +36,7 @@ #include "KRDataBlock.h" #include "KRNode.h" #include "KRSurface.h" +#include "KRMesh.h" using std::map; using std::vector; @@ -54,7 +55,7 @@ public: virtual ~KRPipelineManager(); KRPipeline* get(const char* szKey); - KRPipeline *getPipeline(KRSurface& surface, const std::string& shader_name, uint32_t vertexAttributes); + KRPipeline *getPipeline(KRSurface& surface, const std::string& shader_name, uint32_t vertexAttributes, KRMesh::model_format_t modelFormat); KRPipeline *getPipeline(const std::string &pipeline_name, KRCamera *pCamera, const std::vector &point_lights, const std::vector &directional_lights, const std::vector&spot_lights, int bone_count, bool bDiffuseMap, bool bNormalMap, bool bSpecMap, bool bReflectionMap, bool bReflectionCubeMap, bool bLightMap, bool bDiffuseMapScale,bool bSpecMapScale, bool bNormalMapScale, bool bReflectionMapScale, bool bDiffuseMapOffset, bool bSpecMapOffset, bool bNormalMapOffset, bool bReflectionMapOffset, bool bAlphaTest, bool bAlphaBlend, KRNode::RenderPass renderPass, bool bRimColor = false); bool selectPipeline(KRCamera &camera, KRPipeline *pPipeline, const KRViewport &viewport, const Matrix4 &matModel, const std::vector &point_lights, const std::vector &directional_lights, const std::vector&spot_lights, int bone_count, const KRNode::RenderPass &renderPass, const Vector3 &rim_color, float rim_power, const Vector4 &fade_color); diff --git a/kraken/KRPresentationThread.cpp b/kraken/KRPresentationThread.cpp index 516303e..b353011 100644 --- a/kraken/KRPresentationThread.cpp +++ b/kraken/KRPresentationThread.cpp @@ -167,10 +167,10 @@ void KRPresentationThread::renderFrame() bool haveMesh = testVertices.isVBOReady(); if (haveMesh) { - KRPipeline* testPipeline = m_pContext->getPipelineManager()->getPipeline(surface, "vulkan_test", testVertices.getVertexAttributes()); + KRPipeline* testPipeline = m_pContext->getPipelineManager()->getPipeline(surface, "vulkan_test", testVertices.getVertexAttributes(), KRMesh::model_format_t::KRENGINE_MODEL_FORMAT_STRIP); testPipeline->bind(commandBuffer); testVertices.bind(commandBuffer); - vkCmdDraw(commandBuffer, 3, 1, 0, 0); + vkCmdDraw(commandBuffer, 4, 1, 0, 0); } vkCmdEndRenderPass(commandBuffer); diff --git a/standard_assets/shaders/vulkan_test.vert b/standard_assets/shaders/vulkan_test.vert index b01c423..8e25724 100644 --- a/standard_assets/shaders/vulkan_test.vert +++ b/standard_assets/shaders/vulkan_test.vert @@ -8,16 +8,11 @@ layout(location = 0) in vec3 vertex_position; layout(location = 1) in vec3 vertex_uv; layout(constant_id = 0) const int QUALITY_LEVEL = 64; // Specialization constant test -vec2 positions[3] = vec2[]( - vec2(0.0, -0.5), - vec2(0.5, 0.5), - vec2(-0.5, 0.5) -); - -vec3 colors[3] = vec3[]( +vec3 colors[4] = vec3[]( vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), - vec3(0.0, 0.0, 1.0) + vec3(0.0, 0.0, 1.0), + vec3(1.0, 0.0, 1.0) ); void main() {