From cca65d463166090a918831d69162deb6d1caade8 Mon Sep 17 00:00:00 2001 From: Keli Li Date: Thu, 24 Oct 2024 09:53:00 -0400 Subject: [PATCH] Add new dome-light-textures-max-resolution render setting --- pxr/imaging/hdSt/domeLightComputations.cpp | 12 +++-- pxr/imaging/hdSt/domeLightComputations.h | 6 +++ pxr/imaging/hdSt/renderDelegate.cpp | 7 +++ pxr/imaging/hdSt/simpleLightingShader.cpp | 57 +++++++++++++++++----- pxr/imaging/hdSt/simpleLightingShader.h | 6 +++ pxr/imaging/hdSt/tokens.h | 3 +- pxr/imaging/hdx/simpleLightTask.cpp | 6 +++ 7 files changed, 81 insertions(+), 16 deletions(-) diff --git a/pxr/imaging/hdSt/domeLightComputations.cpp b/pxr/imaging/hdSt/domeLightComputations.cpp index ea5ead4d1f..f637eff003 100644 --- a/pxr/imaging/hdSt/domeLightComputations.cpp +++ b/pxr/imaging/hdSt/domeLightComputations.cpp @@ -32,11 +32,15 @@ PXR_NAMESPACE_OPEN_SCOPE HdSt_DomeLightComputationGPU::HdSt_DomeLightComputationGPU( const TfToken &shaderToken, HdStSimpleLightingShaderPtr const &lightingShader, + const unsigned int outputWidth, + const unsigned int outputHeight, const unsigned int numLevels, const unsigned int level, const float roughness) : _shaderToken(shaderToken), _lightingShader(lightingShader), + _outputWidth(outputWidth), + _outputHeight(outputHeight), _numLevels(numLevels), _level(level), _roughness(roughness) @@ -167,10 +171,10 @@ HdSt_DomeLightComputationGPU::Execute( // Size of texture to be created. // Downsize larger textures - bool downsize = (srcDim[0] > 256 && srcDim[1] > 256); - int width = downsize ? srcDim[0] / 2 : srcDim[0]; - int height = downsize ? srcDim[1] / 2 : srcDim[1]; - + bool downsize = (_outputWidth > 256 && _outputHeight > 256); + int width = downsize ? _outputWidth / 2 : _outputWidth; + int height = downsize ? _outputHeight / 2 : _outputHeight; + // Make sure dimensions align with the local size used in the Compute Shader width = _MakeMultipleOf(width, localSize); height = _MakeMultipleOf(height, localSize); diff --git a/pxr/imaging/hdSt/domeLightComputations.h b/pxr/imaging/hdSt/domeLightComputations.h index f496164f06..117fd980c6 100644 --- a/pxr/imaging/hdSt/domeLightComputations.h +++ b/pxr/imaging/hdSt/domeLightComputations.h @@ -46,6 +46,10 @@ class HdSt_DomeLightComputationGPU : public HdStComputation const TfToken & shaderToken, // Lighting shader that remembers the GL texture names HdStSimpleLightingShaderPtr const &lightingShader, + // Width of output textures. + unsigned int outputWidth, + // Height of output textures. + unsigned int outputHeight, // Number of mip levels. unsigned int numLevels = 1, // Level to be filled (0 means also to allocate texture) @@ -67,6 +71,8 @@ class HdSt_DomeLightComputationGPU : public HdStComputation private: const TfToken _shaderToken; HdStSimpleLightingShaderPtr const _lightingShader; + const unsigned int _outputWidth; + const unsigned int _outputHeight; const unsigned int _numLevels; const unsigned int _level; const float _roughness; diff --git a/pxr/imaging/hdSt/renderDelegate.cpp b/pxr/imaging/hdSt/renderDelegate.cpp index 37d22c5127..c0d87d69bd 100644 --- a/pxr/imaging/hdSt/renderDelegate.cpp +++ b/pxr/imaging/hdSt/renderDelegate.cpp @@ -54,6 +54,9 @@ TF_DEFINE_ENV_SETTING(HD_ENABLE_GPU_TINY_PRIM_CULLING, false, TF_DEFINE_ENV_SETTING(HDST_MAX_LIGHTS, 16, "Maximum number of lights to render with"); +TF_DEFINE_ENV_SETTING(HDST_DOME_LIGHT_TEXTURES_MAX_RES, 8192, + "Maximum resolution of processed textures for dome light"); + const TfTokenVector HdStRenderDelegate::SUPPORTED_RPRIM_TYPES = { HdPrimTypeTokens->mesh, @@ -192,6 +195,10 @@ HdStRenderDelegate::HdStRenderDelegate(HdRenderSettingsMap const& settingsMap) "Maximum number of lights", HdStRenderSettingsTokens->maxLights, VtValue(int(TfGetEnvSetting(HDST_MAX_LIGHTS))) }, + HdRenderSettingDescriptor{ + "Maximum resolution of processed textures for dome light", + HdStRenderSettingsTokens->domeLightTexturesMaxResolution, + VtValue(static_cast(TfGetEnvSetting(HDST_DOME_LIGHT_TEXTURES_MAX_RES))) }, }; _PopulateDefaultSettings(_settingDescriptors); diff --git a/pxr/imaging/hdSt/simpleLightingShader.cpp b/pxr/imaging/hdSt/simpleLightingShader.cpp index 70d7b7f34b..26721306dd 100644 --- a/pxr/imaging/hdSt/simpleLightingShader.cpp +++ b/pxr/imaging/hdSt/simpleLightingShader.cpp @@ -49,6 +49,7 @@ HdStSimpleLightingShader::HdStSimpleLightingShader() : _lightingContext(GlfSimpleLightingContext::New()) , _useLighting(true) , _glslfx(std::make_unique(HdStPackageSimpleLightingShader())) + , _domeLightTexturesMaxResolution(8192) , _renderParam(nullptr) { } @@ -593,14 +594,6 @@ HdStSimpleLightingShader::AddResourcesFromTextures(ResourceContext &ctx) const std::const_pointer_cast( shared_from_this())); - // Irriadiance map computations. - ctx.AddComputation( - nullptr, - std::make_shared( - _tokens->domeLightIrradiance, - thisShader), - HdStComputeQueueZero); - // Calculate the number of mips for the prefilter texture // Note that the size of the prefilter texture is half the size of the // original Environment Map (srcTextureObject) @@ -619,8 +612,36 @@ HdStSimpleLightingShader::AddResourcesFromTextures(ResourceContext &ctx) const } const GfVec3i srcDim = srcTexture->GetDescriptor().dimensions; - const unsigned int numPrefilterLevels = - std::max((unsigned int)(std::log2(std::max(srcDim[0], srcDim[1]))), 1u); + // Size of texture to be created. + unsigned int outputWidth = srcDim[0]; + unsigned int outputHeight = srcDim[1]; + + // Check max resolution. + unsigned int maxDim = std::max(outputWidth, outputHeight); + if (maxDim > _domeLightTexturesMaxResolution) { + float ratio = outputWidth / outputHeight; + if (outputWidth >= outputHeight) { + outputWidth = _domeLightTexturesMaxResolution; + outputHeight = outputWidth / ratio; + } + else { + outputHeight = _domeLightTexturesMaxResolution; + outputWidth = outputHeight * ratio; + } + } + + // Irriadiance map computations. + ctx.AddComputation( + nullptr, + std::make_shared( + _tokens->domeLightIrradiance, + thisShader, + outputWidth, + outputHeight), + HdStComputeQueueZero); + + const unsigned int numPrefilterLevels = + std::max(static_cast(std::log2(std::max(outputWidth, outputHeight))), 1u); // Prefilter map computations. mipLevel = 0 allocates texture. for (unsigned int mipLevel = 0; mipLevel < numPrefilterLevels; ++mipLevel) { @@ -632,6 +653,8 @@ HdStSimpleLightingShader::AddResourcesFromTextures(ResourceContext &ctx) const std::make_shared( _tokens->domeLightPrefilter, thisShader, + outputWidth, + outputHeight, numPrefilterLevels, mipLevel, roughness), @@ -643,7 +666,9 @@ HdStSimpleLightingShader::AddResourcesFromTextures(ResourceContext &ctx) const nullptr, std::make_shared( _tokens->domeLightBRDF, - thisShader), + thisShader, + outputWidth, + outputHeight), HdStComputeQueueZero); } @@ -653,5 +678,15 @@ HdStSimpleLightingShader::GetNamedTextureHandles() const return _namedTextureHandles; } +void +HdStSimpleLightingShader::SetDomeLightTexturesMaxResolution(unsigned int maxRes) +{ + if (_domeLightTexturesMaxResolution != maxRes) { + _domeLightTexturesMaxResolution = maxRes; + _domeLightEnvironmentTextureHandle = nullptr; + _domeLightTextureHandles.clear(); + } +} + PXR_NAMESPACE_CLOSE_SCOPE diff --git a/pxr/imaging/hdSt/simpleLightingShader.h b/pxr/imaging/hdSt/simpleLightingShader.h index 18bbfc7415..a9b75cceca 100644 --- a/pxr/imaging/hdSt/simpleLightingShader.h +++ b/pxr/imaging/hdSt/simpleLightingShader.h @@ -121,6 +121,9 @@ class HdStSimpleLightingShader : public HdStLightingShader return _shadowAovBindings; } + HDST_API + void SetDomeLightTexturesMaxResolution(unsigned int maxRes); + private: SdfPath _GetAovPath(TfToken const &aov, size_t shadowIndex) const; void _ResizeOrCreateBufferForAov(size_t shadowIndex) const; @@ -143,6 +146,9 @@ class HdStSimpleLightingShader : public HdStLightingShader NamedTextureHandleVector _namedTextureHandles; NamedTextureHandleVector _domeLightTextureHandles; + // Maximum resolution of processed textures for dome light + unsigned int _domeLightTexturesMaxResolution; + NamedTextureHandleVector _shadowTextureHandles; HdSt_MaterialParamVector _lightTextureParams; diff --git a/pxr/imaging/hdSt/tokens.h b/pxr/imaging/hdSt/tokens.h index 8ac07a6e79..4ec2f679e7 100644 --- a/pxr/imaging/hdSt/tokens.h +++ b/pxr/imaging/hdSt/tokens.h @@ -87,7 +87,8 @@ PXR_NAMESPACE_OPEN_SCOPE (volumeRaymarchingStepSize) \ (volumeRaymarchingStepSizeLighting) \ (volumeMaxTextureMemoryPerField) \ - (maxLights) + (maxLights) \ + (domeLightTexturesMaxResolution) // Material tags help bucket prims into different queues for draw submission. // The tags supported by Storm are: diff --git a/pxr/imaging/hdx/simpleLightTask.cpp b/pxr/imaging/hdx/simpleLightTask.cpp index 6b7ad561cd..00a6cf6c8c 100644 --- a/pxr/imaging/hdx/simpleLightTask.cpp +++ b/pxr/imaging/hdx/simpleLightTask.cpp @@ -155,6 +155,12 @@ HdxSimpleLightTask::Sync(HdSceneDelegate* delegate, _maxLights = size_t( renderIndex.GetRenderDelegate()->GetRenderSetting( HdStRenderSettingsTokens->maxLights, 16)); + + // Update dome light textures max resolution. + _lightingShader->SetDomeLightTexturesMaxResolution(static_cast( + renderIndex.GetRenderDelegate()->GetRenderSetting( + HdStRenderSettingsTokens->domeLightTexturesMaxResolution, 8192))); + _settingsVersion = renderDelegate->GetRenderSettingsVersion(); verifyNumLights = true; }