Skip to content

fix(agc): advance stored layers when a write-back keeps CPU-written blocks - #4663

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
fdfawef324:fix/agc-settle-stored-units
Oct 14, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
fdfawef324:fix/agc-settle-stored-units

Conversation

@fdfawef324

@fdfawef324 fdfawef324 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What

On a host-imported surface a storage write-back keeps the 64 KiB blocks that the CPU wrote since their tracked layer's generation, stores the rest, and stamps the stored ranges with MarkWritten. Settling advanced the stored layers' generations past those stamps only when no layer kept a block (or when an adjacent pending surface needed the collect).

So when one tracked layer kept a block, every other stored layer kept its old generation, and the write-back's own stamps then read as CPU writes: the next write-back kept those layers as well and did not store the GPU's newer results over them.

A tracked layer is one 64 KiB unit when the surface has block units: tiled (not linear), 64 KiB-aligned base, not thick, with layer and block tracking on. Otherwise it is an array layer (64 KiB-aligned layer slices, layer tracking on) or the whole surface. With a single tracked layer, a layer that kept a block is the only stored layer, so nothing advances and settling behaves as on main.

Settling now collects and advances each stored layer that kept no block. A layer that kept a block keeps its old generation and re-uploads at its next use, as before. When no stored layer advances (nothing stored, or every selected layer kept a block), no adjacent pending surface needs the collect and no unit was retiled into the unit shadow, settling still skips the collect, as on main.

MarkShadowed now takes the settle collect's generation also when a block was kept; main passed TrackerGeneration() there without collecting. The two differ only for a CPU write to a stored unit made after the keep decision: on main a later walk stamped it newer, so the retiled unit went stale and the CPU's bytes won; now this walk stamps it at the marked generation and the stored results win, as they already did when no block was kept.

agc_storage_kept_unit_settle uses a 1024x512 RGBA8 surface in 64 KiB tiling (32 block units). It writes one byte of unit 0 from the CPU, stores a red clear (unit 0 is kept, as intended), then stores a blue clear, and requires units 1-31 to be blue and unit 0 to keep the CPU's byte after both stores. It runs on GPU-mapped direct memory (the host-import path, where that import keeps the memory write-watched) and on watched memory with host imports off (the write-back that stores through the CPU). On main the direct-memory case fails:

AGC graphics: (k) GPU-mapped direct memory: unit 1 kept the results of the earlier write-back; that write-back's own store was taken for a CPU write because a neighbouring unit was kept for the CPU

The watched-memory case passes on main. It guards the kept layer's old generation on the path that stores through the CPU, which this change also makes collect and advance when a unit is kept: if settling advanced the layer that kept a block too, the watched case fails (the second write-back stored over unit 0, which the CPU wrote after that unit's generation).

The watched-memory case is required and passes independently when the optional direct-memory subcase is unavailable. Unsupported direct imports still print the specific subcase reason and continue. A missing host write watch or any readback failure fails the test. The suite no longer reports all of its coverage skipped merely because the direct-memory subcase could not run.

Found while tracing the storage corruption in Demon's Souls (PPSA01342 v1.005). On its own this change shows no visible difference there: the green band in that title's movies comes from shared write tracking, fixed in #4661. This PR fixes the write-back bookkeeping that the test above exercises.

Tested

Windows Server 2025, RTX 3060, GCC 15.2 (Release). The branch is rebased onto main at 14db084; the tests below ran on the same commit rebased onto 84659e1 (git range-diff shows it unchanged). Main between the two adds the 3rdparty/libusb-cmake submodule, which this worktree's build directory could not configure without initialising, and changes no libSceAgcDriver file.

  • agc_storage_kept_unit_settle: passes with this change. With only the Texture.cpp change reverted, the direct-memory case fails with the message above. With settling also advancing layers that kept a block, the watched-memory case fails (checked on the earlier base 0142215).
  • ctest -R agc -j 4 (446 tests): 436 pass and agc_driver_stencil_ref_export skips. 9 fail, and each also failed with only the Texture.cpp change reverted (checked on the earlier base 621ce1d): agc_driver_flat_loads, agc_driver_flat_loads_x2_x3, agc_driver_flat_loads_x4, agc_driver_global_loads, agc_driver_global_loads_x2_x3, agc_driver_global_loads_x4, agc_driver_flat_apertures_2, agc_driver_flat_apertures_5 (abort with "guest allocation ... is still pinned by GPU work or an import") and agc_driver_pixel_interlock (vkDeviceWaitIdle returns -4).
  • tools/check_test_registration.py: passes.
  • Not run in this round: the full ctest outside agc, and any title or homebrew.

