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.

Heavy-mesh strokes: parallel BVH ancestor refit, a 4-dab backlog floor, no full re-upload on press or for big brushes

islandmedf5bc36ec0550
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 37f417b7..8b19c408 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -580,6 +580,7 @@ constexpr int kCaretTickMs = 530; // a text field has the keyboard: redraw
constexpr double kAfterChangeSeconds = 0.25; // a poller changed something: draw for this long
constexpr double kStrokeBudgetSeconds = 0.010; // a frame's share of placing a stroke's dabs; the rest waits for the next
constexpr double kStrokeBacklogSeconds = 0.05; // queued dabs beyond this much work are thinned (strokeTo)
+constexpr size_t kStrokeBacklogMinDabs = 4; // ... but never fewer dabs than this waiting
} // namespace
@@ -1127,7 +1128,9 @@ void App::beginStroke(float x, float y, const RayHit& hit) {
drag_ = Drag::Stroke;
playingLayer_ = -1;
if (strokeType_ == StrokeType::DragRect || strokeType_ == StrokeType::DragDot) doc_.sculptor.holdTopology(); // (they re-stamp)
- clearLods();
+ // (Only when there are LODs: clearing forgets which mesh was shown, so the next frame sent the whole mesh
+ // again, 0.3 s at 21M triangles before the first dab showed.)
+ if (!lods_.empty() || previewLod_ >= 0) clearLods();
beginPaintStroke();
if (doc_.sculptor.dyntopo.enabled && strokeSculpt_ && !dyntopoBlocker(doc_.mesh).empty())
status_ = "Dynamic is on, but this SubTool has subdivision levels: sculpting without remeshing";
@@ -1240,17 +1243,15 @@ void App::strokeTo(float x, float y) {
if (interactiveStrokes_) {
// Too far behind the pointer: place every second waiting dab only (a wider spacing under load). The limit
// is in time, not dabs: what the last dabs cost says how many are kStrokeBacklogSeconds of work, and never
- // fewer than 24. A light mesh places hundreds of dabs a frame, so a small brush moved fast keeps its spacing
- // (several input samples arrive before each frame). SF_STROKE_BACKLOG=0 never drops dabs; a number is a
- // fixed limit in dabs.
+ // fewer than kStrokeBacklogMinDabs. A light mesh places hundreds of dabs a frame, so a small brush moved
+ // fast keeps its spacing (several input samples arrive before each frame); on a heavy mesh the brush stays
+ // near the pointer (a floor of 24 dabs was a second of work behind it at 21M triangles, and releasing
+ // placed all of them). SF_STROKE_BACKLOG=0 never drops dabs; a number is a fixed limit in dabs.
static const long fixedLimit = [] {
const char* env = std::getenv("SF_STROKE_BACKLOG");
return env && *env ? std::max(0L, std::strtol(env, nullptr, 10)) : -1L;
}();
- size_t limit = fixedLimit >= 0 ? size_t(fixedLimit) : size_t(24);
- if (fixedLimit < 0 && dabSeconds_ > 0.0)
- limit = std::max(limit, size_t(std::min(kStrokeBacklogSeconds / dabSeconds_, 1e6)));
- dabQueue_.thin(limit);
+ dabQueue_.thin(strokeBacklogLimit(dabSeconds_, fixedLimit, kStrokeBacklogSeconds, kStrokeBacklogMinDabs));
} else {
drainStrokeDabs(0.0); // scripts, UI tests and recordings place every dab at once, as before
}
diff --git a/src/core/BVH.cpp b/src/core/BVH.cpp
index 2e961938..c2eb102d 100644
--- a/src/core/BVH.cpp
+++ b/src/core/BVH.cpp
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/BVH.h"
+#include "core/AtomicRef.h"
#include "core/ThreadPool.h"
#include <algorithm>
@@ -13,6 +14,7 @@ namespace sf {
namespace {
constexpr uint32_t kLeafSize = 16;
+constexpr size_t kParallelRefitLeaves = 4096; // fewer: the serial walk up is cheaper than waking threads
bool rayBox(const AABB& b, const glm::vec3& o, const glm::vec3& invDir, float tMax) {
glm::vec3 t0 = (b.min - o) * invDir;
@@ -365,6 +367,10 @@ void BVH::refitLeaves(const Mesh& mesh, std::span<const uint32_t> leaves) {
poolFor(leaves.size(), [&](size_t b, size_t e) {
for (size_t i = b; i < e; ++i) nodes_[leaves[i]].box = leafBox(mesh, nodes_[leaves[i]]);
}, 512);
+ if (leaves.size() >= kParallelRefitLeaves) {
+ refitAncestors(leaves);
+ return;
+ }
// Grow ancestors until one already contains the new bounds (a union, so the order doesn't matter).
for (uint32_t li : leaves) {
AABB box = nodes_[li].box;
@@ -380,6 +386,75 @@ void BVH::refitLeaves(const Mesh& mesh, std::span<const uint32_t> leaves) {
}
}
+// A big brush refits hundreds of thousands of leaves, and walking each one's ancestors on one thread cost more
+// than the leaves themselves (13 ms a dab at 21M triangles). Here the ancestors are flagged in parallel (each once:
+// the thread that flags a node first goes on up, the others stop there), then recomputed from their children:
+// bottom-up within subtrees taken in parallel, then the few nodes above those subtrees. Every box is the union
+// of its children's (tighter than growing the old one), so the result doesn't depend on the thread count.
+void BVH::refitAncestors(std::span<const uint32_t> leaves) {
+ if (nodeDirty_.size() < nodes_.size()) nodeDirty_.resize(nodes_.size(), 0); // (all 0 between calls)
+ poolFor(leaves.size(), [&](size_t b, size_t e) {
+ for (size_t i = b; i < e; ++i)
+ for (uint32_t ni = leaves[i]; ni != 0;) {
+ ni = nodes_[ni].parent;
+ if (AtomicRef<uint8_t>(nodeDirty_[ni]).exchange(1, std::memory_order_relaxed)) break;
+ }
+ }, 1024);
+ auto inner = [&](uint32_t ni) { return nodes_[ni].count == 0 && nodes_[ni].first != kNoLeaf; };
+ auto dirtyInner = [&](uint32_t ni) { return inner(ni) && nodeDirty_[ni]; };
+ auto recompute = [&](uint32_t ni) {
+ Node& n = nodes_[ni];
+ AABB box = nodes_[n.first].box;
+ box.expand(nodes_[n.first + 1].box);
+ n.box = box;
+ nodeDirty_[ni] = 0;
+ };
+ // The subtrees: dirty nodes split breadth first until there are enough; the split ones are done last.
+ constexpr size_t kTasks = 128;
+ std::vector<uint32_t>& tasks = queryTasks_;
+ std::vector<uint32_t>& next = queryNext_;
+ std::vector<uint32_t> aboveNodes;
+ tasks.clear();
+ if (dirtyInner(0)) tasks.push_back(0);
+ for (bool split = true; split && tasks.size() < kTasks;) {
+ split = false;
+ next.clear();
+ for (uint32_t ni : tasks) {
+ const uint32_t c = nodes_[ni].first;
+ if (!dirtyInner(c) && !dirtyInner(c + 1)) {
+ next.push_back(ni);
+ continue;
+ }
+ split = true;
+ aboveNodes.push_back(ni);
+ for (uint32_t k : {c, c + 1})
+ if (dirtyInner(k)) next.push_back(k);
+ }
+ tasks.swap(next);
+ }
+ // Each subtree post-order: a node once both its dirty children are done.
+ poolFor(tasks.size(), [&](size_t b, size_t e) {
+ std::vector<std::pair<uint32_t, bool>> stack;
+ for (size_t t = b; t < e; ++t) {
+ stack.assign(1, {tasks[t], false});
+ while (!stack.empty()) {
+ auto [ni, childrenDone] = stack.back();
+ stack.pop_back();
+ if (childrenDone) {
+ recompute(ni);
+ continue;
+ }
+ stack.push_back({ni, true});
+ const uint32_t c = nodes_[ni].first;
+ for (uint32_t k : {c, c + 1})
+ if (dirtyInner(k)) stack.push_back({k, false});
+ }
+ }
+ }, 1, 2);
+ // Then the nodes above them, deepest first (they were split in breadth-first order).
+ for (auto it = aboveNodes.rbegin(); it != aboveNodes.rend(); ++it) recompute(*it);
+}
+
void BVH::editLeaf(uint32_t leaf) {
if (leafCap_.size() < nodes_.size()) { // the first edit: every leaf full
const size_t from = leafCap_.size();
@@ -528,7 +603,7 @@ bool BVH::degraded() const {
size_t BVH::memoryBytes() const {
auto bytes = [](const auto& v) { return v.capacity() * sizeof(v[0]); };
return bytes(nodes_) + bytes(triIndices_) + bytes(triLeaf_) + bytes(leafCap_) + bytes(editedLeaves_) + bytes(leafEdited_) + vertBits_.memoryBytes() + leafBits_.memoryBytes() +
- bytes(queryLeaves_) + bytes(scratchLeaves_) + bytes(stack_);
+ bytes(queryLeaves_) + bytes(scratchLeaves_) + bytes(stack_) + bytes(nodeDirty_);
}
void BVH::refitAll(const Mesh& mesh) {
diff --git a/src/core/BVH.h b/src/core/BVH.h
index 097154e3..4b391268 100644
--- a/src/core/BVH.h
+++ b/src/core/BVH.h
@@ -29,8 +29,9 @@ public:
void queryVertices(const Mesh& mesh, const glm::vec3& center, float radius, std::vector<uint32_t>& out) const;
// Appends every triangle whose bounds touch the sphere (cleared first; hidden ones included).
void queryTriangles(const Mesh& mesh, const glm::vec3& center, float radius, std::vector<uint32_t>& out) const;
- // Updates bounds after the given vertices moved. Bounds only ever grow
- // conservatively for ancestors, which keeps queries correct.
+ // Updates bounds after the given vertices moved. A few leaves grow their ancestors' bounds (conservatively,
+ // which keeps queries correct); many leaves (a big brush) recompute their ancestors from their children, in
+ // parallel.
void refit(const Mesh& mesh, std::span<const uint32_t> movedVerts);
// refit() after only vertices the last queryVertices() returned moved (brush dabs): the query already
// found the leaves holding them, so this skips looking them up.
@@ -83,6 +84,7 @@ private:
size_t unusedEntries_ = 0, editsSinceBuild_ = 0;
static constexpr uint32_t kNoLeaf = ~0u;
void editLeaf(uint32_t leaf);
+ void refitAncestors(std::span<const uint32_t> leaves); // refitLeaves for many leaves, in parallel
void splitLeaf(const Mesh& mesh, uint32_t leaf);
mutable IndexBits vertBits_; // queryVertices: the vertices found so far
@@ -90,6 +92,7 @@ private:
mutable std::vector<uint32_t> queryTasks_, queryNext_;
mutable std::vector<std::vector<uint32_t>> queryParts_;
IndexBits leafBits_; // refit: the leaves to recompute
+ std::vector<uint8_t> nodeDirty_; // refitAncestors: the inner nodes to recompute (all 0 between calls)
std::vector<uint32_t> scratchLeaves_;
mutable std::vector<uint32_t> stack_;
};
diff --git a/src/core/StrokePath.h b/src/core/StrokePath.h
index ed969ad3..9a61e71d 100644
--- a/src/core/StrokePath.h
+++ b/src/core/StrokePath.h
@@ -141,6 +141,17 @@ private:
float residual_ = 0.0f;
};
+// How many dabs may wait to be placed before the queue is thinned: about `backlogSeconds` of work at what a dab
+// costs now (dabSeconds, 0 = not measured yet), never fewer than `minDabs`; `fixed` >= 0 is a fixed limit
+// instead (SF_STROKE_BACKLOG). A light mesh keeps hundreds of dabs (a fast small brush keeps its spacing); a heavy
+// one only a few, so the brush stays near the pointer and releasing doesn't place a second of waiting dabs.
+inline size_t strokeBacklogLimit(double dabSeconds, long fixed = -1, double backlogSeconds = 0.05, size_t minDabs = 4) {
+ if (fixed >= 0) return size_t(fixed);
+ size_t limit = minDabs;
+ if (dabSeconds > 0.0) limit = std::max(limit, size_t(std::min(backlogSeconds / dabSeconds, 1e6)));
+ return limit;
+}
+
// Dabs waiting to be placed.
class DabQueue {
public:
diff --git a/src/core/UploadOrder.cpp b/src/core/UploadOrder.cpp
index 2f2309aa..0da2558b 100644
--- a/src/core/UploadOrder.cpp
+++ b/src/core/UploadOrder.cpp
@@ -279,8 +279,10 @@ void UploadOrder::dirtyRuns(const Mesh& mesh, std::vector<std::pair<uint32_t, ui
if (mesh.dirtyBlocks[b])
for (size_t w = b * kWordsPerBlock; w < (b + 1) * kWordsPerBlock && w < mesh.dirtyBits.size(); ++w)
dirty += size_t(std::popcount(mesh.dirtyBits[w]));
- if (dirty * 4 > nv || mesh.dirtyBits.size() * 64 < nv) {
- out.emplace_back(0, ns); // a good part of the mesh (or no per-vertex marks): everything, in one go
+ if (dirty * 4 > nv * 3 || mesh.dirtyBits.size() * 64 < nv) {
+ // Most of the mesh (or no per-vertex marks): everything, in one go. (Above a quarter, as before, a large
+ // brush at 21M triangles re-packed the two thirds it hadn't touched every frame: 78 ms against 14-30.)
+ out.emplace_back(0, ns);
return;
}
auto mark = [&](uint32_t slot) {
diff --git a/tests/test_performance.cpp b/tests/test_performance.cpp
index 53a48e22..8fba40cc 100644
--- a/tests/test_performance.cpp
+++ b/tests/test_performance.cpp
@@ -298,6 +298,42 @@ TEST(index_bits_list_members_in_order) {
CHECK(std::is_sorted(a.begin(), a.end()));
}
+TEST(bvh_refit_of_many_leaves_in_parallel) {
+ // A big brush refits so many leaves that their ancestors are recomputed in parallel (BVH::refitAncestors):
+ // the tree then finds exactly what brute force does, the same on one thread, and after the vertices move
+ // back (the boxes shrink) as well.
+ Document d = sphereDoc(7);
+ const std::vector<glm::vec3> start = d.mesh.positions;
+ std::vector<uint32_t> moved;
+ const glm::vec3 c(0.0f, 0.0f, 1.0f);
+ d.bvh.queryVertices(d.mesh, c, 1.3f, moved);
+ CHECK(d.bvh.queriedLeaves().size() > 4096); // (the parallel path)
+ auto check = [&](BVH& bvh) {
+ std::vector<uint32_t> found;
+ bool ok = true;
+ for (int i = 0; i < 24; ++i) {
+ const glm::vec3 q = glm::normalize(glm::vec3(std::sin(float(i)), std::cos(float(i) * 1.3f), 0.7f)) * 1.05f;
+ bvh.queryVertices(d.mesh, q, 0.12f, found);
+ size_t brute = 0;
+ for (const glm::vec3& p : d.mesh.positions) brute += glm::distance(p, q) <= 0.12f;
+ RayHit h;
+ const bool hit = bvh.raycast(d.mesh, q * 3.0f, -q, h);
+ ok = ok && found.size() == brute && hit;
+ }
+ return ok;
+ };
+ for (uint32_t v : moved) d.mesh.positions[v] *= 1.0f + 0.15f * (1.0f + std::sin(float(v) * 0.01f));
+ BVH serialTree = d.bvh;
+ d.bvh.refitQueried(d.mesh);
+ serially([&] { serialTree.refit(d.mesh, moved); });
+ CHECK(check(d.bvh));
+ CHECK(check(serialTree));
+ // Back where they were: the boxes shrink.
+ d.mesh.positions = start;
+ d.bvh.refit(d.mesh, moved);
+ CHECK(check(d.bvh));
+}
+
TEST(bvh_query_lists_inside_vertices_in_order) {
Document d = sphereDoc(7);
std::vector<uint32_t> found, brute;
diff --git a/tests/test_stroke_path.cpp b/tests/test_stroke_path.cpp
index a31a1550..3efa7c7e 100644
--- a/tests/test_stroke_path.cpp
+++ b/tests/test_stroke_path.cpp
@@ -180,3 +180,21 @@ TEST(dab_queue_under_the_limit_is_untouched) {
one.push({1.0f, 1.0f, 1.0f});
CHECK(one.thin(1) == 0 && one.size() == 1);
}
+
+TEST(stroke_backlog_keeps_a_heavy_brush_near_the_pointer) {
+ // About 50 ms of dabs may wait: a 45 ms dab on a 21M-triangle mesh keeps the floor of 4 (0.9.6 kept 24, a
+ // second behind the pointer), a cheap dab keeps hundreds so a fast small brush keeps its spacing.
+ CHECK(strokeBacklogLimit(0.045) == 4);
+ CHECK(strokeBacklogLimit(0.0001) == 500);
+ CHECK(strokeBacklogLimit(0.0) == 4); // not measured yet
+ CHECK(strokeBacklogLimit(0.045, 0) == 0); // SF_STROKE_BACKLOG=0: never thinned
+ CHECK(strokeBacklogLimit(0.0001, 10) == 10); // a fixed limit
+ // Thinned to the limit, the newest dab stays.
+ DabQueue q;
+ for (int i = 0; i < 30; ++i) q.push(PathDab{float(i), 0.0f, 1.0f});
+ q.thin(strokeBacklogLimit(0.045));
+ CHECK(q.size() <= 4 && !q.empty());
+ PathDab last{};
+ while (!q.empty()) last = q.pop();
+ CHECK(last.x == 29.0f);
+}