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