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.

Stroke uploads: cavity term from the normals pass, quick culling bounds, a cheaper preview shader

islandmed3c85a7ec2898
diff --git a/src/app/App.cpp b/src/app/App.cpp
index ec2ff634..2effd83e 100644
--- a/src/app/App.cpp
+++ b/src/app/App.cpp
@@ -637,6 +637,13 @@ int App::run() {
const char* continuousEnv = std::getenv("SF_CONTINUOUS_REDRAW");
const bool continuous = continuousEnv && *continuousEnv && *continuousEnv != '0';
const bool logRedraw = std::getenv("SF_REDRAW_LOG") != nullptr; // why each frame is drawn (bug reports)
+ // SF_FRAME_LOG=ms: every frame slower than that, with where its time went.
+ const double frameLogMs = std::getenv("SF_FRAME_LOG") ? std::atof(std::getenv("SF_FRAME_LOG")) : -1.0;
+ double phase[10] = {};
+ int phases = 0;
+ const auto mark = [&] {
+ if (frameLogMs >= 0.0 && phases < 10) phase[phases++] = double(SDL_GetTicksNS()) * 1e-6;
+ };
auto seconds = [] { return double(SDL_GetTicksNS()) * 1e-9; };
// --measure-idle: let start-up settle (first frames, the LightBox's thumbnails), then count.
constexpr double kMeasureSettle = 3.0;
@@ -726,7 +733,10 @@ int App::run() {
}
++wakeUps;
}
+ phases = 0;
+ mark();
while (SDL_PollEvent(&e)) onEvent(e);
+ mark();
if (options_.measureStroke > 0.0f && !sm.started && t0 - started >= kMeasureSettle && !backgroundBusy()) {
const CanvasRect c = canvasRect();
sm.cx = c.x + c.w * 0.5f;
@@ -796,6 +806,7 @@ int App::run() {
// for a limited time, then look at the pointer again.
if (asyncStroke_) pumpStroke();
else drainStrokeDabs(kStrokeBudgetSeconds);
+ mark();
Uint64 now = SDL_GetTicksNS();
float dt = float(double(now - lastTicks) * 1e-9);
lastTicks = now;
@@ -846,10 +857,12 @@ int App::run() {
ImGui::NewFrame();
drawUi();
ImGui::Render();
+ mark();
buildGhost(); // the dabs not on the GPU yet, drawn as a preview (predictive strokes)
const auto syncStart = std::chrono::steady_clock::now();
syncScene();
+ mark();
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
@@ -875,8 +888,14 @@ int App::run() {
}
}
renderSettings_.motionProxy = false; // (other renders, e.g. project thumbnails, draw in full)
+ mark();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window_);
+ mark();
+ if (phases == 7 && phase[6] - phase[0] > frameLogMs)
+ SDL_Log("frame %.1f ms: events %.1f, dabs %.1f, UI %.1f, ghost + sync %.1f, draw %.1f, UI draw + swap %.1f",
+ phase[6] - phase[0], phase[1] - phase[0], phase[2] - phase[1], phase[3] - phase[2], phase[4] - phase[3],
+ phase[5] - phase[4], phase[6] - phase[5]);
++framesDrawn;
{
const double frameNow = seconds();
diff --git a/src/app/AsyncStroke.cpp b/src/app/AsyncStroke.cpp
index 86524dda..dd583c6d 100644
--- a/src/app/AsyncStroke.cpp
+++ b/src/app/AsyncStroke.cpp
@@ -62,6 +62,7 @@ void App::StrokeSolver::loop() {
}
probe.clear();
double spent = 0.0;
+ const SculptTimings before = app.doc_.sculptor.timings;
app.dabView_ = &view;
for (const PathDab& d : batch) {
const auto t0 = std::chrono::steady_clock::now();
@@ -72,7 +73,18 @@ void App::StrokeSolver::loop() {
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);
+ const auto prepStart = std::chrono::steady_clock::now();
+ uploadReady = prepare && app.renderer_.prepareStrokeUpload(uploadId, app.doc_.mesh, upload, app.doc_.sculptor.strokeCurvature());
+ static const bool log = std::getenv("SF_STROKE_LOG") != nullptr;
+ if (log) {
+ const SculptTimings& t = app.doc_.sculptor.timings;
+ SDL_Log("solver: %zu dabs %.1f ms (kernel %.1f, normals %.1f, BVH %.1f), upload packed in %.1f ms (vertices %.1f, "
+ "bounds %.1f), %.1f MB",
+ batch.size(), spent * 1000.0, t.kernelMs - before.kernelMs, t.normalsMs - before.normalsMs,
+ t.bvhMs - before.bvhMs,
+ std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - prepStart).count(),
+ upload.packMs, upload.boundsMs, double(upload.bytes()) / 1e6);
+ }
{
std::lock_guard<std::mutex> lock(mutex_);
done = true;
@@ -137,8 +149,11 @@ void App::commitSolverBatch() {
batch.swap(s.batch);
}
if (s.uploadReady) {
+ const auto t0 = std::chrono::steady_clock::now();
renderer_.commitStrokeUpload(s.upload);
s.uploadReady = false;
+ static const bool log = std::getenv("SF_STROKE_LOG") != nullptr;
+ if (log) SDL_Log("commit: %.1f ms", std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count());
} else {
syncScene(); // (the regular sync: a new grid, or a mesh the two-part upload doesn't handle)
}
@@ -215,6 +230,7 @@ void App::pumpStroke() {
}
void App::finishStrokeNow() {
+ if (asyncStroke_) renderer_.refineStrokeBounds(doc_.activeSubtoolInfo().id, displayedMesh()); // (the quick ones)
strokeFinishing_ = false;
asyncStroke_ = false;
renderer_.setGhost(0, GhostStroke());
diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cpp
index 41adc74d..f85945a4 100644
--- a/src/app/UiTest.cpp
+++ b/src/app/UiTest.cpp
@@ -344,6 +344,7 @@ double Demo::query(const std::string& name, bool& known) const {
if (name == "antialiasing") return double(app_.uiPrefs_.antialiasing);
if (name == "view_samples") return double(app_.perf_.samplesWanted);
if (name == "multithreading") return ThreadPool::shared().serial() ? 0.0 : 1.0;
+ if (name == "predictive_strokes") return app_.predictiveStrokes() ? 1.0 : 0.0;
if (name == "last_stroke_dabs") return double(app_.perf_.lastStroke.dabs);
if (name == "texture_size") return hasTexture(m) ? double(m.texture->width) : 0.0;
if (name == "paint_texture") return paintsTexture(m) ? 1.0 : 0.0;
diff --git a/src/core/Mesh.cpp b/src/core/Mesh.cpp
index 2dc24c97..84c94f35 100644
--- a/src/core/Mesh.cpp
+++ b/src/core/Mesh.cpp
@@ -601,7 +601,7 @@ void Mesh::recomputeAllNormals() {
markAllDirty();
}
-void Mesh::updateNormals(std::span<const uint32_t> verts) {
+void Mesh::updateNormals(std::span<const uint32_t> verts, float* curvature) {
// The vertices whose normal changes: the moved ones and every neighbour, gathered as a set and listed in
// index order; then each one's normal from its faces, in its triangle order (as recomputeAllNormals sums
// them). The set, and every normal computed from it, doesn't depend on the thread count.
@@ -613,13 +613,33 @@ void Mesh::updateNormals(std::span<const uint32_t> verts) {
for (uint32_t u : neighbors(v)) set.insertLocal(u);
}
});
- finishNormals();
+ finishNormals(curvature);
}
-void Mesh::updateNormalsAround(size_t count, size_t grain, const std::function<void(size_t, size_t, IndexBits&)>& fill) {
+void Mesh::updateNormalsAround(size_t count, size_t grain, const std::function<void(size_t, size_t, IndexBits&)>& fill,
+ float* curvature) {
if (!beginNormalSet()) return;
normalBits_.insertParallel(count, grain, fill);
- finishNormals();
+ finishNormals(curvature);
+}
+
+float Mesh::curvatureAt(uint32_t v) const {
+ if (v >= positions.size() || v >= normals.size()) return 0.0f;
+ const glm::vec3 p = positions[v];
+ glm::vec3 around(0.0f);
+ float edges = 0.0f;
+ uint32_t corners = 0;
+ for (uint32_t ti : vertexTriangles(v)) {
+ const glm::uvec3& t = triangles[ti];
+ for (int k = 0; k < 3; ++k)
+ if (t[k] != v) {
+ const glm::vec3& q = positions[t[k]];
+ around += q;
+ edges += glm::distance(q, p);
+ ++corners;
+ }
+ }
+ return corners && edges > 0.0f ? glm::dot(normals[v], around - p * float(corners)) / edges : 0.0f;
}
bool Mesh::beginNormalSet() {
@@ -630,7 +650,7 @@ bool Mesh::beginNormalSet() {
return true;
}
-void Mesh::finishNormals() {
+void Mesh::finishNormals(float* curvature) {
// Listing the set also marks it dirty: its words are laid out like dirtyBits, and each 2048-vertex dirty
// block lies in one of its chunks (so no two threads write the same word).
static_assert(IndexBits::kChunk % kDirtyBlock == 0);
@@ -657,9 +677,31 @@ void Mesh::finishNormals() {
}
const uint32_t v = scratchVerts_[i];
glm::vec3 sum(0.0f);
- for (uint32_t ti : vertexTriangles(v)) sum += faceNormal(positions, triangles[ti]);
+ if (!curvature) {
+ for (uint32_t ti : vertexTriangles(v)) sum += faceNormal(positions, triangles[ti]);
+ const float len = glm::length(sum);
+ if (len > 1e-20f) normals[v] = sum / len;
+ continue;
+ }
+ // With the cavity term (curvatureAt) from the same corners.
+ const glm::vec3 p = positions[v];
+ glm::vec3 around(0.0f);
+ float edges = 0.0f;
+ uint32_t corners = 0;
+ for (uint32_t ti : vertexTriangles(v)) {
+ const glm::uvec3& t = triangles[ti];
+ sum += faceNormal(positions, t);
+ for (int k = 0; k < 3; ++k)
+ if (t[k] != v) {
+ const glm::vec3& q = positions[t[k]];
+ around += q;
+ edges += glm::distance(q, p);
+ ++corners;
+ }
+ }
const float len = glm::length(sum);
if (len > 1e-20f) normals[v] = sum / len;
+ curvature[v] = corners && edges > 0.0f ? glm::dot(normals[v], around - p * float(corners)) / edges : 0.0f;
}
}, 4096);
}
diff --git a/src/core/Mesh.h b/src/core/Mesh.h
index 2fe4cdf7..9902268c 100644
--- a/src/core/Mesh.h
+++ b/src/core/Mesh.h
@@ -204,12 +204,20 @@ public:
// subdivision stack, made by Divide, gets implicit adjacency (implicitAdjacency).
void rebuildTopology();
void recomputeAllNormals();
- // Recompute normals of the faces around `verts` and of every vertex they touch.
- void updateNormals(std::span<const uint32_t> verts);
+ // Recompute normals of the faces around `verts` and of every vertex they touch. With `curvature` (indexed
+ // by vertex), also each of those vertices' curvatureAt, from the same triangles (a brush stroke hands it to
+ // the renderer, which then needn't walk the rings again).
+ void updateNormals(std::span<const uint32_t> verts, float* curvature = nullptr);
// The same, given the vertices whose normals may have changed (any superset: the others come out the
// same): fill(begin, end, set) adds those of jobs [begin, end) of `count` with set.insertLocal(). A brush
// gives the corners of the BVH leaves its query listed, which is cheaper than every moved vertex's ring.
- void updateNormalsAround(size_t count, size_t grain, const std::function<void(size_t, size_t, IndexBits&)>& fill);
+ void updateNormalsAround(size_t count, size_t grain, const std::function<void(size_t, size_t, IndexBits&)>& fill,
+ float* curvature = nullptr);
+ // The renderer's cavity term at `v` (render/Clusters.h): how far the average of the other corners of its
+ // triangles sits above the vertex along its normal (> 0 in a crease), over their mean distance. Each
+ // neighbour of an inner vertex of a manifold is the corner of two of its triangles, so this is the average
+ // over its ring.
+ float curvatureAt(uint32_t v) const;
// The vertices sharing an edge with `v`, and the triangles around it, in ascending order.
IndexRing neighbors(uint32_t v) const {
@@ -280,7 +288,7 @@ public:
private:
void finishTopology();
bool beginNormalSet(); // updateNormals: room for the set (false: no topology)
- void finishNormals(); // ... then each member's normal
+ void finishNormals(float* curvature); // ... then each member's normal (and curvatureAt)
IndexRing implicitTriangles(uint32_t v) const;
IndexRing implicitNeighbors(uint32_t v) const;
diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cpp
index bede67b2..75c082b7 100644
--- a/src/core/Sculpt.cpp
+++ b/src/core/Sculpt.cpp
@@ -663,8 +663,9 @@ void Sculptor::finishDab(Mesh& mesh, BVH& bvh, std::chrono::steady_clock::time_p
// as it remeshed, so there the rings are used.
void Sculptor::updateDabNormals(Mesh& mesh, const BVH& bvh) {
auto clock = std::chrono::steady_clock::now();
+ if (curvature_.size() != mesh.vertexCount()) curvature_.assign(mesh.vertexCount(), 0.0f);
if (dynTopo_.active() || dabLeaves_.empty()) {
- mesh.updateNormals(dabVerts_);
+ mesh.updateNormals(dabVerts_, curvature_.data());
} else {
mesh.updateNormalsAround(dabLeaves_.size(), 64, [&](size_t b, size_t e, IndexBits& set) {
for (size_t i = b; i < e; ++i)
@@ -674,7 +675,7 @@ void Sculptor::updateDabNormals(Mesh& mesh, const BVH& bvh) {
set.insertLocal(tri.y);
set.insertLocal(tri.z);
}
- });
+ }, curvature_.data());
}
dabLeaves_.clear();
timings.normalsMs += lapMs(clock);
diff --git a/src/core/Sculpt.h b/src/core/Sculpt.h
index 86b22035..0057166c 100644
--- a/src/core/Sculpt.h
+++ b/src/core/Sculpt.h
@@ -125,6 +125,11 @@ public:
// DragDot; restoreStroke) put positions back, not topology. restoreStroke holds it too.
void holdTopology() { topologyHeld_ = true; }
+ // The cavity term (Mesh::curvatureAt) of every vertex whose normal this stroke's dabs updated, by vertex,
+ // worked out with the normals (the renderer packs it instead of walking the rings again); null outside a
+ // stroke. Entries of other vertices are stale.
+ const float* strokeCurvature() const { return active_ && !curvature_.empty() ? curvature_.data() : nullptr; }
+
// Mirror sign vectors for the enabled symmetry axes (always starts with (1,1,1)).
static std::vector<glm::vec3> mirrors(Symmetry sym);
@@ -169,6 +174,7 @@ private:
std::vector<uint32_t> verts_, dabVerts_; // one mirror's vertices; every mirror's (for the normals)
std::vector<uint32_t> dabLeaves_; // the BVH leaves every mirror's query listed (the normals' set)
+ std::vector<float> curvature_; // strokeCurvature
std::vector<glm::vec3> scratch_;
std::vector<float> weights_, influence_;
std::vector<uint8_t> firstTouch_;
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index b67be0a8..c5c5f883 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -65,15 +65,23 @@ 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);
+// The falloff and its first two derivatives in the distance (brushFalloff with Focal Shift).
+vec3 ghostFalloff(float d, float r) {
+ float u = clamp(1.0 - d / r, 0.0, 1.0);
+ if (u <= 0.0) return vec3(0.0);
+ if (uGhostFocal == 0.0) // (the plain smoothstep: t = u)
+ return vec3(u * u * (3.0 - 2.0 * u), -6.0 * u * (1.0 - u) / r, (6.0 - 12.0 * u) / (r * r));
+ float k = exp2(2.0 * clamp(uGhostFocal, -1.0, 1.0));
+ float t = pow(u, k), dt = -k * t / (u * r), ddt = k * (k - 1.0) * t / (u * u * r * r);
+ return vec3(t * t * (3.0 - 2.0 * t), 6.0 * t * (1.0 - t) * dt, (6.0 - 12.0 * t) * dt * dt + 6.0 * t * (1.0 - t) * ddt);
}
vec3 onPlane(vec3 v, vec3 n) { return v - n * dot(v, n); }
-vec3 ghostMove(vec3 x, vec3 n, int first, int end) {
+// Moves x as the dabs [first, end) would, and follows what the moves do to the surface seen as a height field
+// over the vertex's tangent plane (normal n): the height's gradient g (in that plane) and its Laplacian lap
+// (forward differentiation of each kernel's height change; lateral moves don't count). The shading normal is
+// then n - g, and the cavity term changes by the edge length over four times lap (curvatureAt).
+vec3 ghostMove(vec3 x, vec3 n, int first, int end, inout vec3 g, inout float lap) {
float sgn = uGhostKind.y != 0 ? -1.0 : 1.0;
int kind = uGhostKind.x;
for (int i = first; i < end; ++i) {
@@ -82,30 +90,64 @@ vec3 ghostMove(vec3 x, vec3 n, int first, int end) {
float dist = length(x - c);
if (dist >= r) continue;
float w = uGhostC[i].w;
- float f = ghostFalloff(dist, r) * w;
+ vec3 F = ghostFalloff(dist, r) * w; // f, f', f'' in the distance
+ float f = F.x;
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) {
+ vec3 radial = onPlane(x - c, n);
+ float rl = length(radial);
+ radial = rl > 1e-12 ? radial / rl : vec3(0.0);
+ float inv = 1.0 / max(dist, r * 1e-3);
+ float cn = dot(an, n); // the kernel moves along an: the height changes by cn per unit
+ vec3 dh = onPlane(an, n) + cn * g; // gradient of dot(x, an) over the surface
+ float radialLap = F.z + F.y * inv; // Laplacian of f
+ if (kind == 1 || kind == 2) { // Standard (along the area normal), Inflate (along the vertex's)
+ float a = sgn * s * r * (kind == 1 ? 0.06 : 0.05);
+ float k = kind == 1 ? cn : 1.0;
+ x += (kind == 1 ? an : n) * (a * f);
+ g += radial * (k * a * F.y);
+ lap += k * a * radialLap;
+ } else if (kind == 3 || kind == 4) { // Clay, Clay Buildup: everything below a plane pulled up to it
+ float h = dot(x - (ac + an * (sgn * r * (kind == 3 ? 0.05 : 0.08) * s)), an);
+ if (sgn * h < 0.0) {
+ vec3 W = F; // the pull's profile and its derivatives
+ if (kind == 4) {
+ float e = clamp((1.0 - dist / r) / 0.35, 0.0, 1.0), de = e > 0.0 && e < 1.0 ? -1.0 / (0.35 * r) : 0.0;
+ W = vec3(e * e * (3.0 - 2.0 * e), 6.0 * e * (1.0 - e) * de, (6.0 - 12.0 * e) * de * de) * w;
+ }
+ x -= an * (h * W.x);
+ lap -= cn * (2.0 * W.y * dot(radial, dh) + W.x * cn * lap + h * (W.z + W.y * inv));
+ g -= cn * (W.x * dh + h * W.y * radial);
+ }
+ } else if (kind == 5) { // Flatten
+ float m = s * (uGhostKind.y != 0 ? -0.5 : 1.0);
+ float h = dot(x - ac, an);
+ x -= an * (h * m * f);
+ lap -= cn * m * (2.0 * F.y * dot(radial, dh) + f * cn * lap + h * radialLap);
+ g -= cn * m * (f * dh + h * F.y * radial);
+ } else if (kind == 6) { // Crease: a valley of f^2, the sides pinched in
+ float a = -sgn * s * r * 0.05;
+ x += an * (a * f * f) + onPlane(c - x, an) * (0.25 * s * f);
+ g += radial * (cn * a * 2.0 * f * F.y);
+ lap += cn * a * (2.0 * (F.y * F.y + f * F.z) + 2.0 * f * F.y * inv);
+ } else if (kind == 7) { // Pinch: sideways only
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) {
+ } else if (kind == 8) { // Dam Standard: f^4, the sides pinched in
+ float a = -sgn * s * r * 0.08;
+ float f3 = f * f * f;
+ x += an * (a * f3 * f) + onPlane(c - x, an) * (0.3 * s * f);
+ g += radial * (cn * a * 4.0 * f3 * F.y);
+ lap += cn * a * (12.0 * f * f * F.y * F.y + 4.0 * f3 * F.z + 4.0 * f3 * F.y * inv);
+ } else if (kind == 9) { // Trim: what sticks out above a plane cut off
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)));
+ if (sgn * h > 0.0) {
+ float q = sqrt(max(f, 0.0)) * (0.5 + s);
+ float m = min(1.0, q), dm = q < 1.0 && f > 1e-6 ? (0.5 + s) * F.y / (2.0 * sqrt(f)) : 0.0;
+ x -= an * (h * m);
+ lap -= cn * (2.0 * dm * dot(radial, dh) + m * cn * lap + h * dm * inv);
+ g -= cn * (m * dh + h * dm * radial);
+ }
}
}
return x;
@@ -118,26 +160,15 @@ void main() {
float curvature = float(int(aNormal >> 22) - 512) / 511.0; // sign(c) sqrt(|c|), render/Clusters.h
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);
+ // The moved position, normal and cavity term (one evaluation per vertex, no extra varyings: a tiled GPU
+ // writes every vertex's outputs to memory and reads them back).
+ for (int k = 0; k < uGhostGroups; ++k) {
+ if (distance(world, uGhostBound[k].xyz) >= uGhostBound[k].w) continue;
+ vec3 g = vec3(0.0);
+ float lap = 0.0;
+ world = ghostMove(world, normal, uGhostRange[k].x, uGhostRange[k].y, g, lap);
+ normal = normalize(normal - g);
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);
@@ -634,7 +665,6 @@ const Renderer::MeshProgram* Renderer::meshProgram(int features) {
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");
@@ -916,21 +946,8 @@ void Renderer::drawSplats(const glm::mat4& view, const glm::mat4& proj, int fbWi
namespace {
// Signed curvature at vertex `v`: how far the neighbour average sits above (crease, > 0) or below (ridge, < 0)
-// the surface, relative to the local edge length.
-float curvatureAt(const Mesh& mesh, uint32_t v) {
- const auto ring = mesh.neighbors(v);
- if (ring.empty()) return 0.0f;
- glm::vec3 avg(0.0f);
- float edge = 0.0f;
- const glm::vec3 p = mesh.positions[v];
- for (uint32_t n : ring) {
- avg += mesh.positions[n];
- edge += glm::distance(mesh.positions[n], p);
- }
- avg /= float(ring.size());
- edge /= float(ring.size());
- return edge > 0.0f ? glm::dot(mesh.normals[v], avg - p) / edge : 0.0f;
-}
+// the surface, relative to the local edge length (Mesh::curvatureAt).
+float curvatureAt(const Mesh& mesh, uint32_t v) { return mesh.curvatureAt(v); }
using Runs = std::vector<std::pair<uint32_t, uint32_t>>;
@@ -1082,6 +1099,7 @@ void Renderer::uploadVertices(MeshSlot& s, const Mesh& mesh, bool full) {
// Per block, hashes of what it last sent (positions; normals and curvature): a block a dab marked but didn't
// change (paint, mask) isn't sent again, and a cluster whose positions didn't move keeps its bounds and proxy.
s.blockHash.resize(nb * 2, 0);
+ s.blockBox.resize(nb);
blockChanged_.assign(nb, 0);
blockMoved_.assign(nb, 0);
bool everything = full;
@@ -1089,12 +1107,15 @@ void Renderer::uploadVertices(MeshSlot& s, const Mesh& mesh, bool full) {
PackedVertex* out = static_cast<PackedVertex*>(dst);
bool inside = true;
uint64_t hp = 0, hn = 0;
+ AABB box;
for (uint32_t i = b; i < e; ++i) {
const uint32_t v = order[i];
PackedVertex pv{}; // packed here and stored once: `out` may be mapped GPU memory, slow to read back
- if (v < nv) // (else a vertex dynamic topology removed, which only patched clusters named)
+ if (v < nv) { // (else a vertex dynamic topology removed, which only patched clusters named)
inside &= packVertex(s.grid, mesh.positions[v], normals ? mesh.normals[v] : glm::vec3(0.0f, 0.0f, 1.0f),
curvatureAt(mesh, v), pv);
+ box.expand(mesh.positions[v]);
+ }
out[i - b] = pv;
hp = (hp ^ (uint64_t(pv.pos[0]) | uint64_t(pv.pos[1]) << 32)) * 0x100000001B3ull + i;
hn = (hn ^ pv.normal) * 0x100000001B3ull + i;
@@ -1104,7 +1125,9 @@ void Renderer::uploadVertices(MeshSlot& s, const Mesh& mesh, bool full) {
blockChanged_[g] = blockMoved_[g] || s.blockHash[g * 2 + 1] != hn;
s.blockHash[g * 2] = hp;
s.blockHash[g * 2 + 1] = hn;
+ s.blockBox[g] = box;
hp = hn = 0;
+ box = AABB();
}
}
if (!inside) outside.store(true, std::memory_order_relaxed);
@@ -2336,13 +2359,14 @@ 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;
+ // (Not through the textured program's geometry shader: those meshes are sculpted without the preview.)
+ const bool ghost = active && s.id == ghostId_ && !ghost_.empty() && !settings.motionProxy && !textured && !normalMapped;
const int features = (s.hasColors || s.hasMask ? kPaint : 0) | (groups == 1 ? kPrim : 0) | (textured ? kTextured : 0) |
- (normalMapped ? kNormalMap : 0) | (flat ? kFlat : 0) | (ghost ? kGhost : 0);
+ (normalMapped ? kNormalMap : 0) | (flat ? kFlat : 0);
const MeshProgram* p = meshProgram(features);
if (!p) return;
+ const auto bind = [&](const MeshProgram* p) {
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));
@@ -2381,6 +2405,8 @@ void Renderer::drawSlot(const MeshSlot& s, bool active, bool flat, const RenderS
gl::Uniform1i(p->groups, groups);
gl::Uniform1i(p->dim, !active && settings.dimInactive ? 1 : 0);
gl::Uniform1i(p->twoSided, 1);
+ };
+ bind(p);
gl::BindVertexArray(s.vao);
// Constant values for the attribute arrays that are disabled.
if (!s.hasColors) gl::VertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
@@ -2391,7 +2417,30 @@ void Renderer::drawSlot(const MeshSlot& s, bool active, bool flat, const RenderS
cull(s, frameCull_, settings.motionProxy && !perTriangle && !(active && settings.wireframe));
// The back faces left in clusters along the silhouette: the GPU skips them too when they can't show.
if (cullBackfaces_) gl::Enable(GL_CULL_FACE);
+ // The predictive preview (render/Ghost.h): the clusters a ghost dab reaches draw with the GHOST program, the
+ // rest as usual (most of the mesh, which then doesn't pay for the preview).
+ ghostClusters_.clear();
+ if (ghost) {
+ size_t kept = 0;
+ for (uint32_t c : drawClusters_) {
+ const ClusterBounds& b = s.bounds[c];
+ bool reached = b.radius < 0.0f;
+ for (size_t i = 0; i < ghost_.dabs.size() && !reached; ++i)
+ reached = glm::distance(b.center, ghost_.dabs[i].center) < b.radius + ghost_.dabs[i].radius * 1.05f;
+ if (reached) ghostClusters_.push_back(c);
+ else drawClusters_[kept++] = c;
+ }
+ drawClusters_.resize(kept);
+ }
drawClusters(s, drawClusters_, GL_TRIANGLES, Part::Triangles, perTriangle ? p->primBase : -1);
+ if (!ghostClusters_.empty()) {
+ if (const MeshProgram* pg = meshProgram(features | kGhost)) {
+ bind(pg);
+ setGhostUniforms(*pg);
+ drawClusters(s, ghostClusters_, GL_TRIANGLES, Part::Triangles, perTriangle ? pg->primBase : -1);
+ bind(p);
+ }
+ }
if (!s.patches.empty() && s.patchVao) { // dynamic topology: what changed since the layout (in full while moving)
gl::BindVertexArray(s.patchVao);
if (!s.patchColorsOn) gl::VertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);
@@ -2432,14 +2481,12 @@ void Renderer::setGhostUniforms(const MeshProgram& p) {
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;
@@ -2465,17 +2512,18 @@ void Renderer::setGhostUniforms(const MeshProgram& p) {
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) {
+bool Renderer::prepareStrokeUpload(uint32_t id, Mesh& mesh, StrokeUpload& out, const float* curvature) {
out.id = id;
out.ready = false;
out.data.clear();
out.ranges.clear();
out.movedClusters.clear();
out.bounds.clear();
+ out.packMs = out.boundsMs = 0.0;
+ const auto clock = std::chrono::steady_clock::now();
MeshSlot* found = nullptr;
for (MeshSlot& s : slots_)
if (s.id == id) found = &s;
@@ -2518,16 +2566,22 @@ bool Renderer::prepareStrokeUpload(uint32_t id, Mesh& mesh, StrokeUpload& out) {
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)
+ std::vector<AABB> boxes(nb);
+ s.blockBox.resize(nb);
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;
+ AABB box;
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);
+ if (v < nv) {
+ inside &= packVertex(s.grid, mesh.positions[v], mesh.normals[v], curvature ? curvature[v] : curvatureAt(mesh, v), pv);
+ box.expand(mesh.positions[v]);
+ }
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;
@@ -2537,7 +2591,9 @@ bool Renderer::prepareStrokeUpload(uint32_t id, Mesh& mesh, StrokeUpload& out) {
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;
+ boxes[g] = box;
hp = hn = 0;
+ box = AABB();
}
}
if (!inside) outside.store(true, std::memory_order_relaxed);
@@ -2547,11 +2603,13 @@ bool Renderer::prepareStrokeUpload(uint32_t id, Mesh& mesh, StrokeUpload& out) {
s.regrid = true; // the next syncScene makes a new grid and sends everything (the mesh stays dirty)
return false;
}
+ out.packMs = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - clock).count();
// 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;
+ s.blockBox[g] = boxes[g];
if (out.blockChanged[g]) {
s.blockHash[g * 2] = hashes[g * 2];
s.blockHash[g * 2 + 1] = hashes[g * 2 + 1];
@@ -2570,11 +2628,28 @@ bool Renderer::prepareStrokeUpload(uint32_t id, Mesh& mesh, StrokeUpload& out) {
const uint32_t c = s.order.clusterOfSlot(g << UploadOrder::kBlockShift);
if (out.movedClusters.empty() || c > out.movedClusters.back()) out.movedClusters.push_back(c);
}
+ // Their quick bounds: a sphere round their blocks' boxes, no normal cone (never culled as facing away), the
+ // volume as it was (a stroke doesn't turn the mesh inside out); refineStrokeBounds makes them exact.
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);
+ const auto layout = s.order.clusters();
+ for (size_t i = 0; i < out.movedClusters.size(); ++i) {
+ const uint32_t c = out.movedClusters[i];
+ AABB box;
+ for (uint32_t g = layout[c].firstVertex >> UploadOrder::kBlockShift,
+ ge = (layout[c].firstVertex + layout[c].vertexCount + UploadOrder::kBlock - 1) >> UploadOrder::kBlockShift;
+ g < ge && g < nb; ++g)
+ if (s.blockBox[g].valid()) box.expand(s.blockBox[g]);
+ ClusterBounds& cb = out.bounds[i];
+ cb = ClusterBounds();
+ if (box.valid()) {
+ cb.center = box.center();
+ cb.radius = glm::length(box.max - box.min) * 0.5f;
+ }
+ cb.coneAngle = -1.0f;
+ cb.volume = c < s.bounds.size() ? s.bounds[c].volume : 0.0;
+ }
mesh.clearDirty();
+ out.boundsMs = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - clock).count() - out.packMs;
out.ready = true;
return true;
}
@@ -2601,6 +2676,7 @@ void Renderer::commitStrokeUpload(StrokeUpload& up) {
s.bounds[up.movedClusters[i]] = up.bounds[i];
s.proxyStale[up.movedClusters[i]] = 1;
}
+ s.looseClusters.insert(s.looseClusters.end(), up.movedClusters.begin(), up.movedClusters.end());
s.box = AABB();
s.volume = 0.0;
for (const ClusterBounds& b : s.bounds) {
@@ -2613,6 +2689,35 @@ void Renderer::commitStrokeUpload(StrokeUpload& up) {
}
}
+void Renderer::refineStrokeBounds(uint32_t id, const Mesh& mesh) {
+ MeshSlot* found = nullptr;
+ for (MeshSlot& s : slots_)
+ if (s.id == id) found = &s;
+ if (!found || found->looseClusters.empty()) return;
+ MeshSlot& s = *found;
+ std::sort(s.looseClusters.begin(), s.looseClusters.end());
+ s.looseClusters.erase(std::unique(s.looseClusters.begin(), s.looseClusters.end()), s.looseClusters.end());
+ const size_t nc = s.order.clusters().size();
+ if (s.fresh || mesh.topologyDirty || s.bounds.size() != nc) { // (a full upload since makes them anyway)
+ s.looseClusters.clear();
+ return;
+ }
+ poolFor(s.looseClusters.size(), [&](size_t i0, size_t i1) {
+ for (size_t i = i0; i < i1; ++i)
+ if (s.looseClusters[i] < nc) s.bounds[s.looseClusters[i]] = clusterBounds(mesh, s.order, s.looseClusters[i]);
+ }, 1, 2);
+ s.looseClusters.clear();
+ s.volume = 0.0;
+ s.box = AABB();
+ 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)
// ---------------------------------------------------------------------------
diff --git a/src/render/Renderer.h b/src/render/Renderer.h
index f9ce7d0d..08301e65 100644
--- a/src/render/Renderer.h
+++ b/src/render/Renderer.h
@@ -128,10 +128,17 @@ public:
std::vector<uint32_t> movedClusters;
std::vector<ClusterBounds> bounds; // of movedClusters
std::vector<uint8_t> blockChanged, blockMoved; // scratch, per block
+ double packMs = 0.0, boundsMs = 0.0; // where prepare's time went
size_t bytes() const { return data.size() * sizeof(PackedVertex); }
};
- bool prepareStrokeUpload(uint32_t id, Mesh& mesh, StrokeUpload& out);
+ // `curvature` (optional, by vertex): the cavity term of every vertex the mesh marked dirty, already worked
+ // out (Sculptor::strokeCurvature).
+ bool prepareStrokeUpload(uint32_t id, Mesh& mesh, StrokeUpload& out, const float* curvature = nullptr);
void commitStrokeUpload(StrokeUpload& up);
+ // The stroke uploads gave the clusters they moved quick culling bounds (around their vertices' boxes, which
+ // packing finds anyway, without the normal cone, so they are never culled as facing away); at the stroke's
+ // end this works out the exact ones (GL thread, the mesh no longer changing).
+ void refineStrokeBounds(uint32_t id, const Mesh& mesh);
// Bytes of GPU buffers and textures the mesh slots hold.
size_t gpuBytes() const;
@@ -210,6 +217,10 @@ private:
size_t drawnTriangles = 0;
size_t vertexCapacity = 0;
std::vector<uint64_t> blockHash; // per UploadOrder block: hashes of its packed positions, and the rest
+ std::vector<AABB> blockBox; // per UploadOrder block: its vertices' box as last packed
+ // Clusters whose culling bounds a stroke upload set from blockBox (a sphere round the box, no normal cone,
+ // the old volume): refineStrokeBounds makes them exact.
+ std::vector<uint32_t> looseClusters;
size_t bytes[8] = {}; // GPU sizes: vertices, indices, lines, colours, mask, groups, prims, uvs + tangents
// Dynamic topology (Mesh::changedTriangles, core/Dyntopo.h): the clusters whose triangles changed since
@@ -283,6 +294,7 @@ private:
std::vector<const void*> drawOffsets_;
std::vector<GLint> drawBases_;
std::vector<uint32_t> drawClusters_;
+ std::vector<uint32_t> ghostClusters_; // drawSlot: the clusters a ghost dab reaches
bool cullBackfaces_ = false; // the last cull(): back faces can't show (so the GPU may skip them too)
std::vector<uint32_t> cullScratch_;
size_t lastDrawn_ = 0;
@@ -296,7 +308,7 @@ 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;
+ GLint ghostGroups = -1, ghostRange = -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, kGhost = 32;
MeshProgram programs_[64];
diff --git a/tests/test_ghost.cpp b/tests/test_ghost.cpp
index 4eabb687..d9f6ff66 100644
--- a/tests/test_ghost.cpp
+++ b/tests/test_ghost.cpp
@@ -1,8 +1,8 @@
// 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).
+// The predictive stroke preview (render/Ghost.h) against the brush solver it stands in for, the cavity term the
+// normals pass hands the renderer (Mesh::curvatureAt), and the pen prediction (core/PenPredictor.h).
#include "TestHarness.h"
#include "core/PenPredictor.h"
@@ -125,6 +125,51 @@ TEST(ghost_kinds_cover_the_displacement_brushes) {
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(curvature_from_triangles_matches_the_ring_average) {
+ // Mesh::curvatureAt (the renderer's cavity term) from each vertex's triangles is the old average over its
+ // ring on a closed manifold; the normals pass that writes it alongside gives the same numbers and normals.
+ Mesh m = makeIcosphere(4);
+ for (size_t v = 0; v < m.positions.size(); ++v)
+ m.positions[v] *= 1.0f + 0.05f * std::sin(float(v) * 1.7f);
+ m.recomputeAllNormals();
+ float worst = 0.0f;
+ for (uint32_t v = 0; v < m.vertexCount(); ++v) {
+ glm::vec3 avg(0.0f);
+ float edge = 0.0f;
+ const auto ring = m.neighbors(v);
+ for (uint32_t n : ring) {
+ avg += m.positions[n];
+ edge += glm::distance(m.positions[n], m.positions[v]);
+ }
+ avg /= float(ring.size());
+ edge /= float(ring.size());
+ const float ringCurvature = glm::dot(m.normals[v], avg - m.positions[v]) / edge;
+ worst = std::max(worst, std::abs(ringCurvature - m.curvatureAt(v)));
+ }
+ CHECK(worst < 1e-4f);
+
+ Mesh a = m, b = m;
+ std::vector<uint32_t> moved;
+ for (uint32_t v = 0; v < a.vertexCount(); v += 7) {
+ a.positions[v] *= 1.02f;
+ b.positions[v] *= 1.02f;
+ moved.push_back(v);
+ }
+ std::vector<float> curvature(a.vertexCount(), -99.0f);
+ a.updateNormals(moved, curvature.data());
+ b.updateNormals(moved);
+ CHECK(a.normals == b.normals);
+ float off = 0.0f;
+ size_t written = 0;
+ for (uint32_t v = 0; v < a.vertexCount(); ++v) {
+ if (curvature[v] == -99.0f) continue;
+ ++written;
+ off = std::max(off, std::abs(curvature[v] - a.curvatureAt(v)));
+ }
+ CHECK(written >= moved.size());
+ CHECK(off < 1e-5f);
+}
+
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.
diff --git a/tests/ui/performance.sfui b/tests/ui/performance.sfui
index 6ad74a03..a9d7277e 100644
--- a/tests/ui/performance.sfui
+++ b/tests/ui/performance.sfui
@@ -18,8 +18,18 @@ hover "Fast Navigation"
expect_help "10 million triangles"
hover "Multithreading##perf.mt"
expect_help "every core"
+hover "Predictive Strokes"
+expect_help "exactly the same"
hold none
+# Predictive Strokes: on by default, off and on again.
+expect_value predictive_strokes == 1
+click "Predictive Strokes"
+expect_value predictive_strokes == 0
+expect_status "Predictive strokes off"
+click "Predictive Strokes"
+expect_value predictive_strokes == 1
+
# Antialiasing off and back; 8x.
click "AA Off##perf.aa_1"
expect_value antialiasing == 1
diff --git a/tools/bench_strokes.sh b/tools/bench_strokes.sh
index 75b7ef8c..c5d5c0c2 100644
--- a/tools/bench_strokes.sh
+++ b/tools/bench_strokes.sh
@@ -8,6 +8,8 @@
#
# tools/bench_strokes.sh [options] BINARY [BINARY_B]
#
+# A binary may carry its own environment: VAR=VAL[,VAR=VAL...]@BINARY (e.g. SF_PREDICTIVE_STROKE=0@build/sculptforge
+# against build/sculptforge compares predictive strokes off and on with one build).
# -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)
@@ -30,11 +32,11 @@ while getopts "s:n:r:t:e:h" opt; do
r) radius=$OPTARG ;;
t) seconds=$OPTARG ;;
e) envs+=("$OPTARG") ;;
- *) sed -n '5,18p' "$0"; exit 1 ;;
+ *) sed -n "5,21p" "$0"; exit 1 ;;
esac
done
shift $((OPTIND - 1))
-[ $# -ge 1 ] || { sed -n '5,18p' "$0"; exit 1; }
+[ $# -ge 1 ] || { sed -n "5,21p" "$0"; exit 1; }
bins=("$@")
scene_for() {
@@ -54,10 +56,15 @@ for size in "${sizeList[@]}"; do
for ((i = 1; i <= runs; i++)); do
for b in "${!bins[@]}"; do
bin=${bins[$b]}
+ own=()
+ if [[ $bin == *@* ]]; then
+ IFS=, read -ra own <<<"${bin%%@*}"
+ bin=${bin#*@}
+ fi
label=$([ "$b" = 0 ] && echo A || echo B)
- out=$(env ${envs[@]+"${envs[@]}"} "$bin" --run "$scene" --measure-stroke "$seconds,$radius" 2>&1 || true)
+ out=$(env ${envs[@]+"${envs[@]}"} ${own[@]+"${own[@]}"} "$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)"
+ echo "== ${size}M $label run $i (${bins[$b]})"
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"