From d7672d31f4371904a1fab455c8ce909f6c56ed70 Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Sat, 21 Sep 2024 16:46:04 -0700 Subject: [PATCH] Refactoring to reduce KRScene::Render parameter count, passing by RenderInfo structure instead. --- kraken/nodes/KRAmbientZone.cpp | 2 +- kraken/nodes/KRAudioSource.cpp | 2 +- kraken/nodes/KRBone.cpp | 2 +- kraken/nodes/KRCamera.cpp | 72 ++++++++++++++-------- kraken/nodes/KRCollider.cpp | 2 +- kraken/nodes/KRDirectionalLight.cpp | 4 +- kraken/nodes/KRLight.cpp | 21 ++++--- kraken/nodes/KRModel.cpp | 6 +- kraken/nodes/KRNode.h | 4 +- kraken/nodes/KRParticleSystemNewtonian.cpp | 4 +- kraken/nodes/KRPointLight.cpp | 6 +- kraken/nodes/KRReverbZone.cpp | 2 +- kraken/nodes/KRSprite.cpp | 2 +- kraken/resources/material/KRMaterial.cpp | 2 +- kraken/resources/scene/KRScene.cpp | 31 +++++----- kraken/resources/scene/KRScene.h | 4 +- 16 files changed, 95 insertions(+), 71 deletions(-) diff --git a/kraken/nodes/KRAmbientZone.cpp b/kraken/nodes/KRAmbientZone.cpp index 31a8c46..c65efc9 100755 --- a/kraken/nodes/KRAmbientZone.cpp +++ b/kraken/nodes/KRAmbientZone.cpp @@ -148,7 +148,7 @@ void KRAmbientZone::render(RenderInfo& ri) info.vertexAttributes = sphereModel->getVertexAttributes(); KRPipeline* pPipeline = getContext().getPipelineManager()->getPipeline(*ri.surface, info); - pPipeline->bind(ri.commandBuffer, *ri.camera, ri.viewport, sphereModelMatrix, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); + pPipeline->bind(ri.commandBuffer, *ri.camera, *ri.viewport, sphereModelMatrix, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); sphereModel->renderNoMaterials(ri.commandBuffer, ri.renderPass, getName(), "visualize_overlay", 1.0f); } // sphereModel diff --git a/kraken/nodes/KRAudioSource.cpp b/kraken/nodes/KRAudioSource.cpp index eb9e1aa..69260ec 100755 --- a/kraken/nodes/KRAudioSource.cpp +++ b/kraken/nodes/KRAudioSource.cpp @@ -215,7 +215,7 @@ void KRAudioSource::render(RenderInfo& ri) info.vertexAttributes = sphereModel->getVertexAttributes(); KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); - pShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, sphereModelMatrix, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); + pShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, sphereModelMatrix, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); sphereModel->renderNoMaterials(ri.commandBuffer, ri.renderPass, getName(), "visualize_overlay", 1.0f); } // sphereModels.size() diff --git a/kraken/nodes/KRBone.cpp b/kraken/nodes/KRBone.cpp index b416d5f..15cadb5 100755 --- a/kraken/nodes/KRBone.cpp +++ b/kraken/nodes/KRBone.cpp @@ -99,7 +99,7 @@ void KRBone::render(RenderInfo& ri) info.vertexAttributes = sphereModel->getVertexAttributes(); KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); - pShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, sphereModelMatrix, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); + pShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, sphereModelMatrix, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); sphereModel->renderNoMaterials(ri.commandBuffer, ri.renderPass, getName(), "visualize_overlay", 1.0f); } // sphereModel diff --git a/kraken/nodes/KRCamera.cpp b/kraken/nodes/KRCamera.cpp index 8b37b54..2eae544 100755 --- a/kraken/nodes/KRCamera.cpp +++ b/kraken/nodes/KRCamera.cpp @@ -181,13 +181,19 @@ void KRCamera::renderFrame(VkCommandBuffer& commandBuffer, KRSurface& compositeS scene.updateOctree(m_viewport); // ----====---- Pre-stream resources ----====---- - scene.render(commandBuffer, compositeSurface, this, m_viewport.getVisibleBounds(), m_viewport, compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_PRESTREAM), true); + KRNode::RenderInfo ri(commandBuffer); + ri.camera = this; + ri.viewport = &m_viewport; + ri.renderPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_PRESTREAM); + ri.surface = &compositeSurface; + + scene.render(ri, true); // ----====---- Generate Shadowmaps for Lights ----====---- if (settings.m_cShadowBuffers > 0) { GL_PUSH_GROUP_MARKER("Generate Shadowmaps"); - - scene.render(commandBuffer, compositeSurface, this, m_viewport.getVisibleBounds(), m_viewport, compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_SHADOWMAP), false /*settings.bEnableDeferredLighting*/); + ri.renderPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_SHADOWMAP); + scene.render(ri, false /*settings.bEnableDeferredLighting*/); GL_POP_GROUP_MARKER; } @@ -198,14 +204,14 @@ void KRCamera::renderFrame(VkCommandBuffer& commandBuffer, KRSurface& compositeS GL_PUSH_GROUP_MARKER("Deferred Lighting - Pass 1 (Opaque)"); // Start render pass - KRRenderPass* deferredGBufferPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_DEFERRED_GBUFFER); - deferredGBufferPass->begin(commandBuffer, compositeSurface); + ri.renderPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_DEFERRED_GBUFFER); + ri.renderPass->begin(commandBuffer, compositeSurface); // Render the geometry - scene.render(commandBuffer, compositeSurface, this, m_viewport.getVisibleBounds(), m_viewport, deferredGBufferPass, false); + scene.render(ri, false); // End render pass - deferredGBufferPass->end(commandBuffer); + ri.renderPass->end(commandBuffer); GL_POP_GROUP_MARKER; @@ -226,7 +232,10 @@ void KRCamera::renderFrame(VkCommandBuffer& commandBuffer, KRSurface& compositeS m_pContext->getTextureManager()->selectTexture(0 /*GL_TEXTURE_2D*/, 7, compositeDepthTexture); // Render the geometry - scene.render(commandBuffer, compositeSurface, this, m_viewport.getVisibleBounds(), m_viewport, compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_DEFERRED_LIGHTS), false); + ri.renderPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_DEFERRED_LIGHTS); + ri.renderPass->begin(commandBuffer, compositeSurface); + scene.render(ri, false); + ri.renderPass->end(commandBuffer); GL_POP_GROUP_MARKER; @@ -235,18 +244,18 @@ void KRCamera::renderFrame(VkCommandBuffer& commandBuffer, KRSurface& compositeS GL_PUSH_GROUP_MARKER("Deferred Lighting - Pass 3 (Opaque)"); // Start render pass - KRRenderPass* deferredOpaquePass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_DEFERRED_OPAQUE); - deferredOpaquePass->begin(commandBuffer, compositeSurface); + ri.renderPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_DEFERRED_OPAQUE); + ri.renderPass->begin(commandBuffer, compositeSurface); // Set source to buffers from pass 2 m_pContext->getTextureManager()->selectTexture(0 /*GL_TEXTURE_2D*/, 6, lightAccumulationTexture); // Render the geometry // TODO: At this point, we only want to render octree nodes that produced fragments during the 1st pass into the GBuffer - scene.render(commandBuffer, compositeSurface, this, m_viewport.getVisibleBounds(), m_viewport, deferredOpaquePass, false); + scene.render(ri, false); // End render pass - deferredOpaquePass->end(commandBuffer); + ri.renderPass->end(commandBuffer); GL_POP_GROUP_MARKER; } else { @@ -254,11 +263,11 @@ void KRCamera::renderFrame(VkCommandBuffer& commandBuffer, KRSurface& compositeS GL_PUSH_GROUP_MARKER("Forward Rendering - Opaque"); // Start render pass - KRRenderPass* forwardOpaquePass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_FORWARD_OPAQUE); - forwardOpaquePass->begin(commandBuffer, compositeSurface); + ri.renderPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_FORWARD_OPAQUE); + ri.renderPass->begin(commandBuffer, compositeSurface); // Render the geometry - scene.render(commandBuffer, compositeSurface, this, m_viewport.getVisibleBounds(), m_viewport, forwardOpaquePass, false); + scene.render(ri, false); GL_POP_GROUP_MARKER; @@ -270,19 +279,19 @@ void KRCamera::renderFrame(VkCommandBuffer& commandBuffer, KRSurface& compositeS std::string shader_name("vulkan_test"); info.shader_name = &shader_name; info.pCamera = this; - info.renderPass = forwardOpaquePass; + info.renderPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_FORWARD_OPAQUE); info.rasterMode = RasterMode::kOpaque; info.vertexAttributes = sphereMesh->getVertexAttributes(); info.modelFormat = sphereMesh->getModelFormat(); KRPipeline* testPipeline = m_pContext->getPipelineManager()->getPipeline(compositeSurface, info); - testPipeline->bind(commandBuffer, *this, m_viewport, Matrix4(), nullptr, nullptr, nullptr, compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_FORWARD_OPAQUE)); + testPipeline->bind(commandBuffer, *this, m_viewport, Matrix4(), nullptr, nullptr, nullptr, info.renderPass); sphereMesh->renderNoMaterials(commandBuffer, info.renderPass, "Vulkan Test", "vulkan_test", 1.0); } // ---------- End: Vulkan Debug Code ---------- - forwardOpaquePass->end(commandBuffer); + ri.renderPass->end(commandBuffer); } // ----====---- Sky Box ----====---- @@ -321,8 +330,10 @@ void KRCamera::renderFrame(VkCommandBuffer& commandBuffer, KRSurface& compositeS GL_PUSH_GROUP_MARKER("Forward Rendering - Transparent"); // Render all transparent geometry - scene.render(commandBuffer, compositeSurface, this, m_viewport.getVisibleBounds(), m_viewport, compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT), false); - + ri.renderPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT); + ri.renderPass->begin(commandBuffer, compositeSurface); + scene.render(ri, false); + ri.renderPass->end(commandBuffer); GL_POP_GROUP_MARKER; // ----====---- Particle Occlusion Tests ----====---- @@ -330,7 +341,12 @@ void KRCamera::renderFrame(VkCommandBuffer& commandBuffer, KRSurface& compositeS GL_PUSH_GROUP_MARKER("Particle Occlusion Tests"); // ----====---- Perform Occlusion Tests ----====---- - scene.render(commandBuffer, compositeSurface, this, m_viewport.getVisibleBounds(), m_viewport, compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_PARTICLE_OCCLUSION), false); + ri.renderPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_PARTICLE_OCCLUSION); + if (ri.renderPass) { + ri.renderPass->begin(commandBuffer, compositeSurface); + scene.render(ri, false); + ri.renderPass->end(commandBuffer); + } GL_POP_GROUP_MARKER; @@ -339,7 +355,12 @@ void KRCamera::renderFrame(VkCommandBuffer& commandBuffer, KRSurface& compositeS GL_PUSH_GROUP_MARKER("Additive Particles"); // Render all flares - scene.render(commandBuffer, compositeSurface, this, m_viewport.getVisibleBounds(), m_viewport, compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_ADDITIVE_PARTICLES), false); + ri.renderPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_ADDITIVE_PARTICLES); + if (ri.renderPass) { + ri.renderPass->begin(commandBuffer, compositeSurface); + scene.render(ri, false); + ri.renderPass->end(commandBuffer); + } GL_POP_GROUP_MARKER; @@ -369,8 +390,11 @@ void KRCamera::renderFrame(VkCommandBuffer& commandBuffer, KRSurface& compositeS GLDEBUG(glDepthRangef(0.0, 1.0)); } - scene.render(commandBuffer, compositeSurface, this, m_viewport.getVisibleBounds(), volumetricLightingViewport, compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_VOLUMETRIC_EFFECTS_ADDITIVE), false); - + ri.renderPass = compositeSurface.getRenderPass(RenderPassType::RENDER_PASS_VOLUMETRIC_EFFECTS_ADDITIVE); + ri.viewport = &volumetricLightingViewport; + ri.renderPass->begin(commandBuffer, compositeSurface); + scene.render(ri, false); + ri.renderPass->end(commandBuffer); GL_POP_GROUP_MARKER; } diff --git a/kraken/nodes/KRCollider.cpp b/kraken/nodes/KRCollider.cpp index 378f0cd..b81c74d 100755 --- a/kraken/nodes/KRCollider.cpp +++ b/kraken/nodes/KRCollider.cpp @@ -238,7 +238,7 @@ void KRCollider::render(RenderInfo& ri) KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); - pShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, getModelMatrix(), &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); + pShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, getModelMatrix(), &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); m_model->renderNoMaterials(ri.commandBuffer, ri.renderPass, getName(), "visualize_overlay", 1.0f); diff --git a/kraken/nodes/KRDirectionalLight.cpp b/kraken/nodes/KRDirectionalLight.cpp index 7ac44d4..8151a51 100755 --- a/kraken/nodes/KRDirectionalLight.cpp +++ b/kraken/nodes/KRDirectionalLight.cpp @@ -138,7 +138,7 @@ void KRDirectionalLight::render(RenderInfo& ri) std::vector this_light; this_light.push_back(this); - Matrix4 matModelViewInverseTranspose = ri.viewport.getViewMatrix() * getModelMatrix(); + Matrix4 matModelViewInverseTranspose = ri.viewport->getViewMatrix() * getModelMatrix(); matModelViewInverseTranspose.transpose(); matModelViewInverseTranspose.invert(); @@ -163,7 +163,7 @@ void KRDirectionalLight::render(RenderInfo& ri) pShader->setPushConstant(KRPipeline::PushConstant::light_direction_view_space, light_direction_view_space); pShader->setPushConstant(KRPipeline::PushConstant::light_color, m_color); pShader->setPushConstant(KRPipeline::PushConstant::light_intensity, m_intensity * 0.01f); - pShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, getModelMatrix(), nullptr, &this_light, nullptr, ri.renderPass); + pShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, getModelMatrix(), nullptr, &this_light, nullptr, ri.renderPass); // Render a full screen quad m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &vertices, 1.0f); diff --git a/kraken/nodes/KRLight.cpp b/kraken/nodes/KRLight.cpp index 2d4ef2c..2d564e9 100755 --- a/kraken/nodes/KRLight.cpp +++ b/kraken/nodes/KRLight.cpp @@ -233,7 +233,7 @@ void KRLight::render(RenderInfo& ri) KRNode::render(ri); if (ri.renderPass->getType() == RenderPassType::RENDER_PASS_SHADOWMAP && (ri.camera->settings.volumetric_environment_enable || ri.camera->settings.dust_particle_enable || (ri.camera->settings.m_cShadowBuffers > 0 && m_casts_shadow))) { - allocateShadowBuffers(configureShadowBufferViewports(ri.viewport)); + allocateShadowBuffers(configureShadowBufferViewports(*ri.viewport)); renderShadowBuffers(ri); } @@ -241,7 +241,7 @@ void KRLight::render(RenderInfo& ri) // Render brownian particles for dust floating in air if (m_cShadowBuffers >= 1 && shadowValid[0] && m_dust_particle_density > 0.0f && m_dust_particle_size > 0.0f && m_dust_particle_intensity > 0.0f) { - if (ri.viewport.visible(getBounds()) || true) { // FINDME, HACK need to remove "|| true"? + if (ri.viewport->visible(getBounds()) || true) { // FINDME, HACK need to remove "|| true"? float particle_range = 600.0f; @@ -252,7 +252,7 @@ void KRLight::render(RenderInfo& ri) Matrix4 particleModelMatrix; particleModelMatrix.scale(particle_range); // Scale the box symetrically to ensure that we don't have an uneven distribution of particles for different angles of the view frustrum - particleModelMatrix.translate(ri.viewport.getCameraPosition()); + particleModelMatrix.translate(ri.viewport->getCameraPosition()); std::vector this_directional_light; std::vector this_spot_light; @@ -287,7 +287,7 @@ void KRLight::render(RenderInfo& ri) pParticleShader->setPushConstant(KRPipeline::PushConstant::light_color, m_color * ri.camera->settings.dust_particle_intensity * m_dust_particle_intensity * m_intensity); pParticleShader->setPushConstant(KRPipeline::PushConstant::particle_origin, Matrix4::DotWDiv(Matrix4::Invert(particleModelMatrix), Vector3::Zero())); pParticleShader->setPushConstant(KRPipeline::PushConstant::flare_size, m_dust_particle_size); - pParticleShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, particleModelMatrix, &this_point_light, &this_directional_light, &this_spot_light, ri.renderPass); + pParticleShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, particleModelMatrix, &this_point_light, &this_directional_light, &this_spot_light, ri.renderPass); m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &m_pContext->getMeshManager()->KRENGINE_VBO_DATA_RANDOM_PARTICLES, 1.0f); @@ -338,7 +338,7 @@ void KRLight::render(RenderInfo& ri) pFogShader->setPushConstant(KRPipeline::PushConstant::slice_depth_scale, Vector2::Create(slice_near, slice_spacing)); pFogShader->setPushConstant(KRPipeline::PushConstant::light_color, (m_color * ri.camera->settings.volumetric_environment_intensity * m_intensity * -slice_spacing / 1000.0f)); - pFogShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, Matrix4(), &this_point_light, &this_directional_light, &this_spot_light, ri.renderPass); + pFogShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, Matrix4(), &this_point_light, &this_directional_light, &this_spot_light, ri.renderPass); m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &m_pContext->getMeshManager()->KRENGINE_VBO_DATA_VOLUMETRIC_LIGHTING, 1.0f); vkCmdDraw(ri.commandBuffer, slice_count * 6, 1, 0, 0); @@ -371,7 +371,7 @@ void KRLight::render(RenderInfo& ri) info.vertexAttributes = sphereModel->getVertexAttributes(); KRPipeline* pPipeline = getContext().getPipelineManager()->getPipeline(*ri.surface, info); - pPipeline->bind(ri.commandBuffer, *info.pCamera, ri.viewport, occlusion_test_sphere_matrix, info.point_lights, info.directional_lights, info.spot_lights, info.renderPass); + pPipeline->bind(ri.commandBuffer, *info.pCamera, *ri.viewport, occlusion_test_sphere_matrix, info.point_lights, info.directional_lights, info.spot_lights, info.renderPass); GLDEBUG(glGenQueriesEXT(1, &m_occlusionQuery)); #if TARGET_OS_IPHONE || defined(ANDROID) @@ -429,7 +429,7 @@ void KRLight::render(RenderInfo& ri) pShader->setPushConstant(KRPipeline::PushConstant::material_alpha, 1.0f); pShader->setPushConstant(KRPipeline::PushConstant::flare_size, m_flareSize); pShader->setImageBinding("diffuseTexture", m_pFlareTexture, getContext().getSamplerManager()->DEFAULT_CLAMPED_SAMPLER); - pShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, getModelMatrix(), &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); + pShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, getModelMatrix(), &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &vertices, 1.0f); vkCmdDraw(ri.commandBuffer, 4, 1, 0, 0); @@ -510,6 +510,7 @@ int KRLight::configureShadowBufferViewports(const KRViewport& viewport) void KRLight::renderShadowBuffers(RenderInfo& ri) { + KRViewport* prevViewport = ri.viewport; for (int iShadow = 0; iShadow < m_cShadowBuffers; iShadow++) { if (!shadowValid[iShadow]) { shadowValid[iShadow] = true; @@ -540,11 +541,13 @@ void KRLight::renderShadowBuffers(RenderInfo& ri) info.cullMode = CullMode::kCullNone; // Disabling culling, which eliminates some self-cast shadow artifacts KRPipeline* shadowShader = m_pContext->getPipelineManager()->getPipeline(*ri.surface, info); - shadowShader->bind(ri.commandBuffer, *ri.camera, m_shadowViewports[iShadow], Matrix4(), nullptr, nullptr, nullptr, ri.renderPass); + ri.viewport = &m_shadowViewports[iShadow]; + shadowShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, Matrix4(), nullptr, nullptr, nullptr, ri.renderPass); - getScene().render(ri.commandBuffer, *ri.surface, ri.camera, m_shadowViewports[iShadow].getVisibleBounds(), m_shadowViewports[iShadow], ri.renderPass, true); + getScene().render(ri, true); } } + ri.viewport = prevViewport; } diff --git a/kraken/nodes/KRModel.cpp b/kraken/nodes/KRModel.cpp index fcfc5f6..3d65c5a 100755 --- a/kraken/nodes/KRModel.cpp +++ b/kraken/nodes/KRModel.cpp @@ -199,7 +199,7 @@ void KRModel::render(KRNode::RenderInfo& ri) { if (m_lod_visible >= LOD_VISIBILITY_PRESTREAM && ri.renderPass->getType() == RenderPassType::RENDER_PASS_PRESTREAM) { - preStream(ri.viewport); + preStream(*ri.viewport); } if (m_lod_visible <= LOD_VISIBILITY_PRESTREAM) return; @@ -226,7 +226,7 @@ void KRModel::render(KRNode::RenderInfo& ri) } */ - float lod_coverage = ri.viewport.coverage(getBounds()); // This also checks the view frustrum culling + float lod_coverage = ri.viewport->coverage(getBounds()); // This also checks the view frustrum culling if (lod_coverage > m_min_lod_coverage) { @@ -256,7 +256,7 @@ void KRModel::render(KRNode::RenderInfo& ri) Matrix4 matModel = getModelMatrix(); if (m_faces_camera) { Vector3 model_center = Matrix4::Dot(matModel, Vector3::Zero()); - Vector3 camera_pos = ri.viewport.getCameraPosition(); + Vector3 camera_pos = ri.viewport->getCameraPosition(); matModel = Quaternion::Create(Vector3::Forward(), Vector3::Normalize(camera_pos - model_center)).rotationMatrix() * matModel; } diff --git a/kraken/nodes/KRNode.h b/kraken/nodes/KRNode.h index 6175f32..fa84a10 100755 --- a/kraken/nodes/KRNode.h +++ b/kraken/nodes/KRNode.h @@ -90,8 +90,8 @@ public: std::vector point_lights; std::vector directional_lights; std::vector spot_lights; - KRViewport viewport; - const KRRenderPass* renderPass; + KRViewport* viewport; + KRRenderPass* renderPass; }; static void InitNodeInfo(KrNodeInfo* nodeInfo); diff --git a/kraken/nodes/KRParticleSystemNewtonian.cpp b/kraken/nodes/KRParticleSystemNewtonian.cpp index d0947b0..f126601 100755 --- a/kraken/nodes/KRParticleSystemNewtonian.cpp +++ b/kraken/nodes/KRParticleSystemNewtonian.cpp @@ -88,7 +88,7 @@ void KRParticleSystemNewtonian::render(RenderInfo& ri) KRNode::render(ri); if (ri.renderPass->getType() == RenderPassType::RENDER_PASS_ADDITIVE_PARTICLES) { - if (ri.viewport.visible(getBounds())) { + if (ri.viewport->visible(getBounds())) { int particle_count = 10000; PipelineInfo info{}; @@ -106,7 +106,7 @@ void KRParticleSystemNewtonian::render(RenderInfo& ri) KRPipeline* pParticleShader = m_pContext->getPipelineManager()->getPipeline(*ri.surface, info); pParticleShader->setPushConstant(KRPipeline::PushConstant::flare_size, 1.0f); - pParticleShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, getModelMatrix(), &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); + pParticleShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, getModelMatrix(), &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &m_pContext->getMeshManager()->KRENGINE_VBO_DATA_RANDOM_PARTICLES, 1.0f); diff --git a/kraken/nodes/KRPointLight.cpp b/kraken/nodes/KRPointLight.cpp index 6ef7e5f..40da0d4 100755 --- a/kraken/nodes/KRPointLight.cpp +++ b/kraken/nodes/KRPointLight.cpp @@ -95,9 +95,9 @@ void KRPointLight::render(RenderInfo& ri) sphereModelMatrix.scale(influence_radius); sphereModelMatrix.translate(light_position.x, light_position.y, light_position.z); - if (ri.viewport.visible(getBounds())) { // Cull out any lights not within the view frustrum + if (ri.viewport->visible(getBounds())) { // Cull out any lights not within the view frustrum - Vector3 view_light_position = Matrix4::Dot(ri.viewport.getViewMatrix(), light_position); + Vector3 view_light_position = Matrix4::Dot(ri.viewport->getViewMatrix(), light_position); bool bInsideLight = view_light_position.sqrMagnitude() <= (influence_radius + ri.camera->settings.getPerspectiveNearZ()) * (influence_radius + ri.camera->settings.getPerspectiveNearZ()); @@ -121,7 +121,7 @@ void KRPointLight::render(RenderInfo& ri) pShader->setPushConstant(KRPipeline::PushConstant::light_decay_start, getDecayStart()); pShader->setPushConstant(KRPipeline::PushConstant::light_cutoff, KRLIGHT_MIN_INFLUENCE); pShader->setPushConstant(KRPipeline::PushConstant::light_position, light_position); - pShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, sphereModelMatrix, &this_light, nullptr, nullptr, ri.renderPass); + pShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, sphereModelMatrix, &this_light, nullptr, nullptr, ri.renderPass); if (bInsideLight) { // Render a full screen quad diff --git a/kraken/nodes/KRReverbZone.cpp b/kraken/nodes/KRReverbZone.cpp index e167ad1..f0476a6 100755 --- a/kraken/nodes/KRReverbZone.cpp +++ b/kraken/nodes/KRReverbZone.cpp @@ -145,7 +145,7 @@ void KRReverbZone::render(RenderInfo& ri) KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); - pShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, sphereModelMatrix, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); + pShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, sphereModelMatrix, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); sphereModel->renderNoMaterials(ri.commandBuffer, ri.renderPass, getName(), "visualize_overlay", 1.0f); } // sphereModel diff --git a/kraken/nodes/KRSprite.cpp b/kraken/nodes/KRSprite.cpp index 36a413c..4df60f0 100755 --- a/kraken/nodes/KRSprite.cpp +++ b/kraken/nodes/KRSprite.cpp @@ -167,7 +167,7 @@ void KRSprite::render(RenderInfo& ri) KRPipeline* pShader = getContext().getPipelineManager()->getPipeline(*ri.surface, info); pShader->setPushConstant(KRPipeline::PushConstant::material_alpha, m_spriteAlpha); pShader->setImageBinding("diffuseTexture", m_pSpriteTexture, m_pContext->getSamplerManager()->DEFAULT_CLAMPED_SAMPLER); - pShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, getModelMatrix(), &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); + pShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, getModelMatrix(), &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); m_pContext->getMeshManager()->bindVBO(ri.commandBuffer, &vertices, 1.0f); vkCmdDraw(ri.commandBuffer, 4, 1, 0, 0); diff --git a/kraken/resources/material/KRMaterial.cpp b/kraken/resources/material/KRMaterial.cpp index 1192544..34e1c06 100755 --- a/kraken/resources/material/KRMaterial.cpp +++ b/kraken/resources/material/KRMaterial.cpp @@ -467,7 +467,7 @@ void KRMaterial::bind(const KRNode::RenderInfo& ri, ModelFormat modelFormat, __u pShader->setImageBinding("reflectionTexture", m_pReflectionMap, getContext().getSamplerManager()->DEFAULT_CLAMPED_SAMPLER); } - pShader->bind(ri.commandBuffer, *ri.camera, ri.viewport, matModel, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); + pShader->bind(ri.commandBuffer, *ri.camera, *ri.viewport, matModel, &ri.point_lights, &ri.directional_lights, &ri.spot_lights, ri.renderPass); } const std::string& KRMaterial::getName() const diff --git a/kraken/resources/scene/KRScene.cpp b/kraken/resources/scene/KRScene.cpp index 1a814b4..71fca1c 100755 --- a/kraken/resources/scene/KRScene.cpp +++ b/kraken/resources/scene/KRScene.cpp @@ -103,8 +103,9 @@ std::set& KRScene::getLights() return m_lights; } -void KRScene::render(VkCommandBuffer& commandBuffer, KRSurface& surface, KRCamera* pCamera, unordered_map& visibleBounds, const KRViewport& viewport, const KRRenderPass* renderPass, bool new_frame) +void KRScene::render(KRNode::RenderInfo& ri, bool new_frame) { + unordered_map& visibleBounds = ri.viewport->getVisibleBounds(); if (new_frame) { // Expire cached occlusion test results. // Cached "failed" results are expired on the next frame (marked with .second of -1) @@ -124,11 +125,6 @@ void KRScene::render(VkCommandBuffer& commandBuffer, KRSurface& surface, KRCamer addDefaultLights(); } - KRNode::RenderInfo ri(commandBuffer); - ri.camera = pCamera; - ri.viewport = viewport; - ri.renderPass = renderPass; - std::vector point_lights; std::vectordirectional_lights; std::vectorspot_lights; @@ -171,10 +167,10 @@ void KRScene::render(VkCommandBuffer& commandBuffer, KRSurface& surface, KRCamer newRemainingOctrees.clear(); newRemainingOctreesTestResults.clear(); for (std::vector::iterator octree_itr = remainingOctrees.begin(); octree_itr != remainingOctrees.end(); octree_itr++) { - render(ri, *octree_itr, visibleBounds, newRemainingOctrees, newRemainingOctreesTestResults, remainingOctreesTestResultsOnly, false, false); + render(ri, *octree_itr, newRemainingOctrees, newRemainingOctreesTestResults, remainingOctreesTestResultsOnly, false, false); } for (std::vector::iterator octree_itr = remainingOctreesTestResults.begin(); octree_itr != remainingOctreesTestResults.end(); octree_itr++) { - render(ri, *octree_itr, visibleBounds, newRemainingOctrees, newRemainingOctreesTestResults, remainingOctreesTestResultsOnly, true, false); + render(ri, *octree_itr, newRemainingOctrees, newRemainingOctreesTestResults, remainingOctreesTestResultsOnly, true, false); } remainingOctrees = newRemainingOctrees; remainingOctreesTestResults = newRemainingOctreesTestResults; @@ -183,12 +179,13 @@ void KRScene::render(VkCommandBuffer& commandBuffer, KRSurface& surface, KRCamer newRemainingOctrees.clear(); newRemainingOctreesTestResults.clear(); for (std::vector::iterator octree_itr = remainingOctreesTestResultsOnly.begin(); octree_itr != remainingOctreesTestResultsOnly.end(); octree_itr++) { - render(ri, *octree_itr, visibleBounds, newRemainingOctrees, newRemainingOctreesTestResults, remainingOctreesTestResultsOnly, true, true); + render(ri, *octree_itr, newRemainingOctrees, newRemainingOctreesTestResults, remainingOctreesTestResultsOnly, true, true); } } -void KRScene::render(KRNode::RenderInfo& ri, KROctreeNode* pOctreeNode, unordered_map& visibleBounds, std::vector& remainingOctrees, std::vector& remainingOctreesTestResults, std::vector& remainingOctreesTestResultsOnly, bool bOcclusionResultsPass, bool bOcclusionTestResultsOnly) +void KRScene::render(KRNode::RenderInfo& ri, KROctreeNode* pOctreeNode, std::vector& remainingOctrees, std::vector& remainingOctreesTestResults, std::vector& remainingOctreesTestResultsOnly, bool bOcclusionResultsPass, bool bOcclusionTestResultsOnly) { + unordered_map& visibleBounds = ri.viewport->getVisibleBounds(); if (pOctreeNode) { AABB octreeBounds = pOctreeNode->getBounds(); @@ -220,10 +217,10 @@ void KRScene::render(KRNode::RenderInfo& ri, KROctreeNode* pOctreeNode, unordere bool in_viewport = false; if (ri.renderPass->getType() == RenderPassType::RENDER_PASS_PRESTREAM) { // When pre-streaming, objects are streamed in behind and in-front of the camera - AABB viewportExtents = AABB::Create(ri.viewport.getCameraPosition() - Vector3::Create(ri.camera->settings.getPerspectiveFarZ()), ri.viewport.getCameraPosition() + Vector3::Create(ri.camera->settings.getPerspectiveFarZ())); + AABB viewportExtents = AABB::Create(ri.viewport->getCameraPosition() - Vector3::Create(ri.camera->settings.getPerspectiveFarZ()), ri.viewport->getCameraPosition() + Vector3::Create(ri.camera->settings.getPerspectiveFarZ())); in_viewport = octreeBounds.intersects(viewportExtents); } else { - in_viewport = ri.viewport.visible(pOctreeNode->getBounds()); + in_viewport = ri.viewport->visible(pOctreeNode->getBounds()); } if (in_viewport) { @@ -239,7 +236,7 @@ void KRScene::render(KRNode::RenderInfo& ri, KROctreeNode* pOctreeNode, unordere if (!bVisible) { // Assume bounding boxes are visible without occlusion test queries if the camera is inside the box. // The near clipping plane of the camera is taken into consideration by expanding the match area - AABB cameraExtents = AABB::Create(ri.viewport.getCameraPosition() - Vector3::Create(ri.camera->settings.getPerspectiveNearZ()), ri.viewport.getCameraPosition() + Vector3::Create(ri.camera->settings.getPerspectiveNearZ())); + AABB cameraExtents = AABB::Create(ri.viewport->getCameraPosition() - Vector3::Create(ri.camera->settings.getPerspectiveNearZ()), ri.viewport->getCameraPosition() + Vector3::Create(ri.camera->settings.getPerspectiveNearZ())); bVisible = octreeBounds.intersects(cameraExtents); if (bVisible) { // Record the frame number in which the camera was within the bounds @@ -287,7 +284,7 @@ void KRScene::render(KRNode::RenderInfo& ri, KROctreeNode* pOctreeNode, unordere Matrix4 matModel = Matrix4(); matModel.scale(octreeBounds.size() * 0.5f); matModel.translate(octreeBounds.center()); - Matrix4 mvpmatrix = matModel * ri.viewport.getViewProjectionMatrix(); + Matrix4 mvpmatrix = matModel * ri.viewport->getViewProjectionMatrix(); KRMeshManager::KRVBOData& vertices = getContext().getMeshManager()->KRENGINE_VBO_DATA_3D_CUBE_VERTICES; @@ -306,7 +303,7 @@ void KRScene::render(KRNode::RenderInfo& ri, KROctreeNode* pOctreeNode, unordere info.modelFormat = ModelFormat::KRENGINE_MODEL_FORMAT_STRIP; KRPipeline* pPipeline = getContext().getPipelineManager()->getPipeline(*ri.surface, info); - pPipeline->bind(ri.commandBuffer, *info.pCamera, ri.viewport, matModel, info.point_lights, info.directional_lights, info.spot_lights, info.renderPass); + pPipeline->bind(ri.commandBuffer, *info.pCamera, *ri.viewport, matModel, info.point_lights, info.directional_lights, info.spot_lights, info.renderPass); vkCmdDraw(ri.commandBuffer, 14, 1, 0, 0); m_pContext->getMeshManager()->log_draw_call(ri.renderPass->getType(), "octree", "occlusion_test", 14); @@ -353,10 +350,10 @@ void KRScene::render(KRNode::RenderInfo& ri, KROctreeNode* pOctreeNode, unordere } // Render child octrees - const int* childOctreeOrder = ri.renderPass->getType() == RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT || ri.renderPass->getType() == RenderPassType::RENDER_PASS_ADDITIVE_PARTICLES || ri.renderPass->getType() == RenderPassType::RENDER_PASS_VOLUMETRIC_EFFECTS_ADDITIVE ? ri.viewport.getBackToFrontOrder() : ri.viewport.getFrontToBackOrder(); + const int* childOctreeOrder = ri.renderPass->getType() == RenderPassType::RENDER_PASS_FORWARD_TRANSPARENT || ri.renderPass->getType() == RenderPassType::RENDER_PASS_ADDITIVE_PARTICLES || ri.renderPass->getType() == RenderPassType::RENDER_PASS_VOLUMETRIC_EFFECTS_ADDITIVE ? ri.viewport->getBackToFrontOrder() : ri.viewport->getFrontToBackOrder(); for (int i = 0; i < 8; i++) { - render(ri, pOctreeNode->getChildren()[childOctreeOrder[i]], visibleBounds, remainingOctrees, remainingOctreesTestResults, remainingOctreesTestResultsOnly, false, false); + render(ri, pOctreeNode->getChildren()[childOctreeOrder[i]], remainingOctrees, remainingOctreesTestResults, remainingOctreesTestResultsOnly, false, false); } // Remove lights added at this octree level from the stack diff --git a/kraken/resources/scene/KRScene.h b/kraken/resources/scene/KRScene.h index b859afb..2804ace 100755 --- a/kraken/resources/scene/KRScene.h +++ b/kraken/resources/scene/KRScene.h @@ -71,8 +71,7 @@ public: bool sphereCast(const hydra::Vector3& v0, const hydra::Vector3& v1, float radius, hydra::HitInfo& hitinfo, unsigned int layer_mask); void renderFrame(VkCommandBuffer& commandBuffer, KRSurface& surface, float deltaTime); - void render(VkCommandBuffer& commandBuffer, KRSurface& surface, KRCamera* pCamera, unordered_map& visibleBounds, const KRViewport& viewport, const KRRenderPass* renderPass, bool new_frame); - void render(KRNode::RenderInfo& ri, KROctreeNode* pOctreeNode, unordered_map& visibleBounds, std::vector& remainingOctrees, std::vector& remainingOctreesTestResults, std::vector& remainingOctreesTestResultsOnly, bool bOcclusionResultsPass, bool bOcclusionTestResultsOnly); + void render(KRNode::RenderInfo& ri, bool new_frame); void updateOctree(const KRViewport& viewport); void buildOctreeForTheFirstTime(); @@ -92,6 +91,7 @@ public: std::set& getLights(); private: + void render(KRNode::RenderInfo& ri, KROctreeNode* pOctreeNode, std::vector& remainingOctrees, std::vector& remainingOctreesTestResults, std::vector& remainingOctreesTestResultsOnly, bool bOcclusionResultsPass, bool bOcclusionTestResultsOnly); KRNode* m_pRootNode; KRLight* m_pFirstLight;