Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tests/test_calibrated_texture_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<BBHH", s, i, x, y).ljust(bs, b"\0")

for name, w, h, fmt, bs, mips, depth, mode in (("compact BC1", 17, 13, 71, 8, 2, 1, 4),
("mode-1 BC7 array", 513, 520, 98, 16, 2, 2, 1)):
with self.subTest(name):
tile = ttpf.tile_bytes(mode)
if depth > 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('<BBHH', got, bad)}, want {struct.unpack_from('<BBHH', expected, bad)}")

def test_compact_dds_layout_uses_tex_headers_tail_and_mips(self):
# ASSET-568: NPOT chains and a single-mip sub-tile must use the measured compact
# layout's first_tail/mips, not locally re-derived grids (the latter packed a
Expand Down
15 changes: 13 additions & 2 deletions tools/calibrated_texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,18 @@ def dds(self, index, mips=None):
)
if expected > 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)
]
if faces > 1 and d["mode"] == 1:
# 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"]
Expand Down Expand Up @@ -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
)
Expand Down