Skip to content

d3d12_xs: write descriptors back into a title-backed heap's own memory - #9586

Closed
AAyberkU wants to merge 3 commits into
ProjectLeonida:mainfrom
AAyberkU:pr/view-writeback
Closed

AAyberkU wants to merge 3 commits into
ProjectLeonida:mainfrom
AAyberkU:pr/view-writeback

Conversation

@AAyberkU

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue: #9401 (comment)

Depends on: #7021. This branch includes a merge of its head, so review only the last commit.

What and why

On the console a descriptor heap is plain memory. The title creates views with Create*View into a CPU-only heap backed by its own __XMemAllocationHooks__ allocator (#7021). It then builds its shader-visible descriptor tables with plain CPU copies from that backing.

With #7021, device slots 17-24 translate the destination handle to the layer's heap, so only the layer's copy gets written. The title's backing at that slot stays zero, the tables it builds hold zeros, and shaders read null V#s.

In runtime/shims/d3d12_xs_device.cpp, slots 17-24 now copy what they write back into the title's backing. That covers CreateConstantBufferView, CreateShaderResourceView, CreateUnorderedAccessView, CreateRenderTargetView, CreateDepthStencilView, CreateSampler, CopyDescriptors (a null size array means one descriptor per range) and CopyDescriptorsSimple.

  • When: the original destination lies in a title-backed range. The layer's freshly written bytes are copied from real_start + offset back to the title address: count * stride bytes, clamped to the heap.
  • Stride: the layer's increment for the heap type. That increment already sized the backing (slot 14), and the title steps its handles by it (slot 15 forwards it), so the two sides cannot disagree.
  • Safety check: when a heap is registered, VirtualQuery checks that the layer's CPU handles for it are readable committed memory. They are with the Vulkan layer and with WARP. If a driver hands out opaque CPU handles, the copy is turned off for that heap and one line is logged.
  • RTV/DSV heaps: they go through the same translation and are mirrored too, so the title's backing always matches the layer heap after a device write.

Measured result

Retail 1.0.2.1, RTX 5060 Ti, driver 621.01, with #9137 and #9276 also applied. The details are on #7021 and #8851.

  • Slot 0 of the UI vertex shader's table goes from a null V# to a valid buffer V#, so its constants now reach the shader.
  • The UI pass covers its area: a probe pixel shader fills it, where before nothing was drawn.
  • Every descriptor of the UI pass now resolves. The black frame that remains is what the title draws at this stage: a clear, a fully transparent overlay whose constants are all zero, and a final copy of a 4K render target. No texture is uploaded yet. The next stop is the title not getting past loading, not rendering.

Tested

Windows 11, MSVC 2022 (RelWithDebInfo, Ninja), Vulkan SDK 1.4.363.0.

  • xs_proxy_mock with its 5 fault/env variants, plus device_proxy and device_proxy_trace (WARP): 8/8 pass.
  • New check_descriptor_mirror in xs_proxy_mock_test:
    • every slot from 17 to 24 into a CPU-only title-backed heap;
    • clamping at the heap end, with the guard bytes left untouched;
    • a CopyDescriptors with a null size array;
    • no copy after the heap is released.
  • New WARP check in device_proxy_test: a CBV into a CPU-only title-backed heap reaches the title's backing.
  • python -I scripts/check_no_game_data.py on the changed files: exit 0.
  • Note for later: if a PR adds the console raw-SRV device slot 70 path, it should call mirror_title_descriptors(self, destination, 1) there too.

AI-assisted: yes. Built and measured with 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 (n/a: runtime change).

🤖 Generated with Claude Code

https://claude.ai/code/session_016ABh377Xfov1ZDULWTYvTm

Khuraie and others added 3 commits November 23, 2026 23:42
…descriptor heaps and translate CPU handles)

Conflicts with main resolved by keeping both sides: device_slot_overridden keeps
main's Xbox slot routes and adds ItsNotRexed#7021's slots 17-24; the mock test keeps the
command_list kind and check_xbox_slots next to ItsNotRexed#7021's heap kind and
check_descriptor_heap_hooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016ABh377Xfov1ZDULWTYvTm
On the console a descriptor heap is plain memory. A title can create views
with Create*View into a CPU-only heap and later assemble its shader-visible
tables with plain CPU copies from that heap's memory. With ItsNotRexed#7021 the heap's
memory is the title's own backing, but device slots 17-24 translate the
destination handle to the layer's heap, so only the layer's copy is written;
the title's backing stays zero and the tables it assembles from it hold null
descriptors.

After each CreateConstantBufferView, CreateShaderResourceView,
CreateUnorderedAccessView, CreateRenderTargetView, CreateDepthStencilView,
CreateSampler, CopyDescriptors and CopyDescriptorsSimple whose destination lies
in a title-backed heap, the bytes the layer wrote are copied back over the
title's backing at the same offset: count * stride bytes, clamped to the heap.
The stride is the layer's increment for the heap type, which already sized the
backing (slot 14) and which the title steps its handles by (slot 15 forwards
it), so the two sides cannot disagree. The copy is enabled per heap only when
the layer's CPU handles for it are readable committed memory (the Vulkan
layer's and WARP's are); an opaque driver handle disables it with one log line.

Tests: xs_proxy_mock_test checks every slot and both copies into a CPU-only
title-backed heap, the clamp at the heap's end and a null size array;
device_proxy_test checks a CBV into a CPU-only title-backed heap on WARP.

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

Copy link
Copy Markdown

Tested 474781951 merged onto main 5d74c2723 on native Windows 11 26200, MSVC 14.44, RTX 3070 (NVIDIA 610.88): pass, with a control against this PR's own parent.

The mirrored checks cover Create*View and CopyDescriptors*. Not covered here: a write that straddles the end of a title-backed range (the "clamped to the range" part of the description). A case with count * stride crossing the backing's end would pin the clamp.

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

@KkelebekK

Copy link
Copy Markdown

@AAyberkU, a static read of the retail executable that backs this PR, plus one more slot your "note for later" already points at.

Linux/Wine data on the same draw is in #8683 (7114755514) and #9522 (7114817347). I'm moving my Linux integration onto #7021 + #9137 + #9276 + this PR now and will report the result here.

AI-assisted (Claude Code).

@Etr0p

Etr0p commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

A complement from the #9137 side. The mirror covers what the layer writes. The other direction is not wired anywhere yet: what the title writes or copies itself into a title-backed heap. #9137 has a hook for it, TitleVaAliases::set_descriptor_source in runtime/src/title_va_alias.h. Before the title-VA table scan, it pulls the bound table's slots from the title's backing into the layer's heap. Nothing registers a DescriptorSource, so with #9137 + #9586 that pull never runs. Any slot the title fills by a plain CPU copy stays zero in the layer's heap.

