[Bf-blender-cvs] [de6bf5d4d2f] master: Nodes: support sharing node declarations between nodes

Jacques Lucke noreply at git.blender.org
Mon Oct 18 15:22:52 CEST 2021


Commit: de6bf5d4d2f1f832f8305c519fc88d8896ea9a0b
Author: Jacques Lucke
Date:   Mon Oct 18 15:21:51 2021 +0200
Branches: master
https://developer.blender.org/rBde6bf5d4d2f1f832f8305c519fc88d8896ea9a0b

Nodes: support sharing node declarations between nodes

Previously, every node had its own declaration. This isn't ideal, because
it's often the case that all nodes of the same type have the same declaration.
That's the case for all nodes using declarations currently. It will not be true
for e.g. group nodes in the future.

Sharing node declarations between nodes makes it a bit more efficient.

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

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

M	source/blender/blenkernel/BKE_node.h
M	source/blender/blenkernel/intern/node.cc

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

diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index d33c5e9940c..65e54be7db4 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -339,6 +339,10 @@ typedef struct bNodeType {
 
   /* Declares which sockets the node has. */
   NodeDeclareFunction declare;
+  /* Different nodes of this type can have different declarations. */
+  bool declaration_is_dynamic;
+  /* Declaration to be used when it is not dynamic. */
+  NodeDeclarationHandle *fixed_declaration;
 
   /* RNA integration */
   ExtensionRNA rna_ext;
diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc
index 145a40d30cc..5a4849f1d05 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -1379,6 +1379,8 @@ static void node_free_type(void *nodetype_v)
     free_dynamic_typeinfo(nodetype);
   }
 
+  delete nodetype->fixed_declaration;
+
   /* Can be null when the type is not dynamically allocated. */
   if (nodetype->free_self) {
     nodetype->free_self(nodetype);
@@ -1391,6 +1393,14 @@ void nodeRegisterType(bNodeType *nt)
   BLI_assert(nt->idname[0] != '\0');
   BLI_assert(nt->poll != nullptr);
 
+  if (nt->declare && !nt->declaration_is_dynamic) {
+    if (nt->fixed_declaration == nullptr) {
+      nt->fixed_declaration = new blender::nodes::NodeDeclaration();
+      blender::nodes::NodeDeclarationBuilder builder{*nt->fixed_declaration};
+      nt->declare(builder);
+    }
+  }
+
   BLI_ghash_insert(nodetypes_hash, nt->idname, nt);
   /* XXX pass Main to register function? */
   /* Probably not. It is pretty much expected we want to update G_MAIN here I think -
@@ -2254,9 +2264,6 @@ bNode *BKE_node_copy_ex(bNodeTree *ntree,
 
   *node_dst = *node_src;
 
-  /* Reset the declaration of the new node. */
-  node_dst->declaration = nullptr;
-
   /* can be called for nodes outside a node tree (e.g. clipboard) */
   if (ntree) {
     if (unique_name) {
@@ -2327,6 +2334,10 @@ bNode *BKE_node_copy_ex(bNodeTree *ntree,
     ntree->update |= NTREE_UPDATE_NODES;
   }
 
+  /* Reset the declaration of the new node. */
+  node_dst->declaration = nullptr;
+  nodeDeclarationEnsure(ntree, node_dst);
+
   return node_dst;
 }
 
@@ -3144,7 +3155,9 @@ static void node_free_node(bNodeTree *ntree, bNode *node)
     MEM_freeN(node->prop);
   }
 
-  delete node->declaration;
+  if (node->typeinfo->declaration_is_dynamic) {
+    delete node->declaration;
+  }
 
   MEM_freeN(node);
 
@@ -3982,16 +3995,22 @@ int nodeSocketLinkLimit(const bNodeSocket *sock)
  */
 void nodeDeclarationEnsure(bNodeTree *UNUSED(ntree), bNode *node)
 {
-  if (node->typeinfo->declare == nullptr) {
+  if (node->declaration != nullptr) {
     return;
   }
-  if (node->declaration != nullptr) {
+  if (node->typeinfo->declare == nullptr) {
     return;
   }
-
-  node->declaration = new blender::nodes::NodeDeclaration();
-  blender::nodes::NodeDeclarationBuilder builder{*node->declaration};
-  node->typeinfo->declare(builder);
+  if (node->typeinfo->declaration_is_dynamic) {
+    node->declaration = new blender::nodes::NodeDeclaration();
+    blender::nodes::NodeDeclarationBuilder builder{*node->declaration};
+    node->typeinfo->declare(builder);
+  }
+  else {
+    /* Declaration should have been created in #nodeRegisterType. */
+    BLI_assert(node->typeinfo->fixed_declaration != nullptr);
+    node->declaration = node->typeinfo->fixed_declaration;
+  }
 }
 
 /* ************** Node Clipboard *********** */



More information about the Bf-blender-cvs mailing list