Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / depsgraph / intern / depsgraph.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
20 /** \file \ingroup depsgraph
21  *
22  * Core routines for how the Depsgraph works.
23  */
24
25 #include "intern/depsgraph.h" /* own include */
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_utildefines.h"
30 #include "BLI_console.h"
31 #include "BLI_hash.h"
32 #include "BLI_ghash.h"
33 #include "BLI_listbase.h"
34
35 extern "C" {
36 #include "DNA_action_types.h"
37 #include "DNA_armature_types.h"
38 #include "DNA_constraint_types.h"
39 #include "DNA_key_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_sequence_types.h"
42
43 #include "RNA_access.h"
44
45 #include "BKE_scene.h"
46 #include "BKE_constraint.h"
47 #include "BKE_global.h"
48 }
49
50 #include <algorithm>
51 #include <cstring>
52
53 #include "DEG_depsgraph.h"
54 #include "DEG_depsgraph_debug.h"
55
56 #include "intern/depsgraph_update.h"
57
58 #include "intern/eval/deg_eval_copy_on_write.h"
59
60 #include "intern/node/deg_node.h"
61 #include "intern/node/deg_node_component.h"
62 #include "intern/node/deg_node_factory.h"
63 #include "intern/node/deg_node_id.h"
64 #include "intern/node/deg_node_operation.h"
65 #include "intern/node/deg_node_time.h"
66
67 #include "intern/depsgraph_physics.h"
68
69 namespace DEG {
70
71 /* TODO(sergey): Find a better place for this. */
72 template <typename T>
73 static void remove_from_vector(vector<T> *vector, const T& value)
74 {
75         vector->erase(std::remove(vector->begin(), vector->end(), value),
76                       vector->end());
77 }
78
79 Depsgraph::Depsgraph(Scene *scene,
80                      ViewLayer *view_layer,
81                      eEvaluationMode mode)
82   : time_source(NULL),
83     need_update(true),
84     scene(scene),
85     view_layer(view_layer),
86     mode(mode),
87     ctime(BKE_scene_frame_get(scene)),
88     scene_cow(NULL),
89     is_active(false),
90     debug_is_evaluating(false)
91 {
92         BLI_spin_init(&lock);
93         id_hash = BLI_ghash_ptr_new("Depsgraph id hash");
94         entry_tags = BLI_gset_ptr_new("Depsgraph entry_tags");
95         debug_flags = G.debug;
96         memset(id_type_updated, 0, sizeof(id_type_updated));
97         memset(physics_relations, 0, sizeof(physics_relations));
98 }
99
100 Depsgraph::~Depsgraph()
101 {
102         clear_id_nodes();
103         BLI_ghash_free(id_hash, NULL, NULL);
104         BLI_gset_free(entry_tags, NULL);
105         if (time_source != NULL) {
106                 OBJECT_GUARDED_DELETE(time_source, TimeSourceNode);
107         }
108         BLI_spin_end(&lock);
109 }
110
111 /* Query Conditions from RNA ----------------------- */
112
113 static bool pointer_to_component_node_criteria(
114         const PointerRNA *ptr,
115         const PropertyRNA *prop,
116         RNAPointerSource /*source*/,
117         ID **id,
118         NodeType *type,
119         const char **component_name,
120         OperationCode *operation_code,
121         const char **operation_name,
122         int *operation_name_tag)
123 {
124         if (ptr->type == NULL) {
125                 return false;
126         }
127         /* Set default values for returns. */
128         *id = (ID *)ptr->id.data;
129         *component_name = "";
130         *operation_code = OperationCode::OPERATION;
131         *operation_name = "";
132         *operation_name_tag = -1;
133         /* Handling of commonly known scenarios. */
134         if (ptr->type == &RNA_PoseBone) {
135                 bPoseChannel *pchan = (bPoseChannel *)ptr->data;
136                 if (prop != NULL && RNA_property_is_idprop(prop)) {
137                         *type = NodeType::PARAMETERS;
138                         *operation_code = OperationCode::ID_PROPERTY;
139                         *operation_name = RNA_property_identifier((PropertyRNA *)prop);
140                         *operation_name_tag = -1;
141                 }
142                 else {
143                         /* Bone - generally, we just want the bone component. */
144                         *type = NodeType::BONE;
145                         *component_name = pchan->name;
146                         /* But B-Bone properties should connect to the actual operation. */
147                         if (!ELEM(NULL, pchan->bone, prop) && pchan->bone->segments > 1 &&
148                             STRPREFIX(RNA_property_identifier(prop), "bbone_"))
149                         {
150                                 *operation_code = OperationCode::BONE_SEGMENTS;
151                         }
152                 }
153                 return true;
154         }
155         else if (ptr->type == &RNA_Bone) {
156                 Bone *bone = (Bone *)ptr->data;
157                 /* armature-level bone, but it ends up going to bone component anyway */
158                 // NOTE: the ID in this case will end up being bArmature.
159                 *type = NodeType::BONE;
160                 *component_name = bone->name;
161                 return true;
162         }
163         else if (RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
164                 Object *object = (Object *)ptr->id.data;
165                 bConstraint *con = (bConstraint *)ptr->data;
166                 /* Check whether is object or bone constraint. */
167                 /* NOTE: Currently none of the area can address transform of an object
168                  * at a given constraint, but for rigging one might use constraint
169                  * influence to be used to drive some corrective shape keys or so. */
170                 if (BLI_findindex(&object->constraints, con) != -1) {
171                         *type = NodeType::TRANSFORM;
172                         *operation_code = OperationCode::TRANSFORM_LOCAL;
173                         return true;
174                 }
175                 else if (object->pose != NULL) {
176                         LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) {
177                                 if (BLI_findindex(&pchan->constraints, con) != -1) {
178                                         *type = NodeType::BONE;
179                                         *operation_code = OperationCode::BONE_LOCAL;
180                                         *component_name = pchan->name;
181                                         return true;
182                                 }
183                         }
184                 }
185         }
186         else if (ELEM(ptr->type, &RNA_ConstraintTarget, &RNA_ConstraintTargetBone)) {
187                 Object *object = (Object *)ptr->id.data;
188                 bConstraintTarget *tgt = (bConstraintTarget *)ptr->data;
189                 /* Check whether is object or bone constraint. */
190                 bPoseChannel *pchan = NULL;
191                 bConstraint *con = BKE_constraint_find_from_target(object, tgt, &pchan);
192                 if (con != NULL) {
193                         if (pchan != NULL) {
194                                 *type = NodeType::BONE;
195                                 *operation_code = OperationCode::BONE_LOCAL;
196                                 *component_name = pchan->name;
197                         }
198                         else {
199                                 *type = NodeType::TRANSFORM;
200                                 *operation_code = OperationCode::TRANSFORM_LOCAL;
201                         }
202                         return true;
203                 }
204         }
205         else if (RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
206                 *type = NodeType::GEOMETRY;
207                 return true;
208         }
209         else if (ptr->type == &RNA_Object) {
210                 /* Transforms props? */
211                 if (prop != NULL) {
212                         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
213                         /* TODO(sergey): How to optimize this? */
214                         if (strstr(prop_identifier, "location") ||
215                             strstr(prop_identifier, "rotation") ||
216                             strstr(prop_identifier, "scale") ||
217                             strstr(prop_identifier, "matrix_"))
218                         {
219                                 *type = NodeType::TRANSFORM;
220                                 return true;
221                         }
222                         else if (strstr(prop_identifier, "data")) {
223                                 /* We access object.data, most likely a geometry.
224                                  * Might be a bone tho. */
225                                 *type = NodeType::GEOMETRY;
226                                 return true;
227                         }
228                 }
229         }
230         else if (ptr->type == &RNA_ShapeKey) {
231                 KeyBlock *key_block = (KeyBlock *)ptr->data;
232                 *id = (ID *)ptr->id.data;
233                 *type = NodeType::PARAMETERS;
234                 *operation_code = OperationCode::PARAMETERS_EVAL;
235                 *operation_name = key_block->name;
236                 return true;
237         }
238         else if (ptr->type == &RNA_Key) {
239                 *id = (ID *)ptr->id.data;
240                 *type = NodeType::GEOMETRY;
241                 return true;
242         }
243         else if (RNA_struct_is_a(ptr->type, &RNA_Sequence)) {
244                 Sequence *seq = (Sequence *)ptr->data;
245                 /* Sequencer strip */
246                 *type = NodeType::SEQUENCER;
247                 *component_name = seq->name;
248                 return true;
249         }
250         else if (RNA_struct_is_a(ptr->type, &RNA_NodeSocket)) {
251                 *type = NodeType::SHADING;
252                 return true;
253         }
254         else if (RNA_struct_is_a(ptr->type, &RNA_ShaderNode)) {
255                 *type = NodeType::SHADING;
256                 return true;
257         }
258         else if (ELEM(ptr->type, &RNA_Curve, &RNA_TextCurve)) {
259                 *id = (ID *)ptr->id.data;
260                 *type = NodeType::GEOMETRY;
261                 return true;
262         }
263         if (prop != NULL) {
264                 /* All unknown data effectively falls under "parameter evaluation". */
265                 if (RNA_property_is_idprop(prop)) {
266                         *type = NodeType::PARAMETERS;
267                         *operation_code = OperationCode::ID_PROPERTY;
268                         *operation_name = RNA_property_identifier((PropertyRNA *)prop);
269                         *operation_name_tag = -1;
270                 }
271                 else {
272                         *type = NodeType::PARAMETERS;
273                         *operation_code = OperationCode::PARAMETERS_EVAL;
274                         *operation_name = "";
275                         *operation_name_tag = -1;
276                 }
277                 return true;
278         }
279         return false;
280 }
281
282 /* Convenience wrapper to find node given just pointer + property. */
283 Node *Depsgraph::find_node_from_pointer(const PointerRNA *ptr,
284                                         const PropertyRNA *prop,
285                                         RNAPointerSource source) const
286 {
287         ID *id;
288         NodeType node_type;
289         const char *component_name, *operation_name;
290         OperationCode operation_code;
291         int operation_name_tag;
292         if (pointer_to_component_node_criteria(
293                          ptr, prop, source,
294                          &id, &node_type, &component_name,
295                          &operation_code, &operation_name, &operation_name_tag))
296         {
297                 IDNode *id_node = find_id_node(id);
298                 if (id_node == NULL) {
299                         return NULL;
300                 }
301                 ComponentNode *comp_node =
302                         id_node->find_component(node_type, component_name);
303                 if (comp_node == NULL) {
304                         return NULL;
305                 }
306                 if (operation_code == OperationCode::OPERATION) {
307                         return comp_node;
308                 }
309                 return comp_node->find_operation(operation_code,
310                                                  operation_name,
311                                                  operation_name_tag);
312         }
313         return NULL;
314 }
315
316 /* Node Management ---------------------------- */
317
318 TimeSourceNode *Depsgraph::add_time_source()
319 {
320         if (time_source == NULL) {
321                 DepsNodeFactory *factory = type_get_factory(NodeType::TIMESOURCE);
322                 time_source = (TimeSourceNode *)factory->create_node(NULL, "", "Time Source");
323         }
324         return time_source;
325 }
326
327 TimeSourceNode *Depsgraph::find_time_source() const
328 {
329         return time_source;
330 }
331
332 IDNode *Depsgraph::find_id_node(const ID *id) const
333 {
334         return reinterpret_cast<IDNode *>(BLI_ghash_lookup(id_hash, id));
335 }
336
337 IDNode *Depsgraph::add_id_node(ID *id, ID *id_cow_hint)
338 {
339         BLI_assert((id->tag & LIB_TAG_COPIED_ON_WRITE) == 0);
340         IDNode *id_node = find_id_node(id);
341         if (!id_node) {
342                 DepsNodeFactory *factory = type_get_factory(NodeType::ID_REF);
343                 id_node = (IDNode *)factory->create_node(id, "", id->name);
344                 id_node->init_copy_on_write(id_cow_hint);
345                 /* Register node in ID hash.
346                  *
347                  * NOTE: We address ID nodes by the original ID pointer they are
348                  * referencing to. */
349                 BLI_ghash_insert(id_hash, id, id_node);
350                 id_nodes.push_back(id_node);
351         }
352         return id_node;
353 }
354
355 void Depsgraph::clear_id_nodes_conditional(const std::function <bool (ID_Type id_type)>& filter)
356 {
357         for (IDNode *id_node : id_nodes) {
358                 if (id_node->id_cow == NULL) {
359                         /* This means builder "stole" ownership of the copy-on-written
360                          * datablock for her own dirty needs. */
361                         continue;
362                 }
363                 if (!deg_copy_on_write_is_expanded(id_node->id_cow)) {
364                         continue;
365                 }
366                 const ID_Type id_type = GS(id_node->id_cow->name);
367                 if (filter(id_type)) {
368                         id_node->destroy();
369                 }
370         }
371 }
372
373 void Depsgraph::clear_id_nodes()
374 {
375         /* Free memory used by ID nodes. */
376
377         /* Stupid workaround to ensure we free IDs in a proper order. */
378         clear_id_nodes_conditional([](ID_Type id_type) { return id_type == ID_SCE; });
379         clear_id_nodes_conditional([](ID_Type id_type) { return id_type != ID_PA; });
380
381         for (IDNode *id_node : id_nodes) {
382                 OBJECT_GUARDED_DELETE(id_node, IDNode);
383         }
384         /* Clear containers. */
385         BLI_ghash_clear(id_hash, NULL, NULL);
386         id_nodes.clear();
387         /* Clear physics relation caches. */
388         clear_physics_relations(this);
389 }
390
391 /* Add new relation between two nodes */
392 Relation *Depsgraph::add_new_relation(Node *from, Node *to,
393                                       const char *description,
394                                       int flags)
395 {
396         Relation *rel = NULL;
397         if (flags & RELATION_CHECK_BEFORE_ADD) {
398                 rel = check_nodes_connected(from, to, description);
399         }
400         if (rel != NULL) {
401                 rel->flag |= flags;
402                 return rel;
403         }
404
405 #ifndef NDEBUG
406         if (from->type == NodeType::OPERATION &&
407             to->type == NodeType::OPERATION)
408         {
409                 OperationNode *operation_from = static_cast<OperationNode *>(from);
410                 OperationNode *operation_to = static_cast<OperationNode *>(to);
411                 BLI_assert(operation_to->owner->type != NodeType::COPY_ON_WRITE ||
412                            operation_from->owner->type == NodeType::COPY_ON_WRITE);
413         }
414 #endif
415
416         /* Create new relation, and add it to the graph. */
417         rel = OBJECT_GUARDED_NEW(Relation, from, to, description);
418         rel->flag |= flags;
419         return rel;
420 }
421
422 Relation *Depsgraph::check_nodes_connected(const Node *from,
423                                            const Node *to,
424                                            const char *description)
425 {
426         for (Relation *rel : from->outlinks) {
427                 BLI_assert(rel->from == from);
428                 if (rel->to != to) {
429                         continue;
430                 }
431                 if (description != NULL && !STREQ(rel->name, description)) {
432                         continue;
433                 }
434                 return rel;
435         }
436         return NULL;
437 }
438
439 /* ************************ */
440 /* Relationships Management */
441
442 Relation::Relation(Node *from, Node *to, const char *description)
443   : from(from),
444     to(to),
445     name(description),
446     flag(0)
447 {
448         /* Hook it up to the nodes which use it.
449          *
450          * NOTE: We register relation in the nodes which this link connects to here
451          * in constructor but we don't unregister it in the destructor.
452          *
453          * Reasoning:
454          *
455          * - Destructor is currently used on global graph destruction, so there's no
456          *   real need in avoiding dangling pointers, all the memory is to be freed
457          *   anyway.
458          *
459          * - Unregistering relation is not a cheap operation, so better to have it
460          *   as an explicit call if we need this. */
461         from->outlinks.push_back(this);
462         to->inlinks.push_back(this);
463 }
464
465 Relation::~Relation()
466 {
467         /* Sanity check. */
468         BLI_assert(from != NULL && to != NULL);
469 }
470
471 void Relation::unlink()
472 {
473         /* Sanity check. */
474         BLI_assert(from != NULL && to != NULL);
475         remove_from_vector(&from->outlinks, this);
476         remove_from_vector(&to->inlinks, this);
477 }
478
479 /* Low level tagging -------------------------------------- */
480
481 /* Tag a specific node as needing updates. */
482 void Depsgraph::add_entry_tag(OperationNode *node)
483 {
484         /* Sanity check. */
485         if (node == NULL) {
486                 return;
487         }
488         /* Add to graph-level set of directly modified nodes to start searching
489          * from.
490          * NOTE: this is necessary since we have several thousand nodes to play
491          * with. */
492         BLI_gset_insert(entry_tags, node);
493 }
494
495 void Depsgraph::clear_all_nodes()
496 {
497         clear_id_nodes();
498         if (time_source != NULL) {
499                 OBJECT_GUARDED_DELETE(time_source, TimeSourceNode);
500                 time_source = NULL;
501         }
502 }
503
504 ID *Depsgraph::get_cow_id(const ID *id_orig) const
505 {
506         IDNode *id_node = find_id_node(id_orig);
507         if (id_node == NULL) {
508                 /* This function is used from places where we expect ID to be either
509                  * already a copy-on-write version or have a corresponding copy-on-write
510                  * version.
511                  *
512                  * We try to enforce that in debug builds, for for release we play a bit
513                  * safer game here. */
514                 if ((id_orig->tag & LIB_TAG_COPIED_ON_WRITE) == 0) {
515                         /* TODO(sergey): This is nice sanity check to have, but it fails
516                          * in following situations:
517                          *
518                          * - Material has link to texture, which is not needed by new
519                          *   shading system and hence can be ignored at construction.
520                          * - Object or mesh has material at a slot which is not used (for
521                          *   example, object has material slot by materials are set to
522                          *   object data). */
523                         // BLI_assert(!"Request for non-existing copy-on-write ID");
524                 }
525                 return (ID *)id_orig;
526         }
527         return id_node->id_cow;
528 }
529
530 }  // namespace DEG
531
532 /* **************** */
533 /* Public Graph API */
534
535 /* Initialize a new Depsgraph */
536 Depsgraph *DEG_graph_new(Scene *scene,
537                          ViewLayer *view_layer,
538                          eEvaluationMode mode)
539 {
540         DEG::Depsgraph *deg_depsgraph = OBJECT_GUARDED_NEW(DEG::Depsgraph,
541                                                            scene,
542                                                            view_layer,
543                                                            mode);
544         return reinterpret_cast<Depsgraph *>(deg_depsgraph);
545 }
546
547 /* Free graph's contents and graph itself */
548 void DEG_graph_free(Depsgraph *graph)
549 {
550         using DEG::Depsgraph;
551         DEG::Depsgraph *deg_depsgraph = reinterpret_cast<DEG::Depsgraph *>(graph);
552         OBJECT_GUARDED_DELETE(deg_depsgraph, Depsgraph);
553 }
554
555 bool DEG_is_active(const struct Depsgraph *depsgraph)
556 {
557         if (depsgraph == NULL) {
558                 /* Happens for such cases as work object in what_does_obaction(),
559                  * and sine render pipeline parts. Shouldn't really be accepting
560                  * NULL depsgraph, but is quite hard to get proper one in those
561                  * cases. */
562                 return false;
563         }
564         const DEG::Depsgraph *deg_graph =
565                 reinterpret_cast<const DEG::Depsgraph *>(depsgraph);
566         return deg_graph->is_active;
567 }
568
569 void DEG_make_active(struct Depsgraph *depsgraph)
570 {
571         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
572         deg_graph->is_active = true;
573         /* TODO(sergey): Copy data from evaluated state to original. */
574 }
575
576 void DEG_make_inactive(struct Depsgraph *depsgraph)
577 {
578         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
579         deg_graph->is_active = false;
580 }