Skip to content

d3d12: preserve title placement VA on slot-133 fallback textures for GetGPUVirtualAddress() - #9320

Closed
TanmaySatyam wants to merge 20 commits into
ProjectLeonida:mainfrom
TanmaySatyam:fix/slot133-texture-va
Closed

TanmaySatyam wants to merge 20 commits into
ProjectLeonida:mainfrom
TanmaySatyam:fix/slot133-texture-va

Conversation

@TanmaySatyam

Copy link
Copy Markdown

Claim: #9235 (comment) (item 3)
Depends on: #7218

Why

In #7218, create_at_host_address() (device slot 133, CreatePlacedResourceX) creates non-buffer resources as ordinary committed DEFAULT textures, leaving Resource::va_ == 0.

On Xbox Series unified memory, the title calls ID3D12Resource::GetGPUVirtualAddress() on slot-133 placed textures and writes through the returned pointer; returning 0 caused an immediate access violation (0xC0000005 writing at 0x20 inside VCRUNTIME140!memset) during retail 1.0.2.1 boot on Windows 11 + RTX 5070.

What changed

  • runtime/shims/d3d12_vulkan_device.cpp:
    • Add Resource::set_title_va(D3D12_GPU_VIRTUAL_ADDRESS) and call it in create_at_host_address() when the non-buffer fallback succeeds and address != nullptr. Ordinary PC textures (and address == nullptr) still return 0.
  • runtime/test/d3d12_existing_heap_test.cpp:
    • Verify placed_texture->GetGPUVirtualAddress() == reinterpret_cast<D3D12_GPU_VIRTUAL_ADDRESS>(pages) for slot-133 placed textures, and verify a null placement address still returns 0.

(Built on top of #7218 head 1d4d2952e; orthogonal to #8225, which covers buffer shadow copies.)

Testing

Built cleanly with zero new MSVC warnings (RelWithDebInfo, Windows 11):

  • xs_proxy_mock_test — PASS
  • d3d12_existing_heap_test on NVIDIA GeForce RTX 5070 (driver 591.74) — PASS (8/8 OpenExistingHeapFromAddress host-match + slot-133 buffer & texture VA checks)
  • d3d12_existing_heap_test on AMD Radeon(TM) Graphics (gfx1036) (driver 31.0.24002.92) — PASS (8/8 OpenExistingHeapFromAddress host-match + slot-133 buffer & texture VA checks)
  • python -I scripts/check_no_game_data.py runtime/shims/d3d12_vulkan_device.cpp runtime/test/d3d12_existing_heap_test.cpp — clean

AI-assisted.

Pipodi and others added 20 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>
…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>
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>
… (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>

@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 Linux CPU review

Checked: slot-133 / host-pointer placement VA preserved for GetGPUVirtualAddress on fallback textures; create_buffer_at_host_address + host_pointer_alignment plumbing. Windows/D3D12/Vulkan path not built here.

Looks correct from review; no game files.

@smowlyy

smowlyy commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Verified on native Windows 11 (10.0.26300.9974, MSVC 19.44, RelWithDebInfo, Vulkan SDK 1.4.363.0), PR head f4273fa7b merged onto main (6312c057f):

  • python -I scripts/check_no_game_data.py ... (all 7 changed files): clean (exit 0).
  • cmake --build runtime/build --target d3d12_existing_heap_test xs_proxy_mock_test --parallel: built cleanly.
  • LEONIDA_VK_VALIDATION=1 ctest --test-dir runtime/build -R ^d3d12_existing_heap$ --output-on-failure -V:
    • NVIDIA GeForce RTX 5060 Ti (driver 610.88, Vulkan 1.4.341): 1/1 passed (0 validation errors).
    • AMD Radeon(TM) Graphics (UMA, driver 24.10.24, Vulkan 1.3.280): 1/1 passed (0 validation errors, host HW + WARP parity matched).
  • ctest --test-dir runtime/build -R xs_proxy_mock --output-on-failure: 6/6 passed.

AI-assisted.

@ikurta92-png

Copy link
Copy Markdown

Tested f4273fa7b on NVIDIA GeForce RTX 3060 (Ampere), driver 617.42, Windows 11 Pro 10.0.26300, MSVC 19.51 (VS 2026), RelWithDebInfo, Vulkan SDK 1.4.363.1: pass; the host-pointer import works on Ampere, and one review fix isn't covered by the test.

  • This PR, LEONIDA_VK_VALIDATION off and on, 2 runs each: d3d12_existing_heap, device_proxy and the six xs_proxy_mock* tests pass, 0 validation messages (device: NVIDIA GeForce RTX 3060 (validation on)).
    • VK_EXT_external_memory_host is present here, so the import paths run instead of being skipped: vulkan import: existing heap opened on the caller's memory.
    • All 8 OpenExistingHeapFromAddress cases match the native RTX 3060 and WARP: vulkan vs host NVIDIA GeForce RTX 3060: 8 OpenExistingHeapFromAddress cases match, and the same for WARP. Write-combined opens as cpu=2, the others as cpu=3; flags are 0x421 on all three.
    • Slot-133 buffers: GPU VA is the address, a GPU copy reads the CPU's writes for the VirtualAlloc range, 64 bytes into a page and the section view (NVIDIA imports the pagefile-backed view that RADV refuses). The 0x400/0x200 modifiers and the partly committed range take the fallback.
  • Since the tests call the new API, a revert to the parent doesn't build, so I used three local mutations on this head instead (validation on; restored afterwards):
    • Slot 133 dispatch short-circuited to E_NOTIMPL: xs_proxy_mock line 421: r133 == S_OK && placed == ... fails.
    • create_host_buffer forced to E_NOTIMPL (always fallback): vulkan slot-133 buffer line 195: buffer->GetGPUVirtualAddress() == ...(pages) fails, so the readback checks depend on the real import.
    • Not caught: without the host_import_ block in ~Resource (the wait plus the immediate vkFreeMemory; memory goes to the deferred queue as before), d3d12_existing_heap still passes, readbacks and validation included. The test releases each buffer and then VirtualFrees the pages, but nothing records work after that point, so freeing the pages before the deferred free is never observed. A case that releases the buffer, frees the pages and then submits something (ideally with validation on, or on a driver that faults) would pin that fix.

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

@MiguelMatMar

Copy link
Copy Markdown

Tested f4273fa on NVIDIA GeForce RTX 4080 SUPER, driver 621.24 (Vulkan 1.4.357), Windows 11 Pro 26300, MSVC 14.44, Ninja RelWithDebInfo (fresh build folder), applied on main 6312c05, no game data: pass.

(AI-assisted check, Claude Code.)

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.

9 participants