d3d12x: clear tracked viewports, scissors, and title index buffer on unbind #3366

Open
Kalmarv wants to merge 1 commit from upstream-pr/3366 into main
Kalmarv commented 2026-11-29 10:33:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T10:33:54Z.


What is in it

Clear stale tracked command-list bindings when a title explicitly unbinds viewports, scissor rects, or the index buffer in crates/xcl/src/hle/d3d12x/list_x.rs:

  • rs_set_viewports_x: when called with n == 0 or a null pointer, clear b.viewports instead of leaving the previous viewport array cached (and skip slicing the raw pointer for capture::op when n == 0). Previously, restore_bindings replayed the stale viewports onto fresh native command lists after KickoffX or mid-list splits.
  • rs_set_scissors_x and apply_scissors: when called with n == 0 or a null pointer on the direct path, or an empty rect slice on the deferred two-pass path, clear b.scissors (and skip slicing the raw pointer for capture::op when n == 0) so restore_bindings does not replay unbound scissor rects.
  • ia_set_index_buffer_x: when called with a null view pointer, clear s.bindings.index_title = None alongside s.index_buffer = None so sync_title_buffers and geometry_summary do not keep reading an unbound title index buffer.
  • Add synthetic unit test hle::d3d12x::list_x::tests::unbinding_viewports_scissors_and_index_buffer_clears_tracked_state covering all three unbind paths (verified failing before the fix with RSSetViewports(0, NULL) must clear tracked viewports and passing after).

Tested

  • cargo test -p xcl --lib -- hle::d3d12x::list_x:: --test-threads=4: 103 passed, 0 failed (including the new test, which failed before the fix).
  • cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests:: with DXC 1.9 (XCL_DXC_DIR set) on NVIDIA GeForce RTX 5070 Ti (wave32): 2610 passed, 0 failed, 49 ignored across 41 test suites; 26 SKIPPED runtime lines (21 wave64-only GPU tests on a wave32 GPU, 3 per-lane-form #1426 refusals for V_MUL_F16/V_PK_MUL_F16/V_CVT_F16_F32, 2 XCL_HEAP_WINDOWS=1 opt-in tests).

Regression run

xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB
xcl-smoke v1
commit: d56525a003417dd09d9462dbe6f36b63e7b85712
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1858 (PresentX 1858)
presents per 2 s (last 3): 43 34 0
device removals: 0
crashes: 0
peak commit: 32419 MB

Not tested

  • Wave64-only GPU tests (NVIDIA RTX 5070 Ti is wave32), uma_driver::tests::*, test_title_runs_and_game_folder_is_untouched, XCL_HEAP_WINDOWS=1 opt-in tests, and real titles that issue explicit RSSetViewports(0, NULL) / RSSetScissorRects(0, NULL) / IASetIndexBuffer(NULL) immediately before a mid-list KickoffX split.

AI use

Written and tested with an AI coding agent; all changes and test results were verified directly against the repository and local test/smoke runs.

<!-- greyfinch-sync:ivall/xcl:issue:5549870599 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3366). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T10:33:54Z. --- **What is in it** Clear stale tracked command-list bindings when a title explicitly unbinds viewports, scissor rects, or the index buffer in `crates/xcl/src/hle/d3d12x/list_x.rs`: - `rs_set_viewports_x`: when called with `n == 0` or a null pointer, clear `b.viewports` instead of leaving the previous viewport array cached (and skip slicing the raw pointer for `capture::op` when `n == 0`). Previously, `restore_bindings` replayed the stale viewports onto fresh native command lists after `KickoffX` or mid-list splits. - `rs_set_scissors_x` and `apply_scissors`: when called with `n == 0` or a null pointer on the direct path, or an empty rect slice on the deferred two-pass path, clear `b.scissors` (and skip slicing the raw pointer for `capture::op` when `n == 0`) so `restore_bindings` does not replay unbound scissor rects. - `ia_set_index_buffer_x`: when called with a null `view` pointer, clear `s.bindings.index_title = None` alongside `s.index_buffer = None` so `sync_title_buffers` and `geometry_summary` do not keep reading an unbound title index buffer. - Add synthetic unit test `hle::d3d12x::list_x::tests::unbinding_viewports_scissors_and_index_buffer_clears_tracked_state` covering all three unbind paths (verified failing before the fix with `RSSetViewports(0, NULL) must clear tracked viewports` and passing after). **Tested** - `cargo test -p xcl --lib -- hle::d3d12x::list_x:: --test-threads=4`: 103 passed, 0 failed (including the new test, which failed before the fix). - `cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::` with DXC 1.9 (`XCL_DXC_DIR` set) on NVIDIA GeForce RTX 5070 Ti (wave32): 2610 passed, 0 failed, 49 ignored across 41 test suites; 26 `SKIPPED` runtime lines (21 wave64-only GPU tests on a wave32 GPU, 3 per-lane-form `#1426` refusals for `V_MUL_F16`/`V_PK_MUL_F16`/`V_CVT_F16_F32`, 2 `XCL_HEAP_WINDOWS=1` opt-in tests). **Regression run** ``` xcl-smoke v1 commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1695 (PresentX 1695) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 31546 MB ``` ``` xcl-smoke v1 commit: d56525a003417dd09d9462dbe6f36b63e7b85712 gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1858 (PresentX 1858) presents per 2 s (last 3): 43 34 0 device removals: 0 crashes: 0 peak commit: 32419 MB ``` **Not tested** - Wave64-only GPU tests (NVIDIA RTX 5070 Ti is wave32), `uma_driver::tests::*`, `test_title_runs_and_game_folder_is_untouched`, `XCL_HEAP_WINDOWS=1` opt-in tests, and real titles that issue explicit `RSSetViewports(0, NULL)` / `RSSetScissorRects(0, NULL)` / `IASetIndexBuffer(NULL)` immediately before a mid-list `KickoffX` split. **AI use** Written and tested with an AI coding agent; all changes and test results were verified directly against the repository and local test/smoke runs.
Foniass commented 2026-12-01 05:20:20 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:20:20Z.


merge-check
base: d0832e614f
head: d56525a003
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2891 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: read the whole diff (list_x.rs, +150/-10, mostly the test). It only changes titles that call RSSetViewports(0/NULL), RSSetScissorRects(0, ...) or IASetIndexBuffer(NULL): after a KickoffX or mid-list split, restore_bindings no longer sets the old viewport or scissors again, and sync_title_buffers and the frame trace no longer read the unbound index buffer. Findings: (1) question, not a confirmed bug: the PC list still gets (0, NULL) unchanged (~632-642), and nothing shows what the native list holds afterwards. If the driver keeps the previous viewport, draws before a split use it and draws after the split have none. (2) The description overstates the viewport fix: on main a zero count with a non-null pointer already cleared the tracked viewports; only the NULL-pointer case is new. (3) AI use names no tool or model; the suite run skipped the e2e test and uma_driver::tests:: without a reason. Fine: the scissor clears on the direct and deferred two-pass paths agree with queue_scissors / apply_scissors and the batch replay, rect-list and two-pass code never read the tracked viewport or scissors, the DrawIndexedX fallback restores the index buffer from s.index_buffer (already None after an unbind), and each part of the new test fails against main.
performance: 60 s smoke of a retail title, main d0832e61 vs this merge (407ebca2): 1516 -> 1656 frames shown (109 %; last presents per 2 s: 60 60 61), peak commit 18133 -> 17991 MB, 0 device removals, 0 crashes.
verdict: safe to merge (the description should name the AI model and say only the NULL-pointer viewport case is new)
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.

<!-- greyfinch-sync:ivall/xcl:comment:7301666315 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3366#issuecomment-7301666315). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:20:20Z. --- merge-check base: d0832e614fa7d6ae027bb4feb27ccb9121f54a8f head: d56525a003417dd09d9462dbe6f36b63e7b85712 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2891 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: read the whole diff (list_x.rs, +150/-10, mostly the test). It only changes titles that call `RSSetViewports(0/NULL)`, `RSSetScissorRects(0, ...)` or `IASetIndexBuffer(NULL)`: after a KickoffX or mid-list split, `restore_bindings` no longer sets the old viewport or scissors again, and `sync_title_buffers` and the frame trace no longer read the unbound index buffer. Findings: (1) question, not a confirmed bug: the PC list still gets `(0, NULL)` unchanged (~632-642), and nothing shows what the native list holds afterwards. If the driver keeps the previous viewport, draws before a split use it and draws after the split have none. (2) The description overstates the viewport fix: on main a zero count with a non-null pointer already cleared the tracked viewports; only the NULL-pointer case is new. (3) AI use names no tool or model; the suite run skipped the e2e test and `uma_driver::tests::` without a reason. Fine: the scissor clears on the direct and deferred two-pass paths agree with `queue_scissors` / `apply_scissors` and the batch replay, rect-list and two-pass code never read the tracked viewport or scissors, the DrawIndexedX fallback restores the index buffer from `s.index_buffer` (already None after an unbind), and each part of the new test fails against main. performance: 60 s smoke of a retail title, main d0832e61 vs this merge (407ebca2): 1516 -> 1656 frames shown (109 %; last presents per 2 s: 60 60 61), peak commit 18133 -> 17991 MB, 0 device removals, 0 crashes. verdict: safe to merge (the description should name the AI model and say only the NULL-pointer viewport case is new) 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.
bmjask commented 2026-12-02 02:14:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T02:14:25Z.

Code review location: crates/xcl/src/hle/d3d12x/list_x.rs; line 643

