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
21  * \ingroup depsgraph
22  *
23  * Datatypes for internal use in the Depsgraph
24  *
25  * All of these datatypes are only really used within the "core" depsgraph.
26  * In particular, node types declared here form the structure of operations
27  * in the graph.
28  */
29 
30 #pragma once
31 
32 #include <stdlib.h>
33 
34 #include "MEM_guardedalloc.h"
35 
36 #include "DNA_ID.h" /* for ID_Type */
37 
38 #include "BKE_main.h" /* for MAX_LIBARRAY */
39 
40 #include "BLI_threads.h" /* for SpinLock */
41 
42 #include "DEG_depsgraph.h"
43 #include "DEG_depsgraph_physics.h"
44 
45 #include "intern/debug/deg_debug.h"
46 #include "intern/depsgraph_type.h"
47 
48 struct ID;
49 struct Scene;
50 struct ViewLayer;
51 
52 namespace blender {
53 namespace deg {
54 
55 struct IDNode;
56 struct Node;
57 struct OperationNode;
58 struct Relation;
59 struct TimeSourceNode;
60 
61 /* Dependency Graph object */
62 struct Depsgraph {
63   typedef Vector<OperationNode *> OperationNodes;
64   typedef Vector<IDNode *> IDDepsNodes;
65 
66   Depsgraph(Main *bmain, Scene *scene, ViewLayer *view_layer, eEvaluationMode mode);
67   ~Depsgraph();
68 
69   TimeSourceNode *add_time_source();
70   TimeSourceNode *find_time_source() const;
71   void tag_time_source();
72 
73   IDNode *find_id_node(const ID *id) const;
74   IDNode *add_id_node(ID *id, ID *id_cow_hint = nullptr);
75   void clear_id_nodes();
76   void clear_id_nodes_conditional(const std::function<bool(ID_Type id_type)> &filter);
77 
78   /* Add new relationship between two nodes. */
79   Relation *add_new_relation(Node *from, Node *to, const char *description, int flags = 0);
80 
81   /* Check whether two nodes are connected by relation with given
82    * description. Description might be nullptr to check ANY relation between
83    * given nodes. */
84   Relation *check_nodes_connected(const Node *from, const Node *to, const char *description);
85 
86   /* Tag a specific node as needing updates. */
87   void add_entry_tag(OperationNode *node);
88 
89   /* Clear storage used by all nodes. */
90   void clear_all_nodes();
91 
92   /* Copy-on-Write Functionality ........ */
93 
94   /* For given original ID get ID which is created by CoW system. */
95   ID *get_cow_id(const ID *id_orig) const;
96 
97   /* Core Graph Functionality ........... */
98 
99   /* <ID : IDNode> mapping from ID blocks to nodes representing these
100    * blocks, used for quick lookups. */
101   Map<const ID *, IDNode *> id_hash;
102 
103   /* Ordered list of ID nodes, order matches ID allocation order.
104    * Used for faster iteration, especially for areas which are critical to
105    * keep exact order of iteration. */
106   IDDepsNodes id_nodes;
107 
108   /* Top-level time source node. */
109   TimeSourceNode *time_source;
110 
111   /* Indicates whether relations needs to be updated. */
112   bool need_update;
113 
114   /* Indicates which ID types were updated. */
115   char id_type_updated[MAX_LIBARRAY];
116 
117   /* Indicates type of IDs present in the depsgraph. */
118   char id_type_exist[MAX_LIBARRAY];
119 
120   /* Quick-Access Temp Data ............. */
121 
122   /* Nodes which have been tagged as "directly modified". */
123   Set<OperationNode *> entry_tags;
124 
125   /* Convenience Data ................... */
126 
127   /* XXX: should be collected after building (if actually needed?) */
128   /* All operation nodes, sorted in order of single-thread traversal order. */
129   OperationNodes operations;
130 
131   /* Spin lock for threading-critical operations.
132    * Mainly used by graph evaluation. */
133   SpinLock lock;
134 
135   /* Main, scene, layer, mode this dependency graph is built for. */
136   Main *bmain;
137   Scene *scene;
138   ViewLayer *view_layer;
139   eEvaluationMode mode;
140 
141   /* Time at which dependency graph is being or was last evaluated. */
142   float ctime;
143 
144   /* Evaluated version of datablocks we access a lot.
145    * Stored here to save us form doing hash lookup. */
146   Scene *scene_cow;
147 
148   /* Active dependency graph is a dependency graph which is used by the
149    * currently active window. When dependency graph is active, it is allowed
150    * for evaluation functions to write animation f-curve result, drivers
151    * result and other selective things (object matrix?) to original object.
152    *
153    * This way we simplify operators, which don't need to worry about where
154    * to read stuff from. */
155   bool is_active;
156 
157   DepsgraphDebug debug;
158 
159   bool is_evaluating;
160 
161   /* Is set to truth for dependency graph which are used for post-processing (compositor and
162    * sequencer).
163    * Such dependency graph needs all view layers (so render pipeline can access names), but it
164    * does not need any bases. */
165   bool is_render_pipeline_depsgraph;
166 
167   /* Cached list of colliders/effectors for collections and the scene
168    * created along with relations, for fast lookup during evaluation. */
169   Map<const ID *, ListBase *> *physics_relations[DEG_PHYSICS_RELATIONS_NUM];
170 
171   MEM_CXX_CLASS_ALLOC_FUNCS("Depsgraph");
172 };
173 
174 }  // namespace deg
175 }  // namespace blender
176