Skip to content

fix(agc): release evicted depth plane copies and scale their budget - #1248

Open
Thimo188 wants to merge 2 commits into
mainfrom
upstream-pr/25534
Open

Thimo188 wants to merge 2 commits into
mainfrom
upstream-pr/25534

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#25534, originally opened by Mouhand-Kaddo.

Source commit: 198a1f6cc1dd0ae9e00549ea56b045dc491cc555. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

Sampling layered depth surfaces through DepthPlaneCopy leaked gigabytes of VRAM into SharedResourceCache and thrashed on >4 GiB GPUs.

In Demon's Souls (PPSA01342, OUTPOST PASSAGE), the title samples 21 active 16 MiB depth plane copies (336 MiB of images plus 336 MiB of staging = 672 MiB). Because evictPlaneCopies used a hardcoded 256 MiB budget (512 MiB active-frame ceiling), copies evicted continuously within a single frame. On each eviction or replacement, DepthPlaneCopy::release() cleared its textures map without marking externally referenced Texture views as released (NoteReleasedTexture), so SharedResourceCache kept every evicted OwnedImage alive indefinitely. Over two minutes of gameplay on a 16 GiB RX 6900 XT, vkAllocateMemory depth plane copy leaked to 6.76 GiB, forced sampled textures down to their 2,048 MiB floor with heavy eviction churn, stalled for 10+ seconds, and crashed with radv/amdgpu: Not enough memory for command submission (VK_ERROR_DEVICE_LOST).

Changes:

  • Expose NoteReleasedTexture and ResourceCache::DropReleasedTextures in ShaderResources, and check textures[i]->Released() before the surface.depth shortcut in fastRevalidate and fastRevalidateEach.
  • In DepthPlaneCopy::release(), mark any externally referenced Texture (use_count() > 1) released via NoteReleasedTexture(texture, address) (guarded inside noexcept).
  • In DepthSurfaceTexture, unlock surfacesMutex() and sweep SharedResourceCache().DropReleasedTextures(&stale) whenever a copy was replaced or evicted (keeping displaced sets on any active recording Recorder), so copies evicted during a resource-cache hit (ReuseLookupHolds) are released without waiting for a subsequent Insert.
  • Scope evictPlaneCopies accounting and eviction to context.device, and add DepthPlaneCopyBudget (matching BufferPool::DeviceBudget): when APS5_DEPTH_PLANE_COPY_MIB is unset, scale the default budget to max(256 MiB, heapBudget / 16) on discrete GPUs or max(256 MiB, heapBudget / 64) on integrated/unified heaps, clamped by VK_EXT_memory_budget when reported. Explicit APS5_DEPTH_PLANE_COPY_MIB values (including 0, 1, and empty string) keep their existing behaviour.
  • Update docs/dev/ENVIRONMENT.md and record the unmeasured budget heuristic and release details in docs/dev/debt/changes/fix-agc-depth-plane-copy-release-budget.md.
  • Note on open PRs: fix(agc): bind memory only for the backed slices of a depth plane copy boykopovar/AnyPS5#22531 (sparse unbacked tail slices) and fix(agc): copy only the viewed slices of a 2D array over a depth surface boykopovar/AnyPS5#22778 (ViewedArrayWindow) touch the DepthPlaneCopy construction line at DepthSurface.cpp:4782; they address orthogonal issues and compose cleanly with this fix.

Tested

Arch Linux (7.3.5-arch1-1), Intel Core i7-13700K, AMD Radeon RX 6900 XT (RADV NAVI21), Mesa 26.3.1-arch1.2, Release build (-DBUILD_TESTING=ON), base main c880e0e50fe7.

  • New tests:
    • agc_driver_depth_plane_sampling_evicting (FrameCopiesCase, APS5_DEPTH_PLANE_COPY_MIB=1): verifies a sampled depth-plane copy is held by both planeCopies() and SharedResourceCache (use_count() >= 3), expires when evicted on a new copy insert, and also expires when evicted during a SharedResourceCache hit (Texture::TakeReuseLookupCounts() held == 1 && changed == 0, cache size drops without an Insert).
    • agc_driver_graphics_tests (depthPlaneCopyBudgetTests): covers discrete heap sizes, 256 MiB floor, host-only heap, unified/system memory heap, integrated GPU flag, and VkPhysicalDeviceMemoryBudgetPropertiesEXT clamping.
  • Vulkan validation A/B (ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation layer 1.4.363, VK_LOADER_DEBUG=layer, ctest --test-dir build -R "^(agc_driver_depth_plane_|agc_driver_graphics_tests$|agc_driver_sampled_cache_budget_|agc_driver_resource_cache_)" -V):
    • main (c880e0e50fe7): 34/34 passed, Insert instance layer "VK_LAYER_KHRONOS_validation" logged on every test, 0 was not found lines, 0 VUID- errors.
    • This branch (3057fe82db): 34/34 passed, Insert instance layer "VK_LAYER_KHRONOS_validation" logged on every test, 0 was not found lines, 0 VUID- errors.
  • tools/debt_fold.py --check and tools/check_conventions.py --base upstream/main: passed.
  • Demon's Souls (PPSA01342 01.004.000, OUTPOST PASSAGE, APS5_OUTPUT_RESOLUTION=1920x1080, ANYPS5_PRESENT_MODE=immediate, warmed shader cache, unprofiled) — tools/trace_digest.py:
main (c880e0e) — crashes at 125.9 s with VK_ERROR_DEVICE_LOST after 10 s stall
**12 intervals**, 0.000 to 125.890 s, 1785 presents
AMD Radeon RX 6900 XT (RADV NAVI21), radv Mesa 26.3.1-arch1.2, Linux 7.3.5-arch1-1, 3840x2160, commit c880e0e50fe7
build, environment, host, resolution or title metadata changed at interval 3

| # | seconds | presents | p50 | p90 | p99 | max | GPU busy | busiest worker | largest phase |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 0.000-10.007 | 112 | 40.6 | 64.6 | 526.1 | 4268.6 | 38.0 | 88 % | Driver.Packet 608.3 |
| 2 | 10.007-20.034 | 243 | 39.6 | 43.7 | 97.5 | 114.4 | 37.3 | 96 % | Driver.Packet 306.2 |
| 3 | 20.034-30.045 | 359 | 22.2 | 40.3 | 45.7 | 86.1 | 21.3 | 99 % | Driver.Packet 214.5 |
| 4 | 30.045-40.078 | 289 | 32.3 | 46.9 | 89.3 | 108.1 | 21.4 | 100 % | Driver.Packet 271.8 |
| 5 | 40.078-50.088 | 268 | 35.2 | 40.3 | 80.7 | 113.7 | 22.9 | 100 % | Driver.Packet 292.9 |
| 6 | 50.088-60.096 | 345 | 28.1 | 33.8 | 37.3 | 53.6 | 19.8 | 100 % | Driver.Packet 227.5 |
| 7 | 60.096-71.414 | 89 | 27.8 | 153.3 | 3438.8 | 3438.8 | 28.0 | 100 % | Driver.Packet 989.2 |
| 8 | 71.414-83.556 | 23 | 123.7 | 1578.5 | 4316.2 | 4316.2 | 98.7 | 101 % | Driver.Packet 3798.4 |
| 9 | 83.556-93.614 | 33 | 112.1 | 489.6 | 2395.9 | 2395.9 | 99.6 | 100 % | Driver.Packet 2171.5 |
| 10 | 93.614-103.758 | 5 | 1968.9 | 3477.3 | 3477.3 | 3477.3 | 538.4 | 100 % | Driver.Packet 15132.7 |
| 11 | 103.758-113.858 | 15 | 644.6 | 792.8 | 902.4 | 902.4 | 335.6 | 100 % | Driver.Packet 4789.0 |
| 12 | 113.858-125.890 | 4 | 749.8 | 10020.0 | 10020.0 | 10020.0 | 326.8 | 100 % | Driver.Packet 23346.1 |
this change — runs full 213 s cleanly; steady-state p50 413–436 ms (-34% to -43%), GPU busy 178–195 ms (-42% to -46%)
**21 intervals**, 0.000 to 213.047 s, 2153 presents
AMD Radeon RX 6900 XT (RADV NAVI21), radv Mesa 26.3.1-arch1.2, Linux 7.3.5-arch1-1, 3840x2160, commit c880e0e50fe7 (dirty)
build, environment, host, resolution or title metadata changed at interval 3

| # | seconds | presents | p50 | p90 | p99 | max | GPU busy | busiest worker | largest phase |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 0.000-10.012 | 123 | 39.7 | 46.9 | 459.5 | 3879.9 | 37.0 | 87 % | Driver.Packet 555.1 |
| 2 | 10.012-20.040 | 244 | 39.6 | 42.8 | 96.8 | 169.2 | 37.8 | 96 % | Driver.Packet 301.3 |
| 3 | 20.040-30.051 | 354 | 22.4 | 39.6 | 46.1 | 205.4 | 21.2 | 98 % | Driver.Packet 216.2 |
| 4 | 30.051-40.107 | 310 | 30.8 | 43.5 | 87.4 | 99.0 | 20.3 | 100 % | Driver.Packet 254.3 |
| 5 | 40.107-50.116 | 282 | 33.7 | 39.8 | 61.3 | 64.2 | 22.6 | 100 % | Driver.Packet 278.6 |
| 6 | 50.116-60.127 | 343 | 28.6 | 32.5 | 36.6 | 56.6 | 20.0 | 100 % | Driver.Packet 229.3 |
| 7 | 60.127-70.231 | 131 | 28.4 | 119.5 | 1413.7 | 1659.3 | 31.0 | 100 % | Driver.Packet 590.1 |
| 8 | 70.231-80.282 | 59 | 102.8 | 304.3 | 1931.8 | 1931.8 | 73.6 | 100 % | Driver.Packet 1131.8 |
| 9 | 80.282-90.500 | 21 | 439.2 | 708.7 | 1924.1 | 1924.1 | 143.7 | 100 % | Driver.Packet 3398.5 |
| 10 | 90.500-100.898 | 24 | 435.1 | 455.8 | 469.5 | 469.5 | 193.9 | 99 % | Driver.Packet 2775.3 |
| 11 | 100.898-111.054 | 24 | 428.1 | 446.1 | 465.2 | 465.2 | 178.6 | 99 % | Driver.Packet 2692.5 |
| 12 | 111.054-121.476 | 25 | 413.4 | 455.6 | 470.7 | 470.7 | 193.8 | 99 % | Driver.Packet 2647.7 |
| 13 | 121.476-131.707 | 24 | 425.7 | 450.7 | 468.1 | 468.1 | 189.4 | 99 % | Driver.Packet 2715.8 |
| 14 | 131.707-141.885 | 24 | 428.0 | 446.6 | 472.7 | 472.7 | 180.1 | 99 % | Driver.Packet 2704.9 |
| 15 | 141.885-152.003 | 23 | 433.5 | 469.6 | 507.8 | 507.8 | 192.9 | 99 % | Driver.Packet 2801.3 |
| 16 | 152.003-162.228 | 24 | 426.7 | 446.4 | 466.2 | 466.2 | 190.0 | 99 % | Driver.Packet 2703.8 |
| 17 | 162.228-172.383 | 24 | 423.6 | 453.7 | 478.4 | 478.4 | 192.6 | 99 % | Driver.Packet 2701.8 |
| 18 | 172.383-182.387 | 23 | 434.4 | 474.5 | 494.9 | 494.9 | 193.8 | 99 % | Driver.Packet 2740.6 |
| 19 | 182.387-192.590 | 24 | 425.5 | 457.0 | 496.3 | 496.3 | 194.7 | 99 % | Driver.Packet 2711.5 |
| 20 | 192.590-202.938 | 24 | 433.4 | 491.1 | 491.7 | 491.7 | 179.2 | 99 % | Driver.Packet 2730.7 |
| 21 | 202.938-213.047 | 23 | 436.1 | 475.6 | 489.9 | 489.9 | 190.5 | 99 % | Driver.Packet 2828.8 |

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 6900 XT (RADV NAVI21), Mesa 26.3.1-arch1.2, Arch Linux (kernel 7.3.5-arch1-1)
  • AI-assisted: yes

