Blender V5.3
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
30size_t SocketType::size(Type type, bool packed)
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<packed_float3>);
73 case VECTOR_ARRAY:
74 return sizeof(array<packed_float3>);
75 case POINT_ARRAY:
76 return sizeof(array<packed_float3>);
77 case NORMAL_ARRAY:
78 return sizeof(array<packed_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;
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() < SocketModifiedFlags().size());
168 socket.modified_flag_bit = 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 *
210 * We want to be able to find and enumerate types without the need for a centralized
211 * function for all node types. Due to static initialization order issues and guarded
212 * allocators, there are some subtle implementation choices.
213 *
214 * Only the init functions are gathered on static initialization, as registering the
215 * node type itself could otherwise happen in the wrong order for base and derived
216 * types, and Blender's guarded allocator may not have been initialized yet.
217 *
218 * The initialization functions are stored in std::vector without guarded allocation
219 * as that may not been properly initialized yet. */
220
222{
223 static thread_mutex types_mutex_;
224 return types_mutex_;
225}
226
227static unordered_map<ustring, NodeType> &types()
228{
229 static unordered_map<ustring, NodeType> _types;
230 return _types;
231}
232
234{
235 static thread_mutex types_on_init_mutex_;
236 return types_on_init_mutex_;
237}
238
239static std::vector<const NodeType *(*)()> &types_on_init()
240{
241 static std::vector<const NodeType *(*)()> _types_on_init;
242 return _types_on_init;
243}
244
246{
248 for (const auto &init_func : types_on_init()) {
249 init_func();
250 }
251 types_on_init().clear();
252}
253
254bool NodeType::register_on_init(const NodeType *(*init_func)())
255{
257 types_on_init().push_back(init_func);
258 return true;
259}
260
261NodeType *NodeType::add(const char *name_, CreateFunc create_, Type type_, const NodeType *base_)
262{
263 const ustring name(name_);
264
265 /* Types can be lazily registered from multiple threads. */
267
268 if (types().contains(name)) {
269 LOG_ERROR << "Node type " << name_ << " registered twice";
270 assert(0);
271 return nullptr;
272 }
273
274 types()[name] = NodeType(type_, base_);
275
276 NodeType *type = &types()[name];
277 type->name = name;
278 type->create = create_;
279 return type;
280}
281
283{
285
287 const unordered_map<ustring, NodeType>::iterator it = types().find(name);
288 return (it == types().end()) ? nullptr : &it->second;
289}
290
292{
294
296 vector<ustring> names;
297 for (const auto &pair : types()) {
298 names.push_back(pair.first);
299 }
300
301 return names;
302}
303
volatile int lock
unsigned long long int uint64_t
#define CCL_NAMESPACE_END
#define assert(assertion)
#define LOG_ERROR
Definition log.h:101
static unordered_map< ustring, NodeType > & types()
static thread_mutex & types_on_init_mutex()
static thread_mutex & types_mutex()
static void ensure_types_initialized()
static std::vector< const NodeType *(*)()> & types_on_init()
std::bitset< 128 > SocketModifiedFlags
Definition node_type.h:24
static PyObject * init_func(PyObject *, PyObject *args)
Definition python.cpp:119
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Type type
Definition node_type.h:129
vector< SocketType, std::allocator< SocketType > > inputs
Definition node_type.h:131
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:126
const SocketType * find_output(ustring name) const
static const NodeType * find(ustring name)
CreateFunc create
Definition node_type.h:133
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)
static vector< ustring > type_names()
static bool register_on_init(const NodeType *(*init_func)())
const NodeType * base
Definition node_type.h:130
ustring name
Definition node_type.h:128
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:132
static size_t size(Type type, bool packed)
Definition node_type.cpp:30
const void * default_value
Definition node_type.h:87
ustring name
Definition node_type.h:84
const NodeType * node_type
Definition node_type.h:89
@ BOOLEAN_ARRAY
Definition node_type.h:48
@ TRANSFORM_ARRAY
Definition node_type.h:57
static size_t max_size()
Definition node_type.cpp:93
ustring ui_name
Definition node_type.h:91
Type type
Definition node_type.h:85
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:88
uint8_t modified_flag_bit
Definition node_type.h:92
size_t packed_size() const
Definition node_type.cpp:20
bool is_array() const
Definition node_type.cpp:25
int struct_offset
Definition node_type.h:86
std::mutex thread_mutex
Definition thread.h:27
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28
ccl_device_inline Transform transform_zero()
Definition transform.h:280