[Bf-blender-cvs] [f1bc7326eb1] refactor-mesh-face-generic: Add allocation API, switch to offsets in more places

Hans Goudey noreply at git.blender.org
Tue Feb 7 00:00:56 CET 2023


Commit: f1bc7326eb1c021ef224ce1603fc5af1583df2e7
Author: Hans Goudey
Date:   Mon Feb 6 18:00:38 2023 -0500
Branches: refactor-mesh-face-generic
https://developer.blender.org/rBf1bc7326eb1c021ef224ce1603fc5af1583df2e7

Add allocation API, switch to offsets in more places

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

M	intern/cycles/blender/mesh.cpp
M	source/blender/blenkernel/BKE_mesh.h
M	source/blender/blenkernel/intern/customdata.cc
M	source/blender/blenkernel/intern/mesh.cc
M	source/blender/blenkernel/intern/mesh_boolean_convert.cc
M	source/blender/blenkernel/intern/mesh_legacy_convert.cc
M	source/blender/blenkernel/intern/pbvh.cc
M	source/blender/blenkernel/intern/subsurf_ccg.cc
M	source/blender/bmesh/intern/bmesh_mesh_convert.cc
M	source/blender/draw/DRW_pbvh.hh
M	source/blender/draw/intern/draw_pbvh.cc
M	source/blender/editors/mesh/mesh_data.cc
M	source/blender/editors/mesh/meshtools.cc
M	source/blender/editors/sculpt_paint/sculpt_face_set.cc
M	source/blender/editors/uvedit/uvedit_unwrap_ops.cc
M	source/blender/freestyle/intern/blender_interface/BlenderStrokeRenderer.cpp
M	source/blender/io/collada/MeshImporter.cpp
M	source/blender/io/stl/importer/stl_import_mesh.cc
M	source/blender/makesdna/DNA_customdata_types.h

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