Our measurements on the retail boot (RTX 5070 Ti Laptop, NVIDIA 621.24, our boot stack, which includes #7021 and #9137; counts only):

  • mirror alone: the test PS covers 0% of the screen;
  • mirror + a registered descriptor source + a table scan of at least 16 slots: the test PS covers 100% of the screen.

The scan window is now in #9137 (70e49fd87, kTableScanMinimum = 16). A console vertex shader on that boot reads slot 2 of a table declared with one slot.

Proposal for the source, which needs #7021's ranges, so it belongs on this branch rather than in #9137:

  • a pull_title_descriptors(context, real, bytes) in d3d12_xs_device.cpp. It finds the title-backed range whose layer memory holds [real, real + bytes) and copies the title's bytes over the layer's, 32 bytes at a time, only where the two differ. It returns the count, which shows up in the scan counters;
  • registered with set_descriptor_source only for heaps whose layer CPU handles are host memory, i.e. the heaps your VirtualQuery check already keeps the mirror on for.

One risk: the pull must not run without the mirror. A slot the layer wrote via Create*View but that is stale or zero in the title's backing would be pulled back over the layer's descriptor. With the mirror on for the same heaps, both sides agree after every layer write, so only real title-side writes move.

I can push that as a commit on your branch, with a mock check in xs_proxy_mock_test, or open a small PR that depends on this one and #9137. Which do you prefer?

AI-assisted (Claude Code, Claude Opus 5.5).

@KkelebekK

Copy link
Copy Markdown

Linux/Wine test of this PR at head 4747819517f5: Wine 11.21, clang-cl + xwin cross build, separate Wine prefix.

Test Result
xs_proxy_mock (with the new check_descriptor_mirror) PASS: "CBV/SRV/UAV/RTV/DSV/sampler and both descriptor copies into a title-backed CPU-only heap reach the title's backing, clamped to the heap"
xs_proxy_mock --fail-virtualalloc / --fail-virtualprotect / --fail-veh / --env-slots / --env-slots-invalid PASS, all five
device_proxy, device_proxy_trace (WARP) not run: Wine has no WARP, so D3D12CreateDevice fails

One side note, not caused by this PR as far as I can tell: under Wine those WARP tests exit with 1, not the SKIP_RETURN_CODE 77 their CTest entries expect, so ctest lists them as FAIL rather than skipped there. So on Linux this confirms the mock half. The WARP check (a CBV into a title-backed heap reaching the backing on a real device) still needs a Windows run, or a Vulkan-device variant.

AI-assisted (Claude Code).

@AAyberkU

Copy link
Copy Markdown
Contributor Author

Thanks, all three of you.

VibeClauding, the straddle case: it is in check_descriptor_mirror. The CopyDescriptorsSimple check copies a count that crosses the backing's end (xs_proxy_mock_test.cpp, the "count past the heap's end that the backing clamps" step). It then checks that the guard descriptor right after the heap is untouched at both its first and last byte. Thanks for running the control against 3b77f4335. That is the right parent.

Etr0p, the other direction: agreed. Our local stack already has exactly that source, and that is how I measured coverage:

  • It is a pull_title_descriptors in d3d12_xs_device.cpp, registered through TitleVaAliases::set_descriptor_source.
  • It copies the title's bytes over the layer's wherever they changed against a snapshot. The mirror moves that snapshot along, so the pull never sees its own write-back as a title store.
  • Our numbers match yours:
    • pull alone: slot 0 stays null, because the title copies zeros;
    • mirror alone: the title's backing gets the V#s, but nothing reaches the shader-visible heap;
    • mirror + pull + a wider table scan (we used 64 locally; your 16 in 70e49fd87 covers the slot-2 read): the UI VS covers its area.
  • Your risk is real. Without the mirror, the pull overwrites the layer's fresh descriptors with the stale bytes in the title's backing. So the pull needs the mirror, as you say.

Since #9137 isn't in main, I'd keep this PR mirror-only so it stands on #7021 alone. The source would go in a small follow-up on top of #7021 + #9137 + this PR, registered only for heaps where the mirror is on (the VirtualQuery check). I'll open that unless you'd rather fold it into #9137.

KkelebekK, slot 70: yes. Our local build also calls the mirror from the console raw-SRV slot 70 path. Whichever PR gives slot 70 a real view (#8304 / #7066 / Etr0p's item) should call mirror_title_descriptors(self, destination, 1) there. Thanks for the static read: the clwb at the slot's own address is a good confirmation of the intended semantics.

AI-assisted (Claude Code).

@adriaespii

Copy link
Copy Markdown
Contributor

Tested 4747819 merged onto main 5d74c27 on Windows 11 Pro 24H2 (build 26100), AMD Radeon RX 9070 (RDNA4 / GFX1201), AMD Software 26.10.1 (Vulkan 1.4.359), MSVC 14.44 (VS 2022 Community 17.14.40), RelWithDebInfo: pass.

Confirming the WARP and Windows host runtime checks noted by @KkelebekK (which could not run under Wine):

  • device_proxy and device_proxy_trace (WARP): PASS (CreateConstantBufferView into a title-backed CPU-only heap reaches the title's backing on WARP).
  • xs_proxy_mock (including the new check_descriptor_mirror for all slots 17–24, clamping, and CopyDescriptors with null size array): PASS (CBV/SRV/UAV/RTV/DSV/sampler and both descriptor copies into a title-backed CPU-only heap reach the title's backing, clamped to the heap).
  • All 6 fault and env variants (xs_proxy_mock_no_reserve, _no_protect, _no_handler, _env_slots, _env_slots_invalid): 8/8 PASS under CTest (plain and with LEONIDA_VK_VALIDATION=1, 0 VUIDs).
  • git diff --check upstream/main..HEAD: clean.
  • scripts/check_no_game_data.py: clean.

AI-assisted. Synthetic tests only, no game files.

@MarcPique

Copy link
Copy Markdown

Independent native Windows check of the unmodified #9586 head 4747819517f582995224c6071dcbb2a107b5ff73 using MSVC 19.44 and the Windows SDK 10.0.26100.0. Eight focused CTests passed. Both device_proxy and device_proxy_trace use D3D12 WARP and confirm that a CBV written through the proxy reaches the title's CPU-only heap backing. The xs_proxy_mock test also passed the clamped copy with a guard slot.

I confirmed the distinction between the two directions in an offline Windows/RTX 4090 boot comparison with the original game shaders. Adding this PR's write-back and mirroring the existing console slot-70 buffer-view path left slots 0–2 empty in all 63 sampled shader-visible tables. Registering a local snapshot-based source through #9137 then populated slot 0 in all 63, with the scan still requesting one descriptor for each of these tables. Both captures with the one-slot scan were black. This corroborates the need for a second, separate change to pull CPU stores into shader-visible storage. The local callback is an integration test of the proposed follow-up.

AI-assisted (Codex).

@slint6614-dev

Copy link
Copy Markdown
Contributor

AMD Windows check of 474781951 (AI-assisted), a third vendor after the RTX 3070 and Wine runs above. Windows 11 26300, MSVC 2022 (Ninja, RelWithDebInfo, full runtime/ build of this head), Vulkan SDK 1.4.363.1, AMD Adrenalin 26.11.2: Radeon RX 9070 (RDNA4, Vulkan driver 2.0.416) and Ryzen 9600X iGPU "AMD Radeon(TM) Graphics" (RDNA2 gfx1036, driver 2.0.353), each picked with LEONIDA_VK_DEVICE/LEONIDA_HOST_ADAPTER.

No game files.

Capi360 added a commit to Capi360/Leonida that referenced this pull request Nov 23, 2026
Connect the descriptor-source hook from ProjectLeonida#9137 to the heap backing tracked by ProjectLeonida#7021 and written by ProjectLeonida#9586. Cache observed source bytes so repeated submissions keep their translated V# addresses. The GPU fixture now assigns separate title backing, checks its shader reads, and verifies unchanged source writes retain the translated address. All 15 focused tests passed on Windows 11 and RTX 3060 Ti. AI-assisted with Codex.
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