Skip to content

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

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

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

Conversation

@wish131400

@wish131400 wish131400 commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Claim: BCPACK-STANDALONE-BC1-BC7.

Depends on: no unmerged PRs. Updated by a normal merge of main at b8bd3711160b829d3b17d2e0faf468f1d405b4ee; published decoder history is preserved.

What and why

The original Rust decoder misread BC7 dictionary indices after entry zero and rejected valid BC1/BC4 lane schedules. The legacy C++/Python production paths used incomplete or fitted initial dictionaries, and BC2 was missing. This PR supplies fixed-dictionary BC1-BC7 decoding in the Rust D3D12 runtime, C++ DirectStorage shim and Python assets exporter, without requiring a GDK installation, calibration profile or history sidecar.

  • Correct Rust lane scheduling/output budgets and implement BC2. Main's overlapping BC7 parser correction is reconciled with the full independent record-layout and golden tests.
  • Vendor the standalone C++ decoder from Xbox2Windows d049001ab9a906588f47868277b77e9a00e27053 (#3293), preserving notices and scalar/CPU-checked AVX2 dispatch.
  • Port the same reconstruction and constants to Python; optional native acceleration is confined to entropy decoding with fallback.
  • Remove automatic fitting and profile loading from normal runtime/assets builds. Keep explicit offline research in bcpack_research.py. Legacy quality/history arguments cannot change production output.
  • Share 1,076 independent synthetic goldens among all three implementations, including independent expected bytes in DirectStorage requests. Keep staged publication and malformed-input guards.

Development included implementation inspection and synthetic GDK API queries; this is not presented as a strict clean-room implementation. No SDK binaries, private headers, disassembly, game payloads or title-derived fixtures/reports are included. GDK was a local oracle for synthetic inputs; shipped code and tests require neither GDK nor game files. The original independent audit of 15,402 synthetic streams per implementation and 168 synthetic compressor roundtrips is documented in docs/bcpack-standalone.md; this merge reruns the committed regression fixtures and integration suites below.

Reconciliation with current main

This merge resolves the conflicts while retaining main's runtime installation staging/rollback, crash-monitor target, current README status and unrelated DirectStorage I/O diagnostics.

Historical overlaps #1044 / #1103, #1935 / #1974 and #1628 remain documented in docs/bcpack-standalone.md. This PR does not claim their separate legacy performance work as applied to the replacement decoder.

Validation after this merge

Windows x64, MSVC 19.43, Python 3.14; no game files or GPU commands used:

  • C++ BCPack CMake/CTest: 1,076 goldens on every supported CPU path, all 128 entropy models, output canaries and malformed-input guards passed.
  • Rust leonida-formats: 42 passed, 5 existing ignored, 0 failed.
  • Direct MSVC build/run of the modified DirectStorage shim: 552 synthetic checks, 0 failures.
  • Related Python decoder, offline research, assets/export, installer rollback, path/atomic-write, docs and CTest-registration suites: 412 tests, 14 platform/tool-dependent skips, 0 failures.
  • Full translator suite: 529 files, 6,526 tests, 1 failing file with 2 f16 interpolation failures. The same two failures, with the same values, reproduce on a separately extracted, unmodified main at b8bd371116. No translator implementation or semantic fixture is changed by this PR.
  • Shared-table --check, Python compilation, make_game.py --help, changed Rust formatting, line-ending tests, repository game-data screening and whitespace checks relative to current main passed.

The broad portable runtime build hits missing newer D3D12 barrier constants in main's unchanged d3d12_state_map_test with the locally installed Windows SDK 10.0.22621. The standalone BCPack target builds/tests independently, and the modified DirectStorage sources compile and pass directly. No full Windows runtime/game/GPU image or frame-rate result is claimed for this refresh. Finite synthetic tests do not prove every possible input bit sequence.

Independent results at the original decoder head: Windows verification, Linux/gfx906 verification. Those results belong to their contributors and are not GPU/game tests of this merge.

GPUs tested: n/a; profile Z, CPU/synthetic validation only.

AI-assisted: yes, Codex.

Checklist

  • Main merged normally; no rebase or force push.
  • Standalone BCPack decoding/integration remains the only topic.
  • Merged overlaps reconciled and review-requested offline path coverage restored.
  • No game data, keys, SDK binaries or private implementation excerpts included.
  • Decoder/integration tests pass; unrelated baseline failures and SDK build limitation disclosed.

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
Contributor

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.

@GrahamboJangles GrahamboJangles left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Independent review pass (overnight shift; verification from a Windows x64 checkout, Python 3.14.9).

Claim protocol: verified. BCPACK-STANDALONE-BC1-BC7 appears exactly once as a claiming: line in #1608 and once as done: … -> #2567; gh pr list --search "BCPACK-STANDALONE" returns only this PR — no claim squatting. Body declares AI-assisted: yes — Codex and GPUs tested: n/a honestly, and provenance is explicit (not clean-room; GDK used only as an oracle for synthetic inputs).

Local grounding at the PR head cb947eac0: ran the six Python bcpack suites unmodified:

  • tests/test_bcpack_standalone.py: 3 tests OK in ~4.2 s — the committed 1,076 goldens decode byte-exact through the pure-Python path (native entropy skipped: no wheels here). The len(cases) == 1076 assertion fired, so the fixture count is real.
  • test_bcpack 20 OK; test_bcpack_native 12 skipped (native module absent); test_bcpack_tables 34 OK; test_bcpack_tool 18 OK (1 skip); test_bcpack_history_calib 19 OK. Total: 106 tests, 0 failures.

Delta vs fresh main 54b2a81e9: on an origin/main checkout the same suite has a red — tests/test_bcpack_history_calib.py::test_resolve_pools_deduplicates_macos_case_and_accents_and_keeps_case_sensitive fails (AssertionError: 1 != 2), and test_bcpack_standalone.py does not exist on main at all. At the PR head that file is green. Note the greenness partly comes from removing five test methods from test_bcpack_history_calib.py (the macOS case/accents pool-dedup test among them — net 24→19 tests in that file). Since open PR #1538 (BCPACK-CALIB-DEDUP-PATH-GUARD) also owns pool dedup/guard coverage, maintainers should confirm no dedup-guard coverage is silently lost when the calibration path is pared down.

Merge status needs work — flagged. GitHub reports this PR CONFLICTING / DIRTY against current main. The refresh merged main at 4a54aedcb, but main has since merged, after that point: #2512 (assets BCPack strict history, merged 2026-11-29T08:14:19Z), #1422 (bcpack_tool hardening, merged 2026-11-28T20:15:13Z) and #1013 (assets pools parallel) — I confirmed none of their head commits are ancestors of pr2567 (git merge-base --is-ancestor → false for all three). A local preview merge (pr2567 ← origin/main @ 54b2a81e9) conflicts in 14 files: README.md, docs/directstorage.md, make_game.py, runtime/CMakeLists.txt, runtime/shims/dstorage.cpp, runtime/src/bcpack.cpp, runtime/src/bcpack.h, runtime/test/dstorage_test.cpp, tests/test_bcpack_history_calib.py, tests/test_ttpf_assets.py, tools/bcpack_calibration/README.md, tools/bcpack_tool.py, tools/gen_bcpack_runtime_tables.py, tools/ttpf_assets.py. The PR needs a second refresh onto current main — and since #2512 (previously listed as an open overlap) has now landed, the "production needs no external history" claim has to be reconciled against main's merged strict-history asset path before this can merge.

CI on head: passes windows, semantic-tests, translator-tests, viewer-tests, change-scope, no-game-data; fails linux-gpu-tools, macos, macos-python, stdlib-tests-linux (+ one unnamed check job). Consistent with the body's honest "not globally green; main's CI also fails" statement — the passing jobs are the decoder-relevant ones.

Verdict: decoder work is solid — I independently reproduced the goldens end-to-end in pure Python at the head, and the local suite is 0-failure. Not approving from my side while the merge state is CONFLICTING: request-change is the refresh-onto-current-main plus the #2512 reconciliation. Commented review, no merge action taken.

@fiestorra

Copy link
Copy Markdown
Contributor

Mechanical note, no review: this branch conflicts with main (4364a4fa6) on a rebase, and the conflict is smaller than it sounds.

git merge-tree --write-tree origin/main refs/pull/2567/head reports 15 conflicted file(s):

  • d3d12/crates/leonida-formats/src/bcpack.rs, docs/directstorage.md, make_game.py, README.md… (15 in total)

This is the heaviest of the five: 49 files changed, 10130 insertions and 2033 deletions, and 1320 commits behind. A rebase here is real work, not a formality.

So a rebase is all it needs - the change itself may well be fine, I have not looked at its content, only at whether git can apply it.

Written with an AI assistant.

@MoonEyes

Copy link
Copy Markdown
Collaborator

Tried cb947eac0 against main 060c47160 (Windows 11, Rust toolchain from d3d12/rust-toolchain.toml, CPU only): it no longer merges, git merge conflicts with main. Not tested further.

  • On main alone, cargo test -p leonida-formats --locked gives 41 passed, 5 ignored, and clippy has 0 findings for that crate.
  • Could you rebase on main? I will rerun the tests on the new head.

AI-assisted (Claude Code).

@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 30, 2026

@GrahamboJangles GrahamboJangles left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Follow-up re-check after the reconcile push (head cb947ea -> 7078256), 2026-11-30.

Merge reconciliation verified. The new merge commit 7078256 has second parent b8bd371, which is on current main (e1fe82f), and #2512's head (a23d691) is now an ancestor of the PR head. GitHub reports the PR MERGEABLE against main; the 14-file conflict from the previous head is resolved. The head is ~20 commits behind current main, but since b8bd371 is on main, the merge-into-main effect is exactly the diff I analyzed locally.

Local verification at the new head (Windows, Python 3.14, unmodified suites, checkout of head 7078256):

  • All six Python bcpack suites: 121 tests, 0 failures. The five pre-existing suites run 118/118 at the head; current main runs the same 118, 0 failures — zero test regression vs fresh main. The new standalone suite adds 3 tests including the 1,076 synthetic goldens decoding byte-exact via the pure-Python path.
  • The pool-dedup test (tests/test_bcpack_history_calib.py::test_resolve_pools_deduplicates_macos_case_and_accents_and_keeps_case_sensitive) that was red on main at main@54b2a81e9 yesterday is green on current main, and stays green at this head.

Semantic-tests CI failure at the new head is inherited, not introduced. Reproduced locally with scripts/run_semantic_tests.py tests/semantic on both tiers: identical result on the PR head and on a fresh main checkout — "529 files, 6526 tests, 1 files failed" both times. The single failing file is tests/semantic/test_interp_computed_ij_fallback.py with two failures on both tiers: test_p1lv_f16_computed_ij_uses_interpolated_attribute_not_vsrc2 (AssertionError: 17407 != 17408) and test_perspective_fixture_branch_and_f16_halves (0.07745361328125 != 0.0775146484375). Nothing in this PR touches interp fallback; main's CI is red the same way.

One semantic note for maintainers. Vs current main the PR's net effect is 50 files, +10,234/-2,322. In runtime/src/bcpack.cpp, bcpack.h, shims/dstorage.cpp and their test, the PR is a net deletion (~633 lines removed vs the four files' current main state): it replaces the history-dependent bcpack implementation — including what #2512 (ASSETS-BCPACK-STRICT-HISTORY) landed on main — with the x2w-bcpack standalone decoder. #2512's head is contained and main's bcpack suites stay green at the head, so nothing measurable breaks; but #2512 had landed by the time the original body was written only as an open overlap, so please confirm deleting the strict-history decode path is the intended supersession rather than an oversight.

CI at 7078256: windows, linux-gpu-tools, translator-tests, viewer-tests, change-scope, no-game-data pass; check, macos, macos-python, semantic-tests, stdlib-tests-linux fail, all inherited from main as shown above. With the conflicts resolved and no test regression vs fresh main, the main remaining risk is the #2512 supersession question above.

@KelboLive

Copy link
Copy Markdown

Independent check on Windows (not the author), at 7078256d1.

  • cargo test --locked -p leonida-formats --lib (shared warm target): 42 passed, 0 failed, 5 ignored in 5.7 s.
  • The BC1–BC7 standalone decode tests, including payload_step_matches_the_reference_decode, pass.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned.

AI-assisted: yes (opencode).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants