Blender V5.0
node_type.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "graph/node_type.h"
6
7#include "util/log.h"
8#include "util/transform.h"
9#include "util/types_float3.h"
10
12
13/* Node Socket Type */
14
16{
17 return size(type, false);
18}
19
21{
22 return size(type, true);
23}
24
26{
27 return (type >= BOOLEAN_ARRAY);
28}
29
31{
32 switch (type) {
33 case UNDEFINED:
34 case NUM_TYPES:
35 return 0;
36
37 case BOOLEAN:
38 return sizeof(bool);
39 case FLOAT:
40 return sizeof(float);
41 case INT:
42 return sizeof(int);
43 case UINT:
44 return sizeof(uint);
45 case UINT64:
46 return sizeof(uint64_t);
47 case COLOR:
48 case VECTOR:
49 case POINT:
50 case NORMAL:
51 return (packed) ? sizeof(packed_float3) : sizeof(float3);
52 case POINT2:
53 return sizeof(float2);
54 case CLOSURE:
55 return 0;
56 case STRING:
57 return sizeof(ustring);
58 case ENUM:
59 return sizeof(int);
60 case TRANSFORM:
61 return sizeof(Transform);
62 case NODE:
63 return sizeof(void *);
64
65 case BOOLEAN_ARRAY:
66 return sizeof(array<bool>);
67 case FLOAT_ARRAY:
68 return sizeof(array<float>);
69 case INT_ARRAY:
70 return sizeof(array<int>);
71 case COLOR_ARRAY:
72 return sizeof(array<float3>);
73 case VECTOR_ARRAY:
74 return sizeof(array<float3>);
75 case POINT_ARRAY:
76 return sizeof(array<float3>);
77 case NORMAL_ARRAY:
78 return sizeof(array<float3>);
79 case POINT2_ARRAY:
80 return sizeof(array<float2>);
81 case STRING_ARRAY:
82 return sizeof(array<ustring>);
83 case TRANSFORM_ARRAY:
84 return sizeof(array<Transform>);
85 case NODE_ARRAY:
86 return sizeof(array<void *>);
87 }
88
89 assert(0);
90 return 0;
91}
92
94{
95 return sizeof(Transform);
96}
97
99{
100 static Transform zero_transform = transform_zero();
101 return &zero_transform;
102}
103
105{
106 static const ustring names[] = {ustring("undefined"),
107
108 ustring("boolean"), ustring("float"),
109 ustring("int"), ustring("uint"),
110 ustring("uint64"), ustring("color"),
111 ustring("vector"), ustring("point"),
112 ustring("normal"), ustring("point2"),
113 ustring("closure"), ustring("string"),
114 ustring("enum"), ustring("transform"),
115 ustring("node"),
116
117 ustring("array_boolean"), ustring("array_float"),
118 ustring("array_int"), ustring("array_color"),
119 ustring("array_vector"), ustring("array_point"),
120 ustring("array_normal"), ustring("array_point2"),
121 ustring("array_string"), ustring("array_transform"),
122 ustring("array_node")};
123
124 constexpr size_t num_names = sizeof(names) / sizeof(*names);
125 static_assert(num_names == NUM_TYPES);
126
127 return names[(int)type];
128}
129
131{
132 return (type == COLOR || type == VECTOR || type == POINT || type == NORMAL);
133}
134
135/* Node Type */
136
138{
139 if (base) {
140 /* Inherit sockets. */
141 inputs = base->inputs;
142 outputs = base->outputs;
143 }
144}
145
146NodeType::~NodeType() = default;
147
149 ustring ui_name,
151 const int struct_offset,
152 const void *default_value,
153 const NodeEnum *enum_values,
154 const NodeType *node_type,
155 const int flags,
156 const int extra_flags)
157{
158 SocketType socket;
159 socket.name = name;
160 socket.ui_name = ui_name;
161 socket.type = type;
162 socket.struct_offset = struct_offset;
163 socket.default_value = default_value;
164 socket.enum_values = enum_values;
165 socket.node_type = node_type;
166 socket.flags = flags | extra_flags;
167 assert(inputs.size() < std::numeric_limits<SocketModifiedFlags>::digits);
168 socket.modified_flag_bit = (1ull << inputs.size());
169 inputs.push_back(socket);
170}
171
173{
174 SocketType socket;
175 socket.name = name;
176 socket.ui_name = ui_name;
177 socket.type = type;
178 socket.struct_offset = 0;
179 socket.default_value = nullptr;
180 socket.enum_values = nullptr;
181 socket.node_type = nullptr;
183 outputs.push_back(socket);
184}
185
187{
188 for (const SocketType &socket : inputs) {
189 if (socket.name == name) {
190 return &socket;
191 }
192 }
193
194 return nullptr;
195}
196
198{
199 for (const SocketType &socket : outputs) {
200 if (socket.name == name) {
201 return &socket;
202 }
203 }
204
205 return nullptr;
206}
207
208/* Node Type Registry */
209
210unordered_map<ustring, NodeType> &NodeType::types()
211{
212 static unordered_map<ustring, NodeType> _types;
213 return _types;
214}
215
216NodeType *NodeType::add(const char *name_, CreateFunc create_, Type type_, const NodeType *base_)
217{
218 const ustring name(name_);
219
220 if (types().find(name) != types().end()) {
221 LOG_ERROR << "Node type " << name_ << " registered twice";
222 assert(0);
223 return nullptr;
224 }
225
226 types()[name] = NodeType(type_, base_);
227
228 NodeType *type = &types()[name];
229 type->name = name;
230 type->create = create_;
231 return type;
232}
233
235{
236 const unordered_map<ustring, NodeType>::iterator it = types().find(name);
237 return (it == types().end()) ? nullptr : &it->second;
238}
239
unsigned int uint
unsigned long long int uint64_t
nullptr float
#define CCL_NAMESPACE_END
#define packed
#define assert(assertion)
#define LOG_ERROR
Definition log.h:101
Type type
Definition node_type.h:126
vector< SocketType, std::allocator< SocketType > > inputs
Definition node_type.h:128
NodeType(Type type=NONE, const NodeType *base=nullptr)
static NodeType * add(const char *name, CreateFunc create, Type type=NONE, const NodeType *base=nullptr)
unique_ptr< Node >(*)(const NodeType *) CreateFunc
Definition node_type.h:123
const SocketType * find_output(ustring name) const
static const NodeType * find(ustring name)
void register_input(ustring name, ustring ui_name, SocketType::Type type, const int struct_offset, const void *default_value, const NodeEnum *enum_values=nullptr, const NodeType *node_type=nullptr, int flags=0, int extra_flags=0)
const NodeType * base
Definition node_type.h:127
ustring name
Definition node_type.h:125
static unordered_map< ustring, NodeType > & types()
void register_output(ustring name, ustring ui_name, SocketType::Type type)
const SocketType * find_input(ustring name) const
vector< SocketType, std::allocator< SocketType > > outputs
Definition node_type.h:129
static size_t size(Type type, bool packed)
Definition node_type.cpp:30
const void * default_value
Definition node_type.h:84
ustring name
Definition node_type.h:81
const NodeType * node_type
Definition node_type.h:86
@ BOOLEAN_ARRAY
Definition node_type.h:45
@ TRANSFORM_ARRAY
Definition node_type.h:54
static size_t max_size()
Definition node_type.cpp:93
ustring ui_name
Definition node_type.h:88
Type type
Definition node_type.h:82
size_t storage_size() const
Definition node_type.cpp:15
static bool is_float3(Type type)
static ustring type_name(Type type)
static void * zero_default_value()
Definition node_type.cpp:98
const NodeEnum * enum_values
Definition node_type.h:85
size_t packed_size() const
Definition node_type.cpp:20
SocketModifiedFlags modified_flag_bit
Definition node_type.h:89
bool is_array() const
Definition node_type.cpp:25
int struct_offset
Definition node_type.h:83
ccl_device_inline Transform transform_zero()
Definition transform.h:254