Skip to content

fix(agc): store uncompressed DCC keys at the first write of a fast-cleared image - #903

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
Celegans12:fix/agc-dcc-keys-on-first-write
Oct 7, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
Celegans12:fix/agc-dcc-keys-on-first-write

Conversation

@Celegans12

@Celegans12 Celegans12 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

What

A storage image uploaded under fast-clear DCC keys kept the clear code in memory until its write-back, so a kernel reading the keys after a write to the image still read it.

Details

The splash draw (queue 0 +6617) writes a 3840x2160 image at 0x5005160000; the retile kernel (+6693) copies its keys (0x5007140000), still 0x00. The store covers 0x1fe00 of its 0x28000 keys.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0, based on main ee391a5: configure -DBUILD_TESTING=ON, full build, --target libs, ctest:
  • 369/369 tests pass (ctest --output-on-failure)
  • Same build on lavapipe (Mesa 26.2.4 llvmpipe, 8-wide subgroups, VK_DRIVER_FILES): 368/368 pass; agc_driver_flat_store is left out there because it takes several minutes on Windows llvmpipe
  • New agc_driver_dcc_keys_first_write (plain and write-watched memory): a key copy after a first write reads 0xff; a pending 0x10 fill skips the wait, 0000 fills take it and the refresh clears the image; a write marked after a 0001 key fill, before a refresh, keeps 0x40. Fails without the store or the fill check; does not build on main.
  • agc_driver_recorder_tests (not in ctest) passes on NVIDIA; its key fills now write the keys.
  • PPSA26344 v1.008 with fix(agc): store and read DCC keys over the console's metadata extent #788: 696/700 flips show the logos (black before) on an earlier revision; this one gives the same frames, with recorded-store syncs 3.0 to 1.4 per frame.
  • Not tested: Linux (CI), console behaviour.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

@github-actions github-actions Bot added the agc libSceAgc* label Oct 6, 2026
@Celegans12
Celegans12 force-pushed the fix/agc-dcc-keys-on-first-write branch from a976f1e to c99a7be Compare October 6, 2026 22:47
@NahuelGenchi

Copy link
Copy Markdown
Contributor

Tested c99a7be on an R9 380 (Tonga, GFX8, RADV, Mesa 26.0.8, subgroup 64), Ubuntu 26.04, GCC 15.2, Release build.

The new agc_driver_dcc_keys_first_write test fails here every time (5 out of 5 runs, also with ANYPS5_NO_SHADER_CACHE=1). The kernel rejects the command stream ("radv/amdgpu: The CS has been rejected, see dmesg for more information (-14)"), vkQueueSubmit then returns -4 (device lost) and the test ends with "the refresh after the one that cleared the image uploaded it again". Right before that it logs "has mixed DCC keys ... its texels are read as stored" for the 128x128 format 56 storage image.

The same binary passes on llvmpipe on this machine, so it looks specific to RADV on GCN. The rest of the suite passes (367 of 368, lod_clamp skipped since the card has no shaderResourceMinLod). I also ran all agc_driver_* binaries under the Khronos validation layer with sync validation on and got no errors or hazards, the same as main. I couldn't read dmesg on this box, so I don't have the kernel side of the rejection. Happy to run anything else on this card if it helps.

@Celegans12
Celegans12 force-pushed the fix/agc-dcc-keys-on-first-write branch from c99a7be to a8ba1c5 Compare October 7, 2026 00:33
@Celegans12

Copy link
Copy Markdown
Contributor Author

Thanks for running this on the 380. Your log was enough to find it, and I'm fairly confident it's a bug in my test, not in GCN or DCC. When the device starts on Linux, the driver checks whether the kernel driver writes to imported pages, and amdgpu does: it re-faults userptr pages for write on every submission. So on any amdgpu card, host imports are taken off the write watch. My test decided its second block was write-watched before that block got imported, so its last check expected a second refresh to skip the upload, which only holds while the watch is on. I get your exact message on NVIDIA when I run that check on unwatched memory.

