Repository navigation
perf(agc): stage written-only buffer elements a dispatch sweeps on discrete GPUs - #7618
Conversation
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
This pull request is labeled |
|
Rebase handoff, with a feature-vs-feature verdict to review — rebased onto current So the practical question for you and the maintainers: Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, To pick it up: Head is |
272b4c7 to
f959abc
Compare
|
@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
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 I've rebased it myself onto 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). |
|
The two traces of the 10 s windows in the description, for the summary lines and the
Both are also in https://gist.github.com/mrfelek/91c0ecb88784089ddd913adcfca8bab2. AI-assisted; the runs are from my machine(s). |
CnRJay
left a comment
There was a problem hiding this comment.
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() != 0doesn'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.
|
@CnRJay thanks for the review. All four points are in the new head (ab4c1fe), 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 ( D2R is unchanged by the narrower rule, since its records are 160 bytes:
2. Memory and PCIe.
3. The copy-in. New debt entry "Swept write-only staging" in 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 AI-assisted; the runs are from my machine(s). |
ab4c1fe to
9d8a96b
Compare
|
@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 (
I've also rebased onto AI-assisted; the runs are from my machine(s). |
adriadam10
left a comment
There was a problem hiding this comment.
Rechecked against current main (513ca7e): 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.
On a discrete GPU a write-only V# element whose stride is wider than the widest buffer store (16 bytes) and that a direct dispatch sweeps is staged in a device-local shadow of up to 256 MiB, as read and written swept elements are. Its stores no longer cross the bus into the host import. AI-assisted. Tested by me on my own machine.
c12e8e8 to
a293f79
Compare
|
@adriadam10 both points are in the description now, under "Shadow memory bound" and "Device memory, Diablo II: Resurrected":
I also rebased onto main 2dc8e45. #8336 had landed in the same lines, so the change now sits on its AI-assisted. Tested by me on my own machine. |
|
Rechecked the new head: the logic is the one already reviewed, now on top of #8336's |
…revert A/B AI-assisted. Tested by me on my own machine.
…revert A/B AI-assisted. Tested by me on my own machine.
…revert A/B AI-assisted. Tested by me on my own machine.
…scussion AI-assisted. Tested by me on my own machine.
…scussion AI-assisted. Tested by me on my own machine.
Staging a swept write-only element (boykopovar#7618) costs a copy-in and a copy-back per use. In PPSA03912's town that saves GPU time, but in its main menu the 202 MiB element is faster in place (73 vs 91 ms of GPU time per frame), so the menu lost about a fifth of its frames. Decide per element by measurement. The busiest element is probed in its current mode, the other mode and its current mode again; the other mode is kept when its frames are faster by more than twice the standard error. A repeated answer doubles the wait for the next probe from 20 s up to 160 s, and a jump of the frame GPU time by more than a quarter (a scene change) probes again at once. The frame GPU time comes from the whole-batch timestamps, which the first probe turns on. Resource builds made under the other decision are not reused. APS5_NO_ADAPTIVE_STAGING=1 stages every eligible element, as before. The policy is in AdaptiveStaging.hpp, fed by the caller with frame counts and time; agc_adaptive_staging covers keeping the faster mode, frame noise, the back-off cap, a scene change and the share threshold. AI-assisted. Tested by me on my own machine.
Main's #7618 adds its swept write-only staging entry at the end of the debt file, where this branch adds its partially committed 3D textures entry; the merge keeps both.
What
2688b76 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.
mainapplies that up to 16 MiB, and only when the shader also reads the element. 6d92220 (#6486) extended it to raw elements through their access extent, still only for read and written elements. The message of 2688b76 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 fromVK_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.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:
main's rule unchanged, apart from the larger cap on discrete GPUs;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 2688b76 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:
[buffers] staging shadow of N bytes refusedis printed andstagingRefusedcounts it; nothing aborts.APS5_NO_WRITTEN_SHADOW=1binds 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_recorderreadWrittenStagingTests:discreteGpufrom the selected device's type.I ran the group on its own, through a local
--tmp-staging-onlyswitch 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 ab4c1fe, and on the RTX 3080 Ti again after the rebases ontomaineaa5c92 and d14a186 (this head). On the RTX 3080 Ti I also ran it onmainf71a311, on ab4c1fe and on this head withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(layer 1.4.357, loaded): all three pass with noVUID-message, so the change adds none. The fullagc_driver_recorder_testsbinary stops earlier, at(9) a scan under a pending write was kept, the same way as onmain, so it's unrelated.D2R (PPSA03912) on Linux, RTX 3080 Ti: in game with a saved character, the game's default settings, no input.
mainf71a311 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:mainf71a311staging-in/staging-outper presentThe 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
mainrun 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 (f959abc,Stride() != 0) gave 79.1 and 81.5 presents per 10 s against 59.5 and 56.7 onmainin earlier runs; this rule stages the same D2R element.trace_digest.pyrows (steady part, second run of each):performance_compare.py on the two 10 s trace windows (second run of each)
Performance comparison
Configuration differences
Frame and GPU times (ms)
Top phases (ms per frame)
aps5-summary.jsonl lines of the two trace windows above
main f71a311:
{"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=1to list every distinct staged element. One run each, onmainf71a311:mainf71a311Stride() != 0)staging-inper presentThis 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 (Astro Bot (PPSA21564) #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.mainbf2c63f (Silksong needs fix(libc): give the scripting heap hooks blocks from libc's own heap #6842) and the samemainwith 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
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.stagingBufferreturns nullptr,Region::unstaged) and the[buffers] stagingline counts it under "shadows refused". No failure.Device memory, Diablo II: Resurrected
RTX 3080 Ti 12 GB, NVIDIA 615.71.09, Ubuntu 26.10, main a19e7f6 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-smipeakmemory.used: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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)maind14a186 (a conflict in the debt file only: both sides appended entries); the measurements were taken onmainf71a311 with the same code. Thatmainhas perf(agc): stage raw buffer elements a dispatch sweeps #6486 (read and written raw elements); this PR keeps its form of the sweep test.pr_overlap.pylisted perf(agc): reuse address-based draw builds whose inputs are unchanged #6636, perf(agc): GPU descriptor heap (APS5_GPU_HEAP) #6947 and perf(agc): draws that only store to a buffer continue one render pass #7340 for neighbouring lines inShaderResources.cpp.AI-assisted; the runs are from my machine(s).