fix(agc): keep storage image results recorded after a direct write when its completion stamps it again #8126
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8126"
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 Longziip on 2026-10-19T22:56:44Z.
What
A dispatch's direct (host-imported) and copied-back buffer writes are stamped twice: by
GuestBufferMemory::MarkDirectWriteswhen the dispatch is recorded, and again byGuestBufferMemory::WriteBackwhen its batch completes, if the dispatch needs a completion (a copied written buffer or BDA fault checks). The second stamp gets a newer generation than anything recorded in between. A storage image over the same memory that was uploaded and written after the dispatch in GPU order therefore saw the completion stamp as a newer write: its nextRefreshdropped all of its pending results ("guest memory changed while GPU results were pending ... writers: 0 CPU, 28 driver ... 28 dropped") and re-uploaded the dispatch's older bytes.WriteBacknow stamps throughStorageTexture::StampKeepingNewerResults. Before the stamp it collects the pending units of pending storage images in the range whose generation is at or after the dispatch's record-time stamp (kept inGuestBufferMemory::directWritesGeneration) and that nothing stamped since; after the stamp those units move to the new generation, so they keep their results. Each candidate unit's range is collected withGuestMemory::CollectWritesUncachedbefore that check, so a CPU write that nothing had collected yet invalidates the unit instead of being absorbed by the stamp. Every other reader is still invalidated by the completion stamp as before. Removing the completion stamp instead is not correct: in-game it left stale textures and garbage after menu navigation.EA SPORTS UFC 6 (PPSA23566) hits this on its main menu: a compute pass writes an 888x504 surface as a buffer and a storage image (format 22 with tile mode 3, or format 36 with tile mode 7) writes the same memory after it in the same batch. In about half of the launches the menu background and banner turned into blue and black blocks within a few minutes.
New test
agc_driver_direct_write_completion(tests/execution/DirectWriteCompletion.cpp): one submission fills a write-watched surface as a buffer (plus a second, copied buffer so the dispatch takes a completion), then writes it as a storage image; after the completion has run,Refreshmust keep the image current and pending. It uses as many surfaces as fit in one settled 4 MiB host import block (15 runs): a surface past it is imported alone below the settle floor and unwatched, so its record-time stamp is 0 and its results are dropped by design. Onmainall runs drop the results; with this change none do. A second case writes the surface from the CPU after the record and before the completion (the submission ends with a CPU-releasedWAIT_REG_MEM, the write is made underGpuMutexonce the image is pending): its results must be dropped and the CPU bytes kept.Related: #7233 changes the same three
MarkWrittencalls toMarkPossiblyWrittenfor a different issue (complete-coverage evidence); possible writes still invalidate readers there, so it does not fix this. Whichever lands second needs a one-line rebase.Tested
Windows 11 Pro (build 26300), AMD Ryzen 7 5700X, NVIDIA GeForce RTX 4060 8 GB (driver 616.64), 32 GB RAM, WinLibs GCC 15.2.0, Release.
main(77e31219c) and on this change withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation:ctest -R "agc_.*(storage|copied|direct_write|idle_store|untracked)".main: 35 of 35 pass (it has noagc_driver_direct_write_completion). This change: 36 of 36 pass. Neither log containswas not found but was requested by env var VK_INSTANCE_LAYERSor anyVUID-error.python tools/check_conventions.py --base origin/mainpasses.mainstill stops before the menu at the jump-table shader0x1163c84000, so the title was run on the UFC 6 testing build from #5996 with the same change. Before: the menu corrupted in about half of the launches (hundreds of the 888x504 drop reports per run). After: three 4-minute menu runs with no corruption and no drop reports, then Fight Now through fighter select and versus into the T-Mobile Arena walkout with no visible corruption.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 github-actions[bot] on 2026-10-19T22:57:00Z.
Thank you for your contribution! Authors with no pull requests merged into
maincan have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T08:24:02Z.
Reopening: this was closed by the open-PR limit while our intake was paused, not on its merits. We're taking it on and will review it under the normal process; it's assigned to us so it isn't closed again by the limit.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T08:30:01Z.
Thanks, the double-stamp analysis matches the code, and the new test pins the drop.
One case the keep list misses: CPU writes that have not been collected yet.
GuestMemory::UnchangedSinceonly reads the block stamps.GuestMemory::MarkWrittenfirst collects the range's fresh CPU writes (GuestWriteWatchCollectFreshon Linux,collectWindowsWriteswithStampKind::Freshon Windows), then stamps every block with the generation it returns. So if the guest CPU wrote over a pending unit after the unit's generation, and nothing collected that write before the completion, the unit passes theUnchangedSincefilter. The write is absorbed into the completion stamp, and moving the unit to that generation hides it. The nextRefreshkeeps the GPU results, and storing them writes older texels over the CPU's bytes. Onmainthe completion stamp drops the results, and the re-upload keeps the CPU write.The other decisions about pending results in
Texture.cppcollect the range first withGuestMemory::CollectWritesUncached(for example around lines 4129, 4141 and 4198 on currentmain). CouldStampKeepingNewerResultsdo the same for each candidate unit before theUnchangedSincefilter? A write landing between that collect andMarkWritten's own collect would still be absorbed. If the stamp can report whether its fresh collect found writes in a unit's range, skipping the unit in that case would close that window too.A second case in
DirectWriteCompletion.cppwould cover it: the CPU writes the surface between the submission and its completion, and the test expects the image's results to be dropped and the CPU bytes kept.Imported from boykopovar/AnyPS5. Originally posted by Longziip on 2026-10-20T16:34:52Z.
Thanks, agreed on the uncollected writes. Rebased onto current
mainand pushed:StampKeepingNewerResultsnow runsGuestMemory::CollectWritesUncachedover each candidate unit before theUnchangedSincefilter, like the other pending-result decisions inTexture.cpp.mainregardless of this change: its 17th surface fell outside the 4 MiB host import block that the first run settles, so it was imported alone belowsettleFloorByteswithstaysWatchedfalse.MarkDirectWritesthen got 0 fromMarkWritten, the keep path was skipped, and theRewatchstamp dropped the results (which is correct for unwatched memory). The test now uses as many surfaces as fit in one settled import block (15 runs).Second case: I tried a CPU write after
AgcDriverWaitIdleand one behind a CPU-releasedWAIT_REG_MEMplaced after the store. Both pass even without the collect, because the write is ordered or collected before the completion's stamp, so neither exercises the window. A deterministic test would need a hook that runs between the record and the completion'sMarkWritten. Would a test-only hook inGuestBufferMemory::WriteBackbe acceptable here, or would you rather keep that out of the driver?The window between this collect and
MarkWritten's own collect is still open. Closing it needsMarkWrittento report which units its fresh collect found writes in. I can do that in a follow-up if you prefer to keep this one small.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T16:42:32Z.
Thanks, the collect before
UnchangedSincefixes what I raised, and the rebased first commit is unchanged. One thing is still open from my earlier comment: a test where the CPU writes the surface between submission and completion and the pending results are expected to be dropped. Without it, removing the newCollectWritesUncachedline still passes the suite.Imported from boykopovar/AnyPS5. Originally posted by Longziip on 2026-10-20T16:54:31Z.
Added the case to
DirectWriteCompletion.cpp: the submission ends with aWAIT_REG_MEMon a CPU-released label, and the test writes the surface underGpuMutexonce the image is pending, then releases the label, so the CPU write lands after the record and before the completion's stamp (checked with a temporary trace of the stamp). All 15 runs drop the results and keep the CPU bytes.It also passes without the new
CollectWritesUncachedline, and I traced why.MarkWrittencollects withStampKind::Fresh, and on WindowsWindowsMappings::collectResidentreports only pages that were never collected since they were committed (report = freshforCollection::Fresh); it does not read or reset the write watch of pages already collected. A pending unit's pages were collected when the image was recorded, so a later CPU write over them is not taken by the completion'sMarkWritten: the unit is kept by the stamp (4 of 4 units in the trace), but the write-watch bit stays set and the nextRefreshcollects it and drops the results. The Linux and macOS paths split fresh and armed collects the same way; I could only run Windows.So the absorbed-write case needs pages inside a pending unit that were never collected, which the record does not leave. I kept the collect (it makes the keep decision not depend on that) and the new case (it pins that a CPU write before the completion wins). If you know a path that leaves fresh pages in a pending unit, I'll turn it into the failing case.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T17:45:22Z.
Thanks for the test. A correction to my earlier comment about uncollected CPU writes: I instrumented it, and on this path the case doesn't produce wrong output, even without the
CollectWritesUncachedline.With that line reverted, your new CPU-write case still passes 15/15. The completion's filter does keep the units (the write isn't collected yet), but the next
Refreshsees the CPU write through the tracker's CPU-kind block stamps, logs "guest memory changed while GPU results were pending; keeping the CPU's blocks", and drops the image. The write survives either way. So the extra collect is harmless defence rather than a required fix; keeping it or dropping it are both fine with me. No further changes needed from my side.Imported from boykopovar/AnyPS5. Originally posted by simonbbby on 2026-10-20T18:26:05Z.
Linux control at
021d6d98d658e13cd0ecdbadc791a2495a865ea4, using the unchangedagc_driver_direct_write_completion_testsfixture. Ubuntu 24.04, GCC 13.3 Release, shader cache disabled andANYPS5_REQUIRE_VULKAN=1:GuestBufferMemory.cpprestored from base77e31219cEach process was fresh; parent and restored columns each contain three runs per ICD.
On every restored run, the first case keeps all 15 newer image results. The second case keeps zero images over the CPU write and loses zero CPU writes, across all three ICDs. NVIDIA's parent control stops at
the completion of a buffer write dropped image results recorded after it; restoring the PR removes that failure. RADV/lavapipe passing the parent control means they do not discriminate the original double-stamp bug here.This adds Linux/NVIDIA reproduction and RADV/lavapipe compatibility coverage. I did not remove the extra
CollectWritesUncachedcall: the latest maintainer comment already resolves that point. No retail title was run, and no Khronos validation layer is installed here. Source restored to the exact PR head, clean working tree.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T22:14:24Z.
The overlap bot flags #7233 here, and I checked the current hunks: #7233 changes the three
MarkWrittencalls inMarkDirectWrites/WriteBacktoMarkPossiblyWritten; this PR captures the generation fromMarkWritteninMarkDirectWritesand wraps the twoWriteBackstamps. These changes appear composable, but whichever branch lands second should preserve both the possible-write classification and the generation needed byStampKeepingNewerResults. #7233 is currently mergeable and has already been rebased onto current main. Could you coordinate the order with Marc before merging/rebasing so the combined hunk keeps both behaviors? I have not run this PR locally.Imported from boykopovar/AnyPS5. Originally posted by Longziip on 2026-10-20T22:47:59Z.
Thanks both, and thanks @simonbbby for the Linux/NVIDIA reproduction.
Order with #7233: whichever lands second rebases, and the combined hunk is mechanical. I merged
pr7233into this branch locally: the only conflict is the threeMarkWrittencalls inGuestBufferMemory.cpp.MarkPossiblyWrittenreturns the generation of the samemarkWrittenhelper, so the resolution keeps both behaviors:MarkDirectWrites:const auto generation = GuestMemory::MarkPossiblyWritten(...); if (generation != 0) directWritesGeneration = std::max(directWritesGeneration, generation);WriteBackstamps:StampKeepingNewerResults(begin, size, directWritesGeneration, [&] { return GuestMemory::MarkPossiblyWritten(begin, size); })agc_driver_direct_write_completionpasses on that combined tree here (15 of 15 kept, 0 of 15 kept over the CPU write). If #7233 merges first I will rebase this onto it with that resolution; if this one merges first, #7233 only needs the same one-line change per call. I have not pushed the combined tree.