46a9d2c5d726a8f1abdcbac46d9995d8eb297c41
[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          */
134         struct DupliObject *dupli_object_current;
135         /* Temporary storage to report fully populated DNA to the render engine or
136          * other users of the iterator.
137          */
138         struct Object temp_dupli_object;
139
140         /* **** Iteration over ID nodes **** */
141         size_t id_node_index;
142         size_t num_id_nodes;
143 } DEGObjectIterData;
144
145 void DEG_iterator_objects_begin(struct BLI_Iterator *iter, DEGObjectIterData *data);
146 void DEG_iterator_objects_next(struct BLI_Iterator *iter);
147 void DEG_iterator_objects_end(struct BLI_Iterator *iter);
148
149 /**
150  * Note: Be careful with DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY objects.
151  * Although they are available they have no overrides (collection_properties)
152  * and will crash if you try to access it.
153  */
154 #define DEG_OBJECT_ITER_BEGIN(graph_, instance_, flag_)                           \
155         {                                                                             \
156                 DEGObjectIterData data_ = {                                               \
157                         graph_,                                                               \
158                         flag_                                                                 \
159                 };                                                                        \
160                                                                                   \
161                 ITER_BEGIN(DEG_iterator_objects_begin,                                    \
162                            DEG_iterator_objects_next,                                     \
163                            DEG_iterator_objects_end,                                      \
164                            &data_, Object *, instance_)
165
166 #define DEG_OBJECT_ITER_END                                                       \
167                 ITER_END;                                                                 \
168         }
169
170 /**
171   * Depsgraph objects iterator for draw manager and final render
172   */
173 #define DEG_OBJECT_ITER_FOR_RENDER_ENGINE_BEGIN(graph_, instance_)        \
174         DEG_OBJECT_ITER_BEGIN(graph_, instance_,                              \
175                 DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |                        \
176                 DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET |                         \
177                 DEG_ITER_OBJECT_FLAG_VISIBLE |                                \
178                 DEG_ITER_OBJECT_FLAG_DUPLI)
179
180 #define DEG_OBJECT_ITER_FOR_RENDER_ENGINE_END                             \
181         DEG_OBJECT_ITER_END
182
183
184 /* ************************ DEG ID iterators ********************* */
185
186 typedef struct DEGIDIterData {
187         struct Depsgraph *graph;
188         bool only_updated;
189
190         size_t id_node_index;
191         size_t num_id_nodes;
192 } DEGIDIterData;
193
194 void DEG_iterator_ids_begin(struct BLI_Iterator *iter, DEGIDIterData *data);
195 void DEG_iterator_ids_next(struct BLI_Iterator *iter);
196 void DEG_iterator_ids_end(struct BLI_Iterator *iter);
197
198 /* ************************ DEG traversal ********************* */
199
200 typedef void (*DEGForeachIDCallback)(ID *id, void *user_data);
201
202 /* NOTE: Modifies runtime flags in depsgraph nodes, so can not be used in
203  * parallel. Keep an eye on that!
204  */
205 void DEG_foreach_ancestor_ID(const Depsgraph *depsgraph,
206                              const ID *id,
207                              DEGForeachIDCallback callback, void *user_data);
208 void DEG_foreach_dependent_ID(const Depsgraph *depsgraph,
209                               const ID *id,
210                               DEGForeachIDCallback callback, void *user_data);
211
212 void DEG_foreach_ID(const Depsgraph *depsgraph,
213                     DEGForeachIDCallback callback, void *user_data);
214
215 /* ********************* DEG graph filtering ****************** */
216
217 /* ComponentKey for nodes we want to be able to evaluate in the filtered graph */
218 typedef struct DEG_FilterTarget {
219         struct DEG_FilterTarget *next, *prev;
220
221         struct ID *id;
222         /* TODO: component identifiers - Component Type, Subdata/Component Name */
223 } DEG_FilterTarget;
224
225 typedef enum eDEG_FilterQuery_Granularity {
226         DEG_FILTER_NODES_ALL           = 0,
227         DEG_FILTER_NODES_NO_OPS        = 1,
228         DEG_FILTER_NODES_ID_ONLY       = 2,
229 } eDEG_FilterQuery_Granularity;
230
231
232 typedef struct DEG_FilterQuery {
233         /* List of DEG_FilterTarget's */
234         struct ListBase targets;
235
236         /* Level of detail in the resulting graph */
237         eDEG_FilterQuery_Granularity detail_level;
238 } DEG_FilterQuery;
239
240 /* Obtain a new graph instance that only contains the subset of desired nodes
241  * WARNING: Do NOT pass an already filtered depsgraph through this function again,
242  *          as we are currently unable to accurately recreate it.
243  */
244 Depsgraph *DEG_graph_filter(const Depsgraph *depsgraph, struct Main *bmain, DEG_FilterQuery *query);
245
246
247 #ifdef __cplusplus
248 } /* extern "C" */
249 #endif
250
251 #endif  /* __DEG_DEPSGRAPH_QUERY_H__ */