Skip to content

GPU: title memory in place instead of copied, opt-in (VICTORY_IN_PLACE=1; stacked on #2088, towards #880) - #2112

Draft
desplmfao wants to merge 36 commits into
Marvisak:mainfrom
desplmfao:perf/loose-windows-in-place
Draft

desplmfao wants to merge 36 commits into
Marvisak:mainfrom
desplmfao:perf/loose-windows-in-place

Conversation

@desplmfao

@desplmfao desplmfao commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #2088, which is itself stacked on #402. The new commits are the last two here. This is a first, opt-in step towards #880 (reading title memory in place instead of copying it). It covers loose windows (memory outside every buffer object) and, since the third commit, title buffer objects.

Change

With VICTORY_IN_PLACE=1, a loose window is the title's memory itself. The allocation holding it is opened as a host heap with ID3D12Device3::OpenExistingHeapFromAddress (under vkd3d-proton that is VK_EXT_external_memory_host), 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: every copy path of Shadow returns early for such a window.

  • Only the committed part of the window around the requested range, cut to 64 KiB (D3D12's placement granularity), is used. A range that isn't inside one committed allocation keeps the copy from GPU: reuse loose windows that hold a range (#402), 256 KiB windows opt-in (VICTORY_LOOSE_WINDOW_KIB) #2088. Opening a heap has to start at the allocation's base; the buffer is placed at the window's offset in it.
  • watch::title_memory_remapped drops in-place windows and heaps over the remapped range, so a later lookup opens the new pages. Lists that still hold an old window keep its pinned pages until they are reset.
  • It's off by default, so nothing changes unless the variable is set.

Measured

RTX 5080, Linux, proton-cachyos 11.0, vkd3d-proton 3.2.0, main + #1849. Grand Theft Auto VI, Series X 1.0.2.1, from start into world loading:

loose windows at the same point of loading host memory
main (4 MiB copies) about 3.4 GB
#2088 (256 KiB copies) about 2.2 GB
this PR, VICTORY_IN_PLACE=1 79 MB (1,448 windows in place, none refused)

No device-removed errors or GPU faults in any of these runs. Before the patch above, a quick probe showed that process memory opens as a heap here for a plain VirtualAlloc, a write-watched one and a view of a section (also a second view of the same section). The GPU wrote to such memory, and the CPU read the result at the right offset.

Released title memory

Most of what goes in place (about 570 of 572 MB here) is private memory the title allocates and frees itself, partly through calls VIctory doesn't see. A heap opened over an allocation that is no longer the same (released, another size, or not all committed) is let go, together with the in-place windows and buffers in it. That's checked both when the heap would be reused and once per frame for all opened heaps. Test: a_heap_over_a_released_allocation_is_let_go. Known gap: an allocation that is released and made again at the same address with the same size can't be told apart. Under vkd3d-proton, the GPU would then read the old, still pinned pages: stale data, not someone else's memory. This is one reason the switch is opt-in.

Not measured / limits

  • Under Wine these runs stop at world entry: they hit my video-memory limit (driver memory, not these windows) or the title's watchdog freeze, the same as without this PR. So there are no in-world frames, no frame-time comparison, and no picture comparison yet.
  • Title buffer objects in place (third commit): a copy starts at the 64 KiB boundary at or below the buffer; views find their bytes by address, so the extra bytes in front are never read. Measured: 3,335 title buffers and 2,117 loose windows in place, none refused, no device errors.
  • Still copied: buffers over many separately mapped 64 KiB views (MEM_MAPPED, each its own allocation). Here that's 8 large buffers, 378 MB. One heap can't span them, and tile mappings into heaps opened over process memory weren't reliable under vkd3d-proton (a probe worked once, then not).
  • Not tested on Windows or on AMD.

Tested

  • Three new tests (the third, for released memory, is above): the_committed_part_of_a_window_holds_the_range_or_is_none (pure CPU, a reservation with a committed middle) and committed_memory_opens_as_a_buffer_in_place (GPU; skipped where the host can't open process memory as a heap).
  • cargo test -p victory-gpu -- --test-threads 1: 136 passed, 14 failed. Plain main fails the same 14 on this machine.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean for these files.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

kxiserino and others added 5 commits November 24, 2026 02:56
…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>
…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>

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu --lib shadow:: — 10 passed, 0 failed
  • cargo clippy -p victory-gpu --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean opt-in VICTORY_IN_PLACE=1 loose title-memory mapping with remap tracking and solid shadow:: test coverage. Looks good.

AI-assisted (Gemini 4 Neon).

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>
@desplmfao desplmfao changed the title GPU: loose title memory in place instead of copied, opt-in (VICTORY_IN_PLACE=1; stacked on #2088, towards #880) GPU: title memory in place instead of copied, opt-in (VICTORY_IN_PLACE=1; stacked on #2088, towards #880) Nov 25, 2026
@desplmfao

desplmfao commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

A note on why this is opt-in and untested in the world: on this machine I currently can't get into the world at all. On the RTX 5080 (16 GB) under Wine, every run fills video memory while the world loads, before the first world frame. That's mostly driver memory that grows while pipelines are created, not these copies, and it's what I'm working on next. So the missing world test is a limit of my setup, not something this PR has been seen to break.

Once the world loads here, I'll:

  • check the picture and frame time with VICTORY_IN_PLACE=1 against without it;
  • cover title memory that is released while an in-place heap is still open over it. Remaps are handled; a plain release isn't yet.
  • then propose making it the default.

Tests on a machine that already reaches the world (Windows or AMD in particular) would help a lot before that.

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>
@RKouchoo

Copy link
Copy Markdown

#2112 did map real game memory on Windows. In my last loading sample, 243 MiB used that path and 1.11 GiB still used copies. That sample took 2.8 seconds per frame.

…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>
@yualk

yualk commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Scoped native Windows result on RTX 5070 Ti 10de:2c05 (610.88) and explicit WARP 1414:008c. The #2112 allocation-base import and COMMON / ALLOW_CROSS_ADAPTER | ALLOW_UNORDERED_ACCESS 128 KiB placement at a 64 KiB offset worked for fully committed private memory, private MEM_WRITE_WATCH, and a fully committed view in an otherwise reserved pagefile section. GetResourceAllocationInfo required exactly 128 KiB/64 KiB. Two different 4,096-byte CPU patterns per class were read and XOR-written by a real root-UAV dispatch. Each fence completed; the CPU saw all exact words and all 258,048 surrounding canary bytes stayed unchanged. For the section, phase 2 wrote through a separate CPU view; both views received the correct GPU result. These were authored stable allocations held until the placed resource and heap died. No guest release, lifetime transplant, title, commit reduction or timing claim.

Separate hold on 1bcef8016a21: find holds BUFFERS and loose holds LOOSE when calling in_place_buffer. On a stale cached extent it calls let_go_in_place synchronously; that function takes both locks. Either caller can reenter its own lock. The current release test calls the helper directly, not these locked callers. This is a source-level finding, not a hung title repro. Consider separating stale detection from eviction/retry outside the caller locks, with a generation comparison on re-entry. Extent polling alone also cannot distinguish a same-address/same-size reuse. Keep the mode off pending ownership tests. AI-assisted; no title files or captures used.

…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>
@desplmfao

Copy link
Copy Markdown
Contributor Author

Update: CI on Windows showed that Windows doesn't release memory that a heap was opened over while that heap exists; Wine releases it at once. The test now accounts for that (1bcef80). For the title this means something too: on Windows, its own VirtualFree of such memory could fail while VIctory holds a heap over it. That's another reason to keep this opt-in until releases are seen and heaps let go first.

@RKouchoo thanks for the Windows sample (243 MiB in place, 1.11 GiB still copied, 2.8 s per frame). Two things would help:

  • the same point of loading without VICTORY_IN_PLACE, so the 2.8 s can be compared;
  • if your log has them, the reasons memory stayed copied. Here on Wine almost all of it went in place, so the share on Windows is much lower than I expected. Is it memory spread over several allocations (mapped views), or partly uncommitted?

@yualk thanks, both very useful. It's good to have the native Windows placements confirmed, and the reentrancy is a real bug: my release test called the helper directly, without the locks find and loose hold. Fixed in 97503f9: in_place_buffer now only drops a stale heap from the cache and queues it, and check_in_place lets go of its windows and buffers at the next frame, outside BUFFERS and LOOSE. The new test a_stale_heap_is_replaced_under_the_callers_locks calls it while holding both locks. Agreed on the rest: extent polling can't tell a same-address, same-size reuse apart, so the mode stays off until ownership is tracked properly (a generation per allocation, as you suggest).

…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>

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Title memory in place instead of copied, opt-in via VICTORY_* env — zero-copy path behind a flag; risky default-off is right, needs Windows validation.

@desplmfao

Copy link
Copy Markdown
Contributor Author

World result, and it's not a gain yet on Linux/NVIDIA. With #2143 (code tables) I can now get into the world, so here's an A/B on the same build, with and without VICTORY_IN_PLACE=1. Grand Theft Auto VI in the world, RTX 5080, proton-cachyos 11.0, with #2153, #2099 and #2071 applied as well:

process memory in the world without with VICTORY_IN_PLACE=1
anonymous 4.9 GB 6.8 GB (+1.9)
GPU-mapped (host copies) 4.1 GB 2.7 GB (−1.4)
memfd (title memory) 4.5 GB 4.0 GB
RSS 13.5 GB 13.6 GB

In-place works as intended: every loose window and title buffer went in place, refused=0 in all six reasons, with no crash or corruption over several minutes in the world and the same frame time. But the copies it saves come back as anonymous memory. With in-place, 5.7 GB of the anonymous memory sits in 140 regions that are 100% resident.

The likely cause: OpenExistingHeapFromAddress has to start at the allocation base and covers the whole committed allocation. Importing that as host memory makes the driver pin every page of it, including committed pages the title never touched, which would otherwise take no memory on Linux. So for sparsely used allocations, opening the heap costs more than the copy it replaces.

Next, on this branch: before going in place, check how much of the allocation is already resident (QueryWorkingSetEx), and open the heap only for densely used allocations. Sparse ones keep the copy. I'll post the A/B again once that's in.

desplmfao and others added 3 commits November 25, 2026 01:51
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>
@desplmfao

Copy link
Copy Markdown
Contributor Author

Update: two changes for the residency problem from my last comment.

1. Density check (0ae1d5c, keyed per allocation in fcfecd1). With QueryWorkingSetEx, the two pools that held most buffers were far from resident:

allocation size pages resident
0xd89c00000 2.3 GB 10%
0xd40800000 1.2 GB 31%

Opening those whole pinned about 2.9 GB that the title had committed but never touched. A whole allocation now goes in place only if at least 90% of its pages are already resident (VICTORY_IN_PLACE_DENSITY changes the threshold). Sparse allocations keep their copies.

Same build, in the world, average of 9 samples per run:

density RSS anonymous GPU-mapped copies memfd in place
off (no in-place) 12.77 GB 5.03 3.95 3.70 none
90 12.84 GB 4.88 3.87 3.98 11,200 buffers, 729 windows
70 — — — — the GPU hung while the world loaded, and the title's watchdog ended the process; none of 8 other runs did

So on Linux this is now no worse than copies (anonymous memory stays where it is without in-place), but the gain is small. The buffers that matter live in the sparse pools. 70% matches what MrEjecutor saw in #880: putting more in place hangs the GPU.

2. Sub-range heaps where the host has them (fcfecd1, c79c031). If the device has ID3D12Device13, a buffer goes in place through OpenExistingHeapFromAddress1 over its own pages only, so nothing else of the allocation becomes resident and no density check is needed. The placed buffer holds its heap, so the heap goes away with the buffer (and with it on a remap). vkd3d-proton has no ID3D12Device13 (only the interface declaration in its IDL), so Linux keeps the whole-allocation path with the density check. That's also what Witredd measured on AMD/Windows in #880: shared memory grew by the whole 2.15 GiB on each heap opened, and they concluded the sub-range variant is needed. I can't run the Windows path here. The new test a_sub_range_heap_covers_only_the_buffer covers it and skips on hosts without ID3D12Device13. A Windows run with VICTORY_IN_PLACE=1 would be very welcome.

Tests on the RTX 5080 under Proton: victory-gpu 139 passed, 13 failed, the same 13 as main here. All in-place tests pass, including the new a_sparse_allocation_is_not_opened_whole. Clippy is clean. (fcfecd1 alone fails clippy over a missing safety comment; c79c031 fixes it.)

desplmfao and others added 2 commits November 25, 2026 02:15
A range in one window, then a range across its end, which gets a window
of its own over both: the title's writes reach both copies at their
refresh, and a shader's write through the spanning copy goes back to
title memory and into the first copy at its next refresh.

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>
desplmfao and others added 3 commits November 25, 2026 20:20
Clean merge: 256 KiB windows only with VICTORY_LOOSE_WINDOW_KIB=256; the
in-place code is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No range: sizes far from the default are the user's own experiment. Only
a value that is not a power of two, or one whose bytes overflow, falls
back to 4 MiB.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 26, 2026
…n place, opt-in)

A plain merge would have dropped Marvisak#2112: the old fork main merged an
earlier Marvisak#2112 head (cd079af), so it is a merge base, and the current
main left Marvisak#2112 out, which reads as a deletion. Merged against the right
base instead: upstream main (67e8ae1) with Marvisak#2088's two commits Marvisak#2112
stacks on (b670a33), and Marvisak#2112's head applied over that file by file
(README, host.rs, memory.rs, shadow.rs, watch.rs; no conflicts). Marvisak#2088's
reverted two-step lookup is not in Marvisak#2112 and stays out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 26, 2026
The previous renderer (old upstream base 940d32f) is the first parent and
is also kept as the branch renderer-2026-11-25-oldbase; the second parent
is the rebuild: fork main (upstream main 63bcca4 + our pull requests) with
the renderer stack merged on it: Marvisak#439 (on main: update/439-main's
resolution), Marvisak#2309, Marvisak#2167, Marvisak#2118, Marvisak#2311, Marvisak#1722, Marvisak#1751, Marvisak#688, Marvisak#1872, Marvisak#2292,
Marvisak#2310, Marvisak#2112 (opt-in), Marvisak#2230 (latch on by default), Marvisak#2389, Marvisak#2088 (small
windows opt-in), Marvisak#2099's new head, and tonight's Marvisak#491 journal fixes.
Merged upstream since: Marvisak#2143, Marvisak#2162, Marvisak#158, Marvisak#1906, Marvisak#1152; superseded by
main: Marvisak#1769, Marvisak#1763, Marvisak#2366.

The picture on this base still shows the overlay that plain upstream main
shows too; that is being looked into.

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

Copy link
Copy Markdown
Contributor Author

@0xKaji thanks, this is the measurement this PR was missing. With 8 GB of video memory and 16 GiB RAM in Series S mode, 15.67 GiB RSS down to 11.25 GiB at world entry, and a 4-minute run without running out of memory, is a real benefit on exactly the kind of machine that needs it. The threshold check helps too: it confirms that the 1 MiB / 90 % defaults are the right side of the trade-off.

Still open before this leaves draft: the skinning artifacts with the switch on (stretched arms, a tilted NPC; see the A/B above). Did you see anything like that on characters in your runs?

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM — reviewed the delta over #2088 (8aef2dd..f83faa7).

The coherence story holds at every seam:

  • in_place no-ops the whole copy pipeline (refresh/gpu_writes/writing/copy_back*/update) except two deliberate paths: keep_in_place retains the placed buffer — hence its heap, hence the pinned pages — for any list whose commands name the GPU address, and landed marks title pages changed so other shadows (sub-floor buffers, view copies, textures) refresh from memory the shaders wrote in place. memory::bind calls landed on computed-address writes even while withholding — the one path that must not be skipped.
  • committed_part: the VirtualQuery walk down/up one AllocationBase with the 64 KiB floor / 4 KiB ceiling cut and lo <= address && end <= hi guard is exact — including the alignment push-up being guarded rather than clamped silently.
  • in_place_buffer: single-allocation verification walks every region (refuses multi-allocation), offset%64KiB enforced, sub-range opens via OpenExistingHeapFromAddress1 keep heap lifetime bound to the placed buffer, whole-allocation path goes through the cache with extent staleness + the dense() working-set gate (64Ki-page sampling, 10 s cache, 90 % default — the GTA6-on-Linux measurement justifies it).
  • Stale-heap sweep: check_in_place once a frame drains the deferred STALE list plus a fresh extent scan; let_go_heap(id) filters by HeapId so a new heap at the same base survives; deferral under STALE_IN_PLACE correctly avoids re-entering BUFFERS/LOOSE from callers that hold them.
  • keep_once's raw-pointer key contract is documented and keep_in_place uses the buffer pointer — no aliasing.
  • The in_place_remapped hook on title_memory_remapped closes the runtime-map/unmap hole; pin_heap (from #2945) closes the title-side free/protect one — the two halves compose correctly.
  • Tests serialize on IN_PLACE_TESTS and cover committed-part geometry, real heap opens, stale eviction, and overlapping windows.

Noting scope: this reviews #2112's own commits; the 256 KiB loose-window lookup rework is #2088's.

@kawaii-shakal

kawaii-shakal commented Nov 26, 2026 •

Copy link
Copy Markdown

A data point from Windows with native D3D12, not vkd3d-proton: RTX 5070 Ti, Windows 11. I merged #2112 with #402 and #2088 on top of current main plus #2870 (copies in GPU upload heaps), and ran it with VICTORY_IN_PLACE=1.

  • Very little went in place. The log shows only refused={"sparse": 1} and refused={"heap refused": 1}, and host copies in system memory went from 6.4 GB to just 4.6 GB.
  • CPU copy time per frame went from 70–90 ms (~420 MB) to ~2.8 s (~1.6 GB), and the frame went from ~1.5 s to ~3.8 s.
  • Cutscenes rendered as a flat white-to-grey screen.

The same build without the variable renders normally. So on Windows/NVIDIA the opt-in currently costs far more than it saves. I'm happy to run a diagnostics build of a newer revision if that helps.

🤖 Generated with Claude Code (Claude Opus 5.5)

@desplmfao

desplmfao commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

@kawaii-shakal thanks, the first native D3D12 numbers for this, and they're clear. With the switch on, copies per frame go from ~420 MB to ~1.6 GB while almost nothing goes in place, and cutscenes go flat. So on a discrete NVIDIA card under Windows the opt-in costs far more than it saves. That matches my Linux A/B (no clear gain at 90 %). The benefit so far has only shown up where system RAM is the limit (0xKaji's 16 GiB RAM machine with an 8 GB card: RSS down from 15.67 to 11.25 GiB) and on shared-memory hosts (#2945). The switch stays off by default, and this PR stays a draft.

Three things would help pin down where the extra 1.2 GB per frame comes from:

  1. Which GPU: title memory in place instead of copied, opt-in (VICTORY_IN_PLACE=1; stacked on #2088, towards #880) #2112 head did you build? Since 2f0590a (GPU: reuse loose windows that hold a range (#402), 256 KiB windows opt-in (VICTORY_LOOSE_WINDOW_KIB) #2088), loose windows are 4 MiB by default again. The 256 KiB ones copy several times the bytes per frame, which looks a lot like your numbers. The current head is ab2b7ef.
  2. The same run without GPU: host copies of title buffers are in video memory by default where the host has GPU upload heaps #2870, if that's easy. In-place buffers are left out of the copy tracking, and I haven't checked how that interacts with copies placed in upload heaps.
  3. From a diagnostics build: the copies per frame lines (copystat) with the switch on and off, and the in place: refusal counts.

The flat cutscenes are probably the same problem as the stretched arms I saw: a buffer the title writes every frame, read in place mid-write. The fix I have in mind keeps a buffer copied until the title has left it unwritten for a few seconds.

@kawaii-shakal

Copy link
Copy Markdown

@desplmfao answers in order:

  1. Head: it was ab2b7ef, so the 4 MiB loose windows were the default and the 256 KiB windows aren't the cause.
  2. Without GPU: host copies of title buffers are in video memory by default where the host has GPU upload heaps #2870: I reran on plain current main merged with ab2b7ef (a fast-forward), with nothing else in the tree: no GPU: host copies of title buffers are in video memory by default where the host has GPU upload heaps #2870, none of the other PRs from my first run. RTX 5070 Ti, Windows 11, native D3D12, diagnostics build, same save, ~3 min each into the world. The cost is the same without GPU: host copies of title buffers are in video memory by default where the host has GPU upload heaps #2870.
  3. copystat and refusals, three consecutive 5 s lines each:
per frame VICTORY_IN_PLACE unset VICTORY_IN_PLACE=1
frame 0.67–2.0 s 6.3–8.3 s
preparing: copies 69–118 ms, 456–529 MB 7.6–10.9 s, 2.3–2.4 GB
refreshes 14,925–15,442, 11.5–12.1 GB 4,950–5,282, 5.1–5.2 GB
watches 13–19 ms, 4.1–5.6 GB checked (3.6–5.1 GB unchanged) 5.7–7.7 s, 11.3–13.2 GB checked (9.0–10.9 GB unchanged)
snapshots 646–957 MB in 72–76 ms 38–53 MB in 1.3–1.8 ms
on the GPU 0.23–1.7 s 0.21–0.24 s
draws / dropped / dropped indirect 2,095–2,717 / 827–1,311 / 420–1,282 1,140–1,222 / 228–269 / 42
host copies of title memory at the end 5,023 (6,032 MB) 6,157 (4,992 MB)

The only in place refusal line in the whole run is title memory kept as a copy, not in place (since the line before) refused={"sparse": 1}.

So almost all of the extra time is in watches: about 2.5× as many bytes checked per frame, at roughly 400× the time. Refreshes and snapshots actually go down. That looks like whatever the in-place buffers still go through in watch (checking or guarding pages of memory that is now read in place) rather than copies proper. Also, with the switch on the frame draws about half as much, which fits the flat cutscenes you suspect come from buffers read mid-write.

🤖 Generated with Claude Code (Claude Opus 5.5)

…saving them or opening guards

landed() used watch::writing, which is for kernel writes into title
memory: every tracker holding a page saved its bytes first, and the
guards were opened, so the pages had to be compared at every refresh
after. In place, every submission marks the whole ranges its shaders may
write, so that was GBs a frame: on Windows with VICTORY_IN_PLACE=1 the
watches took 5.7-7.7 s a frame against 13-19 ms, checking 11-13 GB (9-11
GB unchanged).

watch::gpu_wrote marks the pages (and their aliases) changed in every
tracker, as before, with nothing saved (the write happened; there is no
'before' to keep) and no guard opened (a GPU write does not go through
the CPU's page protection, so the title's writes stay seen).

Found from kawaii-shakal's copystat lines on Marvisak#2112 (RTX 5070 Ti,
Windows 11, native D3D12).

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

Copy link
Copy Markdown
Contributor Author

@kawaii-shakal thanks, the watches line pinned it down. It was this PR, in Shadow::landed. After every submission, a buffer in place marks the whole range its shaders may write as changed, and it did that through watch::writing. That function is meant for kernel writes into title memory: every tracker holding a page saves its bytes first, and the guards are opened, so those pages get compared again at every later refresh. Over whole buffer ranges, every submission, that is the 11–13 GB checked (9–11 GB unchanged) and the 5.7–7.7 s in your table.

29decb3 adds watch::gpu_wrote. It marks the pages, and pages aliased to them, as changed in every tracker, as before. It saves nothing, because the write has already happened and there is no "before" to keep. It opens no guards, because a GPU write doesn't go through the CPU's page protection, so the title's own writes stay seen. shadow:: and watch:: give 40 passed under Wine, including writes_in_place_mark_other_copies_of_the_page_changed, and clippy is clean.

If you can, the same A/B on 29decb3 would show whether the watch time is back near the switch-off level. Your table also shows the switch-on frame drawing about half as much (1,140–1,222 draws against 2,095–2,717). This fix doesn't explain that, and it fits the buffers read mid-write. That part is still open.

desplmfao added a commit to desplmfao/VIctory that referenced this pull request Nov 26, 2026
…without saving or opening guards

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
@MeMako

MeMako commented Nov 26, 2026

Copy link
Copy Markdown

Tested #2112 (29decb3, VICTORY_IN_PLACE=1) on native Windows 11 (10.0.26200) + AMD Radeon RX 9070 XT (RDNA 4, driver 32.0.32029.2010, Ryzen 9 7950X, 32 GB RAM).

Unit Tests

  • cargo clippy -p victory-gpu -p victory --all-targets --locked -- -D warnings: clean.
  • cargo test -p victory-gpu --lib --locked -- --test-threads=1: 287/287 passed, 0 failed (10 ignored benchmarks), including committed_memory_opens_as_a_buffer_in_place, a_sub_range_heap_covers_only_the_buffer, a_heap_over_a_released_allocation_is_let_go, a_list_keeps_the_in_place_buffers_it_uses, writes_in_place_mark_other_copies_of_the_page_changed, and stale_queue_does_not_evict_replacement_heap_at_same_base.

Live Title Run (VICTORY_IN_PLACE=1, default 1 MiB min)

  • Setup: Local integration stack on main 63bcca4 + #2051 + #2472 + #2918 (shadow.rs/watch.rs) + #1010 + #2130 (VRAM upload flags off, shadow.rs) + #3055 (shadow.rs/descriptor) + #2914 (shadow.rs) + #2985 + #2849 (watch.rs) + #3035 (watch.rs) + #2958 (no xmem.rs changes). Comparing VICTORY_IN_PLACE unset (0 crashes across 65 world frames) vs. VICTORY_IN_PLACE=1 on the same tree:
  1. Crash during world load (0xc0000005 in title) preceded by 21× xmem os error 487:
    • Baseline (VICTORY_IN_PLACE unset): 0 xmem remap warnings.
    • With VICTORY_IN_PLACE=1: 21× WARN victory_hle::modules::xmem: mapping physical pages failed (not a placeholder?) address=0x717c10000 len=65536 error=Attempt to access invalid address. (os error 487), followed shortly by a title access violation (0xc0000005).
    • Likely cause: on native Windows D3D12, OpenExistingHeapFromAddress1 pins the virtual pages until the underlying ID3D12Heap and placed ID3D12Resource COM objects are actually destroyed. title_memory_remapped() removes entries from BUFFERS/LOOSE, but either a recorded HostList (keep_in_place) or an in-flight/retired resource still sitting in RETIRED waiting on a GPU fence keeps those COM objects alive when xmem calls UnmapViewOfFile2(..., MEM_PRESERVE_PLACEHOLDER) (whose return value is unchecked), preventing the range from reverting to a placeholder unless the GPU is drained and those objects are dropped first.
  2. Large copy blow-up still occurs on 29decb3:
    • On the two world-entry frames right before the crash, preparing: copies jumped from ~468 MB to 2,295–3,478 MB/frame (local profiler stage timers: deferred 1,455–2,490 ms, flush 2,366–3,840 ms, post-copy-back after covering check_in_place 123–156 ms; total 4,683–7,081 ms/frame). Even without opening guards or saving bytes in watch::gpu_wrote, marking whole in-place written ranges changed still appears to trigger massive re-refreshes on overlapping copied buffers.

Hardware runs were performed by me on my 7950X / RX 9070 XT; comment drafted with Antigravity.

@crashbashash

Copy link
Copy Markdown

A data point from AMD on Linux, where this ends differently than on the 5080 or on Windows: RX 6800 XT, RADV
(Mesa 26.2.6), vkd3d-proton 71cae091b in GE-Proton11-14, this PR merged onto main 219878a plus our Linux
commits, diag build, start from a save in the prison.

head VICTORY_IN_PLACE runs result
ab2b7ef unset 2 into the world, 120 s, normal
ab2b7ef 1 2 device removed about 50 s in, before the world
29decb3 unset 1 into the world, 120 s, normal
29decb3 1 2 device removed about 45 s in, before the world

All four in-place runs end the same way, with no GPU fault and no reset in the kernel log:

vkd3d_import_host_memory: Failed to import host memory, hr 0x8007000e     (1,594 to 2,054 times a run)
radv/amdgpu: The CS has been rejected, see dmesg for more information (-1).
d3d12_command_queue_execute: Failed to submit queue(s), vr -4.
d3d12_device_mark_as_removed: ... VK_ERROR_DEVICE_LOST

The crate's tests pass under Wine at 29decb3, the in-place ones included
(committed_memory_opens_as_a_buffer_in_place, a_list_keeps_the_in_place_buffers_it_uses,
writes_in_place_mark_other_copies_of_the_page_changed): a fresh private allocation imports fine.

So the kernel refuses a submission instead of the GPU faulting. What I think happens, from the sources (I have not
traced the failing BO itself):

  • RADV imports host memory through libdrm's amdgpu_create_bo_from_user_mem, which always asks for
    AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_REGISTER | AMDGPU_GEM_USERPTR_VALIDATE
    (amdgpu_bo.c).
  • With ANONONLY, amdgpu_ttm_tt_get_user_pages returns -EPERM when the range is file-backed or does not lie
    inside a single VMA
    (amdgpu_ttm.c).
    That check runs at import and again when a submission revalidates the pages after they changed.
  • Under Wine, an mprotect of a single page splits the VMA. vkd3d-proton imports the whole allocation (the PR notes
    this), so one page guarded anywhere in it (a buffer under VICTORY_IN_PLACE_MIN_KB that keeps its copy and its
    guard, or another copy's tracker) leaves the imported range across several VMAs. Memory mapped as a section view is
    file-backed under Wine, which ANONONLY refuses outright.

That fits the fresh allocation in the tests importing fine (one anonymous VMA), the failed imports in the game, and a
later submission being refused once a guard or a remap touches an allocation that was imported earlier. NVIDIA's
driver doesn't have this restriction, which fits your runs getting further.

If that's right, on amdgpu an allocation can only go in place if nothing guards any page of it for as long as it's
imported, and it must be private memory (no section views). Happy to test a change on this card.

AI-assisted (Claude Code). No game data attached.

@potatoe0100

Copy link
Copy Markdown

Unit-test run on native Windows with NVIDIA, the combination the description lists as untested: RTX 4060 8 GB (PCIe 4.0 x8, Resizable BAR on), driver 621.14, Windows 11 Pro 26300. Head 29decb3 merged onto main 63bcca4, cargo test -p victory-gpu --lib --locked -- --test-threads=1. No game files.

A cost data point for in place on a discrete card, from #3102's synthetic benchmark on the same 4060 (its in-place path also opens title memory as heaps; comment there). 200 dispatches over 320 MiB of title buffers, ms per submission: copies in system memory 160.8, in place 113-122, copies in video memory 53-106. On a narrow link, shaders reading title memory across the bus cost more than keeping a VRAM copy. That fits #2734 putting hot buffers in VRAM on top of this.

AI-assisted (Claude)

…end on it

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

borosf commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

@desplmfao — facts about the three in-place PRs are in #880 (comment 7212390146). One thing from it worth your attention: #402 is CONFLICTING with main, and #2088 → #2112 sit on top of it, so the chain cannot be judged until it is rebased — 1,210 of the 1,690 lines are in shadow.rs. Nothing else in the comparison contradicts your design: your coverage (loose windows + buffer objects) is disjoint from #2494's (physical-page sections).

@desplmfao

Copy link
Copy Markdown
Contributor Author

Thanks @MeMako, @crashbashash and @potatoe0100. Together these settle where the switch stands:

  • AMD on Windows (RX 9070 XT): a title crash while loading the world, after 21× xmem … os error 487. The heaps opened over title memory still hold the pages when the title remaps them.
  • AMD on Linux (RADV): device lost before the world. The host-memory import fails once our guards have split the allocation across VMAs, so it can't be fixed from this side.
  • Discrete NVIDIA: the tests pass, but reading title memory in place is slower than a copy in video memory (potatoe0100's 113–122 ms against 53–106 ms per submission), and it was far slower in kawaii-shakal's Windows run.

So in place only helps where RAM is the limit (0xKaji's 16 GiB machine) or the GPU shares memory with the CPU (#2945). It stays opt-in and stays a draft, and I'd not use it on AMD or discrete NVIDIA.

@borosf, on the chain: #2088 doesn't wait for #402. It carries #402's commit with current main merged in (update/402-main, the commit itself unchanged), and both #2088 and this PR merge cleanly with main 63bcca4. If #402 is updated upstream, #2088 follows it.

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.