From 273b3bdf9d6e736442ad584ad464df6e86ee928b Mon Sep 17 00:00:00 2001 From: Moriz82 <72836413+Moriz82@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:42:12 -0600 Subject: [integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD) One squashed commit of a fork that was developed on a Linux/Proton machine and tested on two native Windows machines. On all of them the retail 1.0.2.1 package now launches, shows its launch movie, first-run pages and landing prompt, runs the story load to its end with the loading slideshow drawing its artwork, and enters the world phase. A first world frame is not verified yet. docs/linux_proton.md has the setup, a healthy boot's timeline, the machines and where it stops. Runtime: frames in the title's own window; DirectStorage pinned reads, page-list scatter/gather, BCPack and unshuffle with a locally generated predictor file; pooled, device-local and deferred image backing for streamed and placed textures; enhanced barriers into LAYOUT_UNDEFINED; a bindless table that degrades instead of refusing a submission; a descriptor hash finalizer (off until a library carries it); directly indexed heap offsets for compute dispatches and VS/PS/GS draws; title buffer windows outside a fragmented arena; opt-in device-local copies of streamed buffer views; write-fault retries under the tracker lock, shared trackers and write-watched title memory for native Windows; device-loss, compute-input and memory probes. Translator: loop bounds at structured exits, the eight FidelityFX programs that use a packed-dot form the disassembler skips, buffer bounds modes per view kind, a compaction option, the hash finalizer option. Tools and scripts: Linux boot scripts (Wine/Proton, no network), a Windows boot script, the paired compute translation script. No game data. AI-assisted (Claude Code and Codex agents), reviewed and booted by people on the machines named in docs/linux_proton.md. Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 --- runtime/src/bcpack.cpp | 423 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 423 insertions(+) create mode 100644 runtime/src/bcpack.cpp (limited to 'runtime/src/bcpack.cpp') diff --git a/runtime/src/bcpack.cpp b/runtime/src/bcpack.cpp new file mode 100644 index 000000000..bb4646787 --- /dev/null +++ b/runtime/src/bcpack.cpp @@ -0,0 +1,423 @@ +// C++ port of tools/bcpack.py. The build derives the model and BCn format +// constants from that same reference. +#include "bcpack.h" +#include "bcpack_stream.h" +#include +#include +#include +#include + +namespace leonida::bcpack { +namespace { +using namespace detail; + +void color(uint8_t *dst, const int *e, int a, int b, int c, int d) { + for (int i = 0; i < 2; ++i, e += 3) { + const int rgb = (e[0] << 11) | (e[1] << 5) | e[2]; + dst[i * 2] = uint8_t(rgb); + dst[i * 2 + 1] = uint8_t(rgb >> 8); + } + dst[4] = uint8_t((a & 15) | (b << 4)); + dst[5] = uint8_t((a >> 4) | (b & 240)); + dst[6] = uint8_t((c & 15) | (d << 4)); + dst[7] = uint8_t((c >> 4) | (d & 240)); +} + +struct Endpoints { + int v[8]{}; +}; + +bool rebuild_classic(unsigned codec, size_t i, const Symbols &a, + const Symbols &b, array &past, + uint8_t *dst, bool allow_fitted, Quality &quality) { + const bool single = codec == 1 || codec == 4; + if (a[1] || b[1]) + return false; + const unsigned distance = unzig(single ? a[0] : interleave(a[0], b[0])) & 255; + Endpoints e; + const int count = codec == 1 ? 6 : codec == 3 ? 8 : codec == 4 ? 2 : 4; + if (!distance) { + if (codec != 1) + fill_n(e.v, codec == 5 ? 4 : 2, 128); + } else if (distance <= i) { + e = past[(i - distance) & 255]; + } else { + const unsigned slot = (i - distance) & 255; + const Seed &seed = codec == 1 ? history1[slot] + : codec == 3 ? history3[slot] + : codec == 4 ? history4[slot] + : history5[slot]; + if (seed.values[0] < 0 || (seed.fitted && !allow_fitted)) + return false; + if (seed.fitted) + quality = Quality::fitted; + copy_n(seed.values, count, e.v); + } + const int alpha = codec == 1 ? 0 : codec == 5 ? 4 : 2; + for (int k = 0; k < count; ++k) { + const int coord = k - alpha; + const int mask = coord < 0 ? 255 : coord % 3 == 1 ? 63 : 31; + const int symbol = single ? a[2 + k] + : coord < 0 ? (k % 2 ? b : a)[2 + k / 2] + : (coord < 3 ? a : b)[7 + coord % 3]; + e.v[k] = (e.v[k] + unzig(symbol)) & mask; + } + past[i & 255] = e; + for (int ch = 0; ch < alpha / 2; ++ch) { + const auto *pairs = codec == 3 ? pair3 : codec == 4 ? pair4 : pair5; + uint64_t bits = 0; + for (int row = 0; row < 4; ++row) { + const int lane = + codec == 4 ? 4 + 2 * row : (codec == 3 ? 3 : 4 + 4 * ch) + row; + const int right = single ? a[lane + 1] : b[lane]; + bits |= uint64_t(pairs[a[lane]] | (pairs[right] << 6)) << (12 * row); + } + *dst++ = uint8_t(e.v[ch * 2]); + *dst++ = uint8_t(e.v[ch * 2 + 1]); + for (int j = 0; j < 6; ++j) + *dst++ = uint8_t(bits >> (8 * j)); + } + if (codec == 1) + color(dst, e.v, quad1[a[8]], quad1[a[9]], quad1[a[10]], quad1[a[11]]); + if (codec == 3) + color(dst, e.v + 2, quad3[a[10]], quad3[b[10]], quad3[a[11]], quad3[b[11]]); + return true; +} + +struct BitWriter { + uint8_t *dst; + int pos{}, violations{}; + void put(int value, int bits) { + while (bits > 0) { + const int n = min(bits, 8 - pos % 8); + dst[pos / 8] |= uint8_t((value & ((1 << n) - 1)) << (pos % 8)); + value >>= n; + bits -= n; + pos += n; + } + } +}; + +// Endpoints and indices have independent predictors, with distances limited to +// eight bits. +struct BptcState { + array ep{}; + array ix{}; + int ep_root = -1, ix_root = -1; +}; +int mod_bits(int v, int n) { return v & ((1 << n) - 1); } + +void index_row(int a, int b, int row, int bits, const int *ref, int *value, + int *history) { + for (int x = 0; x < 4; ++x) { + const int s = (x < 2 ? a : b) >> ((x % 2) ^ (row < 3)); + const int delta = + (s & 1) | ((s >> 1) & 2) | ((s >> 2) & 4) | ((s >> 3) & 8); + const int p = 4 * row + x; + value[p] = mod_bits((ref[p] >> (4 - bits)) + unzig(delta), bits); + history[p] = value[p] << (4 - bits); + } +} + +bool put_indices(BitWriter &writer, const int *values, int bits, + int anchor1 = -1, int anchor2 = -1) { + for (int p = 0; p < 16; ++p) { + const int n = bits - (p == 0 || p == anchor1 || p == anchor2); + if (values[p] >> n) + ++writer.violations; + writer.put(values[p], n); + } + return true; +} + +bool bc6(const Symbols &a, const Symbols &b, const BptcState &ep, + const BptcState &ix, BptcState &next, BitWriter &writer) { + if (a[15] >= 14) + return false; + const auto &m = bc6Modes[a[15]]; + const int partition = min(b[15], 31), base_shift = 16 - m.base; + int fields[3][4]{}; + for (int c = 0; c < 3; ++c) { + const int n = m.delta[c], shift = max(0, 6 - n); + const auto *ref = &ep.ep[c * 4]; + const int w = mod_bits((ref[0] >> base_shift) + + unzig(interleave(a[2 + c], b[2 + c], 8)), + m.base); + const int x = mod_bits( + (ref[1] >> (10 - n)) + unzig(interleave(a[5 + c], b[5 + c], 5)), n); + const int y = mod_bits((ref[2] >> shift) + unzig(a[8 + c]), n); + const int z = mod_bits((ref[3] >> shift) + unzig(b[8 + c]), n); + auto *state = &next.ep[c * 4]; + state[0] = w << base_shift; + state[1] = x << (10 - n); + if (m.regions == 2) { + state[2] = y << shift; + state[3] = z << shift; + } else { + const int v = mod_bits(w, n) ^ (m.transformed << (n - 1)); + state[2] = n >= 6 ? v >> (n - 6) : v << shift; + state[3] = state[1] >> 4; + } + fields[c][0] = w; + if (a[15] >= 12) { + fields[c][0] &= 1023; + for (int bit = 10; bit < m.base; ++bit) + fields[c][0] |= ((w >> bit) & 1) << (m.base + 9 - bit); + } + fields[c][1] = x; + fields[c][2] = y; + fields[c][3] = z; + } + writer.put(m.code, m.code < 2 ? 2 : 5); + for (int j = 0; j < m.count; ++j) { + const auto &f = bc6Plan[m.first + j]; + writer.put((f[0] < 0 ? partition : fields[f[0]][f[1]]) >> f[2], 1); + } + int values[16]; + const int bits = m.regions == 2 ? 3 : 4; + for (int y = 0; y < 4; ++y) + index_row(a[11 + y], b[11 + y], y, bits, ix.ix.data(), values, + next.ix.data()); + return put_indices(writer, values, bits, + m.regions == 2 ? anchor2[partition] : -1); +} + +bool bc7(const Symbols &a, const Symbols &b, const BptcState &ep, + const BptcState &ix, BptcState &next, BitWriter &writer) { + if (a[15] >= 32 || b[15] >= 16) + return false; + const auto &cfg = bc7Config[a[15] * 16 + b[15]]; + const auto [tag, part, rot, isel] = cfg; + const int mode = tag & 127; + if (mode > 7 || part >= 64) + return false; + const auto &f = bc7_formats[mode]; + int widths[4] = {f.color, f.color, f.color, f.alpha}; + if (rot) + swap(widths[rot - 1], widths[3]); + constexpr int lane[3][4] = {{2, 3, 4, 5}, {6, 7, 8, 9}, {10, 5, 9, -1}}; + int v[3][2][4]{}; + for (int sub = 0; sub < f.subsets; ++sub) { + for (int end = 0; end < 2; ++end) { + for (int c = 0; c < 4; ++c) { + if (!widths[c] || lane[sub][c] < 0) + continue; + const int n = widths[c] + (f.pbits && c == 0); + const int k = 8 * sub + 4 * end + c; + v[sub][end][c] = mod_bits( + (ep.ep[k] >> (8 - n)) + unzig((end ? b : a)[lane[sub][c]]), n); + next.ep[k] = v[sub][end][c] << (8 - n); + } + } + if (f.pbits == 2 && ((v[sub][0][0] ^ v[sub][1][0]) & 1)) + ++writer.violations; + } + for (int sub = f.subsets; sub < 3; ++sub) + copy_n(next.ep.begin(), 8, next.ep.begin() + 8 * sub); + int primary[16], secondary[16]; + constexpr int second_rows[] = {6, 7, 8, 10}; + const bool two = mode == 4 || mode == 5; + for (int y = 0; y < 4; ++y) { + index_row(a[11 + y], b[11 + y], y, f.index, ix.ix.data(), primary, + next.ix.data()); + if (two) + index_row(a[second_rows[y]], b[second_rows[y]], y, 2, ix.ix.data() + 16, + secondary, next.ix.data() + 16); + } + if (mode == 0 && part == 0 && + ranges::all_of(next.ep, [](int v) { return !v; }) && + ranges::all_of(primary, [](int v) { return !v; })) + return true; + + writer.put(1 << mode, mode + 1); + writer.put(part, f.partition); + if (two) + writer.put(rot, 2); + if (mode == 4) + writer.put(isel, 1); + for (int c = 0; c < 4; ++c) { + const int src = rot && c == rot - 1 ? 3 : rot && c == 3 ? rot - 1 : c; + for (int sub = 0; sub < f.subsets; ++sub) + for (int end = 0; end < 2; ++end) + writer.put(v[sub][end][src] >> (f.pbits && src == 0), + c == 3 ? f.alpha : f.color); + } + if (f.pbits) { + for (int sub = 0; sub < f.subsets; ++sub) + for (int end = 0; end < (f.pbits == 1 ? 2 : 1); ++end) + writer.put(v[sub][end][0], 1); + } + const int anc1 = f.subsets == 2 ? anchor2[part] + : f.subsets == 3 ? anchor3a[part] + : -1; + const int anc2 = f.subsets == 3 ? anchor3b[part] : -1; + if (mode == 4) + return put_indices(writer, secondary, 2) && + put_indices(writer, primary, f.index); + return put_indices(writer, primary, f.index, anc1, anc2) && + (!two || put_indices(writer, secondary, 2)); +} + +// A predictor can influence later blocks through either independent reference +// chain. A bit-depth gap discovered during the first 256 blocks also affects +// earlier uses of the slot. +struct Coverage { + array missing_ep{}, missing_ix{}; + size_t lost{}, inconsistent{}, limit{}; + bool lost_endpoints{}, lost_indices{}; + void note_missing(const BptcState &block) { + const bool ep = block.ep_root >= 0 && missing_ep[block.ep_root]; + const bool ix = block.ix_root >= 0 && missing_ix[block.ix_root]; + lost += ep || ix; + lost_endpoints |= ep; + lost_indices |= ix; + } +}; + +bool rebuild_bptc(unsigned codec, size_t i, size_t blocks, const Symbols &a, + const Symbols &b, array &past, uint8_t *dest, + const Calibration *history, bool allow_fitted, + Quality &quality, Coverage &coverage, const char*& reason) { + const unsigned di = interleave(a[1], b[1]); + const unsigned de = (di + unzig(interleave(a[0], b[0]))) & 255; + if ((di > i || de > i) && !history) { + reason = "missing-profile"; + return false; + } + const int islot = di > i ? int(256 + i - di) : -1; + const int eslot = de > i ? int(256 + i - de) : -1; + bool fitted = false; + BptcState zero{}; + if (codec == 6) { + for (int c = 0; c < 3; ++c) { + zero.ep[4 * c] = 32768; + zero.ep[4 * c + 1] = 512; + zero.ep[4 * c + 2] = zero.ep[4 * c + 3] = 32; + } + zero.ix.fill(8); + } + if (codec == 6) { + if (a[15] >= 14) + return false; + const auto &m = bc6Modes[a[15]]; + if (eslot >= 0) { + const auto &row = history->bc6[eslot]; + for (int c = 0; c < 3; ++c) { + const int needs[] = {m.base, m.delta[c], + m.regions == 2 ? m.delta[c] : 0, + m.regions == 2 ? m.delta[c] : 0}; + for (int k = 0; k < 4; ++k) { + const int j = 4 * c + k; + zero.ep[j] = row.ep[2 * j] | (row.ep[2 * j + 1] << 8); + fitted |= row.ep_confirmed[j] < needs[k]; + } + } + fitted |= row.ep_class != 3; + coverage.missing_ep[eslot] |= !row.ep_class; + } + if (islot >= 0) { + const auto &row = history->bc6[islot]; + copy(row.ix.begin(), row.ix.end(), zero.ix.begin()); + fitted |= + row.ix_class != 3 || ranges::any_of(row.ix_confirmed, [&](int n) { + return n < (m.regions == 2 ? 3 : 4); + }); + coverage.missing_ix[islot] |= !row.ix_class; + } + } else { + if (a[15] >= 32 || b[15] >= 16) + return false; + const auto &cfg = bc7Config[a[15] * 16 + b[15]]; + const unsigned mode = cfg[0] & 127; + if (mode >= 8 || cfg[1] >= 64) + return false; + if (cfg[0] & 128) + fitted = true; + const auto &format = bc7_formats[mode]; + if (eslot >= 0) { + const auto &row = history->bc7[eslot]; + copy(row.ep.begin(), row.ep.end(), zero.ep.begin()); + int width[] = {format.color, format.color, format.color, format.alpha}; + if (cfg[2]) + swap(width[3], width[cfg[2] - 1]); + width[0] += format.pbits != 0; + for (int j = 0; j < 8 * format.subsets; ++j) { + const int need = width[j % 4]; + fitted |= row.ep_confirmed[j] < need || row.ep_bits[j] < need; + coverage.missing_ep[eslot] |= row.ep_bits[j] < min(need, 4); + } + } + if (islot >= 0) { + const auto &row = history->bc7[islot]; + copy(row.ix.begin(), row.ix.end(), zero.ix.begin()); + for (int j = 0; j < (mode == 4 || mode == 5 ? 32 : 16); ++j) { + int need = j < 16 ? format.index : 2; + fitted |= row.ix_confirmed[j] < need || row.ix_bits[j] < need; + coverage.missing_ix[islot] |= row.ix_bits[j] < need; + } + } + } + if (fitted) { + if (!allow_fitted) { + reason = "unconfirmed-predictor"; + return false; + } + quality = Quality::fitted; + } + const auto &ep = de && eslot < 0 ? past[(i - de) & 255] : zero; + const auto &ix = di && islot < 0 ? past[(i - di) & 255] : zero; + auto &next = past[i & 255]; + next = {}; + next.ep_root = eslot >= 0 ? eslot : ep.ep_root; + next.ix_root = islot >= 0 ? islot : ix.ix_root; + fill_n(dest, 16, 0); + BitWriter writer{dest}; + const bool valid = codec == 6 ? bc6(a, b, ep, ix, next, writer) + : bc7(a, b, ep, ix, next, writer); + coverage.inconsistent += + codec == 6 ? writer.violations : int(writer.violations != 0); + if (i + 1 == min(256, blocks)) { + for (size_t k = 0; k <= i; ++k) + coverage.note_missing(past[k]); + } else if (i >= 256) + coverage.note_missing(next); + if (!valid) return false; + if (coverage.lost > coverage.limit) { + reason = coverage.lost_endpoints && coverage.lost_indices ? "missing-endpoint-and-index" : + coverage.lost_endpoints ? "missing-endpoint" : "missing-index"; + return false; + } + if (coverage.inconsistent > coverage.limit) { reason = "anchor-or-pbit"; return false; } + return true; +} +} // namespace + +Quality decode(unsigned codec, std::span source, + std::span output, bool allow_fitted, + const Calibration *history, const char** failure, bool fast_entropy) { + const char* reason = "bcpack-stream"; + if (failure) *failure = reason; + Quality quality = Quality::confirmed; + if (!codec || codec == 2 || codec > 7) + return Quality::failed; + const bool single = codec == 1 || codec == 4; + const unsigned block_size = single ? 8 : 16; + if (output.size() % block_size) + return Quality::failed; + array classic{}; + array bptc{}; + Coverage coverage{}; + coverage.limit = allow_fitted && history + ? max(2, output.size() / block_size / 1024) + : 0; + const bool valid = visit_blocks(codec, source, output.size(), [&](size_t i, const Symbols &first, const Symbols &tuple) { + auto *dst = output.data() + i * block_size; + if (codec >= 6) + return rebuild_bptc(codec, i, output.size() / block_size, first, tuple, bptc, dst, history, allow_fitted, quality, coverage, reason); + return rebuild_classic(codec, i, first, tuple, classic, dst, allow_fitted, quality); + }, fast_entropy); + if (valid && (!coverage.inconsistent || quality == Quality::fitted)) return quality; + if (failure) *failure = coverage.inconsistent && valid ? "anchor-or-pbit" : reason; + return Quality::failed; +} +} // namespace leonida::bcpack -- cgit v1.2.3 From 7b251064ade817ade05a1bfe40d160558ac62604 Mon Sep 17 00:00:00 2001 From: kawaii-shakal Date: Wed, 25 Nov 2026 14:20:30 +0500 Subject: dstorage: rebuild a BC6H/BC7 request with a few lost blocks instead of failing it (BCPACK-LOST-BLOCK-LIMIT) A fitted BPTC request whose predictor chains reach a history entry the local profile does not know was refused once more than max(2, blocks/1024) blocks were affected, leaving the whole destination untouched. On the 1.0.2.1 package two BC7 requests fail this way at every boot, with one unknown endpoint entry each (5 of 1,536 and 15 of 768 blocks). bcpack::decode gets a separate limit for these lost blocks (lost_divisor, 1024 by default, as before) and an optional Report with the block and lost counts. DirectStorage passes 32 (LEONIDA_DSTORAGE_LOST_DIVISOR) and logs each approximated request. Inconsistent blocks keep the 1-in-1,024 limit; strict mode refuses any lost block. Co-Authored-By: Claude Opus 5.5 --- docs/directstorage.md | 12 +++++++++++- runtime/shims/dstorage.cpp | 32 +++++++++++++++++++++++++++++++- runtime/src/bcpack.cpp | 12 +++++++++--- runtime/src/bcpack.h | 13 ++++++++++++- runtime/test/dstorage_test.cpp | 18 ++++++++++++++++++ 5 files changed, 81 insertions(+), 6 deletions(-) (limited to 'runtime/src/bcpack.cpp') diff --git a/docs/directstorage.md b/docs/directstorage.md index 919d52282..e45cb55fa 100644 --- a/docs/directstorage.md +++ b/docs/directstorage.md @@ -174,7 +174,17 @@ The first boot with the executable settles most of this: the dry-run report show - `runtime/src/bcpack.cpp` ports the interleaved rANS and strict block rebuilders from `tools/bcpack.py` for BC1, BC3, BC4, BC5, BC6H and BC7. The predictor window holds 256 blocks. The build generates the lookup tables from the existing codec modules. Damaged streams and missing predictors or inconsistent blocks - exceeding the reference tolerance give `E_DSTORAGE_DECOMPRESSION_ERROR`. BC2 has no reference decoder. + exceeding the tolerance give `E_DSTORAGE_DECOMPRESSION_ERROR`. BC2 has no reference decoder. + In fitted mode with a profile, a BC6H/BC7 request may have up to `max(2, blocks / 32)` **lost** blocks: + blocks whose endpoint or index predictor chain starts at a history entry the profile does not know. They + are rebuilt from the bits the profile has, so their colours can be wrong, and each such request is logged + (`[rt] dstorage: BC7 request approximated N of M blocks ...`; the first eight, then at powers of two). + `LEONIDA_DSTORAGE_LOST_DIVISOR=N` sets the divisor; 1024 is the reference decoder's limit + (`_loose_limit` in `tools/bcpack.py`), which refused such requests outright and left the whole destination + untouched. On the 1.0.2.1 package two BC7 requests in `data/ttdb/props/octa_imposters.ttpf` failed at every + boot this way (one unknown endpoint entry each, 5 of 1,536 and 15 of 768 blocks lost with a locally built + profile); with the default they are published. Inconsistent blocks (anchor or p-bit) keep the 1-in-1,024 + limit, and strict mode (`LEONIDA_DSTORAGE_FITTED=0`) refuses any lost block as before. The runtime accepts the reference's fitted BC1/3/4/5 predictors for streamed art. `LEONIDA_DSTORAGE_FITTED=0` requires confirmed entries. Fitted blocks can differ from console output. The runtime stage of `make_game.py` generates `/shaders/bcpack/history.bcph` locally. diff --git a/runtime/shims/dstorage.cpp b/runtime/shims/dstorage.cpp index 7f4cef9d9..e035bb6ed 100644 --- a/runtime/shims/dstorage.cpp +++ b/runtime/shims/dstorage.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -1122,13 +1123,42 @@ private: char value[2]{}; return GetEnvironmentVariableA("LEONIDA_DSTORAGE_FAST_BCPACK", value, sizeof value) != 1 || value[0] != '0'; }(); + // LEONIDA_DSTORAGE_LOST_DIVISOR=N: a fitted BC6H/BC7 request may rebuild up to 1 in N of its blocks from + // a predictor the profile lacks (default 32; 1024 is the reference decoder's limit). Past it the request + // fails, as before. A few approximate blocks cost a few wrong 4x4 texels; a failed request leaves the + // whole destination untouched. + static const std::size_t lost_divisor = [] { + char value[16]{}; + const DWORD n = GetEnvironmentVariableA("LEONIDA_DSTORAGE_LOST_DIVISOR", value, sizeof value); + if (n == 0 || n >= sizeof value) return std::size_t(32); + char* end = nullptr; + const unsigned long v = std::strtoul(value, &end, 10); + if (end == value || *end || v < 1) { + log_once("lost-divisor", std::string("LEONIDA_DSTORAGE_LOST_DIVISOR=") + value + + " is not a positive number: using 32"); + return std::size_t(32); + } + return std::size_t(v); + }(); const char* failure = nullptr; - const auto quality = leonida::bcpack::decode(bc, data, scratch, allow_fitted, bc >= 6 ? bcpack_history() : nullptr, &failure, fast_entropy); + leonida::bcpack::Report report; + const auto quality = leonida::bcpack::decode(bc, data, scratch, allow_fitted, bc >= 6 ? bcpack_history() : nullptr, + &failure, fast_entropy, lost_divisor, &report); e.timing.bcpack = Timing::elapsed(begin); if (quality == leonida::bcpack::Quality::failed) { e.reason = failure; return E_DSTORAGE_DECOMPRESSION_ERROR; } if (quality == leonida::bcpack::Quality::fitted) { static const bool reported = (log_once("fitted", "using fitted BCPack predictors"), true); (void)reported; + if (report.lost) { + static std::atomic requests{0}, blocks{0}; + const auto n = requests.fetch_add(1) + 1; + const auto total = blocks.fetch_add(report.lost) + report.lost; + if (n <= 8 || (n & (n - 1)) == 0) // the first eight, then at powers of two + std::fprintf(stderr, "[rt] dstorage: BC%u request approximated %zu of %zu blocks from predictors the " + "profile lacks (%llu requests, %llu blocks so far; LEONIDA_DSTORAGE_LOST_DIVISOR=%zu)\n", + bc, report.lost, report.blocks, static_cast(n), + static_cast(total), lost_divisor); + } if (leonida::dstorage::trace_enabled()) std::fprintf(stderr, "[rt] ds-fitted bc=%u bytes=%u\n", bc, r.DestinationSize); } diff --git a/runtime/src/bcpack.cpp b/runtime/src/bcpack.cpp index bb4646787..8a6049d73 100644 --- a/runtime/src/bcpack.cpp +++ b/runtime/src/bcpack.cpp @@ -263,7 +263,7 @@ bool bc7(const Symbols &a, const Symbols &b, const BptcState &ep, // earlier uses of the slot. struct Coverage { array missing_ep{}, missing_ix{}; - size_t lost{}, inconsistent{}, limit{}; + size_t lost{}, inconsistent{}, limit{}, lost_limit{}; bool lost_endpoints{}, lost_indices{}; void note_missing(const BptcState &block) { const bool ep = block.ep_root >= 0 && missing_ep[block.ep_root]; @@ -382,7 +382,7 @@ bool rebuild_bptc(unsigned codec, size_t i, size_t blocks, const Symbols &a, } else if (i >= 256) coverage.note_missing(next); if (!valid) return false; - if (coverage.lost > coverage.limit) { + if (coverage.lost > coverage.lost_limit) { reason = coverage.lost_endpoints && coverage.lost_indices ? "missing-endpoint-and-index" : coverage.lost_endpoints ? "missing-endpoint" : "missing-index"; return false; @@ -394,7 +394,8 @@ bool rebuild_bptc(unsigned codec, size_t i, size_t blocks, const Symbols &a, Quality decode(unsigned codec, std::span source, std::span output, bool allow_fitted, - const Calibration *history, const char** failure, bool fast_entropy) { + const Calibration *history, const char** failure, bool fast_entropy, size_t lost_divisor, + Report *report) { const char* reason = "bcpack-stream"; if (failure) *failure = reason; Quality quality = Quality::confirmed; @@ -410,12 +411,17 @@ Quality decode(unsigned codec, std::span source, coverage.limit = allow_fitted && history ? max(2, output.size() / block_size / 1024) : 0; + coverage.lost_limit = allow_fitted && history + ? max(2, output.size() / block_size / max(1, lost_divisor)) + : 0; + if (report) *report = {output.size() / block_size, 0}; const bool valid = visit_blocks(codec, source, output.size(), [&](size_t i, const Symbols &first, const Symbols &tuple) { auto *dst = output.data() + i * block_size; if (codec >= 6) return rebuild_bptc(codec, i, output.size() / block_size, first, tuple, bptc, dst, history, allow_fitted, quality, coverage, reason); return rebuild_classic(codec, i, first, tuple, classic, dst, allow_fitted, quality); }, fast_entropy); + if (report) report->lost = coverage.lost; if (valid && (!coverage.inconsistent || quality == Quality::fitted)) return quality; if (failure) *failure = coverage.inconsistent && valid ? "anchor-or-pbit" : reason; return Quality::failed; diff --git a/runtime/src/bcpack.h b/runtime/src/bcpack.h index 9c34a074b..22890dd1a 100644 --- a/runtime/src/bcpack.h +++ b/runtime/src/bcpack.h @@ -1,6 +1,7 @@ // C++ BCPack decoder. Publish the caller's staged output only on success. #pragma once #include +#include #include #include @@ -26,8 +27,18 @@ struct Calibration { std::array bc7; }; static_assert(sizeof(Calibration) == 256 * (70 + 168)); +// What a BC6H/BC7 decode had to approximate: blocks whose endpoint or index predictor chain starts at a history +// entry the profile does not know ("lost"), out of all blocks of the request. +struct Report { + std::size_t blocks = 0, lost = 0; +}; + +// lost_divisor: in fitted mode with a profile, a BC6H/BC7 request may have up to max(2, blocks / lost_divisor) lost +// blocks; they are rebuilt from the bits the profile has, so their colours can be wrong. More than that fails as +// missing-endpoint / missing-index. 1024 is tools/bcpack.py's _loose_limit. Inconsistent blocks (anchor or p-bit) +// keep the 1-in-1,024 limit whatever this is. Quality decode(unsigned codec, std::span source, std::span output, bool allow_fitted = false, const Calibration *history = nullptr, const char** failure = nullptr, - bool fast_entropy = true); + bool fast_entropy = true, std::size_t lost_divisor = 1024, Report *report = nullptr); } // namespace leonida::bcpack diff --git a/runtime/test/dstorage_test.cpp b/runtime/test/dstorage_test.cpp index 4c1bb7d2f..f6c652977 100644 --- a/runtime/test/dstorage_test.cpp +++ b/runtime/test/dstorage_test.cpp @@ -1421,6 +1421,24 @@ void test_extended(Env& env) { check(leonida::bcpack::decode(codec, raw, result, false, &history) == leonida::bcpack::Quality::failed && leonida::bcpack::decode(codec, raw, result, true, &history) == leonida::bcpack::Quality::fitted, "partially confirmed BPTC profile requires fitted mode"); + // A history entry the profile does not know (no bits at all): every block whose predictor chain starts + // there is lost. The reference limit (1 in 1,024, at least 2) refuses the request; a lower divisor + // rebuilds it and reports how many blocks were approximated. Strict mode still refuses it. + auto unknown = history; + if (codec == 6) unknown.bc6[255].ep_class = 0; + else unknown.bc7[255].ep_bits.fill(0); + const char* why = nullptr; + leonida::bcpack::Report report; + check(leonida::bcpack::decode(codec, raw, result, true, &unknown, &why) == leonida::bcpack::Quality::failed && + why && std::string(why) == "missing-endpoint", + "an unknown BPTC predictor past the reference limit fails as missing-endpoint"); + check(leonida::bcpack::decode(codec, raw, result, true, &unknown, &why, true, 1, &report) == + leonida::bcpack::Quality::fitted && + report.blocks == result.size() / 16 && report.lost > 2 && report.lost <= report.blocks, + "a lower lost-block divisor rebuilds the request and reports the approximated blocks"); + check(leonida::bcpack::decode(codec, raw, result, false, &unknown, &why, true, 1, &report) == + leonida::bcpack::Quality::failed, + "strict decoding still refuses an unknown BPTC predictor"); } } std::vector fitted(512); -- cgit v1.2.3