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) 2017 Blender Foundation.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup depsgraph
22 *
23 * Implementation of Querying and Filtering API's
24 */
25
26 /* Silence warnings from copying deprecated fields. */
27 #define DNA_DEPRECATED_ALLOW
28
29 #include "MEM_guardedalloc.h"
30
31 #include "BKE_duplilist.h"
32 #include "BKE_idprop.h"
33 #include "BKE_layer.h"
34 #include "BKE_node.h"
35 #include "BKE_object.h"
36
37 #include "BLI_math.h"
38 #include "BLI_utildefines.h"
39
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42
43 #include "DEG_depsgraph.h"
44 #include "DEG_depsgraph_query.h"
45
46 #include "intern/depsgraph.h"
47 #include "intern/node/deg_node_id.h"
48
49 #ifndef NDEBUG
50 # include "intern/eval/deg_eval_copy_on_write.h"
51 #endif
52
53 // If defined, all working data will be set to an invalid state, helping
54 // to catch issues when areas accessing data which is considered to be no
55 // longer available.
56 #undef INVALIDATE_WORK_DATA
57
58 #ifndef NDEBUG
59 # define INVALIDATE_WORK_DATA
60 #endif
61
62 namespace deg = blender::deg;
63
64 /* ************************ DEG ITERATORS ********************* */
65
66 namespace {
67
deg_invalidate_iterator_work_data(DEGObjectIterData * data)68 void deg_invalidate_iterator_work_data(DEGObjectIterData *data)
69 {
70 #ifdef INVALIDATE_WORK_DATA
71 BLI_assert(data != nullptr);
72 memset(&data->temp_dupli_object, 0xff, sizeof(data->temp_dupli_object));
73 #else
74 (void)data;
75 #endif
76 }
77
verify_id_properties_freed(DEGObjectIterData * data)78 void verify_id_properties_freed(DEGObjectIterData *data)
79 {
80 if (data->dupli_object_current == nullptr) {
81 // We didn't enter duplication yet, so we can't have any dangling
82 // pointers.
83 return;
84 }
85 const Object *dupli_object = data->dupli_object_current->ob;
86 Object *temp_dupli_object = &data->temp_dupli_object;
87 if (temp_dupli_object->id.properties == nullptr) {
88 // No ID properties in temp datablock -- no leak is possible.
89 return;
90 }
91 if (temp_dupli_object->id.properties == dupli_object->id.properties) {
92 // Temp copy of object did not modify ID properties.
93 return;
94 }
95 // Free memory which is owned by temporary storage which is about to
96 // get overwritten.
97 IDP_FreeProperty(temp_dupli_object->id.properties);
98 temp_dupli_object->id.properties = nullptr;
99 }
100
deg_object_hide_original(eEvaluationMode eval_mode,Object * ob,DupliObject * dob)101 bool deg_object_hide_original(eEvaluationMode eval_mode, Object *ob, DupliObject *dob)
102 {
103 /* Automatic hiding if this object is being instanced on verts/faces/frames
104 * by its parent. Ideally this should not be needed, but due to the wrong
105 * dependency direction in the data design there is no way to keep the object
106 * visible otherwise. The better solution eventually would be for objects
107 * to specify which object they instance, instead of through parenting.
108 *
109 * This function should not be used for meta-balls. They have custom visibility rules, as hiding
110 * the base meta-ball will also hide all the other balls in the group. */
111 if (eval_mode == DAG_EVAL_RENDER || dob) {
112 const int hide_original_types = OB_DUPLIVERTS | OB_DUPLIFACES;
113
114 if (!dob || !(dob->type & hide_original_types)) {
115 if (ob->parent && (ob->parent->transflag & hide_original_types)) {
116 return true;
117 }
118 }
119 }
120
121 return false;
122 }
123
deg_objects_dupli_iterator_next(BLI_Iterator * iter)124 bool deg_objects_dupli_iterator_next(BLI_Iterator *iter)
125 {
126 DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
127 while (data->dupli_object_next != nullptr) {
128 DupliObject *dob = data->dupli_object_next;
129 Object *obd = dob->ob;
130
131 data->dupli_object_next = data->dupli_object_next->next;
132
133 if (dob->no_draw) {
134 continue;
135 }
136 if (obd->type == OB_MBALL) {
137 continue;
138 }
139 if (deg_object_hide_original(data->eval_mode, dob->ob, dob)) {
140 continue;
141 }
142
143 verify_id_properties_freed(data);
144
145 data->dupli_object_current = dob;
146
147 /* Temporary object to evaluate. */
148 Object *dupli_parent = data->dupli_parent;
149 Object *temp_dupli_object = &data->temp_dupli_object;
150 *temp_dupli_object = *dob->ob;
151 temp_dupli_object->base_flag = dupli_parent->base_flag | BASE_FROM_DUPLI;
152 temp_dupli_object->base_local_view_bits = dupli_parent->base_local_view_bits;
153 temp_dupli_object->runtime.local_collections_bits =
154 dupli_parent->runtime.local_collections_bits;
155 temp_dupli_object->dt = MIN2(temp_dupli_object->dt, dupli_parent->dt);
156 copy_v4_v4(temp_dupli_object->color, dupli_parent->color);
157
158 /* Duplicated elements shouldn't care whether their original collection is visible or not. */
159 temp_dupli_object->base_flag |= BASE_VISIBLE_DEPSGRAPH;
160
161 int ob_visibility = BKE_object_visibility(temp_dupli_object, data->eval_mode);
162 if ((ob_visibility & (OB_VISIBLE_SELF | OB_VISIBLE_PARTICLES)) == 0) {
163 continue;
164 }
165
166 /* This could be avoided by refactoring make_dupli() in order to track all negative scaling
167 * recursively. */
168 bool is_neg_scale = is_negative_m4(dob->mat);
169 SET_FLAG_FROM_TEST(data->temp_dupli_object.transflag, is_neg_scale, OB_NEG_SCALE);
170
171 copy_m4_m4(data->temp_dupli_object.obmat, dob->mat);
172 invert_m4_m4(data->temp_dupli_object.imat, data->temp_dupli_object.obmat);
173 iter->current = &data->temp_dupli_object;
174 BLI_assert(deg::deg_validate_copy_on_write_datablock(&data->temp_dupli_object.id));
175 return true;
176 }
177
178 return false;
179 }
180
deg_iterator_objects_step(BLI_Iterator * iter,deg::IDNode * id_node)181 void deg_iterator_objects_step(BLI_Iterator *iter, deg::IDNode *id_node)
182 {
183 /* Set it early in case we need to exit and we are running from within a loop. */
184 iter->skip = true;
185
186 if (!id_node->is_directly_visible) {
187 return;
188 }
189
190 DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
191 const ID_Type id_type = GS(id_node->id_orig->name);
192
193 if (id_type != ID_OB) {
194 return;
195 }
196
197 switch (id_node->linked_state) {
198 case deg::DEG_ID_LINKED_DIRECTLY:
199 if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY) == 0) {
200 return;
201 }
202 break;
203 case deg::DEG_ID_LINKED_VIA_SET:
204 if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET) == 0) {
205 return;
206 }
207 break;
208 case deg::DEG_ID_LINKED_INDIRECTLY:
209 if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY) == 0) {
210 return;
211 }
212 break;
213 }
214
215 Object *object = (Object *)id_node->id_cow;
216 BLI_assert(deg::deg_validate_copy_on_write_datablock(&object->id));
217
218 int ob_visibility = OB_VISIBLE_ALL;
219 if (data->flag & DEG_ITER_OBJECT_FLAG_VISIBLE) {
220 ob_visibility = BKE_object_visibility(object, data->eval_mode);
221
222 if (object->type != OB_MBALL && deg_object_hide_original(data->eval_mode, object, nullptr)) {
223 return;
224 }
225 }
226
227 if (ob_visibility & OB_VISIBLE_INSTANCES) {
228 if ((data->flag & DEG_ITER_OBJECT_FLAG_DUPLI) && (object->transflag & OB_DUPLI)) {
229 data->dupli_parent = object;
230 data->dupli_list = object_duplilist(data->graph, data->scene, object);
231 data->dupli_object_next = (DupliObject *)data->dupli_list->first;
232 }
233 }
234
235 if (ob_visibility & (OB_VISIBLE_SELF | OB_VISIBLE_PARTICLES)) {
236 iter->current = object;
237 iter->skip = false;
238 }
239 }
240
241 } // namespace
242
DEG_iterator_objects_begin(BLI_Iterator * iter,DEGObjectIterData * data)243 void DEG_iterator_objects_begin(BLI_Iterator *iter, DEGObjectIterData *data)
244 {
245 Depsgraph *depsgraph = data->graph;
246 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
247 const size_t num_id_nodes = deg_graph->id_nodes.size();
248
249 iter->data = data;
250
251 if (num_id_nodes == 0) {
252 iter->valid = false;
253 return;
254 }
255
256 data->dupli_parent = nullptr;
257 data->dupli_list = nullptr;
258 data->dupli_object_next = nullptr;
259 data->dupli_object_current = nullptr;
260 data->scene = DEG_get_evaluated_scene(depsgraph);
261 data->id_node_index = 0;
262 data->num_id_nodes = num_id_nodes;
263 data->eval_mode = DEG_get_mode(depsgraph);
264 deg_invalidate_iterator_work_data(data);
265
266 deg::IDNode *id_node = deg_graph->id_nodes[data->id_node_index];
267 deg_iterator_objects_step(iter, id_node);
268
269 if (iter->skip) {
270 DEG_iterator_objects_next(iter);
271 }
272 }
273
DEG_iterator_objects_next(BLI_Iterator * iter)274 void DEG_iterator_objects_next(BLI_Iterator *iter)
275 {
276 DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
277 Depsgraph *depsgraph = data->graph;
278 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
279 do {
280 iter->skip = false;
281 if (data->dupli_list) {
282 if (deg_objects_dupli_iterator_next(iter)) {
283 return;
284 }
285
286 verify_id_properties_freed(data);
287 free_object_duplilist(data->dupli_list);
288 data->dupli_parent = nullptr;
289 data->dupli_list = nullptr;
290 data->dupli_object_next = nullptr;
291 data->dupli_object_current = nullptr;
292 deg_invalidate_iterator_work_data(data);
293 }
294
295 ++data->id_node_index;
296 if (data->id_node_index == data->num_id_nodes) {
297 iter->valid = false;
298 return;
299 }
300
301 deg::IDNode *id_node = deg_graph->id_nodes[data->id_node_index];
302 deg_iterator_objects_step(iter, id_node);
303 } while (iter->skip);
304 }
305
DEG_iterator_objects_end(BLI_Iterator * iter)306 void DEG_iterator_objects_end(BLI_Iterator *iter)
307 {
308 DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
309 if (data != nullptr) {
310 /* Force crash in case the iterator data is referenced and accessed down
311 * the line. (T51718) */
312 deg_invalidate_iterator_work_data(data);
313 }
314 }
315
316 /* ************************ DEG ID ITERATOR ********************* */
317
DEG_iterator_ids_step(BLI_Iterator * iter,deg::IDNode * id_node,bool only_updated)318 static void DEG_iterator_ids_step(BLI_Iterator *iter, deg::IDNode *id_node, bool only_updated)
319 {
320 ID *id_cow = id_node->id_cow;
321
322 if (!id_node->is_directly_visible) {
323 iter->skip = true;
324 return;
325 }
326 if (only_updated && !(id_cow->recalc & ID_RECALC_ALL)) {
327 bNodeTree *ntree = ntreeFromID(id_cow);
328
329 /* Nodetree is considered part of the datablock. */
330 if (!(ntree && (ntree->id.recalc & ID_RECALC_ALL))) {
331 iter->skip = true;
332 return;
333 }
334 }
335
336 iter->current = id_cow;
337 iter->skip = false;
338 }
339
DEG_iterator_ids_begin(BLI_Iterator * iter,DEGIDIterData * data)340 void DEG_iterator_ids_begin(BLI_Iterator *iter, DEGIDIterData *data)
341 {
342 Depsgraph *depsgraph = data->graph;
343 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
344 const size_t num_id_nodes = deg_graph->id_nodes.size();
345
346 iter->data = data;
347
348 if ((num_id_nodes == 0) || (data->only_updated && !DEG_id_type_any_updated(depsgraph))) {
349 iter->valid = false;
350 return;
351 }
352
353 data->id_node_index = 0;
354 data->num_id_nodes = num_id_nodes;
355
356 deg::IDNode *id_node = deg_graph->id_nodes[data->id_node_index];
357 DEG_iterator_ids_step(iter, id_node, data->only_updated);
358
359 if (iter->skip) {
360 DEG_iterator_ids_next(iter);
361 }
362 }
363
DEG_iterator_ids_next(BLI_Iterator * iter)364 void DEG_iterator_ids_next(BLI_Iterator *iter)
365 {
366 DEGIDIterData *data = (DEGIDIterData *)iter->data;
367 Depsgraph *depsgraph = data->graph;
368 deg::Depsgraph *deg_graph = reinterpret_cast<deg::Depsgraph *>(depsgraph);
369
370 do {
371 iter->skip = false;
372
373 ++data->id_node_index;
374 if (data->id_node_index == data->num_id_nodes) {
375 iter->valid = false;
376 return;
377 }
378
379 deg::IDNode *id_node = deg_graph->id_nodes[data->id_node_index];
380 DEG_iterator_ids_step(iter, id_node, data->only_updated);
381 } while (iter->skip);
382 }
383
DEG_iterator_ids_end(BLI_Iterator * UNUSED (iter))384 void DEG_iterator_ids_end(BLI_Iterator *UNUSED(iter))
385 {
386 }
387