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/multires_reshape_apply_base.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/blenkernel/intern/multires_reshape_apply_base.c')
-rw-r--r-- | source/blender/blenkernel/intern/multires_reshape_apply_base.c | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/source/blender/blenkernel/intern/multires_reshape_apply_base.c b/source/blender/blenkernel/intern/multires_reshape_apply_base.c index f7d29806353..73f7197dcc9 100644 --- a/source/blender/blenkernel/intern/multires_reshape_apply_base.c +++ b/source/blender/blenkernel/intern/multires_reshape_apply_base.c @@ -30,11 +30,14 @@ void multires_reshape_apply_base_update_mesh_coords(MultiresReshapeContext *reshape_context) { Mesh *base_mesh = reshape_context->base_mesh; - const MLoop *mloop = base_mesh->mloop; - MVert *mvert = base_mesh->mvert; + MVert *base_verts = BKE_mesh_vertices_for_write(base_mesh); + /* Update the context in case the vertices were duplicated. */ + reshape_context->base_verts = base_verts; + + const MLoop *mloop = reshape_context->base_loops; for (int loop_index = 0; loop_index < base_mesh->totloop; ++loop_index) { const MLoop *loop = &mloop[loop_index]; - MVert *vert = &mvert[loop->v]; + MVert *vert = &base_verts[loop->v]; GridCoord grid_coord; grid_coord.grid_index = loop_index; @@ -66,13 +69,15 @@ static float v3_dist_from_plane(const float v[3], const float center[3], const f void multires_reshape_apply_base_refit_base_mesh(MultiresReshapeContext *reshape_context) { Mesh *base_mesh = reshape_context->base_mesh; - + MVert *base_verts = BKE_mesh_vertices_for_write(base_mesh); + /* Update the context in case the vertices were duplicated. */ + reshape_context->base_verts = base_verts; MeshElemMap *pmap; int *pmap_mem; BKE_mesh_vert_poly_map_create(&pmap, &pmap_mem, - base_mesh->mpoly, - base_mesh->mloop, + reshape_context->base_polys, + reshape_context->base_loops, base_mesh->totvert, base_mesh->totpoly, base_mesh->totloop); @@ -80,7 +85,7 @@ void multires_reshape_apply_base_refit_base_mesh(MultiresReshapeContext *reshape float(*origco)[3] = MEM_calloc_arrayN( base_mesh->totvert, sizeof(float[3]), "multires apply base origco"); for (int i = 0; i < base_mesh->totvert; i++) { - copy_v3_v3(origco[i], base_mesh->mvert[i].co); + copy_v3_v3(origco[i], base_verts[i].co); } for (int i = 0; i < base_mesh->totvert; i++) { @@ -94,11 +99,11 @@ void multires_reshape_apply_base_refit_base_mesh(MultiresReshapeContext *reshape /* Find center. */ int tot = 0; for (int j = 0; j < pmap[i].count; j++) { - const MPoly *p = &base_mesh->mpoly[pmap[i].indices[j]]; + const MPoly *p = &reshape_context->base_polys[pmap[i].indices[j]]; /* This double counts, not sure if that's bad or good. */ for (int k = 0; k < p->totloop; k++) { - const int vndx = base_mesh->mloop[p->loopstart + k].v; + const int vndx = reshape_context->base_loops[p->loopstart + k].v; if (vndx != i) { add_v3_v3(center, origco[vndx]); tot++; @@ -109,7 +114,7 @@ void multires_reshape_apply_base_refit_base_mesh(MultiresReshapeContext *reshape /* Find normal. */ for (int j = 0; j < pmap[i].count; j++) { - const MPoly *p = &base_mesh->mpoly[pmap[i].indices[j]]; + const MPoly *p = &reshape_context->base_polys[pmap[i].indices[j]]; MPoly fake_poly; MLoop *fake_loops; float(*fake_co)[3]; @@ -122,7 +127,7 @@ void multires_reshape_apply_base_refit_base_mesh(MultiresReshapeContext *reshape fake_co = MEM_malloc_arrayN(p->totloop, sizeof(float[3]), "fake_co"); for (int k = 0; k < p->totloop; k++) { - const int vndx = base_mesh->mloop[p->loopstart + k].v; + const int vndx = reshape_context->base_loops[p->loopstart + k].v; fake_loops[k].v = k; @@ -143,10 +148,10 @@ void multires_reshape_apply_base_refit_base_mesh(MultiresReshapeContext *reshape normalize_v3(avg_no); /* Push vertex away from the plane. */ - const float dist = v3_dist_from_plane(base_mesh->mvert[i].co, center, avg_no); + const float dist = v3_dist_from_plane(base_verts[i].co, center, avg_no); copy_v3_v3(push, avg_no); mul_v3_fl(push, dist); - add_v3_v3(base_mesh->mvert[i].co, push); + add_v3_v3(base_verts[i].co, push); } MEM_freeN(origco); |