Skip to content

fix(agc): keep resident DCC targets watched under aligned block imports - #8172

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
wachi35676:fix/agc-import-watch
Oct 20, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
wachi35676:fix/agc-import-watch

Conversation

@wachi35676

Copy link
Copy Markdown
Contributor

What

Fixes #8103. Since #6457, agc_driver_msaa_dcc_clear_4 has failed on Windows on both NVIDIA and AMD. Two separate problems cause it, and this PR fixes both.

1. ImportWatched unwatched the whole import (Execution/src/GuestMemory.cpp)

#6457 imports adjacent registered ranges as one aligned power-of-two block. In the test, that block is 0x200000000+0x400000, and it contains:

  • two 256 KiB blocks that earlier sub-settle-floor imports had taken out of the write watch, and
  • the resident 4x color target and its DCC metadata.

ImportWatched(..., staysWatched = true) checked whether the whole run was covered. One unwatched block made before false, and it then called unwatchLocked over the whole run, which took the resident target out of the watch. checkDccCurrent then failed with multisampled DCC target reuse requires watched guest memory.

The fix splits the run into maximal runs of watched 64 KiB blocks. For each run it:

  • saves the window copy,
  • walks it before the import, and
  • walks and compares it against the window after the import.

It unwatches only the parts that were not watched or whose walk failed.

  • A wholly covered run takes exactly the old path: one span, the same copy, the same walks and the same failure handling.
  • The !staysWatched path is unchanged.

2. DCC metadata generations came from a walk of the metadata pages only (Graphics/src/MultisampleTarget.cpp)

With (1) alone, the test got further and failed with writes to a resident multisampled DCC target or its metadata are not implemented (DCC range 0x2003b0000+0x1000). A diagnostic trace showed why:

  • The metadata is a 4 KiB range at the start of a 64 KiB block that the CPU wrote whole before the target became resident.
  • initialDccClear and absorbDccFill took the range's generation from CollectWritesUncached(range). That walk covered only the 4 KiB, so the other 15 pages still had write-watch bits pending.
  • The 4 MiB import's walk before the import stamped those pages as a CPU store newer than the generation. The window walk after the import then marked the metadata page as possibly stored over: the driver had resolved its write protection, and its bytes were unchanged. So both UnchangedSince and page-level StoredOver failed.

The generations are now taken from a walk of the whole 64 KiB blocks around the range (collectBlocks). If that walk returns 0, they fall back to a walk of the range alone. Every write collected this way was made before the generation, so this can only make the tracking more precise.

  • A CPU store to the metadata page after the generation is still refused (see the new negative check below).
  • A store to a neighbouring page after the generation stays allowed, as before, through the page-level StoredOver.

Why there are two changes. Each one fixes a separate refusal, and the test passes only with both (see Tested). Plan (a) in the bisect comment, changing importRun, was not used: sameRange also goes through importRun when imports are refreshed, so its result can't depend on the watch state. This PR leaves importRun and #6457's block selection unchanged.

Credit. @simonbbby's control in the issue, SetHostImportWindow(BlockBytes) in DccRefillTests, is what pointed at the multi-range block. Their Linux data (the test passes at 9127ea3, 5242599 and main, because the Linux fixture's mmap addresses never form the block) confirms that the failure depends on the memory layout.

Regression tests. Both are layout-independent, so they do not rely on the Windows arena addresses:

  • agc_write_tracking, new CheckImportWindowOverUnwatchedBlock (GuestMemory level, no Vulkan, both platforms):
    • It unwatches one block of four and imports all four with staysWatched.
    • It requires the three watched blocks to stay watched and the unwatched one to stay unwatched.
    • It also checks the window stamps inside the kept blocks: a resolved page with unchanged bytes reads as unchanged; a store of new bytes inside the window, and a CPU store made before the import, read as written.
  • agc_driver_msaa_dcc_clear_{2,4,8}, new DccImportWindowTests:
    • It makes a DCC target resident, then imports its metadata block with a same-byte store to the metadata page, which stands in for a driver resolving write protection. The next draw must be accepted.
    • A second import stores a new byte there, and the next draw must be refused with writes to a resident multisampled DCC.

Remaining case, not fixed here: if the CPU stores to another page of the metadata's 64 KiB block after the generation, and an import window later covers the metadata page, the draw is still refused. The window conservatively counts a resolved page as possibly touched by the CPU, and I left that tracker rule alone.

