Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions crates/xcl/src/gpu/dxil/lower_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand All @@ -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");
}
}
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> = (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| {
Expand Down
43 changes: 35 additions & 8 deletions crates/xcl/src/hle/d3d12x/console_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ static GLOBAL: Mutex<Option<Global>> = 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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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<ID3D12Device> = 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]
Expand Down
Loading