Skip to content

D3D12.X: a resource's last Release waits for the queues to pass the work in flight - #866

Merged
theaustinayers merged 1 commit into
xcl-dev:mainfrom
theaustinayers:late-release
Nov 24, 2026
Merged

theaustinayers merged 1 commit into
xcl-dev:mainfrom
theaustinayers:late-release

Conversation

@theaustinayers

Copy link
Copy Markdown
Collaborator

The title's final Release of a resource goes straight to D3D12 today. A retail title did that while a queue still had work on the resource: it counts its own finished frames, but XCL's submits are not the title's (texture uploads go out with a later submit, tile mappings on XCL's own queue). With the debug layer on, the run stopped during loading with CORRUPTION: An ID3D12Resource object … is referenced by GPU operations in-flight on Command Queue (id 921) on the title's render thread.

XCL now keeps the last reference until every queue it knows of has passed a fence value signalled after the release. With nothing in flight the resource is still gone when Release returns.

What is in it

  • crates/xcl/src/hle/d3d12x/late_release.rs (new): batches of held resources, a fence value a queue for each batch, release once all queues are past it; a thread looks every 4 ms; the Release itself waits up to 300 µs so that idle queues let the resource go at once. Two tests (one on the GPU: a queue made to stand still keeps the resource held, and it goes once the queue moves).
  • real.rs res_release: the last reference goes to late_release::hold instead of D3D12; XCL's own bookkeeping for the resource is dropped then, as before.
  • present.rs note_execute: a drain with every submit.
  • sparse.rs queue(), title_textures.rs upload_queues(): the queues of XCL's own, for the fence values.
  • XCL_LATE_RELEASE=0 turns it off.

Tested

  • NVIDIA GeForce RTX 5090 (driver 621.14), Ryzen 9 9950X3D, Windows 11 build 26300, rustc 1.100.0, debug build for tests, release for the runs
  • cargo build --workspace, then cargo test --workspace -- --test-threads=4: 715 passed, 0 failed, 41 ignored; no SKIPPED tests on this adapter
  • An earlier version of this change (no wait in Release; resources only let go by the thread) failed readback_routing_tests::coherent_buffer_sanitizes_console_flags_and_state in the whole suite, twice out of two runs: VirtualFree of memory under an imported heap right after the last Release of its placed resource. That is why Release waits for idle queues; the suite passes with it.
  • A retail title with XCL_D3D12_DEBUG=1, on a build of main at beaa245 plus an earlier version of this change (the thread only, without the 300 µs wait): before, one run of two stopped at 50.7 s with id 921; with the change, no id 921 in two runs, and 356 last releases were held and all 356 released by 74 s.

Regression run

main:

xcl-smoke v1
commit: beaa245ca04e212f064efc7cafcdce104bc5c0a2
gpu: NVIDIA GeForce RTX 5090
title: NYCMain
seconds: 60
exit: 0x0
frames shown: 1616 (PresentX 1616)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17204 MB

PR:

xcl-smoke v1
commit: e7da8a18afd45d41d37ec44056b5b21bc68b4727
gpu: NVIDIA GeForce RTX 5090
title: NYCMain
seconds: 60
exit: 0x0
frames shown: 1623 (PresentX 1623)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16397 MB

Not tested

  • AMD and Intel GPUs, Wine / vkd3d-proton.
  • The head commit itself with the debug layer on the retail title: the id 921 runs above were made with the earlier version (no wait in Release). The 60 s regression run above is of the head commit, and does not get as far as the title's world.
  • The title still loses its device later in the world for another reason (the debug layer names a command list executed after its allocator was reset, id 541, after a queue had stood still for seconds). This PR does not change that.
  • Heaps and other object kinds: only resources are held.
  • The remaining debug-layer reports about tile mappings while another queue writes (id 1047).

AI use

  • Claude Code, Claude Fable 5.2: found the cause from the debug layer's report, wrote the code, the tests and this description. The runs above were made on the hardware named.

🤖 Generated with Claude Code

…ork in flight

