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