From 7c9a7090e68ef8f055b49735be4e7e6188984300 Mon Sep 17 00:00:00 2001 From: SShikhali <99796443+SShikhali@users.noreply.github.com> Date: Wed, 14 Jun 2023 21:06:38 +0200 Subject: [PATCH] Deterministic code generation (#1376) These changes make code generation deterministic by changing std::set to std::vector when removing unused nodes --- source/MaterialXGenShader/ShaderGraph.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/source/MaterialXGenShader/ShaderGraph.cpp b/source/MaterialXGenShader/ShaderGraph.cpp index 4fd92c11aa..e6ad6eca29 100644 --- a/source/MaterialXGenShader/ShaderGraph.cpp +++ b/source/MaterialXGenShader/ShaderGraph.cpp @@ -1073,7 +1073,8 @@ void ShaderGraph::optimize(GenContext& context) if (numEdits > 0) { - std::set usedNodes; + std::set usedNodesSet; + std::vector usedNodesVec; // Traverse the graph to find nodes still in use for (ShaderGraphOutputSocket* outputSocket : getOutputSockets()) @@ -1084,7 +1085,12 @@ 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); + } } } } @@ -1092,7 +1098,7 @@ void ShaderGraph::optimize(GenContext& context) // Remove any unused nodes for (ShaderNode* node : _nodeOrder) { - if (usedNodes.count(node) == 0) + if (usedNodesSet.count(node) == 0) { // Break all connections disconnect(node); @@ -1102,8 +1108,7 @@ void ShaderGraph::optimize(GenContext& context) } } - _nodeOrder.resize(usedNodes.size()); - _nodeOrder.assign(usedNodes.begin(), usedNodes.end()); + _nodeOrder = usedNodesVec; } }