perf(agc): keep the highest index of a large in-place index buffer while it is unchanged #4221

Merged
oneandonlydean merged 1 commit from upstream-pr/4221 into main 2026-10-12 04:32:00 +00:00
oneandonlydean commented 2026-10-12 01:43:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T01:43:22Z.


What

An indexed draw that binds its index buffer in place from a host import (#2253) scans the bound range for its highest index on every draw. The copied path does not: a reused snapshot carries the value it was scanned with. A draw of many indices therefore pays for the scan each time, and an indirect draw, which binds the whole declared index buffer, scans the whole buffer per draw.

The recorder now keeps the highest index of an in-place index range of 4096 indices or more, together with the write generation collected before the scan. A draw takes the kept value while the range is unchanged since (GuestMemory::UnchangedSince and the allocation registry generation, the proof a reused draw snapshot uses). A guest write to the range, another address, another size, another index width or another restart rule scans again. Where the imported range is not write-watched (CollectWrites returns 0: Windows direct imports, and Linux drivers whose imports are compared), nothing is kept and every draw scans as before.

A range of fewer than 4096 indices is scanned at every draw, as on main: no write collect, no lookup. #2253 tried keeping the value for every in-place range and left it out, because in a title with small index ranges the write collect and lookup cost more than the scan (Hades II, 37.5 against 38.4 fps). The size cut is where the two costs cross, measured below; both sides of it give the same highest index, so it decides only which is paid.

The scan runs on main for any index range whose allocation is host-imported, so this does not need #290. #290 imports more (a larger budget), which is where one title we test with started to pay for the scans.

The cut

Measured on an i9-14900KF, Linux, Release.

  • The scan alone, in a loop over warm memory (a benchmark added to drawInputInPlaceTests for this, not part of the commit): 0.36 ns per index for 16-bit and for 32-bit indices, with or without the restart rule. 1024 indices take 0.37 us, 4096 take 1.45 us.
  • The write collect and lookup of a kept value, same loop: 0.32 us per draw whatever the size. There the two cross near 900 indices.
  • In a 3D title we test with, both timed inside every in-place indexed draw of a build with counters (not part of the commit), 1.4 million draws in one level and 1.2 million in another run. The scan is 0.4 to 0.47 ns per index plus the cost of memory that is not warm. The collect and lookup are dearer than in the loop, since the tracker and a table of thousands of ranges are not warm either:
range, 16-bit indices draws in the two runs scan per draw collect and lookup per draw
256 to 511 121,000 / 83,000 0.23 / 0.24 us 0.58 / 0.65 us
512 to 1023 137,000 / 104,000 0.41 / 0.44 us 0.86 / 0.85 us
1024 to 2047 105,000 / 97,000 0.73 / 0.67 us 1.21 / 0.82 us
2048 to 4095 146,000 / 109,000 1.34 / 1.33 us 1.14 / 1.09 us
4096 to 8191 94,000 / 122,000 2.51 / 2.46 us 1.39 / 1.01 us
131,072 to 262,143 74,000 / 0 70.5 us 1.70 us

There they cross between 2,000 and 3,000 indices. The 32-bit ranges of that title all hold 8,000 indices or more.

  • In the 2D title of the earlier measurement the collect and lookup took 0.21 us per draw (timer reads included), which the scan reaches near 500 indices.

The cut is 4096 indices, the next power of two above the highest crossing measured. Below it a draw does what it does on main. In the benchmark loop the call that makes the choice adds 2 to 6 ns to a scan below the cut (16 indices: 5.7 ns on main's scan, 10.5 ns through the new function; 2048 indices: 725.5 against 727.7 ns); from 4096 indices up a kept value takes 0.32 us where the scan takes 1.45 us or more.

A range that changes between its draws is scanned and kept again each time, which costs the collect, the lookup and the insert (0.3 to 1.8 us for the insert in the title) on top of the scan; at 4096 indices that is about as much again as the scan, and less above. In the title 99.8% of the draws above the cut found their value.

Tested

Linux (CachyOS, GCC 16.2.1, Release, BUILD_TESTING=ON, ANYPS5_ENABLE_SPIRV_TOOLS=ON), RTX 4090, NVIDIA 610.57.04. The branch is one commit, 1dc342dd, on main 43bc17f7.

  • New test agc_draw_inputs_in_place (agc_driver_recorder_tests --draw-inputs-in-place-only) runs drawInputInPlaceTests, which ctest did not run before (only the binary's default path called it), with new checks. For a 16-bit range of exactly 4096 indices: the first use scans it, the second takes the kept value, the other restart rule and another size scan again, a guest write to the range gives the new highest index, and a range of restart indices alone keeps "no index". A range two bytes shorter is scanned at both of its uses. The same for 32-bit indices: 4095 indices are scanned twice, 4096 are kept. It passes, 10 of 10 repeats. With Recorder::KnownHighestIndex made to find nothing it fails with an unchanged in-place index range was scanned again; with the UnchangedSince check removed, with an in-place index range kept its highest index over a guest write; with the cut lowered to one index, with an in-place index range below the kept size was kept.
  • The whole agc_driver_recorder_tests binary (default path, not in ctest): 5 of 5 runs pass on main 43bc17f7 and on the branch.
  • ctest -R "agc|shader": main 43bc17f7 424 tests, branch 425. On both the same 2 fail (agc_vector_window, agc_driver_prepared_shaders_registered_workgroup_sync), in a parallel run and again when run alone.
  • lavapipe (LLVM 22.1.8): the new test passes, 10 of 10 repeats, with its new checks not run: lavapipe's host imports are compared, not watched, and the test stops before them. The rest of ctest was not run there on this head.
  • python3 tools/check_conventions.py --base 43bc17f7 passes.
  • A 3D title we test with, a level with 6,400 draws per frame of which 1,380 are indexed indirect, warm caches, APS5_PROFILE_DRAW=1, over the same 280 level frames. Both builds are main 0a8b1d32 with #425, #454, #2490, #2491, #3702, #3874 and #290, which the title needs to reach the level; the first is without this commit. Other builds and tests ran on the machine during all three runs, and the two runs without the commit differ by 19%, so the frame times bound the gain loosely; the vertex phase is the part this changes:
build frame time vertex phase per draw
without, two runs 473.6 ms, 396.4 ms 14.8 us, 13.3 us
with, one run 378.6 ms 2.0 us

An earlier form that kept every range, on main a11c8f7f with the same PRs in a quieter hour, one run each: 433.1 ms without, 345.3 and 351.4 ms with.

Not tested:

  • Windows, AMD and Intel. On Windows direct host imports are not write-watched, so the kept value is never used there.
  • The title #2253 measured, or any other title with thousands of small indexed draws per frame. Its draws take the same scan as on main plus the size compare; a frame rate was not measured.
  • The full ctest, on this head (only agc|shader), and main after 43bc17f7.
  • The 3D title on plain main (it stops before the levels) and on this branch without the other PRs.
  • A guest store into an index range inside one collect epoch (between two ordering points of a queue) after the range was collected: the kept value is not rescanned until the next epoch, while the GPU reads the new indices in place. main has the same exposure from its scan to the GPU's read.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823949492 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4221). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-12T01:43:22Z. --- ### What An indexed draw that binds its index buffer in place from a host import (#2253) scans the bound range for its highest index on every draw. The copied path does not: a reused snapshot carries the value it was scanned with. A draw of many indices therefore pays for the scan each time, and an indirect draw, which binds the whole declared index buffer, scans the whole buffer per draw. The recorder now keeps the highest index of an in-place index range of 4096 indices or more, together with the write generation collected before the scan. A draw takes the kept value while the range is unchanged since (`GuestMemory::UnchangedSince` and the allocation registry generation, the proof a reused draw snapshot uses). A guest write to the range, another address, another size, another index width or another restart rule scans again. Where the imported range is not write-watched (`CollectWrites` returns 0: Windows direct imports, and Linux drivers whose imports are compared), nothing is kept and every draw scans as before. A range of fewer than 4096 indices is scanned at every draw, as on `main`: no write collect, no lookup. #2253 tried keeping the value for every in-place range and left it out, because in a title with small index ranges the write collect and lookup cost more than the scan (Hades II, 37.5 against 38.4 fps). The size cut is where the two costs cross, measured below; both sides of it give the same highest index, so it decides only which is paid. The scan runs on `main` for any index range whose allocation is host-imported, so this does not need #290. #290 imports more (a larger budget), which is where one title we test with started to pay for the scans. ### The cut Measured on an i9-14900KF, Linux, Release. - The scan alone, in a loop over warm memory (a benchmark added to `drawInputInPlaceTests` for this, not part of the commit): 0.36 ns per index for 16-bit and for 32-bit indices, with or without the restart rule. 1024 indices take 0.37 us, 4096 take 1.45 us. - The write collect and lookup of a kept value, same loop: 0.32 us per draw whatever the size. There the two cross near 900 indices. - In a 3D title we test with, both timed inside every in-place indexed draw of a build with counters (not part of the commit), 1.4 million draws in one level and 1.2 million in another run. The scan is 0.4 to 0.47 ns per index plus the cost of memory that is not warm. The collect and lookup are dearer than in the loop, since the tracker and a table of thousands of ranges are not warm either: | range, 16-bit indices | draws in the two runs | scan per draw | collect and lookup per draw | |---|---|---|---| | 256 to 511 | 121,000 / 83,000 | 0.23 / 0.24 us | 0.58 / 0.65 us | | 512 to 1023 | 137,000 / 104,000 | 0.41 / 0.44 us | 0.86 / 0.85 us | | 1024 to 2047 | 105,000 / 97,000 | 0.73 / 0.67 us | 1.21 / 0.82 us | | 2048 to 4095 | 146,000 / 109,000 | 1.34 / 1.33 us | 1.14 / 1.09 us | | 4096 to 8191 | 94,000 / 122,000 | 2.51 / 2.46 us | 1.39 / 1.01 us | | 131,072 to 262,143 | 74,000 / 0 | 70.5 us | 1.70 us | There they cross between 2,000 and 3,000 indices. The 32-bit ranges of that title all hold 8,000 indices or more. - In the 2D title of the earlier measurement the collect and lookup took 0.21 us per draw (timer reads included), which the scan reaches near 500 indices. The cut is 4096 indices, the next power of two above the highest crossing measured. Below it a draw does what it does on `main`. In the benchmark loop the call that makes the choice adds 2 to 6 ns to a scan below the cut (16 indices: 5.7 ns on `main`'s scan, 10.5 ns through the new function; 2048 indices: 725.5 against 727.7 ns); from 4096 indices up a kept value takes 0.32 us where the scan takes 1.45 us or more. A range that changes between its draws is scanned and kept again each time, which costs the collect, the lookup and the insert (0.3 to 1.8 us for the insert in the title) on top of the scan; at 4096 indices that is about as much again as the scan, and less above. In the title 99.8% of the draws above the cut found their value. ### Tested Linux (CachyOS, GCC 16.2.1, Release, `BUILD_TESTING=ON`, `ANYPS5_ENABLE_SPIRV_TOOLS=ON`), RTX 4090, NVIDIA 610.57.04. The branch is one commit, 1dc342dd, on `main` 43bc17f7. - New test `agc_draw_inputs_in_place` (`agc_driver_recorder_tests --draw-inputs-in-place-only`) runs `drawInputInPlaceTests`, which `ctest` did not run before (only the binary's default path called it), with new checks. For a 16-bit range of exactly 4096 indices: the first use scans it, the second takes the kept value, the other restart rule and another size scan again, a guest write to the range gives the new highest index, and a range of restart indices alone keeps "no index". A range two bytes shorter is scanned at both of its uses. The same for 32-bit indices: 4095 indices are scanned twice, 4096 are kept. It passes, 10 of 10 repeats. With `Recorder::KnownHighestIndex` made to find nothing it fails with `an unchanged in-place index range was scanned again`; with the `UnchangedSince` check removed, with `an in-place index range kept its highest index over a guest write`; with the cut lowered to one index, with `an in-place index range below the kept size was kept`. - The whole `agc_driver_recorder_tests` binary (default path, not in `ctest`): 5 of 5 runs pass on `main` 43bc17f7 and on the branch. - `ctest -R "agc|shader"`: `main` 43bc17f7 424 tests, branch 425. On both the same 2 fail (`agc_vector_window`, `agc_driver_prepared_shaders_registered_workgroup_sync`), in a parallel run and again when run alone. - lavapipe (LLVM 22.1.8): the new test passes, 10 of 10 repeats, with its new checks not run: lavapipe's host imports are compared, not watched, and the test stops before them. The rest of `ctest` was not run there on this head. - `python3 tools/check_conventions.py --base 43bc17f7` passes. - A 3D title we test with, a level with 6,400 draws per frame of which 1,380 are indexed indirect, warm caches, `APS5_PROFILE_DRAW=1`, over the same 280 level frames. Both builds are `main` 0a8b1d32 with #425, #454, #2490, #2491, #3702, #3874 and #290, which the title needs to reach the level; the first is without this commit. Other builds and tests ran on the machine during all three runs, and the two runs without the commit differ by 19%, so the frame times bound the gain loosely; the vertex phase is the part this changes: | build | frame time | vertex phase per draw | |---|---|---| | without, two runs | 473.6 ms, 396.4 ms | 14.8 us, 13.3 us | | with, one run | 378.6 ms | 2.0 us | An earlier form that kept every range, on `main` a11c8f7f with the same PRs in a quieter hour, one run each: 433.1 ms without, 345.3 and 351.4 ms with. Not tested: - Windows, AMD and Intel. On Windows direct host imports are not write-watched, so the kept value is never used there. - The title #2253 measured, or any other title with thousands of small indexed draws per frame. Its draws take the same scan as on `main` plus the size compare; a frame rate was not measured. - The full `ctest`, on this head (only `agc|shader`), and `main` after 43bc17f7. - The 3D title on plain `main` (it stops before the levels) and on this branch without the other PRs. - A guest store into an index range inside one collect epoch (between two ordering points of a queue) after the range was collected: the kept value is not rescanned until the next epoch, while the GPU reads the new indices in place. `main` has the same exposure from its scan to the GPU's read. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes (Claude Code)
github-actions[bot] commented 2026-10-12 01:55:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T01:55:02Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#1368 core/libs/prx/libSceAgcDriver/CMakeLists.txt L821, L823
#4017 core/libs/prx/libSceAgcDriver/CMakeLists.txt L823
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116365054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4221#issuecomment-6116365054). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T01:55:02Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1368 | `core/libs/prx/libSceAgcDriver/CMakeLists.txt` [L821](https://github.com/boykopovar/AnyPS5/blob/18af2172112c311c4b59e8fec992a6a53a90d249/core/libs/prx/libSceAgcDriver/CMakeLists.txt#L821), [L823](https://github.com/boykopovar/AnyPS5/blob/18af2172112c311c4b59e8fec992a6a53a90d249/core/libs/prx/libSceAgcDriver/CMakeLists.txt#L823) | | | | #4017 | `core/libs/prx/libSceAgcDriver/CMakeLists.txt` [L823](https://github.com/boykopovar/AnyPS5/blob/18af2172112c311c4b59e8fec992a6a53a90d249/core/libs/prx/libSceAgcDriver/CMakeLists.txt#L823) | | |
AlxcBTW commented 2026-10-12 02:42:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T02:42:53Z.


Data point from Sword of the Sea (PPSA25344), main menu: no measurable change in either direction.

Setup: Windows 11, RTX 5070 Laptop, WinLibs GCC 15.2. Both trees are main a11c8f7f7 plus our local patches for this title; B adds #4221, with a tests-only cherry-pick conflict and the library code unchanged. Env on both: APS5_WRITE_WATCH_IMPORTS=watch, so the in-place imports are write-watched. Rows ran 180 s each, alternating, on the same shader cache, with no device loss.

Row Tree frame / GPU / worker ms DRAW_INDEX_INDIRECT per 10 s
a1 main 344 / 128 / 342 120 draws, 48 ms
b1 + #4221 343 / 128 / 342 116 draws, 34 ms
a2 main 350 / 127 / 348 116 draws, 43 ms
b2 + #4221 349 / 128 / 347 116 draws, 79 ms

The two main rows differ from each other as much as from the #4221 rows. This menu issues only ~4 indirect indexed draws per frame (~116 per 10 s at ~2.9 FPS), so the index scan isn't a visible cost here; its q0_indirect time is dominated by dispatches and uploads the change doesn't touch. That's neither a regression nor evidence against the change, just a title that doesn't exercise a large in-place index buffer on a hot path.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116795692 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4221#issuecomment-6116795692). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-12T02:42:53Z. --- Data point from Sword of the Sea (PPSA25344), main menu: no measurable change in either direction. **Setup:** Windows 11, RTX 5070 Laptop, WinLibs GCC 15.2. Both trees are `main` a11c8f7f7 plus our local patches for this title; B adds #4221, with a tests-only cherry-pick conflict and the library code unchanged. Env on both: `APS5_WRITE_WATCH_IMPORTS=watch`, so the in-place imports are write-watched. Rows ran 180 s each, alternating, on the same shader cache, with no device loss. | Row | Tree | frame / GPU / worker ms | `DRAW_INDEX_INDIRECT` per 10 s | |---|---|---|---| | a1 | main | 344 / 128 / 342 | 120 draws, 48 ms | | b1 | + #4221 | 343 / 128 / 342 | 116 draws, 34 ms | | a2 | main | 350 / 127 / 348 | 116 draws, 43 ms | | b2 | + #4221 | 349 / 128 / 347 | 116 draws, 79 ms | The two `main` rows differ from each other as much as from the #4221 rows. This menu issues only ~4 indirect indexed draws per frame (~116 per 10 s at ~2.9 FPS), so the index scan isn't a visible cost here; its `q0_indirect` time is dominated by dispatches and uploads the change doesn't touch. That's neither a regression nor evidence against the change, just a title that doesn't exercise a large in-place index buffer on a hot path. AI-assisted: yes (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!4221
No description provided.