[Bf-blender-cvs] [b76cafb1919] temp-lineart-contained: Cleanup: Use const mesh to ensure BVH and triangulation cache

Hans Goudey noreply at git.blender.org
Sun Jul 4 07:28:04 CEST 2021


Commit: b76cafb1919e31b701f6eb70f24390e7916b22f9
Author: Hans Goudey
Date:   Fri Jul 2 11:37:01 2021 -0500
Branches: temp-lineart-contained
https://developer.blender.org/rBb76cafb1919e31b701f6eb70f24390e7916b22f9

Cleanup: Use const mesh to ensure BVH and triangulation cache

As noted in a comment now, these functions only update a cache, so they
don't change the logical state of the mesh, which is "it will have the
data when necessary." Using a const argument will help const correctness
when accessing an object's evaluated mesh.

===================================================================

M	source/blender/blenkernel/BKE_bvhutils.h
M	source/blender/blenkernel/BKE_mesh_runtime.h
M	source/blender/blenkernel/intern/bvhutils.cc
M	source/blender/blenkernel/intern/mesh_runtime.c
M	source/blender/nodes/geometry/nodes/node_geo_attribute_proximity.cc
M	source/blender/nodes/geometry/nodes/node_geo_attribute_transfer.cc
M	source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc
M	source/blender/nodes/geometry/nodes/node_geo_raycast.cc

===================================================================

diff --git a/source/blender/blenkernel/BKE_bvhutils.h b/source/blender/blenkernel/BKE_bvhutils.h
index 78908908343..8be2fcbdb83 100644
--- a/source/blender/blenkernel/BKE_bvhutils.h
+++ b/source/blender/blenkernel/BKE_bvhutils.h
@@ -218,7 +218,7 @@ BVHTree *bvhtree_from_mesh_looptri_ex(struct BVHTreeFromMesh *data,
                                       ThreadMutex *mesh_eval_mutex);
 
 BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data,
-                                   struct Mesh *mesh,
+                                   const struct Mesh *mesh,
                                    const BVHCacheType bvh_cache_type,
                                    const int tree_type);
 
diff --git a/source/blender/blenkernel/BKE_mesh_runtime.h b/source/blender/blenkernel/BKE_mesh_runtime.h
index 67c87e96aff..3efbef94081 100644
--- a/source/blender/blenkernel/BKE_mesh_runtime.h
+++ b/source/blender/blenkernel/BKE_mesh_runtime.h
@@ -45,7 +45,7 @@ void BKE_mesh_runtime_reset(struct Mesh *mesh);
 void BKE_mesh_runtime_reset_on_copy(struct Mesh *mesh, const int flag);
 int BKE_mesh_runtime_looptri_len(const struct Mesh *mesh);
 void BKE_mesh_runtime_looptri_recalc(struct Mesh *mesh);
-const struct MLoopTri *BKE_mesh_runtime_looptri_ensure(struct Mesh *mesh);
+const struct MLoopTri *BKE_mesh_runtime_looptri_ensure(const struct Mesh *mesh);
 bool BKE_mesh_runtime_ensure_edit_data(struct Mesh *mesh);
 bool BKE_mesh_runtime_clear_edit_data(struct Mesh *mesh);
 bool BKE_mesh_runtime_reset_edit_data(struct Mesh *mesh);
