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