Skip to content

D3D12.X: check writability in note_signaled and remove per-frame allocations in WaitFrameEventX - #2692

Closed
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-frame-note-signaled-writable-and-zero-alloc
Closed

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-frame-note-signaled-writable-and-zero-alloc

Conversation

@Decodere

@Decodere Decodere commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What is in it

Hardens note_signaled against non-writable output pointers and removes per-frame heap allocations, unnecessary Arc::clone traffic, and O(N) ring-buffer shifts on the WaitFrameEventX / mark_flipped hot path in crates/xcl/src/hle/d3d12x/frame.rs.

  • Writability check in note_signaled: Replaces crate::gpu::shader::readable(out, 4) with writable(p, len) (via writable_with_uma(uma, p, len)):
    • Keeps gpu::shader::readable's Uma::in_committed_window(p, len) fast path right after the null/zero/oversize/overflow guards and before resident_and_writable / VirtualQuery (per @upipibi's review), so a non-resident page inside a 4 GiB committed coherent window (PAGE_READWRITE as a whole) never falls through to a slow kernel VirtualQuery walk, while cross-window boundary straddles fall through cleanly.
    • Otherwise checks a K32QueryWorkingSetEx fast path (resident_and_writable, up to 16 pages, treating only flags & 1 != 0 && protect != 0 entries as resident so non-resident or protect == 0 pages fall through cleanly) and falls back to VirtualQuery, requiring MEM_COMMIT, no PAGE_GUARD, and a writable page protection (PAGE_READWRITE, PAGE_WRITECOPY, PAGE_EXECUTE_READWRITE, or PAGE_EXECUTE_WRITECOPY). Read-only, execute-read, guard, no-access, reserved-only, null, and cross-page RW->RO targets are safely skipped without touching pages or triggering guard faults.
  • Zero per-frame heap allocations on paced WaitFrameEventX: Replaces ancillary's Vec<HANDLE> with a stack-allocated AncillaryHandles ([HANDLE; 63] + len, implementing Deref<Target = [HANDLE]> and IntoIterator) and replaces the paced wait-handle Vec<HANDLE> with a stack [HANDLE; 64] array.
  • Lazy Arc::clone for uncapped mode: Clones c.kinds[ty as usize].extra inside wait_frame_event_x only when interval_override() == Some(0), avoiding an unused atomic reference-count increment/decrement on every default paced frame wait.
  • O(1) ring-buffer eviction: Changes ISSUED and FLIPPED from Vec (remove(0) / drain(..n)) to VecDeque (pop_front() / push_back(...)).
  • Unit tests: Adds four pure-Win32 unit tests covering writable / note_signaled (read-only, resident read-only, RW->RO cross-page straddle, PAGE_GUARD preservation, PAGE_EXECUTE_READ, PAGE_NOACCESS, reserved-only, and valid write), the committed coherent Uma::with_windows(1, 0x2_0000) fast path (non-resident untouched page, PAGE_GUARD shortcut proof, guard rejections, and end-of-window boundary fallthrough), AncillaryHandles boundary cases (null, 0, 63, 64, u32::MAX, unreadable list), and ISSUED/FLIPPED FIFO eviction order.

Part of #103

Tested

  • Failing-first probe on exact parent (b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7): Confirmed unmodified gpu::shader::readable accepted both a PAGE_READONLY target (ro_ok=true) and a 4-byte PAGE_READWRITE -> PAGE_READONLY cross-page straddle (straddle_ok=true), whereas post-fix writable rejects both and note_signaled skips them safely.
  • Clippy: cargo clippy --all-targets -- -A clippy::approx_constant -> 0 new warnings in crates/xcl/src/hle/d3d12x/frame.rs.
  • 3x Debug WARP (XCL_ADAPTER=warp, XCL_WARP=1): cargo test -p xcl --lib -- hle::d3d12x::frame::tests:: --nocapture -> 9 passed; 0 failed; 0 ignored (0 skipped, 0.25s each across all 3 runs).
  • 3x Release RTX 4060: cargo test --release -p xcl --lib -- hle::d3d12x::frame::tests:: --nocapture -> 9 passed; 0 failed; 0 ignored (0 skipped, 0.25s each across all 3 runs).
  • Full release workspace on RTX 4060: cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture -> 37 test targets, 1439 passed; 0 failed; 47 ignored (29 pre-existing host-gated SKIPPED notices: 21 wave64-only tests on a 32-lane GPU, 3 per-lane wave32 tests refused by the driver, 3 D3D12 debug layer tests without d3d12SDKLayers.dll, 1 no heap windows, 1 cannot disable the driver cache).

Regression run

Baseline (exact parent b49be183):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1604 (PresentX 1604)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13978 MB

Branch (HEAD):

xcl-smoke v1
commit: 4f9c7ff6a5a9dc5aa05305275f86c3ad82da3222
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14011 MB

Not tested

  • Wave64-only GPU tests and D3D12 debug-layer tests (unavailable on this 32-lane RTX 4060 host without d3d12SDKLayers.dll).
  • AMD and Intel GPUs, and Linux / Wine / Proton runtime behavior when K32QueryWorkingSetEx reports protect == 0 (covered structurally by falling through to VirtualQuery).
  • Retail title smoke under XCL_FRAME_INTERVAL_US=0 (uncapped override path).

AI use

Implemented, unit-tested, and verified with assistance from Google Antigravity (Gemini 4 Neon); every change and test result was reviewed and executed locally.

@SlopTart

SlopTart commented Nov 26, 2026 •

Copy link
Copy Markdown

Tested on the Linux/Wine lane, full standard env (AMD rig: Ryzen 9 9900X, 96 GB DDR5, RX 7900 XT 20 GB, Arch kernel 7.2.15, Mesa 26.2.5 RADV, wine-11.0 Proton, rustc 1.94.0; d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b overrides, official DXC kit, VKD3D_CONFIG=ignore_shared_fence, threads=2), head 9a547e23, mergeable against main b49be183:
non-gpu suite (--skip gpu::): 599 passed, 3 failed, 4 ignored, 733 filtered.
The failure set is identical to bare main b49be183 measured on the same rig the same day (596 passed, 3 failed): gpu_promoted_uav_clear (our #2490), xma2_source_voice (#2481, vendored-FAudio gate). The third red, the sparse tile video-memory test, is a known batch flake (green solo).
Zero delta from this PR; every test it adds passes inside the suite.
Not tested: gpu suite, Windows native.
Correction: an earlier version of this row reported three extra reds; those runs lacked the d3d12,d3d12core=n,b DLL overrides and hit stub-DLL NOTIMPL artifacts. Re-ran with the standard env, artifacts gone.
AI use: Hermes agent (local Qwen-class) ran the tests and drafted this; I checked it before posting.

@sage-sage

Copy link
Copy Markdown
Contributor

Test report: passes on NVIDIA. Exact head 9a547e23 (its base is current main b49be18, so head = merged).

Tested

  • RTX 4080 SUPER (driver 621.40), Ryzen 5 7600X, Windows 11 26H2 build 26300.10075, rustc 1.100.0 MSVC, DXC 1.9.2602.17 (Windows SDK 10.0.26100), debug build
  • cargo test --workspace -- --test-threads=4: 1438 passed, 0 failed, 47 ignored; the 3 new tests pass
  • SKIPPED (29 lines, the same as on main on this machine): 21 wave64 tests (NVIDIA is wave32 only), 3 per-lane wave32 f16/f32 forms (refused), 3 D3D12 debug layer (d3d12SDKLayers.dll not installed), 1 no heap windows, 1 pso_measurement_compiles_an_exact_synthetic_record (Developer Mode off)

Not tested

  • No game, so not WaitFrameEventX pacing on a title.

Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

@relativespace2K

Copy link
Copy Markdown

Tested - native Windows AMD RDNA2, PR head 9a547e23 on main b49be183

Tool: OpenCode with DeepSeek V4.1 Flash 1017

  • AMD Radeon RX 6800 XT (16338 MiB), driver 32.0.21045.29003; Ryzen 5 5600, 31.9 GB;
    Windows 11 IoT Enterprise LTSC 26100; release build; DXC 1.9.2602.17.
  • cargo test --release -p xcl --lib -- hle::d3d12x::frame::tests:: --nocapture:
    8 passed, 0 failed, 0 ignored, 0 skipped, 1331 filtered out.
  • Game: retail title 1.0.2.1, four 240 s runs, XCL_NETWORK=0 XCL_ACTIVITY=1 XCL_FRAME_DUMP=1 XCL_RUN_SECONDS=240, no other XCL_* variables; shader and pipeline
    caches warm; main and the PR run alternately.
run presents peak commit local VRAM first frame DXIL misses
main 1 1,625 33,980 MB 15,933 MB 20.95 s 139
PR 1 3,780 31,490 MB 16,274 MB 17.22 s 41
main 2 (warm) 1,849 31,819 MB 16,465 MB 20.13 s 79
PR 2 (warm) 1,780 32,490 MB 16,401 MB 16.17 s 60

PR 1 ran second and started with a warmer cache (139 -> 41 DXIL misses); the warm repeat
does not reproduce 3,780 (PR 1,780 vs main 1,849, within run-to-run variance). The only
consistent difference is the first frame ~4 s earlier in both PR runs (17.2/16.2 s vs
20.9/20.1 s); n=2, not enough to claim a gain.

Both trees: 40 mesh refusals, 0 failed pipelines, 0 DXIL failures, no crash and no device
removal. The title still stalls on its loading screen (presentation falls to zero around
125 s; 1,780-1,849 frames), so there is no world or FPS comparison in this window.

Not tested: full workspace, NVIDIA, debug build, world/FPS runs.

AI use: OpenCode with DeepSeek V4.1 Flash 1017 ran the tests, read the logs and wrote this comment; the figures and crash lines were checked against the run logs.

@luckyteagirl

Copy link
Copy Markdown
Contributor

RX 6800M verification at exact PR head 9a547e23ab97a0620384a91c305f5a8ebac3088b against current main b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 (Windows 11, AMD driver 30.0.13002.13003, Rust 1.99.0 MSVC):

  • cargo test --locked -p xcl --lib hle::d3d12x::frame::tests:: -j 1 -- --test-threads=1 --nocapture, with XCL_ADAPTER=6800M: 8 passed, 0 failed, 0 ignored, 1,331 filtered.
  • cargo build --locked --release -p xcl -j 1: passed; head executable SHA-256 0A0B772118DD4E4A7DB88D8CE014A70A1146BCCF5F5B48A5C778C62195A7C95C.
  • Paired guarded 12-second release smokes used XCL_FRAME_INTERVAL_US=0, XCL_TRACE_FRAME_EVENTS=1, XCL_ADAPTER=6800M, separate data/DXIL cache directories, and the same locally owned title build (hash 2ef5fd64). Both logs explicitly say XCL_FRAME_INTERVAL_US=0 – uncapped and show WaitFrameEventX calls plus nonzero frame-event wait counts.
Main b49be183 PR 9a547e23
Exit 0x0 0x0
Frames shown / PresentX 825 / 826 896 / 897
Last three 2-second present windows 184, 322, 320 187, 351, 358
Frame-event waits in those windows 367, 644, 640 375, 702, 715
Device removals / crashes 0 / 0 0 / 0
Peak commit 7,589 MB 7,567 MB

This confirms startup presentation and frame-event activity on this AMD card with the uncapped override. The 12-second samples are too short for a performance conclusion and do not establish world-load behavior or exercise every changed edge case. I did not run the full workspace suite on this head. AI-assisted test report with Codex.

@Aniseto

Aniseto commented Nov 26, 2026

Copy link
Copy Markdown

Ran the frame hot-path tests at 9a547e23:

Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC 14.44.35207, DXC 1.9.2609.5, debug build.

XCL_ADAPTER=nvidia cargo test -p xcl --lib hle::d3d12x::frame::tests:: -- --test-threads=1 --nocapture
8 passed; 0 failed; 0 ignored; 1331 filtered out      (exit 0)

All eight tests pass, including the three new ones for the writability check (note_signaled_skips_readonly_guard_and_cross_page_targets), the 63/64-handle boundary (ancillary_stack_handles_boundary_cases) and the FIFO eviction (issued_and_flipped_ring_buffers_evict_oldest_in_order). These are Win32 unit tests, not GPU rendering measurements.

What this adds: the author measured on an RTX 4060 (Ada) and WARP; this is a second NVIDIA host (Blackwell, different driver), and the thread already has one more NVIDIA report on an RTX 4080 SUPER.

Not tested: the full workspace suite, a paired 60 s smoke run, the retail title under XCL_FRAME_INTERVAL_US=0, AMD and Intel GPUs, Wine, release profile, title data.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash Free - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.

bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 26, 2026
Validate WaitFrameEventX's signaled-index target as committed, unguarded, and writable without touching its pages. Replace ancillary and paced wait handle vectors with fixed stack storage, avoid the scheduled-handle Arc clone on the paced path, and use deques for bounded frame history eviction.\n\nAdapted from xcl-dev#2692.\nPart of #103.\n\nAssisted-by: Google Antigravity (Gemini 4 Neon)\nAssisted-by: Codex (GPT-5.6-sol)
bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 26, 2026
Validate WaitFrameEventX's signaled-index target as committed, unguarded, and writable without touching its pages. Replace ancillary and paced wait handle vectors with fixed stack storage, avoid the scheduled-handle Arc clone on the paced path, and use deques for bounded frame history eviction.\n\nAdapted from xcl-dev#2692.\nPart of #103.\n\nAssisted-by: Google Antigravity (Gemini 4 Neon)\nAssisted-by: Codex (GPT-5.6-sol)
@upipibi

upipibi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Source read of 9a547e2, no run. writable (frame.rs:303) drops the coherent-window shortcut that readable has (gpu/shader.rs:215, in_committed_window). If the title's signaled pointer is in a committed coherent window and its page is not in the working set, resident_and_writable returns None and writable calls VirtualQuery. Per uma.rs:381-382 that walks the whole window, about 40 ms a call on 4 GiB. Before this PR, readable returned at once for that pointer. The same in_committed_window check before the working-set query (the window is read-write as a whole, kernelx.rs:673) would keep note_signaled off that path. I did not trace whether a title's signaled pointer actually falls in a committed window, so the cost is the code path, not a measured slowdown.

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

…cations in WaitFrameEventX

`note_signaled` previously guarded its `i32` write with `gpu::shader::readable(out, 4)`, which only checks that pages are readable. A pointer to `PAGE_READONLY`, `PAGE_EXECUTE_READ`, or a cross-page straddle ending on a read-only page passed the guard and faulted on write.

- Add `writable(p, len)` (via `writable_with_uma`) matching `gpu::shader::readable`'s `Uma::in_committed_window(p, len)` fast path (so non-resident pages in a 4 GiB committed coherent window never fall through to a slow kernel `VirtualQuery` walk, per @upipibi's review), followed by a `K32QueryWorkingSetEx` fast path (`resident_and_writable`) and `VirtualQuery` fallback, and use it in `note_signaled` so read-only, guard, no-access, unmapped, and cross-page non-writable targets are safely skipped without touching pages.
- Replace `ancillary`'s `Vec<HANDLE>` with a stack-allocated `AncillaryHandles` (`[HANDLE; 63]` + `len`) and replace the paced `WaitFrameEventX` wait-handle `Vec` with a `[HANDLE; 64]` stack array, removing both per-frame heap allocations on the default paced path.
- Only `Arc::clone` the scheduled ancillary handles inside `WaitFrameEventX` when `XCL_FRAME_INTERVAL_US` is set to uncapped (`Some(0)`).
- Switch `ISSUED` and `FLIPPED` ring buffers from `Vec` (`remove(0)` / `drain(..n)`) to `VecDeque` (`pop_front()` / `push_back(...)`) for O(1) eviction.
- Add unit tests covering `writable` / `note_signaled` (read-only, resident read-only, RW->RO straddle, `PAGE_GUARD`, `PAGE_EXECUTE_READ`, `PAGE_NOACCESS`, reserved-only), the committed coherent UMA window fast path and boundary fallthrough, `AncillaryHandles` boundary cases, and `ISSUED`/`FLIPPED` FIFO eviction order.

