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_convert.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_convert.cc')
-rw-r--r-- | source/blender/blenkernel/intern/mesh_convert.cc | 100 |
1 files changed, 47 insertions, 53 deletions
diff --git a/source/blender/blenkernel/intern/mesh_convert.cc b/source/blender/blenkernel/intern/mesh_convert.cc index 393d54bb03e..e56c248e81a 100644 --- a/source/blender/blenkernel/intern/mesh_convert.cc +++ b/source/blender/blenkernel/intern/mesh_convert.cc @@ -81,8 +81,8 @@ static void make_edges_mdata_extend(Mesh &mesh) const MPoly *mp; int i; - Span<MPoly> polys(mesh.mpoly, mesh.totpoly); - MutableSpan<MLoop> loops(mesh.mloop, mesh.totloop); + const Span<MPoly> polys = mesh.polygons(); + MutableSpan<MLoop> loops = mesh.loops_for_write(); const int eh_reserve = max_ii(totedge, BLI_EDGEHASH_SIZE_GUESS_FROM_POLYS(mesh.totpoly)); EdgeHash *eh = BLI_edgehash_new_ex(__func__, eh_reserve); @@ -96,20 +96,18 @@ static void make_edges_mdata_extend(Mesh &mesh) #ifdef DEBUG /* ensure that there's no overlap! */ if (totedge_new) { - MEdge *medge = mesh.medge; - for (i = 0; i < totedge; i++, medge++) { - BLI_assert(BLI_edgehash_haskey(eh, medge->v1, medge->v2) == false); + for (const MEdge &edge : mesh.edges()) { + BLI_assert(BLI_edgehash_haskey(eh, edge.v1, edge.v2) == false); } } #endif if (totedge_new) { CustomData_realloc(&mesh.edata, totedge + totedge_new); - BKE_mesh_update_customdata_pointers(&mesh, false); - - MEdge *medge = mesh.medge + totedge; mesh.totedge += totedge_new; + MutableSpan<MEdge> edges = mesh.edges_for_write(); + MEdge *medge = &edges[totedge]; EdgeHashIterator *ehi; uint e_index = totedge; @@ -123,7 +121,7 @@ static void make_edges_mdata_extend(Mesh &mesh) } BLI_edgehashIterator_free(ehi); - for (i = 0, mp = mesh.mpoly; i < mesh.totpoly; i++, mp++) { + for (i = 0, mp = polys.data(); i < mesh.totpoly; i++, mp++) { MLoop *l = &loops[mp->loopstart]; MLoop *l_prev = (l + (mp->totloop - 1)); int j; @@ -186,10 +184,10 @@ static Mesh *mesh_nurbs_displist_to_mesh(const Curve *cu, const ListBase *dispba } Mesh *mesh = BKE_mesh_new_nomain(totvert, totedge, 0, totloop, totpoly); - MutableSpan<MVert> verts(mesh->mvert, mesh->totvert); - MutableSpan<MEdge> edges(mesh->medge, mesh->totedge); - MutableSpan<MPoly> polys(mesh->mpoly, mesh->totpoly); - MutableSpan<MLoop> loops(mesh->mloop, mesh->totloop); + MutableSpan<MVert> verts = mesh->vertices_for_write(); + MutableSpan<MEdge> edges = mesh->edges_for_write(); + MutableSpan<MPoly> polys = mesh->polygons_for_write(); + MutableSpan<MLoop> loops = mesh->loops_for_write(); MVert *mvert = verts.data(); MEdge *medge = edges.data(); @@ -434,7 +432,7 @@ Mesh *BKE_mesh_new_nomain_from_curve(const Object *ob) struct EdgeLink { struct EdgeLink *next, *prev; - void *edge; + const void *edge; }; struct VertLink { @@ -458,10 +456,13 @@ static void appendPolyLineVert(ListBase *lb, uint index) void BKE_mesh_to_curve_nurblist(const Mesh *me, ListBase *nurblist, const int edge_users_test) { - MVert *mvert = me->mvert; - MEdge *med, *medge = me->medge; - MPoly *mp, *mpoly = me->mpoly; - MLoop *mloop = me->mloop; + const Span<MVert> verts = me->vertices(); + const Span<MEdge> mesh_edges = me->edges(); + const Span<MPoly> polys = me->polygons(); + const Span<MLoop> loops = me->loops(); + + const MEdge *med; + const MPoly *mp; int medge_len = me->totedge; int mpoly_len = me->totpoly; @@ -475,8 +476,8 @@ void BKE_mesh_to_curve_nurblist(const Mesh *me, ListBase *nurblist, const int ed /* get boundary edges */ edge_users = (int *)MEM_calloc_arrayN(medge_len, sizeof(int), __func__); - for (i = 0, mp = mpoly; i < mpoly_len; i++, mp++) { - MLoop *ml = &mloop[mp->loopstart]; + for (i = 0, mp = polys.data(); i < mpoly_len; i++, mp++) { + const MLoop *ml = &loops[mp->loopstart]; int j; for (j = 0; j < mp->totloop; j++, ml++) { edge_users[ml->e]++; @@ -484,7 +485,7 @@ void BKE_mesh_to_curve_nurblist(const Mesh *me, ListBase *nurblist, const int ed } /* create edges from all faces (so as to find edges not in any faces) */ - med = medge; + med = mesh_edges.data(); for (i = 0; i < medge_len; i++, med++) { if (edge_users[i] == edge_users_test) { EdgeLink *edl = MEM_cnew<EdgeLink>("EdgeLink"); @@ -587,7 +588,7 @@ void BKE_mesh_to_curve_nurblist(const Mesh *me, ListBase *nurblist, const int ed /* add points */ vl = (VertLink *)polyline.first; for (i = 0, bp = nu->bp; i < totpoly; i++, bp++, vl = (VertLink *)vl->next) { - copy_v3_v3(bp->vec, mvert[vl->index].co); + copy_v3_v3(bp->vec, verts[vl->index].co); bp->f1 = SELECT; bp->radius = bp->weight = 1.0; } @@ -682,19 +683,16 @@ void BKE_mesh_from_pointcloud(const PointCloud *pointcloud, Mesh *me) &pointcloud->pdata, &me->vdata, CD_MASK_PROP_ALL, CD_DUPLICATE, pointcloud->totpoint); /* Convert the Position attribute to a mesh vertex. */ - me->mvert = (MVert *)CustomData_add_layer( - &me->vdata, CD_MVERT, CD_SET_DEFAULT, nullptr, me->totvert); - CustomData_update_typemap(&me->vdata); + CustomData_add_layer(&me->vdata, CD_MVERT, CD_SET_DEFAULT, nullptr, me->totvert); const int layer_idx = CustomData_get_named_layer_index( &me->vdata, CD_PROP_FLOAT3, POINTCLOUD_ATTR_POSITION); CustomDataLayer *pos_layer = &me->vdata.layers[layer_idx]; float(*positions)[3] = (float(*)[3])pos_layer->data; - MVert *mvert; - mvert = me->mvert; - for (int i = 0; i < me->totvert; i++, mvert++) { - copy_v3_v3(mvert->co, positions[i]); + MutableSpan<MVert> verts = me->vertices_for_write(); + for (int i = 0; i < me->totvert; i++) { + copy_v3_v3(verts[i].co, positions[i]); } /* Delete Position attribute since it is now in vertex coordinates. */ @@ -703,9 +701,9 @@ void BKE_mesh_from_pointcloud(const PointCloud *pointcloud, Mesh *me) void BKE_mesh_edges_set_draw_render(Mesh *mesh) { - MEdge *med = mesh->medge; - for (int i = 0; i < mesh->totedge; i++, med++) { - med->flag |= ME_EDGEDRAW | ME_EDGERENDER; + MutableSpan<MEdge> edges = mesh->edges_for_write(); + for (int i = 0; i < mesh->totedge; i++) { + edges[i].flag |= ME_EDGEDRAW | ME_EDGERENDER; } } @@ -1171,7 +1169,8 @@ Mesh *BKE_mesh_create_derived_for_modifier(struct Depsgraph *depsgraph, if (build_shapekey_layers && me->key && (kb = (KeyBlock *)BLI_findlink(&me->key->block, ob_eval->shapenr - 1))) { - BKE_keyblock_convert_to_mesh(kb, me->mvert, me->totvert); + MutableSpan<MVert> verts = me->vertices_for_write(); + BKE_keyblock_convert_to_mesh(kb, verts.data(), me->totvert); } Mesh *mesh_temp = (Mesh *)BKE_id_copy_ex(nullptr, &me->id, nullptr, LIB_ID_COPY_LOCALIZE); @@ -1274,10 +1273,9 @@ static void shapekey_layers_to_keyblocks(Mesh *mesh_src, Mesh *mesh_dst, int act kb->data = kbcos = (float(*)[3])MEM_malloc_arrayN(kb->totelem, sizeof(float[3]), __func__); if (kb->uid == actshape_uid) { - MVert *mvert = mesh_src->mvert; - - for (j = 0; j < mesh_src->totvert; j++, kbcos++, mvert++) { - copy_v3_v3(*kbcos, mvert->co); + const Span<MVert> verts = mesh_src->vertices(); + for (j = 0; j < mesh_src->totvert; j++, kbcos++) { + copy_v3_v3(*kbcos, verts[j].co); } } else { @@ -1306,6 +1304,7 @@ void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, const CustomData_MeshMasks *mask, bool take_ownership) { + using namespace blender::bke; BLI_assert(mesh_src->id.tag & LIB_TAG_NO_MAIN); /* mesh_src might depend on mesh_dst, so we need to do everything with a local copy */ @@ -1385,30 +1384,30 @@ void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, CustomData_add_layer(&tmp.vdata, CD_MVERT, CD_ASSIGN, - (alloctype == CD_ASSIGN) ? mesh_src->mvert : - MEM_dupallocN(mesh_src->mvert), + (alloctype == CD_ASSIGN) ? mesh_src->vertices_for_write().data() : + MEM_dupallocN(mesh_src->vertices().data()), totvert); } if (!CustomData_has_layer(&tmp.edata, CD_MEDGE)) { CustomData_add_layer(&tmp.edata, CD_MEDGE, CD_ASSIGN, - (alloctype == CD_ASSIGN) ? mesh_src->medge : - MEM_dupallocN(mesh_src->medge), + (alloctype == CD_ASSIGN) ? mesh_src->edges_for_write().data() : + MEM_dupallocN(mesh_src->edges().data()), totedge); } if (!CustomData_has_layer(&tmp.pdata, CD_MPOLY)) { CustomData_add_layer(&tmp.ldata, CD_MLOOP, CD_ASSIGN, - (alloctype == CD_ASSIGN) ? mesh_src->mloop : - MEM_dupallocN(mesh_src->mloop), + (alloctype == CD_ASSIGN) ? mesh_src->loops_for_write().data() : + MEM_dupallocN(mesh_src->loops().data()), tmp.totloop); CustomData_add_layer(&tmp.pdata, CD_MPOLY, CD_ASSIGN, - (alloctype == CD_ASSIGN) ? mesh_src->mpoly : - MEM_dupallocN(mesh_src->mpoly), + (alloctype == CD_ASSIGN) ? mesh_src->polygons_for_write().data() : + MEM_dupallocN(mesh_src->polygons().data()), tmp.totpoly); } @@ -1425,9 +1424,6 @@ void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, } } - /* yes, must be before _and_ after tessellate */ - BKE_mesh_update_customdata_pointers(&tmp, false); - CustomData_free(&mesh_dst->vdata, mesh_dst->totvert); CustomData_free(&mesh_dst->edata, mesh_dst->totedge); CustomData_free(&mesh_dst->fdata, mesh_dst->totface); @@ -1481,7 +1477,6 @@ void BKE_mesh_nomain_to_meshkey(Mesh *mesh_src, Mesh *mesh_dst, KeyBlock *kb) int a, totvert = mesh_src->totvert; float *fp; - MVert *mvert; if (totvert == 0 || mesh_dst->totvert == 0 || mesh_dst->totvert != totvert) { return; @@ -1494,9 +1489,8 @@ void BKE_mesh_nomain_to_meshkey(Mesh *mesh_src, Mesh *mesh_dst, KeyBlock *kb) kb->totelem = totvert; fp = (float *)kb->data; - mvert = mesh_src->mvert; - - for (a = 0; a < kb->totelem; a++, fp += 3, mvert++) { - copy_v3_v3(fp, mvert->co); + const Span<MVert> verts = mesh_src->vertices(); + for (a = 0; a < kb->totelem; a++, fp += 3) { + copy_v3_v3(fp, verts[a].co); } } |