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
Fast Big Brushes: scale-adaptive dabs and fused batches for big brushes
islandmedb598e2a581db
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 463d9b69..e8bdb06b 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -41,6 +41,7 @@ add_library(sculptforge_core STATICsrc/core/TierConfig.cppsrc/core/MemoryGuard.cppsrc/core/Sculpt.cpp+ src/core/SculptBatch.cppsrc/core/Dyntopo.cppsrc/core/DyntopoCommands.cppsrc/core/Undo.cppdiff --git a/changes/beta-boost.md b/changes/beta-boost.mdnew file mode 100644index 00000000..f46abf87--- /dev/null+++ b/changes/beta-boost.md@@ -0,0 +1,23 @@+---+whats_new: **Fast Big Brushes**: big brushes on dense meshes keep up with the pen. A 240 px Clay stroke on a 15 million+ triangle head trails the pen by about a quarter of what it did, and every dab is placed. On by default:+ **Preferences > Performance > Fast Big Brushes**.+highlight: Big brushes keep up on dense meshes+added: **Fast Big Brushes** (Preferences > Performance, on by default): a dab that reaches more than 150,000 vertices+ takes its plane from an even sample of them and turns the shading along with the move it makes (from the brush's+ own slope, no neighbour walks), and the dabs waiting for a frame go through the vertices together in one pass, so+ the normals, the picking structure and the GPU copy are updated once a frame instead of once a dab. Every vertex+ still moves by the brush itself; the exact shading follows when the pen rests and when it lifts. Standard, Clay,+ Flatten, Crease, Pinch and Dam Standard; Trim takes the sampled plane and shading but not the fused pass. Small+ brushes, Smooth, Layer, Inflate, Nudge, Clay Block, alphas and masked areas are placed dab by dab as before.+ The result matches the dab-by-dab stroke to within about 2% of how far it moved the surface+ (tests/test_beta_boost.cpp).+testers:+ - Windows: open a dense model (or Help > Benchmark, standard-15m) and sculpt with a 200-300 px Clay or Standard+ brush across the face, fast: the surface should follow the pen closely, without the stroke falling behind.+ - Any OS: same stroke with Preferences > Performance > Fast Big Brushes off, then on: the result should look the+ same; the shading under the brush should be smooth while you drag and after you lift the pen.+ - Any OS: X symmetry on, a big brush back and forth across the middle of the face: no seam or step at the mirror line.+ - macOS / Linux: undo and redo a big stroke: the surface and its shading come back exactly.+---+Numbers before/after (laptop, floor profile, Windows PC): REPORT.md on perf/beta-boost.diff --git a/src/app/App.cpp b/src/app/App.cppindex 86375317..b92f9bda 100644--- a/src/app/App.cpp+++ b/src/app/App.cpp@@ -1822,6 +1822,7 @@ void App::beginStroke(float x, float y, const RayHit& hit) {}haveLastDab_ = false;doc_.sculptor.draftStrokes = draftStrokes() && asyncStrokeAllowed(); // (AsyncStroke.cpp)+ doc_.sculptor.adaptiveDabs = fastBigBrushes();if (!doc_.sculptor.beginStroke(doc_.mesh,!strokeSculpt_ ? std::string("Paint") : recipe ? recipe->name : brushLabel(strokeBrush_),strokeSculpt_ ? doc_.activeLayer : -1)) {@@ -2024,11 +2025,13 @@ void App::drainStrokeDabs(double budgetSeconds) {const size_t waiting = dabQueue_.size();const SculptTimings before = doc_.sculptor.timings;size_t placed = 0;+ doc_.sculptor.beginBatch(); // (a big brush's dabs wait and go through the vertices together: core/SculptBatch.cpp)while (!dabQueue_.empty()) {const PathDab d = dabQueue_.pop();const auto dabStart = std::chrono::steady_clock::now();dabAt(d.x, d.y, d.pressure);++placed;+ if (doc_.sculptor.batchWaiting() > 0) continue; // (its cost shows when the batch is placed, below)// what a dab costs on this mesh with this brush (the backlog limit in strokeTo)const double cost = std::chrono::duration<double>(std::chrono::steady_clock::now() - dabStart).count();dabSeconds_ = dabSeconds_ > 0.0 ? dabSeconds_ * 0.8 + cost * 0.2 : cost;@@ -2036,6 +2039,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 (const size_t fused = doc_.sculptor.batchWaiting(); fused > 0) {+ const auto batchStart = std::chrono::steady_clock::now();+ doc_.sculptor.endBatch(doc_.mesh, doc_.bvh);+ const double cost = std::chrono::duration<double>(std::chrono::steady_clock::now() - batchStart).count() / double(fused);+ dabSeconds_ = dabSeconds_ > 0.0 ? dabSeconds_ * 0.8 + cost * 0.2 : cost;+ } else {+ doc_.sculptor.endBatch(doc_.mesh, doc_.bvh);+ }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",@@ -2117,6 +2128,8 @@ void App::dabAt(float x, float y, float pressure) {DabParams p;p.center = hit.position;p.viewDir = cam.forward();+ p.pickDir = cam.ortho ? cam.forward() : hit.position - cam.position();+ p.pickNormal = glm::length(hit.normal) > 0.0f ? glm::normalize(hit.normal) : glm::vec3(0.0f);p.radius = baseRadius * (st.pressureSize ? std::max(pressure, 0.05f) : 1.0f) * dabRadiusScale_;// Smooth keeps its own Z Intensity (ZBrush), whether picked or held with Shift.p.strength = (st.brush == BrushType::Smooth ? st.smoothStrength : st.strength) * dabStrengthScale_;diff --git a/src/app/App.h b/src/app/App.hindex b1963139..dc13ff75 100644--- a/src/app/App.h+++ b/src/app/App.h@@ -276,6 +276,7 @@ private:// frame in the interactive loop, so a stroke on a huge mesh keeps its curve (core/StrokePath.h).StrokePath strokePath_;DabQueue dabQueue_;+ double lastDabsQueued_ = 0.0; // when pumpStroke last handed the solver dabs (catch-up waits for a pause)std::vector<PathDab> strokeScratch_;std::vector<glm::vec2>* strokeProbe_ = nullptr; // --measure-stroke: where each dab went (screen)std::vector<float>* strokeRadiusProbe_ = nullptr; // ... and its brush radius (px)@@ -334,6 +335,7 @@ private:bool asyncStroke_ = false; // this stroke's dabs go to the solver threadbool strokeFinishing_ = false; // released: the solver places the last dabs, the UI carries onbool predictiveStrokes() const; // the preference (and its environment override)+ bool fastBigBrushes() const; // Preferences > Performance > Fast Big Brushes (SF_ADAPTIVE_DABS=0 / 1 overrides it)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 threaddiff --git a/src/app/AsyncStroke.cpp b/src/app/AsyncStroke.cppindex 9d4c027c..5cef1d7a 100644--- a/src/app/AsyncStroke.cpp+++ b/src/app/AsyncStroke.cpp@@ -82,11 +82,19 @@ void App::StrokeSolver::loop() {app.doc_.sculptor.flushDraft(app.doc_.mesh, app.doc_.bvh);spent += std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();}+ app.doc_.sculptor.beginBatch(); // (a big brush's dabs go through the vertices together: core/SculptBatch.cpp)for (const PathDab& d : batch) {const auto t0 = std::chrono::steady_clock::now();app.dabAt(d.x, d.y, d.pressure);spent += std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();}+ {+ const auto t0 = std::chrono::steady_clock::now();+ 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);+ spent += std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();+ }app.dabView_ = nullptr;if (!kelvinSteps.empty() || kelvinFar) { // a Kelvinlet stroke's moves (PanelKelvinlet.cpp)const auto t0 = std::chrono::steady_clock::now();@@ -132,6 +140,14 @@ bool App::predictiveStrokes() const {return env >= 0 ? env == 1 : uiPrefs_.predictiveStrokes;}+bool App::fastBigBrushes() const {+ static const int env = [] {+ const char* v = std::getenv("SF_ADAPTIVE_DABS");+ return v && *v ? (*v == '0' ? 0 : 1) : -1;+ }();+ return env >= 0 ? env == 1 : uiPrefs_.fastBigBrushes;+}+bool App::draftStrokes() const {static const int env = [] {const char* v = std::getenv("SF_DRAFT_STROKE");@@ -255,6 +271,7 @@ void App::pumpStroke() {StrokeSolver& s = *solver_;s.batch.clear();s.finalize = true;+ s.catchUp = false;s.view.camera = camera_;s.view.canvas = canvasRect();s.view.probe = nullptr;@@ -267,7 +284,26 @@ void App::pumpStroke() {return;}if (dabQueue_.empty()) {- if (strokeFinishing_) finishStrokeNow(); // released, and every dab is on the GPU+ if (strokeFinishing_) {+ finishStrokeNow(); // released, and every dab is on the GPU+ return;+ }+ // The pen rests (no dabs for a tenth of a second): the exact normals behind the brush (Fast Big Brushes)+ // catch up meanwhile. (Not while dabs keep coming: a catch-up batch would hold the next one back.)+ if (!doc_.sculptor.normalsPending() || double(SDL_GetTicksNS()) * 1e-9 - lastDabsQueued_ < 0.1) return;+ StrokeSolver& s = *solver_;+ s.batch.clear();+ s.finalize = false;+ s.catchUp = true;+ s.view.camera = camera_;+ s.view.canvas = canvasRect();+ s.view.probe = nullptr;+ s.view.radiusProbe = nullptr;+ s.view.settings = dabSettings();+ s.uploadId = doc_.activeSubtoolInfo().id;+ renderer_.bindStrokeUpload(s.uploadId, s.upload);+ s.prepare = &displayedMesh() == &doc_.mesh;+ s.start();return;}// The next batch, oldest first: about kBatchSeconds of work, or as much as packing its upload costs (so the@@ -279,10 +315,13 @@ void App::pumpStroke() {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;+ // A big brush's batch goes through the vertices together (Fast Big Brushes): every dab waiting, at once.+ if (strokeFinishing_ || doc_.sculptor.fusesBatches()) n = 64;s.batch.clear();while (!dabQueue_.empty() && s.batch.size() < n) s.batch.push_back(dabQueue_.pop());s.finalize = strokeFinishing_ && dabQueue_.empty();+ s.catchUp = false; // (dabs are coming: the exact normals wait for a pause or the release)+ lastDabsQueued_ = double(SDL_GetTicksNS()) * 1e-9;s.view.camera = camera_;s.view.canvas = canvasRect();s.view.probe = strokeProbe_ ? &s.probe : nullptr;diff --git a/src/app/AsyncStroke.h b/src/app/AsyncStroke.hindex 2f1fae58..413b03e0 100644--- a/src/app/AsyncStroke.h+++ b/src/app/AsyncStroke.h@@ -37,6 +37,8 @@ struct App::StrokeSolver {uint32_t uploadId = 0; // the active SubTool's renderer slotbool prepare = true; // pack the upload on the threadbool finalize = false; // before the batch: Sculptor::flushDraft (a draft stroke's end)+ bool catchUp = false; // after the batch (no dabs were waiting): Sculptor::catchUpNormals, this many leaves+ size_t catchUpLeaves = 16384; // (about 150,000 vertices: a few ms)// Kelvinlet strokes (PanelKelvinlet.cpp): after the batch's dabs, these moves (taken on the main thread, by// value) and, when kelvinFar, one group of the deferred far field.std::vector<KelvinletStep> kelvinSteps;diff --git a/src/app/Config.cpp b/src/app/Config.cppindex 483648c0..9caa7057 100644--- a/src/app/Config.cpp+++ b/src/app/Config.cpp@@ -114,6 +114,7 @@ Json toJson(const UiPrefs& p) {j.set("multithreading", p.multithreading);j.set("predictiveStrokes", p.predictiveStrokes);j.set("draftStrokes", p.draftStrokes);+ j.set("fastBigBrushes", p.fastBigBrushes);j.set("dyntopoConstantDetail", p.dyntopoConstantDetail);j.set("flatShading", p.flatShading);j.set("viewportCursor", double(p.viewportCursor));@@ -192,6 +193,7 @@ void fromJson(const Json& j, UiPrefs& p) {p.multithreading = j["multithreading"].asBool(p.multithreading);p.predictiveStrokes = j["predictiveStrokes"].asBool(p.predictiveStrokes);p.draftStrokes = j["draftStrokes"].asBool(p.draftStrokes);+ p.fastBigBrushes = j["fastBigBrushes"].asBool(p.fastBigBrushes);p.dyntopoConstantDetail = j["dyntopoConstantDetail"].asBool(p.dyntopoConstantDetail);p.flatShading = j["flatShading"].asBool(p.flatShading);p.viewportCursor = std::clamp(int(j["viewportCursor"].asNumber(p.viewportCursor)), 0, 1);diff --git a/src/app/Config.h b/src/app/Config.hindex b3f2d1cb..13ec5b1e 100644--- a/src/app/Config.h+++ b/src/app/Config.h@@ -100,6 +100,11 @@ struct UiPrefs {// bounds of what they touched are worked out once, when the pen lifts (Sculptor::draftStrokes). Off by// default; meant for laptops and battery (a hardware tier will turn it on).bool draftStrokes = false;+ // Fast big brushes (Sculptor::adaptiveDabs, core/SculptBatch.cpp): a big brush's dabs take their plane from a+ // sample of the region, carry the normals along with the move (the exact ones follow), and the dabs waiting+ // for a frame go through the vertices together. The result matches the dab-by-dab stroke to within about 2% of+ // how far it moved the surface (tests/test_beta_boost.cpp). On by default.+ bool fastBigBrushes = true;// Dynamic topology (Draw palette): false = the detail follows the brush size on screen (Sculptris Pro);// true = one detail size for the whole mesh.bool dyntopoConstantDetail = false;diff --git a/src/app/PanelPerformance.cpp b/src/app/PanelPerformance.cppindex 22c8fbc5..32a87442 100644--- a/src/app/PanelPerformance.cpp+++ b/src/app/PanelPerformance.cpp@@ -110,6 +110,17 @@ void App::registerPerformanceUiActions() {"Lighter on laptops and on battery: the hardware profile turns it on there by itself (its Draft brushes ""setting); this turns it on everywhere. Brushes that follow the surface's direction (Inflate, the plane ""brushes) use its direction from the stroke's start, so the result can differ slightly from a full stroke.");+ add("perf.fast_big_brushes", "Fast Big Brushes",+ [this] {+ uiPrefs_.fastBigBrushes = !uiPrefs_.fastBigBrushes;+ status_ = uiPrefs_.fastBigBrushes ? "Fast big brushes on" : "Fast big brushes off";+ },+ [this] { return uiPrefs_.fastBigBrushes; },+ "Big brushes on dense meshes keep up with the pen: a dab that reaches more than 150,000 vertices finds its "+ "plane from a sample of them and turns the shading along with the move it makes, and the dabs waiting for a "+ "frame are placed together in one pass. Every vertex still moves by the brush itself; the exact shading "+ "follows behind the brush and when you lift the pen. Small brushes, Smooth, Layer, Inflate, Nudge, Clay "+ "Block, alphas and masked areas are placed dab by dab as before. Off: every dab one by one.");registerPerformanceProfileUiActions(); // Re-calibrate, the frame governor (PanelPerformanceProfile.cpp)}@@ -177,6 +188,9 @@ void App::drawPerformanceSection() {bool draft = uiPrefs_.draftStrokes;if (ImGui::Checkbox(tr("Draft Strokes"), &draft)) actions_.find("perf.draft_strokes")->run();ui::HelpAction(*this, "perf.draft_strokes");+ bool fastBig = uiPrefs_.fastBigBrushes;+ if (ImGui::Checkbox(tr("Fast Big Brushes"), &fastBig)) actions_.find("perf.fast_big_brushes")->run();+ ui::HelpAction(*this, "perf.fast_big_brushes");// --- Threads ---bool threads = !ThreadPool::shared().serial();diff --git a/src/core/BVH.cpp b/src/core/BVH.cppindex d8d1149f..1e8c45bf 100644--- a/src/core/BVH.cpp+++ b/src/core/BVH.cpp@@ -409,6 +409,36 @@ struct CylinderShape {} // namespace+// A capsule: the points within sqrt(r2) of the segment a..a + ab.+struct CapsuleShape {+ glm::vec3 a, ab;+ float r, r2, len2;+ float dist2(const glm::vec3& p) const {+ const glm::vec3 d = p - a;+ const float t = len2 > 0.0f ? std::clamp(glm::dot(d, ab) / len2, 0.0f, 1.0f) : 0.0f;+ const glm::vec3 off = d - ab * t;+ return glm::dot(off, off);+ }+ bool touches(const AABB& b) const { // conservative: the box's bounding sphere against the capsule+ const glm::vec3 mid = (b.min + b.max) * 0.5f;+ const float reach = r + glm::length(b.max - mid);+ return dist2(mid) <= reach * reach;+ }+ bool containsBox(const AABB& b) const {+ for (int k = 0; k < 8; ++k) {+ const glm::vec3 p((k & 1) ? b.max.x : b.min.x, (k & 2) ? b.max.y : b.min.y, (k & 4) ? b.max.z : b.min.z);+ if (dist2(p) > r2) return false;+ }+ return true;+ }+ bool contains(const glm::vec3& p) const { return dist2(p) <= r2; }+};++void BVH::queryVerticesCapsule(const Mesh& mesh, const glm::vec3& a, const glm::vec3& b, float radius,+ std::vector<uint32_t>& out) const {+ queryVerticesIn(mesh, CapsuleShape{a, b - a, radius, radius * radius, glm::dot(b - a, b - a)}, out);+}+void BVH::queryVertices(const Mesh& mesh, const glm::vec3& center, float radius, std::vector<uint32_t>& out) const {queryVerticesIn(mesh, SphereShape{center, radius * radius}, out);}diff --git a/src/core/BVH.h b/src/core/BVH.hindex d74922c0..5fad0b7d 100644--- a/src/core/BVH.h+++ b/src/core/BVH.h@@ -32,6 +32,10 @@ public:// `axis` (a brush whose falloff is measured on screen reaches through the model along the view).void queryVerticesCylinder(const Mesh& mesh, const glm::vec3& point, const glm::vec3& axis, float radius,std::vector<uint32_t>& out) const;+ // queryVertices for a capsule: every vertex within `radius` of the segment a..b (a fused batch of dabs along+ // a stroke, core/SculptBatch.cpp).+ void queryVerticesCapsule(const Mesh& mesh, const glm::vec3& a, const glm::vec3& b, 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;// Calls fn(triangles of the leaf) for every leaf whose box touches the sphere (hidden triangles included);diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cppindex aeaf05eb..e81fd575 100644--- a/src/core/Sculpt.cpp+++ b/src/core/Sculpt.cpp@@ -1,14 +1,17 @@// SculptForge - open source digital sculpting// SPDX-License-Identifier: GPL-3.0-or-later#include "core/Sculpt.h"+#include "core/SculptKernel.h"#include "core/AtomicRef.h"#include "core/BrushAlpha.h"#include "core/ThreadPool.h"#include <algorithm>+#include <atomic>#include <cctype>#include <cmath>+#include <iterator>#include <limits>#include <unordered_map>@@ -392,6 +395,10 @@ bool Sculptor::beginStroke(const Mesh& mesh, std::string label, int layer) {draft_ = draftStrokes && !dyntopo.enabled;draftStrokes = false; // (asked for per stroke)draftLeaves_.clear();+ dropLazy(); // (a stroke that never ended)+ pending_.clear();+ lastRegion_ = 0;+ lastRegionRadius_ = 0.0f;strokeDiagonal_ = 0.0f;lastRemesh_ = false;topologyHeld_ = false;@@ -591,7 +598,8 @@ void Sculptor::moveVertices(Mesh& mesh, BVH& bvh, std::span<const uint32_t> vertelse bvh.refit(mesh, verts);}-void Sculptor::displaceAll(Mesh& mesh, std::span<const uint32_t> verts, std::span<const glm::vec3> d, bool influence) {+void Sculptor::displaceAll(Mesh& mesh, std::span<const uint32_t> verts, std::span<const glm::vec3> d, bool influence,+ const float* given) {const size_t n = verts.size();if (dynTopo_.active()) // the remesher's record keeps them (see touch); their bits then keep them out of oursfor (uint32_t v : verts)@@ -603,7 +611,7 @@ void Sculptor::displaceAll(Mesh& mesh, std::span<const uint32_t> verts, std::spapoolFor(n, [&](size_t b, size_t e) {uint32_t count = 0;for (size_t i = b; i < e; ++i) {- const float k = influence ? vertexInfluence(mesh, verts[i]) : 1.0f;+ const float k = given ? given[i] : influence ? vertexInfluence(mesh, verts[i]) : 1.0f;influence_[i] = k;firstTouch_[i] = k > 0.0f && markBit(touched_, verts[i]);count += firstTouch_[i];@@ -635,7 +643,61 @@ void Sculptor::displaceAll(Mesh& mesh, std::span<const uint32_t> verts, std::spa}, kBlock);}+void Sculptor::flushLazy(Mesh& mesh, const BVH& bvh, std::span<const uint32_t> leaves) {+ if (leaves.empty()) return;+ const std::vector<uint32_t> keep = std::move(dabLeaves_);+ dabLeaves_.assign(leaves.begin(), leaves.end());+ const bool draft = draft_;+ draft_ = false; // (updateDabNormals in full)+ updateDabNormals(mesh, bvh);+ draft_ = draft;+ dabLeaves_ = keep;+}++void Sculptor::dropLazy() {+ if (lazyCount_ > 0) {+ std::vector<uint32_t> drop;+ lazyLeaves_.take(drop);+ 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);+ }+ 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();+}++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);+ }+ flushLazy(mesh, bvh, leaves);+}+void Sculptor::flushDraft(Mesh& mesh, const BVH& bvh) {+ if (lazyCount_ > 0 && &mesh == lazyMesh_) {+ std::vector<uint32_t> leaves;+ lazyLeaves_.take(leaves);+ lazyCount_ = 0;+ lastLazyLeaves_.clear();+ flushLazy(mesh, bvh, leaves);+ }if (!draft_ || draftLeaves_.empty()) return;std::sort(draftLeaves_.begin(), draftLeaves_.end());draftLeaves_.erase(std::unique(draftLeaves_.begin(), draftLeaves_.end()), draftLeaves_.end());@@ -647,6 +709,13 @@ void Sculptor::flushDraft(Mesh& mesh, const BVH& bvh) {}UndoRecord Sculptor::endStroke() {+ if (!pending_.empty() && pendingMesh_ && pendingBvh_) flushBatch(*pendingMesh_, *pendingBvh_); // (callers that didn't)+ pending_.clear();+ batching_ = false;+ if (lazyCount_ > 0 && lazyMesh_ && lazyBvh_) flushDraft(*lazyMesh_, *lazyBvh_); // (callers that didn't)+ dropLazy();+ lazyMesh_ = nullptr;+ lazyBvh_ = nullptr;if (dynTopo_.active()) {Mesh& mesh = *dynTopo_.strokeMesh();std::unique_ptr<TopologyUndo> topo = dynTopo_.finish(mesh, *dynTopo_.strokeBvh());@@ -716,6 +785,15 @@ void Sculptor::dab(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p, Sym++timings.dabs;return;}+ lastFusable_ = adaptiveDabs && isPlaneKernelBrush(type) && type != BrushType::Trim && !(p.alpha && !p.alpha->empty()) &&+ !p.frontFacesOnly && !p.connectedOnly && !p.viewFalloff && !dyntopo.enabled;+ if (batching_ && batchable(mesh, type, p, sym)) { // a fused batch's (SculptBatch.cpp)+ pending_.push_back({type, p, sym});+ pendingMesh_ = &mesh;+ pendingBvh_ = &bvh;+ return;+ }+ if (!pending_.empty()) flushBatch(mesh, bvh);if (type != BrushType::Move && type != BrushType::Morph) { // (Morph needs the vertices the target has)const bool useAlpha = p.alpha && !p.alpha->empty(); // (the reach dabSingle gives the brush)const float reach = useAlpha ? kAlphaReach : type == BrushType::ClayBuildup ? 1.3f : 1.0f;@@ -725,13 +803,14 @@ void Sculptor::dab(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p, Sym}dabVerts_.clear();dabLeaves_.clear();+ lastLazyLeaves_.clear();for (const SymCopy& m : copies(sym)) {frameWanted_ = m.identity(); // (the unmirrored pass records lastFrame)const DabParams mp = m.identity() ? p : transformed(p, m);if (type == BrushType::Vdm) dabVdmSingle(mesh, bvh, mp, m.handedness());else dabSingle(mesh, bvh, type, mp);}- updateDabNormals(mesh, bvh);+ if (!dabVerts_.empty() || !dabLeaves_.empty() || lastLazyLeaves_.empty()) updateDabNormals(mesh, bvh);if (type != BrushType::Move) repairDab(mesh, bvh);}@@ -751,9 +830,28 @@ void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams&if (verts_.empty()) return;}+ // Scale-adaptive dabs (Sculpt.h, adaptiveDabs): a big region's plane from an even sample of its vertices.+ adaptiveDab_ = adaptiveDabs && !dynTopo_.active() && !draft_ && verts_.size() >= kAdaptiveMinVertices &&+ type != BrushType::Smooth && type != BrushType::Layer && type != BrushType::Morph &&+ // (an alpha's fine pattern or Clay Block's square tip, which turn with the area normal, and the brushes that move along each vertex's own normal amplify a+ // hair's difference in the normals the next dabs read)+ type != BrushType::Nudge && type != BrushType::Inflate && type != BrushType::ClayBuildup && !useAlpha &&+ mesh.normals.size() == mesh.vertexCount();+ if (adaptiveDab_ && (mesh.mask.size() == mesh.vertexCount() || mesh.hasHidden())) {+ // A mask's edge is a step the adaptive normals don't see (they follow the brush, not the mask): regions+ // with any of it take the exact path.+ std::atomic<bool> masked{false};+ poolFor(verts_.size(), [&](size_t b, size_t e) {+ for (size_t i = b; i < e && !masked.load(std::memory_order_relaxed); ++i)+ if (vertexInfluence(mesh, verts_[i]) < 1.0f) masked.store(true, std::memory_order_relaxed);+ }, 65536);+ adaptiveDab_ = !masked.load();+ }+ const size_t stride = adaptiveDab_ ? std::max<size_t>(1, verts_.size() / kAreaSamples) : 1;+// Weighted area normal and centre of the brush region.- const AreaSums sums = areaSums(verts_.size(), [&](size_t i, AreaSums& acc) {- const uint32_t v = verts_[i];+ const AreaSums sums = areaSums((verts_.size() + stride - 1) / stride, [&](size_t i, AreaSums& acc) {+ const uint32_t v = verts_[i * stride];float w = brushFalloff(brushDistance(p, pos[v]), p.radius, p.focal);acc.normal += nrm[v] * w;acc.center += pos[v] * w;@@ -768,6 +866,8 @@ void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams&if (frameWanted_) {lastFrame = {p.center, areaNormal, areaCenter, true};frameWanted_ = false;+ lastRegion_ = verts_.size();+ lastRegionRadius_ = p.radius;}const float s = std::clamp(p.strength, 0.0f, 1.0f) * std::clamp(p.pressure, 0.0f, 1.0f);@@ -788,20 +888,34 @@ void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams&const std::vector<glm::vec3>* morph = type == BrushType::Morph ? morphTarget(mesh) : nullptr;if (type == BrushType::Morph && !morph) return;+ PlaneKernel plane;+ plane.type = type;+ plane.center = p.center;+ plane.viewDir = p.viewDir;+ plane.areaNormal = areaNormal;+ plane.areaCenter = areaCenter;+ plane.strokeDir = strokeDir;+ plane.r = r;+ plane.s = s;+ plane.sign = sign;+ plane.focal = p.focal;+ plane.invert = p.invert;+ plane.viewFalloff = p.viewFalloff;+scratch_.resize(verts_.size());- // Each vertex's offset depends only on the positions before the dab (and, for Layer, on its own state).- poolFor(verts_.size(), [&](size_t begin, size_t end) {- for (size_t i = begin; i < end; ++i) {- const uint32_t v = verts_[i];- const glm::vec3& x = pos[v];+ // Each vertex's offset depends only on the positions before the dab (and, for Layer, on its own state). The+ // offset of the i-th vertex v if it were at x (x = pos[v] for the dab itself; the adaptive normals ask for+ // points next to it, which only the brushes that read nothing but the place, and v's own normal, are asked).+ const auto offset = [&](size_t i, uint32_t v, const glm::vec3& x) -> glm::vec3 {const glm::vec3 o = x - p.center;const float f = useAlpha ? p.alpha->stamp(glm::dot(o, right) / r, glm::dot(o, up) / r, p.focal): brushFalloff(brushDistance(p, x), r, p.focal);glm::vec3 d(0.0f);switch (type) {- case BrushType::Standard:- d = areaNormal * (sign * s * r * 0.06f * f);+ case BrushType::Standard: case BrushType::Flatten: case BrushType::Clay: case BrushType::Crease:+ case BrushType::Pinch: case BrushType::DamStandard: case BrushType::Trim:+ d = plane.offsetWith(x, f); // (core/SculptKernel.h)break;case BrushType::Inflate:d = nrm[v] * (sign * s * r * 0.05f * f);@@ -834,38 +948,6 @@ void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams&d = (avg - x) * std::min(1.0f, s * f);break;}- case BrushType::Flatten: {- float h = glm::dot(x - areaCenter, areaNormal);- d = -areaNormal * (h * s * f * (p.invert ? -0.5f : 1.0f));- break;- }- case BrushType::Clay: {- // Raise a plane slightly above the surface and pull everything below up to it.- glm::vec3 planePoint = areaCenter + areaNormal * (sign * r * 0.05f * s);- float h = glm::dot(x - planePoint, areaNormal);- if (sign * h < 0.0f) d = -areaNormal * (h * f * clayPull(s));- break;- }- case BrushType::Crease: {- // Carves a sharp valley by default (raises a ridge when inverted).- float sharp = f * f;- d = areaNormal * (-sign * s * r * 0.05f * sharp);- d += projectOnPlane(p.center - x, areaNormal) * (0.25f * s * f);- break;- }- case BrushType::Pinch:- d = projectOnPlane(p.center - x, areaNormal) * (sign * 0.2f * s * f);- break;- case BrushType::DamStandard: {- // Narrow, deep V-groove (a ridge when inverted); the sides are always- // pinched in toward the stroke line, so the crease stays sharp.- const float core = f * f * f * f;- d = areaNormal * (-sign * s * r * 0.08f * core);- glm::vec3 pull = projectOnPlane(p.center - x, areaNormal);- pull -= strokeDir * glm::dot(pull, strokeDir);- d += pull * (0.3f * s * f);- break;- }case BrushType::ClayBuildup: {// Square tip (rounded corners) with a flat top: a raised plane that// everything below is pulled up to, so strokes build up in slabs.@@ -896,14 +978,6 @@ void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams&d += side * (0.1f * s * f * std::min(1.0f, strokeLen / (0.1f * r)));break;}- case BrushType::Trim: {- // Trim Dynamic: a plane just below the area cuts off whatever sticks out- // above it (fills up to it when inverted). sqrt falloff for a crisp edge.- glm::vec3 planePoint = areaCenter - areaNormal * (sign * r * 0.02f * s);- float h = glm::dot(x - planePoint, areaNormal);- if (sign * h > 0.0f) d = -areaNormal * (h * std::min(1.0f, std::sqrt(f) * (0.5f + s)));- break;- }case BrushType::Layer: {// Each vertex rises to the stroke's height x falloff once; later dabs// only add what is missing, so overlaps within a stroke never stack.@@ -930,9 +1004,57 @@ void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams&default:break;}- scratch_[i] = d;+ return d;+ };+ if (!adaptiveDab_) {+ poolFor(verts_.size(), [&](size_t begin, size_t end) {+ for (size_t i = begin; i < end; ++i) scratch_[i] = offset(i, verts_[i], pos[verts_[i]]);+ });+ } else {+ // The new normal from the displaced tangents: t + (offset(x + h t) - offset(x)) / h along two directions+ // on the surface (first order in the brush's slope, exact for a plane); the cavity term each vertex had+ // before the stroke moved it is kept for the GPU until the exact one catches up.+ if (curvature_.size() != mesh.vertexCount() || curvatureMesh_ != &mesh) {+ curvature_.assign(mesh.vertexCount(), 0.0f);+ curvatureFilled_ = false;+ curvatureMesh_ = &mesh;+ }+ if (curvatureStamp_.size() != mesh.vertexCount()) curvatureStamp_.assign(mesh.vertexCount(), 0);+ const uint32_t stamp = uint32_t(strokeId_);+ const float h = r * 1e-3f;+ // The plane brushes carry the normals by their offset's derivative (core/SculptKernel.h); the others by+ // finite differences.+ const bool analytic = isPlaneKernelBrush(type) && !p.viewFalloff;+ const float kexp = std::exp2(2.0f * std::clamp(p.focal, -1.0f, 1.0f));+ std::vector<glm::vec3>& normals = mesh.normals;+ poolFor(verts_.size(), [&](size_t begin, size_t end) {+ for (size_t i = begin; i < end; ++i) {+ const uint32_t v = verts_[i];+ const glm::vec3 x = pos[v];+ if (curvatureStamp_[v] != stamp) {+ curvatureStamp_[v] = stamp;+ curvature_[v] = mesh.curvatureAt(v);+ }+ const glm::vec3 n = normals[v];+ if (analytic) { // (no mask here: such regions take the exact path)+ normals[v] = plane.offsetAndNormal(x, n, kexp, scratch_[i]);+ continue;+ }+ const glm::vec3 d = offset(i, v, x);+ scratch_[i] = d;+ const float k = vertexInfluence(mesh, v);+ if (k <= 0.0f) continue;+ const glm::vec3 t1 = glm::normalize(std::abs(n.x) < 0.9f ? glm::cross(n, glm::vec3(1, 0, 0))+ : glm::cross(n, glm::vec3(0, 1, 0)));+ const glm::vec3 t2 = glm::cross(n, t1);+ const glm::vec3 a = t1 + (offset(i, v, x + t1 * h) - d) * (k / h);+ const glm::vec3 b = t2 + (offset(i, v, x + t2 * h) - d) * (k / h);+ const glm::vec3 m = glm::cross(a, b);+ const float len = glm::length(m);+ if (len > 1e-20f && glm::dot(m, n) > 0.0f) normals[v] = m / len;+ }+ });}- });displaceAll(mesh, verts_, scratch_);finishDab(mesh, bvh, clock);@@ -970,8 +1092,20 @@ void Sculptor::finishDab(Mesh& mesh, BVH& bvh, std::chrono::steady_clock::time_ptimings.bvhMs += lapMs(clock);++timings.dabs;timings.vertices += verts_.size();- dabVerts_.insert(dabVerts_.end(), verts_.begin(), verts_.end()); // a vertex twice is fine: updateNormals dedupsconst auto leaves = bvh.queriedLeaves(); // (the query that found verts_)+ if (adaptiveDab_) { // the normals are done; the exact ones later (catchUpNormals, flushDraft), the GPU's now+ adaptiveDab_ = false;+ lazyMesh_ = &mesh;+ lazyBvh_ = &bvh;+ addLazyLeaves(bvh, leaves);+ lastLazyLeaves_.insert(lastLazyLeaves_.end(), leaves.begin(), leaves.end());+ mesh.markDirtyAround(verts_.size(), 4096, [&](size_t b, size_t e, IndexBits& set) {+ for (size_t i = b; i < e; ++i) set.insertLocal(verts_[i]);+ });+ timings.normalsMs += lapMs(clock);+ return;+ }+ dabVerts_.insert(dabVerts_.end(), verts_.begin(), verts_.end()); // a vertex twice is fine: updateNormals dedupsdabLeaves_.insert(dabLeaves_.end(), leaves.begin(), leaves.end());}@@ -1040,6 +1174,7 @@ void Sculptor::dabRecipe(Mesh& mesh, BVH& bvh, const BrushRecipe& recipe, constbase.viewFalloff = p.viewFalloff || recipe.viewFalloff;dabVerts_.clear();dabLeaves_.clear();+ lastLazyLeaves_.clear();for (const SymCopy& m : copies(sym)) {frameWanted_ = m.identity(); // (the unmirrored pass records lastFrame)const DabParams mp = m.identity() ? base : transformed(base, m);@@ -1122,6 +1257,8 @@ void Sculptor::dabRecipeSingle(Mesh& mesh, BVH& bvh, const BrushRecipe& r, constif (frameWanted_) {lastFrame = {p.center, areaNormal, areaCenter, true};frameWanted_ = false;+ lastRegion_ = verts_.size();+ lastRegionRadius_ = p.radius;}glm::vec3 strokeDir = p.strokeDelta;diff --git a/src/core/Sculpt.h b/src/core/Sculpt.hindex 81f59e7b..43f1848f 100644--- a/src/core/Sculpt.h+++ b/src/core/Sculpt.h@@ -5,6 +5,7 @@#include "core/BVH.h"#include "core/BrushRecipe.h"#include "core/Dyntopo.h"+#include "core/IndexBits.h"#include "core/Kelvinlet.h"#include "core/Mesh.h"#include "core/Symmetry.h"@@ -82,6 +83,10 @@ struct DabParams {// on screen, so the brush reaches through the model along the view as a cylinder.bool connectedOnly = false;bool viewFalloff = false;+ // The direction of the ray that picked `center` (zero: viewDir), so a fused batch (Sculptor::beginBatch) can+ // find where that ray meets the surface the batch's earlier dabs left.+ glm::vec3 pickDir{0.0f};+ glm::vec3 pickNormal{0.0f}; // the surface's normal there (zero: unknown)};// Local symmetry's centre for a Part: the middle of its bounding box (in parallel).@@ -93,6 +98,8 @@ inline DabParams transformed(const DabParams& p, const SymCopy& c) {o.viewDir = c.dir(p.viewDir);o.strokeDelta = c.dir(p.strokeDelta);o.alphaUp = c.dir(p.alphaUp);+ o.pickDir = c.dir(p.pickDir);+ o.pickNormal = c.dir(p.pickNormal);return o;}@@ -249,6 +256,34 @@ public:// for the GPU. flushDraft works out every normal and cavity term the stroke touched, once (the app calls// it before endStroke). Not with dynamic topology.bool draftStrokes = false;+ // Scale-adaptive dabs (Preferences > Performance > Fast big brushes, on by default): a dab whose region holds+ // more than kAdaptiveMinVertices vertices takes its area plane from an even sample of about kAreaSamples of+ // them (an average, so the sample gives it to within a fraction of a percent) and updates the normals of+ // what it moved from the derivative of its own displacement (the brush's offset at two points a hair away+ // along the surface: no neighbour rings are read), so the cost per vertex is the brush itself, the move and+ // its undo entry. Every vertex still moves by the exact brush. The exact normals and cavity terms follow+ // lazily: behind the brush as the stroke goes on (catchUpNormals) and the rest when it ends (flushDraft, or+ // endStroke). Brushes whose offset depends on more than the vertex's place (Smooth, Layer, Morph) and the+ // remesher always take the exact path, and so do regions below the threshold (small brushes are unchanged).+ bool adaptiveDabs = true;+ static constexpr size_t kAdaptiveMinVertices = 150'000;+ static constexpr size_t kAreaSamples = 65'536;+ // Whether adaptive dabs moved leaves whose exact normals are still to come, and catching up with up to+ // `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);+ // 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+ // the plane brushes core/SculptKernel.h lists, no alpha, mask or falloff options) wait and are then placed+ // in one pass over the vertices they reach: each vertex goes through the dabs in order, moved by exactly+ // the offset each gives where it is by then, so it ends where the dab-by-dab stroke leaves it. Each dab's+ // 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 endBatch(Mesh& mesh, BVH& bvh);+ size_t batchWaiting() const { return pending_.size(); }bool draftActive() const { return draft_; }bool draftPending() const { return draft_ && !draftLeaves_.empty(); }void flushDraft(Mesh& mesh, const BVH& bvh);@@ -310,7 +345,8 @@ private:void touch(Mesh& mesh, uint32_t v);// The one place brushes move vertices: scales `d` by vertexInfluence().void displace(Mesh& mesh, uint32_t v, const glm::vec3& d);- void displaceAll(Mesh& mesh, std::span<const uint32_t> verts, std::span<const glm::vec3> d, bool influence = true);+ void displaceAll(Mesh& mesh, std::span<const uint32_t> verts, std::span<const glm::vec3> d, bool influence = true,+ const float* given = nullptr); // given: each vertex's influence (0 = not moved)// Dynamic topology: remeshes before a dab, and keeps the stroke's per-vertex state in step with it.void remeshDab(Mesh& mesh, BVH& bvh, const DabParams& p, Symmetry sym, float scale, float reach);@@ -347,6 +383,36 @@ private:const Mesh* curvatureMesh_ = nullptr; // the mesh curvature_ is forbool draft_ = false; // this stroke is a draftstd::vector<uint32_t> draftLeaves_; // the BVH leaves its dabs touched (flushDraft)+ // 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> lastLazyLeaves_;+ void addLazyLeaves(const BVH& bvh, std::span<const uint32_t> leaves);+ void dropLazy(); // (empties the set without the normals)+ Mesh* lazyMesh_ = nullptr;+ const BVH* lazyBvh_ = nullptr;+ std::vector<uint32_t> curvatureStamp_;+ bool adaptiveDab_ = false; // the dab dabSingle is placing takes the adaptive path+ // Fused batches: the dabs waiting, and the last unmirrored region's size and radius (whether the next dab+ // is big enough to wait), and the place a batch's vertices are kept while it is applied.+ struct PendingDab {+ BrushType type;+ DabParams p;+ Symmetry sym;+ };+ bool batching_ = false;+ std::vector<PendingDab> pending_;+ Mesh* pendingMesh_ = nullptr;+ BVH* pendingBvh_ = nullptr;+ size_t lastRegion_ = 0;+ bool lastFusable_ = false; // the last dab's brush and options are a fused batch's (batchable, but for its size)+ float lastRegionRadius_ = 0.0f;+ bool batchable(const Mesh& mesh, BrushType type, const DabParams& p, const Symmetry& sym) const;+ void flushBatch(Mesh& mesh, BVH& bvh);+ std::vector<float> givenInfluence_;+ void flushLazy(Mesh& mesh, const BVH& bvh, std::span<const uint32_t> leaves);std::vector<glm::vec3> scratch_;// Smooth with keepVolume: each brush vertex's shrunk position, and (vertex, index) sorted to look it up.void taubinShrink(const Mesh& mesh);diff --git a/src/core/SculptBatch.cpp b/src/core/SculptBatch.cppnew file mode 100644index 00000000..a3e4ac20--- /dev/null+++ b/src/core/SculptBatch.cpp@@ -0,0 +1,363 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// Fused batches of big dabs (Sculptor::beginBatch, Sculpt.h). A 240 px dab on a 15M triangle head reaches a few+// million vertices, and the dab-by-dab path visits each of them (query, offset, undo entry, normals, BVH) once per+// dab, then the GPU gets them once per batch: the stroke trailed the pen by the 3-5 dabs waiting. Here the waiting+// dabs go through the vertices together:+// 1. per copy of the symmetry, one query of a sphere around all of the batch's dabs (plus what they could move+// into it), so each vertex is found once;+// 2. dab by dab, on an even sample of those vertices (moved along as the dabs go): the area plane each dab+// takes, from the sample as the dabs before it left it, and its centre, picked as the batch began, moved+// along by the dabs before it (a point on the surface goes where a vertex there would);+// 3. one pass over the vertices: each goes through the dabs in order, taking the offset each gives where the+// vertex is by then (so it lands where the dab-by-dab stroke puts it, give or take the sampled plane), and+// its normal goes along (the adaptive normals, Sculpt.cpp);+// 4. the undo entries, the BVH, the GPU's dirty marks and the exact normals' catch-up list, once.+#include "core/Sculpt.h"+#include "core/BrushAlpha.h"+#include "core/SculptKernel.h"+#include "core/IndexBits.h"+#include "core/ThreadPool.h"++#include <algorithm>+#include <atomic>+#include <chrono>+#include <cmath>+#include <iterator>+#include <limits>++namespace sf {++bool Sculptor::batchable(const Mesh& mesh, BrushType type, const DabParams& p, const Symmetry& sym) const {+ if (!adaptiveDabs || !isPlaneKernelBrush(type) || type == BrushType::Trim || dyntopo.enabled || draft_ || mesh.micro)+ return false;+ if ((p.alpha && !p.alpha->empty()) || p.frontFacesOnly || p.connectedOnly || p.viewFalloff) return false;+ if (mesh.normals.size() != mesh.vertexCount()) return false;+ if (!pending_.empty()) {+ const PendingDab& f = pending_.front();+ if (f.type != type || f.sym.x != sym.x || f.sym.y != sym.y || f.sym.z != sym.z || f.sym.radialOn() != sym.radialOn() ||+ f.sym.radial != sym.radial || f.sym.radialAxis != sym.radialAxis || f.sym.local != sym.local ||+ f.sym.pivot != sym.pivot)+ return false;+ if (pending_.size() >= 64 || f.p.focal != p.focal) return false;+ }+ // Big enough: the last region, scaled to this radius (the first dab of a stroke is placed as it comes).+ if (lastRegionRadius_ <= 0.0f || lastRegion_ == 0) return false;+ const float k = p.radius / lastRegionRadius_;+ return double(lastRegion_) * double(k * k) >= double(kAdaptiveMinVertices);+}++void Sculptor::endBatch(Mesh& mesh, BVH& bvh) {+ if (!pending_.empty()) flushBatch(mesh, bvh);+ batching_ = false;+}++namespace {++// A dab's frame on the surface from points with normals (the dab-by-dab path's areaSums, over a sample).+struct Frame {+ glm::vec3 normal{0.0f}, center{0.0f};+ float weight = 0.0f;+};++// A copy of the symmetry's share of the batch: its dabs' indices (in order), the sphere around them, the vertices+// in it (increasing), the BVH leaves holding them, and the sample.+struct Group {+ std::vector<size_t> passes;+ glm::vec3 center{0.0f};+ float radius = 0.0f;+ std::vector<uint32_t> verts, leaves, sample;+ bool merged = false; // holds more than one copy's query (its leaves may repeat)+ std::vector<glm::vec3> samplePos, sampleNrm;+};++} // namespace++void Sculptor::flushBatch(Mesh& mesh, BVH& bvh) {+ std::vector<PendingDab> dabs;+ dabs.swap(pending_);+ const auto placeOneByOne = [&] {+ const bool was = batching_;+ batching_ = false;+ for (const PendingDab& d : dabs) dab(mesh, bvh, d.type, d.p, d.sym);+ batching_ = was;+ };+ if (dabs.size() < 2) { // (nothing to fuse)+ placeOneByOne();+ return;+ }+ auto clock = std::chrono::steady_clock::now();+ const BrushType type = dabs.front().type;+ const std::vector<SymCopy> cs = copies(dabs.front().sym);++ // The passes, dab by dab and copy by copy (the dab-by-dab path's order), and a group per copy.+ struct Pass {+ DabParams p;+ size_t group = 0;+ bool identity = false, valid = false;+ PlaneKernel k;+ };+ std::vector<Pass> passes;+ std::vector<Group> groups(cs.size());+ float maxRadius = 0.0f;+ for (const PendingDab& d : dabs)+ for (size_t c = 0; c < cs.size(); ++c) {+ Pass ps;+ ps.p = cs[c].identity() ? d.p : transformed(d.p, cs[c]);+ ps.group = c;+ ps.identity = cs[c].identity();+ groups[c].passes.push_back(passes.size());+ passes.push_back(ps);+ maxRadius = std::max(maxRadius, d.p.radius);+ }+ // What a vertex outside a dab's sphere could be moved into it by the dabs before it (near the rim, where+ // their falloff is all but 0): a twentieth of the radius.+ const float margin = 0.05f * maxRadius;+ // Per copy: the capsule from its first dab's centre to its last one's, wide enough for every dab's sphere.+ struct Capsule {+ glm::vec3 a, b;+ float radius;+ };+ std::vector<Capsule> caps(groups.size());+ for (size_t gi = 0; gi < groups.size(); ++gi) {+ Group& g = groups[gi];+ const glm::vec3 a = passes[g.passes.front()].p.center, b = passes[g.passes.back()].p.center;+ const glm::vec3 ab = b - a;+ const float len2 = glm::dot(ab, ab);+ float reach = 0.0f;+ for (size_t j : g.passes) {+ const glm::vec3 d = passes[j].p.center - a;+ const float t = len2 > 0.0f ? std::clamp(glm::dot(d, ab) / len2, 0.0f, 1.0f) : 0.0f;+ reach = std::max(reach, glm::length(d - ab * t) + passes[j].p.radius);+ }+ caps[gi] = {a, b, reach + margin};+ g.center = (a + b) * 0.5f;+ g.radius = 0.5f * std::sqrt(len2) + reach + margin; // (a sphere around the capsule: which copies meet)+ bvh.queryVerticesCapsule(mesh, a, b, reach + margin, g.verts);+ if (mesh.hasHidden()) // (brushVertices: no fully hidden vertices; such regions go one by one below)+ std::erase_if(g.verts, [&](uint32_t v) { return mesh.vertexHidden(v); });+ const auto leaves = bvh.queriedLeaves();+ g.leaves.assign(leaves.begin(), leaves.end());+ }+ // Copies whose capsules meet (a stroke over the mirror plane) share vertices, which go through both copies'+ // dabs in the dab-by-dab order: such copies become one group (every pass of it is tried on each vertex).+ const auto meet = [&](size_t x, size_t y) {+ if (glm::distance(groups[x].center, groups[y].center) >= groups[x].radius + groups[y].radius) return false;+ // segment to segment, sampled finely enough for a conservative answer+ float best = std::numeric_limits<float>::max();+ for (int i = 0; i <= 16; ++i) {+ const glm::vec3 p = glm::mix(caps[x].a, caps[x].b, float(i) / 16.0f);+ const glm::vec3 ab = caps[y].b - caps[y].a;+ const float l2 = glm::dot(ab, ab);+ const float t = l2 > 0.0f ? std::clamp(glm::dot(p - caps[y].a, ab) / l2, 0.0f, 1.0f) : 0.0f;+ best = std::min(best, glm::distance(p, caps[y].a + ab * t));+ }+ return best < caps[x].radius + caps[y].radius + glm::distance(caps[x].a, caps[x].b) / 16.0f;+ };+ IndexBits merged;+ for (size_t x = 0; x < groups.size(); ++x)+ for (size_t y = x + 1; y < groups.size();) {+ if (!meet(x, y)) {+ ++y;+ continue;+ }+ Group& gx = groups[x];+ Group& gy = groups[y];+ if (merged.capacity() < mesh.vertexCount()) merged.resize(mesh.vertexCount());+ for (const std::vector<uint32_t>* list : {&gx.verts, &gy.verts})+ merged.insertParallel(list->size(), 16384, [&](size_t b, size_t e, IndexBits& set) {+ for (size_t i = b; i < e; ++i) set.insertLocal((*list)[i]);+ });+ gx.verts.clear();+ merged.take(gx.verts);+ gx.leaves.insert(gx.leaves.end(), gy.leaves.begin(), gy.leaves.end()); // (deduplicated below)+ gx.merged = true;+ gx.passes.insert(gx.passes.end(), gy.passes.begin(), gy.passes.end());+ std::sort(gx.passes.begin(), gx.passes.end());+ gx.radius = std::max(gx.radius, glm::distance(gx.center, gy.center) + gy.radius);+ caps[x].radius = std::max(caps[x].radius, caps[y].radius + glm::distance(caps[x].a, caps[y].a)); // (covers both)+ groups.erase(groups.begin() + std::ptrdiff_t(y));+ caps.erase(caps.begin() + std::ptrdiff_t(y));+ y = x + 1; // (the grown group may now meet one passed over)+ }+ for (Group& g : groups) { // leaves listed by more than one copy's query: once+ if (!g.merged) continue;+ IndexBits bits;+ bits.resize(bvh.nodeCount());+ bits.insertParallel(g.leaves.size(), 16384, [&](size_t b, size_t e, IndexBits& set) {+ for (size_t i = b; i < e; ++i) set.insertLocal(g.leaves[i]);+ });+ g.leaves.clear();+ bits.take(g.leaves);+ }+ for (Group& g : groups)+ for (size_t j : g.passes) passes[j].group = size_t(&g - groups.data());+ // A mask or hidden faces in reach: their edges are steps the adaptive normals don't see (the dab-by-dab path).+ if (mesh.mask.size() == mesh.vertexCount() || mesh.hasHidden()) {+ std::atomic<bool> masked{false};+ for (const Group& g : groups)+ poolFor(g.verts.size(), [&](size_t b, size_t e) {+ for (size_t i = b; i < e && !masked.load(std::memory_order_relaxed); ++i)+ if (vertexInfluence(mesh, g.verts[i]) < 1.0f) masked.store(true, std::memory_order_relaxed);+ }, 65536);+ if (masked.load()) {+ placeOneByOne();+ return;+ }+ }++ // 2. Dab by dab on the samples: each one's centre and area plane, then its move of the samples.+ const auto& pos = mesh.positions;+ std::vector<glm::vec3>& normals = mesh.normals;+ for (Group& g : groups) {+ // About kAreaSamples in each dab's own sphere (lastRegion_: the size of one, both copies' if they met).+ const size_t inDab = std::max<size_t>(1, lastRegion_ * (g.passes.size() > dabs.size() ? 2 : 1));+ const size_t stride = std::max<size_t>(1, inDab / kAreaSamples);+ g.sample.clear();+ for (size_t i = 0; i < g.verts.size(); i += stride) g.sample.push_back(g.verts[i]);+ g.samplePos.resize(g.sample.size());+ g.sampleNrm.resize(g.sample.size());+ for (size_t i = 0; i < g.sample.size(); ++i) {+ g.samplePos[i] = pos[g.sample[i]];+ g.sampleNrm[i] = normals[g.sample[i]];+ }+ }+ constexpr size_t kBlock = 4096;+ std::vector<Frame> parts;+ for (size_t j = 0; j < passes.size(); ++j) {+ Pass& ps = passes[j];+ Group& g = groups[ps.group];+ // The centre: where the pick's ray meets the surface the dabs before it left. The picked point goes where+ // they move it (with its normal, from the last dab's plane), and back onto the ray through the tangent+ // plane there.+ const glm::vec3 c0 = ps.p.center;+ glm::vec3 c = c0, n = ps.p.pickNormal;+ bool moved = false;+ for (size_t i : g.passes) {+ if (i >= j) break;+ if (!passes[i].valid) continue;+ const PlaneKernel& k = passes[i].k;+ if (glm::length(n) == 0.0f) n = k.areaNormal;+ if (!k.reaches(c)) continue;+ glm::vec3 d;+ n = k.offsetAndNormal(c, n, std::exp2(2.0f * std::clamp(k.focal, -1.0f, 1.0f)), d);+ c += d;+ moved = true;+ }+ if (moved) {+ const glm::vec3 ray = glm::length(ps.p.pickDir) > 0.0f ? glm::normalize(ps.p.pickDir) : glm::normalize(ps.p.viewDir);+ const float across = glm::dot(ray, n);+ if (std::abs(across) > 0.2f) c = c0 + ray * (glm::dot(c - c0, n) / across);+ }+ ps.p.center = c;+ const DabParams& p = ps.p;+ const size_t ns = g.sample.size();+ parts.assign((ns + kBlock - 1) / kBlock, Frame{});+ poolFor(ns, [&](size_t b, size_t e) {+ Frame& acc = parts[b / kBlock];+ for (size_t i = b; i < e; ++i) {+ const glm::vec3& x = g.samplePos[i];+ const float dist = brushDistance(p, x);+ if (dist > p.radius) continue;+ const float w = brushFalloff(dist, p.radius, p.focal);+ acc.normal += g.sampleNrm[i] * w;+ acc.center += x * w;+ acc.weight += w;+ }+ }, kBlock);+ Frame sum;+ for (const Frame& f : parts) {+ sum.normal += f.normal;+ sum.center += f.center;+ sum.weight += f.weight;+ }+ if (sum.weight <= 0.0f) continue; // (the dab-by-dab path: a dab that reaches nothing does nothing)+ const float nlen = glm::length(sum.normal);+ PlaneKernel& k = ps.k;+ k.type = type;+ k.center = p.center;+ k.viewDir = p.viewDir;+ k.areaNormal = nlen > 1e-12f ? sum.normal / nlen : -p.viewDir;+ k.areaCenter = sum.center / sum.weight;+ const float strokeLen = glm::length(p.strokeDelta);+ k.strokeDir = strokeLen > 1e-12f ? p.strokeDelta / strokeLen : glm::vec3(0.0f);+ k.r = p.radius;+ k.s = std::clamp(p.strength, 0.0f, 1.0f) * std::clamp(p.pressure, 0.0f, 1.0f);+ k.sign = p.invert ? -1.0f : 1.0f;+ k.focal = p.focal;+ k.invert = p.invert;+ k.viewFalloff = p.viewFalloff;+ ps.valid = true;+ if (ps.identity) lastFrame = {p.center, k.areaNormal, k.areaCenter, true};+ const float kexp = std::exp2(2.0f * std::clamp(k.focal, -1.0f, 1.0f));+ poolFor(ns, [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) {+ const glm::vec3 x = g.samplePos[i];+ if (!k.reaches(x)) continue;+ glm::vec3 d;+ g.sampleNrm[i] = k.offsetAndNormal(x, g.sampleNrm[i], kexp, d);+ g.samplePos[i] = x + d;+ }+ }, kBlock);+ ++timings.dabs;+ }++ // 3. Every vertex through its dabs; 4. the undo entries and the moves (displaceAll), once.+ if (curvature_.size() != mesh.vertexCount() || curvatureMesh_ != &mesh) {+ curvature_.assign(mesh.vertexCount(), 0.0f);+ curvatureFilled_ = false;+ curvatureMesh_ = &mesh;+ }+ 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);+ givenInfluence_.resize(n);+ std::vector<const PlaneKernel*> ks;+ for (size_t j : g.passes)+ if (passes[j].valid) ks.push_back(&passes[j].k);+ const float kexp = ks.empty() ? 1.0f : std::exp2(2.0f * std::clamp(ks.front()->focal, -1.0f, 1.0f)); // (one brush)+ if (ks.empty()) continue;+ poolFor(n, [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) {+ const uint32_t v = g.verts[i];+ const glm::vec3 x0 = pos[v];+ glm::vec3 x = x0, nv = normals[v];+ bool reached = false;+ for (const PlaneKernel* k : ks) {+ if (!k->reaches(x)) continue;+ if (!reached && curvatureStamp_[v] != stamp) { // (the cavity term from before the stroke moved it)+ curvatureStamp_[v] = stamp;+ curvature_[v] = mesh.curvatureAt(v);+ }+ reached = true;+ glm::vec3 d;+ nv = k->offsetAndNormal(x, nv, kexp, d);+ x += d;+ }+ scratch_[i] = x - x0;+ givenInfluence_[i] = reached ? 1.0f : 0.0f;+ if (reached) normals[v] = nv;+ }+ }, 2048);+ displaceAll(mesh, g.verts, scratch_, false, givenInfluence_.data());+ timings.kernelMs += lapMs(clock);+ bvh.refitLeaves(mesh, g.leaves);+ timings.bvhMs += lapMs(clock);+ timings.vertices += n;+ addLazyLeaves(bvh, g.leaves);+ lastLazyLeaves_.insert(lastLazyLeaves_.end(), g.leaves.begin(), g.leaves.end());+ mesh.markDirtyAround(n, 4096, [&](size_t b, size_t e, IndexBits& set) {+ for (size_t i = b; i < e; ++i)+ if (givenInfluence_[i] > 0.0f) set.insertLocal(g.verts[i]);+ });+ timings.normalsMs += lapMs(clock);+ }+ lazyMesh_ = &mesh;+ lazyBvh_ = &bvh;+}++} // namespace sfdiff --git a/src/core/SculptKernel.h b/src/core/SculptKernel.hnew file mode 100644index 00000000..2c1c70f6--- /dev/null+++ b/src/core/SculptKernel.h@@ -0,0 +1,160 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// The brushes whose offset depends only on where a vertex is (and the dab's frame on the surface): one formula+// shared by the per-dab path (Sculptor::dabSingle) and the fused batches of big brushes (SculptBatch.cpp), so the+// two can't drift apart.+#pragma once++#include "core/Sculpt.h"++#include <glm/glm.hpp>++#include <algorithm>+#include <cmath>++namespace sf {++// Standard, Clay, Flatten, Crease, Pinch, Dam Standard and Trim: the offset of a point from its place alone.+// Fused batches take all but Trim (its crisp cut shows where a centre moved by a hair).+inline bool isPlaneKernelBrush(BrushType t) {+ return t == BrushType::Standard || t == BrushType::Clay || t == BrushType::Flatten || t == BrushType::Crease ||+ t == BrushType::Pinch || t == BrushType::DamStandard || t == BrushType::Trim;+}++struct PlaneKernel {+ BrushType type = BrushType::Standard;+ glm::vec3 center{0.0f}, viewDir{0.0f, 0.0f, -1.0f};+ glm::vec3 areaNormal{0.0f, 0.0f, 1.0f}, areaCenter{0.0f}, strokeDir{0.0f};+ float r = 0.1f, s = 0.5f, sign = 1.0f, focal = 0.0f;+ bool invert = false, viewFalloff = false;++ static glm::vec3 flat(const glm::vec3& v, const glm::vec3& n) { return v - n * glm::dot(v, n); } // onto the plane++ // The offset of a point x with falloff f there.+ glm::vec3 offsetWith(const glm::vec3& x, float f) const {+ glm::vec3 d(0.0f);+ switch (type) {+ case BrushType::Standard:+ d = areaNormal * (sign * s * r * 0.06f * f);+ break;+ case BrushType::Flatten: {+ const float h = glm::dot(x - areaCenter, areaNormal);+ d = -areaNormal * (h * s * f * (invert ? -0.5f : 1.0f));+ break;+ }+ case BrushType::Clay: {+ // Raise a plane slightly above the surface and pull everything below up to it.+ const glm::vec3 planePoint = areaCenter + areaNormal * (sign * r * 0.05f * s);+ const float h = glm::dot(x - planePoint, areaNormal);+ if (sign * h < 0.0f) d = -areaNormal * (h * f * clayPull(s));+ break;+ }+ case BrushType::Crease: {+ // Carves a sharp valley by default (raises a ridge when inverted).+ const float sharp = f * f;+ d = areaNormal * (-sign * s * r * 0.05f * sharp);+ d += flat(center - x, areaNormal) * (0.25f * s * f);+ break;+ }+ case BrushType::Pinch:+ d = flat(center - x, areaNormal) * (sign * 0.2f * s * f);+ break;+ case BrushType::DamStandard: {+ // Narrow, deep V-groove (a ridge when inverted); the sides are always+ // pinched in toward the stroke line, so the crease stays sharp.+ const float core = f * f * f * f;+ d = areaNormal * (-sign * s * r * 0.08f * core);+ glm::vec3 pull = flat(center - x, areaNormal);+ pull -= strokeDir * glm::dot(pull, strokeDir);+ d += pull * (0.3f * s * f);+ break;+ }+ case BrushType::Trim: {+ // Trim Dynamic: a plane just below the area cuts off whatever sticks out+ // above it (fills up to it when inverted). sqrt falloff for a crisp edge.+ const glm::vec3 planePoint = areaCenter - areaNormal * (sign * r * 0.02f * s);+ const float h = glm::dot(x - planePoint, areaNormal);+ if (sign * h > 0.0f) d = -areaNormal * (h * std::min(1.0f, std::sqrt(f) * (0.5f + s)));+ break;+ }+ default:+ break;+ }+ return d;+ }+ // ... with the round falloff (no alpha).+ glm::vec3 offset(const glm::vec3& x) const {+ return offsetWith(x, brushFalloff(brushDistance(x, center, viewDir, viewFalloff), r, focal));+ }+ // Inside the dab's reach (the falloff is 0 beyond it).+ bool reaches(const glm::vec3& x) const { return brushDistance(x, center, viewDir, viewFalloff) <= r; }++ // The offset at x (into d) and the normal n of the surface there after the move: the tangents carried by the+ // offset's derivative, t + J t (J worked out from the formulas above: one falloff slope, no extra offsets),+ // so n' is the moved surface's normal to first order (exact where the brush's slope is constant). Not with+ // viewFalloff (carriedNormal's finite differences then). `k` = exp2(2 focal), the falloff's exponent.+ glm::vec3 offsetAndNormal(const glm::vec3& x, const glm::vec3& n, float k, glm::vec3& d) const {+ const glm::vec3 o = x - center;+ const float dist = glm::length(o);+ const float t = std::clamp(1.0f - dist / r, 0.0f, 1.0f);+ const float g = focal != 0.0f ? std::pow(t, k) : t;+ const float f = g * g * (3.0f - 2.0f * g);+ d = offsetWith(x, f);+ if (t <= 0.0f) return n; // (outside: no move)+ // df/d(dist) = df/dg dg/dt dt/d(dist).+ const float dg = focal != 0.0f ? (t > 0.0f ? k * g / t : 0.0f) : 1.0f;+ const float slope = dist > 1e-12f && t < 1.0f ? -6.0f * g * (1.0f - g) * dg / r : 0.0f;+ const glm::vec3 grad = dist > 1e-12f ? o * (slope / dist) : glm::vec3(0.0f); // the falloff's gradient+ const glm::vec3 t1 = glm::normalize(std::abs(n.x) < 0.9f ? glm::cross(n, glm::vec3(1, 0, 0)) : glm::cross(n, glm::vec3(0, 1, 0)));+ const glm::vec3 t2 = glm::cross(n, t1);+ const auto carry = [&](const glm::vec3& tv) -> glm::vec3 { // J tv+ const float gf = glm::dot(grad, tv);+ const glm::vec3 tflat = tv - areaNormal * glm::dot(areaNormal, tv);+ switch (type) {+ case BrushType::Standard:+ return areaNormal * (sign * s * r * 0.06f * gf);+ case BrushType::Flatten: {+ const float h = glm::dot(x - areaCenter, areaNormal);+ return -areaNormal * (s * (invert ? -0.5f : 1.0f) * (f * glm::dot(areaNormal, tv) + h * gf));+ }+ case BrushType::Clay: {+ const glm::vec3 planePoint = areaCenter + areaNormal * (sign * r * 0.05f * s);+ const float h = glm::dot(x - planePoint, areaNormal);+ if (!(sign * h < 0.0f)) return glm::vec3(0.0f);+ return -areaNormal * (clayPull(s) * (f * glm::dot(areaNormal, tv) + h * gf));+ }+ case BrushType::Crease: {+ const glm::vec3 q = flat(center - x, areaNormal);+ return areaNormal * (-sign * s * r * 0.05f * 2.0f * f * gf) + (q * gf - tflat * f) * (0.25f * s);+ }+ case BrushType::Pinch: {+ const glm::vec3 q = flat(center - x, areaNormal);+ return (q * gf - tflat * f) * (sign * 0.2f * s);+ }+ case BrushType::DamStandard: {+ glm::vec3 q = flat(center - x, areaNormal);+ q -= strokeDir * glm::dot(q, strokeDir);+ const glm::vec3 dq = -(tflat - strokeDir * glm::dot(tflat, strokeDir));+ return areaNormal * (-sign * s * r * 0.08f * 4.0f * f * f * f * gf) + (q * gf + dq * f) * (0.3f * s);+ }+ case BrushType::Trim: {+ const glm::vec3 planePoint = areaCenter - areaNormal * (sign * r * 0.02f * s);+ const float h = glm::dot(x - planePoint, areaNormal);+ if (!(sign * h > 0.0f)) return glm::vec3(0.0f);+ const float sf = std::sqrt(f), w = sf * (0.5f + s);+ if (w >= 1.0f) return -areaNormal * glm::dot(areaNormal, tv);+ const float dw = sf > 1e-12f ? (0.5f + s) * 0.5f / sf * gf : 0.0f;+ return -areaNormal * (w * glm::dot(areaNormal, tv) + h * dw);+ }+ default:+ return glm::vec3(0.0f);+ }+ };+ const glm::vec3 m = glm::cross(t1 + carry(t1), t2 + carry(t2));+ const float len = glm::length(m);+ return len > 1e-20f && glm::dot(m, n) > 0.0f ? m / len : n;+ }+};++} // namespace sfdiff --git a/tests/test_beta_boost.cpp b/tests/test_beta_boost.cppnew file mode 100644index 00000000..65b32fea--- /dev/null+++ b/tests/test_beta_boost.cpp@@ -0,0 +1,266 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// Scale-adaptive dabs (Sculptor::adaptiveDabs): a big brush's stroke comes out as the exact path's, within a+// small tolerance, for every brush that takes the adaptive path; small brushes are unchanged, bit for bit.+// SF_BOOST_TRIS=15000000 times one big Clay stroke on the standard head (the 240 px case) both ways.+#include "TestHarness.h"++#include "core/Benchmark.h"+#include "core/BrushAlpha.h"+#include "core/Sculpt.h"+#include "core/SculptKernel.h"++#include <chrono>+#include <algorithm>+#include <cmath>+#include <tuple>++using namespace sf;++namespace {++std::vector<glm::vec3>* g_record = nullptr; // (diagnosis: the exact run's centres, replayed)+const std::vector<glm::vec3>* g_replay = nullptr;++struct StrokeResult {+ Mesh mesh;+ SculptTimings timings;+ double ms = 0.0, flushMs = 0.0;+};++// 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) {+ Document d = makeDoc(head);+ if (mask) { // a hard-edged mask over part of the stroke (such regions take the exact path)+ d.mesh.ensureMask();+ for (size_t v = 0; v < d.mesh.vertexCount(); ++v) d.mesh.mask[v] = d.mesh.positions[v].y > 0.1f ? 1.0f : 0.0f;+ }+ BrushAlpha stamp;+ if (alpha) {+ stamp.width = stamp.height = 64;+ stamp.values.resize(64 * 64);+ for (int y = 0; y < 64; ++y)+ for (int x = 0; x < 64; ++x) stamp.values[size_t(y) * 64 + x] = 0.5f + 0.5f * std::sin(0.3f * float(x)) * std::cos(0.2f * float(y));+ }+ d.sculptor.adaptiveDabs = adaptive;+ d.sculptor.beginStroke(d.mesh, brushName(type));+ StrokeResult out;+ const auto t0 = std::chrono::steady_clock::now();+ 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);+ d.sculptor.beginBatch();+ }+ DabParams p;+ const float t = -0.5f + float(i) * 0.15f * radius;+ p.center = glm::vec3(t, 0.1f, 1.0f);+ if (RayHit hit; d.bvh.raycast(d.mesh, glm::vec3(t, 0.1f, 5.0f), glm::vec3(0, 0, -1), hit)) {+ p.center = hit.position;+ p.pickNormal = glm::normalize(hit.normal);+ }+ if (g_record) g_record->push_back(p.center);+ if (g_replay) p.center = (*g_replay)[size_t(i)];+ p.viewDir = p.pickDir = glm::vec3(0, 0, -1);+ p.radius = radius;+ p.strength = strength;+ p.strokeDelta = glm::vec3(0.15f * radius, 0, 0);+ if (alpha) p.alpha = &stamp;+ d.sculptor.dab(d.mesh, d.bvh, type, p, Symmetry{true, false, false});+ }+ d.sculptor.endBatch(d.mesh, d.bvh);+ const auto t1 = std::chrono::steady_clock::now();+ d.sculptor.flushDraft(d.mesh, d.bvh);+ const auto t2 = std::chrono::steady_clock::now();+ out.timings = d.sculptor.timings;+ out.ms = std::chrono::duration<double, std::milli>(t1 - t0).count();+ out.flushMs = std::chrono::duration<double, std::milli>(t2 - t1).count();+ d.sculptor.endStroke();+ out.mesh = std::move(d.mesh);+ return out;+}++struct Difference {+ float moved = 0.0f, position = 0.0f, normalDeg = 0.0f; // the most; normalDeg: of 999 vertices in 1000+ double meanDeg = 0.0;+};+Difference compare(const Mesh& start, const Mesh& a, const Mesh& b) {+ Difference r;+ std::vector<float> deg;+ for (size_t v = 0; v < a.positions.size(); ++v) {+ const float moved = glm::length(a.positions[v] - start.positions[v]);+ r.moved = std::max(r.moved, moved);+ r.position = std::max(r.position, glm::length(a.positions[v] - b.positions[v]));+ if (moved == 0.0f) continue;+ deg.push_back(glm::degrees(std::acos(std::clamp(glm::dot(a.normals[v], b.normals[v]), -1.0f, 1.0f))));+ r.meanDeg += deg.back();+ }+ if (deg.empty()) return r;+ r.meanDeg /= double(deg.size());+ std::nth_element(deg.begin(), deg.begin() + std::ptrdiff_t(deg.size() * 999 / 1000), deg.end());+ r.normalDeg = deg[deg.size() * 999 / 1000];+ return r;+}++} // namespace++TEST(adaptive_dabs_match_exact_strokes) {+ const Mesh head = makeBenchHead(3'000'000); // ~1.5M vertices: a 0.6 radius holds well over the threshold+ struct Case {+ BrushType type;+ bool alpha = false, mask = false;+ float strength = 0.3f;+ };+ // (Inflate, Nudge, Clay Block, alpha brushes and masked regions take the exact path: adaptive_dabs_leave_small_brushes_alone checks it.)+ for (const Case c : {Case{BrushType::Clay}, Case{BrushType::Standard}, Case{BrushType::Flatten},+ Case{BrushType::Pinch}, Case{BrushType::Crease}, Case{BrushType::Trim}}) {+ const StrokeResult exact = strokeOn(head, c.type, 0.8f, false, 16, c.alpha, c.mask, c.strength);+ const StrokeResult fast = strokeOn(head, c.type, 0.8f, true, 16, c.alpha, c.mask, c.strength);+ const Difference d = compare(head, exact.mesh, fast.mesh);+ const double perDab = double(fast.timings.vertices) / double(std::max<uint64_t>(1, fast.timings.dabs));+ std::printf(" %-12s%s%s %.0fk verts/dab, moved %.4f, adaptive within %.2g (%.2f%%), normals: mean %.3f, 99.9%% within %.2f deg; %.0f vs %.0f ms\n",+ brushName(c.type), c.alpha ? " alpha" : "", c.mask ? " mask" : "", perDab / 1e3, double(d.moved), double(d.position),+ 100.0 * double(d.position / std::max(d.moved, 1e-9f)), d.meanDeg, double(d.normalDeg), exact.ms, fast.ms);+ CHECK(d.moved > 1e-3f); // (the stroke did something)+ CHECK(perDab > double(Sculptor::kAdaptiveMinVertices)); // (on the adaptive path)+ CHECK(d.position <= 0.01f * d.moved); // within 1% of how far the stroke moved the surface+ // After the stroke the exact normals of nearly the same surface (where a plane brush's crease line moved+ // by a hair, a few vertices' normals turn by some degrees).+ CHECK(d.meanDeg < 0.5);+ }+}++TEST(adaptive_dabs_fused_batches_match_exact_strokes) {+ // Batches of 2-6 dabs placed in one pass (Sculptor::beginBatch), each picked on the surface as its batch began.+ const Mesh head = makeBenchHead(3'000'000);+ for (BrushType type : {BrushType::Clay, BrushType::Standard, BrushType::Flatten, BrushType::Pinch, BrushType::Crease,+ BrushType::DamStandard})+ for (int batch : {2, 6}) {+ std::vector<glm::vec3> centres;+ static const bool replay = std::getenv("SF_BOOST_REPLAY") != nullptr;+ if (replay) g_record = ¢res;+ const StrokeResult exact = strokeOn(head, type, 0.8f, false, 18);+ g_record = nullptr;+ if (replay) g_replay = ¢res;+ const StrokeResult fused = strokeOn(head, type, 0.8f, true, 18, false, false, 0.3f, batch);+ g_replay = nullptr;+ const Difference d = compare(head, exact.mesh, fused.mesh);+ std::printf(" %-12s batches of %d: moved %.4f, within %.2g (%.2f%%), normals: mean %.3f, 99.9%% within %.2f deg; %.0f vs %.0f ms\n",+ brushName(type), batch, double(d.moved), double(d.position),+ 100.0 * double(d.position / std::max(d.moved, 1e-9f)), d.meanDeg, double(d.normalDeg), exact.ms, fused.ms);+ CHECK(d.moved > 1e-3f);+ CHECK(d.position <= 0.025f * d.moved); // within 2.5% (the centres are picked as the batch began)+ CHECK(d.meanDeg < 0.5);+ }+}++TEST(adaptive_dabs_analytic_normals_match_finite_differences) {+ // PlaneKernel::offsetAndNormal against the moved surface's normal from offsets a hair apart, on a bumpy patch.+ for (BrushType type : {BrushType::Standard, BrushType::Clay, BrushType::Flatten, BrushType::Crease, BrushType::Pinch,+ BrushType::DamStandard, BrushType::Trim})+ for (float focal : {0.0f, -0.6f, 0.5f})+ for (bool invert : {false, true}) {+ PlaneKernel k;+ k.type = type;+ k.center = glm::vec3(0.05f, -0.02f, 0.0f);+ k.areaNormal = glm::normalize(glm::vec3(0.1f, 0.05f, 1.0f));+ k.areaCenter = glm::vec3(0.0f, 0.0f, -0.01f);+ k.strokeDir = glm::vec3(1, 0, 0);+ k.r = 0.5f;+ k.s = 0.6f;+ k.sign = invert ? -1.0f : 1.0f;+ k.invert = invert;+ k.focal = focal;+ const float kexp = std::exp2(2.0f * focal);+ float worst = 0.0f;+ for (int i = 0; i < 400; ++i) {+ const float u = -0.45f + 0.9f * float(i % 20) / 19.0f, w = -0.45f + 0.9f * float(i / 20) / 19.0f;+ const glm::vec3 x(u, w, 0.03f * std::sin(9.0f * u) * std::cos(7.0f * w));+ const glm::vec3 n = glm::normalize(glm::vec3(-0.27f * std::cos(9.0f * u) * std::cos(7.0f * w),+ 0.21f * std::sin(9.0f * u) * std::sin(7.0f * w), 1.0f));+ glm::vec3 d;+ const glm::vec3 a = k.offsetAndNormal(x, n, kexp, d);+ CHECK(glm::length(d - k.offset(x)) < 1e-6f);+ const glm::vec3 t1 = glm::normalize(glm::cross(n, glm::vec3(1, 0, 0))), t2 = glm::cross(n, t1);+ const float h = 1e-3f;+ const glm::vec3 p1 = t1 + (k.offset(x + t1 * h) - k.offset(x - t1 * h)) / (2.0f * h);+ const glm::vec3 p2 = t2 + (k.offset(x + t2 * h) - k.offset(x - t2 * h)) / (2.0f * h);+ const glm::vec3 fd = glm::normalize(glm::cross(p1, p2));+ if (glm::dot(fd, n) <= 0.0f) continue; // (folded: both keep n)+ // (a negative focal's falloff is infinitely steep at the rim: differences a hair apart can't follow)+ if (k.r - glm::length(x - k.center) < 0.05f) continue;+ // (where Clay / Trim cut in, the slope jumps: points a hair from the cut are skipped)+ const float cut = glm::length(k.offset(x + t1 * 2e-3f) - k.offset(x - t1 * 2e-3f)) + glm::length(k.offset(x + t2 * 2e-3f) - k.offset(x - t2 * 2e-3f));+ if ((type == BrushType::Clay || type == BrushType::Trim) && cut > 0.0f && glm::length(d) < 1e-4f) continue;+ worst = std::max(worst, glm::degrees(std::acos(std::clamp(glm::dot(a, fd), -1.0f, 1.0f))));+ }+ if (worst >= 1.0f)+ std::printf(" %s focal %.1f%s: analytic normals within %.3f deg\n", brushName(type), double(focal),+ invert ? " inverted" : "", double(worst));+ CHECK(worst < 1.0f); // (Trim: its min(1, ...) bends the slope)+ }+}++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);+ for (const auto& [type, radius, alpha] : {std::tuple{BrushType::Clay, 0.08f, false}, std::tuple{BrushType::Smooth, 0.8f, false},+ std::tuple{BrushType::Nudge, 0.8f, false}, std::tuple{BrushType::Inflate, 0.8f, false}, std::tuple{BrushType::ClayBuildup, 0.8f, false}, std::tuple{BrushType::Standard, 0.8f, true}}) {+ const StrokeResult exact = strokeOn(head, type, radius, false, 8, alpha), fast = strokeOn(head, type, radius, true, 8, alpha);+ CHECK(exact.mesh.positions == fast.mesh.positions);+ CHECK(exact.mesh.normals == fast.mesh.normals);+ }+ const StrokeResult exact = strokeOn(head, BrushType::Clay, 0.8f, false, 8, false, true);+ const StrokeResult fast = strokeOn(head, BrushType::Clay, 0.8f, true, 8, false, true);+ CHECK(exact.mesh.positions == fast.mesh.positions); // (a masked region)+}++TEST(adaptive_dabs_normals_during_the_stroke) {+ // Mid-stroke (before the exact normals catch up) the adaptive ones are already close to the exact ones.+ const Mesh head = makeBenchHead(1'500'000);+ auto run = [&](bool adaptive) {+ Document d = makeDoc(head);+ d.sculptor.adaptiveDabs = adaptive;+ d.sculptor.beginStroke(d.mesh, "Clay");+ for (int i = 0; i < 8; ++i) {+ DabParams p;+ p.center = glm::vec3(-0.3f + 0.09f * float(i), 0.1f, 0.0f);+ if (RayHit hit; d.bvh.raycast(d.mesh, glm::vec3(p.center.x, 0.1f, 5.0f), glm::vec3(0, 0, -1), hit))+ p.center = hit.position;+ p.radius = 0.6f;+ p.strength = 1.0f;+ d.sculptor.dab(d.mesh, d.bvh, BrushType::Standard, p, Symmetry{});+ }+ Mesh m = d.mesh; // (before endStroke's exact normals)+ d.sculptor.endStroke();+ return m;+ };+ const Mesh exact = run(false), fast = run(true);+ double sum = 0.0;+ float worst = 0.0f;+ for (size_t v = 0; v < exact.normals.size(); ++v) {+ const float a = glm::degrees(std::acos(std::clamp(glm::dot(exact.normals[v], fast.normals[v]), -1.0f, 1.0f)));+ sum += a;+ worst = std::max(worst, a);+ }+ const double mean = sum / double(exact.normals.size());+ std::printf(" mid-stroke normals: mean %.4f deg, worst %.2f deg from the exact ones\n", mean, double(worst));+ CHECK(mean < 0.05);+ CHECK(worst < 5.0f);+}++TEST(adaptive_dabs_timing) {+ const char* env = std::getenv("SF_BOOST_TRIS");+ if (!env) return; // (a measurement, not a check: SF_BOOST_TRIS=15000000)+ const Mesh head = makeBenchHead(size_t(std::strtod(env, nullptr)));+ const float radius = std::getenv("SF_BOOST_RADIUS") ? float(std::strtod(std::getenv("SF_BOOST_RADIUS"), nullptr)) : 0.7f;+ const int batch = std::getenv("SF_BOOST_BATCH") ? std::atoi(std::getenv("SF_BOOST_BATCH")) : 4;+ for (bool adaptive : {false, true}) {+ const StrokeResult r = strokeOn(head, BrushType::Clay, radius, adaptive, 16, false, false, 0.3f, adaptive ? batch : 0);+ const SculptTimings& t = r.timings;+ std::printf(" %s: %zu vertices, %.1f k verts/dab, %.1f ms/dab (kernel %.1f, normals %.1f, BVH %.1f), end %.0f ms\n",+ adaptive ? "adaptive" : "exact ", head.vertexCount(), double(t.vertices) / double(std::max<uint64_t>(1, t.dabs)) / 1e3,+ r.ms / 16.0, t.kernelMs / 16.0, t.normalsMs / 16.0, t.bvhMs / 16.0, r.flushMs);+ }+}