Mesh: Move BVH storage to shared cache system

Avoid rebuilding BVH trees when meshes are copied.
Similar to the other uses of the shared cache system,
this can arbitrarily improve performance when meshes
are copied but not deformed and BVH building is the
main bottleneck. In a simple test file I got a 6x speedup.

The amount of code is also reduced and the system is
much simpler overall-- built out of common threading
patterns like `SharedCache` with its double-checked lock.
RAII is used in a few places to simplify memory management
too.

The downside is storing more `SharedCache` items in the
mesh runtime struct. That has a slight cost when copying
a small mesh many times, but we have ideas to improve that
in the future anyway (#104327).

Pull Request: https://projects.blender.org/blender/blender/pulls/130865
This commit is contained in:
Hans Goudey
2024-12-04 00:17:17 +01:00
committed by Hans Goudey
parent ee9ddbeea3
commit 024d7d12e2
42 changed files with 438 additions and 577 deletions

View File

@@ -273,9 +273,7 @@ static void try_convert_single_object(Object &curves_ob,
}
Mesh &surface_me = *static_cast<Mesh *>(surface_ob.data);
BVHTreeFromMesh surface_bvh;
BKE_bvhtree_from_mesh_get(&surface_bvh, &surface_me, BVHTREE_FROM_CORNER_TRIS, 2);
BLI_SCOPED_DEFER([&]() { free_bvhtree_from_mesh(&surface_bvh); });
BVHTreeFromMesh surface_bvh = surface_me.bvh_corner_tris();
const Span<float3> positions_cu = curves.positions();
const Span<int> tri_faces = surface_me.corner_tri_faces();
@@ -615,9 +613,7 @@ static void snap_curves_to_surface_exec_object(Object &curves_ob,
switch (attach_mode) {
case AttachMode::Nearest: {
BVHTreeFromMesh surface_bvh;
BKE_bvhtree_from_mesh_get(&surface_bvh, &surface_mesh, BVHTREE_FROM_CORNER_TRIS, 2);
BLI_SCOPED_DEFER([&]() { free_bvhtree_from_mesh(&surface_bvh); });
BVHTreeFromMesh surface_bvh = surface_mesh.bvh_corner_tris();
threading::parallel_for(curves.curves_range(), 256, [&](const IndexRange curves_range) {
for (const int curve_i : curves_range) {