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