Skip to content

startup: apply .ps57in pixel masks, carry .ps57in through rekey, and reuse cached plain-key translations - #3410

Merged
JohnConnorNPC merged 3 commits into
R0Y00:mainfrom
Ammmae0x:startup/native-shaders-ps57in
Nov 29, 2026
Merged

JohnConnorNPC merged 3 commits into
R0Y00:mainfrom
Ammmae0x:startup/native-shaders-ps57in

Conversation

@Ammmae0x

@Ammmae0x Ammmae0x commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3385

With -NativeShaders, ordinary direct graphics pipelines stopped translating: cached_translation appended the PSI1 slot 57 entry to every 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 (shader request source key mismatch).

#3448 (ea68a605) already landed the key half of that on main: read_slot57_entry and pipeline_key(..., slot57_entry=...) hash the entry bytes into the key, and the request reader refuses a malformed entry. #3449 already made the launcher default to native-shaders-only mode. This PR merges origin/main (5de0b114, merge commit 1fdd1f7f, no force-push) and is cut down to the three pieces main still lacks:

  • tools/startup/gfx2hlsl.py (request_ps_inputs + .psdep guard): takes the pixel input masks from the entry through main's read_slot57_entry — the entry's address is both masks, as the native fixed entry builds them — refuses a request with both .psin and .ps57in, validates the entry's layout (address 2 or 0x8002 with 1 to 7 interpolants, as in prepare_direct_graphics), and verifies the suffixed key. No .psdep is recorded for a slot 57 entry, since its bytes are part of the key. The native path additionally bounds the interpolant count by the pixel interface's attribute count, which the Python fallback cannot know; that bound stays native-only.
  • tools/startup/shader_cache_rekey.py: carries .ps57in in PARTS/OPTIONAL and passes slot57_entry=entry to pipeline_key in current_key(), so a rekeyed slot 57 request is written under its suffixed key with the entry copied unchanged instead of under the plain key with no entry. A request with both records is refused.
  • src/startup_stubs/d3d12_draw.inl (cached_translation): when a translation is already published under the plain key, reuse it instead of suffixing the key. Only a cold pipeline keeps the entry in its key. The reuse is skipped when native_shaders_only() is true.

