diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index f23d4ab1c73..1e11f787478 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -267,7 +267,8 @@ pub(crate) fn near_heaps(va: u64) -> String { /// The host's limit for shader-visible view heaps on resource binding /// tiers 1 and 2; tier 3 hardware may take more. -const PORTABLE_VIEWS: u32 = 1_000_000; +#[cfg(test)] +const PORTABLE_VIEWS: u32 = victory_shader::emit::PORTABLE_HEAP_VIEWS; /// The maximum shader-visible sampler count on D3D12 (all tiers). const PORTABLE_SAMPLERS: u32 = 2048; @@ -307,6 +308,7 @@ impl HostHeap { let guard = if sampler { 0 } else { 1 }; // The larger dynamic range only where the host takes it on top of // every title slot; else the smaller one, and the cap after that. + let views = victory_shader::emit::heap_views(); let (heap, count, extra) = if sampler { let requested = count; let target_count = if count > PORTABLE_SAMPLERS { @@ -322,22 +324,22 @@ impl HostHeap { return None; } } - } else if count + DYNAMIC + aliases + guard <= PORTABLE_VIEWS + } else if count + DYNAMIC + aliases + guard <= views && let Ok(heap) = make(count + DYNAMIC + aliases + guard) { (heap, count, DYNAMIC) - } else if count + MIN_DYNAMIC + aliases + guard <= PORTABLE_VIEWS + } else if count + MIN_DYNAMIC + aliases + guard <= views && let Ok(heap) = make(count + MIN_DYNAMIC + aliases + guard) { (heap, count, MIN_DYNAMIC) } else { // shortcut: descriptors past the host's limit are left // out; a title that reaches them needs remapping. - tracing::warn!(count, "host view heap capped at {PORTABLE_VIEWS}"); - let (heap, total) = [PORTABLE_VIEWS, 500_000, 250_000, MIN_DYNAMIC] + tracing::warn!(count, "host view heap capped at {views}"); + let (heap, total) = [views, 500_000, 250_000, MIN_DYNAMIC] .into_iter() .find_map(|cap| make(cap).ok().map(|h| (h, cap)))?; - let extra = if total >= PORTABLE_VIEWS { + let extra = if total >= views { (1 << 17).min(total / 4) } else { MIN_DYNAMIC.min(total / 2) diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 09e08f34de0..7ebb5b068b7 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -277,6 +277,7 @@ fn create() -> windows::core::Result { })?; let dsv_size = device.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV); tracing::info!("host D3D12 device ready"); + view_limit(&device); let wave64_mode = recommended_wave64_mode(&device); crate::shader_cache::set_wave64_mode(wave64_mode); tracing::info!(?wave64_mode, "Wave64 compute lowering mode selected"); @@ -298,6 +299,38 @@ fn create() -> windows::core::Result { } } +/// Clamps translated shaders' view-heap indices below what `device` takes +/// (`victory_shader::emit::heap_views`), before any translation: a host +/// that refuses a shader-visible heap of `HEAP_VIEWS` (NVIDIA caps them at +/// 10^6 slots) gets the portable limit, which every host takes. A clamp +/// past the heap faulted the device there (#3330). +fn view_limit(device: &ID3D12Device) { + use victory_shader::emit::{HEAP_VIEWS, PORTABLE_HEAP_VIEWS, set_heap_views}; + // SAFETY: plain heap creation with a valid description; dropped at once. + let full = unsafe { + device.CreateDescriptorHeap::(&D3D12_DESCRIPTOR_HEAP_DESC { + Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + NumDescriptors: HEAP_VIEWS, + Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, + NodeMask: 0, + }) + }; + let views = match full { + Ok(_) => HEAP_VIEWS, + Err(e) => { + tracing::info!(error = %e, "host view heaps capped at {PORTABLE_HEAP_VIEWS}: shaders index below it"); + PORTABLE_HEAP_VIEWS + } + }; + if !set_heap_views(views) && victory_shader::emit::heap_views() != views { + tracing::warn!( + views, + set = victory_shader::emit::heap_views(), + "view-heap limit read before the host device: shaders may index past its heaps" + ); + } +} + /// Queries D3D12 wave lane capabilities and chooses the Wave64 lowering mode. fn recommended_wave64_mode(device: &ID3D12Device) -> victory_shader::emit::Wave64Mode { use victory_shader::emit::Wave64Mode; diff --git a/crates/victory-gpu/src/shader_cache.rs b/crates/victory-gpu/src/shader_cache.rs index a26d23f2ff4..5aa6eaeafe8 100644 --- a/crates/victory-gpu/src/shader_cache.rs +++ b/crates/victory-gpu/src/shader_cache.rs @@ -76,8 +76,10 @@ pub(crate) fn counts() -> (u64, u64) { fn build() -> &'static [u8; 32] { static BUILD: OnceLock<[u8; 32]> = OnceLock::new(); BUILD.get_or_init(|| { + let _ = crate::host::host(); let exe = std::env::current_exe().and_then(std::fs::read); let limit = victory_shader::emit::loop_limit(); + let views = victory_shader::emit::heap_views(); if limit != victory_shader::emit::DEFAULT_LOOP_LIMIT { tracing::warn!( limit, @@ -87,6 +89,7 @@ fn build() -> &'static [u8; 32] { match exe { Ok(mut bytes) => { bytes.extend_from_slice(&limit.to_le_bytes()); + bytes.extend_from_slice(&views.to_le_bytes()); *blake3::hash(&bytes).as_bytes() } Err(e) => { @@ -107,6 +110,7 @@ fn build() -> &'static [u8; 32] { /// with `stage` the program of that hardware stage (the ES or GS half of a /// geometry pipeline). pub(crate) fn host_shader(container: &[u8], stage: Option) -> Result { + let _ = crate::host::host(); let wave64 = effective_wave64_mode(); let options = TranslateOptions { wave64 }; let translate = |c: &[u8]| { diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index 7b48cbc50c7..a0da400569a 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -133,8 +133,9 @@ use crate::{ShaderError, io, ir, resource, structure}; pub use sampler::StaticSampler; pub use shader::{ - ROOT_ADDRESS_SPACE, ROOT_CBV_SPACE, RawWindow, SAMPLER_SPACE, STRIDE_SPACE, StrideEntry, - USER_DATA_SPACE, + HEAP_SAMPLERS, HEAP_VIEWS, PORTABLE_HEAP_VIEWS, ROOT_ADDRESS_SPACE, ROOT_CBV_SPACE, RawWindow, + SAMPLER_SPACE, STRIDE_SPACE, StrideEntry, USER_DATA_SPACE, heap_views, null_buffer_view, + set_heap_views, }; /// The loop limit without an override: 2^20. @@ -772,6 +773,25 @@ mod tests { assert_eq!(clamped_dxil, in_range_dxil); } + #[test] + fn heap_handle_clamps_out_of_range_indices() { + let mut s = Shader::new(false); + let b = s.entry; + let index = s.m.i32(2_000_000); + let _ = s.heap_handle(b, index, false, (0, 0)); + let sampler_index = s.m.i32(5000); + let _ = s.heap_handle(b, sampler_index, true, (0, 0)); + s.m.terminate(b, super::bitcode::Term::Ret); + let dxil = s.finish(); + assert!(!dxil.is_empty()); + } + + #[test] + fn heap_views_defaults_or_clamps() { + assert!(heap_views() >= 4); + assert_eq!(null_buffer_view(), heap_views() - 2); + } + #[test] #[cfg(windows)] fn stub_pipeline_is_created() { diff --git a/crates/victory-shader/src/emit/shader.rs b/crates/victory-shader/src/emit/shader.rs index 117b45b9a0b..3e80a780489 100644 --- a/crates/victory-shader/src/emit/shader.rs +++ b/crates/victory-shader/src/emit/shader.rs @@ -19,6 +19,38 @@ pub const ROOT_ADDRESS_SPACE: u32 = 2; /// (one [`StrideEntry`] per view-heap slot). pub const STRIDE_SPACE: u32 = 3; +/// The most view-heap slots a translated shader can index: its indices are +/// clamped below [`heap_views`], at most this. +pub const HEAP_VIEWS: u32 = 1 << 21; +/// Sampler-heap indices are clamped below this: the host's sampler heaps +/// are at least this large. +pub const HEAP_SAMPLERS: u32 = 2048; +/// The shader-visible view heap size every host takes (resource binding +/// tiers 1 and 2; NVIDIA's tier 3 hardware takes no more), the view limit +/// on a host that refuses a heap of [`HEAP_VIEWS`]. +pub const PORTABLE_HEAP_VIEWS: u32 = 1_000_000; + +static VIEW_LIMIT: std::sync::OnceLock = std::sync::OnceLock::new(); + +/// View-heap indices are clamped below this: the host's view heaps are at +/// least this large. [`HEAP_VIEWS`] unless [`set_heap_views`] chose less (a host +/// whose heaps are capped, NVIDIA). The value is part of the translation, so the +/// shader cache must key on it. +pub fn heap_views() -> u32 { + *VIEW_LIMIT.get_or_init(|| HEAP_VIEWS) +} + +/// Sets [`heap_views`] (between 4 and [`HEAP_VIEWS`]), before the first +/// translation; false once it was read or set (the value then stays). +pub fn set_heap_views(views: u32) -> bool { + VIEW_LIMIT.set(views.clamp(4, HEAP_VIEWS)).is_ok() +} + +/// The null buffer view index, two below [`heap_views`]. +pub fn null_buffer_view() -> u32 { + heap_views() - 2 +} + /// What the host writes per view-heap slot in the runtime stride table, for /// buffers a shader reads without a stride known when it was translated. /// @@ -436,6 +468,9 @@ impl Shader { }; #[cfg(test)] self.heap_props.push(props); + let last = self.m.i32(if sampler { HEAP_SAMPLERS } else { heap_views() } - 1); + let over = self.m.cmp(block, super::bitcode::pred::IUGT, index, last); + let index = self.m.select(block, over, last, index); let (s, n) = (self.m.bool(sampler), self.m.bool(false)); let h = self.op( block,