Skip to content

Recompiler: bound descriptor heap indices from T# and S# words to the heap - #1215

Open
duudiis wants to merge 2 commits into
xcl-dev:mainfrom
duudiis:pr/heap-index-bound
Open

duudiis wants to merge 2 commits into
xcl-dev:mainfrom
duudiis:pr/heap-index-bound

Conversation

@duudiis

@duudiis duudiis commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Translated shaders index ResourceDescriptorHeap with T# word 0 and SamplerDescriptorHeap with S# word 3 [11:0], and both come from console memory. If a T# was read from memory the GPU never wrote, the index can be anything. On NVIDIA an index past the heap wraps onto an unrelated slot, and if that slot holds a descriptor of the wrong kind the GPU hangs with no DRED page fault (device removal 0x887a0005). A retail title's cutscene reads T#s from memory that skipped GPU work was meant to write. This PR keeps those indices inside the heap. The shared heap now ends in null-view guard slots, and every data-derived heap index in a translated shader is bounded to them, so a garbage T# reads zeros and its stores are dropped.

Idea from VIctory (GPL-3.0), reimplemented; no code taken.

Overlaps #610 (draft): it also puts null views at the end of the heap and sends T# indices that are not views to them. The approaches differ. #610 fixes the heap at 1 000 000 views, checks word 0 against a compile-time constant and also checks word 3's TYPE. This PR keeps the heap at its OPTIONS19 size and passes the bound at run time in an io slot. It also bounds S# indices and fills shader-visible sampler heaps to 2048. The two conflict in console_heap.rs, emit.rs and runner.rs, so only one of them should land.

What is in it

  • D3D12.X: reserve null-view guard slots at the end of the shared heap:
    • hle/d3d12x/console_heap.rs: the last 14 descriptors of the shared heap are guard slots. For each view kind the emitter declares (1D, 2D, 3D, CubeArray, 1DArray, 2DArray, 2DMS) there is a null SRV with a null UAV next to it, and no console heap is ever given these slots. view_limit returns the first guard slot when the shared heap is bound, and 0 otherwise or with XCL_HEAP_CLAMP=0.
    • gpu/recomp/mod.rs: IO_VIEW_LIMIT = io slot 113. Slot 112 is already IO_UMA_BASE_HI, so the new one goes next to it in the trap-temp range. Also adds VIEW_GUARD_KINDS and SAMPLER_HEAP_SLOTS.
    • bind.rs, console_list.rs and list_x.rs write the io slot for each translated draw and dispatch, from the heaps the list has bound.
    • real.rs: shader-visible PC sampler heaps get 2048 samplers. The slots past the title's are the zero S#'s point sampler.
  • Recompiler: bound heap indices from T# and S# words to the heap (gpu/recomp/emit.rs):
    • Each T# heap index (sample, get_lod, get_resinfo, load, store, atomics) becomes one select against xcl_vlast = io[IO_VIEW_LIMIT] - 1.
    • The UAV + 1 is applied after the bound. Before, word 0 0xFFFFFFFF wrapped the UAV index to 0.
    • S# indices become min(s & 0xFFF, 2047).
    • Shaders that index no heap produce the same HLSL as before.
    • gpu/recomp/runner.rs: the kernel test runner puts the guard slots after the test textures and fills its sampler heap the same way the runtime does.

Tested

  • NVIDIA GeForce RTX 5070 (12 GB), Intel Core i7-14700KF, Windows 11; Rust 1.98.0, release.
  • cargo build --workspace --release: ok.
  • cargo test --workspace --release -- --test-threads=4 on 757ece9: 744 passed, 0 failed, 41 ignored (lib: 701 passed, 0 failed, 21 ignored).
    • SKIPPED (no wave64): gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware.
  • New tests:
    • shared_heap_guard_slots (console_heap.rs): passes.
    • heap_indices_are_bounded (gpu/recomp/tests.rs): passes, and fails with this head's emit.rs reverted.
    • gpu_heap_index_past_the_views_reads_the_guard (GPU): passes. On this NVIDIA GPU it also passes with emit.rs reverted, because the out-of-range reads there returned 0 and the stores were dropped. It shows that the guard path reads zeros and drops stores. It does not reproduce the hang.

Regression run
main (47efa2e):

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 5070
title: NYCMain
seconds: 60
exit: 0x0
frames shown: 386 (PresentX 387)
presents per 2 s (last 3): 11 11 14
device removals: 0
crashes: 0
peak commit: 15980 MB

This PR (757ece9):

xcl-smoke v1
commit: 757ece9795bc1f047661c77c039645d1893bb949
gpu: NVIDIA GeForce RTX 5070
title: NYCMain
seconds: 60
exit: 0x0
frames shown: 1600 (PresentX 1600)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 16739 MB

The main run went first with a cold shader and driver cache (both builds share the cache folder), so most of the frame difference is the cache, not this change. Both runs cover boot and the intro, not the cutscene that reads garbage T#s.

Not tested

  • AMD GPUs.
  • The retail title past its intro with this exact head: the smoke runs stop at 60 s, before the cutscene that reads garbage T#s.
  • Whether the guarded reads (zeros) let the cutscene draw correctly.

AI use

  • Claude Code, Claude Opus 5.5: wrote the code, the tests and this description.

Translated shaders index the shared shader-visible heap with T# word 0
and the sampler heap with S# word 3 [11:0], both read from console
memory. A T# from memory the GPU never wrote (a retail game's cutscene,
after skipped dispatches) gives an index anywhere: past the heap's end
NVIDIA is reported to wrap onto an unrelated slot, and a descriptor of
the wrong kind there hangs the GPU without a DRED page fault (device
removal 0x887a0005).

The shared heap's last 14 descriptors become guard slots, never given
to a console heap: per kind of view a translated shader declares
(Texture1D, 2D, 3D, CubeArray, 1DArray, 2DArray, 2DMS) a null SRV and
next to it a null UAV, written when the heap is made. The heap's size
depends on the GPU (OPTIONS19, at most 2^21), so the first guard slot
goes to the shaders in io[IO_VIEW_LIMIT] (113, in the trap-temp range
next to IO_UMA_BASE_HI) for every translated draw and dispatch that
binds the shared heap; 0 otherwise (a view heap of its own when the
shared heap could not be made) and with XCL_HEAP_CLAMP=0.

Shader-visible PC sampler heaps get all 2048 samplers D3D12 allows,
the ones past the title's slots the zero S#'s point sampler, so a
sampler index bounded to 2047 always names a sampler.

Nothing reads the io slot yet: the recompiler uses it in the next
commit.

Idea from VIctory (GPL-3.0), reimplemented; no code taken.

Assisted-by: Claude Code (Claude Opus 5.5)
Every ResourceDescriptorHeap index a translated shader takes from T#
word 0 (image sample / get_lod / get_resinfo / load / store / atomics)
is now one select: below io[IO_VIEW_LIMIT] it is itself, else the guard
slot of its view kind there (a null SRV, the UAV one next to it), so a
garbage T# reads zeros and its stores go nowhere instead of indexing
past the heap. The UAV index is taken after the bound: word 0
0xFFFFFFFF used to make it 0 (wrapped), the shared heap's first SRV, as
an RWTexture. The bound is loaded once in main (xcl_vlast = io - 1, so
an io slot of 0 leaves the indices as they were). Sampler heap indices
are min(S# word 3 & 0xFFF, 2047): 4095 was past any shader-visible
sampler heap.

Only shaders that index a heap change; the others' HLSL stays byte for
byte the same. emit.rs and mod.rs are translator sources, so the
translation caches' identity changes with them.

The kernel test runner puts the guard slots after the test textures and
fills its sampler heap like the runtime's.

Assisted-by: Claude Code (Claude Opus 5.5)
@Aniseto

Aniseto commented Nov 24, 2026

Copy link
Copy Markdown

Ran the new heap-bound tests on an NVIDIA Blackwell part at 757ece9:

Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, wave32 only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC 14.44.35207, DXC 1.9.2609.5, debug build.

XCL_ADAPTER=nvidia cargo test -p xcl --lib heap_indices
1 passed; 0 failed; 0 ignored      (exit 0)

XCL_ADAPTER=nvidia cargo test -p xcl --lib gpu_heap_index
1 passed; 0 failed; 0 ignored      (exit 0)

XCL_ADAPTER=nvidia cargo test -p xcl --lib console_heap
4 passed; 0 failed; 0 ignored      (exit 0)

gpu_heap_index_past_the_views_reads_the_guard runs on the GPU here and reads the guard slot as expected; heap_indices_are_bounded and the console-heap unit set are unaffected. This is a debug build on a Blackwell part with driver 32.0.16.2124 (the author's run was release on an RTX 5070).

Not tested: retail titles, RDNA2/RDNA4 and Intel GPUs, Wine, native wave64.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash 1017 - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib recomp: 191 passed, 0 failed.

Note: the full d3d12x lib filter currently aborts with 0xc0000005 on unmodified main too (pre-existing, unrelated to this PR); the PR's own tests were run by name and pass.

@jessenic

Copy link
Copy Markdown
Contributor

A measurement that supports this PR's explanation of the 0x887a0005 removal, from a retail title on an RTX 5070 Ti, on a tree without this PR. I'm now testing the PR on the same setup and will add the result here.

Setup: RTX 5070 Ti 16 GB (driver 610.88), Windows 11 Pro 26300, release, Series S profile. A local integration branch on main 8f5fca5 with #1323, #1405, #1464, #1291, #1342, #1156, #2009, #1993 and others. XCL_HEAP_WINDOWS=1 XCL_COHERENT_HEAPS=sparse:256. The title reaches the intro cutscene's title card and then loses the device 7–9 s after its first #101, in every run (about 10 so far). DRED shows no list in progress and no page fault, and Windows logs no TDR event (nvlddmkm) for it.

Where the removal comes from: cdb attached to the game at #101, with breakpoints on D3D12Core!*RemoveDevice* (public Microsoft symbols):

  • The device is removed inside an ExecuteCommandLists, in two runs one of XCL's own sparse uploads (sparse::State::submit_slot). The chain is nvwgf2umx → D3D12Core!CDevice::UMSetError_ → CDevice::ProcessDriverErrors → NDXGI::CDevice::RemoveDevice.
  • The error the NVIDIA user-mode driver passes to UMSetError_ is 0x88760870 (D3DDDIERR_DEVICEREMOVED). So the list submitted then is not the cause. The kernel had already marked the device lost, and the next submission found out.
  • The kernel's device execution state, the 5th argument of CD3D12TelemetryHelper::LogRemoveDeviceEvent, is 6, D3DKMT_DEVICEEXECUTION_ERROR_DMAFAULT. It is not a page fault (7), a hang (3) or out of memory (5).

A DMA fault with no page fault and no TDR fits what this PR describes: the GPU stops on a heap index that lands on a descriptor of the wrong kind. Two more things about main's guard, seen while applying this PR on top of it:

  • main's MIMG guard (5195e27) passes any word 0 below 2 097 151. The shared heap is smaller than that, so an index between its end and 2M still runs past it.
  • Its implicit-LOD sample path sends an invalid T# to heap index 0. With XCL_HEAP_WINDOWS=1, slot 0 is memory window 0's raw buffer UAV, so a T# that fails the check samples a buffer UAV as a texture.

When I combined this PR with main's guard, I sent the invalid-T# fallback to xcl_vlast + 1u (the guard slot when the heap is bounded, 0 when it is not), not to 0. That may be worth doing in the PR if it gets rebased onto main.

AI use: Claude Code (Claude Opus 5.5) ran the debugger sessions on the machine above and wrote this note.

@jessenic

Copy link
Copy Markdown
Contributor

Follow-up to my note above: with this PR, the removal 7–9 s after the title's first #101 is gone in 3 of 3 runs on the RTX 5070 Ti. Without the PR it happened in every run (about 10).

Same tree and setup as the note: the PR's two commits were cherry-picked onto the integration branch, with the conflicts against main's 5195e27 guard resolved as described there. Every compute pipeline was precompiled for the new translation first (12 threads, 3.8 h). Pixel and vertex programs were not precompiled.

run first #101 how it ends
1 (a second precompile was using the CPU and GPU) 355 s no removal until the run ended at 420 s; no presents after #101 + 2 s
2 195 s the same: no removal in 225 s, no presents after #101 + 2 s
3 191 s 0x887a0005 at 284 s, #101 + 94 s

Not tested: AMD; Series X; more than 3 runs; a scripts/smoke.ps1 regression run; this PR alone on main.

AI use: Claude Code (Claude Opus 5.5) ran the runs and the debugger on the machine above and wrote this comment.

@jessenic

Copy link
Copy Markdown
Contributor

Correction to my comment above: "gone in 3 of 3" was wrong. With a warm cache, this PR delays the removal; it does not prevent it.

In those three runs the world drew almost nothing after #101. Draws waited for the graphics pipelines this PR's changed HLSL had made cold, and run 3's removal came as soon as drawing resumed. So those runs could not tell "fixed" from "delayed".

Run 4 had the graphics driver blobs from runs 1–3 already cached. Same tree and switches, cdb attached at #101 with a breakpoint on CD3D12TelemetryHelper::LogRemoveDeviceEvent:

  • the world drew from #101 on (up to about 980 draws every 2 s, 1–2 presents every 2 s);
  • the device was removed at #101 + 18.7 s, with execution state 6 (D3DKMT_DEVICEEXECUTION_ERROR_DMAFAULT) and 0x887a0005, as without the PR.

Without the PR this removal came at #101 + 7–9 s. With it, it came at + 18.7 s in this one warm run, so the PR may move it, but one run is not enough to say. The fault itself is still there on this tree. My guess in the earlier comment, "a later and different fault", is not supported: the kernel's reason is the same.

I'm next checking whether the page budget's evictions take part (a run without XCL_PAGE_BUDGET_MB), and testing a tree with #1803.

AI use: Claude Code (Claude Opus 5.5) ran the run and the debugger and wrote this correction.

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

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants