Skip to content

perf(libSceAgcDriver): bypass the draw cache while its hit rate is under 5% - #5404

Closed
bcortezf wants to merge 1 commit into
boykopovar:mainfrom
bcortezf:perf/draw-cache-adaptive-bypass-main
Closed

bcortezf wants to merge 1 commit into
boykopovar:mainfrom
bcortezf:perf/draw-cache-adaptive-bypass-main

Conversation

@bcortezf

@bcortezf bcortezf commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

What

The draw cache (APS5_DRAW_CACHE_ENTRIES, 4096 entries) misses on almost every draw in scenes whose user-data words change every frame: in Red Dead Redemption gameplay it hits 0 to 6% of 120k to 130k lookups per 10 s and spends the time inserting and evicting entries (131k inserts and 300k to 730k evictions per 10 s).

Count the lookups in windows of 2,048. When fewer than 5% of a window hit, draws skip the cache (no register key, no lookup, no insert, the same path as APS5_NO_DRAW_SRT_ENTRIES) for the next 16,384 draws, then the cache is probed again. Scenes that hit (menus and cutscenes, 98 to 100%) are not bypassed. APS5_NO_DRAW_CACHE_BYPASS=1 restores the previous behaviour, and [draw-cache] bypassed N draws is printed with APS5_PROFILE_DRAW.

Related: #5345 (report), #5364 (miss breakdown; independent, may need a rebase in Draw.cpp).

Tested

Windows 11, RTX 4070 SUPER (driver 616.64), Ryzen 7 7800X3D, 3840x2160. PPSA30528, Armadillo after the intro, APS5_TRACE_SECONDS=100:5, caches warm, same session. The base is APS5_NO_DRAW_CACHE_BYPASS=1 on the same binary.

frame p50 FPS
bypass off (main behaviour) 392 ms 2.55
bypass on 362 ms 2.76

Two earlier rounds on the same machine: 2.63 / 2.61 FPS off, 2.80 / 2.83 FPS on. The queue worker stays at 98.8% busy; the gain is per-draw cost, not a different bottleneck. trace_dropped is true in these captures (the worker buffer fills within 5 s).

Summary lines (off, then on):

{"commit": "981acf21ed70", "dirty": true, "env": {"APS5_NO_DRAW_CACHE_BYPASS": "1", "APS5_PAD_INPUT_FILE": "<path>", "APS5_TRACE_SECONDS": "100:5"}, "gpu": "NVIDIA GeForce RTX 4070 SUPER", "driver": "NVIDIA 616.64", "os": "Windows 10.0.26200", "resolution": "3840x2160", "window": {"spec": "seconds 100:5", "first_present": 1791, "ms": 5097.913}, "presents": 13, "frame_ms": {"p50": 392.154, "p90": 406.612, "p99": 413.368, "max": 413.368}, "gpu_busy_ms_per_present": 133.99, "worker_busy_pct": {"queue worker 0x0": 98.886}, "top_phases_ms_per_frame": {"Driver.Packet": 387.509, "Driver.Packet.draw": 360.408, "Driver.Draw": 358.815, "Driver.Draw.draw_and_resource_release": 181.247, "Graphics.Draw": 179.59, "Graphics.Draw.draw_and_resource_release": 154.663, "Driver.Draw.shader_compile_and_link": 126.894, "Shader.DrawStage": 95.646}, "trace_dropped": true, "trace": "aps5-trace-981acf21ed70-20260.json.gz"}
{"commit": "981acf21ed70", "dirty": true, "env": {"APS5_PAD_INPUT_FILE": "<path>", "APS5_TRACE_SECONDS": "100:5"}, "gpu": "NVIDIA GeForce RTX 4070 SUPER", "driver": "NVIDIA 616.64", "os": "Windows 10.0.26200", "resolution": "3840x2160", "window": {"spec": "seconds 100:5", "first_present": 1673, "ms": 5053.279}, "presents": 14, "frame_ms": {"p50": 362.483, "p90": 371.102, "p99": 373.611, "max": 373.611}, "gpu_busy_ms_per_present": 145.365, "worker_busy_pct": {"queue worker 0x0": 98.813}, "top_phases_ms_per_frame": {"Driver.Packet": 356.413, "Driver.Packet.draw": 329.475, "Driver.Draw": 327.91, "Driver.Draw.draw_and_resource_release": 174.267, "Graphics.Draw": 172.722, "Graphics.Draw.draw_and_resource_release": 148.854, "Driver.Draw.shader_compile_and_link": 106.221, "Shader.DrawStage": 91.167}, "trace_dropped": true, "trace": "aps5-trace-981acf21ed70-6036.json.gz"}

