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