Skip to content

fix(agc): refresh stale sampled depth plane copies and size their budget from device memory - #24705

Closed
RafaelMendes1407 wants to merge 2 commits into
boykopovar:mainfrom
RafaelMendes1407:fix/agc-depth-plane-copy-staleness
Closed

RafaelMendes1407 wants to merge 2 commits into
boykopovar:mainfrom
RafaelMendes1407:fix/agc-depth-plane-copy-staleness

Conversation

@RafaelMendes1407

Copy link
Copy Markdown
Contributor

What

Fixes two problems in the cache of sampled depth-plane copies (DepthPlaneCopy in DepthSurface.cpp). Layered and array samples of a depth plane go through this cache, which APS5_DEPTH_PLANE_COPY_MIB bounds.

1. fix(agc): a cached copy could be served after its source plane changed. With the default 256 MiB budget, copies were usually evicted before the bug showed. A larger budget exposes it. In Demon's Souls (PPSA01341), APS5_DEPTH_PLANE_COPY_MIB=1024 on main makes most of the world vanish in Outpost Passage: black ground, with only the sky and a distant structure left. The title samples stale depth and discards geometry. The trace digest shows that "speed-up", because less is drawn. Three paths returned a stale copy:

  • Sampled lookups skipped the guest sync. The lookup compared the resident Vulkan image's write serial without first settling pending guest writes. A CPU or DMA rewrite of the depth memory leaves that serial unchanged, so the old copy came back. Sampled lookups now sync the surface from guest memory, with uncached write collection (DepthMemoryUse::Sample), and refresh each resident slice before comparing serials.

  • Guest-backed slices checked only their write generation. They now also:

    • flush pending depth-plane stores over the layer;
    • compare the GuestMemory forget stamp, to catch remapping;
    • refuse reuse while a snapshot write, a unit shadow or a pending storage unit overlaps the layer.

    Layer bookkeeping changes only after the refresh is recorded, so a refresh that never ran cannot mark the layer current.

  • The cache key lacked the guest layout. It was (device, address, width, height, format). Tiled and linear descriptors of the same address, or descriptors that differ in pipe-bank XOR, pitch or mip count, shared one image and one reuse decision. The key now carries the full layout. The detiler dispatch now uses the descriptor's mip offsets and pipe-bank XOR.

depthSlice also stops falling back to a linear scan that could return a multisampled or retired surface at the same address.

2. perf(agc): size the budget from device memory. Once reuse is correct, 256 MiB is too small for this scene. Copies are evicted and rebuilt every frame: a new image and staging buffer, a transfer of every slice, and a failed resource-cache revalidation. That happens about 68 times a second at 256 MiB, against 0.08 at 1 GiB. The default is now 1/16 of the largest device-local Vulkan heap, rounded up to 256 MiB steps, with a 256 MiB floor:

Device-local heap Budget
16 GB 1 GiB
8 GB 512 MiB
4 GB or less 256 MiB, as before

APS5_DEPTH_PLANE_COPY_MIB still overrides the default, and 0 still keeps every copy. docs/dev/ENVIRONMENT.md is updated.

Tests. Three new tests:

  • agc_driver_depth_plane_sampling_guest_rewrite runs with APS5_DEPTH_PLANE_COPY_MIB=0. It writes depth, samples it, rewrites the memory from the guest and samples again. Before the fix it read the old 0.25 instead of the new 0.75.
  • agc_driver_depth_plane_sampling_layout_reuse runs with APS5_DEPTH_PLANE_COPY_MIB=0. It samples one address through a tiled and a linear descriptor. Before the fix both got one shared image.
  • agc_driver_depth_plane_copy_budget checks the budget computation for several heap sizes.

Tested

CachyOS (Linux 7.3.5), AMD Radeon RX 9060 XT 16 GB (RDNA 4, RADV GFX1200), Mesa 26.3.1, Ryzen with 30 GB RAM.

