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