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
Strokes: record undo entries in parallel, a bit per vertex instead of three 32-bit stamps; Move runs each mirror in parallel; undo swaps positions in parallel
islandmed3047cb27e1f6
diff --git a/src/core/BVH.h b/src/core/BVH.hindex 485f3013..ac155476 100644--- a/src/core/BVH.h+++ b/src/core/BVH.h@@ -36,6 +36,10 @@ public:// 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);+ // The leaves holding the vertices the last queryVertices() returned, and refit() given such leaves (from+ // one or more queries, each once: the Move brush keeps them for its whole stroke).+ std::span<const uint32_t> queriedLeaves() const { return queryLeaves_; }+ void refitLeaves(const Mesh& mesh, std::span<const uint32_t> leaves);// Recomputes every box (same tree) after most of the mesh moved, e.g. a whole SubTool transformed.void refitAll(const Mesh& mesh);@@ -52,7 +56,6 @@ private: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_;diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cppindex f342589d..6ceacedf 100644--- a/src/core/Sculpt.cpp+++ b/src/core/Sculpt.cpp@@ -2,6 +2,7 @@// SPDX-License-Identifier: GPL-3.0-or-later#include "core/Sculpt.h"+#include "core/AtomicRef.h"#include "core/BrushAlpha.h"#include "core/ThreadPool.h"@@ -155,44 +156,62 @@ std::vector<glm::vec3> Sculptor::mirrors(Symmetry sym) {return out;}+namespace {++// Sets bit `v` (atomically: neighbouring blocks of a parallel pass may share a word); true if it was clear.+bool markBit(std::vector<uint64_t>& bits, uint32_t v) {+ const uint64_t bit = uint64_t(1) << (v & 63);+ AtomicRef<uint64_t> w(bits[v >> 6]);+ if (w.load(std::memory_order_relaxed) & bit) return false;+ return !(w.fetch_or(bit, std::memory_order_relaxed) & bit);+}++// Clears the bits of `indices` (what a stroke recorded), so the next stroke starts with an empty set.+void clearBits(std::vector<uint64_t>& bits, const std::vector<uint32_t>& indices) {+ poolFor(indices.size(), [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i)+ if (indices[i] < bits.size() * 64) AtomicRef<uint64_t>(bits[indices[i] >> 6]).store(0, std::memory_order_relaxed);+ }, 1 << 14);+}++} // namespace++void Sculptor::clearRecorded() {+ clearBits(touched_, record_.positions.indices);+ clearBits(masked_, record_.mask.indices);+ clearBits(colored_, record_.colors.indices);+}+bool Sculptor::beginStroke(const Mesh& mesh, std::string label, int layer) {if (layer >= static_cast<int>(mesh.layers.size())) layer = -1;if (std::abs(mesh.layerTargetWeight(layer)) < 1e-3f) return false;+ clearRecorded(); // a stroke that never endedlayer_ = layer;record_ = UndoRecord{};record_.label = std::move(label);record_.layer = layer;- if (touchStamp_.size() != mesh.vertexCount()) {- touchStamp_.assign(mesh.vertexCount(), 0);- maskStamp_.assign(mesh.vertexCount(), 0);- colorStamp_.assign(mesh.vertexCount(), 0);- stamp_ = 0;- }- if (++stamp_ == 0) {- std::fill(touchStamp_.begin(), touchStamp_.end(), 0);- std::fill(maskStamp_.begin(), maskStamp_.end(), 0);- std::fill(colorStamp_.begin(), colorStamp_.end(), 0);- stamp_ = 1;+ const size_t words = (mesh.vertexCount() + 63) / 64;+ if (touched_.size() != words) {+ touched_.assign(words, 0);+ masked_.assign(words, 0);+ colored_.assign(words, 0);}+ ++strokeId_;active_ = true;return true;}void Sculptor::touch(Mesh& mesh, uint32_t v) {- if (touchStamp_[v] == stamp_) return;- touchStamp_[v] = stamp_;- record_.positions.add(v, mesh.layerTarget(layer_)[v]);+ if (markBit(touched_, v)) record_.positions.add(v, mesh.layerTarget(layer_)[v]);}void Sculptor::recordMask(const Mesh& mesh, uint32_t v) {- if (!active_ || maskStamp_[v] == stamp_) return;- maskStamp_[v] = stamp_;+ if (!active_ || !markBit(masked_, v)) return;record_.mask.add(v, mesh.maskAt(v));}void Sculptor::recordColor(const Mesh& mesh, uint32_t v) {- if (!active_ || colorStamp_[v] == stamp_) return;- colorStamp_[v] = stamp_;+ if (!active_ || !markBit(colored_, v)) return;// With paint layers the stroke paints the active layer (colour and coverage).const int layer = mesh.paintLayers.empty() ? -1 : mesh.activePaintLayer;record_.paintLayer = layer;@@ -217,33 +236,54 @@ void Sculptor::displace(Mesh& mesh, uint32_t v, const glm::vec3& d) {if (!mesh.layers.empty()) mesh.layerTarget(layer_)[v] += dk / mesh.layerTargetWeight(layer_);}-// displace() for many vertices (each listed once): influences in parallel, the undo record in order, then the-// moves in parallel.+// displace() for many vertices (each listed once), in parallel: the influences and which vertices the stroke+// moves for the first time, then their undo entries (in list order, at offsets counted per block) and the moves.void Sculptor::displaceAll(Mesh& mesh, std::span<const uint32_t> verts, std::span<const glm::vec3> d) {const size_t n = verts.size();+ constexpr size_t kBlock = 4096;influence_.resize(n);+ firstTouch_.resize(n);+ blockFirst_.assign((n + kBlock - 1) / kBlock + 1, 0);poolFor(n, [&](size_t b, size_t e) {- for (size_t i = b; i < e; ++i) influence_[i] = vertexInfluence(mesh, verts[i]);- });- for (size_t i = 0; i < n; ++i)- if (influence_[i] > 0.0f) touch(mesh, verts[i]);- const bool layered = !mesh.layers.empty();+ uint32_t count = 0;+ for (size_t i = b; i < e; ++i) {+ const float k = vertexInfluence(mesh, verts[i]);+ influence_[i] = k;+ firstTouch_[i] = k > 0.0f && markBit(touched_, verts[i]);+ count += firstTouch_[i];+ }+ blockFirst_[b / kBlock + 1] = count;+ }, kBlock);+ for (size_t i = 1; i < blockFirst_.size(); ++i) blockFirst_[i] += blockFirst_[i - 1];std::vector<glm::vec3>& target = mesh.layerTarget(layer_);+ UndoChannel<glm::vec3>& rec = record_.positions;+ const size_t base = rec.indices.size();+ rec.indices.resize(base + blockFirst_.back());+ rec.values.resize(base + blockFirst_.back());+ const bool layered = !mesh.layers.empty();const float weight = mesh.layerTargetWeight(layer_);poolFor(n, [&](size_t b, size_t e) {+ size_t r = base + blockFirst_[b / kBlock];for (size_t i = b; i < e; ++i) {const float k = influence_[i];if (k <= 0.0f) continue;+ const uint32_t v = verts[i];+ if (firstTouch_[i]) {+ rec.indices[r] = v;+ rec.values[r++] = target[v];+ }const glm::vec3 dk = d[i] * k;- mesh.positions[verts[i]] += dk;- if (layered) target[verts[i]] += dk / weight;+ mesh.positions[v] += dk;+ if (layered) target[v] += dk / weight;}- });+ }, kBlock);}UndoRecord Sculptor::endStroke() {+ clearRecorded();active_ = false;- grab_.clear();+ grabVerts_.clear();+ grabAll_.clear();return std::move(record_);}@@ -581,8 +621,11 @@ void Sculptor::dabRecipeSingle(Mesh& mesh, BVH& bvh, const BrushRecipe& r, const}void Sculptor::beginGrab(Mesh& mesh, BVH& bvh, const DabParams& p, Symmetry sym) {- grab_.clear();grabVerts_.clear();+ grabWeights_.clear();+ grabMirrors_.clear();+ grabAll_.clear();+ grabLeaves_.clear();if (!active_) return;for (const glm::vec3& m : mirrors(sym)) {glm::vec3 c = p.center * m;@@ -591,19 +634,46 @@ void Sculptor::beginGrab(Mesh& mesh, BVH& bvh, const DabParams& p, Symmetry sym)float w = brushFalloff(glm::distance(mesh.positions[v], c), p.radius) *std::clamp(p.strength * 2.0f, 0.0f, 1.0f);if (w <= 0.0f) continue;- grab_.push_back({v, w, m});grabVerts_.push_back(v);+ grabWeights_.push_back(w);}+ grabMirrors_.push_back({grabVerts_.size(), m});+ const std::span<const uint32_t> leaves = bvh.queriedLeaves();+ grabLeaves_.insert(grabLeaves_.end(), leaves.begin(), leaves.end());+ }+ // Every vertex and leaf the mirrors move, once each (each mirror's list is sorted already).+ grabAll_ = grabVerts_;+ for (size_t i = 1; i < grabMirrors_.size(); ++i)+ std::inplace_merge(grabAll_.begin(), grabAll_.begin() + std::ptrdiff_t(grabMirrors_[i - 1].first),+ grabAll_.begin() + std::ptrdiff_t(grabMirrors_[i].first));+ grabAll_.erase(std::unique(grabAll_.begin(), grabAll_.end()), grabAll_.end());+ if (grabMirrors_.size() > 1) {+ std::sort(grabLeaves_.begin(), grabLeaves_.end());+ grabLeaves_.erase(std::unique(grabLeaves_.begin(), grabLeaves_.end()), grabLeaves_.end());}- std::sort(grabVerts_.begin(), grabVerts_.end());- grabVerts_.erase(std::unique(grabVerts_.begin(), grabVerts_.end()), grabVerts_.end());}void Sculptor::grab(Mesh& mesh, BVH& bvh, const glm::vec3& delta) {- if (grab_.empty()) return;+ if (grabVerts_.empty()) return;auto clock = std::chrono::steady_clock::now();- for (const auto& g : grab_) displace(mesh, g.vertex, delta * g.mirror * g.weight);- finishDab(mesh, bvh, grabVerts_, clock);+ // One mirror after another (a vertex may be in two), each in parallel.+ size_t begin = 0;+ for (const auto& [end, m] : grabMirrors_) {+ const glm::vec3 dm = delta * m;+ scratch_.resize(end - begin);+ poolFor(end - begin, [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) scratch_[i] = dm * grabWeights_[begin + i];+ });+ displaceAll(mesh, std::span(grabVerts_).subspan(begin, end - begin), scratch_);+ begin = end;+ }+ timings.kernelMs += lapMs(clock);+ mesh.updateNormals(grabAll_);+ timings.normalsMs += lapMs(clock);+ bvh.refitLeaves(mesh, grabLeaves_);+ timings.bvhMs += lapMs(clock);+ ++timings.dabs;+ timings.vertices += grabAll_.size();}// Puts every vertex the current stroke moved back to its stroke-start position.@@ -643,8 +713,8 @@ void Sculptor::prepareLayer(const Mesh& mesh, float height) {layerDir_.assign(mesh.vertexCount(), glm::vec3(0.0f));layerEpoch_ = 0;}- if (layerEpoch_ != 0 && layerSeenStamp_ == stamp_) return;- layerSeenStamp_ = stamp_;+ if (layerEpoch_ != 0 && layerSeenStroke_ == strokeId_) return;+ layerSeenStroke_ = strokeId_;layerHeight_ = height;if (++layerEpoch_ == 0) {std::fill(layerStamp_.begin(), layerStamp_.end(), 0);diff --git a/src/core/Sculpt.h b/src/core/Sculpt.hindex 891c04a0..72930394 100644--- a/src/core/Sculpt.h+++ b/src/core/Sculpt.h@@ -108,12 +108,6 @@ public:void restoreStroke(Mesh& mesh, BVH& bvh);private:- struct GrabEntry {- uint32_t vertex;- float weight;- glm::vec3 mirror;- };-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,@@ -126,21 +120,30 @@ private:bool active_ = false;int layer_ = -1;UndoRecord record_;- std::vector<uint32_t> touchStamp_, maskStamp_, colorStamp_;- uint32_t stamp_ = 0;+ // A bit per vertex: recorded in record_'s positions / mask / colours (cleared from those lists).+ std::vector<uint64_t> touched_, masked_, colored_;+ uint64_t strokeId_ = 0;+ void clearRecorded();std::vector<uint32_t> verts_;std::vector<glm::vec3> scratch_;std::vector<float> weights_, influence_;- std::vector<GrabEntry> grab_;+ std::vector<uint8_t> firstTouch_;+ std::vector<uint32_t> blockFirst_;+ // Move brush: each mirror's vertices and weights (grabMirrors_: where each mirror's run ends, and its+ // signs), every vertex once (sorted), and the BVH leaves holding them.std::vector<uint32_t> grabVerts_;+ std::vector<float> grabWeights_;+ std::vector<std::pair<size_t, glm::vec3>> grabMirrors_;+ std::vector<uint32_t> grabAll_, grabLeaves_;// Layer brush: per-stroke height already applied to each vertex.void prepareLayer(const Mesh& mesh, float height);std::vector<uint32_t> layerStamp_;std::vector<float> layerDone_;std::vector<glm::vec3> layerDir_;- uint32_t layerEpoch_ = 0, layerSeenStamp_ = 0;+ uint32_t layerEpoch_ = 0;+ uint64_t layerSeenStroke_ = 0;float layerHeight_ = 0.0f;public:diff --git a/src/core/Undo.cpp b/src/core/Undo.cppindex 75e7c92e..cb159cf7 100644--- a/src/core/Undo.cpp+++ b/src/core/Undo.cpp@@ -159,9 +159,9 @@ void UndoStack::apply(UndoRecord& r, Mesh& mesh, BVH& bvh) {return;}if (!r.positions.empty()) {- r.positions.swapWith(mesh.layerTarget(r.layer));+ r.positions.swapWithUnique(mesh.layerTarget(r.layer));for (size_t i = 0; i < r.layerOffsets.size() && i < mesh.layers.size(); ++i)- r.layerOffsets[i].swapWith(mesh.layers[i].delta);+ r.layerOffsets[i].swapWithUnique(mesh.layers[i].delta);mesh.evaluate(r.positions.indices);mesh.updateNormals(r.positions.indices);// Refits only grow boxes: after moving much of the mesh (a transform), tighten the whole tree instead.diff --git a/src/core/Undo.h b/src/core/Undo.hindex c7ffadc5..90ade228 100644--- a/src/core/Undo.h+++ b/src/core/Undo.h@@ -5,6 +5,7 @@#include "core/BVH.h"#include "core/Mesh.h"#include "core/Multires.h"+#include "core/ThreadPool.h"#include <deque>#include <memory>@@ -29,6 +30,12 @@ struct UndoChannel {void swapWith(std::vector<T>& target) {for (size_t i = 0; i < indices.size(); ++i) std::swap(target[indices[i]], values[i]);}+ // swapWith() in parallel, for channels that list each element once (positions: strokes, transforms).+ void swapWithUnique(std::vector<T>& target) {+ poolFor(indices.size(), [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) std::swap(target[indices[i]], values[i]);+ }, 1 << 14);+ }size_t bytes() const { return indices.size() * (sizeof(uint32_t) + sizeof(T)); }};@@ -88,7 +95,7 @@ inline constexpr int kActivePaintLayer = -2;// (topology/layer-set changes; the snapshot holds every attribute).// - positions: the stroke's target array (positions, layer base or a layer's// offsets; see Mesh::layerTarget), selected by `layer`; layerOffsets: every-// layer's offsets too (transforms)+// layer's offsets too (transforms). Each lists a vertex at most once.// - mask / colors: per-vertex mask and polypaint. `colors` holds the paint// target of `paintLayer` (Mesh::colors without paint layers, else Base for// -1 or that layer's colours, with its coverage in `paintAlpha`)