Overlap:

Tested

Windows 11 Home 26300, Ryzen 5 9600X, WinLibs GCC 15.2.0, Release, Ninja, on main 5253446. Environment: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 APS5_NO_PIPELINE_JOURNAL=1.

The Khronos validation layer is not installed on this machine, so the VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation run was not done.

Focused tests:

main, original tests new tests + (2) only new tests + (1) only this PR
agc_write_tracking passes fails: took the watched blocks out of the write watch passes passes
agc_driver_msaa_dcc_clear_4 requires watched guest memory requires watched guest memory writes to a resident multisampled DCC passes
agc_driver_msaa_dcc_clear_2, _8 pass pass writes to a resident multisampled DCC (the new DccImportWindowTests) pass
  • The table holds on both the RTX 5070 Ti and the Radeon iGPU.
  • With this PR, agc_driver_msaa_dcc_clear_{2,4,8}, agc_driver_msaa_dcc_state and agc_write_tracking passed 3 runs of 3 on each GPU.

ctest -R "agc|shader|spirv|recomp|guest_memory|import|watch" -j6 (837 tests):

  • NVIDIA
    • main fails 3: agc_driver_msaa_dcc_clear_4, agc_driver_pass_input_hazard, agc_pipeline_journal.
    • This PR fails 2 of those: agc_driver_pass_input_hazard and agc_pipeline_journal, both of which also fail on main when run alone.
    • This PR's run also failed agc_driver_sgpr_pattern_fill_keys. That is a 10 s polling deadline that times out under load on main too: 2 of 8 parallel batches failed on main, and 0 of 8 with this PR on an idle machine. It passes serially.
  • AMD
    • main fails 7: agc_driver_msaa_dcc_clear_4, agc_driver_draw_cache_reuse{,_verify,_inline}, agc_driver_layered_volume_vertex_no_gpl, agc_driver_stencil_replay_no_gpl, agc_pipeline_journal.
    • This PR fails the same set minus agc_driver_msaa_dcc_clear_4, plus two others:
      • agc_driver_sgpr_pattern_fill_keys, the same load-dependent timeout as above.
      • agc_trimmed_import, which fails 5 of 5 serially on main on this iGPU too (after an unmap of 1440 KiB ... served from the import that pinned the old ones). It passed on main only in that run's parallel batch.

#6457's claims: importRun and the block choice are not touched. The tests that carry #6457's import counts and costs pass on both GPUs: agc_storage_writeback_blocks, agc_driver_graphics (the BdaResources mocks), agc_driver_storage_write_back_overhang and the host_import tests.

  • The only new cost is on an import whose range is only partly watched: it now copies, compares and walks its watched blocks. That is the same work a fully watched import of that size already did on main.
  • I did not take a title performance trace.

Linux: WSL Ubuntu 24.04, gcc 13.3, built from this branch and from main. Same suite:

  • main fails 18, and this PR fails 17, the same set minus agc_driver_sgpr_pattern_fill_keys.
  • WSL has no userfaultfd write protection ([memory] write watch unavailable), so agc_write_tracking is skipped and the DCC tests fail at initial multisampled DCC contents require watched guest memory, on main and here alike.
  • The new tests have therefore not run on a Linux machine with write watching.

python tools/check_conventions.py --base origin/main passes on Python 3.13 and 3.9.

Checklist

🤖 Generated with Claude Code

Since host imports group adjacent ranges into aligned blocks (boykopovar#6457), an
import can span both 64 KiB blocks that an earlier import left unwatched
and the metadata of resident multisampled DCC targets.

ImportWatched with staysWatched took the whole import out of the write
watch when any part of it was not watched before. It now walks and
compares each run of watched blocks of the import on its own, and
unwatches only the blocks that were not watched or whose walk failed.

A resident DCC target took the generation of its metadata from a walk
of the metadata's pages alone. CPU stores to the rest of the metadata's
64 KiB block made before that walk were stamped later by the import's
walk, so the block read as written, and the import window marked the
metadata page as possibly stored over: the next draw refused the
target. The generations now come from a walk of the whole blocks.

agc_write_tracking and agc_driver_msaa_dcc_clear_* gain regression
tests for both.

Fixes boykopovar#8103

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MarcZX10

Copy link
Copy Markdown
Contributor

FYI, I have PR #8169 open for the same #8103 report, with a narrower importRun guard that avoids selecting an aligned block spanning unwatched ranges. Your PR addresses the same initial watch-loss failure and also covers the later DCC metadata-generation refusal with separate tests. I am linking the two for comparison; I have not copied or changed your branch.

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6308 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp L1576
core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp L1329
PR 7948 core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp L38
PR 7981 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp L1544-1563, L1574-1576
core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp L1046, L1329-1330

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4448 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 5021 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6185 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6186 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6187 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6188 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6192 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6209 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6273 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp
PR 6292 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6615 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6628 core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp
PR 6683 core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp
PR 6718 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6759 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6808 core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp
PR 6820 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6868 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp
PR 6926 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6946 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6947 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6960 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
PR 6971 core/libs/prx/libSceAgcDriver/Graphics/src/MultisampleTarget.cpp
PR 7201 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp
PR 7332 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp
PR 7505 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp
PR 7604 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp
core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp
PR 7615 core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp

@github-actions github-actions Bot added the agc libSceAgc* label Oct 20, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 20, 2026
@github-actions github-actions Bot added the fix error Closes an issue (Fixes #N) label Oct 20, 2026
@simonbbby

Copy link
Copy Markdown
Contributor

Native Linux with working write watching fills the WSL coverage gap for exact head 58f0e89ca353671960a1f38c31a3765ae93d60f5 (parent 5253446882472f584358007637dd18d77c48e16a). I rebuilt the fixtures and runtime dependencies, retaining the new tests in all controls.

The mixed-watch regression is discriminating on Linux:

  • With both production files restored to the exact parent, agc_write_tracking fails 3/3 fresh processes: an import window over an unwatched block took the watched blocks out of the write watch.
  • Restoring only this PR's GuestMemory.cpp, while leaving MultisampleTarget.cpp at the parent, makes it pass 3/3.
  • The complete PR passes 3/3 before the controls and 3/3 after restoring/rebuilding the exact head. No write-watch skip occurred.

DCC compatibility passes, with an important negative-control limit:

  • Complete head, both before and after the controls: 2x/4x/8x pass 3/3 each on RTX 3090 and RADV gfx1036; lavapipe 4x passes 3/3. Actual devices verified from the restricted ICD and loader's Using records. These are 42 GPU test processes across the two complete-head batches.
  • With both production changes reverted, 4x still passes 3/3 on each of those three drivers, consistent with the earlier Linux stock-fixture result in AGC: 4x DCC refill resolve loses watched color after an adjacent aligned import (Windows RTX5080) #8103.
  • With only the metadata-generation change reverted, 4x also passes 3/3 on each driver. Thus this Linux result independently validates the mixed-watch fix and compatibility of the DCC checks, but does not reproduce the Windows metadata-generation refusal or prove that second change is necessary here. I have not constructed the Windows 4 MiB allocation layout.

After exact restoration, the registered agc_write_tracking, agc_driver_graphics, agc_driver_msaa_dcc_state, agc_storage_writeback_blocks, agc_host_import_unmap, agc_host_import_retirement, agc_driver_host_import_lifetime, and agc_driver_host_import_mutation_race tests pass 8/8, no skips (3.43 s). These nearby fixture tests are not counted as eight additional GPU executions. The source tree is clean; raw logs are retained.

Ubuntu 24.04, kernel 7.0.0-38, Ryzen 9950X, GCC 13.3, Release; RTX 3090/NVIDIA 595.99.02; gfx1036 1002:13c0/Mesa 25.2.8; lavapipe LLVM 20.1.2. DCC runs use ANYPS5_REQUIRE_VULKAN=1, shader cache and pipeline journal disabled, and other AnyPS5/Vulkan overrides removed. No Khronos validation layer is installed; no validation-layer, Windows, title-performance, or full-suite claim.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

The Linux result matches our CI on this head: agc_write_tracking passes on Linux and Windows in run 39353242551. The controls make it usable: the mixed-watch failure at the parent, gone with GuestMemory.cpp alone, is exactly the part Linux can show.

Your limit stands, and the PR should be read that way: the metadata-generation change in MultisampleTarget.cpp is supported only by the Windows ablation in the PR, since the Linux 4x runs pass without it. Neither run shows it necessary on Linux.

@oneandonlydean
oneandonlydean merged commit d9df34d into boykopovar:main Oct 20, 2026
11 checks passed
EagleBearerYS added a commit to EagleBearerYS/AnyPS5 that referenced this pull request Oct 21, 2026
Since boykopovar#8172, a host import that stays in the write watch keeps each of
its watched runs in the watch instead of taking the whole import out
when part of it was unwatched. Grand Theft Auto V (PPSA04264) imports
resident direct memory that way, and later maps the same memory at a
second address, which turns it into shared views. From then on the write
watch arms them, and the game's own stores fault and are resumed, the
red zone hazard of boykopovar#361 (boykopovar#9136, eboot 0x162b9e4 and 0x28f413a).

ImportWatched now marks the resident pages of each run it keeps in the
watch with GuestArenaCompareResidentWhenShared. They stay write-watched
while they are private, as boykopovar#8172 needs for resident DCC targets. Once
they are shared, their collects fail and they are compared, as before
boykopovar#8172, instead of being armed. Memory that is already shared when it is
imported is not marked and stays tracked as before.

agc_rewatched_direct_shared gains the imported case: resident direct
memory imported with the watch kept, then mapped a second time, must
collect as 0 through either mapping, stay writable and show a store
through both, while unimported shared direct memory is still collected.
windows_write_watch gains CheckResidentOnlyComparedWhenShared: a
resident-only mark leaves a shared view armed and collected, and still
keeps a resident page compared once a second mapping makes it shared.
EagleBearerYS added a commit to EagleBearerYS/AnyPS5 that referenced this pull request Oct 21, 2026
Since boykopovar#8172, a host import that stays in the write watch keeps each of
its watched runs in the watch instead of taking the whole import out
when part of it was unwatched. Grand Theft Auto V (PPSA04264) imports
resident direct memory that way, and later maps the same memory at a
second address, which turns it into shared views. From then on the write
watch arms them, and the game's own stores fault and are resumed, the
red zone hazard of boykopovar#361 (boykopovar#9136, eboot 0x162b9e4 and 0x28f413a).

ImportWatched now marks the resident pages of each run it keeps in the
watch with GuestArenaCompareResidentWhenShared. They stay write-watched
while they are private, as boykopovar#8172 needs for resident DCC targets. Once
they are shared, their collects fail and they are compared, as before
boykopovar#8172, instead of being armed. Memory that is already shared when it is
imported is not marked and stays tracked as before.

agc_rewatched_direct_shared gains the imported case: resident direct
memory imported with the watch kept, then mapped a second time, must
collect as 0 through either mapping, stay writable and show a store
through both, while unimported shared direct memory is still collected.
windows_write_watch gains CheckResidentOnlyComparedWhenShared: a
resident-only mark leaves a shared view armed and collected, and still
keeps a resident page compared once a second mapping makes it shared.
Celegans12 added a commit to Celegans12/AnyPS5-Ghost-of-Yotei that referenced this pull request Oct 21, 2026
- ImageArrayWindowPastLayerLimit (upstream test): MinGW GCC refuses
  alignas(65536) on a static object; the texels now come from an aligned
  heap allocation.
- BufferPool ImageMemoryOverBudget (boykopovar#7870): uses our mock's heap budget
  and usage fields.
- WriteTracking CheckImportWindowOverUnwatchedPart: with boykopovar#8172's per-span
  unwatch the watched blocks stay watched; the unwatch hook must cover the
  part that leaves the watch.

AI-assisted: yes
Pipodi pushed a commit to Pipodi/AnyPS5 that referenced this pull request Nov 14, 2026
- ImageArrayWindowPastLayerLimit (upstream test): MinGW GCC refuses
  alignas(65536) on a static object; the texels now come from an aligned
  heap allocation.
- BufferPool ImageMemoryOverBudget (boykopovar#7870): uses our mock's heap budget
  and usage fields.
- WriteTracking CheckImportWindowOverUnwatchedPart: with boykopovar#8172's per-span
  unwatch the watched blocks stay watched; the unwatch hook must cover the
  part that leaves the watch.

AI-assisted: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* fix error Closes an issue (Fixes #N)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AGC: 4x DCC refill resolve loses watched color after an adjacent aligned import (Windows RTX5080)

4 participants