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/modifiers/intern/MOD_remesh.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'source/blender/modifiers/intern/MOD_remesh.c') diff --git a/source/blender/modifiers/intern/MOD_remesh.c b/source/blender/modifiers/intern/MOD_remesh.c index f21d536fadf..37d711a4bfa 100644 --- a/source/blender/modifiers/intern/MOD_remesh.c +++ b/source/blender/modifiers/intern/MOD_remesh.c @@ -60,11 +60,11 @@ static void init_dualcon_mesh(DualConInput *input, Mesh *mesh) { memset(input, 0, sizeof(DualConInput)); - input->co = (void *)mesh->mvert; + input->co = (void *)BKE_mesh_vertices(mesh); input->co_stride = sizeof(MVert); input->totco = mesh->totvert; - input->mloop = (void *)mesh->mloop; + input->mloop = (void *)BKE_mesh_loops(mesh); input->loop_stride = sizeof(MLoop); BKE_mesh_runtime_looptri_ensure(mesh); @@ -80,6 +80,9 @@ static void init_dualcon_mesh(DualConInput *input, Mesh *mesh) * keep track of the current elements */ typedef struct { Mesh *mesh; + MVert *verts; + MPoly *polys; + MLoop *loops; int curvert, curface; } DualConOutput; @@ -93,17 +96,20 @@ static void *dualcon_alloc_output(int totvert, int totquad) } output->mesh = BKE_mesh_new_nomain(totvert, 0, 0, 4 * totquad, totquad); + output->verts = BKE_mesh_vertices_for_write(output->mesh); + output->polys = BKE_mesh_polygons_for_write(output->mesh); + output->loops = BKE_mesh_loops_for_write(output->mesh); + return output; } static void dualcon_add_vert(void *output_v, const float co[3]) { DualConOutput *output = output_v; - Mesh *mesh = output->mesh; - BLI_assert(output->curvert < mesh->totvert); + BLI_assert(output->curvert < output->mesh->totvert); - copy_v3_v3(mesh->mvert[output->curvert].co, co); + copy_v3_v3(output->verts[output->curvert].co, co); output->curvert++; } @@ -111,14 +117,13 @@ static void dualcon_add_quad(void *output_v, const int vert_indices[4]) { DualConOutput *output = output_v; Mesh *mesh = output->mesh; - MLoop *mloop; - MPoly *cur_poly; int i; BLI_assert(output->curface < mesh->totpoly); + UNUSED_VARS_NDEBUG(mesh); - mloop = mesh->mloop; - cur_poly = &mesh->mpoly[output->curface]; + MLoop *mloop = output->loops; + MPoly *cur_poly = &output->polys[output->curface]; cur_poly->loopstart = output->curface * 4; cur_poly->totloop = 4; @@ -195,7 +200,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *UNUSED(ctx) } if (rmd->flag & MOD_REMESH_SMOOTH_SHADING) { - MPoly *mpoly = result->mpoly; + MPoly *mpoly = BKE_mesh_polygons_for_write(result); int i, totpoly = result->totpoly; /* Apply smooth shading to output faces */ -- cgit v1.2.3