Skip to content
Merged
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
46 changes: 37 additions & 9 deletions tests/startup_compute_translate_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

Request checks: cache files are written here with gfx2hlsl.compute_key. Case A of
startup_gfx2hlsl_compute_check.py, with a 28-byte state block A carrying its group size and COMPUTE_PGM_RSRC2,
translates (manifest only, X2W_FXC empty) to a `stage cs` manifest. Each malformed request is refused into
<key>.fail with no manifest: code of 0 bytes or not whole dwords, code without s_endpgm, a part whose length
disagrees with the size words in .desc, files that do not hash to the key in their name, a state block A that
is not 28 bytes, and a missing part (the .fail text names no path).
translates (manifest only, X2W_FXC empty) to a `stage cs` manifest. The same case keyed with the revision-28
hash replays through the same path, while a revision-28 request altered after naming is refused with the
key-mismatch reason. Each malformed request is refused into <key>.fail with no manifest: code of 0 bytes or not
whole dwords, code without s_endpgm, a part whose length disagrees with the size words in .desc, files that do
not hash to the key in their name, a state block A that is not 28 bytes, and a missing part (the .fail text
names no path).

e2e: the stand-in publishes case A, gfx2hlsl.py --pending translates it with DXC, and offscreen_check dispatches
the published <key>.cs.cso and compares all 320 words with that test's expectation. This proves the plumbing
Expand All @@ -27,17 +29,20 @@
from startup_compute_request_check import DATA, STATE_B, prepare, run
from startup_stream_check import gfx2hlsl as G

REVISION_28 = b'x2w-startup-shader-28cs'


def state_a(group, rsrc2):
# Dword 3 holds X and Y, dword 4 holds Z and the wave32 bit, and dword 6 holds COMPUTE_PGM_RSRC2.
return struct.pack('<7I', 0, 0, 0, group[0] | group[1] << 16, group[2] | (1 << 20), 0, rsrc2)


def write_request(cache, code, state, data=DATA, root=None, desc_sizes=None):
def write_request(cache, code, state, data=DATA, root=None, desc_sizes=None, revision=None):
root = C.root_blob() if root is None else root
sizes = desc_sizes or (len(state), len(STATE_B), len(code), len(data))
desc = struct.pack('<12Q', 0, 0, sizes[0], 0, sizes[1], 0, sizes[2], 0, sizes[3], 0, 0, 11)
key = G.compute_key(code, data, root, desc, state, STATE_B)
extra = {} if revision is None else {'revision': revision}
key = G.compute_key(code, data, root, desc, state, STATE_B, **extra)
for ext, part in (('.desc', desc), ('.sa', state), ('.sb', STATE_B), ('.data', data), ('.root', root), ('.c67', code)):
(cache / ('req_%016x%s' % (key, ext))).write_bytes(part)
return key
Expand All @@ -55,6 +60,29 @@ def check_requests(directory):
assert manifest.startswith('x2w-manifest 2\nstage cs\ngroup 2 3 4\n'), manifest
assert '[numthreads(2, 3, 4)]' in (cache / ('%016x.cs.hlsl' % key)).read_text()

# Revision-28 archives remain replayable, as src/shader_compute_request.cpp read_compute_request accepts the
# revision-28 hash after the current one. Case A keyed with that revision translates the same way.
key28 = write_request(cache, words, good, revision=REVISION_28)
assert key28 != key
legacy = G.fnv(REVISION_28)
for ext in ('.c67', '.data', '.root', '.desc', '.sa', '.sb'):
part = (cache / ('req_%016x%s' % (key28, ext))).read_bytes()
legacy = G.fnv(part, G.fnv(struct.pack('<Q', len(part)), legacy))
assert key28 == legacy, 'revision-28 key is not the runtime length-framed chain'
assert G.translate_request(str(cache), key28) == 'ok'
manifest28 = (cache / ('%016x.txt' % key28)).read_text()
assert manifest28.startswith('x2w-manifest 2\nstage cs\ngroup 2 3 4\n'), manifest28
assert '[numthreads(2, 3, 4)]' in (cache / ('%016x.cs.hlsl' % key28)).read_text()
# A revision-28 request whose parts no longer hash to its name stays refused with the same reason.
altered = DATA[:-1] + bytes([DATA[-1] ^ 0xff])
bad28 = write_request(cache, words, good, data=altered, revision=REVISION_28)
assert bad28 not in (key, key28)
(cache / ('req_%016x.data' % bad28)).write_bytes(DATA)
result = G.translate_request(str(cache), bad28)
assert result == 'unsupported: compute request files do not match the key in their name', result
assert (cache / ('%016x.fail' % bad28)).read_text() == 'compute request files do not match the key in their name\n'
assert not (cache / ('%016x.txt' % bad28)).exists()

