D3D12.X: sanitize optimized clear values against PC resource descriptors #3418
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3418"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T14:51:22Z.
What is in it
pc_clear_for_descincrates/xcl/src/hle/d3d12x/device_x.rspreviously only remapped promotedB5G6R5/B8G8R8X8UAV target formats and console-only formats inD3D12_CLEAR_VALUE, passing other combinations straight through to PC D3D12 whereCreateCommittedResource/CreatePlacedResourcereject them withE_INVALIDARG:pc_clear_for_resource_desc(and extendspc_clear_format) incrates/xcl/src/hle/d3d12x/device_x.rs:D3D12_RESOURCE_DIMENSION_BUFFER) or a texture withoutALLOW_RENDER_TARGETorALLOW_DEPTH_STENCIL.ALLOW_DEPTH_STENCIL), mapsDXGI_FORMAT_UNKNOWN(0) or typeless depth clear formats (19/39/44/53, whichpc_resource_descitself produces for depth buffers) to their unique typed depth format (20/40/45/55).DXGI_FORMAT_UNKNOWN(0) or typeless clear formats to the resource's concretepc_format.R32_TYPELESSwithoutALLOW_DEPTH_STENCIL), preserves concrete clear formats such asR8G8B8A8_UNORM_SRGBorR32_FLOAT, while dropping unresolvableUNKNOWN/typeless clear values to null rather than failing resource creation.pc_clear_for_resource_descintocreate_placed_resource_x(device_x.rs) anddev_create_committed_resource_x(real.rs), and passes nullpOptimizedClearValuefor coherent UMA buffer placements inplace_console_bufferandplace_at_title_address(real.rs). The existingpc_clear_for_descsignature is kept intact (and benefits from thepc_clear_formatimprovements) so open PR #3291 has zero merge conflict.pc_clear_for_resource_desc_sanitizes_formats_and_non_targetsand D3D12 verification testgpu_pc_clear_for_resource_desc_sanitizes_invalid_clear_valuescovering 11 invalid clear-value cases (all rejected raw byCreateCommittedResourceand accepted after sanitization) plus 3 preserved valid concrete clear cases.Tested
cargo test -p xcl --lib -- hle::d3d12x::device_x::tests::):pc_clear_for_descfailedID3D12Device::CreateCommittedResourcewithE_INVALIDARG.22 passed, 0 failed, includingpc_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, andgpu_promoted_uav_clear_values_pass_create_committed_resource.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 onmain(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, andgpu::dxil::lower_mem::stage_guard_tests::geometry_export_rejected.Regression run
Not tested
22wave64 tests skipped on RTX 5070 Ti).ea3b5e4dor paths requiringuma_driver::tests::kernel driver tests.placed_in_heap_xfallback andown_memory_if_larger_on_the_pcinreal.rsstill callpc_clear_for_desc(leaving those lines untouched to avoid conflicting with #3291; they still inherit thepc_clear_formatimprovements).AI use
Used an AI coding assistant to audit optimized clear-value handling across
device_x.rsandreal.rs, reproduce theCreateCommittedResourcerejections on D3D12, implement the fix and synthetic unit/GPU tests, and run local verification.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T04:11:41Z.
merge-check
base:
d0832e614fhead:
5c616e862cmerge: 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.rschanges passing a null clear for buffers (place_console_buffer~2173,place_at_title_address~2271) have no test; the tests only callpc_clear_for_resource_descdirectly. 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_formatleaves out 21/22 and 46/47, sopc_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_descis 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 onhle::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 whatpc_resource_descmakes of depth buffers, the promoted formats 85/88/67/118 behave as before,place_at_title_addresscallers only pass buffers, and #3376 / #3291 edit other lines of the same functions.performance: 60 s smoke of a retail title, main
d0832e61vs 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.
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 1084For 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_formatcomment, 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 usingdepth_clear_format(pc_format)whenever it's Some would fix all of them.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 1016is_typeless_formatleaves 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.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 1072pc_clear_for_resource_descchecks 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 areadable()query on every buffer creation).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 inplaced_in_heap_x(4474) andown_memory_if_larger_on_the_pc(4534) still usepc_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 inplaced_in_heap_xfails the same way. Both should callpc_clear_for_resource_desc(clear, &d, ...).crates/xcl/src/hle/d3d12x/real.rs:3837:create_display_target, reached fromdev_create_committed_resource_layout_x(#127) with ALLOW_DISPLAY, passes the raw console clear value, though the desc has already been rewritten bypc_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.
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.
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.
Pull request closed