Skip to content

D3D12.X: drain present queue after Present before ResizeBuffers - #711

Open
RandomGithub12309 wants to merge 1 commit into
xcl-dev:mainfrom
RandomGithub12309:d3d12x/drain-present-before-resize
Open

RandomGithub12309 wants to merge 1 commit into
xcl-dev:mainfrom
RandomGithub12309:d3d12x/drain-present-before-resize

Conversation

@RandomGithub12309

@RandomGithub12309 RandomGithub12309 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fix a pre-existing swap-chain synchronization bug on main (surfaced during review of #674; isolated here from #674's triple-buffering change) where q.Signal(&p.fence, p.value) ran before sc.Present(0, DXGI_PRESENT(0)) and ensure_swapchain called sc.ResizeBuffers without draining the present queue first.

Because DXGI's Present submits internal GPU queue work on the swap-chain back buffer after p.fence was signaled, waiting on p.value did not wait for Present to retire. When ResizeBuffers ran (or when present_now moved to another queue), old back-buffer allocations could be freed while still referenced by DXGI's in-flight Present work (D3D12 error 921 / OBJECT_DELETED_WHILE_STILL_IN_USE).

What is in it

  • crates/xcl/src/hle/d3d12x/present.rs:
    • Extract present_frame_on from present_now and move p.value += 1; q.Signal(&p.fence, p.value) after sc.Present(0, DXGI_PRESENT(0)) so p.fence orders behind DXGI's Present queue work (while still signaling when Present returns an error so the submitted copy command list remains fenced).
    • Add drain_queue(p, timeout_ms) to signal a new p.fence value on p.queue, wait via fence::wait, and clear p.copy_source and p.dumps; extract drain_and_reset_for_queue(slot, queue, timeout_ms) for cross-queue switch draining, retaining the old presenter when a drain fails with a non-Removed error (e.g. timeout) so in-flight swap-chain, copy_source, and dump resources stay alive until the GPU finishes.
    • Call drain_queue(p, 2000) before sc.ResizeBuffers(2, ...) in ensure_swapchain (preserving p.disabled |= ... on device removal) and drain_and_reset_for_queue before dropping old on queue switches in present_now.
    • Add FRAME_DUMP_TEST_LOCK and unit/GPU regression tests covering multi-resize + frame dumps (present_and_resize_with_frame_dumps_drains_queue_cleanly), cross-queue switch draining (queue_switch_drains_old_presenter_before_recreating_swapchain), and drain/queue-move timeout retention and ResizeBuffers error recovery (drain_queue_and_resize_error_paths_preserve_in_flight_state).

Tested

  • Hardware: NVIDIA GeForce RTX 5050 Laptop GPU, Intel Core Ultra 7 255HX, Windows 11 Build 26300, rustc 1.100.0 (0ef1bc386 2026-11-09) stable-x86_64-pc-windows-msvc (debug for unit tests, release for smoke.ps1).
  • cargo test -p xcl --lib hle::d3d12x::present -- --test-threads=1 --nocapture at f98dc20: 9 passed, 0 failed, 0 ignored (0 skipped).
  • cargo test --workspace -- --test-threads=4 --skip fuzz_validate_and_gpu at f98dc20: xcl lib 940 passed, 0 failed, 25 ignored (SKIPPED on this adapter: wave64-only GPU tests and pso_measurement_compiles_an_exact_synthetic_record because Windows Developer Mode is off; gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_{0..3} excluded due to pre-existing 0x8007000E CreateComputePipelineState OOM on this adapter).
  • Game checked: 60 s scripts/smoke.ps1 release build at f98dc20 vs main (8e87fc7).

Regression run

xcl-smoke v1
commit: 8e87fc7b62a87ea589ccb9ea6182f514016cf7db
gpu: NVIDIA GeForce RTX 5050 Laptop GPU
title: e61570a3
seconds: 60
exit: 0x0
frames shown: 1603 (PresentX 1603)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14543 MB
xcl-smoke v1
commit: f98dc204d358723e497259e6fd3c8203f4aa146c
gpu: NVIDIA GeForce RTX 5050 Laptop GPU
title: e61570a3
seconds: 60
exit: 0x0
frames shown: 1596 (PresentX 1596)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14538 MB

Not tested

  • D3D12 debug layer on this host (d3d12SDKLayers.dll / Graphics Tools not installed; 0x887A002D), AMD/Intel/wave64 GPUs, Wine/Linux, and gameplay beyond the 60 s smoke window.

AI use

  • Antigravity (Gemini 4 Neon) wrote the initial fix, addressed the queue-move drain timeout review comments, rebased onto main, ran the tests and scripts/smoke.ps1 on the hardware above, and updated this description.

@thehollowedone

Copy link
Copy Markdown

Tested

I reran the synthetic frame-capture fixture from #674 on the standalone fix 091a941, with the D3D12 debug layer active.

  • One DIRECT queue: 28 presents through three ResizeBuffers calls, with zero D3D12 validation errors.
  • Two DIRECT queues on the same device: two resizes, then a switch to the second queue at the final size change. XCL logged that the swap chain moved to the second queue and created the replacement. All 28 presents completed with zero D3D12 validation errors.

In each run, all 14 BMP captures matched their expected dimensions and all 250,944 sampled pixels matched. Both scratch tests passed 1/1 with no skips. Runtime code was stock.

Windows 11 build 26300, Ryzen 7 9800X3D, RTX 3080 Ti / 621.35, Rust 1.99.1 MSVC, debug. cargo test --offline --locked -p xcl --lib hle::d3d12x::present::scratch_resize::check -- --exact --nocapture --test-threads=1 with XCL_FRAME_DUMP=2.

Not tested

Queue-drain timeout, device removal, and retail performance.

AI use

Codex with GPT-6.2 Sol adapted the scratch fixture, ran the tests, and drafted this report.

@iggyvilla

Copy link
Copy Markdown
Contributor

Read of e21f158 (nothing built or run by me).

Overlap with #674. This is the drain / signal-after-Present part of #674 (present_frame_on, drain_queue, the drain before ResizeBuffers), on the two-buffer presenter. A tree merge of the two heads conflicts in present.rs, so they cannot both land as they stand. If #674 goes first, what #711 has that #674 does not is the |= on p.disabled after a failed drain (present.rs:605, :619; #674 assigns with = at :631, which can clear the disabled that a failed Signal just set) and the test helpers. If #711 goes first, #674 has to drop its copies of the drain hunks.

Queue move. drain_and_reset_for_queue ignores the result of drain_queue(old, 1000) and then sets *slot = None (:657-667). On a timeout that releases the swap chain with a Present still queued, which is the error 921 case this PR exists to remove. Returning false without dropping old when the drain fails with anything but Removed (the p.queue != queue guard then skips that frame) would close it; the same point is on #674.

Tests. assert_no_debug_errors does nothing without the D3D12 debug layer (the PR says 0x887A002D on the author's machine), so a green run shows the fence/drain order but not the 921 itself; a run on a machine with Graphics Tools installed, named in the report, would show it.

AI use: Claude Code, Claude Sonnet 5.5, read the code at the head above; the points are mine.

1 similar comment
@iggyvilla

Copy link
Copy Markdown
Contributor

Read of e21f158 (nothing built or run by me).

Overlap with #674. This is the drain / signal-after-Present part of #674 (present_frame_on, drain_queue, the drain before ResizeBuffers), on the two-buffer presenter. A tree merge of the two heads conflicts in present.rs, so they cannot both land as they stand. If #674 goes first, what #711 has that #674 does not is the |= on p.disabled after a failed drain (present.rs:605, :619; #674 assigns with = at :631, which can clear the disabled that a failed Signal just set) and the test helpers. If #711 goes first, #674 has to drop its copies of the drain hunks.

Queue move. drain_and_reset_for_queue ignores the result of drain_queue(old, 1000) and then sets *slot = None (:657-667). On a timeout that releases the swap chain with a Present still queued, which is the error 921 case this PR exists to remove. Returning false without dropping old when the drain fails with anything but Removed (the p.queue != queue guard then skips that frame) would close it; the same point is on #674.

Tests. assert_no_debug_errors does nothing without the D3D12 debug layer (the PR says 0x887A002D on the author's machine), so a green run shows the fence/drain order but not the 921 itself; a run on a machine with Graphics Tools installed, named in the report, would show it.

AI use: Claude Code, Claude Sonnet 5.5, read the code at the head above; the points are mine.

@gut0to gut0to 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.

AI-assisted review (GPT-6): I verified this finding against the queue wait and presenter-owned resource lifetimes in the current diff.

Comment thread crates/xcl/src/hle/d3d12x/present.rs Outdated
return false;
}
if let Some(old) = slot.as_mut() {
let _ = drain_queue(old, 1000);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could we keep the old presenter until this drain succeeds? If the wait times out, the queue may still be executing the copy or present, but this code drops the presenter immediately, including copy_source and pending dump resources. Those D3D12 resources must stay alive until the GPU finishes using them. Please retain the presenter after a timeout and reset the slot only after a successful drain. AI-assisted review: I checked this path against drain_queue and the presenter-owned resource lifetimes.

Previously present_now signaled p.fence before sc.Present(0, DXGI_PRESENT(0)), leaving DXGI's internal Present queue work unordered behind p.fence. When ensure_swapchain called sc.ResizeBuffers or present_now moved queues, old swap-chain back buffers could be freed while still in use (D3D12 error 921).

Extract present_frame_on so q.Signal runs after sc.Present (while still fencing submitted copy command lists on Present failure), extract drain_and_reset_for_queue, add drain_queue to wait for all in-flight copy and Present work before ResizeBuffers and queue switches, handle ResizeBuffers device removal via fence::api_error, and add regression tests for dimension/format resizes, queue switches, timeouts, and ResizeBuffers error recovery.

Assisted-by: Antigravity (Gemini 4 Neon)
RandomGithub12309 added a commit to RandomGithub12309/xcl that referenced this pull request Nov 25, 2026
Rotate three command allocators, command lists, and swap-chain buffers in Presenter so PresentX no longer blocks on the previous frame's fence every frame. Stacked on xcl-dev#711.
@RandomGithub12309
RandomGithub12309 force-pushed the d3d12x/drain-present-before-resize branch from e21f158 to f98dc20 Compare November 25, 2026 00:56
@RandomGithub12309

RandomGithub12309 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Updated at f98dc20 (rebased onto main 8e87fc7):

@JJAutoCompare

Copy link
Copy Markdown

Empirical test report (f98dc20)

Hardware / environment

  • PR D3D12.X: drain present queue after Present before ResizeBuffers #711 head f98dc204d358723e497259e6fd3c8203f4aa146c, detached and clean. Windows 11 Enterprise Evaluation x64, build 26200, non-interactive Session 0, AMD Ryzen 7 3800X.
  • Intel Arc A770, driver 32.0.101.9175. The WARP case explicitly uses the software adapter. The HWND cases use an automatic adapter selection with fallback, despite XCL_ADAPTER=A770.
  • Rust 1.100.0 (0ef1bc386), x86_64-pc-windows-msvc, unoptimized test profile with debuginfo and incremental compilation disabled. DXC 1.9.2609.5 (01b62ad47).

Commands and results

  • cargo test --workspace --no-run exited 0 outside the Arc lock. This compiled the test binaries without running tests.
  • Ran xcl-77aca8ccb20e299f.exe --exact <test> --test-threads=1 --format terse once per test. Each name below starts with hle::d3d12x::present::frame_dump_tests::.
    • Outside the lock, XCL_ADAPTER=NONE_CPU_ONLY: reports_without_frame_dumps_remain_readable exited 0. 1 passed, 0 failed, 0 ignored, 0 skipped.
    • Inside Invoke-ArcTest.ps1 -Test, XCL_ADAPTER=A770 and RUST_TEST_NOCAPTURE=1: mapped_frame_dump_distinguishes_dark_bright_and_undecoded_content exited 0. 1 passed, 0 failed, 0 ignored, 0 skipped.
    • In that same wrapper, present_and_resize_with_frame_dumps_drains_queue_cleanly, queue_switch_drains_old_presenter_before_recreating_swapchain, and drain_queue_and_resize_error_paths_preserve_in_flight_state each exited 0. The wrapper exited 0.
  • Log totals: libtest reported 5 passed, 0 failed, 0 ignored. Three of those passes emitted a case-sensitive SKIPPED line, leaving 2 tests that ran without skipping.

Observed behavior

All three HWND tests skipped at CreateSwapChainForHwnd with 0x887A0022. The resize, queue-switch, and error-path assertions were not reached.

Not tested

Full GPU workspace suite, interactive desktop presentation, and game smoke runs.

AI use: Verified with T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high). Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against pr711-focused.log and published.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

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.

6 participants