Skip to content

fix(bcpack): standalone BC1-BC7 decoding across runtimes and assets - #2567

Open
wish131400 wants to merge 2 commits into
ProjectLeonida:mainfrom
wish131400:bcpack/standalone-correctness
Open

wish131400 wants to merge 2 commits into
ProjectLeonida:mainfrom
wish131400:bcpack/standalone-correctness

Conversation

@wish131400

@wish131400 wish131400 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: BCPACK-STANDALONE-BC1-BC7.

Depends on: no unmerged PRs; initially based on main (493867366), now updated by merging main at 4a54aedcb without rewriting the published decoder commit. Overlapping/conflicting proposals are listed below and in docs/bcpack-standalone.md for maintainer coordination.

What and why

The Rust decoder reads BC7 dictionary indices at the wrong offset after entry zero, and its lane scheduler rejects valid BC1/BC4 streams. The legacy C++/Python production decoders have incomplete or fitted initial dictionaries, and all three paths omit BC2. On an independent 15,402-stream synthetic audit, the original Rust path produced 514 different BC7 outputs among 774 BC7 cases; the legacy paths rejected most dictionary-dependent cases.

This change provides fixed-dictionary BC1–BC7 decoding in the D3D12 Rust runtime, C++ DirectStorage shim and Python assets exporter:

  • Fix the Rust BC7 record parser and lane scheduling/output budgets, and implement BC2's 15-lane reconstruction.
  • Vendor the standalone C++ decoder from Xbox2Windows d049001ab9a906588f47868277b77e9a00e27053 (#3293), with preserved source/license notices and C++20 portability. Check CPU/OS support before AVX2 dispatch; retain scalar decoding.
  • Port the same reconstruction and fixed source constants into Python. Optional native acceleration is used only for entropy decoding, with Python fallback.
  • Remove automatic predictor fitting and history/calibration loading from the normal runtime/assets build. Isolate existing explicit offline research tools in bcpack_research.py; they remain usable and tested. Legacy quality/history arguments are accepted for compatibility and cannot change production output.
  • Add 1,076 independent synthetic goldens shared by Rust, C++ and Python, and use independent expected bytes in DirectStorage request tests. Keep staged publication/error handling and regenerate assets when decoder inputs change.

Provenance is explicit: development included implementation inspection and synthetic GDK API queries, so this is not presented as a strict clean-room implementation. No SDK DLLs, private headers, disassembly, game payloads or title-derived fixtures/logs are included. The shipped decoder and tests need neither GDK nor game files. The GDK was only a local oracle for synthetic inputs.

Existing PR overlap

This is a replacement proposal based on main, not a stack on the earlier decoder series:

These overlaps require maintainer coordination; no claim is made that those unmerged changes were applied or that their separate hardening/performance work has been completed by this PR.

Tested

Windows x64, MSVC 19.43; the integration checks below were rerun after merging main 4a54aedcb (head cb947eac0):

  • Local independent GDK comparison: 15,402 synthetic streams per implementation, all exact in C++/Rust/Python; 168 GDK compressor/decompressor synthetic roundtrips, all exact. Expected bytes are independent of the replacement decoders.
  • Committed fixture: 1,076 goldens. C++ CMake/CTest passes them on every supported reference/scalar/AVX2 path, exhausts all non-reserved slots of all 128 entropy models, and checks canaries/malformed input. Python checks pure Python and optional native entropy with fallback; Rust checks the same fixture.
  • cargo test -p leonida-formats: 42 passed, 5 existing ignored, 0 failed.
  • Related Python suites: 308 tests, 4 skips, 0 failures; includes production decoding/export/assets, explicit offline research, asset build entry points and line endings.
  • python -I scripts/run_semantic_tests.py tests/semantic -j 12: 507 files, 6,410 tests, 0 files failed.
  • Direct MSVC build/run of runtime/test/dstorage_test.cpp with the modified shim: 546 checks, 0 failures, including BC2 and plain/zlib requests.
  • tests/test_docs_links.py: 17 passed. Generated-table --check, compileall, make_game.py --help, formatting of the changed Rust files, whole-repository game-data screening and git diff --check passed.

Portable CMake used LEONIDA_BUILD_WINDOWS_RUNTIME=OFF and LEONIDA_VULKAN_HEADERS_FETCH=ON. The full Windows runtime build requires Windows SDK 10.0.26100; this machine selects 10.0.22621, so the modified DirectStorage shim was compiled/tested directly instead. No game launch, GPU image comparison or frame-rate benchmark was performed. Finite synthetic tests do not prove every possible bit sequence.

The refresh preserves main's DirectStorage vector generator CLI validation and atomic-write behavior (two tests passed), fixes the new table generator's stdout guard, and corrects make_game.py's stale calibration recommendation. CTest registration: seven tests passed.

Eight remaining failing tool/documentation test files were rerun on a separate unmodified checkout of main 4a54aedcb. Both checkouts reported the same ten failing tests, including the same ten unrelated CLI tools missing stdout guards. The new generator is no longer among the failures. No tests were skipped or disabled to hide those baseline failures. Main's own CI run also reports failures; this PR does not claim globally green CI. The previous runtime CI also reported GPU failures outside BCPack (cs_imgatomic_ext and macOS vk_bindless_dimensions); those remain separate from the passing decoder checks.

Independent results at the original decoder head are linked in the Windows verification and Linux/gfx906 verification. Those results belong to their contributors, not this local refresh.

GPUs tested: n/a — CPU/synthetic validation only; no GPU/driver result claimed.

AI-assisted: yes — Codex.

Checklist

  • Updated with main at 4a54aedcb; published history preserved with a normal merge and push.
  • One topic: standalone BCPack production decoding and its integration/regressions.
  • No game data, keys, decryption, SDK binaries or private implementation excerpts included.
  • Existing overlapping claims/PRs documented and linked.
  • No shader translator rules changed; full translator regression passed.

Fix the Rust BC7 dictionary reader and lane scheduling, add BC2, and replace
the legacy production C++/Python rebuilders with fixed format dictionaries.
Vendor Xbox2Windows d049001 with preserved notices and C++20 portability.
Remove automatic calibration/history use from runtime and asset builds;
isolate explicit offline research tools and document existing PR overlap.

Validation: 1,076 committed synthetic goldens, 15,402 GDK comparisons per
implementation, 168 GDK compressor roundtrips, 308 related Python tests
(3 skips), 42 Rust tests (5 ignored), portable CTest, and 546 DirectStorage
checks. Full Windows runtime/game rendering was not tested.

AI-assisted: Codex.
@ecox982745

Copy link
Copy Markdown

The full Windows runtime builds at 3501c5bfa729a6d807263c51074ea25a51672c91. On Windows 11 x64 with MSVC 19.44, Ninja Release and Windows SDK 10.0.26100.0, CMake linked both gta6rt.exe and dstorage_test.exe with LEONIDA_BUILD_WINDOWS_RUNTIME=ON. Ninja’s dependency record for the runtime’s DirectStorage object confirms it compiled against the 26100 D3D12 and Windows headers.

I checked the production decoder library and queue objects from that build using the PR’s committed synthetic BC1–BC7 expected outputs:

CPU path Result
Direct call to the linked decoder All 1,076 fixtures matched, covering 737,776 output bytes.
DirectStorage memory queue, raw BCPack All 133 eligible requests completed with S_OK and exact output.
DirectStorage memory queue, zlib around BCPack All 133 eligible requests completed with S_OK and exact output.

The queue checks covered 19 fixtures per codec, in both modes, for 1,099,776 output bytes. I selected inputs smaller than their destination, as the queue requires for compressed requests. The smaller-output examples were checked through the decoder library. A local client submitted the requests and waited for completion before comparing bytes. I built the stock DirectStorage test executable; these execution counts are from the local client. GPU rendering and game performance remain unmeasured here.

AI-assisted with OpenAI Codex (GPT-6).

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the core Rust, C++, DirectStorage shim, vendored third_party/x2w-bcpack decoder, Python standalone integration, and license/notice files, and tested merged onto main (493867366) with full Windows runtime enabled (MSVC 19.44, Ninja Release, Windows SDK 10.0.26100.0) and Rust 1.100.1.

Code review notes

  • Rust (d3d12/crates/leonida-formats/src/bcpack.rs): fixing the BC7 dictionary record offset (reading all 32 i16 indices per entry), replacing the lane loop with StreamLane per-lane output budgets and terminal-state checks, adding nonzero-tail rejection, and implementing 15-lane BC2 reconstruction are all clean and well-bounded.
  • C++ & DirectStorage (third_party/x2w-bcpack/bcpack_decoder.h, runtime/src/bcpack.cpp, runtime/shims/dstorage.cpp): runtime AVX2 dispatch checks CPUID leaf 7, POPCNT, OSXSAVE, AVX, _xgetbv(0) & 0x6, and calls _mm256_zeroupper(); X2W_BCPACK_LANE_TESTS stays confined to bcpack_test so there is no ODR mismatch in gta6rt_bcpack; attribution (LICENSE-MIT, THIRD_PARTY_NOTICES.md) and provenance/overlap notes (docs/bcpack-standalone.md) are clear. (Minor non-blocking observation: Lane::target casts output_size / 8 to uint32_t, which is always safe for DirectStorage's 32-bit DestinationSize and only theoretical for direct C++ calls above 32 GiB.)
  • Python & assets (tools/bcpack.py, tools/bcpack_standalone.py, tools/ttpf_assets.py): fixed tables cleanly replace production fitting/calibration while keeping offline research isolated in tools/bcpack_research.py and invalidating old stamps via DECODER_SOURCES.
  • Maintainer coordination on the documented overlapping PRs (#1044, #1103, #1935, #1974, #1715, #2512, etc.) remains a policy/merge-order decision, and this PR documents those overlaps transparently.

Tests run

  • Full Windows runtime build (cmake --build <build> --target bcpack_test dstorage_test gta6rt): built cleanly with LEONIDA_BUILD_WINDOWS_RUNTIME=ON.
  • ctest --output-on-failure -R "^(bcpack|dstorage)$": 2/2 passed (bcpack, dstorage 5.99 s).
  • bcpack_test.exe tests/fixtures/bcpack-standalone.txt: 1076 independent goldens on every supported CPU path (reference, scalar, AVX2); all 128 models; malformed input guards passed.
  • cargo test --manifest-path d3d12/Cargo.toml --locked -p leonida-formats: 42 passed, 0 failed, 5 ignored (5.48 s).
  • Python unit suites (test_bcpack.py, test_bcpack_standalone.py, test_bcpack_history_calib.py, test_bcpack_native.py, test_bcpack_tables.py, test_bcpack_tool.py, test_calibrated_texture_extra.py, test_make_game_assets_pools.py, test_ttpf_assets.py, test_ttpf_mips.py, test_docs_links.py, test_ctest_registration.py): 275 passed (14 skipped, 0 failures, 20.84 s).
  • python -I tools/gen_bcpack_standalone_tables.py --check: verified.
  • python -I scripts/check_no_game_data.py: clean.

AI-assisted (Antigravity, Gemini).

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

@wish131400 — VERIFY-2567-LINUX-VEGA20: tested 3501c5bf merged cleanly with 3ca0707dd, synthetic inputs only.

Same-input Red → Green: main’s strict Python and native C++ production decoders each matched 44 of the 1,076 committed golden rows and rejected 1,032. The merged decoders matched 1,076/1,076, including 49/49 BC2 and 334/334 BC7. Pure Python and the optional native entropy path both matched. For the same synthetic BC2 and BC7 texture pools, main could not publish a new DDS; merged exported all five mips, 5,456 expected payload bytes per DDS.

Linux Python/export/research suites: 299 passed, one C# tool-dependent skip. Portable C++ BCPack CTest: 1/1. Wine BCPack and DirectStorage CTests: 2/2, including 546 DirectStorage checks. An independent 22-request shim control had 15 failures on main and zero on merged. The C++ path here was scalar/reference, not the MSVC AVX2 path.

Physical gfx906 / RADV Mesa 26.0.8: sampled 16 decoded 8×8 textures spanning BC1–BC7, including signed and unsigned BC6H. Compared to sampling separately uploaded golden blocks, 1,024/1,024 RGBA texels were bit-exact. A BC1 color change affected only the intended 4×4 block. Zero Khronos validation errors or sync hazards. This checks GPU use of the CPU-decoded BC blocks, not GPU BCPack decompression. No Rust results claimed.

Minor documentation mismatch: the module overview in make_game.py still recommends a local calibration profile for assets, but production ttpf_assets.load_profile() no longer reads one.

AI-assisted.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 28, 2026
Preserve upstream DirectStorage vector CLI validation and atomic writes alongside independent goldens. Guard the standalone table generator stdout and correct the assets calibration overview.
@wish131400

Copy link
Copy Markdown
Contributor Author

Updated to cb947eac0 by merging main at 4a54aedcb; the published decoder commit is preserved and no force push was used.

  • Resolved the DirectStorage vector generator conflict by retaining the independent synthetic goldens together with main's CLI validation and atomic output handling. Its two upstream regression tests pass.
  • Added the stdout encoding guard to the new standalone-table generator.
  • Corrected the make_game.py module overview's obsolete local-calibration recommendation, as noted in the independent Linux review.

Validation on Windows x64 / MSVC 19.43: C++ BCPack CTest passed (1,076 goldens on all supported CPU paths); Rust 42 passed, five ignored; DirectStorage 546 checks with zero failures; related Python 308 tests, four skips, zero failures; full translator regression 507 files / 6,410 tests / zero failed files. Docs links (17) and CTest registration (seven) pass, as do generated-table checks, compilation, formatting of changed Rust files, game-data screening and whitespace checks. CPU-only local refresh; no game or GPU run.

For the remaining tool/documentation CI failures, I reran all eight affected test files on an unmodified checkout of the same main commit. Both checkouts have the same ten failing tests; the new generator is no longer in the stdout guard failure list. No tests were disabled. Main's own CI is also failing: https://github.com/ProjectLeonida/mandy/actions/runs/46138636141 . Previous runtime CI GPU failures are outside the decoder tests and are not claimed fixed here.

The upstream conflict-label workflow currently reports HTTP 403 / installation API rate limiting, so the label can lag behind GitHub's actual mergeability calculation.

AI-assisted: Codex.

@github-actions github-actions Bot added needs rebase Conflicts with the base branch: rebase it on main and removed needs rebase Conflicts with the base branch: rebase it on main labels Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants