[Bf-blender-cvs] [87fd798ae38] master: Nodes: Remove runtime socket location from struct

Hans Goudey noreply at git.blender.org
Fri Jan 6 15:42:15 CET 2023


Commit: 87fd798ae383a344d51dcbd9f66d5834595bdc5a
Author: Hans Goudey
Date:   Fri Jan 6 09:26:48 2023 -0500
Branches: master
https://developer.blender.org/rB87fd798ae383a344d51dcbd9f66d5834595bdc5a

Nodes: Remove runtime socket location from struct

Socket locations are set while drawing the node tree in the editor.
They can always be recalculated this way based on the node position and
other factors. Storing them in the socket is misleading. Plus, ideally
sockets would be quite small to store, this helps us move in that
direction.

Now the socket locations are stored as runtime data of the node editor,
making use of the new node topology cache's `index_in_tree` function
to make a SoA layout possible.

Differential Revision: https://developer.blender.org/D15874

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

M	source/blender/blenkernel/BKE_node_runtime.hh
M	source/blender/editors/space_node/drawnode.cc
M	source/blender/editors/space_node/node_add.cc
M	source/blender/editors/space_node/node_draw.cc
M	source/blender/editors/space_node/node_edit.cc
M	source/blender/editors/space_node/node_intern.hh
M	source/blender/editors/space_node/node_relationships.cc

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

diff --git a/source/blender/blenkernel/BKE_node_runtime.hh b/source/blender/blenkernel/BKE_node_runtime.hh
index ca2fa5d0b01..6146e8ea7ae 100644
--- a/source/blender/blenkernel/BKE_node_runtime.hh
+++ b/source/blender/blenkernel/BKE_node_runtime.hh
@@ -169,13 +169,6 @@ class bNodeSocketRuntime : NonCopyable, NonMovable {
   /** #eNodeTreeChangedFlag. */
   uint32_t changed_flag = 0;
 
-  /**
-   * The location of the sockets, in the view-space of the node editor.
-   * \note Only calculated when drawing.
-   */
-  float locx = 0;
-  float locy = 0;
-
   /**
    * Runtime-only cache of the number of input links, for multi-input sockets,
    * including dragged node links that aren't actually in the tree.
diff --git a/source/blender/editors/space_node/drawnode.cc b/source/blender/editors/space_node/drawnode.cc
index 7c2e7c003a3..34b5ba3ab22 100644
--- a/source/blender/editors/space_node/drawnode.cc
+++ b/source/blender/editors/space_node/drawnode.cc
@@ -1582,11 +1582,12 @@ void draw_nodespace_back_pix(const bContext &C,
   GPU_matrix_pop();
 }
 
-static float2 socket_link_connection_location(const bNode &node,
+static float2 socket_link_connection_location(const Span<float2> socket_locations,
+                                              const bNode &node,
                                               const bNodeSocket &socket,
                                               const bNodeLink &link)
 {
-  const float2 socket_location(socket.runtime->locx, socket.runtime->locy);
+  const float2 socket_location = socket_locations[socket.index_in_tree()];
   if (socket.is_multi_input() && socket.is_input() && !(node.flag & NODE_HIDDEN)) {
     return node_link_calculate_multi_input_position(
         socket_location, link.multi_input_socket_index, socket.runtime->total_inputs);
@@ -1620,11 +1621,13 @@ static void calculate_inner_link_bezier_points(std::array<float2, 4> &points)
   }
 }
 
-static std::array<float2, 4> node_link_bezier_points(const bNodeLink &link)
+static std::array<float2, 4> node_link_bezier_points(const Span<float2> socket_locations,
+                                                     const bNodeLink &link)
 {
   std::array<float2, 4> points;
-  points[0] = socket_link_connection_location(*link.fromnode, *link.fromsock, link);
-  points[3] = socket_link_connection_location(*link.tonode, *link.tosock, link);
+  points[0] = socket_link_connection_location(
+      socket_locations, *link.fromnode, *link.fromsock, link);
+  points[3] = socket_link_connection_location(socket_locations, *link.tonode, *link.tosock, link);
   calculate_inner_link_bezier_points(points);
   return points;
 }
@@ -1640,10 +1643,11 @@ static bool node_link_draw_is_visible(const View2D &v2d, const std::array<float2
   return true;
 }
 
-void node_link_bezier_points_evaluated(const bNodeLink &link,
+void node_link_bezier_points_evaluated(const Span<float2> socket_locations,
+                                       const bNodeLink &link,
                                        std::array<float2, NODE_LINK_RESOL + 1> &coords)
 {
-  const std::array<float2, 4> points = node_link_bezier_points(link);
+  const std::array<float2, 4> points = node_link_bezier_points(socket_locations, link);
 
   /* The extra +1 in size is required by these functions and would be removed ideally. */
   BKE_curve_forward_diff_bezier(points[0].x,
@@ -2028,7 +2032,9 @@ static NodeLinkDrawConfig nodelink_get_draw_config(const bContext &C,
   draw_config.th_col2 = th_col2;
   draw_config.th_col3 = th_col3;
 
-  draw_config.dim_factor = selected ? 1.0f : node_link_dim_factor(v2d, link);
+  draw_config.dim_factor = selected ? 1.0f :
+                                      node_link_dim_factor(
+                                          snode.runtime->all_socket_locations, v2d, link);
 
   bTheme *btheme = UI_GetTheme();
   draw_config.dash_alpha = btheme->space_node.dash_alpha;
@@ -2154,7 +2160,8 @@ void node_draw_link_bezier(const bContext &C,
                            const int th_col3,
                            const bool selected)
 {
-  const std::array<float2, 4> points = node_link_bezier_points(link);
+  const std::array<float2, 4> points = node_link_bezier_points(snode.runtime->all_socket_locations,
+                                                               link);
   if (!node_link_draw_is_visible(v2d, points)) {
     return;
   }
@@ -2216,10 +2223,13 @@ static std::array<float2, 4> node_link_bezier_points_dragged(const SpaceNode &sn
   const float2 cursor = snode.runtime->cursor * UI_DPI_FAC;
   std::array<float2, 4> points;
   points[0] = link.fromsock ?
-                  socket_link_connection_location(*link.fromnode, *link.fromsock, link) :
+                  socket_link_connection_location(
+                      snode.runtime->all_socket_locations, *link.fromnode, *link.fromsock, link) :
+                  cursor;
+  points[3] = link.tosock ?
+                  socket_link_connection_location(
+                      snode.runtime->all_socket_locations, *link.tonode, *link.tosock, link) :
                   cursor;
-  points[3] = link.tosock ? socket_link_connection_location(*link.tonode, *link.tosock, link) :
-                            cursor;
   calculate_inner_link_bezier_points(points);
   return points;
 }
diff --git a/source/blender/editors/space_node/node_add.cc b/source/blender/editors/space_node/node_add.cc
index 534222b58e6..0e3e6f0ece9 100644
--- a/source/blender/editors/space_node/node_add.cc
+++ b/source/blender/editors/space_node/node_add.cc
@@ -107,10 +107,12 @@ bNode *add_static_node(const bContext &C, int type, const float2 &location)
 /** \name Add Reroute Operator
  * \{ */
 
-std::optional<float2> link_path_intersection(const bNodeLink &link, const Span<float2> path)
+std::optional<float2> link_path_intersection(const Span<float2> socket_locations,
+                                             const bNodeLink &link,
+                                             const Span<float2> path)
 {
   std::array<float2, NODE_LINK_RESOL + 1> coords;
-  node_link_bezier_points_evaluated(link, coords);
+  node_link_bezier_points_evaluated(socket_locations, link, coords);
 
   for (const int i : path.index_range().drop_back(1)) {
     for (const int j : IndexRange(NODE_LINK_RESOL)) {
@@ -136,6 +138,7 @@ static int add_reroute_exec(bContext *C, wmOperator *op)
   const ARegion &region = *CTX_wm_region(C);
   SpaceNode &snode = *CTX_wm_space_node(C);
   bNodeTree &ntree = *snode.edittree;
+  const Span<float2> socket_locations = snode.runtime->all_socket_locations;
 
   Vector<float2> path;
   RNA_BEGIN (op->ptr, itemptr, "path") {
@@ -167,16 +170,16 @@ static int add_reroute_exec(bContext *C, wmOperator *op)
   Map<bNodeSocket *, RerouteCutsForSocket> cuts_per_socket;
 
   LISTBASE_FOREACH (bNodeLink *, link, &ntree.links) {
-    if (node_link_is_hidden_or_dimmed(region.v2d, *link)) {
+    if (node_link_is_hidden_or_dimmed(socket_locations, region.v2d, *link)) {
       continue;
     }
-    const std::optional<float2> intersection = link_path_intersection(*link, path);
-    if (!intersection) {
+    const std::optional<float2> cut = link_path_intersection(socket_locations, *link, path);
+    if (!cut) {
       continue;
     }
     RerouteCutsForSocket &from_cuts = cuts_per_socket.lookup_or_add_default(link->fromsock);
     from_cuts.from_node = link->fromnode;
-    from_cuts.links.add(link, *intersection);
+    from_cuts.links.add(link, *cut);
   }
 
   for (const auto item : cuts_per_socket.items()) {
diff --git a/source/blender/editors/space_node/node_draw.cc b/source/blender/editors/space_node/node_draw.cc
index 1660f79fe5d..98f45fc4956 100644
--- a/source/blender/editors/space_node/node_draw.cc
+++ b/source/blender/editors/space_node/node_draw.cc
@@ -317,7 +317,8 @@ static void node_update_basis(const bContext &C,
                               const TreeDrawContext & /*tree_draw_ctx*/,
                               bNodeTree &ntree,
                               bNode &node,
-                              uiBlock &block)
+                              uiBlock &block,
+                              MutableSpan<float2> socket_locations)
 {
   PointerRNA nodeptr;
   RNA_pointer_create(&ntree.id, &RNA_Node, &node, &nodeptr);
@@ -387,8 +388,8 @@ static void node_update_basis(const bContext &C,
     buty = min_ii(buty, dy - NODE_DY);
 
     /* Round the socket location to stop it from jiggling. */
-    socket->runtime->locx = round(loc.x + NODE_WIDTH(node));
-    socket->runtime->locy = round(dy - NODE_DYS);
+    socket_locations[socket->index_in_tree()] = float2(round(loc.x + NODE_WIDTH(node)),
+                                                       round(dy - NODE_DYS));
 
     dy = buty;
     if (socket->next) {
@@ -519,9 +520,8 @@ static void node_update_basis(const bContext &C,
     /* Ensure minimum socket height in case layout is empty. */
     buty = min_ii(buty, dy - NODE_DY);
 
-    socket->runtime->locx = loc.x;
     /* Round the socket vertical position to stop it from jiggling. */
-    socket->runtime->locy = round(dy - NODE_DYS);
+    socket_locations[socket->index_in_tree()] = float2(loc.x, round(dy - NODE_DYS));
 
     dy = buty - multi_input_socket_offset * 0.5;
     if (socket->next) {
@@ -551,7 +551,7 @@ static void node_update_basis(const bContext &C,
 /**
  * Based on settings in node, sets drawing rect info.
  */
-static void node_update_hidden(bNode &node, uiBlock &block)
+static void node_update_hidden(bNode &node, uiBlock &block, MutableSpan<float2> socket_locations)
 {
   int totin = 0, totout = 0;
 
@@ -591,8 +591,9 @@ static void node_update_hidden(bNode &node, uiBlock &block)
   for (bNodeSocket *socket : node.output_sockets()) {
     if (socket->is_visible()) {
       /* Round the socket location to stop it from jiggling. */
-      socket->runtime->locx = round(node.runtime->totr.xmax - hiddenrad + sinf(rad) * hiddenrad);
-      socket->runtime->locy = round(node.runtime->totr.ymin + hiddenrad + cosf(rad) * hiddenrad);
+      socket_locations[socket->index_in_tree()] = {
+          round(node.runtime->totr.xmax - hiddenrad + sinf(rad) * hiddenrad),
+          round(node.runtime->totr.ymin + hiddenrad + cosf(rad) * hiddenrad)};
       

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list