calibrated_texture: crop NPOT mips to the DDS grid (CALIBRATED-TEXTURE-NPOT-DDS) #2325

Merged
leonida/mandy
syfyra merged 1 commit from fix/calibrated-texture-npot-dds into main 2026-12-01 15:18:43 +00:00

Pull request successfully merged and closed

syfyra commented 2026-11-26 13:48:14 +00:00

Claimed in the coordination issue as: CALIBRATED-TEXTURE-NPOT-DDS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7193838373)

What and why

tools/calibrated_texture.py LocalDecoder.dds (the --calibration export) wrote every block of a mip's stored grid under a standard DDS header. For NPOT chains the stored grid can be wider or taller than the DDS mip, which is the case ttpf_mips._visible_rows exists for in the regular export:

  • Compact chains. 17x13 BC1 stores its 8x6 mip 1 as 3x2 blocks; DDS holds 2x2. The payload was 208 bytes instead of 192, and a reader took blocks (0,0) (1,0) (2,0) (0,1) as mip 1. ASSET-568 moved this branch to the stored grid to get the offsets right and dropped the crop; its test checks compact_dds_layout's return values, not the payload.
  • Mode-1 arrays. This branch kept the DDS grid, but addresses() takes its tile pitch from the grid it is given. A 513x520 BC7 array stores mip 1 as 65x65 blocks (two 64-block tiles per row) where DDS holds 64x65, so rows 64 and on were read from the wrong tile (128 blocks differ per export).

Each mip is now read on its stored grid, which also sets the tile pitch, and cropped to the DDS rows and columns. Aligned textures are unchanged (the crop is skipped when the grids match). For both cases above the export is now byte-identical to ttpf_mips.texture_dds.

Tested

