Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
Brushes: vertex sets as bitmaps, listed in index order (BVH query, normal update, refit); dab refits reuse the query's leaves
islandmed6339ed69b39d
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 4fd19ef2..a8776e7a 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -27,6 +27,7 @@ include(cmake/Dependencies.cmake)add_library(sculptforge_core STATICsrc/core/Mesh.cppsrc/core/BVH.cpp+ src/core/IndexBits.cppsrc/core/ThreadPool.cppsrc/core/UploadOrder.cppsrc/core/Benchmark.cppdiff --git a/src/core/BVH.cpp b/src/core/BVH.cppindex 43aee3d1..2863db56 100644--- a/src/core/BVH.cpp+++ b/src/core/BVH.cpp@@ -2,7 +2,6 @@// SPDX-License-Identifier: GPL-3.0-or-later#include "core/BVH.h"-#include "core/AtomicRef.h"#include "core/ThreadPool.h"#include <algorithm>@@ -54,8 +53,8 @@ void BVH::build(const Mesh& mesh) {nodes_.clear();triIndices_.resize(nt);triLeaf_.assign(nt, 0);- vertStamp_.assign(mesh.vertexCount(), 0);- queryEpoch_ = 0;+ vertBits_.resize(mesh.vertexCount());+ queryLeaves_.clear();if (nt == 0) return;std::vector<glm::vec3> centroids(nt);@@ -153,27 +152,23 @@ void BVH::build(const Mesh& mesh) {n.box.expand(nodes_[n.first + 1].box);}}- leafStamp_.assign(nodes_.size(), 0);- leafStampValue_ = 0;+ leafBits_.resize(nodes_.size());}-void BVH::recomputeLeaf(const Mesh& mesh, Node& n) {- n.box = AABB{};+AABB BVH::leafBox(const Mesh& mesh, const Node& n) const {+ AABB box;for (uint32_t i = n.first; i < n.first + n.count; ++i) {const auto& t = mesh.triangles[triIndices_[i]];- n.box.expand(mesh.positions[t.x]);- n.box.expand(mesh.positions[t.y]);- n.box.expand(mesh.positions[t.z]);- triLeaf_[triIndices_[i]] = static_cast<uint32_t>(&n - nodes_.data());+ box.expand(mesh.positions[t.x]);+ box.expand(mesh.positions[t.y]);+ box.expand(mesh.positions[t.z]);}+ return box;}-uint32_t BVH::nextStamp(std::vector<uint32_t>& stamps, uint32_t& stamp) const {- if (++stamp == 0) {- std::fill(stamps.begin(), stamps.end(), 0);- stamp = 1;- }- return stamp;+void BVH::recomputeLeaf(const Mesh& mesh, Node& n) {+ n.box = leafBox(mesh, n);+ for (uint32_t i = n.first; i < n.first + n.count; ++i) triLeaf_[triIndices_[i]] = static_cast<uint32_t>(&n - nodes_.data());}bool BVH::raycast(const Mesh& mesh, const glm::vec3& origin, const glm::vec3& dir, RayHit& hit) const {@@ -251,22 +246,12 @@ void BVH::queryTriangles(const Mesh& mesh, const glm::vec3& center, float radiusvoid BVH::queryVertices(const Mesh& mesh, const glm::vec3& center, float radius, std::vector<uint32_t>& out) const {out.clear();+ queryLeaves_.clear();if (nodes_.empty()) return;+ if (vertBits_.capacity() < mesh.vertexCount()) vertBits_.resize(mesh.vertexCount());const float r2 = radius * radius;- // vertStamp_ holds (epoch << 16) | code: kDone once a vertex is output in this query; while splitting- // over threads, the code of the earliest subtree (in traversal order) holding it, kTaskCode - task.- constexpr uint32_t kDone = 0xFFFF, kTaskCode = 0xFFFE;- if (++queryEpoch_ == 0x10000) {- std::fill(vertStamp_.begin(), vertStamp_.end(), 0);- queryEpoch_ = 1;- }- const uint32_t epoch = queryEpoch_ << 16, done = epoch | kDone;- auto inside = [&](uint32_t v) {- const glm::vec3 d = mesh.positions[v] - center;- return glm::dot(d, d) <= r2;- };- // The subtrees the sphere touches at a fixed depth, in the order a depth-first traversal reaches them.+ // The subtrees the sphere touches at a fixed depth (in depth-first order), the units of parallel work.constexpr int kTaskDepth = 9;std::vector<uint32_t>& tasks = queryTasks_;tasks.clear();@@ -283,11 +268,13 @@ void BVH::queryVertices(const Mesh& mesh, const glm::vec3& center, float radius,stack.push_back({n.first, depth + 1});stack.push_back({n.first + 1, depth + 1});}- // Visits the vertices of the triangles under node `root` that the sphere's boxes touch, depth first.- auto walk = [&](uint32_t root, std::vector<uint32_t>& nodeStack, auto&& visit) {+ // Adds the vertices inside the sphere of the triangles under node `root` that the sphere's boxes touch, and+ // lists (depth first) the leaves holding any of them.+ auto walk = [&](uint32_t root, std::vector<uint32_t>& nodeStack, std::vector<uint32_t>& leaves) {nodeStack.assign(1, root);while (!nodeStack.empty()) {- const Node& n = nodes_[nodeStack.back()];+ const uint32_t ni = nodeStack.back();+ const Node& n = nodes_[ni];nodeStack.pop_back();if (!sphereBox(n.box, center, r2)) continue;if (n.count == 0) {@@ -295,98 +282,63 @@ void BVH::queryVertices(const Mesh& mesh, const glm::vec3& center, float radius,nodeStack.push_back(n.first + 1);continue;}+ bool any = false;for (uint32_t i = n.first; i < n.first + n.count; ++i) {const auto& t = mesh.triangles[triIndices_[i]];- visit(t.x);- visit(t.y);- visit(t.z);+ for (int c = 0; c < 3; ++c) {+ const uint32_t v = t[c];+ if (vertBits_.contains(v)) { // only vertices inside are ever added+ any = true;+ continue;+ }+ const glm::vec3 d = mesh.positions[v] - center;+ if (glm::dot(d, d) <= r2) {+ vertBits_.insert(v);+ any = true;+ }+ }}+ if (any) leaves.push_back(ni);}};-- if (tasks.size() < 8 || tasks.size() > kTaskCode || ThreadPool::shared().serial()) {- for (uint32_t root : tasks)- walk(root, stack_, [&](uint32_t v) {- if (vertStamp_[v] == done) return;- vertStamp_[v] = done;- if (inside(v)) out.push_back(v);- });- return;- }- // In parallel, with the serial order: each subtree lists the vertices inside the sphere and claims them- // (the earliest subtree's code wins: an atomic max); then each lists the ones it owns, in the order it- // first met them, and the lists are joined in subtree order.- if (queryParts_.size() < tasks.size()) queryParts_.resize(tasks.size());- poolFor(tasks.size(), [&](size_t b, size_t e) {- std::vector<uint32_t> nodeStack;- for (size_t k = b; k < e; ++k) {- const uint32_t mine = epoch | (kTaskCode - uint32_t(k));- std::vector<uint32_t>& found = queryParts_[k];- found.clear();- walk(tasks[k], nodeStack, [&](uint32_t v) {- AtomicRef<uint32_t> owner(vertStamp_[v]);- if (owner.load(std::memory_order_relaxed) == mine || !inside(v)) return;- found.push_back(v);- for (uint32_t cur = owner.load(std::memory_order_relaxed);- cur < mine && !owner.compare_exchange_weak(cur, mine, std::memory_order_relaxed);) {- }- });- }- }, 1, 2);- poolFor(tasks.size(), [&](size_t b, size_t e) {- for (size_t k = b; k < e; ++k) {- const uint32_t mine = epoch | (kTaskCode - uint32_t(k));- std::vector<uint32_t>& found = queryParts_[k];- size_t kept = 0;- for (uint32_t v : found) {- AtomicRef<uint32_t> owner(vertStamp_[v]);- if (owner.load(std::memory_order_relaxed) != mine) continue;- owner.store(done, std::memory_order_relaxed);- found[kept++] = v;+ // The set doesn't depend on which thread found what, take() lists it in index order, and the leaves are+ // joined in task order (the serial order).+ if (tasks.size() < 8 || ThreadPool::shared().serial()) {+ for (uint32_t root : tasks) walk(root, stack_, queryLeaves_);+ } else {+ if (queryParts_.size() < tasks.size()) queryParts_.resize(tasks.size());+ poolFor(tasks.size(), [&](size_t b, size_t e) {+ std::vector<uint32_t> nodeStack;+ for (size_t k = b; k < e; ++k) {+ queryParts_[k].clear();+ walk(tasks[k], nodeStack, queryParts_[k]);}- found.resize(kept);- }- }, 1, 2);- for (size_t k = 0; k < tasks.size(); ++k) out.insert(out.end(), queryParts_[k].begin(), queryParts_[k].end());+ }, 1, 2);+ for (size_t k = 0; k < tasks.size(); ++k) queryLeaves_.insert(queryLeaves_.end(), queryParts_[k].begin(), queryParts_[k].end());+ }+ vertBits_.take(out);}void BVH::refit(const Mesh& mesh, std::span<const uint32_t> movedVerts) {if (nodes_.empty()) return;- const uint32_t s = nextStamp(leafStamp_, leafStampValue_);+ if (leafBits_.capacity() < nodes_.size()) leafBits_.resize(nodes_.size());+ poolFor(movedVerts.size(), [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i)+ for (uint32_t ti : mesh.vertexTriangles(movedVerts[i])) leafBits_.insert(triLeaf_[ti]);+ }, 4096);scratchLeaves_.clear();- constexpr size_t kBlock = 4096;- if (movedVerts.size() <= kBlock) {- for (uint32_t v : movedVerts) {- for (uint32_t ti : mesh.vertexTriangles(v)) {- uint32_t leaf = triLeaf_[ti];- if (leafStamp_[leaf] != s) {- leafStamp_[leaf] = s;- scratchLeaves_.push_back(leaf);- }- }- }- } else {- // Threads claim leaves with an atomic exchange of the stamp (the set, and so the result, is the same).- const size_t parts = (movedVerts.size() + kBlock - 1) / kBlock;- if (scratchParts_.size() < parts) scratchParts_.resize(parts);- poolFor(movedVerts.size(), [&](size_t b, size_t e) {- std::vector<uint32_t>& out = scratchParts_[b / kBlock];- out.clear();- for (size_t i = b; i < e; ++i)- for (uint32_t ti : mesh.vertexTriangles(movedVerts[i])) {- const uint32_t leaf = triLeaf_[ti];- if (AtomicRef<uint32_t>(leafStamp_[leaf]).exchange(s, std::memory_order_relaxed) != s)- out.push_back(leaf);- }- }, kBlock);- for (size_t p = 0; p < parts; ++p)- scratchLeaves_.insert(scratchLeaves_.end(), scratchParts_[p].begin(), scratchParts_[p].end());- }- poolFor(scratchLeaves_.size(), [&](size_t b, size_t e) {- for (size_t i = b; i < e; ++i) recomputeLeaf(mesh, nodes_[scratchLeaves_[i]]);+ leafBits_.take(scratchLeaves_);+ refitLeaves(mesh, scratchLeaves_);+}++void BVH::refitQueried(const Mesh& mesh) { refitLeaves(mesh, queryLeaves_); }++void BVH::refitLeaves(const Mesh& mesh, std::span<const uint32_t> leaves) {+ poolFor(leaves.size(), [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) nodes_[leaves[i]].box = leafBox(mesh, nodes_[leaves[i]]);}, 512);// Grow ancestors until one already contains the new bounds (a union, so the order doesn't matter).- for (uint32_t li : scratchLeaves_) {+ for (uint32_t li : leaves) {AABB box = nodes_[li].box;uint32_t ni = li;while (ni != 0) {@@ -402,8 +354,8 @@ void BVH::refit(const Mesh& mesh, std::span<const uint32_t> movedVerts) {size_t BVH::memoryBytes() const {auto bytes = [](const auto& v) { return v.capacity() * sizeof(v[0]); };- return bytes(nodes_) + bytes(triIndices_) + bytes(triLeaf_) + bytes(vertStamp_) + bytes(leafStamp_) +- bytes(scratchLeaves_) + bytes(stack_);+ return bytes(nodes_) + bytes(triIndices_) + bytes(triLeaf_) + vertBits_.memoryBytes() + leafBits_.memoryBytes() ++ bytes(queryLeaves_) + bytes(scratchLeaves_) + bytes(stack_);}void BVH::refitAll(const Mesh& mesh) {diff --git a/src/core/BVH.h b/src/core/BVH.hindex 7d8a97a2..abbea262 100644--- a/src/core/BVH.h+++ b/src/core/BVH.h@@ -2,6 +2,8 @@// SPDX-License-Identifier: GPL-3.0-or-later#pragma once+#include "core/IndexBits.h"+#include "core/IndexBits.h"#include "core/Mesh.h"#include <span>@@ -23,7 +25,7 @@ public:void build(const Mesh& mesh);// Nearest hit along the ray; hidden triangles (Mesh::hidden) are skipped.bool raycast(const Mesh& mesh, const glm::vec3& origin, const glm::vec3& dir, RayHit& hit) const;- // Appends every vertex within `radius` of `center` to `out` (cleared first).+ // Appends every vertex within `radius` of `center` to `out` (cleared first), in increasing index order.// Ignores visibility; brushes use sf::brushVertices, which filters hidden ones.void queryVertices(const Mesh& mesh, const glm::vec3& center, float radius, std::vector<uint32_t>& out) const;// Appends every triangle whose bounds touch the sphere (cleared first; hidden ones included).@@ -31,6 +33,9 @@ public:// Updates bounds after the given vertices moved. Bounds only ever grow// conservatively for ancestors, which keeps queries correct.void refit(const Mesh& mesh, std::span<const uint32_t> movedVerts);+ // refit() after only vertices the last queryVertices() returned moved (brush dabs): the query already+ // found the leaves holding them, so this skips looking them up.+ void refitQueried(const Mesh& mesh);// Recomputes every box (same tree) after most of the mesh moved, e.g. a whole SubTool transformed.void refitAll(const Mesh& mesh);@@ -45,21 +50,20 @@ private:uint32_t parent = 0;};- uint32_t nextStamp(std::vector<uint32_t>& stamps, uint32_t& stamp) const;- void recomputeLeaf(const Mesh& mesh, Node& n);+ AABB leafBox(const Mesh& mesh, const Node& n) const;+ void recomputeLeaf(const Mesh& mesh, Node& n); // its box, and triLeaf_ for its triangles+ void refitLeaves(const Mesh& mesh, std::span<const uint32_t> leaves);std::vector<Node> nodes_;std::vector<uint32_t> triIndices_;std::vector<uint32_t> triLeaf_;- mutable std::vector<uint32_t> vertStamp_; // queryVertices: (epoch << 16) | per-query code- mutable uint32_t queryEpoch_ = 0;+ mutable IndexBits vertBits_; // queryVertices: the vertices found so far+ mutable std::vector<uint32_t> queryLeaves_; // the leaves holding them, for refitQueried()mutable std::vector<uint32_t> queryTasks_;mutable std::vector<std::vector<uint32_t>> queryParts_;- std::vector<uint32_t> leafStamp_;- uint32_t leafStampValue_ = 0;+ IndexBits leafBits_; // refit: the leaves to recomputestd::vector<uint32_t> scratchLeaves_;- std::vector<std::vector<uint32_t>> scratchParts_;mutable std::vector<uint32_t> stack_;};diff --git a/src/core/IndexBits.cpp b/src/core/IndexBits.cppnew file mode 100644index 00000000..333fa1d3--- /dev/null+++ b/src/core/IndexBits.cpp@@ -0,0 +1,65 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+#include "core/IndexBits.h"++#include "core/ThreadPool.h"++#include <bit>++namespace sf {++void IndexBits::resize(size_t n) {+ const size_t chunks = (n + kChunk - 1) / kChunk;+ words_.assign(chunks * kChunkWords, 0);+ summary_.assign((chunks + 63) / 64, 0);+}++bool IndexBits::empty() const {+ for (uint64_t s : summary_)+ if (s) return false;+ return true;+}++void IndexBits::chunks(std::vector<uint32_t>& out) const {+ out.clear();+ for (size_t i = 0; i < summary_.size(); ++i)+ for (uint64_t bits = summary_[i]; bits; bits &= bits - 1) out.push_back(uint32_t(i * 64 + size_t(std::countr_zero(bits))));+}++void IndexBits::take(std::vector<uint32_t>& out, const std::function<void(size_t, uint64_t)>& perWord) {+ chunks(chunks_);+ const size_t nc = chunks_.size();+ if (nc == 0) return;+ // Count each chunk's members, then write them at their offsets (blocks of chunks, so small sets run inline).+ constexpr size_t kBlock = 8;+ counts_.resize(nc + 1);+ poolFor(nc, [&](size_t b, size_t e) {+ for (size_t k = b; k < e; ++k) {+ const uint64_t* w = words_.data() + size_t(chunks_[k]) * kChunkWords;+ uint32_t n = 0;+ for (uint32_t j = 0; j < kChunkWords; ++j) n += uint32_t(std::popcount(w[j]));+ counts_[k + 1] = n;+ }+ }, kBlock);+ counts_[0] = 0;+ for (size_t k = 0; k < nc; ++k) counts_[k + 1] += counts_[k];+ const size_t base = out.size();+ out.resize(base + counts_[nc]);+ poolFor(nc, [&](size_t b, size_t e) {+ for (size_t k = b; k < e; ++k) {+ const size_t first = size_t(chunks_[k]) * kChunkWords;+ uint32_t* dst = out.data() + base + counts_[k];+ for (uint32_t j = 0; j < kChunkWords; ++j) {+ uint64_t bits = words_[first + j];+ if (!bits) continue;+ if (perWord) perWord(first + j, bits);+ words_[first + j] = 0;+ const uint32_t i0 = uint32_t((first + j) * 64);+ for (; bits; bits &= bits - 1) *dst++ = i0 + uint32_t(std::countr_zero(bits));+ }+ }+ }, kBlock);+ for (uint32_t c : chunks_) summary_[c >> 6] &= ~(uint64_t(1) << (c & 63));+}++} // namespace sfdiff --git a/src/core/IndexBits.h b/src/core/IndexBits.hnew file mode 100644index 00000000..82569b5f--- /dev/null+++ b/src/core/IndexBits.h@@ -0,0 +1,65 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// IndexBits: a set of vertex (or triangle) indices as one bit each, for the per-dab brush passes (BVH vertex+// queries, normal updates). Threads add indices with an atomic OR; take() then lists the members in increasing+// index order and empties the set. The order doesn't depend on which thread added what, so results stay the+// same with any thread count, and passes over the list walk the mesh's arrays front to back instead of+// jumping around (the indices a dab touches lie in a few dozen runs).+//+// A summary bit per kChunk indices marks the chunks holding members, so take() only visits those: its cost+// follows the size of the set, not of the mesh.+#pragma once++#include "core/AtomicRef.h"++#include <cstddef>+#include <cstdint>+#include <functional>+#include <vector>++namespace sf {++class IndexBits {+public:+ static constexpr uint32_t kChunkWords = 64, kChunk = kChunkWords * 64; // 4096 indices per summary bit++ // Holds indices [0, n); empties the set.+ void resize(size_t n);+ size_t capacity() const { return words_.size() * 64; }+ bool empty() const;++ bool contains(uint32_t i) const {+ return (AtomicRef<uint64_t>(const_cast<uint64_t&>(words_[i >> 6])).load(std::memory_order_relaxed) >> (i & 63)) & 1;+ }+ // Adds `i` (safe to call from several threads at once); returns true if it wasn't a member yet.+ bool insert(uint32_t i) {+ const uint64_t bit = uint64_t(1) << (i & 63);+ AtomicRef<uint64_t> w(words_[i >> 6]);+ if (w.load(std::memory_order_relaxed) & bit) return false;+ const uint64_t old = w.fetch_or(bit, std::memory_order_relaxed);+ if (old & bit) return false;+ if (old == 0) {+ const uint32_t c = i / kChunk;+ const uint64_t sbit = uint64_t(1) << (c & 63);+ AtomicRef<uint64_t> s(summary_[c >> 6]);+ if (!(s.load(std::memory_order_relaxed) & sbit)) s.fetch_or(sbit, std::memory_order_relaxed);+ }+ return true;+ }++ // Appends the members to `out` in increasing order and empties the set (in parallel on the shared pool+ // for big sets). `perWord(wordIndex, bits)`, if given, sees every non-zero word once (from any thread, but+ // each 64-word chunk on one thread), e.g. to OR the set into another bitmap of the same layout.+ void take(std::vector<uint32_t>& out, const std::function<void(size_t, uint64_t)>& perWord = {});++ size_t memoryBytes() const { return (words_.capacity() + summary_.capacity()) * sizeof(uint64_t); }++private:+ void chunks(std::vector<uint32_t>& out) const;++ std::vector<uint64_t> words_, summary_;+ std::vector<uint32_t> chunks_, counts_;+};++} // namespace sfdiff --git a/src/core/Mesh.cpp b/src/core/Mesh.cppindex 7e654d68..522f5d4c 100644--- a/src/core/Mesh.cpp+++ b/src/core/Mesh.cpp@@ -166,8 +166,7 @@ void Mesh::rebuildTopology() {}});- vertStamp_.assign(nv, 0);- stamp_ = 0;+ normalBits_.resize(nv);normals.resize(nv);dirtyBlocks.assign((nv + kDirtyBlock - 1) / kDirtyBlock, 0);dirtyBits.assign(dirtyBlocks.size() * (kDirtyBlock / 64), 0);@@ -176,14 +175,6 @@ void Mesh::rebuildTopology() {facesDirty = true;}-uint32_t Mesh::nextStamp() {- if (++stamp_ == 0) {- std::fill(vertStamp_.begin(), vertStamp_.end(), 0);- stamp_ = 1;- }- return stamp_;-}-void Mesh::growDirtyBlocks(size_t blocks) {if (dirtyBlocks.size() < blocks) dirtyBlocks.resize(blocks, 0);if (dirtyBits.size() < blocks * (kDirtyBlock / 64)) dirtyBits.resize(blocks * (kDirtyBlock / 64), 0);@@ -245,77 +236,40 @@ void Mesh::recomputeAllNormals() {}void Mesh::updateNormals(std::span<const uint32_t> verts) {- // The vertices whose normal changes: the moved ones and every neighbour (each once), then each one's- // normal from its faces, in its triangle order (as recomputeAllNormals sums them).+ // 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.const size_t nv = positions.size();- if (vertStamp_.size() != nv) return; // no topology+ if (adjOffsets_.size() != nv + 1) return; // no topologygrowDirtyBlocks((nv + kDirtyBlock - 1) / kDirtyBlock);- constexpr size_t kBlock = 4096;- scratchVerts_.clear();- if (verts.size() <= 4 * kBlock || ThreadPool::shared().serial()) {- const uint32_t s = nextStamp();- auto claim = [&](uint32_t u) {- if (vertStamp_[u] == s) return;- vertStamp_[u] = s;- scratchVerts_.push_back(u);- };- for (uint32_t v : verts) {- claim(v);- for (uint32_t u : neighbors(v)) claim(u);- }- } else {- // In parallel: first the moved vertices themselves (stamp `own`), then the ring around them that isn't- // one of them (stamp `ring`, claimed with an atomic exchange: only the ring is shared between blocks).- // Which block lists a ring vertex varies, but the set, and every normal computed from it, doesn't.- if (stamp_ >= 0xFFFFFFF0u) { // two stamps in a row: start over first, so they can't wrap in between- std::fill(vertStamp_.begin(), vertStamp_.end(), 0);- stamp_ = 0;+ if (normalBits_.capacity() < nv) normalBits_.resize(nv);+ poolFor(verts.size(), [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) {+ const uint32_t v = verts[i];+ normalBits_.insert(v);+ for (uint32_t u : neighbors(v)) normalBits_.insert(u);}- const uint32_t own = nextStamp(), ring = nextStamp();- const size_t parts = (verts.size() + kBlock - 1) / kBlock;- if (scratchParts_.size() < 2 * parts) scratchParts_.resize(2 * parts);- poolFor(verts.size(), [&](size_t b, size_t e) {- std::vector<uint32_t>& out = scratchParts_[b / kBlock];- out.clear();- for (size_t i = b; i < e; ++i)- if (AtomicRef<uint32_t>(vertStamp_[verts[i]]).exchange(own, std::memory_order_relaxed) != own)- out.push_back(verts[i]);- }, kBlock);- poolFor(verts.size(), [&](size_t b, size_t e) {- std::vector<uint32_t>& out = scratchParts_[parts + b / kBlock];- out.clear();- for (size_t i = b; i < e; ++i)- for (uint32_t u : neighbors(verts[i])) {- AtomicRef<uint32_t> stamp(vertStamp_[u]);- const uint32_t seen = stamp.load(std::memory_order_relaxed);- if (seen == own || seen == ring) continue;- if (stamp.exchange(ring, std::memory_order_relaxed) != ring) out.push_back(u);- }- }, kBlock);- for (size_t p = 0; p < 2 * parts; ++p) scratchVerts_.insert(scratchVerts_.end(), scratchParts_[p].begin(), scratchParts_[p].end());- }-- const size_t n = scratchVerts_.size();- std::vector<std::pair<uint32_t, uint32_t>> bounds((n + kBlock - 1) / kBlock);- poolFor(n, [&](size_t b, size_t e) {- uint32_t lo = std::numeric_limits<uint32_t>::max(), hi = 0;+ }, 4096);+ // 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);+ scratchVerts_.clear();+ normalBits_.take(scratchVerts_, [&](size_t word, uint64_t bits) {+ dirtyBits[word] |= bits;+ dirtyBlocks[(word * 64) >> kDirtyBlockShift] = 1;+ });+ if (scratchVerts_.empty()) return;+ dirtyBegin = std::min(dirtyBegin, scratchVerts_.front());+ dirtyEnd = std::max(dirtyEnd, scratchVerts_.back() + 1);+ poolFor(scratchVerts_.size(), [&](size_t b, size_t e) {for (size_t i = b; i < e; ++i) {const uint32_t v = scratchVerts_[i];glm::vec3 sum(0.0f);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;- lo = std::min(lo, v);- hi = std::max(hi, v);- AtomicRef<uint8_t>(dirtyBlocks[v >> kDirtyBlockShift]).store(1, std::memory_order_relaxed);- AtomicRef<uint64_t>(dirtyBits[v >> 6]).fetch_or(uint64_t(1) << (v & 63), std::memory_order_relaxed);}- bounds[b / kBlock] = {lo, hi + 1};- }, kBlock);- for (const auto& [lo, hi] : bounds) {- dirtyBegin = std::min(dirtyBegin, lo);- dirtyEnd = std::max(dirtyEnd, hi);- }+ }, 4096);}int Mesh::addLayer(std::string name) {@@ -382,7 +336,7 @@ size_t Mesh::memoryBytes() const {auto bytes = [](const auto& v) { return v.capacity() * sizeof(v[0]); };size_t n = bytes(positions) + bytes(normals) + bytes(triangles) + bytes(mask) + bytes(colors) + bytes(polygroups) +bytes(hidden) + bytes(uvs) + bytes(quadPartners) + bytes(base) + bytes(paintBase) + bytes(adjOffsets_) +- bytes(adjList_) + bytes(vtOffsets_) + bytes(vtList_) + bytes(vertStamp_) + bytes(scratchVerts_) + bytes(dirtyBlocks) + bytes(dirtyBits);+ bytes(adjList_) + bytes(vtOffsets_) + bytes(vtList_) + normalBits_.memoryBytes() + bytes(scratchVerts_) + bytes(dirtyBlocks) + bytes(dirtyBits);for (const auto& l : layers) n += bytes(l.delta);for (const auto& l : paintLayers) n += bytes(l.color) + bytes(l.alpha);return n;diff --git a/src/core/Mesh.h b/src/core/Mesh.hindex b5cab46c..c9eea325 100644--- a/src/core/Mesh.h+++ b/src/core/Mesh.h@@ -2,6 +2,7 @@// SPDX-License-Identifier: GPL-3.0-or-later#pragma once+#include "core/IndexBits.h"#include "core/PaintLayer.h"#include <glm/glm.hpp>@@ -209,16 +210,12 @@ public:bool vertexDirty(uint32_t v) const { return (v >> 6) < dirtyBits.size() && (dirtyBits[v >> 6] >> (v & 63)) & 1; }private:- uint32_t nextStamp();-std::vector<uint32_t> adjOffsets_, adjList_;std::vector<uint32_t> vtOffsets_, vtList_;- // updateNormals: vertices already gathered this call (== stamp_), the gathered ones, per-block parts.- std::vector<uint32_t> vertStamp_;- uint32_t stamp_ = 0;+ // updateNormals: the vertices to update, as a set and as a list.+ IndexBits normalBits_;std::vector<uint32_t> scratchVerts_;- std::vector<std::vector<uint32_t>> scratchParts_;};// Shape-preserving Taubin (lambda/mu) smoothing of positions; needs topology.diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cppindex ec82d98a..f342589d 100644--- a/src/core/Sculpt.cpp+++ b/src/core/Sculpt.cpp@@ -421,15 +421,18 @@ void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams&});displaceAll(mesh, verts_, scratch_);- finishDab(mesh, bvh, verts_, clock);+ finishDab(mesh, bvh, verts_, clock, true);}// Normals and BVH after a dab moved `verts`, with the time each takes (the kernel ran since `clock`).-void Sculptor::finishDab(Mesh& mesh, BVH& bvh, std::span<const uint32_t> verts, std::chrono::steady_clock::time_point clock) {+// `queried`: verts are (some of) what the BVH's last vertex query returned.+void Sculptor::finishDab(Mesh& mesh, BVH& bvh, std::span<const uint32_t> verts, std::chrono::steady_clock::time_point clock,+ bool queried) {timings.kernelMs += lapMs(clock);mesh.updateNormals(verts);timings.normalsMs += lapMs(clock);- bvh.refit(mesh, verts);+ if (queried) bvh.refitQueried(mesh);+ else bvh.refit(mesh, verts);timings.bvhMs += lapMs(clock);++timings.dabs;timings.vertices += verts.size();@@ -574,7 +577,7 @@ void Sculptor::dabRecipeSingle(Mesh& mesh, BVH& bvh, const BrushRecipe& r, const});displaceAll(mesh, verts_, scratch_);}- finishDab(mesh, bvh, verts_, clock);+ finishDab(mesh, bvh, verts_, clock, true);}void Sculptor::beginGrab(Mesh& mesh, BVH& bvh, const DabParams& p, Symmetry sym) {diff --git a/src/core/Sculpt.h b/src/core/Sculpt.hindex c5edb717..891c04a0 100644--- a/src/core/Sculpt.h+++ b/src/core/Sculpt.h@@ -116,7 +116,8 @@ private:void dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p);void dabRecipeSingle(Mesh& mesh, BVH& bvh, const BrushRecipe& r, const DabParams& p);- void finishDab(Mesh& mesh, BVH& bvh, std::span<const uint32_t> verts, std::chrono::steady_clock::time_point clock);+ void finishDab(Mesh& mesh, BVH& bvh, std::span<const uint32_t> verts, std::chrono::steady_clock::time_point clock,+ bool queried = false);void touch(Mesh& mesh, uint32_t v);// The one place brushes move vertices: scales `d` by vertexInfluence().void displace(Mesh& mesh, uint32_t v, const glm::vec3& d);