textures: decode bounded multi-lane BCPack rANS streams #607
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/613"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by Joacoorandom on 2026-11-22T15:50:41Z.
Change
Add a bounded, standard-library-only MDU rANS symbol decoder alongside the existing fixed-symbol prefix probe. Contributors can supply known lane widths and a symbol count to recover full per-lane symbol arrays, byte consumption and fragment/end-state diagnostics.
The command accepts 1–16 lanes with 1–9-bit symbols, at most 1,048,576 total output symbols and 2 MiB of input. It rejects malformed headers, truncated inputs, illegal terminal states, mismatched output shapes and non-zero trailing data.
The entropy engine is extracted from bcpack-rs by Ahmet Dedeler, with exact generated tables, source revision, original credits, dual-license files and Microsoft's MIT notice preserved. Most added lines are these vendored tables and reference code. The public wrapper, integration and additional controls are in this change.
One decoder extension restores at most two omitted low zero bytes when a flushed fragment exhausts its physical budget. Without it, the existing 16-lane synthetic packet from #442 stalls after consuming its input. The extension makes that packet reproduce all 32 expected symbols without extra physical reads; trimmed and untrimmed reverse-encoded controls also pass.
Refs #15, #442. This completes an entropy-layer step, not either issue's full BC7 block reconstruction goal. No other PR is required.
Verification
Commit:
6c905a22fc.Linux, Python 3.14.4:
Results:
Windows 11 build 26300.9974, Python 3.12.8, configured with MSVC 19.44.35230 and INTAKE_PYTHON pointing to that interpreter:
2/2 tests passed. These are Python tests; no GPU or full native build is claimed.
At the pinned bcpack-rs checkout:
The Rust vector regeneration comparison passed, confirming both vendored JSON vectors are current synthetic encoder outputs.
For the existing #442 packet, a separate local comparison against Leonida revision 6a6704df1592e1dc4a3215ddf1423a78adb52045's decode_lanes and strict decode_with_info matched the same symbols, all 104 input bytes and the known BC7 block with no provisional entries. No Leonida source is included in this change.
Evidence and follow-up
The two vendored vectors come unchanged from bcpack-rs's source-built Rust encoder; model tables come from Microsoft's silicon-rans revision 2f6827d. The third packet already exists in tests/bcpack_mode6_vector.py. No game or archive bytes are used.
Provenance and licenses and reproduction documentation accompany the README entry, CTest registration and Linux CI command.
BC1 endpoint/codebook reconstruction, BC7 lane-to-field mapping, archive stream discovery and rendered pixels remain follow-up work. The full repository suite was not run locally.
Checklist
AI-assisted implementation, test development and documentation.
Follow-up after independent verification (2026-11-22)
Commit
fa4a28cimproves robustness in response to independent review:Local reruns on Linux / Python 3.14:
python3 -B tests/bcpack_symbols_check.py -v10/10 passed, plusbcpack_check.py,bc7_tiles_check.py,bc7_blocks_check.py(2 optional skips),docs_links_check.py,bidi_controls_check.py, andgit diff --check. Original Windows results above are for6c905a2; current-head GitHub CI is the separate Windows revalidation.The strict home-state check applies when another fragment follows; there is no claim of an exhaustive corrupt-stream classifier. The original count-mismatch test can still fail through the generic no-progress path, and the input limit test does not process a complete 2 MiB valid stream.
Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T17:27:59Z.
Verification of
6c905a2on Windows 11 26100.9168 with Python 3.12.10. Your run used Linux and Python 3.14, andAGENTS.mdasks for 3.11 or newer. The head merges cleanly withmain(git merge-tree).python -B tests/bcpack_symbols_check.pypython -B tests/bcpack_check.pypython -B tests/bc7_tiles_check.pypython -B tests/bc7_blocks_check.pypython -B tests/docs_links_check.pypython -B tests/bidi_controls_check.pyI also checked the provenance claims in
tools/_bcpack_rans/README.md. I compared each vendored file with upstream bcpack-rs at5bf5cc56ed56f1ababf1f9592300bd182ff90bd8, fetched from GitHub, with line endings normalized:LICENSE-MITLICENSE-MITLICENSE-APACHELICENSE-APACHETHIRD_PARTY_NOTICES.mdTHIRD_PARTY_NOTICES.mdbcpack_tables.pypython/bcpack_tables.pyvectors/synthetic_11.jsontests/vectors/synthetic_11.jsonvectors/synthetic_12.jsontests/vectors/synthetic_12.json__init__.pypython/bcpack.pyup todef decode_symbolsThe 52 changed lines in
__init__.pyare all of these, and nothing else:sys.path/json/osimports were removed;TABLES_SHA256import was dropped;virtual_zerosslot and the restoration of at most two bytes on a flushed, exhausted fragment were added.The remaining decoder logic matches upstream, as the README states.
AI-assisted: run and compared with Claude Code; outputs copied from the local runs.
Imported from R0Y00/Xbox2Windows. Originally posted by thepatrickworld on 2026-11-22T18:33:21Z.
@Joacoorandom Independent run on
6c905a2, twice, plus coverage observations. Measurements only, no code change.Environment
out/pe-envvenv), pure standard library — no GPU involved, so the adapter is irrelevant here6c905a2.Commands
Each command was run twice, in separate processes, and the two logs were compared after normalising timings.
Observed — same results across two independent runs
bcpack_symbols_check: 8 test methods, 8 ok (all_models_and_alphabet_widths,argument_bounds,cli,narrowing_long_headers_and_flushed_fragments,published_table_checksum_and_inverse,reject_malformed_streams,rust_generated_streams,sixteen_lane_mode6_entropy).bcpack_check,bc7_tiles_check,bc7_blocks_check: all ok on both runs.0.700svs0.724s); no ordering, hashing or global-state dependence showed up.Merge state
mergeable: true, and all four check-runs are green (Windows/O2, Windows, gfx1013 fixture assembly, Linux Python checks). Theblockedstate GitHub shows is a missing review, not a conflict.The virtual-zero extension is grounded, not a patch to pass #442
I checked this specifically because it is the kind of change that can be a quiet workaround.
src/encode.rsat5bf5cc56: "Bytes emitted while encoding the last symbol from x = L are zeros that the decoder never needs to read: drop up to two"). So restoring them is the inverse of a documented encoder behaviour.while 0 < x < L and ln.virtual_zeros < 2: x <<= 8) touches onlyx. No byte is read:pos,nbytesandconsumedstay intact, and I confirmed full consumption on the fixtures (104/104, 515/515). In an interleaved shared stream, a real over-read would eat another lane's byte and desynchronise everything; this is structurally incapable of that.6a6704d) does the same virtual-zero fill on flush, which corroborates the mechanism as a format property.What is not settled: that implementation caps at a different bound and fails explicitly when the fragment does not return to its home state (
fragment did not end at its home state). Here, anyxin(0, L)is absorbed and the FSM drains into the next header. So nothing distinguishes a legitimately trimmed fragment from a corrupted one, and there is no negative control for a flush fragment short by more than two bytes.ln.flushdoes not discriminate either — upstream sets it unconditionally, and the block fires on untrimmed vectors too.Bounds: exact for lanes and widths, only "beyond" for symbols and input size
symbols <= MAX_SYMBOLS // len(widths), so with two lanes the effective bound is 524 288. The only rejection case passesMAX_SYMBOLS(1 048 576), i.e. twice the effective bound, and no positive case asserts the exact limit for one lane or for sixteen.MAX_INPUT + 1is tested; the exact 2 097 152 bound is never asserted and no valid 2 MiB stream is decoded.Rejections: three messages checked, the rest bare
assertRaisesRegexcoverstrailing,alphabetandillegal, and those do reach the intended paths. Everything else is a bareassertRaises, and all truncations plus the wrong-count case raise the same genericno progress (stream truncated or not BCPack BC1) at byte N. The wrong-count control therefore passes by coincidence: the shape guard is never reached,decoderaises first.Provenance of the 9,168 symbols
vectors/synthetic_{11,12}.jsonare byte-identical to upstream5bf5cc56and were produced by that repo's own Rust encoder. That is not tautological in the strict sense (Rust encoder vs vendored Python decoder), but it is a round-trip within one project on the same Microsoft tables — no retail stream, no hardware capture. The genuinely external anchor is the #442 packet, and it holds: Leonida's independent decoder reproduces all 32 symbols withused=104. That anchor is asserted in a comment rather than replayed by a test.Licence form
LICENSE-APACHEis the 15-line notice, not the full Apache-2.0 text that §4(a) asks you to ship with a copy of the work, and the vendored file header gives the source URL without the pinned revision (the revision only appears in the README). Everything else is clean: only__init__.pydiffers from upstream, and the modification is declared in three places (docstring, README,docs/experiments.md).AI-assisted; runs executed locally, twice per suite. The coverage observations come from a separate code review that diffed against the pinned upstream, ran the decoder with and without the extension, and cross-checked with an independent implementation; the two load-bearing claims (effective symbol bound, no byte consumption) were re-verified by me before posting.
Imported from R0Y00/Xbox2Windows. Originally posted by Joacoorandom on 2026-11-22T18:41:23Z.
Thanks for the independent Windows runs and the detailed coverage/format review. I followed up in
fa4a28cwith three focused changes: (1) intermediate flushed fragments must reach the rANS home state before switching headers; a new two-fragment negative control rejects 2–4 omitted bytes while preserving a legitimate single-zero trim, (2) exact 1/2/16-lane symbol-count limits and the exact 2 MiB input boundary are tested with a sentinel backend (no claim of actually decoding a valid 2 MiB stream), and (3) the complete Apache-2.0 license and pinned vendored revision are included. All 10 entropy test methods and the existing BCPack/BC7/docs checks passed locally; the Linux CI jobs on this head are green and Windows CI is still running. The #442 packet is directly replayed intest_sixteen_lane_mode6_entropy, checking all 32 symbols and full byte consumption. I have not changed the generic wrong-count no-progress diagnostic and do not claim exhaustive malformed-stream classification. Thanks for pinpointing the gaps.Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T18:45:22Z.
Re-checked the new head
fa4a28con Windows 11 26100.9168 with Python 3.12.10. It merges cleanly withmain.tests/bcpack_symbols_check.pytests/bcpack_check.py,tests/bc7_tiles_check.pytests/bc7_blocks_check.pytests/docs_links_check.py,tests/bidi_controls_check.pyOn provenance, correcting my earlier table for one file.
LICENSE-APACHEis no longer byte-identical to upstream's short notice. It now holds the full license text, which the README states. It matches the official text at https://www.apache.org/licenses/LICENSE-2.0.txt, ignoring whitespace.LICENSE-MIT,THIRD_PARTY_NOTICES.mdandbcpack_tables.pyare still identical to bcpack-rs5bf5cc56.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by JB2WIN on 2026-11-22T19:13:34Z.
Independent Windows run of this PR on physical hardware:
fa4a28c(PR head), built from sourcectest --preset dev --no-tests=error -R '^(bcpack|bc7|texture|resource_range)' -V: 9/9 passed:bcpack_prefix_and_mip_tail,bcpack_rans_symbols,bc7_block_reference,bc7_tile_swizzle,resource_range_controls,texture_mips_{hardware,warp}andbc7_draw_{hardware,warp}. No skips, and the debug layer reporteddebug_errors=0on all 28 draws. I did not run any mutations.AI-assisted.