Depsgraph: More clear name for geometry evaluation
[blender.git] / source / blender / depsgraph / intern / builder / deg_builder_nodes.cc
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation.
17  * All rights reserved.
18  *
19  * Original Author: Joshua Leung
20  */
21
22 /** \file blender/depsgraph/intern/builder/deg_builder_nodes.cc
23  *  \ingroup depsgraph
24  *
25  * Methods for constructing depsgraph's nodes
26  */
27
28 #include "intern/builder/deg_builder_nodes.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_blenlib.h"
36 #include "BLI_string.h"
37 #include "BLI_utildefines.h"
38
39 extern "C" {
40 #include "DNA_action_types.h"
41 #include "DNA_anim_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_cachefile_types.h"
44 #include "DNA_camera_types.h"
45 #include "DNA_collection_types.h"
46 #include "DNA_constraint_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_effect_types.h"
49 #include "DNA_gpencil_types.h"
50 #include "DNA_key_types.h"
51 #include "DNA_lamp_types.h"
52 #include "DNA_material_types.h"
53 #include "DNA_mask_types.h"
54 #include "DNA_mesh_types.h"
55 #include "DNA_meta_types.h"
56 #include "DNA_movieclip_types.h"
57 #include "DNA_node_types.h"
58 #include "DNA_particle_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_lightprobe_types.h"
61 #include "DNA_rigidbody_types.h"
62 #include "DNA_scene_types.h"
63 #include "DNA_speaker_types.h"
64 #include "DNA_texture_types.h"
65 #include "DNA_world_types.h"
66
67 #include "BKE_action.h"
68 #include "BKE_armature.h"
69 #include "BKE_animsys.h"
70 #include "BKE_collection.h"
71 #include "BKE_constraint.h"
72 #include "BKE_curve.h"
73 #include "BKE_effect.h"
74 #include "BKE_fcurve.h"
75 #include "BKE_gpencil.h"
76 #include "BKE_gpencil_modifier.h"
77 #include "BKE_idcode.h"
78 #include "BKE_key.h"
79 #include "BKE_lattice.h"
80 #include "BKE_mask.h"
81 #include "BKE_material.h"
82 #include "BKE_mesh.h"
83 #include "BKE_mball.h"
84 #include "BKE_modifier.h"
85 #include "BKE_movieclip.h"
86 #include "BKE_node.h"
87 #include "BKE_object.h"
88 #include "BKE_particle.h"
89 #include "BKE_pointcache.h"
90 #include "BKE_rigidbody.h"
91 #include "BKE_shader_fx.h"
92 #include "BKE_sound.h"
93 #include "BKE_tracking.h"
94 #include "BKE_world.h"
95
96 #include "RNA_access.h"
97 #include "RNA_types.h"
98 } /* extern "C" */
99
100 #include "DEG_depsgraph.h"
101 #include "DEG_depsgraph_build.h"
102
103 #include "intern/builder/deg_builder.h"
104 #include "intern/depsgraph.h"
105 #include "intern/eval/deg_eval_copy_on_write.h"
106 #include "intern/node/deg_node.h"
107 #include "intern/node/deg_node_component.h"
108 #include "intern/node/deg_node_id.h"
109 #include "intern/node/deg_node_operation.h"
110 #include "intern/depsgraph_type.h"
111
112 namespace DEG {
113
114 namespace {
115
116 void free_copy_on_write_datablock(void *id_info_v)
117 {
118         DepsgraphNodeBuilder::IDInfo *id_info =
119             (DepsgraphNodeBuilder::IDInfo *)id_info_v;
120         if (id_info->id_cow != NULL) {
121                 deg_free_copy_on_write_datablock(id_info->id_cow);
122                 MEM_freeN(id_info->id_cow);
123         }
124         MEM_freeN(id_info);
125 }
126
127 }  /* namespace */
128
129 /* ************ */
130 /* Node Builder */
131
132 /* **** General purpose functions **** */
133
134 DepsgraphNodeBuilder::DepsgraphNodeBuilder(Main *bmain, Depsgraph *graph)
135     : bmain_(bmain),
136       graph_(graph),
137       scene_(NULL),
138       view_layer_(NULL),
139       view_layer_index_(-1),
140       collection_(NULL),
141       is_parent_collection_visible_(true),
142       id_info_hash_(NULL)
143 {
144 }
145
146 DepsgraphNodeBuilder::~DepsgraphNodeBuilder()
147 {
148         if (id_info_hash_ != NULL) {
149                 BLI_ghash_free(id_info_hash_, NULL, free_copy_on_write_datablock);
150         }
151 }
152
153 IDNode *DepsgraphNodeBuilder::add_id_node(ID *id)
154 {
155         IDNode *id_node = NULL;
156         ID *id_cow = NULL;
157         IDComponentsMask previously_visible_components_mask = 0;
158         uint32_t previous_eval_flags = 0;
159         uint64_t previous_customdata_mask = 0;
160         IDInfo *id_info = (IDInfo *)BLI_ghash_lookup(id_info_hash_, id);
161         if (id_info != NULL) {
162                 id_cow = id_info->id_cow;
163                 previously_visible_components_mask =
164                         id_info->previously_visible_components_mask;
165                 previous_eval_flags = id_info->previous_eval_flags;
166                 previous_customdata_mask = id_info->previous_customdata_mask;
167                 /* Tag ID info to not free the CoW ID pointer. */
168                 id_info->id_cow = NULL;
169         }
170         id_node = graph_->add_id_node(id, id_cow);
171         id_node->previously_visible_components_mask =
172                 previously_visible_components_mask;
173         id_node->previous_eval_flags = previous_eval_flags;
174         id_node->previous_customdata_mask = previous_customdata_mask;
175         /* Currently all ID nodes are supposed to have copy-on-write logic.
176          *
177          * NOTE: Zero number of components indicates that ID node was just created. */
178         if (BLI_ghash_len(id_node->components) == 0) {
179                 ComponentNode *comp_cow =
180                         id_node->add_component(NodeType::COPY_ON_WRITE);
181                 OperationNode *op_cow = comp_cow->add_operation(
182                         function_bind(deg_evaluate_copy_on_write, _1, id_node),
183                         OperationCode::COPY_ON_WRITE,
184                         "", -1);
185                 graph_->operations.push_back(op_cow);
186         }
187         return id_node;
188 }
189
190 IDNode *DepsgraphNodeBuilder::find_id_node(ID *id)
191 {
192         return graph_->find_id_node(id);
193 }
194
195 TimeSourceNode *DepsgraphNodeBuilder::add_time_source()
196 {
197         return graph_->add_time_source();
198 }
199
200 ComponentNode *DepsgraphNodeBuilder::add_component_node(
201         ID *id,
202         NodeType comp_type,
203         const char *comp_name)
204 {
205         IDNode *id_node = add_id_node(id);
206         ComponentNode *comp_node = id_node->add_component(comp_type, comp_name);
207         comp_node->owner = id_node;
208         return comp_node;
209 }
210
211 OperationNode *DepsgraphNodeBuilder::add_operation_node(
212         ComponentNode *comp_node,
213         const DepsEvalOperationCb& op,
214         OperationCode opcode,
215         const char *name,
216         int name_tag)
217 {
218         OperationNode *op_node = comp_node->find_operation(opcode,
219                                                                name,
220                                                                name_tag);
221         if (op_node == NULL) {
222                 op_node = comp_node->add_operation(op, opcode, name, name_tag);
223                 graph_->operations.push_back(op_node);
224         }
225         else {
226                 fprintf(stderr,
227                         "add_operation: Operation already exists - %s has %s at %p\n",
228                         comp_node->identifier().c_str(),
229                         op_node->identifier().c_str(),
230                         op_node);
231                 BLI_assert(!"Should not happen!");
232         }
233         return op_node;
234 }
235
236 OperationNode *DepsgraphNodeBuilder::add_operation_node(
237         ID *id,
238         NodeType comp_type,
239         const char *comp_name,
240         const DepsEvalOperationCb& op,
241         OperationCode opcode,
242         const char *name,
243         int name_tag)
244 {
245         ComponentNode *comp_node = add_component_node(id, comp_type, comp_name);
246         return add_operation_node(comp_node, op, opcode, name, name_tag);
247 }
248
249 OperationNode *DepsgraphNodeBuilder::add_operation_node(
250         ID *id,
251         NodeType comp_type,
252         const DepsEvalOperationCb& op,
253         OperationCode opcode,
254         const char *name,
255         int name_tag)
256 {
257         return add_operation_node(id,
258                                   comp_type,
259                                   "",
260                                   op,
261                                   opcode,
262                                   name,
263                                   name_tag);
264 }
265
266 OperationNode *DepsgraphNodeBuilder::ensure_operation_node(
267         ID *id,
268         NodeType comp_type,
269         const DepsEvalOperationCb& op,
270         OperationCode opcode,
271         const char *name,
272         int name_tag)
273 {
274         OperationNode *operation =
275                 find_operation_node(id, comp_type, opcode, name, name_tag);
276         if (operation != NULL) {
277                 return operation;
278         }
279         return add_operation_node(id, comp_type, op, opcode, name, name_tag);
280 }
281
282 bool DepsgraphNodeBuilder::has_operation_node(ID *id,
283                                               NodeType comp_type,
284                                               const char *comp_name,
285                                               OperationCode 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 OperationNode *DepsgraphNodeBuilder::find_operation_node(
298         ID *id,
299         NodeType comp_type,
300         const char *comp_name,
301         OperationCode opcode,
302         const char *name,
303         int name_tag)
304 {
305         ComponentNode *comp_node = add_component_node(id, comp_type, comp_name);
306         return comp_node->find_operation(opcode, name, name_tag);
307 }
308
309 OperationNode *DepsgraphNodeBuilder::find_operation_node(
310         ID *id,
311         NodeType comp_type,
312         OperationCode 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 ID *DepsgraphNodeBuilder::get_cow_id(const ID *id_orig) const
320 {
321         return graph_->get_cow_id(id_orig);
322 }
323
324 ID *DepsgraphNodeBuilder::ensure_cow_id(ID *id_orig)
325 {
326         if (id_orig->tag & LIB_TAG_COPIED_ON_WRITE) {
327                 /* ID is already remapped to copy-on-write. */
328                 return id_orig;
329         }
330         IDNode *id_node = add_id_node(id_orig);
331         return id_node->id_cow;
332 }
333
334 /* **** Build functions for entity nodes **** */
335
336 void DepsgraphNodeBuilder::begin_build()
337 {
338         /* Store existing copy-on-write versions of datablock, so we can re-use
339          * them for new ID nodes. */
340         id_info_hash_ = BLI_ghash_ptr_new("Depsgraph id hash");
341         for (IDNode *id_node : graph_->id_nodes) {
342                 IDInfo *id_info = (IDInfo *)MEM_mallocN(
343                         sizeof(IDInfo), "depsgraph id info");
344                 if (deg_copy_on_write_is_expanded(id_node->id_cow) &&
345                     id_node->id_orig != id_node->id_cow)
346                 {
347                         id_info->id_cow = id_node->id_cow;
348                 }
349                 else {
350                         id_info->id_cow = NULL;
351                 }
352                 id_info->previously_visible_components_mask =
353                         id_node->visible_components_mask;
354                 id_info->previous_eval_flags = id_node->eval_flags;
355                 id_info->previous_customdata_mask = id_node->customdata_mask;
356                 BLI_ghash_insert(id_info_hash_, id_node->id_orig, id_info);
357                 id_node->id_cow = NULL;
358         }
359
360         GSET_FOREACH_BEGIN(OperationNode *, op_node, graph_->entry_tags)
361         {
362                 ComponentNode *comp_node = op_node->owner;
363                 IDNode *id_node = comp_node->owner;
364
365                 SavedEntryTag entry_tag;
366                 entry_tag.id_orig = id_node->id_orig;
367                 entry_tag.component_type = comp_node->type;
368                 entry_tag.opcode = op_node->opcode;
369                 entry_tag.name = op_node->name;
370                 entry_tag.name_tag = op_node->name_tag;
371                 saved_entry_tags_.push_back(entry_tag);
372         };
373         GSET_FOREACH_END();
374
375         /* Make sure graph has no nodes left from previous state. */
376         graph_->clear_all_nodes();
377         graph_->operations.clear();
378         BLI_gset_clear(graph_->entry_tags, NULL);
379 }
380
381 void DepsgraphNodeBuilder::end_build()
382 {
383         for (const SavedEntryTag& entry_tag : saved_entry_tags_) {
384                 IDNode *id_node = find_id_node(entry_tag.id_orig);
385                 if (id_node == NULL) {
386                         continue;
387                 }
388                 ComponentNode *comp_node =
389                         id_node->find_component(entry_tag.component_type);
390                 if (comp_node == NULL) {
391                         continue;
392                 }
393                 OperationNode *op_node = comp_node->find_operation(entry_tag.opcode, entry_tag.name, entry_tag.name_tag);
394                 if (op_node == NULL) {
395                         continue;
396                 }
397                 /* Since the tag is coming from a saved copy of entry tags, this means
398                  * that originally node was explicitly tagged for user update. */
399                 op_node->tag_update(graph_, DEG_UPDATE_SOURCE_USER_EDIT);
400         }
401 }
402
403 void DepsgraphNodeBuilder::build_id(ID *id)
404 {
405         if (id == NULL) {
406                 return;
407         }
408         switch (GS(id->name)) {
409                 case ID_AC:
410                         build_action((bAction *)id);
411                         break;
412                 case ID_AR:
413                         build_armature((bArmature *)id);
414                         break;
415                 case ID_CA:
416                         build_camera((Camera *)id);
417                         break;
418                 case ID_GR:
419                         build_collection(NULL, (Collection *)id);
420                         break;
421                 case ID_OB:
422                         /* TODO(sergey): Get visibility from a "parent" somehow.
423                          *
424                          * NOTE: Using `false` visibility here should be fine, since if this
425                          * driver affects on something invisible we don't really care if the
426                          * driver gets evaluated (and even don't want this to force object
427                          * to become visible).
428                          *
429                          * If this happened to be affecting visible object, then it is up to
430                          * deg_graph_build_flush_visibility() to ensure visibility of the
431                          * object is true. */
432                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY, false);
433                         break;
434                 case ID_KE:
435                         build_shapekeys((Key *)id);
436                         break;
437                 case ID_LA:
438                         build_lamp((Lamp *)id);
439                         break;
440                 case ID_LP:
441                         build_lightprobe((LightProbe *)id);
442                         break;
443                 case ID_NT:
444                         build_nodetree((bNodeTree *)id);
445                         break;
446                 case ID_MA:
447                         build_material((Material *)id);
448                         break;
449                 case ID_TE:
450                         build_texture((Tex *)id);
451                         break;
452                 case ID_IM:
453                         build_image((Image *)id);
454                         break;
455                 case ID_WO:
456                         build_world((World *)id);
457                         break;
458                 case ID_MSK:
459                         build_mask((Mask *)id);
460                         break;
461                 case ID_MC:
462                         build_movieclip((MovieClip *)id);
463                         break;
464                 case ID_ME:
465                 case ID_CU:
466                 case ID_MB:
467                 case ID_LT:
468                         /* TODO(sergey): Get visibility from a "parent" somehow.
469                          *
470                          * NOTE: Similarly to above, we don't want false-positives on
471                          * visibility. */
472                         build_object_data_geometry_datablock(id, false);
473                         break;
474                 case ID_SPK:
475                         build_speaker((Speaker *)id);
476                         break;
477                 case ID_TXT:
478                         /* Not a part of dependency graph. */
479                         break;
480                 case ID_CF:
481                         build_cachefile((CacheFile *)id);
482                         break;
483                 default:
484                         fprintf(stderr, "Unhandled ID %s\n", id->name);
485                         BLI_assert(!"Should never happen");
486                         break;
487         }
488 }
489
490 void DepsgraphNodeBuilder::build_collection(
491         LayerCollection *from_layer_collection,
492         Collection *collection)
493 {
494         const int restrict_flag = (graph_->mode == DAG_EVAL_VIEWPORT)
495                 ? COLLECTION_RESTRICT_VIEW
496                 : COLLECTION_RESTRICT_RENDER;
497         const bool is_collection_restricted = (collection->flag & restrict_flag);
498         const bool is_collection_visible =
499                 !is_collection_restricted && is_parent_collection_visible_;
500         IDNode *id_node;
501         if (built_map_.checkIsBuiltAndTag(collection)) {
502                 id_node = find_id_node(&collection->id);
503                 if (is_collection_visible &&
504                     id_node->is_directly_visible == false &&
505                     id_node->is_collection_fully_expanded == true)
506                 {
507                         /* Collection became visible, make sure nested collections and
508                          * objects are poked with the new visibility flag, since they
509                          * might become visible too. */
510                 }
511                 else {
512                         return;
513                 }
514         }
515         else {
516                 /* Collection itself. */
517                 id_node = add_id_node(&collection->id);
518                 id_node->is_directly_visible = is_collection_visible;
519         }
520         if (from_layer_collection != NULL) {
521                 /* If we came from layer collection we don't go deeper, view layer
522                  * builder takes care of going deeper. */
523                 return;
524         }
525         /* Backup state. */
526         Collection *current_state_collection = collection_;
527         const bool is_current_parent_collection_visible =
528                 is_parent_collection_visible_;
529         /* Modify state as we've entered new collection/ */
530         collection_ = collection;
531         is_parent_collection_visible_ = is_collection_visible;
532         /* Build collection objects. */
533         LISTBASE_FOREACH (CollectionObject *, cob, &collection->gobject) {
534                 build_object(
535                         -1, cob->ob, DEG_ID_LINKED_INDIRECTLY, is_collection_visible);
536         }
537         /* Build child collections. */
538         LISTBASE_FOREACH (CollectionChild *, child, &collection->children) {
539                 build_collection(NULL, child->collection);
540         }
541         /* Restore state. */
542         collection_ = current_state_collection;
543         is_parent_collection_visible_ = is_current_parent_collection_visible;
544         id_node->is_collection_fully_expanded = true;
545 }
546
547 void DepsgraphNodeBuilder::build_object(int base_index,
548                                         Object *object,
549                                         eDepsNode_LinkedState_Type linked_state,
550                                         bool is_visible)
551 {
552         const bool has_object = built_map_.checkIsBuiltAndTag(object);
553         /* Skip rest of components if the ID node was already there. */
554         if (has_object) {
555                 IDNode *id_node = find_id_node(&object->id);
556                 /* We need to build some extra stuff if object becomes linked
557                  * directly. */
558                 if (id_node->linked_state == DEG_ID_LINKED_INDIRECTLY) {
559                         build_object_flags(base_index, object, linked_state);
560                 }
561                 id_node->linked_state = max(id_node->linked_state, linked_state);
562                 if (id_node->linked_state == DEG_ID_LINKED_DIRECTLY) {
563                         id_node->is_directly_visible |= is_visible;
564                 }
565                 return;
566         }
567         /* Create ID node for object and begin init. */
568         IDNode *id_node = add_id_node(&object->id);
569         Object *object_cow = get_cow_datablock(object);
570         id_node->linked_state = linked_state;
571         if (object == scene_->camera) {
572                 id_node->is_directly_visible = true;
573         }
574         else {
575                 id_node->is_directly_visible = is_visible;
576         }
577         /* Various flags, flushing from bases/collections. */
578         build_object_flags(base_index, object, linked_state);
579         /* Transform. */
580         build_object_transform(object);
581         /* Parent. */
582         if (object->parent != NULL) {
583                 build_object(
584                         -1, object->parent, DEG_ID_LINKED_INDIRECTLY, is_visible);
585         }
586         /* Modifiers. */
587         if (object->modifiers.first != NULL) {
588                 BuilderWalkUserData data;
589                 data.builder = this;
590                 data.is_parent_visible = is_visible;
591                 modifiers_foreachIDLink(object, modifier_walk, &data);
592         }
593         /* Grease Pencil Modifiers. */
594         if (object->greasepencil_modifiers.first != NULL) {
595                 BuilderWalkUserData data;
596                 data.builder = this;
597                 data.is_parent_visible = is_visible;
598                 BKE_gpencil_modifiers_foreachIDLink(object, modifier_walk, &data);
599         }
600         /* Shader FX. */
601         if (object->shader_fx.first != NULL) {
602                 BuilderWalkUserData data;
603                 data.builder = this;
604                 data.is_parent_visible = is_visible;
605                 BKE_shaderfx_foreachIDLink(object, modifier_walk, &data);
606         }
607         /* Constraints. */
608         if (object->constraints.first != NULL) {
609                 BuilderWalkUserData data;
610                 data.builder = this;
611                 data.is_parent_visible = is_visible;
612                 BKE_constraints_id_loop(&object->constraints, constraint_walk, &data);
613         }
614         /* Object data. */
615         build_object_data(object, is_visible);
616         /* Build animation data,
617          *
618          * Do it now because it's possible object data will affect
619          * on object's level animation, for example in case of rebuilding
620          * pose for proxy. */
621         OperationNode *op_node = add_operation_node(&object->id,
622                                                         NodeType::PARAMETERS,
623                                                         NULL,
624                                                         OperationCode::PARAMETERS_EVAL);
625         op_node->set_as_exit();
626         build_animdata(&object->id);
627         /* Particle systems. */
628         if (object->particlesystem.first != NULL) {
629                 build_particle_systems(object, is_visible);
630         }
631         /* Proxy object to copy from. */
632         if (object->proxy_from != NULL) {
633                 build_object(
634                         -1, object->proxy_from, DEG_ID_LINKED_INDIRECTLY, is_visible);
635         }
636         if (object->proxy_group != NULL) {
637                 build_object(
638                         -1, object->proxy_group, DEG_ID_LINKED_INDIRECTLY, is_visible);
639         }
640         /* Object dupligroup. */
641         if (object->dup_group != NULL) {
642                 const bool is_current_parent_collection_visible =
643                         is_parent_collection_visible_;
644                 is_parent_collection_visible_ = is_visible;
645                 build_collection(NULL, object->dup_group);
646                 is_parent_collection_visible_ = is_current_parent_collection_visible;
647                 add_operation_node(&object->id,
648                                    NodeType::DUPLI,
649                                    NULL,
650                                    OperationCode::PLACEHOLDER,
651                                    "Dupli");
652         }
653         /* Syncronization back to original object. */
654         add_operation_node(&object->id,
655                            NodeType::SYNCHRONIZATION,
656                            function_bind(BKE_object_synchronize_to_original,
657                                          _1,
658                                          object_cow),
659                            OperationCode::SYNCHRONIZE_TO_ORIGINAL);
660 }
661
662 void DepsgraphNodeBuilder::build_object_flags(
663         int base_index,
664         Object *object,
665         eDepsNode_LinkedState_Type linked_state)
666 {
667         if (base_index == -1) {
668                 return;
669         }
670         Scene *scene_cow = get_cow_datablock(scene_);
671         Object *object_cow = get_cow_datablock(object);
672         const bool is_from_set = (linked_state == DEG_ID_LINKED_VIA_SET);
673         /* TODO(sergey): Is this really best component to be used? */
674         add_operation_node(&object->id,
675                            NodeType::OBJECT_FROM_LAYER,
676                            function_bind(BKE_object_eval_flush_base_flags,
677                                          _1,
678                                          scene_cow,
679                                          view_layer_index_,
680                                          object_cow, base_index,
681                                          is_from_set),
682                            OperationCode::OBJECT_BASE_FLAGS);
683 }
684
685 void DepsgraphNodeBuilder::build_object_data(
686         Object *object, bool is_object_visible)
687 {
688         if (object->data == NULL) {
689                 return;
690         }
691         /* type-specific data. */
692         switch (object->type) {
693                 case OB_MESH:
694                 case OB_CURVE:
695                 case OB_FONT:
696                 case OB_SURF:
697                 case OB_MBALL:
698                 case OB_LATTICE:
699                 case OB_GPENCIL:
700                         build_object_data_geometry(object, is_object_visible);
701                         break;
702                 case OB_ARMATURE:
703                         if (ID_IS_LINKED(object) && object->proxy_from != NULL) {
704                                 build_proxy_rig(object);
705                         }
706                         else {
707                                 build_rig(object, is_object_visible);
708                         }
709                         break;
710                 case OB_LAMP:
711                         build_object_data_lamp(object);
712                         break;
713                 case OB_CAMERA:
714                         build_object_data_camera(object);
715                         break;
716                 case OB_LIGHTPROBE:
717                         build_object_data_lightprobe(object);
718                         break;
719                 case OB_SPEAKER:
720                         build_object_data_speaker(object);
721                         break;
722                 default:
723                 {
724                         ID *obdata = (ID *)object->data;
725                         if (built_map_.checkIsBuilt(obdata) == 0) {
726                                 build_animdata(obdata);
727                         }
728                         break;
729                 }
730         }
731 }
732
733 void DepsgraphNodeBuilder::build_object_data_camera(Object *object)
734 {
735         Camera *camera = (Camera *)object->data;
736         build_camera(camera);
737 }
738
739 void DepsgraphNodeBuilder::build_object_data_lamp(Object *object)
740 {
741         Lamp *lamp = (Lamp *)object->data;
742         build_lamp(lamp);
743 }
744
745 void DepsgraphNodeBuilder::build_object_data_lightprobe(Object *object)
746 {
747         LightProbe *probe = (LightProbe *)object->data;
748         build_lightprobe(probe);
749         add_operation_node(&object->id,
750                            NodeType::PARAMETERS,
751                            NULL,
752                            OperationCode::LIGHT_PROBE_EVAL);
753 }
754
755 void DepsgraphNodeBuilder::build_object_data_speaker(Object *object)
756 {
757         Speaker *speaker = (Speaker *)object->data;
758         build_speaker(speaker);
759         add_operation_node(&object->id,
760                            NodeType::PARAMETERS,
761                            NULL,
762                            OperationCode::SPEAKER_EVAL);
763 }
764
765 void DepsgraphNodeBuilder::build_object_transform(Object *object)
766 {
767         OperationNode *op_node;
768         Object *ob_cow = get_cow_datablock(object);
769
770         /* local transforms (from transform channels - loc/rot/scale + deltas) */
771         op_node = add_operation_node(&object->id, NodeType::TRANSFORM,
772                                      function_bind(BKE_object_eval_local_transform,
773                                                    _1,
774                                                    ob_cow),
775                                      OperationCode::TRANSFORM_LOCAL);
776         op_node->set_as_entry();
777
778         /* object parent */
779         if (object->parent != NULL) {
780                 add_operation_node(&object->id, NodeType::TRANSFORM,
781                                    function_bind(BKE_object_eval_parent,
782                                                  _1,
783                                                  ob_cow),
784                                    OperationCode::TRANSFORM_PARENT);
785         }
786
787         /* object constraints */
788         if (object->constraints.first != NULL) {
789                 build_object_constraints(object);
790         }
791
792         /* Rest of transformation update. */
793         add_operation_node(&object->id, NodeType::TRANSFORM,
794                            function_bind(BKE_object_eval_uber_transform,
795                                          _1,
796                                          ob_cow),
797                            OperationCode::TRANSFORM_OBJECT_UBEREVAL);
798
799         /* object transform is done */
800         op_node = add_operation_node(&object->id, NodeType::TRANSFORM,
801                                      function_bind(BKE_object_eval_transform_final,
802                                                    _1,
803                                                    ob_cow),
804                                      OperationCode::TRANSFORM_FINAL);
805         op_node->set_as_exit();
806 }
807
808 /**
809  * Constraints Graph Notes
810  *
811  * For constraints, we currently only add a operation node to the Transform
812  * or Bone components (depending on whichever type of owner we have).
813  * This represents the entire constraints stack, which is for now just
814  * executed as a single monolithic block. At least initially, this should
815  * be sufficient for ensuring that the porting/refactoring process remains
816  * manageable.
817  *
818  * However, when the time comes for developing "node-based" constraints,
819  * we'll need to split this up into pre/post nodes for "constraint stack
820  * evaluation" + operation nodes for each constraint (i.e. the contents
821  * of the loop body used in the current "solve_constraints()" operation).
822  *
823  * -- Aligorith, August 2013
824  */
825 void DepsgraphNodeBuilder::build_object_constraints(Object *object)
826 {
827         /* create node for constraint stack */
828         add_operation_node(&object->id, NodeType::TRANSFORM,
829                            function_bind(BKE_object_eval_constraints,
830                                          _1,
831                                          get_cow_datablock(scene_),
832                                          get_cow_datablock(object)),
833                            OperationCode::TRANSFORM_CONSTRAINTS);
834 }
835
836 void DepsgraphNodeBuilder::build_object_pointcache(Object *object)
837 {
838         if (!BKE_ptcache_object_has(scene_, object, 0)) {
839                 return;
840         }
841         Scene *scene_cow = get_cow_datablock(scene_);
842         Object *object_cow = get_cow_datablock(object);
843         add_operation_node(&object->id,
844                            NodeType::POINT_CACHE,
845                            function_bind(BKE_object_eval_ptcache_reset,
846                                          _1,
847                                          scene_cow,
848                                          object_cow),
849                            OperationCode::POINT_CACHE_RESET);
850 }
851
852 /**
853  * Build graph nodes for AnimData block
854  * \param id: ID-Block which hosts the AnimData
855  */
856 void DepsgraphNodeBuilder::build_animdata(ID *id)
857 {
858         AnimData *adt = BKE_animdata_from_id(id);
859         if (adt == NULL) {
860                 return;
861         }
862         if (adt->action != NULL) {
863                 build_action(adt->action);
864         }
865         /* animation */
866         if (adt->action || adt->nla_tracks.first || adt->drivers.first) {
867                 (void) add_id_node(id);
868                 ID *id_cow = get_cow_id(id);
869
870                 // XXX: Hook up specific update callbacks for special properties which
871                 // may need it...
872
873                 /* actions and NLA - as a single unit for now, as it gets complicated to
874                  * schedule otherwise.  */
875                 if ((adt->action) || (adt->nla_tracks.first)) {
876                         /* create the node */
877                         add_operation_node(id, NodeType::ANIMATION,
878                                            function_bind(BKE_animsys_eval_animdata,
879                                                          _1,
880                                                          id_cow),
881                                            OperationCode::ANIMATION,
882                                            id->name);
883
884                         /* TODO: for each channel affected, we might also want to add some
885                          * support for running RNA update callbacks on them
886                          * (which will be needed for proper handling of drivers later) */
887                 }
888
889                 /* NLA strips contain actions */
890                 LISTBASE_FOREACH (NlaTrack *, nlt, &adt->nla_tracks) {
891                         build_animdata_nlastrip_targets(&nlt->strips);
892                 }
893
894                 /* drivers */
895                 int driver_index = 0;
896                 LISTBASE_FOREACH (FCurve *, fcu, &adt->drivers) {
897                         /* create driver */
898                         build_driver(id, fcu, driver_index++);
899                 }
900         }
901 }
902
903 void DepsgraphNodeBuilder::build_animdata_nlastrip_targets(ListBase *strips)
904 {
905         LISTBASE_FOREACH (NlaStrip *, strip, strips) {
906                 if (strip->act != NULL) {
907                         build_action(strip->act);
908                 }
909                 else if (strip->strips.first != NULL) {
910                         build_animdata_nlastrip_targets(&strip->strips);
911                 }
912         }
913 }
914
915 void DepsgraphNodeBuilder::build_action(bAction *action)
916 {
917         if (built_map_.checkIsBuiltAndTag(action)) {
918                 return;
919         }
920         add_operation_node(&action->id,
921                            NodeType::ANIMATION,
922                            NULL,
923                            OperationCode::ANIMATION);
924 }
925
926 /**
927  * Build graph node(s) for Driver
928  * \param id: ID-Block that driver is attached to
929  * \param fcu: Driver-FCurve
930  * \param driver_index: Index in animation data drivers list
931  */
932 void DepsgraphNodeBuilder::build_driver(ID *id, FCurve *fcurve, int driver_index)
933 {
934         /* Create data node for this driver */
935         ID *id_cow = get_cow_id(id);
936         ChannelDriver *driver_orig = fcurve->driver;
937
938         /* TODO(sergey): ideally we could pass the COW of fcu, but since it
939          * has not yet been allocated at this point we can't. As a workaround
940          * the animation systems allocates an array so we can do a fast lookup
941          * with the driver index. */
942         ensure_operation_node(id,
943                               NodeType::PARAMETERS,
944                               function_bind(BKE_animsys_eval_driver, _1, id_cow, driver_index, driver_orig),
945                               OperationCode::DRIVER,
946                               fcurve->rna_path ? fcurve->rna_path : "",
947                               fcurve->array_index);
948         build_driver_variables(id, fcurve);
949 }
950
951 void DepsgraphNodeBuilder::build_driver_variables(ID * id, FCurve *fcurve)
952 {
953         build_driver_id_property(id, fcurve->rna_path);
954         LISTBASE_FOREACH (DriverVar *, dvar, &fcurve->driver->variables) {
955                 DRIVER_TARGETS_USED_LOOPER_BEGIN(dvar)
956                 {
957                         if (dtar->id == NULL) {
958                                 continue;
959                         }
960                         build_id(dtar->id);
961                         build_driver_id_property(dtar->id, dtar->rna_path);
962                         /* Corresponds to dtar_id_ensure_proxy_from(). */
963                         if ((GS(dtar->id->name) == ID_OB) &&
964                             (((Object *)dtar->id)->proxy_from != NULL))
965                         {
966                                 Object *proxy_from = ((Object *)dtar->id)->proxy_from;
967                                 build_id(&proxy_from->id);
968                                 build_driver_id_property(&proxy_from->id, dtar->rna_path);
969                         }
970                 }
971                 DRIVER_TARGETS_LOOPER_END;
972         }
973 }
974
975 void DepsgraphNodeBuilder::build_driver_id_property(ID *id,
976                                                     const char *rna_path)
977 {
978         if (id == NULL || rna_path == NULL) {
979                 return;
980         }
981         PointerRNA id_ptr, ptr;
982         PropertyRNA *prop;
983         RNA_id_pointer_create(id, &id_ptr);
984         if (!RNA_path_resolve_full(&id_ptr, rna_path, &ptr, &prop, NULL)) {
985                 return;
986         }
987         if (prop == NULL) {
988                 return;
989         }
990         if (!RNA_property_is_idprop(prop)) {
991                 return;
992         }
993         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
994         ensure_operation_node(id,
995                               NodeType::PARAMETERS,
996                               NULL,
997                               OperationCode::ID_PROPERTY,
998                               prop_identifier);
999 }
1000
1001 /* Recursively build graph for world */
1002 void DepsgraphNodeBuilder::build_world(World *world)
1003 {
1004         if (built_map_.checkIsBuiltAndTag(world)) {
1005                 return;
1006         }
1007         /* World itself. */
1008         add_id_node(&world->id);
1009         World *world_cow = get_cow_datablock(world);
1010         /* Shading update. */
1011         add_operation_node(&world->id,
1012                            NodeType::SHADING,
1013                            function_bind(BKE_world_eval,
1014                                          _1,
1015                                          world_cow),
1016                            OperationCode::WORLD_UPDATE);
1017         /* Animation. */
1018         build_animdata(&world->id);
1019         /* World's nodetree. */
1020         build_nodetree(world->nodetree);
1021 }
1022
1023 /* Rigidbody Simulation - Scene Level */
1024 void DepsgraphNodeBuilder::build_rigidbody(Scene *scene)
1025 {
1026         RigidBodyWorld *rbw = scene->rigidbody_world;
1027         Scene *scene_cow = get_cow_datablock(scene);
1028
1029         /**
1030          * Rigidbody Simulation Nodes
1031          * ==========================
1032          *
1033          * There are 3 nodes related to Rigidbody Simulation:
1034          * 1) "Initialize/Rebuild World" - this is called sparingly, only when the
1035          *    simulation needs to be rebuilt (mainly after file reload, or moving
1036          *    back to start frame)
1037          * 2) "Do Simulation" - perform a simulation step - interleaved between the
1038          *    evaluation steps for clusters of objects (i.e. between those affected
1039          *    and/or not affected by the sim for instance).
1040          *
1041          * 3) "Pull Results" - grab the specific transforms applied for a specific
1042          *    object - performed as part of object's transform-stack building. */
1043
1044         /* Create nodes --------------------------------------------------------- */
1045
1046         /* XXX: is this the right component, or do we want to use another one
1047          * instead? */
1048
1049         /* Init/rebuild operation. */
1050         add_operation_node(&scene->id, NodeType::TRANSFORM,
1051                           function_bind(BKE_rigidbody_rebuild_sim, _1, scene_cow),
1052                           OperationCode::RIGIDBODY_REBUILD);
1053         /* Do-sim operation. */
1054         OperationNode *sim_node = add_operation_node(
1055                 &scene->id, NodeType::TRANSFORM,
1056                 function_bind(BKE_rigidbody_eval_simulation, _1, scene_cow),
1057                 OperationCode::RIGIDBODY_SIM);
1058         sim_node->set_as_entry();
1059         sim_node->set_as_exit();
1060         sim_node->owner->entry_operation = sim_node;
1061         /* Objects - simulation participants. */
1062         if (rbw->group  != NULL) {
1063                 build_collection(NULL, rbw->group);
1064                 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->group, object)
1065                 {
1066                         if (object->type != OB_MESH) {
1067                                 continue;
1068                         }
1069                         /* 2) create operation for flushing results */
1070                         /* object's transform component - where the rigidbody operation
1071                          * lives. */
1072                         add_operation_node(&object->id, NodeType::TRANSFORM,
1073                                            function_bind(
1074                                                    BKE_rigidbody_object_sync_transforms,
1075                                                    _1,
1076                                                    scene_cow,
1077                                                    get_cow_datablock(object)),
1078                                            OperationCode::RIGIDBODY_TRANSFORM_COPY);
1079                 }
1080                 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
1081         }
1082         /* Constraints. */
1083         if (rbw->constraints != NULL) {
1084                 FOREACH_COLLECTION_OBJECT_RECURSIVE_BEGIN(rbw->constraints, object)
1085                 {
1086                         RigidBodyCon *rbc = object->rigidbody_constraint;
1087                         if (rbc == NULL || rbc->ob1 == NULL || rbc->ob2 == NULL) {
1088                                 /* When either ob1 or ob2 is NULL, the constraint doesn't work. */
1089                                 continue;
1090                         }
1091                         /* Make sure indirectly linked objects are fully built. */
1092                         build_object(-1, object, DEG_ID_LINKED_INDIRECTLY, false);
1093                         build_object(-1, rbc->ob1, DEG_ID_LINKED_INDIRECTLY, false);
1094                         build_object(-1, rbc->ob2, DEG_ID_LINKED_INDIRECTLY, false);
1095                 }
1096                 FOREACH_COLLECTION_OBJECT_RECURSIVE_END;
1097         }
1098 }
1099
1100 void DepsgraphNodeBuilder::build_particle_systems(Object *object,
1101                                                   bool is_object_visible)
1102 {
1103         /**
1104          * Particle Systems Nodes
1105          * ======================
1106          *
1107          * There are two types of nodes associated with representing
1108          * particle systems:
1109          *  1) Component (EVAL_PARTICLES) - This is the particle-system
1110          *     evaluation context for an object. It acts as the container
1111          *     for all the nodes associated with a particular set of particle
1112          *     systems.
1113          *  2) Particle System Eval Operation - This operation node acts as a
1114          *     blackbox evaluation step for one particle system referenced by
1115          *     the particle systems stack. All dependencies link to this operation. */
1116         /* Component for all particle systems. */
1117         ComponentNode *psys_comp =
1118                 add_component_node(&object->id, NodeType::PARTICLE_SYSTEM);
1119
1120         Object *ob_cow = get_cow_datablock(object);
1121         OperationNode *op_node;
1122         op_node = add_operation_node(psys_comp,
1123                                      function_bind(BKE_particle_system_eval_init,
1124                                                    _1,
1125                                                    ob_cow),
1126                                      OperationCode::PARTICLE_SYSTEM_INIT);
1127         op_node->set_as_entry();
1128         /* Build all particle systems. */
1129         LISTBASE_FOREACH (ParticleSystem *, psys, &object->particlesystem) {
1130                 ParticleSettings *part = psys->part;
1131                 /* Build particle settings operations.
1132                  *
1133                  * NOTE: The call itself ensures settings are only build once.  */
1134                 build_particle_settings(part);
1135                 /* Particle system evaluation. */
1136                 add_operation_node(psys_comp,
1137                                    NULL,
1138                                    OperationCode::PARTICLE_SYSTEM_EVAL,
1139                                    psys->name);
1140                 /* Keyed particle targets. */
1141                 if (part->phystype == PART_PHYS_KEYED) {
1142                         LISTBASE_FOREACH (ParticleTarget *, particle_target, &psys->targets) {
1143                                 if (particle_target->ob == NULL ||
1144                                     particle_target->ob == object)
1145                                 {
1146                                         continue;
1147                                 }
1148                                 build_object(-1,
1149                                              particle_target->ob,
1150                                              DEG_ID_LINKED_INDIRECTLY,
1151                                              is_object_visible);
1152                         }
1153                 }
1154                 /* Visualization of particle system. */
1155                 switch (part->ren_as) {
1156                         case PART_DRAW_OB:
1157                                 if (part->dup_ob != NULL) {
1158                                         build_object(-1,
1159                                                      part->dup_ob,
1160                                                      DEG_ID_LINKED_INDIRECTLY,
1161                                                      is_object_visible);
1162                                 }
1163                                 break;
1164                         case PART_DRAW_GR:
1165                                 if (part->dup_group != NULL) {
1166                                         build_collection(NULL, part->dup_group);
1167                                 }
1168                                 break;
1169                 }
1170         }
1171         op_node = add_operation_node(psys_comp,
1172                                      NULL,
1173                                      OperationCode::PARTICLE_SYSTEM_DONE);
1174         op_node->set_as_exit();
1175 }
1176
1177 void DepsgraphNodeBuilder::build_particle_settings(
1178         ParticleSettings *particle_settings) {
1179         if (built_map_.checkIsBuiltAndTag(particle_settings)) {
1180                 return;
1181         }
1182         /* Make sure we've got proper copied ID pointer. */
1183         add_id_node(&particle_settings->id);
1184         ParticleSettings *particle_settings_cow =
1185                 get_cow_datablock(particle_settings);
1186         /* Animation data. */
1187         build_animdata(&particle_settings->id);
1188         /* Parameters change. */
1189         OperationNode *op_node;
1190         op_node = add_operation_node(&particle_settings->id,
1191                                      NodeType::PARTICLE_SETTINGS,
1192                                      NULL,
1193                                      OperationCode::PARTICLE_SETTINGS_INIT);
1194         op_node->set_as_entry();
1195         add_operation_node(&particle_settings->id,
1196                            NodeType::PARTICLE_SETTINGS,
1197                            function_bind(BKE_particle_settings_eval_reset,
1198                                          _1,
1199                                          particle_settings_cow),
1200                            OperationCode::PARTICLE_SETTINGS_RESET);
1201         op_node = add_operation_node(&particle_settings->id,
1202                                      NodeType::PARTICLE_SETTINGS,
1203                                      NULL,
1204                                      OperationCode::PARTICLE_SETTINGS_EVAL);
1205         op_node->set_as_exit();
1206         /* Texture slots. */
1207         for (int mtex_index = 0; mtex_index < MAX_MTEX; ++mtex_index) {
1208                 MTex *mtex = particle_settings->mtex[mtex_index];
1209                 if (mtex == NULL || mtex->tex == NULL) {
1210                         continue;
1211                 }
1212                 build_texture(mtex->tex);
1213         }
1214 }
1215
1216 /* Shapekeys */
1217 void DepsgraphNodeBuilder::build_shapekeys(Key *key)
1218 {
1219         if (built_map_.checkIsBuiltAndTag(key)) {
1220                 return;
1221         }
1222         build_animdata(&key->id);
1223         /* This is an exit operation for the entire key datablock, is what is used
1224          * as dependency for modifiers evaluation. */
1225         add_operation_node(&key->id,
1226                            NodeType::GEOMETRY,
1227                            NULL,
1228                            OperationCode::GEOMETRY_SHAPEKEY);
1229         /* Create per-key block properties, allowing tricky inter-dependnecies for
1230          * drivers evaluation. */
1231         LISTBASE_FOREACH (KeyBlock *, key_block, &key->block) {
1232                 add_operation_node(&key->id,
1233                                    NodeType::PARAMETERS,
1234                                    NULL,
1235                                    OperationCode::PARAMETERS_EVAL,
1236                                    key_block->name);
1237         }
1238 }
1239
1240 /* ObData Geometry Evaluation */
1241 // XXX: what happens if the datablock is shared!
1242 void DepsgraphNodeBuilder::build_object_data_geometry(
1243         Object *object,
1244         bool is_object_visible)
1245 {
1246         OperationNode *op_node;
1247         Scene *scene_cow = get_cow_datablock(scene_);
1248         Object *object_cow = get_cow_datablock(object);
1249         /* Temporary uber-update node, which does everything.
1250          * It is for the being we're porting old dependencies into the new system.
1251          * We'll get rid of this node as soon as all the granular update functions
1252          * are filled in.
1253          *
1254          * TODO(sergey): Get rid of this node. */
1255         op_node = add_operation_node(&object->id,
1256                                      NodeType::GEOMETRY,
1257                                      function_bind(BKE_object_eval_uber_data,
1258                                                    _1,
1259                                                    scene_cow,
1260                                                    object_cow),
1261                                      OperationCode::GEOMETRY_EVAL);
1262         op_node->set_as_exit();
1263
1264         op_node = add_operation_node(&object->id,
1265                                      NodeType::GEOMETRY,
1266                                      NULL,
1267                                      OperationCode::PLACEHOLDER,
1268                                      "Eval Init");
1269         op_node->set_as_entry();
1270         /* Materials. */
1271         if (object->totcol != 0) {
1272                 if (object->type == OB_MESH) {
1273                         add_operation_node(&object->id,
1274                                            NodeType::SHADING,
1275                                            function_bind(BKE_object_eval_update_shading,
1276                                                          _1,
1277                                                          object_cow),
1278                                            OperationCode::SHADING);
1279                 }
1280                 for (int a = 1; a <= object->totcol; a++) {
1281                         Material *ma = give_current_material(object, a);
1282                         if (ma != NULL) {
1283                                 build_material(ma);
1284                         }
1285                 }
1286         }
1287         /* Point caches. */
1288         build_object_pointcache(object);
1289         /* Geometry. */
1290         build_object_data_geometry_datablock((ID *)object->data, is_object_visible);
1291 }
1292
1293 void DepsgraphNodeBuilder::build_object_data_geometry_datablock(
1294         ID *obdata,
1295         bool is_object_visible)
1296 {
1297         if (built_map_.checkIsBuiltAndTag(obdata)) {
1298                 return;
1299         }
1300         OperationNode *op_node;
1301         /* Make sure we've got an ID node before requesting CoW pointer. */
1302         (void) add_id_node((ID *)obdata);
1303         ID *obdata_cow = get_cow_id(obdata);
1304         /* Animation. */
1305         build_animdata(obdata);
1306         /* ShapeKeys */
1307         Key *key = BKE_key_from_id(obdata);
1308         if (key) {
1309                 build_shapekeys(key);
1310         }
1311         /* Nodes for result of obdata's evaluation, and geometry
1312          * evaluation on object. */
1313         const ID_Type id_type = GS(obdata->name);
1314         switch (id_type) {
1315                 case ID_ME:
1316                 {
1317                         op_node = add_operation_node(obdata,
1318                                                      NodeType::GEOMETRY,
1319                                                      function_bind(BKE_mesh_eval_geometry,
1320                                                                    _1,
1321                                                                    (Mesh *)obdata_cow),
1322                                                      OperationCode::PLACEHOLDER,
1323                                                      "Geometry Eval");
1324                         op_node->set_as_entry();
1325                         break;
1326                 }
1327                 case ID_MB:
1328                 {
1329                         op_node = add_operation_node(obdata,
1330                                                      NodeType::GEOMETRY,
1331                                                      NULL,
1332                                                      OperationCode::PLACEHOLDER,
1333                                                      "Geometry Eval");
1334                         op_node->set_as_entry();
1335                         break;
1336                 }
1337                 case ID_CU:
1338                 {
1339                         op_node = add_operation_node(obdata,
1340                                                      NodeType::GEOMETRY,
1341                                                      function_bind(BKE_curve_eval_geometry,
1342                                                                    _1,
1343                                                                    (Curve *)obdata_cow),
1344                                                                    OperationCode::PLACEHOLDER,
1345                                                                    "Geometry Eval");
1346                         op_node->set_as_entry();
1347                         /* Make sure objects used for bevel.taper are in the graph.
1348                          * NOTE: This objects might be not linked to the scene. */
1349                         Curve *cu = (Curve *)obdata;
1350                         if (cu->bevobj != NULL) {
1351                                 build_object(-1,
1352                                              cu->bevobj,
1353                                              DEG_ID_LINKED_INDIRECTLY,
1354                                              is_object_visible);
1355                         }
1356                         if (cu->taperobj != NULL) {
1357                                 build_object(-1,
1358                                              cu->taperobj,
1359                                              DEG_ID_LINKED_INDIRECTLY,
1360                                              is_object_visible);
1361                         }
1362                         if (cu->textoncurve != NULL) {
1363                                 build_object(-1,
1364                                              cu->textoncurve,
1365                                              DEG_ID_LINKED_INDIRECTLY,
1366                                              is_object_visible);
1367                         }
1368                         break;
1369                 }
1370                 case ID_LT:
1371                 {
1372                         op_node = add_operation_node(obdata,
1373                                                      NodeType::GEOMETRY,
1374                                                      function_bind(BKE_lattice_eval_geometry,
1375                                                                    _1,
1376                                                                    (Lattice *)obdata_cow),
1377                                                                    OperationCode::PLACEHOLDER,
1378                                                                    "Geometry Eval");
1379                         op_node->set_as_entry();
1380                         break;
1381                 }
1382
1383                 case ID_GD:
1384                 {
1385                         /* GPencil evaluation operations. */
1386                         op_node = add_operation_node(obdata,
1387                                                      NodeType::GEOMETRY,
1388                                                      function_bind(BKE_gpencil_eval_geometry,
1389                                                                    _1,
1390                                                                    (bGPdata *)obdata_cow),
1391                                                      OperationCode::PLACEHOLDER,
1392                                                      "Geometry Eval");
1393                         op_node->set_as_entry();
1394                         break;
1395                 }
1396                 default:
1397                         BLI_assert(!"Should not happen");
1398                         break;
1399         }
1400         op_node = add_operation_node(obdata, NodeType::GEOMETRY, NULL,
1401                                      OperationCode::PLACEHOLDER, "Eval Done");
1402         op_node->set_as_exit();
1403         /* Parameters for driver sources. */
1404         add_operation_node(obdata,
1405                            NodeType::PARAMETERS,
1406                            NULL,
1407                            OperationCode::PARAMETERS_EVAL);
1408         /* Batch cache. */
1409         add_operation_node(obdata,
1410                            NodeType::BATCH_CACHE,
1411                            function_bind(BKE_object_data_select_update,
1412                                          _1,
1413                                          obdata_cow),
1414                            OperationCode::GEOMETRY_SELECT_UPDATE);
1415 }
1416
1417 void DepsgraphNodeBuilder::build_armature(bArmature *armature)
1418 {
1419         if (built_map_.checkIsBuiltAndTag(armature)) {
1420                 return;
1421         }
1422         build_animdata(&armature->id);
1423         /* Make sure pose is up-to-date with armature updates. */
1424         add_operation_node(&armature->id,
1425                            NodeType::PARAMETERS,
1426                            NULL,
1427                            OperationCode::PLACEHOLDER,
1428                            "Armature Eval");
1429 }
1430
1431 void DepsgraphNodeBuilder::build_camera(Camera *camera)
1432 {
1433         if (built_map_.checkIsBuiltAndTag(camera)) {
1434                 return;
1435         }
1436         OperationNode *op_node;
1437         build_animdata(&camera->id);
1438         op_node = add_operation_node(&camera->id,
1439                                      NodeType::PARAMETERS,
1440                                      NULL,
1441                                      OperationCode::PARAMETERS_EVAL);
1442         op_node->set_as_exit();
1443 }
1444
1445 void DepsgraphNodeBuilder::build_lamp(Lamp *lamp)
1446 {
1447         if (built_map_.checkIsBuiltAndTag(lamp)) {
1448                 return;
1449         }
1450         OperationNode *op_node;
1451         build_animdata(&lamp->id);
1452         op_node = add_operation_node(&lamp->id,
1453                                      NodeType::PARAMETERS,
1454                                      NULL,
1455                                      OperationCode::PARAMETERS_EVAL);
1456         /* NOTE: We mark this node as both entry and exit. This way we have a
1457          * node to link all dependencies for shading (which includes relation to the
1458          * lamp object, and incldues relation from node tree) without adding a
1459          * dedicated component type. */
1460         op_node->set_as_entry();
1461         op_node->set_as_exit();
1462         /* lamp's nodetree */
1463         build_nodetree(lamp->nodetree);
1464 }
1465
1466 void DepsgraphNodeBuilder::build_nodetree(bNodeTree *ntree)
1467 {
1468         if (ntree == NULL) {
1469                 return;
1470         }
1471         if (built_map_.checkIsBuiltAndTag(ntree)) {
1472                 return;
1473         }
1474         /* nodetree itself */
1475         add_id_node(&ntree->id);
1476         bNodeTree *ntree_cow = get_cow_datablock(ntree);
1477         /* Animation, */
1478         build_animdata(&ntree->id);
1479         /* Shading update. */
1480         add_operation_node(&ntree->id,
1481                            NodeType::SHADING,
1482                            NULL,
1483                            OperationCode::MATERIAL_UPDATE);
1484         /* NOTE: We really pass original and CoW node trees here, this is how the
1485          * callback works. Ideally we need to find a better way for that. */
1486         add_operation_node(&ntree->id,
1487                            NodeType::SHADING_PARAMETERS,
1488                            function_bind(BKE_nodetree_shading_params_eval,
1489                                          _1,
1490                                          ntree_cow,
1491                                          ntree),
1492                            OperationCode::MATERIAL_UPDATE);
1493         /* nodetree's nodes... */
1494         LISTBASE_FOREACH (bNode *, bnode, &ntree->nodes) {
1495                 ID *id = bnode->id;
1496                 if (id == NULL) {
1497                         continue;
1498                 }
1499                 ID_Type id_type = GS(id->name);
1500                 if (id_type == ID_MA) {
1501                         build_material((Material *)id);
1502                 }
1503                 else if (id_type == ID_TE) {
1504                         build_texture((Tex *)id);
1505                 }
1506                 else if (id_type == ID_IM) {
1507                         build_image((Image *)id);
1508                 }
1509                 else if (id_type == ID_OB) {
1510                         /* TODO(sergey): Use visibility of owner of the node tree. */
1511                         build_object(-1, (Object *)id, DEG_ID_LINKED_INDIRECTLY, true);
1512                 }
1513                 else if (id_type == ID_SCE) {
1514                         /* Scenes are used by compositor trees, and handled by render
1515                          * pipeline. No need to build dependencies for them here. */
1516                 }
1517                 else if (id_type == ID_TXT) {
1518                         /* Ignore script nodes. */
1519                 }
1520                 else if (id_type == ID_MSK) {
1521                         build_mask((Mask *)id);
1522                 }
1523                 else if (id_type == ID_MC) {
1524                         build_movieclip((MovieClip *)id);
1525                 }
1526                 else if (bnode->type == NODE_GROUP) {
1527                         bNodeTree *group_ntree = (bNodeTree *)id;
1528                         build_nodetree(group_ntree);
1529                 }
1530                 else {
1531                         BLI_assert(!"Unknown ID type used for node");
1532                 }
1533         }
1534
1535         // TODO: link from nodetree to owner_component?
1536 }
1537
1538 /* Recursively build graph for material */
1539 void DepsgraphNodeBuilder::build_material(Material *material)
1540 {
1541         if (built_map_.checkIsBuiltAndTag(material)) {
1542                 return;
1543         }
1544         /* Material itself. */
1545         add_id_node(&material->id);
1546         Material *material_cow = get_cow_datablock(material);
1547         /* Shading update. */
1548         add_operation_node(&material->id,
1549                            NodeType::SHADING,
1550                            function_bind(BKE_material_eval,
1551                                          _1,
1552                                          material_cow),
1553                            OperationCode::MATERIAL_UPDATE);
1554         /* Material animation. */
1555         build_animdata(&material->id);
1556         /* Material's nodetree. */
1557         build_nodetree(material->nodetree);
1558 }
1559
1560 /* Recursively build graph for texture */
1561 void DepsgraphNodeBuilder::build_texture(Tex *texture)
1562 {
1563         if (built_map_.checkIsBuiltAndTag(texture)) {
1564                 return;
1565         }
1566         /* Texture itself. */
1567         build_animdata(&texture->id);
1568         /* Texture's nodetree. */
1569         build_nodetree(texture->nodetree);
1570         /* Special cases for different IDs which texture uses. */
1571         if (texture->type == TEX_IMAGE) {
1572                 if (texture->ima != NULL) {
1573                         build_image(texture->ima);
1574                 }
1575         }
1576         add_operation_node(&texture->id,
1577                            NodeType::GENERIC_DATABLOCK,
1578                            NULL,
1579                            OperationCode::GENERIC_DATABLOCK_UPDATE);
1580 }
1581
1582 void DepsgraphNodeBuilder::build_image(Image *image) {
1583         if (built_map_.checkIsBuiltAndTag(image)) {
1584                 return;
1585         }
1586         add_operation_node(&image->id,
1587                            NodeType::GENERIC_DATABLOCK,
1588                            NULL,
1589                            OperationCode::GENERIC_DATABLOCK_UPDATE);
1590 }
1591
1592 void DepsgraphNodeBuilder::build_compositor(Scene *scene)
1593 {
1594         /* For now, just a plain wrapper? */
1595         // TODO: create compositing component?
1596         // XXX: component type undefined!
1597         //graph->get_node(&scene->id, NULL, NodeType::COMPOSITING, NULL);
1598
1599         /* for now, nodetrees are just parameters; compositing occurs in internals
1600          * of renderer... */
1601         add_component_node(&scene->id, NodeType::PARAMETERS);
1602         build_nodetree(scene->nodetree);
1603 }
1604
1605 void DepsgraphNodeBuilder::build_gpencil(bGPdata *gpd)
1606 {
1607         if (built_map_.checkIsBuiltAndTag(gpd)) {
1608                 return;
1609         }
1610         ID *gpd_id = &gpd->id;
1611
1612         /* TODO(sergey): what about multiple users of same datablock? This should
1613          * only get added once. */
1614
1615         /* The main reason Grease Pencil is included here is because the animation
1616          * (and drivers) need to be hosted somewhere. */
1617         build_animdata(gpd_id);
1618 }
1619
1620 void DepsgraphNodeBuilder::build_cachefile(CacheFile *cache_file)
1621 {
1622         if (built_map_.checkIsBuiltAndTag(cache_file)) {
1623                 return;
1624         }
1625         ID *cache_file_id = &cache_file->id;
1626         /* Animation, */
1627         build_animdata(cache_file_id);
1628         /* Cache evaluation itself. */
1629         add_operation_node(cache_file_id, NodeType::CACHE, NULL,
1630                            OperationCode::PLACEHOLDER, "Cache File Update");
1631 }
1632
1633 void DepsgraphNodeBuilder::build_mask(Mask *mask)
1634 {
1635         if (built_map_.checkIsBuiltAndTag(mask)) {
1636                 return;
1637         }
1638         ID *mask_id = &mask->id;
1639         Mask *mask_cow = get_cow_datablock(mask);
1640         /* F-Curve based animation. */
1641         build_animdata(mask_id);
1642         /* Animation based on mask's shapes. */
1643         add_operation_node(mask_id,
1644                            NodeType::ANIMATION,
1645                            function_bind(BKE_mask_eval_animation, _1, mask_cow),
1646                            OperationCode::MASK_ANIMATION);
1647         /* Final mask evaluation. */
1648         add_operation_node(mask_id,
1649                            NodeType::PARAMETERS,
1650                            function_bind(BKE_mask_eval_update, _1, mask_cow),
1651                            OperationCode::MASK_EVAL);
1652 }
1653
1654 void DepsgraphNodeBuilder::build_movieclip(MovieClip *clip)
1655 {
1656         if (built_map_.checkIsBuiltAndTag(clip)) {
1657                 return;
1658         }
1659         ID *clip_id = &clip->id;
1660         MovieClip *clip_cow = (MovieClip *)ensure_cow_id(clip_id);
1661         /* Animation. */
1662         build_animdata(clip_id);
1663         /* Movie clip evaluation. */
1664         add_operation_node(clip_id,
1665                            NodeType::PARAMETERS,
1666                            function_bind(BKE_movieclip_eval_update, _1, clip_cow),
1667                            OperationCode::MOVIECLIP_EVAL);
1668
1669         add_operation_node(clip_id,
1670                            NodeType::BATCH_CACHE,
1671                            function_bind(BKE_movieclip_eval_selection_update, _1, clip_cow),
1672                            OperationCode::MOVIECLIP_SELECT_UPDATE);
1673 }
1674
1675 void DepsgraphNodeBuilder::build_lightprobe(LightProbe *probe)
1676 {
1677         if (built_map_.checkIsBuiltAndTag(probe)) {
1678                 return;
1679         }
1680         /* Placeholder so we can add relations and tag ID node for update. */
1681         add_operation_node(&probe->id,
1682                            NodeType::PARAMETERS,
1683                            NULL,
1684                            OperationCode::LIGHT_PROBE_EVAL);
1685
1686         build_animdata(&probe->id);
1687 }
1688
1689 void DepsgraphNodeBuilder::build_speaker(Speaker *speaker)
1690 {
1691         if (built_map_.checkIsBuiltAndTag(speaker)) {
1692                 return;
1693         }
1694         /* Placeholder so we can add relations and tag ID node for update. */
1695         add_operation_node(&speaker->id,
1696                            NodeType::PARAMETERS,
1697                            NULL,
1698                            OperationCode::SPEAKER_EVAL);
1699         build_animdata(&speaker->id);
1700 }
1701
1702 /* **** ID traversal callbacks functions **** */
1703
1704 void DepsgraphNodeBuilder::modifier_walk(void *user_data,
1705                                          struct Object * /*object*/,
1706                                          struct ID **idpoin,
1707                                          int /*cb_flag*/)
1708 {
1709         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
1710         ID *id = *idpoin;
1711         if (id == NULL) {
1712                 return;
1713         }
1714         switch (GS(id->name)) {
1715                 case ID_OB:
1716                         /* Special case for object, so we take owner visibility into
1717                          * account. */
1718                         data->builder->build_object(-1,
1719                                                     (Object *)id,
1720                                                     DEG_ID_LINKED_INDIRECTLY,
1721                                                     data->is_parent_visible);
1722                         break;
1723                 default:
1724                         data->builder->build_id(id);
1725                         break;
1726         }
1727 }
1728
1729 void DepsgraphNodeBuilder::constraint_walk(bConstraint * /*con*/,
1730                                            ID **idpoin,
1731                                            bool /*is_reference*/,
1732                                            void *user_data)
1733 {
1734         BuilderWalkUserData *data = (BuilderWalkUserData *)user_data;
1735         ID *id = *idpoin;
1736         if (id == NULL) {
1737                 return;
1738         }
1739         switch (GS(id->name)) {
1740                 case ID_OB:
1741                         /* Special case for object, so we take owner visibility into
1742                          * account. */
1743                         data->builder->build_object(-1,
1744                                                     (Object *)id,
1745                                                     DEG_ID_LINKED_INDIRECTLY,
1746                                                     data->is_parent_visible);
1747                         break;
1748                 default:
1749                         data->builder->build_id(id);
1750                         break;
1751         }
1752 }
1753
1754 }  // namespace DEG