startup: cache translated graphics pipelines #3839

Open
github-bot wants to merge 10 commits from github-bot/gh-3837 into main AGit
Owner

@⁠Leyukaka on GitHub · 2026-12-02 08:12 UTC · original
Leyukaka:takeover/pso-cache-1641 → main · +1585 −6 · 9 files · 10 commits · open

Takes over #1641 by @⁠lambirou with his commits. #1641 was closed after the rebase request of 11-25, and the question asked there on 12-01 has had no reply so far. His three commits are kept with their authorship and messages; the conflicts with current main are resolved inside them, mechanically. @⁠lambirou, if you would rather continue this yourself, say so and I will close this PR.

Problem

The translated draw path creates each desktop graphics PSO again on every run and keeps only an in-process table. X2W_SHADER_CACHE stores the translated shader bytecode, but not the driver's compiled pipelines, so every run pays the driver's pipeline compile again.

Change

The cache is opt-in: it needs X2W_D3D12_PSO_LIBRARY=1 and X2W_SHADER_CACHE. With the switch unset, nothing constructs the library or hashes the key, and pipeline_for calls CreateGraphicsPipelineState exactly as main does.

From #1641 (see its description for the full design and the review history):

  • src/startup_stubs/graphics_pipeline_cache.h: an ID3D12PipelineLibrary cache in X2W_SHADER_CACHE\pso_<adapter>.lib, with a versioned header, a checksum and an adapter/driver identity. The file is replaced atomically.
  • pipeline_for tries LoadGraphicsPipeline first and falls back to CreateGraphicsPipelineState on a miss, a corrupt or truncated file, a driver or adapter mismatch, or a missing ID3D12PipelineLibrary. A cache failure never fails a draw.

Added here:

  • 1dd7a9deb: the library gets its own name, distinct from the in-process pso_state_key. The name covers the translated VS/PS bytes, the blend and depth blocks the PSO really uses, the rasterizer state, the input layout, the geometry stage bytes, the sample mask, the sample description and the topology type.
  • f17ba72f3 (maintainer): the X2W_D3D12_PSO_LIBRARY=1 opt-in gate.
  • cde52a18b, 7784dee76: the write schedule.
    • The present thread writes the first pending library at the next Present, then at most once every 15 s after the previous attempt. A device change or a module unload writes once more.
    • It only tries the locks, so a frame never waits for a draw.
    • The library is serialized into one buffer, and the checksum and the file write run outside the cache lock.
    • The driver compile runs outside the cache lock.
    • A failed write keeps the stores pending and is retried.
    • The cache log reports file loaded, file new or file unset.
  • 2e40b1d35: on process exit the cache follows the d3d12_teardown.h boundary. Nothing is serialized, written or released, and stores made since the last write are compiled again by the next run.
  • 135ea1b57: a Present smoke test, the move of the registration to tests/cmake/startup_pso_cache.cmake, and the pr preset entries.

As #1641 states, the implementation uses only the public Microsoft ID3D12PipelineLibrary API.

Validation

Windows, MSVC, head 135ea1b57.

cmake --build --preset dev --target startup_pso_cache_check startup_stream_depth d3d12_xs_shim
ctest --preset dev --no-tests=error -R "^(startup_pso_cache|startup_pso_library_present_(warp|hardware))$" -V

Result: startup_pso_cache, startup_pso_library_present_warp and startup_pso_library_present_hardware passed (3/3).

  • startup_pso_cache (WARP) covers:
    • cold store and warm hit;
    • corrupt file, disabled interface, concurrent calls;
    • cold-created and warm-loaded pixels;
    • key-field controls for blend, depth and the extended state;
    • the write schedule (first write at once, no rewrite before the interval, one write when due, none without a new store, the final write at destruction);
    • the retry after a blocked write.
  • startup_pso_library_present_{warp,hardware} runs the startup_stream_depth draws plus one guest Present, three times on one cache directory:
    • switch unset: no library file and correct readbacks;
    • switch set, cold: one library file exists after the run, and the debug layer may report only id 971;
    • switch set, fresh process: the file is loaded, every pipeline is a hit, no debug-layer message, and the readbacks are byte-identical to the unset run.
  • The pr preset runs startup_pso_cache and startup_pso_library_present_warp.

Known limits

  • PSOs replayed from recipes (X2W_PSO_RECIPE_DIR) are still created directly and bypass the cache.
  • There is no eviction. The library grows up to the 256 MB cap; beyond it, persistence fails and is counted, and draws continue.
  • Each write still serializes the whole library. Its frame cost on a physical adapter with a large library has not been measured.
  • Stores made after the last write are lost at process exit, by design, and compiled again by the next run.
  • No test fails if the process-exit detach is removed. The smoke test only shows that its processes exit with code 0 with the switch set.
