Skip to content

D3D12.X: a native host's last Release only queues the resource - #2445

Open
Ahmet-Dedeler wants to merge 3 commits into
xcl-dev:mainfrom
Ahmet-Dedeler:d3d12x/release-native-host
Open

Ahmet-Dedeler wants to merge 3 commits into
xcl-dev:mainfrom
Ahmet-Dedeler:d3d12x/release-native-host

Conversation

@Ahmet-Dedeler

@Ahmet-Dedeler Ahmet-Dedeler commented Nov 25, 2026 •

Copy link
Copy Markdown

Stacked on #1789; only the top two commits belong here (D3D12.X: a resource's last Release forgets its address in one function and D3D12.X: a native host's last Release only queues the resource). The bottom one is #1789's commit, @vakaobr's, rebased onto current main (#1789's base predates a change to res_release that the second commit builds on). Part of #258.

Apple's D3DMetal calls Release on threads of its own (its queue thread at submit) and from inside calls XCL makes while XCL holds a lock of its own. When that is the title's last Release and late release keeps the object, res_release took the maps' locks, the sparse state's and the texture table's, and late_release::hold signalled the queues and waited. Whatever the host's thread waits for there, its queue waits for too. On a native host, such a Release now only writes the resource into a ring of atomics and returns. The late-release thread forgets what XCL keeps by the address and then lets the object go. This is the ring from mac7's fd1e593, which @vakaobr asked to see opened on main in #258 so his host-thread part can go on top. That part (no host Release at all on a host thread) is not in this PR.

The gate needs no other open PR. class_for already calls module_of on the table's first method. When that is 0 (the host's D3D12 is in no PE module), it sets NATIVE_HOST once and starts the late-release thread there, on the adopting thread. D3D12 on Windows and vkd3d-proton are PE modules. There the flag is never set, the ring stays empty (drain_known reads two atomics) and a Release does what it did.

What is in it

  • crates/xcl/src/hle/d3d12x/real.rs: the body of res_release that forgets a released resource moves into forget_released, unchanged (first commit, no behaviour change).
  • crates/xcl/src/hle/d3d12x/real.rs: NATIVE_HOST, set in class_for from module_of(...) == 0. With it set, a kept last Release calls late_release::hand_over and returns 0. Tests only_a_table_in_no_pe_module_makes_a_native_host and a_kept_last_release_on_a_native_host_waits_for_no_lock.
  • crates/xcl/src/hle/d3d12x/late_release.rs:
    • Ring (16,384 slots; the indices only grow; a writer claims an index with one compare-exchange, only while it is less than N past the tail, so a claimed slot is always one the reader has emptied; one reader at a time, which empties a slot before it moves the tail).
    • hand_over (atomics only; with the ring full the reference is kept for good and counted, never the locking path).
    • drain_known takes the ring first: it forgets each resource, then holds it like any other.
    • start_thread is split out of hold.
    • Ring tests: in order, a full ring, 4 writers × 50,000 against 2 readers, and 300 writers let go by one barrier against a ring with 257 free slots (exactly 257 claimed, nothing in the ring overwritten).

Tested

  • MacBook Pro, Apple M5 Max (40-core GPU), 64 GB, macOS 27.3, CrossOver 26.3 (bottle with D3DMetal). Rust 1.100.0, x86_64-pc-windows-gnu (mingw-w64 GCC 16.2.0), debug, DXC 1.9.2609.
  • The repository's CI workflow (Windows runner, no GPU) on this head 6df8f64, run on my fork because it waits for approval here: Build and test passed.
  • cargo build --workspace --target x86_64-pc-windows-gnu at each of the two commits: builds, no new warnings (the same 3 as main).
  • Each test in its own process under Wine (<xcl lib test exe> --exact <test> --test-threads=1): passed:
    • the 6 late_release tests: the 4 new ring tests (on 6df8f64), plus a_held_resource_goes_when_the_queue_is_past_its_batch and without_queues_a_batch_goes_at_once;
    • the 2 new real::native_host_tests;
    • title_textures::tests::gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes;
    • sparse::tests::a_buffer_released_while_the_state_is_held_does_not_wait_for_it.
  • a_kept_last_release_on_a_native_host_waits_for_no_lock with the hand_over branch taken out of res_release fails: "the last Release waited for a lock another thread held: Timeout".
  • In this bottle the test device's tables are in d3d12.dll, so the gate test checked the PE-host side. The native path ran through the test's own switch (native_host_tests::FORCED, a thread-local read only after the flag).

