graphics: Async shader compile #2119
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!2119
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2119"
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?
@newblord on GitHub · 2026-11-05 00:11 UTC · original
newblord:async_shader_compile→main· +1085 −54 · 12 files · 5 commits · openSummary
Shaders and pipelines are now built on worker threads, so the render thread no longer stops for them. A draw whose shaders or pipeline are not ready yet is skipped, and appears a moment later once they are. Before, every new shader or pipeline was translated and compiled on the render thread while the game waited, which showed as multi-second freezes on a new scene and long stutters when a game streams in new content.
It is on by default. --no-async-shader-compile restores the old behaviour (async_shader_compile_enabled in the config).
The last commit is a separate fix found while testing this: a draw whose render area is larger than the image behind its attachment writes past the image, and on NVIDIA that ends in a GPU timeout. The render area and layer count are now bounded to the real image.
What changed
Worker pool (pipelineAsync.cpp/.h, new). Translate a shader, finish it into a module and create the pipeline as separate jobs on Common::ParallelThreads() workers, at below-normal priority so the game’s own threads go first. The render thread applies finished work at the start of each draw’s program lookup.
Scheduling. A worker takes the job a draw asked for most recently, so a pipeline drawn every frame cannot starve behind the new ones each frame brings. Worker 0 takes the oldest. Jobs no draw has asked for in 2 s go to two workers only. Jobs that make a draw drawable go before translations.
When a draw may be skipped. Only when everything it draws into was drawn in at least three of the last six frames, or when it is a G-buffer pass. Anything else (a picture drawn once, a small target, a draw whose shaders write memory others may read) is still built on the render thread, as before. Compute is always built on the render thread.
Presenter. A frame that skipped draws for shaders is held back (the previous complete frame stays on screen) for at most 3 s, then shown anyway.
Status. Every 2 s while anything is pending, one console line shows what the workers are doing and how many draws were skipped. When the render thread still had to build something itself, a second line says how long and why, so the freezes that remain can be found.
Render-area guard (renderDraw.cpp, last commit). The render area is bounded to the smaller of the cache’s image extent and the extent really allocated, and the layer count to the allocated layers. Each clamp prints one console line (first 32 of each kind).
The worker scheduling follows the pipeline compiler in usa-ratt/KytyPS5-GT7, and the render-area bound is based on its 15cddeb.
@coderabbitai[bot] on GitHub · 2026-11-05 00:27 UTC · original · edited 2026-11-05 00:28 UTC
Walkthrough
The changes add configurable, worker-based shader and graphics-pipeline compilation. The pipeline cache requests asynchronous work for eligible stages and draws, and the render thread applies completed work. Shader-skipped draws are tracked, and presentation can hold the previous frame while a flip group is incomplete. Render attachment extents and layer counts are limited to backing allocations. Driver-cache saves run in the background.
Suggested reviewers:
nmzik,shadowbeat070Change: Feature
Merge Risk:
Low · up to
22c02Async compilation can omit shader memory updates, causing stale graphics data. Multi-port frame counting and size thresholds also reduce deferral, while shared pending pipelines can busy-wait. These bounded issues warrant fixes or explicit acceptance before merging.
Pre-merge checks:
4 ·
1
Full details: Title check
Explanation
The title "graphics: Async shader compile" matches the main change. The PR moves shader translation, module compilation, and graphics-pipeline creation onto worker threads, and adds the async_shader_compile_enabled option with a --no-async-shader-compile flag. The title is short and specific, and it follows the subsystem-prefix style.
Full details: Description check
Explanation
The description directly explains the asynchronous shader and pipeline compilation changes, draw-skipping behavior, configuration option, presenter behavior, scheduling, status reporting, and render-area bounds.
Full details: Docstring Coverage
Explanation
Docstring coverage is 23.08% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 52 functions across 12 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
@ -856,0 +978,4 @@rt.attrib2.height < SmallTargetSyncPixels;large_targets += rt.attrib2.width >= GbufferMinPixels && rt.attrib2.height >= GbufferMinPixels? 1u: 0u;@coderabbitai[bot] on GitHub · 2026-11-05 00:27 UTC · original
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4528
Convert
attrib2to pixel counts before the threshold checks.CB_COLOR_ATTRIB2stores width and height minus one.LogMrtStateinrenderDraw.cppadds one before it prints the size. The checks here compare the raw fields with pixel counts, so each threshold is off by one:attrib2.width == 255. It fails>= GbufferMinPixels, sogbuffer_passis false. The first frames of that pass are not deferred, even though the comment says a G-buffer pass is skipped from its first frame.< SmallTargetSyncPixelsas 1023. It is marked as small, which gives the wrong stall reason.all_smallaffects only that reason. It does not decide whether a draw is skipped.Keep the strict
<operator for the small-target check. With the one-unit correction, a 1024-pixel target is not small, which matches the below-1024 threshold.Also update the header comment at lines 52-53. Change "up to this many pixels" to "below this many pixels." Remove "never skipped," because small targets follow the redrawn rule like other targets.
🐛 Proposed fix
Committable suggestion
@ -859,0 +1009,4 @@const auto* pixel_module = GetProgram(pixel_params, pixel_info, push_data_cursor, mode);if (pixel_module == nullptr) {EXIT_IF(m_async == nullptr);ready = false;@coderabbitai[bot] on GitHub · 2026-11-05 00:27 UTC · original
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 10315
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 20513
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 30489
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 16223
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8380
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 1382
Do not skip a graphics draw before its shader write safety is known.
state.deferrableenablesAsyncMode::Onfrom redraw history or the G-buffer rule. If a stage is unavailable,GetGraphicsProgramsclears the vertex module and returns beforeEnsureGraphicsPipeline, soWritesMemorycannot inspect that stage. The skipped draw is not replayed, andResetBindings()only clears bindings. A buffer, image, or address write can therefore be missing from the current command stream, causing later graphics work to use stale data.At the shared deferral boundary, use retained write metadata when it is complete. Use synchronous compilation whenever the program or its write metadata is unknown. Apply this decision before any graphics stage may return
nullptrfor a deferrable draw.@ -1046,0 +1234,4 @@while (pipeline.pending) {PumpAsync();std::this_thread::yield();}@coderabbitai[bot] on GitHub · 2026-11-05 00:27 UTC · original
The render thread can busy-wait on a pending worker pipeline. The comment says this cannot happen.
GraphicsPipelineKeydoes not include the render-target addresses. Consider this sequence:Kon a worker.state.deferrable == false) uses the same shaders and state on a target that is not redrawn.EnsureGraphicsPipelinereturns true at once for that draw (pipelineAsync.cppLine 364-366).GetGraphicsPipelinefindspipeline.pendingand spins withPumpAsync()andyield()until a below-normal-priority worker runs the job.This wait can be long because nothing refreshes the job's priority.
EnsureGraphicsPipelinedrops theJobPtrthatPostreturns and never callsTouch()on it. Two effects follow:StaleAfterMs, only the firstBackgroundWorkersworkers take it.During that time, the render thread keeps one core busy.
Fix: store the
JobPtrfor pending pipelines, for example inAsynckeyed by the pipeline key, and callTouch()on it each time a draw checks it. In this loop, take the job from the queue and run it inline if it has not started yet. Update the comment either way.@ -947,1 +953,4 @@void FlipQueue::Prepare(uint64_t request_id, Graphics::CommandBuffer& buffer) {// The draws recorded before this flip are the ones its frame is made of.const bool incomplete = Graphics::TakeDrawsSkippedForShaders();Graphics::NoteFrameFlipped();@coderabbitai[bot] on GitHub · 2026-11-05 00:27 UTC · original
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 2810
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11042
Advance the frame counter once per resolved flip group.
Keep
Graphics::TakeDrawsSkippedForShaders()per request. Remove the unconditionalGraphics::NoteFrameFlipped()call. Set the owner flag only after a request is found, then increment after the stale-generation cancellation check only forrequest->id == request->group.Suggested fix
@coderabbitai[bot] on GitHub · 2026-11-05 00:27 UTC · original · reviewed
Actionable comments posted: 4
9b27a0be-5b7c-47ef-b4b6-5e4d645ef825Reviewing files that changed from the base of the PR and between
07e87b950aand22c0267240.src/common/emulatorConfig.cppsrc/common/emulatorConfig.hsrc/common/threads.cppsrc/common/threads.hsrc/graphics/host_gpu/renderer/pipeline/pipelineAsync.cppsrc/graphics/host_gpu/renderer/pipeline/pipelineAsync.hsrc/graphics/host_gpu/renderer/pipeline/pipelineCache.cppsrc/graphics/host_gpu/renderer/pipeline/pipelineCache.hsrc/graphics/host_gpu/renderer/pipeline/skippedDraws.hsrc/graphics/host_gpu/renderer/renderDraw.cppsrc/graphics/presentation/videoOut.cppsrc/main.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@ZrDaniel07 on GitHub · 2026-11-16 01:18 UTC · original
Test report on NVIDIA / Windows with Gran Turismo 7 (PPSA01317 v01.680).
Setup. Windows 11 Pro 26300, RTX 5090 (driver 621.24), Ryzen 9 5900X (12C/24T), 32 GB RAM, clang-cl Release. This PR's head
22c02672merged intomain41e2147c(plus #1748, which all my GT7 test builds carry, and local timing logs). The merge conflicted only inpipelineCache.cpp/.h: I kept main's driver-cache save on the store thread and passedDrawTargetsthroughMakeGraphicsPipelineKey/EnsureGraphicsPipeline/GetGraphicsPipeline, since main no longer carries the targets inDrawRenderState.m_driver_cache_dirtybecamemutable std::atomic<bool>because it is now set from a const path. Unit tests were not run.All three runs start with an empty shader store (every rebuild discards it, see #2178), from the same save, into the Music Rally challenge. A and B are the same
mainbuild without this PR; B addsVK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BITto check whether driver optimization is the cost.The runs did not follow the exact same path: in A the menus were so slow that I quit before the race, B reached the race at 0.5–3 FPS, and C got through several stages of it. Subjectively, the race in C felt about ten times more responsive than in the earlier runs. Without this PR, the slow pipelines are mostly large pixel shaders (median ~59k SPIR-V words), and the driver compile time follows the module size. Disabling driver optimization changed it by only ~6% (B), so moving the compile off the render thread is what helps here.
What still blocked the render thread in C (from the PR's status lines): 61 s in total.
About 34k draws were skipped over the run. I saw no rendering corruption that I could tie to the skipped draws, and no GPU timeout, but I did not look for them specifically. System memory reached "Critical" in the overlay once (28.8 / 31.9 GB) during the race. I did not check whether the extra worker memory contributes to that.
Happy to rerun with a different skip rule or extra logging if that would help.
This comment was written with the help of an AI assistant (Claude); the tests were run by me and the numbers come from the emulator's logs.
@ZrDaniel07 on GitHub · 2026-11-21 04:38 UTC · original
Follow-up to my GT7 report above, with a correction: skipped draws do corrupt the image in GT7 races, and with
--no-async-shader-compilethe corruption goes away.Setup. Same PC as before (Windows 11 Pro 26300, RTX 5090 driver 621.24, Ryzen 9 5900X, 32 GB). This PR's head
22c02672merged intomaina21fd80c(2026-11-20), plus #1748 and local timing logs. New conflicts against thismain:threads.h/.cpp, where I kept both main'sReserveCoreForCurrentThread()(froma21fd80c) and this PR'sLowerPriority(), andpipelineCache.cpp, where I kept this PR'sCompileStage, which readsoptions.profile. Unit tests were not run.All three runs follow the same path: the opening "Welcome to Gran Turismo 7" sequence, the Music Rally menu, then the Music Rally race in cockpit view.
--no-async-shader-compile, warm storeThe corruption. In the race, trees, water and the track surface look stretched and smeared, and parts of the scene show stale content from earlier frames, so you cannot read the road ahead. The opening sequence and menus were clean in run 2, once the store held their pipelines. Run 3 uses the same build and the same store with
--no-async-shader-compile, and the race renders correctly. So the corruption comes from skipping, not frommainor #1748. In my first report I wrote that I had not tied any corruption to skipped draws; that run never got far enough into the race to show it.Why the race keeps skipping, even with a warm store. The store works: run 3 loaded 2079 pipelines, and the opening and menus ran at 60 FPS with nothing to compile. But GT7 keeps reaching new shader combinations as the car goes further. Of the 333 slow pipelines in run 2, only 1 had appeared in run 1; of the 137 in run 3, none had appeared in earlier runs. Each run got further down the track, and each new stretch brought new pipelines. Without this PR, those pipelines (median 691 ms) block the render thread: near the second corner of the race there were 20-second windows with 15–21 s of compilation, and long freezes.
What still blocked the render thread in run 1 (from the PR's status lines): 271 s, against 61 s in my first report. Of that, 136 s was "targets not yet redrawn", 110 s "small targets" (4.2 s before), and 25 s compute. This run went further than the first one (into the cockpit race), so part of that is new content. I have not checked why "small targets" grew so much.
Separate from this PR. The race runs at about 7 FPS in all three runs, even in stretches where nothing compiles (about 4400 draws and 1300–2000 dispatches per frame), so that limit is elsewhere.
A question about the skip rule. In a race, stale content on the track is as bad as a freeze, because the player cannot see where to go. Would it make sense to skip only when the draw is small or off to the side, and to build on the render thread for draws into the main scene targets? Or to cap how long a draw may stay skipped before falling back to a synchronous build? I can rerun the same three-step path with either change, or with extra logging that shows which targets the skipped draws write to.
This comment was written with the help of an AI assistant (Claude); the tests were run by me and the numbers come from the emulator's logs.
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.