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