What each piece does on the default launcher path vs -Translate (@JohnConnorNPC SHOULD #1): since #3449, run.ps1/run.sh set X2W_NATIVE_SHADERS_ONLY=1 unless -Translate is given. In that mode the cached_translation plain-key reuse is skipped (!native_shaders_only() && cache_path(...)), and load_translation never reads a .txt manifest, so the masks and the reuse only change -Translate runs. The .ps57in entry itself is built only when X2W_NATIVE_SHADERS is set; on a -Translate run that variable comes either from -NativeShaders or from what a previous run in the same PowerShell session left behind. tools/startup/run.ps1 is untouched by this PR (zero diff against main).

Tests:

  • tests/startup_gfx2hlsl_ps57in_check.py (new, CTest startup_gfx2hlsl_ps57in): suffixed-key known answer, suffixed request translates and applies the entry masks, plain request still translates without them, address 2 and 7-interpolant entries translate, RTV variant of a suffixed request translates, rekey copies the entry, and negative controls (bad-layout addresses, 0 and 8 interpolants, bad prefix, short and 13-byte entries, both records, entry under the plain key, wide address, bad rekey). Mesh57 native requests still translate. The check also pins the cached_translation guard with in-test mutants.
  • tests/startup_shader_cache_key_check.py: the slot 57 block's "translation does not read the record's values" case is replaced — an address outside the fixed entry's layout (address 3) under its own key is now refused as invalid slot-57 pixel entry layout, since request_ps_inputs reads the address and count. The key still hashes the record as stored, and a key-mismatched entry is refused by load_request as shader request source key mismatch.

Checks run on Linux (this host cannot run Windows CTest or compile_dxil):

  • python -B tools/run_checks_parallel.py --from-dir tests/checks/python-linux with the affected checks — all 61 checks passed
  • python -B tests/startup_translator_check.py — 0 failed
  • python -B tests/startup_gfx2hlsl_watch_check.py — passed
  • python -B tests/startup_gfx2hlsl_refusals_check.py — passed
  • python -B tests/startup_archive_requests_check.py — passed
  • python -B tests/startup_flat_param_check.py — passed
  • python -B tests/startup_mesh57_skeleton_check.py — passed
  • python -B tests/run_checks_parallel_check.py — ok
  • python -B tests/form_coverage_check.py — passed (throwaway venv, needs iced-x86)
  • python -B tests/docs_links_check.py — passed (175 Markdown files, 711 links)
  • python -B tests/bidi_controls_check.py — passed (2670 files)
  • py_compile on the touched Python files — ok
  • git merge-tree --write-tree origin/main HEAD — exits 0; git diff --check — clean

Spot mutations (on sandbox copies outside the repo, each restored after), each made the new check fail as expected: dropping the layout validation in request_ps_inputs (bad-layout entries then translate); dropping the .ps57in file check from the .psdep guard (the suffixed request then writes a .psdep and is no longer complete); dropping the entry branch from rekey current_key (the entry is not copied under the suffixed key); removing !native_shaders_only() && from the plain-key reuse guard; and dropping the mutual-exclusion check in request_ps_inputs (both records are then accepted there; load_request still refuses them).

Not verified here: Windows CTest, compile_dxil, and the native graphics draw path; those are for the WIN-VERIFY run.

Review feedback addressed (@Leyukaka, @JohnConnorNPC):

Windows Verification

  • Commit: 1fdd1f7fb1e3261df9780a82c298fb0e1c00639c
  • Preset dev: ctest --preset dev --no-tests=error -R '^(startup_gfx2hlsl_ps57in|startup_shader_cache_key|startup_tools_lint)$' --output-on-failure -> 3 passed, 0 failed (2.27s); also ^(startup_shader_cache_rekey|startup_translator|startup_run_ps1)$ -> 3 passed, 0 failed (40.91s)
  • Preset release: ctest --preset release --no-tests=error -R '^(startup_gfx2hlsl_ps57in|startup_shader_cache_key|startup_tools_lint)$' --output-on-failure -> 3 passed, 0 failed (1.97s)
  • Repo checks: form_coverage_check.py, docs_links_check.py (175 Markdown files, 711 links), and bidi_controls_check.py (2666 files) passed.
1/3 Test   #56: startup_shader_cache_key .........   Passed    0.96 sec
2/3 Test   #57: startup_tools_lint ...............   Passed    0.42 sec
3/3 Test #1505: startup_gfx2hlsl_ps57in ..........   Passed    0.69 sec

AI-assisted.

…ations

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 R0Y00#3385
@Ammmae0x
Ammmae0x marked this pull request as ready for review November 28, 2026 23:15
@Leyukaka

Copy link
Copy Markdown
Collaborator

Reviewed ec57297d by its diff. The Python key and translation follow the runtime's order for .ps57in (entry built at d3d12_draw.inl:1161-1173, key and write in cached_translation, :652-664), and the mesh path is untouched (with a control). Points:

  1. X2W_NATIVE_SHADERS_ONLY=1. In that mode .txt manifests are ignored, but the reuse guard still looks for one, so a leftover Python manifest strips the entry from the request the native path receives. The native path then either translates a reflected pair without the packed position or refuses it with raw graphics requires its captured pixel entry. Skipping the reuse when native_shaders_only() would avoid it.
  2. Wider than the native path. The Python fallback accepts any address up to 0xffff with enable equal to address and ignores the interpolant count and control flags; the native path accepts only address 2 or 0x8002 with 1 to 7 interpolants. Refusing the rest would keep both paths aligned.
  3. Cache-dependent outcome. Reusing the plain-key translation adds no key collision or stale-revision risk, but the result now depends on what is cached. For pipelines the native path refuses on main this is an improvement; for reflected pairs, which the native path accepts on main, a warm cache now replaces the native entry-aware result with the plain Python one (the PR's own test shows the plain pixel stage lacks the packed position). Worth stating in the body.
  4. Tests. No test covers these: in translate_request (the .psdep condition), 'req_%016x.ps57in' % base changed to % key (an RTV variant gets a .psdep, is refused on the next watcher pass and its .txt removed); in d3d12_draw.inl, (pixel_entry.empty() || changed to (slot57_entry.empty() || (a 0-byte .ps57in under the warm plain key; the C++ guard is only checked by source text); in read_ps57_inputs, len(entry) != 12 changed to < 12 (a 13-byte entry raises struct.error out of translate_request, recorded as translator error instead of the named refusal).

A start-up run with -NativeShaders on main and on this head, from copies of the same warmed cache, would show how many slot 57 pixel stages get a translation and how many are reused under the plain key.

Point 1 should be fixed before merge; 2-4 at the author's choice.

@Ammmae0x

Copy link
Copy Markdown
Contributor Author

Addressed in 24755d1f (thanks @Leyukaka for the thorough diff review):

  • Point 1 (X2W_NATIVE_SHADERS_ONLY=1): cached_translation in src/startup_stubs/d3d12_draw.inl now skips plain-key reuse when native_shaders_only() is true, pinned with a failing mutant in tests/startup_gfx2hlsl_ps57in_check.py.
  • Point 2 (slot-57 layout parity): read_ps57_inputs now refuses entries outside the native fixed-entry layout (SPI_PS_INPUT_ADDR 2 or 0x8002, 1..7 interpolants), with positive (2, 7) and negative (0x8003, 0/8, 1, 0x10002) controls.
  • Point 3 (cache outcome): Documented in the PR body that reusing a cached plain-key translation trades native entry-aware retranslation on warm caches while cold pipelines and X2W_NATIVE_SHADERS_ONLY=1 keep .ps57in in the key.
  • Point 4 (mutant/test gaps): Pinned RTV-variant .psdep absence (% base vs % key), (pixel_entry.empty() || vs (slot57_entry.empty() || in guard_problems(), and a 13-byte .ps57in refusal (len(entry) != 12 vs < 12).

Windows verification (24755d1f, dev & release): 5/5 passed (0 failed, 0 skipped) in both presets (46.06s / 36.80s) plus form_coverage_check, docs_links_check, and bidi_controls_check (3/3 passed).

AI-assisted.

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-read 24755d1f by its diff (ec57297d...24755d1f, 3 files) and the full diff against the merge base.

  1. Fixed. cached_translation skips the plain-key reuse when native_shaders_only() (d3d12_draw.inl:665), so the entry stays in the key and in the request in that mode, as on main. Pinned by source text with the mutant native-shaders-only guard removed.
  2. read_ps57_inputs now refuses anything but address 2 or 0x8002 with 1 to 7 interpolants. The native path additionally checks the count against the reflected pixel interface's attribute count and the address against its reflected input address (src/direct_compute_cache.cpp:723-728). The Python reader has no reflected interface, so that part stays native-only, as the body says. No change requested.
  3. Stated in the body.
  4. All three covered: no .psdep for the RTV variant of a suffixed request, the (pixel_entry.empty() || pin with the mutant publish gated on the wrong entry, and the 13-byte entry refused as invalid slot-57 pixel entry record.

Not covered: the cached_translation change is checked by source text only, and startup_gfx2hlsl_ps57in is not in the pr test preset, so on this PR CI runs it in the Linux Python job only (tests/checks/python-linux/); the Windows results are the ones reported in the body. No -NativeShaders run comparing main and this head on a warmed cache.

Approving 24755d1f.

@Leyukaka

Copy link
Copy Markdown
Collaborator

#3448 (ea68a605) is on main and already hashes .ps57in into pipeline_key (read_slot57_entry, slot57_entry=b''). git merge-tree --write-tree origin/main 24755d1f (main 77d1ba4d) now conflicts in tools/startup/gfx2hlsl.py (3 regions: pipeline_key, the key check in the request reader, read_ps57_inputs against read_slot57_entry) and in tools/startup/run.ps1 (#3449 rewrote the -NativeShaders block where this branch saves nativeShadersBefore).

What main still lacks from this branch:

  • the pixel input masks taken from the entry (request_ps_inputs) and the layout check (address 2 or 0x8002, 1 to 7 interpolants); main's read_slot57_entry checks only the size and the PSI1 tag and does not read the values;
  • .ps57in in PARTS and OPTIONAL of tools/startup/shader_cache_rekey.py;
  • the plain-key reuse in cached_translation (src/startup_stubs/d3d12_draw.inl), which this branch skips when X2W_NATIVE_SHADERS_ONLY=1; since runtime: default to native shaders and Opus decoding #3449 run.ps1 sets that unless -Translate is given.

Could you rebase onto main's read_slot57_entry / slot57_entry and keep only these?

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Review of 24755d1f against main 77d1ba4d. Verdict: hold. This is superseded in its main fix by #3448 (merged as 20bd1553, commit ea68a605). I'd close it, or cut it down to the parts main still lacks. Leyukaka's comment above already lists those parts and the conflict regions, so I won't repeat them.

MUST

  1. It conflicts with main. git merge-tree --write-tree --name-only origin/main 24755d1f → CONFLICT (content): Merge conflict in tools/startup/gfx2hlsl.py and ... in tools/startup/run.ps1. The .ps57in key and refusal fix is already on main: git show origin/main:tools/startup/gfx2hlsl.py | grep -n 'def read_slot57_entry\|def pipeline_key' → def pipeline_key(vs_code, ps_code, root_blob, layout=b'', ps_inputs=None, slot57_entry=b''): and def read_slot57_entry(path):. A rebase should build on those, not add a second reader (read_ps57_inputs) with a different contract.

SHOULD (for the cut-down version)

  1. Say in the body what each remaining piece does on the default launcher path. Since runtime: default to native shaders and Opus decoding #3449, run.ps1/run.sh set X2W_NATIVE_SHADERS_ONLY=1 unless -Translate is given. In that mode the cached_translation plain-key reuse is skipped (!native_shaders_only() && cache_path(...)), and load_translation never reads a .txt manifest (if (manifest.empty() && native_shaders_only()) { tr.manifest_retry.found(); return; }). So the mask application and the reuse only change -Translate runs. The .ps57in entry itself is built only when X2W_NATIVE_SHADERS is set (if (GetEnvironmentVariableW(L"X2W_NATIVE_SHADERS", nullptr, 0) && ...). On a -Translate run that variable comes either from -NativeShaders or from what a previous run in the same PowerShell session left behind.
  2. The shader_cache_rekey.py part is a real gap on main and stands on its own. git show origin/main:tools/startup/shader_cache_rekey.py | grep -n "PARTS = " → PARTS = {'g': ('.b', '.root', '.ia', '.psin', '.a'), ..., and current_key calls pipeline_key(...) without the entry. A rekeyed slot 57 request is therefore written under its plain key with no .ps57in. A small PR with just that change and its control would be easy to review.

AI-assisted.

…the remaining gaps

R0Y00#3448 already hashes .ps57in into pipeline_key (read_slot57_entry /
slot57_entry) and R0Y00#3449 already made the launcher default to
X2W_NATIVE_SHADERS_ONLY=1, so this branch drops its duplicate
read_ps57_inputs and its run.ps1 edits and keeps only what main lacks:
request_ps_inputs takes the pixel input masks from the entry and
validates its layout, translate_request writes no .psdep for an entry,
shader_cache_rekey carries .ps57in, and cached_translation reuses a
cached plain-key translation outside native-shaders-only mode.
@Ammmae0x Ammmae0x changed the title startup: translate .ps57in requests and reuse cached plain-key translations startup: apply .ps57in pixel masks, carry .ps57in through rekey, and reuse cached plain-key translations Nov 29, 2026
@Ammmae0x

Copy link
Copy Markdown
Contributor Author

Rebased onto upstream/main (5de0b114, merge commit 1fdd1f7f) per @Leyukaka and @JohnConnorNPC:

  • Cut down to the 3 remaining gaps: Dropped the duplicate read_ps57_inputs reader (using main's read_slot57_entry / pipeline_key(..., slot57_entry=...) from #3448) and dropped all tools/startup/run.ps1 edits (zero diff against main).
  • Remaining pieces + default-path scope (SHOULD #1): Kept (1) request_ps_inputs layout check (2 / 0x8002, 1..7 interpolants) and .psdep guard in tools/startup/gfx2hlsl.py, (2) .ps57in carry-over in tools/startup/shader_cache_rekey.py, and (3) !native_shaders_only() plain-key reuse in src/startup_stubs/d3d12_draw.inl, with default (X2W_NATIVE_SHADERS_ONLY=1) vs -Translate behavior documented in the PR body.
  • Windows verification (1fdd1f7f): Passed d3d12_xs_shim build + 3/3 in dev (2.27s) and release (1.97s), plus startup_run_ps1 / startup_translator / startup_shader_cache_rekey (3/3, 40.91s) and repo Python checks (175 docs / 2666 files).

AI-assisted.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Re-review of 1fdd1f7f against main cf0c30af, delta only. Verdict: good to merge. 0 MUST. The merge resolved the conflicts I flagged earlier and dropped the duplicate reader. git merge-tree --write-tree origin/main 1fdd1f7f exits 0 (tree a56a683e). No change on main since the merge base touches gfx2hlsl.py, shader_cache_rekey.py, d3d12_draw.inl, direct_compute_cache.cpp or startup_shader_cache_key_check.py (git diff --stat 5de0b114 origin/main -- <those files> is empty).

What it still adds after #3448

  1. Python masks from the entry (request_ps_inputs). The layout check matches the native fixed entry. prepare_direct_graphics also reads the whole second dword as the count (!interpolants || interpolants > 7), accepts address 2 or 0x8002 and builds psin1 with the address twice. The PR does the same: masks = (address, address).
  2. Rekey of .ps57in. Main's PARTS['g'] is ('.b', '.root', '.ia', '.psin', '.a'), so main's rekey neither reads nor copies the entry. After a revision bump it would write every suffixed slot-57 request under its plain key, without the entry, and a runtime with X2W_NATIVE_SHADERS set would then miss all of them. This PR fixes that. It only helps caches whose requests have .ps57in files, though.
  3. Plain-key reuse in cached_translation. On the default launcher path this is inert (X2W_NATIVE_SHADERS_ONLY=1). It is active on -Translate / --translate runs with X2W_NATIVE_SHADERS set.

Measured on a local warmed cache. A runtime that left the slot-57 entry out of the key built this cache. After #3448, a --translate run with X2W_NATIVE_SHADERS set missed 12 slot-57 pipelines. For each one I recomputed the plain key from the run's suffixed request files with this branch's pipeline_key and looked it up in the source cache (read-only):

  • 12/12 suffixed keys match pipeline_key(..., slot57_entry=entry).
  • 12/12 plain keys have a published <key>.txt, and 10 of them logged translated in an earlier run of the same cache.
  • The source cache contains 0 .ps57in files, and current_key on each plain request returns the plain key (0/12 move to the suffixed key).
  • All 12 entries pass the new layout check: 9 have address 2 and 3 have 0x8002, with 1 to 4 interpolants.

So the reuse would have hit all 12 at pipeline creation. That part is inference: I did not run it. The rekey part would not have changed this cache. Without the reuse, the run skipped those pipelines' draws until the background translator landed them, and 3 had not landed by exit.

Tests: tests/startup_gfx2hlsl_ps57in_check.py → startup gfx2hlsl ps57in: 0 failed. tests/startup_shader_cache_key_check.py → startup shader cache key checks: passed. Both pass on a git archive of the PR head and on the merge-tree result. Mutants, each run on a copy:

  • Removing the layout check in request_ps_inputs → 10 FAIL (first: entry with another address is refused: got 'ok').
  • Removing the if entry: branch in rekey current_key → 2 FAIL (rekey copies the entry unchanged: got None).

CI on 1fdd1f7f: Windows build and test, Linux Python checks and gfx1013 fixtures all pass.

SHOULD

  1. Use the cache index for the plain-key probe. const bool base_cached = !native_shaders_only() && cache_path(base_path, ...) && GetFileAttributesW(base_path) ... runs before the translation_by_key() lookup. So every new slot-57 pipeline object in that mode probes the cache root, including a repeat of a key already in the map. For a cold key the probe is a miss. src/startup_stubs/cache_index.h says a missing name in a cache of ~274k entries costs ~155 ms under Wine, which is why native_mesh57.inl asks cache_lookup(...) == CacheLookup::absent first. cache_lookup(L"", key, L".txt") != x2w_startup::CacheLookup::absent && GetFileAttributesW(...) would keep the hit and skip the miss. A .txt written by another process during the run would read absent until the next process, which only means it takes the suffixed key, as today. I did not measure the added time.

NIT

  1. The new comment says "the native path can still try the plain request". When the reuse hits, load_translation finds the manifest, and translation_for_targets only calls try_native_graphics when manifest_retry.pending() || native_shaders_only(). So on a hit the native path does not run.
  2. Consider one counter or log line for the reuse, such as "slot 57 pipeline : reused plain-key translation". A run would then show how many pipelines took the plain translation, which has no packed position for 0x8002 entries, instead of the masked one this PR adds.

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

startup: -NativeShaders stops ordinary direct graphics pipelines from translating (PSI1 key suffix, .ps57in not read by fallback)

3 participants