Skip to content

Commit 2d65ec4

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

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-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: 19 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,22 @@ 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_TypeId == pxr::HdPrimTypeTokens->domeLight)
446+
{
447+
// NB: we need to update IBL cube maps even if the light became invisible since
448+
// if multiple dome lights were visible, the IBL was precomputed for
449+
// the first one, which might be any of them.
450+
IBLCubemapDirty = true;
451+
}
445452
}
446453
}
447454

@@ -527,9 +534,10 @@ void HnLight::Sync(pxr::HdSceneDelegate* SceneDelegate,
527534
std::string TexturePath = GetTextureFile(*SceneDelegate, Id).GetAssetPath();
528535
if (TexturePath != m_TexturePath)
529536
{
530-
m_TexturePath = std::move(TexturePath);
531-
LightDirty = true;
532-
m_IsTextureDirty = true;
537+
m_TexturePath = std::move(TexturePath);
538+
LightDirty = true;
539+
// If the dome light is invisible, we will precompute its IBL cubemaps when it becomes visible
540+
IBLCubemapDirty = m_IsVisible;
533541
}
534542
}
535543

@@ -627,7 +635,7 @@ void HnLight::Sync(pxr::HdSceneDelegate* SceneDelegate,
627635
if (LightDirty)
628636
static_cast<HnRenderParam*>(RenderParam)->MakeAttribDirty(HnRenderParam::GlobalAttrib::Light);
629637

630-
if (m_IsTextureDirty)
638+
if (IBLCubemapDirty)
631639
static_cast<HnRenderParam*>(RenderParam)->MakeAttribDirty(HnRenderParam::GlobalAttrib::LightResources);
632640
}
633641

@@ -637,7 +645,9 @@ void HnLight::Sync(pxr::HdSceneDelegate* SceneDelegate,
637645
void HnLight::PrecomputeIBLCubemaps(HnRenderDelegate& RenderDelegate)
638646
{
639647
VERIFY_EXPR(m_TypeId == pxr::HdPrimTypeTokens->domeLight);
640-
if (!m_IsTextureDirty)
648+
const HnRenderParam* pRenderParam = static_cast<const HnRenderParam*>(RenderDelegate.GetRenderParam());
649+
const uint32_t LightResourcesVersion = pRenderParam->GetAttribVersion(HnRenderParam::GlobalAttrib::LightResources);
650+
if (LightResourcesVersion == m_LightResourcesVersion)
641651
return;
642652

643653
IRenderDevice* pDevice = RenderDelegate.GetDevice();
@@ -681,7 +691,7 @@ void HnLight::PrecomputeIBLCubemaps(HnRenderDelegate& RenderDelegate)
681691

682692
RenderDelegate.GetUSDRenderer()->PrecomputeCubemaps(pCtx, pEnvMap->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE));
683693

684-
m_IsTextureDirty = false;
694+
m_LightResourcesVersion = LightResourcesVersion;
685695
}
686696

687697
} // 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)