perf(agc): keep the collect epoch at a wait an earlier wait already ordered #16897

Merged
klosixgaming-arch merged 4 commits from upstream-pr/16897 into main 2026-11-14 04:03:07 +00:00
klosixgaming-arch commented 2026-11-07 21:04:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-11-07T21:04:38Z.


What

A WAIT_REG_MEM that is already satisfied when the queue worker reaches it starts a new collect epoch, so every range collected before it (render targets, shader resources, vertex ranges) is walked again with GetWriteWatch after it. When the awaited value was written by the CPU long before and has not changed, the wait orders no new CPU store: everything the CPU stored before that value was already ordered by the earlier wait that saw it.

Each queue worker now remembers, per awaited address, the value an earlier wait saw and the write generation of the address when that wait bumped the epoch. A later wait that finds the same value, after a fresh CollectWritesUncached of the awaited bytes, and whose range is UnchangedSince that generation keeps the epoch. A changed value, any write to the awaited range's 64 KiB block, an unwatched range (generation 0) or APS5_NO_ORDERED_WAITS=1 bumps as before. This applies to the pipelined at-entry path in PacketExecution.cpp and to waitMemory. The memo holds at most 4096 addresses per worker and is cleared when it is full. The [packets] line counts the waits ordered this way.

Why: in Soul Hackers 2 gameplay about 88 waits per frame are satisfied at entry. Sampling them showed func=3 ref=1 on many addresses that no GPU label store wrote, so they are CPU-written flags the title leaves at 1. Each one reset the collect memo. With this change about 57% of those bumps go away (16.3k to 7.1k per 10 s), GetWriteWatch time drops from about 3.4 s to 2.4 s per 10 s, and the draw committer's draw work drops by about a third in the trace. The other bumps are waits whose block the CPU wrote since (the title rewrites its flags), which still start an epoch.

Tested

Windows 11 (build 26200), RTX 5090 (driver 617.42), Ryzen 9 9950X3D, WinLibs GCC 15.2, Release.

Soul Hackers 2 (PPSA03743, 01.002.000), 3840x2160. Each run loads the same save with scripted pad input and stands at the East Shipping District spawn. Trace window APS5_TRACE_SECONDS=85:5, no other trace or profile switch:

{"commit":"fc42d49548b9","dirty":false,"env":{"APS5_LOG_TIMESTAMPS":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"85:5"},"gpu":"NVIDIA GeForce RTX 5090","driver":"NVIDIA 617.42","os":"Windows 10.0.26200","cpu":"AMD Ryzen 9 9950X3D 16-Core Processor","cpu_threads":32,"memory_mib":194197,"title_id":"PPSA03743","title":"Soul Hackers 2","content_version":"01.002.000","resolution":"3840x2160","window":{"spec":"seconds 85:5","first_present":3199,"ms":5033.766},"presents":105,"frame_ms":{"p50":47.593,"p90":48.767,"p99":69.642,"max":70.212},"gpu_busy_ms_per_present":39.514,"worker_busy_pct":{"queue worker 0x0":93.992},"top_phases_ms_per_frame":{"Driver.Packet":45.058,"Graphics.Draw":33.218,"Driver.Packet.draw":28.353,"Driver.Draw":28.124,"Graphics.Draw.draw_and_resource_release":25.823,"Driver.Draw.draw_enqueued":19.238,"Recorder.Sync":5.175,"Graphics.Draw.vertex_upload":5.133},"trace_dropped":false,"trace":"aps5-trace-fc42d49548b9-9312.json.gz"}
{"commit":"b7936ecc5f69","dirty":false,"env":{"APS5_LOG_TIMESTAMPS":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"85:5"},"gpu":"NVIDIA GeForce RTX 5090","driver":"NVIDIA 617.42","os":"Windows 10.0.26200","cpu":"AMD Ryzen 9 9950X3D 16-Core Processor","cpu_threads":32,"memory_mib":194197,"title_id":"PPSA03743","title":"Soul Hackers 2","content_version":"01.002.000","resolution":"3840x2160","window":{"spec":"seconds 85:5","first_present":3209,"ms":5033.627},"presents":112,"frame_ms":{"p50":45.044,"p90":46.088,"p99":46.795,"max":46.823},"gpu_busy_ms_per_present":38.775,"worker_busy_pct":{"queue worker 0x0":93.743},"top_phases_ms_per_frame":{"Driver.Packet":42.129,"Graphics.Draw":25.562,"Driver.Packet.draw":20.511,"Driver.Draw":20.306,"Graphics.Draw.draw_and_resource_release":19.306,"Driver.Draw.draw_enqueued":12.560,"Recorder.Sync":7.149,"Driver.Draw.shader_compile_and_link":4.226},"trace_dropped":false,"trace":"aps5-trace-b7936ecc5f69-57468.json.gz"}

105 -> 112 presents in the window, frame p50 47.6 -> 45.0 ms. Traces: aps5-trace-fc42d49548b9-9312.json.gz (main), aps5-trace-b7936ecc5f69-57468.json.gz (change).

Interval summaries (APS5_TRACE_SUMMARY_EVERY=10), median presents per 10 s over the gameplay windows of each run. The machine was slower in the afternoon than in the morning for every build, so only compare runs inside one block:

Block Off (APS5_NO_ORDERED_WAITS=1) On
Same build, base fc42d49548 (morning), alternating runs 212.5, 218.5 230, 230
Same build, this branch on main 8f7c957ed0 (afternoon), alternating runs 195, 192, 193, 194 193, 198.5, 199, 200, 203
Same build, this branch merged with #9712 on 8f7c957ed0, alternating runs 228, 238 261, 260

Different builds, morning: main 8f7c957ed0 210; this change 219, 218.5, 220.5. Afternoon: main 8f7c957ed0 211.5, 198, 193.

Different builds back to back in the evening, the gameplay windows from 50 to 90 s of each run (the GPU time per present is the same, 37.8 to 38.2 ms):

  • main 8f7c957ed0: 214, 215, 215, 215, 215
  • this change: 227, 227, 226, 226

With the GPU still waiting on vertex reads over PCIe (plain main) the gain is about 4%. Once #9712 moves those reads to device memory the frame is bound by the draw committer, and this change gives about 12% (GPU 12.2 ms per present in both).

Stability: 21 runs with this change in the build (morning, afternoon and evening, switch on and off, alone and with #9712). One of them, the first morning run, stopped about 47 s in, during loading, with an access violation in the title's SPFXUnityPlugin on UnityGfxDeviceWorker (read of 0x8). The other 20 reached gameplay without a stop. I have not seen that stop on main (10 runs of 8f7c957ed0 today), so I can't rule the change out, but I could not make it happen again.

Tests: agc_driver_same_queue_label_wait now also waits 16 times on a write-watched CPU flag that an earlier wait saw: 0 extra epoch bumps with this change, 16 with APS5_NO_ORDERED_WAITS=1 (the test then fails). Rewriting the flag with the same value before one wait gives 1 extra bump, as it should. Passing: agc_driver_same_queue_label_wait, agc_driver_same_queue_label_wait_draw_pipeline, agc_driver_recorder, agc_graphics_profile_draw.

Validation layer (Vulkan SDK in aps5-toolchain, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1, ANYPS5_REQUIRE_VULKAN=1, ctest -V): the four tests above on main fc42d49548 (without the new test cases) and on this change: 0 VUID- in both logs, no was not found but was requested by env var VK_INSTANCE_LAYERS.

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; no silent stubs
  • 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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 5090, driver 617.42, Windows 11 (build 26200)
  • AI-assisted: yes. Written with AI assistance and reviewed by me.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5205829559 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16897). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-11-07T21:04:38Z. --- ### What A `WAIT_REG_MEM` that is already satisfied when the queue worker reaches it starts a new collect epoch, so every range collected before it (render targets, shader resources, vertex ranges) is walked again with `GetWriteWatch` after it. When the awaited value was written by the CPU long before and has not changed, the wait orders no new CPU store: everything the CPU stored before that value was already ordered by the earlier wait that saw it. Each queue worker now remembers, per awaited address, the value an earlier wait saw and the write generation of the address when that wait bumped the epoch. A later wait that finds the same value, after a fresh `CollectWritesUncached` of the awaited bytes, and whose range is `UnchangedSince` that generation keeps the epoch. A changed value, any write to the awaited range's 64 KiB block, an unwatched range (generation 0) or `APS5_NO_ORDERED_WAITS=1` bumps as before. This applies to the pipelined at-entry path in `PacketExecution.cpp` and to `waitMemory`. The memo holds at most 4096 addresses per worker and is cleared when it is full. The `[packets]` line counts the waits ordered this way. Why: in Soul Hackers 2 gameplay about 88 waits per frame are satisfied at entry. Sampling them showed `func=3 ref=1` on many addresses that no GPU label store wrote, so they are CPU-written flags the title leaves at 1. Each one reset the collect memo. With this change about 57% of those bumps go away (16.3k to 7.1k per 10 s), `GetWriteWatch` time drops from about 3.4 s to 2.4 s per 10 s, and the draw committer's draw work drops by about a third in the trace. The other bumps are waits whose block the CPU wrote since (the title rewrites its flags), which still start an epoch. ### Tested Windows 11 (build 26200), RTX 5090 (driver 617.42), Ryzen 9 9950X3D, WinLibs GCC 15.2, Release. **Soul Hackers 2 (PPSA03743, 01.002.000), 3840x2160.** Each run loads the same save with scripted pad input and stands at the East Shipping District spawn. Trace window `APS5_TRACE_SECONDS=85:5`, no other trace or profile switch: ``` {"commit":"fc42d49548b9","dirty":false,"env":{"APS5_LOG_TIMESTAMPS":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"85:5"},"gpu":"NVIDIA GeForce RTX 5090","driver":"NVIDIA 617.42","os":"Windows 10.0.26200","cpu":"AMD Ryzen 9 9950X3D 16-Core Processor","cpu_threads":32,"memory_mib":194197,"title_id":"PPSA03743","title":"Soul Hackers 2","content_version":"01.002.000","resolution":"3840x2160","window":{"spec":"seconds 85:5","first_present":3199,"ms":5033.766},"presents":105,"frame_ms":{"p50":47.593,"p90":48.767,"p99":69.642,"max":70.212},"gpu_busy_ms_per_present":39.514,"worker_busy_pct":{"queue worker 0x0":93.992},"top_phases_ms_per_frame":{"Driver.Packet":45.058,"Graphics.Draw":33.218,"Driver.Packet.draw":28.353,"Driver.Draw":28.124,"Graphics.Draw.draw_and_resource_release":25.823,"Driver.Draw.draw_enqueued":19.238,"Recorder.Sync":5.175,"Graphics.Draw.vertex_upload":5.133},"trace_dropped":false,"trace":"aps5-trace-fc42d49548b9-9312.json.gz"} {"commit":"b7936ecc5f69","dirty":false,"env":{"APS5_LOG_TIMESTAMPS":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"85:5"},"gpu":"NVIDIA GeForce RTX 5090","driver":"NVIDIA 617.42","os":"Windows 10.0.26200","cpu":"AMD Ryzen 9 9950X3D 16-Core Processor","cpu_threads":32,"memory_mib":194197,"title_id":"PPSA03743","title":"Soul Hackers 2","content_version":"01.002.000","resolution":"3840x2160","window":{"spec":"seconds 85:5","first_present":3209,"ms":5033.627},"presents":112,"frame_ms":{"p50":45.044,"p90":46.088,"p99":46.795,"max":46.823},"gpu_busy_ms_per_present":38.775,"worker_busy_pct":{"queue worker 0x0":93.743},"top_phases_ms_per_frame":{"Driver.Packet":42.129,"Graphics.Draw":25.562,"Driver.Packet.draw":20.511,"Driver.Draw":20.306,"Graphics.Draw.draw_and_resource_release":19.306,"Driver.Draw.draw_enqueued":12.560,"Recorder.Sync":7.149,"Driver.Draw.shader_compile_and_link":4.226},"trace_dropped":false,"trace":"aps5-trace-b7936ecc5f69-57468.json.gz"} ``` 105 -> 112 presents in the window, frame p50 47.6 -> 45.0 ms. Traces: [aps5-trace-fc42d49548b9-9312.json.gz](https://github.com/klosixgaming-arch/AnyPS5/releases/download/sh2-ordered-waits-traces/aps5-trace-fc42d49548b9-9312.json.gz) (main), [aps5-trace-b7936ecc5f69-57468.json.gz](https://github.com/klosixgaming-arch/AnyPS5/releases/download/sh2-ordered-waits-traces/aps5-trace-b7936ecc5f69-57468.json.gz) (change). Interval summaries (`APS5_TRACE_SUMMARY_EVERY=10`), median presents per 10 s over the gameplay windows of each run. The machine was slower in the afternoon than in the morning for every build, so only compare runs inside one block: | Block | Off (`APS5_NO_ORDERED_WAITS=1`) | On | |---|---|---| | Same build, base fc42d49548 (morning), alternating runs | 212.5, 218.5 | 230, 230 | | Same build, this branch on `main` 8f7c957ed0 (afternoon), alternating runs | 195, 192, 193, 194 | 193, 198.5, 199, 200, 203 | | Same build, this branch merged with #9712 on 8f7c957ed0, alternating runs | 228, 238 | 261, 260 | Different builds, morning: `main` 8f7c957ed0 210; this change 219, 218.5, 220.5. Afternoon: `main` 8f7c957ed0 211.5, 198, 193. Different builds back to back in the evening, the gameplay windows from 50 to 90 s of each run (the GPU time per present is the same, 37.8 to 38.2 ms): - `main` 8f7c957ed0: 214, 215, 215, 215, 215 - this change: 227, 227, 226, 226 With the GPU still waiting on vertex reads over PCIe (plain `main`) the gain is about 4%. Once #9712 moves those reads to device memory the frame is bound by the draw committer, and this change gives about 12% (GPU 12.2 ms per present in both). Stability: 21 runs with this change in the build (morning, afternoon and evening, switch on and off, alone and with #9712). One of them, the first morning run, stopped about 47 s in, during loading, with an access violation in the title's SPFXUnityPlugin on `UnityGfxDeviceWorker` (read of 0x8). The other 20 reached gameplay without a stop. I have not seen that stop on `main` (10 runs of 8f7c957ed0 today), so I can't rule the change out, but I could not make it happen again. **Tests:** `agc_driver_same_queue_label_wait` now also waits 16 times on a write-watched CPU flag that an earlier wait saw: 0 extra epoch bumps with this change, 16 with `APS5_NO_ORDERED_WAITS=1` (the test then fails). Rewriting the flag with the same value before one wait gives 1 extra bump, as it should. Passing: `agc_driver_same_queue_label_wait`, `agc_driver_same_queue_label_wait_draw_pipeline`, `agc_driver_recorder`, `agc_graphics_profile_draw`. **Validation layer** (Vulkan SDK in `aps5-toolchain`, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1`, `ANYPS5_REQUIRE_VULKAN=1`, `ctest -V`): the four tests above on `main` fc42d49548 (without the new test cases) and on this change: 0 `VUID-` in both logs, no `was not found but was requested by env var VK_INSTANCE_LAYERS`. ### 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 - [x] Unimplemented paths throw; no 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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 5090, driver 617.42, Windows 11 (build 26200) - [x] AI-assisted: yes. Written with AI assistance and reviewed by me.
gabichuuuuu commented 2026-11-08 01:31:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T01:31:07Z.

Review: COMMENTED


Ran 46d30d5049 merged on main 302864d48c on Windows 11 x64 (WinLibs GCC 15.2), on an NVIDIA laptop GPU (Turing, driver 610) and an Intel iGPU (driver 101.70xx), with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (Khronos validation layer 1.4.368), ctest -R '^(agc_driver_same_queue_label_wait|agc_completion_label_wait)' -V. 16cf4d9070 (the Accessible guard and the ENVIRONMENT.md row) landed after these runs; I'll add a run of it.

main main + the PR's SameQueueLabelWait.cpp only change
NVIDIA 3/3 1/3 3/3, twice
Intel 3/3 1/3 3/3, twice
  • Without the WaitStatistics.cpp change, the PR's version of agc_driver_same_queue_label_wait and its _draw_pipeline variant fail on both GPUs with the failure the PR describes: "WAIT_REG_MEMs on a CPU flag an earlier wait already saw with the same value, unwritten since, started new collect epochs: 16 extra bumps for 16 waits" (17 bumps with 16 waits, against 1 without). With the change they pass, and the rewritten-flag cases still bump once per wait.
  • The layer was inserted in every test, no log has was not found but was requested by env var VK_INSTANCE_LAYERS, and no log on either side has a VUID-. To be sure validation messages reach these logs, the same layer with best practices on printed 28 BestPractices- warnings into an agc_driver_recorder log on each GPU.

No finding in the code. ordered is per queue worker thread, a changed value or any write to the range since the stored generation still bumps, and with APS5_NO_ORDERED_WAITS set generation stays 0, so every wait bumps as on main.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5815951185 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16897#pullrequestreview-5815951185). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T01:31:07Z. **Review:** COMMENTED --- Ran `46d30d5049` merged on `main` 302864d48c on Windows 11 x64 (WinLibs GCC 15.2), on an NVIDIA laptop GPU (Turing, driver 610) and an Intel iGPU (driver 101.70xx), with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (Khronos validation layer 1.4.368), `ctest -R '^(agc_driver_same_queue_label_wait|agc_completion_label_wait)' -V`. 16cf4d9070 (the `Accessible` guard and the `ENVIRONMENT.md` row) landed after these runs; I'll add a run of it. | | `main` | `main` + the PR's `SameQueueLabelWait.cpp` only | change | |---|---|---|---| | NVIDIA | 3/3 | 1/3 | 3/3, twice | | Intel | 3/3 | 1/3 | 3/3, twice | - Without the `WaitStatistics.cpp` change, the PR's version of `agc_driver_same_queue_label_wait` and its `_draw_pipeline` variant fail on both GPUs with the failure the PR describes: "WAIT_REG_MEMs on a CPU flag an earlier wait already saw with the same value, unwritten since, started new collect epochs: 16 extra bumps for 16 waits" (17 bumps with 16 waits, against 1 without). With the change they pass, and the rewritten-flag cases still bump once per wait. - The layer was inserted in every test, no log has `was not found but was requested by env var VK_INSTANCE_LAYERS`, and no log on either side has a `VUID-`. To be sure validation messages reach these logs, the same layer with best practices on printed 28 `BestPractices-` warnings into an `agc_driver_recorder` log on each GPU. No finding in the code. `ordered` is per queue worker thread, a changed value or any write to the range since the stored generation still bumps, and with `APS5_NO_ORDERED_WAITS` set `generation` stays 0, so every wait bumps as on `main`. AI-assisted: yes.
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!16897
No description provided.