Refactor: Attributes: Add assign_data function
Prevent an extra copy that could happen when calling data_for_write() to retrieve mutable access, only to just replace the data. Pull Request: https://projects.blender.org/blender/blender/pulls/140946
This commit is contained in:
@@ -108,6 +108,9 @@ class Attribute {
|
||||
* \warning Does not yet support attributes stored as a single value (#AttrStorageType::Single).
|
||||
*/
|
||||
DataVariant &data_for_write();
|
||||
|
||||
/** Replace the attribute's data without first making the existing data mutable. */
|
||||
void assign_data(DataVariant &&data);
|
||||
};
|
||||
|
||||
class AttributeStorageRuntime {
|
||||
@@ -228,6 +231,11 @@ inline const Attribute::DataVariant &Attribute::data() const
|
||||
return data_;
|
||||
}
|
||||
|
||||
inline void Attribute::assign_data(DataVariant &&data)
|
||||
{
|
||||
data_ = std::move(data);
|
||||
}
|
||||
|
||||
} // namespace blender::bke
|
||||
|
||||
inline blender::bke::AttributeStorage &AttributeStorage::wrap()
|
||||
|
||||
@@ -232,7 +232,7 @@ static MutableSpan<T> get_mutable_attribute(PointCloud &pointcloud,
|
||||
if (const auto *single_data = std::get_if<bke::Attribute::SingleData>(&attr->data())) {
|
||||
/* Convert single value storage to array storage. */
|
||||
const GPointer g_value(CPPType::get<T>(), single_data->value);
|
||||
attr->data_for_write() = bke::Attribute::ArrayData::ForValue(g_value, pointcloud.totpoint);
|
||||
attr->assign_data(bke::Attribute::ArrayData::ForValue(g_value, pointcloud.totpoint));
|
||||
}
|
||||
auto &array_data = std::get<bke::Attribute::ArrayData>(attr->data_for_write());
|
||||
BLI_assert(array_data.size == pointcloud.totpoint);
|
||||
|
||||
@@ -98,7 +98,7 @@ static void reorder_attribute_domain(bke::AttributeStorage &data,
|
||||
bke::attribute_math::gather(GSpan(type, data.data, data.size),
|
||||
new_by_old_map,
|
||||
GMutableSpan(type, new_data.data, new_data.size));
|
||||
attr.data_for_write() = std::move(new_data);
|
||||
attr.assign_data(std::move(new_data));
|
||||
}
|
||||
case bke::AttrStorageType::Single: {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user