Skip to content

d3d12_xs: slot 133 commits the buffer's xmem pages before the host-pointer import - #9745

Closed
Raavve wants to merge 27 commits into
ProjectLeonida:mainfrom
Raavve:frame-commit-before-import
Closed

Raavve wants to merge 27 commits into
ProjectLeonida:mainfrom
Raavve:frame-commit-before-import

Conversation

@Raavve

@Raavve Raavve commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Requested by Diego · project thread

Claimed in the coordination issue as: new D3D item, #9065 (comment) (the #7556 / #7218 interaction @tibzejoker suggested making its own finding: #7218 (comment))

Depends on: #7218, #7556

Stacked: this branch is #7218's head 3644c72a5 with #7556 merged in. Only the last commit is this PR's: d3d12_xs: slot 133 commits the buffer's xmem pages before the host-pointer import. Draft until #7218 and #7556 land; I'll rebase it then.

What and why

With #7556, xmem commits physical pages on first touch. So when the title places a buffer through device slot 133 on pages it has mapped but not written yet, the range is still MEM_RESERVE. #7218's import refuses reserved pages and answers with its fallback buffer, which the title's writes never reach. On retail this is the title's main 182,452,224-byte slot-133 buffer. Without #7556 every page is committed up front instead, and the process reaches 24 GB of commit before the first frame.

  • d3d12_xs_device.h/.cpp: set_placed_at_va_pages_hook(bool (*)(const void* va, size_t bytes)), registered like d3d12_xs: device slot 133 (CreatePlacedResourceX) makes a buffer on the title's own pages (VK_EXT_external_memory_host) #7218's set_placed_at_va_handler. Slot 133 calls it for a buffer with [va, va + Width) before the handler. Textures don't call it.
  • If the hook fails, that's logged once and the handler still runs, so the result is the same fallback as before.
  • main.cpp: gta6rt registers rt::xmem::commit_physical_range (from xmem: commit physical pages on first touch, not when they are allocated #7556, unused until now). It already skips memory xmem doesn't own. The Vulkan layer doesn't link xmem.
  • xs_proxy_mock_test: the hook gets the buffer's range before the handler. A failing hook still reaches the handler, and a texture doesn't call the hook.
  • New d3d12_xmem_placed_test: a slot-133 buffer on xmem physical pages that are mapped but never touched. Without the hook the pages stay reserved and the fallback answers. With commit_physical_range the range is committed, the GPU VA is the window, and a GPU copy reads the CPU's writes. Skipped without VK_EXT_external_memory_host.

Separate from @Etr0p's #9166 (ReadFile commits first-touch pages before kernel writes) and #9045 (re-copying a fallback shadow buffer), and from RT-753 (the single-address-space V# item).

Tested

Windows 11, RTX 4060 8 GB, NVIDIA 610.88, MSVC RelWithDebInfo + Ninja.

  • Full build OK. ctest -R "xs_proxy|xmem|existing_heap|device_proxy|pe_loader": 13/13 pass (xmem 210 checks, 0 failures).
  • d3d12_xmem_placed_test passes, also with LEONIDA_VK_VALIDATION=1 (0 validation errors).

Retail, own 1.0.2.1 copy, 180 s cap, aggregate counts only. I ran it on my local boot stack, since #7218 + #7556 alone stop right after load: #7218 + #7556 + #8458 + #7290 + #7286 and the loader PRs from my #7218 test (#7218 (comment)), plus this commit.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

Pipodi and others added 27 commits November 23, 2026 16:30
…e PC vtable refuse with E_NOTIMPL

The retail title calls device slot 133, past the 128-entry table; the call read an empty entry and jumped to 0.
Every slot beyond the PC device's vtable now reaches the generic thunk. Mock test covers slot 133.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ory (VK_EXT_external_memory_host)

The retail executable opens the 64 KiB graphics region it has just committed with XMemVirtualAlloc
through device slot 48 right after its first queue and fence; the layer answered E_NOTIMPL and the
title asserted.

The call now answers as the host runtime does on an RTX 4090 and WARP (six cases measured in
d3d12_existing_heap_test): the base of a VirtualAlloc allocation committed read-write over its whole
size opens a heap of that size, 64 KiB aligned, CUSTOM / WRITE_BACK / L0, SHARED |
SHARED_CROSS_ADAPTER | ALLOW_SHADER_ATOMICS; an inner address, a partial or reserve-only commit
and a read-only range are E_INVALIDARG. The backing is the range itself, imported with
VK_EXT_external_memory_host, which the device and the test device enable when present.
CheckFeatureSupport(EXISTING_HEAPS) follows the extension. Placed resources in such a heap are
refused (E_NOTIMPL, logged) until the placed path addresses memory outside the arena.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… an uncached one is refused

Measured on the RTX 4090 and WARP: PAGE_READWRITE | PAGE_WRITECOMBINE is accepted with
CPUPageProperty WRITE_COMBINE, PAGE_READWRITE | PAGE_NOCACHE is E_INVALIDARG. Two new cases in
d3d12_existing_heap_test; 8 of 8 match both host devices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he title's own pages

Xbox device slot 133 is (va, desc, state, clear, riid, out): the title places a buffer at the VA of a
physical-page range it has just mapped (docs/retail_boot_chain.md 3.4, ItsNotRexed#6915). The proxy now hands it to
a handler the Vulkan layer registers (set_placed_at_va_handler), which imports [va, va + width) with
VK_EXT_external_memory_host and binds a VkBuffer to it: the CPU's writes there are the GPU's reads with no
copy, and the resource's console VA is the title's VA, so views and immediate writes the title names by
that VA resolve to it through the buffer VA index. The Xbox flag bits above the PC ones are dropped and
the layout made ROW_MAJOR, as slot 76 does; textures are E_NOTIMPL, a misaligned VA E_INVALIDARG, and
with no handler (a PC device) the slot refuses with E_NOTIMPL as ItsNotRexed#6953's table does.

Not decided here: a shader that adds heap_base to the title's VA (its own V#s) does not reach these
pages; that needs the single address space of docs/d3d12_vulkan.md 3.3.

d3d12_existing_heap_test: on a VirtualAlloc range and on a view of a pagefile-backed section (the form
xmem's physical pages take), the buffer's GPU VA is the address and a GPU copy to a READBACK buffer reads
a pattern the CPU wrote after creation; Vulkan validation clean on an RTX 4090.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ler registered by every Vulkan device

The PC flag mask was a D3D12_RESOURCE_FLAGS constant past the SDK's last enumerator, which clang-cl with
SDK 10.0.26100 refuses as a constant expression; it is an integer now, cast after the AND. The slot-133
handler was registered in create_vulkan_device only, so the standalone device gta6rt --run creates never
served the slot; the Device constructor registers it now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	runtime/shims/d3d12_xs_device.cpp
… found in review)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… not import the title's pages

RADV under Wine refuses to import a view of a pagefile-backed section (E_OUTOFMEMORY on an RX 9070 XT,
found in review), which is the form xmem's physical pages take, so the retail boot stopped at the first
174 MiB placement. An import failure other than a misaligned VA now gives an ordinary DEFAULT buffer of
the same description, as ItsNotRexed#6915's stand-in did, logged once: the boot goes on, and the title's writes at
that VA don't reach it on such a driver.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…commit re-added it, found in review)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ge check, immediate free, proxy test

- A write-combined (or uncached) page in the range is not imported: an RTX 5090 imported such a range
  and read wrong bytes back (found in review). It takes the ordinary-buffer fallback.
- The whole range must be committed, walked with VirtualQuery across allocations (the retail buffer
  spans three mapped windows); otherwise E_INVALIDARG instead of undefined behaviour in Vulkan.
- The imported memory is freed as soon as the submitted work has retired (wait_retire_ticket), not
  after the next submission: VK_EXT_external_memory_host needs the caller's pages to outlive it, and a
  title may free them right after releasing the resource.
- The PC flag mask is 0x5ff: 0x200, which the host refuses, is dropped. Slot 133 is listed in
  device_slot_overridden.
- xs_proxy_mock_test registers a handler and checks slot 133's argument positions, the flag mask
  (0x20204 -> 0x4), the ROW_MAJOR rewrite and the iid; d3d12_existing_heap_test checks the
  write-combined fallback and the partly committed refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ich varies from run to run

The host runtime answers 64 KiB on some runs and 4 MiB on others for the same allocation (RX 9070 XT,
RTX 5090, WARP; found in review), which made the test fail about 1 run in 40. The layer's Alignment
must still be 64 KiB for an opened heap, and the host's either of the two values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
XMemAllocatePhysicalPages backed each call with one SEC_COMMIT section, so
the title's ~6.1 GiB page-pool request after boot stop 17 was charged in
full at once and fails with ERROR_COMMITMENT_LIMIT on a 16 GB machine.
Mapping the pages right away (as the title does) would charge most of it
within seconds too, so the commit now follows first access.

The section is SEC_RESERVE. A vectored handler commits the faulting 64 KiB
page through a reserve-only view of the whole section when the address
lies in a live mapping, so every view of that page sees it. Faults on
committed pages whose protection forbids the access, and faults outside
the mapped pages, go on to the next handler, and a failed commit is not
swallowed. rt::xmem::commit_physical_range commits a range up front for
shims that hand guest memory to the kernel.

Reported by controldebils and rafaelpssz in ItsNotRexed#7333.
A writer that copies into physical pages through its own view of their
section (a DirectStorage destination given as page numbers, ItsNotRexed#7652) sees
reserve-only memory for pages the title has not touched yet, and no
fault handler can resolve that address. commit_physical_pages commits
the given pages through each section's own view, one VirtualAlloc per
contiguous run, so such a writer can commit before it copies. Raised by
nurtrino in ItsNotRexed#7599.
…s at the offset; uncommitted pages fall back

The title places small buffers inside a page too: a 16-byte UAV at a 64-byte-aligned address on an
RTX 5090 (found in review), which the import refused with E_INVALIDARG, halting the boot. The import now
starts at the page holding the address and the buffer is bound at the offset; an offset off the
buffer's alignment takes the ordinary-buffer fallback. A range not fully committed (xmem commits on
first touch) also falls back instead of failing. Only a null address or a zero width are E_INVALIDARG.

d3d12_existing_heap_test: a buffer 64 bytes into a page reads back the CPU's writes at its own VA; a
1-byte offset and a partly committed range fall back.

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

unmap_page moved the record out of g_mapped before taking the exclusive
lock, so a first-touch fault on that page could read a moved-from
section, and map_page made the view live before recording it, so a
fault in between was not recognised. unmap_page now holds the exclusive
lock from the lookup to the erase, and map_page holds it across mapping
the view and recording it. xmem_test adds four threads writing into a
window while 256 slices of pages are mapped and unmapped under them.
Reported by tupacfan04 in the review of ItsNotRexed#7556.
…ad of E_NOTIMPL

The retail title places textures at slot 133 too, and asserted on the E_NOTIMPL (found in review). A
texture now gets an ordinary committed DEFAULT texture of the same description, as slot 76 gives one,
logged once; it is not on the title's pages. The proxy rewrites a texture's Xbox layout word to
UNKNOWN (a buffer's to ROW_MAJOR), as slot 76 does.

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

The handler queried the protection at the start of the 64 KiB page, so
a write to a 4 KiB part the title had made read-only looked allowed and
was resumed, faulting again forever instead of reaching the title's own
handler. It now looks the record up by the page and the protection up
by the faulting address. xmem_test writes to a read-only 4 KiB part of
a read-write page and expects the fault.
Wine can report a requested write-combined allocation as ordinary read/write memory. Check the observed protection and retain the fallback expectation when a cache modifier is present; otherwise verify the read/write import. Add PAGE_NOCACHE to keep the unsafe-protection guard covered under Wine.

Claim: ItsNotRexed#7913 (comment)

Depends on: ItsNotRexed#7218

AI-assisted: Codex
The commit-on-touch checks expect hardware faults from plain loads and
stores inside __try. MSVC catches those as it is; clang-cl only with
asynchronous exceptions, so without /EHa the expected fault escaped and
xmem_test crashed. Same option as tiny_dll. Reported by AirKyzzZ in ItsNotRexed#7556.
…e starts in the matching state

The title's third word at slot 133 is a D3D12_BARRIER_LAYOUT, not a resource state (measured by
@dracbat on a retail boot: 0x6 SHADER_RESOURCE for its RT/DS textures, 0x3 UNORDERED_ACCESS for its
RT+UAV ones, 0xFFFFFFFF UNDEFINED for the 182 MiB buffer), as PC CreatePlacedResource2 takes it. The
proxy passes it to the handler, and a texture's ordinary committed fallback starts in the equivalent
legacy state (UNDEFINED and unlisted values give COMMON; a state the description refuses retries with
COMMON, so the title always gets its resource). Buffers are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… (RADV userptr is anonymous-only)

RADV imports host memory through amdgpu userptr, which libdrm creates anonymous-only, so it refuses a
view of a pagefile-backed section with EPERM (RX 9060 XT under Wine, found in review), and the layer
answers with its fallback as designed. The section-view case now accepts that outcome and checks the
fallback resource exists; NVIDIA and AMD's Windows driver still import it, and the readback is still
checked there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hides the function's pages (C4456)

Found in review under MSVC /W4: the loop's local 'pages' shadowed the function's own. Renamed to
cached_pages; no behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… arena's VA window takes the fallback

From review (@FRaccie):
- The texture fallback called create_committed, which takes the state without checking it (textures start
  undefined and take their first command's layout), so the barrier-layout state and its COMMON retry did
  nothing. It now goes through create_committed_validated, as CreateCommittedResource does: RENDER_TARGET on a
  texture without ALLOW_RENDER_TARGET is refused and the COMMON retry answers; a description the checks refuse
  outright is refused.
- An imported buffer's VA is its host address, in the same BufferVaIndex as default-heap buffers, whose VAs are
  offsets in [0, arena size). A host address inside that window (low addresses, as under Wine) would shadow arena
  buffers in the nearest-start lookup, so such a range is not imported and the fallback answers.

d3d12_existing_heap_test checks the two texture cases.

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

xmem commits physical pages on first touch (ItsNotRexed#7556), so a range the title has
mapped but not written yet is still MEM_RESERVE when it places a buffer on it
through device slot 133, and the Vulkan layer's import (ItsNotRexed#7218) refuses reserved
pages and answers with its fallback buffer, which the title's writes never reach.

Slot 133 now calls a registered pages hook (set_placed_at_va_pages_hook, next to
set_placed_at_va_handler) with the buffer's range before the handler; gta6rt
registers rt::xmem::commit_physical_range. A failed commit is logged once and the
handler still runs. Textures don't call it. The Vulkan layer doesn't link xmem.

Tests: xs_proxy_mock_test checks the hook order, arguments, failure and texture
cases; the new d3d12_xmem_placed_test places a buffer on untouched xmem physical
pages and checks the fallback without the hook, and with it the import and a GPU
copy reading the CPU's writes.

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

@Grimkaaah Grimkaaah 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.

Independent review (Linux CPU) — PASS (code-only; XS/Vulkan host import not run)

Checked

  • Slot 133 CreatePlacedResourceX / host-pointer import via VK_EXT_external_memory_host; commits xmem pages before import; destructor waits retire before free (lifetime note in comments).
  • Large but focused on xmem + xs device + existing-heap / xmem / xs_proxy tests + CMake.

Not executed here: Vulkan external-memory-host / Xbox ABI device tests on this reviewer.

Overlap: touches d3d12_vulkan_device.cpp / CMake like other open d3d12 PRs — expect rebase churn.

No game files. Reviewer: Grimkeh (AI-assisted Cursor / Composer).

@ItsNotRexed

Copy link
Copy Markdown
Member

Closing: pull requests on Leonida are now limited to ProjectLeonida members. Thanks for the contribution.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants