@@ -83,8 +83,7 @@ void ShaderGraph::createConnectedNodes(const ElementPtr& downstreamElement,
8383 throw ExceptionShaderGenError (" Upstream element to connect is not a node '" +
8484 upstreamElement->getName () + " '" );
8585 }
86- const string& newNodeName = upstreamNode->getName ();
87- ShaderNode* newNode = getNode (newNodeName);
86+ ShaderNode* newNode = getNode (upstreamNode->getNamePath ());
8887 if (!newNode)
8988 {
9089 newNode = createNode (upstreamNode, context);
@@ -101,7 +100,7 @@ void ShaderGraph::createConnectedNodes(const ElementPtr& downstreamElement,
101100 InputPtr graphInput = activeInput->getInterfaceInput ();
102101 if (graphInput && graphInput->hasDefaultGeomPropString ())
103102 {
104- ShaderInput* shaderInput = getNode (upstreamNode-> getName ()) ->getInput (activeInput->getName ());
103+ ShaderInput* shaderInput = newNode ->getInput (activeInput->getName ());
105104 addDefaultGeomNode (shaderInput, *graphInput->getDefaultGeomProp (), context);
106105 }
107106 }
@@ -127,12 +126,11 @@ void ShaderGraph::createConnectedNodes(const ElementPtr& downstreamElement,
127126 " ' on upstream node '" + upstreamNode->getName () + " '" );
128127 }
129128
130- // Check if it was a node downstream
129+ // Connect to downstream node or graph output socket.
131130 NodePtr downstreamNode = downstreamElement->asA <Node>();
132131 if (downstreamNode)
133132 {
134- // We have a node downstream
135- ShaderNode* downstream = getNode (downstreamNode->getName ());
133+ ShaderNode* downstream = getNode (downstreamNode->getNamePath ());
136134 if (downstream)
137135 {
138136 if (downstream == newNode)
@@ -152,7 +150,7 @@ void ShaderGraph::createConnectedNodes(const ElementPtr& downstreamElement,
152150 }
153151 else
154152 {
155- throw ExceptionShaderGenError (" Could not find downstream node ' " + downstreamNode->getName () + " '" );
153+ throw ExceptionShaderGenError (" No connecting element for downstream node '" + downstreamNode->getName () + " '" );
156154 }
157155 }
158156 }
@@ -542,8 +540,7 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const string& name
542540 graph->addOutputSockets (*nodeDef, context);
543541
544542 // Create this shader node in the graph.
545- ShaderNodePtr newNode = ShaderNode::create (graph.get (), node->getName (), *nodeDef, context);
546- graph->addNode (newNode);
543+ ShaderNode* newNode = graph->createNode (node->getName (), node->getNamePath (), nodeDef, context);
547544
548545 // Share metadata.
549546 graph->setMetadata (newNode->getMetadata ());
@@ -624,7 +621,7 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const string& name
624621 }
625622
626623 // Apply color and unit transforms to each input.
627- graph->applyInputTransforms (node, newNode. get () , context);
624+ graph->applyInputTransforms (node, newNode, context);
628625
629626 // Set root for upstream dependency traversal
630627 root = node;
@@ -696,7 +693,7 @@ void ShaderGraph::applyInputTransforms(ConstNodePtr node, ShaderNode* shaderNode
696693 }
697694}
698695
699- ShaderNode* ShaderGraph::createNode (const string& name, ConstNodeDefPtr nodeDef, GenContext& context)
696+ ShaderNode* ShaderGraph::createNode (const string& name, const string& uniqueId, ConstNodeDefPtr nodeDef, GenContext& context)
700697{
701698 if (!nodeDef)
702699 {
@@ -705,7 +702,8 @@ ShaderNode* ShaderGraph::createNode(const string& name, ConstNodeDefPtr nodeDef,
705702
706703 // Create this node in the graph.
707704 ShaderNodePtr newNode = ShaderNode::create (this , name, *nodeDef, context);
708- _nodeMap[name] = newNode;
705+ newNode->_uniqueId = uniqueId;
706+ _nodeMap[uniqueId] = newNode;
709707 _nodeOrder.push_back (newNode.get ());
710708
711709 return newNode.get ();
@@ -717,7 +715,7 @@ ShaderNode* ShaderGraph::createNode(ConstNodePtr node, GenContext& context)
717715
718716 // Create this node in the graph.
719717 context.pushParentNode (node);
720- ShaderNode* newNode = createNode (node->getName (), nodeDef, context);
718+ ShaderNode* newNode = createNode (node->getName (), node-> getNamePath (), nodeDef, context);
721719 newNode->initialize (*node, *nodeDef, context);
722720 context.popParentNode ();
723721
@@ -807,7 +805,7 @@ ShaderNode* ShaderGraph::inlineNodeBeforeOutput(ShaderGraphOutputSocket* output,
807805 }
808806
809807 // create the new node, and connect its output to the provided graph output
810- auto newNode = createNode (newNodeName, nodeDef, context);
808+ auto newNode = createNode (newNodeName, newNodeName, nodeDef, context);
811809 if (!newNode)
812810 {
813811 throw ExceptionShaderGenError (" Error while creating node '" +newNodeName+" ' of type '" +nodeDefName+" '" );
@@ -852,19 +850,19 @@ ShaderGraphEdgeIterator ShaderGraph::traverseUpstream(ShaderOutput* output)
852850
853851void ShaderGraph::addNode (ShaderNodePtr node)
854852{
855- _nodeMap[node->getName ()] = node;
853+ _nodeMap[node->getUniqueId ()] = node;
856854 _nodeOrder.push_back (node.get ());
857855}
858856
859- ShaderNode* ShaderGraph::getNode (const string& name )
857+ ShaderNode* ShaderGraph::getNode (const string& uniqueId )
860858{
861- auto it = _nodeMap.find (name );
859+ auto it = _nodeMap.find (uniqueId );
862860 return it != _nodeMap.end () ? it->second .get () : nullptr ;
863861}
864862
865- const ShaderNode* ShaderGraph::getNode (const string& name ) const
863+ const ShaderNode* ShaderGraph::getNode (const string& uniqueId ) const
866864{
867- return const_cast <ShaderGraph*>(this )->getNode (name );
865+ return const_cast <ShaderGraph*>(this )->getNode (uniqueId );
868866}
869867
870868void ShaderGraph::finalize (GenContext& context)
@@ -1045,15 +1043,19 @@ void ShaderGraph::optimize(GenContext& context)
10451043 }
10461044
10471045 // Remove any unused nodes
1048- for (ShaderNode* node : _nodeOrder )
1046+ for (auto it = _nodeMap. begin (); it != _nodeMap. end (); )
10491047 {
1050- if (usedNodesSet.count (node ) == 0 )
1048+ if (usedNodesSet.count (it-> second . get () ) == 0 )
10511049 {
10521050 // Break all connections
1053- disconnect (node );
1051+ disconnect (it-> second . get () );
10541052
10551053 // Erase from storage
1056- _nodeMap.erase (node->getName ());
1054+ it = _nodeMap.erase (it);
1055+ }
1056+ else
1057+ {
1058+ ++it;
10571059 }
10581060 }
10591061
0 commit comments