Skip to content

GPU: less copying back and forth: hot title buffers kept in video memory (opt-in, stacked on #2112) - #2734

Draft
sirrus98 wants to merge 24 commits into
Marvisak:mainfrom
sirrus98:perf/up-hot-vram
Draft

sirrus98 wants to merge 24 commits into
Marvisak:mainfrom
sirrus98:perf/up-hot-vram

Conversation

@sirrus98

Copy link
Copy Markdown

Stacked on #2112 (title memory in place, VICTORY_IN_PLACE=1). Everything up to 14d9119 is #2112; the new commits are the last two here. Thanks to #2112 for the groundwork: this builds directly on its buffers in place.

Change

With #2112, a title buffer in place is the title's own pages, and shaders read and write it across the bus. With VICTORY_IN_PLACE_VRAM=1 as well, a buffer in place from 1 MiB up gets a copy in video memory (new crates/victory-gpu/src/resident.rs). Views and the memory page table point at that copy, and the card alone keeps it in step with title memory, inside the submission:

  • Before the submission's lists: the 64 KiB pages the title wrote since they last went up (found by write watching; the runtime's pools are MEM_WRITE_WATCH) are copied by the GPU from the buffer in place into the video-memory copy, and from there into a twin.
  • After the lists: a compute pass of ours compares the copy with its twin over the ranges declared written. It stores each changed dword into the buffer in place and into the twin. A partial dword goes through InterlockedAnd/InterlockedOr, so bytes the CPU wrote meanwhile in the same dword stay. A row is 64 dwords side by side, so the stores cross the bus as runs of whole bytes.
  • Writes through the memory page table: they are only known from its bitmaps after the submission ran. One more list of ours merges them, and it is waited for before the title is told.

Nothing is copied, snapshotted, compared or hashed on the CPU for these buffers. Budget: 60% of the card's (VICTORY_IN_PLACE_VRAM_MB), and none past 80% of the budget in use. Minimum size: VICTORY_IN_PLACE_VRAM_MIN_MB. A log line every 5 s counts the copies and what went up and back. Unset, nothing changes.

The first commit fixes a gap in #2112 that this needs. For a buffer in place, Shadow::gpu_writes returned before the write was noted with the list. So landed() never marked the pages that shaders wrote through root and table views, and other copies over those pages never saw them; only writes through the memory page table were marked. The write is now declared as for any copy. Nothing is snapshotted or copied back for it, since the in-place paths still return early.

This is the "hot memory in video memory" stage of fxceless/victory-fork#772, ported to this tree's synchronous submissions.

Related drafts, none of which writes back from the GPU: #2130 (written buffers in a GPU upload heap plus a CPU mirror), #693 (shadows in DEFAULT video memory), #2494 (SEC_COMMIT in place).

Not measured / limits

Tested

  • New: a_buffer_in_video_memory_takes_the_titles_writes_and_gives_back_the_cards. It runs on the host GPU (verified on the RTX 4090): the title's writes go up, the card's come back, and a CPU byte beside a byte the card wrote in the same dword stays. Also new: pages_go_up_in_runs, ranges_join_where_they_touch, writes_declared_in_place_reach_the_copy_back.
  • a_list_keeps_the_in_place_buffers_it_uses now counts the declared write's reference.
  • Two tests fail (descriptor::host dynamic slots, memory lists share a table). They fail the same on GPU: title memory in place instead of copied, opt-in (VICTORY_IN_PLACE=1; stacked on #2088, towards #880) #2112 without these commits.
  • cargo clippy -D warnings, with and without --all-features: clean. cargo run -p xtask -- policy: ok.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

kxiserino and others added 24 commits November 24, 2026 23:32
…oose windows that hold a range

`shadow::find` looked at only the 64 buffers starting closest below an
address. A big buffer with more than 64 smaller buffers placed in it
before the address was missed, and the range fell through to `loose`,
which made a second 4 MiB host copy of memory the big buffer's copy
already had, not aliased with it. The search now goes back as far as the
longest buffer registered so far (`MAX_SPAN`): one starting further below
cannot hold the range.

`loose` reused a window only for the exact same (start, length), so a
range inside a window made for a range spanning two windows got another
copy. It now reuses any window holding the range. It also gives no copy
for null-page addresses, or for memory that is not committed when a new
window would be made, instead of 4 MiB of zeros.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Memory outside every buffer object gets host copies of the aligned
windows lookups touch. While Grand Theft Auto VI loads its world, about
700 such 4 MiB windows (3.4 GB of host memory) were live; the lookups
touched about 30% of their bytes (in 64 KiB pieces). With 256 KiB
windows, at the same point of loading: 5,713 windows, 2.2 GB, and the
process 15.4 GB instead of 17.3 to 20.4 GB. No descriptor-slot warnings
for the extra windows' page-table entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With VICTORY_IN_PLACE=1, a loose window is the title's memory itself:
the allocation holding it is opened as a host heap
(OpenExistingHeapFromAddress; VK_EXT_external_memory_host under
vkd3d-proton) and a buffer is placed at the window's offset. Nothing is
copied to the host or back, and the window needs no page guards. Only the
committed part of the window around the asked range, cut to 64 KiB, is
used; ranges that are not one committed allocation keep the copy. A
remap of title memory drops the in-place windows and heaps over it, so a
later lookup opens the new pages; lists still holding an old window keep
its (pinned) pages until they are reset.

Off by default. RTX 5080, proton-cachyos 11.0, vkd3d-proton 3.2.0, Grand
Theft Auto VI loading its world: 1,448 windows in place, none refused,
no device errors; loose-window host memory at the same point of loading
79 MB instead of 2.2 GB (256 KiB copies) or 3.4 GB (4 MiB copies).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ETY comments)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A title buffer object's copy is, with VICTORY_IN_PLACE=1, the title's
memory too: placed from the 64 KiB boundary at or below the buffer
(views find their bytes by address), past its end by 256 bytes where the
allocation has them. A placed buffer's end only needs to be committed
pages, not 64 KiB. A remap drops in-place title buffers over it as well.

