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:
authorwxrrry <127362966+wxrrry@users.noreply.github.com>2026-11-23 00:17:56 +0300
committerwxrrry <127362966+wxrrry@users.noreply.github.com>2026-11-23 00:17:56 +0300
commitcac7fe25cf488d620dd7bb2a8cec4ba83217b02a (patch)
treeeda137d76b9a543c86d89431414274d9d784c285 /tools/tex_headers.py
parent14c5b93ff7943e1f578a37f0f1efbd05ce076299 (diff)
tex_headers: address review comments on pointer tags, JOAAT name mapping, and mip layout
- tools/tex_headers.py: read mips from flags at +0x42 (flags >> 16) and preserve b6 at +0x3e. - tools/tex_headers.py: validate system pointer tag (0x5...) and reject null pointers so all-zero buffers produce empty records. - tools/tex_headers.py: match texture names by lowercase JOAAT32 hash rather than index position. - docs/textures.md: wrap lines under 118 chars and label dry cell occupancy as hypothesis. - tests/test_tex_headers.py: add tests for differing b6/mips offsets, zero buffers, invalid tags, reversed and missing names.
Diffstat (limited to 'tools/tex_headers.py')
-rw-r--r--tools/tex_headers.py38
1 files changed, 31 insertions, 7 deletions
diff --git a/tools/tex_headers.py b/tools/tex_headers.py
index 96eb2d8ca..fc499489f 100644
--- a/tools/tex_headers.py
+++ b/tools/tex_headers.py
@@ -207,6 +207,18 @@ def gfx10_tile_bytes(mode):
207 return COMPACT_TILE_64K if gfx10_tile_mode(mode) == 0 else COMPACT_TILE_4K 207 return COMPACT_TILE_64K if gfx10_tile_mode(mode) == 0 else COMPACT_TILE_4K
208 208
209 209
210def joaat(s: str) -> int:
211 """Standard Jenkins One-at-a-Time 32-bit hash of lowercase ASCII string."""
212 h = 0
213 for c in s.lower().encode("latin-1", "replace"):
214 h = (h + c) & 0xFFFFFFFF
215 h = (h + (h << 10)) & 0xFFFFFFFF
216 h ^= h >> 6
217 h = (h + (h << 3)) & 0xFFFFFFFF
218 h ^= h >> 11
219 return (h + (h << 15)) & 0xFFFFFFFF
220
221
210def parse_texture_dict_page(data: bytes): 222def parse_texture_dict_page(data: bytes):
211 """Parse a small RAGE texture dictionary system page (e.g. the 400-byte stream in textures.rpf). 223 """Parse a small RAGE texture dictionary system page (e.g. the 400-byte stream in textures.rpf).
212 224
@@ -217,34 +229,46 @@ def parse_texture_dict_page(data: bytes):
217 Within each grcTexture descriptor: 229 Within each grcTexture descriptor:
218 +0x10: dtype (0x980) 230 +0x10: dtype (0x980)
219 +0x18: data pointer (0x60000000 + GPU data offset) 231 +0x18: data pointer (0x60000000 + GPU data offset)
220 +0x38: u16 width, u16 height, u16 depth, u8 mips, u8 DXGI format 232 +0x38: u16 width, u16 height, u16 depth, u8 b6, u8 DXGI format
233 +0x40: u64 flags (bits 16..23: mip count)
221 """ 234 """
222 if len(data) < 400: 235 if len(data) < 400:
223 raise ValueError(f"texture dictionary page truncated: expected >= 400 bytes, got {len(data)}") 236 raise ValueError(f"texture dictionary page truncated: expected >= 400 bytes, got {len(data)}")
224 237
225 p0 = struct.unpack_from("<Q", data, 0x170)[0] & 0x0FFFFFFF 238 p0 = struct.unpack_from("<Q", data, 0x170)[0]
226 p1 = struct.unpack_from("<Q", data, 0x178)[0] & 0x0FFFFFFF 239 p1 = struct.unpack_from("<Q", data, 0x178)[0]
227 h0 = struct.unpack_from("<I", data, 0x180)[0] 240 h0 = struct.unpack_from("<I", data, 0x180)[0]
228 h1 = struct.unpack_from("<I", data, 0x184)[0] 241 h1 = struct.unpack_from("<I", data, 0x184)[0]
229 242
230 names_raw = data[0x140:0x170] 243 names_raw = data[0x140:0x170]
231 names = [s.decode("latin1") for s in names_raw.split(b"\x00") if s] 244 names = [s.decode("latin1") for s in names_raw.split(b"\x00") if s]
245 names_by_hash = {joaat(n): n for n in names}
232 246
233 textures = [] 247 textures = []
234 for idx, (ptr, thash) in enumerate([(p0, h0), (p1, h1)]): 248 for raw_ptr, thash in [(p0, h0), (p1, h1)]:
235 if ptr + 0x40 > len(data): 249 if raw_ptr == 0:
250 continue
251 # Validate that the pointer is in the system page segment (0x5...)
252 if (raw_ptr >> 28) & 0xF != 5:
253 continue
254 ptr = raw_ptr & 0x0FFFFFFF
255 if ptr + 0x48 > len(data):
236 continue 256 continue
237 dtype = struct.unpack_from("<Q", data, ptr + 0x10)[0] 257 dtype = struct.unpack_from("<Q", data, ptr + 0x10)[0]
238 data_off = struct.unpack_from("<Q", data, ptr + 0x18)[0] & 0x0FFFFFFF 258 data_off = struct.unpack_from("<Q", data, ptr + 0x18)[0] & 0x0FFFFFFF
239 w, height, depth, mips, fmt = struct.unpack_from("<HHHBB", data, ptr + 0x38) 259 w, height, depth, b6, fmt = struct.unpack_from("<HHHBB", data, ptr + 0x38)
240 name = names[idx] if idx < len(names) else f"tex_{thash:08x}" 260 flags = struct.unpack_from("<Q", data, ptr + 0x40)[0]
261 mips = (flags >> 16) & 0xFF
262 name = names_by_hash.get(thash, f"tex_{thash:08x}")
241 textures.append({ 263 textures.append({
242 "name": name, 264 "name": name,
243 "hash": thash, 265 "hash": thash,
244 "width": w, 266 "width": w,
245 "height": height, 267 "height": height,
246 "depth": depth, 268 "depth": depth,
269 "b6": b6,
247 "mips": mips, 270 "mips": mips,
271 "flags": flags,
248 "format": fmt, 272 "format": fmt,
249 "format_name": DXGI.get(fmt, f"dxgi{fmt}"), 273 "format_name": DXGI.get(fmt, f"dxgi{fmt}"),
250 "data_offset": data_off, 274 "data_offset": data_off,