fix(agc): keep the user-data tables of a handed-off draw as they were at its draw packet #17024

Merged
muhmuhkuh merged 3 commits from upstream-pr/17024 into main 2026-11-16 05:42:17 +00:00
muhmuhkuh commented 2026-11-08 08:26:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by muhmuhkuh on 2026-11-08T08:26:12Z.


What

A draw handed to the finisher builds its stages after reading the packet. A later guest write can therefore change a pointed-to user-data table before the stage reads it.

With APS5_DRAW_TABLE_SNAPSHOT=1, the driver keeps 256 bytes behind each user-data pointer at the draw packet. Shader memory reads those kept words when building the stages. The switch stays off by default and requires APS5_DRAW_PIPELINE; phase B of APS5_AB can enable it. A block is kept only when the pending-write checks before and after copying accept it. Overlapping, unmapped, unaligned and otherwise unsupported blocks retain the existing path. The debt entry records the remaining limits.

The inactive commit-time comparison commit and its specific test have been dropped. This PR now contains the snapshot and its wiring/word-by-word tests. Commit-time comparison integration with #15455 remains separate and is not claimed as tested here.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, Release. Local rebased head: 434da344030ecfb4f30716908c25f26f3cc4b4e0; tested main: 808a902ea17ccff6cab2d013fd5e702217d47f50.

  • guest_standard_streams passes solo on both main and this head, rc0. These runs use isolated, hash-bound executable/libc copies.
  • Five focused CPU/mock cases pass on this head: agc_runtime_switches, agc_runtime_switches_refused, agc_shader_table_snapshot_reuse, agc_shader_table_snapshot_reuse_unmasked, and agc_shader_table_keep. Main passes the four existing cases. Their executables compile the relevant sources directly and do not import libSceAgcDriver.prx.
  • The historical head reproduces the Windows abort in CheckGetdelimAndGetline: the second ungetc returns -1. The preceding fscanf returns 2 and the expected data. Main already removed that fixture through ff82682151b5f85c319f8fd109cff34052b15ce7; no libc diagnostic or unrelated libc fix is included in this PR.
  • tools/check_conventions.py --base origin/main and git merge-tree --write-tree origin/main HEAD pass.
  • Current paired Vulkan qualification remains open. Earlier local GPU runs returned rc0, but a co-located AGC DLL invalidated their source binding. They are excluded as evidence for this head. A subsequent local staging-helper error ended that test window. No GPU red/restore proof or performance result 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: PPSA02015 (Death Stranding 2) #5424
  • GPUs tested: NVIDIA GeForce RTX 3080 Ti, driver 610.88, Windows 11 Pro 10.0.26200; current paired GPU source qualification pending as described above
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5209858044 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17024). Originally posted by [muhmuhkuh](https://github.com/muhmuhkuh) on 2026-11-08T08:26:12Z. --- ### What A draw handed to the finisher builds its stages after reading the packet. A later guest write can therefore change a pointed-to user-data table before the stage reads it. With `APS5_DRAW_TABLE_SNAPSHOT=1`, the driver keeps 256 bytes behind each user-data pointer at the draw packet. Shader memory reads those kept words when building the stages. The switch stays off by default and requires `APS5_DRAW_PIPELINE`; phase B of `APS5_AB` can enable it. A block is kept only when the pending-write checks before and after copying accept it. Overlapping, unmapped, unaligned and otherwise unsupported blocks retain the existing path. The debt entry records the remaining limits. The inactive commit-time comparison commit and its specific test have been dropped. This PR now contains the snapshot and its wiring/word-by-word tests. Commit-time comparison integration with #15455 remains separate and is not claimed as tested here. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, MinGW-w64 GCC 15.2.0, Release. Local rebased head: `434da344030ecfb4f30716908c25f26f3cc4b4e0`; tested main: `808a902ea17ccff6cab2d013fd5e702217d47f50`. - `guest_standard_streams` passes solo on both main and this head, rc0. These runs use isolated, hash-bound executable/libc copies. - Five focused CPU/mock cases pass on this head: `agc_runtime_switches`, `agc_runtime_switches_refused`, `agc_shader_table_snapshot_reuse`, `agc_shader_table_snapshot_reuse_unmasked`, and `agc_shader_table_keep`. Main passes the four existing cases. Their executables compile the relevant sources directly and do not import `libSceAgcDriver.prx`. - The historical head reproduces the Windows abort in `CheckGetdelimAndGetline`: the second `ungetc` returns -1. The preceding `fscanf` returns 2 and the expected data. Main already removed that fixture through `ff82682151b5f85c319f8fd109cff34052b15ce7`; no libc diagnostic or unrelated libc fix is included in this PR. - `tools/check_conventions.py --base origin/main` and `git merge-tree --write-tree origin/main HEAD` pass. - Current paired Vulkan qualification remains open. Earlier local GPU runs returned rc0, but a co-located AGC DLL invalidated their source binding. They are excluded as evidence for this head. A subsequent local staging-helper error ended that test window. No GPU red/restore proof or performance result 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: PPSA02015 (Death Stranding 2) #5424 - [x] GPUs tested: NVIDIA GeForce RTX 3080 Ti, driver 610.88, Windows 11 Pro 10.0.26200; current paired GPU source qualification pending as described above - [x] AI-assisted: yes
Superseabas commented 2026-11-08 20:26:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-08T20:26:15Z.

Review: APPROVED


Windows check of head f9a3585a11, merged onto main 095ff72367 (clean merge), with the validation layer on an NVIDIA and an AMD GPU. The switch does what the description says in a real draw on both. Two notes below, neither blocking from me.

Setup: Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (x86_64-ucrt-posix-seh, r3), Release, BUILD_TESTING=ON. Both GPUs, selected with ANYPS5_GPU: an RTX 5070 Ti (driver 621.01) and the Ryzen 7 7700X's integrated Radeon (AMD 26.10.2). ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation.

Full ^agc suite (-j 8), passed / total:

RTX 5070 Ti Radeon (iGPU)
main 1489 / 1490 1489 / 1490
this PR, switch off 1491 / 1492 1491 / 1492
this PR, APS5_DRAW_PIPELINE=1 1488 / 1492 1488 / 1492
this PR, APS5_DRAW_PIPELINE=1 and APS5_DRAW_TABLE_SNAPSHOT=1 1488 / 1492 1488 / 1492
  • The one failure in the first two rows is the same with and without this PR: agc_driver_quad_pixel_mask on the RTX (#14341) and agc_driver_sample_order_grid_reference on the Radeon (#9503).
  • The last two rows add agc_driver_draw_recapture_on_change, agc_driver_queued_constant_ram_dump and agc_driver_draw_unsupported_buffer_type_skip_refused. All three also fail on main with APS5_DRAW_PIPELINE=1, so turning the switch on adds no failures.
  • No VUID- lines and no missing-layer lines in any run.
  • agc_shader_table_keep, agc_shader_table_keep_compare and the switch tests pass in 3 of 3 runs on each GPU. tools/check_conventions.py --base upstream/main passes.

A real draw, with a temporary test. No test here turns the switch on for a draw, so I ran one locally in the DrawDataHitUserWords fixture with APS5_DRAW_PIPELINE=1. It holds the finisher with a blocker job, submits a draw whose table holds a V# for 0.25, waits for the packet to be read, rewrites the table to a V# for 1.0, then releases the finisher. The value is the brightest byte the draw wrote, with the same results on both GPUs from one run of each case:

Table Switch off Switch on
Page aligned 255 (the table the game wrote later) 64 (the table at the packet)
16 bytes into a block not run 64
Keep crosses a page not run 64

The next draw over the rewritten table wrote 255 in every case, so the draw cache didn't reuse the kept words.

1. The commit-time comparison isn't reached on today's main. Keeps are only taken on the hand-off path, and tail.queue is only set on the other path (Draw.cpp:786). Each of the four keptTables calls sits behind tail.queue != nullptr, so a comparison never sees a keep. I logged this: tail.queue wasn't set on any finisher job in the runs above or in the 68 from agc_driver_draw_cache_reuse_draw_pipeline and _plan_verify, where 8 tables were kept, and no comparison ran with a keep. So the second commit does nothing until #15455 drops that condition, and the description's "the check the finisher runs at commit time" describes main with #15455 rather than main today. It might help to say so there. I didn't build the two together.

2. Two parts of the switch have no test.

  • The wiring: besides RuntimeSwitches, which only checks the name, no test refers to the switch, so as far as I can see nothing would fail if KeepUserDataTables stopped being called. The temporary test above could become one. I set Pending::unknown = false on the blocker job, because PendingOverlaps treats an unknown job as overlapping everything. Mine waits a fixed 300 ms after the lookup counter moves, which you'd probably want to replace.
  • Word-by-word pages: with !page.kept.Test(index) removed from ShaderMemory::read, both new tests still pass. They pass no pendingWrite, so their pages are never read word by word.

One small thing: the description says a game run needs APS5_DRAW_TABLE_SNAPSHOT=1. From handOff in Draw.cpp it also needs APS5_DRAW_PIPELINE, since without it no draw reaches the finisher. The ENVIRONMENT.md entry could name it too.

Not run: any title, or this PR merged with #15455.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5819526624 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17024#pullrequestreview-5819526624). Originally posted by [Superseabas](https://github.com/Superseabas) on 2026-11-08T20:26:15Z. **Review:** APPROVED --- Windows check of head `f9a3585a11`, merged onto `main` `095ff72367` (clean merge), with the validation layer on an NVIDIA and an AMD GPU. The switch does what the description says in a real draw on both. Two notes below, neither blocking from me. **Setup:** Windows 11 Home 10.0.26200, WinLibs GCC 16.2.0 (`x86_64-ucrt-posix-seh`, r3), Release, `BUILD_TESTING=ON`. Both GPUs, selected with `ANYPS5_GPU`: an RTX 5070 Ti (driver 621.01) and the Ryzen 7 7700X's integrated Radeon (AMD 26.10.2). `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`. **Full `^agc` suite** (`-j 8`), passed / total: | | RTX 5070 Ti | Radeon (iGPU) | |---|---|---| | `main` | 1489 / 1490 | 1489 / 1490 | | this PR, switch off | 1491 / 1492 | 1491 / 1492 | | this PR, `APS5_DRAW_PIPELINE=1` | 1488 / 1492 | 1488 / 1492 | | this PR, `APS5_DRAW_PIPELINE=1` and `APS5_DRAW_TABLE_SNAPSHOT=1` | 1488 / 1492 | 1488 / 1492 | - The one failure in the first two rows is the same with and without this PR: `agc_driver_quad_pixel_mask` on the RTX (#14341) and `agc_driver_sample_order_grid_reference` on the Radeon (#9503). - The last two rows add `agc_driver_draw_recapture_on_change`, `agc_driver_queued_constant_ram_dump` and `agc_driver_draw_unsupported_buffer_type_skip_refused`. All three also fail on `main` with `APS5_DRAW_PIPELINE=1`, so turning the switch on adds no failures. - No `VUID-` lines and no missing-layer lines in any run. - `agc_shader_table_keep`, `agc_shader_table_keep_compare` and the switch tests pass in 3 of 3 runs on each GPU. `tools/check_conventions.py --base upstream/main` passes. **A real draw, with a temporary test.** No test here turns the switch on for a draw, so I ran one locally in the `DrawDataHitUserWords` fixture with `APS5_DRAW_PIPELINE=1`. It holds the finisher with a blocker job, submits a draw whose table holds a V# for 0.25, waits for the packet to be read, rewrites the table to a V# for 1.0, then releases the finisher. The value is the brightest byte the draw wrote, with the same results on both GPUs from one run of each case: | Table | Switch off | Switch on | |---|---|---| | Page aligned | 255 (the table the game wrote later) | 64 (the table at the packet) | | 16 bytes into a block | not run | 64 | | Keep crosses a page | not run | 64 | The next draw over the rewritten table wrote 255 in every case, so the draw cache didn't reuse the kept words. **1. The commit-time comparison isn't reached on today's `main`.** Keeps are only taken on the hand-off path, and `tail.queue` is only set on the other path (`Draw.cpp:786`). Each of the four `keptTables` calls sits behind `tail.queue != nullptr`, so a comparison never sees a keep. I logged this: `tail.queue` wasn't set on any finisher job in the runs above or in the 68 from `agc_driver_draw_cache_reuse_draw_pipeline` and `_plan_verify`, where 8 tables were kept, and no comparison ran with a keep. So the second commit does nothing until #15455 drops that condition, and the description's "the check the finisher runs at commit time" describes `main` with #15455 rather than `main` today. It might help to say so there. I didn't build the two together. **2. Two parts of the switch have no test.** - **The wiring:** besides `RuntimeSwitches`, which only checks the name, no test refers to the switch, so as far as I can see nothing would fail if `KeepUserDataTables` stopped being called. The temporary test above could become one. I set `Pending::unknown = false` on the blocker job, because `PendingOverlaps` treats an unknown job as overlapping everything. Mine waits a fixed 300 ms after the lookup counter moves, which you'd probably want to replace. - **Word-by-word pages:** with `!page.kept.Test(index)` removed from `ShaderMemory::read`, both new tests still pass. They pass no `pendingWrite`, so their pages are never read word by word. **One small thing:** the description says a game run needs `APS5_DRAW_TABLE_SNAPSHOT=1`. From `handOff` in `Draw.cpp` it also needs `APS5_DRAW_PIPELINE`, since without it no draw reaches the finisher. The `ENVIRONMENT.md` entry could name it too. Not run: any title, or this PR merged with #15455.
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!17024
No description provided.