fix(agc): leave the uncompressed key store out when the guest replaced the keys by another clear code #16411

Merged
SleglJan merged 4 commits from upstream-pr/16411 into main 2026-11-09 20:16:57 +00:00
SleglJan commented 2026-11-06 16:23:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-11-06T16:23:55Z.


What

A storage image uploaded under fast-clear DCC keys stores uncompressed keys (0xff) at its first GPU write and at its write-backs. Whether the keys still are that clear kind is decided earlier, by the lookup, which can answer in three ways without seeing a guest write that landed after the thread's earlier collect of the key range in the same collect epoch: from the memoized collect behind the key proof, from the refresh's own proof, and, with APS5_DRAW_PIPELINE, inside the epoch of the last proof with no collect at all.

When the guest replaced the keys by another clear code in that window, the lookup keeps the image as uploaded under the old code, and the first write's store then puts uncompressed keys over texels taken under the replaced code. Where main sees such a write it honours the guest's code in either order. A lookup that sees it before the first write takes the image under the new code (as stored for the register clear code, whose keys stay): the order "clear before the draw". Once an image holds results, a fixed clear code written over them wins ("new keys that are a clear code make every result dead on hardware too", the comment over droppable in StorageTexture::refresh, for images tracked in 64 KiB units), and the register clear code stays in memory once the first write has stored its keys: the order "clear after the draw". Here the store covers the guest's code, which neither order gives outside the two cases below. This PR gives the second.