**@⁠Leyukaka** on GitHub · 2026-12-02 08:12 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3837) `Leyukaka:takeover/pso-cache-1641` → `main` · +1585 −6 · 9 files · 10 commits · open Takes over #1641 by @⁠lambirou with his commits. #1641 was closed after the rebase request of 11-25, and the question asked there on 12-01 has had no reply so far. His three commits are kept with their authorship and messages; the conflicts with current `main` are resolved inside them, mechanically. @⁠lambirou, if you would rather continue this yourself, say so and I will close this PR. ## Problem The translated draw path creates each desktop graphics PSO again on every run and keeps only an in-process table. `X2W_SHADER_CACHE` stores the translated shader bytecode, but not the driver's compiled pipelines, so every run pays the driver's pipeline compile again. ## Change The cache is **opt-in**: it needs `X2W_D3D12_PSO_LIBRARY=1` and `X2W_SHADER_CACHE`. With the switch unset, nothing constructs the library or hashes the key, and `pipeline_for` calls `CreateGraphicsPipelineState` exactly as `main` does. From #1641 (see its description for the full design and the review history): - `src/startup_stubs/graphics_pipeline_cache.h`: an `ID3D12PipelineLibrary` cache in `X2W_SHADER_CACHE\pso_<adapter>.lib`, with a versioned header, a checksum and an adapter/driver identity. The file is replaced atomically. - `pipeline_for` tries `LoadGraphicsPipeline` first and falls back to `CreateGraphicsPipelineState` on a miss, a corrupt or truncated file, a driver or adapter mismatch, or a missing `ID3D12PipelineLibrary`. A cache failure never fails a draw. Added here: - `1dd7a9deb`: the library gets its own name, distinct from the in-process `pso_state_key`. The name covers the translated VS/PS bytes, the blend and depth blocks the PSO really uses, the rasterizer state, the input layout, the geometry stage bytes, the sample mask, the sample description and the topology type. - `f17ba72f3` (maintainer): the `X2W_D3D12_PSO_LIBRARY=1` opt-in gate. - `cde52a18b`, `7784dee76`: the write schedule. - The present thread writes the first pending library at the next Present, then at most once every 15 s after the previous attempt. A device change or a module unload writes once more. - It only tries the locks, so a frame never waits for a draw. - The library is serialized into one buffer, and the checksum and the file write run outside the cache lock. - The driver compile runs outside the cache lock. - A failed write keeps the stores pending and is retried. - The cache log reports `file loaded`, `file new` or `file unset`. - `2e40b1d35`: on process exit the cache follows the `d3d12_teardown.h` boundary. Nothing is serialized, written or released, and stores made since the last write are compiled again by the next run. - `135ea1b57`: a Present smoke test, the move of the registration to `tests/cmake/startup_pso_cache.cmake`, and the `pr` preset entries. As #1641 states, the implementation uses only the public Microsoft `ID3D12PipelineLibrary` API. ## Validation Windows, MSVC, head `135ea1b57`. ```powershell cmake --build --preset dev --target startup_pso_cache_check startup_stream_depth d3d12_xs_shim ctest --preset dev --no-tests=error -R "^(startup_pso_cache|startup_pso_library_present_(warp|hardware))$" -V ``` Result: `startup_pso_cache`, `startup_pso_library_present_warp` and `startup_pso_library_present_hardware` passed (3/3). - `startup_pso_cache` (WARP) covers: - cold store and warm hit; - corrupt file, disabled interface, concurrent calls; - cold-created and warm-loaded pixels; - key-field controls for blend, depth and the extended state; - the write schedule (first write at once, no rewrite before the interval, one write when due, none without a new store, the final write at destruction); - the retry after a blocked write. - `startup_pso_library_present_{warp,hardware}` runs the `startup_stream_depth` draws plus one guest Present, three times on one cache directory: - switch unset: no library file and correct readbacks; - switch set, cold: one library file exists after the run, and the debug layer may report only id 971; - switch set, fresh process: the file is loaded, every pipeline is a hit, no debug-layer message, and the readbacks are byte-identical to the unset run. - The `pr` preset runs `startup_pso_cache` and `startup_pso_library_present_warp`. ## Known limits - PSOs replayed from recipes (`X2W_PSO_RECIPE_DIR`) are still created directly and bypass the cache. - There is no eviction. The library grows up to the 256 MB cap; beyond it, persistence fails and is counted, and draws continue. - Each write still serializes the whole library. Its frame cost on a physical adapter with a large library has not been measured. - Stores made after the last write are lost at process exit, by design, and compiled again by the next run. - No test fails if the process-exit detach is removed. The smoke test only shows that its processes exit with code 0 with the switch set.
The present thread flushes outside draw_lock while the draw thread may replace the cache on a device change. Return a shared_ptr from pipeline_cache_for so a concurrent Flush keeps the old cache alive. Existing startup_pso_cache, docs_links and bidi checks pass.
Extract translated_code_key and pipeline_key so the device-free control can require two translations with the same title key but different VS/PS bytes to produce different pipeline names. Dropping either byte hash or the code-key fallback now fails the control. startup_pso_cache 1/1; docs_links and bidi_controls pass.
Since this change was written, pipeline_for keys bound.pipelines by
pso_state_key and builds the rasterizer, a fixed-stream input layout,
the rectangle geometry stage and export write masks into the PSO. Keep
that state key for the in-process table and give the pipeline library
its own name: pipeline_key over the translated code and the blend/depth
blocks the PSO really uses, extended with the rasterizer, input layout,
geometry bytes and sample/topology fields. Without it two translations
with the same draw state shared one library name and the second load
was refused. The code key is only computed when both stages are loaded.
The WARP control now requires each extended field to change the name.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 08:16:07 +00:00
Author
Owner

@⁠lukaslw-lab on GitHub · 2026-12-02 08:25 UTC · original · requested changes

Reviewed 1dd7a9de by reading the diff; not built or run.

Verdict: changes requested. The cache logic is careful: names, fallbacks, atomic replace and the extended key are all handled. But it becomes active on every render run, the flush is synchronous on the present thread, and it has not been tested on hardware.

  • MUST Make it opt-in (e.g. X2W_D3D12_PSO_LIBRARY=1) until it is measured on a physical adapter. run.ps1 -Render always sets X2W_SHADER_CACHE (tools/startup/run.ps1:83), so with this PR every render run uses the library. The two items below are unmeasured, as the Known limits say. The other D3D12 runtime switches started opt-in and moved into the presets with run evidence. This should follow the same path.
  • SHOULD Avoid whole-library rewrites on the present thread. Each frame that stored a PSO calls PersistLibrary from queue_present (d3d12_xs_device.cpp:8923): GetSerializedSize + Serialize of the whole library, two heap copies of it (payload + header copy, up to 2 × 256 MB), then FlushFileBuffers and MOVEFILE_WRITE_THROUGH. During a load, most frames add PSOs, so the file is rewritten nearly every frame and the transient commit grows with the library. Throttling (every N s, or at exit) and serializing into one buffer would bound both.
  • SHOULD Don't hold the cache lock across CreateGraphicsPipelineState. GetOrCreateGraphicsPipeline takes lock_ exclusively for the whole driver compile (graphics_pipeline_cache.h:319), and Flush takes it for the whole write. pipeline_for runs under draw_lock, so a flush stalls the draw path, and concurrent pipeline creation is serialized. Locking only around LoadGraphicsPipeline and StorePipeline avoids both.
  • NIT Register the test in tests/cmake/startup_pso_cache.cmake (per tests/cmake/README.md) rather than in CMakeLists.txt. startup_pso_cache is also not in the pr preset filter, so CI does not run it.
  • Checked, no finding:
    • A name collision cannot hand back a wrong PSO: LoadGraphicsPipeline validates the desc, and a duplicate StorePipeline is counted and kept out.
    • A driver or adapter mismatch drops the file.
    • The shared_ptr keeps an old cache alive through a concurrent flush after a device change.
    • pso_state_key stays the in-process key.
**@⁠lukaslw-lab** on GitHub · 2026-12-02 08:25 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6147095187) · **requested changes** Reviewed `1dd7a9de` by reading the diff; not built or run. **Verdict: changes requested.** The cache logic is careful: names, fallbacks, atomic replace and the extended key are all handled. But it becomes active on every render run, the flush is synchronous on the present thread, and it has not been tested on hardware. - **MUST** Make it opt-in (e.g. `X2W_D3D12_PSO_LIBRARY=1`) until it is measured on a physical adapter. `run.ps1 -Render` always sets `X2W_SHADER_CACHE` (`tools/startup/run.ps1:83`), so with this PR every render run uses the library. The two items below are unmeasured, as the Known limits say. The other D3D12 runtime switches started opt-in and moved into the presets with run evidence. This should follow the same path. - **SHOULD** Avoid whole-library rewrites on the present thread. Each frame that stored a PSO calls `PersistLibrary` from `queue_present` (`d3d12_xs_device.cpp:8923`): `GetSerializedSize` + `Serialize` of the **whole** library, two heap copies of it (payload + header copy, up to 2 × 256 MB), then `FlushFileBuffers` and `MOVEFILE_WRITE_THROUGH`. During a load, most frames add PSOs, so the file is rewritten nearly every frame and the transient commit grows with the library. Throttling (every N s, or at exit) and serializing into one buffer would bound both. - **SHOULD** Don't hold the cache lock across `CreateGraphicsPipelineState`. `GetOrCreateGraphicsPipeline` takes `lock_` exclusively for the whole driver compile (`graphics_pipeline_cache.h:319`), and `Flush` takes it for the whole write. `pipeline_for` runs under `draw_lock`, so a flush stalls the draw path, and concurrent pipeline creation is serialized. Locking only around `LoadGraphicsPipeline` and `StorePipeline` avoids both. - **NIT** Register the test in `tests/cmake/startup_pso_cache.cmake` (per `tests/cmake/README.md`) rather than in `CMakeLists.txt`. `startup_pso_cache` is also not in the `pr` preset filter, so CI does not run it. - Checked, no finding: - A name collision cannot hand back a wrong PSO: `LoadGraphicsPipeline` validates the desc, and a duplicate `StorePipeline` is counted and kept out. - A driver or adapter mismatch drops the file. - The `shared_ptr` keeps an old cache alive through a concurrent flush after a device change. - `pso_state_key` stays the in-process key.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 19:48 UTC · original · reviewed

I reran ctest --preset dev --no-tests=error -j 1 --output-on-failure -R '^startup_pso_cache$' on 1dd7a9de with MSVC Debug. The WARP library fixture passed (1/1), including its warm-library and bad-file controls. It calls the cache helper directly. It does not exercise the presenter flush or establish the hardware frame cost, so the open opt-in request still applies.

**@⁠R0Y00** on GitHub · 2026-12-02 19:48 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6157767552) · **reviewed** I reran `ctest --preset dev --no-tests=error -j 1 --output-on-failure -R '^startup_pso_cache$'` on 1dd7a9de with MSVC Debug. The WARP library fixture passed (1/1), including its warm-library and bad-file controls. It calls the cache helper directly. It does not exercise the presenter flush or establish the hardware frame cost, so the open opt-in request still applies.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 21:07 UTC · original · reviewed

Additional independent hardware result on 1dd7a9de: the unmodified WARP fixture passed. In a local copy of tests/startup_pso_cache_check.cpp, I selected the RTX 4060 Laptop (32.0.16.1656, Windows 11 build 26300) via create_device(false, unmatched). The fixed color oracle then reported ff407fff versus WARP's ff4080ff. Cold-create and warm-load pixels matched byte for byte on NVIDIA. Allowing the one-step green difference only in the fixed oracle gave a full hardware fixture pass, including the bad-file and disabled-interface controls. This tests the helper and the driver library. The review request to start with an explicit opt-in and measure synchronous Present-thread flushing still applies.

**@⁠R0Y00** on GitHub · 2026-12-02 21:07 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6159027768) · **reviewed** Additional independent hardware result on `1dd7a9de`: the unmodified WARP fixture passed. In a local copy of `tests/startup_pso_cache_check.cpp`, I selected the RTX 4060 Laptop (32.0.16.1656, Windows 11 build 26300) via `create_device(false, unmatched)`. The fixed color oracle then reported `ff407fff` versus WARP's `ff4080ff`. Cold-create and warm-load pixels matched byte for byte on NVIDIA. Allowing the one-step green difference only in the fixed oracle gave a full hardware fixture pass, including the bad-file and disabled-interface controls. This tests the helper and the driver library. The review request to start with an explicit opt-in and measure synchronous Present-thread flushing still applies.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 21:33 UTC · original · reviewed

I pushed f17ba72f to address the opt-in request. The runtime now requires X2W_D3D12_PSO_LIBRARY=1 before it constructs the library or hashes the on-disk key. With MSVC Debug on Windows 11 build 26300, a local startup_stream_depth variant called guest Present after drawing. With the switch unset, the WARP and RTX 4060 Laptop runs drew correctly and neither wrote a library. With it set, both wrote the library during Present. A fresh process then logged warm hits and reproduced every source-built color/depth readback byte for byte (2/2 fixtures in each mode). Cold misses emitted D3D12 warning 971 with the debug layer on, as expected for missing names. The warm runs emitted no warnings. The local Present variant does not measure frame-time cost. Please re-review the opt-in gate on this head.

**@⁠R0Y00** on GitHub · 2026-12-02 21:33 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6159407125) · **reviewed** I pushed `f17ba72f` to address the opt-in request. The runtime now requires `X2W_D3D12_PSO_LIBRARY=1` before it constructs the library or hashes the on-disk key. With MSVC Debug on Windows 11 build 26300, a local `startup_stream_depth` variant called guest Present after drawing. With the switch unset, the WARP and RTX 4060 Laptop runs drew correctly and neither wrote a library. With it set, both wrote the library during Present. A fresh process then logged warm hits and reproduced every source-built color/depth readback byte for byte (2/2 fixtures in each mode). Cold misses emitted D3D12 warning 971 with the debug layer on, as expected for missing names. The warm runs emitted no warnings. The local Present variant does not measure frame-time cost. Please re-review the opt-in gate on this head.
Author
Owner

@⁠Leyukaka on GitHub · 2026-12-02 21:57 UTC · original

Thank you for the gate and for the runs. I re-reviewed f17ba72f by reading the diff; I have not built or run this head.

Verdict: the gate is correct. With X2W_D3D12_PSO_LIBRARY unset, nothing constructs the library or hashes its key:

  • graphics_pipeline_library_enabled() (src/startup_stubs/d3d12_draw.inl:161) reads x2w_local::env_on once. It parses like the other switches: the value must start with 1.
  • load_translation skips translated_code_key (d3d12_draw.inl:850).
  • pipeline_cache_for returns before its mutex (d3d12_draw.inl:1595). The only construction site of GraphicsPipelineCache is behind it (:1604), so the file is never opened.
  • flush_pipeline_cache, called from queue_present (d3d12_xs_device.cpp:9102), returns immediately (d3d12_draw.inl:1618).
  • pipeline_for calls CreateGraphicsPipelineState directly, as main does (d3d12_draw.inl:1877-1889). The only visible difference is the , cache off suffix in the log line. tests/native_rtv_variant_check.py:303 matches a prefix of that line.

Non-blocking:

  • No committed test asserts that nothing is written when the switch is unset. startup_pso_cache calls the helper directly, so it does not pass through the gate.
  • The switch is described in docs/startup-runtime.md:1994 but has no row in the environment table.
  • lukaslw-lab's NIT is still open: the test is registered in CMakeLists.txt:1392, not in tests/cmake/, and is not in the pr filter.

A follow-up for the two remaining SHOULD items is ready locally as two commits on top of f17ba72f. It is not pushed and leaves your gate unchanged:

  • cde52a18b startup: throttle pipeline library writes and compile outside the lock
    • The present thread writes at most every 15 s; destruction writes once more.
    • The present thread only tries the locks, so it never waits for a draw.
    • The library is serialized into one buffer. The checksum, the file write and CreateGraphicsPipelineState run outside the cache lock.
    • startup_pso_cache gains a throttle check.
  • 2e40b1d35 startup: detach the pipeline library on process exit
    • Same boundary as the caches in d3d12_teardown.h: when the process is terminating, nothing is serialized or released.

Validation: these two commits have not been built or run on your head yet. My earlier runs were on my previous head (1dd7a9de). I will run startup_pso_cache on the new base and report before asking for a merge.

Would you like them pushed to this branch, or opened as a separate PR?

**@⁠Leyukaka** on GitHub · 2026-12-02 21:57 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3837#issuecomment-7360031062) Thank you for the gate and for the runs. I re-reviewed `f17ba72f` by reading the diff; I have not built or run this head. **Verdict: the gate is correct.** With `X2W_D3D12_PSO_LIBRARY` unset, nothing constructs the library or hashes its key: - `graphics_pipeline_library_enabled()` (`src/startup_stubs/d3d12_draw.inl:161`) reads `x2w_local::env_on` once. It parses like the other switches: the value must start with `1`. - `load_translation` skips `translated_code_key` (`d3d12_draw.inl:850`). - `pipeline_cache_for` returns before its mutex (`d3d12_draw.inl:1595`). The only construction site of `GraphicsPipelineCache` is behind it (`:1604`), so the file is never opened. - `flush_pipeline_cache`, called from `queue_present` (`d3d12_xs_device.cpp:9102`), returns immediately (`d3d12_draw.inl:1618`). - `pipeline_for` calls `CreateGraphicsPipelineState` directly, as main does (`d3d12_draw.inl:1877-1889`). The only visible difference is the `, cache off` suffix in the log line. `tests/native_rtv_variant_check.py:303` matches a prefix of that line. Non-blocking: - No committed test asserts that nothing is written when the switch is unset. `startup_pso_cache` calls the helper directly, so it does not pass through the gate. - The switch is described in `docs/startup-runtime.md:1994` but has no row in the environment table. - lukaslw-lab's NIT is still open: the test is registered in `CMakeLists.txt:1392`, not in `tests/cmake/`, and is not in the `pr` filter. A follow-up for the two remaining SHOULD items is ready locally as two commits on top of `f17ba72f`. It is not pushed and leaves your gate unchanged: - `cde52a18b` startup: throttle pipeline library writes and compile outside the lock - The present thread writes at most every 15 s; destruction writes once more. - The present thread only tries the locks, so it never waits for a draw. - The library is serialized into one buffer. The checksum, the file write and `CreateGraphicsPipelineState` run outside the cache lock. - `startup_pso_cache` gains a throttle check. - `2e40b1d35` startup: detach the pipeline library on process exit - Same boundary as the caches in `d3d12_teardown.h`: when the process is terminating, nothing is serialized or released. Validation: these two commits have not been built or run on your head yet. My earlier runs were on my previous head (`1dd7a9de`). I will run `startup_pso_cache` on the new base and report before asking for a merge. Would you like them pushed to this branch, or opened as a separate PR?
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 22:44 UTC · original

Thanks for rechecking the default-off gate. Please add the throttled writes and the process-exit detach to this PR after testing them on top of f17ba72f. They are part of the behavior of the same opt-in cache, and the detach should be checked before landing it. Please include a Present smoke test with the switch unset and set, then a fresh-process warm-cache readback. If the test can run in PR CI, register it under tests/cmake/ and add the applicable WARP case to the pr preset.

**@⁠R0Y00** on GitHub · 2026-12-02 22:44 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3837#issuecomment-7361098701) Thanks for rechecking the default-off gate. Please add the throttled writes and the process-exit detach to this PR after testing them on top of `f17ba72f`. They are part of the behavior of the same opt-in cache, and the detach should be checked before landing it. Please include a Present smoke test with the switch unset and set, then a fresh-process warm-cache readback. If the test can run in PR CI, register it under `tests/cmake/` and add the applicable WARP case to the `pr` preset.
Author
Owner

@⁠lukaslw-lab on GitHub · 2026-12-02 22:47 UTC · original · requested changes

Re-review on f17ba72f: full diff and test read, built and run this time.

Verdict: changes requested, for one point. My earlier MUST (opt-in) is resolved. The remaining one is the process-exit detach, which @⁠R0Y00 already asked for here.

  • Resolved: with X2W_D3D12_PSO_LIBRARY unset, nothing constructs the library, hashes the code key or flushes, and pipeline_for creates the PSO directly.
  • MUST Detach the library at process exit. The function-local static std::shared_ptr in pipeline_cache_for runs Shutdown (serialize, write, release) in this module's CRT teardown. d3d12_teardown.h documents why the other caches release nothing at that point (0xC0000409 through the AMD driver). This is by reading: the two startup_stream_depth processes below exited with code 0 with the cache on.
  • SHOULD (open) Throttle the Present-thread write. In startup_pso_cache one flush after eight small PSOs took 17.1 and 13.4 ms here (WARP, two runs), before any title-sized library.
  • SHOULD (open) Compile outside lock_. GetOrCreateGraphicsPipeline still holds it across CreateGraphicsPipelineState.
  • NIT Flush clears dirty_ even when PersistLibrary fails, so those entries are not retried until the next store.
  • NIT The log says file present whenever the directory is set; it prints configured(), not whether the file exists.
  • NIT The PR body still says the cache needs only X2W_SHADER_CACHE.

Run: Windows 10 19045, RTX 3080 Ti (32.0.16.1047) and WARP 10.0.19041.7811, MSVC Release, full build.

Test Switch unset =1
startup_pso_cache pass pass
startup_frame_copy_{warp,hardware} pass pass
startup_stream_depth_{warp,hardware} pass fail
native_rtv_variant_{warp,hardware} fail fail
  • startup_stream_depth fails only because startup_stream_depth_check.py:71 rejects every warning, and each cold miss emits id 971. It stops before its pixel comparison, so this run says nothing about pixels with the switch set. The requested Present smoke test will need to allow 971 on the cold run.
  • native_rtv_variant_warp fails on this host in both modes because the in-box runtime rejects the shader model (id 68); I did not trace the hardware one.

Not measured: hardware frame cost, and a warm run in a title.

AI-assisted.

