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
Focus Detail on main: the app loop updates the focus (camera promotion ran only in UI tests); a widened displacement range rescales the tiles not in memory (coarse grids, parked tiles, a per-tile shift for the file; Save writes every coarse chunk and the new step)
islandmedd2604a9a45e8
diff --git a/changes/fix-focus-detail-main.md b/changes/fix-focus-detail-main.mdnew file mode 100644index 00000000..74a8eeba--- /dev/null+++ b/changes/fix-focus-detail-main.md@@ -0,0 +1,9 @@+---+fixed: Focus Detail (experimental, `--focus-detail`): the app loop now loads full detail where the camera looks (it+ only did in UI tests); a stroke that widened the displacement range left the tiles not yet loaded at the old scale+ (they now follow, and Save writes them at the new scale).+testers:+ - Linux or macOS: `sculptforge --focus-detail head.sfdetail` (make one with `sculptforge-cli --bench-focus`), zoom+ into the face: pores appear within a second. A strong Inflate stroke, then orbit: no tile looks flattened or+ spiky, and after Save + reopen it looks the same.+---diff --git a/src/app/App.cpp b/src/app/App.cppindex 754ad0f1..ebff0a0a 100644--- a/src/app/App.cpp+++ b/src/app/App.cpp@@ -1197,6 +1197,7 @@ int App::run() {perf_.proxyPrebuild = uiPrefs_.fastNavigation && !perf_.movingNow && !doc_.sculptor.strokeActive() &&dabQueue_.empty() && !strokeFinishing_ && !solverBusy();renderer_.setProxyPrebuild(perf_.proxyPrebuild);+ updateFocusDetail(); // a streamed micro level: promote what the camera and brush need, before the uploadsyncScene();mark();notePerformanceFrame(std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - syncStart).count());diff --git a/src/core/FocusDetail.cpp b/src/core/FocusDetail.cppindex c42c849e..77c13de1 100644--- a/src/core/FocusDetail.cpp+++ b/src/core/FocusDetail.cpp@@ -516,7 +516,12 @@ std::shared_ptr<FocusDetail> FocusDetail::open(const std::string& path, Mesh& oubool FocusDetail::readTile(uint32_t t, int16_t* out) const {if (!parked_[t].empty()) return decompressTile(codec_, parked_[t].data(), parked_[t].size(), n_, samples_, out);const Entry& e = dir_[t];- return map_ && decompressTile(codec_, map_ + e.offset, e.bytes, n_, samples_, out);+ if (!map_ || !decompressTile(codec_, map_ + e.offset, e.bytes, n_, samples_, out)) return false;+ if (e.shift) { // in an older step: as MicroMesh::widen divides (truncating; successive divisions compose)+ const int div = e.shift >= 15 ? 32768 : 1 << e.shift;+ for (uint32_t k = 0; k < samples_; ++k) out[k] = int16_t(int(out[k]) / div);+ }+ return true;}void FocusDetail::fillCoarse(MicroMesh& mm, uint32_t t) const {@@ -576,6 +581,46 @@ void FocusDetail::ensureFull(MicroMesh& mm, std::span<const uint32_t> tris, boolif (!need.empty()) promoteLocked(mm, need, edit, true);}+void FocusDetail::rescale(MicroMesh& mm, int doublings) {+ if (doublings <= 0) return;+ std::lock_guard<std::mutex> lock(mutex_);+ const int div = 1 << doublings;+ const uint32_t T = uint32_t(state_.size());+ poolFor(coarse_.size(), [&](size_t b, size_t e) {+ for (size_t i = b; i < e; ++i) coarse_[i] = int16_t(int(coarse_[i]) / div);+ }, 1 << 16);+ poolFor(T, [&](size_t b, size_t e) {+ std::vector<int16_t> g(samples_);+ for (size_t t = b; t < e; ++t) {+ Entry& d = dir_[t];+ d.shift = uint16_t(std::min(int(d.shift) + doublings, 15));+ d.range = uint16_t(std::min(int(d.range) / div + 1, 32767));+ if (parked_[t].empty() || !decompressTile(codec_, parked_[t].data(), parked_[t].size(), n_, samples_, g.data())) continue;+ for (int16_t& v : g) v = int16_t(int(v) / div);+ compressTile(codec_, g.data(), n_, samples_, parked_[t]);+ }+ }, 64);+ step_ *= float(div);+ rescaled_ = true;+ // widen divided the coarse tiles' upsampled values, which rounds unlike upsampling the divided coarse grid:+ // upsampled again, then the real edges of their full neighbours.+ poolFor(T, [&](size_t b, size_t e) {+ for (size_t t = b; t < e; ++t)+ if (state_[t] == kCoarse) fillCoarse(mm, uint32_t(t));+ }, 64);+ for (uint32_t t = 0; t < T; ++t)+ if (state_[t] != kCoarse) shareEdges(tris_, mm, t, [&](uint32_t u) { return state_[u] == kCoarse ? 1 : 0; });+ mm.markAllDirty();+}++bool FocusDetail::unsaved() const {+ std::lock_guard<std::mutex> lock(mutex_);+ if (rescaled_) return true;+ for (size_t t = 0; t < state_.size(); ++t)+ if (state_[t] == kDirty || !parked_[t].empty()) return true;+ return false;+}+void FocusDetail::update(Mesh& m, const FocusView& view, const FocusBrush& brush, double budgetMs) {if (!m.micro || m.micro->focus.get() != this) return;const auto t0 = Clock::now();@@ -710,7 +755,7 @@ bool FocusDetail::save(Mesh& m, std::string& error) {std::memcpy(baseBlock.data() + m.vertexCount() * 24, tris_.data(), tris_.size() * 12);if (fnv1a(baseBlock.data(), m.vertexCount() * 24) == baseHash_) baseBlock.clear();}- if (tiles.empty() && baseBlock.empty()) return true;+ if (tiles.empty() && baseBlock.empty() && !rescaled_) return true;std::vector<std::vector<uint8_t>> blobs(tiles.size());poolFor(tiles.size(), [&](size_t b, size_t e) {for (size_t k = b; k < e; ++k) {@@ -727,8 +772,12 @@ bool FocusDetail::save(Mesh& m, std::string& error) {}}, 8);std::vector<uint32_t> chunkList;- for (uint32_t t : tiles)- if (chunkList.empty() || chunkList.back() != t / kChunk) chunkList.push_back(t / kChunk);+ if (rescaled_) { // every coarse grid changed+ for (uint32_t c = 0; c < uint32_t(chunks_.size()); ++c) chunkList.push_back(c);+ } else {+ for (uint32_t t : tiles)+ if (chunkList.empty() || chunkList.back() != t / kChunk) chunkList.push_back(t / kChunk);+ }std::vector<std::vector<uint8_t>> chunkBlobs(chunkList.size());poolFor(chunkList.size(), [&](size_t b, size_t e) {for (size_t k = b; k < e; ++k) {@@ -746,6 +795,7 @@ bool FocusDetail::save(Mesh& m, std::string& error) {garbage += dir[tiles[k]].bytes;dir[tiles[k]].offset = offset + tail.size();dir[tiles[k]].bytes = uint32_t(blobs[k].size());+ dir[tiles[k]].shift = 0;tail.insert(tail.end(), blobs[k].begin(), blobs[k].end());}for (size_t k = 0; k < chunkList.size(); ++k) {@@ -771,6 +821,7 @@ bool FocusDetail::save(Mesh& m, std::string& error) {h.dirHash = fnv1a(tail.data() + dirAt, h.dirBytes);h.generation = generation_ + 1;h.garbage = garbage;+ h.step = step_;auto pwriteAll = [&](const uint8_t* p, size_t size, uint64_t at) {while (size) {const ssize_t w = ::pwrite(fd_, p, size, off_t(at));@@ -789,6 +840,7 @@ bool FocusDetail::save(Mesh& m, std::string& error) {chunks_ = std::move(chunkDir);garbage_ = garbage;generation_ = h.generation;+ rescaled_ = false;if (!baseBlock.empty()) baseHash_ = fnv1a(baseBlock.data(), m.vertexCount() * 24);for (uint32_t t : tiles) {if (state_[t] == kDirty) state_[t] = kFull;diff --git a/src/core/FocusDetail.h b/src/core/FocusDetail.hindex b9bb283e..24eb7096 100644--- a/src/core/FocusDetail.h+++ b/src/core/FocusDetail.h@@ -19,6 +19,8 @@// - Edited tiles are dirty until saved; a dirty tile that gets demoted is compressed into memory ("parked").// save() appends only the dirty and parked tiles, the coarse chunks holding them and a new directory, then// rewrites the header (the old directory stays valid until then: a crash keeps the last save).+// - When a brush widens the displacement range (MicroMesh::widen), rescale() divides the coarse grids and parked+// tiles at once and the tiles still in the file as they are read (a per-tile shift in the directory).// Undo needs nothing new: a stroke's MicroUndo already keeps copy-on-write deltas of the tiles it touched.#pragma once@@ -88,6 +90,8 @@ public:void update(Mesh& m, const FocusView& view, const FocusBrush& brush, double budgetMs = 4.0);// Makes tiles `tris` full now (brushes, undo); `edit` marks them dirty. Thread-safe.void ensureFull(MicroMesh& mm, std::span<const uint32_t> tris, bool edit);+ // MicroMesh::widen divided every value by 2^doublings and multiplied the step: the same for what isn't in disp.+ void rescale(MicroMesh& mm, int doublings);// Writes the changed tiles to the file (appended), a new directory and header. Returns false with `error`.bool save(Mesh& m, std::string& error);@@ -95,6 +99,7 @@ public:int tileLevel(uint32_t tri) const { return tri < state_.size() && state_[tri] != kCoarse ? 1 : 0; }FocusStats stats() const;const std::string& path() const { return path_; }+ bool unsaved() const; // edits since the last savesize_t budgetBytes = size_t(512) << 20; // full tiles kept (2 bytes a point) before the LRU demotesfloat lookaheadSeconds = 0.25f; // how far ahead of the brush promotion looks@@ -107,7 +112,7 @@ private:uint64_t offset = 0;uint32_t bytes = 0;uint16_t range = 0; // the largest |value| of the full tile- uint16_t pad = 0;+ uint16_t shift = 0; // the stored tile is in step / 2^shift units (the range widened since it was written)float coarseErr = 0; // world units: the most the upsampled coarse grid is offuint32_t pad2 = 0;};@@ -127,6 +132,7 @@ private:uint32_t n_ = 0, samples_ = 0, coarseN_ = 0, coarseSamples_ = 0, codec_ = 0;float step_ = 0;uint64_t generation_ = 0, garbage_ = 0;+ bool rescaled_ = false; // the coarse chunks in the file are in an older stepstd::vector<Entry> dir_;std::vector<ChunkEntry> chunks_;std::vector<int16_t> coarse_; // tiles * coarseSamples, always residentdiff --git a/src/core/Micro.cpp b/src/core/Micro.cppindex 9d5a4fea..967b953a 100644--- a/src/core/Micro.cpp+++ b/src/core/Micro.cpp@@ -76,6 +76,7 @@ int MicroMesh::widen(float need) {step *= float(div);rescaled = true;reach.all = true;+ if (focus) focus->rescale(*this, doublings); // (the tiles not in disp: core/FocusDetail.h)++revision;return div;}diff --git a/tests/test_focus_detail.cpp b/tests/test_focus_detail.cppindex 1345bae0..dad6c899 100644--- a/tests/test_focus_detail.cpp+++ b/tests/test_focus_detail.cpp@@ -254,4 +254,56 @@ TEST(focus_detail_brush_thread_and_frames_together) {CHECK(edgesShared(m));}+namespace {++// The detail file at `path` fully decompressed (a reference).+std::vector<int16_t> fullValues(const std::string& path) {+ Mesh m;+ std::string err;+ auto fd = FocusDetail::open(path, m, err);+ if (!fd) return {};+ fd->ensureFull(*m.micro, allTiles(m), false);+ return m.micro->disp.toVector();+}++std::vector<int16_t> streamedFull(Mesh& m) {+ m.micro->focus->ensureFull(*m.micro, allTiles(m), false);+ return m.micro->disp.toVector();+}++} // namespace++// A brush that widens the displacement range (MicroMesh::widen) divides every value: coarse grids, parked tiles+// and the tiles still in the file (read with a shift) follow, so later promotions match, watertight, saved and+// reopened alike.+TEST(focus_detail_widened_range_rescales_streamed_tiles) {+ const std::string path = writeSmall("widen.sfdetail");+ const std::vector<int16_t> ref = fullValues(path);+ Mesh m;+ std::string err;+ auto fd = FocusDetail::open(path, m, err);+ CHECK(fd != nullptr && !ref.empty());+ if (!fd || ref.empty()) return;+ MicroMesh& mm = *m.micro;+ std::vector<uint32_t> parked, full;+ for (uint32_t t = 0; t < mm.baseTriangles; t += 5) parked.push_back(t);+ for (uint32_t t = 2; t < mm.baseTriangles; t += 5) full.push_back(t);+ fd->ensureFull(mm, parked, true);+ fd->budgetBytes = 0; // demoted while dirty: parked (compressed in memory)+ for (int f = 0; f < 12; ++f) fd->update(m, FocusView{}, FocusBrush{}, 1000.0);+ CHECK(fd->stats().parkedTiles > 0);+ fd->budgetBytes = size_t(1) << 30;+ fd->ensureFull(mm, full, true); // full and dirty; the rest coarse, read from the file+ const int div = mm.widen(mm.rangeWorld() * 3.0f);+ CHECK(div >= 4);+ CHECK(edgesShared(m));+ CHECK(fd->unsaved());+ auto expected = ref;+ for (int16_t& v : expected) v = int16_t(int(v) / div);+ CHECK(streamedFull(m) == expected);+ CHECK(fd->save(m, err));+ CHECK(!fd->unsaved());+ CHECK(fullValues(path) == expected);+}+#endif