For a fixed clear code main's result is still the first order in two cases: the first write covers every texel of the surface, or the two codes decode to the same texels for the surface's format. There this PR gives the other order, the guest's clear after the draw.

  • test(agc): six modes of the agc_driver_dcc_keys_first_write_tests executable. Each uploads an image under 0000 keys in a watched block, collects the keys, lets the guest write in the same collect epoch, looks the image up again (the step requires that the lookup kept the clear keys) and marks it written. Each step also requires the path that answers that lookup, read through the driver library (StorageTexture::RefreshesProved() and the thread's RefreshProved lookup outcome): neither moves for a refresh under the memoized collect, both rise by one for the refresh's own proof, and RefreshesProved() alone rises for an answer inside the epoch of the last proof.
    • agc_driver_dcc_keys_rewrite_after_memo: every key replaced by another clear code (1111); the lookup is answered by the memoized proof.
    • agc_driver_dcc_keys_rewrite_after_refresh_proof: the same, the lookup answered by the refresh's own proof (required by the step).
    • agc_driver_dcc_keys_rewrite_after_epoch_proof: the same, the lookup answered inside the epoch of the last proof (required by the step; APS5_DRAW_PIPELINE=1).
      On main these three fail with AGC graphics: a storage image's first write stored uncompressed keys over keys the guest had replaced by another clear code after a memoized collect: 0 of 256 key bytes are 0xc0 (key 0 is 0xff).
    • agc_driver_dcc_keys_rewrite_to_register_clear: every key replaced by the register clear code. On main it fails with AGC graphics: a storage image's first write stored uncompressed keys over keys the guest had replaced by the register clear code after a memoized collect: 0 of 256 key bytes are 0x20 (key 0 is 0xff).
    • agc_driver_dcc_keys_partial_rewrite_keeps_store: half of the keys replaced. The half the guest did not touch must read uncompressed after the first write. The rewritten half is not checked (see Limits).
    • agc_driver_dcc_keys_write_beside_keys_keeps_store: a guest write beside the keys, in their page. Every key must read uncompressed after the first write.
      These two pass on main and with the fix.
      The steps run with APS5_NO_HOST_IMPORT=1, because an upload's host import ends the write watch over the test's block; every skip prints its reason.
  • fix(agc): the check is made at the store. StorageTexture::keysReplacedByClear runs in MarkDirty and at the three write-back stores. It answers yes only when the proved key count is the stored key count, a key proof for the uploaded kind exists, the caller holds the device lock, no recorded work writes the keys, the guest wrote the key range since the proof (GuestMemory::CpuWroteSince, new: the uncached collect and CPU-stamp compare of UnchangedSinceCollected, answering "no" where it cannot tell), and the keys read again as another clear code (0000, 0001, 1110, 1111 or the register clear). Then the store is left out and the guest's clear stands.

Limits:

  • A partial rewrite keeps the store, as main does when its lookup misses the write. When main sees a partial rewrite it reads the mixed keys as uncompressed and stores nothing, so the guest's keys stay; for a given image, texture reads take its stored texels whether the keys are uncompressed or mixed. The store is kept here so that a later CB metadata pass does not meet mixed keys, which it refuses ("per-block metadata is not modeled"). Neither result is the per-key one of the order "clear after the draw", the guest's code on the keys it wrote and uncompressed keys on the rest; a store per key is not attempted here.
  • Keys replaced by the single-colour kind keep the store, and so do surfaces whose stored key count exceeds guestBytes / 256 (single-level pipe-aligned R64KBX with 4-byte elements). For element sizes that do not divide 256 main reads single-colour keys as uncompressed and leaves them in place where it sees them; here the store covers them.
  • Register clear keys left in memory are read as on main: texture reads take the stored texels, and only the colour-target path applies the guest's clear.
  • The check needs a write-watched key range. Keys in host-imported memory that is compared, not watched, have no proof generation and keep the store.
  • The three write-back stores share the check and have no step of their own: in the steps MarkDirty has already taken the image out of its clear kind when the write-back runs.
  • ProvedCurrentDccKeys (the register clear's proof) has the same window and is not changed here.

Cost: one uncached collect of the key range per first write, and per write-back store that finds the image still under its clear kind, of an image that has a proof for its uploaded kind and no key tail; a read of the keys only when that collect finds a CPU write.

Related: #6209 (open) changes ProvedCurrentDccKeys and ProvedClearKeys, which this PR does not touch. #6186 (open) keeps CPU stores made after a deferred key store was recorded, the window after this one.

Tested

Head 5287cda761 is this PR's three commits on upstream/main 3bd88ca119 (the third, test only, came after review: see the thread). The runs below are clean merges onto main 9f2b939606: main + the two test commits, main + the head, and main + the head + the head of #15456.

  • Linux (CachyOS, kernel 7.2.10), Release, GCC 16.2.1, write watch available. ctest -V -R '^agc_driver_dcc_key' with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1, on an AMD Radeon RX 9070 XT (RADV GFX1201, Mesa 26.2.4) and on lavapipe (llvmpipe, LLVM 23.1.1, Mesa 26.2.4). The pattern matches the six new steps and the two existing key tests, agc_driver_dcc_keys_first_write and agc_driver_dcc_key_presync.
    • main + the two test commits: on both drivers the four rewrite steps fail with the lines above, and they are failures, not skips; no step stops at a path check; the two keep-store steps and the two existing tests pass. No VUID-.
    • main + the head: all eight pass on both drivers, none skipped. No VUID-.
    • main + the head + the head of #15456 (6330546a47, where the draw pipeline is on unless APS5_NO_DRAW_PIPELINE is set): all eight pass on both drivers, none skipped. No VUID-.
    • On both drivers and all three trees the loader's debug log shows VK_LAYER_KHRONOS_validation inserted; no log reports was not found but was requested by env var VK_INSTANCE_LAYERS.
  • ctest -R '^agc' on the RX 9070 XT, without the validation layer, main + the head: 1 of 1352 fails, agc_driver_tessellation (segmentation fault), and 23 tests skip. On the earlier head b04dc41f0a over main 3bd88ca119: 5 of 1349 failed with the test commit alone and 1 of 1349 with the fix; the four rewrite steps were the only difference in the failed list, with the same failing test and the same 23 skipped names as now.
  • Windows: not run by me. On b04dc41f0a the epoch step failed there (JJenkx's run in this thread), by the code because the test read a header-defined counter across the library boundary; 5287cda761 reads through the library instead.
  • python3 tools/check_conventions.py --base upstream/main: passes.

No title effect is claimed.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 9070 XT (RADV GFX1201), Mesa 26.2.4, Linux (CachyOS, kernel 7.2.10); lavapipe (llvmpipe, LLVM 23.1.1), Mesa 26.2.4, same host
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5191044318 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16411). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-11-06T16:23:55Z. --- ### What A storage image uploaded under fast-clear DCC keys stores uncompressed keys (0xff) at its first GPU write and at its write-backs. Whether the keys still are that clear kind is decided earlier, by the lookup, which can answer in three ways without seeing a guest write that landed after the thread's earlier collect of the key range in the same collect epoch: from the memoized collect behind the key proof, from the refresh's own proof, and, with `APS5_DRAW_PIPELINE`, inside the epoch of the last proof with no collect at all. When the guest replaced the keys by another clear code in that window, the lookup keeps the image as uploaded under the old code, and the first write's store then puts uncompressed keys over texels taken under the replaced code. Where main sees such a write it honours the guest's code in either order. A lookup that sees it before the first write takes the image under the new code (as stored for the register clear code, whose keys stay): the order "clear before the draw". Once an image holds results, a fixed clear code written over them wins ("new keys that are a clear code make every result dead on hardware too", the comment over `droppable` in `StorageTexture::refresh`, for images tracked in 64 KiB units), and the register clear code stays in memory once the first write has stored its keys: the order "clear after the draw". Here the store covers the guest's code, which neither order gives outside the two cases below. This PR gives the second. For a fixed clear code main's result is still the first order in two cases: the first write covers every texel of the surface, or the two codes decode to the same texels for the surface's format. There this PR gives the other order, the guest's clear after the draw. - `test(agc)`: six modes of the `agc_driver_dcc_keys_first_write_tests` executable. Each uploads an image under 0000 keys in a watched block, collects the keys, lets the guest write in the same collect epoch, looks the image up again (the step requires that the lookup kept the clear keys) and marks it written. Each step also requires the path that answers that lookup, read through the driver library (`StorageTexture::RefreshesProved()` and the thread's `RefreshProved` lookup outcome): neither moves for a refresh under the memoized collect, both rise by one for the refresh's own proof, and `RefreshesProved()` alone rises for an answer inside the epoch of the last proof. - `agc_driver_dcc_keys_rewrite_after_memo`: every key replaced by another clear code (1111); the lookup is answered by the memoized proof. - `agc_driver_dcc_keys_rewrite_after_refresh_proof`: the same, the lookup answered by the refresh's own proof (required by the step). - `agc_driver_dcc_keys_rewrite_after_epoch_proof`: the same, the lookup answered inside the epoch of the last proof (required by the step; `APS5_DRAW_PIPELINE=1`). On main these three fail with `AGC graphics: a storage image's first write stored uncompressed keys over keys the guest had replaced by another clear code after a memoized collect: 0 of 256 key bytes are 0xc0 (key 0 is 0xff)`. - `agc_driver_dcc_keys_rewrite_to_register_clear`: every key replaced by the register clear code. On main it fails with `AGC graphics: a storage image's first write stored uncompressed keys over keys the guest had replaced by the register clear code after a memoized collect: 0 of 256 key bytes are 0x20 (key 0 is 0xff)`. - `agc_driver_dcc_keys_partial_rewrite_keeps_store`: half of the keys replaced. The half the guest did not touch must read uncompressed after the first write. The rewritten half is not checked (see Limits). - `agc_driver_dcc_keys_write_beside_keys_keeps_store`: a guest write beside the keys, in their page. Every key must read uncompressed after the first write. These two pass on main and with the fix. The steps run with `APS5_NO_HOST_IMPORT=1`, because an upload's host import ends the write watch over the test's block; every skip prints its reason. - `fix(agc)`: the check is made at the store. `StorageTexture::keysReplacedByClear` runs in `MarkDirty` and at the three write-back stores. It answers yes only when the proved key count is the stored key count, a key proof for the uploaded kind exists, the caller holds the device lock, no recorded work writes the keys, the guest wrote the key range since the proof (`GuestMemory::CpuWroteSince`, new: the uncached collect and CPU-stamp compare of `UnchangedSinceCollected`, answering "no" where it cannot tell), and the keys read again as another clear code (0000, 0001, 1110, 1111 or the register clear). Then the store is left out and the guest's clear stands. Limits: - A partial rewrite keeps the store, as main does when its lookup misses the write. When main sees a partial rewrite it reads the mixed keys as uncompressed and stores nothing, so the guest's keys stay; for a given image, texture reads take its stored texels whether the keys are uncompressed or mixed. The store is kept here so that a later CB metadata pass does not meet mixed keys, which it refuses ("per-block metadata is not modeled"). Neither result is the per-key one of the order "clear after the draw", the guest's code on the keys it wrote and uncompressed keys on the rest; a store per key is not attempted here. - Keys replaced by the single-colour kind keep the store, and so do surfaces whose stored key count exceeds `guestBytes / 256` (single-level pipe-aligned R64KBX with 4-byte elements). For element sizes that do not divide 256 main reads single-colour keys as uncompressed and leaves them in place where it sees them; here the store covers them. - Register clear keys left in memory are read as on main: texture reads take the stored texels, and only the colour-target path applies the guest's clear. - The check needs a write-watched key range. Keys in host-imported memory that is compared, not watched, have no proof generation and keep the store. - The three write-back stores share the check and have no step of their own: in the steps `MarkDirty` has already taken the image out of its clear kind when the write-back runs. - `ProvedCurrentDccKeys` (the register clear's proof) has the same window and is not changed here. Cost: one uncached collect of the key range per first write, and per write-back store that finds the image still under its clear kind, of an image that has a proof for its uploaded kind and no key tail; a read of the keys only when that collect finds a CPU write. Related: #6209 (open) changes `ProvedCurrentDccKeys` and `ProvedClearKeys`, which this PR does not touch. #6186 (open) keeps CPU stores made after a deferred key store was recorded, the window after this one. ### Tested Head 5287cda761 is this PR's three commits on upstream/main 3bd88ca119 (the third, test only, came after review: see the thread). The runs below are clean merges onto main 9f2b939606: main + the two test commits, main + the head, and main + the head + the head of #15456. - Linux (CachyOS, kernel 7.2.10), Release, GCC 16.2.1, write watch available. `ctest -V -R '^agc_driver_dcc_key'` with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_NO_SHADER_CACHE=1 ANYPS5_REQUIRE_VULKAN=1`, on an AMD Radeon RX 9070 XT (RADV GFX1201, Mesa 26.2.4) and on lavapipe (llvmpipe, LLVM 23.1.1, Mesa 26.2.4). The pattern matches the six new steps and the two existing key tests, `agc_driver_dcc_keys_first_write` and `agc_driver_dcc_key_presync`. - main + the two test commits: on both drivers the four rewrite steps fail with the lines above, and they are failures, not skips; no step stops at a path check; the two keep-store steps and the two existing tests pass. No `VUID-`. - main + the head: all eight pass on both drivers, none skipped. No `VUID-`. - main + the head + the head of #15456 (6330546a47, where the draw pipeline is on unless `APS5_NO_DRAW_PIPELINE` is set): all eight pass on both drivers, none skipped. No `VUID-`. - On both drivers and all three trees the loader's debug log shows `VK_LAYER_KHRONOS_validation` inserted; no log reports `was not found but was requested by env var VK_INSTANCE_LAYERS`. - `ctest -R '^agc'` on the RX 9070 XT, without the validation layer, main + the head: 1 of 1352 fails, `agc_driver_tessellation` (segmentation fault), and 23 tests skip. On the earlier head b04dc41f0a over main 3bd88ca119: 5 of 1349 failed with the test commit alone and 1 of 1349 with the fix; the four rewrite steps were the only difference in the failed list, with the same failing test and the same 23 skipped names as now. - Windows: not run by me. On b04dc41f0a the epoch step failed there (JJenkx's run in this thread), by the code because the test read a header-defined counter across the library boundary; 5287cda761 reads through the library instead. - `python3 tools/check_conventions.py --base upstream/main`: passes. No title effect is claimed. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 9070 XT (RADV GFX1201), Mesa 26.2.4, Linux (CachyOS, kernel 7.2.10); lavapipe (llvmpipe, LLVM 23.1.1), Mesa 26.2.4, same host - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
milescoviello commented 2026-11-06 16:55:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-06T16:55:09Z.

Review: COMMENTED


One interaction with #15456 (the draw finisher by default), which edits neither file, so nothing will flag it as an overlap. On #15456's head ShaderResources::EpochProofs() returns DrawPipelineEnabled(), and that is true unless APS5_NO_DRAW_PIPELINE is set. Once it lands, every mode here runs with epoch proofs. --rewrite-after-memo and --rewrite-after-refresh-proof can then be answered by the epoch proof instead of the memoized collect or the refresh's own proof. Their names and the description would no longer match the path they test, while --rewrite-after-epoch-proof still passes. Setting APS5_NO_DRAW_PIPELINE=1 on the five non-epoch modes (and dropping APS5_DRAW_PIPELINE=1 from the epoch mode) would keep each mode on its own path, whichever of the two lands second.

The rest reads right to me: keysReplacedByClear only keeps the store off when the guest wrote the keys since the key proof's generation, nothing recorded is pending over them, and what they hold now is a different clear code.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5803822526 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16411#pullrequestreview-5803822526). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-06T16:55:09Z. **Review:** COMMENTED --- One interaction with #15456 (the draw finisher by default), which edits neither file, so nothing will flag it as an overlap. On #15456's head `ShaderResources::EpochProofs()` returns `DrawPipelineEnabled()`, and that is true unless `APS5_NO_DRAW_PIPELINE` is set. Once it lands, every mode here runs with epoch proofs. `--rewrite-after-memo` and `--rewrite-after-refresh-proof` can then be answered by the epoch proof instead of the memoized collect or the refresh's own proof. Their names and the description would no longer match the path they test, while `--rewrite-after-epoch-proof` still passes. Setting `APS5_NO_DRAW_PIPELINE=1` on the five non-epoch modes (and dropping `APS5_DRAW_PIPELINE=1` from the epoch mode) would keep each mode on its own path, whichever of the two lands second. The rest reads right to me: `keysReplacedByClear` only keeps the store off when the guest wrote the keys since the key proof's generation, nothing recorded is pending over them, and what they hold now is a different clear code. AI-assisted: yes (Claude Code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!16411
No description provided.