Skip to content

Recompiler: keep a draw's descriptor heaps alive until the list has run - #553

Open
us3r40456 wants to merge 3 commits into
xcl-dev:mainfrom
us3r40456:recompiler-draw-heap-lifetime
Open

us3r40456 wants to merge 3 commits into
xcl-dev:mainfrom
us3r40456:recompiler-draw-heap-lifetime

Conversation

@us3r40456

@us3r40456 us3r40456 commented Nov 24, 2026 •

Copy link
Copy Markdown

Draws with no textures or samplers released their descriptor heaps right after SetDescriptorHeaps, before the command list ran. On D3DMetal (CrossOver on Apple Silicon) such a list never completes: this is the draw hang reported for Apple Silicon in #42.

What is in it

  • draw_indexed_on binds its heaps with bind_views in every case and keeps them until the fence wait. The no-texture case went through bind_resources(...).map(|_| Vec::new()), which dropped the Bound and its heaps at once, and D3D12 command lists do not keep the heaps they bind alive. bind_views already handles the empty case (2 CBV/SRV/UAV slots, 1 sampler slot).
  • gpu_draw_keeps_its_descriptor_heaps (Kalmarv's test from the report below, co-authored): a plain HLSL VS / PS triangle through runner::draw, gated only on an adapter and DXC, so it runs on the CI runner's WARP, where the seven draw tests skip.

Tested

  • Kalmarv's Windows runs (below): without this change, a draw on WARP removes the device (0x887A0020) or faults, and every later test on the runner's device then fails (gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426's CI failure). With it, the draw passes 3 of 3, and the lib suite on main b7aa9f3 has 723 passed, 0 failed on WARP and on an RTX 5070 Ti.
  • gpu_draw_keeps_its_descriptor_heaps on D3DMetal (Apple M5 Pro): 3 of 3 passed with the change; no result after 60 s without it.
  • Apple M5 Pro (20-core GPU), macOS 27.2.1, CrossOver 26.3.0.39832 (wine-11.0-8726-g2e2f5fca349) with D3DMetal 3.0; Rust 1.100.0, cross-built with cargo-xwin 0.24.1, debug; DXC 1.9.2609 (XCL_DXC_DIR). Unit-test binary run under CrossOver's wine.
  • The 7 draw tests that hung (gpu_graphics_vs_ps_draw, gpu_rect_list_corners, gpu_rect_list_flat_outputs, gpu_rect_list_fullscreen_order, gpu_wave64_ps_spill_slots, gpu_wave64_ps_whole_wave_reductions, gpu_wave64_vs_spill_slots_and_identity), one per process with --exact: on this PR (be2b06c), 7 passed, about 0.5 s each. On main 508a0c7, gpu_graphics_vs_ps_draw has no result after 60 s; on 2b4bb43 (same runner.rs) none of the 7 had a result after 2 minutes.
  • gpu::recomp (200 tests) with this change on 2b4bb43 (runner.rs, tests.rs and fence.rs are unchanged on 508a0c7): 160 passed (2 of them print SKIPPED: no wave64), 22 failed, 18 ignored. Without it: 153 passed, 22 failed, 7 hung, 18 ignored. The 22 failures are the same tests in both runs (FP64, OpenExistingHeapFromAddress and the others listed in GPU test results: NVIDIA RTX 3080 (wave32) and AMD RDNA2 iGPU (no mesh shaders) #42).
  • tests/gpu_pso_e2e.rs: 7 passed; tests/console_shaders.rs: 3 passed, 4 ignored. Without XCL_SAMPLE_RPF / XCL_SAMPLE_DXC they return early, so they do not draw here.

Regression run
Not run: I have no Xbox title to run scripts/smoke.ps1 on. runner.rs is under crates/xcl/src, but the changed function is only reached from tests (runner::draw, draw_on, draw_on_multi); the runtime uses only adapter(), wave64_supported() and graphics_wave32() from this file.

Not tested

  • Windows.
  • The rest of cargo test --workspace.
  • gpu::recomp in one process with --test-threads=4 did not finish: after 139 results, gpu_unified_memory_arena (fails on D3DMetal with E_NOTIMPL) and three other tests stayed "running for over 60 seconds" at 100 % CPU. One per process, the other three pass. Not investigated; I could not compare it without this change, because the draw tests hang first there.

AI use

  • Claude Code, Claude Opus 5.5: found the cause, wrote the change and this description, and ran the tests on this machine.

For a draw with no textures or samplers, draw_indexed_on bound its
CBV/SRV/UAV and sampler heaps through
`bind_resources(...).map(|_| Vec::new())`, which drops the returned
`Bound`, and the heaps with it, right after SetDescriptorHeaps. D3D12
command lists hold no reference to the heaps they bind, so the list was
executed with released heaps.

On D3DMetal (CrossOver 26.3, Apple M5 Pro) such a list never completes:
the fence wait times out after 10 s, then GetDeviceRemovedReason blocks,
so the seven graphics tests that draw without textures hang
(gpu_graphics_vs_ps_draw, the three gpu_rect_list_* tests and the three
gpu_wave64_{ps,vs}_* tests).

bind_views handles the empty case the same way (two CBV/SRV/UAV slots,
one sampler slot) and returns the heaps, so every draw now goes through
it and keeps them until the fence wait. The compute paths already keep
theirs.
@us3r40456

Copy link
Copy Markdown
Author

Overlap with #239: this is the same empty-draw heap fix as in #239 (draw_indexed_on keeping the heaps that bind_resources(...).map(|_| Vec::new()) dropped), on its own, without the device-removal changes that #239's review asks to revise. On D3DMetal (Apple M5 Pro) it is what makes the 7 draw tests stop hanging (see Tested). I missed #239 before opening this; if you'd rather take the fix through #239, I'll close this one.

@thehollowedone

Copy link
Copy Markdown

Tested

The seven draw tests listed in the PR description passed on both exact parent 508a0c7 and PR head be2b06c on native Windows D3D12. Each revision had 7 passed, 0 failed, 0 ignored, and no reported skips or timeouts. The CrossOver hang did not occur here.

RTX 3080 Ti (621.35), Ryzen 7 9800X3D, Windows 11 build 26300, Rust 1.99.1 MSVC, debug. I built each revision separately with cargo test --offline --locked -p xcl --lib --no-run, then ran each test in its own process with --exact gpu::recomp::tests::<name> --nocapture, XCL_ADAPTER=3080 Ti, and a 15-second timeout.

Not tested

D3DMetal, the D3D12 debug layer, full workspace, and retail titles.

AI use

Codex with GPT-6.2 Sol ran the tests and drafted this report.

@eizispark

Copy link
Copy Markdown

Hardware check of be2b06c99b0f83773ecd3dc867c71a6cc9cf12bc (verified clean merge with main 9ce2070): pass.

Tested

  • Hardware: AMD Radeon RX 9070 XT (RDNA4, driver 32.0.32015.16001), AMD Ryzen 7 9800X3D, 32 GB RAM, Windows 11, rustc 1.100.0 (0ef1bc386 2026-11-09), release + test profile
  • cargo test --workspace -- --test-threads=4: 680 passed, 0 failed, 41 ignored (GPU tests skipped on this adapter: none)
  • All 7 empty-texture draw tests in gpu::recomp::tests (gpu_graphics_vs_ps_draw, gpu_rect_list_corners, gpu_rect_list_flat_outputs, gpu_rect_list_fullscreen_order, gpu_wave64_ps_spill_slots, gpu_wave64_ps_whole_wave_reductions, gpu_wave64_vs_spill_slots_and_identity): 7 passed

Regression run
main (9ce2070):

xcl-smoke v1
commit: 9ce2070eccf436d7bd38601c90407da447df34f3
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1213 (PresentX 1213)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15615 MB

PR (be2b06c):

xcl-smoke v1
commit: be2b06c99b0f83773ecd3dc867c71a6cc9cf12bc
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1571 (PresentX 1571)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14749 MB

Not tested

  • wave32-only GPUs (NVIDIA / Intel); D3DMetal on macOS; gameplay past the menus (runner::draw_indexed_on is exercised by the GPU test suite above rather than retail title runs)

AI use

  • Antigravity (Gemini 4 Carbon) ran the build, test suite, and scripts/smoke.ps1 harness on this machine and formatted this report.

@us3r40456

Copy link
Copy Markdown
Author

@ivall: could this one get the test-only label (#507)? The diff is in runner.rs, but only in draw_indexed_on, which nothing outside the tests reaches (runner::draw, draw_on and draw_on_multi are called from gpu/recomp/tests.rs and tests/ only, still so on main 47efa2e). That is why there is no xcl-smoke pair: I have no Xbox title to make one with.

What it has instead: the Windows runs above, on an RTX 3080 Ti and on an RX 9070 XT (full suite, 680 passed, 0 failed), and on D3DMetal it is what stops the 7 draw tests from hanging. It still merges cleanly onto main 47efa2e.

@shortside

Copy link
Copy Markdown

Test report from a second Mac, head be2b06c against its merge base 508a0c7.

Tested

  • MacBook Pro M5 Pro, macOS 27.2.1, Rosetta 2; CrossOver 26.3 (Wine 11.0), Windows 10 bottle, D3DMetal; DXC 1.9.2609. Cross-built for x86_64-pc-windows-msvc with clang-cl / lld-link 23.1.6 and an xwin SDK 10.0.26100, Rust 1.100.0, debug. The lib test binary run under Wine, one test per process (--exact --nocapture, 60 s cap).
  • The 7 draw tests (gpu_graphics_vs_ps_draw, gpu_rect_list_corners, gpu_rect_list_flat_outputs, gpu_rect_list_fullscreen_order, gpu_wave64_ps_spill_slots, gpu_wave64_ps_whole_wave_reductions, gpu_wave64_vs_spill_slots_and_identity): base 7 of 7 with no result at the 60 s cap; head 7 of 7 passed.
  • Cause seen independently on main 9aec53d: a macOS sample of the hung process shows the test thread waiting inside D3DMetal while D3DMetal's own "Async Queue" thread sits in a fault handler; the queue completes when the SetDescriptorHeaps heaps are kept alive, which is this change.

Not tested

  • Windows, the rest of the suite, any title.

AI use

  • Claude Code with Claude Fable 5.2 built both commits, ran the tests and wrote this report.

@Lucasyudi

Copy link
Copy Markdown

Test report from a second Apple silicon Mac (different chip and toolchain), plus an independent bisection that lands on the same cause.

Tested

  • Apple M4 Pro, macOS 27.2.1, CrossOver 26.3 with D3DMetal, Rosetta 2. Cross-built for x86_64-pc-windows-gnu with mingw-w64 (GCC 16.2.0), Rust 1.100.0, debug, DXC 1.9.2609. Your diff applied on main 47efa2e (it applies cleanly; runner.rs there is the same around the change). Lib test binary under CrossOver's wine, one test per process, 60 s cap.
  • 8 tests that hang on main here, all ok with the diff (about 0.5 s each): the 7 in your list plus gpu_ps_p0_reads_flat_integer_selectors, which goes through the same runner::draw with no textures and hung in my sweep on 64b3f35 too.
  • Controls: gpu_mesh_mode_two_passes passes, gpu_ps_image_sample_offset_implicit_lod_between_mips fails at the same assertion as on main (that one is Tests: implicit-LOD sample offset test uses s_wqm and the host's LOD #982).

Bisection, before I found this PR (on main 47efa2e, a temporary test that reads the VS / PS HLSL from files and draws through runner::draw, with switches in draw_indexed_on):

  • Trivial hand-written pass-through HLSL (no recompiler output, no wave ops, no UAV access) still hangs, so it is not the translated shaders.
  • Skipping the draw call but keeping the bind_resources(...).map(|_| Vec::new()) heaps: still hangs. Skipping the root UAVs or the final CopyTextureRegion: still hangs.
  • Skipping only bind_resources (no SetDescriptorHeaps): completes, with the expected pixels.
  • Markers in wait_for_submission / fence::wait: Signal returns, GetCompletedValue stays 0, the 10 s WaitForSingleObject times out, then GetDeviceRemovedReason never returns, the same thing the GPU test results: NVIDIA RTX 3080 (wave32) and AMD RDNA2 iGPU (no mesh shaders) #42 notes describe.

Not tested

  • Your exact head be2b06c (I applied the diff on a newer main), Windows, the rest of the suite.

AI-assisted (Claude Code, Claude Opus 5.5): bisected, built and ran the tests, wrote this report.

@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Windows test report for be2b06c against its base 508a0c7: on WARP (the software adapter the CI runner has) a draw through runner::draw removes the device or crashes the test process on the base and passes on this PR. On an RTX 5070 Ti nothing changes. We ran into the same line from the other side today: it was the cause of #1426's CI failure at 065aef3.

Tested

  • Windows 11 Pro 26200, AMD Ryzen 9 9950X; WARP (XCL_ADAPTER=warp, d3d10warp.dll 10.0.26100.9711) and NVIDIA GeForce RTX 5070 Ti 16 GB (driver 610.74); inbox D3D12Core 10.0.26100.9549; DXC 1.9.2609 (XCL_DXC_DIR); Rust 1.99.0, debug.
  • Test binary from cargo test -p xcl --lib --no-run at 508a0c7 and at be2b06c. Each test in its own process, <exe> --exact gpu::recomp::tests::<name> --test-threads=1, 3 runs each.
  • The 7 draw tests of the description, WARP: all 7 print SKIPPED ...: no wave64 on both commits, 3 of 3 runs. Each of them skips on an adapter that runs neither 64- nor 32-lane waves, so on WARP they never reach the draw. That is why CI has not seen this bug.
  • The 7 draw tests, RTX 5070 Ti: 7 passed on 508a0c7 and 7 passed on be2b06c, 3 of 3 runs each. The released heaps go unnoticed on this driver.
  • One more test, added locally on both commits so that a draw does run on WARP (gpu_draw_keeps_its_descriptor_heaps, below: a plain HLSL VS/PS triangle through runner::draw, gated only on an adapter and DXC):
WARP RTX 5070 Ti
508a0c7 run 1 and 2: draw: D3D12 device removed (0x887a0020 ...); run 3: access violation, the process exits without a test result passed, 3 of 3
be2b06c passed, 3 of 3 passed, 3 of 3
  • After a device removal every later test in the same process that uses the runner's device fails with 0x887A0005, so one such draw takes the rest of a CI run with it (seen on gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426: 6 failures from 2 draws).
  • The lib suite at be2b06c as it is (no local test), <exe> --test-threads=4:
    • WARP: 640 passed, 0 failed, 21 ignored; 83 SKIPPED (no wave64).
    • RTX 5070 Ti: 640 passed, 0 failed, 21 ignored; 2 SKIPPED (no wave64).
  • The same one-line change on today's main b7aa9f3, with the test below: WARP 723 passed, 0 failed, 21 ignored (206 SKIPPED, no wave64); RTX 5070 Ti 723 passed, 0 failed, 21 ignored (2 SKIPPED). The test alone without the change on b7aa9f3, WARP, 3 runs: one access violation, two device removed (0x887a0020).
  • With the D3D12 debug layer on WARP (on gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426's tree, which has the same runner.rs line): access violation in 3 of 3 runs before the change, 0 messages in 3 runs with it.

Not tested

  • scripts/smoke.ps1: the changed function is only reached from tests, as the description says.
  • The other test binaries of cargo test --workspace (tests/gpu_pso_e2e.rs, tests/console_shaders.rs) and scripts/test.ps1.
  • AMD and Intel adapters, macOS / CrossOver.
  • be2b06c rebased onto today's main (it is 1 commit on 508a0c7; the line is unchanged on b7aa9f3, where the same change was tested as above).

Follow-up offered
The 7 draw tests cannot catch this on CI because they skip on WARP. The test we used does run there, and fails on the base. If you want it in this PR, take it as it is (it goes into crates/xcl/src/gpu/recomp/tests.rs); otherwise we can send it as a separate PR after this one merges.

/// `runner::draw` without textures, on any adapter with DXC (plain HLSL VS/PS, no wave size
/// needed, so also the CI runner's WARP): a triangle over the whole target, every pixel its
/// colour. The draw's descriptor heaps have to live until the command list has run. They were
/// released right after `SetDescriptorHeaps`, which a hardware driver did not notice; WARP
/// removed the device (0x887A0020) or faulted in the test process.
#[test]
fn gpu_draw_keeps_its_descriptor_heaps() {
    if runner::adapter().is_err() || super::dxc::location().is_err() {
        return runner::skip_test(if runner::adapter().is_err() { "no GPU" } else { "no DXC" });
    }
    use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32B32A32_FLOAT;
    let vs_hlsl = "struct V { float4 pos : SV_Position; float4 color : COLOR0; };\nV main(float4 pos : POSITION, float4 color : COLOR0) { V o; o.pos = pos; o.color = color; return o; }\n";
    let ps_hlsl = "float4 main(float4 pos : SV_Position, float4 color : COLOR0) : SV_Target { return color; }\n";
    let vs = super::dxc::compile(vs_hlsl, "main", "vs_6_6", &[]).unwrap();
    let ps = super::dxc::compile(ps_hlsl, "main", "ps_6_6", &[]).unwrap();
    let layout = [
        runner::VertexElement { semantic: "POSITION", index: 0, format: DXGI_FORMAT_R32G32B32A32_FLOAT, offset: 0 },
        runner::VertexElement { semantic: "COLOR", index: 0, format: DXGI_FORMAT_R32G32B32A32_FLOAT, offset: 16 },
    ];
    let color = [0.25f32, 0.5, 0.75, 1.0];
    let v: Vec<f32> = [[-1.0f32, -1.0], [-1.0, 3.0], [3.0, -1.0]].iter().flat_map(|&[x, y]| [x, y, 0.0, 1.0, color[0], color[1], color[2], color[3]]).collect();
    // a few draws: each makes and binds its own heaps
    for run in 0..4 {
        let px = runner::draw(&vs, &ps, &layout, &v, 32, &[], &[], 8, [0.0; 4]).unwrap();
        assert!(px.len() == 64 && px.iter().all(|p| *p == color), "run {run}: {px:?}");
    }
}

AI use: Claude Code (Claude Fable 5.2, Claude Opus 5.5)

us3r40456 and others added 2 commits November 24, 2026 18:03
…pter

The seven draw tests skip on an adapter that runs neither 64- nor 32-lane
waves, so on the CI runner's WARP no draw reached the released heaps.
gpu_draw_keeps_its_descriptor_heaps draws a plain HLSL VS / PS triangle
through runner::draw four times, gated only on an adapter and DXC.

Without the heap fix it removes the device (0x887A0020) or faults on WARP
(Kalmarv's runs on xcl-dev#553) and has no result after 60 s on D3DMetal (Apple
M5 Pro, CrossOver 26.3); with it, it passes on both, 3 of 3 runs.

Co-authored-by: Kalmarv <59708937+Kalmarv@users.noreply.github.com>
At the end of tests.rs it conflicted with the tests main has added there
since 508a0c7.
@iLucasRT

Copy link
Copy Markdown

Test report for this PR (bbbb8de, its diff on main 9e3831e), and a related gap in the same function, with a patch on top of this PR in case you want to take it.

Tested

  • AMD Radeon RX 7900 XTX 24 GB (RDNA3, native wave64), driver 32.0.32029.2010, Ryzen 7 5800X3D, 64 GB RAM, Windows 11 Pro 26300, Rust 1.100.0 MSVC, DXC 1.9.2602.17, debug. XCL_SAMPLE_RPF, XCL_SAMPLE_DLLS and XCL_SAMPLE_BVH4 set.
  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4:
tree XCL_HEAP_WINDOWS=1 variable unset
main 9e3831e 773 passed, 3 failed 776 passed, 0 failed
+ this PR 774 passed, 3 failed (the same 3) not run
+ this PR + the patch below 777 passed, 0 failed 777 passed, 0 failed

41 ignored in every run. SKIPPED: none with the variable; heap_window_out_of_memory_falls_back_to_the_switch_form without it. gpu_draw_keeps_its_descriptor_heaps passes.

The gap (not caused by this PR): since #680, with XCL_HEAP_WINDOWS=1 a pipeline built by the backend's hook reads the memory windows through descriptors MEM_HEAP_BASE.. of the bound heap. draw_indexed_on binds one without them, and the textures of the textured test sit in those slots, so three gpu_pso_e2e tests (they need XCL_SAMPLE_RPF, so CI does not run them) keep the clear colour: retail_vs_ps_through_the_pso_hook_render_and_read_back, retail_pipelines_built_in_the_background_behind_a_stand_in, retail_textured_ps_decoded_descriptors_and_a_descriptor_change_between_draws. run_in_region and dispatch_with already make those views. It is a test-harness gap, not a driver problem.

The change is small and touches the line this PR rewrites, so here it is on top of it: the raw views of mem first, the textures after them, and only for draws of hook-built pipelines (draw_on_multi). With heap windows off nothing changes. Take it, change it or leave it for later, whatever suits you. The same for plain main, as a commit: https://github.com/iLucasRT/xcl/tree/tests-heap-windows-draws (72b98dc).

Patch on top of this PR (+33 −10)
diff --git a/crates/xcl/src/gpu/recomp/runner.rs b/crates/xcl/src/gpu/recomp/runner.rs
index 92d9d88..54b310e 100644
--- a/crates/xcl/src/gpu/recomp/runner.rs
+++ b/crates/xcl/src/gpu/recomp/runner.rs
@@ -741,3 +741,3 @@ fn draw_gs_views(
         let draw = Draw { sgprs: sgprs.to_vec(), scissor: [0, 0, size as i32, size as i32] };
-        draw_indexed_on(dev, &pso, &rs, vertices, stride, indices, &[draw], mem, textures, samplers, size, clear)
+        draw_indexed_on(dev, &pso, &rs, vertices, stride, indices, &[draw], mem, textures, samplers, size, clear, 0)
     }
@@ -766,3 +766,3 @@ pub fn draw_on(
 /// A texture in D3D12 terms (e.g. decoded from a console T# by `gpu::desc`): SRV at
-/// descriptor heap index 2·i; `data` = the `MipLevels` levels one after the other, each in
+/// descriptor heap index `texture_heap_base()` + 2·i in `draw_on_multi`; `data` = the `MipLevels` levels one after the other, each in
 /// tightly packed rows (`row_bytes` for level 0, its width's share for the smaller ones).
@@ -782,4 +782,22 @@ pub struct Draw {
 
-/// Heaps with `textures` (SRV at 2·i) and `samplers` (at j), bound on `list`.
-unsafe fn bind_views(dev: &ID3D12Device, list: &ID3D12GraphicsCommandList, textures: &[ViewTexture], samplers: &[D3D12_SAMPLER_DESC]) -> Result<Vec<windows::core::IUnknown>, String> {
+/// Where the textures of `draw_on_multi` start in the view heap: after the memory windows'
+/// raw views when a PSO built by the backend's hook reads them there (`Options::heap_windows`).
+pub fn texture_heap_base() -> u32 {
+    if super::pso::heap_windows() { super::MEM_HEAP_BASE + 4 } else { 0 }
+}
+
+/// The raw views of `mem` for every window slot (see `dispatch_with`).
+unsafe fn window_views(dev: &ID3D12Device, heap: &ID3D12DescriptorHeap, mem: &ID3D12Resource, mem_bytes: u64) {
+    let inc = dev.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) as usize;
+    for k in 0..4usize {
+        let mut h = heap.GetCPUDescriptorHandleForHeapStart();
+        h.ptr += (super::MEM_HEAP_BASE as usize + k) * inc;
+        dev.CreateUnorderedAccessView(mem, None::<&ID3D12Resource>, Some(&raw_window_uav(mem_bytes)), h);
+    }
+}
+
+/// Heaps with `textures` (SRV at `base` + 2·i) and `samplers` (at j), bound on `list`; with
+/// `base` > 0 the memory windows' raw views of `mem` come first.
+unsafe fn bind_views(dev: &ID3D12Device, list: &ID3D12GraphicsCommandList, textures: &[ViewTexture], samplers: &[D3D12_SAMPLER_DESC], mem: &ID3D12Resource, mem_bytes: u64, base: u32) -> Result<Vec<windows::core::IUnknown>, String> {
+    let base = base as usize;
     let heap: ID3D12DescriptorHeap = dev
@@ -787,3 +805,3 @@ unsafe fn bind_views(dev: &ID3D12Device, list: &ID3D12GraphicsCommandList, textu
             Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
-            NumDescriptors: (2 * textures.len()).max(2) as u32,
+            NumDescriptors: (base + (2 * textures.len()).max(2)) as u32,
             Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
@@ -807,2 +825,5 @@ unsafe fn bind_views(dev: &ID3D12Device, list: &ID3D12GraphicsCommandList, textu
     let mut keep: Vec<windows::core::IUnknown> = vec![heap.cast().map_err(e)?, sheap.cast().map_err(e)?];
+    if base != 0 {
+        window_views(dev, &heap, mem, mem_bytes);
+    }
     for (i, t) in textures.iter().enumerate() {
@@ -837,3 +858,3 @@ unsafe fn bind_views(dev: &ID3D12Device, list: &ID3D12GraphicsCommandList, textu
         list.ResourceBarrier(&[transition(&r, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE)]);
-        dev.CreateShaderResourceView(&r, Some(&t.srv), at(&heap, 2 * i, inc));
+        dev.CreateShaderResourceView(&r, Some(&t.srv), at(&heap, base + 2 * i, inc));
         keep.push(r.cast().map_err(e)?);
@@ -862,6 +883,7 @@ pub fn draw_on_multi(
 ) -> Result<Vec<[f32; 4]>, String> {
-    draw_indexed_on(dev, pso, rs, vertices, stride, &[], draws, mem, textures, samplers, size, clear)
+    draw_indexed_on(dev, pso, rs, vertices, stride, &[], draws, mem, textures, samplers, size, clear, texture_heap_base())
 }
 
-/// `draw_on_multi`, the draws indexed when there are `indices` (one index buffer for all).
+/// `draw_on_multi`, the draws indexed when there are `indices` (one index buffer for all);
+/// `base`: where the textures start in the view heap (`texture_heap_base`).
 #[allow(clippy::too_many_arguments)]
@@ -880,2 +902,3 @@ fn draw_indexed_on(
     clear: [f32; 4],
+    base: u32,
 ) -> Result<Vec<[f32; 4]>, String> {
@@ -959,3 +982,3 @@ fn draw_indexed_on(
         // Keep the descriptor heaps alive until the list has run: D3D12 command lists do not hold a reference to them.
-        let _bound = bind_views(dev, &list, textures, samplers)?;
+        let _bound = bind_views(dev, &list, textures, samplers, &mem_buf, mem_bytes, base)?;
         list.SetGraphicsRootSignature(rs);
diff --git a/crates/xcl/tests/gpu_pso_e2e.rs b/crates/xcl/tests/gpu_pso_e2e.rs
index 6e3968f..3ca9c09 100644
--- a/crates/xcl/tests/gpu_pso_e2e.rs
+++ b/crates/xcl/tests/gpu_pso_e2e.rs
@@ -439,3 +439,3 @@ fn retail_textured_ps_decoded_descriptors_and_a_descriptor_change_between_draws(
         mem[table / 4..table / 4 + 4].copy_from_slice(&vsharp(0x1000));
-        mem[(table + 0x20) / 4..(table + 0x20) / 4 + 8].copy_from_slice(&desc::xcl_image_words(&t, heap_index));
+        mem[(table + 0x20) / 4..(table + 0x20) / 4 + 8].copy_from_slice(&desc::xcl_image_words(&t, runner::texture_heap_base() + heap_index));
         mem[(table + 0x40) / 4..(table + 0x40) / 4 + 4].copy_from_slice(&vsharp(0x2000));

Not tested

  • NVIDIA, Intel, WARP and wave32-only GPUs; release; a title. No test without game data covers the gap.

Own brain use: Double-checked code and tests. It works on my RX 7900 XTX but still needs a run on other GPUs (NVIDIA, Intel, RDNA2/RDNA4).
AI use: Claude Code (Claude Opus 5.5) found the cause, wrote the patch, ran the tests on this machine and wrote this report.

vakaobr added a commit to vakaobr/xcl that referenced this pull request Nov 24, 2026
…eate

D3DMetal calls Release on adopted resources from a native thread of its
own at submit time (4 per drain in the two tests below).  That thread has
no Wine TEB.  res_release takes XCL's mutexes and, for a last reference,
the late-release hold; uncontended, a std Mutex is a few atomics and gets
through, but a contended one waits in Wine's ntdll, which never returns
there.  This commit keeps all of that off the host's thread.

On a foreign thread the caller's Release does not run at all: the pointer
goes into a lock-free ring, and a Wine thread (the late-release worker, or
the next res_release on a Wine thread) runs it through res_release.
Checking for the last reference there instead (AddRef returning 2) is not
safe: a Release on another thread between that check and ours can take
the count to 0 on the host's thread.  The returned count is read with an
AddRef and its Release, which cannot reach 0 while the caller's reference
is still held.

The ring has one consumer at a time (a flag; a drain that finds it set
returns at once), and the consumer empties a slot before moving the tail
past it, so a producer reusing the slot never has its entry erased.  It
has 16,384 slots: every foreign Release goes through it, and a full ring
keeps the reference (the object leaks; the worker logs how many).

Detection: only on a macOS host (util::mac_host, read once at adoption),
so the path is off on Windows and on Wine on Linux.  There, a thread whose
stack pointer is at or above 0x7000_0000_0000 is a native thread.
Measured on an M5 Pro (CrossOver 26.3, Rosetta 2): Wine threads
0x0180ea9f..0x0220fb37, D3DMetal threads 0x7ff2063df667..0x7ff2066c3677.
The TEB pointer (gs:[0x30]) is not read: that read hangs on a D3DMetal
thread (measured).

Tested on main 85e3826 + xcl-dev#553 + ddf3be7: console_wave64_compute_through_
the_pso_hook_dispatch_and_read_back and gpu_mirror_upload_follows_cpu_
writes_and_later_mapping_changes hang without this series, and pass with
"read the late-release setting at adoption" alone and with the series.  No
test here hangs without this commit; it covers contention those tests do
not produce.

Part of xcl-dev#258.

Assisted-by: Claude Code (Claude Opus 4.6 wrote the first version; Claude Opus 5.5 rewrote the ring and the foreign Release after review by Ahmet-Dedeler)
@Toons29

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

Regression run of bbbb8def on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: bbbb8defa2d0d3be04a14a5ef032c0ae50ea61c9
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1454 (PresentX 1454)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14748 MB

Against the limits in CONTRIBUTING.md: 90 % of main's frames, 99 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

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.

8 participants