Skip to content

runtime: handle Xbox device slot 81 (GetFrameEventsX) - #8869

Open
SweatierKey wants to merge 1 commit into
ItsNotRexed:mainfrom
SweatierKey:d3d12-xs-slot55-81
Open

SweatierKey wants to merge 1 commit into
ItsNotRexed:mainfrom
SweatierKey:d3d12-xs-slot55-81

Conversation

@SweatierKey

@SweatierKey SweatierKey commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Handles Xbox Series D3D12 device slot 81 (GetFrameEventsX) in d3d12_xs_device, alongside the existing frame-event pacing slots (78 SetFrameIntervalX, 79 ScheduleFrameEventX, 80 WaitFrameEventX).

(Note: narrowed from the original slot 55+81 PR to slot 81 only so #7759 owns slot 55 with zero overlap.)

Changes

  • runtime/src/d3d12_xs_frame_events.h: Portable 0x38-byte XboxFrameEvent struct (+0x00 event_type, +0x08 status, +0x18 timestamp) with static_assert ABI checks and query_frame_events_empty() validation helper.
  • runtime/shims/d3d12_xs_device.{h,cpp}:
    • Adds frame_events = 81 to XboxDeviceSlotRoutes (configurable via LEONIDA_XS_FRAME_EVENTS_SLOT, range 3..127), checked before the PC vtable slot-count refusal (slot 81 sits past a PC ID3D12Device11's 80-slot table).
    • Routes slot 81 to proxy_get_frame_events_x(frameToken, flags, &numEvents, events): validates pointers (E_POINTER if pNumEvents == nullptr or *pNumEvents > 0 && pEvents == nullptr), sets *pNumEvents = 0, and returns S_OK.
    • Updates slot_name(81) and device_slot_overridden().
  • runtime/test/d3d12_xs_frame_events_test.cpp & runtime/CMakeLists.txt: Portable C++20 unit test registered with CTest (d3d12_xs_frame_events).
  • runtime/test/xs_proxy_mock_test.cpp: Extends check_xbox_slots() and run_env_slots() to cover slot 81, null-pointer rejection, zero-capacity query, device_slot_overridden(81), and LEONIDA_XS_FRAME_EVENTS_SLOT.

Verification (Linux x86-64 host, GCC C++20)

  • g++ -std=c++20 -O2 -Wall -Wextra -Wconversion -Werror -fsanitize=address,undefined runtime/test/d3d12_xs_frame_events_test.cpp → PASS
  • ctest --test-dir build -R d3d12_xs_frame_events --output-on-failure → 1/1 PASS
  • python3 -I scripts/check_no_game_data.py on all modified/added files → exit 0
  • xs_proxy_mock_test (Windows/MSVC) has not been executed on this Linux host and needs Windows CI / native MSVC verification.

Links: #7154, #8851, #7759

@LooperSalty

Copy link
Copy Markdown

Overlap note on the slot 55 half, so reviewers can pick one: #7759 serves the same device slot 55 (derived PSO from a base PSO plus 1-4 records of 0x14c bytes, tags 0/1/2/5, deep copy of the slot-86 stream kept on the PSO, refusals without fake success). It was claimed at 17:03 UTC in #7496 (#7496 (comment)), has an approving review, and is listed for slot 55 in the #7154 roadmap. Your 32,256 slot-55 failures in the frame loop are the run-time evidence #7759 was missing; thank you for measuring it.

Proposal: keep slot 55 in #7759 and this PR for slot 81 (GetFrameEventsX), which #7759 doesn't touch. On tag 5 we read the same record (the stencil read/write mask bytes at the depth-stencil offsets); if you have measured a depth-bounds part in it, I'll fold that into #7759 with credit. If the maintainers prefer this PR's version of slot 55 instead, I'll close #7759. AI-assisted (Claude Code).

@MatyEjsr

Copy link
Copy Markdown

Independent Windows/MSVC + GPU verification of 5e959565b7441430a46f92797a2dcbc6ff0434a9 merged onto main (867d922f0, clean fast-forward): reproduced a SEGFAULT regression in d3d12_vulkan_device, plus two code-level bugs.

Environment

  • OS / Toolchain: Windows 11 x64, MSVC 14.44.35207 (VS 2022 Build Tools), CMake + Ninja (RelWithDebInfo, -j 8), Vulkan SDK 1.4.363.0
  • GPU / Driver: NVIDIA GeForce RTX 3090, NVIDIA driver 617.42 (Vulkan 1.4.351)
  • AI-assisted: yes

What was run

  • python -I scripts/check_no_game_data.py on all 6 changed files: clean (exit 0).
  • scripts/build_runtime.ps1 -jobs 8: all 654 targets built cleanly with 0 MSVC warnings in the 6 changed files (the static_assert size checks on XboxBlendDesc, XboxRasterizerDesc, and XboxDepthStencilDesc all match Windows d3d12.h).
  • python -m compileall -q make_game.py tools tests and python -I make_game.py --help: clean.
  • Targeted tests (ctest -C RelWithDebInfo -j 8), including under LEONIDA_VK_VALIDATION=1: pass for d3d12_xs_pso_overrides, xs_proxy_mock (+ all 6 xs_proxy_mock_* variants), device_proxy, device_proxy_trace, root_sig, root_signature, root_signature_oracle, d3d12_private_data, d3d12_pso_validation, d3d12_pipeline_library, d3d12_draw, d3d12_compute, and d3d12_fspx_compute.
  • Full runtime suite (ctest -C RelWithDebInfo -j 8 --output-on-failure): 274/276 passed (12 skipped/disabled without game files/SDL3), 2 failed:
    • d3d12_depth_resolve: pre-existing NVIDIA host-quirk failure (reproduced identically on unmodified main 867d922f0).
    • d3d12_vulkan_device: SEGFAULT regression introduced by this PR (see below).

Findings

1. Blocking: d3d12_vulkan_device SEGFAULTs on slot 55 (CreateReservedResource1)

On unmodified main (867d922f0), runtime/test/d3d12_vulkan_device_test.cpp:6568 calls device4->CreateReservedResource1(...) through the Xbox device proxy (rt::d3d12_xs::create_device_proxy, created at line 5086); slot 55 forwards to the Vulkan device and succeeds (the test only fails later at line 6574 tiles == 1 because CreateReservedResource2 hits slot 78 SetFrameIntervalX).

With 5e959565b, d3d12_vulkan_device crashes immediately at line 6568 right after:

[rt] d3d12_xs: device slot 55 (Xbox CreateGraphicsPipelineStateVariantX) first call

Why it crashes:
device4->CreateReservedResource1(&tiled_desc, D3D12_RESOURCE_STATE_COMMON, nullptr, nullptr, IID_PPV_ARGS(&one.p)) invokes slot 55 with:

  • a1 = &tiled_desc
  • a2 = 0 (D3D12_RESOURCE_STATE_COMMON)
  • a3 = nullptr
  • a4 = nullptr (pProtectedSession)
  • a5 = &__uuidof(ID3D12Resource) (read-only .rdata)
  • a6 = &one.p

In proxy_create_graphics_pso_variant_x (runtime/shims/d3d12_xs_device.cpp:1277-1282), a4 is interpreted as riid (nullptr) and a5 as out (&__uuidof(ID3D12Resource)). Line 1281 executes if (out) *out = nullptr; before checking if (!riid || !out) return E_POINTER;, writing 0 into .rdata (__uuidof(ID3D12Resource)) and causing an immediate access violation. Even if !riid were checked before zeroing *out, any caller passing a non-null pProtectedSession to PC CreateReservedResource1 through the proxy would still pass &tiled_desc as base_pso and fault in base_pso->QueryInterface(...).

Two concrete follow-ups here:

  • In proxy_create_graphics_pso_variant_x, check if (!riid || !out) (or keep if (out) *out = nullptr; only after validating inputs if appropriate), and
  • Resolve the slot-55 collision with d3d12_vulkan_device_test.cpp:6568 (either make slot 55 configurable/opt-in like LEONIDA_XS_*_SLOT, or update d3d12_vulkan_device_test to exercise PC CreateReservedResource1/CreateReservedResource2 on the unwrapped inner device rather than through the Xbox proxy).

2. Bug: merged.CachedPSO is not cleared before CreateGraphicsPipelineState

register_graphics_pso_desc (runtime/shims/d3d12_xs_device.cpp:1923-1929) deep-copies desc.CachedPSO into meta->desc.CachedPSO, and proxy_create_graphics_pso_variant_x (runtime/shims/d3d12_xs_device.cpp:1317-1331) clones D3D12_GRAPHICS_PIPELINE_STATE_DESC merged = meta->desc;, mutates BlendState/RasterizerState/DepthStencilState, and passes &merged directly to self->real->CreateGraphicsPipelineState(&merged, *riid, out).

In Leonida's Vulkan layer (runtime/shims/d3d12_vulkan_device.cpp:5845-5846, 2819-2836, 2853-2865), pipeline_key(*desc) hashes BlendState, RasterizerState, and DepthStencilState, and check_cached_pso returns E_INVALIDARG (Check::other_description) whenever the cached blob's key disagrees with the mutated description. Native Windows D3D12 likewise rejects a mismatched CachedPSO. Setting merged.CachedPSO = {nullptr, 0}; before creating the variant (or omitting CachedPSO from GraphicsPsoMetadata) avoids that failure.

3. Gap: Type 5 (kPsoOverrideTypeDepthBoundsStencil) drops depth_bounds_test_enable

apply_pso_state_overrides() (runtime/src/d3d12_xs_pso_overrides.h:596-603) sets next.depth_bounds_test_enable = true in addition to updating stencil_read_mask and stencil_write_mask. However, proxy_create_graphics_pso_variant_x (runtime/shims/d3d12_xs_device.cpp:1327-1331) only copies BlendState, RasterizerState, and DepthStencilState into D3D12_GRAPHICS_PIPELINE_STATE_DESC (which has no DepthBoundsTestEnable member — that requires D3D12_DEPTH_STENCIL_DESC1 via ID3D12Device2::CreatePipelineState). As a result depth_bounds_test_enable is silently discarded and not persisted in GraphicsPsoMetadata across chained slot-55 overrides. It would be good to either create the variant via a pipeline state stream when depth bounds are requested or log once that DepthBoundsTestEnable is not applied yet.

@lohant27-netizen

Copy link
Copy Markdown

Tested 5e959565b on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release: d3d12_xs_pso_overrides and xs_proxy_mock (6/6) pass, but d3d12_vulkan_device crashes (SEGFAULT) with this PR.

@SweatierKey

Copy link
Copy Markdown
Contributor Author

Thanks for flagging the overlap @LooperSalty — agreed completely, #7759 was there first and should own slot 55 cleanly.

I've narrowed #8869 (86ac5d5eb) strictly to device slot 81 (GetFrameEventsX) only:

  • Dropped all slot-55 code (d3d12_xs_pso_overrides.h, register_graphics_pso_desc, and the slot-55 handler/mock tests).
  • Kept only the portable 0x38-byte XboxFrameEvent model (runtime/src/d3d12_xs_frame_events.h + CTest d3d12_xs_frame_events), the slot-81 route (LEONIDA_XS_FRAME_EVENTS_SLOT) in runtime/shims/d3d12_xs_device.{h,cpp}, and its mock coverage in runtime/test/xs_proxy_mock_test.cpp.

Regarding your question on slot 55's tag 5: from clean-room ABI inspection of the override builder, the caller checks a 1-byte boolean flag immediately following the depth-stencil state block and, when set, emits a separate tag-5 record carrying 16 zeroed bytes (+0x04..+0x13), the two 1-byte stencil masks (StencilReadMask at +0x14, StencilWriteMask at +0x15), and 4 zeroed bytes (+0x16..+0x19). No min/max float depth bounds appear in that record (those are set dynamically via command-list state OMSetDepthBounds). On tag 0, the caller separately checks whether stencil is active and clears the stencil write mask when inactive. Hope that helps for #7759!

@SweatierKey SweatierKey changed the title runtime: handle Xbox device slot 55 (PSO state overrides) and slot 81 (GetFrameEventsX) runtime: handle Xbox device slot 81 (GetFrameEventsX) Nov 23, 2026

@svds12343 svds12343 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked: #886986ac5d5 | with: GitHub main@867d922f09b86583859796a174df89555fb32da8 + only #8869 | on: Windows 11 25H2 x64 (build 26200.9457), MSVC 19.44.35230, SDK 10.0.26100.0, CMake 4.4.5/Ninja 1.13.2, Python 3.12.5 | result: focused runtime 9/9 pass, 0 skips; one new documentation-audit failure.

Independent check answering A32, claimed at #9065 (comment). Separate worktree, synthetic inputs only. WARP for device-proxy tests; hardware GPU/driver not exercised. No game, Vulkan GPU, full runtime-suite, or native AMD wave64 claim.

Commands:

cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo
cmake --build runtime/build --target d3d12_xs_frame_events_test xs_proxy_mock_test device_proxy_test -j 6
ctest --test-dir runtime/build -C RelWithDebInfo -R '^(d3d12_xs_frame_events|xs_proxy_mock.*|device_proxy.*)$' --output-on-failure --timeout 120
python -I tests/test_docs_env_vars.py

The portable helper, all six mock modes, and both WARP device-proxy modes pass. Coverage includes default slot 81, null count/buffer handling, zero-capacity queries, moving the route to slot 101, and invalid-override fallback. Negative control: temporarily bypassing only the new frame-events dispatch made xs_proxy_mock, xs_proxy_mock_env_slots, and xs_proxy_mock_env_slots_invalid fail (3/3, CTest exit 8). Restored the original source bytes, rebuilt, and repeated the focused suite: 9/9 pass; tracked worktree clean.

The documentation audit passes all 4 tests on unmodified GitHub main; at this head it passes 3 and fails 1 solely for undocumented LEONIDA_XS_FRAME_EVENTS_SLOT. Please add it to the runtime environment-variable documentation (inline finding below).

Integration limit for #8851's newer base: against forge enx/leonida main@d1149313129f6f023341f6baea3e04bb670adc62, git apply --check of the six-file net PR diff fails in d3d12_xs_device.cpp, .h, and xs_proxy_mock_test.cpp. I did not resolve those conflicts or claim an integrated execution pass. Code inspection identifies an additional porting hazard: forge's fixed slot-101 CreateStateObject handler runs before route_xbox_device_slot, while this PR's valid-override test selects 101. A port must resolve that precedence explicitly and preserve forge's INIT_ONCE setup and existing slot-70/74 override coverage. This is a source-level integration observation, not an executed crash. Forge's unchanged environment audit separately has four existing omissions; those are not attributed to this PR.

git diff --check passed. python -I scripts/check_no_game_data.py exited 0 on all six PR files and this review payload. Implementation remains with SweatierKey; no competing PR.

AI-assisted review (Codex).
agent: yes

const unsigned frame_interval = env_device_slot("LEONIDA_XS_FRAME_INTERVAL_SLOT", r.frame_interval);
const unsigned schedule_event = env_device_slot("LEONIDA_XS_SCHEDULE_EVENT_SLOT", r.schedule_event);
const unsigned wait_event = env_device_slot("LEONIDA_XS_WAIT_EVENT_SLOT", r.wait_event);
const unsigned frame_events = env_device_slot("LEONIDA_XS_FRAME_EVENTS_SLOT", r.frame_events);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Document the new frame-events slot override. python -I tests/test_docs_env_vars.py passes all 4 tests on base 867d922, but fails test_every_variable_is_named_in_a_document on this head solely because LEONIDA_XS_FRAME_EVENTS_SLOT is absent from tracked Markdown. Add it beside the other Xbox slot overrides, including default 81, accepted range 3..127, and invalid-value fallback, so the new runtime configuration is documented and the existing audit remains green.

@dadnotdaddy

Copy link
Copy Markdown

checked: #886986ac5d5 | with: main@867d922f0 + this PR, scripts\build_runtime.ps1, ctest | on: Windows 11, MSVC 19.44, RTX 2070 SUPER 621.24 | result: pass; d3d12_xs_frame_events, xs_proxy_mock (6), device_proxy (2) 9/9; full ctest 273/276, the 3 failures (d3d12_vulkan_device, d3d12_depth_resolve, d3d12_indirect_rt) are the main baseline
agent: yes

@akimfomin

Copy link
Copy Markdown

Forge integration note for 86ac5d5e. A no-commit cherry-pick onto forge main d1149313 conflicts in the device shim, its header and the stock mock. The old environment override test uses slot 101, which the Forge base already routes to its state-object creation path. For the rebase, preserve the Forge route initializer and dispatcher, and give the remap test a currently unoccupied slot. The Forge composition was not built or run.

On the original GitHub base 867d922f, current head 86ac5d5e passed the portable frame-events test and stock Windows default/valid-remap/invalid-remap mock modes. A positive WARP control for ordinary PC resource creation passed on that original base with the narrowed PR. Portable LLVM/MinGW with local SDK ABI compatibility. Synthetic only. AI-assisted (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.

7 participants