Buffers over many separately mapped 64 KiB views (MEM_MAPPED, one
allocation each) are still copied: one heap cannot span them, and tile
mappings into heaps opened over process memory were not reliable under
vkd3d-proton here.

Measured (RTX 5080, proton-cachyos 11.0, the title loading its world,
with Marvisak#259, Marvisak#177 and Marvisak#1849 on top for NVIDIA under Wine): 3,335 title
buffers and 2,117 loose windows in place, none refused, no device
errors; 8 large buffers (378 MB) over separate views still copied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The title releases some of its memory itself, which we do not see. A
heap opened over an allocation that is no longer the same (released,
another size, or not all committed) is now let go, with the in-place
windows and title buffers placed in it: when the heap would be reused,
and once a frame for all opened heaps. What this cannot tell is an
allocation released and made again at the same address with the same
size.

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

Windows did not release memory a heap was opened over while the heap
lived (CI: the allocation was still there after VirtualFree); Wine
releases it at once. The test now lets go of the heap in that case and
checks that the release succeeds then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the next frame

find holds BUFFERS and loose holds LOOSE when they call
in_place_buffer, and letting go of what is placed in a stale heap takes
both locks again (found by yualk in review). in_place_buffer now only
drops the stale heap from the cache and queues it; check_in_place lets
go of its windows and buffers at the next frame, outside those locks.
Test: a_stale_heap_is_replaced_under_the_callers_locks.

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

They share the heap cache and the queue of stale heaps, and a parallel
test can get the same address back (CI). The stale-heap test now makes
the cached entry stale itself: Windows does not decommit memory a heap
was opened over while the heap lives, so the decommit did nothing there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Opening a title allocation as a host heap makes all of it resident: the
host memory import pins every page, also committed pages the title never
touched. Grand Theft Auto VI keeps most buffers in two large pools that
were 10% and 31% resident (2.3 and 1.2 GB), so in place cost 1.9 GB more
anonymous memory for 1.4 GB fewer copies.

An allocation now goes in place only if at least 90% of its pages are
already in memory (QueryWorkingSetEx, at most 64Ki pages sampled; asked
again after 10 s; VICTORY_IN_PLACE_DENSITY sets the percentage). Sparse
ones keep their copies. In the world: anonymous memory as without in
place (4.9 GB), 0.3 GB fewer copies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Where the device has ID3D12Device13, a buffer in place is a heap over
its own pages (OpenExistingHeapFromAddress1) instead of the whole
allocation: nothing else of the allocation becomes resident, so no
density check applies, and the placed buffer holds its heap, which goes
with it. vkd3d-proton has no ID3D12Device13 and keeps the whole-
allocation heaps with the density check.

The density decisions are keyed by allocation base and size: an
allocation made again at the same address is a new one. Tests: a sparse
allocation is not opened whole; a sub-range heap covers only its
buffer (where the host has one); the heap-cache tests take the
whole-allocation path.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One run at 70% hung while the world loaded; a later run without in-place
hung the same way, so the hang is not shown to come from the threshold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Shaders reach memory in place over the bus; for small, often-touched
buffers (atomics, constants) that costs more than the copy: sirrus98
measured a once-a-frame compute pass over a tiny buffer in host memory
at 19-67 ms of GPU time instead of 1-6 ms (RTX 4090, for the in-place
work of fxceless/victory-fork#772, which keeps the same 1 MiB floor).
VICTORY_IN_PLACE_MIN_KB sets it. Loose windows are not affected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A heap found stale is queued for the frame's check, and by then a new
heap may be open at the same base with buffers placed in it. Letting go
by address range dropped those too. Each opened heap now has an id; the
queue and the periodic check let go of that heap and of what is placed
in it only. Remaps still let go by address.

Reported by nathanbzrr on Marvisak#2112.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Refreshing a copy also keeps it alive until the list is reset; an
in-place buffer has nothing to refresh, so `refresh` and `gpu_writes`
returned before anything held it. The title releasing the buffer, or a
stale heap being let go, between recording and submission then freed a
resource whose GPU address the list names. Both now keep the placed
buffer once per list (`HostList::keep_once`); a placed resource holds
its heap, so the heap lives as long.

Reported by nathanbzrr on Marvisak#2112.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The argument clamp writes the records where it reads them. For a copy
that is our memory; for a buffer in place it is the title's own, which
the clamp changed under it. In-place records are now copied on the GPU
(after the title's passes that wrote them) into one buffer per list,
clamped and executed there. Copies keep the clamp in place as before.

Reported by nathanbzrr on Marvisak#2112.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A copy-back writes title memory under `watch::writing`, so other copies
of the same pages (title buffers under the in-place floor, a structured
view's own copy, textures) see them changed and copy again. A buffer in
place has nothing to copy back: the shaders' writes land in title memory
unseen by guards and write watch, and those other copies kept their
older bytes. After the submission, the ranges written in place (declared,
or marked through the memory page table, withheld or not) are now marked
changed the same way.

Reported by nathanbzrr on Marvisak#2112.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since stale heaps are let go by id, `a_stale_heap_is_replaced_under_the_
callers_locks` left the heap opened in place of the stale one cached at
its base, and freed the memory. A later test given that address back
(`a_sub_range_heap_covers_only_the_buffer`, run after it) found the
entry and failed. The address-based let-go hid this by also dropping the
new heap, which was the bug. The test now lets go of its memory's heaps
before freeing it, and the sub-range test checks that its call adds
nothing to the cache rather than that the cache has nothing at the base.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Main's indirect clamp (315cea1) now reads the title's records through a
root view and writes the clamped ones into an argument buffer of the
list's own, so it no longer writes title memory: the in-place copy of the
records (indirect::copied, HostList::arguments(len), Shadow::in_place and
their test) is dropped and draw.rs, indirect.rs follow main as is. Copies
sharing a buffer (6cc6aa3): Shadow keeps both main's base/slice and the
in-place flag and heap; new, new_at and new_with all build through one
constructor, and in-place buffers (own buffer, base 0) stay untracked
(track_copy for copies). Shadow::update returns early in place as well as
when clean. keep_once, check_in_place at present and the landed marks
after withheld submissions sit unchanged beside main's drop_withheld.

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

`Shadow::gpu_writes` returned for a buffer in place before the write was
noted with the list, so the copy-back after the submission never reached
`Shadow::copy_back` / `keep_writes` for it, and `landed` never marked the
pages changed: what shaders wrote through root and table views in place
stayed unseen by the other copies holding those pages (only writes through
the memory page table were marked). The write is now declared as for any
copy; nothing is snapshotted or copied back for it (`writing` and
`copy_back` still return early in place), and the list holds the copy until
it is reset, as it does for other copies.

Tests: writes_declared_in_place_reach_the_copy_back (new);
a_list_keeps_the_in_place_buffers_it_uses counts the declared write's
reference. The two failures in descriptor::host and memory tests are the
same on Marvisak#2112 without this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SZC5UAgZQcRDWzY4Tzppah
…k by the card (VICTORY_IN_PLACE_VRAM=1)

With VICTORY_IN_PLACE=1 a title buffer in place is the title's own pages,
which its shaders read and write across the bus. With VICTORY_IN_PLACE_VRAM=1
as well, a buffer in place from 1 MiB gets a "resident" (crate::resident): a
buffer in the card's memory, R, that views and the memory page table name
instead, and a twin B. The card alone keeps R in step with title memory, in
the submission itself:

- before the submission's lists, the 64 KiB pages of the ranges it brings
  up to date that the title wrote since they last went up (write watching;
  the runtime's pools are MEM_WRITE_WATCH) are copied from the buffer in
  place into R, then from R into B;
- after the lists, a compute pass of ours (FXC, cs_5_1, root signature in
  the shader, as the argument clamp) compares R with B over the ranges
  declared written and stores each dword that differs into the buffer in
  place and into B: whole when every byte differs, else by InterlockedAnd and
  InterlockedOr, so bytes the CPU wrote meanwhile in the same dword stay. A
  row is 64 dwords side by side, so the stores into title memory cross the
  bus as runs of whole bytes;
- pages shaders wrote through the memory page table, known from its bitmaps
  only after the submission ran, are merged by one more list of ours, run
  and waited for before the title is told.

Each list of ours runs in an ExecuteCommandLists of its own, before and
after the submission's. Nothing is copied, snapshotted, compared or hashed
on the CPU for these buffers. Where the card writes title memory through
another copy (a buffer in place over the same pages), residents over it take
those pages up again before the next submission. Budget: 60% of the card's
(VICTORY_IN_PLACE_VRAM_MB sets it), none made past 80% of the budget in use,
none over memory that is not write-watched or that another resident holds.
A line every 5 s counts them and what went up and back.

This is the fork's Stage 2 (fxceless/victory-fork#772, "hot memory in VRAM"),
ported to this tree's synchronous submissions on top of Marvisak#2112's buffers in
place.

Test: a_buffer_in_video_memory_takes_the_titles_writes_and_gives_back_the_cards
(on the host GPU: the title's writes go up, the card's go back, a CPU byte
beside a byte the card wrote in the same dword stays); pages_go_up_in_runs,
ranges_join_where_they_touch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SZC5UAgZQcRDWzY4Tzppah
@kotekompotek

Copy link
Copy Markdown

Read the resident.rs design + the watch/shadow integration. The R/B twin-diff approach is coherent: prepare() uploads title-written pages before the submission, MERGE runs after the lists but before the fence signal so host-visible memory is consistent once waited, and the InterlockedAnd/Or partial-dword path correctly preserves CPU bytes in a shared dword (the hard case for any card-side merge). written_beside handles the aliasing leak I was looking for. Opt-in + stacked on #2112 makes the risk acceptable. What reading can't confirm: whether the submit-ordering (our lists before/after in separate ExecuteCommandLists) is airtight against a title list that both reads R and signals early - worth a soak test once #2112 lands.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This pull request conflicts with main (conflicting files: crates/victory-gpu/src/host.rs, crates/victory-gpu/src/shadow.rs). It will be reviewed once it is rebased onto main.


Generated by Claude Code

github-actions Bot pushed a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
…e runtime changes it

A GPU heap opened over title memory (OpenExistingHeapFromAddress1, as
Marvisak#2112 does) locks its pages. While it lives, VirtualProtect,
UnmapViewOfFile2 and VirtualFree fail with ERROR_INVALID_PARAMETER, on
NVIDIA, AMD and WARP alike. The title frees and remaps such memory.

victory_gpu::pinned keeps a registry of held ranges, each with how to let
it go. title_memory_change(at, len, change) releases every holder
overlapping the range, then runs the host call. The system sometimes
unlocks released pages late (RTX 5070 Ti: 32 of 40 within 0.01 ms, the
slowest 5.9 ms), so after a release a call refused with that error is
tried again for up to 100 ms.

The changes go through it:
- the title's VirtualFree, VirtualFreeEx, VirtualProtect, VirtualProtectEx,
  UnmapViewOfFile, UnmapViewOfFileEx and UnmapViewOfFile2 (new HLE module
  memoryapi; everything else in those DLLs stays the host's);
- xmem's unmapping of physical pages;
- XMemFreeDefault's release of 32-bit memory.

Nothing registers a pin on main yet, so the cost is one atomic load per
call. Holders (Marvisak#2112, Marvisak#2734) register when they open a heap.
@potatoe0100

Copy link
Copy Markdown

Hardware run on a second NVIDIA card: RTX 4060 8 GB (Ada, PCIe 4.0 x8, Resizable BAR on), driver 621.14, Windows 11 Pro 26300. I tested at the PR head 4d28d6d as it is (its base is 67e8ae1), because it conflicts with current main. cargo test -p victory-gpu --lib --locked -- --test-threads=1. No game files.

For context on why this stage matters on narrow-link cards: on the same 4060, #3102's synthetic benchmark puts title memory in place at 113-122 ms per submission, against 53-106 ms with copies in video memory (numbers). I'm happy to re-run this once it's rebased onto main.

AI-assisted (Claude)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants