D3D12.X: sanitize reserved and placed resource descriptions and initial states #3291

Closed
Kalmarv wants to merge 1 commit from upstream-pr/3291 into main
Kalmarv commented 2026-11-29 06:00:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T06:00:25Z.


Console CreatePlacedResource (placed_in_heap_x / own_memory_if_larger_on_the_pc) and CreateReservedResource(1) in crates/xcl/src/hle/d3d12x/real.rs missed parts of the PC description/state/clear sanitization already used by CreateCommittedResource and CreatePlacedResourceX:

  • placed_in_heap_x and own_memory_if_larger_on_the_pc called pc_resource_desc(&x) instead of pc_resource_desc_for_device(this, &x) and pc_initial_state(orig_flags, state) instead of pc_initial_state_for_desc(orig_flags, pc_flags, state). When pc_resource_desc_for_device stripped D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS from a non-depth texture (for example an sRGB or unsupported-UAV format), D3D12_RESOURCE_STATE_UNORDERED_ACCESS (0x8) remained in the initial state, causing both the placed retry and the committed fallback to fail with E_INVALIDARG.
  • dev_create_reserved_resource and dev_create_reserved_resource1 had no sanitization fallback when PC D3D12 refused a console format, console tile layout (> 3), console resource flag bits, console initial-state bits, ALLOW_DEPTH_STENCIL | ALLOW_UNORDERED_ACCESS, unsupported texture UAV flags, or promoted clear-value format.

What is in it

  • crates/xcl/src/hle/d3d12x/real.rs:
    • Update placed_in_heap_x and own_memory_if_larger_on_the_pc to use pc_resource_desc_for_device(this, &x) and pc_initial_state_for_desc(orig_flags, pc_flags, state as u32) so stripped UAV flags also drop UNORDERED_ACCESS from the initial state and per-device UAV format checks are honored.
    • Add pc_reserved_desc_for_device (preserving PC layouts <= 3 so invalid PC layouts like ROW_MAJOR are still rejected like the host driver, while mapping console tile layouts > 3 to D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE) and create_reserved_with_fallback for dev_create_reserved_resource and dev_create_reserved_resource1, retrying only when sanitization changed the description, initial state, or clear value, and calling depth_uav::note_created on texture fallback success.
    • Add child-isolated WARP debug-layer tests in reserved_placed_sanitize_tests covering console CreateReservedResource (D32+UAV depth target with console flags/layout/state, B5G6R5 RT+UAV with promoted clear format), CreateReservedResource1 (sRGB+UAV in UNORDERED_ACCESS state), and CreatePlacedResource (dev_create_placed_resource_x) with sRGB+UAV in UNORDERED_ACCESS state.

Tested

  • Host: Windows 11 Pro (build 26200), AMD Ryzen 9 9950X, NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), rustc 1.99.0 (b940084d7 2026-09-28), DXC 1.9 (1.9.2609).
  • Pre-fix check: both new reserved_placed_sanitize_tests failed on WARP with 0x80070057 (E_INVALIDARG).
  • Post-fix targeted tests (cargo test -p xcl --lib -- hle::d3d12x::real::reserved_placed_sanitize_tests::): 2 passed, 0 failed (0.09s, D3D12 debug layer enabled).
  • Host-parity integration test (cargo test -p xcl --test d3d12_create_reserved_resource): 3 passed, 0 failed (0.30s), including pc_reserved_resources_match_host_driver (invalid_tex2d_row_major still rejected with 0x80070057).
  • Full workspace (cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests:: with XCL_DXC_DIR set to DXC 1.9): 2601 passed, 0 failed, 49 ignored, 9 filtered (532.3s).
  • Explicit GPU SKIPPED lines in workspace run (26 total): 21 no wave64 (RTX 5070 Ti is wave32), 3 wave32 per-lane #1426 (V_MUL_F16, V_PK_MUL_F16, V_CVT_F16_F32), 2 no heap windows (default XCL_HEAP_WINDOWS off).
  • Release build (cargo build --release -p xcl -j 4) succeeded and passed the paired 150 s title smoke below.

Regression run

main (f0e8fdd5c3942ddd399d5ee11384c8595c5aacff):

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

PR (37fc811b087ae6067493e8865613017c24add47e):

xcl-smoke v1
commit: 37fc811b087ae6067493e8865613017c24add47e
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1579 (PresentX 1579)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 32531 MB

Not tested

  • AMD or Intel GPUs (only tested on NVIDIA GeForce RTX 5070 Ti and WARP).
  • Native wave64 GPU tests (RTX 5070 Ti is wave32; 21 wave64 tests skipped).
  • uma_driver::tests:: and test_title_runs_and_game_folder_is_untouched (filtered out of the workspace test run).
  • CreateReservedResource1 with a non-null ID3D12ProtectedResourceSession.

AI use

  • Drafted, implemented, and tested with AI assistance (Antigravity); all code changes, pre/post WARP and RTX tests, and regression runs were verified locally.
<!-- greyfinch-sync:ivall/xcl:issue:5548099326 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3291). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T06:00:25Z. --- Console `CreatePlacedResource` (`placed_in_heap_x` / `own_memory_if_larger_on_the_pc`) and `CreateReservedResource(1)` in `crates/xcl/src/hle/d3d12x/real.rs` missed parts of the PC description/state/clear sanitization already used by `CreateCommittedResource` and `CreatePlacedResourceX`: - `placed_in_heap_x` and `own_memory_if_larger_on_the_pc` called `pc_resource_desc(&x)` instead of `pc_resource_desc_for_device(this, &x)` and `pc_initial_state(orig_flags, state)` instead of `pc_initial_state_for_desc(orig_flags, pc_flags, state)`. When `pc_resource_desc_for_device` stripped `D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS` from a non-depth texture (for example an sRGB or unsupported-UAV format), `D3D12_RESOURCE_STATE_UNORDERED_ACCESS` (`0x8`) remained in the initial state, causing both the placed retry and the committed fallback to fail with `E_INVALIDARG`. - `dev_create_reserved_resource` and `dev_create_reserved_resource1` had no sanitization fallback when PC D3D12 refused a console format, console tile layout (`> 3`), console resource flag bits, console initial-state bits, `ALLOW_DEPTH_STENCIL | ALLOW_UNORDERED_ACCESS`, unsupported texture UAV flags, or promoted clear-value format. **What is in it** - `crates/xcl/src/hle/d3d12x/real.rs`: - Update `placed_in_heap_x` and `own_memory_if_larger_on_the_pc` to use `pc_resource_desc_for_device(this, &x)` and `pc_initial_state_for_desc(orig_flags, pc_flags, state as u32)` so stripped UAV flags also drop `UNORDERED_ACCESS` from the initial state and per-device UAV format checks are honored. - Add `pc_reserved_desc_for_device` (preserving PC layouts `<= 3` so invalid PC layouts like `ROW_MAJOR` are still rejected like the host driver, while mapping console tile layouts `> 3` to `D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE`) and `create_reserved_with_fallback` for `dev_create_reserved_resource` and `dev_create_reserved_resource1`, retrying only when sanitization changed the description, initial state, or clear value, and calling `depth_uav::note_created` on texture fallback success. - Add child-isolated WARP debug-layer tests in `reserved_placed_sanitize_tests` covering console `CreateReservedResource` (D32+UAV depth target with console flags/layout/state, `B5G6R5` RT+UAV with promoted clear format), `CreateReservedResource1` (sRGB+UAV in `UNORDERED_ACCESS` state), and `CreatePlacedResource` (`dev_create_placed_resource_x`) with sRGB+UAV in `UNORDERED_ACCESS` state. **Tested** - Host: Windows 11 Pro (build 26200), AMD Ryzen 9 9950X, NVIDIA GeForce RTX 5070 Ti (driver `32.0.16.1074`), `rustc 1.99.0 (b940084d7 2026-09-28)`, DXC 1.9 (`1.9.2609`). - Pre-fix check: both new `reserved_placed_sanitize_tests` failed on WARP with `0x80070057` (`E_INVALIDARG`). - Post-fix targeted tests (`cargo test -p xcl --lib -- hle::d3d12x::real::reserved_placed_sanitize_tests::`): 2 passed, 0 failed (`0.09s`, D3D12 debug layer enabled). - Host-parity integration test (`cargo test -p xcl --test d3d12_create_reserved_resource`): 3 passed, 0 failed (`0.30s`), including `pc_reserved_resources_match_host_driver` (`invalid_tex2d_row_major` still rejected with `0x80070057`). - Full workspace (`cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::` with `XCL_DXC_DIR` set to DXC 1.9): 2601 passed, 0 failed, 49 ignored, 9 filtered (`532.3s`). - Explicit GPU `SKIPPED` lines in workspace run (26 total): 21 `no wave64` (RTX 5070 Ti is wave32), 3 wave32 per-lane `#1426` (`V_MUL_F16`, `V_PK_MUL_F16`, `V_CVT_F16_F32`), 2 `no heap windows` (default `XCL_HEAP_WINDOWS` off). - Release build (`cargo build --release -p xcl -j 4`) succeeded and passed the paired 150 s title smoke below. **Regression run** main (`f0e8fdd5c3942ddd399d5ee11384c8595c5aacff`): ``` 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 ``` PR (`37fc811b087ae6067493e8865613017c24add47e`): ``` xcl-smoke v1 commit: 37fc811b087ae6067493e8865613017c24add47e gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1579 (PresentX 1579) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 32531 MB ``` **Not tested** - AMD or Intel GPUs (only tested on NVIDIA GeForce RTX 5070 Ti and WARP). - Native wave64 GPU tests (RTX 5070 Ti is wave32; 21 wave64 tests skipped). - `uma_driver::tests::` and `test_title_runs_and_game_folder_is_untouched` (filtered out of the workspace test run). - `CreateReservedResource1` with a non-null `ID3D12ProtectedResourceSession`. **AI use** - Drafted, implemented, and tested with AI assistance (Antigravity); all code changes, pre/post WARP and RTX tests, and regression runs were verified locally.
Foniass commented 2026-11-29 14:57:16 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-11-29T14:57:16Z.


merge-check
base: fa204595a488c60c6c5f905244f0172072c9c2a9
head: 37fc811b087ae6067493e8865613017c24add47e
merge: clean (git diff --check: 1 line, blank line at EOF)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2606 passed, 4 failed, 49 ignored; SKIPPED: 44; failing: gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (driver E_OUTOFMEMORY, same on base on this machine)
review: no bug found. It runs only after the PC driver refuses a resource, and copies rules that already exist. Its tests fail on main. The reserved fallback also retries XCL's own internal reserved resources (vram.rs, sparse.rs), which the PR does not mention. Nothing shows a title reaching these paths. git diff --check flags a blank line at EOF in real.rs. AI use names no model.
performance: 60 s smoke: 1688 frames (100.4 %), peak commit 16873 MB (99.5 %)
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti (driver 32.0.16.2114), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

Merge-check run and written with Claude Code (Claude Opus 5.5): a test merge onto the base above, the full suite, and a 60 s scripts/smoke.ps1 run of a retail title against main on the same machine. The 4 fuzz_validate_and_gpu_* failures happen on main on this machine too.

<!-- greyfinch-sync:ivall/xcl:comment:7257704581 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3291#issuecomment-7257704581). Originally posted by [Foniass](https://github.com/Foniass) on 2026-11-29T14:57:16Z. --- ```text merge-check base: fa204595a488c60c6c5f905244f0172072c9c2a9 head: 37fc811b087ae6067493e8865613017c24add47e merge: clean (git diff --check: 1 line, blank line at EOF) build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2606 passed, 4 failed, 49 ignored; SKIPPED: 44; failing: gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (driver E_OUTOFMEMORY, same on base on this machine) review: no bug found. It runs only after the PC driver refuses a resource, and copies rules that already exist. Its tests fail on main. The reserved fallback also retries XCL's own internal reserved resources (vram.rs, sparse.rs), which the PR does not mention. Nothing shows a title reaching these paths. git diff --check flags a blank line at EOF in real.rs. AI use names no model. performance: 60 s smoke: 1688 frames (100.4 %), peak commit 16873 MB (99.5 %) verdict: safe to merge hardware / tool and model: RTX 5070 Ti (driver 32.0.16.2114), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5 ``` Merge-check run and written with Claude Code (Claude Opus 5.5): a test merge onto the base above, the full suite, and a 60 s `scripts/smoke.ps1` run of a retail title against main on the same machine. The 4 `fuzz_validate_and_gpu_*` failures happen on main on this machine too.
bmjask commented 2026-12-01 21:12:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:12:38Z.

Code review location: crates/xcl/src/hle/d3d12x/real.rs; line 4627

@@ -4587,14 +4591,75 @@ unsafe fn is_reserved(res: *mut c_void) -> bool {
     kept.is_some_and(|k| desc_of(res) == Some(k))
 }
 
