From 93d665b356df00c958bbb90bfc0406e6b33f3693 Mon Sep 17 00:00:00 2001 From: kearwood Date: Wed, 6 Jul 2022 23:04:43 -0700 Subject: [PATCH] KRMesh::renderSubmesh is now private. Added KRMesh::renderNoMaterials. Replaced renderSubmesh loops outside KRMesh with calls to KRMesh::renderNoMaterials. Added KRMesh::getVertexAttributes. Call sites are now using KRMesh::getVertexAttributes and KRMesh::getModelFormat to configure the pipeline. --- kraken/KRLight.cpp | 81 ++++++++++++++++++++++------------------- kraken/KRMesh.h | 5 ++- kraken/KRReverbZone.cpp | 37 +++++++++---------- 3 files changed, 65 insertions(+), 58 deletions(-) diff --git a/kraken/KRLight.cpp b/kraken/KRLight.cpp index 289089d..9901ed6 100755 --- a/kraken/KRLight.cpp +++ b/kraken/KRLight.cpp @@ -281,7 +281,6 @@ void KRLight::render(RenderInfo& ri) { , "Light Particles" #endif ); - GLDEBUG(glDrawArrays(GL_TRIANGLES, 0, particle_count*3)); vkCmdDraw(ri.commandBuffer, particle_count * 3, 1, 0, 0); } } @@ -316,6 +315,8 @@ void KRLight::render(RenderInfo& ri) { info.renderPass = KRNode::RENDER_PASS_ADDITIVE_PARTICLES; info.rasterMode = PipelineInfo::RasterMode::kAdditive; info.cullMode = PipelineInfo::CullMode::kCullNone; + info.vertexAttributes = (1 << KRMesh::KRENGINE_ATTRIB_VERTEX); + info.modelFormat = KRMesh::model_format_t::KRENGINE_MODEL_FORMAT_TRIANGLES; KRPipeline *pFogShader = m_pContext->getPipelineManager()->getPipeline(*ri.surface, info); @@ -336,55 +337,53 @@ void KRLight::render(RenderInfo& ri) { , "Participating Media" #endif ); - GLDEBUG(glDrawArrays(GL_TRIANGLES, 0, slice_count*6)); + vkCmdDraw(ri.commandBuffer, slice_count * 6, 1, 0, 0); } if(ri.renderPass == KRNode::RENDER_PASS_PARTICLE_OCCLUSION) { if(m_flareTexture.size() && m_flareSize > 0.0f) { + std::vector sphereModels = getContext().getMeshManager()->getModel("__sphere"); + if (sphereModels.size()) { - - Matrix4 occlusion_test_sphere_matrix = Matrix4(); - occlusion_test_sphere_matrix.scale(m_localScale * m_flareOcclusionSize); - occlusion_test_sphere_matrix.translate(m_localTranslation); - if(m_parentNode) { - occlusion_test_sphere_matrix *= m_parentNode->getModelMatrix(); - } + Matrix4 occlusion_test_sphere_matrix = Matrix4(); + occlusion_test_sphere_matrix.scale(m_localScale * m_flareOcclusionSize); + occlusion_test_sphere_matrix.translate(m_localTranslation); + if(m_parentNode) { + occlusion_test_sphere_matrix *= m_parentNode->getModelMatrix(); + } - PipelineInfo info{}; - std::string shader_name("occlusion_test"); - info.shader_name = &shader_name; - info.pCamera = ri.camera; - info.point_lights = &ri.point_lights; - info.directional_lights = &ri.directional_lights; - info.spot_lights = &ri.spot_lights; - info.renderPass = ri.renderPass; - info.rasterMode = PipelineInfo::RasterMode::kAdditive; - info.cullMode = PipelineInfo::CullMode::kCullNone; - // TODO: set info.vertexAttributes and info.modelFormat + PipelineInfo info{}; + std::string shader_name("occlusion_test"); + info.shader_name = &shader_name; + info.pCamera = ri.camera; + info.point_lights = &ri.point_lights; + info.directional_lights = &ri.directional_lights; + info.spot_lights = &ri.spot_lights; + info.renderPass = ri.renderPass; + info.rasterMode = PipelineInfo::RasterMode::kAdditive; + info.cullMode = PipelineInfo::CullMode::kCullNone; + info.modelFormat = sphereModels[0]->getModelFormat(); + info.vertexAttributes = sphereModels[0]->getVertexAttributes(); - KRPipeline* pPipeline = getContext().getPipelineManager()->getPipeline(*ri.surface, info); - pPipeline->bind(*info.pCamera, ri.viewport, occlusion_test_sphere_matrix, info.point_lights, info.directional_lights, info.spot_lights, info.renderPass, Vector3::Zero(), 0.0f, Vector4::Zero()); + KRPipeline* pPipeline = getContext().getPipelineManager()->getPipeline(*ri.surface, info); + pPipeline->bind(*info.pCamera, ri.viewport, occlusion_test_sphere_matrix, info.point_lights, info.directional_lights, info.spot_lights, info.renderPass, Vector3::Zero(), 0.0f, Vector4::Zero()); - GLDEBUG(glGenQueriesEXT(1, &m_occlusionQuery)); -#if TARGET_OS_IPHONE || defined(ANDROID) - GLDEBUG(glBeginQueryEXT(GL_ANY_SAMPLES_PASSED_EXT, m_occlusionQuery)); -#else - GLDEBUG(glBeginQuery(GL_SAMPLES_PASSED, m_occlusionQuery)); -#endif + GLDEBUG(glGenQueriesEXT(1, &m_occlusionQuery)); + #if TARGET_OS_IPHONE || defined(ANDROID) + GLDEBUG(glBeginQueryEXT(GL_ANY_SAMPLES_PASSED_EXT, m_occlusionQuery)); + #else + GLDEBUG(glBeginQuery(GL_SAMPLES_PASSED, m_occlusionQuery)); + #endif - std::vector sphereModels = getContext().getMeshManager()->getModel("__sphere"); - if(sphereModels.size()) { - for(int i=0; i < sphereModels[0]->getSubmeshCount(); i++) { - sphereModels[0]->renderSubmesh(ri.commandBuffer, i, ri.renderPass, getName(), "occlusion_test", 1.0f); - } - } + sphereModels[0]->renderNoMaterials(ri.commandBuffer, ri.renderPass, getName(), "occlusion_test", 1.0f); #if TARGET_OS_IPHONE || defined(ANDROID) - GLDEBUG(glEndQueryEXT(GL_ANY_SAMPLES_PASSED_EXT)); + GLDEBUG(glEndQueryEXT(GL_ANY_SAMPLES_PASSED_EXT)); #else - GLDEBUG(glEndQuery(GL_SAMPLES_PASSED)); + GLDEBUG(glEndQuery(GL_SAMPLES_PASSED)); #endif + } } } @@ -404,6 +403,8 @@ void KRLight::render(RenderInfo& ri) { } if(m_pFlareTexture) { + KRMeshManager::KRVBOData& vertices = getContext().getMeshManager()->KRENGINE_VBO_DATA_2D_SQUARE_VERTICES; + // Render light flare on transparency pass PipelineInfo info{}; std::string shader_name("flare"); @@ -415,6 +416,10 @@ void KRLight::render(RenderInfo& ri) { info.renderPass = ri.renderPass; info.rasterMode = PipelineInfo::RasterMode::kAdditiveNoTest; info.cullMode = PipelineInfo::CullMode::kCullNone; + info.vertexAttributes = vertices.getVertexAttributes(); + info.modelFormat = KRMesh::model_format_t::KRENGINE_MODEL_FORMAT_STRIP; + + KRPipeline *pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); pShader->bind(*ri.camera, ri.viewport, getModelMatrix(), &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass, Vector3::Zero(), 0.0f, Vector4::Zero()); @@ -422,8 +427,8 @@ void KRLight::render(RenderInfo& ri) { pShader->setUniform(KRPipeline::KRENGINE_UNIFORM_MATERIAL_ALPHA, 1.0f); pShader->setUniform(KRPipeline::KRENGINE_UNIFORM_FLARE_SIZE, m_flareSize); m_pContext->getTextureManager()->selectTexture(0, m_pFlareTexture, 0.0f, KRTexture::TEXTURE_USAGE_LIGHT_FLARE); - m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &getContext().getMeshManager()->KRENGINE_VBO_DATA_2D_SQUARE_VERTICES, 1.0f); - GLDEBUG(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)); + m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &vertices, 1.0f); + vkCmdDraw(ri.commandBuffer, 4, 1, 0, 0); } } } diff --git a/kraken/KRMesh.h b/kraken/KRMesh.h index a770453..3244172 100755 --- a/kraken/KRMesh.h +++ b/kraken/KRMesh.h @@ -127,7 +127,7 @@ public: void optimize(); void optimizeIndexes(); - void renderSubmesh(VkCommandBuffer& commandBuffer, int iSubmesh, KRNode::RenderPass renderPass, const std::string &object_name, const std::string &material_name, float lodCoverage); + void renderNoMaterials(VkCommandBuffer& commandBuffer, KRNode::RenderPass renderPass, const std::string& object_name, const std::string& material_name, float lodCoverage); float getMaxDimension(); @@ -184,6 +184,8 @@ public: int getSubmeshCount() const; int getVertexCount(int submesh) const; + __uint32_t getVertexAttributes() const; + int getTriangleVertexIndex(int submesh, int index) const; Vector3 getVertexPosition(int index) const; Vector3 getVertexNormal(int index) const; @@ -228,6 +230,7 @@ private: void getSubmeshes(); void getMaterials(); + void renderSubmesh(VkCommandBuffer& commandBuffer, int iSubmesh, KRNode::RenderPass renderPass, const std::string& object_name, const std::string& material_name, float lodCoverage); static bool rayCast(const Vector3 &start, const Vector3 &dir, const Triangle3 &tri, const Vector3 &tri_n0, const Vector3 &tri_n1, const Vector3 &tri_n2, HitInfo &hitinfo); static bool sphereCast(const Matrix4 &model_to_world, const Vector3 &v0, const Vector3 &v1, float radius, const Triangle3 &tri, HitInfo &hitinfo); diff --git a/kraken/KRReverbZone.cpp b/kraken/KRReverbZone.cpp index c04eb07..09adbb0 100755 --- a/kraken/KRReverbZone.cpp +++ b/kraken/KRReverbZone.cpp @@ -126,28 +126,27 @@ void KRReverbZone::render(RenderInfo& ri) bool bVisualize = ri.camera->settings.debug_display == KRRenderSettings::KRENGINE_DEBUG_DISPLAY_SIREN_REVERB_ZONES; if(ri.renderPass == KRNode::RENDER_PASS_FORWARD_TRANSPARENT && bVisualize) { - Matrix4 sphereModelMatrix = getModelMatrix(); - PipelineInfo info{}; - std::string shader_name("visualize_overlay"); - info.shader_name = &shader_name; - info.pCamera = ri.camera; - info.point_lights = &ri.point_lights; - info.directional_lights = &ri.directional_lights; - info.spot_lights = &ri.spot_lights; - info.renderPass = ri.renderPass; - info.rasterMode = PipelineInfo::RasterMode::kAlphaBlend; + std::vector sphereModels = getContext().getMeshManager()->getModel("__sphere"); + if (sphereModels.size()) { + Matrix4 sphereModelMatrix = getModelMatrix(); + PipelineInfo info{}; + std::string shader_name("visualize_overlay"); + info.shader_name = &shader_name; + info.pCamera = ri.camera; + info.point_lights = &ri.point_lights; + info.directional_lights = &ri.directional_lights; + info.spot_lights = &ri.spot_lights; + info.renderPass = ri.renderPass; + info.rasterMode = PipelineInfo::RasterMode::kAlphaBlend; + info.modelFormat = sphereModels[0]->getModelFormat(); + info.vertexAttributes = sphereModels[0]->getVertexAttributes(); - KRPipeline *pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); - + KRPipeline *pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); - pShader->bind(*ri.camera, ri.viewport, sphereModelMatrix, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass, Vector3::Zero(), 0.0f, Vector4::Zero()); + pShader->bind(*ri.camera, ri.viewport, sphereModelMatrix, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass, Vector3::Zero(), 0.0f, Vector4::Zero()); - std::vector sphereModels = getContext().getMeshManager()->getModel("__sphere"); - if(sphereModels.size()) { - for(int i=0; i < sphereModels[0]->getSubmeshCount(); i++) { - sphereModels[0]->renderSubmesh(ri.commandBuffer, i, ri.renderPass, getName(), "visualize_overlay", 1.0f); - } - } + sphereModels[0]->renderNoMaterials(ri.commandBuffer, ri.renderPass, getName(), "visualize_overlay", 1.0f); + } // sphereModels.size() } }