I'm less sure about the CS rejection, but it looks like a side effect. The failed check skipped the final wait, and the test had already freed its memory when the device was torn down. The teardown then submitted the leftover work with userptr BOs whose pages were gone, which amdgpu rejects with -EFAULT.

a8ba1c5 fixes the test. It checks the watch again once its block is imported, as the storage refresh test in Recorder.cpp does, and it frees its memory after the device. On amdgpu, the second pass now runs as unwatched. Could you run it once on that head?

ctest --test-dir build -R 'agc_driver_dcc_keys_first_write$' -V 2>&1 | grep -E 'write-watch\]|host imports are compared|teardown|tests passed'

I'd expect three lines:

  • "host imports resolve write protection: yes" from the probe;
  • "host imports are compared, not watched: the write-watched block runs as unwatched";
  • "DCC keys first write tests passed".

There shouldn't be a teardown line.

Details
  • The check that failed is the last one in the write-watched pass (DccKeysFirstWrite.cpp, the if (watched) block after the 0000 fills). The plain pass had already finished.
  • The "mixed DCC keys ... read as stored" warning is expected. The test stores 0000 over half the keys on purpose, and NVIDIA prints the warning for both passes too. On your run it was just the last driver line before the failure.
  • The import probe (decideImportWatch in GuestBufferMemory.cpp) runs at device creation. After a "yes", every import calls GuestMemory::Unwatch first. An unwatched block can't prove it is unchanged, so StorageTexture::Refresh uploads again.
  • On NVIDIA the probe reads 0 of 4 pages, so imports stay watched there. My understanding is that llvmpipe just uses the host pages as they are, so its imports stay watched too, which is why the test passed there.
  • The -4 comes from [gpu] unit shadow teardown: AGC graphics: vkQueueSubmit recorder: Vulkan result -4. ~VulkanDevice runs PublishAllShadows and recorder->Sync(), and imports are only retired lazily, so both BOs were still in that submission. dmesg probably won't show anything: as far as I know, amdgpu logs this failure only at debug level.
  • To check without rebuilding, on c99a7be:
    • with APS5_WRITE_WATCH_IMPORTS=unwatch VK_DRIVER_FILES=/usr/share/vulkan/icd.d/lvp_icd.x86_64.json, llvmpipe should fail the same way, without the CS rejection;
    • with APS5_NO_WRITE_WATCH=1, the test should pass on RADV.
  • The same teardown pattern could hit a game on RADV that unmaps imported guest memory while work is still queued. That would be a separate issue on main, not part of this PR.

@github-actions

github-actions Bot commented Oct 7, 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
#1202 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp L1465, L1507

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran it merged onto current main on RADV (RDNA2, amdgpu): build + ctest 401/401. agc_driver_dcc_keys_first_write also passes on CI's lavapipe. On amdgpu it takes the "host imports are compared, not watched" path and passes, so the R9 380 failure looks fixed by a8ba1c5 (@NahuelGenchi, a rerun on GCN would confirm it).

Negative checks: the new execution test fails without the MarkDirty store, without the wait in ProvedKeys, without the fill value in FillBuffer, and without the value on the key store. Two gaps:

  • tests/Recorder.cpp:2702-2735: agc_driver_graphics still passes with every non-test change of the PR reverted to main, and also with only the MarkDirty store disabled. Its draw doesn't reach StorageTexture::MarkDirty, so these new checks don't test this change. Please make them reach the storage-image first write, or drop them.
  • Texture.cpp:2105: nothing covers the !IsDccClear(filledKeys) guard. With it removed, both tests still pass. Recorder.cpp:2733-2735 looks meant to cover it, but see the point above. A case in DccKeysFirstWrite.cpp would pin it: a 0x40 key fill after the clear, then a storage write, and the keys must still read 0x40.

