fix(agc): keep fused geometry programs out of the caller's memory #5573

Merged
fma-rc merged 1 commit from upstream-pr/5573 into main 2026-10-14 06:25:23 +00:00
fma-rc commented 2026-10-14 00:10:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-14T00:10:31Z.


What

Refs #5099. Stray (PPSA02100) and Kena (PPSA01746) stop on current main with

AGC driver: guest memory is not readable at 0xdfac00000008 (range 0xdfac00000008+0x4)

The draw is a fused geometry shader. sceAgcFuseShaderHalves copied both halves' code, the fused header and its tables into the caller's memory and registered the program at that address. Stray fuses different halves into the same memory a few calls apart while draws that use the earlier result are still queued. The registry keeps one program per code address, so those draws were compiled from the later program, whose code reads a user-data word the bound layout does not have. With APS5_TRACE_SHADER_REGS=all one run shows two registrations at the same code address twelve log lines apart, with RSRC2 0x008b0018 and then 0x008b0010, and the failing draw carries the first value.

The export in the PS5 5.02 module writes only the register list into that memory: it copies the back half's header, memcpys num_sh_registers * 8 bytes, patches the checksum, RSRC1, RSRC2 and program address entries from the front half, and its size query returns num_sh_registers * 8 with alignment 4. No code goes there.

This PR keeps the fused code, header and tables in memory libSceAgc owns, one program per distinct pair of halves, registered once. The caller's memory receives the fused register list and the size query reports that list. Fusing the same halves again returns the same program; halves whose contents changed get a new one.

The driver matched a header against the registered one field by field, including the register list's address. A header whose register list is an identical copy elsewhere now matches as well, which is what a title passes after a fusion. Without that every run stops on rectangle ABI refers to a replaced shader header.

This changes where #124 put the fused program; its code layout and register merge are untouched. PR 4826 edits another function in the same file. #5384 works around the same stop by recording the unreadable root as poison; with this change the read does not happen, so the two do not depend on each other.

Limits. The fused list still appends the front half's own SH registers as #124 does, so it can be longer than the real one; a title that sizes the memory without the size query would be overrun by those entries (before this PR it was overrun by the whole program). Geometry halves without memory still throw, where the real function patches the back half's list in place. A fused result whose register list the title has overwritten no longer matches its header and throws refers to a replaced shader header; neither title does that.

Tested

Arch Linux, GCC 16.2.1, main d1670009f with this PR.

agc_shader_fusion fails on main with fused size is not the fused register list; agc_driver_prepared_shaders and its _indirect, _uncached and _debug variants fail without the ShaderRegistry.cpp change with graphics ABI refers to a replaced shader header.

test NVIDIA RADV lavapipe
agc_shader_fusion pass pass pass
agc_driver_prepared_shaders pass pass pass
agc_driver_prepared_shaders_indirect pass pass pass
agc_driver_prepared_shaders_uncached pass pass pass
agc_driver_prepared_shaders_debug pass pass pass
agc_driver_prepared_shaders_debug_failure pass pass pass
agc_driver_prepared_shaders_deferred pass pass pass
agc_driver_prepared_shaders_deferred_undecodable pass pass pass
agc_driver_prepared_shaders_registered_workgroup pass pass pass
agc_driver_prepared_shaders_registered_workgroup_sync pass pass pass

Windows Server 2025, WinLibs GCC 15.2 (no GPU): build and ctest, 1103 tests, 0 failed.

Stray, runs of 120 s on the RTX 5080, alternating builds of main 30f961266 and that commit with this PR:

build runs stopped on the line above reached 120 s
main 6 2 (at 71 s and 77.5 s) 4
this PR 6 0 6

On main d1670009f with this PR (the commit before a last change that leaves struct padding out of the cache key): Stray 4 of 4 reach 120 s, Kena 1 of 2; the other Kena run stops at 120 s on buffer descriptor uses an unsupported type, a stop Kena also has without this change. No run has BDA access failed or guest memory is not readable. On the older base Kena reached 120 s in 2 of 2 runs. I checked that the titles keep presenting; I did not compare pictures.

On Intel Arc (Meteor Lake, Mesa 26.2.3 ANV) ThatsLeo ran this head on Stray: the BDA access failed ... 0xdfac00000004 lines and the stop of presents after the calibration screen are gone, one run (https://github.com/boykopovar/AnyPS5/issues/5099#issuecomment-6159208105).

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: PPSA02100 and PPSA01746 past the stop of #5099
  • GPUs tested: NVIDIA RTX 5080 (615.78.08), AMD Radeon 610M (RADV), lavapipe, all on Arch Linux; Windows Server 2025 build and ctest without a GPU
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4851093570 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5573). Originally posted by [fma-rc](https://github.com/fma-rc) on 2026-10-14T00:10:31Z. --- ### What Refs #5099. Stray (PPSA02100) and Kena (PPSA01746) stop on current `main` with ``` AGC driver: guest memory is not readable at 0xdfac00000008 (range 0xdfac00000008+0x4) ``` The draw is a fused geometry shader. `sceAgcFuseShaderHalves` copied both halves' code, the fused header and its tables into the caller's memory and registered the program at that address. Stray fuses different halves into the same memory a few calls apart while draws that use the earlier result are still queued. The registry keeps one program per code address, so those draws were compiled from the later program, whose code reads a user-data word the bound layout does not have. With `APS5_TRACE_SHADER_REGS=all` one run shows two registrations at the same code address twelve log lines apart, with RSRC2 `0x008b0018` and then `0x008b0010`, and the failing draw carries the first value. The export in the PS5 5.02 module writes only the register list into that memory: it copies the back half's header, memcpys `num_sh_registers * 8` bytes, patches the checksum, RSRC1, RSRC2 and program address entries from the front half, and its size query returns `num_sh_registers * 8` with alignment 4. No code goes there. This PR keeps the fused code, header and tables in memory libSceAgc owns, one program per distinct pair of halves, registered once. The caller's memory receives the fused register list and the size query reports that list. Fusing the same halves again returns the same program; halves whose contents changed get a new one. The driver matched a header against the registered one field by field, including the register list's address. A header whose register list is an identical copy elsewhere now matches as well, which is what a title passes after a fusion. Without that every run stops on `rectangle ABI refers to a replaced shader header`. This changes where #124 put the fused program; its code layout and register merge are untouched. PR 4826 edits another function in the same file. #5384 works around the same stop by recording the unreadable root as poison; with this change the read does not happen, so the two do not depend on each other. Limits. The fused list still appends the front half's own SH registers as #124 does, so it can be longer than the real one; a title that sizes the memory without the size query would be overrun by those entries (before this PR it was overrun by the whole program). Geometry halves without memory still throw, where the real function patches the back half's list in place. A fused result whose register list the title has overwritten no longer matches its header and throws `refers to a replaced shader header`; neither title does that. ### Tested Arch Linux, GCC 16.2.1, `main` d1670009f with this PR. `agc_shader_fusion` fails on `main` with `fused size is not the fused register list`; `agc_driver_prepared_shaders` and its `_indirect`, `_uncached` and `_debug` variants fail without the `ShaderRegistry.cpp` change with `graphics ABI refers to a replaced shader header`. | test | NVIDIA | RADV | lavapipe | |---|---|---|---| | `agc_shader_fusion` | pass | pass | pass | | `agc_driver_prepared_shaders` | pass | pass | pass | | `agc_driver_prepared_shaders_indirect` | pass | pass | pass | | `agc_driver_prepared_shaders_uncached` | pass | pass | pass | | `agc_driver_prepared_shaders_debug` | pass | pass | pass | | `agc_driver_prepared_shaders_debug_failure` | pass | pass | pass | | `agc_driver_prepared_shaders_deferred` | pass | pass | pass | | `agc_driver_prepared_shaders_deferred_undecodable` | pass | pass | pass | | `agc_driver_prepared_shaders_registered_workgroup` | pass | pass | pass | | `agc_driver_prepared_shaders_registered_workgroup_sync` | pass | pass | pass | Windows Server 2025, WinLibs GCC 15.2 (no GPU): build and ctest, 1103 tests, 0 failed. Stray, runs of 120 s on the RTX 5080, alternating builds of `main` 30f961266 and that commit with this PR: | build | runs | stopped on the line above | reached 120 s | |---|---|---|---| | `main` | 6 | 2 (at 71 s and 77.5 s) | 4 | | this PR | 6 | 0 | 6 | On `main` d1670009f with this PR (the commit before a last change that leaves struct padding out of the cache key): Stray 4 of 4 reach 120 s, Kena 1 of 2; the other Kena run stops at 120 s on `buffer descriptor uses an unsupported type`, a stop Kena also has without this change. No run has `BDA access failed` or `guest memory is not readable`. On the older base Kena reached 120 s in 2 of 2 runs. I checked that the titles keep presenting; I did not compare pictures. On Intel Arc (Meteor Lake, Mesa 26.2.3 ANV) ThatsLeo ran this head on Stray: the `BDA access failed ... 0xdfac00000004` lines and the stop of presents after the calibration screen are gone, one run (https://github.com/boykopovar/AnyPS5/issues/5099#issuecomment-6159208105). ### Checklist - [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] Unblocks: PPSA02100 and PPSA01746 past the stop of #5099 - [x] GPUs tested: NVIDIA RTX 5080 (615.78.08), AMD Radeon 610M (RADV), lavapipe, all on Arch Linux; Windows Server 2025 build and ctest without a GPU - [x] AI-assisted: yes
MarcZX10 commented 2026-10-14 00:18:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T00:18:26Z.

Review: COMMENTED


The overwrite diagnosis is compelling, and the scratch-reuse regression covers the original failure mode. One lifecycle question: FusedPrograms retains a full key copy and fused storage for every distinct half pair until process shutdown. Do the title runs show that the number/total size of distinct fusions stays bounded during a longer session? If not, this changes reusable caller scratch into process-lifetime storage and could accumulate across dynamically generated shader pairs.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5511598624 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5573#pullrequestreview-5511598624). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T00:18:26Z. **Review:** COMMENTED --- The overwrite diagnosis is compelling, and the scratch-reuse regression covers the original failure mode. One lifecycle question: `FusedPrograms` retains a full key copy and fused storage for every distinct half pair until process shutdown. Do the title runs show that the number/total size of distinct fusions stays bounded during a longer session? If not, this changes reusable caller scratch into process-lifetime storage and could accumulate across dynamically generated shader pairs.
MarcZX10 commented 2026-10-14 00:41:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T00:41:15Z.


Linux validation on PR head d6f67d1be2ef7dc7cc0e9167cb7fc79197e381cc (GCC 12.2, CMake/Ninja Release, BUILD_TESTING=ON): built agc_shader_fusion_tests and ran ctest -R "^agc_shader_fusion$" (1/1 passed). Also built agc_driver_prepared_shaders_tests and ran ctest -R "^agc_driver_prepared_shaders" (9/9 passed). No failures. These focused Linux tests complement the reported title A/B; the open question in my review about process-lifetime growth of distinct FusedPrograms entries remains unanswered.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158268751 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5573#issuecomment-6158268751). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T00:41:15Z. --- Linux validation on PR head `d6f67d1be2ef7dc7cc0e9167cb7fc79197e381cc` (GCC 12.2, CMake/Ninja Release, `BUILD_TESTING=ON`): built `agc_shader_fusion_tests` and ran `ctest -R "^agc_shader_fusion$"` (1/1 passed). Also built `agc_driver_prepared_shaders_tests` and ran `ctest -R "^agc_driver_prepared_shaders"` (9/9 passed). No failures. These focused Linux tests complement the reported title A/B; the open question in my review about process-lifetime growth of distinct `FusedPrograms` entries remains unanswered.
fma-rc commented 2026-10-14 00:58:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fma-rc on 2026-10-14T00:58:18Z.


I measured it. A local build of this head with a counter (one line per new entry and the running total of key plus storage bytes) on both titles:

title run fusion calls distinct programs bytes kept
Stray 420 s, 9,648 frames 3 2 12,610
Kena 124 s 2 2 12,610

Both fuse when a shader pair is first set up, a handful of times in a session. In Stray the second call was a hit on the first entry, and the last new entry appeared 70% of the way through the log, so the map follows the pairs the title has loaded.

What stays unbounded is a title that builds new halves at run time: each distinct pair would keep about 6 KB until exit. I have not seen one. I left eviction out because a draw queued earlier may still name an entry's code address, which is the failure this PR removes.

Kena's run ended on shader buffer exceeds descriptor range limit, a stop it also reaches without this change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158474769 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5573#issuecomment-6158474769). Originally posted by [fma-rc](https://github.com/fma-rc) on 2026-10-14T00:58:18Z. --- I measured it. A local build of this head with a counter (one line per new entry and the running total of key plus storage bytes) on both titles: | title | run | fusion calls | distinct programs | bytes kept | |---|---|---|---|---| | Stray | 420 s, 9,648 frames | 3 | 2 | 12,610 | | Kena | 124 s | 2 | 2 | 12,610 | Both fuse when a shader pair is first set up, a handful of times in a session. In Stray the second call was a hit on the first entry, and the last new entry appeared 70% of the way through the log, so the map follows the pairs the title has loaded. What stays unbounded is a title that builds new halves at run time: each distinct pair would keep about 6 KB until exit. I have not seen one. I left eviction out because a draw queued earlier may still name an entry's code address, which is the failure this PR removes. Kena's run ended on `shader buffer exceeds descriptor range limit`, a stop it also reaches without this change.
MarcZX10 commented 2026-10-14 01:02:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T01:02:40Z.

Review: APPROVED


Thanks for measuring the lifetime retention. The two title runs each retain about 12.6 KB across two distinct fused programs, with later calls mostly reusing existing pairs. That addresses my lifecycle question for these observed workloads. The dynamic-pair case remains process-lifetime storage, and the PR explains why eviction could invalidate code still referenced by queued draws. I previously ran the focused fusion and prepared-shader suites on this exact head; I see no blocker from the retention data.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5511867073 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5573#pullrequestreview-5511867073). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T01:02:40Z. **Review:** APPROVED --- Thanks for measuring the lifetime retention. The two title runs each retain about 12.6 KB across two distinct fused programs, with later calls mostly reusing existing pairs. That addresses my lifecycle question for these observed workloads. The dynamic-pair case remains process-lifetime storage, and the PR explains why eviction could invalidate code still referenced by queued draws. I previously ran the focused fusion and prepared-shader suites on this exact head; I see no blocker from the retention data.
xXPBSXx commented 2026-10-14 03:23:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-14T03:23:21Z.


Windows test of #5573: Windows 10 (19045), MinGW-w64 GCC 15.2, Release, ninja -j 4, ctest called directly; ctest ran without -V, so the logs of the passing runs do not show which GPU, if any, the tests used (not verified). Tree: main 095a653c8 + merge of PR head d6f67d1be2ef7dc7cc0e9167cb7fc79197e381cc (local merge 7fa9258eb, no conflict). Logs: rtx5573_*_N.log, rtx5573_result.txt.
10 ctest targets, 3 runs each, exit code 0 in 30 of 30 runs: agc_driver_prepared_shaders, agc_driver_prepared_shaders_debug, agc_driver_prepared_shaders_debug_failure, agc_driver_prepared_shaders_deferred, agc_driver_prepared_shaders_deferred_undecodable, agc_driver_prepared_shaders_indirect, agc_driver_prepared_shaders_registered_workgroup, agc_driver_prepared_shaders_registered_workgroup_sync, agc_driver_prepared_shaders_uncached, agc_shader_fusion.
Not tested: that the new or changed tests fail without the change (no baseline run), other ctest targets, the AMD GPU unless named above, Linux, a title run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6160161160 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5573#issuecomment-6160161160). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-14T03:23:21Z. --- Windows test of #5573: Windows 10 (19045), MinGW-w64 GCC 15.2, Release, `ninja -j 4`, ctest called directly; ctest ran without `-V`, so the logs of the passing runs do not show which GPU, if any, the tests used (not verified). Tree: `main` `095a653c8` + merge of PR head `d6f67d1be2ef7dc7cc0e9167cb7fc79197e381cc` (local merge `7fa9258eb`, no conflict). Logs: `rtx5573_*_N.log`, `rtx5573_result.txt`. 10 ctest targets, 3 runs each, exit code 0 in 30 of 30 runs: `agc_driver_prepared_shaders`, `agc_driver_prepared_shaders_debug`, `agc_driver_prepared_shaders_debug_failure`, `agc_driver_prepared_shaders_deferred`, `agc_driver_prepared_shaders_deferred_undecodable`, `agc_driver_prepared_shaders_indirect`, `agc_driver_prepared_shaders_registered_workgroup`, `agc_driver_prepared_shaders_registered_workgroup_sync`, `agc_driver_prepared_shaders_uncached`, `agc_shader_fusion`. Not tested: that the new or changed tests fail without the change (no baseline run), other ctest targets, the AMD GPU unless named above, Linux, a title run. AI-assisted (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!5573
No description provided.