[Bf-blender-cvs] [25237d26250] master: Attributes: Improve custom data initialization options

Hans Goudey noreply at git.blender.org
Tue Aug 30 21:58:36 CEST 2022


Commit: 25237d2625078c6d14d744f288776299efd3c7c8
Author: Hans Goudey
Date:   Tue Aug 30 14:54:53 2022 -0500
Branches: master
https://developer.blender.org/rB25237d2625078c6d14d744f288776299efd3c7c8

Attributes: Improve custom data initialization options

When allocating new `CustomData` layers, often we do redundant
initialization of arrays. For example, it's common that values are
allocated, set to their default value, and then set to some other
value. This is wasteful, and it negates the benefits of optimizations
to the allocator like D15082. There are two reasons for this. The
first is array-of-structs storage that makes it annoying to initialize
values manually, and the second is confusing options in the Custom Data
API. This patch addresses the latter.

The `CustomData` "alloc type" options are rearranged. Now, besides
the options that use existing layers, there are two remaining:
* `CD_SET_DEFAULT` sets the default value.
  * Usually zeroes, but for colors this is white (how it was before).
  * Should be used when you add the layer but don't set all values.
* `CD_CONSTRUCT` refers to the "default construct" C++ term.
  * Only necessary or defined for non-trivial types like vertex groups.
  * Doesn't do anything for trivial types like `int` or `float3`.
  * Should be used every other time, when all values will be set.

The attribute API's `AttributeInit` types are updated as well.
To update code, replace `CD_CALLOC` with `CD_SET_DEFAULT` and
`CD_DEFAULT` with `CD_CONSTRUCT`. This doesn't cause any functional
changes yet. Follow-up commits will change to avoid initializing
new layers where the correctness is clear.

Differential Revision: https://developer.blender.org/D15617

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

M	release/datafiles/locale
M	release/scripts/addons
M	source/blender/blenkernel/BKE_attribute.hh
M	source/blender/blenkernel/BKE_customdata.h
M	source/blender/blenkernel/intern/DerivedMesh.cc
M	source/blender/blenkernel/intern/attribute.cc
M	source/blender/blenkernel/intern/attribute_access.cc
M	source/blender/blenkernel/intern/curves_geometry.cc
M	source/blender/blenkernel/intern/customdata.cc
M	source/blender/blenkernel/intern/data_transfer.c
M	source/blender/blenkernel/intern/deform.c
M	source/blender/blenkernel/intern/dynamicpaint.c
M	source/blender/blenkernel/intern/editmesh_tangent.c
M	source/blender/blenkernel/intern/geometry_component_curve.cc
M	source/blender/blenkernel/intern/mball_tessellate.c
M	source/blender/blenkernel/intern/mesh.cc
M	source/blender/blenkernel/intern/mesh_boolean_convert.cc
M	source/blender/blenkernel/intern/mesh_convert.cc
M	source/blender/blenkernel/intern/mesh_legacy_convert.cc
M	source/blender/blenkernel/intern/mesh_normals.cc
M	source/blender/blenkernel/intern/mesh_remap.c
M	source/blender/blenkernel/intern/mesh_remesh_voxel.cc
M	source/blender/blenkernel/intern/mesh_tangent.c
M	source/blender/blenkernel/intern/mesh_validate.cc
M	source/blender/blenkernel/intern/multires.c
M	source/blender/blenkernel/intern/multires_reshape.c
M	source/blender/blenkernel/intern/multires_reshape_subdivide.c
M	source/blender/blenkernel/intern/multires_unsubdivide.c
M	source/blender/blenkernel/intern/object.cc
M	source/blender/blenkernel/intern/object_deform.c
M	source/blender/blenkernel/intern/paint.cc
M	source/blender/blenkernel/intern/particle_system.c
M	source/blender/blenkernel/intern/pbvh.c
M	source/blender/blenkernel/intern/pointcloud.cc
M	source/blender/blenkernel/intern/subdiv_mesh.cc
M	source/blender/blenkernel/intern/subsurf_ccg.c
M	source/blender/blenlib/intern/BLI_memarena.c
M	source/blender/blenloader/intern/versioning_legacy.c
M	source/blender/bmesh/intern/bmesh_construct.c
M	source/blender/bmesh/intern/bmesh_interp.c
M	source/blender/bmesh/intern/bmesh_mesh_convert.cc
M	source/blender/editors/curves/intern/curves_add.cc
M	source/blender/editors/geometry/geometry_attributes.cc
M	source/blender/editors/mesh/mesh_data.cc
M	source/blender/editors/mesh/meshtools.cc
M	source/blender/editors/object/object_facemap_ops.c
M	source/blender/editors/object/object_modifier.cc
M	source/blender/editors/sculpt_paint/paint_hide.c
M	source/blender/editors/sculpt_paint/sculpt_dyntopo.c
M	source/blender/editors/sculpt_paint/sculpt_undo.c
M	source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp
M	source/blender/io/alembic/intern/abc_customdata.cc
M	source/blender/io/alembic/intern/abc_reader_mesh.cc
M	source/blender/io/collada/MeshImporter.cpp
M	source/blender/io/stl/importer/stl_import_mesh.cc
M	source/blender/io/usd/intern/usd_reader_mesh.cc
M	source/blender/io/wavefront_obj/importer/obj_import_mesh.cc
M	source/blender/io/wavefront_obj/tests/obj_importer_tests.cc
M	source/blender/makesrna/intern/rna_mesh.c
M	source/blender/makesrna/intern/rna_mesh_api.c
M	source/blender/modifiers/intern/MOD_cloth.c
M	source/blender/modifiers/intern/MOD_nodes.cc
M	source/blender/modifiers/intern/MOD_normal_edit.c
M	source/blender/modifiers/intern/MOD_ocean.c
M	source/blender/modifiers/intern/MOD_screw.c
M	source/blender/modifiers/intern/MOD_skin.c
M	source/blender/modifiers/intern/MOD_solidify_extrude.c
M	source/blender/modifiers/intern/MOD_solidify_nonmanifold.c
M	source/blender/modifiers/intern/MOD_uvproject.c
M	source/blender/modifiers/intern/MOD_weighted_normal.c
M	source/blender/modifiers/intern/MOD_weightvgedit.c
M	source/blender/modifiers/intern/MOD_weightvgmix.c
M	source/blender/nodes/geometry/nodes/node_geo_store_named_attribute.cc
M	source/blender/nodes/geometry/nodes/node_geo_subdivision_surface.cc

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

diff --git a/release/datafiles/locale b/release/datafiles/locale
index a2eb5078914..1b891478f44 160000
--- a/release/datafiles/locale
+++ b/release/datafiles/locale
@@ -1 +1 @@
-Subproject commit a2eb507891449a0b67582be9561840075513661d
+Subproject commit 1b891478f44dd047c3a92fda3ebd17fae1c3acd3
diff --git a/release/scripts/addons b/release/scripts/addons
index 7a8502871c3..25ffc6f430f 160000
--- a/release/scripts/addons
+++ b/release/scripts/addons
@@ -1 +1 @@
-Subproject commit 7a8502871c34db0343cc7de52d6b49b15a84238a
+Subproject commit 25ffc6f430fc995b1c046b01acba1c3e6c1896b0
diff --git a/source/blender/blenkernel/BKE_attribute.hh b/source/blender/blenkernel/BKE_attribute.hh
index c2f65c93cbe..6284cce9dc0 100644
--- a/source/blender/blenkernel/BKE_attribute.hh
+++ b/source/blender/blenkernel/BKE_attribute.hh
@@ -73,8 +73,13 @@ struct AttributeKind {
  */
 struct AttributeInit {
   enum class Type {
-    Default,
+    /** #AttributeInitConstruct. */
+    Construct,
+    /** #AttributeInitDefaultValue. */
+    DefaultValue,
+    /** #AttributeInitVArray. */
     VArray,
+    /** #AttributeInitMoveArray. */
     MoveArray,
   };
   Type type;
@@ -84,11 +89,20 @@ struct AttributeInit {
 };
 
 /**
- * Create an attribute using the default value for the data type.
- * The default values may depend on the attribute provider implementation.
+ * Default construct new attribute values. Does nothing for trivial types. This should be used
+ * if all attribute element values will be set by the caller after creating the attribute.
  */
-struct AttributeInitDefault : public AttributeInit {
-  AttributeInitDefault() : AttributeInit(Type::Default)
+struct AttributeInitConstruct : public AttributeInit {
+  AttributeInitConstruct() : AttributeInit(Type::Construct)
+  {
+  }
+};
+
+/**
+ * Create an attribute using the default value for the data type (almost always "zero").
+ */
+struct AttributeInitDefaultValue : public AttributeInit {
+  AttributeInitDefaultValue() : AttributeInit(Type::DefaultValue)
   {
   }
 };
@@ -96,14 +110,11 @@ struct AttributeInitDefault : public AttributeInit {
 /**
  * Create an attribute by copying data from an existing virtual array. The virtual array
  * must have the same type as the newly created attribute.
- *
- * Note that this can be used to fill the new attribute with the default
  */
 struct AttributeInitVArray : public AttributeInit {
-  blender::GVArray varray;
+  GVArray varray;
 
-  AttributeInitVArray(blender::GVArray varray)
-      : AttributeInit(Type::VArray), varray(std::move(varray))
+  AttributeInitVArray(GVArray varray) : AttributeInit(Type::VArray), varray(std::move(varray))
   {
   }
 };
@@ -119,10 +130,10 @@ struct AttributeInitVArray : public AttributeInit {
  * The array must be allocated with MEM_*, since `attribute_try_create` will free the array if it
  * can't be used directly, and that is generally how Blender expects custom data to be allocated.
  */
-struct AttributeInitMove : public AttributeInit {
+struct AttributeInitMoveArray : public AttributeInit {
   void *data = nullptr;
 
-  AttributeInitMove(void *data) : AttributeInit(Type::MoveArray), data(data)
+  AttributeInitMoveArray(void *data) : AttributeInit(Type::MoveArray), data(data)
   {
   }
 };
@@ -579,7 +590,7 @@ class MutableAttributeAccessor : public AttributeAccessor {
       const AttributeIDRef &attribute_id,
       const eAttrDomain domain,
       const eCustomDataType data_type,
-      const AttributeInit &initializer = AttributeInitDefault());
+      const AttributeInit &initializer = AttributeInitDefaultValue());
 
   /**
    * Same as above, but returns a type that makes it easier to work with the attribute as a span.
@@ -590,7 +601,7 @@ class MutableAttributeAccessor : public AttributeAccessor {
       const AttributeIDRef &attribute_id,
       const eAttrDomain domain,
       const eCustomDataType data_type,
-      const AttributeInit &initializer = AttributeInitDefault());
+      const AttributeInit &initializer = AttributeInitDefaultValue());
 
   /**
    * Same as above, but should be used when the type is known at compile time.
@@ -599,7 +610,7 @@ class MutableAttributeAccessor : public AttributeAccessor {
   AttributeWriter<T> lookup_or_add_for_write(
       const AttributeIDRef &attribute_id,
       const eAttrDomain domain,
-      const AttributeInit &initializer = AttributeInitDefault())
+      const AttributeInit &initializer = AttributeInitDefaultValue())
   {
     const CPPType &cpp_type = CPPType::get<T>();
     const eCustomDataType data_type = cpp_type_to_custom_data_type(cpp_type);
@@ -613,7 +624,7 @@ class MutableAttributeAccessor : public AttributeAccessor {
   SpanAttributeWriter<T> lookup_or_add_for_write_span(
       const AttributeIDRef &attribute_id,
       const eAttrDomain domain,
-      const AttributeInit &initializer = AttributeInitDefault())
+      const AttributeInit &initializer = AttributeInitDefaultValue())
   {
     AttributeWriter<T> attribute = this->lookup_or_add_for_write<T>(
         attribute_id, domain, initializer);
diff --git a/source/blender/blenkernel/BKE_customdata.h b/source/blender/blenkernel/BKE_customdata.h
index 3db75fff12c..44a4f4b5395 100644
--- a/source/blender/blenkernel/BKE_customdata.h
+++ b/source/blender/blenkernel/BKE_customdata.h
@@ -55,14 +55,17 @@ extern const CustomData_MeshMasks CD_MASK_EVERYTHING;
 typedef enum eCDAllocType {
   /** Use the data pointer. */
   CD_ASSIGN = 0,
-  /** Allocate blank memory. */
-  CD_CALLOC = 1,
-  /** Allocate and set to default. */
-  CD_DEFAULT = 2,
+  /** Allocate and set to default, which is usually just zeroed memory. */
+  CD_SET_DEFAULT = 2,
   /** Use data pointers, set layer flag NOFREE. */
   CD_REFERENCE = 3,
   /** Do a full copy of all layers, only allowed if source has same number of elements. */
   CD_DUPLICATE = 4,
+  /**
+   * Default construct new layer values. Does nothing for trivial types. This should be used
+   * if all layer values will be set by the caller after creating the layer.
+   */
+  CD_CONSTRUCT = 5,
 } eCDAllocType;
 
 #define CD_TYPE_AS_MASK(_type) (eCustomDataMask)((eCustomDataMask)1 << (eCustomDataMask)(_type))
diff --git a/source/blender/blenkernel/intern/DerivedMesh.cc b/source/blender/blenkernel/intern/DerivedMesh.cc
index 2ce5863c176..3afdbccb1f4 100644
--- a/source/blender/blenkernel/intern/DerivedMesh.cc
+++ b/source/blender/blenkernel/intern/DerivedMesh.cc
@@ -95,7 +95,7 @@ static MVert *dm_getVertArray(DerivedMesh *dm)
 
   if (!mvert) {
     mvert = (MVert *)CustomData_add_layer(
-        &dm->vertData, CD_MVERT, CD_CALLOC, nullptr, dm->getNumVerts(dm));
+        &dm->vertData, CD_MVERT, CD_SET_DEFAULT, nullptr, dm->getNumVerts(dm));
     CustomData_set_layer_flag(&dm->vertData, CD_MVERT, CD_FLAG_TEMPORARY);
     dm->copyVertArray(dm, mvert);
   }
@@ -109,7 +109,7 @@ static MEdge *dm_getEdgeArray(DerivedMesh *dm)
 
   if (!medge) {
     medge = (MEdge *)CustomData_add_layer(
-        &dm->edgeData, CD_MEDGE, CD_CALLOC, nullptr, dm->getNumEdges(dm));
+        &dm->edgeData, CD_MEDGE, CD_SET_DEFAULT, nullptr, dm->getNumEdges(dm));
     CustomData_set_layer_flag(&dm->edgeData, CD_MEDGE, CD_FLAG_TEMPORARY);
     dm->copyEdgeArray(dm, medge);
   }
@@ -123,7 +123,7 @@ static MLoop *dm_getLoopArray(DerivedMesh *dm)
 
   if (!mloop) {
     mloop = (MLoop *)CustomData_add_layer(
-        &dm->loopData, CD_MLOOP, CD_CALLOC, nullptr, dm->getNumLoops(dm));
+        &dm->loopData, CD_MLOOP, CD_SET_DEFAULT, nullptr, dm->getNumLoops(dm));
     CustomData_set_layer_flag(&dm->loopData, CD_MLOOP, CD_FLAG_TEMPORARY);
     dm->copyLoopArray(dm, mloop);
   }
@@ -137,7 +137,7 @@ static MPoly *dm_getPolyArray(DerivedMesh *dm)
 
   if (!mpoly) {
     mpoly = (MPoly *)CustomData_add_layer(
-        &dm->polyData, CD_MPOLY, CD_CALLOC, nullptr, dm->getNumPolys(dm));
+        &dm->polyData, CD_MPOLY, CD_SET_DEFAULT, nullptr, dm->getNumPolys(dm));
     CustomData_set_layer_flag(&dm->polyData, CD_MPOLY, CD_FLAG_TEMPORARY);
     dm->copyPolyArray(dm, mpoly);
   }
