fix(agc): share a registered shader's decoded state with its other headers #9991

Merged
milescoviello merged 1 commit from upstream-pr/9991 into main 2026-10-24 02:26:55 +00:00
milescoviello commented 2026-10-23 03:19:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-23T03:19:32Z.


What

Ghost of Tsushima Director's Cut (PPSA02225) registers shaders about 400 times a second during its opening. It registers the same code with the same registers over and over, each time through a header at a new address. Every registration decodes the header's registers into a RegisteredShaderState, three dense register sets of about 10 KiB, and its snapshot keeps that copy for as long as the registry holds it. In 200 s the title made 80,000 registrations: none matched a published snapshot's header address, so all 80,000 were published, and 68,144 of them (85 %) repeated the code and decoded registers of a snapshot at another header address. Only 11,856 were distinct. A jemalloc profile at 240 s put about 1 GiB of live heap under RegisterShader's register sets (InitialContextRegisters copies, the Registers built from the user-config initializer, Registers::mark). This title's opening is where the 20 GiB memory cap ends my runs.

How

  • A new snapshot takes the RegisteredShaderState of a published snapshot at the same code address with the same type, the same code and equal decoded registers, instead of keeping its own copy. The lookup runs under the registry mutex, next to the existing same-header check.
  • RegisteredShaderState gets a defaulted operator==; Registers already compares by entries.
  • The state is immutable once published (shared_ptr<const RegisteredShaderState>), and its users copy it into a QueueState before using it. Registers::Fingerprint's cache is only written on those live copies.
  • The preparation is not shared. Its request carries the header bytes and address, and this title's headers at other addresses hold other pointers.

Tested

Ubuntu 26.04.1, GCC 15.2, NVIDIA RTX 4070 Ti (615.71.09).

Ghost of Tsushima Director's Cut (PPSA02225). main can't take this title past its first minutes, so these runs use @EagleBearerYS's test/ghost-9742-9717 stack with #8115, #8121, #8213, #8608 and #9318, built from release libs with and without this change, plus a local counter of registrations. Each run is warm, 200 s, with a 20 GiB cap. Both builds see the same registration stream: 90,000 registrations, 76,792 of them (85 %) repeating another header's code and registers, and 13,208 distinct.

run this change RssAnon at 100 s RssAnon at 190 s
S1 yes 3,100 MB 4,632 MB
R1 no 4,987 MB 5,319 MB
S2 yes 4,202 MB 4,664 MB
R2 no 4,982 MB 5,284 MB

That's about 650 MB less at 190 s, and more earlier, where the registrations are densest.

Tests

  • agc_driver_prepared_shaders's shared-code case now checks the state at both points. While its two headers have different registers, they keep separate states. Once both are type 2 with the same code and registers, they hold the same state, with the header's context register in it.
  • With the sharing reverted and the test kept, agc_driver_prepared_shaders fails: "two headers with the same code and registers decoded their registered state twice".
  • The 25 tests matching ^agc_driver_prepared_shaders|^agc_driver_graphics$|^agc_driver_submit$|draw_cache pass 3 of 3.
  • Khronos validation layer 1.4.363, main against this branch over ^agc_driver_prepared_shaders|draw_cache: 24 of 24 pass on both, with no VUID on either. GCC 15.2 crashed (an internal compiler error in Pm4.hpp) building agc_driver_draw_cache_relocation_tests on the main side, so that one test ran from an earlier build there.
  • Windows 11 VM (GeForce GT 1030, NVIDIA R580, WinLibs GCC, Release, ANYPS5_REQUIRE_VULKAN=1): the same 25 tests pass 3 of 3 on this branch and on its merge base.

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: none
  • GPUs tested: NVIDIA RTX 4070 Ti (Linux); NVIDIA GeForce GT 1030 (Windows); NVIDIA GeForce RTX 3080, driver 617.42, Windows 11 (@VakzOs)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4977302185 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9991). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-23T03:19:32Z. --- ### What Ghost of Tsushima Director's Cut (PPSA02225) registers shaders about 400 times a second during its opening. It registers the same code with the same registers over and over, each time through a header at a new address. Every registration decodes the header's registers into a `RegisteredShaderState`, three dense register sets of about 10 KiB, and its snapshot keeps that copy for as long as the registry holds it. In 200 s the title made 80,000 registrations: none matched a published snapshot's header address, so all 80,000 were published, and 68,144 of them (85 %) repeated the code and decoded registers of a snapshot at another header address. Only 11,856 were distinct. A jemalloc profile at 240 s put about 1 GiB of live heap under `RegisterShader`'s register sets (`InitialContextRegisters` copies, the `Registers` built from the user-config initializer, `Registers::mark`). This title's opening is where the 20 GiB memory cap ends my runs. ### How - A new snapshot takes the `RegisteredShaderState` of a published snapshot at the same code address with the same type, the same code and equal decoded registers, instead of keeping its own copy. The lookup runs under the registry mutex, next to the existing same-header check. - `RegisteredShaderState` gets a defaulted `operator==`; `Registers` already compares by entries. - The state is immutable once published (`shared_ptr<const RegisteredShaderState>`), and its users copy it into a `QueueState` before using it. `Registers::Fingerprint`'s cache is only written on those live copies. - The preparation is not shared. Its request carries the header bytes and address, and this title's headers at other addresses hold other pointers. ### Tested Ubuntu 26.04.1, GCC 15.2, NVIDIA RTX 4070 Ti (615.71.09). **Ghost of Tsushima Director's Cut (PPSA02225).** `main` can't take this title past its first minutes, so these runs use @EagleBearerYS's `test/ghost-9742-9717` stack with #8115, #8121, #8213, #8608 and #9318, built from release libs with and without this change, plus a local counter of registrations. Each run is warm, 200 s, with a 20 GiB cap. Both builds see the same registration stream: 90,000 registrations, 76,792 of them (85 %) repeating another header's code and registers, and 13,208 distinct. | run | this change | RssAnon at 100 s | RssAnon at 190 s | |---|---|---|---| | S1 | yes | 3,100 MB | 4,632 MB | | R1 | no | 4,987 MB | 5,319 MB | | S2 | yes | 4,202 MB | 4,664 MB | | R2 | no | 4,982 MB | 5,284 MB | That's about 650 MB less at 190 s, and more earlier, where the registrations are densest. **Tests** - `agc_driver_prepared_shaders`'s shared-code case now checks the state at both points. While its two headers have different registers, they keep separate states. Once both are type 2 with the same code and registers, they hold the same state, with the header's context register in it. - With the sharing reverted and the test kept, `agc_driver_prepared_shaders` fails: "two headers with the same code and registers decoded their registered state twice". - The 25 tests matching `^agc_driver_prepared_shaders|^agc_driver_graphics$|^agc_driver_submit$|draw_cache` pass 3 of 3. - Khronos validation layer 1.4.363, `main` against this branch over `^agc_driver_prepared_shaders|draw_cache`: 24 of 24 pass on both, with no VUID on either. GCC 15.2 crashed (an internal compiler error in `Pm4.hpp`) building `agc_driver_draw_cache_relocation_tests` on the `main` side, so that one test ran from an earlier build there. - Windows 11 VM (GeForce GT 1030, NVIDIA R580, WinLibs GCC, Release, `ANYPS5_REQUIRE_VULKAN=1`): the same 25 tests pass 3 of 3 on this branch and on its merge base. ### 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: none - [x] GPUs tested: NVIDIA RTX 4070 Ti (Linux); NVIDIA GeForce GT 1030 (Windows); NVIDIA GeForce RTX 3080, driver 617.42, Windows 11 (@VakzOs) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-23 03:21:00 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed exact head e118657d4d. The implementation shares only immutable decoded register state across snapshots with the same code address, type, code bytes and decoded state; prepared state remains per snapshot. I found no correctness issue in the diff. The focused regression checks both non-sharing when registers differ and sharing after they match. The PR reports 25 focused tests passing 3/3 and matched main/change validation runs with no VUIDs.

Eligibility, conventions, intake, labels and report checks pass. The hosted product build jobs are skipped, and I have not run the GPU tests locally because this environment has no Vulkan device.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623511611 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9991#pullrequestreview-5623511611). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T03:21:00Z. **Review:** APPROVED --- Reviewed exact head e118657d4d2c7c52524e2041a348c28bf12d8fd7. The implementation shares only immutable decoded register state across snapshots with the same code address, type, code bytes and decoded state; prepared state remains per snapshot. I found no correctness issue in the diff. The focused regression checks both non-sharing when registers differ and sharing after they match. The PR reports 25 focused tests passing 3/3 and matched main/change validation runs with no VUIDs. Eligibility, conventions, intake, labels and report checks pass. The hosted product build jobs are skipped, and I have not run the GPU tests locally because this environment has no Vulkan device.
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!9991
No description provided.