From eac7ed8d047457564033f1547fea9a912ff0d9b7 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Sun, 29 May 2016 11:20:10 +0200 Subject: Code refactor: minor node and node type utility functions and changes. --- intern/cycles/graph/node.cpp | 122 ++++++++++++++++++++++++-------------- intern/cycles/graph/node.h | 9 ++- intern/cycles/graph/node_type.cpp | 38 ++++++++++-- intern/cycles/graph/node_type.h | 20 +++++-- intern/cycles/graph/node_xml.cpp | 8 +-- 5 files changed, 134 insertions(+), 63 deletions(-) (limited to 'intern/cycles/graph') diff --git a/intern/cycles/graph/node.cpp b/intern/cycles/graph/node.cpp index 98b66fb9a7a..941a66741c5 100644 --- a/intern/cycles/graph/node.cpp +++ b/intern/cycles/graph/node.cpp @@ -36,12 +36,8 @@ Node::Node(const NodeType *type_, ustring name_) } /* initialize default values */ - typedef unordered_map map_type; - foreach(const map_type::value_type& it, type->inputs) { - const SocketType& socket = it.second; - const void *src = socket.default_value; - void *dst = ((char*)this) + socket.struct_offset; - memcpy(dst, src, socket.size()); + foreach(const SocketType& socket, type->inputs) { + set_default_value(socket); } } @@ -297,7 +293,8 @@ const array& Node::get_node_array(const SocketType& input) const return get_socket_value >(this, input); } -/* default values */ +/* generic value operations */ + bool Node::has_default_value(const SocketType& input) const { const void *src = input.default_value; @@ -305,6 +302,48 @@ bool Node::has_default_value(const SocketType& input) const return memcmp(dst, src, input.size()) == 0; } +void Node::set_default_value(const SocketType& socket) +{ + const void *src = socket.default_value; + void *dst = ((char*)this) + socket.struct_offset; + memcpy(dst, src, socket.size()); +} + +template +static void copy_array(const Node *node, const SocketType& socket, const Node *other, const SocketType& other_socket) +{ + const array* src = (const array*)(((char*)other) + other_socket.struct_offset); + array* dst = (array*)(((char*)node) + socket.struct_offset); + *dst = *src; +} + +void Node::copy_value(const SocketType& socket, const Node& other, const SocketType& other_socket) +{ + assert(socket.type == other_socket.type); + + if(socket.is_array()) { + switch(socket.type) { + case SocketType::BOOLEAN_ARRAY: copy_array(this, socket, &other, other_socket); break; + case SocketType::FLOAT_ARRAY: copy_array(this, socket, &other, other_socket); break; + case SocketType::INT_ARRAY: copy_array(this, socket, &other, other_socket); break; + case SocketType::COLOR_ARRAY: copy_array(this, socket, &other, other_socket); break; + case SocketType::VECTOR_ARRAY: copy_array(this, socket, &other, other_socket); break; + case SocketType::POINT_ARRAY: copy_array(this, socket, &other, other_socket); break; + case SocketType::NORMAL_ARRAY: copy_array(this, socket, &other, other_socket); break; + case SocketType::POINT2_ARRAY: copy_array(this, socket, &other, other_socket); break; + case SocketType::STRING_ARRAY: copy_array(this, socket, &other, other_socket); break; + case SocketType::TRANSFORM_ARRAY: copy_array(this, socket, &other, other_socket); break; + case SocketType::NODE_ARRAY: copy_array(this, socket, &other, other_socket); break; + default: assert(0); break; + } + } + else { + const void *src = ((char*)&other) + other_socket.struct_offset; + void *dst = ((char*)this) + socket.struct_offset; + memcpy(dst, src, socket.size()); + } +} + template static bool is_array_equal(const Node *node, const Node *other, const SocketType& socket) { @@ -313,48 +352,43 @@ static bool is_array_equal(const Node *node, const Node *other, const SocketType return *a == *b; } -/* modified */ -bool Node::modified(const Node& other) +bool Node::equals_value(const Node& other, const SocketType& socket) const +{ + if(socket.is_array()) { + switch(socket.type) { + case SocketType::BOOLEAN_ARRAY: return is_array_equal(this, &other, socket); + case SocketType::FLOAT_ARRAY: return is_array_equal(this, &other, socket); + case SocketType::INT_ARRAY: return is_array_equal(this, &other, socket); + case SocketType::COLOR_ARRAY: return is_array_equal(this, &other, socket); + case SocketType::VECTOR_ARRAY: return is_array_equal(this, &other, socket); + case SocketType::POINT_ARRAY: return is_array_equal(this, &other, socket); + case SocketType::NORMAL_ARRAY: return is_array_equal(this, &other, socket); + case SocketType::POINT2_ARRAY: return is_array_equal(this, &other, socket); + case SocketType::STRING_ARRAY: return is_array_equal(this, &other, socket); + case SocketType::TRANSFORM_ARRAY: return is_array_equal(this, &other, socket); + case SocketType::NODE_ARRAY: return is_array_equal(this, &other, socket); + default: assert(0); return true; + } + } + else { + const void *a = ((char*)this) + socket.struct_offset; + const void *b = ((char*)&other) + socket.struct_offset; + return (memcmp(a, b, socket.size()) == 0); + } +} + +/* equals */ + +bool Node::equals(const Node& other) const { assert(type == other.type); - typedef unordered_map map_type; - foreach(const map_type::value_type& it, type->inputs) { - const SocketType& socket = it.second; - - if(socket.is_array()) { - bool equal = true; - - switch(socket.type) - { - case SocketType::BOOLEAN_ARRAY: equal = is_array_equal(this, &other, socket); break; - case SocketType::FLOAT_ARRAY: equal = is_array_equal(this, &other, socket); break; - case SocketType::INT_ARRAY: equal = is_array_equal(this, &other, socket); break; - case SocketType::COLOR_ARRAY: equal = is_array_equal(this, &other, socket); break; - case SocketType::VECTOR_ARRAY: equal = is_array_equal(this, &other, socket); break; - case SocketType::POINT_ARRAY: equal = is_array_equal(this, &other, socket); break; - case SocketType::NORMAL_ARRAY: equal = is_array_equal(this, &other, socket); break; - case SocketType::POINT2_ARRAY: equal = is_array_equal(this, &other, socket); break; - case SocketType::STRING_ARRAY: equal = is_array_equal(this, &other, socket); break; - case SocketType::TRANSFORM_ARRAY: equal = is_array_equal(this, &other, socket); break; - case SocketType::NODE_ARRAY: equal = is_array_equal(this, &other, socket); break; - default: assert(0); break; - } - - if(!equal) { - return true; - } - } - else { - const void *a = ((char*)this) + socket.struct_offset; - const void *b = ((char*)&other) + socket.struct_offset; - if(memcmp(a, b, socket.size()) != 0) { - return true; - } - } + foreach(const SocketType& socket, type->inputs) { + if(!equals_value(other, socket)) + return false; } - return false; + return true; } CCL_NAMESPACE_END diff --git a/intern/cycles/graph/node.h b/intern/cycles/graph/node.h index de06df10265..bb84f982fb3 100644 --- a/intern/cycles/graph/node.h +++ b/intern/cycles/graph/node.h @@ -77,11 +77,14 @@ struct Node const array& get_transform_array(const SocketType& input) const; const array& get_node_array(const SocketType& input) const; - /* default values */ + /* generic values operations */ bool has_default_value(const SocketType& input) const; + void set_default_value(const SocketType& input); + bool equals_value(const Node& other, const SocketType& input) const; + void copy_value(const SocketType& input, const Node& other, const SocketType& other_input); - /* modified */ - bool modified(const Node& other); + /* equals */ + bool equals(const Node& other) const; ustring name; const NodeType *type; diff --git a/intern/cycles/graph/node_type.cpp b/intern/cycles/graph/node_type.cpp index dc879655d7f..7f68ae9c7c7 100644 --- a/intern/cycles/graph/node_type.cpp +++ b/intern/cycles/graph/node_type.cpp @@ -114,9 +114,15 @@ ustring SocketType::type_name(Type type) return names[(int)type]; } +bool SocketType::is_float3(Type type) +{ + return (type == COLOR || type == VECTOR || type == POINT || type == NORMAL); +} + /* Node Type */ -NodeType::NodeType() +NodeType::NodeType(Type type_) +: type(type_) { } @@ -137,7 +143,7 @@ void NodeType::register_input(ustring name, ustring ui_name, SocketType::Type ty socket.enum_values = enum_values; socket.node_type = node_type; socket.flags = flags | extra_flags; - inputs[name] = socket; + inputs.push_back(socket); } void NodeType::register_output(ustring name, ustring ui_name, SocketType::Type type) @@ -151,7 +157,29 @@ void NodeType::register_output(ustring name, ustring ui_name, SocketType::Type t socket.enum_values = NULL; socket.node_type = NULL; socket.flags = SocketType::LINKABLE; - outputs[name] = socket; + outputs.push_back(socket); +} + +const SocketType *NodeType::find_input(ustring name) const +{ + foreach(const SocketType& socket, inputs) { + if(socket.name == name) { + return &socket; + } + } + + return NULL; +} + +const SocketType *NodeType::find_output(ustring name) const +{ + foreach(const SocketType& socket, outputs) { + if(socket.name == name) { + return &socket; + } + } + + return NULL; } /* Node Type Registry */ @@ -162,7 +190,7 @@ unordered_map& NodeType::types() return _types; } -NodeType *NodeType::add(const char *name_, CreateFunc create_) +NodeType *NodeType::add(const char *name_, CreateFunc create_, Type type_) { ustring name(name_); @@ -172,7 +200,7 @@ NodeType *NodeType::add(const char *name_, CreateFunc create_) return NULL; } - types()[name] = NodeType(); + types()[name] = NodeType(type_); NodeType *type = &types()[name]; type->name = name; diff --git a/intern/cycles/graph/node_type.h b/intern/cycles/graph/node_type.h index 82ddd29da33..20816f634cd 100644 --- a/intern/cycles/graph/node_type.h +++ b/intern/cycles/graph/node_type.h @@ -21,6 +21,7 @@ #include "util_map.h" #include "util_param.h" #include "util_string.h" +#include "util_vector.h" CCL_NAMESPACE_BEGIN @@ -94,13 +95,19 @@ struct SocketType static size_t max_size(); static ustring type_name(Type type); static void *zero_default_value(); + static bool is_float3(Type type); }; /* Node Type */ struct NodeType { - explicit NodeType(); + enum Type { + NONE, + SHADER + }; + + explicit NodeType(Type type = NONE); ~NodeType(); void register_input(ustring name, ustring ui_name, SocketType::Type type, @@ -110,15 +117,18 @@ struct NodeType int flags = 0, int extra_flags = 0); void register_output(ustring name, ustring ui_name, SocketType::Type type); + const SocketType *find_input(ustring name) const; + const SocketType *find_output(ustring name) const; + typedef Node *(*CreateFunc)(const NodeType *type); - typedef unordered_map SocketMap; ustring name; - SocketMap inputs; - SocketMap outputs; + Type type; + std::vector inputs; + std::vector outputs; CreateFunc create; - static NodeType *add(const char *name, CreateFunc create); + static NodeType *add(const char *name, CreateFunc create, Type type = NONE); static const NodeType *find(ustring name); static unordered_map& types(); }; diff --git a/intern/cycles/graph/node_xml.cpp b/intern/cycles/graph/node_xml.cpp index a6040405c30..022de7cf32a 100644 --- a/intern/cycles/graph/node_xml.cpp +++ b/intern/cycles/graph/node_xml.cpp @@ -58,9 +58,7 @@ void xml_read_node(XMLReader& reader, Node *node, pugi::xml_node xml_node) node->name = ustring(name_attr.value()); } - foreach(const NodeType::SocketMap::value_type& it, node->type->inputs) { - const SocketType& socket = it.second; - + foreach(const SocketType& socket, node->type->inputs) { if(socket.type == SocketType::CLOSURE || socket.type == SocketType::UNDEFINED) { continue; } @@ -257,9 +255,7 @@ pugi::xml_node xml_write_node(Node *node, pugi::xml_node xml_root) xml_node.append_attribute("name") = node->name.c_str(); - foreach(const NodeType::SocketMap::value_type& it, node->type->inputs) { - const SocketType& socket = it.second; - + foreach(const SocketType& socket, node->type->inputs) { if(socket.type == SocketType::CLOSURE || socket.type == SocketType::UNDEFINED) { continue; } -- cgit v1.2.3