Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions dm/gfx/tools/shader_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -1541,27 +1541,53 @@ def pg_sized_shader(d, label):
return [(f'{label}#PGSO', st, ctl, code)]

MTARC, MTSPK = b'ARC\0', b'SPK\0'
# PEANUTS(upstream-candidate) begin: detect both ARC v7 entry widths and stream SPK records.
def mtarc_members(d, label):
"""Capcom's MT Framework archive (The Great Ace Attorney Chronicles: 447 '.arc' files under nativeNXx64): 'ARC\\0', u16 version 7, u16 entry count, then one 0x90-byte entry per file
{char path[128] ('\\\\'-separated, no extension), u32 type hash, u32 stored length, u32 length in the low 29 bits (flags above), u32 offset}; the data is a zlib stream, or stored when the
stored length equals the length. → (path, data) of each entry"""
"""Capcom MT Framework ARC v7: magic, u16 version, u16 count, then a table.
Entries are 0x90 bytes in The Great Ace Attorney Chronicles (128-byte path)
and 0x50 bytes in Resident Evil 6 (64-byte path). Four little-endian u32s
follow the path: type hash, stored size, size (low 29 bits) with flags, offset.
A member is stored if its sizes match, and zlib-compressed otherwise.
"""
import zlib
if len(d) < 8: red(f'{label}: truncated ARC header')
ver, n = struct.unpack_from('<HH', d, 4)
if ver != 7: red(f'{label}: ARC version {ver}: only version 7 is known')
for i in range(n):
o = 8 + 0x90 * i
path = d[o:o + 0x80].split(b'\0')[0].decode('latin-1')
csz, usz, off = struct.unpack_from('<3I', d, o + 0x84); usz &= 0x1fffffff
data = d[off:off + csz] if csz == usz else zlib.decompress(d[off:off + csz])
if not n: return

def entries(path_size):
stride = path_size + 16
end = 8 + stride * n
if end > len(d): return None
rows = []
for i in range(n):
o = 8 + stride * i
path = d[o:o + path_size].split(b'\0', 1)[0].decode('latin-1')
csz, usz, off = struct.unpack_from('<3I', d, o + path_size + 4)
if not path or not path.isprintable() or off < end or off + csz > len(d):
return None
rows.append((path, csz, usz & 0x1fffffff, off))
end = off + csz
return rows

# Prefer the original wide layout if both tables are consistent.
rows = entries(128) or entries(64)
if rows is None: red(f'{label}: no consistent ARC v7 entry layout (0x90 or 0x50)')
for path, csz, usz, off in rows:
data = d[off:off + csz] if csz == usz else zlib.decompress(memoryview(d)[off:off + csz])
if len(data) != usz: red(f'{label}#{path}: {len(data):#x} bytes, the entry says {usz:#x}')
yield path.replace('\\', '/'), data

