Skip to content

Commit a41a559

Browse files
Hydrogent: use IBL from the first visible dome light
1 parent 6047fab commit a41a559

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

Hydrogent/interface/HnLight.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 Diligent Graphics LLC
2+
* Copyright 2023-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -102,9 +102,9 @@ class HnLight final : public pxr::HdLight
102102
float3 m_Position;
103103
float3 m_Direction;
104104
GLTF::Light m_Params;
105-
bool m_IsVisible = true;
106-
bool m_IsShadowMapDirty = true;
107-
bool m_IsTextureDirty = true;
105+
bool m_IsVisible = true;
106+
bool m_IsShadowMapDirty = true;
107+
Uint32 m_LightResourcesVersion = ~0u;
108108

109109
float4x4 m_ViewMatrix;
110110
float4x4 m_ProjMatrix;

Hydrogent/src/HnLight.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 Diligent Graphics LLC
2+
* Copyright 2023-2025 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -433,15 +433,20 @@ void HnLight::Sync(pxr::HdSceneDelegate* SceneDelegate,
433433

434434
const pxr::SdfPath& Id = GetId();
435435

436-
bool LightDirty = false;
437-
436+
bool LightDirty = false;
437+
bool IBLCubemapDirty = false;
438438
{
439439
bool IsVisible = SceneDelegate->GetVisible(Id);
440440
if (IsVisible != m_IsVisible)
441441
{
442442
m_IsVisible = IsVisible;
443443
LightDirty = true;
444444
m_IsShadowMapDirty = true;
445+
if (m_IsVisible && m_TypeId == pxr::HdPrimTypeTokens->domeLight)
446+
{
447+
// This dome light became visible, so we need to precompute the IBL cubemaps
448+
IBLCubemapDirty = true;
449+
}
445450
}
446451
}
447452

@@ -527,9 +532,10 @@ void HnLight::Sync(pxr::HdSceneDelegate* SceneDelegate,
527532
std::string TexturePath = GetTextureFile(*SceneDelegate, Id).GetAssetPath();
528533
if (TexturePath != m_TexturePath)
529534
{
530-
m_TexturePath = std::move(TexturePath);
531-
LightDirty = true;
532-
m_IsTextureDirty = true;
535+
m_TexturePath = std::move(TexturePath);
536+
LightDirty = true;
537+
// If the dome light is invisible, we will precompute its IBL cubemaps when it becomes visible
538+
IBLCubemapDirty = m_IsVisible;
533539
}
534540
}
535541

@@ -627,7 +633,7 @@ void HnLight::Sync(pxr::HdSceneDelegate* SceneDelegate,
627633
if (LightDirty)
628634
static_cast<HnRenderParam*>(RenderParam)->MakeAttribDirty(HnRenderParam::GlobalAttrib::Light);
629635

630-
if (m_IsTextureDirty)
636+
if (IBLCubemapDirty)
631637
static_cast<HnRenderParam*>(RenderParam)->MakeAttribDirty(HnRenderParam::GlobalAttrib::LightResources);
632638
}
633639

@@ -637,7 +643,9 @@ void HnLight::Sync(pxr::HdSceneDelegate* SceneDelegate,
637643
void HnLight::PrecomputeIBLCubemaps(HnRenderDelegate& RenderDelegate)
638644
{
639645
VERIFY_EXPR(m_TypeId == pxr::HdPrimTypeTokens->domeLight);
640-
if (!m_IsTextureDirty)
646+
const HnRenderParam* pRenderParam = static_cast<const HnRenderParam*>(RenderDelegate.GetRenderParam());
647+
const uint32_t LightResourcesVersion = pRenderParam->GetAttribVersion(HnRenderParam::GlobalAttrib::LightResources);
648+
if (LightResourcesVersion == m_LightResourcesVersion)
641649
return;
642650

643651
IRenderDevice* pDevice = RenderDelegate.GetDevice();
@@ -681,7 +689,7 @@ void HnLight::PrecomputeIBLCubemaps(HnRenderDelegate& RenderDelegate)
681689

682690
RenderDelegate.GetUSDRenderer()->PrecomputeCubemaps(pCtx, pEnvMap->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE));
683691

684-
m_IsTextureDirty = false;
692+
m_LightResourcesVersion = LightResourcesVersion;
685693
}
686694

687695
} // namespace USD

Hydrogent/src/HnRenderDelegate.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,12 @@ void HnRenderDelegate::CommitResources(pxr::HdChangeTracker* tracker)
917917
HnLight* pLight = it->second;
918918
VERIFY_EXPR(pLight->GetTypeId() == pxr::HdPrimTypeTokens->domeLight);
919919

920+
if (!pLight->IsVisible())
921+
{
922+
// Skip invisible dome lights
923+
continue;
924+
}
925+
920926
if (DomeLight == nullptr)
921927
{
922928
pLight->PrecomputeIBLCubemaps(*this);

Hydrogent/src/Tasks/HnRenderEnvMapTask.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,33 @@ void HnRenderEnvMapTask::Prepare(pxr::HdTaskContext* TaskCtx,
180180
EnvMapAttribs.Options = EnvMapRenderer::OPTION_FLAG_COMPUTE_MOTION_VECTORS;
181181

182182
{
183-
const auto& Lights = pRenderDelegate->GetLights();
184-
const auto dome_light_it = Lights.find(pxr::HdPrimTypeTokens->domeLight);
185-
if (dome_light_it != Lights.end())
183+
const auto& Lights = pRenderDelegate->GetLights();
184+
auto dome_lights_range = Lights.equal_range(pxr::HdPrimTypeTokens->domeLight);
185+
HnLight* pDomeLight = nullptr;
186+
for (auto it = dome_lights_range.first; it != dome_lights_range.second; ++it)
186187
{
187-
if (const HnLight* pDomeLight = dome_light_it->second)
188+
pDomeLight = it->second;
189+
if (pDomeLight == nullptr)
188190
{
189-
EnvMapAttribs.Scale = pDomeLight->GetParams().Color * pDomeLight->GetParams().Intensity;
191+
UNEXPECTED("Dome light is null");
192+
continue;
193+
}
194+
VERIFY_EXPR(pDomeLight->GetTypeId() == pxr::HdPrimTypeTokens->domeLight);
195+
196+
if (pDomeLight->IsVisible())
197+
{
198+
break;
199+
}
200+
else
201+
{
202+
// Skip invisible dome lights
203+
pDomeLight = nullptr;
190204
}
191205
}
206+
207+
EnvMapAttribs.Scale = pDomeLight != nullptr ?
208+
pDomeLight->GetParams().Color * pDomeLight->GetParams().Intensity :
209+
float3{0};
192210
}
193211

194212
bool UseReverseDepth = false;

0 commit comments

Comments
 (0)