D3D12.X: flush tied texture downloads on SetEventOnCompletion and preserve u64::MAX removal #3389
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3389"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T12:40:41Z.
What changed
crates/xcl/src/hle/d3d12x/real.rs:native_fence_set_event_on_completionto invoke the underlying PCID3D12Fence::SetEventOnCompletionwithout re-entering XCL's hook for internal XCL wake events.fence_set_event_on_completionandfence_wait_firedso both blocking (event == NULL) and event-based (event != NULL) title waits flush and wait (bounded, with immediate exit on device removalu64::MAX) for GPU-to-CPU texture downloads tied to that fence/value before returning or signaling the title's event, dropping the cloned fence reference beforeSetEvent(w.title).sparse::readbacks_async()ortitle_textures::fence_event_intercept_needed()is true.crates/xcl/src/hle/d3d12x/title_textures.rs:FENCE_DOWNLOADS_CV,fence_event_intercept_needed, andwait_for_fence_downloads/wait_for_fence_downloads_with(notifyingFENCE_DOWNLOADS_CVonfinish_tieand bothcomplete_fence_downloadscompletion paths, and usingReleaseordering onTEXTURES_LEN/GPU_WRITTENupdates).safe_fence_valueto returnu64::MAXimmediately on device removal without poisoningSAFE_FENCE_VALUESor withholding removal behind pending downloads.PendingDownload::armandtie_downloadsevent registration toreal::native_fence_set_event_on_completion.safe_fence_value_preserves_device_removal_while_downloads_are_pending,wait_for_fence_downloads_waits_for_active_processing_and_exits_on_device_loss,fence_set_event_on_completion_publishes_gpu_texture_downloads).crates/xcl/src/hle/d3d12x/fence_x.rs:FenceTable::refresh_oneto publishu64::MAXto mirrored fence cells immediately on device removal even while texture downloads are pending.fence_x::armregistration toreal::native_fence_set_event_on_completion.refresh_publishes_device_removal_even_while_downloads_are_pending.Why
GetCompletedValueandfence_xmirror refreshes, not onID3D12Fence::SetEventOnCompletion(neither blockingevent == NULLnor event-drivenWaitForSingleObject). A title synchronizing GPU texture writes viaSetEventOnCompletioncould wake and read stale CPU texture memory before the tied download landed.GetCompletedValue() == u64::MAXwhile a texture download tie or processing pass was active, bothtitle_textures::safe_fence_valueandfence_x::FenceTable::refresh_onewithheldu64::MAXbehindcomplete_fence_downloads, potentially stalling title fence polls instead of surfacing device loss immediately.fence_xandtitle_textureswent through the hookedSetEventOnCompletionvtable entry, causing unnecessary wrapper event / threadpool indirection when event interception was active.Tested
Environment: Windows 11 Pro (Build 26200), AMD Ryzen 9 9950X, NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), rustc 1.99.0, DXC 1.9.2609.5.
cargo check --workspace --all-targets: passed (0 errors, 0 new warnings).cargo test --workspace --no-runandcargo build --release -p xcl: passed.cargo test -p xcl --lib "hle::d3d12x::fence_x::tests::" "hle::d3d12x::title_textures::tests::"filtered to the 13 fence/download tests): 13 passed, 0 failed (including all 4 new CPU and real-GPU tests coveringSetEventOnCompletion(NULL), post-signal and wait-before-signal events, native bypass, active-processing wait, andu64::MAXdevice-removal propagation).cargo test --workspace -- --test-threads=4,XCL_DXC_DIRset to DXC 1.9.2609.5): passed, exit 0, 0 failures.daae6fd2(note: 3 earlier runs hit the pre-existingmaintitle watchdog crash duringxcl-fences::write_changed_pageswith 0 threads in our modified paths and 0 download-wait warnings; run 4 completed cleanly with 1765 frames, 0 removals, 0 crashes):Main-ancestor baseline on the same machine (
f0e8fdd5):Not tested
DXGI_ERROR_DEVICE_REMOVEDevent during an active multi-megabyte texture download (theu64::MAXremoval paths are verified by synthetic CPU tests).AI use
Used an AI coding assistant to investigate the fence/download paths, draft the patch and synthetic CPU/GPU tests, and run local verification; all changes and test outputs were reviewed and verified on local hardware.
Imported from ivall/xcl. Originally posted by saintwolf on 2026-11-29T14:20:56Z.
Reviewed and tested at
daae6fd2.Code review findings
fence_wait_fired(crates/xcl/src/hle/d3d12x/real.rs): Convertingctxvialet mut w = Box::from_raw(ctx as *mut FenceWait);at the top offence_wait_fired— before thewhile w.wait.load(Ordering::Acquire) == 0spinloop — creates a uniqueBox<FenceWait>whilefence_set_event_on_completionon the calling thread may still be concurrently writing(*w).wait.store(wh as usize, Ordering::Release)through the raw pointer. The original code deliberately keptwas*mut FenceWaituntil afterwh != 0andUnregisterWaitEx, only callingdrop(Box::from_raw(w))at the very end. Please keep raw-pointer access ((*w).fence.take()etc.) until after the spinloop finishes.CreateEventWoverhead on every non-nullSetEventOnCompletion(crates/xcl/src/hle/d3d12x/title_textures.rs):fence_event_intercept_needed()returnstruewheneverTEXTURES_LEN != 0, which is permanently true as soon as any title texture is registered. That forces every non-nullSetEventOnCompletioncall in the title to allocate a Win32CreateEventW, clone anID3D12Fence, heap-allocateBox<FenceWait>, and go throughRegisterWaitForSingleObject/ thread pool even whenGPU_WRITTEN == 0and no downloads exist.Tested locally
32.0.16.2140), AMD Ryzen 7 7800X3D,rustc 1.100.0.cargo test -p xcl --lib hle::d3d12x::fence_x::tests::refresh_publishes_device_removal -- --test-threads=1: 1 passed, 0 failed.cargo test -p xcl --lib hle::d3d12x::title_textures::tests::safe_fence_value_preserves_device_removal -- --test-threads=1: 1 passed, 0 failed.cargo test -p xcl --lib hle::d3d12x::title_textures::tests::wait_for_fence_downloads_waits_for_active_processing -- --test-threads=1: 1 passed, 0 failed.cargo test -p xcl --lib hle::d3d12x::title_textures::tests::fence_set_event_on_completion_publishes_gpu_texture_downloads -- --test-threads=1: 1 passed, 0 failed.AI use
Imported from ivall/xcl. Originally posted by andrewDoing on 2026-11-29T19:33:08Z.
I checked the blocking
SetEventOnCompletion(value, NULL)path with a real D3D12 texture readback and a late destination on RTX 5070. I tested main0fe06eb0and the same main with the three-file PR delta applied (headdaae6fd2, GitHub merge delta7a649b0e..f68cd29b). Native GPU completion was already 1 before each title wait. The CPU destination was reserved-only, and a separate thread committed it on the controlled schedule without making any new GPU submission.The short-delay case confirms the bounded retry can publish a tied download without another GPU submission. In the timeout case, after committing the address I saw zero expected texels before a manual retry and all 4,096 after it. The timeout still returns native success, so code relying on that return alone cannot distinguish it from the published case.
cargo test --offline --locked -p xcl --lib -- gpu_blocking_fence_wait_with_late_texture_destination --test-threads=1 --nocapture: one pass per case, zero skips. RTX 5070 12 GB (616.92), Ryzen 7 5700X3D, Windows 11 26300.10075, Rust 1.100.0, debug. The test covers the blocking path and synthetic destination timing. It does not exercise thread-pool event interception or a retail title. Codex with GPT-6.2 Sol adapted and ran the fixture.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T03:28:01Z.
Review (head
e348eb0d, diff read, not a merge-check: thePR descriptioncheck fails, so not built or run here yet):daae6fd2, not the heade348eb0d, which is whyPR descriptionfails. The description also says 3 of 4 runs crashed inxcl-fences::write_changed_pagesand calls that a pre-existing crash on main, but the main run shown is clean. Since this PR now drives that same publish path from title threads and thread-pool callbacks too, a main run that shows the crash (or a clean PR run) is needed to rule it out.real.rs~11931,title_textures.rs~4186-4190), which is nearly always in a game. Every title wait with an event then costs an extra event, a thread-pool wait and a thread hop, including XCL's own waits on title fences (cpu_writes.rs~74).Err), each wait on that fence holds a title thread or pool worker for up to 2 s, redoing the readback and re-layout about every 1 ms, then fires the event with stale data anyway (title_textures.rs~4199-4240). On main event waits weren't delayed. A bounded single retry, or firing at once and logging, would avoid that.tie_downloadsalso wakes thexcl-fencesthread, which publishes the same downloads concurrently. It skips withprintln!instead ofskip_test.real.rs~11952), so a wait whose value is never reached keeps the fence and its device alive.u64::MAXfix is independent of the download flush and could be its own PR. AI use names no tool or model, and Tested has no pass/fail/ignored counts.Fine: the
u64::MAX(device removed) handling is right and its unit tests fail on main. No lock-order inversion found. The native SetEventOnCompletion helper followsnative_fence_completed, and the fence reference is dropped before the title's event is set.AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T15:47:49Z.
Test report (not a merge-check:
PR descriptionred)base:
b2b1f109d2head:
e348eb0dc2merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2906 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: not repeated here; my review of this same head is at https://github.com/xcl-dev/xcl/pull/3389#issuecomment-7299734300, and its findings stand. This run adds what reading couldn't: whether the change merges, builds and passes the whole suite on current main.
performance: no smoke run.
assessment: merges, builds and passes the whole suite on current main; the findings in my review stand
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5
AI-written and posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T03:13:26Z.
Review: COMMENTED
Review of
e348eb0d: 4 finding(s). 4 are inline.These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T03:13:27Z.
Code review location:
crates/xcl/src/hle/d3d12x/title_textures.rs; line 4186fence_event_intercept_needed()is true almost all the time, because GPU_WRITTEN only goes up and TEXTURES_LEN != 0 once any texture is registered. So every title SetEventOnCompletion(value, event) now takes the wrapper path (CreateEventW + RegisterWaitForSingleObject + fence AddRef/Release), and the title's event is set from a thread-pool callback, even when no download is tied to that fence. Before, this only happened with readbacks_async(). It also changes behaviour: SetEventOnCompletion on an already-completed value followed by WaitForSingleObject(ev, 0) now returns WAIT_TIMEOUT, where native D3D12 sets the event synchronously. The smoke run's 1639 vs 1695 frames and +2.7 GB peak commit may be related. Intercepting only when a download is actually tied to that fence would avoid it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T03:13:27Z.
Code review location:
crates/xcl/src/hle/d3d12x/real.rs; line 11931Whether to intercept is decided once, when SetEventOnCompletion is registered, so the fix misses a common order. At startup (TEXTURES_LEN == 0, nothing pending) the title calls SetEventOnCompletion(v, ev), then registers and GPU-writes a texture, then
queue->Signal(fence, v)ties a download. The native event fires as soon as the GPU reaches v, and the title reads stale CPU texture memory, which is the bug this PR fixes.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T03:13:27Z.
Code review location:
crates/xcl/src/hle/d3d12x/title_textures.rs; line 4237If publishing keeps failing (
write_changed_pagesreturns false orpublish_downloaderrs, e.g. the title protected or unmapped a page),wait_for_fence_downloads_withcallscomplete_fence_downloadsabout every 1 ms for the full 2 s. Each pass redoes the readback Map/copy/reverse-layout/compare and logs another warning. A blocking SetEventOnCompletion(v, NULL) then burns a core for 2 s, writes up to ~2000 warnings, and stalls the title thread 2 s on every such wait, every frame if the title waits each frame. Before, GetCompletedValue just withheld the value cheaply.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T03:13:27Z.
Code review location:
crates/xcl/src/hle/d3d12x/real.rs; line 11906fence_wait_firednow blocks its thread-pool worker for up to 2 s inwait_for_fence_downloads, but the wait is registered without WT_EXECUTELONGFUNCTION (~11962). With several frame or async-compute waits in flight while downloads are slow, each callback holds a default pool worker the pool doesn't know is long-running. Other wait callbacks, including XCL's own, queue behind them, so title events are delayed far more than the download time.Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T19:39:37Z.
Closing: hot-path intercept blocking thread-pool workers up to 2 s with review findings and
xcl-fencescrashes in smoke.Pull request closed