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