Real title: Demon's Souls (PPSA01341 v01.007.000, bring-up #1283). Same save and the same spot in Outpost Passage, 1920x1080, shader cache warm.

main with APS5_DEPTH_PLANE_COPY_MIB=1024: world geometry missing This PR with APS5_DEPTH_PLANE_COPY_MIB=1024: correct
main 1024 PR 1024
main, default This PR, default (1 GiB on this card)
main default PR default

Performance, captured as the Performance report form describes. Each run has one APS5_TRACE_SECONDS=0:20 window, opened by APS5_TRACE_TRIGGER_FILE about 60 s after the player gains control. There is no other APS5_TRACE_* or APS5_PROFILE_* switch. The other switches, the same in both runs, are:

  • APS5_PAD_INPUT_FILE and APS5_NO_PAD_INPUT: scripted input that loads the save;
  • APS5_DUMP_FRAMES_EVERY=120: sparse frame dumps that the menu navigation reads;
  • APS5_NO_UNIT_SHADOW.

The title's log went to a file in both runs.

presents in 20 s frame p50 p90 p99 GPU busy per present app.elf peak RSS
main 451f25bd26 42 459.7 ms 548.3 830.1 222.6 ms 15.50 GB
this PR 54 364.7 ms 411.5 451.0 125.0 ms 15.17 GB

That is about 2.1 → 2.7 fps. The remaining frame time is CPU-side: queue worker 0x0 is about 75 % busy in Driver.Packet. Host memory does not grow. Both traces report dropped: yes, because queue worker 0x0 filled its default 262,144-event buffer after 5 to 7 s. The summary lines and the frame statistics above cover the whole window. The PR was measured at 51b5f93df2, which has the same tree as this branch (only the commit messages were reworded).

Summary lines (aps5-summary.jsonl)

main:

