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