Part of xcl-dev#103
Assisted-by: Google Antigravity (Gemini 4 Neon)
@Decodere
Decodere force-pushed the fix-frame-note-signaled-writable-and-zero-alloc branch from 9a547e2 to 4f9c7ff Compare November 26, 2026 21:11
@Decodere

Copy link
Copy Markdown
Contributor Author

Good catch, @upipibi — updated in 4f9c7ff6:

  • Factored writable(p, len) through writable_with_uma(uma, p, len) and restored gpu::shader::readable's Uma::in_committed_window(p, len) fast path right after the null/zero/oversize/overflow guards and before resident_and_writable / VirtualQuery, keeping committed coherent UMA windows off the kernel VirtualQuery path while falling through on cross-window straddles.
  • Added writable_uses_committed_window_shortcut_and_falls_through_on_boundary covering untouched non-resident pages inside a local Uma::with_windows(1, 0x2_0000) window, a PAGE_GUARD shortcut proof (confirming Some(&u) short-circuits before resident_and_writable/VirtualQuery without touching the guard page while None rejects), guard checks, and end-of-window straddle fallthrough.
  • Re-verified on RTX 4060 (Win11): cargo clippy --all-targets -- -A clippy::approx_constant (0 new warnings in frame.rs), 3x Debug WARP & 3x Release RTX 4060 hle::d3d12x::frame::tests:: (9/0/0, 0 skipped), full release workspace (1439/0/47), and paired 60 s smokes vs exact parent b49be183 (1618 vs 1604 frames, 60 60 60, 0 rem/crash, 14011 vs 13978 MB).

bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 27, 2026
Validate WaitFrameEventX's signaled-index target as committed, unguarded, and writable without touching its pages. Replace ancillary and paced wait handle vectors with fixed stack storage, avoid the scheduled-handle Arc clone on the paced path, and use deques for bounded frame history eviction.\n\nAdapted from xcl-dev#2692.\nPart of #103.\n\nAssisted-by: Google Antigravity (Gemini 4 Neon)\nAssisted-by: Codex (GPT-5.6-sol)
bmjask pushed a commit to bmjask/xcl that referenced this pull request Nov 27, 2026
Validate WaitFrameEventX's signaled-index target as committed, unguarded, and writable without touching its pages. Replace ancillary and paced wait handle vectors with fixed stack storage, avoid the scheduled-handle Arc clone on the paced path, and use deques for bounded frame history eviction.

Adapted from xcl-dev#2692.
Part of #103.

Assisted-by: Google Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-5.6-sol)
Prime1997 pushed a commit that referenced this pull request Nov 28, 2026
Validate WaitFrameEventX's signaled-index target as committed, unguarded, and writable without touching its pages. Replace ancillary and paced wait handle vectors with fixed stack storage, avoid the scheduled-handle Arc clone on the paced path, and use deques for bounded frame history eviction.

Adapted from #2692.
Part of #103.