diff --git a/source/blender/blenkernel/intern/bvhutils.cc b/source/blender/blenkernel/intern/bvhutils.cc
index c2852426174..3dea49d1953 100644
--- a/source/blender/blenkernel/intern/bvhutils.cc
+++ b/source/blender/blenkernel/intern/bvhutils.cc
@@ -1438,9 +1438,12 @@ static BLI_bitmap *looptri_no_hidden_map_get(const MPoly *mpoly,
 
 /**
  * Builds or queries a bvhcache for the cache bvhtree of the request type.
+ *
+ * \note This function only fills a cache, and therefore the mesh argument can
+ * be considered logically const. Concurrent access is protected by a mutex.
  */
 BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data,
-                                   struct Mesh *mesh,
+                                   const struct Mesh *mesh,
                                    const BVHCacheType bvh_cache_type,
                                    const int tree_type)
 {
@@ -1448,7 +1451,7 @@ BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data,
   BVHCache **bvh_cache_p = (BVHCache **)&mesh->runtime.bvh_cache;
   ThreadMutex *mesh_eval_mutex = (ThreadMutex *)mesh->runtime.eval_mutex;
 
-  bool is_cached = bvhcache_find(bvh_cache_p, bvh_cache_type, &tree, nullptr, nullptr);
+  const bool is_cached = bvhcache_find(bvh_cache_p, bvh_cache_type, &tree, nullptr, nullptr);
 
   if (is_cached && tree == nullptr) {
     memset(data, 0, sizeof(*data));
diff --git a/source/blender/blenkernel/intern/mesh_runtime.c b/source/blender/blenkernel/intern/mesh_runtime.c
index 011dd7e25ee..6ae534012a9 100644
--- a/source/blender/blenkernel/intern/mesh_runtime.c
+++ b/source/blender/blenkernel/intern/mesh_runtime.c
@@ -158,8 +158,12 @@ static void mesh_runtime_looptri_recalc_isolated(void *userdata)
   BKE_mesh_runtime_looptri_recalc(mesh);
 }
 
-/* This is a ported copy of dm_getLoopTriArray(dm). */
-const MLoopTri *BKE_mesh_runtime_looptri_ensure(Mesh *mesh)
+/**
+ * \note This function only fills a cache, and therefore the mesh argument can
+ * be considered logically const. Concurrent access is protected by a mutex.
+ * \note This is a ported copy of dm_getLoopTriArray(dm).
+ */
+const MLoopTri *BKE_mesh_runtime_looptri_ensure(const Mesh *mesh)
 {
   ThreadMutex *mesh_eval_mutex = (ThreadMutex *)mesh->runtime.eval_mutex;
   BLI_mutex_lock(mesh_eval_mutex);
diff --git a/source/blender/nodes/geometry/nodes/node_geo_attribute_proximity.cc b/source/blender/nodes/geometry/nodes/node_geo_attribute_proximity.cc
index b7863d38fc2..d71cb09f1bd 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_attribute_proximity.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_attribute_proximity.cc
@@ -143,8 +143,7 @@ static bool bvh_from_mesh(const Mesh *target_mesh,
       break;
   }
 
-  /* This only updates a cache and can be considered to be logically const. */
-  BKE_bvhtree_from_mesh_get(&r_tree_data_mesh, const_cast<Mesh *>(target_mesh), bvh_type, 2);
+  BKE_bvhtree_from_mesh_get(&r_tree_data_mesh, target_mesh, bvh_type, 2);
   if (r_tree_data_mesh.tree == nullptr) {
     return false;
   }
diff --git a/source/blender/nodes/geometry/nodes/node_geo_attribute_transfer.cc b/source/blender/nodes/geometry/nodes/node_geo_attribute_transfer.cc
index d1114713672..756f93f154f 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_attribute_transfer.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_attribute_transfer.cc
@@ -162,7 +162,7 @@ static void get_closest_mesh_points(const Mesh &mesh,
 {
   BLI_assert(mesh.totvert > 0);
   BVHTreeFromMesh tree_data;
-  BKE_bvhtree_from_mesh_get(&tree_data, const_cast<Mesh *>(&mesh), BVHTREE_FROM_VERTS, 2);
+  BKE_bvhtree_from_mesh_get(&tree_data, &mesh, BVHTREE_FROM_VERTS, 2);
   get_closest_in_bvhtree(tree_data, positions, r_point_indices, r_distances_sq, r_positions);
   free_bvhtree_from_mesh(&tree_data);
 }
@@ -175,7 +175,7 @@ static void get_closest_mesh_edges(const Mesh &mesh,
 {
   BLI_assert(mesh.totedge > 0);
   BVHTreeFromMesh tree_data;
-  BKE_bvhtree_from_mesh_get(&tree_data, const_cast<Mesh *>(&mesh), BVHTREE_FROM_EDGES, 2);
+  BKE_bvhtree_from_mesh_get(&tree_data, &mesh, BVHTREE_FROM_EDGES, 2);
   get_closest_in_bvhtree(tree_data, positions, r_edge_indices, r_distances_sq, r_positions);
   free_bvhtree_from_mesh(&tree_data);
 }
@@ -188,7 +188,7 @@ static void get_closest_mesh_looptris(const Mesh &mesh,
 {
   BLI_assert(mesh.totpoly > 0);
   BVHTreeFromMesh tree_data;
-  BKE_bvhtree_from_mesh_get(&tree_data, const_cast<Mesh *>(&mesh), BVHTREE_FROM_LOOPTRI, 2);
+  BKE_bvhtree_from_mesh_get(&tree_data, &mesh, BVHTREE_FROM_LOOPTRI, 2);
   get_closest_in_bvhtree(tree_data, positions, r_looptri_indices, r_distances_sq, r_positions);
   free_bvhtree_from_mesh(&tree_data);
 }
diff --git a/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc b/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc
index 772bd8a1080..d456c72744f 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_point_distribute.cc
@@ -83,8 +83,7 @@ static float3 normal_to_euler_rotation(const float3 normal)
 
 static Span<MLoopTri> get_mesh_looptris(const Mesh &mesh)
 {
-  /* This only updates a cache and can be considered to be logically const. */
-  const MLoopTri *looptris = BKE_mesh_runtime_looptri_ensure(const_cast<Mesh *>(&mesh));
+  const MLoopTri *looptris = BKE_mesh_runtime_looptri_ensure(&mesh);
   const int looptris_len = BKE_mesh_runtime_looptri_len(&mesh);
   return {looptris, looptris_len};
 }
diff --git a/source/blender/nodes/geometry/nodes/node_geo_raycast.cc b/source/blender/nodes/geometry/nodes/node_geo_raycast.cc
index bfd6027e0fc..0c1d8645411 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_raycast.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_raycast.cc
@@ -95,7 +95,7 @@ static void raycast_to_mesh(const Mesh *mesh,
   BLI_assert(ray_origins.size() == r_hit_distances.size() || r_hit_distances.is_empty());
 
   BVHTreeFromMesh tree_data;
-  BKE_bvhtree_from_mesh_get(&tree_data, const_cast<Mesh *>(mesh), BVHTREE_FROM_LOOPTRI, 4);
+  BKE_bvhtree_from_mesh_get(&tree_data, mesh, BVHTREE_FROM_LOOPTRI, 4);
 
   if (tree_data.tree != nullptr) {
     for (const int i : ray_origins.index_range()) {



More information about the Bf-blender-cvs mailing list