[Bf-blender-cvs] [a05012d5006] geometry-nodes: Geometry Nodes: simplify and deduplicate callbacks on sockets

Jacques Lucke noreply at git.blender.org
Thu Oct 22 18:08:36 CEST 2020


Commit: a05012d50066c397e83ebe67631875df69860d6b
Author: Jacques Lucke
Date:   Thu Oct 22 18:08:27 2020 +0200
Branches: geometry-nodes
https://developer.blender.org/rBa05012d50066c397e83ebe67631875df69860d6b

Geometry Nodes: simplify and deduplicate callbacks on sockets

This adds a layer of abstraction between the code calling callbacks
on sockets and the implementation of those callbacks.
This abstraction layer allows some sockets to not implement all
callbacks when those can be derived from some other callback.

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

M	source/blender/blenkernel/BKE_node.h
M	source/blender/modifiers/intern/MOD_nodes.cc
M	source/blender/nodes/CMakeLists.txt
M	source/blender/nodes/NOD_node_tree_multi_function.hh
A	source/blender/nodes/NOD_type_callbacks.hh
M	source/blender/nodes/intern/node_socket.cc
M	source/blender/nodes/intern/node_tree_multi_function.cc
A	source/blender/nodes/intern/type_callbacks.cc

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

diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index b03e45749a8..da4bcb8a00e 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -115,22 +115,25 @@ class NodeMFNetworkBuilder;
 class GValueByName;
 }  // namespace nodes
 namespace fn {
+class CPPType;
 class MFDataType;
-}
+}  // namespace fn
 }  // namespace blender
 
 using NodeExpandInMFNetworkFunction = void (*)(blender::nodes::NodeMFNetworkBuilder &builder);
-using SocketGetMFDataTypeFunction = blender::fn::MFDataType (*)();
-using SocketExpandInMFNetworkFunction = void (*)(blender::nodes::SocketMFNetworkBuilder &builder);
 using NodeGeometryExecFunction = void (*)(struct bNode *node,
                                           blender::nodes::GValueByName &inputs,
                                           blender::nodes::GValueByName &outputs);
+using SocketGetCPPTypeFunction = const blender::fn::CPPType *(*)();
+using SocketGetCPPValueFunction = void (*)(const struct bNodeSocket &socket, void *r_value);
+using SocketExpandInMFNetworkFunction = void (*)(blender::nodes::SocketMFNetworkBuilder &builder);
 
 #else
 typedef void *NodeExpandInMFNetworkFunction;
-typedef void *SocketGetMFDataTypeFunction;
 typedef void *SocketExpandInMFNetworkFunction;
 typedef void *NodeGeometryExecFunction;
+typedef void *SocketGetCPPTypeFunction;
+typedef void *SocketGetCPPValueFunction;
 #endif
 
 /**
@@ -186,10 +189,12 @@ typedef struct bNodeSocketType {
   /* Callback to free the socket type. */
   void (*free_self)(struct bNodeSocketType *stype);
 
-  /* Returns the multi-function data type of this socket type. */
-  SocketGetMFDataTypeFunction get_mf_data_type;
   /* Expands the socket into a multi-function node that outputs the socket value. */
   SocketExpandInMFNetworkFunction expand_in_mf_network;
