perf(vk): sparse VRAM mirrors, memory only where a game pool is (RX_VK_MIRROR_SPARSE=1) #2794
Open
github-sync
wants to merge 9 commits from
github/pr/2794 into main
pull from: github/pr/2794
merge into: MagicBeanDev:main
MagicBeanDev:main
MagicBeanDev:github/pr/2893
MagicBeanDev:github/pr/2890
MagicBeanDev:github/pr/2887
MagicBeanDev:github/pr/2878
MagicBeanDev:github/pr/2875
MagicBeanDev:github/pr/2874
MagicBeanDev:github/pr/2873
MagicBeanDev:github/pr/2871
MagicBeanDev:github/pr/2756
MagicBeanDev:github/pr/2897
MagicBeanDev:github/pr/2896
MagicBeanDev:github/pr/2718
MagicBeanDev:github/pr/2842
MagicBeanDev:github/pr/2691
MagicBeanDev:github/pr/2895
MagicBeanDev:github/pr/2743
MagicBeanDev:github/pr/2765
MagicBeanDev:github/pr/2708
MagicBeanDev:github/pr/2732
MagicBeanDev:github/pr/2755
MagicBeanDev:github/pr/2817
MagicBeanDev:github/pr/2876
MagicBeanDev:release-please--branches--main
MagicBeanDev:release-please--branches--main--release-notes
MagicBeanDev:github/pr/2778
MagicBeanDev:github/pr/2888
MagicBeanDev:github/pr/2866
MagicBeanDev:github/pr/2877
MagicBeanDev:github/pr/2800
MagicBeanDev:github/pr/2829
MagicBeanDev:github/pr/2850
MagicBeanDev:github/pr/2889
MagicBeanDev:github/pr/2882
MagicBeanDev:github/pr/2860
MagicBeanDev:github/pr/2849
MagicBeanDev:github/pr/2886
MagicBeanDev:github/pr/2840
MagicBeanDev:github/pr/2830
MagicBeanDev:github/pr/2834
MagicBeanDev:github/pr/2719
MagicBeanDev:github/pr/2742
MagicBeanDev:github/pr/2741
MagicBeanDev:github/pr/2820
MagicBeanDev:github/pr/2771
MagicBeanDev:github/pr/2859
MagicBeanDev:github/pr/2858
MagicBeanDev:github/pr/2823
MagicBeanDev:github/pr/2757
MagicBeanDev:github/pr/2769
MagicBeanDev:github/pr/2844
MagicBeanDev:github/pr/2843
MagicBeanDev:github/pr/2673
MagicBeanDev:github/pr/2855
MagicBeanDev:github/pr/2739
MagicBeanDev:github/pr/2807
MagicBeanDev:github/pr/2851
MagicBeanDev:github/pr/2789
MagicBeanDev:github/pr/2814
MagicBeanDev:github/pr/2818
MagicBeanDev:github/pr/2774
MagicBeanDev:github/pr/2772
MagicBeanDev:github/pr/2750
MagicBeanDev:github/pr/2786
MagicBeanDev:github/pr/2785
MagicBeanDev:github/pr/2782
MagicBeanDev:github/pr/2779
MagicBeanDev:github/pr/2775
MagicBeanDev:github/pr/2706
MagicBeanDev:github/pr/2766
MagicBeanDev:github/pr/2747
MagicBeanDev:github/pr/2759
MagicBeanDev:github/pr/2745
MagicBeanDev:github/pr/2740
MagicBeanDev:github/pr/2733
MagicBeanDev:github/pr/2725
MagicBeanDev:github/pr/2727
MagicBeanDev:github/pr/2728
MagicBeanDev:github/pr/2729
MagicBeanDev:github/pr/2721
MagicBeanDev:github/pr/2722
MagicBeanDev:github/pr/2715
MagicBeanDev:github/pr/2714
MagicBeanDev:github/pr/2707
MagicBeanDev:github/pr/2703
MagicBeanDev:github/pr/2705
MagicBeanDev:github/pr/2697
MagicBeanDev:github/pr/2679
MagicBeanDev:github/pr/2676
MagicBeanDev:github/pr/2669
MagicBeanDev:docs/what-hookshot-connects-to
MagicBeanDev:fix/hdr-encode-test
MagicBeanDev:fix/main-tests
MagicBeanDev:fix/play-cache-files
MagicBeanDev:perf/game-barrier-order
MagicBeanDev:feat/heap-lazy
MagicBeanDev:feat/texture-detail
MagicBeanDev:perf/astc-opaque-bc1
MagicBeanDev:fix/fs-dir-lock
MagicBeanDev:fix/framegen-native-kept
MagicBeanDev:fix/update-no-downgrade
MagicBeanDev:port/k10-gcn-f16-mirrors
MagicBeanDev:fix/framegen-mult-cap
MagicBeanDev:chore/release-as-2.1.1
MagicBeanDev:perf/pcache-load-switch
MagicBeanDev:fix/prepare-progress-log
MagicBeanDev:fix/update-closes-stale-game
MagicBeanDev:fix/hdr-scaled-sampled
MagicBeanDev:perf/first-start-preset
MagicBeanDev:perf/zero-upload
MagicBeanDev:fix/vcore-escape-2s
MagicBeanDev:test/fake-gh-atomic
MagicBeanDev:perf/batch-idle-flush
MagicBeanDev:test/finish-stub-a
MagicBeanDev:chore/record-2.1.0
MagicBeanDev:fix/main-red-tests
MagicBeanDev:fix/test-display-startup-driver-cache
MagicBeanDev:feat/mesh-shader-mode
MagicBeanDev:fix/menu-probes-at-startup
MagicBeanDev:fix/fps-counter-classic
MagicBeanDev:fix/test-log-batch-flake
MagicBeanDev:fix/narrow-screen-hud
MagicBeanDev:ci/prereleases
MagicBeanDev:release/v2.0.15
MagicBeanDev:hotfix/v2.0.15
MagicBeanDev:fix/present-ahead-replay
MagicBeanDev:release/v2.0.13
MagicBeanDev:fix/bracket-paths-2013
MagicBeanDev:fix/hdr-overlays-2013
MagicBeanDev:fix/fps-counter-scale-2013
MagicBeanDev:fix/stick-seen-at-rest
MagicBeanDev:hotfix/menu-fits-screen
MagicBeanDev:fix/f1-always-opens-menu
MagicBeanDev:fix/ini-keep-lines
MagicBeanDev:claude/cranky-pike-7cf247
MagicBeanDev:perf/texture-answer-flat
MagicBeanDev:fix/stamp-test-modules
MagicBeanDev:fix/appimage-fonts
MagicBeanDev:fix/display-awake
MagicBeanDev:release/v2.0.12
MagicBeanDev:fix/discord-windows
MagicBeanDev:rc/v2.0.12-tests
MagicBeanDev:fix/dlss-keep-feature
MagicBeanDev:rc/v2.0.12
MagicBeanDev:button-icons
No reviewers
Labels
Clear labels
accessibility
Barrier affecting people with disabilities
autorelease: pending
autorelease: tagged
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
duplicate
This issue or pull request already exists
enhancement
New feature or request
github_actions
Pull requests that update GitHub Actions code
good first issue
Good for newcomers
help wanted
Extra attention is needed
invalid
This doesn't seem right
question
Further information is requested
testing wanted
wontfix
This will not be worked on
No labels
accessibility
autorelease: pending
autorelease: tagged
bug
dependencies
documentation
duplicate
enhancement
github_actions
good first issue
help wanted
invalid
question
testing wanted
wontfix
Milestone
Clear milestone
No items
No milestone
Assignees
Clear assignees
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Hookshot!2794
Loading…
Reference in a new issue
No description provided.
Delete branch "github/pr/2794"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
On cards short of video memory the buffer pools' VRAM mirrors hold hundreds of MB that no game pool is in. This makes them sparse, opt-in. On a 12 GB card held to a 6 GB card's allowance (
RX_VK_TEST_ALLOWED_MB, not a real 6 GB card), the field walk went from 47.7-51.5 fps to 55.3-58.2 fps over three pairs. Held to 8 GB, and at 12 GB, there's no fps change, but the VRAM peak is lower. Loads are no slower.Cause. A pool's mirror covers its import's whole range, and imports are made by chunk around what was bound (
import_chunk). A temporary measuring patch counted, per 64 KB block of the live mirrors, whether anyguest_poolsentry was in it. Of 1.1-1.6 GB of mirrors, 243-533 MB had none (DLAA 2560x1440: Hyrule Field, Kokiri, and up to 533 MB after Castle Town to the field): chunk padding, and pools the game had let go. On an 8 GB card's allowance (6,900 MB) the field walk peaked at 6,952 MB, with 321 frames over 33 ms in five minutes.Change (
RX_VK_MIRROR_SPARSE=1; off by default, and only with strict sparse residency for buffers):gpu/vk/sparse_mirror.h: which blocks a game pool is in, block spans, runs, and copies clipped to blocks with memory or cut by ranges.gpu/vk/vk_vma.cpp: a sparse buffer, and its blocks' memory from VMA (counted in its budget).vmaFreeMemoryjoins the recorder's drain boundaries (vk_recorder.h,vk_work.h).mirror_make: a sparse buffer, with memory for the blocks a game pool is in. The page table points blocks with none at guest memory, so the shaders' pointers reach the game's own memory there, as for a pool with no mirror.gpu_buffer_bind: a binding that reaches a block with none is served from guest memory for that submission. It's noted as an in-place read, or, if written, as a direct write with its shadow forgotten, and the block is wanted.guest_pools_gen), get memory in one batchedvkQueueBindSparse.RX_VK_SPARSE_WAIT=1waits on the CPU, as before.Measured. RTX 4070 SUPER 12 GB (617.42), Windows 11, DLAA 2560x1440, unattended, the same build with and without the switch:
RX_VK_TEST_ALLOWED_MB=6144,RX_VK_MIRROR_MB=1536), field walk, three pairs: fpsRX_VK_TEST_ALLOWED_MB=6900), field walk: fps / VRAM peakRX_VK_SPARSE_WAIT=1)Screenshots of Castle Town, Kokiri and the field look the same on and off. No device was lost and no write-back warning appeared in any run. The rows are in
docs/tested-configurations.md.Two independent code reviews of earlier versions found ten problems in all, each fixed as described above:
Tests.
tests/test_sparse_mirror.pyruns the header: live blocks, an earlier long pool, partial blocks, spans, runs, clipping across both sides' block ends, and cut ranges. It also checks the wiring in vk_memory.cpp, vk_vma.cpp and vk_replay.cpp. The fixtures oftest_direct_buffer_writes,test_parallel_init_order,test_draw_stream_visibilityandtest_fsr_image_retirementget the helpers they now reach.test_readback_partcounts the three new reads of guest memory, each noted. 102 checks read these files. 95 pass; the other 7 fail the same way on plain main on this PC (C tools it lacks).Overlap with open PRs. It merges cleanly with #2711 and #2760. With #2618 (a mirror that found no VRAM asked for again) it conflicts in the same lines of
mirror_make(where a whole mirror's buffer is made): no conflict in what they do, since a sparse mirror that cannot be filled falls back to that same whole buffer, where #2618's retry would apply. Whichever lands second takes a small merge, which I'll do if it's this one. #1377, #2338 and #2609 conflict with main already.Not tested. AMD (RADV and Windows), Intel, Linux; a real 6 or 8 GB card; a long session.
Part of #993.
🤖 Generated with Claude Code
I merged this into current main and ran tests/run_all.py. test_draw_stream_visibility and test_fsr_image_retirement fail here exactly as they do in CI, and they fail because of this change. Both cut submit_recording (and queue_flush_locked, in the fsr one) out of vk_replay.cpp and compile them alone. Those functions now call memory_sparse_wait, which their fixtures don't declare ("use of undeclared identifier 'memory_sparse_wait'"). They need the same
bool memory_sparse_wait(VkSemaphoreSubmitInfo*) { return false; }stub you gave parallel_init_order_test.cpp. test_sparse_mirror passes.It's off by default and gated on sparseResidencyBuffer, residencyNonResidentStrict and a sparse-binding queue, so other tiers aren't exposed. Only the three recording submits wait on the binds' timeline. A sentence on why no other submission (presents, upscalers, texture uploads) can read a newly bound block would help. The 6 and 8 GB rows come from a 12 GB card with a lowered allowance, not real 6/8 GB cards. Your Not tested section says so, but the summary reads as if they were real. Conflicts: #2618 in mirror_make, and #2795 only in docs.
Thanks. You're right on both failures. On this PC those two checks were failing on a local compiler wrapper (a
_CRT_SECURE_NO_WARNINGSredefinition under-Werror), and I had filed them with the ones that fail on main here, which hid the real failure. Fixed in65fb4b14: both fixtures get thememory_sparse_waitstub. With the wrapper fixed, both fail without the stub and pass with it, and of the 102 checks that read the changed files, the only failures left are 7 that fail the same way on plain main (C tools this PC lacks).On the other submissions: every read or write of a mirror is recorded in a recording (bindings, write-backs, the captures' copies, texture uploads, the page table's updates), and those go out only through the three submits that wait. The rest of what reaches the queue is:
None of them binds a pool's mirror. I've added that to the description, and made the summary say the 6 and 8 GB rows are a 12 GB card held to those allowances.
Both of my points are fixed.
65fb4b14gives tests/draw_stream_visibility_test.cpp and fsr_image_retirement_test.cpp the memory_sparse_wait stub. The description now says why only the three recording submits need to wait: nothing else that reaches the queue binds a pool's mirror. It also says the 6 and 8 GB rows come from a 12 GB card held to those allowances.It conflicts with main again, since #2825 (mirror trim) landed after your last merge. There are two conflicts:
Please also check one interaction. #2825 remakes a mostly-unused import's mirror smaller while VRAM is over 80% full. A sparse mirror already holds memory only where a game pool is, so trimming it gains nothing and could fight sparse_release. Skipping p->sparse pools in the trim's candidate check looks like the simplest fix, with a test case.
Once that merge is in and CI is green, I'd approve. It's off by default and gated on strict sparse residency.
Merged main again (#2825, then f1d0a679's shadow remake and #2760). Both conflicts were as you described: both
trims_settle()calls are kept, with the sparse block before the early return. #2825 also added a per-pooldead_since, which clashed with this PR's per-block one, so that isblock_dead_sincenow.On skipping sparse pools in the trim: I looked at it, and I'd rather keep the trim on for them.
sparse_release: that only walksimported, and a trimmed pool is retired out of it before the release looks. By the time the trim considers a pool (1,800 submissions mostly outside every pool), the release (600) has already given its dead blocks back, so the two never act on the same blocks.shadow_make,p.hi - p.lo) and its import both cover the whole range, and sparse leaves both as they are. Trimming shrinks them; skipping would keep a full-size shadow in VRAM on the 8 GB cards the trim is for.If you'd still rather skip them, it's one condition in the candidate loop plus a test case, and I'll add it.