Skip to content

build_dstorage_history: fit missing BPTC history slots across multi-level sibling mips (DSTORAGE-MULTILEVEL-MIP-FIT) - #2236

Open
zmasi wants to merge 8 commits into
ProjectLeonida:mainfrom
zmasi:fix/dstorage-multilevel-bcpack-fit
Open

zmasi wants to merge 8 commits into
ProjectLeonida:mainfrom
zmasi:fix/dstorage-multilevel-bcpack-fit

Conversation

@zmasi

@zmasi zmasi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: DSTORAGE-MULTILEVEL-MIP-FIT (#1608 (comment))

What and why

calibrate_from_pools and fit_slices in tools/build_dstorage_history.py only pair BC6H and BC7 chunks with immediate level ± 1 raw or zlib neighbor mips. When a mixed-codec texture stores lossless raw/zlib at mip[0] (<= 1 MiB) and BCPack across mips[1..N], packed tail and mid-chain BC7/BC6H mips at |level - ref_level| >= 2 are never visited, leaving initial-history slots (eslot = 256 + i - de, islot = 256 + i - di) at 0 bits and causing runtime/src/bcpack.cpp (missing-endpoint at :387) to reject the stream with 0x89240030 (E_DSTORAGE_DECOMPRESSION_ERROR) when transitive descendants exceed max(2, blocks / 1024).

fit_distant_mips runs after calibrate_from_pools and fit_slices (preserving full precedence of existing adjacent-mip, array, and cubemap fits via extend_missing) and scans only BC6H/BC7 chunks whose 256-block prefix still references an unfitted slot (77 / 5,167 BC7 chunks across the 57 retail pools), pairing those mips with the closest raw or zlib mip on the same layer across multi-level mip distances (child upsample for delta_level < 0, parent box average for delta_level > 0) ahead of the native bcpack_history_train full-stream constraint pass.

Across all 57 v1.0.2.1 pools (5,435 BPTC streams evaluated at strict divisor = 1024, counts only):

  • BC7 predictor fields: 4,228 / 6,144 -> 4,336 / 6,144 (+108); index fields 8,160 / 8,192.
  • BC6H predictor slots: endpoints 210 / 256 -> 226 / 256 (+16); indices 249 / 256 -> 251 / 256 (+2).
  • BC6H missing-slot refusals: 0 / 268 (100% pass).
  • BC7 missing-slot refusals: 60 / 5,167 -> 18 / 5,167 (-70%), resolving 25 / 25 mixed-codec BC7 streams (including both world-load 0x89240030 missing-endpoint requests, each decoding with 0 unknown blocks and 0 consistency violations at divisor = 1024). The remaining 18 streams belong to pure-BCPack textures with no raw or zlib mips.

Tested

  • python -m compileall -q make_game.py tools tests (exit 0)
  • python -I tests/test_dstorage_multilevel_fit.py -v (3/3 synthetic multi-level BC7 downsample, BC7 upsample, and BC6H + slot-precedence tests pass; fails without fit_distant_mips)
  • python -I -m unittest discover -s tests -p "test_dstorage_*.py" -v (7/7 pass, including bcpack_history_train integration)
  • python -I tests/test_bcpack_history_calib.py -v (19/19 pass)
  • python -I tests/test_text_write_line_endings.py & python -I -m unittest discover -s tests -p "test_docs_*.py" (55/55 pass)
  • python -I scripts/check_no_game_data.py tools/build_dstorage_history.py docs/directstorage.md tests/test_dstorage_multilevel_fit.py (exit 0)
  • Full 57-pool sidecar rebuild + bcpack_history_train refinement on Windows 11 (fit_distant_mips pass: ~88 s)

GPUs tested: n/a (CPU calibration tool and unit tests; verified on Windows 11 x64, Ryzen 9 5950X / RTX 3090 host)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

…ling mips

When a BC6H or BC7 stream still references an unfitted built-in predictor slot after adjacent-mip fitting, pair its mips with the closest raw or zlib mip on the same layer across multi-level mip distances before running the full-stream constraint pass. Existing adjacent-mip, array, and cubemap fits keep precedence.
@real-rusher

Copy link
Copy Markdown
Contributor

Verified on main a73a57e (Ubuntu 24.04 x86-64, Python 3.13, no GPU, no game data). Merges cleanly. With this PR, python -I tests/test_dstorage_multilevel_fit.py: 3 tests OK. The PR's test file against main's tools: 3 errors, so the new tests fail on main and pass with the change. compileall clean.

AI-assisted: yes (Claude Code)

@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.

Tested this head locally with synthetic fixtures. In Ubuntu on WSL, all 7 DirectStorage tests passed, including a native bcpack_history_train build and build/resume/repair, the BC7 parent and child cases, and BC6H slot precedence. On Windows, the 6 Python checks passed and the native fitter integration skipped without a C++ compiler on PATH. No findings from these checks. The retail pool coverage numbers were outside this run.

AI-assisted.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 26, 2026
Run fit_distant_mips ahead of fit_decoded_neighbors so mixed-codec BC6H and BC7 streams fit directly against lossless raw/zlib mips across multi-level distances before iterative BCPack sibling decoding, eliminating all 10 remaining BC6H stream refusals on 1.0.2.1 (210 -> 226/256 endpoints, 249 -> 251/256 indices) and cutting fit_decoded_neighbors runtime by 46%.
@zmasi

zmasi commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @real-rusher and @lixonik for verifying the synthetic and native fitter checks!

Merged main (d1dff7949, including 2a491668a and #2124) cleanly in 801f8e7b7 and re-measured the 57-pool corpus directly against d1dff7949 alone vs d1dff7949 + fit_distant_mips:

Metric (57 pools, 5,435 BPTC streams) main (d1dff7949) main + #2236 (801f8e7b7) Delta
BC6H endpoint rows 210 / 256 226 / 256 +16
BC6H index rows 249 / 256 251 / 256 +2
BC6H refused streams 10 / 268 0 / 268 -10 (100% pass)
BC7 endpoint fields 4,406 / 6,144 4,406 / 6,144 (4,336 lossless-anchored, low2Fits = 7 vs 6) Lossless-anchored + +1 low2Fit
BC7 index fields 8,160 / 8,192 8,160 / 8,192 0
BC7 refused streams 8 / 5,167 8 / 5,167 0
Supplemental fit runtime (fit_distant + fit_decoded) 1,185.56 s 767.73 s (136.06 s + 631.56 s) -417.83 s (-35.2%)
  • Why fit_distant_mips complements fit_decoded_neighbors (2a491668a): fit_decoded_neighbors skips refused BC6H siblings (10 / 268 left refused on main), whereas running fit_distant_mips immediately ahead of fit_decoded_neighbors pairs mixed-codec BPTC mips directly against lossless raw/zlib mips across multi-level distances — eliminating all 10 / 268 remaining BC6H refusals, anchoring 4,336 BC7 endpoint fields directly from lossless mips, and cutting fit_decoded_neighbors runtime by 46.7% (-35.2% overall).
  • Added regression test: test_refused_bc6h_intermediate_sibling_falls_back_to_multilevel_zlib_mip in tests/test_dstorage_multilevel_fit.py (10 / 10 DirectStorage tests passing).

No game files. AI-assisted.

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

sodove commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2236 | head 801f8e7b7ec95dd42ff03acbf8fdaf8f0dff80b2 | python3 -I tests/test_dstorage_multilevel_fit.py -v | red: 4 tests, 4 errors | green: 4 passed | Exceptions on main → pass

All four baseline cases stopped at AttributeError because main lacks fit_distant_mips. This demonstrates the missing API. The baseline did not reach a value comparison.

Pinned main 4938673668d807bf365b61284bf22b1ff8d02cc4, merged into the PR head. Ubuntu 26.04.1 (WSL2), Python 3.14.4. Full suite: not run. AI-assisted: OpenAI Codex. Batch claim.

Exercise the public history.build(...) pipeline end-to-end across all four
synthetic multi-level BPTC mip fitting tests so running the test file against
main reaches value assertions (4 AssertionErrors, 0 AttributeErrors) instead
of stopping on the missing fit_distant_mips helper.
@zmasi

zmasi commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @sodove for the baseline verification! Merged main (33db04a70) cleanly and updated tests/test_dstorage_multilevel_fit.py in b55056766 so all four test cases drive the public build_dstorage_history.build(...) pipeline end-to-end:

  • Red on main (33db04a70): python -I tests/test_dstorage_multilevel_fit.py -v runs all 4 cases through history.build(...) with 0 AttributeErrors and fails on value comparisons (FAILED (failures=4)).
  • Green on b55056766: python -I tests/test_dstorage_multilevel_fit.py -v passes (4/4 OK), along with unittest discover -s tests -p "test_dstorage*.py" and the test_bcpack* suites.

No game files. AI-assisted.

…in multilevel mip fit

- Keep secret_entry >= 192 with ref_count <= secret_entry across all synthetic
  BPTC streams so predictor distances never wrap mod 256 and main leaves
  unknown slots at zero rather than self-fitting wrapped blocks.
- Drive dual-slot BC6H and BC7 chunks (missing slot 240 + pre-populated slot
  241 with conflicting pool colors) so extend_missing precedence is genuinely
  exercised for both codecs.
- Assert fitted BC6H endpoint values, zero confirmed-bit masks, and per-block
  decoded RGBA / unquantized HDR pixels across both parent-downsample and
  child-upsample streams, and include codec mode in fit_distant_mips wanted keys.

@timbot103 timbot103 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.

I reviewed head b55056766 statically against the PR base and checked the predictor consumers in tools/ and runtime/. No builds, tests, GPU runs, or game-data access. The synthetic cases in tests/test_dstorage_multilevel_fit.py exercise both directions and existing-slot precedence through build() with native vote collection stubbed. I found two fitting errors.

  • [MAJOR] tools/build_dstorage_history.py:257 Signed BC6H references reach an unsigned fitter. With a missing mode-14 history row, zero base-endpoint deltas, and a constant signed reference endpoint of −16384, the clamp in tools/bcpack_history_calib.py:1401 turns the target into zero and the new pass publishes a fitted zero endpoint. Preserve signed targets and evaluate candidates in signed unquantized space. A DXGI 96 regression case should check that the decoded endpoint remains negative.
  • [MAJOR] tools/build_dstorage_history.py:151 At a four-level downsample, the stride samples only one column phase. A BC7 reference with alternating 0 and 254 columns gives the fitter 0 for every quadrant when the box mean is 127. Average the full footprint and add a patterned reference case at that distance.

AI-assisted.

…ti-level footprint

- Thread signed=(format == 96) into Bc6hCalibrator.add_pool_chunk so signed
  BC6H_SF16 reference targets clamp to [-32768, 32767] instead of [0, 65535]
  and _eval_w_ops sign-extends and unquantizes candidates in signed space.
- Average the full [x0, x1) x [y0, y1) quadrant footprint across multi-level
  downsamples in _make_multilevel_quads_getter (caching per-4x4-block sums),
  eliminating single-phase column/row aliasing at 4+ mip levels.
- Add DXGI 96 negative-endpoint and 4-level alternating-column BC7 regression
  tests in tests/test_dstorage_multilevel_fit.py.
@zmasi

zmasi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @timbot103 for catching both fitting issues! Fixed in ab4102efee (also merged current main ef32995848 cleanly):

  1. Signed BC6H (DXGI 96, BC6H_SF16) endpoint fitting (tools/bcpack_history_calib.py, tools/build_dstorage_history.py):
    • Threaded signed=(d["format"] == 96) into Bc6hCalibrator.add_pool_chunk so BC6H_SF16 observations clamp to [-32768.0, 32767.0] instead of [0.0, 65535.0], and updated _eval_w_ops / _fit_w to sign-extend and unquantize candidate w values in signed BC6H_SF16 space.
    • Added test_signed_bc6h_sf16_multilevel_fit_preserves_negative_endpoints in tests/test_dstorage_multilevel_fit.py (Mode-14 DXGI 96 stream with a missing history row, zero base-endpoint deltas, and signed reference endpoints (-16384, -12288, -6144) — failed before fix and now preserves negative decoded endpoints within <= 64 unquantized units).
  2. Full-footprint box mean across multi-level downsamples (tools/build_dstorage_history.py):
    • Replaced the strided subsample in _make_multilevel_quads_getter with an exact box mean over the full [x0, x1) x [y0, y1) quadrant footprint (caching 16-texel 4x4 block sums for block-aligned footprints).
    • Added test_bc7_four_level_downsample_averages_full_footprint_across_alternating_columns in tests/test_dstorage_multilevel_fit.py (512x512 -> 32x32 4-level BC7 downsample over alternating 0 and 254 columns — fitted 0 before fix and now fits the 127 box mean within <= 4).

python -I tests/test_dstorage_multilevel_fit.py -v (6/6 passed) and all DirectStorage / BCPack suites pass.

No game files. AI-assisted.

@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.

Re-reviewed ab4102efe (tools/bcpack_history_calib.py, tools/build_dstorage_history.py, tests/test_dstorage_multilevel_fit.py).

Both fitting fixes look clean: signed BC6H_SF16 (DXGI 96) endpoints are now clamped and unquantized in signed space, and _make_multilevel_quads_getter averages the full quadrant footprint across multi-level downsamples with new regression coverage for both cases.

Local verification (Windows 11, Python 3):

  • python -I tests/test_dstorage_multilevel_fit.py -v: 6/6 passed (including test_signed_bc6h_sf16_multilevel_fit_preserves_negative_endpoints and test_bc7_four_level_downsample_averages_full_footprint_across_alternating_columns).
  • python -I scripts/check_no_game_data.py: passed.

AI-assisted (Antigravity, Gemini).

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.

5 participants