startup: cache translated graphics pipelines #3839
No reviewers
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows!3839
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3837"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@Leyukaka on GitHub · 2026-12-02 08:12 UTC · original
Leyukaka:takeover/pso-cache-1641→main· +1585 −6 · 9 files · 10 commits · openTakes 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
mainare 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_CACHEstores 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=1andX2W_SHADER_CACHE. With the switch unset, nothing constructs the library or hashes the key, andpipeline_forcallsCreateGraphicsPipelineStateexactly asmaindoes.From #1641 (see its description for the full design and the review history):
src/startup_stubs/graphics_pipeline_cache.h: anID3D12PipelineLibrarycache inX2W_SHADER_CACHE\pso_<adapter>.lib, with a versioned header, a checksum and an adapter/driver identity. The file is replaced atomically.pipeline_fortriesLoadGraphicsPipelinefirst and falls back toCreateGraphicsPipelineStateon a miss, a corrupt or truncated file, a driver or adapter mismatch, or a missingID3D12PipelineLibrary. A cache failure never fails a draw.Added here:
1dd7a9deb: the library gets its own name, distinct from the in-processpso_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): theX2W_D3D12_PSO_LIBRARY=1opt-in gate.cde52a18b,7784dee76: the write schedule.file loaded,file neworfile unset.2e40b1d35: on process exit the cache follows thed3d12_teardown.hboundary. 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 totests/cmake/startup_pso_cache.cmake, and theprpreset entries.As #1641 states, the implementation uses only the public Microsoft
ID3D12PipelineLibraryAPI.Validation
Windows, MSVC, head
135ea1b57.Result:
startup_pso_cache,startup_pso_library_present_warpandstartup_pso_library_present_hardwarepassed (3/3).startup_pso_cache(WARP) covers:startup_pso_library_present_{warp,hardware}runs thestartup_stream_depthdraws plus one guest Present, three times on one cache directory:prpreset runsstartup_pso_cacheandstartup_pso_library_present_warp.Known limits
X2W_PSO_RECIPE_DIR) are still created directly and bypass the cache.@lukaslw-lab on GitHub · 2026-12-02 08:25 UTC · original · requested changes
Reviewed
1dd7a9deby 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.
X2W_D3D12_PSO_LIBRARY=1) until it is measured on a physical adapter.run.ps1 -Renderalways setsX2W_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.PersistLibraryfromqueue_present(d3d12_xs_device.cpp:8923):GetSerializedSize+Serializeof the whole library, two heap copies of it (payload + header copy, up to 2 × 256 MB), thenFlushFileBuffersandMOVEFILE_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.CreateGraphicsPipelineState.GetOrCreateGraphicsPipelinetakeslock_exclusively for the whole driver compile (graphics_pipeline_cache.h:319), andFlushtakes it for the whole write.pipeline_forruns underdraw_lock, so a flush stalls the draw path, and concurrent pipeline creation is serialized. Locking only aroundLoadGraphicsPipelineandStorePipelineavoids both.tests/cmake/startup_pso_cache.cmake(pertests/cmake/README.md) rather than inCMakeLists.txt.startup_pso_cacheis also not in theprpreset filter, so CI does not run it.LoadGraphicsPipelinevalidates the desc, and a duplicateStorePipelineis counted and kept out.shared_ptrkeeps an old cache alive through a concurrent flush after a device change.pso_state_keystays the in-process key.@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$'on1dd7a9dewith 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 21:07 UTC · original · reviewed
Additional independent hardware result on
1dd7a9de: the unmodified WARP fixture passed. In a local copy oftests/startup_pso_cache_check.cpp, I selected the RTX 4060 Laptop (32.0.16.1656, Windows 11 build 26300) viacreate_device(false, unmatched). The fixed color oracle then reportedff407fffversus WARP'sff4080ff. 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.c3a8543333' into HEAD ce94960334@R0Y00 on GitHub · 2026-12-02 21:33 UTC · original · reviewed
I pushed
f17ba72fto address the opt-in request. The runtime now requiresX2W_D3D12_PSO_LIBRARY=1before it constructs the library or hashes the on-disk key. With MSVC Debug on Windows 11 build 26300, a localstartup_stream_depthvariant 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 on GitHub · 2026-12-02 21:57 UTC · original
Thank you for the gate and for the runs. I re-reviewed
f17ba72fby reading the diff; I have not built or run this head.Verdict: the gate is correct. With
X2W_D3D12_PSO_LIBRARYunset, nothing constructs the library or hashes its key:graphics_pipeline_library_enabled()(src/startup_stubs/d3d12_draw.inl:161) readsx2w_local::env_ononce. It parses like the other switches: the value must start with1.load_translationskipstranslated_code_key(d3d12_draw.inl:850).pipeline_cache_forreturns before its mutex (d3d12_draw.inl:1595). The only construction site ofGraphicsPipelineCacheis behind it (:1604), so the file is never opened.flush_pipeline_cache, called fromqueue_present(d3d12_xs_device.cpp:9102), returns immediately (d3d12_draw.inl:1618).pipeline_forcallsCreateGraphicsPipelineStatedirectly, as main does (d3d12_draw.inl:1877-1889). The only visible difference is the, cache offsuffix in the log line.tests/native_rtv_variant_check.py:303matches a prefix of that line.Non-blocking:
startup_pso_cachecalls the helper directly, so it does not pass through the gate.docs/startup-runtime.md:1994but has no row in the environment table.CMakeLists.txt:1392, not intests/cmake/, and is not in theprfilter.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:cde52a18bstartup: throttle pipeline library writes and compile outside the lockCreateGraphicsPipelineStaterun outside the cache lock.startup_pso_cachegains a throttle check.2e40b1d35startup: detach the pipeline library on process exitd3d12_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 runstartup_pso_cacheon 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 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 undertests/cmake/and add the applicable WARP case to theprpreset.@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.
X2W_D3D12_PSO_LIBRARYunset, nothing constructs the library, hashes the code key or flushes, andpipeline_forcreates the PSO directly.static std::shared_ptrinpipeline_cache_forrunsShutdown(serialize, write, release) in this module's CRT teardown.d3d12_teardown.hdocuments why the other caches release nothing at that point (0xC0000409 through the AMD driver). This is by reading: the twostartup_stream_depthprocesses below exited with code 0 with the cache on.startup_pso_cacheone flush after eight small PSOs took 17.1 and 13.4 ms here (WARP, two runs), before any title-sized library.lock_.GetOrCreateGraphicsPipelinestill holds it acrossCreateGraphicsPipelineState.Flushclearsdirty_even whenPersistLibraryfails, so those entries are not retried until the next store.file presentwhenever the directory is set; it printsconfigured(), not whether the file exists.X2W_SHADER_CACHE.Run: Windows 10 19045, RTX 3080 Ti (32.0.16.1047) and WARP 10.0.19041.7811, MSVC Release, full build.
=1startup_pso_cachestartup_frame_copy_{warp,hardware}startup_stream_depth_{warp,hardware}native_rtv_variant_{warp,hardware}startup_stream_depthfails only becausestartup_stream_depth_check.py:71rejects 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_warpfails 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.
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.@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.cde52a18bstartup: throttle pipeline library writes and compile outside the lock2e40b1d35startup: detach the pipeline library on process exit7784dee76startup: retry failed pipeline library writes and write the first at once135ea1b57startup: smoke-test the pipeline library through guest PresentBehaviour 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:
d3d12_process_terminatingis set,Shutdownclearslibrary_,blob_anddevice_and returns before anySerialize, file write orRelease(src/startup_stubs/graphics_pipeline_cache.h:311-316). This is thed3d12_teardown.hboundary. A device change or FreeLibrary still writes the pending stores and releases (:319-335).flush_pipeline_cachecallsFlushIfDue(GetTickCount64())(src/startup_stubs/d3d12_draw.inl:1622).graphics_pipeline_cache.h:486-521,persist_interval_msat:30).:494-495), so it never waits for a draw.lock_is released (:510-511).lock_.GetOrCreateGraphicsPipelinetakeslock_forLoadGraphicsPipeline(:348), releases it beforeCreateGraphicsPipelineState(:381) and takes it again only forStorePipeline(:389). Two threads that miss the same name both compile, and the second store is counted as a duplicate.dirty_on failure. A failedSerializekeeps the stores pending. A failed file replace marks them pending again, and the next due Present retries them without a new store (:507-518).file loaded(a valid file was read),file new(the library starts empty) orfile unset(d3d12_draw.inl:1609-1612,loaded_from_fileatgraphics_pipeline_cache.h:285).Tests:
tests/startup_pso_library_present_check.py.startup_stream_depthtakes a newpresentargument that calls guest Present (queue slot 32) after its fourteen cases (tests/startup_stream_depth.cpp:219,456). Three runs share one cache directory:cache offon every pipeline, nopso_*.lib.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.tests/cmake/startup_pso_cache.cmakeregistersstartup_pso_cache(moved fromCMakeLists.txt) andstartup_pso_library_present_{warp,hardware}. Theprtest preset runsstartup_pso_cacheandstartup_pso_library_present_warp, and theprbuild preset buildsstartup_pso_cache_checkandstartup_stream_depth.startup_pso_cachealso 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_warpandstartup_pso_library_present_hardwarepassed (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 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))$'on135ea1b57: 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. InPersist(false), a file replacement failure takeslock_withAcquireSRWLockExclusiveafter releasing the first try-lock, so a concurrentLoadGraphicsPipelineorStorePipelinemay 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. Keepmesh57_integer_aluandnative_heap_token_texture, then add the two PSO test selections and their build targets.@R0Y00 on GitHub · 2026-12-03 07:14 UTC · original
I independently built and ran
startup_pso_cache_checkwith MSVC against135ea1b57. 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: againstmainat29bb2aed, Git conflicts inCMakePresets.jsonandd3d12_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.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.Merge
Merge the changes and update on Forgejo.