D3D12.X: sanitize optimized clear values against PC resource descriptors #3418

Closed
Kalmarv wants to merge 1 commit from upstream-pr/3418 into main
Kalmarv commented 2026-11-29 14:51:22 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T14:51:22Z.


What is in it

pc_clear_for_desc in crates/xcl/src/hle/d3d12x/device_x.rs previously only remapped promoted B5G6R5 / B8G8R8X8 UAV target formats and console-only formats in D3D12_CLEAR_VALUE, passing other combinations straight through to PC D3D12 where CreateCommittedResource / CreatePlacedResource reject them with E_INVALIDARG:

  • Adds pc_clear_for_resource_desc (and extends pc_clear_format) in crates/xcl/src/hle/d3d12x/device_x.rs:
    • Returns null when the PC resource is a buffer (D3D12_RESOURCE_DIMENSION_BUFFER) or a texture without ALLOW_RENDER_TARGET or ALLOW_DEPTH_STENCIL.
    • For depth textures (ALLOW_DEPTH_STENCIL), maps DXGI_FORMAT_UNKNOWN (0) or typeless depth clear formats (19/39/44/53, which pc_resource_desc itself produces for depth buffers) to their unique typed depth format (20/40/45/55).
    • For concrete render-target textures, maps DXGI_FORMAT_UNKNOWN (0) or typeless clear formats to the resource's concrete pc_format.
    • For typeless color render targets (including R32_TYPELESS without ALLOW_DEPTH_STENCIL), preserves concrete clear formats such as R8G8B8A8_UNORM_SRGB or R32_FLOAT, while dropping unresolvable UNKNOWN/typeless clear values to null rather than failing resource creation.
  • Wires pc_clear_for_resource_desc into create_placed_resource_x (device_x.rs) and dev_create_committed_resource_x (real.rs), and passes null pOptimizedClearValue for coherent UMA buffer placements in place_console_buffer and place_at_title_address (real.rs). The existing pc_clear_for_desc signature is kept intact (and benefits from the pc_clear_format improvements) so open PR #3291 has zero merge conflict.
  • Adds unit test pc_clear_for_resource_desc_sanitizes_formats_and_non_targets and D3D12 verification test gpu_pc_clear_for_resource_desc_sanitizes_invalid_clear_values covering 11 invalid clear-value cases (all rejected raw by CreateCommittedResource and accepted after sanitization) plus 3 preserved valid concrete clear cases.

Tested

  • Causal reproduction + fix verification (cargo test -p xcl --lib -- hle::d3d12x::device_x::tests::):
    • Before fix: all 10 invalid clear-value cases passed through pc_clear_for_desc failed ID3D12Device::CreateCommittedResource with E_INVALIDARG.
    • After fix: 22 passed, 0 failed, including pc_clear_for_resource_desc_sanitizes_formats_and_non_targets, gpu_pc_clear_for_resource_desc_sanitizes_invalid_clear_values, pc_clear_for_desc_aligns_promoted_uav_targets_only, and gpu_promoted_uav_clear_values_pass_create_committed_resource.
  • Full workspace test suite (cargo test --workspace --no-fail-fast -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::, RTX 5070 Ti + DXC 1.9): 41 binaries, 2713 passed, 4 failed, 49 ignored, 9 filtered; 27 SKIPPED (22 no wave64, 3 refused #1426, 2 no heap windows). All 4 failures are pre-existing geometry test failures on main (3b6a91ed, tracked in #3411): gpu::dxil::geometry_tests::gpu_geometry_msg_gs_done_records, gpu::dxil::geometry_tests::msg_gs_done_emits_in_dxil, gpu::dxil::lower_geom::tests::gpu_epilogue_filters_oob_triangles_and_no_store_emits, and gpu::dxil::lower_mem::stage_guard_tests::geometry_export_rejected.

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: 5c616e862c175eafc21db1aa2cd8497c4c9db72f
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1701 (PresentX 1701)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 32748 MB

Not tested

  • Hardware adapters with wave64-only shaders or non-NVIDIA drivers (22 wave64 tests skipped on RTX 5070 Ti).
  • Full title execution for games other than ea3b5e4d or paths requiring uma_driver::tests:: kernel driver tests.
  • placed_in_heap_x fallback and own_memory_if_larger_on_the_pc in real.rs still call pc_clear_for_desc (leaving those lines untouched to avoid conflicting with #3291; they still inherit the pc_clear_format improvements).

AI use

Used an AI coding assistant to audit optimized clear-value handling across device_x.rs and real.rs, reproduce the CreateCommittedResource rejections on D3D12, implement the fix and synthetic unit/GPU tests, and run local verification.

<!-- greyfinch-sync:ivall/xcl:issue:5551867235 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3418). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T14:51:22Z. --- **What is in it** `pc_clear_for_desc` in `crates/xcl/src/hle/d3d12x/device_x.rs` previously only remapped promoted `B5G6R5` / `B8G8R8X8` UAV target formats and console-only formats in `D3D12_CLEAR_VALUE`, passing other combinations straight through to PC D3D12 where `CreateCommittedResource` / `CreatePlacedResource` reject them with `E_INVALIDARG`: - Adds `pc_clear_for_resource_desc` (and extends `pc_clear_format`) in `crates/xcl/src/hle/d3d12x/device_x.rs`: - Returns null when the PC resource is a buffer (`D3D12_RESOURCE_DIMENSION_BUFFER`) or a texture without `ALLOW_RENDER_TARGET` or `ALLOW_DEPTH_STENCIL`. - For depth textures (`ALLOW_DEPTH_STENCIL`), maps `DXGI_FORMAT_UNKNOWN` (`0`) or typeless depth clear formats (`19`/`39`/`44`/`53`, which `pc_resource_desc` itself produces for depth buffers) to their unique typed depth format (`20`/`40`/`45`/`55`). - For concrete render-target textures, maps `DXGI_FORMAT_UNKNOWN` (`0`) or typeless clear formats to the resource's concrete `pc_format`. - For typeless color render targets (including `R32_TYPELESS` without `ALLOW_DEPTH_STENCIL`), preserves concrete clear formats such as `R8G8B8A8_UNORM_SRGB` or `R32_FLOAT`, while dropping unresolvable `UNKNOWN`/typeless clear values to null rather than failing resource creation. - Wires `pc_clear_for_resource_desc` into `create_placed_resource_x` (`device_x.rs`) and `dev_create_committed_resource_x` (`real.rs`), and passes null `pOptimizedClearValue` for coherent UMA buffer placements in `place_console_buffer` and `place_at_title_address` (`real.rs`). The existing `pc_clear_for_desc` signature is kept intact (and benefits from the `pc_clear_format` improvements) so open PR #3291 has zero merge conflict. - Adds unit test `pc_clear_for_resource_desc_sanitizes_formats_and_non_targets` and D3D12 verification test `gpu_pc_clear_for_resource_desc_sanitizes_invalid_clear_values` covering 11 invalid clear-value cases (all rejected raw by `CreateCommittedResource` and accepted after sanitization) plus 3 preserved valid concrete clear cases. **Tested** - Causal reproduction + fix verification (`cargo test -p xcl --lib -- hle::d3d12x::device_x::tests::`): - Before fix: all 10 invalid clear-value cases passed through `pc_clear_for_desc` failed `ID3D12Device::CreateCommittedResource` with `E_INVALIDARG`. - After fix: `22 passed, 0 failed`, including `pc_clear_for_resource_desc_sanitizes_formats_and_non_targets`, `gpu_pc_clear_for_resource_desc_sanitizes_invalid_clear_values`, `pc_clear_for_desc_aligns_promoted_uav_targets_only`, and `gpu_promoted_uav_clear_values_pass_create_committed_resource`. - Full workspace test suite (`cargo test --workspace --no-fail-fast -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::`, RTX 5070 Ti + DXC 1.9): `41 binaries, 2713 passed, 4 failed, 49 ignored, 9 filtered; 27 SKIPPED (22 no wave64, 3 refused #1426, 2 no heap windows)`. All 4 failures are pre-existing geometry test failures on `main` (`3b6a91ed`, tracked in #3411): `gpu::dxil::geometry_tests::gpu_geometry_msg_gs_done_records`, `gpu::dxil::geometry_tests::msg_gs_done_emits_in_dxil`, `gpu::dxil::lower_geom::tests::gpu_epilogue_filters_oob_triangles_and_no_store_emits`, and `gpu::dxil::lower_mem::stage_guard_tests::geometry_export_rejected`. **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: 5c616e862c175eafc21db1aa2cd8497c4c9db72f gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1701 (PresentX 1701) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 32748 MB ``` **Not tested** - Hardware adapters with wave64-only shaders or non-NVIDIA drivers (`22` wave64 tests skipped on RTX 5070 Ti). - Full title execution for games other than `ea3b5e4d` or paths requiring `uma_driver::tests::` kernel driver tests. - `placed_in_heap_x` fallback and `own_memory_if_larger_on_the_pc` in `real.rs` still call `pc_clear_for_desc` (leaving those lines untouched to avoid conflicting with #3291; they still inherit the `pc_clear_format` improvements). **AI use** Used an AI coding assistant to audit optimized clear-value handling across `device_x.rs` and `real.rs`, reproduce the `CreateCommittedResource` rejections on D3D12, implement the fix and synthetic unit/GPU tests, and run local verification.
Foniass commented 2026-12-01 04:11:41 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T04:11:41Z.


merge-check
base: d0832e614f
head: 5c616e862c
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2892 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 (device_x.rs, real.rs). It changes the default path: console committed and placed resources that aren't render targets or depth buffers are now created without an optimized clear value, and a missing or typeless clear format is filled in. Every case I traced was one the PC already refused with E_INVALIDARG (the WARP test checks that); I found none where an accepted clear value is now changed or dropped. Findings: (1) the real.rs changes passing a null clear for buffers (place_console_buffer ~2173, place_at_title_address ~2271) have no test; the tests only call pc_clear_for_resource_desc directly. Both functions only handle buffers, so the change is right. (2) The readability check runs before the buffer check (~1072), so an unreadable clear pointer still reaches buffers (as on main). (3) is_typeless_format leaves out 21/22 and 46/47, so pc_target_format(120) = 47 would still be refused as a depth clear format (unlikely from a title). (4) Nits: let _ = clear; leaves an unused parameter; make_desc is copied into both tests; the description says 10 invalid cases in one place and 11 in another. (5) AI use names no tool or model. The PR's CI run failed on hle::kernelx::tests::a_page_whose_move_fails_stays_mapped_where_it_was, outside this diff; it passes in the run above on this tree, so that CI failure looks flaky. Fine: the typeless and depth format mappings match DXGI and what pc_resource_desc makes of depth buffers, the promoted formats 85/88/67/118 behave as before, place_at_title_address callers only pass buffers, and #3376 / #3291 edit other lines of the same functions.
performance: 60 s smoke of a retail title, main d0832e61 vs this merge (0b01bb0e): 1516 -> 1685 frames shown (111 %; last presents per 2 s: 60 60 60), peak commit 18133 -> 17130 MB, 0 device removals, 0 crashes.
verdict: safe to merge (after a CI rerun; the description should name the AI model)
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:7300475899 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3418#issuecomment-7300475899). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T04:11:41Z. --- merge-check base: d0832e614fa7d6ae027bb4feb27ccb9121f54a8f head: 5c616e862c175eafc21db1aa2cd8497c4c9db72f merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2892 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 (device_x.rs, real.rs). It changes the default path: console committed and placed resources that aren't render targets or depth buffers are now created without an optimized clear value, and a missing or typeless clear format is filled in. Every case I traced was one the PC already refused with E_INVALIDARG (the WARP test checks that); I found none where an accepted clear value is now changed or dropped. Findings: (1) the `real.rs` changes passing a null clear for buffers (`place_console_buffer` ~2173, `place_at_title_address` ~2271) have no test; the tests only call `pc_clear_for_resource_desc` directly. Both functions only handle buffers, so the change is right. (2) The readability check runs before the buffer check (~1072), so an unreadable clear pointer still reaches buffers (as on main). (3) `is_typeless_format` leaves out 21/22 and 46/47, so `pc_target_format(120)` = 47 would still be refused as a depth clear format (unlikely from a title). (4) Nits: `let _ = clear;` leaves an unused parameter; `make_desc` is copied into both tests; the description says 10 invalid cases in one place and 11 in another. (5) AI use names no tool or model. The PR's CI run failed on `hle::kernelx::tests::a_page_whose_move_fails_stays_mapped_where_it_was`, outside this diff; it passes in the run above on this tree, so that CI failure looks flaky. Fine: the typeless and depth format mappings match DXGI and what `pc_resource_desc` makes of depth buffers, the promoted formats 85/88/67/118 behave as before, `place_at_title_address` callers only pass buffers, and #3376 / #3291 edit other lines of the same functions. performance: 60 s smoke of a retail title, main d0832e61 vs this merge (0b01bb0e): 1516 -> 1685 frames shown (111 %; last presents per 2 s: 60 60 60), peak commit 18133 -> 17130 MB, 0 device removals, 0 crashes. verdict: safe to merge (after a CI rerun; the description should name the AI model) 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 05:15:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T05:15:48Z.

Code review location: crates/xcl/src/hle/d3d12x/device_x.rs; line 1084

@@ -1035,6 +1061,38 @@ pub(super) unsafe fn pc_clear_for_desc(clear: *const c_void, pc_format: u32, buf
     buf.as_ptr() as *const c_void
 }
 
+/// Sanitize a `D3D12_CLEAR_VALUE` against a PC `D3D12_RESOURCE_DESC`:
+/// - buffers and textures without `ALLOW_RENDER_TARGET` or `ALLOW_DEPTH_STENCIL` cannot carry a
+///   non-null `pOptimizedClearValue` on PC D3D12 (`E_INVALIDARG`), so return null;
+/// - depth textures (`ALLOW_DEPTH_STENCIL`) map `UNKNOWN` or typeless clear formats (`19`/`39`/
+///   `44`/`53`) to their unique depth format (`20`/`40`/`45`/`55`);
+/// - concrete render targets map `UNKNOWN` or typeless clear formats to `pc_format`, while
+///   unresolvable `UNKNOWN`/typeless clear formats on typeless color textures return null.
+pub(super) unsafe fn pc_clear_for_resource_desc(clear: *const c_void, pc_desc: &[u8; 0x38], buf: &mut [u8; 24]) -> *const c_void {
+    if clear.is_null() || !crate::gpu::shader::readable(clear as usize, 24) {
+        return clear;
+    }
+    let dim = u32::from_le_bytes(pc_desc[0..4].try_into().unwrap());
+    let pc_format = u32::from_le_bytes(pc_desc[0x20..0x24].try_into().unwrap());
+    let pc_flags = u32::from_le_bytes(pc_desc[0x30..0x34].try_into().unwrap());
+    if dim == D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 || (pc_flags & 3) == 0 {
+        return std::ptr::null();
+    }
+    std::ptr::copy_nonoverlapping(clear as *const u8, buf.as_mut_ptr(), 24);
+    let raw = u32::from_le_bytes(buf[0..4].try_into().unwrap());
+    let mut f = pc_clear_format(pc_format, raw);
+    if (pc_flags & 2) != 0 && (f == 0 || is_typeless_format(f)) {

For ALLOW_DEPTH_STENCIL textures the depth remap only runs when the clear format is UNKNOWN or typeless, so a concrete clear format that isn't the family's depth format passes through unchanged. A console D32 buffer (PC R32_TYPELESS, 39) created with clear Format R32_FLOAT (41), which console code uses for depth views per the pc_shader_view_format comment, keeps 41, and CreateCommittedResource/CreatePlacedResource return E_INVALIDARG. The same goes for D16S8 (PC 44) cleared with D16_UNORM (55) or R16_UNORM (56). Each depth family has exactly one DSV format, so using depth_clear_format(pc_format) whenever it's Some would fix all of them.

<!-- greyfinch-sync:ivall/xcl:review-comment:4765070271 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3418#discussion_r4765070271). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T05:15:48Z. **Code review location:** `crates/xcl/src/hle/d3d12x/device_x.rs`; line 1084 ~~~~diff @@ -1035,6 +1061,38 @@ pub(super) unsafe fn pc_clear_for_desc(clear: *const c_void, pc_format: u32, buf buf.as_ptr() as *const c_void } +/// Sanitize a `D3D12_CLEAR_VALUE` against a PC `D3D12_RESOURCE_DESC`: +/// - buffers and textures without `ALLOW_RENDER_TARGET` or `ALLOW_DEPTH_STENCIL` cannot carry a +/// non-null `pOptimizedClearValue` on PC D3D12 (`E_INVALIDARG`), so return null; +/// - depth textures (`ALLOW_DEPTH_STENCIL`) map `UNKNOWN` or typeless clear formats (`19`/`39`/ +/// `44`/`53`) to their unique depth format (`20`/`40`/`45`/`55`); +/// - concrete render targets map `UNKNOWN` or typeless clear formats to `pc_format`, while +/// unresolvable `UNKNOWN`/typeless clear formats on typeless color textures return null. +pub(super) unsafe fn pc_clear_for_resource_desc(clear: *const c_void, pc_desc: &[u8; 0x38], buf: &mut [u8; 24]) -> *const c_void { + if clear.is_null() || !crate::gpu::shader::readable(clear as usize, 24) { + return clear; + } + let dim = u32::from_le_bytes(pc_desc[0..4].try_into().unwrap()); + let pc_format = u32::from_le_bytes(pc_desc[0x20..0x24].try_into().unwrap()); + let pc_flags = u32::from_le_bytes(pc_desc[0x30..0x34].try_into().unwrap()); + if dim == D3D12_RESOURCE_DIMENSION_BUFFER.0 as u32 || (pc_flags & 3) == 0 { + return std::ptr::null(); + } + std::ptr::copy_nonoverlapping(clear as *const u8, buf.as_mut_ptr(), 24); + let raw = u32::from_le_bytes(buf[0..4].try_into().unwrap()); + let mut f = pc_clear_format(pc_format, raw); + if (pc_flags & 2) != 0 && (f == 0 || is_typeless_format(f)) { ~~~~ --- For ALLOW_DEPTH_STENCIL textures the depth remap only runs when the clear format is UNKNOWN or typeless, so a concrete clear format that isn't the family's depth format passes through unchanged. A console D32 buffer (PC R32_TYPELESS, 39) created with clear Format R32_FLOAT (41), which console code uses for depth views per the `pc_shader_view_format` comment, keeps 41, and CreateCommittedResource/CreatePlacedResource return E_INVALIDARG. The same goes for D16S8 (PC 44) cleared with D16_UNORM (55) or R16_UNORM (56). Each depth family has exactly one DSV format, so using `depth_clear_format(pc_format)` whenever it's Some would fix all of them.
bmjask commented 2026-12-02 05:15:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T05:15:48Z.

Code review location: crates/xcl/src/hle/d3d12x/device_x.rs; line 1016

@@ -1012,13 +1012,39 @@ pub(super) unsafe fn pc_clear(clear: *const c_void, buf: &mut [u8; 24]) -> *cons
     buf.as_ptr() as *const c_void
 }
 
+/// Whether `f` is a typeless DXGI format (invalid in `D3D12_CLEAR_VALUE::Format`).
+fn is_typeless_format(f: u32) -> bool {

is_typeless_format leaves out the partially-typeless view formats 21 (R32_FLOAT_X8X24_TYPELESS), 22 (X32_TYPELESS_G8X24_UINT), 46 (R24_UNORM_X8_TYPELESS) and 47 (X24_TYPELESS_G8_UINT), which are also invalid as D3D12_CLEAR_VALUE formats. A depth texture with pc_format 44 and clear 46 (or 21 on a 19 resource) passes through as is, and the PC returns E_INVALIDARG.

<!-- greyfinch-sync:ivall/xcl:review-comment:4765070278 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3418#discussion_r4765070278). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T05:15:48Z. **Code review location:** `crates/xcl/src/hle/d3d12x/device_x.rs`; line 1016 ~~~~diff @@ -1012,13 +1012,39 @@ pub(super) unsafe fn pc_clear(clear: *const c_void, buf: &mut [u8; 24]) -> *cons buf.as_ptr() as *const c_void } +/// Whether `f` is a typeless DXGI format (invalid in `D3D12_CLEAR_VALUE::Format`). +fn is_typeless_format(f: u32) -> bool { ~~~~ --- `is_typeless_format` leaves out the partially-typeless view formats 21 (R32_FLOAT_X8X24_TYPELESS), 22 (X32_TYPELESS_G8X24_UINT), 46 (R24_UNORM_X8_TYPELESS) and 47 (X24_TYPELESS_G8_UINT), which are also invalid as D3D12_CLEAR_VALUE formats. A depth texture with pc_format 44 and clear 46 (or 21 on a 19 resource) passes through as is, and the PC returns E_INVALIDARG.
bmjask commented 2026-12-02 05:15:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T05:15:48Z.

Code review location: crates/xcl/src/hle/d3d12x/device_x.rs; line 1072

@@ -1035,6 +1061,38 @@ pub(super) unsafe fn pc_clear_for_desc(clear: *const c_void, pc_format: u32, buf
     buf.as_ptr() as *const c_void
 }
 
+/// Sanitize a `D3D12_CLEAR_VALUE` against a PC `D3D12_RESOURCE_DESC`:
+/// - buffers and textures without `ALLOW_RENDER_TARGET` or `ALLOW_DEPTH_STENCIL` cannot carry a
+///   non-null `pOptimizedClearValue` on PC D3D12 (`E_INVALIDARG`), so return null;
+/// - depth textures (`ALLOW_DEPTH_STENCIL`) map `UNKNOWN` or typeless clear formats (`19`/`39`/
+///   `44`/`53`) to their unique depth format (`20`/`40`/`45`/`55`);
+/// - concrete render targets map `UNKNOWN` or typeless clear formats to `pc_format`, while
+///   unresolvable `UNKNOWN`/typeless clear formats on typeless color textures return null.
+pub(super) unsafe fn pc_clear_for_resource_desc(clear: *const c_void, pc_desc: &[u8; 0x38], buf: &mut [u8; 24]) -> *const c_void {
+    if clear.is_null() || !crate::gpu::shader::readable(clear as usize, 24) {

pc_clear_for_resource_desc checks readability before checking buffer/non-target, so an unreadable non-null clear pointer is returned as is, even for buffers and non-RT/DS textures where the answer should always be null. A garbage or stale pClearValue for a buffer then reaches the PC driver, which dereferences it or returns E_INVALIDARG. Checking dim/flags first returns null unconditionally (and skips a readable() query on every buffer creation).

<!-- greyfinch-sync:ivall/xcl:review-comment:4765070282 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3418#discussion_r4765070282). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T05:15:48Z. **Code review location:** `crates/xcl/src/hle/d3d12x/device_x.rs`; line 1072 ~~~~diff @@ -1035,6 +1061,38 @@ pub(super) unsafe fn pc_clear_for_desc(clear: *const c_void, pc_format: u32, buf buf.as_ptr() as *const c_void } +/// Sanitize a `D3D12_CLEAR_VALUE` against a PC `D3D12_RESOURCE_DESC`: +/// - buffers and textures without `ALLOW_RENDER_TARGET` or `ALLOW_DEPTH_STENCIL` cannot carry a +/// non-null `pOptimizedClearValue` on PC D3D12 (`E_INVALIDARG`), so return null; +/// - depth textures (`ALLOW_DEPTH_STENCIL`) map `UNKNOWN` or typeless clear formats (`19`/`39`/ +/// `44`/`53`) to their unique depth format (`20`/`40`/`45`/`55`); +/// - concrete render targets map `UNKNOWN` or typeless clear formats to `pc_format`, while +/// unresolvable `UNKNOWN`/typeless clear formats on typeless color textures return null. +pub(super) unsafe fn pc_clear_for_resource_desc(clear: *const c_void, pc_desc: &[u8; 0x38], buf: &mut [u8; 24]) -> *const c_void { + if clear.is_null() || !crate::gpu::shader::readable(clear as usize, 24) { ~~~~ --- `pc_clear_for_resource_desc` checks readability before checking buffer/non-target, so an unreadable non-null clear pointer is returned as is, even for buffers and non-RT/DS textures where the answer should always be null. A garbage or stale pClearValue for a buffer then reaches the PC driver, which dereferences it or returns E_INVALIDARG. Checking dim/flags first returns null unconditionally (and skips a `readable()` query on every buffer creation).
bmjask commented 2026-12-02 05:15:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T05:15:48Z.

Review: COMMENTED


Review of 5c616e86: 5 finding(s). 3 are inline. These are on lines outside the diff:

crates/xcl/src/hle/d3d12x/real.rs:4474: The fallback paths in placed_in_heap_x (4474) and own_memory_if_larger_on_the_pc (4534) still use pc_clear_for_desc, which never nulls the clear for buffers or non-RT/DS textures and leaves typeless depth clears unresolved, so the same E_INVALIDARG remains there. For a sampled-only texture (flags 0) placed with a non-null clear whose PC size is larger, own_memory_if_larger's CreateCommittedResource fails and returns None. The code then falls back to the overlapping placement it exists to avoid, and the hr2/hr3 fallback in placed_in_heap_x fails the same way. Both should call pc_clear_for_resource_desc(clear, &d, ...).

crates/xcl/src/hle/d3d12x/real.rs:3837: create_display_target, reached from dev_create_committed_resource_layout_x (#127) with ALLOW_DISPLAY, passes the raw console clear value, though the desc has already been rewritten by pc_resource_desc. A display target in R9G9B9E5 (67) or B5G5R5A1 (86) with RT flags is promoted to 26/28 while the clear still says 67/86, so CreateCommittedResource returns E_INVALIDARG and display target creation fails.


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:6145152948 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3418#pullrequestreview-6145152948). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T05:15:48Z. **Review:** COMMENTED --- Review of `5c616e86`: 5 finding(s). 3 are inline. These are on lines outside the diff: **`crates/xcl/src/hle/d3d12x/real.rs:4474`**: The fallback paths in `placed_in_heap_x` (4474) and `own_memory_if_larger_on_the_pc` (4534) still use `pc_clear_for_desc`, which never nulls the clear for buffers or non-RT/DS textures and leaves typeless depth clears unresolved, so the same E_INVALIDARG remains there. For a sampled-only texture (flags 0) placed with a non-null clear whose PC size is larger, `own_memory_if_larger`'s CreateCommittedResource fails and returns None. The code then falls back to the overlapping placement it exists to avoid, and the hr2/hr3 fallback in `placed_in_heap_x` fails the same way. Both should call `pc_clear_for_resource_desc(clear, &d, ...)`. **`crates/xcl/src/hle/d3d12x/real.rs:3837`**: `create_display_target`, reached from `dev_create_committed_resource_layout_x` (#127) with ALLOW_DISPLAY, passes the raw console clear value, though the desc has already been rewritten by `pc_resource_desc`. A display target in R9G9B9E5 (67) or B5G5R5A1 (86) with RT flags is promoted to 26/28 while the clear still says 67/86, so CreateCommittedResource returns E_INVALIDARG and display target creation fails. --- _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._
bmjask commented 2026-12-02 14:14:12 +00:00 (Migrated from github.com)

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


@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.

<!-- greyfinch-sync:ivall/xcl:comment:7348210093 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3418#issuecomment-7348210093). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T14:14:12Z. --- @Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.
Kalmarv commented 2026-12-02 20:49:01 +00:00 (Migrated from github.com)

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


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:7358362996 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3418#issuecomment-7358362996). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-12-02T20:49:01Z. --- 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!3418
No description provided.