def mtspk_shaders(d, label):
"""MT Framework's shader package ('SPK\\0'; The Great Ace Attorney Chronicles keeps its only one, sc\\NX_64\\root, in sa/NX_64/root.arc): after the package's tables every shader is a
code area followed directly by its control section, back to back to the end of the file (230 vertex shaders, then 230 pixel shaders). Each control section names its code length (+0x704),
so the code area is the bytes right before it, and every block must start where the previous one ended. The tables that say which vertex and pixel shaders the engine combines are not
read, so the shaders are paired as single-stage programs (separable_pairs)"""
out, end, c = [], None, d.find(CTL_MAGIC)
read, so the shaders are paired as single-stage programs (separable_pairs).
Resident Evil 6 (SDK 6.5.0) uses the same control and code layout in its four
packages. Yield records one at a time so the caller can deduplicate them
without keeping a second copy of the entire package (GameStage is 424 MB).
"""
end, c = None, d.find(CTL_MAGIC)
while c >= 0:
nctl, ncode = struct.unpack_from('<I', d, c + 0x20)[0], struct.unpack_from('<I', d, c + 0x704)[0]
a = c - ncode
Expand All @@ -1570,11 +1596,11 @@ def mtspk_shaders(d, label):
st = sph_stage(code)
why = check(label, st, ctl, code) if st else 'no stage in the SPH'
if why: red(f'{label} SPK @{a:#x}: {why}')
out.append((f'{label}#MTSPK@{a:#x}', st, ctl, code))
yield f'{label}#MTSPK@{a:#x}', st, ctl, code
end = c + nctl; c = d.find(CTL_MAGIC, end)
if end != len(d): red(f'{label} SPK: the last shader ends at {end}, not at the end of the package ({len(d):#x})')
return out

# PEANUTS(upstream-candidate) end
def mtarc_shaders(d, label, out):
for path, m in mtarc_members(d, label):
if m[:4] == MTSPK: out.extend(mtspk_shaders(m, f'{label}#{path}'))
Expand Down
45 changes: 45 additions & 0 deletions dm/tools/tests/test_split_targeted.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import sys
import tempfile
import unittest
import zlib # PEANUTS(upstream-candidate): compressed ARC fixture
from pathlib import Path
from unittest import mock

Expand Down Expand Up @@ -63,6 +64,50 @@ def bnsh(v, p):
return bytes(d)


# PEANUTS(upstream-candidate): ARC v7 layout coverage.
class MtFramework(unittest.TestCase):
@staticmethod
def arc(path_size, members):
table = bytearray(ss.MTARC + struct.pack('<HH', 7, len(members)))
offset = len(table) + len(members) * (path_size + 16)
payload = bytearray()
for path, data, compress in members:
stored = zlib.compress(data) if compress else data
table += path.ljust(path_size, b'\0')
table += struct.pack('<4I', 0x12345678, len(stored), 0x40000000 | len(data), offset)
offset += len(stored)
payload += stored
return bytes(table + payload)

def test_arc_v7_both_layouts(self):
vctl, vcode = vs(1)
pctl, pcode = ps(2)
head = ss.MTSPK + bytes(28)
spk = head + vcode + vctl + pcode + pctl
members = [(b'other\\plain', b'stored member', False), (b'sc\\NX_64\\root', spk, True)]
for path_size in (128, 64):
with self.subTest(path_size=path_size):
arc = self.arc(path_size, members)
self.assertEqual(list(ss.mtarc_members(arc, 'test.arc')),
[('other/plain', b'stored member'), ('sc/NX_64/root', spk)])
found = []
ss.mtarc_shaders(arc, 'test.arc', found)
label = 'test.arc#sc/NX_64/root#MTSPK'
self.assertEqual(found, [
(f'{label}@{len(head):#x}', 'vs', vctl, vcode),
(f'{label}@{len(head) + len(vcode) + len(vctl):#x}', 'ps', pctl, pcode),
])

def test_arc_v7_rejects_invalid_tables(self):
arc = self.arc(64, [(b'first', b'one', False), (b'second', b'two', False)])
for field, value in ((8 + 76, 8), (8 + 80 + 76, 8 + 160 + 1), (8 + 80 + 76, len(arc))):
with self.subTest(field=field, value=value):
malformed = bytearray(arc)
struct.pack_into('<I', malformed, field, value)
with self.assertRaisesRegex(SystemExit, r'\[RED\] test.arc: no consistent ARC v7 entry layout'):
list(ss.mtarc_members(malformed, 'test.arc'))


class Targeted(unittest.TestCase):
def setUp(self):
self.tmp = Path(tempfile.mkdtemp()); self.addCleanup(lambda: shutil.rmtree(self.tmp))
Expand Down
1 change: 1 addition & 0 deletions docs/agents/symptoms.md
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- every frame black, the title screen included, while the game runs at full rate and draws (about 19,000 NVN calls a frame), with `[RED] shader … is not in the shader map` (26 keys in the first minute) and `program has no vertex shader`; the dumped shaders (`DM_NVN_SHDUMP`) match no file byte for byte, and some differ from a split shader only in the control section's compile information (+0x7d0), which is a sign of another GLSLC compile of the same source, not of a shared section. → [Gust's chunked zlib `.gz` files](../reference/shaders.md#gusts-chunked-zlib-gz-files)
- the split finds no shaders in a RomFS made of `.sdfdata`, `.sdftoc` and `.sdfver` files. → [Shader records in Snowdrop SDF data slices](../reference/shaders.md#shader-records-in-snowdrop-sdf-data-slices)
- the survey finds no shader container in a RomFS of `.toc` and `.cas` files (`Data/NX`, `Patch/NX`, `Data/layout.toc`, `Data/initfs_NX`); the split reads every file loosely and finds nothing. → [CompiledBytecode resources in Frostbite superbundles](../reference/shaders.md#compiledbytecode-resources-in-frostbite-superbundles)
- Shader extraction fails on ARC v7 archives with 64-byte path fields. → [ARC v7 archives with short paths](../reference/shaders.md#arc-v7-archives-with-short-paths)
- `[RED] ... PKZ version 0x10000: only 0x20000 is known` stops the shader split. → [PKZ version 1 archives](../reference/shaders.md#pkz-version-1-archives)
- the split finds zero shaders and refuses the shader build, although the CPK's DAT archives contain the objects passed to NVN at boot. → [Size-delimited PlatinumGames shader objects](../reference/shaders.md#size-delimited-platinumgames-shader-objects)
- the CPK directory and CRILAYLA decoder find shader files, but the split does not recognize their contents. → [Level-5 NNDXDHSC shader objects](../reference/shaders.md#level-5-nndxdhsc-shader-objects)
Expand Down
8 changes: 8 additions & 0 deletions docs/reference/shaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ Finding, keying and translating shaders (`dm/gfx/tools/shader_split.py`, `dmsh_b
- **Note:** `CustomShaderPackage.mfx` (`MFX\0`, the effect descriptions) holds no shader code, and neither `main` nor any other ARC entry does.
- **Found on:** [The Great Ace Attorney Chronicles](../../dm/games/010036E00FB20000/README.md) (Capcom's MT Framework): all 460 build, with Mario's binding layout; every binding decodes.

<!-- PEANUTS(upstream-candidate): ARC v7 entry widths -->
### ARC v7 archives with short paths

- **Symptom:** Shader extraction fails on ARC v7 archives with 64-byte path fields.
- **Cause:** The reader assumed the 128-byte path field and read the length and offset fields at the wrong positions.
- **Fix:** Validate the member table for both entry widths and read shader records from SPK packages one at a time.
- **Found on:** Resident Evil 6 2.0.0 (v131072).

### PKZ version 1 archives

- **Symptom:** `[RED] ... PKZ version 0x10000: only 0x20000 is known` stops the shader split.
Expand Down
2 changes: 2 additions & 0 deletions peanuts/UPSTREAM-EDITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ Also gated, but not `#ifdef`-paired with upstream code (new Peanuts-owned files/

## Summary of good upstream-contribution candidates (general fixes, not Peanuts-specific)

- MT Framework ARC v7 short-path layout and streamed SPK records ([shader reader](../docs/reference/shaders.md#arc-v7-archives-with-short-paths)).

1. `dm/host/plat/plat_android.c` — stack guard-page exclusion (IL2CPP GC crash fix)
2. `dm/host/crash.c` — stripped-library lib+offset in crash output
3. `dm/host/loader.c` — module arena (fixes an Android-only OOB read vs the console's layout)
Expand Down