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