Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans Goudey <h.goudey@me.com>2022-09-05 19:56:34 +0300
committerHans Goudey <h.goudey@me.com>2022-09-05 19:56:34 +0300
commit05952aa94d33eeb504fa63618ba35c2bcc8bd19b (patch)
treec9ec37adf20c3c37ccaab44869220dcbe8e987a3 /source/blender/blenkernel/intern/mesh.cc
parent63cfc8f9f6d623f33b50c5c07976af2b22845713 (diff)
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes where possible. Mutable pointers like `Mesh.mvert` make that difficult by making ownership vague. They also make code more complex by adding redundancy. The simplest solution is just removing them and retrieving layers from `CustomData` as needed. Similar changes have already been applied to curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of the pointers generally makes code more obvious and more reusable. Mesh data is now accessed with a C++ API (`Mesh::edges()` or `Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`). The CoW changes this commit makes possible are described in T95845 and T95842, and started in D14139 and D14140. The change also simplifies the ongoing mesh struct-of-array refactors from T95965. **RNA/Python Access Performance** Theoretically, accessing mesh elements with the RNA API may become slower, since the layer needs to be found on every random access. However, overhead is already high enough that this doesn't make a noticible differenc, and performance is actually improved in some cases. Random access can be up to 10% faster, but other situations might be a bit slower. Generally using `foreach_get/set` are the best way to improve performance. See the differential revision for more discussion about Python performance. Cycles has been updated to use raw pointers and the internal Blender mesh types, mostly because there is no sense in having this overhead when it's already compiled with Blender. In my tests this roughly halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million face grid). Differential Revision: https://developer.blender.org/D15488
Diffstat (limited to 'source/blender/blenkernel/intern/mesh.cc')
-rw-r--r--source/blender/blenkernel/intern/mesh.cc289
1 files changed, 97 insertions, 192 deletions
diff --git a/source/blender/blenkernel/intern/mesh.cc b/source/blender/blenkernel/intern/mesh.cc
index b44a956eec4..c0379c50de4 100644
--- a/source/blender/blenkernel/intern/mesh.cc
+++ b/source/blender/blenkernel/intern/mesh.cc
@@ -66,6 +66,7 @@
using blender::float3;
using blender::MutableSpan;
+using blender::Span;
using blender::VArray;
using blender::Vector;
@@ -146,8 +147,6 @@ static void mesh_copy_data(Main *bmain, ID *id_dst, const ID *id_src, const int
mesh_tessface_clear_intern(mesh_dst, false);
}
- BKE_mesh_update_customdata_pointers(mesh_dst, do_tessface);
-
mesh_dst->cd_flag = mesh_src->cd_flag;
mesh_dst->edit_mesh = nullptr;
@@ -234,29 +233,32 @@ static void mesh_blend_write(BlendWriter *writer, ID *id, const void *id_address
/* Do not store actual geometry data in case this is a library override ID. */
if (ID_IS_OVERRIDE_LIBRARY(mesh) && !is_undo) {
- mesh->mvert = nullptr;
mesh->totvert = 0;
memset(&mesh->vdata, 0, sizeof(mesh->vdata));
- mesh->medge = nullptr;
mesh->totedge = 0;
memset(&mesh->edata, 0, sizeof(mesh->edata));
- mesh->mloop = nullptr;
mesh->totloop = 0;
memset(&mesh->ldata, 0, sizeof(mesh->ldata));
- mesh->mpoly = nullptr;
mesh->totpoly = 0;
memset(&mesh->pdata, 0, sizeof(mesh->pdata));
}
else {
Set<std::string> names_to_skip;
if (!BLO_write_is_undo(writer)) {
+
BKE_mesh_legacy_convert_hide_layers_to_flags(mesh);
BKE_mesh_legacy_convert_material_indices_to_mpoly(mesh);
/* When converting to the old mesh format, don't save redundant attributes. */
names_to_skip.add_multiple_new({".hide_vert", ".hide_edge", ".hide_poly"});
+
+ /* Set deprecated mesh data pointers for forward compatibility. */
+ mesh->mvert = const_cast<MVert *>(mesh->vertices().data());
+ mesh->medge = const_cast<MEdge *>(mesh->edges().data());
+ mesh->mpoly = const_cast<MPoly *>(mesh->polygons().data());
+ mesh->mloop = const_cast<MLoop *>(mesh->loops().data());
}
CustomData_blend_write_prepare(mesh->vdata, vert_layers, names_to_skip);
@@ -295,17 +297,16 @@ static void mesh_blend_read_data(BlendDataReader *reader, ID *id)
Mesh *mesh = (Mesh *)id;
BLO_read_pointer_array(reader, (void **)&mesh->mat);
+ /* Deprecated pointers to custom data layers are read here for backward compatibility
+ * with files where these were owning pointers rather than a view into custom data. */
BLO_read_data_address(reader, &mesh->mvert);
BLO_read_data_address(reader, &mesh->medge);
BLO_read_data_address(reader, &mesh->mface);
- BLO_read_data_address(reader, &mesh->mloop);
- BLO_read_data_address(reader, &mesh->mpoly);
- BLO_read_data_address(reader, &mesh->tface);
BLO_read_data_address(reader, &mesh->mtface);
- BLO_read_data_address(reader, &mesh->mcol);
BLO_read_data_address(reader, &mesh->dvert);
- BLO_read_data_address(reader, &mesh->mloopcol);
- BLO_read_data_address(reader, &mesh->mloopuv);
+ BLO_read_data_address(reader, &mesh->tface);
+ BLO_read_data_address(reader, &mesh->mcol);
+
BLO_read_data_address(reader, &mesh->mselect);
/* animdata */
@@ -468,6 +469,8 @@ static int customdata_compare(
CD_MASK_MLOOPUV | CD_MASK_PROP_BYTE_COLOR |
CD_MASK_MDEFORMVERT;
const uint64_t cd_mask_all_attr = CD_MASK_PROP_ALL | cd_mask_non_generic;
+ const Span<MLoop> loops_1 = m1->loops();
+ const Span<MLoop> loops_2 = m2->loops();
for (int i = 0; i < c1->totlayer; i++) {
l1 = &c1->layers[i];
@@ -543,15 +546,14 @@ static int customdata_compare(
int ptot = m1->totpoly;
for (j = 0; j < ptot; j++, p1++, p2++) {
- MLoop *lp1, *lp2;
int k;
if (p1->totloop != p2->totloop) {
return MESHCMP_POLYMISMATCH;
}
- lp1 = m1->mloop + p1->loopstart;
- lp2 = m2->mloop + p2->loopstart;
+ const MLoop *lp1 = &loops_1[p1->loopstart];
+ const MLoop *lp2 = &loops_2[p2->loopstart];
for (k = 0; k < p1->totloop; k++, lp1++, lp2++) {
if (lp1->v != lp2->v) {
@@ -762,47 +764,6 @@ const char *BKE_mesh_cmp(Mesh *me1, Mesh *me2, float thresh)
return nullptr;
}
-static void mesh_ensure_tessellation_customdata(Mesh *me)
-{
- if (UNLIKELY((me->totface != 0) && (me->totpoly == 0))) {
- /* Pass, otherwise this function clears 'mface' before
- * versioning 'mface -> mpoly' code kicks in T30583.
- *
- * Callers could also check but safer to do here - campbell */
- }
- else {
- const int tottex_original = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
- const int totcol_original = CustomData_number_of_layers(&me->ldata, CD_PROP_BYTE_COLOR);
-
- const int tottex_tessface = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
- const int totcol_tessface = CustomData_number_of_layers(&me->fdata, CD_MCOL);
-
- if (tottex_tessface != tottex_original || totcol_tessface != totcol_original) {
- BKE_mesh_tessface_clear(me);
-
- BKE_mesh_add_mface_layers(&me->fdata, &me->ldata, me->totface);
-
- /* TODO: add some `--debug-mesh` option. */
- if (G.debug & G_DEBUG) {
- /* NOTE(@campbellbarton): this warning may be un-called for if we are initializing the mesh
- * for the first time from #BMesh, rather than giving a warning about this we could be
- * smarter and check if there was any data to begin with, for now just print the warning
- * with some info to help troubleshoot what's going on. */
- printf(
- "%s: warning! Tessellation uvs or vcol data got out of sync, "
- "had to reset!\n CD_MTFACE: %d != CD_MLOOPUV: %d || CD_MCOL: %d != "
- "CD_PROP_BYTE_COLOR: "
- "%d\n",
- __func__,
- tottex_tessface,
- tottex_original,
- totcol_tessface,
- totcol_original);
- }
- }
- }
-}
-
void BKE_mesh_ensure_skin_customdata(Mesh *me)
{
BMesh *bm = me->edit_mesh ? me->edit_mesh->bm : nullptr;
@@ -874,43 +835,6 @@ bool BKE_mesh_clear_facemap_customdata(struct Mesh *me)
return changed;
}
-/**
- * This ensures grouped custom-data (e.g. #CD_MLOOPUV and #CD_MTFACE, or
- * #CD_PROP_BYTE_COLOR and #CD_MCOL) have the same relative active/render/clone/mask indices.
- *
- * NOTE(@campbellbarton): that for undo mesh data we want to skip 'ensure_tess_cd' call since
- * we don't want to store memory for #MFace data when its only used for older
- * versions of the mesh.
- */
-static void mesh_update_linked_customdata(Mesh *me, const bool do_ensure_tess_cd)
-{
- if (do_ensure_tess_cd) {
- mesh_ensure_tessellation_customdata(me);
- }
-
- CustomData_bmesh_update_active_layers(&me->fdata, &me->ldata);
-}
-
-void BKE_mesh_update_customdata_pointers(Mesh *me, const bool do_ensure_tess_cd)
-{
- mesh_update_linked_customdata(me, do_ensure_tess_cd);
-
- me->mvert = (MVert *)CustomData_get_layer(&me->vdata, CD_MVERT);
- me->dvert = (MDeformVert *)CustomData_get_layer(&me->vdata, CD_MDEFORMVERT);
-
- me->medge = (MEdge *)CustomData_get_layer(&me->edata, CD_MEDGE);
-
- me->mface = (MFace *)CustomData_get_layer(&me->fdata, CD_MFACE);
- me->mcol = (MCol *)CustomData_get_layer(&me->fdata, CD_MCOL);
- me->mtface = (MTFace *)CustomData_get_layer(&me->fdata, CD_MTFACE);
-
- me->mpoly = (MPoly *)CustomData_get_layer(&me->pdata, CD_MPOLY);
- me->mloop = (MLoop *)CustomData_get_layer(&me->ldata, CD_MLOOP);
-
- me->mloopcol = (MLoopCol *)CustomData_get_layer(&me->ldata, CD_PROP_BYTE_COLOR);
- me->mloopuv = (MLoopUV *)CustomData_get_layer(&me->ldata, CD_MLOOPUV);
-}
-
bool BKE_mesh_has_custom_loop_normals(Mesh *me)
{
if (me->edit_mesh) {
@@ -954,8 +878,6 @@ static void mesh_clear_geometry(Mesh *mesh)
mesh->totpoly = 0;
mesh->act_face = -1;
mesh->totselect = 0;
-
- BKE_mesh_update_customdata_pointers(mesh, false);
}
void BKE_mesh_clear_geometry(Mesh *mesh)
@@ -974,9 +896,6 @@ static void mesh_tessface_clear_intern(Mesh *mesh, int free_customdata)
CustomData_reset(&mesh->fdata);
}
- mesh->mface = nullptr;
- mesh->mtface = nullptr;
- mesh->mcol = nullptr;
mesh->totface = 0;
}
@@ -1029,7 +948,6 @@ Mesh *BKE_mesh_new_nomain(
mesh->totpoly = polys_len;
mesh_ensure_cdlayers_primary(mesh, true);
- BKE_mesh_update_customdata_pointers(mesh, false);
return mesh;
}
@@ -1115,7 +1033,6 @@ Mesh *BKE_mesh_new_nomain_from_template_ex(const Mesh *me_src,
/* The destination mesh should at least have valid primary CD layers,
* even in cases where the source mesh does not. */
mesh_ensure_cdlayers_primary(me_dst, do_tessface);
- BKE_mesh_update_customdata_pointers(me_dst, false);
/* Expect that normals aren't copied at all, since the destination mesh is new. */
BLI_assert(BKE_mesh_vertex_normals_are_dirty(me_dst));
@@ -1338,11 +1255,12 @@ float (*BKE_mesh_orco_verts_get(Object *ob))[3]
/* Get appropriate vertex coordinates */
float(*vcos)[3] = (float(*)[3])MEM_calloc_arrayN(me->totvert, sizeof(*vcos), "orco mesh");
- MVert *mvert = tme->mvert;
+ const Span<MVert> verts = tme->vertices();
+
int totvert = min_ii(tme->totvert, me->totvert);
- for (int a = 0; a < totvert; a++, mvert++) {
- copy_v3_v3(vcos[a], mvert->co);
+ for (int a = 0; a < totvert; a++) {
+ copy_v3_v3(vcos[a], verts[a].co);
}
return vcos;
@@ -1512,14 +1430,15 @@ void BKE_mesh_material_remap(Mesh *me, const uint *remap, uint remap_len)
void BKE_mesh_smooth_flag_set(Mesh *me, const bool use_smooth)
{
+ MutableSpan<MPoly> polys = me->polygons_for_write();
if (use_smooth) {
- for (int i = 0; i < me->totpoly; i++) {
- me->mpoly[i].flag |= ME_SMOOTH;
+ for (MPoly &poly : polys) {
+ poly.flag |= ME_SMOOTH;
}
}
else {
- for (int i = 0; i < me->totpoly; i++) {
- me->mpoly[i].flag &= ~ME_SMOOTH;
+ for (MPoly &poly : polys) {
+ poly.flag &= ~ME_SMOOTH;
}
}
}
@@ -1575,9 +1494,12 @@ int BKE_mesh_edge_other_vert(const MEdge *e, int v)
void BKE_mesh_looptri_get_real_edges(const Mesh *mesh, const MLoopTri *looptri, int r_edges[3])
{
+ const Span<MEdge> edges = mesh->edges();
+ const Span<MLoop> loops = mesh->loops();
+
for (int i = 2, i_next = 0; i_next < 3; i = i_next++) {
- const MLoop *l1 = &mesh->mloop[looptri->tri[i]], *l2 = &mesh->mloop[looptri->tri[i_next]];
- const MEdge *e = &mesh->medge[l1->e];
+ const MLoop *l1 = &loops[looptri->tri[i]], *l2 = &loops[looptri->tri[i_next]];
+ const MEdge *e = &edges[l1->e];
bool is_real = (l1->v == e->v1 && l2->v == e->v2) || (l1->v == e->v2 && l2->v == e->v1);
@@ -1596,15 +1518,16 @@ bool BKE_mesh_minmax(const Mesh *me, float r_min[3], float r_max[3])
float3 min;
float3 max;
};
+ const Span<MVert> verts = me->vertices();
const Result minmax = threading::parallel_reduce(
- IndexRange(me->totvert),
+ verts.index_range(),
1024,
Result{float3(FLT_MAX), float3(-FLT_MAX)},
- [&](IndexRange range, const Result &init) {
+ [verts](IndexRange range, const Result &init) {
Result result = init;
for (const int i : range) {
- math::min_max(float3(me->mvert[i].co), result.min, result.max);
+ math::min_max(float3(verts[i].co), result.min, result.max);
}
return result;
},
@@ -1620,22 +1543,16 @@ bool BKE_mesh_minmax(const Mesh *me, float r_min[3], float r_max[3])
void BKE_mesh_transform(Mesh *me, const float mat[4][4], bool do_keys)
{
- int i;
- MVert *mvert = (MVert *)CustomData_duplicate_referenced_layer(&me->vdata, CD_MVERT, me->totvert);
- float(*lnors)[3] = (float(*)[3])CustomData_duplicate_referenced_layer(
- &me->ldata, CD_NORMAL, me->totloop);
-
- /* If the referenced layer has been re-allocated need to update pointers stored in the mesh. */
- BKE_mesh_update_customdata_pointers(me, false);
+ MutableSpan<MVert> verts = me->vertices_for_write();
- for (i = 0; i < me->totvert; i++, mvert++) {
- mul_m4_v3(mat, mvert->co);
+ for (MVert &vert : verts) {
+ mul_m4_v3(mat, vert.co);
}
if (do_keys && me->key) {
LISTBASE_FOREACH (KeyBlock *, kb, &me->key->block) {
float *fp = (float *)kb->data;
- for (i = kb->totelem; i--; fp += 3) {
+ for (int i = kb->totelem; i--; fp += 3) {
mul_m4_v3(mat, fp);
}
}
@@ -1644,12 +1561,14 @@ void BKE_mesh_transform(Mesh *me, const float mat[4][4], bool do_keys)
/* don't update normals, caller can do this explicitly.
* We do update loop normals though, those may not be auto-generated
* (see e.g. STL import script)! */
+ float(*lnors)[3] = (float(*)[3])CustomData_duplicate_referenced_layer(
+ &me->ldata, CD_NORMAL, me->totloop);
if (lnors) {
float m3[3][3];
copy_m3_m4(m3, mat);
normalize_m3(m3);
- for (i = 0; i < me->totloop; i++, lnors++) {
+ for (int i = 0; i < me->totloop; i++, lnors++) {
mul_m3_v3(m3, *lnors);
}
}
@@ -1658,15 +1577,12 @@ void BKE_mesh_transform(Mesh *me, const float mat[4][4], bool do_keys)
void BKE_mesh_translate(Mesh *me, const float offset[3], const bool do_keys)
{
- CustomData_duplicate_referenced_layer(&me->vdata, CD_MVERT, me->totvert);
- /* If the referenced layer has been re-allocated need to update pointers stored in the mesh. */
- BKE_mesh_update_customdata_pointers(me, false);
-
- int i = me->totvert;
- for (MVert *mvert = me->mvert; i--; mvert++) {
- add_v3_v3(mvert->co, offset);
+ MutableSpan<MVert> verts = me->vertices_for_write();
+ for (MVert &vert : verts) {
+ add_v3_v3(vert.co, offset);
}
+ int i;
if (do_keys && me->key) {
LISTBASE_FOREACH (KeyBlock *, kb, &me->key->block) {
float *fp = (float *)kb->data;
@@ -1689,25 +1605,24 @@ void BKE_mesh_do_versions_cd_flag_init(Mesh *mesh)
return;
}
- MVert *mv;
- MEdge *med;
- int i;
+ const Span<MVert> verts = mesh->vertices();
+ const Span<MEdge> edges = mesh->edges();
- for (mv = mesh->mvert, i = 0; i < mesh->totvert; mv++, i++) {
- if (mv->bweight != 0) {
+ for (const MVert &vert : verts) {
+ if (vert.bweight != 0) {
mesh->cd_flag |= ME_CDFLAG_VERT_BWEIGHT;
break;
}
}
- for (med = mesh->medge, i = 0; i < mesh->totedge; med++, i++) {
- if (med->bweight != 0) {
+ for (const MEdge &edge : edges) {
+ if (edge.bweight != 0) {
mesh->cd_flag |= ME_CDFLAG_EDGE_BWEIGHT;
if (mesh->cd_flag & ME_CDFLAG_EDGE_CREASE) {
break;
}
}
- if (med->crease != 0) {
+ if (edge.crease != 0) {
mesh->cd_flag |= ME_CDFLAG_EDGE_CREASE;
if (mesh->cd_flag & ME_CDFLAG_EDGE_BWEIGHT) {
break;
@@ -1733,6 +1648,9 @@ void BKE_mesh_mselect_validate(Mesh *me)
if (me->totselect == 0) {
return;
}
+ const Span<MVert> verts = me->vertices();
+ const Span<MEdge> edges = me->edges();
+ const Span<MPoly> polys = me->polygons();
mselect_src = me->mselect;
mselect_dst = (MSelect *)MEM_malloc_arrayN(
@@ -1742,21 +1660,21 @@ void BKE_mesh_mselect_validate(Mesh *me)
int index = mselect_src[i_src].index;
switch (mselect_src[i_src].type) {
case ME_VSEL: {
- if (me->mvert[index].flag & SELECT) {
+ if (verts[index].flag & SELECT) {
mselect_dst[i_dst] = mselect_src[i_src];
i_dst++;
}
break;
}
case ME_ESEL: {
- if (me->medge[index].flag & SELECT) {
+ if (edges[index].flag & SELECT) {
mselect_dst[i_dst] = mselect_src[i_src];
i_dst++;
}
break;
}
case ME_FSEL: {
- if (me->mpoly[index].flag & SELECT) {
+ if (polys[index].flag & SELECT) {
mselect_dst[i_dst] = mselect_src[i_src];
i_dst++;
}
@@ -1842,10 +1760,10 @@ void BKE_mesh_count_selected_items(const Mesh *mesh, int r_count[3])
void BKE_mesh_vert_coords_get(const Mesh *mesh, float (*vert_coords)[3])
{
- const MVert *mv = mesh->mvert;
- for (int i = 0; i < mesh->totvert; i++, mv++) {
- copy_v3_v3(vert_coords[i], mv->co);
- }
+ blender::bke::AttributeAccessor attributes = blender::bke::mesh_attributes(*mesh);
+ VArray<float3> positions = attributes.lookup_or_default(
+ "position", ATTR_DOMAIN_POINT, float3(0));
+ positions.materialize({(float3 *)vert_coords, mesh->totvert});
}
float (*BKE_mesh_vert_coords_alloc(const Mesh *mesh, int *r_vert_len))[3]
@@ -1860,12 +1778,9 @@ float (*BKE_mesh_vert_coords_alloc(const Mesh *mesh, int *r_vert_len))[3]
void BKE_mesh_vert_coords_apply(Mesh *mesh, const float (*vert_coords)[3])
{
- /* This will just return the pointer if it wasn't a referenced layer. */
- MVert *mv = (MVert *)CustomData_duplicate_referenced_layer(
- &mesh->vdata, CD_MVERT, mesh->totvert);
- mesh->mvert = mv;
- for (int i = 0; i < mesh->totvert; i++, mv++) {
- copy_v3_v3(mv->co, vert_coords[i]);
+ MutableSpan<MVert> verts = mesh->vertices_for_write();
+ for (const int i : verts.index_range()) {
+ copy_v3_v3(verts[i].co, vert_coords[i]);
}
BKE_mesh_tag_coords_changed(mesh);
}
@@ -1874,12 +1789,9 @@ void BKE_mesh_vert_coords_apply_with_mat4(Mesh *mesh,
const float (*vert_coords)[3],
const float mat[4][4])
{
- /* This will just return the pointer if it wasn't a referenced layer. */
- MVert *mv = (MVert *)CustomData_duplicate_referenced_layer(
- &mesh->vdata, CD_MVERT, mesh->totvert);
- mesh->mvert = mv;
- for (int i = 0; i < mesh->totvert; i++, mv++) {
- mul_v3_m4v3(mv->co, mat, vert_coords[i]);
+ MutableSpan<MVert> verts = mesh->vertices_for_write();
+ for (const int i : verts.index_range()) {
+ mul_v3_m4v3(verts[i].co, mat, vert_coords[i]);
}
BKE_mesh_tag_coords_changed(mesh);
}
@@ -1915,17 +1827,22 @@ void BKE_mesh_calc_normals_split_ex(Mesh *mesh,
/* may be nullptr */
clnors = (short(*)[2])CustomData_get_layer(&mesh->ldata, CD_CUSTOMLOOPNORMAL);
- BKE_mesh_normals_loop_split(mesh->mvert,
+ const Span<MVert> verts = mesh->vertices();
+ const Span<MEdge> edges = mesh->edges();
+ const Span<MPoly> polys = mesh->polygons();
+ const Span<MLoop> loops = mesh->loops();
+
+ BKE_mesh_normals_loop_split(verts.data(),
BKE_mesh_vertex_normals_ensure(mesh),
- mesh->totvert,
- mesh->medge,
- mesh->totedge,
- mesh->mloop,
+ verts.size(),
+ edges.data(),
+ edges.size(),
+ loops.data(),
r_corner_normals,
- mesh->totloop,
- mesh->mpoly,
+ loops.size(),
+ polys.data(),
BKE_mesh_poly_normals_ensure(mesh),
- mesh->totpoly,
+ polys.size(),
use_split_normals,
split_angle,
r_lnors_spacearr,
@@ -1971,14 +1888,14 @@ static int split_faces_prepare_new_verts(Mesh *mesh,
const int loops_len = mesh->totloop;
int verts_len = mesh->totvert;
- MLoop *mloop = mesh->mloop;
+ MutableSpan<MLoop> loops = mesh->loops_for_write();
BKE_mesh_vertex_normals_ensure(mesh);
float(*vert_normals)[3] = BKE_mesh_vertex_normals_for_write(mesh);
BLI_bitmap *verts_used = BLI_BITMAP_NEW(verts_len, __func__);
BLI_bitmap *done_loops = BLI_BITMAP_NEW(loops_len, __func__);
- MLoop *ml = mloop;
+ MLoop *ml = loops.data();
MLoopNorSpace **lnor_space = lnors_spacearr->lspacearr;
BLI_assert(lnors_spacearr->data_type == MLNOR_SPACEARR_LOOP_INDEX);
@@ -2005,7 +1922,7 @@ static int split_faces_prepare_new_verts(Mesh *mesh,
const int ml_fan_idx = POINTER_AS_INT(lnode->link);
BLI_BITMAP_ENABLE(done_loops, ml_fan_idx);
if (vert_used) {
- mloop[ml_fan_idx].v = new_vert_idx;
+ loops[ml_fan_idx].v = new_vert_idx;
}
}
}
@@ -2040,23 +1957,23 @@ static int split_faces_prepare_new_verts(Mesh *mesh,
/* Detect needed new edges, and update accordingly loops' edge indices.
* WARNING! Leaves mesh in invalid state. */
-static int split_faces_prepare_new_edges(const Mesh *mesh,
+static int split_faces_prepare_new_edges(Mesh *mesh,
SplitFaceNewEdge **new_edges,
MemArena *memarena)
{
const int num_polys = mesh->totpoly;
int num_edges = mesh->totedge;
- MEdge *medge = mesh->medge;
- MLoop *mloop = mesh->mloop;
- const MPoly *mpoly = mesh->mpoly;
+ MutableSpan<MEdge> edges = mesh->edges_for_write();
+ MutableSpan<MLoop> loops = mesh->loops_for_write();
+ const Span<MPoly> polys = mesh->polygons();
BLI_bitmap *edges_used = BLI_BITMAP_NEW(num_edges, __func__);
EdgeHash *edges_hash = BLI_edgehash_new_ex(__func__, num_edges);
- const MPoly *mp = mpoly;
+ const MPoly *mp = polys.data();
for (int poly_idx = 0; poly_idx < num_polys; poly_idx++, mp++) {
- MLoop *ml_prev = &mloop[mp->loopstart + mp->totloop - 1];
- MLoop *ml = &mloop[mp->loopstart];
+ MLoop *ml_prev = &loops[mp->loopstart + mp->totloop - 1];
+ MLoop *ml = &loops[mp->loopstart];
for (int loop_idx = 0; loop_idx < mp->totloop; loop_idx++, ml++) {
void **eval;
if (!BLI_edgehash_ensure_p(edges_hash, ml_prev->v, ml->v, &eval)) {
@@ -2080,8 +1997,8 @@ static int split_faces_prepare_new_edges(const Mesh *mesh,
}
else {
/* We can re-use original edge. */
- medge[edge_idx].v1 = ml_prev->v;
- medge[edge_idx].v2 = ml->v;
+ edges[edge_idx].v1 = ml_prev->v;
+ edges[edge_idx].v2 = ml->v;
*eval = POINTER_FROM_INT(edge_idx);
BLI_BITMAP_ENABLE(edges_used, edge_idx);
}
@@ -2107,7 +2024,6 @@ static void split_faces_split_new_verts(Mesh *mesh,
const int num_new_verts)
{
const int verts_len = mesh->totvert - num_new_verts;
- MVert *mvert = mesh->mvert;
float(*vert_normals)[3] = BKE_mesh_vertex_normals_for_write(mesh);
/* Normals were already calculated at the beginning of this operation, we rely on that to update
@@ -2115,8 +2031,7 @@ static void split_faces_split_new_verts(Mesh *mesh,
BLI_assert(!BKE_mesh_vertex_normals_are_dirty(mesh));
/* Remember new_verts is a single linklist, so its items are in reversed order... */
- MVert *new_mv = &mvert[mesh->totvert - 1];
- for (int i = mesh->totvert - 1; i >= verts_len; i--, new_mv--, new_verts = new_verts->next) {
+ for (int i = mesh->totvert - 1; i >= verts_len; i--, new_verts = new_verts->next) {
BLI_assert(new_verts->new_index == i);
BLI_assert(new_verts->new_index != new_verts->orig_index);
CustomData_copy_data(&mesh->vdata, &mesh->vdata, new_verts->orig_index, i, 1);
@@ -2132,10 +2047,10 @@ static void split_faces_split_new_edges(Mesh *mesh,
const int num_new_edges)
{
const int num_edges = mesh->totedge - num_new_edges;
- MEdge *medge = mesh->medge;
+ MutableSpan<MEdge> edges = mesh->edges_for_write();
/* Remember new_edges is a single linklist, so its items are in reversed order... */
- MEdge *new_med = &medge[mesh->totedge - 1];
+ MEdge *new_med = &edges[mesh->totedge - 1];
for (int i = mesh->totedge - 1; i >= num_edges; i--, new_med--, new_edges = new_edges->next) {
BLI_assert(new_edges->new_index == i);
BLI_assert(new_edges->new_index != new_edges->orig_index);
@@ -2163,14 +2078,6 @@ void BKE_mesh_split_faces(Mesh *mesh, bool free_loop_normals)
SplitFaceNewVert *new_verts = nullptr;
SplitFaceNewEdge *new_edges = nullptr;
- /* Ensure we own the layers, we need to do this before split_faces_prepare_new_verts as it will
- * directly assign new indices to existing edges and loops. */
- CustomData_duplicate_referenced_layers(&mesh->vdata, mesh->totvert);
- CustomData_duplicate_referenced_layers(&mesh->edata, mesh->totedge);
- CustomData_duplicate_referenced_layers(&mesh->ldata, mesh->totloop);
- /* Update pointers in case we duplicated referenced layers. */
- BKE_mesh_update_customdata_pointers(mesh, false);
-
/* Detect loop normal spaces (a.k.a. smooth fans) that will need a new vert. */
const int num_new_verts = split_faces_prepare_new_verts(
mesh, &lnors_spacearr, &new_verts, memarena);
@@ -2191,8 +2098,6 @@ void BKE_mesh_split_faces(Mesh *mesh, bool free_loop_normals)
mesh->totedge += num_new_edges;
CustomData_realloc(&mesh->edata, mesh->totedge);
}
- /* Update pointers to a newly allocated memory. */
- BKE_mesh_update_customdata_pointers(mesh, false);
/* Update normals manually to avoid recalculation after this operation. */
mesh->runtime.vert_normals = (float(*)[3])MEM_reallocN(mesh->runtime.vert_normals,