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/bvhutils.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/bvhutils.cc')
-rw-r--r-- | source/blender/blenkernel/intern/bvhutils.cc | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/source/blender/blenkernel/intern/bvhutils.cc b/source/blender/blenkernel/intern/bvhutils.cc index d0b57b45d35..d33ff933004 100644 --- a/source/blender/blenkernel/intern/bvhutils.cc +++ b/source/blender/blenkernel/intern/bvhutils.cc @@ -15,6 +15,7 @@ #include "BLI_linklist.h" #include "BLI_math.h" +#include "BLI_span.hh" #include "BLI_task.h" #include "BLI_threads.h" #include "BLI_utildefines.h" @@ -27,6 +28,7 @@ #include "MEM_guardedalloc.h" +using blender::Span; using blender::VArray; /* -------------------------------------------------------------------- */ @@ -1229,14 +1231,17 @@ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, looptri = BKE_mesh_runtime_looptri_ensure(mesh); looptri_len = BKE_mesh_runtime_looptri_len(mesh); } + const Span<MVert> verts = mesh->vertices(); + const Span<MEdge> edges = mesh->edges(); + const Span<MLoop> loops = mesh->loops(); /* Setup BVHTreeFromMesh */ bvhtree_from_mesh_setup_data(nullptr, bvh_cache_type, - mesh->mvert, - mesh->medge, - mesh->mface, - mesh->mloop, + verts.data(), + edges.data(), + (const MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE), + loops.data(), looptri, BKE_mesh_vertex_normals_ensure(mesh), data); @@ -1260,31 +1265,38 @@ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, switch (bvh_cache_type) { case BVHTREE_FROM_LOOSEVERTS: mask = loose_verts_map_get( - mesh->medge, mesh->totedge, mesh->mvert, mesh->totvert, &mask_bits_act_len); + edges.data(), mesh->totedge, verts.data(), mesh->totvert, &mask_bits_act_len); ATTR_FALLTHROUGH; case BVHTREE_FROM_VERTS: data->tree = bvhtree_from_mesh_verts_create_tree( - 0.0f, tree_type, 6, mesh->mvert, mesh->totvert, mask, mask_bits_act_len); + 0.0f, tree_type, 6, verts.data(), mesh->totvert, mask, mask_bits_act_len); break; case BVHTREE_FROM_LOOSEEDGES: - mask = loose_edges_map_get(mesh->medge, mesh->totedge, &mask_bits_act_len); + mask = loose_edges_map_get(edges.data(), mesh->totedge, &mask_bits_act_len); ATTR_FALLTHROUGH; case BVHTREE_FROM_EDGES: data->tree = bvhtree_from_mesh_edges_create_tree( - mesh->mvert, mesh->medge, mesh->totedge, mask, mask_bits_act_len, 0.0f, tree_type, 6); + verts.data(), edges.data(), mesh->totedge, mask, mask_bits_act_len, 0.0f, tree_type, 6); break; case BVHTREE_FROM_FACES: BLI_assert(!(mesh->totface == 0 && mesh->totpoly != 0)); data->tree = bvhtree_from_mesh_faces_create_tree( - 0.0f, tree_type, 6, mesh->mvert, mesh->mface, mesh->totface, nullptr, -1); + 0.0f, + tree_type, + 6, + verts.data(), + (const MFace *)CustomData_get_layer(&mesh->fdata, CD_MFACE), + mesh->totface, + nullptr, + -1); break; case BVHTREE_FROM_LOOPTRI_NO_HIDDEN: { blender::bke::AttributeAccessor attributes = blender::bke::mesh_attributes(*mesh); mask = looptri_no_hidden_map_get( - mesh->mpoly, + mesh->polygons().data(), attributes.lookup_or_default(".hide_poly", ATTR_DOMAIN_FACE, false), looptri_len, &mask_bits_act_len); @@ -1294,8 +1306,8 @@ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, data->tree = bvhtree_from_mesh_looptri_create_tree(0.0f, tree_type, 6, - mesh->mvert, - mesh->mloop, + verts.data(), + loops.data(), looptri, looptri_len, mask, |