fix(agc): share the code of registrations of one program #10685

Merged
milescoviello merged 3 commits from upstream-pr/10685 into main 2026-11-02 10:23:46 +00:00
milescoviello commented 2026-10-24 17:45:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-24T17:45:19Z.


What

Every shader registration keeps a ShaderSnapshot with a copy of its program's code, and a program registered through several headers kept one copy per header. In Ghost of Tsushima (PPSA02225) that adds up: 172 s in, the registry held 94,000 snapshots at 14,321 code addresses, with 185.8 MiB of code of which 43.0 MiB was distinct (counted by a local diagnostic over the live registry, numbers below).

A snapshot's code is now a SharedCode: a shared, immutable buffer that reads like the std::vector<std::uint32_t> it replaces (data(), size(), iteration, ==, conversion to const std::vector&, and std::span builds from it). RegisterShader already compares a new snapshot's code with the snapshots registered at the same code address. When the words are equal, the new snapshot now takes that snapshot's buffer, so all headers of one program share one copy. Equality checks the buffer first, so later comparisons between snapshots of one program no longer compare the words.

How

  • ShaderRegistry.hpp: SharedCode holds a std::shared_ptr to the words. Writes go through Mutable(), which copies a buffer that is already shared, and through resize and assign. There is no non-const data(), so a span over a non-const snapshot can't copy by accident. Snapshots are published as shared_ptr<const ShaderSnapshot>, so a published buffer is never written.
  • ShaderRegistry.cpp: the second pass over the code address's snapshots in RegisterShader takes the buffer of the first snapshot with equal code, then looks for an equal registered state as before. The raw-shader reader and RegisterShader's copy from guest memory write through Mutable().
  • PreparedGraphics.cpp (a test fixture) grows its code through Mutable().

Tested

Ubuntu 26.04.1, GCC 15.2, Release, RTX 4070 Ti, NVIDIA 615.71.09, on main dc9f1ef656.

  • SharedCodeHeaders in agc_driver_prepared_shaders registers a vertex and a mesh header over one program and now also requires their snapshots to share the code buffer. With the sharing step removed (a mutant), the indirect, uncached and debug modes, which run that test, fail 3 of 3: two headers of one program keep separate copies of its code.
  • The 76 tests of every test source that uses ShaderSnapshot (register_key|_submit|alpha_to_coverage|1555|565|dual_source|packed_|pipeline_journal|prepared_|program_writes_memory|reversed_order|sample_multisample_target|vertex_clip_cull|^agc_driver_graphics$|lazy_resolved|draw_cache): 76 of 76 pass 3 of 3 on this head (4f15f29b42) and on main.
  • Windows 11 VM (GeForce GT 1030, 582.78, WinLibs GCC 15.2.0 r7, Release), main dc9f1ef656 vs this head: the same pattern matches 77 tests there, and they pass 3 of 3 on each side (agc_driver_packed_float16_native skips on that GPU).
  • Windows hardware (ycbuzi, Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 r7, Release): 22d82e41c4 merged onto main 65c2713b6c (no conflicts), the 28 tests of the four changed test sources, ctest --repeat until-fail:2: 28 of 28 in both runs on a GeForce RTX 4070 SUPER and a GTX 1650 SUPER (NVIDIA 616.92) and on an AMD Radeon RDNA2 iGPU (1002:164e, AMD 32.0.21030.2001). These tests don't build against main's ShaderRegistry files, so there is no main arm.
  • Khronos validation 1.4.363 over the same 76, on main and on this head: 76 of 76 pass on each, with no layer-missing line. Both report the same 14 VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605, and none is new in the head.
  • Ghost of Tsushima (PPSA02225), live registry counts. A local diagnostic counted the registry every 2,000 registrations, including the distinct code buffers by address. A is got/g14, my local stack of open PRs that gets the title through its opening, plus that diagnostic; B is A plus this change. 16 GB cap, warm cache, both runs end at the cap.
Registrations Code addresses Snapshots Code buffers, A Code buffers, B
22,000 1,810 22,001 22,001, 37.8 MiB 1,810, 4.9 MiB
46,000 3,323 46,001 46,001, 76.2 MiB 3,323, 9.9 MiB
70,000 5,156 70,001 70,001, 120.9 MiB 5,156, 16.7 MiB
94,000 14,321 94,001 94,001, 185.8 MiB 14,321, 53.0 MiB

At 94,000 registrations that is 132.8 MiB less code held, one buffer per code address. The 10,862 distinct programs among those 14,321 addresses come to 43.0 MiB, so interning across addresses would save about 10 MiB more; this change keeps to one address. The heap profile that pointed here had RegisterShader holding 295 MiB live 900 s into the opening, 197.5 MiB of it these code copies.

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 (it lowers the memory Ghost of Tsushima, PPSA02225, needs in its opening)
  • GPUs tested: NVIDIA GeForce RTX 4070 Ti, driver 615.71.09, Ubuntu 26.04.1; NVIDIA GeForce GT 1030, driver 582.78, Windows 11 VM; NVIDIA GeForce RTX 4070 SUPER and GTX 1650 SUPER, driver 616.92, and AMD Radeon RDNA2 iGPU, driver 32.0.21030.2001, Windows 11 (ycbuzi)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4997673003 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10685). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-24T17:45:19Z. --- ### What Every shader registration keeps a `ShaderSnapshot` with a copy of its program's code, and a program registered through several headers kept one copy per header. In Ghost of Tsushima (PPSA02225) that adds up: 172 s in, the registry held 94,000 snapshots at 14,321 code addresses, with 185.8 MiB of code of which 43.0 MiB was distinct (counted by a local diagnostic over the live registry, numbers below). A snapshot's `code` is now a `SharedCode`: a shared, immutable buffer that reads like the `std::vector<std::uint32_t>` it replaces (`data()`, `size()`, iteration, `==`, conversion to `const std::vector&`, and `std::span` builds from it). `RegisterShader` already compares a new snapshot's code with the snapshots registered at the same code address. When the words are equal, the new snapshot now takes that snapshot's buffer, so all headers of one program share one copy. Equality checks the buffer first, so later comparisons between snapshots of one program no longer compare the words. ### How - `ShaderRegistry.hpp`: `SharedCode` holds a `std::shared_ptr` to the words. Writes go through `Mutable()`, which copies a buffer that is already shared, and through `resize` and `assign`. There is no non-const `data()`, so a span over a non-const snapshot can't copy by accident. Snapshots are published as `shared_ptr<const ShaderSnapshot>`, so a published buffer is never written. - `ShaderRegistry.cpp`: the second pass over the code address's snapshots in `RegisterShader` takes the buffer of the first snapshot with equal code, then looks for an equal registered state as before. The raw-shader reader and `RegisterShader`'s copy from guest memory write through `Mutable()`. - `PreparedGraphics.cpp` (a test fixture) grows its code through `Mutable()`. ### Tested Ubuntu 26.04.1, GCC 15.2, Release, RTX 4070 Ti, NVIDIA 615.71.09, on `main` dc9f1ef656. - `SharedCodeHeaders` in `agc_driver_prepared_shaders` registers a vertex and a mesh header over one program and now also requires their snapshots to share the code buffer. With the sharing step removed (a mutant), the `indirect`, `uncached` and `debug` modes, which run that test, fail 3 of 3: `two headers of one program keep separate copies of its code`. - The 76 tests of every test source that uses `ShaderSnapshot` (`register_key|_submit|alpha_to_coverage|1555|565|dual_source|packed_|pipeline_journal|prepared_|program_writes_memory|reversed_order|sample_multisample_target|vertex_clip_cull|^agc_driver_graphics$|lazy_resolved|draw_cache`): 76 of 76 pass 3 of 3 on this head (`4f15f29b42`) and on `main`. - Windows 11 VM (GeForce GT 1030, 582.78, WinLibs GCC 15.2.0 r7, Release), `main` dc9f1ef656 vs this head: the same pattern matches 77 tests there, and they pass 3 of 3 on each side (`agc_driver_packed_float16_native` skips on that GPU). - **Windows hardware** (ycbuzi, Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 r7, Release): `22d82e41c4` merged onto `main` 65c2713b6c (no conflicts), the 28 tests of the four changed test sources, `ctest --repeat until-fail:2`: 28 of 28 in both runs on a GeForce RTX 4070 SUPER and a GTX 1650 SUPER (NVIDIA 616.92) and on an AMD Radeon RDNA2 iGPU (`1002:164e`, AMD 32.0.21030.2001). These tests don't build against `main`'s `ShaderRegistry` files, so there is no `main` arm. - Khronos validation 1.4.363 over the same 76, on `main` and on this head: 76 of 76 pass on each, with no layer-missing line. Both report the same 14 `VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605`, and none is new in the head. - **Ghost of Tsushima (PPSA02225), live registry counts.** A local diagnostic counted the registry every 2,000 registrations, including the distinct code buffers by address. A is `got/g14`, my local stack of open PRs that gets the title through its opening, plus that diagnostic; B is A plus this change. 16 GB cap, warm cache, both runs end at the cap. | Registrations | Code addresses | Snapshots | Code buffers, A | Code buffers, B | |---|---|---|---|---| | 22,000 | 1,810 | 22,001 | 22,001, 37.8 MiB | 1,810, 4.9 MiB | | 46,000 | 3,323 | 46,001 | 46,001, 76.2 MiB | 3,323, 9.9 MiB | | 70,000 | 5,156 | 70,001 | 70,001, 120.9 MiB | 5,156, 16.7 MiB | | 94,000 | 14,321 | 94,001 | 94,001, 185.8 MiB | 14,321, 53.0 MiB | At 94,000 registrations that is 132.8 MiB less code held, one buffer per code address. The 10,862 distinct programs among those 14,321 addresses come to 43.0 MiB, so interning across addresses would save about 10 MiB more; this change keeps to one address. The heap profile that pointed here had `RegisterShader` holding 295 MiB live 900 s into the opening, 197.5 MiB of it these code copies. ### 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] Fixes: none - [x] Unblocks: none (it lowers the memory Ghost of Tsushima, PPSA02225, needs in its opening) - [x] GPUs tested: NVIDIA GeForce RTX 4070 Ti, driver 615.71.09, Ubuntu 26.04.1; NVIDIA GeForce GT 1030, driver 582.78, Windows 11 VM; NVIDIA GeForce RTX 4070 SUPER and GTX 1650 SUPER, driver 616.92, and AMD Radeon RDNA2 iGPU, driver 32.0.21030.2001, Windows 11 (ycbuzi) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-24 19:27:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T19:27:33Z.

Review: APPROVED


Reviewed exact head 4f15f29b4215be2105bbad01a9447ffbf130e6be. SharedCode shares an immutable code buffer only after equal code is found for the same code address; Mutable() copies when storage is shared, so construction/raw reads can still write before publication and later edits detach safely. Registered state reuse remains separately gated by shader type and register equality. The new regression checks that two headers over one program share their code while retaining distinct registered states. git diff --check and conventions pass. The PR reports 76 focused AGC tests passing 3/3 on both Linux and Windows with validation, plus title measurements showing the expected memory reduction; I did not rerun the GPU tests locally. Hosted policy checks pass; product jobs are skipped by the current intake policy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639609660 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10685#pullrequestreview-5639609660). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T19:27:33Z. **Review:** APPROVED --- Reviewed exact head `4f15f29b4215be2105bbad01a9447ffbf130e6be`. `SharedCode` shares an immutable code buffer only after equal code is found for the same code address; `Mutable()` copies when storage is shared, so construction/raw reads can still write before publication and later edits detach safely. Registered state reuse remains separately gated by shader type and register equality. The new regression checks that two headers over one program share their code while retaining distinct registered states. `git diff --check` and conventions pass. The PR reports 76 focused AGC tests passing 3/3 on both Linux and Windows with validation, plus title measurements showing the expected memory reduction; I did not rerun the GPU tests locally. Hosted policy checks pass; product jobs are skipped by the current intake policy.
MarcZX10 commented 2026-10-24 20:23:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:23:50Z.

Review: APPROVED


Reviewed the full diff at 4f15f29b42. SharedCode provides copy-on-write mutation for unpublished snapshots and shares immutable words only after equality at the same code address; the test asserts two headers over one program share storage while retaining distinct registered state. The author reports a mutation control failing without the share step, 76 focused tests passing repeatedly on Linux and Windows, matching validation-layer results, and live Ghost of Tsushima registry measurements reducing code storage by 132.8 MiB at 94,000 registrations. Hosted eligibility, conventions, intake and labels pass; product jobs were skipped by path policy. I found no correctness blocker in the reviewed ownership/mutation paths.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639812732 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10685#pullrequestreview-5639812732). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T20:23:50Z. **Review:** APPROVED --- Reviewed the full diff at 4f15f29b4215be2105bbad01a9447ffbf130e6be. SharedCode provides copy-on-write mutation for unpublished snapshots and shares immutable words only after equality at the same code address; the test asserts two headers over one program share storage while retaining distinct registered state. The author reports a mutation control failing without the share step, 76 focused tests passing repeatedly on Linux and Windows, matching validation-layer results, and live Ghost of Tsushima registry measurements reducing code storage by 132.8 MiB at 94,000 registrations. Hosted eligibility, conventions, intake and labels pass; product jobs were skipped by path policy. I found no correctness blocker in the reviewed ownership/mutation paths.
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!10685
No description provided.