Skip to content

Commit

Permalink
Deterministic code generation (#1376)
Browse files Browse the repository at this point in the history
These changes make code generation deterministic by changing std::set to std::vector when removing unused nodes
  • Loading branch information
SShikhali authored Jun 14, 2023
1 parent d7b6129 commit 7c9a709
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions source/MaterialXGenShader/ShaderGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,8 @@ void ShaderGraph::optimize(GenContext& context)

if (numEdits > 0)
{
std::set<ShaderNode*> usedNodes;
std::set<ShaderNode*> usedNodesSet;
std::vector<ShaderNode*> usedNodesVec;

// Traverse the graph to find nodes still in use
for (ShaderGraphOutputSocket* outputSocket : getOutputSockets())
Expand All @@ -1084,15 +1085,20 @@ void ShaderGraph::optimize(GenContext& context)
{
for (ShaderGraphEdge edge : ShaderGraph::traverseUpstream(upstreamPort))
{
usedNodes.insert(edge.upstream->getNode());
ShaderNode* node = edge.upstream->getNode();
if (usedNodesSet.count(node) == 0)
{
usedNodesSet.insert(node);
usedNodesVec.push_back(node);
}
}
}
}

// Remove any unused nodes
for (ShaderNode* node : _nodeOrder)
{
if (usedNodes.count(node) == 0)
if (usedNodesSet.count(node) == 0)
{
// Break all connections
disconnect(node);
Expand All @@ -1102,8 +1108,7 @@ void ShaderGraph::optimize(GenContext& context)
}
}

_nodeOrder.resize(usedNodes.size());
_nodeOrder.assign(usedNodes.begin(), usedNodes.end());
_nodeOrder = usedNodesVec;
}
}

Expand Down

0 comments on commit 7c9a709

Please sign in to comment.