startup: cache translated graphics pipelines #3837

Open
Leyukaka wants to merge 10 commits from upstream-pr/3837 into main
Leyukaka commented 2026-12-02 08:12:17 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-12-02T08:12:17Z.


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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5602487040 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3837). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-12-02T08:12:17Z. --- 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.
lukaslw-lab commented 2026-12-02 08:25:50 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-12-02T08:25:50Z.

Review: CHANGES_REQUESTED


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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6147095187 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6147095187). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-12-02T08:25:50Z. **Review:** CHANGES_REQUESTED --- 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.
R0Y00 commented 2026-12-02 19:48:37 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T19:48:37Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6157767552 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6157767552). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T19:48:37Z. **Review:** COMMENTED --- 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 commented 2026-12-02 21:07:28 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T21:07:28Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6159027768 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6159027768). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T21:07:28Z. **Review:** COMMENTED --- 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 commented 2026-12-02 21:33:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T21:33:44Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6159407125 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6159407125). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T21:33:44Z. **Review:** COMMENTED --- 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.
Leyukaka commented 2026-12-02 21:57:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-12-02T21:57:48Z.


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?

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7360031062 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3837#issuecomment-7360031062). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-12-02T21:57:48Z. --- 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?
R0Y00 commented 2026-12-02 22:44:30 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T22:44:30Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7361098701 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3837#issuecomment-7361098701). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T22:44:30Z. --- 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.
lukaslw-lab commented 2026-12-02 22:47:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-12-02T22:47:46Z.

Review: CHANGES_REQUESTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6160406637 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6160406637). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-12-02T22:47:46Z. **Review:** CHANGES_REQUESTED --- 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.
Leyukaka commented 2026-12-02 23:22:14 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-12-02T23:22:14Z.


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7361908006 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3837#issuecomment-7361908006). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-12-02T23:22:14Z. --- 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.
R0Y00 commented 2026-12-02 23:30:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-02T23:30:58Z.

Review: CHANGES_REQUESTED


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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6160945564 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3837#pullrequestreview-6160945564). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-02T23:30:58Z. **Review:** CHANGES_REQUESTED --- 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 commented 2026-12-03 07:14:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-03T07:14:20Z.


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 #3874’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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7370756466 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3837#issuecomment-7370756466). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-03T07:14:20Z. --- 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 #3874’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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3837:upstream-pr/3837'
git switch 'upstream-pr/3837'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3837'
git switch 'upstream-pr/3837'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3837'
git switch 'upstream-pr/3837'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3837'
git switch 'main'
git merge --squash 'upstream-pr/3837'
git switch 'main'
git merge --ff-only 'upstream-pr/3837'
git switch 'main'
git merge 'upstream-pr/3837'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!3837
No description provided.