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