Blender V5.3
deg_builder_relations_drivers.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
12
13#include <cstring>
14#include <deque>
15
16#include "BLI_listbase.hh"
17
18#include "DNA_anim_types.h"
19
20#include "RNA_access.hh"
21#include "RNA_path.hh"
22
23#include "BKE_anim_data.hh"
24
28
29namespace blender::deg {
30
32 : id_ptr_(id_ptr),
33 fcu_(fcu),
34 driver_relations_needed_(false),
35 pointer_rna_(),
36 property_rna_(nullptr),
37 is_array_(false)
38{
39 driver_relations_needed_ = determine_relations_needed();
40 split_rna_path();
41}
42
43bool DriverDescriptor::determine_relations_needed()
44{
45 if (fcu_->array_index > 0) {
46 /* Drivers on array elements always need relations. */
47 is_array_ = true;
48 return true;
49 }
50
51 if (!resolve_rna()) {
52 /* Properties that don't exist can't cause threading issues either. */
53 return false;
54 }
55
56 if (RNA_property_array_check(property_rna_)) {
57 /* Drivers on array elements always need relations. */
58 is_array_ = true;
59 return true;
60 }
61
62 /* Drivers on Booleans and Enums (when used as bit-flags) can write to the same memory location,
63 * so they need relations between each other. */
64 return ELEM(RNA_property_type(property_rna_), PROP_BOOLEAN, PROP_ENUM);
65}
66
68{
69 return driver_relations_needed_;
70}
71
73{
74 return is_array_;
75}
76
78{
79 if (!is_array_ || !other.is_array_) {
80 return false;
81 }
82 return rna_suffix == other.rna_suffix;
83}
84
86{
87 return OperationKey(id_ptr_->owner_id,
90 fcu_->rna_path().c_str(),
91 fcu_->array_index);
92}
93
94void DriverDescriptor::split_rna_path()
95{
96 const StringRefNull rna_path = fcu_->rna_path();
97 const char *last_dot = strrchr(rna_path.c_str(), '.');
98 if (last_dot == nullptr || last_dot[1] == '\0') {
101 return;
102 }
103
104 rna_prefix = StringRef(rna_path.c_str(), last_dot);
105 rna_suffix = StringRef(last_dot + 1);
106}
107
108bool DriverDescriptor::resolve_rna()
109{
111 id_ptr_, fcu_->rna_path().c_str(), &pointer_rna_, &property_rna_);
112}
113
114static bool is_reachable(const Node *const from, const Node *const to)
115{
116 if (from == to) {
117 return true;
118 }
119
120 /* Perform a graph walk from 'to' towards its incoming connections.
121 * Walking from 'from' towards its outgoing connections is 10x slower on the Spring rig. */
122 std::deque<const Node *> queue;
124 queue.push_back(to);
125 while (!queue.empty()) {
126 /* Visit the next node to inspect. */
127 const Node *visit = queue.back();
128 queue.pop_back();
129
130 if (visit == from) {
131 return true;
132 }
133
134 /* Queue all incoming relations that we haven't seen before. */
135 for (Relation *relation : visit->inlinks) {
136 const Node *prev_node = relation->from;
137 if (seen.add(prev_node)) {
138 queue.push_back(prev_node);
139 }
140 }
141 }
142 return false;
143}
144
145/* **** DepsgraphRelationBuilder functions **** */
146
148{
149 for (IDNode *id_node : graph_->id_nodes) {
150 build_driver_relations(id_node);
151 }
152}
153
155{
156 /* Add relations between drivers that write to the same datablock.
157 *
158 * This prevents threading issues when two separate RNA properties write to
159 * the same memory address. For example:
160 * - Drivers on individual array elements, as the animation system will write
161 * the whole array back to RNA even when changing individual array value.
162 * - Drivers on RNA properties that map to a single bit flag. Changing the RNA
163 * value will write the entire int containing the bit, in a non-thread-safe
164 * way.
165 */
166 ID *id_orig = id_node->id_orig;
167 AnimData *adt = BKE_animdata_from_id(id_orig);
168 if (adt == nullptr) {
169 return;
170 }
171
172 /* Mapping from RNA prefix -> set of driver descriptors: */
174
175 PointerRNA id_ptr = RNA_id_pointer_create(id_orig);
176
177 for (FCurve &fcu : adt->drivers) {
178 if (fcu.rna_path().is_empty()) {
179 continue;
180 }
181
182 DriverDescriptor driver_desc(&id_ptr, &fcu);
183 if (!driver_desc.driver_relations_needed()) {
184 continue;
185 }
186
187 driver_groups.lookup_or_add_default_as(driver_desc.rna_prefix).append(driver_desc);
188 }
189
190 for (Span<DriverDescriptor> prefix_group : driver_groups.values()) {
191 /* For each node in the driver group, try to connect it to another node
192 * in the same group without creating any cycles. */
193 int num_drivers = prefix_group.size();
194 if (num_drivers < 2) {
195 /* A relation requires two drivers. */
196 continue;
197 }
198 for (int from_index = 0; from_index < num_drivers; ++from_index) {
199 const DriverDescriptor &driver_from = prefix_group[from_index];
200 Node *op_from = get_node(driver_from.depsgraph_key());
201
202 /* Start by trying the next node in the group. */
203 for (int to_offset = 1; to_offset < num_drivers; ++to_offset) {
204 const int to_index = (from_index + to_offset) % num_drivers;
205 const DriverDescriptor &driver_to = prefix_group[to_index];
206 Node *op_to = get_node(driver_to.depsgraph_key());
207
208 /* Duplicate drivers can exist (see #78615), but cannot be distinguished by OperationKey
209 * and thus have the same depsgraph node. Relations between those drivers should not be
210 * created. This not something that is expected to happen (both the UI and the Python API
211 * prevent duplicate drivers), it did happen in a file and it is easy to deal with here. */
212 if (op_from == op_to) {
213 continue;
214 }
215
216 if (from_index < to_index && driver_from.is_same_array_as(driver_to)) {
217 /* This is for adding a relation like `color[0]` -> `color[1]`.
218 * When the search for another driver wraps around,
219 * we cannot blindly add relations any more. */
220 }
221 else {
222 /* Investigate whether this relation would create a dependency cycle.
223 * Example graph:
224 * A -> B -> C
225 * and investigating a potential connection C->A. Because A->C is an
226 * existing transitive connection, adding C->A would create a cycle. */
227 if (is_reachable(op_to, op_from)) {
228 continue;
229 }
230
231 /* No need to directly connect this node if there is already a transitive connection. */
232 if (is_reachable(op_from, op_to)) {
233 break;
234 }
235 }
236
238 op_from->get_exit_operation(), op_to->get_entry_operation(), "Driver Serialization");
239 break;
240 }
241 }
242 }
243}
244
245bool data_path_maybe_shared(const ID &id, const StringRef data_path)
246{
247 /* As it is hard to generally detect implicit sharing, this is implemented as
248 * a 'known to not share' list. */
249
250 /* Allow concurrent writes to custom properties. #140706 shows that this
251 * shouldn't be a problem in practice. */
252 if (data_path.startswith("[\"") && data_path.endswith("\"]")) {
253 return false;
254 }
255
256 if (GS(id.name) == ID_OB) {
257 const Object &ob = *reinterpret_cast<const Object *>(&id);
258 const bool is_thread_safe = (ob.type == OB_ARMATURE && data_path.startswith("pose.bones["));
259 return !is_thread_safe;
260 }
261
262 /* Allow concurrent writes to shape-key values. #140706 shows that this
263 * shouldn't be a problem in practice. */
264 if (GS(id.name) == ID_KE) {
265 const bool is_thread_safe = data_path.startswith("key_blocks[") &&
266 data_path.endswith("].value");
267 return !is_thread_safe;
268 }
269
270 return true;
271}
272
273} // namespace blender::deg
#define ELEM(...)
ValueIterator values() const &
Definition BLI_map.hh:884
Value & lookup_or_add_default_as(ForwardKey &&key)
Definition BLI_map.hh:647
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr StringRef()
constexpr bool startswith(StringRef prefix) const
constexpr bool endswith(StringRef suffix) const
TimeSourceNode * get_node(const TimeSourceKey &key) const
Relation * add_operation_relation(OperationNode *node_from, OperationNode *node_to, const char *description, int flags=0)
DriverDescriptor(PointerRNA *id_ptr, FCurve *fcu)
bool is_same_array_as(const DriverDescriptor &other) const
static bool is_reachable(const Node *const from, const Node *const to)
bool data_path_maybe_shared(const ID &id, const StringRef data_path)
PointerRNA RNA_id_pointer_create(ID *id)
bool RNA_property_array_check(PropertyRNA *prop)
PropertyType RNA_property_type(PropertyRNA *prop)
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
@ PROP_BOOLEAN
Definition RNA_types.hh:210
ID_Type GS(const char *name)
Definition DNA_ID.h:410
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:578
ListBaseT< FCurve > drivers
virtual OperationNode * get_entry_operation()
Definition deg_node.hh:209
Relations inlinks
Definition deg_node.hh:188
virtual OperationNode * get_exit_operation()
Definition deg_node.hh:213