Regression run

From this Mac, scripts/smoke.ps1 unchanged under PowerShell, with a wrapper as -Xcl that starts xcl.exe under Wine, both from the same copy of the caches. On this host main shows no frame (the title stops about 3 s in, before it creates its device), so neither run reaches a title's Release: this pair shows that the build starts and ends as main does.

main:

xcl-smoke v1
commit: 3ceb5507a05355719ff1d7b5c29bc52aff142000
gpu: AMD Compatibility Mode
title: 21222967
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

PR:

xcl-smoke v1
commit: 6df8f647253b3e2cb071543afce1248be3a4f08f
gpu: AMD Compatibility Mode
title: 21222967
seconds: 60
exit: 0x0
frames shown: 0 (PresentX 0)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 0 MB

(peak commit is 0 because Wine reports no commit charge.)

Not tested

  • Windows and the whole suite. On a PE host this only adds a flag that stays false and an empty ring.
  • A retail title with this branch on main, and the gate on a real D3DMetal table in a test. (In this bottle XCL's own device goes through d3d12.dll, a PE module, so the gate test saw that side. In earlier retail runs on this Mac the tables of resources, queues, lists and fences were at addresses in no module, PC vtable 0x21a6… in the log, which is what the gate keys on.) Without D3D12.X: preserve native host vtable prefixes and tails #1904, main does not get far on D3DMetal. On mac7, the loading runs I have predate this change.
  • Linux Wine.

AI use

  • Claude Code with Claude Opus 5.5 (port and tests), from code Claude Mythos 5.2 wrote for the Mac port.

vakaobr and others added 2 commits November 25, 2026 21:42
D3DMetal calls Release on adopted resources from a native thread of its
own at submit time.  That thread has no Wine TEB, so the first-time
std::env::var read inside the OnceLock in late_release::enabled() enters
Wine's ntdll and never returns.

Reading the setting once in adopt(), on the Wine thread that creates the
object, makes sure the OnceLock is already set before the host's thread
can reach it.

Part of xcl-dev#258.

Assisted-by: Claude Code (Claude Opus 4.6 (1M context))
The steps that drop what XCL keeps by a released resource's address (the maps, sparse pins,
title textures, tiled textures, its unified-memory block) move from res_release into
real::forget_released, unchanged and in the same order. The next commit calls it from the
late-release thread as well. No change in behaviour.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@SlopTart

Copy link
Copy Markdown

gpu_ suite (cold cache): 160 passed / 4 failed, no crashes. The four are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, depth-buffer state refusal, and gpu_promoted_uav_clear_values_pass_create_committed_resource (report); none caused by this PR.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1: Ring::push has a concurrent reservation race. late_release.rs:172-179 checks head-tail and then separately fetch_adds tail; RING_WRITERS=256 reserves space only under a bounded simultaneous-writer assumption, but no such bound is enforced. For example with N=512 and used=255, 258 writers can all observe occupancy below N-256 before incrementing; the last claim reaches tail+N and writes slots[tail % N] before the reader consumes it, overwriting a pending resource. That loses its pointer/reference and leaves its XCL address state unforgotten. The new 4-writer stress test cannot cover this. Please use CAS reservation/a bounded MPMC queue or enforce the writer bound, with a barrier test exceeding the reserve.

@fromthemoonx

fromthemoonx commented Nov 25, 2026 •

Copy link
Copy Markdown

Rerun on the new head 6df8f64: passes on NVIDIA, and the ring tests are stable. The new writers_at_once_past_the_free_room_overwrite_nothing catches the old check-then-fetch_add push, but only rarely here: 1 of 340 runs.

result
cargo test --workspace --no-fail-fast -- --test-threads=4, 6df8f64 merged onto main e1d84b7 (clean) 1429 passed, 0 failed, 46 ignored; 25 SKIPPED lines
same at the head 6df8f64 1352 passed, 1 failed, 46 ignored: a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, from the base 3ceb550 (fixed on main by c828d8c), not this PR
writers_at_once_past_the_free_room_overwrite_nothing, writers_and_readers_at_once_…, a_full_ring_…, alone, 40 runs each 40 of 40 each
late_release:: + real::native_host_tests:: (8 tests), --test-threads=8, 40 runs 40 of 40
the new test with push put back as a check followed by fetch_add (scratch) failed 1 of 340 runs: exactly the free slots are claimed, left 258, right 257

So the new test can catch the race, but on an 8-core Ryzen it almost never does: the 300 writers rarely reach the check in the same window. A test that holds writers between the check and the claim (or loops the barrier round many times in one run) would fail reliably. The compare-exchange itself looks right to me by reading.


First run, on 6979e9f: Windows run: passed on NVIDIA with the whole suite, the ring and native-host tests were stable under repeats, and on Windows the gate stays off (the e2e title's log has no native-host line, and its debug-layer messages are the same as main's). This covers the "Windows and the whole suite" item under Not tested.

Tested: 6979e9f, and the same commit merged onto main e0d93c8 (clean; real.rs auto-merged). RTX 3060 12 GB (driver 610.88), Ryzen 7 5700X, Windows 11 Pro 26300, Rust 1.100.0 MSVC (x86_64-pc-windows-msvc), debug, Graphics Tools (D3D12 debug layer) installed.

result
cargo test --workspace --no-fail-fast -- --test-threads=4, merged 1426 passed, 0 failed, 46 ignored; 25 SKIPPED lines (no wave64, refused per-lane forms)
same at the head 6979e9f 1351 passed, 1 failed, 46 ignored: a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, because the base 3ceb550 predates its fix on main (c828d8c); not this PR
each of the 3 new late_release ring tests and the 2 native_host_tests, alone, 40 runs each 40 of 40 each
hle::d3d12x::late_release:: + hle::d3d12x::real:: (66 tests), --test-threads=8, 30 runs 30 of 30
e2e test_title_draws_with_a_root_signature_under_the_debug_layer, merged vs main e0d93c8 the same messages on both: 3× error id 93, 1× error id 1046, 1× warning id 820, 8× id 0 at exit; the host's D3D12 is in no PE module appears in neither log

Not tested: a title on Windows with this branch, a release build, AMD / Intel, Wine / D3DMetal (nothing here).

AI use: Claude Code (Claude Opus 5.5) ran the tests and the debug-layer comparison, and wrote this report.

Apple's D3DMetal (CrossOver, the Game Porting Toolkit) calls Release on threads of its own (its
queue thread at submit, for a barrier's resource) and from inside calls XCL makes while XCL
holds a lock of its own. When that Release is the title's last one and late release keeps the
object, res_release took the maps' locks, the sparse state's and the texture table's, and
late_release::hold signalled the queues and waited. Whatever the host's thread waits for there,
its queue waits for too. (The one such wait seen in a retail run on the Mac port, sparse's
forget_buffer, no longer waits since xcl-dev#1981; the others are the same shape and are not waited
for here either.)

On a host whose D3D12 is in no PE module (class_for: module_of of the table's first method is
0, noted once when the first such table is adopted) a kept last Release now writes the resource
into a ring of atomics (late_release::hand_over) and returns: no lock, no fence, no allocation,
no thread start, no environment read (xcl-dev#1789 reads XCL_LATE_RELEASE at adoption). The next
drain_known, on the late-release thread (started at adoption on such a host) or a submit,
forgets what XCL keeps by the address (real::forget_released, the old body of res_release) and
holds the object like any other; the address cannot come again before that, the object is still
there. With the ring full (16,384 slots) the reference is kept for good and counted.

D3D12 on Windows and vkd3d-proton are PE modules: there the flag is never set, the ring stays
empty (drain_known reads two atomics) and a Release does what it did.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ahmet-Dedeler

Copy link
Copy Markdown
Author

@bmjask you're right, thanks. The check and the fetch_add were two steps, and nothing bounded how many writers could pass the check at once.

Fixed in 6df8f64 (squashed into the ring commit): Ring::push now claims an index with one compare_exchange_weak on head, and only while head - tail < N, so writers arriving at once can't claim past the tail together. The tail is still stored only after the reader has emptied the slot, so a claimed slot is always an empty one. RING_WRITERS and its margin are gone, and the full ring is exactly N slots now.

New test writers_at_once_past_the_free_room_overwrite_nothing: a 512-slot ring with 255 entries, then 300 writers released by one barrier. It checks that exactly 257 are accepted, that the 255 already in the ring come out unchanged and in order, and that every accepted resource comes out once. With the old push that's more writers than the 256 reserve. The other three ring tests pass on the new head too (under Wine, --exact, one process each), and the description has the smoke block for 6df8f64.

vakaobr added a commit to vakaobr/xcl that referenced this pull request Nov 26, 2026
@DavidM1911

Copy link
Copy Markdown
Contributor

Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

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