Depsgraph: Comb code to a better state all over
[blender.git] / source / blender / depsgraph / DEG_depsgraph_query.h
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): Sergey Sharybin
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/DEG_depsgraph_query.h
28  *  \ingroup depsgraph
29  *
30  * Public API for Querying and Filtering Depsgraph.
31  */
32
33 #ifndef __DEG_DEPSGRAPH_QUERY_H__
34 #define __DEG_DEPSGRAPH_QUERY_H__
35
36 #include "DEG_depsgraph.h"
37 #include "DEG_depsgraph_build.h"
38
39 struct ID;
40
41 struct BLI_Iterator;
42 struct Base;
43 struct Depsgraph;
44 struct DupliObject;
45 struct ListBase;
46 struct Scene;
47 struct ViewLayer;
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 /* *********************** DEG input data ********************* */
54
55 /* Get scene that depsgraph was built for. */
56 struct Scene *DEG_get_input_scene(const Depsgraph *graph);
57
58 /* Get view layer that depsgraph was built for. */
59 struct ViewLayer *DEG_get_input_view_layer(const Depsgraph *graph);
60
61 /* Get evaluation mode that depsgraph was built for. */
62 eEvaluationMode DEG_get_mode(const Depsgraph *graph);
63
64 /* Get time that depsgraph is being evaluated or was last evaluated at. */
65 float DEG_get_ctime(const Depsgraph *graph);
66
67 /* ********************* DEG evaluated data ******************* */
68
69 /* Check if given ID type was tagged for update. */
70 bool DEG_id_type_updated(const struct Depsgraph *depsgraph, short id_type);
71 bool DEG_id_type_any_updated(const struct Depsgraph *depsgraph);
72
73 /* Get additional evaluation flags for the given ID. */
74 uint32_t DEG_get_eval_flags_for_id(const struct Depsgraph *graph, struct ID *id);
75
76 /* Get additional mesh CustomDataMask flags for the given object. */
77 uint64_t DEG_get_customdata_mask_for_object(const struct Depsgraph *graph,
78                                             struct Object *object);
79
80 /* Get scene the despgraph is created for. */
81 struct Scene *DEG_get_evaluated_scene(const struct Depsgraph *graph);
82
83 /* Get scene layer the despgraph is created for. */
84 struct ViewLayer *DEG_get_evaluated_view_layer(const struct Depsgraph *graph);
85
86 /* Get evaluated version of object for given original one. */
87 struct Object *DEG_get_evaluated_object(const struct Depsgraph *depsgraph,
88                                         struct Object *object);
89
90 /* Get evaluated version of given ID datablock. */
91 struct ID *DEG_get_evaluated_id(const struct Depsgraph *depsgraph,
92                                 struct ID *id);
93
94 /* Get evaluated version of data pointed to by RNA pointer */
95 void DEG_get_evaluated_rna_pointer(const struct Depsgraph *depsgraph,
96                                    struct PointerRNA *ptr,
97                                    struct PointerRNA *r_ptr_eval);
98
99 /* Get original version of object for given evaluated one. */
100 struct Object *DEG_get_original_object(struct Object *object);
101
102 /* Get original version of given evaluated ID datablock. */
103 struct ID *DEG_get_original_id(struct ID *id);
104
105 /* ************************ DEG object iterators ********************* */
106
107 enum {
108         DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY   = (1 << 0),
109         DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY = (1 << 1),
110         DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET    = (1 << 2),
111         DEG_ITER_OBJECT_FLAG_VISIBLE           = (1 << 3),
112         DEG_ITER_OBJECT_FLAG_DUPLI             = (1 << 4),
113 };
114
115 typedef struct DEGObjectIterData {
116         struct Depsgraph *graph;
117         int flag;
118
119         struct Scene *scene;
120
121         eEvaluationMode eval_mode;
122
123         /* **** Iteration over dupli-list. *** */
124
125         /* Object which created the dupli-list. */
126         struct Object *dupli_parent;
127         /* List of duplicated objects. */
128         struct ListBase *dupli_list;
129         /* Next duplicated object to step into. */
130         struct DupliObject *dupli_object_next;
131         /* Corresponds to current object: current iterator object is evaluated from
132          * this duplicated object. */
133         struct DupliObject *dupli_object_current;
134         /* Temporary storage to report fully populated DNA to the render engine or
135          * other users of the iterator. */
136         struct Object temp_dupli_object;
137
138         /* **** Iteration over ID nodes **** */
139         size_t id_node_index;
140         size_t num_id_nodes;
141 } DEGObjectIterData;
142
143 void DEG_iterator_objects_begin(struct BLI_Iterator *iter, DEGObjectIterData *data);
144 void DEG_iterator_objects_next(struct BLI_Iterator *iter);
145 void DEG_iterator_objects_end(struct BLI_Iterator *iter);
146
147 /**
148  * Note: Be careful with DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY objects.
149  * Although they are available they have no overrides (collection_properties)
150  * and will crash if you try to access it.
151  */
152 #define DEG_OBJECT_ITER_BEGIN(graph_, instance_, flag_)                           \
153         {                                                                             \
154                 DEGObjectIterData data_ = {                                               \
155                         graph_,                                                               \
156                         flag_                                                                 \
157                 };                                                                        \
158                                                                                   \
159                 ITER_BEGIN(DEG_iterator_objects_begin,                                    \
160                            DEG_iterator_objects_next,                                     \
161                            DEG_iterator_objects_end,                                      \
162                            &data_, Object *, instance_)
163
164 #define DEG_OBJECT_ITER_END                                                       \
165                 ITER_END;                                                                 \
166         }
167
168 /**
169   * Depsgraph objects iterator for draw manager and final render
170   */
171 #define DEG_OBJECT_ITER_FOR_RENDER_ENGINE_BEGIN(graph_, instance_)        \
172         DEG_OBJECT_ITER_BEGIN(graph_, instance_,                              \
173                 DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |                        \
174                 DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET |                         \
175                 DEG_ITER_OBJECT_FLAG_VISIBLE |                                \
176                 DEG_ITER_OBJECT_FLAG_DUPLI)
177
178 #define DEG_OBJECT_ITER_FOR_RENDER_ENGINE_END                             \
179         DEG_OBJECT_ITER_END
180
181
182 /* ************************ DEG ID iterators ********************* */
183
184 typedef struct DEGIDIterData {
185         struct Depsgraph *graph;
186         bool only_updated;
187
188         size_t id_node_index;
189         size_t num_id_nodes;
190 } DEGIDIterData;
191
192 void DEG_iterator_ids_begin(struct BLI_Iterator *iter, DEGIDIterData *data);
193 void DEG_iterator_ids_next(struct BLI_Iterator *iter);
194 void DEG_iterator_ids_end(struct BLI_Iterator *iter);
195
196 /* ************************ DEG traversal ********************* */
197
198 typedef void (*DEGForeachIDCallback)(ID *id, void *user_data);
199
200 /* NOTE: Modifies runtime flags in depsgraph nodes, so can not be used in
201  * parallel. Keep an eye on that!
202  */
203 void DEG_foreach_ancestor_ID(const Depsgraph *depsgraph,
204                              const ID *id,
205                              DEGForeachIDCallback callback, void *user_data);
206 void DEG_foreach_dependent_ID(const Depsgraph *depsgraph,
207                               const ID *id,
208                               DEGForeachIDCallback callback, void *user_data);
209
210 void DEG_foreach_ID(const Depsgraph *depsgraph,
211                     DEGForeachIDCallback callback, void *user_data);
212
213 /* ********************* DEG graph filtering ****************** */
214
215 /* ComponentKey for nodes we want to be able to evaluate in the filtered graph */
216 typedef struct DEG_FilterTarget {
217         struct DEG_FilterTarget *next, *prev;
218
219         struct ID *id;
220         /* TODO: component identifiers - Component Type, Subdata/Component Name */
221 } DEG_FilterTarget;
222
223 typedef enum eDEG_FilterQuery_Granularity {
224         DEG_FILTER_NODES_ALL           = 0,
225         DEG_FILTER_NODES_NO_OPS        = 1,
226         DEG_FILTER_NODES_ID_ONLY       = 2,
227 } eDEG_FilterQuery_Granularity;
228
229
230 typedef struct DEG_FilterQuery {
231         /* List of DEG_FilterTarget's */
232         struct ListBase targets;
233
234         /* Level of detail in the resulting graph */
235         eDEG_FilterQuery_Granularity detail_level;
236 } DEG_FilterQuery;
237
238 /* Obtain a new graph instance that only contains the subset of desired nodes
239  * WARNING: Do NOT pass an already filtered depsgraph through this function again,
240  *          as we are currently unable to accurately recreate it.
241  */
242 Depsgraph *DEG_graph_filter(const Depsgraph *depsgraph, struct Main *bmain, DEG_FilterQuery *query);
243
244
245 #ifdef __cplusplus
246 } /* extern "C" */
247 #endif
248
249 #endif  /* __DEG_DEPSGRAPH_QUERY_H__ */