upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tools/tex_headers.py
diff options
context:
space:
mode:
authorNotRexed <therealnothingfn@gmail.com>2026-11-20 15:22:16 -0500
committerNotRexed <therealnothingfn@gmail.com>2026-11-20 15:22:16 -0500
commitdb0dc8bd5ebce6e2bc27d47085f7f104eb3037d3 (patch)
tree4fb3edca45f8b4dd3368c6ac2a0e4fdc8ed7065c /tools/tex_headers.py
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>
Diffstat (limited to 'tools/tex_headers.py')
-rw-r--r--tools/tex_headers.py96
1 files changed, 96 insertions, 0 deletions
diff --git a/tools/tex_headers.py b/tools/tex_headers.py
new file mode 100644
index 000000000..e3c07557a
--- /dev/null
+++ b/tools/tex_headers.py
@@ -0,0 +1,96 @@
1"""Find texture headers in carved CPU-segment blobs and pair them with their GPU blob.
2
3Texture descriptor layout (qwords, found in GTA VI's resource CPU segments):
4 q[k-2] packed u64 (unknown: size/flags)
5 q[k-1] 0x980 (descriptor type)
6 q[k] 0x60000000 + offset (GPU data pointer into the resource's GPU segment)
7 q[k+4] u16 width, u16 height, u16 depth/array, u8 ?, u8 DXGI format
8 q[k+5] tiling / flags (0x0001 = AMD SW_4K_S verified on an R16 texture)
9The GPU segment is the carved blob that follows the CPU blob.
10
11Usage:
12 python -I tex_headers.py <carve dir> [--archives a,b] [--out textures_index.json]
13"""
14import argparse
15import json
16import os
17import sys
18from collections import Counter
19
20import numpy as np
21
22DXGI = {28: "R8G8B8A8_UNORM", 29: "R8G8B8A8_UNORM_SRGB", 56: "R16_UNORM", 61: "R8_UNORM", 71: "BC1_UNORM",
23 72: "BC1_UNORM_SRGB", 74: "BC2_UNORM", 77: "BC3_UNORM", 78: "BC3_UNORM_SRGB", 80: "BC4_UNORM",
24 81: "BC4_SNORM", 83: "BC5_UNORM", 84: "BC5_SNORM", 87: "B8G8R8A8_UNORM", 91: "B8G8R8A8_UNORM_SRGB",
25 95: "BC6H_UF16", 96: "BC6H_SF16", 98: "BC7_UNORM", 99: "BC7_UNORM_SRGB", 10: "R16G16B16A16_FLOAT",
26 2: "R32G32B32A32_FLOAT", 41: "R32_FLOAT", 54: "R16_FLOAT", 34: "R16G16_FLOAT", 24: "R10G10B10A2_UNORM",
27 26: "R11G11B10_FLOAT", 49: "R8G8_UNORM"}
28
29
30def scan_archive(carve, arch):
31 files = sorted(json.load(open(os.path.join(carve, arch, "index.json"), encoding="utf-8"))["files"],
32 key=lambda r: r["off"])
33 out = []
34 for i in range(len(files) - 1):
35 v, g = files[i], files[i + 1]
36 # CPU segment: starts with a zero qword but isn't zero-padded GPU data. The GPU segment is whatever
37 # blob follows it (texture data often starts directly with BC blocks, not a zero header).
38 if not (v["head"][:16] == "0" * 16 and not v["head"].startswith("0" * 32)):
39 continue
40 if g["head"].startswith("42400000") or g.get("type") == "bink2":
41 continue
42 # The GPU segment can span several consecutive blobs: everything up to the next CPU/header blob.
43 chain = []
44 for j in range(i + 1, min(len(files), i + 257)):
45 r = files[j]
46 if r["head"].startswith("42400000") or r.get("type") == "bink2" or (
47 r["head"][:16] == "0" * 16 and not r["head"].startswith("0" * 32) and chain):
48 break
49 chain.append([r["off"], r["size"]])
50 with open(os.path.join(carve, arch, f"{v['off']:012x}.bin"), "rb") as f:
51 V = f.read()
52 q = np.frombuffer(V[: len(V) // 8 * 8], "<u8")
53 # Any GPU pointer whose header (4 qwords later) decodes to sane dimensions and a known DXGI format.
54 for k in np.nonzero(((q >> 28) == 6) & ((q >> 32) == 0))[0]:
55 k = int(k)
56 if k < 2 or k + 5 >= len(q):
57 continue
58 d = int(q[k + 4]).to_bytes(8, "little")
59 w, h, depth = int.from_bytes(d[0:2], "little"), int.from_bytes(d[2:4], "little"), int.from_bytes(d[4:6], "little")
60 fmt = d[7]
61 if fmt not in DXGI or not (4 <= w <= 16384 and 4 <= h <= 16384 and 1 <= depth <= 2048):
62 continue
63 # Real texture data is 256-byte aligned (4 KB in every verified case); unaligned hits are other structs.
64 if (int(q[k]) & 0xFF) != 0:
65 continue
66 out.append({"archive": arch, "cpu": v["off"], "gpu": g["off"], "gpu_size": g["size"], "gpu_chain": chain,
67 "data_off": int(q[k]) & 0x0FFFFFFF, "w": w, "h": h, "depth": depth, "b6": d[6], "fmt": fmt,
68 "fmt_name": DXGI.get(fmt, f"dxgi{fmt}"), "flags": int(q[k + 5]), "packed": int(q[k - 2]),
69 "dtype": int(q[k - 1]), "mips": (int(q[k + 5]) >> 16) & 0xFF, "q_index": k})
70 return out
71
72
73def main():
74 ap = argparse.ArgumentParser()
75 ap.add_argument("carve")
76 ap.add_argument("--archives")
77 ap.add_argument("--out")
78 a = ap.parse_args()
79 archs = a.archives.split(",") if a.archives else sorted(
80 d for d in os.listdir(a.carve) if os.path.isfile(os.path.join(a.carve, d, "index.json")))
81 allt = []
82 for arch in archs:
83 t = scan_archive(a.carve, arch)
84 print(f"{arch}: {len(t)} texture headers", flush=True)
85 allt += t
86 path = a.out or os.path.join(a.carve, "textures_index.json")
87 with open(path, "w", encoding="utf-8") as f:
88 json.dump(allt, f)
89 print(f"{len(allt)} textures -> {path}")
90 print("formats:", Counter(t["fmt_name"] for t in allt).most_common(12))
91 print("flags (low 16 bits):", Counter(hex(t["flags"] & 0xFFFF) for t in allt).most_common(10))
92 print("byte6:", Counter(t["b6"] for t in allt).most_common(8))
93
94
95if __name__ == "__main__":
96 sys.exit(main())