Depsgraph: Cleanup, don't use view layer name
[blender.git] / source / blender / depsgraph / intern / nodes / deg_node_component.cc
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2013 Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Joshua Leung
22  * Contributor(s): None Yet
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/nodes/deg_node_component.cc
28  *  \ingroup depsgraph
29  */
30
31 #include "intern/nodes/deg_node_component.h"
32
33 #include <stdio.h>
34 #include <cstring>  /* required for STREQ later on. */
35
36 #include "BLI_utildefines.h"
37 #include "BLI_ghash.h"
38
39 extern "C" {
40 #include "DNA_object_types.h"
41
42 #include "BKE_action.h"
43 } /* extern "C" */
44
45 #include "intern/nodes/deg_node_operation.h"
46 #include "intern/depsgraph_intern.h"
47 #include "util/deg_util_foreach.h"
48
49 namespace DEG {
50
51 /* *********** */
52 /* Outer Nodes */
53
54 /* Standard Component Methods ============================= */
55
56 ComponentDepsNode::OperationIDKey::OperationIDKey()
57         : opcode(DEG_OPCODE_OPERATION),
58           name(""),
59           name_tag(-1)
60 {
61 }
62
63 ComponentDepsNode::OperationIDKey::OperationIDKey(eDepsOperation_Code opcode)
64         : opcode(opcode),
65           name(""),
66           name_tag(-1)
67 {
68 }
69
70 ComponentDepsNode::OperationIDKey::OperationIDKey(eDepsOperation_Code opcode,
71                                                  const char *name,
72                                                  int name_tag)
73         : opcode(opcode),
74           name(name),
75           name_tag(name_tag)
76 {
77 }
78
79 string ComponentDepsNode::OperationIDKey::identifier() const
80 {
81         char codebuf[5];
82         BLI_snprintf(codebuf, sizeof(codebuf), "%d", opcode);
83         return string("OperationIDKey(") + codebuf + ", " + name + ")";
84 }
85
86 bool ComponentDepsNode::OperationIDKey::operator==(
87         const OperationIDKey &other) const
88 {
89         return (opcode == other.opcode) &&
90                 (STREQ(name, other.name)) &&
91                 (name_tag == other.name_tag);
92 }
93
94 static unsigned int comp_node_hash_key(const void *key_v)
95 {
96         const ComponentDepsNode::OperationIDKey *key =
97                 reinterpret_cast<const ComponentDepsNode::OperationIDKey *>(key_v);
98         return BLI_ghashutil_combine_hash(BLI_ghashutil_uinthash(key->opcode),
99                                           BLI_ghashutil_strhash_p(key->name));
100 }
101
102 static bool comp_node_hash_key_cmp(const void *a, const void *b)
103 {
104         const ComponentDepsNode::OperationIDKey *key_a =
105                 reinterpret_cast<const ComponentDepsNode::OperationIDKey *>(a);
106         const ComponentDepsNode::OperationIDKey *key_b =
107                 reinterpret_cast<const ComponentDepsNode::OperationIDKey *>(b);
108         return !(*key_a == *key_b);
109 }
110
111 static void comp_node_hash_key_free(void *key_v)
112 {
113         typedef ComponentDepsNode::OperationIDKey OperationIDKey;
114         OperationIDKey *key = reinterpret_cast<OperationIDKey *>(key_v);
115         OBJECT_GUARDED_DELETE(key, OperationIDKey);
116 }
117
118 static void comp_node_hash_value_free(void *value_v)
119 {
120         OperationDepsNode *op_node = reinterpret_cast<OperationDepsNode *>(value_v);
121         OBJECT_GUARDED_DELETE(op_node, OperationDepsNode);
122 }
123
124 ComponentDepsNode::ComponentDepsNode() :
125     entry_operation(NULL),
126     exit_operation(NULL)
127 {
128         operations_map = BLI_ghash_new(comp_node_hash_key,
129                                        comp_node_hash_key_cmp,
130                                        "Depsgraph id hash");
131 }
132
133 /* Initialize 'component' node - from pointer data given */
134 void ComponentDepsNode::init(const ID * /*id*/,
135                              const char * /*subdata*/)
136 {
137         /* hook up eval context? */
138         // XXX: maybe this needs a special API?
139 }
140
141 /* Free 'component' node */
142 ComponentDepsNode::~ComponentDepsNode()
143 {
144         clear_operations();
145         if (operations_map != NULL) {
146                 BLI_ghash_free(operations_map,
147                                comp_node_hash_key_free,
148                                comp_node_hash_value_free);
149         }
150 }
151
152 string ComponentDepsNode::identifier() const
153 {
154         string idname = this->owner->name;
155
156         char typebuf[16];
157         sprintf(typebuf, "(%d)", type);
158
159         return string(typebuf) + name + " : " + idname;
160 }
161
162 OperationDepsNode *ComponentDepsNode::find_operation(OperationIDKey key) const
163 {
164         OperationDepsNode *node = reinterpret_cast<OperationDepsNode *>(BLI_ghash_lookup(operations_map, &key));
165         if (node != NULL) {
166                 return node;
167         }
168         else {
169                 fprintf(stderr, "%s: find_operation(%s) failed\n",
170                         this->identifier().c_str(), key.identifier().c_str());
171                 BLI_assert(!"Request for non-existing operation, should not happen");
172                 return NULL;
173         }
174 }
175
176 OperationDepsNode *ComponentDepsNode::find_operation(eDepsOperation_Code opcode,
177                                                      const char *name,
178                                                      int name_tag) const
179 {
180         OperationIDKey key(opcode, name, name_tag);
181         return find_operation(key);
182 }
183
184 OperationDepsNode *ComponentDepsNode::has_operation(OperationIDKey key) const
185 {
186         return reinterpret_cast<OperationDepsNode *>(BLI_ghash_lookup(operations_map, &key));
187 }
188
189 OperationDepsNode *ComponentDepsNode::has_operation(eDepsOperation_Code opcode,
190                                                     const char *name,
191                                                     int name_tag) const
192 {
193         OperationIDKey key(opcode, name, name_tag);
194         return has_operation(key);
195 }
196
197 OperationDepsNode *ComponentDepsNode::add_operation(const DepsEvalOperationCb& op,
198                                                     eDepsOperation_Code opcode,
199                                                     const char *name,
200                                                     int name_tag)
201 {
202         OperationDepsNode *op_node = has_operation(opcode, name, name_tag);
203         if (!op_node) {
204                 DepsNodeFactory *factory = deg_get_node_factory(DEG_NODE_TYPE_OPERATION);
205                 op_node = (OperationDepsNode *)factory->create_node(this->owner->id_orig, "", name);
206
207                 /* register opnode in this component's operation set */
208                 OperationIDKey *key = OBJECT_GUARDED_NEW(OperationIDKey, opcode, name, name_tag);
209                 BLI_ghash_insert(operations_map, key, op_node);
210
211                 /* set backlink */
212                 op_node->owner = this;
213         }
214         else {
215                 fprintf(stderr, "add_operation: Operation already exists - %s has %s at %p\n",
216                         this->identifier().c_str(), op_node->identifier().c_str(), op_node);
217                 BLI_assert(!"Should not happen!");
218         }
219
220         /* attach extra data */
221         op_node->evaluate = op;
222         op_node->opcode = opcode;
223         op_node->name = name;
224
225         return op_node;
226 }
227
228 void ComponentDepsNode::set_entry_operation(OperationDepsNode *op_node)
229 {
230         BLI_assert(entry_operation == NULL);
231         entry_operation = op_node;
232 }
233
234 void ComponentDepsNode::set_exit_operation(OperationDepsNode *op_node)
235 {
236         BLI_assert(exit_operation == NULL);
237         exit_operation = op_node;
238 }
239
240 void ComponentDepsNode::clear_operations()
241 {
242         if (operations_map != NULL) {
243                 BLI_ghash_clear(operations_map,
244                                 comp_node_hash_key_free,
245                                 comp_node_hash_value_free);
246         }
247         foreach (OperationDepsNode *op_node, operations) {
248                 OBJECT_GUARDED_DELETE(op_node, OperationDepsNode);
249         }
250         operations.clear();
251 }
252
253 void ComponentDepsNode::tag_update(Depsgraph *graph)
254 {
255         OperationDepsNode *entry_op = get_entry_operation();
256         if (entry_op != NULL && entry_op->flag & DEPSOP_FLAG_NEEDS_UPDATE) {
257                 return;
258         }
259         foreach (OperationDepsNode *op_node, operations) {
260                 op_node->tag_update(graph);
261         }
262         // It is possible that tag happens before finalization.
263         if (operations_map != NULL) {
264                 GHASH_FOREACH_BEGIN(OperationDepsNode *, op_node, operations_map)
265                 {
266                         op_node->tag_update(graph);
267                 }
268                 GHASH_FOREACH_END();
269         }
270 }
271
272 OperationDepsNode *ComponentDepsNode::get_entry_operation()
273 {
274         if (entry_operation) {
275                 return entry_operation;
276         }
277         else if (operations_map != NULL && BLI_ghash_size(operations_map) == 1) {
278                 OperationDepsNode *op_node = NULL;
279                 /* TODO(sergey): This is somewhat slow. */
280                 GHASH_FOREACH_BEGIN(OperationDepsNode *, tmp, operations_map)
281                 {
282                         op_node = tmp;
283                 }
284                 GHASH_FOREACH_END();
285                 /* Cache for the subsequent usage. */
286                 entry_operation = op_node;
287                 return op_node;
288         }
289         else if (operations.size() == 1) {
290                 return operations[0];
291         }
292         return NULL;
293 }
294
295 OperationDepsNode *ComponentDepsNode::get_exit_operation()
296 {
297         if (exit_operation) {
298                 return exit_operation;
299         }
300         else if (operations_map != NULL && BLI_ghash_size(operations_map) == 1) {
301                 OperationDepsNode *op_node = NULL;
302                 /* TODO(sergey): This is somewhat slow. */
303                 GHASH_FOREACH_BEGIN(OperationDepsNode *, tmp, operations_map)
304                 {
305                         op_node = tmp;
306                 }
307                 GHASH_FOREACH_END();
308                 /* Cache for the subsequent usage. */
309                 exit_operation = op_node;
310                 return op_node;
311         }
312         else if (operations.size() == 1) {
313                 return operations[0];
314         }
315         return NULL;
316 }
317
318 void ComponentDepsNode::finalize_build(Depsgraph * /*graph*/)
319 {
320         operations.reserve(BLI_ghash_size(operations_map));
321         GHASH_FOREACH_BEGIN(OperationDepsNode *, op_node, operations_map)
322         {
323                 operations.push_back(op_node);
324         }
325         GHASH_FOREACH_END();
326         BLI_ghash_free(operations_map,
327                        comp_node_hash_key_free,
328                        NULL);
329         operations_map = NULL;
330 }
331
332 /* Parameter Component Defines ============================ */
333
334 DEG_DEPSNODE_DEFINE(ParametersComponentDepsNode, DEG_NODE_TYPE_PARAMETERS, "Parameters Component");
335 static DepsNodeFactoryImpl<ParametersComponentDepsNode> DNTI_PARAMETERS;
336
337 /* Animation Component Defines ============================ */
338
339 DEG_DEPSNODE_DEFINE(AnimationComponentDepsNode, DEG_NODE_TYPE_ANIMATION, "Animation Component");
340 static DepsNodeFactoryImpl<AnimationComponentDepsNode> DNTI_ANIMATION;
341
342 /* Transform Component Defines ============================ */
343
344 DEG_DEPSNODE_DEFINE(TransformComponentDepsNode, DEG_NODE_TYPE_TRANSFORM, "Transform Component");
345 static DepsNodeFactoryImpl<TransformComponentDepsNode> DNTI_TRANSFORM;
346
347 /* Proxy Component Defines ================================ */
348
349 DEG_DEPSNODE_DEFINE(ProxyComponentDepsNode, DEG_NODE_TYPE_PROXY, "Proxy Component");
350 static DepsNodeFactoryImpl<ProxyComponentDepsNode> DNTI_PROXY;
351
352 /* Geometry Component Defines ============================= */
353
354 DEG_DEPSNODE_DEFINE(GeometryComponentDepsNode, DEG_NODE_TYPE_GEOMETRY, "Geometry Component");
355 static DepsNodeFactoryImpl<GeometryComponentDepsNode> DNTI_GEOMETRY;
356
357 /* Sequencer Component Defines ============================ */
358
359 DEG_DEPSNODE_DEFINE(SequencerComponentDepsNode, DEG_NODE_TYPE_SEQUENCER, "Sequencer Component");
360 static DepsNodeFactoryImpl<SequencerComponentDepsNode> DNTI_SEQUENCER;
361
362 /* Pose Component ========================================= */
363
364 DEG_DEPSNODE_DEFINE(PoseComponentDepsNode, DEG_NODE_TYPE_EVAL_POSE, "Pose Eval Component");
365 static DepsNodeFactoryImpl<PoseComponentDepsNode> DNTI_EVAL_POSE;
366
367 /* Bone Component ========================================= */
368
369 /* Initialize 'bone component' node - from pointer data given */
370 void BoneComponentDepsNode::init(const ID *id, const char *subdata)
371 {
372         /* generic component-node... */
373         ComponentDepsNode::init(id, subdata);
374
375         /* name of component comes is bone name */
376         /* TODO(sergey): This sets name to an empty string because subdata is
377          * empty. Is it a bug?
378          */
379         //this->name = subdata;
380
381         /* bone-specific node data */
382         Object *object = (Object *)id;
383         this->pchan = BKE_pose_channel_find_name(object->pose, subdata);
384 }
385
386 DEG_DEPSNODE_DEFINE(BoneComponentDepsNode, DEG_NODE_TYPE_BONE, "Bone Component");
387 static DepsNodeFactoryImpl<BoneComponentDepsNode> DNTI_BONE;
388
389 /* Particles Component Defines ============================ */
390
391 DEG_DEPSNODE_DEFINE(ParticlesComponentDepsNode, DEG_NODE_TYPE_EVAL_PARTICLES, "Particles Component");
392 static DepsNodeFactoryImpl<ParticlesComponentDepsNode> DNTI_EVAL_PARTICLES;
393
394 /* Shading Component Defines ============================ */
395
396 DEG_DEPSNODE_DEFINE(ShadingComponentDepsNode, DEG_NODE_TYPE_SHADING, "Shading Component");
397 static DepsNodeFactoryImpl<ShadingComponentDepsNode> DNTI_SHADING;
398
399 /* Shading Parameters Component Defines ============================ */
400
401 DEG_DEPSNODE_DEFINE(ShadingParametersComponentDepsNode, DEG_NODE_TYPE_SHADING_PARAMETERS, "Shading Parameters Component");
402 static DepsNodeFactoryImpl<ShadingParametersComponentDepsNode> DNTI_SHADING_PARAMETERS;
403
404 /* Cache Component Defines ============================ */
405
406 DEG_DEPSNODE_DEFINE(CacheComponentDepsNode, DEG_NODE_TYPE_CACHE, "Cache Component");
407 static DepsNodeFactoryImpl<CacheComponentDepsNode> DNTI_CACHE;
408
409 /* Layer Collections Defines ============================ */
410
411 DEG_DEPSNODE_DEFINE(LayerCollectionsDepsNode, DEG_NODE_TYPE_LAYER_COLLECTIONS, "Layer Collections Component");
412 static DepsNodeFactoryImpl<LayerCollectionsDepsNode> DNTI_LAYER_COLLECTIONS;
413
414 /* Copy-on-write Defines ============================ */
415
416 DEG_DEPSNODE_DEFINE(CopyOnWriteDepsNode, DEG_NODE_TYPE_COPY_ON_WRITE, "Copy-on-Write Component");
417 static DepsNodeFactoryImpl<CopyOnWriteDepsNode> DNTI_COPY_ON_WRITE;
418
419 /* Node Types Register =================================== */
420
421 void deg_register_component_depsnodes()
422 {
423         deg_register_node_typeinfo(&DNTI_PARAMETERS);
424         deg_register_node_typeinfo(&DNTI_PROXY);
425         deg_register_node_typeinfo(&DNTI_ANIMATION);
426         deg_register_node_typeinfo(&DNTI_TRANSFORM);
427         deg_register_node_typeinfo(&DNTI_GEOMETRY);
428         deg_register_node_typeinfo(&DNTI_SEQUENCER);
429
430         deg_register_node_typeinfo(&DNTI_EVAL_POSE);
431         deg_register_node_typeinfo(&DNTI_BONE);
432
433         deg_register_node_typeinfo(&DNTI_EVAL_PARTICLES);
434
435         deg_register_node_typeinfo(&DNTI_SHADING);
436         deg_register_node_typeinfo(&DNTI_SHADING_PARAMETERS);
437
438         deg_register_node_typeinfo(&DNTI_CACHE);
439
440         deg_register_node_typeinfo(&DNTI_LAYER_COLLECTIONS);
441
442         deg_register_node_typeinfo(&DNTI_COPY_ON_WRITE);
443 }
444
445 }  // namespace DEG