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.

Performance: benchmark (--bench), thread pool, parallel brushes / normals / BVH, partial GPU uploads

islandmed001000e98979
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4d94baab..85efcd11 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -27,6 +27,8 @@ include(cmake/Dependencies.cmake)
add_library(sculptforge_core STATIC
src/core/Mesh.cpp
src/core/BVH.cpp
+ src/core/ThreadPool.cpp
+ src/core/Benchmark.cpp
src/core/Sculpt.cpp
src/core/Undo.cpp
src/core/Primitives.cpp
@@ -91,7 +93,8 @@ if(MSVC)
else()
set_source_files_properties(third_party/ufbx/ufbx.c third_party/xatlas/xatlas.cpp third_party/mikktspace/mikktspace.c PROPERTIES COMPILE_OPTIONS "-w")
endif()
-target_link_libraries(sculptforge_core PUBLIC glm::glm)
+find_package(Threads REQUIRED)
+target_link_libraries(sculptforge_core PUBLIC glm::glm Threads::Threads)
target_compile_definitions(sculptforge_core PUBLIC GLM_FORCE_XYZW_ONLY)
target_compile_definitions(sculptforge_core PRIVATE SCULPTFORGE_VERSION="${PROJECT_VERSION}") # written into projects
if(MSVC)
@@ -145,6 +148,7 @@ if(SCULPTFORGE_BUILD_APP)
${CMAKE_CURRENT_BINARY_DIR}/EmbeddedFonts.cpp
${CMAKE_CURRENT_BINARY_DIR}/EmbeddedGuide.cpp
src/app/main.cpp
+ src/app/Bench.cpp
src/app/App.cpp
src/app/Demo.cpp
src/app/UiTest.cpp
diff --git a/src/app/App.h b/src/app/App.h
index 9e9316d6..1893de85 100644
--- a/src/app/App.h
+++ b/src/app/App.h
@@ -383,8 +383,9 @@ private:
void registerPaintUiCommands();
void paintAt(const DabParams& p);
void pickColorUnderCursor();
- // First paint stroke on a mesh without polypaint: undo restores this snapshot so the attribute goes away again.
- std::unique_ptr<Mesh> paintSnapshot_;
+ // First paint stroke on a mesh without polypaint: undo swaps this paint stack (no colours) back in, so the
+ // attribute goes away again.
+ std::unique_ptr<PaintStackUndo> paintSnapshot_;
void beginPaintStroke();
void finishPaintStroke(UndoRecord& record);
TexturePainter texturePainter_; // strokes into the SubTool's texture (core/Texture.h)
diff --git a/src/app/Bench.cpp b/src/app/Bench.cpp
new file mode 100644
index 00000000..b1e88a16
--- /dev/null
+++ b/src/app/Bench.cpp
@@ -0,0 +1,104 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// sculptforge --bench: the benchmark of core/Benchmark.h with the GPU side added (uploading what each dab
+// changed, and the frame time while orbiting), in a window the size of the app's default one.
+#include "app/Bench.h"
+
+#include "render/Camera.h"
+#include "render/GL.h"
+#include "render/Renderer.h"
+
+#include <SDL3/SDL.h>
+
+#include <chrono>
+#include <cmath>
+#include <cstdio>
+
+namespace sf {
+
+namespace {
+using Clock = std::chrono::steady_clock;
+double msSince(Clock::time_point t) { return std::chrono::duration<double, std::milli>(Clock::now() - t).count(); }
+} // namespace
+
+int runAppBenchmark(BenchCommandLine cl) {
+ if (!SDL_Init(SDL_INIT_VIDEO)) {
+ std::fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError());
+ return 1;
+ }
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
+ SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+ SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
+ SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
+ SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
+ SDL_Window* window = SDL_CreateWindow("SculptForge benchmark", 1600, 960, SDL_WINDOW_OPENGL | SDL_WINDOW_HIGH_PIXEL_DENSITY);
+ if (!window) {
+ std::fprintf(stderr, "SDL_CreateWindow failed: %s\n", SDL_GetError());
+ SDL_Quit();
+ return 1;
+ }
+ SDL_GLContext context = SDL_GL_CreateContext(window);
+ if (!context) {
+ std::fprintf(stderr, "OpenGL 3.3 context creation failed: %s\n", SDL_GetError());
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+ return 1;
+ }
+ SDL_GL_MakeCurrent(window, context);
+ SDL_GL_SetSwapInterval(0); // measure the frame, not the display's refresh
+ int code = 1;
+ Renderer renderer;
+ std::string error;
+ if (const char* missing = gl::load([](const char* n) { return reinterpret_cast<gl::GenericProc>(SDL_GL_GetProcAddress(n)); })) {
+ std::fprintf(stderr, "Missing OpenGL function: %s\n", missing);
+ } else if (!renderer.init(error)) {
+ std::fprintf(stderr, "%s\n", error.c_str());
+ } else {
+ BenchGpu gpu;
+ gpu.description = std::string(reinterpret_cast<const char*>(gl::GetString(GL_RENDERER))) + ", OpenGL " +
+ reinterpret_cast<const char*>(gl::GetString(GL_VERSION));
+ auto scene = [](Mesh& m) { return std::vector<SceneMesh>{SceneMesh{1, &m, true, true, nullptr}}; };
+ gpu.upload = [&](Mesh& m) {
+ SDL_PumpEvents();
+ const auto t = Clock::now();
+ renderer.syncScene(scene(m));
+ gl::Finish();
+ return msSince(t);
+ };
+ gpu.orbitFrameMs = [&](Mesh& m) {
+ int w = 0, h = 0;
+ SDL_GetWindowSizeInPixels(window, &w, &h);
+ Camera camera;
+ camera.aspect = float(w) / float(std::max(h, 1));
+ camera.frame(m.bounds());
+ RenderSettings settings;
+ renderer.syncScene(scene(m));
+ const int warmup = 5, frames = 120;
+ Clock::time_point t;
+ for (int i = 0; i < warmup + frames; ++i) {
+ if (i == warmup) {
+ gl::Finish();
+ t = Clock::now();
+ }
+ SDL_PumpEvents();
+ camera.yaw = float(i) * 6.2831853f / float(frames);
+ renderer.draw(camera, w, h, settings);
+ SDL_GL_SwapWindow(window);
+ }
+ gl::Finish();
+ return msSince(t) / frames;
+ };
+ code = runBenchCommandLine(cl, "## With the GPU (sculptforge --bench)\n\n", &gpu);
+ renderer.shutdown();
+ }
+ SDL_GL_DestroyContext(context);
+ SDL_DestroyWindow(window);
+ SDL_Quit();
+ return code;
+}
+
+} // namespace sf
diff --git a/src/app/Bench.h b/src/app/Bench.h
new file mode 100644
index 00000000..6e048406
--- /dev/null
+++ b/src/app/Bench.h
@@ -0,0 +1,12 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+#pragma once
+
+#include "core/Benchmark.h"
+
+namespace sf {
+
+// sculptforge --bench: runs core/Benchmark.h in a window with the GPU timings added; returns the exit code.
+int runAppBenchmark(BenchCommandLine cl);
+
+} // namespace sf
diff --git a/src/app/PanelPolypaint.cpp b/src/app/PanelPolypaint.cpp
index 45098588..9141648d 100644
--- a/src/app/PanelPolypaint.cpp
+++ b/src/app/PanelPolypaint.cpp
@@ -65,16 +65,17 @@ void App::beginPaintStroke() {
const PaintLayer& l = m.paintLayers[size_t(m.activePaintLayer)];
status_ = "Paint layer '" + l.name + "' is " + (l.locked ? "locked" : "hidden");
}
- if (strokePaint_ && !doc_.mesh.hasColors()) paintSnapshot_ = std::make_unique<Mesh>(doc_.mesh);
+ // (Only the paint stack: a copy of the whole mesh would be gigabytes at tens of millions of polygons.)
+ if (strokePaint_ && !doc_.mesh.hasColors()) paintSnapshot_ = std::make_unique<PaintStackUndo>(PaintStackUndo::capture(doc_.mesh));
}
void App::finishPaintStroke(UndoRecord& record) {
if (texturePainter_.active()) texturePainter_.end(record);
if (!paintSnapshot_) return;
- UndoRecord snapshot;
- snapshot.label = std::move(record.label);
- snapshot.mesh = std::move(paintSnapshot_);
- record = std::move(snapshot);
+ // The colours the stroke recorded are the white it started from; restoring "no polypaint" replaces them.
+ record.colors = {};
+ record.paintAlpha = {};
+ record.paint = std::move(paintSnapshot_);
}
void App::pickColorUnderCursor() {
diff --git a/src/app/main.cpp b/src/app/main.cpp
index 83d70f3b..876babc4 100644
--- a/src/app/main.cpp
+++ b/src/app/main.cpp
@@ -1,6 +1,7 @@
// SculptForge - open source digital sculpting
// SPDX-License-Identifier: GPL-3.0-or-later
#include "app/App.h"
+#include "app/Bench.h"
#include <SDL3/SDL_main.h>
@@ -34,7 +35,12 @@ void usage() {
" --supersample N Render recordings N times larger and downsample (sharper, allows zoom-ins)\n"
" --ui-test FILE.sfui Run a UI test script headless and exit (0 = pass); settings are isolated\n"
" --ui-out DIR Where UI tests save screenshots\n"
- " --fullscreen Start filling the screen (F11 toggles)");
+ " --fullscreen Start filling the screen (F11 toggles)\n"
+ " --bench Run the performance benchmark (with GPU upload and orbit timings) and exit\n"
+ " --bench-sizes 4,16,32 Benchmark sizes in millions of triangles\n"
+ " --bench-dabs N Dabs per benchmark stroke (default 40)\n"
+ " --bench-no-project Skip the project save / open (it needs twice the memory)\n"
+ " --bench-out FILE Write the benchmark report (Markdown) to FILE");
}
// UI tests must never read or change the user's real settings, recent files or
@@ -68,9 +74,11 @@ void isolateSettings() {
int main(int argc, char** argv) {
sf::AppOptions options;
bool sizeGiven = false;
+ sf::BenchCommandLine bench;
for (int i = 1; i < argc; ++i) {
std::string a = argv[i];
auto next = [&]() -> std::string { return i + 1 < argc ? argv[++i] : std::string(); };
+ if (sf::parseBenchArg(argc, argv, i, bench)) continue;
if (a == "--run") options.script += next() + "\n";
else if (a == "--render") options.renderPath = next();
else if (a == "--turntable") options.turntable = std::atoi(next().c_str());
@@ -99,6 +107,7 @@ int main(int argc, char** argv) {
return 1;
}
}
+ if (bench.run) return sf::runAppBenchmark(bench);
if (!options.uiTest.empty()) {
if (!sizeGiven) options.width = 1600, options.height = 960;
isolateSettings();
diff --git a/src/cli/main.cpp b/src/cli/main.cpp
index e7bb4805..ba7c8aa3 100644
--- a/src/cli/main.cpp
+++ b/src/cli/main.cpp
@@ -5,6 +5,7 @@
// pipelines and AI agents.
#include "ai/AiCommands.h"
#include "cli/McpServer.h"
+#include "core/Benchmark.h"
#include "core/BrushLibrary.h"
#include "core/Commands.h"
#include "core/Script.h"
@@ -30,6 +31,11 @@ void usage() {
" --schema Print commands as LLM tool definitions (JSON)\n"
" --mcp Run as an MCP server on stdio (Claude Code, Claude Desktop, any MCP client)\n"
" -q, --quiet Only print errors\n"
+ " --bench Run the performance benchmark (CPU side; the app's --bench adds the GPU)\n"
+ " --bench-sizes 4,16,32 Benchmark sizes in millions of triangles\n"
+ " --bench-dabs N Dabs per benchmark stroke (default 40)\n"
+ " --bench-no-project Skip the project save / open (it needs twice the memory)\n"
+ " --bench-out FILE Write the benchmark report (Markdown) to FILE\n"
" -h, --help Show this help\n"
"\n"
"Connect Claude Code: claude mcp add sculptforge -- sculptforge-cli --mcp\n"
@@ -47,6 +53,7 @@ int main(int argc, char** argv) {
std::string script;
bool quiet = false;
std::map<std::string, std::string> vars;
+ sf::BenchCommandLine bench;
if (argc < 2) {
usage();
@@ -92,6 +99,8 @@ int main(int argc, char** argv) {
} else if (a == "--schema") {
std::puts(registry.toolSchemaJson().c_str());
return 0;
+ } else if (sf::parseBenchArg(argc, argv, i, bench)) {
+ // handled after the loop
} else if (a == "-q" || a == "--quiet") {
quiet = true;
} else if (a == "-h" || a == "--help") {
@@ -104,6 +113,8 @@ int main(int argc, char** argv) {
}
}
+ if (bench.run) return sf::runBenchCommandLine(bench, "## CPU only (sculptforge-cli --bench)\n\n");
+
std::vector<sf::ScriptStatement> statements;
std::string error;
if (!sf::parseScript(script, statements, error)) {
diff --git a/src/core/BVH.cpp b/src/core/BVH.cpp
index be742caf..6719d26a 100644
--- a/src/core/BVH.cpp
+++ b/src/core/BVH.cpp
@@ -2,7 +2,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/BVH.h"
+#include "core/ThreadPool.h"
+
#include <algorithm>
+#include <atomic>
#include <cmath>
namespace sf {
@@ -55,46 +58,93 @@ void BVH::build(const Mesh& mesh) {
if (nt == 0) return;
std::vector<glm::vec3> centroids(nt);
- for (uint32_t i = 0; i < nt; ++i) {
- const auto& t = mesh.triangles[i];
- centroids[i] = (mesh.positions[t.x] + mesh.positions[t.y] + mesh.positions[t.z]) / 3.0f;
- triIndices_[i] = i;
- }
-
- nodes_.reserve(2 * (nt / kLeafSize + 1));
- nodes_.push_back(Node{AABB{}, 0, static_cast<uint32_t>(nt), 0});
-
- struct Task { uint32_t node; };
- std::vector<Task> tasks{{0}};
- while (!tasks.empty()) {
- uint32_t ni = tasks.back().node;
- tasks.pop_back();
- uint32_t first = nodes_[ni].first, count = nodes_[ni].count;
+ poolFor(nt, [&](size_t b, size_t e) {
+ for (size_t i = b; i < e; ++i) {
+ const auto& t = mesh.triangles[i];
+ centroids[i] = (mesh.positions[t.x] + mesh.positions[t.y] + mesh.positions[t.z]) / 3.0f;
+ triIndices_[i] = uint32_t(i);
+ }
+ }, 1 << 16);
+ // Splits node `ni` of `nodes` at the median centroid along its longest axis (pushing its two children), or
+ // leaves it a leaf. Only touches triIndices_ within the node's range, so subtrees can be split in parallel.
+ auto split = [&](std::vector<Node>& nodes, uint32_t ni) -> bool {
+ const uint32_t first = nodes[ni].first, count = nodes[ni].count;
AABB cb;
for (uint32_t i = first; i < first + count; ++i) cb.expand(centroids[triIndices_[i]]);
-
- glm::vec3 ext = cb.extent();
- int axis = ext.x > ext.y ? (ext.x > ext.z ? 0 : 2) : (ext.y > ext.z ? 1 : 2);
- if (count <= kLeafSize || ext[axis] <= 0.0f) {
- recomputeLeaf(mesh, nodes_[ni]);
- continue;
- }
-
- uint32_t mid = first + count / 2;
+ const glm::vec3 ext = cb.extent();
+ const int axis = ext.x > ext.y ? (ext.x > ext.z ? 0 : 2) : (ext.y > ext.z ? 1 : 2);
+ if (count <= kLeafSize || ext[axis] <= 0.0f) return false;
+ const uint32_t mid = first + count / 2;
std::nth_element(triIndices_.begin() + first, triIndices_.begin() + mid, triIndices_.begin() + first + count,
[&](uint32_t a, uint32_t b) { return centroids[a][axis] < centroids[b][axis]; });
+ const uint32_t left = static_cast<uint32_t>(nodes.size());
+ nodes.push_back(Node{AABB{}, first, mid - first, ni});
+ nodes.push_back(Node{AABB{}, mid, first + count - mid, ni});
+ nodes[ni].first = left;
+ nodes[ni].count = 0;
+ return true;
+ };
+ // Splits `nodes` depth first from the nodes in `tasks`, until only leaves are left.
+ auto buildFrom = [&](std::vector<Node>& nodes, std::vector<uint32_t> tasks) {
+ while (!tasks.empty()) {
+ const uint32_t ni = tasks.back();
+ tasks.pop_back();
+ if (!split(nodes, ni)) continue;
+ tasks.push_back(nodes[ni].first);
+ tasks.push_back(nodes[ni].first + 1);
+ }
+ };
- uint32_t left = static_cast<uint32_t>(nodes_.size());
- nodes_.push_back(Node{AABB{}, first, mid - first, ni});
- nodes_.push_back(Node{AABB{}, mid, first + count - mid, ni});
- nodes_[ni].first = left;
- nodes_[ni].count = 0;
- tasks.push_back({left});
- tasks.push_back({left + 1});
+ nodes_.reserve(2 * (nt / kLeafSize + 1));
+ nodes_.push_back(Node{AABB{}, 0, static_cast<uint32_t>(nt), 0});
+ if (nt < (size_t(1) << 18) || ThreadPool::shared().threads() == 1 || ThreadPool::shared().serial()) {
+ buildFrom(nodes_, {0});
+ } else {
+ // Split the top breadth first until there is work for every thread, then build those subtrees in
+ // parallel (each into its own node list) and append them. The splits are the same as a serial build's,
+ // and children still follow their parents with the right child next to the left one.
+ std::vector<uint32_t> open{0};
+ const size_t want = size_t(ThreadPool::shared().threads()) * 8;
+ while (open.size() < want) {
+ std::vector<uint32_t> next;
+ for (uint32_t ni : open)
+ if (split(nodes_, ni)) {
+ next.push_back(nodes_[ni].first);
+ next.push_back(nodes_[ni].first + 1);
+ }
+ if (next.empty()) break;
+ open = std::move(next);
+ }
+ std::vector<std::vector<Node>> sub(open.size());
+ poolFor(open.size(), [&](size_t b, size_t e) {
+ for (size_t k = b; k < e; ++k) {
+ const Node& root = nodes_[open[k]];
+ sub[k].push_back(Node{AABB{}, root.first, root.count, 0});
+ if (root.count > 0) buildFrom(sub[k], {0});
+ }
+ }, 1);
+ for (size_t k = 0; k < open.size(); ++k) {
+ // Local node i >= 1 becomes base + i - 1; local 0 is the subtree's root, nodes_[open[k]].
+ const uint32_t root = open[k], base = static_cast<uint32_t>(nodes_.size());
+ auto global = [&](uint32_t i) { return i == 0 ? root : base + i - 1; };
+ const std::vector<Node>& local = sub[k];
+ nodes_[root].first = local[0].count > 0 ? local[0].first : global(local[0].first);
+ nodes_[root].count = local[0].count;
+ for (size_t i = 1; i < local.size(); ++i) {
+ Node n = local[i];
+ n.parent = global(n.parent);
+ if (n.count == 0) n.first = global(n.first);
+ nodes_.push_back(n);
+ }
+ }
}
- // Inner node bounds, bottom-up (children always have larger indices).
+ // Leaf bounds (and each triangle's leaf), then inner bounds bottom-up (children have larger indices).
+ poolFor(nodes_.size(), [&](size_t b, size_t e) {
+ for (size_t i = b; i < e; ++i)
+ if (nodes_[i].count > 0) recomputeLeaf(mesh, nodes_[i]);
+ }, 1 << 12);
for (size_t i = nodes_.size(); i-- > 0;) {
Node& n = nodes_[i];
if (n.count == 0) {
@@ -232,18 +282,39 @@ void BVH::refit(const Mesh& mesh, std::span<const uint32_t> movedVerts) {
if (nodes_.empty()) return;
const uint32_t s = nextStamp(leafStamp_, leafStampValue_);
scratchLeaves_.clear();
- 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);
+ 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 (std::atomic_ref<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]]);
+ }, 512);
+ // Grow ancestors until one already contains the new bounds (a union, so the order doesn't matter).
for (uint32_t li : scratchLeaves_) {
- recomputeLeaf(mesh, nodes_[li]);
- // Grow ancestors until one already contains the new bounds.
AABB box = nodes_[li].box;
uint32_t ni = li;
while (ni != 0) {
@@ -257,6 +328,12 @@ 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_);
+}
+
void BVH::refitAll(const Mesh& mesh) {
for (size_t i = nodes_.size(); i-- > 0;) { // children always have larger indices
Node& n = nodes_[i];
diff --git a/src/core/BVH.h b/src/core/BVH.h
index e006d43f..46a3f39e 100644
--- a/src/core/BVH.h
+++ b/src/core/BVH.h
@@ -35,6 +35,7 @@ public:
void refitAll(const Mesh& mesh);
size_t nodeCount() const { return nodes_.size(); }
+ size_t memoryBytes() const;
private:
struct Node {
@@ -56,6 +57,7 @@ private:
std::vector<uint32_t> leafStamp_;
uint32_t leafStampValue_ = 0;
std::vector<uint32_t> scratchLeaves_;
+ std::vector<std::vector<uint32_t>> scratchParts_;
mutable std::vector<uint32_t> stack_;
};
diff --git a/src/core/Benchmark.cpp b/src/core/Benchmark.cpp
new file mode 100644
index 00000000..752c64b3
--- /dev/null
+++ b/src/core/Benchmark.cpp
@@ -0,0 +1,326 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "core/Benchmark.h"
+
+#include "core/BrushRecipe.h"
+#include "core/Document.h"
+#include "core/Multires.h"
+#include "core/Polypaint.h"
+#include "core/Primitives.h"
+#include "core/Project.h"
+#include "core/ThreadPool.h"
+
+#include <algorithm>
+#include <chrono>
+#include <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <filesystem>
+#include <sstream>
+
+#if defined(__linux__)
+#include <unistd.h>
+#include <fstream>
+#endif
+
+namespace sf {
+
+size_t residentBytes() {
+#if defined(__linux__)
+ std::ifstream f("/proc/self/statm");
+ size_t pages = 0, resident = 0;
+ if (f >> pages >> resident) return resident * size_t(sysconf(_SC_PAGESIZE));
+#endif
+ return 0;
+}
+
+Mesh makeBenchSphere(size_t triangles) {
+ // A cube of r x r quads per side has 12 r^2 triangles; each Divide multiplies that by 4.
+ int levels = 1, res = 1;
+ for (; levels <= 8; ++levels) {
+ res = std::max(1, int(std::lround(std::sqrt(double(triangles) / (12.0 * std::pow(4.0, levels))))));
+ if (res <= 40) break;
+ }
+ Mesh m = makeCube(res, 2.0f);
+ for (auto& p : m.positions) p = glm::normalize(p);
+ m.recomputeAllNormals();
+ std::string error;
+ for (int i = 0; i < levels; ++i)
+ if (!subdivDivide(m, true, error, nullptr, size_t(1) << 40)) break;
+ return m;
+}
+
+namespace {
+
+using Clock = std::chrono::steady_clock;
+
+double msSince(Clock::time_point t) { return std::chrono::duration<double, std::milli>(Clock::now() - t).count(); }
+
+enum class Kind { Brush, Recipe, Move, Mask, Paint };
+
+struct BenchBrush {
+ std::string name;
+ Kind kind = Kind::Brush;
+ BrushType type = BrushType::Standard;
+ float radius = 0.12f; // world units on a unit sphere: ~64 px on a 1080p view framing the model
+};
+
+struct StrokeStats {
+ int dabs = 0;
+ double verts = 0, kernel = 0, normals = 0, bvh = 0, pick = 0, upload = 0, total = 0;
+ double endStroke = 0, undo = 0, redo = 0;
+ size_t undoBytes = 0;
+};
+
+StrokeStats runStroke(Document& doc, const BenchBrush& b, int dabs, const BenchGpu* gpu) {
+ StrokeStats st;
+ Mesh& m = doc.mesh;
+ Sculptor& sc = doc.sculptor;
+ const std::vector<BrushRecipe> recipes = builtinBrushRecipes();
+ const BrushRecipe* recipe = nullptr;
+ for (const auto& r : recipes)
+ if (r.pattern != BrushRecipe::Pattern::None) recipe = &r;
+ if (!recipe && !recipes.empty()) recipe = &recipes.front();
+ if (b.kind == Kind::Paint) m.ensureColors();
+ sc.beginStroke(m, b.name);
+ sc.timings = {};
+ Symmetry sym; // X on, as in the app
+ // Dabs 0.15 radius apart (the app's spacing) along a line across the front of the sphere.
+ const float step = 0.15f * b.radius;
+ glm::vec3 last(0.0f);
+ const auto stroke0 = Clock::now();
+ for (int i = 0; i < dabs; ++i) {
+ const float x = 0.15f + std::fmod(float(i) * step, 0.6f), y = 0.2f + 0.1f * std::sin(float(i) * 0.3f);
+ auto t = Clock::now();
+ RayHit hit;
+ const bool hitOk = doc.bvh.raycast(m, glm::vec3(x, y, 5.0f), glm::vec3(0, 0, -1), hit);
+ st.pick += msSince(t);
+ if (!hitOk) continue;
+ DabParams p;
+ p.center = hit.position;
+ p.viewDir = glm::vec3(0, 0, -1);
+ p.radius = b.radius;
+ p.strength = 0.5f;
+ p.strokeDelta = i ? hit.position - last : glm::vec3(0.0f);
+ last = hit.position;
+ switch (b.kind) {
+ case Kind::Brush: sc.dab(m, doc.bvh, b.type, p, sym); break;
+ case Kind::Recipe:
+ if (recipe) sc.dabRecipe(m, doc.bvh, *recipe, p, sym);
+ break;
+ case Kind::Move:
+ if (i == 0) sc.beginGrab(m, doc.bvh, p, sym);
+ else sc.grab(m, doc.bvh, glm::vec3(0.002f, 0.0015f, 0.0f));
+ break;
+ case Kind::Mask: sc.dabMask(m, doc.bvh, p, sym); break;
+ case Kind::Paint: paintDab(sc, m, doc.bvh, p, glm::vec3(0.6f, 0.1f, 0.1f), sym); break;
+ }
+ if (gpu && gpu->upload) st.upload += gpu->upload(m);
+ ++st.dabs;
+ }
+ st.total = msSince(stroke0);
+ st.kernel = sc.timings.kernelMs;
+ st.normals = sc.timings.normalsMs;
+ st.bvh = sc.timings.bvhMs;
+ st.verts = double(sc.timings.vertices) / double(std::max(st.dabs, 1)); // per step (both mirrors)
+
+ auto t = Clock::now();
+ UndoRecord r = sc.endStroke();
+ st.undoBytes = r.approxBytes();
+ doc.undo.push(std::move(r));
+ st.endStroke = msSince(t);
+ t = Clock::now();
+ doc.undo.undo(m, doc.bvh);
+ st.undo = msSince(t);
+ if (gpu && gpu->upload) gpu->upload(m);
+ t = Clock::now();
+ doc.undo.redo(m, doc.bvh);
+ st.redo = msSince(t);
+ if (gpu && gpu->upload) gpu->upload(m);
+ return st;
+}
+
+std::string fmt(const char* f, double v) {
+ char buf[64];
+ std::snprintf(buf, sizeof buf, f, v);
+ return buf;
+}
+
+std::string millions(double n) { return n >= 1e6 ? fmt("%.1fM", n / 1e6) : fmt("%.0fk", n / 1e3); }
+
+} // namespace
+
+bool parseBenchArg(int argc, char** argv, int& i, BenchCommandLine& cl) {
+ const std::string a = argv[i];
+ auto next = [&]() -> std::string { return i + 1 < argc ? argv[++i] : std::string(); };
+ if (a == "--bench") {
+ cl.run = true;
+ } else if (a == "--bench-sizes") {
+ cl.options.sizesMillions.clear();
+ std::stringstream ss(next());
+ for (std::string item; std::getline(ss, item, ',');)
+ if (const double v = std::atof(item.c_str()); v > 0.0) cl.options.sizesMillions.push_back(v);
+ } else if (a == "--bench-dabs") {
+ cl.options.dabsPerStroke = std::max(1, std::atoi(next().c_str()));
+ } else if (a == "--bench-no-project") {
+ cl.options.projectRoundTrip = false;
+ } else if (a == "--bench-out") {
+ cl.outPath = next();
+ } else {
+ return false;
+ }
+ return true;
+}
+
+int runBenchCommandLine(BenchCommandLine cl, const std::string& heading, const BenchGpu* gpu) {
+ bool writeFailed = false;
+ auto write = [&](const std::string& report) {
+ FILE* f = std::fopen(cl.outPath.c_str(), "wb");
+ if (!f) {
+ writeFailed = true;
+ return;
+ }
+ std::fputs((heading + report).c_str(), f);
+ std::fclose(f);
+ };
+ cl.options.log = [](const std::string& line) { std::fprintf(stderr, "%s\n", line.c_str()); };
+ if (!cl.outPath.empty()) cl.options.partial = write;
+ const std::string report = runBenchmark(cl.options, gpu);
+ if (cl.outPath.empty()) {
+ std::fputs((heading + report).c_str(), stdout);
+ return 0;
+ }
+ write(report);
+ if (writeFailed) {
+ std::fprintf(stderr, "error: cannot write %s\n", cl.outPath.c_str());
+ return 1;
+ }
+ std::printf("Benchmark written to %s\n", cl.outPath.c_str());
+ return 0;
+}
+
+std::string runBenchmark(const BenchOptions& o, const BenchGpu* gpu) {
+ auto log = [&](const std::string& s) {
+ if (o.log) o.log(s);
+ };
+ std::ostringstream md;
+ md << "Threads: " << ThreadPool::shared().threads();
+ if (gpu && !gpu->description.empty()) md << ", GPU: " << gpu->description;
+ md << ". Brushes use X symmetry (two dabs per step), radius " << 0.12 << " on a unit sphere unless noted, "
+ << o.dabsPerStroke << " dabs per stroke. Times in milliseconds.\n\n";
+
+ const std::vector<BenchBrush> brushes = {
+ {"Standard", Kind::Brush, BrushType::Standard},
+ {"Standard (large, r 0.4)", Kind::Brush, BrushType::Standard, 0.4f},
+ {"Clay", Kind::Brush, BrushType::Clay},
+ {"ClayBuildup", Kind::Brush, BrushType::ClayBuildup},
+ {"Smooth", Kind::Brush, BrushType::Smooth},
+ {"Inflate", Kind::Brush, BrushType::Inflate},
+ {"DamStandard", Kind::Brush, BrushType::DamStandard},
+ {"Layer", Kind::Brush, BrushType::Layer},
+ {"Move", Kind::Move, BrushType::Move},
+ {"Recipe (pattern)", Kind::Recipe},
+ {"Mask", Kind::Mask},
+ {"Paint", Kind::Paint},
+ };
+
+ for (double sizeM : o.sizesMillions) {
+ const size_t target = size_t(sizeM * 1e6);
+ log("Building a " + millions(double(target)) + " triangle sphere...");
+ Document doc;
+ const size_t rss0 = residentBytes();
+ auto t = Clock::now();
+ doc.mesh = makeBenchSphere(target);
+ const double divideMs = msSince(t);
+ t = Clock::now();
+ doc.bvh.build(doc.mesh);
+ const double bvhMs = msSince(t);
+ Mesh& m = doc.mesh;
+ const double nt = double(m.triangleCount()), nv = double(m.vertexCount());
+ double uploadAll = 0;
+ if (gpu && gpu->upload) uploadAll = gpu->upload(m);
+ const size_t meshBytes = m.memoryBytes(), bvhBytes = doc.bvh.memoryBytes(), stackBytes = subdivBytes(m);
+ const size_t rss = residentBytes();
+
+ md << "### " << millions(nt) << " triangles (" << millions(nv) << " vertices, "
+ << subdivLevelCount(m) << " subdivision levels)\n\n";
+ md << "| Setup | |\n|---|---|\n";
+ md << "| Divide up to this size | " << fmt("%.0f", divideMs) << " |\n";
+ md << "| BVH build | " << fmt("%.0f", bvhMs) << " |\n";
+ if (gpu) md << "| First GPU upload | " << fmt("%.0f", uploadAll) << " |\n";
+ md << "| Mesh (incl. topology) | " << fmt("%.0f MB", double(meshBytes) / 1e6) << " = "
+ << fmt("%.0f", double(meshBytes) / nt) << " B/triangle |\n";
+ md << "| BVH | " << fmt("%.0f MB", double(bvhBytes) / 1e6) << " = " << fmt("%.1f", double(bvhBytes) / nt)
+ << " B/triangle |\n";
+ md << "| Subdivision levels (stack) | " << fmt("%.0f MB", double(stackBytes) / 1e6) << " = "
+ << fmt("%.0f", double(stackBytes) / nt) << " B/triangle |\n";
+ if (rss) md << "| Process memory (RSS) | " << fmt("%.0f MB", double(rss) / 1e6) << " (+" << fmt("%.0f", double(rss - std::min(rss, rss0)) / 1e6) << " MB) |\n";
+ md << "\n";
+
+ md << "| Brush | verts/dab | kernel | normals | BVH refit | pick | " << (gpu ? "GPU upload | " : "")
+ << "**dab total** | stroke end (undo record) | undo | redo | undo step |\n";
+ md << "|---|---:|---:|---:|---:|---:|" << (gpu ? "---:|" : "") << "---:|---:|---:|---:|---:|\n";
+ for (const BenchBrush& b : brushes) {
+ log(" " + b.name);
+ const StrokeStats s = runStroke(doc, b, o.dabsPerStroke, gpu);
+ const double d = std::max(s.dabs, 1);
+ md << "| " << b.name << " | " << millions(s.verts) << " | " << fmt("%.2f", s.kernel / d) << " | "
+ << fmt("%.2f", s.normals / d) << " | " << fmt("%.2f", s.bvh / d) << " | " << fmt("%.2f", s.pick / d) << " | ";
+ if (gpu) md << fmt("%.2f", s.upload / d) << " | ";
+ md << "**" << fmt("%.2f", s.total / d) << "** | " << fmt("%.2f", s.endStroke) << " | " << fmt("%.1f", s.undo)
+ << " | " << fmt("%.1f", s.redo) << " | " << fmt("%.1f MB", double(s.undoBytes) / 1e6) << " |\n";
+ }
+ md << "\n";
+
+ md << "| Operation | ms |\n|---|---:|\n";
+ if (subdivLevelCount(m) > 1) {
+ std::string err;
+ const int top = subdivCurrentLevel(m);
+ log(" subdivision levels");
+ t = Clock::now();
+ subdivSetLevel(m, top - 1, err, &doc.bvh);
+ md << "| Lower Res (to " << millions(double(m.triangleCount())) << ", after edits) | " << fmt("%.0f", msSince(t)) << " |\n";
+ if (gpu && gpu->upload) gpu->upload(m);
+ t = Clock::now();
+ subdivSetLevel(m, top, err, &doc.bvh);
+ md << "| Higher Res (back up) | " << fmt("%.0f", msSince(t)) << " |\n";
+ if (gpu && gpu->upload) gpu->upload(m);
+ t = Clock::now();
+ subdivSetLevel(m, top - 1, err, &doc.bvh);
+ md << "| Lower Res again (no edits) | " << fmt("%.0f", msSince(t)) << " |\n";
+ t = Clock::now();
+ subdivSetLevel(m, top, err, &doc.bvh);
+ md << "| Higher Res again (no edits) | " << fmt("%.0f", msSince(t)) << " |\n";
+ if (gpu && gpu->upload) gpu->upload(m);
+ }
+ if (o.projectRoundTrip) {
+ namespace fs = std::filesystem;
+ std::error_code ec;
+ const fs::path dir = o.projectDir.empty() ? fs::temp_directory_path(ec) : fs::path(o.projectDir);
+ const std::string path = (dir / "sculptforge-bench.sfproj").string();
+ std::string err;
+ log(" project save / open");
+ t = Clock::now();
+ const bool saved = saveProject(path, doc, err);
+ const double saveMs = msSince(t);
+ const double fileMb = saved ? double(fs::file_size(path, ec)) / 1e6 : 0.0;
+ Document loaded;
+ t = Clock::now();
+ const bool opened = saved && loadProject(path, loaded, err);
+ const double openMs = msSince(t);
+ fs::remove(path, ec);
+ md << "| Save project (" << fmt("%.0f MB", fileMb) << ") | " << (saved ? fmt("%.0f", saveMs) : "failed: " + err) << " |\n";
+ md << "| Open project | " << (opened ? fmt("%.0f", openMs) : "failed: " + err) << " |\n";
+ }
+ if (gpu && gpu->orbitFrameMs) {
+ log(" orbit");
+ const double f = gpu->orbitFrameMs(m);
+ md << "| Orbit: frame time | " << fmt("%.1f", f) << " (" << fmt("%.0f", 1000.0 / std::max(f, 1e-3)) << " fps) |\n";
+ }
+ md << "\n";
+ if (o.partial) o.partial(md.str());
+ }
+ return md.str();
+}
+
+} // namespace sf
diff --git a/src/core/Benchmark.h b/src/core/Benchmark.h
new file mode 100644
index 00000000..0cefc3d0
--- /dev/null
+++ b/src/core/Benchmark.h
@@ -0,0 +1,62 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Performance benchmark (sculptforge-cli --bench, sculptforge --bench; numbers in docs/performance.md).
+//
+// For each size it builds a sphere the way a sculptor gets one (a quad cube projected onto a sphere, then
+// Divide up to the size, so the mesh has subdivision levels) and measures, per dab and per stroke: the brush
+// kernel, normal update, BVH refit and undo record for every kind of brush (with X symmetry, as in the app),
+// plus opening / saving the project and stepping subdivision levels. The app adds what needs a GPU through
+// BenchGpu: uploading what a dab changed and the frame time while orbiting.
+#pragma once
+
+#include "core/Mesh.h"
+
+#include <cstddef>
+#include <functional>
+#include <string>
+#include <vector>
+
+namespace sf {
+
+struct BenchGpu {
+ // Uploads what the mesh marked dirty and waits for the GPU; returns milliseconds. First call: everything.
+ std::function<double(Mesh&)> upload;
+ // Average milliseconds per frame over a turn around the mesh (already uploaded).
+ std::function<double(Mesh&)> orbitFrameMs;
+ std::string description; // GPU / driver, for the report
+};
+
+struct BenchOptions {
+ std::vector<double> sizesMillions{4, 16, 32}; // triangle counts, in millions
+ int dabsPerStroke = 40;
+ std::string projectDir; // where the project round trip writes (default: the system temp folder)
+ bool projectRoundTrip = true;
+ std::function<void(const std::string&)> log; // progress lines (optional)
+ std::function<void(const std::string&)> partial; // the report so far, after each size (optional)
+};
+
+// Command-line options shared by sculptforge-cli and the app: --bench, --bench-sizes 4,16,32 (millions of
+// triangles), --bench-dabs N, --bench-no-project (skip saving / opening, which needs twice the memory),
+// --bench-out FILE. parseBenchArg returns true when argv[i] was one of them
+// (advancing i past its value).
+struct BenchCommandLine {
+ bool run = false;
+ BenchOptions options;
+ std::string outPath;
+};
+bool parseBenchArg(int argc, char** argv, int& i, BenchCommandLine& cl);
+// Runs the benchmark for the command line: progress on stderr, the report (under `heading`) to cl.outPath
+// (rewritten after each size, so a long run keeps what it measured) or stdout. Returns the exit code.
+int runBenchCommandLine(BenchCommandLine cl, const std::string& heading, const BenchGpu* gpu = nullptr);
+
+// Runs the benchmark and returns the report as Markdown tables.
+std::string runBenchmark(const BenchOptions& options, const BenchGpu* gpu = nullptr);
+
+// The benchmark mesh: about `triangles` triangles, with subdivision levels.
+Mesh makeBenchSphere(size_t triangles);
+
+// Resident memory of this process in bytes (0 where unknown).
+size_t residentBytes();
+
+} // namespace sf
diff --git a/src/core/Masking.cpp b/src/core/Masking.cpp
index dd80a92e..436164df 100644
--- a/src/core/Masking.cpp
+++ b/src/core/Masking.cpp
@@ -5,6 +5,7 @@
#include "core/Commands.h"
#include "core/ScreenSelect.h"
#include "core/Sculpt.h"
+#include "core/ThreadPool.h"
#include <algorithm>
#include <cmath>
@@ -174,21 +175,35 @@ void Sculptor::dabMask(Mesh& mesh, const BVH& bvh, const DabParams& p, Symmetry
if (!active_) return;
const float s = std::clamp(p.strength, 0.0f, 1.0f) * std::clamp(p.pressure, 0.0f, 1.0f);
if (s <= 0.0f) return;
+ auto clock = std::chrono::steady_clock::now();
std::vector<float>& mask = mesh.ensureMask();
for (const glm::vec3& mirror : mirrors(sym)) {
const glm::vec3 center = p.center * mirror, viewDir = p.viewDir * mirror;
brushVertices(mesh, bvh, center, p.radius, verts_);
- for (uint32_t v : verts_) {
- if (p.frontFacesOnly && glm::dot(mesh.normals[v], viewDir) > 0.0f) continue;
- const float a = s * brushFalloff(glm::distance(mesh.positions[v], center), p.radius);
- if (a <= 0.0f) continue;
- const float m = p.invert ? mask[v] * (1.0f - a) : mask[v] + (1.0f - mask[v]) * a;
- if (m == mask[v]) continue;
+ // New values in parallel; recording and writing them in order.
+ weights_.resize(verts_.size());
+ poolFor(verts_.size(), [&](size_t b, size_t e) {
+ for (size_t i = b; i < e; ++i) {
+ const uint32_t v = verts_[i];
+ weights_[i] = -1.0f; // unchanged
+ if (p.frontFacesOnly && glm::dot(mesh.normals[v], viewDir) > 0.0f) continue;
+ const float a = s * brushFalloff(glm::distance(mesh.positions[v], center), p.radius);
+ if (a <= 0.0f) continue;
+ const float m = p.invert ? mask[v] * (1.0f - a) : mask[v] + (1.0f - mask[v]) * a;
+ if (m != mask[v]) weights_[i] = m;
+ }
+ });
+ for (size_t i = 0; i < verts_.size(); ++i) {
+ if (weights_[i] < 0.0f) continue;
+ const uint32_t v = verts_[i];
recordMask(mesh, v);
- mask[v] = m;
+ mask[v] = weights_[i];
mesh.markDirty(v);
}
+ timings.vertices += verts_.size();
}
+ timings.kernelMs += lapMs(clock);
+ ++timings.dabs;
}
void registerMaskingCommands(CommandRegistry& registry) {
diff --git a/src/core/Mesh.cpp b/src/core/Mesh.cpp
index 0abd4bba..65395b7c 100644
--- a/src/core/Mesh.cpp
+++ b/src/core/Mesh.cpp
@@ -2,8 +2,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/Mesh.h"
#include "core/Multires.h"
+#include "core/ThreadPool.h"
#include <algorithm>
+#include <atomic>
#include <cmath>
#include <unordered_map>
@@ -135,86 +137,166 @@ void Mesh::rebuildTopology() {
for (int c = 0; c < 3; ++c) vtList_[cursor[triangles[ti][c]]++] = ti;
}
+ // Rings (sorted, unique) per vertex: sized in one parallel pass, written in a second.
adjOffsets_.assign(nv + 1, 0);
- adjList_.clear();
- adjList_.reserve(vtList_.size() * 2);
- std::vector<uint32_t> ring;
- for (uint32_t v = 0; v < nv; ++v) {
- ring.clear();
+ auto ring = [&](uint32_t v, std::vector<uint32_t>& out) {
+ out.clear();
for (uint32_t ti : vertexTriangles(v))
for (int c = 0; c < 3; ++c)
- if (triangles[ti][c] != v) ring.push_back(triangles[ti][c]);
- std::sort(ring.begin(), ring.end());
- ring.erase(std::unique(ring.begin(), ring.end()), ring.end());
- adjList_.insert(adjList_.end(), ring.begin(), ring.end());
- adjOffsets_[v + 1] = static_cast<uint32_t>(adjList_.size());
- }
+ if (triangles[ti][c] != v) out.push_back(triangles[ti][c]);
+ std::sort(out.begin(), out.end());
+ out.erase(std::unique(out.begin(), out.end()), out.end());
+ };
+ poolFor(nv, [&](size_t b, size_t e) {
+ std::vector<uint32_t> r;
+ for (size_t v = b; v < e; ++v) {
+ ring(uint32_t(v), r);
+ adjOffsets_[v + 1] = uint32_t(r.size());
+ }
+ });
+ for (size_t i = 0; i < nv; ++i) adjOffsets_[i + 1] += adjOffsets_[i];
+ std::vector<uint32_t>().swap(adjList_); // exact size, no spare capacity
+ adjList_.resize(adjOffsets_[nv]);
+ poolFor(nv, [&](size_t b, size_t e) {
+ std::vector<uint32_t> r;
+ for (size_t v = b; v < e; ++v) {
+ ring(uint32_t(v), r);
+ std::copy(r.begin(), r.end(), adjList_.begin() + adjOffsets_[v]);
+ }
+ });
- faceNormals_.resize(triangles.size());
- triStamp_.assign(triangles.size(), 0);
vertStamp_.assign(nv, 0);
stamp_ = 0;
normals.resize(nv);
+ dirtyBlocks.assign((nv + kDirtyBlock - 1) / kDirtyBlock, 0);
+ if (dirtyBegin < dirtyEnd) markDirtyRange(dirtyBegin, std::min(dirtyEnd, uint32_t(nv)));
topologyDirty = true;
facesDirty = true;
}
uint32_t Mesh::nextStamp() {
if (++stamp_ == 0) {
- std::fill(triStamp_.begin(), triStamp_.end(), 0);
std::fill(vertStamp_.begin(), vertStamp_.end(), 0);
stamp_ = 1;
}
return stamp_;
}
+void Mesh::growDirtyBlocks(size_t count) {
+ if (dirtyBlocks.size() < count) dirtyBlocks.resize(count, 0);
+}
+
+void Mesh::markDirtyRange(uint32_t begin, uint32_t end) {
+ if (begin >= end) return;
+ dirtyBegin = std::min(dirtyBegin, begin);
+ dirtyEnd = std::max(dirtyEnd, end);
+ const size_t last = (end - 1) >> kDirtyBlockShift;
+ growDirtyBlocks(last + 1);
+ std::fill(dirtyBlocks.begin() + (begin >> kDirtyBlockShift), dirtyBlocks.begin() + last + 1, uint8_t(1));
+}
+
+void Mesh::clearDirty() {
+ if (dirtyBegin < dirtyEnd) {
+ const size_t b = dirtyBegin >> kDirtyBlockShift, e = std::min(dirtyBlocks.size(), size_t((dirtyEnd - 1) >> kDirtyBlockShift) + 1);
+ if (b < e) std::fill(dirtyBlocks.begin() + b, dirtyBlocks.begin() + e, uint8_t(0));
+ }
+ dirtyBegin = std::numeric_limits<uint32_t>::max();
+ dirtyEnd = 0;
+ topologyDirty = false;
+ facesDirty = false;
+}
+
+void Mesh::dirtyRuns(std::vector<std::pair<uint32_t, uint32_t>>& out, uint32_t mergeGap) const {
+ out.clear();
+ const uint32_t end = std::min(dirtyEnd, uint32_t(positions.size()));
+ if (dirtyBegin >= end) return;
+ const size_t lastBlock = std::min(dirtyBlocks.size(), size_t((end - 1) >> kDirtyBlockShift) + 1);
+ for (size_t b = dirtyBegin >> kDirtyBlockShift; b < lastBlock; ++b) {
+ if (!dirtyBlocks[b]) continue;
+ const uint32_t lo = std::max(dirtyBegin, uint32_t(b << kDirtyBlockShift));
+ const uint32_t hi = std::min(end, uint32_t((b + 1) << kDirtyBlockShift));
+ if (!out.empty() && lo - out.back().second <= mergeGap) out.back().second = hi;
+ else out.emplace_back(lo, hi);
+ }
+}
+
void Mesh::recomputeAllNormals() {
- faceNormals_.resize(triangles.size());
- for (size_t i = 0; i < triangles.size(); ++i) faceNormals_[i] = faceNormal(positions, triangles[i]);
+ // Area-weighted face normals summed in triangle order (each vertex's sum in the same order as
+ // updateNormals adds them); the faces and the normalising run in parallel.
+ std::vector<glm::vec3> faces(triangles.size());
+ poolFor(triangles.size(), [&](size_t b, size_t e) {
+ for (size_t i = b; i < e; ++i) faces[i] = faceNormal(positions, triangles[i]);
+ }, 1 << 16);
normals.assign(positions.size(), glm::vec3(0.0f));
for (size_t i = 0; i < triangles.size(); ++i)
- for (int c = 0; c < 3; ++c) normals[triangles[i][c]] += faceNormals_[i];
- for (auto& n : normals) {
- float len = glm::length(n);
- n = len > 1e-20f ? n / len : glm::vec3(0, 1, 0);
- }
- if (!positions.empty()) {
- dirtyBegin = 0;
- dirtyEnd = static_cast<uint32_t>(positions.size());
- }
+ for (int c = 0; c < 3; ++c) normals[triangles[i][c]] += faces[i];
+ poolFor(normals.size(), [&](size_t b, size_t e) {
+ for (size_t v = b; v < e; ++v) {
+ glm::vec3& n = normals[v];
+ float len = glm::length(n);
+ n = len > 1e-20f ? n / len : glm::vec3(0, 1, 0);
+ }
+ }, 1 << 16);
+ markAllDirty();
}
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).
+ const size_t nv = positions.size();
+ if (vertStamp_.size() != nv) return; // no topology
+ growDirtyBlocks((nv + kDirtyBlock - 1) / kDirtyBlock);
const uint32_t s = nextStamp();
-
- scratchTris_.clear();
- for (uint32_t v : verts) {
- markDirty(v);
- for (uint32_t ti : vertexTriangles(v)) {
- if (triStamp_[ti] != s) {
- triStamp_[ti] = s;
- scratchTris_.push_back(ti);
- }
+ constexpr size_t kBlock = 4096;
+ scratchVerts_.clear();
+ if (verts.size() <= kBlock) {
+ 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 {
+ // Threads claim vertices with an atomic exchange of the stamp: which part lists a shared neighbour
+ // varies, but the gathered set and every normal computed from it don't.
+ const size_t parts = (verts.size() + kBlock - 1) / kBlock;
+ if (scratchParts_.size() < parts) scratchParts_.resize(parts);
+ poolFor(verts.size(), [&](size_t b, size_t e) {
+ std::vector<uint32_t>& out = scratchParts_[b / kBlock];
+ out.clear();
+ auto claim = [&](uint32_t u) {
+ if (std::atomic_ref<uint32_t>(vertStamp_[u]).exchange(s, std::memory_order_relaxed) != s) out.push_back(u);
+ };
+ for (size_t i = b; i < e; ++i) {
+ claim(verts[i]);
+ for (uint32_t u : neighbors(verts[i])) claim(u);
+ }
+ }, kBlock);
+ for (size_t p = 0; p < parts; ++p) scratchVerts_.insert(scratchVerts_.end(), scratchParts_[p].begin(), scratchParts_[p].end());
}
- for (uint32_t ti : scratchTris_) faceNormals_[ti] = faceNormal(positions, triangles[ti]);
- scratchVerts_.clear();
- for (uint32_t ti : scratchTris_) {
- for (int c = 0; c < 3; ++c) {
- uint32_t v = triangles[ti][c];
- if (vertStamp_[v] != s) {
- vertStamp_[v] = s;
- scratchVerts_.push_back(v);
- }
+ 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;
+ 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);
+ std::atomic_ref<uint8_t>(dirtyBlocks[v >> kDirtyBlockShift]).store(1, std::memory_order_relaxed);
}
- }
- for (uint32_t v : scratchVerts_) {
- glm::vec3 n(0.0f);
- for (uint32_t ti : vertexTriangles(v)) n += faceNormals_[ti];
- float len = glm::length(n);
- if (len > 1e-20f) normals[v] = n / len;
- markDirty(v);
+ bounds[b / kBlock] = {lo, hi + 1};
+ }, kBlock);
+ for (const auto& [lo, hi] : bounds) {
+ dirtyBegin = std::min(dirtyBegin, lo);
+ dirtyEnd = std::max(dirtyEnd, hi);
}
}
@@ -278,6 +360,16 @@ AABB Mesh::bounds() const {
return b;
}
+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);
+ for (const auto& l : layers) n += bytes(l.delta);
+ for (const auto& l : paintLayers) n += bytes(l.color) + bytes(l.alpha);
+ return n;
+}
+
static void laplacianStep(Mesh& m, float lambda, std::vector<glm::vec3>& next) {
next.resize(m.vertexCount());
for (uint32_t v = 0; v < m.vertexCount(); ++v) {
diff --git a/src/core/Mesh.h b/src/core/Mesh.h
index b3de002f..f1b0176b 100644
--- a/src/core/Mesh.h
+++ b/src/core/Mesh.h
@@ -11,6 +11,7 @@
#include <memory>
#include <span>
#include <string>
+#include <utility>
#include <vector>
namespace sf {
@@ -172,42 +173,53 @@ public:
}
AABB bounds() const;
+ // Bytes this mesh holds (every array, topology included; not the shared texture or subdivision stack).
+ size_t memoryBytes() const;
- // GPU synchronisation state: [dirtyBegin, dirtyEnd) needs re-uploading,
+ // GPU synchronisation state: [dirtyBegin, dirtyEnd) bounds what needs re-uploading,
// topologyDirty means everything (including indices) does.
// Vertex attributes (mask, colours) share the vertex range; facesDirty
// means per-triangle state (hidden, polygroups) needs re-uploading.
+ // Within the range, vertices are tracked in blocks of kDirtyBlock (dirtyBlocks[b] != 0: some vertex of
+ // block b changed), so a dab that touches a few far-apart parts of a big mesh (symmetry, the vertex order
+ // of subdivided levels) uploads only those parts (dirtyRuns).
uint32_t dirtyBegin = std::numeric_limits<uint32_t>::max();
uint32_t dirtyEnd = 0;
bool topologyDirty = true;
bool facesDirty = true;
+ static constexpr uint32_t kDirtyBlockShift = 11, kDirtyBlock = 1u << kDirtyBlockShift; // 2048 vertices
+ std::vector<uint8_t> dirtyBlocks;
void markDirty(uint32_t v) {
dirtyBegin = std::min(dirtyBegin, v);
dirtyEnd = std::max(dirtyEnd, v + 1);
+ const size_t b = v >> kDirtyBlockShift;
+ if (b >= dirtyBlocks.size()) growDirtyBlocks(b + 1);
+ dirtyBlocks[b] = 1;
}
- void clearDirty() {
- dirtyBegin = std::numeric_limits<uint32_t>::max();
- dirtyEnd = 0;
- topologyDirty = false;
- facesDirty = false;
- }
+ void growDirtyBlocks(size_t count); // (out of line: rarely needed)
+ // Marks the vertices [begin, end).
+ void markDirtyRange(uint32_t begin, uint32_t end);
+ void clearDirty();
void markAllDirty() {
if (positions.empty()) return;
- dirtyBegin = 0;
- dirtyEnd = static_cast<uint32_t>(positions.size());
+ markDirtyRange(0, static_cast<uint32_t>(positions.size()));
}
+ // The dirty vertices as sorted, disjoint [begin, end) ranges of whole blocks (clipped to the dirty range
+ // and the vertex count); runs closer than `mergeGap` vertices are joined (one upload call instead of two).
+ void dirtyRuns(std::vector<std::pair<uint32_t, uint32_t>>& out, uint32_t mergeGap = 2 * kDirtyBlock) const;
private:
uint32_t nextStamp();
std::vector<uint32_t> adjOffsets_, adjList_;
std::vector<uint32_t> vtOffsets_, vtList_;
- std::vector<glm::vec3> faceNormals_;
- std::vector<uint32_t> triStamp_, vertStamp_;
+ // updateNormals: vertices already gathered this call (== stamp_), the gathered ones, per-block parts.
+ std::vector<uint32_t> vertStamp_;
uint32_t stamp_ = 0;
- std::vector<uint32_t> scratchTris_, scratchVerts_;
+ 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/MeshTransform.cpp b/src/core/MeshTransform.cpp
index 275f9109..0b93c7b8 100644
--- a/src/core/MeshTransform.cpp
+++ b/src/core/MeshTransform.cpp
@@ -266,8 +266,7 @@ void TransformSession::apply(Mesh& mesh, const MeshTransform& t) {
if (layered_) mesh.evaluate(verts_);
// Rigidly moved normals just turn (above); recompute them only where neighbours move differently.
if (!boundary_.empty()) mesh.updateNormals(boundary_);
- mesh.markDirty(verts_.front());
- mesh.markDirty(verts_.back());
+ mesh.markDirtyRange(verts_.front(), verts_.back() + 1);
moved_ = !t.identity();
}
diff --git a/src/core/Polypaint.cpp b/src/core/Polypaint.cpp
index feb3e2d2..ce9d5cc7 100644
--- a/src/core/Polypaint.cpp
+++ b/src/core/Polypaint.cpp
@@ -116,6 +116,7 @@ size_t paintDab(Sculptor& sculptor, Mesh& mesh, const BVH& bvh, const DabParams&
mesh.ensureColors();
const float s = std::clamp(p.strength, 0.0f, 1.0f) * std::clamp(p.pressure, 0.0f, 1.0f);
static thread_local std::vector<uint32_t> verts;
+ auto clock = std::chrono::steady_clock::now();
size_t changed = 0;
for (const glm::vec3& m : Sculptor::mirrors(sym)) {
const glm::vec3 center = p.center * m, viewDir = p.viewDir * m;
@@ -128,7 +129,10 @@ size_t paintDab(Sculptor& sculptor, Mesh& mesh, const BVH& bvh, const DabParams&
paintVertex(mesh, v, color, w, p.invert); // Alt paints Base back to white, erases a layer
++changed;
}
+ sculptor.timings.vertices += verts.size();
}
+ sculptor.timings.kernelMs += lapMs(clock);
+ ++sculptor.timings.dabs;
return changed;
}
diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cpp
index 5d66d58c..ec82d98a 100644
--- a/src/core/Sculpt.cpp
+++ b/src/core/Sculpt.cpp
@@ -3,6 +3,7 @@
#include "core/Sculpt.h"
#include "core/BrushAlpha.h"
+#include "core/ThreadPool.h"
#include <algorithm>
#include <cmath>
@@ -120,6 +121,29 @@ float samplePattern(BrushRecipe::Pattern pattern, const glm::vec3& p) {
}
}
+// Weighted sums over a brush region: per-block partials added in block order, so the result doesn't
+// depend on the thread count (and a region of one block sums exactly like a plain loop).
+struct AreaSums {
+ glm::vec3 normal{0.0f}, center{0.0f};
+ float weight = 0.0f;
+};
+template <class F>
+AreaSums areaSums(size_t n, F&& add) {
+ constexpr size_t kBlock = 4096;
+ std::vector<AreaSums> parts((n + kBlock - 1) / kBlock);
+ poolFor(n, [&](size_t b, size_t e) {
+ AreaSums& acc = parts[b / kBlock];
+ for (size_t i = b; i < e; ++i) add(i, acc);
+ }, kBlock);
+ AreaSums total;
+ for (const AreaSums& p : parts) {
+ total.normal += p.normal;
+ total.center += p.center;
+ total.weight += p.weight;
+ }
+ return total;
+}
+
} // namespace
std::vector<glm::vec3> Sculptor::mirrors(Symmetry sym) {
@@ -193,6 +217,30 @@ 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.
+void Sculptor::displaceAll(Mesh& mesh, std::span<const uint32_t> verts, std::span<const glm::vec3> d) {
+ const size_t n = verts.size();
+ influence_.resize(n);
+ 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();
+ std::vector<glm::vec3>& target = mesh.layerTarget(layer_);
+ const float weight = mesh.layerTargetWeight(layer_);
+ poolFor(n, [&](size_t b, size_t e) {
+ for (size_t i = b; i < e; ++i) {
+ const float k = influence_[i];
+ if (k <= 0.0f) continue;
+ const glm::vec3 dk = d[i] * k;
+ mesh.positions[verts[i]] += dk;
+ if (layered) target[verts[i]] += dk / weight;
+ }
+ });
+}
+
UndoRecord Sculptor::endStroke() {
active_ = false;
grab_.clear();
@@ -212,6 +260,7 @@ void Sculptor::dab(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p, Sym
}
void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams& p) {
+ auto clock = std::chrono::steady_clock::now();
// Alphas and the square ClayBuildup tip reach into the corners of their square.
const bool useAlpha = p.alpha && !p.alpha->empty();
const float reach = useAlpha ? 1.4143f : type == BrushType::ClayBuildup ? 1.3f : 1.0f;
@@ -227,14 +276,15 @@ void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams&
}
// Weighted area normal and centre of the brush region.
- glm::vec3 areaNormal(0.0f), areaCenter(0.0f);
- float wsum = 0.0f;
- for (uint32_t v : verts_) {
+ const AreaSums sums = areaSums(verts_.size(), [&](size_t i, AreaSums& acc) {
+ const uint32_t v = verts_[i];
float w = brushFalloff(glm::distance(pos[v], p.center), p.radius);
- areaNormal += nrm[v] * w;
- areaCenter += pos[v] * w;
- wsum += w;
- }
+ acc.normal += nrm[v] * w;
+ acc.center += pos[v] * w;
+ acc.weight += w;
+ });
+ glm::vec3 areaNormal = sums.normal, areaCenter = sums.center;
+ const float wsum = sums.weight;
if (wsum <= 0.0f) return;
areaCenter /= wsum;
float nlen = glm::length(areaNormal);
@@ -255,7 +305,9 @@ void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams&
if (type == BrushType::Layer) prepareLayer(mesh, r * 0.08f * std::clamp(p.strength, 0.0f, 1.0f));
scratch_.resize(verts_.size());
- for (size_t i = 0; i < verts_.size(); ++i) {
+ // Each vertex's offset depends only on the positions before the dab (and, for Layer, on its own state).
+ poolFor(verts_.size(), [&](size_t begin, size_t end) {
+ for (size_t i = begin; i < end; ++i) {
const uint32_t v = verts_[i];
const glm::vec3& x = pos[v];
const glm::vec3 o = x - p.center;
@@ -366,10 +418,21 @@ void Sculptor::dabSingle(Mesh& mesh, BVH& bvh, BrushType type, const DabParams&
}
scratch_[i] = d;
}
+ });
+
+ displaceAll(mesh, verts_, scratch_);
+ finishDab(mesh, bvh, verts_, clock);
+}
- for (size_t i = 0; i < verts_.size(); ++i) displace(mesh, verts_[i], scratch_[i]);
- mesh.updateNormals(verts_);
- bvh.refit(mesh, verts_);
+// 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) {
+ timings.kernelMs += lapMs(clock);
+ mesh.updateNormals(verts);
+ timings.normalsMs += lapMs(clock);
+ bvh.refit(mesh, verts);
+ timings.bvhMs += lapMs(clock);
+ ++timings.dabs;
+ timings.vertices += verts.size();
}
void Sculptor::dabRecipe(Mesh& mesh, BVH& bvh, const BrushRecipe& recipe, const DabParams& p, Symmetry sym) {
@@ -388,6 +451,7 @@ void Sculptor::dabRecipe(Mesh& mesh, BVH& bvh, const BrushRecipe& recipe, const
}
void Sculptor::dabRecipeSingle(Mesh& mesh, BVH& bvh, const BrushRecipe& r, const DabParams& p) {
+ auto clock = std::chrono::steady_clock::now();
brushVertices(mesh, bvh, p.center, p.radius, verts_);
if (verts_.empty()) return;
const auto& pos = mesh.positions;
@@ -415,15 +479,13 @@ void Sculptor::dabRecipeSingle(Mesh& mesh, BVH& bvh, const BrushRecipe& r, const
const float edge = std::max(0.05f, 1.0f - r.hardness * 0.95f);
const float exponent = 1.0f + r.tipSharpness * 5.0f;
const float patternFreq = 1.0f / std::max(rad * r.patternScale, 1e-6f);
- glm::vec3 areaNormal(0.0f), areaCenter(0.0f);
- float wsum = 0.0f;
- for (size_t i = 0; i < verts_.size(); ++i) {
+ const AreaSums sums = areaSums(verts_.size(), [&](size_t i, AreaSums& acc) {
const uint32_t v = verts_[i];
float t = std::clamp((1.0f - glm::distance(pos[v], p.center) / rad) / edge, 0.0f, 1.0f);
float w = std::pow(t * t * (3.0f - 2.0f * t), exponent);
- areaNormal += nrm[v] * w;
- areaCenter += pos[v] * w;
- wsum += w;
+ acc.normal += nrm[v] * w;
+ acc.center += pos[v] * w;
+ acc.weight += w;
if (r.cavityMask != 0.0f) {
auto ring = mesh.neighbors(v);
if (!ring.empty()) {
@@ -445,7 +507,9 @@ void Sculptor::dabRecipeSingle(Mesh& mesh, BVH& bvh, const BrushRecipe& r, const
w *= glm::mix(1.0f, samplePattern(r.pattern, pos[v] * patternFreq), r.patternAmount);
if (useAlpha) w *= alphaWeight(pos[v]);
weights_[i] = w;
- }
+ });
+ glm::vec3 areaNormal = sums.normal, areaCenter = sums.center;
+ const float wsum = sums.weight;
if (wsum <= 0.0f) return;
areaCenter /= wsum;
float nlen = glm::length(areaNormal);
@@ -456,7 +520,8 @@ void Sculptor::dabRecipeSingle(Mesh& mesh, BVH& bvh, const BrushRecipe& r, const
strokeDir = strokeLen > 1e-12f ? strokeDir / strokeLen : glm::vec3(0.0f);
scratch_.resize(verts_.size());
- for (size_t i = 0; i < verts_.size(); ++i) {
+ poolFor(verts_.size(), [&](size_t begin, size_t end) {
+ for (size_t i = begin; i < end; ++i) {
const uint32_t v = verts_[i];
const glm::vec3& x = pos[v];
const float f = weights_[i];
@@ -490,25 +555,26 @@ void Sculptor::dabRecipeSingle(Mesh& mesh, BVH& bvh, const BrushRecipe& r, const
if (r.drag > 0.0f) d += p.strokeDelta * (r.drag * f);
scratch_[i] = d;
}
- for (size_t i = 0; i < verts_.size(); ++i) displace(mesh, verts_[i], scratch_[i]);
+ });
+ displaceAll(mesh, verts_, scratch_);
if (r.smooth > 0.0f) {
- for (size_t i = 0; i < verts_.size(); ++i) {
- auto ring = mesh.neighbors(verts_[i]);
- if (ring.empty()) {
- scratch_[i] = glm::vec3(0.0f);
- continue;
+ poolFor(verts_.size(), [&](size_t begin, size_t end) {
+ for (size_t i = begin; i < end; ++i) {
+ auto ring = mesh.neighbors(verts_[i]);
+ if (ring.empty()) {
+ scratch_[i] = glm::vec3(0.0f);
+ continue;
+ }
+ glm::vec3 avg(0.0f);
+ for (uint32_t n : ring) avg += pos[n];
+ avg /= float(ring.size());
+ scratch_[i] = (avg - pos[verts_[i]]) * std::min(1.0f, r.smooth * weights_[i]);
}
- glm::vec3 avg(0.0f);
- for (uint32_t n : ring) avg += pos[n];
- avg /= float(ring.size());
- scratch_[i] = (avg - pos[verts_[i]]) * std::min(1.0f, r.smooth * weights_[i]);
- }
- for (size_t i = 0; i < verts_.size(); ++i) displace(mesh, verts_[i], scratch_[i]);
+ });
+ displaceAll(mesh, verts_, scratch_);
}
-
- mesh.updateNormals(verts_);
- bvh.refit(mesh, verts_);
+ finishDab(mesh, bvh, verts_, clock);
}
void Sculptor::beginGrab(Mesh& mesh, BVH& bvh, const DabParams& p, Symmetry sym) {
@@ -532,9 +598,9 @@ void Sculptor::beginGrab(Mesh& mesh, BVH& bvh, const DabParams& p, Symmetry sym)
void Sculptor::grab(Mesh& mesh, BVH& bvh, const glm::vec3& delta) {
if (grab_.empty()) return;
+ auto clock = std::chrono::steady_clock::now();
for (const auto& g : grab_) displace(mesh, g.vertex, delta * g.mirror * g.weight);
- mesh.updateNormals(grabVerts_);
- bvh.refit(mesh, grabVerts_);
+ finishDab(mesh, bvh, grabVerts_, clock);
}
// Puts every vertex the current stroke moved back to its stroke-start position.
diff --git a/src/core/Sculpt.h b/src/core/Sculpt.h
index fe069eed..c5edb717 100644
--- a/src/core/Sculpt.h
+++ b/src/core/Sculpt.h
@@ -7,6 +7,7 @@
#include "core/Mesh.h"
#include "core/Undo.h"
+#include <chrono>
#include <string>
#include <vector>
@@ -59,6 +60,20 @@ void brushVertices(const Mesh& mesh, const BVH& bvh, const glm::vec3& center, fl
// Smoothstep brush falloff: 1 at the centre, 0 at `radius`.
float brushFalloff(float dist, float radius);
+// Where brush time goes (core/Benchmark.h, Help > Performance): milliseconds summed over dabs since the
+// counters were last zeroed. kernel = finding the vertices and moving / masking / painting them.
+struct SculptTimings {
+ double kernelMs = 0.0, normalsMs = 0.0, bvhMs = 0.0;
+ uint64_t dabs = 0, vertices = 0;
+};
+// Milliseconds since `t`, which moves on to now (for SculptTimings).
+inline double lapMs(std::chrono::steady_clock::time_point& t) {
+ const auto now = std::chrono::steady_clock::now();
+ const double ms = std::chrono::duration<double, std::milli>(now - t).count();
+ t = now;
+ return ms;
+}
+
// Applies brush dabs to a mesh, keeping normals and the BVH in sync and
// recording the original positions of every touched vertex for undo.
class Sculptor {
@@ -83,6 +98,8 @@ public:
UndoRecord endStroke();
bool strokeActive() const { return active_; }
+ SculptTimings timings;
+
// Mirror sign vectors for the enabled symmetry axes (always starts with (1,1,1)).
static std::vector<glm::vec3> mirrors(Symmetry sym);
@@ -99,9 +116,11 @@ 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 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);
+ void displaceAll(Mesh& mesh, std::span<const uint32_t> verts, std::span<const glm::vec3> d);
bool active_ = false;
int layer_ = -1;
@@ -111,7 +130,7 @@ private:
std::vector<uint32_t> verts_;
std::vector<glm::vec3> scratch_;
- std::vector<float> weights_;
+ std::vector<float> weights_, influence_;
std::vector<GrabEntry> grab_;
std::vector<uint32_t> grabVerts_;
diff --git a/src/core/ThreadPool.cpp b/src/core/ThreadPool.cpp
new file mode 100644
index 00000000..623046b0
--- /dev/null
+++ b/src/core/ThreadPool.cpp
@@ -0,0 +1,80 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "core/ThreadPool.h"
+
+namespace sf {
+
+namespace {
+thread_local bool insidePoolJob = false;
+}
+
+ThreadPool::ThreadPool(int threads) {
+ for (int i = 1; i < threads; ++i) workers_.emplace_back([this] { workerLoop(); });
+}
+
+ThreadPool::~ThreadPool() {
+ {
+ std::lock_guard lock(mutex_);
+ stop_ = true;
+ }
+ wake_.notify_all();
+ for (auto& t : workers_) t.join();
+}
+
+ThreadPool& ThreadPool::shared() {
+ static ThreadPool pool(int(std::clamp(std::thread::hardware_concurrency(), 1u, 16u)));
+ return pool;
+}
+
+// Runs jobs of `run` until none are left; returns how many this thread ran.
+size_t ThreadPool::drain(Run& run) {
+ const bool wasInside = insidePoolJob;
+ insidePoolJob = true;
+ size_t ran = 0;
+ for (size_t i; (i = run.next.fetch_add(1)) < run.count; ++ran) (*run.job)(i);
+ insidePoolJob = wasInside;
+ return ran;
+}
+
+void ThreadPool::workerLoop() {
+ uint64_t seen = 0;
+ std::unique_lock lock(mutex_);
+ for (;;) {
+ wake_.wait(lock, [&] { return stop_ || generation_ != seen; });
+ if (stop_) return;
+ seen = generation_;
+ Run* run = current_;
+ if (!run) continue;
+ ++run->users;
+ lock.unlock();
+ const size_t ran = drain(*run);
+ lock.lock();
+ run->finished += ran;
+ if (--run->users == 0 && run->finished == run->count) done_.notify_all();
+ }
+}
+
+void ThreadPool::run(size_t count, const std::function<void(size_t)>& job) {
+ if (count == 0) return;
+ std::unique_lock runLock(runMutex_, std::defer_lock);
+ if (count == 1 || workers_.empty() || serial_ || insidePoolJob || !runLock.try_lock()) {
+ for (size_t i = 0; i < count; ++i) job(i);
+ return;
+ }
+ Run run;
+ run.job = &job;
+ run.count = count;
+ {
+ std::lock_guard lock(mutex_);
+ current_ = &run;
+ ++generation_;
+ }
+ wake_.notify_all();
+ const size_t ran = drain(run);
+ std::unique_lock lock(mutex_);
+ run.finished += ran;
+ done_.wait(lock, [&] { return run.users == 0 && run.finished == run.count; });
+ current_ = nullptr;
+}
+
+} // namespace sf
diff --git a/src/core/ThreadPool.h b/src/core/ThreadPool.h
new file mode 100644
index 00000000..45ebf15b
--- /dev/null
+++ b/src/core/ThreadPool.h
@@ -0,0 +1,81 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// A small persistent thread pool for the per-dab work of brushes (kernels, normals, BVH refits), where
+// starting threads for every dab (core/Parallel.h) would cost more than the work.
+//
+// poolFor() splits [0, n) into fixed-size blocks: which block a thread takes varies, but the blocks
+// themselves, and so any per-block partial results combined in block order, don't depend on the thread
+// count, so results are deterministic. A range of one block runs inline on the calling thread, exactly as
+// a plain loop would.
+#pragma once
+
+#include <algorithm>
+#include <atomic>
+#include <condition_variable>
+#include <cstddef>
+#include <functional>
+#include <mutex>
+#include <thread>
+#include <vector>
+
+namespace sf {
+
+class ThreadPool {
+public:
+ explicit ThreadPool(int threads);
+ ~ThreadPool();
+ ThreadPool(const ThreadPool&) = delete;
+ ThreadPool& operator=(const ThreadPool&) = delete;
+
+ // The pool brushes use: one thread per hardware thread (up to 16), the caller included.
+ static ThreadPool& shared();
+
+ int threads() const { return int(workers_.size()) + 1; }
+ // Serial: every run() executes inline on the calling thread (tests compare results against this).
+ void setSerial(bool serial) { serial_ = serial; }
+ bool serial() const { return serial_; }
+ // Calls job(i) for every i in [0, count), spread over the pool and the calling thread; returns when all
+ // are done. Runs inline when called from inside a job, while another thread is using the pool, or when
+ // count <= 1.
+ void run(size_t count, const std::function<void(size_t)>& job);
+
+private:
+ // One run(): workers join it under mutex_ while it is current, and run() waits until every job is done and
+ // every worker that joined has left, so a late worker can never touch a finished run.
+ struct Run {
+ const std::function<void(size_t)>* job = nullptr;
+ size_t count = 0;
+ std::atomic<size_t> next{0};
+ size_t finished = 0; // guarded by mutex_
+ int users = 0; // workers inside, guarded by mutex_
+ };
+ void workerLoop();
+ size_t drain(Run& run);
+
+ std::vector<std::thread> workers_;
+ std::mutex runMutex_; // one run() at a time
+ std::mutex mutex_;
+ std::condition_variable wake_, done_;
+ Run* current_ = nullptr;
+ uint64_t generation_ = 0;
+ bool stop_ = false;
+ std::atomic<bool> serial_{false};
+};
+
+// Calls fn(begin, end) for the blocks [b * block, min(n, (b + 1) * block)) of [0, n) on the shared pool.
+// Each block is one call, so per-block partial results indexed by begin / block are deterministic.
+template <class F>
+void poolFor(size_t n, F&& fn, size_t block = 4096) {
+ if (n == 0) return;
+ block = std::max<size_t>(block, 1);
+ const size_t blocks = (n + block - 1) / block;
+ if (blocks == 1) {
+ fn(size_t(0), n);
+ return;
+ }
+ const std::function<void(size_t)> job = [&](size_t b) { fn(b * block, std::min(n, (b + 1) * block)); };
+ ThreadPool::shared().run(blocks, job);
+}
+
+} // namespace sf
diff --git a/src/render/GL.h b/src/render/GL.h
index d9bf0a27..cc39f480 100644
--- a/src/render/GL.h
+++ b/src/render/GL.h
@@ -106,6 +106,7 @@ constexpr GLbitfield GL_COLOR_BUFFER_BIT = 0x4000;
X(void, PolygonMode, GLenum, GLenum) \
X(void, PolygonOffset, GLfloat, GLfloat) \
X(const GLubyte*, GetString, GLenum) \
+ X(void, Finish, void) \
X(void, GenBuffers, GLsizei, GLuint*) \
X(void, DeleteBuffers, GLsizei, const GLuint*) \
X(void, BindBuffer, GLenum, GLuint) \
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index c4a0b58d..e9d68735 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -4,6 +4,7 @@
#include "core/Matcap.h"
#include "core/Texture.h"
+#include "core/ThreadPool.h"
#include <glm/gtc/type_ptr.hpp>
@@ -590,7 +591,10 @@ void Renderer::drawSplats(const glm::mat4& view, const glm::mat4& proj, int fbWi
// (ridge, < 0) the surface, relative to the local edge length.
void Renderer::updateCurvature(MeshSlot& s, const Mesh& mesh, uint32_t begin, uint32_t end) {
s.curvature.resize(mesh.vertexCount(), 0.0f);
- for (uint32_t v = begin; v < end && v < mesh.vertexCount(); ++v) {
+ end = std::min(end, uint32_t(mesh.vertexCount()));
+ if (begin >= end) return;
+ poolFor(end - begin, [&](size_t b, size_t e) {
+ for (uint32_t v = begin + uint32_t(b); v < begin + uint32_t(e); ++v) {
const auto ring = mesh.neighbors(v);
if (ring.empty()) {
s.curvature[v] = 0.0f;
@@ -607,6 +611,7 @@ void Renderer::updateCurvature(MeshSlot& s, const Mesh& mesh, uint32_t begin, ui
edge /= float(ring.size());
s.curvature[v] = edge > 0.0f ? glm::dot(mesh.normals[v], avg - p) / edge : 0.0f;
}
+ }, 1 << 14);
}
// Index buffer without hidden triangles, plus the polygroup id of every drawn
@@ -725,11 +730,10 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap) {
normalMap = nullptr; // made for another mesh
const bool wantUvs = hasTexture(mesh) || normalMap;
const bool full = s.fresh || mesh.topologyDirty || nv != s.vertexCapacity;
- const bool ranged = !full && mesh.dirtyBegin < mesh.dirtyEnd && mesh.dirtyEnd <= nv;
- const GLintptr off3 = ranged ? GLintptr(mesh.dirtyBegin * sizeof(glm::vec3)) : 0;
- const GLsizeiptr size3 = ranged ? GLsizeiptr((mesh.dirtyEnd - mesh.dirtyBegin) * sizeof(glm::vec3)) : 0;
- const GLintptr off1 = ranged ? GLintptr(mesh.dirtyBegin * sizeof(float)) : 0;
- const GLsizeiptr size1 = ranged ? GLsizeiptr((mesh.dirtyEnd - mesh.dirtyBegin) * sizeof(float)) : 0;
+ // Partial updates upload only the dirty blocks (Mesh::dirtyRuns): a dab with symmetry on a subdivided
+ // mesh touches a few small, far-apart index ranges, and one range spanning them all could be most of it.
+ if (!full) mesh.dirtyRuns(dirtyRuns_);
+ const bool ranged = !full && !dirtyRuns_.empty();
if (full) {
updateCurvature(s, mesh, 0, uint32_t(nv));
gl::BindBuffer(GL_ARRAY_BUFFER, s.posBuffer);
@@ -741,13 +745,10 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap) {
uploadFaces(s, mesh, wantUvs);
s.vertexCapacity = nv;
} else if (ranged) {
- gl::BindBuffer(GL_ARRAY_BUFFER, s.posBuffer);
- gl::BufferSubData(GL_ARRAY_BUFFER, off3, size3, mesh.positions.data() + mesh.dirtyBegin);
- gl::BindBuffer(GL_ARRAY_BUFFER, s.nrmBuffer);
- gl::BufferSubData(GL_ARRAY_BUFFER, off3, size3, mesh.normals.data() + mesh.dirtyBegin);
- updateCurvature(s, mesh, mesh.dirtyBegin, mesh.dirtyEnd);
- gl::BindBuffer(GL_ARRAY_BUFFER, s.curvBuffer);
- gl::BufferSubData(GL_ARRAY_BUFFER, off1, size1, s.curvature.data() + mesh.dirtyBegin);
+ for (const auto& [b, e] : dirtyRuns_) updateCurvature(s, mesh, b, e);
+ uploadRuns(s.posBuffer, mesh.positions.data(), sizeof(glm::vec3));
+ uploadRuns(s.nrmBuffer, mesh.normals.data(), sizeof(glm::vec3));
+ uploadRuns(s.curvBuffer, s.curvature.data(), sizeof(float));
}
if (!full && (mesh.facesDirty || wantUvs != s.hasUvs)) uploadFaces(s, mesh, wantUvs);
syncTexture(s, mesh);
@@ -756,12 +757,12 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap) {
// Optional per-vertex attributes: a full upload when they appear (or the
// mesh changed), the dirty range otherwise, and a disabled array when absent.
auto syncAttribute = [&](GLuint buffer, GLuint location, bool& uploaded, const void* data, size_t elementSize,
- bool present, GLintptr offset, GLsizeiptr size) {
+ bool present) {
gl::BindBuffer(GL_ARRAY_BUFFER, buffer);
if (present && (full || !uploaded)) {
gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(nv * elementSize), data, GL_DYNAMIC_DRAW);
} else if (present && ranged) {
- gl::BufferSubData(GL_ARRAY_BUFFER, offset, size, static_cast<const uint8_t*>(data) + size_t(offset));
+ uploadRuns(buffer, data, elementSize);
} else if (!present && uploaded) {
gl::BufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
}
@@ -773,15 +774,21 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap) {
}
uploaded = present;
};
- syncAttribute(s.colorBuffer, 3, s.hasColors, mesh.colors.data(), sizeof(glm::vec3), nv > 0 && mesh.colors.size() == nv,
- off3, size3);
- syncAttribute(s.maskBuffer, 4, s.hasMask, mesh.mask.data(), sizeof(float), nv > 0 && mesh.mask.size() == nv, off1,
- size1);
+ syncAttribute(s.colorBuffer, 3, s.hasColors, mesh.colors.data(), sizeof(glm::vec3), nv > 0 && mesh.colors.size() == nv);
+ syncAttribute(s.maskBuffer, 4, s.hasMask, mesh.mask.data(), sizeof(float), nv > 0 && mesh.mask.size() == nv);
gl::BindBuffer(GL_ARRAY_BUFFER, 0);
s.fresh = false;
mesh.clearDirty();
}
+// Uploads the vertex ranges in dirtyRuns_ of a per-vertex array to `buffer` (bound as GL_ARRAY_BUFFER).
+void Renderer::uploadRuns(GLuint buffer, const void* data, size_t elementSize) {
+ gl::BindBuffer(GL_ARRAY_BUFFER, buffer);
+ for (const auto& [b, e] : dirtyRuns_)
+ gl::BufferSubData(GL_ARRAY_BUFFER, GLintptr(size_t(b) * elementSize), GLsizeiptr(size_t(e - b) * elementSize),
+ static_cast<const uint8_t*>(data) + size_t(b) * elementSize);
+}
+
// The painted texture (core/Texture.h): a full upload when it is new or replaced, then only the rectangle
// painted since this slot's last upload (PaintTexture::revision / dirtyFrom), with fresh mipmaps.
void Renderer::syncTexture(MeshSlot& s, Mesh& mesh) {
diff --git a/src/render/Renderer.h b/src/render/Renderer.h
index 48c08d2c..b13e1af5 100644
--- a/src/render/Renderer.h
+++ b/src/render/Renderer.h
@@ -138,6 +138,8 @@ private:
void syncNormalMap(MeshSlot& s, const Mesh& mesh, const NormalMapView* normalMap);
static void updateCurvature(MeshSlot& s, const Mesh& mesh, uint32_t begin, uint32_t end);
void uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs);
+ void uploadRuns(GLuint buffer, const void* data, size_t elementSize);
+ std::vector<std::pair<uint32_t, uint32_t>> dirtyRuns_; // scratch for sync: Mesh::dirtyRuns
void syncTexture(MeshSlot& s, Mesh& mesh);
void drawSlot(const MeshSlot& s, bool active, const RenderSettings& settings);
std::vector<MeshSlot> slots_;
diff --git a/tests/test_performance.cpp b/tests/test_performance.cpp
new file mode 100644
index 00000000..65f5904b
--- /dev/null
+++ b/tests/test_performance.cpp
@@ -0,0 +1,282 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Performance work (docs/performance.md): the thread pool, brushes / normals / BVH refits that run on it give
+// the same result as running serially, partial GPU uploads (Mesh::dirtyRuns) cover every changed vertex, the
+// parallel BVH build answers like the serial one, undo after a big stroke is exact, and the benchmark runs.
+#include "TestHarness.h"
+
+#include "core/Benchmark.h"
+#include "core/BrushRecipe.h"
+#include "core/Primitives.h"
+#include "core/ThreadPool.h"
+
+#include <atomic>
+#include <cmath>
+#include <thread>
+
+using namespace sf;
+
+namespace {
+
+// Runs `fn` with the shared pool forced serial, then restores it.
+template <class F>
+void serially(F&& fn) {
+ ThreadPool::shared().setSerial(true);
+ fn();
+ ThreadPool::shared().setSerial(false);
+}
+
+bool sameVec3(const std::vector<glm::vec3>& a, const std::vector<glm::vec3>& b) {
+ if (a.size() != b.size()) return false;
+ for (size_t i = 0; i < a.size(); ++i)
+ if (a[i] != b[i]) return false;
+ return true;
+}
+
+// A stroke with every built-in brush type plus a recipe, mask and paint, big enough (a few blocks of vertices
+// per dab) to run in parallel.
+void bigStroke(Document& d) {
+ const float radius = 0.4f;
+ for (int t = 0; t < int(BrushType::Count); ++t) {
+ const BrushType type = BrushType(t);
+ d.sculptor.beginStroke(d.mesh, brushName(type));
+ for (int i = 0; i < 4; ++i) {
+ DabParams p;
+ p.center = glm::normalize(glm::vec3(0.3f + 0.05f * float(i), 0.2f, 1.0f));
+ p.radius = radius;
+ p.strength = 0.6f;
+ p.strokeDelta = glm::vec3(0.01f, 0.0f, 0.0f);
+ if (type == BrushType::Move) {
+ if (i == 0) d.sculptor.beginGrab(d.mesh, d.bvh, p, Symmetry{});
+ else d.sculptor.grab(d.mesh, d.bvh, glm::vec3(0.01f, 0.005f, 0.0f));
+ } else {
+ d.sculptor.dab(d.mesh, d.bvh, type, p, Symmetry{});
+ }
+ }
+ d.undo.push(d.sculptor.endStroke());
+ }
+ const std::vector<BrushRecipe> recipes = builtinBrushRecipes();
+ for (const BrushRecipe& r : recipes) {
+ d.sculptor.beginStroke(d.mesh, r.name);
+ DabParams p;
+ p.center = glm::normalize(glm::vec3(-0.2f, 0.4f, 1.0f));
+ p.radius = radius;
+ p.strokeDelta = glm::vec3(0.0f, 0.01f, 0.0f);
+ d.sculptor.dabRecipe(d.mesh, d.bvh, r, p, Symmetry{});
+ d.undo.push(d.sculptor.endStroke());
+ }
+ d.sculptor.beginStroke(d.mesh, "Mask");
+ DabParams p;
+ p.center = glm::normalize(glm::vec3(0.0f, -0.3f, 1.0f));
+ p.radius = radius;
+ d.sculptor.dabMask(d.mesh, d.bvh, p, Symmetry{});
+ d.undo.push(d.sculptor.endStroke());
+}
+
+Document sphereDoc(int subdivisions) { return makeDoc(makeIcosphere(subdivisions)); }
+
+} // namespace
+
+TEST(thread_pool_runs_every_job_once) {
+ ThreadPool& pool = ThreadPool::shared();
+ CHECK(pool.threads() >= 1);
+ std::vector<std::atomic<int>> hits(100000);
+ poolFor(hits.size(), [&](size_t b, size_t e) {
+ for (size_t i = b; i < e; ++i) hits[i].fetch_add(1);
+ }, 1000);
+ bool once = true;
+ for (auto& h : hits) once = once && h.load() == 1;
+ CHECK(once);
+
+ // Nested use runs inline; two threads using the pool at once both finish.
+ std::atomic<size_t> nested{0};
+ poolFor(64, [&](size_t b, size_t e) {
+ for (size_t i = b; i < e; ++i) poolFor(100, [&](size_t b2, size_t e2) { nested += e2 - b2; }, 10);
+ }, 1);
+ CHECK(nested.load() == 6400);
+ std::atomic<size_t> a{0}, c{0};
+ std::thread other([&] {
+ for (int k = 0; k < 50; ++k) poolFor(10000, [&](size_t b, size_t e) { a += e - b; }, 100);
+ });
+ for (int k = 0; k < 50; ++k) poolFor(10000, [&](size_t b, size_t e) { c += e - b; }, 100);
+ other.join();
+ CHECK(a.load() == 500000 && c.load() == 500000);
+
+ // One block runs on the calling thread, like a plain loop.
+ std::thread::id where;
+ poolFor(10, [&](size_t, size_t) { where = std::this_thread::get_id(); }, 4096);
+ CHECK(where == std::this_thread::get_id());
+}
+
+TEST(parallel_brushes_match_serial) {
+ // icosphere(7): 164k vertices, so each dab covers several blocks (> 4096 vertices) and runs in parallel.
+ Document par = sphereDoc(7), ser = sphereDoc(7);
+ bigStroke(par);
+ serially([&] { bigStroke(ser); });
+ CHECK(sameVec3(par.mesh.positions, ser.mesh.positions));
+ CHECK(sameVec3(par.mesh.normals, ser.mesh.normals));
+ CHECK(par.mesh.mask == ser.mesh.mask);
+ CHECK(par.undo.undoCount() == ser.undo.undoCount());
+ // Undo every stroke: back to the sphere exactly.
+ const Mesh fresh = makeIcosphere(7);
+ while (par.undo.canUndo()) par.undo.undo(par.mesh, par.bvh);
+ CHECK(sameVec3(par.mesh.positions, fresh.positions));
+ bool maskClear = true;
+ for (float m : par.mesh.mask) maskClear = maskClear && m == 0.0f;
+ CHECK(maskClear);
+}
+
+TEST(parallel_normals_match_full_recompute) {
+ Mesh m = makeIcosphere(6);
+ std::vector<uint32_t> moved;
+ for (uint32_t v = 0; v < m.vertexCount(); v += 3) {
+ m.positions[v] *= 1.0f + 0.05f * std::sin(float(v));
+ moved.push_back(v);
+ }
+ m.updateNormals(moved);
+ const std::vector<glm::vec3> partial = m.normals;
+ m.recomputeAllNormals();
+ float worst = 0.0f;
+ for (size_t v = 0; v < partial.size(); ++v) worst = std::max(worst, glm::length(partial[v] - m.normals[v]));
+ CHECK(worst < 1e-5f);
+}
+
+TEST(dirty_runs_cover_changed_vertices) {
+ Mesh m = makeIcosphere(6);
+ m.clearDirty();
+ std::vector<std::pair<uint32_t, uint32_t>> runs;
+ m.dirtyRuns(runs);
+ CHECK(runs.empty());
+ // Two far-apart vertices: two short runs, not one span over the mesh between them.
+ const uint32_t a = 10, b = uint32_t(m.vertexCount()) - 10;
+ m.markDirty(a);
+ m.markDirty(b);
+ m.dirtyRuns(runs);
+ CHECK(runs.size() == 2);
+ size_t covered = 0;
+ for (const auto& [lo, hi] : runs) {
+ CHECK(lo < hi && hi <= m.vertexCount());
+ covered += hi - lo;
+ }
+ CHECK(covered <= 2 * Mesh::kDirtyBlock);
+ CHECK(runs.front().first <= a && a < runs.front().second);
+ CHECK(runs.back().first <= b && b < runs.back().second);
+ // A brush dab: every vertex whose normal changed is inside a run.
+ m.clearDirty();
+ BVH bvh;
+ bvh.build(m);
+ Sculptor s;
+ s.beginStroke(m, "Standard");
+ const std::vector<glm::vec3> before = m.normals;
+ DabParams p;
+ p.center = glm::normalize(glm::vec3(0.5f, 0.5f, 0.7f));
+ p.radius = 0.3f;
+ s.dab(m, bvh, BrushType::Standard, p, Symmetry{});
+ m.dirtyRuns(runs);
+ std::vector<uint8_t> inRun(m.vertexCount(), 0);
+ for (const auto& [lo, hi] : runs)
+ for (uint32_t v = lo; v < hi; ++v) inRun[v] = 1;
+ bool allCovered = true;
+ size_t changed = 0;
+ for (uint32_t v = 0; v < m.vertexCount(); ++v)
+ if (m.normals[v] != before[v]) {
+ ++changed;
+ allCovered = allCovered && inRun[v];
+ }
+ CHECK(changed > 1000);
+ CHECK(allCovered);
+ m.clearDirty();
+ m.dirtyRuns(runs);
+ CHECK(runs.empty());
+ m.markAllDirty();
+ m.dirtyRuns(runs);
+ CHECK(runs.size() == 1 && runs[0].first == 0 && runs[0].second == m.vertexCount());
+}
+
+TEST(parallel_bvh_build_answers_like_serial) {
+ // icosphere(7): 328k triangles, above the size the build splits over threads.
+ const Mesh m = makeIcosphere(7);
+ BVH par, ser;
+ par.build(m);
+ serially([&] { ser.build(m); });
+ CHECK(par.nodeCount() == ser.nodeCount());
+ std::vector<uint32_t> a, b;
+ bool same = true;
+ for (int i = 0; i < 50; ++i) {
+ const glm::vec3 c = glm::normalize(glm::vec3(std::sin(float(i)), std::cos(float(i) * 1.3f), std::sin(float(i) * 0.7f)));
+ par.queryVertices(m, c, 0.1f, a);
+ ser.queryVertices(m, c, 0.1f, b);
+ same = same && a == b;
+ par.queryTriangles(m, c, 0.05f, a);
+ ser.queryTriangles(m, c, 0.05f, b);
+ same = same && a == b;
+ RayHit h1, h2;
+ const bool r1 = par.raycast(m, c * 3.0f, -c, h1), r2 = ser.raycast(m, c * 3.0f, -c, h2);
+ same = same && r1 && r2 && h1.triangle == h2.triangle && h1.t == h2.t;
+ }
+ CHECK(same);
+}
+
+TEST(bvh_refit_after_big_stroke_finds_everything) {
+ Document d = sphereDoc(7);
+ bigStroke(d);
+ // Brute force vs. the refitted tree.
+ std::vector<uint32_t> found;
+ bool ok = true;
+ for (int i = 0; i < 20; ++i) {
+ const glm::vec3 c = glm::normalize(glm::vec3(0.3f, 0.2f - 0.03f * float(i), 1.0f));
+ d.bvh.queryVertices(d.mesh, c, 0.2f, found);
+ size_t brute = 0;
+ for (const glm::vec3& p : d.mesh.positions) brute += glm::distance(p, c) <= 0.2f;
+ ok = ok && found.size() == brute;
+ }
+ CHECK(ok);
+}
+
+TEST(first_paint_undo_is_cheap_and_exact) {
+ // The app's first polypaint stroke on an unpainted mesh: the undo step holds the paint stack without
+ // colours (not a copy of the mesh), and undo removes the attribute again.
+ Document d = sphereDoc(5);
+ CHECK(!d.mesh.hasColors());
+ auto snapshot = std::make_unique<PaintStackUndo>(PaintStackUndo::capture(d.mesh));
+ d.sculptor.beginStroke(d.mesh, "Paint");
+ d.mesh.ensureColors();
+ for (uint32_t v = 0; v < 100; ++v) {
+ d.sculptor.recordColor(d.mesh, v);
+ d.mesh.colors[v] = glm::vec3(1, 0, 0);
+ }
+ UndoRecord r = d.sculptor.endStroke();
+ r.colors = {};
+ r.paintAlpha = {};
+ r.paint = std::move(snapshot);
+ CHECK(r.approxBytes() < 1000);
+ d.undo.push(std::move(r));
+ d.undo.undo(d.mesh, d.bvh);
+ CHECK(!d.mesh.hasColors());
+ d.undo.redo(d.mesh, d.bvh);
+ CHECK(d.mesh.hasColors() && d.mesh.colors[5] == glm::vec3(1, 0, 0) && d.mesh.colors[500] == glm::vec3(1.0f));
+}
+
+TEST(mesh_memory_has_no_face_normal_cache) {
+ // Bytes per triangle of a sculpting mesh (positions, normals, triangles, both adjacency lists and stamps):
+ // about 54 on a closed triangle mesh (it was 78 with a per-triangle face normal cache, its stamps and the
+ // adjacency list's spare capacity).
+ const Mesh m = makeIcosphere(6);
+ const double perTriangle = double(m.memoryBytes()) / double(m.triangleCount());
+ CHECK(perTriangle < 60.0);
+}
+
+TEST(benchmark_smoke) {
+ BenchOptions o;
+ o.sizesMillions = {0.02};
+ o.dabsPerStroke = 3;
+ o.projectDir = std::filesystem::temp_directory_path().string();
+ const std::string report = runBenchmark(o);
+ CHECK(report.find("| Standard |") != std::string::npos);
+ CHECK(report.find("| Paint |") != std::string::npos);
+ CHECK(report.find("Open project") != std::string::npos);
+ CHECK(report.find("failed") == std::string::npos);
+ const Mesh m = makeBenchSphere(50000);
+ CHECK(m.triangleCount() > 40000 && m.triangleCount() < 60000);
+}