[Bf-blender-cvs] [1e69a250431] master: Nodes: add more flexible method to declare sockets of a node

Jacques Lucke noreply at git.blender.org
Mon Aug 30 17:24:01 CEST 2021


Commit: 1e69a25043120cc8dddc3f58622eb50e1443def1
Author: Jacques Lucke
Date:   Mon Aug 30 17:13:46 2021 +0200
Branches: master
https://developer.blender.org/rB1e69a25043120cc8dddc3f58622eb50e1443def1

Nodes: add more flexible method to declare sockets of a node

Previously, built-in nodes had to implement "socket templates"
(`bNodeSocketTemplate`) to tell Blender which sockets they have.
It was nice that this was declarative, but this approach was way
too rigid and was cumbersome to use in many cases.

This commit starts to move us away from this rigid structure
by letting nodes implement a function that declares the sockets
the node has. Right now this is used as a direct replacement
of the "socket template" approach to keep the refactor smaller.
It's just a bit easier to read and write.

In the future we want to support more complex features like
dynamic numbers of sockets and type inferencing. Those features
will be easier to build on this new approach.

This new approach can live side by side with `bNodeSocketTemplate`
for a while. That makes it easier to update nodes one by one.

Note: In `bNodeSocketTemplate` socket identifiers were made
unique automatically. In this new approach, one has to specify
unique identifiers manually (unless the name is unique already).

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

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

M	source/blender/blenkernel/BKE_node.h
M	source/blender/blenkernel/intern/node.cc
M	source/blender/blenloader/intern/versioning_270.c
M	source/blender/nodes/CMakeLists.txt
A	source/blender/nodes/NOD_node_declaration.hh
M	source/blender/nodes/NOD_socket.h
A	source/blender/nodes/NOD_socket_declarations.hh
M	source/blender/nodes/geometry/node_geometry_util.hh
M	source/blender/nodes/geometry/nodes/node_geo_attribute_fill.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_trim.cc
M	source/blender/nodes/geometry/nodes/node_geo_edge_split.cc
M	source/blender/nodes/geometry/nodes/node_geo_input_material.cc
M	source/blender/nodes/geometry/nodes/node_geo_is_viewport.cc
M	source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_circle.cc
M	source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cone.cc
M	source/blender/nodes/geometry/nodes/node_geo_object_info.cc
M	source/blender/nodes/geometry/nodes/node_geo_transform.cc
A	source/blender/nodes/intern/node_declaration.cc
M	source/blender/nodes/intern/node_socket.cc
A	source/blender/nodes/intern/node_socket_declarations.cc

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

diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index 379d4332473..5e0526ab262 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -113,6 +113,7 @@ namespace blender {
 namespace nodes {
 class NodeMultiFunctionBuilder;
 class GeoNodeExecParams;
+class NodeDeclarationBuilder;
 }  // namespace nodes
 namespace fn {
 class CPPType;
@@ -122,6 +123,7 @@ class MFDataType;
 
 using NodeMultiFunctionBuildFunction = void (*)(blender::nodes::NodeMultiFunctionBuilder &builder);
 using NodeGeometryExecFunction = void (*)(blender::nodes::GeoNodeExecParams params);
+using NodeDeclareFunction = void (*)(blender::nodes::NodeDeclarationBuilder &builder);
 using SocketGetCPPTypeFunction = const blender::fn::CPPType *(*)();
 using SocketGetCPPValueFunction = void (*)(const struct bNodeSocket &socket, void *r_value);
 using SocketGetGeometryNodesCPPTypeFunction = const blender::fn::CPPType *(*)();
@@ -131,6 +133,7 @@ using SocketGetGeometryNodesCPPValueFunction = void (*)(const struct bNodeSocket
 #else
 typedef void *NodeMultiFunctionBuildFunction;
 typedef void *NodeGeometryExecFunction;
+typedef void *NodeDeclareFunction;
 typedef void *SocketGetCPPTypeFunction;
 typedef void *SocketGetGeometryNodesCPPTypeFunction;
 typedef void *SocketGetGeometryNodesCPPValueFunction;
@@ -334,6 +337,9 @@ typedef struct bNodeType {
   NodeGeometryExecFunction geometry_node_execute;
   bool geometry_node_execute_supports_laziness;
 
+  /* Declares which sockets the node has. */
+  NodeDeclareFunction declare;
+
   /* RNA integration */
   ExtensionRNA rna_ext;
 } bNodeType;
@@ -615,6 +621,10 @@ struct bNodeSocket *nodeInsertStaticSocket(struct bNodeTree *ntree,
                                            const char *identifier,
                                            const char *name);
 void nodeRemoveSocket(struct bNodeTree *ntree, struct bNode *node, struct bNodeSocket *sock);
+void nodeRemoveSocketEx(struct bNodeTree *ntree,
+                        struct bNode *node,
+                        struct bNodeSocket *sock,
+                        bool do_id_user);
 void nodeRemoveAllSockets(struct bNodeTree *ntree, struct bNode *node);
 void nodeModifySocketType(struct bNodeTree *ntree,
                           struct bNode *node,
diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc
index fb43d1fc87f..48d747e2bf3 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -79,6 +79,7 @@
 #include "NOD_composite.h"
 #include "NOD_function.h"
 #include "NOD_geometry.h"
+#include "NOD_node_declaration.hh"
 #include "NOD_shader.h"
 #include "NOD_socket.h"
 #include "NOD_texture.h"
@@ -887,7 +888,7 @@ void ntreeBlendReadLib(struct BlendLibReader *reader, struct bNodeTree *ntree)
    * to match the static layout. */
   if (!BLO_read_lib_is_undo(reader)) {
     LISTBASE_FOREACH (bNode *, node, &ntree->nodes) {
-      node_verify_socket_templates(ntree, node);
+      node_verify_sockets(ntree, node, false);
     }
   }
 }
@@ -1011,6 +1012,13 @@ IDTypeInfo IDType_ID_NT = {
 
 static void node_add_sockets_from_type(bNodeTree *ntree, bNode *node, bNodeType *ntype)
 {
+  if (ntype->declare != nullptr) {
+    blender::nodes::NodeDeclaration node_decl;
+    blender::nodes::NodeDeclarationBuilder builder{node_decl};
+    ntype->declare(builder);
+    node_decl.build(*ntree, *node);
+    return;
+  }
   bNodeSocketTemplate *sockdef;
   /* bNodeSocket *sock; */ /* UNUSED */
 
@@ -1915,6 +1923,14 @@ static void node_socket_free(bNodeTree *UNUSED(ntree),
 }
 
 void nodeRemoveSocket(bNodeTree *ntree, bNode *node, bNodeSocket *sock)
+{
+  nodeRemoveSocketEx(ntree, node, sock, true);
+}
+
+void nodeRemoveSocketEx(struct bNodeTree *ntree,
+                        struct bNode *node,
+                        struct bNodeSocket *sock,
+                        bool do_id_user)
 {
   LISTBASE_FOREACH_MUTABLE (bNodeLink *, link, &ntree->links) {
     if (link->fromsock == sock || link->tosock == sock) {
@@ -1926,7 +1942,7 @@ void nodeRemoveSocket(bNodeTree *ntree, bNode *node, bNodeSocket *sock)
   BLI_remlink(&node->inputs, sock);
   BLI_remlink(&node->outputs, sock);
 
-  node_socket_free(ntree, sock, node, true);
+  node_socket_free(ntree, sock, node, do_id_user);
   MEM_freeN(sock);
 
   node->update |= NODE_UPDATE;
diff --git a/source/blender/blenloader/intern/versioning_270.c b/source/blender/blenloader/intern/versioning_270.c
index 6492f0d1f69..fa15e541e43 100644
--- a/source/blender/blenloader/intern/versioning_270.c
+++ b/source/blender/blenloader/intern/versioning_270.c
@@ -291,7 +291,7 @@ static void do_version_hue_sat_node(bNodeTree *ntree, bNode *node)
   }
 
   /* Make sure new sockets are properly created. */
-  node_verify_socket_templates(ntree, node);
+  node_verify_sockets(ntree, node, false);
   /* Convert value from old storage to new sockets. */
   NodeHueSat *nhs = node->storage;
   bNodeSocket *hue = nodeFindSocket(node, SOCK_IN, "Hue"),
@@ -357,7 +357,7 @@ static void do_versions_compositor_render_passes(bNodeTree *ntree)
        */
       do_versions_compositor_render_passes_storage(node);
       /* Make sure new sockets are properly created. */
-      node_verify_socket_templates(ntree, node);
+      node_verify_sockets(ntree, node, false);
       /* Make sure all possibly created sockets have proper storage. */
       do_versions_compositor_render_passes_storage(node);
     }
diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt
index 8dfab671565..29a1de381b5 100644
--- a/source/blender/nodes/CMakeLists.txt
+++ b/source/blender/nodes/CMakeLists.txt
@@ -347,6 +347,8 @@ set(SRC
   intern/node_exec.cc
   intern/node_geometry_exec.cc
   intern/node_multi_function.cc
+  intern/node_declaration.cc
+  intern/node_socket_declarations.cc
   intern/node_socket.cc
   intern/node_tree_ref.cc
   intern/node_util.c
@@ -367,6 +369,8 @@ set(SRC
   NOD_geometry_nodes_eval_log.hh
   NOD_math_functions.hh
   NOD_multi_function.hh
+  NOD_node_declaration.hh
+  NOD_socket_declarations.hh
   NOD_node_tree_ref.hh
   NOD_shader.h
   NOD_socket.h
diff --git a/source/blender/nodes/NOD_node_declaration.hh b/source/blender/nodes/NOD_node_declaration.hh
new file mode 100644
index 00000000000..9c461885859
--- /dev/null
+++ b/source/blender/nodes/NOD_node_declaration.hh
@@ -0,0 +1,135 @@
+/*
+ * 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 <type_traits>
+
+#include "BLI_string_ref.hh"
+#include "BLI_vector.hh"
+
+#include "DNA_node_types.h"
+
+namespace blender::nodes {
+
+class NodeDeclarationBuilder;
+
+class SocketDeclaration {
+ protected:
+  std::string name_;
+  std::string identifier_;
+
+  friend NodeDeclarationBuilder;
+
+ public:
+  virtual bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const = 0;
+  virtual bool matches(const bNodeSocket &socket) const = 0;
+  virtual bNodeSocket &update_or_build(bNodeTree &ntree, bNode &node, bNodeSocket &socket) const;
+
+  StringRefNull name() const;
+  StringRefNull identifier() const;
+};
+
+using SocketDeclarationPtr = std::unique_ptr<SocketDeclaration>;
+
+class NodeDeclaration {
+ private:
+  Vector<SocketDeclarationPtr> inputs_;
+  Vector<SocketDeclarationPtr> outputs_;
+
+  friend NodeDeclarationBuilder;
+
+ public:
+  void build(bNodeTree &ntree, bNode &node) const;
+  bool matches(const bNode &node) const;
+
+  Span<SocketDeclarationPtr> inputs() const;
+  Span<SocketDeclarationPtr> outputs() const;
+};
+
+class NodeDeclarationBuilder {
+ private:
+  NodeDeclaration &declaration_;
+
+ public:
+  NodeDeclarationBuilder(NodeDeclaration &declaration);
+
+  template<typename DeclType> DeclType &add_input(StringRef name, StringRef identifier = "");
+  template<typename DeclType> DeclType &add_output(StringRef name, StringRef identifier = "");
+};
+
+/* --------------------------------------------------------------------
+ * SocketDeclaration inline methods.
+ */
+
+inline StringRefNull SocketDeclaration::name() const
+{
+  return name_;
+}
+
+inline StringRefNull SocketDeclaration::identifier() const
+{
+  return identifier_;
+}
+
+/* --------------------------------------------------------------------
+ * NodeDeclarationBuilder inline methods.
+ */
+
+inline NodeDeclarationBuilder::NodeDeclarationBuilder(NodeDeclaration &declaration)
+    : declaration_(declaration)
+{
+}
+
+template<typename DeclType>
+inline DeclType &NodeDeclarationBuilder::add_input(StringRef name, StringRef identifier)
+{
+  static_assert(std::is_base_of_v<SocketDeclaration, DeclType>);
+  std::unique_ptr<DeclType> socket_decl = std::make_unique<DeclType>();
+  DeclType &ref = *socket_decl;
+  ref.name_ = name;
+  ref.identifier_ = identifier.is_empty() ? name : identifier;
+  declaration_.inputs_.append(std::move(socket_decl));
+  return ref;
+}
+
+template<typename DeclType>
+inline DeclType &NodeDeclarationBuilder::add_output(StringRef name, StringRef identifier)
+{
+  static_assert(std::is_base_of_v<SocketDeclaration, DeclType>);
+  std::unique_ptr<DeclType> socket_decl = std::make_unique<DeclType>();
+  DeclType &ref = *socket_decl;
+  ref.name_ = name;
+  ref.identifier_ = identifier.is_empty() ? name : identifier;
+  declaration_.outputs_.append(std::move(socket_decl));
+  return ref;
+}
+
+/* --------------------------------------------------------------------
+ * NodeDeclaration inline methods.
+ */
+
+inline Span<SocketDeclarationPtr> Nod

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list