Skip to content

Commit 94d092c

Browse files
klucknavpixar-oss
authored andcommitted
Add target memory for the dome light textures
To improve the loading time of dome light in Storm, this change adds a new render setting ("domeLightTexturesTargetMemory") to adjust the size of the computed cubemap and pre-processed textures of dome light. (based on github pull request #3383) Closes #3383 (Internal change: 2383587)
1 parent a71f76e commit 94d092c

File tree

7 files changed

+86
-14
lines changed

7 files changed

+86
-14
lines changed

pxr/imaging/hdSt/domeLightComputations.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,14 @@ HdSt_DomeLightComputationGPU::HdSt_DomeLightComputationGPU(
204204
const TfToken & shaderToken,
205205
const bool useCubemapAsSourceTexture,
206206
HdStSimpleLightingShaderPtr const &lightingShader,
207+
const unsigned int computedCubeMapDim,
207208
const unsigned int numLevels,
208209
const unsigned int level,
209210
const float roughness)
210211
: _shaderToken(shaderToken),
211212
_useCubemapAsSourceTexture(useCubemapAsSourceTexture),
212213
_lightingShader(lightingShader),
214+
_computedCubeMapDim(computedCubeMapDim),
213215
_numLevels(numLevels),
214216
_level(level),
215217
_roughness(roughness)
@@ -328,7 +330,7 @@ HdSt_DomeLightComputationGPU::Execute(
328330
// We are either generating a cubemap with 6 equal-sized faces from the
329331
// latlong texture, or a single 2D lookup texture that will
330332
// be the size of one face of the cubemap.
331-
width = HdSt_ComputeDomeLightCubemapDimensions(srcDim)[0];
333+
width = _computedCubeMapDim;
332334
height = width;
333335
}
334336

@@ -475,11 +477,29 @@ void HdSt_DomeLightMipmapComputationGPU::Execute(
475477
srcTextureObject->GenerateMipmaps();
476478
}
477479

478-
GfVec3i HdSt_ComputeDomeLightCubemapDimensions(
479-
const GfVec3i& srcDim)
480+
int HdSt_ComputeDomeLightCubemapWidth(
481+
const std::string& domeLightFilePath,
482+
const HgiTextureDesc& domelightTextureDesc,
483+
const unsigned int cubemapTargetMemoryMB)
480484
{
481-
const int width = std::max(srcDim[0] / 4, 1);
482-
return { width, width, 1 };
485+
// Standard cubemap width calculation
486+
const int cubemapWidth = std::max(domelightTextureDesc.dimensions[0] / 4, 1);
487+
488+
if (cubemapTargetMemoryMB == 0) {
489+
return cubemapWidth;
490+
}
491+
492+
// Cubemap width calculation based on the target memory
493+
const size_t MB = 1048576;
494+
size_t blockWidth, blockHeight;
495+
const size_t bytesPerTexel = HgiGetDataSizeOfFormat(
496+
domelightTextureDesc.format, &blockWidth, &blockHeight);
497+
const size_t bytesPerPixel = bytesPerTexel / (blockHeight * blockWidth);
498+
// The 0.75 factor is to account for all lower mipmaps
499+
const int targetMemoryWidth =
500+
sqrt((0.75 * cubemapTargetMemoryMB * MB) / (6 * bytesPerPixel));
501+
502+
return std::min(targetMemoryWidth, cubemapWidth);
483503
}
484504

485505
PXR_NAMESPACE_CLOSE_SCOPE

pxr/imaging/hdSt/domeLightComputations.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ PXR_NAMESPACE_OPEN_SCOPE
2222

2323
using HdStSimpleLightingShaderPtr =
2424
std::weak_ptr<class HdStSimpleLightingShader>;
25+
struct HgiTextureDesc;
2526

2627
////
2728
//// \class HdSt_DomeLightComputationGPU
@@ -46,6 +47,9 @@ class HdSt_DomeLightComputationGPU : public HdStComputation
4647
bool useCubemapAsSourceTexture,
4748
// Lighting shader for accessing any referenced textures.
4849
HdStSimpleLightingShaderPtr const &lightingShader,
50+
// Dimension of the dome light cubemap texture. NOTE: this is only used
51+
// when the latlong texture is input for the compuation.
52+
unsigned int computedCubeMapDim,
4953
// Number of mip levels.
5054
unsigned int numLevels = 1,
5155
// Level to be filled (0 means also to allocate texture).
@@ -69,6 +73,7 @@ class HdSt_DomeLightComputationGPU : public HdStComputation
6973
const TfToken _shaderToken;
7074
const bool _useCubemapAsSourceTexture;
7175
const HdStSimpleLightingShaderPtr _lightingShader;
76+
const unsigned int _computedCubeMapDim;
7277
const unsigned int _numLevels;
7378
const unsigned int _level;
7479
const float _roughness;
@@ -100,11 +105,13 @@ class HdSt_DomeLightMipmapComputationGPU : public HdStComputation
100105
const HdStSimpleLightingShaderPtr _lightingShader;
101106
};
102107

