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/draw/intern/mesh_extractors | |
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/draw/intern/mesh_extractors')
3 files changed, 21 insertions, 14 deletions
diff --git a/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_mesh_analysis.cc b/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_mesh_analysis.cc index 951990466d0..fe2a02b6b63 100644 --- a/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_mesh_analysis.cc +++ b/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_mesh_analysis.cc @@ -259,7 +259,8 @@ static void statvis_calc_thickness(const MeshRenderData *mr, float *r_thickness) } struct BVHTree_OverlapData { - const Mesh *me; + const MVert *verts; + const MLoop *loops; const MLoopTri *mlooptri; float epsilon; }; @@ -267,7 +268,6 @@ struct BVHTree_OverlapData { static bool bvh_overlap_cb(void *userdata, int index_a, int index_b, int UNUSED(thread)) { struct BVHTree_OverlapData *data = static_cast<struct BVHTree_OverlapData *>(userdata); - const Mesh *me = data->me; const MLoopTri *tri_a = &data->mlooptri[index_a]; const MLoopTri *tri_b = &data->mlooptri[index_b]; @@ -276,12 +276,12 @@ static bool bvh_overlap_cb(void *userdata, int index_a, int index_b, int UNUSED( return false; } - const float *tri_a_co[3] = {me->mvert[me->mloop[tri_a->tri[0]].v].co, - me->mvert[me->mloop[tri_a->tri[1]].v].co, - me->mvert[me->mloop[tri_a->tri[2]].v].co}; - const float *tri_b_co[3] = {me->mvert[me->mloop[tri_b->tri[0]].v].co, - me->mvert[me->mloop[tri_b->tri[1]].v].co, - me->mvert[me->mloop[tri_b->tri[2]].v].co}; + const float *tri_a_co[3] = {data->verts[data->loops[tri_a->tri[0]].v].co, + data->verts[data->loops[tri_a->tri[1]].v].co, + data->verts[data->loops[tri_a->tri[2]].v].co}; + const float *tri_b_co[3] = {data->verts[data->loops[tri_b->tri[0]].v].co, + data->verts[data->loops[tri_b->tri[1]].v].co, + data->verts[data->loops[tri_b->tri[2]].v].co}; float ix_pair[2][3]; int verts_shared = 0; @@ -342,7 +342,8 @@ static void statvis_calc_intersect(const MeshRenderData *mr, float *r_intersect) BVHTree *tree = BKE_bvhtree_from_mesh_get(&treeData, mr->me, BVHTREE_FROM_LOOPTRI, 4); struct BVHTree_OverlapData data = {nullptr}; - data.me = mr->me; + data.verts = mr->mvert; + data.loops = mr->mloop; data.mlooptri = mr->mlooptri; data.epsilon = BLI_bvhtree_get_epsilon(tree); diff --git a/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_sculpt_data.cc b/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_sculpt_data.cc index 492721b4853..4e3d4235487 100644 --- a/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_sculpt_data.cc +++ b/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_sculpt_data.cc @@ -9,6 +9,7 @@ #include "BLI_string.h" +#include "BKE_mesh.h" #include "BKE_paint.h" #include "draw_subdivision.h" @@ -128,6 +129,9 @@ static void extract_sculpt_data_init_subdiv(const DRWSubdivCache *subdiv_cache, GPUVertBuf *subdiv_mask_vbo = nullptr; const float *cd_mask = (const float *)CustomData_get_layer(cd_vdata, CD_PAINT_MASK); + const Span<MPoly> coarse_polys = coarse_mesh->polygons(); + const Span<MLoop> coarse_loops = coarse_mesh->loops(); + if (cd_mask) { GPUVertFormat mask_format = {0}; GPU_vertformat_attr_add(&mask_format, "msk", GPU_COMP_F32, 1, GPU_FETCH_FLOAT); @@ -138,11 +142,11 @@ static void extract_sculpt_data_init_subdiv(const DRWSubdivCache *subdiv_cache, float *v_mask = static_cast<float *>(GPU_vertbuf_get_data(mask_vbo)); for (int i = 0; i < coarse_mesh->totpoly; i++) { - const MPoly *mpoly = &coarse_mesh->mpoly[i]; + const MPoly *mpoly = &coarse_polys[i]; for (int loop_index = mpoly->loopstart; loop_index < mpoly->loopstart + mpoly->totloop; loop_index++) { - const MLoop *ml = &coarse_mesh->mloop[loop_index]; + const MLoop *ml = &coarse_loops[loop_index]; *v_mask++ = cd_mask[ml->v]; } } diff --git a/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_weights.cc b/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_weights.cc index ba194a4b167..8fe4940c6de 100644 --- a/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_weights.cc +++ b/source/blender/draw/intern/mesh_extractors/extract_mesh_vbo_weights.cc @@ -8,6 +8,7 @@ #include "MEM_guardedalloc.h" #include "BKE_deform.h" +#include "BKE_mesh.h" #include "draw_subdivision.h" #include "extract_mesh.hh" @@ -105,7 +106,7 @@ static void extract_weights_init(const MeshRenderData *mr, data->cd_ofs = CustomData_get_offset(&mr->bm->vdata, CD_MDEFORMVERT); } else { - data->dvert = (const MDeformVert *)CustomData_get_layer(&mr->me->vdata, CD_MDEFORMVERT); + data->dvert = mr->me->deform_verts().data(); data->cd_ofs = -1; } } @@ -171,8 +172,9 @@ static void extract_weights_init_subdiv(const DRWSubdivCache *subdiv_cache, extract_weights_init(mr, cache, coarse_weights, _data); if (mr->extract_type != MR_EXTRACT_BMESH) { - for (int i = 0; i < coarse_mesh->totpoly; i++) { - const MPoly *mpoly = &coarse_mesh->mpoly[i]; + const Span<MPoly> coarse_polys = coarse_mesh->polygons(); + for (const int i : coarse_polys.index_range()) { + const MPoly *mpoly = &coarse_polys[i]; extract_weights_iter_poly_mesh(mr, mpoly, i, _data); } } |