From ec57297d2bfda283736040cb3f4d61565abdc7a4 Mon Sep 17 00:00:00 2001 From: Ammar Abdelfattah <241957358+Ammmae0x@users.noreply.github.com> Date: Sun, 29 Nov 2026 01:09:58 +0200 Subject: [PATCH 1/2] startup: translate .ps57in requests and reuse cached plain-key translations With X2W_NATIVE_SHADERS set, cached_translation appended the PSI1 slot 57 entry to every ordinary graphics key, so a translation already cached under the plain key never hit and the Python fallback refused the suffixed request because it never read .ps57in. Read the 12-byte slot 57 pixel entry in the fallback, validate it against the suffixed key, and apply its address as both pixel input masks; carry the entry unchanged through cache rekeying; reuse an already published plain-key translation in cached_translation; and restore X2W_NATIVE_SHADERS when run.ps1 exits so later runs without -NativeShaders are not polluted. Fixes #3385 --- CMakeLists.txt | 4 + src/startup_stubs/d3d12_draw.inl | 16 +- .../python-linux/startup_gfx2hlsl_ps57in.txt | 1 + tests/startup_gfx2hlsl_ps57in_check.py | 294 ++++++++++++++++++ tools/startup/gfx2hlsl.py | 75 ++++- tools/startup/run.ps1 | 8 +- tools/startup/shader_cache_rekey.py | 15 +- 7 files changed, 393 insertions(+), 20 deletions(-) create mode 100644 tests/checks/python-linux/startup_gfx2hlsl_ps57in.txt create mode 100644 tests/startup_gfx2hlsl_ps57in_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 252816910f..a8f0ea5ef3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5209,6 +5209,10 @@ if(EXISTS "${INTAKE_PYTHON}") # Startup shader translator: descriptor offsets inside wide scalar loads (no game data). add_test(NAME startup_translator COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_translator_check.py") + # Startup shader translator: slot 57 pixel entries (.ps57in) translate, and a cached plain-key + # translation is reused instead of the suffixed key (#3385). + add_test(NAME startup_gfx2hlsl_ps57in COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_ps57in_check.py") # Startup shader translator: the SMEM offset 'off' is the literal 0; m0 and other operands keep their handling (#1674). add_test(NAME startup_gfx2hlsl_smem_offset COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_smem_offset_check.py") diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 1933d5f78e..589dab691c 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -652,7 +652,19 @@ Translation* cached_translation(const void* a, std::size_t a_bytes, const void* u64 key = fnv_bytes(root_blob.data(), root_blob.size(), fnv_bytes(b, b_bytes, fnv_bytes(a, a_bytes, seed))); // A stream's input layout (stream_input_layout) feeds the vertex fetch, so it is part of the key. if (!layout.empty()) key = fnv_bytes(layout.data(), layout.size(), key); - if (!slot57_entry.empty()) key = fnv_bytes(slot57_entry.data(), slot57_entry.size(), key); + std::string pixel_entry = slot57_entry; + if (!pixel_entry.empty()) { + // #3385: with X2W_NATIVE_SHADERS set, the fixed pixel entry used to join every slot 57 key, so a + // translation already cached under the plain key never hit and the Python fallback refused the + // suffixed request (it never read .ps57in). Reuse the cached plain-key translation when one is + // already published: the pipeline already ran with it, and the native path can still try the plain + // request. Only a cold pipeline keeps the entry in its key. + wchar_t base_path[MAX_PATH]; + const bool base_cached = cache_path(base_path, L"", key, L".txt") && + GetFileAttributesW(base_path) != INVALID_FILE_ATTRIBUTES; + if (base_cached) pixel_entry.clear(); + else key = fnv_bytes(pixel_entry.data(), pixel_entry.size(), key); + } auto& entry = translation_by_key()[key]; if (!entry) { entry = new Translation; @@ -661,7 +673,7 @@ Translation* cached_translation(const void* a, std::size_t a_bytes, const void* if (publish_shader_request(key, L".b", b, b_bytes) && publish_shader_request(key, L".root", root_blob.data(), root_blob.size()) && (layout.empty() || publish_shader_request(key, L".ia", layout.data(), layout.size())) && - (slot57_entry.empty() || publish_shader_request(key, L".ps57in", slot57_entry.data(), slot57_entry.size()))) + (pixel_entry.empty() || publish_shader_request(key, L".ps57in", pixel_entry.data(), pixel_entry.size()))) publish_shader_request(key, L".a", a, a_bytes); load_translation(*entry); } diff --git a/tests/checks/python-linux/startup_gfx2hlsl_ps57in.txt b/tests/checks/python-linux/startup_gfx2hlsl_ps57in.txt new file mode 100644 index 0000000000..7965caeb28 --- /dev/null +++ b/tests/checks/python-linux/startup_gfx2hlsl_ps57in.txt @@ -0,0 +1 @@ +tests/startup_gfx2hlsl_ps57in_check.py diff --git a/tests/startup_gfx2hlsl_ps57in_check.py b/tests/startup_gfx2hlsl_ps57in_check.py new file mode 100644 index 0000000000..1674312199 --- /dev/null +++ b/tests/startup_gfx2hlsl_ps57in_check.py @@ -0,0 +1,294 @@ +"""Slot 57 pixel entries (.ps57in) in the startup shader translator (#3385). + +With -NativeShaders (X2W_NATIVE_SHADERS set), the runtime appends a PSI1 suffix plus 8 bytes to the +key of every slot 57 graphics request and writes a .ps57in sidecar (src/startup_stubs/d3d12_draw.inl +cached_translation). The Python fallback never read that sidecar, so every such request failed with +`shader request source key mismatch`, the native path then refused it with `fixed graphics entry requires +matching stage interfaces`, and translations already cached under the plain key never hit. + +The fix has three parts, all covered here with source-built fixtures (pure Python, no Windows, no GPU): +- tools/startup/gfx2hlsl.py reads .ps57in: the suffixed key is pipeline_key() over the entry bytes, the + entry's address is both pixel input masks (as the native fixed entry builds them), a request with both + .psin and .ps57in is refused, and no .psdep is written for an entry (its bytes are part of the key, so a + changed entry is a different request). tools/startup/shader_cache_rekey.py copies the entry unchanged. +- src/startup_stubs/d3d12_draw.inl reuses a translation already cached under the plain key: when + .txt is published, the entry leaves the key, so a warmed cache hits at once instead of waiting for + a retranslation under the suffixed key. Only a cold pipeline keeps the entry in its key. The runtime + behaviour is Windows-only, so the guard is pinned by source text here, with mutants that must fail it. +- tools/startup/run.ps1 restores X2W_NATIVE_SHADERS when the run ends (to what the session had before, or + removes it), so the next run without -NativeShaders is not still affected. The restore is pinned by + source text here, with mutants that must fail it; the live path is still covered by + tests/startup_run_ps1_check.py, which must keep passing. + +A slot 57 mesh request (.m57, translate_mesh57) is untouched by the fix and still translates: the +negative control for the mesh path. +""" +import os +import re +import struct +import sys +import tempfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +sys.path.insert(0, str(ROOT / 'tools' / 'startup')) +sys.path.insert(0, str(ROOT / 'tests')) +os.environ['X2W_FXC'] = '' # manifest only: no FXC/DXC on this host +import gfx2hlsl as G # noqa: E402 +import shader_cache_rekey as R # noqa: E402 +from startup_flat_param_check import VS_WORDS, PS_WORDS, root_blob # noqa: E402 +from startup_mesh57_skeleton_check import MESH, PIXEL, code as mesh_code, root as mesh_root # noqa: E402 + +VS, PS, ROOT_BLOB = VS_WORDS, PS_WORDS, root_blob() +ENTRY = b'PSI1' + struct.pack('..hlsl, or None when the translation never published it.""" + path = Path(directory) / ('%016x.%s.hlsl' % (key, stage)) + return path.read_bytes() if path.is_file() else None + + +# The suffixed key is the plain key hashed with the entry bytes, in runtime order, and nothing else. +key_parts = G.fnv(b'x2w-startup-shader-29') +for part in (VS, PS, ROOT_BLOB, ENTRY): + key_parts = G.fnv(part, key_parts) +expect('suffixed key hashes the entry after the plain key', KEY, key_parts) +expect('suffixed key differs from the plain key', KEY != BASE_KEY, True) +expect('plain key is unchanged by the fix', BASE_KEY, G.pipeline_key(VS, PS, ROOT_BLOB)) +try: + G.pipeline_key(VS, PS, ROOT_BLOB, ps_inputs=(0x8002, 0x8002), ps57_entry=ENTRY) + failures.append('pipeline_key accepted both a .psin mask pair and a .ps57in entry') +except ValueError as error: + expect('pipeline_key refuses both pixel entries', str(error), + 'a pipeline has one pixel entry: .psin masks or .ps57in, not both') + +with tempfile.TemporaryDirectory() as tmp: + d = Path(tmp) + # A suffixed request translates, applies the entry's masks, and is then complete: the watcher must + # not re-queue it. No .psdep is written for it: there is no .psin to lose. + put_request(d, KEY, entry=ENTRY) + expect('suffixed request translates', translate(d, KEY), 'ok') + expect('suffixed request publishes a manifest', (d / ('%016x.txt' % KEY)).is_file(), True) + expect('suffixed request writes no .fail', fail_text(d, KEY), None) + expect('suffixed request writes no .psdep', (d / ('%016x.psdep' % KEY)).is_file(), False) + expect('suffixed request masks are the entry address twice', + G.request_ps_inputs(str(d), KEY), (0x8002, 0x8002)) + expect('suffixed translation applies the entry masks', + PACKED_LINE in (hlsl_bytes(d, KEY, 'ps') or b'').decode('utf-8'), True) + expect('translated suffixed request is complete', G.pending_keys(str(d), False), []) + + # The plain request still translates, and without the entry its pixel stage has no packed position: + # the masks above really come from the .ps57in, and the two keys are two translations. + put_request(d, BASE_KEY) + expect('plain request still translates', translate(d, BASE_KEY), 'ok') + expect('plain translation has no packed position', + PACKED_LINE in (hlsl_bytes(d, BASE_KEY, 'ps') or b'').decode('utf-8'), False) + expect('plain and suffixed pixel stages differ', + hlsl_bytes(d, BASE_KEY, 'ps') != hlsl_bytes(d, KEY, 'ps'), True) + + # An RTV variant of the suffixed request translates from the same blobs and entry. + variant_text = 'rtv1 %d 28' % KEY + variant_key = G.fnv(variant_text.encode('ascii')) + (d / ('req_%016x.rtv' % variant_key)).write_text(variant_text, encoding='ascii') + expect('RTV variant of a suffixed request translates', translate(d, variant_key), 'ok') + expect('RTV variant publishes a manifest', (d / ('%016x.txt' % variant_key)).is_file(), True) + + # The rekey tool copies the entry unchanged under the current key, and the copy still translates. + src, dst = d / 'rekey_src', d / 'rekey_dst' + src.mkdir() + put_request(src, KEY, entry=ENTRY) + expect('rekey of a suffixed request succeeds', R.rekey(str(src), str(dst)), 0) + rekeyed_entry = dst / ('req_%016x.ps57in' % KEY) + expect('rekey copies the entry unchanged', + rekeyed_entry.read_bytes() if rekeyed_entry.is_file() else None, ENTRY) + expect('rekeyed suffixed request translates', translate(dst, KEY), 'ok') + +with tempfile.TemporaryDirectory() as tmp: + d = Path(tmp) + # Negative controls: each must fail by name, with no manifest. + other_entry = b'PSI1' + struct.pack('= 0 else None + + +def guard_problems(text): + """The plain-key reuse guard in cached_translation, and the mutants that must fail it.""" + body = cached_translation_body(text) + if body is None: + return ['cached_translation is missing from d3d12_draw.inl'] + found = [] + if 'std::string pixel_entry = slot57_entry;' not in body: + found.append('cached_translation does not keep the slot 57 entry in a local') + if not re.search(r'const bool base_cached = cache_path\(base_path, L"", key, L"\.txt"\) &&\s*' + r'GetFileAttributesW\(base_path\) != INVALID_FILE_ATTRIBUTES;', body): + found.append('cached_translation does not check for a published plain-key manifest') + if 'if (base_cached) pixel_entry.clear();' not in body: + found.append('cached_translation does not drop the entry when the plain key is cached') + if 'else key = fnv_bytes(pixel_entry.data(), pixel_entry.size(), key);' not in body: + found.append('cached_translation does not suffix the key only for a cold pipeline') + if 'publish_shader_request(key, L".ps57in", pixel_entry.data(), pixel_entry.size())' not in body: + found.append('cached_translation does not publish .ps57in under the suffixed key') + return found + + +DRAW = (ROOT / 'src' / 'startup_stubs' / 'd3d12_draw.inl').read_text(encoding='utf-8') +expect('plain-key reuse guard is present', guard_problems(DRAW), []) +for label, mutant in { + 'guard removed': DRAW.replace('if (base_cached) pixel_entry.clear();\n', ''), + 'guard inverted': DRAW.replace('GetFileAttributesW(base_path) != INVALID_FILE_ATTRIBUTES', + 'GetFileAttributesW(base_path) == INVALID_FILE_ATTRIBUTES'), + 'guard checks bytecode, not the manifest': DRAW.replace( + 'cache_path(base_path, L"", key, L".txt")', 'cache_path(base_path, L"", key, L".vs.cso")'), + 'entry never dropped': DRAW.replace('if (base_cached) pixel_entry.clear();', + 'if (base_cached) pixel_entry.size();'), +}.items(): + expect("mutant '%s' fails the guard" % label, guard_problems(mutant) != [], True) + + +def ps1_problems(script): + """run.ps1 saves X2W_NATIVE_SHADERS before setting it and restores it when the run ends.""" + found = [] + save = re.search(r'\$script:nativeShadersBefore\s*=\s*\$env:X2W_NATIVE_SHADERS\s*;\s*' + r'\$env:X2W_NATIVE_SHADERS\s*=\s*\(Resolve-Path \$NativeShaders\)\.Path', script) + if not save: + found.append('run.ps1 does not save X2W_NATIVE_SHADERS before setting it') + return found + if 'debug_launch.py' not in script[save.start():]: + found.append('run.ps1 sets X2W_NATIVE_SHADERS after the debugger call') + finally_at = script.find('} finally {', save.start()) + restore = re.search(r'if \(Test-Path variable:script:nativeShadersBefore\) \{\s*' + r'if \(\$null -eq \$script:nativeShadersBefore\) \{ ' + r'Remove-Item Env:X2W_NATIVE_SHADERS -ErrorAction SilentlyContinue \}\s*' + r'else \{ \$env:X2W_NATIVE_SHADERS = \$script:nativeShadersBefore \}\s*\}', script) + if not restore: + found.append('run.ps1 does not restore X2W_NATIVE_SHADERS in the run cleanup') + elif finally_at < 0 or not finally_at < restore.start() < script.find('exit $debuggerExit'): + found.append('run.ps1 restores X2W_NATIVE_SHADERS outside the debugger cleanup') + return found + + +RUN_PS1 = (ROOT / 'tools' / 'startup' / 'run.ps1').read_text(encoding='utf-8') +expect('run.ps1 restores X2W_NATIVE_SHADERS on exit', ps1_problems(RUN_PS1), []) +RESTORE = (' if (Test-Path variable:script:nativeShadersBefore) {\n' + ' if ($null -eq $script:nativeShadersBefore) { Remove-Item Env:X2W_NATIVE_SHADERS ' + '-ErrorAction SilentlyContinue }\n' + ' else { $env:X2W_NATIVE_SHADERS = $script:nativeShadersBefore }\n' + ' }\n') +for label, mutant in { + 'restore removed': RUN_PS1.replace(RESTORE, ''), + 'save removed': RUN_PS1.replace('$script:nativeShadersBefore = $env:X2W_NATIVE_SHADERS; ', ''), + 'unset case removed': RUN_PS1.replace( + 'if ($null -eq $script:nativeShadersBefore) { Remove-Item Env:X2W_NATIVE_SHADERS ' + '-ErrorAction SilentlyContinue }\n', ''), +}.items(): + expect("mutant '%s' fails the run.ps1 check" % label, ps1_problems(mutant) != [], True) + + +def main(): + for item in failures: + print('FAIL', item) + print('startup gfx2hlsl ps57in: %d failed' % len(failures)) + return 1 if failures else 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 5b2775b10d..a190b16770 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -12956,15 +12956,22 @@ 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, ps57_entry=None): """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_.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.""" + SPI_PS_INPUT_ENA and SPI_PS_INPUT_ADDR as little-endian u32. A slot 57 pipeline with a fixed pixel entry + (req_.ps57in) instead hashes the entry itself: b'PSI1' and then SPI_PS_INPUT_ADDR and + SPI_PS_IN_CONTROL as little-endian u32, exactly as the runtime does (src/startup_stubs/d3d12_draw.inl + cached_translation, src/shader_compute_request.cpp graphics_request_key). Two pipelines with the same + stages, root signature and input layout but different masks or entries are then two requests with their own + results. Without a description the key is unchanged. A request has one pixel entry or the other, never both.""" key = fnv(root_blob, fnv(ps_code, fnv(vs_code, fnv(b'x2w-startup-shader-29')))) key = fnv(layout, key) if layout else key + if ps_inputs is not None and ps57_entry is not None: + raise ValueError('a pipeline has one pixel entry: .psin masks or .ps57in, not both') + if ps57_entry is not None: + return fnv(ps57_entry, key) return key if ps_inputs is None else fnv(b'psin1' + struct.pack('.psin, the separately captured - description of exactly that pipeline (see read_ps_inputs), or for an RTV variant its source pipeline's. The - masks are part of the request's identity: the key must equal pipeline_key() of the stage blobs, root - signature, input layout and these masks, so a description copied from another pipeline, left stale after a - stage change or rewritten with another mask pair is refused, never applied. No file means no captured - description: the translation keeps its model without hardware inputs.""" + description of exactly that pipeline (see read_ps_inputs), or for an RTV variant its source pipeline's; or + from the slot 57 pixel entry req_.ps57in (see read_ps57_inputs), whose address is both masks, as the + native fixed entry builds it. The masks are part of the request's identity: the key must equal + pipeline_key() of the stage blobs, root signature, input layout and these masks or that entry, so a + description copied from another pipeline, left stale after a stage change or rewritten with another mask + pair or entry is refused, never applied. No file means no captured description: the translation keeps its + model without hardware inputs. A request has one pixel entry or the other, never both.""" _, base, _, description = request_source(out_dir, key) masks = read_ps_inputs(description, base) - if masks is None: + ps57 = read_ps57_inputs(os.path.join(out_dir, 'req_%016x.ps57in' % base), base) + if masks is not None and ps57 is not None: + raise Unsupported('conflicting stream and slot-57 pixel entry records') + if masks is None and ps57 is None: return None name = os.path.join(out_dir, 'req_%016x' % base) a, b, root = (open(name + ext, 'rb').read() for ext in ('.a', '.b', '.root')) layout = open(name + '.ia', 'rb').read() if os.path.isfile(name + '.ia') else b'' - if pipeline_key(a, b, root, layout, masks) != base: + if ps57 is not None: + entry, masks = ps57 + if pipeline_key(a, b, root, layout, ps57_entry=entry) != base: + raise Unsupported('slot-57 pixel entry names another pipeline') + elif pipeline_key(a, b, root, layout, masks) != base: raise Unsupported('pixel input description names another pipeline') return masks @@ -13383,6 +13408,25 @@ def read_ps_inputs(path, key): return int(masks['ena'], 16), int(masks['addr'], 16) +def read_ps57_inputs(path, key): + """The (entry, masks) of the slot 57 pixel entry at `path` (req_.ps57in), or None without the file. + + The entry is 12 bytes: b'PSI1' then SPI_PS_INPUT_ADDR and SPI_PS_IN_CONTROL as little-endian u32, as the + runtime writes it (src/startup_stubs/d3d12_draw.inl cached_translation). The masks are the address twice: + like the native fixed entry (src/direct_compute_cache.cpp prepare_direct_graphics), every allocated input + is loaded, so the enable mask is the address mask. The interpolation count in the second word is not a + mask and is not applied here. A short entry or another prefix is refused by name, as the native request + reader refuses it ('invalid slot-57 pixel entry record').""" + if not os.path.isfile(path): + return None + with open(path, 'rb') as file: + entry = file.read() + if len(entry) != 12 or entry[:4] != b'PSI1': + raise Unsupported('invalid slot-57 pixel entry record') + address, _interpolants = struct.unpack('.psin; pending_keys withdraws it when that + # file is gone. A slot 57 entry has no .psin to lose (its bytes are part of the key, so a changed + # entry is a different request), so no record is written for it. + if ps_inputs is not None and not os.path.isfile(os.path.join(out_dir, 'req_%016x.ps57in' % base)): record_dependency(out_dir, key, base, ps_inputs) # before the manifest is published except Unsupported: # A result published earlier was translated from sources or masks this request no longer confirms. diff --git a/tools/startup/run.ps1 b/tools/startup/run.ps1 index 36582acecb..dd8793d124 100644 --- a/tools/startup/run.ps1 +++ b/tools/startup/run.ps1 @@ -80,7 +80,7 @@ if ($Render) { New-Item -ItemType Directory -Force $ShaderCache | Out-Null $env:X2W_D3D12_RENDER = "1"; $env:X2W_DRAW_NULL_IDENTITY = "1" $env:X2W_SHADER_CACHE = (Resolve-Path $ShaderCache).Path - if ($NativeShaders) { $env:X2W_NATIVE_SHADERS = (Resolve-Path $NativeShaders).Path } + if ($NativeShaders) { $script:nativeShadersBefore = $env:X2W_NATIVE_SHADERS; $env:X2W_NATIVE_SHADERS = (Resolve-Path $NativeShaders).Path } } if ($NativeShaders -and -not $Render) { Write-Warning "-NativeShaders needs -Render (no shader requests without it); ignored" } New-Item -ItemType Directory -Force $LogDir | Out-Null @@ -130,6 +130,12 @@ try { & taskkill.exe /T /F /PID $translator.Id 2>&1 | Out-Null } } + # #3385: a run with -NativeShaders must not leak the SDK directory into the session, or the next run + # without the flag still translates with native shaders. Restore what was there before this run set it. + if (Test-Path variable:script:nativeShadersBefore) { + if ($null -eq $script:nativeShadersBefore) { Remove-Item Env:X2W_NATIVE_SHADERS -ErrorAction SilentlyContinue } + else { $env:X2W_NATIVE_SHADERS = $script:nativeShadersBefore } + } } if ($watchShaders -and -not $revisionWarned) { $null = & $checkRevision } Get-Content "$LogDir.out" -TotalCount 5 diff --git a/tools/startup/shader_cache_rekey.py b/tools/startup/shader_cache_rekey.py index a1baf93941..8930a380bd 100644 --- a/tools/startup/shader_cache_rekey.py +++ b/tools/startup/shader_cache_rekey.py @@ -14,6 +14,7 @@ that already exists in is left as it is. Each file is written to a temporary name and renamed; the ready marker (.a, .c67, .m57) goes last, as the runtime writes it. A stream description (.psin) is written again under the new key with the same masks, since its line names the key and its masks are part of the key. +A slot 57 pixel entry (.ps57in) holds no key and is copied unchanged, since its bytes are part of the key. Slot 86 mesh requests are only counted. All requests are read before anything is written. When a request part is missing or unreadable, or an RTV @@ -31,9 +32,10 @@ REQUEST = re.compile(r'req_([0-9a-f]{16})\.(a|rtv|c67|m57|m86)') # Request files of each kind; the ready marker is last. -PARTS = {'g': ('.b', '.root', '.ia', '.psin', '.a'), 'c': ('.desc', '.sa', '.sb', '.data', '.root', '.c67'), +PARTS = {'g': ('.b', '.root', '.ia', '.psin', '.ps57in', '.a'), 'c': ('.desc', '.sa', '.sb', '.data', '.root', '.c67'), 'm57': ('.ps57', '.root', '.m57')} -OPTIONAL = {'.ia', '.psin'} # a graphics request may have no input layout (.ia) and no stream description (.psin) +OPTIONAL = {'.ia', '.psin', '.ps57in'} # a graphics request may have no input layout (.ia) and no stream +# description (.psin) or slot 57 pixel entry (.ps57in); the entry holds no key, so it is copied unchanged class Incomplete(Exception): @@ -63,11 +65,18 @@ def current_key(job): except OSError as error: return kind, key, None, 'unreadable %s: %s' % (os.path.basename(error.filename or ''), error.strerror) if kind == 'g': - # The pixel input masks of a stream description are part of the key; the description names its key. + # The pixel input masks of a stream description, or the bytes of a slot 57 pixel entry, are part of + # the key; the description names its key. A request has one pixel entry or the other, never both. try: masks = g.read_ps_inputs(request_name(src, key, '.psin'), key) + ps57 = g.read_ps57_inputs(request_name(src, key, '.ps57in'), key) except g.Unsupported as reason: return kind, key, None, str(reason) + if masks is not None and ps57 is not None: + return kind, key, None, 'conflicting stream and slot-57 pixel entry records' + if ps57 is not None: + entry, _masks = ps57 + return kind, key, g.pipeline_key(p['.a'], p['.b'], p['.root'], p.get('.ia', b''), ps57_entry=entry), None return kind, key, g.pipeline_key(p['.a'], p['.b'], p['.root'], p.get('.ia', b''), masks), None if kind == 'm57': return kind, key, g.mesh57_key(p['.m57'], p['.ps57'], p['.root']), None From 24755d1f6ee0075d03934a079cc891608b63149e Mon Sep 17 00:00:00 2001 From: Ammar Abdelfattah <241957358+Ammmae0x@users.noreply.github.com> Date: Sun, 29 Nov 2026 00:44:32 +0000 Subject: [PATCH 2/2] startup: address review feedback on native-shaders .ps57in handling --- src/startup_stubs/d3d12_draw.inl | 6 +- tests/startup_gfx2hlsl_ps57in_check.py | 80 +++++++++++++++++++++++--- tools/startup/gfx2hlsl.py | 11 ++-- 3 files changed, 83 insertions(+), 14 deletions(-) diff --git a/src/startup_stubs/d3d12_draw.inl b/src/startup_stubs/d3d12_draw.inl index 589dab691c..d70ca42854 100644 --- a/src/startup_stubs/d3d12_draw.inl +++ b/src/startup_stubs/d3d12_draw.inl @@ -658,9 +658,11 @@ Translation* cached_translation(const void* a, std::size_t a_bytes, const void* // translation already cached under the plain key never hit and the Python fallback refused the // suffixed request (it never read .ps57in). Reuse the cached plain-key translation when one is // already published: the pipeline already ran with it, and the native path can still try the plain - // request. Only a cold pipeline keeps the entry in its key. + // request. Only a cold pipeline keeps the entry in its key. In native-shaders-only mode .txt + // manifests are ignored, so a leftover Python manifest must not strip the entry from the request + // the native path receives: the reuse is skipped there and the entry stays in the key. wchar_t base_path[MAX_PATH]; - const bool base_cached = cache_path(base_path, L"", key, L".txt") && + const bool base_cached = !native_shaders_only() && cache_path(base_path, L"", key, L".txt") && GetFileAttributesW(base_path) != INVALID_FILE_ATTRIBUTES; if (base_cached) pixel_entry.clear(); else key = fnv_bytes(pixel_entry.data(), pixel_entry.size(), key); diff --git a/tests/startup_gfx2hlsl_ps57in_check.py b/tests/startup_gfx2hlsl_ps57in_check.py index 1674312199..1ff70fd903 100644 --- a/tests/startup_gfx2hlsl_ps57in_check.py +++ b/tests/startup_gfx2hlsl_ps57in_check.py @@ -10,11 +10,16 @@ - tools/startup/gfx2hlsl.py reads .ps57in: the suffixed key is pipeline_key() over the entry bytes, the entry's address is both pixel input masks (as the native fixed entry builds them), a request with both .psin and .ps57in is refused, and no .psdep is written for an entry (its bytes are part of the key, so a - changed entry is a different request). tools/startup/shader_cache_rekey.py copies the entry unchanged. + changed entry is a different request). Only the entries the native fixed entry accepts (address 2 or + 0x8002 with 1 to 7 interpolants, as in src/direct_compute_cache.cpp prepare_direct_graphics) translate; + anything wider is refused by name. tools/startup/shader_cache_rekey.py copies the entry unchanged. - src/startup_stubs/d3d12_draw.inl reuses a translation already cached under the plain key: when .txt is published, the entry leaves the key, so a warmed cache hits at once instead of waiting for - a retranslation under the suffixed key. Only a cold pipeline keeps the entry in its key. The runtime - behaviour is Windows-only, so the guard is pinned by source text here, with mutants that must fail it. + a retranslation under the suffixed key. Only a cold pipeline keeps the entry in its key. In + native-shaders-only mode (X2W_NATIVE_SHADERS_ONLY=1) the reuse is skipped, because .txt manifests are + ignored there and a leftover Python manifest would otherwise strip the entry from the request the native + path receives. The runtime behaviour is Windows-only, so the guard is pinned by source text here, with + mutants that must fail it. - tools/startup/run.ps1 restores X2W_NATIVE_SHADERS when the run ends (to what the session had before, or removes it), so the next run without -NativeShaders is not still affected. The restore is pinned by source text here, with mutants that must fail it; the live path is still covered by @@ -119,12 +124,29 @@ def hlsl_bytes(directory, key, stage): expect('plain and suffixed pixel stages differ', hlsl_bytes(d, BASE_KEY, 'ps') != hlsl_bytes(d, KEY, 'ps'), True) + # The native fixed entry accepts address 2 as well as 0x8002, with 1 to 7 interpolants: both ends of + # that range translate here. + addr2_entry = b'PSI1' + struct.pack('