+/// The PC description for `CreateReservedResource(1)`: `pc_resource_desc_for_device`, preserving
+/// PC texture layouts (`<= 3`, so invalid PC layouts like `ROW_MAJOR` are still rejected like the
+/// host driver) and mapping console tile layouts (`> 3`) to `64KB_UNDEFINED_SWIZZLE`.
+unsafe fn pc_reserved_desc_for_device(device: *mut c_void, x: &[u8; 0x38]) -> [u8; 0x38] {
+    let mut d = super::device_x::pc_resource_desc_for_device(device, x);
+    let dim = u32::from_le_bytes(d[0..4].try_into().unwrap());
+    if dim != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 {
+        let orig_layout = u32::from_le_bytes(x[0x2C..0x30].try_into().unwrap());
+        let layout = if orig_layout > 3 {
+            D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE.0 as u32
+        } else {
+            orig_layout
+        };
+        d[0x2C..0x30].copy_from_slice(&layout.to_le_bytes());
+    }
+    d
+}
+
+/// Shared fallback for `CreateReservedResource` and `CreateReservedResource1`: when the PC
+/// runtime refuses a console description, state, or clear value, sanitize them the PC's way
+/// (matching `CreateCommittedResource` / `CreatePlacedResource`) and retry.
+unsafe fn create_reserved_with_fallback(
+    this: *mut c_void,
+    desc: usize,
+    state: usize,
+    clear: usize,
+    ppv: *mut *mut c_void,
+    mut create: impl FnMut(usize, usize, usize) -> HRESULT,
+) -> HRESULT {
+    let mut hr = create(desc, state, clear);
+    let mut used_state = state;
+    if hr < 0 && desc != 0 && crate::gpu::shader::readable(desc, 0x38) {
+        let x: [u8; 0x38] = (desc as *const [u8; 0x38]).read_unaligned();
+        let d = pc_reserved_desc_for_device(this, &x);

The reserved fallback applies pc_resource_desc's format promotions (85/86/115 -> 28, 116/117 -> 10, 118-120 -> 44), which change bytes per texel and therefore the 64 KB tile count and shape. Nothing in the UpdateTileMappings / GetResourceTiling hooks accounts for that. A reserved B5G6R5 RT|UAV texture (test case 2 here) becomes R8G8B8A8, with twice the tiles, but the title maps tiles computed for the console layout. Only part of the resource is backed, so unmapped regions read as zero and drop writes. Before, creation failed loudly; now it succeeds and the data is silently wrong.

<!-- greyfinch-sync:ivall/xcl:review-comment:4761065315 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3291#discussion_r4761065315). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T21:12:38Z. **Code review location:** `crates/xcl/src/hle/d3d12x/real.rs`; line 4627 ~~~~diff @@ -4587,14 +4591,75 @@ unsafe fn is_reserved(res: *mut c_void) -> bool { kept.is_some_and(|k| desc_of(res) == Some(k)) } +/// The PC description for `CreateReservedResource(1)`: `pc_resource_desc_for_device`, preserving +/// PC texture layouts (`<= 3`, so invalid PC layouts like `ROW_MAJOR` are still rejected like the +/// host driver) and mapping console tile layouts (`> 3`) to `64KB_UNDEFINED_SWIZZLE`. +unsafe fn pc_reserved_desc_for_device(device: *mut c_void, x: &[u8; 0x38]) -> [u8; 0x38] { + let mut d = super::device_x::pc_resource_desc_for_device(device, x); + let dim = u32::from_le_bytes(d[0..4].try_into().unwrap()); + if dim != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 { + let orig_layout = u32::from_le_bytes(x[0x2C..0x30].try_into().unwrap()); + let layout = if orig_layout > 3 { + D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE.0 as u32 + } else { + orig_layout + }; + d[0x2C..0x30].copy_from_slice(&layout.to_le_bytes()); + } + d +} + +/// Shared fallback for `CreateReservedResource` and `CreateReservedResource1`: when the PC +/// runtime refuses a console description, state, or clear value, sanitize them the PC's way +/// (matching `CreateCommittedResource` / `CreatePlacedResource`) and retry. +unsafe fn create_reserved_with_fallback( + this: *mut c_void, + desc: usize, + state: usize, + clear: usize, + ppv: *mut *mut c_void, + mut create: impl FnMut(usize, usize, usize) -> HRESULT, +) -> HRESULT { + let mut hr = create(desc, state, clear); + let mut used_state = state; + if hr < 0 && desc != 0 && crate::gpu::shader::readable(desc, 0x38) { + let x: [u8; 0x38] = (desc as *const [u8; 0x38]).read_unaligned(); + let d = pc_reserved_desc_for_device(this, &x); ~~~~ --- The reserved fallback applies `pc_resource_desc`'s format promotions (85/86/115 -> 28, 116/117 -> 10, 118-120 -> 44), which change bytes per texel and therefore the 64 KB tile count and shape. Nothing in the UpdateTileMappings / GetResourceTiling hooks accounts for that. A reserved B5G6R5 RT|UAV texture (test case 2 here) becomes R8G8B8A8, with twice the tiles, but the title maps tiles computed for the console layout. Only part of the resource is backed, so unmapped regions read as zero and drop writes. Before, creation failed loudly; now it succeeds and the data is silently wrong.
bmjask commented 2026-12-01 21:12:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:12:38Z.

Code review location: crates/xcl/src/hle/d3d12x/real.rs; line 4640

@@ -4587,14 +4591,75 @@ unsafe fn is_reserved(res: *mut c_void) -> bool {
     kept.is_some_and(|k| desc_of(res) == Some(k))
 }
 
+/// The PC description for `CreateReservedResource(1)`: `pc_resource_desc_for_device`, preserving
+/// PC texture layouts (`<= 3`, so invalid PC layouts like `ROW_MAJOR` are still rejected like the
+/// host driver) and mapping console tile layouts (`> 3`) to `64KB_UNDEFINED_SWIZZLE`.
+unsafe fn pc_reserved_desc_for_device(device: *mut c_void, x: &[u8; 0x38]) -> [u8; 0x38] {
+    let mut d = super::device_x::pc_resource_desc_for_device(device, x);
+    let dim = u32::from_le_bytes(d[0..4].try_into().unwrap());
+    if dim != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 {
+        let orig_layout = u32::from_le_bytes(x[0x2C..0x30].try_into().unwrap());
+        let layout = if orig_layout > 3 {
+            D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE.0 as u32
+        } else {
+            orig_layout
+        };
+        d[0x2C..0x30].copy_from_slice(&layout.to_le_bytes());
+    }
+    d
+}
+
+/// Shared fallback for `CreateReservedResource` and `CreateReservedResource1`: when the PC
+/// runtime refuses a console description, state, or clear value, sanitize them the PC's way
+/// (matching `CreateCommittedResource` / `CreatePlacedResource`) and retry.
+unsafe fn create_reserved_with_fallback(
+    this: *mut c_void,
+    desc: usize,
+    state: usize,
+    clear: usize,
+    ppv: *mut *mut c_void,
+    mut create: impl FnMut(usize, usize, usize) -> HRESULT,
+) -> HRESULT {
+    let mut hr = create(desc, state, clear);
+    let mut used_state = state;
+    if hr < 0 && desc != 0 && crate::gpu::shader::readable(desc, 0x38) {
+        let x: [u8; 0x38] = (desc as *const [u8; 0x38]).read_unaligned();
+        let d = pc_reserved_desc_for_device(this, &x);
+        let orig_flags = u32::from_le_bytes(x[0x30..0x34].try_into().unwrap());
+        let pc_flags = u32::from_le_bytes(d[0x30..0x34].try_into().unwrap());
+        let st = super::device_x::pc_initial_state_for_desc(orig_flags, pc_flags, state as u32);
+        let mut clear_buf = [0u8; 24];
+        let clear_d = super::device_x::pc_clear_for_desc(
+            clear as *const c_void,
+            u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()),
+            &mut clear_buf,
+        ) as usize;
+        let clear_changed = clear != 0
+            && clear_d != clear
+            && (clear as *const [u8; 24]).read_unaligned() != clear_buf;
+        if d != x || st as usize != state || clear_changed {

This hook is on every adopted PC device's vtable, so the fallback also rewrites PC-native descriptions that should be rejected. It strips DS|UAV and sRGB+UAV, masks flags to 0xFF and drops state bits. A PC or XCL-internal CreateReservedResource with DS|UAV, or R8G8B8A8_UNORM_SRGB+UAV in UNORDERED_ACCESS, now gets S_OK and a resource without the UAV flag instead of E_INVALIDARG. A later CreateUnorderedAccessView on it is invalid, so the caller's bug is hidden. Only ROW_MAJOR is checked for parity.

<!-- greyfinch-sync:ivall/xcl:review-comment:4761065329 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3291#discussion_r4761065329). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T21:12:38Z. **Code review location:** `crates/xcl/src/hle/d3d12x/real.rs`; line 4640 ~~~~diff @@ -4587,14 +4591,75 @@ unsafe fn is_reserved(res: *mut c_void) -> bool { kept.is_some_and(|k| desc_of(res) == Some(k)) } +/// The PC description for `CreateReservedResource(1)`: `pc_resource_desc_for_device`, preserving +/// PC texture layouts (`<= 3`, so invalid PC layouts like `ROW_MAJOR` are still rejected like the +/// host driver) and mapping console tile layouts (`> 3`) to `64KB_UNDEFINED_SWIZZLE`. +unsafe fn pc_reserved_desc_for_device(device: *mut c_void, x: &[u8; 0x38]) -> [u8; 0x38] { + let mut d = super::device_x::pc_resource_desc_for_device(device, x); + let dim = u32::from_le_bytes(d[0..4].try_into().unwrap()); + if dim != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 { + let orig_layout = u32::from_le_bytes(x[0x2C..0x30].try_into().unwrap()); + let layout = if orig_layout > 3 { + D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE.0 as u32 + } else { + orig_layout + }; + d[0x2C..0x30].copy_from_slice(&layout.to_le_bytes()); + } + d +} + +/// Shared fallback for `CreateReservedResource` and `CreateReservedResource1`: when the PC +/// runtime refuses a console description, state, or clear value, sanitize them the PC's way +/// (matching `CreateCommittedResource` / `CreatePlacedResource`) and retry. +unsafe fn create_reserved_with_fallback( + this: *mut c_void, + desc: usize, + state: usize, + clear: usize, + ppv: *mut *mut c_void, + mut create: impl FnMut(usize, usize, usize) -> HRESULT, +) -> HRESULT { + let mut hr = create(desc, state, clear); + let mut used_state = state; + if hr < 0 && desc != 0 && crate::gpu::shader::readable(desc, 0x38) { + let x: [u8; 0x38] = (desc as *const [u8; 0x38]).read_unaligned(); + let d = pc_reserved_desc_for_device(this, &x); + let orig_flags = u32::from_le_bytes(x[0x30..0x34].try_into().unwrap()); + let pc_flags = u32::from_le_bytes(d[0x30..0x34].try_into().unwrap()); + let st = super::device_x::pc_initial_state_for_desc(orig_flags, pc_flags, state as u32); + let mut clear_buf = [0u8; 24]; + let clear_d = super::device_x::pc_clear_for_desc( + clear as *const c_void, + u32::from_le_bytes(d[0x20..0x24].try_into().unwrap()), + &mut clear_buf, + ) as usize; + let clear_changed = clear != 0 + && clear_d != clear + && (clear as *const [u8; 24]).read_unaligned() != clear_buf; + if d != x || st as usize != state || clear_changed { ~~~~ --- This hook is on every adopted PC device's vtable, so the fallback also rewrites PC-native descriptions that should be rejected. It strips DS|UAV and sRGB+UAV, masks flags to 0xFF and drops state bits. A PC or XCL-internal `CreateReservedResource` with DS|UAV, or R8G8B8A8_UNORM_SRGB+UAV in UNORDERED_ACCESS, now gets S_OK and a resource without the UAV flag instead of E_INVALIDARG. A later CreateUnorderedAccessView on it is invalid, so the caller's bug is hidden. Only ROW_MAJOR is checked for parity.
bmjask commented 2026-12-01 21:12:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:12:38Z.

Code review location: crates/xcl/src/hle/d3d12x/real.rs; line 4624

@@ -4587,14 +4591,75 @@ unsafe fn is_reserved(res: *mut c_void) -> bool {
     kept.is_some_and(|k| desc_of(res) == Some(k))
 }
 
+/// The PC description for `CreateReservedResource(1)`: `pc_resource_desc_for_device`, preserving
+/// PC texture layouts (`<= 3`, so invalid PC layouts like `ROW_MAJOR` are still rejected like the
+/// host driver) and mapping console tile layouts (`> 3`) to `64KB_UNDEFINED_SWIZZLE`.
+unsafe fn pc_reserved_desc_for_device(device: *mut c_void, x: &[u8; 0x38]) -> [u8; 0x38] {
+    let mut d = super::device_x::pc_resource_desc_for_device(device, x);
+    let dim = u32::from_le_bytes(d[0..4].try_into().unwrap());
+    if dim != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 {
+        let orig_layout = u32::from_le_bytes(x[0x2C..0x30].try_into().unwrap());
+        let layout = if orig_layout > 3 {
+            D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE.0 as u32
+        } else {
+            orig_layout
+        };
+        d[0x2C..0x30].copy_from_slice(&layout.to_le_bytes());
+    }
+    d
+}
+
+/// Shared fallback for `CreateReservedResource` and `CreateReservedResource1`: when the PC
+/// runtime refuses a console description, state, or clear value, sanitize them the PC's way
+/// (matching `CreateCommittedResource` / `CreatePlacedResource`) and retry.
+unsafe fn create_reserved_with_fallback(
+    this: *mut c_void,
+    desc: usize,
+    state: usize,
+    clear: usize,
+    ppv: *mut *mut c_void,
+    mut create: impl FnMut(usize, usize, usize) -> HRESULT,
+) -> HRESULT {
+    let mut hr = create(desc, state, clear);
+    let mut used_state = state;

When the first call succeeds, used_state is the raw console state, console-only bits included (e.g. 0x100004). The fallback branch uses the sanitized st, and the placed path masks with & PC_RESOURCE_STATES. reserved_3d then hands lut3d::created a value that isn't a PC state, and it's tracked and logged as one.

<!-- greyfinch-sync:ivall/xcl:review-comment:4761065341 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3291#discussion_r4761065341). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T21:12:38Z. **Code review location:** `crates/xcl/src/hle/d3d12x/real.rs`; line 4624 ~~~~diff @@ -4587,14 +4591,75 @@ unsafe fn is_reserved(res: *mut c_void) -> bool { kept.is_some_and(|k| desc_of(res) == Some(k)) } +/// The PC description for `CreateReservedResource(1)`: `pc_resource_desc_for_device`, preserving +/// PC texture layouts (`<= 3`, so invalid PC layouts like `ROW_MAJOR` are still rejected like the +/// host driver) and mapping console tile layouts (`> 3`) to `64KB_UNDEFINED_SWIZZLE`. +unsafe fn pc_reserved_desc_for_device(device: *mut c_void, x: &[u8; 0x38]) -> [u8; 0x38] { + let mut d = super::device_x::pc_resource_desc_for_device(device, x); + let dim = u32::from_le_bytes(d[0..4].try_into().unwrap()); + if dim != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 { + let orig_layout = u32::from_le_bytes(x[0x2C..0x30].try_into().unwrap()); + let layout = if orig_layout > 3 { + D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE.0 as u32 + } else { + orig_layout + }; + d[0x2C..0x30].copy_from_slice(&layout.to_le_bytes()); + } + d +} + +/// Shared fallback for `CreateReservedResource` and `CreateReservedResource1`: when the PC +/// runtime refuses a console description, state, or clear value, sanitize them the PC's way +/// (matching `CreateCommittedResource` / `CreatePlacedResource`) and retry. +unsafe fn create_reserved_with_fallback( + this: *mut c_void, + desc: usize, + state: usize, + clear: usize, + ppv: *mut *mut c_void, + mut create: impl FnMut(usize, usize, usize) -> HRESULT, +) -> HRESULT { + let mut hr = create(desc, state, clear); + let mut used_state = state; ~~~~ --- When the first call succeeds, `used_state` is the raw console state, console-only bits included (e.g. 0x100004). The fallback branch uses the sanitized `st`, and the placed path masks with `& PC_RESOURCE_STATES`. `reserved_3d` then hands `lut3d::created` a value that isn't a PC state, and it's tracked and logged as one.
bmjask commented 2026-12-01 21:12:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:12:38Z.

Code review location: crates/xcl/src/hle/d3d12x/real.rs; line 4602

@@ -4587,14 +4591,75 @@ unsafe fn is_reserved(res: *mut c_void) -> bool {
     kept.is_some_and(|k| desc_of(res) == Some(k))
 }
 
+/// The PC description for `CreateReservedResource(1)`: `pc_resource_desc_for_device`, preserving
+/// PC texture layouts (`<= 3`, so invalid PC layouts like `ROW_MAJOR` are still rejected like the
+/// host driver) and mapping console tile layouts (`> 3`) to `64KB_UNDEFINED_SWIZZLE`.
+unsafe fn pc_reserved_desc_for_device(device: *mut c_void, x: &[u8; 0x38]) -> [u8; 0x38] {
+    let mut d = super::device_x::pc_resource_desc_for_device(device, x);
+    let dim = u32::from_le_bytes(d[0..4].try_into().unwrap());
+    if dim != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 {
+        let orig_layout = u32::from_le_bytes(x[0x2C..0x30].try_into().unwrap());
+        let layout = if orig_layout > 3 {

pc_reserved_desc_for_device keeps texture layout 0 (UNKNOWN) as it is, but PC CreateReservedResource only accepts 64KB_UNDEFINED_SWIZZLE / 64KB_STANDARD_SWIZZLE for textures. A console reserved Texture2D with Layout=0 plus console flag bits fails, the retry still has layout 0, and it fails again, which is the case the fallback was meant to cover.

<!-- greyfinch-sync:ivall/xcl:review-comment:4761065349 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3291#discussion_r4761065349). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T21:12:38Z. **Code review location:** `crates/xcl/src/hle/d3d12x/real.rs`; line 4602 ~~~~diff @@ -4587,14 +4591,75 @@ unsafe fn is_reserved(res: *mut c_void) -> bool { kept.is_some_and(|k| desc_of(res) == Some(k)) } +/// The PC description for `CreateReservedResource(1)`: `pc_resource_desc_for_device`, preserving +/// PC texture layouts (`<= 3`, so invalid PC layouts like `ROW_MAJOR` are still rejected like the +/// host driver) and mapping console tile layouts (`> 3`) to `64KB_UNDEFINED_SWIZZLE`. +unsafe fn pc_reserved_desc_for_device(device: *mut c_void, x: &[u8; 0x38]) -> [u8; 0x38] { + let mut d = super::device_x::pc_resource_desc_for_device(device, x); + let dim = u32::from_le_bytes(d[0..4].try_into().unwrap()); + if dim != D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 { + let orig_layout = u32::from_le_bytes(x[0x2C..0x30].try_into().unwrap()); + let layout = if orig_layout > 3 { ~~~~ --- `pc_reserved_desc_for_device` keeps texture layout 0 (UNKNOWN) as it is, but PC CreateReservedResource only accepts 64KB_UNDEFINED_SWIZZLE / 64KB_STANDARD_SWIZZLE for textures. A console reserved Texture2D with Layout=0 plus console flag bits fails, the retry still has layout 0, and it fails again, which is the case the fallback was meant to cover.
bmjask commented 2026-12-01 21:12:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:12:38Z.

Review: COMMENTED


Review of 37fc811b: 5 finding(s). 4 are inline. These are on lines outside the diff:

crates/xcl/src/hle/d3d12x/real.rs:4412: lut3d::created still gets state & PC_RESOURCE_STATES, but the fallback in placed_in_heap_x / own_memory_if_larger_on_the_pc now creates the resource in the sanitized st (0x8 dropped, DS|UAV becomes DEPTH_WRITE) or in COMMON. A placed 3D sRGB+UAV texture requested in UNORDERED_ACCESS is created in COMMON while lut3d tracks UNORDERED_ACCESS, which gives barrier before-state mismatches and wrong dumps with XCL_DUMP_TARGETS/capture.


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:6140250409 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3291#pullrequestreview-6140250409). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T21:12:38Z. **Review:** COMMENTED --- Review of `37fc811b`: 5 finding(s). 4 are inline. These are on lines outside the diff: **`crates/xcl/src/hle/d3d12x/real.rs:4412`**: `lut3d::created` still gets `state & PC_RESOURCE_STATES`, but the fallback in placed_in_heap_x / own_memory_if_larger_on_the_pc now creates the resource in the sanitized `st` (0x8 dropped, DS|UAV becomes DEPTH_WRITE) or in COMMON. A placed 3D sRGB+UAV texture requested in UNORDERED_ACCESS is created in COMMON while lut3d tracks UNORDERED_ACCESS, which gives barrier before-state mismatches and wrong dumps with XCL_DUMP_TARGETS/capture. --- _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._
Kalmarv commented 2026-12-02 20:48:56 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T20:48:56Z.


Consolidated into and superseded by #3729 (https://github.com/xcl-dev/xcl/pull/3729), which combines #3291, #3414, #3418, and #3376 and addresses all open review findings in one PR. Closing this one.

<!-- greyfinch-sync:ivall/xcl:comment:7358360720 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3291#issuecomment-7358360720). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-12-02T20:48:56Z. --- Consolidated into and superseded by #3729 (https://github.com/xcl-dev/xcl/pull/3729), which combines #3291, #3414, #3418, and #3376 and addresses all open review findings in one PR. Closing this one.

Pull request closed

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!3291
No description provided.