Merge branch 'master' into blender2.8
[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 <cstring>  /* required for STREQ later on. */
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_utildefines.h"
42 #include "BLI_blenlib.h"
43
44 extern "C" {
45 #include "DNA_action_types.h"
46 #include "DNA_anim_types.h"
47 #include "DNA_armature_types.h"
48 #include "DNA_camera_types.h"
49 #include "DNA_cachefile_types.h"
50 #include "DNA_constraint_types.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_effect_types.h"
53 #include "DNA_gpencil_types.h"
54 #include "DNA_group_types.h"
55 #include "DNA_key_types.h"
56 #include "DNA_lamp_types.h"
57 #include "DNA_material_types.h"
58 #include "DNA_mask_types.h"
59 #include "DNA_mesh_types.h"
60 #include "DNA_meta_types.h"
61 #include "DNA_movieclip_types.h"
62 #include "DNA_node_types.h"
63 #include "DNA_particle_types.h"
64 #include "DNA_lightprobe_types.h"
65 #include "DNA_object_types.h"
66 #include "DNA_rigidbody_types.h"
67 #include "DNA_scene_types.h"
68 #include "DNA_texture_types.h"
69 #include "DNA_world_types.h"
70 #include "DNA_object_force.h"
71
72 #include "BKE_action.h"
73 #include "BKE_armature.h"
74 #include "BKE_animsys.h"
75 #include "BKE_constraint.h"
76 #include "BKE_curve.h"
77 #include "BKE_effect.h"
78 #include "BKE_collision.h"
79 #include "BKE_fcurve.h"
80 #include "BKE_group.h"
81 #include "BKE_key.h"
82 #include "BKE_library.h"
83 #include "BKE_main.h"
84 #include "BKE_material.h"
85 #include "BKE_mball.h"
86 #include "BKE_modifier.h"
87 #include "BKE_node.h"
88 #include "BKE_object.h"
89 #include "BKE_particle.h"
90 #include "BKE_rigidbody.h"
91 #include "BKE_sound.h"
92 #include "BKE_tracking.h"
93 #include "BKE_world.h"
94
95 #include "RNA_access.h"
96 #include "RNA_types.h"
97 } /* extern "C" */
98
99 #include "DEG_depsgraph.h"
100 #include "DEG_depsgraph_build.h"
101
102 #include "intern/builder/deg_builder.h"
103 #include "intern/builder/deg_builder_pchanmap.h"
104
105 #include "intern/nodes/deg_node.h"
106 #include "intern/nodes/deg_node_component.h"
107 #include "intern/nodes/deg_node_id.h"
108 #include "intern/nodes/deg_node_operation.h"
109 #include "intern/nodes/deg_node_time.h"
110
111 #include "intern/depsgraph_intern.h"
112 #include "intern/depsgraph_types.h"
113
114 #include "util/deg_util_foreach.h"
115
116 namespace DEG {
117
118 namespace {
119
120 struct BuilderWalkUserData {
121         DepsgraphRelationBuilder *builder;
122 };
123
124 void modifier_walk(void *user_data,
125                    struct Object * /*object*/,
126                    struct Object **obpoin,
127                    int /*cb_flag*/)
128 {
129         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
130         if (*obpoin) {
131                 data->builder->build_object(NULL, *obpoin);
132         }
133 }
134
135 void constraint_walk(bConstraint * /*con*/,
136                      ID **idpoin,
137                      bool /*is_reference*/,
138                      void *user_data)
139 {
140         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
141         if (*idpoin) {
142                 ID *id = *idpoin;
143                 if (GS(id->name) == ID_OB) {
144                         data->builder->build_object(NULL, (Object *)id);
145                 }
146         }
147 }
148
149 }  /* namespace */
150
151 /* ***************** */
152 /* Relations Builder */
153
154 /* TODO(sergey): This is somewhat weak, but we don't want neither false-positive
155  * time dependencies nor special exceptions in the depsgraph evaluation.
156  */
157 static bool python_driver_depends_on_time(ChannelDriver *driver)
158 {
159         if (driver->expression[0] == '\0') {
160                 /* Empty expression depends on nothing. */
161                 return false;
162         }
163         if (strchr(driver->expression, '(') != NULL) {
164                 /* Function calls are considered dependent on a time. */
165                 return true;
166         }
167         if (strstr(driver->expression, "frame") != NULL) {
168                 /* Variable `frame` depends on time. */
169                 /* TODO(sergey): This is a bit weak, but not sure about better way of
170                  * handling this.
171                  */
172                 return true;
173         }
174         /* Possible indirect time relation s should be handled via variable
175          * targets.
176          */
177         return false;
178 }
179
180 static bool particle_system_depends_on_time(ParticleSystem *psys)
181 {
182         ParticleSettings *part = psys->part;
183         /* Non-hair particles we always consider dependent on time. */
184         if (part->type != PART_HAIR) {
185                 return true;
186         }
187         /* Dynamics always depends on time. */
188         if (psys->flag & PSYS_HAIR_DYNAMICS) {
189                 return true;
190         }
191         /* TODO(sergey): Check what else makes hair dependent on time. */
192         return false;
193 }
194
195 static bool object_particles_depends_on_time(Object *object)
196 {
197         return true;
198         BLI_LISTBASE_FOREACH (ParticleSystem *, psys, &object->particlesystem) {
199                 if (particle_system_depends_on_time(psys)) {
200                         return true;
201                 }
202         }
203         return false;
204 }
205
206 /* **** General purpose functions ****  */
207
208 DepsgraphRelationBuilder::DepsgraphRelationBuilder(Main *bmain,
209                                                    Depsgraph *graph)
210     : bmain_(bmain),
211       graph_(graph),
212       scene_(NULL)
213 {
214 }
215
216 TimeSourceDepsNode *DepsgraphRelationBuilder::get_node(
217         const TimeSourceKey &key) const
218 {
219         if (key.id) {
220                 /* XXX TODO */
221                 return NULL;
222         }
223         else {
224                 return graph_->time_source;
225         }
226 }
227
228 ComponentDepsNode *DepsgraphRelationBuilder::get_node(
229         const ComponentKey &key) const
230 {
231         IDDepsNode *id_node = graph_->find_id_node(key.id);
232         if (!id_node) {
233                 fprintf(stderr, "find_node component: Could not find ID %s\n",
234                         (key.id != NULL) ? key.id->name : "<null>");
235                 return NULL;
236         }
237
238         ComponentDepsNode *node = id_node->find_component(key.type, key.name);
239         return node;
240 }
241
242 OperationDepsNode *DepsgraphRelationBuilder::get_node(
243         const OperationKey &key) const
244 {
245         OperationDepsNode *op_node = find_node(key);
246         if (op_node == NULL) {
247                 fprintf(stderr, "find_node_operation: Failed for (%s, '%s')\n",
248                         DEG_OPNAMES[key.opcode], key.name);
249         }
250         return op_node;
251 }
252
253 DepsNode *DepsgraphRelationBuilder::get_node(const RNAPathKey &key) const
254 {
255         return graph_->find_node_from_pointer(&key.ptr, key.prop);
256 }
257
258 OperationDepsNode *DepsgraphRelationBuilder::find_node(
259         const OperationKey &key) const
260 {
261         IDDepsNode *id_node = graph_->find_id_node(key.id);
262         if (!id_node) {
263                 return NULL;
264         }
265         ComponentDepsNode *comp_node = id_node->find_component(key.component_type,
266                                                                key.component_name);
267         if (!comp_node) {
268                 return NULL;
269         }
270         return comp_node->find_operation(key.opcode, key.name, key.name_tag);
271 }
272
273 bool DepsgraphRelationBuilder::has_node(const OperationKey &key) const
274 {
275         return find_node(key) != NULL;
276 }
277
278 void DepsgraphRelationBuilder::add_time_relation(TimeSourceDepsNode *timesrc,
279                                                  DepsNode *node_to,
280                                                  const char *description,
281                                                  bool check_unique)
282 {
283         if (timesrc && node_to) {
284                 graph_->add_new_relation(timesrc, node_to, description, check_unique);
285         }
286         else {
287                 DEG_DEBUG_PRINTF("add_time_relation(%p = %s, %p = %s, %s) Failed\n",
288                                  timesrc,   (timesrc) ? timesrc->identifier().c_str() : "<None>",
289                                  node_to,   (node_to) ? node_to->identifier().c_str() : "<None>",
290                                  description);
291         }
292 }
293
294 void DepsgraphRelationBuilder::add_operation_relation(
295         OperationDepsNode *node_from,
296         OperationDepsNode *node_to,
297         const char *description,
298         bool check_unique)
299 {
300         if (node_from && node_to) {
301                 graph_->add_new_relation(node_from, node_to, description, check_unique);
302         }
303         else {
304                 DEG_DEBUG_PRINTF("add_operation_relation(%p = %s, %p = %s, %s) Failed\n",
305                                  node_from, (node_from) ? node_from->identifier().c_str() : "<None>",
306                                  node_to,   (node_to)   ? node_to->identifier().c_str() : "<None>",
307                                  description);
308         }
309 }
310
311 void DepsgraphRelationBuilder::add_collision_relations(
312         const OperationKey &key,
313         Scene *scene,
314         Object *object,
315         Group *group,
316         bool dupli,
317         const char *name)
318 {
319         unsigned int numcollobj;
320         Object **collobjs = get_collisionobjects_ext(scene,
321                                                      object,
322                                                      group,
323                                                      &numcollobj,
324                                                      eModifierType_Collision,
325                                                      dupli);
326         for (unsigned int i = 0; i < numcollobj; i++) {
327                 Object *ob1 = collobjs[i];
328
329                 ComponentKey trf_key(&ob1->id, DEG_NODE_TYPE_TRANSFORM);
330                 add_relation(trf_key, key, name);
331
332                 ComponentKey coll_key(&ob1->id, DEG_NODE_TYPE_GEOMETRY);
333                 add_relation(coll_key, key, name);
334         }
335         if (collobjs != NULL) {
336                 MEM_freeN(collobjs);
337         }
338 }
339
340 void DepsgraphRelationBuilder::add_forcefield_relations(
341         const OperationKey &key,
342         Scene *scene,
343         Object *object,
344         ParticleSystem *psys,
345         EffectorWeights *eff,
346         bool add_absorption,
347         const char *name)
348 {
349         ListBase *effectors = pdInitEffectors(NULL, scene, object, psys, eff, false);
350         if (effectors != NULL) {
351                 BLI_LISTBASE_FOREACH (EffectorCache *, eff, effectors) {
352                         if (eff->ob != object) {
353                                 ComponentKey eff_key(&eff->ob->id, DEG_NODE_TYPE_TRANSFORM);
354                                 add_relation(eff_key, key, name);
355                         }
356                         if (eff->psys != NULL) {
357                                 if (eff->ob != object) {
358                                         ComponentKey eff_key(&eff->ob->id, DEG_NODE_TYPE_EVAL_PARTICLES);
359                                         add_relation(eff_key, key, name);
360
361                                         /* TODO: remove this when/if EVAL_PARTICLES is sufficient
362                                          * for up to date particles.
363                                          */
364                                         ComponentKey mod_key(&eff->ob->id, DEG_NODE_TYPE_GEOMETRY);
365                                         add_relation(mod_key, key, name);
366                                 }
367                                 else if (eff->psys != psys) {
368                                         OperationKey eff_key(&eff->ob->id,
369                                                              DEG_NODE_TYPE_EVAL_PARTICLES,
370                                                              DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
371                                                              eff->psys->name);
372                                         add_relation(eff_key, key, name);
373                                 }
374                         }
375                         if (eff->pd->forcefield == PFIELD_SMOKEFLOW && eff->pd->f_source) {
376                                 ComponentKey trf_key(&eff->pd->f_source->id,
377                                                      DEG_NODE_TYPE_TRANSFORM);
378                                 add_relation(trf_key, key, "Smoke Force Domain");
379
380                                 ComponentKey eff_key(&eff->pd->f_source->id,
381                                                      DEG_NODE_TYPE_GEOMETRY);
382                                 add_relation(eff_key, key, "Smoke Force Domain");
383                         }
384                         if (add_absorption && (eff->pd->flag & PFIELD_VISIBILITY)) {
385                                 add_collision_relations(key,
386                                                         scene,
387                                                         object,
388                                                         NULL,
389                                                         true,
390                                                         "Force Absorption");
391                         }
392                 }
393         }
394
395         pdEndEffectors(&effectors);
396 }
397
398 Depsgraph *DepsgraphRelationBuilder::getGraph()
399 {
400         return graph_;
401 }
402
403 /* **** Functions to build relations between entities  **** */
404
405 void DepsgraphRelationBuilder::begin_build()
406 {
407         /* LIB_TAG_DOIT is used to indicate whether node for given ID was already
408          * created or not.
409          */
410         BKE_main_id_tag_all(bmain_, LIB_TAG_DOIT, false);
411         /* XXX nested node trees are notr included in tag-clearing above,
412          * so we need to do this manually.
413          */
414         FOREACH_NODETREE(bmain_, nodetree, id)
415         {
416                 if (id != (ID *)nodetree) {
417                         nodetree->id.tag &= ~LIB_TAG_DOIT;
418                 }
419         }
420         FOREACH_NODETREE_END
421 }
422
423 void DepsgraphRelationBuilder::build_group(Object *object, Group *group)
424 {
425         ID *group_id = &group->id;
426         bool group_done = (group_id->tag & LIB_TAG_DOIT) != 0;
427         OperationKey object_local_transform_key(object != NULL ? &object->id : NULL,
428                                                 DEG_NODE_TYPE_TRANSFORM,
429                                                 DEG_OPCODE_TRANSFORM_LOCAL);
430         if (!group_done) {
431                 BLI_LISTBASE_FOREACH (Base *, base, &group->view_layer->object_bases) {
432                         build_object(NULL, base->object);
433                 }
434                 group_id->tag |= LIB_TAG_DOIT;
435         }
436         if (object != NULL) {
437                 BLI_LISTBASE_FOREACH (Base *, base, &group->view_layer->object_bases) {
438                         ComponentKey dupli_transform_key(&base->object->id, DEG_NODE_TYPE_TRANSFORM);
439                         add_relation(dupli_transform_key, object_local_transform_key, "Dupligroup");
440                 }
441         }
442 }
443
444 void DepsgraphRelationBuilder::build_object(Base *base, Object *object)
445 {
446         if (object->id.tag & LIB_TAG_DOIT) {
447                 if (base != NULL) {
448                         build_object_flags(base, object);
449                 }
450                 return;
451         }
452         object->id.tag |= LIB_TAG_DOIT;
453         /* Object Transforms */
454         eDepsOperation_Code base_op = (object->parent) ? DEG_OPCODE_TRANSFORM_PARENT
455                                                        : DEG_OPCODE_TRANSFORM_LOCAL;
456         OperationKey base_op_key(&object->id, DEG_NODE_TYPE_TRANSFORM, base_op);
457         OperationKey local_transform_key(&object->id,
458                                          DEG_NODE_TYPE_TRANSFORM,
459                                          DEG_OPCODE_TRANSFORM_LOCAL);
460         OperationKey parent_transform_key(&object->id,
461                                           DEG_NODE_TYPE_TRANSFORM,
462                                           DEG_OPCODE_TRANSFORM_PARENT);
463         OperationKey final_transform_key(&object->id,
464                                          DEG_NODE_TYPE_TRANSFORM,
465                                          DEG_OPCODE_TRANSFORM_FINAL);
466         OperationKey ob_ubereval_key(&object->id,
467                                      DEG_NODE_TYPE_TRANSFORM,
468                                      DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
469         /* Various flags, flushing from bases/collections. */
470         build_object_flags(base, object);
471         /* Parenting. */
472         if (object->parent != NULL) {
473                 /* Parent relationship. */
474                 build_object_parent(object);
475                 /* Local -> parent. */
476                 add_relation(local_transform_key,
477                              parent_transform_key,
478                              "ObLocal -> ObParent");
479         }
480         /* Modifiers. */
481         if (object->modifiers.first != NULL) {
482                 BuilderWalkUserData data;
483                 data.builder = this;
484                 modifiers_foreachObjectLink(object, modifier_walk, &data);
485         }
486         /* Constraints. */
487         if (object->constraints.first != NULL) {
488                 BuilderWalkUserData data;
489                 data.builder = this;
490                 BKE_constraints_id_loop(&object->constraints, constraint_walk, &data);
491         }
492         /* Object constraints. */
493         if (object->constraints.first != NULL) {
494                 OperationKey constraint_key(&object->id,
495                                             DEG_NODE_TYPE_TRANSFORM,
496                                             DEG_OPCODE_TRANSFORM_CONSTRAINTS);
497                 /* Constraint relations. */
498                 build_constraints(&object->id,
499                                   DEG_NODE_TYPE_TRANSFORM,
500                                   "",
501                                   &object->constraints,
502                                   NULL);
503                 /* operation order */
504                 add_relation(base_op_key, constraint_key, "ObBase-> Constraint Stack");
505                 add_relation(constraint_key, final_transform_key, "ObConstraints -> Done");
506                 // XXX
507                 add_relation(constraint_key, ob_ubereval_key, "Temp Ubereval");
508                 add_relation(ob_ubereval_key, final_transform_key, "Temp Ubereval");
509         }
510         else {
511                 /* NOTE: Keep an eye here, we skip some relations here to "streamline"
512                  * dependencies and avoid transitive relations which causes overhead.
513                  * But once we get rid of uber eval node this will need reconsideration.
514                  */
515                 if (object->rigidbody_object == NULL) {
516                         /* Rigid body will hook up another node inbetween, so skip
517                          * relation here to avoid transitive relation.
518                          */
519                         add_relation(base_op_key, ob_ubereval_key, "Temp Ubereval");
520                 }
521                 add_relation(ob_ubereval_key, final_transform_key, "Temp Ubereval");
522         }
523         /* Animation data */
524         build_animdata(&object->id);
525         /* Object data. */
526         build_object_data(object);
527         /* Particle systems. */
528         if (object->particlesystem.first != NULL) {
529                 build_particles(object);
530         }
531         /* Grease pencil. */
532         if (object->gpd != NULL) {
533                 build_gpencil(object->gpd);
534         }
535         /* Object that this is a proxy for. */
536         if (object->proxy != NULL) {
537                 object->proxy->proxy_from = object;
538                 build_object(NULL, object->proxy);
539                 /* TODO(sergey): This is an inverted relation, matches old depsgraph
540                  * behavior and need to be investigated if it still need to be inverted.
541                  */
542                 ComponentKey ob_pose_key(&object->id, DEG_NODE_TYPE_EVAL_POSE);
543                 ComponentKey proxy_pose_key(&object->proxy->id, DEG_NODE_TYPE_EVAL_POSE);
544                 add_relation(ob_pose_key, proxy_pose_key, "Proxy");
545         }
546         /* Object dupligroup. */
547         if (object->dup_group != NULL) {
548                 build_group(object, object->dup_group);
549         }
550 }
551
552 void DepsgraphRelationBuilder::build_object_flags(Base *base, Object *object)
553 {
554         if (base == NULL) {
555                 return;
556         }
557         OperationKey view_layer_done_key(&scene_->id,
558                                          DEG_NODE_TYPE_LAYER_COLLECTIONS,
559                                          DEG_OPCODE_VIEW_LAYER_DONE);
560         OperationKey object_flags_key(&object->id,
561                                       DEG_NODE_TYPE_LAYER_COLLECTIONS,
562                                       DEG_OPCODE_OBJECT_BASE_FLAGS);
563         add_relation(view_layer_done_key, object_flags_key, "Base flags flush");
564 }
565
566 void DepsgraphRelationBuilder::build_object_data(Object *object)
567 {
568         if (object->data == NULL) {
569                 return;
570         }
571         ID *obdata_id = (ID *)object->data;
572         /* Object data animation. */
573         build_animdata(obdata_id);
574         /* type-specific data. */
575         switch (object->type) {
576                 case OB_MESH:
577                 case OB_CURVE:
578                 case OB_FONT:
579                 case OB_SURF:
580                 case OB_MBALL:
581                 case OB_LATTICE:
582                 {
583                         build_obdata_geom(object);
584                         break;
585                 }
586                 case OB_ARMATURE:
587                         if (ID_IS_LINKED(object) && object->proxy_from != NULL) {
588                                 build_proxy_rig(object);
589                         }
590                         else {
591                                 build_rig(object);
592                         }
593                         break;
594                 case OB_LAMP:
595                         build_lamp(object);
596                         break;
597                 case OB_CAMERA:
598                         build_camera(object);
599                         break;
600                 case OB_LIGHTPROBE:
601                         build_lightprobe(object);
602                         break;
603         }
604         Key *key = BKE_key_from_object(object);
605         if (key != NULL) {
606                 ComponentKey geometry_key((ID *)object->data, DEG_NODE_TYPE_GEOMETRY);
607                 ComponentKey key_key(&key->id, DEG_NODE_TYPE_GEOMETRY);
608                 add_relation(key_key, geometry_key, "Shapekeys");
609         }
610 }
611
612 void DepsgraphRelationBuilder::build_object_parent(Object *object)
613 {
614         /* XXX: for now, need to use the component key (not just direct to the parent op),
615          * or else the matrix doesn't get reset/
616          */
617         // XXX: @sergey - it would be good if we got that backwards flushing working
618         // when tagging for updates.
619         //OperationKey ob_key(&object->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_TRANSFORM_PARENT);
620         ComponentKey ob_key(&object->id, DEG_NODE_TYPE_TRANSFORM);
621
622         /* type-specific links */
623         switch (object->partype) {
624                 case PARSKEL:  /* Armature Deform (Virtual Modifier) */
625                 {
626                         ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
627                         add_relation(parent_key, ob_key, "Armature Deform Parent");
628                         break;
629                 }
630
631                 case PARVERT1: /* Vertex Parent */
632                 case PARVERT3:
633                 {
634                         ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_GEOMETRY);
635                         add_relation(parent_key, ob_key, "Vertex Parent");
636
637                         /* XXX not sure what this is for or how you could be done properly - lukas */
638                         OperationDepsNode *parent_node = find_operation_node(parent_key);
639                         if (parent_node != NULL) {
640                                 parent_node->customdata_mask |= CD_MASK_ORIGINDEX;
641                         }
642
643                         ComponentKey transform_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
644                         add_relation(transform_key, ob_key, "Vertex Parent TFM");
645                         break;
646                 }
647
648                 case PARBONE: /* Bone Parent */
649                 {
650                         ComponentKey parent_bone_key(&object->parent->id,
651                                                      DEG_NODE_TYPE_BONE,
652                                                      object->parsubstr);
653                         OperationKey parent_transform_key(&object->parent->id,
654                                                           DEG_NODE_TYPE_TRANSFORM,
655                                                           DEG_OPCODE_TRANSFORM_FINAL);
656                         add_relation(parent_bone_key, ob_key, "Bone Parent");
657                         add_relation(parent_transform_key, ob_key, "Armature Parent");
658                         break;
659                 }
660
661                 default:
662                 {
663                         if (object->parent->type == OB_LATTICE) {
664                                 /* Lattice Deform Parent - Virtual Modifier */
665                                 // XXX: no virtual modifiers should be left!
666                                 ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
667                                 ComponentKey geom_key(&object->parent->id, DEG_NODE_TYPE_GEOMETRY);
668
669                                 add_relation(parent_key, ob_key, "Lattice Deform Parent");
670                                 add_relation(geom_key, ob_key, "Lattice Deform Parent Geom");
671                         }
672                         else if (object->parent->type == OB_CURVE) {
673                                 Curve *cu = (Curve *)object->parent->data;
674
675                                 if (cu->flag & CU_PATH) {
676                                         /* Follow Path */
677                                         ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_GEOMETRY);
678                                         add_relation(parent_key, ob_key, "Curve Follow Parent");
679
680                                         ComponentKey transform_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
681                                         add_relation(transform_key, ob_key, "Curve Follow TFM");
682                                 }
683                                 else {
684                                         /* Standard Parent */
685                                         ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
686                                         add_relation(parent_key, ob_key, "Curve Parent");
687                                 }
688                         }
689                         else {
690                                 /* Standard Parent */
691                                 ComponentKey parent_key(&object->parent->id, DEG_NODE_TYPE_TRANSFORM);
692                                 add_relation(parent_key, ob_key, "Parent");
693                         }
694                         break;
695                 }
696         }
697
698         /* exception case: parent is duplivert */
699         if ((object->type == OB_MBALL) && (object->parent->transflag & OB_DUPLIVERTS)) {
700                 //dag_add_relation(dag, node2, node, DAG_RL_DATA_DATA | DAG_RL_OB_OB, "Duplivert");
701         }
702 }
703
704 void DepsgraphRelationBuilder::build_constraints(ID *id,
705                                                  eDepsNode_Type component_type,
706                                                  const char *component_subdata,
707                                                  ListBase *constraints,
708                                                  RootPChanMap *root_map)
709 {
710         OperationKey constraint_op_key(
711                 id,
712                 component_type,
713                 component_subdata,
714                 (component_type == DEG_NODE_TYPE_BONE)
715                         ? DEG_OPCODE_BONE_CONSTRAINTS
716                         : DEG_OPCODE_TRANSFORM_CONSTRAINTS);
717         /* Add dependencies for each constraint in turn. */
718         for (bConstraint *con = (bConstraint *)constraints->first; con; con = con->next) {
719                 const bConstraintTypeInfo *cti = BKE_constraint_typeinfo_get(con);
720                 /* Invalid constraint type. */
721                 if (cti == NULL) {
722                         continue;
723                 }
724                 /* Special case for camera tracking -- it doesn't use targets to
725                  * define relations.
726                  */
727                 /* TODO: we can now represent dependencies in a much richer manner,
728                  * so review how this is done.
729                  */
730                 if (ELEM(cti->type,
731                          CONSTRAINT_TYPE_FOLLOWTRACK,
732                          CONSTRAINT_TYPE_CAMERASOLVER,
733                          CONSTRAINT_TYPE_OBJECTSOLVER))
734                 {
735                         bool depends_on_camera = false;
736                         if (cti->type == CONSTRAINT_TYPE_FOLLOWTRACK) {
737                                 bFollowTrackConstraint *data = (bFollowTrackConstraint *)con->data;
738                                 if (((data->clip) ||
739                                      (data->flag & FOLLOWTRACK_ACTIVECLIP)) && data->track[0])
740                                 {
741                                         depends_on_camera = true;
742                                 }
743                                 if (data->depth_ob) {
744                                         ComponentKey depth_transform_key(&data->depth_ob->id,
745                                                                          DEG_NODE_TYPE_TRANSFORM);
746                                         ComponentKey depth_geometry_key(&data->depth_ob->id,
747                                                                         DEG_NODE_TYPE_GEOMETRY);
748                                         add_relation(depth_transform_key, constraint_op_key, cti->name);
749                                         add_relation(depth_geometry_key, constraint_op_key, cti->name);
750                                 }
751                         }
752                         else if (cti->type == CONSTRAINT_TYPE_OBJECTSOLVER) {
753                                 depends_on_camera = true;
754                         }
755                         if (depends_on_camera && scene_->camera != NULL) {
756                                 ComponentKey camera_key(&scene_->camera->id, DEG_NODE_TYPE_TRANSFORM);
757                                 add_relation(camera_key, constraint_op_key, cti->name);
758                         }
759                         /* TODO(sergey): This is more a TimeSource -> MovieClip ->
760                          * Constraint dependency chain.
761                          */
762                         TimeSourceKey time_src_key;
763                         add_relation(time_src_key, constraint_op_key, "TimeSrc -> Animation");
764                 }
765                 else if (cti->type == CONSTRAINT_TYPE_TRANSFORM_CACHE) {
766                         /* TODO(kevin): This is more a TimeSource -> CacheFile -> Constraint
767                          * dependency chain.
768                          */
769                         TimeSourceKey time_src_key;
770                         add_relation(time_src_key, constraint_op_key, "TimeSrc -> Animation");
771                         bTransformCacheConstraint *data = (bTransformCacheConstraint *)con->data;
772                         if (data->cache_file) {
773                                 ComponentKey cache_key(&data->cache_file->id, DEG_NODE_TYPE_CACHE);
774                                 add_relation(cache_key, constraint_op_key, cti->name);
775                         }
776                 }
777                 else if (cti->get_constraint_targets) {
778                         ListBase targets = {NULL, NULL};
779                         cti->get_constraint_targets(con, &targets);
780                         BLI_LISTBASE_FOREACH (bConstraintTarget *, ct, &targets) {
781                                 if (ct->tar == NULL) {
782                                         continue;
783                                 }
784                                 if (ELEM(con->type,
785                                          CONSTRAINT_TYPE_KINEMATIC,
786                                          CONSTRAINT_TYPE_SPLINEIK))
787                                 {
788                                         /* Ignore IK constraints - these are handled separately
789                                          * (on pose level).
790                                          */
791                                 }
792                                 else if (ELEM(con->type,
793                                               CONSTRAINT_TYPE_FOLLOWPATH,
794                                               CONSTRAINT_TYPE_CLAMPTO))
795                                 {
796                                         /* These constraints require path geometry data. */
797                                         ComponentKey target_key(&ct->tar->id, DEG_NODE_TYPE_GEOMETRY);
798                                         add_relation(target_key, constraint_op_key, cti->name);
799                                         ComponentKey target_transform_key(&ct->tar->id,
800                                                                           DEG_NODE_TYPE_TRANSFORM);
801                                         add_relation(target_transform_key, constraint_op_key, cti->name);
802                                 }
803                                 else if ((ct->tar->type == OB_ARMATURE) && (ct->subtarget[0])) {
804                                         /* bone */
805                                         if (&ct->tar->id == id) {
806                                                 /* same armature  */
807                                                 eDepsOperation_Code target_key_opcode;
808                                                 /* Using "done" here breaks in-chain deps, while using
809                                                  * "ready" here breaks most production rigs instead.
810                                                  * So, we do a compromise here, and only do this when an
811                                                  * IK chain conflict may occur.
812                                                  */
813                                                 if (root_map->has_common_root(component_subdata,
814                                                                               ct->subtarget))
815                                                 {
816                                                         target_key_opcode = DEG_OPCODE_BONE_READY;
817                                                 }
818                                                 else {
819                                                         target_key_opcode = DEG_OPCODE_BONE_DONE;
820                                                 }
821                                                 OperationKey target_key(&ct->tar->id,
822                                                                         DEG_NODE_TYPE_BONE,
823                                                                         ct->subtarget,
824                                                                         target_key_opcode);
825                                                 add_relation(target_key, constraint_op_key, cti->name);
826                                         }
827                                         else {
828                                                 /* Different armature - we can safely use the result
829                                                  * of that.
830                                                  */
831                                                 OperationKey target_key(&ct->tar->id,
832                                                                         DEG_NODE_TYPE_BONE,
833                                                                         ct->subtarget,
834                                                                         DEG_OPCODE_BONE_DONE);
835                                                 add_relation(target_key, constraint_op_key, cti->name);
836                                         }
837                                 }
838                                 else if (ELEM(ct->tar->type, OB_MESH, OB_LATTICE) &&
839                                          (ct->subtarget[0]))
840                                 {
841                                         /* Vertex group. */
842                                         /* NOTE: for now, we don't need to represent vertex groups
843                                          * separately.
844                                          */
845                                         ComponentKey target_key(&ct->tar->id, DEG_NODE_TYPE_GEOMETRY);
846                                         add_relation(target_key, constraint_op_key, cti->name);
847                                         if (ct->tar->type == OB_MESH) {
848                                                 OperationDepsNode *node2 = find_operation_node(target_key);
849                                                 if (node2 != NULL) {
850                                                         node2->customdata_mask |= CD_MASK_MDEFORMVERT;
851                                                 }
852                                         }
853                                 }
854                                 else if (con->type == CONSTRAINT_TYPE_SHRINKWRAP) {
855                                         /* Constraints which requires the target object surface. */
856                                         ComponentKey target_key(&ct->tar->id, DEG_NODE_TYPE_GEOMETRY);
857                                         add_relation(target_key, constraint_op_key, cti->name);
858                                         /* NOTE: obdata eval now doesn't necessarily depend on the
859                                          * object's transform.
860                                          */
861                                         ComponentKey target_transform_key(&ct->tar->id,
862                                                                           DEG_NODE_TYPE_TRANSFORM);
863                                         add_relation(target_transform_key, constraint_op_key, cti->name);
864                                 }
865                                 else {
866                                         /* Standard object relation. */
867                                         // TODO: loc vs rot vs scale?
868                                         if (&ct->tar->id == id) {
869                                                 /* Constraint targetting own object:
870                                                  * - This case is fine IFF we're dealing with a bone
871                                                  *   constraint pointing to its own armature. In that
872                                                  *   case, it's just transform -> bone.
873                                                  * - If however it is a real self targetting case, just
874                                                  *   make it depend on the previous constraint (or the
875                                                  *   pre-constraint state).
876                                                  */
877                                                 if ((ct->tar->type == OB_ARMATURE) &&
878                                                     (component_type == DEG_NODE_TYPE_BONE))
879                                                 {
880                                                         OperationKey target_key(&ct->tar->id,
881                                                                                 DEG_NODE_TYPE_TRANSFORM,
882                                                                                 DEG_OPCODE_TRANSFORM_FINAL);
883                                                         add_relation(target_key, constraint_op_key, cti->name);
884                                                 }
885                                                 else {
886                                                         OperationKey target_key(&ct->tar->id,
887                                                                                 DEG_NODE_TYPE_TRANSFORM,
888                                                                                 DEG_OPCODE_TRANSFORM_LOCAL);
889                                                         add_relation(target_key, constraint_op_key, cti->name);
890                                                 }
891                                         }
892                                         else {
893                                                 /* Normal object dependency. */
894                                                 OperationKey target_key(&ct->tar->id,
895                                                                         DEG_NODE_TYPE_TRANSFORM,
896                                                                         DEG_OPCODE_TRANSFORM_FINAL);
897                                                 add_relation(target_key, constraint_op_key, cti->name);
898                                         }
899                                 }
900                                 /* Constraints which needs world's matrix for transform.
901                                  * TODO(sergey): More constraints here?
902                                  */
903                                 if (ELEM(con->type,
904                                          CONSTRAINT_TYPE_ROTLIKE,
905                                          CONSTRAINT_TYPE_SIZELIKE,
906                                          CONSTRAINT_TYPE_LOCLIKE,
907                                          CONSTRAINT_TYPE_TRANSLIKE))
908                                 {
909                                         /* TODO(sergey): Add used space check. */
910                                         ComponentKey target_transform_key(&ct->tar->id,
911                                                                           DEG_NODE_TYPE_TRANSFORM);
912                                         add_relation(target_transform_key, constraint_op_key, cti->name);
913                                 }
914                         }
915                         if (cti->flush_constraint_targets) {
916                                 cti->flush_constraint_targets(con, &targets, 1);
917                         }
918                 }
919         }
920 }
921
922 void DepsgraphRelationBuilder::build_animdata(ID *id)
923 {
924         /* Animation curves and NLA. */
925         build_animdata_curves(id);
926         /* Drivers. */
927         build_animdata_drivers(id);
928 }
929
930 void DepsgraphRelationBuilder::build_animdata_curves(ID *id)
931 {
932         AnimData *adt = BKE_animdata_from_id(id);
933         if (adt == NULL) {
934                 return;
935         }
936         if (adt->action == NULL && adt->nla_tracks.first == NULL) {
937                 return;
938         }
939         /* Wire up dependency to time source. */
940         ComponentKey adt_key(id, DEG_NODE_TYPE_ANIMATION);
941         TimeSourceKey time_src_key;
942         add_relation(time_src_key, adt_key, "TimeSrc -> Animation");
943         /* Get source operations. */
944         DepsNode *node_from = get_node(adt_key);
945         BLI_assert(node_from != NULL);
946         if (node_from == NULL) {
947                 return;
948         }
949         OperationDepsNode *operation_from = node_from->get_exit_operation();
950         BLI_assert(operation_from != NULL);
951         /* Build relations from animation operation to properties it changes. */
952         if (adt->action != NULL) {
953                 build_animdata_curves_targets(id, adt_key,
954                                       operation_from,
955                                       &adt->action->curves);
956         }
957         BLI_LISTBASE_FOREACH(NlaTrack *, nlt, &adt->nla_tracks) {
958                 build_animdata_nlastrip_targets(id, adt_key,
959                                                 operation_from,
960                                                 &nlt->strips);
961         }
962 }
963
964 void DepsgraphRelationBuilder::build_animdata_curves_targets(
965         ID *id, ComponentKey &adt_key,
966         OperationDepsNode *operation_from,
967         ListBase *curves)
968 {
969         /* Iterate over all curves and build relations. */
970         PointerRNA id_ptr;
971         RNA_id_pointer_create(id, &id_ptr);
972         BLI_LISTBASE_FOREACH(FCurve *, fcu, curves) {
973                 PointerRNA ptr;
974                 PropertyRNA *prop;
975                 int index;
976                 if (!RNA_path_resolve_full(&id_ptr, fcu->rna_path,
977                                            &ptr, &prop, &index))
978                 {
979                         continue;
980                 }
981                 DepsNode *node_to = graph_->find_node_from_pointer(&ptr, prop);
982                 if (node_to == NULL) {
983                         continue;
984                 }
985                 OperationDepsNode *operation_to = node_to->get_entry_operation();
986                 /* NOTE: Special case for bones, avoid relation from animation to
987                  * each of the bones. Bone evaluation could only start from pose
988                  * init anyway.
989                  */
990                 if (operation_to->opcode == DEG_OPCODE_BONE_LOCAL) {
991                         OperationKey pose_init_key(id,
992                                                    DEG_NODE_TYPE_EVAL_POSE,
993                                                    DEG_OPCODE_POSE_INIT);
994                         add_relation(adt_key, pose_init_key, "Animation -> Prop", true);
995                         continue;
996                 }
997                 graph_->add_new_relation(operation_from, operation_to,
998                                          "Animation -> Prop",
999                                          true);
1000                 /* It is possible that animation is writing to a nested ID datablock,
1001                  * need to make sure animation is evaluated after target ID is copied.
1002                  */
1003                 if (DEG_depsgraph_use_copy_on_write()) {
1004                         const IDDepsNode *id_node_from = operation_from->owner->owner;
1005                         const IDDepsNode *id_node_to = operation_to->owner->owner;
1006                         if (id_node_from != id_node_to) {
1007                                 ComponentKey cow_key(id_node_to->id_orig,
1008                                                      DEG_NODE_TYPE_COPY_ON_WRITE);
1009                                 add_relation(cow_key, adt_key, "Target CoW -> Animation", true);
1010                         }
1011                 }
1012         }
1013 }
1014
1015 void DepsgraphRelationBuilder::build_animdata_nlastrip_targets(
1016         ID *id, ComponentKey &adt_key,
1017         OperationDepsNode *operation_from,
1018         ListBase *strips)
1019 {
1020         BLI_LISTBASE_FOREACH(NlaStrip *, strip, strips) {
1021                 if (strip->act != NULL) {
1022                         build_animdata_curves_targets(id, adt_key,
1023                                                       operation_from,
1024                                                       &strip->act->curves);
1025                 }
1026                 else if (strip->strips.first != NULL) {
1027                         build_animdata_nlastrip_targets(id, adt_key,
1028                                                         operation_from,
1029                                                         &strip->strips);
1030                 }
1031         }
1032 }
1033
1034 void DepsgraphRelationBuilder::build_animdata_drivers(ID *id)
1035 {
1036         AnimData *adt = BKE_animdata_from_id(id);
1037         if (adt == NULL) {
1038                 return;
1039         }
1040         ComponentKey adt_key(id, DEG_NODE_TYPE_ANIMATION);
1041         BLI_LISTBASE_FOREACH (FCurve *, fcu, &adt->drivers) {
1042                 OperationKey driver_key(id,
1043                                         DEG_NODE_TYPE_PARAMETERS,
1044                                         DEG_OPCODE_DRIVER,
1045                                         fcu->rna_path ? fcu->rna_path : "",
1046                                         fcu->array_index);
1047
1048                 /* create the driver's relations to targets */
1049                 build_driver(id, fcu);
1050                 /* Special case for array drivers: we can not multithread them because
1051                  * of the way how they work internally: animation system will write the
1052                  * whole array back to RNA even when changing individual array value.
1053                  *
1054                  * Some tricky things here:
1055                  * - array_index is -1 for single channel drivers, meaning we only have
1056                  *   to do some magic when array_index is not -1.
1057                  * - We do relation from next array index to a previous one, so we don't
1058                  *   have to deal with array index 0.
1059                  *
1060                  * TODO(sergey): Avoid liner lookup somehow.
1061                  */
1062                 if (fcu->array_index > 0) {
1063                         FCurve *fcu_prev = NULL;
1064                         BLI_LISTBASE_FOREACH (FCurve *, fcu_candidate, &adt->drivers) {
1065                                 /* Writing to different RNA paths is  */
1066                                 const char *rna_path = fcu->rna_path ? fcu->rna_path : "";
1067                                 if (!STREQ(fcu_candidate->rna_path, rna_path)) {
1068                                         continue;
1069                                 }
1070                                 /* We only do relation from previous fcurve to previous one. */
1071                                 if (fcu_candidate->array_index >= fcu->array_index) {
1072                                         continue;
1073                                 }
1074                                 /* Choose fcurve with highest possible array index. */
1075                                 if (fcu_prev == NULL ||
1076                                     fcu_candidate->array_index > fcu_prev->array_index)
1077                                 {
1078                                         fcu_prev = fcu_candidate;
1079                                 }
1080                         }
1081                         if (fcu_prev != NULL) {
1082                                 OperationKey prev_driver_key(id,
1083                                                              DEG_NODE_TYPE_PARAMETERS,
1084                                                              DEG_OPCODE_DRIVER,
1085                                                              fcu_prev->rna_path ? fcu_prev->rna_path : "",
1086                                                              fcu_prev->array_index);
1087                                 OperationKey driver_key(id,
1088                                                         DEG_NODE_TYPE_PARAMETERS,
1089                                                         DEG_OPCODE_DRIVER,
1090                                                         fcu->rna_path ? fcu->rna_path : "",
1091                                                         fcu->array_index);
1092                                 add_relation(prev_driver_key, driver_key, "Driver Order");
1093                         }
1094                 }
1095
1096                 /* prevent driver from occurring before own animation... */
1097                 if (adt->action || adt->nla_tracks.first) {
1098                         add_relation(adt_key, driver_key, "AnimData Before Drivers");
1099                 }
1100         }
1101 }
1102
1103 void DepsgraphRelationBuilder::build_driver(ID *id, FCurve *fcu)
1104 {
1105         ChannelDriver *driver = fcu->driver;
1106         OperationKey driver_key(id,
1107                                 DEG_NODE_TYPE_PARAMETERS,
1108                                 DEG_OPCODE_DRIVER,
1109                                 fcu->rna_path ? fcu->rna_path : "",
1110                                 fcu->array_index);
1111         /* Driver -> data components (for interleaved evaluation
1112          * bones/constraints/modifiers).
1113          */
1114         build_driver_data(id, fcu);
1115         /* Loop over variables to get the target relationships. */
1116         build_driver_variables(id, fcu);
1117         /* It's quite tricky to detect if the driver actually depends on time or
1118          * not, so for now we'll be quite conservative here about optimization and
1119          * consider all python drivers to be depending on time.
1120          */
1121         if ((driver->type == DRIVER_TYPE_PYTHON) &&
1122             python_driver_depends_on_time(driver))
1123         {
1124                 TimeSourceKey time_src_key;
1125                 add_relation(time_src_key, driver_key, "TimeSrc -> Driver");
1126         }
1127 }
1128
1129 void DepsgraphRelationBuilder::build_driver_data(ID *id, FCurve *fcu)
1130 {
1131         OperationKey driver_key(id,
1132                                 DEG_NODE_TYPE_PARAMETERS,
1133                                 DEG_OPCODE_DRIVER,
1134                                 fcu->rna_path ? fcu->rna_path : "",
1135                                 fcu->array_index);
1136         const char *rna_path = fcu->rna_path ? fcu->rna_path : "";
1137         const RNAPathKey self_key(id, rna_path);
1138         if (GS(id->name) == ID_AR && strstr(rna_path, "bones[")) {
1139                 /* Drivers on armature-level bone settings (i.e. bbone stuff),
1140                  * which will affect the evaluation of corresponding pose bones.
1141                  */
1142                 IDDepsNode *arm_node = graph_->find_id_node(id);
1143                 char *bone_name = BLI_str_quoted_substrN(rna_path, "bones[");
1144                 if (arm_node != NULL && bone_name != NULL) {
1145                         /* Find objects which use this, and make their eval callbacks
1146                          * depend on this.
1147                          */
1148                         foreach (DepsRelation *rel, arm_node->outlinks) {
1149                                 IDDepsNode *to_node = (IDDepsNode *)rel->to;
1150                                 /* We only care about objects with pose data which use this. */
1151                                 if (GS(to_node->id_orig->name) == ID_OB) {
1152                                         Object *object = (Object *)to_node->id_orig;
1153                                         // NOTE: object->pose may be NULL
1154                                         bPoseChannel *pchan = BKE_pose_channel_find_name(object->pose,
1155                                                                                          bone_name);
1156                                         if (pchan != NULL) {
1157                                                 OperationKey bone_key(&object->id,
1158                                                                       DEG_NODE_TYPE_BONE,
1159                                                                       pchan->name,
1160                                                                       DEG_OPCODE_BONE_LOCAL);
1161                                                 add_relation(driver_key,
1162                                                              bone_key,
1163                                                              "Arm Bone -> Driver -> Bone");
1164                                         }
1165                                 }
1166                         }
1167                         /* Free temp data. */
1168                         MEM_freeN(bone_name);
1169                         bone_name = NULL;
1170                 }
1171                 else {
1172                         fprintf(stderr,
1173                                 "Couldn't find armature bone name for driver path - '%s'\n",
1174                                 rna_path);
1175                 }
1176         }
1177         else {
1178                 RNAPathKey target_key(id, rna_path);
1179                 add_relation(driver_key, target_key, "Driver -> Target");
1180                 /* Similar to the case with f-curves, driver might drive a nested
1181                  * datablock, which means driver execution should wait for that
1182                  * datablock to be copied.
1183                  */
1184                 if (DEG_depsgraph_use_copy_on_write()) {
1185                         PointerRNA id_ptr;
1186                         PointerRNA ptr;
1187                         RNA_id_pointer_create(id, &id_ptr);
1188                         if (RNA_path_resolve_full(&id_ptr, fcu->rna_path, &ptr, NULL, NULL)) {
1189                                 if (id_ptr.id.data != ptr.id.data) {
1190                                         ComponentKey cow_key((ID *)ptr.id.data,
1191                                                              DEG_NODE_TYPE_COPY_ON_WRITE);
1192                                         add_relation(cow_key,
1193                                                      driver_key,
1194                                                      "Target CoW -> Driver",
1195                                                      true);
1196                                 }
1197                         }
1198                 }
1199         }
1200 }
1201
1202 void DepsgraphRelationBuilder::build_driver_variables(ID *id, FCurve *fcu)
1203 {
1204         ChannelDriver *driver = fcu->driver;
1205         OperationKey driver_key(id,
1206                                 DEG_NODE_TYPE_PARAMETERS,
1207                                 DEG_OPCODE_DRIVER,
1208                                 fcu->rna_path ? fcu->rna_path : "",
1209                                 fcu->array_index);
1210         const char *rna_path = fcu->rna_path ? fcu->rna_path : "";
1211         const RNAPathKey self_key(id, rna_path);
1212
1213         BLI_LISTBASE_FOREACH (DriverVar *, dvar, &driver->variables) {
1214                 /* Only used targets. */
1215                 DRIVER_TARGETS_USED_LOOPER(dvar)
1216                 {
1217                         if (dtar->id == NULL) {
1218                                 continue;
1219                         }
1220                         /* Special handling for directly-named bones. */
1221                         if ((dtar->flag & DTAR_FLAG_STRUCT_REF) &&
1222                             (((Object *)dtar->id)->type == OB_ARMATURE) &&
1223                             (dtar->pchan_name[0]))
1224                         {
1225                                 Object *object = (Object *)dtar->id;
1226                                 bPoseChannel *target_pchan =
1227                                         BKE_pose_channel_find_name(object->pose,
1228                                                                    dtar->pchan_name);
1229                                 if (target_pchan == NULL) {
1230                                         continue;
1231                                 }
1232                                 OperationKey variable_key(dtar->id,
1233                                                           DEG_NODE_TYPE_BONE,
1234                                                           target_pchan->name,
1235                                                           DEG_OPCODE_BONE_DONE);
1236                                 if (is_same_bone_dependency(variable_key, self_key)) {
1237                                         continue;
1238                                 }
1239                                 add_relation(variable_key, driver_key, "Bone Target -> Driver");
1240                         }
1241                         else if (dtar->flag & DTAR_FLAG_STRUCT_REF) {
1242                                 /* Get node associated with the object's transforms. */
1243                                 if (dtar->id == id) {
1244                                         /* Ignore input dependency if we're driving properties of
1245                                          * the same ID, otherwise we'll be ending up in a cyclic
1246                                          * dependency here.
1247                                          */
1248                                         continue;
1249                                 }
1250                                 OperationKey target_key(dtar->id,
1251                                                         DEG_NODE_TYPE_TRANSFORM,
1252                                                         DEG_OPCODE_TRANSFORM_FINAL);
1253                                 add_relation(target_key, driver_key, "Target -> Driver");
1254                         }
1255                         else if (dtar->rna_path) {
1256                                 RNAPathKey variable_key(dtar->id, dtar->rna_path);
1257                                 if (RNA_pointer_is_null(&variable_key.ptr)) {
1258                                         continue;
1259                                 }
1260                                 if (is_same_bone_dependency(variable_key, self_key) ||
1261                                     is_same_nodetree_node_dependency(variable_key, self_key) ||
1262                                     is_same_shapekey_dependency(variable_key, self_key))
1263                                 {
1264                                         continue;
1265                                 }
1266                                 add_relation(variable_key, driver_key, "RNA Target -> Driver");
1267                         }
1268                         else {
1269                                 if (dtar->id == id) {
1270                                         /* Ignore input dependency if we're driving properties of
1271                                          * the same ID, otherwise we'll be ending up in a cyclic
1272                                          * dependency here.
1273                                          */
1274                                         continue;
1275                                 }
1276                                 /* Resolve path to get node. */
1277                                 RNAPathKey target_key(dtar->id,
1278                                                       dtar->rna_path ? dtar->rna_path : "");
1279                                 add_relation(target_key, driver_key, "RNA Target -> Driver");
1280                         }
1281                 }
1282                 DRIVER_TARGETS_LOOPER_END
1283         }
1284 }
1285
1286 void DepsgraphRelationBuilder::build_world(World *world)
1287 {
1288         ID *world_id = &world->id;
1289         if (world_id->tag & LIB_TAG_DOIT) {
1290                 return;
1291         }
1292         world_id->tag |= LIB_TAG_DOIT;
1293
1294         build_animdata(world_id);
1295
1296         /* TODO: other settings? */
1297
1298         /* textures */
1299         build_texture_stack(world->mtex);
1300
1301         /* world's nodetree */
1302         if (world->nodetree != NULL) {
1303                 build_nodetree(world->nodetree);
1304                 ComponentKey ntree_key(&world->nodetree->id, DEG_NODE_TYPE_SHADING);
1305                 ComponentKey world_key(world_id, DEG_NODE_TYPE_SHADING);
1306                 add_relation(ntree_key, world_key, "NTree->World Shading Update");
1307         }
1308 }
1309
1310 void DepsgraphRelationBuilder::build_rigidbody(Scene *scene)
1311 {
1312         RigidBodyWorld *rbw = scene->rigidbody_world;
1313
1314         OperationKey init_key(&scene->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_REBUILD);
1315         OperationKey sim_key(&scene->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_SIM);
1316
1317         /* rel between the two sim-nodes */
1318         add_relation(init_key, sim_key, "Rigidbody [Init -> SimStep]");
1319
1320         /* set up dependencies between these operations and other builtin nodes --------------- */
1321
1322         /* time dependency */
1323         TimeSourceKey time_src_key;
1324         add_relation(time_src_key, init_key, "TimeSrc -> Rigidbody Reset/Rebuild (Optional)");
1325
1326         /* objects - simulation participants */
1327         if (rbw->group) {
1328                 BLI_LISTBASE_FOREACH (Base *, base, &rbw->group->view_layer->object_bases) {
1329                         Object *object = base->object;
1330                         if (object == NULL || object->type != OB_MESH) {
1331                                 continue;
1332                         }
1333
1334                         /* hook up evaluation order...
1335                          * 1) flushing rigidbody results follows base transforms being applied
1336                          * 2) rigidbody flushing can only be performed after simulation has been run
1337                          *
1338                          * 3) simulation needs to know base transforms to figure out what to do
1339                          *    XXX: there's probably a difference between passive and active
1340                          *         - passive don't change, so may need to know full transform...
1341                          */
1342                         OperationKey rbo_key(&object->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1343
1344                         eDepsOperation_Code trans_opcode = object->parent ? DEG_OPCODE_TRANSFORM_PARENT : DEG_OPCODE_TRANSFORM_LOCAL;
1345                         OperationKey trans_op(&object->id, DEG_NODE_TYPE_TRANSFORM, trans_opcode);
1346
1347                         add_relation(sim_key, rbo_key, "Rigidbody Sim Eval -> RBO Sync");
1348
1349                         /* if constraints exist, those depend on the result of the rigidbody sim
1350                          * - This allows constraints to modify the result of the sim (i.e. clamping)
1351                          *   while still allowing the sim to depend on some changes to the objects.
1352                          *   Also, since constraints are hooked up to the final nodes, this link
1353                          *   means that we can also fit in there too...
1354                          * - Later, it might be good to include a constraint in the stack allowing us
1355                          *   to control whether rigidbody eval gets interleaved into the constraint stack
1356                          */
1357                         if (object->constraints.first) {
1358                                 OperationKey constraint_key(&object->id,
1359                                                             DEG_NODE_TYPE_TRANSFORM,
1360                                                             DEG_OPCODE_TRANSFORM_CONSTRAINTS);
1361                                 add_relation(rbo_key, constraint_key, "RBO Sync -> Ob Constraints");
1362                         }
1363                         else {
1364                                 /* Final object transform depends on rigidbody.
1365                                  *
1366                                  * NOTE: Currently we consider final here an ubereval node.
1367                                  * If it is gone we'll need to reconsider relation here.
1368                                  */
1369                                 OperationKey uber_key(&object->id,
1370                                                       DEG_NODE_TYPE_TRANSFORM,
1371                                                       DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
1372                                 add_relation(rbo_key, uber_key, "RBO Sync -> Uber (Temp)");
1373                         }
1374
1375                         /* Needed to get correct base values. */
1376                         add_relation(trans_op, sim_key, "Base Ob Transform -> Rigidbody Sim Eval");
1377                 }
1378         }
1379
1380         /* constraints */
1381         if (rbw->constraints) {
1382                 BLI_LISTBASE_FOREACH (Base *, base, &rbw->constraints->view_layer->object_bases) {
1383                         Object *object = base->object;
1384                         if (object == NULL || !object->rigidbody_constraint) {
1385                                 continue;
1386                         }
1387
1388                         RigidBodyCon *rbc = object->rigidbody_constraint;
1389
1390                         /* final result of the constraint object's transform controls how the
1391                          * constraint affects the physics sim for these objects
1392                          */
1393                         ComponentKey trans_key(&object->id, DEG_NODE_TYPE_TRANSFORM);
1394                         OperationKey ob1_key(&rbc->ob1->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1395                         OperationKey ob2_key(&rbc->ob2->id, DEG_NODE_TYPE_TRANSFORM, DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
1396
1397                         /* - constrained-objects sync depends on the constraint-holder */
1398                         add_relation(trans_key, ob1_key, "RigidBodyConstraint -> RBC.Object_1");
1399                         add_relation(trans_key, ob2_key, "RigidBodyConstraint -> RBC.Object_2");
1400
1401                         /* - ensure that sim depends on this constraint's transform */
1402                         add_relation(trans_key, sim_key, "RigidBodyConstraint Transform -> RB Simulation");
1403                 }
1404         }
1405 }
1406
1407 void DepsgraphRelationBuilder::build_particles(Object *object)
1408 {
1409         TimeSourceKey time_src_key;
1410         OperationKey obdata_ubereval_key(&object->id,
1411                                          DEG_NODE_TYPE_GEOMETRY,
1412                                          DEG_OPCODE_GEOMETRY_UBEREVAL);
1413         OperationKey eval_init_key(&object->id,
1414                                    DEG_NODE_TYPE_EVAL_PARTICLES,
1415                                    DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
1416
1417         /* Particle systems. */
1418         BLI_LISTBASE_FOREACH (ParticleSystem *, psys, &object->particlesystem) {
1419                 ParticleSettings *part = psys->part;
1420
1421                 /* Build particle settings relations.
1422                  *
1423                  * NOTE: The call itself ensures settings are only build once.
1424                  */
1425                 build_particle_settings(part);
1426
1427                 /* This particle system. */
1428                 OperationKey psys_key(&object->id,
1429                                       DEG_NODE_TYPE_EVAL_PARTICLES,
1430                                       DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
1431                                       psys->name);
1432
1433                 /* Update particle system when settings changes. */
1434                 OperationKey particle_settings_key(&part->id,
1435                                                    DEG_NODE_TYPE_PARAMETERS,
1436                                                    DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
1437                 OperationKey particle_settings_recalc_clear_key(
1438                         &part->id,
1439                         DEG_NODE_TYPE_PARAMETERS,
1440                         DEG_OPCODE_PARTICLE_SETTINGS_RECALC_CLEAR);
1441                 OperationKey psys_settings_key(&object->id,
1442                                                DEG_NODE_TYPE_EVAL_PARTICLES,
1443                                                DEG_OPCODE_PARTICLE_SETTINGS_EVAL,
1444                                                psys->name);
1445                 add_relation(particle_settings_key, psys_settings_key, "Particle Settings Change");
1446                 add_relation(psys_settings_key, psys_key, "Particle Settings Update");
1447                 add_relation(psys_key,
1448                              particle_settings_recalc_clear_key,
1449                              "Particle Settings Recalc Clear");
1450                 add_relation(eval_init_key, psys_key, "Init -> PSys");
1451                 /* TODO(sergey): Currently particle update is just a placeholder,
1452                  * hook it to the ubereval node so particle system is getting updated
1453                  * on playback.
1454                  */
1455                 add_relation(psys_key, obdata_ubereval_key, "PSys -> UberEval");
1456                 /* Collisions */
1457                 if (part->type != PART_HAIR) {
1458                         add_collision_relations(psys_key,
1459                                                 scene_,
1460                                                 object,
1461                                                 part->collision_group,
1462                                                 true,
1463                                                 "Particle Collision");
1464                 }
1465                 else if ((psys->flag & PSYS_HAIR_DYNAMICS) &&
1466                          psys->clmd != NULL &&
1467                          psys->clmd->coll_parms != NULL)
1468                 {
1469                         add_collision_relations(psys_key,
1470                                                 scene_,
1471                                                 object,
1472                                                 psys->clmd->coll_parms->group,
1473                                                 true,
1474                                                 "Hair Collision");
1475                 }
1476                 /* Effectors. */
1477                 add_forcefield_relations(psys_key,
1478                                          scene_,
1479                                          object,
1480                                          psys,
1481                                          part->effector_weights,
1482                                          part->type == PART_HAIR,
1483                                          "Particle Field");
1484                 /* Boids .*/
1485                 if (part->boids) {
1486                         BLI_LISTBASE_FOREACH (BoidState *, state, &part->boids->states) {
1487                                 BLI_LISTBASE_FOREACH (BoidRule *, rule, &state->rules) {
1488                                         Object *ruleob = NULL;
1489                                         if (rule->type == eBoidRuleType_Avoid) {
1490                                                 ruleob = ((BoidRuleGoalAvoid *)rule)->ob;
1491                                         }
1492                                         else if (rule->type == eBoidRuleType_FollowLeader) {
1493                                                 ruleob = ((BoidRuleFollowLeader *)rule)->ob;
1494                                         }
1495                                         if (ruleob) {
1496                                                 ComponentKey ruleob_key(&ruleob->id,
1497                                                                         DEG_NODE_TYPE_TRANSFORM);
1498                                                 add_relation(ruleob_key, psys_key, "Boid Rule");
1499                                         }
1500                                 }
1501                         }
1502                 }
1503                 switch (part->ren_as) {
1504                         case PART_DRAW_OB:
1505                                 if (part->dup_ob != NULL) {
1506                                         /* Make sure object's relations are all built.  */
1507                                         build_object(NULL, part->dup_ob);
1508                                         /* Build relation for the particle visualization. */
1509                                         build_particles_visualization_object(object,
1510                                                                              psys,
1511                                                                              part->dup_ob);
1512                                 }
1513                                 break;
1514                         case PART_DRAW_GR:
1515                                 if (part->dup_group != NULL) {
1516                                         build_group(NULL, part->dup_group);
1517                                         BLI_LISTBASE_FOREACH (GroupObject *, go, &part->dup_group->gobject) {
1518                                                 build_particles_visualization_object(object,
1519                                                                                      psys,
1520                                                                                      go->ob);
1521                                         }
1522                                 }
1523                                 break;
1524                 }
1525         }
1526
1527         /* Particle depends on the object transform, so that channel is to be ready
1528          * first.
1529          *
1530          * TODO(sergey): This relation should be altered once real granular update
1531          * is implemented.
1532          */
1533         ComponentKey transform_key(&object->id, DEG_NODE_TYPE_TRANSFORM);
1534         add_relation(transform_key, obdata_ubereval_key, "Partcile Eval");
1535
1536         /* TODO(sergey): Do we need a point cache operations here? */
1537 }
1538
1539 void DepsgraphRelationBuilder::build_particle_settings(ParticleSettings *part)
1540 {
1541         ID *part_id = &part->id;
1542         if (part_id->tag & LIB_TAG_DOIT) {
1543                 return;
1544         }
1545         part_id->tag |= LIB_TAG_DOIT;
1546
1547         /* Animation data relations. */
1548         build_animdata(&part->id);
1549
1550         OperationKey eval_key(part_id,
1551                               DEG_NODE_TYPE_PARAMETERS,
1552                               DEG_OPCODE_PARTICLE_SETTINGS_EVAL);
1553         OperationKey recalc_clear_key(part_id,
1554                                      DEG_NODE_TYPE_PARAMETERS,
1555                                      DEG_OPCODE_PARTICLE_SETTINGS_RECALC_CLEAR);
1556         add_relation(eval_key, recalc_clear_key, "Particle Settings Clear Recalc");
1557 }
1558
1559 void DepsgraphRelationBuilder::build_particles_visualization_object(
1560         Object *object,
1561         ParticleSystem *psys,
1562         Object *draw_object)
1563 {
1564         OperationKey psys_key(&object->id,
1565                               DEG_NODE_TYPE_EVAL_PARTICLES,
1566                               DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
1567                               psys->name);
1568         OperationKey obdata_ubereval_key(&object->id,
1569                                          DEG_NODE_TYPE_GEOMETRY,
1570                                          DEG_OPCODE_GEOMETRY_UBEREVAL);
1571         ComponentKey dup_ob_key(&draw_object->id, DEG_NODE_TYPE_TRANSFORM);
1572         add_relation(dup_ob_key, psys_key, "Particle Object Visualization");
1573         if (draw_object->type == OB_MBALL) {
1574                 ComponentKey dup_geometry_key(&draw_object->id, DEG_NODE_TYPE_GEOMETRY);
1575                 add_relation(obdata_ubereval_key,
1576                              dup_geometry_key,
1577                              "Particle MBall Visualization");
1578         }
1579 }
1580
1581 void DepsgraphRelationBuilder::build_cloth(Object *object,
1582                                            ModifierData * /*md*/)
1583 {
1584         OperationKey cache_key(&object->id,
1585                                DEG_NODE_TYPE_CACHE,
1586                                DEG_OPCODE_GEOMETRY_CLOTH_MODIFIER);
1587         /* Cache component affects on modifier. */
1588         OperationKey modifier_key(&object->id,
1589                                   DEG_NODE_TYPE_GEOMETRY,
1590                                   DEG_OPCODE_GEOMETRY_UBEREVAL);
1591         add_relation(cache_key, modifier_key, "Cloth Cache -> Cloth");
1592 }
1593
1594 /* Shapekeys */
1595 void DepsgraphRelationBuilder::build_shapekeys(ID *obdata, Key *key)
1596 {
1597         ComponentKey obdata_key(obdata, DEG_NODE_TYPE_GEOMETRY);
1598
1599         /* attach animdata to geometry */
1600         build_animdata(&key->id);
1601
1602         if (key->adt) {
1603                 // TODO: this should really be handled in build_animdata, since many of these cases will need it
1604                 if (key->adt->action || key->adt->nla_tracks.first) {
1605                         ComponentKey adt_key(&key->id, DEG_NODE_TYPE_ANIMATION);
1606                         add_relation(adt_key, obdata_key, "Animation");
1607                 }
1608
1609                 /* NOTE: individual shapekey drivers are handled above already */
1610         }
1611
1612         /* attach to geometry */
1613         // XXX: aren't shapekeys now done as a pseudo-modifier on object?
1614         //ComponentKey key_key(&key->id, DEG_NODE_TYPE_GEOMETRY); // FIXME: this doesn't exist
1615         //add_relation(key_key, obdata_key, "Shapekeys");
1616 }
1617
1618 /**
1619  * ObData Geometry Evaluation
1620  * ==========================
1621  *
1622  * The evaluation of geometry on objects is as follows:
1623  * - The actual evaluated of the derived geometry (e.g. DerivedMesh, DispList, etc.)
1624  *   occurs in the Geometry component of the object which references this. This includes
1625  *   modifiers, and the temporary "ubereval" for geometry.
1626  * - Therefore, each user of a piece of shared geometry data ends up evaluating its own
1627  *   version of the stuff, complete with whatever modifiers it may use.
1628  *
1629  * - The datablocks for the geometry data - "obdata" (e.g. ID_ME, ID_CU, ID_LT, etc.) are used for
1630  *     1) calculating the bounding boxes of the geometry data,
1631  *     2) aggregating inward links from other objects (e.g. for text on curve, etc.)
1632  *        and also for the links coming from the shapekey datablocks
1633  * - Animation/Drivers affecting the parameters of the geometry are made to trigger
1634  *   updates on the obdata geometry component, which then trigger downstream
1635  *   re-evaluation of the individual instances of this geometry.
1636  */
1637 // TODO: Materials and lighting should probably get their own component, instead of being lumped under geometry?
1638 void DepsgraphRelationBuilder::build_obdata_geom(Object *object)
1639 {
1640         ID *obdata = (ID *)object->data;
1641
1642         /* Init operation of object-level geometry evaluation. */
1643         OperationKey geom_init_key(&object->id, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Eval Init");
1644
1645         /* get nodes for result of obdata's evaluation, and geometry evaluation on object */
1646         ComponentKey obdata_geom_key(obdata, DEG_NODE_TYPE_GEOMETRY);
1647         ComponentKey geom_key(&object->id, DEG_NODE_TYPE_GEOMETRY);
1648
1649         /* link components to each other */
1650         add_relation(obdata_geom_key, geom_key, "Object Geometry Base Data");
1651
1652         OperationKey obdata_ubereval_key(&object->id,
1653                                          DEG_NODE_TYPE_GEOMETRY,
1654                                          DEG_OPCODE_GEOMETRY_UBEREVAL);
1655
1656         /* Special case: modifiers and DerivedMesh creation queries scene for various
1657          * things like data mask to be used. We add relation here to ensure object is
1658          * never evaluated prior to Scene's CoW is ready.
1659          */
1660         OperationKey scene_key(&scene_->id,
1661                                DEG_NODE_TYPE_PARAMETERS,
1662                                DEG_OPCODE_PLACEHOLDER,
1663                                "Scene Eval");
1664         add_relation(scene_key, obdata_ubereval_key, "CoW Relation");
1665
1666         /* Modifiers */
1667         if (object->modifiers.first != NULL) {
1668                 BLI_LISTBASE_FOREACH (ModifierData *, md, &object->modifiers) {
1669                         const ModifierTypeInfo *mti = modifierType_getInfo((ModifierType)md->type);
1670                         if (mti->updateDepsgraph) {
1671                                 DepsNodeHandle handle = create_node_handle(obdata_ubereval_key);
1672                                 mti->updateDepsgraph(
1673                                         md,
1674                                         bmain_,
1675                                         scene_,
1676                                         object,
1677                                         reinterpret_cast< ::DepsNodeHandle* >(&handle));
1678                         }
1679                         if (BKE_object_modifier_use_time(object, md)) {
1680                                 TimeSourceKey time_src_key;
1681                                 add_relation(time_src_key, obdata_ubereval_key, "Time Source");
1682                         }
1683                         if (md->type == eModifierType_Cloth) {
1684                                 build_cloth(object, md);
1685                         }
1686                 }
1687         }
1688
1689         /* materials */
1690         if (object->totcol) {
1691                 for (int a = 1; a <= object->totcol; a++) {
1692                         Material *ma = give_current_material(object, a);
1693                         if (ma != NULL) {
1694                                 build_material(ma);
1695
1696                                 if (object->type == OB_MESH) {
1697                                         OperationKey material_key(&ma->id,
1698                                                                   DEG_NODE_TYPE_SHADING,
1699                                                                   DEG_OPCODE_MATERIAL_UPDATE);
1700                                         OperationKey shading_key(&object->id, DEG_NODE_TYPE_SHADING, DEG_OPCODE_SHADING);
1701                                         add_relation(material_key, shading_key, "Material Update");
1702                                 }
1703                         }
1704                 }
1705         }
1706
1707         /* geometry collision */
1708         if (ELEM(object->type, OB_MESH, OB_CURVE, OB_LATTICE)) {
1709                 // add geometry collider relations
1710         }
1711
1712         /* Make sure uber update is the last in the dependencies.
1713          *
1714          * TODO(sergey): Get rid of this node.
1715          */
1716         if (object->type != OB_ARMATURE) {
1717                 /* Armatures does no longer require uber node. */
1718                 OperationKey obdata_ubereval_key(&object->id, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_GEOMETRY_UBEREVAL);
1719                 add_relation(geom_init_key, obdata_ubereval_key, "Object Geometry UberEval");
1720         }
1721
1722         if (obdata->tag & LIB_TAG_DOIT) {
1723                 return;
1724         }
1725         obdata->tag |= LIB_TAG_DOIT;
1726
1727         /* Link object data evaluation node to exit operation. */
1728         OperationKey obdata_geom_eval_key(obdata, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Geometry Eval");
1729         OperationKey obdata_geom_done_key(obdata, DEG_NODE_TYPE_GEOMETRY, DEG_OPCODE_PLACEHOLDER, "Eval Done");
1730         add_relation(obdata_geom_eval_key, obdata_geom_done_key, "ObData Geom Eval Done");
1731
1732         /* type-specific node/links */
1733         switch (object->type) {
1734                 case OB_MESH:
1735                         /* NOTE: This is compatibility code to support particle systems
1736                          *
1737                          * for viewport being properly rendered in final render mode.
1738                          * This relation is similar to what dag_object_time_update_flags()
1739                          * was doing for mesh objects with particle system.
1740                          *
1741                          * Ideally we need to get rid of this relation.
1742                          */
1743                         if (object_particles_depends_on_time(object)) {
1744                                 TimeSourceKey time_key;
1745                                 OperationKey obdata_ubereval_key(&object->id,
1746                                                                  DEG_NODE_TYPE_GEOMETRY,
1747                                                                  DEG_OPCODE_GEOMETRY_UBEREVAL);
1748                                 add_relation(time_key, obdata_ubereval_key, "Legacy particle time");
1749                         }
1750                         break;
1751
1752                 case OB_MBALL:
1753                 {
1754                         Object *mom = BKE_mball_basis_find(scene_, object);
1755                         ComponentKey mom_geom_key(&mom->id, DEG_NODE_TYPE_GEOMETRY);
1756                         /* motherball - mom depends on children! */
1757                         if (mom == object) {
1758                                 ComponentKey mom_transform_key(&mom->id,
1759                                                                DEG_NODE_TYPE_TRANSFORM);
1760                                 add_relation(mom_transform_key,
1761                                              mom_geom_key,
1762                                              "Metaball Motherball Transform -> Geometry");
1763                         }
1764                         else {
1765                                 ComponentKey transform_key(&object->id, DEG_NODE_TYPE_TRANSFORM);
1766                                 add_relation(geom_key, mom_geom_key, "Metaball Motherball");
1767                                 add_relation(transform_key, mom_geom_key, "Metaball Motherball");
1768                         }
1769                         break;
1770                 }
1771
1772                 case OB_CURVE:
1773                 case OB_FONT:
1774                 {
1775                         Curve *cu = (Curve *)obdata;
1776
1777                         /* curve's dependencies */
1778                         // XXX: these needs geom data, but where is geom stored?
1779                         if (cu->bevobj) {
1780                                 ComponentKey bevob_key(&cu->bevobj->id, DEG_NODE_TYPE_GEOMETRY);
1781                                 build_object(NULL, cu->bevobj);
1782                                 add_relation(bevob_key, geom_key, "Curve Bevel");
1783                         }
1784                         if (cu->taperobj) {
1785                                 ComponentKey taperob_key(&cu->taperobj->id, DEG_NODE_TYPE_GEOMETRY);
1786                                 build_object(NULL, cu->taperobj);
1787                                 add_relation(taperob_key, geom_key, "Curve Taper");
1788                         }
1789                         if (object->type == OB_FONT) {
1790                                 if (cu->textoncurve) {
1791                                         ComponentKey textoncurve_key(&cu->textoncurve->id, DEG_NODE_TYPE_GEOMETRY);
1792                                         build_object(NULL, cu->textoncurve);
1793                                         add_relation(textoncurve_key, geom_key, "Text on Curve");
1794                                 }
1795                         }
1796                         break;
1797                 }
1798
1799                 case OB_SURF: /* Nurbs Surface */
1800                 {
1801                         break;
1802                 }
1803
1804                 case OB_LATTICE: /* Lattice */
1805                 {
1806                         break;
1807                 }
1808         }
1809
1810         /* ShapeKeys */
1811         Key *key = BKE_key_from_object(object);
1812         if (key) {
1813                 build_shapekeys(obdata, key);
1814         }
1815 }
1816
1817 /* Cameras */
1818 // TODO: Link scene-camera links in somehow...
1819 void DepsgraphRelationBuilder::build_camera(Object *object)
1820 {
1821         Camera *cam = (Camera *)object->data;
1822         ID *camera_id = &cam->id;
1823         if (camera_id->tag & LIB_TAG_DOIT) {
1824                 return;
1825         }
1826         camera_id->tag |= LIB_TAG_DOIT;
1827
1828         ComponentKey object_parameters_key(&object->id, DEG_NODE_TYPE_PARAMETERS);
1829         ComponentKey camera_parameters_key(camera_id, DEG_NODE_TYPE_PARAMETERS);
1830
1831         add_relation(camera_parameters_key, object_parameters_key,
1832                      "Camera -> Object");
1833
1834         /* DOF */
1835         if (cam->dof_ob != NULL) {
1836                 ComponentKey dof_ob_key(&cam->dof_ob->id, DEG_NODE_TYPE_TRANSFORM);
1837                 add_relation(dof_ob_key, object_parameters_key, "Camera DOF");
1838         }
1839 }
1840
1841 /* Lamps */
1842 void DepsgraphRelationBuilder::build_lamp(Object *object)
1843 {
1844         Lamp *la = (Lamp *)object->data;
1845         ID *lamp_id = &la->id;
1846         if (lamp_id->tag & LIB_TAG_DOIT) {
1847                 return;
1848         }
1849         lamp_id->tag |= LIB_TAG_DOIT;
1850
1851         ComponentKey object_parameters_key(&object->id, DEG_NODE_TYPE_PARAMETERS);
1852         ComponentKey lamp_parameters_key(lamp_id, DEG_NODE_TYPE_PARAMETERS);
1853
1854         add_relation(lamp_parameters_key, object_parameters_key,
1855                      "Lamp -> Object");
1856
1857         /* lamp's nodetree */
1858         if (la->nodetree != NULL) {
1859                 build_nodetree(la->nodetree);
1860                 ComponentKey nodetree_key(&la->nodetree->id, DEG_NODE_TYPE_SHADING);
1861                 add_relation(nodetree_key, lamp_parameters_key, "NTree->Lamp Parameters");
1862         }
1863         /* textures */
1864         build_texture_stack(la->mtex);
1865
1866         if (DEG_depsgraph_use_copy_on_write()) {
1867                 /* Make sure copy on write of lamp data is always properly updated for
1868                  * visible lamps.
1869                  */
1870                 OperationKey ob_copy_on_write_key(&object->id,
1871                                                   DEG_NODE_TYPE_COPY_ON_WRITE,
1872                                                   DEG_OPCODE_COPY_ON_WRITE);
1873                 OperationKey lamp_copy_on_write_key(lamp_id,
1874                                                     DEG_NODE_TYPE_COPY_ON_WRITE,
1875                                                     DEG_OPCODE_COPY_ON_WRITE);
1876                 add_relation(lamp_copy_on_write_key, ob_copy_on_write_key, "Eval Order");
1877         }
1878 }
1879
1880 void DepsgraphRelationBuilder::build_nodetree(bNodeTree *ntree)
1881 {
1882         if (ntree == NULL) {
1883                 return;
1884         }
1885         ID *ntree_id = &ntree->id;
1886         build_animdata(ntree_id);
1887         ComponentKey shading_key(ntree_id, DEG_NODE_TYPE_SHADING);
1888         /* nodetree's nodes... */
1889         BLI_LISTBASE_FOREACH (bNode *, bnode, &ntree->nodes) {
1890                 ID *id = bnode->id;
1891                 if (id == NULL) {
1892                         continue;
1893                 }
1894                 ID_Type id_type = GS(id->name);
1895                 if (id_type == ID_MA) {
1896                         build_material((Material *)bnode->id);
1897                 }
1898                 else if (id_type == ID_TE) {
1899                         build_texture((Tex *)bnode->id);
1900                 }
1901                 else if (id_type == ID_IM) {
1902                         /* nothing for now. */
1903                 }
1904                 else if (id_type == ID_OB) {
1905                         build_object(NULL, (Object *)id);
1906                 }
1907                 else if (id_type == ID_SCE) {
1908                         /* Scenes are used by compositor trees, and handled by render
1909                          * pipeline. No need to build dependencies for them here.
1910                          */
1911                 }
1912                 else if (id_type == ID_TXT) {
1913                         /* Ignore script nodes. */
1914                 }
1915                 else if (bnode->type == NODE_GROUP) {
1916                         bNodeTree *group_ntree = (bNodeTree *)id;
1917                         if ((group_ntree->id.tag & LIB_TAG_DOIT) == 0) {
1918                                 build_nodetree(group_ntree);
1919                                 group_ntree->id.tag |= LIB_TAG_DOIT;
1920                         }
1921                         ComponentKey group_shading_key(&group_ntree->id,
1922                                                        DEG_NODE_TYPE_SHADING);
1923                         add_relation(group_shading_key, shading_key, "Group Node");
1924                 }
1925                 else {
1926                         BLI_assert(!"Unknown ID type used for node");
1927                 }
1928         }
1929
1930         OperationKey shading_update_key(ntree_id,
1931                                         DEG_NODE_TYPE_SHADING,
1932                                         DEG_OPCODE_MATERIAL_UPDATE);
1933         OperationKey shading_parameters_key(ntree_id,
1934                                             DEG_NODE_TYPE_SHADING_PARAMETERS,
1935                                             DEG_OPCODE_MATERIAL_UPDATE);
1936         add_relation(shading_parameters_key, shading_update_key, "NTree Shading Parameters");
1937 }
1938
1939 /* Recursively build graph for material */
1940 void DepsgraphRelationBuilder::build_material(Material *ma)
1941 {
1942         ID *ma_id = &ma->id;
1943         if (ma_id->tag & LIB_TAG_DOIT) {
1944                 return;
1945         }
1946         ma_id->tag |= LIB_TAG_DOIT;
1947
1948         /* animation */
1949         build_animdata(ma_id);
1950
1951         /* textures */
1952         build_texture_stack(ma->mtex);
1953
1954         /* material's nodetree */
1955         if (ma->nodetree != NULL) {
1956                 build_nodetree(ma->nodetree);
1957                 OperationKey ntree_key(&ma->nodetree->id,
1958                                        DEG_NODE_TYPE_SHADING,
1959                                        DEG_OPCODE_MATERIAL_UPDATE);
1960                 OperationKey material_key(&ma->id,
1961                                           DEG_NODE_TYPE_SHADING,
1962                                           DEG_OPCODE_MATERIAL_UPDATE);
1963                 add_relation(ntree_key, material_key, "Material's NTree");
1964         }
1965 }
1966
1967 /* Recursively build graph for texture */
1968 void DepsgraphRelationBuilder::build_texture(Tex *tex)
1969 {
1970         ID *tex_id = &tex->id;
1971         if (tex_id->tag & LIB_TAG_DOIT) {
1972                 return;
1973         }
1974         tex_id->tag |= LIB_TAG_DOIT;
1975
1976         /* texture itself */
1977         build_animdata(tex_id);
1978
1979         /* texture's nodetree */
1980         build_nodetree(tex->nodetree);
1981 }
1982
1983 /* Texture-stack attached to some shading datablock */
1984 void DepsgraphRelationBuilder::build_texture_stack(MTex **texture_stack)
1985 {
1986         int i;
1987
1988         /* for now assume that all texture-stacks have same number of max items */
1989         for (i = 0; i < MAX_MTEX; i++) {
1990                 MTex *mtex = texture_stack[i];
1991                 if (mtex && mtex->tex)
1992                         build_texture(mtex->tex);
1993         }
1994 }
1995
1996 void DepsgraphRelationBuilder::build_compositor(Scene *scene)
1997 {
1998         /* For now, just a plain wrapper? */
1999         build_nodetree(scene->nodetree);
2000 }
2001
2002 void DepsgraphRelationBuilder::build_gpencil(bGPdata *gpd)
2003 {
2004         /* animation */
2005         build_animdata(&gpd->id);
2006
2007         // TODO: parent object (when that feature is implemented)
2008 }
2009
2010 void DepsgraphRelationBuilder::build_cachefile(CacheFile *cache_file)
2011 {
2012         /* Animation. */
2013         build_animdata(&cache_file->id);
2014 }
2015
2016 void DepsgraphRelationBuilder::build_mask(Mask *mask)
2017 {
2018         ID *mask_id = &mask->id;
2019         /* F-Curve animation. */
2020         build_animdata(mask_id);
2021         /* Own mask animation. */
2022         OperationKey mask_animation_key(mask_id,
2023                                         DEG_NODE_TYPE_ANIMATION,
2024                                         DEG_OPCODE_MASK_ANIMATION);
2025         TimeSourceKey time_src_key;
2026         add_relation(time_src_key, mask_animation_key, "TimeSrc -> Mask Animation");
2027         /* Final mask evaluation. */
2028         ComponentKey parameters_key(mask_id, DEG_NODE_TYPE_PARAMETERS);
2029         add_relation(mask_animation_key, parameters_key, "Mask Animation -> Mask Eval");
2030 }
2031
2032 void DepsgraphRelationBuilder::build_movieclip(MovieClip *clip)
2033 {
2034         /* Animation. */
2035         build_animdata(&clip->id);
2036 }
2037
2038 void DepsgraphRelationBuilder::build_lightprobe(Object *object)
2039 {
2040         LightProbe *probe = (LightProbe *)object->data;
2041         ID *probe_id = &probe->id;
2042         if (probe_id->tag & LIB_TAG_DOIT) {
2043                 return;
2044         }
2045         probe_id->tag |= LIB_TAG_DOIT;
2046         build_animdata(&probe->id);
2047
2048         OperationKey probe_key(probe_id,
2049                                DEG_NODE_TYPE_PARAMETERS,
2050                                DEG_OPCODE_PLACEHOLDER,
2051                                "LightProbe Eval");
2052         OperationKey object_key(&object->id,
2053                                 DEG_NODE_TYPE_PARAMETERS,
2054                                 DEG_OPCODE_PLACEHOLDER,
2055                                 "LightProbe Eval");
2056         add_relation(probe_key, object_key, "LightProbe Update");
2057 }
2058
2059 void DepsgraphRelationBuilder::build_copy_on_write_relations()
2060 {
2061         foreach (IDDepsNode *id_node, graph_->id_nodes) {
2062                 build_copy_on_write_relations(id_node);
2063         }
2064 }
2065
2066 void DepsgraphRelationBuilder::build_copy_on_write_relations(IDDepsNode *id_node)
2067 {
2068         ID *id_orig = id_node->id_orig;
2069
2070         TimeSourceKey time_source_key;
2071         OperationKey copy_on_write_key(id_orig,
2072                                        DEG_NODE_TYPE_COPY_ON_WRITE,
2073                                        DEG_OPCODE_COPY_ON_WRITE);
2074         /* XXX: This is a quick hack to make Alt-A to work. */
2075         // add_relation(time_source_key, copy_on_write_key, "Fluxgate capacitor hack");
2076         /* Resat of code is using rather low level trickery, so need to get some
2077          * explicit pointers.
2078          */
2079         DepsNode *node_cow = find_node(copy_on_write_key);
2080         OperationDepsNode *op_cow = node_cow->get_exit_operation();
2081         /* Plug any other components to this one. */
2082         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, id_node->components)
2083         {
2084                 if (comp_node->type == DEG_NODE_TYPE_COPY_ON_WRITE) {
2085                         /* Copy-on-write component never depends on itself. */
2086                         continue;
2087                 }
2088                 if (!comp_node->depends_on_cow()) {
2089                         /* Component explicitly requests to not add relation. */
2090                         continue;
2091                 }
2092                 /* All entry operations of each component should wait for a proper
2093                  * copy of ID.
2094                  */
2095                 OperationDepsNode *op_entry = comp_node->get_entry_operation();
2096                 if (op_entry != NULL) {
2097                         graph_->add_new_relation(op_cow, op_entry, "CoW Dependency");
2098                 }
2099                 /* All dangling operations should also be executed after copy-on-write. */
2100                 GHASH_FOREACH_BEGIN(OperationDepsNode *, op_node, comp_node->operations_map)
2101                 {
2102                         if (op_node->inlinks.size() == 0) {
2103                                 graph_->add_new_relation(op_cow, op_node, "CoW Dependency");
2104                         }
2105                 }
2106                 GHASH_FOREACH_END();
2107                 /* NOTE: We currently ignore implicit relations to an external
2108                  * datablocks for copy-on-write operations. This means, for example,
2109                  * copy-on-write component of Object will not wait for copy-on-write
2110                  * component of it's Mesh. This is because pointers are all known
2111                  * already so remapping will happen all correct. And then If some object
2112                  * evaluation step needs geometry, it will have transitive dependency
2113                  * to Mesh copy-on-write already.
2114                  */
2115         }
2116         GHASH_FOREACH_END();
2117         /* TODO(sergey): This solves crash for now, but causes too many
2118          * updates potentially.
2119          */
2120         if (GS(id_orig->name) == ID_OB) {
2121                 Object *object = (Object *)id_orig;
2122                 ID *object_data_id = (ID *)object->data;
2123                 if (object_data_id != NULL) {
2124                         OperationKey data_copy_on_write_key(object_data_id,
2125                                                             DEG_NODE_TYPE_COPY_ON_WRITE,
2126                                                             DEG_OPCODE_COPY_ON_WRITE);
2127                         add_relation(data_copy_on_write_key, copy_on_write_key, "Eval Order");
2128                 }
2129                 else {
2130                         BLI_assert(object->type == OB_EMPTY);
2131                 }
2132         }
2133 }
2134
2135 }  // namespace DEG