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) 2016 Blender Foundation.
17 * All rights reserved.
18 */
19
20 /** \file
21 * \ingroup depsgraph
22 */
23
24 #include "intern/builder/deg_builder.h"
25
26 #include <cstring>
27
28 #include "DNA_ID.h"
29 #include "DNA_anim_types.h"
30 #include "DNA_armature_types.h"
31 #include "DNA_layer_types.h"
32 #include "DNA_object_types.h"
33
34 #include "BLI_stack.h"
35 #include "BLI_utildefines.h"
36
37 #include "BKE_action.h"
38
39 #include "intern/builder/deg_builder_cache.h"
40 #include "intern/builder/deg_builder_remove_noop.h"
41 #include "intern/depsgraph.h"
42 #include "intern/depsgraph_relation.h"
43 #include "intern/depsgraph_tag.h"
44 #include "intern/depsgraph_type.h"
45 #include "intern/eval/deg_eval_copy_on_write.h"
46 #include "intern/node/deg_node.h"
47 #include "intern/node/deg_node_component.h"
48 #include "intern/node/deg_node_id.h"
49 #include "intern/node/deg_node_operation.h"
50
51 #include "DEG_depsgraph.h"
52
53 namespace blender {
54 namespace deg {
55
deg_check_id_in_depsgraph(const Depsgraph * graph,ID * id_orig)56 bool deg_check_id_in_depsgraph(const Depsgraph *graph, ID *id_orig)
57 {
58 IDNode *id_node = graph->find_id_node(id_orig);
59 return id_node != nullptr;
60 }
61
deg_check_base_in_depsgraph(const Depsgraph * graph,Base * base)62 bool deg_check_base_in_depsgraph(const Depsgraph *graph, Base *base)
63 {
64 Object *object_orig = base->base_orig->object;
65 IDNode *id_node = graph->find_id_node(&object_orig->id);
66 if (id_node == nullptr) {
67 return false;
68 }
69 return id_node->has_base;
70 }
71
72 /*******************************************************************************
73 * Base class for builders.
74 */
75
DepsgraphBuilder(Main * bmain,Depsgraph * graph,DepsgraphBuilderCache * cache)76 DepsgraphBuilder::DepsgraphBuilder(Main *bmain, Depsgraph *graph, DepsgraphBuilderCache *cache)
77 : bmain_(bmain), graph_(graph), cache_(cache)
78 {
79 }
80
~DepsgraphBuilder()81 DepsgraphBuilder::~DepsgraphBuilder()
82 {
83 }
84
need_pull_base_into_graph(Base * base)85 bool DepsgraphBuilder::need_pull_base_into_graph(Base *base)
86 {
87 /* Simple check: enabled bases are always part of dependency graph. */
88 const int base_flag = (graph_->mode == DAG_EVAL_VIEWPORT) ? BASE_ENABLED_VIEWPORT :
89 BASE_ENABLED_RENDER;
90 if (base->flag & base_flag) {
91 return true;
92 }
93 /* More involved check: since we don't support dynamic changes in dependency graph topology and
94 * all visible objects are to be part of dependency graph, we pull all objects which has animated
95 * visibility. */
96 Object *object = base->object;
97 AnimatedPropertyID property_id;
98 if (graph_->mode == DAG_EVAL_VIEWPORT) {
99 property_id = AnimatedPropertyID(&object->id, &RNA_Object, "hide_viewport");
100 }
101 else if (graph_->mode == DAG_EVAL_RENDER) {
102 property_id = AnimatedPropertyID(&object->id, &RNA_Object, "hide_render");
103 }
104 else {
105 BLI_assert(!"Unknown evaluation mode.");
106 return false;
107 }
108 return cache_->isPropertyAnimated(&object->id, property_id);
109 }
110
check_pchan_has_bbone(Object * object,const bPoseChannel * pchan)111 bool DepsgraphBuilder::check_pchan_has_bbone(Object *object, const bPoseChannel *pchan)
112 {
113 BLI_assert(object->type == OB_ARMATURE);
114 if (pchan == nullptr || pchan->bone == nullptr) {
115 return false;
116 }
117 /* We don't really care whether segments are higher than 1 due to static user input (as in,
118 * rigger entered value like 3 manually), or due to animation. In either way we need to create
119 * special evaluation. */
120 if (pchan->bone->segments > 1) {
121 return true;
122 }
123 bArmature *armature = static_cast<bArmature *>(object->data);
124 AnimatedPropertyID property_id(&armature->id, &RNA_Bone, pchan->bone, "bbone_segments");
125 /* Check both Object and Armature animation data, because drivers modifying Armature
126 * state could easily be created in the Object AnimData. */
127 return cache_->isPropertyAnimated(&object->id, property_id) ||
128 cache_->isPropertyAnimated(&armature->id, property_id);
129 }
130
check_pchan_has_bbone_segments(Object * object,const bPoseChannel * pchan)131 bool DepsgraphBuilder::check_pchan_has_bbone_segments(Object *object, const bPoseChannel *pchan)
132 {
133 /* Proxies don't have BONE_SEGMENTS */
134 if (ID_IS_LINKED(object) && object->proxy_from != nullptr) {
135 return false;
136 }
137 return check_pchan_has_bbone(object, pchan);
138 }
139
check_pchan_has_bbone_segments(Object * object,const char * bone_name)140 bool DepsgraphBuilder::check_pchan_has_bbone_segments(Object *object, const char *bone_name)
141 {
142 const bPoseChannel *pchan = BKE_pose_channel_find_name(object->pose, bone_name);
143 return check_pchan_has_bbone_segments(object, pchan);
144 }
145
146 /*******************************************************************************
147 * Builder finalizer.
148 */
149
150 namespace {
151
deg_graph_build_flush_visibility(Depsgraph * graph)152 void deg_graph_build_flush_visibility(Depsgraph *graph)
153 {
154 enum {
155 DEG_NODE_VISITED = (1 << 0),
156 };
157
158 BLI_Stack *stack = BLI_stack_new(sizeof(OperationNode *), "DEG flush layers stack");
159 for (IDNode *id_node : graph->id_nodes) {
160 for (ComponentNode *comp_node : id_node->components.values()) {
161 comp_node->affects_directly_visible |= id_node->is_directly_visible;
162 }
163 }
164 for (OperationNode *op_node : graph->operations) {
165 op_node->custom_flags = 0;
166 op_node->num_links_pending = 0;
167 for (Relation *rel : op_node->outlinks) {
168 if ((rel->from->type == NodeType::OPERATION) && (rel->flag & RELATION_FLAG_CYCLIC) == 0) {
169 ++op_node->num_links_pending;
170 }
171 }
172 if (op_node->num_links_pending == 0) {
173 BLI_stack_push(stack, &op_node);
174 op_node->custom_flags |= DEG_NODE_VISITED;
175 }
176 }
177 while (!BLI_stack_is_empty(stack)) {
178 OperationNode *op_node;
179 BLI_stack_pop(stack, &op_node);
180 /* Flush layers to parents. */
181 for (Relation *rel : op_node->inlinks) {
182 if (rel->from->type == NodeType::OPERATION) {
183 OperationNode *op_from = (OperationNode *)rel->from;
184 op_from->owner->affects_directly_visible |= op_node->owner->affects_directly_visible;
185 }
186 }
187 /* Schedule parent nodes. */
188 for (Relation *rel : op_node->inlinks) {
189 if (rel->from->type == NodeType::OPERATION) {
190 OperationNode *op_from = (OperationNode *)rel->from;
191 if ((rel->flag & RELATION_FLAG_CYCLIC) == 0) {
192 BLI_assert(op_from->num_links_pending > 0);
193 --op_from->num_links_pending;
194 }
195 if ((op_from->num_links_pending == 0) && (op_from->custom_flags & DEG_NODE_VISITED) == 0) {
196 BLI_stack_push(stack, &op_from);
197 op_from->custom_flags |= DEG_NODE_VISITED;
198 }
199 }
200 }
201 }
202 BLI_stack_free(stack);
203 }
204
205 } // namespace
206
deg_graph_build_finalize(Main * bmain,Depsgraph * graph)207 void deg_graph_build_finalize(Main *bmain, Depsgraph *graph)
208 {
209 /* Make sure dependencies of visible ID datablocks are visible. */
210 deg_graph_build_flush_visibility(graph);
211 deg_graph_remove_unused_noops(graph);
212
213 /* Re-tag IDs for update if it was tagged before the relations
214 * update tag. */
215 for (IDNode *id_node : graph->id_nodes) {
216 ID *id_orig = id_node->id_orig;
217 id_node->finalize_build(graph);
218 int flag = 0;
219 /* Tag rebuild if special evaluation flags changed. */
220 if (id_node->eval_flags != id_node->previous_eval_flags) {
221 flag |= ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY;
222 }
223 /* Tag rebuild if the custom data mask changed. */
224 if (id_node->customdata_masks != id_node->previous_customdata_masks) {
225 flag |= ID_RECALC_GEOMETRY;
226 }
227 if (!deg_copy_on_write_is_expanded(id_node->id_cow)) {
228 flag |= ID_RECALC_COPY_ON_WRITE;
229 /* This means ID is being added to the dependency graph first
230 * time, which is similar to "ob-visible-change" */
231 if (GS(id_orig->name) == ID_OB) {
232 flag |= ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY;
233 }
234 }
235 /* Restore recalc flags from original ID, which could possibly contain recalc flags set by
236 * an operator and then were carried on by the undo system. */
237 flag |= id_orig->recalc;
238 if (flag != 0) {
239 graph_id_tag_update(bmain, graph, id_node->id_orig, flag, DEG_UPDATE_SOURCE_RELATIONS);
240 }
241 }
242 }
243
244 } // namespace deg
245 } // namespace blender
246