Skip to content

D3D12.X: sanitize null-resource SRV, image UAV, and RTV formats - #3319

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/null-view-format-sanitize
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/null-view-format-sanitize

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Sanitize null-resource (pResource == NULL) SRV, image UAV, and RTV formats in crates/xcl/src/hle/d3d12x/real.rs so console depth/stencil, sRGB, promoted target, and host-unsupported formats never trigger DXGI_ERROR_INVALID_CALL (0x887A0001) device removal on PC D3D12.

What is in it

  • crates/xcl/src/hle/d3d12x/real.rs:
    • dev_create_srv_inner: when res.is_null() for an image SRV dimension, map depth and depth-typeless formats (D16_UNORM_S8_UINT -> D24_UNORM_S8_UINT -> R24_UNORM_X8_TYPELESS, D32_FLOAT / R32_TYPELESS -> R32_FLOAT, D16_UNORM / R16_TYPELESS -> R16_UNORM, D32_FLOAT_S8X24_UINT / R32G8X24_TYPELESS -> R32_FLOAT_X8X24_TYPELESS) in addition to UNKNOWN -> R32_FLOAT, and set PlaneSlice = 1 for null Texture2D / Texture2DArray stencil SRVs (X24_TYPELESS_G8_UINT, X32_TYPELESS_G8X24_UINT).
    • dev_create_uav_x: when res.is_null() for an image UAV dimension, map through pc_target_format and pc_uav_linear_format and fall back to DXGI_FORMAT_R32_FLOAT when the result is UNKNOWN or lacks host TYPED_UNORDERED_ACCESS_VIEW support (pc_typed_uav_supported), leaving null buffer UAVs untouched.
    • dev_create_rtv: when res.is_null(), map through pc_target_format (promoting B5G5R5A1_UNORM / B4G4R4A4_UNORM -> R8G8B8A8_UNORM and R9G9B9E5_SHAREDEXP -> R11G11B10_FLOAT) and map UNKNOWN to R8G8B8A8_UNORM.
    • Add a child-isolated WARP + D3D12 debug-layer test null_view_format_tests::null_srv_uav_and_rtv_sanitize_console_and_incompatible_formats covering 11 null SRV cases, 11 null image UAV cases, and 4 null RTV cases and asserting zero D3D12 debug-layer errors and no device removal.

Tested

  • Targeted child-isolated WARP + D3D12 debug-layer test (cargo test -p xcl --lib -- hle::d3d12x::real::null_view_format_tests::):
    • Before the fix: failed on the first null SRV (fmt=118, mapped to D24_UNORM_S8_UINT) with D3D12 debug-layer [id=28] ID3D12Device::CreateShaderResourceView: The format (0x2d, D24_UNORM_S8_UINT) cannot be used with a ShaderResource view and [id=232] ID3D12Device::RemoveDevice (DXGI_ERROR_INVALID_CALL).
    • After the fix: 1 passed; 0 failed (0.18s, zero debug-layer errors across all 26 null SRV/UAV/RTV cases).
  • Full workspace test suite on Windows 11 Pro (rustc 1.99.0, RTX 5070 Ti driver 32.0.16.1074, DXC 1.9.2609.5):
    • Command: cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::
    • Result: 2600 passed; 0 failed; 49 ignored (301.6s).
    • SKIPPED: 1 (1 no heap windows).

Regression run
Baseline (main ancestor f0e8fdd5c3942ddd399d5ee11384c8595c5aacff) and this PR (ea2aead7aadaa1505eb83c4a92a1832ee51e8baa), 150 s on RTX 5070 Ti:

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

Not tested

  • AMD RDNA hardware, macOS/Linux hosts, and non-depth color *_TYPELESS formats on null SRVs or null RTVs.
  • test_title_runs_and_game_folder_is_untouched and uma_driver::tests:: were skipped in the workspace test run.

AI use

  • Implemented, debugged, and verified with an AI coding assistant; all code, tests, workspace results, and smoke output were reviewed and verified locally.

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 29, 2026
@Kalmarv Kalmarv closed this Nov 29, 2026
@Kalmarv Kalmarv reopened this Nov 29, 2026
@Kalmarv Kalmarv closed this Nov 29, 2026
@Kalmarv Kalmarv reopened this Nov 29, 2026
@Kalmarv Kalmarv closed this Nov 29, 2026
@Kalmarv Kalmarv reopened this Nov 29, 2026
@Kalmarv Kalmarv closed this Nov 29, 2026
@Kalmarv Kalmarv reopened this Nov 29, 2026
@Foniass

Foniass commented Nov 29, 2026

