[Bf-blender-cvs] [fb0d5124f28] temp-geometry-nodes-fields--anonymous-attributes: support anonymous attributes in foreach_attribute

Jacques Lucke noreply at git.blender.org
Mon Aug 30 16:03:07 CEST 2021


Commit: fb0d5124f28c639ebd49deeb461e9bb5fc8accc2
Author: Jacques Lucke
Date:   Mon Aug 30 16:02:58 2021 +0200
Branches: temp-geometry-nodes-fields--anonymous-attributes
https://developer.blender.org/rBfb0d5124f28c639ebd49deeb461e9bb5fc8accc2

support anonymous attributes in foreach_attribute

===================================================================

M	source/blender/blenkernel/BKE_anonymous_attribute.h
M	source/blender/blenkernel/BKE_anonymous_attribute.hh
M	source/blender/blenkernel/BKE_attribute_access.hh
M	source/blender/blenkernel/BKE_customdata.h
M	source/blender/blenkernel/BKE_geometry_set.hh
M	source/blender/blenkernel/BKE_geometry_set_instances.hh
M	source/blender/blenkernel/intern/anonymous_attribute.cc
M	source/blender/blenkernel/intern/attribute_access.cc
M	source/blender/blenkernel/intern/curve_eval.cc
M	source/blender/blenkernel/intern/customdata.c
M	source/blender/blenkernel/intern/geometry_set_instances.cc
M	source/blender/editors/space_spreadsheet/spreadsheet_data_source_geometry.cc
M	source/blender/nodes/NOD_geometry_exec.hh
M	source/blender/nodes/geometry/node_geometry_util.hh
M	source/blender/nodes/geometry/nodes/node_geo_curve_endpoints.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_resample.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_reverse.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_spline_type.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_subdivide.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_to_points.cc
M	source/blender/nodes/geometry/nodes/node_geo_curve_trim.cc
M	source/blender/nodes/geometry/nodes/node_geo_delete_geometry.cc
M	source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc
M	source/blender/nodes/geometry/nodes/node_geo_mesh_to_curve.cc
M	source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc
M	source/blender/nodes/geometry/nodes/node_geo_point_separate.cc
M	source/blender/nodes/intern/geometry_nodes_eval_log.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_anonymous_attribute.h b/source/blender/blenkernel/BKE_anonymous_attribute.h
index cc9604dd99d..6ac9a514ec9 100644
--- a/source/blender/blenkernel/BKE_anonymous_attribute.h
+++ b/source/blender/blenkernel/BKE_anonymous_attribute.h
@@ -30,6 +30,7 @@ void BKE_anonymous_attribute_id_increment_strong(const AnonymousAttributeID *ano
 void BKE_anonymous_attribute_id_decrement_weak(const AnonymousAttributeID *anonymous_id);
 void BKE_anonymous_attribute_id_decrement_strong(const AnonymousAttributeID *anonymous_id);
 const char *BKE_anonymous_attribute_id_debug_name(const AnonymousAttributeID *anonymous_id);
+const char *BKE_anonymous_attribute_id_internal_name(const AnonymousAttributeID *anonymous_id);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/blenkernel/BKE_anonymous_attribute.hh b/source/blender/blenkernel/BKE_anonymous_attribute.hh
index 3366455ad89..ee7aaa2d4d5 100644
--- a/source/blender/blenkernel/BKE_anonymous_attribute.hh
+++ b/source/blender/blenkernel/BKE_anonymous_attribute.hh
@@ -19,6 +19,7 @@
 #include <atomic>
 #include <string>
 
+#include "BLI_hash.hh"
 #include "BLI_string_ref.hh"
 
 #include "BKE_anonymous_attribute.h"
@@ -186,6 +187,16 @@ class AttributeIDRef {
     return this->is_named() || this->is_anonymous();
   }
 
+  friend bool operator==(const AttributeIDRef &a, const AttributeIDRef &b)
+  {
+    return a.anonymous_id_ == b.anonymous_id_ && a.name_ == b.name_;
+  }
+
+  uint64_t hash() const
+  {
+    return get_default_hash_2(name_, anonymous_id_);
+  }
+
   bool is_named() const
   {
     return !name_.is_empty();
diff --git a/source/blender/blenkernel/BKE_attribute_access.hh b/source/blender/blenkernel/BKE_attribute_access.hh
index 7d90fedd99d..6f5567b1aa5 100644
--- a/source/blender/blenkernel/BKE_attribute_access.hh
+++ b/source/blender/blenkernel/BKE_attribute_access.hh
@@ -105,8 +105,8 @@ struct AttributeInitMove : public AttributeInit {
 };
 
 /* Returns false when the iteration should be stopped. */
-using AttributeForeachCallback = blender::FunctionRef<bool(blender::StringRefNull attribute_name,
-                                                           const AttributeMetaData &meta_data)>;
+using AttributeForeachCallback = blender::FunctionRef<bool(
+    const blender::bke::AttributeIDRef &attribute_id, const AttributeMetaData &meta_data)>;
 
 namespace blender::bke {
 
diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h
index 7a44553c565..0732f1e5190 100644
--- a/source/blender/blenkernel/BKE_customdata.h
+++ b/source/blender/blenkernel/BKE_customdata.h
@@ -33,6 +33,7 @@
 extern "C" {
 #endif
 
+struct AnonymousAttributeID;
 struct BMesh;
 struct BlendDataReader;
 struct BlendWriter;
@@ -193,6 +194,12 @@ void *CustomData_add_layer_named(struct CustomData *data,
                                  void *layer,
                                  int totelem,
                                  const char *name);
+void *CustomData_add_layer_anonymous(struct CustomData *data,
+                                     int type,
+                                     eCDAllocType alloctype,
+                                     void *layer,
+                                     int totelem,
+                                     const struct AnonymousAttributeID *anonymous_id);
 
 /* frees the active or first data layer with the give type.
  * returns 1 on success, 0 if no layer with the given type is found
@@ -231,6 +238,11 @@ void *CustomData_duplicate_referenced_layer_named(struct CustomData *data,
                                                   const int type,
                                                   const char *name,
                                                   const int totelem);
+void *CustomData_duplicate_referenced_layer_anonymous(
+    CustomData *data,
+    const int type,
+    const struct AnonymousAttributeID *anonymous_id,
+    const int totelem);
 bool CustomData_is_referenced_layer(struct CustomData *data, int type);
 
 /* Duplicate all the layers with flag NOFREE, and remove the flag from duplicated layers. */
diff --git a/source/blender/blenkernel/BKE_geometry_set.hh b/source/blender/blenkernel/BKE_geometry_set.hh
index cf9d213384c..45e8ab92e41 100644
--- a/source/blender/blenkernel/BKE_geometry_set.hh
+++ b/source/blender/blenkernel/BKE_geometry_set.hh
@@ -134,7 +134,7 @@ class GeometryComponent {
   bool attribute_try_create_builtin(const blender::StringRef attribute_name,
                                     const AttributeInit &initializer);
 
-  blender::Set<std::string> attribute_names() const;
+  blender::Set<blender::bke::AttributeIDRef> attribute_ids() const;
   bool attribute_foreach(const AttributeForeachCallback callback) const;
 
   virtual bool is_empty() const;
diff --git a/source/blender/blenkernel/BKE_geometry_set_instances.hh b/source/blender/blenkernel/BKE_geometry_set_instances.hh
index 25876296a47..44a0ee30c4c 100644
--- a/source/blender/blenkernel/BKE_geometry_set_instances.hh
+++ b/source/blender/blenkernel/BKE_geometry_set_instances.hh
@@ -59,9 +59,10 @@ struct AttributeKind {
  * will contain the highest complexity data type and the highest priority domain among every
  * attribute with the given name on all of the input components.
  */
-void geometry_set_gather_instances_attribute_info(Span<GeometryInstanceGroup> set_groups,
-                                                  Span<GeometryComponentType> component_types,
-                                                  const Set<std::string> &ignored_attributes,
-                                                  Map<std::string, AttributeKind> &r_attributes);
+void geometry_set_gather_instances_attribute_info(
+    Span<GeometryInstanceGroup> set_groups,
+    Span<GeometryComponentType> component_types,
+    const Set<std::string> &ignored_attributes,
+    Map<AttributeIDRef, AttributeKind> &r_attributes);
 
 }  // namespace blender::bke
diff --git a/source/blender/blenkernel/intern/anonymous_attribute.cc b/source/blender/blenkernel/intern/anonymous_attribute.cc
index 8d9cddcbcb4..7088dff3db9 100644
--- a/source/blender/blenkernel/intern/anonymous_attribute.cc
+++ b/source/blender/blenkernel/intern/anonymous_attribute.cc
@@ -19,15 +19,24 @@
 using namespace blender::bke;
 
 struct AnonymousAttributeID {
-  mutable std::atomic<int> refcount_weak;
-  mutable std::atomic<int> refcount_strong;
+  mutable std::atomic<int> refcount_weak = 0;
+  mutable std::atomic<int> refcount_strong = 0;
   std::string debug_name;
+  std::string internal_name;
 };
 
+static std::string get_new_internal_name()
+{
+  static std::atomic<int> index = 0;
+  const int next_index = index.fetch_add(1);
+  return "anonymous_attribute_" + std::to_string(next_index);
+}
+
 AnonymousAttributeID *BKE_anonymous_attribute_id_new_weak(const char *debug_name)
 {
   AnonymousAttributeID *anonymous_id = new AnonymousAttributeID();
   anonymous_id->debug_name = debug_name;
+  anonymous_id->internal_name = get_new_internal_name();
   anonymous_id->refcount_weak.store(1);
   return anonymous_id;
 }
@@ -36,6 +45,7 @@ AnonymousAttributeID *BKE_anonymous_attribute_id_new_strong(const char *debug_na
 {
   AnonymousAttributeID *anonymous_id = new AnonymousAttributeID();
   anonymous_id->debug_name = debug_name;
+  anonymous_id->internal_name = get_new_internal_name();
   anonymous_id->refcount_weak.store(1);
   anonymous_id->refcount_strong.store(1);
   return anonymous_id;
@@ -75,3 +85,8 @@ const char *BKE_anonymous_attribute_id_debug_name(const AnonymousAttributeID *an
 {
   return anonymous_id->debug_name.c_str();
 }
+
+const char *BKE_anonymous_attribute_id_internal_name(const AnonymousAttributeID *anonymous_id)
+{
+  return anonymous_id->internal_name.c_str();
+}
diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index fbe5b664ad9..941bfebcc60 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -391,8 +391,14 @@ WriteAttributeLookup CustomDataAttributeProvider::try_get_for_write(
     if (!custom_data_layer_matches_attribute_id(layer, attribute_id)) {
       continue;
     }
-    /* TODO: Properly handle anonymous attribute. */
-    CustomData_duplicate_referenced_layer_named(custom_data, layer.type, layer.name, domain_size);
+    if (attribute_id.is_named()) {
+      CustomData_duplicate_referenced_layer_named(
+          custom_data, layer.type, layer.name, domain_size);
+    }
+    else {
+      CustomData_duplicate_referenced_layer_anonymous(
+          custom_data, layer.type, &attribute_id.anonymous_id(), domain_size);
+    }
     const CustomDataType data_type = (CustomDataType)layer.type;
     switch (data_type) {
       case CD_PROP_FLOAT:
@@ -433,11 +439,22 @@ bool CustomDataAttributeProvider::try_delete(GeometryComponent &component,
   return false;
 }
 
-static std::string get_anonymous_attribute_name()
+static void *add_generic_custom_data_layer(CustomData &custom_data,
+                                           const CustomDataType data_type,
+                                           const eCDAllocType alloctype,
+                                           void *layer_data,
+                                           const int domain_size,
+                                           const AttributeIDRef &attribute_id)
 {
-  static std::atomic<int> index = 0;
-  const int next_index = index.fetch_add(1);
-  return "anonymous_attribute_" + std::to_string(next_index);
+  if (attribute_id.is_named()) {
+    char attribute_name_c[MAX_NAME];
+    attribute_id.name().copy(attribute_name_c);
+    return CustomData_add_layer_named(
+        &custom_data, data_type, CD_DEFAULT, nullptr, domain_size, attribute_name_c);
+  }
+  const AnonymousAttributeID &anonymous_id = attribute_id.anonymous_id();
+  return CustomData_add_layer_anonymous(
+      &custom_data, data_type, alloctype, layer_data, domain_size, &anonymous_id);
 }
 
 static bool add_custom_data_layer_from_attribute_init(const AttributeIDRef &attribute_id,
@@ -446,62 +463,36 @@ static bool add_custom_data_layer_from_attribute_init(const AttributeIDR

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list