Skip to content

d3d12_xs: committed buffers live on the memory the title's XMem hook gives (slots 27, 53, 76, 127) - #7411

Open
garuspl wants to merge 25 commits into
ItsNotRexed:mainfrom
garuspl:xmem-hook-backing
Open

garuspl wants to merge 25 commits into
ItsNotRexed:mainfrom
garuspl:xmem-hook-backing

Conversation

@garuspl

@garuspl garuspl commented Nov 23, 2026

Copy link
Copy Markdown

Claimed in the coordination issue as: #7231 (comment) (the open item in #7231 (comment))

Depends on: #7235 (slot 127 through the XMem hook, itself on #7021 and #7052) and #7218 (the slot-133 create_buffer_at_host_address handler, on #6953). This branch is #7235's head with #7218 merged in; only the last commit, 9335d6b14, is this PR. Overlaps #7240, which takes stop 17 another way.

What and why

Since #7235, a committed resource takes its memory through the title's __XMemAllocationHooks__ hook, but only for the accounting: the PC/Vulkan resource isn't backed by that memory, so anything the title writes or reads at that address misses the resource. This PR backs the resource with that memory where the layer can:

  • Buffers whose memory the hook gives are made on that memory through the slot-133 handler (create_buffer_at_host_address, which imports it with VK_EXT_external_memory_host). The buffer's GPU VA is the hook's address, and the title's CPU reads and writes there are the buffer's bytes, with no copy. The memory is zeroed first unless the heap flags say D3D12_HEAP_FLAG_CREATE_NOT_ZEROED, as a PC committed resource starts, and it goes back through the hook's free with the resource.
  • Textures stay on the device's memory, and the hook memory is bookkeeping as before. The console's texture tiling isn't reproduced by any Vulkan image on imported host memory, so the title's CPU view of those bytes couldn't match the image anyway. The reason is in docs/retail_boot_chain.md §3.5.
  • Fallbacks: a buffer the handler can't make (a PC D3D12 device behind the proxy, no extension, an address off the import alignment) and a shared heap keep the device's memory, with the hook memory as bookkeeping. The hook is asked once per resource, never twice. If the PC device then refuses, the memory goes straight back.
  • Device slots 27 and 53 (CreateCommittedResource, CreateCommittedResource1) were forwarded verbatim and never called the hook. They now take the same path as 76 and 127. Their arguments reach the PC device unchanged, and a capability check (no output) or an argument error still gets the PC runtime's own answer, with no hook call.
  • LEONIDA_XMEM_RESOURCE_BACKING=0 keeps every committed resource on the device's memory (bookkeeping only), to compare runs. GPU reads of host memory cross the bus, so a heavily read DEFAULT buffer is slower there.

The attribute word is #7235's kXmemCommittedResourceAttributes, measured at stop 17's call sites. Whether the console uses the same word for slots 27/53/76 isn't measured. Not run against the retail executable.

Tested

Windows 11, MSVC 14.44, NVIDIA GeForce RTX 4090, driver 617.42, Vulkan SDK 1.4.363:

  • xs_proxy_mock_test: PASS. The new check_committed_buffers_on_xmem covers: a buffer on the hook memory (zeroed, PC device not called, freed with the resource), CREATE_NOT_ZEROED, a texture, a shared heap, the handler refusing (one hook allocation, used as bookkeeping), handler and PC device both refusing (memory freed, null object), the hook refusing, slots 27/53 (arguments and session passed through, a capability check takes no memory), and slot 127. d3d12_xs: device slot 127 takes the resource's memory through the title's XMem hook (retail boot chain stop 17) #7235's own checks still pass.
  • d3d12_existing_heap_test with LEONIDA_VK_VALIDATION=1: PASS, 0 validation errors. A new check_committed_on_hook puts the Xbox device proxy over the Vulkan device, with a bump-allocator hook over a VirtualAlloc range. The buffer's GPU VA is the hook's address, a GPU copy reads what the CPU wrote there, and the CPU reads what a GPU copy wrote. A texture only takes its size from the hook and leaves its bytes alone, and both go back through the hook's free.
  • Full ctest: 253 of 255 pass, plus 12 skipped or disabled (they need game files). The 2 failures, d3d12_vulkan_device and d3d12_indirect_dispatch_count, fail the same way on this branch's base without this commit. d3d12_vulkan_device is TEST-1649 in Coordinating who works on what (42) (moved to #7333) #7231.
  • The tests/test_docs_*.py checks pass. scripts/check_no_game_data.py passes on the changed files.

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. (No translator change.)

🤖 Generated with Claude Code

Pipodi and others added 17 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>
…ttedResource3 with Xbox descriptors, RT-5 stop 15)