@Celegans12
Celegans12 force-pushed the fix/agc-dcc-keys-on-first-write branch from a8ba1c5 to 124375c Compare October 7, 2026 21:13
@Celegans12

Copy link
Copy Markdown
Contributor Author

Thanks for the negative checks; they're what found both gaps. I've pushed 124375c.

  • Recorder.cpp checks: they did call MarkDirty, but they sit in agc_driver_recorder_tests, which ctest never builds or runs, so they tested nothing in CI. I dropped them and left that coverage to agc_driver_dcc_keys_first_write, which fails without the store.
  • The guard: your 0x40 case is now at the end of DccKeysFirstWrite.cpp, and with !IsDccClear(filledKeys) removed it fails on NVIDIA and lavapipe. One difference from what you suggested: a dispatch right after the fill refreshes the image first, which clears it again under 0001 and then correctly stores 0xff. So the case marks the write before any refresh has seen the fill, which is the only path that reaches the guard.

While testing this I found one sequence the guard still gets wrong: a 0x40 fill, then a kernel storing 0000 over the keys, then a storage write. On write-watched memory the refresh sees 0000, which matches the keys the image was uploaded under, so FilledKeys keeps the stale 0001 and the first write skips its store; the reader then sees the clear colour. It isn't a regression against main, which never stores at the first write, but it's the same kind of bug this PR is for, so I'll push a fix (drop filledKeys when a refresh sees keys that differ from the fill) with a test for that sequence here.

Details

Without the store: a kernel reading the keys after the first write of a 0000-cleared surface: 0 of 256 key bytes are 0xff (key 0 is 0x00)

Without the guard: a write marked after a 0001 key fill over a cleared image, before a refresh saw the fill: 0 of 256 key bytes are 0x40 (key 0 is 0xff)

Both fail the same way on an RTX 3080 and on llvmpipe. The merge with current main builds, and the DCC, image and graphics tests pass on both.

Celegans12 and others added 2 commits October 7, 2026 23:24
…eared image

A storage image uploaded under fast-clear DCC keys left the clear code
in guest memory until its write-back, so a title kernel that read the
keys after a draw or dispatch into the image saw the clear code.
PPSA26344's DCC retile kernel copies such keys into its display
buffer's metadata every frame, and the flip presented the clear color.

MarkDirty now stores uncompressed keys the first time an image uploaded
under a clear code is written, unless the title filled the keys with a
clear code since, and the image's uploaded keys become uncompressed.
While they are, Refresh and the fast revalidation read the keys as the
recorded work writing them leaves them: from the bytes pending uniform
fills and driver key stores note (NotePendingFill) when those already
make the keys read uncompressed (bytes that differ, 0xff, or a byte the
format can't use as a clear code), otherwise after waiting for that work.
@adriadam10
adriadam10 force-pushed the fix/agc-dcc-keys-on-first-write branch from 124375c to cfaa336 Compare October 7, 2026 21:26

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked 124375c merged onto current main on RADV (RDNA2): build + ctest 407/407. Both earlier points are addressed.

Mutation checks on the new test: it fails without the !IsDccClear(filledKeys) guard, without the first-write store, without the fill value in FillBuffer or on the key store, and when WaitForKeyWriters always or never waits. Two mutants survived, so I added a commit with two rows in DccKeysFirstWrite.cpp:

  • a pending 0x10 fill overwritten by a pending 0000 fill reads 0000 after waiting (kills sorting PendingFillsLeaveUncompressed oldest-first);
  • a pending 0000 fill over the first key half, with 0x10 already in memory in the other half, reads uncompressed without waiting (kills ignoring the keys in memory that no pending fill covers).

Rebased onto main, build + ctest green. Merging once CI is green.

@Celegans12

Copy link
Copy Markdown
Contributor Author

Thanks for the two extra rows, both are good catches. I'll leave this branch as it is so it can go in, and send the stale FilledKeys fix I mentioned as a separate follow-up PR once this is merged, with the test for that sequence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants