diff --git a/d3d12/crates/leonida-gpu/src/descriptor/host.rs b/d3d12/crates/leonida-gpu/src/descriptor/host.rs index 79efb896f9..4d5c190e62 100644 --- a/d3d12/crates/leonida-gpu/src/descriptor/host.rs +++ b/d3d12/crates/leonida-gpu/src/descriptor/host.rs @@ -2313,7 +2313,21 @@ impl HostHeap { }; let raw = if sampler { 0 } else { leonida_shader::emit::raw_views() }; let extra = if sampler { 0 } else { dynamic_slots(count, null) }; - let (heap, count, size) = if null == 0 { + // A sampler heap has all 2,048 slots a host allows, whatever the title's count: shaders clamp + // a sampler index to 2,047 (`leonida-shader`, `emit/shader.rs`), not to the bound heap's own + // count, so a smaller heap would let them read past its end. The slots past the title's + // count are null point samplers (`fill_null` below makes every slot of the heap one). + const SAMPLERS: u32 = 2048; + let (heap, count, size) = if sampler { + let count = count.min(SAMPLERS); + match make(SAMPLERS) { + Ok(heap) => (heap, count, SAMPLERS), + Err(e) => { + tracing::warn!(error = %e, count, sampler, "no host descriptor heap"); + return None; + } + } + } else if null == 0 { match make(count + extra) { Ok(heap) => (heap, count, count + extra), Err(e) => { @@ -6628,6 +6642,22 @@ mod listed_raw_tests; mod tests { use super::*; + /// A host sampler heap has all 2,048 slots, whatever the title's count, so a sampler index the + /// shader clamps to 2,047 (`leonida-shader`, `emit/shader.rs`) stays inside it; the slots past + /// the title's count are the null point sampler the heap is filled with. A title heap of 2,048 + /// keeps all of its slots. + #[test] + fn sampler_heaps_have_every_slot_the_shaders_can_reach() { + let Some(small) = HostHeap::new(true, 16) else { return }; + // SAFETY: live heap query. + assert_eq!(unsafe { small.heap.GetDesc().NumDescriptors }, 2048, "a 16-sampler heap backs 2,048 slots"); + assert_eq!(small.count, 16); + let Some(full) = HostHeap::new(true, 2048) else { return }; + // SAFETY: live heap query. + assert_eq!(unsafe { full.heap.GetDesc().NumDescriptors }, 2048); + assert_eq!(full.count, 2048); + } + /// A view of a host copy that begins at a later level shows the same /// levels of it. #[test]