Assisted-by: Google Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-5.6-sol)
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

@github-actions github-actions Bot added the stale No activity for 3 days; closed 4 days later unless something happens label Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 4f9c7ff

Main commit 8aba55b ("Frame clock: remove paced wait allocations and validate writes", "Adapted from #2692") contains this change in frame.rs: is_writable_protect / resident_and_writable / writable, note_signaled using writable, AncillaryHandles, the fixed hs array in wait_frame_event_x, the scheduled-extra clone only when uncapped, and VecDeque for ISSUED / FLIPPED, with tests.
The one part main left out is the writable_with_uma shortcut through Uma::in_committed_window. As your own test shows, that shortcut returns true for a PAGE_GUARD page inside a coherent window. If it is still wanted, it needs its own small PR that says why.
Recommendation: close (superseded by main commit 8aba55b).

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@throny

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Closing as superseded: this change is already on main (8aba55b). main frame.rs has writable() check in note_signaled, AncillaryHandles stack array, VecDeque ISSUED/FLIPPED/ORIGINS and the Arc clone only when uncapped; same changes as the PR

Two independent checks found this: Foniass's close list on #1607 and ours against current main. If something here is still missing on main, say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

@throny throny added the duplicate This issue or pull request already exists label Dec 1, 2026
@throny throny closed this Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants