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
11 changes: 11 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -17698,6 +17698,17 @@ A literal known-answer vector independently executed with the runtime C++ `fnv_b
and revision/order (including optional `.ia`) (`src/startup_stubs/d3d12_draw.inl`) prevents a shared Python-key error
from silently agreeing in both producer and validator.

A slot 57 request can carry a pixel entry record, `req_<key>.ps57in`: `PSI1` followed by 8 bytes of
fixed graphics state. A native-shader runtime build with `X2W_NATIVE_SHADERS` set writes it. Its 12
bytes are hashed as stored after the optional `.ia`, as the runtime and `src/shader_compute_request.cpp`
already do. The two known answers for it come from that reader's `graphics_request_key`, built on its
own. The check accepts a request keyed with its record. It rejects a key without the record, a missing
or changed record, and an RTV variant whose base key lacks the record. A short, long, mistagged or
empty record is refused by name even when the key hashes exactly those bytes, and so is a request with
both `.psin` and `.ps57in`. Two different records on the same stages translate to identical HLSL:
gfx2hlsl does not read the record's values. Before this change, gfx2hlsl refused every keyed slot 57
request with `shader request source key mismatch`.

```powershell
python -B tests/startup_shader_cache_key_check.py
ctest --preset dev --no-tests=error -j 1 -R '^startup_shader_cache_key$' -V
Expand Down
4 changes: 3 additions & 1 deletion docs/experiments/native-startup-request-cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ ctest --preset release --no-tests=error -R '^native_compute_pack(_hardware)?$'
- Graphics requests validate `x2w-startup-shader-29` or the archived revision 28 over `.a`, `.b`, `.root`, and optional `.ia`. When
`req_<key>.psin` is present, the reader parses `psin1 <key> ena=0x<mask> addr=0x<mask>`, checks that the
embedded key matches `<key>` and `(ena & ~addr) == 0`, and folds `b"psin1" + struct.pack("<II", ena, addr)`
into the FNV-1a key identically to `tools/startup/gfx2hlsl.py:pipeline_key`.
into the FNV-1a key identically to `tools/startup/gfx2hlsl.py:pipeline_key`. A slot 57 `req_<key>.ps57in`
(`PSI1` and 8 state bytes, 12 in all) is folded as stored, again as `pipeline_key` does. A request with
both records is refused.
- Compute requests validate `x2w-startup-shader-29cs` or the archived revision 28 over length-framed `.c67`, `.data`, `.root`, `.desc`,
`.sa`, and `.sb` identically to `d3d12_draw.inl:publish_compute_request` and `gfx2hlsl.py:compute_key`.
- In raw graphics translation (`src/gfx1013_compute_native.cpp`, `src/gfx1013_direct_graphics.inl`), pixel
Expand Down
2 changes: 2 additions & 0 deletions tests/checks/python-linux/startup_shader_cache_key.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Startup request identity: gfx2hlsl.pipeline_key, including .ia, .psin and slot 57 .ps57in records.
tests/startup_shader_cache_key_check.py
46 changes: 45 additions & 1 deletion tests/startup_shader_cache_key_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
BASE_MISMATCH = "shader request source key mismatch"


def check_request(blobs, key, diagnostic=None, rtv_request=None, layout=None):
def check_request(blobs, key, diagnostic=None, rtv_request=None, layout=None, records=None):
with tempfile.TemporaryDirectory() as directory:
cache = Path(directory)
base = int(rtv_request.split()[1]) if rtv_request else key
for suffix, data in zip((".a", ".b", ".root"), blobs):
(cache / f"req_{base:016x}{suffix}").write_bytes(data)
if layout is not None:
(cache / f"req_{base:016x}.ia").write_bytes(layout)
for suffix, data in (records or {}).items(): # pixel input records (.psin, .ps57in) of the base request
(cache / f"req_{base:016x}{suffix}").write_bytes(data)
if rtv_request:
(cache / f"req_{key:016x}.rtv").write_text(rtv_request, encoding="ascii")
# Explicit upstream manifest-only mode; real hashing, parsing and HLSL translation.
Expand All @@ -47,6 +49,47 @@ def check_request(blobs, key, diagnostic=None, rtv_request=None, layout=None):
return outputs


def check_slot57_entry(vector, blobs, canonical):
"""A slot 57 request's pixel entry record (req_<key>.ps57in) is part of its identity, as stored."""
# Known answers from x2w::graphics_request_key (src/shader_compute_request.cpp, the native request reader),
# built and run separately: the record's 12 bytes follow the optional .ia bytes with no tag or length, as
# the runtime's cached_translation hashes them.
entry = b"PSI1" + struct.pack("<II", 2, 1)
assert gfx2hlsl.pipeline_key(*vector, slot57_entry=entry) == 0x9ffda271506f7dc6
assert gfx2hlsl.pipeline_key(*vector, b"ia\x00\xfe", slot57_entry=entry) == 0x06315afc32bae986
entry_key = gfx2hlsl.pipeline_key(*blobs, slot57_entry=entry)
assert entry_key == gfx2hlsl.fnv(entry, canonical) != canonical
accepted = check_request(blobs, entry_key, records={".ps57in": entry})
print("slot 57 entry: request keyed with its .ps57in accepted and published")
check_request(blobs, canonical, BASE_MISMATCH, records={".ps57in": entry})
check_request(blobs, entry_key, BASE_MISMATCH)
other = b"PSI1" + struct.pack("<II", 3, 1)
check_request(blobs, entry_key, BASE_MISMATCH, records={".ps57in": other})
print("slot 57 entry: key without the record, missing record and changed record rejected before translation")
# Each malformed record is refused even though the key hashes exactly those bytes.
for bad in (entry[:11], entry + b"\x00", b"PSI2" + entry[4:], b""):
check_request(blobs, gfx2hlsl.fnv(bad, canonical), "invalid slot-57 pixel entry record",
records={".ps57in": bad})
print("slot 57 entry: short, long, mistagged and empty records refused by name")
masks = (2, 2)
both = gfx2hlsl.pipeline_key(*blobs, ps_inputs=masks, slot57_entry=entry)
description = b"psin1 %016x ena=0x2 addr=0x2\n" % both
check_request(blobs, both, "conflicting stream and slot-57 pixel entry records",
records={".psin": description, ".ps57in": entry})
print("slot 57 entry: a request with both .psin and .ps57in refused by name")
# The translation does not read the record's values: another record under its own key gives the same shaders.
changed = check_request(blobs, gfx2hlsl.pipeline_key(*blobs, slot57_entry=other), records={".ps57in": other})
for stage in (".vs.hlsl", ".ps.hlsl"):
assert accepted[f"{entry_key:016x}{stage}"] == changed[f"{gfx2hlsl.fnv(other, canonical):016x}{stage}"], stage
print("slot 57 entry: two records with the same stages translate to the same HLSL")
for correct in (True, False):
base = entry_key if correct else canonical
request = f"rtv1 {base} 28"
check_request(blobs, gfx2hlsl.fnv(request.encode("ascii")),
None if correct else "RTV variant source key mismatch", request, records={".ps57in": entry})
print("slot 57 entry: RTV variant of a keyed base accepted; of a base keyed without the record rejected")


def main():
# Independently executed C++ runtime fnv_bytes/cached_translation known-answer vector.
# Revision x2w-startup-shader-29 excludes NUL. Stored order is a, b, root, then optional input layout.
Expand Down Expand Up @@ -85,6 +128,7 @@ def main():
key = gfx2hlsl.fnv(request.encode("ascii"))
check_request(blobs, key, None if correct else "RTV variant source key mismatch", request)
print("RTV requests: correct accepted; existing mismatch diagnostic preserved")
check_slot57_entry(vector, blobs, canonical)
print("startup shader cache key checks: passed")
return 0

Expand Down
35 changes: 30 additions & 5 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12956,16 +12956,21 @@ def fnv(data, h=1469598103934665603):
return h


def pipeline_key(vs_code, ps_code, root_blob, layout=b'', ps_inputs=None):
def pipeline_key(vs_code, ps_code, root_blob, layout=b'', ps_inputs=None, slot57_entry=b''):
"""Hash the startup translation revision and the three blobs in runtime order, then a stream's input layout.

A pipeline with a stream description (req_<key>.psin) also hashes its pixel input masks: b'psin1' and then
SPI_PS_INPUT_ENA and SPI_PS_INPUT_ADDR as little-endian u32. Two pipelines with the same stages, root
signature and input layout but different masks are then two requests with their own results. Without a
description the key is unchanged."""
description the key is unchanged.

A slot 57 pipeline whose request has a pixel entry record (req_<key>.ps57in, see read_slot57_entry) hashes
its 12 bytes as stored, last, as the runtime's cached_translation and shader_compute_request.cpp's
graphics_request_key do. This translator does not read the record's values."""
key = fnv(root_blob, fnv(ps_code, fnv(vs_code, fnv(b'x2w-startup-shader-29'))))
key = fnv(layout, key) if layout else key
return key if ps_inputs is None else fnv(b'psin1' + struct.pack('<II', *ps_inputs), key)
key = key if ps_inputs is None else fnv(b'psin1' + struct.pack('<II', *ps_inputs), key)
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''):
Expand Down Expand Up @@ -13261,9 +13266,12 @@ def classify_stage(blob):
# A stream with an input layout publishes it as .ia (before .a) and hashes it into the key.
layout = open(name + '.ia', 'rb').read() if os.path.isfile(name + '.ia') else b''
if request or validate_base_key:
# The masks of a stream description are part of the key (pipeline_key).
# The masks of a stream description and a slot 57 pixel entry record are part of the key (pipeline_key).
masks = read_ps_inputs(description, base)
if pipeline_key(a, b, root, layout, masks) != base:
entry = read_slot57_entry(name + '.ps57in')
if masks is not None and entry:
raise Unsupported('conflicting stream and slot-57 pixel entry records')
if pipeline_key(a, b, root, layout, masks, entry) != base:
raise Unsupported('RTV variant source key mismatch' if request else
'pixel input description names another pipeline' if masks is not None else
'shader request source key mismatch')
Expand Down Expand Up @@ -13383,6 +13391,23 @@ def read_ps_inputs(path, key):
return int(masks['ena'], 16), int(masks['addr'], 16)


def read_slot57_entry(path):
"""The pixel entry record at `path` (req_<key>.ps57in) as stored, or b'' without the file.

A native-shader runtime build with X2W_NATIVE_SHADERS set publishes it before .a for a slot 57 pipeline whose
stages are not DXBC containers: b'PSI1' and the
8 bytes at offset 0x58 of the fixed graphics state, which d3d12_draw.inl reads as SPI_PS_INPUT_ADDR and
SPI_PS_IN_CONTROL. Any other size or tag is refused by name; shader_compute_request.cpp's reader refuses
the same records."""
if not os.path.isfile(path):
return b''
with open(path, 'rb') as file:
entry = file.read(13)
if len(entry) != 12 or not entry.startswith(b'PSI1'):
raise Unsupported('invalid slot-57 pixel entry record')
return entry


def mesh57_key(ms, ps, root):
value = fnv(b'x2w-startup-mesh-57-3')
for part in (ms, ps, root):
Expand Down
Loading