[Bf-blender-cvs] [1d92a4d1a04] temp-geometry-nodes-fields-prototype: initial anonymous attributes implementation
Jacques Lucke
noreply at git.blender.org
Thu Jul 29 21:41:13 CEST 2021
Commit: 1d92a4d1a045ea195410e02823b6dcccc0ae0525
Author: Jacques Lucke
Date: Thu Jul 29 19:16:09 2021 +0200
Branches: temp-geometry-nodes-fields-prototype
https://developer.blender.org/rB1d92a4d1a045ea195410e02823b6dcccc0ae0525
initial anonymous attributes implementation
===================================================================
M source/blender/blenkernel/BKE_customdata.h
M source/blender/blenkernel/BKE_geometry_set.hh
M source/blender/blenkernel/intern/attribute_access.cc
M source/blender/blenkernel/intern/attribute_access_intern.hh
M source/blender/blenkernel/intern/customdata.c
M source/blender/makesdna/DNA_customdata_types.h
===================================================================
diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h
index 7a44553c565..7fc7d8bddb1 100644
--- a/source/blender/blenkernel/BKE_customdata.h
+++ b/source/blender/blenkernel/BKE_customdata.h
@@ -482,6 +482,14 @@ void CustomData_external_reload(struct CustomData *data,
CustomDataMask mask,
int totelem);
+/* Anonymous layers. */
+struct AnonymousCustomDataLayerID *CustomData_anonymous_id_new(const char *debug_name);
+void CustomData_anonymous_id_strong_decrement(struct AnonymousCustomDataLayerID *layer_id);
+void CustomData_anonymous_id_strong_increment(struct AnonymousCustomDataLayerID *layer_id);
+void CustomData_anonymous_id_weak_decrement(struct AnonymousCustomDataLayerID *layer_id);
+void CustomData_anonymous_id_weak_increment(struct AnonymousCustomDataLayerID *layer_id);
+bool CustomData_layer_is_unused_anonymous(struct CustomDataLayer *layer);
+
/* Mesh-to-mesh transfer data. */
struct CustomDataTransferLayerMap;
diff --git a/source/blender/blenkernel/BKE_geometry_set.hh b/source/blender/blenkernel/BKE_geometry_set.hh
index 42e9ce82278..56cdffc9311 100644
--- a/source/blender/blenkernel/BKE_geometry_set.hh
+++ b/source/blender/blenkernel/BKE_geometry_set.hh
@@ -128,6 +128,17 @@ class GeometryComponent {
const CustomDataType data_type,
const AttributeInit &initializer);
+ AnonymousCustomDataLayerID *attribute_try_create_anonymous(const blender::StringRef debug_name,
+ const AttributeDomain domain,
+ const CustomDataType data_type,
+ const AttributeInit &initializer);
+
+ blender::bke::ReadAttributeLookup attribute_try_get_anonymous_for_read(
+ const AnonymousCustomDataLayerID &layer_id) const;
+
+ blender::bke::WriteAttributeLookup attribute_try_get_anonymous_for_write(
+ const AnonymousCustomDataLayerID &layer_id);
+
/* Try to create the builtin attribute with the given name. No data type or domain has to be
* provided, because those are fixed for builtin attributes. */
bool attribute_try_create_builtin(const blender::StringRef attribute_name,
diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index aa0af294bc3..442be01a0dc 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -346,27 +346,33 @@ ReadAttributeLookup CustomDataAttributeProvider::try_get_for_read(
if (layer.name != attribute_name) {
continue;
}
- const CustomDataType data_type = (CustomDataType)layer.type;
- switch (data_type) {
- case CD_PROP_FLOAT:
- return this->layer_to_read_attribute<float>(layer, domain_size);
- case CD_PROP_FLOAT2:
- return this->layer_to_read_attribute<float2>(layer, domain_size);
- case CD_PROP_FLOAT3:
- return this->layer_to_read_attribute<float3>(layer, domain_size);
- case CD_PROP_INT32:
- return this->layer_to_read_attribute<int>(layer, domain_size);
- case CD_PROP_COLOR:
- return this->layer_to_read_attribute<ColorGeometry4f>(layer, domain_size);
- case CD_PROP_BOOL:
- return this->layer_to_read_attribute<bool>(layer, domain_size);
- default:
- break;
- }
+ return this->layer_to_read_attribute(layer, domain_size);
}
return {};
}
+ReadAttributeLookup CustomDataAttributeProvider::layer_to_read_attribute(
+ const CustomDataLayer &layer, const int domain_size) const
+{
+ const CustomDataType data_type = (CustomDataType)layer.type;
+ switch (data_type) {
+ case CD_PROP_FLOAT:
+ return this->layer_to_read_attribute<float>(layer, domain_size);
+ case CD_PROP_FLOAT2:
+ return this->layer_to_read_attribute<float2>(layer, domain_size);
+ case CD_PROP_FLOAT3:
+ return this->layer_to_read_attribute<float3>(layer, domain_size);
+ case CD_PROP_INT32:
+ return this->layer_to_read_attribute<int>(layer, domain_size);
+ case CD_PROP_COLOR:
+ return this->layer_to_read_attribute<ColorGeometry4f>(layer, domain_size);
+ case CD_PROP_BOOL:
+ return this->layer_to_read_attribute<bool>(layer, domain_size);
+ default:
+ return {};
+ }
+}
+
WriteAttributeLookup CustomDataAttributeProvider::try_get_for_write(
GeometryComponent &component, const StringRef attribute_name) const
{
@@ -380,27 +386,33 @@ WriteAttributeLookup CustomDataAttributeProvider::try_get_for_write(
continue;
}
CustomData_duplicate_referenced_layer_named(custom_data, layer.type, layer.name, domain_size);
- const CustomDataType data_type = (CustomDataType)layer.type;
- switch (data_type) {
- case CD_PROP_FLOAT:
- return this->layer_to_write_attribute<float>(layer, domain_size);
- case CD_PROP_FLOAT2:
- return this->layer_to_write_attribute<float2>(layer, domain_size);
- case CD_PROP_FLOAT3:
- return this->layer_to_write_attribute<float3>(layer, domain_size);
- case CD_PROP_INT32:
- return this->layer_to_write_attribute<int>(layer, domain_size);
- case CD_PROP_COLOR:
- return this->layer_to_write_attribute<ColorGeometry4f>(layer, domain_size);
- case CD_PROP_BOOL:
- return this->layer_to_write_attribute<bool>(layer, domain_size);
- default:
- break;
- }
+ this->layer_to_write_attribute(layer, domain_size);
}
return {};
}
+WriteAttributeLookup CustomDataAttributeProvider::layer_to_write_attribute(
+ CustomDataLayer &layer, const int domain_size) const
+{
+ const CustomDataType data_type = (CustomDataType)layer.type;
+ switch (data_type) {
+ case CD_PROP_FLOAT:
+ return this->layer_to_write_attribute<float>(layer, domain_size);
+ case CD_PROP_FLOAT2:
+ return this->layer_to_write_attribute<float2>(layer, domain_size);
+ case CD_PROP_FLOAT3:
+ return this->layer_to_write_attribute<float3>(layer, domain_size);
+ case CD_PROP_INT32:
+ return this->layer_to_write_attribute<int>(layer, domain_size);
+ case CD_PROP_COLOR:
+ return this->layer_to_write_attribute<ColorGeometry4f>(layer, domain_size);
+ case CD_PROP_BOOL:
+ return this->layer_to_write_attribute<bool>(layer, domain_size);
+ default:
+ return {};
+ }
+}
+
bool CustomDataAttributeProvider::try_delete(GeometryComponent &component,
const StringRef attribute_name) const
{
@@ -487,6 +499,80 @@ bool CustomDataAttributeProvider::try_create(GeometryComponent &component,
return true;
}
+static std::string get_anonymous_attribute_name()
+{
+ static std::atomic<int> index = 0;
+ const int next_index = index.fetch_add(1);
+ return "anonymous_attribute_" + std::to_string(next_index);
+}
+
+AnonymousCustomDataLayerID *CustomDataAttributeProvider::try_create_anonymous(
+ GeometryComponent &component,
+ const StringRef debug_name,
+ const AttributeDomain domain,
+ const CustomDataType data_type,
+ const AttributeInit &initializer) const
+{
+ if (domain_ != domain) {
+ return nullptr;
+ }
+ if (!this->type_is_supported(data_type)) {
+ return nullptr;
+ }
+ CustomData *custom_data = custom_data_access_.get_custom_data(component);
+ if (custom_data == nullptr) {
+ return nullptr;
+ }
+
+ const int domain_size = component.attribute_domain_size(domain_);
+ const std::string attribute_name = get_anonymous_attribute_name();
+ add_named_custom_data_layer_from_attribute_init(
+ attribute_name, *custom_data, data_type, domain_size, initializer);
+ const int layer_index = CustomData_get_named_layer_index(
+ custom_data, data_type, attribute_name.c_str());
+ CustomDataLayer *layer = &custom_data->layers[layer_index];
+ layer->flag |= CD_FLAG_ANONYMOUS;
+ layer->anonymous_id = CustomData_anonymous_id_new(
+ BLI_strdupn(debug_name.data(), debug_name.size()));
+ CustomData_anonymous_id_weak_increment(layer->anonymous_id);
+ return layer->anonymous_id;
+}
+
+ReadAttributeLookup CustomDataAttributeProvider::try_get_anonymous_for_read(
+ const GeometryComponent &component, const AnonymousCustomDataLayerID &layer_id) const
+{
+ const CustomData *custom_data = custom_data_access_.get_const_custom_data(component);
+ if (custom_data == nullptr) {
+ return {};
+ }
+ const int domain_size = component.attribute_domain_size(domain_);
+ for (const CustomDataLayer &layer : Span(custom_data->layers, custom_data->totlayer)) {
+ if (layer.anonymous_id != &layer_id) {
+ continue;
+ }
+ return this->layer_to_read_attribute(layer, domain_size);
+ }
+ return {};
+}
+
+WriteAttributeLookup CustomDataAttributeProvider::try_get_anonymous_for_write(
+ GeometryComponent &component, const AnonymousCustomDataLayerID &layer_id) const
+{
+ CustomData *custom_data = custom_data_access_.get_custom_data(component);
+ if (custom_data == nullptr) {
+ return {};
+ }
+ const int domain_size = component.attribute_domain_size(domain_);
+ for (CustomDataLayer &layer : MutableSpan(custom_data->layers, custom_data->totlayer)) {
+ if (layer.anonymous_id != &layer_id) {
+ continue;
+ }
+ CustomData_duplicate_referenced_layer_named(custom_data, layer.type, layer.name, domain_size);
+ return this->layer_to_write_attribute(layer, domain_size);
+ }
+ return {};
+}
+
bool CustomDataAttributeProvider::foreach_attribute(const GeometryComponent &component,
const AttributeForeachCallback callback) const
{
@@ -875,6 +961,56 @@ bool GeometryComponent::attribute_try_create(const StringRef attribute_name,
return false;
}
+AnonymousCustomDataLayerID *GeometryComponent::attribute_try_create_anonymous(
+ const blender::StringRef debug_name,
+ const AttributeDomain domain,
+ const CustomDataType data_type,
+ const AttributeInit &initializer)
+{
+ using namespace blender::bke;
+ const ComponentAttributeProviders *provide
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list