From 05bf5c4e0ea8bd53d7f121efc683f990afc8a69d Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Sun, 11 Dec 2022 19:38:26 +0100 Subject: [PATCH] Nodes: simplify handling of function nodes in declaration This adds an explicit post processing step to node declarations. The purpose of this is to keep the actual node declaration functions concise by avoiding to specify redundant information. Also it improves the separation of the creation of the declaration from using it. --- .../intern/node_tree_field_inferencing.cc | 12 ---------- source/blender/nodes/NOD_node_declaration.hh | 22 +++++-------------- .../blender/nodes/intern/node_declaration.cc | 15 +++++++++++++ 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/source/blender/blenkernel/intern/node_tree_field_inferencing.cc b/source/blender/blenkernel/intern/node_tree_field_inferencing.cc index 0413e28a297..82e425dfbb9 100644 --- a/source/blender/blenkernel/intern/node_tree_field_inferencing.cc +++ b/source/blender/blenkernel/intern/node_tree_field_inferencing.cc @@ -56,13 +56,6 @@ static InputSocketFieldType get_interface_input_field_type(const bNode &node, /* Get the field type from the declaration. */ const SocketDeclaration &socket_decl = *node_decl->inputs()[socket.index()]; const InputSocketFieldType field_type = socket_decl.input_field_type(); - if (field_type == InputSocketFieldType::Implicit) { - return field_type; - } - if (node_decl->is_function_node()) { - /* In a function node, every socket supports fields. */ - return InputSocketFieldType::IsSupported; - } return field_type; } @@ -93,11 +86,6 @@ static OutputFieldDependency get_interface_output_field_dependency(const bNode & /* Node declarations should be implemented for nodes involved here. */ BLI_assert(node_decl != nullptr); - if (node_decl->is_function_node()) { - /* In a generic function node, all outputs depend on all inputs. */ - return OutputFieldDependency::ForDependentField(); - } - /* Use the socket declaration. */ const SocketDeclaration &socket_decl = *node_decl->outputs()[socket.index()]; return socket_decl.output_field_dependency(); diff --git a/source/blender/nodes/NOD_node_declaration.hh b/source/blender/nodes/NOD_node_declaration.hh index b06f0736917..975cc96131c 100644 --- a/source/blender/nodes/NOD_node_declaration.hh +++ b/source/blender/nodes/NOD_node_declaration.hh @@ -309,7 +309,6 @@ class NodeDeclaration { private: Vector inputs_; Vector outputs_; - bool is_function_node_ = false; friend NodeDeclarationBuilder; @@ -320,11 +319,6 @@ class NodeDeclaration { Span outputs() const; Span sockets(eNodeSocketInOut in_out) const; - bool is_function_node() const - { - return is_function_node_; - } - MEM_CXX_CLASS_ALLOC_FUNCS("NodeDeclaration") }; @@ -332,22 +326,22 @@ class NodeDeclarationBuilder { private: NodeDeclaration &declaration_; Vector> builders_; + bool is_function_node_ = false; public: NodeDeclarationBuilder(NodeDeclaration &declaration); /** * All inputs support fields, and all outputs are fields if any of the inputs is a field. - * Calling field status definitions on each socket is unnecessary. Must be called before adding - * any sockets. + * Calling field status definitions on each socket is unnecessary. */ - void is_function_node(bool value = true) + void is_function_node() { - BLI_assert_msg(declaration_.inputs().is_empty() && declaration_.outputs().is_empty(), - "is_function_node() must be called before any socket is created"); - declaration_.is_function_node_ = value; + is_function_node_ = true; } + void finalize(); + template typename DeclType::Builder &add_input(StringRef name, StringRef identifier = ""); template @@ -553,10 +547,6 @@ inline typename DeclType::Builder &NodeDeclarationBuilder::add_socket(StringRef socket_decl->name_ = name; socket_decl->identifier_ = identifier.is_empty() ? name : identifier; socket_decl->in_out_ = in_out; - if (declaration_.is_function_node()) { - socket_decl->input_field_type_ = InputSocketFieldType::IsSupported; - socket_decl->output_field_dependency_ = OutputFieldDependency::ForDependentField(); - } declarations.append(std::move(socket_decl)); Builder &socket_decl_builder_ref = *socket_decl_builder; builders_.append(std::move(socket_decl_builder)); diff --git a/source/blender/nodes/intern/node_declaration.cc b/source/blender/nodes/intern/node_declaration.cc index 7a6e237a18f..0811bfeb516 100644 --- a/source/blender/nodes/intern/node_declaration.cc +++ b/source/blender/nodes/intern/node_declaration.cc @@ -11,6 +11,21 @@ void build_node_declaration(const bNodeType &typeinfo, NodeDeclaration &r_declar { NodeDeclarationBuilder node_decl_builder{r_declaration}; typeinfo.declare(node_decl_builder); + node_decl_builder.finalize(); +} + +void NodeDeclarationBuilder::finalize() +{ + if (is_function_node_) { + for (SocketDeclarationPtr &socket_decl : declaration_.inputs_) { + if (socket_decl->input_field_type_ != InputSocketFieldType::Implicit) { + socket_decl->input_field_type_ = InputSocketFieldType::IsSupported; + } + } + for (SocketDeclarationPtr &socket_decl : declaration_.outputs_) { + socket_decl->output_field_dependency_ = OutputFieldDependency::ForDependentField(); + } + } } bool NodeDeclaration::matches(const bNode &node) const