From 25a10c211fe85e8ed1b5bcd89ccb62de6f73ff9a Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Wed, 20 Mar 2024 12:37:37 +0100 Subject: [PATCH] Nodes: support accessing socket type directly from declaration Previously, we haven't added this because there were plans to use these declarations at a higher abstraction level where one declaration potentially contains more than one socket. This hasn't happened yet, and we are also using other ways to achieve dynamic socket amounts (using dynamic declarations). Therefore, it is reasonable to simplify the code by storing the integer socket type in the declaration directly. Pull Request: https://projects.blender.org/blender/blender/pulls/119691 --- .../editors/space_node/node_templates.cc | 49 +--------------- source/blender/nodes/NOD_node_declaration.hh | 4 ++ .../blender/nodes/NOD_socket_declarations.hh | 34 +++++++++++ .../nodes/NOD_socket_declarations_geometry.hh | 2 + source/blender/nodes/intern/node_common.cc | 4 +- source/blender/nodes/intern/node_socket.cc | 56 +------------------ 6 files changed, 44 insertions(+), 105 deletions(-) diff --git a/source/blender/editors/space_node/node_templates.cc b/source/blender/editors/space_node/node_templates.cc index 3a0474ce8ab..31d47ce5b66 100644 --- a/source/blender/editors/space_node/node_templates.cc +++ b/source/blender/editors/space_node/node_templates.cc @@ -373,54 +373,7 @@ static Vector ui_node_link_items(NodeLinkArg *arg, const SocketDeclaration &socket_decl = *socket_decl_ptr; NodeLinkItem item; item.socket_index = index++; - if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_FLOAT; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_INT; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_BOOLEAN; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_VECTOR; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_RGBA; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_ROTATION; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_MATRIX; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_STRING; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_MENU; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_IMAGE; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_TEXTURE; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_MATERIAL; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_SHADER; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_COLLECTION; - } - else if (dynamic_cast(&socket_decl)) { - item.socket_type = SOCK_OBJECT; - } - else { - item.socket_type = SOCK_CUSTOM; - } + item.socket_type = socket_decl.socket_type; item.socket_name = socket_decl.name.c_str(); item.node_name = arg->node_type->ui_name; items.append(item); diff --git a/source/blender/nodes/NOD_node_declaration.hh b/source/blender/nodes/NOD_node_declaration.hh index be9212b01de..3e79fd5c54e 100644 --- a/source/blender/nodes/NOD_node_declaration.hh +++ b/source/blender/nodes/NOD_node_declaration.hh @@ -171,6 +171,8 @@ class SocketDeclaration : public ItemDeclaration { /** Defined by whether the socket is part of the node's input or * output socket declaration list. Included here for convenience. */ eNodeSocketInOut in_out; + /** Socket type that corresponds to this socket declaration. */ + eNodeSocketDatatype socket_type; bool hide_label = false; bool hide_value = false; bool compact = false; @@ -682,6 +684,7 @@ inline typename DeclType::Builder &NodeDeclarationBuilder::add_socket(StringRef socket_decl->name = name; socket_decl->identifier = identifier_in.is_empty() ? name : identifier_in; socket_decl->in_out = SOCK_IN; + socket_decl->socket_type = DeclType::static_socket_type; socket_decl_builder->index_ = declaration_.inputs.append_and_get_index(socket_decl.get()); declaration_.items.append(std::move(socket_decl)); input_socket_builders_.append(&*socket_decl_builder); @@ -693,6 +696,7 @@ inline typename DeclType::Builder &NodeDeclarationBuilder::add_socket(StringRef socket_decl->name = name; socket_decl->identifier = identifier_out.is_empty() ? name : identifier_out; socket_decl->in_out = SOCK_OUT; + socket_decl->socket_type = DeclType::static_socket_type; socket_decl_builder->index_ = declaration_.outputs.append_and_get_index(socket_decl.get()); declaration_.items.append(std::move(socket_decl)); output_socket_builders_.append(&*socket_decl_builder); diff --git a/source/blender/nodes/NOD_socket_declarations.hh b/source/blender/nodes/NOD_socket_declarations.hh index 562ab62f09a..c52a50b2edc 100644 --- a/source/blender/nodes/NOD_socket_declarations.hh +++ b/source/blender/nodes/NOD_socket_declarations.hh @@ -18,6 +18,8 @@ class FloatBuilder; class Float : public SocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_FLOAT; + float default_value = 0.0f; float soft_min_value = -FLT_MAX; float soft_max_value = FLT_MAX; @@ -45,6 +47,8 @@ class IntBuilder; class Int : public SocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_INT; + int default_value = 0; int soft_min_value = INT32_MIN; int soft_max_value = INT32_MAX; @@ -72,6 +76,8 @@ class VectorBuilder; class Vector : public SocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_VECTOR; + float3 default_value = {0, 0, 0}; float soft_min_value = -FLT_MAX; float soft_max_value = FLT_MAX; @@ -100,6 +106,8 @@ class BoolBuilder; class Bool : public SocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_BOOLEAN; + bool default_value = false; friend BoolBuilder; @@ -120,6 +128,8 @@ class ColorBuilder; class Color : public SocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_RGBA; + ColorGeometry4f default_value{0.8f, 0.8f, 0.8f, 1.0f}; friend ColorBuilder; @@ -141,6 +151,8 @@ class RotationBuilder; class Rotation : public SocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_ROTATION; + math::EulerXYZ default_value; friend RotationBuilder; @@ -162,6 +174,8 @@ class MatrixBuilder; class Matrix : public SocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_MATRIX; + friend MatrixBuilder; using Builder = MatrixBuilder; @@ -178,6 +192,8 @@ class StringBuilder; class String : public SocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_STRING; + std::string default_value; friend StringBuilder; @@ -199,6 +215,8 @@ class MenuBuilder; class Menu : public SocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_MENU; + int32_t default_value; friend MenuBuilder; @@ -237,6 +255,8 @@ class IDSocketDeclaration : public SocketDeclaration { class Object : public IDSocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_OBJECT; + using Builder = SocketDeclarationBuilder; Object(); @@ -244,6 +264,8 @@ class Object : public IDSocketDeclaration { class Material : public IDSocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_MATERIAL; + using Builder = SocketDeclarationBuilder; Material(); @@ -251,6 +273,8 @@ class Material : public IDSocketDeclaration { class Collection : public IDSocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_COLLECTION; + using Builder = SocketDeclarationBuilder; Collection(); @@ -258,6 +282,8 @@ class Collection : public IDSocketDeclaration { class Texture : public IDSocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_TEXTURE; + using Builder = SocketDeclarationBuilder; Texture(); @@ -265,6 +291,8 @@ class Texture : public IDSocketDeclaration { class Image : public IDSocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_IMAGE; + using Builder = SocketDeclarationBuilder; Image(); @@ -274,6 +302,8 @@ class ShaderBuilder; class Shader : public SocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_SHADER; + friend ShaderBuilder; using Builder = ShaderBuilder; @@ -292,6 +322,8 @@ class Extend : public SocketDeclaration { friend ExtendBuilder; public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_CUSTOM; + using Builder = ExtendBuilder; bNodeSocket &build(bNodeTree &ntree, bNode &node) const override; @@ -304,6 +336,8 @@ class ExtendBuilder : public SocketDeclarationBuilder {}; class Custom : public SocketDeclaration { public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_CUSTOM; + const char *idname_; std::function init_socket_fn; diff --git a/source/blender/nodes/NOD_socket_declarations_geometry.hh b/source/blender/nodes/NOD_socket_declarations_geometry.hh index 7448b39f0f0..d48eff99ff5 100644 --- a/source/blender/nodes/NOD_socket_declarations_geometry.hh +++ b/source/blender/nodes/NOD_socket_declarations_geometry.hh @@ -21,6 +21,8 @@ class Geometry : public SocketDeclaration { friend GeometryBuilder; public: + static constexpr eNodeSocketDatatype static_socket_type = SOCK_GEOMETRY; + using Builder = GeometryBuilder; bNodeSocket &build(bNodeTree &ntree, bNode &node) const override; diff --git a/source/blender/nodes/intern/node_common.cc b/source/blender/nodes/intern/node_common.cc index 4938695271f..55c62964043 100644 --- a/source/blender/nodes/intern/node_common.cc +++ b/source/blender/nodes/intern/node_common.cc @@ -368,7 +368,7 @@ static PanelDeclarationPtr declaration_for_interface_panel(const bNodeTree & /*n static void set_default_input_field(const bNodeTreeInterfaceSocket &input, SocketDeclaration &decl) { - if (dynamic_cast(&decl)) { + if (decl.socket_type == SOCK_VECTOR) { if (input.default_input == GEO_NODE_DEFAULT_FIELD_INPUT_NORMAL_FIELD) { decl.implicit_input_fn = std::make_unique( implicit_field_inputs::normal); @@ -380,7 +380,7 @@ static void set_default_input_field(const bNodeTreeInterfaceSocket &input, Socke decl.hide_value = true; } } - else if (dynamic_cast(&decl)) { + else if (decl.socket_type == SOCK_INT) { if (input.default_input == GEO_NODE_DEFAULT_FIELD_INPUT_INDEX_FIELD) { decl.implicit_input_fn = std::make_unique( implicit_field_inputs::index); diff --git a/source/blender/nodes/intern/node_socket.cc b/source/blender/nodes/intern/node_socket.cc index bb54702400a..4b27f2000e2 100644 --- a/source/blender/nodes/intern/node_socket.cc +++ b/source/blender/nodes/intern/node_socket.cc @@ -277,60 +277,6 @@ static void refresh_node_panel(const PanelDeclaration &panel_decl, } } -/** - * Not great to have this here, but this is only for forward compatibility, so this code shouldn't - * in the `main` branch. - */ -static std::optional decl_to_data_type(const SocketDeclaration &socket_decl) -{ - if (dynamic_cast(&socket_decl)) { - return SOCK_FLOAT; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_INT; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_BOOLEAN; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_VECTOR; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_RGBA; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_ROTATION; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_MATRIX; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_STRING; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_IMAGE; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_TEXTURE; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_MATERIAL; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_SHADER; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_COLLECTION; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_OBJECT; - } - else if (dynamic_cast(&socket_decl)) { - return SOCK_GEOMETRY; - } - return std::nullopt; -} - static const char *get_identifier_from_decl(const char *identifier_prefix, const bNodeSocket &socket, const Span socket_decls) @@ -340,7 +286,7 @@ static const char *get_identifier_from_decl(const char *identifier_prefix, } for (const SocketDeclaration *socket_decl : socket_decls) { if (BLI_str_startswith(socket_decl->identifier.c_str(), identifier_prefix)) { - if (socket.type == decl_to_data_type(*socket_decl)) { + if (socket.type == socket_decl->socket_type) { return socket_decl->identifier.c_str(); } }