From 05952aa94d33eeb504fa63618ba35c2bcc8bd19b Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Mon, 5 Sep 2022 11:56:34 -0500 Subject: 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 --- source/blender/blenkernel/intern/DerivedMesh.cc | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'source/blender/blenkernel/intern/DerivedMesh.cc') diff --git a/source/blender/blenkernel/intern/DerivedMesh.cc b/source/blender/blenkernel/intern/DerivedMesh.cc index 7ef6eaa64cd..46a8caafa70 100644 --- a/source/blender/blenkernel/intern/DerivedMesh.cc +++ b/source/blender/blenkernel/intern/DerivedMesh.cc @@ -68,6 +68,8 @@ using blender::float3; using blender::IndexRange; +using blender::Span; +using blender::VArray; /* very slow! enable for testing only! */ //#define USE_MODIFIER_VALIDATE @@ -585,7 +587,6 @@ 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_SET_DEFAULT, nullptr, mesh->totvert); - BKE_mesh_update_customdata_pointers(mesh, false); layerorco = (float(*)[3])CustomData_get_layer(&mesh->vdata, layer); } @@ -2002,9 +2003,9 @@ void mesh_get_mapped_verts_coords(Mesh *me_eval, float (*r_cos)[3], const int to MEM_freeN(userData.vertex_visit); } else { - MVert *mv = me_eval->mvert; - for (int i = 0; i < totcos; i++, mv++) { - copy_v3_v3(r_cos[i], mv->co); + const Span verts = me_eval->vertices(); + for (int i = 0; i < totcos; i++) { + copy_v3_v3(r_cos[i], verts[i].co); } } } @@ -2017,9 +2018,11 @@ static void mesh_init_origspace(Mesh *mesh) CD_ORIGSPACE_MLOOP); const int numpoly = mesh->totpoly; // const int numloop = mesh->totloop; - MVert *mv = mesh->mvert; - MLoop *ml = mesh->mloop; - MPoly *mp = mesh->mpoly; + const Span verts = mesh->vertices(); + const Span polys = mesh->polygons(); + const Span loops = mesh->loops(); + + const MPoly *mp = polys.data(); int i, j, k; blender::Vector vcos_2d; @@ -2033,19 +2036,19 @@ static void mesh_init_origspace(Mesh *mesh) } } else { - MLoop *l = &ml[mp->loopstart]; + const MLoop *l = &loops[mp->loopstart]; float p_nor[3], co[3]; float mat[3][3]; float min[2] = {FLT_MAX, FLT_MAX}, max[2] = {-FLT_MAX, -FLT_MAX}; float translate[2], scale[2]; - BKE_mesh_calc_poly_normal(mp, l, mv, p_nor); + BKE_mesh_calc_poly_normal(mp, l, verts.data(), p_nor); axis_dominant_v3_to_m3(mat, p_nor); vcos_2d.resize(mp->totloop); for (j = 0; j < mp->totloop; j++, l++) { - mul_v3_m3v3(co, mat, mv[l->v].co); + mul_v3_m3v3(co, mat, verts[l->v].co); copy_v2_v2(vcos_2d[j], co); for (k = 0; k < 2; k++) { -- cgit v1.2.3