Files
mandy/tools/tile_probe.py
NotRexed db0dc8bd5e Initial commit: Leonida, build-it-yourself PC port groundwork
Shader pipeline (carve, FSSM unpack, gfx1013 disassembly, RDNA2 to SPIR-V),
asset export via rpf9-explorer's Core (submodule), PE loader and shim
skeleton, and make_game.py, which builds from the user's own copy of the game.
No game data, keys or executables are included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 15:22:16 -05:00

151 lines
5.3 KiB
Python

"""Find the memory layout of a GPU texture blob empirically.
For each candidate (block size 8 or 16 bytes, linear or tiled with a Z-order inside 4 KB / 64 KB tiles, and a range
of widths), it reorders the compressed blocks, builds a small image from each block's colour endpoint, and scores
how smooth that image is. Real textures are spatially coherent, so the right layout scores best.
Usage:
python -I tile_probe.py <blob> [--out preview.png] [--offset N]
"""
import argparse
import sys
import numpy as np
def morton_decode(i, bits):
"""Split a Z-order index into (x, y), x taking the even bits."""
x = np.zeros_like(i)
y = np.zeros_like(i)
for b in range(bits):
x |= ((i >> (2 * b)) & 1) << b
y |= ((i >> (2 * b + 1)) & 1) << b
return x, y
def tile_dims(tile_bytes, bpe):
n = tile_bytes // bpe # blocks per tile
bits = int(np.log2(n))
tw = 1 << ((bits + 1) // 2) # wider than tall when bits is odd
return tw, n // tw, bits
def block_feature(blocks, bpe):
"""One RGB value per compressed block, from its first colour endpoint."""
if bpe == 8:
c = blocks[:, 0].astype(np.uint32) | (blocks[:, 1].astype(np.uint32) << 8) # BC1 color0 (565)
else:
c = blocks[:, 8].astype(np.uint32) | (blocks[:, 9].astype(np.uint32) << 8) # BC3 color0 (565)
r = ((c >> 11) & 31) * 255 // 31
g = ((c >> 5) & 63) * 255 // 63
b = (c & 31) * 255 // 31
if bpe == 16:
# BC5/BC7 have no 565 endpoint there; mix in the first byte (BC5 red0 / BC7 mode+endpoint bits).
r = (r + blocks[:, 0]) // 2
return np.stack([r, g, b], -1).astype(np.float32)
def layout(nblocks, wblocks, mode, bpe):
"""Linear position (y * wblocks + x) for each stored block, or None if the layout doesn't fit."""
idx = np.arange(nblocks, dtype=np.int64)
if mode == "linear":
x, y = idx % wblocks, idx // wblocks
else:
tb = 4096 if mode == "4k" else 65536
tw, th, bits = tile_dims(tb, bpe)
if wblocks % tw:
return None
per = tw * th
if nblocks % (wblocks * th):
return None
tile, inner = idx // per, idx % per
# x takes the even bits, y the odd ones; with an odd bit count x gets the extra top bit.
ix, iy = morton_decode(inner, (bits + 1) // 2)
tiles_x = wblocks // tw
x = (tile % tiles_x) * tw + ix
y = (tile // tiles_x) * th + iy
return y * wblocks + x
def score(img):
dx = np.abs(np.diff(img, axis=1)).mean()
dy = np.abs(np.diff(img, axis=0)).mean()
return dx + dy
def probe(data: bytes, offset=0):
best = []
for bpe in (8, 16):
raw = np.frombuffer(data[offset:offset + (len(data) - offset) // bpe * bpe], np.uint8).reshape(-1, bpe)
feat = block_feature(raw, bpe)
for wpx in (256, 512, 1024, 2048, 4096):
wb = wpx // 4
hb0 = min(len(raw) // wb, wb) # top mip assumed square or wider
for mode in ("linear", "4k", "64k"):
hb = hb0
if mode != "linear":
th = tile_dims(4096 if mode == "4k" else 65536, bpe)[1]
hb = hb0 // th * th
if hb < 16:
continue
n = wb * hb
pos = layout(n, wb, mode, bpe)
if pos is None:
continue
img = np.zeros((hb * wb, 3), np.float32)
img[pos] = feat[:n]
s = score(img.reshape(hb, wb, 3))
best.append((s, bpe, wpx, mode))
best.sort()
return best
def render(data, offset, bpe, wpx, mode, out):
"""Full BC decode of the reordered top mip through Pillow (BC1 for 8-byte blocks, BC7 for 16-byte)."""
import io
import struct
from PIL import Image
wb = wpx // 4
raw = np.frombuffer(data[offset:offset + (len(data) - offset) // bpe * bpe], np.uint8).reshape(-1, bpe)
hb = min(len(raw) // wb, wb)
if mode != "linear":
th = tile_dims(4096 if mode == "4k" else 65536, bpe)[1]
hb = hb // th * th
n = wb * hb
pos = layout(n, wb, mode, bpe)
lin = np.zeros((n, bpe), np.uint8)
lin[pos] = raw[:n]
w, h = wb * 4, hb * 4
hdr = bytearray(128)
hdr[0:4] = b"DDS "
struct.pack_into("<IIIIIII", hdr, 4, 124, 0x1007, h, w, n * bpe, 0, 1)
if bpe == 8:
struct.pack_into("<II4s", hdr, 76, 32, 4, b"DXT1")
body = bytes(hdr)
else:
struct.pack_into("<II4s", hdr, 76, 32, 4, b"DX10")
body = bytes(hdr) + struct.pack("<IIIII", 98, 3, 0, 1, 0) # BC7_UNORM, TEXTURE2D
struct.pack_into("<I", hdr, 108, 0x1000)
im = Image.open(io.BytesIO(body + lin.tobytes())).convert("RGB")
im.save(out)
return im.size
def main():
ap = argparse.ArgumentParser()
ap.add_argument("blob")
ap.add_argument("--offset", type=int, default=0)
ap.add_argument("--out")
a = ap.parse_args()
data = open(a.blob, "rb").read()
res = probe(data, a.offset)
for s, bpe, wpx, mode in res[:6]:
print(f"score {s:8.2f} {bpe}-byte blocks width {wpx:5} {mode}")
if a.out and res:
_, bpe, wpx, mode = res[0]
print("preview", render(data, a.offset, bpe, wpx, mode, a.out), "->", a.out)
if __name__ == "__main__":
sys.exit(main())