perf(agc): answer guard range checks from a per-thread span copy before taking the guard locks #16116

Merged
Nesyth merged 4 commits from upstream-pr/16116 into main 2026-11-10 22:31:42 +00:00
Nesyth commented 2026-11-05 17:43:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nesyth on 2026-11-05T17:43:09Z.


What

Every guest buffer of every draw, and every index buffer, vertex buffer, texture and storage image read, asks two guard lists whether it touches memory whose samples live only on the host: RequireNoResidentDepthRead_nid_no_patch (native multisample depth planes) and RequireNoMultisampleColorTarget (multisampled colour targets and their FMASK, DCC and CMASK). Once a title has made either kind of target, each check takes the list's std::shared_mutex and walks the list, although almost every check overlaps nothing. With MinGW the shared mutex is a winpthreads read-write lock. In PPSA04264 the two functions took 9.0 % of the draw committer's busy samples, 7.5 % of them inside the lock.

This PR keeps a per-thread copy of each list's ranges, tagged with the list's generation (GuardSpans in Graphics/include/GuardSpans.hpp). The copy is sorted by begin and stores, for each begin, the largest end so far. A check first loads the list's generation. If the copy is current and a binary search shows that no range overlaps, the check returns without the lock. Any overlap goes the old way, and so does a check whose copy is behind (the copy is refilled under the shared lock first).

  • Each list has a generation for the copies, which only the copies read. The depth list's moves in publishDepthGuards when a range is added or removed. The multisample list's (moveGuardSpans) moves wherever the existing guardedGeneration moves, and also when GuardOneFragmentFmask adds a range and when RequireNoMultisampleColorTarget retires ranges. guardedGeneration, which keys the BDA tables, moves exactly as before.
  • Every addition moves the generation before the lock is released, so a current copy holds every range that a check under the lock could see. A removal only lets the copy drop the range sooner.
  • The copy is used in RequireNoResidentDepthRead_nid_no_patch, RoutesToNativeDepthOwner, RequireNoMultisampleColorTarget, ClaimMultisampleTargetMemory and MultisampleColorTargetsOver. Each of them does nothing when no range overlaps.
  • APS5_NO_GUARD_SPANS=1 turns it off. It is a runtime switch (RuntimeSwitch::NoGuardSpans), so APS5_AB can toggle it.
  • APS5_VERIFY_GUARD_SPANS=1 checks every lock-free answer against the list under the lock and takes the list's answer. A copy can miss an overlapping guard in two ways: it was current, or it was refilled from the changed list and still misses the guard. For the first twenty such misses a [guard-spans] verify line is printed. A [guard-spans] line every 10 s counts the lock-free answers, refills, overlaps, checks and misses. It also prints with APS5_PROFILE_DRAW; the counts are kept per thread, so the line adds no shared atomic to each check.

Both variables have rows in docs/dev/ENVIRONMENT.md. docs/dev/TRACING.md now says that APS5_AB takes any runtime switch, not only APS5_NO_BARRIER_BATCH. agc_guard_spans tests the copy against the list check: edges, nested and empty guards, and 400,000 random queries.

Tested

Windows 11 Pro 26300, Radeon RX 7900 XTX (AMD Software 26.10.1), Ryzen 7 7800X3D, MinGW-w64 GCC 15.2, Ninja, Release, BUILD_TESTING=ON, this head rebased on current main 4741457fed.

  • Full build on main 4741457fed (ninja -C build): clean.
  • Focused tests: agc_guard_spans, agc_runtime_switches, the tests for multisampled colour targets, native multisample depth, HTILE, FMASK, EQAA and alpha to coverage, and the agc_driver_recorder, agc_driver_graphics, _draw_pipeline and agc_driver_draw_cache_reuse registrations: 84 tests, ANYPS5_REQUIRE_VULKAN=1, -j4: 100% pass, 0 failed (one, agc_driver_native_depth_driver_queued_fill, skips itself). The four agc_driver_native_depth_aliased_depth*/_publication tests that failed on the old base now pass on 4741457fed.
  • In game, PPSA04264, Performance graphics mode, open-world gameplay, this branch alone on main 4741457fed, APS5_AB=30:APS5_NO_GUARD_SPANS, 30 s phases from 190 s, 20 phases, each B phase against its neighbouring A phases. APS5_DRAW_PIPELINE=1 is set in both arms, because its [draw] report gives the commits per present. The guard checks run on whichever thread prepares a draw, with or without the pipeline. Draws per present swing with the scene, so frame time is fitted against commits per present with a common slope (residual SD 3.3 ms, n 20).
copy (A) no copy (B) B against neighbouring A
committer per commit 23.68 µs 25.89 µs +2.21 ± 0.23 µs (n 10)
producers' wait for room per draw 5.38 µs 6.24 µs +0.86 ± 0.13 µs (n 10)
frame time at equal commits per present 85.0 ms 91.8 ms +6.80 ± 1.50 ms (+8.0 ± 1.8 %)
presents per 10 s 114.6 112.8 -2.5 ± 3.9, inside the scene's swing

No errors in the run.

Performance report

Captures of PPSA04264 on main 4741457fed and on this head, same scene, machine and settings (APS5_DRAW_PIPELINE=1, trace window 240-300 s). The two traces, both digests and a second pair of boots are in the report comment.

{"commit":"4741457fed42","dirty":false,"env":{"APS5_DRAW_PIPELINE":"1","APS5_LOG_TIMESTAMPS":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"240:60","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"AMD Radeon RX 7900 XTX","driver":"AMD proprietary driver 26.10.1 (LLPC)","os":"Windows 10.0.26300","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31860,"title_id":"PPSA04264","title":"Grand Theft Auto V","content_version":"01.011.001","resolution":"3840x2160","window":{"spec":"seconds 240:60","first_present":3623,"ms":60028.203},"presents":436,"frame_ms":{"p50":136.663,"p90":143.590,"p99":177.093,"max":250.592},"gpu_busy_ms_per_present":34.217,"worker_busy_pct":{"queue worker 0x0":95.244},"top_phases_ms_per_frame":{"Driver.Packet":124.285,"Graphics.Draw":96.804,"Driver.Packet.draw":90.558,"Driver.Draw":89.662,"Graphics.Draw.draw_and_resource_release":84.355,"Driver.DrawFinish":76.874,"Driver.Draw.prepare":68.527,"Driver.DrawFinish.shader_compile_and_link":47.988},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":5.458,"events_lost":2605270,"totals_full":false},"draw committer":{"events_full_after_s":12.260,"events_lost":1036681,"totals_full":false},"draw finisher":{"events_full_after_s":11.992,"events_lost":1070545,"totals_full":false}},"trace":"aps5-trace-4741457fed42-55484.json.gz"}
{"commit":"6e1413677c69","dirty":false,"env":{"APS5_DRAW_PIPELINE":"1","APS5_LOG_TIMESTAMPS":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"240:60","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"AMD Radeon RX 7900 XTX","driver":"AMD proprietary driver 26.10.1 (LLPC)","os":"Windows 10.0.26300","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31860,"title_id":"PPSA04264","title":"Grand Theft Auto V","content_version":"01.011.001","resolution":"3840x2160","window":{"spec":"seconds 240:60","first_present":4026,"ms":60097.374},"presents":502,"frame_ms":{"p50":118.603,"p90":125.776,"p99":142.113,"max":182.951},"gpu_busy_ms_per_present":34.723,"worker_busy_pct":{"queue worker 0x0":95.056},"top_phases_ms_per_frame":{"Driver.Packet":108.501,"Graphics.Draw":82.479,"Driver.Packet.draw":76.556,"Driver.Draw":75.754,"Graphics.Draw.draw_and_resource_release":71.807,"Driver.DrawFinish":65.978,"Driver.Draw.prepare":58.101,"Driver.DrawFinish.shader_compile_and_link":41.781},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":5.782,"events_lost":2420083,"totals_full":false},"draw committer":{"events_full_after_s":13.486,"events_lost":919149,"totals_full":false},"draw finisher":{"events_full_after_s":13.400,"events_lost":898022,"totals_full":false}},"trace":"aps5-trace-6e1413677c69-37292.json.gz"}

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: AMD Radeon RX 7900 XTX, AMD Software 26.10.1, Windows 11 Pro 26300
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5176022925 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16116). Originally posted by [Nesyth](https://github.com/Nesyth) on 2026-11-05T17:43:09Z. --- ### What Every guest buffer of every draw, and every index buffer, vertex buffer, texture and storage image read, asks two guard lists whether it touches memory whose samples live only on the host: `RequireNoResidentDepthRead_nid_no_patch` (native multisample depth planes) and `RequireNoMultisampleColorTarget` (multisampled colour targets and their FMASK, DCC and CMASK). Once a title has made either kind of target, each check takes the list's `std::shared_mutex` and walks the list, although almost every check overlaps nothing. With MinGW the shared mutex is a winpthreads read-write lock. In PPSA04264 the two functions took 9.0 % of the draw committer's busy samples, 7.5 % of them inside the lock. This PR keeps a per-thread copy of each list's ranges, tagged with the list's generation (`GuardSpans` in `Graphics/include/GuardSpans.hpp`). The copy is sorted by begin and stores, for each begin, the largest end so far. A check first loads the list's generation. If the copy is current and a binary search shows that no range overlaps, the check returns without the lock. Any overlap goes the old way, and so does a check whose copy is behind (the copy is refilled under the shared lock first). - Each list has a generation for the copies, which only the copies read. The depth list's moves in `publishDepthGuards` when a range is added or removed. The multisample list's (`moveGuardSpans`) moves wherever the existing `guardedGeneration` moves, and also when `GuardOneFragmentFmask` adds a range and when `RequireNoMultisampleColorTarget` retires ranges. `guardedGeneration`, which keys the BDA tables, moves exactly as before. - Every addition moves the generation before the lock is released, so a current copy holds every range that a check under the lock could see. A removal only lets the copy drop the range sooner. - The copy is used in `RequireNoResidentDepthRead_nid_no_patch`, `RoutesToNativeDepthOwner`, `RequireNoMultisampleColorTarget`, `ClaimMultisampleTargetMemory` and `MultisampleColorTargetsOver`. Each of them does nothing when no range overlaps. - `APS5_NO_GUARD_SPANS=1` turns it off. It is a runtime switch (`RuntimeSwitch::NoGuardSpans`), so `APS5_AB` can toggle it. - `APS5_VERIFY_GUARD_SPANS=1` checks every lock-free answer against the list under the lock and takes the list's answer. A copy can miss an overlapping guard in two ways: it was current, or it was refilled from the changed list and still misses the guard. For the first twenty such misses a `[guard-spans] verify` line is printed. A `[guard-spans]` line every 10 s counts the lock-free answers, refills, overlaps, checks and misses. It also prints with `APS5_PROFILE_DRAW`; the counts are kept per thread, so the line adds no shared atomic to each check. Both variables have rows in `docs/dev/ENVIRONMENT.md`. `docs/dev/TRACING.md` now says that `APS5_AB` takes any runtime switch, not only `APS5_NO_BARRIER_BATCH`. `agc_guard_spans` tests the copy against the list check: edges, nested and empty guards, and 400,000 random queries. ### Tested Windows 11 Pro 26300, Radeon RX 7900 XTX (AMD Software 26.10.1), Ryzen 7 7800X3D, MinGW-w64 GCC 15.2, Ninja, Release, `BUILD_TESTING=ON`, this head rebased on current `main` `4741457fed`. - Full build on `main` `4741457fed` (`ninja -C build`): clean. - Focused tests: `agc_guard_spans`, `agc_runtime_switches`, the tests for multisampled colour targets, native multisample depth, HTILE, FMASK, EQAA and alpha to coverage, and the `agc_driver_recorder`, `agc_driver_graphics`, `_draw_pipeline` and `agc_driver_draw_cache_reuse` registrations: 84 tests, `ANYPS5_REQUIRE_VULKAN=1`, `-j4`: 100% pass, 0 failed (one, `agc_driver_native_depth_driver_queued_fill`, skips itself). The four `agc_driver_native_depth_aliased_depth*`/`_publication` tests that failed on the old base now pass on `4741457fed`. - **In game, PPSA04264**, Performance graphics mode, open-world gameplay, this branch alone on `main` `4741457fed`, `APS5_AB=30:APS5_NO_GUARD_SPANS`, 30 s phases from 190 s, 20 phases, each B phase against its neighbouring A phases. `APS5_DRAW_PIPELINE=1` is set in both arms, because its `[draw]` report gives the commits per present. The guard checks run on whichever thread prepares a draw, with or without the pipeline. Draws per present swing with the scene, so frame time is fitted against commits per present with a common slope (residual SD 3.3 ms, n 20). | | copy (A) | no copy (B) | B against neighbouring A | |---|---|---|---| | committer per commit | 23.68 µs | 25.89 µs | +2.21 ± 0.23 µs (n 10) | | producers' wait for room per draw | 5.38 µs | 6.24 µs | +0.86 ± 0.13 µs (n 10) | | frame time at equal commits per present | 85.0 ms | 91.8 ms | +6.80 ± 1.50 ms (+8.0 ± 1.8 %) | | presents per 10 s | 114.6 | 112.8 | -2.5 ± 3.9, inside the scene's swing | No errors in the run. ### Performance report Captures of PPSA04264 on `main` `4741457fed` and on this head, same scene, machine and settings (`APS5_DRAW_PIPELINE=1`, trace window 240-300 s). The two traces, both digests and a second pair of boots are in [the report comment](https://github.com/boykopovar/AnyPS5/pull/16116#issuecomment-6682576565). ```json {"commit":"4741457fed42","dirty":false,"env":{"APS5_DRAW_PIPELINE":"1","APS5_LOG_TIMESTAMPS":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"240:60","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"AMD Radeon RX 7900 XTX","driver":"AMD proprietary driver 26.10.1 (LLPC)","os":"Windows 10.0.26300","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31860,"title_id":"PPSA04264","title":"Grand Theft Auto V","content_version":"01.011.001","resolution":"3840x2160","window":{"spec":"seconds 240:60","first_present":3623,"ms":60028.203},"presents":436,"frame_ms":{"p50":136.663,"p90":143.590,"p99":177.093,"max":250.592},"gpu_busy_ms_per_present":34.217,"worker_busy_pct":{"queue worker 0x0":95.244},"top_phases_ms_per_frame":{"Driver.Packet":124.285,"Graphics.Draw":96.804,"Driver.Packet.draw":90.558,"Driver.Draw":89.662,"Graphics.Draw.draw_and_resource_release":84.355,"Driver.DrawFinish":76.874,"Driver.Draw.prepare":68.527,"Driver.DrawFinish.shader_compile_and_link":47.988},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":5.458,"events_lost":2605270,"totals_full":false},"draw committer":{"events_full_after_s":12.260,"events_lost":1036681,"totals_full":false},"draw finisher":{"events_full_after_s":11.992,"events_lost":1070545,"totals_full":false}},"trace":"aps5-trace-4741457fed42-55484.json.gz"} ``` ```json {"commit":"6e1413677c69","dirty":false,"env":{"APS5_DRAW_PIPELINE":"1","APS5_LOG_TIMESTAMPS":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"240:60","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"AMD Radeon RX 7900 XTX","driver":"AMD proprietary driver 26.10.1 (LLPC)","os":"Windows 10.0.26300","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31860,"title_id":"PPSA04264","title":"Grand Theft Auto V","content_version":"01.011.001","resolution":"3840x2160","window":{"spec":"seconds 240:60","first_present":4026,"ms":60097.374},"presents":502,"frame_ms":{"p50":118.603,"p90":125.776,"p99":142.113,"max":182.951},"gpu_busy_ms_per_present":34.723,"worker_busy_pct":{"queue worker 0x0":95.056},"top_phases_ms_per_frame":{"Driver.Packet":108.501,"Graphics.Draw":82.479,"Driver.Packet.draw":76.556,"Driver.Draw":75.754,"Graphics.Draw.draw_and_resource_release":71.807,"Driver.DrawFinish":65.978,"Driver.Draw.prepare":58.101,"Driver.DrawFinish.shader_compile_and_link":41.781},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":5.782,"events_lost":2420083,"totals_full":false},"draw committer":{"events_full_after_s":13.486,"events_lost":919149,"totals_full":false},"draw finisher":{"events_full_after_s":13.400,"events_lost":898022,"totals_full":false}},"trace":"aps5-trace-6e1413677c69-37292.json.gz"} ``` ### 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 - [x] GPUs tested: AMD Radeon RX 7900 XTX, AMD Software 26.10.1, Windows 11 Pro 26300 - [x] 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!16116
No description provided.