perf(agc): stage written-only buffer elements a dispatch sweeps on discrete GPUs #7618

Merged
mrfelek merged 1 commit from upstream-pr/7618 into main 2026-10-25 12:19:08 +00:00
mrfelek commented 2026-10-18 17:30:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-18T17:30:58Z.


What

2688b7646 stages a written V# element in a device-local buffer when a direct dispatch sweeps it: threads times the record size cover at least half of the element. main applies that up to 16 MiB, and only when the shader also reads the element. 6d9222082 (#6486) extended it to raw elements through their access extent, still only for read and written elements. The message of 2688b7646 explains why write-only elements keep the 2 MiB window: a kernel streaming once through a large buffer, like the engine's memcpy over 4–8 MiB video frames, would only gain the two copies.

Diablo II: Resurrected (PPSA03912) shows the other side of that. Once per frame it runs a compute dispatch of 144x144 groups of 8x8 threads (1,327,104 threads). Each thread writes into its own 160-byte record with buffer_store_dwordx4 (8 of them, so at most 128 of the 160 bytes), in a 212 MB write-only element (0x2a86e47f0+0xca80000, exactly threads x 160). One store instruction writes 16 bytes per lane, and neighbouring lanes are 160 bytes apart, so in place in the host import the stores are scattered. They take about 56–60 ms of GPU time per frame. Staged, the kernel takes about 3.5 ms, plus about 22 ms of bulk copies in and back.

Change, on discrete GPUs only (Context::discreteGpu, set from VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU):

  • ShaderResources::addGuestBuffer: a write-only element that a direct dispatch sweeps is staged as well when its V# stride is wider than the widest buffer store, 16 bytes (read || (context.discreteGpu && descriptor.Stride() > WidestBufferStoreBytes)). With records that wide, the lanes of every store land a record apart, which is what staging speeds up.
  • The swept cap goes from 16 MiB to 256 MiB (DiscreteSweptShadowMax).

Integrated GPUs keep today's rule. Their device-local memory is the same system RAM, so staging there gains little and pays for two copies.

What stays as it is:

  • a write-only element whose records are 16 bytes or less: one store can fill a whole record, so the lanes of a store write it contiguously, and it stays in place;
  • write-only raw elements (V# stride 0) are not swept, so they keep the 2 MiB window;
  • read and written elements, raw or strided, follow main's rule unchanged, apart from the larger cap on discrete GPUs;
  • indirect dispatches (unknown thread count) are not swept;
  • dispatches that touch only part of an element are not swept;
  • the copy-in is kept for write-only elements, because the copy-back covers the whole element and the stores may leave part of each record unwritten (in D2R at most 128 of each 160-byte record). The debt entry "Swept write-only staging" says when it could go.

Trade-off: a kernel with records wider than 16 bytes that it writes completely and once now pays two bulk copies instead of one pass of scattered in-place stores. If the memcpy kernel from 2688b7646 binds its frames with a stride wider than 16 bytes, it falls under this; I couldn't find that title in the commit, so I haven't measured it. Records of 16 bytes or less stay in place even when their stores are narrower, because store widths are not recorded per instruction; that is the safe side.

Cost:

  • Memory: from the code, not measured: every shadow is its own device-local buffer, and a cached resource build keeps it, so D2R's 212 MB shadow stays allocated while that build is cached and a reused build records its copies again. When the device refuses the allocation (device memory or the allocation count exhausted, for example on a card close to its VRAM budget), the element is bound in place as before, [buffers] staging shadow of N bytes refused is printed and stagingRefused counts it; nothing aborts.
  • PCIe: on discrete GPUs the copies cross the bus. D2R moves 236 MiB in and 236 MiB back per present, about 4 GiB/s at 8.5 presents per second. That is a small share of an x16 link; on an x4 or x8 link (laptop eGPU, bifurcated slot) it is a visible share. I have no such machine to measure it. APS5_NO_WRITTEN_SHADOW=1 binds these elements in place again.

Tested

Linux: Ubuntu 26.10 (kernel 7.3), Ryzen 9 5950X, RTX 3080 Ti (NVIDIA 615.71.09 open), GCC 15.3.0, Release.

Windows: Windows 11 Pro 26200, i7-11850H, RTX A2000 Laptop GPU (NVIDIA 610.60), WinLibs GCC 15.2.0, Release.

Linux / AMD integrated: Ubuntu 26.04.1 (kernel 7.0), Ryzen 9 PRO 8945HS, Radeon 780M (RADV, Mesa 26.0.8, 2 GB carve-out), GCC 15.2.0, Release.

  • agc_driver_recorder readWrittenStagingTests:

    • The test device sets discreteGpu from the selected device's type.
    • A dispatch sweeping a written-only 4 MiB element of 32-byte records expects staging on a discrete GPU and in place otherwise.
    • New: a dispatch sweeping a written-only 4 MiB element of 16-byte records, which its stores write contiguously, binds it in place on every GPU.
    • A dispatch touching 32 KiB of a written-only element binds it in place.
    • A written-only raw (stride 0) element binds it in place.
    • A dispatch sweeping a written-only 31.25 MiB element of 160-byte records, and one sweeping a read and written element of that size, are staged on a discrete GPU and bound in place otherwise. The test block is 40 MiB for these cases.

    I ran the group on its own, through a local --tmp-staging-only switch that isn't part of this PR. It passes on the RTX 3080 Ti, on the Radeon 780M (integrated) and on the RTX A2000 under Windows at ab4c1febe, and on the RTX 3080 Ti again after the rebases onto main eaa5c9238 and d14a186f4 (this head). On the RTX 3080 Ti I also ran it on main f71a31193, on ab4c1febe and on this head with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (layer 1.4.357, loaded): all three pass with no VUID- message, so the change adds none. The full agc_driver_recorder_tests binary stops earlier, at (9) a scan under a pending write was kept, the same way as on main, so it's unrelated.

  • D2R (PPSA03912) on Linux, RTX 3080 Ti: in game with a saved character, the game's default settings, no input. main f71a31193 and this head on top of it, each relinked with its own relinker. APS5_PROFILE_GPU=1 APS5_TRACE_SUMMARY_EVERY=10 APS5_TRACE_SECONDS=170:10, 120 s from 60 s after the character loads, run alternately (main, this, this, main). Steady intervals from 100 s on:

main f71a31193 this change (27876656b, same code as this head)
Presents per 10 s 61.8, 61.0 86.6, 85.0
Frame p50 162, 164 ms 116, 117 ms
GPU busy per present 150, 152 ms 106 ms
GPU time of the dispatch, per frame ~55 ms ~3.4 ms
staging-in / staging-out per present 0.16 / 0.20 ms, 2.4 MiB 9.8 / 11.8 ms, 236 MiB

The first run of this change reported GPU time only for its first two intervals, so the GPU busy row has the second run only. One more main run gave no numbers: it started while the monitors were asleep (SDL_GetWindowDisplayIndex failed: Couldn't find any displays). None of the runs of this change failed. The previous head (f959abc09, Stride() != 0) gave 79.1 and 81.5 presents per 10 s against 59.5 and 56.7 on main in earlier runs; this rule stages the same D2R element.

trace_digest.py rows (steady part, second run of each):

main f71a31193   | 103-113 | 61 | 163.9 | 171.1 | 175.4 | 175.4 | 151.8 | 98 % | Driver.Packet 251.8 |
                 | 113-123 | 61 | 164.7 | 171.3 | 175.8 | 175.8 | 152.1 | 98 % | Driver.Packet 253.5 |
this 27876656b   | 104-114 | 85 | 118.3 | 122.6 | 126.8 | 126.8 | 106.3 | 98 % | Driver.Packet 170.1 |
                 | 114-124 | 86 | 117.1 | 120.9 | 125.1 | 125.1 | 107.3 | 98 % | Driver.Packet 169.1 |
performance_compare.py on the two 10 s trace windows (second run of each)

Performance comparison

Capture Commit Trace Presents Window (ms)
baseline f71a311935 aps5-trace-f71a311935f3-50227.json.gz 62 10135.718
candidate 27876656bdf9 aps5-trace-27876656bdf9-48243.json.gz 85 10047.981
  • Compare captures from the same machine, title and scene; summaries do not record the CPU, title or scene.
  • Changes are candidate minus baseline. A negative frame-time change means a shorter frame interval.
  • A missing top phase is unreported, not zero; its change is unavailable.
  • Recorded configuration differs; these measurements do not isolate the code change.
  • baseline: trace events were dropped; the event trace omits some of the work included in phase totals.
  • candidate: trace events were dropped; the event trace omits some of the work included in phase totals.

Configuration differences

Setting Baseline Candidate
window.first_present 814 1085

Frame and GPU times (ms)

Metric Baseline Candidate Change Change (%)
frame_ms.p50 162.924 117.445 -45.479 -27.914
frame_ms.p90 172.193 123.167 -49.026 -28.472
frame_ms.p99 178.736 131.254 -47.482 -26.565
frame_ms.max 178.736 131.254 -47.482 -26.565
mean_frame_ms 163.479 118.212 -45.268 -27.690
gpu_busy_ms_per_present 153.323 105.947 -47.376 -30.899

Top phases (ms per frame)

Metric Baseline Candidate Change Change (%)
Driver.Draw 142.854 96.904 -45.950 -32.166
Driver.Draw.draw_and_resource_release 66.593 n/a n/a n/a
Driver.Draw.shader_compile_and_link 55.959 56.238 +0.279 +0.499
Driver.Packet 250.643 169.643 -81.000 -32.317
Driver.Packet.draw 143.541 97.621 -45.920 -31.991
Driver.Packet.memory_wait 90.114 54.566 -35.548 -39.448
Graphics.Draw 65.706 66.725 +1.019 +1.551
Graphics.Draw.draw_and_resource_release 55.070 56.054 +0.984 +1.787
Shader.DrawStage n/a 48.709 n/a n/a
aps5-summary.jsonl lines of the two trace windows above

main f71a31193:

{"commit":"f71a311935f3","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"16384","APS5_PAD_INPUT_FILE":"<path>","APS5_PROFILE_GPU":"1","APS5_THREAD_PRIORITY":"high","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"170:10","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 3080 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.3.0-9-generic","resolution":"3840x2160","window":{"spec":"seconds 170:10","first_present":814,"ms":10135.718},"presents":62,"frame_ms":{"p50":162.924,"p90":172.193,"p99":178.736,"max":178.736},"gpu_busy_ms_per_present":153.323,"worker_busy_pct":{"queue worker 0x23":55.278,"queue worker 0x0":98.207},"top_phases_ms_per_frame":{"Driver.Packet":250.643,"Driver.Packet.draw":143.541,"Driver.Draw":142.854,"Driver.Packet.memory_wait":90.114,"Driver.Draw.draw_and_resource_release":66.593,"Graphics.Draw":65.706,"Driver.Draw.shader_compile_and_link":55.959,"Graphics.Draw.draw_and_resource_release":55.070},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":5.886,"events_lost":186347,"totals_full":false}},"trace":"aps5-trace-f71a311935f3-50227.json.gz"}

this change (27876656b, same code as this head):

{"commit":"27876656bdf9","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"16384","APS5_PAD_INPUT_FILE":"<path>","APS5_PROFILE_GPU":"1","APS5_THREAD_PRIORITY":"high","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"170:10","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 3080 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.3.0-9-generic","resolution":"3840x2160","window":{"spec":"seconds 170:10","first_present":1085,"ms":10047.981},"presents":85,"frame_ms":{"p50":117.445,"p90":123.167,"p99":131.254,"max":131.254},"gpu_busy_ms_per_present":105.947,"worker_busy_pct":{"queue worker 0x23":46.384,"queue worker 0x0":97.366},"top_phases_ms_per_frame":{"Driver.Packet":169.643,"Driver.Packet.draw":97.621,"Driver.Draw":96.904,"Graphics.Draw":66.725,"Driver.Draw.shader_compile_and_link":56.238,"Graphics.Draw.draw_and_resource_release":56.054,"Driver.Packet.memory_wait":54.566,"Shader.DrawStage":48.709},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":4.265,"events_lost":356234,"totals_full":false}},"trace":"aps5-trace-27876656bdf9-48243.json.gz"}
  • Astro Bot (PPSA21564) on Linux, RTX 3080 Ti: Sky Garden (underwater_aerial_garden, actual gameplay), from a progress save, default settings, hands off from the level start (the level flies on its own), APS5_TRACE_STAGING=1 to list every distinct staged element. One run each, on main f71a31193:

    main f71a31193 this change (27876656b) previous head f959abc09 (Stride() != 0)
    Distinct staged elements 91, largest 10 MiB 91, largest 10 MiB 95, four more of 16, 26, 26 and 49 MiB
    staging-in per present about 3.0 ms, 64 MiB about 3.0 ms, 66 MiB about 4.5 ms, 97 MiB
    Presents per 10 s from the level start 29–31 for about 2 min, then 10–13 29–31 for about 2 min, then 9–10 29–30 for about 20 s, then 12–21

    This change stages the same elements in Astro Bot as main; the previous condition staged four large elements whose records are 16 bytes or less, and the frame rate fell much earlier. The drop later in the level is the texture cache thrashing (#357), on every build; the automatic texture budget there follows free VRAM, which the previous head's extra shadows reduced. One run each, so the numbers after the drop are noise-level.

  • Hollow Knight: Silksong (PPSA12544) on Linux, RTX 3080 Ti: a fixed spot in game from a save, standing still, APS5_TRACE_STAGING=1, 12 intervals of 10 s. main bf2c63f85 (Silksong needs #6842) and the same main with this PR's three commits: 1,199 presents per 10 s in every interval on both, and the same 4 staged elements (largest 64 KiB). Nothing there qualifies.

  • D2R on the Radeon 780M (integrated): with the discrete-only rule the change does nothing there. The integrated GPU's FPS numbers I had were taken with a non-default graphics mode, so I've left them out.

  • D2R on Windows: the laptop GPU has 4 GB, and D2R thrashes on it with or without staging, so I have no Windows FPS numbers.

  • I did not compare frames image by image.

Shadow memory bound

  • Each staged element gets its own device-local shadow (one VkDeviceMemory), sized to the element. This PR lets a swept write-only element with records wider than 16 bytes take one, up to 256 MiB, and only on a discrete GPU (context.discreteGpu); other GPUs keep main's caps.
  • A shadow is held by the cached build whose element it serves and is released with that build when it leaves the build cache. So at most (cached builds with such an element) × (element size, ≤ 256 MiB) is held at once; there is no separate total cap.
  • When the device refuses a shadow (VRAM or allocation count exhausted), the element binds in place as on main (stagingBuffer returns nullptr, Region::unstaged) and the [buffers] staging line counts it under "shadows refused". No failure.
  • In Diablo II: Resurrected the only element this adds is ~31.25 MiB (160-byte records).

Device memory, Diablo II: Resurrected

RTX 3080 Ti 12 GB, NVIDIA 615.71.09, Ubuntu 26.10, main a19e7f6fd vs this change on it (same code as the current head), Rogue Encampment, default settings, two alternating pairs, 10 s windows at 180 s and 240 s in game, nvidia-smi peak memory.used:

main this PR
peak VRAM in window (total) 8077-8140 MiB 8275-8293 MiB
peak VRAM in window (app.elf) 6471-6509 MiB 6671-6680 MiB
presents / 10 s 55-57 77-85
staged copy-in / copy-back per 10 s ~0.47 GB ~18-21 GB
shadows refused 0 0

So about +150-200 MiB of VRAM for about +40% presents. Whole-run peaks are in the same range on both (main 8486-9035 MiB, this PR 8732-9173 MiB). The copy volume is the full-element copy-in and copy-back on every use; the debt entry describes skipping the copy-in when a sweep is proven to write every byte. No failure lines in any run.

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. Rebased onto main d14a186f4 (a conflict in the debt file only: both sides appended entries); the measurements were taken on main f71a31193 with the same code. That main has #6486 (read and written raw elements); this PR keeps its form of the sweep test. pr_overlap.py listed #6636, #6947 and #7340 for neighbouring lines in ShaderResources.cpp.
  • Fixes: none
  • Unblocks: none (PPSA03912 #5146 is already in game; this is a speedup)
  • GPUs tested: NVIDIA RTX 3080 Ti, driver 615.71.09, Linux; NVIDIA RTX A2000 Laptop GPU, driver 610.60, Windows 11 (tests only); AMD Radeon 780M, RADV Mesa 26.0.8, Linux (tests only)
  • AI-assisted: yes

AI-assisted; the runs are from my machine(s).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4911235792 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7618). Originally posted by [mrfelek](https://github.com/mrfelek) on 2026-10-18T17:30:58Z. --- ### What 2688b7646 stages a written V# element in a device-local buffer when a direct dispatch sweeps it: threads times the record size cover at least half of the element. `main` applies that up to 16 MiB, and only when the shader also reads the element. 6d9222082 (#6486) extended it to raw elements through their access extent, still only for read and written elements. The message of 2688b7646 explains why write-only elements keep the 2 MiB window: a kernel streaming once through a large buffer, like the engine's memcpy over 4–8 MiB video frames, would only gain the two copies. Diablo II: Resurrected (PPSA03912) shows the other side of that. Once per frame it runs a compute dispatch of 144x144 groups of 8x8 threads (1,327,104 threads). Each thread writes into its own 160-byte record with `buffer_store_dwordx4` (8 of them, so at most 128 of the 160 bytes), in a 212 MB write-only element (`0x2a86e47f0+0xca80000`, exactly threads x 160). One store instruction writes 16 bytes per lane, and neighbouring lanes are 160 bytes apart, so in place in the host import the stores are scattered. They take about 56–60 ms of GPU time per frame. Staged, the kernel takes about 3.5 ms, plus about 22 ms of bulk copies in and back. Change, on discrete GPUs only (`Context::discreteGpu`, set from `VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU`): - `ShaderResources::addGuestBuffer`: a write-only element that a direct dispatch sweeps is staged as well when its V# stride is wider than the widest buffer store, 16 bytes (`read || (context.discreteGpu && descriptor.Stride() > WidestBufferStoreBytes)`). With records that wide, the lanes of every store land a record apart, which is what staging speeds up. - The swept cap goes from 16 MiB to 256 MiB (`DiscreteSweptShadowMax`). Integrated GPUs keep today's rule. Their device-local memory is the same system RAM, so staging there gains little and pays for two copies. What stays as it is: - a write-only element whose records are 16 bytes or less: one store can fill a whole record, so the lanes of a store write it contiguously, and it stays in place; - write-only raw elements (V# stride 0) are not swept, so they keep the 2 MiB window; - read and written elements, raw or strided, follow `main`'s rule unchanged, apart from the larger cap on discrete GPUs; - indirect dispatches (unknown thread count) are not swept; - dispatches that touch only part of an element are not swept; - the copy-in is kept for write-only elements, because the copy-back covers the whole element and the stores may leave part of each record unwritten (in D2R at most 128 of each 160-byte record). The debt entry "Swept write-only staging" says when it could go. **Trade-off:** a kernel with records wider than 16 bytes that it writes completely and once now pays two bulk copies instead of one pass of scattered in-place stores. If the memcpy kernel from 2688b7646 binds its frames with a stride wider than 16 bytes, it falls under this; I couldn't find that title in the commit, so I haven't measured it. Records of 16 bytes or less stay in place even when their stores are narrower, because store widths are not recorded per instruction; that is the safe side. **Cost:** - **Memory:** from the code, not measured: every shadow is its own device-local buffer, and a cached resource build keeps it, so D2R's 212 MB shadow stays allocated while that build is cached and a reused build records its copies again. When the device refuses the allocation (device memory or the allocation count exhausted, for example on a card close to its VRAM budget), the element is bound in place as before, `[buffers] staging shadow of N bytes refused` is printed and `stagingRefused` counts it; nothing aborts. - **PCIe:** on discrete GPUs the copies cross the bus. D2R moves 236 MiB in and 236 MiB back per present, about 4 GiB/s at 8.5 presents per second. That is a small share of an x16 link; on an x4 or x8 link (laptop eGPU, bifurcated slot) it is a visible share. I have no such machine to measure it. `APS5_NO_WRITTEN_SHADOW=1` binds these elements in place again. ### Tested **Linux:** Ubuntu 26.10 (kernel 7.3), Ryzen 9 5950X, RTX 3080 Ti (NVIDIA 615.71.09 open), GCC 15.3.0, Release. **Windows:** Windows 11 Pro 26200, i7-11850H, RTX A2000 Laptop GPU (NVIDIA 610.60), WinLibs GCC 15.2.0, Release. **Linux / AMD integrated:** Ubuntu 26.04.1 (kernel 7.0), Ryzen 9 PRO 8945HS, Radeon 780M (RADV, Mesa 26.0.8, 2 GB carve-out), GCC 15.2.0, Release. - **`agc_driver_recorder` `readWrittenStagingTests`:** - The test device sets `discreteGpu` from the selected device's type. - A dispatch sweeping a written-only 4 MiB element of 32-byte records expects staging on a discrete GPU and in place otherwise. - New: a dispatch sweeping a written-only 4 MiB element of 16-byte records, which its stores write contiguously, binds it in place on every GPU. - A dispatch touching 32 KiB of a written-only element binds it in place. - A written-only raw (stride 0) element binds it in place. - A dispatch sweeping a written-only 31.25 MiB element of 160-byte records, and one sweeping a read and written element of that size, are staged on a discrete GPU and bound in place otherwise. The test block is 40 MiB for these cases. I ran the group on its own, through a local `--tmp-staging-only` switch that isn't part of this PR. It passes on the RTX 3080 Ti, on the Radeon 780M (integrated) and on the RTX A2000 under Windows at ab4c1febe, and on the RTX 3080 Ti again after the rebases onto `main` eaa5c9238 and d14a186f4 (this head). On the RTX 3080 Ti I also ran it on `main` f71a31193, on ab4c1febe and on this head with `ANYPS5_REQUIRE_VULKAN=1`, `ANYPS5_NO_SHADER_CACHE=1` and `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation` (layer 1.4.357, loaded): all three pass with no `VUID-` message, so the change adds none. The full `agc_driver_recorder_tests` binary stops earlier, at `(9) a scan under a pending write was kept`, the same way as on `main`, so it's unrelated. - **D2R (PPSA03912) on Linux, RTX 3080 Ti:** in game with a saved character, the game's default settings, no input. `main` f71a31193 and this head on top of it, each relinked with its own relinker. `APS5_PROFILE_GPU=1 APS5_TRACE_SUMMARY_EVERY=10 APS5_TRACE_SECONDS=170:10`, 120 s from 60 s after the character loads, run alternately (`main`, this, this, `main`). Steady intervals from 100 s on: | | `main` f71a31193 | this change (27876656b, same code as this head) | |---|---|---| | Presents per 10 s | 61.8, 61.0 | 86.6, 85.0 | | Frame p50 | 162, 164 ms | 116, 117 ms | | GPU busy per present | 150, 152 ms | 106 ms | | GPU time of the dispatch, per frame | ~55 ms | ~3.4 ms | | `staging-in` / `staging-out` per present | 0.16 / 0.20 ms, 2.4 MiB | 9.8 / 11.8 ms, 236 MiB | The first run of this change reported GPU time only for its first two intervals, so the GPU busy row has the second run only. One more `main` run gave no numbers: it started while the monitors were asleep (`SDL_GetWindowDisplayIndex failed: Couldn't find any displays`). None of the runs of this change failed. The previous head (f959abc09, `Stride() != 0`) gave 79.1 and 81.5 presents per 10 s against 59.5 and 56.7 on `main` in earlier runs; this rule stages the same D2R element. `trace_digest.py` rows (steady part, second run of each): ``` main f71a31193 | 103-113 | 61 | 163.9 | 171.1 | 175.4 | 175.4 | 151.8 | 98 % | Driver.Packet 251.8 | | 113-123 | 61 | 164.7 | 171.3 | 175.8 | 175.8 | 152.1 | 98 % | Driver.Packet 253.5 | this 27876656b | 104-114 | 85 | 118.3 | 122.6 | 126.8 | 126.8 | 106.3 | 98 % | Driver.Packet 170.1 | | 114-124 | 86 | 117.1 | 120.9 | 125.1 | 125.1 | 107.3 | 98 % | Driver.Packet 169.1 | ``` <details><summary>performance_compare.py on the two 10 s trace windows (second run of each)</summary> # Performance comparison | Capture | Commit | Trace | Presents | Window (ms) | | --- | --- | --- | ---: | ---: | | baseline | f71a311935f3 | aps5-trace-f71a311935f3-50227.json.gz | 62 | 10135.718 | | candidate | 27876656bdf9 | aps5-trace-27876656bdf9-48243.json.gz | 85 | 10047.981 | - Compare captures from the same machine, title and scene; summaries do not record the CPU, title or scene. - Changes are candidate minus baseline. A negative frame-time change means a shorter frame interval. - A missing top phase is unreported, not zero; its change is unavailable. - Recorded configuration differs; these measurements do not isolate the code change. - baseline: trace events were dropped; the event trace omits some of the work included in phase totals. - candidate: trace events were dropped; the event trace omits some of the work included in phase totals. ## Configuration differences | Setting | Baseline | Candidate | | --- | --- | --- | | window.first_present | 814 | 1085 | ## Frame and GPU times (ms) | Metric | Baseline | Candidate | Change | Change (%) | | --- | ---: | ---: | ---: | ---: | | frame_ms.p50 | 162.924 | 117.445 | -45.479 | -27.914 | | frame_ms.p90 | 172.193 | 123.167 | -49.026 | -28.472 | | frame_ms.p99 | 178.736 | 131.254 | -47.482 | -26.565 | | frame_ms.max | 178.736 | 131.254 | -47.482 | -26.565 | | mean_frame_ms | 163.479 | 118.212 | -45.268 | -27.690 | | gpu_busy_ms_per_present | 153.323 | 105.947 | -47.376 | -30.899 | ## Top phases (ms per frame) | Metric | Baseline | Candidate | Change | Change (%) | | --- | ---: | ---: | ---: | ---: | | Driver.Draw | 142.854 | 96.904 | -45.950 | -32.166 | | Driver.Draw.draw_and_resource_release | 66.593 | n/a | n/a | n/a | | Driver.Draw.shader_compile_and_link | 55.959 | 56.238 | +0.279 | +0.499 | | Driver.Packet | 250.643 | 169.643 | -81.000 | -32.317 | | Driver.Packet.draw | 143.541 | 97.621 | -45.920 | -31.991 | | Driver.Packet.memory_wait | 90.114 | 54.566 | -35.548 | -39.448 | | Graphics.Draw | 65.706 | 66.725 | +1.019 | +1.551 | | Graphics.Draw.draw_and_resource_release | 55.070 | 56.054 | +0.984 | +1.787 | | Shader.DrawStage | n/a | 48.709 | n/a | n/a | </details> <details><summary>aps5-summary.jsonl lines of the two trace windows above</summary> main f71a31193: ```json {"commit":"f71a311935f3","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"16384","APS5_PAD_INPUT_FILE":"<path>","APS5_PROFILE_GPU":"1","APS5_THREAD_PRIORITY":"high","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"170:10","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 3080 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.3.0-9-generic","resolution":"3840x2160","window":{"spec":"seconds 170:10","first_present":814,"ms":10135.718},"presents":62,"frame_ms":{"p50":162.924,"p90":172.193,"p99":178.736,"max":178.736},"gpu_busy_ms_per_present":153.323,"worker_busy_pct":{"queue worker 0x23":55.278,"queue worker 0x0":98.207},"top_phases_ms_per_frame":{"Driver.Packet":250.643,"Driver.Packet.draw":143.541,"Driver.Draw":142.854,"Driver.Packet.memory_wait":90.114,"Driver.Draw.draw_and_resource_release":66.593,"Graphics.Draw":65.706,"Driver.Draw.shader_compile_and_link":55.959,"Graphics.Draw.draw_and_resource_release":55.070},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":5.886,"events_lost":186347,"totals_full":false}},"trace":"aps5-trace-f71a311935f3-50227.json.gz"} ``` this change (27876656b, same code as this head): ```json {"commit":"27876656bdf9","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"16384","APS5_PAD_INPUT_FILE":"<path>","APS5_PROFILE_GPU":"1","APS5_THREAD_PRIORITY":"high","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"170:10","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 3080 Ti","driver":"NVIDIA 615.71.09","os":"Linux 7.3.0-9-generic","resolution":"3840x2160","window":{"spec":"seconds 170:10","first_present":1085,"ms":10047.981},"presents":85,"frame_ms":{"p50":117.445,"p90":123.167,"p99":131.254,"max":131.254},"gpu_busy_ms_per_present":105.947,"worker_busy_pct":{"queue worker 0x23":46.384,"queue worker 0x0":97.366},"top_phases_ms_per_frame":{"Driver.Packet":169.643,"Driver.Packet.draw":97.621,"Driver.Draw":96.904,"Graphics.Draw":66.725,"Driver.Draw.shader_compile_and_link":56.238,"Graphics.Draw.draw_and_resource_release":56.054,"Driver.Packet.memory_wait":54.566,"Shader.DrawStage":48.709},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":4.265,"events_lost":356234,"totals_full":false}},"trace":"aps5-trace-27876656bdf9-48243.json.gz"} ``` </details> - **Astro Bot (PPSA21564) on Linux, RTX 3080 Ti:** Sky Garden (`underwater_aerial_garden`, actual gameplay), from a progress save, default settings, hands off from the level start (the level flies on its own), `APS5_TRACE_STAGING=1` to list every distinct staged element. One run each, on `main` f71a31193: | | `main` f71a31193 | this change (27876656b) | previous head f959abc09 (`Stride() != 0`) | |---|---|---|---| | Distinct staged elements | 91, largest 10 MiB | 91, largest 10 MiB | 95, four more of 16, 26, 26 and 49 MiB | | `staging-in` per present | about 3.0 ms, 64 MiB | about 3.0 ms, 66 MiB | about 4.5 ms, 97 MiB | | Presents per 10 s from the level start | 29–31 for about 2 min, then 10–13 | 29–31 for about 2 min, then 9–10 | 29–30 for about 20 s, then 12–21 | This change stages the same elements in Astro Bot as `main`; the previous condition staged four large elements whose records are 16 bytes or less, and the frame rate fell much earlier. The drop later in the level is the texture cache thrashing (#357), on every build; the automatic texture budget there follows free VRAM, which the previous head's extra shadows reduced. One run each, so the numbers after the drop are noise-level. - **Hollow Knight: Silksong (PPSA12544) on Linux, RTX 3080 Ti:** a fixed spot in game from a save, standing still, `APS5_TRACE_STAGING=1`, 12 intervals of 10 s. `main` bf2c63f85 (Silksong needs #6842) and the same `main` with this PR's three commits: 1,199 presents per 10 s in every interval on both, and the same 4 staged elements (largest 64 KiB). Nothing there qualifies. - **D2R on the Radeon 780M (integrated):** with the discrete-only rule the change does nothing there. The integrated GPU's FPS numbers I had were taken with a non-default graphics mode, so I've left them out. - **D2R on Windows:** the laptop GPU has 4 GB, and D2R thrashes on it with or without staging, so I have no Windows FPS numbers. - I did not compare frames image by image. ### Shadow memory bound - Each staged element gets its own device-local shadow (one `VkDeviceMemory`), sized to the element. This PR lets a swept write-only element with records wider than 16 bytes take one, up to 256 MiB, and only on a discrete GPU (`context.discreteGpu`); other GPUs keep main's caps. - A shadow is held by the cached build whose element it serves and is released with that build when it leaves the build cache. So at most (cached builds with such an element) × (element size, ≤ 256 MiB) is held at once; there is no separate total cap. - When the device refuses a shadow (VRAM or allocation count exhausted), the element binds in place as on main (`stagingBuffer` returns nullptr, `Region::unstaged`) and the `[buffers] staging` line counts it under "shadows refused". No failure. - In Diablo II: Resurrected the only element this adds is ~31.25 MiB (160-byte records). ### Device memory, Diablo II: Resurrected RTX 3080 Ti 12 GB, NVIDIA 615.71.09, Ubuntu 26.10, main a19e7f6fd vs this change on it (same code as the current head), Rogue Encampment, default settings, two alternating pairs, 10 s windows at 180 s and 240 s in game, `nvidia-smi` peak `memory.used`: | | main | this PR | |---|---|---| | peak VRAM in window (total) | 8077-8140 MiB | 8275-8293 MiB | | peak VRAM in window (app.elf) | 6471-6509 MiB | 6671-6680 MiB | | presents / 10 s | 55-57 | 77-85 | | staged copy-in / copy-back per 10 s | ~0.47 GB | ~18-21 GB | | shadows refused | 0 | 0 | So about +150-200 MiB of VRAM for about +40% presents. Whole-run peaks are in the same range on both (main 8486-9035 MiB, this PR 8732-9173 MiB). The copy volume is the full-element copy-in and copy-back on every use; the debt entry describes skipping the copy-in when a sweep is proven to write every byte. No failure lines in any run. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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. Rebased onto `main` d14a186f4 (a conflict in the debt file only: both sides appended entries); the measurements were taken on `main` f71a31193 with the same code. That `main` has #6486 (read and written raw elements); this PR keeps its form of the sweep test. `pr_overlap.py` listed #6636, #6947 and #7340 for neighbouring lines in `ShaderResources.cpp`. - [x] Fixes: none - [x] Unblocks: none (PPSA03912 #5146 is already in game; this is a speedup) - [x] GPUs tested: NVIDIA RTX 3080 Ti, driver 615.71.09, Linux; NVIDIA RTX A2000 Laptop GPU, driver 610.60, Windows 11 (tests only); AMD Radeon 780M, RADV Mesa 26.0.8, Linux (tests only) - [x] AI-assisted: yes AI-assisted; the runs are from my machine(s).
github-actions[bot] commented 2026-10-18 22:54:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T22:54:34Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6256189413 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7618#issuecomment-6256189413). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T22:54:34Z. --- <!-- pr-labels-perf --> 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`.
blawesom commented 2026-10-19 07:35:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-19T07:35:49Z.


Rebase handoff, with a feature-vs-feature verdict to review — rebased onto current main (all three commits, authorship unchanged). The conflicts are the same shape on two commits: main has since landed its own read/extent-aware addGuestBuffer (the read flag, bufferExtent, the recordBytes/swept computation and the 4-argument test form) — which is the same feature this PR implements, from a different angle. I kept main's forms throughout; your discrete-GPU gating (commit 3) and any behavior main's version lacks are what remains distinct.

So the practical question for you and the maintainers: main's landed version covers the read-and-written and raw-element staging paths this PR adds — worth checking whether your discrete-GPU-only gate (and anything else main's landed form misses) justifies a delta on top, or whether this closes as superseded.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV): builds; conventions pass; full suite green apart from the four known main regressions (#5444 Mesa, #7584 BDA, #7766, #7784), none from this PR.

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/7618
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 7d8e4c7b1.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6262136048 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7618#issuecomment-6262136048). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-19T07:35:49Z. --- **Rebase handoff, with a feature-vs-feature verdict to review** — rebased onto current `main` (all three commits, authorship unchanged). The conflicts are the same shape on two commits: `main` has since landed its own read/extent-aware `addGuestBuffer` (the `read` flag, `bufferExtent`, the `recordBytes`/swept computation and the 4-argument test form) — which is **the same feature this PR implements, from a different angle**. I kept `main`'s forms throughout; your discrete-GPU gating (commit 3) and any behavior `main`'s version lacks are what remains distinct. So the practical question for you and the maintainers: `main`'s landed version covers the read-and-written and raw-element staging paths this PR adds — worth checking whether your discrete-GPU-only gate (and anything else `main`'s landed form misses) justifies a delta on top, or whether this closes as superseded. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): builds; conventions pass; **full suite green apart from the four known `main` regressions** (#5444 Mesa, #7584 BDA, #7766, #7784), none from this PR. **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/7618 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `7d8e4c7b1`.
mrfelek commented 2026-10-19 10:04:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-19T10:04:49Z.


@blawesom thanks for the rebase and for running the suite on the RX 9070 XT. I didn't take it as is, because it dropped the part this PR is about. It isn't superseded by main.

main's addGuestBuffer still requires read:

const bool swept = read && dispatchThreads != 0 && recordBytes != 0 && dispatchThreads * recordBytes * 2u >= byteSize;

Your rebase kept that line, so a write-only element is still never swept. The 256 MiB cap from commit 3 then has nothing to apply to. D2R's 212 MB write-only element stays bound in place, and the dispatch still takes about 56–60 ms per frame. #6486 (now in main) covers raw elements that are read and written. This PR covers strided elements that are only written, on discrete GPUs. The two don't overlap.

I've rebased it myself onto main f71a31193 (head f959abc09). It keeps main's recordBytes form and changes the condition to (read || (context.discreteGpu && descriptor.Stride() != 0)), so write-only raw elements still keep the 2 MiB window. readWrittenStagingTests uses main's four-argument form plus an element size.

At the game's default settings on the RTX 3080 Ti, D2R goes from 59.5 / 56.7 to 79.1 / 81.5 presents per 10 s. The description has the full numbers, the summary lines and the traces.

AI-assisted; the runs are from my machine(s).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6264696418 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7618#issuecomment-6264696418). Originally posted by [mrfelek](https://github.com/mrfelek) on 2026-10-19T10:04:49Z. --- @blawesom thanks for the rebase and for running the suite on the RX 9070 XT. I didn't take it as is, because it dropped the part this PR is about. It isn't superseded by `main`. `main`'s `addGuestBuffer` still requires `read`: ```cpp const bool swept = read && dispatchThreads != 0 && recordBytes != 0 && dispatchThreads * recordBytes * 2u >= byteSize; ``` Your rebase kept that line, so a write-only element is still never swept. The 256 MiB cap from commit 3 then has nothing to apply to. D2R's 212 MB write-only element stays bound in place, and the dispatch still takes about 56–60 ms per frame. #6486 (now in `main`) covers raw elements that are read and written. This PR covers strided elements that are only written, on discrete GPUs. The two don't overlap. I've rebased it myself onto `main` f71a31193 (head f959abc09). It keeps `main`'s `recordBytes` form and changes the condition to `(read || (context.discreteGpu && descriptor.Stride() != 0))`, so write-only raw elements still keep the 2 MiB window. `readWrittenStagingTests` uses `main`'s four-argument form plus an element size. At the game's default settings on the RTX 3080 Ti, D2R goes from 59.5 / 56.7 to 79.1 / 81.5 presents per 10 s. The description has the full numbers, the summary lines and the traces. AI-assisted; the runs are from my machine(s).
mrfelek commented 2026-10-19 10:11:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-19T10:11:53Z.


The two traces of the 10 s windows in the description, for the summary lines and the performance_compare.py output there (open in ui.perfetto.dev):

Both are also in https://gist.github.com/mrfelek/91c0ecb88784089ddd913adcfca8bab2.

AI-assisted; the runs are from my machine(s).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6264819836 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7618#issuecomment-6264819836). Originally posted by [mrfelek](https://github.com/mrfelek) on 2026-10-19T10:11:53Z. --- The two traces of the 10 s windows in the description, for the summary lines and the `performance_compare.py` output there (open in ui.perfetto.dev): - `main` f71a31193, run 3: [aps5-trace-f71a311935f3-3059676.json.gz](https://gist.githubusercontent.com/mrfelek/91c0ecb88784089ddd913adcfca8bab2/raw/f37e21a0c19d96848356bbfef5f31ca42508815c/aps5-trace-f71a311935f3-3059676.json.gz) - this change f959abc09, run 2: [aps5-trace-f959abc099c6-3059169.json.gz](https://gist.githubusercontent.com/mrfelek/91c0ecb88784089ddd913adcfca8bab2/raw/b757052ddf4a7a46a09b2119485a5b917ba27fd1/aps5-trace-f959abc099c6-3059169.json.gz) Both are also in https://gist.github.com/mrfelek/91c0ecb88784089ddd913adcfca8bab2. AI-assisted; the runs are from my machine(s).
CnRJay commented 2026-10-19 17:49:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-19T17:49:12Z.

Review: COMMENTED


Good PR: the cause is isolated (one strided 212 MB write-only element, 56 to 60 ms of GPU time in place against 3.5 ms staged), the result is measured twice on D2R (presents per 10 s 59.5/56.7 to 79.1/81.5, GPU busy 155/162 to 112/109 ms), and the fallbacks (integrated GPUs, raw elements, partial sweeps, indirect dispatches) are tested. The cost side needs more than the description gives it.

  • The staged path costs 11.3 ms in and 13.9 ms out per present, 267 MiB of copies, against 0.2 ms in place. On this card that is a clear win; the PR's own trade-off paragraph says it is a loss for a kernel that writes a large element once and contiguously. The condition descriptor.Stride() != 0 doesn't distinguish those two. The narrower condition the description proposes (the record stride larger than the store width per thread, so the in-place stores are scattered) is the real discriminator; D2R has 160-byte records with 16-byte stores. Please use it now rather than after a regression report, and add the contiguous case (stride equal to the store width) to the test as "bound in place".
  • The copies run over PCIe on a discrete card. 267 MiB per present at about 8 presents per second is about 2 GiB/s in total, trivial on x16 but a visible share of an x4 or x8 link (laptop eGPU, bifurcated slot). The test cards are all x16-class; say what the cap DiscreteSweptShadowMax (256 MiB, up from 16) does on a card that is already near its VRAM budget, since the shadow buffer is allocated for every such dispatch. Is it allocated from the pool and released each frame, or kept?
  • The description keeps the copy-in for write-only elements, because the copy-back covers the whole element. For D2R the records are 160 bytes and stores are 16 bytes, so gaps hold old data and the copy-in is needed. If a later check can prove the sweep writes every byte, the copy-in (11.3 ms here) could go; note that in debt rather than leaving it implicit.
  • A second title that is not D2R and does stage would help (Astro Bot has nothing it stages). If nobody can name the memcpy title, the stride-versus-store-width condition is the safe choice.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5575502764 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7618#pullrequestreview-5575502764). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-19T17:49:12Z. **Review:** COMMENTED --- Good PR: the cause is isolated (one strided 212 MB write-only element, 56 to 60 ms of GPU time in place against 3.5 ms staged), the result is measured twice on D2R (presents per 10 s 59.5/56.7 to 79.1/81.5, GPU busy 155/162 to 112/109 ms), and the fallbacks (integrated GPUs, raw elements, partial sweeps, indirect dispatches) are tested. The cost side needs more than the description gives it. - The staged path costs 11.3 ms in and 13.9 ms out per present, 267 MiB of copies, against 0.2 ms in place. On this card that is a clear win; the PR's own trade-off paragraph says it is a loss for a kernel that writes a large element once and contiguously. The condition `descriptor.Stride() != 0` doesn't distinguish those two. The narrower condition the description proposes (the record stride larger than the store width per thread, so the in-place stores are scattered) is the real discriminator; D2R has 160-byte records with 16-byte stores. Please use it now rather than after a regression report, and add the contiguous case (stride equal to the store width) to the test as "bound in place". - The copies run over PCIe on a discrete card. 267 MiB per present at about 8 presents per second is about 2 GiB/s in total, trivial on x16 but a visible share of an x4 or x8 link (laptop eGPU, bifurcated slot). The test cards are all x16-class; say what the cap `DiscreteSweptShadowMax` (256 MiB, up from 16) does on a card that is already near its VRAM budget, since the shadow buffer is allocated for every such dispatch. Is it allocated from the pool and released each frame, or kept? - The description keeps the copy-in for write-only elements, because the copy-back covers the whole element. For D2R the records are 160 bytes and stores are 16 bytes, so gaps hold old data and the copy-in is needed. If a later check can prove the sweep writes every byte, the copy-in (11.3 ms here) could go; note that in debt rather than leaving it implicit. - A second title that is not D2R and does stage would help (Astro Bot has nothing it stages). If nobody can name the memcpy title, the stride-versus-store-width condition is the safe choice.
mrfelek commented 2026-10-19 20:55:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-19T20:55:36Z.


@CnRJay thanks for the review. All four points are in the new head (ab4c1febe), and the description is updated.

1. The condition. A write-only element is now staged only when its V# stride is wider than 16 bytes, the widest buffer store (descriptor.Stride() > WidestBufferStoreBytes). With records that wide, the lanes of each store land a record apart, so in place the stores are scattered. With 16 bytes or less, one store can fill a whole record and the element stays in place. The recompiler doesn't record store widths per instruction, so this uses the widest possible store instead of the real one. That errs on the side of keeping an element in place. New test case: a written-only element of 16-byte records, swept, "bound in place" on every GPU. The swept-and-staged case now uses 32-byte records, and the partial-sweep case too, so it doesn't pass for the wrong reason.

D2R is unchanged by the narrower rule, since its records are 160 bytes:

D2R, default settings, alternating runs main f71a31193 this head (run on 27876656b, same code)
Presents per 10 s 61.8, 61.0 86.6, 85.0
GPU busy per present 150, 152 ms 106 ms
staging-in / staging-out per present 0.16 / 0.20 ms 9.8 / 11.8 ms (236 MiB each way)

2. Memory and PCIe.

  • From the code: each shadow is its own device-local buffer, kept by the cached resource build that uses it. So it isn't pooled and released each frame; it stays allocated while that build is cached, and each reuse records its copies again. If the allocation is refused, for example near the VRAM budget, the element is bound in place as before: [buffers] staging shadow of N bytes refused and the stagingRefused counter, no abort. I haven't run D2R on a card that is close to its VRAM limit.
  • Bus traffic: about 472 MiB per present (236 MiB each way), about 4 GiB/s at 8.5 presents per second. I have no x4 or x8 machine to measure it on. Both points are in the description's new Cost paragraph, with APS5_NO_WRITTEN_SHADOW=1 as the way back to in place.

3. The copy-in. New debt entry "Swept write-only staging" in docs/dev/debt/libSceAgcDriver.md. It's needed because the stores can leave part of each record unwritten (D2R's thread has 8 buffer_store_dwordx4, at most 128 of its 160 bytes). A sweep proven to cover every byte could drop it, about 10 ms per frame here.

4. A second title. I couldn't name one that stages, so I took the narrower condition, as you suggested.

Tests at this head: the staging group passes on the RTX 3080 Ti (Linux), the Radeon 780M (integrated, Linux) and the RTX A2000 (Windows). Under the Khronos validation layer on the 3080 Ti, it passes on both main and this head with no VUID- message.

AI-assisted; the runs are from my machine(s).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6277126420 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7618#issuecomment-6277126420). Originally posted by [mrfelek](https://github.com/mrfelek) on 2026-10-19T20:55:36Z. --- @CnRJay thanks for the review. All four points are in the new head (ab4c1febe), and the description is updated. **1. The condition.** A write-only element is now staged only when its V# stride is wider than 16 bytes, the widest buffer store (`descriptor.Stride() > WidestBufferStoreBytes`). With records that wide, the lanes of each store land a record apart, so in place the stores are scattered. With 16 bytes or less, one store can fill a whole record and the element stays in place. The recompiler doesn't record store widths per instruction, so this uses the widest possible store instead of the real one. That errs on the side of keeping an element in place. New test case: a written-only element of 16-byte records, swept, "bound in place" on every GPU. The swept-and-staged case now uses 32-byte records, and the partial-sweep case too, so it doesn't pass for the wrong reason. D2R is unchanged by the narrower rule, since its records are 160 bytes: | D2R, default settings, alternating runs | `main` f71a31193 | this head (run on 27876656b, same code) | |---|---|---| | Presents per 10 s | 61.8, 61.0 | 86.6, 85.0 | | GPU busy per present | 150, 152 ms | 106 ms | | `staging-in` / `staging-out` per present | 0.16 / 0.20 ms | 9.8 / 11.8 ms (236 MiB each way) | **2. Memory and PCIe.** - From the code: each shadow is its own device-local buffer, kept by the cached resource build that uses it. So it isn't pooled and released each frame; it stays allocated while that build is cached, and each reuse records its copies again. If the allocation is refused, for example near the VRAM budget, the element is bound in place as before: `[buffers] staging shadow of N bytes refused` and the `stagingRefused` counter, no abort. I haven't run D2R on a card that is close to its VRAM limit. - Bus traffic: about 472 MiB per present (236 MiB each way), about 4 GiB/s at 8.5 presents per second. I have no x4 or x8 machine to measure it on. Both points are in the description's new **Cost** paragraph, with `APS5_NO_WRITTEN_SHADOW=1` as the way back to in place. **3. The copy-in.** New debt entry "Swept write-only staging" in `docs/dev/debt/libSceAgcDriver.md`. It's needed because the stores can leave part of each record unwritten (D2R's thread has 8 `buffer_store_dwordx4`, at most 128 of its 160 bytes). A sweep proven to cover every byte could drop it, about 10 ms per frame here. **4. A second title.** I couldn't name one that stages, so I took the narrower condition, as you suggested. **Tests at this head:** the staging group passes on the RTX 3080 Ti (Linux), the Radeon 780M (integrated, Linux) and the RTX A2000 (Windows). Under the Khronos validation layer on the 3080 Ti, it passes on both `main` and this head with no `VUID-` message. AI-assisted; the runs are from my machine(s).
mrfelek commented 2026-10-19 21:55:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mrfelek on 2026-10-19T21:55:34Z.


@CnRJay a follow-up on point 4: Astro Bot turned out to be a second title the rule matters for, in gameplay this time instead of the galaxy map.

Setup: Linux, RTX 3080 Ti, default settings. Sky Garden (underwater_aerial_garden) from a progress save, hands off from the level start (the level flies on its own). APS5_TRACE_STAGING=1 lists every distinct staged element. One run each, on main f71a31193.

Astro Bot, Sky Garden main this change previous head (Stride() != 0)
Distinct staged elements 91, largest 10 MiB 91, largest 10 MiB 95, four more of 16, 26, 26 and 49 MiB
staging-in per present ~3.0 ms, 64 MiB ~3.0 ms, 66 MiB ~4.5 ms, 97 MiB
Presents per 10 s from the level start 29–31 for ~2 min, then 10–13 29–31 for ~2 min, then 9–10 29–30 for ~20 s, then 12–21
  • The previous condition staged four large elements whose records are 16 bytes or less, and the level slowed down much earlier. The narrower condition stages exactly what main stages.
  • The drop later in the level is the texture cache thrashing (#357) and happens on every build. The automatic texture budget follows free VRAM, which the previous head's extra shadows reduced.
  • Silksong (a fixed spot, standing still): 1,199 presents per 10 s on main bf2c63f85 and with this PR, the same 4 small staged elements. Nothing there qualifies.

I've also rebased onto main eaa5c9238 (a debt-file conflict only, both sides appended entries) and replaced the galaxy-map table in the description with these results.

AI-assisted; the runs are from my machine(s).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6278253960 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7618#issuecomment-6278253960). Originally posted by [mrfelek](https://github.com/mrfelek) on 2026-10-19T21:55:34Z. --- @CnRJay a follow-up on point 4: Astro Bot turned out to be a second title the rule matters for, in gameplay this time instead of the galaxy map. **Setup:** Linux, RTX 3080 Ti, default settings. Sky Garden (`underwater_aerial_garden`) from a progress save, hands off from the level start (the level flies on its own). `APS5_TRACE_STAGING=1` lists every distinct staged element. One run each, on `main` f71a31193. | Astro Bot, Sky Garden | `main` | this change | previous head (`Stride() != 0`) | |---|---|---|---| | Distinct staged elements | 91, largest 10 MiB | 91, largest 10 MiB | 95, four more of 16, 26, 26 and 49 MiB | | `staging-in` per present | ~3.0 ms, 64 MiB | ~3.0 ms, 66 MiB | ~4.5 ms, 97 MiB | | Presents per 10 s from the level start | 29–31 for ~2 min, then 10–13 | 29–31 for ~2 min, then 9–10 | 29–30 for ~20 s, then 12–21 | - The previous condition staged four large elements whose records are 16 bytes or less, and the level slowed down much earlier. The narrower condition stages exactly what `main` stages. - The drop later in the level is the texture cache thrashing (#357) and happens on every build. The automatic texture budget follows free VRAM, which the previous head's extra shadows reduced. - Silksong (a fixed spot, standing still): 1,199 presents per 10 s on `main` bf2c63f85 and with this PR, the same 4 small staged elements. Nothing there qualifies. I've also rebased onto `main` eaa5c9238 (a debt-file conflict only, both sides appended entries) and replaced the galaxy-map table in the description with these results. AI-assisted; the runs are from my machine(s).
adriadam10 commented 2026-10-23 10:29:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-23T10:29:48Z.

Review: CHANGES_REQUESTED


Rechecked against current main (513ca7e55): the PR applies cleanly, context is in scope in addGuestBuffer, and the head's patch is the one already reviewed, so the code is unchanged. The measurements on D2R and Astro Bot look sound.

One point before merge: the swept cap goes from 16 MiB to 256 MiB per written element on discrete GPUs, and the shadow stays allocated while its cached resource build is kept. The description says the memory is "from the code, not measured". Please add:

  • the bound on how many such shadows the cache can hold at once (cached builds × 256 MiB, or whatever limit the cache applies), and what releases a shadow when its build leaves the cache;
  • device-memory use before and after on D2R (peak allocated VRAM over the same 10 s window, from the same machine), so the cost of the larger cap is a number and not an estimate.

Once that is in the description, the rest of the review stands.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5627014159 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7618#pullrequestreview-5627014159). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-23T10:29:48Z. **Review:** CHANGES_REQUESTED --- Rechecked against current `main` (513ca7e55): the PR applies cleanly, `context` is in scope in `addGuestBuffer`, and the head's patch is the one already reviewed, so the code is unchanged. The measurements on D2R and Astro Bot look sound. One point before merge: the swept cap goes from 16 MiB to 256 MiB per written element on discrete GPUs, and the shadow stays allocated while its cached resource build is kept. The description says the memory is "from the code, not measured". Please add: - the bound on how many such shadows the cache can hold at once (cached builds × 256 MiB, or whatever limit the cache applies), and what releases a shadow when its build leaves the cache; - device-memory use before and after on D2R (peak allocated VRAM over the same 10 s window, from the same machine), so the cost of the larger cap is a number and not an estimate. Once that is in the description, the rest of the review stands.
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!7618
No description provided.