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
14 changes: 8 additions & 6 deletions crates/victory-gpu/src/descriptor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ fn create() -> windows::core::Result<Host> {
})?;
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");
Expand All @@ -298,6 +299,38 @@ fn create() -> windows::core::Result<Host> {
}
}

/// 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::<ID3D12DescriptorHeap>(&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;
Expand Down
4 changes: 4 additions & 0 deletions crates/victory-gpu/src/shader_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) => {
Expand All @@ -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<HwStage>) -> Result<HostShader, String> {
let _ = crate::host::host();
let wave64 = effective_wave64_mode();
let options = TranslateOptions { wave64 };
let translate = |c: &[u8]| {
Expand Down
24 changes: 22 additions & 2 deletions crates/victory-shader/src/emit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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() {
Expand Down
35 changes: 35 additions & 0 deletions crates/victory-shader/src/emit/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = 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.
///
Expand Down Expand Up @@ -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,
Expand Down
Loading