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
Stroke backlog floor: about 150 ms of work for slow dabs (big brushes on dense meshes lag less, same stroke)
islandmed4d86603d5d00
diff --git a/changes/perf-dab-normals.md b/changes/perf-dab-normals.mdnew file mode 100644index 00000000..8cb007e1--- /dev/null+++ b/changes/perf-dab-normals.md@@ -0,0 +1,12 @@+---+whats_new: **Big brushes on big meshes** follow the pen closer on 4-core machines: a 240 px brush on a 28M-triangle scan lags about a sixth less and settles sooner after pen-up.+changed: Big brushes on dense meshes keep a shorter queue of pending dabs when each dab is slow (about 150 ms of work,+ at least one dab, instead of always four): a 240 px Clay stroke on a 28M scan with 4 threads lags 297 ms instead of+ 357 (median) and settles 0.16 s sooner after pen-up, with the same stroke; small brushes are unchanged.+testers:+ - 4-core laptop, a 20M+ mesh: a long fast stroke with a 240 px Clay brush; the stroke stays close behind the pen,+ no gaps, same depth as before.+---+Lever: the stroke backlog floor (kStrokeBacklogMinDabs = 4) times a 70 ms dab was most of the lag. 100 ms (one dab)+cut more lag (262 ms) but placed fewer dabs and a weaker stroke on average (0.90x volume); 150 ms keeps it (0.97x).+Analytic normals stay gated on the floor tier (release +170 ms, no lag gain there). Data: ~/Work/.sf/bench-dabnormals/.diff --git a/src/app/App.cpp b/src/app/App.cppindex 6bddfecc..ab3d2f85 100644--- a/src/app/App.cpp+++ b/src/app/App.cpp@@ -777,6 +777,7 @@ constexpr double kStrokeBudgetSeconds = 0.010; // a frame's share of placing a sconstexpr float kRightClickSlop = 4.0f; // pixels a right press may move and still be a click (#22)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+constexpr double kStrokeBacklogFloorSeconds = 0.15; // ... unless that is more than this much work (at least one)} // namespace@@ -2364,7 +2365,12 @@ void App::strokeTo(float x, float y) {const char* env = std::getenv("SF_STROKE_BACKLOG");return env && *env ? std::max(0L, std::strtol(env, nullptr, 10)) : -1L;}();- dabQueue_.thin(strokeBacklogLimit(dabSeconds_, fixedLimit, kStrokeBacklogSeconds, kStrokeBacklogMinDabs));+ // Slow dabs keep a shorter floor: about kStrokeBacklogFloorSeconds of them (SF_BACKLOG_FLOOR_MS; 0 = always 4).+ static const double floorSeconds = [] {+ const char* env = std::getenv("SF_BACKLOG_FLOOR_MS");+ return env && *env ? std::max(0.0, std::strtod(env, nullptr)) / 1000.0 : kStrokeBacklogFloorSeconds;+ }();+ dabQueue_.thin(strokeBacklogLimit(dabSeconds_, fixedLimit, kStrokeBacklogSeconds, kStrokeBacklogMinDabs, floorSeconds));} else {drainStrokeDabs(0.0); // scripts, UI tests and recordings place every dab at once, as before}diff --git a/src/app/ProofMode.cpp b/src/app/ProofMode.cppindex 9f889f5d..4791e180 100644--- a/src/app/ProofMode.cpp+++ b/src/app/ProofMode.cpp@@ -937,6 +937,18 @@ void App::proofBeforeSwap(int fbW, int fbH) {ph.strokeFrames = perf_.lastStrokeFrames;strokeProbe_ = nullptr;ph.dabs = p.dabs.size();+ // SF_BENCH_DUMP=<prefix>: the positions after each stroke (<prefix>-<phase>.bin, raw floats), to compare+ // what two settings' strokes made (a result check beside the timings).+ // SF_BENCH_DUMP_PHASES="clay large,smooth": only those.+ const char* only = std::getenv("SF_BENCH_DUMP_PHASES");+ if (const char* dump = std::getenv("SF_BENCH_DUMP");+ dump && *dump && (!only || std::string(",").append(only).append(",").find(std::string(",") + spec.name + ",") != std::string::npos)) {+ std::string file = std::string(dump) + "-" + spec.name + ".bin";+ std::replace(file.begin(), file.end(), ' ', '_');+ if (std::ofstream out(file, std::ios::binary); out)+ out.write(reinterpret_cast<const char*>(doc_.mesh.positions.data()),+ std::streamsize(doc_.mesh.positions.size() * sizeof(glm::vec3)));+ }nextPhase();break;}diff --git a/src/core/StrokePath.h b/src/core/StrokePath.hindex e79d767c..aa5e0fe5 100644--- a/src/core/StrokePath.h+++ b/src/core/StrokePath.h@@ -145,9 +145,14 @@ private:// 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) {+// `floorSeconds` > 0: where dabs are slow, the floor is only as many as that much work (at least one): `minDabs`+// dabs of 70 ms each (a 240 px brush on a 28M scan, floor tier) were a third of a second behind the pointer.+inline size_t strokeBacklogLimit(double dabSeconds, long fixed = -1, double backlogSeconds = 0.05, size_t minDabs = 4,+ double floorSeconds = 0.0) {if (fixed >= 0) return size_t(fixed);size_t limit = minDabs;+ if (floorSeconds > 0.0 && dabSeconds > 0.0)+ limit = std::clamp(size_t(std::min(floorSeconds / dabSeconds, 1e6)), size_t(1), minDabs);if (dabSeconds > 0.0) limit = std::max(limit, size_t(std::min(backlogSeconds / dabSeconds, 1e6)));return limit;}diff --git a/tests/test_stroke_path.cpp b/tests/test_stroke_path.cppindex 3efa7c7e..cc3bd9cc 100644--- a/tests/test_stroke_path.cpp+++ b/tests/test_stroke_path.cpp@@ -189,6 +189,13 @@ TEST(stroke_backlog_keeps_a_heavy_brush_near_the_pointer) {CHECK(strokeBacklogLimit(0.0) == 4); // not measured yetCHECK(strokeBacklogLimit(0.045, 0) == 0); // SF_STROKE_BACKLOG=0: never thinnedCHECK(strokeBacklogLimit(0.0001, 10) == 10); // a fixed limit+ // A floor in time: slow dabs keep fewer waiting (at least one), quick ones the usual floor.+ CHECK(strokeBacklogLimit(0.07, -1, 0.05, 4, 0.15) == 2); // a 240 px dab on a 28M scan: two waiting, not four+ CHECK(strokeBacklogLimit(0.04, -1, 0.05, 4, 0.15) == 3);+ CHECK(strokeBacklogLimit(0.02, -1, 0.05, 4, 0.15) == 4);+ CHECK(strokeBacklogLimit(0.0001, -1, 0.05, 4, 0.15) == 500);+ CHECK(strokeBacklogLimit(0.0, -1, 0.05, 4, 0.15) == 4);+ CHECK(strokeBacklogLimit(0.2, -1, 0.05, 4, 0.15) == 1); // slower still: one// 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});