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.

Fast Big Brushes: carry each vertex's tangents through a batch's dabs (one normal at the end), dirty marks straight into the mesh's bits

islandmed2f66217902bb
diff --git a/src/core/SculptBatch.cpp b/src/core/SculptBatch.cpp
index a3e4ac20..be7ca5d9 100644
--- a/src/core/SculptBatch.cpp
+++ b/src/core/SculptBatch.cpp
@@ -325,22 +325,25 @@ void Sculptor::flushBatch(Mesh& mesh, BVH& bvh) {
for (size_t i = b; i < e; ++i) {
const uint32_t v = g.verts[i];
const glm::vec3 x0 = pos[v];
- glm::vec3 x = x0, nv = normals[v];
+ glm::vec3 x = x0, t1, t2;
bool reached = false;
for (const PlaneKernel* k : ks) {
- if (!k->reaches(x)) continue;
- if (!reached && curvatureStamp_[v] != stamp) { // (the cavity term from before the stroke moved it)
- curvatureStamp_[v] = stamp;
- curvature_[v] = mesh.curvatureAt(v);
+ if (!reached) { // (the first dab that reaches it: its cavity term from before, its tangents)
+ const glm::vec3 o = x - k->center;
+ if (glm::dot(o, o) >= k->r * k->r) continue;
+ if (curvatureStamp_[v] != stamp) {
+ curvatureStamp_[v] = stamp;
+ curvature_[v] = mesh.curvatureAt(v);
+ }
+ PlaneKernel::tangents(normals[v], t1, t2);
+ reached = true;
}
- reached = true;
glm::vec3 d;
- nv = k->offsetAndNormal(x, nv, kexp, d);
- x += d;
+ if (k->offsetAndCarry(x, kexp, d, t1, t2)) x += d;
}
scratch_[i] = x - x0;
givenInfluence_[i] = reached ? 1.0f : 0.0f;
- if (reached) normals[v] = nv;
+ if (reached) normals[v] = PlaneKernel::carriedNormal(t1, t2, normals[v]);
}
}, 2048);
displaceAll(mesh, g.verts, scratch_, false, givenInfluence_.data());
@@ -350,10 +353,30 @@ void Sculptor::flushBatch(Mesh& mesh, BVH& bvh) {
timings.vertices += n;
addLazyLeaves(bvh, g.leaves);
lastLazyLeaves_.insert(lastLazyLeaves_.end(), g.leaves.begin(), g.leaves.end());
- mesh.markDirtyAround(n, 4096, [&](size_t b, size_t e, IndexBits& set) {
- for (size_t i = b; i < e; ++i)
- if (givenInfluence_[i] > 0.0f) set.insertLocal(g.verts[i]);
- });
+ // The GPU's dirty marks of what moved, in parallel straight into the mesh's bits: the list is increasing,
+ // so each stretch of it starts and ends at a dirty block's edge and no two threads share a word.
+ size_t first = 0, last = n;
+ while (first < n && givenInfluence_[first] <= 0.0f) ++first;
+ while (last > first && givenInfluence_[last - 1] <= 0.0f) --last;
+ if (first < last) {
+ const std::vector<uint32_t>& vs = g.verts;
+ mesh.growDirtyBlocks((size_t(vs[last - 1]) >> Mesh::kDirtyBlockShift) + 1);
+ mesh.dirtyBegin = std::min(mesh.dirtyBegin, vs[first]);
+ mesh.dirtyEnd = std::max(mesh.dirtyEnd, vs[last - 1] + 1);
+ const auto block = [&](size_t i) { return vs[i] >> Mesh::kDirtyBlockShift; };
+ poolFor(n, [&](size_t b, size_t e) {
+ size_t i = b, end = e;
+ if (b > 0)
+ while (i < n && block(i) == block(b - 1)) ++i; // (the stretch before has that block)
+ while (end < n && block(end) == block(end - 1)) ++end;
+ for (; i < end; ++i)
+ if (givenInfluence_[i] > 0.0f) {
+ const uint32_t v = vs[i];
+ mesh.dirtyBlocks[v >> Mesh::kDirtyBlockShift] = 1;
+ mesh.dirtyBits[v >> 6] |= uint64_t(1) << (v & 63);
+ }
+ }, 16384);
+ }
timings.normalsMs += lapMs(clock);
}
lazyMesh_ = &mesh;
diff --git a/src/core/SculptKernel.h b/src/core/SculptKernel.h
index 2c1c70f6..716ce299 100644
--- a/src/core/SculptKernel.h
+++ b/src/core/SculptKernel.h
@@ -90,24 +90,27 @@ struct PlaneKernel {
// Inside the dab's reach (the falloff is 0 beyond it).
bool reaches(const glm::vec3& x) const { return brushDistance(x, center, viewDir, viewFalloff) <= r; }
- // The offset at x (into d) and the normal n of the surface there after the move: the tangents carried by the
- // offset's derivative, t + J t (J worked out from the formulas above: one falloff slope, no extra offsets),
- // so n' is the moved surface's normal to first order (exact where the brush's slope is constant). Not with
- // viewFalloff (carriedNormal's finite differences then). `k` = exp2(2 focal), the falloff's exponent.
- glm::vec3 offsetAndNormal(const glm::vec3& x, const glm::vec3& n, float k, glm::vec3& d) const {
+ // The offset at x (into d), and the surface's tangents t1, t2 there carried by the move: t + J t, J the
+ // offset's derivative (worked out from the formulas above: one falloff slope, no extra offsets). Carried
+ // through several dabs in turn, the moved surface's normal is cross(t1, t2) at the end (to first order in each
+ // dab's slope, exact where it is constant). Not with viewFalloff. `k` = exp2(2 focal), the falloff's exponent.
+ // False (d = 0, tangents as they were) outside the dab.
+ bool offsetAndCarry(const glm::vec3& x, float k, glm::vec3& d, glm::vec3& t1, glm::vec3& t2) const {
const glm::vec3 o = x - center;
- const float dist = glm::length(o);
- const float t = std::clamp(1.0f - dist / r, 0.0f, 1.0f);
+ const float dist2 = glm::dot(o, o);
+ if (dist2 >= r * r) {
+ d = glm::vec3(0.0f);
+ return false;
+ }
+ const float dist = std::sqrt(dist2);
+ const float t = 1.0f - dist / r;
const float g = focal != 0.0f ? std::pow(t, k) : t;
const float f = g * g * (3.0f - 2.0f * g);
d = offsetWith(x, f);
- if (t <= 0.0f) return n; // (outside: no move)
// df/d(dist) = df/dg dg/dt dt/d(dist).
const float dg = focal != 0.0f ? (t > 0.0f ? k * g / t : 0.0f) : 1.0f;
- const float slope = dist > 1e-12f && t < 1.0f ? -6.0f * g * (1.0f - g) * dg / r : 0.0f;
+ const float slope = dist > 1e-12f ? -6.0f * g * (1.0f - g) * dg / r : 0.0f;
const glm::vec3 grad = dist > 1e-12f ? o * (slope / dist) : glm::vec3(0.0f); // the falloff's gradient
- const glm::vec3 t1 = glm::normalize(std::abs(n.x) < 0.9f ? glm::cross(n, glm::vec3(1, 0, 0)) : glm::cross(n, glm::vec3(0, 1, 0)));
- const glm::vec3 t2 = glm::cross(n, t1);
const auto carry = [&](const glm::vec3& tv) -> glm::vec3 { // J tv
const float gf = glm::dot(grad, tv);
const glm::vec3 tflat = tv - areaNormal * glm::dot(areaNormal, tv);
@@ -151,10 +154,28 @@ struct PlaneKernel {
return glm::vec3(0.0f);
}
};
- const glm::vec3 m = glm::cross(t1 + carry(t1), t2 + carry(t2));
+ t1 += carry(t1);
+ t2 += carry(t2);
+ return true;
+ }
+ // A tangent pair for the normal n (cross(t1, t2) = n), to carry through offsetAndCarry.
+ static void tangents(const glm::vec3& n, glm::vec3& t1, glm::vec3& t2) {
+ t1 = glm::normalize(std::abs(n.x) < 0.9f ? glm::cross(n, glm::vec3(1, 0, 0)) : glm::cross(n, glm::vec3(0, 1, 0)));
+ t2 = glm::cross(n, t1);
+ }
+ // The normal the carried tangents give, or n where they folded over (or collapsed).
+ static glm::vec3 carriedNormal(const glm::vec3& t1, const glm::vec3& t2, const glm::vec3& n) {
+ const glm::vec3 m = glm::cross(t1, t2);
const float len = glm::length(m);
return len > 1e-20f && glm::dot(m, n) > 0.0f ? m / len : n;
}
+ // One dab: the offset at x and the normal n after it (offsetAndCarry with fresh tangents).
+ glm::vec3 offsetAndNormal(const glm::vec3& x, const glm::vec3& n, float k, glm::vec3& d) const {
+ glm::vec3 t1, t2;
+ tangents(n, t1, t2);
+ if (!offsetAndCarry(x, k, d, t1, t2)) return n;
+ return carriedNormal(t1, t2, n);
+ }
};
} // namespace sf
diff --git a/tests/test_beta_boost.cpp b/tests/test_beta_boost.cpp
index 65b32fea..c56e7ad2 100644
--- a/tests/test_beta_boost.cpp
+++ b/tests/test_beta_boost.cpp
@@ -10,6 +10,7 @@
#include "core/BrushAlpha.h"
#include "core/Sculpt.h"
#include "core/SculptKernel.h"
+#include "core/ThreadPool.h"
#include <chrono>
#include <algorithm>
@@ -253,6 +254,7 @@ TEST(adaptive_dabs_normals_during_the_stroke) {
TEST(adaptive_dabs_timing) {
const char* env = std::getenv("SF_BOOST_TRIS");
if (!env) return; // (a measurement, not a check: SF_BOOST_TRIS=15000000)
+ if (const char* t = std::getenv("SF_BOOST_THREADS")) ThreadPool::setSharedThreadCount(std::atoi(t)); // (before first use)
const Mesh head = makeBenchHead(size_t(std::strtod(env, nullptr)));
const float radius = std::getenv("SF_BOOST_RADIUS") ? float(std::strtod(std::getenv("SF_BOOST_RADIUS"), nullptr)) : 0.7f;
const int batch = std::getenv("SF_BOOST_BATCH") ? std::atoi(std::getenv("SF_BOOST_BATCH")) : 4;