Follow-up for Linux CI coverage: ca73d0f changes only the fixture's completion policy; the production code and GPU readback body are unchanged. The entire matching-header Recorder.cpp and unchanged Texture.cpp pass GCC -O3 syntax checks. A CPU replay of the literal completion selector/catch/Require, with explicit observation callbacks replacing GPU work, reports old exit 77 versus corrected exit 0 after watched success/direct unavailability; the unchanged Linux check_skips.py reports lost coverage before and passes afterward. Injected watched/direct failures still fail. This is a completion-policy check, not a fresh Linux or native GPU rerun; the published CI run must verify lavapipe rendering coverage.

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
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 3060 12 GB, driver 591.86 (32.0.15.9186), Vulkan 1.4.325, Windows Server 2025 10.0.26100
  • AI-assisted: yes

🤖 Generated with Claude Code

Co-Authored-By: Codex noreply@openai.com

@github-actions github-actions Bot added build agc libSceAgc* labels Oct 12, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@fdfawef324
fdfawef324 force-pushed the fix/agc-settle-stored-units branch from b624363 to c6b403d Compare October 12, 2026 21:49
@fdfawef324 fdfawef324 changed the title fix(agc): advance the units a write-back stored when it keeps another fix(agc): advance stored layers when a write-back keeps CPU-written blocks Oct 12, 2026
@fdfawef324

Copy link
Copy Markdown
Contributor Author

Changes in this revision (rebased onto current main at 14db084, still one commit; the conflict in tests/Recorder.cpp with --integer-clear-only keeps both options):

  • Block units are now defined as in Texture.cpp: tiled (not linear), 64 KiB-aligned base, not thick, with layer and block tracking on; otherwise a tracked layer is an array layer (64 KiB-aligned layer slices, layer tracking on) or the whole surface. Fixed in the commit message and the body.
  • settle returns before the collect when no stored layer advances (nothing stored, or every selected layer kept a block, e.g. a single tracked layer that kept one), no adjacent pending surface needs it and nothing was retiled into the unit shadow. This replaces the keep.empty() check and drops the walk that advanced nothing; a cost change, not a correctness one.
  • The body and message now say that MarkShadowed takes the settle collect's generation instead of TrackerGeneration() when a block was kept, and when the two differ (a CPU write to a stored unit after the keep decision: the stored results now win, as when no block was kept).
  • Test helper: Backing's destructor releases the direct memory whenever it was allocated, also when mapping it failed.
  • Red/green: with the Texture.cpp change reverted the direct-memory case fails at unit 1; with it both cases pass. The suite ran on base 84659e1 (same commit per git range-diff); the newest base adds the libusb-cmake submodule, which my build directory could not configure.
    No finding was refuted in this round.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@kr4y4

kr4y4 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

An Intel run, and the full suite, which the description lists as not run.

Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU and an Intel Raptor Lake-S iGPU. The head c6b403d merged into main 495bf91, which passes 1023 of 1023 on both GPUs.

  • agc_storage_kept_unit_settle and the other 16 recorder entries pass on both GPUs with --repeat until-fail:3.
  • Full suite (ctest --timeout 120 --parallel 4): 1024 of 1024 on both GPUs. That includes the nine that fail on your machine with and without this change (agc_driver_flat_loads and _x2_x3, _x4; agc_driver_global_loads and _x2_x3, _x4; agc_driver_flat_apertures_2, _5; agc_driver_pixel_interlock). They pass on both GPUs here.
  • With Texture.cpp as on main, agc_storage_kept_unit_settle fails on both GPUs with the direct-memory message from the description ((k) GPU-mapped direct memory: unit 1 kept the results of the earlier write-back; ...). The other 16 pass.

Written with AI assistance.

@nicolasfalesy

Copy link
Copy Markdown
Contributor

Linux test (Arch, GCC 16.2.1, Release, Intel Arc B390 on Mesa 26.2.2 ANV), head c6b403d71 merged alone onto main 7e181fc (clean merge): agc_storage_kept_unit_settle fails on the direct-memory case, with the same message your description gives for the unfixed code:

units stored next to a unit kept for the CPU in watched memory: ok
AGC graphics: (k) GPU-mapped direct memory: the second write-back stored over unit 0, which the CPU wrote after that unit's generation

The watched-memory case passes. On this machine the write-watch probe reports dma-buf imports keep write protection: yes and host imports resolve write protection: yes, so GPU-mapped direct memory goes through the dma-buf import path here rather than Windows' host import, which may be why the Texture.cpp change doesn't reach it. Happy to rerun with any trace switch.

@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.

Checked on Linux (RADV, Radeon 660M): the PR merged onto main builds, agc_storage_kept_unit_settle passes, and it fails with only the Texture.cpp change reverted, so the test does exercise the change there. libSceAgcDriver ctest otherwise matches main (agc_driver_tessellation segfaults on main too).

  • However, @nicolasfalesy reports that on Linux with Intel Arc (ANV, Mesa 26.2.2) the direct-memory case fails with this PR: the second write-back stored over unit 0, which the CPU wrote after that unit's generation. That is the failure that the watched-memory case guards against (a layer that kept a block must keep its old generation). There the probe reports dma-buf imports keep write protection: yes, so direct memory takes the dma-buf import path, not the host-import path the description targets. We can't reproduce it on RADV, so please find out why that path advances (or stamps) unit 0's generation, and either fix it or restrict what the test claims. A test that fails on a Linux driver can't be merged.
  • Windows runs by @kr4y4 (RTX 4070 and Intel iGPU, full suite) pass, so this looks specific to the Linux dma-buf path.

Happy to re-review once the Linux/ANV case passes.

@fdfawef324
fdfawef324 force-pushed the fix/agc-settle-stored-units branch from c6b403d to dd97094 Compare October 13, 2026 15:11
@fdfawef324

Copy link
Copy Markdown
Contributor Author

Thanks for the Linux run. I traced it, and the failure doesn't come from a dma-buf write-back path. Your direct memory never reached the dma-buf import.

  • With host imports resolve write protection: yes and dma-buf imports keep write protection: yes, importAllocation (GuestBufferMemory.cpp:759-774) tries the host-pointer import first. That attempt runs in a window that doesn't stay watched. dma-buf is tried only if the host-pointer import fails. On ANV the host-pointer import of shared direct memory succeeds, so ImportWatched (GuestMemory.cpp:1216-1219) takes the surface out of the write watch.
  • The test checked the write watch before it made that import, so the direct case ran on the compared path:
    • The upload saves the guest bytes.
    • The first write-back keeps unit 0 by comparison and drops the saved bytes of every selected unit (Texture.cpp:4166).
    • The second write-back has nothing to compare.
  • On your base (7e181fc, before e544993) that store wrote every unit whole, over unit 0. That's your message. On current main the same store throws storing storage image results pending over memory outside the write watch ... not implemented, which no technical-debt entry covers yet. The existing entry only covers results over a range that left the watch after they became pending. Here the range was compared from before the upload, and the saved bytes are gone after the first GPU-direct store. That needs its own PR.
  • None of this depends on this PR's Texture.cpp change, because the compared path has no generations to settle. I reproduced both outcomes on Windows by importing private watched memory (Windows compares that import):
    • On 7e181fc, with and without this PR's change, the run fails with your message.
    • On main the run throws.

