From 19bef421e6f974fec2aa8289b228c78ab92f3013 Mon Sep 17 00:00:00 2001 From: Grein Date: Wed, 25 Nov 2026 04:03:48 -0300 Subject: [PATCH] D3D12.X: the T# index guard matches a 1,000,000-descriptor shared heap The translated shaders accept a T# SRV index below 2^21 - 1 (the UAV is at tw0 + 1), while the shared shader-visible heap is min(device limit, 2^21) descriptors: 1,000,000 on an RTX 4070. An index in between reads past the heap's end, which hangs NVIDIA GPUs. XCL writes only indices it reserved, but a T# the title wrote itself (raw console words: word 0 is the base address >> 8, so a texture at 256-512 MB gives 1-2 million) passes the type and BASE_ADDRESS_HI checks. The shared heap is now 1,000,000 descriptors on every GPU (resource binding tier 2+ takes that many) and the guard is tied to it: VIEW_HEAP_MAX_SRV = GLOBAL_MAX - 1 in the direct DXIL translator, the same constant in the HLSL path. global_cap() holds the heap size computation so a test can check it against the bound; on the RTX 4070 it fails with the old values ("shaders accept indices up to 2097151, the shared heap holds 1000000"). Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/dxil/lower_image.rs | 10 +++--- crates/xcl/src/gpu/dxil/mod.rs | 2 +- crates/xcl/src/gpu/recomp/emit.rs | 2 +- crates/xcl/src/hle/d3d12x/console_heap.rs | 43 ++++++++++++++++++----- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/crates/xcl/src/gpu/dxil/lower_image.rs b/crates/xcl/src/gpu/dxil/lower_image.rs index 07c5aba4a9..7055c46596 100644 --- a/crates/xcl/src/gpu/dxil/lower_image.rs +++ b/crates/xcl/src/gpu/dxil/lower_image.rs @@ -15,7 +15,7 @@ use crate::gpu::rdna2::{self, Decoded}; /// (`console_heap::GLOBAL_MAX = 1 << 21`, leaving `index + 1` in-bounds for the paired UAV). /// Guarding / clamping dynamic heap indices prevents AMD Tier-3 GPUs from page-faulting /// on null, uninitialized, or unpatched console T# descriptors. -const VIEW_HEAP_MAX_SRV: u32 = (1 << 21) - 1; +pub(crate) const VIEW_HEAP_MAX_SRV: u32 = crate::hle::d3d12x::console_heap::GLOBAL_MAX - 1; /// D3D12 shader-visible sampler heap limit (`2048` samplers: `0..=2047`). const SAMPLER_HEAP_MASK: u32 = 0x7FF; @@ -1143,12 +1143,12 @@ mod heap_bounds_tests { let dxil = kernel::translate(&code, kernel::KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 }).expect("direct translate"); validate::validate(&dxil).expect("dxil validate"); let dis = validate::disassemble(&dxil).expect("dxil disassemble"); - assert!(dis.contains("2097151"), "expected VIEW_HEAP_MAX_SRV guard in direct DXIL:\n{dis}"); + assert!(dis.contains(&super::VIEW_HEAP_MAX_SRV.to_string()), "expected VIEW_HEAP_MAX_SRV guard in direct DXIL:\n{dis}"); assert!(dis.contains("2047"), "expected 0x7FF sampler heap mask in direct DXIL:\n{dis}"); let hlsl = recomp::translate(&code, &recomp::Options::default()).expect("hlsl translate"); assert!(hlsl.complete(), "hlsl unsupported: {:?}", hlsl.unsupported); - assert!(hlsl.hlsl.contains("2097151u"), "expected VIEW_HEAP_MAX_SRV check in HLSL"); + assert!(hlsl.hlsl.contains(&format!("{}u", super::VIEW_HEAP_MAX_SRV)), "expected VIEW_HEAP_MAX_SRV check in HLSL"); assert!(hlsl.hlsl.contains("& 0x7FFu"), "expected 0x7FFu sampler heap mask in HLSL"); assert!(!hlsl.hlsl.contains("& 0xFFFu"), "unexpected unclamped 0xFFFu sampler mask in HLSL"); // Ensure atomic UAV handle creation is inside the guarded `if (and(..., ..._ok))` block. @@ -1167,11 +1167,11 @@ mod heap_bounds_tests { let k = kernel::translate_pixel(&code, kernel::KernelOptions { wave64: true, split: false, mem_windows: 1, sampler_base: 0 }, &ps).expect("direct ps translate"); validate::validate(&k.dxil).expect("dxil ps validate"); let dis = validate::disassemble(&k.dxil).expect("dxil ps disassemble"); - assert!(dis.contains("2097151"), "expected VIEW_HEAP_MAX_SRV clamp in PS DXIL:\n{dis}"); + assert!(dis.contains(&super::VIEW_HEAP_MAX_SRV.to_string()), "expected VIEW_HEAP_MAX_SRV clamp in PS DXIL:\n{dis}"); assert!(dis.contains("2047"), "expected 0x7FF sampler mask in PS DXIL:\n{dis}"); let hlsl = recomp::translate(&code, &recomp::Options { mode: recomp::Mode::Pixel(ps), ..Default::default() }).expect("hlsl ps translate"); assert!(hlsl.complete(), "hlsl ps unsupported: {:?}", hlsl.unsupported); - assert!(hlsl.hlsl.contains("select(") && hlsl.hlsl.contains("2097151u"), "expected convergent clamp in PS HLSL"); + assert!(hlsl.hlsl.contains("select(") && hlsl.hlsl.contains(&format!("{}u", super::VIEW_HEAP_MAX_SRV)), "expected convergent clamp in PS HLSL"); } } diff --git a/crates/xcl/src/gpu/dxil/mod.rs b/crates/xcl/src/gpu/dxil/mod.rs index f8ce756509..d944b2c56e 100644 --- a/crates/xcl/src/gpu/dxil/mod.rs +++ b/crates/xcl/src/gpu/dxil/mod.rs @@ -22,7 +22,7 @@ mod lanes; pub mod lower; mod lower_geom; mod lower_gfx; -mod lower_image; +pub(crate) mod lower_image; mod lower_mem; mod lower_round; mod lower_valu; diff --git a/crates/xcl/src/gpu/recomp/emit.rs b/crates/xcl/src/gpu/recomp/emit.rs index 8f257a043f..bf959ac058 100644 --- a/crates/xcl/src/gpu/recomp/emit.rs +++ b/crates/xcl/src/gpu/recomp/emit.rs @@ -2600,7 +2600,7 @@ impl Em { // Guard against null / unpatched / OOB T# descriptors so AMD Tier-3 GPUs never page-fault // indexing `ResourceDescriptorHeap` (valid XCL T#: type >= 8, BASE_ADDRESS_HI cleared, // SRV index < (1 << 21) - 1 so `tw0 + 1` stays in bounds without wrapping). - self.line(&format!("bool {t}_ok = (({tw3} >> 28) >= 8u) && ((({tw1}) & 0xFFu) == 0u) && (({tw0}) < 2097151u);")); + self.line(&format!("bool {t}_ok = (({tw3} >> 28) >= 8u) && ((({tw1}) & 0xFFu) == 0u) && (({tw0}) < {}u);", crate::gpu::dxil::lower_image::VIEW_HEAP_MAX_SRV)); let comps: Vec = (0..4).filter(|c| dmask >> c & 1 == 1).collect(); // write the selected result components (`r` is a uint4 of raw bits); D16 packs halves let write_result = |s: &mut Self, r: &str, n: usize, sel: &dyn Fn(usize) -> u64, half: &dyn Fn(&str) -> String| { diff --git a/crates/xcl/src/hle/d3d12x/console_heap.rs b/crates/xcl/src/hle/d3d12x/console_heap.rs index 14d4f3695a..7f8c18664e 100644 --- a/crates/xcl/src/hle/d3d12x/console_heap.rs +++ b/crates/xcl/src/hle/d3d12x/console_heap.rs @@ -94,7 +94,11 @@ static GLOBAL: Mutex> = Mutex::new(None); static GLOBAL_FAILED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); /// descriptors of the shared heap at most (the GPU may take more: OPTIONS19) -const GLOBAL_MAX: u32 = 1 << 21; +pub const GLOBAL_MAX: u32 = 1_000_000; +// (one million: what every resource binding tier 2+ GPU takes. The translated shaders accept a +// T# index only below it (`gpu::dxil::lower_image::VIEW_HEAP_MAX_SRV`), so the heap must never be +// smaller than that bound: an index past the heap's end hangs NVIDIA GPUs, which took 1 000 000 +// while the bound was 2^21 – a raw console T# of a texture at 256–512 MB passed it.) /// view slots per page of a heap's place in the shared heap (2 PC descriptors each) pub const PAGE_SLOTS: u32 = 256; @@ -133,6 +137,18 @@ fn take_range(free: &mut Vec<(u32, u32)>, next: &mut u32, cap: u32, count: u32) Some(*next - count) } +/// Descriptors of the shared heap on `dev`: what it takes (OPTIONS19; 1 000 000 without it), at +/// most `GLOBAL_MAX`. +unsafe fn global_cap(dev: &ID3D12Device) -> u32 { + let mut o = D3D12_FEATURE_DATA_D3D12_OPTIONS19::default(); + let max = if dev.CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS19, &mut o as *mut _ as *mut std::ffi::c_void, std::mem::size_of_val(&o) as u32).is_ok() && o.MaxViewDescriptorHeapSize > 0 { + o.MaxViewDescriptorHeapSize + } else { + 1_000_000 + }; + max.min(GLOBAL_MAX) +} + /// The shared shader-visible heap, made on first use. False: there is none (it could not be /// made) – view heaps then keep their own. pub unsafe fn shared_heap(dev: &ID3D12Device) -> bool { @@ -142,13 +158,7 @@ pub unsafe fn shared_heap(dev: &ID3D12Device) -> bool { if GLOBAL_FAILED.load(Relaxed) { return false; } - let mut o = D3D12_FEATURE_DATA_D3D12_OPTIONS19::default(); - let max = if dev.CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS19, &mut o as *mut _ as *mut std::ffi::c_void, std::mem::size_of_val(&o) as u32).is_ok() && o.MaxViewDescriptorHeapSize > 0 { - o.MaxViewDescriptorHeapSize - } else { - 1_000_000 - }; - let cap = max.min(GLOBAL_MAX); + let cap = global_cap(dev); let d = D3D12_DESCRIPTOR_HEAP_DESC { Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, NumDescriptors: cap, Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, NodeMask: 0 }; let heap: ID3D12DescriptorHeap = match dev.CreateDescriptorHeap(&d) { Ok(h) => h, @@ -580,6 +590,23 @@ pub fn words_at(addr32: u32) -> Option<[u32; 8]> { mod tests { use super::*; + /// Every T# index the translated shaders accept, and its UAV at `tw0 + 1`, is inside the + /// shared heap this GPU gets: an index past the heap's end hangs NVIDIA GPUs (whose heap is + /// 1 000 000, while the shaders accepted indices up to 2^21 - 2). + #[test] + fn the_shaders_t_sharp_index_bound_fits_the_shared_heap() { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + let mut dev: Option = None; + if unsafe { D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev) }.is_err() { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + } + let cap = unsafe { global_cap(&dev.unwrap()) }; + let bound = crate::gpu::dxil::lower_image::VIEW_HEAP_MAX_SRV; + // accepted: tw0 < bound, so the highest UAV index is bound - 1 + 1 = bound + assert!(bound < cap, "shaders accept indices up to {bound}, the shared heap holds {cap}"); + } + /// Ranges of the shared heap: released ones first (first fit, split), then new ones, and /// none past its size. #[test]