From e7b9959c55996c611f69a22b8aa7e18706644771 Mon Sep 17 00:00:00 2001 From: Kal0s-design <356578040+Kal0s-design@users.noreply.github.com> Date: Mon, 30 Nov 2026 11:44:27 +0100 Subject: [PATCH] d3d12: give host sampler heaps every slot the shaders can reach Shaders clamp a sampler index to 2,047 (leonida-shader emit/shader.rs: "not the bound heap's own count, which a title heap smaller than 2,048 would need"), but HostHeap::new made a sampler heap with exactly the title's count. With a title sampler heap of fewer than 2,048 samplers, an index between its count and 2,047 (a number read from memory the host has not got yet) read past the end of the host heap. A host sampler heap now always has 2,048 slots; fill_null already makes every slot a null point sampler, so the slots past the title's count read as that. The title's own count is kept (up to 2,048): unlike the sibling tree's version, slot 2,047 is not taken from a title heap that uses it, since any clamped index now lands inside the heap anyway. Test: sampler_heaps_have_every_slot_the_shaders_can_reach (a 16-sampler title heap gets 2,048 host slots and keeps count 16; a 2,048 one keeps 2,048). On main it fails: the 16-sampler heap has 16 slots. After ESAT's fix in the sibling VIctory tree (TaynanGT/Vice-Tay dd67ae7, GPL-3.0). Co-Authored-By: Claude Opus 5.5 --- .../crates/leonida-gpu/src/descriptor/host.rs | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) 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]