From 85631a54331288b1418b20ce241b9c27fadb79b7 Mon Sep 17 00:00:00 2001 From: Panos Karabelas Date: Thu, 24 Oct 2024 03:42:42 +0100 Subject: [PATCH] [renderer] cleaned up environment filtering code --- runtime/Rendering/Renderer.cpp | 28 -------------- runtime/Rendering/Renderer_Passes.cpp | 53 +++++++++++++++++---------- runtime/World/Components/Light.h | 5 ++- 3 files changed, 36 insertions(+), 50 deletions(-) diff --git a/runtime/Rendering/Renderer.cpp b/runtime/Rendering/Renderer.cpp index ec68f3b62..12bd5b29b 100644 --- a/runtime/Rendering/Renderer.cpp +++ b/runtime/Rendering/Renderer.cpp @@ -599,34 +599,6 @@ namespace Spartan UpdateConstantBufferFrame(cmd_list_graphics); AddLinesToBeRendered(); - - // filter environment on directional light change - { - static Quaternion rotation; - static float intensity; - static Color color; - - for (const shared_ptr& entity : m_renderables[Renderer_Entity::Light]) - { - if (const shared_ptr& light = entity->GetComponent()) - { - if (light->GetLightType() == LightType::Directional) - { - if (light->GetEntity()->GetRotation() != rotation || - light->GetIntensityLumens() != intensity || - light->GetColor() != color - ) - { - rotation = light->GetEntity()->GetRotation(); - intensity = light->GetIntensityLumens(); - color = light->GetColor(); - - m_environment_mips_to_filter_count = GetRenderTarget(Renderer_RenderTarget::skysphere)->GetMipCount() - 1; - } - } - } - } - } } void Renderer::DrawString(const string& text, const Vector2& position_screen_percentage) diff --git a/runtime/Rendering/Renderer_Passes.cpp b/runtime/Rendering/Renderer_Passes.cpp index 7428d7862..85608e265 100644 --- a/runtime/Rendering/Renderer_Passes.cpp +++ b/runtime/Rendering/Renderer_Passes.cpp @@ -389,20 +389,17 @@ namespace Spartan light_integration_brdf_speculat_lut_completed = true; } - if (m_environment_mips_to_filter_count > 0) - { - Pass_Light_Integration_EnvironmentPrefilter(cmd_list_graphics); - } + Pass_Light_Integration_EnvironmentPrefilter(cmd_list_graphics); } if (shared_ptr camera = GetCamera()) { // shadow maps { - Pass_ShadowMaps(cmd_list_graphics, false); + //Pass_ShadowMaps(cmd_list_graphics, false); if (mesh_index_transparent != -1) { - Pass_ShadowMaps(cmd_list_graphics, true); + // Pass_ShadowMaps(cmd_list_graphics, true); } } @@ -1368,27 +1365,43 @@ namespace Spartan void Renderer::Pass_Light_Integration_EnvironmentPrefilter(RHI_CommandList* cmd_list) { - // acquire resources - RHI_Texture* tex_environment = GetRenderTarget(Renderer_RenderTarget::skysphere); - RHI_Shader* shader_c = GetShader(Renderer_Shader::light_integration_environment_filter_c); - if (!shader_c || !shader_c->IsCompiled()) - return; - - // this pass is costly and can cause the engine to freeze for a few seconds - // so while the light is moving, we don't update the environment map - auto& entities = m_renderables[Renderer_Entity::Light]; - for (shared_ptr entity : entities) + // find a directional light, check if it has changed and update the mip count that we need to process + if (m_environment_mips_to_filter_count == 0) { - if (shared_ptr light = entity->GetComponent()) + static Quaternion rotation; + static float intensity; + static Color color; + + for (const shared_ptr& entity : m_renderables[Renderer_Entity::Light]) { - if (light->GetLightType() == LightType::Directional) + if (const shared_ptr& light = entity->GetComponent()) { - if (light->GetEntity()->IsMoving()) - return; + if (light->GetLightType() == LightType::Directional) + { + // filtering is very expensive hence why we try to minimize it + if (!light->GetEntity()->IsMoving() && // has a rest time (a couple of seconds) + (light->GetEntity()->GetRotation() != rotation || light->GetIntensityLumens() != intensity || light->GetColor() != color)) + { + rotation = light->GetEntity()->GetRotation(); + intensity = light->GetIntensityLumens(); + color = light->GetColor(); + + m_environment_mips_to_filter_count = GetRenderTarget(Renderer_RenderTarget::skysphere)->GetMipCount() - 1; + } + } } } } + if (m_environment_mips_to_filter_count < 1) + return; + + // acquire resources + RHI_Texture* tex_environment = GetRenderTarget(Renderer_RenderTarget::skysphere); + RHI_Shader* shader_c = GetShader(Renderer_Shader::light_integration_environment_filter_c); + if (!shader_c || !shader_c->IsCompiled()) + return; + cmd_list->BeginTimeblock("light_integration_environment_filter"); uint32_t mip_count = tex_environment->GetMipCount(); diff --git a/runtime/World/Components/Light.h b/runtime/World/Components/Light.h index 7666039b6..59e5020a1 100644 --- a/runtime/World/Components/Light.h +++ b/runtime/World/Components/Light.h @@ -41,7 +41,8 @@ namespace Spartan { Directional, Point, - Spot + Spot, + Max }; enum class LightIntensity @@ -148,7 +149,7 @@ namespace Spartan // misc uint32_t m_flags = 0; - LightType m_light_type = LightType::Directional; + LightType m_light_type = LightType::Max; Color m_color_rgb = Color::standard_black;; float m_temperature_kelvin = 0.0f; float m_range = 0.0f;