Skip to content

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

Closed
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/reserved-placed-resource-sanitize
Closed

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/reserved-placed-resource-sanitize

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

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.

@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
@Foniass

Foniass commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator
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 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 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.

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);

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 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.

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 {

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.

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.

mut create: impl FnMut(usize, usize, usize) -> HRESULT,
) -> HRESULT {
let mut hr = create(desc, state, clear);
let mut used_state = state;

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 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.

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 {

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.

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.

@Kalmarv

Kalmarv commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Consolidated into and superseded by #3729 (#3729), which combines #3291, #3414, #3418, and #3376 and addresses all open review findings in one PR. Closing this one.

@Kalmarv Kalmarv closed this Dec 2, 2026
Kalmarv added a commit to Kalmarv/xcl that referenced this pull request Dec 3, 2026
Unifies console resource description, resource flag, initial state, optimized clear value, and heap property/flag sanitization across CreateCommittedResource(X), display target creation, CreatePlacedResource(X) and its fallbacks, and CreateReservedResource(1).

Consolidates and resolves all review findings from xcl-dev#3291, xcl-dev#3414, xcl-dev#3418, and xcl-dev#3376:
- pc_resource_desc enforces PC buffer/texture flag invariants (buffer strips DS/DENY_SRV/SIMULTANEOUS_ACCESS while keeping RT/UAV; texture RT|DS resolves by format family; depth strips UAV/SIMULTANEOUS_ACCESS; DENY_SHADER_RESOURCE requires DS or video reference flags; MSAA strips SIMULTANEOUS_ACCESS; textures strip ALLOW_CROSS_ADAPTER).
- pc_initial_state_for_desc derives initial state from sanitized PC flags and drops RENDER_TARGET, DEPTH_WRITE/READ, and UNORDERED_ACCESS when the matching PC flag is absent; placed console buffers created in UNORDERED_ACCESS or RAYTRACING_ACCELERATION_STRUCTURE promote ALLOW_UNORDERED_ACCESS before pc_initial_state_for_desc so place_at_title_address / sparse::place_buffer_in keep the UAV flag and state.
- pc_clear_for_resource_desc handles buffers/non-targets before reading the pointer, maps depth families (including typeless/partially-typeless formats) to their typed DSV clear format, resolves typeless/UNKNOWN clears on concrete RTs, and is used across every creation and fallback path.
- dev_create_committed_resource_x routes non-buffer ALLOW_DISPLAY resources through create_display_target (falling through to the existing path if it fails), retries sanitized heap properties (preserving CPU-visible console buffers as CUSTOM+L0) and heap flags only on failure, and logs both raw and tried values on failure.
- dev_create_reserved_resource(1) adds a console-only fallback (skipping own_work and PC-native invalid calls) that maps console layouts (0 or >3) to 64KB_UNDEFINED_SWIZZLE, refuses bytes-per-texel-changing format promotions, and propagates the actual PC state to lut3d/reserved_3d.

Assisted-by: Antigravity (Gemini 4 Neon)
Assisted-by: Claude Code (Claude Opus 5.5)
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants