Skip to content

Commit

Permalink
Merge pull request #3073 from erikaharrison-adsk/adsk/feature/materia…
Browse files Browse the repository at this point in the history
…lx_topo_update_in_storm

Autodesk: Optimize MaterialX shader generation in Storm by comparing topologies

(Internal change: 2328987)
  • Loading branch information
pixar-oss committed May 29, 2024
2 parents 3ecaaed + 31a89bc commit c8d22f9
Show file tree
Hide file tree
Showing 11 changed files with 1,161 additions and 671 deletions.
18 changes: 10 additions & 8 deletions pxr/imaging/hdMtlx/hdMtlx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,15 @@ _AddMaterialXNode(
}
}

// MaterialX nodes that use textures are assumed to have a filename input
if (mxNodeDef->getNodeGroup() == "texture2d") {
if (mxHdData) {
// Save the corresponding MaterialX and Hydra names for ShaderGen
mxHdData->mxHdTextureMap[mxNodeName] = connectionName;
// Save the path to adjust parameters after traversing the network
mxHdData->hdTextureNodes.insert(hdNodePath);
// MaterialX nodes that use textures can have more than one filename input
if (mxHdData) {
for (mx::InputPtr const& mxInput : mxNodeDef->getActiveInputs()) {
if (mxInput->getType() == "filename") {
// Save the corresponding Mx and Hydra names for ShaderGen
mxHdData->mxHdTextureMap[mxNodeName].insert(mxInput->getName());
// Save the path to adjust parameters after for ShaderGen
mxHdData->hdTextureNodes.insert(hdNodePath);
}
}
}

Expand Down Expand Up @@ -602,7 +604,7 @@ HdMtlxCreateMtlxDocumentFromHdMaterialNetworkInterface(

// Validate the MaterialX Document.
{
TRACE_FUNCTION_SCOPE("Validate created Mtlx Document")
TRACE_FUNCTION_SCOPE("Validate created Mtlx Document")
std::string message;
if (!mxDoc->validate(&message)) {
TF_WARN("Validation warnings for generated MaterialX file.\n%s\n",
Expand Down
12 changes: 5 additions & 7 deletions pxr/imaging/hdMtlx/hdMtlx.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ HdMtlxConvertToString(VtValue const& hdParameterValue);

// Storing MaterialX-Hydra texture and primvar information
struct HdMtlxTexturePrimvarData {
HdMtlxTexturePrimvarData()
: mxHdTextureMap(MaterialX::StringMap()), // Mx-Hd texture name mapping
hdTextureNodes(std::set<SdfPath>()), // Paths to HdTexture Nodes
hdPrimvarNodes(std::set<SdfPath>()) {} // Paths to HdPrimvar nodes
MaterialX::StringMap mxHdTextureMap;
std::set<SdfPath> hdTextureNodes;
std::set<SdfPath> hdPrimvarNodes;
HdMtlxTexturePrimvarData() = default;
using TextureMap = std::map<std::string, std::set<std::string>>;
TextureMap mxHdTextureMap; // Mx-Hd texture name mapping
std::set<SdfPath> hdTextureNodes; // Paths to HdTexture Nodes
std::set<SdfPath> hdPrimvarNodes; // Paths to HdPrimvar nodes
};

/// Creates and returns a MaterialX Document from the given HdMaterialNetwork2
Expand Down
2 changes: 1 addition & 1 deletion pxr/imaging/hdSt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2966,7 +2966,7 @@ pxr_register_test(testHdStMaterialXShaderGen_SStextured
TESTENV testHdStMaterialXShaderGen
)
pxr_register_test(testHdStMaterialXShaderGen_UsdPSglass
COMMAND "${CMAKE_INSTALL_PREFIX}/tests/testHdStMaterialXShaderGen --filename usd_preview_surface_glass.mtlx --materialTag translucent"
COMMAND "${CMAKE_INSTALL_PREFIX}/tests/testHdStMaterialXShaderGen --filename usd_preview_surface_glass.mtlx"
STDOUT_REDIRECT shadergen_UsdPSglass.out
EXPECTED_RETURN_CODE 0
POST_COMMAND "diff -B -b shadergen_UsdPSglass.out ${CMAKE_INSTALL_PREFIX}/tests/ctest/testHdStMaterialXShaderGen/baseline/shadergen_UsdPSglass.out"
Expand Down
17 changes: 15 additions & 2 deletions pxr/imaging/hdSt/materialNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,21 @@ _MakeMaterialParamsForTexture(
NdrTokenVec const& assetIdentifierPropertyNames =
sdrNode->GetAssetIdentifierInputNames();

if (assetIdentifierPropertyNames.size() == 1) {
TfToken const& fileProp = assetIdentifierPropertyNames[0];
if (!assetIdentifierPropertyNames.empty()) {
TfToken fileProp = assetIdentifierPropertyNames[0];

// Some MaterialX nodes can have multiple file inputs. Take the first
// one that matches the param name. If we lookup a <trilinear> texture
// against an output named "N42_fileY", we will find the right one.
if (assetIdentifierPropertyNames.size() > 1) {
for (auto const& propName: assetIdentifierPropertyNames) {
if (TfStringEndsWith(outputName.GetString(), propName)) {
fileProp = propName;
break;
}
}
}

auto const& it = node.parameters.find(fileProp);
if (it != node.parameters.end()){
const VtValue &v = it->second;
Expand Down
Loading

0 comments on commit c8d22f9

Please sign in to comment.