Skip to content

Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.

Predictive strokes: dabs solved on a thread, a GPU preview of the dabs still to come, the pen predicted a frame ahead

islandmed469edc7e3461
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ff6dc1f0..46ef8cf3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -207,6 +207,7 @@ if(SCULPTFORGE_BUILD_APP)
src/app/Bench.cpp
src/app/CrashLog.cpp
src/app/App.cpp
+ src/app/AsyncStroke.cpp
src/app/Demo.cpp
src/app/UiTest.cpp
src/app/Actions.cpp
@@ -256,6 +257,7 @@ if(SCULPTFORGE_BUILD_APP)
src/render/GL.cpp
src/render/Camera.cpp
src/render/Clusters.cpp
+ src/render/Ghost.cpp
src/render/Renderer.cpp
)
target_link_libraries(sculptforge PRIVATE sculptforge_core sculptforge_ai sculptforge_phone imgui SDL3::SDL3)
@@ -435,7 +437,7 @@ if(SCULPTFORGE_BUILD_TESTS)
file(GLOB SCULPTFORGE_TEST_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_*.cpp)
add_executable(sculptforge_tests ${SCULPTFORGE_TEST_SOURCES} src/cli/McpServer.cpp src/app/AssetLibrary.cpp
src/app/UiLayout.cpp src/app/ShellGeometry.cpp src/render/Camera.cpp
- src/render/Clusters.cpp)
+ src/render/Clusters.cpp src/render/Ghost.cpp)
target_include_directories(sculptforge_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests)
target_compile_definitions(sculptforge_tests PRIVATE SF_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/data")
target_link_libraries(sculptforge_tests PRIVATE sculptforge_core sculptforge_ai sculptforge_phone)
diff --git a/src/app/App.cpp b/src/app/App.cpp
index 20b9c572..ec2ff634 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -1,6 +1,7 @@
// SculptForge - open source digital sculpting
// SPDX-License-Identifier: GPL-3.0-or-later
#include "app/App.h"
+#include "app/AsyncStroke.h"
#include "app/CrashLog.h"
#include "ai/AiCommands.h"
@@ -278,6 +279,8 @@ bool App::init(const AppOptions& options) {
}
void App::shutdown() {
+ if (doc_.sculptor.strokeActive()) endStroke();
+ solver_.reset(); // (its thread uses the renderer)
flushBrushEdits();
if (blenderInitialised_) writeBridgeHeartbeat(blenderExchangeRoot(), true); // the add-on starts a new one
@@ -608,6 +611,7 @@ void App::redrawIn(double seconds) {
bool App::wantsFrames(double now) const {
return now < activeUntil_ || drag_ != Drag::None || doc_.sculptor.strokeActive() ||
!dabQueue_.empty() || // a stroke's dabs still waiting (drainStrokeDabs) are placed a few each frame
+ strokeFinishing_ || solverBusy() || // the solver thread is placing a stroke's dabs (AsyncStroke.cpp)
backgroundBusy() || playingLayer_ >= 0 || faceTakePlaying_ ||
spaceMouseMoving() || // a held SpaceMouse cap sends no events, but the view keeps moving (SpaceMouse.cpp)
perf_.framesStill < 10; // the view just moved: Fast Navigation's full-quality frame comes after it stops
@@ -646,13 +650,29 @@ int App::run() {
activeUntil_ = started + 1.0;
// --measure-stroke: two turns of a circle over the model in S seconds. The pointer is read once per pass of the loop, as a busy program gets it (the window system hands
// over only the latest position), so a pass that places many dabs makes the next gap longer.
+ // The brush behind the pointer is measured twice: the surface (the last dab whose result is on the GPU) and
+ // the preview (the predictive stroke's head, render/Ghost.h; the surface when there is none). Release is how
+ // long letting go holds up the UI, then how long until the last dab is on screen.
struct StrokeMeasure {
bool started = false, moving = false; // moving: the press's first frame is on screen, the pointer moves
+ bool releasing = false; // let go: waiting for the last dab to reach the screen
double pressMs = 0.0, start = 0.0, lastFrame = 0.0, maxFrame = 0.0, sumFrames = 0.0, maxLag = 0.0, sumLag = 0.0;
+ double pressAt = 0.0, firstSurfaceMs = 0.0, maxPreviewLag = 0.0, sumPreviewLag = 0.0;
+ double releaseMs = 0.0, releaseAt = 0.0;
uint64_t frames = 0, samples = 0, lagSamples = 0;
float cx = 0.0f, cy = 0.0f, r = 0.0f;
std::vector<glm::vec2> dabs;
+ // SF_MEASURE_VERIFY=1: the mesh before the stroke, to check undo and replay the dabs (verifyStroke).
+ uint64_t before = 0;
+ // SF_MEASURE_SNAPSHOT=prefix: the canvas at a few points of the stroke, as prefix-N.png.
+ int snapshots = 0;
} sm;
+ // SF_MEASURE_POP=1: how much the picture jumps when the solver's result replaces the preview (commitSolverBatch).
+ const char* popEnv = std::getenv("SF_MEASURE_POP");
+ popProbe_ = popEnv && *popEnv && *popEnv != '0';
+ const char* verifyEnv = std::getenv("SF_MEASURE_VERIFY");
+ const bool verify = verifyEnv && *verifyEnv && *verifyEnv != '0';
+ const char* snapshotEnv = std::getenv("SF_MEASURE_SNAPSHOT");
while (running_) {
const double t0 = seconds();
if (options_.measureIdle > 0.0f) {
@@ -713,9 +733,10 @@ int App::run() {
sm.cy = c.y + c.h * 0.5f;
sm.r = 0.25f * std::min(c.w, c.h);
sm.started = true;
- sm.start = sm.lastFrame = seconds();
+ sm.start = sm.lastFrame = sm.pressAt = seconds();
strokeProbe_ = &sm.dabs;
if (options_.measureStrokeRadius > 0.0f) radiusPx_ = std::clamp(options_.measureStrokeRadius, 2.0f, 800.0f);
+ if (verify) sm.before = meshStateHash(doc_.mesh);
onMouseDown(sm.cx + sm.r, sm.cy, SDL_BUTTON_LEFT);
if (drag_ != Drag::Stroke) {
std::printf("Stroke: no stroke began (nothing to sculpt under the circle's start)\n");
@@ -727,47 +748,72 @@ int App::run() {
onMouseMove(sm.cx + sm.r * float(std::cos(a)), sm.cy + sm.r * float(std::sin(a)), 0.0f, 0.0f, false);
++sm.samples;
if (u >= 1.0) {
- const double releaseStart = seconds();
- onMouseUp(); // places the dabs still waiting
- const double releaseMs = (seconds() - releaseStart) * 1000.0;
+ sm.releaseAt = seconds();
+ onMouseUp(); // places the dabs still waiting (or, with predictive strokes, leaves them to the solver)
+ sm.releaseMs = (seconds() - sm.releaseAt) * 1000.0;
+ sm.moving = false;
+ sm.releasing = true;
+ }
+ } else if (sm.releasing && !doc_.sculptor.strokeActive()) {
+ {
+ const double releaseDoneMs = (seconds() - sm.releaseAt) * 1000.0;
+ const double releaseMs = sm.releaseMs;
strokeProbe_ = nullptr;
std::vector<float> off;
for (const glm::vec2& d : sm.dabs) off.push_back(std::abs(std::hypot(d.x - sm.cx, d.y - sm.cy) - sm.r));
std::sort(off.begin(), off.end());
const auto pct = [&](double q) { return off.empty() ? 0.0f : off[std::min(off.size() - 1, size_t(q * off.size()))]; };
- std::printf("Stroke (%s): %zu dabs from %llu pointer samples in %.1f s, circle radius %.0f px, brush radius %.0f px\n"
+ const double frameMean = sm.frames ? sm.sumFrames / double(sm.frames) * 1000.0 : 0.0;
+ const double lagMean = sm.lagSamples ? sm.sumLag / double(sm.lagSamples) : 0.0;
+ const double previewMean = sm.lagSamples ? sm.sumPreviewLag / double(sm.lagSamples) : 0.0;
+ std::printf("Stroke (%s%s): %zu dabs from %llu pointer samples in %.1f s, circle radius %.0f px, brush radius %.0f px\n"
" off the circle: median %.1f px, 95%% %.1f px, worst %.1f px\n"
- " press %.0f ms; then frames: %llu, mean %.1f ms, worst %.1f ms; brush behind the pointer: mean %.0f px, "
- "worst %.0f px; release %.0f ms\n",
- interactiveStrokes_ ? "queued" : "immediate", sm.dabs.size(), (unsigned long long)sm.samples,
- double(options_.measureStroke), double(sm.r), double(radiusPx_), double(pct(0.5)), double(pct(0.95)),
- double(off.empty() ? 0.0f : off.back()), sm.pressMs, (unsigned long long)sm.frames,
- sm.frames ? sm.sumFrames / double(sm.frames) * 1000.0 : 0.0, sm.maxFrame * 1000.0,
- sm.lagSamples ? sm.sumLag / double(sm.lagSamples) : 0.0, sm.maxLag, releaseMs);
+ " press %.0f ms (first dab's surface on screen %.0f ms); then frames: %llu, mean %.1f ms, worst %.1f ms\n"
+ " brush behind the pointer: surface mean %.0f px, worst %.0f px; preview mean %.0f px, worst %.0f px\n"
+ " release %.0f ms (last dab on screen after %.0f ms)\n",
+ interactiveStrokes_ ? "queued" : "immediate", predictiveStrokes() ? ", predictive" : "", sm.dabs.size(),
+ (unsigned long long)sm.samples, double(options_.measureStroke), double(sm.r), double(radiusPx_),
+ double(pct(0.5)), double(pct(0.95)), double(off.empty() ? 0.0f : off.back()), sm.pressMs,
+ sm.firstSurfaceMs, (unsigned long long)sm.frames, frameMean, sm.maxFrame * 1000.0, lagMean, sm.maxLag,
+ previewMean, sm.maxPreviewLag, releaseMs, releaseDoneMs);
+ // tools/bench_strokes.sh reads this line.
+ std::printf("bench: %.1f %.1f %.1f %.1f %.1f %.1f %.1f %.1f\n", frameMean, sm.maxFrame * 1000.0, lagMean, previewMean,
+ sm.pressMs, sm.firstSurfaceMs, releaseMs, releaseDoneMs);
+ if (popProbe_ && popCommits_ > 0)
+ std::printf("pop: when the solver's dabs replace their preview the view changes by %.3f on average "
+ "(worst %.3f); without the preview those dabs would change it by %.3f (%llu commits)\n",
+ popWith_ / double(popCommits_), popWorst_, popWithout_ / double(popCommits_),
+ (unsigned long long)popCommits_);
std::fflush(stdout);
+ if (verify && !verifyStroke(sm.before, sm.dabs)) return 1;
// The test (ui_stroke_path, a light sphere): every dab placed, all of them on the circle.
const float spacing = std::max(1.0f, radiusPx_ * 0.15f);
const double expected = 4.0 * 3.14159265358979 * double(sm.r) / double(spacing);
return double(sm.dabs.size()) >= 0.9 * expected && !off.empty() && off.back() < spacing ? 0 : 1;
}
}
- drainStrokeDabs(kStrokeBudgetSeconds); // the stroke's queued dabs, for a limited time: then look at the pointer again
+ // The stroke's queued dabs: handed to the solver thread (predictive strokes, AsyncStroke.cpp), or placed here
+ // for a limited time, then look at the pointer again.
+ if (asyncStroke_) pumpStroke();
+ else drainStrokeDabs(kStrokeBudgetSeconds);
Uint64 now = SDL_GetTicksNS();
float dt = float(double(now - lastTicks) * 1e-9);
lastTicks = now;
- processPendingFiles();
- animateLayers(dt);
- pollBrushRequest();
- pollGenerator();
- pollUnreal();
- pollGoz();
- pollBlender();
- pollPhone();
- pollAutoUv();
- pollBake();
- pollQRemesh();
- pollSplatTrain();
- pollSculptable();
+ if (!solverBusy()) { // (the solver thread owns the document's mesh until the stroke's end)
+ processPendingFiles();
+ animateLayers(dt);
+ pollBrushRequest();
+ pollGenerator();
+ pollUnreal();
+ pollGoz();
+ pollBlender();
+ pollPhone();
+ pollAutoUv();
+ pollBake();
+ pollQRemesh();
+ pollSplatTrain();
+ pollSculptable();
+ }
tickProject(dt); // the title and autosave, asleep or not
const double t = seconds();
if (const uint64_t key = frameKey(); key != lastKey) { // a job finished, the Blender bridge sent a model...
@@ -801,17 +847,45 @@ int App::run() {
drawUi();
ImGui::Render();
+ buildGhost(); // the dabs not on the GPU yet, drawn as a preview (predictive strokes)
const auto syncStart = std::chrono::steady_clock::now();
syncScene();
notePerformanceFrame(std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - syncStart).count());
const int samples = viewSamples();
renderSettings_.motionProxy = perf_.proxyNow; // big meshes draw their lighter proxy while the view moves
perf_.samplesUsed = renderer_.drawView(camera_, fbW, fbH, renderSettings_, canvasViewport(fbW, fbH), samples);
+ if (snapshotEnv && *snapshotEnv && (sm.moving || sm.releasing)) {
+ // Three points of the stroke, and the end (the frame the last dab reached the screen).
+ const double u = sm.moving ? (seconds() - sm.start) / double(options_.measureStroke) : 2.0;
+ const bool last = sm.releasing && !doc_.sculptor.strokeActive();
+ if ((sm.snapshots < 3 && u >= 0.3 * (sm.snapshots + 1)) || (last && sm.snapshots < 4)) {
+ sm.snapshots = last ? 4 : sm.snapshots + 1;
+ const Renderer::Viewport v = canvasViewport(fbW, fbH);
+ Image img;
+ img.width = v.w > 0 ? v.w : fbW;
+ img.height = v.h > 0 ? v.h : fbH;
+ img.rgba.resize(size_t(img.width) * size_t(img.height) * 4);
+ gl::ReadPixels(v.w > 0 ? v.x : 0, v.w > 0 ? v.y : 0, img.width, img.height, GL_RGBA, GL_UNSIGNED_BYTE, img.rgba.data());
+ for (int y = 0; y < img.height / 2; ++y) // (GL rows are bottom-up)
+ std::swap_ranges(img.rgba.begin() + std::ptrdiff_t(size_t(y) * img.width * 4),
+ img.rgba.begin() + std::ptrdiff_t(size_t(y + 1) * img.width * 4),
+ img.rgba.begin() + std::ptrdiff_t(size_t(img.height - 1 - y) * img.width * 4));
+ std::string error;
+ img.savePng(std::string(snapshotEnv) + "-" + std::to_string(sm.snapshots) + ".png", error);
+ }
+ }
renderSettings_.motionProxy = false; // (other renders, e.g. project thumbnails, draw in full)
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window_);
++framesDrawn;
- if (sm.started && !sm.moving) { // the press is on screen: start moving round the circle
+ {
+ const double frameNow = seconds();
+ static double lastSwap = 0.0;
+ if (lastSwap > 0.0) frameSeconds_ = frameSeconds_ * 0.9 + std::min(0.1, frameNow - lastSwap) * 0.1;
+ lastSwap = frameNow;
+ }
+ if (sm.started && sm.firstSurfaceMs == 0.0 && !sm.dabs.empty()) sm.firstSurfaceMs = (seconds() - sm.pressAt) * 1000.0;
+ if (sm.started && !sm.moving && !sm.releasing && sm.pressMs == 0.0) { // the press is on screen: start moving round the circle
sm.moving = true;
sm.pressMs = (seconds() - sm.start) * 1000.0;
sm.start = sm.lastFrame = seconds();
@@ -831,6 +905,11 @@ int App::run() {
sm.maxLag = std::max(sm.maxLag, px);
sm.sumLag += px;
++sm.lagSamples;
+ const glm::vec2 h = ghostHeadValid_ ? ghostHead_ : d; // the preview's head (or the surface's)
+ double ahead = pointer - std::atan2(double(h.y - sm.cy), double(h.x - sm.cx));
+ const double hpx = std::abs(std::remainder(ahead, 2.0 * 3.14159265358979)) * double(sm.r);
+ sm.maxPreviewLag = std::max(sm.maxPreviewLag, hpx);
+ sm.sumPreviewLag += hpx;
}
}
lastKey = frameKey(); // what this frame changed is on screen already
@@ -925,6 +1004,7 @@ BrushType App::activeBrush() const {
}
void App::onMouseDown(float x, float y, Uint8 button) {
+ if (strokeFinishing_) endStroke(); // the last stroke's dabs still being placed: finish them first
mouseX_ = x;
mouseY_ = y;
const SDL_Keymod mod = inputMods();
@@ -1105,7 +1185,12 @@ void App::onMouseUp() {
panFromAlt_ = false;
if (drag_ == Drag::Select) selectionMouseUp();
if (drag_ == Drag::Stroke && maskClickPending_) maskClickRelease(); // Ctrl+click: blur / sharpen the mask
- else if (drag_ == Drag::Stroke) endStroke();
+ else if (drag_ == Drag::Stroke && asyncStroke_) {
+ // Predictive strokes: letting go doesn't wait for the dabs still to place; the solver carries on and
+ // pumpStroke ends the stroke once they are on screen (anything that needs the document first ends it).
+ strokeFinishing_ = true;
+ if (!solverBusy() && dabQueue_.empty()) endStroke();
+ } else if (drag_ == Drag::Stroke) endStroke();
if (drag_ == Drag::MaskCanvas) maskCanvasRelease();
if (drag_ == Drag::SubToolClick) subtoolClickRelease();
if (drag_ == Drag::Guide) guideRelease();
@@ -1162,6 +1247,19 @@ void App::beginStroke(float x, float y, const RayHit& hit) {
strokePath_.reset(x, y);
dabQueue_.clear();
dabSeconds_ = 0.0; // measured again: the mesh and the brush may have changed
+ // Predictive strokes (AsyncStroke.cpp): the solver thread, the ghost's first estimate (the picked triangle).
+ asyncStroke_ = asyncStrokeAllowed();
+ pointerLatest_ = glm::vec2(x, y);
+ pointerLatestAt_ = double(SDL_GetTicksNS()) * 1e-9;
+ penPredictor_.reset(pointerLatest_, pointerLatestAt_);
+ {
+ const glm::vec3 n = glm::dot(hit.normal, camera_.forward()) > 0.0f ? -hit.normal : hit.normal;
+ ghostFrame_ = {hit.position, glm::length(n) > 0.0f ? glm::normalize(n) : -camera_.forward(), hit.position, true};
+ const glm::uvec3 t = hit.triangle < doc_.mesh.triangles.size() ? doc_.mesh.triangles[hit.triangle] : glm::uvec3(0);
+ const auto& P = doc_.mesh.positions;
+ ghostCurvature_ = 0.0f;
+ ghostEdge_ = (glm::distance(P[t.x], P[t.y]) + glm::distance(P[t.y], P[t.z]) + glm::distance(P[t.z], P[t.x])) / 3.0f;
+ }
if (strokeBrush_ == BrushType::Move && !recipe && strokeSculpt_) {
const float pressure = pressureSize_ ? std::max(strokePressure(), 0.05f) : 1.0f;
@@ -1193,6 +1291,11 @@ void App::beginStroke(float x, float y, const RayHit& hit) {
}
void App::strokeTo(float x, float y) {
+ if (asyncStroke_) {
+ pointerLatest_ = glm::vec2(x, y);
+ pointerLatestAt_ = double(SDL_GetTicksNS()) * 1e-9;
+ penPredictor_.add(pointerLatest_, pointerLatestAt_);
+ }
if (maskClickPending_) { // Ctrl+press on the model: a drag past the click slop paints the mask after all
if (std::hypot(x - maskClickX_, y - maskClickY_) < 4.0f) return;
maskClickPending_ = false;
@@ -1245,7 +1348,8 @@ void App::strokeTo(float x, float y) {
}
firstDabPending_ = false;
lastStrokePressure_ = strokePressure();
- dabAt(lastX_, lastY_, lastStrokePressure_);
+ if (asyncStroke_) dabQueue_.push({lastX_, lastY_, lastStrokePressure_}); // (the solver thread places it)
+ else dabAt(lastX_, lastY_, lastStrokePressure_);
}
// Place dabs at even screen-space spacing along the cursor path. Every input sample counts (pens
@@ -1306,7 +1410,13 @@ void App::drainStrokeDabs(double budgetSeconds) {
}
void App::dabAt(float x, float y, float pressure) {
- if (strokeProbe_) strokeProbe_->emplace_back(x, y);
+ // On the solver thread (predictive strokes) the dab sees the view as it was when its batch began.
+ if (dabView_) {
+ if (dabView_->probe) dabView_->probe->emplace_back(x, y);
+ } else if (strokeProbe_) {
+ strokeProbe_->emplace_back(x, y);
+ }
+ const Camera& cam = dabView_ ? dabView_->camera : camera_;
const BrushRecipe* recipe = strokeRecipe();
if (recipe && recipe->jitter > 0.0f) {
// Deterministic per-stroke scatter (xorshift).
@@ -1330,28 +1440,28 @@ void App::dabAt(float x, float y, float pressure) {
dabAngle_ = s.angle;
}
RayHit hit;
- if (!pick(x, y, hit)) return;
+ if (!(dabView_ ? pickWith(*dabView_, x, y, hit) : pick(x, y, hit))) return;
if (pressure < 0.0f) pressure = strokePressure();
DabParams p;
p.center = hit.position;
- p.viewDir = camera_.forward();
+ p.viewDir = cam.forward();
p.radius = radiusPx_ * (pressureSize_ ? std::max(pressure, 0.05f) : 1.0f) *
- camera_.worldPerPixel(hit.position, canvasHeight()) * dabRadiusScale_;
+ cam.worldPerPixel(hit.position, dabView_ ? dabView_->canvas.h : canvasHeight()) * dabRadiusScale_;
// Smooth keeps its own Z Intensity (ZBrush), whether picked or held with Shift.
p.strength = (strokeBrush_ == BrushType::Smooth ? smoothStrength_ : strength_) * dabStrengthScale_;
p.pressure = pressureStrength_ ? pressure : 1.0f;
p.invert = strokeInvert_;
p.frontFacesOnly = frontFacesOnly_;
p.focal = focalShift_;
- lastDabScreen_ = glm::vec2(x, y);
+ if (!dabView_) lastDabScreen_ = glm::vec2(x, y); // (the solver's: when its batch is committed)
if (strokeMask_) {
p.strength = maskStrength_;
doc_.sculptor.dabMask(doc_.mesh, doc_.bvh, p, symmetry_);
return;
}
p.alpha = &doc_.brushAlpha;
- p.alphaUp = screenUpVector(dabAngle_);
+ p.alphaUp = cam.orientation() * glm::vec3(-std::sin(dabAngle_), std::cos(dabAngle_), 0.0f); // screenUpVector
p.vdm = doc_.brushVdm.get();
p.strokeDelta = haveLastDab_ ? hit.position - lastDabHit_ : glm::vec3(0.0f);
lastDabHit_ = hit.position;
@@ -1366,28 +1476,10 @@ void App::dabAt(float x, float y, float pressure) {
}
void App::endStroke() {
+ if (asyncStroke_) solverWait(); // (the dabs in flight; then the solver thread is idle)
drainStrokeDabs(0.0); // the dabs still waiting belong to this stroke
- strokeMask_ = false;
- maskClickPending_ = false; // (a command can end a Ctrl+click before its release)
- firstDabPending_ = false;
- // Preferences > Performance shows what the stroke cost.
- if (doc_.sculptor.timings.dabs > 0) {
- perf_.lastStroke = doc_.sculptor.timings;
- perf_.lastStrokeSyncMs = perf_.strokeSyncMs;
- perf_.lastStrokeFrames = perf_.strokeFrames;
- }
- doc_.sculptor.timings = {};
- perf_.strokeSyncMs = 0.0;
- perf_.strokeFrames = 0;
- UndoRecord r = doc_.sculptor.endStroke();
- finishPaintStroke(r);
- if (r.topology && !r.topology->uvs.empty())
- status_ = "Dynamic topology removed the UVs of the remeshed SubTool (Undo brings them back)";
- if (!r.empty()) {
- doc_.undo.push(std::move(r));
- doc_.modified = true;
- updateTitle();
- }
+ // (a command can end a Ctrl+click before its release: finishStrokeNow clears the mask click too)
+ finishStrokeNow(); // AsyncStroke.cpp
}
// ---------------------------------------------------------------------------
@@ -1581,6 +1673,7 @@ Mesh& App::displayedMesh() {
}
void App::syncScene() {
+ if (solverBusy()) return; // the solver thread is changing the mesh: the GPU keeps what it has (AsyncStroke.cpp)
// The active SubTool's slot shows the LOD being previewed instead of the sculpt.
Mesh& shown = displayedMesh();
if (&shown != lastDisplayed_) shown.topologyDirty = true;
@@ -2601,7 +2694,8 @@ void App::drawBrushCursor() {
~PopClip() { dl->PopClipRect(); }
} popClip{dl};
const float r = radiusPx_ * (pressureSize_ && lastInputPen_ ? std::max(strokePressure(), 0.05f) : 1.0f);
- const ImVec2 c(mouseX_, mouseY_);
+ const glm::vec2 at = predictedPointer(); // during a predictive stroke, where the pen will be as this frame shows
+ const ImVec2 c(at.x, at.y);
const SDL_Keymod mod = inputMods();
const bool inverted = drag_ == Drag::Stroke ? strokeInvert_
: invertToggle_ != (((mod & SDL_KMOD_ALT) != 0) ||
diff --git a/src/app/App.h b/src/app/App.h
index fffafe32..c86123da 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -21,6 +21,7 @@
#include "core/Document.h"
#include "core/Face.h"
#include "core/MeshTransform.h"
+#include "core/PenPredictor.h"
#include "core/AutoUv.h"
#include "core/QRemesh.h"
#include "core/Stroke.h"
@@ -220,6 +221,53 @@ private:
double dabSeconds_ = 0.0; // what a dab of this stroke costs (running average), for the backlog limit
bool interactiveStrokes_ = false; // App::run(): budgeted dabs and curved long gaps; other loops dab at once
void drainStrokeDabs(double budgetSeconds); // places queued dabs; budget <= 0: all of them
+
+ // --- Strokes on heavy meshes (AsyncStroke.cpp) ---
+ // A sculpt stroke's dabs are placed on a solver thread while the main loop keeps drawing at the display's rate:
+ // each frame the finished batch of dabs is uploaded (packed on the solver thread, render/Renderer.h) and the
+ // next batch starts, and the dabs still waiting are drawn as a GPU preview (the ghost, render/Ghost.h) ahead of
+ // the solver, the stroke's head and the brush cursor where the pen will be a frame later (core/PenPredictor.h).
+ // The dabs, their order and so the geometry are those of the synchronous path. Preferences > Performance >
+ // Predictive strokes (SF_PREDICTIVE_STROKE=0 / 1 overrides it).
+ struct StrokeSolver;
+ struct DabView { // what a dab on the solver thread sees of the view (the main thread's may move on)
+ Camera camera;
+ CanvasRect canvas;
+ std::vector<glm::vec2>* probe = nullptr; // --measure-stroke: where its dabs went
+ };
+ std::unique_ptr<StrokeSolver> solver_;
+ const DabView* dabView_ = nullptr; // set while the solver thread places dabs (dabAt reads it)
+ bool asyncStroke_ = false; // this stroke's dabs go to the solver thread
+ bool strokeFinishing_ = false; // released: the solver places the last dabs, the UI carries on
+ bool predictiveStrokes() const; // the preference (and its environment override)
+ bool asyncStrokeAllowed() const; // the stroke beginning now may use the solver thread
+ bool solverBusy() const;
+ void solverWait(); // waits for the dabs in flight and commits them
+ void pumpStroke(); // per frame: commit what the solver finished, start its next batch
+ void commitSolverBatch();
+ void finishStrokeNow(); // the rest of endStroke: undo step, timings
+ void buildGhost(); // per frame: the dabs not yet on the GPU, for the renderer's preview
+ bool pickWith(const DabView& view, float x, float y, RayHit& hit) const;
+ // Where a screen ray meets the surface as the last dabs placed describe it (the ghost's estimate).
+ bool ghostSurface(const glm::vec3& origin, const glm::vec3& dir, glm::vec3& at, glm::vec3& normal) const;
+ PenPredictor penPredictor_;
+ glm::vec2 pointerLatest_{0.0f}; // the newest pointer sample of the stroke (window pixels)
+ double pointerLatestAt_ = 0.0; // its time (seconds)
+ double frameSeconds_ = 1.0 / 60.0; // running mean of the frame interval (how far ahead to predict)
+ Sculptor::DabFrame ghostFrame_; // the last placed dab's frame (the ghost's estimates start from it)
+ float ghostEdge_ = 0.0f; // mean edge length of the triangle the stroke began on (the preview's cavities)
+ float ghostCurvature_ = 0.0f; // the surface's curvature along the stroke (1 / radius, > 0 convex; 0 flat)
+ glm::vec2 ghostHead_{0.0f}; // where the preview's head is on screen (--measure-stroke)
+ bool ghostHeadValid_ = false;
+ uint64_t commits_ = 0; // solver batches committed
+ // --measure-stroke SF_MEASURE_POP=1: at each commit the view is drawn offscreen as it was (with the batch's
+ // preview), as it is (the batch's result), and as it was without the batch's preview; the mean differences.
+ bool popProbe_ = false;
+ double popWith_ = 0.0, popWithout_ = 0.0, popWorst_ = 0.0;
+ uint64_t popCommits_ = 0;
+ glm::vec2 predictedPointer() const; // where the pen will be when this frame shows (the cursor, the head)
+ static uint64_t meshStateHash(const Mesh& mesh); // --measure-stroke SF_MEASURE_VERIFY=1
+ bool verifyStroke(uint64_t before, const std::vector<glm::vec2>& dabs);
bool lastInputPen_ = false; // the last mouse event came from a pen (SDL_PEN_MOUSEID)
bool penSeen_ = false;
BrushType strokeBrush_ = BrushType::Standard;
diff --git a/src/app/AsyncStroke.cpp b/src/app/AsyncStroke.cpp
new file mode 100644
index 00000000..86524dda
--- /dev/null
+++ b/src/app/AsyncStroke.cpp
@@ -0,0 +1,397 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Strokes on heavy meshes (App.h): the solver thread, the per-frame hand-over, the ghost and the pen prediction.
+//
+// One frame of a stroke on the main thread: events queue dabs (strokeTo); pumpStroke commits the batch the
+// solver finished (its packed vertices go up with glBufferSubData, its dabs leave the ghost) and hands it the
+// dabs waiting; buildGhost gives the renderer every dab not on the GPU yet; the frame draws and swaps while the
+// solver works. The mesh, its BVH and the sculptor belong to the solver while it is busy: nothing on the main
+// thread reads or writes them then (syncScene and the pollers wait, and every path that edits the document
+// ends the stroke first, which waits for the solver).
+#include "app/AsyncStroke.h"
+
+#include "core/Sculpt.h"
+
+#include <algorithm>
+#include <chrono>
+#include <cmath>
+#include <cstdlib>
+
+namespace sf {
+
+namespace {
+
+constexpr double kBatchSeconds = 0.033; // a batch's dabs: about this much work (the surface updates this often)
+
+} // namespace
+
+App::StrokeSolver::StrokeSolver(App& a) : app(a) { thread_ = std::thread([this] { loop(); }); }
+
+App::StrokeSolver::~StrokeSolver() {
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ quit_ = true;
+ }
+ wake_.notify_all();
+ if (thread_.joinable()) thread_.join();
+}
+
+void App::StrokeSolver::start() {
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ pending_ = true;
+ done = false;
+ busy_.store(true, std::memory_order_release);
+ }
+ wake_.notify_one();
+}
+
+void App::StrokeSolver::wait() {
+ std::unique_lock<std::mutex> lock(mutex_);
+ idle_.wait(lock, [this] { return !busy_.load(std::memory_order_acquire); });
+}
+
+void App::StrokeSolver::loop() {
+ for (;;) {
+ {
+ std::unique_lock<std::mutex> lock(mutex_);
+ wake_.wait(lock, [this] { return pending_ || quit_; });
+ if (quit_) return;
+ pending_ = false;
+ }
+ probe.clear();
+ double spent = 0.0;
+ app.dabView_ = &view;
+ 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();
+ }
+ app.dabView_ = nullptr;
+ static const char* slow = std::getenv("SF_SOLVER_DELAY_MS"); // (a slower solver, for testing the preview)
+ if (slow && *slow) std::this_thread::sleep_for(std::chrono::milliseconds(std::atoi(slow)));
+ dabSeconds = batch.empty() ? 0.0 : spent / double(batch.size());
+ uploadReady = prepare && app.renderer_.prepareStrokeUpload(uploadId, app.doc_.mesh, upload);
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ done = true;
+ busy_.store(false, std::memory_order_release);
+ }
+ idle_.notify_all();
+ wakeMainLoop();
+ }
+}
+
+// ---------------------------------------------------------------------------
+
+bool App::predictiveStrokes() const {
+ static const int env = [] {
+ const char* v = std::getenv("SF_PREDICTIVE_STROKE");
+ return v && *v ? (*v == '0' ? 0 : 1) : -1;
+ }();
+ return env >= 0 ? env == 1 : uiPrefs_.predictiveStrokes;
+}
+
+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_ &&
+ !doc_.sculptor.dyntopo.enabled && previewLod_ < 0 && !faceTakePlaying_ &&
+ (recipe || (strokeBrush_ != BrushType::Move && strokeBrush_ != BrushType::SnakeHook)) &&
+ strokeType_ != StrokeType::DragRect && strokeType_ != StrokeType::DragDot;
+}
+
+bool App::solverBusy() const { return solver_ && solver_->busy(); }
+
+void App::solverWait() {
+ if (!solver_) return;
+ solver_->wait();
+ if (solver_->done) commitSolverBatch();
+}
+
+void App::commitSolverBatch() {
+ StrokeSolver& s = *solver_;
+ s.done = false;
+ ++commits_;
+ // SF_MEASURE_POP: the view before the commit, with and without the batch's preview.
+ std::vector<uint8_t> withPreview, withoutPreview, after;
+ const CanvasRect canvas = canvasRect();
+ const float scale = std::min(1.0f, 640.0f / std::max(canvas.w, canvas.h));
+ const int pw = std::max(1, int(canvas.w * scale)), ph = std::max(1, int(canvas.h * scale));
+ auto render = [&](std::vector<uint8_t>& out) {
+ std::string error;
+ Camera cam = camera_;
+ cam.aspect = float(pw) / float(ph);
+ renderer_.renderToImage(cam, pw, ph, renderSettings_, out, error);
+ };
+ const bool probe = popProbe_ && !s.batch.empty();
+ if (probe) {
+ buildGhost();
+ render(withPreview);
+ std::vector<PathDab> batch;
+ batch.swap(s.batch);
+ buildGhost();
+ render(withoutPreview);
+ batch.swap(s.batch);
+ }
+ if (s.uploadReady) {
+ renderer_.commitStrokeUpload(s.upload);
+ s.uploadReady = false;
+ } else {
+ syncScene(); // (the regular sync: a new grid, or a mesh the two-part upload doesn't handle)
+ }
+ if (strokeProbe_) strokeProbe_->insert(strokeProbe_->end(), s.probe.begin(), s.probe.end());
+ if (!s.batch.empty()) {
+ lastDabScreen_ = glm::vec2(s.batch.back().x, s.batch.back().y);
+ if (s.dabSeconds > 0.0) dabSeconds_ = dabSeconds_ > 0.0 ? dabSeconds_ * 0.8 + s.dabSeconds * 0.2 : s.dabSeconds;
+ }
+ static const bool debug = std::getenv("SF_GHOST_DEBUG") != nullptr;
+ if (debug && !s.batch.empty() && doc_.sculptor.lastFrame.valid) {
+ // The last dab's estimate (from the frame before it) against where the solver placed it.
+ const CanvasRect c = canvasRect();
+ glm::vec3 origin, dir, est, n;
+ camera_.screenRay(s.batch.back().x - c.x, s.batch.back().y - c.y, c.w, c.h, origin, dir);
+ if (!ghostSurface(origin, dir, est, n)) est = n = glm::vec3(0.0f);
+ const Sculptor::DabFrame& f = doc_.sculptor.lastFrame;
+ const float r = radiusPx_ * camera_.worldPerPixel(f.center, c.h);
+ SDL_Log("ghost: centre off by %.3f r, normal off %.1f deg, area depth est %.4f r real %.4f r", glm::distance(est, f.center) / r,
+ glm::degrees(std::acos(std::clamp(glm::dot(n, f.normal), -1.0f, 1.0f))),
+ glm::dot(ghostFrame_.center - ghostFrame_.areaCenter, ghostFrame_.normal) / r,
+ glm::dot(f.center - f.areaCenter, f.normal) / r);
+ }
+ if (doc_.sculptor.lastFrame.valid) {
+ // The surface's curvature along the stroke (signed, 1 / radius; > 0 convex), from how the area normal
+ // turned since the last frame, smoothed over the batches; small turns count as flat.
+ const Sculptor::DabFrame& f = doc_.sculptor.lastFrame;
+ const glm::vec3 d = f.center - ghostFrame_.center;
+ const float dist = glm::length(d);
+ const float angle = std::acos(std::clamp(glm::dot(glm::normalize(f.normal), glm::normalize(ghostFrame_.normal)), -1.0f, 1.0f));
+ if (dist > 1e-6f) {
+ float k = angle > glm::radians(0.5f) ? angle / dist : 0.0f;
+ if (glm::dot(d, f.normal - ghostFrame_.normal) < 0.0f) k = -k; // normals converging: concave
+ ghostCurvature_ = ghostCurvature_ == 0.0f ? k : ghostCurvature_ * 0.5f + k * 0.5f;
+ }
+ ghostFrame_ = f;
+ }
+ s.batch.clear();
+ if (probe) {
+ buildGhost();
+ render(after);
+ auto diff = [&](const std::vector<uint8_t>& a) {
+ uint64_t sum = 0;
+ for (size_t i = 1; i < a.size() && i < after.size(); i += 4) sum += uint64_t(std::abs(int(a[i]) - int(after[i])));
+ return double(sum) / double(std::max<size_t>(1, after.size() / 4));
+ };
+ const double with = diff(withPreview);
+ popWith_ += with;
+ popWithout_ += diff(withoutPreview);
+ popWorst_ = std::max(popWorst_, with);
+ ++popCommits_;
+ }
+}
+
+void App::pumpStroke() {
+ if (!asyncStroke_) return;
+ if (!solver_) solver_ = std::make_unique<StrokeSolver>(*this);
+ if (solver_->busy()) return;
+ if (solver_->done) commitSolverBatch();
+ if (dabQueue_.empty()) {
+ if (strokeFinishing_) finishStrokeNow(); // released, and every dab is on the GPU
+ return;
+ }
+ // The next batch: about kBatchSeconds of work (at least one dab), oldest first.
+ StrokeSolver& s = *solver_;
+ const size_t n = dabSeconds_ > 0.0 ? size_t(std::clamp(kBatchSeconds / dabSeconds_, 1.0, 64.0)) : 1;
+ s.batch.clear();
+ while (!dabQueue_.empty() && s.batch.size() < n) s.batch.push_back(dabQueue_.pop());
+ s.view.camera = camera_;
+ s.view.canvas = canvasRect();
+ s.view.probe = strokeProbe_ ? &s.probe : nullptr;
+ s.uploadId = doc_.activeSubtoolInfo().id;
+ s.prepare = &displayedMesh() == &doc_.mesh; // (a previewed LOD shows instead: nothing to pack)
+ s.start();
+}
+
+void App::finishStrokeNow() {
+ strokeFinishing_ = false;
+ asyncStroke_ = false;
+ renderer_.setGhost(0, GhostStroke());
+ ghostHeadValid_ = false;
+ strokeMask_ = false;
+ maskClickPending_ = false;
+ firstDabPending_ = false;
+ // Preferences > Performance shows what the stroke cost.
+ if (doc_.sculptor.timings.dabs > 0) {
+ perf_.lastStroke = doc_.sculptor.timings;
+ perf_.lastStrokeSyncMs = perf_.strokeSyncMs;
+ perf_.lastStrokeFrames = perf_.strokeFrames;
+ }
+ doc_.sculptor.timings = {};
+ perf_.strokeSyncMs = 0.0;
+ perf_.strokeFrames = 0;
+ UndoRecord r = doc_.sculptor.endStroke();
+ finishPaintStroke(r);
+ if (r.topology && !r.topology->uvs.empty())
+ status_ = "Dynamic topology removed the UVs of the remeshed SubTool (Undo brings them back)";
+ if (!r.empty()) {
+ doc_.undo.push(std::move(r));
+ doc_.modified = true;
+ updateTitle();
+ }
+}
+
+bool App::pickWith(const DabView& view, float x, float y, RayHit& hit) const {
+ if (!doc_.activeSubtoolInfo().visible) return false;
+ glm::vec3 origin, dir;
+ view.camera.screenRay(x - view.canvas.x, y - view.canvas.y, view.canvas.w, view.canvas.h, origin, dir);
+ return doc_.bvh.raycast(doc_.mesh, origin, dir, hit);
+}
+
+glm::vec2 App::predictedPointer() const {
+ if (!predictiveStrokes() || drag_ != Drag::Stroke || !asyncStroke_) return glm::vec2(mouseX_, mouseY_);
+ // To when this frame reaches the screen: about a frame after the newest sample.
+ const double ahead = std::clamp(frameSeconds_, 0.004, PenPredictor::kMaxAhead);
+ return penPredictor_.predict(pointerLatestAt_ + ahead, pointerLatest_);
+}
+
+bool App::ghostSurface(const glm::vec3& origin, const glm::vec3& dir, glm::vec3& at, glm::vec3& normal) const {
+ const glm::vec3 n0 = glm::normalize(ghostFrame_.normal);
+ const float denom = glm::dot(dir, n0);
+ if (std::abs(denom) < 1e-4f) return false;
+ const float tp = glm::dot(ghostFrame_.center - origin, n0) / denom; // the tangent plane
+ if (tp <= 0.0f) return false;
+ at = origin + dir * tp;
+ normal = n0;
+ if (ghostCurvature_ == 0.0f) return true;
+ // The sphere: centre one radius behind (convex) or in front of (concave) the last dab, along its normal.
+ const float R = 1.0f / std::abs(ghostCurvature_), side = ghostCurvature_ > 0.0f ? 1.0f : -1.0f;
+ const glm::vec3 C = ghostFrame_.center - n0 * (side * R);
+ const glm::vec3 oc = origin - C;
+ const float b = glm::dot(oc, dir), cc = glm::dot(oc, oc) - R * R, disc = b * b - cc;
+ if (disc < 0.0f) return true; // (the ray misses it: the plane)
+ const float sq = std::sqrt(disc);
+ const float t = std::abs(-b - sq - tp) < std::abs(-b + sq - tp) ? -b - sq : -b + sq; // the root nearer the plane's
+ if (t <= 0.0f) return true;
+ at = origin + dir * t;
+ normal = glm::normalize(at - C) * side;
+ return true;
+}
+
+void App::buildGhost() {
+ ghostHeadValid_ = false;
+ const uint32_t id = doc_.activeSubtoolInfo().id;
+ static const bool noGhost = [] { // SF_GHOST=0: the solver thread without the preview (for comparison)
+ const char* v = std::getenv("SF_GHOST");
+ return v && *v == '0';
+ }();
+ if (!asyncStroke_ || !solver_ || !predictiveStrokes() || noGhost) {
+ renderer_.setGhost(id, GhostStroke());
+ return;
+ }
+ GhostStroke g;
+ g.kind = strokeRecipe() ? GhostKind::None : ghostKindForBrush(int(strokeBrush_));
+ g.invert = strokeInvert_;
+ g.focal = focalShift_;
+ g.edge = ghostEdge_;
+ // Where the dabs not on the GPU yet are on screen: those in flight, those waiting, and the head.
+ struct Screen {
+ float x, y, pressure, weight;
+ };
+ std::vector<Screen> pending;
+ for (const PathDab& d : solver_->batch) pending.push_back({d.x, d.y, d.pressure, 1.0f});
+ for (const PathDab& d : dabQueue_.items()) pending.push_back({d.x, d.y, d.pressure, 1.0f});
+ // The head: from the last dab to where the pen will be when this frame shows (not for strokes that move the
+ // pointer's path: Lazy Mouse, a Shift line, jitter or spray), in part, as the dabs the pen is about to lay.
+ const float spacing = std::max(1.0f, radiusPx_ * (strokeRecipe() ? strokeRecipe()->spacing : 0.15f));
+ if (drag_ == Drag::Stroke && strokeType_ != StrokeType::LazyMouse && strokeType_ != StrokeType::Spray && !straight_.active &&
+ !(strokeRecipe() && strokeRecipe()->jitter > 0.0f)) {
+ const glm::vec2 head = predictedPointer();
+ const glm::vec2 from = !pending.empty() ? glm::vec2(pending.back().x, pending.back().y) : lastDabScreen_;
+ const float gap = glm::distance(head, from);
+ const bool moving = glm::length(penPredictor_.velocity()) > 20.0f; // px / s
+ if (moving && gap > 0.2f * spacing)
+ pending.push_back({head.x, head.y, lastStrokePressure_, std::min(1.0f, gap / spacing)});
+ }
+ if (!pending.empty()) {
+ ghostHead_ = glm::vec2(pending.back().x, pending.back().y);
+ ghostHeadValid_ = true;
+ }
+ if (g.kind == GhostKind::None || pending.empty() || !ghostFrame_.valid) {
+ renderer_.setGhost(id, GhostStroke());
+ return;
+ }
+ // Each dab's surface point and normal: where its ray meets the surface as the last dabs placed describe it, a
+ // sphere through the last one with the curvature the last two show (their normals turn by an angle over the
+ // distance between them), or its tangent plane where they don't turn. The solver's result replaces the
+ // estimate when it arrives.
+ const CanvasRect c = canvasRect();
+ const glm::vec3 n0 = glm::normalize(ghostFrame_.normal);
+ const float depth = glm::dot(ghostFrame_.center - ghostFrame_.areaCenter, n0); // area centre below the surface
+ const std::vector<glm::vec3> mirrors = Sculptor::mirrors(symmetry_);
+ const size_t perMirror = GhostStroke::kMaxDabs / std::max<size_t>(1, mirrors.size());
+ std::vector<GhostDab> base;
+ for (const Screen& sd : pending) {
+ if (base.size() >= perMirror) break;
+ glm::vec3 origin, dir, n;
+ camera_.screenRay(sd.x - c.x, sd.y - c.y, c.w, c.h, origin, dir);
+ GhostDab d;
+ if (!ghostSurface(origin, dir, d.center, n)) continue;
+ d.radius = radiusPx_ * (pressureSize_ ? std::max(sd.pressure, 0.05f) : 1.0f) * camera_.worldPerPixel(d.center, c.h);
+ d.normal = n;
+ d.strength = std::clamp((strokeBrush_ == BrushType::Smooth ? smoothStrength_ : strength_), 0.0f, 1.0f) *
+ std::clamp(pressureStrength_ ? sd.pressure : 1.0f, 0.0f, 1.0f);
+ d.areaCenter = d.center - n * depth;
+ d.weight = sd.weight;
+ base.push_back(d);
+ }
+ for (uint32_t m = 0; m < mirrors.size(); ++m)
+ for (const GhostDab& b : base) {
+ GhostDab d = b;
+ d.center *= mirrors[m];
+ d.normal *= mirrors[m];
+ d.areaCenter *= mirrors[m];
+ d.mirror = m;
+ g.dabs.push_back(d);
+ }
+ renderer_.setGhost(id, g);
+}
+
+// --measure-stroke with SF_MEASURE_VERIFY=1: the stroke just drawn must come out of the frame loop's own path the
+// same, bit for bit, and its undo must give back the mesh it started from. Undoes it, checks the mesh, places the
+// same dabs (the probe's screen points, the mouse's pressure) with the solver thread off, and compares.
+uint64_t App::meshStateHash(const Mesh& mesh) {
+ uint64_t h = 1469598103934665603ull;
+ auto add = [&](const void* data, size_t bytes) {
+ const auto* p = static_cast<const uint8_t*>(data);
+ for (size_t i = 0; i < bytes; ++i) h = (h ^ p[i]) * 1099511628211ull;
+ };
+ add(mesh.positions.data(), mesh.positions.size() * sizeof(glm::vec3));
+ add(mesh.normals.data(), mesh.normals.size() * sizeof(glm::vec3));
+ return h;
+}
+
+bool App::verifyStroke(uint64_t before, const std::vector<glm::vec2>& dabs) {
+ const uint64_t after = meshStateHash(doc_.mesh);
+ runCommand("undo", {});
+ const uint64_t undone = meshStateHash(doc_.mesh);
+ const bool undoOk = undone == before;
+ // The same dabs, placed here (as with predictive strokes off).
+ const bool wasAsync = asyncStroke_;
+ asyncStroke_ = false;
+ haveLastDab_ = false;
+ doc_.syncBvh();
+ doc_.sculptor.beginStroke(doc_.mesh, brushName(strokeBrush_), doc_.activeLayer);
+ for (const glm::vec2& d : dabs) dabAt(d.x, d.y, 1.0f);
+ UndoRecord r = doc_.sculptor.endStroke();
+ const uint64_t replay = meshStateHash(doc_.mesh);
+ asyncStroke_ = wasAsync;
+ const bool same = replay == after;
+ std::printf("verify: undo %s; %zu dabs replayed without the solver thread: %s\n", undoOk ? "restores the mesh" : "DIFFERS",
+ dabs.size(), same ? "identical" : "DIFFERENT");
+ std::fflush(stdout);
+ return undoOk && same;
+}
+
+} // namespace sf
diff --git a/src/app/AsyncStroke.h b/src/app/AsyncStroke.h
new file mode 100644
index 00000000..bd83052f
--- /dev/null
+++ b/src/app/AsyncStroke.h
@@ -0,0 +1,49 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// The solver thread of a sculpt stroke (App::StrokeSolver, see App.h "Strokes on heavy meshes").
+#pragma once
+
+#include "app/App.h"
+
+#include <atomic>
+#include <condition_variable>
+#include <mutex>
+#include <thread>
+#include <vector>
+
+namespace sf {
+
+struct App::StrokeSolver {
+ explicit StrokeSolver(App& app);
+ ~StrokeSolver();
+ StrokeSolver(const StrokeSolver&) = delete;
+ StrokeSolver& operator=(const StrokeSolver&) = delete;
+
+ // Main thread: hands over `batch` (and view); the thread places the dabs, then packs the upload.
+ void start();
+ bool busy() const { return busy_.load(std::memory_order_acquire); }
+ void wait(); // until the batch in flight is done
+
+ App& app;
+ // While busy, owned by the thread (the main thread only reads batch, to draw the preview).
+ std::vector<PathDab> batch;
+ DabView view;
+ std::vector<glm::vec2> probe; // --measure-stroke: where the batch's dabs went
+ double dabSeconds = 0.0; // the batch's mean cost per dab
+ uint32_t uploadId = 0; // the active SubTool's renderer slot
+ bool prepare = true; // pack the upload on the thread
+ Renderer::StrokeUpload upload;
+ bool uploadReady = false; // packed (else the main thread's syncScene sends the change)
+ bool done = false; // a finished batch waits for its commit
+
+private:
+ void loop();
+ std::thread thread_;
+ std::mutex mutex_;
+ std::condition_variable wake_, idle_;
+ bool pending_ = false, quit_ = false;
+ std::atomic<bool> busy_{false};
+};
+
+} // namespace sf
diff --git a/src/app/Config.cpp b/src/app/Config.cpp
index ac6e7afe..8fc24b1e 100644
--- a/src/app/Config.cpp
+++ b/src/app/Config.cpp
@@ -56,6 +56,7 @@ Json toJson(const UiPrefs& p) {
j.set("antialiasing", double(p.antialiasing));
j.set("fastNavigation", p.fastNavigation);
j.set("multithreading", p.multithreading);
+ j.set("predictiveStrokes", p.predictiveStrokes);
j.set("dyntopoConstantDetail", p.dyntopoConstantDetail);
j.set("flatShading", p.flatShading);
j.set("background", std::string(p.gradientBackground ? "gradient" : "flat"));
@@ -91,6 +92,7 @@ void fromJson(const Json& j, UiPrefs& p) {
p.antialiasing = aa >= 8 ? 8 : aa >= 4 ? 4 : aa >= 2 ? 2 : 1;
p.fastNavigation = j["fastNavigation"].asBool(p.fastNavigation);
p.multithreading = j["multithreading"].asBool(p.multithreading);
+ p.predictiveStrokes = j["predictiveStrokes"].asBool(p.predictiveStrokes);
p.dyntopoConstantDetail = j["dyntopoConstantDetail"].asBool(p.dyntopoConstantDetail);
p.flatShading = j["flatShading"].asBool(p.flatShading);
if (j["background"].isString()) p.gradientBackground = j["background"].asString() != "flat";
diff --git a/src/app/Config.h b/src/app/Config.h
index 2f6dc245..faf2f3d1 100644
--- a/src/app/Config.h
+++ b/src/app/Config.h
@@ -40,6 +40,9 @@ struct UiPrefs {
int antialiasing = 4; // samples per pixel in the 3D view: 1 (off), 2, 4 or 8
bool fastNavigation = true; // big scenes draw without antialiasing while the view moves
bool multithreading = true; // brushes, normals and BVH updates on every core (off: one thread)
+ // Sculpt strokes solved on their own thread with a GPU preview of the dabs still to come, the cursor and the
+ // stroke's head predicted a frame ahead (app/AsyncStroke.cpp). Off: the dabs are placed in the frame loop.
+ bool predictiveStrokes = 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.cpp
index 12609854..6ed6ff9e 100644
--- a/src/app/PanelPerformance.cpp
+++ b/src/app/PanelPerformance.cpp
@@ -84,6 +84,16 @@ void App::registerPerformanceUiActions() {
[] { return !ThreadPool::shared().serial(); },
"Run brushes, normal and BVH updates and project saving on every core. Turn it off only to compare speeds "
"or rule it out when troubleshooting: the results are the same either way.");
+ add("perf.predictive_strokes", "Predictive Strokes",
+ [this] {
+ uiPrefs_.predictiveStrokes = !uiPrefs_.predictiveStrokes;
+ status_ = uiPrefs_.predictiveStrokes ? "Predictive strokes on" : "Predictive strokes off";
+ },
+ [this] { return uiPrefs_.predictiveStrokes; },
+ "Sculpt strokes work out the brush on their own thread while the view keeps drawing at full speed, and show "
+ "the dabs still being worked out as a preview on the surface, with the cursor and the stroke's head drawn "
+ "where the pen is about to be. On a heavy mesh the surface moves under the pen at once and lifting the pen "
+ "is instant. The finished sculpt is exactly the same either way.");
}
void App::applyPerformancePrefs() { ThreadPool::shared().setSerial(!uiPrefs_.multithreading); }
@@ -135,6 +145,10 @@ void App::drawPerformanceSection() {
dimText("The GPU had no room for " + std::to_string(uiPrefs_.antialiasing) + "x: using " +
(perf_.samplesUsed == 1 ? std::string("none") : std::to_string(perf_.samplesUsed) + "x") + ".");
+ bool predictive = uiPrefs_.predictiveStrokes;
+ if (ImGui::Checkbox("Predictive Strokes", &predictive)) actions_.find("perf.predictive_strokes")->run();
+ ui::HelpAction(*this, "perf.predictive_strokes");
+
// --- Threads ---
bool threads = !ThreadPool::shared().serial();
if (ImGui::Checkbox("Multithreading##perf.mt", &threads)) actions_.find("perf.multithreading")->run();
diff --git a/src/app/Tablet.cpp b/src/app/Tablet.cpp
index 63e8637d..72febeb5 100644
--- a/src/app/Tablet.cpp
+++ b/src/app/Tablet.cpp
@@ -182,7 +182,8 @@ void App::firstDab(float x, float y) {
}
firstDabPending_ = false;
lastStrokePressure_ = strokePressure();
- dabAt(x, y, lastStrokePressure_);
+ if (asyncStroke_) dabQueue_.push({x, y, lastStrokePressure_}); // the solver thread places it (AsyncStroke.cpp)
+ else dabAt(x, y, lastStrokePressure_);
}
// SDL has no pen names, so on Linux read the kernel's input device list; elsewhere the pen stays unnamed.
diff --git a/src/core/Commands.cpp b/src/core/Commands.cpp
index 5af26762..7d3c5264 100644
--- a/src/core/Commands.cpp
+++ b/src/core/Commands.cpp
@@ -107,8 +107,21 @@ CommandRegistry::CommandRegistry() {
add({"new_sphere",
"Replace the scene with a new sphere ready for sculpting.",
- {{"subdivisions", T::Number, "Icosphere subdivision level (0-8). Default 6.", false}},
+ {{"subdivisions", T::Number, "Icosphere subdivision level (0-8). Default 6.", false},
+ {"triangles", T::Number,
+ "About this many triangles instead (a cube's quads pushed onto the sphere, up to 48M; benchmarks: "
+ "tools/bench_strokes.sh).",
+ false}},
[](Document& doc, const Args& a) -> CommandResult {
+ if (const double t = argNumber(a, "triangles", 0.0); t > 0.0) {
+ const int res = std::clamp(int(std::lround(std::sqrt(t / 12.0))), 1, 2000);
+ Mesh m = makeCube(res, 2.0f);
+ for (glm::vec3& p : m.positions) p = glm::normalize(p);
+ m.recomputeAllNormals();
+ doc.setMesh(std::move(m), "Sphere");
+ doc.path.clear();
+ return {true, "Created sphere with " + std::to_string(doc.mesh.triangleCount()) + " triangles"};
+ }
int level = std::clamp(int(argNumber(a, "subdivisions", 6)), 0, 8);
doc.setMesh(makeIcosphere(level), "Sphere");
doc.path.clear();
diff --git a/src/core/PenPredictor.h b/src/core/PenPredictor.h
new file mode 100644
index 00000000..f36bbf6b
--- /dev/null
+++ b/src/core/PenPredictor.h
@@ -0,0 +1,89 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Where the pointer will be a frame from now, for the predictive stroke preview (render/Ghost.h): the brush
+// cursor and the head of the stroke are drawn there, so they sit under the pen when the frame reaches the screen
+// instead of a frame behind it. Each new sample corrects the prediction (nothing predicted is ever sculpted).
+//
+// An alpha-beta-gamma filter over the samples (position, velocity, acceleration; the gains of a critically
+// damped tracker), extrapolated with the acceleration damped, at most kMaxAhead seconds ahead. A pointer that
+// stops, or one that hasn't sent a sample for a while, predicts where it is.
+#pragma once
+
+#include <glm/glm.hpp>
+
+#include <algorithm>
+
+namespace sf {
+
+class PenPredictor {
+public:
+ static constexpr double kMaxAhead = 0.025; // seconds
+ static constexpr double kStale = 0.05; // no sample for this long: the pointer has stopped
+
+ void reset(glm::vec2 p, double t) {
+ pos_ = p;
+ vel_ = acc_ = glm::vec2(0.0f);
+ last_ = t;
+ samples_ = 1;
+ }
+
+ void add(glm::vec2 p, double t) {
+ if (samples_ == 0) {
+ reset(p, t);
+ return;
+ }
+ const double dt = t - last_;
+ if (dt <= 1e-4) { // the same instant (several events in one batch): take the newest position
+ pos_ = p;
+ return;
+ }
+ if (dt > kStale) { // after a pause the old velocity says nothing
+ vel_ = (p - pos_) / float(dt);
+ acc_ = glm::vec2(0.0f);
+ pos_ = p;
+ last_ = t;
+ ++samples_;
+ return;
+ }
+ const float h = float(dt);
+ // Predict to this sample's time, then correct by the residual.
+ const glm::vec2 xp = pos_ + vel_ * h + 0.5f * acc_ * h * h;
+ const glm::vec2 vp = vel_ + acc_ * h;
+ const glm::vec2 r = p - xp;
+ constexpr float a = 0.7f, b = 0.35f, g = 0.06f; // stable, responsive gains
+ pos_ = xp + a * r;
+ vel_ = vp + (b / h) * r;
+ acc_ = acc_ + (2.0f * g / (h * h)) * r;
+ // The first samples: velocity from the difference alone.
+ if (samples_ == 1) {
+ vel_ = r / h;
+ acc_ = glm::vec2(0.0f);
+ pos_ = p;
+ }
+ last_ = t;
+ ++samples_;
+ }
+
+ // The pointer at time t (normally a frame after the last sample); `latest` is the newest sample, returned when
+ // the filter has too little to go on.
+ glm::vec2 predict(double t, glm::vec2 latest) const {
+ if (samples_ < 3) return latest;
+ const double since = t - last_;
+ if (since > kStale || since < -kStale) return latest;
+ const float h = float(std::clamp(since, 0.0, kMaxAhead));
+ // From the newest sample (not the filtered position, which trails it a little), with the motion the
+ // filter sees; the acceleration counts half, since it is the noisiest part.
+ return latest + vel_ * h + 0.25f * acc_ * h * h;
+ }
+
+ glm::vec2 velocity() const { return vel_; }
+ int samples() const { return samples_; }
+
+private:
+ glm::vec2 pos_{0.0f}, vel_{0.0f}, acc_{0.0f};
+ double last_ = 0.0;
+ int samples_ = 0;
+};
+
+} // namespace sf
diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cpp
index 389b0545..bede67b2 100644
--- a/src/core/Sculpt.cpp
+++ b/src/core/Sculpt.cpp
@@ -461,6 +461,7 @@ void Sculptor::dab(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p, Sym
dabVerts_.clear();
dabLeaves_.clear();
for (const glm::vec3& m : mirrors(sym)) {
+ frameWanted_ = m == glm::vec3(1.0f); // (the unmirrored pass records lastFrame)
DabParams mp = p;
mp.center *= m;
mp.viewDir *= m;
@@ -503,6 +504,10 @@ void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams&
areaCenter /= wsum;
float nlen = glm::length(areaNormal);
areaNormal = nlen > 1e-12f ? areaNormal / nlen : -p.viewDir;
+ if (frameWanted_) {
+ lastFrame = {p.center, areaNormal, areaCenter, true};
+ frameWanted_ = false;
+ }
const float s = std::clamp(p.strength, 0.0f, 1.0f) * std::clamp(p.pressure, 0.0f, 1.0f);
const float sign = p.invert ? -1.0f : 1.0f;
@@ -684,6 +689,7 @@ void Sculptor::dabRecipe(Mesh& mesh, BVH& bvh, const BrushRecipe& recipe, const
dabVerts_.clear();
dabLeaves_.clear();
for (const glm::vec3& m : mirrors(sym)) {
+ frameWanted_ = m == glm::vec3(1.0f); // (the unmirrored pass records lastFrame)
DabParams mp = base;
mp.center *= m;
mp.viewDir *= m;
@@ -759,6 +765,10 @@ void Sculptor::dabRecipeSingle(Mesh& mesh, BVH& bvh, const BrushRecipe& r, const
areaCenter /= wsum;
float nlen = glm::length(areaNormal);
areaNormal = nlen > 1e-12f ? areaNormal / nlen : -p.viewDir;
+ if (frameWanted_) {
+ lastFrame = {p.center, areaNormal, areaCenter, true};
+ frameWanted_ = false;
+ }
glm::vec3 strokeDir = p.strokeDelta;
float strokeLen = glm::length(strokeDir);
diff --git a/src/core/Sculpt.h b/src/core/Sculpt.h
index 674e5c8a..86b22035 100644
--- a/src/core/Sculpt.h
+++ b/src/core/Sculpt.h
@@ -109,6 +109,13 @@ public:
bool strokeActive() const { return active_; }
SculptTimings timings;
+ // The brush's frame on the surface at the last dab's first (unmirrored) pass: the dab centre, the area normal
+ // and the area centre its kernel used (the predictive stroke preview estimates the next dabs from it).
+ struct DabFrame {
+ glm::vec3 center{0.0f}, normal{0.0f, 0.0f, 1.0f}, areaCenter{0.0f};
+ bool valid = false;
+ };
+ DabFrame lastFrame;
// Dynamic topology (core/Dyntopo.h): while enabled, dab() and dabRecipe() remesh the surface under the
// brush first (Smooth towards twice the edge length, so it simplifies, as in ZBrush). Not the Move brush.
@@ -152,6 +159,7 @@ private:
std::vector<glm::vec3> lastMirrors_;
bool active_ = false;
+ bool frameWanted_ = false; // the next dabSingle records lastFrame
int layer_ = -1;
UndoRecord record_;
// A bit per vertex: recorded in record_'s positions / mask / colours (cleared from those lists).
diff --git a/src/core/StrokePath.h b/src/core/StrokePath.h
index 9a61e71d..e79d767c 100644
--- a/src/core/StrokePath.h
+++ b/src/core/StrokePath.h
@@ -159,6 +159,7 @@ public:
bool empty() const { return q_.empty(); }
size_t size() const { return q_.size(); }
void clear() { q_.clear(); }
+ const std::deque<PathDab>& items() const { return q_; } // oldest first (the predictive preview draws them)
PathDab pop() {
PathDab d = q_.front();
q_.pop_front();
diff --git a/src/render/GL.h b/src/render/GL.h
index d10b24aa..7d523c6a 100644
--- a/src/render/GL.h
+++ b/src/render/GL.h
@@ -176,6 +176,8 @@ constexpr GLbitfield GL_COLOR_BUFFER_BIT = 0x4000;
X(void, Uniform1i, GLint, GLint) \
X(void, Uniform1f, GLint, GLfloat) \
X(void, Uniform2f, GLint, GLfloat, GLfloat) \
+ X(void, Uniform2i, GLint, GLint, GLint) \
+ X(void, Uniform2iv, GLint, GLsizei, const GLint*) \
X(void, DisableVertexAttribArray, GLuint) \
X(void, VertexAttrib1f, GLuint, GLfloat) \
X(void, VertexAttrib3f, GLuint, GLfloat, GLfloat, GLfloat) \
diff --git a/src/render/Ghost.cpp b/src/render/Ghost.cpp
new file mode 100644
index 00000000..d7d945bc
--- /dev/null
+++ b/src/render/Ghost.cpp
@@ -0,0 +1,86 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "render/Ghost.h"
+
+#include <algorithm>
+#include <cmath>
+
+namespace sf {
+
+GhostKind ghostKindForBrush(int brushType) {
+ // core/Sculpt.h BrushType order: Standard, Clay, Smooth, Move, Inflate, Crease, Flatten, Pinch, DamStandard,
+ // ClayBuildup, SnakeHook, Trim, Layer, Nudge, Vdm.
+ switch (brushType) {
+ case 0: return GhostKind::Standard;
+ case 1: return GhostKind::Clay;
+ case 4: return GhostKind::Inflate;
+ case 5: return GhostKind::Crease;
+ case 6: return GhostKind::Flatten;
+ case 7: return GhostKind::Pinch;
+ case 8: return GhostKind::DamStandard;
+ case 9: return GhostKind::ClayBuildup;
+ case 11: return GhostKind::Trim;
+ default: return GhostKind::None;
+ }
+}
+
+float ghostFalloff(float dist, float radius, float focal) {
+ float t = std::clamp(1.0f - dist / radius, 0.0f, 1.0f);
+ if (focal != 0.0f) t = std::pow(t, std::exp2(2.0f * std::clamp(focal, -1.0f, 1.0f)));
+ return t * t * (3.0f - 2.0f * t);
+}
+
+namespace {
+glm::vec3 onPlane(const glm::vec3& v, const glm::vec3& n) { return v - n * glm::dot(v, n); }
+} // namespace
+
+// Keep in step with kMeshVS's ghostMove (Renderer.cpp) and the kernels in Sculptor::dabSingle.
+glm::vec3 ghostMove(const GhostStroke& g, glm::vec3 x, const glm::vec3& n) {
+ const float sign = g.invert ? -1.0f : 1.0f;
+ for (const GhostDab& d : g.dabs) {
+ const glm::vec3 o = x - d.center;
+ const float dist = glm::length(o);
+ if (dist >= d.radius || d.radius <= 0.0f) continue;
+ const float f = ghostFalloff(dist, d.radius, g.focal) * d.weight;
+ const float s = d.strength, r = d.radius;
+ const glm::vec3& an = d.normal;
+ switch (g.kind) {
+ case GhostKind::Standard: x += an * (sign * s * r * 0.06f * f); break;
+ case GhostKind::Inflate: x += n * (sign * s * r * 0.05f * f); break;
+ case GhostKind::Clay: {
+ const float h = glm::dot(x - (d.areaCenter + an * (sign * r * 0.05f * s)), an);
+ if (sign * h < 0.0f) x -= an * (h * f);
+ break;
+ }
+ case GhostKind::ClayBuildup: {
+ // (round: the square tip's corners need the brush's screen frame)
+ const float e = std::clamp((1.0f - dist / r) / 0.35f, 0.0f, 1.0f);
+ const float w = e * e * (3.0f - 2.0f * e) * d.weight;
+ const float h = glm::dot(x - (d.areaCenter + an * (sign * r * 0.08f * s)), an);
+ if (sign * h < 0.0f) x -= an * (h * w);
+ break;
+ }
+ case GhostKind::Flatten: {
+ const float h = glm::dot(x - d.areaCenter, an);
+ x -= an * (h * s * f * (g.invert ? -0.5f : 1.0f));
+ break;
+ }
+ case GhostKind::Crease:
+ x += an * (-sign * s * r * 0.05f * f * f) + onPlane(d.center - x, an) * (0.25f * s * f);
+ break;
+ case GhostKind::Pinch: x += onPlane(d.center - x, an) * (sign * 0.2f * s * f); break;
+ case GhostKind::DamStandard:
+ x += an * (-sign * s * r * 0.08f * f * f * f * f) + onPlane(d.center - x, an) * (0.3f * s * f);
+ break;
+ case GhostKind::Trim: {
+ const float h = glm::dot(x - (d.areaCenter - an * (sign * r * 0.02f * s)), an);
+ if (sign * h > 0.0f) x -= an * (h * std::min(1.0f, std::sqrt(f) * (0.5f + s)));
+ break;
+ }
+ case GhostKind::None: break;
+ }
+ }
+ return x;
+}
+
+} // namespace sf
diff --git a/src/render/Ghost.h b/src/render/Ghost.h
new file mode 100644
index 00000000..c48a37d8
--- /dev/null
+++ b/src/render/Ghost.h
@@ -0,0 +1,62 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Predictive stroke preview ("ghost"): the dabs of a stroke that the brush solver hasn't placed yet, drawn as a
+// displacement of the geometry the GPU already holds, so the surface moves under the pointer the frame the
+// pointer moves while the solver catches up on another thread (app/AsyncStroke.cpp).
+//
+// The mesh vertex shader (Renderer.cpp, GHOST) moves each vertex within a dab's radius as that brush's kernel
+// would (core/Sculpt.cpp), dab after dab, from the uploaded position and normal, and bends the shading normal
+// by the same moves evaluated at two nearby points of the tangent plane. What it can't know it estimates: each
+// dab's surface point is where its screen ray meets the plane of the last placed dab, and the brush's area
+// normal and centre are the last placed dab's. When the solver's result for a dab is uploaded, that dab leaves
+// the ghost in the same frame, so the real surface replaces the preview without a jump wherever the estimate
+// holds (exactly on a plane with Standard, test ghost_matches_standard_dab).
+//
+// ghostMove() is the shader's function on the CPU, for the tests. The preview never changes the mesh: the
+// final geometry is the solver's, as without it.
+#pragma once
+
+#include <glm/glm.hpp>
+
+#include <cstdint>
+#include <vector>
+
+namespace sf {
+
+// The brush kernels the preview can draw (the rest show no ghost: Smooth, Layer, Nudge, Move, Snake Hook,
+// VDM, recipes, masking, painting).
+enum class GhostKind : int32_t { None = 0, Standard, Inflate, Clay, ClayBuildup, Flatten, Crease, Pinch, DamStandard, Trim };
+
+struct GhostDab {
+ glm::vec3 center{0.0f}; // surface point (estimated)
+ float radius = 0.0f; // world units
+ glm::vec3 normal{0.0f, 0.0f, 1.0f}; // the brush's area normal (estimated), mirrored with the dab
+ float strength = 0.0f; // strength x pressure, 0..1
+ glm::vec3 areaCenter{0.0f}; // the brush's area centre (plane brushes), estimated
+ float weight = 1.0f; // < 1: a provisional dab at the predicted head, shown in part
+ uint32_t mirror = 0; // symmetry pass (0: the dab itself); the shader bounds each pass's dabs together
+};
+
+struct GhostStroke {
+ GhostKind kind = GhostKind::None;
+ bool invert = false;
+ float focal = 0.0f;
+ float edge = 0.0f; // the mesh's edge length under the brush: the preview's cavity shading (curvatureAt) scales with it
+ // Grouped by mirror, each group in the order the solver will place its dabs (the solver does every mirror of
+ // a dab before the next dab; the order only matters where mirrored dabs overlap, on the symmetry plane).
+ std::vector<GhostDab> dabs;
+ static constexpr size_t kMaxDabs = 64, kMaxMirrors = 8; // the shader's uniform arrays
+ bool empty() const { return kind == GhostKind::None || dabs.empty(); }
+};
+
+// The ghost's kernel for a brush type (core/Sculpt.h BrushType as int, so this header needs no core).
+GhostKind ghostKindForBrush(int brushType);
+
+// brushFalloff (core/Sculpt.cpp), as the shader computes it.
+float ghostFalloff(float dist, float radius, float focal);
+
+// Where the preview draws a vertex at x with normal n (the shader's ghostMove).
+glm::vec3 ghostMove(const GhostStroke& g, glm::vec3 x, const glm::vec3& n);
+
+} // namespace sf
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index a19b422e..b67be0a8 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -54,11 +54,94 @@ vec3 decodeNormal(uint a) {
n.y += n.y >= 0.0 ? -t : t;
return normalize(n);
}
+#ifdef GHOST
+// The predictive stroke preview (render/Ghost.h): the dabs the solver hasn't placed yet move the vertices as the
+// brush would. Keep in step with sf::ghostMove (Ghost.cpp).
+uniform vec4 uGhostA[64]; // centre, radius
+uniform vec4 uGhostB[64]; // area normal, strength x pressure
+uniform vec4 uGhostC[64]; // area centre, weight
+uniform int uGhostGroups; // runs of dabs (one per mirror), each with a sphere holding its dabs
+uniform vec4 uGhostBound[8];
+uniform ivec2 uGhostRange[8]; // [first, end) of each run
+uniform ivec2 uGhostKind; // kernel (sf::GhostKind), inverted
+uniform float uGhostFocal;
+uniform float uGhostEps; // the tangent step for the bent normal and the curvature
+uniform float uGhostEdge; // the mesh's edge length under the brush (the cavity term's scale)
+float ghostFalloff(float d, float r) {
+ float t = clamp(1.0 - d / r, 0.0, 1.0);
+ if (uGhostFocal != 0.0) t = pow(t, exp2(2.0 * clamp(uGhostFocal, -1.0, 1.0)));
+ return t * t * (3.0 - 2.0 * t);
+}
+vec3 onPlane(vec3 v, vec3 n) { return v - n * dot(v, n); }
+vec3 ghostMove(vec3 x, vec3 n, int first, int end) {
+ float sgn = uGhostKind.y != 0 ? -1.0 : 1.0;
+ int kind = uGhostKind.x;
+ for (int i = first; i < end; ++i) {
+ vec3 c = uGhostA[i].xyz;
+ float r = uGhostA[i].w;
+ float dist = length(x - c);
+ if (dist >= r) continue;
+ float w = uGhostC[i].w;
+ float f = ghostFalloff(dist, r) * w;
+ float s = uGhostB[i].w;
+ vec3 an = uGhostB[i].xyz;
+ vec3 ac = uGhostC[i].xyz;
+ if (kind == 1) x += an * (sgn * s * r * 0.06 * f);
+ else if (kind == 2) x += n * (sgn * s * r * 0.05 * f);
+ else if (kind == 3) {
+ float h = dot(x - (ac + an * (sgn * r * 0.05 * s)), an);
+ if (sgn * h < 0.0) x -= an * (h * f);
+ } else if (kind == 4) {
+ float e = clamp((1.0 - dist / r) / 0.35, 0.0, 1.0);
+ float h = dot(x - (ac + an * (sgn * r * 0.08 * s)), an);
+ if (sgn * h < 0.0) x -= an * (h * e * e * (3.0 - 2.0 * e) * w);
+ } else if (kind == 5) {
+ x -= an * (dot(x - ac, an) * s * f * (uGhostKind.y != 0 ? -0.5 : 1.0));
+ } else if (kind == 6) {
+ x += an * (-sgn * s * r * 0.05 * f * f) + onPlane(c - x, an) * (0.25 * s * f);
+ } else if (kind == 7) {
+ x += onPlane(c - x, an) * (sgn * 0.2 * s * f);
+ } else if (kind == 8) {
+ x += an * (-sgn * s * r * 0.08 * f * f * f * f) + onPlane(c - x, an) * (0.3 * s * f);
+ } else if (kind == 9) {
+ float h = dot(x - (ac - an * (sgn * r * 0.02 * s)), an);
+ if (sgn * h > 0.0) x -= an * (h * min(1.0, sqrt(f) * (0.5 + s)));
+ }
+ }
+ return x;
+}
+#endif
void main() {
uvec3 q = uvec3(aPos.x & 0x1FFFFFu, (aPos.x >> 21) | ((aPos.y & 0x3FFu) << 11), (aPos.y >> 10) & 0x1FFFFFu);
- vec4 p = uView * vec4(uGridOrigin + vec3(q) * uGridStep, 1.0);
+ vec3 world = uGridOrigin + vec3(q) * uGridStep;
+ vec3 normal = decodeNormal(aNormal);
float curvature = float(int(aNormal >> 22) - 512) / 511.0; // sign(c) sqrt(|c|), render/Clusters.h
- vNormalCurv = vec4(mat3(uView) * decodeNormal(aNormal), curvature * abs(curvature));
+ curvature *= abs(curvature);
+#ifdef GHOST
+ for (int g = 0; g < uGhostGroups; ++g) {
+ if (distance(world, uGhostBound[g].xyz) >= uGhostBound[g].w) continue;
+ int first = uGhostRange[g].x, end = uGhostRange[g].y;
+ vec3 m0 = ghostMove(world, normal, first, end);
+ if (m0 == world) continue;
+ // The moved point and four neighbours on its tangent plane moved alike: the displaced surface's normal
+ // (central differences) and the change of the cavity term, which is the edge length over four times the
+ // Laplacian of the move along the normal (Renderer.cpp curvatureAt: neighbour average above the vertex).
+ vec3 t1 = normalize(abs(normal.x) < 0.9 ? cross(normal, vec3(1.0, 0.0, 0.0)) : cross(normal, vec3(0.0, 1.0, 0.0)));
+ vec3 t2 = cross(normal, t1);
+ float e = uGhostEps;
+ vec3 a1 = ghostMove(world + t1 * e, normal, first, end), b1 = ghostMove(world - t1 * e, normal, first, end);
+ vec3 a2 = ghostMove(world + t2 * e, normal, first, end), b2 = ghostMove(world - t2 * e, normal, first, end);
+ float h0 = dot(m0 - world, normal);
+ float lap = (dot(a1 - world - t1 * e, normal) + dot(b1 - world + t1 * e, normal) + dot(a2 - world - t2 * e, normal) +
+ dot(b2 - world + t2 * e, normal) - 4.0 * h0) / (e * e);
+ curvature = clamp(curvature + 0.25 * uGhostEdge * lap, -1.0, 1.0);
+ vec3 bent = cross(a1 - b1, a2 - b2);
+ if (dot(bent, bent) > 1e-30) normal = normalize(dot(bent, normal) < 0.0 ? -bent : bent);
+ world = m0;
+ }
+#endif
+ vec4 p = uView * vec4(world, 1.0);
+ vNormalCurv = vec4(mat3(uView) * normal, curvature);
#ifdef FLAT
vViewPos = p.xyz;
#endif
@@ -519,7 +602,7 @@ bool Renderer::init(std::string& error) {
const Renderer::MeshProgram* Renderer::meshProgram(int features) {
if (features & (kTextured | kNormalMap)) features |= kTextured | kPrim;
- MeshProgram& p = programs_[features & 31];
+ MeshProgram& p = programs_[features & 63];
if (p.id) return &p;
std::string prefix = "#version 330 core\n";
if (features & kPaint) prefix += "#define PAINT\n";
@@ -527,6 +610,7 @@ const Renderer::MeshProgram* Renderer::meshProgram(int features) {
if (features & kTextured) prefix += "#define TEXTURED\n";
if (features & kNormalMap) prefix += "#define NORMALMAP\n";
if (features & kFlat) prefix += "#define FLAT\n";
+ if (features & kGhost) prefix += "#define GHOST\n";
const std::string vs = prefix + kMeshVS, fs = prefix + kMeshFS, gs = prefix + kMeshGS;
p.id = link(vs.c_str(), fs.c_str(), programError_, nullptr, (features & kTextured) ? gs.c_str() : nullptr);
if (!p.id) return nullptr;
@@ -548,6 +632,16 @@ const Renderer::MeshProgram* Renderer::meshProgram(int features) {
p.hasPaint = at("uHasPaint");
p.normalFlipY = at("uNormalFlipY");
p.twoSided = at("uTwoSided");
+ p.ghostGroups = at("uGhostGroups");
+ p.ghostRange = at("uGhostRange");
+ p.ghostEps = at("uGhostEps");
+ p.ghostEdge = at("uGhostEdge");
+ p.ghostA = at("uGhostA");
+ p.ghostB = at("uGhostB");
+ p.ghostC = at("uGhostC");
+ p.ghostBound = at("uGhostBound");
+ p.ghostKind = at("uGhostKind");
+ p.ghostFocal = at("uGhostFocal");
// Texture units: groups 1, matcap 2, UVs 3, painted texture 4, normal map 5, tangents 6, cluster table 7.
gl::UseProgram(p.id);
gl::Uniform1i(at("uGroupIds"), 1);
@@ -959,14 +1053,14 @@ bool Renderer::outOfMemory(uint32_t id) const {
void Renderer::syncScene(const std::vector<SceneMesh>& scene) {
syncMatcaps();
// Free the slots of SubTools that are gone (deleted, merged, a new scene).
- for (size_t i = 0; i < slots_.size();) {
- const bool live = std::any_of(scene.begin(), scene.end(), [&](const SceneMesh& m) { return m.id == slots_[i].id; });
+ for (auto it = slots_.begin(); it != slots_.end();) {
+ const bool live = std::any_of(scene.begin(), scene.end(), [&](const SceneMesh& m) { return m.id == it->id; });
if (live) {
- ++i;
+ ++it;
continue;
}
- freeSlot(slots_[i]);
- slots_.erase(slots_.begin() + std::ptrdiff_t(i));
+ freeSlot(*it);
+ it = slots_.erase(it);
}
drawList_.clear();
for (const SceneMesh& m : scene) {
@@ -1688,9 +1782,14 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boo
gl::BindVertexArray(0);
s.grid = PositionGrid::around(meshBounds(mesh));
dirtyRuns_.assign(1, {0u, uint32_t(s.order.size())});
+ } else if (s.regrid) { // a stroke's vertex left the grid (prepareStrokeUpload): a new one, everything sent
+ s.grid = PositionGrid::around(meshBounds(mesh));
+ dirtyRuns_.assign(1, {0u, uint32_t(s.order.size())});
+ std::fill(s.blockHash.begin(), s.blockHash.end(), ~uint64_t(0) >> 1); // (no block keeps its hash)
} else {
s.order.dirtyRuns(mesh, dirtyRuns_);
}
+ s.regrid = false;
const size_t ns = s.order.size();
const bool ranged = !full && !dirtyRuns_.empty();
const std::span<const uint32_t> order = s.order.fromGpu();
@@ -2237,11 +2336,13 @@ void Renderer::drawSlot(const MeshSlot& s, bool active, bool flat, const RenderS
// A baked normal map preview bends the shading normal (core/Bake.h).
const bool normalMapped = s.hasUvs && s.normalTexture && s.hasTangents;
const int groups = active && settings.showPolygroups ? (s.hasGroups ? 1 : 2) : 0;
+ const bool ghost = active && s.id == ghostId_ && !ghost_.empty() && !settings.motionProxy;
const int features = (s.hasColors || s.hasMask ? kPaint : 0) | (groups == 1 ? kPrim : 0) | (textured ? kTextured : 0) |
- (normalMapped ? kNormalMap : 0) | (flat ? kFlat : 0);
+ (normalMapped ? kNormalMap : 0) | (flat ? kFlat : 0) | (ghost ? kGhost : 0);
const MeshProgram* p = meshProgram(features);
if (!p) return;
gl::UseProgram(p->id);
+ if (ghost) setGhostUniforms(*p);
gl::UniformMatrix4fv(p->view, 1, GL_FALSE, glm::value_ptr(frameView_));
gl::UniformMatrix4fv(p->proj, 1, GL_FALSE, glm::value_ptr(frameProj_));
gl::Uniform3fv(p->gridOrigin, 1, glm::value_ptr(s.grid.origin));
@@ -2313,6 +2414,205 @@ void Renderer::drawSlot(const MeshSlot& s, bool active, bool flat, const RenderS
gl::Disable(GL_CULL_FACE);
}
+// ---------------------------------------------------------------------------
+// Strokes on heavy meshes: the ghost and the two-part upload (app/AsyncStroke.cpp)
+// ---------------------------------------------------------------------------
+
+void Renderer::setGhost(uint32_t id, const GhostStroke& ghost) {
+ ghostId_ = id;
+ ghost_.kind = ghost.kind;
+ ghost_.invert = ghost.invert;
+ ghost_.focal = ghost.focal;
+ ghost_.edge = ghost.edge;
+ ghost_.dabs.assign(ghost.dabs.begin(), ghost.dabs.begin() + std::ptrdiff_t(std::min(ghost.dabs.size(), GhostStroke::kMaxDabs)));
+}
+
+void Renderer::setGhostUniforms(const MeshProgram& p) {
+ glm::vec4 a[GhostStroke::kMaxDabs], b[GhostStroke::kMaxDabs], c[GhostStroke::kMaxDabs];
+ glm::vec4 bound[GhostStroke::kMaxMirrors];
+ GLint range[GhostStroke::kMaxMirrors * 2];
+ int groups = 0;
+ float minRadius = 1e30f;
+ const size_t n = ghost_.dabs.size();
+ for (size_t i = 0; i < n; ++i) {
+ const GhostDab& d = ghost_.dabs[i];
+ a[i] = glm::vec4(d.center, d.radius);
+ b[i] = glm::vec4(d.normal, d.strength);
+ c[i] = glm::vec4(d.areaCenter, d.weight);
+ minRadius = std::min(minRadius, d.radius);
+ const bool next = i == 0 || d.mirror != ghost_.dabs[i - 1].mirror;
+ if (next) {
+ if (groups == int(GhostStroke::kMaxMirrors)) break;
+ range[groups * 2] = range[groups * 2 + 1] = GLint(i);
+ ++groups;
+ }
+ range[groups * 2 - 1] = GLint(i + 1);
+ }
+ // Each run's bounding sphere (around its dabs' spheres, a little wider: the moves reach out a little).
+ for (int g = 0; g < groups; ++g) {
+ AABB box;
+ for (GLint i = range[g * 2]; i < range[g * 2 + 1]; ++i) {
+ box.expand(glm::vec3(a[i]) - glm::vec3(a[i].w));
+ box.expand(glm::vec3(a[i]) + glm::vec3(a[i].w));
+ }
+ bound[g] = glm::vec4(box.center(), glm::length(box.max - box.min) * 0.5f * 1.1f);
+ }
+ gl::Uniform1i(p.ghostGroups, groups);
+ gl::Uniform4fv(p.ghostBound, groups, glm::value_ptr(bound[0]));
+ gl::Uniform2iv(p.ghostRange, groups, range);
+ gl::Uniform4fv(p.ghostA, GLsizei(n), glm::value_ptr(a[0]));
+ gl::Uniform4fv(p.ghostB, GLsizei(n), glm::value_ptr(b[0]));
+ gl::Uniform4fv(p.ghostC, GLsizei(n), glm::value_ptr(c[0]));
+ gl::Uniform2i(p.ghostKind, GLint(ghost_.kind), ghost_.invert ? 1 : 0);
+ gl::Uniform1f(p.ghostFocal, ghost_.focal);
+ gl::Uniform1f(p.ghostEps, std::max(minRadius, 1e-6f) * 0.02f);
+ gl::Uniform1f(p.ghostEdge, ghost_.edge);
+}
+
+bool Renderer::prepareStrokeUpload(uint32_t id, Mesh& mesh, StrokeUpload& out) {
+ out.id = id;
+ out.ready = false;
+ out.data.clear();
+ out.ranges.clear();
+ out.movedClusters.clear();
+ out.bounds.clear();
+ MeshSlot* found = nullptr;
+ for (MeshSlot& s : slots_)
+ if (s.id == id) found = &s;
+ if (!found) return false;
+ MeshSlot& s = *found;
+ const size_t nv = mesh.vertexCount();
+ // What only the regular sync handles: a new layout, faces, attributes appearing, dynamic topology, textures.
+ if (s.fresh || s.outOfMemory || s.regrid || mesh.topologyDirty || mesh.facesDirty || nv != s.vertexCapacity ||
+ !mesh.changedTriangles.empty() || !s.patches.empty() || mesh.normals.size() != nv || s.hasUvs ||
+ s.hasColors != (nv > 0 && mesh.colors.size() == nv) || s.hasMask != (nv > 0 && mesh.mask.size() == nv))
+ return false;
+ std::vector<std::pair<uint32_t, uint32_t>> runs;
+ s.order.dirtyRuns(mesh, runs);
+ if (runs.empty()) {
+ out.ready = true;
+ return true;
+ }
+ // The colours and mask of the dirty runs would go up too (a sculpt stroke doesn't change them; a stroke that
+ // does takes the regular sync).
+ const size_t ns = s.order.size();
+ const size_t nb = (ns + UploadOrder::kBlock - 1) / UploadOrder::kBlock;
+ const std::span<const uint32_t> order = s.order.fromGpu();
+ s.blockHash.resize(nb * 2, 0);
+ out.blockChanged.assign(nb, 0);
+ out.blockMoved.assign(nb, 0);
+ // Pieces of a few thousand slots packed in parallel, each at its offset in `data`.
+ constexpr uint32_t kPiece = 2048;
+ struct Piece {
+ uint32_t begin, end;
+ size_t offset;
+ };
+ std::vector<Piece> pieces;
+ size_t total = 0;
+ for (const auto& [b, e] : runs)
+ for (uint32_t c = b; c < e; c += kPiece) {
+ const uint32_t ce = std::min(e, c + kPiece);
+ pieces.push_back({c, ce, total});
+ total += ce - c;
+ }
+ out.data.resize(total);
+ std::atomic<bool> outside{false};
+ std::vector<uint64_t> hashes(nb * 2, 0); // (written per block, then compared: a failed prepare keeps the old)
+ poolFor(pieces.size(), [&](size_t i0, size_t i1) {
+ for (size_t pi = i0; pi < i1; ++pi) {
+ const Piece& pc = pieces[pi];
+ PackedVertex* dst = out.data.data() + pc.offset;
+ bool inside = true;
+ uint64_t hp = 0, hn = 0;
+ for (uint32_t i = pc.begin; i < pc.end; ++i) {
+ const uint32_t v = order[i];
+ PackedVertex pv{};
+ if (v < nv) inside &= packVertex(s.grid, mesh.positions[v], mesh.normals[v], curvatureAt(mesh, v), pv);
+ dst[i - pc.begin] = pv;
+ hp = (hp ^ (uint64_t(pv.pos[0]) | uint64_t(pv.pos[1]) << 32)) * 0x100000001B3ull + i;
+ hn = (hn ^ pv.normal) * 0x100000001B3ull + i;
+ if ((i + 1) % UploadOrder::kBlock == 0 || i + 1 == pc.end) {
+ const size_t g = i >> UploadOrder::kBlockShift;
+ hashes[g * 2] = hp;
+ hashes[g * 2 + 1] = hn;
+ out.blockMoved[g] = s.blockHash[g * 2] != hp;
+ out.blockChanged[g] = out.blockMoved[g] || s.blockHash[g * 2 + 1] != hn;
+ hp = hn = 0;
+ }
+ }
+ if (!inside) outside.store(true, std::memory_order_relaxed);
+ }
+ }, 1, 2);
+ if (outside.load()) {
+ s.regrid = true; // the next syncScene makes a new grid and sends everything (the mesh stays dirty)
+ return false;
+ }
+ // The changed blocks' GPU ranges and where they are in `data`.
+ for (const Piece& pc : pieces)
+ for (uint32_t b = pc.begin; b < pc.end;) {
+ const uint32_t e = std::min(pc.end, (b & ~(UploadOrder::kBlock - 1)) + UploadOrder::kBlock);
+ const size_t g = b >> UploadOrder::kBlockShift;
+ if (out.blockChanged[g]) {
+ s.blockHash[g * 2] = hashes[g * 2];
+ s.blockHash[g * 2 + 1] = hashes[g * 2 + 1];
+ const size_t offset = pc.offset + (b - pc.begin);
+ if (!out.ranges.empty() && out.ranges.back().end == b && out.ranges.back().offset +
+ (out.ranges.back().end - out.ranges.back().begin) == offset)
+ out.ranges.back().end = e;
+ else out.ranges.push_back({b, e, offset});
+ }
+ b = e;
+ }
+ // The clusters with a block that moved, and their culling bounds.
+ for (const auto& [b, e] : runs)
+ for (uint32_t g = b >> UploadOrder::kBlockShift; g < (e + UploadOrder::kBlock - 1) >> UploadOrder::kBlockShift; ++g) {
+ if (!out.blockMoved[g]) continue;
+ const uint32_t c = s.order.clusterOfSlot(g << UploadOrder::kBlockShift);
+ if (out.movedClusters.empty() || c > out.movedClusters.back()) out.movedClusters.push_back(c);
+ }
+ out.bounds.resize(out.movedClusters.size());
+ poolFor(out.movedClusters.size(), [&](size_t i0, size_t i1) {
+ for (size_t i = i0; i < i1; ++i) out.bounds[i] = clusterBounds(mesh, s.order, out.movedClusters[i]);
+ }, 1, 2);
+ mesh.clearDirty();
+ out.ready = true;
+ return true;
+}
+
+void Renderer::commitStrokeUpload(StrokeUpload& up) {
+ if (!up.ready) return;
+ up.ready = false;
+ MeshSlot* found = nullptr;
+ for (MeshSlot& s : slots_)
+ if (s.id == up.id) found = &s;
+ if (!found) return;
+ MeshSlot& s = *found;
+ if (!up.ranges.empty()) {
+ gl::BindBuffer(GL_ARRAY_BUFFER, s.vertexBuffer);
+ for (const StrokeUpload::Range& r : up.ranges)
+ gl::BufferSubData(GL_ARRAY_BUFFER, GLintptr(size_t(r.begin) * sizeof(PackedVertex)),
+ GLsizeiptr(size_t(r.end - r.begin) * sizeof(PackedVertex)), up.data.data() + r.offset);
+ gl::BindBuffer(GL_ARRAY_BUFFER, 0);
+ }
+ if (!up.movedClusters.empty()) {
+ s.bounds.resize(s.order.clusters().size());
+ s.proxyStale.resize(s.bounds.size(), 1);
+ for (size_t i = 0; i < up.movedClusters.size(); ++i) {
+ s.bounds[up.movedClusters[i]] = up.bounds[i];
+ s.proxyStale[up.movedClusters[i]] = 1;
+ }
+ s.box = AABB();
+ s.volume = 0.0;
+ for (const ClusterBounds& b : s.bounds) {
+ s.volume += b.volume;
+ if (b.radius >= 0.0f) {
+ s.box.expand(b.center - glm::vec3(b.radius));
+ s.box.expand(b.center + glm::vec3(b.radius));
+ }
+ }
+ }
+}
+
// ---------------------------------------------------------------------------
// Thumbnails (LightBox / pickers)
// ---------------------------------------------------------------------------
@@ -2335,10 +2635,10 @@ bool Renderer::renderMeshThumbnail(Mesh& mesh, const Camera& camera, int size, c
const bool ok = renderToImage(camera, size, size, settings, rgba, error);
thumbnailPass_ = false;
drawList_.swap(scene);
- for (size_t i = 0; i < slots_.size(); ++i)
- if (slots_[i].id == kThumbnailSlot) {
- freeSlot(slots_[i]);
- slots_.erase(slots_.begin() + std::ptrdiff_t(i));
+ for (auto it = slots_.begin(); it != slots_.end(); ++it)
+ if (it->id == kThumbnailSlot) {
+ freeSlot(*it);
+ slots_.erase(it);
break;
}
diff --git a/src/render/Renderer.h b/src/render/Renderer.h
index db74e32e..f9ce7d0d 100644
--- a/src/render/Renderer.h
+++ b/src/render/Renderer.h
@@ -9,8 +9,10 @@
#include "render/Camera.h"
#include "render/Clusters.h"
#include "render/GL.h"
+#include "render/Ghost.h"
#include <cstdint>
+#include <list>
#include <string>
#include <vector>
@@ -105,6 +107,32 @@ public:
// GL texture of the painted texture of SubTool `id` as last synced (0 if none), for UI previews (rows bottom-up).
GLuint paintTexture(uint32_t id) const;
+ // --- Strokes on heavy meshes (app/AsyncStroke.cpp) ---
+ // The predictive stroke preview (render/Ghost.h) drawn on SubTool `id`; an empty stroke turns it off.
+ void setGhost(uint32_t id, const GhostStroke& ghost);
+ // A sculpt stroke's vertex upload made in two halves: prepareStrokeUpload packs what the mesh marked dirty
+ // (positions and normals only) and the culling bounds of the clusters that moved, on any thread and without
+ // GL, while the GL thread keeps drawing from what it has; commitStrokeUpload sends it on the GL thread.
+ // prepare clears the mesh's dirty state; it returns false, and leaves the mesh as it was, when the change
+ // needs the regular syncScene instead (a new layout, attributes, dynamic topology, a vertex off the grid).
+ // Only one prepare at a time, and no syncScene of that mesh until its commit.
+ struct StrokeUpload {
+ uint32_t id = 0;
+ bool ready = false;
+ std::vector<PackedVertex> data;
+ struct Range {
+ uint32_t begin, end; // GPU slots
+ size_t offset; // into data
+ };
+ std::vector<Range> ranges;
+ std::vector<uint32_t> movedClusters;
+ std::vector<ClusterBounds> bounds; // of movedClusters
+ std::vector<uint8_t> blockChanged, blockMoved; // scratch, per block
+ size_t bytes() const { return data.size() * sizeof(PackedVertex); }
+ };
+ bool prepareStrokeUpload(uint32_t id, Mesh& mesh, StrokeUpload& out);
+ void commitStrokeUpload(StrokeUpload& up);
+
// Bytes of GPU buffers and textures the mesh slots hold.
size_t gpuBytes() const;
// Triangles the last draw() sent to the GPU (after culling), for the benchmark.
@@ -159,6 +187,7 @@ private:
bool hasTangents = false, tangentsStale = true, normalFlipGreen = false;
bool hasColors = false, hasMask = false, hasGroups = false, hasUvs = false;
bool fresh = true; // nothing uploaded yet
+ bool regrid = false; // a stroke's vertex left the position grid (prepareStrokeUpload): the next sync re-sends everything
bool outOfMemory = false; // the last full upload ran out of GPU memory: not drawn
UploadOrder order; // the cluster layout
PositionGrid grid; // the positions' quantisation
@@ -218,6 +247,11 @@ private:
void buildLines(MeshSlot& s, const Mesh& mesh);
void buildProxies(MeshSlot& s, const Mesh& mesh);
bool proxyUsed_ = false; // the last draw() drew motion proxies: sync keeps them current
+ // The ghost (setGhost), drawn on the slot ghostId_.
+ GhostStroke ghost_;
+ uint32_t ghostId_ = 0;
+ struct MeshProgram;
+ void setGhostUniforms(const MeshProgram& p);
void syncNormalMap(MeshSlot& s, const Mesh& mesh, const NormalMapView* normalMap);
// `indicesDone`: the index buffer and drawCount are already written (a full upload writes them while
// building the layout).
@@ -262,14 +296,16 @@ private:
GLint view = -1, proj = -1, gridOrigin = -1, gridStep = -1, viewport = -1, projScale = -1, baseColor = -1, paint = -1,
showMask = -1, groups = -1, matcap = -1, matcapCavity = -1, dim = -1, primBase = -1, hasPaint = -1,
normalFlipY = -1, twoSided = -1;
+ GLint ghostGroups = -1, ghostRange = -1, ghostEps = -1, ghostEdge = -1, ghostA = -1, ghostB = -1, ghostC = -1, ghostBound = -1, ghostKind = -1, ghostFocal = -1;
};
- static constexpr int kPaint = 1, kPrim = 2, kTextured = 4, kNormalMap = 8, kFlat = 16;
- MeshProgram programs_[32];
+ static constexpr int kPaint = 1, kPrim = 2, kTextured = 4, kNormalMap = 8, kFlat = 16, kGhost = 32;
+ MeshProgram programs_[64];
std::string programError_;
const MeshProgram* meshProgram(int features);
bool vertexIdHasBase_ = true; // gl_VertexID includes the base vertex (checked at init; else per-cluster draws)
bool checkVertexIdBase();
- std::vector<MeshSlot> slots_;
+ // (A list: a slot stays put while others come and go, so prepareStrokeUpload can work on one from another thread.)
+ std::list<MeshSlot> slots_;
struct DrawItem {
uint32_t id;
bool active;
diff --git a/tests/test_ghost.cpp b/tests/test_ghost.cpp
new file mode 100644
index 00000000..4eabb687
--- /dev/null
+++ b/tests/test_ghost.cpp
@@ -0,0 +1,167 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// The predictive stroke preview (render/Ghost.h) against the brush solver it stands in for, and the pen
+// prediction (core/PenPredictor.h).
+#include "TestHarness.h"
+
+#include "core/PenPredictor.h"
+#include "core/Primitives.h"
+#include "core/Sculpt.h"
+#include "render/Ghost.h"
+
+#include <algorithm>
+#include <cmath>
+#include <vector>
+
+using namespace sf;
+
+namespace {
+
+// The ghost of one dab with the frame the solver used for it (what the preview has once a dab is placed).
+GhostStroke ghostOf(BrushType type, const Sculptor::DabFrame& f, const DabParams& p, Symmetry sym) {
+ GhostStroke g;
+ g.kind = ghostKindForBrush(int(type));
+ g.invert = p.invert;
+ g.focal = p.focal;
+ const std::vector<glm::vec3> mirrors = Sculptor::mirrors(sym);
+ for (uint32_t m = 0; m < mirrors.size(); ++m) {
+ GhostDab d;
+ d.center = f.center * mirrors[m];
+ d.radius = p.radius;
+ d.normal = f.normal * mirrors[m];
+ d.strength = p.strength * p.pressure;
+ d.areaCenter = f.areaCenter * mirrors[m];
+ d.mirror = m;
+ g.dabs.push_back(d);
+ }
+ return g;
+}
+
+// Largest distance between where the solver put each vertex and where the ghost draws it, and the largest move.
+struct Error {
+ float worst = 0.0f, moved = 0.0f;
+};
+Error compare(BrushType type, Mesh mesh, const DabParams& p, Symmetry sym) {
+ Document doc = makeDoc(std::move(mesh));
+ const std::vector<glm::vec3> before = doc.mesh.positions, normals = doc.mesh.normals;
+ doc.sculptor.beginStroke(doc.mesh, "test");
+ doc.sculptor.dab(doc.mesh, doc.bvh, type, p, sym);
+ const GhostStroke g = ghostOf(type, doc.sculptor.lastFrame, p, sym);
+ doc.sculptor.endStroke();
+ Error e;
+ for (size_t v = 0; v < before.size(); ++v) {
+ const glm::vec3 ghost = ghostMove(g, before[v], normals[v]);
+ e.worst = std::max(e.worst, glm::distance(ghost, doc.mesh.positions[v]));
+ e.moved = std::max(e.moved, glm::distance(before[v], doc.mesh.positions[v]));
+ }
+ return e;
+}
+
+} // namespace
+
+TEST(ghost_matches_standard_dab_on_a_plane) {
+ // On a plane the area normal is the plane's: the preview is the solver's dab exactly (no jump when it lands).
+ DabParams p;
+ p.center = glm::vec3(0.1f, 0.0f, -0.05f);
+ p.radius = 0.4f;
+ p.strength = 0.7f;
+ p.viewDir = glm::vec3(0.0f, -1.0f, 0.0f);
+ Mesh plane = makePlane(120);
+ const Error e = compare(BrushType::Standard, plane, p, Symmetry{false, false, false});
+ CHECK(e.moved > 0.01f);
+ CHECK(e.worst < e.moved * 1e-3f);
+ // Inverted, focal shift, X symmetry: still exact.
+ p.invert = true;
+ p.focal = 0.4f;
+ p.center.x = 0.3f;
+ const Error s = compare(BrushType::Standard, plane, p, Symmetry{true, false, false});
+ CHECK(s.moved > 0.005f);
+ CHECK(s.worst < s.moved * 1e-3f);
+}
+
+TEST(ghost_follows_other_kernels_on_a_plane) {
+ // Kernels that use only the area frame and the falloff match on a plane up to float noise; Clay and Flatten
+ // pull towards a plane through the area centre, which the frame carries.
+ Mesh plane = makePlane(120);
+ for (BrushType t : {BrushType::Inflate, BrushType::Crease, BrushType::Pinch, BrushType::DamStandard, BrushType::Clay,
+ BrushType::Flatten, BrushType::Trim}) {
+ DabParams p;
+ p.center = glm::vec3(0.05f, 0.0f, 0.0f);
+ p.radius = 0.35f;
+ p.strength = 0.6f;
+ p.viewDir = glm::vec3(0.0f, -1.0f, 0.0f);
+ const Error e = compare(t, plane, p, Symmetry{false, false, false});
+ // (Clay, Flatten and Trim leave a flat plane where it is: nothing to compare then.)
+ CHECK(e.worst <= std::max(e.moved * 0.02f, 1e-5f));
+ }
+}
+
+TEST(ghost_estimates_a_curved_surface_closely) {
+ // On a sphere the preview uses the solver's frame of the dab; the vertices' own curvature makes the dab's
+ // normals fan out a little where the solver's area normal doesn't: the preview stays within a tenth of the move.
+ Mesh sphere = makeIcosphere(6);
+ DabParams p;
+ p.center = glm::normalize(glm::vec3(0.2f, 0.3f, 1.0f));
+ p.radius = 0.3f;
+ p.strength = 0.8f;
+ p.viewDir = -p.center;
+ const Error e = compare(BrushType::Standard, sphere, p, Symmetry{true, false, false});
+ CHECK(e.moved > 0.005f);
+ CHECK(e.worst < e.moved * 0.1f);
+}
+
+TEST(ghost_kinds_cover_the_displacement_brushes) {
+ CHECK(ghostKindForBrush(int(BrushType::Standard)) == GhostKind::Standard);
+ CHECK(ghostKindForBrush(int(BrushType::Clay)) == GhostKind::Clay);
+ CHECK(ghostKindForBrush(int(BrushType::ClayBuildup)) == GhostKind::ClayBuildup);
+ CHECK(ghostKindForBrush(int(BrushType::Trim)) == GhostKind::Trim);
+ CHECK(ghostKindForBrush(int(BrushType::Smooth)) == GhostKind::None);
+ CHECK(ghostKindForBrush(int(BrushType::Layer)) == GhostKind::None);
+ CHECK(ghostKindForBrush(int(BrushType::Move)) == GhostKind::None);
+ CHECK(ghostKindForBrush(int(BrushType::Vdm)) == GhostKind::None);
+ CHECK(ghostFalloff(0.0f, 1.0f, 0.0f) == brushFalloff(0.0f, 1.0f, 0.0f));
+ for (float f : {-0.7f, 0.0f, 0.5f})
+ for (float d : {0.1f, 0.5f, 0.9f}) CHECK(std::abs(ghostFalloff(d, 1.0f, f) - brushFalloff(d, 1.0f, f)) < 1e-6f);
+}
+
+TEST(pen_predictor_extrapolates_steady_motion) {
+ PenPredictor pp;
+ // 200 Hz samples on a straight line at 800 px/s: a frame (16 ms) ahead lands on the line, 12.8 px on.
+ pp.reset(glm::vec2(100.0f, 100.0f), 0.0);
+ glm::vec2 last;
+ for (int i = 1; i <= 40; ++i) {
+ const double t = i * 0.005;
+ last = glm::vec2(100.0f + 800.0f * float(t), 100.0f);
+ pp.add(last, t);
+ }
+ const glm::vec2 at = pp.predict(0.2 + 0.016, last);
+ CHECK(std::abs(at.x - (last.x + 12.8f)) < 0.5f);
+ CHECK(std::abs(at.y - 100.0f) < 0.1f);
+ // Stopped: no new sample for a while predicts where it is.
+ CHECK(glm::distance(pp.predict(0.2 + 0.2, last), last) < 1e-4f);
+}
+
+TEST(pen_predictor_tracks_a_circle_better_than_holding_still) {
+ // The --measure-stroke circle (176 px, two turns in 3 s), sampled at 120 Hz: predicting a frame ahead must be
+ // much closer to where the pointer is a frame later than the last sample is.
+ PenPredictor pp;
+ const float r = 176.0f;
+ auto at = [&](double t) {
+ const double a = t / 3.0 * 4.0 * 3.14159265358979;
+ return glm::vec2(r * float(std::cos(a)), r * float(std::sin(a)));
+ };
+ pp.reset(at(0.0), 0.0);
+ double predicted = 0.0, held = 0.0;
+ int n = 0;
+ for (int i = 1; i < 300; ++i) {
+ const double t = i / 120.0;
+ pp.add(at(t), t);
+ if (i < 10) continue;
+ const glm::vec2 truth = at(t + 1.0 / 120.0);
+ predicted += glm::distance(pp.predict(t + 1.0 / 120.0, at(t)), truth);
+ held += glm::distance(at(t), truth);
+ ++n;
+ }
+ CHECK(predicted / n < held / n * 0.25);
+}
diff --git a/tools/bench_strokes.sh b/tools/bench_strokes.sh
new file mode 100644
index 00000000..75b7ef8c
--- /dev/null
+++ b/tools/bench_strokes.sh
@@ -0,0 +1,72 @@
+#!/usr/bin/env bash
+# SculptForge - open source digital sculpting
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+# Big-brush stroke benchmark: the stroke SculptForge draws itself (--measure-stroke, a fast two-turn circle with a
+# 200 px brush, X symmetry) on spheres of about 5M, 21M and 33M triangles (two of them made by Divide), with the
+# frame times, how far the brush trails the pointer, press and release. See docs/performance.md.
+#
+# tools/bench_strokes.sh [options] BINARY [BINARY_B]
+#
+# -s SIZES comma list of 5, 21, 33 (default all three)
+# -n RUNS runs per size and binary (default 3); with two binaries the runs alternate A, B, A, B...
+# -r PX brush radius in screen pixels (default 200)
+# -t SEC stroke length in seconds (default 3)
+# -e VAR=VAL environment for every run (repeatable), e.g. -e SF_PREDICTIVE_STROKE=0
+#
+# Each run prints SculptForge's own summary line; at the end a table of the means per size and binary.
+# Needs a display (the stroke is drawn in the real window); run it on an otherwise idle machine.
+set -euo pipefail
+
+sizes="5,21,33"
+runs=3
+radius=200
+seconds=3
+envs=()
+while getopts "s:n:r:t:e:h" opt; do
+ case $opt in
+ s) sizes=$OPTARG ;;
+ n) runs=$OPTARG ;;
+ r) radius=$OPTARG ;;
+ t) seconds=$OPTARG ;;
+ e) envs+=("$OPTARG") ;;
+ *) sed -n '5,18p' "$0"; exit 1 ;;
+ esac
+done
+shift $((OPTIND - 1))
+[ $# -ge 1 ] || { sed -n '5,18p' "$0"; exit 1; }
+bins=("$@")
+
+scene_for() {
+ case $1 in
+ 5) echo "new_sphere subdivisions=8; subdiv_divide" ;; # 5.2M triangles
+ 21) echo "new_sphere subdivisions=8; subdiv_divide; subdiv_divide" ;; # 21.0M
+ 33) echo "new_sphere triangles=2100000; subdiv_divide; subdiv_divide" ;; # 33.6M
+ *) echo "unknown size $1 (use 5, 21 or 33)" >&2; exit 1 ;;
+ esac
+}
+
+results=$(mktemp)
+trap 'rm -f "$results"' EXIT
+IFS=, read -ra sizeList <<<"$sizes"
+for size in "${sizeList[@]}"; do
+ scene=$(scene_for "$size")
+ for ((i = 1; i <= runs; i++)); do
+ for b in "${!bins[@]}"; do
+ bin=${bins[$b]}
+ label=$([ "$b" = 0 ] && echo A || echo B)
+ out=$(env ${envs[@]+"${envs[@]}"} "$bin" --run "$scene" --measure-stroke "$seconds,$radius" 2>&1 || true)
+ line=$(printf '%s\n' "$out" | grep -A3 '^Stroke' || true)
+ echo "== ${size}M $label run $i ($bin)"
+ printf '%s\n' "$line"
+ # The machine-readable line: frames mean, worst, surface lag mean, preview lag mean, press, release.
+ printf '%s\n' "$out" | sed -n "s/^bench: /$size $label /p" >>"$results"
+ done
+ done
+done
+
+echo
+echo "Means (ms / px): size, binary, frame mean, frame worst, surface lag, preview lag, press, first surface, release, release until done"
+awk '{ k = $1 " " $2; n[k]++; for (i = 3; i <= NF; i++) s[k, i] += $i; nf[k] = NF }
+ END { for (k in n) { printf "%-6s", k; for (i = 3; i <= nf[k]; i++) printf " %8.1f", s[k, i] / n[k]; printf " (%d runs)\n", n[k] } }' \
+ "$results" | sort -n