diff options
author | Jacques Lucke <jacques@blender.org> | 2021-10-18 16:21:51 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-10-18 16:22:34 +0300 |
commit | de6bf5d4d2f1f832f8305c519fc88d8896ea9a0b (patch) | |
tree | f364034ebfb5b98537b0ce75162c2a38aa4bea46 | |
parent | e150f171d5fb2b93277e55329b08e1ebd6dff631 (diff) |
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
-rw-r--r-- | source/blender/blenkernel/BKE_node.h | 4 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/node.cc | 39 |
2 files changed, 33 insertions, 10 deletions
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 *********** */ |