Created with gemini-4-neon in T3 Code.

Summary by CodeRabbit

  • Bug Fixes
    • Improved depth-texture sampling by preventing released textures from being reused and ensuring evicted copies are released promptly.
    • Improved resource-cache cleanup and reuse when depth-plane copies are evicted or replaced.
  • New Features
    • Depth-plane copy memory limits now scale with available graphics memory, with a 256 MiB minimum and an optional environment-variable override.
  • Documentation
    • Updated developer guidance with the budget calculation and copy-retention behavior.

@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Depth-plane copies

Layer / File(s) Summary
Heap-based copy budget
core/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp, core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp, core/libs/prx/libSceAgcDriver/tests/Graphics.cpp, docs/dev/ENVIRONMENT.md
The default budget uses device-local heap size, optional reported budget, GPU type, and a 256 MiB minimum. Eviction applies to copies for the current device. Tests cover budget calculations, and the environment documentation describes the default and retention rule.
Released-copy cache cleanup
core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp, core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp, core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp, core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneSampling.cpp, docs/dev/debt/changes/fix-agc-depth-plane-copy-release-budget.md
Copies track guest-plane addresses and notify the resource cache when externally retained textures are released. Resource revalidation rejects released textures before depth-surface checks. Replacement or eviction drops released cache entries and retains resulting resources in an active recording. Sampling tests check cache retention, reuse, and eviction.

Change: Bug fix

Merge Risk: Minimal · up to 198a1

This change scales the depth-plane copy budget with GPU memory and sweeps released copies from the resource cache sooner. No concrete merge-blocking problem was found, and new tests cover the budget and eviction paths. The default budget is still an unmeasured heuristic, which the PR documents.

Security architecture risk: Minimal · up to 198a1

AgcDriver now removes released depth-plane copies from reuse without relying on the shared cache to keep recorded GPU work alive. Eviction is limited to the current Vulkan device, and operators can still set a fixed cache budget. The reviewed paths did not establish a new security or failure-containment regression.

Security review details

Security Blast Radius

  • observed — A lookup can trigger eviction only within its device's plane-copy map. The follow-on sweep can remove entries anywhere in the process-wide shared resource cache, but only if they reference an already-released texture.

Resilience and Maintainability Implications

  • inferred — For the inspected paths, removal from the shared cache is not permission to destroy an in-flight image. Copy work either waits synchronously or retains the image and staging buffer in Recorder; recorded draws retain their shader resources, and submitted batches carry those references through completion.

Hardening Proposals

  • proposed — Make the existing shared-cache sweep retryable if collection fails partway through, so reclaiming released entries does not depend on a later release notification.

Rollout and rollback

  • observed — Operators who leave APS5_DEPTH_PLANE_COPY_MIB unset move from a fixed default to automatic sizing. Setting it to 256 before process startup restores the old numeric threshold, not the old cross-device policy or cleanup behavior. An explicit zero still disables budget eviction; it does not disable replacement cleanup.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Cppcheck (2.22.0)

Cppcheck (2.22.0) exceeded its 180-second time budget. Findings from completed work are retained; remaining checks are incomplete.

core/libs/prx/libSceAgcDriver/tests/Graphics.cpp

Cppcheck interrupted; analysis of this file is incomplete

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 8
  • Waived: $2.00 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 48290caf-e92e-4cf9-8c99-8030e16c8e4e
Commits

Reviewing files that changed from the base of the PR and between 4131f51 and 198a1f6.

Files selected for processing: 8

  • core/libs/prx/libSceAgcDriver/Graphics/include/DepthSurface.hpp
  • core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp
  • core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp
  • core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
  • core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
  • core/libs/prx/libSceAgcDriver/tests/execution/DepthPlaneSampling.cpp
  • docs/dev/ENVIRONMENT.md
  • docs/dev/debt/changes/fix-agc-depth-plane-copy-release-budget.md

Limit details: You’ve used all 10 included reviews currently available.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants