[Bf-blender-cvs] [cad897de166] master: Transform: remove SnapData cache for meshes

Germano Cavalcante noreply at git.blender.org
Mon Nov 7 12:27:42 CET 2022


Commit: cad897de1660b1c81971c58cbcea41100d5f930c
Author: Germano Cavalcante
Date:   Thu Nov 3 16:33:41 2022 -0300
Branches: master
https://developer.blender.org/rBcad897de1660b1c81971c58cbcea41100d5f930c

Transform: remove SnapData cache for meshes

All cache needed is already stored in `Mesh.runtime`.

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

M	source/blender/editors/transform/transform_snap_object.cc

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

diff --git a/source/blender/editors/transform/transform_snap_object.cc b/source/blender/editors/transform/transform_snap_object.cc
index 24d76a50117..a75a94803f7 100644
--- a/source/blender/editors/transform/transform_snap_object.cc
+++ b/source/blender/editors/transform/transform_snap_object.cc
@@ -61,41 +61,6 @@ enum eViewProj {
   VIEW_PROJ_PERSP = -1,
 };
 
-/* SnapObjectContext.cache.mesh_map */
-struct SnapData_Mesh {
-  /* Loose edges, loose verts. */
-  BVHTree *bvhtree[2];
-  bool cached[2];
-
-  /* Looptris. */
-  BVHTreeFromMesh treedata_mesh;
-
-  const MPoly *poly;
-  bool has_looptris;
-  bool has_loose_edge;
-  bool has_loose_vert;
-
-  void clear()
-  {
-    for (int i = 0; i < ARRAY_SIZE(this->bvhtree); i++) {
-      if (!this->cached[i]) {
-        BLI_bvhtree_free(this->bvhtree[i]);
-      }
-      this->bvhtree[i] = nullptr;
-    }
-    free_bvhtree_from_mesh(&this->treedata_mesh);
-  }
-
-  ~SnapData_Mesh()
-  {
-    this->clear();
-  }
-
-#ifdef WITH_CXX_GUARDEDALLOC
-  MEM_CXX_CLASS_ALLOC_FUNCS("SnapData_Mesh")
-#endif
-};
-
 /* SnapObjectContext.cache.editmesh_map */
 struct SnapData_EditMesh {
   /* Verts, Edges. */
@@ -134,7 +99,6 @@ struct SnapObjectContext {
 
   int flag;
 
-  Map<const Object *, std::unique_ptr<SnapData_Mesh>> mesh_caches;
   Map<const BMEditMesh *, std::unique_ptr<SnapData_EditMesh>> editmesh_caches;
 
   /* Filter data, returns true to check this value */
@@ -180,28 +144,36 @@ static const Mesh *mesh_for_snap(Object *ob_eval, eSnapEditType edit_mode_type,
 {
   const Mesh *me_eval = BKE_object_get_evaluated_mesh(ob_eval);
   bool use_hide = false;
-  if (BKE_object_is_in_editmode(ob_eval)) {
-    if (edit_mode_type == SNAP_GEOM_EDIT) {
-      return nullptr;
-    }
 
-    const Mesh *editmesh_eval_final = BKE_object_get_editmesh_eval_final(ob_eval);
-    const Mesh *editmesh_eval_cage = BKE_object_get_editmesh_eval_cage(ob_eval);
+  switch (ob_eval->type) {
+    case OB_MESH: {
+      if (BKE_object_is_in_editmode(ob_eval)) {
+        if (edit_mode_type == SNAP_GEOM_EDIT) {
+          return nullptr;
+        }
+
+        const Mesh *editmesh_eval_final = BKE_object_get_editmesh_eval_final(ob_eval);
+        const Mesh *editmesh_eval_cage = BKE_object_get_editmesh_eval_cage(ob_eval);
 
-    if ((edit_mode_type == SNAP_GEOM_FINAL) && editmesh_eval_final) {
-      if (editmesh_eval_final->runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH) {
-        return nullptr;
-      }
-      me_eval = editmesh_eval_final;
-      use_hide = true;
-    }
-    else if ((edit_mode_type == SNAP_GEOM_CAGE) && editmesh_eval_cage) {
-      if (editmesh_eval_cage->runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH) {
-        return nullptr;
+        if ((edit_mode_type == SNAP_GEOM_FINAL) && editmesh_eval_final) {
+          if (editmesh_eval_final->runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH) {
+            return nullptr;
+          }
+          me_eval = editmesh_eval_final;
+          use_hide = true;
+        }
+        else if ((edit_mode_type == SNAP_GEOM_CAGE) && editmesh_eval_cage) {
+          if (editmesh_eval_cage->runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH) {
+            return nullptr;
+          }
+          me_eval = editmesh_eval_cage;
+          use_hide = true;
+        }
       }
-      me_eval = editmesh_eval_cage;
-      use_hide = true;
+      break;
     }
+    default:
+      break;
   }
   if (r_use_hide) {
     *r_use_hide = use_hide;
@@ -236,96 +208,31 @@ static void snap_editmesh_minmax(SnapObjectContext *sctx,
   }
 }
 
-static SnapData_Mesh *snap_object_data_mesh_get(SnapObjectContext *sctx,
-                                                Object *ob_eval,
-                                                const Mesh *me_eval,
-                                                bool use_hide)
+static void snap_object_data_mesh_get(SnapObjectContext *sctx,
+                                      Object *ob_eval,
+                                      const Mesh *me_eval,
+                                      bool use_hide,
+                                      BVHTreeFromMesh *r_treedata)
 {
-  SnapData_Mesh *sod;
-  bool init = false;
-
   const Span<MVert> verts = me_eval->verts();
   const Span<MEdge> edges = me_eval->edges();
   const Span<MPoly> polys = me_eval->polys();
   const Span<MLoop> loops = me_eval->loops();
 
-  if (std::unique_ptr<SnapData_Mesh> *sod_p = sctx->mesh_caches.lookup_ptr(ob_eval)) {
-    sod = sod_p->get();
-    bool is_dirty = false;
-    if (sod->treedata_mesh.tree && sod->treedata_mesh.cached &&
-        !bvhcache_has_tree(me_eval->runtime->bvh_cache, sod->treedata_mesh.tree)) {
-      /* The tree is owned by the Mesh and may have been freed since we last used. */
-      is_dirty = true;
-    }
-    else if (sod->bvhtree[0] && sod->cached[0] &&
-             !bvhcache_has_tree(me_eval->runtime->bvh_cache, sod->bvhtree[0])) {
-      /* The tree is owned by the Mesh and may have been freed since we last used. */
-      is_dirty = true;
-    }
-    else if (sod->bvhtree[1] && sod->cached[1] &&
-             !bvhcache_has_tree(me_eval->runtime->bvh_cache, sod->bvhtree[1])) {
-      /* The tree is owned by the Mesh and may have been freed since we last used. */
-      is_dirty = true;
-    }
-    else if (sod->treedata_mesh.looptri != me_eval->looptris().data()) {
-      is_dirty = true;
-    }
-    else if (sod->treedata_mesh.vert != verts.data()) {
-      is_dirty = true;
-    }
-    else if (sod->treedata_mesh.loop != loops.data()) {
-      is_dirty = true;
-    }
-    else if (sod->treedata_mesh.edge != edges.data()) {
-      is_dirty = true;
-    }
-    else if (sod->poly != polys.data()) {
-      is_dirty = true;
-    }
-
-    if (is_dirty) {
-      sod->clear();
-      init = true;
-    }
-  }
-  else {
-    if (ob_eval->type == OB_MESH) {
-      /* Any existing #SnapData_EditMesh is now invalid. */
-      sctx->editmesh_caches.remove(BKE_editmesh_from_object(ob_eval));
-    }
-
-    std::unique_ptr<SnapData_Mesh> sod_ptr = std::make_unique<SnapData_Mesh>();
-    sod = sod_ptr.get();
-    sctx->mesh_caches.add_new(ob_eval, std::move(sod_ptr));
-
-    init = true;
+  if (ob_eval->type == OB_MESH) {
+    /* Any existing #SnapData_EditMesh is now invalid. */
+    sctx->editmesh_caches.remove(BKE_editmesh_from_object(ob_eval));
   }
 
-  if (init) {
-    /* The BVHTree from looptris is always required. */
-    BLI_assert(sod->treedata_mesh.tree == nullptr);
-    BKE_bvhtree_from_mesh_get(&sod->treedata_mesh,
-                              me_eval,
-                              use_hide ? BVHTREE_FROM_LOOPTRI_NO_HIDDEN : BVHTREE_FROM_LOOPTRI,
-                              4);
-
-    BLI_assert(sod->treedata_mesh.vert == verts.data());
-    BLI_assert(!verts.data() || sod->treedata_mesh.vert_normals);
-    BLI_assert(sod->treedata_mesh.loop == loops.data());
-    BLI_assert(!polys.data() || sod->treedata_mesh.looptri);
-
-    sod->has_looptris = sod->treedata_mesh.tree != nullptr;
-
-    /* Required for snapping with occlusion. */
-    sod->treedata_mesh.edge = edges.data();
-    sod->poly = polys.data();
+  /* The BVHTree from looptris is always required. */
+  BKE_bvhtree_from_mesh_get(
+      r_treedata, me_eval, use_hide ? BVHTREE_FROM_LOOPTRI_NO_HIDDEN : BVHTREE_FROM_LOOPTRI, 4);
 
-    /* Start assuming that it has each of these element types. */
-    sod->has_loose_edge = true;
-    sod->has_loose_vert = true;
-  }
-
-  return sod;
+  BLI_assert(r_treedata->vert == verts.data());
+  BLI_assert(!verts.data() || r_treedata->vert_normals);
+  BLI_assert(r_treedata->loop == loops.data());
+  BLI_assert(!polys.data() || r_treedata->looptri);
+  BLI_assert(!r_treedata->tree || r_treedata->looptri);
 }
 
 /* Searches for the #Mesh_Runtime associated with the object that is most likely to be updated due
@@ -352,9 +259,6 @@ static SnapData_EditMesh *snap_object_data_editmesh_get(SnapObjectContext *sctx,
   SnapData_EditMesh *sod;
   bool init = false;
 
-  /* Any existing #SnapData_Mesh is now invalid. */
-  sctx->mesh_caches.remove(ob_eval);
-
   if (std::unique_ptr<SnapData_EditMesh> *sod_p = sctx->editmesh_caches.lookup_ptr(em)) {
     sod = sod_p->get();
     bool is_dirty = false;
@@ -411,15 +315,6 @@ static SnapData_EditMesh *snap_object_data_editmesh_get(SnapObjectContext *sctx,
   return sod;
 }
 
-static BVHTreeFromMesh *snap_object_data_mesh_treedata_get(SnapObjectContext *sctx,
-                                                           Object *ob_eval,
-                                                           const Mesh *me_eval,
-                                                           bool use_hide)
-{
-  SnapData_Mesh *sod = snap_object_data_mesh_get(sctx, ob_eval, me_eval, use_hide);
-  return &sod->treedata_mesh;
-}
-
 static BVHTreeFromEditMesh *snap_object_data_editmesh_treedata_get(SnapObjectContext *sctx,
                                                                    Object *ob_eval,
                                                                    BMEditMesh *em)
@@ -787,23 +682,22 @@ static bool raycastMesh(SnapObjectContext *sctx,
     len_diff = 0.0f;
   }
 
-  SnapData_Mesh *sod = snap_object_data_mesh_get(sctx, ob_eval, me_eval, use_hide);
+  BVHTreeFromMesh treedata;
+  snap_object_data_mesh_get(sctx, ob_eval, me_eval, use_hide, &treedata);
 
-  BVHTreeFromMesh *treedata = &sod->treedata_mesh;
-
-  if (treedata->tree == nullptr) {
+  if (treedata.tree == nullptr) {
     return retval;
   }
 
   float timat[3][3]; /* transpose inverse matrix for normals */
   transpose_m3_m4(timat, imat);
 
-  BLI_assert(treedata->raycast_callback != nullptr);
+  BLI_assert(treedata.raycast_callback != nullptr);
   if (r_hit_list) {
     RayCastAll_Data data;
 
-    data.bvhdata = treedata;
-    data.raycast_callback = treedata->raycast_callback;
+    data.bvhdata = &treedata;
+    data.raycast_callback = treedata.raycast_callback;
     data.obmat = obmat;
     data.timat = timat;
     data.len_diff = len_diff;
@@ -813,13 +707,8 @@ static bool raycastMesh(SnapObjectContext *sctx,
     data.hit_list = r_hit_list;
     data.retval = retval;
 
-    BLI_bvhtree_ray_cast_all(treedata->tree,
-                             ray_start_local,
-          

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list