From: cvj Date: Fri, 15 Dec 2023 00:30:48 +0000 (-0800) Subject: Move material node sampler parameter extraction into a standalone function. X-Git-Tag: accepted/tizen/unified/x/20250428.070456~5^2~145 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=31eaa264e90f0f181d02b10b7ca79172dcadee4b;p=platform%2Fcore%2Fuifw%2FOpenUSD.git Move material node sampler parameter extraction into a standalone function. (Internal change: 2308507) --- diff --git a/pxr/imaging/hd/CMakeLists.txt b/pxr/imaging/hd/CMakeLists.txt index 4d3fbf0ff..ecf3c0bbf 100644 --- a/pxr/imaging/hd/CMakeLists.txt +++ b/pxr/imaging/hd/CMakeLists.txt @@ -14,6 +14,7 @@ pxr_library(hd cameraUtil hf pxOsd + sdr ${TBB_tbb_LIBRARY} INCLUDE_DIRS diff --git a/pxr/imaging/hd/material.cpp b/pxr/imaging/hd/material.cpp index d628b18ec..b7b5bdd57 100644 --- a/pxr/imaging/hd/material.cpp +++ b/pxr/imaging/hd/material.cpp @@ -25,8 +25,37 @@ #include "pxr/imaging/hd/tokens.h" #include "pxr/imaging/hd/perfLog.h" +#include "pxr/usd/sdr/shaderNode.h" +#include "pxr/usd/sdr/shaderProperty.h" + PXR_NAMESPACE_OPEN_SCOPE +TF_DEFINE_PRIVATE_TOKENS( + _tokens, + + (wrapS) + (wrapT) + (wrapR) + + (repeat) + (mirror) + (clamp) + (black) + (useMetadata) + + (HwUvTexture_1) + + (minFilter) + (magFilter) + + (nearest) + (linear) + (nearestMipmapNearest) + (nearestMipmapLinear) + (linearMipmapNearest) + (linearMipmapLinear) +); + HdMaterial::HdMaterial(SdfPath const& id) : HdSprim(id) { @@ -96,6 +125,164 @@ HdConvertToHdMaterialNetwork2( return result; } +// Look up value from material node parameters and fallback to +// corresponding value on given SdrNode. +template +static +auto +_ResolveParameter( + const HdMaterialNode2& node, + const SdrShaderNodeConstPtr& sdrNode, + const TfToken& name, + const T& defaultValue) -> T +{ + // First consult node parameters... + const auto it = node.parameters.find(name); + if (it != node.parameters.end()) { + const VtValue& value = it->second; + if (value.IsHolding()) { + return value.UncheckedGet(); + } + } + + // Then fallback to SdrNode. + if (sdrNode) { + if (const SdrShaderPropertyConstPtr input = + sdrNode->GetShaderInput(name)) { + const VtValue& value = input->GetDefaultValue(); + if (value.IsHolding()) { + return value.UncheckedGet(); + } + } + } + + return defaultValue; +} + +static +HdWrap +_ResolveWrapSamplerParameter( + const SdfPath& nodePath, + const HdMaterialNode2& node, + const SdrShaderNodeConstPtr& sdrNode, + const TfToken& name) +{ + const TfToken value = _ResolveParameter( + node, sdrNode, name, _tokens->useMetadata); + + if (value == _tokens->repeat) { + return HdWrapRepeat; + } + + if (value == _tokens->mirror) { + return HdWrapMirror; + } + + if (value == _tokens->clamp) { + return HdWrapClamp; + } + + if (value == _tokens->black) { + return HdWrapBlack; + } + + if (value == _tokens->useMetadata) { + if (node.nodeTypeId == _tokens->HwUvTexture_1) { + return HdWrapLegacy; + } + return HdWrapUseMetadata; + } + + TF_WARN("Unknown wrap mode on prim %s: %s", + nodePath.GetText(), value.GetText()); + + return HdWrapUseMetadata; +} + +static +HdMinFilter +_ResolveMinSamplerParameter( + const SdfPath& nodePath, + const HdMaterialNode2& node, + const SdrShaderNodeConstPtr& sdrNode) +{ + // Using linearMipmapLinear as fallback value. + + // Note that it is ambiguous whether the fallback value in the old + // texture system (usdImagingGL/textureUtils.cpp) was linear or + // linearMipmapLinear: when nothing was authored in USD for the + // min filter, linearMipmapLinear was used, but when an empty + // token was authored, linear was used. + + const TfToken value = _ResolveParameter( + node, sdrNode, _tokens->minFilter, + _tokens->linearMipmapLinear); + + if (value == _tokens->nearest) { + return HdMinFilterNearest; + } + + if (value == _tokens->linear) { + return HdMinFilterLinear; + } + + if (value == _tokens->nearestMipmapNearest) { + return HdMinFilterNearestMipmapNearest; + } + + if (value == _tokens->nearestMipmapLinear) { + return HdMinFilterNearestMipmapLinear; + } + + if (value == _tokens->linearMipmapNearest) { + return HdMinFilterLinearMipmapNearest; + } + + if (value == _tokens->linearMipmapLinear) { + return HdMinFilterLinearMipmapLinear; + } + + return HdMinFilterLinearMipmapLinear; +} + +static +HdMagFilter +_ResolveMagSamplerParameter( + const SdfPath& nodePath, + const HdMaterialNode2& node, + const SdrShaderNodeConstPtr& sdrNode) +{ + const TfToken value = _ResolveParameter( + node, sdrNode, _tokens->magFilter, _tokens->linear); + + if (value == _tokens->nearest) { + return HdMagFilterNearest; + } + + return HdMagFilterLinear; +} + +HdSamplerParameters +HdGetSamplerParameters( + const SdfPath& nodePath, + const HdMaterialNode2& node, + const SdrShaderNodeConstPtr& sdrNode) +{ + return { _ResolveWrapSamplerParameter( + nodePath, node, sdrNode, _tokens->wrapS), + _ResolveWrapSamplerParameter( + nodePath, node, sdrNode, _tokens->wrapT), + _ResolveWrapSamplerParameter( + nodePath, node, sdrNode, _tokens->wrapR), + _ResolveMinSamplerParameter( + nodePath, node, sdrNode), + _ResolveMagSamplerParameter( + nodePath, node, sdrNode), + HdBorderColorTransparentBlack, + /*enableCompare*/false, + HdCmpFuncNever }; +} + // -------------------------------------------------------------------------- // // VtValue Requirements diff --git a/pxr/imaging/hd/material.h b/pxr/imaging/hd/material.h index 2b1c20256..e17abe523 100644 --- a/pxr/imaging/hd/material.h +++ b/pxr/imaging/hd/material.h @@ -27,6 +27,8 @@ #include "pxr/pxr.h" #include "pxr/imaging/hd/api.h" #include "pxr/imaging/hd/sprim.h" +#include "pxr/imaging/hd/types.h" +#include "pxr/usd/sdr/declare.h" PXR_NAMESPACE_OPEN_SCOPE @@ -191,6 +193,14 @@ HdMaterialNetwork2 HdConvertToHdMaterialNetwork2( const HdMaterialNetworkMap & hdNetworkMap, bool *isVolume = nullptr); +/// Extracts HdSamplerParameters from the parameters on the HdMaterialNode2 if +/// present. Otherwise extracts the sampler parameters from the SdrNode. +HD_API +HdSamplerParameters HdGetSamplerParameters( + const SdfPath& nodePath, + const HdMaterialNode2& node, + const SdrShaderNodeConstPtr& sdrNode); + // VtValue requirements HD_API diff --git a/pxr/imaging/hdSt/materialNetwork.cpp b/pxr/imaging/hdSt/materialNetwork.cpp index 03f5f7853..fdb806a80 100644 --- a/pxr/imaging/hdSt/materialNetwork.cpp +++ b/pxr/imaging/hdSt/materialNetwork.cpp @@ -567,130 +567,6 @@ _ResolveParameter( return defaultValue; } -static HdWrap -_ResolveWrapSamplerParameter( - SdfPath const &nodePath, - HdMaterialNode2 const& node, - SdrShaderNodeConstPtr const &sdrNode, - TfToken const &name) -{ - const TfToken value = _ResolveParameter( - node, sdrNode, name, HdStTextureTokens->useMetadata); - - if (value == HdStTextureTokens->repeat) { - return HdWrapRepeat; - } - - if (value == HdStTextureTokens->mirror) { - return HdWrapMirror; - } - - if (value == HdStTextureTokens->clamp) { - return HdWrapClamp; - } - - if (value == HdStTextureTokens->black) { - return HdWrapBlack; - } - - if (value == HdStTextureTokens->useMetadata) { - if (node.nodeTypeId == _tokens->HwUvTexture_1) { - return HdWrapLegacy; - } - return HdWrapUseMetadata; - } - - TF_WARN("Unknown wrap mode on prim %s: %s", - nodePath.GetText(), value.GetText()); - - return HdWrapUseMetadata; -} - -static HdMinFilter -_ResolveMinSamplerParameter( - SdfPath const &nodePath, - HdMaterialNode2 const& node, - SdrShaderNodeConstPtr const &sdrNode) -{ - // Using linearMipmapLinear as fallback value. - - // Note that it is ambiguous whether the fallback value in the old - // texture system (usdImagingGL/textureUtils.cpp) was linear or - // linearMipmapLinear: when nothing was authored in USD for the - // min filter, linearMipmapLinear was used, but when an empty - // token was authored, linear was used. - - const TfToken value = _ResolveParameter( - node, sdrNode, HdStTextureTokens->minFilter, - HdStTextureTokens->linearMipmapLinear); - - if (value == HdStTextureTokens->nearest) { - return HdMinFilterNearest; - } - - if (value == HdStTextureTokens->linear) { - return HdMinFilterLinear; - } - - if (value == HdStTextureTokens->nearestMipmapNearest) { - return HdMinFilterNearestMipmapNearest; - } - - if (value == HdStTextureTokens->nearestMipmapLinear) { - return HdMinFilterNearestMipmapLinear; - } - - if (value == HdStTextureTokens->linearMipmapNearest) { - return HdMinFilterLinearMipmapNearest; - } - - if (value == HdStTextureTokens->linearMipmapLinear) { - return HdMinFilterLinearMipmapLinear; - } - - return HdMinFilterLinearMipmapLinear; -} - -static HdMagFilter -_ResolveMagSamplerParameter( - SdfPath const &nodePath, - HdMaterialNode2 const& node, - SdrShaderNodeConstPtr const &sdrNode) -{ - const TfToken value = _ResolveParameter( - node, sdrNode, HdStTextureTokens->magFilter, HdStTextureTokens->linear); - - if (value == HdStTextureTokens->nearest) { - return HdMagFilterNearest; - } - - return HdMagFilterLinear; -} - -// Resolve sampling parameters for texture node by -// looking at material node parameters and falling back to -// fallback values from Sdr. -static HdSamplerParameters -_GetSamplerParameters( - SdfPath const &nodePath, - HdMaterialNode2 const& node, - SdrShaderNodeConstPtr const &sdrNode) -{ - return { _ResolveWrapSamplerParameter( - nodePath, node, sdrNode, HdStTextureTokens->wrapS), - _ResolveWrapSamplerParameter( - nodePath, node, sdrNode, HdStTextureTokens->wrapT), - _ResolveWrapSamplerParameter( - nodePath, node, sdrNode, HdStTextureTokens->wrapR), - _ResolveMinSamplerParameter( - nodePath, node, sdrNode), - _ResolveMagSamplerParameter( - nodePath, node, sdrNode), - HdBorderColorTransparentBlack, - /*enableCompare*/false, - HdCmpFuncNever }; -} - // // We need to flip the image for the legacy HwUvTexture_1 shader node, // pre-multiply textures by their alpha if applicable, and provide a hint for @@ -981,7 +857,7 @@ _MakeMaterialParamsForTexture( { paramName, textureId, texParam.textureType, - _GetSamplerParameters(nodePath, node, sdrNode), + HdGetSamplerParameters(nodePath, node, sdrNode), memoryRequest, useTexturePrimToFindTexture, texturePrimPathForSceneDelegate });