2 * Copyright 2011-2016 Blender Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "graph/node.h"
18 #include "graph/node_type.h"
20 #include "util/util_foreach.h"
21 #include "util/util_md5.h"
22 #include "util/util_param.h"
23 #include "util/util_transform.h"
29 Node::Node(const NodeType *type_, ustring name_)
30 : name(name_), type(type_)
34 /* assign non-empty name, convenient for debugging */
39 /* initialize default values */
40 foreach(const SocketType& socket, type->inputs) {
41 set_default_value(socket);
50 static T& get_socket_value(const Node *node, const SocketType& socket)
52 return (T&)*(((char*)node) + socket.struct_offset);
56 static bool is_socket_float3(const SocketType& socket)
58 return socket.type == SocketType::COLOR ||
59 socket.type == SocketType::POINT ||
60 socket.type == SocketType::VECTOR ||
61 socket.type == SocketType::NORMAL;
64 static bool is_socket_array_float3(const SocketType& socket)
66 return socket.type == SocketType::COLOR_ARRAY ||
67 socket.type == SocketType::POINT_ARRAY ||
68 socket.type == SocketType::VECTOR_ARRAY ||
69 socket.type == SocketType::NORMAL_ARRAY;
74 void Node::set(const SocketType& input, bool value)
76 assert(input.type == SocketType::BOOLEAN);
77 get_socket_value<bool>(this, input) = value;
80 void Node::set(const SocketType& input, int value)
82 assert((input.type == SocketType::INT || input.type == SocketType::ENUM));
83 get_socket_value<int>(this, input) = value;
86 void Node::set(const SocketType& input, uint value)
88 assert(input.type == SocketType::UINT);
89 get_socket_value<uint>(this, input) = value;
92 void Node::set(const SocketType& input, float value)
94 assert(input.type == SocketType::FLOAT);
95 get_socket_value<float>(this, input) = value;
98 void Node::set(const SocketType& input, float2 value)
100 assert(input.type == SocketType::FLOAT);
101 get_socket_value<float2>(this, input) = value;
104 void Node::set(const SocketType& input, float3 value)
106 assert(is_socket_float3(input));
107 get_socket_value<float3>(this, input) = value;
110 void Node::set(const SocketType& input, const char *value)
112 set(input, ustring(value));
115 void Node::set(const SocketType& input, ustring value)
117 if(input.type == SocketType::STRING) {
118 get_socket_value<ustring>(this, input) = value;
120 else if(input.type == SocketType::ENUM) {
121 const NodeEnum& enm = *input.enum_values;
122 if(enm.exists(value)) {
123 get_socket_value<int>(this, input) = enm[value];
134 void Node::set(const SocketType& input, const Transform& value)
136 assert(input.type == SocketType::TRANSFORM);
137 get_socket_value<Transform>(this, input) = value;
140 void Node::set(const SocketType& input, Node *value)
142 assert(input.type == SocketType::TRANSFORM);
143 get_socket_value<Node*>(this, input) = value;
146 /* set array values */
147 void Node::set(const SocketType& input, array<bool>& value)
149 assert(input.type == SocketType::BOOLEAN_ARRAY);
150 get_socket_value<array<bool> >(this, input).steal_data(value);
153 void Node::set(const SocketType& input, array<int>& value)
155 assert(input.type == SocketType::INT_ARRAY);
156 get_socket_value<array<int> >(this, input).steal_data(value);
159 void Node::set(const SocketType& input, array<float>& value)
161 assert(input.type == SocketType::FLOAT_ARRAY);
162 get_socket_value<array<float> >(this, input).steal_data(value);
165 void Node::set(const SocketType& input, array<float2>& value)
167 assert(input.type == SocketType::FLOAT_ARRAY);
168 get_socket_value<array<float2> >(this, input).steal_data(value);
171 void Node::set(const SocketType& input, array<float3>& value)
173 assert(is_socket_array_float3(input));
174 get_socket_value<array<float3> >(this, input).steal_data(value);
177 void Node::set(const SocketType& input, array<ustring>& value)
179 assert(input.type == SocketType::STRING_ARRAY);
180 get_socket_value<array<ustring> >(this, input).steal_data(value);
183 void Node::set(const SocketType& input, array<Transform>& value)
185 assert(input.type == SocketType::TRANSFORM_ARRAY);
186 get_socket_value<array<Transform> >(this, input).steal_data(value);
189 void Node::set(const SocketType& input, array<Node*>& value)
191 assert(input.type == SocketType::TRANSFORM_ARRAY);
192 get_socket_value<array<Node*> >(this, input).steal_data(value);
196 bool Node::get_bool(const SocketType& input) const
198 assert(input.type == SocketType::BOOLEAN);
199 return get_socket_value<bool>(this, input);
202 int Node::get_int(const SocketType& input) const
204 assert(input.type == SocketType::INT || input.type == SocketType::ENUM);
205 return get_socket_value<int>(this, input);
208 uint Node::get_uint(const SocketType& input) const
210 assert(input.type == SocketType::UINT);
211 return get_socket_value<uint>(this, input);
214 float Node::get_float(const SocketType& input) const
216 assert(input.type == SocketType::FLOAT);
217 return get_socket_value<float>(this, input);
220 float2 Node::get_float2(const SocketType& input) const
222 assert(input.type == SocketType::FLOAT);
223 return get_socket_value<float2>(this, input);
226 float3 Node::get_float3(const SocketType& input) const
228 assert(is_socket_float3(input));
229 return get_socket_value<float3>(this, input);
232 ustring Node::get_string(const SocketType& input) const
234 if(input.type == SocketType::STRING) {
235 return get_socket_value<ustring>(this, input);
237 else if(input.type == SocketType::ENUM) {
238 const NodeEnum& enm = *input.enum_values;
239 int intvalue = get_socket_value<int>(this, input);
240 return (enm.exists(intvalue)) ? enm[intvalue] : ustring();
248 Transform Node::get_transform(const SocketType& input) const
250 assert(input.type == SocketType::TRANSFORM);
251 return get_socket_value<Transform>(this, input);
254 Node *Node::get_node(const SocketType& input) const
256 assert(input.type == SocketType::NODE);
257 return get_socket_value<Node*>(this, input);
260 /* get array values */
261 const array<bool>& Node::get_bool_array(const SocketType& input) const
263 assert(input.type == SocketType::BOOLEAN_ARRAY);
264 return get_socket_value<array<bool> >(this, input);
267 const array<int>& Node::get_int_array(const SocketType& input) const
269 assert(input.type == SocketType::INT_ARRAY);
270 return get_socket_value<array<int> >(this, input);
273 const array<float>& Node::get_float_array(const SocketType& input) const
275 assert(input.type == SocketType::FLOAT_ARRAY);
276 return get_socket_value<array<float> >(this, input);
279 const array<float2>& Node::get_float2_array(const SocketType& input) const
281 assert(input.type == SocketType::FLOAT_ARRAY);
282 return get_socket_value<array<float2> >(this, input);
285 const array<float3>& Node::get_float3_array(const SocketType& input) const
287 assert(is_socket_array_float3(input));
288 return get_socket_value<array<float3> >(this, input);
291 const array<ustring>& Node::get_string_array(const SocketType& input) const
293 assert(input.type == SocketType::STRING_ARRAY);
294 return get_socket_value<array<ustring> >(this, input);
297 const array<Transform>& Node::get_transform_array(const SocketType& input) const
299 assert(input.type == SocketType::TRANSFORM_ARRAY);
300 return get_socket_value<array<Transform> >(this, input);
303 const array<Node*>& Node::get_node_array(const SocketType& input) const
305 assert(input.type == SocketType::NODE_ARRAY);
306 return get_socket_value<array<Node*> >(this, input);
309 /* generic value operations */
311 bool Node::has_default_value(const SocketType& input) const
313 const void *src = input.default_value;
314 void *dst = &get_socket_value<char>(this, input);
315 return memcmp(dst, src, input.size()) == 0;
318 void Node::set_default_value(const SocketType& socket)
320 const void *src = socket.default_value;
321 void *dst = ((char*)this) + socket.struct_offset;
322 memcpy(dst, src, socket.size());
326 static void copy_array(const Node *node, const SocketType& socket, const Node *other, const SocketType& other_socket)
328 const array<T>* src = (const array<T>*)(((char*)other) + other_socket.struct_offset);
329 array<T>* dst = (array<T>*)(((char*)node) + socket.struct_offset);
333 void Node::copy_value(const SocketType& socket, const Node& other, const SocketType& other_socket)
335 assert(socket.type == other_socket.type);
337 if(socket.is_array()) {
338 switch(socket.type) {
339 case SocketType::BOOLEAN_ARRAY: copy_array<bool>(this, socket, &other, other_socket); break;
340 case SocketType::FLOAT_ARRAY: copy_array<float>(this, socket, &other, other_socket); break;
341 case SocketType::INT_ARRAY: copy_array<int>(this, socket, &other, other_socket); break;
342 case SocketType::COLOR_ARRAY: copy_array<float3>(this, socket, &other, other_socket); break;
343 case SocketType::VECTOR_ARRAY: copy_array<float3>(this, socket, &other, other_socket); break;
344 case SocketType::POINT_ARRAY: copy_array<float3>(this, socket, &other, other_socket); break;
345 case SocketType::NORMAL_ARRAY: copy_array<float3>(this, socket, &other, other_socket); break;
346 case SocketType::POINT2_ARRAY: copy_array<float2>(this, socket, &other, other_socket); break;
347 case SocketType::STRING_ARRAY: copy_array<ustring>(this, socket, &other, other_socket); break;
348 case SocketType::TRANSFORM_ARRAY: copy_array<Transform>(this, socket, &other, other_socket); break;
349 case SocketType::NODE_ARRAY: copy_array<void*>(this, socket, &other, other_socket); break;
350 default: assert(0); break;
354 const void *src = ((char*)&other) + other_socket.struct_offset;
355 void *dst = ((char*)this) + socket.struct_offset;
356 memcpy(dst, src, socket.size());
361 static bool is_array_equal(const Node *node, const Node *other, const SocketType& socket)
363 const array<T>* a = (const array<T>*)(((char*)node) + socket.struct_offset);
364 const array<T>* b = (const array<T>*)(((char*)other) + socket.struct_offset);
369 static bool is_value_equal(const Node *node, const Node *other, const SocketType& socket)
371 const T *a = (const T*)(((char*)node) + socket.struct_offset);
372 const T *b = (const T*)(((char*)other) + socket.struct_offset);
376 bool Node::equals_value(const Node& other, const SocketType& socket) const
378 switch(socket.type) {
379 case SocketType::BOOLEAN: return is_value_equal<bool>(this, &other, socket);
380 case SocketType::FLOAT: return is_value_equal<float>(this, &other, socket);
381 case SocketType::INT: return is_value_equal<int>(this, &other, socket);
382 case SocketType::UINT: return is_value_equal<uint>(this, &other, socket);
383 case SocketType::COLOR: return is_value_equal<float3>(this, &other, socket);
384 case SocketType::VECTOR: return is_value_equal<float3>(this, &other, socket);
385 case SocketType::POINT: return is_value_equal<float3>(this, &other, socket);
386 case SocketType::NORMAL: return is_value_equal<float3>(this, &other, socket);
387 case SocketType::POINT2: return is_value_equal<float2>(this, &other, socket);
388 case SocketType::CLOSURE: return true;
389 case SocketType::STRING: return is_value_equal<ustring>(this, &other, socket);
390 case SocketType::ENUM: return is_value_equal<int>(this, &other, socket);
391 case SocketType::TRANSFORM: return is_value_equal<Transform>(this, &other, socket);
392 case SocketType::NODE: return is_value_equal<void*>(this, &other, socket);
394 case SocketType::BOOLEAN_ARRAY: return is_array_equal<bool>(this, &other, socket);
395 case SocketType::FLOAT_ARRAY: return is_array_equal<float>(this, &other, socket);
396 case SocketType::INT_ARRAY: return is_array_equal<int>(this, &other, socket);
397 case SocketType::COLOR_ARRAY: return is_array_equal<float3>(this, &other, socket);
398 case SocketType::VECTOR_ARRAY: return is_array_equal<float3>(this, &other, socket);
399 case SocketType::POINT_ARRAY: return is_array_equal<float3>(this, &other, socket);
400 case SocketType::NORMAL_ARRAY: return is_array_equal<float3>(this, &other, socket);
401 case SocketType::POINT2_ARRAY: return is_array_equal<float2>(this, &other, socket);
402 case SocketType::STRING_ARRAY: return is_array_equal<ustring>(this, &other, socket);
403 case SocketType::TRANSFORM_ARRAY: return is_array_equal<Transform>(this, &other, socket);
404 case SocketType::NODE_ARRAY: return is_array_equal<void*>(this, &other, socket);
406 case SocketType::UNDEFINED: return true;
414 bool Node::equals(const Node& other) const
416 assert(type == other.type);
418 foreach(const SocketType& socket, type->inputs) {
419 if(!equals_value(other, socket))
429 static void value_hash(const Node *node, const SocketType& socket, MD5Hash& md5)
431 md5.append(((uint8_t*)node) + socket.struct_offset, socket.size());
434 static void float3_hash(const Node *node, const SocketType& socket, MD5Hash& md5)
436 /* Don't compare 4th element used for padding. */
437 md5.append(((uint8_t*)node) + socket.struct_offset, sizeof(float) * 3);
441 static void array_hash(const Node *node, const SocketType& socket, MD5Hash& md5)
443 const array<T>& a = *(const array<T>*)(((char*)node) + socket.struct_offset);
444 for (size_t i = 0; i < a.size(); i++) {
445 md5.append((uint8_t*)&a[i], sizeof(T));
449 static void float3_array_hash(const Node *node, const SocketType& socket, MD5Hash& md5)
451 /* Don't compare 4th element used for padding. */
452 const array<float3>& a = *(const array<float3>*)(((char*)node) + socket.struct_offset);
453 for (size_t i = 0; i < a.size(); i++) {
454 md5.append((uint8_t*)&a[i], sizeof(float) * 3);
458 void Node::hash(MD5Hash& md5)
460 md5.append(type->name.string());
462 foreach(const SocketType& socket, type->inputs) {
463 md5.append(socket.name.string());
465 switch(socket.type) {
466 case SocketType::BOOLEAN: value_hash<bool>(this, socket, md5); break;
467 case SocketType::FLOAT: value_hash<float>(this, socket, md5); break;
468 case SocketType::INT: value_hash<int>(this, socket, md5); break;
469 case SocketType::UINT: value_hash<uint>(this, socket, md5); break;
470 case SocketType::COLOR: float3_hash(this, socket, md5); break;
471 case SocketType::VECTOR: float3_hash(this, socket, md5); break;
472 case SocketType::POINT: float3_hash(this, socket, md5); break;
473 case SocketType::NORMAL: float3_hash(this, socket, md5); break;
474 case SocketType::POINT2: value_hash<float2>(this, socket, md5); break;
475 case SocketType::CLOSURE: break;
476 case SocketType::STRING: value_hash<ustring>(this, socket, md5); break;
477 case SocketType::ENUM: value_hash<int>(this, socket, md5); break;
478 case SocketType::TRANSFORM: value_hash<Transform>(this, socket, md5); break;
479 case SocketType::NODE: value_hash<void*>(this, socket, md5); break;
481 case SocketType::BOOLEAN_ARRAY: array_hash<bool>(this, socket, md5); break;
482 case SocketType::FLOAT_ARRAY: array_hash<float>(this, socket, md5); break;
483 case SocketType::INT_ARRAY: array_hash<int>(this, socket, md5); break;
484 case SocketType::COLOR_ARRAY: float3_array_hash(this, socket, md5); break;
485 case SocketType::VECTOR_ARRAY: float3_array_hash(this, socket, md5); break;
486 case SocketType::POINT_ARRAY: float3_array_hash(this, socket, md5); break;
487 case SocketType::NORMAL_ARRAY: float3_array_hash(this, socket, md5); break;
488 case SocketType::POINT2_ARRAY: array_hash<float2>(this, socket, md5); break;
489 case SocketType::STRING_ARRAY: array_hash<ustring>(this, socket, md5); break;
490 case SocketType::TRANSFORM_ARRAY: array_hash<Transform>(this, socket, md5); break;
491 case SocketType::NODE_ARRAY: array_hash<void*>(this, socket, md5); break;
493 case SocketType::UNDEFINED: break;