From a80bb68f75918ca3faf711b7da78030157ece700 Mon Sep 17 00:00:00 2001 From: syfyra <258908070+syfyra@users.noreply.github.com> Date: Thu, 26 Nov 2026 12:28:44 +0100 Subject: [PATCH] calibrated_texture: crop NPOT mips to the DDS grid (CALIBRATED-TEXTURE-NPOT-DDS) LocalDecoder.dds wrote every block of a mip's stored grid under a DDS header. For NPOT chains the stored grid is wider or taller than the DDS mip: 17x13 BC1 stores its 8x6 mip 1 as 3x2 blocks where DDS holds 2x2, so the payload was 208 bytes instead of 192 and a DDS reader saw every later mip shifted. ASSET-568 switched the compact branch to the stored grid to get the offsets right but dropped the crop. The mode-1 array branch kept the DDS grid instead, and addresses() takes its tile pitch from that grid, so a 513x520 BC7 array read mip 1 rows 64 and on from the wrong tile. Read each mip on its stored grid (which also gives the tile pitch) and keep only the DDS rows and columns, as ttpf_mips._visible_rows does for the regular export. Both cases now give the same bytes as ttpf_mips.texture_dds. Tested: new test_dds_npot_mips_are_cropped_to_the_dds_grid (fails before the fix in both cases, passes after). Co-Authored-By: Claude Opus 5.5 --- tests/test_calibrated_texture_extra.py | 44 ++++++++++++++++++++++++++ tools/calibrated_texture.py | 15 +++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/tests/test_calibrated_texture_extra.py b/tests/test_calibrated_texture_extra.py index d76eb17471..6577d89d63 100644 --- a/tests/test_calibrated_texture_extra.py +++ b/tests/test_calibrated_texture_extra.py @@ -2018,6 +2018,50 @@ def test_dds_no_tail_64k_tile_layout(self): dds = dec.dds(0) self.assertEqual(dds[148:], lin0 + lin1) + def test_dds_npot_mips_are_cropped_to_the_dds_grid(self): + # An NPOT mip can be stored on a wider or taller grid than its DDS grid: 17x13 BC1 stores the 8x6 mip 1 as + # 3x2 blocks where DDS holds 2x2, and a 513x520 BC7 mode-1 array stores mip 1 as 65x65 blocks (two 64-block + # tiles per row) where DDS holds 64x65. The export wrote the stored blocks (one block per row too many, + # every later mip shifted) or, for the array, took the DDS width as the tile pitch. Every block holds its + # own (slice, mip, x, y), so the expected payload is the DDS rows of each mip, slice by slice. + def block(s, i, x, y, bs): + return struct.pack(" 1: + lay = ttpf.array_surface_layout(w, h, depth, mips, bs, 4, tile) + else: + lay = ttpf.surface_layout(w, h, fmt, mips, tile) + stored = [(m["blocks_w"], m["blocks_h"]) for m in lay["mips"]] + dds_grid = [((max(1, w >> i) + 3) // 4, (max(1, h >> i) + 3) // 4) for i in range(mips)] + self.assertNotEqual(stored[1], dds_grid[1]) # the case under test + tiled = bytearray(lay["size"]) + expected = bytearray() + for s in range(depth): + for m in lay["mips"]: + i, (vw, vh) = m["index"], dds_grid[m["index"]] + lin = b"".join(block(s, i, x, y, bs) + for y in range(m["blocks_h"]) for x in range(m["blocks_w"])) + ttm.retile_to(lay, m, lin, tiled, base_offset=s * m.get("slice_stride", 0)) + expected += b"".join(block(s, i, x, y, bs) for y in range(vh) for x in range(vw)) + pool, _ = self._open_pool( + f"npot{depth}.ttpf", + [ttm.pack_etpf_record(w, h, mips, 0, fmt, depth, 1, mode, 4096)], + [(8, zlib.compress(bytes(tiled) + bytes(-len(tiled) % 4096)), len(tiled) + -len(tiled) % 4096)], + ) + dds = ct.LocalDecoder(pool, {"version": 1}).dds(0) + hdr = tex_headers.parse_dds_header(dds) + self.assertEqual((hdr["mips"], hdr["array_size"]), (mips, depth)) + got = dds[148:] + self.assertEqual(len(got), len(expected)) + bad = next((k for k in range(0, len(got), bs) if got[k:k + bs] != expected[k:k + bs]), None) + if bad is not None: + self.fail(f"block {bad // bs} of the payload is (slice, mip, x, y) " + f"{struct.unpack_from(' ttpf.MAX_OUTPUT: raise ValueError("DDS exceeds output limit") - grids = [ + # The DDS mip grid. The stored grid can be wider or taller (17x13 BC1 stores its 8x6 child as 3x2 blocks, + # DDS holds 2x2): each mip is read on its stored grid, which also sets the tile pitch, and then cropped to + # this one, as ttpf_mips._visible_rows does for the regular export. + visible = [ ((max(1, width >> i) + 3) // 4, (max(1, height >> i) + 3) // 4) for i in range(levels) ] @@ -277,6 +280,7 @@ def dds(self, index, mips=None): # Mode-1 arrays group source slices by mip. DDS groups mips by slice. layout = ttpf.array_surface_layout(width, height, faces, levels, bs, 4, tile) bw, bh = layout["block_w"], layout["block_h"] + grids = [(mip["blocks_w"], mip["blocks_h"]) for mip in layout["mips"]] offsets = { mip["index"]: (mip["offset"], mip["tile_x"], mip["tile_y"], mip["slice_stride"]) for mip in layout["mips"] @@ -305,7 +309,14 @@ def dds(self, index, mips=None): ) if at.max() + bs > d["base"] + span: raise ValueError("Texture crosses its allocation boundary") - payload.extend(self.surface(at, bs, profile)) + blocks = self.surface(at, bs, profile) + vw, vh = visible[i] + if vw > w or vh > h: + raise ValueError(f"Mip {i} stores {w}x{h} blocks, fewer than its {vw}x{vh} DDS blocks") + if (vw, vh) == (w, h): + payload.extend(blocks) + else: + payload.extend(b"".join(blocks[r * w * bs:(r * w + vw) * bs] for r in range(vh))) return ttpf.dds_bytes( width, height, fmt, count, linear, bytes(payload), array_size=faces )