[Bf-blender-cvs] [aa08545a019] master: Cleanup: Use new node topology cache for sorting multi input sockets

Hans Goudey noreply at git.blender.org
Sat Sep 3 01:01:59 CEST 2022


Commit: aa08545a019606e9155d62144ffc773c787e31b9
Author: Hans Goudey
Date:   Fri Sep 2 17:37:10 2022 -0500
Branches: master
https://developer.blender.org/rBaa08545a019606e9155d62144ffc773c787e31b9

Cleanup: Use new node topology cache for sorting multi input sockets

Assuming the cost of building the cache is ammortized, this
may be be helpful because it removes some quadratic behavior.

===================================================================

M	source/blender/editors/space_node/node_group.cc
M	source/blender/editors/space_node/node_intern.hh
M	source/blender/editors/space_node/node_relationships.cc

===================================================================

diff --git a/source/blender/editors/space_node/node_group.cc b/source/blender/editors/space_node/node_group.cc
index 3a2c2c7a9da..0edeac574df 100644
--- a/source/blender/editors/space_node/node_group.cc
+++ b/source/blender/editors/space_node/node_group.cc
@@ -653,7 +653,7 @@ static bool node_group_make_use_node(bNode &node, bNode *gnode)
 static bool node_group_make_test_selected(bNodeTree &ntree,
                                           bNode *gnode,
                                           const char *ntree_idname,
-                                           ReportList &reports)
+                                          ReportList &reports)
 {
   int ok = true;
 
@@ -1042,8 +1042,10 @@ static int node_group_make_exec(bContext *C, wmOperator *op)
     nodeSetActive(&ntree, gnode);
     if (ngroup) {
       ED_node_tree_push(&snode, ngroup, gnode);
+
+      ngroup->ensure_topology_cache();
       LISTBASE_FOREACH (bNode *, node, &ngroup->nodes) {
-        sort_multi_input_socket_links(snode, *node, nullptr, nullptr);
+        sort_multi_input_socket_links(*node, nullptr, nullptr);
       }
     }
   }
diff --git a/source/blender/editors/space_node/node_intern.hh b/source/blender/editors/space_node/node_intern.hh
index d760a976862..bbfcaed9f93 100644
--- a/source/blender/editors/space_node/node_intern.hh
+++ b/source/blender/editors/space_node/node_intern.hh
@@ -262,10 +262,7 @@ void NODE_OT_group_edit(wmOperatorType *ot);
 
 /* node_relationships.cc */
 
-void sort_multi_input_socket_links(SpaceNode &snode,
-                                   bNode &node,
-                                   bNodeLink *drag_link,
-                                   const float2 *cursor);
+void sort_multi_input_socket_links(bNode &node, bNodeLink *drag_link, const float2 *cursor);
 
 void NODE_OT_link(wmOperatorType *ot);
 void NODE_OT_link_make(wmOperatorType *ot);