A retail title's final Release of a resource went straight to D3D12 while a
queue still had work on it: the title counts its own finished frames, but
XCL's submits are not the title's (texture uploads go out with a later submit,
tile mappings on XCL's own queue). The debug layer stopped the run with
"CORRUPTION: An ID3D12Resource object is referenced by GPU operations
in-flight on Command Queue" (id 921) on the title's render thread.

The last reference is now held: each batch of held resources gets a fence
value signalled on every queue XCL knows of (the title's, the tile-mapping
one, the upload ones) and is released once all of them have passed it.

With no work in flight the queues pass that value at once, and the Release
itself waits up to 300 us for it: the resource is gone when it returns, as
before (a title, and a test here, frees the memory under an imported heap
right after the last Release of its resource - held, the pages were still
pinned). Otherwise a thread of its own lets the batch go, looking every 4 ms.
XCL's own bookkeeping for the resource is dropped at the title's last
Release, as before.

Measured on the retail title with the debug layer on (RTX 5090): id 921 no
longer reported; 356 last releases held and all 356 released by 74 s.

XCL_LATE_RELEASE=0 turns it off.

Assisted-by: Claude Code (Claude Fable 5.2)
@theaustinayers
theaustinayers merged commit a1672ef into xcl-dev:main Nov 24, 2026
2 checks passed
@ryosen0

ryosen0 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Windows / RTX 3060 Ti 8 GB cross-check of e7da8a1 (Ryzen 7 9800X3D, driver 32.0.16.1742, Windows 11, release) on the other retail title, including a run into the loading phase where your id 921 fired:

Official smoke:

xcl-smoke v1
commit: e7da8a18afd45d41d37ec44056b5b21bc68b4727
gpu: NVIDIA GeForce RTX 3060 Ti
title: ScarlettGame
seconds: 90
exit: 0x0
frames shown: 2450 (PresentX 2450)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14497 MB

Plus a 150 s run with scripted presses (25/35/45/55/65/80/95/110/125/140 s) that reaches the loading screens and the title's texture-upload churn (~4096 uploads left out, ≈1.75 GB of CPU writes through the sparse windows, 16k+ tiles mapped): clean, 4171 frames, 60 presents / 2 s throughout, 0 removals, 0 crashes. No debug layer here, so this shows the hold path doesn't disturb normal pacing on a second host — not that id 921 is gone (only a debug-layer run can show that).

Tests on this head: cargo test --release -p xcl --lib late_release -- --test-threads=1 → 2 passed / 0 failed (both, including the GPU one — the queue-standstill hold and release).

One question from the 8 GB side: this title's load phase intermittently ends in a device removal here (~1 in 6 runs, 0x887a0006, also seen by others on #541). Since your change holds resources past the title's own frame counting, do you expect any interaction with resources the title releases because it is shedding memory under pressure — i.e. could the hold delay a release the title relies on to free VRAM promptly? The 300 µs wait plus the 4 ms drain thread suggest the delay is bounded and tiny, but on a card at its budget that is the kind of thing worth a sentence in the description if you have a view.

AI use: GPT-6.2 Astra (API model selector) in DSH built the head, ran the smoke, the 150 s run and the tests, and drafted this comment; the human contributor reviewed the numbers before posting.

heretic1321 added a commit to heretic1321/xcl that referenced this pull request Nov 24, 2026
Part of xcl-dev#248. Re-scoped onto upstream/main after xcl-dev#288 and xcl-dev#866: drops xcl-dev#269 overlap retirement (covered by xcl-dev#288) and unsafe mid-device MADE eviction (D3D12 descriptors hold no COM reference). Scopes real::MADE by device, returns an owned COM ID3D12Resource from resource_at held across descriptor creation, releases device-owned MADE entries and title_textures state on dev_release (routed through xcl-dev#866 late_release), clears released queues from vram::Pages::waited, and adds title-texture counts to XCL_ACTIVITY.

Assisted-by: T3 Code / Antigravity (gemini-pro-orchestrator-neon)
CalvyPZ pushed a commit to CalvyPZ/xcl that referenced this pull request Nov 24, 2026
`coherent_buffer_sanitizes_console_flags_and_state` failed now and then in the whole suite
(`VirtualFree` returned 0 at its end) and passed alone. The device it makes is the one other
tests adopted: D3D12 has one device per adapter. On that device its placed resource is XCL's,
and since e7da8a1 (xcl-dev#866) a last `Release` is held until the queues XCL knows of are past their
work; `Release` waits 300 us for that. With other tests' work in flight the resource goes a few
ms later, and until then the imported heap pins the pages: `VirtualFree` fails with
ERROR_INVALID_PARAMETER.

The test now frees the memory with `free_imported`, which retries `VirtualFree` until the held
release has gone (10 s at most). `imported_heap_memory_frees_after_a_held_release` makes the
case on purpose: an adopted device, a known queue that waits on a fence, so the release is held
and `VirtualFree` fails; then the queue goes on and the memory is freed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants