[Bf-blender-cvs] [676137f043f] master: Sculpt/Paint: Use cached triangulation when building PBVH

Hans Goudey noreply at git.blender.org
Wed Nov 16 20:35:42 CET 2022


Commit: 676137f043f09b6d3726271fe1ff8e387dd52325
Author: Hans Goudey
Date:   Wed Nov 16 13:35:27 2022 -0600
Branches: master
https://developer.blender.org/rB676137f043f09b6d3726271fe1ff8e387dd52325

Sculpt/Paint: Use cached triangulation when building PBVH

This avoids recalculation of looptri derived triangulation whenever
switching to sculpt mode or whenever the PBVH is rebuilt, which can
happen after strokes in some situations. In my tests actually building
the PBVH is much more expensive (300ms), but this saves 6ms when
switching to sculpt mode and in other situations.

The cost is the possibility of higher memory usage because the cache
will live in the original main database mesh. However, the impact of
that will be smaller when the shared cache concept from D16204 is
applied to this data too.

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

M	source/blender/blenkernel/intern/paint.cc
M	source/blender/blenkernel/intern/pbvh.c

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

diff --git a/source/blender/blenkernel/intern/paint.cc b/source/blender/blenkernel/intern/paint.cc
index a39e53662aa..46e200c7d2c 100644
--- a/source/blender/blenkernel/intern/paint.cc
+++ b/source/blender/blenkernel/intern/paint.cc
@@ -2189,7 +2189,6 @@ static PBVH *build_pbvh_for_dynamic_topology(Object *ob)
 static PBVH *build_pbvh_from_regular_mesh(Object *ob, Mesh *me_eval_deform, bool respect_hide)
 {
   Mesh *me = BKE_object_get_original_mesh(ob);
-  const int looptris_num = poly_to_tri_count(me->totpoly, me->totloop);
   PBVH *pbvh = BKE_pbvh_new(PBVH_FACES);
   BKE_pbvh_respect_hide_set(pbvh, respect_hide);
 
@@ -2197,11 +2196,7 @@ static PBVH *build_pbvh_from_regular_mesh(Object *ob, Mesh *me_eval_deform, bool
   const Span<MPoly> polys = me->polys();
   const Span<MLoop> loops = me->loops();
 
-  MLoopTri *looptri = static_cast<MLoopTri *>(
-      MEM_malloc_arrayN(looptris_num, sizeof(*looptri), __func__));
-
-  BKE_mesh_recalc_looptri(
-      loops.data(), polys.data(), verts.data(), me->totloop, me->totpoly, looptri);
+  const Span<MLoopTri> looptris = me->looptris();
 
   BKE_pbvh_build_mesh(pbvh,
                       me,
@@ -2212,8 +2207,8 @@ static PBVH *build_pbvh_from_regular_mesh(Object *ob, Mesh *me_eval_deform, bool
                       &me->vdata,
                       &me->ldata,
                       &me->pdata,
-                      looptri,
-                      looptris_num);
+                      looptris.data(),
+                      looptris.size());
 
   pbvh_show_mask_set(pbvh, ob->sculpt->show_mask);
   pbvh_show_face_sets_set(pbvh, ob->sculpt->show_face_sets);
diff --git a/source/blender/blenkernel/intern/pbvh.c b/source/blender/blenkernel/intern/pbvh.c
index 24ea2de98f6..40c6ef3f114 100644
--- a/source/blender/blenkernel/intern/pbvh.c
+++ b/source/blender/blenkernel/intern/pbvh.c
@@ -959,10 +959,6 @@ void BKE_pbvh_free(PBVH *pbvh)
     }
   }
 
-  if (pbvh->looptri) {
-    MEM_freeN((void *)pbvh->looptri);
-  }
-
   if (pbvh->nodes) {
     MEM_freeN(pbvh->nodes);
   }



More information about the Bf-blender-cvs mailing list