What changed: the branch is rebased onto f2c314e with one extra commit. The direct case now checks the write watch again after the import. If the import took the memory out of the watch, it prints the host import of the direct memory took it out of the write watch (its imports are compared): ... not tested and the test returns 77. Where the import keeps the memory watched (the Windows alias import, or a Linux dma-buf import when the host-pointer import fails, as with shared memory on RADV), the case runs as before. That path uses the same write-back and settle code as Windows.

Could you rerun ctest -R '^agc_storage_kept_unit_settle$' -V on the new head? ctest prints nothing for a skipped test without -V. I expect units stored next to a unit kept for the CPU in watched memory: ok, the new skip line, and the test reported as skipped. A different result would mean my reading is wrong somewhere. If you also have an AMD or other non-ANV Linux machine with /dev/udmabuf access, a run there would cover the dma-buf case.

Written with AI assistance.

@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.

Checked on Linux (RADV): the PR merged onto main builds, agc_storage_kept_unit_settle passes, and it fails with only the Texture.cpp change reverted. The remaining libSceAgcDriver ctest difference vs main is agc_driver_tessellation (segfaults on main too). The ANV case from @nicolasfalesy is now skipped (77) when the host import takes the memory out of the write watch, which matches the trace in your reply; the compared-path store throwing on main is a separate follow-up. Windows full-suite runs by @kr4y4 pass. Merging once the full Build is green.

@fdfawef324
fdfawef324 force-pushed the fix/agc-settle-stored-units branch 3 times, most recently from 3b69ed3 to cedf5f6 Compare October 13, 2026 21:56
@github-actions

github-actions Bot commented Oct 13, 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
#5522 core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp L4474-4485

fdfawef324 and others added 2 commits October 13, 2026 22:43
…locks

On a host-imported surface a storage write-back keeps the 64 KiB blocks
the CPU wrote since their tracked layer's generation, stores the rest
and stamps the stored ranges with MarkWritten. Settling advanced the
stored layers' generations past those stamps only when no layer kept a
block (or when an adjacent pending surface needed the collect). When one
tracked layer kept a block, every other stored layer kept its old
generation, so the write-back's own stamps read as CPU writes: the next
write-back kept those layers as well and did not store the GPU's newer
results over them.

A tracked layer is one 64 KiB unit when the surface has block units:
tiled (not linear), 64 KiB-aligned base, not thick, with layer and block
tracking on. Otherwise it is an array layer (64 KiB-aligned layer
slices, layer tracking on) or the whole surface.

Settling now collects and advances each stored layer that kept no
block; a layer that kept a block keeps its old generation and
re-uploads at its next use, as before. When no stored layer advances
(nothing stored, or every selected layer kept a block, such as a single
tracked layer that kept one), no adjacent pending surface needs the collect
and no unit was retiled into the unit shadow, settling still skips the
collect, as main did. Units retiled into the unit shadow are now marked
at the settle collect's generation also when a block was kept; main
marked them at TrackerGeneration() there without collecting. The two
differ only for a CPU write to a stored unit made after the keep
decision: main left it to a later walk, which made the retiled unit
stale so the CPU's bytes won; now the walk stamps it at the marked
generation and the stored results win, as they already did when no
block was kept.

agc_storage_kept_unit_settle uses a 1024x512 RGBA8 surface in 64 KiB
tiling (32 block units): it writes one byte of unit 0 from the CPU,
stores a red clear, stores a blue clear, and requires units 1-31 to
hold blue and unit 0 to keep the CPU's byte after both stores. On main
the GPU-mapped direct memory case fails with unit 1 still red. The
watched-memory case (host imports off: the write-back that stores
through the CPU) passes on main; it guards the kept layer's old
generation and fails when settling also advances a layer that kept a
block. The test returns 77, naming the reason, when the direct-memory
case cannot run: no write watching, no host imports, direct memory not
write-watched, or its host import refused.

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

agc_storage_kept_unit_settle checked that the GPU-mapped direct memory
was write-watched before it made the host import. On Linux, when host
imports resolve write protection and dma-buf imports keep it, the
host-pointer import is tried first in a window that does not stay
watched, and dma-buf only when it fails (importAllocation in
GuestBufferMemory.cpp). Where the host-pointer import of shared memory
succeeds, as on Intel's ANV, the import takes the direct memory out of
the write watch, so the case ran on the compared path: the upload saves
the guest bytes, the first write-back keeps unit 0 by comparison and
drops the saved bytes of every unit it selected, and the second
write-back has nothing to compare. Before e544993 it stored every
unit whole, over the CPU's byte in unit 0, which is the failure
reported on Linux; on main it throws (technical debt: results over
memory outside the write watch without saved bytes). Neither depends
on the settle change. On Windows, importing watched private memory
(compared there) reproduces both: on 7e181fc, with or without this
branch's Texture.cpp change, the second check fails with that message;
on main the second write-back throws.

The case now checks the write watch again after the import and returns
77, naming the reason, when the import took the memory out of it. Where
the import keeps the memory watched (the Windows alias import, a Linux
dma-buf import) it runs as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fdfawef324
fdfawef324 force-pushed the fix/agc-settle-stored-units branch from 841ca00 to 7bfeeb9 Compare October 14, 2026 02:49
@oneandonlydean

Copy link
Copy Markdown
Collaborator

The full Build is red on this head, on Linux only: tools/check_skips.py fails with agc_storage_kept_unit_settle skipped (run 38513427247). On CI's lavapipe the test now returns 77, and the reason it prints isn't one of the device limitations the check accepts, so the job counts it as lost coverage.

Could the direct-memory case skip on its own and let the rest of the test run (and pass) there? If the remaining skip really is a device limitation, word it the way check_skips.py expects ("skipped, the device ..."); otherwise the case needs to run on lavapipe.

…ilable

Co-Authored-By: Codex <noreply@openai.com>
@fdfawef324

fdfawef324 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Fixed in ca73d0f. The watched-memory case now runs and passes independently; an unavailable direct-memory subcase only prints its own reason. Missing host write watching and all readback failures remain failures, so this does not turn required coverage into a skip.

Verification: full matching-header Recorder.cpp and unchanged Texture.cpp pass GCC -O3 syntax checks. The literal completion-policy CPU replay reproduces old exit77/check_skips failure and corrected exit0/check_skips success; injected watched/direct failures still fail. The GPU readback body and production code are unchanged. This follow-up has not been rerun on Linux or on a native GPU; CI must confirm lavapipe coverage.

On ca73d0f, conventions run 38533264055, eligibility and report pass. GitHub retains cancelled duplicate conventions run 38533264336; rerunning it requires repository admin rights. The test-only Build gate skipped Linux/Windows, so a maintainer Build dispatch with pr=4663 is needed for a fresh lavapipe result.

GPUs tested: NVIDIA GeForce RTX 3060 12 GB, driver 591.86 (32.0.15.9186), Vulkan 1.4.325, Windows Server 2025 10.0.26100 (prior production-fix/readback evidence in the PR; this follow-up uses source and CPU checks only).

AI-assisted: yes

Co-Authored-By: Codex noreply@openai.com

@adriadam10 adriadam10 self-assigned this Oct 14, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator

Re-checked the new commit ca73d0f: the watched-memory case now runs and passes on its own when the direct-memory subcase is unavailable, so lavapipe no longer returns 77 for the whole test (the check_skips.py failure from the last Build). Production code is unchanged. On Linux (RADV) the PR merged onto main builds, agc_storage_kept_unit_settle passes, and it fails with only the Texture.cpp change reverted. Merging once the full Build is green.

@adriadam10
adriadam10 merged commit e9817cd into boykopovar:main Oct 14, 2026
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants