Skip to content

Commit

Permalink
[renderer] cleaned up environment filtering code
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Oct 24, 2024
1 parent 3d83fc6 commit 85631a5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.
28 changes: 0 additions & 28 deletions runtime/Rendering/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>& entity : m_renderables[Renderer_Entity::Light])
{
if (const shared_ptr<Light>& light = entity->GetComponent<Light>())
{
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)
Expand Down
53 changes: 33 additions & 20 deletions runtime/Rendering/Renderer_Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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);
}
}

Expand Down Expand Up @@ -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> 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> light = entity->GetComponent<Light>())
static Quaternion rotation;
static float intensity;
static Color color;

for (const shared_ptr<Entity>& entity : m_renderables[Renderer_Entity::Light])
{
if (light->GetLightType() == LightType::Directional)
if (const shared_ptr<Light>& light = entity->GetComponent<Light>())
{
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();
Expand Down
5 changes: 3 additions & 2 deletions runtime/World/Components/Light.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ namespace Spartan
{
Directional,
Point,
Spot
Spot,
Max
};

enum class LightIntensity
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 85631a5

Please sign in to comment.