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