diff --git a/intern/cycles/blender/mesh.cpp b/intern/cycles/blender/mesh.cpp
index 3cd762bbf05..72d4750c7db 100644
--- a/intern/cycles/blender/mesh.cpp
+++ b/intern/cycles/blender/mesh.cpp
@@ -246,11 +246,12 @@ static void fill_generic_attribute(BL::Mesh &b_mesh,
         if (polys_num == 0) {
           return;
         }
-        const MPoly *polys = static_cast<const MPoly *>(b_mesh.polygons[0].ptr.data);
+        const int *poly_offsets = static_cast<const int *>(b_mesh.polygons[0].ptr.data);
         for (int i = 0; i < polys_num; i++) {
-          const MPoly &b_poly = polys[i];
-          for (int j = 0; j < b_poly.totloop; j++) {
-            *data = get_value_at_index(b_poly.loopstart + j);
+          const int poly_start = poly_offsets[i];
+          const int poly_size = poly_offsets[i + 1] - poly_start;
+          for (int j = 0; j < poly_size; j++) {
+            *data = get_value_at_index(poly_start + j);
             data++;
           }
         }
@@ -578,7 +579,7 @@ static void attr_create_subd_uv_map(Scene *scene, Mesh *mesh, BL::Mesh &b_mesh,
   if (polys_num == 0) {
     return;
   }
-  const MPoly *polys = static_cast<const MPoly *>(b_mesh.polygons[0].ptr.data);
+  const int *poly_offsets = static_cast<const int *>(b_mesh.polygons[0].ptr.data);
 
   if (!b_mesh.uv_layers.empty()) {
     BL::Mesh::uv_layers_iterator l;
@@ -614,9 +615,10 @@ static void attr_create_subd_uv_map(Scene *scene, Mesh *mesh, BL::Mesh &b_mesh,
         float2 *fdata = uv_attr->data_float2();
 
         for (int i = 0; i < polys_num; i++) {
-          const MPoly &b_poly = polys[i];
-          for (int j = 0; j < b_poly.totloop; j++) {
-            *(fdata++) = get_float2(l->data[b_poly.loopstart + j].uv());
+          const int poly_start = poly_offsets[i];
+          const int poly_size = poly_offsets[i + 1] - poly_start;
+          for (int j = 0; j < poly_size; j++) {
+            *(fdata++) = get_float2(l->data[poly_start + j].uv());
           }
         }
       }
@@ -880,11 +882,12 @@ static void attr_create_random_per_island(Scene *scene,
   }
   else {
     if (polys_num != 0) {
-      const MPoly *polys = static_cast<const MPoly *>(b_mesh.polygons[0].ptr.data);
+      const int *poly_offsets = static_cast<const int *>(b_mesh.polygons[0].ptr.data);
       BL::IntAttribute corner_verts = *find_corner_vert_attribute(b_mesh);
       for (int i = 0; i < polys_num; i++) {
-        const MPoly &b_poly = polys[i];
-        const int vert = corner_verts.data[b_poly.loopstart].value();
+        const int poly_start = poly_offsets[i];
+        const int poly_size = poly_offsets[i + 1] - poly_start;
+        const int vert = corner_verts.data[poly_start].value();
         data[i] = hash_uint_to_float(vertices_sets.find(vert));
       }
     }
@@ -957,11 +960,12 @@ static void create_mesh(Scene *scene,
     numtris = numfaces;
   }
   else {
-    const MPoly *polys = static_cast<const MPoly *>(b_mesh.polygons[0].ptr.data);
+    const int *poly_offsets = static_cast<const int *>(b_mesh.polygons[0].ptr.data);
     for (int i = 0; i < polys_num; i++) {
-      const MPoly &b_poly = polys[i];
-      numngons += (b_poly.totloop == 4) ? 0 : 1;
-      numcorners += b_poly.totloop;
+      const int poly_start = poly_offsets[i];
+      const int poly_size = poly_offsets[i + 1] - poly_start;
+      numngons += (poly_size == 4) ? 0 : 1;
+      numcorners += poly_size;
     }
   }
 
@@ -1052,23 +1056,23 @@ static void create_mesh(Scene *scene,
   else {
     vector<int> vi;
 
-    const MPoly *polys = static_cast<const MPoly *>(b_mesh.polygons[0].ptr.data);
+    const int *poly_offsets = static_cast<const int *>(b_mesh.polygons[0].ptr.data);
     std::optional<BL::IntAttribute> corner_verts = find_corner_vert_attribute(b_mesh);
 
     for (int i = 0; i < numfaces; i++) {
-      const MPoly &b_poly = polys[i];
-      int n = b_poly.totloop;
+      const int poly_start = poly_offsets[i];
+      const int poly_size = poly_offsets[i + 1] - poly_start;
       int shader = get_material_index(i);
       bool smooth = !get_face_sharp(i) || use_loop_normals;
 
-      vi.resize(n);
-      for (int i = 0; i < n; i++) {
+      vi.resize(poly_size);
+      for (int i = 0; i < poly_size; i++) {
         /* NOTE: Autosmooth is already taken care about. */
-        vi[i] = corner_verts->data[b_poly.loopstart + i].value();
+        vi[i] = corner_verts->data[poly_start + i].value();
       }
 
       /* create subd faces */
-      mesh->add_subd_face(&vi[0], n, shader, smooth);
+      mesh->add_subd_face(&vi[0], poly_size, shader, smooth);
     }
   }
 
diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h
index 5eabf3c62fd..fc966691367 100644
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@ -161,6 +161,8 @@ void BKE_mesh_ensure_skin_customdata(struct Mesh *me);
 
 struct Mesh *BKE_mesh_new_nomain(
     int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len);
+/** Add poly offsets to describe faces to a new mesh. */
+void BKE_mesh_ensure_poly_offsets(struct Mesh *mesh);
 struct Mesh *BKE_mesh_new_nomain_from_template(const struct Mesh *me_src,
                                                int verts_len,
                                                int edges_len,
diff --git a/source/blender/blenkernel/intern/customdata.cc b/source/blender/blenkernel/intern/customdata.cc
index f2674352249..3842d3d3700 100644
--- a/source/blender/blenkernel/intern/customdata.cc
+++ b/source/blender/blenkernel/intern/customdata.cc
@@ -1770,7 +1770,7 @@ static const LayerTypeInfo LAYERTYPEINFO[CD_NUMTYPES] = {
     {sizeof(float[3]), "", 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr},
     /* 24: CD_RECAST */
     {sizeof(MRecast), "MRecast", 1, N_("Recast"), nullptr, nullptr, nullptr, nullptr},
-    /* 25: CD_MPOLY */
+    /* 25: CD_MPOLY */ /* DEPRECATED */
     {sizeof(MPoly), "MPoly", 1, N_("NGon Face"), nullptr, nullptr, nullptr, nullptr, nullptr},
     /* 26: CD_MLOOP */ /* DEPRECATED*/
     {sizeof(MLoop),
@@ -2031,14 +2031,14 @@ const CustomData_MeshMasks CD_MASK_BAREMESH = {
     /*vmask*/ CD_MASK_PROP_FLOAT3,
     /*emask*/ CD_MASK_MEDGE,
     /*fmask*/ 0,
-    /*pmask*/ CD_MASK_MPOLY | CD_MASK_FACEMAP,
+    /*pmask*/ CD_MASK_FACEMAP,
     /*lmask*/ CD_MASK_PROP_INT32,
 };
 const CustomData_MeshMasks CD_MASK_BAREMESH_ORIGINDEX = {
     /*vmask*/ CD_MASK_PROP_FLOAT3 | CD_MASK_ORIGINDEX,
     /*emask*/ CD_MASK_MEDGE | CD_MASK_ORIGINDEX,
     /*fmask*/ 0,
-    /*pmask*/ CD_MASK_MPOLY | CD_MASK_FACEMAP | CD_MASK_ORIGINDEX,
+    /*pmask*/ CD_MASK_FACEMAP | CD_MASK_ORIGINDEX,
     /*lmask*/ CD_MASK_PROP_INT32,
 };
 const CustomData_MeshMasks CD_MASK_MESH = {
@@ -2048,7 +2048,7 @@ const CustomData_MeshMasks CD_MASK_MESH = {
     (CD_MASK_MEDGE | CD_MASK_FREESTYLE_EDGE | CD_MASK_PROP_ALL | CD_MASK_BWEIGHT | CD_MASK_CREASE),
     /*fmask*/ 0,
     /*pmask*/
-    (CD_MASK_MPOLY | CD_MASK_FACEMAP | CD_MASK_FREESTYLE_FACE | CD_MASK_PROP_ALL),
+    (CD_MASK_FACEMAP | CD_MASK_FREESTYLE_FACE | CD_MASK_PROP_ALL),
     /*lmask*/
     (CD_MASK_MDISPS | CD_MASK_CUSTOMLOOPNORMAL | CD_MASK_GRID_PAINT_MASK | CD_MASK_PROP_ALL),
 };
@@ -2088,8 +2088,8 @@ const CustomData_MeshMasks CD_MASK_EVERYTHING = {
      CD_MASK_ORIGSPACE | CD_MASK_TANGENT | CD_MASK_TESSLOOPNORMAL | CD_MASK_PREVIEW_MCOL |
      CD_MASK_PROP_ALL),
     /*pmask*/
-    (CD_MASK_MPOLY | CD_MASK_BM_ELEM_PYPTR | CD_MASK_ORIGINDEX | CD_MASK_FACEMAP |
-     CD_MASK_FREESTYLE_FACE | CD_MASK_PROP_ALL),
+    (CD_MASK_BM_ELEM_PYPTR | CD_MASK_ORIGINDEX | CD_MASK_FACEMAP | CD_MASK_FREESTYLE_FACE |
+     CD_MASK_PROP_ALL),
     /*lmask*/
     (CD_MASK_BM_ELEM_PYPTR | CD_MASK_MDISPS | CD_MASK_NORMAL | CD_MASK_CUSTOMLOOPNORMAL |
      CD_MASK_MLOOPTANGENT | CD_MASK_PREVIEW_MLOOPCOL | CD_MASK_ORIGSPACE_MLOOP |
diff --git a/source/blender/blenkernel/intern/mesh.cc b/source/blender/blenkernel/intern/mesh.cc
index 53c8a777db5..3d77e661255 100644
--- a/source/blender/blenkernel/intern/mesh.cc
+++ b/source/blender/blenkernel/intern/mesh.cc
@@ -520,7 +520,7 @@ static int customdata_compare(
 {
   CustomDataLayer *l1, *l2;
   int layer_count1 = 0, layer_count2 = 0, j;
-  const uint64_t cd_mask_non_generic = CD_MASK_MEDGE | CD_MASK_MPOLY | CD_MASK_MDEFORMVERT;
+  const uint64_t cd_mask_non_generic = CD_MASK_MEDGE | CD_MASK_MDEFORMVERT;
   const uint64_t cd_mask_all_attr = CD_MASK_PROP_ALL | cd_mask_non_generic;
 
   /* The uv selection / pin layers are ignored in the comparisons because
@@ -932,6 +932,22 @@ Mesh *BKE_mesh_add(Main *bmain, const char *name)
   return me;
 }
 
+void BKE_mesh_poly_offsets_ensure(Mesh *mesh)
+{
+  BLI_assert(mesh->poly_offsets_data == nullptr);
+  if (mesh->totpoly == 0) {
+    return;
+  }
+  if (!mesh->poly_offsets_data) {
+    mesh->poly_offsets_data = static_cast<int *>(
+        MEM_malloc_arrayN(mesh->totpoly + 1, sizeof(int), __func__));
+  }
+#ifdef DEBUG
+  mesh->poly_offsets_for_write().fill(-1);
+#endif
+  mesh->poly_offsets_for_write().last() = mesh->totloop;
+}
+
 /* Custom data layer functions; those assume that totXXX are set correctly. */
 static void mesh_ensure_cdlayers_primary(Mesh *mesh, bool do_tessface)
 {
@@ -950,13 +966,6 @@ static void mesh_ensure_cdlayers_primary(Mesh *mesh, bool do_tessface)
     CustomData_add_layer_named(
         &mesh->ldata, CD_PROP_INT32, CD_SET_DEFAULT, nullptr, mesh->totloop, ".corner_edge");
   }
-  if (mesh->totpoly != 0) {
-    if (!mesh->poly_offsets_data) {
-      mesh->poly_offsets_data = static_cast<int *>(
-          MEM_malloc_arrayN(mesh->totpoly + 1, sizeof(int), __func__));
-    }
-    mesh->poly_offsets_for_write().last() = mesh->totloop;
-  }
   if (do_tessface && !CustomData_get_layer(&mesh->fdata, CD_MFACE)) {
     CustomData_add_layer(&mesh->fdata, CD_MFACE, CD_SET_DEFAULT, nullptr, mesh->totface);
   }
@@ -982,12 +991,8 @@ Mesh *BKE_mesh_new_nomain(
   mesh->totloop = loops_len;
   mesh->totpoly = polys_len;
 
-#ifdef DEBUG
-  mesh->poly_offsets_for_write().fill(-1);
-#endif
-  mesh->poly_offsets_for_write().last() == loops_len;
-
   mesh_ensure_cdlayers_primary(mesh, true);
+  BKE_mesh_poly_offsets_ensure(mesh);
 
   return mesh;
 }
@@ -1084,6 +1089,7 @@ Mesh *BK

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list