end = words.rindex(struct.pack('<I', G.S_ENDPGM))
cases = {
'code of 0 bytes': (lambda: write_request(cache, b'', good), 'compute code of 0 bytes is not whole dwords'),
Expand Down Expand Up @@ -113,9 +141,9 @@ def check_requests(directory):
assert key in G.pending_keys(str(cache), skip_failed=False), 'an empty .cs.cso made the request done'
(cache / ('%016x.cs.cso' % key)).write_bytes(b'DXBC')
assert key not in G.pending_keys(str(cache), skip_failed=False), 'manifest and .cs.cso did not make it done'
print('compute request translation: case A manifest, %d malformed requests refused with no manifest, '
'renamed and incomplete requests refused, done test, failed compile withdraws the manifest, '
'compiled done test' % len(cases))
print('compute request translation: case A manifest, revision-28 replay and altered revision-28 refusal, '
'%d malformed requests refused with no manifest, renamed and incomplete requests refused, done test, '
'failed compile withdraws the manifest, compiled done test' % len(cases))


def check_e2e(directory, consumer, dxc, runner, adapter, wrong_group):
Expand Down
19 changes: 15 additions & 4 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -13160,12 +13160,20 @@ def pipeline_key(vs_code, ps_code, root_blob, layout=b'', ps_inputs=None, slot57
return fnv(slot57_entry, key) if slot57_entry else key


def compute_key(cs_code, data, root_blob, desc, state_a=b'', state_b=b''):
COMPUTE_REVISION = b'x2w-startup-shader-29cs'
# A string, not a bytes literal: shader_cache_revision.py reads the translator's current compute revision from
# its b'x2w-startup-shader-<n>cs' literal and must keep seeing COMPUTE_REVISION alone. The runtime still accepts
# revision-28 archives (src/shader_compute_request.cpp read_compute_request).
COMPUTE_REVISION_28 = 'x2w-startup-shader-28cs'.encode('ascii')


def compute_key(cs_code, data, root_blob, desc, state_a=b'', state_b=b'', *, revision=COMPUTE_REVISION):
"""The runtime key of a device slot 67 compute request (req_<key>.c67, .data, .root, .desc, .sa, .sb): the
graphics revision followed by "cs", then each part preceded by its length as a little-endian u64: the code,
the words 7-8 blob, the root signature, the 12 description words with their pointers zeroed and the two
state blocks."""
key = fnv(b'x2w-startup-shader-29cs')
state blocks. revision names the hash revision: the current one by default, revision 28 when replaying an
older archive."""
key = fnv(revision)
for part in (cs_code, data, root_blob, desc, state_a, state_b):
key = fnv(part, fnv(struct.pack('<Q', len(part)), key))
return key
Expand Down Expand Up @@ -13198,7 +13206,10 @@ def load_compute_request(out_dir, key, refusals=None, *, pipeline_type=ComputePi
for ext, word in (('.c67', 6), ('.data', 8), ('.sa', 2), ('.sb', 4)):
if len(parts[ext]) != words[word]:
raise Unsupported('compute request %s is %d bytes, the description says %d' % (ext, len(parts[ext]), words[word]))
if compute_key(parts['.c67'], parts['.data'], parts['.root'], desc, parts['.sa'], parts['.sb']) != key:
# Match new captures and keep revision-28 archives replayable, as read_compute_request does.
if all(compute_key(parts['.c67'], parts['.data'], parts['.root'], desc, parts['.sa'], parts['.sb'],
revision=revision) != key
for revision in (COMPUTE_REVISION, COMPUTE_REVISION_28)):
raise Unsupported('compute request files do not match the key in their name')
code = parts['.c67']
if not code or len(code) % 4:
Expand Down
Loading