Depsgraph: Add missing movie clip dopesheet invalidation
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_nodes.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_nodes.cc
28  *  \ingroup depsgraph
29  *
30  * Methods for constructing depsgraph's nodes
31  */
32
33 #include "intern/builder/deg_builder_nodes.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_string.h"
42 #include "BLI_utildefines.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_cachefile_types.h"
49 #include "DNA_camera_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_object_types.h"
65 #include "DNA_rigidbody_types.h"
66 #include "DNA_scene_types.h"
67 #include "DNA_texture_types.h"
68 #include "DNA_world_types.h"
69
70 #include "BKE_action.h"
71 #include "BKE_armature.h"
72 #include "BKE_animsys.h"
73 #include "BKE_constraint.h"
74 #include "BKE_curve.h"
75 #include "BKE_depsgraph.h"
76 #include "BKE_effect.h"
77 #include "BKE_fcurve.h"
78 #include "BKE_idcode.h"
79 #include "BKE_group.h"
80 #include "BKE_key.h"
81 #include "BKE_lattice.h"
82 #include "BKE_library.h"
83 #include "BKE_main.h"
84 #include "BKE_mask.h"
85 #include "BKE_material.h"
86 #include "BKE_mesh.h"
87 #include "BKE_mball.h"
88 #include "BKE_modifier.h"
89 #include "BKE_movieclip.h"
90 #include "BKE_node.h"
91 #include "BKE_object.h"
92 #include "BKE_particle.h"
93 #include "BKE_rigidbody.h"
94 #include "BKE_sound.h"
95 #include "BKE_texture.h"
96 #include "BKE_tracking.h"
97 #include "BKE_world.h"
98
99 #include "RNA_access.h"
100 #include "RNA_types.h"
101 } /* extern "C" */
102
103 #include "DEG_depsgraph.h"
104 #include "DEG_depsgraph_build.h"
105
106 #include "intern/builder/deg_builder.h"
107 #include "intern/nodes/deg_node.h"
108 #include "intern/nodes/deg_node_component.h"
109 #include "intern/nodes/deg_node_operation.h"
110 #include "intern/depsgraph_types.h"
111 #include "intern/depsgraph_intern.h"
112 #include "util/deg_util_foreach.h"
113
114 namespace DEG {
115
116 namespace {
117
118 struct BuilderWalkUserData {
119         DepsgraphNodeBuilder *builder;
120         Scene *scene;
121 };
122
123 static void modifier_walk(void *user_data,
124                           struct Object * /*ob*/,
125                           struct Object **obpoin,
126                           int /*cb_flag*/)
127 {
128         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
129         if (*obpoin) {
130                 data->builder->build_object(data->scene, 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(data->scene, NULL, (Object *)id);
144                 }
145         }
146 }
147
148 }  /* namespace */
149
150 /* ************ */
151 /* Node Builder */
152
153 /* **** General purpose functions **** */
154
155 DepsgraphNodeBuilder::DepsgraphNodeBuilder(Main *bmain, Depsgraph *graph) :
156     m_bmain(bmain),
157     m_graph(graph)
158 {
159 }
160
161 DepsgraphNodeBuilder::~DepsgraphNodeBuilder()
162 {
163 }
164
165 IDDepsNode *DepsgraphNodeBuilder::add_id_node(ID *id)
166 {
167         return m_graph->add_id_node(id, id->name);
168 }
169
170 TimeSourceDepsNode *DepsgraphNodeBuilder::add_time_source()
171 {
172         return m_graph->add_time_source();
173 }
174
175 ComponentDepsNode *DepsgraphNodeBuilder::add_component_node(
176         ID *id,
177         eDepsNode_Type comp_type,
178         const char *comp_name)
179 {
180         IDDepsNode *id_node = add_id_node(id);
181         ComponentDepsNode *comp_node = id_node->add_component(comp_type, comp_name);
182         comp_node->owner = id_node;
183         return comp_node;
184 }
185
186 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
187         ComponentDepsNode *comp_node,
188         const DepsEvalOperationCb& op,
189         eDepsOperation_Code opcode,
190         const char *name,
191         int name_tag)
192 {
193         OperationDepsNode *op_node = comp_node->has_operation(opcode,
194                                                               name,
195                                                               name_tag);
196         if (op_node == NULL) {
197                 op_node = comp_node->add_operation(op, opcode, name, name_tag);
198                 m_graph->operations.push_back(op_node);
199         }
200         else {
201                 fprintf(stderr,
202                         "add_operation: Operation already exists - %s has %s at %p\n",
203                         comp_node->identifier().c_str(),
204                         op_node->identifier().c_str(),
205                         op_node);
206                 BLI_assert(!"Should not happen!");
207         }
208         return op_node;
209 }
210
211 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
212         ID *id,
213         eDepsNode_Type comp_type,
214         const char *comp_name,
215         const DepsEvalOperationCb& op,
216         eDepsOperation_Code opcode,
217         const char *name,
218         int name_tag)
219 {
220         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
221         return add_operation_node(comp_node, op, opcode, name, name_tag);
222 }
223
224 OperationDepsNode *DepsgraphNodeBuilder::add_operation_node(
225         ID *id,
226         eDepsNode_Type comp_type,
227         const DepsEvalOperationCb& op,
228         eDepsOperation_Code opcode,
229         const char *name,
230         int name_tag)
231 {
232         return add_operation_node(id,
233                                   comp_type,
234                                   "",
235                                   op,
236                                   opcode,
237                                   name,
238                                   name_tag);
239 }
240
241 bool DepsgraphNodeBuilder::has_operation_node(ID *id,
242                                               eDepsNode_Type comp_type,
243                                               const char *comp_name,
244                                               eDepsOperation_Code opcode,
245                                               const char *name,
246                                               int name_tag)
247 {
248         return find_operation_node(id,
249                                    comp_type,
250                                    comp_name,
251                                    opcode,
252                                    name,
253                                    name_tag) != NULL;
254 }
255
256 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
257         ID *id,
258         eDepsNode_Type comp_type,
259         const char *comp_name,
260         eDepsOperation_Code opcode,
261         const char *name,
262         int name_tag)
263 {
264         ComponentDepsNode *comp_node = add_component_node(id, comp_type, comp_name);
265         return comp_node->has_operation(opcode, name, name_tag);
266 }
267
268 OperationDepsNode *DepsgraphNodeBuilder::find_operation_node(
269         ID *id,
270         eDepsNode_Type comp_type,
271         eDepsOperation_Code opcode,
272         const char *name,
273         int name_tag)
274 {
275         return find_operation_node(id, comp_type, "", opcode, name, name_tag);
276 }
277
278 /* **** Build functions for entity nodes **** */
279
280 void DepsgraphNodeBuilder::begin_build(Main *bmain) {
281         /* LIB_TAG_DOIT is used to indicate whether node for given ID was already
282          * created or not. This flag is being set in add_id_node(), so functions
283          * shouldn't bother with setting it, they only might query this flag when
284          * needed.
285          */
286         BKE_main_id_tag_all(bmain, LIB_TAG_DOIT, false);
287         /* XXX nested node trees are not included in tag-clearing above,
288          * so we need to do this manually.
289          */
290         FOREACH_NODETREE(bmain, nodetree, id)
291         {
292                 if (id != (ID *)nodetree) {
293                         nodetree->id.tag &= ~LIB_TAG_DOIT;
294                 }
295         }
296         FOREACH_NODETREE_END;
297 }
298
299 void DepsgraphNodeBuilder::build_group(Scene *scene,
300                                        Base *base,
301                                        Group *group)
302 {
303         ID *group_id = &group->id;
304         if (group_id->tag & LIB_TAG_DOIT) {
305                 return;
306         }
307         group_id->tag |= LIB_TAG_DOIT;
308
309         LINKLIST_FOREACH (GroupObject *, go, &group->gobject) {
310                 build_object(scene, base, go->ob);
311         }
312 }
313
314 void DepsgraphNodeBuilder::build_object(Scene *scene, Base *base, Object *ob)
315 {
316         const bool has_object = (ob->id.tag & LIB_TAG_DOIT);
317         IDDepsNode *id_node = (has_object)
318                 ? m_graph->find_id_node(&ob->id)
319                 : add_id_node(&ob->id);
320         /* Update node layers.
321          * Do it for both new and existing ID nodes. This is so because several
322          * bases might be sharing same object.
323          */
324         if (base != NULL) {
325                 id_node->layers |= base->lay;
326         }
327         if (ob->type == OB_CAMERA) {
328                 /* Camera should always be updated, it used directly by viewport.
329                  *
330                  * TODO(sergey): Make it only for active scene camera.
331                  */
332                 id_node->layers |= (unsigned int)(-1);
333         }
334         /* Skip rest of components if the ID node was already there. */
335         if (has_object) {
336                 return;
337         }
338         ob->id.tag |= LIB_TAG_DOIT;
339         ob->customdata_mask = 0;
340
341         /* Standard components. */
342         build_object_transform(scene, ob);
343
344         if (ob->parent != NULL) {
345                 build_object(scene, NULL, ob->parent);
346         }
347         if (ob->modifiers.first != NULL) {
348                 BuilderWalkUserData data;
349                 data.builder = this;
350                 data.scene = scene;
351                 modifiers_foreachObjectLink(ob, modifier_walk, &data);
352         }
353         if (ob->constraints.first != NULL) {
354                 BuilderWalkUserData data;
355                 data.builder = this;
356                 data.scene = scene;
357                 BKE_constraints_id_loop(&ob->constraints, constraint_walk, &data);
358         }
359
360         /* Object data. */
361         if (ob->data != NULL) {
362                 /* type-specific data... */
363                 switch (ob->type) {
364                         case OB_MESH:     /* Geometry */
365                         case OB_CURVE:
366                         case OB_FONT:
367                         case OB_SURF:
368                         case OB_MBALL:
369                         case OB_LATTICE:
370                                 build_obdata_geom(scene, ob);
371                                 /* TODO(sergey): Only for until we support granular
372                                  * update of curves.
373                                  */
374                                 if (ob->type == OB_FONT) {
375                                         Curve *curve = (Curve *)ob->data;
376                                         if (curve->textoncurve) {
377                                                 id_node->eval_flags |= DAG_EVAL_NEED_CURVE_PATH;
378                                         }
379                                 }
380                                 break;
381
382                         case OB_ARMATURE: /* Pose */
383                                 if (ID_IS_LINKED_DATABLOCK(ob) && ob->proxy_from != NULL) {
384                                         build_proxy_rig(ob);
385                                 }
386                                 else {
387                                         build_rig(scene, ob);
388                                 }
389                                 break;
390
391                         case OB_LAMP:   /* Lamp */
392                                 build_lamp(ob);
393                                 break;
394
395                         case OB_CAMERA: /* Camera */
396                                 build_camera(ob);
397                                 break;
398
399                         default:
400                         {
401                                 ID *obdata = (ID *)ob->data;
402                                 if ((obdata->tag & LIB_TAG_DOIT) == 0) {
403                                         build_animdata(obdata);
404                                 }
405                                 break;
406                         }
407                 }
408         }
409
410         /* Build animation data,
411          *
412          * Do it now because it's possible object data will affect
413          * on object's level animation, for example in case of rebuilding
414          * pose for proxy.
415          */
416         build_animdata(&ob->id);
417
418         /* particle systems */
419         if (ob->particlesystem.first != NULL) {
420                 build_particles(scene, ob);
421         }
422
423         /* Grease pencil. */
424         if (ob->gpd != NULL) {
425                 build_gpencil(ob->gpd);
426         }
427
428         /* Object that this is a proxy for. */
429         if (ob->proxy) {
430                 ob->proxy->proxy_from = ob;
431                 build_object(scene, base, ob->proxy);
432         }
433
434         /* Object dupligroup. */
435         if (ob->dup_group != NULL) {
436                 build_group(scene, base, ob->dup_group);
437         }
438 }
439
440 void DepsgraphNodeBuilder::build_object_transform(Scene *scene, Object *ob)
441 {
442         OperationDepsNode *op_node;
443
444         /* local transforms (from transform channels - loc/rot/scale + deltas) */
445         op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
446                                      function_bind(BKE_object_eval_local_transform, _1, scene, ob),
447                                      DEG_OPCODE_TRANSFORM_LOCAL);
448         op_node->set_as_entry();
449
450         /* object parent */
451         if (ob->parent) {
452                 add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
453                                    function_bind(BKE_object_eval_parent, _1, scene, ob),
454                                    DEG_OPCODE_TRANSFORM_PARENT);
455         }
456
457         /* object constraints */
458         if (ob->constraints.first) {
459                 build_object_constraints(scene, ob);
460         }
461
462         /* Temporary uber-update node, which does everything.
463          * It is for the being we're porting old dependencies into the new system.
464          * We'll get rid of this node as soon as all the granular update functions
465          * are filled in.
466          *
467          * TODO(sergey): Get rid of this node.
468          */
469         add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
470                            function_bind(BKE_object_eval_uber_transform, _1, scene, ob),
471                            DEG_OPCODE_TRANSFORM_OBJECT_UBEREVAL);
472
473         /* object transform is done */
474         op_node = add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
475                                      function_bind(BKE_object_eval_done, _1, ob),
476                                      DEG_OPCODE_TRANSFORM_FINAL);
477         op_node->set_as_exit();
478 }
479
480 /**
481  * Constraints Graph Notes
482  *
483  * For constraints, we currently only add a operation node to the Transform
484  * or Bone components (depending on whichever type of owner we have).
485  * This represents the entire constraints stack, which is for now just
486  * executed as a single monolithic block. At least initially, this should
487  * be sufficient for ensuring that the porting/refactoring process remains
488  * manageable.
489  *
490  * However, when the time comes for developing "node-based" constraints,
491  * we'll need to split this up into pre/post nodes for "constraint stack
492  * evaluation" + operation nodes for each constraint (i.e. the contents
493  * of the loop body used in the current "solve_constraints()" operation).
494  *
495  * -- Aligorith, August 2013
496  */
497 void DepsgraphNodeBuilder::build_object_constraints(Scene *scene, Object *ob)
498 {
499         /* create node for constraint stack */
500         add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
501                            function_bind(BKE_object_eval_constraints, _1, scene, ob),
502                            DEG_OPCODE_TRANSFORM_CONSTRAINTS);
503 }
504
505 /**
506  * Build graph nodes for AnimData block
507  * \param id: ID-Block which hosts the AnimData
508  */
509 void DepsgraphNodeBuilder::build_animdata(ID *id)
510 {
511         AnimData *adt = BKE_animdata_from_id(id);
512
513         if (adt == NULL)
514                 return;
515
516         /* animation */
517         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
518                 // XXX: Hook up specific update callbacks for special properties which may need it...
519
520                 /* actions and NLA - as a single unit for now, as it gets complicated to schedule otherwise */
521                 if ((adt->action) || (adt->nla_tracks.first)) {
522                         /* create the node */
523                         add_operation_node(id, DEG_NODE_TYPE_ANIMATION,
524                                            function_bind(BKE_animsys_eval_animdata, _1, id),
525                                            DEG_OPCODE_ANIMATION, id->name);
526
527                         // TODO: for each channel affected, we might also want to add some support for running RNA update callbacks on them
528                         // (which will be needed for proper handling of drivers later)
529                 }
530
531                 /* drivers */
532                 LINKLIST_FOREACH (FCurve *, fcu, &adt->drivers) {
533                         /* create driver */
534                         build_driver(id, fcu);
535                 }
536         }
537 }
538
539 /**
540  * Build graph node(s) for Driver
541  * \param id: ID-Block that driver is attached to
542  * \param fcu: Driver-FCurve
543  */
544 OperationDepsNode *DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcu)
545 {
546         /* Create data node for this driver */
547         /* TODO(sergey): Avoid creating same operation multiple times,
548          * in the future we need to avoid lookup of the operation as well
549          * and use some tagging magic instead.
550          */
551         OperationDepsNode *driver_op = find_operation_node(id,
552                                                            DEG_NODE_TYPE_PARAMETERS,
553                                                            DEG_OPCODE_DRIVER,
554                                                            fcu->rna_path ? fcu->rna_path : "",
555                                                            fcu->array_index);
556
557         if (driver_op == NULL) {
558                 driver_op = add_operation_node(id,
559                                                DEG_NODE_TYPE_PARAMETERS,
560                                                function_bind(BKE_animsys_eval_driver, _1, id, fcu),
561                                                DEG_OPCODE_DRIVER,
562                                                fcu->rna_path ? fcu->rna_path : "",
563                                                fcu->array_index);
564         }
565
566         /* return driver node created */
567         return driver_op;
568 }
569
570 /* Recursively build graph for world */
571 void DepsgraphNodeBuilder::build_world(World *world)
572 {
573         ID *world_id = &world->id;
574         if (world_id->tag & LIB_TAG_DOIT) {
575                 return;
576         }
577
578         build_animdata(world_id);
579
580         /* world itself */
581         add_component_node(world_id, DEG_NODE_TYPE_PARAMETERS);
582         add_operation_node(world_id,
583                            DEG_NODE_TYPE_PARAMETERS,
584                            NULL,
585                            DEG_OPCODE_PARAMETERS_EVAL);
586
587         /* textures */
588         build_texture_stack(world->mtex);
589
590         /* world's nodetree */
591         if (world->nodetree) {
592                 build_nodetree(world->nodetree);
593         }
594 }
595
596 /* Rigidbody Simulation - Scene Level */
597 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
598 {
599         RigidBodyWorld *rbw = scene->rigidbody_world;
600
601         /**
602          * Rigidbody Simulation Nodes
603          * ==========================
604          *
605          * There are 3 nodes related to Rigidbody Simulation:
606          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the simulation
607          *    needs to be rebuilt (mainly after file reload, or moving back to start frame)
608          * 2) "Do Simulation" - perform a simulation step - interleaved between the evaluation
609          *    steps for clusters of objects (i.e. between those affected and/or not affected by
610          *    the sim for instance)
611          *
612          * 3) "Pull Results" - grab the specific transforms applied for a specific object -
613          *    performed as part of object's transform-stack building
614          */
615
616         /* create nodes ------------------------------------------------------------------------ */
617         /* XXX: is this the right component, or do we want to use another one instead? */
618
619         /* init/rebuild operation */
620         /*OperationDepsNode *init_node =*/ add_operation_node(&scene->id, DEG_NODE_TYPE_TRANSFORM,
621                                                               function_bind(BKE_rigidbody_rebuild_sim, _1, scene),
622                                                               DEG_OPCODE_RIGIDBODY_REBUILD);
623
624         /* do-sim operation */
625         // XXX: what happens if we need to split into several groups?
626         OperationDepsNode *sim_node     = add_operation_node(&scene->id, DEG_NODE_TYPE_TRANSFORM,
627                                                              function_bind(BKE_rigidbody_eval_simulation, _1, scene),
628                                                              DEG_OPCODE_RIGIDBODY_SIM);
629
630         /* XXX: For now, the sim node is the only one that really matters here. If any other
631          * sims get added later, we may have to remove these hacks...
632          */
633         sim_node->owner->entry_operation = sim_node;
634         sim_node->owner->exit_operation  = sim_node;
635
636
637         /* objects - simulation participants */
638         if (rbw->group) {
639                 LINKLIST_FOREACH (GroupObject *, go, &rbw->group->gobject) {
640                         Object *ob = go->ob;
641
642                         if (!ob || (ob->type != OB_MESH))
643                                 continue;
644
645                         /* 2) create operation for flushing results */
646                         /* object's transform component - where the rigidbody operation lives */
647                         add_operation_node(&ob->id, DEG_NODE_TYPE_TRANSFORM,
648                                            function_bind(BKE_rigidbody_object_sync_transforms, _1, scene, ob),
649                                            DEG_OPCODE_RIGIDBODY_TRANSFORM_COPY);
650                 }
651         }
652 }
653
654 void DepsgraphNodeBuilder::build_particles(Scene *scene, Object *ob)
655 {
656         /**
657          * Particle Systems Nodes
658          * ======================
659          *
660          * There are two types of nodes associated with representing
661          * particle systems:
662          *  1) Component (EVAL_PARTICLES) - This is the particle-system
663          *     evaluation context for an object. It acts as the container
664          *     for all the nodes associated with a particular set of particle
665          *     systems.
666          *  2) Particle System Eval Operation - This operation node acts as a
667          *     blackbox evaluation step for one particle system referenced by
668          *     the particle systems stack. All dependencies link to this operation.
669          */
670
671         /* component for all particle systems */
672         ComponentDepsNode *psys_comp =
673                 add_component_node(&ob->id, DEG_NODE_TYPE_EVAL_PARTICLES);
674
675         add_operation_node(psys_comp,
676                            function_bind(BKE_particle_system_eval_init,
677                                          _1,
678                                          scene,
679                                          ob),
680                            DEG_OPCODE_PARTICLE_SYSTEM_EVAL_INIT);
681
682         /* particle systems */
683         LINKLIST_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
684                 ParticleSettings *part = psys->part;
685
686                 /* particle settings */
687                 // XXX: what if this is used more than once!
688                 build_animdata(&part->id);
689
690                 /* this particle system */
691                 // TODO: for now, this will just be a placeholder "ubereval" node
692                 add_operation_node(psys_comp,
693                                    NULL,
694                                    DEG_OPCODE_PARTICLE_SYSTEM_EVAL,
695                                    psys->name);
696         }
697
698         /* pointcache */
699         // TODO...
700 }
701
702 void DepsgraphNodeBuilder::build_cloth(Scene *scene, Object *object)
703 {
704         ComponentDepsNode *cache_comp = add_component_node(&object->id,
705                                                            DEG_NODE_TYPE_CACHE);
706         add_operation_node(cache_comp,
707                            function_bind(BKE_object_eval_cloth,
708                                          _1,
709                                          scene,
710                                          object),
711                            DEG_OPCODE_GEOMETRY_CLOTH_MODIFIER);
712 }
713
714 /* Shapekeys */
715 void DepsgraphNodeBuilder::build_shapekeys(Key *key)
716 {
717         build_animdata(&key->id);
718         add_operation_node(&key->id,
719                            DEG_NODE_TYPE_GEOMETRY,
720                            NULL,
721                            DEG_OPCODE_GEOMETRY_SHAPEKEY);
722 }
723
724 /* ObData Geometry Evaluation */
725 // XXX: what happens if the datablock is shared!
726 void DepsgraphNodeBuilder::build_obdata_geom(Scene *scene, Object *ob)
727 {
728         ID *obdata = (ID *)ob->data;
729         OperationDepsNode *op_node;
730
731         /* TODO(sergey): This way using this object's properties as driver target
732          * works fine.
733          *
734          * Does this depend on other nodes?
735          */
736         op_node = add_operation_node(&ob->id,
737                                      DEG_NODE_TYPE_PARAMETERS,
738                                      NULL,
739                                      DEG_OPCODE_PARAMETERS_EVAL);
740         op_node->set_as_exit();
741
742         /* Temporary uber-update node, which does everything.
743          * It is for the being we're porting old dependencies into the new system.
744          * We'll get rid of this node as soon as all the granular update functions
745          * are filled in.
746          *
747          * TODO(sergey): Get rid of this node.
748          */
749         op_node = add_operation_node(&ob->id,
750                                      DEG_NODE_TYPE_GEOMETRY,
751                                      function_bind(BKE_object_eval_uber_data, _1, scene, ob),
752                                      DEG_OPCODE_GEOMETRY_UBEREVAL);
753         op_node->set_as_exit();
754
755         op_node = add_operation_node(&ob->id,
756                                      DEG_NODE_TYPE_GEOMETRY,
757                                      NULL,
758                                      DEG_OPCODE_PLACEHOLDER,
759                                      "Eval Init");
760         op_node->set_as_entry();
761
762         // TODO: "Done" operation
763
764         /* Cloyth modifier. */
765         LINKLIST_FOREACH (ModifierData *, md, &ob->modifiers) {
766                 if (md->type == eModifierType_Cloth) {
767                         build_cloth(scene, ob);
768                 }
769         }
770
771         /* materials */
772         for (int a = 1; a <= ob->totcol; a++) {
773                 Material *ma = give_current_material(ob, a);
774                 if (ma != NULL) {
775                         build_material(ma);
776                 }
777         }
778
779         /* geometry collision */
780         if (ELEM(ob->type, OB_MESH, OB_CURVE, OB_LATTICE)) {
781                 // add geometry collider relations
782         }
783
784         if (obdata->tag & LIB_TAG_DOIT) {
785                 return;
786         }
787
788         /* ShapeKeys */
789         Key *key = BKE_key_from_object(ob);
790         if (key) {
791                 build_shapekeys(key);
792         }
793
794         build_animdata(obdata);
795
796         /* Nodes for result of obdata's evaluation, and geometry
797          * evaluation on object.
798          */
799         switch (ob->type) {
800                 case OB_MESH:
801                 {
802                         //Mesh *me = (Mesh *)ob->data;
803
804                         /* evaluation operations */
805                         op_node = add_operation_node(obdata,
806                                                      DEG_NODE_TYPE_GEOMETRY,
807                                                      function_bind(BKE_mesh_eval_geometry,
808                                                                    _1,
809                                                                    (Mesh *)obdata),
810                                                      DEG_OPCODE_PLACEHOLDER,
811                                                      "Geometry Eval");
812                         op_node->set_as_entry();
813                         break;
814                 }
815
816                 case OB_MBALL:
817                 {
818                         Object *mom = BKE_mball_basis_find(scene, ob);
819
820                         /* Motherball - mom depends on children! */
821                         if (mom == ob) {
822                                 /* metaball evaluation operations */
823                                 /* NOTE: only the motherball gets evaluated! */
824                                 op_node = add_operation_node(obdata,
825                                                              DEG_NODE_TYPE_GEOMETRY,
826                                                              function_bind(BKE_mball_eval_geometry,
827                                                                            _1,
828                                                                            (MetaBall *)obdata),
829                                                              DEG_OPCODE_PLACEHOLDER,
830                                                              "Geometry Eval");
831                                 op_node->set_as_entry();
832                         }
833                         break;
834                 }
835
836                 case OB_CURVE:
837                 case OB_SURF:
838                 case OB_FONT:
839                 {
840                         /* Curve/nurms evaluation operations. */
841                         /* - calculate curve geometry (including path) */
842                         op_node = add_operation_node(obdata,
843                                                      DEG_NODE_TYPE_GEOMETRY,
844                                                      function_bind(BKE_curve_eval_geometry,
845                                                                    _1,
846                                                                    (Curve *)obdata),
847                                                                    DEG_OPCODE_PLACEHOLDER,
848                                                                    "Geometry Eval");
849                         op_node->set_as_entry();
850
851                         /* Make sure objects used for bevel.taper are in the graph.
852                          * NOTE: This objects might be not linked to the scene.
853                          */
854                         Curve *cu = (Curve *)obdata;
855                         if (cu->bevobj != NULL) {
856                                 build_object(scene, NULL, cu->bevobj);
857                         }
858                         if (cu->taperobj != NULL) {
859                                 build_object(scene, NULL, cu->taperobj);
860                         }
861                         if (ob->type == OB_FONT && cu->textoncurve != NULL) {
862                                 build_object(scene, NULL, cu->textoncurve);
863                         }
864                         break;
865                 }
866
867                 case OB_LATTICE:
868                 {
869                         /* Lattice evaluation operations. */
870                         op_node = add_operation_node(obdata,
871                                                      DEG_NODE_TYPE_GEOMETRY,
872                                                      function_bind(BKE_lattice_eval_geometry,
873                                                                    _1,
874                                                                    (Lattice *)obdata),
875                                                                    DEG_OPCODE_PLACEHOLDER,
876                                                                    "Geometry Eval");
877                         op_node->set_as_entry();
878                         break;
879                 }
880         }
881
882         op_node = add_operation_node(obdata, DEG_NODE_TYPE_GEOMETRY, NULL,
883                                      DEG_OPCODE_PLACEHOLDER, "Eval Done");
884         op_node->set_as_exit();
885
886         /* Parameters for driver sources. */
887         add_operation_node(obdata,
888                            DEG_NODE_TYPE_PARAMETERS,
889                            NULL,
890                            DEG_OPCODE_PARAMETERS_EVAL);
891 }
892
893 /* Cameras */
894 void DepsgraphNodeBuilder::build_camera(Object *ob)
895 {
896         /* TODO: Link scene-camera links in somehow... */
897         Camera *cam = (Camera *)ob->data;
898         ID *camera_id = &cam->id;
899         if (camera_id->tag & LIB_TAG_DOIT) {
900                 return;
901         }
902
903         build_animdata(&cam->id);
904
905         add_operation_node(camera_id,
906                            DEG_NODE_TYPE_PARAMETERS,
907                            NULL,
908                            DEG_OPCODE_PARAMETERS_EVAL);
909
910         if (cam->dof_ob != NULL) {
911                 /* TODO(sergey): For now parametrs are on object level. */
912                 add_operation_node(&ob->id, DEG_NODE_TYPE_PARAMETERS, NULL,
913                                    DEG_OPCODE_PLACEHOLDER, "Camera DOF");
914         }
915 }
916
917 /* Lamps */
918 void DepsgraphNodeBuilder::build_lamp(Object *ob)
919 {
920         Lamp *la = (Lamp *)ob->data;
921         ID *lamp_id = &la->id;
922         if (lamp_id->tag & LIB_TAG_DOIT) {
923                 return;
924         }
925
926         build_animdata(&la->id);
927
928         /* node for obdata */
929         add_component_node(lamp_id, DEG_NODE_TYPE_PARAMETERS);
930
931         /* TODO(sergey): Is it really how we're supposed to work with drivers? */
932         add_operation_node(lamp_id,
933                            DEG_NODE_TYPE_PARAMETERS,
934                            NULL,
935                            DEG_OPCODE_PARAMETERS_EVAL);
936
937         /* lamp's nodetree */
938         if (la->nodetree) {
939                 build_nodetree(la->nodetree);
940         }
941
942         /* textures */
943         build_texture_stack(la->mtex);
944 }
945
946 void DepsgraphNodeBuilder::build_nodetree(bNodeTree *ntree)
947 {
948         if (!ntree)
949                 return;
950
951         /* nodetree itself */
952         ID *ntree_id = &ntree->id;
953         OperationDepsNode *op_node;
954
955         build_animdata(ntree_id);
956
957         /* Parameters for drivers. */
958         op_node = add_operation_node(ntree_id,
959                                      DEG_NODE_TYPE_PARAMETERS,
960                                      NULL,
961                                      DEG_OPCODE_PARAMETERS_EVAL);
962         op_node->set_as_exit();
963
964         /* nodetree's nodes... */
965         LINKLIST_FOREACH (bNode *, bnode, &ntree->nodes) {
966                 ID *id = bnode->id;
967                 if (id != NULL) {
968                         ID_Type id_type = GS(id->name);
969                         if (id_type == ID_MA) {
970                                 build_material((Material *)id);
971                         }
972                         else if (id_type == ID_TE) {
973                                 build_texture((Tex *)id);
974                         }
975                         else if (id_type == ID_IM) {
976                                 build_image((Image *)id);
977                         }
978                         else if (bnode->type == NODE_GROUP) {
979                                 bNodeTree *group_ntree = (bNodeTree *)id;
980                                 if ((group_ntree->id.tag & LIB_TAG_DOIT) == 0) {
981                                         build_nodetree(group_ntree);
982                                 }
983                         }
984                 }
985         }
986
987         // TODO: link from nodetree to owner_component?
988 }
989
990 /* Recursively build graph for material */
991 void DepsgraphNodeBuilder::build_material(Material *ma)
992 {
993         ID *ma_id = &ma->id;
994         if (ma_id->tag & LIB_TAG_DOIT) {
995                 return;
996         }
997
998         add_operation_node(ma_id, DEG_NODE_TYPE_SHADING, NULL,
999                            DEG_OPCODE_PLACEHOLDER, "Material Update");
1000
1001         /* material animation */
1002         build_animdata(ma_id);
1003
1004         /* textures */
1005         build_texture_stack(ma->mtex);
1006
1007         /* material's nodetree */
1008         build_nodetree(ma->nodetree);
1009 }
1010
1011 /* Texture-stack attached to some shading datablock */
1012 void DepsgraphNodeBuilder::build_texture_stack(MTex **texture_stack)
1013 {
1014         int i;
1015
1016         /* for now assume that all texture-stacks have same number of max items */
1017         for (i = 0; i < MAX_MTEX; i++) {
1018                 MTex *mtex = texture_stack[i];
1019                 if (mtex && mtex->tex)
1020                         build_texture(mtex->tex);
1021         }
1022 }
1023
1024 /* Recursively build graph for texture */
1025 void DepsgraphNodeBuilder::build_texture(Tex *tex)
1026 {
1027         ID *tex_id = &tex->id;
1028         if (tex_id->tag & LIB_TAG_DOIT) {
1029                 return;
1030         }
1031         tex_id->tag |= LIB_TAG_DOIT;
1032         /* Texture itself. */
1033         build_animdata(tex_id);
1034         /* Texture's nodetree. */
1035         build_nodetree(tex->nodetree);
1036         /* Special cases for different IDs which texture uses. */
1037         if (tex->type == TEX_IMAGE) {
1038                 if (tex->ima != NULL) {
1039                         build_image(tex->ima);
1040                 }
1041         }
1042 }
1043
1044 void DepsgraphNodeBuilder::build_image(Image *image) {
1045         ID *image_id = &image->id;
1046         if (image_id->tag & LIB_TAG_DOIT) {
1047                 return;
1048         }
1049         image_id->tag |= LIB_TAG_DOIT;
1050         /* Placeholder so we can add relations and tag ID node for update. */
1051         add_operation_node(image_id,
1052                            DEG_NODE_TYPE_PARAMETERS,
1053                            NULL,
1054                            DEG_OPCODE_PLACEHOLDER,
1055                            "Image Eval");
1056 }
1057
1058 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1059 {
1060         /* For now, just a plain wrapper? */
1061         // TODO: create compositing component?
1062         // XXX: component type undefined!
1063         //graph->get_node(&scene->id, NULL, DEG_NODE_TYPE_COMPOSITING, NULL);
1064
1065         /* for now, nodetrees are just parameters; compositing occurs in internals of renderer... */
1066         add_component_node(&scene->id, DEG_NODE_TYPE_PARAMETERS);
1067         build_nodetree(scene->nodetree);
1068 }
1069
1070 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1071 {
1072         ID *gpd_id = &gpd->id;
1073
1074         /* TODO(sergey): what about multiple users of same datablock? This should
1075          * only get added once.
1076          */
1077
1078         /* The main reason Grease Pencil is included here is because the animation
1079          * (and drivers) need to be hosted somewhere.
1080          */
1081         build_animdata(gpd_id);
1082 }
1083
1084 void DepsgraphNodeBuilder::build_cachefile(CacheFile *cache_file)
1085 {
1086         ID *cache_file_id = &cache_file->id;
1087         /* Animation, */
1088         build_animdata(cache_file_id);
1089         /* Cache evaluation itself. */
1090         add_component_node(cache_file_id, DEG_NODE_TYPE_CACHE);
1091         add_operation_node(cache_file_id, DEG_NODE_TYPE_CACHE, NULL,
1092                            DEG_OPCODE_PLACEHOLDER, "Cache File Update");
1093 }
1094
1095 void DepsgraphNodeBuilder::build_mask(Mask *mask)
1096 {
1097         ID *mask_id = &mask->id;
1098         /* F-Curve based animation. */
1099         build_animdata(mask_id);
1100         /* Animation based on mask's shapes. */
1101         add_operation_node(mask_id,
1102                            DEG_NODE_TYPE_ANIMATION,
1103                            function_bind(BKE_mask_eval_animation, _1, mask),
1104                            DEG_OPCODE_MASK_ANIMATION);
1105         /* Final mask evaluation. */
1106         add_operation_node(mask_id,
1107                            DEG_NODE_TYPE_PARAMETERS,
1108                            function_bind(BKE_mask_eval_update, _1, mask),
1109                            DEG_OPCODE_MASK_EVAL);
1110 }
1111
1112 void DepsgraphNodeBuilder::build_movieclip(MovieClip *clip) {
1113         ID *clip_id = &clip->id;
1114         /* Animation. */
1115         build_animdata(clip_id);
1116         /* Movie clip evaluation. */
1117         add_operation_node(clip_id,
1118                            DEG_NODE_TYPE_PARAMETERS,
1119                            function_bind(BKE_movieclip_eval_update, _1, clip),
1120                            DEG_OPCODE_MOVIECLIP_EVAL);
1121 }
1122
1123 }  // namespace DEG