Skip to content

Commit

Permalink
Simplified and self documenting as requested.
Browse files Browse the repository at this point in the history
  • Loading branch information
JGamache-autodesk committed Dec 20, 2024
1 parent cd47c0c commit ccf4260
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
25 changes: 12 additions & 13 deletions lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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() + ":"
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/mayaUsd/render/MaterialXGenOgsXml/LobePruner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/mayaUsd/render/MaterialXGenOgsXml/ShaderGenUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down
7 changes: 4 additions & 3 deletions test/lib/mayaUsd/utils/test_ShaderGenUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit ccf4260

Please sign in to comment.