fix(agc): run the packets after a call once its REWIND is released #22441

Merged
adam25136 merged 1 commit from upstream-pr/22441 into main 2026-11-27 15:40:50 +00:00
adam25136 commented 2026-11-17 16:05:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adam25136 on 2026-11-17T16:05:05Z.


What

A REWIND's second dword is its control word; its tail, the packets after it, may run once the game sets bit 31 of that word. When a buffer reached by INDIRECT_BUFFER stopped at a REWIND, copyLink (Submission.cpp) returned at once, so the caller's packets after the call were never copied. Once the game released the REWIND, the called buffer's tail ran and the submission ended there: the packets after the call, in that caller and in any caller above it, never ran.

  • The change:
    • at submit (and when a released tail is copied), copyLink keeps a copy of the packets after each such call (AGC's copy), innermost caller first, with the address they were read from;
    • executeRewindTail copies them after the released tail, through a new copyRewindTail around the unchanged copyCommands, and the look-ahead over released tails in noteReleasedTails uses it too;
    • a further REWIND among them stops there again. Its control word and its tail are read at the address those packets were read from at submit, not from AGC's copy; only the packets after the outer calls stay kept for its release.
  • Not changed:
    • a COND_INDIRECT_BUFFER branch still replaces the rest of the buffer holding it (testBranchSubmission);
    • a chained INDIRECT_BUFFER still does not return;
    • a REWIND inside a conditional execution range or a predicated buffer still throws.
  • Copied at submit: the CP reads the packets after a call when the call returns (not checked on hardware). Here they are copied at submit, as AGC already copies called buffers, so a game that rewrites them before releasing the REWIND runs AGC's copy. AGC's copy also keeps them readable when the submitted buffer is a temporary one: on the PS4 Gnm path, SubmitLocked passes a merged copy that it frees on return.
  • Gnm path: a further REWIND among the merged copy's packets after a call is a REWIND at the top of a Gnm draw command buffer. Its wait and tail still read the freed merged copy, as a top-level REWIND does on main (read from the code, not reproduced); open #22375 makes MergeEngines refuse such a REWIND.
  • Technical debt: a fragment adds a Packets after a call that stops at a REWIND entry under Functional.

Tested

Windows 10 Pro 22H2 (19045), NVIDIA GeForce RTX 5080 (driver 621.14), WinLibs GCC 15.2.0 (r7), CMake 4.3.1, Release, this branch on main 5a23c6303b. main's own agc_driver_upload_cache_read_only_flush_tests does not build with MinGW (setenv; open #19899 fixes it), so the builds went on past it. No game is known to hit this; it was found while working on #22375.

  • On main 7058988458 with only this branch's test file and its CMake line: the new test fails at its first case, the caller's packets after a call whose REWIND was released did not run; the called buffer's own label was written, as that case checks first.
  • The new test, agc_driver_pm4_rewind_call, passes. Its five cases:
    • one call: nothing after the REWIND runs before the release; after it, the called buffer's packet and the caller's packet after the call both run;
    • two calls deep, with a second REWIND in the middle buffer's packets after the inner call: releasing the inner REWIND runs its tail and the middle buffer's packet, but not the packets after the second REWIND or after the outer call. Those run once the second REWIND is released in the middle buffer;
    • a branch: a COND_INDIRECT_BUFFER to a buffer with a REWIND, in a called buffer. After the release the branch target's tail and the outer caller's packet run, and the packet after the branch does not;
    • a chain: the same with a chained INDIRECT_BUFFER;
    • copied at submit: after submitting, the test clears the caller's packets after the call to zeros, as a freed temporary buffer could be reused. After the release the original packet runs.
  • Deliberate breaks: each of these six makes agc_driver_pm4_rewind_call fail:
    • not keeping the packets after a call (the caller's packets after a call whose REWIND was released did not run);
    • dropping the packets still kept when a second REWIND stops the copy (the packets after a second released REWIND and after the outer call did not run);
    • reading the caller's packets from its buffer at release instead of the copy (the copied-at-submit case reads the cleared words: unsupported PM4 packet type: header 0x00000000);
    • pointing a REWIND met in AGC's copy at that copy instead of its original address (the test times out at 30 s);
    • running the kept packets outermost first (packets after a second REWIND, met in the packets after a call, ran before the title released it);
    • keeping the packets after a COND_INDIRECT_BUFFER as after a call (the branch returned like a call).
  • The tests tools/select_tests.py selects for the 6 changed files (ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1): 3,282 on this branch and the same 3,281 on main, which lacks the new test. No test fails only on the branch, and both runs skip the same 19. Five fail on both:
    • agc_driver_bda_device and agc_driver_stencil_plane_memory;
    • agc_driver_upload_cache_read_only_flush (not built);
    • agc_driver_store_over_untracked, which fails on either side from run to run in earlier runs here too (a pending storage result was stored over bytes the CPU wrote to untracked memory);
    • performance_runner_PerformanceRunTests: 3 of its 5 cases error, because run_performance_title refuses ANYPS5_NO_SHADER_CACHE=1.
  • Validation layer: VK_LAYER_KHRONOS_validation 1.4.367 (through VK_ADD_LAYER_PATH) with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer, VK_LOADER_LAYERS_DISABLE=~implicit~, ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1, ctest -V, on the tests matching pm4|rewind|frame_replay|frame_capture: 89 on this branch, 88 on main.
    • Each log has 44 Insert instance layer "VK_LAYER_KHRONOS_validation" lines, from the 19 tests that create a Vulkan instance, no "was not found" line and no VUID- message.
    • The new test creates none, so the layer does not see it.
    • All pass. In an earlier run of the same change, frame_capture_minimize failed once in the branch's run (PermissionError deleting a temporary file), then passed 3 of 3 times alone on the branch.
  • python tools/check_conventions.py --base origin/main --title "<this title>" --body <this description>: All conventions checks passed.
  • python tools/pr_overlap.py --branch:
    • 59 of the 1,722 open PRs share files or exports with this branch. It compared 38 with no overlap and could not compare 21 that conflict with main, so pr_overlap exits 2.
    • Checked by text, no hunk of those 21 contains, changes or adds a line of this branch's hunks, except #12944. Its hunk in Pm4.cpp's main() sits next to the new test's mode block.
    • git merge-tree of this branch with the heads of #12944, #18914, #22029 and #22156 finds no conflict in this branch's files. The last three edit copyCommands, copyLink or Pm4.cpp near this branch's lines. #12944's four conflicts are the ones it has with main.
    • Opened after that run: #22434 (opt-in APS5_LEAN_FRONT) changes copyCommands, copySegment, copyLink, noteReleasedTails and executeRewindTail too, so git merge-tree with its head conflicts in Driver.hpp, Submission.cpp and Pm4.cpp. Where this branch keeps the caller's packets after a call that stopped at a REWIND, #22434 records the words read for its packet table (read(cursor + count)) and still returns without them, so the two fix different things; whichever merges second needs a rebase.
  • Not covered: other OSes and GPUs; any game; the Gnm path itself (the copied-at-submit case clears the caller's buffer to stand in for a freed one).

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: NVIDIA GeForce RTX 5080, driver 621.14, Windows 10 22H2
  • AI-assisted: yes

AI-assisted: Claude Code found the bug while working on #22375, reproduced it with the new test, wrote the change, ran the tests, and drafted this description; separate review agents checked it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5360378617 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22441). Originally posted by [adam25136](https://github.com/adam25136) on 2026-11-17T16:05:05Z. --- ### What A REWIND's second dword is its control word; its tail, the packets after it, may run once the game sets bit 31 of that word. When a buffer reached by INDIRECT_BUFFER stopped at a REWIND, `copyLink` (`Submission.cpp`) returned at once, so the caller's packets after the call were never copied. Once the game released the REWIND, the called buffer's tail ran and the submission ended there: the packets after the call, in that caller and in any caller above it, never ran. - **The change:** - at submit (and when a released tail is copied), `copyLink` keeps a copy of the packets after each such call (AGC's copy), innermost caller first, with the address they were read from; - `executeRewindTail` copies them after the released tail, through a new `copyRewindTail` around the unchanged `copyCommands`, and the look-ahead over released tails in `noteReleasedTails` uses it too; - a further REWIND among them stops there again. Its control word and its tail are read at the address those packets were read from at submit, not from AGC's copy; only the packets after the outer calls stay kept for its release. - **Not changed:** - a COND_INDIRECT_BUFFER branch still replaces the rest of the buffer holding it (`testBranchSubmission`); - a chained INDIRECT_BUFFER still does not return; - a REWIND inside a conditional execution range or a predicated buffer still throws. - **Copied at submit:** the CP reads the packets after a call when the call returns (not checked on hardware). Here they are copied at submit, as AGC already copies called buffers, so a game that rewrites them before releasing the REWIND runs AGC's copy. AGC's copy also keeps them readable when the submitted buffer is a temporary one: on the PS4 Gnm path, `SubmitLocked` passes a merged copy that it frees on return. - **Gnm path:** a further REWIND among the merged copy's packets after a call is a REWIND at the top of a Gnm draw command buffer. Its wait and tail still read the freed merged copy, as a top-level REWIND does on `main` (read from the code, not reproduced); open #22375 makes `MergeEngines` refuse such a REWIND. - **Technical debt:** a fragment adds a `Packets after a call that stops at a REWIND` entry under Functional. ### 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 10 Pro 22H2 (19045), NVIDIA GeForce RTX 5080 (driver 621.14), WinLibs GCC 15.2.0 (r7), CMake 4.3.1, Release, this branch on `main` `5a23c6303b`. `main`'s own `agc_driver_upload_cache_read_only_flush_tests` does not build with MinGW (`setenv`; open #19899 fixes it), so the builds went on past it. No game is known to hit this; it was found while working on #22375. - **On `main` `7058988458` with only this branch's test file and its CMake line:** the new test fails at its first case, `the caller's packets after a call whose REWIND was released did not run`; the called buffer's own label was written, as that case checks first. - **The new test, `agc_driver_pm4_rewind_call`, passes.** Its five cases: - one call: nothing after the REWIND runs before the release; after it, the called buffer's packet and the caller's packet after the call both run; - two calls deep, with a second REWIND in the middle buffer's packets after the inner call: releasing the inner REWIND runs its tail and the middle buffer's packet, but not the packets after the second REWIND or after the outer call. Those run once the second REWIND is released in the middle buffer; - a branch: a COND_INDIRECT_BUFFER to a buffer with a REWIND, in a called buffer. After the release the branch target's tail and the outer caller's packet run, and the packet after the branch does not; - a chain: the same with a chained INDIRECT_BUFFER; - copied at submit: after submitting, the test clears the caller's packets after the call to zeros, as a freed temporary buffer could be reused. After the release the original packet runs. - **Deliberate breaks:** each of these six makes `agc_driver_pm4_rewind_call` fail: - not keeping the packets after a call (`the caller's packets after a call whose REWIND was released did not run`); - dropping the packets still kept when a second REWIND stops the copy (`the packets after a second released REWIND and after the outer call did not run`); - reading the caller's packets from its buffer at release instead of the copy (the copied-at-submit case reads the cleared words: `unsupported PM4 packet type: header 0x00000000`); - pointing a REWIND met in AGC's copy at that copy instead of its original address (the test times out at 30 s); - running the kept packets outermost first (`packets after a second REWIND, met in the packets after a call, ran before the title released it`); - keeping the packets after a COND_INDIRECT_BUFFER as after a call (`the branch returned like a call`). - **The tests `tools/select_tests.py` selects for the 6 changed files** (`ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`): 3,282 on this branch and the same 3,281 on `main`, which lacks the new test. No test fails only on the branch, and both runs skip the same 19. Five fail on both: - `agc_driver_bda_device` and `agc_driver_stencil_plane_memory`; - `agc_driver_upload_cache_read_only_flush` (not built); - `agc_driver_store_over_untracked`, which fails on either side from run to run in earlier runs here too (`a pending storage result was stored over bytes the CPU wrote to untracked memory`); - `performance_runner_PerformanceRunTests`: 3 of its 5 cases error, because `run_performance_title` refuses `ANYPS5_NO_SHADER_CACHE=1`. - **Validation layer:** `VK_LAYER_KHRONOS_validation` 1.4.367 (through `VK_ADD_LAYER_PATH`) with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer`, `VK_LOADER_LAYERS_DISABLE=~implicit~`, `ANYPS5_REQUIRE_VULKAN=1` and `ANYPS5_NO_SHADER_CACHE=1`, `ctest -V`, on the tests matching `pm4|rewind|frame_replay|frame_capture`: 89 on this branch, 88 on `main`. - Each log has 44 `Insert instance layer "VK_LAYER_KHRONOS_validation"` lines, from the 19 tests that create a Vulkan instance, no "was not found" line and no `VUID-` message. - The new test creates none, so the layer does not see it. - All pass. In an earlier run of the same change, `frame_capture_minimize` failed once in the branch's run (`PermissionError` deleting a temporary file), then passed 3 of 3 times alone on the branch. - **`python tools/check_conventions.py --base origin/main --title "<this title>" --body <this description>`:** `All conventions checks passed.` - **`python tools/pr_overlap.py --branch`:** - 59 of the 1,722 open PRs share files or exports with this branch. It compared 38 with no overlap and could not compare 21 that conflict with `main`, so pr_overlap exits 2. - Checked by text, no hunk of those 21 contains, changes or adds a line of this branch's hunks, except #12944. Its hunk in `Pm4.cpp`'s `main()` sits next to the new test's mode block. - `git merge-tree` of this branch with the heads of #12944, #18914, #22029 and #22156 finds no conflict in this branch's files. The last three edit `copyCommands`, `copyLink` or `Pm4.cpp` near this branch's lines. #12944's four conflicts are the ones it has with `main`. - Opened after that run: #22434 (opt-in `APS5_LEAN_FRONT`) changes `copyCommands`, `copySegment`, `copyLink`, `noteReleasedTails` and `executeRewindTail` too, so `git merge-tree` with its head conflicts in `Driver.hpp`, `Submission.cpp` and `Pm4.cpp`. Where this branch keeps the caller's packets after a call that stopped at a REWIND, #22434 records the words read for its packet table (`read(cursor + count)`) and still returns without them, so the two fix different things; whichever merges second needs a rebase. - **Not covered:** other OSes and GPUs; any game; the Gnm path itself (the copied-at-submit case clears the caller's buffer to stand in for a freed one). ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: NVIDIA GeForce RTX 5080, driver 621.14, Windows 10 22H2 <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> AI-assisted: Claude Code found the bug while working on #22375, reproduced it with the new test, wrote the change, ran the tests, and drafted this description; separate review agents checked it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-23 03:46:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T03:46:53Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the implementation and its added test cover the described REWIND return behavior, while the Gnm path remains unverified.

What I ran

Reviewed head f1377ee36bed5742cef0dfc6a1df4d979149aa6f. No commands or tests were run in this review; the commands and test results in the PR description are author-reported.

Results

Check Result Note
Changed-file review passed All six changed files and added lines read
Targeted execution-path review passed Resume flow and ordering inspected
Runtime checks not run Author-reported results only

Findings

Blocking

None.

Non-blocking

None.

Not verified

  • Runtime behavior on hardware, other operating systems or GPUs, and the Gnm path was not verified. The description specifically notes that a further REWIND encountered in the temporary Gnm merged copy can wait on freed memory.
  • Test/build results in the description were not independently reproduced.
  • Files read: description.md, commits.md, manifest.json, diff.patch, README.md; all six changed files listed in manifest.json; and the targeted PacketExecution.cpp ranges. No changed files were excluded.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6016335487 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22441#pullrequestreview-6016335487). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T03:46:53Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the implementation and its added test cover the described REWIND return behavior, while the Gnm path remains unverified. ## What I ran Reviewed head `f1377ee36bed5742cef0dfc6a1df4d979149aa6f`. No commands or tests were run in this review; the commands and test results in the PR description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Changed-file review | passed | All six changed files and added lines read | | Targeted execution-path review | passed | Resume flow and ordering inspected | | Runtime checks | not run | Author-reported results only | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified - Runtime behavior on hardware, other operating systems or GPUs, and the Gnm path was not verified. The description specifically notes that a further REWIND encountered in the temporary Gnm merged copy can wait on freed memory. - Test/build results in the description were not independently reproduced. - Files read: `description.md`, `commits.md`, `manifest.json`, `diff.patch`, `README.md`; all six changed files listed in `manifest.json`; and the targeted `PacketExecution.cpp` ranges. No changed files were excluded. ## To get this to a good state Nothing is required.
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!22441
No description provided.