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/modifiers/intern/MOD_build.c | |
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/modifiers/intern/MOD_build.c')
-rw-r--r-- | source/blender/modifiers/intern/MOD_build.c | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/source/blender/modifiers/intern/MOD_build.c b/source/blender/modifiers/intern/MOD_build.c index a9b6af967be..02d56560fc5 100644 --- a/source/blender/modifiers/intern/MOD_build.c +++ b/source/blender/modifiers/intern/MOD_build.c @@ -62,7 +62,8 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct int *vertMap, *edgeMap, *faceMap; float frac; MPoly *mpoly_dst; - MLoop *ml_dst, *ml_src /*, *mloop_dst */; + MLoop *ml_dst; + const MLoop *ml_src; GHashIterator gh_iter; /* maps vert indices in old mesh to indices in new mesh */ GHash *vertHash = BLI_ghash_int_new("build ve apply gh"); @@ -74,10 +75,10 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct const int vert_src_num = mesh->totvert; const int edge_src_num = mesh->totedge; const int poly_src_num = mesh->totpoly; - MPoly *mpoly_src = mesh->mpoly; - MLoop *mloop_src = mesh->mloop; - MEdge *medge_src = mesh->medge; - MVert *mvert_src = mesh->mvert; + const MVert *mvert_src = BKE_mesh_vertices(mesh); + const MEdge *medge_src = BKE_mesh_edges(mesh); + const MPoly *mpoly_src = BKE_mesh_polygons(mesh); + const MLoop *mloop_src = BKE_mesh_loops(mesh); vertMap = MEM_malloc_arrayN(vert_src_num, sizeof(*vertMap), "build modifier vertMap"); edgeMap = MEM_malloc_arrayN(edge_src_num, sizeof(*edgeMap), "build modifier edgeMap"); @@ -99,8 +100,8 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct /* if there's at least one face, build based on faces */ if (faces_dst_num) { - MPoly *mpoly, *mp; - MLoop *ml, *mloop; + const MPoly *mpoly, *mp; + const MLoop *ml, *mloop; uintptr_t hash_num, hash_num_alt; if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) { @@ -135,7 +136,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct hash_num = 0; hash_num_alt = 0; for (i = 0; i < edge_src_num; i++, hash_num_alt++) { - MEdge *me = medge_src + i; + const MEdge *me = medge_src + i; if (BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v1)) && BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v2))) { @@ -147,7 +148,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct BLI_assert(hash_num == BLI_ghash_len(edgeHash)); } else if (edges_dst_num) { - MEdge *medge, *me; + const MEdge *medge, *me; uintptr_t hash_num; if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) { @@ -201,6 +202,10 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct /* now we know the number of verts, edges and faces, we can create the mesh. */ result = BKE_mesh_new_nomain_from_template( mesh, BLI_ghash_len(vertHash), BLI_ghash_len(edgeHash), 0, loops_dst_num, faces_dst_num); + MVert *result_verts = BKE_mesh_vertices_for_write(result); + MEdge *result_edges = BKE_mesh_edges_for_write(result); + MPoly *result_polys = BKE_mesh_polygons_for_write(result); + MLoop *result_loops = BKE_mesh_loops_for_write(result); /* copy the vertices across */ GHASH_ITER (gh_iter, vertHash) { @@ -210,7 +215,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct int newIndex = POINTER_AS_INT(BLI_ghashIterator_getValue(&gh_iter)); source = mvert_src[oldIndex]; - dest = &result->mvert[newIndex]; + dest = &result_verts[newIndex]; CustomData_copy_data(&mesh->vdata, &result->vdata, oldIndex, newIndex, 1); *dest = source; @@ -223,7 +228,7 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct int oldIndex = POINTER_AS_INT(BLI_ghash_lookup(edgeHash, POINTER_FROM_INT(i))); source = medge_src[oldIndex]; - dest = &result->medge[i]; + dest = &result_edges[i]; source.v1 = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(source.v1))); source.v2 = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(source.v2))); @@ -232,13 +237,13 @@ static Mesh *modifyMesh(ModifierData *md, const ModifierEvalContext *ctx, struct *dest = source; } - mpoly_dst = result->mpoly; - ml_dst = result->mloop; + mpoly_dst = result_polys; + ml_dst = result_loops; /* copy the faces across, remapping indices */ k = 0; for (i = 0; i < faces_dst_num; i++) { - MPoly *source; + const MPoly *source; MPoly *dest; source = mpoly_src + faceMap[i]; |