diff options
author | Hans Goudey <h.goudey@me.com> | 2022-09-05 19:56:34 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-09-05 19:56:34 +0300 |
commit | 05952aa94d33eeb504fa63618ba35c2bcc8bd19b (patch) | |
tree | c9ec37adf20c3c37ccaab44869220dcbe8e987a3 /source/blender/blenkernel/intern/mesh_validate.cc | |
parent | 63cfc8f9f6d623f33b50c5c07976af2b22845713 (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_validate.cc')
-rw-r--r-- | source/blender/blenkernel/intern/mesh_validate.cc | 141 |
1 files changed, 79 insertions, 62 deletions
diff --git a/source/blender/blenkernel/intern/mesh_validate.cc b/source/blender/blenkernel/intern/mesh_validate.cc index c30f3661103..dafc2384282 100644 --- a/source/blender/blenkernel/intern/mesh_validate.cc +++ b/source/blender/blenkernel/intern/mesh_validate.cc @@ -33,6 +33,9 @@ #include "MEM_guardedalloc.h" +using blender::MutableSpan; +using blender::Span; + /* loop v/e are unsigned, so using max uint_32 value as invalid marker... */ #define INVALID_LOOP_EDGE_MARKER 4294967295u @@ -1064,19 +1067,23 @@ bool BKE_mesh_validate(Mesh *me, const bool do_verbose, const bool cddata_check_ do_verbose, true, &changed); + MutableSpan<MVert> verts = me->vertices_for_write(); + MutableSpan<MEdge> edges = me->edges_for_write(); + MutableSpan<MPoly> polys = me->polygons_for_write(); + MutableSpan<MLoop> loops = me->loops_for_write(); BKE_mesh_validate_arrays(me, - me->mvert, - me->totvert, - me->medge, - me->totedge, - me->mface, + verts.data(), + verts.size(), + edges.data(), + edges.size(), + (MFace *)CustomData_get_layer(&me->fdata, CD_MFACE), me->totface, - me->mloop, - me->totloop, - me->mpoly, - me->totpoly, - me->dvert, + loops.data(), + loops.size(), + polys.data(), + polys.size(), + me->deform_verts_for_write().data(), do_verbose, true, &changed); @@ -1113,18 +1120,23 @@ bool BKE_mesh_is_valid(Mesh *me) do_fixes, &changed); + MutableSpan<MVert> verts = me->vertices_for_write(); + MutableSpan<MEdge> edges = me->edges_for_write(); + MutableSpan<MPoly> polys = me->polygons_for_write(); + MutableSpan<MLoop> loops = me->loops_for_write(); + is_valid &= BKE_mesh_validate_arrays(me, - me->mvert, - me->totvert, - me->medge, - me->totedge, - me->mface, + verts.data(), + verts.size(), + edges.data(), + edges.size(), + (MFace *)CustomData_get_layer(&me->fdata, CD_MFACE), me->totface, - me->mloop, - me->totloop, - me->mpoly, - me->totpoly, - me->dvert, + loops.data(), + loops.size(), + polys.data(), + polys.size(), + me->deform_verts_for_write().data(), do_verbose, do_fixes, &changed); @@ -1170,11 +1182,12 @@ void BKE_mesh_strip_loose_faces(Mesh *me) /* NOTE: We need to keep this for edge creation (for now?), and some old `readfile.c` code. */ MFace *f; int a, b; + MFace *mfaces = (MFace *)CustomData_get_layer(&me->fdata, CD_MFACE); - for (a = b = 0, f = me->mface; a < me->totface; a++, f++) { + for (a = b = 0, f = mfaces; a < me->totface; a++, f++) { if (f->v3) { if (a != b) { - memcpy(&me->mface[b], f, sizeof(me->mface[b])); + memcpy(&mfaces[b], f, sizeof(mfaces[b])); CustomData_copy_data(&me->fdata, &me->fdata, a, b, 1); } b++; @@ -1188,13 +1201,16 @@ void BKE_mesh_strip_loose_faces(Mesh *me) void BKE_mesh_strip_loose_polysloops(Mesh *me) { + MutableSpan<MPoly> polys = me->polygons_for_write(); + MutableSpan<MLoop> loops = me->loops_for_write(); + MPoly *p; MLoop *l; int a, b; /* New loops idx! */ int *new_idx = (int *)MEM_mallocN(sizeof(int) * me->totloop, __func__); - for (a = b = 0, p = me->mpoly; a < me->totpoly; a++, p++) { + for (a = b = 0, p = polys.data(); a < me->totpoly; a++, p++) { bool invalid = false; int i = p->loopstart; int stop = i + p->totloop; @@ -1203,7 +1219,7 @@ void BKE_mesh_strip_loose_polysloops(Mesh *me) invalid = true; } else { - l = &me->mloop[i]; + l = &loops[i]; i = stop - i; /* If one of the poly's loops is invalid, the whole poly is invalid! */ for (; i--; l++) { @@ -1216,7 +1232,7 @@ void BKE_mesh_strip_loose_polysloops(Mesh *me) if (p->totloop >= 3 && !invalid) { if (a != b) { - memcpy(&me->mpoly[b], p, sizeof(me->mpoly[b])); + memcpy(&polys[b], p, sizeof(polys[b])); CustomData_copy_data(&me->pdata, &me->pdata, a, b, 1); } b++; @@ -1228,10 +1244,10 @@ void BKE_mesh_strip_loose_polysloops(Mesh *me) } /* And now, get rid of invalid loops. */ - for (a = b = 0, l = me->mloop; a < me->totloop; a++, l++) { + for (a = b = 0, l = loops.data(); a < me->totloop; a++, l++) { if (l->e != INVALID_LOOP_EDGE_MARKER) { if (a != b) { - memcpy(&me->mloop[b], l, sizeof(me->mloop[b])); + memcpy(&loops[b], l, sizeof(loops[b])); CustomData_copy_data(&me->ldata, &me->ldata, a, b, 1); } new_idx[a] = b; @@ -1250,8 +1266,8 @@ void BKE_mesh_strip_loose_polysloops(Mesh *me) /* And now, update polys' start loop index. */ /* NOTE: At this point, there should never be any poly using a striped loop! */ - for (a = 0, p = me->mpoly; a < me->totpoly; a++, p++) { - p->loopstart = new_idx[p->loopstart]; + for (const int i : polys.index_range()) { + polys[i].loopstart = new_idx[polys[i].loopstart]; } MEM_freeN(new_idx); @@ -1260,14 +1276,14 @@ void BKE_mesh_strip_loose_polysloops(Mesh *me) void BKE_mesh_strip_loose_edges(Mesh *me) { MEdge *e; - MLoop *l; int a, b; uint *new_idx = (uint *)MEM_mallocN(sizeof(int) * me->totedge, __func__); + MutableSpan<MEdge> edges = me->edges_for_write(); - for (a = b = 0, e = me->medge; a < me->totedge; a++, e++) { + for (a = b = 0, e = edges.data(); a < me->totedge; a++, e++) { if (e->v1 != e->v2) { if (a != b) { - memcpy(&me->medge[b], e, sizeof(me->medge[b])); + memcpy(&edges[b], e, sizeof(edges[b])); CustomData_copy_data(&me->edata, &me->edata, a, b, 1); } new_idx[a] = b; @@ -1285,8 +1301,9 @@ void BKE_mesh_strip_loose_edges(Mesh *me) /* And now, update loops' edge indices. */ /* XXX We hope no loop was pointing to a striped edge! * Else, its e will be set to INVALID_LOOP_EDGE_MARKER :/ */ - for (a = 0, l = me->mloop; a < me->totloop; a++, l++) { - l->e = new_idx[l->e]; + MutableSpan<MLoop> loops = me->loops_for_write(); + for (MLoop &loop : loops) { + loop.e = new_idx[loop.e]; } MEM_freeN(new_idx); @@ -1344,10 +1361,10 @@ static int vergedgesort(const void *v1, const void *v2) /* Create edges based on known verts and faces, * this function is only used when loading very old blend files */ -static void mesh_calc_edges_mdata(MVert *UNUSED(allvert), - MFace *allface, +static void mesh_calc_edges_mdata(const MVert *UNUSED(allvert), + const MFace *allface, MLoop *allloop, - MPoly *allpoly, + const MPoly *allpoly, int UNUSED(totvert), int totface, int UNUSED(totloop), @@ -1356,8 +1373,8 @@ static void mesh_calc_edges_mdata(MVert *UNUSED(allvert), MEdge **r_medge, int *r_totedge) { - MPoly *mpoly; - MFace *mface; + const MPoly *mpoly; + const MFace *mface; MEdge *medge, *med; EdgeHash *hash; struct EdgeSort *edsort, *ed; @@ -1480,28 +1497,29 @@ void BKE_mesh_calc_edges_legacy(Mesh *me, const bool use_old) { MEdge *medge; int totedge = 0; - - mesh_calc_edges_mdata(me->mvert, - me->mface, - me->mloop, - me->mpoly, - me->totvert, + const Span<MVert> verts = me->vertices(); + const Span<MPoly> polys = me->polygons(); + MutableSpan<MLoop> loops = me->loops_for_write(); + + mesh_calc_edges_mdata(verts.data(), + (MFace *)CustomData_get_layer(&me->fdata, CD_MFACE), + loops.data(), + polys.data(), + verts.size(), me->totface, - me->totloop, - me->totpoly, + loops.size(), + polys.size(), use_old, &medge, &totedge); if (totedge == 0) { /* flag that mesh has edges */ - me->medge = medge; me->totedge = 0; return; } medge = (MEdge *)CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, medge, totedge); - me->medge = medge; me->totedge = totedge; BKE_mesh_strip_loose_faces(me); @@ -1509,18 +1527,18 @@ void BKE_mesh_calc_edges_legacy(Mesh *me, const bool use_old) void BKE_mesh_calc_edges_loose(Mesh *mesh) { - MEdge *med = mesh->medge; - for (int i = 0; i < mesh->totedge; i++, med++) { - med->flag |= ME_LOOSEEDGE; + const Span<MLoop> loops = mesh->loops(); + MutableSpan<MEdge> edges = mesh->edges_for_write(); + + for (const int i : edges.index_range()) { + edges[i].flag |= ME_LOOSEEDGE; } - MLoop *ml = mesh->mloop; - for (int i = 0; i < mesh->totloop; i++, ml++) { - mesh->medge[ml->e].flag &= ~ME_LOOSEEDGE; + for (const int i : loops.index_range()) { + edges[loops[i].e].flag &= ~ME_LOOSEEDGE; } - med = mesh->medge; - for (int i = 0; i < mesh->totedge; i++, med++) { - if (med->flag & ME_LOOSEEDGE) { - med->flag |= ME_EDGEDRAW; + for (const int i : edges.index_range()) { + if (edges[i].flag & ME_LOOSEEDGE) { + edges[i].flag |= ME_EDGEDRAW; } } } @@ -1529,8 +1547,9 @@ void BKE_mesh_calc_edges_tessface(Mesh *mesh) { const int numFaces = mesh->totface; EdgeSet *eh = BLI_edgeset_new_ex(__func__, BLI_EDGEHASH_SIZE_GUESS_FROM_POLYS(numFaces)); + MFace *mfaces = (MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE); - MFace *mf = mesh->mface; + MFace *mf = mfaces; for (int i = 0; i < numFaces; i++, mf++) { BLI_edgeset_add(eh, mf->v1, mf->v2); BLI_edgeset_add(eh, mf->v2, mf->v3); @@ -1570,8 +1589,6 @@ void BKE_mesh_calc_edges_tessface(Mesh *mesh) mesh->edata = edgeData; mesh->totedge = numEdges; - mesh->medge = (MEdge *)CustomData_get_layer(&mesh->edata, CD_MEDGE); - BLI_edgeset_free(eh); } |