+  /* Return the CPPType of this socket. */
+  SocketGetCPPTypeFunction get_cpp_type;
+  /* Get the value of this socket in a generic way. */
+  SocketGetCPPValueFunction get_cpp_value;
 } bNodeSocketType;
 
 typedef void *(*NodeInitExecFunction)(struct bNodeExecContext *context,
diff --git a/source/blender/modifiers/intern/MOD_nodes.cc b/source/blender/modifiers/intern/MOD_nodes.cc
index ddf89074da0..11254896461 100644
--- a/source/blender/modifiers/intern/MOD_nodes.cc
+++ b/source/blender/modifiers/intern/MOD_nodes.cc
@@ -65,6 +65,7 @@
 
 #include "NOD_derived_node_tree.hh"
 #include "NOD_geometry_exec.hh"
+#include "NOD_type_callbacks.hh"
 
 using blender::float3;
 
@@ -116,40 +117,6 @@ using namespace blender::nodes;
 using namespace blender::fn;
 using namespace blender::bke;
 
-static void get_socket_value(const bNodeSocket *bsocket, void *r_value)
-{
-  /* TODO: Move this into socket callbacks. */
-  switch (bsocket->type) {
-    case SOCK_GEOMETRY:
-      new (r_value) GeometryPtr();
-      break;
-    case SOCK_FLOAT:
-      *(float *)r_value = ((bNodeSocketValueFloat *)bsocket->default_value)->value;
-      break;
-    case SOCK_INT:
-      *(int *)r_value = ((bNodeSocketValueInt *)bsocket->default_value)->value;
-      break;
-    default:
-      BLI_assert(false);
-  }
-}
-
-static const CPPType &get_socket_type(const bNodeSocket *bsocket)
-{
-  /* TODO: Move this into socket callbacks. */
-  switch (bsocket->type) {
-    case SOCK_GEOMETRY:
-      return CPPType::get<GeometryPtr>();
-    case SOCK_FLOAT:
-      return CPPType::get<float>();
-    case SOCK_INT:
-      return CPPType::get<int>();
-    default:
-      BLI_assert(false);
-      return CPPType::get<float>();
-  }
-}
-
 static bool compute_input_socket(const DOutputSocket *group_input,
                                  GeometryPtr &group_input_geometry,
                                  const DInputSocket &socket_to_compute,
@@ -173,7 +140,7 @@ static bool compute_output_socket(const DOutputSocket *group_input,
 
   /* Compute all inputs for the node. */
   for (const DInputSocket *input_socket : node.inputs()) {
-    const CPPType &type = get_socket_type(input_socket->bsocket());
+    const CPPType &type = *socket_cpp_type_get(*input_socket->bsocket()->typeinfo);
     void *buffer = allocator.allocate(type.size(), type.alignment());
     compute_input_socket(group_input, group_input_geometry, *input_socket, buffer);
     node_inputs.move_in(input_socket->bsocket()->identifier, GMutablePointer{type, buffer});
@@ -186,7 +153,7 @@ static bool compute_output_socket(const DOutputSocket *group_input,
 
   /* Pass relevant value to the caller. */
   bNodeSocket *bsocket_to_compute = socket_to_compute.bsocket();
-  const CPPType &type_to_compute = get_socket_type(bsocket_to_compute);
+  const CPPType &type_to_compute = *socket_cpp_type_get(*bsocket_to_compute->typeinfo);
   GMutablePointer computed_value = node_outputs.extract(bsocket_to_compute->identifier);
   type_to_compute.relocate_to_uninitialized(computed_value.get(), r_value);
   return true;
@@ -206,13 +173,13 @@ static bool compute_input_socket(const DOutputSocket *group_input,
   }
   if (total_inputs == 0) {
     bNodeSocket *bsocket = socket_to_compute.bsocket();
-    get_socket_value(bsocket, r_value);
+    socket_cpp_value_get(*bsocket, r_value);
     return true;
   }
   if (from_group_inputs.size() == 1) {
     bNodeSocket *bsocket = from_group_inputs[0]->bsocket();
     BLI_assert(bsocket->type == socket_to_compute.bsocket()->type);
-    get_socket_value(bsocket, r_value);
+    socket_cpp_value_get(*bsocket, r_value);
     return true;
   }
 
diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt
index b3621c44613..9058ee965a5 100644
--- a/source/blender/nodes/CMakeLists.txt
+++ b/source/blender/nodes/CMakeLists.txt
@@ -271,6 +271,7 @@ set(SRC
   intern/node_tree_multi_function.cc
   intern/node_tree_ref.cc
   intern/node_util.c
+  intern/type_callbacks.cc
 
   composite/node_composite_util.h
   function/node_function_util.hh
@@ -290,6 +291,7 @@ set(SRC
   NOD_socket.h
   NOD_static_types.h
   NOD_texture.h
+  NOD_type_callbacks.hh
   intern/node_common.h
   intern/node_exec.h
   intern/node_util.h
diff --git a/source/blender/nodes/NOD_node_tree_multi_function.hh b/source/blender/nodes/NOD_node_tree_multi_function.hh
index 25787231afa..a8e1d7417ef 100644
--- a/source/blender/nodes/NOD_node_tree_multi_function.hh
+++ b/source/blender/nodes/NOD_node_tree_multi_function.hh
@@ -26,21 +26,12 @@
 #include "FN_multi_function_network.hh"
 
 #include "NOD_derived_node_tree.hh"
+#include "NOD_type_callbacks.hh"
 
 #include "BLI_resource_collector.hh"
 
 namespace blender::nodes {
 
-/* Maybe this should be moved to BKE_node.h. */
-inline bool is_multi_function_data_socket(const bNodeSocket *bsocket)
-{
-  if (bsocket->typeinfo->get_mf_data_type != nullptr) {
-    BLI_assert(bsocket->typeinfo->expand_in_mf_network != nullptr);
-    return true;
-  }
-  return false;
-}
-
 /**
  * A MFNetworkTreeMap maps various components of a DerivedNodeTree to components of a
  * fn::MFNetwork. This is necessary for further processing of a multi-function network that has
@@ -149,7 +140,7 @@ class MFNetworkTreeMap {
       if (!dsocket->is_available()) {
         continue;
       }
-      if (!is_multi_function_data_socket(dsocket->bsocket())) {
+      if (!socket_is_mf_data_socket(*dsocket->bsocket()->typeinfo)) {
         continue;
       }
       fn::MFSocket *socket = sockets[used_sockets];
@@ -299,6 +290,11 @@ class SocketMFNetworkBuilder : public MFNetworkBuilderBase {
   {
     this->construct_generator_fn<fn::CustomMF_Constant<T>>(std::move(value));
   }
+  void set_constant_value(const CPPType &type, const void *value)
+  {
+    /* The value has live as long as the generated mf network. */
+    this->construct_generator_fn<fn::CustomMF_GenericConstant>(type, value);
+  }
 
   template<typename T, typename... Args> void construct_generator_fn(Args &&... args)
   {
diff --git a/source/blender/nodes/NOD_type_callbacks.hh b/source/blender/nodes/NOD_type_callbacks.hh
new file mode 100644
index 00000000000..d1a4bd3ad7a
--- /dev/null
+++ b/source/blender/nodes/NOD_type_callbacks.hh
@@ -0,0 +1,36 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#pragma once
+
+#include <optional>
+
+#include "BKE_node.h"
+
+#include "FN_multi_function_data_type.hh"
+
+namespace blender::nodes {
+
+using fn::CPPType;
+using fn::MFDataType;
+
+const CPPType *socket_cpp_type_get(const bNodeSocketType &stype);
+std::optional<MFDataType> socket_mf_type_get(const bNodeSocketType &stype);
+bool socket_is_mf_data_socket(const bNodeSocketType &stype);
+bool socket_cpp_value_get(const bNodeSocket &socket, void *r_value);
+void socket_expand_in_mf_network(SocketMFNetworkBuilder &builder);
+
+}  // namespace blender::nodes
diff --git a/source/blender/nodes/intern/node_socket.cc b/source/blender/nodes/intern/node_socket.cc
index 84a51afb922..7dee0f40538 100644
--- a/source/blender/nodes/intern/node_socket.cc
+++ b/source/blender/nodes/intern/node_socket.cc
@@ -32,6 +32,7 @@
 #include "BLI_string.h"
 #include "BLI_utildefines.h"
 
+#include "BKE_geometry.hh"
 #include "BKE_lib_id.h"
 #include "BKE_node.h"
 #include "BKE_persistent_data_handle.hh"
@@ -552,10 +553,9 @@ static bNodeSocketType *make_socket_type_virtual(void)
 static bNodeSocketType *make_socket_type_bool()
 {
   bNodeSocketType *socktype = make_standard_socket_type(SOCK_BOOLEAN, PROP_NONE);
-  socktype->get_mf_data_type = []() { return blender::fn::MFDataType::ForSingle<bool>(); };
-  socktype->expand_in_mf_network = [](blender::nodes::SocketMFNetworkBuilder &build

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list