Skip to content

Runtime: Xbox-ABI proxies for the command queue and the fence (BUILD-231) - #1270

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
AimLogin:rt/queue-fence-proxy
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
AimLogin:rt/queue-fence-proxy

Conversation

@AimLogin

@AimLogin AimLogin commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Item: BUILD-231, claimed in #634 and tracked in the coordination issue.
Follow-up that builds on this: D3D-136, @quackottu's console-order command-list proxy. It adds the ExecuteCommandLists unwrap in its own PR, as agreed in #1218.
Related: #908 (PresentX). Its queue methods sit past the public chain, which is exactly the tail this proxy refuses and logs. This PR does not depend on #908, and #908 does not depend on this PR.

What

Until now, D3D12XboxCreateDevice wrapped only the device and the slot-14 descriptor heaps. Queues and fences reached the title as bare PC or Vulkan-layer objects with the SDK vtable, so a console-only queue method past the PC chain jumped through whatever memory followed that vtable.

  • What is wrapped. Device slots 8 CreateCommandQueue, 75 CreateCommandQueue1 (only with ID3D12Device9), 36 CreateFence and 32 OpenSharedHandle (when it opens a fence) return queue and fence proxies.

  • Forwarding. The public chain is forwarded in PC order: 19 queue slots, and 11 or 12 fence slots depending on whether the real fence has ID3D12Fence1 (SDK 10.0.26100).

  • The refused tail. A slot past the public chain, up to the end of the proxy's table (128 queue slots, 64 fence slots), logs its first four argument words once and returns E_NOTIMPL. That is where PresentX/SuspendX/ResumeX will show up. Their slots are not guessed.

  • Guard region. Each table sits at the end of the first page of a 64 KiB reservation whose remaining pages are never committed. A slot past the table therefore faults on the vtable read itself, and a vectored handler names the slot before the fault continues. If the reservation, VirtualProtect or the handler fails, that is logged, and command_object_tables_guarded() reports false wherever a fault past the table would no longer be named.

  • Unwrapping. Wherever a device or queue method hands a heap, queue or fence object to the real runtime, the real object is passed:

    • queue Signal/Wait (14/15);
    • device CreateSharedHandle (31), MakeResident/Evict (34/35), SetEventOnMultipleFenceCompletion (45), SetResidencyPriority (46) and EnqueueMakeResident (50).

    Arrays are copied without exceptions: up to 16 entries inline, longer ones with new (std::nothrow). An allocation failure returns E_OUTOFMEMORY.

  • Leonida's own code. d3d12_com.cpp looks through the queue proxy wherever it needs the real queue (queue_uses_calibrated_timestamps, last_execute_result).

  • COM identity. QueryInterface answers only the modelled interfaces. An interface the real object implements but the proxy does not model is logged once with its GUID and refused. GetDevice returns the device proxy. Each proxy holds its real object and the device proxy.

  • Output pointers. A non-null output is cleared before any failure, following bc59ef4's convention for slots 14/62/76. With a null output, the device's own answer is returned (S_FALSE for a capability check).

  • Logging and trace. First calls are logged per slot. --trace-d3d12 records proxy calls in the README grammar (XboxID3D12CommandQueue::Signal called, XboxID3D12Fence::Slot40 unsupported). Device slots 31/34/35/45/46/50/75 log "beyond the PC device vtable" once when the real device predates the interface, as the generic thunk does.

  • Not covered. Command lists are still passed through ExecuteCommandLists as they are, and a heap proxy given to a list's SetDescriptorHeaps reaches the real list unchanged. D3D-136 closes both gaps.

Why it is built this way

  • Same pattern as the existing proxies. The queue and fence proxies use the same shape as the device and heap proxies: slot-templated thunks, real at offset 8, per-slot counters, and QueryInterface restricted to modelled interfaces. A reviewer who knows HeapProxy knows these too.
  • Bounded table. A refused tail plus a guard region turns an unknown console slot into a named log line instead of a jump through arbitrary memory. It does not prove the console keeps the public slots. That remains an assumption (docs/d3d12_command_objects.md §5.2), and the proxy makes a wrong assumption visible.
  • A rule every thunk follows. Every function a thunk hands its words to takes them in the same positions. A title passes only as many words as the method takes, and the compiler may write a function's incoming stack-argument slots, a tail call included. Moving a word to another stack position would overwrite the title's frame. This is documented at the top of d3d12_xs_device.cpp. The mock test hit exactly this in its own thunks in an optimized build, which is how the rule was found and written down.

Tests