103-
/// Given the dimensions of a 2D dome light texture, compute the resulting
104-
/// dimensions of the cubemap.
108+
// For the given 2D dome light texture, compute the resulting dimensions of
109+
// the cubemap.
105110
HDST_API
106-
GfVec3i HdSt_ComputeDomeLightCubemapDimensions(
107-
const GfVec3i& srcDim);
111+
int HdSt_ComputeDomeLightCubemapWidth(
112+
const std::string& domeLightFilePath,
113+
const HgiTextureDesc& domelightTextureDesc,
114+
const unsigned int cubemapTargetMemoryMB);
108115

109116
PXR_NAMESPACE_CLOSE_SCOPE
110117

pxr/imaging/hdSt/renderDelegate.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ TF_DEFINE_ENV_SETTING(HD_ENABLE_GPU_TINY_PRIM_CULLING, false,
5454
TF_DEFINE_ENV_SETTING(HDST_MAX_LIGHTS, 16,
5555
"Maximum number of lights to render with");
5656

57+
TF_DEFINE_ENV_SETTING(HDST_DOME_LIGHT_CUBEMAP_TARGET_MEMORY_MB, 0,
58+
"Maximum memory target in MB for the cubemap computed "
59+
"from the latlong texture for the dome light.");
60+
5761
const TfTokenVector HdStRenderDelegate::SUPPORTED_RPRIM_TYPES =
5862
{
5963
HdPrimTypeTokens->mesh,
@@ -206,6 +210,12 @@ HdStRenderDelegate::HdStRenderDelegate(HdRenderSettingsMap const& settingsMap)
206210
"Dome light camera visibility",
207211
HdRenderSettingsTokens->domeLightCameraVisibility,
208212
VtValue(true) },
213+
HdRenderSettingDescriptor{
214+
"Maximum memory target, in MB, of calculated cubemap texture for "
215+
"dome light",
216+
HdStRenderSettingsTokens->domeLightCubemapTargetMemory,
217+
VtValue(static_cast<unsigned int>(
218+
TfGetEnvSetting(HDST_DOME_LIGHT_CUBEMAP_TARGET_MEMORY_MB))) },
209219
HdRenderSettingDescriptor{
210220
"Enable exposure compensation",
211221
HdRenderSettingsTokens->enableExposureCompensation,

pxr/imaging/hdSt/simpleLightingShader.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ HdStSimpleLightingShader::HdStSimpleLightingShader()
4747
: _lightingContext(GlfSimpleLightingContext::New())
4848
, _useLighting(true)
4949
, _glslfx(std::make_unique<HioGlslfx>(HdStPackageSimpleLightingShader()))
50+
, _domeLightCubemapTargetMemoryMB(0)
5051
, _shadowTextureHandle(
5152
NamedTextureHandle{
5253
HdStTokens->shadowCompareTextures,
@@ -596,8 +597,12 @@ HdStSimpleLightingShader::AddResourcesFromTextures(ResourceContext &ctx) const
596597
srcTextureObject->GetTextureIdentifier().GetFilePath().GetText());
597598
return;
598599
}
599-
const GfVec3i srcDim = srcTexture->GetDescriptor().dimensions;
600-
const int cubemapDim = HdSt_ComputeDomeLightCubemapDimensions(srcDim)[0];
600+
601+
const int cubemapDim =
602+
HdSt_ComputeDomeLightCubemapWidth(
603+
srcTextureObject->GetTextureIdentifier().GetFilePath().GetString(),
604+
srcTextureObject->GetTexture()->GetDescriptor(),
605+
_domeLightCubemapTargetMemoryMB);
601606
const auto numCubemapLevels = (unsigned int)(std::log2(cubemapDim) + 1);
602607

603608
// Cubemap generation from latlong texture.
@@ -607,6 +612,7 @@ HdStSimpleLightingShader::AddResourcesFromTextures(ResourceContext &ctx) const
607612
_tokens->domeLightCubemap,
608613
/*useCubemapAsSourceTexture =*/false,
609614
thisShader,
615+
cubemapDim,
610616
numCubemapLevels),
611617
HdStComputeQueueZero
612618
);
@@ -617,7 +623,8 @@ HdStSimpleLightingShader::AddResourcesFromTextures(ResourceContext &ctx) const
617623
std::make_shared<HdSt_DomeLightComputationGPU>(
618624
_tokens->domeLightBRDF,
619625
/*useCubemapAsSourceTexture =*/false,
620-
thisShader),
626+
thisShader,
627+
cubemapDim),
621628
HdStComputeQueueZero
622629
);
623630