Windows 11, Python 3.14.7, synthetic pools only (numpy is not installed here, so the test runs on the file's own numpy stand-in; CI's numpy job runs it with numpy):

  • python -I tests/test_calibrated_texture_extra.py: the new TestLocalDecoder.test_dds_npot_mips_are_cropped_to_the_dds_grid writes a 17x13 BC1 chain and a 2-slice 513x520 BC7 mode-1 array whose every block holds its own (slice, mip, x, y) and compares the export with the DDS rows of each mip. On main both cases fail (208 != 192; block 20866 is (0, 1, 64, 0) instead of (0, 1, 0, 64)); with the fix both pass. 30 of 31 tests pass; test_export_file_replacement_failure_cleans_temporary_file fails on main as well (claimed in #1608, open #2101).
  • tests/test_calibrated_texture.py (skips without numpy), tests/test_ttpf_mips.py (93), tests/test_ttpf_assets.py (31), tests/test_tool_startup.py OK.
  • python -m compileall; scripts/check_no_game_data.py on both files.

GPUs tested: n/a (CPU-only tool)

AI-assisted: yes (Claude Code, Claude Opus 5.5)

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. (n/a: no translator change)

🤖 Generated with Claude Code

Claimed in the coordination issue as: CALIBRATED-TEXTURE-NPOT-DDS (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7193838373) ### What and why `tools/calibrated_texture.py` `LocalDecoder.dds` (the `--calibration` export) wrote every block of a mip's stored grid under a standard DDS header. For NPOT chains the stored grid can be wider or taller than the DDS mip, which is the case `ttpf_mips._visible_rows` exists for in the regular export: - **Compact chains.** 17x13 BC1 stores its 8x6 mip 1 as 3x2 blocks; DDS holds 2x2. The payload was 208 bytes instead of 192, and a reader took blocks (0,0) (1,0) (2,0) (0,1) as mip 1. ASSET-568 moved this branch to the stored grid to get the offsets right and dropped the crop; its test checks `compact_dds_layout`'s return values, not the payload. - **Mode-1 arrays.** This branch kept the DDS grid, but `addresses()` takes its tile pitch from the grid it is given. A 513x520 BC7 array stores mip 1 as 65x65 blocks (two 64-block tiles per row) where DDS holds 64x65, so rows 64 and on were read from the wrong tile (128 blocks differ per export). Each mip is now read on its stored grid, which also sets the tile pitch, and cropped to the DDS rows and columns. Aligned textures are unchanged (the crop is skipped when the grids match). For both cases above the export is now byte-identical to `ttpf_mips.texture_dds`. ### Tested Windows 11, Python 3.14.7, synthetic pools only (numpy is not installed here, so the test runs on the file's own numpy stand-in; CI's numpy job runs it with numpy): - `python -I tests/test_calibrated_texture_extra.py`: the new `TestLocalDecoder.test_dds_npot_mips_are_cropped_to_the_dds_grid` writes a 17x13 BC1 chain and a 2-slice 513x520 BC7 mode-1 array whose every block holds its own (slice, mip, x, y) and compares the export with the DDS rows of each mip. On `main` both cases fail (`208 != 192`; block 20866 is (0, 1, 64, 0) instead of (0, 1, 0, 64)); with the fix both pass. 30 of 31 tests pass; `test_export_file_replacement_failure_cleans_temporary_file` fails on `main` as well (claimed in #1608, open #2101). - `tests/test_calibrated_texture.py` (skips without numpy), `tests/test_ttpf_mips.py` (93), `tests/test_ttpf_assets.py` (31), `tests/test_tool_startup.py` OK. - `python -m compileall`; `scripts/check_no_game_data.py` on both files. GPUs tested: n/a (CPU-only tool) AI-assisted: yes (Claude Code, Claude Opus 5.5) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
lixonik reviewed 2026-11-26 15:43:59 +00:00
lixonik left a comment
No description provided.
Windows 11: 32/33 calibrated texture checks ran and passed, including the new synthetic NPOT crops. One end-to-end BC1 export was skipped because the optional native calibration DLL was unavailable. No findings from these checks. AI-assisted.
wavehs reviewed 2026-11-26 18:54:22 +00:00
wavehs left a comment
No description provided.
**macOS arm64 check of #2325** (CALIBRATED-TEXTURE-NPOT-DDS). A texture-decoder change and its test; no GPU, no build. Tested head `a80bb68f7` with its diff applied to a copy of `main` `bdbc7e923` (branch 86 commits behind; `git apply` / `git merge-tree` clean). Environment: Apple M3 Pro, arm64, macOS 27.2.1, CPython 3.15.2. No numpy on this interpreter, so `_load_calibrated_texture()` takes its stdlib-shim path - relevant to the failure below. **Result** - The new test passes with the change: `python -I tests/test_calibrated_texture_extra.py -k npot_mips` - 1 test OK (both subtests: compact BC1 17x13, mode-1 BC7 513x520 array). - Whole file: 31 tests, 1 failure. That failure is **not** from this PR: `test_export_file_replacement_failure_cleans_temporary_file` fails identically on a clean `main` tree (I ran it there: same `AssertionError: OSError not raised`). Root cause and fix are the finding below. - Negative control (new test kept, `tools/calibrated_texture.py` put back to `main`'s): both subtests fail with the payload mismatch the PR describes - `AssertionError: 208 != 192` for compact BC1 (one stored block row too many), and `block 20866 ... is (slice, mip, x, y) (0, 1, 64, 0), want (0, 1, 0, 64)` for the mode-1 array (DDS width used as the tile pitch). **Review of the change** - The crop is applied after the read, so the stored grid still governs the addresses: `visible` is the DDS grid (`:275-278`), the array branch takes the stored grid from `layout["mips"]` (`:283`), and the compact branch takes it from `compact_dds_layout`. Reading with the DDS grid, which is what the old code did, is what shifted every later mip - the test's two shapes cover both a wider and a taller stored grid. - The row slice `blocks[r * w * bs:(r * w + vw) * bs]` uses `w`, the stored block width of that mip, as the stride, which is correct for a row-major read of `w * h` blocks; the guard `if vw > w or vh > h` fails closed on a layout that stores fewer blocks than DDS needs instead of reading past the surface. - `visible[i]` and `grids[:count]` are indexed by the same mip number in both branches (`offsets` is keyed by `mip["index"]` and the array layout lists mips in index order), so a `mips=` export of a prefix of the chain crops the right grid. - The comment naming `ttpf_mips._visible_rows` is accurate: the regular export already crops, so this brings `LocalDecoder.dds` in line rather than inventing a rule. **Finding (exists on `main`, not introduced here): `tests/test_calibrated_texture_extra.py:2230-2241` cannot see the failure it asserts without numpy.** The test patches `replace_retry.replace`, but `export_file` calls `replace_retry.replace` through the module object that `_load_calibrated_texture()` created. With numpy absent, that loader runs `tools/calibrated_texture.py` under `mock.patch.dict(sys.modules, {"numpy": fake_np})` (`:959`); `patch.dict` restores `sys.modules` on exit, which drops the `replace_retry` module that was first imported inside the block, so `T.ct.replace_retry` is an orphaned instance and the later `mock.patch("replace_retry.replace")` patches a different one. I confirmed both directions: `export_file` returns 0 with the global patch in place, and the test passes when the patch is `mock.patch.object(T.ct.replace_retry, "replace", ...)`. With numpy installed the plain-import branch is taken and the test passes, which is why CI (numpy present) does not see it. Patch the module object, as the neighbouring tests already do with `mock.patch.object(ct, ...)`. Related, not from this PR either: the same shim path means any test that patches a module `tools/calibrated_texture.py` imports lazily can silently patch the wrong instance. `python -I scripts/check_no_game_data.py tools/calibrated_texture.py tests/test_calibrated_texture_extra.py`: clean. No game files. AI-assisted.
aslan2142 reviewed 2026-12-01 12:02:19 +00:00
aslan2142 left a comment
No description provided.
Draft review against current `main`: reading each mip on its stored grid and cropping to the visible DDS grid in `tools/calibrated_texture.py:272-322` looks clean and clean-room verified. Still Draft with no CI run yet — please mark Ready for Review so CI runs green. *(AI-assisted review.)*
aslan2142 commented 2026-12-01 12:28:40 +00:00

Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review.

(AI-assisted triage.)

Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review. *(AI-assisted triage.)*
aslan2142 approved these changes 2026-12-01 13:04:45 +00:00
aslan2142 left a comment

Now ready for review and MERGEABLE. Clean-room verified: cropping NPOT mips to the DDS grid in tools/calibrated_texture.py:272-322 (with fail-closed bounds guard and regression coverage in tests/test_calibrated_texture_extra.py) is clean and verified on Windows and macOS (@lixonik, @wavehs; the unrelated no-numpy replace_retry failure @wavehs noted is pre-existing on main).

Approving — ready to merge once CI runs green on current main.

(AI-assisted review.)

Now ready for review and `MERGEABLE`. Clean-room verified: cropping NPOT mips to the DDS grid in `tools/calibrated_texture.py:272-322` (with fail-closed bounds guard and regression coverage in `tests/test_calibrated_texture_extra.py`) is clean and verified on Windows and macOS (`@lixonik`, `@wavehs`; the unrelated no-numpy `replace_retry` failure `@wavehs` noted is pre-existing on `main`). Approving — ready to merge once CI runs green on current `main`. *(AI-assisted review.)*
merzleckeier commented 2026-12-01 13:33:15 +00:00

Added portrait cases to the synthetic DDS comparison at a80bb68f7 + e04721c7 on Linux. I ran the same block-tagged fixture on main and the PR with both orientations of the compact BC1 and mode-1 BC7 array cases. The new 13×17 BC1 case with four mips had 232 payload bytes on main, where 208 were expected. Main also failed the two original cases. With the PR all four checked byte-for-byte, including the additional three-mip portrait BC7 array. The checked-in test file passed 31/31. Synthetic blocks only.

AI-assisted with OpenAI Codex.

Added portrait cases to the synthetic DDS comparison at `a80bb68f7` + `e04721c7` on Linux. I ran the same block-tagged fixture on main and the PR with both orientations of the compact BC1 and mode-1 BC7 array cases. The new 13×17 BC1 case with four mips had 232 payload bytes on main, where 208 were expected. Main also failed the two original cases. With the PR all four checked byte-for-byte, including the additional three-mip portrait BC7 array. The checked-in test file passed 31/31. Synthetic blocks only. AI-assisted with OpenAI Codex.
Sign in to join this conversation.
No description provided.