Skip to content

D3D12.X: write MEM_NULL_LOAD/MEM_NULL_STORE raw UAVs after fill_nulls in shared_heap - #3349

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/shared-heap-null-window-uavs
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/shared-heap-null-window-uavs

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Write the shared view heap's out-of-layout null raw buffer UAVs (MEM_NULL_LOAD and MEM_NULL_STORE) after fill_nulls in console_heap::shared_heap (crates/xcl/src/hle/d3d12x/console_heap.rs), so fill_nulls no longer overwrites those two RWByteAddressBuffer null UAV slots with Texture2D null SRV/UAV descriptors.

What is in it

  • crates/xcl/src/hle/d3d12x/console_heap.rs:
    • Move null_window_views(dev, cpu, inc) after the fill_nulls(dev, cpu, inc, MEM_HEAP_BASE, cap, at) pass in shared_heap. Previously null_window_views ran first and fill_nulls immediately overwrote MEM_NULL_LOAD (even index 20) with a Texture2D null SRV and MEM_NULL_STORE (odd index 21) with a Texture2D null UAV, causing descriptor-type mismatches whenever XCL_HEAP_WINDOWS=1 shaders redirected out-of-layout memory loads/stores to those slots (set_window_views only writes MEM_HEAP_BASE..MEM_NULL_LOAD).
    • Add child-isolated WARP + D3D12 debug-layer + GPU-based validation (ID3D12Debug1::SetEnableGPUBasedValidation(true)) test hle::d3d12x::console_heap::tests::shared_heap_preserves_null_window_raw_uavs that initializes shared_heap, dispatches a Direct DXIL SM 6.6 compute shader accessing ResourceDescriptorHeap[MEM_NULL_LOAD] and ResourceDescriptorHeap[MEM_NULL_STORE] as RWByteAddressBuffer, verifies readback (0 ^ 0xA5A5_5A5A), and asserts 0 D3D12 debug/GBV errors.

Tested

  • Targeted console_heap tests (cargo test -p xcl --lib -j 4 -- hle::d3d12x::console_heap:: and cargo test -p xcl --test console_heap_contract -j 4):
    • Before the fix: shared_heap_preserves_null_window_raw_uavs failed on WARP+GBV with two D3D12 GPU-based validation errors ([id=939] Descriptor Heap Index: [20], Descriptor Type in Heap: D3D12_DESCRIPTOR_RANGE_TYPE_SRV, Register Type: D3D12_DESCRIPTOR_RANGE_TYPE_UAV and [id=941] UAV Dimension Expected: D3D12_UAV_DIMENSION_BUFFER, UAV Dimension In Descriptor: D3D12_UAV_DIMENSION_TEXTURE2D, Descriptor Heap Index: [21]).
    • After the fix: 26 passed; 0 failed (0.27s) in hle::d3d12x::console_heap:: (including shared_heap_preserves_null_window_raw_uavs with 0 GBV errors and 0xA5A5_5A5A readback) and 9 passed; 0 failed (0.01s) in console_heap_contract.
  • 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: 2610 passed; 0 failed; 49 ignored (128.2s).
    • SKIPPED: 26 runtime SKIPPED lines (21 wave64-only checks on this wave32 RTX 5070 Ti, 3 wave32 per-lane #1426 op refusals, 2 heap-windows opt-in checks).

Regression run
Baseline (main ancestor f0e8fdd5c3942ddd399d5ee11384c8595c5aacff) and this PR (5dc64ea70b240d48ee0da31b1deff64ae1260710), 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: 5dc64ea70b240d48ee0da31b1deff64ae1260710
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 4154 (PresentX 4154)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17074 MB

Not tested

  • AMD RDNA hardware, macOS/Linux hosts, and set_window_views slot expansion past windows.len().max(MAX_WINDOWS) (left untouched for #3096).

AI use

  • Drafted, verified, and tested with Antigravity (Claude Opus 5.5 for initial patch/GBV test; Gemini 4 Neon for verification and PR maintenance); all code, tests, and regression runs were validated locally.

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

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: d0832e6
head: 5dc64ea
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 (console_heap.rs: a 3-line move plus the test). The bug is real on main: shared_heap writes the raw null UAVs (~503) and then fill_nulls(MEM_HEAP_BASE..cap) (~511) writes Texture2D nulls over descriptors 20 and 21. Those slots are never handed out (next starts at 22) and are outside every range that release, drain or resource_gone refill, so writing them once at creation is enough. By default slots 20/21 now hold raw buffer null UAVs instead of Texture2D nulls; only XCL_HEAP_WINDOWS=1 (off by default) uses them, and a garbage T# index of 20 would find a UAV in an SRV slot, the same kind of risk slots 4-19 already carry with heap windows on. Findings: (1) the test catches the bug only through GPU-based validation: a null descriptor reads 0 whatever its type, so the readback (~3022) passes on main too, and without the debug layer it skips. (2) The PR's smoke numbers (4154 vs 1695 frames) aren't caused by this change: smoke doesn't turn heap windows on, and main's run had stopped presenting. (3) AI use names no tool or model. Fine: the test runs in its own child process and calls skip_test; open #3486 says it depends on this PR.
performance: 60 s smoke of a retail title, main d0832e6 vs this merge (ac66e869): 1516 -> 1652 frames shown (108 %; last presents per 2 s: 60 60 60), peak commit 18133 -> 17101 MB, 0 device removals, 0 crashes. Smoke doesn't set XCL_HEAP_WINDOWS=1, so descriptors 20/21 aren't used here.
verdict: safe to merge (the test only catches the bug through GPU-based validation; 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 5dc64ea7: 2 finding(s). 2 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.

@@ -511,6 +510,9 @@ pub unsafe fn shared_heap(dev: &ID3D12Device) -> bool {
fill_nulls(dev, cpu, inc, crate::gpu::recomp::MEM_HEAP_BASE, cap, at);

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 fix for slots 20/21 looks right, but fill_nulls still starts at MEM_HEAP_BASE, so it writes Texture2D null SRV/UAVs over the window slots 4..19, which are reserved for raw buffer UAVs too. set_window_views only rewrites windows.len().max(MAX_WINDOWS) slots (4 for a normal layout), so slots 8..19 keep Texture2D nulls. A heap-windows shader that reaches window k >= count (a cached PSO translated for a wider layout, or the #3096 expansion) hits the same type mismatch this PR fixes for 20/21 (GBV 939/941, undefined on hardware). Starting fill_nulls at MEM_NULL_STORE + 1 (where Global.next starts), and having null_window_views write raw null UAVs for all of MEM_HEAP_BASE..=MEM_NULL_STORE once, would fix the whole region.

});
// Written after `fill_nulls` so the two out-of-layout raw buffer null UAVs at
// `MEM_NULL_LOAD` and `MEM_NULL_STORE` are not overwritten with Texture2D null SRV/UAVs.
null_window_views(dev, cpu, inc);

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.

Not certain about this one: with slots 20/21 holding raw buffer UAVs again, a stale or garbage T# index that lands on them reaches a raw UAV through a Texture2D access. The image guard only checks tw0 < VIEW_HEAP_MAX_SRV, with no lower bound past the window region. A T# with word 0 = 20 indexes ResourceDescriptorHeap[20] as Texture2D and [21] as RWTexture2D and finds a 4-byte raw buffer UAV. The MEM_HEAP_BASE doc already records that a raw buffer UAV read as a texture hangs NVIDIA GPUs, and the shared_heap comment's promise that garbage T# indexes read zeros no longer holds for 4..21.

@SavageCore SavageCore left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sorry, ignore, agent went crazy

KDenKC added a commit to KDenKC/xcl that referenced this pull request Dec 2, 2026
XCL_HEAP_FLAT=1 reads a compute program's heap memory windows without a
branch (2f52333): the access runs for every lane, and a lane outside the
windows or under a false guard (a buffer index past num_records, an EXEC-
masked lane) had its value set to zero afterwards. But its access went to
window 0's descriptor, at the address it was given, so it read window 0's
memory at an address the program never reaches: the guarded form branches
around it, and the HLSL accessors (409cf07) send it to the null view
MEM_NULL_LOAD. The direct form now selects MEM_NULL_LOAD for it as well,
so it touches no window's memory. The scalar flat read (s_buffer_load in the
flat form, `scalar_flat_read`) picked window 0 the same way for an
address outside the windows; it takes the null view too (its dwords
were and are masked as well).

On the RTX 5090, the retail title with XCL_HEAP_FLAT=1 hung on its first
world frame (DRED: a page fault in a compute dispatch); with it off it ran
past 200 s.

Test: raw_loads_in_root_windows_are_branchless checks that the flat form
selects the null-load view, and, for a program of one s_buffer_load alone,
that the scalar flat read does (it fails without the scalar change).

MEM_NULL_LOAD is only the null view once xcl-dev#3349 writes it after
fill_nulls in shared_heap; before that, descriptor 20 holds a Texture2D
null SRV.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Pipodi

Pipodi commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

merge-check
base: 1c8b551
head: 5dc64ea
merge: clean (merge commit bfb2d5db2d578a7ab0860cd4ab4125587f2938b9)
build: cargo build --workspace -> ok (and cargo build --release -p xcl -> ok)
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2910 passed, 0 failed, 51 ignored; failing: none. 25 SKIPPED lines (22 no wave64, 2 no heap windows, 1 cannot disable the driver cache). shared_heap_preserves_null_window_raw_uavs runs here (the debug layer is installed; the test turns on GPU-based validation on WARP itself, no env needed) and passes. With only the 3-line move reverted (test kept) it fails at console_heap.rs:3037 with two GBV errors: heap index 20 is an SRV where the shader has a UAV, and index 21 is a TEXTURE2D UAV where the shader expects a BUFFER UAV.
review:

  • Correct: on the base null_window_views (console_heap.rs:503) runs before fill_nulls(MEM_HEAP_BASE..cap) (:511), which copies the null block over 20 (even: Texture2D null SRV) and 21 (odd: Texture2D null UAV); writing them after fill_nulls (:515 in the merge) fixes both. shared_heap is the only caller of null_window_views; the other fill_nulls callers (resource_gone :418, range release/drain :708/:768, GONE copies :1308) only touch handed-out slots, which start at MEM_NULL_STORE + 1 (:536), so 20/21 are never overwritten later. The test runner's heaps (runner.rs:1599/1891) write the same views without fill_nulls. Without the CPU-only null heap fill_nulls does not run and the order does not matter.
  • Format: raw_window_uav(4) is an R32_TYPELESS buffer UAV with the RAW flag, which is what the heap-window accessors bind at those indexes (RWByteAddressBuffer from ResourceDescriptorHeap, emit.rs:6228); with no resource, loads read 0 and stores are dropped.
  • Minor (test): when the child process skips (no debug layer, no WARP, no SM 6.6), the parent only checks the exit status and drops the child's output, so no SKIPPED line reaches the suite and the test shows ok without coverage.
  • Description: AI use still names no tool or model (the point from the first check is not fixed).
    performance: no cost by default: one more pair of view writes at heap creation, and slots 20/21 are only bound with heap windows on (XCL_HEAP_WINDOWS=1, off by default). Smoke pair on the retail title, 60 s, same scripted presses on both sides, one untimed warm-up run before each: main 1611 frames, peak commit 19846 MB; merge 1594 frames (98.9 %), 19825 MB (99.9 %); 0 device removals and 0 crashes on both. The smoke runs do not set XCL_HEAP_WINDOWS=1, so they do not reach descriptors 20/21.

main (base):

xcl-smoke v1
commit: 1c8b551475e1e7bfe48f94448492a4c3544a2e0f
gpu: NVIDIA GeForce RTX 4090
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1611 (PresentX 1611)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19846 MB

this PR merged into base:

xcl-smoke v1
commit: bfb2d5db2d578a7ab0860cd4ab4125587f2938b9
gpu: NVIDIA GeForce RTX 4090
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1594 (PresentX 1594)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 19825 MB

verdict: safe to merge (the AI use section should still name the tool and model)
hardware / tool and model: NVIDIA GeForce RTX 4090 24 GB, driver 621.40; Intel Core i9-13900K, 32 GB; Windows 11 build 26300; rustc 1.100.0; debug suite, release smoke. Claude Code, Claude Opus 5.5 (ran the merge, build, suite, revert test and smoke pair, and wrote this check).

KDenKC added a commit to KDenKC/xcl that referenced this pull request Dec 2, 2026
XCL_HEAP_FLAT=1 reads a compute program's heap memory windows without a
branch (2f52333): the access runs for every lane, and a lane outside the
windows or under a false guard (a buffer index past num_records, an EXEC-
masked lane) had its value set to zero afterwards. But its access went to
window 0's descriptor, at the address it was given, so it read window 0's
memory at an address the program never reaches: the guarded form branches
around it, and the HLSL accessors (409cf07) send it to the null view
MEM_NULL_LOAD. The direct form now selects MEM_NULL_LOAD for it as well,
so it touches no window's memory. The scalar flat read (s_buffer_load in the
flat form, `scalar_flat_read`) picked window 0 the same way for an
address outside the windows; it takes the null view too (its dwords
were and are masked as well), and so does the carry case's slot for a
high dword of one (`high_one_slot`).

On the RTX 5090, the retail title with XCL_HEAP_FLAT=1 hung on its first
world frame (DRED: a page fault in a compute dispatch); with it off it ran
past 200 s.

Test: raw_loads_in_root_windows_are_branchless checks that the flat form
selects the null-load view, and, for a program of one s_buffer_load alone,
that the scalar flat read does (it fails without the scalar change).
the_flat_read_carry_rewrite_keeps_the_null_select (module/opt.rs) checks
that the optimizer's FlatRead carry rewrite turns the read's bounds test
and slot into carry selects and leaves its null select in place.

Stacked on xcl-dev#3349: MEM_NULL_LOAD is only the null view once xcl-dev#3349 writes
it after fill_nulls in shared_heap; before that, descriptor 20 holds a
Texture2D null SRV.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed 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 ready-to-merge Has 2 independent safe merge-checks on current head (#3041) labels Dec 2, 2026
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 area:memory Virtual/physical memory, arenas, VRAM budget, UMA 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.

5 participants