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.

Fast Big Brushes on Windows: exact normals caught up behind the brush every batch (FIFO), bigger sample jobs; SF_BENCH_PHASES

islandmeda291ca1721af
diff --git a/src/app/AsyncStroke.cpp b/src/app/AsyncStroke.cpp
index d50172b1..f4ae2df5 100644
--- a/src/app/AsyncStroke.cpp
+++ b/src/app/AsyncStroke.cpp
@@ -98,6 +98,9 @@ void App::StrokeSolver::loop() {
app.doc_.sculptor.endBatch(app.doc_.mesh, app.doc_.bvh);
// Nothing more waiting: the exact normals behind the brush catch up (a frame's share of them).
if (catchUp && !finalize) app.doc_.sculptor.catchUpNormals(app.doc_.mesh, app.doc_.bvh, catchUpLeaves);
+ // A share of the exact normals the brush has left behind, every batch: lifting the pen then waits for
+ // the last stretch only (they cost a tenth of what the dab-by-dab normals did over the stroke).
+ else if (!finalize) app.doc_.sculptor.catchUpBehind(app.doc_.mesh, app.doc_.bvh, 0.35);
spent += std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
}
app.dabView_ = nullptr;
diff --git a/src/app/ProofMode.cpp b/src/app/ProofMode.cpp
index 814b2826..9950adeb 100644
--- a/src/app/ProofMode.cpp
+++ b/src/app/ProofMode.cpp
@@ -544,6 +544,19 @@ void App::startProofMode() {
{"orbit", 0.0f, 10.0}};
else if (!p->scene.empty()) p->phases = standardPhases(p->strokeSeconds);
else p->phases = {{"stroke", 60.0f, p->strokeSeconds}, {"large brush stroke", 240.0f, p->strokeSeconds}, {"orbit", 0.0f, 3.0}};
+ // SF_BENCH_PHASES=smooth,240: only the phases whose names contain one of these (each on the fresh mesh's
+ // shape, not on what the phases before it left).
+ if (const char* only = std::getenv("SF_BENCH_PHASES"); only && *only) {
+ std::vector<std::string> keep;
+ for (std::string rest = only; !rest.empty();) {
+ const size_t comma = rest.find(',');
+ keep.push_back(rest.substr(0, comma));
+ rest = comma == std::string::npos ? std::string() : rest.substr(comma + 1);
+ }
+ std::erase_if(p->phases, [&](const PhaseSpec& ph) {
+ return std::none_of(keep.begin(), keep.end(), [&](const std::string& k) { return !k.empty() && std::string(ph.name).find(k) != std::string::npos; });
+ });
+ }
p->stage = p->cli ? ProofState::Stage::Announce : ProofState::Stage::Intro;
p->radiusBefore = radiusPx_;
p->strengthBefore = strength_;
diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cpp
index 87f3b155..a8a61b9f 100644
--- a/src/core/Sculpt.cpp
+++ b/src/core/Sculpt.cpp
@@ -765,46 +765,70 @@ void Sculptor::flushLazy(Mesh& mesh, const BVH& bvh, std::span<const uint32_t> l
}
void Sculptor::dropLazy() {
- if (lazyCount_ > 0) {
- std::vector<uint32_t> drop;
- lazyLeaves_.take(drop);
- lazyCount_ = 0;
- }
+ for (size_t i = lazyHead_; i < lazyLeaves_.size(); ++i) lazyFlag_[lazyLeaves_[i]] = 0;
+ for (uint32_t l : lastLazyLeaves_)
+ if (l < lazyFlag_.size()) lazyFlag_[l] = 0;
+ lazyLeaves_.clear();
+ lazyHead_ = 0;
+ lazyCount_ = 0;
lastLazyLeaves_.clear();
}
void Sculptor::addLazyLeaves(const BVH& bvh, std::span<const uint32_t> leaves) {
- if (leaves.empty()) return;
- if (lazyLeaves_.capacity() < bvh.nodeCount()) {
- std::vector<uint32_t> keep;
- if (lazyCount_ > 0) lazyLeaves_.take(keep);
- lazyLeaves_.resize(bvh.nodeCount());
- for (uint32_t l : keep) lazyLeaves_.insertLocal(l);
+ if (lazyFlag_.size() < bvh.nodeCount()) lazyFlag_.resize(bvh.nodeCount(), 0);
+ for (uint32_t l : leaves) {
+ if (lazyFlag_[l] == 0) {
+ lazyLeaves_.push_back(l);
+ ++lazyCount_;
+ }
+ lazyFlag_[l] = 2; // (moved by the batch being placed: catchUpBehind leaves it for now)
}
- lazyLeaves_.insertParallel(leaves.size(), 16384, [&](size_t b, size_t e, IndexBits& set) {
- for (size_t i = b; i < e; ++i) set.insertLocal(leaves[i]);
- });
- lazyCount_ += leaves.size();
+}
+
+// Takes up to `maxLeaves` pending leaves, oldest first, skipping (and keeping, at the back) those flagged 2 when
+// `skipLast`.
+static std::vector<uint32_t> takeLazy(std::vector<uint32_t>& list, size_t& head, std::vector<uint8_t>& flag, size_t& count,
+ size_t maxLeaves, bool skipLast) {
+ std::vector<uint32_t> out, kept;
+ const size_t end = list.size();
+ for (size_t i = head; i < end && out.size() < maxLeaves; ++i, ++head) {
+ const uint32_t l = list[i];
+ if (skipLast && flag[l] == 2) {
+ kept.push_back(l);
+ continue;
+ }
+ flag[l] = 0;
+ out.push_back(l);
+ --count;
+ }
+ list.insert(list.end(), kept.begin(), kept.end());
+ if (head > 4096 && head * 2 > list.size()) { // (drop the taken front now and then)
+ list.erase(list.begin(), list.begin() + std::ptrdiff_t(head));
+ head = 0;
+ }
+ return out;
}
void Sculptor::catchUpNormals(Mesh& mesh, const BVH& bvh, size_t maxLeaves) {
if (lazyCount_ == 0 || &mesh != lazyMesh_) return;
- std::vector<uint32_t> leaves;
- lazyLeaves_.take(leaves);
- lazyCount_ = 0;
- if (leaves.size() > maxLeaves) { // the rest stay pending
- for (size_t i = maxLeaves; i < leaves.size(); ++i) lazyLeaves_.insertLocal(leaves[i]);
- lazyCount_ = leaves.size() - maxLeaves;
- leaves.resize(maxLeaves);
- }
+ const std::vector<uint32_t> leaves = takeLazy(lazyLeaves_, lazyHead_, lazyFlag_, lazyCount_, maxLeaves, false);
flushLazy(mesh, bvh, leaves);
}
+void Sculptor::catchUpBehind(Mesh& mesh, const BVH& bvh, double share) {
+ if (lazyCount_ == 0 || &mesh != lazyMesh_) return;
+ const size_t budget = size_t(double(lastLazyLeaves_.size()) * share);
+ if (budget > 0) {
+ const std::vector<uint32_t> leaves = takeLazy(lazyLeaves_, lazyHead_, lazyFlag_, lazyCount_, budget, true);
+ flushLazy(mesh, bvh, leaves);
+ }
+ for (uint32_t l : lastLazyLeaves_) // (the next batch's own are what count as recent then)
+ if (l < lazyFlag_.size() && lazyFlag_[l] == 2) lazyFlag_[l] = 1;
+}
+
void Sculptor::flushDraft(Mesh& mesh, const BVH& bvh) {
if (lazyCount_ > 0 && &mesh == lazyMesh_) {
- std::vector<uint32_t> leaves;
- lazyLeaves_.take(leaves);
- lazyCount_ = 0;
+ const std::vector<uint32_t> leaves = takeLazy(lazyLeaves_, lazyHead_, lazyFlag_, lazyCount_, SIZE_MAX, false);
lastLazyLeaves_.clear();
flushLazy(mesh, bvh, leaves);
}
@@ -919,7 +943,7 @@ void Sculptor::dab(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p, Sym
}
dabVerts_.clear();
dabLeaves_.clear();
- lastLazyLeaves_.clear();
+ if (!batching_) lastLazyLeaves_.clear(); // (a batch's: catchUpBehind clears them after it)
for (const SymCopy& m : copies(sym)) {
frameWanted_ = m.identity(); // (the unmirrored pass records lastFrame)
const DabParams mp = m.identity() ? p : transformed(p, m);
diff --git a/src/core/Sculpt.h b/src/core/Sculpt.h
index 1ad18098..e761e5a3 100644
--- a/src/core/Sculpt.h
+++ b/src/core/Sculpt.h
@@ -272,6 +272,9 @@ public:
// `maxLeaves` of them (in leaf order). The app calls it when no dabs are waiting (the pen rests or slows).
bool normalsPending() const { return lazyCount_ > 0; }
void catchUpNormals(Mesh& mesh, const BVH& bvh, size_t maxLeaves);
+ // After each batch: the exact normals of up to `share` x as many leaves as the batch moved, of those the brush
+ // has left (the oldest first), so lifting the pen only waits for the last stretch of the stroke.
+ void catchUpBehind(Mesh& mesh, const BVH& bvh, double share);
// The last dab was big enough for a fused batch (the app then hands the solver every dab waiting at once).
bool fusesBatches() const { return adaptiveDabs && lastFusable_ && lastRegion_ >= kAdaptiveMinVertices; }
// Fused batches (SculptBatch.cpp): between beginBatch and endBatch, the dabs of a big brush (adaptive, one of
@@ -281,7 +284,10 @@ public:
// area plane comes from an even sample of the region, moved along dab by dab; each dab's centre, picked on
// the surface as the batch began, is moved along by the dabs before it. The normals, the BVH and the GPU's
// copy follow once a batch instead of once a dab. Any other dab places the waiting ones first.
- void beginBatch() { batching_ = true; }
+ void beginBatch() {
+ batching_ = true;
+ lastLazyLeaves_.clear();
+ }
void endBatch(Mesh& mesh, BVH& bvh);
size_t batchWaiting() const { return pending_.size(); }
bool draftActive() const { return draft_; }
@@ -405,8 +411,10 @@ private:
// Adaptive dabs: the leaves they moved (exact normals to come), the mesh and BVH they are of (endStroke
// flushes them when the app didn't), the leaves of the last dab,
// and per vertex the stroke whose cavity term curvature_ holds (taken once, before the stroke moved it).
- IndexBits lazyLeaves_; // (a set: a leaf many dabs moved is listed once)
- size_t lazyCount_ = 0; // leaves added since the set was last emptied (0: none pending)
+ std::vector<uint32_t> lazyLeaves_; // pending, oldest first (each once: lazyFlag_)
+ size_t lazyHead_ = 0; // lazyLeaves_[lazyHead_..] are the pending ones
+ std::vector<uint8_t> lazyFlag_; // per BVH node: 1 pending, 2 pending and moved by the last batch
+ size_t lazyCount_ = 0; // pending leaves
std::vector<uint32_t> lastLazyLeaves_;
void addLazyLeaves(const BVH& bvh, std::span<const uint32_t> leaves);
void dropLazy(); // (empties the set without the normals)
diff --git a/src/core/SculptBatch.cpp b/src/core/SculptBatch.cpp
index be7ca5d9..c7272d44 100644
--- a/src/core/SculptBatch.cpp
+++ b/src/core/SculptBatch.cpp
@@ -223,7 +223,7 @@ void Sculptor::flushBatch(Mesh& mesh, BVH& bvh) {
g.sampleNrm[i] = normals[g.sample[i]];
}
}
- constexpr size_t kBlock = 4096;
+ constexpr size_t kBlock = 32768; // (few jobs: waking a 12-thread pool costs more than these do)
std::vector<Frame> parts;
for (size_t j = 0; j < passes.size(); ++j) {
Pass& ps = passes[j];
@@ -311,7 +311,6 @@ void Sculptor::flushBatch(Mesh& mesh, BVH& bvh) {
}
if (curvatureStamp_.size() != mesh.vertexCount()) curvatureStamp_.assign(mesh.vertexCount(), 0);
const uint32_t stamp = uint32_t(strokeId_);
- lastLazyLeaves_.clear();
for (Group& g : groups) {
const size_t n = g.verts.size();
scratch_.resize(n);
diff --git a/tests/test_beta_boost.cpp b/tests/test_beta_boost.cpp
index 54a9132e..626f9536 100644
--- a/tests/test_beta_boost.cpp
+++ b/tests/test_beta_boost.cpp
@@ -32,7 +32,7 @@ struct StrokeResult {
// A stroke of `dabs` dabs across the face of `head` (0.15 radius apart, as the app spaces them), X symmetry.
StrokeResult strokeOn(const Mesh& head, BrushType type, float radius, bool adaptive, int dabs = 16, bool alpha = false,
- bool mask = false, float strength = 0.3f, int batch = 0) {
+ bool mask = false, float strength = 0.3f, int batch = 0, bool catchUp = false) {
Document d = makeDoc(head);
if (mask) { // a hard-edged mask over part of the stroke (such regions take the exact path)
d.mesh.ensureMask();
@@ -52,6 +52,7 @@ StrokeResult strokeOn(const Mesh& head, BrushType type, float radius, bool adapt
for (int i = 0; i < dabs; ++i) {
if (batch > 0 && i % batch == 0) { // (the solver's batches: picked on the surface as each began)
d.sculptor.endBatch(d.mesh, d.bvh);
+ if (catchUp) d.sculptor.catchUpBehind(d.mesh, d.bvh, 0.35); // (as the solver does after each batch)
d.sculptor.beginBatch();
}
DabParams p;
@@ -203,6 +204,19 @@ TEST(adaptive_dabs_analytic_normals_match_finite_differences) {
}
}
+TEST(adaptive_dabs_catch_up_behind_ends_exact) {
+ // Catching up the exact normals behind the brush after each batch ends where flushing them all at the end does.
+ const Mesh head = makeBenchHead(3'000'000);
+ const StrokeResult once = strokeOn(head, BrushType::Clay, 0.8f, true, 18, false, false, 0.3f, 3, false);
+ const StrokeResult spread = strokeOn(head, BrushType::Clay, 0.8f, true, 18, false, false, 0.3f, 3, true);
+ // (The caught-up normals feed the later dabs' planes, which then move the surface by a hair differently.)
+ const Difference d = compare(head, once.mesh, spread.mesh);
+ std::printf(" spread catch-up vs one flush: within %.2g (%.3f%% of the move), normals mean %.4f deg; pen-up flush %.0f vs %.0f ms\n",
+ double(d.position), 100.0 * double(d.position / std::max(d.moved, 1e-9f)), d.meanDeg, spread.flushMs, once.flushMs);
+ CHECK(d.position <= 0.005f * d.moved);
+ CHECK(d.meanDeg < 0.05);
+}
+
TEST(adaptive_dabs_leave_small_brushes_alone) {
// Small brushes, and the brushes that always take the exact path, come out bit for bit as before.
const Mesh head = makeBenchHead(1'500'000);