"""Tests for tools/ttpf_mips.py: tile modes, mip-tail layout, BC decode, metric, and a synthetic RDS5/ETPF pool round trip. No game data is used. python -I tests/test_ttpf_mips.py """ import importlib.util import io import json import math import os import random import struct import subprocess import sys import tempfile import tracemalloc import unittest import zlib from contextlib import ExitStack, closing, redirect_stderr, redirect_stdout from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOL = os.path.join(ROOT, "tools", "ttpf_mips.py") spec = importlib.util.spec_from_file_location("ttpf_mips", TOOL) ttpf = importlib.util.module_from_spec(spec) spec.loader.exec_module(ttpf) def rgb565(r, g, b): return ((r * 31 // 255) << 11) | ((g * 63 // 255) << 5) | (b * 31 // 255) def solid_bc1(color): c = rgb565(*color) return struct.pack("> 1, r >> 1), g=(g >> 1, g >> 1), b=(b >> 1, b >> 1), a=(127, 127), pbits=(r & 1, r & 1), ) def pack_bc6h_block(code, *, part=0, fields=None, indices=None): """Assemble one 16-byte BC6H block for any of the 14 valid mode codes.""" _, _, _, reg = ttpf.bcpack._BC6H_INFO[code] plan, hdr = ttpf.bcpack._bc6h_plan(code) blk = code fields = fields or [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] for c, q, bit_idx, pos in plan: bit = ((part >> bit_idx) & 1) if c < 0 else ((fields[c][q] >> bit_idx) & 1) if bit: blk |= 1 << pos pos = hdr indices = [0] * 16 if indices is None else indices if reg == 1: for p in range(16): nb = 3 if p == 0 else 4 blk |= (indices[p] & ((1 << nb) - 1)) << pos pos += nb else: anchor1 = ttpf._bptc.load_bc7()["anchor2"][part] for p in range(16): nb = 2 if (p == 0 or p == anchor1) else 3 blk |= (indices[p] & ((1 << nb) - 1)) << pos pos += nb assert pos == 128, f"BC6H mode {code:#x} ended at bit {pos}" return blk.to_bytes(16, "little") def solid_bc6h(r, g, b, *, signed=False): """Mode 0x0F (16-bit base, zero delta) solid BC6H block for float16 RGB.""" fields = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] for c, v in enumerate((r, g, b)): neg = signed and v < 0 hv = struct.unpack(" 0: off |= (((x + mip["tile_x"]) >> (s - 1)) & 1) << bit elif s < 0: off |= (((y + mip["tile_y"]) >> (-s - 1)) & 1) << bit at = (y * mip["blocks_w"] + x) * eb out[mip_off + off:mip_off + off + eb] = linear[at:at + eb] def retile_volume(layout, linear): eb = layout["element_bytes"] pat = ttpf.VOLUME_PATTERNS_4K[{1: 0, 2: 1, 4: 2, 8: 3, 16: 4}[eb]] bw, bh, bd = layout["block_w"], layout["block_h"], layout["block_d"] mip = layout["mips"][0] w, h, d = mip["blocks_w"], mip["blocks_h"], mip["blocks_d"] tiles_x = mip["padded_w"] // bw out = bytearray(layout["size"]) for z in range(d): z_base = (z // bd) * layout["z_tile_stride"] z_in_tile = 0 for bit, s in enumerate(pat): if s >= 101: z_in_tile |= ((z >> (s - 101)) & 1) << bit for y in range(h): y_tile = (y // bh) * tiles_x y_in_tile = z_in_tile for bit, s in enumerate(pat): if s < 0: y_in_tile |= ((y >> (-s - 1)) & 1) << bit for x in range(w): off = z_base + (y_tile + x // bw) * layout["tile"] + y_in_tile for bit, s in enumerate(pat): if 0 < s < 100: off |= ((x >> (s - 1)) & 1) << bit at = ((z * h + y) * w + x) * eb out[off:off + eb] = linear[at:at + eb] return bytes(out) def solid_chain(layout, fmt=71, color_fn=None): """Build block-linear bytes for every mip; each block is one solid BC colour.""" eb = layout["element_bytes"] out = {} is_hdr = fmt in (95, 96) # Smooth gradient at the top level; every lower mip is the 2x2 block mean. top = layout["mips"][0] colours = {} for y in range(top["blocks_h"]): for x in range(top["blocks_w"]): if color_fn is not None: colours[(0, x, y)] = color_fn(x, y, top["blocks_w"], top["blocks_h"]) elif fmt == 95: colours[(0, x, y)] = (x * 16.0 / max(1, top["blocks_w"] - 1), y * 8.0 / max(1, top["blocks_h"] - 1), 1.5) elif fmt == 96: colours[(0, x, y)] = (-4.0 + x * 8.0 / max(1, top["blocks_w"] - 1), -2.0 + y * 4.0 / max(1, top["blocks_h"] - 1), 0.5) else: colours[(0, x, y)] = (min(255, x * 255 // max(1, top["blocks_w"] - 1)), min(255, y * 255 // max(1, top["blocks_h"] - 1)), 128) for m in range(1, len(layout["mips"])): pm = layout["mips"][m - 1] cm = layout["mips"][m] for y in range(cm["blocks_h"]): for x in range(cm["blocks_w"]): acc = [0.0, 0.0, 0.0] if is_hdr else [0, 0, 0] n = 0 for dy in range(2): for dx in range(2): px, py = min(pm["blocks_w"] - 1, 2 * x + dx), min(pm["blocks_h"] - 1, 2 * y + dy) q = colours[(m - 1, px, py)] for k in range(3): acc[k] += q[k] n += 1 colours[(m, x, y)] = tuple((a / n) if is_hdr else (a // n) for a in acc) for mip in layout["mips"]: buf = bytearray(mip["blocks_w"] * mip["blocks_h"] * eb) for y in range(mip["blocks_h"]): for x in range(mip["blocks_w"]): at = (y * mip["blocks_w"] + x) * eb c = colours[(mip["index"], x, y)] if fmt == 80: blk = solid_bc4(c[0]) elif fmt == 83: blk = solid_bc5(c[0], c[1]) elif fmt in (95, 96): blk = solid_bc6h(c[0], c[1], c[2], signed=(fmt == 96)) elif fmt in (98, 99): blk = solid_bc7(c[0], c[1], c[2]) else: blk = solid_bc1(c)[:eb].ljust(eb, b"\0") buf[at:at + eb] = blk out[mip["index"]] = bytes(buf) return out def write_rds5_pool(path, records, chunks, area=None): """Write a synthetic RDS5/ETPF pool with one or more zlib-compressed chunks. `area` is the size of the record region in bytes. The default, len(records) * 24, fits every 8-byte asset key after the 16-byte descriptors. A smaller area (at least len(records) * 16) lets the key array continue into the 32-byte ID region, as most retail pools do.""" count = len(chunks) + 2 table_end = 24 + (count - 1) * 8 etpf = ttpf.align(table_end, 256) area = len(records) * 24 if area is None else area metadata = 16 + area + len(records) * 32 padded = ttpf.align(metadata, 4096) source = etpf + padded virt = padded entries = [] packed_chunks = [] for raw in chunks: assert len(raw) % 4096 == 0 and len(raw) > 0 packed_payload = zlib.compress(raw, 9) pad = (-len(packed_payload)) % 256 entries.append((source // 256) | (8 << 26) | ((virt // 4096) << 30) | (pad << 52)) packed_chunks.append((source, packed_payload)) source += len(packed_payload) + pad virt += len(raw) vsize = virt entries.append((source // 256) | ((vsize // 4096) << 30)) header = bytearray(24) header[0:4] = b"RDS5" struct.pack_into(" c1=black: index 0 -> white, index 3 -> (c0 + 2*c1)/3 white = ttpf.decode_pixels(71, struct.pack(" e1=70): index 0 -> 210, index 1 -> 70, index 2 -> (6*210+70)//7 = 190 bits8 = 0 | (1 << 3) | (2 << 6) blk8 = bytes([210, 70]) + bits8.to_bytes(6, "little") px8 = ttpf.decode_pixels(80, blk8, 1, 1) self.assertEqual(px8[0], (210.0, 210.0, 210.0)) self.assertEqual(px8[1], (70.0, 70.0, 70.0)) self.assertEqual(px8[2], (190.0, 190.0, 190.0)) # BC4 6-value mode (e0=50 <= e1=150): index 6 -> 0, index 7 -> 255 bits6 = 0 | (1 << 3) | (6 << 6) | (7 << 9) blk6 = bytes([50, 150]) + bits6.to_bytes(6, "little") px6 = ttpf.decode_pixels(80, blk6, 1, 1) self.assertEqual(px6[0], (50.0, 50.0, 50.0)) self.assertEqual(px6[1], (150.0, 150.0, 150.0)) self.assertEqual(px6[2], (0.0, 0.0, 0.0)) self.assertEqual(px6[3], (255.0, 255.0, 255.0)) # BC5 (fmt=83): channel R from first BC4 block, channel G from second, B=0.0 px5 = ttpf.decode_pixels(83, blk8 + blk6, 1, 1) self.assertEqual(px5[0], (210.0, 50.0, 0.0)) self.assertEqual(px5[1], (70.0, 150.0, 0.0)) self.assertEqual(px5[2], (190.0, 0.0, 0.0)) self.assertEqual(px5[3], (210.0, 255.0, 0.0)) def test_block_means_sub_block_clamping(self): # 4x4 block where only the top-left 2x2 texels are active; remaining 12 are zero-padded. pixels = [] for y in range(4): for x in range(4): pixels.append((200.0, 100.0, 50.0) if (x < 2 and y < 2) else (0.0, 0.0, 0.0)) clamped = ttpf.block_means(pixels, 1, 1, pixels_w=2, pixels_h=2) self.assertEqual(clamped, [(200.0, 100.0, 50.0)]) unclamped = ttpf.block_means(pixels, 1, 1) self.assertEqual(unclamped, [(50.0, 25.0, 12.5)]) def test_bc7_all_eight_modes_and_reserved(self): tab = ttpf._bptc.load_bc7() # Reserved mode (b0 == 0): decodes to all-zero black. self.assertEqual(ttpf.decode_pixels(98, b"\x00" * 16, 1, 1), [(0.0, 0.0, 0.0)] * 16) self.assertEqual(ttpf.decode_pixels(99, b"\x00" * 16, 1, 1), [(0.0, 0.0, 0.0)] * 16) # Mode 0: 3 subsets, 4-bit endpoints + per-endpoint p-bit -> 5-bit expanded via MSB rep. # Subset 0: (15,0,0) p=1 -> (255,8,8); Subset 1: (0,15,0) p=0 -> (0,247,0); Subset 2: (0,0,15) p=1 -> (8,8,255). blk0 = pack_bc7_block( 0, part=0, r=(15, 15, 0, 0, 0, 0), g=(0, 0, 15, 15, 0, 0), b=(0, 0, 0, 0, 15, 15), pbits=(1, 1, 0, 0, 1, 1), ) px0 = ttpf.decode_pixels(98, blk0, 1, 1) exp0 = [(255.0, 8.0, 8.0), (0.0, 247.0, 0.0), (8.0, 8.0, 255.0)] for p, sub in enumerate(tab["partition3"][0]): self.assertEqual(px0[p], exp0[sub], f"mode 0 pixel {p}") # Mode 1: 2 subsets, 6-bit endpoints + shared per-subset p-bit -> 7-bit expanded. # Subset 0: r=0..63, p=1 -> 2..255; Subset 1: g=32..32, p=0 -> 129..129. # Index 3 on 3-bit table has weight 27 -> ((37*2 + 27*255 + 32) >> 6) = 109. idx1 = [3 if p == 1 else 0 for p in range(16)] blk1 = pack_bc7_block( 1, part=0, r=(0, 63, 0, 0), g=(0, 0, 32, 32), b=(0, 0, 0, 0), pbits=(1, 0), idx1=idx1, ) px1 = ttpf.decode_pixels(98, blk1, 1, 1) self.assertEqual(px1[0], (2.0, 2.0, 2.0)) self.assertEqual(px1[1], (109.0, 2.0, 2.0)) self.assertEqual(px1[2], (0.0, 129.0, 0.0)) # partition2[0][2] == 1 # Mode 2: 3 subsets, 5-bit endpoints (no p-bits), 2-bit indices. blk2 = pack_bc7_block( 2, part=1, r=(31, 0, 16, 16, 0, 0), g=(0, 0, 0, 0, 31, 31), b=(0, 31, 0, 0, 0, 0), idx1=[3 if p == 1 else 0 for p in range(16)], ) px2 = ttpf.decode_pixels(98, blk2, 1, 1) self.assertEqual(px2[0], (255.0, 0.0, 0.0)) # pixel 1 is subset 0 in partition3[1]; idx=3 -> weight 64 -> (0, 0, 255) self.assertEqual(tab["partition3"][1][1], 0) self.assertEqual(px2[1], (0.0, 0.0, 255.0)) # Mode 3: 2 subsets, 7-bit + per-endpoint p-bit (exact 8-bit), 2-bit indices. # Subset 0: R=0..255, idx=1 (w=21) -> (21*255 + 32)>>6 = 84. blk3 = pack_bc7_block( 3, part=0, r=(0, 127, 50, 50), g=(0, 0, 100, 100), b=(0, 0, 0, 0), pbits=(0, 1, 0, 0), idx1=[1 if p == 1 else 0 for p in range(16)], ) px3 = ttpf.decode_pixels(98, blk3, 1, 1) self.assertEqual(px3[0], (0.0, 0.0, 0.0)) self.assertEqual(px3[1], (84.0, 0.0, 0.0)) self.assertEqual(px3[2], (100.0, 200.0, 0.0)) # Mode 4: 1 subset, 5-bit color, 6-bit alpha, rotation + index selection (isel=0 and isel=1). # Color endpoints: R=0..255, G=0..0, B=0..0. Alpha endpoints: A=0..255. # idx1[1]=1 (2-bit, w=21 -> 84), idx2[1]=4 (3-bit, w=37 -> 147). for rot, isel, expected in ( (0, 0, (84.0, 0.0, 0.0)), # color uses idx1 (84), no swap (0, 1, (147.0, 0.0, 0.0)), # color uses idx2 (147), no swap (1, 0, (147.0, 0.0, 0.0)), # rot=1 swaps R and A -> R gets alpha(idx2=147) (2, 0, (84.0, 147.0, 0.0)), # rot=2 swaps G and A -> G gets alpha(147) (3, 1, (147.0, 0.0, 84.0)), # isel=1: color=idx2(147), alpha=idx1(84), rot=3 puts A into B ): blk4 = pack_bc7_block( 4, rot=rot, isel=isel, r=(0, 31), g=(0, 0), b=(0, 0), a=(0, 63), idx1=[1 if p == 1 else 0 for p in range(16)], idx2=[4 if p == 1 else 0 for p in range(16)], ) px4 = ttpf.decode_pixels(98, blk4, 1, 1) self.assertEqual(px4[1], expected, f"mode 4 rot={rot} isel={isel}") # Mode 5: 1 subset, 7-bit color, 8-bit alpha, rotation, 2-bit color & alpha indices. blk5 = pack_bc7_block( 5, rot=2, r=(64, 64), g=(0, 0), b=(16, 16), a=(0, 200), idx1=[0] * 16, idx2=[3 if p == 5 else 0 for p in range(16)], ) px5 = ttpf.decode_pixels(98, blk5, 1, 1) self.assertEqual(px5[0], (129.0, 0.0, 32.0)) self.assertEqual(px5[5], (129.0, 200.0, 32.0)) # rot=2 swaps A into G # Mode 6: 1 subset, 7-bit RGBA + per-endpoint p-bit (exact 8-bit), 4-bit indices. # idx=5 has weight 21 -> ((43*20 + 21*220 + 32) >> 6) = 86. blk6 = pack_bc7_block( 6, r=(10, 110), g=(0, 0), b=(127, 127), a=(127, 127), pbits=(0, 0), idx1=[5 if p == 3 else 0 for p in range(16)], ) px6 = ttpf.decode_pixels(99, blk6, 1, 1) self.assertEqual(px6[0], (20.0, 0.0, 254.0)) self.assertEqual(px6[3], (86.0, 0.0, 254.0)) # Mode 7: 2 subsets, 5-bit RGBA + per-endpoint p-bit -> 6-bit expanded, 2-bit indices. blk7 = pack_bc7_block( 7, part=0, r=(31, 31, 0, 0), g=(0, 0, 31, 31), b=(0, 0, 0, 0), a=(31, 31, 31, 31), pbits=(1, 1, 1, 1), ) px7 = ttpf.decode_pixels(98, blk7, 1, 1) self.assertEqual(px7[0], (255.0, 4.0, 4.0)) self.assertEqual(px7[2], (4.0, 255.0, 4.0)) def test_bc6h_modes_uf16_sf16_and_reserved(self): lut = ttpf._f16_lut() # Reserved modes (0x13, 0x17, 0x1B, 0x1F) decode to zero. for bad_mode in (0x13, 0x17, 0x1B, 0x1F): blk_bad = bytes([bad_mode]) + b"\x00" * 15 self.assertEqual(ttpf.decode_pixels(95, blk_bad, 1, 1), [(0.0, 0.0, 0.0)] * 16) self.assertEqual(ttpf.decode_pixels(96, blk_bad, 1, 1), [(0.0, 0.0, 0.0)] * 16) # Mode 0x03 (1-region untransformed 10-bit): UF16 max endpoint 1023 -> 0xFFFF -> 0x7BF7 -> ~65024.0 blk03 = pack_bc6h_block(0x03, fields=[[0, 1023, 0, 0], [512, 512, 0, 0], [0, 0, 0, 0]], indices=[0, 15] + [0] * 14) px03_u = ttpf.decode_pixels(95, blk03, 1, 1) self.assertEqual(px03_u[0][0], 0.0) self.assertAlmostEqual(px03_u[1][0], lut[(0xFFFF * 31) >> 6], places=5) self.assertGreater(px03_u[0][1], 1.0) # Same bit pattern as SF16: 1023 -> -1 (negative!), 512 -> -512 (max negative magnitude -> -65504.0). px03_s = ttpf.decode_pixels(96, blk03, 1, 1) self.assertEqual(px03_s[0][0], 0.0) self.assertLess(px03_s[1][0], 0.0) self.assertEqual(px03_s[0][1], -65504.0) # Mode 0x0B (1-region transformed 12-bit base + 8-bit delta, reversed rw[11:10]). # e0 = 0x900 (2304, uses both bits 11 and 8), delta = -16 -> e1 = 2288. blk0b = pack_bc6h_block(0x0B, fields=[[0x900, (-16) & 0xFF, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], indices=[0, 15] + [0] * 14) px0b = ttpf.decode_pixels(95, blk0b, 1, 1) exp_e0 = ttpf._bc6h_finish(ttpf._bc6h_unquantize(0x900, 12, False), False, lut) exp_e1 = ttpf._bc6h_finish(ttpf._bc6h_unquantize(0x8F0, 12, False), False, lut) self.assertEqual(px0b[0][0], exp_e0) self.assertEqual(px0b[1][0], exp_e1) # Mode 0x0F (1-region transformed 16-bit base + 4-bit delta, reversed rw[15:10]). # Test exact round-trip of large positive UF16 and negative SF16 via solid_bc6h, # plus -32768 clamp in SF16 -> finite -65504.0 (never -inf/NaN). px_hi = ttpf.decode_pixels(95, solid_bc6h(128.0, 0.5, 0.0, signed=False), 1, 1) self.assertAlmostEqual(px_hi[0][0], 128.0, places=2) self.assertAlmostEqual(px_hi[0][1], 0.5, places=3) px_neg = ttpf.decode_pixels(96, solid_bc6h(-12.5, 3.25, -0.25, signed=True), 1, 1) self.assertAlmostEqual(px_neg[0][0], -12.5, places=2) self.assertAlmostEqual(px_neg[0][1], 3.25, places=2) self.assertAlmostEqual(px_neg[0][2], -0.25, places=3) blk_min16 = pack_bc6h_block(0x0F, fields=[[0x8000, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) px_min16 = ttpf.decode_pixels(96, blk_min16, 1, 1) self.assertEqual(px_min16[0][0], -65504.0) # Mode 0x02 (2-region transformed 11-bit base, deltas (5,4,4)) with partition 1. # Subset 0: R=1000, Subset 1: R=1000+10=1010..1000-6=994. blk02 = pack_bc6h_block( 0x02, part=1, fields=[[1000, 0, 10, (-6) & 0x1F], [0, 0, 0, 0], [0, 0, 0, 0]], indices=[0] * 16, ) px02 = ttpf.decode_pixels(95, blk02, 1, 1) sm1 = ttpf._bptc.load_bc7()["partition2"][1] exp_s0 = ttpf._bc6h_finish(ttpf._bc6h_unquantize(1000, 11, False), False, lut) exp_s1 = ttpf._bc6h_finish(ttpf._bc6h_unquantize(1010, 11, False), False, lut) for p, sub in enumerate(sm1): self.assertEqual(px02[p][0], exp_s0 if sub == 0 else exp_s1, f"mode 0x02 pixel {p}") # Mode 0x1E (2-region untransformed 6-bit) in SF16: negative subset 1 endpoint. blk1e = pack_bc6h_block( 0x1E, part=0, fields=[[16, 16, (-16) & 0x3F, (-16) & 0x3F], [0, 0, 0, 0], [0, 0, 0, 0]], indices=[0] * 16, ) px1e = ttpf.decode_pixels(96, blk1e, 1, 1) self.assertGreater(px1e[0][0], 0.0) self.assertEqual(px1e[2][0], -px1e[0][0]) def test_hdr_tonemap_metric(self): same = [(0.0, 1.0, 500.0), (1000.0, 0.25, 65504.0)] self.assertEqual(ttpf.mean_abs_error(same, same, hdr=True), 0.0) # Bounded even on extreme HDR highlights. hi_err = ttpf.mean_abs_error([(0.0, 0.0, 0.0)], [(65504.0, 65504.0, 65504.0)], hdr=True) self.assertGreater(hi_err, 250.0) self.assertLess(hi_err, 255.0) # Signed sensitivity for SF16: -1.0 -> -127.5, +1.0 -> +127.5 -> diff = 255.0. sign_err = ttpf.mean_abs_error([(-1.0, -1.0, -1.0)], [(1.0, 1.0, 1.0)], hdr=True) self.assertAlmostEqual(sign_err, 255.0, places=5) class Bc2AndBc3CheckTest(unittest.TestCase): def run_check(self, fmt, lower_colour): import io from contextlib import redirect_stdout # Black then white, selector 3: the high-resolution image is uniform gray 170. top_colour = struct.pack(" None: # Explicit alpha bytes precede the color block. Black comes before # white, but BC2 still uses both interpolated colors. block = struct.pack(" black): selector 1 = black, selector 0 = white. BC1_LEFT_BLACK = struct.pack(" 3x2 blocks: a flat zip of 2x1 predictions would misalign rows. ((20, 12), bc1_column(4), bc1_column(2), 0.0), ((20, 12), bc1_column(4), bc1_black, 85.0), # Child texels 6-7 are padding: only the visible half of the edge block counts. ((12, 8), bc1_black, bc1_column(1, BC1_LEFT_BLACK), 0.0), ((12, 8), bc1_black, bc1_column(1, BC1_LEFT_WHITE), 127.5), # Parent texels 10-11 are padding and must not leak into child texel 4. ((10, 8), bc1_column(2, BC1_LEFT_BLACK), bc1_black, 0.0), ((10, 8), bc1_column(2, BC1_LEFT_WHITE), bc1_black, 127.5), ) for (w, h), parent_fn, child_fn, expected in cases: with self.subTest(size=(w, h), expected=expected): self.assertAlmostEqual(pair_error(odd_mip(0, w, h), parent_fn, odd_mip(1, w // 2, h // 2), child_fn), expected, places=9) def test_matches_block_downsample_on_power_of_two_chains(self): rng = random.Random(1) for w, h in ((16, 8), (8, 8), (4, 4), (2, 2), (1, 1)): with self.subTest(size=(w, h)): parent = odd_mip(0, w, h) child = odd_mip(1, max(1, w // 2), max(1, h // 2)) bw, bh = parent["blocks_w"], parent["blocks_h"] data = bytes(rng.randrange(256) for _ in range(8 * bw * bh)) px = ttpf.decode_pixels(71, data, bw, bh) old = ttpf.downsample2(ttpf.block_means(px, bw, bh, w, h), bw, bh) new = ttpf.predict_child_means(px, parent, child) self.assertEqual(len(new), len(old)) for a, b in zip(new, old): for x, y in zip(a, b): self.assertAlmostEqual(x, y, places=9) def test_matches_nested_list_box_filter_on_odd_sizes(self): rng = random.Random(2) for w, h in ((28, 20), (22, 14), (6, 1), (1, 6)): with self.subTest(size=(w, h)): parent = odd_mip(0, w, h) child = odd_mip(1, max(1, w // 2), max(1, h // 2)) bw, bh = parent["blocks_w"], parent["blocks_h"] px = ttpf.decode_pixels(71, bytes(rng.randrange(256) for _ in range(8 * bw * bh)), bw, bh) grid = [[px[y * bw * 4 + x] for x in range(w)] for y in range(h)] cw, ch = child["pixels_w"], child["pixels_h"] texels = [[tuple(sum(grid[min(h - 1, 2 * y + dy)][min(w - 1, 2 * x + dx)][k] for dy in (0, 1) for dx in (0, 1)) / 4 for k in range(3)) for x in range(cw)] for y in range(ch)] expected = [] for by in range(0, ch, 4): for bx in range(0, cw, 4): cell = [t for row in texels[by:by + 4] for t in row[bx:bx + 4]] expected.append(tuple(sum(t[k] for t in cell) / len(cell) for k in range(3))) got = ttpf.predict_child_means(px, parent, child) self.assertEqual(len(got), child["blocks_w"] * child["blocks_h"]) for a, b in zip(got, expected): for x, y in zip(a, b): self.assertAlmostEqual(x, y, places=9) def test_mismatched_geometry_is_rejected(self): parent = odd_mip(0, 12, 8) px = ttpf.decode_pixels(71, BC1_BLACK * 6, 3, 2) for child in (odd_mip(1, 5, 4), odd_mip(1, 6, 3), dict(odd_mip(1, 6, 4), blocks_w=1)): with self.subTest(child=child), self.assertRaises(ttpf.MipGeometryError): ttpf.predict_child_means(px, parent, child) with self.assertRaises(ttpf.MipGeometryError): ttpf.predict_child_means(px[:-1], parent, odd_mip(1, 6, 4)) with self.assertRaises(ttpf.MipGeometryError): ttpf.visible_block_means([(0.0, 0.0, 0.0)], odd_mip(1, 6, 4)) with self.assertRaises(ValueError): ttpf.mean_abs_error([(0.0, 0.0, 0.0)], [(0.0, 0.0, 0.0)] * 2) def linear_layout(sizes, first_tail=0, depth=1): """Fake layout storing each mip block-linear, mip-major across `depth` slices.""" mips, off = [], 0 for i, (w, h) in enumerate(sizes): m = odd_mip(i, w, h) m.update(offset=off, region=8 * m["blocks_w"] * m["blocks_h"], tile_x=0, tile_y=0) m["slice_stride"] = m["region"] mips.append(m) off += m["region"] * depth return dict(mips=mips, size=off, slice_size=off // depth, face_size=off, tile=4096, first_tail=first_tail, element_bytes=8) def run_shape_check(shape, sizes, chunks, first_tail=0, depth=1): layout = linear_layout(sizes, first_tail, depth) if shape == "cubemap": layout.update(data_offset=0, full_bytes=6 * layout["size"], snapshots=1) w, h = sizes[0] d = dict(base=0, format=71, width=w, height=h, depth=depth, mips=len(sizes), non_tail=0) a = dict(layout=layout, descriptor=d, available=True, full_chain=True) if shape != "2d": a["shape"] = shape tiled = b"".join(chunks) + bytes(1 << 17) buf = io.StringIO() with mock.patch.object(ttpf, "analyze", lambda pool, index: a), \ mock.patch.object(ttpf, "_read_range_or_block", lambda *args, **kw: tiled), \ mock.patch.object(ttpf, "detile", lambda lay, mip, data: bytes( data[:8 * mip["blocks_w"] * mip["blocks_h"]])), \ redirect_stdout(buf): code = ttpf.check(None, 0) return code, json.loads(buf.getvalue()) class OddBlockShapeCheckTest(unittest.TestCase): """check() on 12x8 -> 6x4 BC1 (3x2 parent blocks) for each 2D-family shape. The container and tiling are faked (block-linear mips); OddBlockPoolTest covers the real path.""" SIZES = ((12, 8), (6, 4)) def chunk(self, level, block_fn): return bc1_blocks(odd_mip(level, *self.SIZES[level]), block_fn) def test_2d_edge_column_and_tail_origin(self): parent = self.chunk(0, bc1_column(2)) code, result = run_shape_check("2d", self.SIZES, [parent, self.chunk(1, bc1_column(1))], first_tail=1) self.assertEqual(code, 0, result) self.assertEqual(result["chain_error"], [0.0]) # The fake tile-origin read sees parent blocks 0-1 (black) where the white edge belongs. self.assertEqual(result["tail_origin_control"], 127.5) code, result = run_shape_check("2d", self.SIZES, [parent, self.chunk(1, bc1_black)], first_tail=1) self.assertEqual(code, 1, result) self.assertEqual(result["chain_error"], [127.5]) def test_array_reports_the_slice_with_a_dropped_edge(self): parents = [self.chunk(0, bc1_column(2))] * 3 children = [self.chunk(1, bc1_column(1)), self.chunk(1, bc1_black), self.chunk(1, bc1_column(1))] code, result = run_shape_check("2d_array", self.SIZES, parents + children, depth=3) self.assertEqual(code, 1, result) self.assertEqual(result["per_slice_max_error"], [0.0, 127.5, 0.0]) self.assertEqual(result["max_error"], 127.5) def test_cube_reports_the_face_with_a_dropped_edge_and_tail_origin(self): chunks = [] for face in range(6): chunks += [self.chunk(0, bc1_column(2)), self.chunk(1, bc1_black if face == 4 else bc1_column(1))] code, result = run_shape_check("cubemap", self.SIZES, chunks, first_tail=1) self.assertEqual(code, 1, result) self.assertEqual(result["per_face_max_error"], [0.0, 0.0, 0.0, 0.0, 127.5, 0.0]) self.assertEqual(result["tail_origin_control"], 127.5) def test_mismatched_mip_geometry_is_blocked(self): sizes = ((12, 8), (5, 4)) chunks = [bc1_blocks(odd_mip(0, 12, 8), bc1_black), bc1_blocks(odd_mip(1, 5, 4), bc1_black)] code, result = run_shape_check("2d", sizes, chunks) self.assertEqual(code, 2, result) self.assertIn("geometry", result["blocked"]) def odd_bc1_pool(path, width, height, parent_fn, child_fn): """Write a two-mip BC1 pool through the real layout and tiling.""" layout = ttpf.surface_layout(width, height, 71, 2, 4096) payload = bytearray(layout["size"]) for mip, block_fn in zip(layout["mips"], (parent_fn, child_fn)): retile_to(layout, mip, bc1_blocks(mip, block_fn), payload) non_tail = layout["first_tail"] record = pack_etpf_record(width, height, 2 - non_tail, non_tail, 71, 1, 1, 2, 4096) write_rds5_pool(path, [record], [payload]) class OddBlockPoolTest(unittest.TestCase): """Before the fix the changed 12x8, 8x12 and 12x12 children passed with error 0.""" CASES = ( # (width, height, parent, child, rc, chain_error) (12, 8, bc1_black, bc1_black, 0, [0.0]), (12, 8, bc1_black, bc1_single(1, 0), 1, [127.5]), (8, 12, bc1_black, bc1_black, 0, [0.0]), (8, 12, bc1_black, bc1_single(0, 1), 1, [127.5]), (12, 12, bc1_black, bc1_black, 0, [0.0]), (12, 12, bc1_black, bc1_single(1, 1), 1, [63.75]), (12, 8, bc1_column(2), bc1_column(1), 0, [0.0]), (16, 8, bc1_black, bc1_black, 0, [0.0]), (16, 8, bc1_black, bc1_single(1, 0), 1, [127.5]), (256, 256, bc1_black, bc1_black, 0, [0.0]), (256, 256, bc1_black, bc1_single(31, 31), 0, [0.25]), ) def run_check(self, *case): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "odd.ttpf") odd_bc1_pool(path, *case) pool = ttpf.Pool(path) buf = io.StringIO() try: with redirect_stdout(buf): code = ttpf.check(pool, 0) finally: pool.close() return code, json.loads(buf.getvalue()) def test_odd_block_counts_cover_every_child_block(self): for w, h, parent_fn, child_fn, rc, errs in self.CASES: with self.subTest(size=(w, h), rc=rc, chain_error=errs): code, result = self.run_check(w, h, parent_fn, child_fn) self.assertEqual(code, rc, result) self.assertEqual(result["chain_error"], errs) def test_cli_rejects_a_dropped_edge_block(self): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "odd.ttpf") odd_bc1_pool(path, 12, 8, bc1_black, bc1_single(1, 0)) proc = subprocess.run([sys.executable, "-I", TOOL, "check", path, "0"], capture_output=True, text=True, check=False) self.assertEqual(proc.returncode, 1, proc.stdout + proc.stderr) result = json.loads(proc.stdout) self.assertEqual(result["max_error"], 127.5) self.assertTrue(result["full_chain"]) class VisibleExportTest(unittest.TestCase): """Before the fix 17x13 BC1 exported its 2x2-block child as 3x2 storage blocks (208 bytes).""" RED = (255, 0, 0) def test_export_packs_visible_blocks_only(self): def child(x, y): return solid_bc1(self.RED) if y == 1 else bc1_black(x, y) for w, h in ((17, 13), (17, 16), (16, 13), (16, 16)): with self.subTest(size=(w, h)), tempfile.TemporaryDirectory() as tmp: path, out = os.path.join(tmp, "edge.ttpf"), os.path.join(tmp, "edge.dds") odd_bc1_pool(path, w, h, bc1_black, child) pool = ttpf.Pool(path) try: with redirect_stdout(io.StringIO()): self.assertEqual(ttpf.export(pool, 0, out), 0) finally: pool.close() with open(out, "rb") as f: blob = f.read() pw, ph, cw, ch = (w + 3) // 4, (h + 3) // 4, (w // 2 + 3) // 4, (h // 2 + 3) // 4 self.assertEqual(len(blob) - 148, 8 * (pw * ph + cw * ch)) self.assertEqual(struct.unpack_from(" None: layout = self.layout linear = solid_chain(layout) payload = bytearray(layout["size"]) for mip in layout["mips"]: retile_to(layout, mip, linear[mip["index"]], payload) top_start = layout["mips"][0]["offset"] path = os.path.join(self.tmp.name, "partially-readable.ttpf") record = pack_etpf_record(256, 256, 6, 1, 71, 1, 1, 4, 4096, meta=0x103) write_rds5_pool(path, [record], [payload[:top_start], payload[top_start:]]) # Give the lower mip chunk an unsupported codec. The second chunk is still valid zlib. with open(path, "r+b") as f: f.seek(24) entry, = struct.unpack(" None: # Room for exactly one key after the descriptors: the other two sit in the ID region. with tempfile.TemporaryDirectory() as tmp: path = self.write(tmp, "straddle.ttpf", area=self.RECORDS * 16 + 8) self.assertEqual(self.keys(path), [0x1234567890ABCDEF + i for i in range(self.RECORDS)]) def test_keys_inside_the_record_area_are_unchanged(self) -> None: with tempfile.TemporaryDirectory() as tmp: path = self.write(tmp, "plain.ttpf") self.assertEqual(self.keys(path), [0x1234567890ABCDEF + i for i in range(self.RECORDS)]) def test_a_key_split_by_the_records_ids_boundary_is_refused(self) -> None: # A record area that cuts the first key in half, as TtpfReader refuses it. with tempfile.TemporaryDirectory() as tmp: path = self.write(tmp, "split.ttpf", area=self.RECORDS * 16 + 4) with closing(ttpf.Pool(path)) as pool: with self.assertRaises(ValueError): pool.descriptor(0) class PoolCacheTest(unittest.TestCase): def test_zlib_chunk_rejects_a_decoded_size_larger_than_its_range(self) -> None: with tempfile.TemporaryDirectory() as temp: path = os.path.join(temp, "pool.ttpf") record = pack_etpf_record(4, 4, 1, 0, 71, 1, 1, 4, 4096) write_rds5_pool(path, [record], [bytes(8192)]) with open(path, "r+b") as stream: stream.seek(8) stream.write(struct.pack(" None: with tempfile.TemporaryDirectory() as temp, ExitStack() as stack: record = pack_etpf_record(4, 4, 1, 0, 71, 1, 1, 4, 4096) pools = [] payloads = (bytes((0x11,)) * 4096, bytes((0x22,)) * 4096) for i, payload in enumerate(payloads): path = os.path.join(temp, f"pool_{i}.ttpf") write_rds5_pool(path, [record], [payload]) pools.append(stack.enter_context(closing(ttpf.Pool(path)))) self.assertEqual(pools[0].chunks, pools[1].chunks) cache = {} for pool, payload in zip(pools, payloads): self.assertEqual(pool.read_range(4096, 4096, cache), payload) self.assertEqual(pools[0].read_range(4096, 4096, cache), payloads[0]) class SyntheticBcpackPoolTest(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) def test_bc4_and_bc5_decode_pixels(self): bc4_blk = bytes([200, 100]) + (0).to_bytes(6, "little") px4 = ttpf.decode_pixels(80, bc4_blk, 1, 1) self.assertEqual(len(px4), 16) self.assertEqual(px4[0], (200.0, 200.0, 200.0)) bc5_blk = bc4_blk + bytes([50, 25]) + (0).to_bytes(6, "little") px5 = ttpf.decode_pixels(83, bc5_blk, 1, 1) self.assertEqual(len(px5), 16) self.assertEqual(px5[0], (200.0, 50.0, 0.0)) def test_bc1_bcpack_pool_check_export_and_scan(self): import io from contextlib import redirect_stdout path = os.path.join(self.tmp.name, "bc1_bcpack.ttpf") layout, linear, expected_tiled = build_bc1_bcpack_pool(path) pool = ttpf.Pool(path) self.addCleanup(pool.close) a = ttpf.analyze(pool, 0) self.assertTrue(a["full_chain"]) self.assertEqual(a["mip_codecs"][0], [1]) with self.assertRaises(ttpf.BcpackRequired): pool.read_range(a["descriptor"]["base"], layout["size"], bcpack_mode="off") self.assertEqual(pool.read_range(a["descriptor"]["base"], layout["size"]), expected_tiled) buf = io.StringIO() with redirect_stdout(buf): code = ttpf.check(pool, 0) res = json.loads(buf.getvalue()) self.assertEqual(code, 0, res) self.assertLess(res["max_error"], 4.0) out_dds = os.path.join(self.tmp.name, "bc1_bcpack.dds") with redirect_stdout(io.StringIO()): self.assertEqual(ttpf.export(pool, 0, out_dds), 0) with open(out_dds, "rb") as f: dds = f.read() self.assertEqual(dds[148:], b"".join(linear[m["index"]] for m in layout["mips"])) scan_out = os.path.join(self.tmp.name, "scan_bcpack.json") with redirect_stdout(io.StringIO()): ttpf.scan_pools([path], scan_out) with open(scan_out, encoding="utf-8") as f: total = json.load(f)["total"] self.assertEqual((total["after_full"], total["after_skip"]), (0, 1)) self.assertEqual((total["after_bcpack_full"], total["after_bcpack_skip"], total["bcpack_unlocked_full"]), (1, 0, 1)) def test_provisional_history_strict_refusal_and_opt_in(self): import io from contextlib import redirect_stdout path = os.path.join(self.tmp.name, "bc1_prov.ttpf") layout = ttpf.surface_layout(64, 64, 71, 5, ttpf.tile_bytes(4)) nblocks = layout["size"] // 8 # Block 0 references provisional entry 6 (256 + 0 - 250 = 6). build_bc1_bcpack_pool(path, ref_dists=[250] + [0] * (nblocks - 1)) pool = ttpf.Pool(path) self.addCleanup(pool.close) buf = io.StringIO() with redirect_stdout(buf): code_strict = ttpf.check(pool, 0) self.assertEqual(code_strict, 2) self.assertIn("provisional BCPack history required", json.loads(buf.getvalue())["blocked"]) buf2 = io.StringIO() with redirect_stdout(buf2): code_prov = ttpf.check(pool, 0, allow_provisional=True) self.assertEqual(code_prov, 0, buf2.getvalue()) def test_bc7_bcpack_pool_export_and_scan(self): import io from contextlib import redirect_stdout sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import test_bcpack as tb # 64x64 DXGI 98 (BC7_UNORM, 16-byte blocks), 1 mip, 4 KiB tiles, codec 7. layout = ttpf.surface_layout(64, 64, 98, 1, ttpf.tile_bytes(4)) mip = layout["mips"][0] nblocks = mip["blocks_w"] * mip["blocks_h"] # Solid mode-6 BC7 blocks with distinct per-block endpoints. linear = bytearray() for i in range(nblocks): r0 = (i * 5) & 0x7F g0 = (i * 3 + 17) & 0x7F b0 = (i * 7 + 31) & 0x7F acc = (1 << 6) | (r0 << 7) | (r0 << 14) | (g0 << 21) | (g0 << 28) | (b0 << 35) | (b0 << 42) acc |= (0x7F << 49) | (0x7F << 56) | (1 << 63) | (1 << 64) linear += acc.to_bytes(16, "little") payload = bytearray(layout["size"]) retile_to(layout, mip, bytes(linear), payload) # Fill any unused tail padding blocks in the tile with mode-6 blocks too. mode6_pad = (1 << 6).to_bytes(16, "little") for off in range(0, len(payload), 16): if payload[off:off + 16] == b"\x00" * 16: payload[off:off + 16] = mode6_pad packed_payload = tb.encode_bc7_mode6_blocks(bytes(payload)) path = os.path.join(self.tmp.name, "bc7_bcpack.ttpf") pad = (-len(packed_payload)) % 256 count = 3 table_end = 24 + (count - 1) * 8 etpf = ttpf.align(table_end, 256) padded = ttpf.align(16 + 24 + 32, 4096) source = etpf + padded chunk_end = source + len(packed_payload) + pad virt = padded vsize = virt + layout["size"] entry = (source // 256) | (7 << 26) | ((virt // 4096) << 30) | (pad << 52) sentinel = (chunk_end // 256) | ((vsize // 4096) << 30) header = bytearray(24) header[0:4] = b"RDS5" struct.pack_into(" 6 | (21 << 5) = 0x2A6 meta = 6 | (21 << 5) base = ttpf.align(16 + 24 + 32, 4096) packed_mips = mips - non_tail rec = pack_etpf_record(width, height, packed_mips, non_tail, fmt, depth, 2, mode, base, meta=meta, extra=extra) write_rds5_pool(path, [rec], chunks) return layout, linear_faces def test_cubemap_mode18_and_streaming_mode20(self): import io from contextlib import redirect_stdout for non_tail, mode, expected_offset, expected_pages in [ (0, 18, 0, ()), (1, 20, 6 * 4096, (6, 18)), ]: with self.subTest(non_tail=non_tail, mode=mode): path = os.path.join(self.tmp.name, f"cube_mode{mode}.ttpf") layout, linear_faces = self._build_cubemap_pool(path, non_tail=non_tail, mode=mode) pool = ttpf.Pool(path) self.addCleanup(pool.close) a = ttpf.analyze(pool, 0) self.assertEqual(a["shape"], "cubemap") self.assertTrue(a["span_matches"]) self.assertTrue(a["full_chain"]) self.assertEqual(a["layout"]["data_offset"], expected_offset) ok_meta, pages = ttpf.validate_chunk_metadata(pool, a["descriptor"], a["allocation_span"]) self.assertTrue(ok_meta) self.assertEqual(pages, expected_pages) desc = ttpf.describe(pool, 0) self.assertEqual(desc["shape"], "cubemap") self.assertEqual(desc["data_offset"], expected_offset) self.assertEqual(desc["snapshots"], non_tail + 1) buf = io.StringIO() with redirect_stdout(buf): rc = ttpf.check(pool, 0) res = json.loads(buf.getvalue()) self.assertEqual(rc, 0, res) self.assertEqual(len(res["per_face_max_error"]), 6) self.assertLessEqual(res["max_error"], 6.0) self.assertGreater(res["tail_origin_control"], 5 * res["max_error"]) out_dds = os.path.join(self.tmp.name, f"cube_mode{mode}.dds") with redirect_stdout(io.StringIO()): self.assertEqual(ttpf.export(pool, 0, out_dds), 0) with open(out_dds, "rb") as f: dds = f.read() self.assertEqual(dds[:4], b"DDS ") caps2 = struct.unpack_from(" 1 else 1) buf = bytearray(layout["full_bytes"]) for face in range(6): color = (face * 35, 60 + face * 25, 20 + face * 30) for m in layout["mips"]: lin = solid_bc1(color) * (m["blocks_w"] * m["blocks_h"]) retile_to(layout, m, lin, buf, base_offset=face * layout["face_size"]) path = os.path.join(self.tmp.name, name) rec = pack_etpf_record(w, h, mips, 0, 71, 6, 2, 18, base, meta=0, extra=0) write_rds5_pool(path, [rec], [bytes(buf)]) pool = ttpf.Pool(path) self.addCleanup(pool.close) out = io.StringIO() with redirect_stdout(out): rc = ttpf.check(pool, 0) self.assertEqual(rc, 0) res = json.loads(out.getvalue()) self.assertEqual(res["first_tail"], 0 if mips > 1 else 1) self.assertIsNone(res["tail_origin_control"]) if mips == 1: self.assertIsNone(res["max_error"]) else: self.assertLessEqual(res["max_error"], 1.0) # Mid-chunk base mismatch rejected by validate_chunk_metadata. misaligned = os.path.join(self.tmp.name, "cube_mid_chunk.ttpf") rec = pack_etpf_record(32, 32, 1, 0, 71, 6, 2, 18, base + 4096, meta=6, extra=0) write_rds5_pool(misaligned, [rec], [bytes(6 * 4096)]) pool_m = ttpf.Pool(misaligned) self.addCleanup(pool_m.close) ok_meta, _ = ttpf.validate_chunk_metadata(pool_m, pool_m.descriptor(0), 6 * 4096) self.assertFalse(ok_meta) # non_tail == 3 alignment-padding validation (align_group 1/2/3 positive + negative). # Group 1: chunk0 (4 pages) + pad (12) == 16; chunks = [4, 16, 64]. # Group 2: chunk0 (1) + chunk1 (3) + pad (12) == 16. # Group 3: chunk0 (1) + chunk1 (3) + pad (4) == 8. cases = [ ("g1_ok", (4 | (11 << 5) | (15 << 10)), (1 << 19) | (12 << 21), [4, 16, 64], True), ("g1_bad", (4 | (11 << 5) | (15 << 10)), (1 << 19) | (11 << 21), [4, 16, 64], False), ("g2_ok", (1 | (3 << 5) | (11 << 10)), (2 << 19) | (12 << 21), [1, 3, 16], True), ("g3_ok", (1 | (3 << 5) | (8 << 10)), (3 << 19) | (4 << 21), [1, 3, 8], True), ("g0_bad", (1 | (3 << 5) | (8 << 10)), (0 << 19) | (4 << 21), [1, 3, 8], False), ] for tag, val20, hi_extra, page_counts, expected in cases: meta = val20 & 0x7FF extra = ((val20 >> 11) & 0x1FF) | hi_extra p_path = os.path.join(self.tmp.name, f"align_{tag}.ttpf") chunks = [bytes(pg * 4096) for pg in page_counts] span = sum(len(c) for c in chunks) r = pack_etpf_record(256, 256, 6, 3, 71, 1, 1, 4, base, meta=meta, extra=extra) write_rds5_pool(p_path, [r], chunks) pl = ttpf.Pool(p_path) self.addCleanup(pl.close) ok_al, _ = ttpf.validate_chunk_metadata(pl, pl.descriptor(0), span) self.assertEqual(ok_al, expected, tag) class SyntheticVolumePoolTest(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) def _build_volume_pool(self, path, width, height, depth, fmt, mips=1, mode=2): eb = ttpf.element_bytes(fmt) if fmt in ttpf.VOLUME_FLOAT_FORMATS and mips == 1: layout = ttpf.volume_surface_layout(width, height, depth, mips, eb) channels = ttpf.VOLUME_FLOAT_FORMATS[fmt] vals = [] for z in range(depth): for y in range(height): for x in range(width): if channels == 1: vals.append(10.0 + 0.05 * z + 0.1 * x + 0.1 * y) else: vals.extend(( 10.0 + 0.02 * z + 0.05 * x, 5.0 + 0.02 * z + 0.05 * y, 8.0 + 0.01 * z, 1.0, )) linear = struct.pack(f"<{len(vals)}e", *vals) tiled = retile_volume(layout, linear) else: layout = None linear = b"" tiled = b"\x00" * 4096 base = ttpf.align(16 + 24 + 32, 4096) rec = pack_etpf_record(width, height, mips, 0, fmt, depth, 3, mode, base) write_rds5_pool(path, [rec], [tiled]) return layout, linear, tiled def test_volume_fmt54_and_fmt10_round_trip_check_export_and_scan(self): import io from contextlib import redirect_stdout cases = [ # fmt=54 (R16_FLOAT): depth=8 pads to block_d=16 -> padded_d=16 (16, 8, 8, 54, 16, 16), # fmt=10 (R16G16B16A16_FLOAT): block_d=8 -> padded_d=8 (8, 8, 8, 10, 8, 8), ] for w, h, d, fmt, exp_bd, exp_pd in cases: with self.subTest(dims=(w, h, d), fmt=fmt): path = os.path.join(self.tmp.name, f"vol_fmt{fmt}.ttpf") layout, linear, tiled = self._build_volume_pool(path, w, h, d, fmt) pool = ttpf.Pool(path) self.addCleanup(pool.close) a = ttpf.analyze(pool, 0) self.assertEqual(a["shape"], "volume") self.assertTrue(a["span_matches"]) self.assertTrue(a["full_chain"]) self.assertEqual((a["layout"]["block_d"], a["layout"]["padded_d"]), (exp_bd, exp_pd)) desc = ttpf.describe(pool, 0) self.assertEqual(desc["shape"], "volume") self.assertEqual((desc["block_d"], desc["padded_d"]), (exp_bd, exp_pd)) # Byte-exact 3D detile round-trip. self.assertEqual(ttpf.detile_volume(layout, tiled), linear) buf = io.StringIO() with redirect_stdout(buf): rc = ttpf.check(pool, 0) res = json.loads(buf.getvalue()) self.assertEqual(rc, 0, res) self.assertLess(res["volume_3d_z_diff"], 0.5 * res["volume_2d_control_z_diff"]) out_dds = os.path.join(self.tmp.name, f"vol_fmt{fmt}.dds") with redirect_stdout(io.StringIO()): self.assertEqual(ttpf.export(pool, 0, out_dds), 0) with open(out_dds, "rb") as f: dds = f.read() self.assertEqual(dds[:4], b"DDS ") dds_depth = struct.unpack_from(" +64 bytes); pixel (x=0, y=4) sets Y2 (bit 7 -> +128 bytes). layout = ttpf.surface_layout(128, 128, 28, 1, tile=65536) mip = layout["mips"][0] tiled = bytearray(65536) tiled[64:68] = b"\x11\x22\x33\x44" tiled[128:132] = b"\x55\x66\x77\x88" det = ttpf.detile(layout, mip, bytes(tiled)) self.assertEqual(det[4 * 4:4 * 4 + 4], b"\x11\x22\x33\x44") self.assertEqual(det[(4 * 128) * 4:(4 * 128) * 4 + 4], b"\x55\x66\x77\x88") def test_decode_pixels_all_validated_uncompressed_formats(self): self.assertEqual(ttpf.ELEMENT_BYTES[11], 8) for fmt in sorted(ttpf.UNCOMPRESSED_2D_FORMATS): with self.subTest(fmt=fmt): raw = self._encode_pixel(fmt, 64, 128, 192, 255) px = ttpf.decode_pixels(fmt, raw, 1, 1) self.assertEqual(len(px), 1) r, g, b = px[0] if fmt in (61, 65, 56): self.assertAlmostEqual(r, 64.0, places=3) self.assertAlmostEqual(g, 64.0, places=3) self.assertAlmostEqual(b, 64.0, places=3) elif fmt == 49: self.assertEqual((r, g, b), (64.0, 128.0, 0.0)) elif fmt == 54: self.assertAlmostEqual(r, 1.0, places=3) self.assertAlmostEqual(g, 1.0, places=3) self.assertAlmostEqual(b, 1.0, places=3) elif fmt == 10: self.assertAlmostEqual(r, 1.0, places=3) self.assertAlmostEqual(g, 2.0, places=3) self.assertAlmostEqual(b, 3.0, places=3) else: # 11, 28, 29, 87, 91 self.assertAlmostEqual(r, 64.0, places=3) self.assertAlmostEqual(g, 128.0, places=3) self.assertAlmostEqual(b, 192.0, places=3) # Non-finite float16 values clamp cleanly in _hdr_tonemap. self.assertEqual(ttpf._hdr_tonemap(float("nan")), 0.0) self.assertEqual(ttpf._hdr_tonemap(float("inf")), 255.0) self.assertEqual(ttpf._hdr_tonemap(float("-inf")), -255.0) def test_uncompressed_2d_check_export_and_scan(self): import io from contextlib import redirect_stdout, redirect_stderr cases = [ # (name, w, h, fmt, mips, mode, expect_tail_control) ("dxgi11_1mip_4k", 8, 4, 11, 1, 2, False), ("dxgi11_multi_4k", 32, 32, 11, 4, 2, True), ("dxgi28_multi_64k", 256, 256, 28, 5, 0, True), ("dxgi28_odd_4k", 17, 13, 28, 4, 2, True), ("dxgi49_multi_4k", 64, 64, 49, 4, 2, True), ("dxgi56_multi_4k", 64, 64, 56, 4, 2, True), ("dxgi54_multi_64k", 128, 128, 54, 3, 0, False), ("dxgi61_multi_64k", 256, 256, 61, 4, 0, True), ("dxgi65_multi_4k", 64, 64, 65, 5, 2, True), ("dxgi87_multi_4k", 64, 64, 87, 5, 2, True), ("dxgi10_multi_4k", 32, 32, 10, 4, 2, True), ] for name, w, h, fmt, mips, mode, expect_ctrl in cases: with self.subTest(case=name): path = os.path.join(self.tmp.name, f"{name}.ttpf") _, expected_payload = self._build_uncompressed_pool(path, w, h, fmt, mips, mode) pool = ttpf.Pool(path) self.addCleanup(pool.close) buf = io.StringIO() with redirect_stdout(buf): rc = ttpf.check(pool, 0) res = json.loads(buf.getvalue()) self.assertEqual(rc, 0, res) if mips > 1: self.assertLessEqual(res["max_error"], 1.0, res) if expect_ctrl and res["tail_origin_control"] is not None: self.assertGreater(res["tail_origin_control"], 6.0, res) out_dds = os.path.join(self.tmp.name, f"{name}.dds") with redirect_stdout(io.StringIO()): erc = ttpf.export(pool, 0, out_dds) self.assertEqual(erc, 0) with open(out_dds, "rb") as f: dds = f.read() self.assertEqual(dds[:4], b"DDS ") flags, dh, dw, pitch = struct.unpack_from("= 101)): bits = sorted(abs(s) - (100 if s >= 101 else 0) for s in pat if keep(s)) self.assertEqual(bits, list(range(1, len(bits) + 1)), axis) def test_tile_extents_follow_from_the_bit_order(self): tex = sys.modules["tex_headers"] # imported by ttpf_mips.py for log, pat in ttpf.VOLUME_PATTERNS_4K.items(): with self.subTest(element_bytes=1 << log): want = (1 << sum(0 < s < 100 for s in pat), 1 << sum(s < 0 for s in pat), 1 << sum(s >= 101 for s in pat)) self.assertEqual(tex._VOLUME_4K_TILE[log], want) lay = tex.volume_surface_layout(64, 64, 32, 1, 1 << log) self.assertEqual((lay["block_w"], lay["block_h"], lay["block_d"]), want) self.assertEqual(lay["z_tile_stride"] % 4096, 0) class UnmappedSentinelSpanTest(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) def test_span_skips_unmapped_sentinels_and_rejects_unmapped_base(self): # A valid 256x256 BC1 texture followed by unmapped ETPF records (base=0 and base=0x1FFFFF*4096), # and then a second valid texture, must resolve both mapped spans cleanly and return None for # the unmapped records (including in scan_pools, which previously passed a negative span). layout = ttpf.surface_layout(256, 256, 71, 7, ttpf.tile_bytes(4)) size = layout["size"] base0 = 4096 base1 = base0 + size rec0 = pack_etpf_record(256, 256, 5, 2, 71, 1, 1, 4, base0) rec_zero = pack_etpf_record(256, 256, 5, 2, 71, 1, 1, 4, 0) rec_max = pack_etpf_record(256, 256, 5, 2, 71, 1, 1, 4, 0x1FFFFF * 4096) rec1 = pack_etpf_record(256, 256, 5, 2, 71, 1, 1, 4, base1) for label, recs, chunks, mapped_indices in ( ("zero_between", [rec0, rec_zero, rec1], [bytes(size), bytes(size)], (0, 2)), ("max_between", [rec0, rec_max, rec1], [bytes(size), bytes(size)], (0, 2)), ("trailing_zero_and_max", [rec0, rec_zero, rec_max], [bytes(size)], (0,)), ): with self.subTest(case=label): path = os.path.join(self.tmp.name, f"{label}.ttpf") write_rds5_pool(path, recs, chunks) pool = ttpf.Pool(path) self.addCleanup(pool.close) for idx in mapped_indices: d = pool.descriptor(idx) self.assertEqual(pool.span(idx, d["base"]), size) a = ttpf.analyze(pool, idx) self.assertTrue(a["span_matches"], a) self.assertNotIn("blocked", a) for idx in range(len(recs)): if idx not in mapped_indices: d = pool.descriptor(idx) self.assertIsNone(pool.span(idx, d["base"])) a = ttpf.analyze(pool, idx) self.assertEqual(a["blocked"], "no mapped allocation") scan_out = os.path.join(self.tmp.name, f"{label}.json") with redirect_stdout(io.StringIO()): ttpf.scan_pools([path], scan_out) with open(scan_out, encoding="utf-8") as f: total = json.load(f)["total"] self.assertEqual(total["chunk_metadata_fail"], 0) self.assertEqual(total["chunk_metadata_ok"], len(mapped_indices)) class DetileFastPathTest(unittest.TestCase): def test_coalesce_x_runs_merges_contiguous_blocks(self): self.assertEqual(ttpf._coalesce_x_runs([], 16), []) self.assertEqual(ttpf._coalesce_x_runs([0, 16, 32, 128, 144, 64], 16), [(0, 48), (128, 32), (64, 16)]) def test_detile_matches_reference_formula_across_all_element_sizes_and_tiles(self): def ref_detile(layout, mip, tiled): pat = (ttpf.PATTERNS_4K if layout["tile"] == 4096 else ttpf.PATTERNS_64K)[ {1: 0, 2: 1, 4: 2, 8: 3, 16: 4}[layout["element_bytes"]]] bw, bh = layout["block_w"], layout["block_h"] eb = layout["element_bytes"] out = bytearray(mip["blocks_w"] * mip["blocks_h"] * eb) for y in range(mip["blocks_h"]): for x in range(mip["blocks_w"]): off = ((y // bh) * (mip["padded_w"] // bw) + x // bw) * layout["tile"] for bit, s in enumerate(pat): if s > 0: off |= (((x + mip["tile_x"]) >> (s - 1)) & 1) << bit elif s < 0: off |= (((y + mip["tile_y"]) >> (-s - 1)) & 1) << bit at = (y * mip["blocks_w"] + x) * eb out[at:at + eb] = tiled[off:off + eb] return bytes(out) for fmt, eb in ((61, 1), (49, 2), (28, 4), (71, 8), (98, 16)): for tile in (4096, 65536): layout = ttpf.surface_layout(60, 44, fmt, 4, tile=tile) for idx, mip in enumerate(layout["mips"]): nbytes = max(mip["padded_w"] * mip["padded_h"] * eb, tile) tiled = bytes((i * 131 + 17) & 255 for i in range(nbytes)) with self.subTest(fmt=fmt, tile=tile, level=idx): self.assertEqual(ttpf.detile(layout, mip, tiled), ref_detile(layout, mip, tiled)) # Truncated input falls back to the exact slice behavior without raising ValueError. layout_t = ttpf.surface_layout(64, 64, 98, 1, tile=4096) mip_t = layout_t["mips"][0] short_tiled = bytes(range(200)) self.assertEqual(ttpf.detile(layout_t, mip_t, short_tiled), ref_detile(layout_t, mip_t, short_tiled)) @staticmethod def _ref_detile(layout, mip, tiled): pat = (ttpf.PATTERNS_4K if layout["tile"] == 4096 else ttpf.PATTERNS_64K)[ {1: 0, 2: 1, 4: 2, 8: 3, 16: 4}[layout["element_bytes"]]] bw, bh, eb = layout["block_w"], layout["block_h"], layout["element_bytes"] out = bytearray(mip["blocks_w"] * mip["blocks_h"] * eb) for y in range(mip["blocks_h"]): for x in range(mip["blocks_w"]): off = ((y // bh) * (mip["padded_w"] // bw) + x // bw) * layout["tile"] for bit, s in enumerate(pat): if s > 0: off |= (((x + mip["tile_x"]) >> (s - 1)) & 1) << bit elif s < 0: off |= (((y + mip["tile_y"]) >> (-s - 1)) & 1) << bit at = (y * mip["blocks_w"] + x) * eb out[at:at + eb] = tiled[off:off + eb] return bytes(out) def test_narrow_shapes_stream_and_match_reference(self): for w, h in ((16384, 1), (1, 16384), (1, 1), (3, 1), (1, 3)): for tile in (4096, 65536): layout = ttpf.surface_layout(w, h, 28, 1, tile=tile) mip = layout["mips"][0] nbytes = max(mip["padded_w"] * mip["padded_h"] * 4, tile) full = bytes((i * 131 + 17) & 255 for i in range(nbytes)) for tiled in (full, full[:nbytes // 3 + 5]): # complete and truncated input with self.subTest(w=w, h=h, tile=tile, size=len(tiled)): self.assertEqual(ttpf.detile(layout, mip, tiled), self._ref_detile(layout, mip, tiled)) def test_volume_slice_nonzero_z_truncated_matches_reference(self): layout = ttpf.volume_surface_layout(16, 8, 8, 1, 2) w, h, d = (layout["mips"][0][k] for k in ("blocks_w", "blocks_h", "blocks_d")) linear = bytes((i * 7 + 3) & 255 for i in range(w * h * d * 2)) tiled = retile_volume(layout, linear) for z in (0, 3, 7): self.assertEqual(ttpf.detile_volume_slice(layout, tiled, z), linear[z * w * h * 2:(z + 1) * w * h * 2]) # Truncated allocation: the exact slice-copy fallback must match a per-element reference. eb = layout["element_bytes"] pat = ttpf.VOLUME_PATTERNS_4K[{1: 0, 2: 1, 4: 2, 8: 3, 16: 4}[eb]] bw, bh, bd = layout["block_w"], layout["block_h"], layout["block_d"] tiles_x = layout["mips"][0]["padded_w"] // bw cut = tiled[:len(tiled) // 2 + 3] for z in (1, 5): ref = bytearray(w * h * eb) for y in range(h): for x in range(w): off = (z // bd) * layout["z_tile_stride"] + ((y // bh) * tiles_x + x // bw) * layout["tile"] for bit, s in enumerate(pat): if s >= 101: off |= ((z >> (s - 101)) & 1) << bit elif s < 0: off |= ((y >> (-s - 1)) & 1) << bit elif 0 < s < 100: off |= ((x >> (s - 1)) & 1) << bit at = (y * w + x) * eb ref[at:at + eb] = cut[off:off + eb] self.assertEqual(ttpf.detile_volume_slice(layout, cut, z), bytes(ref)) self.assertEqual(ttpf.detile_volume_slice(layout, tiled, 0), linear[:w * h * 2]) # --- The BC1/BC4 decode and block_means as they were before the table-driven rewrite ------- # Kept verbatim as the reference the fast paths must reproduce float for float. def _ref_rgb565(c): return ((c >> 11) & 31) * 255.0 / 31, ((c >> 5) & 63) * 255.0 / 63, (c & 31) * 255.0 / 31 def _ref_bc1_block(data, at, four_color=False): c0, c1, idx = struct.unpack_from(" c1: pal = [p0, p1, tuple((2 * a + b) / 3 for a, b in zip(p0, p1)), tuple((a + 2 * b) / 3 for a, b in zip(p0, p1))] else: pal = [p0, p1, tuple((a + b) / 2 for a, b in zip(p0, p1)), (0.0, 0.0, 0.0)] return [pal[(idx >> (2 * (y * 4 + x))) & 3] for y in range(4) for x in range(4)] def _ref_bc4_block(data, at): e0, e1 = data[at], data[at + 1] bits = int.from_bytes(data[at + 2:at + 8], "little") if e0 > e1: pal = [e0, e1] + [((7 - i) * e0 + i * e1) // 7 for i in range(1, 7)] else: pal = [e0, e1] + [((5 - i) * e0 + i * e1) // 5 for i in range(1, 5)] + [0, 255] vals = [pal[(bits >> (3 * (y * 4 + x))) & 7] for y in range(4) for x in range(4)] return [(float(v),) * 3 for v in vals] def _ref_decode_pixels(fmt, data, blocks_w, blocks_h): px = [] for by in range(blocks_h): rows = [[None] * (blocks_w * 4) for _ in range(4)] for bx in range(blocks_w): at = (by * blocks_w + bx) * 16 if fmt in (71, 72): blk = _ref_bc1_block(data, (by * blocks_w + bx) * 8) elif fmt in (74, 75, 77, 78): blk = _ref_bc1_block(data, at + 8, four_color=True) elif fmt in (80,): blk = _ref_bc4_block(data, (by * blocks_w + bx) * 8) elif fmt in (83,): a = _ref_bc4_block(data, at) b = _ref_bc4_block(data, at + 8) blk = [(p[0], q[0], 0.0) for p, q in zip(a, b)] else: return None for y in range(4): for x in range(4): rows[y][bx * 4 + x] = blk[y * 4 + x] px.extend(rows) return [p for row in px for p in row] def _ref_block_means(pixels, blocks_w, blocks_h, pixels_w=None, pixels_h=None, block=4): if block == 1: return list(pixels) pw = blocks_w * block if pixels_w is None else pixels_w ph = blocks_h * block if pixels_h is None else pixels_h out = [] for by in range(blocks_h): for bx in range(blocks_w): acc = [0.0, 0.0, 0.0] n = 0 for y in range(block): if by * block + y >= ph: continue for x in range(block): if bx * block + x >= pw: continue q = pixels[(by * block + y) * (blocks_w * block) + bx * block + x] for k in range(3): acc[k] += q[k] n += 1 out.append((acc[0] / n, acc[1] / n, acc[2] / n) if n else (0.0, 0.0, 0.0)) return out def _float_bits(pixels): """Every channel's IEEE bits (so -0.0 counts); each channel must be a float. All NaNs map to one: which NaN comes out of `NaN + NaN` follows the operand order the interpreter's float add happens to use (it differs between a cold and a specialized `+`), in the scalar loop as much as here, and no consumer of the means reads it (`_hdr_tonemap` maps NaN to 0).""" flat = [c for p in pixels for c in p] assert all(type(c) is float for c in flat), "a channel is not a float" return struct.pack(f"<{len(flat)}d", *[float("nan") if c != c else c for c in flat]) class BcFastDecodeParityTest(unittest.TestCase): """decode_bc1_block / decode_bc4_block / decode_pixels / block_means against the scalar code they replaced, plus hand-derived values that need no reference.""" def assert_same_pixels(self, got, want): self.assertEqual(len(got), len(want)) self.assertEqual(_float_bits(got), _float_bits(want)) def test_bc1_hand_derived_palette_and_pixel_order(self): red, blue = 0xF800, 0x001F # c0 > c1: four colors; index = row number, so row y is all pal[y]. blk = struct.pack("