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