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