Blender V5.3
attribute_storage_access.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_attribute.hh"
11#include "BKE_deform.hh"
12
13#include "BLI_listbase.hh"
14
15#include "DNA_object_types.h"
16
18
19namespace blender::bke {
20
22 const AttrDomain domain,
23 const int64_t domain_size)
24{
25 const CPPType &cpp_type = attribute_type_to_cpp_type(attribute.data_type());
26 switch (attribute.storage_type()) {
28 const auto &data = std::get<Attribute::ArrayData>(attribute.data());
29 return GAttributeReader{GVArray::from_span(GSpan(cpp_type, data.data, data.size)),
30 domain,
31 data.sharing_info.get()};
32 }
34 const auto &data = std::get<Attribute::SingleData>(attribute.data());
35 return GAttributeReader{GVArray::from_single_ref(cpp_type, domain_size, data.value),
36 domain,
37 data.sharing_info.get()};
38 }
39 }
41 return {};
42}
43
45 const Map<StringRef, AttrUpdateOnChange> &changed_tags,
46 const int64_t domain_size,
48{
49 const CPPType &cpp_type = attribute_type_to_cpp_type(attribute.data_type());
50 switch (attribute.storage_type()) {
52 auto &data = std::get<Attribute::ArrayData>(attribute.data_for_write());
53 BLI_assert(data.size == domain_size);
54
55 std::function<void()> tag_modified_fn;
56 if (const AttrUpdateOnChange update_fn = changed_tags.lookup_default(attribute.name(),
57 nullptr))
58 {
59 tag_modified_fn = [owner, update_fn]() { update_fn(owner); };
60 };
61
62 return GAttributeWriter{
63 GVMutableArray::from_span(GMutableSpan(cpp_type, data.data, domain_size)),
64 attribute.domain(),
65 std::move(tag_modified_fn)};
66 }
68 /* Just convert the stored type to an array for modification. It might not make sense to
69 * implement editing of single values at this level. */
70 const auto &data = std::get<Attribute::SingleData>(attribute.data());
71 const GPointer value(cpp_type, data.value);
72 attribute.assign_data(Attribute::ArrayData::from_value(value, domain_size));
73 return attribute_to_writer(owner, changed_tags, domain_size, attribute);
74 }
75 }
77 return {};
78}
79
81 const int64_t domain_size,
82 const AttributeInit &initializer,
83 const bool require_array_data)
84{
85 switch (initializer.type) {
87 const CPPType &type = bke::attribute_type_to_cpp_type(data_type);
88 return Attribute::ArrayData::from_constructed(type, domain_size);
89 }
91 const auto &init = static_cast<const AttributeInitValue &>(initializer);
92 BLI_assert(*init.value.type() == bke::attribute_type_to_cpp_type(data_type));
93 if (require_array_data) {
94 return Attribute::ArrayData::from_value(init.value, domain_size);
95 }
97 }
99 const CPPType &type = bke::attribute_type_to_cpp_type(data_type);
100 return Attribute::ArrayData::from_default_value(type, domain_size);
101 }
103 const auto &init = static_cast<const AttributeInitVArray &>(initializer);
104 const GVArray &varray = init.varray;
105 BLI_assert(varray.size() == domain_size);
106 if (!require_array_data) {
107 const CommonVArrayInfo &info = varray.common_info();
109 return Attribute::SingleData::from_value(GPointer(varray.type(), info.data));
110 }
111 }
112 const CPPType &type = varray.type();
114 varray.materialize_to_uninitialized(varray.index_range(), data.data);
115 return data;
116 }
118 const auto &init = static_cast<const AttributeInitShared &>(initializer);
120 data.data = const_cast<void *>(init.data);
121 data.size = domain_size;
122 data.sharing_info = ImplicitSharingPtr<>(init.sharing_info);
123 data.sharing_info->add_user();
124 return data;
125 }
126 }
128 return {};
129}
130
132 AttrDomain domain,
133 const CPPType &cpp_type,
134 StringRef name,
135 int64_t domain_size,
136 const void *default_value)
137{
138 const bke::Attribute *attr = storage.wrap().lookup(name);
139
140 const auto return_default = [&]() {
141 return GVArray::from_single(cpp_type, domain_size, default_value);
142 };
143
144 if (!attr) {
145 return return_default();
146 }
147 if (attr->domain() != domain) {
148 return return_default();
149 }
150 if (attr->data_type() != cpp_type_to_attribute_type(cpp_type)) {
151 return return_default();
152 }
153 switch (attr->storage_type()) {
155 const auto &data = std::get<bke::Attribute::ArrayData>(attr->data());
156 const GSpan span(cpp_type, data.data, data.size);
157 return GVArray::from_span(span);
158 }
160 const auto &data = std::get<bke::Attribute::SingleData>(attr->data());
161 return GVArray::from_single(cpp_type, domain_size, data.value);
162 }
163 }
164 return return_default();
165}
166
167std::optional<GSpan> get_span_attribute(const AttributeStorage &storage,
168 const AttrDomain domain,
169 const CPPType &cpp_type,
170 const StringRef name,
171 const int64_t domain_size)
172{
173 const bke::Attribute *attr = storage.wrap().lookup(name);
174 if (!attr) {
175 return {};
176 }
177 if (attr->domain() != domain) {
178 return {};
179 }
180 if (const auto *array_data = std::get_if<bke::Attribute::ArrayData>(&attr->data())) {
181 BLI_assert(array_data->size == domain_size);
182 UNUSED_VARS_NDEBUG(domain_size);
183 return GSpan(cpp_type, array_data->data, array_data->size);
184 }
185 return {};
186}
187
189 const AttrDomain domain,
190 const CPPType &cpp_type,
191 const StringRef name,
192 const int64_t domain_size,
193 const void *custom_default_value)
194{
195 if (domain_size <= 0) {
196 return {};
197 }
198 const bke::AttrType type = bke::cpp_type_to_attribute_type(cpp_type);
199 if (bke::Attribute *attr = storage.wrap().lookup(name)) {
200 if (attr->data_type() == type) {
201 if (const auto *single_data = std::get_if<bke::Attribute::SingleData>(&attr->data())) {
202 /* Convert single value storage to array storage. */
203 const GPointer g_value(cpp_type, single_data->value);
204 attr->assign_data(bke::Attribute::ArrayData::from_value(g_value, domain_size));
205 }
206 auto &array_data = std::get<bke::Attribute::ArrayData>(attr->data_for_write());
207 return GMutableSpan(cpp_type, array_data.data, domain_size);
208 }
209 /* The attribute has the wrong type. This shouldn't happen for builtin attributes, but just
210 * in case, remove it. */
211 storage.wrap().remove(name);
212 }
213 const void *default_value = custom_default_value ? custom_default_value :
214 cpp_type.default_value();
215 bke::Attribute &attr = storage.wrap().add(
216 name,
217 domain,
218 type,
219 bke::Attribute::ArrayData::from_value({cpp_type, default_value}, domain_size));
220 auto &array_data = std::get<bke::Attribute::ArrayData>(attr.data_for_write());
221 BLI_assert(array_data.size == domain_size);
222 return GMutableSpan(cpp_type, array_data.data, domain_size);
223}
224
225bool try_delete_vertex_group(ListBaseT<bDeformGroup> &vertex_groups,
226 const StringRef name,
227 FunctionRef<MutableSpan<MDeformVert>()> get_mutable_dverts)
228{
229 int index;
230 bDeformGroup *group;
231 if (!BKE_defgroup_listbase_name_find(&vertex_groups, name, &index, &group)) {
232 return false;
233 }
234 BLI_remlink(&vertex_groups, group);
235 MEM_delete(group);
236 MutableSpan<MDeformVert> dverts = get_mutable_dverts();
237 if (dverts.is_empty()) {
238 return true;
239 }
241 return true;
242}
243
244static bDeformGroup *find_vertex_group(ListBaseT<bDeformGroup> &vertex_groups,
245 const StringRef name)
246{
247 for (bDeformGroup &group : vertex_groups) {
248 if (group.name == name) {
249 return &group;
250 }
251 }
252 return nullptr;
253}
254
256 const FunctionRef<int(AttrDomain)> domain_size_fn)
257{
258 switch (attr.storage_type()) {
260 const auto &data = std::get<Attribute::SingleData>(attr.data());
261 const GPointer value(attribute_type_to_cpp_type(attr.data_type()), data.value);
262 attr.assign_data(Attribute::ArrayData::from_value(value, domain_size_fn(attr.domain())));
263 break;
264 }
266 break;
267 }
268}
269
270static std::variant<std::monostate, Attribute *, bDeformGroup *> find_attr_or_group(
272 std::optional<ListBaseT<bDeformGroup> *> vertex_groups,
273 const StringRef name)
274{
275 if (Attribute *attr = storage.lookup(name)) {
276 return attr;
277 }
278 if (vertex_groups) {
279 if (bDeformGroup *group = find_vertex_group(**vertex_groups, name)) {
280 return group;
281 }
282 }
283 return std::monostate();
284}
285
287 const Map<StringRef, StringRef> &name_map,
288 const bool overwrite,
289 const Map<StringRef, AttrBuiltinInfo> &builtin_attributes,
290 const Set<StringRef> &array_storage_required,
291 const FunctionRef<int(AttrDomain)> domain_size_fn,
292 std::optional<ListBaseT<bDeformGroup> *> vertex_groups,
293 FunctionRef<MutableSpan<MDeformVert>()> get_mutable_dverts)
294{
295 Set<StringRef> names_to_remove;
296 Set<StringRef> failed;
298 Map<bDeformGroup *, StringRef> vertex_group_map;
299 map.reserve(name_map.size());
300 for (const auto &[old_name, new_name] : name_map.items()) {
301 if (new_name.is_empty()) {
302 failed.add_new(old_name);
303 continue;
304 }
305 const std::variant<std::monostate, Attribute *, bDeformGroup *> old_attr = find_attr_or_group(
306 storage, vertex_groups, old_name);
307 if (std::holds_alternative<std::monostate>(old_attr)) {
308 failed.add_new(old_name);
309 continue;
310 }
311 const bke::AttrDomain old_domain = std::holds_alternative<Attribute *>(old_attr) ?
312 std::get<Attribute *>(old_attr)->domain() :
314 const bke::AttrType old_type = std::holds_alternative<Attribute *>(old_attr) ?
315 std::get<Attribute *>(old_attr)->data_type() :
317
318 if (const AttrBuiltinInfo *old_info = builtin_attributes.lookup_ptr(old_name)) {
319 if (!old_info->deletable) {
320 failed.add_new(old_name);
321 continue;
322 }
323 }
324 if (const AttrBuiltinInfo *new_info = builtin_attributes.lookup_ptr(new_name)) {
325 if (new_info->domain != old_domain || new_info->type != old_type) {
326 failed.add_new(old_name);
327 continue;
328 }
329 }
330 if (array_storage_required.contains(new_name)) {
331 if (std::holds_alternative<Attribute *>(old_attr)) {
332 ensure_array_storage(*std::get<Attribute *>(old_attr), domain_size_fn);
333 }
334 else {
335 /* Converting vertex groups to real attributes remains unsupported here. */
336 failed.add_new(old_name);
337 continue;
338 }
339 }
340 if (overwrite) {
341 names_to_remove.add_new(new_name);
342 }
343 else {
344 const std::variant<std::monostate, Attribute *, bDeformGroup *> new_attr =
345 find_attr_or_group(storage, vertex_groups, new_name);
346 if (!std::holds_alternative<std::monostate>(new_attr)) {
347 failed.add_new(old_name);
348 continue;
349 }
350 }
351
352 if (std::holds_alternative<Attribute *>(old_attr)) {
353 map.add_new(std::get<Attribute *>(old_attr), new_name);
354 }
355 else {
356 vertex_group_map.add_new(std::get<bDeformGroup *>(old_attr), new_name);
357 }
358 }
359
360 if (!names_to_remove.is_empty()) {
361 const int start_attrs_num = storage.count();
362 storage.remove(names_to_remove);
363 if (vertex_groups && (start_attrs_num - storage.count()) != names_to_remove.size()) {
364 /* Also remove vertex groups if names weren't all removed as attributes. */
365 for (bDeformGroup &defgroup : (*vertex_groups)->items_mutable()) {
366 if (names_to_remove.contains_as(defgroup.name)) {
367 try_delete_vertex_group(**vertex_groups, defgroup.name, get_mutable_dverts);
368 }
369 }
370 }
371 }
372
373 if (!map.is_empty()) {
374 storage.rename(map);
375 }
376 if (vertex_groups && !vertex_group_map.is_empty()) {
377 for (bDeformGroup &defgroup : **vertex_groups) {
378 if (const std::optional<StringRef> name = vertex_group_map.lookup_try(&defgroup)) {
379 name->copy_utf8_truncated(defgroup.name);
380 }
381 }
382 }
383 return failed;
384}
385
386} // namespace blender::bke
support for deformation groups and hooks.
Object is a sort of wrapper for general info.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
const void * default_value() const
GPointer()=default
GSpan()=default
CommonVArrayInfo common_info() const
void materialize_to_uninitialized(void *dst) const
static GVArray from_single(const CPPType &type, int64_t size, const void *value)
static GVArray from_span(GSpan span)
static GVArray from_single_ref(const CPPType &type, int64_t size, const void *value)
static GVMutableArray from_span(GMutableSpan span)
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
std::optional< Value > lookup_try(const Key &key) const
Definition BLI_map.hh:531
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
int64_t size() const
Definition BLI_map.hh:976
bool is_empty() const
Definition BLI_map.hh:986
void reserve(int64_t n)
Definition BLI_map.hh:1028
ItemIterator items() const &
Definition BLI_map.hh:902
constexpr bool is_empty() const
Definition BLI_span.hh:515
int64_t size() const
Definition BLI_set.hh:606
bool contains_as(const ForwardKey &key) const
Definition BLI_set.hh:314
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool is_empty() const
Definition BLI_set.hh:614
void add_new(const Key &key)
Definition BLI_set.hh:233
AttrStorageType storage_type() const
const DataVariant & data() const
std::variant< ArrayData, SingleData > DataVariant
void assign_data(DataVariant &&data)
void copy_utf8_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:28
#define storage(slot, qualifiers)
#define attribute(slot)
GMutableSpan get_mutable_attribute(AttributeStorage &storage, const AttrDomain domain, const CPPType &cpp_type, const StringRef name, const int64_t domain_size, const void *custom_default_value)
GAttributeReader attribute_to_reader(const Attribute &attribute, const AttrDomain domain, const int64_t domain_size)
std::optional< GSpan > get_span_attribute(const AttributeStorage &storage, const AttrDomain domain, const CPPType &cpp_type, const StringRef name, const int64_t domain_size)
Set< StringRef > rename_attributes(AttributeStorage &storage, const Map< StringRef, StringRef > &name_map, const bool overwrite, const Map< StringRef, AttrBuiltinInfo > &builtin_attributes, const Set< StringRef > &array_storage_required, const FunctionRef< int(AttrDomain)> domain_size_fn, std::optional< ListBaseT< bDeformGroup > * > vertex_groups, FunctionRef< MutableSpan< MDeformVert >()> get_mutable_dverts)
void remove_defgroup_index(MutableSpan< MDeformVert > dverts, int defgroup_index)
Definition deform.cc:1758
GAttributeWriter attribute_to_writer(void *owner, const Map< StringRef, AttrUpdateOnChange > &changed_tags, const int64_t domain_size, Attribute &attribute)
static std::variant< std::monostate, Attribute *, bDeformGroup * > find_attr_or_group(AttributeStorage &storage, std::optional< ListBaseT< bDeformGroup > * > vertex_groups, const StringRef name)
bool try_delete_vertex_group(ListBaseT< bDeformGroup > &vertex_groups, const StringRef name, FunctionRef< MutableSpan< MDeformVert >()> get_mutable_dverts)
GVArray get_varray_attribute(const AttributeStorage &storage, AttrDomain domain, const CPPType &cpp_type, StringRef name, int64_t domain_size, const void *default_value)
void(*)(void *owner) AttrUpdateOnChange
Attribute::DataVariant attribute_init_to_data(const bke::AttrType data_type, const int64_t domain_size, const AttributeInit &initializer, const bool require_array_data)
static void ensure_array_storage(Attribute &attr, const FunctionRef< int(AttrDomain)> domain_size_fn)
static bDeformGroup * find_vertex_group(ListBaseT< bDeformGroup > &vertex_groups, const StringRef name)
AttrType cpp_type_to_attribute_type(const CPPType &type)
const CPPType & attribute_type_to_cpp_type(AttrType type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
bool BKE_defgroup_listbase_name_find(ListBaseT< bDeformGroup > *defbase, StringRef name, int *r_index, bDeformGroup **r_group)
Definition deform.cc:562
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
BMesh const char void * data
static ArrayData from_uninitialized(const CPPType &type, int64_t domain_size)
static ArrayData from_default_value(const CPPType &type, int64_t domain_size)
static ArrayData from_value(const GPointer &value, int64_t domain_size)
static ArrayData from_constructed(const CPPType &type, int64_t domain_size)
static SingleData from_value(const GPointer &value)