No game files and no Xbox DLLs anywhere.

  • queue_proxy_test (new) runs on host WARP, host D3D12 on the hardware adapter, and the Vulkan device. It covers:

    • every public slot forwarded and counted, and the refused tail;
    • the guard-region faults, at the exact address;
    • 32 error cases per backend, each run in its own process on the real object and on the proxy, with the HRESULT and device-removed state compared (27 on the host runtime, where the real runtime faults on the other 5);
    • QueryInterface identity, refcounts and release orders;
    • shared fences, and a shared buffer and a null handle through OpenSharedHandle;
    • eight threads at once;
    • repeated creation;
    • a console-style raw-slot client that copies through the queue proxy and reads the data back byte for byte;
    • a coverage gate over all table slots.

    The child-process runner of the differential check drains the pipe while the child runs, so output of any size can't stall it. It keeps only the output tail, stops hung children at the time limit and reports crashes and failed starts. Its own self-test covers a normal exit, 8 MiB of output, hung children (silent, and after 256 KiB), a crash, a missing executable, and injected pipe and thread failures, all without leaking a handle.

  • xs_proxy_mock_test (new) runs over synthetic COM objects, with no D3D12 runtime and no GPU. It covers:

    • devices that stop at ID3D12Device, Device1, Device3 and Device9;
    • fences with and without ID3D12Fence1;
    • creation failures and creation that returns S_OK with no object;
    • null IID and null output;
    • refused IIDs;
    • every OpenSharedHandle branch, with refcounts;
    • allocation failures, injected through this executable's operator new.

    Three more CTest entries (--fail-virtualalloc, --fail-virtualprotect, --fail-veh) patch the test's own import table so the guard-region setup fails.

  • device_proxy_test gets a queue and a fence through the registered D3D12XboxCreateDevice shim and runs a GPU signal through them.

Results, on origin/main 0d61c35, with this branch against the same commit without it:

Check This branch main 0d61c35
CTest, default environment 64/64 59/59
CTest, LEONIDA_VK_VALIDATION=1, implicit layers off 64/64 59/59
CTest, validation, implicit layers on 64/64 59/59
CTest, validation plus synchronization validation 64/64 59/59
Vulkan validation messages (sync mode) 0 0
Unregistered GPU programs (#883), 3 modes 12/13 12/13, same failure: d3d12_clear_test (#917)
AddressSanitizer (queue_proxy_test, device_proxy_test, mock test, 3 fault modes) 0 reports n/a
/W4 /WX /analyze (d3d12_xs_device.cpp, both new tests) clean n/a
--trace-d3d12 lines checked against the README grammar 34 279, 0 mismatches n/a
Repeated runs (validation on) mock test 10/10, device_proxy_test 10/10, queue_proxy_test 5/5 n/a

The five new CTest entries are queue_proxy, xs_proxy_mock, and the three mock fault modes. gameinput stays disabled without SDL3, as on main.

Coverage of runtime/shims/d3d12_xs_device.cpp (whole file; clang-cl 23 source-based, all three test executables): 96.7 % of lines, 100 % of functions (98), 72.5 % of branches. In the new code, only two paths are not covered:

  • the per-call log behind XS_TRACE;
  • the VirtualFree after a failed commit inside a successful reservation.

The rest of the uncovered lines are earlier device/heap proxy code (slot 62's extra words, slot 67, slot names).

Not tested:

  • Wine. Not run. The test skips the host part under Wine and runs only the Vulkan part.
  • Vulkan shared fences. The Vulkan device refuses them with E_NOTIMPL, so OpenSharedHandle is checked on the host runtime only. The test prints "not checked" on Vulkan.
  • Console slot layout. No measurement against a console. The public-slot assumption stays as documented.

Hardware

Windows 11 (10.0.26200), NVIDIA GeForce RTX 4060 Ti 16 GB, driver 621.01, Vulkan SDK 1.4.363.1, MSVC 19.50 (VS 18 Build Tools), LLVM 23.1.6 (coverage and clangd only).

Generated with [Claude Code]

@AimLogin
AimLogin marked this pull request as ready for review November 22, 2026 09:33
@AimLogin
AimLogin marked this pull request as draft November 22, 2026 09:33
@evilclanker9000

Copy link
Copy Markdown
Contributor

I checked the public queue/fence chain against a pixel oracle on 74764c5aba5c6db32d0826730b9d949b521fd214, then checked the graphics integration separately.

  • Exact PR, native D3D12. A source-built HLSL draw wrote a 37×19 target. Two direct queues and three fences came through the device proxy. One queue submitted the draw and signaled value 3; the other waited for 3, copied the texture with a 256-byte row pitch, and signaled 7. A separate CPU-controlled fence held the work: completion remained 0 and its event timed out before release, then reached 7. Every coordinate matched 119 red / 584 green on RTX 5080, Radeon 1002:13c0 and WARP. Omitting the draw left 703 blue on each. Raw queues/fences gave the same results. The host device created the PSO and lists; the test asserted both queues and all three fences were proxies in the wrapped runs.
  • Exact PR, Vulkan layer. I repeated the gated public queue/fence/copy sequence with render-target clears for the same rectangular pattern. RTX and Radeon both produced 119 red / 584 green, or 703 blue with only the initial clear, through raw and wrapped objects. Khronos validation reported zero errors. This part exercised ordered clears and copies.
  • Local integration with Runtime: compute path and graphics draws on Vulkan (D3D-3, D3D-4, milestone M1) #291. The three production shim-file changes from this PR applied cleanly to the graphics merge candidate at 70178d307cb87146a42414eefa19aa4ff0e0c40a. Its source-built M1 indexed-draw test asserted the queue and fence were proxies, then matched all 4,096 expected pixels on RTX and Radeon. Removing only the first draw made the unchanged readback oracle fail at (0,0), blue instead of red, on both. The passing runs had zero validation errors.

Windows 11 build 26200, NVIDIA 616.92, AMD 26.10.1. These were standalone test processes using the public queue and fence methods. Guest-loader execution, the D3D-136 command-list proxy, and #908 presentation are outside this measurement.

@AimLogin

Copy link
Copy Markdown
Contributor Author

@Vonsek, thanks. This covers ground my own runs didn't. queue_proxy_test signals and copies through the proxies, but it never draws, never makes one queue wait on another queue's fence with a CPU-held gate in front, and I have only an RTX 4060 Ti and WARP here. Your RTX 5080 and Radeon results are the first AMD data on this PR, and your #291 integration is the first check against the M1 graphics path. Your numbers are consistent (37 × 19 = 703 = 119 + 584), and the no-draw control at 703 blue is a useful check.

Two questions so others can reproduce it:

  1. Could you share the test sources (a branch or gist) for the native and the Vulkan-layer runs? Until someone else can rerun them, I'll cite them in the PR as your externally reported result.
  2. Did the Vulkan-layer run use synchronization validation (VK_LAYER_VALIDATE_SYNC=1) as well? The cross-queue wait is exactly where it would complain.

For the record: #1270 is still on 0d61c357 and conflicts with current main only in runtime/CMakeLists.txt (test registrations). The shim files you applied to the #291 candidate don't change in that rebase.

AI-assisted reply (Claude Code).

…231)

Device slots 8 (CreateCommandQueue), 75 (CreateCommandQueue1), 36
(CreateFence) and 32 (OpenSharedHandle of a fence) now return queue and
fence proxies. They forward the public chain in PC order (19 queue slots,
11 or 12 fence slots), refuse and log a slot past it up to the end of the
proxy's table, and name a read past the table through a guard region and
a vectored handler.

Heap, queue and fence proxies are unwrapped wherever a device or queue
method hands them to the real runtime: queue Signal/Wait, device slots
31/34/35/45/46/50. Arrays are copied without exceptions. QueryInterface
answers only the modelled interfaces, and logs one the real object has
but the proxy does not model. Outputs are cleared on failure as for
slots 14/62/76. --trace-d3d12 records proxy calls in the README grammar.
Every function a thunk hands its words to keeps them in the same
positions, because a title passes only as many as the method takes.

Tests: queue_proxy_test (host WARP, host hardware, Vulkan device;
differential per-process error cases, guard faults, identity, threads,
raw-slot client, child-runner self-test), xs_proxy_mock_test (older
devices, fences without Fence1, OpenSharedHandle branches, allocation
failures; three guard-setup fault modes), and device_proxy_test through
the D3D12XboxCreateDevice shim. No game files.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
quackottu added a commit to quackottu/Leonida that referenced this pull request Nov 22, 2026
…ist proxy

Queue slot 10 ExecuteCommandLists hands the real queue the PC list behind each
list proxy (UnwrappedArray: 16 inline, E_OUTOFMEMORY when a longer copy cannot
be allocated), and proxy_real_object knows the list vtable. device_proxy_test
and d3d12_vulkan_device_test execute the list proxy itself on the queue proxy.
@AimLogin
AimLogin force-pushed the rt/queue-fence-proxy branch from 74764c5 to 62593f4 Compare November 22, 2026 10:19
@ItsNotRexed
ItsNotRexed merged commit 53300de into ItsNotRexed:main Nov 22, 2026
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.

3 participants