fix(agc): publish occlusion counts before the ready marker #5213
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5213"
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 ivica00 on 2026-10-13T14:45:28Z.
What
Asynchronous GPU sample counter dumps used to store the sample count and ready bit 63 from one compute dispatch. A guest CPU that polls the dump can observe that bit set while the count words are still the previous or zero values.
The dump now publishes in three dispatches. Each one waits until the previous stores are visible to the host:
APS5_SYNC_SAMPLE_DUMPSorAPS5_NO_GPU_SAMPLE_DUMPSstill forces the synchronous CPU dump. GPU dumps stay the default.agc_sample_dumpsrunssampleDumpTeststhroughagc_driver_recorder_tests --sample-dumps-only.Tested
SampleDumps.compcompiles with glslangvulkan1.1.agc_sample_dumpswas not run on lavapipe or another GPU in this pass.Checklist
main; no other open PR implements the same functionsNotImplemented_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)Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T16:03:35Z.
Thanks for tracking this down. Reading the diff against
PacketMemory.cpp: withDumpSamplesOnGpureturning false by default,wroteOnGpustays false and the sample-dump packet falls into thedrainsbranch (sampleDumpis in the list there), so every sample counter dump now drains the queue (SubmitAndEpoch/WaitIdle). That is the intended synchronous behaviour, but it applies to every title that issues sample dumps, and the description only reports one title (PPSA03522) and no timing. A frame-time number from a title with many occlusion queries (before/after) would show what the default costs.Also, the description says the asynchronous dump lacks a host-visible barrier; if that is the cause, adding the barrier on the GPU path would keep it asynchronous. I only read the code and did not run anything.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by ivica00 on 2026-10-13T16:51:10Z.
Good points, thanks @xXPBSXx!
Regarding the host-visible barrier:
The phrasing in the initial description was misleading. oldSamples and closeStoreRun already emit barriers to VK_PIPELINE_STAGE_HOST_BIT / VK_ACCESS_HOST_READ_BIT in the command stream. However, in Vulkan, pipeline barriers only establish visibility once execution completes and the host synchronizes (e.g. via fence or idle); they do not block host CPU threads while the command buffer is unsubmitted (such as when queued in open->run.queued under LabelRunsPerBatch) or executing in flight on the GPU. Titles like Unreal Engine 4 directly poll the query counter on the CPU without waiting on a GPU fence; because GuestMemory uses write-watching rather than read-trapping, CPU loads do not invoke flush hooks and observe unready memory (ready bit = 0) or zero samples, causing false-negative culling and flickering geometry.
Regarding the default:
You are completely right that draining by default would risk regressions on titles with heavy occlusion queries like PPSA21564. I have updated the PR so that asynchronous GPU sample dumps remain enabled by default. Titles whose CPU polls the counters directly without GPU synchronization can now opt in to synchronous dumps via APS5_SYNC_SAMPLE_DUMPS=1 or APS5_NO_GPU_SAMPLE_DUMPS=1.
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T01:09:08Z.
Review: COMMENTED
As it stands the PR is an opt-in switch, so the default behaviour is unchanged. That makes it a reasonable diagnostic, but it isn't a fix users will find: Hello Neighbor 2 still flickers unless someone sets
APS5_SYNC_SAMPLE_DUMPS=1.A CPU seeing "not ready" while the GPU hasn't finished is also what happens on the console. UE4's visibility thread handles a not-ready query by keeping the previous result, so polling before completion shouldn't cause flicker by itself. Flicker suggests the CPU sees the result as ready with wrong counts, which points to the order in which the dump's words become visible:
HOST_READonly makes writes visible once the host waits on a fence. A polling CPU can then see the marker before the counts.To confirm the cause: a trace of one flickering query slot, logging the dump's words at the moment the guest reads them, would show whether the ready word is set while the counts are still the previous or zero values.
Imported from boykopovar/AnyPS5. Originally posted by Tamalero on 2026-10-14T04:14:02Z.
High on Life (PPSA15388, UE4) is another title this fixes, together with #5232. Tested at the upstairs window from #5488 and the #5232 report.
APS5_SYNC_SAMPLE_DUMPSbehaviour)A second symptom also went away. With #5232 alone, objects that the first-person weapon passed over came back a few frames late. I had put that down to UE4 reading occlusion results from earlier frames, but with this PR added they no longer disappear behind the weapon at all.
The occlusion trace from #5488 agrees: the three large steady queries (0.7–1.6 M samples) read correctly in 55 of 55 frames, with no zero frame.
The combination matters. Without #5232, this same CPU-side path only counted finished work. Many queries in a row then shared one stale total, and at low frame rates doors vanished (on RDNA2 at about 1 FPS, see #5488). With #5232's ordering in place, the per-query totals on this path are real, and they move smoothly as the camera sways.
Details
main18c438b10plus #4487, #5156, #5003, #5494, local diagnostics and #5232. This PR's two-line change matches a local switch I already had (DumpSamplesOnGpureturns false), so the run used that switch rather than the PR commit.ZPASS_DONEtrace from #5488, armed about 6 s, 55 frames, 45–63 queries per frame. Every dump took the CPU path.Tested and written by Claude (AI assistant) on a contributor's machine.
Imported from boykopovar/AnyPS5. Originally posted by ivica00 on 2026-10-14T06:47:22Z.
The default path no longer stores the count and bit 63 in one dispatch. Each dump now clears bit 63 and writes the high half, then writes the low half, then sets bit 63. Each of those dispatches waits until the previous stores are visible to the host, so a poll that sees the ready bit also sees the counts.
APS5_SYNC_SAMPLE_DUMPS/APS5_NO_GPU_SAMPLE_DUMPSremain a diagnostic for the synchronous CPU dump.Pushed in
6b164f21a.Imported from boykopovar/AnyPS5. Originally posted by KauaSantos702 on 2026-10-14T13:45:53Z.
NVIDIA run at this head (
6b164f21a), adding a vendor to the AMD results in the body: noagc_*test fails.BUILD_TESTING=ONctest -R '^agc_' -j2: 484 tests, 483 passed, 0 failed, 1 skipped. The skipped test isagc_driver_stencil_ref_export, because the device lacksVK_EXT_shader_stencil_export; it is also skipped onmain.Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-14T17:33:37Z.
The three phases are in the right order, but at
6b164f21aphase 2 crashes lavapipe, and the occlusion dump tests that would catch it are not in ctest.Phase 2 reads the dump back, and lavapipe faults on that read. With this PR merged into main,
sampleDumpTestssegfaults at its first queued dump on lavapipe (Mesa 26.0.8, LLVM 21.1.8). Main passes the same test. The fault is the|= 0x80000000uin the phase-2 loop of SampleDumps.comp#L18-L24. llvmpipe's JIT runs that loop body once more after the loop has ended (its registers showdump= 1 withdumpCount= 1). The stores in that pass are masked, but the load throughtargetis not. It reads the next list entry, which was never written and is 0, as the target and faults at address 0x4. Phases 0 and 1 only store to the target, so they don't hit this, and the RTX 5080 runs the same shader without a fault. CI runs the GPU tests only on lavapipe (CONTRIBUTING.md), so these tests can't be registered while phase 2 reads.If phase 2 stores the whole high word from the same fold, the read goes away and the order stays the same: both count halves first, bit 63 last. The counter then has to move in phase 2 instead of phase 1:
SampleDumps.comp
SampleCounter.compcan keep its|=, because its target is the pushed address, never a list entry. WithAPS5_LABEL_RUNS_INLINE=1, which sends the dumps throughfoldSamples,sampleDumpTestspasses every value check on lavapipe and stops where it should, at "occlusion dumps are recorded at once".The dump tests aren't in ctest.
sampleDumpTestsonly runs whenagc_driver_recorder_testsis started with no argument (tests/Recorder.cpp#L7272), and noadd_testdoes that. On main that run also stops earlier, at "(9) a scan under a pending write was kept", on both lavapipe and the RTX 5080. So neither CI nor thectest -R '^agc_'run above executes the dump shaders. An entry next to--pending-key-stores-onlycovers them, with skip lines thattools/check_skips.pyaccepts:tests/Recorder.cpp and CMakeLists.txt (against main)
With both changes on this PR merged into main
e34c47921,agc_sample_dumpspasses 3 of 3 times on lavapipe (--repeat until-fail:3, 0.07 s), the 32 registered recorder ctests pass (agc_shadowed_key_storesskips, as on main), andtools/check_test_registration.pyandtools/check_skips.pyare clean. With the shader of6b164f21a, the same test fails with SEGFAULT. Main's shader passes it. On the RTX 5080, the test passes 3 of 3 times with either shader, and the registered recorder ctests pass for main, this PR, and this PR with the change.What the default path changes, and #5488. A dump ends with the same words as on main: DB 0 holds the total with bit 63 set, DBs 1 to 15 hold bit 63 alone, and the counter moves once. Only the order in which a polling CPU can see them changes. To check that order, I used a local probe (not for this PR). It records 256 query slots per batch (clear, draw, begin dump, draw, end dump), and the CPU polls DB 0's begin and end words and DB 15's end word while the batch runs:
With this PR the poller does catch dumps in flight. On main, the RTX 5080 never showed it a half-written dump, so this probe can't reproduce the race there. The new order is still the safe one, but this is no evidence that the #5488 dropouts come from this race.
A readback taken after the GPU goes idle, like the per-flip trace in #5488, reads the same values at this head as on main. @Tamalero's 55 clean frames (comment) used the synchronous path (
DumpSamplesOnGpureturning false), which this head makes opt-in. Nobody has run this head's default path on PPSA15388 yet. With #5232 alone, a run of queries still read 0 about once in 50 frames, and if those zeros are in the post-idle trace, this head won't change them. A run of this head withoutAPS5_SYNC_SAMPLE_DUMPSat the same window would show that, on top of the current-main run @oneandonlydean asked for in #5488. Until then, #5488 should stay open when this merges.Cost. Each fold now runs three dispatches and two more barriers. Since #5232, a store after a dump starts a new group, so with a clear before each query slot (the pattern in #5232's test) this happens once per slot. On the RTX 5080, one probe batch of 256 query slots, from recording to idle, took a median of 2.37 ms on main, 6.8 ms on this PR and 4.8 ms with the change above (3 runs of 200 batches each). That is about 17 µs per query slot for this PR and about 9.4 µs with the change. What that costs in a title needs a frame-time run, like the one @xXPBSXx asked for.
AI-assisted: written with Claude Code; I checked the diff at
6b164f21aagainst main's dump path, the words each phase writes, the llvmpipe JIT code at the crash,tools/check_skips.py, this thread and #5488, and ranagc_driver_recorder_tests(the dump tests through a--sample-dumps-onlyentry, the registered recorder ctests and a local CPU polling probe) for main, this PR merged into main, and that merge with the shader change, on lavapipe in WSL (main763ad20fcande34c47921, gdb on the crash) and on an RTX 5080 (driver 616.92, Windows 11, WinLibs GCC, main763ad20fc).Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T18:06:06Z.
Review: CHANGES_REQUESTED
The three-phase order (high half without bit 63, low half, bit 63 last) is right, but two things block merging:
core/libs/prx/libSceAgcDriver/Graphics/shaders/SampleDumps.comp:18-24: phase 2 doestarget.words[...] |= 0x80000000u, which reads throughtarget. As @nikowolf123 reported, lavapipe runs one extra masked iteration, loads the next (unwritten, 0) list entry and faults at 0x4 insampleDumpTests. Phases 0 and 1 only store, so they are fine. Please apply the fix from their comment: phase 2 stores the whole high word from the same fold ((db == 0u ? high : 0u) | 0x80000000u) and phase 1 no longer folds the counter, so the read goes away and bit 63 is still the last write.sampleDumpTests,tests/Recorder.cpp:5278) only run whenagc_driver_recorder_testsis started by hand and are not in ctest, so CI cannot catch a regression like the one above. Please register a ctest entry for them (CI runs these GPU tests on lavapipe) and check it passes there.Note on testing: we could not run
sampleDumpTeststo the end on our lavapipe setup, so the lavapipe crash is taken from the report above; the ordering fix needs a run on lavapipe after the change.Imported from boykopovar/AnyPS5. Originally posted by ivica00 on 2026-10-14T23:25:20Z.
Phase 2 now stores the whole high word,
(db == 0u ? high : 0u) | 0x80000000u, and does not read the dump. The counter is written in that dispatch, so phase 1 no longer publishes it.agc_sample_dumpsrunssampleDumpTestsvia--sample-dumps-onlyand skips with code 77 when host imports or buffer device addresses are missing.SampleDumps.compcompiles with glslang vulkan1.1. I have not runagc_sample_dumpson lavapipe in this pass.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:08:46Z.
Review: APPROVED
Checked the re-push at
b526a115a:SampleDumps.compnow stores the whole high word and never reads the dump;--sample-dumps-onlyon lavapipe (Mesa 25.2.8, LLVM 20) passes, without the fault reported at6b164f21a.agc_sample_dumpsis registered in ctest and passes on RADV; the rest of the libSceAgcDriver test scope is green.Merging once the full Build is green.