{"commit":"451f25bd2672","dirty":false,"env":{"APS5_DUMP_FRAMES":"100000","APS5_DUMP_FRAMES_EVERY":"120","APS5_NO_PAD_INPUT":"1","APS5_NO_UNIT_SHADOW":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"0:20","APS5_TRACE_TRIGGER_FILE":"<path>"},"gpu":"AMD Radeon RX 9060 XT (RADV GFX1200)","driver":"radv Mesa 26.3.1-arch3.4","os":"Linux 7.3.5-1-cachyos","cpu":"AMD Ryzen 9 9900X 12-Core Processor","cpu_threads":24,"memory_mib":31183,"title_id":"PPSA01341","title":"Demon's Souls","content_version":"01.007.000","resolution":"1920x1080","window":{"spec":"seconds 0:20","first_present":15879,"ms":20291.962},"presents":42,"frame_ms":{"p50":459.746,"p90":548.316,"p99":830.086,"max":830.086},"gpu_busy_ms_per_present":222.575,"worker_busy_pct":{"queue worker 0x0":99.326,"queue worker 0x20":78.022,"queue worker 0x28":70.317,"queue worker 0x30":98.528,"queue worker 0x40":75.841,"queue worker 0x38":93.897,"queue worker 0x50":84.172,"queue worker 0x48":69.253},"phase_totals_ms":{"Driver.Completion":5.446,"Driver.Dispatch":13689.282,"Driver.Dispatch.capture_resources":1125.546,"Driver.Dispatch.decode_key_device_setup":1909.366,"Driver.Dispatch.lookup_cache":448.329,"Driver.Dispatch.materialize":134.679,"Driver.Dispatch.prepared_invocation":93.905,"Driver.Draw":10272.834,"Driver.Draw.device_setup":208.288,"Driver.Draw.draw_enqueued":5524.950,"Driver.Draw.empty_indirect_skipped":7.676,"Driver.Draw.post_compile_prepare":38.480,"Driver.Draw.prepare":760.129,"Driver.Draw.shader_compile_and_link":3504.559,"Driver.EndSubmission":309.461,"Driver.Packet":135505.873,"Driver.Packet.device_lock":33.303,"Driver.Packet.dispatch":17238.925,"Driver.Packet.draw":10181.240,"Driver.Packet.execute":134.914,"Driver.Packet.flip_flush":14.087,"Driver.Packet.flush_between_packets":966.248,"Driver.Packet.memory_wait":106145.603,"Driver.Packet.pipeline_drain":128.735,"Driver.Packet.pipeline_prepare":49.638,"Driver.Packet.prepare_memory":55.020,"Driver.Packet.rendering_wait":0.074,"Driver.Present":37.782,"Driver.Present.acquire_image":2.264,"Driver.Present.device_setup":0.040,"Driver.Present.gpu_mutex_wait":9.436,"Driver.Present.inflight_wait":0.097,"Driver.Present.present_display_buffer":19.885,"Driver.Present.queue_present":1.178,"Driver.Present.release_and_callback":0.029,"Driver.Present.render_fence_wait":4.757,"Driver.Present.resize":0.070,"Driver.Present.validate":0.011,"Driver.Suspend":4.794,"GpuMutex.wait.copy":653.357,"GpuMutex.wait.dispatch":1430.486,"GpuMutex.wait.draw":3393.127,"GpuMutex.wait.end":65.648,"GpuMutex.wait.fill":1.787,"GpuMutex.wait.flush":37.335,"GpuMutex.wait.hook":1107.842,"GpuMutex.wait.indirect":292.581,"GpuMutex.wait.label":106.974,"GpuMutex.wait.present":9.367,"GpuMutex.wait.wait":21.286,"Graphics.Draw":4470.544,"Graphics.Draw.draw_and_resource_release":3728.364,"Graphics.Draw.index_upload":485.534,"Graphics.Draw.validate":223.595,"Graphics.Draw.vertex_upload":11.199,"Graphics.DrawWithRecipe":46.923,"Graphics.DrawWithRecipe.draw_and_resource_release":44.841,"Graphics.DrawWithRecipe.index_upload":0.754,"Graphics.DrawWithRecipe.validate":0.629,"Graphics.DrawWithRecipe.vertex_upload":0.229,"Recorder.Reap":121.260,"Recorder.Submit":2134.320,"Recorder.WaitSerial":0.192,"Resources.Complete":5415.575,"Resources.Prepare":262.341,"Shader.DrawStage":4583.731,"Shader.DrawStage.capture_resources":3505.470,"Shader.DrawStage.materialize":686.048,"Shader.DrawStage.prepared_invocation":352.917,"Shader.ResolveAbi":150.770,"Shader.ResolveGraphicsAbi":47.283,"Shader.ResolveGraphicsStagesAbi":123.472,"VideoOut.Flip":37.608,"VideoOut.Flip.completion_mutex_wait":0.017,"VideoOut.Flip.config_mutex_wait":0.004,"VideoOut.Flip.frame_limit":0.032,"VideoOut.Flip.notify_game":0.491,"VideoOut.Flip.present":36.721,"VideoOut.Flip.validate":0.053,"VideoOut.Flip.vblank_wait":0.042,"VideoOut.Flip.window_prepare":0.075,"VideoOut.Flip.window_title":0.162,"VideoOut.Readiness":0.128,"VideoOut.Readiness.locks_validate":0.074,"Vulkan.Acquire":2.086,"Vulkan.Acquire.acquire_fence_wait":0.072,"Vulkan.Acquire.acquire_image":1.833,"Vulkan.Acquire.fence_reset":0.157,"Vulkan.Acquire.retired_swapchains":0.015,"Vulkan.Dispatch":6048.024,"Vulkan.Dispatch.command_record":1561.717,"Vulkan.Dispatch.shader_resources":4472.945,"Vulkan.DispatchRecipe":4.242,"Vulkan.DispatchRecipe.command_record":4.173,"Vulkan.DrawFromRecipe":47.452,"Vulkan.GraphicsPipeline":0.123,"Vulkan.GraphicsPipeline.create":0.068,"Vulkan.GraphicsPipeline.modules_and_state":0.054,"Vulkan.GraphicsPipelineLibrary":0.025,"Vulkan.GraphicsPipelineLink":0.012,"Vulkan.Present":19.410,"Vulkan.Present.command_record_scale":0.964,"Vulkan.Present.pixel_upload":5.121,"Vulkan.Present.queue_submit":13.293,"Vulkan.QueuePresent":1.126,"Vulkan.QueuePresent.queue_present":1.008,"Vulkan.Retire":4.705,"Vulkan.Retire.wait":4.015},"top_phases_ms_per_frame":{"Driver.Packet":3226.330,"Driver.Packet.memory_wait":2527.276,"Driver.Packet.dispatch":410.451,"Driver.Dispatch":325.935,"Driver.Draw":244.591,"Driver.Packet.draw":242.410,"Vulkan.Dispatch":144.001,"Driver.Draw.draw_enqueued":131.546},"owned_draw_counts":{},"graphics_track":{"Graphics.Barrier.copy":2561,"Graphics.Barrier.dcc-clear":840,"Graphics.Barrier.dcc-keys":548,"Graphics.Barrier.dispatch-lead":108176,"Graphics.Barrier.dispatch-trail":99593,"Graphics.Barrier.draw":28611,"Graphics.Barrier.fill":1820,"Graphics.Barrier.fill-clear":168,"Graphics.Barrier.indirect-args":10500,"Graphics.Barrier.label-run":28859,"Graphics.Barrier.present-blit":84,"Graphics.Barrier.staging-in":168955,"Graphics.Barrier.staging-out":83689,"Graphics.Barrier.storage-upload":7286,"Graphics.Barrier.storage-writeback":26146,"Graphics.Barrier.template-refresh":4654,"Graphics.BarrierMerged.copy":1449,"Graphics.BarrierMerged.dispatch-lead":12417,"Graphics.BarrierMerged.fill":326,"Graphics.BarrierMerged.label-run":1,"Graphics.BarrierMerged.staging-in":81557,"Graphics.BarrierMerged.staging-out":83689,"Graphics.BarrierMerged.storage-writeback":4357,"Graphics.Copy":147612,"Graphics.Copy.bytes":74368029960,"Graphics.RenderPass":12708,"Graphics.RenderPass.Close.command":1831,"Graphics.RenderPass.Close.image":7893,"Graphics.RenderPass.Close.key":345,"Graphics.RenderPass.Close.label":1,"Graphics.RenderPass.Close.new-batch":2638,"Graphics.RenderPass.Continued":244979},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":7.025,"events_lost":495752,"totals_full":false}},"trace":"aps5-trace-451f25bd2672-592657.json.gz"}

this PR:

{"commit":"51b5f93df23b","dirty":false,"env":{"APS5_DUMP_FRAMES":"100000","APS5_DUMP_FRAMES_EVERY":"120","APS5_NO_PAD_INPUT":"1","APS5_NO_UNIT_SHADOW":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"0:20","APS5_TRACE_TRIGGER_FILE":"<path>"},"gpu":"AMD Radeon RX 9060 XT (RADV GFX1200)","driver":"radv Mesa 26.3.1-arch3.4","os":"Linux 7.3.5-1-cachyos","cpu":"AMD Ryzen 9 9900X 12-Core Processor","cpu_threads":24,"memory_mib":31183,"title_id":"PPSA01341","title":"Demon's Souls","content_version":"01.007.000","resolution":"1920x1080","window":{"spec":"seconds 0:20","first_present":11619,"ms":20091.300},"presents":54,"frame_ms":{"p50":364.692,"p90":411.496,"p99":450.995,"max":450.995},"gpu_busy_ms_per_present":125.022,"worker_busy_pct":{"queue worker 0x0":99.328,"queue worker 0x20":70.636,"queue worker 0x30":97.857,"queue worker 0x28":63.051,"queue worker 0x38":95.173,"queue worker 0x48":61.390,"queue worker 0x50":79.438,"queue worker 0x40":67.190},"phase_totals_ms":{"Driver.Completion":6.175,"Driver.Dispatch":12371.246,"Driver.Dispatch.capture_resources":1401.522,"Driver.Dispatch.decode_key_device_setup":1079.576,"Driver.Dispatch.lookup_cache":576.785,"Driver.Dispatch.materialize":171.728,"Driver.Dispatch.prepared_invocation":115.857,"Driver.Draw":9706.407,"Driver.Draw.device_setup":259.372,"Driver.Draw.draw_enqueued":3739.623,"Driver.Draw.empty_indirect_skipped":9.826,"Driver.Draw.post_compile_prepare":50.830,"Driver.Draw.prepare":977.447,"Driver.Draw.shader_compile_and_link":4406.247,"Driver.EndSubmission":553.942,"Driver.Packet":126812.379,"Driver.Packet.device_lock":43.020,"Driver.Packet.dispatch":14035.030,"Driver.Packet.draw":9609.302,"Driver.Packet.execute":174.108,"Driver.Packet.flip_flush":24.146,"Driver.Packet.flush_between_packets":1787.159,"Driver.Packet.memory_wait":100694.003,"Driver.Packet.pipeline_drain":212.479,"Driver.Packet.pipeline_prepare":63.154,"Driver.Packet.prepare_memory":71.735,"Driver.Packet.rendering_wait":0.097,"Driver.Present":73.840,"Driver.Present.acquire_image":6.062,"Driver.Present.device_setup":0.048,"Driver.Present.gpu_mutex_wait":21.451,"Driver.Present.inflight_wait":0.660,"Driver.Present.present_display_buffer":39.058,"Driver.Present.queue_present":1.510,"Driver.Present.release_and_callback":0.039,"Driver.Present.render_fence_wait":4.891,"Driver.Present.resize":0.091,"Driver.Present.validate":0.017,"Driver.Suspend":19.176,"GpuMutex.wait.copy":240.875,"GpuMutex.wait.dispatch":2322.963,"GpuMutex.wait.draw":441.272,"GpuMutex.wait.end":118.314,"GpuMutex.wait.fill":4.404,"GpuMutex.wait.flush":113.022,"GpuMutex.wait.hook":921.019,"GpuMutex.wait.indirect":575.916,"GpuMutex.wait.label":214.926,"GpuMutex.wait.present":21.367,"GpuMutex.wait.wait":37.734,"Graphics.Draw":3688.848,"Graphics.Draw.draw_and_resource_release":2740.903,"Graphics.Draw.index_upload":622.352,"Graphics.Draw.validate":283.128,"Graphics.Draw.vertex_upload":14.055,"Graphics.DrawWithRecipe":70.505,"Graphics.DrawWithRecipe.draw_and_resource_release":67.662,"Graphics.DrawWithRecipe.index_upload":1.183,"Graphics.DrawWithRecipe.validate":0.966,"Graphics.DrawWithRecipe.vertex_upload":0.356,"Recorder.Reap":156.334,"Recorder.Submit":3571.190,"Recorder.WaitSerial":0.280,"Resources.Complete":859.713,"Resources.Prepare":296.967,"Shader.DrawStage":5825.352,"Shader.DrawStage.capture_resources":4502.840,"Shader.DrawStage.materialize":824.311,"Shader.DrawStage.prepared_invocation":449.963,"Shader.ResolveAbi":181.779,"Shader.ResolveGraphicsAbi":64.109,"Shader.ResolveGraphicsStagesAbi":152.425,"VideoOut.Flip":74.420,"VideoOut.Flip.completion_mutex_wait":0.026,"VideoOut.Flip.config_mutex_wait":0.004,"VideoOut.Flip.frame_limit":0.037,"VideoOut.Flip.notify_game":1.004,"VideoOut.Flip.present":72.938,"VideoOut.Flip.validate":0.069,"VideoOut.Flip.vblank_wait":0.061,"VideoOut.Flip.window_prepare":0.101,"VideoOut.Flip.window_title":0.164,"VideoOut.Readiness":0.164,"VideoOut.Readiness.locks_validate":0.090,"Vulkan.Acquire":5.849,"Vulkan.Acquire.acquire_fence_wait":0.094,"Vulkan.Acquire.acquire_image":5.505,"Vulkan.Acquire.fence_reset":0.214,"Vulkan.Acquire.retired_swapchains":0.023,"Vulkan.Dispatch":4393.487,"Vulkan.Dispatch.command_record":2084.994,"Vulkan.Dispatch.shader_resources":2290.248,"Vulkan.DispatchRecipe":5.365,"Vulkan.DispatchRecipe.command_record":5.280,"Vulkan.DrawFromRecipe":71.296,"Vulkan.GraphicsPipeline":0.383,"Vulkan.GraphicsPipeline.create":0.221,"Vulkan.GraphicsPipeline.modules_and_state":0.160,"Vulkan.GraphicsPipelineLibrary":0.080,"Vulkan.GraphicsPipelineLink":0.044,"Vulkan.Present":38.433,"Vulkan.Present.command_record_scale":1.266,"Vulkan.Present.pixel_upload":12.687,"Vulkan.Present.queue_submit":24.439,"Vulkan.QueuePresent":1.443,"Vulkan.QueuePresent.queue_present":1.306,"Vulkan.Retire":4.837,"Vulkan.Retire.dump":0.062,"Vulkan.Retire.wait":4.292},"top_phases_ms_per_frame":{"Driver.Packet":2348.377,"Driver.Packet.memory_wait":1864.704,"Driver.Packet.dispatch":259.908,"Driver.Dispatch":229.097,"Driver.Draw":179.748,"Driver.Packet.draw":177.950,"Shader.DrawStage":107.877,"Shader.DrawStage.capture_resources":83.386},"owned_draw_counts":{},"graphics_track":{"Graphics.Barrier.copy":3676,"Graphics.Barrier.dcc-clear":1080,"Graphics.Barrier.dcc-keys":774,"Graphics.Barrier.dispatch-lead":139811,"Graphics.Barrier.dispatch-trail":127746,"Graphics.Barrier.draw":34268,"Graphics.Barrier.fill":2379,"Graphics.Barrier.fill-clear":216,"Graphics.Barrier.indirect-args":13500,"Graphics.Barrier.label-run":37078,"Graphics.Barrier.present-blit":108,"Graphics.Barrier.staging-in":217328,"Graphics.Barrier.staging-out":107608,"Graphics.Barrier.storage-upload":9372,"Graphics.Barrier.storage-writeback":33683,"Graphics.Barrier.template-refresh":6318,"Graphics.BarrierMerged.copy":2232,"Graphics.BarrierMerged.dispatch-lead":14935,"Graphics.BarrierMerged.fill":421,"Graphics.BarrierMerged.label-run":85,"Graphics.BarrierMerged.staging-in":104802,"Graphics.BarrierMerged.staging-out":107608,"Graphics.BarrierMerged.storage-writeback":5656,"Graphics.Copy":187509,"Graphics.Copy.bytes":87987597160,"Graphics.RenderPass":16147,"Graphics.RenderPass.Close.command":2099,"Graphics.RenderPass.Close.image":10129,"Graphics.RenderPass.Close.key":486,"Graphics.RenderPass.Close.label":85,"Graphics.RenderPass.Close.new-batch":3348,"Graphics.RenderPass.Continued":315178},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":5.335,"events_lost":720920,"totals_full":false}},"trace":"aps5-trace-51b5f93df23b-603254.json.gz"}

Traces: main · this PR. trace_digest.py outputs: main · this PR.

Focused GPU tests, on the same machine and GPU, for main (451f25bd26) and this PR:

ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 \
VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer \
ctest --test-dir build -R agc_driver_depth_plane -V

The Khronos validation layer is 1.4.363.

Build Result Insert instance layer "VK_LAYER_KHRONOS_validation" was not found but was requested VUID-
main 19/19 passed 19 0 0
this PR 22/22 passed 21 (agc_driver_depth_plane_copy_budget creates no Vulkan instance) 0 0

python3 tools/check_conventions.py --base origin/main passes. python3 tools/pr_overlap.py --branch fix/agc-depth-plane-copy-staleness lists #15621, #20315, #22062 and #22647 as touching DepthSurface.cpp. None of them changes the sampled depth-plane copy cache: they cover store ordering, host imports, multisample publish and draw commit. The other matches touch docs/dev/ENVIRONMENT.md only.

Not tested: NVIDIA, Intel, Windows, and cards with less than 16 GB of VRAM. On those cards only the budget arithmetic is covered, by the unit test.

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: PPSA01341 Demon's Souls (PPSA01341) #1283
  • GPUs tested: AMD Radeon RX 9060 XT (RDNA 4), RADV Mesa 26.3.1, CachyOS Linux 7.3.5
  • AI-assisted: yes

🤖 Generated with Claude Code

RafaelMendes1407 and others added 2 commits November 20, 2026 20:25
Layered and array samples of a depth plane go through a cache of
DepthPlaneCopy images, kept for reuse under the
APS5_DEPTH_PLANE_COPY_MIB budget. A copy was served again after its
source plane had changed in three cases:

- Sampled lookups checked the resident Vulkan image and its write
  serial without first settling pending guest writes. A CPU or DMA
  rewrite of the depth memory left the serial unchanged, so the old
  copy was returned. The lookup now syncs the surface from guest memory
  with uncached write collection (DepthMemoryUse::Sample) and refreshes
  each resident slice before comparing serials.
- Guest-backed slices compared only their write generation. They now
  also flush pending depth-plane stores over the layer, compare the
  GuestMemory forget stamp (remapping), and refuse reuse while a
  snapshot write, a unit shadow or a pending storage unit overlaps the
  layer. Layer bookkeeping is updated only after the refresh is
  recorded.
- The cache key was (device, address, width, height, format). Tiled
  and linear descriptors of the same address, or descriptors with a
  different pipe-bank XOR, pitch or mip count, shared one image and its
  reuse decision. The key now carries the full guest layout, and the
  detiler dispatch uses the descriptor's mip offsets and pipe-bank XOR.

depthSlice no longer falls back to a linear scan that could pick a
multisampled or retired surface at the same address.

With the default 256 MiB budget, copies were usually evicted before the
stale reuse could happen. With a larger budget
(APS5_DEPTH_PLANE_COPY_MIB=1024), PPSA01341 lost most world geometry in
Outpost Passage (black ground, only sky visible), because it sampled
old depth.

Tests: agc_driver_depth_plane_sampling_guest_rewrite and
agc_driver_depth_plane_sampling_layout_reuse run with
APS5_DEPTH_PLANE_COPY_MIB=0. Before this change the rewrite test sampled
the old value 0.25 after a guest write of 0.75, and the layout test
found one shared image.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A fixed 256 MiB budget is too small for scenes that sample many depth
planes per frame. Copies are evicted and rebuilt every frame: a new
image and staging buffer, a transfer of every slice and a failed
resource-cache revalidation, about 68 per second in PPSA01341 Outpost
Passage against 0.08 with a 1 GiB budget.

The default is now 1/16 of the largest device-local Vulkan heap,
rounded up to 256 MiB steps, with a 256 MiB floor. A 16 GB card gets
1 GiB and an 8 GB card 512 MiB; cards of 4 GB or less keep 256 MiB.
APS5_DEPTH_PLANE_COPY_MIB still overrides it, and 0 still keeps every
copy.

Test: agc_driver_depth_plane_copy_budget covers the computation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — changes requested

Changes are required before merge: make eviction select candidates from the device whose bytes are being counted.

What I ran

  • Read the prepared diff and all five changed files at head 27f8ef19730d394d9fe9084bc860734a9dcf6482.
  • Read targeted cache refresh, eviction, depth-plane flush, and device-local heap code. No tests or builds were run.

Results

Check Result Note
Changed-file review passed All five changed files read
Targeted source review passed Relevant callers and helpers inspected
Tests and build not run No commands run

Findings

Blocking

  • core/libs/prx/libSceAgcDriver/Graphics/src/DepthSurface.cpp:2888: The byte total is calculated only for keep.device (lines 2878–2880), but the eviction candidate loop chooses the globally oldest copy, without requiring that it belongs to that device. If another device’s entry is older, its bytes are subtracted from this device’s total and its cache entry is erased. The loop can then stop while the target device remains over budget, and can evict another device’s cache unnecessarily. Restrict candidates to keep.device before selecting the oldest entry.

Non-blocking

None.

Not verified

  • No build or test execution; the new regression tests and runtime behavior remain unverified here.
  • Prepared bundle lists all five changed files as included; all five were read. No changed files were excluded.

To get this to a good state

  • Restrict the eviction candidate scan to copies belonging to keep.device, then rerun the relevant depth-plane cache tests and build on the updated head.

@tablefrogpillow

Copy link
Copy Markdown
Contributor

I ran this under RADV on three AMD generations besides your RDNA4 (Mesa 26.2.6 here, not 26.3.1), plus llvmpipe. There are no failures and no validation errors.

Builds: merge-base 451f25bd26 against head 27f8ef1973, Release, Linux 6.8, RADV from Mesa 26.2.6. Env: ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (layer 1.4.363), VK_LOADER_DEBUG=layer. Tests: those under ^agc_.*(depth_plane|depth_surface|depth_storage|layered_depth|depth_sampl|depth_copy) whose targets I built (30 on main, 33 with this change), one run per arm per GPU, -j1.

GPU main this change
RX 7900 GRE (NAVI31, gfx1100, RDNA3) 30/30 33/33
Ryzen 7800X3D iGPU (RAPHAEL, gfx1036, RDNA2) 30/30 33/33
RX Vega 56 (VEGA10, gfx900, GCN5) 30/30 33/33
llvmpipe 30/30 33/33

The three extra tests on this change are the new ones.

  • agc_driver_depth_plane_sampling_guest_rewrite, agc_driver_depth_plane_sampling_layout_reuse and agc_driver_depth_plane_copy_budget pass everywhere, and none of them skipped (exit 77). The existing depth_plane_sampling_evicting, depth_plane_guest_layers, gcn_padded_depth_sampling, depth_storage* and depth_surface_* tests pass on both arms.
  • Every log shows Insert instance layer "VK_LAYER_KHRONOS_validation" and no was not found but was requested. No log has a VUID-.
  • On Vega I ran the GRE-built binaries directly with each test's ctest environment and arguments. The log confirms the device: Selected GPU name=AMD Radeon RX Vega (RADV VEGA10) device=0x687f.
  • Five more tests match the pattern (agc_depth_copy_kept_bytes and four image_table_reuse_*depth_surface). They show Not Run on both arms only because I didn't build their targets.

I didn't reproduce the stale-copy bug on main: the new tests don't exist there, and the head binary doesn't link against main's library. So this shows the change causes no regression on these GPUs and that the new tests pass on them, not that the fix works. I did not run Demon's Souls. Each process here opens a single device, so the runs don't cover the cross-device eviction case from the review.

@github-actions github-actions Bot added unblocks title Names a title ID on the Unblocks line build docs agc libSceAgc* open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 labels Nov 21, 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 Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build docs open PR limit Closed over the open PR limit: 1 without merged PRs, 20 with at least 1, none from 20 unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants