diff options
| author | Moriz82 <72836413+Moriz82@users.noreply.github.com> | 2026-11-24 11:42:12 -0600 |
|---|---|---|
| committer | Moriz82 <72836413+Moriz82@users.noreply.github.com> | 2026-11-24 11:42:12 -0600 |
| commit | 273b3bdf9d6e736442ad584ad464df6e86ee928b (patch) | |
| tree | 3886a0af7c74ffa7d9415d662d8dc0fb708f1c08 /runtime/src/bcpack.cpp | |
| parent | ece4ba5b31ebc6f345c8e29eae1261f2f780057a (diff) | |
[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 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/bcpack.cpp')
| -rw-r--r-- | runtime/src/bcpack.cpp | 423 |
1 files changed, 423 insertions, 0 deletions
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 @@ | |||
| 1 | // C++ port of tools/bcpack.py. The build derives the model and BCn format | ||
| 2 | // constants from that same reference. | ||
| 3 | #include "bcpack.h" | ||
| 4 | #include "bcpack_stream.h" | ||
| 5 | #include <algorithm> | ||
| 6 | #include <array> | ||
| 7 | #include <bit> | ||
| 8 | #include <cstring> | ||
| 9 | |||
| 10 | namespace leonida::bcpack { | ||
| 11 | namespace { | ||
| 12 | using namespace detail; | ||
| 13 | |||
| 14 | void color(uint8_t *dst, const int *e, int a, int b, int c, int d) { | ||
| 15 | for (int i = 0; i < 2; ++i, e += 3) { | ||
| 16 | const int rgb = (e[0] << 11) | (e[1] << 5) | e[2]; | ||
| 17 | dst[i * 2] = uint8_t(rgb); | ||
| 18 | dst[i * 2 + 1] = uint8_t(rgb >> 8); | ||
| 19 | } | ||
| 20 | dst[4] = uint8_t((a & 15) | (b << 4)); | ||
| 21 | dst[5] = uint8_t((a >> 4) | (b & 240)); | ||
| 22 | dst[6] = uint8_t((c & 15) | (d << 4)); | ||
| 23 | dst[7] = uint8_t((c >> 4) | (d & 240)); | ||
| 24 | } | ||
| 25 | |||
| 26 | struct Endpoints { | ||
| 27 | int v[8]{}; | ||
| 28 | }; | ||
| 29 | |||
| 30 | bool rebuild_classic(unsigned codec, size_t i, const Symbols &a, | ||
| 31 | const Symbols &b, array<Endpoints, 256> &past, | ||
| 32 | uint8_t *dst, bool allow_fitted, Quality &quality) { | ||
| 33 | const bool single = codec == 1 || codec == 4; | ||
| 34 | if (a[1] || b[1]) | ||
| 35 | return false; | ||
| 36 | const unsigned distance = unzig(single ? a[0] : interleave(a[0], b[0])) & 255; | ||
| 37 | Endpoints e; | ||
| 38 | const int count = codec == 1 ? 6 : codec == 3 ? 8 : codec == 4 ? 2 : 4; | ||
| 39 | if (!distance) { | ||
| 40 | if (codec != 1) | ||
| 41 | fill_n(e.v, codec == 5 ? 4 : 2, 128); | ||
| 42 | } else if (distance <= i) { | ||
| 43 | e = past[(i - distance) & 255]; | ||
| 44 | } else { | ||
| 45 | const unsigned slot = (i - distance) & 255; | ||
| 46 | const Seed &seed = codec == 1 ? history1[slot] | ||
| 47 | : codec == 3 ? history3[slot] | ||
| 48 | : codec == 4 ? history4[slot] | ||
| 49 | : history5[slot]; | ||
| 50 | if (seed.values[0] < 0 || (seed.fitted && !allow_fitted)) | ||
| 51 | return false; | ||
| 52 | if (seed.fitted) | ||
| 53 | quality = Quality::fitted; | ||
| 54 | copy_n(seed.values, count, e.v); | ||
| 55 | } | ||
| 56 | const int alpha = codec == 1 ? 0 : codec == 5 ? 4 : 2; | ||
| 57 | for (int k = 0; k < count; ++k) { | ||
| 58 | const int coord = k - alpha; | ||
| 59 | const int mask = coord < 0 ? 255 : coord % 3 == 1 ? 63 : 31; | ||
| 60 | const int symbol = single ? a[2 + k] | ||
| 61 | : coord < 0 ? (k % 2 ? b : a)[2 + k / 2] | ||
| 62 | : (coord < 3 ? a : b)[7 + coord % 3]; | ||
| 63 | e.v[k] = (e.v[k] + unzig(symbol)) & mask; | ||
| 64 | } | ||
| 65 | past[i & 255] = e; | ||
| 66 | for (int ch = 0; ch < alpha / 2; ++ch) { | ||
| 67 | const auto *pairs = codec == 3 ? pair3 : codec == 4 ? pair4 : pair5; | ||
| 68 | uint64_t bits = 0; | ||
| 69 | for (int row = 0; row < 4; ++row) { | ||
| 70 | const int lane = | ||
| 71 | codec == 4 ? 4 + 2 * row : (codec == 3 ? 3 : 4 + 4 * ch) + row; | ||
| 72 | const int right = single ? a[lane + 1] : b[lane]; | ||
| 73 | bits |= uint64_t(pairs[a[lane]] | (pairs[right] << 6)) << (12 * row); | ||
| 74 | } | ||
| 75 | *dst++ = uint8_t(e.v[ch * 2]); | ||
| 76 | *dst++ = uint8_t(e.v[ch * 2 + 1]); | ||
| 77 | for (int j = 0; j < 6; ++j) | ||
| 78 | *dst++ = uint8_t(bits >> (8 * j)); | ||
| 79 | } | ||
| 80 | if (codec == 1) | ||
| 81 | color(dst, e.v, quad1[a[8]], quad1[a[9]], quad1[a[10]], quad1[a[11]]); | ||
| 82 | if (codec == 3) | ||
| 83 | color(dst, e.v + 2, quad3[a[10]], quad3[b[10]], quad3[a[11]], quad3[b[11]]); | ||
| 84 | return true; | ||
| 85 | } | ||
| 86 | |||
| 87 | struct BitWriter { | ||
| 88 | uint8_t *dst; | ||
| 89 | int pos{}, violations{}; | ||
| 90 | void put(int value, int bits) { | ||
| 91 | while (bits > 0) { | ||
| 92 | const int n = min(bits, 8 - pos % 8); | ||
| 93 | dst[pos / 8] |= uint8_t((value & ((1 << n) - 1)) << (pos % 8)); | ||
| 94 | value >>= n; | ||
| 95 | bits -= n; | ||
| 96 | pos += n; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | }; | ||
| 100 | |||
| 101 | // Endpoints and indices have independent predictors, with distances limited to | ||
| 102 | // eight bits. | ||
| 103 | struct BptcState { | ||
| 104 | array<int, 24> ep{}; | ||
| 105 | array<int, 32> ix{}; | ||
| 106 | int ep_root = -1, ix_root = -1; | ||
| 107 | }; | ||
| 108 | int mod_bits(int v, int n) { return v & ((1 << n) - 1); } | ||
| 109 | |||
| 110 | void index_row(int a, int b, int row, int bits, const int *ref, int *value, | ||
| 111 | int *history) { | ||
| 112 | for (int x = 0; x < 4; ++x) { | ||
| 113 | const int s = (x < 2 ? a : b) >> ((x % 2) ^ (row < 3)); | ||
| 114 | const int delta = | ||
| 115 | (s & 1) | ((s >> 1) & 2) | ((s >> 2) & 4) | ((s >> 3) & 8); | ||
| 116 | const int p = 4 * row + x; | ||
| 117 | value[p] = mod_bits((ref[p] >> (4 - bits)) + unzig(delta), bits); | ||
| 118 | history[p] = value[p] << (4 - bits); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | bool put_indices(BitWriter &writer, const int *values, int bits, | ||
| 123 | int anchor1 = -1, int anchor2 = -1) { | ||
| 124 | for (int p = 0; p < 16; ++p) { | ||
| 125 | const int n = bits - (p == 0 || p == anchor1 || p == anchor2); | ||
| 126 | if (values[p] >> n) | ||
| 127 | ++writer.violations; | ||
| 128 | writer.put(values[p], n); | ||
| 129 | } | ||
| 130 | return true; | ||
| 131 | } | ||
| 132 | |||
| 133 | bool bc6(const Symbols &a, const Symbols &b, const BptcState &ep, | ||
| 134 | const BptcState &ix, BptcState &next, BitWriter &writer) { | ||
| 135 | if (a[15] >= 14) | ||
| 136 | return false; | ||
| 137 | const auto &m = bc6Modes[a[15]]; | ||
| 138 | const int partition = min<int>(b[15], 31), base_shift = 16 - m.base; | ||
| 139 | int fields[3][4]{}; | ||
| 140 | for (int c = 0; c < 3; ++c) { | ||
| 141 | const int n = m.delta[c], shift = max(0, 6 - n); | ||
| 142 | const auto *ref = &ep.ep[c * 4]; | ||
| 143 | const int w = mod_bits((ref[0] >> base_shift) + | ||
| 144 | unzig(interleave(a[2 + c], b[2 + c], 8)), | ||
| 145 | m.base); | ||
| 146 | const int x = mod_bits( | ||
| 147 | (ref[1] >> (10 - n)) + unzig(interleave(a[5 + c], b[5 + c], 5)), n); | ||
| 148 | const int y = mod_bits((ref[2] >> shift) + unzig(a[8 + c]), n); | ||
| 149 | const int z = mod_bits((ref[3] >> shift) + unzig(b[8 + c]), n); | ||
| 150 | auto *state = &next.ep[c * 4]; | ||
| 151 | state[0] = w << base_shift; | ||
| 152 | state[1] = x << (10 - n); | ||
| 153 | if (m.regions == 2) { | ||
| 154 | state[2] = y << shift; | ||
| 155 | state[3] = z << shift; | ||
| 156 | } else { | ||
| 157 | const int v = mod_bits(w, n) ^ (m.transformed << (n - 1)); | ||
| 158 | state[2] = n >= 6 ? v >> (n - 6) : v << shift; | ||
| 159 | state[3] = state[1] >> 4; | ||
| 160 | } | ||
| 161 | fields[c][0] = w; | ||
| 162 | if (a[15] >= 12) { | ||
| 163 | fields[c][0] &= 1023; | ||
| 164 | for (int bit = 10; bit < m.base; ++bit) | ||
| 165 | fields[c][0] |= ((w >> bit) & 1) << (m.base + 9 - bit); | ||
| 166 | } | ||
| 167 | fields[c][1] = x; | ||
| 168 | fields[c][2] = y; | ||
| 169 | fields[c][3] = z; | ||
| 170 | } | ||
| 171 | writer.put(m.code, m.code < 2 ? 2 : 5); | ||
| 172 | for (int j = 0; j < m.count; ++j) { | ||
| 173 | const auto &f = bc6Plan[m.first + j]; | ||
| 174 | writer.put((f[0] < 0 ? partition : fields[f[0]][f[1]]) >> f[2], 1); | ||
| 175 | } | ||
| 176 | int values[16]; | ||
| 177 | const int bits = m.regions == 2 ? 3 : 4; | ||
| 178 | for (int y = 0; y < 4; ++y) | ||
| 179 | index_row(a[11 + y], b[11 + y], y, bits, ix.ix.data(), values, | ||
| 180 | next.ix.data()); | ||
| 181 | return put_indices(writer, values, bits, | ||
| 182 | m.regions == 2 ? anchor2[partition] : -1); | ||
| 183 | } | ||
| 184 | |||
| 185 | bool bc7(const Symbols &a, const Symbols &b, const BptcState &ep, | ||
| 186 | const BptcState &ix, BptcState &next, BitWriter &writer) { | ||
| 187 | if (a[15] >= 32 || b[15] >= 16) | ||
| 188 | return false; | ||
| 189 | const auto &cfg = bc7Config[a[15] * 16 + b[15]]; | ||
| 190 | const auto [tag, part, rot, isel] = cfg; | ||
| 191 | const int mode = tag & 127; | ||
| 192 | if (mode > 7 || part >= 64) | ||
| 193 | return false; | ||
| 194 | const auto &f = bc7_formats[mode]; | ||
| 195 | int widths[4] = {f.color, f.color, f.color, f.alpha}; | ||
| 196 | if (rot) | ||
| 197 | swap(widths[rot - 1], widths[3]); | ||
| 198 | constexpr int lane[3][4] = {{2, 3, 4, 5}, {6, 7, 8, 9}, {10, 5, 9, -1}}; | ||
| 199 | int v[3][2][4]{}; | ||
| 200 | for (int sub = 0; sub < f.subsets; ++sub) { | ||
| 201 | for (int end = 0; end < 2; ++end) { | ||
| 202 | for (int c = 0; c < 4; ++c) { | ||
| 203 | if (!widths[c] || lane[sub][c] < 0) | ||
| 204 | continue; | ||
| 205 | const int n = widths[c] + (f.pbits && c == 0); | ||
| 206 | const int k = 8 * sub + 4 * end + c; | ||
| 207 | v[sub][end][c] = mod_bits( | ||
| 208 | (ep.ep[k] >> (8 - n)) + unzig((end ? b : a)[lane[sub][c]]), n); | ||
| 209 | next.ep[k] = v[sub][end][c] << (8 - n); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | if (f.pbits == 2 && ((v[sub][0][0] ^ v[sub][1][0]) & 1)) | ||
| 213 | ++writer.violations; | ||
| 214 | } | ||
| 215 | for (int sub = f.subsets; sub < 3; ++sub) | ||
| 216 | copy_n(next.ep.begin(), 8, next.ep.begin() + 8 * sub); | ||
| 217 | int primary[16], secondary[16]; | ||
| 218 | constexpr int second_rows[] = {6, 7, 8, 10}; | ||
| 219 | const bool two = mode == 4 || mode == 5; | ||
| 220 | for (int y = 0; y < 4; ++y) { | ||
| 221 | index_row(a[11 + y], b[11 + y], y, f.index, ix.ix.data(), primary, | ||
| 222 | next.ix.data()); | ||
| 223 | if (two) | ||
| 224 | index_row(a[second_rows[y]], b[second_rows[y]], y, 2, ix.ix.data() + 16, | ||
| 225 | secondary, next.ix.data() + 16); | ||
| 226 | } | ||
| 227 | if (mode == 0 && part == 0 && | ||
| 228 | ranges::all_of(next.ep, [](int v) { return !v; }) && | ||
| 229 | ranges::all_of(primary, [](int v) { return !v; })) | ||
| 230 | return true; | ||
| 231 | |||
| 232 | writer.put(1 << mode, mode + 1); | ||
| 233 | writer.put(part, f.partition); | ||
| 234 | if (two) | ||
| 235 | writer.put(rot, 2); | ||
| 236 | if (mode == 4) | ||
| 237 | writer.put(isel, 1); | ||
| 238 | for (int c = 0; c < 4; ++c) { | ||
| 239 | const int src = rot && c == rot - 1 ? 3 : rot && c == 3 ? rot - 1 : c; | ||
| 240 | for (int sub = 0; sub < f.subsets; ++sub) | ||
| 241 | for (int end = 0; end < 2; ++end) | ||
| 242 | writer.put(v[sub][end][src] >> (f.pbits && src == 0), | ||
| 243 | c == 3 ? f.alpha : f.color); | ||
| 244 | } | ||
| 245 | if (f.pbits) { | ||
| 246 | for (int sub = 0; sub < f.subsets; ++sub) | ||
| 247 | for (int end = 0; end < (f.pbits == 1 ? 2 : 1); ++end) | ||
| 248 | writer.put(v[sub][end][0], 1); | ||
| 249 | } | ||
| 250 | const int anc1 = f.subsets == 2 ? anchor2[part] | ||
| 251 | : f.subsets == 3 ? anchor3a[part] | ||
| 252 | : -1; | ||
| 253 | const int anc2 = f.subsets == 3 ? anchor3b[part] : -1; | ||
| 254 | if (mode == 4) | ||
| 255 | return put_indices(writer, secondary, 2) && | ||
| 256 | put_indices(writer, primary, f.index); | ||
| 257 | return put_indices(writer, primary, f.index, anc1, anc2) && | ||
| 258 | (!two || put_indices(writer, secondary, 2)); | ||
| 259 | } | ||
| 260 | |||
| 261 | // A predictor can influence later blocks through either independent reference | ||
| 262 | // chain. A bit-depth gap discovered during the first 256 blocks also affects | ||
| 263 | // earlier uses of the slot. | ||
| 264 | struct Coverage { | ||
| 265 | array<bool, 256> missing_ep{}, missing_ix{}; | ||
| 266 | size_t lost{}, inconsistent{}, limit{}; | ||
| 267 | bool lost_endpoints{}, lost_indices{}; | ||
| 268 | void note_missing(const BptcState &block) { | ||
| 269 | const bool ep = block.ep_root >= 0 && missing_ep[block.ep_root]; | ||
| 270 | const bool ix = block.ix_root >= 0 && missing_ix[block.ix_root]; | ||
| 271 | lost += ep || ix; | ||
| 272 | lost_endpoints |= ep; | ||
| 273 | lost_indices |= ix; | ||
| 274 | } | ||
| 275 | }; | ||
| 276 | |||
| 277 | bool rebuild_bptc(unsigned codec, size_t i, size_t blocks, const Symbols &a, | ||
| 278 | const Symbols &b, array<BptcState, 256> &past, uint8_t *dest, | ||
| 279 | const Calibration *history, bool allow_fitted, | ||
| 280 | Quality &quality, Coverage &coverage, const char*& reason) { | ||
| 281 | const unsigned di = interleave(a[1], b[1]); | ||
| 282 | const unsigned de = (di + unzig(interleave(a[0], b[0]))) & 255; | ||
| 283 | if ((di > i || de > i) && !history) { | ||
| 284 | reason = "missing-profile"; | ||
| 285 | return false; | ||
| 286 | } | ||
| 287 | const int islot = di > i ? int(256 + i - di) : -1; | ||
| 288 | const int eslot = de > i ? int(256 + i - de) : -1; | ||
| 289 | bool fitted = false; | ||
| 290 | BptcState zero{}; | ||
| 291 | if (codec == 6) { | ||
| 292 | for (int c = 0; c < 3; ++c) { | ||
| 293 | zero.ep[4 * c] = 32768; | ||
| 294 | zero.ep[4 * c + 1] = 512; | ||
| 295 | zero.ep[4 * c + 2] = zero.ep[4 * c + 3] = 32; | ||
| 296 | } | ||
| 297 | zero.ix.fill(8); | ||
| 298 | } | ||
| 299 | if (codec == 6) { | ||
| 300 | if (a[15] >= 14) | ||
| 301 | return false; | ||
| 302 | const auto &m = bc6Modes[a[15]]; | ||
| 303 | if (eslot >= 0) { | ||
| 304 | const auto &row = history->bc6[eslot]; | ||
| 305 | for (int c = 0; c < 3; ++c) { | ||
| 306 | const int needs[] = {m.base, m.delta[c], | ||
| 307 | m.regions == 2 ? m.delta[c] : 0, | ||
| 308 | m.regions == 2 ? m.delta[c] : 0}; | ||
| 309 | for (int k = 0; k < 4; ++k) { | ||
| 310 | const int j = 4 * c + k; | ||
| 311 | zero.ep[j] = row.ep[2 * j] | (row.ep[2 * j + 1] << 8); | ||
| 312 | fitted |= row.ep_confirmed[j] < needs[k]; | ||
| 313 | } | ||
| 314 | } | ||
| 315 | fitted |= row.ep_class != 3; | ||
| 316 | coverage.missing_ep[eslot] |= !row.ep_class; | ||
| 317 | } | ||
| 318 | if (islot >= 0) { | ||
| 319 | const auto &row = history->bc6[islot]; | ||
| 320 | copy(row.ix.begin(), row.ix.end(), zero.ix.begin()); | ||
| 321 | fitted |= | ||
| 322 | row.ix_class != 3 || ranges::any_of(row.ix_confirmed, [&](int n) { | ||
| 323 | return n < (m.regions == 2 ? 3 : 4); | ||
| 324 | }); | ||
| 325 | coverage.missing_ix[islot] |= !row.ix_class; | ||
| 326 | } | ||
| 327 | } else { | ||
| 328 | if (a[15] >= 32 || b[15] >= 16) | ||
| 329 | return false; | ||
| 330 | const auto &cfg = bc7Config[a[15] * 16 + b[15]]; | ||
| 331 | const unsigned mode = cfg[0] & 127; | ||
| 332 | if (mode >= 8 || cfg[1] >= 64) | ||
| 333 | return false; | ||
| 334 | if (cfg[0] & 128) | ||
| 335 | fitted = true; | ||
| 336 | const auto &format = bc7_formats[mode]; | ||
| 337 | if (eslot >= 0) { | ||
| 338 | const auto &row = history->bc7[eslot]; | ||
| 339 | copy(row.ep.begin(), row.ep.end(), zero.ep.begin()); | ||
| 340 | int width[] = {format.color, format.color, format.color, format.alpha}; | ||
| 341 | if (cfg[2]) | ||
| 342 | swap(width[3], width[cfg[2] - 1]); | ||
| 343 | width[0] += format.pbits != 0; | ||
| 344 | for (int j = 0; j < 8 * format.subsets; ++j) { | ||
| 345 | const int need = width[j % 4]; | ||
| 346 | fitted |= row.ep_confirmed[j] < need || row.ep_bits[j] < need; | ||
| 347 | coverage.missing_ep[eslot] |= row.ep_bits[j] < min(need, 4); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | if (islot >= 0) { | ||
| 351 | const auto &row = history->bc7[islot]; | ||
| 352 | copy(row.ix.begin(), row.ix.end(), zero.ix.begin()); | ||
| 353 | for (int j = 0; j < (mode == 4 || mode == 5 ? 32 : 16); ++j) { | ||
| 354 | int need = j < 16 ? format.index : 2; | ||
| 355 | fitted |= row.ix_confirmed[j] < need || row.ix_bits[j] < need; | ||
| 356 | coverage.missing_ix[islot] |= row.ix_bits[j] < need; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | } | ||
| 360 | if (fitted) { | ||
| 361 | if (!allow_fitted) { | ||
| 362 | reason = "unconfirmed-predictor"; | ||
| 363 | return false; | ||
| 364 | } | ||
| 365 | quality = Quality::fitted; | ||
| 366 | } | ||
| 367 | const auto &ep = de && eslot < 0 ? past[(i - de) & 255] : zero; | ||
| 368 | const auto &ix = di && islot < 0 ? past[(i - di) & 255] : zero; | ||
| 369 | auto &next = past[i & 255]; | ||
| 370 | next = {}; | ||
| 371 | next.ep_root = eslot >= 0 ? eslot : ep.ep_root; | ||
| 372 | next.ix_root = islot >= 0 ? islot : ix.ix_root; | ||
| 373 | fill_n(dest, 16, 0); | ||
| 374 | BitWriter writer{dest}; | ||
| 375 | const bool valid = codec == 6 ? bc6(a, b, ep, ix, next, writer) | ||
| 376 | : bc7(a, b, ep, ix, next, writer); | ||
| 377 | coverage.inconsistent += | ||
| 378 | codec == 6 ? writer.violations : int(writer.violations != 0); | ||
| 379 | if (i + 1 == min<size_t>(256, blocks)) { | ||
| 380 | for (size_t k = 0; k <= i; ++k) | ||
| 381 | coverage.note_missing(past[k]); | ||
| 382 | } else if (i >= 256) | ||
| 383 | coverage.note_missing(next); | ||
| 384 | if (!valid) return false; | ||
| 385 | if (coverage.lost > coverage.limit) { | ||
| 386 | reason = coverage.lost_endpoints && coverage.lost_indices ? "missing-endpoint-and-index" : | ||
| 387 | coverage.lost_endpoints ? "missing-endpoint" : "missing-index"; | ||
| 388 | return false; | ||
| 389 | } | ||
| 390 | if (coverage.inconsistent > coverage.limit) { reason = "anchor-or-pbit"; return false; } | ||
| 391 | return true; | ||
| 392 | } | ||
| 393 | } // namespace | ||
| 394 | |||
| 395 | Quality decode(unsigned codec, std::span<const uint8_t> source, | ||
| 396 | std::span<uint8_t> output, bool allow_fitted, | ||
| 397 | const Calibration *history, const char** failure, bool fast_entropy) { | ||
| 398 | const char* reason = "bcpack-stream"; | ||
| 399 | if (failure) *failure = reason; | ||
| 400 | Quality quality = Quality::confirmed; | ||
| 401 | if (!codec || codec == 2 || codec > 7) | ||
| 402 | return Quality::failed; | ||
| 403 | const bool single = codec == 1 || codec == 4; | ||
| 404 | const unsigned block_size = single ? 8 : 16; | ||
| 405 | if (output.size() % block_size) | ||
| 406 | return Quality::failed; | ||
| 407 | array<Endpoints, 256> classic{}; | ||
| 408 | array<BptcState, 256> bptc{}; | ||
| 409 | Coverage coverage{}; | ||
| 410 | coverage.limit = allow_fitted && history | ||
| 411 | ? max<size_t>(2, output.size() / block_size / 1024) | ||
| 412 | : 0; | ||
| 413 | const bool valid = visit_blocks(codec, source, output.size(), [&](size_t i, const Symbols &first, const Symbols &tuple) { | ||
| 414 | auto *dst = output.data() + i * block_size; | ||
| 415 | if (codec >= 6) | ||
| 416 | return rebuild_bptc(codec, i, output.size() / block_size, first, tuple, bptc, dst, history, allow_fitted, quality, coverage, reason); | ||
| 417 | return rebuild_classic(codec, i, first, tuple, classic, dst, allow_fitted, quality); | ||
| 418 | }, fast_entropy); | ||
| 419 | if (valid && (!coverage.inconsistent || quality == Quality::fitted)) return quality; | ||
| 420 | if (failure) *failure = coverage.inconsistent && valid ? "anchor-or-pbit" : reason; | ||
| 421 | return Quality::failed; | ||
| 422 | } | ||
| 423 | } // namespace leonida::bcpack | ||