D3D12.X: fix SetPredicationBufferX resource state lifecycle and KickoffX restore #207

Manually merged
github merged 1 commit from gh-pr/207/head into gh-pr/207/base 2026-11-26 04:15:10 +00:00
Owner

@⁠RandomGithub12309 on GitHub · 2026-11-23 20:57 UTC · original
RandomGithub12309:fix/set-predication-buffer-state → main · +915 −19 · 3 files · 1 commits · merged 2026-11-23 21:08 UTC

Addresses #104 item 3 with a live GPU reproducer, fix, and unit/E2E coverage for SetPredicationBufferX.

Problems reproduced & fixed

  1. KickoffX dropped active predication (reproduced live on GPU): KickoffX closes, submits, and resets the PC command list mid-recording without saving or restoring active predication. Before this fix, predication_e2e Case 2 failed on an NVIDIA GeForce RTX 5050 Laptop GPU (dst[4] was overwritten with 0xEEEE_0004 instead of remaining 0x5555_5555 under EQUAL_ZERO(0)).
  2. UMA window resource state lifecycle: Previously, SetPredicationBufferX transitioned the shared UMA window buffer COMMON -> PREDICATION, bound SetPredication, and immediately transitioned it back PREDICATION -> COMMON. On PC D3D12, a bound predication buffer must remain in D3D12_RESOURCE_STATE_PREDICATION across subsequent predicated commands—while keeping the shared UMA window in PREDICATION would conflict with other UMA operations (Write32/64BitValue*, CopyMemoryToMemoryX, FillMemoryWith*, timestamps) targeting that same window.
  3. Lifecycle & switching edge cases:
    • Because CopyBufferRegion is predicated in D3D12, switching directly from an active failing predicate to another buffer without calling SetPredicationBufferX(NULL) first requires disarming the active predicate before copying the new 8-byte value.
    • ClearState unbinds predication on the list without altering the buffer's GPU resource state.
    • Close / Reset must cleanly return pred_buf to COMMON / reset tracked per-recording state.
    • BUNDLE and COPY command lists, unreadable descriptors, unknown ops (> 1), unaligned addresses, and non-UMA addresses must be rejected without disturbing an already-active predicate.

Summary of changes

  • crates/xcl/src/hle/d3d12x/list_x.rs:
    • Lazily allocate a dedicated per-list 256-byte D3D12_HEAP_TYPE_DEFAULT predication buffer (pred_buf, created in COMMON state) and track pred_in_state and predication on ListState, guarded by an atomic PREDICATION_USED fast path so command lists that never use predication incur no Close/Reset overhead.
    • On non-null SetPredicationBufferX: validate DIRECT/COMPUTE list type, descriptor readability, op <= 1, and 8-byte-aligned UMA window resolution; disarm any active predicate before copying; copy 8 bytes from the UMA window (COMMON -> COPY_SOURCE -> COMMON) into pred_buf ((COMMON|PREDICATION) -> COPY_DEST -> PREDICATION); and bind SetPredication(pb, 0, op).
    • Save/restore pred_buf state and SetPredication across kickoff_x, preserve pred_in_state across on_clear_state, transition pred_buf back to COMMON on NULL or before_close, clear state in on_pc_reset and reset_x, and drop removed ListState COM resources outside the LISTS lock.
    • Add pure headless unit tests (set_predication_buffer_state_transitions_and_lifecycle) asserting exact ordered ResourceBarrier, CopyBufferRegion, and SetPredication calls, lifecycle transitions, rejection paths, and COM resource cleanup.
  • crates/xcl/src/hle/d3d12x/real.rs:
    • Invoke list_x::before_close in cl_close and list_x::on_pc_reset on successful cl_reset.
  • crates/xcl/tests/predication_e2e.rs:
    • Live GPU test exercising CPU- and GPU-written predicates (Write32BitValueBottomOfPipeX -> SetPredicationBufferX), interleaved UMA writes, direct buffer switches without NULL, KickoffX preservation, ClearState, Close while active, and reset_x reuse.

Verification

  • Pre-fix reproducer: cargo test -p xcl --test predication_e2e failed Case 2 on NVIDIA GeForce RTX 5050 Laptop GPU (KickoffX lost active EQUAL_ZERO(0) predication).
  • Post-fix GPU E2E (cargo test -p xcl --test predication_e2e -- --nocapture): 1 passed; 0 failed on NVIDIA GeForce RTX 5050 Laptop GPU. (Note: Windows Graphics Tools / D3D12SDKLayers.dll was unavailable on the test machine with 0x887A002D, so ID3D12InfoQueue checks were inactive locally; predication_e2e hooks and checks ID3D12InfoQueue / ID3D12InfoQueue1 automatically when Graphics Tools is installed.)
  • Headless unit tests (cargo test -p xcl --lib list_x::tests -- --nocapture): 15 passed; 0 failed.
  • cargo clippy --workspace --all-targets: exited 0 with 0 warnings in predication_e2e.rs and 0 new warnings in list_x.rs / real.rs.
  • Full workspace suite (.\scripts\test.ps1 on rebased main 2abc562): exited 0 (lib 548 passed; 0 failed; 19 ignored, plus all integration targets passing with sample-env tests ignored when unset and 2 (no wave64) GPU skips on this adapter).

AI-assisted contribution (code, tests, and verification reviewed).