**@⁠lukaslw-lab** on GitHub · 2026-12-02 22:47 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6160406637) · **requested changes** Re-review on `f17ba72f`: full diff and test read, built and run this time. **Verdict: changes requested, for one point.** My earlier MUST (opt-in) is resolved. The remaining one is the process-exit detach, which @⁠R0Y00 already asked for here. - **Resolved:** with `X2W_D3D12_PSO_LIBRARY` unset, nothing constructs the library, hashes the code key or flushes, and `pipeline_for` creates the PSO directly. - **MUST** Detach the library at process exit. The function-local `static std::shared_ptr` in `pipeline_cache_for` runs `Shutdown` (serialize, write, release) in this module's CRT teardown. `d3d12_teardown.h` documents why the other caches release nothing at that point (0xC0000409 through the AMD driver). This is by reading: the two `startup_stream_depth` processes below exited with code 0 with the cache on. - **SHOULD (open)** Throttle the Present-thread write. In `startup_pso_cache` one flush after eight small PSOs took 17.1 and 13.4 ms here (WARP, two runs), before any title-sized library. - **SHOULD (open)** Compile outside `lock_`. `GetOrCreateGraphicsPipeline` still holds it across `CreateGraphicsPipelineState`. - **NIT** `Flush` clears `dirty_` even when `PersistLibrary` fails, so those entries are not retried until the next store. - **NIT** The log says `file present` whenever the directory is set; it prints `configured()`, not whether the file exists. - **NIT** The PR body still says the cache needs only `X2W_SHADER_CACHE`. Run: Windows 10 19045, RTX 3080 Ti (32.0.16.1047) and WARP 10.0.19041.7811, MSVC Release, full build. | Test | Switch unset | `=1` | |---|---|---| | `startup_pso_cache` | pass | pass | | `startup_frame_copy_{warp,hardware}` | pass | pass | | `startup_stream_depth_{warp,hardware}` | pass | fail | | `native_rtv_variant_{warp,hardware}` | fail | fail | - `startup_stream_depth` fails only because `startup_stream_depth_check.py:71` rejects every warning, and each cold miss emits id 971. It stops before its pixel comparison, so this run says nothing about pixels with the switch set. The requested Present smoke test will need to allow 971 on the cold run. - `native_rtv_variant_warp` fails on this host in both modes because the in-box runtime rejects the shader model (id 68); I did not trace the hardware one. Not measured: hardware frame cost, and a warm run in a title. AI-assisted.
With X2W_D3D12_PSO_LIBRARY=1, every presented frame that stored a PSO
serialized the whole library on the present thread, copied it twice
and rewrote the file. The present thread now writes a pending library
at most once every 15 seconds after the first unwritten store, and
destruction writes the rest. It only tries the cache locks, so a frame
never waits for a draw.

The library is serialized into one buffer behind the header slot under
the cache lock; the checksum and the file write run after it is
released, and a separate lock keeps an older snapshot from replacing a
newer one. The cache lock no longer covers CreateGraphicsPipelineState:
a second thread that misses the same name compiles too and its store
is counted as a duplicate. Teardown only tries the locks, so module
unload never blocks on a lock held by a terminated thread.

The opt-in gate is unchanged. The WARP fixture adds a throttle check
and accepts concurrent misses.
The cache is a function-local static destroyed during the module's CRT
teardown. On process exit the display driver may already be detached
and the other threads are gone, so Serialize and Release must not reach
it. Follow the d3d12_teardown.h boundary used by the other caches: when
d3d12_process_terminating is set, detach the library, blob and device
references and drop the stores not yet written (at most one throttle
interval); the next run compiles them again. Device changes and an
explicit unload still write the pending stores and release normally.
A failed Serialize or file replace cleared the dirty flag, so those
stores were not written again until the next new PSO. They now stay
pending, and the next due Present retries them.

The present-thread throttle started its interval at the first unwritten
store, so a run shorter than 15 seconds never wrote from Present, and
the process-exit detach then dropped everything. The first pending
library is now written at the next Present, and later writes wait 15
seconds after the previous attempt.

The cache log line printed "file present" whenever X2W_SHADER_CACHE was
set. It now reports "file loaded" when a valid library file was read,
"file new" when the library starts empty, and "file unset".

The WARP fixture checks the first write, the interval, and a write
blocked by a directory in place of the file that is retried once the
directory is gone.
startup_pso_library_present_{warp,hardware} runs the startup_stream_depth
draws, followed by one guest Present (the fixture's new "present"
argument), three times on one cache directory:

- switch unset: correct readbacks, every PSO created directly, and no
  pso_*.lib written by Present or at exit;
- switch set, cold: misses, the library file is written by Present
  (process exit writes nothing), and the debug layer reports only
  id 971 for names missing from the library;
- switch set, fresh process: the file is loaded, every pipeline is a
  hit, no debug-layer message, and all fourteen readbacks are
  byte-identical to the first run.

startup_pso_cache moves to tests/cmake/startup_pso_cache.cmake with the
new tests. startup_pso_cache and the WARP case join the pr preset, with
their targets in the pr build. docs/startup-runtime.md describes the
write schedule, the retry, the log line and both tests.
Author
Owner

@⁠Leyukaka on GitHub · 2026-12-02 23:22 UTC · original

Thank you both. Four commits are pushed on top of f17ba72f. The opt-in gate is unchanged.

  • cde52a18b startup: throttle pipeline library writes and compile outside the lock
  • 2e40b1d35 startup: detach the pipeline library on process exit
  • 7784dee76 startup: retry failed pipeline library writes and write the first at once
  • 135ea1b57 startup: smoke-test the pipeline library through guest Present

Behaviour change: the library is first written at the first Present with a pending store, then at most every 15 s. Process exit no longer writes, so stores made after the last write are compiled again by the next run.

By review point:

  • MUST: detach at process exit. When d3d12_process_terminating is set, Shutdown clears library_, blob_ and device_ and returns before any Serialize, file write or Release (src/startup_stubs/graphics_pipeline_cache.h:311-316). This is the d3d12_teardown.h boundary. A device change or FreeLibrary still writes the pending stores and releases (:319-335).
  • SHOULD: throttle the Present-thread write. flush_pipeline_cache calls FlushIfDue(GetTickCount64()) (src/startup_stubs/d3d12_draw.inl:1622).
    • Every attempt starts a new 15 s interval (graphics_pipeline_cache.h:486-521, persist_interval_ms at :30).
    • The present thread only tries both locks (:494-495), so it never waits for a draw.
    • The library is serialized into one buffer behind the header slot. The checksum and the file write run after lock_ is released (:510-511).
  • SHOULD: compile outside lock_. GetOrCreateGraphicsPipeline takes lock_ for LoadGraphicsPipeline (:348), releases it before CreateGraphicsPipelineState (:381) and takes it again only for StorePipeline (:389). Two threads that miss the same name both compile, and the second store is counted as a duplicate.
  • NIT: dirty_ on failure. A failed Serialize keeps the stores pending. A failed file replace marks them pending again, and the next due Present retries them without a new store (:507-518).
  • NIT: "file present". The log prints file loaded (a valid file was read), file new (the library starts empty) or file unset (d3d12_draw.inl:1609-1612, loaded_from_file at graphics_pipeline_cache.h:285).
  • NIT: PR body. Updated for the switch, the write schedule and the tests.

Tests:

  • Present smoke test. tests/startup_pso_library_present_check.py. startup_stream_depth takes a new present argument that calls guest Present (queue slot 32) after its fourteen cases (tests/startup_stream_depth.cpp:219,456). Three runs share one cache directory:
    • Switch unset: correct readbacks, cache off on every pipeline, no pso_*.lib.
    • Switch set, cold: one non-empty library file exists after the run. The debug layer may report id 971, and nothing else, in this run only.
    • Switch set, fresh process: file loaded, one hit per pipeline created in the cold run, no miss, no debug-layer message. All fourteen readbacks are byte-identical to the unset run, and the library file is unchanged.
  • Registration. tests/cmake/startup_pso_cache.cmake registers startup_pso_cache (moved from CMakeLists.txt) and startup_pso_library_present_{warp,hardware}. The pr test preset runs startup_pso_cache and startup_pso_library_present_warp, and the pr build preset builds startup_pso_cache_check and startup_stream_depth.
  • Fixture. startup_pso_cache also checks the first write, the interval, and a write blocked by a directory in place of the file, retried once the directory is removed (tests/startup_pso_cache_check.cpp:635-714).

Validation: Windows, MSVC, head 135ea1b57. startup_pso_cache, startup_pso_library_present_warp and startup_pso_library_present_hardware passed (3/3).

Not covered: no test fails if the detach is removed. The smoke test only shows that all its processes exit with code 0 with the switch set. The frame cost of a write on a physical adapter is still not measured.

**@⁠Leyukaka** on GitHub · 2026-12-02 23:22 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3837#issuecomment-7361908006) Thank you both. Four commits are pushed on top of `f17ba72f`. The opt-in gate is unchanged. - `cde52a18b` startup: throttle pipeline library writes and compile outside the lock - `2e40b1d35` startup: detach the pipeline library on process exit - `7784dee76` startup: retry failed pipeline library writes and write the first at once - `135ea1b57` startup: smoke-test the pipeline library through guest Present **Behaviour change:** the library is first written at the first Present with a pending store, then at most every 15 s. Process exit no longer writes, so stores made after the last write are compiled again by the next run. By review point: - **MUST: detach at process exit.** When `d3d12_process_terminating` is set, `Shutdown` clears `library_`, `blob_` and `device_` and returns before any `Serialize`, file write or `Release` (`src/startup_stubs/graphics_pipeline_cache.h:311-316`). This is the `d3d12_teardown.h` boundary. A device change or FreeLibrary still writes the pending stores and releases (`:319-335`). - **SHOULD: throttle the Present-thread write.** `flush_pipeline_cache` calls `FlushIfDue(GetTickCount64())` (`src/startup_stubs/d3d12_draw.inl:1622`). - Every attempt starts a new 15 s interval (`graphics_pipeline_cache.h:486-521`, `persist_interval_ms` at `:30`). - The present thread only tries both locks (`:494-495`), so it never waits for a draw. - The library is serialized into one buffer behind the header slot. The checksum and the file write run after `lock_` is released (`:510-511`). - **SHOULD: compile outside `lock_`.** `GetOrCreateGraphicsPipeline` takes `lock_` for `LoadGraphicsPipeline` (`:348`), releases it before `CreateGraphicsPipelineState` (`:381`) and takes it again only for `StorePipeline` (`:389`). Two threads that miss the same name both compile, and the second store is counted as a duplicate. - **NIT: `dirty_` on failure.** A failed `Serialize` keeps the stores pending. A failed file replace marks them pending again, and the next due Present retries them without a new store (`:507-518`). - **NIT: "file present".** The log prints `file loaded` (a valid file was read), `file new` (the library starts empty) or `file unset` (`d3d12_draw.inl:1609-1612`, `loaded_from_file` at `graphics_pipeline_cache.h:285`). - **NIT: PR body.** Updated for the switch, the write schedule and the tests. Tests: - **Present smoke test.** `tests/startup_pso_library_present_check.py`. `startup_stream_depth` takes a new `present` argument that calls guest Present (queue slot 32) after its fourteen cases (`tests/startup_stream_depth.cpp:219,456`). Three runs share one cache directory: - Switch unset: correct readbacks, `cache off` on every pipeline, no `pso_*.lib`. - Switch set, cold: one non-empty library file exists after the run. The debug layer may report id 971, and nothing else, in this run only. - Switch set, fresh process: `file loaded`, one hit per pipeline created in the cold run, no miss, no debug-layer message. All fourteen readbacks are byte-identical to the unset run, and the library file is unchanged. - **Registration.** `tests/cmake/startup_pso_cache.cmake` registers `startup_pso_cache` (moved from `CMakeLists.txt`) and `startup_pso_library_present_{warp,hardware}`. The `pr` test preset runs `startup_pso_cache` and `startup_pso_library_present_warp`, and the `pr` build preset builds `startup_pso_cache_check` and `startup_stream_depth`. - **Fixture.** `startup_pso_cache` also checks the first write, the interval, and a write blocked by a directory in place of the file, retried once the directory is removed (`tests/startup_pso_cache_check.cpp:635-714`). Validation: Windows, MSVC, head `135ea1b57`. `startup_pso_cache`, `startup_pso_library_present_warp` and `startup_pso_library_present_hardware` passed (3/3). Not covered: no test fails if the detach is removed. The smoke test only shows that all its processes exit with code 0 with the switch set. The frame cost of a write on a physical adapter is still not measured.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-02 23:30 UTC · original · requested changes

