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