**@⁠RandomGithub12309** on GitHub · 2026-11-23 20:57 UTC · [original](https://github.com/xcl-dev/xcl/pull/207) `RandomGithub12309:fix/set-predication-buffer-state` → `main` · +915 −19 · 3 files · 1 commits · merged 2026-11-23 21:08 UTC Addresses #104 item 3 with a live GPU reproducer, fix, and unit/E2E coverage for `SetPredicationBufferX`. ### Problems reproduced & fixed 1. **`KickoffX` dropped active predication (reproduced live on GPU):** `KickoffX` closes, submits, and resets the PC command list mid-recording without saving or restoring active predication. Before this fix, `predication_e2e` Case 2 failed on an NVIDIA GeForce RTX 5050 Laptop GPU (`dst[4]` was overwritten with `0xEEEE_0004` instead of remaining `0x5555_5555` under `EQUAL_ZERO(0)`). 2. **UMA window resource state lifecycle:** Previously, `SetPredicationBufferX` transitioned the shared UMA window buffer `COMMON -> PREDICATION`, bound `SetPredication`, and immediately transitioned it back `PREDICATION -> COMMON`. On PC D3D12, a bound predication buffer must remain in `D3D12_RESOURCE_STATE_PREDICATION` across subsequent predicated commands—while keeping the shared UMA window in `PREDICATION` would conflict with other UMA operations (`Write32/64BitValue*`, `CopyMemoryToMemoryX`, `FillMemoryWith*`, timestamps) targeting that same window. 3. **Lifecycle & switching edge cases:** - Because `CopyBufferRegion` is predicated in D3D12, switching directly from an active failing predicate to another buffer without calling `SetPredicationBufferX(NULL)` first requires disarming the active predicate before copying the new 8-byte value. - `ClearState` unbinds predication on the list without altering the buffer's GPU resource state. - `Close` / `Reset` must cleanly return `pred_buf` to `COMMON` / reset tracked per-recording state. - `BUNDLE` and `COPY` command lists, unreadable descriptors, unknown ops (`> 1`), unaligned addresses, and non-UMA addresses must be rejected without disturbing an already-active predicate. ### Summary of changes - **`crates/xcl/src/hle/d3d12x/list_x.rs`:** - Lazily allocate a dedicated per-list 256-byte `D3D12_HEAP_TYPE_DEFAULT` predication buffer (`pred_buf`, created in `COMMON` state) and track `pred_in_state` and `predication` on `ListState`, guarded by an atomic `PREDICATION_USED` fast path so command lists that never use predication incur no `Close`/`Reset` overhead. - On non-null `SetPredicationBufferX`: validate `DIRECT`/`COMPUTE` list type, descriptor readability, `op <= 1`, and 8-byte-aligned UMA window resolution; disarm any active predicate before copying; copy 8 bytes from the UMA window (`COMMON -> COPY_SOURCE -> COMMON`) into `pred_buf` (`(COMMON|PREDICATION) -> COPY_DEST -> PREDICATION`); and bind `SetPredication(pb, 0, op)`. - Save/restore `pred_buf` state and `SetPredication` across `kickoff_x`, preserve `pred_in_state` across `on_clear_state`, transition `pred_buf` back to `COMMON` on `NULL` or `before_close`, clear state in `on_pc_reset` and `reset_x`, and drop removed `ListState` COM resources outside the `LISTS` lock. - Add pure headless unit tests (`set_predication_buffer_state_transitions_and_lifecycle`) asserting exact ordered `ResourceBarrier`, `CopyBufferRegion`, and `SetPredication` calls, lifecycle transitions, rejection paths, and COM resource cleanup. - **`crates/xcl/src/hle/d3d12x/real.rs`:** - Invoke `list_x::before_close` in `cl_close` and `list_x::on_pc_reset` on successful `cl_reset`. - **`crates/xcl/tests/predication_e2e.rs`:** - Live GPU test exercising CPU- and GPU-written predicates (`Write32BitValueBottomOfPipeX` -> `SetPredicationBufferX`), interleaved UMA writes, direct buffer switches without `NULL`, `KickoffX` preservation, `ClearState`, `Close` while active, and `reset_x` reuse. ### Verification - **Pre-fix reproducer:** `cargo test -p xcl --test predication_e2e` failed Case 2 on NVIDIA GeForce RTX 5050 Laptop GPU (`KickoffX` lost active `EQUAL_ZERO(0)` predication). - **Post-fix GPU E2E (`cargo test -p xcl --test predication_e2e -- --nocapture`):** `1 passed; 0 failed` on NVIDIA GeForce RTX 5050 Laptop GPU. *(Note: Windows Graphics Tools / `D3D12SDKLayers.dll` was unavailable on the test machine with `0x887A002D`, so `ID3D12InfoQueue` checks were inactive locally; `predication_e2e` hooks and checks `ID3D12InfoQueue` / `ID3D12InfoQueue1` automatically when Graphics Tools is installed.)* - **Headless unit tests (`cargo test -p xcl --lib list_x::tests -- --nocapture`):** `15 passed; 0 failed`. - **`cargo clippy --workspace --all-targets`:** exited `0` with `0` warnings in `predication_e2e.rs` and `0` new warnings in `list_x.rs` / `real.rs`. - **Full workspace suite (`.\scripts\test.ps1` on rebased `main` `2abc562`):** exited `0` (lib `548 passed; 0 failed; 19 ignored`, plus all integration targets passing with sample-env tests ignored when unset and `2 (no wave64)` GPU skips on this adapter). --- *AI-assisted contribution (code, tests, and verification reviewed).*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:15:08 +00:00
github manually merged commit e8564f1b01 into gh-pr/207/base 2026-11-26 04:15:10 +00:00
Author
Owner

@⁠ivall on GitHub · 2026-11-23 21:09 UTC · original

Merged (main 6da33fa). Checked on an RX 9070 (RDNA4) with the 22 PRs merged together on main eaa36b6: cargo build --workspace && cargo test --workspace -- --test-threads=4 - 582 unit tests passed, 0 failed (20 ignored), the integration and e2e tests passed too; and the retail game still reaches its first menu (646 frames in 30 s, no device removal). The merge conflicted with #135's title_textures::list_reset calls in list_x::on_create / on_release; both are kept (the reset after the list state is replaced).

**@⁠ivall** on GitHub · 2026-11-23 21:09 UTC · [original](https://github.com/xcl-dev/xcl/pull/207#issuecomment-7114081035) Merged (main 6da33fa). Checked on an RX 9070 (RDNA4) with the 22 PRs merged together on main eaa36b6: `cargo build --workspace && cargo test --workspace -- --test-threads=4` - 582 unit tests passed, 0 failed (20 ignored), the integration and e2e tests passed too; and the retail game still reaches its first menu (646 frames in 30 s, no device removal). The merge conflicted with #135's `title_textures::list_reset` calls in `list_x::on_create` / `on_release`; both are kept (the reset after the list state is replaced).
This discussion has been locked. Commenting is limited to contributors.
No description provided.