Skip to content
Merged
1 change: 1 addition & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ jobs:
tests/startup_gfx2hlsl_capture_check.py \
tests/startup_translator_check.py \
tests/startup_gfx2hlsl_smem_offset_check.py \
tests/startup_archive_requests_check.py \
tests/startup_gfx2hlsl_refusals_check.py \
tests/startup_gfx2hlsl_scalar_m0_check.py \
tests/startup_gfx2hlsl_readfirstlane_uniform_check.py \
Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ python3 -m venv out/pe-env
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_capture_check.py
./out/pe-env/bin/python -B tests/startup_translator_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_smem_offset_check.py
./out/pe-env/bin/python -B tests/startup_archive_requests_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_refusals_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_scalar_m0_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_readfirstlane_uniform_check.py
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,10 @@ if(EXISTS "${INTAKE_PYTHON}")
add_test(NAME startup_gfx2hlsl_capture COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_capture_check.py")
set_tests_properties(startup_gfx2hlsl_capture PROPERTIES TIMEOUT 120)
# archive_requests.py writes runtime-keyed shader requests from a synthetic archive.
add_test(NAME startup_archive_requests COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_archive_requests_check.py")
set_tests_properties(startup_archive_requests PROPERTIES TIMEOUT 120)
# Two integer operations whose refusal was never reached: v_cvt_i32_f32 with an output
# modifier, and v_readfirstlane_b32 without a proven uniform source.
add_test(NAME startup_gfx2hlsl_op_refusals COMMAND "${INTAKE_PYTHON}" -B
Expand Down
61 changes: 61 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,67 @@ It writes:
| `<log>/stderr.txt` | the stand-ins' logs (`x2w-d3d12`, `x2w-dstorage`, `x2w-xmem`, `x2w-runtime`, ...) |
| `<log>/files.txt` | every file the process opened |

### Shader requests from the archive, before a run

`tools/startup/archive_requests.py <archive> <shader cache> [--roots <dir or req_*.root> ...]` writes
requests without starting the title, then `gfx2hlsl.py --pending <shader cache>` translates them:

```powershell
python -B tools/startup/archive_requests.py <shader archive> out/startup/shader_cache
python -B tools/startup/gfx2hlsl.py --pending out/startup/shader_cache
```

A startup run that traced every DirectStorage read showed where the native programs come from:
zlib streams in the shader archive that start on 64-byte boundaries and inflate to 64 KiB. Inflated
offline, those streams hold the exact bytes the runtime later records. Each program has a 36-byte
header followed by `s_inst_prefetch`, is padded in 64-byte blocks and starts on a 256-byte step,
and in every measured pipeline the vertex program directly follows the pixel program. The tool
carves every program that ends in `s_endpgm` (an all-zero block inside a program would cut it
short), pairs each pixel program with a vertex program that starts on the next 256-byte step
and writes `req_<key>.{a,b,root}` with the runtime's key.

What it cannot take from the archive:

- **Root signatures.** None of the recorded root signatures occurs in the archive or the
executable; the title appears to serialize them at run time. The tool reads `req_*.root` files
from earlier runs (by default those in the cache) and writes each pair once per distinct root.
A key is only used when the title binds that pair with that root, so the extra requests cost
translation time and nothing else.
- **Pipelines the title builds from other sources.** Programs embedded in the executable as DXBC
containers are not in the archive.
- **Other pairings.** A program shared by several pipelines, or a pair that is not adjacent, is
only found by a run (`run.ps1 -Translate`).
- **Programs cut off at the end of a stream.** A zlib stream that starts on the 64-byte boundary
right after the previous one continues it, and the two are carved as one, so a program split
between them is kept. A program that reaches the end of such a joined run is skipped.

A stream that would inflate past 1 MiB is counted as `oversized` and skipped without inflating it in
full, and a joined run stops growing at 64 MiB. A request is written as `.b`, `.root`, then `.a`;
if one part cannot be written, no `.a` and no temporary file are left and the run exits with an
error, as it does when the archive yields no pair at all.

In one local measurement the tool joined 1,079 zlib streams into 800 runs, carved 4,982 programs
(2,730 pixel, 2,115 vertex, 137 other) and wrote 3,774 requests for 1,258 pairs and three recorded
root signatures in 22 s. Eight of the eleven pipelines that a cold run recorded were among them with
identical keys, and their translations (`<key>.txt`, both `.hlsl` files and both `.cso` files) were
byte-identical to those from the run. The other three were two pipelines whose programs are DXBC
containers in the executable and one pipeline whose programs were not carved.

Check with `python3 -B tests/startup_archive_requests_check.py` (ctest `startup_archive_requests`).
On a synthetic archive built from documented instruction words, each adjacent pixel/vertex pair
becomes one request per root, and the test computes the expected keys with its own copy of the
runtime's hash. Controls that write nothing:

- a pixel program followed by a pixel program, or a vertex program without a pixel program before it;
- a vertex program one empty step after its pixel program, or after a pixel program without `s_endpgm`;
- a program without the header tag, or a pair at offsets that are not a multiple of 4;
- a pair that reaches the end of its stream, or a pair in a stream off the 64-byte grid;
- an empty or duplicate root file, an oversized stream, or a run without root signatures.

A pair split between two consecutive zlib streams is carved whole. A blocked `.root` write leaves no
`.a` and no temporary file and fails the run, as does an archive without pairs. A second run writes
nothing new.

`run.ps1` prints the first five lines of `<log>.out` and exits with the debugger's exit code: 2 when
the process could not be created, 0 when the debugger finished its report, whatever the process
did. Check with `python3 -B tests/startup_run_ps1_check.py` (ctest `startup_run_ps1`): on Windows it
Expand Down
206 changes: 206 additions & 0 deletions tests/startup_archive_requests_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
"""archive_requests.py on a synthetic archive (pure Python, no Windows, no GPU, no title files).

Programs follow the measured layout: a 36-byte header, s_inst_prefetch, code, s_code_end padding to
64 bytes, each on a 256-byte step of a zlib stream that starts on a 64-byte boundary. Each pixel
program directly followed by a vertex program must become one request per root, keyed exactly as
the runtime keys it; the expected keys are computed here with an independent FNV-1a, not with the
translator's function.

Controls that must write nothing: a pixel program followed by another pixel program, a vertex
program with no pixel program before it, a vertex program one empty 256-byte step after a pixel
program, a pixel program without s_endpgm, a program without the header tag, a pair at an offset
that is not a multiple of 4, a program that runs to the end of its stream, a valid pair in a stream
that is not on a 64-byte boundary, an empty or duplicate root file, a stream that inflates past the
limit, and a run with no root signature. A pair split between two consecutive zlib streams must be
carved whole. A request whose .root cannot be written must leave no .a and no temporary file and make
the run fail. An archive without any pair must make the run fail. 65 MiB of consecutive streams
must be split into two joined runs. A second run must write nothing new.
"""
import re
import struct
import subprocess
import sys
import tempfile
import zlib
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
TOOL = ROOT / 'tools' / 'startup' / 'archive_requests.py'

REVISION = re.search(rb'constexpr char revision\[\] = "([^"]+)";',
(ROOT / 'src' / 'startup_stubs' / 'd3d12_draw.inl').read_bytes()).group(1)
PREFETCH = bytes.fromhex('0300a0bf') # s_inst_prefetch 0x3
CODE_END = bytes.fromhex('00009fbf')
ENDPGM = struct.pack('<I', 0xbf810000)
failures = []


def expect(label, got, want):
if got != want:
failures.append('%s: got %r, want %r' % (label, got, want))


def runtime_key(a, b, root):
"""The runtime's key (fnv_bytes and cached_translation in src/startup_stubs/d3d12_draw.inl): FNV-1a 64
over the revision text, then the first recorded blob, the second and the root signature. The
runtime's start value is 1469598103934665603, not the standard offset basis 14695981039346656037;
the cache keys depend on it, so it is reproduced as written. The revision text is read from the
runtime source, so a revision bump there is followed here and checked against the tool."""
h = 1469598103934665603
for part in (REVISION, a, b, root):
for byte in part:
h = ((h ^ byte) * 1099511628211) & 0xffffffffffffffff
return h


def program(stage, serial, blocks=1, tag=b'\x80\xd5', extra=b''):
target = {'vs': 12, 'ps': 0}[stage] # exp pos0 / exp mrt0, then s_endpgm
header = b'\x00\x00' + tag + struct.pack('<I', serial) + bytes(28)
body = header + PREFETCH + extra + struct.pack('<2I', 0xf800080f | target << 4, 0) + ENDPGM
size = 0x40 * blocks
return body + CODE_END * ((size - len(body)) // 4)


def stream(layout, length=0x1000):
"""layout: [(offset, program)], offsets on 256-byte steps from 0x2c as in the measured streams."""
data = bytearray(length)
for at, blob in layout:
data[at:at + len(blob)] = blob
return bytes(data)


def summary(text):
return dict(item.split('=', 1) for item in text.split() if '=' in item)


PS1, VS1 = program('ps', 1), program('vs', 2, blocks=2)
PS2, PS3, VS3, VS4 = program('ps', 3), program('ps', 4), program('vs', 5), program('vs', 6)
PS_TAIL, VS_TAIL = program('ps', 7), program('vs', 12, blocks=2) # VS_TAIL ends where the stream ends
# VS5 holds v_cndmask_b32_e64 v1, -1, v0, vcc_lo with NEG on the inline -1 (VOP3 opcode 0x101,
# src0 193, src1 v0, src2 vcc_lo, neg bit 0): a form the translator refuses while decoding. Pairing
# must depend on the export targets only, not on whether the program can be translated.
CNDMASK_NEG_IMM = struct.pack('<2I', 0xd5010001, 193 | 256 << 9 | 106 << 18 | 1 << 29)
PS5, VS5 = program('ps', 8), program('vs', 9, extra=CNDMASK_NEG_IMM)
PS6, VS6 = program('ps', 10), program('vs', 11)

first = stream([(0x02c, PS1), (0x12c, VS1), (0x22c, PS2), (0x32c, PS3), (0x42c, VS3), (0x52c, VS4),
(0x62c, PS_TAIL), (0x72c, VS_TAIL)], length=0x72c + len(VS_TAIL))
second = stream([(0x02c, PS5), (0x12c, VS5)])
misaligned = stream([(0x02c, PS6), (0x12c, VS6)])

# A pair split between two zlib streams, the second starting on the 64-byte boundary right after the
# first, must be carved whole.
PS8, VS8 = program('ps', 13), program('vs', 14, blocks=2)
split = stream([(0x02c, PS8), (0x12c, VS8)], length=0x300)

# Must not pair: a vertex program one empty 256-byte step after its pixel program, and a vertex
# program after a pixel program that has no s_endpgm (cut short by an all-zero block).
PS9, VS9 = program('ps', 15), program('vs', 16)
cut = program('ps', 17).replace(ENDPGM, CODE_END)
VS10 = program('vs', 18)
gaps = stream([(0x02c, PS9), (0x22c, VS9), (0x32c, cut), (0x42c, VS10)], length=0x600)

# Must not pair: a pixel program without the header tag, and a pair at offsets that are not a
# multiple of 4 (whose bytes would otherwise form a complete, adjacent pair).
untagged, VS11 = program('ps', 19, tag=b'\x00\x00'), program('vs', 20)
PS12, VS12 = program('ps', 21), program('vs', 22)
decoys = stream([(0x02c, untagged), (0x12c, VS11), (0x32e, PS12), (0x42e, VS12)], length=0x600)

# A stream that inflates to 2 MiB: refused as oversized, never inflated in full.
oversized = zlib.compress(bytes(0x200000), 9)


def aligned(archive, alignment=0):
while len(archive) % 64 != alignment:
archive.append(0)


archive = bytearray()
for blob, offset in ((first, 0), (misaligned, 32), (second, 0)):
aligned(archive, offset)
archive += zlib.compress(blob, 9)
for blob in (gaps, decoys):
aligned(archive)
archive += bytes(64) # a gap keeps each stream apart from the previous one
archive += zlib.compress(blob, 9)
aligned(archive)
archive += bytes(64) + oversized
aligned(archive)
archive += bytes(64)
for piece in (split[:0x150], split[0x150:]):
aligned(archive)
archive += zlib.compress(piece, 9)

roots = [b'DXBC-root-one', b'DXBC-root-two']
want = {runtime_key(a, b, r): (a, b, r) for a, b in ((PS1, VS1), (PS3, VS3), (PS5, VS5), (PS8, VS8)) for r in roots}

with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
(tmp / 'archive.bin').write_bytes(archive)
(tmp / 'nopairs.bin').write_bytes(zlib.compress(stream([(0x02c, PS2), (0x12c, PS3)]), 9))
rootdir = tmp / 'roots'
rootdir.mkdir()
for i, r in enumerate(roots + roots[:1] + [b'']): # a duplicate and an empty root must be ignored
(rootdir / ('req_%016x.root' % i)).write_bytes(r)
cache = tmp / 'cache'

def run(cache_dir, *extra, archive_name='archive.bin'):
return subprocess.run([sys.executable, '-B', str(TOOL), str(tmp / archive_name), str(cache_dir), *extra],
capture_output=True, text=True, timeout=120)

missing = run(cache, '--roots', str(tmp / 'absent'))
expect('missing roots path: refused', missing.returncode != 0 and 'not found' in missing.stderr, True)
cache.mkdir() # an existing cache without any req_*.root
empty = run(cache)
expect('no roots: refused with a message', empty.returncode != 0 and 'no root signature' in empty.stderr, True)
expect('no roots: requests written', sorted(cache.glob('req_*')), [])

out = run(cache, '--roots', str(rootdir))
expect('run exit status', out.returncode, 0)
got = {int(p.name[4:20], 16) for p in cache.glob('req_*.a')}
expect('request keys', sorted(got), sorted(want))
for key, (a, b, r) in want.items():
name = cache / ('req_%016x' % key)
parts = tuple(name.with_suffix(ext).read_bytes() if name.with_suffix(ext).exists() else None
for ext in ('.a', '.b', '.root'))
expect('request %016x parts' % key, parts, (a, b, r))
expect('no temporary files left', sorted(p.name for p in cache.glob('*.tmp')), [])
counts = summary(out.stdout)
expect('summary', {k: counts.get(k) for k in ('streams', 'oversized', 'incomplete', 'pairs', 'written', 'roots')},
{'streams': '5', 'oversized': '1', 'incomplete': '1', 'pairs': '4', 'written': '8', 'roots': '2'})

again = run(cache, '--roots', str(rootdir))
expect('second run', {k: summary(again.stdout).get(k) for k in ('written', 'existing')}, {'written': '0', 'existing': '8'})

# A .root that cannot be written (a directory stands in the way): no .a, no temporary file, exit 1.
blocked = tmp / 'blocked'
blocked.mkdir()
stuck = runtime_key(PS1, VS1, roots[0])
(blocked / ('req_%016x.root' % stuck)).mkdir()
jam = run(blocked, '--roots', str(rootdir))
expect('blocked write: run fails', jam.returncode != 0 and 'could not be written' in jam.stderr, True)
expect('blocked write: no ready marker', (blocked / ('req_%016x.a' % stuck)).exists(), False)
expect('blocked write: no temporary files', sorted(p.name for p in blocked.glob('*.tmp')), [])
expect('blocked write: others written', summary(jam.stdout).get('written'), '7')

none = run(tmp / 'none', '--roots', str(rootdir), archive_name='nopairs.bin')
expect('no pairs: run fails', none.returncode != 0 and 'no program pair' in none.stderr, True)

# 130 consecutive 512 KiB streams (65 MiB inflated, a few KB on disk) would join into one run;
# the 64 MiB cap must split them into two.
piece = zlib.compress(bytes(0x80000), 9)
joined = bytearray()
for _ in range(130):
aligned(joined)
joined += piece
(tmp / 'joined.bin').write_bytes(joined)
capped = run(tmp / 'capped', '--roots', str(rootdir), archive_name='joined.bin')
expect('joined run capped at 64 MiB', summary(capped.stdout).get('streams'), '2')
if failures:
print(out.stdout, out.stderr, again.stdout, jam.stdout, jam.stderr, sep='\n')

for item in failures:
print('FAIL', item)
if failures:
sys.exit(1)
print('startup_archive_requests_check passed')
Loading
Loading