Slot 127 is past the PC device vtable and was refused with E_NOTIMPL. It has the argument list of ID3D12Device10::CreateCommittedResource3; the proxy now calls the PC device's with the Xbox layout word and flag bits cleaned as slot 76 does. Measured in ItsNotRexed#6982.
…nment, refuses null heap properties; tests pin heap flags, session and reference counts

Review of ItsNotRexed#7052: dispatch and release through the ID3D12Device10 interface pointer (not the base device's vtable); keep every PC-valid resource flag bit, 0x400 included; null heap properties return E_POINTER; the test now passes non-zero heap flags and a protected session, checks the single release returns 0 for texture and buffer, and covers a distinct Device10 pointer.
…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>
…allocation hooks for descriptor heaps)
…le's XMem hook (retail boot chain stop 17)

The console D3D runtime allocates a committed resource's memory with XMemAlloc, so a title's
__XMemAllocationHooks__ see it. Around its first full-screen render targets the title installs a hook
that bump-allocates allocations with page-size field 2 from a fixed arena and then asserts that little
of the arena is left unused; with no call reaching the hook, nothing is counted and the assert fires.

After the PC device creates the resource, slot 127 calls the installed hook (the one ItsNotRexed#7021 bridges for
descriptor heaps) with the PC allocation size and the D3D allocator ID, page-size field 2, and attaches
the memory to the resource as private data, so it goes back through the hook's free when the resource
dies. The PC resource is not backed by it.

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
# 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>
…gives (slots 27, 53, 76, 127)

The memory a committed resource took through the title's XMem allocation hook was bookkeeping only: the
PC/Vulkan resource was not backed by it, so what the title read or wrote at that address missed the resource.

- A committed buffer whose memory the hook gives is now made on that memory through the slot-133 placed-at-VA
  handler (create_buffer_at_host_address, VK_EXT_external_memory_host). Its GPU VA is the hook's address, and
  the title's CPU reads and writes there are the buffer's bytes. The memory is zeroed first unless the heap
  flags say CREATE_NOT_ZEROED, and it goes back through the hook's free with the resource.
- A texture, a shared heap, or a buffer the handler cannot make (a PC device behind the proxy, no extension,
  an address off the import alignment) keeps the device's own memory, with the hook memory as bookkeeping.
  The hook is asked once per resource either way.
- Device slots 27 (CreateCommittedResource) and 53 (CreateCommittedResource1) were forwarded verbatim; they now
  take their memory through the hook like slots 76 and 127. Their arguments reach the PC device unchanged, and a
  capability check (no output) or an argument error is the PC runtime's answer.
- LEONIDA_XMEM_RESOURCE_BACKING=0 keeps every committed resource on the device's memory, as before.

Tests: xs_proxy_mock_test covers every path on the mock device; d3d12_existing_heap_test checks, through the
proxy over the Vulkan device, that the buffer's GPU VA is the hook's address and that CPU and GPU writes are
seen both ways. docs/retail_boot_chain.md section 3.5 records the model.

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

Copy link
Copy Markdown

Independent Windows + GPU test of 9335d6b14: pass.

Setup: Windows 11 x64, MSVC 19.44.35220 (RelWithDebInfo, Windows SDK 10.0.26100.0), NVIDIA GeForce RTX 3080 (driver 32.0.16.2124 / 621.24), Vulkan SDK 1.4.363.1, no game files.

  • xs_proxy_mock_test.exe: PASS (exit 0).
  • d3d12_existing_heap_test.exe: PASS (exit 0) — confirms vulkan committed buffer on the XMem hook (98,304 bytes, GPU VA matches hook address, CPU->GPU and GPU->CPU copies round-trip, textures bookkeeping-only) plus 8 OpenExistingHeapFromAddress cases match against both host RTX 3080 and host WARP.

AI-assisted review/test.

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

Copy link
Copy Markdown
Contributor

Live on Wine 11.22 + RTX 5070 Ti with #7411 9335d6b14 (which carries #7235): the stop-17 assert no longer fires. The two slot-127 targets are leased through the title's hook (33,423,360 bytes each), and the run goes on to stop 18. One build note: #7411's merge of #7218 still has constexpr auto kPcResourceFlags = static_cast<D3D12_RESOURCE_FLAGS>(0x7ff) in the slot-133 path, which clang-cl rejects. #7218's own head already uses std::uint32_t, so re-merging #7218's head fixes it.

Environment: CachyOS, Wine 11.22, RTX 5070 Ti / NVIDIA 620.73.04, Vulkan 1.4.357, clang-cl 23.1.5 + xwin SDK 10.0.26100, Release, LEONIDA_D3D12=vulkan, network blocked, own 1.0.2.1 copy. Boot stack: main 86a7807 + #7411, #7110, #7076, #7066, #6954, #6582, #4348, #7216, #4313, #7499, #7516 (PR heads merged, conflicts resolved locally). AI-assisted (Claude Code).

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

Copy link
Copy Markdown
Contributor

Integration note (AI-assisted, no game files): this design and #7240 don't have to be either/or. I ported this PR's last commit (9335d6b14) onto #7240's lease instead of #7235 in a local integration branch:

Result on AMD Radeon RX 7800 XT (RDNA3), Windows 11, MSVC 2022: builds clean; your d3d12_existing_heap_test additions pass unchanged (vulkan committed buffer on the XMem hook: 98304 bytes at the hook's address: GPU VA is that address, a GPU copy reads the CPU's writes and the CPU reads the GPU's), and the proxy/mock/registry/allocator subset is 19/19 including #7240's check_committed_resource_leases. Your xs_proxy_mock_test additions were not ported (they assume #7235's mock).

So landing #7240 first and rebasing this PR onto it (dropping #7235) looks like a small step rather than a rewrite.

@justrealyme

Copy link
Copy Markdown

Tested 9335d6b on NVIDIA GeForce RTX 5070 Ti, driver 617.42, Windows 11 build 26300, VS 2022 Build Tools (MSVC 19.44), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, RelWithDebInfo: pass.

Built device_proxy_test, xs_proxy_mock_test and d3d12_existing_heap_test and ran ctest -R "^device_proxy|^xs_proxy_mock|^d3d12_existing_heap": 9 of 9 Passed (device_proxy, device_proxy_trace, the six xs_proxy_mock entries, d3d12_existing_heap).

Synthetic tests only, no game files. AI-assisted (Claude Code).

venetanji and others added 2 commits November 23, 2026 19:03
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
…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>
@trick-ye

Copy link
Copy Markdown

Tested 9335d6b14 merged onto main dde82f27e on AMD Radeon RX 7800 XT (RDNA3), AMD 26.11.2 (Windows driver 32.0.32029.2010), Windows 11 26200: no recorded test status changed against main; d3d12_existing_heap, device_proxy and xs_proxy_mock pass.

  • The three tests this PR extends (d3d12_existing_heap, device_proxy, xs_proxy_mock) pass. device_proxy runs on WARP and xs_proxy_mock on mock objects, so neither touches the Radeon.
  • The host-pointer paths of d3d12_existing_heap ran on this card: it exposes VK_EXT_external_memory_host (minImportedHostPointerAlignment 0x1000), and a direct run of d3d12_existing_heap_test exits 0 and prints vulkan import: existing heap opened on the caller's memory and vulkan committed buffer on the XMem hook: 98304 bytes at the hook's address: GPU VA is that address, a GPU copy reads the CPU's writes and the CPU reads the GPU's; a texture only takes its size from the hook.
  • Whole suite, ctest -j 1: 272 tests, 253 pass, 13 skipped, 6 fail, the same counts as main dde82f27e on this card. The six failures are the same in both runs: vk_queue_query_sync, d3d12_vulkan_device, d3d12_texture_tiles, d3d12_indirect_dispatch_count, d3d12_depth_resolve and d3d12_pipeline_library_host. No recorded test status differs between the two runs; that includes the other GPU tests, whose test device now enables VK_EXT_external_memory_host through vk_test_device.h.
  • The build has 22 compiler warnings, the same count as main.
  • Not covered here: anything with the game; these are the synthetic tests only.
  • MSVC 14.42 (VS 2022), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, llvm-mc 23.1.6, RelWithDebInfo, LEONIDA_VK_DEVICE=7800; the configured build completed (GameInput shim not built: no SDL3, so gameinput is listed as disabled and not counted). Synthetic tests only, no game files.

AI-assisted (Claude Code).

ramenbellic added a commit to ramenbellic/Leonida that referenced this pull request Nov 23, 2026
ramenbellic added a commit to ramenbellic/Leonida that referenced this pull request Nov 23, 2026
@optinium51-bot

Copy link
Copy Markdown

Tested 9335d6b14 merged onto main 47428cc22 (clean merge) on NVIDIA GeForce RTX 3060 (driver 616.92), Windows 11 26200, MSVC 14.52 (VS 2026), RelWithDebInfo + Ninja: pass.

No game files. AI-assisted (Claude Code).

…ndler really placed them

ItsNotRexed#7218's head fixes the slot-133 flags constant clang-cl rejected (reported on ItsNotRexed#7411) and changes the
placed-at-VA handler: it takes the title's barrier layout, and when it cannot import the pages it answers with
an ordinary DEFAULT buffer instead of failing.

For a committed buffer that fallback would drop the title's heap type (an UPLOAD or READBACK buffer could no
longer be mapped). create_committed_through_xmem now passes UNDEFINED as the layout, checks that the buffer
it got back has the hook's address as its GPU VA, and otherwise gives it back and makes the buffer on the PC
device with the title's heap, keeping the hook memory as bookkeeping. xs_proxy_mock_test covers that case.

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