From 9a776830a67af5d55d12e93dd5dc38533669fafc Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Wed, 14 Aug 2024 23:27:10 -0400 Subject: [PATCH] Refactor: Sculpt: Reduce usage of BVH tree positions array In preparation for removing the positions array from the BVH tree and just retrieving the evaluated or original positions as necessary. --- source/blender/blenkernel/BKE_pbvh_api.hh | 2 ++ source/blender/blenkernel/intern/pbvh.cc | 22 +++++++++++-------- source/blender/editors/sculpt_paint/sculpt.cc | 7 ++++++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/source/blender/blenkernel/BKE_pbvh_api.hh b/source/blender/blenkernel/BKE_pbvh_api.hh index ee825fa4f7b..fb8d103efcd 100644 --- a/source/blender/blenkernel/BKE_pbvh_api.hh +++ b/source/blender/blenkernel/BKE_pbvh_api.hh @@ -303,6 +303,7 @@ bool raycast_node(Tree &pbvh, Node &node, const float (*origco)[3], bool use_origco, + Span vert_positions, Span corner_verts, Span corner_tris, Span corner_tri_faces, @@ -344,6 +345,7 @@ bool find_nearest_to_ray_node(Tree &pbvh, Node &node, const float (*origco)[3], bool use_origco, + Span vert_positions, Span corner_verts, Span corner_tris, Span corner_tri_faces, diff --git a/source/blender/blenkernel/intern/pbvh.cc b/source/blender/blenkernel/intern/pbvh.cc index 7ba360fffb4..55b9e7a9284 100644 --- a/source/blender/blenkernel/intern/pbvh.cc +++ b/source/blender/blenkernel/intern/pbvh.cc @@ -1934,6 +1934,7 @@ bool ray_face_nearest_tri(const float ray_start[3], static bool pbvh_faces_node_raycast(Tree &pbvh, const Node &node, const float (*origco)[3], + const Span vert_positions, const Span corner_verts, const Span corner_tris, const Span corner_tri_faces, @@ -1969,9 +1970,9 @@ static bool pbvh_faces_node_raycast(Tree &pbvh, } else { /* intersect with current coordinates */ - co[0] = positions[corner_verts[tri[0]]]; - co[1] = positions[corner_verts[tri[1]]]; - co[2] = positions[corner_verts[tri[2]]]; + co[0] = vert_positions[corner_verts[tri[0]]]; + co[1] = vert_positions[corner_verts[tri[1]]]; + co[2] = vert_positions[corner_verts[tri[2]]]; } if (ray_face_intersection_tri(ray_start, isect_precalc, co[0], co[1], co[2], depth)) { @@ -2101,6 +2102,7 @@ bool raycast_node(Tree &pbvh, Node &node, const float (*origco)[3], bool use_origco, + const Span vert_positions, const Span corner_verts, const Span corner_tris, const Span corner_tri_faces, @@ -2124,6 +2126,7 @@ bool raycast_node(Tree &pbvh, hit |= pbvh_faces_node_raycast(pbvh, node, origco, + vert_positions, corner_verts, corner_tris, corner_tri_faces, @@ -2294,6 +2297,7 @@ void find_nearest_to_ray(Tree &pbvh, static bool pbvh_faces_node_nearest_to_ray(Tree &pbvh, const Node &node, const float (*origco)[3], + const Span vert_positions, const Span corner_verts, const Span corner_tris, const Span corner_tri_faces, @@ -2303,8 +2307,6 @@ static bool pbvh_faces_node_nearest_to_ray(Tree &pbvh, float *depth, float *dist_sq) { - using namespace blender; - const Span positions = pbvh.vert_positions_; bool hit = false; for (const int i : node.prim_indices_.index_range()) { @@ -2330,9 +2332,9 @@ static bool pbvh_faces_node_nearest_to_ray(Tree &pbvh, /* intersect with current coordinates */ hit |= ray_face_nearest_tri(ray_start, ray_normal, - positions[corner_verts[corner_tri[0]]], - positions[corner_verts[corner_tri[1]]], - positions[corner_verts[corner_tri[2]]], + vert_positions[corner_verts[corner_tri[0]]], + vert_positions[corner_verts[corner_tri[1]]], + vert_positions[corner_verts[corner_tri[2]]], depth, dist_sq); } @@ -2406,6 +2408,7 @@ bool find_nearest_to_ray_node(Tree &pbvh, Node &node, const float (*origco)[3], bool use_origco, + const Span vert_positions, const Span corner_verts, const Span corner_tris, const Span corner_tri_faces, @@ -2426,6 +2429,7 @@ bool find_nearest_to_ray_node(Tree &pbvh, hit |= pbvh_faces_node_nearest_to_ray(pbvh, node, origco, + vert_positions, corner_verts, corner_tris, corner_tri_faces, @@ -2533,7 +2537,7 @@ static blender::draw::pbvh::PBVH_GPU_Args pbvh_draw_args_init(const Mesh &mesh, args.corner_data = &mesh.corner_data; args.face_data = &mesh.face_data; args.mesh = pbvh.mesh_; - args.vert_positions = pbvh.vert_positions_; + args.vert_positions = BKE_pbvh_get_vert_positions(pbvh); args.corner_verts = mesh.corner_verts(); args.corner_edges = mesh.corner_edges(); args.corner_tris = mesh.corner_tris(); diff --git a/source/blender/editors/sculpt_paint/sculpt.cc b/source/blender/editors/sculpt_paint/sculpt.cc index 40ab57e8c5b..015a78e2522 100644 --- a/source/blender/editors/sculpt_paint/sculpt.cc +++ b/source/blender/editors/sculpt_paint/sculpt.cc @@ -2915,6 +2915,7 @@ struct SculptRaycastData { bool hit; float depth; bool original; + Span vert_positions; Span corner_verts; Span corner_tris; Span corner_tri_faces; @@ -2935,6 +2936,7 @@ struct SculptFindNearestToRayData { float depth; float dist_sq_to_ray; bool original; + Span vert_positions; Span corner_verts; Span corner_tris; Span corner_tri_faces; @@ -4690,6 +4692,7 @@ static void sculpt_raycast_cb(blender::bke::pbvh::Node &node, SculptRaycastData node, origco, use_origco, + srd.vert_positions, srd.corner_verts, srd.corner_tris, srd.corner_tri_faces, @@ -4735,6 +4738,7 @@ static void sculpt_find_nearest_to_ray_cb(blender::bke::pbvh::Node &node, node, origco, use_origco, + srd.vert_positions, srd.corner_verts, srd.corner_tris, srd.corner_tri_faces, @@ -4831,6 +4835,7 @@ bool SCULPT_cursor_geometry_info_update(bContext *C, srd.hit = false; if (ss.pbvh->type() == bke::pbvh::Type::Mesh) { const Mesh &mesh = *static_cast(ob.data); + srd.vert_positions = BKE_pbvh_get_vert_positions(*ss.pbvh); srd.corner_verts = mesh.corner_verts(); srd.corner_tris = mesh.corner_tris(); srd.corner_tri_faces = mesh.corner_tri_faces(); @@ -4983,6 +4988,7 @@ bool SCULPT_stroke_get_location_ex(bContext *C, srd.hit = false; if (ss.pbvh->type() == bke::pbvh::Type::Mesh) { const Mesh &mesh = *static_cast(ob.data); + srd.vert_positions = BKE_pbvh_get_vert_positions(*ss.pbvh); srd.corner_verts = mesh.corner_verts(); srd.corner_tris = mesh.corner_tris(); srd.corner_tri_faces = mesh.corner_tri_faces(); @@ -5018,6 +5024,7 @@ bool SCULPT_stroke_get_location_ex(bContext *C, srd.hit = false; if (ss.pbvh->type() == bke::pbvh::Type::Mesh) { const Mesh &mesh = *static_cast(ob.data); + srd.vert_positions = BKE_pbvh_get_vert_positions(*ss.pbvh); srd.corner_verts = mesh.corner_verts(); srd.corner_tris = mesh.corner_tris(); srd.corner_tri_faces = mesh.corner_tri_faces();