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
Micro levels: TierConfig budgets, dual-scale Move, a packed undo journal, the two-tier names
islandmede2416f64121e
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 43f3f755..26fd4bdf 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -72,6 +72,7 @@ add_library(sculptforge_core STATICsrc/core/MicroSculpt.cppsrc/core/MicroCommands.cppsrc/core/MicroStore.cpp+ src/core/TierConfig.cppsrc/core/MultiresCommands.cppsrc/core/AutoUv.cppsrc/core/TextureTransfer.cppdiff --git a/src/core/Micro.cpp b/src/core/Micro.cppindex af7216d7..0cdb88aa 100644--- a/src/core/Micro.cpp+++ b/src/core/Micro.cpp@@ -10,6 +10,7 @@#include "core/BVH.h"#include "core/Multires.h"#include "core/ThreadPool.h"+#include "core/TierConfig.h"#include <algorithm>#include <atomic>@@ -153,6 +154,28 @@ glm::vec3 MicroTriangle::flat(uint32_t i, uint32_t j) const {return f;}+MicroPatch microPatch(Mesh& m, uint32_t id) {+ MicroMesh& mm = mutableMicro(m);+ MicroPatch p;+ p.id = id;+ p.n = mm.n;+ p.heights = mm.disp.data() + size_t(id) * mm.samples;+ p.mask = mm.hasMask() ? mm.mask.data() + size_t(id) * mm.samples : nullptr;+ return p;+}++bool PatchPageTable::resident(uint32_t patch) const {+ if (!mm_.disp.mapped()) return true;+ return mm_.disp.rangeResident(size_t(patch) * mm_.samples, mm_.samples);+}++size_t PatchPageTable::residentPatches() const {+ if (!mm_.disp.mapped()) return mm_.baseTriangles;+ size_t count = 0;+ for (uint32_t t = 0; t < mm_.baseTriangles; ++t) count += resident(t);+ return count;+}+int microLevel(const Mesh& m) { return m.micro ? m.micro->level : 0; }size_t microTriangleCount(const Mesh& m) { return m.micro ? m.micro->microTriangles() : 0; }size_t microBytes(const Mesh& m) { return m.micro ? m.micro->bytes() : 0; }@@ -272,6 +295,7 @@ float triangleBulge(const MicroTriangle& T) {bool microWantsOutOfCore(size_t points) {if (const char* e = std::getenv("SF_MICRO_OUT_OF_CORE"); e && *e) return std::string(e) == "1";+ if (const size_t limit = tierConfig().outOfCorePoints) return points > limit;size_t memory = 0;#if defined(_SC_PHYS_PAGES)const long pages = sysconf(_SC_PHYS_PAGES), page = sysconf(_SC_PAGESIZE);@@ -808,7 +832,7 @@ void MicroUndo::swapWith(Mesh& m) {MicroMesh& mm = mutableMicro(m);const size_t S = samples;// Values kept in another step size (the range grew since): convert to the current one.- if (step != mm.step && step > 0.0f) {+ if (!journal && step != mm.step && step > 0.0f) {const float r = step / mm.step;for (int16_t& v : disp) v = int16_t(std::clamp(std::lround(float(v) * r), -32767L, 32767L));step = mm.step;@@ -837,6 +861,42 @@ void MicroUndo::swapWith(Mesh& m) {mm.mask.begin() + std::ptrdiff_t(size_t(tris[i]) * S));}, 64);}+ if (journal) { // the stroke's change: subtract it (undo) or add it back (redo)+ std::vector<int16_t> change(tris.size() * S, 0);+ if (!microUnpackTiles(packed.data(), packed.size(), tris.size(), samples, change.data())) return;+ poolFor(tris.size(), [&](size_t b, size_t e) { // (stored as differences to the point before, see MicroUndo)+ const uint32_t n = mm.n;+ for (size_t t = b; t < e; ++t) {+ int16_t* c = change.data() + t * S;+ uint32_t p = 0;+ for (uint32_t j = 0; j <= n; ++j)+ for (uint32_t i = 0; i + j <= n; ++i, ++p) {+ int v;+ if (p == 0) v = firsts[t];+ else if (i > 0) v = int(c[p - 1]) + int(c[p]);+ else v = int(c[MicroMesh::index(0, j - 1, n)]) + int(c[p]);+ c[p] = int16_t(std::clamp(v, -32767, 32767));+ }+ }+ }, 256);+ const float ratio = step / mm.step; // (the range grew since: the change in today's units)+ const int sgn = applied ? -1 : 1;+ poolFor(tris.size(), [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) {+ int16_t* g = mm.disp.data() + size_t(tris[i]) * S;+ int r = 0;+ for (size_t k = 0; k < S; ++k) {+ const float c = ratio == 1.0f ? float(change[i * S + k]) : float(change[i * S + k]) * ratio;+ const int v = std::clamp(int(g[k]) + sgn * int(std::lround(c)), -32767, 32767);+ g[k] = int16_t(v);+ r = std::max(r, std::abs(v));+ }+ mm.triRange[tris[i]] = uint16_t(std::max<int>(r, mm.triRange[tris[i]]));+ }+ }, 64);+ applied = !applied;+ for (uint32_t t : tris) mm.rangeMax = std::max(mm.rangeMax, mm.triRange[t]);+ }if (!disp.empty()) {poolFor(tris.size(), [&](size_t b, size_t e) {for (size_t i = b; i < e; ++i) {@@ -854,7 +914,8 @@ void MicroUndo::swapWith(Mesh& m) {}size_t MicroUndo::bytes() const {- return tris.size() * sizeof(uint32_t) + disp.size() * sizeof(int16_t) + mask.size() + (swapWhole && whole ? whole->bytes() : 0);+ return tris.size() * sizeof(uint32_t) + keys.size() * sizeof(uint64_t) + packed.size() + firsts.size() * sizeof(int16_t) + disp.size() * sizeof(int16_t) ++ mask.size() + (swapWhole && whole ? whole->bytes() : 0);}// ---------------------------------------------------------------------------diff --git a/src/core/Micro.h b/src/core/Micro.hindex 9c526bba..7a9f3feb 100644--- a/src/core/Micro.h+++ b/src/core/Micro.h@@ -78,6 +78,7 @@ struct MicroMesh {std::vector<uint64_t> dirtyBits;bool allDirty = true;bool maskDirty = false; // the mask was added or removed (the renderer's shading changes everywhere)+ bool stroking = false; // a stroke is running (TierConfig draft normals: the renderer shades less meanwhile)bool rescaled = false; // `step` grew and every value was rescaled (same surface: the renderer re-sends the values only)uint64_t revision = 0; // bumped by every edit// Which level this is (new on Divide / load; kept by copies, which hold the same values): the renderer keeps@@ -108,6 +109,35 @@ struct MicroMesh {void ensureTopology(const Mesh& base);};+// --- The two-tier engine's vocabulary ---+// BaseCage: the coarse surface the brushes for big forms (Move, transforms) deform: the SubTool's mesh, its highest+// subdivision level. Its polygons are triangles (a quad of the cage is a triangle pair, one patch each).+using BaseCage = Mesh;+// MicroPatch: one base triangle's regular grid of scalar heights (n + 1)(n + 2) / 2, implicit topology (no index+// buffer anywhere), heights 16-bit in memory and packed to their range (0-16 bits) on disk and in undo steps.+struct MicroPatch {+ uint32_t id = 0; // the base triangle+ uint32_t n = 0; // segments per edge+ int16_t* heights = nullptr;+ uint8_t* mask = nullptr; // null without a mask+ size_t points() const { return size_t(n + 1) * (n + 2) / 2; }+};+MicroPatch microPatch(Mesh& m, uint32_t id);+// PatchPageTable: which patches are resident. In memory every patch is; out of core (MicroStore::mapped) a patch+// is resident when its pages are, cold ones live in the sparse file (untouched = a hole) and come back on touch;+// the operating system's page cache keeps the LRU order and trim() enforces TierConfig::residentPatchCacheMB.+class PatchPageTable {+public:+ explicit PatchPageTable(MicroMesh& mm) : mm_(mm) {}+ bool resident(uint32_t patch) const;+ size_t residentPatches() const;+ size_t residentBytes() const { return mm_.disp.residentBytes(); }+ size_t trim(size_t maxBytes) { return mm_.disp.trim(maxBytes); }++private:+ MicroMesh& mm_;+};+// The base triangle's corners and normals, sorted by vertex index, for evaluating its grid points.struct MicroTriangle {glm::vec3 p[3], nrm[3]; // in vertex-index order@@ -264,11 +294,22 @@ private:// One micro step: the old grid points of the base triangles a stroke touched (in units of `step`), or a whole// micro level swapped in or out (Divide to micro, Delete micro: `whole`, with `swapWhole`).+//+// Strokes keep a journal (`journal`): the touched tiles sorted by a Morton code of their centre (`keys`, so the+// journal of a region is contiguous and can be looked up), each as the stroke's change (new - old, stored as the+// difference to the point before it in its row, or below it for a row's first) packed with the tile codec (core/MicroStore.h): untouched points cost nothing, a smooth change a few bits+// per point, so a typical stroke's step is tens of KB instead of 2 bytes per point. `applied`: the change is in+// the mesh (undo subtracts it, redo adds it back).struct MicroUndo {float step = 0.0f;uint32_t samples = 0;std::vector<uint32_t> tris;- std::vector<int16_t> disp;+ std::vector<int16_t> disp; // whole-tile old values (no journal)+ bool journal = false;+ bool applied = true;+ std::vector<uint64_t> keys; // journal: Morton code of each tile's centre, ascending+ std::vector<uint8_t> packed; // journal: the tiles' changes (each point less its predecessor), packed+ std::vector<int16_t> firsts; // journal: each tile's first changestd::vector<uint8_t> mask;bool maskAdded = false; // the step allocated the mask: undo frees it again (redo brings it back)bool swapWhole = false;diff --git a/src/core/MicroSculpt.cpp b/src/core/MicroSculpt.cppindex 4cb29421..feb4de97 100644--- a/src/core/MicroSculpt.cpp+++ b/src/core/MicroSculpt.cpp@@ -14,8 +14,10 @@#include "core/BVH.h"#include "core/Sculpt.h"#include "core/ThreadPool.h"+#include "core/TierConfig.h"#include <algorithm>+#include <atomic>#include <cmath>namespace sf {@@ -123,6 +125,7 @@ void MicroStroke::begin(Mesh& m) {grabWeight_.clear();grabNormal_.clear();baseEdge_ = mm.baseEdge;+ mm.stroking = true;active_ = true;}@@ -644,6 +647,10 @@ void MicroStroke::restore(Mesh& m) {std::unique_ptr<MicroUndo> MicroStroke::end(Mesh& m) {active_ = false;+ if (m.micro) {+ m.micro->stroking = false;+ if (const size_t cap = tierConfig().residentPatchCacheMB) m.micro->disp.trim(cap << 20);+ }grabMirrors_.clear();grabTris_.clear();grabWeight_.clear();@@ -653,13 +660,81 @@ std::unique_ptr<MicroUndo> MicroStroke::end(Mesh& m) {return nullptr;}auto u = std::make_unique<MicroUndo>();+ const MicroMesh& mm = *m.micro;+ const size_t S = mm.samples, count = tiles_.size();u->step = strokeStep_;- u->samples = m.micro->samples;- u->tris = std::move(tiles_);- u->disp = std::move(oldDisp_);- if (m.micro->hasMask() && oldMask_.size() == u->tris.size() * u->samples) u->mask = std::move(oldMask_);+ u->samples = mm.samples;+ for (uint32_t t : tiles_) tileOf_[t] = 0;+ // The journal: tiles in Morton order of their centres, each the stroke's change, packed.+ AABB box;+ for (uint32_t t : tiles_) {+ const glm::uvec3 tr = m.triangles[t];+ box.expand(m.positions[tr.x]);+ }+ const glm::vec3 lo = box.min, ext = glm::max(box.extent(), glm::vec3(1e-20f));+ auto spread = [](uint32_t x) {+ uint64_t v = x & 0x1FFFFF;+ v = (v | v << 32) & 0x1F00000000FFFFull;+ v = (v | v << 16) & 0x1F0000FF0000FFull;+ v = (v | v << 8) & 0x100F00F00F00F00Full;+ v = (v | v << 4) & 0x10C30C30C30C30C3ull;+ v = (v | v << 2) & 0x1249249249249249ull;+ return v;+ };+ std::vector<std::pair<uint64_t, uint32_t>> order(count);+ for (size_t i = 0; i < count; ++i) {+ const glm::uvec3 tr = m.triangles[tiles_[i]];+ const glm::vec3 c = ((m.positions[tr.x] + m.positions[tr.y] + m.positions[tr.z]) / 3.0f - lo) / ext;+ const glm::uvec3 q(glm::clamp(c, 0.0f, 1.0f) * 2097151.0f);+ order[i] = {spread(q.x) | spread(q.y) << 1 | spread(q.z) << 2, uint32_t(i)};+ }+ std::sort(order.begin(), order.end());+ std::vector<int16_t> change(count * S), firsts(count);+ std::atomic<bool> fits{true};+ poolFor(count, [&](size_t b, size_t e) {+ for (size_t k = b; k < e; ++k) {+ const size_t i = order[k].second;+ const int16_t* now = mm.disp.data() + size_t(tiles_[i]) * S;+ const int16_t* was = oldDisp_.data() + i * S;+ // The change, predicted from the point before it in its row (a row's first point: from the row below;+ // the very first is kept apart), so a smooth change needs few bits.+ const uint32_t n = mm.n;+ uint32_t p = 0;+ for (uint32_t j = 0; j <= n; ++j)+ for (uint32_t i = 0; i + j <= n; ++i, ++p) {+ const int d = int(now[p]) - int(was[p]);+ int pred = 0;+ if (i > 0) pred = int(now[p - 1]) - int(was[p - 1]);+ else if (j > 0) {+ const uint32_t q = MicroMesh::index(0, j - 1, n);+ pred = int(now[q]) - int(was[q]);+ }+ const int r = d - pred;+ if (r < -32767 || r > 32767) fits = false;+ if (p == 0) firsts[k] = int16_t(std::clamp(d, -32767, 32767)), fits = fits && d >= -32767 && d <= 32767;+ change[k * S + p] = p == 0 ? int16_t(0) : int16_t(r);+ }+ }+ }, 256);+ const bool masked = mm.hasMask() && oldMask_.size() == count * S;+ if (fits) {+ u->journal = true;+ u->tris.resize(count);+ u->keys.resize(count);+ for (size_t k = 0; k < count; ++k) u->tris[k] = tiles_[order[k].second], u->keys[k] = order[k].first;+ microPackTiles(change.data(), count, mm.samples, u->packed);+ u->firsts = std::move(firsts);+ if (masked) {+ u->mask.resize(count * S);+ for (size_t k = 0; k < count; ++k)+ std::copy_n(oldMask_.data() + size_t(order[k].second) * S, S, u->mask.data() + k * S);+ }+ } else { // (a change wider than 16 bits: whole old tiles)+ u->tris = tiles_;+ u->disp = std::move(oldDisp_);+ if (masked) u->mask = std::move(oldMask_);+ }u->maskAdded = maskAdded_;- for (uint32_t t : u->tris) tileOf_[t] = 0;tiles_.clear();oldDisp_.clear();oldMask_.clear();diff --git a/src/core/MicroStore.cpp b/src/core/MicroStore.cppindex 6468aab4..8e2927f0 100644--- a/src/core/MicroStore.cpp+++ b/src/core/MicroStore.cpp@@ -146,6 +146,37 @@ size_t MicroStore::residentBytes() const {#endif}+size_t MicroStore::trim(size_t maxBytes) {+ const size_t resident = residentBytes();+#if SF_MICRO_MMAP+ if (map_ && maxBytes && resident > maxBytes) {+ const size_t bytes = mapCount_ * sizeof(int16_t);+ msync(map_, bytes, MS_SYNC); // the file keeps every value+ madvise(map_, bytes, MADV_DONTNEED); // and the pages leave memory+ return residentBytes();+ }+#endif+ return resident;+}++bool MicroStore::rangeResident(size_t first, size_t count) const {+ if (!map_ || count == 0) return true;+#if SF_MICRO_MMAP+ const uintptr_t page = uintptr_t(sysconf(_SC_PAGESIZE));+ const uintptr_t a = reinterpret_cast<uintptr_t>(map_ + first) & ~(page - 1);+ const uintptr_t b = reinterpret_cast<uintptr_t>(map_ + first + count);+ std::vector<unsigned char> vec((b - a + page - 1) / page);+#if defined(__APPLE__)+ if (mincore(reinterpret_cast<void*>(a), b - a, reinterpret_cast<char*>(vec.data())) != 0) return false;+#else+ if (mincore(reinterpret_cast<void*>(a), b - a, vec.data()) != 0) return false;+#endif+ for (unsigned char c : vec)+ if (!(c & 1)) return false;+#endif+ return true;+}+size_t MicroStore::capacity() const { return map_ ? residentBytes() / sizeof(int16_t) : heap_.capacity(); }bool MicroStore::operator==(const std::vector<int16_t>& v) const {diff --git a/src/core/MicroStore.h b/src/core/MicroStore.hindex faaad536..c8f47c16 100644--- a/src/core/MicroStore.h+++ b/src/core/MicroStore.h@@ -53,6 +53,11 @@ public:// Bytes of memory it takes: the array in memory; mapped, what is resident now (mincore).size_t capacity() const; // (in elements, like std::vector: residentBytes() / 2 when mapped)size_t residentBytes() const;+ // Out of core: when more than `maxBytes` are resident, writes the dirty tiles back and lets the system drop+ // them (they page in again when touched). TierConfig::residentPatchCacheMB. Returns the bytes resident after.+ size_t trim(size_t maxBytes);+ // True when every page holding elements [first, first + count) is in memory (always, in memory).+ bool rangeResident(size_t first, size_t count) const;std::vector<int16_t> toVector() const { return std::vector<int16_t>(begin(), end()); }operator std::vector<int16_t>() const { return toVector(); }bool operator==(const std::vector<int16_t>& v) const;diff --git a/src/core/Sculpt.cpp b/src/core/Sculpt.cppindex 4045657a..7bd1f232 100644--- a/src/core/Sculpt.cpp+++ b/src/core/Sculpt.cpp@@ -857,7 +857,9 @@ void Sculptor::beginGrab(Mesh& mesh, BVH& bvh, const DabParams& p, Symmetry sym)grabAll_.clear();grabLeaves_.clear();if (!active_) return;- if (microReady(mesh)) { // along the normals (a micro level can't move sideways)+ // A micro level (core/Micro.h): Move deforms the base cage below it (dual scale: the detail is relative to the+ // base, so it rides along in its local frame), unless asked to move the micro surface along its normals.+ if (microMoveAlongNormals && microReady(mesh)) {for (const glm::vec3& m : mirrors(sym)) micro_.beginGrab(mesh, bvh, p.center * m, p.radius, p.focal, m);return;}diff --git a/src/core/Sculpt.h b/src/core/Sculpt.hindex fa86d31d..cd8bceed 100644--- a/src/core/Sculpt.h+++ b/src/core/Sculpt.h@@ -120,6 +120,9 @@ public:// Micro level timings (findMs, kernelMs, stitchMs, points) of the current stroke (core/Micro.h).const MicroStroke& microStroke() const { return micro_; }+ // Move on a micro level: false (default) deforms the base cage and the detail rides along; true moves the+ // micro displacement along its normals instead.+ bool microMoveAlongNormals = false;MicroStroke& microStroke() { return micro_; }// Mirror sign vectors for the enabled symmetry axes (always starts with (1,1,1)).static std::vector<glm::vec3> mirrors(Symmetry sym);diff --git a/src/core/TierConfig.cpp b/src/core/TierConfig.cppnew file mode 100644index 00000000..3b0bc8c1--- /dev/null+++ b/src/core/TierConfig.cpp@@ -0,0 +1,50 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+#include "core/TierConfig.h"++#include <cstdlib>+#include <mutex>++namespace sf {++TierConfig TierConfig::laptop() {+ TierConfig c;+ c.name = "Laptop";+ c.residentPatchCacheMB = 1024;+ c.activeTriangleBudget = size_t(2) << 20;+ c.continuousNormals = false;+ c.shadeBudgetPoints = size_t(1) << 20;+ c.outOfCorePoints = size_t(256) << 20; // 512 MB of tiles+ return c;+}++TierConfig TierConfig::workstation() { return TierConfig(); }++namespace {++std::mutex& tierMutex() {+ static std::mutex m;+ return m;+}++TierConfig& current() {+ static TierConfig c = [] {+ const char* e = std::getenv("SF_TIER");+ return e && std::string(e) == "laptop" ? TierConfig::laptop() : TierConfig::workstation();+ }();+ return c;+}++} // namespace++const TierConfig& tierConfig() {+ std::lock_guard<std::mutex> lock(tierMutex());+ return current();+}++void setTierConfig(const TierConfig& config) {+ std::lock_guard<std::mutex> lock(tierMutex());+ current() = config;+}++} // namespace sfdiff --git a/src/core/TierConfig.h b/src/core/TierConfig.hnew file mode 100644index 00000000..278fcf25--- /dev/null+++ b/src/core/TierConfig.h@@ -0,0 +1,34 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// Budgets for the two-tier engine (micro-displacement levels, core/Micro.h), by machine class. This branch only+// READS it: the hardware-profile work (feat/hw-profile) detects the machine and calls setTierConfig(); until then+// the Workstation defaults apply (or SF_TIER=laptop|workstation for testing).+#pragma once++#include <cstddef>+#include <string>++namespace sf {++struct TierConfig {+ std::string name = "Workstation";+ // Resident patch cache: the most memory out-of-core micro tiles may keep resident (MicroStore::trim). 0 = no cap.+ size_t residentPatchCacheMB = 0;+ // Active-triangle budget: the most triangles a frame draws for micro levels (render/MicroRenderer.h).+ size_t activeTriangleBudget = size_t(8) << 20;+ // Normal updates while sculpting: continuous (every frame, up to shadeBudgetPoints) or draft (a quarter of+ // that while the stroke runs; the rest when it ends).+ bool continuousNormals = true;+ size_t shadeBudgetPoints = size_t(3) << 20;+ // Micro levels larger than this many grid points go out of core (MicroStore; 0 = a quarter of RAM).+ size_t outOfCorePoints = 0;++ static TierConfig laptop();+ static TierConfig workstation();+};++const TierConfig& tierConfig();+void setTierConfig(const TierConfig& config);++} // namespace sfdiff --git a/src/render/MicroRenderer.cpp b/src/render/MicroRenderer.cppindex 2f020f1d..d0a730d5 100644--- a/src/render/MicroRenderer.cpp+++ b/src/render/MicroRenderer.cpp@@ -3,6 +3,7 @@#include "render/MicroRenderer.h"#include "core/ThreadPool.h"+#include "core/TierConfig.h"#include <glm/gtc/type_ptr.hpp>@@ -474,17 +475,45 @@ bool MicroRenderer::sync(Mesh& mesh) {uploadDispTiles(mesh, scratch_, true);shadePending(mesh, true);} else {- if (baseMoved) { // a transform or a base edit: the base, the bounds and every shading point- uploadBase(mesh);- bytes_ -= mesh.vertexCount() * 2 * sizeof(glm::vec4);- std::vector<uint32_t> all(bounds_.size());- for (uint32_t c = 0; c < all.size(); ++c) all[c] = c;- updateClusterBounds(mesh, all);+ // The base cage moved (dual-scale Move, a transform, undo of either): the detail rides along on the GPU (it+ // is relative to the base); what changes is the base, the bounds and the shading around moved vertices.+ bool baseEverywhere = false;+ std::vector<uint32_t> baseTris;+ if (baseMoved) {+ size_t moved = 0;+ const uint32_t b0 = mesh.dirtyBegin, b1 = std::min<uint32_t>(mesh.dirtyEnd, uint32_t(mesh.vertexCount()));+ for (uint32_t v = b0; v < b1; ++v)+ if (mesh.vertexDirty(v)) ++moved;+ baseEverywhere = moved * 4 > mesh.vertexCount();+ if (baseEverywhere) {+ uploadBase(mesh);+ bytes_ -= mesh.vertexCount() * 2 * sizeof(glm::vec4);+ std::vector<uint32_t> all(bounds_.size());+ for (uint32_t c = 0; c < all.size(); ++c) all[c] = c;+ updateClusterBounds(mesh, all);+ } else {+ std::vector<glm::vec4> part(size_t(b1 - b0) * 2);+ for (uint32_t v = b0; v < b1; ++v) {+ part[size_t(v - b0) * 2] = glm::vec4(mesh.positions[v], 1.0f);+ part[size_t(v - b0) * 2 + 1] = glm::vec4(mesh.normals[v], 0.0f);+ if (mesh.vertexDirty(v))+ for (uint32_t t : mesh.vertexTriangles(v)) baseTris.push_back(t);+ }+ gl::BindBuffer(GL_TEXTURE_BUFFER, baseData_);+ gl::BufferSubData(GL_TEXTURE_BUFFER, GLintptr(size_t(b0) * 2 * sizeof(glm::vec4)), GLsizeiptr(part.size() * sizeof(glm::vec4)),+ part.data());+ gl::BindBuffer(GL_TEXTURE_BUFFER, 0);+ }}// This sync's dirty triangles.scratch_.clear();for (size_t w = 0; w < mm.dirtyBits.size(); ++w)for (uint64_t bits = mm.dirtyBits[w]; bits; bits &= bits - 1) scratch_.push_back(uint32_t(w * 64 + size_t(std::countr_zero(bits))));+ if (!baseTris.empty()) {+ scratch_.insert(scratch_.end(), baseTris.begin(), baseTris.end());+ std::sort(scratch_.begin(), scratch_.end());+ scratch_.erase(std::unique(scratch_.begin(), scratch_.end()), scratch_.end());+ }if (mm.allDirty || mm.rescaled) {uploadDispTiles(mesh, scratch_, true);} else if (!scratch_.empty()) {@@ -499,7 +528,7 @@ bool MicroRenderer::sync(Mesh& mesh) {cl.erase(std::unique(cl.begin(), cl.end()), cl.end());updateClusterBounds(mesh, cl);}- if (mm.allDirty || baseMoved) {+ if (mm.allDirty || baseEverywhere) {shadePending(mesh, true);} else {for (uint32_t t : scratch_) {@@ -507,6 +536,10 @@ bool MicroRenderer::sync(Mesh& mesh) {const uint64_t bit = uint64_t(1) << (t & 63);if (!(w & bit)) w |= bit, ++pendingCount_;}+ // TierConfig: continuous normals shade up to the budget every sync; draft ones a quarter of it while a+ // stroke runs (the rest follows when it ends).+ const TierConfig& tier = tierConfig();+ shadeBudget = tier.shadeBudgetPoints / (mm.stroking && !tier.continuousNormals ? 4 : 1);if (pendingCount_) shadePending(mesh, false);}}diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cppindex 3c6bf838..88022340 100644--- a/src/render/Renderer.cpp+++ b/src/render/Renderer.cpp@@ -6,6 +6,7 @@#include "core/Matcap.h"#include "core/Texture.h"#include "core/ThreadPool.h"+#include "core/TierConfig.h"#include "core/UploadOrder.h"#include <glm/gtc/type_ptr.hpp>@@ -2321,7 +2322,8 @@ void Renderer::drawMicro(const MeshSlot& s, bool active, bool flat, const Rendergl::Uniform1i(p->twoSided, 1);// Moving (Fast Navigation): a coarser target and half the budget, so orbiting stays smooth.const bool moving = settings.motionProxy;- const size_t budget = moving ? settings.renderTriangleBudget / 2 : settings.renderTriangleBudget;+ const size_t full = settings.renderTriangleBudget ? settings.renderTriangleBudget : tierConfig().activeTriangleBudget;+ const size_t budget = moving ? full / 2 : full;const float target = settings.microTargetPx * (moving ? 2.0f : 1.0f);// (the budget is shared by the micro SubTools on screen: each gets it whole; one is the usual case)const_cast<MeshSlot&>(s).microDrawn = s.micro->draw(p->id, frameCull_, frameProj_, frameViewport_, budget, target);diff --git a/src/render/Renderer.h b/src/render/Renderer.hindex f52c466e..a58aed17 100644--- a/src/render/Renderer.h+++ b/src/render/Renderer.h@@ -38,7 +38,7 @@ struct RenderSettings {glm::vec3 backgroundColor{0.165f, 0.165f, 0.173f};// Micro-displacement levels (render/MicroRenderer.h): the most triangles a frame draws for them (their level// of detail drops to fit; a hardware profile may set it), and the micro edge length to aim for on screen.- size_t renderTriangleBudget = size_t(6) << 20;+ size_t renderTriangleBudget = 0; // 0 = TierConfig::activeTriangleBudget (core/TierConfig.h)float microTargetPx = 2.0f;};diff --git a/tests/test_micro.cpp b/tests/test_micro.cppindex 5826b32c..a2b076e3 100644--- a/tests/test_micro.cpp+++ b/tests/test_micro.cpp@@ -11,6 +11,7 @@#include "core/Multires.h"#include "core/Primitives.h"#include "core/Project.h"+#include "core/TierConfig.h"#include "io/Zip.h"#include <chrono>@@ -18,6 +19,7 @@#include <cstdlib>#include <cstring>#include <filesystem>+#include <algorithm>#include <map>using namespace sf;@@ -225,8 +227,29 @@ TEST(micro_mask_brush_protects) {CHECK(doc.mesh.micro->hasMask());}+TEST(micro_move_deforms_the_base_cage) {+ // Dual scale: Move reshapes the base below the micro level; the detail (displacements) rides along unchanged.+ Document doc = microDoc(3);+ microNoise(doc.mesh, 0.005f, 20.0f);+ const std::vector<int16_t> detail = doc.mesh.micro->disp;+ const float h0 = frontHeight(doc);+ Symmetry sym;+ doc.sculptor.beginStroke(doc.mesh, "Move");+ doc.sculptor.beginGrab(doc.mesh, doc.bvh, dabAt(glm::vec3(0, 0, 1), 0.4f, 1.0f), sym);+ for (int i = 0; i < 5; ++i) doc.sculptor.grab(doc.mesh, doc.bvh, glm::vec3(0.03f, 0, 0.02f));+ UndoRecord r = doc.sculptor.endStroke();+ CHECK(!r.positions.empty() && !r.micro);+ doc.undo.push(std::move(r));+ CHECK(doc.mesh.micro->disp == detail);+ CHECK(frontHeight(doc) > h0 + 0.05f);+ CHECK(watertight(doc.mesh));+ doc.undo.undo(doc.mesh, doc.bvh);+ CHECK(std::abs(frontHeight(doc) - h0) < 1e-4f);+}+TEST(micro_move_brush_moves_along_normals) {Document doc = microDoc(3);+ doc.sculptor.microMoveAlongNormals = true;const float h0 = frontHeight(doc);Symmetry sym;doc.sculptor.beginStroke(doc.mesh, "Move");@@ -496,3 +519,51 @@ TEST(micro_adaptive_brush_matches_full) {CHECK(std::abs(ha - hb) < (ha - 1.0f) * 0.02f);CHECK(watertight(b.mesh));}++TEST(micro_undo_journal_is_small) {+ // A stroke's undo step is its change, packed per tile in Morton order: far below 2 bytes per touched point.+ Document doc = microDoc(4);+ microNoise(doc.mesh, 0.003f, 30.0f); // detail under the brush (old values far from zero)+ Symmetry sym;+ doc.sculptor.beginStroke(doc.mesh, "Standard");+ for (int i = 0; i < 10; ++i)+ doc.sculptor.dab(doc.mesh, doc.bvh, BrushType::Standard, dabAt(glm::normalize(glm::vec3(0.03f * float(i), 0.1f, 1)), 0.15f), sym);+ UndoRecord r = doc.sculptor.endStroke();+ CHECK(r.micro && r.micro->journal);+ const size_t raw = r.micro->tris.size() * doc.mesh.micro->samples * 2;+ std::printf(" micro undo journal: %zu tiles, %.1f KB (whole tiles %.1f KB)\n", r.micro->tris.size(),+ double(r.micro->bytes()) / 1e3, double(raw) / 1e3);+ CHECK(r.micro->bytes() * 2 < raw);+ CHECK(std::is_sorted(r.micro->keys.begin(), r.micro->keys.end()));+ const std::vector<int16_t> after = doc.mesh.micro->disp;+ doc.undo.push(std::move(r));+ doc.undo.undo(doc.mesh, doc.bvh);+ doc.undo.redo(doc.mesh, doc.bvh);+ CHECK(doc.mesh.micro->disp == after);+}++TEST(micro_tier_config_and_page_table) {+ const TierConfig saved = tierConfig();+ setTierConfig(TierConfig::laptop());+ CHECK(tierConfig().activeTriangleBudget < TierConfig::workstation().activeTriangleBudget);+ CHECK(!tierConfig().continuousNormals);+ // Laptop: big levels go out of core sooner.+ CHECK(microWantsOutOfCore(size_t(300) << 20) || std::getenv("SF_MICRO_OUT_OF_CORE"));+ setTierConfig(saved);+ Document doc = microDoc(2, 3000);+ PatchPageTable pages(*doc.mesh.micro);+ CHECK(pages.residentPatches() == doc.mesh.micro->baseTriangles); // in memory: every patch+ MicroPatch p = microPatch(doc.mesh, 5);+ CHECK(p.points() == doc.mesh.micro->samples && p.heights == doc.mesh.micro->disp.data() + 5 * p.points());+ std::string err;+ Document ooc = makeDoc(makeBenchSphere(20000));+ CHECK(microDivide(ooc.mesh, 3, err, 1));+ if (ooc.mesh.micro->disp.mapped()) {+ PatchPageTable cold(*ooc.mesh.micro);+ CHECK(cold.residentPatches() < ooc.mesh.micro->baseTriangles); // untouched: holes, not resident+ microPatch(ooc.mesh, 7).heights[0] = 3;+ CHECK(cold.resident(7));+ cold.trim(1);+ CHECK(ooc.mesh.micro->disp[7 * ooc.mesh.micro->samples] == 3); // trimmed pages come back from the file+ }+}