I independently built and ran ctest --preset dev --no-tests=error -j 1 -V -R '^(startup_pso_cache|startup_pso_library_present_(warp|hardware))$' on 135ea1b57: 3/3 passed, no skips. Inbox WARP and RTX 4060 Laptop both produced 14 byte-identical readbacks and seven warm hits in a fresh process with the debug layer enabled. The process-exit branch now avoids Serialize and Release.

The new Present path can still block on draw-side work in two places. pipeline_cache_for(device, false) takes the ordinary mutex while cache creation/replacement uses that same mutex, so Present can wait for library creation or the old cache's final write. In Persist(false), a file replacement failure takes lock_ with AcquireSRWLockExclusive after releasing the first try-lock, so a concurrent LoadGraphicsPipeline or StorePipeline may also delay Present on the failure path. Please use a nonblocking lookup for Present and preserve the failed-write retry without a blocking reacquire on that path. The existing failure retry case is a good baseline for the change.

The current main merge conflicts only in CMakePresets.json. Keep mesh57_integer_alu and native_heap_token_texture, then add the two PSO test selections and their build targets.

**@⁠R0Y00** on GitHub · 2026-12-02 23:30 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6160945564) · **requested changes** I independently built and ran `ctest --preset dev --no-tests=error -j 1 -V -R '^(startup_pso_cache|startup_pso_library_present_(warp|hardware))$'` on `135ea1b57`: 3/3 passed, no skips. Inbox WARP and RTX 4060 Laptop both produced 14 byte-identical readbacks and seven warm hits in a fresh process with the debug layer enabled. The process-exit branch now avoids Serialize and Release. The new Present path can still block on draw-side work in two places. `pipeline_cache_for(device, false)` takes the ordinary mutex while cache creation/replacement uses that same mutex, so Present can wait for library creation or the old cache's final write. In `Persist(false)`, a file replacement failure takes `lock_` with `AcquireSRWLockExclusive` after releasing the first try-lock, so a concurrent `LoadGraphicsPipeline` or `StorePipeline` may also delay Present on the failure path. Please use a nonblocking lookup for Present and preserve the failed-write retry without a blocking reacquire on that path. The existing failure retry case is a good baseline for the change. The current main merge conflicts only in `CMakePresets.json`. Keep `mesh57_integer_alu` and `native_heap_token_texture`, then add the two PSO test selections and their build targets.
Author
Owner

@⁠R0Y00 on GitHub · 2026-12-03 07:14 UTC · original

I independently built and ran startup_pso_cache_check with MSVC against 135ea1b57. The WARP fixture passed, including cold store, warm load, corrupted/unsupported fallback, throttling and retry. This is the cache helper fixture. The runtime Present/fresh-process proof still needs the rebased head: against main at 29bb2aed, Git conflicts in CMakePresets.json and d3d12_draw.inl. In the latter, please keep #3876’s candidate-failure injection and same-process retry outside the library load path, and derive the library key from the VS/PS blobs of the translation actually used for each attempt. That preserves the disk fallback when the candidate fails.

**@⁠R0Y00** on GitHub · 2026-12-03 07:14 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3837#issuecomment-7370756466) I independently built and ran `startup_pso_cache_check` with MSVC against `135ea1b57`. The WARP fixture passed, including cold store, warm load, corrupted/unsupported fallback, throttling and retry. This is the cache helper fixture. The runtime Present/fresh-process proof still needs the rebased head: against `main` at `29bb2aed`, Git conflicts in `CMakePresets.json` and `d3d12_draw.inl`. In the latter, please keep #3876’s candidate-failure injection and same-process retry outside the library load path, and derive the library key from the VS/PS blobs of the translation actually used for each attempt. That preserves the disk fallback when the candidate fails.
This pull request has changes conflicting with the target branch.
  • CMakePresets.json
  • src/startup_stubs/d3d12_draw.inl
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3839/head:github-bot/gh-3837'
git switch 'github-bot/gh-3837'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3837'
git switch 'github-bot/gh-3837'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3837'
git switch 'github-bot/gh-3837'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3837'
git switch 'main'
git merge --squash 'github-bot/gh-3837'
git switch 'main'
git merge --ff-only 'github-bot/gh-3837'
git switch 'main'
git merge 'github-bot/gh-3837'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.