Skip to content

Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.

Dynamic topology: no stale patch triangles when the patch buffers grow (#153)

islandmed2e544c12a34b
diff --git a/changes/fix-dyntopo-spike.md b/changes/fix-dyntopo-spike.md
new file mode 100644
index 00000000..8929dfbf
--- /dev/null
+++ b/changes/fix-dyntopo-spike.md
@@ -0,0 +1,15 @@
+---
+whats_new: **Dynamic topology** no longer shows a long spike and a patch of broken faces after zooming in and sculpting
+ (only the display was wrong: Undo or the next strokes put it right) (thanks @editordenis4-collab, #153).
+fixed: Dynamic topology: after strokes in several places (zooming in between them makes many more triangles), the
+ view could draw a long spike out of the mesh and a patch of dark, broken or missing faces until the next Undo or a
+ full redraw. When the renderer's buffers for the remeshed areas grew, the areas drawn before kept their vertices
+ but lost their triangles, which then drew whatever the new buffer held. The mesh itself was never damaged. Thanks
+ @editordenis4-collab (#153).
+testers:
+ - Any OS (Intel graphics especially): New Sphere, Dynamic on (Detail 50), X symmetry on, Wire on. Zoom in, make
+ short strokes in 8-10 different places, zoom in again and stroke more. No spike, no dark or missing faces.
+---
+Renderer::preparePatches marks every patch stale when uploadPatches is about to reallocate the patch buffers
+(patches > patchParts), so each patch's indices, edges and face groups are rebuilt and re-sent. Regression:
+tests/ui/dyntopo_patches.sfui reads the patch index buffers back (render_patch_errors) after each stroke.
diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cpp
index 3772ed00..9b902c09 100644
--- a/src/app/UiTest.cpp
+++ b/src/app/UiTest.cpp
@@ -1114,6 +1114,11 @@ double Demo::query(const std::string& name, bool& known) const {
return double(app_.renderer_.coarseDrawnClusters());
}
if (name == "stroke_programs") return double(app_.renderer_.strokePrograms()); // compiled before any stroke (warmed)
+ if (name == "render_patch_errors") { // the 3D view drawn once more now: dynamic-topology patch triangles wrong on the GPU
+ app_.benchmarkDrawMs(0);
+ return double(app_.renderer_.patchErrors(app_.doc_.parts[size_t(app_.doc_.activePart)].id, app_.displayedMesh()));
+ }
+ if (name == "render_patches") return double(app_.renderer_.patchCount(app_.doc_.parts[size_t(app_.doc_.activePart)].id));
if (name == "coarse_pending") return double(app_.renderer_.coarsePendingClusters()); // edited, levels not updated yet
if (name == "panel_drag_scrolls") return double(ui::DragScrolls()); // #100: drags that scrolled a panel
if (name == "panel_drag_scroll_y") return double(ui::DragScrollY()); // that panel's scroll after the last one
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index a368ce16..93592ada 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -1957,6 +1957,11 @@ bool Renderer::preparePatches(MeshSlot& s, const Mesh& mesh, bool wireframe) {
}
if (wireframe && !s.patchLinesOn)
for (MeshSlot::Patch& p : s.patches) p.stale = true; // (their edges weren't built)
+ // More patches than the buffers have room for: uploadPatches makes new, empty ones, so every patch's triangles,
+ // edges and face groups go up again, and those already uploaded kept only their vertices (#153: they drew
+ // whatever the new index buffer held, spikes and broken faces until the next full upload).
+ if (s.patches.size() > s.patchParts)
+ for (MeshSlot::Patch& p : s.patches) p.stale = true;
const bool hidden = mesh.hidden.size() == nt && nt > 0;
const bool groups = mesh.facegroups.size() == nt && nt > 0;
for (MeshSlot::Patch& p : s.patches) {
@@ -3361,6 +3366,49 @@ size_t Renderer::coarsePendingClusters() const {
return n;
}
+size_t Renderer::patchErrors(uint32_t id, const Mesh& mesh) {
+ size_t errors = 0;
+ const uint32_t nt = uint32_t(mesh.triangleCount());
+ const bool hidden = mesh.hidden.size() == nt && nt > 0;
+ std::vector<uint16_t> got;
+ std::vector<uint32_t> want;
+ for (MeshSlot& s : slots_) {
+ if (s.id != id) continue;
+ for (const MeshSlot::Patch& p : s.patches) {
+ if (p.stale || !p.uploaded) continue;
+ // The mesh vertices of the patch's triangles now, as preparePatches lists them.
+ want.clear();
+ auto add = [&](uint32_t t) {
+ if (t >= nt || (hidden && mesh.hidden[t])) return;
+ for (int c = 0; c < 3; ++c) want.push_back(mesh.triangles[t][c]);
+ };
+ if (p.key < kTailPatch) {
+ for (uint32_t i = 0; i < s.order.clusterTriangles(p.key); ++i) add(s.order.triangle(p.key, i));
+ } else {
+ const uint32_t first = s.layoutTriangles + (p.key - kTailPatch) * kPatchTriangles;
+ for (uint32_t t = first; t < first + kPatchTriangles && t < nt; ++t) add(t);
+ }
+ got.assign(p.indices, 0);
+ if (!got.empty()) {
+ gl::BindVertexArray(s.patchVao);
+ gl::GetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, GLintptr(size_t(p.part) * kPatchTriangles * 3 * sizeof(uint16_t)),
+ GLsizeiptr(got.size() * sizeof(uint16_t)), got.data());
+ gl::BindVertexArray(0);
+ }
+ if (got.size() != want.size()) {
+ errors += std::max(got.size(), want.size()) / 3;
+ continue;
+ }
+ for (size_t i = 0; i < got.size(); i += 3) {
+ bool same = true;
+ for (size_t c = i; c < i + 3; ++c) same = same && got[c] < p.verts.size() && p.verts[got[c]] == want[c];
+ errors += same ? 0 : 1;
+ }
+ }
+ }
+ return errors;
+}
+
void Renderer::markCoarseDirty(MeshSlot& s, uint32_t c) {
if (!s.coarse || c >= s.coarseDirty.size()) return;
s.coarseEdited = true;
diff --git a/src/render/Renderer.h b/src/render/Renderer.h
index 5b0c9cb0..37174917 100644
--- a/src/render/Renderer.h
+++ b/src/render/Renderer.h
@@ -256,6 +256,14 @@ public:
// are still to be updated (drawn in full until then).
size_t coarseDrawnClusters() const { return lastCoarseClusters_; }
size_t coarsePendingClusters() const;
+ // Dynamic topology patches (tests): the triangles of mesh slot `id`'s uploaded patches whose indices on the GPU
+ // (read back) aren't the mesh's triangles now.
+ size_t patchErrors(uint32_t id, const Mesh& mesh);
+ size_t patchCount(uint32_t id) const {
+ for (const MeshSlot& s : slots_)
+ if (s.id == id) return s.patches.size();
+ return 0;
+ }
// The stroke-only programs (predictive preview, draft normals) compiled so far: drawSlot warms them before a stroke.
int strokePrograms() const {
int n = 0;
diff --git a/tests/ui/dyntopo_patches.sfui b/tests/ui/dyntopo_patches.sfui
new file mode 100644
index 00000000..c60b44dc
--- /dev/null
+++ b/tests/ui/dyntopo_patches.sfui
@@ -0,0 +1,37 @@
+# Dynamic topology on a finer sphere (#153): short strokes in different places, with X symmetry, change more and more
+# of its clusters, which the renderer draws from patches. When the patches outgrow their GPU buffers (8, then 16) the
+# buffers are made again and every patch drawn before must be sent again: none may draw stale triangles (seen as a
+# long spike and broken faces until the next stroke or Undo). render_patch_errors reads the patches back from the GPU.
+run new_sphere subdivisions=5
+run dyntopo enabled=true detail=100
+click "Standard##brush.standard"
+expect_value symmetry_x == 1
+stroke 0.42 0.30 0.47 0.33
+expect_value render_patch_errors == 0
+stroke 0.42 0.62 0.47 0.66
+expect_value render_patch_errors == 0
+stroke 0.36 0.45 0.40 0.50
+expect_value render_patch_errors == 0
+stroke 0.45 0.20 0.48 0.24
+expect_value render_patch_errors == 0
+stroke 0.44 0.75 0.47 0.79
+expect_value render_patch_errors == 0
+stroke 0.33 0.36 0.36 0.40
+expect_value render_patch_errors == 0
+stroke 0.34 0.58 0.37 0.62
+expect_value render_patch_errors == 0
+stroke 0.30 0.48 0.33 0.52
+expect_value render_patch_errors == 0
+stroke 0.38 0.25 0.41 0.29
+expect_value render_patch_errors == 0
+stroke 0.38 0.70 0.41 0.74
+expect_value render_patch_errors == 0
+stroke 0.47 0.45 0.49 0.55
+expect_value render_patch_errors == 0
+stroke 0.32 0.28 0.35 0.31
+expect_value render_patch_errors == 0
+stroke 0.32 0.68 0.35 0.71
+expect_value render_patch_errors == 0
+expect_value render_patches > 8
+click "Wire##view.wire"
+shot wireframe