diff options
| author | Diesel <dev.diesel@proton.me> | 2026-11-21 22:44:07 +0100 |
|---|---|---|
| committer | Diesel <dev.diesel@proton.me> | 2026-11-21 22:44:07 +0100 |
| commit | 5c1a1fbb6bf6f609eb4e61baa4961ce382743a4e (patch) | |
| tree | ac8ad90058b97ab241fdc65edae3d4628d20ee1e /tests/test_ttpf_mips.py | |
| parent | c45309e29f21c1d5b75678db0e2dc2dd11cff25b (diff) | |
| parent | 4e390e0871d7265e629b51aca8ff99a15a6a6845 (diff) | |
Merge main to include the compute Y/Z harness test fix
Diffstat (limited to 'tests/test_ttpf_mips.py')
| -rw-r--r-- | tests/test_ttpf_mips.py | 291 |
1 files changed, 291 insertions, 0 deletions
diff --git a/tests/test_ttpf_mips.py b/tests/test_ttpf_mips.py new file mode 100644 index 000000000..a41f6c0e3 --- /dev/null +++ b/tests/test_ttpf_mips.py | |||
| @@ -0,0 +1,291 @@ | |||
| 1 | """Tests for tools/ttpf_mips.py: tile modes, mip-tail layout, BC decode, metric, and a | ||
| 2 | synthetic RDS5/ETPF pool round trip. No game data is used. | ||
| 3 | |||
| 4 | python -I tests/test_ttpf_mips.py | ||
| 5 | """ | ||
| 6 | import importlib.util | ||
| 7 | import json | ||
| 8 | import os | ||
| 9 | import struct | ||
| 10 | import sys | ||
| 11 | import tempfile | ||
| 12 | import unittest | ||
| 13 | import zlib | ||
| 14 | |||
| 15 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
| 16 | TOOL = os.path.join(ROOT, "tools", "ttpf_mips.py") | ||
| 17 | spec = importlib.util.spec_from_file_location("ttpf_mips", TOOL) | ||
| 18 | ttpf = importlib.util.module_from_spec(spec) | ||
| 19 | spec.loader.exec_module(ttpf) | ||
| 20 | |||
| 21 | |||
| 22 | def rgb565(r, g, b): | ||
| 23 | return ((r * 31 // 255) << 11) | ((g * 63 // 255) << 5) | (b * 31 // 255) | ||
| 24 | |||
| 25 | |||
| 26 | def solid_bc1(color): | ||
| 27 | c = rgb565(*color) | ||
| 28 | return struct.pack("<HHI", c, c, 0) | ||
| 29 | |||
| 30 | |||
| 31 | def retile_to(layout, mip, linear, out): | ||
| 32 | pat = (ttpf.PATTERNS_4K if layout["tile"] == 4096 else ttp64)[ | ||
| 33 | {1: 0, 2: 1, 4: 2, 8: 3, 16: 4}[layout["element_bytes"]]] | ||
| 34 | eb = layout["element_bytes"] | ||
| 35 | bw, bh = layout["block_w"], layout["block_h"] | ||
| 36 | for y in range(mip["blocks_h"]): | ||
| 37 | for x in range(mip["blocks_w"]): | ||
| 38 | off = ((y // bh) * (mip["padded_w"] // bw) + x // bw) * layout["tile"] | ||
| 39 | for bit, s in enumerate(pat): | ||
| 40 | if s > 0: | ||
| 41 | off |= (((x + mip["tile_x"]) >> (s - 1)) & 1) << bit | ||
| 42 | elif s < 0: | ||
| 43 | off |= (((y + mip["tile_y"]) >> (-s - 1)) & 1) << bit | ||
| 44 | at = (y * mip["blocks_w"] + x) * eb | ||
| 45 | out[mip["offset"] + off:mip["offset"] + off + eb] = linear[at:at + eb] | ||
| 46 | |||
| 47 | |||
| 48 | ttpf64 = { | ||
| 49 | 0: (1, 2, 3, -2, -1, -3, 4, -4, -5, 5, -6, 6, -7, 7, -8, 8), | ||
| 50 | 1: (0, 1, 2, 3, -1, -2, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8), | ||
| 51 | 2: (0, 0, 1, 2, -1, -2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7), | ||
| 52 | 3: (0, 0, 0, 1, -1, 2, 3, -2, -3, 4, -4, 5, -5, 6, -6, 7), | ||
| 53 | 4: (0, 0, 0, 0, 1, -1, 2, -2, -3, 3, -4, 4, -5, 5, -6, 6), | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | def solid_chain(layout, fmt=71): | ||
| 58 | """Build block-linear bytes for every mip; each block is one solid BC1 colour.""" | ||
| 59 | eb = layout["element_bytes"] | ||
| 60 | out = {} | ||
| 61 | # Smooth gradient at the top level; every lower mip is the 2x2 block mean. | ||
| 62 | top = layout["mips"][0] | ||
| 63 | colours = {} | ||
| 64 | for y in range(top["blocks_h"]): | ||
| 65 | for x in range(top["blocks_w"]): | ||
| 66 | colours[(0, x, y)] = (min(255, x * 255 // max(1, top["blocks_w"] - 1)), | ||
| 67 | min(255, y * 255 // max(1, top["blocks_h"] - 1)), 128) | ||
| 68 | for m in range(1, len(layout["mips"])): | ||
| 69 | pm = layout["mips"][m - 1] | ||
| 70 | cm = layout["mips"][m] | ||
| 71 | for y in range(cm["blocks_h"]): | ||
| 72 | for x in range(cm["blocks_w"]): | ||
| 73 | acc = [0, 0, 0] | ||
| 74 | n = 0 | ||
| 75 | for dy in range(2): | ||
| 76 | for dx in range(2): | ||
| 77 | px, py = min(pm["blocks_w"] - 1, 2 * x + dx), min(pm["blocks_h"] - 1, 2 * y + dy) | ||
| 78 | q = colours[(m - 1, px, py)] | ||
| 79 | for k in range(3): | ||
| 80 | acc[k] += q[k] | ||
| 81 | n += 1 | ||
| 82 | colours[(m, x, y)] = tuple(a // n for a in acc) | ||
| 83 | for mip in layout["mips"]: | ||
| 84 | buf = bytearray(mip["blocks_w"] * mip["blocks_h"] * eb) | ||
| 85 | for y in range(mip["blocks_h"]): | ||
| 86 | for x in range(mip["blocks_w"]): | ||
| 87 | at = (y * mip["blocks_w"] + x) * eb | ||
| 88 | buf[at:at + eb] = solid_bc1(colours[(mip["index"], x, y)])[:eb].ljust(eb, b"\0") | ||
| 89 | out[mip["index"]] = bytes(buf) | ||
| 90 | return out | ||
| 91 | |||
| 92 | |||
| 93 | def build_pool(path, width=256, height=256, mips=7, non_tail=1, fmt=71, mode=4, | ||
| 94 | meta=0x103, extra=0, packed=6): | ||
| 95 | layout = ttpf.surface_layout(width, height, fmt, mips, ttpf.tile_bytes(mode)) | ||
| 96 | linear = solid_chain(layout, fmt) | ||
| 97 | payload = bytearray(layout["size"]) | ||
| 98 | for mip in layout["mips"]: | ||
| 99 | retile_to(layout, mip, linear[mip["index"]], payload) | ||
| 100 | packed_payload = zlib.compress(bytes(payload), 9) | ||
| 101 | pad = (-len(packed_payload)) % 256 | ||
| 102 | count = 3 | ||
| 103 | table_end = 24 + (count - 1) * 8 | ||
| 104 | etpf = ttpf.align(table_end, 256) | ||
| 105 | metadata = 16 + 24 + 32 | ||
| 106 | padded = ttpf.align(metadata, 4096) | ||
| 107 | source = etpf + padded | ||
| 108 | chunk_end = source + len(packed_payload) + pad | ||
| 109 | virt = padded | ||
| 110 | vsize = virt + layout["size"] | ||
| 111 | assert vsize % 4096 == 0 | ||
| 112 | entry = (source // 256) | (8 << 26) | ((virt // 4096) << 30) | (pad << 52) | ||
| 113 | sentinel = (chunk_end // 256) | ((vsize // 4096) << 30) | ||
| 114 | header = bytearray(24) | ||
| 115 | header[0:4] = b"RDS5" | ||
| 116 | struct.pack_into("<I", header, 4, count) | ||
| 117 | struct.pack_into("<Q", header, 8, vsize | (22 << 56)) | ||
| 118 | struct.pack_into("<I", header, 16, (count + 31) // 32) | ||
| 119 | struct.pack_into("<I", header, 20, 0) | ||
| 120 | a = (width - 1) | ((height - 1) << 14) | ((packed & 0xF) << 28) | ||
| 121 | b = non_tail | (fmt << 3) | (1 << 21) | (mode << 23) | ||
| 122 | c = (virt // 4096) | (meta << 21) | ||
| 123 | record = struct.pack("<IIII", a, b, c, extra) + struct.pack("<Q", 0x1234567890ABCDEF) | ||
| 124 | blob = bytearray(chunk_end) | ||
| 125 | blob[0:24] = header | ||
| 126 | struct.pack_into("<QQ", blob, 24, entry, sentinel) | ||
| 127 | blob[etpf:etpf + 4] = b"ETPF" | ||
| 128 | struct.pack_into("<III", blob, etpf + 4, 634, 1, 24) | ||
| 129 | blob[etpf + 16:etpf + 16 + 24] = record | ||
| 130 | blob[source:source + len(packed_payload)] = packed_payload | ||
| 131 | with open(path, "wb") as f: | ||
| 132 | f.write(blob) | ||
| 133 | return layout | ||
| 134 | |||
| 135 | |||
| 136 | class TileModeTest(unittest.TestCase): | ||
| 137 | def test_mode_is_flag_plus_tile_mode(self): | ||
| 138 | self.assertEqual([ttpf.tile_mode(m) for m in (0, 1, 2, 4, 5, 6, 7, 32, 34, 36, 38)], | ||
| 139 | [0, 0, 1, 2, 2, 3, 3, 0, 1, 2, 3]) | ||
| 140 | self.assertEqual([ttpf.tile_bytes(m) for m in (0, 1, 2, 4, 32, 34)], | ||
| 141 | [65536, 65536, 4096, 4096, 65536, 4096]) | ||
| 142 | |||
| 143 | |||
| 144 | class LayoutTest(unittest.TestCase): | ||
| 145 | def test_known_bc1_chain_sizes(self): | ||
| 146 | lay = ttpf.surface_layout(256, 256, 71, 7, 4096) | ||
| 147 | self.assertEqual(lay["first_tail"], 2) | ||
| 148 | self.assertEqual(lay["size"], 45056) | ||
| 149 | self.assertEqual([m["region"] for m in lay["mips"]], | ||
| 150 | [32768, 8192, 4096, 4096, 4096, 4096, 4096]) | ||
| 151 | self.assertEqual([m["offset"] for m in lay["mips"][:2]], [12288, 4096]) | ||
| 152 | # 16x16 pixels = 4x4 blocks at in-tile origin (16, 0) | ||
| 153 | self.assertEqual((lay["mips"][2]["blocks_w"], lay["mips"][2]["blocks_h"]), (16, 16)) | ||
| 154 | self.assertEqual((lay["mips"][2]["tile_x"], lay["mips"][2]["tile_y"]), (16, 0)) | ||
| 155 | |||
| 156 | def test_validated_1024_chain(self): | ||
| 157 | lay = ttpf.surface_layout(1024, 1024, 71, 9, 4096) | ||
| 158 | self.assertEqual(lay["first_tail"], 4) | ||
| 159 | self.assertEqual(lay["size"], 700416) | ||
| 160 | self.assertEqual([m["region"] for m in lay["mips"][:4]], | ||
| 161 | [524288, 131072, 32768, 8192]) | ||
| 162 | self.assertEqual(lay["mips"][4]["tile_x"], 16) | ||
| 163 | self.assertEqual(lay["mips"][4]["tile_y"], 0) | ||
| 164 | |||
| 165 | def test_64k_tile_mode(self): | ||
| 166 | lay = ttpf.surface_layout(1024, 1024, 98, 9, 65536) | ||
| 167 | self.assertEqual(lay["size"], 1441792) | ||
| 168 | self.assertEqual(lay["first_tail"], 3) | ||
| 169 | self.assertEqual(lay["tile"], 65536) | ||
| 170 | |||
| 171 | def test_bad_mip_count(self): | ||
| 172 | with self.assertRaises(ttpf.LayoutError): | ||
| 173 | ttpf.surface_layout(64, 64, 71, 20, 4096) | ||
| 174 | |||
| 175 | |||
| 176 | class BcDecodeTest(unittest.TestCase): | ||
| 177 | def test_bc1_solid_block(self): | ||
| 178 | block = solid_bc1((255, 0, 0)) | ||
| 179 | px = ttpf.decode_pixels(71, block, 1, 1) | ||
| 180 | self.assertEqual(len(px), 16) | ||
| 181 | self.assertGreater(px[0][0], 250) | ||
| 182 | self.assertLess(px[0][1], 8) | ||
| 183 | |||
| 184 | def test_bc1_four_colour_mode(self): | ||
| 185 | # c0=white > c1=black: index 0 -> white, index 3 -> (c0 + 2*c1)/3 | ||
| 186 | white = ttpf.decode_pixels(71, struct.pack("<HHI", 0xFFFF, 0x0000, 0), 1, 1) | ||
| 187 | self.assertGreater(white[0][0], 250) | ||
| 188 | dark = ttpf.decode_pixels(71, struct.pack("<HHI", 0xFFFF, 0x0000, 0xFFFFFFFF), 1, 1) | ||
| 189 | self.assertLess(dark[0][0], 100) | ||
| 190 | |||
| 191 | def test_metric_rewards_the_true_downsample(self): | ||
| 192 | parent = [(0.0, 0.0, 0.0), (255.0, 0.0, 0.0), (0.0, 255.0, 0.0), (0.0, 0.0, 255.0)] | ||
| 193 | child = [(63.75, 63.75, 63.75)] | ||
| 194 | self.assertLess(ttpf.mean_abs_error(ttpf.downsample2(parent, 2, 2), child), 1.0) | ||
| 195 | wrong = [(0.0, 255.0, 0.0)] | ||
| 196 | self.assertGreater(ttpf.mean_abs_error(ttpf.downsample2(parent, 2, 2), wrong), 50) | ||
| 197 | |||
| 198 | |||
| 199 | class SyntheticPoolTest(unittest.TestCase): | ||
| 200 | def setUp(self): | ||
| 201 | self.tmp = tempfile.TemporaryDirectory() | ||
| 202 | self.addCleanup(self.tmp.cleanup) | ||
| 203 | self.path = os.path.join(self.tmp.name, "synthetic.ttpf") | ||
| 204 | self.layout = build_pool(self.path) | ||
| 205 | self.pool = ttpf.Pool(self.path) | ||
| 206 | self.addCleanup(self.pool.close) | ||
| 207 | |||
| 208 | def test_descriptor_and_layout(self): | ||
| 209 | d = self.pool.descriptor(0) | ||
| 210 | self.assertEqual((d["width"], d["height"], d["format"], d["mips"], d["non_tail"]), | ||
| 211 | (256, 256, 71, 7, 1)) | ||
| 212 | self.assertEqual(d["mode"], 4) | ||
| 213 | a = ttpf.analyze(self.pool, 0) | ||
| 214 | self.assertTrue(a["span_matches"]) | ||
| 215 | self.assertTrue(a["full_chain"]) | ||
| 216 | self.assertEqual(a["layout"]["first_tail"], 2) | ||
| 217 | |||
| 218 | def test_detile_round_trip(self): | ||
| 219 | a = ttpf.analyze(self.pool, 0) | ||
| 220 | layout = a["layout"] | ||
| 221 | tiled = self.pool.read_range(a["descriptor"]["base"], layout["size"]) | ||
| 222 | linear = solid_chain(layout) | ||
| 223 | for mip in layout["mips"]: | ||
| 224 | data = ttpf.detile(layout, mip, tiled[mip["offset"]:mip["offset"] + mip["region"]]) | ||
| 225 | self.assertEqual(data, linear[mip["index"]]) | ||
| 226 | |||
| 227 | def test_chain_metric(self): | ||
| 228 | import io | ||
| 229 | from contextlib import redirect_stdout | ||
| 230 | buf = io.StringIO() | ||
| 231 | with redirect_stdout(buf): | ||
| 232 | code = ttpf.check(self.pool, 0) | ||
| 233 | result = json.loads(buf.getvalue()) | ||
| 234 | self.assertEqual(code, 0, result) | ||
| 235 | self.assertLess(result["max_error"], 4.0) | ||
| 236 | # a wrong in-tile origin for the first tail mip must be much worse | ||
| 237 | self.assertGreater(result["tail_origin_control"], 5 * result["max_error"]) | ||
| 238 | |||
| 239 | def test_export_full_chain(self): | ||
| 240 | out = os.path.join(self.tmp.name, "chain.dds") | ||
| 241 | import io | ||
| 242 | from contextlib import redirect_stdout | ||
| 243 | with redirect_stdout(io.StringIO()): | ||
| 244 | self.assertEqual(ttpf.export(self.pool, 0, out), 0) | ||
| 245 | with open(out, "rb") as f: | ||
| 246 | data = f.read() | ||
| 247 | self.assertEqual(data[:4], b"DDS ") | ||
| 248 | mips = struct.unpack_from("<I", data, 28)[0] | ||
| 249 | self.assertEqual(mips, 7) | ||
| 250 | expected = 148 + sum(m["blocks_w"] * m["blocks_h"] * 8 for m in self.layout["mips"]) | ||
| 251 | self.assertEqual(len(data), expected) | ||
| 252 | # payload is exactly the concatenated block-linear chain | ||
| 253 | linear = solid_chain(self.layout) | ||
| 254 | self.assertEqual(data[148:], b"".join(linear[m["index"]] for m in self.layout["mips"])) | ||
| 255 | |||
| 256 | def test_scan_reports_the_whitelist_gate(self): | ||
| 257 | # The synthetic tuple is validated, so it is not gated: the partial export is | ||
| 258 | # the descriptor non_tail limit, not the 319-byte profile table. | ||
| 259 | out = os.path.join(self.tmp.name, "scan.json") | ||
| 260 | import io | ||
| 261 | from contextlib import redirect_stdout | ||
| 262 | with redirect_stdout(io.StringIO()): | ||
| 263 | ttpf.scan_pools([self.path], out) | ||
| 264 | with open(out, encoding="utf-8") as f: | ||
| 265 | result = json.load(f)["total"] | ||
| 266 | self.assertEqual(result["entries"], 1) | ||
| 267 | self.assertEqual(result["whitelist_gated"], 0) | ||
| 268 | self.assertEqual(result["reader_partial"], 1) | ||
| 269 | self.assertEqual(result["compact_partial_rows"], 1) | ||
| 270 | self.assertEqual(result["compact_missing_mips"], 6) | ||
| 271 | self.assertEqual(result["compact_decodable_mips"], 6) | ||
| 272 | self.assertEqual(result["compact_full_rows"], 1) | ||
| 273 | |||
| 274 | def test_scan_counts_an_unlisted_tuple_as_gated(self): | ||
| 275 | path = os.path.join(self.tmp.name, "unlisted.ttpf") | ||
| 276 | build_pool(path, meta=0x599) | ||
| 277 | out = os.path.join(self.tmp.name, "scan2.json") | ||
| 278 | import io | ||
| 279 | from contextlib import redirect_stdout | ||
| 280 | with redirect_stdout(io.StringIO()): | ||
| 281 | ttpf.scan_pools([path], out) | ||
| 282 | with open(out, encoding="utf-8") as f: | ||
| 283 | total = json.load(f)["total"] | ||
| 284 | self.assertEqual(total["whitelist_gated"], 1) | ||
| 285 | self.assertEqual(total["gated_layout_ok"], 1) | ||
| 286 | self.assertEqual(total["gated_full_chain"], 1) | ||
| 287 | self.assertEqual(total["gated_full_mips"], 7) | ||
| 288 | |||
| 289 | |||
| 290 | if __name__ == "__main__": | ||
| 291 | unittest.main() | ||