fix(agc): store linear compute writes behind their dispatch #6948
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6948"
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?
Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-16T20:50:38Z.
What
Kena: Bridge of Spirits (PPSA01802): terrain, the player character and props pop in and out every few frames while the camera turns, and the sky and fog show through the ground where terrain is missing.
UE4 tests its hierarchical-Z occlusion on the GPU into a 256x256 tiled target, then a compute shader (
0x2006e70000in Kena) copies the results into a 256x256 linear image (format 60) that the CPU reads after the frame's fence to decide which primitives to draw next frame. That linear image stayed pending as a storage image: the flip-time store (StoreSmallPending) skips surfaces over 64 KiB (this one is 256 KiB), and it runs after the fence the CPU waits on anyway. So the CPU read stale or empty occlusion results and culled visible primitives.Before a label or an end-of-pipe interrupt the CPU may wait on, every pending linear storage image and every pending image of at most
SmallStoreBytes(64 KiB, any tile mode) is stored to guest memory (StorageTexture::StoreReadbackBeforeLabel, aFlushPendingover each one). The store is recorded in GPU order before the label, so the CPU sees the results once the fence passes. Only images the GPU wrote are pending, and an image is stored once per write. Larger tiled images are left alone.APS5_NO_LABEL_READBACK_STORE=1restores the old behaviour.The same pattern on a small tiled image, reported by Pacuka: The Smurfs - Dreams (PPSA21607) computes the sky light's average brightness into a 1x1 RGBA16F R64KBX storage image and reads it on the CPU after a label. The image stayed pending, the CPU read 0, and the Smurfs' Village rendered washed out (specular about 100x too strong).
Tested
AMD Radeon RX 9070 XT (RADV GFX1201), Mesa 26.2.4, Arch Linux (kernel 7.2.9), Kena: Bridge of Spirits (PPSA01802).
agc_driver_linear_readback_label: a PM4 dispatch stores into a linear 32x8 8_8_8_8_UINT image, a RELEASE_MEM label follows, the test polls the label and reads guest memory. A second case does the same with a 16x16 32_UINT R64KBX image (64 KiB); before the small-tiled change it failed with 0 of 8 texels stored. Both pass.agc_driver_linear_readback_label_offruns it withAPS5_NO_LABEL_READBACK_STORE=1and is expected to fail (texel (0, 0) reads the old bytes).main0d078f75d: with only linear images stored, the village is washed out (near-white); with small images too, it renders correctly. Village presents per 10 s are 100-105 vs 100-108. One of three runs with the change crashed during loading in game code (app.elf+0x290e36, a null table pointer read on a new thread); the other two reached the village. The cause is not known.main0d078f75d, no input,APS5_PROFILE_DRAW=1, 150 s, three alternating rounds, mean presents per 10 s of the last four windows: linear only 56.3 / 66.3 / 72.3, with small images 60.8 / 67.8 / 68.5 (within the spread). The flip-time stores (about 450 per 10 s) now happen at labels:readback before labelabout 740 per 10 s and 55 MiB, againstlinear before labelabout 130 andflipabout 450 (45 MiB together).mainf8b22edfb, no input,APS5_TRACE_SECONDS=100:30, three alternating rounds:main165 / 175 / 181 presents, this PR 176 / 155 / 169 (within the spread). About 2 stores per present, about 127 KiB each ([storage]line of anAPS5_PROFILE_DRAWrun:linear before label209/26.3 MiB per 10 s at 103 presents). Summary lines in the review reply.main0d078f75d+ this PR: 1439 of 1440 pass;agc_driver_tessellationsegfaults inside RADV (LinkPipelineFromLibraries) onmaintoo.Checklist
main; no other open PR implements the same functions (#6960 also stores pending images before a label, at the same two call sites, but only images over memory outside the write watch (!GuestMemory::Watched), for stores a CPU write could lose; images over watched memory stay pending there)NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-16T20:59:02Z.
Checked on plain main (
a7f108030+ this change only, no other local changes), Kena PPSA01802 from the same save, default settings:APS5_NO_LINEAR_READBACK_STORE=1(main behaviour): once in the forest, Kena herself, the Rot around her and nearby trunks are missing in every dumped frame for the rest of the run. The occlusion results the CPU reads are stale, so these primitives are culled.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-16T22:07:28Z.
Review: COMMENTED
The diagnosis is clear (a pending 256 KiB linear image the CPU reads after the fence). The fix does its work in the wrong place and the cost is unmeasured.
StoreLinearWritesruns after every compute dispatch, on both the inline path and the committer path. For each dispatch it walks everyGuestImagesbinding, decodes every 8-word descriptor withDecodeTextureResource, and usestry/catchas the filter for descriptors that don't decode. That is per-dispatch CPU work on the hottest path, in every title, to serve one shader. Do the decode once per compiled variant, not per dispatch.FlushPendingtoo, and a linear image written by several dispatches in a frame is stored after each of them. The recompile result knows which images a program stores to; restrict to those, and store once, before the label or fence the CPU waits on, rather than at each dispatch. #6960 addsStoreBeforeGuestCompletionfor the same moment, so these two should be one mechanism.FlushPendingover a 256 KiB surface after each dispatch records a store into the batch, so the number that matters is how many such dispatches a frame has and how large the images are. The description measures Kena at 24.3 vs 23.3 fps on one scene each, which is a 4% drop that the description reads as noise without showing the spread. Please post the summary lines of main and this PR from the same scene in PPSA01802 without anyAPS5_PROFILE_*, with a repeat of each, and the count ofStoreLinearReadbackcalls per frame.APS5_NO_LINEAR_READBACK_STORE=1) is what shows it.Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-17T10:06:44Z.
Thanks. Reworked in
931f7297b(pushed on top, no rebase):StoreLinearBeforeLabelruns at the start ofWriteLabelOnGpuand for an end-of-pipe interrupt without a label. Those are the moments #6960'sStoreBeforeGuestCompletionuses. It stores every pending linear storage image, recorded before the label.FlushPendingloop overPending(). #6960 stores pending results over compared (unwatched) memory; this stores pending linear images in either kind of memory. Whichever merges second can fold its condition into the other's loop. I didn't make this PR depend on #6960, so it can be reviewed on its own.agc_driver_linear_readback_labelsubmits a PM4 dispatch that stores into a linear 32x8 8_8_8_8_UINT image (format 60, as in Kena), then a RELEASE_MEM label. It polls the label and reads guest memory. It passes.agc_driver_linear_readback_label_offruns it withAPS5_NO_LINEAR_READBACK_STORE=1and is markedWILL_FAIL: there texel (0, 0) still holds the bytes from before the dispatch.Measurement on Kena (PPSA01802), forest title scene, no input.
mainf8b22edfbwith and without this PR, warm cache,APS5_TRACE_SECONDS=100:30and no other switch. Three rounds, alternating with other builds. Linux 7.2.9, RX 9070 XT, RADV Mesa 26.2.4, Release.mainThis is within the run-to-run spread. GPU busy per present is 3–5 ms higher (80–85 vs 86–88 ms). Store count from the
[storage]line of a separateAPS5_PROFILE_DRAW=1run:linear before label209 stores / 26.3 MiB per 10 s at 103 presents, so about 2 per present at about 127 KiB each.Summary lines
The traces are kept locally; I can attach any of them. The visual check (camera turn, terrain and character no longer dropping out) was done with the first, per-dispatch version. I haven't repeated it in gameplay with this version yet; the new test covers the ordering it relies on.
ctest -R '^agc_'onmain+ this PR: 621 of 622 pass.agc_driver_tessellationsegfaults onmaintoo.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by TheSovietWaffle on 2026-10-17T11:30:08Z.
Windows data point: head
931f7297bas-is, Release, Windows 10 Pro 22H2 (19045), WinLibs GCC 15.2.0 r7. Device chosen withANYPS5_GPU:agc_driver_linear_readback_labelagc_driver_linear_readback_label_off(WILL_FAIL)1002:164e), AMD Windows driverSo on both, the test detects the missing store and passes with it. An initial batch of 5 runs of the first test through my run script reported neither a pass nor any failure output; the 40 direct runs afterwards all passed, so I count that batch as a harness problem, not a test failure.
Not checked: other tests, titles.
AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-17T13:35:46Z.
Review: COMMENTED
NVIDIA and lavapipe runs, since the description has only RADV.
Machine: Ubuntu 26.04, i7-8700, GCC 15.2, Release,
BUILD_TESTING=ON. NVIDIA GeForce RTX 3070 (driver 595.99.02) and lavapipe (Mesa 26.0.3, LLVM 21.1.8) on the same host. PR merged ontomain12a71f3f5(clean).ANYPS5_NO_SHADER_CACHE=1,-j1 --timeout 30.agc_driver_linear_readback_label,_off,agc_driver_linear_target_resident,agc_driver_storage_cache_budget,_one_slice_alias,_write_back_overhangmainagc_driver_linear_readback_labelfails:texel (0, 0) read after the label is 0xefcaa580, the dispatch before the label stored a different value^agc(638 tests), PR vsmainagc_driver_f16_fma_denormal_modesandagc_driver_fma_mix_f16_denormal_modes, fail onmainon this GPU too)Not run: Windows, AMD, Kena or any title.
GPUs tested: NVIDIA GeForce RTX 3070 (595.99.02) and lavapipe (Mesa 26.0.3), Ubuntu 26.04
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-10-18T10:12:22Z.
@alfalfa-star a data point for this PR from The Smurfs - Dreams (PPSA21607, Unreal Engine), RX 6650 XT, RADV / Mesa 26.2.4.
The Smurfs' Village rendered washed out (pale, near-white). The cause is the same readback pattern as here, but on a tiled image. At level load the engine computes the sky light's average brightness on the GPU into a 1x1 RGBA16F (format 71), tile mode R64KBX storage image (
guestBytes0x10000) and reads it on the CPU after a label. The image stays pending, so the CPU reads 0.SkyIrradianceEnvironmentMap[7].xends up 0 in guest memory, and the reflection pass's static-sky lightmap mixing divides bymax(0, 1e-4), which makes specular ~100x too strong.What I checked:
APS5_STORE_AT_LABEL=1fixes the colors, but drops the village from ~6.5 to ~3.4 fps.StoreLinearBeforeLabelskips tiled images.agc_driver_linear_readback_labelstill passes:Would you consider covering small tiled images too, or would you rather have it as a separate follow-up PR? Happy to test either.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-18T13:06:31Z.
@Pacuka Thanks for tracking this down. I took your change into this PR (56d7ebe41, credited in the commit message) and rebased on
main0d078f75d:SmallStoreBytes. The function is renamedStoreReadbackBeforeLabel, and the opt-outAPS5_NO_LABEL_READBACK_STORE, since it no longer covers only linear images.agc_driver_linear_readback_labelhas a second case: a 16x16 32_UINT R64KBX image (64 KiB) stored by a dispatch, then a label. It failed before the change (0 of 8 texels) and passes now.Tested on the same RX 9070 XT / RADV 26.2.4:
agc_driver_tessellationsegfaults in RADV onmaintoo.One thing I can't explain yet: one of my three Smurfs runs with the change crashed during loading, before the main menu. It was a SIGSEGV in game code at
app.elf+0x290e36,mov (%rsi,%rcx,4),%ecxwithrsi = [rbx+0x30] = 0, on a thread that had just started. The other two runs reached the village. I made only one run without the change, so I can't tell whether it's related. Have you seen a crash there, with or without the change?#6960 adds a store at the same two call sites, but only for images over memory outside the write watch. Whichever lands second will need a small merge.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-10-18T16:47:25Z.
@alfalfa-star Thank you for taking it in and for the extra test case!
I haven't seen that crash. My Smurfs runs with the change (about a dozen today, on
9f25d0bf5+ this PR and later stacks with #4449 / #6208 / #6501 / #6617) all reached the menu and the village. A run on plainmaine5f18ea99from a new save this evening also loaded fine. Setup: RX 6650 XT / RADV Mesa 26.2.4, CachyOS.I'll run your head 56d7ebe41 a few times from a new save and report back whether the loading crash shows up here. If it does, I'll attach a backtrace of that thread. Is there anything else on this PR you'd like tested on RDNA2?
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-10-18T16:52:57Z.
@alfalfa-star Ran your head 56d7ebe41 (no other PRs) three times, each from a new save (save data removed before every run), warm shader cache. RX 6650 XT / RADV Mesa 26.2.4, CachyOS, Release.
All three runs got through loading, the title screen, the New Save dialog and the Solo / Co-op menu into the Smurfs' Village, and exited cleanly when closed. No SIGSEGV or other crash, so I couldn't reproduce the loading crash here. The intro was skipped in at least one run.
The village is clearly brighter than on plain
main, where it is almost fully black. On this GPU it still has black areas and a black menu background, but that is the separate 8-layer mesh-shader limit (#6528), which this build doesn't include.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T14:59:52Z.
Review: CHANGES_REQUESTED
The full Build fails
agc_driver_clamped_global_fillon linux (run 39267846472); it passes on main.ClampedGlobalFill.cpp:229(KeyFillOrderTests) waits for a label and then expects the 64 KiB surface (SurfaceBytes = 65536) to still be pending under its keys.StoreReadbackBeforeLabel(Texture.cpp:3963) stores every pending image withguestBytes <= SmallStoreBytesbefore the label, so it is no longer pending: "the surface written after the key fill has no pending image under its keys".APS5_NO_LABEL_READBACK_STORE=1, or make its surface larger thanSmallStoreBytes. Please also checkDccKeysFirstWrite.cpp, which has the same "pending image under its keys" assertion, and rerun everyagc_driver_*test that polls a label and then callsFindPending.Imported from boykopovar/AnyPS5. Originally posted by Pacuka on 2026-10-19T16:51:31Z.
@alfalfa-star I reproduced the
agc_driver_clamped_global_fillfailure from the review locally and tried both of the suggested fixes. Setup: RX 6650 XT, RADV Mesa 26.2.4, CachyOS, Release. Build: this PR's head56d7ebe41merged ontomain32004dfa5, the way CI tests it.the surface written after the key fill has no pending image under its keys. The test's surface is exactly 64 KiB (128x128 RGBA8), which isSmallStoreBytes, so the<=check stores it before the label.FindPendingtests pass on that build:DccKeysFirstWrite,AliasCopyLabel,ArrayLayerTarget,BcBlockCopy,DepthCompareCopy,DepthSurfaceFromStorage,ImageSrgbLoad,ImageSrgbSampleCopy(+_forced),PackedMipTailTarget,PendingExactSurface,StagedClearSampledRead,StorageFileWrite,StorageOneSliceAlias,StoreOverUntracked, andLinearReadbackLabel(+_off).Both fixes pass 3 of 3 runs:
APS5_NO_LABEL_READBACK_STORE=1, which leaves the test unchanged.I'd keep the threshold at
<=. The Smurfs image is a 1x1 R64KBX surface, which most likely also takes exactly 64 KiB, so a<check would probably bring the washed-out village back.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-19T16:59:28Z.
@adriadam10 Fixed in
2c4d1388a, rebased onmaindbac9fa15(force-pushed).agc_driver_clamped_global_fillnow runs withAPS5_NO_LABEL_READBACK_STORE=1, set inCMakeLists.txtthe same wayagc_driver_storage_file_writegetsAPS5_SUSPEND_DRAIN.KeyFillOrderTestschecks that the 64 KiB surface stays pending under its keys after the label, and the store before the label retires exactly that surface. With the store off, the key-order check means what it did onmain.I reran every
agc_driver_*test on the rebased head (591 tests, Linux 7.2.9, RX 9070 XT, RADV Mesa 26.2.4, Release):clamped_global_fillfailed with "the surface written after the key fill has no pending image under its keys" before the change and passes after it.dcc_keys_first_writepasses without a change. It has the same assertion, but its check doesn't wait on a label.linear_readback_label,linear_readback_label_off(WILL_FAIL) andflip_stores_small_resultspass.maindbac9fa15built in the same tree:image_table_vouched_lookups,bda_device,guest_memory_lifetime(amdgpu rejects a CS) andtessellation(SIGSEGV). None of them polls a label and then callsFindPending.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by ntamas94 on 2026-10-19T21:43:28Z.
Confirmed on another title and platform: this also fixes the realtime lightmaps of Gang Beasts (CUSA04865, a PS4 title, Unity 2017.2 with Enlighten), which rendered every static surface of the main menu white.
Mechanism there: Unity's Enlighten reads back its per-system albedo (sRGB8) and emissive (fp16) meta-pass textures through a GPU block copy into a linear scratch image, an EOP label and a CPU read. With the stale scratch the radiosity solver produced NaN and huge values, and the RGB9E5 lightmaps the CPU then tiled saturated to white (20% of the texels 0xFFFFFFFF, shared exponents 25–31). With this branch the lightmaps are sane (shared exponent at most 14, no saturated texel) and the brick wall matches shadPS4. I had arrived at the same change (storing pending linear images in
WriteLabelOnGpu) before finding this PR, so no separate PR.Tested: Windows 11 (10.0.26200), NVIDIA GeForce RTX 2080, driver 617.14. The net diff of this PR at
2c4d1388aapplied on a local branch (main+ #6977 and local PS4 title changes; withoutdocs/dev/ENVIRONMENT.md, which that branch lacks).agc_driver_linear_readback_labelpasses andagc_driver_linear_readback_label_offfails as expected;agc_driver_flip_stores_small_results,agc_driver_store_idle_results,agc_driver_same_queue_label_waitandagc_driver_unimported_label_orderpass.APS5_PROFILE_DRAW=1,APS5_TRACE_FLUSH=1, 150 s: 133readback before labelstores (0x200–0x9000 bytes each), all while loading, none at interrupts; no crash.APS5_NO_LABEL_READBACK_STORE=1against without, two 95 s runs each, warm shader cache, presents per 10 s: off 335–354, on 328–350 (GPU busy 22–24 ms per frame both ways). With the store off the lightmap dump is white again (52989 saturated texels); one of the off runs aborted after 20 s on an unrelated unimplemented sampler feature.Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-20T01:16:52Z.
Independent PPSA21607 (The Smurfs – Dreams 01.002.000) rendering check on RX 6650 XT / RADV Mesa 26.2.4, Arch Linux / GCC 16.2.1: applying this PR removes the severe white village washout in my local comparison.
Baseline
7f39ff42fbe39b386a7c99633757dca2f1099be6is main3aa35dc014a84b783c2fbe642f8e08341feb1a30plus #6959. Candidate97a9225d722842fc1e893ceabca3969617230e47adds the exact #6948 diff at2c4d1388a17bb84784bb877514b6dd1e19478526. This isolates this PR on that integration base; it is not a result against newest main.Both started from the same checkpoint with separate initially empty shader/Mesa caches, identical input through the compared 345-second village capture, and no keyboard-focus holding. Both reached the village and closed normally (native/supervisor exit 0, PID gone, udmabuf restored to 64 MiB). Baseline subsequently accepted analog movement, moving the character and camera from spawn to the right fence. Movement on the candidate was not tested. The opening sequence also progresses beyond the previously reported still image. Audio and full-title progression remain unverified.
Focused tests on the same GPU: baseline 6/6; candidate 8/8 including
agc_driver_linear_readback_labeland its expected-failure disabled control. Mandatory Vulkan, core validation and shader cache disabled: no skips, missing-layer messages or VUIDs. The candidate follows this PR's test configuration, includingAPS5_NO_LABEL_READBACK_STORE=1for clamped_global_fill.Performance is still very slow: the candidate window reported about 0.9–1 FPS in the village. These are observations, not a controlled performance comparison or a speed-up claim. Removing the visible washout does not establish pixel-perfect reference correctness.
Evidence packet: exact commands, pins, native reports, focused-test summaries, selected captures and hashes. No game assets, binaries or saves included. Game discussion: #8158.