mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 15:18:38 +00:00
920 lines
40 KiB
Python
920 lines
40 KiB
Python
"""Measure and decode the TTPF (RDS5) compact mip chain, including the packed tail.
|
|
|
|
The compact profile in `TtpfReader` is the AMD Gfx10 mip-tail layout: mips smaller
|
|
than half a tile share one tile at offset 0, and the rest are standalone, smallest
|
|
first. `TtpfReader` only trusts a hand-validated tuple table and stops at the
|
|
descriptor's `nonTail` field, which is not the layout boundary. The layout rule lives
|
|
in `tex_headers.py` (`compact_surface_layout` and the helpers around it); this tool
|
|
supplies the RDS5 reader, decoders and commands. It computes the layout from the
|
|
descriptor, validates it against the native allocation span, then decodes zlib/raw
|
|
mips and supported BCPack codecs (BC1, BC3, BC4 and BC5 via `bcpack.py`, strict
|
|
confirmed-history mode by default; pass `--allow-provisional` to opt into unconfirmed
|
|
built-in history entries).
|
|
|
|
Read-only: no texture bytes are printed or committed. `scan` emits aggregate
|
|
counters only (both the zlib/raw baseline and the BCPack-enabled projection).
|
|
`layout` prints descriptor fields and offsets. `check` prints the BC block-average
|
|
error against a 2x2 downsample of the parent mip; it decodes BC1, BC3, BC4_UNORM
|
|
and BC5_UNORM (BC2, the SNORM BC4/5 variants, BC6H and BC7 are sized but not
|
|
decoded, and uncompressed tiling is not validated). `export` writes a full BC chain
|
|
when every source mip uses a supported codec.
|
|
|
|
python -I tools/ttpf_mips.py scan <game root or pool paths...> [--json out] [--tuples]
|
|
python -I tools/ttpf_mips.py layout <pool.ttpf> <index>
|
|
python -I tools/ttpf_mips.py check <pool.ttpf> <index> [--allow-provisional]
|
|
python -I tools/ttpf_mips.py export <pool.ttpf> <index> <out.dds> [--mips N] [--force] [--allow-provisional]
|
|
|
|
Standard library only. The RDS5/ETPF checks mirror rpf9-explorer's `TtpfReader`
|
|
(MIT); the tile patterns and tail formula are the AMD Gfx10 4 KiB/64 KiB layouts
|
|
already used by that reader and recorded in docs/textures.md.
|
|
"""
|
|
import argparse
|
|
import glob
|
|
import json
|
|
import mmap
|
|
import os
|
|
import struct
|
|
import sys
|
|
import zlib
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
import bcpack # noqa: E402
|
|
from tex_headers import ( # noqa: E402
|
|
compact_surface_layout, gfx10_tile_bytes, gfx10_tile_mode,
|
|
parse_etpf_texture_record,
|
|
)
|
|
|
|
MAX_OUTPUT = 256 * 1024 * 1024
|
|
MAX_TEXTURES = 250_000
|
|
ETPF_VERSION = 634
|
|
|
|
# DXGI format -> element bytes, as RageTextureDecoder.ElementBytes. DXGI 11
|
|
# (R16G16B16A16_UNORM) is 8 bytes but is left out here as well: its uncompressed
|
|
# tiling has not been validated, so `scan` reports it as a format gap.
|
|
ELEMENT_BYTES = {
|
|
2: 16, 3: 16, 4: 16, 6: 12, 7: 12, 8: 12, 10: 8, 12: 8, 13: 8, 14: 8,
|
|
16: 8, 17: 8, 18: 8, 24: 4, 25: 4, 26: 4, 28: 4, 29: 4, 30: 4, 31: 4, 32: 4,
|
|
34: 4, 35: 4, 36: 4, 37: 4, 38: 4, 41: 4, 42: 4, 43: 4, 49: 2, 50: 2, 51: 2,
|
|
52: 2, 54: 2, 56: 2, 57: 2, 58: 2, 59: 2, 61: 1, 62: 1, 63: 1, 64: 1, 65: 1,
|
|
71: 8, 72: 8, 74: 16, 75: 16, 77: 16, 78: 16, 80: 8, 81: 8, 83: 16, 84: 16,
|
|
87: 4, 88: 4, 91: 4, 93: 4, 95: 16, 96: 16, 98: 16, 99: 16,
|
|
}
|
|
BC_FORMATS = frozenset(range(70, 85)) | frozenset(range(94, 100))
|
|
|
|
# AMD Gfx10 intra-tile bit order (bit index -> source bit), from TtpfReader.
|
|
PATTERNS_4K = {
|
|
0: (1, 2, 3, 4, -1, -2, -3, -4, -5, 5, -6, 6),
|
|
1: (0, 1, 2, 3, -1, -2, -3, 4, -4, 5, -5, 6),
|
|
2: (0, 0, 1, 2, -1, -2, -3, 3, -4, 4, -5, 5),
|
|
3: (0, 0, 0, 1, -1, -2, 2, 3, -3, 4, -4, 5),
|
|
4: (0, 0, 0, 0, -1, -2, 1, 2, -3, 3, -4, 4),
|
|
}
|
|
PATTERNS_64K = {
|
|
0: (1, 2, 3, -2, -1, -3, 4, -4, -5, 5, -6, 6, -7, 7, -8, 8),
|
|
1: (0, 1, 2, 3, -1, -2, -3, 4, -4, 5, -5, 6, -6, 7, -7, 8),
|
|
2: (0, 0, 1, 2, -1, -2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7),
|
|
3: (0, 0, 0, 1, -1, 2, 3, -2, -3, 4, -4, 5, -5, 6, -6, 7),
|
|
4: (0, 0, 0, 0, 1, -1, 2, -2, -3, 3, -4, 4, -5, 5, -6, 6),
|
|
}
|
|
|
|
# The profile tuples `TtpfReader.ValidatedCompactProfile` accepts. Reproduced only
|
|
# so `scan` can show the reader's published skip/partial split next to the layout
|
|
# result; decoding does not depend on this table.
|
|
VALIDATED_COMPACT = {
|
|
(4, 1024, 1024, 9, 3, 8, 0x503, 0x00B00106),
|
|
(6, 512, 512, 8, 3, 8, 0x041, 0x00B800D4),
|
|
(4, 256, 256, 7, 1, 8, 0x103, 0),
|
|
(4, 512, 512, 8, 3, 16, 0x482, 0x015000F5),
|
|
(4, 1024, 512, 8, 3, 8, 0x482, 0x015000F5),
|
|
(4, 512, 1024, 8, 3, 8, 0x483, 0x013000F5),
|
|
(4, 128, 256, 6, 1, 8, 0x083, 0),
|
|
(4, 256, 512, 7, 1, 8, 0x167, 0),
|
|
(4, 512, 256, 7, 1, 8, 0x166, 0),
|
|
(4, 256, 256, 7, 1, 16, 0x166, 0),
|
|
(4, 1024, 512, 8, 3, 16, 0x504, 0x900106),
|
|
(4, 1024, 256, 7, 3, 16, 0x484, 0x11000F5),
|
|
(6, 1024, 256, 7, 3, 8, 0x42, 0x9800D4),
|
|
(6, 64, 1024, 5, 3, 8, 0x83, 0x3800B4),
|
|
(4, 512, 1024, 8, 3, 16, 0x503, 0xB00106),
|
|
(4, 512, 128, 6, 1, 16, 0x168, 0),
|
|
(6, 256, 1024, 7, 3, 8, 0x83, 0x3800D4),
|
|
(4, 1024, 128, 6, 1, 8, 0x168, 0),
|
|
(4, 2048, 256, 7, 3, 16, 0x508, 0x100106),
|
|
(4, 2048, 512, 8, 3, 8, 0x504, 0x900106),
|
|
(6, 512, 256, 7, 3, 16, 0x42, 0x9800D4),
|
|
(6, 1024, 128, 6, 3, 16, 0x84, 0x1800D4),
|
|
(4, 512, 128, 6, 1, 8, 0x104, 0),
|
|
(4, 256, 1024, 7, 3, 16, 0x483, 0x13000F5),
|
|
(6, 1024, 64, 5, 3, 16, 0x83, 0x3800B4),
|
|
(6, 256, 512, 7, 3, 16, 0x41, 0xB800D4),
|
|
(4, 128, 128, 6, 1, 16, 0x82, 0),
|
|
(4, 512, 64, 5, 1, 16, 0x108, 0),
|
|
(4, 256, 128, 6, 1, 16, 0x104, 0),
|
|
(6, 128, 1024, 6, 3, 16, 0x83, 0x3800D4),
|
|
(4, 512, 64, 5, 1, 8, 0x84, 0),
|
|
(4, 128, 512, 6, 1, 16, 0x167, 0),
|
|
(4, 256, 128, 6, 1, 8, 0x82, 0),
|
|
(4, 1024, 64, 5, 1, 8, 0x108, 0),
|
|
(6, 1024, 32, 4, 3, 16, 0x82, 0x5800B4),
|
|
(4, 128, 512, 6, 1, 8, 0x107, 0),
|
|
(4, 512, 2048, 8, 3, 8, 0x507, 0x300106),
|
|
(4, 256, 2048, 7, 3, 8, 0x507, 0x3000F5),
|
|
(4, 512, 32, 4, 1, 16, 0x107, 0),
|
|
(6, 128, 1024, 6, 3, 8, 0x83, 0x3800B4),
|
|
(6, 64, 1024, 5, 3, 16, 0x83, 0x3800B4),
|
|
(4, 256, 256, 5, 1, 16, 0x166, 0),
|
|
(4, 128, 256, 6, 1, 16, 0x103, 0),
|
|
(4, 256, 64, 5, 1, 16, 0x84, 0),
|
|
(4, 16, 256, 3, 1, 16, 0x83, 0),
|
|
(4, 256, 32, 4, 1, 16, 0x84, 0),
|
|
(4, 64, 512, 5, 1, 16, 0x107, 0),
|
|
(4, 2048, 256, 7, 3, 8, 0x484, 0x11000F5),
|
|
(4, 64, 512, 5, 1, 8, 0x107, 0),
|
|
(4, 64, 256, 5, 1, 8, 0x83, 0),
|
|
(6, 256, 512, 9, 3, 16, 0x41, 0xB800D4),
|
|
(6, 32, 1024, 4, 3, 16, 0x82, 0x5800B4),
|
|
(4, 512, 512, 3, 3, 8, 0x502, 0xD00006),
|
|
(36, 1024, 1024, 9, 3, 8, 0x503, 0xB00106),
|
|
(36, 256, 256, 7, 1, 8, 0x103, 0),
|
|
}
|
|
VALIDATED_AUX = dict(mips=3, non_tail=3, meta=0x1A8, extra=0x01080008, width=1024,
|
|
height_a=512, fmt_a=61, height_b=1024, bytes_b=8)
|
|
|
|
|
|
class LayoutError(ValueError):
|
|
pass
|
|
|
|
|
|
class BcpackRequired(NotImplementedError):
|
|
pass
|
|
|
|
|
|
def u32(b, o):
|
|
return struct.unpack_from("<I", b, o)[0]
|
|
|
|
|
|
def u64(b, o):
|
|
return struct.unpack_from("<Q", b, o)[0]
|
|
|
|
|
|
def align(v, n):
|
|
return (v + n - 1) // n * n
|
|
|
|
|
|
def tile_mode(mode):
|
|
"""Descriptor mode is a flag bit plus a 4-bit Gfx10 tile mode at bit 1."""
|
|
return gfx10_tile_mode(mode)
|
|
|
|
|
|
def tile_bytes(mode):
|
|
# Tile mode 0 is SW_64KB_D; 1-3 are SW_4KB_S; 9-12 appear only on arrays.
|
|
return gfx10_tile_bytes(mode)
|
|
|
|
|
|
def element_bytes(fmt):
|
|
if fmt not in ELEMENT_BYTES:
|
|
raise LayoutError(f"no element-size mapping for DXGI format {fmt}")
|
|
return ELEMENT_BYTES[fmt]
|
|
|
|
|
|
def surface_layout(width, height, fmt, mips, tile=4096):
|
|
"""AMD Gfx10 surface layout: standalone mips smallest-first, then one tail tile."""
|
|
try:
|
|
return compact_surface_layout(width, height, mips, element_bytes(fmt),
|
|
4 if fmt in BC_FORMATS else 1, tile)
|
|
except ValueError as exc:
|
|
raise LayoutError(str(exc)) from exc
|
|
|
|
|
|
def detile(layout, mip, tiled):
|
|
"""Copy the mip's elements out of the tiled allocation into block-linear order."""
|
|
pat = (PATTERNS_4K if layout["tile"] == 4096 else 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)
|
|
|
|
|
|
# --- BC decoding (color only; enough for the downsample check) -------------
|
|
|
|
def _rgb565(c):
|
|
return ((c >> 11) & 31) * 255.0 / 31, ((c >> 5) & 63) * 255.0 / 63, (c & 31) * 255.0 / 31
|
|
|
|
|
|
def decode_bc1_block(data, at):
|
|
c0, c1, idx = struct.unpack_from("<HHI", data, at)
|
|
p0, p1 = _rgb565(c0), _rgb565(c1)
|
|
if c0 > 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 decode_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 decode_pixels(fmt, data, blocks_w, blocks_h):
|
|
"""Return one (r, g, b) per pixel, or None for a format this tool cannot decode."""
|
|
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 = decode_bc1_block(data, (by * blocks_w + bx) * 8)
|
|
elif fmt in (77, 78):
|
|
blk = decode_bc1_block(data, at + 8)
|
|
elif fmt in (80,):
|
|
blk = decode_bc4_block(data, (by * blocks_w + bx) * 8)
|
|
elif fmt in (83,):
|
|
a = decode_bc4_block(data, at)
|
|
b = decode_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 block_means(pixels, blocks_w, blocks_h):
|
|
out = []
|
|
for by in range(blocks_h):
|
|
for bx in range(blocks_w):
|
|
acc = [0.0, 0.0, 0.0]
|
|
for y in range(4):
|
|
for x in range(4):
|
|
q = pixels[(by * 4 + y) * (blocks_w * 4) + bx * 4 + x]
|
|
for k in range(3):
|
|
acc[k] += q[k]
|
|
out.append((acc[0] / 16, acc[1] / 16, acc[2] / 16))
|
|
return out
|
|
|
|
|
|
def downsample2(blocks, blocks_w, blocks_h):
|
|
ow, oh = max(1, blocks_w // 2), max(1, blocks_h // 2)
|
|
out = []
|
|
for y in range(oh):
|
|
for x in range(ow):
|
|
acc = [0.0, 0.0, 0.0]
|
|
n = 0
|
|
for dy in range(2):
|
|
for dx in range(2):
|
|
bx = min(blocks_w - 1, 2 * x + dx)
|
|
by = min(blocks_h - 1, 2 * y + dy)
|
|
q = blocks[by * blocks_w + bx]
|
|
for k in range(3):
|
|
acc[k] += q[k]
|
|
n += 1
|
|
out.append(tuple(a / n for a in acc))
|
|
return out
|
|
|
|
|
|
def mean_abs_error(a, b):
|
|
if not a:
|
|
return None
|
|
return sum(abs(x - y) for pa, pb in zip(a, b) for x, y in zip(pa, pb)) / (3 * len(a))
|
|
|
|
|
|
# --- RDS5 / ETPF container ------------------------------------------------
|
|
|
|
class Pool:
|
|
"""Read-only RDS5 texture pool, mirroring TtpfReader's catalog checks."""
|
|
|
|
def __init__(self, path):
|
|
self.path = path
|
|
self._fh = open(path, "rb")
|
|
try:
|
|
self.data = mmap.mmap(self._fh.fileno(), 0, access=mmap.ACCESS_READ)
|
|
except Exception:
|
|
self._fh.close()
|
|
raise
|
|
d = self.data
|
|
if len(d) < 256 or d[:4] != b"RDS5":
|
|
raise ValueError("not an RDS5 container or truncated header")
|
|
count = u32(d, 4)
|
|
size_word = u64(d, 8)
|
|
self.virtual_size = size_word & 0x00FFFFFFFFFFFFFF
|
|
if (count < 3 or count > 1_000_002 or (size_word >> 56) != 22 or
|
|
self.virtual_size < 4096 or u32(d, 16) not in
|
|
((count + 31) // 32, (count + 31) // 32 + 1)):
|
|
raise ValueError("unsupported RDS5 index layout")
|
|
self.chunk_count = count - 2
|
|
table_end = 24 + (count - 1) * 8
|
|
self.etpf = align(table_end, 256)
|
|
if table_end > len(d) - 16:
|
|
raise ValueError("RDS5 index exceeds its source")
|
|
table = d[24:table_end]
|
|
if ((u64(table, len(table) - 8) & 0x3FFFFFF) * 256 != len(d) or
|
|
((u64(table, len(table) - 8) >> 30) & 0x3FFFFF) * 4096 != self.virtual_size):
|
|
raise ValueError("RDS5 sentinel does not match its lengths")
|
|
self.chunks = []
|
|
for i in range(self.chunk_count):
|
|
v, nxt = u64(table, i * 8), u64(table, (i + 1) * 8)
|
|
start, end = (v & 0x3FFFFFF) * 256, (nxt & 0x3FFFFFF) * 256
|
|
virt, vend = ((v >> 30) & 0x3FFFFF) * 4096, ((nxt >> 30) & 0x3FFFFF) * 4096
|
|
pad = v >> 52
|
|
codec = (v >> 26) & 15
|
|
packed, size = end - start - pad, vend - virt
|
|
if (start < self.etpf + 16 or end > len(d) or packed < 1 or packed > MAX_OUTPUT or
|
|
size < 1 or size > MAX_OUTPUT or vend > self.virtual_size or
|
|
(codec == 0 and packed != size)):
|
|
raise ValueError(f"RDS5 chunk {i} range is invalid")
|
|
self.chunks.append((virt, size, start, packed, codec, pad))
|
|
eh = d[self.etpf:self.etpf + 16]
|
|
if eh[:4] != b"ETPF" or u32(eh, 4) != ETPF_VERSION:
|
|
raise ValueError("unsupported embedded ETPF version")
|
|
self.textures = u32(eh, 8)
|
|
area = u32(eh, 12)
|
|
metadata = 16 + area + self.textures * 32
|
|
padded = align(metadata, 4096)
|
|
alignment = u32(d, 20)
|
|
source_padding = (alignment >> 28) * 256
|
|
if (self.textures < 1 or self.textures > MAX_TEXTURES or area < self.textures * 16 or
|
|
(alignment & 0x0FFFFFFF) != 0 or padded != self.chunks[0][0] or
|
|
self.etpf + padded + source_padding != self.chunks[0][2]):
|
|
raise ValueError("ETPF tables do not match the data boundary")
|
|
self.area = area
|
|
self.records = d[self.etpf + 16:self.etpf + 16 + area]
|
|
self.ids = d[self.etpf + 16 + area:self.etpf + 16 + area + self.textures * 32]
|
|
self._starts = [c[0] for c in self.chunks]
|
|
|
|
def close(self):
|
|
self.data.close()
|
|
self._fh.close()
|
|
|
|
def descriptor(self, index):
|
|
if not 0 <= index < self.textures:
|
|
raise IndexError(index)
|
|
d = parse_etpf_texture_record(self.records[index * 16:index * 16 + 16])
|
|
d["index"] = index
|
|
d["asset_key_at"] = (
|
|
u64(self.records, self.textures * 16 + index * 8)
|
|
if (self.textures * 16 + index * 8 + 8 <= len(self.records))
|
|
else None)
|
|
return d
|
|
|
|
def chunks_for(self, start, size):
|
|
import bisect
|
|
end = start + size
|
|
i = max(0, bisect.bisect_right(self._starts, start) - 1)
|
|
out = []
|
|
covered = start
|
|
while i < len(self.chunks) and self.chunks[i][0] < end:
|
|
virt, csize, src, packed, codec, pad = self.chunks[i]
|
|
if virt > covered:
|
|
raise ValueError("texture crosses an unmapped virtual range")
|
|
out.append(self.chunks[i])
|
|
covered = min(end, virt + csize)
|
|
i += 1
|
|
if covered != end:
|
|
raise ValueError("texture range is not completely mapped")
|
|
return out
|
|
|
|
def codecs(self, start, size):
|
|
return {c[4] for c in self.chunks_for(start, size)}
|
|
|
|
def readable(self, start, size, *, allow_bcpack=False):
|
|
allowed = ({0, 8} | bcpack.SUPPORTED_REBUILD_CODECS) if allow_bcpack else {0, 8}
|
|
return self.codecs(start, size) <= allowed
|
|
|
|
def read_range(self, start, size, cache=None, *, bcpack_mode="strict"):
|
|
if bcpack_mode not in ("strict", "provisional", "off"):
|
|
raise ValueError(f"invalid bcpack_mode: {bcpack_mode!r}")
|
|
cache = {} if cache is None else cache
|
|
out = bytearray(size)
|
|
for virt, csize, src, packed, codec, pad in self.chunks_for(start, size):
|
|
key = (src, packed, codec, bcpack_mode if codec not in (0, 8) else "raw")
|
|
if key not in cache:
|
|
raw = self.data[src:src + packed]
|
|
if codec == 0:
|
|
cache[key] = bytes(raw)
|
|
elif codec == 8:
|
|
cache[key] = zlib.decompress(raw)
|
|
elif bcpack_mode != "off" and codec in bcpack.SUPPORTED_REBUILD_CODECS:
|
|
cache[key] = bcpack.decode(
|
|
raw, codec, csize, allow_provisional=(bcpack_mode == "provisional")
|
|
)
|
|
else:
|
|
raise BcpackRequired(
|
|
f"range touches a BCPack chunk (codec {codec})"
|
|
)
|
|
if len(cache[key]) != csize:
|
|
raise ValueError("decoded chunk length disagrees with its range")
|
|
lo, hi = max(start, virt), min(start + size, virt + csize)
|
|
out[lo - start:hi - start] = cache[key][lo - virt:hi - virt]
|
|
return bytes(out)
|
|
|
|
def span(self, index, base):
|
|
for j in range(index + 1, self.textures):
|
|
cand = (u32(self.records, j * 16 + 8) & 0x1FFFFF) * 4096
|
|
if cand < base:
|
|
return None
|
|
if cand > base:
|
|
return cand - base
|
|
return self.virtual_size - base
|
|
|
|
|
|
def compact_row(desc):
|
|
"""The reader's compact-profile gate (mode families 4/6/36 and aux mode 2)."""
|
|
return desc["mode"] in (4, 6, 36) or (desc["mode"] == 2 and (desc["meta"] or desc["extra"]))
|
|
|
|
|
|
def validated_compact(desc, eb):
|
|
return (desc["mode"], desc["width"], desc["height"], desc["mips"], desc["non_tail"],
|
|
eb, desc["meta"], desc["extra"]) in VALIDATED_COMPACT
|
|
|
|
|
|
def validated_aux(desc):
|
|
a = VALIDATED_AUX
|
|
return (desc["mips"] == a["mips"] and desc["non_tail"] == a["non_tail"] and
|
|
desc["meta"] == a["meta"] and desc["extra"] == a["extra"] and
|
|
desc["width"] == a["width"] and
|
|
((desc["height"] == a["height_a"] and desc["format"] == a["fmt_a"]) or
|
|
(desc["height"] == a["height_b"] and desc["format"] in BC_FORMATS and
|
|
ELEMENT_BYTES.get(desc["format"]) == a["bytes_b"])))
|
|
|
|
|
|
def analyze(pool, index, *, allow_bcpack=True):
|
|
"""Descriptor + layout + per-mip codec availability for one texture."""
|
|
d = pool.descriptor(index)
|
|
out = dict(descriptor=d, tile_mode=tile_mode(d["mode"]), tile=tile_bytes(d["mode"]))
|
|
if d["dimension"] != 1 or d["depth"] != 1:
|
|
out["blocked"] = "array/volume descriptor"
|
|
return out
|
|
try:
|
|
layout = surface_layout(d["width"], d["height"], d["format"], d["mips"],
|
|
out["tile"])
|
|
except LayoutError as e:
|
|
out["blocked"] = str(e)
|
|
return out
|
|
out["layout"] = layout
|
|
if d["base"] == 0 or (d["base"] >> 12) == 0x1FFFFF:
|
|
out["blocked"] = "no mapped allocation"
|
|
return out
|
|
span = pool.span(index, d["base"])
|
|
out["allocation_span"] = span
|
|
out["span_matches"] = span == layout["size"]
|
|
if not out["span_matches"]:
|
|
out["blocked"] = "layout does not match the allocation span"
|
|
return out
|
|
avail = []
|
|
mip_codecs = []
|
|
for mip in layout["mips"]:
|
|
try:
|
|
codecs = sorted(pool.codecs(d["base"] + mip["offset"], mip["region"]))
|
|
mip_codecs.append(codecs)
|
|
avail.append(pool.readable(d["base"] + mip["offset"], mip["region"],
|
|
allow_bcpack=allow_bcpack))
|
|
except ValueError:
|
|
avail = []
|
|
mip_codecs = []
|
|
break
|
|
out["available"] = avail
|
|
out["mip_codecs"] = mip_codecs
|
|
if avail:
|
|
limit = d["non_tail"] if compact_row(d) else d["mips"]
|
|
limit = max(0, min(limit, len(avail)))
|
|
base_mip = next((i for i in range(limit) if avail[i]), None)
|
|
exported = 0
|
|
if base_mip is not None:
|
|
while base_mip + exported < limit and avail[base_mip + exported]:
|
|
exported += 1
|
|
out["reader_limit"] = limit
|
|
out["reader_base_mip"] = base_mip
|
|
out["reader_exported"] = exported
|
|
out["full_chain"] = bool(avail) and all(avail)
|
|
out["recoverable_mips"] = sum(1 for a in avail[exported:] if a) if base_mip == 0 else 0
|
|
return out
|
|
|
|
|
|
# --- commands --------------------------------------------------------------
|
|
|
|
def find_pools(arg):
|
|
if os.path.isfile(arg):
|
|
return [arg]
|
|
pools = sorted(glob.glob(os.path.join(arg, "data", "ttdb", "**", "*.ttpf"), recursive=True))
|
|
pools += sorted(glob.glob(os.path.join(arg, "pack", "*", "data", "ttdb", "**", "*.ttpf"),
|
|
recursive=True))
|
|
return pools
|
|
|
|
|
|
def scan_pools(paths, as_json=None, include_tuples=False):
|
|
total = dict(pools=0, entries=0, arrays=0, unsupported_format=0, format11=0,
|
|
layout_mismatch=0, reader_full=0, reader_partial=0,
|
|
reader_skip_bcpack=0, whitelist_gated=0, gated_layout_ok=0,
|
|
gated_full_chain=0, gated_full_mips=0, gated_full_bytes=0,
|
|
gated_tail_only=0, gated_partial=0, gated_no_contiguous=0,
|
|
gated_no_layout=0, gated_unknown_format=0,
|
|
gated_other_layout_error=0, gated_span_mismatch=0,
|
|
gated_unreadable_range=0, gated_tuple_distinct=0, gated_by_mode={},
|
|
compact_partial_rows=0, compact_missing_mips=0,
|
|
compact_decodable_mips=0, compact_decodable_bytes=0,
|
|
compact_missing_standalone=0, compact_missing_tail=0,
|
|
compact_rows_measured=0, compact_non_tail_before_tail=0,
|
|
compact_standalone_beyond_limit=0,
|
|
compact_standalone_decodable_beyond_limit=0,
|
|
compact_full_rows=0, after_bcpack_full=0,
|
|
after_bcpack_partial=0, tile_modes={})
|
|
gated_tuples = {}
|
|
bcpack_allowed = {0, 8} | bcpack.SUPPORTED_REBUILD_CODECS
|
|
for path in paths:
|
|
pool = Pool(path)
|
|
try:
|
|
total["pools"] += 1
|
|
for i in range(pool.textures):
|
|
d = pool.descriptor(i)
|
|
total["entries"] += 1
|
|
tm = str(tile_mode(d["mode"]))
|
|
total["tile_modes"][tm] = total["tile_modes"].get(tm, 0) + 1
|
|
if d["dimension"] != 1 or d["depth"] != 1:
|
|
total["arrays"] += 1
|
|
continue
|
|
# The reader's gate: modes 4/6/36 and aux mode 2 need a validated tuple;
|
|
# every other non-0/2 mode is rejected by the same branch.
|
|
aux = d["mode"] == 2 and (d["meta"] or d["extra"])
|
|
if d["mode"] in (0, 2):
|
|
gated = d["mode"] == 2 and aux and not validated_aux(d)
|
|
else:
|
|
eb = ELEMENT_BYTES.get(d["format"])
|
|
gated = not (d["mode"] in (4, 6, 36) and eb is not None and
|
|
validated_compact(d, eb))
|
|
try:
|
|
layout = surface_layout(d["width"], d["height"], d["format"],
|
|
d["mips"], tile_bytes(d["mode"]))
|
|
except LayoutError:
|
|
total["unsupported_format"] += 1
|
|
if d["format"] == 11:
|
|
total["format11"] += 1
|
|
if gated:
|
|
total["whitelist_gated"] += 1
|
|
total["gated_no_layout"] += 1
|
|
if d["format"] not in ELEMENT_BYTES:
|
|
total["gated_unknown_format"] += 1
|
|
else:
|
|
total["gated_other_layout_error"] += 1
|
|
continue
|
|
span = pool.span(i, d["base"])
|
|
if d["base"] == 0 or span != layout["size"]:
|
|
total["layout_mismatch"] += 1
|
|
if gated:
|
|
total["whitelist_gated"] += 1
|
|
total["gated_span_mismatch"] += 1
|
|
continue
|
|
avail = []
|
|
avail_bc = []
|
|
for mip in layout["mips"]:
|
|
try:
|
|
cs = pool.codecs(d["base"] + mip["offset"], mip["region"])
|
|
except ValueError:
|
|
avail = []
|
|
avail_bc = []
|
|
break
|
|
avail.append(cs <= {0, 8})
|
|
avail_bc.append(cs <= bcpack_allowed)
|
|
if not avail:
|
|
total["layout_mismatch"] += 1
|
|
if gated:
|
|
total["whitelist_gated"] += 1
|
|
total["gated_unreadable_range"] += 1
|
|
continue
|
|
if all(avail_bc):
|
|
total["after_bcpack_full"] += 1
|
|
else:
|
|
ft = layout["first_tail"]
|
|
usable_tail_bc = (ft < d["mips"] and all(avail_bc[ft:]))
|
|
lim_bc = (d["mips"] if (not compact_row(d) or usable_tail_bc)
|
|
else min(ft, len(avail_bc)))
|
|
bm_bc = next((k for k in range(lim_bc) if avail_bc[k]), None)
|
|
exp_bc = 0
|
|
if bm_bc is not None:
|
|
while bm_bc + exp_bc < lim_bc and avail_bc[bm_bc + exp_bc]:
|
|
exp_bc += 1
|
|
if exp_bc:
|
|
total["after_bcpack_partial"] += 1
|
|
if gated:
|
|
total["whitelist_gated"] += 1
|
|
total["gated_layout_ok"] += 1
|
|
key = str(d["mode"])
|
|
total["gated_by_mode"][key] = total["gated_by_mode"].get(key, 0) + 1
|
|
tuple_key = (d["mode"], d["width"], d["height"], d["mips"],
|
|
d["non_tail"], layout["element_bytes"])
|
|
gated_tuples[tuple_key] = gated_tuples.get(tuple_key, 0) + 1
|
|
first_tail = layout["first_tail"]
|
|
if all(avail):
|
|
total["gated_full_chain"] += 1
|
|
total["gated_full_mips"] += d["mips"]
|
|
total["gated_full_bytes"] += sum(
|
|
mip["blocks_w"] * mip["blocks_h"] * layout["element_bytes"]
|
|
for mip in layout["mips"])
|
|
else:
|
|
# After the layout fix the export limit is the usable
|
|
# tail tile, or the computed tail boundary when it is
|
|
# not. Count what would still export and what would not.
|
|
usable_tail = (first_tail < d["mips"] and
|
|
all(avail[first_tail:]))
|
|
if usable_tail:
|
|
total["gated_tail_only"] += 1
|
|
limit = d["mips"]
|
|
else:
|
|
limit = min(first_tail, len(avail))
|
|
base_mip = next((k for k in range(limit) if avail[k]), None)
|
|
exported = 0
|
|
if base_mip is not None:
|
|
while base_mip + exported < limit and avail[base_mip + exported]:
|
|
exported += 1
|
|
if exported:
|
|
total["gated_partial"] += 1
|
|
else:
|
|
total["gated_no_contiguous"] += 1
|
|
continue
|
|
is_compact = compact_row(d)
|
|
limit = min(d["non_tail"], len(avail)) if is_compact else len(avail)
|
|
base_mip = next((k for k in range(limit) if avail[k]), None)
|
|
exported = 0
|
|
if base_mip is not None:
|
|
while base_mip + exported < limit and avail[base_mip + exported]:
|
|
exported += 1
|
|
if exported == 0:
|
|
total["reader_skip_bcpack"] += 1
|
|
elif exported < d["mips"]:
|
|
total["reader_partial"] += 1
|
|
if is_compact and base_mip == 0 and exported == limit:
|
|
total["compact_partial_rows"] += 1
|
|
missing = d["mips"] - exported
|
|
total["compact_missing_mips"] += missing
|
|
dec = 0
|
|
for k in range(exported, d["mips"]):
|
|
if avail[k]:
|
|
dec += 1
|
|
if k < layout["first_tail"]:
|
|
total["compact_missing_standalone"] += 1
|
|
else:
|
|
total["compact_missing_tail"] += 1
|
|
total["compact_decodable_mips"] += dec
|
|
total["compact_decodable_bytes"] += sum(
|
|
layout["mips"][k]["blocks_w"] * layout["mips"][k]["blocks_h"] *
|
|
layout["element_bytes"] for k in range(exported, d["mips"])
|
|
if avail[k])
|
|
if dec == missing:
|
|
total["compact_full_rows"] += 1
|
|
else:
|
|
total["reader_full"] += 1
|
|
if d["mode"] in (4, 6, 36):
|
|
total["compact_rows_measured"] += 1
|
|
if d["non_tail"] < layout["first_tail"]:
|
|
total["compact_non_tail_before_tail"] += 1
|
|
total["compact_standalone_beyond_limit"] += (
|
|
layout["first_tail"] - d["non_tail"])
|
|
total["compact_standalone_decodable_beyond_limit"] += sum(
|
|
1 for k in range(d["non_tail"], layout["first_tail"]) if avail[k])
|
|
finally:
|
|
pool.close()
|
|
total["gated_accounted"] = (total["gated_layout_ok"] + total["gated_no_layout"] +
|
|
total["gated_span_mismatch"] + total["gated_unreadable_range"])
|
|
total["gated_tuple_distinct"] = len(gated_tuples)
|
|
# Projected split once the reader uses the computed layout: existing full
|
|
# exports plus compact partials that complete plus gated records that
|
|
# complete; partials lose the completed rows and gain the gated partials.
|
|
total["after_full"] = (total["reader_full"] + total["compact_full_rows"] +
|
|
total["gated_full_chain"])
|
|
total["after_partial"] = (total["reader_partial"] - total["compact_full_rows"] +
|
|
total["gated_partial"])
|
|
total["after_skip"] = total["entries"] - total["after_full"] - total["after_partial"]
|
|
total["after_bcpack_skip"] = (total["entries"] - total["after_bcpack_full"] -
|
|
total["after_bcpack_partial"])
|
|
total["bcpack_unlocked_full"] = total["after_bcpack_full"] - total["after_full"]
|
|
if include_tuples:
|
|
total["gated_tuples"] = [
|
|
{"tuple": list(key), "count": count}
|
|
for key, count in sorted(gated_tuples.items(), key=lambda kv: (-kv[1], kv[0]))
|
|
]
|
|
report = dict(total=total)
|
|
if as_json:
|
|
with open(as_json, "w", encoding="utf-8") as f:
|
|
json.dump(report, f, indent=1)
|
|
print(json.dumps(report, indent=1))
|
|
|
|
|
|
def describe(pool, index):
|
|
a = analyze(pool, index)
|
|
d = a["descriptor"]
|
|
out = dict(index=index, width=d["width"], height=d["height"], format=d["format"],
|
|
mips=d["mips"], non_tail=d["non_tail"], mode=d["mode"],
|
|
tile_mode=a["tile_mode"], tile=a["tile"], base=d["base"],
|
|
meta=d["meta"], extra=d["extra"])
|
|
if "layout" in a:
|
|
out["allocation_size"] = a["layout"]["size"]
|
|
out["allocation_span"] = a.get("allocation_span")
|
|
out["span_matches"] = a.get("span_matches")
|
|
out["first_tail_mip"] = a["layout"]["first_tail"]
|
|
mip_codecs = a.get("mip_codecs") or []
|
|
out["mips_detail"] = [
|
|
dict(index=m["index"], blocks=[m["blocks_w"], m["blocks_h"]], offset=m["offset"],
|
|
region=m["region"], tail=m["in_tail"], tile_origin=[m["tile_x"], m["tile_y"]],
|
|
**({"codecs": mip_codecs[m["index"]]} if m["index"] < len(mip_codecs) else {}))
|
|
for m in a["layout"]["mips"]]
|
|
if "available" in a:
|
|
out["available"] = a["available"]
|
|
out["reader_limit"] = a["reader_limit"]
|
|
out["reader_exported"] = a["reader_exported"]
|
|
out["full_chain"] = a["full_chain"]
|
|
if a.get("blocked"):
|
|
out["blocked"] = a["blocked"]
|
|
return out
|
|
|
|
|
|
def check(pool, index, *, allow_provisional=False):
|
|
a = analyze(pool, index)
|
|
if "layout" not in a:
|
|
print(json.dumps(dict(index=index, blocked=a.get("blocked")), indent=1))
|
|
return 2
|
|
layout = a["layout"]
|
|
d = a["descriptor"]
|
|
cache = {}
|
|
bcpack_mode = "provisional" if allow_provisional else "strict"
|
|
try:
|
|
tiled = pool.read_range(d["base"], layout["size"], cache, bcpack_mode=bcpack_mode)
|
|
except BcpackRequired as exc:
|
|
print(json.dumps(dict(index=index, blocked=str(exc)), indent=1))
|
|
return 2
|
|
except bcpack.BcpackProvisionalError as exc:
|
|
print(json.dumps(dict(
|
|
index=index,
|
|
blocked=f"provisional BCPack history required ({exc}); pass --allow-provisional",
|
|
), indent=1))
|
|
return 2
|
|
decoded, means = [], []
|
|
for mip in layout["mips"]:
|
|
data = detile(layout, mip, tiled[mip["offset"]:mip["offset"] + mip["region"]])
|
|
px = decode_pixels(d["format"], data, mip["blocks_w"], mip["blocks_h"])
|
|
if px is None:
|
|
decoded.append(None)
|
|
means.append(None)
|
|
else:
|
|
decoded.append(px)
|
|
means.append(block_means(px, mip["blocks_w"], mip["blocks_h"]))
|
|
if all(px is None for px in decoded):
|
|
print(json.dumps(dict(index=index, blocked=(
|
|
f"format {d['format']} is not one of the BC formats this tool decodes "
|
|
"(71/72/77/78/80/83)")), indent=1))
|
|
return 2
|
|
errs = []
|
|
for k in range(1, d["mips"]):
|
|
if means[k - 1] is None or means[k] is None:
|
|
errs.append(None)
|
|
continue
|
|
pm = layout["mips"][k - 1]
|
|
cm = layout["mips"][k]
|
|
errs.append(round(mean_abs_error(downsample2(means[k - 1], pm["blocks_w"], pm["blocks_h"]),
|
|
means[k]), 2))
|
|
control = None
|
|
if layout["first_tail"] < d["mips"] and decoded[layout["first_tail"] - 1] is not None:
|
|
tm = layout["mips"][layout["first_tail"]]
|
|
bad = dict(tm, tile_x=0, tile_y=0)
|
|
data = detile(layout, bad, tiled[:layout["tile"]])
|
|
px = decode_pixels(d["format"], data, tm["blocks_w"], tm["blocks_h"])
|
|
if px is not None:
|
|
pm = layout["mips"][layout["first_tail"] - 1]
|
|
control = round(mean_abs_error(
|
|
downsample2(means[layout["first_tail"] - 1], pm["blocks_w"], pm["blocks_h"]),
|
|
block_means(px, tm["blocks_w"], tm["blocks_h"])), 2)
|
|
result = dict(index=index, width=d["width"], height=d["height"], format=d["format"],
|
|
mips=d["mips"], non_tail=d["non_tail"], first_tail=layout["first_tail"],
|
|
tile=layout["tile"], chain_error=errs, max_error=max(
|
|
(e for e in errs if e is not None), default=None),
|
|
tail_origin_control=control, full_chain=a.get("full_chain"))
|
|
print(json.dumps(result, indent=1))
|
|
return 0 if all(e is not None and e <= 6 for e in errs) else 1
|
|
|
|
|
|
def dds_bytes(width, height, fmt, mips, linear, payload):
|
|
hdr = bytearray(128)
|
|
hdr[0:4] = b"DDS "
|
|
flags = 0x81007 if mips == 1 else 0xA1007
|
|
struct.pack_into("<IIIIIII", hdr, 4, 124, flags, height, width, linear, 0, mips)
|
|
struct.pack_into("<II4s", hdr, 76, 32, 4, b"DX10")
|
|
struct.pack_into("<II", hdr, 108, 0x1000 if mips == 1 else 0x401008, 0)
|
|
return bytes(hdr) + struct.pack("<IIIII", fmt, 3, 0, 1, 0) + payload
|
|
|
|
|
|
def export(pool, index, out_path, mips=None, force=False, *, allow_provisional=False):
|
|
a = analyze(pool, index)
|
|
if "layout" not in a or "available" not in a:
|
|
print(f"texture {index}: {a.get('blocked', 'layout unavailable')}", file=sys.stderr)
|
|
return 2
|
|
d = a["descriptor"]
|
|
if d["format"] not in BC_FORMATS:
|
|
print(f"texture {index}: format {d['format']} is uncompressed; this tool does not "
|
|
"validate uncompressed tiling, so it will not write a DDS", file=sys.stderr)
|
|
return 2
|
|
if not a["full_chain"]:
|
|
print(f"texture {index}: not every source mip uses a supported codec "
|
|
"(zlib/raw/BCPack BC1/BC3/BC4/BC5)", file=sys.stderr)
|
|
return 2
|
|
count = d["mips"] if mips is None else mips
|
|
if count < 1 or count > d["mips"]:
|
|
print(f"texture {index}: --mips must be 1..{d['mips']}", file=sys.stderr)
|
|
return 2
|
|
layout = a["layout"]
|
|
cache = {}
|
|
bcpack_mode = "provisional" if allow_provisional else "strict"
|
|
payload = bytearray()
|
|
try:
|
|
for mip in layout["mips"][:count]:
|
|
tiled = pool.read_range(d["base"] + mip["offset"], mip["region"], cache,
|
|
bcpack_mode=bcpack_mode)
|
|
payload += detile(layout, mip, tiled)
|
|
except BcpackRequired as exc:
|
|
print(f"texture {index}: {exc}", file=sys.stderr)
|
|
return 2
|
|
except bcpack.BcpackProvisionalError as exc:
|
|
print(f"texture {index}: provisional BCPack history required ({exc}); "
|
|
"pass --allow-provisional", file=sys.stderr)
|
|
return 2
|
|
if 148 + len(payload) > MAX_OUTPUT:
|
|
print("DDS exceeds the 256 MiB output limit", file=sys.stderr)
|
|
return 2
|
|
if os.path.exists(out_path) and not force:
|
|
print(f"{out_path} exists; pass --force to replace it", file=sys.stderr)
|
|
return 2
|
|
top = layout["mips"][0]
|
|
linear = (top["blocks_w"] * top["blocks_h"] * layout["element_bytes"]
|
|
if d["format"] in BC_FORMATS else d["width"] * layout["element_bytes"])
|
|
with open(out_path, "wb") as f:
|
|
f.write(dds_bytes(d["width"], d["height"], d["format"], count, linear, payload))
|
|
print(f"texture {index}: {d['width']}x{d['height']} fmt {d['format']} {count} mips "
|
|
f"({148 + len(payload)} bytes) -> {out_path}")
|
|
return 0
|
|
|
|
|
|
def main(argv=None):
|
|
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
|
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
ps = sub.add_parser("scan", help="aggregate counters over pools")
|
|
ps.add_argument("paths", nargs="+")
|
|
ps.add_argument("--json")
|
|
ps.add_argument("--tuples", action="store_true",
|
|
help="include the distinct rejected compact-profile tuples")
|
|
pl = sub.add_parser("layout", help="descriptor fields and mip offsets")
|
|
pl.add_argument("pool")
|
|
pl.add_argument("index", type=int)
|
|
pc = sub.add_parser("check", help="decode a chain and report downsample error")
|
|
pc.add_argument("pool")
|
|
pc.add_argument("index", type=int)
|
|
pc.add_argument("--allow-provisional", action="store_true",
|
|
help="allow unconfirmed built-in BCPack history entries")
|
|
pe = sub.add_parser("export", help="write the full chain as DDS")
|
|
pe.add_argument("pool")
|
|
pe.add_argument("index", type=int)
|
|
pe.add_argument("out")
|
|
pe.add_argument("--mips", type=int)
|
|
pe.add_argument("--force", action="store_true")
|
|
pe.add_argument("--allow-provisional", action="store_true",
|
|
help="allow unconfirmed built-in BCPack history entries")
|
|
a = ap.parse_args(argv)
|
|
if a.cmd == "scan":
|
|
pools = []
|
|
for p in a.paths:
|
|
pools += find_pools(p)
|
|
scan_pools(pools, a.json, a.tuples)
|
|
return 0
|
|
pool = Pool(a.pool)
|
|
try:
|
|
if a.cmd == "layout":
|
|
print(json.dumps(describe(pool, a.index), indent=1))
|
|
return 0
|
|
if a.cmd == "check":
|
|
return check(pool, a.index, allow_provisional=a.allow_provisional)
|
|
return export(pool, a.index, a.out, a.mips, a.force,
|
|
allow_provisional=a.allow_provisional)
|
|
finally:
|
|
pool.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|