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.

GPU vertex order by first use in the triangle list: dabs upload what they changed

islandmedfb24798bcb46
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 85efcd11..0cc93f49 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -28,6 +28,7 @@ add_library(sculptforge_core STATIC
src/core/Mesh.cpp
src/core/BVH.cpp
src/core/ThreadPool.cpp
+ src/core/UploadOrder.cpp
src/core/Benchmark.cpp
src/core/Sculpt.cpp
src/core/Undo.cpp
diff --git a/src/core/Benchmark.cpp b/src/core/Benchmark.cpp
index 752c64b3..ff330674 100644
--- a/src/core/Benchmark.cpp
+++ b/src/core/Benchmark.cpp
@@ -9,6 +9,7 @@
#include "core/Primitives.h"
#include "core/Project.h"
#include "core/ThreadPool.h"
+#include "core/UploadOrder.h"
#include <algorithm>
#include <chrono>
@@ -67,12 +68,12 @@ struct BenchBrush {
struct StrokeStats {
int dabs = 0;
- double verts = 0, kernel = 0, normals = 0, bvh = 0, pick = 0, upload = 0, total = 0;
+ double verts = 0, uploaded = 0, runs = 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 runStroke(Document& doc, const BenchBrush& b, int dabs, const BenchGpu* gpu, UploadOrder& order) {
StrokeStats st;
Mesh& m = doc.mesh;
Sculptor& sc = doc.sculptor;
@@ -88,6 +89,8 @@ StrokeStats runStroke(Document& doc, const BenchBrush& b, int dabs, const BenchG
// 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);
+ std::vector<std::pair<uint32_t, uint32_t>> runs;
+ m.clearDirty();
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);
@@ -115,7 +118,12 @@ StrokeStats runStroke(Document& doc, const BenchBrush& b, int dabs, const BenchG
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;
}
+ // What a partial upload of this dab sends (as the renderer finds it), then the upload itself.
+ order.dirtyRuns(m, runs);
+ for (const auto& [b, e] : runs) st.uploaded += double(e - b);
+ st.runs += double(runs.size());
if (gpu && gpu->upload) st.upload += gpu->upload(m);
+ else m.clearDirty();
++st.dabs;
}
st.total = msSince(stroke0);
@@ -123,6 +131,8 @@ StrokeStats runStroke(Document& doc, const BenchBrush& b, int dabs, const BenchG
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)
+ st.uploaded /= double(std::max(st.dabs, 1));
+ st.runs /= double(std::max(st.dabs, 1));
auto t = Clock::now();
UndoRecord r = sc.endStroke();
@@ -241,6 +251,8 @@ std::string runBenchmark(const BenchOptions& o, const BenchGpu* gpu) {
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();
+ UploadOrder order;
+ order.build(m);
md << "### " << millions(nt) << " triangles (" << millions(nv) << " vertices, "
<< subdivLevelCount(m) << " subdivision levels)\n\n";
@@ -257,14 +269,15 @@ std::string runBenchmark(const BenchOptions& o, const BenchGpu* gpu) {
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 | " : "")
+ md << "| Brush | verts/dab | uploaded verts (ranges) | kernel | normals | BVH refit | pick | " << (gpu ? "GPU upload | " : "")
<< "**dab total** | stroke end (undo record) | undo | redo | undo step |\n";
- md << "|---|---:|---:|---:|---:|---:|" << (gpu ? "---:|" : "") << "---:|---:|---:|---:|---:|\n";
+ md << "|---|---:|---:|---:|---:|---:|---:|" << (gpu ? "---:|" : "") << "---:|---:|---:|---:|---:|\n";
for (const BenchBrush& b : brushes) {
log(" " + b.name);
- const StrokeStats s = runStroke(doc, b, o.dabsPerStroke, gpu);
+ const StrokeStats s = runStroke(doc, b, o.dabsPerStroke, gpu, order);
const double d = std::max(s.dabs, 1);
- md << "| " << b.name << " | " << millions(s.verts) << " | " << fmt("%.2f", s.kernel / d) << " | "
+ md << "| " << b.name << " | " << millions(s.verts) << " | " << millions(s.uploaded) << " (" << fmt("%.0f", s.runs)
+ << ") | " << 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)
diff --git a/src/core/Mesh.cpp b/src/core/Mesh.cpp
index 65395b7c..6c2ed10d 100644
--- a/src/core/Mesh.cpp
+++ b/src/core/Mesh.cpp
@@ -169,6 +169,7 @@ void Mesh::rebuildTopology() {
stamp_ = 0;
normals.resize(nv);
dirtyBlocks.assign((nv + kDirtyBlock - 1) / kDirtyBlock, 0);
+ dirtyBits.assign(dirtyBlocks.size() * (kDirtyBlock / 64), 0);
if (dirtyBegin < dirtyEnd) markDirtyRange(dirtyBegin, std::min(dirtyEnd, uint32_t(nv)));
topologyDirty = true;
facesDirty = true;
@@ -182,8 +183,9 @@ uint32_t Mesh::nextStamp() {
return stamp_;
}
-void Mesh::growDirtyBlocks(size_t count) {
- if (dirtyBlocks.size() < count) dirtyBlocks.resize(count, 0);
+void Mesh::growDirtyBlocks(size_t blocks) {
+ if (dirtyBlocks.size() < blocks) dirtyBlocks.resize(blocks, 0);
+ if (dirtyBits.size() < blocks * (kDirtyBlock / 64)) dirtyBits.resize(blocks * (kDirtyBlock / 64), 0);
}
void Mesh::markDirtyRange(uint32_t begin, uint32_t end) {
@@ -193,12 +195,27 @@ void Mesh::markDirtyRange(uint32_t begin, uint32_t end) {
const size_t last = (end - 1) >> kDirtyBlockShift;
growDirtyBlocks(last + 1);
std::fill(dirtyBlocks.begin() + (begin >> kDirtyBlockShift), dirtyBlocks.begin() + last + 1, uint8_t(1));
+ // Bits: the partial words at both ends, whole words between.
+ const size_t w0 = begin >> 6, w1 = (end - 1) >> 6;
+ const uint64_t head = ~uint64_t(0) << (begin & 63), tail = ~uint64_t(0) >> (63 - ((end - 1) & 63));
+ if (w0 == w1) {
+ dirtyBits[w0] |= head & tail;
+ return;
+ }
+ dirtyBits[w0] |= head;
+ std::fill(dirtyBits.begin() + w0 + 1, dirtyBits.begin() + w1, ~uint64_t(0));
+ dirtyBits[w1] |= tail;
}
void Mesh::clearDirty() {
if (dirtyBegin < dirtyEnd) {
+ constexpr size_t kWords = kDirtyBlock / 64;
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));
+ for (size_t i = b; i < e; ++i) {
+ if (!dirtyBlocks[i]) continue;
+ dirtyBlocks[i] = 0;
+ std::fill(dirtyBits.begin() + std::ptrdiff_t(i * kWords), dirtyBits.begin() + std::ptrdiff_t((i + 1) * kWords), 0);
+ }
}
dirtyBegin = std::numeric_limits<uint32_t>::max();
dirtyEnd = 0;
@@ -206,20 +223,6 @@ void Mesh::clearDirty() {
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() {
// 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.
@@ -291,6 +294,7 @@ void Mesh::updateNormals(std::span<const uint32_t> verts) {
lo = std::min(lo, v);
hi = std::max(hi, v);
std::atomic_ref<uint8_t>(dirtyBlocks[v >> kDirtyBlockShift]).store(1, std::memory_order_relaxed);
+ std::atomic_ref<uint64_t>(dirtyBits[v >> 6]).fetch_or(uint64_t(1) << (v & 63), std::memory_order_relaxed);
}
bounds[b / kBlock] = {lo, hi + 1};
}, kBlock);
@@ -364,7 +368,7 @@ size_t Mesh::memoryBytes() const {
auto bytes = [](const auto& v) { return v.capacity() * sizeof(v[0]); };
size_t n = bytes(positions) + bytes(normals) + bytes(triangles) + bytes(mask) + bytes(colors) + bytes(polygroups) +
bytes(hidden) + bytes(uvs) + bytes(quadPartners) + bytes(base) + bytes(paintBase) + bytes(adjOffsets_) +
- bytes(adjList_) + bytes(vtOffsets_) + bytes(vtList_) + bytes(vertStamp_) + bytes(scratchVerts_) + bytes(dirtyBlocks);
+ bytes(adjList_) + bytes(vtOffsets_) + bytes(vtList_) + bytes(vertStamp_) + bytes(scratchVerts_) + bytes(dirtyBlocks) + bytes(dirtyBits);
for (const auto& l : layers) n += bytes(l.delta);
for (const auto& l : paintLayers) n += bytes(l.color) + bytes(l.alpha);
return n;
diff --git a/src/core/Mesh.h b/src/core/Mesh.h
index f1b0176b..b5cab46c 100644
--- a/src/core/Mesh.h
+++ b/src/core/Mesh.h
@@ -180,15 +180,15 @@ public:
// 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).
+ // Within the range each changed vertex has a bit in dirtyBits, and each block of kDirtyBlock vertices with
+ // any a mark in dirtyBlocks, so uploads find a dab's vertices without scanning the mesh (core/UploadOrder.h).
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;
+ std::vector<uint64_t> dirtyBits;
void markDirty(uint32_t v) {
dirtyBegin = std::min(dirtyBegin, v);
@@ -196,8 +196,9 @@ public:
const size_t b = v >> kDirtyBlockShift;
if (b >= dirtyBlocks.size()) growDirtyBlocks(b + 1);
dirtyBlocks[b] = 1;
+ dirtyBits[v >> 6] |= uint64_t(1) << (v & 63);
}
- void growDirtyBlocks(size_t count); // (out of line: rarely needed)
+ void growDirtyBlocks(size_t blocks); // (out of line: rarely needed)
// Marks the vertices [begin, end).
void markDirtyRange(uint32_t begin, uint32_t end);
void clearDirty();
@@ -205,9 +206,7 @@ public:
if (positions.empty()) return;
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;
+ bool vertexDirty(uint32_t v) const { return (v >> 6) < dirtyBits.size() && (dirtyBits[v >> 6] >> (v & 63)) & 1; }
private:
uint32_t nextStamp();
diff --git a/src/core/UploadOrder.cpp b/src/core/UploadOrder.cpp
new file mode 100644
index 00000000..c6b9d81e
--- /dev/null
+++ b/src/core/UploadOrder.cpp
@@ -0,0 +1,77 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "core/UploadOrder.h"
+
+#include <algorithm>
+#include <bit>
+
+namespace sf {
+
+void UploadOrder::build(const Mesh& mesh) {
+ const size_t nv = mesh.vertexCount();
+ constexpr uint32_t kUnset = ~0u;
+ toGpu_.assign(nv, kUnset);
+ fromGpu_.clear();
+ fromGpu_.reserve(nv);
+ for (const glm::uvec3& t : mesh.triangles)
+ for (int c = 0; c < 3; ++c)
+ if (t[c] < nv && toGpu_[t[c]] == kUnset) {
+ toGpu_[t[c]] = uint32_t(fromGpu_.size());
+ fromGpu_.push_back(t[c]);
+ }
+ for (uint32_t v = 0; v < nv; ++v)
+ if (toGpu_[v] == kUnset) {
+ toGpu_[v] = uint32_t(fromGpu_.size());
+ fromGpu_.push_back(v);
+ }
+ marked_.assign((nv + kBlock - 1) / kBlock, 0);
+ blocks_.clear();
+}
+
+void UploadOrder::dirtyRuns(const Mesh& mesh, std::vector<std::pair<uint32_t, uint32_t>>& out) {
+ out.clear();
+ const uint32_t nv = uint32_t(std::min(mesh.vertexCount(), fromGpu_.size()));
+ const uint32_t end = std::min(mesh.dirtyEnd, nv);
+ if (mesh.dirtyBegin >= end) return;
+ const size_t firstBlock = mesh.dirtyBegin >> Mesh::kDirtyBlockShift;
+ const size_t lastBlock = std::min(mesh.dirtyBlocks.size(), size_t((end - 1) >> Mesh::kDirtyBlockShift) + 1);
+ constexpr size_t kWordsPerBlock = Mesh::kDirtyBlock / 64;
+ size_t dirty = 0;
+ for (size_t b = firstBlock; b < lastBlock; ++b)
+ if (mesh.dirtyBlocks[b])
+ for (size_t w = b * kWordsPerBlock; w < (b + 1) * kWordsPerBlock && w < mesh.dirtyBits.size(); ++w)
+ dirty += size_t(std::popcount(mesh.dirtyBits[w]));
+ if (dirty * 4 > nv || mesh.dirtyBits.size() * 64 < nv) {
+ out.emplace_back(0, nv); // a good part of the mesh (or no per-vertex marks): everything, in one go
+ return;
+ }
+ // The GPU blocks of every marked vertex.
+ for (size_t b = firstBlock; b < lastBlock; ++b) {
+ if (!mesh.dirtyBlocks[b]) continue;
+ for (size_t w = b * kWordsPerBlock; w < (b + 1) * kWordsPerBlock && w < mesh.dirtyBits.size(); ++w) {
+ for (uint64_t bits = mesh.dirtyBits[w]; bits; bits &= bits - 1) {
+ const size_t v = w * 64 + size_t(std::countr_zero(bits));
+ if (v >= nv) break;
+ const uint32_t g = toGpu_[v] >> kBlockShift;
+ if (!marked_[g]) {
+ marked_[g] = 1;
+ blocks_.push_back(g);
+ }
+ }
+ }
+ }
+ std::sort(blocks_.begin(), blocks_.end());
+ for (uint32_t g : blocks_) {
+ marked_[g] = 0;
+ const uint32_t lo = g << kBlockShift, hi = std::min(nv, (g + 1) << kBlockShift);
+ if (!out.empty() && lo - out.back().second <= kBlock) out.back().second = hi;
+ else out.emplace_back(lo, hi);
+ }
+ blocks_.clear();
+}
+
+size_t UploadOrder::memoryBytes() const {
+ return (toGpu_.capacity() + fromGpu_.capacity() + blocks_.capacity()) * sizeof(uint32_t) + marked_.capacity();
+}
+
+} // namespace sf
diff --git a/src/core/UploadOrder.h b/src/core/UploadOrder.h
new file mode 100644
index 00000000..3a3d6c96
--- /dev/null
+++ b/src/core/UploadOrder.h
@@ -0,0 +1,47 @@
+// SculptForge - open source digital sculpting
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// The order a renderer keeps a mesh's vertices in on the GPU, and which parts of it a change needs re-sent.
+//
+// Subdivided meshes number their vertices level by level (the cage's, then each level's edge and face
+// points), so the vertices under one brush dab are scattered over the whole index range, and re-sending the
+// changed index ranges meant re-sending ten times what changed. Their triangles though stay grouped by the
+// face they came from, at every level, so numbering vertices by first use in the triangle list keeps the
+// vertices of any region close together. The renderer stores vertex arrays in that order (and remaps the
+// indices); dirtyRuns() turns the vertices the mesh marked dirty (Mesh::markDirty) into a few short GPU index
+// ranges to gather and upload.
+#pragma once
+
+#include "core/Mesh.h"
+
+#include <cstdint>
+#include <span>
+#include <utility>
+#include <vector>
+
+namespace sf {
+
+class UploadOrder {
+public:
+ static constexpr uint32_t kBlockShift = 9, kBlock = 1u << kBlockShift; // GPU upload granularity: 512 vertices
+
+ // Numbers the vertices by first use in the triangle list (unused vertices last).
+ void build(const Mesh& mesh);
+ size_t size() const { return fromGpu_.size(); }
+ uint32_t toGpu(uint32_t v) const { return toGpu_[v]; }
+ // fromGpu()[i] = the mesh vertex at GPU index i.
+ std::span<const uint32_t> fromGpu() const { return fromGpu_; }
+
+ // Sorted, disjoint GPU index ranges [begin, end) holding every vertex marked dirty, in whole blocks (runs
+ // one block apart are joined). Everything, as one range, when most of the mesh is dirty.
+ void dirtyRuns(const Mesh& mesh, std::vector<std::pair<uint32_t, uint32_t>>& out);
+
+ size_t memoryBytes() const;
+
+private:
+ std::vector<uint32_t> toGpu_, fromGpu_;
+ std::vector<uint8_t> marked_; // per GPU block, while dirtyRuns collects (all zero between calls)
+ std::vector<uint32_t> blocks_; // the marked blocks
+};
+
+} // namespace sf
diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp
index e9d68735..3531343b 100644
--- a/src/render/Renderer.cpp
+++ b/src/render/Renderer.cpp
@@ -5,6 +5,7 @@
#include "core/Matcap.h"
#include "core/Texture.h"
#include "core/ThreadPool.h"
+#include "core/UploadOrder.h"
#include <glm/gtc/type_ptr.hpp>
@@ -217,7 +218,7 @@ void main() {
float spec = pow(max(dot(n, h), 0.0), 40.0) * 0.18;
float rim = pow(1.0 - max(dot(n, v), 0.0), 3.0) * 0.3;
float facing = smoothstep(-0.2, 0.6, dot(n, v));
- // vCurvature > 0 in creases, < 0 on ridges (see Renderer::updateCurvature).
+ // vCurvature > 0 in creases, < 0 on ridges (see curvatureAt in Renderer.cpp).
float crease = smoothstep(0.0, 0.16, vCurvature);
float ridge = smoothstep(0.0, 0.2, -vCurvature);
float cavity = 1.0 - 0.62 * crease;
@@ -587,33 +588,58 @@ void Renderer::drawSplats(const glm::mat4& view, const glm::mat4& proj, int fbWi
gl::BindTexture(GL_TEXTURE_BUFFER, 0);
}
-// Signed curvature per vertex: how far the neighbour average sits above (crease, > 0) or below
-// (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);
- 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;
- continue;
- }
- glm::vec3 avg(0.0f);
- float edge = 0.0f;
- const glm::vec3 p = mesh.positions[v];
- for (uint32_t n : ring) {
- avg += mesh.positions[n];
- edge += glm::distance(mesh.positions[n], p);
- }
- avg /= float(ring.size());
- edge /= float(ring.size());
- s.curvature[v] = edge > 0.0f ? glm::dot(mesh.normals[v], avg - p) / edge : 0.0f;
+namespace {
+
+// Signed curvature at vertex `v`: how far the neighbour average sits above (crease, > 0) or below (ridge, < 0)
+// the surface, relative to the local edge length.
+float curvatureAt(const Mesh& mesh, uint32_t v) {
+ const auto ring = mesh.neighbors(v);
+ if (ring.empty()) return 0.0f;
+ glm::vec3 avg(0.0f);
+ float edge = 0.0f;
+ const glm::vec3 p = mesh.positions[v];
+ for (uint32_t n : ring) {
+ avg += mesh.positions[n];
+ edge += glm::distance(mesh.positions[n], p);
}
- }, 1 << 14);
+ avg /= float(ring.size());
+ edge /= float(ring.size());
+ return edge > 0.0f ? glm::dot(mesh.normals[v], avg - p) / edge : 0.0f;
+}
+
+using Runs = std::vector<std::pair<uint32_t, uint32_t>>;
+
+// Uploads the GPU index ranges `runs` of one vertex array (bound to GL_ARRAY_BUFFER first; `allocate` sizes it
+// to `count` elements): fill(begin, end, dst) writes those elements, in GPU order, to dst. Big ranges go in
+// chunks through `staging`, so a full upload never copies the whole array.
+template <class Fill>
+void uploadGathered(std::vector<uint8_t>& staging, GLuint buffer, size_t elementSize, size_t count, bool allocate,
+ const Runs& runs, const Fill& fill) {
+ constexpr uint32_t kChunk = 1u << 18;
+ gl::BindBuffer(GL_ARRAY_BUFFER, buffer);
+ if (allocate) gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(count * elementSize), nullptr, GL_DYNAMIC_DRAW);
+ for (const auto& [b, e] : runs)
+ for (uint32_t c = b; c < e; c += kChunk) {
+ const uint32_t ce = std::min(e, c + kChunk);
+ staging.resize(size_t(ce - c) * elementSize);
+ fill(c, ce, staging.data());
+ gl::BufferSubData(GL_ARRAY_BUFFER, GLintptr(size_t(c) * elementSize), GLsizeiptr(staging.size()), staging.data());
+ }
}
+// fill() for uploadGathered: copies src[order[i]] in parallel.
+template <class T>
+auto gatherFrom(const T* src, std::span<const uint32_t> order) {
+ return [src, order](uint32_t b, uint32_t e, void* dst) {
+ T* out = static_cast<T*>(dst);
+ poolFor(e - b, [&](size_t i0, size_t i1) {
+ for (size_t i = i0; i < i1; ++i) out[i] = src[order[b + i]];
+ }, 1 << 14);
+ };
+}
+
+} // namespace
+
// Index buffer without hidden triangles, plus the polygroup id of every drawn
// triangle in draw order (looked up by gl_PrimitiveID). Meshes with quads
// (Mesh::quadPartners) also get wireframe edges without the quad diagonals,
@@ -622,18 +648,17 @@ void Renderer::uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs) {
const size_t nt = mesh.triangleCount();
const bool hidden = mesh.hidden.size() == nt && nt > 0;
const bool groups = mesh.polygroups.size() == nt && nt > 0;
- const glm::uvec3* tris = mesh.triangles.data();
+ // Indices are remapped to the slot's vertex order (core/UploadOrder.h) and uploaded in chunks.
+ auto gpu = [&](const glm::uvec3& t) { return glm::uvec3(s.order.toGpu(t.x), s.order.toGpu(t.y), s.order.toGpu(t.z)); };
size_t drawn = nt;
visibleGroups_.clear();
if (hidden) {
- visibleTris_.clear();
+ drawn = 0;
for (size_t t = 0; t < nt; ++t) {
if (mesh.hidden[t]) continue;
- visibleTris_.push_back(mesh.triangles[t]);
+ ++drawn;
if (groups) visibleGroups_.push_back(mesh.polygroups[t]);
}
- tris = visibleTris_.data();
- drawn = visibleTris_.size();
} else if (groups) {
visibleGroups_ = mesh.polygroups;
}
@@ -644,19 +669,35 @@ void Renderer::uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs) {
// The diagonal stays visible where the other half of the quad is hidden (it is the visible edge).
const uint32_t u = mesh.quadPartnerAt(t);
const int diagonal = u != Mesh::kNoQuad && !mesh.triangleHidden(u) ? mesh.quadDiagonal(t, u) : -1;
- const glm::uvec3& tri = mesh.triangles[t];
+ const glm::uvec3 tri = gpu(mesh.triangles[t]);
for (int e = 0; e < 3; ++e)
if (e != diagonal) wireLines_.insert(wireLines_.end(), {tri[e], tri[(e + 1) % 3]});
}
}
const GLsizeiptr triBytes = GLsizeiptr(drawn * sizeof(glm::uvec3));
gl::BindVertexArray(s.vao);
- gl::BufferData(GL_ELEMENT_ARRAY_BUFFER, triBytes + GLsizeiptr(wireLines_.size() * sizeof(uint32_t)),
- wireLines_.empty() ? tris : nullptr, GL_STATIC_DRAW);
- if (!wireLines_.empty()) {
- gl::BufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, triBytes, tris);
- gl::BufferSubData(GL_ELEMENT_ARRAY_BUFFER, triBytes, GLsizeiptr(wireLines_.size() * sizeof(uint32_t)), wireLines_.data());
+ gl::BufferData(GL_ELEMENT_ARRAY_BUFFER, triBytes + GLsizeiptr(wireLines_.size() * sizeof(uint32_t)), nullptr, GL_STATIC_DRAW);
+ constexpr size_t kChunk = size_t(1) << 18;
+ size_t written = 0;
+ for (size_t t0 = 0; t0 < nt; t0 += kChunk) {
+ const size_t t1 = std::min(nt, t0 + kChunk);
+ visibleTris_.clear();
+ if (hidden) {
+ for (size_t t = t0; t < t1; ++t)
+ if (!mesh.hidden[t]) visibleTris_.push_back(gpu(mesh.triangles[t]));
+ } else {
+ visibleTris_.resize(t1 - t0);
+ poolFor(t1 - t0, [&](size_t b, size_t e) {
+ for (size_t i = b; i < e; ++i) visibleTris_[i] = gpu(mesh.triangles[t0 + i]);
+ }, 1 << 14);
+ }
+ if (visibleTris_.empty()) continue;
+ gl::BufferSubData(GL_ELEMENT_ARRAY_BUFFER, GLintptr(written * sizeof(glm::uvec3)),
+ GLsizeiptr(visibleTris_.size() * sizeof(glm::uvec3)), visibleTris_.data());
+ written += visibleTris_.size();
}
+ if (!wireLines_.empty())
+ gl::BufferSubData(GL_ELEMENT_ARRAY_BUFFER, triBytes, GLsizeiptr(wireLines_.size() * sizeof(uint32_t)), wireLines_.data());
gl::BindVertexArray(0);
s.indexCount = GLsizei(drawn * 3);
s.lineCount = GLsizei(wireLines_.size());
@@ -730,39 +771,43 @@ 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;
- // 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_);
+ // The GPU keeps the vertices in the slot's upload order (core/UploadOrder.h), so a dab re-sends a few short
+ // ranges holding what it changed, gathered from the mesh arrays.
+ if (full) {
+ s.order.build(mesh);
+ dirtyRuns_.assign(1, {0u, uint32_t(nv)});
+ } else {
+ s.order.dirtyRuns(mesh, dirtyRuns_);
+ }
const bool ranged = !full && !dirtyRuns_.empty();
+ const std::span<const uint32_t> order = s.order.fromGpu();
+ if (full || ranged) {
+ uploadGathered(staging_, s.posBuffer, sizeof(glm::vec3), nv, full, dirtyRuns_, gatherFrom(mesh.positions.data(), order));
+ uploadGathered(staging_, s.nrmBuffer, sizeof(glm::vec3), nv, full, dirtyRuns_, gatherFrom(mesh.normals.data(), order));
+ uploadGathered(staging_, s.curvBuffer, sizeof(float), nv, full, dirtyRuns_, [&](uint32_t b, uint32_t e, void* dst) {
+ float* out = static_cast<float*>(dst);
+ poolFor(e - b, [&](size_t i0, size_t i1) {
+ for (size_t i = i0; i < i1; ++i) out[i] = curvatureAt(mesh, order[b + i]);
+ }, 1 << 12);
+ });
+ }
if (full) {
- updateCurvature(s, mesh, 0, uint32_t(nv));
- gl::BindBuffer(GL_ARRAY_BUFFER, s.posBuffer);
- gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(nv * sizeof(glm::vec3)), mesh.positions.data(), GL_DYNAMIC_DRAW);
- gl::BindBuffer(GL_ARRAY_BUFFER, s.nrmBuffer);
- gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(nv * sizeof(glm::vec3)), mesh.normals.data(), GL_DYNAMIC_DRAW);
- gl::BindBuffer(GL_ARRAY_BUFFER, s.curvBuffer);
- gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(nv * sizeof(float)), s.curvature.data(), GL_DYNAMIC_DRAW);
uploadFaces(s, mesh, wantUvs);
s.vertexCapacity = nv;
- } else if (ranged) {
- 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);
syncNormalMap(s, mesh, 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) {
+ // mesh changed), the dirty runs otherwise, and a disabled array when absent.
+ const Runs everything{{0u, uint32_t(nv)}};
+ auto syncAttribute = [&](GLuint buffer, GLuint location, bool& uploaded, auto gather, size_t elementSize, bool present) {
gl::BindBuffer(GL_ARRAY_BUFFER, buffer);
if (present && (full || !uploaded)) {
- gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(nv * elementSize), data, GL_DYNAMIC_DRAW);
+ uploadGathered(staging_, buffer, elementSize, nv, true, everything, gather);
} else if (present && ranged) {
- uploadRuns(buffer, data, elementSize);
+ uploadGathered(staging_, buffer, elementSize, nv, false, dirtyRuns_, gather);
} else if (!present && uploaded) {
gl::BufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
}
@@ -774,21 +819,16 @@ 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);
- syncAttribute(s.maskBuffer, 4, s.hasMask, mesh.mask.data(), sizeof(float), nv > 0 && mesh.mask.size() == nv);
+ syncAttribute(s.colorBuffer, 3, s.hasColors, gatherFrom(mesh.colors.data(), order), sizeof(glm::vec3),
+ nv > 0 && mesh.colors.size() == nv);
+ syncAttribute(s.maskBuffer, 4, s.hasMask, gatherFrom(mesh.mask.data(), order), sizeof(float), nv > 0 && mesh.mask.size() == nv);
+ // Scratch only: don't keep a big upload's staging copy around.
+ if (staging_.capacity() > (size_t(1) << 22)) std::vector<uint8_t>().swap(staging_);
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 b13e1af5..06e7841f 100644
--- a/src/render/Renderer.h
+++ b/src/render/Renderer.h
@@ -4,6 +4,7 @@
#include "core/Mesh.h"
#include "core/Splats.h"
+#include "core/UploadOrder.h"
#include "render/Camera.h"
#include "render/GL.h"
@@ -127,7 +128,7 @@ private:
bool hasTangents = false, tangentsStale = true, normalFlipGreen = false;
bool hasColors = false, hasMask = false, hasGroups = false, hasUvs = false;
bool fresh = true; // nothing uploaded yet
- std::vector<float> curvature;
+ UploadOrder order; // the vertices' order in the GPU buffers
GLsizei indexCount = 0;
GLsizei lineCount = 0; // wireframe edge indices after the triangles (meshes with quads; 0 = draw triangles)
size_t vertexCapacity = 0;
@@ -136,10 +137,9 @@ private:
static void freeSlot(MeshSlot& s);
void sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap = nullptr);
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
+ std::vector<std::pair<uint32_t, uint32_t>> dirtyRuns_; // scratch for sync: UploadOrder::dirtyRuns
+ std::vector<uint8_t> staging_; // scratch for sync: gathered vertex data
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
index 65f5904b..6221293a 100644
--- a/tests/test_performance.cpp
+++ b/tests/test_performance.cpp
@@ -2,7 +2,7 @@
// 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
+// the same result as running serially, partial GPU uploads (core/UploadOrder.h) 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"
@@ -10,6 +10,7 @@
#include "core/BrushRecipe.h"
#include "core/Primitives.h"
#include "core/ThreadPool.h"
+#include "core/UploadOrder.h"
#include <atomic>
#include <cmath>
@@ -142,55 +143,70 @@ TEST(parallel_normals_match_full_recompute) {
CHECK(worst < 1e-5f);
}
-TEST(dirty_runs_cover_changed_vertices) {
- Mesh m = makeIcosphere(6);
+TEST(upload_runs_cover_changed_vertices) {
+ // A subdivided mesh, whose vertex numbering scatters any region over the index range.
+ Mesh m = makeBenchSphere(200000);
+ BVH bvh;
+ bvh.build(m);
+ UploadOrder order;
+ order.build(m);
+ // A permutation: every vertex once.
+ std::vector<uint8_t> seen(m.vertexCount(), 0);
+ bool perm = order.size() == m.vertexCount();
+ for (uint32_t i = 0; perm && i < order.size(); ++i) {
+ const uint32_t v = order.fromGpu()[i];
+ perm = v < m.vertexCount() && !seen[v] && order.toGpu(v) == i;
+ seen[v] = 1;
+ }
+ CHECK(perm);
+
m.clearDirty();
std::vector<std::pair<uint32_t, uint32_t>> runs;
- m.dirtyRuns(runs);
+ order.dirtyRuns(m, 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);
+ CHECK(!m.vertexDirty(0));
+ // A brush dab with symmetry: every vertex whose normal changed is in a run, and the runs hold not much
+ // more than the dab touched.
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;
+ p.radius = 0.2f;
s.dab(m, bvh, BrushType::Standard, p, Symmetry{});
- m.dirtyRuns(runs);
+ order.dirtyRuns(m, 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;
+ size_t uploaded = 0;
+ for (size_t i = 0; i < runs.size(); ++i) {
+ CHECK(runs[i].first < runs[i].second && runs[i].second <= m.vertexCount());
+ if (i) CHECK(runs[i - 1].second < runs[i].first);
+ uploaded += runs[i].second - runs[i].first;
+ for (uint32_t g = runs[i].first; g < runs[i].second; ++g) inRun[order.fromGpu()[g]] = 1;
+ }
bool allCovered = true;
- size_t changed = 0;
- for (uint32_t v = 0; v < m.vertexCount(); ++v)
+ size_t changed = 0, dirty = 0;
+ for (uint32_t v = 0; v < m.vertexCount(); ++v) {
+ dirty += m.vertexDirty(v);
if (m.normals[v] != before[v]) {
++changed;
- allCovered = allCovered && inRun[v];
+ allCovered = allCovered && inRun[v] && m.vertexDirty(v);
}
+ }
CHECK(changed > 1000);
CHECK(allCovered);
+ // In mesh order, the 2048-vertex blocks holding them would be several times more.
+ size_t meshOrder = 0;
+ for (size_t b = 0; b < m.dirtyBlocks.size(); ++b) meshOrder += m.dirtyBlocks[b] ? Mesh::kDirtyBlock : 0;
+ CHECK(uploaded * 3 < meshOrder);
m.clearDirty();
- m.dirtyRuns(runs);
+ order.dirtyRuns(m, runs);
CHECK(runs.empty());
+ CHECK(!m.vertexDirty(order.fromGpu()[0]));
+ // Ranges mark per vertex too; most of the mesh dirty uploads everything as one range.
+ m.markDirtyRange(70, 200);
+ CHECK(!m.vertexDirty(69) && m.vertexDirty(70) && m.vertexDirty(199) && !m.vertexDirty(200));
m.markAllDirty();
- m.dirtyRuns(runs);
+ order.dirtyRuns(m, runs);
CHECK(runs.size() == 1 && runs[0].first == 0 && runs[0].second == m.vertexCount());
}