diff --git a/lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.cpp b/lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.cpp index dc1e5aef7..927d704a7 100644 --- a/lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.cpp +++ b/lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.cpp @@ -85,7 +85,7 @@ class LobePrunerImpl LobePrunerImpl(LobePrunerImpl&&) = delete; LobePrunerImpl& operator=(LobePrunerImpl&&) = delete; - bool getOptimizedNodeDef(const mx::Node& node, mx::NodeDefPtr& nodeDef); + mx::NodeDefPtr getOptimizedNodeDef(const mx::Node& node); mx::StringVec getOptimizedAttributeNames(const mx::NodeDefPtr& nodeDef) const; @@ -110,7 +110,7 @@ class LobePrunerImpl const mx::NodeGraphPtr& ng, const mx::NodeDefPtr& nd); mx::NodeDefPtr - ensureLibraryHasOptimizedShader(const PXR_NS::TfToken& nodeDefName, const std::string& flags); + getOrAddOptimizedNodeDef(const PXR_NS::TfToken& nodeDefName, const std::string& flags); void optimizeZeroValue( mx::NodeGraphPtr& optimizedNodeGraph, const OptimizableValueMap& optimizationMap, @@ -290,7 +290,7 @@ void LobePrunerImpl::optimizeLibrary(const MaterialX::DocumentPtr& library) } if (canOptimize) { - const auto optimizedNodeDef = ensureLibraryHasOptimizedShader(ndName, flags); + const auto optimizedNodeDef = getOrAddOptimizedNodeDef(ndName, flags); // Replace the node with an optimized one: const auto nsPrefix = optimizedNodeDef->hasNamespace() ? optimizedNodeDef->getNamespace() + ":" @@ -331,17 +331,17 @@ void LobePrunerImpl::addOptimizableValue( valueMap.find(value)->second.push_back(PXR_NS::TfToken(input->getParent()->getName())); } -bool LobePrunerImpl::getOptimizedNodeDef(const mx::Node& node, mx::NodeDefPtr& nodeDef) +mx::NodeDefPtr LobePrunerImpl::getOptimizedNodeDef(const mx::Node& node) { const auto& nd = node.getNodeDef(); if (!nd) { - return false; + return {}; } const auto ndName = PXR_NS::TfToken(nd->getName()); const auto ndIt = _prunerData.find(ndName); if (ndIt == _prunerData.end()) { - return false; + return {}; } std::string flags(ndIt->second._attributeData.size(), 'x'); @@ -377,11 +377,10 @@ bool LobePrunerImpl::getOptimizedNodeDef(const mx::Node& node, mx::NodeDefPtr& n } if (canOptimize) { - nodeDef = ensureLibraryHasOptimizedShader(ndName, flags); - return true; + return getOrAddOptimizedNodeDef(ndName, flags); } - return false; + return {}; } mx::StringVec LobePrunerImpl::getOptimizedAttributeNames(const mx::NodeDefPtr& nodeDef) const @@ -444,7 +443,7 @@ PXR_NS::TfToken LobePrunerImpl::getOptimizedNodeId(const PXR_NS::HdMaterialNode2 } if (canOptimize) { - return PXR_NS::TfToken(ensureLibraryHasOptimizedShader(node.nodeTypeId, flags)->getName()); + return PXR_NS::TfToken(getOrAddOptimizedNodeDef(node.nodeTypeId, flags)->getName()); } return retVal; @@ -455,7 +454,7 @@ bool LobePrunerImpl::isOptimizedNodeId(const PXR_NS::TfToken& nodeId) return _optimizedNodeIds.count(nodeId) != 0; } -mx::NodeDefPtr LobePrunerImpl::ensureLibraryHasOptimizedShader( +mx::NodeDefPtr LobePrunerImpl::getOrAddOptimizedNodeDef( const PXR_NS::TfToken& nodeDefName, const std::string& flags) { @@ -671,9 +670,9 @@ void LobePruner::optimizeLibrary(const MaterialX::DocumentPtr& library) } } -bool LobePruner::getOptimizedNodeDef(const mx::Node& node, mx::NodeDefPtr& nodeDef) +mx::NodeDefPtr LobePruner::getOptimizedNodeDef(const mx::Node& node) { - return _impl ? _impl->getOptimizedNodeDef(node, nodeDef) : false; + return _impl ? _impl->getOptimizedNodeDef(node) : mx::NodeDefPtr {}; } mx::StringVec LobePruner::getOptimizedAttributeNames(const mx::NodeDefPtr& nodeDef) const diff --git a/lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.h b/lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.h index 777777855..418fbd33c 100644 --- a/lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.h +++ b/lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.h @@ -63,10 +63,9 @@ class MAYAUSD_CORE_PUBLIC LobePruner /*! Checks if a node is optimizable and if this is the case, create the optimized NodeDef and * NodeGraph in the library and return the optimized NodeDef. * @param[in] node is a node we want to optimize. All nodes are welcome. - * @param[out] nodeDef is the NodeDef of the optimized node. - * \return true if an optimization was found + * \return the optimized NodeDef if one was found, nullptr otherwise */ - bool getOptimizedNodeDef(const mx::Node& node, mx::NodeDefPtr& nodeDef); + mx::NodeDefPtr getOptimizedNodeDef(const mx::Node& node); /*! Get the list of attribute names that are optimization targets for a specific NodeDef. * @param[in] nodeDef is preferably node definition that has previously been optimized, but all diff --git a/lib/mayaUsd/render/MaterialXGenOgsXml/ShaderGenUtil.cpp b/lib/mayaUsd/render/MaterialXGenOgsXml/ShaderGenUtil.cpp index 2a8ff3880..a01141321 100644 --- a/lib/mayaUsd/render/MaterialXGenOgsXml/ShaderGenUtil.cpp +++ b/lib/mayaUsd/render/MaterialXGenOgsXml/ShaderGenUtil.cpp @@ -233,8 +233,9 @@ mx::NodePtr TopoNeutralGraph::cloneNode(const mx::Node& node, mx::GraphElement& if (!nodeDef) { throw mx::Exception("Ambiguous node is not fully resolvable"); } - mx::NodeDefPtr optimizedNodeDef; - if (_lobePruner && _lobePruner->getOptimizedNodeDef(node, optimizedNodeDef)) { + auto optimizedNodeDef + = _lobePruner ? _lobePruner->getOptimizedNodeDef(node) : mx::NodeDefPtr {}; + if (optimizedNodeDef) { const auto nsPrefix = optimizedNodeDef->hasNamespace() ? optimizedNodeDef->getNamespace() + ":" : std::string {}; diff --git a/test/lib/mayaUsd/utils/test_ShaderGenUtils.cpp b/test/lib/mayaUsd/utils/test_ShaderGenUtils.cpp index c60799cd0..d6929363b 100644 --- a/test/lib/mayaUsd/utils/test_ShaderGenUtils.cpp +++ b/test/lib/mayaUsd/utils/test_ShaderGenUtils.cpp @@ -179,15 +179,16 @@ TEST(ShaderGenUtils, lobePruner) const auto node = doc->addNode("standard_surface", "bob", "surfaceshader"); - mx::NodeDefPtr optimizedNodeDef; - ASSERT_TRUE(lobePruner->getOptimizedNodeDef(*node, optimizedNodeDef)); + auto optimizedNodeDef = lobePruner->getOptimizedNodeDef(*node); + ASSERT_TRUE(optimizedNodeDef); // An x means can not optimize on that attribute // A 0 means we optimized due to this value being zero ASSERT_EQ(optimizedNodeDef->getNodeString(), "standard_surface_x0000x00x000"); auto input = node->addInputFromNodeDef("subsurface"); input->setValueString("1.0"); - ASSERT_TRUE(lobePruner->getOptimizedNodeDef(*node, optimizedNodeDef)); + optimizedNodeDef = lobePruner->getOptimizedNodeDef(*node); + ASSERT_TRUE(optimizedNodeDef); // Now have a 1 for subsurface since we can also optimize the 1 value for mix nodes. ASSERT_EQ(optimizedNodeDef->getNodeString(), "standard_surface_x0000x00x010");