Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
Renderer: clusters culled on the CPU, 12-byte packed vertices, 16-bit indices, one multi-draw
islandmed96f0e72f532e
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 4fd19ef2..51370628 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -189,6 +189,7 @@ if(SCULPTFORGE_BUILD_APP)src/app/Tablet.cppsrc/render/GL.cppsrc/render/Camera.cpp+ src/render/Clusters.cppsrc/render/Renderer.cpp)target_link_libraries(sculptforge PRIVATE sculptforge_core sculptforge_ai imgui SDL3::SDL3)@@ -357,7 +358,8 @@ if(SCULPTFORGE_BUILD_TESTS)# tests/test_main.cpp plus every tests/test_*.cpp (add a file to add tests; see tests/TestHarness.h).file(GLOB SCULPTFORGE_TEST_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_*.cpp)add_executable(sculptforge_tests ${SCULPTFORGE_TEST_SOURCES} src/cli/McpServer.cpp src/app/AssetLibrary.cpp- src/app/UiLayout.cpp src/app/ShellGeometry.cpp src/render/Camera.cpp)+ src/app/UiLayout.cpp src/app/ShellGeometry.cpp src/render/Camera.cpp+ src/render/Clusters.cpp)target_include_directories(sculptforge_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests)target_compile_definitions(sculptforge_tests PRIVATE SF_TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/data")target_link_libraries(sculptforge_tests PRIVATE sculptforge_core sculptforge_ai)diff --git a/src/app/Bench.cpp b/src/app/Bench.cppindex 3b6c051f..5104538c 100644--- a/src/app/Bench.cpp+++ b/src/app/Bench.cpp@@ -114,6 +114,8 @@ int runAppBenchmark(BenchCommandLine cl) {}return ms;};+ gpu.gpuBytes = [&] { return renderer.gpuBytes(); };+ gpu.drawnTriangles = [&] { return renderer.lastDrawnTriangles(); };code = runBenchCommandLine(cl, "## With the GPU (sculptforge --bench)\n\n", &gpu);renderer.shutdown();}diff --git a/src/app/main.cpp b/src/app/main.cppindex 876babc4..2a15b7f9 100644--- a/src/app/main.cpp+++ b/src/app/main.cpp@@ -40,6 +40,7 @@ void usage() {" --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-view Benchmark only what the viewport costs (uploads, orbit)\n"" --bench-out FILE Write the benchmark report (Markdown) to FILE");}diff --git a/src/cli/main.cpp b/src/cli/main.cppindex ba7c8aa3..244bf289 100644--- a/src/cli/main.cpp+++ b/src/cli/main.cpp@@ -35,6 +35,7 @@ void usage() {" --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-view Benchmark only what the viewport costs (uploads, orbit)\n"" --bench-out FILE Write the benchmark report (Markdown) to FILE\n"" -h, --help Show this help\n""\n"diff --git a/src/core/Benchmark.cpp b/src/core/Benchmark.cppindex b5d7892f..49abd2e4 100644--- a/src/core/Benchmark.cpp+++ b/src/core/Benchmark.cpp@@ -60,8 +60,10 @@ MemoryUse documentMemory(const Document& doc) {u.undo += (active ? doc.undo : st.undo).undoBytes();u.triangles += m.triangleCount();u.vertices += m.vertexCount();- if (st.visible) // positions, normals, curvature (+ colours, mask) per vertex; indices per triangle- u.gpu += m.vertexCount() * (28 + (m.hasColors() ? 12 : 0) + (m.hasMask() ? 4 : 0)) + m.triangleCount() * 12;+ if (st.visible) // the renderer's cluster layout (core/UploadOrder.h): packed vertices (12 bytes, + colour 4,+ // mask 1), about a tenth more for the copies on cluster seams; 16-bit indices and polygroups+ u.gpu += m.vertexCount() * (12 + (m.hasColors() ? 4 : 0) + (m.hasMask() ? 1 : 0)) * 11 / 10 ++ m.triangleCount() * (6 + (m.hasPolygroups() ? 2 : 0));}return u;}@@ -224,6 +226,9 @@ bool parseBenchArg(int argc, char** argv, int& i, BenchCommandLine& cl) {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-view") {+ cl.options.viewOnly = true;+ cl.options.projectRoundTrip = false;} else if (a == "--bench-out") {cl.outPath = next();} else {@@ -310,6 +315,10 @@ std::string runBenchmark(const BenchOptions& o, const BenchGpu* gpu) {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";+ if (gpu && gpu->gpuBytes) {+ const double b = double(gpu->gpuBytes());+ md << "| GPU memory (buffers) | " << fmt("%.0f MB", b / 1e6) << " = " << fmt("%.1f", b / nt) << " B/triangle |\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)@@ -323,6 +332,7 @@ std::string runBenchmark(const BenchOptions& o, const BenchGpu* gpu) {<< "**dab total** | stroke end (undo record) | undo | redo | undo step |\n";md << "|---|---:|---:|---:|---:|---:|---:|" << (gpu ? "---:|" : "") << "---:|---:|---:|---:|---:|\n";for (const BenchBrush& b : brushes) {+ if (o.viewOnly && b.name != "Standard" && b.kind != Kind::Mask && b.kind != Kind::Paint) continue;log(" " + b.name);const StrokeStats s = runStroke(doc, b, o.dabsPerStroke, gpu, order);const double d = std::max(s.dabs, 1);@@ -336,7 +346,7 @@ std::string runBenchmark(const BenchOptions& o, const BenchGpu* gpu) {md << "\n";md << "| Operation | ms |\n|---|---:|\n";- if (subdivLevelCount(m) > 1) {+ if (subdivLevelCount(m) > 1 && !o.viewOnly) {std::string err;const int top = subdivCurrentLevel(m);log(" subdivision levels");@@ -382,6 +392,10 @@ std::string runBenchmark(const BenchOptions& o, const BenchGpu* gpu) {md << "| Orbit: frame time, " << (samples == 1 ? "Fast Navigation (no antialiasing)" : "4x antialiasing") << " | "<< fmt("%.1f", f) << " (" << fmt("%.0f", 1000.0 / std::max(f, 1e-3)) << " fps) |\n";}+ if (gpu->drawnTriangles) {+ const double d = double(gpu->drawnTriangles());+ md << "| Orbit: triangles drawn after culling | " << millions(d) << " (" << fmt("%.0f", 100.0 * d / nt) << "%) |\n";+ }}md << "\n";if (o.partial) o.partial(md.str());diff --git a/src/core/Benchmark.h b/src/core/Benchmark.hindex c607ad2c..d520378d 100644--- a/src/core/Benchmark.h+++ b/src/core/Benchmark.h@@ -27,6 +27,8 @@ struct BenchGpu {// Average milliseconds per frame over a turn around the mesh (already uploaded), drawn with `samples`// antialiasing samples as the app's view draws (1 = Fast Navigation while the view moves).std::function<double(Mesh&, int samples)> orbitFrameMs;+ std::function<size_t()> gpuBytes; // GPU memory the renderer holds for the mesh (optional)+ std::function<size_t()> drawnTriangles; // triangles the last orbit frame drew, after culling (optional)std::string description; // GPU / driver, for the report};@@ -35,13 +37,16 @@ struct BenchOptions {int dabsPerStroke = 40;std::string projectDir; // where the project round trip writes (default: the system temp folder)bool projectRoundTrip = true;+ // Only what the viewport costs (--bench-view): setup, the first upload, the strokes whose uploads differ+ // (Standard, Mask, Paint), orbiting; no other brushes, subdivision levels or project round trip.+ bool viewOnly = false;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+// --bench-view (only the viewport's costs), --bench-out FILE. parseBenchArg returns true when argv[i] was one of them// (advancing i past its value).struct BenchCommandLine {bool run = false;diff --git a/src/core/UploadOrder.cpp b/src/core/UploadOrder.cppindex c6b9d81e..054d67d9 100644--- a/src/core/UploadOrder.cpp+++ b/src/core/UploadOrder.cpp@@ -2,37 +2,226 @@// SPDX-License-Identifier: GPL-3.0-or-later#include "core/UploadOrder.h"+#include "core/AtomicRef.h"+#include "core/ThreadPool.h"+#include <algorithm>#include <bit>namespace sf {+namespace {++// Mesh vertex -> local index within one cluster: an open-addressing table, cleared by bumping a stamp. Big+// enough for a cluster's worst case (3 distinct vertices per triangle) at under half load.+struct LocalMap {+ static constexpr uint32_t kBits = 15, kSize = 1u << kBits;+ std::vector<uint32_t> keys = std::vector<uint32_t>(kSize), stamps = std::vector<uint32_t>(kSize, 0);+ std::vector<uint16_t> values = std::vector<uint16_t>(kSize);+ uint32_t stamp = 0;+ uint32_t count = 0;+ void clear() {+ if (++stamp == 0) {+ std::fill(stamps.begin(), stamps.end(), 0);+ stamp = 1;+ }+ count = 0;+ }+ // The local index of v, assigning the next one on first use (`isNew` says which).+ uint16_t get(uint32_t v, bool& isNew) {+ for (uint32_t h = (v * 0x9E3779B1u) >> (32 - kBits);; h = (h + 1) & (kSize - 1)) {+ if (stamps[h] != stamp) {+ stamps[h] = stamp;+ keys[h] = v;+ values[h] = uint16_t(count++);+ isNew = true;+ return values[h];+ }+ if (keys[h] == v) {+ isNew = false;+ return values[h];+ }+ }+ }+};++LocalMap& localMap() {+ thread_local LocalMap m;+ return m;+}++// Calls fn(local, meshVertex, isNew) for every corner of cluster c's triangles in order.+template <class F>+void walkCluster(const Mesh& mesh, const UploadOrder& order, uint32_t c, F&& fn) {+ LocalMap& map = localMap();+ map.clear();+ const uint32_t nv = uint32_t(mesh.vertexCount());+ const uint32_t n = order.clusterTriangles(c);+ for (uint32_t i = 0; i < n; ++i) {+ const glm::uvec3& tri = mesh.triangles[order.triangle(c, i)];+ const bool valid = tri.x < nv && tri.y < nv && tri.z < nv;+ for (int k = 0; k < 3; ++k) {+ if (!valid) {+ fn(-1, 0u, false);+ continue;+ }+ bool isNew = false;+ const uint16_t local = map.get(tri[k], isNew);+ fn(int(local), tri[k], isNew);+ }+ }+}++// Sorts (key, value) pairs by key, keeping equal keys in order (LSD radix, 11 bits per pass).+void radixSortByKey(std::vector<std::pair<uint32_t, uint32_t>>& items, uint32_t maxKey) {+ if (items.size() < 2) return;+ std::vector<std::pair<uint32_t, uint32_t>> tmp(items.size());+ constexpr uint32_t kDigit = 11, kBuckets = 1u << kDigit;+ const int bits = std::max(1, int(std::bit_width(maxKey)));+ std::vector<uint32_t> count(kBuckets);+ for (int shift = 0; shift < bits; shift += int(kDigit)) {+ std::fill(count.begin(), count.end(), 0);+ for (const auto& e : items) ++count[(e.first >> shift) & (kBuckets - 1)];+ uint32_t sum = 0;+ for (uint32_t& n : count) sum += std::exchange(n, sum);+ for (const auto& e : items) tmp[count[(e.first >> shift) & (kBuckets - 1)]++] = e;+ items.swap(tmp);+ }+}++// Spreads the low 10 bits of x to every third bit (Morton codes).+uint32_t spread3(uint32_t x) {+ x &= 0x3FF;+ x = (x | (x << 16)) & 0x030000FF;+ x = (x | (x << 8)) & 0x0300F00F;+ x = (x | (x << 4)) & 0x030C30C3;+ x = (x | (x << 2)) & 0x09249249;+ return x;+}++} // namespace++uint32_t UploadOrder::clusterOfSlot(uint32_t i) const {+ const auto it = std::upper_bound(clusters_.begin(), clusters_.end(), i,+ [](uint32_t s, const Cluster& c) { return s < c.firstVertex; });+ return it == clusters_.begin() ? 0u : uint32_t(it - clusters_.begin() - 1);+}+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]);+ triangles_ = mesh.triangleCount();+ vertices_ = mesh.vertexCount();+ // Chunks along a Morton curve of their centres (10 bits per axis over the mesh's bounds); a partial last+ // chunk goes last, so every cluster but the last is made of whole chunks.+ const size_t fullChunks = triangles_ >> kChunkShift, nChunks = (triangles_ + kChunkTriangles - 1) >> kChunkShift;+ std::vector<AABB> boxes(fullChunks);+ poolFor(fullChunks, [&](size_t k0, size_t k1) {+ for (size_t k = k0; k < k1; ++k) {+ AABB box;+ for (size_t t = k << kChunkShift; t < (k + 1) << kChunkShift; ++t)+ for (int c = 0; c < 3; ++c)+ if (mesh.triangles[t][c] < vertices_) box.expand(mesh.positions[mesh.triangles[t][c]]);+ boxes[k] = box;+ }+ }, 256);+ AABB all;+ for (const AABB& b : boxes)+ if (b.valid()) all.expand(b);+ std::vector<std::pair<uint32_t, uint32_t>> keyed(fullChunks);+ const glm::vec3 lo = all.valid() ? all.min : glm::vec3(0.0f);+ const glm::vec3 ext = all.valid() ? glm::max(all.extent(), glm::vec3(1e-30f)) : glm::vec3(1.0f);+ poolFor(fullChunks, [&](size_t k0, size_t k1) {+ for (size_t k = k0; k < k1; ++k) {+ uint32_t code = 0;+ if (boxes[k].valid()) {+ const glm::vec3 q = glm::clamp((boxes[k].center() - lo) / ext, 0.0f, 1.0f) * 1023.0f;+ code = spread3(uint32_t(q.x)) | (spread3(uint32_t(q.y)) << 1) | (spread3(uint32_t(q.z)) << 2);}- for (uint32_t v = 0; v < nv; ++v)- if (toGpu_[v] == kUnset) {- toGpu_[v] = uint32_t(fromGpu_.size());- fromGpu_.push_back(v);+ keyed[k] = {code, uint32_t(k)};+ }+ }, 4096);+ std::vector<AABB>().swap(boxes);+ radixSortByKey(keyed, (1u << 30) - 1);+ chunks_.resize(nChunks);+ for (size_t k = 0; k < fullChunks; ++k) chunks_[k] = keyed[k].second;+ if (nChunks > fullChunks) chunks_.back() = uint32_t(fullChunks);+ const size_t nc = (nChunks + kClusterChunks - 1) / kClusterChunks;+ clusters_.assign(nc, Cluster{});+ for (size_t c = 0, first = 0; c < nc; ++c) {+ const size_t endChunk = std::min(nChunks, (c + 1) * kClusterChunks);+ const size_t count = std::min(triangles_, endChunk << kChunkShift) - (c * kClusterChunks << kChunkShift);+ clusters_[c].firstTriangle = uint32_t(first);+ clusters_[c].triangleCount = uint32_t(count);+ first += count;+ }+ // Distinct vertices per cluster, then block-aligned ranges.+ poolFor(nc, [&](size_t c0, size_t c1) {+ for (size_t c = c0; c < c1; ++c) {+ uint32_t n = 0;+ walkCluster(mesh, *this, uint32_t(c), [&](int, uint32_t, bool isNew) { n += isNew; });+ clusters_[c].vertexCount = n;}- marked_.assign((nv + kBlock - 1) / kBlock, 0);+ }, 1);+ size_t slots = 0;+ for (Cluster& c : clusters_) {+ c.firstVertex = uint32_t(slots);+ slots += (size_t(c.vertexCount) + kBlock - 1) & ~size_t(kBlock - 1);+ }+ fromGpu_.assign(slots, 0);+ toGpu_.assign(vertices_, kNone);+ // Each cluster's vertices in first-use order; a vertex's first slot is the lowest (an atomic min, so the+ // result doesn't depend on which thread gets there first).+ poolFor(nc, [&](size_t c0, size_t c1) {+ for (size_t c = c0; c < c1; ++c) {+ const Cluster& cl = clusters_[c];+ walkCluster(mesh, *this, uint32_t(c), [&](int local, uint32_t v, bool isNew) {+ if (!isNew) return;+ const uint32_t slot = cl.firstVertex + uint32_t(local);+ fromGpu_[slot] = v;+ AtomicRef<uint32_t> first(toGpu_[v]);+ uint32_t cur = first.load(std::memory_order_relaxed);+ while (slot < cur && !first.compare_exchange_weak(cur, slot, std::memory_order_relaxed)) {+ }+ });+ const uint32_t padEnd = c + 1 < nc ? clusters_[c + 1].firstVertex : uint32_t(slots);+ const uint32_t fill = cl.vertexCount ? fromGpu_[cl.firstVertex + cl.vertexCount - 1] : 0u;+ std::fill(fromGpu_.begin() + cl.firstVertex + cl.vertexCount, fromGpu_.begin() + padEnd, fill);+ }+ }, 1);+ // The other copies, by vertex (a stable sort of the slot-ordered list, so slots stay increasing).+ copies_.clear();+ hasCopy_.assign((vertices_ + 63) / 64, 0);+ std::vector<std::pair<uint32_t, uint32_t>> bySlot;+ for (size_t c = 0; c < nc; ++c) {+ const Cluster& cl = clusters_[c];+ for (uint32_t i = cl.firstVertex; i < cl.firstVertex + cl.vertexCount; ++i)+ if (toGpu_[fromGpu_[i]] != i) bySlot.emplace_back(fromGpu_[i], i);+ }+ radixSortByKey(bySlot, uint32_t(vertices_));+ for (const auto& e : bySlot) hasCopy_[e.first >> 6] |= uint64_t(1) << (e.first & 63);+ copies_ = std::move(bySlot);+ marked_.assign((slots + kBlock - 1) / kBlock, 0);blocks_.clear();}+void UploadOrder::slotsOf(uint32_t v, std::vector<uint32_t>& out) const {+ out.clear();+ if (v >= toGpu_.size() || toGpu_[v] == kNone) return;+ out.push_back(toGpu_[v]);+ if (!((hasCopy_[v >> 6] >> (v & 63)) & 1)) return;+ auto it = std::lower_bound(copies_.begin(), copies_.end(), std::make_pair(v, 0u));+ for (; it != copies_.end() && it->first == v; ++it) out.push_back(it->second);+}++void UploadOrder::localTriangles(const Mesh& mesh, uint32_t c, uint16_t* out) const {+ walkCluster(mesh, *this, c, [&](int local, uint32_t, bool) { *out++ = uint16_t(std::max(local, 0)); });+}+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 nv = uint32_t(std::min(mesh.vertexCount(), vertices_));+ const uint32_t ns = uint32_t(fromGpu_.size());const uint32_t end = std::min(mesh.dirtyEnd, nv);- if (mesh.dirtyBegin >= end) return;+ if (mesh.dirtyBegin >= end || ns == 0) 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;@@ -42,36 +231,49 @@ void UploadOrder::dirtyRuns(const Mesh& mesh, std::vector<std::pair<uint32_t, uifor (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+ out.emplace_back(0, ns); // a good part of the mesh (or no per-vertex marks): everything, in one goreturn;}- // The GPU blocks of every marked vertex.+ auto mark = [&](uint32_t slot) {+ const uint32_t g = slot >> kBlockShift;+ if (!marked_[g]) {+ marked_[g] = 1;+ blocks_.push_back(g);+ }+ };+ // The GPU blocks of every slot 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) {+ const uint64_t copies = w < hasCopy_.size() ? hasCopy_[w] : 0;for (uint64_t bits = mesh.dirtyBits[w]; bits; bits &= bits - 1) {- const size_t v = w * 64 + size_t(std::countr_zero(bits));+ const int bit = std::countr_zero(bits);+ const size_t v = w * 64 + size_t(bit);if (v >= nv) break;- const uint32_t g = toGpu_[v] >> kBlockShift;- if (!marked_[g]) {- marked_[g] = 1;- blocks_.push_back(g);- }+ if (toGpu_[v] == kNone) continue;+ mark(toGpu_[v]);+ if ((copies >> bit) & 1)+ for (auto it = std::lower_bound(copies_.begin(), copies_.end(), std::make_pair(uint32_t(v), 0u));+ it != copies_.end() && it->first == v; ++it)+ mark(it->second);}}}std::sort(blocks_.begin(), blocks_.end());+ constexpr uint32_t kJoin = 4 * kBlock; // fewer, slightly longer uploadsfor (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;+ const uint32_t lo = g << kBlockShift, hi = std::min(ns, (g + 1) << kBlockShift);+ if (!out.empty() && lo - out.back().second <= kJoin) 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();+ return (toGpu_.capacity() + fromGpu_.capacity() + blocks_.capacity()) * sizeof(uint32_t) + marked_.capacity() ++ copies_.capacity() * sizeof(copies_[0]) + hasCopy_.capacity() * sizeof(uint64_t) + clusters_.capacity() * sizeof(Cluster) ++ chunks_.capacity() * sizeof(uint32_t);}} // namespace sfdiff --git a/src/core/UploadOrder.h b/src/core/UploadOrder.hindex 3a3d6c96..6c3ea488 100644--- a/src/core/UploadOrder.h+++ b/src/core/UploadOrder.h@@ -1,15 +1,23 @@// 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.+// How a renderer lays a mesh out 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.+// The triangles are cut into chunks of kChunkTriangles consecutive triangles (a compact bit of surface on any+// mesh whose triangle order is not random: a divided mesh keeps each cage face's triangles together at every+// level, imports and remeshes come out in scan order). The chunks are sorted along a Morton curve of their+// centres, and every kClusterChunks consecutive chunks make a cluster: a compact patch of up to+// kClusterTriangles triangles even when the cage faces are small (a remesh divided three times). The renderer+// culls whole clusters each frame (outside the view, or all facing away) and draws the rest with one+// multi-draw. Every cluster has its own contiguous range of GPU vertices (block aligned), numbered by first+// use in its triangles, so its indices fit in 16 bits relative to the range's start. Vertices on the seams+// between clusters have a copy in each cluster that uses them (about a tenth of the vertices). The mesh's own+// numbering is untouched.+//+// Subdivided meshes number their vertices level by level, so the vertices under one brush dab are scattered+// over the whole index range; in this layout they sit in a few clusters. dirtyRuns() turns the vertices the+// mesh marked dirty (Mesh::markDirty) into a few short GPU index ranges (every copy included) to gather and+// upload.#pragma once#include "core/Mesh.h"@@ -23,23 +31,59 @@ namespace sf {class UploadOrder {public:- static constexpr uint32_t kBlockShift = 9, kBlock = 1u << kBlockShift; // GPU upload granularity: 512 vertices+ static constexpr uint32_t kBlockShift = 7, kBlock = 1u << kBlockShift; // GPU upload granularity: 128 vertices+ static constexpr uint32_t kChunkShift = 6, kChunkTriangles = 1u << kChunkShift; // 64 triangles+ static constexpr uint32_t kClusterChunks = 64, kClusterTriangles = kChunkTriangles * kClusterChunks; // 4096+ static constexpr uint32_t kNone = ~0u;++ struct Cluster {+ uint32_t firstVertex = 0; // GPU vertex range [firstVertex, firstVertex + vertexCount), block aligned+ uint32_t vertexCount = 0;+ uint32_t firstTriangle = 0; // position of its first triangle in the clusters' triangle order+ uint32_t triangleCount = 0;+ };- // Numbers the vertices by first use in the triangle list (unused vertices last).+ // Lays out `mesh` (its triangles and vertex count, and positions for the chunk order).void build(const Mesh& mesh);+ // GPU vertex slots, including the padding that block-aligns each cluster.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.+ // fromGpu()[i] = the mesh vertex GPU slot i holds (padding repeats a vertex of its cluster).std::span<const uint32_t> fromGpu() const { return fromGpu_; }+ // The first GPU slot holding mesh vertex v (kNone for vertices no triangle uses).+ uint32_t toGpu(uint32_t v) const { return toGpu_[v]; }+ // Every GPU slot holding v, in increasing order (one for most vertices).+ void slotsOf(uint32_t v, std::vector<uint32_t>& out) const;++ // The clusters, in order; their triangles follow each other in the "clusters' triangle order" (the order+ // a renderer keeps per-triangle data in).+ std::span<const Cluster> clusters() const { return clusters_; }+ size_t triangleCount() const { return triangles_; }+ uint32_t firstTriangle(uint32_t c) const { return clusters_[c].firstTriangle; }+ uint32_t clusterTriangles(uint32_t c) const { return clusters_[c].triangleCount; }+ // The mesh triangle at position i of cluster c (i < clusterTriangles(c)).+ uint32_t triangle(uint32_t c, uint32_t i) const {+ return (chunks_[size_t(c) * kClusterChunks + (i >> kChunkShift)] << kChunkShift) + (i & (kChunkTriangles - 1));+ }+ // The cluster holding GPU slot i.+ uint32_t clusterOfSlot(uint32_t i) const;+ // The triangles of cluster c as indices into its vertex range (3 per triangle; a triangle with an index+ // outside the mesh becomes 0, 0, 0). `mesh` must be the one built from. Thread safe.+ void localTriangles(const Mesh& mesh, uint32_t c, uint16_t* out) const;- // 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.+ // Sorted, disjoint GPU index ranges [begin, end) holding every slot of every vertex marked dirty, in whole+ // blocks (runs a few blocks 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:+ size_t triangles_ = 0, vertices_ = 0;+ std::vector<Cluster> clusters_;+ std::vector<uint32_t> chunks_; // chunk ids in cluster order (the mesh's partial last chunk, if any, last)std::vector<uint32_t> toGpu_, fromGpu_;+ // The copies: (mesh vertex, slot) for every slot but a vertex's first, sorted; hasCopy_ has a bit per vertex.+ std::vector<std::pair<uint32_t, uint32_t>> copies_;+ std::vector<uint64_t> hasCopy_;std::vector<uint8_t> marked_; // per GPU block, while dirtyRuns collects (all zero between calls)std::vector<uint32_t> blocks_; // the marked blocks};diff --git a/src/render/Clusters.cpp b/src/render/Clusters.cppnew file mode 100644index 00000000..471625c0--- /dev/null+++ b/src/render/Clusters.cpp@@ -0,0 +1,222 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+#include "render/Clusters.h"++#include "core/ThreadPool.h"++#include <algorithm>+#include <array>+#include <atomic>+#include <cmath>+#include <vector>++namespace sf {++PositionGrid PositionGrid::around(const AABB& box) {+ PositionGrid g;+ if (!box.valid()) return g;+ const glm::vec3 e = box.extent();+ const float size = std::max({e.x, e.y, e.z});+ const float scale = std::max({std::abs(box.min.x), std::abs(box.min.y), std::abs(box.min.z), std::abs(box.max.x),+ std::abs(box.max.y), std::abs(box.max.z), 1e-3f});+ const float pad = std::max(size * 0.125f, scale * 1e-4f);+ g.origin = box.min - glm::vec3(pad);+ g.step = (size + 2.0f * pad) / float(kMax);+ return g;+}++bool PositionGrid::contains(const glm::vec3& p) const {+ const glm::vec3 q = (p - origin) / step;+ return q.x >= 0.0f && q.y >= 0.0f && q.z >= 0.0f && q.x <= float(kMax) && q.y <= float(kMax) && q.z <= float(kMax);+}++namespace {++uint32_t quantize(float v, float origin, float step, bool& inside) {+ const float q = std::round((v - origin) / step);+ if (!(q >= 0.0f && q <= float(PositionGrid::kMax))) { // NaN too+ inside = false;+ return q > 0.0f ? PositionGrid::kMax : 0u;+ }+ return uint32_t(q);+}++uint32_t snorm(float v, int bits) {+ const int range = (1 << (bits - 1)) - 1; // e.g. 11 bits: -1023..1023 stored as 1..2047, so 0 is exact+ const int q = int(std::lround(std::clamp(v, -1.0f, 1.0f) * float(range)));+ return uint32_t(q + range + 1);+}++float unsnorm(uint32_t q, int bits) {+ const int range = (1 << (bits - 1)) - 1;+ return float(int(q) - range - 1) / float(range);+}++} // namespace++bool packVertex(const PositionGrid& grid, const glm::vec3& p, const glm::vec3& n, float curvature, PackedVertex& out) {+ bool inside = true;+ const uint64_t x = quantize(p.x, grid.origin.x, grid.step, inside);+ const uint64_t y = quantize(p.y, grid.origin.y, grid.step, inside);+ const uint64_t z = quantize(p.z, grid.origin.z, grid.step, inside);+ const uint64_t bits = x | (y << 21) | (z << 42);+ out.pos[0] = uint32_t(bits);+ out.pos[1] = uint32_t(bits >> 32);+ // Octahedral normal: project onto |x| + |y| + |z| = 1, fold the lower half over the diagonals.+ const float l1 = std::abs(n.x) + std::abs(n.y) + std::abs(n.z);+ glm::vec2 e(0.0f);+ if (l1 > 0.0f && std::isfinite(l1)) {+ const glm::vec3 a = n / l1;+ e = glm::vec2(a.x, a.y);+ if (a.z < 0.0f)+ e = glm::vec2((1.0f - std::abs(a.y)) * (a.x >= 0.0f ? 1.0f : -1.0f), (1.0f - std::abs(a.x)) * (a.y >= 0.0f ? 1.0f : -1.0f));+ }+ const float c = std::isfinite(curvature) ? curvature / kCurvatureRange : 0.0f;+ out.normal = snorm(e.x, 11) | (snorm(e.y, 11) << 11) | (snorm(c, 10) << 22);+ return inside;+}++glm::vec3 unpackPosition(const PositionGrid& grid, const PackedVertex& v) {+ const uint64_t bits = uint64_t(v.pos[0]) | (uint64_t(v.pos[1]) << 32);+ const glm::vec3 q(float(bits & 0x1FFFFF), float((bits >> 21) & 0x1FFFFF), float((bits >> 42) & 0x1FFFFF));+ return grid.origin + q * grid.step;+}++glm::vec3 unpackNormal(const PackedVertex& v) {+ const glm::vec2 e(unsnorm(v.normal & 2047u, 11), unsnorm((v.normal >> 11) & 2047u, 11));+ glm::vec3 n(e.x, e.y, 1.0f - std::abs(e.x) - std::abs(e.y));+ const float t = std::max(-n.z, 0.0f);+ n.x += n.x >= 0.0f ? -t : t;+ n.y += n.y >= 0.0f ? -t : t;+ return glm::normalize(n);+}++float unpackCurvature(const PackedVertex& v) { return unsnorm(v.normal >> 22, 10) * kCurvatureRange; }++uint32_t packColor(const glm::vec3& c) {+ static const std::vector<uint8_t> table = [] {+ std::vector<uint8_t> t(65536);+ for (size_t i = 0; i < t.size(); ++i) t[i] = uint8_t(std::lround(std::pow(double(i) / 65535.0, 1.0 / 2.2) * 255.0));+ return t;+ }();+ auto ch = [&](float v) { return uint32_t(table[size_t(std::clamp(v, 0.0f, 1.0f) * 65535.0f + 0.5f)]); };+ return ch(c.x) | (ch(c.y) << 8) | (ch(c.z) << 16) | (255u << 24);+}++ClusterBounds clusterBounds(const Mesh& mesh, const UploadOrder& order, uint32_t cluster) {+ ClusterBounds b;+ const size_t nv = mesh.vertexCount(), count = order.clusterTriangles(cluster);+ const auto& P = mesh.positions;+ thread_local std::vector<glm::vec3> normals; // unit face normals (zero: degenerate or invalid)+ normals.resize(count);+ AABB box;+ glm::vec3 sum(0.0f);+ for (uint32_t i = 0; i < count; ++i) {+ const glm::uvec3& tri = mesh.triangles[order.triangle(cluster, i)];+ glm::vec3& n = normals[i];+ n = glm::vec3(0.0f);+ if (tri.x >= nv || tri.y >= nv || tri.z >= nv) continue;+ const glm::vec3 a = P[tri.x], e1 = P[tri.y] - a, e2 = P[tri.z] - a;+ box.expand(a);+ box.expand(P[tri.y]);+ box.expand(P[tri.z]);+ const glm::vec3 c = glm::cross(e1, e2);+ const float l2 = glm::dot(c, c);+ if (l2 > 0.0f && std::isfinite(l2)) {+ n = c / std::sqrt(l2);+ sum += n;+ }+ }+ if (!box.valid()) return b;+ b.center = box.center();+ const float sl = glm::length(sum);+ const glm::vec3 axis = sl > 1e-6f ? sum / sl : glm::vec3(0.0f);+ float r2 = 0.0f, minDot = 1.0f;+ bool faces = false;+ for (uint32_t i = 0; i < count; ++i) {+ const glm::vec3& n = normals[i];+ const glm::uvec3& tri = mesh.triangles[order.triangle(cluster, i)];+ if (n.x == 0.0f && n.y == 0.0f && n.z == 0.0f) {+ if (tri.x >= nv || tri.y >= nv || tri.z >= nv) continue;+ } else {+ minDot = std::min(minDot, glm::dot(n, axis));+ faces = true;+ }+ for (int c = 0; c < 3; ++c) {+ const glm::vec3 d = P[tri[c]] - b.center;+ r2 = std::max(r2, glm::dot(d, d));+ }+ }+ // A little slack for float rounding and the GPU's quantised positions.+ b.radius = std::sqrt(r2) * 1.0001f + 1e-6f;+ if (faces && sl > 1e-6f && minDot > 0.0f) {+ b.axis = axis;+ b.coneAngle = std::acos(std::min(minDot, 1.0f));+ }+ return b;+}++bool closedOriented(const Mesh& mesh) {+ const size_t nv = mesh.vertexCount(), nt = mesh.triangleCount();+ if (nt == 0) return false;+ std::atomic<bool> ok{true};+ poolFor(nv, [&](size_t v0, size_t v1) {+ thread_local std::vector<uint32_t> outs, ins;+ for (size_t a = v0; a < v1 && ok.load(std::memory_order_relaxed); ++a) {+ outs.clear();+ ins.clear();+ for (uint32_t t : mesh.vertexTriangles(uint32_t(a))) {+ if (t >= nt) continue;+ const glm::uvec3& tri = mesh.triangles[t];+ for (int c = 0; c < 3; ++c)+ if (tri[c] == a) {+ outs.push_back(tri[(c + 1) % 3]);+ ins.push_back(tri[(c + 2) % 3]);+ }+ }+ std::sort(outs.begin(), outs.end());+ std::sort(ins.begin(), ins.end());+ if (outs != ins) ok.store(false, std::memory_order_relaxed);+ }+ }, 1 << 14);+ return ok.load();+}++CullView CullView::from(const glm::mat4& view, const glm::mat4& proj) {+ CullView v;+ const glm::mat4 m = proj * view;+ auto row = [&](int i) { return glm::vec4(m[0][i], m[1][i], m[2][i], m[3][i]); };+ const glm::vec4 r0 = row(0), r1 = row(1), r2 = row(2), r3 = row(3);+ const std::array<glm::vec4, 6> p = {r3 + r0, r3 - r0, r3 + r1, r3 - r1, r3 + r2, r3 - r2};+ for (int i = 0; i < 6; ++i) {+ const float l = glm::length(glm::vec3(p[size_t(i)]));+ v.planes[i] = l > 0.0f ? p[size_t(i)] / l : glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);+ }+ const glm::mat4 inv = glm::inverse(view);+ v.eye = glm::vec3(inv[3]);+ v.forward = glm::normalize(glm::vec3(inv * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));+ v.nearDistance = std::abs(proj[2][2] - 1.0f) > 1e-12f ? proj[3][2] / (proj[2][2] - 1.0f) : 0.0f;+ return v;+}++bool insideFrustum(const CullView& v, const ClusterBounds& b) {+ if (b.radius < 0.0f) return false;+ for (const glm::vec4& p : v.planes)+ if (glm::dot(glm::vec3(p), b.center) + p.w < -b.radius) return false;+ return true;+}++bool facingAway(const CullView& v, const ClusterBounds& b) {+ if (!v.backfaces || b.coneAngle < 0.0f) return false;+ const glm::vec3 d = b.center - v.eye;+ const float dist = glm::length(d);+ if (!(dist > b.radius * 1.001f)) return false;+ // Every normal is within coneAngle of the axis, every direction from the eye to the cluster within+ // asin(r / dist) of the centre's: faces point away when the three angles stay under 90 degrees.+ const float gamma = std::acos(std::clamp(glm::dot(d / dist, b.axis), -1.0f, 1.0f));+ const float beta = std::asin(std::min(1.0f, b.radius / dist));+ constexpr float kMargin = 0.02f; // radians+ return gamma + beta + b.coneAngle < 1.5707963f - kMargin;+}++} // namespace sfdiff --git a/src/render/Clusters.h b/src/render/Clusters.hnew file mode 100644index 00000000..72e43572--- /dev/null+++ b/src/render/Clusters.h@@ -0,0 +1,78 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// The CPU side of the renderer's mesh drawing, kept apart from GL so tests can check it:+//+// - The compact vertex the GPU keeps (12 bytes, was 28): the position on a 21-bit grid over the mesh's bounds+// (with a margin, so sculpting rarely leaves it; one grid for the whole mesh, so a vertex copied into two+// clusters lands on exactly the same point and seams can't crack), an octahedral normal (2 x 11 bits) and the+// curvature the shading uses for cavities (10 bits). The shader decodes it the same way (Renderer.cpp).+// - Culling per cluster (core/UploadOrder.h): outside the view, or every triangle facing away. The second only+// holds for a closed mesh seen from outside (closedOriented, and nothing cut by the near plane): its back+// faces are then always behind front faces. Open meshes, hidden triangles and views from inside draw them.+#pragma once++#include "core/Mesh.h"+#include "core/UploadOrder.h"++#include <algorithm>+#include <cstdint>++namespace sf {++// Positions: origin + q * step, q in [0, 2^21) per axis.+struct PositionGrid {+ glm::vec3 origin{0.0f};+ float step = 1.0f;+ static constexpr uint32_t kMax = (1u << 21) - 1;+ // A grid over `box` with a margin of an eighth of its size on every side.+ static PositionGrid around(const AABB& box);+ bool contains(const glm::vec3& p) const;+};++struct PackedVertex {+ uint32_t pos[2]; // x: bits 0-20, y: 21-41, z: 42-62+ uint32_t normal; // octahedral x, y (11 bits each, signed), curvature (10 bits, signed)+};+static_assert(sizeof(PackedVertex) == 12);++// Packs a vertex; returns false (and clamps) when p is outside the grid.+bool packVertex(const PositionGrid& grid, const glm::vec3& p, const glm::vec3& n, float curvature, PackedVertex& out);+// What the shader decodes (tests).+glm::vec3 unpackPosition(const PositionGrid& grid, const PackedVertex& v);+glm::vec3 unpackNormal(const PackedVertex& v);+float unpackCurvature(const PackedVertex& v);+constexpr float kCurvatureRange = 0.25f; // stored range +-; the shading saturates at 0.16 / 0.2++// Polypaint colours in 8 bits per channel, gamma 2.2 encoded so dark colours keep their steps (the shader+// decodes with pow 2.2). A table lookup, it is called for every vertex of an upload.+uint32_t packColor(const glm::vec3& linear);+inline uint8_t packMask(float m) { return uint8_t(std::clamp(m, 0.0f, 1.0f) * 255.0f + 0.5f); }++// A cluster's bounding sphere and the cone holding its triangles' face normals (axis, half-angle in radians;+// coneAngle < 0: no useful cone).+struct ClusterBounds {+ glm::vec3 center{0.0f};+ float radius = -1.0f; // < 0: empty+ glm::vec3 axis{0.0f, 0.0f, 1.0f};+ float coneAngle = -1.0f;+};+ClusterBounds clusterBounds(const Mesh& mesh, const UploadOrder& order, uint32_t cluster);++// Every directed edge a->b has as many b->a: the mesh is the closed, consistently wound boundary of a volume+// (it may intersect itself). Then from outside, every back face lies behind a front face.+bool closedOriented(const Mesh& mesh);++// One frame's culling: the view's frustum planes, the eye, and whether back-facing clusters may go.+struct CullView {+ glm::vec4 planes[6];+ glm::vec3 eye{0.0f};+ float nearDistance = 0.0f;+ glm::vec3 forward{0.0f, 0.0f, -1.0f};+ bool backfaces = false; // cull clusters facing away+ static CullView from(const glm::mat4& view, const glm::mat4& proj);+};+bool insideFrustum(const CullView& v, const ClusterBounds& b);+bool facingAway(const CullView& v, const ClusterBounds& b); // every triangle of the cluster faces away from the eye++} // namespace sfdiff --git a/src/render/GL.h b/src/render/GL.hindex 71e23b27..e9f9698f 100644--- a/src/render/GL.h+++ b/src/render/GL.h@@ -93,6 +93,11 @@ constexpr GLenum GL_LINEAR_MIPMAP_LINEAR = 0x2703;constexpr GLenum GL_UNPACK_ROW_LENGTH = 0x0CF2;constexpr GLenum GL_TEXTURE3 = 0x84C3;constexpr GLenum GL_TEXTURE4 = 0x84C4;+constexpr GLenum GL_POINTS = 0x0000;+constexpr GLenum GL_UNSIGNED_SHORT = 0x1403;+constexpr GLenum GL_CULL_FACE = 0x0B44;+constexpr GLenum GL_R16UI = 0x8234;+constexpr GLenum GL_RED_INTEGER = 0x8D94;constexpr GLbitfield GL_DEPTH_BUFFER_BIT = 0x0100;constexpr GLbitfield GL_COLOR_BUFFER_BIT = 0x4000;@@ -171,7 +176,10 @@ constexpr GLbitfield GL_COLOR_BUFFER_BIT = 0x4000;X(void, Uniform2f, GLint, GLfloat, GLfloat) \X(void, DisableVertexAttribArray, GLuint) \X(void, VertexAttrib1f, GLuint, GLfloat) \- X(void, VertexAttrib3f, GLuint, GLfloat, GLfloat, GLfloat)+ X(void, VertexAttrib3f, GLuint, GLfloat, GLfloat, GLfloat) \+ X(void, VertexAttrib4f, GLuint, GLfloat, GLfloat, GLfloat, GLfloat) \+ X(void, DrawElementsBaseVertex, GLenum, GLsizei, GLenum, const void*, GLint) \+ X(void, MultiDrawElementsBaseVertex, GLenum, const GLsizei*, GLenum, const void* const*, GLsizei, const GLint*)namespace gl {diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cppindex 63a7bf86..6d466e09 100644--- a/src/render/Renderer.cpp+++ b/src/render/Renderer.cpp@@ -10,6 +10,10 @@#include <glm/gtc/type_ptr.hpp>#include <algorithm>+#include <atomic>+#include <chrono>+#include <cstdio>+#include <cstdlib>#include <limits>#include <vector>@@ -17,100 +21,93 @@ namespace sf {namespace {-const char* kMeshVS = R"(#version 330 core-layout(location = 0) in vec3 aPos;-layout(location = 1) in vec3 aNormal;-layout(location = 2) in float aCurvature;-layout(location = 3) in vec3 aColor; // polypaint (constant white when the mesh has none)-layout(location = 4) in float aMask; // constant 0 when the mesh has no mask+// Mesh vertices are PackedVertex (render/Clusters.h): the position on the slot's grid (21 bits per axis in two+// words), an octahedral normal and the curvature in a third. Decoded here exactly as Clusters.cpp does.+// Compiled per feature set: PAINT (polypaint colour and mask attributes), PRIM (per-triangle data: the first+// drawn triangle of the vertex's cluster, from a per-block table indexed by gl_VertexID, or uPrimBase when the+// clusters are drawn one by one), TEXTURED and NORMALMAP (with a geometry shader, below).+const char* kMeshVS = R"(+layout(location = 0) in uvec2 aPos;+layout(location = 1) in uint aNormal;+#ifdef PAINT+layout(location = 2) in vec4 aColor; // polypaint, gamma 2.2 encoded (constant white when the mesh has none)+layout(location = 3) in float aMask; // constant 0 when the mesh has no mask+out vec4 vPaint; // linear colour, mask+#endifuniform mat4 uView;uniform mat4 uProj;-out vec3 vNormal;-out vec3 vPos;-out float vCurvature;-out vec3 vColor;-out float vMask;+uniform vec3 uGridOrigin;+uniform float uGridStep;+#ifdef PRIM+uniform usamplerBuffer uClusterPrims;+uniform int uPrimBase; // >= 0: this cluster's first drawn triangle (clusters drawn one by one)+flat out int vPrimBase;+#endif+out vec4 vNormalCurv; // view-space normal, curvature+vec3 decodeNormal(uint a) {+ vec2 e = vec2(float(int(a & 2047u) - 1024), float(int((a >> 11) & 2047u) - 1024)) / 1023.0;+ vec3 n = vec3(e, 1.0 - abs(e.x) - abs(e.y));+ float t = max(-n.z, 0.0);+ n.x += n.x >= 0.0 ? -t : t;+ n.y += n.y >= 0.0 ? -t : t;+ return normalize(n);+}void main() {- vec4 p = uView * vec4(aPos, 1.0);- vPos = p.xyz;- vCurvature = aCurvature;- vColor = aColor;- vMask = aMask;- vNormal = mat3(uView) * aNormal;+ uvec3 q = uvec3(aPos.x & 0x1FFFFFu, (aPos.x >> 21) | ((aPos.y & 0x3FFu) << 11), (aPos.y >> 10) & 0x1FFFFFu);+ vec4 p = uView * vec4(uGridOrigin + vec3(q) * uGridStep, 1.0);+ vNormalCurv = vec4(mat3(uView) * decodeNormal(aNormal), float(int(aNormal >> 22) - 512) / 511.0 * 0.25);+#ifdef PAINT+ vPaint = vec4(pow(aColor.rgb, vec3(2.2)), aMask);+#endif+#ifdef PRIM+ vPrimBase = uPrimBase >= 0 ? uPrimBase : int(texelFetch(uClusterPrims, gl_VertexID >> 7).r);+#endifgl_Position = uProj * p;})";-// Studio "clay" shading: wrapped key light, cool fill, rim, soft spec, and curvature-based-// cavity/ridge shading so sculpted detail reads at a glance.// Texture painting (core/Texture.h): the same shading with the per-corner UVs of each drawn triangle// fetched from a texture buffer by primitive id (the vertices are shared, the UVs are not). Only meshes// with a texture use it, so sculpting plain meshes keeps the geometry-shader-free path.-const char* kMeshGS = R"(#version 330 core+// With NORMALMAP, a baked normal map preview (core/Bake.h): each corner's MikkTSpace tangent (xyz, bitangent+// sign in w) too, fetched like the UVs and turned into view space.+const char* kMeshGS = R"(layout(triangles) in;layout(triangle_strip, max_vertices = 3) out;-in vec3 vNormal[];-in vec3 vPos[];-in float vCurvature[];-in vec3 vColor[];-in float vMask[];+in vec4 vNormalCurv[];+#ifdef PAINT+in vec4 vPaint[];+#endif+flat in int vPrimBase[];uniform samplerBuffer uUvs; // RG32F, 3 corners per drawn triangle-out FragData {- vec3 vNormal;- vec3 vPos;- float vCurvature;- vec3 vColor;- float vMask;- vec2 vUv;-} o;-void main() {- for (int i = 0; i < 3; ++i) {- o.vNormal = vNormal[i];- o.vPos = vPos[i];- o.vCurvature = vCurvature[i];- o.vColor = vColor[i];- o.vMask = vMask[i];- o.vUv = texelFetch(uUvs, gl_PrimitiveIDIn * 3 + i).xy;- gl_PrimitiveID = gl_PrimitiveIDIn;- gl_Position = gl_in[i].gl_Position;- EmitVertex();- }- EndPrimitive();-}-)";--// Baked normal map preview (core/Bake.h): kMeshGS plus each corner's MikkTSpace tangent (xyz, bitangent sign-// in w), fetched like the UVs and turned into view space.-const char* kMeshNormalGS = R"(#version 330 core-layout(triangles) in;-layout(triangle_strip, max_vertices = 3) out;-in vec3 vNormal[];-in vec3 vPos[];-in float vCurvature[];-in vec3 vColor[];-in float vMask[];-uniform samplerBuffer uUvs; // RG32F, 3 corners per drawn triangle+#ifdef NORMALMAPuniform samplerBuffer uTangents; // RGBA32F, 3 corners per drawn triangleuniform mat4 uView;+#endifout FragData {- vec3 vNormal;- vec3 vPos;- float vCurvature;- vec3 vColor;- float vMask;+ vec4 vNormalCurv;+#ifdef PAINT+ vec4 vPaint;+#endifvec2 vUv;+#ifdef NORMALMAPvec4 vTangent;+#endif+ flat int vPrimBase;} o;void main() {+ int prim = vPrimBase[0] + gl_PrimitiveIDIn;for (int i = 0; i < 3; ++i) {- o.vNormal = vNormal[i];- o.vPos = vPos[i];- o.vCurvature = vCurvature[i];- o.vColor = vColor[i];- o.vMask = vMask[i];- o.vUv = texelFetch(uUvs, gl_PrimitiveIDIn * 3 + i).xy;- vec4 t = texelFetch(uTangents, gl_PrimitiveIDIn * 3 + i);+ o.vNormalCurv = vNormalCurv[i];+#ifdef PAINT+ o.vPaint = vPaint[i];+#endif+ o.vUv = texelFetch(uUvs, prim * 3 + i).xy;+#ifdef NORMALMAP+ vec4 t = texelFetch(uTangents, prim * 3 + i);o.vTangent = vec4(mat3(uView) * t.xyz, t.w);+#endif+ o.vPrimBase = vPrimBase[0];gl_PrimitiveID = gl_PrimitiveIDIn;gl_Position = gl_in[i].gl_Position;EmitVertex();@@ -119,20 +116,21 @@ void main() {})";-// Compiled three times: as is, with TEXTURED defined (after kMeshGS, multiplying the painted texture in), and-// with NORMALMAP too (after kMeshNormalGS: a baked normal map bends the shading normal). (after kMeshGS, multiplying the painted texture in).+// Studio "clay" shading: wrapped key light, cool fill, rim, soft spec, and curvature-based+// cavity/ridge shading so sculpted detail reads at a glance. The view direction comes from the pixel's+// position (uViewport, uProjScale) rather than an interpolated position, which saves the vertices a varying.const char* kMeshFS = R"(#ifdef TEXTUREDin FragData {- vec3 vNormal;- vec3 vPos;- float vCurvature;- vec3 vColor;- float vMask;+ vec4 vNormalCurv;+#ifdef PAINT+ vec4 vPaint;+#endifvec2 vUv;#ifdef NORMALMAPvec4 vTangent;#endif+ flat int vPrimBase;};uniform sampler2D uPaintTex; // sRGB texture: samples come back linear#ifdef NORMALMAP@@ -141,12 +139,23 @@ uniform int uHasPaint; // 1: a painted texture is bound to uPaintTex toouniform int uNormalFlipY; // 1: DirectX convention (green down)#endif#else-in vec3 vNormal;-in vec3 vPos;-in float vCurvature;-in vec3 vColor;-in float vMask;+in vec4 vNormalCurv;+#ifdef PAINT+in vec4 vPaint;#endif+#ifdef PRIM+flat in int vPrimBase;+#endif+#endif+#ifdef PAINT+#define V_COLOR vPaint.rgb+#define V_MASK vPaint.a+#else+#define V_COLOR vec3(1.0)+#define V_MASK 0.0+#endif+uniform vec4 uViewport; // the view's rectangle in window pixels+uniform vec2 uProjScale; // projection x and y scaleuniform vec3 uBaseColor;uniform int uPaint; // 1: vertex colours replace the base colouruniform int uShowMask; // 1: darken masked areas@@ -176,15 +185,19 @@ vec3 groupColor(uint id) {void main() {vec3 albedo = uBaseColor;vec3 tint = vec3(1.0); // multiplies a matcap: polypaint or polygroup colours- if (uGroups == 1) albedo = tint = groupColor(texelFetch(uGroupIds, gl_PrimitiveID).r);- else if (uGroups == 2) albedo = tint = groupColor(0u);- else if (uPaint == 1) albedo = tint = vColor;+#ifdef PRIM+ if (uGroups == 1) albedo = tint = groupColor(texelFetch(uGroupIds, vPrimBase + gl_PrimitiveID).r);+ else+#endif+ if (uGroups != 0) albedo = tint = groupColor(0u);+ else if (uPaint == 1) albedo = tint = V_COLOR;#if defined(NORMALMAP)- if (uGroups == 0 && uHasPaint == 1) albedo = tint = (uPaint == 1 ? vColor : vec3(1.0)) * texture(uPaintTex, vUv).rgb;+ if (uGroups == 0 && uHasPaint == 1) albedo = tint = (uPaint == 1 ? V_COLOR : vec3(1.0)) * texture(uPaintTex, vUv).rgb;#elif defined(TEXTURED)- if (uGroups == 0) albedo = tint = (uPaint == 1 ? vColor : vec3(1.0)) * texture(uPaintTex, vUv).rgb;+ if (uGroups == 0) albedo = tint = (uPaint == 1 ? V_COLOR : vec3(1.0)) * texture(uPaintTex, vUv).rgb;#endif- vec3 n = normalize(vNormal);+ vec3 n = normalize(vNormalCurv.xyz);+ float curvature = vNormalCurv.w;#ifdef NORMALMAP// MikkTSpace's pixel shader: bitangent rebuilt per pixel, n = normalize(tn.x T + tn.y B + tn.z N).vec3 tn = texture(uNormalTex, vUv).xyz * 2.0 - 1.0;@@ -195,17 +208,18 @@ void main() {n = normalize(tn.x * T + tn.y * B + tn.z * n);#endifif (!gl_FrontFacing) n = -n;- vec3 v = normalize(-vPos);+ vec2 ndc = (gl_FragCoord.xy - uViewport.xy) / uViewport.zw * 2.0 - 1.0;+ vec3 v = normalize(vec3(-ndc / uProjScale, 1.0));if (uMatcap == 1) {// View-aligned basis so the material stays put towards the edges of a perspective view.vec3 bx = normalize(vec3(v.z, 0.0, -v.x));vec3 by = cross(v, bx);vec2 uv = vec2(dot(bx, n), dot(by, n)) * 0.495 + 0.5;vec3 mc = pow(texture(uMatcapTex, uv).rgb, vec3(2.2));- float mcrease = smoothstep(0.0, 0.16, vCurvature);- float mridge = smoothstep(0.0, 0.2, -vCurvature);+ float mcrease = smoothstep(0.0, 0.16, curvature);+ float mridge = smoothstep(0.0, 0.2, -curvature);vec3 c = mc * tint * (1.0 - uMatcapCavity.x * mcrease) * (1.0 + uMatcapCavity.y * mridge);- if (uShowMask == 1) c *= 1.0 - 0.75 * clamp(vMask, 0.0, 1.0);+ if (uShowMask == 1) c *= 1.0 - 0.75 * clamp(V_MASK, 0.0, 1.0);fragColor = finish(c);return;}@@ -218,22 +232,51 @@ 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 curvatureAt in Renderer.cpp).- float crease = smoothstep(0.0, 0.16, vCurvature);- float ridge = smoothstep(0.0, 0.2, -vCurvature);+ // curvature > 0 in creases, < 0 on ridges (see curvatureAt in Renderer.cpp).+ float crease = smoothstep(0.0, 0.16, curvature);+ float ridge = smoothstep(0.0, 0.2, -curvature);float cavity = 1.0 - 0.62 * crease;vec3 c = albedo * (0.12 * hemi + 0.95 * diff * diff + fillL * vec3(0.7, 0.8, 1.0)) * mix(0.7, 1.0, facing) * cavity;c *= mix(vec3(1.0), vec3(0.72, 0.55, 0.5), crease * 0.7); // warm, deeper colour in the cracksc += albedo * ridge * 0.3 * diff; // ridges catch the key lightc += vec3(spec) * (1.0 - crease) + rim * vec3(0.8, 0.85, 1.0);- if (uShowMask == 1) c *= 1.0 - 0.75 * clamp(vMask, 0.0, 1.0);+ if (uShowMask == 1) c *= 1.0 - 0.75 * clamp(V_MASK, 0.0, 1.0);fragColor = finish(c);})";+// The wireframe: positions only. Every edge is drawn once; the old overlay drew each edge twice (once per+// triangle) at 0.35 opacity, which this alpha matches.+const char* kWireVS = R"(#version 330 core+layout(location = 0) in uvec2 aPos;+uniform mat4 uView;+uniform mat4 uProj;+uniform vec3 uGridOrigin;+uniform float uGridStep;+void main() {+ uvec3 q = uvec3(aPos.x & 0x1FFFFFu, (aPos.x >> 21) | ((aPos.y & 0x3FFu) << 11), (aPos.y >> 10) & 0x1FFFFFu);+ gl_Position = uProj * (uView * vec4(uGridOrigin + vec3(q) * uGridStep, 1.0));+}+)";+const char* kWireFS = R"(#version 330 core+uniform float uAlpha;out vec4 fragColor;-void main() { fragColor = vec4(0.05, 0.05, 0.08, 0.35); }+void main() { fragColor = vec4(0.05, 0.05, 0.08, uAlpha); }+)";++// checkVertexIdBase: does gl_VertexID include the base vertex of glDrawElementsBaseVertex (it should)?+const char* kVertexIdVS = R"(#version 330 core+flat out uint vId;+void main() {+ vId = uint(gl_VertexID);+ gl_Position = vec4(0.0, 0.0, 0.0, 1.0);+}+)";+const char* kVertexIdFS = R"(#version 330 core+flat in uint vId;+out uint fragId;+void main() { fragId = vId; })";const char* kBgVS = R"(#version 330 core@@ -313,6 +356,7 @@ void main() {})";+constexpr GLenum kTexture7 = 0x84C7; // GL_TEXTURE7 (the cluster table)constexpr GLenum kTexture2 = 0x84C2; // GL_TEXTURE2 (matcap)constexpr GLenum kTexture5 = 0x84C5; // kTexture5 (baked normal map)constexpr GLenum kTexture6 = 0x84C6; // kTexture6 (its tangents)@@ -387,13 +431,12 @@ glm::vec3 polygroupColor(uint32_t id) {}bool Renderer::init(std::string& error) {- meshProgram_ = link(kMeshVS, kMeshFS, error, "#version 330 core\n");- if (!meshProgram_) return false;- meshTexProgram_ = link(kMeshVS, kMeshFS, error, "#version 330 core\n#define TEXTURED\n", kMeshGS);- if (!meshTexProgram_) return false;- meshNormalProgram_ = link(kMeshVS, kMeshFS, error, "#version 330 core\n#define TEXTURED\n#define NORMALMAP\n", kMeshNormalGS);- if (!meshNormalProgram_) return false;- wireProgram_ = link(kMeshVS, kWireFS, error);+ for (int f : {0, kPaint}) // the common ones up front, so a broken driver fails here rather than mid-session+ if (!meshProgram(f)) {+ error = programError_;+ return false;+ }+ wireProgram_ = link(kWireVS, kWireFS, error);if (!wireProgram_) return false;bgProgram_ = link(kBgVS, kBgFS, error);if (!bgProgram_) return false;@@ -428,9 +471,94 @@ bool Renderer::init(std::string& error) {gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GLint(GL_CLAMP_TO_EDGE));}gl::BindTexture(GL_TEXTURE_2D, 0);+ vertexIdHasBase_ = checkVertexIdBase();return true;}+const Renderer::MeshProgram* Renderer::meshProgram(int features) {+ if (features & (kTextured | kNormalMap)) features |= kTextured | kPrim;+ MeshProgram& p = programs_[features & 15];+ if (p.id) return &p;+ std::string prefix = "#version 330 core\n";+ if (features & kPaint) prefix += "#define PAINT\n";+ if (features & kPrim) prefix += "#define PRIM\n";+ if (features & kTextured) prefix += "#define TEXTURED\n";+ if (features & kNormalMap) prefix += "#define NORMALMAP\n";+ const std::string vs = prefix + kMeshVS, fs = prefix + kMeshFS, gs = prefix + kMeshGS;+ p.id = link(vs.c_str(), fs.c_str(), programError_, nullptr, (features & kTextured) ? gs.c_str() : nullptr);+ if (!p.id) return nullptr;+ auto at = [&](const char* name) { return gl::GetUniformLocation(p.id, name); };+ p.view = at("uView");+ p.proj = at("uProj");+ p.gridOrigin = at("uGridOrigin");+ p.gridStep = at("uGridStep");+ p.viewport = at("uViewport");+ p.projScale = at("uProjScale");+ p.baseColor = at("uBaseColor");+ p.paint = at("uPaint");+ p.showMask = at("uShowMask");+ p.groups = at("uGroups");+ p.matcap = at("uMatcap");+ p.matcapCavity = at("uMatcapCavity");+ p.dim = at("uDim");+ p.primBase = at("uPrimBase");+ p.hasPaint = at("uHasPaint");+ p.normalFlipY = at("uNormalFlipY");+ // Texture units: groups 1, matcap 2, UVs 3, painted texture 4, normal map 5, tangents 6, cluster table 7.+ gl::UseProgram(p.id);+ gl::Uniform1i(at("uGroupIds"), 1);+ gl::Uniform1i(at("uMatcapTex"), 2);+ gl::Uniform1i(at("uUvs"), 3);+ gl::Uniform1i(at("uPaintTex"), 4);+ gl::Uniform1i(at("uNormalTex"), 5);+ gl::Uniform1i(at("uTangents"), 6);+ gl::Uniform1i(at("uClusterPrims"), 7);+ gl::UseProgram(0);+ return &p;+}++// Draws one point with index 2 and base vertex 1000 into a 1x1 integer target and reads back the gl_VertexID+// the shader saw: 1002 as the spec says, or 2 on drivers that leave the base out (then the per-triangle data of+// polygroups and textures is drawn cluster by cluster with the base as a uniform).+bool Renderer::checkVertexIdBase() {+ std::string error;+ const GLuint prog = link(kVertexIdVS, kVertexIdFS, error);+ if (!prog) return false;+ GLuint fbo = 0, rb = 0, vao = 0, ibo = 0;+ gl::GenFramebuffers(1, &fbo);+ gl::GenRenderbuffers(1, &rb);+ gl::BindRenderbuffer(GL_RENDERBUFFER, rb);+ gl::RenderbufferStorage(GL_RENDERBUFFER, GL_R32UI, 1, 1);+ gl::BindFramebuffer(GL_FRAMEBUFFER, fbo);+ gl::FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb);+ uint32_t id = 0;+ if (gl::CheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) {+ gl::GenVertexArrays(1, &vao);+ gl::GenBuffers(1, &ibo);+ gl::BindVertexArray(vao);+ const uint16_t indices[4] = {0, 1, 2, 3};+ gl::BindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);+ gl::BufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof indices, indices, GL_STATIC_DRAW);+ gl::Viewport(0, 0, 1, 1);+ gl::Disable(GL_DEPTH_TEST);+ gl::UseProgram(prog);+ gl::DrawElementsBaseVertex(GL_POINTS, 1, GL_UNSIGNED_SHORT, reinterpret_cast<const void*>(2 * sizeof(uint16_t)), 1000);+ gl::PixelStorei(GL_PACK_ALIGNMENT, 1);+ gl::ReadPixels(0, 0, 1, 1, GL_RED_INTEGER, GL_UNSIGNED_INT, &id);+ gl::UseProgram(0);+ gl::BindVertexArray(0);+ gl::DeleteBuffers(1, &ibo);+ gl::DeleteVertexArrays(1, &vao);+ }+ gl::BindFramebuffer(GL_FRAMEBUFFER, 0);+ gl::DeleteFramebuffers(1, &fbo);+ gl::DeleteRenderbuffers(1, &rb);+ gl::DeleteProgram(prog);+ for (int i = 0; i < 16 && gl::GetError() != GL_NO_ERROR; ++i) {+ }+ return id == 1002;+}+GLuint Renderer::matcapTexture(int index) const {return index >= 0 && index < int(matcapTextures_.size()) ? matcapTextures_[size_t(index)] : 0;}@@ -441,56 +569,65 @@ Renderer::MeshSlot& Renderer::slot(uint32_t id) {MeshSlot& s = slots_.emplace_back();s.id = id;gl::GenVertexArrays(1, &s.vao);- gl::GenBuffers(1, &s.posBuffer);- gl::GenBuffers(1, &s.nrmBuffer);- gl::GenBuffers(1, &s.curvBuffer);+ gl::GenVertexArrays(1, &s.lineVao);+ gl::GenBuffers(1, &s.vertexBuffer);gl::GenBuffers(1, &s.indexBuffer);+ gl::GenBuffers(1, &s.lineBuffer);gl::GenBuffers(1, &s.colorBuffer);gl::GenBuffers(1, &s.maskBuffer);gl::GenBuffers(1, &s.groupData);gl::GenTextures(1, &s.groupTexture);+ gl::GenBuffers(1, &s.primData);+ gl::GenTextures(1, &s.primTexture);gl::GenBuffers(1, &s.uvData);gl::GenTextures(1, &s.uvTexture);- gl::BindVertexArray(s.vao);- gl::BindBuffer(GL_ARRAY_BUFFER, s.posBuffer);- gl::EnableVertexAttribArray(0);- gl::VertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);- gl::BindBuffer(GL_ARRAY_BUFFER, s.nrmBuffer);- gl::EnableVertexAttribArray(1);- gl::VertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);- gl::BindBuffer(GL_ARRAY_BUFFER, s.curvBuffer);- gl::EnableVertexAttribArray(2);- gl::VertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(float), nullptr);- // Colour (3) and mask (4) arrays are only enabled while the mesh has them.- gl::BindBuffer(GL_ARRAY_BUFFER, s.colorBuffer);- gl::VertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), nullptr);- gl::BindBuffer(GL_ARRAY_BUFFER, s.maskBuffer);- gl::VertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, sizeof(float), nullptr);- gl::BindBuffer(GL_ELEMENT_ARRAY_BUFFER, s.indexBuffer);+ for (GLuint vao : {s.vao, s.lineVao}) {+ gl::BindVertexArray(vao);+ gl::BindBuffer(GL_ARRAY_BUFFER, s.vertexBuffer);+ gl::EnableVertexAttribArray(0);+ gl::VertexAttribIPointer(0, 2, GL_UNSIGNED_INT, sizeof(PackedVertex), nullptr);+ if (vao == s.lineVao) {+ gl::BindBuffer(GL_ELEMENT_ARRAY_BUFFER, s.lineBuffer);+ continue;+ }+ gl::EnableVertexAttribArray(1);+ gl::VertexAttribIPointer(1, 1, GL_UNSIGNED_INT, sizeof(PackedVertex), reinterpret_cast<const void*>(2 * sizeof(uint32_t)));+ // Colour (2) and mask (3) arrays are only enabled while the mesh has them.+ gl::BindBuffer(GL_ARRAY_BUFFER, s.colorBuffer);+ gl::VertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, 4, nullptr);+ gl::BindBuffer(GL_ARRAY_BUFFER, s.maskBuffer);+ gl::VertexAttribPointer(3, 1, GL_UNSIGNED_BYTE, GL_TRUE, 1, nullptr);+ gl::BindBuffer(GL_ELEMENT_ARRAY_BUFFER, s.indexBuffer);+ }gl::BindVertexArray(0);gl::BindBuffer(GL_ARRAY_BUFFER, 0);return s;}void Renderer::freeSlot(MeshSlot& s) {- gl::DeleteBuffers(1, &s.posBuffer);- gl::DeleteBuffers(1, &s.nrmBuffer);- gl::DeleteBuffers(1, &s.curvBuffer);- gl::DeleteBuffers(1, &s.indexBuffer);- gl::DeleteBuffers(1, &s.colorBuffer);- gl::DeleteBuffers(1, &s.maskBuffer);- gl::DeleteBuffers(1, &s.groupData);+ for (GLuint* b : {&s.vertexBuffer, &s.indexBuffer, &s.lineBuffer, &s.colorBuffer, &s.maskBuffer, &s.groupData, &s.primData, &s.uvData})+ gl::DeleteBuffers(1, b);gl::DeleteTextures(1, &s.groupTexture);- gl::DeleteBuffers(1, &s.uvData);+ gl::DeleteTextures(1, &s.primTexture);gl::DeleteTextures(1, &s.uvTexture);if (s.paintTexture) gl::DeleteTextures(1, &s.paintTexture);if (s.normalTexture) gl::DeleteTextures(1, &s.normalTexture);if (s.tangentData) gl::DeleteBuffers(1, &s.tangentData);if (s.tangentTexture) gl::DeleteTextures(1, &s.tangentTexture);gl::DeleteVertexArrays(1, &s.vao);+ gl::DeleteVertexArrays(1, &s.lineVao);s = MeshSlot();}+size_t Renderer::gpuBytes() const {+ size_t total = 0;+ for (const MeshSlot& s : slots_) {+ for (size_t b : s.bytes) total += b;+ if (s.paintTexture) total += size_t(s.textureW) * size_t(s.textureH) * 4 * 4 / 3; // with mipmaps+ }+ return total;+}+void Renderer::shutdown() {for (auto& s : slots_) freeSlot(s);slots_.clear();@@ -498,9 +635,10 @@ void Renderer::shutdown() {if (!matcapTextures_.empty()) gl::DeleteTextures(GLsizei(matcapTextures_.size()), matcapTextures_.data());matcapTextures_.clear();gl::DeleteVertexArrays(1, &emptyVao_);- gl::DeleteProgram(meshProgram_);- gl::DeleteProgram(meshTexProgram_);- gl::DeleteProgram(meshNormalProgram_);+ for (MeshProgram& p : programs_) {+ if (p.id) gl::DeleteProgram(p.id);+ p = MeshProgram();+ }gl::DeleteProgram(wireProgram_);gl::DeleteProgram(bgProgram_);gl::DeleteProgram(splatProgram_);@@ -614,195 +752,403 @@ float curvatureAt(const Mesh& mesh, uint32_t v) {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.+// Uploads the GPU index ranges `runs` of one vertex array (`allocate` sizes it to `count` elements first):+// fill(begin, end, dst) writes those elements, in GPU order, to dst. The runs are cut into pieces of a few+// thousand elements filled in parallel (a dab's runs are short, so per run would be one thread), batched+// through `staging` so a full upload never copies the whole array at once.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;+ constexpr uint32_t kPiece = 2048, kBatch = 1u << 18;gl::BindBuffer(GL_ARRAY_BUFFER, buffer);if (allocate) gl::BufferData(GL_ARRAY_BUFFER, GLsizeiptr(count * elementSize), nullptr, GL_DYNAMIC_DRAW);+ struct Piece {+ uint32_t begin, end;+ size_t offset; // in staging, bytes+ };+ std::vector<Piece> pieces;+ auto flush = [&]() {+ if (pieces.empty()) return;+ poolFor(pieces.size(), [&](size_t i0, size_t i1) {+ for (size_t i = i0; i < i1; ++i) fill(pieces[i].begin, pieces[i].end, staging.data() + pieces[i].offset);+ }, 1, 2);+ // One upload per stretch of adjacent pieces.+ for (size_t i = 0; i < pieces.size();) {+ size_t j = i + 1;+ while (j < pieces.size() && pieces[j].begin == pieces[j - 1].end) ++j;+ gl::BufferSubData(GL_ARRAY_BUFFER, GLintptr(size_t(pieces[i].begin) * elementSize),+ GLsizeiptr(size_t(pieces[j - 1].end - pieces[i].begin) * elementSize), staging.data() + pieces[i].offset);+ i = j;+ }+ pieces.clear();+ };+ size_t used = 0;+ staging.resize(size_t(std::min<size_t>(kBatch, count)) * elementSize);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());+ for (uint32_t c = b; c < e; c += kPiece) {+ const uint32_t ce = std::min(e, c + kPiece);+ if (used + (ce - c) > kBatch) {+ flush();+ used = 0;+ }+ if ((used + (ce - c)) * elementSize > staging.size()) staging.resize((used + (ce - c)) * elementSize);+ pieces.push_back({c, ce, used * elementSize});+ used += ce - c;}+ flush();}-// 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) {+// fill() for uploadGathered: fn(mesh vertex) for every slot.+template <class T, class F>+auto gatherWith(std::span<const uint32_t> order, F fn) {+ return [order, fn](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);+ for (uint32_t i = b; i < e; ++i) out[i - b] = fn(order[i]);};}+// The mesh's bounding box (every vertex), in parallel.+AABB meshBounds(const Mesh& mesh) {+ constexpr size_t kBlock = size_t(1) << 16;+ const size_t n = mesh.vertexCount();+ std::vector<AABB> parts((n + kBlock - 1) / kBlock);+ poolFor(n, [&](size_t b, size_t e) {+ AABB box;+ for (size_t i = b; i < e; ++i) box.expand(mesh.positions[i]);+ parts[b / kBlock] = box;+ }, kBlock);+ AABB box;+ for (const AABB& p : parts)+ if (p.valid()) box.expand(p);+ return box;+}++// Index of the edge a-b (either direction) in triangle `tri` (edge k joins corners k and k + 1), or -1.+int edgeIndex(const glm::uvec3& tri, uint32_t a, uint32_t b) {+ for (int k = 0; k < 3; ++k)+ if ((tri[k] == a && tri[(k + 1) % 3] == b) || (tri[k] == b && tri[(k + 1) % 3] == a)) return k;+ return -1;+}++// The edge of triangle t the wireframe leaves out: the diagonal inside its quad, unless the other half is+// hidden (then it is the visible outline). -1: none.+int hiddenDiagonal(const Mesh& mesh, uint32_t t) {+ const uint32_t u = mesh.quadPartnerAt(t);+ return u != Mesh::kNoQuad && !mesh.triangleHidden(u) ? mesh.quadDiagonal(t, u) : -1;+}+} // 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,-// stored as GL_LINES indices after the triangles.+bool Renderer::outOfMemory(uint32_t id) const {+ for (const MeshSlot& s : slots_)+ if (s.id == id) return s.outOfMemory;+ return false;+}++void Renderer::syncScene(const std::vector<SceneMesh>& scene) {+ // Free the slots of SubTools that are gone (deleted, merged, a new scene).+ for (size_t i = 0; i < slots_.size();) {+ const bool live = std::any_of(scene.begin(), scene.end(), [&](const SceneMesh& m) { return m.id == slots_[i].id; });+ if (live) {+ ++i;+ continue;+ }+ freeSlot(slots_[i]);+ slots_.erase(slots_.begin() + std::ptrdiff_t(i));+ }+ drawList_.clear();+ for (const SceneMesh& m : scene) {+ if (!m.visible || !m.mesh) continue; // hidden: keeps its dirty state until shown+ sync(slot(m.id), *m.mesh, m.normalMap, m.wireframe && m.active);+ drawList_.push_back({m.id, m.active});+ }+}++// The packed vertices of dirtyRuns_ (all of them when `full`, which also sizes the buffer). A vertex that left+// the position grid (sculpting outwards) gets the mesh a new grid and every vertex re-sent.+void Renderer::uploadVertices(MeshSlot& s, const Mesh& mesh, bool full) {+ const size_t ns = s.order.size();+ const std::span<const uint32_t> order = s.order.fromGpu();+ std::atomic<bool> outside{false};+ const bool normals = mesh.normals.size() == mesh.vertexCount();+ auto fill = [&](uint32_t b, uint32_t e, void* dst) {+ PackedVertex* out = static_cast<PackedVertex*>(dst);+ bool inside = true;+ for (uint32_t i = b; i < e; ++i) {+ const uint32_t v = order[i];+ inside &= packVertex(s.grid, mesh.positions[v], normals ? mesh.normals[v] : glm::vec3(0.0f, 0.0f, 1.0f),+ curvatureAt(mesh, v), out[i - b]);+ }+ if (!inside) outside.store(true, std::memory_order_relaxed);+ };+ uploadGathered(staging_, s.vertexBuffer, sizeof(PackedVertex), ns, full, dirtyRuns_, fill);+ if (outside.load()) {+ s.grid = PositionGrid::around(meshBounds(mesh));+ dirtyRuns_.assign(1, {0u, uint32_t(ns)});+ uploadGathered(staging_, s.vertexBuffer, sizeof(PackedVertex), ns, false, dirtyRuns_, fill);+ }+ s.bytes[0] = ns * sizeof(PackedVertex);+}++// Culling bounds of the clusters dirtyRuns_ touches (every copy of a moved vertex is in a run, so every cluster+// with a moved triangle is), or of all.+void Renderer::updateBounds(MeshSlot& s, const Mesh& mesh, bool all) {+ const auto clusters = s.order.clusters();+ s.bounds.resize(clusters.size());+ cullScratch_.clear();+ if (all) {+ for (uint32_t c = 0; c < clusters.size(); ++c) cullScratch_.push_back(c);+ } else {+ for (const auto& [b, e] : dirtyRuns_) {+ if (b >= e) continue;+ const uint32_t c0 = s.order.clusterOfSlot(b), c1 = s.order.clusterOfSlot(e - 1);+ for (uint32_t c = c0; c <= c1; ++c)+ if (cullScratch_.empty() || c > cullScratch_.back()) cullScratch_.push_back(c);+ }+ }+ poolFor(cullScratch_.size(), [&](size_t i0, size_t i1) {+ for (size_t i = i0; i < i1; ++i) {+ const uint32_t c = cullScratch_[i];+ s.bounds[c] = clusterBounds(mesh, s.order, c);+ }+ }, 1, 2);+ s.box = AABB();+ for (const ClusterBounds& b : s.bounds)+ if (b.radius >= 0.0f) {+ s.box.expand(b.center - glm::vec3(b.radius));+ s.box.expand(b.center + glm::vec3(b.radius));+ }+}++// Index buffer without hidden triangles (each cluster's drawn triangles first in its part of the buffer), the+// first drawn triangle of each cluster, and the polygroup id and UVs of every drawn triangle in draw order+// (looked up by that plus gl_PrimitiveID). The wireframe's edges are rebuilt on next need.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;- // 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) {- drawn = 0;- for (size_t t = 0; t < nt; ++t) {- if (mesh.hidden[t]) continue;- ++drawn;- if (groups) visibleGroups_.push_back(mesh.polygroups[t]);- }- } else if (groups) {- visibleGroups_ = mesh.polygroups;+ const auto clusters = s.order.clusters();+ const size_t nc = clusters.size();+ s.drawCount.assign(nc, 0);+ s.primBase.assign(nc, 0);+ gl::BindVertexArray(s.vao);+ gl::BufferData(GL_ELEMENT_ARRAY_BUFFER, GLsizeiptr(nt * 3 * sizeof(uint16_t)), nullptr, GL_STATIC_DRAW);+ s.bytes[1] = nt * 3 * sizeof(uint16_t);+ constexpr size_t kChunk = 64; // clusters per upload+ std::vector<uint16_t> staging;+ for (size_t c0 = 0; c0 < nc; c0 += kChunk) {+ const size_t c1 = std::min(nc, c0 + kChunk);+ const size_t t0 = s.order.firstTriangle(uint32_t(c0));+ const size_t t1 = s.order.firstTriangle(uint32_t(c1 - 1)) + s.order.clusterTriangles(uint32_t(c1 - 1));+ staging.resize((t1 - t0) * 3);+ poolFor(c1 - c0, [&](size_t i0, size_t i1) {+ thread_local std::vector<uint16_t> local;+ for (size_t i = i0; i < i1; ++i) {+ const uint32_t c = uint32_t(c0 + i);+ const size_t first = s.order.firstTriangle(c);+ const uint32_t count = s.order.clusterTriangles(c);+ uint16_t* out = staging.data() + (first - t0) * 3;+ if (!hidden) {+ s.order.localTriangles(mesh, c, out);+ s.drawCount[c] = GLsizei(count * 3);+ continue;+ }+ local.resize(count * 3);+ s.order.localTriangles(mesh, c, local.data());+ size_t drawn = 0;+ for (uint32_t t = 0; t < count; ++t) {+ if (mesh.hidden[s.order.triangle(c, t)]) continue;+ std::copy_n(local.data() + t * 3, 3, out + drawn * 3);+ ++drawn;+ }+ s.drawCount[c] = GLsizei(drawn * 3);+ }+ }, 1, 2);+ gl::BufferSubData(GL_ELEMENT_ARRAY_BUFFER, GLintptr(t0 * 3 * sizeof(uint16_t)), GLsizeiptr(staging.size() * sizeof(uint16_t)),+ staging.data());}- wireLines_.clear();- if (mesh.hasQuads()) {- for (uint32_t t = 0; t < nt; ++t) {- if (hidden && mesh.hidden[t]) continue;- // 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 = gpu(mesh.triangles[t]);- for (int e = 0; e < 3; ++e)- if (e != diagonal) wireLines_.insert(wireLines_.end(), {tri[e], tri[(e + 1) % 3]});- }+ gl::BindVertexArray(0);+ size_t drawn = 0;+ for (size_t c = 0; c < nc; ++c) {+ s.primBase[c] = uint32_t(drawn);+ drawn += size_t(s.drawCount[c]) / 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)), 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);+ s.drawnTriangles = drawn;+ s.anyHidden = hidden && drawn < nt;+ // Per GPU block (clusters are block aligned): its cluster's first drawn triangle.+ {+ std::vector<uint32_t> table((s.order.size() + UploadOrder::kBlock - 1) / UploadOrder::kBlock, 0);+ for (size_t c = 0; c < nc; ++c) {+ const size_t g0 = clusters[c].firstVertex / UploadOrder::kBlock;+ const size_t g1 = c + 1 < nc ? clusters[c + 1].firstVertex / UploadOrder::kBlock : table.size();+ std::fill(table.begin() + std::ptrdiff_t(g0), table.begin() + std::ptrdiff_t(g1), s.primBase[c]);}- 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();+ gl::BindBuffer(GL_TEXTURE_BUFFER, s.primData);+ gl::BufferData(GL_TEXTURE_BUFFER, GLsizeiptr(table.size() * sizeof(uint32_t)), table.data(), GL_STATIC_DRAW);+ gl::BindTexture(GL_TEXTURE_BUFFER, s.primTexture);+ gl::TexBuffer(GL_TEXTURE_BUFFER, GL_R32UI, s.primData);+ gl::BindTexture(GL_TEXTURE_BUFFER, 0);+ gl::BindBuffer(GL_TEXTURE_BUFFER, 0);+ s.bytes[6] = table.size() * sizeof(uint32_t);}- 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());+ // The drawn triangles in draw order (the clusters' order), for the per-triangle data.GLint maxTexels = 0;gl::GetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &maxTexels);+ const bool textured = wantUvs && mesh.hasUvs() && drawn * 3 <= size_t(std::max(maxTexels, 0));+ std::vector<uint32_t> drawnTris(groups || textured ? drawn : 0);+ if (!drawnTris.empty()) poolFor(nc, [&](size_t c0, size_t c1) {+ for (size_t c = c0; c < c1; ++c) {+ uint32_t* out = drawnTris.data() + s.primBase[c];+ for (uint32_t i = 0; i < s.order.clusterTriangles(uint32_t(c)); ++i) {+ const uint32_t t = s.order.triangle(uint32_t(c), i);+ if (!hidden || !mesh.hidden[t]) *out++ = t;+ }+ }+ }, 16);+ visibleGroups_.resize(groups ? drawn : 0);+ if (groups)+ poolFor(drawn, [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) visibleGroups_[i] = mesh.polygroups[drawnTris[i]];+ }, 1 << 16);s.hasGroups = groups && !visibleGroups_.empty() && visibleGroups_.size() <= size_t(std::max(maxTexels, 0));+ s.bytes[5] = 0;if (s.hasGroups) {+ // 16 bits per triangle when every id fits (nearly always).+ const bool small = *std::max_element(visibleGroups_.begin(), visibleGroups_.end()) <= 0xFFFFu;gl::BindBuffer(GL_TEXTURE_BUFFER, s.groupData);- gl::BufferData(GL_TEXTURE_BUFFER, GLsizeiptr(visibleGroups_.size() * sizeof(uint32_t)), visibleGroups_.data(),- GL_STATIC_DRAW);+ if (small) {+ std::vector<uint16_t> ids(visibleGroups_.size());+ poolFor(ids.size(), [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) ids[i] = uint16_t(visibleGroups_[i]);+ }, 1 << 16);+ gl::BufferData(GL_TEXTURE_BUFFER, GLsizeiptr(ids.size() * sizeof(uint16_t)), ids.data(), GL_STATIC_DRAW);+ } else {+ gl::BufferData(GL_TEXTURE_BUFFER, GLsizeiptr(visibleGroups_.size() * sizeof(uint32_t)), visibleGroups_.data(), GL_STATIC_DRAW);+ }gl::BindTexture(GL_TEXTURE_BUFFER, s.groupTexture);- gl::TexBuffer(GL_TEXTURE_BUFFER, GL_R32UI, s.groupData);+ gl::TexBuffer(GL_TEXTURE_BUFFER, small ? GL_R16UI : GL_R32UI, s.groupData);gl::BindTexture(GL_TEXTURE_BUFFER, 0);gl::BindBuffer(GL_TEXTURE_BUFFER, 0);+ s.bytes[5] = visibleGroups_.size() * (small ? 2 : 4);}// Texture painting (and a baked normal map preview): the UVs of every drawn triangle's corners, looked up- // by gl_PrimitiveID.- const bool textured = wantUvs && mesh.hasUvs() && drawn * 3 <= size_t(std::max(maxTexels, 0));+ // by the drawn triangle's index.s.tangentsStale = true;+ s.bytes[7] = 0;if (textured) {- visibleUvs_.clear();- if (hidden) {- visibleUvs_.reserve(drawn * 3);- for (size_t t = 0; t < nt; ++t)- if (!mesh.hidden[t]) visibleUvs_.insert(visibleUvs_.end(), {mesh.uvs[t * 3], mesh.uvs[t * 3 + 1], mesh.uvs[t * 3 + 2]});- }- const std::vector<glm::vec2>& uvs = hidden ? visibleUvs_ : mesh.uvs;+ visibleUvs_.resize(drawn * 3);+ poolFor(drawn, [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) std::copy_n(mesh.uvs.data() + size_t(drawnTris[i]) * 3, 3, visibleUvs_.data() + i * 3);+ }, 1 << 16);+ const std::vector<glm::vec2>& uvs = visibleUvs_;gl::BindBuffer(GL_TEXTURE_BUFFER, s.uvData);gl::BufferData(GL_TEXTURE_BUFFER, GLsizeiptr(uvs.size() * sizeof(glm::vec2)), uvs.data(), GL_STATIC_DRAW);gl::BindTexture(GL_TEXTURE_BUFFER, s.uvTexture);gl::TexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, s.uvData);gl::BindTexture(GL_TEXTURE_BUFFER, 0);gl::BindBuffer(GL_TEXTURE_BUFFER, 0);+ s.bytes[7] = uvs.size() * sizeof(glm::vec2);} else if (s.hasUvs) {gl::BindBuffer(GL_TEXTURE_BUFFER, s.uvData);gl::BufferData(GL_TEXTURE_BUFFER, 0, nullptr, GL_STATIC_DRAW);gl::BindBuffer(GL_TEXTURE_BUFFER, 0);}s.hasUvs = textured;+ s.linesValid = false;+ if (textured) s.drawnOrder = std::move(drawnTris); // the normal map's tangents follow it+ else std::vector<uint32_t>().swap(s.drawnOrder);if (visibleUvs_.capacity() > (size_t(1) << 16)) std::vector<glm::vec2>().swap(visibleUvs_);// Scratch only: don't keep a big mesh's copy around.- if (visibleTris_.capacity() > (size_t(1) << 16)) std::vector<glm::uvec3>().swap(visibleTris_);if (visibleGroups_.capacity() > (size_t(1) << 16)) std::vector<uint32_t>().swap(visibleGroups_);- if (wireLines_.capacity() > (size_t(1) << 16)) std::vector<uint32_t>().swap(wireLines_);-}--bool Renderer::outOfMemory(uint32_t id) const {- for (const MeshSlot& s : slots_)- if (s.id == id) return s.outOfMemory;- return false;}-void Renderer::syncScene(const std::vector<SceneMesh>& scene) {- // Free the slots of SubTools that are gone (deleted, merged, a new scene).- for (size_t i = 0; i < slots_.size();) {- const bool live = std::any_of(scene.begin(), scene.end(), [&](const SceneMesh& m) { return m.id == slots_[i].id; });- if (live) {- ++i;- continue;+// The wireframe's edges, each once (from the lowest-numbered drawn triangle using it), without quad diagonals;+// per cluster, in the cluster's local indices like its triangles.+void Renderer::buildLines(MeshSlot& s, const Mesh& mesh) {+ const size_t nt = mesh.triangleCount();+ const size_t nc = s.order.clusters().size();+ std::vector<std::vector<uint16_t>> lines(nc);+ poolFor(nc, [&](size_t c0, size_t c1) {+ thread_local std::vector<uint16_t> local;+ for (size_t c = c0; c < c1; ++c) {+ const uint32_t count = s.order.clusterTriangles(uint32_t(c));+ local.resize(size_t(count) * 3);+ s.order.localTriangles(mesh, uint32_t(c), local.data());+ std::vector<uint16_t>& out = lines[c];+ for (uint32_t i = 0; i < count; ++i) {+ const uint32_t t = s.order.triangle(uint32_t(c), i);+ if (mesh.triangleHidden(t)) continue;+ const glm::uvec3& tri = mesh.triangles[t];+ const int diagonal = hiddenDiagonal(mesh, t);+ for (int e = 0; e < 3; ++e) {+ if (e == diagonal) continue;+ const uint32_t a = tri[e], b = tri[(e + 1) % 3];+ if (a >= mesh.vertexCount() || b >= mesh.vertexCount()) continue;+ bool owner = true;+ for (uint32_t u : mesh.vertexTriangles(a)) {+ if (u >= t || u >= nt || mesh.triangleHidden(u)) continue;+ const int k = edgeIndex(mesh.triangles[u], a, b);+ if (k >= 0 && k != hiddenDiagonal(mesh, u)) {+ owner = false;+ break;+ }+ }+ if (owner) out.insert(out.end(), {local[i * 3 + size_t(e)], local[i * 3 + size_t((e + 1) % 3)]});+ }+ }}- freeSlot(slots_[i]);- slots_.erase(slots_.begin() + std::ptrdiff_t(i));- }- drawList_.clear();- for (const SceneMesh& m : scene) {- if (!m.visible || !m.mesh) continue; // hidden: keeps its dirty state until shown- sync(slot(m.id), *m.mesh, m.normalMap);- drawList_.push_back({m.id, m.active});+ }, 1);+ s.lineFirst.assign(nc, 0);+ s.lineCount.assign(nc, 0);+ size_t total = 0;+ for (size_t c = 0; c < nc; ++c) {+ s.lineFirst[c] = uint32_t(total);+ s.lineCount[c] = uint32_t(lines[c].size());+ total += lines[c].size();}+ gl::BindVertexArray(s.lineVao);+ gl::BufferData(GL_ELEMENT_ARRAY_BUFFER, GLsizeiptr(total * sizeof(uint16_t)), nullptr, GL_STATIC_DRAW);+ for (size_t c = 0; c < nc; ++c)+ if (!lines[c].empty())+ gl::BufferSubData(GL_ELEMENT_ARRAY_BUFFER, GLintptr(size_t(s.lineFirst[c]) * sizeof(uint16_t)),+ GLsizeiptr(lines[c].size() * sizeof(uint16_t)), lines[c].data());+ gl::BindVertexArray(0);+ s.bytes[2] = total * sizeof(uint16_t);+ s.linesValid = true;}-void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap) {+void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, bool wireframe) {const size_t nv = mesh.vertexCount();if (normalMap && (!normalMap->rgb || !normalMap->tangents || normalMap->tangents->size() != mesh.uvs.size() ||!mesh.hasUvs() || normalMap->rgb->size() != size_t(normalMap->width) * size_t(normalMap->height) * 3))normalMap = nullptr; // made for another meshconst bool wantUvs = hasTexture(mesh) || normalMap;const bool full = s.fresh || mesh.topologyDirty || nv != s.vertexCapacity;- // 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.+ // The GPU keeps the vertices in the slot's cluster layout (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)});+ s.grid = PositionGrid::around(meshBounds(mesh));+ dirtyRuns_.assign(1, {0u, uint32_t(s.order.size())});} else {s.order.dirtyRuns(mesh, dirtyRuns_);}+ const size_t ns = s.order.size();const bool ranged = !full && !dirtyRuns_.empty();const std::span<const uint32_t> order = s.order.fromGpu();+ static const bool prof = std::getenv("SF_PROF");+ auto T0 = std::chrono::steady_clock::now();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);- });+ uploadVertices(s, mesh, full);+ auto T1 = std::chrono::steady_clock::now();+ updateBounds(s, mesh, full);+ auto T2 = std::chrono::steady_clock::now();+ if (prof) std::fprintf(stderr, "PROF runs %zu verts %.3f bounds %.3f clusters %zu\n", dirtyRuns_.size(),+ std::chrono::duration<double,std::milli>(T1-T0).count(), std::chrono::duration<double,std::milli>(T2-T1).count(), cullScratch_.size());}if (full) {+ s.closed = closedOriented(mesh);uploadFaces(s, mesh, wantUvs);s.vertexCapacity = nv;// A mesh too big for the GPU's memory isn't drawn (rather than taking the driver down); the next full@@ -814,30 +1160,31 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap) {s.outOfMemory = s.outOfMemory || e == GL_OUT_OF_MEMORY;}if (s.outOfMemory) {- for (GLuint b : {s.posBuffer, s.nrmBuffer, s.curvBuffer}) {- gl::BindBuffer(GL_ARRAY_BUFFER, b);- gl::BufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);- }+ gl::BindBuffer(GL_ARRAY_BUFFER, s.vertexBuffer);+ gl::BufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);gl::BindVertexArray(s.vao);gl::BufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_STATIC_DRAW);gl::BindVertexArray(0);- s.indexCount = 0;- s.lineCount = 0;+ s.drawCount.assign(s.drawCount.size(), 0);+ s.drawnTriangles = 0;+ s.bytes[0] = s.bytes[1] = 0;}}if (!full && (mesh.facesDirty || wantUvs != s.hasUvs)) uploadFaces(s, mesh, wantUvs);+ if (wireframe && !s.linesValid && !s.outOfMemory) buildLines(s, mesh);syncTexture(s, mesh);syncNormalMap(s, mesh, normalMap);// Optional per-vertex attributes: a full upload when they appear (or the// 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) {+ const Runs everything{{0u, uint32_t(ns)}};+ auto syncAttribute = [&](GLuint buffer, GLuint location, bool& uploaded, auto gather, size_t elementSize, bool present,+ size_t& bytes) {gl::BindBuffer(GL_ARRAY_BUFFER, buffer);if (present && (full || !uploaded)) {- uploadGathered(staging_, buffer, elementSize, nv, true, everything, gather);+ uploadGathered(staging_, buffer, elementSize, ns, true, everything, gather);} else if (present && ranged) {- uploadGathered(staging_, buffer, elementSize, nv, false, dirtyRuns_, gather);+ uploadGathered(staging_, buffer, elementSize, ns, false, dirtyRuns_, gather);} else if (!present && uploaded) {gl::BufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);}@@ -848,10 +1195,12 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap) {gl::BindVertexArray(0);}uploaded = present;+ bytes = present ? ns * elementSize : 0;};- 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);+ syncAttribute(s.colorBuffer, 2, s.hasColors, gatherWith<uint32_t>(order, [&](uint32_t v) { return packColor(mesh.colors[v]); }),+ sizeof(uint32_t), nv > 0 && mesh.colors.size() == nv, s.bytes[3]);+ syncAttribute(s.maskBuffer, 3, s.hasMask, gatherWith<uint8_t>(order, [&](uint32_t v) { return packMask(mesh.mask[v]); }),+ sizeof(uint8_t), nv > 0 && mesh.mask.size() == nv, s.bytes[4]);// 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);@@ -927,15 +1276,10 @@ void Renderer::syncNormalMap(MeshSlot& s, const Mesh& mesh, const NormalMapView*if (!s.tangentsStale && s.hasTangents) return;if (!s.tangentData) gl::GenBuffers(1, &s.tangentData);if (!s.tangentTexture) gl::GenTextures(1, &s.tangentTexture);+ // In draw order (the clusters' order, without hidden triangles), like the UVs.const std::vector<glm::vec4>& all = *nm->tangents;- std::vector<glm::vec4> visible;- const bool hidden = mesh.hidden.size() == mesh.triangleCount() && mesh.triangleCount() > 0;- if (hidden) {- visible.reserve(all.size());- for (size_t t = 0; t < mesh.triangleCount(); ++t)- if (!mesh.hidden[t]) visible.insert(visible.end(), {all[t * 3], all[t * 3 + 1], all[t * 3 + 2]});- }- const std::vector<glm::vec4>& tans = hidden ? visible : all;+ std::vector<glm::vec4> tans(s.drawnOrder.size() * 3);+ for (size_t i = 0; i < s.drawnOrder.size(); ++i) std::copy_n(all.data() + size_t(s.drawnOrder[i]) * 3, 3, tans.data() + i * 3);gl::BindBuffer(GL_TEXTURE_BUFFER, s.tangentData);gl::BufferData(GL_TEXTURE_BUFFER, GLsizeiptr(tans.size() * sizeof(glm::vec4)), tans.data(), GL_STATIC_DRAW);gl::BindTexture(GL_TEXTURE_BUFFER, s.tangentTexture);@@ -944,6 +1288,7 @@ void Renderer::syncNormalMap(MeshSlot& s, const Mesh& mesh, const NormalMapView*gl::BindBuffer(GL_TEXTURE_BUFFER, 0);s.hasTangents = true;s.tangentsStale = false;+ s.bytes[7] = s.drawnOrder.size() * 3 * (sizeof(glm::vec2) + sizeof(glm::vec4));}GLuint Renderer::paintTexture(uint32_t id) const {@@ -1132,7 +1477,13 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, congl::BindVertexArray(emptyVao_);if (!thumbnailPass_) gl::DrawArrays(GL_TRIANGLES, 0, 3);- glm::mat4 view = camera.view(), proj = camera.projection();+ const glm::mat4 view = camera.view(), proj = camera.projection();+ frameView_ = view;+ frameProj_ = proj;+ frameViewport_ = inset ? glm::vec4(float(viewport.x), float(viewport.y), float(viewport.w), float(viewport.h))+ : glm::vec4(0.0f, 0.0f, float(targetWidth), float(targetHeight));+ frameCull_ = CullView::from(view, proj);+ lastDrawn_ = 0;gl::Enable(GL_DEPTH_TEST);gl::DepthFunc(GL_LEQUAL);if (drawList_.empty() || !settings.showMesh) {@@ -1141,70 +1492,58 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, conreturn;}- auto setMatrices = [&](GLuint prog) {- gl::UseProgram(prog);- gl::UniformMatrix4fv(gl::GetUniformLocation(prog, "uView"), 1, GL_FALSE, glm::value_ptr(view));- gl::UniformMatrix4fv(gl::GetUniformLocation(prog, "uProj"), 1, GL_FALSE, glm::value_ptr(proj));- };auto findSlot = [this](uint32_t id) -> const MeshSlot* {for (const auto& s : slots_)if (s.id == id) return &s;return nullptr;};- const GLuint matcap = matcapTexture(settings.matcap);- const MatcapCavity cavity = matcapCavity(settings.matcap);- for (GLuint prog : {meshNormalProgram_, meshTexProgram_, meshProgram_}) { // the plain program stays bound- setMatrices(prog);- gl::Uniform3fv(gl::GetUniformLocation(prog, "uBaseColor"), 1, glm::value_ptr(settings.baseColor));- gl::Uniform1i(gl::GetUniformLocation(prog, "uGroupIds"), 1);- gl::Uniform1i(gl::GetUniformLocation(prog, "uMatcap"), matcap ? 1 : 0);- gl::Uniform1i(gl::GetUniformLocation(prog, "uMatcapTex"), 2);- gl::Uniform2f(gl::GetUniformLocation(prog, "uMatcapCavity"), cavity.crease, cavity.ridge);- gl::Uniform1i(gl::GetUniformLocation(prog, "uUvs"), 3);- gl::Uniform1i(gl::GetUniformLocation(prog, "uPaintTex"), 4);- gl::Uniform1i(gl::GetUniformLocation(prog, "uNormalTex"), 5);- gl::Uniform1i(gl::GetUniformLocation(prog, "uTangents"), 6);- }gl::ActiveTexture(kTexture2);- gl::BindTexture(GL_TEXTURE_2D, matcap);+ gl::BindTexture(GL_TEXTURE_2D, matcapTexture(settings.matcap));gl::ActiveTexture(GL_TEXTURE0);gl::Enable(GL_POLYGON_OFFSET_FILL);gl::PolygonOffset(1.0f, 1.0f);for (const DrawItem& item : drawList_)- if (const MeshSlot* s = findSlot(item.id); s && s->indexCount > 0) drawSlot(*s, item.active, settings);+ if (const MeshSlot* s = findSlot(item.id); s && s->drawnTriangles > 0) {+ cull(*s, frameCull_);+ drawSlot(*s, item.active, settings);+ }gl::Disable(GL_POLYGON_OFFSET_FILL);- gl::ActiveTexture(kTexture5);- gl::BindTexture(GL_TEXTURE_2D, 0);- gl::ActiveTexture(kTexture6);- gl::BindTexture(GL_TEXTURE_BUFFER, 0);- gl::ActiveTexture(GL_TEXTURE3);- gl::BindTexture(GL_TEXTURE_BUFFER, 0);- gl::ActiveTexture(GL_TEXTURE4);- gl::BindTexture(GL_TEXTURE_2D, 0);- gl::ActiveTexture(GL_TEXTURE1);- gl::BindTexture(GL_TEXTURE_BUFFER, 0);- gl::ActiveTexture(kTexture2);- gl::BindTexture(GL_TEXTURE_2D, 0);+ for (GLenum unit : {kTexture7, kTexture6, GL_TEXTURE3, GL_TEXTURE1}) {+ gl::ActiveTexture(unit);+ gl::BindTexture(GL_TEXTURE_BUFFER, 0);+ }+ for (GLenum unit : {kTexture5, GL_TEXTURE4, kTexture2}) {+ gl::ActiveTexture(unit);+ gl::BindTexture(GL_TEXTURE_2D, 0);+ }gl::ActiveTexture(GL_TEXTURE0);if (settings.wireframe) { // the active SubTool only- setMatrices(wireProgram_);+ gl::UseProgram(wireProgram_);+ gl::UniformMatrix4fv(gl::GetUniformLocation(wireProgram_, "uView"), 1, GL_FALSE, glm::value_ptr(view));+ gl::UniformMatrix4fv(gl::GetUniformLocation(wireProgram_, "uProj"), 1, GL_FALSE, glm::value_ptr(proj));gl::Enable(GL_BLEND);gl::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);gl::DepthMask(GL_FALSE);- gl::PolygonMode(GL_FRONT_AND_BACK, GL_LINE);for (const DrawItem& item : drawList_) {const MeshSlot* s = item.active ? findSlot(item.id) : nullptr;- if (!s || s->indexCount == 0) continue;- gl::BindVertexArray(s->vao);- if (s->lineCount > 0) // quads: their edges without the diagonals- gl::DrawElements(GL_LINES, s->lineCount, GL_UNSIGNED_INT,- reinterpret_cast<const void*>(uintptr_t(s->indexCount) * sizeof(uint32_t)));- else- gl::DrawElements(GL_TRIANGLES, s->indexCount, GL_UNSIGNED_INT, nullptr);+ if (!s || s->drawnTriangles == 0) continue;+ gl::Uniform3fv(gl::GetUniformLocation(wireProgram_, "uGridOrigin"), 1, glm::value_ptr(s->grid.origin));+ gl::Uniform1f(gl::GetUniformLocation(wireProgram_, "uGridStep"), s->grid.step);+ cull(*s, frameCull_);+ if (s->linesValid) {+ gl::Uniform1f(gl::GetUniformLocation(wireProgram_, "uAlpha"), 0.5775f); // 1 - (1 - 0.35)^2+ gl::BindVertexArray(s->lineVao);+ drawClusters(*s, GL_LINES, true, -1);+ } else { // edges not built (a mesh not synced for the wireframe): outline the triangles+ gl::Uniform1f(gl::GetUniformLocation(wireProgram_, "uAlpha"), 0.35f);+ gl::PolygonMode(GL_FRONT_AND_BACK, GL_LINE);+ gl::BindVertexArray(s->vao);+ drawClusters(*s, GL_TRIANGLES, false, -1);+ gl::PolygonMode(GL_FRONT_AND_BACK, GL_FILL);+ }}- gl::PolygonMode(GL_FRONT_AND_BACK, GL_FILL);gl::DepthMask(GL_TRUE);gl::Disable(GL_BLEND);}@@ -1213,43 +1552,119 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, congl::UseProgram(0);}-// One mesh with the mesh program bound. Mask and polygroup colours are for the-// SubTool being edited; polypaint shows on all of them.+// The clusters of `s` inside the view, less those facing away when that can't open a hole: the mesh is closed+// and wound consistently (closedOriented) with nothing hidden, the eye is outside its bounds, and no cluster+// reaches into the near plane's slab (a cut-open front would show the back faces behind it).+void Renderer::cull(const MeshSlot& s, const CullView& frame) {+ drawClusters_.clear();+ CullView v = frame;+ const glm::vec3 e = v.eye;+ v.backfaces = s.closed && !s.anyHidden && s.box.valid() &&+ (e.x < s.box.min.x || e.y < s.box.min.y || e.z < s.box.min.z || e.x > s.box.max.x || e.y > s.box.max.y ||+ e.z > s.box.max.z);+ if (v.backfaces)+ for (const ClusterBounds& b : s.bounds) {+ if (b.radius < 0.0f) continue;+ const float z = glm::dot(b.center - e, v.forward);+ if (z - b.radius < v.nearDistance * 1.01f && z + b.radius > 0.0f) {+ v.backfaces = false;+ break;+ }+ }+ const size_t nc = std::min(s.bounds.size(), s.drawCount.size());+ for (uint32_t c = 0; c < nc; ++c) {+ if (s.drawCount[c] == 0) continue;+ const ClusterBounds& b = s.bounds[c];+ if (!insideFrustum(v, b) || facingAway(v, b)) continue;+ drawClusters_.push_back(c);+ }+}++// Draws drawClusters_ of `s` (its VAO bound): triangles, or the wireframe's edges with `lines`. One multi-draw,+// or with per-triangle data on a driver whose gl_VertexID lacks the base vertex, one draw per cluster with its+// first drawn triangle in `primBaseLocation`.+void Renderer::drawClusters(const MeshSlot& s, GLenum mode, bool lines, GLint primBaseLocation) {+ drawCounts_.clear();+ drawOffsets_.clear();+ drawBases_.clear();+ const auto clusters = s.order.clusters();+ size_t indices = 0;+ for (uint32_t c : drawClusters_) {+ const GLsizei count = lines ? GLsizei(s.lineCount[c]) : s.drawCount[c];+ if (count == 0) continue;+ const size_t first = lines ? size_t(s.lineFirst[c]) : size_t(s.order.firstTriangle(c)) * 3;+ drawCounts_.push_back(count);+ drawOffsets_.push_back(reinterpret_cast<const void*>(first * sizeof(uint16_t)));+ drawBases_.push_back(GLint(clusters[c].firstVertex));+ indices += size_t(count);+ }+ if (drawCounts_.empty()) return;+ if (!lines) lastDrawn_ += indices / 3;+ if (primBaseLocation >= 0 && !vertexIdHasBase_) {+ size_t i = 0;+ for (uint32_t c : drawClusters_) {+ if (s.drawCount[c] == 0) continue;+ gl::Uniform1i(primBaseLocation, GLint(s.primBase[c]));+ gl::DrawElementsBaseVertex(mode, drawCounts_[i], GL_UNSIGNED_SHORT, drawOffsets_[i], drawBases_[i]);+ ++i;+ }+ return;+ }+ if (primBaseLocation >= 0) gl::Uniform1i(primBaseLocation, -1);+ gl::MultiDrawElementsBaseVertex(mode, drawCounts_.data(), GL_UNSIGNED_SHORT, drawOffsets_.data(), GLsizei(drawCounts_.size()),+ drawBases_.data());+}++// One mesh. Mask and polygroup colours are for the SubTool being edited; polypaint shows on all of them.void Renderer::drawSlot(const MeshSlot& s, bool active, const RenderSettings& settings) {// A painted texture shows through the textured program (polypaint still multiplies it).const bool textured = s.hasUvs && s.paintTexture && settings.showPolypaint && settings.showTexture;// A baked normal map preview bends the shading normal (core/Bake.h).const bool normalMapped = s.hasUvs && s.normalTexture && s.hasTangents;- const GLuint prog = normalMapped ? meshNormalProgram_ : textured ? meshTexProgram_ : meshProgram_;- gl::UseProgram(prog);+ const int groups = active && settings.showPolygroups ? (s.hasGroups ? 1 : 2) : 0;+ const int features = (s.hasColors || s.hasMask ? kPaint : 0) | (groups == 1 ? kPrim : 0) | (textured ? kTextured : 0) |+ (normalMapped ? kNormalMap : 0);+ const MeshProgram* p = meshProgram(features);+ if (!p) return;+ gl::UseProgram(p->id);+ gl::UniformMatrix4fv(p->view, 1, GL_FALSE, glm::value_ptr(frameView_));+ gl::UniformMatrix4fv(p->proj, 1, GL_FALSE, glm::value_ptr(frameProj_));+ gl::Uniform3fv(p->gridOrigin, 1, glm::value_ptr(s.grid.origin));+ gl::Uniform1f(p->gridStep, s.grid.step);+ gl::Uniform4fv(p->viewport, 1, glm::value_ptr(frameViewport_));+ gl::Uniform2f(p->projScale, frameProj_[0][0], frameProj_[1][1]);+ gl::Uniform3fv(p->baseColor, 1, glm::value_ptr(settings.baseColor));+ gl::Uniform1i(p->matcap, matcapTexture(settings.matcap) ? 1 : 0);+ const MatcapCavity cavity = matcapCavity(settings.matcap);+ gl::Uniform2f(p->matcapCavity, cavity.crease, cavity.ridge);if (textured || normalMapped) {gl::ActiveTexture(GL_TEXTURE3);gl::BindTexture(GL_TEXTURE_BUFFER, s.uvTexture);gl::ActiveTexture(GL_TEXTURE4);gl::BindTexture(GL_TEXTURE_2D, textured ? s.paintTexture : 0);- gl::ActiveTexture(GL_TEXTURE0);}if (normalMapped) {gl::ActiveTexture(kTexture5);gl::BindTexture(GL_TEXTURE_2D, s.normalTexture);gl::ActiveTexture(kTexture6);gl::BindTexture(GL_TEXTURE_BUFFER, s.tangentTexture);- gl::ActiveTexture(GL_TEXTURE0);- gl::Uniform1i(gl::GetUniformLocation(prog, "uHasPaint"), textured ? 1 : 0);- gl::Uniform1i(gl::GetUniformLocation(prog, "uNormalFlipY"), s.normalFlipGreen ? 1 : 0);+ gl::Uniform1i(p->hasPaint, textured ? 1 : 0);+ gl::Uniform1i(p->normalFlipY, s.normalFlipGreen ? 1 : 0);}- gl::Uniform1i(gl::GetUniformLocation(prog, "uPaint"), settings.showPolypaint && s.hasColors ? 1 : 0);- gl::Uniform1i(gl::GetUniformLocation(prog, "uShowMask"), active && settings.showMask && s.hasMask ? 1 : 0);- gl::Uniform1i(gl::GetUniformLocation(prog, "uGroups"), active && settings.showPolygroups ? (s.hasGroups ? 1 : 2) : 0);- gl::Uniform1i(gl::GetUniformLocation(prog, "uDim"), !active && settings.dimInactive ? 1 : 0);- gl::BindVertexArray(s.vao);- // Constant values for the attribute arrays that are disabled.- if (!s.hasColors) gl::VertexAttrib3f(3, 1.0f, 1.0f, 1.0f);- if (!s.hasMask) gl::VertexAttrib1f(4, 0.0f);+ gl::ActiveTexture(kTexture7);+ gl::BindTexture(GL_TEXTURE_BUFFER, s.primTexture);gl::ActiveTexture(GL_TEXTURE1);gl::BindTexture(GL_TEXTURE_BUFFER, s.hasGroups ? s.groupTexture : 0);gl::ActiveTexture(GL_TEXTURE0);- gl::DrawElements(GL_TRIANGLES, s.indexCount, GL_UNSIGNED_INT, nullptr);+ gl::Uniform1i(p->paint, settings.showPolypaint && s.hasColors ? 1 : 0);+ gl::Uniform1i(p->showMask, active && settings.showMask && s.hasMask ? 1 : 0);+ gl::Uniform1i(p->groups, groups);+ gl::Uniform1i(p->dim, !active && settings.dimInactive ? 1 : 0);+ gl::BindVertexArray(s.vao);+ // Constant values for the attribute arrays that are disabled.+ if (!s.hasColors) gl::VertexAttrib4f(2, 1.0f, 1.0f, 1.0f, 1.0f);+ if (!s.hasMask) gl::VertexAttrib1f(3, 0.0f);+ drawClusters(s, GL_TRIANGLES, false, (features & (kPrim | kTextured | kNormalMap)) ? p->primBase : -1);}// ---------------------------------------------------------------------------@@ -1287,3 +1702,4 @@ bool Renderer::renderMeshThumbnail(Mesh& mesh, const Camera& camera, int size, c}} // namespace sf+diff --git a/src/render/Renderer.h b/src/render/Renderer.hindex f0fe8782..7dffede4 100644--- a/src/render/Renderer.h+++ b/src/render/Renderer.h@@ -6,6 +6,7 @@#include "core/Splats.h"#include "core/UploadOrder.h"#include "render/Camera.h"+#include "render/Clusters.h"#include "render/GL.h"#include <cstdint>@@ -45,6 +46,8 @@ struct SceneMesh {bool active = false; // gets mask darkening, polygroup colours and wireframe; never dimmedbool visible = true; // hidden meshes keep their GPU slot but aren't drawn (or uploaded)const NormalMapView* normalMap = nullptr; // shade with this normal map (needs the mesh's UVs)+ bool wireframe = false; // keep the wireframe's edges on the GPU (built on first need; draw() falls back to+ // outlining the triangles, quad diagonals included, until they are)};// Part of the target the 3D view is drawn into (framebuffer pixels, origin bottom-left); the rest is@@ -59,7 +62,8 @@ struct RenderViewport {glm::vec3 polygroupColor(uint32_t id);// Draws every visible SubTool, each from its own GPU buffers (a slot keyed by-// SubTool id). Mesh drawing honours the optional Mesh attributes: colours+// SubTool id), in clusters (core/UploadOrder.h) culled on the CPU every frame+// (render/Clusters.h) and drawn with one multi-draw. Mesh drawing honours the optional Mesh attributes: colours// (polypaint), mask (darkening), polygroups (group colours) and hidden// triangles (not drawn). Mark changed vertices dirty for colour/mask edits and// set Mesh::facesDirty after changing `hidden` or `polygroups`. Only what a@@ -94,6 +98,11 @@ public:// GL texture of the painted texture of SubTool `id` as last synced (0 if none), for UI previews (rows bottom-up).GLuint paintTexture(uint32_t id) const;+ // Bytes of GPU buffers and textures the mesh slots hold.+ size_t gpuBytes() const;+ // Triangles the last draw() sent to the GPU (after culling), for the benchmark.+ size_t lastDrawnTriangles() const { return lastDrawn_; }+// Texture of built-in matcap `index` (sRGB RGBA8, rows bottom-up) for UI previews; 0 if none.GLuint matcapTexture(int index) const;@@ -106,7 +115,7 @@ public:private:void drawSplats(const glm::mat4& view, const glm::mat4& proj, int fbWidth, int fbHeight, float opacity);- GLuint meshProgram_ = 0, meshTexProgram_ = 0, meshNormalProgram_ = 0, wireProgram_ = 0, bgProgram_ = 0, splatProgram_ = 0;+ GLuint wireProgram_ = 0, bgProgram_ = 0, splatProgram_ = 0;// Splats: per-splat data in a texture buffer, drawn as instanced quads in back-to-front order.GLuint splatVao_ = 0, splatData_ = 0, splatTexture_ = 0, splatOrder_ = 0;GLsizei splatCount_ = 0;@@ -122,13 +131,16 @@ private:int captureW_ = 0, captureH_ = 0;GLuint emptyVao_ = 0;bool thumbnailPass_ = false; // draw(): transparent background, no gradient (renderMeshThumbnail)- // The GPU copy of one mesh (one SubTool).+ // The GPU copy of one mesh (one SubTool), in the cluster layout of `order`.struct MeshSlot {uint32_t id = 0;- GLuint vao = 0;- GLuint posBuffer = 0, nrmBuffer = 0, curvBuffer = 0, indexBuffer = 0;- GLuint colorBuffer = 0, maskBuffer = 0;- GLuint groupData = 0, groupTexture = 0; // polygroup id per drawn triangle (texture buffer)+ GLuint vao = 0, lineVao = 0;+ GLuint vertexBuffer = 0; // PackedVertex per GPU slot (render/Clusters.h)+ GLuint indexBuffer = 0; // 16-bit, relative to each cluster's first vertex; kClusterTriangles * 3 per cluster+ GLuint lineBuffer = 0; // wireframe edges, 16-bit like the triangles, each edge once+ GLuint colorBuffer = 0, maskBuffer = 0; // RGBA8 (gamma encoded), R8+ GLuint groupData = 0, groupTexture = 0; // polygroup id per drawn triangle (texture buffer, R16UI or R32UI)+ GLuint primData = 0, primTexture = 0; // per GPU block: the first drawn triangle of its cluster (R32UI)GLuint uvData = 0, uvTexture = 0; // per drawn triangle corner UVs (texture buffer), meshes with a textureGLuint paintTexture = 0; // the painted texture (sRGB, mipmapped)uint64_t textureSerial = 0, textureRevision = 0;@@ -140,30 +152,67 @@ private:bool hasColors = false, hasMask = false, hasGroups = false, hasUvs = false;bool fresh = true; // nothing uploaded yetbool outOfMemory = false; // the last full upload ran out of GPU memory: not drawn- 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)+ UploadOrder order; // the cluster layout+ PositionGrid grid; // the positions' quantisation+ std::vector<ClusterBounds> bounds; // per cluster, for culling+ AABB box; // all clusters+ bool closed = false; // closedOriented: back-facing clusters may be culled (without hidden triangles)+ bool anyHidden = false;+ std::vector<GLsizei> drawCount; // per cluster: indices drawn (hidden triangles left out)+ std::vector<uint32_t> primBase; // per cluster: its first drawn triangle+ std::vector<uint32_t> lineFirst, lineCount; // per cluster: its edges in lineBuffer (in indices)+ bool linesValid = false;+ std::vector<uint32_t> drawnOrder; // the mesh triangle of each drawn triangle (meshes with UVs only)+ size_t drawnTriangles = 0;size_t vertexCapacity = 0;+ size_t bytes[8] = {}; // GPU sizes: vertices, indices, lines, colours, mask, groups, prims, uvs + tangents};MeshSlot& slot(uint32_t id); // created on first usestatic void freeSlot(MeshSlot& s);- void sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap = nullptr);+ void sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap = nullptr, bool wireframe = false);+ void uploadVertices(MeshSlot& s, const Mesh& mesh, bool full);+ void updateBounds(MeshSlot& s, const Mesh& mesh, bool all);+ void buildLines(MeshSlot& s, const Mesh& mesh);void syncNormalMap(MeshSlot& s, const Mesh& mesh, const NormalMapView* normalMap);void uploadFaces(MeshSlot& s, const Mesh& mesh, bool wantUvs);std::vector<std::pair<uint32_t, uint32_t>> dirtyRuns_; // scratch for sync: UploadOrder::dirtyRunsstd::vector<uint8_t> staging_; // scratch for sync: gathered vertex datavoid syncTexture(MeshSlot& s, Mesh& mesh);+ // The clusters of `s` to draw this frame (into drawCounts_ / drawOffsets_ / drawBases_ / drawClusters_).+ void cull(const MeshSlot& s, const CullView& view);void drawSlot(const MeshSlot& s, bool active, const RenderSettings& settings);+ void drawClusters(const MeshSlot& s, GLenum mode, bool lines, GLint primBaseLocation);+ std::vector<GLsizei> drawCounts_;+ std::vector<const void*> drawOffsets_;+ std::vector<GLint> drawBases_;+ std::vector<uint32_t> drawClusters_;+ std::vector<uint32_t> cullScratch_;+ size_t lastDrawn_ = 0;+ // The frame draw() is drawing: matrices, the view's rectangle (x, y, w, h) and its culling.+ glm::mat4 frameView_{1.0f}, frameProj_{1.0f};+ glm::vec4 frameViewport_{0.0f};+ CullView frameCull_;+ // Mesh shader variants by feature bits (kPaint, kPrim, kTextured, kNormalMap), compiled on first use.+ struct MeshProgram {+ GLuint id = 0;+ GLint view = -1, proj = -1, gridOrigin = -1, gridStep = -1, viewport = -1, projScale = -1, baseColor = -1, paint = -1,+ showMask = -1, groups = -1, matcap = -1, matcapCavity = -1, dim = -1, primBase = -1, hasPaint = -1,+ normalFlipY = -1;+ };+ static constexpr int kPaint = 1, kPrim = 2, kTextured = 4, kNormalMap = 8;+ MeshProgram programs_[16];+ std::string programError_;+ const MeshProgram* meshProgram(int features);+ bool vertexIdHasBase_ = true; // gl_VertexID includes the base vertex (checked at init; else per-cluster draws)+ bool checkVertexIdBase();std::vector<MeshSlot> slots_;struct DrawItem {uint32_t id;bool active;};std::vector<DrawItem> drawList_; // visible meshes, in scene order- std::vector<glm::uvec3> visibleTris_; // scratch for uploadFaces- std::vector<uint32_t> visibleGroups_;- std::vector<glm::vec2> visibleUvs_; // scratch for uploadFaces- std::vector<uint32_t> wireLines_; // scratch for uploadFaces: wireframe edges of meshes with quads+ std::vector<uint32_t> visibleGroups_; // scratch for uploadFaces+ std::vector<glm::vec2> visibleUvs_; // scratch for uploadFacesstd::vector<GLuint> matcapTextures_; // one per built-in matcap (core/Matcap.h)};diff --git a/tests/test_performance.cpp b/tests/test_performance.cppindex a961faf4..816e27e1 100644--- a/tests/test_performance.cpp+++ b/tests/test_performance.cpp@@ -152,15 +152,11 @@ TEST(upload_runs_cover_changed_vertices) {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);+ // Every vertex has a slot (the sphere uses them all); toGpu is its first.+ bool mapped = order.size() >= m.vertexCount();+ for (uint32_t v = 0; mapped && v < m.vertexCount(); ++v)+ mapped = order.toGpu(v) != UploadOrder::kNone && order.fromGpu()[order.toGpu(v)] == v;+ CHECK(mapped);m.clearDirty();std::vector<std::pair<uint32_t, uint32_t>> runs;@@ -177,21 +173,25 @@ TEST(upload_runs_cover_changed_vertices) {p.radius = 0.2f;s.dab(m, bvh, BrushType::Standard, p, Symmetry{});order.dirtyRuns(m, runs);- std::vector<uint8_t> inRun(m.vertexCount(), 0);+ std::vector<uint8_t> slotInRun(order.size(), 0);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());+ CHECK(runs[i].first < runs[i].second && runs[i].second <= order.size());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;+ for (uint32_t g = runs[i].first; g < runs[i].second; ++g) slotInRun[g] = 1;}+ // Every slot of a changed vertex, the copies on cluster seams included.bool allCovered = true;size_t changed = 0, dirty = 0;+ std::vector<uint32_t> slots;for (uint32_t v = 0; v < m.vertexCount(); ++v) {dirty += m.vertexDirty(v);if (m.normals[v] != before[v]) {++changed;- allCovered = allCovered && inRun[v] && m.vertexDirty(v);+ order.slotsOf(v, slots);+ allCovered = allCovered && m.vertexDirty(v) && !slots.empty();+ for (uint32_t g : slots) allCovered = allCovered && slotInRun[g];}}CHECK(changed > 1000);@@ -209,7 +209,7 @@ TEST(upload_runs_cover_changed_vertices) {CHECK(!m.vertexDirty(69) && m.vertexDirty(70) && m.vertexDirty(199) && !m.vertexDirty(200));m.markAllDirty();order.dirtyRuns(m, runs);- CHECK(runs.size() == 1 && runs[0].first == 0 && runs[0].second == m.vertexCount());+ CHECK(runs.size() == 1 && runs[0].first == 0 && runs[0].second == order.size());}TEST(parallel_bvh_build_answers_like_serial) {diff --git a/tests/test_render_clusters.cpp b/tests/test_render_clusters.cppnew file mode 100644index 00000000..eec5769e--- /dev/null+++ b/tests/test_render_clusters.cpp@@ -0,0 +1,268 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// The renderer's CPU side without a GPU: the cluster layout (core/UploadOrder.h), the packed vertex format, and+// cluster culling (render/Clusters.h) - culled clusters really face away, closed meshes are told apart from+// open or inconsistently wound ones, and the layout is the same with multithreading off.+#include "TestHarness.h"++#include "core/Benchmark.h"+#include "core/Primitives.h"+#include "core/ThreadPool.h"+#include "core/UploadOrder.h"+#include "render/Clusters.h"++#include <glm/gtc/matrix_transform.hpp>++#include <cmath>+#include <random>++using namespace sf;++namespace {++// Every corner of every cluster's triangles maps to a slot in the cluster's range holding that vertex.+bool layoutHolds(const Mesh& m, const UploadOrder& order) {+ std::vector<uint16_t> local;+ const auto clusters = order.clusters();+ if (clusters.size() != (m.triangleCount() + UploadOrder::kClusterTriangles - 1) / UploadOrder::kClusterTriangles) return false;+ // Every mesh triangle exactly once, clusters back to back in the clusters' triangle order.+ std::vector<uint8_t> seen(m.triangleCount(), 0);+ size_t next = 0;+ for (uint32_t c = 0; c < clusters.size(); ++c) {+ if (clusters[c].firstTriangle != next) return false;+ next += clusters[c].triangleCount;+ for (uint32_t i = 0; i < clusters[c].triangleCount; ++i) {+ const uint32_t t = order.triangle(c, i);+ if (t >= m.triangleCount() || seen[t]) return false;+ seen[t] = 1;+ }+ }+ if (next != m.triangleCount()) return false;+ for (uint32_t c = 0; c < clusters.size(); ++c) {+ const UploadOrder::Cluster& cl = clusters[c];+ if (cl.firstVertex % UploadOrder::kBlock != 0 || cl.vertexCount > 65535) return false;+ if (c + 1 < clusters.size() && cl.firstVertex + cl.vertexCount > clusters[c + 1].firstVertex) return false;+ const uint32_t n = order.clusterTriangles(c);+ local.resize(size_t(n) * 3);+ order.localTriangles(m, c, local.data());+ for (uint32_t i = 0; i < n; ++i)+ for (int k = 0; k < 3; ++k) {+ const uint32_t l = local[size_t(i) * 3 + size_t(k)];+ if (l >= cl.vertexCount) return false;+ if (order.fromGpu()[cl.firstVertex + l] != m.triangles[order.triangle(c, i)][k]) return false;+ if (order.clusterOfSlot(cl.firstVertex + l) != c) return false;+ }+ }+ return true;+}++} // namespace++TEST(cluster_layout_maps_every_corner) {+ Mesh m = makeBenchSphere(100000); // several clusters, divided-mesh numbering+ UploadOrder order;+ order.build(m);+ CHECK(order.clusters().size() > 10);+ CHECK(layoutHolds(m, order));+ // slotsOf lists exactly the slots holding v, the first being toGpu(v).+ std::vector<uint32_t> count(m.vertexCount(), 0);+ for (uint32_t c = 0; c < order.clusters().size(); ++c) {+ const auto& cl = order.clusters()[c];+ for (uint32_t i = cl.firstVertex; i < cl.firstVertex + cl.vertexCount; ++i) ++count[order.fromGpu()[i]];+ }+ bool slotsOk = true;+ size_t copies = 0;+ std::vector<uint32_t> slots;+ for (uint32_t v = 0; v < m.vertexCount(); ++v) {+ order.slotsOf(v, slots);+ slotsOk = slotsOk && slots.size() == count[v] && !slots.empty() && slots[0] == order.toGpu(v);+ for (size_t i = 0; i < slots.size(); ++i) slotsOk = slotsOk && order.fromGpu()[slots[i]] == v && (i == 0 || slots[i - 1] < slots[i]);+ copies += slots.size() - 1;+ }+ CHECK(slotsOk);+ // Clusters of a divided mesh are compact patches: few vertices are copied onto seams.+ CHECK(copies * 5 < m.vertexCount());+ CHECK(order.size() < m.vertexCount() * 13 / 10);+}++TEST(cluster_layout_odd_meshes) {+ // A single cluster, an empty mesh, unused vertices, and a mesh with an out-of-range index.+ Mesh small = makeCube(2);+ UploadOrder order;+ order.build(small);+ CHECK(order.clusters().size() == 1);+ CHECK(layoutHolds(small, order));+ Mesh empty;+ order.build(empty);+ CHECK(order.size() == 0 && order.clusters().empty());+ std::vector<std::pair<uint32_t, uint32_t>> runs;+ order.dirtyRuns(empty, runs);+ CHECK(runs.empty());+ Mesh m = makeCube(4);+ m.positions.push_back(glm::vec3(5.0f)); // used by no triangle+ m.normals.push_back(glm::vec3(0.0f, 0.0f, 1.0f));+ order.build(m);+ CHECK(order.toGpu(uint32_t(m.vertexCount() - 1)) == UploadOrder::kNone);+ m.clearDirty();+ m.markDirty(uint32_t(m.vertexCount() - 1));+ order.dirtyRuns(m, runs);+ CHECK(runs.empty());+ m.triangles[3].y = 1000000; // invalid: drawn as nothing+ order.build(m);+ std::vector<uint16_t> local(size_t(order.clusterTriangles(0)) * 3);+ order.localTriangles(m, 0, local.data());+ for (uint32_t i = 0; i < order.clusterTriangles(0); ++i)+ if (order.triangle(0, i) == 3) CHECK(local[i * 3] == 0 && local[i * 3 + 1] == 0 && local[i * 3 + 2] == 0);+}++TEST(cluster_layout_same_serially) {+ const Mesh m = makeBenchSphere(60000);+ UploadOrder par, ser;+ par.build(m);+ ThreadPool::shared().setSerial(true);+ ser.build(m);+ ThreadPool::shared().setSerial(false);+ CHECK(par.size() == ser.size());+ CHECK(std::equal(par.fromGpu().begin(), par.fromGpu().end(), ser.fromGpu().begin(), ser.fromGpu().end()));+ bool same = par.clusters().size() == ser.clusters().size();+ for (size_t c = 0; same && c < par.clusters().size(); ++c)+ same = par.clusters()[c].firstVertex == ser.clusters()[c].firstVertex && par.clusters()[c].vertexCount == ser.clusters()[c].vertexCount;+ CHECK(same);+}++TEST(packed_vertex_round_trip) {+ std::mt19937 rng(7);+ std::uniform_real_distribution<float> u(-1.0f, 1.0f);+ AABB box;+ box.expand(glm::vec3(-3.0f, -1.0f, -0.5f));+ box.expand(glm::vec3(2.0f, 4.0f, 0.5f));+ const PositionGrid grid = PositionGrid::around(box);+ float worstPos = 0.0f, worstAngle = 0.0f, worstCurv = 0.0f;+ bool inside = true;+ for (int i = 0; i < 20000; ++i) {+ const glm::vec3 p = box.min + (glm::vec3(u(rng), u(rng), u(rng)) * 0.5f + 0.5f) * box.extent();+ glm::vec3 n(u(rng), u(rng), u(rng));+ if (glm::length(n) < 1e-3f) continue;+ n = glm::normalize(n);+ const float curv = u(rng) * 0.3f;+ PackedVertex v;+ inside = inside && packVertex(grid, p, n, curv, v);+ worstPos = std::max(worstPos, glm::length(unpackPosition(grid, v) - p) / grid.step);+ worstAngle = std::max(worstAngle, std::acos(std::clamp(glm::dot(unpackNormal(v), n), -1.0f, 1.0f)));+ const float expected = std::clamp(curv, -kCurvatureRange, kCurvatureRange);+ worstCurv = std::max(worstCurv, std::abs(unpackCurvature(v) - expected));+ }+ std::printf(" worst position error %.3f steps, normal %.4f deg, curvature %.5f\n", worstPos, glm::degrees(worstAngle), worstCurv);+ CHECK(inside);+ CHECK(worstPos < 1.25f); // half a step per axis, plus float rounding+ CHECK(worstAngle < glm::radians(0.15f)); // 11-bit octahedral normals+ CHECK(worstCurv <= kCurvatureRange / 511.0f); // 10-bit curvature+ // Exact values where it matters: zero curvature, axis normals, and the same point always packs the same.+ PackedVertex a, b;+ packVertex(grid, glm::vec3(0.1f), glm::vec3(0, 0, -1), 0.0f, a);+ packVertex(grid, glm::vec3(0.1f), glm::vec3(0, 0, -1), 0.0f, b);+ CHECK(unpackCurvature(a) == 0.0f);+ CHECK(glm::length(unpackNormal(a) - glm::vec3(0, 0, -1)) < 1e-6f);+ CHECK(a.pos[0] == b.pos[0] && a.pos[1] == b.pos[1] && a.normal == b.normal);+ // Outside the grid: reported (the renderer then fits a new one) and clamped.+ CHECK(!packVertex(grid, glm::vec3(100.0f, 0.0f, 0.0f), glm::vec3(0, 0, 1), 0.0f, a));+ CHECK(!packVertex(grid, glm::vec3(std::nanf(""), 0.0f, 0.0f), glm::vec3(0, 0, 1), 0.0f, a));+ // The margin: a little outside the box still fits.+ CHECK(grid.contains(box.max + glm::vec3(0.5f)));+ // Colours: 8-bit gamma 2.2, white and black exact.+ CHECK(packColor(glm::vec3(1.0f)) == 0xFFFFFFFFu);+ CHECK(packColor(glm::vec3(0.0f)) == 0xFF000000u);+ CHECK(packMask(1.0f) == 255 && packMask(0.0f) == 0 && packMask(2.0f) == 255);+}++TEST(closed_oriented_meshes) {+ CHECK(closedOriented(makeIcosphere(3)));+ CHECK(closedOriented(makeCube(3)));+ CHECK(closedOriented(makeBenchSphere(20000)));+ CHECK(!closedOriented(makePlane(4)));+ Mesh flipped = makeIcosphere(2);+ std::swap(flipped.triangles[5].y, flipped.triangles[5].z);+ flipped.rebuildTopology();+ CHECK(!closedOriented(flipped));+ Mesh holed = makeIcosphere(2);+ holed.triangles.pop_back();+ holed.rebuildTopology();+ CHECK(!closedOriented(holed));+ CHECK(!closedOriented(Mesh()));+}++TEST(cluster_culling_is_conservative) {+ // A sculpted-looking sphere with about a hundred clusters: bumps so their normal cones vary.+ Mesh m = makeBenchSphere(400000);+ for (size_t v = 0; v < m.vertexCount(); ++v) {+ const glm::vec3 p = m.positions[v];+ m.positions[v] = p * (1.0f + 0.01f * std::sin(p.x * 23.0f) * std::cos(p.y * 17.0f));+ }+ m.recomputeAllNormals();+ UploadOrder order;+ order.build(m);+ std::vector<ClusterBounds> bounds(order.clusters().size());+ for (uint32_t c = 0; c < bounds.size(); ++c) bounds[c] = clusterBounds(m, order, c);+ std::mt19937 rng(3);+ std::uniform_real_distribution<float> u(-1.0f, 1.0f);+ size_t culled = 0, total = 0;+ bool sound = true, contained = true;+ for (int view = 0; view < 24; ++view) {+ glm::vec3 eye(u(rng), u(rng), u(rng));+ eye = glm::normalize(eye) * (1.6f + 3.0f * std::abs(u(rng)));+ const glm::mat4 V = glm::lookAt(eye, glm::vec3(0.0f), glm::vec3(0, 1, 0));+ const glm::mat4 P = glm::perspective(glm::radians(35.0f), 1.5f, 0.04f, 100.0f);+ CullView cv = CullView::from(V, P);+ cv.backfaces = true;+ contained = contained && glm::length(cv.eye - eye) < 1e-4f && std::abs(cv.nearDistance - 0.04f) < 1e-4f;+ for (uint32_t c = 0; c < bounds.size(); ++c) {+ ++total;+ if (!facingAway(cv, bounds[c])) continue;+ ++culled;+ // Every triangle of a culled cluster faces away from the eye at all three corners.+ for (uint32_t i = 0; i < order.clusterTriangles(c); ++i) {+ const glm::uvec3 tri = m.triangles[order.triangle(c, i)];+ const glm::vec3 n = glm::cross(m.positions[tri.y] - m.positions[tri.x], m.positions[tri.z] - m.positions[tri.x]);+ for (int k = 0; k < 3; ++k) sound = sound && glm::dot(n, m.positions[tri[k]] - eye) > 0.0f;+ }+ }+ }+ std::printf(" culled %zu of %zu cluster views\n", culled, total);+ CHECK(contained);+ CHECK(sound);+ CHECK(culled * 4 > total); // a good part of the back half goes+ // Bounds hold every vertex of their cluster.+ bool inSphere = true;+ for (uint32_t c = 0; c < bounds.size(); ++c)+ for (uint32_t i = 0; i < order.clusterTriangles(c); ++i)+ for (int k = 0; k < 3; ++k)+ inSphere = inSphere && glm::distance(m.positions[m.triangles[order.triangle(c, i)][k]], bounds[c].center) <= bounds[c].radius;+ CHECK(inSphere);+ // Off by default, and never with the eye inside the sphere's bounds.+ CullView off = CullView::from(glm::lookAt(glm::vec3(0, 0, 3), glm::vec3(0.0f), glm::vec3(0, 1, 0)),+ glm::perspective(glm::radians(35.0f), 1.0f, 0.03f, 100.0f));+ bool none = true;+ for (const ClusterBounds& b : bounds) none = none && !facingAway(off, b);+ CHECK(none);+}++TEST(cluster_frustum_culling) {+ const glm::mat4 V = glm::lookAt(glm::vec3(0, 0, 5), glm::vec3(0.0f), glm::vec3(0, 1, 0));+ const glm::mat4 P = glm::perspective(glm::radians(35.0f), 1.0f, 0.05f, 100.0f);+ const CullView cv = CullView::from(V, P);+ ClusterBounds b;+ b.radius = 0.1f;+ b.center = glm::vec3(0.0f);+ CHECK(insideFrustum(cv, b));+ b.center = glm::vec3(0, 0, 6); // behind the eye+ CHECK(!insideFrustum(cv, b));+ b.center = glm::vec3(5, 0, 0); // far off to the side+ CHECK(!insideFrustum(cv, b));+ b.center = glm::vec3(1.6f, 0, 0); // straddling the edge of the view+ b.radius = 0.5f;+ CHECK(insideFrustum(cv, b));+ b.radius = -1.0f; // empty+ b.center = glm::vec3(0.0f);+ CHECK(!insideFrustum(cv, b));+}