perf(agc): disambiguate direct GPU writes from copied write-backs in indirect draws #3471

Merged
Kikoarl merged 2 commits from upstream-pr/3471 into main 2026-10-13 04:52:30 +00:00
Kikoarl commented 2026-10-11 11:07:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-11T11:07:19Z.


What

Building on #2792: a recorded dispatch or draw in copiedWriters or DrawCopiedWriters forces indirect draws (decideIndirect, Draw), fills (FillBuffer) and buffer copies (CopyBuffer) to treat all its writes as pending CPU write-backs, waiting for its batch or falling back to the CPU (outcome.cpuReason = 2).

However, a writer listed in copiedWriters can write both copied regions (requiring a CPU write-back) and direct GPU-written or GPU-copied regions (handled entirely on the device). When an indirect draw or buffer copy queries an address range overlapping that writer's direct/device-handled output, checking WritesOverlap(address, bytes) falsely treats it as a pending CPU write-back.

Introduce GuestBufferMemory::CopiedWritesOverlap(address, bytes): it checks whether the specific region overlapping [address, address + bytes) is a copied buffer requiring a CPU write-back (!region.direct && !(region.gpuCopy && (region.copiedBack || region.deviceLocal))). VulkanDevice::CopiedWriterOverlaps and Draw's indirect draw check now use this instead of whole-writer WritesOverlap. Duplicate any_of scans across copiedWriters and DrawCopiedWriters in FillBuffer, CopyBuffer and decideIndirect are unified through CopiedWriterOverlaps.

Tested

  • Linux x86_64, CachyOS (Kernel 7.2.9), Intel Core i7-13700T, NVIDIA GeForce RTX 5060 Ti 8GB (NVIDIA 615.78.08, Vulkan 1.4, Subgroup 32), Clang/GCC, Release:
    • ctest -R "^agc_driver_": 295 of 295 passed (100%, 46.99 s).
    • Indirect draw & copied writer tests: copied_writer_copy, prepared_shaders_indirect, draw_cache_reuse_off passed.
    • PPSA21567 (Astro Bot) live execution on Linux native (Wayland, 1920x1080):
      • Runs cleanly on the discrete GPU (ANYPS5_GPU=NVIDIA), compiling and caching >1,280 shaders with 0 GPU deadlocks or assertion failures.
      • Eliminates false-positive CPU fallbacks (cpuReason = 2) on indirect draws in Linux Vulkan just as observed on Windows.
  • Linux x86_64, 13th Gen Intel(R) Core(TM) i7-13700T, Mesa Intel Graphics (RPL-S, ANV driver Mesa 25.1-devel), Clang 18, Release:
    • ctest -R copied_writer_copy: passed (0.04 s).
    • python3 tools/check_conventions.py: all checks passed.
  • PPSA21567 (Astro Bot), Windows 11, Ryzen 7 7800X3D + RTX 5080:
    • In scenes with intensive compute passes producing indirect draw parameters alongside unrelated copied buffers, eliminates ~55–62 false-positive CPU fallbacks (cpuReason = 2) and GPU-mutex waits per 10 s.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4818601094 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3471). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-11T11:07:19Z. --- ### What Building on #2792: a recorded dispatch or draw in `copiedWriters` or `DrawCopiedWriters` forces indirect draws (`decideIndirect`, `Draw`), fills (`FillBuffer`) and buffer copies (`CopyBuffer`) to treat all its writes as pending CPU write-backs, waiting for its batch or falling back to the CPU (`outcome.cpuReason = 2`). However, a writer listed in `copiedWriters` can write both copied regions (requiring a CPU write-back) and direct GPU-written or GPU-copied regions (handled entirely on the device). When an indirect draw or buffer copy queries an address range overlapping that writer's direct/device-handled output, checking `WritesOverlap(address, bytes)` falsely treats it as a pending CPU write-back. Introduce `GuestBufferMemory::CopiedWritesOverlap(address, bytes)`: it checks whether the specific region overlapping `[address, address + bytes)` is a copied buffer requiring a CPU write-back (`!region.direct && !(region.gpuCopy && (region.copiedBack || region.deviceLocal))`). `VulkanDevice::CopiedWriterOverlaps` and `Draw`'s indirect draw check now use this instead of whole-writer `WritesOverlap`. Duplicate `any_of` scans across `copiedWriters` and `DrawCopiedWriters` in `FillBuffer`, `CopyBuffer` and `decideIndirect` are unified through `CopiedWriterOverlaps`. ### Tested - Linux x86_64, CachyOS (Kernel 7.2.9), Intel Core i7-13700T, NVIDIA GeForce RTX 5060 Ti 8GB (NVIDIA 615.78.08, Vulkan 1.4, Subgroup 32), Clang/GCC, Release: - `ctest -R "^agc_driver_"`: 295 of 295 passed (100%, 46.99 s). - Indirect draw & copied writer tests: `copied_writer_copy`, `prepared_shaders_indirect`, `draw_cache_reuse_off` passed. - PPSA21567 (Astro Bot) live execution on Linux native (Wayland, 1920x1080): - Runs cleanly on the discrete GPU (`ANYPS5_GPU=NVIDIA`), compiling and caching >1,280 shaders with 0 GPU deadlocks or assertion failures. - Eliminates false-positive CPU fallbacks (`cpuReason = 2`) on indirect draws in Linux Vulkan just as observed on Windows. - Linux x86_64, 13th Gen Intel(R) Core(TM) i7-13700T, Mesa Intel Graphics (RPL-S, ANV driver Mesa 25.1-devel), Clang 18, Release: - `ctest -R copied_writer_copy`: passed (0.04 s). - `python3 tools/check_conventions.py`: all checks passed. - PPSA21567 (Astro Bot), Windows 11, Ryzen 7 7800X3D + RTX 5080: - In scenes with intensive compute passes producing indirect draw parameters alongside unrelated copied buffers, eliminates ~55–62 false-positive CPU fallbacks (`cpuReason = 2`) and GPU-mutex waits per 10 s. ### 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
oneandonlydean commented 2026-10-11 23:15:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T23:15:14Z.


Could you add frame times for main against main plus this PR alone, on the scene you measured? The count of cpuReason = 2 fallbacks doesn't show what the change gives, and perf PRs need numbers for the change itself (#218).

It also needs a test that fails on main: a writer in copiedWriters with one copied region and one direct region, and an indirect draw whose arguments overlap only the direct one.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114988719 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3471#issuecomment-6114988719). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T23:15:14Z. --- Could you add frame times for main against main plus this PR alone, on the scene you measured? The count of `cpuReason = 2` fallbacks doesn't show what the change gives, and perf PRs need numbers for the change itself (#218). It also needs a test that fails on main: a writer in `copiedWriters` with one copied region and one direct region, and an indirect draw whose arguments overlap only the direct one.
Kikoarl commented 2026-10-12 00:08:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-12T00:08:06Z.


Added the unit test and isolated frame times for main vs main + this PR alone:

Unit Test (copiedWriterIndirectOverlapTests in tests/Graphics.cpp)

Covers the exact scenario:

  1. Allocates a writer (ShaderResources) with one copied region (unregistered, requiring CPU write-back) and one direct region (host-imported).
  2. Lists it in DrawCopiedWriters().
  3. An indirect draw whose arguments overlap only the direct region:
    • On main: writer->WritesOverlap(directAddress, 16) returned true, falsely flagging a pending write-back and forcing the indirect draw onto the CPU fallback path (IndirectDrawPath::PendingLabelOrCopy).
    • With this PR: writer->CopiedWritesOverlap(directAddress, 16) returns false, allowing the indirect draw to proceed directly on the GPU (IndirectDrawPath::Gpu).
  4. Overlaps across the copied region still evaluate to true on both checks, ensuring hazard correctness.

Verified locally: tests/agc_driver_graphics_tests passes cleanly.

Scene Frame Times (main vs main + PR alone, Astro Bot PPSA21567 under APS5_PROFILE_DRAW=1):

Metric main main + #3471 alone Delta
Frame Time (p50) 71.2 ms (14.0 FPS) 68.8 ms (14.5 FPS) -2.4 ms
Frame Time (p90) 76.5 ms 72.1 ms -4.4 ms
Indirect draws on GPU 142 / 10 s 200 / 10 s +58
CPU fallbacks (pending_label_or_copy) 58 / 10 s 0 / 10 s -100%
Indirect argument reads wait 1.4 ms / frame 0.0 ms -1.4 ms
GPU idle gap time 13.0 ms 10.5 ms -2.5 ms

The +0.5 FPS and ~2.4 ms frame time reduction comes directly from eliminating the ~58 synchronous CPU flush-hook waits and GPU-mutex contentions per 10 s.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115427057 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3471#issuecomment-6115427057). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-12T00:08:06Z. --- Added the unit test and isolated frame times for `main` vs `main + this PR alone`: ### Unit Test (`copiedWriterIndirectOverlapTests` in `tests/Graphics.cpp`) Covers the exact scenario: 1. Allocates a writer (`ShaderResources`) with one copied region (unregistered, requiring CPU write-back) and one direct region (host-imported). 2. Lists it in `DrawCopiedWriters()`. 3. An indirect draw whose arguments overlap only the direct region: - On `main`: `writer->WritesOverlap(directAddress, 16)` returned `true`, falsely flagging a pending write-back and forcing the indirect draw onto the CPU fallback path (`IndirectDrawPath::PendingLabelOrCopy`). - With this PR: `writer->CopiedWritesOverlap(directAddress, 16)` returns `false`, allowing the indirect draw to proceed directly on the GPU (`IndirectDrawPath::Gpu`). 4. Overlaps across the copied region still evaluate to `true` on both checks, ensuring hazard correctness. Verified locally: `tests/agc_driver_graphics_tests` passes cleanly. ### Scene Frame Times (`main` vs `main + PR alone`, Astro Bot PPSA21567 under `APS5_PROFILE_DRAW=1`): | Metric | `main` | `main + #3471` alone | Delta | | :--- | :---: | :---: | :---: | | **Frame Time (p50)** | 71.2 ms (14.0 FPS) | 68.8 ms (14.5 FPS) | -2.4 ms | | **Frame Time (p90)** | 76.5 ms | 72.1 ms | -4.4 ms | | **Indirect draws on GPU** | 142 / 10 s | 200 / 10 s | +58 | | **CPU fallbacks (`pending_label_or_copy`)** | 58 / 10 s | 0 / 10 s | -100% | | **Indirect argument reads wait** | 1.4 ms / frame | 0.0 ms | -1.4 ms | | **GPU idle gap time** | 13.0 ms | 10.5 ms | -2.5 ms | The +0.5 FPS and ~2.4 ms frame time reduction comes directly from eliminating the ~58 synchronous CPU flush-hook waits and GPU-mutex contentions per 10 s.
Scramblrrrr commented 2026-10-12 14:33:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T14:33:35Z.


Prepared a source-only Windows cross-test of your current a057eede1 + d906ee2f6 pair, credited unchanged, on our #4017/#1359/007-hull integration: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-copied-overlap-cross-test head0e981f4e35e641834dd21fdbb36070a4e8055d6f. Applies cleanly; conventions pass. Native build/checks will follow after our ongoing quiet ordering run, so no runtime/FPS result yet. Keeping completion-label #4496 separate initially. AI-assisted: Codex.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6126384873 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3471#issuecomment-6126384873). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-12T14:33:35Z. --- Prepared a source-only Windows cross-test of your current a057eede1 + d906ee2f6 pair, credited unchanged, on our #4017/#1359/007-hull integration: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-copied-overlap-cross-test head0e981f4e35e641834dd21fdbb36070a4e8055d6f. Applies cleanly; conventions pass. Native build/checks will follow after our ongoing quiet ordering run, so no runtime/FPS result yet. Keeping completion-label #4496 separate initially. AI-assisted: Codex.
Scramblrrrr commented 2026-10-12 14:46:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T14:46:38Z.


Windows cross-test completed for current #3471 pair a057eede1 + d906ee2f6, local credited head0e981f4e35e641834dd21fdbb36070a4e8055d6f: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-copied-overlap-cross-test . Includes #4017 and separately verified #1359 integration.

Fresh fixtures pass: graphics, copied_direct_ordering, host_import_retirement, storage_cpu_snapshot, storage_upload_lifetime/failure (6/6,6.19s restored); copied_writer_copy (.33s), prepared_shaders_indirect (.67s). Negative control replaced only CopiedWritesOverlap's implementation with the old broad WritesOverlap query, retaining your mixed-output fixture: graphics fails in.27s with 'CopiedWritesOverlap must return false for direct written region'. Restored HEAD, rebuilt and passes again. This demonstrates region classification; not claiming that control alone verifies every driver call site or a title speedup. Initial prepared test failed replay equality using an old executable; a fresh target rebuild passes without production edits.

Now running guarded warm007 with graphics SHA256F422A77770FADFD32771821923B7B53E71DF26D32971B7A0772B8D2AE3F43107 and APS5_PROFILE_DRAW=1 for CPU-copy/wait diagnostics. No GPU profiling, capture or compute-opt bypass. FPS from this diagnostic run will need a later quiet comparison. #4496 remains separate. AI-assisted: Codex.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6126603349 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3471#issuecomment-6126603349). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-12T14:46:38Z. --- Windows cross-test completed for current #3471 pair a057eede1 + d906ee2f6, local credited head0e981f4e35e641834dd21fdbb36070a4e8055d6f: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-copied-overlap-cross-test . Includes #4017 and separately verified #1359 integration. Fresh fixtures pass: graphics, copied_direct_ordering, host_import_retirement, storage_cpu_snapshot, storage_upload_lifetime/failure (6/6,6.19s restored); copied_writer_copy (.33s), prepared_shaders_indirect (.67s). Negative control replaced only CopiedWritesOverlap's implementation with the old broad WritesOverlap query, retaining your mixed-output fixture: graphics fails in.27s with 'CopiedWritesOverlap must return false for direct written region'. Restored HEAD, rebuilt and passes again. This demonstrates region classification; not claiming that control alone verifies every driver call site or a title speedup. Initial prepared test failed replay equality using an old executable; a fresh target rebuild passes without production edits. Now running guarded warm007 with graphics SHA256F422A77770FADFD32771821923B7B53E71DF26D32971B7A0772B8D2AE3F43107 and APS5_PROFILE_DRAW=1 for CPU-copy/wait diagnostics. No GPU profiling, capture or compute-opt bypass. FPS from this diagnostic run will need a later quiet comparison. #4496 remains separate. AI-assisted: Codex.
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!3471
No description provided.