[Bf-blender-cvs] [f482afadabe] temp_bmesh_multires: Refactored sculpt code to use a new type, SculptVertRef, that replaces much of the usage of integer indices. Meshes and grids simply store the index here, but bmesh stores a pointer to a BMVert. This greatly speeds up DynTopo by reducing the need to maintain flat pointer arrays in bmesh.

Joseph Eagar noreply at git.blender.org
Sun Oct 25 08:40:56 CET 2020


Commit: f482afadabeeba8cfc9ee00929a17dfb3d1f1789
Author: Joseph Eagar
Date:   Sun Oct 25 00:32:59 2020 -0700
Branches: temp_bmesh_multires
https://developer.blender.org/rBf482afadabeeba8cfc9ee00929a17dfb3d1f1789

Refactored sculpt code to use a new type, SculptVertRef, that replaces
much of the usage of integer indices.  Meshes and grids simply store the
index here, but bmesh stores a pointer to a BMVert.  This greatly speeds
up DynTopo by reducing the need to maintain flat pointer arrays in bmesh.

To prevent the accidental casting of ScuptVertexRef to indices and vice
versa SculptVertRef is defined as a struct:

typedef struct {intptr_t i} SculptVertRef;

There are also two functions to convert flat index indices to
SculptVertRefs and back:

ScultpVertRef BKE_pbvh_table_index_to_vertex(PBVH *pbvh, int index);
int BKE_pbvh_vertex_index_to_table(PBVH *pbvh, SculptVertRef *ref);

Note that these functions require the aforementioned maintanance of
flat pointer arrays in bmesh, so make sure to call
SCULPT_ensure_vertex_random_access().

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

M	source/blender/blenkernel/BKE_paint.h
M	source/blender/blenkernel/BKE_pbvh.h
M	source/blender/blenkernel/intern/pbvh.c
M	source/blender/blenkernel/intern/pbvh_bmesh.c
M	source/blender/blenlib/BLI_ghash.h
M	source/blender/blenlib/intern/BLI_ghash_utils.c
M	source/blender/editors/sculpt_paint/paint_cursor.c
M	source/blender/editors/sculpt_paint/paint_hide.c
M	source/blender/editors/sculpt_paint/paint_mask.c
M	source/blender/editors/sculpt_paint/sculpt.c
M	source/blender/editors/sculpt_paint/sculpt_automasking.c
M	source/blender/editors/sculpt_paint/sculpt_boundary.c
M	source/blender/editors/sculpt_paint/sculpt_cloth.c
M	source/blender/editors/sculpt_paint/sculpt_detail.c
M	source/blender/editors/sculpt_paint/sculpt_face_set.c
M	source/blender/editors/sculpt_paint/sculpt_filter_color.c
M	source/blender/editors/sculpt_paint/sculpt_filter_mask.c
M	source/blender/editors/sculpt_paint/sculpt_filter_mesh.c
M	source/blender/editors/sculpt_paint/sculpt_intern.h
M	source/blender/editors/sculpt_paint/sculpt_mask_expand.c
M	source/blender/editors/sculpt_paint/sculpt_multiplane_scrape.c
M	source/blender/editors/sculpt_paint/sculpt_paint_color.c
M	source/blender/editors/sculpt_paint/sculpt_pose.c
M	source/blender/editors/sculpt_paint/sculpt_smooth.c
M	source/blender/editors/sculpt_paint/sculpt_undo.c

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

diff --git a/source/blender/blenkernel/BKE_paint.h b/source/blender/blenkernel/BKE_paint.h
index 94bb3d7cb03..20af2f1ae0e 100644
--- a/source/blender/blenkernel/BKE_paint.h
+++ b/source/blender/blenkernel/BKE_paint.h
@@ -287,10 +287,10 @@ typedef struct SculptClothLengthConstraint {
    * point, position for a previous state). In that case, elem_index_a and elem_index_b should be
    * the same to avoid affecting two different vertices when solving the constraints.
    * *elem_position points to the position which is owned by the element. */
-  SculptVertRef elem_index_a;
+  int elem_index_a;
   float *elem_position_a;
 
-  SculptVertRef elem_index_b;
+  int elem_index_b;
   float *elem_position_b;
 
   float length;
@@ -343,7 +343,7 @@ typedef struct SculptPersistentBase {
 
 typedef struct SculptVertexInfo {
   /* Indexed by vertex, stores and ID of its topologically connected component. */
-  SculptVertRef *connected_component;
+  int *connected_component;
 
   /* Indexed by base mesh vertex index, stores if that vertex is a boundary. */
   BLI_bitmap *boundary;
@@ -352,6 +352,7 @@ typedef struct SculptVertexInfo {
 typedef struct SculptBoundaryEditInfo {
   /* Vertex index from where the topology propagation reached this vertex. */
   SculptVertRef original_vertex;
+  int original_vertex_i;
 
   /* How many steps were needed to reach this vertex from the boundary. */
   int num_propagation_steps;
@@ -362,13 +363,15 @@ typedef struct SculptBoundaryEditInfo {
 
 /* Edge for drawing the boundary preview in the cursor. */
 typedef struct SculptBoundaryPreviewEdge {
-  int v1;
-  int v2;
+  SculptVertRef v1;
+  SculptVertRef v2;
 } SculptBoundaryPreviewEdge;
 
 typedef struct SculptBoundary {
   /* Vertex indices of the active boundary. */
   SculptVertRef *vertices;
+  int *vertex_indices;
+
   int vertices_capacity;
   int num_vertices;
 
@@ -525,7 +528,7 @@ typedef struct SculptSession {
   int cd_origno_offset;
 
   /* Dynamic mesh preview */
-  int *preview_vert_index_list;
+  SculptVertRef *preview_vert_index_list;
   int preview_vert_index_count;
 
   /* Pose Brush Preview */
diff --git a/source/blender/blenkernel/BKE_pbvh.h b/source/blender/blenkernel/BKE_pbvh.h
index 2567407f10b..0e65d0c2a50 100644
--- a/source/blender/blenkernel/BKE_pbvh.h
+++ b/source/blender/blenkernel/BKE_pbvh.h
@@ -23,6 +23,7 @@
 
 #include "BLI_bitmap.h"
 #include "BLI_ghash.h"
+#include "BLI_compiler_compat.h"
 
 /* For embedding CCGKey in iterator. */
 #include "BKE_ccg.h"
@@ -32,7 +33,13 @@
 extern "C" {
 #endif
 
-typedef int64_t SculptVertRef;
+typedef struct {int64_t i;} SculptVertRef;
+
+BLI_INLINE SculptVertRef BKE_pbvh_make_vref(intptr_t i)
+{
+  SculptVertRef ret = {i};
+  return ret;
+}
 
 struct BMLog;
 struct BMesh;
@@ -421,7 +428,8 @@ typedef struct PBVHVertexIter {
   int gx;
   int gy;
   int i;
-  SculptVertRef index;
+  int index;
+  SculptVertRef vertex;
   bool respect_hide;
 
   /* grid */
@@ -469,7 +477,7 @@ void pbvh_vertex_iter_init(PBVH *pbvh, PBVHNode *node, PBVHVertexIter *vi, int m
     if (vi.grids) { \
       vi.width = vi.gridsize; \
       vi.height = vi.gridsize; \
-      vi.index = vi.grid_indices[vi.g] * vi.key.grid_area - 1; \
+      vi.vertex.i = vi.index = vi.grid_indices[vi.g] * vi.key.grid_area - 1; \
       vi.grid = vi.grids[vi.grid_indices[vi.g]]; \
       if (mode == PBVH_ITER_UNIQUE) { \
         vi.gh = vi.grid_hidden[vi.grid_indices[vi.g]]; \
@@ -488,6 +496,7 @@ void pbvh_vertex_iter_init(PBVH *pbvh, PBVHNode *node, PBVHVertexIter *vi, int m
           vi.mask = vi.key.has_mask ? CCG_elem_mask(&vi.key, vi.grid) : NULL; \
           vi.grid = CCG_elem_next(&vi.key, vi.grid); \
           vi.index++; \
+          vi.vertex.i++;\
           vi.visible = true; \
           if (vi.gh) { \
             if (BLI_BITMAP_TEST(vi.gh, vi.gy * vi.gridsize + vi.gx)) { \
@@ -539,13 +548,14 @@ void pbvh_vertex_iter_init(PBVH *pbvh, PBVHNode *node, PBVHVertexIter *vi, int m
             continue; \
           } \
           vi.bm_vert = bv;\
+          vi.vertex.i = (intptr_t)bv;\
+          vi.index = BM_elem_index_get(vi.bm_vert); \
           vi.visible = !BM_elem_flag_test_bool(vi.bm_vert, BM_ELEM_HIDDEN); \
           if (mode == PBVH_ITER_UNIQUE && !vi.visible) { \
             continue; \
           } \
           vi.co = vi.bm_vert->co; \
           vi.fno = vi.bm_vert->no; \
-          vi.index = BM_elem_index_get(vi.bm_vert); \
           vi.mask = BM_ELEM_CD_GET_VOID_P(vi.bm_vert, vi.cd_vert_mask_offset); \
         }
 
@@ -556,9 +566,10 @@ void pbvh_vertex_iter_init(PBVH *pbvh, PBVHNode *node, PBVHVertexIter *vi, int m
   ((void)0)
 
 #define BKE_pbvh_vertex_index_to_table(pbvh, v) \
-  (BKE_pbvh_type(pbvh) == PBVH_BMESH ? BM_elem_index_get((BMVert *)(v)) : (v))
+  (BKE_pbvh_type(pbvh) == PBVH_BMESH && v.i != -1 ? BM_elem_index_get((BMVert *)(v.i)) : (v.i))
 SculptVertRef BKE_pbvh_table_index_to_vertex(PBVH *pbvh, int idx);
 
+
 void BKE_pbvh_node_get_proxies(PBVHNode *node, PBVHProxyNode **proxies, int *proxy_count);
 void BKE_pbvh_node_free_proxies(PBVHNode *node);
 PBVHProxyNode *BKE_pbvh_node_add_proxy(PBVH *pbvh, PBVHNode *node);
diff --git a/source/blender/blenkernel/intern/pbvh.c b/source/blender/blenkernel/intern/pbvh.c
index be37c934857..a7770af97be 100644
--- a/source/blender/blenkernel/intern/pbvh.c
+++ b/source/blender/blenkernel/intern/pbvh.c
@@ -2173,7 +2173,7 @@ static bool pbvh_faces_node_raycast(PBVH *pbvh,
           if (j == 0 ||
               len_squared_v3v3(location, co[j]) < len_squared_v3v3(location, nearest_vertex_co)) {
             copy_v3_v3(nearest_vertex_co, co[j]);
-            *r_active_vertex_index = mloop[lt->tri[j]].v;
+            *r_active_vertex_index = BKE_pbvh_make_vref(mloop[lt->tri[j]].v);
             *r_active_face_index = lt->poly;
           }
         }
@@ -2258,8 +2258,8 @@ static bool pbvh_grids_node_raycast(PBVH *pbvh,
                                 len_squared_v3v3(location, nearest_vertex_co)) {
                 copy_v3_v3(nearest_vertex_co, co[j]);
 
-                *r_active_vertex_index = gridkey->grid_area * grid_index +
-                                         (y + y_it[j]) * gridkey->grid_size + (x + x_it[j]);
+                *r_active_vertex_index = BKE_pbvh_make_vref(gridkey->grid_area * grid_index +
+                                         (y + y_it[j]) * gridkey->grid_size + (x + x_it[j]));
               }
             }
           }
@@ -2322,7 +2322,7 @@ bool BKE_pbvh_node_raycast(PBVH *pbvh,
                                      face_normal);
       break;
     case PBVH_BMESH:
-      BM_mesh_elem_index_ensure(pbvh->bm, BM_VERT);
+      //BM_mesh_elem_index_ensure(pbvh->bm, BM_VERT);
       hit = pbvh_bmesh_node_raycast(node,
                                     ray_start,
                                     ray_normal,
@@ -2941,6 +2941,8 @@ void pbvh_vertex_iter_init(PBVH *pbvh, PBVHNode *node, PBVHVertexIter *vi, int m
   vi->no = NULL;
   vi->fno = NULL;
   vi->mvert = NULL;
+  vi->vertex.i = 0;
+  vi->index = 0;
 
   vi->respect_hide = pbvh->respect_hide;
   if (pbvh->respect_hide == false) {
@@ -3005,10 +3007,11 @@ bool pbvh_has_mask(PBVH *pbvh)
 
 SculptVertRef BKE_pbvh_table_index_to_vertex(PBVH *pbvh, int idx) {
   if (pbvh->type == PBVH_BMESH) {
-    return (SculptVertRef) pbvh->bm->vtable[idx];
+    SculptVertRef ref = {(intptr_t)pbvh->bm->vtable[idx]};
+    return ref;
   }
 
-  return (SculptVertRef)idx;
+  return BKE_pbvh_make_vref(idx);
 }
 bool pbvh_has_face_sets(PBVH *pbvh)
 {
@@ -3128,7 +3131,7 @@ void BKE_pbvh_ensure_proxyarray_indexmap(PBVH *pbvh, PBVHNode *node, GHash *vert
   int i = 0;
   BKE_pbvh_vertex_iter_begin(pbvh, node, vd, PBVH_ITER_UNIQUE)
   {
-    BLI_ghash_insert(gs, (void *)vd.index, (void *)i);
+    BLI_ghash_insert(gs, (void *)vd.vertex, (void *)i);
     i++;
   }
   BKE_pbvh_vertex_iter_end;
@@ -3159,7 +3162,7 @@ GHash *pbvh_build_vert_node_map(PBVH *pbvh, int mask)
 
     BKE_pbvh_vertex_iter_begin(pbvh, node, vd, PBVH_ITER_UNIQUE)
     {
-      BLI_ghash_insert(vert_node_map, (void *)vd.index, (void *)i);
+      BLI_ghash_insert(vert_node_map, (void *)vd.vertex, (void *)i);
     }
     BKE_pbvh_vertex_iter_end;
   }
@@ -3257,7 +3260,7 @@ void BKE_pbvh_ensure_proxyarray(SculptSession *ss,
   int i = 0;
   BKE_pbvh_vertex_iter_begin(pbvh, node, vd, PBVH_ITER_UNIQUE)
   {
-    BLI_ghash_insert(gs, (void *)vd.index, (void *)i);
+    BLI_ghash_insert(gs, (void *)vd.vertex, (void *)i);
     i++;
   }
   BKE_pbvh_vertex_iter_end;
@@ -3269,7 +3272,7 @@ void BKE_pbvh_ensure_proxyarray(SculptSession *ss,
       p->ownerco[i] = vd.co;
     }
     if (updatemask & PV_INDEX) {
-      p->index[i] = vd.index;
+      p->index[i] = vd.vertex;
     }
     if (updatemask & PV_OWNERNO) {
       p->ownerno[i] = vd.no;
@@ -3297,18 +3300,18 @@ void BKE_pbvh_ensure_proxyarray(SculptSession *ss,
       int j = 0;
       SculptVertexNeighborIter ni;
 
-      SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, vd.index, ni) {
+      SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, vd.vertex, ni) {
         if (j >= MAX_PROXY_NEIGHBORS - 1) {
           break;
         }
 
         ProxyKey key;
 
-        int *pindex = (int *)BLI_ghash_lookup_p(gs, (void *)ni.index);
+        int *pindex = (int *)BLI_ghash_lookup_p(gs, (void *)ni.vertex);
 
         if (!pindex) {
           if (vert_node_map) {
-            int *nindex = BLI_ghash_lookup_p(vert_node_map, (void *)ni.index);
+            int *nindex = BLI_ghash_lookup_p(vert_node_map, (void *)ni.vertex);
 
             if (!nindex) {
               continue;
@@ -3316,7 +3319,7 @@ void BKE_pbvh_ensure_proxyarray(SculptSession *ss,
 
             PBVHNode *node2 = pbvh->nodes + *nindex;
             if (node2->proxyverts.indexmap) {
-              pindex = (int *)BLI_ghash_lookup_p(node2->proxyverts.indexmap, (void *)ni.index);
+              pindex = (int *)BLI_ghash_lookup_p(node2->proxyverts.indexmap, (void *)ni.vertex);
             }
 
             if (!pindex) {
diff --git a/source/blender/blenkernel/intern/pbvh_bmesh.c b/source/blender/blenkernel/intern/pbvh_bmesh.c
index d6c112a0ef3..eb8cb81fdb0 100644
--- a/source/blender/blenkernel/intern/pbvh_bmesh.c
+++ b/source/blender/blenkernel/intern/pbvh_bmesh.c

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list