@@ -635,7 +642,8 @@ HdStSimpleLightingShader::AddResourcesFromTextures(ResourceContext &ctx) const
635642
std::make_shared<HdSt_DomeLightComputationGPU>(
636643
_tokens->domeLightIrradiance,
637644
/*useCubemapAsSourceTexture =*/true,
638-
thisShader),
645+
thisShader,
646+
cubemapDim),
639647
HdStComputeQueueTwo
640648
);
641649

@@ -654,6 +662,7 @@ HdStSimpleLightingShader::AddResourcesFromTextures(ResourceContext &ctx) const
654662
_tokens->domeLightPrefilter,
655663
/*useCubemapAsSourceTexture =*/true,
656664
thisShader,
665+
cubemapDim,
657666
numPrefilterLevels,
658667
mipLevel,
659668
roughness),
@@ -668,5 +677,16 @@ HdStSimpleLightingShader::GetNamedTextureHandles() const
668677
return _namedTextureHandles;
669678
}
670679

680+
void
681+
HdStSimpleLightingShader::SetDomeLightCubemapTargetMemory(
682+
unsigned int targetMemoryMB)
683+
{
684+
if (_domeLightCubemapTargetMemoryMB != targetMemoryMB) {
685+
_domeLightCubemapTargetMemoryMB = targetMemoryMB;
686+
_domeLightEnvironmentTextureHandle = nullptr;
687+
_domeLightTextureHandles.clear();
688+
}
689+
}
690+
671691
PXR_NAMESPACE_CLOSE_SCOPE
672692

pxr/imaging/hdSt/simpleLightingShader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ class HdStSimpleLightingShader : public HdStLightingShader
129129
return _shadowAovBindings;
130130
}
131131

132+
HDST_API
133+
void SetDomeLightCubemapTargetMemory(unsigned int targetMemoryMB);
134+
132135
private:
133136
void _AllocateShadowTextures(
134137
HdStResourceRegistry* const resourceRegistry,
@@ -152,6 +155,10 @@ class HdStSimpleLightingShader : public HdStLightingShader
152155
NamedTextureHandleVector _namedTextureHandles;
153156

154157
NamedTextureHandleVector _domeLightTextureHandles;
158+
159+
// Maximum target memory of computed dome light cubemap texture (in MB)
160+
unsigned int _domeLightCubemapTargetMemoryMB;
161+
155162
NamedTextureHandle _shadowTextureHandle;
156163

157164
HdSt_MaterialParamVector _lightTextureParams;

pxr/imaging/hdSt/tokens.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ PXR_NAMESPACE_OPEN_SCOPE
8787
(volumeRaymarchingStepSize) \
8888
(volumeRaymarchingStepSizeLighting) \
8989
(volumeMaxTextureMemoryPerField) \
90-
(maxLights)
90+
(maxLights) \
91+
(domeLightCubemapTargetMemory)
9192

9293
// Material tags help bucket prims into different queues for draw submission.
9394
// The tags supported by Storm are:

pxr/imaging/hdx/simpleLightTask.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ HdxSimpleLightTask::Sync(HdSceneDelegate* delegate,
155155
_maxLights = size_t(
156156
renderIndex.GetRenderDelegate()->GetRenderSetting<int>(
157157
HdStRenderSettingsTokens->maxLights, 16));
158+
159+
// Update dome light textures max resolution.
160+
_lightingShader->SetDomeLightCubemapTargetMemory(
161+
static_cast<unsigned int>(
162+
renderIndex.GetRenderDelegate()->GetRenderSetting<unsigned int>(
163+
HdStRenderSettingsTokens->domeLightCubemapTargetMemory, 0)));
164+
158165
_settingsVersion = renderDelegate->GetRenderSettingsVersion();
159166
verifyNumLights = true;
160167
}

0 commit comments

Comments
 (0)