@@ -284,11 +284,11 @@ void DM_from_template(DerivedMesh *dm,
                       int numPolys)
 {
   const CustomData_MeshMasks *mask = &CD_MASK_DERIVEDMESH;
-  CustomData_copy(&source->vertData, &dm->vertData, mask->vmask, CD_CALLOC, numVerts);
-  CustomData_copy(&source->edgeData, &dm->edgeData, mask->emask, CD_CALLOC, numEdges);
-  CustomData_copy(&source->faceData, &dm->faceData, mask->fmask, CD_CALLOC, numTessFaces);
-  CustomData_copy(&source->loopData, &dm->loopData, mask->lmask, CD_CALLOC, numLoops);
-  CustomData_copy(&source->polyData, &dm->polyData, mask->pmask, CD_CALLOC, numPolys);
+  CustomData_copy(&source->vertData, &dm->vertData, mask->vmask, CD_SET_DEFAULT, numVerts);
+  CustomData_copy(&source->edgeData, &dm->edgeData, mask->emask, CD_SET_DEFAULT, numEdges);
+  CustomData_copy(&source->faceData, &dm->faceData, mask->fmask, CD_SET_DEFAULT, numTessFaces);
+  CustomData_copy(&source->loopData, &dm->loopData, mask->lmask, CD_SET_DEFAULT, numLoops);
+  CustomData_copy(&source->polyData, &dm->polyData, mask->pmask, CD_SET_DEFAULT, numPolys);
 
   dm->cd_flag = source->cd_flag;
 
@@ -584,7 +584,7 @@ static void add_orco_mesh(Object *ob, BMEditMesh *em, Mesh *mesh, Mesh *mesh_orc
     }
 
     if (!(layerorco = (float(*)[3])CustomData_get_layer(&mesh->vdata, layer))) {
-      CustomData_add_layer(&mesh->vdata, layer, CD_CALLOC, nullptr, mesh->totvert);
+      CustomData_add_layer(&mesh->vdata, layer, CD_SET_DEFAULT, nullptr, mesh->totvert);
       BKE_mesh_update_customdata_pointers(mesh, false);
 
       layerorco = (float(*)[3])CustomData_get_layer(&mesh->vdata, layer);
@@ -826,7 +826,7 @@ static void mesh_calc_modifiers(struct Depsgraph *depsgraph,
     }
     float3 *rest_positions = static_cast<float3 *>(CustomData_add_layer_named(&mesh_final->vdata,
                                                                               CD_PROP_FLOAT3,
-                                                                              CD_DEFAULT,
+                                                                              CD_SET_DEFAULT,
                                                                               nullptr,
                             

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list