Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion docs/directstorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,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 `<work>/shaders/bcpack/history.bcph` locally.
Expand Down
32 changes: 31 additions & 1 deletion runtime/shims/dstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <algorithm>
#include <atomic>
#include <cstdlib>
#include <chrono>
#include <map>
#include <condition_variable>
Expand Down Expand Up @@ -1279,13 +1280,42 @@ class Queue final : public IDStorageQueueX2 {
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<std::uint64_t> 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<unsigned long long>(n),
static_cast<unsigned long long>(total), lost_divisor);
}
if (leonida::dstorage::trace_enabled())
std::fprintf(stderr, "[rt] ds-fitted bc=%u bytes=%u\n", bc, r.DestinationSize);
}
Expand Down
12 changes: 9 additions & 3 deletions runtime/src/bcpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ bool bc7(const Symbols &a, const Symbols &b, const BptcState &ep,
// earlier uses of the slot.
struct Coverage {
array<bool, 256> 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];
Expand Down Expand Up @@ -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;
Expand All @@ -394,7 +394,8 @@ bool rebuild_bptc(unsigned codec, size_t i, size_t blocks, const Symbols &a,

Quality decode(unsigned codec, std::span<const uint8_t> source,
std::span<uint8_t> 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;
Expand All @@ -410,12 +411,17 @@ Quality decode(unsigned codec, std::span<const uint8_t> source,
coverage.limit = allow_fitted && history
? max<size_t>(2, output.size() / block_size / 1024)
: 0;
coverage.lost_limit = allow_fitted && history
? max<size_t>(2, output.size() / block_size / max<size_t>(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;
Expand Down
13 changes: 12 additions & 1 deletion runtime/src/bcpack.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// C++ BCPack decoder. Publish the caller's staged output only on success.
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include <span>

Expand All @@ -26,8 +27,18 @@ struct Calibration {
std::array<BC7, 256> 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<const std::uint8_t> source,
std::span<std::uint8_t> 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
18 changes: 18 additions & 0 deletions runtime/test/dstorage_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,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<uint8_t> fitted(512);
Expand Down
Loading