gfx/tools: read MGFX v10 effects and raw-DEFLATE .zxnb assets (PR #2546)

Extend mgfx_effect to parse MonoGame MGFX version 10 structurally alongside
versions 5 and 8 (i32 effect-level counts and indices, -1 pass sentinel,
trailing MGFX signature, profile-agnostic validation, and clean truncation
errors). Add zxnb_effect to peek raw-DEFLATE .zxnb headers and inflate only
.EffectReader streams, excluding SoundEffectReader and other non-effect
assets without decompressing them in full. Update nxcx_shaders' magic list
and DMSH_CHAIN.

Validated on Teenage Mutant Ninja Turtles: Shredder's Revenge
(0100FE701475A000, update 1.0.9): baseline found 5 shaders with 11 [RED]
lines; with this change, shader_split.py finds all 88 unique shaders (64
from 6 main effects, 24 from 13 Content/Shaders/*.zxnb effects) with zero
[RED] lines, skipping all 1,534 non-effect .zxnb assets, and dmsh_build.py
translates and compiles 88/88 to Mac Metal.

Completes PR #2546 (the Vulkan entry-point rename already landed in 80075911).

Co-authored-by: HenWorks <[email protected]>
This commit is contained in:
seeddsandHenWorks committed 2026-11-24 00:34:16 +08:00
1 parent d58f8a7954
commit 3bcf821365
4 files changed
+273 -62

No files matched your search

+1 -1
View File
@@ -1,3 +1,3 @@
// Generated file (dm/gfx/tools/dmsh_chain.py --header): the B shader library translation chain version this source tree expects (default options), one for every game;
// the host loads only <library dir>/b-<V>/. Run dmsh_chain.py --header after changing the chain's tools and commit this file with the change
#define DMSH_CHAIN "9a60b294a470"
#define DMSH_CHAIN "c5e9d75ce7a5"
+125 -56
View File
@@ -2767,6 +2767,10 @@ def xnb_decompress(d, first_block=False):
class _Rd: # a little-endian reader over bytes, with .NET's BinaryReader strings (7-bit encoded length + UTF-8)
def __init__(s, d, p): s.d, s.p = d, p
def u8(s): s.p += 1; return s.d[s.p - 1]
def i32(self) -> int:
v: int = struct.unpack_from('<i', self.d, self.p)[0]
self.p += 4
return v
def un(s, f): v = struct.unpack_from(f, s.d, s.p); s.p += struct.calcsize(f); return v[0] if len(v) == 1 else v
def raw(s, n): s.p += n; return s.d[s.p - n:s.p]
def str(s):
@@ -2782,74 +2786,132 @@ def mgfx_effect(d, p, label):
pixel (0), u32 length, the bytecode, u8 sampler count, {u8 type, u8 texture slot, u8 sampler slot, u8 has state (then 20 bytes of state), name, u8 parameter} each, u8 count + constant buffer
indices, u8 count + attributes {name, u8 usage, u8 index, i16 location}}; the bytecode is u32 control length, the control section, u32 code length, the code area. Then the parameters and the
techniques and passes, read to the end so that a misread shows: a pass names its vertex and pixel shader by index, 255 for none. → [(label, stage, ctl, code)], or None when the header is not
version 8's or its first shader is not an NVN one (four bytes 'MGFX' anywhere in a main).
version 5, 8 or 10's or its first shader is not an NVN one (four bytes 'MGFX' anywhere in a main).
Version 5 (Bastion: MonoGame 3.0's EffectReader, 24 XNB files in Content/Effects and 7 .dx11.mgfxo files in Resources) has no effect key and no constant buffer names, each shader's
bytecode is a whole GLSLC output of one stage, and its shaders are written in MonoGame's DirectX layout: no sampler names and no attribute list"""
v5 = d[p + 4:p + 5] == b'\x05'
if d[p + 4:p + 5] != b'\x08' and not v5: return None
bytecode is a whole GLSLC output of one stage, and its shaders are written in MonoGame's DirectX layout: no sampler names and no attribute list.
Version 10 (MonoGame 3.8+, TMNT: Shredder's Revenge: 6 stock effects in main and 13 Content/Shaders/*.zxnb effects) widens the effect-level counts and indices (constant buffers, their
parameter bindings, shaders, parameters, annotations, techniques, passes and pass shader indices, -1 for none) from u8 to i32 and ends with a trailing 'MGFX' signature."""
ver = d[p + 4] if p + 5 <= len(d) else None
if ver not in (5, 8, 10): return None
v5, v10 = ver == 5, ver == 10
xnb = label.lower().endswith(('.xnb', '.zxnb'))
r = _Rd(d, p + (6 if v5 else 10))
def count() -> int: return r.i32() if v10 else r.u8()
try:
for _ in range(r.u8()):
ncb = count()
if not 0 <= ncb < 0x1000: return None
for _ in range(ncb):
if not v5: r.str()
r.raw(2); r.raw(3 * r.u8())
if d[r.p + 6:r.p + 10] != b'\x91\x68\x86\x19' if v5 else d[r.p + 10:r.p + 14] != CTL_MAGIC: return None # shader count, then the first shader's stage, length (and control length)
except IndexError: return None
r.raw(2)
npar = count()
if not 0 <= npar < 0x10000: return None
r.raw((6 if v10 else 3) * npar)
nsh = count()
if not 0 < nsh < 0x1000 or r.p + 13 > len(d): return None
if d[r.p + 5:r.p + 9] != b'\x91\x68\x86\x19' if v5 else d[r.p + 9:r.p + 13] != CTL_MAGIC: return None # the first shader's stage, length (and control length)
except (IndexError, struct.error): return None
def no_annotations():
if r.u8(): red(f'{label} MGFX @{p:#x}: annotations (MonoGame writes none)')
if count(): red(f'{label} MGFX @{p:#x}: annotations (MonoGame writes none)')
out = []
for i in range(r.u8()):
vs, n = r.u8(), r.un('<I')
if r.p + n > len(d): # a shader whose length runs past the data: a stray 'MGFX' in other content, not an effect
if label.endswith('.xnb'): red(f'{label} MGFX @{p:#x}: shader {i} claims {n:#x} bytes past the end of the file')
return None
bc = r.raw(n)
if v5:
g = glslc_shaders(bc, f'{label} MGFX @{p:#x} shader {i}', need=False)
if vs > 1 or len(g) != 1 or bc[:4] != b'\x91\x68\x86\x19' or struct.unpack_from('<I', bc, 0x44)[0] != n: red(f'{label} MGFX @{p:#x} shader {i}: {n:#x} bytes, not one GLSLC output of one stage')
(_, st, ctl, code), = g
else:
nctl = struct.unpack_from('<I', bc, 0)[0]; ncode = struct.unpack_from('<I', bc, 4 + nctl)[0] if 8 + nctl <= n else -1
if vs > 1 or 8 + nctl + ncode != n: red(f'{label} MGFX @{p:#x} shader {i}: {n:#x} bytes, not a control section and a code area')
ctl, code = bc[4:4 + nctl], bc[8 + nctl:]
st = sph_stage(code) if code[:4] == GFX_MAGIC else None
why = check(label, st, ctl, code) if st == ('ps', 'vs')[vs] else f'the SPH stage is {st}, the effect says {("pixel", "vertex")[vs]}'
if why: red(f'{label} MGFX @{p:#x} shader {i}: {why}')
out.append((f'{label}#MGFX@{p:#x}#{i}', st, ctl, code))
for _ in range(r.u8()):
r.raw(3)
if r.u8(): red(f'{label} MGFX @{p:#x} shader {i}: a sampler state (none in the version 5 effects seen)') if v5 else r.raw(20) # address u / v / w, border colour, filter, max anisotropy, max mip level, mip LOD bias
if not v5: r.str()
r.u8()
r.raw(r.u8())
if not v5:
for _ in range(r.u8()): r.str(); r.raw(4)
def params(): # {u8 class, u8 type, name, semantic, annotations, u8 rows, u8 columns, elements, struct members, the value when it has neither} each → how many
n = r.u8()
for _ in range(n):
typ = r.raw(2)[1]; r.str(); r.str(); no_annotations()
rows, cols = r.u8(), r.u8()
if params() + params(): continue
if typ in (1, 2, 3): r.raw(4 * rows * cols) # bool, int32, single
elif typ == 4: r.str()
return n
params()
for _ in range(r.u8()): # techniques
r.str(); no_annotations()
for _ in range(r.u8()): # passes: name, annotations, vertex / pixel shader index, then the blend, depth-stencil and rasterizer states when present
r.str(); no_annotations(); v, f = r.u8(), r.u8()
if v != 255 and v >= len(out) or f != 255 and f >= len(out): red(f'{label} MGFX @{p:#x}: a pass names shader {v} / {f} of {len(out)}')
for n in (18, 25, 12):
if r.u8(): r.raw(n)
try:
for i in range(nsh):
vs, n = r.u8(), r.un('<I')
if r.p + n > len(d): # a shader whose length runs past the data: a stray 'MGFX' in other content, not an effect
if xnb: red(f'{label} MGFX @{p:#x}: shader {i} claims {n:#x} bytes past the end of the file')
return None
bc = r.raw(n)
if v5:
g = glslc_shaders(bc, f'{label} MGFX @{p:#x} shader {i}', need=False)
if vs > 1 or len(g) != 1 or bc[:4] != b'\x91\x68\x86\x19' or struct.unpack_from('<I', bc, 0x44)[0] != n: red(f'{label} MGFX @{p:#x} shader {i}: {n:#x} bytes, not one GLSLC output of one stage')
(_, st, ctl, code), = g
else:
nctl = struct.unpack_from('<I', bc, 0)[0]; ncode = struct.unpack_from('<I', bc, 4 + nctl)[0] if 8 + nctl <= n else -1
if vs > 1 or 8 + nctl + ncode != n: red(f'{label} MGFX @{p:#x} shader {i}: {n:#x} bytes, not a control section and a code area')
ctl, code = bc[4:4 + nctl], bc[8 + nctl:]
st = sph_stage(code) if code[:4] == GFX_MAGIC else None
why = check(label, st, ctl, code) if st == ('ps', 'vs')[vs] else f'the SPH stage is {st}, the effect says {("pixel", "vertex")[vs]}'
if why: red(f'{label} MGFX @{p:#x} shader {i}: {why}')
out.append((f'{label}#MGFX@{p:#x}#{i}', st, ctl, code))
for _ in range(r.u8()):
r.raw(3)
if r.u8(): red(f'{label} MGFX @{p:#x} shader {i}: a sampler state (none in the version 5 effects seen)') if v5 else r.raw(20) # address u / v / w, border colour, filter, max anisotropy, max mip level, mip LOD bias
if not v5: r.str()
r.u8()
r.raw(r.u8())
if not v5:
for _ in range(r.u8()): r.str(); r.raw(4)
def params(): # {u8 class, u8 type, name, semantic, annotations, u8 rows, u8 columns, elements, struct members, the value when it has neither} each → how many
n = count()
if not 0 <= n < 0x10000: red(f'{label} MGFX @{p:#x}: invalid parameter count {n}')
for _ in range(n):
typ = r.raw(2)[1]; r.str(); r.str(); no_annotations()
rows, cols = r.u8(), r.u8()
if params() + params(): continue
if typ in (1, 2, 3): r.raw(4 * rows * cols) # bool, int32, single
elif typ == 4: r.str()
return n
params()
none = -1 if v10 else 255
ntech = count()
if not 0 <= ntech < 0x1000: red(f'{label} MGFX @{p:#x}: invalid technique count {ntech}')
for _ in range(ntech): # techniques
r.str(); no_annotations()
npass = count()
if not 0 <= npass < 0x1000: red(f'{label} MGFX @{p:#x}: invalid pass count {npass}')
for _ in range(npass): # passes: name, annotations, vertex / pixel shader index, then the blend, depth-stencil and rasterizer states when present
r.str(); no_annotations(); v, f = count(), count()
if v != none and not 0 <= v < len(out) or f != none and not 0 <= f < len(out): red(f'{label} MGFX @{p:#x}: a pass names shader {v} / {f} of {len(out)}')
for n in (18, 25, 12):
if r.u8(): r.raw(n)
if v10 and r.raw(4) != MGFX: red(f'{label} MGFX @{p:#x}: missing trailing MGFX signature at {r.p - 4:#x}')
except (IndexError, struct.error):
red(f'{label} MGFX @{p:#x}: effect is truncated at {r.p:#x} (size {len(d):#x})')
# in Bastion's 24 XNB files the length in front of 'MGFX' (EffectReader reads that many bytes) runs to the file's end, about 0xff0 bytes past the effect, all zeros
if r.p > len(d) or label.endswith('.xnb') and d[r.p:].strip(b'\0'): red(f'{label} MGFX @{p:#x}: the effect ends at {r.p:#x}, the file at {len(d):#x}')
if r.p > len(d) or xnb and d[r.p:].strip(b'\0'): red(f'{label} MGFX @{p:#x}: the effect ends at {r.p:#x}, the file at {len(d):#x}')
return out
def mgfx_shaders(d, label):
out = []
for m in re.finditer(MGFX, d): out += mgfx_effect(d, m.start(), label) or []
if label.endswith('.xnb') and b'EffectReader' in d[:0x200] and not out: red(f'{label}: an XNB effect without an MGFX version 5 or 8 effect of NVN shaders')
if label.lower().endswith(('.xnb', '.zxnb')) and b'.EffectReader' in d[:0x200] and not out:
red(f'{label}: an XNB effect without an MGFX version 5, 8 or 10 effect of NVN shaders')
return out
def zxnb_effect(path, label):
"""Peek a raw-DEFLATE .zxnb (Tribute Games' ParisEngine: TMNT: Shredder's Revenge has 1,547, only 13 effects):
return the full decompressed XNB only when the stream begins with an XNB EffectReader header, b'' for other raw-DEFLATE
.zxnb assets (textures, fonts, SoundEffectReader audio), or None if the file is not raw DEFLATE."""
limit = max(GZIP_FLOOR, NEST * os.path.getsize(path))
with open(path, 'rb') as f:
dec, head = zlib.decompressobj(-zlib.MAX_WBITS), b''
try:
while len(head) < 0x200 and not dec.eof:
chunk = f.read(4096)
if not chunk: break
head += dec.decompress(chunk, 0x200 - len(head))
except zlib.error:
return None
if head[:3] != b'XNB' or b'.EffectReader' not in head:
return b''
out, total = [head], len(head)
try:
if dec.unconsumed_tail:
part = dec.decompress(dec.unconsumed_tail, limit - total + 1)
out.append(part); total += len(part)
while not dec.eof and total <= limit:
chunk = f.read(65536)
if not chunk: break
part = dec.decompress(chunk, limit - total + 1)
out.append(part); total += len(part)
if total <= limit:
part = dec.flush()
out.append(part); total += len(part)
except zlib.error as e:
red(f'{label}: invalid raw-DEFLATE stream ({e})')
if total > limit: red(f'{label}: raw-DEFLATE stream expands beyond its {limit:#x} byte budget')
if not dec.eof: red(f'{label}: truncated raw-DEFLATE stream')
return b''.join(out)
def rel_targets(img):
"""A module image's relative relocations: {slot: target}, both module offsets. RELA's R_AARCH64_RELATIVE entries (type 0x403) carry the target as their addend; DT_RELR (SDK 20, tag 36,
size tag 35) packs slots whose own 8 bytes are the target: an even entry is a slot (the next slot follows it), an odd one a bitmap of the 63 slots after the last (as loader.c relr_apply)"""
@@ -4588,6 +4650,13 @@ def fzlc_shaders(path, label):
def scan_file_in(args):
path, rel = args[:2]
stem, ext = os.path.splitext(path)
if ext.lower() == '.zxnb':
d = zxnb_effect(path, '/' + rel)
if d is not None:
if not d: return rel, {}, set(), {}
if d[:3] == b'XNB' and d[4] == 5 and d[5] & 0xC0 and len(d) > 14: d = xnb_decompress(d)
found = mgfx_shaders(d, '/' + rel)
return rel, uniq_keys(found), var_pairs(found), {}
with open(path, 'rb') as f:
head = f.read(16)
if head[:4] == b'NXSD':
@@ -4798,7 +4867,7 @@ def scan_file_in(args):
if d[:3] == b'XNB' and d[4] == 5 and d[5] & 0xC0 and len(d) > 14: # a compressed XNB (Stardew Valley: 3,535 LZ4 files, 8 LZX): only effects (EffectReader) carry MGFX shaders, and for LZX the reader names sit in the first block, so it decides whether the rest is worth decoding
if d[5] & 0x80:
head_d = xnb_decompress(d, first_block=True)
if b'EffectReader' not in head_d: return rel, {}, set(), {}
if b'.EffectReader' not in head_d: return rel, {}, set(), {}
d = head_d if len(head_d) >= struct.unpack_from('<I', d, 10)[0] else xnb_decompress(d)
else:
d = xnb_decompress(d)
@@ -4822,7 +4891,7 @@ def nxcx_shaders(f, label):
Seek to GLSLC candidates through a mapping so random magic in compressed textures needs no full-file copy.
"""
from romfs import mmap
other = (b'BNSH\0\0\0\0', b'SLCT\x03\x10\0\0', MGFX + b'\x05', MGFX + b'\x08', NEEDLE, SUMO_NX, NVNSH)
other = (b'BNSH\0\0\0\0', b'SLCT\x03\x10\0\0', MGFX + b'\x05', MGFX + b'\x08', MGFX + b'\x0a', NEEDLE, SUMO_NX, NVNSH)
overlap = max(map(len, other)) - 1
offsets, tail, at = [], b'', 0
loose = False
+142
View File
@@ -0,0 +1,142 @@
"""Regression tests for MGFX v10 and raw-DEFLATE .zxnb shader readers using synthetic inputs."""
import contextlib
import io
from pathlib import Path
import struct
import sys
import tempfile
import unittest
import zlib
REPO = Path(__file__).resolve().parents[3]
sys.path.insert(0, str(REPO / 'dm/tools/fuzz'))
import fuzz_shader_split as fz
ss = fz.ss
def net_str(text: bytes) -> bytes:
"""Encode bytes with a .NET 7-bit length prefix."""
n = len(text)
out = bytearray()
while True:
b = n & 0x7F
n >>= 7
out.append(b | 0x80 if n else b)
if not n:
return bytes(out) + text
def raw_deflate(data: bytes) -> bytes:
comp = zlib.compressobj(wbits=-zlib.MAX_WBITS)
return comp.compress(data) + comp.flush()
def mgfx_v10_effect(profile: int = 0x28, cb_name: bytes = b'global') -> bytes:
"""Build a spec-accurate MonoGame MGFX v10 effect with one vertex and one pixel shader."""
out = bytearray(b'MGFX')
out += struct.pack('<BBI', 10, profile, 0x12345678)
# 1 constant buffer: name, i16 size, i32 param_count=1, {i32 param=0, u16 offset=0}
out += struct.pack('<i', 1)
out += net_str(cb_name) + struct.pack('<hi', 16, 1) + struct.pack('<iH', 0, 0)
# 2 shaders: vertex (1), pixel (0)
out += struct.pack('<i', 2)
for is_vs, (ctl, code) in ((1, fz.VS), (0, fz.PS)):
bc = struct.pack('<I', len(ctl)) + ctl + struct.pack('<I', len(code)) + code
out += struct.pack('<BI', is_vs, len(bc)) + bc
out += bytes((0, 1, 0, 0)) # 0 samplers, 1 cbuffer index (0), 0 attributes
# 1 parameter: class=0, type=3 (Single), name, semantic, 0 annotations, 1x4, 0 elements, 0 members, 4 floats
out += struct.pack('<i', 1)
out += bytes((0, 3)) + net_str(b'Tint') + net_str(b'') + struct.pack('<i', 0) + bytes((1, 4))
out += struct.pack('<ii', 0, 0) + bytes(16)
# 1 technique, 2 passes (one VS+PS, one PS-only with -1 sentinel)
out += struct.pack('<i', 1) + net_str(b'Main') + struct.pack('<ii', 0, 2)
out += net_str(b'P0') + struct.pack('<iii', 0, 0, 1) + bytes((0, 0, 0))
out += net_str(b'P1') + struct.pack('<iii', 0, -1, 1) + bytes((0, 0, 0))
out += b'MGFX'
return bytes(out)
def mgfx_v8_effect() -> bytes:
"""Build a spec-accurate MonoGame MGFX v8 effect (u8 counts, 255 none sentinel, no MGFX tail)."""
out = bytearray(b'MGFX')
out += struct.pack('<BBI', 8, 1, 0x12345678)
out += bytes((1,)) + net_str(b'global') + struct.pack('<hBBH', 16, 1, 0, 0)
out += bytes((1,))
ctl, code = fz.PS
bc = struct.pack('<I', len(ctl)) + ctl + struct.pack('<I', len(code)) + code
out += struct.pack('<BI', 0, len(bc)) + bc + bytes((0, 1, 0, 0))
out += bytes((0, 1)) + net_str(b'Main') + bytes((0, 1)) + net_str(b'P0') + bytes((0, 255, 0, 0, 0, 0))
return bytes(out)
def xnb_effect(effect: bytes) -> bytes:
reader = b'Microsoft.Xna.Framework.Content.EffectReader, MonoGame.Framework'
body = b'\x01' + net_str(reader) + bytes(5) + b'\x01' + struct.pack('<I', len(effect)) + effect
return b'XNBS\x05\x01' + struct.pack('<I', 10 + len(body)) + body
class MgfxV10Readers(unittest.TestCase):
def test_embedded_mgfx8_still_parses(self):
rows = ss.mgfx_shaders(b'prefix' + mgfx_v8_effect() + b'suffix', 'main')
self.assertEqual([row[1] for row in rows], ['ps'])
self.assertEqual(rows[0][2:], fz.PS)
def test_embedded_mgfx10_structural_and_profile_agnostic(self):
# Include b'MGFX' inside a constant buffer name and a non-0x28 profile byte to confirm structural parsing.
effect = mgfx_v10_effect(profile=0x29, cb_name=b'MGFX_cb')
rows = ss.mgfx_shaders(b'prefix' + effect + b'suffix', 'main')
self.assertEqual([row[1] for row in rows], ['vs', 'ps'])
self.assertEqual(rows[0][2:], fz.VS)
self.assertEqual(rows[1][2:], fz.PS)
def test_embedded_mgfx10_missing_tail_reports_red(self):
effect = mgfx_v10_effect()
with self.assertRaises(SystemExit) as ctx:
ss.mgfx_shaders(effect[:-4], 'main')
self.assertIn('[RED]', str(ctx.exception))
self.assertIn('trailing MGFX', str(ctx.exception))
def test_deflated_zxnb_effect(self):
xnb = xnb_effect(mgfx_v10_effect())
with tempfile.TemporaryDirectory() as tmp, contextlib.redirect_stdout(io.StringIO()) as log:
path = Path(tmp) / 'Effect.zxnb'
path.write_bytes(raw_deflate(xnb))
_, shaders, _, extra = ss.scan_file((str(path), 'Content/Shaders/Effect.zxnb'))
self.assertEqual({row[1] for row in shaders.values()}, {'vs', 'ps'})
self.assertEqual(extra, {})
self.assertEqual(log.getvalue(), '')
def test_zxnb_skips_sound_effect_reader_and_non_effect_assets(self):
sfx = b'XNBS\x05\x01' + struct.pack('<I', 0x20000) + b'\x01' + net_str(b'Microsoft.Xna.Framework.Content.SoundEffectReader') + bytes(0x20000)
tex = b'XNBS\x05\x01' + struct.pack('<I', 0x20000) + b'\x01' + net_str(b'Microsoft.Xna.Framework.Content.Texture2DReader') + bytes(0x20000)
for name, payload in (('LogoSFX.zxnb', sfx), ('Sprite.zxnb', tex)):
with self.subTest(name=name), tempfile.TemporaryDirectory() as tmp, contextlib.redirect_stdout(io.StringIO()) as log:
path = Path(tmp) / name
path.write_bytes(raw_deflate(payload))
_, shaders, pairs, extra = ss.scan_file((str(path), f'Content/{name}'))
self.assertEqual((shaders, pairs, extra, log.getvalue()), ({}, set(), {}, ''))
def test_zxnb_damaged_effect_reports_unreadable(self):
bad_tail = raw_deflate(xnb_effect(mgfx_v10_effect()[:-4]))
truncated = raw_deflate(xnb_effect(mgfx_v10_effect()))[:-12]
for label, payload in (('missing tail', bad_tail), ('truncated stream', truncated)):
with self.subTest(label=label), tempfile.TemporaryDirectory() as tmp, contextlib.redirect_stdout(io.StringIO()) as log:
path = Path(tmp) / 'Broken.zxnb'
path.write_bytes(payload)
_, shaders, pairs, extra = ss.scan_file((str(path), 'Content/Shaders/Broken.zxnb'))
self.assertEqual((shaders, pairs), ({}, set()))
self.assertIsInstance(extra, tuple)
self.assertEqual(extra[0], 'unreadable')
self.assertIn('[RED]', log.getvalue())
def test_non_deflate_zxnb_falls_through_cleanly(self):
with tempfile.TemporaryDirectory() as tmp, contextlib.redirect_stdout(io.StringIO()) as log:
path = Path(tmp) / 'Plain.zxnb'
path.write_bytes(b'NOT_DEFLATE_DATA' * 16)
_, shaders, pairs, extra = ss.scan_file((str(path), 'Content/Plain.zxnb'))
self.assertEqual((shaders, pairs, extra, log.getvalue()), ({}, set(), {}, ''))
if __name__ == '__main__':
unittest.main()
+5 -5
View File
@@ -536,11 +536,11 @@ Finding, keying and translating shaders (`dm/gfx/tools/shader_split.py`, `dmsh_b
### MGFX effects in MonoGame XNB files
- **How it is stored:** the 13 XNB files in `Content/Effects` (XNB platform `S`, uncompressed, read by `EffectReader`) each hold one MGFX effect, and `main` holds SpriteBatch's built-in one; no other file and no GLSLC output. A compressed XNB (MonoGame's LZX flag 0x80 or LZ4 flag 0x40, as in Stardew Valley) is decompressed first (`xnb_decompress`).
- **Layout:** MGFX version 8 is MonoGame's own layout: `MGFX`, u8 version, u8 profile, u32 effect key, the constant buffers, then each shader as u8 vertex (1) / pixel (0), u32 length, the bytecode (u32 control length, the control section, u32 code length, the code area: GLSLC's output without its header), the samplers, the constant buffer indices and an attribute list (as MonoGame's OpenGL backend writes it), then the parameters and the techniques with their passes (`mgfx_effect`).
- **Reader:** the reader parses every effect to its end, which checks the layout; in `main`, four bytes `MGFX` count only when version 8 and a control section follow.
- **Pairing:** a pass names its vertex and pixel shader by index, 255 for none, and 10 of the 13 effects have no vertex shader: SpriteBatch binds its own and the effect's pass only replaces the pixel shader. Vertex and pixel shaders therefore combine across effects, and they are paired as single-stage programs.
- **Found on:** [Celeste](../../dm/games/01002B30028F6000/README.md) (MonoGame for the Switch, its C# compiled to C++ by Sickhead's BRUTE): 26 shaders (20 pixel, 6 vertex), all with Mario's binding layout; every binding decodes. [Stardew Valley](../../dm/games/0100E65002BB8000/README.md) (compressed XNB).
- **How it is stored:** the 13 XNB files in `Content/Effects` (XNB platform `S`, uncompressed, read by `EffectReader`) each hold one MGFX effect, and `main` holds SpriteBatch's built-in one; no other file and no GLSLC output. A compressed XNB (MonoGame's LZX flag 0x80 or LZ4 flag 0x40, as in Stardew Valley) is decompressed first (`xnb_decompress`). Tribute Games' ParisEngine wraps XNB assets in raw DEFLATE as `.zxnb` files; `zxnb_effect` peeks the first 0x200 decompressed bytes to skip non-effect assets (including `SoundEffectReader`) and inflates only `.EffectReader` streams.
- **Layout:** MGFX version 8 is MonoGame's own layout: `MGFX`, u8 version, u8 profile, u32 effect key, the constant buffers, then each shader as u8 vertex (1) / pixel (0), u32 length, the bytecode (u32 control length, the control section, u32 code length, the code area: GLSLC's output without its header), the samplers, the constant buffer indices and an attribute list (as MonoGame's OpenGL backend writes it), then the parameters and the techniques with their passes (`mgfx_effect`). MGFX version 10 (MonoGame 3.8+) keeps the same header and per-shader layout, widens effect-level counts and indices (constant buffers, parameter bindings, shaders, parameters, annotations, techniques, passes and pass shader indices, with −1 for none) from u8 to i32, and appends a trailing `MGFX` signature.
- **Reader:** the reader parses every effect to its end, which checks the layout; in `main`, four bytes `MGFX` count only when version 5, 8 or 10 and a control section (or GLSLC output for v5) follow.
- **Pairing:** a pass names its vertex and pixel shader by index, 255 for none (v5/v8) or −1 for none (v10), and 10 of Celeste's 13 effects have no vertex shader: SpriteBatch binds its own and the effect's pass only replaces the pixel shader. Vertex and pixel shaders therefore combine across effects, and they are paired as single-stage programs.
- **Found on:** [Celeste](../../dm/games/01002B30028F6000/README.md) (MonoGame for the Switch, its C# compiled to C++ by Sickhead's BRUTE): 26 shaders (20 pixel, 6 vertex), all with Mario's binding layout; every binding decodes. [Stardew Valley](../../dm/games/0100E65002BB8000/README.md) (compressed XNB). Teenage Mutant Ninja Turtles: Shredder's Revenge (`0100FE701475A000`, update 1.0.9, no profile yet): MGFX version 10 in 6 built-in effects in `main` and 13 `.zxnb` effects in `Content/Shaders/`.
### MGFX version 5 effects (MonoGame 3.0)