@@ -631,20 +631,22 @@ pub(super) unsafe fn apply_topology(this: *mut c_void, t: u32) {
 /// console #21 RSSetViewports (PC #21)
 pub(super) unsafe extern "system" fn rs_set_viewports_x(this: *mut c_void, n: u32, v: *const D3D12_VIEWPORT) {
     let _c = super::capture::op(this, || {
-        let vs = if v.is_null() { &[][..] } else { std::slice::from_raw_parts(v, n as usize) };
+        let vs = if v.is_null() || n == 0 { &[][..] } else { std::slice::from_raw_parts(v, n as usize) };
         Some(super::capture::Op::Viewports(vs.iter().map(|v| [v.TopLeftX, v.TopLeftY, v.Width, v.Height, v.MinDepth, v.MaxDepth]).collect()))
     });
-    if !v.is_null() {
-        let vs = std::slice::from_raw_parts(v, n as usize).to_vec();
-        with_bindings(this, |b| b.viewports = vs);
-    }
+    let vs = if v.is_null() || n == 0 {
+        Vec::new()
+    } else {
+        std::slice::from_raw_parts(v, n as usize).to_vec()
+    };
+    with_bindings(this, |b| b.viewports = vs);
     pc::<unsafe extern "system" fn(*mut c_void, u32, *const D3D12_VIEWPORT)>(this, PC_RS_SET_VIEWPORTS)(this, n, v)

rs_set_viewports_x now treats (n > 0, NULL) as an unbind for tracking, but still forwards the original (n, NULL) to the PC RSSetViewports. On RSSetViewports(1, NULL), XCL clears b.viewports while the PC runtime or driver reads a viewport from address 0 and can crash. Forwarding (0, NULL) when the pointer is null keeps the PC call and the tracked state in agreement.

<!-- greyfinch-sync:ivall/xcl:review-comment:4763824123 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3366#discussion_r4763824123). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T02:14:25Z. **Code review location:** `crates/xcl/src/hle/d3d12x/list_x.rs`; line 643 ~~~~diff @@ -631,20 +631,22 @@ pub(super) unsafe fn apply_topology(this: *mut c_void, t: u32) { /// console #21 RSSetViewports (PC #21) pub(super) unsafe extern "system" fn rs_set_viewports_x(this: *mut c_void, n: u32, v: *const D3D12_VIEWPORT) { let _c = super::capture::op(this, || { - let vs = if v.is_null() { &[][..] } else { std::slice::from_raw_parts(v, n as usize) }; + let vs = if v.is_null() || n == 0 { &[][..] } else { std::slice::from_raw_parts(v, n as usize) }; Some(super::capture::Op::Viewports(vs.iter().map(|v| [v.TopLeftX, v.TopLeftY, v.Width, v.Height, v.MinDepth, v.MaxDepth]).collect())) }); - if !v.is_null() { - let vs = std::slice::from_raw_parts(v, n as usize).to_vec(); - with_bindings(this, |b| b.viewports = vs); - } + let vs = if v.is_null() || n == 0 { + Vec::new() + } else { + std::slice::from_raw_parts(v, n as usize).to_vec() + }; + with_bindings(this, |b| b.viewports = vs); pc::<unsafe extern "system" fn(*mut c_void, u32, *const D3D12_VIEWPORT)>(this, PC_RS_SET_VIEWPORTS)(this, n, v) ~~~~ --- `rs_set_viewports_x` now treats (n > 0, NULL) as an unbind for tracking, but still forwards the original (n, NULL) to the PC RSSetViewports. On `RSSetViewports(1, NULL)`, XCL clears b.viewports while the PC runtime or driver reads a viewport from address 0 and can crash. Forwarding (0, NULL) when the pointer is null keeps the PC call and the tracked state in agreement.
bmjask commented 2026-12-02 02:14:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T02:14:25Z.

Code review location: crates/xcl/src/hle/d3d12x/list_x.rs; line 665

@@ -654,10 +656,11 @@ pub(super) unsafe extern "system" fn rs_set_scissors_x(this: *mut c_void, n: u32
             return;
         }
         with_bindings(this, |b| b.scissors = rs.to_vec());
-    } else if r.is_null() || n == 0 {
+    } else {
         if super::bind::queue_scissors(this, Vec::new()) {
             return;
         }
+        with_bindings(this, |b| b.scissors.clear());
     }
     pc::<unsafe extern "system" fn(*mut c_void, u32, *const windows::Win32::Foundation::RECT)>(this, PC_RS_SET_SCISSOR_RECTS)(this, n, r)

The same mismatch exists in rs_set_scissors_x on the direct path: (n > 0, NULL) clears b.scissors but forwards (n, NULL) to the PC RSSetScissorRects. RSSetScissorRects(2, NULL) on a list that isn't deferring then passes NumRects=2 with a null pointer to the driver. apply_scissors already forwards rs.len() with the pointer, which is the right pattern.

<!-- greyfinch-sync:ivall/xcl:review-comment:4763824135 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3366#discussion_r4763824135). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T02:14:25Z. **Code review location:** `crates/xcl/src/hle/d3d12x/list_x.rs`; line 665 ~~~~diff @@ -654,10 +656,11 @@ pub(super) unsafe extern "system" fn rs_set_scissors_x(this: *mut c_void, n: u32 return; } with_bindings(this, |b| b.scissors = rs.to_vec()); - } else if r.is_null() || n == 0 { + } else { if super::bind::queue_scissors(this, Vec::new()) { return; } + with_bindings(this, |b| b.scissors.clear()); } pc::<unsafe extern "system" fn(*mut c_void, u32, *const windows::Win32::Foundation::RECT)>(this, PC_RS_SET_SCISSOR_RECTS)(this, n, r) ~~~~ --- The same mismatch exists in `rs_set_scissors_x` on the direct path: (n > 0, NULL) clears b.scissors but forwards (n, NULL) to the PC RSSetScissorRects. `RSSetScissorRects(2, NULL)` on a list that isn't deferring then passes NumRects=2 with a null pointer to the driver. `apply_scissors` already forwards `rs.len()` with the pointer, which is the right pattern.
bmjask commented 2026-12-02 02:14:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T02:14:25Z.

Review: COMMENTED


Review of d56525a0: 2 finding(s). 2 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.

<!-- greyfinch-sync:ivall/xcl:review:6143678551 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3366#pullrequestreview-6143678551). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T02:14:25Z. **Review:** COMMENTED --- Review of `d56525a0`: 2 finding(s). 2 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._
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3366:upstream-pr/3366'
git switch 'upstream-pr/3366'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3366'
git switch 'upstream-pr/3366'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3366'
git switch 'upstream-pr/3366'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3366'
git switch 'main'
git merge --squash 'upstream-pr/3366'
git switch 'main'
git merge --ff-only 'upstream-pr/3366'
git switch 'main'
git merge 'upstream-pr/3366'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!3366
No description provided.