python3 tools/check_conventions.py --base origin/main: passed. Not built or run on Linux or macOS.

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
  • No new third-party code
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

…der 5%

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 label Oct 13, 2026
@github-actions

Copy link
Copy Markdown

Thank you for your contribution! Authors with no pull requests merged into main can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.

@github-actions github-actions Bot closed this Oct 13, 2026
@github-actions github-actions Bot removed the open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 label Oct 17, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 17, 2026
@bcortezf

Copy link
Copy Markdown
Contributor Author

Need me to update this branch? @oneandonlydean

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1896 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 5431 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 5441 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 5609 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCache.cpp
PR 5925 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 6188 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCache.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCacheStatistics.cpp
PR 6292 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 6501 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 6539 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCache.cpp
PR 6612 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 6615 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 6639 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 6801 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 6804 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 6946 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCache.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCacheStatistics.cpp
PR 7201 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 7233 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 7275 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 7307 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 7369 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCache.cpp
PR 7371 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 7446 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCache.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCacheStatistics.cpp
PR 7644 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 7691 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 7930 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 7978 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 8083 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 8339 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 8372 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 8740 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 9129 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCache.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCacheStatistics.cpp
PR 9177 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 9243 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCache.cpp
PR 9248 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCache.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCacheStatistics.cpp
PR 9335 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 9387 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 9396 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
PR 9424 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
PR 9508 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/Draw.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCache.cpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawCacheStatistics.cpp

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed agc libSceAgc* labels Oct 17, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks for the measurements and the opt-out switch. Before this can merge it needs a rebase and a fresh measurement, because the draw cache it works around has changed a lot since this branch was cut (it is about 2,600 commits behind main, and all four files conflict).

Why the current numbers no longer carry over. Since your base, main has added several changes aimed at exactly the low hit rate you measured in RDR:

What the rebase needs to account for. useDrawEntries also gates stage caching. On main it gates stageEntries (Draw.cpp, const bool stageEntries = useDrawEntries && …), and stage-level reuse now hits in cases where the whole-draw key misses. The bypass counts only whole-draw drawHit, so a scene whose whole-draw keys miss but whose stages hit would be measured as under 5% and would lose the stage reuse that is working. Please either measure what the bypass switches off (whole-draw plus stage hits), or bypass only the whole-draw lookup and insert and keep stage entries on.

Minor: the window counters (fetch_add, then exchange and store) can race between queue workers. For a heuristic that's acceptable, but a single CAS on a packed counter, or a note, would make it explicit.

Measurements we need on the rebased head:

  1. RDR (PPSA30528) on current main: the [draw-cache] profile lines (lookups, hits, stage hits), showing the low hit rate still occurs.
  2. FPS with the bypass off (APS5_NO_DRAW_CACHE_BYPASS=1) and on, same binary and scene, several rounds as before.
  3. One or two titles where the cache does pay (menus, cutscenes or a typical gameplay scene): the [draw-cache] bypassed N draws count, ideally 0, and FPS unchanged.

With those, this is a straightforward review.

@github-actions github-actions Bot added perf Performance change perf numbers missing perf pull request without a before and after summary line labels Oct 18, 2026
@bcortezf

Copy link
Copy Markdown
Contributor Author

@oneandonlydean Closing: main has since landed the draw-cache admission and reuse changes listed above, which target the same low hit rate, and on the earlier stack (#494, #1193, #1202) this bypass was already neutral (~-3%, within noise). Thanks for the review.

@bcortezf bcortezf closed this Oct 22, 2026
@bcortezf bcortezf reopened this Oct 22, 2026
@github-actions

Copy link
Copy Markdown

This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

@github-actions github-actions Bot added the open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 label Oct 22, 2026
@github-actions github-actions Bot closed this Oct 22, 2026
@EagleBearerYS

Copy link
Copy Markdown
Contributor

@bcortezf, thank you for this bypass. I've ported it to current main as #12478, a draft for now, with you as co-author of its commit.

On Resident Evil Village (PPSA01556)'s static main menu (RTX 3070 Laptop GPU, GPU clocks not held, the draw-cache byte budget off in both arms, B A A B), it kept private bytes about 3.8 GB lower at menu +15 min (19.3 to 19.5 GB against 23.2 GB), and the menu ran at 0.47 to 0.48 fps against 0.35 to 0.41 without it, over menu +5 to +15 min. The numbers and summary lines are in #12478.

Before it leaves draft, a further commit will count stage hits into the window and pack the window counters into one CAS, as suggested in the review here, and a pair with main's default configuration (budget on) will measure that head.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* needs rebase Conflicts with main, rebase needed open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 perf numbers missing perf pull request without a before and after summary line perf Performance change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants