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.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
Background dabs: big brushes on dense meshes no longer stall the view
islandmedb3725142bd93
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7bd3a956..09a4b016 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,11 @@ All notable changes to SculptForge are listed here. The format follows## [Unreleased]### Changed+- Large brushes on dense meshes no longer stall the view: from 2 million vertices a stroke's dabs are placed on the+ stroke thread (the same dabs, in the same order, as the frame loop places them; no preview), so a frame keeps+ drawing while a 240 px dab moves millions of vertices, and a light batch is still waited for within the frame.+ Standard scene on an M1 Pro, 240 px brush: frame p99 139 -> ~31 ms at 15M and 306 -> ~33 ms at 33M, worst stall+ 325 -> ~120 ms, pen lift 162 -> 0 ms; Clay large p99 139 -> ~31 ms at 33M. `SF_BACKGROUND_DABS=0` turns it off.- The membership card at launch is redesigned: the cube mark and the promise up top, the three memberships side byside with their perks checked off and one bold button each (Insider highlighted), and the members' Discord in thefooter. It takes the current look's colours and opens in 150 ms. "Maybe later" now hides it for 30 days, updatesdiff --git a/CMakeLists.txt b/CMakeLists.txtindex 07d96b91..681932b5 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -660,6 +660,11 @@ if(SCULPTFORGE_BUILD_TESTS)# pointer's circle (#19).add_test(NAME ui_stroke_path COMMAND sculptforge --run "new_sphere" --measure-stroke 1.5)set_tests_properties(ui_stroke_path PROPERTIES LABELS ui TIMEOUT 60 ENVIRONMENT "SF_NO_UPDATE_CHECK=1")+ # Background dabs (a heavy mesh's stroke on the solver thread, no preview), forced on a light sphere: replaying+ # the stroke in the frame loop gives the same mesh, bit for bit, and its undo the mesh it started from.+ add_test(NAME ui_stroke_background COMMAND sculptforge --run "new_sphere" --measure-stroke 1.5)+ set_tests_properties(ui_stroke_background PROPERTIES LABELS ui TIMEOUT 60+ ENVIRONMENT "SF_NO_UPDATE_CHECK=1;SF_MEASURE_VERIFY=1;SF_BACKGROUND_DABS=1")# Predictive strokes with the brush size changing every frame mid-stroke: each batch of dabs keeps the size it# was handed (no data race with the solver thread), replaying the stroke without the thread gives the same# mesh, bit for bit, and its undo the mesh it started from.diff --git a/docs/guide/whats-new.md b/docs/guide/whats-new.mdindex 3d09e1df..f551b257 100644--- a/docs/guide/whats-new.md+++ b/docs/guide/whats-new.md@@ -5,6 +5,13 @@ chapter opens by itself the first time you start a new version; you can always f**Help > What's New**. Every detail is in the[changelog](https://github.com/islandmed/SculptForge/blob/main/CHANGELOG.md).+## Version 0.10.8++- **Big brushes stay smooth on dense meshes**: on models from about 4 million triangles a stroke's dabs are worked+ out beside the view, so a 240 px brush on 33 million triangles no longer freezes the view for a third of a+ second at a time (slowest frames about 35 ms instead of 300), and letting go of the pen is instant. The stroke+ itself comes out the same.+## Version 0.10.7- **A clearer membership card**: the three memberships side by side with what each one gets you, in your look'sdiff --git a/docs/performance.md b/docs/performance.mdindex 1e900975..573f46a1 100644--- a/docs/performance.md+++ b/docs/performance.md@@ -12,6 +12,33 @@ sculptforge-cli --bench ... # the same without the GPU (CI,Machine for all figures: Apple M1 Pro (8 performance + 2 efficiency cores, 32 GB unified memory, 30 GB usableunder Asahi Linux), integrated GPU, Mesa 26.1 (OpenGL 4.6). How the benchmark works is described under v0.5 below.+## 0.10.8: background dabs (perf/bigbrush-p99)++`--benchmark standard` placed every stroke in the frame loop (predictive strokes are off by default), and on the+33M head one 240 px dab moves 3-11M vertices (the symmetric brush covers a large part of the face). Where one such+frame went (`SF_FRAME_LOG=40 SF_STROKE_LOG=1 SF_SYNC_LOG=1`): the dab 70-220 ms (kernel 20-100, normals 40-100,+BVH refit 10-30), then the vertex upload 40-110 ms (packing 30-75, cluster bounds 12-32), then the swap. Every step+was already threaded and near memory bandwidth, so the work can't fit a frame; it has to leave the frame.++From 2M vertices a sculpt stroke now uses the solver thread without the preview (`App::backgroundDabs`,+`SF_BACKGROUND_DABS=0/1`): the same dabs in the same order, packed into the two-part upload there. A batch is about+a frame of work (at least 10 ms), the frame waits up to 12.5 ms for it (so light dabs still show in the frame that+placed them) and otherwise keeps drawing, and the next frame commits it. The same dabs through either path give the+same mesh, bit for bit, positions and normals (`ui_stroke_background`; `--measure-stroke 3,120` and `3,240` with+`SF_MEASURE_VERIFY=1` on a subdivided sphere). The benchmark scene itself isn't reproducible to that level: two+frame-loop runs with every dab placed differ by up to 0.26, as much as one of each. Load 3-4, frame p99 / worst+stall ms, before -> after:++| Phase | 15M | 33M |+|---|---|---|+| Clay large | 77 / 136 -> 31 / 34 | 129 / 161 -> 31 / 40 |+| 240 px brush | 139 / 172 -> 31 / 36 | 297 / 323 -> 33 / 41 |+| Pen lift (240 px) | 20 ms -> 0 | 153 ms -> 0 |++On the sphere (`new_sphere subdivisions=9`) at 120 px (`--measure-stroke 3,120`, background dabs forced) the+stroke places as many dabs (156 vs 141 in the frame loop) with about the same lag (62 vs 53 ms). Each 240 px dab at 33M still takes 100-200 ms, so the surface+trails the pointer by about a dab while the view stays smooth.+## Next version: predictive strokes (feat/zero-latency)A stroke on a heavy mesh used to run in the frame loop: each frame placed a dab (45-50 ms at 21M with a 200 pxdiff --git a/src/app/App.cpp b/src/app/App.cppindex 08c6194c..12c487cc 100644--- a/src/app/App.cpp+++ b/src/app/App.cpp@@ -992,7 +992,6 @@ int App::run() {pollPhone();pollAutoUv();pollUvEditor();- pollUvEditor();pollBake();pollQRemesh();pollSplatTrain();@@ -1854,6 +1853,7 @@ void App::drainStrokeDabs(double budgetSeconds) {static const bool log = std::getenv("SF_STROKE_LOG") != nullptr; // per frame: dabs waiting, placed, msconst auto start = std::chrono::steady_clock::now();const size_t waiting = dabQueue_.size();+ const SculptTimings before = doc_.sculptor.timings;size_t placed = 0;while (!dabQueue_.empty()) {const PathDab d = dabQueue_.pop();@@ -1867,11 +1867,14 @@ void App::drainStrokeDabs(double budgetSeconds) {std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count() >= budgetSeconds)break; // at least one dab per call, so a stroke always moves on}- if (log)- SDL_Log("stroke: %llu waiting, %llu placed in %.1f ms, %llu left", (unsigned long long)waiting,- (unsigned long long)placed,+ if (log) {+ const SculptTimings& t = doc_.sculptor.timings;+ SDL_Log("stroke: %llu waiting, %llu placed in %.1f ms (kernel %.1f, normals %.1f, BVH %.1f, %llu vertices), %llu left",+ (unsigned long long)waiting, (unsigned long long)placed,std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - start).count(),- (unsigned long long)dabQueue_.size());+ t.kernelMs - before.kernelMs, t.normalsMs - before.normalsMs, t.bvhMs - before.bvhMs,+ (unsigned long long)(t.vertices - before.vertices), (unsigned long long)dabQueue_.size());+ }}void App::dabAt(float x, float y, float pressure) {diff --git a/src/app/App.h b/src/app/App.hindex 34fbdfa2..57830487 100644--- a/src/app/App.h+++ b/src/app/App.h@@ -313,6 +313,7 @@ private:bool strokeFinishing_ = false; // released: the solver places the last dabs, the UI carries onbool predictiveStrokes() const; // the preference (and its environment override)bool draftStrokes() const; // Draft Strokes (SF_DRAFT_STROKE=0 / 1 overrides)+ bool backgroundDabs() const; // a heavy mesh's dabs on the solver thread without the preview (SF_BACKGROUND_DABS)bool asyncStrokeAllowed() const; // the stroke beginning now may use the solver threadbool solverBusy() const;void solverWait(); // waits for the dabs in flight and commits themdiff --git a/src/app/AsyncStroke.cpp b/src/app/AsyncStroke.cppindex 9e3ce384..91f7e76b 100644--- a/src/app/AsyncStroke.cpp+++ b/src/app/AsyncStroke.cpp@@ -25,6 +25,11 @@ namespace sf {namespace {constexpr double kBatchSeconds = 0.033; // a batch's dabs: about this much work (the surface updates this often)+// Background dabs (no preview to cover a batch): batches of about a frame's share of work, as the frame loop places.+constexpr double kBackgroundBatchSeconds = 0.010;+// Meshes from this many vertices place a stroke's dabs on the solver thread even without predictive strokes: one+// 240 px dab there moves millions of vertices (100-300 ms at 33M triangles), more than a frame can hold.+constexpr size_t kBackgroundDabVertices = 2'000'000;} // namespace@@ -54,6 +59,11 @@ void App::StrokeSolver::wait() {idle_.wait(lock, [this] { return !busy_.load(std::memory_order_acquire); });}+bool App::StrokeSolver::waitFor(double seconds) {+ std::unique_lock<std::mutex> lock(mutex_);+ return idle_.wait_for(lock, std::chrono::duration<double>(seconds), [this] { return !busy_.load(std::memory_order_acquire); });+}+void App::StrokeSolver::loop() {for (;;) {{@@ -132,11 +142,21 @@ bool App::draftStrokes() const {return predictiveStrokes() && (env >= 0 ? env == 1 : uiPrefs_.draftStrokes || currentTierConfig().draftBrushMode);}+bool App::backgroundDabs() const {+ static const int env = [] {+ const char* v = std::getenv("SF_BACKGROUND_DABS");+ return v && *v ? (*v == '0' ? 0 : 1) : -1;+ }();+ // The same dabs in the same order as the frame loop places them, only on the solver thread: the frame keeps+ // drawing what the GPU has while a big dab is worked out, and letting go doesn't wait for the dabs queued.+ return env >= 0 ? env == 1 : doc_.mesh.vertexCount() >= kBackgroundDabVertices;+}+bool App::asyncStrokeAllowed() const {// Sculpt strokes of queued dabs only: Move, Snake Hook and the drag strokes work in strokeTo itself; painting,// masking and dynamic topology (which changes the layout the renderer packs for) stay in the frame loop.const bool recipe = strokeRecipe() != nullptr;- return interactiveStrokes_ && predictiveStrokes() && strokeSculpt_ && !strokePaint_ && !strokeMask_ &&+ return interactiveStrokes_ && (predictiveStrokes() || backgroundDabs()) && strokeSculpt_ && !strokePaint_ && !strokeMask_ &&!doc_.sculptor.dyntopo.enabled && previewLod_ < 0 && !faceTakePlaying_ &&!doc_.mesh.micro && // (a micro level is sculpted and drawn by core/MicroSculpt and the MicroRenderer)(recipe || (strokeBrush_ != BrushType::Move && strokeBrush_ != BrushType::SnakeHook)) &&@@ -241,8 +261,7 @@ void App::pumpStroke() {s.view.radiusProbe = nullptr;s.view.settings = dabSettings();s.uploadId = doc_.activeSubtoolInfo().id;- renderer_.bindStrokeUpload(s.uploadId, s.upload);- renderer_.bindStrokeUpload(s.uploadId, s.upload); // (the slot, found here: the thread doesn't walk the renderer's list)+ renderer_.bindStrokeUpload(s.uploadId, s.upload); // (the slot, found here: the thread doesn't walk the renderer's list)s.prepare = &displayedMesh() == &doc_.mesh;s.start();return;@@ -255,7 +274,10 @@ void App::pumpStroke() {// packing, per batch, stays at most about half the work), at least one dab; once the pen has lifted, every// dab left (the preview shows them all: only the end result is waited for), with the stroke's end work.StrokeSolver& s = *solver_;- const double target = std::max(kBatchSeconds, prepSeconds_);+ // (Background dabs: about a frame of work, which the solver does while the frame draws: at least as many dabs+ // a frame as the frame loop placed, however long the frame.)+ const double target = std::max(predictiveStrokes() ? kBatchSeconds : std::max(kBackgroundBatchSeconds, frameSeconds_ * 0.8),+ prepSeconds_);size_t n = dabSeconds_ > 0.0 ? size_t(std::clamp(target / dabSeconds_, 1.0, 64.0)) : 1;if (strokeFinishing_) n = 64;s.batch.clear();@@ -271,6 +293,10 @@ void App::pumpStroke() {s.prepare = &displayedMesh() == &doc_.mesh; // (a previewed LOD shows instead: nothing to pack)renderer_.setDraftNormals(doc_.sculptor.draftActive() ? s.uploadId : 0);s.start();+ // Background dabs: a batch is waited for here up to a frame's share of work, so a light one shows in this frame+ // as in the frame loop's own path; a heavier one goes on while the frame draws, and the next frame commits it.+ if (!predictiveStrokes() && dabSeconds_ > 0.0 && s.waitFor(kBackgroundBatchSeconds * 1.25) && s.done)+ commitSolverBatch();}void App::finishStrokeNow() {diff --git a/src/app/AsyncStroke.h b/src/app/AsyncStroke.hindex 83eef932..2f1fae58 100644--- a/src/app/AsyncStroke.h+++ b/src/app/AsyncStroke.h@@ -24,6 +24,7 @@ struct App::StrokeSolver {void start();bool busy() const { return busy_.load(std::memory_order_acquire); }void wait(); // until the batch in flight is done+ bool waitFor(double seconds); // ... or at most this long; true: it is doneApp& app;// While busy, owned by the thread (the main thread only reads batch, to draw the preview).diff --git a/src/app/ProofMode.cpp b/src/app/ProofMode.cppindex cb494af6..120a8f23 100644--- a/src/app/ProofMode.cpp+++ b/src/app/ProofMode.cpp@@ -29,6 +29,7 @@#include <cmath>#include <condition_variable>#include <cstdio>+#include <cstdlib>#include <cstring>#include <ctime>#include <deque>@@ -739,6 +740,13 @@ void App::proofBeforeSwap(int fbW, int fbH) {}r.ramBytes = residentBytes();r.peakRamBytes = peakResidentBytes();+ // SF_BENCH_MESH_OUT=prefix: the mesh's positions after every stroke (prefix-15M.bin: float x, y, z per+ // vertex), to compare runs. (SF_STROKE_BACKLOG=0 places every dab, but the scene still differs a little from+ // run to run on either stroke path; --measure-stroke with SF_MEASURE_VERIFY=1 compares the same dabs exactly.)+ if (const char* out = std::getenv("SF_BENCH_MESH_OUT")) {+ std::ofstream f(std::string(out) + "-" + r.label + ".bin", std::ios::binary);+ f.write(reinterpret_cast<const char*>(doc_.mesh.positions.data()), std::streamsize(doc_.mesh.positions.size() * sizeof(glm::vec3)));+ }++p.sizeIndex;p.stage = p.sizeIndex < p.sizes.size() ? Stage::Announce : Stage::Finish;};diff --git a/src/app/main.cpp b/src/app/main.cppindex bf7657ee..15cf3785 100644--- a/src/app/main.cpp+++ b/src/app/main.cpp@@ -78,10 +78,14 @@ void usage() {// UI tests must never read or change the user's real settings, recent files or// autosaves: point the per-user data directories at a fresh temporary folder.+std::filesystem::path g_isolatedDir; // isolateSettings' folder, removed when the run ends well+void isolateSettings() {+ if (!g_isolatedDir.empty()) return; // (once: --run with --measure-stroke asks twice)std::error_code ec;const auto dir = std::filesystem::temp_directory_path(ec) /("sculptforge-ui-test-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));+ g_isolatedDir = dir;std::filesystem::create_directories(dir, ec);const std::string d = dir.string();#if defined(_WIN32)@@ -213,5 +217,11 @@ int main(int argc, char** argv) {int code = app.run();app.shutdown();sf::crashlog::stop();+ // The isolated folder (settings, the log, autosaves: a benchmark's 33M mesh is over a gigabyte) goes with a run+ // that passed; a failed one keeps it, log included, to look at.+ if (code == 0 && !g_isolatedDir.empty()) {+ std::error_code ec;+ std::filesystem::remove_all(g_isolatedDir, ec);+ }return code;}diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cppindex 65fbe19e..7a82b00c 100644--- a/src/render/Renderer.cpp+++ b/src/render/Renderer.cpp@@ -2094,9 +2094,21 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, booconst size_t ns = s.order.size();const bool ranged = !full && !dirtyRuns_.empty();const std::span<const uint32_t> order = s.order.fromGpu();+ static const bool syncLog = std::getenv("SF_SYNC_LOG") != nullptr; // where a vertex upload's time went+ auto lap = std::chrono::steady_clock::now();+ const auto lapStart = lap;+ double vertsMs = 0, boundsMs = 0, proxyMs = 0;+ const auto lapMs = [](std::chrono::steady_clock::time_point& t) {+ const auto now = std::chrono::steady_clock::now();+ const double ms = std::chrono::duration<double, std::milli>(now - t).count();+ t = now;+ return ms;+ };if (full || ranged) {uploadVertices(s, mesh, full);+ if (syncLog) vertsMs = lapMs(lap);updateBounds(s, mesh, full, full); // (a full upload computed them while building)+ if (syncLog) boundsMs = lapMs(lap);}if (full) {s.proxyAllocated = false; // sized for the old mesh@@ -2128,7 +2140,9 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, booif (wireframe && !s.linesValid && !s.outOfMemory) buildLines(s, mesh);// Small meshes draw in full while the view moves too (their proxy would save little).constexpr size_t kProxyMinTriangles = size_t(1) << 18;+ if (syncLog) lapMs(lap);if (proxyUsed_ && !s.outOfMemory && s.drawnTriangles >= kProxyMinTriangles) buildProxies(s, mesh);+ if (syncLog) proxyMs = lapMs(lap);syncTexture(s, mesh, pbr);syncNormalMap(s, mesh, normalMap);@@ -2168,6 +2182,13 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boos.fresh = false;uploadPatches(s, mesh, wireframe); // (may ask for a full upload next time)mesh.clearDirty();+ if (syncLog && (full || ranged)) {+ size_t sent = 0;+ for (const auto& [b0, e0] : dirtyRuns_) sent += e0 - b0;+ SDL_Log("sync: %s %zu slots in %zu runs, %.1f ms (vertices %.1f, bounds %.1f, proxy %.1f)", full ? "full" : "ranged",+ sent, dirtyRuns_.size(), std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - lapStart).count(),+ vertsMs, boundsMs, proxyMs);+ }}// The painted texture (core/Texture.h): a full upload when it is new or replaced, then only the rectangle