From ad489b71646a2e03e2ce9b0d193d541e3987b4cd Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Mon, 3 Feb 2020 09:02:00 +0100 Subject: Cleanup: add type inheritance for Cycles nodes --- intern/cycles/graph/node.cpp | 10 ++++++++++ intern/cycles/graph/node.h | 3 +++ intern/cycles/graph/node_type.cpp | 11 ++++++++--- intern/cycles/graph/node_type.h | 16 ++++++++++++++-- intern/cycles/graph/node_xml.cpp | 4 ++-- 5 files changed, 37 insertions(+), 7 deletions(-) (limited to 'intern/cycles/graph') diff --git a/intern/cycles/graph/node.cpp b/intern/cycles/graph/node.cpp index 4f79a7518dc..1439fb5a407 100644 --- a/intern/cycles/graph/node.cpp +++ b/intern/cycles/graph/node.cpp @@ -669,4 +669,14 @@ size_t Node::get_total_size_in_bytes() const return total_size; } +bool Node::is_a(const NodeType *type_) +{ + for (const NodeType *base = type; base; base = base->base) { + if (base == type_) { + return true; + } + } + return false; +} + CCL_NAMESPACE_END diff --git a/intern/cycles/graph/node.h b/intern/cycles/graph/node.h index d35a1bb489c..4473b8aca28 100644 --- a/intern/cycles/graph/node.h +++ b/intern/cycles/graph/node.h @@ -94,6 +94,9 @@ struct Node { /* Get total size of this node. */ size_t get_total_size_in_bytes() const; + /* Type testing, taking into account base classes. */ + bool is_a(const NodeType *type); + ustring name; const NodeType *type; }; diff --git a/intern/cycles/graph/node_type.cpp b/intern/cycles/graph/node_type.cpp index f46d4e48026..0283ed7c817 100644 --- a/intern/cycles/graph/node_type.cpp +++ b/intern/cycles/graph/node_type.cpp @@ -135,8 +135,13 @@ bool SocketType::is_float3(Type type) /* Node Type */ -NodeType::NodeType(Type type_) : type(type_) +NodeType::NodeType(Type type, const NodeType *base) : type(type), base(base) { + if (base) { + /* Inherit sockets. */ + inputs = base->inputs; + outputs = base->outputs; + } } NodeType::~NodeType() @@ -209,7 +214,7 @@ unordered_map &NodeType::types() return _types; } -NodeType *NodeType::add(const char *name_, CreateFunc create_, Type type_) +NodeType *NodeType::add(const char *name_, CreateFunc create_, Type type_, const NodeType *base_) { ustring name(name_); @@ -219,7 +224,7 @@ NodeType *NodeType::add(const char *name_, CreateFunc create_, Type type_) return NULL; } - types()[name] = NodeType(type_); + types()[name] = NodeType(type_, base_); NodeType *type = &types()[name]; type->name = name; diff --git a/intern/cycles/graph/node_type.h b/intern/cycles/graph/node_type.h index e9496a42658..a4461ec7abb 100644 --- a/intern/cycles/graph/node_type.h +++ b/intern/cycles/graph/node_type.h @@ -103,7 +103,7 @@ struct SocketType { struct NodeType { enum Type { NONE, SHADER }; - explicit NodeType(Type type = NONE); + explicit NodeType(Type type = NONE, const NodeType *base = NULL); ~NodeType(); void register_input(ustring name, @@ -124,11 +124,15 @@ struct NodeType { ustring name; Type type; + const NodeType *base; vector> inputs; vector> outputs; CreateFunc create; - static NodeType *add(const char *name, CreateFunc create, Type type = NONE); + static NodeType *add(const char *name, + CreateFunc create, + Type type = NONE, + const NodeType *base = NULL); static const NodeType *find(ustring name); static unordered_map &types(); }; @@ -148,6 +152,14 @@ struct NodeType { } \ template const NodeType *structname::register_type() +#define NODE_ABSTRACT_DECLARE \ + template static const NodeType *register_type(); \ + static const NodeType *node_type; + +#define NODE_ABSTRACT_DEFINE(structname) \ + const NodeType *structname::node_type = structname::register_type(); \ + template const NodeType *structname::register_type() + /* Sock Definition Macros */ #define SOCKET_OFFSETOF(T, name) (((char *)&(((T *)1)->name)) - (char *)1) diff --git a/intern/cycles/graph/node_xml.cpp b/intern/cycles/graph/node_xml.cpp index a96970cc904..d333400cc4a 100644 --- a/intern/cycles/graph/node_xml.cpp +++ b/intern/cycles/graph/node_xml.cpp @@ -200,7 +200,7 @@ void xml_read_node(XMLReader &reader, Node *node, xml_node xml_node) map::iterator it = reader.node_map.find(value); if (it != reader.node_map.end()) { Node *value_node = it->second; - if (value_node->type == *(socket.node_type)) + if (value_node->is_a(*(socket.node_type))) node->set(socket, it->second); } break; @@ -215,7 +215,7 @@ void xml_read_node(XMLReader &reader, Node *node, xml_node xml_node) map::iterator it = reader.node_map.find(ustring(tokens[i])); if (it != reader.node_map.end()) { Node *value_node = it->second; - value[i] = (value_node->type == *(socket.node_type)) ? value_node : NULL; + value[i] = (value_node->is_a(*(socket.node_type))) ? value_node : NULL; } else { value[i] = NULL; -- cgit v1.2.3