diff --git a/source/blender/editors/space_node/node_relationships.cc b/source/blender/editors/space_node/node_relationships.cc
index cf6e83c9450..26e245c3cf5 100644
--- a/source/blender/editors/space_node/node_relationships.cc
+++ b/source/blender/editors/space_node/node_relationships.cc
@@ -105,8 +105,8 @@ static void pick_link(
 
   BLI_assert(nldrag.last_node_hovered_while_dragging_a_link != nullptr);
 
-  sort_multi_input_socket_links(
-      snode, *nldrag.last_node_hovered_while_dragging_a_link, nullptr, nullptr);
+  snode.edittree->ensure_topology_cache();
+  sort_multi_input_socket_links(*nldrag.last_node_hovered_while_dragging_a_link, nullptr, nullptr);
 
   /* Send changed event to original link->tonode. */
   if (node) {
@@ -291,34 +291,24 @@ struct LinkAndPosition {
   float2 multi_socket_position;
 };
 
-void sort_multi_input_socket_links(SpaceNode &snode,
-                                   bNode &node,
-                                   bNodeLink *drag_link,
-                                   const float2 *cursor)
+void sort_multi_input_socket_links(bNode &node, bNodeLink *drag_link, const float2 *cursor)
 {
-  LISTBASE_FOREACH (bNodeSocket *, socket, &node.inputs) {
-    if (!(socket->flag & SOCK_MULTI_INPUT)) {
+  for (bNodeSocket *socket : node.input_sockets()) {
+    if (!socket->is_multi_input()) {
       continue;
     }
-    Vector<LinkAndPosition, 8> links;
+    const float2 &socket_location = {socket->locx, socket->locy};
 
-    LISTBASE_FOREACH (bNodeLink *, link, &snode.edittree->links) {
-      if (link->tosock == socket) {
-        links.append(
-            {link,
-             node_link_calculate_multi_input_position({link->tosock->locx, link->tosock->locy},
-                                                      link->multi_input_socket_index,
-                                                      link->tosock->total_inputs)});
-      }
-    }
+    Vector<LinkAndPosition, 8> links;
+    for (bNodeLink *link : socket->directly_linked_links()) {
+      const float2 location = node_link_calculate_multi_input_position(
+          socket_location, link->multi_input_socket_index, link->tosock->total_inputs);
+      links.append({link, location});
+    };
 
     if (drag_link) {
-      LinkAndPosition link_and_position{};
-      link_and_position.link = drag_link;
-      if (cursor) {
-        link_and_position.multi_socket_position = *cursor;
-      }
-      links.append(link_and_position);
+      BLI_assert(cursor != nullptr);
+      links.append({drag_link, *cursor});
     }
 
     std::sort(links.begin(), links.end(), [](const LinkAndPosition a, const LinkAndPosition b) {
@@ -926,6 +916,7 @@ static void node_link_find_socket(bContext &C, wmOperator &op, const float2 &cur
   if (nldrag->in_out == SOCK_OUT) {
     bNode *tnode;
     bNodeSocket *tsock = nullptr;
+    snode.edittree->ensure_topology_cache();
     if (node_find_indicated_socket(snode, &tnode, &tsock, cursor, SOCK_IN)) {
       for (bNodeLink *link : nldrag->links) {
         /* skip if socket is on the same node as the fromsock */
@@ -952,7 +943,7 @@ static void node_link_find_socket(bContext &C, wmOperator &op, const float2 &cur
           continue;
         }
         if (link->tosock && link->tosock->flag & SOCK_MULTI_INPUT) {
-          sort_multi_input_socket_links(snode, *tnode, link, &cursor);
+          sort_multi_input_socket_links(*tnode, link, &cursor);
         }
       }
     }
@@ -960,7 +951,7 @@ static void node_link_find_socket(bContext &C, wmOperator &op, const float2 &cur
       for (bNodeLink *link : nldrag->links) {
         if (nldrag->last_node_hovered_while_dragging_a_link) {
           sort_multi_input_socket_links(
-              snode, *nldrag->last_node_hovered_while_dragging_a_link, nullptr, &cursor);
+              *nldrag->last_node_hovered_while_dragging_a_link, nullptr, nullptr);
         }
         link->tonode = nullptr;
         link->tosock = nullptr;
@@ -1355,7 +1346,11 @@ static int cut_links_exec(bContext *C, wmOperator *op)
 
   ED_preview_kill_jobs(CTX_wm_manager(C), &bmain);
 
-  LISTBASE_FOREACH_MUTABLE (bNodeLink *, link, &snode.edittree->links) {
+  bNodeTree &node_tree = *snode.edittree;
+
+  Set<bNode *> affected_nodes;
+
+  LISTBASE_FOREACH_MUTABLE (bNodeLink *, link, &node_tree.links) {
     if (node_link_is_hidden_or_dimmed(region.v2d, *link)) {
       continue;
     }
@@ -1370,10 +1365,16 @@ static int cut_links_exec(bContext *C, wmOperator *op)
 
       bNode *to_node = link->tonode;
       nodeRemLink(snode.edittree, link);
-      sort_multi_input_socket_links(snode, *to_node, nullptr, nullptr);
+      affected_nodes.add(to_node);
     }
   }
 
+  node_tree.ensure_topology_cache();
+
+  for (bNode *node : affected_nodes) {
+    sort_multi_input_socket_links(*node, nullptr, nullptr);
+  }
+
   ED_node_tree_propagate_change(C, CTX_data_main(C), snode.edittree);
   if (found) {
     return OPERATOR_FINISHED;



More information about the Bf-blender-cvs mailing list