Move material node sampler parameter extraction into a standalone function.
authorcvj <cvj@users.noreply.github.com>
Fri, 15 Dec 2023 00:30:48 +0000 (16:30 -0800)
committerpixar-oss <pixar-oss@users.noreply.github.com>
Fri, 15 Dec 2023 00:30:48 +0000 (16:30 -0800)
(Internal change: 2308507)

pxr/imaging/hd/CMakeLists.txt
pxr/imaging/hd/material.cpp
pxr/imaging/hd/material.h
pxr/imaging/hdSt/materialNetwork.cpp

index 4d3fbf0ff179ecf8f145a82d0f843eee06eaddd0..ecf3c0bbf65dd7ff7ae285d704c70ed0d3c7157d 100644 (file)
@@ -14,6 +14,7 @@ pxr_library(hd
         cameraUtil
         hf
         pxOsd
+        sdr
         ${TBB_tbb_LIBRARY}
 
     INCLUDE_DIRS
index d628b18ec9b70d155b0a6ef824cca17d83807cec..b7b5bdd57d4736b91f2a850c2730e97e4616e4f3 100644 (file)
 #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<typename T>
+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<T>()) {
+            return value.UncheckedGet<T>();
+        }
+    }
+
+    // Then fallback to SdrNode.
+    if (sdrNode) {
+        if (const SdrShaderPropertyConstPtr input =
+                                        sdrNode->GetShaderInput(name)) {
+            const VtValue& value = input->GetDefaultValue();
+            if (value.IsHolding<T>()) {
+                return value.UncheckedGet<T>();
+            }
+        }
+    }
+
+    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
index 2b1c20256d02b8d37369308ede2fffc252bc3f11..e17abe52384a2bcade818335fe5ea5a1624ae707 100644 (file)
@@ -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
index 03f5f78535d9b2d171807b94abe7f52a787f32e6..fdb806a8043f75b48ff62b27e0c601ffe68ae4c1 100644 (file)
@@ -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 });