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.

Micro levels on dense bases (scans): mark only the patches a dab changed (the kernel's or the stitch's), cluster bounds by the displacement alone when the base didn't move, no sorts of millions of dirty patches in upload or stroke shading

islandmed0b2d89b810a7
diff --git a/src/core/Micro.h b/src/core/Micro.h
index 868b16d5..1c001456 100644
--- a/src/core/Micro.h
+++ b/src/core/Micro.h
@@ -302,9 +302,13 @@ public:
uint64_t points = 0; // grid points visited
private:
+ std::vector<uint8_t> changed_; // per candidate of the last dab: the kernel changed its grid
+ std::vector<uint32_t> stitchStamp_; // per base triangle: dabEpoch_ when the stitch changed a point of it
void touch(MicroMesh& mm, std::span<const uint32_t> tris);
void widen(MicroMesh& mm, float need); // MicroMesh::widen, keeping this stroke's saved tiles in step
- void stitch(const Mesh& m, MicroMesh& mm, std::span<const uint32_t> tris);
+ // `changed` (per entry of tris, or null: all): the triangles the kernel wrote; only they and those the stitch
+ // changed are marked dirty (the renderer re-sends, re-bounds and re-shades what's marked).
+ void stitch(const Mesh& m, MicroMesh& mm, std::span<const uint32_t> tris, const uint8_t* changed = nullptr);
bool active_ = false;
float strokeStep_ = 0.0f;
std::vector<uint32_t> tileOf_; // per base triangle: its undo tile + 1 (0 = not yet touched)
diff --git a/src/core/MicroSculpt.cpp b/src/core/MicroSculpt.cpp
index e2fc4051..ffe999ca 100644
--- a/src/core/MicroSculpt.cpp
+++ b/src/core/MicroSculpt.cpp
@@ -118,6 +118,7 @@ void MicroStroke::begin(Mesh& m) {
maskAdded_ = false;
if (dabStamp_.size() != mm.baseTriangles) {
dabStamp_.assign(mm.baseTriangles, 0);
+ stitchStamp_.assign(mm.baseTriangles, 0);
dabEpoch_ = 0;
}
if (vertStamp_.size() != m.vertexCount()) vertStamp_.assign(m.vertexCount(), 0);
@@ -153,10 +154,12 @@ void MicroStroke::touch(MicroMesh& mm, std::span<const uint32_t> tris) {
}
// Makes the copies of every shared point of triangles `tris` equal again (see the file comment).
-void MicroStroke::stitch(const Mesh& m, MicroMesh& mm, std::span<const uint32_t> tris) {
+void MicroStroke::stitch(const Mesh& m, MicroMesh& mm, std::span<const uint32_t> tris, const uint8_t* changed) {
const uint32_t n = mm.n, S = mm.samples;
const uint32_t epoch = dabEpoch_;
auto inDab = [&](uint32_t t) { return dabStamp_[t] == epoch; };
+ if (stitchStamp_.size() != mm.baseTriangles) stitchStamp_.assign(mm.baseTriangles, 0);
+ auto hit = [&](uint32_t t) { std::atomic_ref<uint32_t>(stitchStamp_[t]).store(epoch, std::memory_order_relaxed); };
// Edges: each shared edge once, by the lower triangle of the two (both in this dab), or copied from a hidden
// neighbour (which brushes leave alone).
poolFor(tris.size(), [&](size_t b, size_t e) {
@@ -181,12 +184,16 @@ void MicroStroke::stitch(const Mesh& m, MicroMesh& mm, std::span<const uint32_t>
MicroMesh::edgePoint(f, reversed ? n - k : k, n, i2, j2);
const uint32_t a = MicroMesh::index(i, j, n), c = MicroMesh::index(i2, j2, n);
if (hiddenU) {
+ if (g[a] != h[c] || (mk && mk[a] != hk[c])) hit(t);
g[a] = h[c];
if (mk) mk[a] = hk[c];
} else {
const int16_t v = int16_t((int(g[a]) + int(h[c]) + (int(g[a]) + int(h[c]) >= 0 ? 1 : 0)) / 2);
+ const uint8_t mv = mk ? uint8_t((int(mk[a]) + int(hk[c]) + 1) / 2) : 0;
+ if (g[a] != v || (mk && mk[a] != mv)) hit(t);
+ if (h[c] != v || (mk && hk[c] != mv)) hit(u);
g[a] = h[c] = v;
- if (mk) mk[a] = hk[c] = uint8_t((int(mk[a]) + int(hk[c]) + 1) / 2);
+ if (mk) mk[a] = hk[c] = mv;
}
}
}
@@ -238,6 +245,7 @@ void MicroStroke::stitch(const Mesh& m, MicroMesh& mm, std::span<const uint32_t>
for (uint32_t u : ring) {
if (!inDab(u)) continue;
const size_t ci = cornerIndex(u);
+ if (mm.disp[ci] != value || (mm.hasMask() && mm.mask[ci] != mval)) hit(u);
mm.disp[ci] = value;
if (mm.hasMask()) mm.mask[ci] = mval;
}
@@ -259,7 +267,9 @@ void MicroStroke::stitch(const Mesh& m, MicroMesh& mm, std::span<const uint32_t>
mm.triRange[t] = uint16_t(std::min(r, 32767));
}
}, 64);
- for (uint32_t t : tris) {
+ for (size_t s = 0; s < tris.size(); ++s) {
+ const uint32_t t = tris[s];
+ if (changed && !changed[s] && stitchStamp_[t] != epoch) continue; // (nothing of it moved)
mm.rangeMax = std::max(mm.rangeMax, mm.triRange[t]);
mm.markDirty(t);
}
@@ -288,6 +298,7 @@ void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3&
// 1. The base triangles within reach.
if (++dabEpoch_ == 0) {
std::fill(dabStamp_.begin(), dabStamp_.end(), 0);
+ std::fill(stitchStamp_.begin(), stitchStamp_.end(), 0);
std::fill(vertStamp_.begin(), vertStamp_.end(), 0);
dabEpoch_ = 1;
}
@@ -523,7 +534,7 @@ void MicroStroke::dab(Mesh& m, const BVH& bvh, BrushType type, const glm::vec3&
for (float c : clipped) over = std::max(over, c);
// 4. Stitch the shared points.
- stitch(m, mm, cand_);
+ stitch(m, mm, cand_, maskBrush ? nullptr : changed_.data());
if (over > 0.0f) widen(mm, over * 1.5f);
stitchMs += lapMs(clock);
}
diff --git a/src/render/MicroRenderer.cpp b/src/render/MicroRenderer.cpp
index c2f0a077..ad065bc8 100644
--- a/src/render/MicroRenderer.cpp
+++ b/src/render/MicroRenderer.cpp
@@ -275,6 +275,8 @@ void MicroRenderer::buildLayout(const Mesh& mesh) {
closed_ = closedOriented(mesh);
bounds_.assign(clusters, ClusterBounds());
clusterEdge_.assign(clusters, 0.0f);
+ clusterRadius_.assign(clusters, 0.0f);
+ clusterAngle_.assign(clusters, 0.0f);
std::vector<uint32_t> all(clusters);
for (uint32_t c = 0; c < clusters; ++c) all[c] = c;
updateClusterBounds(mesh, all);
@@ -343,11 +345,30 @@ void MicroRenderer::updateClusterBounds(const Mesh& mesh, const std::vector<uint
const float angle = std::acos(std::clamp(cosMin, -1.0f, 1.0f)) + 0.5f;
cb.coneAngle = angle < 1.45f && float(range) * mm.step < radius * 0.5f ? angle : -1.0f;
bounds_[c] = cb;
+ clusterRadius_[c] = radius;
+ clusterAngle_[c] = angle;
clusterEdge_[c] = edge / float(3 * (s1 - s0)) / float(mm.n) * float(mm.n); // (base edge)
}
}, 16);
}
+void MicroRenderer::updateClusterRanges(const Mesh& mesh, const std::vector<uint32_t>& clusters) {
+ const MicroMesh& mm = *mesh.micro;
+ const uint32_t nt = mm.baseTriangles, size = 1u << clusterShift_;
+ poolFor(clusters.size(), [&](size_t b, size_t e) {
+ for (size_t ci = b; ci < e; ++ci) {
+ const uint32_t c = clusters[ci];
+ const uint32_t s0 = c << clusterShift_, s1 = std::min(nt, s0 + size);
+ uint16_t range = 0;
+ for (uint32_t s = s0; s < s1; ++s) range = std::max(range, mm.triRange[order_[s]]);
+ ClusterBounds& cb = bounds_[c];
+ const float radius = clusterRadius_[c], angle = clusterAngle_[c];
+ cb.radius = radius + mm.bulgeMax + float(range) * mm.step;
+ cb.coneAngle = angle < 1.45f && float(range) * mm.step < radius * 0.5f ? angle : -1.0f;
+ }
+ }, 64);
+}
+
void MicroRenderer::uploadDispTiles(const Mesh& mesh, std::vector<uint32_t>& slots, bool all) {
const MicroMesh& mm = *mesh.micro;
const size_t S = mm.samples;
@@ -366,7 +387,17 @@ void MicroRenderer::uploadDispTiles(const Mesh& mesh, std::vector<uint32_t>& slo
return;
}
if (slots.empty()) return;
- std::sort(slots.begin(), slots.end());
+ if (slots.size() > 4096) { // (in slot order through a bitset: sorting millions of tiny patches cost more than sending them)
+ slotBits_.assign((size_t(baseTriangles_) + 63) / 64, 0);
+ for (uint32_t sl : slots) slotBits_[sl >> 6] |= uint64_t(1) << (sl & 63);
+ size_t k = 0;
+ for (size_t w = 0; w < slotBits_.size(); ++w)
+ for (uint64_t bits = slotBits_[w]; bits; bits &= bits - 1) slots[k++] = uint32_t(w * 64 + size_t(std::countr_zero(bits)));
+ slots.resize(k);
+ } else {
+ std::sort(slots.begin(), slots.end());
+ slots.erase(std::unique(slots.begin(), slots.end()), slots.end());
+ }
staging_.resize(slots.size() * S * 2);
auto* st = reinterpret_cast<int16_t*>(staging_.data());
poolFor(slots.size(), [&](size_t b, size_t e) {
@@ -417,15 +448,19 @@ void MicroRenderer::shadePending(const Mesh& mesh, bool all) {
// The pending triangles, newest first (scratch_ holds this sync's dirty ones, already marked).
std::vector<uint32_t>& seeds = scratch_;
const size_t limit = shadeBudget ? std::max<size_t>(1, shadeBudget / (S * 3)) : SIZE_MAX; // (the ring triples it)
- if (seeds.size() < limit)
+ const bool full = seeds.size() >= limit; // (this sync's dirty ones fill it: sorted and unique already)
+ if (full) seeds.resize(limit);
+ else if (seeds.size() < limit)
for (size_t w = 0; w < pending_.size() && seeds.size() < limit; ++w)
for (uint64_t bits = pending_[w]; bits && seeds.size() < limit; bits &= bits - 1) {
const uint32_t t = uint32_t(w * 64 + size_t(std::countr_zero(bits)));
seeds.push_back(t);
}
- std::sort(seeds.begin(), seeds.end());
- seeds.erase(std::unique(seeds.begin(), seeds.end()), seeds.end());
- if (seeds.size() > limit) seeds.resize(limit);
+ if (!full) {
+ std::sort(seeds.begin(), seeds.end());
+ seeds.erase(std::unique(seeds.begin(), seeds.end()), seeds.end());
+ if (seeds.size() > limit) seeds.resize(limit);
+ }
if (seeds.empty()) return;
const auto started = std::chrono::steady_clock::now();
std::vector<uint32_t> sorted;
@@ -530,10 +565,13 @@ bool MicroRenderer::sync(Mesh& mesh) {
}
if (!scratch_.empty()) { // their clusters may reach further now
std::vector<uint32_t> cl;
- for (uint32_t t : scratch_) cl.push_back(slotOf_[t] >> clusterShift_);
- std::sort(cl.begin(), cl.end());
- cl.erase(std::unique(cl.begin(), cl.end()), cl.end());
- updateClusterBounds(mesh, cl);
+ clusterSeen_.assign(bounds_.size(), 0);
+ for (uint32_t t : scratch_) {
+ const uint32_t c = slotOf_[t] >> clusterShift_;
+ if (!clusterSeen_[c]) clusterSeen_[c] = 1, cl.push_back(c);
+ }
+ if (baseTris.empty() && !baseEverywhere) updateClusterRanges(mesh, cl); // (only the displacement changed)
+ else updateClusterBounds(mesh, cl);
}
if (mm.allDirty || baseEverywhere) {
shadePending(mesh, true);
diff --git a/src/render/MicroRenderer.h b/src/render/MicroRenderer.h
index 771c22cc..5f0bfbdb 100644
--- a/src/render/MicroRenderer.h
+++ b/src/render/MicroRenderer.h
@@ -54,6 +54,8 @@ private:
void buildLayout(const Mesh& mesh);
void uploadBase(const Mesh& mesh);
void updateClusterBounds(const Mesh& mesh, const std::vector<uint32_t>& clusters);
+ // The same after only displacement changed (the base didn't move): the clusters' displacement range only.
+ void updateClusterRanges(const Mesh& mesh, const std::vector<uint32_t>& clusters);
void ensurePatches(int level);
void uploadDispTiles(const Mesh& mesh, std::vector<uint32_t>& slots, bool all);
void shadePending(const Mesh& mesh, bool all);
@@ -69,6 +71,9 @@ private:
uint32_t clusterShift_ = 8;
std::vector<ClusterBounds> bounds_;
std::vector<float> clusterEdge_; // average base edge per cluster
+ // Per cluster, from the base alone: the sphere's radius and the normal cone's angle before the displacement's
+ // share (updateClusterRanges: a micro edit only changes that share).
+ std::vector<float> clusterRadius_, clusterAngle_;
AABB box_;
struct Chunk {
uint32_t firstSlot = 0, slots = 0;
@@ -91,6 +96,8 @@ private:
double shadeNsPerPoint_ = 0.0; // what shading a grid point (and sending it) took lately (TierConfig::strokeShadeMs)
size_t pendingCount_ = 0;
std::vector<uint32_t> scratch_, written_, stageIndex_;
+ std::vector<uint8_t> clusterSeen_;
+ std::vector<uint64_t> slotBits_;
std::vector<uint8_t> staging_;
std::vector<uint32_t> levels_;
std::vector<int8_t> want_;