[Bf-blender-cvs] [f6a8d745c22] sculpt-dev: Sculpt: fix broken ray casting of original data in pbvh

Joseph Eagar noreply at git.blender.org
Wed Oct 6 10:47:16 CEST 2021


Commit: f6a8d745c22b2f77210dcc87cbe75aad699eb267
Author: Joseph Eagar
Date:   Wed Oct 6 01:46:25 2021 -0700
Branches: sculpt-dev
https://developer.blender.org/rBf6a8d745c22b2f77210dcc87cbe75aad699eb267

Sculpt: fix broken ray casting of original data in pbvh

Not sure if this ever worked.

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

M	release/scripts/startup/bl_ui/space_view3d_toolbar.py
M	source/blender/blenkernel/BKE_pbvh.h
M	source/blender/blenkernel/intern/brush_channel_define.h
M	source/blender/blenkernel/intern/brush_engine_presets.c
M	source/blender/blenkernel/intern/pbvh.c
M	source/blender/blenkernel/intern/pbvh_bmesh.c
M	source/blender/blenkernel/intern/pbvh_intern.h
M	source/blender/editors/sculpt_paint/sculpt.c
M	source/blender/editors/sculpt_paint/sculpt_dyntopo.c
M	source/blender/editors/sculpt_paint/sculpt_intern.h
M	source/blender/gpu/intern/gpu_buffers.c

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

diff --git a/release/scripts/startup/bl_ui/space_view3d_toolbar.py b/release/scripts/startup/bl_ui/space_view3d_toolbar.py
index 1c5c4eefa52..86fbbda83db 100644
--- a/release/scripts/startup/bl_ui/space_view3d_toolbar.py
+++ b/release/scripts/startup/bl_ui/space_view3d_toolbar.py
@@ -1278,6 +1278,15 @@ class VIEW3D_PT_sculpt_options(Panel, View3DPaintPanel):
         """
 
         col.separator()
+
+        UnifiedPaintPanel.channel_unified(layout.column(),
+            context,
+            brush,
+            "show_origco",
+            toolsettings_only=True, ui_editing=False)
+
+        col.separator()
+
         col.operator("sculpt.set_limit_surface")
 
 
diff --git a/source/blender/blenkernel/BKE_pbvh.h b/source/blender/blenkernel/BKE_pbvh.h
index b9975d3dd5b..34dc6df8830 100644
--- a/source/blender/blenkernel/BKE_pbvh.h
+++ b/source/blender/blenkernel/BKE_pbvh.h
@@ -505,6 +505,7 @@ void BKE_pbvh_bmesh_update_all_valence(PBVH *pbvh);
 void BKE_pbvh_bmesh_flag_all_disk_sort(PBVH *pbvh);
 bool BKE_pbvh_bmesh_mark_update_valence(PBVH *pbvh, SculptVertRef vertex);
 
+void BKE_pbvh_node_mark_original_update(PBVHNode *node);
 void BKE_pbvh_node_mark_update_tri_area(PBVHNode *node);
 void BKE_pbvh_update_all_tri_areas(PBVH *pbvh);
 void BKE_pbvh_node_mark_update(PBVHNode *node);
@@ -836,6 +837,7 @@ void BKE_pbvh_update_vert_boundary(int cd_dyn_vert,
                                    int cd_faceset_offset,
                                    int cd_vert_node_offset,
                                    int cd_face_node_offset,
+                                   int cd_vcol,
                                    struct BMVert *v,
                                    int symmetry);
 
diff --git a/source/blender/blenkernel/intern/brush_channel_define.h b/source/blender/blenkernel/intern/brush_channel_define.h
index fe23ca93fbc..e7149aefd3b 100644
--- a/source/blender/blenkernel/intern/brush_channel_define.h
+++ b/source/blender/blenkernel/intern/brush_channel_define.h
@@ -277,6 +277,7 @@ places in rna_engine_codebase are relevent:
   MAKE_FLOAT(hardness, "Hardness", "Brush falloff hardness", 0.0f, 0.0f, 1.0f)
   MAKE_FLOAT(tip_roundness, "Tip Roundness", "", 1.0f, 0.0f, 1.0f)
   MAKE_BOOL(accumulate, "Accumulate", "", false)
+  MAKE_BOOL_EX(show_origco, "Show OrigCo", "", false, BRUSH_CHANNEL_INHERIT)
   MAKE_ENUM(direction, "Direction", "", 0, {\
         {0, "ADD", "ADD", "Add", "Add effect of brush"},
         {1, "SUBTRACT", "REMOVE", "Subtract", "Subtract effect of brush"},
diff --git a/source/blender/blenkernel/intern/brush_engine_presets.c b/source/blender/blenkernel/intern/brush_engine_presets.c
index a22180c3d2b..cb74a32aa01 100644
--- a/source/blender/blenkernel/intern/brush_engine_presets.c
+++ b/source/blender/blenkernel/intern/brush_engine_presets.c
@@ -974,6 +974,8 @@ void BKE_brush_builtin_patch(Brush *brush, int tool)
   ADDCH(radius_unit);
   ADDCH(unprojected_radius);
 
+  ADDCH(show_origco);
+
   ADDCH(use_surface_falloff);
 
   if (!BRUSHSET_LOOKUP(chset, use_smoothed_rake)) {
@@ -1781,6 +1783,8 @@ void BKE_brush_check_toolsettings(Sculpt *sd)
   ADDCH(radius_unit);
   ADDCH(unprojected_radius);
 
+  ADDCH(show_origco);
+
   ADDCH(smooth_strength_factor);
   ADDCH(smooth_strength_projection);
 
diff --git a/source/blender/blenkernel/intern/pbvh.c b/source/blender/blenkernel/intern/pbvh.c
index 8edc13a423b..58ee35e86b8 100644
--- a/source/blender/blenkernel/intern/pbvh.c
+++ b/source/blender/blenkernel/intern/pbvh.c
@@ -153,26 +153,75 @@ void BBC_update_centroid(BBC *bbc)
 }
 
 /* Not recursive */
-static void update_node_vb(PBVH *pbvh, PBVHNode *node)
+static void update_node_vb(PBVH *pbvh, PBVHNode *node, int updateflag)
 {
+  if (!(updateflag & (PBVH_UpdateBB | PBVH_UpdateOriginalBB))) {
+    return;
+  }
+
+  /* cannot clear flag here, causes leaky pbvh */
+  // node->flag &= ~(updateflag & (PBVH_UpdateBB | PBVH_UpdateOriginalBB));
+
   BB vb;
+  BB orig_vb;
 
   BB_reset(&vb);
+  BB_reset(&orig_vb);
+
+  bool do_orig = updateflag | PBVH_UpdateOriginalBB;
+  bool do_normal = updateflag | PBVH_UpdateBB;
 
   if (node->flag & PBVH_Leaf) {
     PBVHVertexIter vd;
 
     BKE_pbvh_vertex_iter_begin (pbvh, node, vd, PBVH_ITER_ALL) {
-      BB_expand(&vb, vd.co);
+      if (do_normal) {
+        BB_expand(&vb, vd.co);
+      }
+
+      if (do_orig) {
+        MDynTopoVert *mv = pbvh->type == PBVH_BMESH ?
+                               BM_ELEM_CD_GET_VOID_P(vd.bm_vert, pbvh->cd_dyn_vert) :
+                               pbvh->mdyntopo_verts + vd.index;
+
+        if (mv->stroke_id != pbvh->stroke_id) {
+          BB_expand(&orig_vb, vd.co);
+        }
+        else {
+          BB_expand(&orig_vb, mv->origco);
+        }
+      }
     }
     BKE_pbvh_vertex_iter_end;
   }
   else {
-    BB_expand_with_bb(&vb, &pbvh->nodes[node->children_offset].vb);
-    BB_expand_with_bb(&vb, &pbvh->nodes[node->children_offset + 1].vb);
+    if (do_normal) {
+      BB_expand_with_bb(&vb, &pbvh->nodes[node->children_offset].vb);
+      BB_expand_with_bb(&vb, &pbvh->nodes[node->children_offset + 1].vb);
+    }
+
+    if (do_orig) {
+      BB_expand_with_bb(&orig_vb, &pbvh->nodes[node->children_offset].orig_vb);
+      BB_expand_with_bb(&orig_vb, &pbvh->nodes[node->children_offset + 1].orig_vb);
+    }
   }
 
-  node->vb = vb;
+  if (do_normal) {
+    node->vb = vb;
+  }
+
+  if (do_orig) {
+#if 0
+    float size[3];
+
+    sub_v3_v3v3(size, orig_vb.bmax, orig_vb.bmin);
+    mul_v3_fl(size, 0.05);
+
+    sub_v3_v3(orig_vb.bmin, size);
+    add_v3_v3(orig_vb.bmax, size);
+#endif
+    node->orig_vb = orig_vb;
+  }
 }
 
 // void BKE_pbvh_node_BB_reset(PBVHNode *node)
@@ -1288,15 +1337,7 @@ static void pbvh_update_BB_redraw_task_cb(void *__restrict userdata,
   PBVHNode *node = data->nodes[n];
   const int flag = data->flag;
 
-  if ((flag & PBVH_UpdateBB) && (node->flag & PBVH_UpdateBB)) {
-    /* don't clear flag yet, leave it for flushing later */
-    /* Note that bvh usage is read-only here, so no need to thread-protect it. */
-    update_node_vb(pbvh, node);
-  }
-
-  if ((flag & PBVH_UpdateOriginalBB) && (node->flag & PBVH_UpdateOriginalBB)) {
-    node->orig_vb = node->vb;
-  }
+  update_node_vb(pbvh, node, flag);
 
   if ((flag & PBVH_UpdateRedraw) && (node->flag & PBVH_UpdateRedraw)) {
     node->flag &= ~PBVH_UpdateRedraw;
@@ -1577,12 +1618,7 @@ static int pbvh_flush_bb(PBVH *pbvh, PBVHNode *node, int flag)
   update |= pbvh_flush_bb(pbvh, pbvh->nodes + node->children_offset, flag);
   update |= pbvh_flush_bb(pbvh, pbvh->nodes + node->children_offset + 1, flag);
 
-  if (update & PBVH_UpdateBB) {
-    update_node_vb(pbvh, node);
-  }
-  if (update & PBVH_UpdateOriginalBB) {
-    node->orig_vb = node->vb;
-  }
+  update_node_vb(pbvh, node, update);
 
   return update;
 }
@@ -1949,6 +1985,11 @@ BMesh *BKE_pbvh_get_bmesh(PBVH *pbvh)
 
 /***************************** Node Access ***********************************/
 
+void BKE_pbvh_node_mark_original_update(PBVHNode *node)
+{
+  node->flag |= PBVH_UpdateOriginalBB;
+}
+
 void BKE_pbvh_node_mark_update(PBVHNode *node)
 {
   node->flag |= PBVH_UpdateNormals | PBVH_UpdateBB | PBVH_UpdateOriginalBB |
@@ -2261,8 +2302,10 @@ void BKE_pbvh_raycast(PBVH *pbvh,
   RaycastData rcd;
 
   isect_ray_aabb_v3_precalc(&rcd.ray, ray_start, ray_normal);
+
   rcd.original = original;
   rcd.stroke_id = stroke_id;
+  pbvh->stroke_id = stroke_id;
 
   BKE_pbvh_search_callback_occluded(pbvh, ray_aabb_intersect, &rcd, cb, data);
 }
@@ -2352,6 +2395,7 @@ bool ray_face_intersection_depth_tri(const float ray_start[3],
                                      int *hit_count)
 {
   float depth_test;
+
   if (!isect_ray_tri_watertight_v3(ray_start, isect_precalc, t0, t1, t2, &depth_test, NULL)) {
     return false;
   }
@@ -3112,6 +3156,9 @@ void BKE_pbvh_draw_cb(PBVH *pbvh,
   MEM_SAFE_FREE(nodes);
 }
 
+// bad global from gpu_buffers.c
+extern bool pbvh_show_orig_co;
+
 void BKE_pbvh_draw_debug_cb(
     PBVH *pbvh,
     void (*draw_fn)(void *user_data, const float bmin[3], const float bmax[3], PBVHNodeFlags flag),
@@ -3122,7 +3169,12 @@ void BKE_pbvh_draw_debug_cb(
 
     int num = a + node->updategen;
 
-    draw_fn(&num, node->vb.bmin, node->vb.bmax, node->flag);
+    if (pbvh_show_orig_co) {
+      draw_fn(&num, node->orig_vb.bmin, node->orig_vb.bmax, node->flag);
+    }
+    else {
+      draw_fn(&num, node->vb.bmin, node->vb.bmax, node->flag);
+    }
   }
 }
 
diff --git a/source/blender/blenkernel/intern/pbvh_bmesh.c b/source/blender/blenkernel/intern/pbvh_bmesh.c
index a90e6df76c4..f103e53509d 100644
--- a/source/blender/blenkernel/intern/pbvh_bmesh.c
+++ b/source/blender/blenkernel/intern/pbvh_bmesh.c
@@ -254,6 +254,7 @@ static void pbvh_bmesh_node_finalize(PBVH *pbvh,
   n->bm_other_verts = BLI_table_gset_new("bm_other_verts");
 
   BB_reset(&n->vb);
+  BB_reset(&n->orig_vb);
   BMFace *f;
 
   TGSET_ITER (f, n->bm_faces) {
@@ -280,6 +281,7 @@ static void pbvh_bmesh_node_finalize(PBVH *pbvh,
       }
       /* Update node bounding box */
       BB_expand(&n->vb, v->co);
+      BB_expand(&n->orig_vb, mv->origco);
     } while ((l_iter = l_iter->next) != l_first);
 
     if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
@@ -291,8 +293,6 @@ static void pbvh_bmesh_node_finalize(PBVH *pbvh,
   BLI_assert(n->vb.bmin[0] <= n->vb.bmax[0] && n->vb.bmin[1] <= n->vb.bmax[1] &&
              n->vb.bmin[2] <= n->vb.bmax[2]);
 
-  n->orig_vb = n->vb;
-
   /* Build GPU buffers for new node and update vertex normals */
   BKE_pbvh_node_mark_rebuild_draw(n);
 
@@ -888,6 +888,7 @@ bool pbvh_bmesh_node_raycast(PBVH *pbvh,
 
   for (int i = 0; i < node->tribuf->tottri; i++) {
     PBVHTri *tri = tribuf->tris + i;
+
     BMVert *v1 = (BMVert *)tribuf->verts[tri->v[0]].i;
     BMVert *v2 = (BMVert *)tribuf->verts[tri->v[1]].i;
     BMVert *v3 = (BMVert *)tribuf->verts[tri->v[2]].i;
@@ -925,7 +926,7 @@ bool pbvh_bmesh_node_raycast(PBVH *pbvh,
 
       for (int j = 0; j < 3; j++) {
         BMVert *v = (BMVert *)tribuf->verts[tri->v[j]].i;
-        float *co = BKE_PBVH_DYNVERT(cd_dyn_vert, v)->origco;
+        float *co = use_original ? BKE_PBVH_DYNVERT(cd_dyn_vert, v)->origco : v->co;
 
         float dist = len_squared_v3v3(co, ray_start);
    

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list