Copy link
Copy Markdown
Contributor
merge-check
base: d8bd9a38204667f3ca7dd9eeffcc2f1c937b07b0
head: ea2aead7aadaa1505eb83c4a92a1832ee51e8baa
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2615 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: only null-resource views change. The sanitized format also goes into the T# that shaders read (a null D32 SRV now reports R32_FLOAT), which the PR does not disclose; these calls were invalid before. The debug-layer test checks less without Graphics Tools. It conflicts trivially (test module appended at the end of real.rs) with #3313, #3297 and #3291. AI use names no model.
performance: 60 s smoke: 1671 frames (99.9 % of d8bd9a38), peak commit 16827 MB (93.8 %)
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.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: d0832e6
head: ea2aead
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 (real.rs, +204/-10). It changes the default path for null views only: null image SRVs in depth formats or their typeless families (and console D16S8 -> D24S8) get a readable format, the two stencil formats get PlaneSlice 1, null image UAVs go through the target / linear / typed-UAV-support mapping with an R32_FLOAT fallback, and null RTVs get the target-format mapping (UNKNOWN becomes R8G8B8A8_UNORM). Findings: (1) the new test ignores a failed D3D12GetDebugInterface (~15097); without the debug layer, collect_d3d12_errors (~15044) returns nothing, so the 26 per-view checks pass without testing anything and with no SKIPPED notice, and the parent drops the child's output on success (~15086). Calling skip_test("no D3D12 debug layer") in the child and forwarding its output would fix it. (2) PlaneSlice 1 is set only for Texture2D / Texture2DArray null stencil views (~3239); stencil cube and MSAA views aren't covered. (3) AI use names no tool or model. Fine: the readable formats agree with pc_shader_view_format, the data type the null T# gives shaders is the same as on main, and the null UAV path reuses pc_typed_uav_supported as dev_create_uav_checked_inner does. Null buffer UAVs are left alone. #3376 and #3291 will only collide textually at the end of the file.
performance: 60 s smoke of a retail title, main d0832e6 vs this merge (a5056ffd): 1516 -> 1673 frames shown (110 %; last presents per 2 s: 60 60 60), peak commit 18133 -> 17520 MB, 0 device removals, 0 crashes.
verdict: safe to merge (the test should skip_test without the debug layer and forward the child's output; 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.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 1, 2026

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_shader_view_format(rf, c.Format.0 as u32) as i32);
} else if view_dim_to_image_type(c.ViewDimension.0).is_some() {
let mapped = super::device_x::pc_uav_linear_format(super::device_x::pc_target_format(c.Format.0 as u32));
let fmt = if mapped == 0 || !super::device_x::pc_typed_uav_supported(this, mapped as i32) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null image UAV fallback relies on pc_typed_uav_supported, which returns true when CheckFeatureSupport fails. A console-only format that pc_view_format/pc_target_format don't map (outside 116-120/86/115/67) gets E_INVALIDARG from the query, is reported as supported, and reaches CreateUnorderedAccessView unchanged, which removes the device. Typeless formats (e.g. R8G8B8A8_TYPELESS=27) can do the same on drivers that report TYPED_UNORDERED_ACCESS_VIEW or fail the query, since a typed view in a typeless format is always invalid. A failed query should count as unsupported.

} else if view_dim_to_image_type(c.ViewDimension.0).is_some() {
let mapped = super::device_x::pc_uav_linear_format(super::device_x::pc_target_format(c.Format.0 as u32));
let fmt = if mapped == 0 || !super::device_x::pc_typed_uav_supported(this, mapped as i32) {
windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32_FLOAT.0 as u32

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the null UAV format falls back to R32_FLOAT, the console's PlaneSlice is kept. A null Texture2D/Array UAV with PlaneSlice=1 and a depth/stencil format becomes R32_FLOAT with plane 1, which is out of range for a one-plane format, so the debug layer reports it and the device can be removed. The SRV branch has the same issue when a non-stencil format keeps PlaneSlice != 0.

&& view_dim_to_image_type(c.ViewDimension.0).is_some()
{
c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32_FLOAT;
if res.is_null() && view_dim_to_image_type(c.ViewDimension.0).is_some() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null SRV sanitizer only rewrites depth-family typeless and depth formats. Color and BC typeless formats fall through f => f, so a null Texture2D SRV with R8G8B8A8_TYPELESS (27), R16G16B16A16_TYPELESS (9) or BC1_TYPELESS (70) still reaches CreateShaderResourceView typeless. D3D12 rejects that with the same 0x887A0001 removal this PR is meant to stop.

let rf = ID3D12Resource::from_raw_borrowed(&res).map_or(0, |r| r.GetDesc().Format.0 as u32);
c.Format = windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(super::device_x::pc_shader_view_format(rf, c.Format.0 as u32) as i32);
let f = if res.is_null() {
match super::device_x::pc_target_format(c.Format.0 as u32) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null RTV path only applies pc_target_format plus UNKNOWN->RGBA8, so depth, typeless and non-renderable formats pass through. A null RTV with D24_UNORM_S8_UINT (45), console D16S8 (118, which pc_target_format maps to 45) or R8G8B8A8_TYPELESS (27) reaches CreateRenderTargetView with a format that can't be an RTV, and the device is removed. The SRV path in this PR does sanitize depth formats; the RTV path doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) needs:amd-test Needs a test report or merge-check on an AMD GPU needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants