Fix redundant declarations after recent changes in GPU debug
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_relations.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): Based on original depsgraph.c code - Blender Foundation (2005-2013)
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/builder/deg_builder_relations.cc
28  *  \ingroup depsgraph
29  *
30  * Methods for constructing depsgraph
31  */
32
33 #include "intern/builder/deg_builder_relations.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "MEM_guardedalloc.h"
40
41 extern "C" {
42 #include "BLI_blenlib.h"
43 #include "BLI_string.h"
44 #include "BLI_utildefines.h"
45
46 #include "DNA_action_types.h"
47 #include "DNA_anim_types.h"
48 #include "DNA_armature_types.h"
49 #include "DNA_camera_types.h"
50 #include "DNA_cachefile_types.h"
51 #include "DNA_constraint_types.h"
52 #include "DNA_curve_types.h"
53 #include "DNA_effect_types.h"
54 #include "DNA_gpencil_types.h"
55 #include "DNA_group_types.h"
56 #include "DNA_key_types.h"
57 #include "DNA_lamp_types.h"
58 #include "DNA_material_types.h"
59 #include "DNA_mesh_types.h"
60 #include "DNA_meta_types.h"
61 #include "DNA_node_types.h"
62 #include "DNA_particle_types.h"
63 #include "DNA_object_types.h"
64 #include "DNA_rigidbody_types.h"
65 #include "DNA_scene_types.h"
66 #include "DNA_texture_types.h"
67 #include "DNA_world_types.h"
68
69 #include "BKE_action.h"
70 #include "BKE_armature.h"
71 #include "BKE_animsys.h"
72 #include "BKE_constraint.h"
73 #include "BKE_curve.h"
74 #include "BKE_effect.h"
75 #include "BKE_fcurve.h"
76 #include "BKE_group.h"
77 #include "BKE_key.h"
78 #include "BKE_library.h"
79 #include "BKE_main.h"
80 #include "BKE_material.h"
81 #include "BKE_mball.h"
82 #include "BKE_modifier.h"
83 #include "BKE_node.h"
84 #include "BKE_object.h"
85 #include "BKE_particle.h"
86 #include "BKE_rigidbody.h"
87 #include "BKE_sound.h"
88 #include "BKE_texture.h"
89 #include "BKE_tracking.h"
90 #include "BKE_world.h"
91
92 #include "DEG_depsgraph.h"
93 #include "DEG_depsgraph_build.h"
94
95 #include "RNA_access.h"
96 #include "RNA_types.h"
97 } /* extern "C" */
98
99 #include "intern/builder/deg_builder.h"
100 #include "intern/builder/deg_builder_pchanmap.h"
101
102 #include "intern/nodes/deg_node.h"
103 #include "intern/nodes/deg_node_component.h"
104 #include "intern/nodes/deg_node_operation.h"
105
106 #include "intern/depsgraph_intern.h"
107 #include "intern/depsgraph_types.h"
108
109 #include "util/deg_util_foreach.h"
110
111 namespace DEG {
112
113 /* ***************** */
114 /* Relations Builder */
115
116 /* **** General purpose functions ****  */
117
118 RNAPathKey::RNAPathKey(ID *id, const char *path) :
119     id(id)
120 {
121         /* create ID pointer for root of path lookup */
122         PointerRNA id_ptr;
123         RNA_id_pointer_create(id, &id_ptr);
124         /* try to resolve path... */
125         int index;
126         if (!RNA_path_resolve_full(&id_ptr, path, &this->ptr, &this->prop, &index)) {
127                 this->ptr = PointerRNA_NULL;
128                 this->prop = NULL;
129         }
130 }
131
132 DepsgraphRelationBuilder::DepsgraphRelationBuilder(Depsgraph *graph) :
133     m_graph(graph)
134 {
135 }
136
137 RootDepsNode *DepsgraphRelationBuilder::find_node(const RootKey &key) const
138 {
139         (void)key;
140         BLI_assert(!"Doesn't seem to be correct");
141         return m_graph->root_node;
142 }
143
144 TimeSourceDepsNode *DepsgraphRelationBuilder::find_node(
145         const TimeSourceKey &key) const
146 {
147         if (key.id) {
148                 /* XXX TODO */
149                 return NULL;
150         }
151         else {
152                 return m_graph->root_node->time_source;
153         }
154 }
155
156 ComponentDepsNode *DepsgraphRelationBuilder::find_node(
157         const ComponentKey &key) const
158 {
159         IDDepsNode *id_node = m_graph->find_id_node(key.id);
160         if (!id_node) {
161                 fprintf(stderr, "find_node component: Could not find ID %s\n",
162                         (key.id != NULL) ? key.id->name : "<null>");
163                 return NULL;
164         }
165
166         ComponentDepsNode *node = id_node->find_component(key.type, key.name);
167         return node;
168 }
169
170 OperationDepsNode *DepsgraphRelationBuilder::find_node(
171         const OperationKey &key) const
172 {
173         IDDepsNode *id_node = m_graph->find_id_node(key.id);
174         if (!id_node) {
175                 fprintf(stderr, "find_node operation: Could not find ID\n");
176                 return NULL;
177         }
178
179         ComponentDepsNode *comp_node = id_node->find_component(key.component_type,
180                                                                key.component_name);
181         if (!comp_node) {
182                 fprintf(stderr, "find_node operation: Could not find component\n");
183                 return NULL;
184         }
185
186         OperationDepsNode *op_node = comp_node->find_operation(key.opcode, key.name);
187         if (!op_node) {
188                 fprintf(stderr, "find_node_operation: Failed for (%s, '%s')\n",
189                         DEG_OPNAMES[key.opcode], key.name.c_str());
190         }
191         return op_node;
192 }
193
194 DepsNode *DepsgraphRelationBuilder::find_node(const RNAPathKey &key) const
195 {
196         return m_graph->find_node_from_pointer(&key.ptr, key.prop);
197 }
198
199 OperationDepsNode *DepsgraphRelationBuilder::has_node(
200         const OperationKey &key) const
201 {
202         IDDepsNode *id_node = m_graph->find_id_node(key.id);
203         if (!id_node) {
204                 return NULL;
205         }
206         ComponentDepsNode *comp_node = id_node->find_component(key.component_type,
207                                                                key.component_name);
208         if (!comp_node) {
209                 return NULL;
210         }
211         return comp_node->has_operation(key.opcode, key.name);
212 }
213
214 void DepsgraphRelationBuilder::add_time_relation(TimeSourceDepsNode *timesrc,
215                                                  DepsNode *node_to,
216                                                  const char *description)
217 {
218         if (timesrc && node_to) {
219                 m_graph->add_new_relation(timesrc, node_to, DEPSREL_TYPE_TIME, description);
220         }
221         else {
222                 DEG_DEBUG_PRINTF("add_time_relation(%p = %s, %p = %s, %s) Failed\n",
223                                  timesrc,   (timesrc) ? timesrc->identifier().c_str() : "<None>",
224                                  node_to,   (node_to) ? node_to->identifier().c_str() : "<None>",
225                                  description);
226         }
227 }
228
229 void DepsgraphRelationBuilder::add_operation_relation(
230         OperationDepsNode *node_from,
231         OperationDepsNode *node_to,
232         eDepsRelation_Type type,
233         const char *description)
234 {
235         if (node_from && node_to) {
236                 m_graph->add_new_relation(node_from, node_to, type, description);
237         }
238         else {
239                 DEG_DEBUG_PRINTF("add_operation_relation(%p = %s, %p = %s, %d, %s) Failed\n",
240                                  node_from, (node_from) ? node_from->identifier().c_str() : "<None>",
241                                  node_to,   (node_to)   ? node_to->identifier().c_str() : "<None>",
242                                  type, description);
243         }
244 }
245
246 /* **** Functions to build relations between entities  **** */
247
248 void DepsgraphRelationBuilder::build_scene(Main *bmain, Scene *scene)
249 {
250         /* LIB_TAG_DOIT is used to indicate whether node for given ID was already
251          * created or not.
252          */
253         BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
254         /* XXX nested node trees are not included in tag-clearing above,
255          * so we need to do this manually.
256          */
257         FOREACH_NODETREE(bmain, nodetree, id) {
258                 if (id != (ID *)nodetree)
259                         nodetree->id.tag &= ~LIB_TAG_DOIT;
260         } FOREACH_NODETREE_END
261
262         if (scene->set) {
263                 // TODO: link set to scene, especially our timesource...
264         }
265
266         /* scene objects */
267         for (Base *base = (Base *)scene->base.first; base; base = base->next) {
268                 Object *ob = base->object;
269
270                 /* object itself */
271                 build_object(bmain, scene, ob);
272
273                 /* object that this is a proxy for */
274                 if (ob->proxy) {
275                         ob->proxy->proxy_from = ob;
276                         build_object(bmain, scene, ob->proxy);
277                         /* TODO(sergey): This is an inverted relation, matches old depsgraph
278                          * behavior and need to be investigated if it still need to be inverted.
279                          */
280                         ComponentKey ob_pose_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE);
281                         ComponentKey proxy_pose_key(&ob->proxy->id, DEPSNODE_TYPE_EVAL_POSE);
282                         add_relation(ob_pose_key, proxy_pose_key, DEPSREL_TYPE_TRANSFORM, "Proxy");
283                 }
284
285                 /* Object dupligroup. */
286                 if (ob->dup_group) {
287                         build_group(bmain, scene, ob, ob->dup_group);
288                 }
289         }
290
291         /* rigidbody */
292         if (scene->rigidbody_world) {
293                 build_rigidbody(scene);
294         }
295
296         /* scene's animation and drivers */
297         if (scene->adt) {
298                 build_animdata(&scene->id);
299         }
300
301         /* world */
302         if (scene->world) {
303                 build_world(scene->world);
304         }
305
306         /* compo nodes */
307         if (scene->nodetree) {
308                 build_compositor(scene);
309         }
310
311         /* grease pencil */
312         if (scene->gpd) {
313                 build_gpencil(&scene->id, scene->gpd);
314         }
315
316         for (Depsgraph::OperationNodes::const_iterator it_op = m_graph->operations.begin();
317              it_op != m_graph->operations.end();
318              ++it_op)
319         {
320                 OperationDepsNode *node = *it_op;
321                 IDDepsNode *id_node = node->owner->owner;
322                 ID *id = id_node->id;
323                 if (GS(id->name) == ID_OB) {
324                         Object *object = (Object *)id;
325                         object->customdata_mask |= node->customdata_mask;
326                 }
327         }
328 }
329
330 void DepsgraphRelationBuilder::build_group(Main *bmain,
331                                            Scene *scene,
332                                            Object *object,
333                                            Group *group)
334 {
335         ID *group_id = &group->id;
336         bool group_done = (group_id->tag & LIB_TAG_DOIT) != 0;
337         OperationKey object_local_transform_key(&object->id,
338                                                 DEPSNODE_TYPE_TRANSFORM,
339                                                 DEG_OPCODE_TRANSFORM_LOCAL);
340         for (GroupObject *go = (GroupObject *)group->gobject.first;
341              go != NULL;
342              go = go->next)
343         {
344                 if (!group_done) {
345                         build_object(bmain, scene, go->ob);
346                 }
347                 ComponentKey dupli_transform_key(&go->ob->id, DEPSNODE_TYPE_TRANSFORM);
348                 add_relation(dupli_transform_key,
349                              object_local_transform_key,
350                              DEPSREL_TYPE_TRANSFORM,
351                              "Dupligroup");
352         }
353         group_id->tag |= LIB_TAG_DOIT;
354 }
355
356 void DepsgraphRelationBuilder::build_object(Main *bmain, Scene *scene, Object *ob)
357 {
358         if (ob->id.tag & LIB_TAG_DOIT) {
359                 return;
360         }
361
362         /* Object Transforms */
363         eDepsOperation_Code base_op = (ob->parent) ? DEG_OPCODE_TRANSFORM_PARENT : DEG_OPCODE_TRANSFORM_LOCAL;
364         OperationKey base_op_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, base_op);
365
366         OperationKey local_transform_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_LOCAL);
367         OperationKey parent_transform_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_PARENT);
368         OperationKey final_transform_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
369
370         OperationKey ob_ubereval_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_OBJECT_UBEREVAL);
371
372         /* parenting */
373         if (ob->parent) {
374                 /* parent relationship */
375                 build_object_parent(ob);
376
377                 /* local -> parent */
378                 add_relation(local_transform_key, parent_transform_key, DEPSREL_TYPE_COMPONENT_ORDER, "[ObLocal -> ObParent]");
379         }
380
381         /* object constraints */
382         if (ob->constraints.first) {
383                 OperationKey constraint_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_CONSTRAINTS);
384
385                 /* constraint relations */
386                 // TODO: provide base op
387                 // XXX: this is broken
388                 build_constraints(scene, &ob->id, DEPSNODE_TYPE_TRANSFORM, "", &ob->constraints, NULL);
389
390                 /* operation order */
391                 add_relation(base_op_key, constraint_key, DEPSREL_TYPE_COMPONENT_ORDER, "[ObBase-> Constraint Stack]");
392                 add_relation(constraint_key, final_transform_key, DEPSREL_TYPE_COMPONENT_ORDER, "[ObConstraints -> Done]");
393
394                 // XXX
395                 add_relation(constraint_key, ob_ubereval_key, DEPSREL_TYPE_COMPONENT_ORDER, "Temp Ubereval");
396                 add_relation(ob_ubereval_key, final_transform_key, DEPSREL_TYPE_COMPONENT_ORDER, "Temp Ubereval");
397         }
398         else {
399                 /* operation order */
400                 add_relation(base_op_key, final_transform_key, DEPSREL_TYPE_COMPONENT_ORDER, "Object Transform");
401
402                 // XXX
403                 add_relation(base_op_key, ob_ubereval_key, DEPSREL_TYPE_COMPONENT_ORDER, "Temp Ubereval");
404                 add_relation(ob_ubereval_key, final_transform_key, DEPSREL_TYPE_COMPONENT_ORDER, "Temp Ubereval");
405         }
406
407
408         /* AnimData */
409         build_animdata(&ob->id);
410
411         // XXX: This should be hooked up by the build_animdata code
412         if (ob->adt && (ob->adt->action || ob->adt->nla_tracks.first)) {
413                 ComponentKey adt_key(&ob->id, DEPSNODE_TYPE_ANIMATION);
414                 add_relation(adt_key, local_transform_key, DEPSREL_TYPE_OPERATION, "Object Animation");
415         }
416
417
418         /* object data */
419         if (ob->data) {
420                 ID *obdata_id = (ID *)ob->data;
421
422                 /* ob data animation */
423                 build_animdata(obdata_id);
424
425                 /* type-specific data... */
426                 switch (ob->type) {
427                         case OB_MESH:     /* Geometry */
428                         case OB_CURVE:
429                         case OB_FONT:
430                         case OB_SURF:
431                         case OB_MBALL:
432                         case OB_LATTICE:
433                         {
434                                 build_obdata_geom(bmain, scene, ob);
435                                 break;
436                         }
437
438                         case OB_ARMATURE: /* Pose */
439                                 if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from != NULL) {
440                                         build_proxy_rig(ob);
441                                 }
442                                 else {
443                                         build_rig(scene, ob);
444                                 }
445                                 break;
446
447                         case OB_LAMP:   /* Lamp */
448                                 build_lamp(ob);
449                                 break;
450
451                         case OB_CAMERA: /* Camera */
452                                 build_camera(ob);
453                                 break;
454                 }
455
456                 Key *key = BKE_key_from_object(ob);
457                 if (key != NULL) {
458                         ComponentKey geometry_key((ID *)ob->data, DEPSNODE_TYPE_GEOMETRY);
459                         ComponentKey key_key(&key->id, DEPSNODE_TYPE_GEOMETRY);
460                         add_relation(key_key, geometry_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Shapekeys");
461                 }
462         }
463
464         /* particle systems */
465         if (ob->particlesystem.first) {
466                 build_particles(scene, ob);
467         }
468
469         /* grease pencil */
470         if (ob->gpd) {
471                 build_gpencil(&ob->id, ob->gpd);
472         }
473 }
474
475 void DepsgraphRelationBuilder::build_object_parent(Object *ob)
476 {
477         /* XXX: for now, need to use the component key (not just direct to the parent op), or else the matrix doesn't get reset */
478         // XXX: @sergey - it would be good if we got that backwards flushing working when tagging for updates
479         //OperationKey ob_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_PARENT);
480         ComponentKey ob_key(&ob->id, DEPSNODE_TYPE_TRANSFORM);
481
482         /* type-specific links */
483         switch (ob->partype) {
484                 case PARSKEL:  /* Armature Deform (Virtual Modifier) */
485                 {
486                         ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
487                         add_relation(parent_key, ob_key, DEPSREL_TYPE_STANDARD, "Armature Deform Parent");
488                         break;
489                 }
490
491                 case PARVERT1: /* Vertex Parent */
492                 case PARVERT3:
493                 {
494                         ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_GEOMETRY);
495                         add_relation(parent_key, ob_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Vertex Parent");
496
497                         /* XXX not sure what this is for or how you could be done properly - lukas */
498                         OperationDepsNode *parent_node = find_operation_node(parent_key);
499                         if (parent_node != NULL) {
500                                 parent_node->customdata_mask |= CD_MASK_ORIGINDEX;
501                         }
502
503                         ComponentKey transform_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
504                         add_relation(transform_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Vertex Parent TFM");
505                         break;
506                 }
507
508                 case PARBONE: /* Bone Parent */
509                 {
510                         ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_BONE, ob->parsubstr);
511                         add_relation(parent_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Bone Parent");
512                         break;
513                 }
514
515                 default:
516                 {
517                         if (ob->parent->type == OB_LATTICE) {
518                                 /* Lattice Deform Parent - Virtual Modifier */
519                                 // XXX: no virtual modifiers should be left!
520                                 ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
521                                 ComponentKey geom_key(&ob->parent->id, DEPSNODE_TYPE_GEOMETRY);
522
523                                 add_relation(parent_key, ob_key, DEPSREL_TYPE_STANDARD, "Lattice Deform Parent");
524                                 add_relation(geom_key, ob_key, DEPSREL_TYPE_STANDARD, "Lattice Deform Parent Geom");
525                         }
526                         else if (ob->parent->type == OB_CURVE) {
527                                 Curve *cu = (Curve *)ob->parent->data;
528
529                                 if (cu->flag & CU_PATH) {
530                                         /* Follow Path */
531                                         ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_GEOMETRY);
532                                         add_relation(parent_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Curve Follow Parent");
533
534                                         ComponentKey transform_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
535                                         add_relation(transform_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Curve Follow TFM");
536                                 }
537                                 else {
538                                         /* Standard Parent */
539                                         ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
540                                         add_relation(parent_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Curve Parent");
541                                 }
542                         }
543                         else {
544                                 /* Standard Parent */
545                                 ComponentKey parent_key(&ob->parent->id, DEPSNODE_TYPE_TRANSFORM);
546                                 add_relation(parent_key, ob_key, DEPSREL_TYPE_TRANSFORM, "Parent");
547                         }
548                         break;
549                 }
550         }
551
552         /* exception case: parent is duplivert */
553         if ((ob->type == OB_MBALL) && (ob->parent->transflag & OB_DUPLIVERTS)) {
554                 //dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_OB, "Duplivert");
555         }
556 }
557
558 void DepsgraphRelationBuilder::build_constraints(Scene *scene, ID *id, eDepsNode_Type component_type, const char *component_subdata,
559                                                  ListBase *constraints, RootPChanMap *root_map)
560 {
561         OperationKey constraint_op_key(id, component_type, component_subdata,
562                                        (component_type == DEPSNODE_TYPE_BONE) ? DEG_OPCODE_BONE_CONSTRAINTS : DEG_OPCODE_TRANSFORM_CONSTRAINTS);
563
564         /* add dependencies for each constraint in turn */
565         for (bConstraint *con = (bConstraint *)constraints->first; con; con = con->next) {
566                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
567
568                 /* invalid constraint type... */
569                 if (cti == NULL)
570                         continue;
571
572                 /* special case for camera tracking -- it doesn't use targets to define relations */
573                 // TODO: we can now represent dependencies in a much richer manner, so review how this is done...
574                 if (ELEM(cti->type, CONSTRAINT_TYPE_FOLLOWTRACK, CONSTRAINT_TYPE_CAMERASOLVER, CONSTRAINT_TYPE_OBJECTSOLVER)) {
575                         bool depends_on_camera = false;
576
577                         if (cti->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
578                                 bFollowTrackConstraint *data = (bFollowTrackConstraint *)con->data;
579
580                                 if (((data->clip) || (data->flag & FOLLOWTRACK_ACTIVECLIP)) && data->track[0])
581                                         depends_on_camera = true;
582
583                                 if (data->depth_ob) {
584                                         // DAG_RL_DATA_OB | DAG_RL_OB_OB
585                                         ComponentKey depth_key(&data->depth_ob->id, DEPSNODE_TYPE_TRANSFORM);
586                                         add_relation(depth_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
587                                 }
588                         }
589                         else if (cti->type == CONSTRAINT_TYPE_OBJECTSOLVER) {
590                                 depends_on_camera = true;
591                         }
592
593                         if (depends_on_camera && scene->camera) {
594                                 // DAG_RL_DATA_OB | DAG_RL_OB_OB
595                                 ComponentKey camera_key(&scene->camera->id, DEPSNODE_TYPE_TRANSFORM);
596                                 add_relation(camera_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
597                         }
598
599                         /* TODO(sergey): This is more a TimeSource -> MovieClip -> Constraint dependency chain. */
600                         TimeSourceKey time_src_key;
601                         add_relation(time_src_key, constraint_op_key, DEPSREL_TYPE_TIME, "[TimeSrc -> Animation]");
602                 }
603                 else if (cti->type == CONSTRAINT_TYPE_TRANSFORM_CACHE) {
604                         /* TODO(kevin): This is more a TimeSource -> CacheFile -> Constraint dependency chain. */
605                         TimeSourceKey time_src_key;
606                         add_relation(time_src_key, constraint_op_key, DEPSREL_TYPE_TIME, "[TimeSrc -> Animation]");
607
608                         bTransformCacheConstraint *data = (bTransformCacheConstraint *)con->data;
609
610                         if (data->cache_file) {
611                                 ComponentKey cache_key(&data->cache_file->id, DEPSNODE_TYPE_CACHE);
612                                 add_relation(cache_key, constraint_op_key, DEPSREL_TYPE_CACHE, cti->name);
613                         }
614                 }
615                 else if (cti->get_constraint_targets) {
616                         ListBase targets = {NULL, NULL};
617                         cti->get_constraint_targets(con, &targets);
618
619                         for (bConstraintTarget *ct = (bConstraintTarget *)targets.first; ct; ct = ct->next) {
620                                 if (!ct->tar)
621                                         continue;
622
623                                 if (ELEM(con->type, CONSTRAINT_TYPE_KINEMATIC, CONSTRAINT_TYPE_SPLINEIK)) {
624                                         /* ignore IK constraints - these are handled separately (on pose level) */
625                                 }
626                                 else if (ELEM(con->type, CONSTRAINT_TYPE_FOLLOWPATH, CONSTRAINT_TYPE_CLAMPTO)) {
627                                         /* these constraints require path geometry data... */
628                                         ComponentKey target_key(&ct->tar->id, DEPSNODE_TYPE_GEOMETRY);
629                                         add_relation(target_key, constraint_op_key, DEPSREL_TYPE_GEOMETRY_EVAL, cti->name); // XXX: type = geom_transform
630                                         // TODO: path dependency
631                                 }
632                                 else if ((ct->tar->type == OB_ARMATURE) && (ct->subtarget[0])) {
633                                         /* bone */
634                                         if (&ct->tar->id == id) {
635                                                 /* same armature  */
636                                                 eDepsOperation_Code target_key_opcode;
637
638                                                 /* Using "done" here breaks in-chain deps, while using "ready" here breaks most production rigs instead...
639                                                  * So, we do a compromise here, and only do this when an IK chain conflict may occur
640                                                  */
641                                                 if (root_map->has_common_root(component_subdata, ct->subtarget)) {
642                                                         target_key_opcode = DEG_OPCODE_BONE_READY;
643                                                 }
644                                                 else {
645                                                         target_key_opcode = DEG_OPCODE_BONE_DONE;
646                                                 }
647
648                                                 OperationKey target_key(&ct->tar->id, DEPSNODE_TYPE_BONE, ct->subtarget, target_key_opcode);
649                                                 add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
650                                         }
651                                         else {
652                                                 /* different armature - we can safely use the result of that */
653                                                 OperationKey target_key(&ct->tar->id, DEPSNODE_TYPE_BONE, ct->subtarget, DEG_OPCODE_BONE_DONE);
654                                                 add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
655                                         }
656                                 }
657                                 else if (ELEM(ct->tar->type, OB_MESH, OB_LATTICE) && (ct->subtarget[0])) {
658                                         /* vertex group */
659                                         /* NOTE: for now, we don't need to represent vertex groups separately... */
660                                         ComponentKey target_key(&ct->tar->id, DEPSNODE_TYPE_GEOMETRY);
661                                         add_relation(target_key, constraint_op_key, DEPSREL_TYPE_GEOMETRY_EVAL, cti->name);
662
663                                         if (ct->tar->type == OB_MESH) {
664                                                 OperationDepsNode *node2 = find_operation_node(target_key);
665                                                 if (node2 != NULL) {
666                                                         node2->customdata_mask |= CD_MASK_MDEFORMVERT;
667                                                 }
668                                         }
669                                 }
670                                 else if (con->type == CONSTRAINT_TYPE_SHRINKWRAP) {
671                                         /* Constraints which requires the target object surface. */
672                                         ComponentKey target_key(&ct->tar->id, DEPSNODE_TYPE_GEOMETRY);
673                                         add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
674
675                                         /* NOTE: obdata eval now doesn't necessarily depend on the object's transform... */
676                                         ComponentKey target_transform_key(&ct->tar->id, DEPSNODE_TYPE_TRANSFORM);
677                                         add_relation(target_transform_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
678                                 }
679                                 else {
680                                         /* standard object relation */
681                                         // TODO: loc vs rot vs scale?
682                                         if (&ct->tar->id == id) {
683                                                 /* Constraint targetting own object:
684                                                  * - This case is fine IFF we're dealing with a bone constraint pointing to
685                                                  *   its own armature. In that case, it's just transform -> bone.
686                                                  * - If however it is a real self targetting case, just make it depend on the
687                                                  *   previous constraint (or the pre-constraint state)...
688                                                  */
689                                                 if ((ct->tar->type == OB_ARMATURE) && (component_type == DEPSNODE_TYPE_BONE)) {
690                                                         OperationKey target_key(&ct->tar->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
691                                                         add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
692                                                 }
693                                                 else {
694                                                         OperationKey target_key(&ct->tar->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_LOCAL);
695                                                         add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
696                                                 }
697                                         }
698                                         else {
699                                                 /* normal object dependency */
700                                                 OperationKey target_key(&ct->tar->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
701                                                 add_relation(target_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
702                                         }
703                                 }
704
705                                 /* Constraints which needs world's matrix for transform.
706                                  * TODO(sergey): More constraints here?
707                                  */
708                                 if (ELEM(con->type,
709                                          CONSTRAINT_TYPE_ROTLIKE,
710                                          CONSTRAINT_TYPE_SIZELIKE,
711                                          CONSTRAINT_TYPE_LOCLIKE,
712                                          CONSTRAINT_TYPE_TRANSLIKE))
713                                 {
714                                         /* TODO(sergey): Add used space check. */
715                                         ComponentKey target_transform_key(&ct->tar->id, DEPSNODE_TYPE_TRANSFORM);
716                                         add_relation(target_transform_key, constraint_op_key, DEPSREL_TYPE_TRANSFORM, cti->name);
717                                 }
718
719                         }
720
721                         if (cti->flush_constraint_targets)
722                                 cti->flush_constraint_targets(con, &targets, 1);
723                 }
724         }
725 }
726
727 void DepsgraphRelationBuilder::build_animdata(ID *id)
728 {
729         AnimData *adt = BKE_animdata_from_id(id);
730
731         if (adt == NULL)
732                 return;
733
734         ComponentKey adt_key(id, DEPSNODE_TYPE_ANIMATION);
735
736         /* animation */
737         if (adt->action || adt->nla_tracks.first) {
738                 /* wire up dependency to time source */
739                 TimeSourceKey time_src_key;
740                 add_relation(time_src_key, adt_key, DEPSREL_TYPE_TIME, "[TimeSrc -> Animation]");
741
742                 // XXX: Hook up specific update callbacks for special properties which may need it...
743
744                 // XXX: animdata "hierarchy" - top-level overrides need to go after lower-down
745         }
746
747         /* drivers */
748         for (FCurve *fcu = (FCurve *)adt->drivers.first; fcu; fcu = fcu->next) {
749                 OperationKey driver_key(id, DEPSNODE_TYPE_PARAMETERS, DEG_OPCODE_DRIVER, deg_fcurve_id_name(fcu));
750
751                 /* create the driver's relations to targets */
752                 build_driver(id, fcu);
753
754                 /* prevent driver from occurring before own animation... */
755                 if (adt->action || adt->nla_tracks.first) {
756                         add_relation(adt_key, driver_key, DEPSREL_TYPE_OPERATION,
757                                      "[AnimData Before Drivers]");
758                 }
759         }
760 }
761
762 void DepsgraphRelationBuilder::build_driver(ID *id, FCurve *fcu)
763 {
764         ChannelDriver *driver = fcu->driver;
765         OperationKey driver_key(id, DEPSNODE_TYPE_PARAMETERS, DEG_OPCODE_DRIVER, deg_fcurve_id_name(fcu));
766         bPoseChannel *pchan = NULL;
767
768         /* create dependency between driver and data affected by it */
769         /* - direct property relationship... */
770         //RNAPathKey affected_key(id, fcu->rna_path);
771         //add_relation(driver_key, affected_key, DEPSREL_TYPE_DRIVER, "[Driver -> Data] DepsRel");
772
773         /* driver -> data components (for interleaved evaluation - bones/constraints/modifiers) */
774         // XXX: this probably should probably be moved out into a separate function
775         if (strstr(fcu->rna_path, "pose.bones[") != NULL) {
776                 /* interleaved drivers during bone eval */
777                 // TODO: ideally, if this is for a constraint, it goes to said constraint
778                 Object *ob = (Object *)id;
779                 char *bone_name;
780
781                 bone_name = BLI_str_quoted_substrN(fcu->rna_path, "pose.bones[");
782                 pchan = BKE_pose_channel_find_name(ob->pose, bone_name);
783
784                 if (bone_name) {
785                         MEM_freeN(bone_name);
786                         bone_name = NULL;
787                 }
788
789                 if (pchan) {
790                         OperationKey bone_key(id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_LOCAL);
791                         add_relation(driver_key, bone_key, DEPSREL_TYPE_DRIVER, "[Driver -> Bone]");
792                 }
793                 else {
794                         fprintf(stderr,
795                                 "Couldn't find bone name for driver path - '%s'\n",
796                                 fcu->rna_path);
797                 }
798         }
799         else if (GS(id->name) == ID_AR && strstr(fcu->rna_path, "bones[")) {
800                 /* drivers on armature-level bone settings (i.e. bbone stuff),
801                  * which will affect the evaluation of corresponding pose bones
802                  */
803                 IDDepsNode *arm_node = m_graph->find_id_node(id);
804                 char *bone_name = BLI_str_quoted_substrN(fcu->rna_path, "bones[");
805
806                 if (arm_node && bone_name) {
807                         /* find objects which use this, and make their eval callbacks depend on this */
808                         foreach (DepsRelation *rel, arm_node->outlinks) {
809                                 IDDepsNode *to_node = (IDDepsNode *)rel->to;
810
811                                 /* we only care about objects with pose data which use this... */
812                                 if (GS(to_node->id->name) == ID_OB) {
813                                         Object *ob = (Object *)to_node->id;
814                                         bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, bone_name); // NOTE: ob->pose may be NULL
815
816                                         if (pchan) {
817                                                 OperationKey bone_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_LOCAL);
818                                                 add_relation(driver_key, bone_key, DEPSREL_TYPE_DRIVER, "[Arm Bone -> Driver -> Bone]");
819                                         }
820                                 }
821                         }
822
823                         /* free temp data */
824                         MEM_freeN(bone_name);
825                         bone_name = NULL;
826                 }
827                 else {
828                         fprintf(stderr,
829                                 "Couldn't find armature bone name for driver path - '%s'\n",
830                                 fcu->rna_path);
831                 }
832         }
833         else if (GS(id->name) == ID_OB && strstr(fcu->rna_path, "modifiers[")) {
834                 /* modifier driver - connect directly to the modifier */
835                 char *modifier_name = BLI_str_quoted_substrN(fcu->rna_path, "modifiers[");
836                 if (modifier_name) {
837                         OperationKey modifier_key(id,
838                                                   DEPSNODE_TYPE_GEOMETRY,
839                                                   DEG_OPCODE_GEOMETRY_MODIFIER,
840                                                   modifier_name);
841                         if (has_node(modifier_key)) {
842                                 add_relation(driver_key, modifier_key, DEPSREL_TYPE_DRIVER, "[Driver -> Modifier]");
843                         }
844                         else {
845                                 printf("Unexisting driver RNA path: %s\n", fcu->rna_path);
846                         }
847
848                         MEM_freeN(modifier_name);
849                 }
850         }
851         else if (GS(id->name) == ID_KE && strstr(fcu->rna_path, "key_blocks[")) {
852                 /* shape key driver - hook into the base geometry operation */
853                 // XXX: double check where this points
854                 Key *shape_key = (Key *)id;
855
856                 ComponentKey geometry_key(shape_key->from, DEPSNODE_TYPE_GEOMETRY);
857                 add_relation(driver_key, geometry_key, DEPSREL_TYPE_DRIVER, "[Driver -> ShapeKey Geom]");
858         }
859         else if (strstr(fcu->rna_path, "key_blocks[")) {
860                 ComponentKey geometry_key(id, DEPSNODE_TYPE_GEOMETRY);
861                 add_relation(driver_key, geometry_key, DEPSREL_TYPE_DRIVER, "[Driver -> ShapeKey Geom]");
862         }
863         else {
864                 if (GS(id->name) == ID_OB) {
865                         /* assume that driver affects a transform... */
866                         OperationKey local_transform_key(id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_LOCAL);
867                         add_relation(driver_key, local_transform_key, DEPSREL_TYPE_OPERATION, "[Driver -> Transform]");
868                 }
869                 else if (GS(id->name) == ID_KE) {
870                         ComponentKey geometry_key(id, DEPSNODE_TYPE_GEOMETRY);
871                         add_relation(driver_key, geometry_key, DEPSREL_TYPE_GEOMETRY_EVAL, "[Driver -> Shapekey Geometry]");
872                 }
873         }
874
875         /* ensure that affected prop's update callbacks will be triggered once done */
876         // TODO: implement this once the functionality to add these links exists in RNA
877         // XXX: the data itself could also set this, if it were to be truly initialised later?
878
879         /* loop over variables to get the target relationships */
880         for (DriverVar *dvar = (DriverVar *)driver->variables.first; dvar; dvar = dvar->next) {
881                 /* only used targets */
882                 DRIVER_TARGETS_USED_LOOPER(dvar)
883                 {
884                         if (dtar->id == NULL)
885                                 continue;
886
887                         /* special handling for directly-named bones */
888                         if ((dtar->flag & DTAR_FLAG_STRUCT_REF) && (dtar->pchan_name[0])) {
889                                 Object *ob = (Object *)dtar->id;
890                                 bPoseChannel *target_pchan = BKE_pose_channel_find_name(ob->pose, dtar->pchan_name);
891                                 if (target_pchan != NULL) {
892                                         /* get node associated with bone */
893                                         // XXX: watch the space!
894                                         /* Some cases can't use final bone transform, for example:
895                                          * - Driving the bone with itself (addressed here)
896                                          * - Relations inside an IK chain (TODO?)
897                                          */
898                                         if (dtar->id == id &&
899                                             pchan != NULL &&
900                                             STREQ(pchan->name, target_pchan->name))
901                                         {
902                                                 continue;
903                                         }
904                                         OperationKey target_key(dtar->id, DEPSNODE_TYPE_BONE, target_pchan->name, DEG_OPCODE_BONE_DONE);
905                                         add_relation(target_key, driver_key, DEPSREL_TYPE_DRIVER_TARGET, "[Bone Target -> Driver]");
906                                 }
907                         }
908                         else if (dtar->flag & DTAR_FLAG_STRUCT_REF) {
909                                 /* get node associated with the object's transforms */
910                                 OperationKey target_key(dtar->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
911                                 add_relation(target_key, driver_key, DEPSREL_TYPE_DRIVER_TARGET, "[Target -> Driver]");
912                         }
913                         else if (dtar->rna_path && strstr(dtar->rna_path, "pose.bones[")) {
914                                 /* workaround for ensuring that local bone transforms don't end up
915                                  * having to wait for pose eval to finish (to prevent cycles)
916                                  */
917                                 Object *ob = (Object *)dtar->id;
918                                 char *bone_name = BLI_str_quoted_substrN(dtar->rna_path, "pose.bones[");
919                                 bPoseChannel *target_pchan = BKE_pose_channel_find_name(ob->pose, bone_name);
920                                 if (bone_name) {
921                                         MEM_freeN(bone_name);
922                                         bone_name = NULL;
923                                 }
924                                 if (target_pchan) {
925                                         if (dtar->id == id &&
926                                             pchan != NULL &&
927                                             STREQ(pchan->name, target_pchan->name))
928                                         {
929                                                 continue;
930                                         }
931                                         OperationKey bone_key(dtar->id, DEPSNODE_TYPE_BONE, target_pchan->name, DEG_OPCODE_BONE_LOCAL);
932                                         add_relation(bone_key, driver_key, DEPSREL_TYPE_DRIVER, "[RNA Bone -> Driver]");
933                                 }
934                         }
935                         else {
936                                 if (dtar->id == id) {
937                                         /* Ignore input dependency if we're driving properties of the same ID,
938                                          * otherwise we'll be ending up in a cyclic dependency here.
939                                          */
940                                         continue;
941                                 }
942                                 /* resolve path to get node */
943                                 RNAPathKey target_key(dtar->id, dtar->rna_path ? dtar->rna_path : "");
944                                 add_relation(target_key, driver_key, DEPSREL_TYPE_DRIVER_TARGET, "[RNA Target -> Driver]");
945                         }
946                 }
947                 DRIVER_TARGETS_LOOPER_END
948         }
949
950         /* It's quite tricky to detect if the driver actually depends on time or not,
951          * so for now we'll be quite conservative here about optimization and consider
952          * all python drivers to be depending on time.
953          */
954         if (driver->type == DRIVER_TYPE_PYTHON) {
955                 TimeSourceKey time_src_key;
956                 add_relation(time_src_key, driver_key, DEPSREL_TYPE_TIME, "[TimeSrc -> Driver]");
957         }
958 }
959
960 void DepsgraphRelationBuilder::build_world(World *world)
961 {
962         ID *world_id = &world->id;
963         if (world_id->tag & LIB_TAG_DOIT) {
964                 return;
965         }
966         world_id->tag |= LIB_TAG_DOIT;
967
968         build_animdata(world_id);
969
970         /* TODO: other settings? */
971
972         /* textures */
973         build_texture_stack(world_id, world->mtex);
974
975         /* world's nodetree */
976         build_nodetree(world_id, world->nodetree);
977 }
978
979 void DepsgraphRelationBuilder::build_rigidbody(Scene *scene)
980 {
981         RigidBodyWorld *rbw = scene->rigidbody_world;
982
983         OperationKey init_key(&scene->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_REBUILD);
984         OperationKey sim_key(&scene->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_SIM);
985
986         /* rel between the two sim-nodes */
987         add_relation(init_key, sim_key, DEPSREL_TYPE_OPERATION, "Rigidbody [Init -> SimStep]");
988
989         /* set up dependencies between these operations and other builtin nodes --------------- */
990
991         /* time dependency */
992         TimeSourceKey time_src_key;
993         add_relation(time_src_key, init_key, DEPSREL_TYPE_TIME, "TimeSrc -> Rigidbody Reset/Rebuild (Optional)");
994         add_relation(time_src_key, sim_key, DEPSREL_TYPE_TIME, "TimeSrc -> Rigidbody Sim Step");
995
996         /* objects - simulation participants */
997         if (rbw->group) {
998                 for (GroupObject *go = (GroupObject *)rbw->group->gobject.first; go; go = go->next) {
999                         Object *ob = go->ob;
1000                         if (!ob || ob->type != OB_MESH)
1001                                 continue;
1002
1003                         /* hook up evaluation order...
1004                          * 1) flushing rigidbody results follows base transforms being applied
1005                          * 2) rigidbody flushing can only be performed after simulation has been run
1006                          *
1007                          * 3) simulation needs to know base transforms to figure out what to do
1008                          *    XXX: there's probably a difference between passive and active
1009                          *         - passive don't change, so may need to know full transform...
1010                          */
1011                         OperationKey rbo_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_RIGIDBODY);
1012
1013                         eDepsOperation_Code trans_opcode = ob->parent ? DEG_OPCODE_TRANSFORM_PARENT : DEG_OPCODE_TRANSFORM_LOCAL;
1014                         OperationKey trans_op(&ob->id, DEPSNODE_TYPE_TRANSFORM, trans_opcode);
1015
1016                         add_relation(trans_op, rbo_key, DEPSREL_TYPE_OPERATION, "Base Ob Transform -> RBO Sync");
1017                         add_relation(sim_key, rbo_key, DEPSREL_TYPE_COMPONENT_ORDER, "Rigidbody Sim Eval -> RBO Sync");
1018
1019                         /* if constraints exist, those depend on the result of the rigidbody sim
1020                          * - This allows constraints to modify the result of the sim (i.e. clamping)
1021                          *   while still allowing the sim to depend on some changes to the objects.
1022                          *   Also, since constraints are hooked up to the final nodes, this link
1023                          *   means that we can also fit in there too...
1024                          * - Later, it might be good to include a constraint in the stack allowing us
1025                          *   to control whether rigidbody eval gets interleaved into the constraint stack
1026                          */
1027                         if (ob->constraints.first) {
1028                                 OperationKey constraint_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_CONSTRAINTS);
1029                                 add_relation(rbo_key, constraint_key, DEPSREL_TYPE_COMPONENT_ORDER, "RBO Sync -> Ob Constraints");
1030                         }
1031                         else {
1032                                 /* final object transform depends on rigidbody */
1033                                 OperationKey done_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_FINAL);
1034                                 add_relation(rbo_key, done_key, DEPSREL_TYPE_COMPONENT_ORDER, "RBO Sync -> Done");
1035
1036                                 // XXX: ubereval will be removed eventually, but we still need it in the meantime
1037                                 OperationKey uber_key(&ob->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_OBJECT_UBEREVAL);
1038                                 add_relation(rbo_key, uber_key, DEPSREL_TYPE_COMPONENT_ORDER, "RBO Sync -> Uber (Temp)");
1039                         }
1040
1041
1042                         /* needed to get correct base values */
1043                         add_relation(trans_op, sim_key, DEPSREL_TYPE_OPERATION, "Base Ob Transform -> Rigidbody Sim Eval");
1044                 }
1045         }
1046
1047         /* constraints */
1048         if (rbw->constraints) {
1049                 for (GroupObject *go = (GroupObject *)rbw->constraints->gobject.first; go; go = go->next) {
1050                         Object *ob = go->ob;
1051                         if (!ob || !ob->rigidbody_constraint)
1052                                 continue;
1053
1054                         RigidBodyCon *rbc = ob->rigidbody_constraint;
1055
1056                         /* final result of the constraint object's transform controls how the
1057                          * constraint affects the physics sim for these objects
1058                          */
1059                         ComponentKey trans_key(&ob->id, DEPSNODE_TYPE_TRANSFORM);
1060                         OperationKey ob1_key(&rbc->ob1->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_RIGIDBODY);
1061                         OperationKey ob2_key(&rbc->ob2->id, DEPSNODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_RIGIDBODY);
1062
1063                         /* - constrained-objects sync depends on the constraint-holder */
1064                         add_relation(trans_key, ob1_key, DEPSREL_TYPE_TRANSFORM, "RigidBodyConstraint -> RBC.Object_1");
1065                         add_relation(trans_key, ob2_key, DEPSREL_TYPE_TRANSFORM, "RigidBodyConstraint -> RBC.Object_2");
1066
1067                         /* - ensure that sim depends on this constraint's transform */
1068                         add_relation(trans_key, sim_key, DEPSREL_TYPE_TRANSFORM, "RigidBodyConstraint Transform -> RB Simulation");
1069                 }
1070         }
1071 }
1072
1073 void DepsgraphRelationBuilder::build_particles(Scene *scene, Object *ob)
1074 {
1075         TimeSourceKey time_src_key;
1076         OperationKey obdata_ubereval_key(&ob->id,
1077                                          DEPSNODE_TYPE_GEOMETRY,
1078                                          DEG_OPCODE_GEOMETRY_UBEREVAL);
1079
1080         /* particle systems */
1081         for (ParticleSystem *psys = (ParticleSystem *)ob->particlesystem.first; psys; psys = psys->next) {
1082                 ParticleSettings *part = psys->part;
1083
1084                 /* particle settings */
1085                 build_animdata(&part->id);
1086
1087                 /* this particle system */
1088                 OperationKey psys_key(&ob->id, DEPSNODE_TYPE_EVAL_PARTICLES, DEG_OPCODE_PSYS_EVAL, psys->name);
1089
1090                 /* XXX: if particle system is later re-enabled, we must do full rebuild? */
1091                 if (!psys_check_enabled(ob, psys, G.is_rendering))
1092                         continue;
1093
1094                 /* TODO(sergey): Are all particle systems depends on time?
1095                  * Hair without dynamics i.e.
1096                  */
1097                 add_relation(time_src_key, psys_key,
1098                              DEPSREL_TYPE_TIME,
1099                              "TimeSrc -> PSys");
1100
1101                 /* TODO(sergey): Currently particle update is just a placeholder,
1102                  * hook it to the ubereval node so particle system is getting updated
1103                  * on playback.
1104                  */
1105                 add_relation(psys_key,
1106                              obdata_ubereval_key,
1107                              DEPSREL_TYPE_OPERATION,
1108                              "PSys -> UberEval");
1109
1110 #if 0
1111                 if (ELEM(part->phystype, PART_PHYS_KEYED, PART_PHYS_BOIDS)) {
1112                         ParticleTarget *pt;
1113
1114                         for (pt = psys->targets.first; pt; pt = pt->next) {
1115                                 if (pt->ob && BLI_findlink(&pt->ob->particlesystem, pt->psys - 1)) {
1116                                         node2 = dag_get_node(dag, pt->ob);
1117                                         dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Particle Targets");
1118                                 }
1119                         }
1120                 }
1121
1122                 if (part->ren_as == PART_DRAW_OB && part->dup_ob) {
1123                         node2 = dag_get_node(dag, part->dup_ob);
1124                         /* note that this relation actually runs in the wrong direction, the problem
1125                          * is that dupli system all have this (due to parenting), and the render
1126                          * engine instancing assumes particular ordering of objects in list */
1127                         dag_add_relation(dag, node, node2, DAG_RL_OB_OB, "Particle Object Visualization");
1128                         if (part->dup_ob->type == OB_MBALL)
1129                                 dag_add_relation(dag, node, node2, DAG_RL_DATA_DATA, "Particle Object Visualization");
1130                 }
1131
1132                 if (part->ren_as == PART_DRAW_GR && part->dup_group) {
1133                         for (go = part->dup_group->gobject.first; go; go = go->next) {
1134                                 node2 = dag_get_node(dag, go->ob);
1135                                 dag_add_relation(dag, node2, node, DAG_RL_OB_OB, "Particle Group Visualization");
1136                         }
1137                 }
1138 #endif
1139
1140                 /* effectors */
1141                 ListBase *effectors = pdInitEffectors(scene, ob, psys, part->effector_weights, false);
1142
1143                 if (effectors) {
1144                         for (EffectorCache *eff = (EffectorCache *)effectors->first; eff; eff = eff->next) {
1145                                 if (eff->psys) {
1146                                         // XXX: DAG_RL_DATA_DATA | DAG_RL_OB_DATA
1147                                         ComponentKey eff_key(&eff->ob->id, DEPSNODE_TYPE_GEOMETRY); // xxx: particles instead?
1148                                         add_relation(eff_key, psys_key, DEPSREL_TYPE_STANDARD, "Particle Field");
1149                                 }
1150                         }
1151                 }
1152
1153                 pdEndEffectors(&effectors);
1154
1155                 /* boids */
1156                 if (part->boids) {
1157                         BoidRule *rule = NULL;
1158                         BoidState *state = NULL;
1159
1160                         for (state = (BoidState *)part->boids->states.first; state; state = state->next) {
1161                                 for (rule = (BoidRule *)state->rules.first; rule; rule = rule->next) {
1162                                         Object *ruleob = NULL;
1163                                         if (rule->type == eBoidRuleType_Avoid)
1164                                                 ruleob = ((BoidRuleGoalAvoid *)rule)->ob;
1165                                         else if (rule->type == eBoidRuleType_FollowLeader)
1166                                                 ruleob = ((BoidRuleFollowLeader *)rule)->ob;
1167
1168                                         if (ruleob) {
1169                                                 ComponentKey ruleob_key(&ruleob->id, DEPSNODE_TYPE_TRANSFORM);
1170                                                 add_relation(ruleob_key, psys_key, DEPSREL_TYPE_TRANSFORM, "Boid Rule");
1171                                         }
1172                                 }
1173                         }
1174                 }
1175
1176                 if (part->ren_as == PART_DRAW_OB && part->dup_ob) {
1177                         ComponentKey dup_ob_key(&part->dup_ob->id, DEPSNODE_TYPE_TRANSFORM);
1178                         add_relation(dup_ob_key,
1179                                      psys_key,
1180                                      DEPSREL_TYPE_TRANSFORM,
1181                                      "Particle Object Visualization");
1182                 }
1183         }
1184
1185         /* Particle depends on the object transform, so that channel is to be ready
1186          * first.
1187          *
1188          * TODO(sergey): This relation should be altered once real granular update
1189          * is implemented.
1190          */
1191         ComponentKey transform_key(&ob->id, DEPSNODE_TYPE_TRANSFORM);
1192         add_relation(transform_key,
1193                      obdata_ubereval_key,
1194                      DEPSREL_TYPE_GEOMETRY_EVAL,
1195                      "Partcile Eval");
1196
1197         /* pointcache */
1198         // TODO...
1199 }
1200
1201 /* IK Solver Eval Steps */
1202 void DepsgraphRelationBuilder::build_ik_pose(Object *ob,
1203                                              bPoseChannel *pchan,
1204                                              bConstraint *con,
1205                                              RootPChanMap *root_map)
1206 {
1207         bKinematicConstraint *data = (bKinematicConstraint *)con->data;
1208
1209         /* attach owner to IK Solver too
1210          * - assume that owner is always part of chain
1211          * - see notes on direction of rel below...
1212          */
1213         bPoseChannel *rootchan = BKE_armature_ik_solver_find_root(pchan, data);
1214         OperationKey solver_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name, DEG_OPCODE_POSE_IK_SOLVER);
1215
1216         /* IK target */
1217         // XXX: this should get handled as part of the constraint code
1218         if (data->tar != NULL) {
1219                 /* TODO(sergey): For until we'll store partial matricies in the depsgraph,
1220                  * we create dependency between target object and pose eval component.
1221                  *
1222                  * This way we ensuring the whole subtree is updated from scratch without
1223                  * need of intermediate matricies. This is an overkill, but good enough for
1224                  * testing IK solver.
1225                  */
1226                 // FIXME: geometry targets...
1227                 ComponentKey pose_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE);
1228                 if ((data->tar->type == OB_ARMATURE) && (data->subtarget[0])) {
1229                         /* TODO(sergey): This is only for until granular update stores intermediate result. */
1230                         if (data->tar != ob) {
1231                                 /* different armature - can just read the results */
1232                                 ComponentKey target_key(&data->tar->id, DEPSNODE_TYPE_BONE, data->subtarget);
1233                                 add_relation(target_key, pose_key, DEPSREL_TYPE_TRANSFORM, con->name);
1234                         }
1235                         else {
1236                                 /* same armature - we'll use the ready state only, just in case this bone is in the chain we're solving */
1237                                 OperationKey target_key(&data->tar->id, DEPSNODE_TYPE_BONE, data->subtarget, DEG_OPCODE_BONE_DONE);
1238                                 add_relation(target_key, solver_key, DEPSREL_TYPE_TRANSFORM, con->name);
1239                         }
1240                 }
1241                 else if (ELEM(data->tar->type, OB_MESH, OB_LATTICE) && (data->subtarget[0])) {
1242                         /* vertex group target */
1243                         /* NOTE: for now, we don't need to represent vertex groups separately... */
1244                         ComponentKey target_key(&data->tar->id, DEPSNODE_TYPE_GEOMETRY);
1245                         add_relation(target_key, solver_key, DEPSREL_TYPE_GEOMETRY_EVAL, con->name);
1246
1247                         if (data->tar->type == OB_MESH) {
1248                                 OperationDepsNode *node2 = find_operation_node(target_key);
1249                                 if (node2 != NULL) {
1250                                         node2->customdata_mask |= CD_MASK_MDEFORMVERT;
1251                                 }
1252                         }
1253                 }
1254                 else {
1255                         /* Standard Object Target */
1256                         ComponentKey target_key(&data->tar->id, DEPSNODE_TYPE_TRANSFORM);
1257                         add_relation(target_key, pose_key, DEPSREL_TYPE_TRANSFORM, con->name);
1258                 }
1259
1260                 if ((data->tar == ob) && (data->subtarget[0])) {
1261                         /* Prevent target's constraints from linking to anything from same
1262                          * chain that it controls.
1263                          */
1264                         root_map->add_bone(data->subtarget, rootchan->name);
1265                 }
1266         }
1267
1268         /* Pole Target */
1269         // XXX: this should get handled as part of the constraint code
1270         if (data->poletar != NULL) {
1271                 if ((data->poletar->type == OB_ARMATURE) && (data->polesubtarget[0])) {
1272                         // XXX: same armature issues - ready vs done?
1273                         ComponentKey target_key(&data->poletar->id, DEPSNODE_TYPE_BONE, data->subtarget);
1274                         add_relation(target_key, solver_key, DEPSREL_TYPE_TRANSFORM, con->name);
1275                 }
1276                 else if (ELEM(data->poletar->type, OB_MESH, OB_LATTICE) && (data->subtarget[0])) {
1277                         /* vertex group target */
1278                         /* NOTE: for now, we don't need to represent vertex groups separately... */
1279                         ComponentKey target_key(&data->poletar->id, DEPSNODE_TYPE_GEOMETRY);
1280                         add_relation(target_key, solver_key, DEPSREL_TYPE_GEOMETRY_EVAL, con->name);
1281
1282                         if (data->poletar->type == OB_MESH) {
1283                                 OperationDepsNode *node2 = find_operation_node(target_key);
1284                                 if (node2 != NULL) {
1285                                         node2->customdata_mask |= CD_MASK_MDEFORMVERT;
1286                                 }
1287                         }
1288                 }
1289                 else {
1290                         ComponentKey target_key(&data->poletar->id, DEPSNODE_TYPE_TRANSFORM);
1291                         add_relation(target_key, solver_key, DEPSREL_TYPE_TRANSFORM, con->name);
1292                 }
1293         }
1294
1295         DEG_DEBUG_PRINTF("\nStarting IK Build: pchan = %s, target = (%s, %s), segcount = %d\n",
1296                          pchan->name, data->tar->id.name, data->subtarget, data->rootbone);
1297
1298         bPoseChannel *parchan = pchan;
1299         /* exclude tip from chain? */
1300         if (!(data->flag & CONSTRAINT_IK_TIP)) {
1301                 OperationKey tip_transforms_key(&ob->id, DEPSNODE_TYPE_BONE,
1302                                                 parchan->name, DEG_OPCODE_BONE_LOCAL);
1303                 add_relation(solver_key, tip_transforms_key,
1304                              DEPSREL_TYPE_TRANSFORM, "IK Solver Result");
1305                 parchan = pchan->parent;
1306         }
1307
1308         root_map->add_bone(parchan->name, rootchan->name);
1309
1310         OperationKey parchan_transforms_key(&ob->id, DEPSNODE_TYPE_BONE,
1311                                             parchan->name, DEG_OPCODE_BONE_READY);
1312         add_relation(parchan_transforms_key, solver_key,
1313                      DEPSREL_TYPE_TRANSFORM, "IK Solver Owner");
1314
1315         /* Walk to the chain's root */
1316         //size_t segcount = 0;
1317         int segcount = 0;
1318
1319         while (parchan) {
1320                 /* Make IK-solver dependent on this bone's result,
1321                  * since it can only run after the standard results
1322                  * of the bone are know. Validate links step on the
1323                  * bone will ensure that users of this bone only
1324                  * grab the result with IK solver results...
1325                  */
1326                 if (parchan != pchan) {
1327                         OperationKey parent_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_READY);
1328                         add_relation(parent_key, solver_key, DEPSREL_TYPE_TRANSFORM, "IK Chain Parent");
1329
1330                         OperationKey done_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_DONE);
1331                         add_relation(solver_key, done_key, DEPSREL_TYPE_TRANSFORM, "IK Chain Result");
1332                 }
1333                 else {
1334                         OperationKey final_transforms_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_DONE);
1335                         add_relation(solver_key, final_transforms_key, DEPSREL_TYPE_TRANSFORM, "IK Solver Result");
1336                 }
1337                 parchan->flag |= POSE_DONE;
1338
1339
1340                 root_map->add_bone(parchan->name, rootchan->name);
1341
1342                 /* continue up chain, until we reach target number of items... */
1343                 DEG_DEBUG_PRINTF("  %d = %s\n", segcount, parchan->name);
1344                 segcount++;
1345                 if ((segcount == data->rootbone) || (segcount > 255)) break;  /* 255 is weak */
1346
1347                 parchan  = parchan->parent;
1348         }
1349
1350         OperationKey flush_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_DONE);
1351         add_relation(solver_key, flush_key, DEPSREL_TYPE_OPERATION, "PoseEval Result-Bone Link");
1352 }
1353
1354 /* Spline IK Eval Steps */
1355 void DepsgraphRelationBuilder::build_splineik_pose(Object *ob,
1356                                                    bPoseChannel *pchan,
1357                                                    bConstraint *con,
1358                                                    RootPChanMap *root_map)
1359 {
1360         bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
1361         bPoseChannel *rootchan = BKE_armature_splineik_solver_find_root(pchan, data);
1362         OperationKey transforms_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_READY);
1363         OperationKey solver_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, rootchan->name, DEG_OPCODE_POSE_SPLINE_IK_SOLVER);
1364
1365         /* attach owner to IK Solver too
1366          * - assume that owner is always part of chain
1367          * - see notes on direction of rel below...
1368          */
1369         add_relation(transforms_key, solver_key, DEPSREL_TYPE_TRANSFORM, "Spline IK Solver Owner");
1370
1371         /* attach path dependency to solver */
1372         if (data->tar) {
1373                 /* TODO(sergey): For until we'll store partial matricies in the depsgraph,
1374                  * we create dependency between target object and pose eval component.
1375                  * See IK pose for a bit more information.
1376                  */
1377                 // TODO: the bigggest point here is that we need the curve PATH and not just the general geometry...
1378                 ComponentKey target_key(&data->tar->id, DEPSNODE_TYPE_GEOMETRY);
1379                 ComponentKey pose_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE);
1380                 add_relation(target_key, pose_key, DEPSREL_TYPE_TRANSFORM, "[Curve.Path -> Spline IK] DepsRel");
1381         }
1382
1383         pchan->flag |= POSE_DONE;
1384         OperationKey final_transforms_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_DONE);
1385         add_relation(solver_key, final_transforms_key, DEPSREL_TYPE_TRANSFORM, "Spline IK Result");
1386
1387         root_map->add_bone(pchan->name, rootchan->name);
1388
1389         /* Walk to the chain's root */
1390         //size_t segcount = 0;
1391         int segcount = 0;
1392
1393         for (bPoseChannel *parchan = pchan->parent; parchan; parchan = parchan->parent) {
1394                 /* Make Spline IK solver dependent on this bone's result,
1395                  * since it can only run after the standard results
1396                  * of the bone are know. Validate links step on the
1397                  * bone will ensure that users of this bone only
1398                  * grab the result with IK solver results...
1399                  */
1400                 if (parchan != pchan) {
1401                         OperationKey parent_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_READY);
1402                         add_relation(parent_key, solver_key, DEPSREL_TYPE_TRANSFORM, "Spline IK Solver Update");
1403
1404                         OperationKey done_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_DONE);
1405                         add_relation(solver_key, done_key, DEPSREL_TYPE_TRANSFORM, "IK Chain Result");
1406                 }
1407                 parchan->flag |= POSE_DONE;
1408
1409                 OperationKey final_transforms_key(&ob->id, DEPSNODE_TYPE_BONE, parchan->name, DEG_OPCODE_BONE_DONE);
1410                 add_relation(solver_key, final_transforms_key, DEPSREL_TYPE_TRANSFORM, "Spline IK Solver Result");
1411
1412                 root_map->add_bone(parchan->name, rootchan->name);
1413
1414                 /* continue up chain, until we reach target number of items... */
1415                 segcount++;
1416                 if ((segcount == data->chainlen) || (segcount > 255)) break;  /* 255 is weak */
1417         }
1418
1419         OperationKey flush_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_DONE);
1420         add_relation(solver_key, flush_key, DEPSREL_TYPE_OPERATION, "PoseEval Result-Bone Link");
1421 }
1422
1423 /* Pose/Armature Bones Graph */
1424 void DepsgraphRelationBuilder::build_rig(Scene *scene, Object *ob)
1425 {
1426         /* Armature-Data */
1427         bArmature *arm = (bArmature *)ob->data;
1428
1429         // TODO: selection status?
1430
1431         /* attach links between pose operations */
1432         OperationKey init_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_INIT);
1433         OperationKey flush_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_DONE);
1434
1435         add_relation(init_key, flush_key, DEPSREL_TYPE_COMPONENT_ORDER, "[Pose Init -> Pose Cleanup]");
1436
1437         /* Make sure pose is up-to-date with armature updates. */
1438         OperationKey armature_key(&arm->id,
1439                                   DEPSNODE_TYPE_PARAMETERS,
1440                                   DEG_OPCODE_PLACEHOLDER,
1441                                   "Armature Eval");
1442         add_relation(armature_key, init_key, DEPSREL_TYPE_COMPONENT_ORDER, "Data dependency");
1443
1444         if (ob->adt && (ob->adt->action || ob->adt->nla_tracks.first)) {
1445                 ComponentKey animation_key(&ob->id, DEPSNODE_TYPE_ANIMATION);
1446                 add_relation(animation_key, init_key, DEPSREL_TYPE_OPERATION, "Rig Animation");
1447         }
1448
1449         /* IK Solvers...
1450          * - These require separate processing steps are pose-level
1451          *   to be executed between chains of bones (i.e. once the
1452          *   base transforms of a bunch of bones is done)
1453          *
1454          * - We build relations for these before the dependencies
1455          *   between ops in the same component as it is necessary
1456          *   to check whether such bones are in the same IK chain
1457          *   (or else we get weird issues with either in-chain
1458          *   references, or with bones being parented to IK'd bones)
1459          *
1460          * Unsolved Issues:
1461          * - Care is needed to ensure that multi-headed trees work out the same as in ik-tree building
1462          * - Animated chain-lengths are a problem...
1463          */
1464         RootPChanMap root_map;
1465         bool pose_depends_on_local_transform = false;
1466         for (bPoseChannel *pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1467                 for (bConstraint *con = (bConstraint *)pchan->constraints.first; con; con = con->next) {
1468                         switch (con->type) {
1469                                 case CONSTRAINT_TYPE_KINEMATIC:
1470                                         build_ik_pose(ob, pchan, con, &root_map);
1471                                         pose_depends_on_local_transform = true;
1472                                         break;
1473
1474                                 case CONSTRAINT_TYPE_SPLINEIK:
1475                                         build_splineik_pose(ob, pchan, con, &root_map);
1476                                         pose_depends_on_local_transform = true;
1477                                         break;
1478
1479                                 /* Constraints which needs world's matrix for transform.
1480                                  * TODO(sergey): More constraints here?
1481                                  */
1482                                 case CONSTRAINT_TYPE_ROTLIKE:
1483                                 case CONSTRAINT_TYPE_SIZELIKE:
1484                                 case CONSTRAINT_TYPE_LOCLIKE:
1485                                 case CONSTRAINT_TYPE_TRANSLIKE:
1486                                         /* TODO(sergey): Add used space check. */
1487                                         pose_depends_on_local_transform = true;
1488                                         break;
1489
1490                                 default:
1491                                         break;
1492                         }
1493                 }
1494         }
1495         //root_map.print_debug();
1496
1497         if (pose_depends_on_local_transform) {
1498                 /* TODO(sergey): Once partial updates are possible use relation between
1499                  * object transform and solver itself in it's build function.
1500                  */
1501                 ComponentKey pose_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE);
1502                 ComponentKey local_transform_key(&ob->id, DEPSNODE_TYPE_TRANSFORM);
1503                 add_relation(local_transform_key, pose_key, DEPSREL_TYPE_TRANSFORM, "Local Transforms");
1504         }
1505
1506
1507         /* links between operations for each bone */
1508         for (bPoseChannel *pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1509                 OperationKey bone_local_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_LOCAL);
1510                 OperationKey bone_pose_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_POSE_PARENT);
1511                 OperationKey bone_ready_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_READY);
1512                 OperationKey bone_done_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_DONE);
1513
1514                 pchan->flag &= ~POSE_DONE;
1515
1516                 /* pose init to bone local */
1517                 add_relation(init_key, bone_local_key, DEPSREL_TYPE_OPERATION, "PoseEval Source-Bone Link");
1518
1519                 /* local to pose parenting operation */
1520                 add_relation(bone_local_key, bone_pose_key, DEPSREL_TYPE_OPERATION, "Bone Local - PoseSpace Link");
1521
1522                 /* parent relation */
1523                 if (pchan->parent != NULL) {
1524                         eDepsOperation_Code parent_key_opcode;
1525
1526                         /* NOTE: this difference in handling allows us to prevent lockups while ensuring correct poses for separate chains */
1527                         if (root_map.has_common_root(pchan->name, pchan->parent->name)) {
1528                                 parent_key_opcode = DEG_OPCODE_BONE_READY;
1529                         }
1530                         else {
1531                                 parent_key_opcode = DEG_OPCODE_BONE_DONE;
1532                         }
1533
1534                         OperationKey parent_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->parent->name, parent_key_opcode);
1535                         add_relation(parent_key, bone_pose_key, DEPSREL_TYPE_TRANSFORM, "[Parent Bone -> Child Bone]");
1536                 }
1537
1538                 /* constraints */
1539                 if (pchan->constraints.first != NULL) {
1540                         /* constraints stack and constraint dependencies */
1541                         build_constraints(scene, &ob->id, DEPSNODE_TYPE_BONE, pchan->name, &pchan->constraints, &root_map);
1542
1543                         /* pose -> constraints */
1544                         OperationKey constraints_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_CONSTRAINTS);
1545                         add_relation(bone_pose_key, constraints_key, DEPSREL_TYPE_OPERATION, "Constraints Stack");
1546
1547                         /* constraints -> ready */
1548                         // TODO: when constraint stack is exploded, this step should occur before the first IK solver
1549                         add_relation(constraints_key, bone_ready_key, DEPSREL_TYPE_OPERATION, "Constraints -> Ready");
1550                 }
1551                 else {
1552                         /* pose -> ready */
1553                         add_relation(bone_pose_key, bone_ready_key, DEPSREL_TYPE_OPERATION, "Pose -> Ready");
1554                 }
1555
1556                 /* bone ready -> done
1557                  * NOTE: For bones without IK, this is all that's needed.
1558                  *       For IK chains however, an additional rel is created from IK to done,
1559                  *       with transitive reduction removing this one...
1560                  */
1561                 add_relation(bone_ready_key, bone_done_key, DEPSREL_TYPE_OPERATION, "Ready -> Done");
1562
1563                 /* assume that all bones must be done for the pose to be ready (for deformers) */
1564                 add_relation(bone_done_key, flush_key, DEPSREL_TYPE_OPERATION, "PoseEval Result-Bone Link");
1565         }
1566 }
1567
1568 void DepsgraphRelationBuilder::build_proxy_rig(Object *ob)
1569 {
1570         OperationKey pose_init_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_INIT);
1571         OperationKey pose_done_key(&ob->id, DEPSNODE_TYPE_EVAL_POSE, DEG_OPCODE_POSE_DONE);
1572         for (bPoseChannel *pchan = (bPoseChannel *)ob->pose->chanbase.first;
1573              pchan != NULL;
1574              pchan = pchan->next)
1575         {
1576                 OperationKey bone_local_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_LOCAL);
1577                 OperationKey bone_ready_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_READY);
1578                 OperationKey bone_done_key(&ob->id, DEPSNODE_TYPE_BONE, pchan->name, DEG_OPCODE_BONE_DONE);
1579                 add_relation(pose_init_key, bone_local_key, DEPSREL_TYPE_OPERATION, "Pose Init -> Bone Local");
1580                 add_relation(bone_local_key, bone_ready_key, DEPSREL_TYPE_OPERATION, "Local -> Ready");
1581                 add_relation(bone_ready_key, bone_done_key, DEPSREL_TYPE_OPERATION, "Ready -> Done");
1582                 add_relation(bone_done_key, pose_done_key, DEPSREL_TYPE_OPERATION, "Bone Done -> Pose Done");
1583         }
1584 }
1585
1586 /* Shapekeys */
1587 void DepsgraphRelationBuilder::build_shapekeys(ID *obdata, Key *key)
1588 {
1589         ComponentKey obdata_key(obdata, DEPSNODE_TYPE_GEOMETRY);
1590
1591         /* attach animdata to geometry */
1592         build_animdata(&key->id);
1593
1594         if (key->adt) {
1595                 // TODO: this should really be handled in build_animdata, since many of these cases will need it
1596                 if (key->adt->action || key->adt->nla_tracks.first) {
1597                         ComponentKey adt_key(&key->id, DEPSNODE_TYPE_ANIMATION);
1598                         add_relation(adt_key, obdata_key, DEPSREL_TYPE_OPERATION, "Animation");
1599                 }
1600
1601                 /* NOTE: individual shapekey drivers are handled above already */
1602         }
1603
1604         /* attach to geometry */
1605         // XXX: aren't shapekeys now done as a pseudo-modifier on object?
1606         //ComponentKey key_key(&key->id, DEPSNODE_TYPE_GEOMETRY); // FIXME: this doesn't exist
1607         //add_relation(key_key, obdata_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Shapekeys");
1608 }
1609
1610 /**
1611  * ObData Geometry Evaluation
1612  * ==========================
1613  *
1614  * The evaluation of geometry on objects is as follows:
1615  * - The actual evaluated of the derived geometry (e.g. DerivedMesh, DispList, etc.)
1616  *   occurs in the Geometry component of the object which references this. This includes
1617  *   modifiers, and the temporary "ubereval" for geometry.
1618  * - Therefore, each user of a piece of shared geometry data ends up evaluating its own
1619  *   version of the stuff, complete with whatever modifiers it may use.
1620  *
1621  * - The datablocks for the geometry data - "obdata" (e.g. ID_ME, ID_CU, ID_LT, etc.) are used for
1622  *     1) calculating the bounding boxes of the geometry data,
1623  *     2) aggregating inward links from other objects (e.g. for text on curve, etc.)
1624  *        and also for the links coming from the shapekey datablocks
1625  * - Animation/Drivers affecting the parameters of the geometry are made to trigger
1626  *   updates on the obdata geometry component, which then trigger downstream
1627  *   re-evaluation of the individual instances of this geometry.
1628  */
1629 // TODO: Materials and lighting should probably get their own component, instead of being lumped under geometry?
1630 void DepsgraphRelationBuilder::build_obdata_geom(Main *bmain, Scene *scene, Object *ob)
1631 {
1632         ID *obdata = (ID *)ob->data;
1633
1634         /* Init operation of object-level geometry evaluation. */
1635         OperationKey geom_init_key(&ob->id, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Eval Init");
1636
1637         /* get nodes for result of obdata's evaluation, and geometry evaluation on object */
1638         ComponentKey obdata_geom_key(obdata, DEPSNODE_TYPE_GEOMETRY);
1639         ComponentKey geom_key(&ob->id, DEPSNODE_TYPE_GEOMETRY);
1640
1641         /* link components to each other */
1642         add_relation(obdata_geom_key, geom_key, DEPSREL_TYPE_DATABLOCK, "Object Geometry Base Data");
1643
1644         /* Modifiers */
1645         if (ob->modifiers.first) {
1646                 ModifierData *md;
1647                 OperationKey prev_mod_key;
1648
1649                 for (md = (ModifierData *)ob->modifiers.first; md; md = md->next) {
1650                         const ModifierTypeInfo *mti = modifierType_getInfo((ModifierType)md->type);
1651                         OperationKey mod_key(&ob->id, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_GEOMETRY_MODIFIER, md->name);
1652
1653                         if (md->prev) {
1654                                 /* Stack relation: modifier depends on previous modifier in the stack */
1655                                 add_relation(prev_mod_key, mod_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Modifier Stack");
1656                         }
1657                         else {
1658                                 /* Stack relation: first modifier depends on the geometry. */
1659                                 add_relation(geom_init_key, mod_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Modifier Stack");
1660                         }
1661
1662                         if (mti->updateDepsgraph) {
1663                                 DepsNodeHandle handle = create_node_handle(mod_key);
1664                                 mti->updateDepsgraph(
1665                                         md,
1666                                         bmain,
1667                                         scene,
1668                                         ob,
1669                                         reinterpret_cast< ::DepsNodeHandle* >(&handle));
1670                         }
1671
1672                         if (BKE_object_modifier_use_time(ob, md)) {
1673                                 TimeSourceKey time_src_key;
1674                                 add_relation(time_src_key, mod_key, DEPSREL_TYPE_TIME, "Time Source");
1675
1676                                 /* Hacky fix for T45633 (Animated modifiers aren't updated)
1677                                  *
1678                                  * This check works because BKE_object_modifier_use_time() tests
1679                                  * for either the modifier needing time, or that it is animated.
1680                                  */
1681                                 /* XXX: Remove this hack when these links are added as part of build_animdata() instead */
1682                                 if (modifier_dependsOnTime(md) == false) {
1683                                         ComponentKey animation_key(&ob->id, DEPSNODE_TYPE_ANIMATION);
1684                                         add_relation(animation_key, mod_key, DEPSREL_TYPE_OPERATION, "Modifier Animation");
1685                                 }
1686                         }
1687
1688                         prev_mod_key = mod_key;
1689                 }
1690         }
1691
1692         /* materials */
1693         if (ob->totcol) {
1694                 int a;
1695
1696                 for (a = 1; a <= ob->totcol; a++) {
1697                         Material *ma = give_current_material(ob, a);
1698
1699                         if (ma)
1700                                 build_material(&ob->id, ma);
1701                 }
1702         }
1703
1704         /* geometry collision */
1705         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_LATTICE)) {
1706                 // add geometry collider relations
1707         }
1708
1709         /* Make sure uber update is the last in the dependencies.
1710          *
1711          * TODO(sergey): Get rid of this node.
1712          */
1713         if (ob->type != OB_ARMATURE) {
1714                 /* Armatures does no longer require uber node. */
1715                 OperationKey obdata_ubereval_key(&ob->id, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_GEOMETRY_UBEREVAL);
1716                 if (ob->modifiers.last) {
1717                         ModifierData *md = (ModifierData *)ob->modifiers.last;
1718                         OperationKey mod_key(&ob->id, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_GEOMETRY_MODIFIER, md->name);
1719                         add_relation(mod_key, obdata_ubereval_key, DEPSREL_TYPE_OPERATION, "Object Geometry UberEval");
1720                 }
1721                 else {
1722                         add_relation(geom_init_key, obdata_ubereval_key, DEPSREL_TYPE_OPERATION, "Object Geometry UberEval");
1723                 }
1724         }
1725
1726         if (obdata->tag & LIB_TAG_DOIT) {
1727                 return;
1728         }
1729         obdata->tag |= LIB_TAG_DOIT;
1730
1731         /* Link object data evaluation node to exit operation. */
1732         OperationKey obdata_geom_eval_key(obdata, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1733         OperationKey obdata_geom_done_key(obdata, DEPSNODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Eval Done");
1734         add_relation(obdata_geom_eval_key, obdata_geom_done_key, DEPSREL_TYPE_DATABLOCK, "ObData Geom Eval Done");
1735
1736         /* type-specific node/links */
1737         switch (ob->type) {
1738                 case OB_MESH:
1739                         break;
1740
1741                 case OB_MBALL:
1742                 {
1743                         Object *mom = BKE_mball_basis_find(scene, ob);
1744
1745                         /* motherball - mom depends on children! */
1746                         if (mom != ob) {
1747                                 /* non-motherball -> cannot be directly evaluated! */
1748                                 ComponentKey mom_key(&mom->id, DEPSNODE_TYPE_GEOMETRY);
1749                                 ComponentKey transform_key(&ob->id, DEPSNODE_TYPE_TRANSFORM);
1750                                 add_relation(geom_key, mom_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Metaball Motherball");
1751                                 add_relation(transform_key, mom_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Metaball Motherball");
1752                         }
1753                         break;
1754                 }
1755
1756                 case OB_CURVE:
1757                 case OB_FONT:
1758                 {
1759                         Curve *cu = (Curve *)obdata;
1760
1761                         /* curve's dependencies */
1762                         // XXX: these needs geom data, but where is geom stored?
1763                         if (cu->bevobj) {
1764                                 ComponentKey bevob_key(&cu->bevobj->id, DEPSNODE_TYPE_GEOMETRY);
1765                                 add_relation(bevob_key, geom_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Curve Bevel");
1766                         }
1767                         if (cu->taperobj) {
1768                                 ComponentKey taperob_key(&cu->taperobj->id, DEPSNODE_TYPE_GEOMETRY);
1769                                 add_relation(taperob_key, geom_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Curve Taper");
1770                         }
1771                         if (ob->type == OB_FONT) {
1772                                 if (cu->textoncurve) {
1773                                         ComponentKey textoncurve_key(&cu->taperobj->id, DEPSNODE_TYPE_GEOMETRY);
1774                                         add_relation(textoncurve_key, geom_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Text on Curve");
1775                                 }
1776                         }
1777                         break;
1778                 }
1779
1780                 case OB_SURF: /* Nurbs Surface */
1781                 {
1782                         break;
1783                 }
1784
1785                 case OB_LATTICE: /* Lattice */
1786                 {
1787                         break;
1788                 }
1789         }
1790
1791         /* ShapeKeys */
1792         Key *key = BKE_key_from_object(ob);
1793         if (key) {
1794                 build_shapekeys(obdata, key);
1795         }
1796
1797         if (needs_animdata_node(obdata)) {
1798                 ComponentKey animation_key(obdata, DEPSNODE_TYPE_ANIMATION);
1799                 ComponentKey parameters_key(obdata, DEPSNODE_TYPE_PARAMETERS);
1800                 add_relation(animation_key, parameters_key,
1801                              DEPSREL_TYPE_COMPONENT_ORDER, "Geom Parameters");
1802                 /* Evaluation usually depends on animation.
1803                  * TODO(sergey): Need to re-hook it after granular update is implemented..
1804                  */
1805                 add_relation(animation_key, obdata_geom_eval_key, DEPSREL_TYPE_GEOMETRY_EVAL, "Animation");
1806         }
1807 }
1808
1809 /* Cameras */
1810 // TODO: Link scene-camera links in somehow...
1811 void DepsgraphRelationBuilder::build_camera(Object *ob)
1812 {
1813         Camera *cam = (Camera *)ob->data;
1814         ID *camera_id = &cam->id;
1815         if (camera_id->tag & LIB_TAG_DOIT) {
1816                 return;
1817         }
1818         camera_id->tag |= LIB_TAG_DOIT;
1819
1820         ComponentKey parameters_key(camera_id, DEPSNODE_TYPE_PARAMETERS);
1821
1822         if (needs_animdata_node(camera_id)) {
1823                 ComponentKey animation_key(camera_id, DEPSNODE_TYPE_ANIMATION);
1824                 add_relation(animation_key, parameters_key,
1825                              DEPSREL_TYPE_COMPONENT_ORDER, "Camera Parameters");
1826         }
1827
1828         /* DOF */
1829         if (cam->dof_ob) {
1830                 ComponentKey ob_param_key(&ob->id, DEPSNODE_TYPE_PARAMETERS);
1831                 ComponentKey dof_ob_key(&cam->dof_ob->id, DEPSNODE_TYPE_TRANSFORM);
1832                 add_relation(dof_ob_key, ob_param_key, DEPSREL_TYPE_TRANSFORM, "Camera DOF");
1833         }
1834 }
1835
1836 /* Lamps */
1837 void DepsgraphRelationBuilder::build_lamp(Object *ob)
1838 {
1839         Lamp *la = (Lamp *)ob->data;
1840         ID *lamp_id = &la->id;
1841         if (lamp_id->tag & LIB_TAG_DOIT) {
1842                 return;
1843         }
1844         lamp_id->tag |= LIB_TAG_DOIT;
1845
1846         ComponentKey parameters_key(lamp_id, DEPSNODE_TYPE_PARAMETERS);
1847
1848         if (needs_animdata_node(lamp_id)) {
1849                 ComponentKey animation_key(lamp_id, DEPSNODE_TYPE_ANIMATION);
1850                 add_relation(animation_key, parameters_key,
1851                              DEPSREL_TYPE_COMPONENT_ORDER, "Lamp Parameters");
1852         }
1853
1854         /* lamp's nodetree */
1855         if (la->nodetree) {
1856                 build_nodetree(lamp_id, la->nodetree);
1857                 ComponentKey nodetree_key(&la->nodetree->id, DEPSNODE_TYPE_PARAMETERS);
1858                 add_relation(nodetree_key, parameters_key,
1859                              DEPSREL_TYPE_COMPONENT_ORDER, "NTree->Lamp Parameters");
1860         }
1861
1862         /* textures */
1863         build_texture_stack(lamp_id, la->mtex);
1864 }
1865
1866 void DepsgraphRelationBuilder::build_nodetree(ID *owner, bNodeTree *ntree)
1867 {
1868         if (!ntree)
1869                 return;
1870
1871         ID *ntree_id = &ntree->id;
1872
1873         build_animdata(ntree_id);
1874
1875         OperationKey parameters_key(ntree_id,
1876                                     DEPSNODE_TYPE_PARAMETERS,
1877                                     DEG_OPCODE_PLACEHOLDER,
1878                                     "Parameters Eval");
1879
1880         /* nodetree's nodes... */
1881         for (bNode *bnode = (bNode *)ntree->nodes.first; bnode; bnode = bnode->next) {
1882                 if (bnode->id) {
1883                         if (GS(bnode->id->name) == ID_MA) {
1884                                 build_material(owner, (Material *)bnode->id);
1885                         }
1886                         else if (bnode->type == ID_TE) {
1887                                 build_texture(owner, (Tex *)bnode->id);
1888                         }
1889                         else if (bnode->type == NODE_GROUP) {
1890                                 bNodeTree *group_ntree = (bNodeTree *)bnode->id;
1891                                 if ((group_ntree->id.tag & LIB_TAG_DOIT) == 0) {
1892                                         build_nodetree(owner, group_ntree);
1893                                         group_ntree->id.tag |= LIB_TAG_DOIT;
1894                                 }
1895                                 OperationKey group_parameters_key(&group_ntree->id,
1896                                                                   DEPSNODE_TYPE_PARAMETERS,
1897                                                                   DEG_OPCODE_PLACEHOLDER,
1898                                                                   "Parameters Eval");
1899                                 add_relation(group_parameters_key, parameters_key,
1900                                              DEPSREL_TYPE_COMPONENT_ORDER, "Group Node");
1901                         }
1902                 }
1903         }
1904
1905         if (needs_animdata_node(ntree_id)) {
1906                 ComponentKey animation_key(ntree_id, DEPSNODE_TYPE_ANIMATION);
1907                 add_relation(animation_key, parameters_key,
1908                              DEPSREL_TYPE_COMPONENT_ORDER, "NTree Parameters");
1909         }
1910
1911         // TODO: link from nodetree to owner_component?
1912 }
1913
1914 /* Recursively build graph for material */
1915 void DepsgraphRelationBuilder::build_material(ID *owner, Material *ma)
1916 {
1917         ID *ma_id = &ma->id;
1918         if (ma_id->tag & LIB_TAG_DOIT) {
1919                 return;
1920         }
1921         ma_id->tag |= LIB_TAG_DOIT;
1922
1923         /* animation */
1924         build_animdata(ma_id);
1925
1926         /* textures */
1927         build_texture_stack(owner, ma->mtex);
1928
1929         /* material's nodetree */
1930         build_nodetree(owner, ma->nodetree);
1931 }
1932
1933 /* Recursively build graph for texture */
1934 void DepsgraphRelationBuilder::build_texture(ID *owner, Tex *tex)
1935 {
1936         ID *tex_id = &tex->id;
1937         if (tex_id->tag & LIB_TAG_DOIT) {
1938                 return;
1939         }
1940         tex_id->tag |= LIB_TAG_DOIT;
1941
1942         /* texture itself */
1943         build_animdata(tex_id);
1944
1945         /* texture's nodetree */
1946         build_nodetree(owner, tex->nodetree);
1947 }
1948
1949 /* Texture-stack attached to some shading datablock */
1950 void DepsgraphRelationBuilder::build_texture_stack(ID *owner, MTex **texture_stack)
1951 {
1952         int i;
1953
1954         /* for now assume that all texture-stacks have same number of max items */
1955         for (i = 0; i < MAX_MTEX; i++) {
1956                 MTex *mtex = texture_stack[i];
1957                 if (mtex && mtex->tex)
1958                         build_texture(owner, mtex->tex);
1959         }
1960 }
1961
1962 void DepsgraphRelationBuilder::build_compositor(Scene *scene)
1963 {
1964         /* For now, just a plain wrapper? */
1965         build_nodetree(&scene->id, scene->nodetree);
1966 }
1967
1968 void DepsgraphRelationBuilder::build_gpencil(ID *UNUSED(owner), bGPdata *gpd)
1969 {
1970         /* animation */
1971         build_animdata(&gpd->id);
1972
1973         // TODO: parent object (when that feature is implemented)
1974 }
1975
1976 bool DepsgraphRelationBuilder::needs_animdata_node(ID *id)
1977 {
1978         AnimData *adt = BKE_animdata_from_id(id);
1979         if (adt != NULL) {
1980                 return adt->action != NULL;
1981         }
1982         return false;
1983 }
1984
1985 }  // namespace DEG