[Bf-blender-cvs] [6146a679c95] temp-geometry-nodes-fields--anonymous-attributes: support creating anonymous attributes

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


Commit: 6146a679c954b9847e3775add51af4a351bc8aaf
Author: Jacques Lucke
Date:   Mon Aug 30 14:24:18 2021 +0200
Branches: temp-geometry-nodes-fields--anonymous-attributes
https://developer.blender.org/rB6146a679c954b9847e3775add51af4a351bc8aaf

support creating anonymous attributes

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

M	source/blender/blenkernel/BKE_anonymous_attribute.hh
M	source/blender/blenkernel/intern/attribute_access.cc
M	source/blender/makesdna/DNA_customdata_types.h
M	source/blender/nodes/NOD_geometry_exec.hh

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

diff --git a/source/blender/blenkernel/BKE_anonymous_attribute.hh b/source/blender/blenkernel/BKE_anonymous_attribute.hh
index 3f1835052b8..3366455ad89 100644
--- a/source/blender/blenkernel/BKE_anonymous_attribute.hh
+++ b/source/blender/blenkernel/BKE_anonymous_attribute.hh
@@ -116,6 +116,11 @@ template<bool IsStrongReference> class OwnedAnonymousAttributeID {
     return extracted_data;
   }
 
+  const AnonymousAttributeID *get()
+  {
+    return data_;
+  }
+
  private:
   void incref()
   {
diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index 6e152ed188d..fbe5b664ad9 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -433,30 +433,46 @@ bool CustomDataAttributeProvider::try_delete(GeometryComponent &component,
   return false;
 }
 
-static bool add_named_custom_data_layer_from_attribute_init(const StringRef attribute_name,
-                                                            CustomData &custom_data,
-                                                            const CustomDataType data_type,
-                                                            const int domain_size,
-                                                            const AttributeInit &initializer)
+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);
+}
+
+static bool add_custom_data_layer_from_attribute_init(const AttributeIDRef &attribute_id,
+                                                      CustomData &custom_data,
+                                                      const CustomDataType data_type,
+                                                      const int domain_size,
+                                                      const AttributeInit &initializer)
 {
   char attribute_name_c[MAX_NAME];
-  attribute_name.copy(attribute_name_c);
+  if (attribute_id.is_named()) {
+    attribute_id.name().copy(attribute_name_c);
+  }
+  else {
+    const std::string name = get_anonymous_attribute_name();
+    STRNCPY(attribute_name_c, name.c_str());
+  }
+
+  bool success = false;
 
   switch (initializer.type) {
     case AttributeInit::Type::Default: {
       void *data = CustomData_add_layer_named(
           &custom_data, data_type, CD_DEFAULT, nullptr, domain_size, attribute_name_c);
-      return data != nullptr;
+      success = data != nullptr;
+      break;
     }
     case AttributeInit::Type::VArray: {
       void *data = CustomData_add_layer_named(
           &custom_data, data_type, CD_DEFAULT, nullptr, domain_size, attribute_name_c);
-      if (data == nullptr) {
-        return false;
+      if (data != nullptr) {
+        success = true;
+        const GVArray *varray = static_cast<const AttributeInitVArray &>(initializer).varray;
+        varray->materialize_to_uninitialized(IndexRange(varray->size()), data);
       }
-      const GVArray *varray = static_cast<const AttributeInitVArray &>(initializer).varray;
-      varray->materialize_to_uninitialized(IndexRange(varray->size()), data);
-      return true;
+      break;
     }
     case AttributeInit::Type::MoveArray: {
       void *source_data = static_cast<const AttributeInitMove &>(initializer).data;
@@ -464,14 +480,28 @@ static bool add_named_custom_data_layer_from_attribute_init(const StringRef attr
           &custom_data, data_type, CD_ASSIGN, source_data, domain_size, attribute_name_c);
       if (data == nullptr) {
         MEM_freeN(source_data);
-        return false;
       }
-      return true;
+      else {
+        success = true;
+      }
+      break;
     }
   }
 
-  BLI_assert_unreachable();
-  return false;
+  if (!success) {
+    return false;
+  }
+
+  if (attribute_id.is_anonymous()) {
+    for (CustomDataLayer &layer : MutableSpan(custom_data.layers, custom_data.totlayer)) {
+      if (STREQ(layer.name, attribute_name_c)) {
+        layer.anonymous_id = &attribute_id.anonymous_id();
+        BKE_anonymous_attribute_id_increment_weak(layer.anonymous_id);
+      }
+    }
+  }
+
+  return true;
 }
 
 bool CustomDataAttributeProvider::try_create(GeometryComponent &component,
@@ -496,12 +526,8 @@ bool CustomDataAttributeProvider::try_create(GeometryComponent &component,
     }
   }
   const int domain_size = component.attribute_domain_size(domain_);
-  /* TODO: Handle anonymous attribute. */
-  if (!attribute_id.is_named()) {
-    return false;
-  }
-  add_named_custom_data_layer_from_attribute_init(
-      attribute_id.name(), *custom_data, data_type, domain_size, initializer);
+  add_custom_data_layer_from_attribute_init(
+      attribute_id, *custom_data, data_type, domain_size, initializer);
   return true;
 }
 
diff --git a/source/blender/makesdna/DNA_customdata_types.h b/source/blender/makesdna/DNA_customdata_types.h
index d55741cacbe..9d3c2bc74d5 100644
--- a/source/blender/makesdna/DNA_customdata_types.h
+++ b/source/blender/makesdna/DNA_customdata_types.h
@@ -57,7 +57,7 @@ typedef struct CustomDataLayer {
   void *data;
   /** Run-time identifier for this layer.  If no
    * one has a strong reference to this id anymore, the layer can be removed. */
-  struct AnonymousAttributeID *anonymous_id;
+  const struct AnonymousAttributeID *anonymous_id;
 } CustomDataLayer;
 
 #define MAX_CUSTOMDATA_LAYER_NAME 64
diff --git a/source/blender/nodes/NOD_geometry_exec.hh b/source/blender/nodes/NOD_geometry_exec.hh
index d6a23051c0b..18ed8800d49 100644
--- a/source/blender/nodes/NOD_geometry_exec.hh
+++ b/source/blender/nodes/NOD_geometry_exec.hh
@@ -36,6 +36,8 @@ using bke::geometry_set_realize_instances;
 using bke::OutputAttribute;
 using bke::OutputAttribute_Typed;
 using bke::ReadAttributeLookup;
+using bke::StrongAnonymousAttributeID;
+using bke::WeakAnonymousAttributeID;
 using bke::WriteAttributeLookup;
 using fn::CPPType;
 using fn::GMutablePointer;



More information about the Bf-blender-cvs mailing list