diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 5b50288..8f7021f 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -1166,7 +1166,7 @@ impl HostHeap { dynamic: extra, sampler, raw, - twins: (0..if raw == 0 { 0 } else { count }).map(|_| std::sync::atomic::AtomicBool::new(false)).collect(), + twins: (0..if raw == 0 { 0 } else { count + extra }).map(|_| std::sync::atomic::AtomicBool::new(false)).collect(), made: Mutex::new(vec![0; count as usize]), quick: (0..count).map(|_| std::sync::atomic::AtomicU64::new(0)).collect(), copied: Mutex::new(Vec::new()), @@ -2036,11 +2036,12 @@ impl HostHeap { /// Makes slot `index` (a dynamic one) from `view`; a null view if the /// host cannot. pub(crate) fn make_at(&self, index: u32, view: &View) { - let kept = make(view, self.at(index)); + let kept = make_twinned(view, self.at(index), self.twin(index)); if kept.is_none() { null_for(view, self.at(index)); } self.put(index); + self.twin_made(index, kept.is_some() && twinned(view)); self.keep(index, kept.flatten()); } @@ -2052,15 +2053,28 @@ impl HostHeap { null_view(self.at(index)); self.put(index); } + self.clear_twin(index); self.keep(index, None); } - /// Where the raw twin of title slot `index` is. + /// Where the raw twin of slot `index` is made. fn twin(&self, index: u32) -> Option { (index < self.twins.len() as u32).then(|| staged(self.sampler, 1).unwrap_or_else(|| self.cpu(self.raw + index))) } - /// The raw twin of title slot `index` is a null view again. + /// The raw twin of slot `index` was made by this thread ([`make_twinned`]), + /// or is a null view if it was not. + fn twin_made(&self, index: u32, made: bool) { + if !made { + return self.clear_twin(index); + } + if let Some(shown) = self.twins.get(index as usize) { + self.put_twin(index); + shown.store(true, std::sync::atomic::Ordering::Relaxed); + } + } + + /// The raw twin of slot `index` is a null view again. fn clear_twin(&self, index: u32) { if let Some(made) = self.twins.get(index as usize) && made.swap(false, std::sync::atomic::Ordering::Relaxed) @@ -2113,6 +2127,8 @@ impl HostHeap { .CreateShaderResourceView(buffer, Some(&desc), self.at(index)) }; self.put(index); + // (Bound by its handle, not read by index.) + self.clear_twin(index); self.keep(index, Some(buffer.clone())); } @@ -2121,6 +2137,7 @@ impl HostHeap { pub(crate) fn null_raw_at(&self, index: u32) { null_buffer_view(self.at(index)); self.put(index); + self.clear_twin(index); self.keep(index, None); } @@ -2154,6 +2171,7 @@ impl HostHeap { ) }; self.put(index); + self.clear_twin(index); self.keep(index, Some(buffer.clone())); } @@ -2182,6 +2200,7 @@ impl HostHeap { ) }; self.put(index); + self.clear_twin(index); self.keep(index, None); } @@ -2217,6 +2236,28 @@ impl HostHeap { .CreateConstantBufferView(Some(&desc), self.at(index)) }; self.put(index); + // A program that reads it as raw bytes reads its twin. + let twin = self.twin(index).filter(|_| desc.SizeInBytes > 0); + if let Some(twin) = twin { + // SAFETY: a live buffer that holds the range. + let first = (gpu - unsafe { buffer.GetGPUVirtualAddress() }) / 4; + let raw = D3D12_SHADER_RESOURCE_VIEW_DESC { + Format: DXGI_FORMAT_R32_TYPELESS, + ViewDimension: D3D12_SRV_DIMENSION_BUFFER, + Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, + Anonymous: D3D12_SHADER_RESOURCE_VIEW_DESC_0 { + Buffer: D3D12_BUFFER_SRV { + FirstElement: first, + NumElements: desc.SizeInBytes / 4, + StructureByteStride: 0, + Flags: D3D12_BUFFER_SRV_FLAG_RAW, + }, + }, + }; + // SAFETY: as above, and a slot of our heaps. + unsafe { host.device.CreateShaderResourceView(buffer, Some(&raw), twin) }; + } + self.twin_made(index, twin.is_some()); // After the descriptor: what it showed is let go when nothing shows // it any more. self.keep(index, holds.then(|| buffer.clone())); @@ -2379,7 +2420,6 @@ impl HostHeap { if made[index as usize] != 0 { self.quick_is(index, 0); made[index as usize] = 0; - self.clear_twin(index); self.make_null(index); let _ = self.note_buffer(index, None, false); } else if for_draw { @@ -2434,14 +2474,7 @@ impl HostHeap { if kept.is_some() || !self.sampler { self.put(index); } - if kept.is_some() && twinned(&view) { - self.put_twin(index); - if let Some(made) = self.twins.get(index as usize) { - made.store(true, std::sync::atomic::Ordering::Relaxed); - } - } else { - self.clear_twin(index); - } + self.twin_made(index, kept.is_some() && twinned(&view)); self.keep(index, kept.flatten()); self.quick_is(index, made[index as usize]); } diff --git a/crates/victory-gpu/src/indirect.rs b/crates/victory-gpu/src/indirect.rs index 60b645c..dd5b2c0 100644 --- a/crates/victory-gpu/src/indirect.rs +++ b/crates/victory-gpu/src/indirect.rs @@ -28,7 +28,7 @@ //! run empty, saturating without 32-bit wraparound so a list of thousands of //! garbage records cannot run for seconds either. //! -//! shortcut: the bounds (2^24 vertices, 2^26 vertex instances, 65,535 +//! shortcut: the bounds (below 2^23 vertices an instance, 2^26 vertex instances, 65,535 //! groups per dimension, in x 2^20 ([`widest`]), and 2^20 per dispatch, one-dimensional mesh //! dispatches, the budget) cut legitimate work beyond them; raise them if //! the title needs more. @@ -62,6 +62,8 @@ RWByteAddressBuffer dst_args : register(u0); // records run empty. RWByteAddressBuffer budget : register(u1); static const uint BUDGET = 1u << 27; +// The vertices or indices an instance from which a draw record runs empty. +static const uint MOST_DRAW = 1u << 23; // Groups of one dispatch; more run empty. static const uint GROUPS = 1u << 20; // Each group charges the budget once: the costs of its records go here, @@ -102,7 +104,14 @@ void main(uint3 id : SV_DispatchThreadID, uint gi : SV_GroupIndex) { if (ahead && (want.x > (1u << 24) || want.y > (1u << 24))) { want = uint2(0, 0); } - uint count = min(want.x, 1u << 24); + // A record of MOST_DRAW vertices or indices an instance, or more, is + // no draw of the title's (a slot it has not filled): it runs empty + // and charges nothing (victory-fork f85101b6, its window checker + // found 90 of 2^24 vertices in one list). + if (want.x >= MOST_DRAW) { + want = uint2(0, 0); + } + uint count = want.x; uint instances = min(want.y, (1u << 26) / max(count, 1u)); cost = count * instances; draw_counts = uint2(count, instances); @@ -119,10 +128,11 @@ void main(uint3 id : SV_DispatchThreadID, uint gi : SV_GroupIndex) { } else if (active) { // Mesh dispatches: the console's are one-dimensional. uint3 g = src_args.Load3(src_at); - // Ahead of its draw, a record over the most a dispatch runs is a - // slot the title has not filled (the title leaves all - // ones there): empty, and not counted below either. - if (ahead && g.x > 65535u) { + // A record over the most a dispatch runs is a slot the title has + // not filled (the title leaves all ones there): empty, + // and not counted below either. (65,535 groups of meshlets read + // through the garbage beside it read past every buffer.) + if (g.x > 65535u) { g = uint3(0, 0, 0); } asked = g.x; @@ -1006,6 +1016,7 @@ RWByteAddressBuffer o : register(u0); void main(uint3 id : SV_DispatchThreadID, uint g : SV_GroupIndex) { if (step == 0) { if (id.x == 0) o.Store(at, v[which].Load(first)); } else if (step == 1) { if (id.x == 0) o.Store(at, c[which].d[first / 16][(first / 4) & 3]); } + else if (step == 16) { o.Store(at + id.x * 4, c[which].d[first / 16 + id.x][(first / 4) & 3]); } else { o.Store(at + id.x * 4, v[which].Load(first + id.x * step)); } } "#; @@ -1071,7 +1082,7 @@ void main(uint3 id : SV_DispatchThreadID, uint g : SV_GroupIndex) { } let table2 = D3D12_GPU_DESCRIPTOR_HANDLE { ptr: table.ptr + 8 * step as u64 }; let offsets = [0u32, 4092, 4096, 4104, 8192, 65536, 524_288, (1 << 20) - 4]; - for mode in ["one load (uniform)", "a load a thread", "constant buffer load"] { + for mode in ["one load (uniform)", "a load a thread", "constant buffer load", "Constant buffer load a thread"] { println!("{mode}:"); for (which, name) in names.iter().enumerate() { // SAFETY: our open list, live buffers. @@ -1087,6 +1098,7 @@ void main(uint3 id : SV_DispatchThreadID, uint g : SV_GroupIndex) { let step = match mode.as_bytes()[0] { b'o' => 0u32, b'c' => 1, + b'C' => 16, _ => 4, }; // (A thread's loads end at the offset.) @@ -1832,18 +1844,13 @@ void main(uint3 t : SV_DispatchThreadID) { assert_eq!(rec, [groups, u32::from(groups > 0).max(1).min(1), 1, 0xAB00 + i as u32], "filled record {i}, ahead {ahead}"); } let charged_filled = (5 + 519) * 128; - if ahead { - for i in filled.len()..RECORDS as usize { - assert_eq!(out[i * 8..i * 8 + 3], [0, 0, 0], "unfilled slot {i} must come out empty"); - assert_eq!(out[i * 8 + 3], u32::MAX, "the words after the record are copied as they are"); - } - assert_eq!(spent, charged_filled, "unfilled slots charge nothing"); - assert_eq!(largest, 519, "and are not counted as asked for"); - } else { - // Thirteen slots at 65,535 groups: sixteen would be all of it. - assert_eq!(spent, charged_filled + 13 * 65535 * 128, "clamped one by one, each slot is charged the most"); - assert_eq!(largest, u32::MAX); + // As one window ahead of its draws and one by one alike. + for i in filled.len()..RECORDS as usize { + assert_eq!(out[i * 8..i * 8 + 3], [0, 0, 0], "unfilled slot {i} must come out empty"); + assert_eq!(out[i * 8 + 3], u32::MAX, "the words after the record are copied as they are"); } + assert_eq!(spent, charged_filled, "unfilled slots charge nothing"); + assert_eq!(largest, 519, "and are not counted as asked for"); } } @@ -1866,8 +1873,9 @@ void main(uint3 t : SV_DispatchThreadID) { let mut initial_src = vec![0u8; total_bytes]; for i in 0..RECORDS as usize { let words: [u32; 6] = [ - u32::MAX, // IndexCount -> clamps to 1 << 24 - u32::MAX, // InstanceCount -> clamps to 4 (cost = 1 << 26) + // The last is no draw: empty, and free. + if i + 1 < RECORDS as usize { 1 << 22 } else { 1 << 23 }, // IndexCount + u32::MAX, // InstanceCount -> clamps to 16 (cost = 1 << 26) 1000 + i as u32, // StartIndexLocation (-200i32 + i as i32) as u32, // BaseVertexLocation 5000 + i as u32, // StartInstanceLocation @@ -1932,7 +1940,7 @@ void main(uint3 t : SV_DispatchThreadID) { let mut zeroed = 0usize; for i in 0..RECORDS as usize { let rec = &out_words[i * 6..(i + 1) * 6]; - if rec[0] == (1 << 24) && rec[1] == 4 { + if rec[0] == (1 << 22) && rec[1] == 16 { kept += 1; } else { assert_eq!((rec[0], rec[1]), (0, 0), "excess draw must be zeroed"); @@ -2519,7 +2527,7 @@ void main(uint3 group : SV_GroupID, uint3 thread : SV_DispatchThreadID) { }; let pass = make_from(&warp.device, batch::SOURCE).expect("the batched clamp must compile on WARP"); let kinds = [0u32, 1, 2, 10]; - let values = [0u32, 1, 3, 100, 65_535, 65_536, 1 << 20, 1 << 24, (1 << 26) + 1, u32::MAX]; + let values = [0u32, 1, 3, 100, 65_535, 65_536, 1 << 20, (1 << 23) - 1, 1 << 23, 1 << 24, (1 << 26) + 1, u32::MAX]; // Every kind with many records, in two sources. let mut records: Vec<(u32, [u32; 5])> = Vec::new(); for kind in kinds { @@ -2612,7 +2620,7 @@ void main(uint3 group : SV_GroupID, uint3 thread : SV_DispatchThreadID) { let mut table = unsafe { std::ptr::write_bytes(budget_ptr, 0, 256); for r in 0..records { - for (w, word) in [1u32 << 24, 4, 5, 6].into_iter().enumerate() { + for (w, word) in [1u32 << 22, 16, 5, 6].into_iter().enumerate() { src_ptr.cast::().add(r as usize * 4 + w).write_volatile(word); } } @@ -2636,8 +2644,8 @@ void main(uint3 group : SV_GroupID, uint3 thread : SV_DispatchThreadID) { (0..records as usize).map(|r| back_ptr.cast::().add(r * batch::SLOT as usize / 4).read_volatile()).collect(), ) }; - assert_eq!(out.iter().filter(|&&v| v == 1 << 24).count(), filled, "{times} times {records}: {out:?}"); - assert!(out.iter().all(|&v| v == 0 || v == 1 << 24)); + assert_eq!(out.iter().filter(|&&v| v == 1 << 22).count(), filled, "{times} times {records}: {out:?}"); + assert!(out.iter().all(|&v| v == 0 || v == 1 << 22)); assert_eq!(spent, BUDGET.min((1 << 26) * times * records as u32)); } } diff --git a/crates/victory-gpu/src/indirect/batch.rs b/crates/victory-gpu/src/indirect/batch.rs index ed25aa5..21af58a 100644 --- a/crates/victory-gpu/src/indirect/batch.rs +++ b/crates/victory-gpu/src/indirect/batch.rs @@ -77,7 +77,10 @@ void main(uint3 id : SV_DispatchThreadID, uint gi : SV_GroupIndex) { record[w] = load(job.x & 3, job.y + w * 4); } if (kind <= 1) { - record[0] = min(record[0], 1u << 24); + if (record[0] >= 1u << 23) { + record[0] = 0; + record[1] = 0; + } record[1] = min(record[1], (1u << 26) / max(record[0], 1u)); cost = record[0] * record[1]; } else if (kind == 2) { @@ -91,8 +94,10 @@ void main(uint3 id : SV_DispatchThreadID, uint gi : SV_GroupIndex) { record[1] = g.y; record[2] = g.z; } else { + if (record[0] > 65535u) { + record[0] = 0; + } asked = record[0]; - record[0] = min(record[0], 65535u); record[1] = min(record[1], 1u); record[2] = min(record[2], 1u); cost = record[0] * record[1] * record[2] * 128u; diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 86ceff9..3c60174 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -6810,11 +6810,17 @@ impl<'a> Lower<'a> { } /// The heap index to read descriptor `src` at, `index` being the title's: - /// for a buffer picked from the heap itself and read as raw bytes (or as - /// a constant buffer), the host's raw twin of that slot - /// ([`super::shader::raw_views`]), whose reads end where the view does. - /// Everything else (tables, textures, typed buffers) stays in place. - /// And whether it is a twin's (inside the host's heap). + /// for a buffer read as raw bytes, through a table or picked from the + /// heap itself (or picked and read as a constant buffer), the host's raw + /// twin of that slot ([`super::shader::raw_views`]), whose reads end where + /// the view does. Everything else (textures, typed buffers, tables' + /// constant buffers) stays in place. And whether it is a twin's (inside + /// the host's heap). + /// + /// Tables too: a view that does not begin on 16 bytes cannot be raw, and + /// a pixel program of the main pass (3711fdad456538a1, stopped at in the + /// device losses of runs n03 and n04) reads a structured one of 8 byte + /// elements at 8 as raw bytes per lane. fn raw_twin(&mut self, src: &Source, index: V, sampler: bool, props: (u32, u32)) -> (V, bool) { // Raw and constant buffers (DXIL resource kinds 11 and 13). const RAW: u32 = 11; @@ -6827,7 +6833,9 @@ impl<'a> Lower<'a> { self.scalar .get(load) .and_then(|a| self.x.native.layout.get(a.param)) - .is_some_and(|p| p.root == RootType::ResourceHeap) + .is_some_and(|p| { + p.root == RootType::ResourceHeap || (p.root == RootType::Table && props.0 & 0xFF == RAW) + }) }; if first == 0 || sampler || !matches!(props.0 & 0xFF, RAW | CONSTANT) || !src.leaves().iter().all(picked) { return (index, false); diff --git a/crates/victory-shader/src/emit/shader.rs b/crates/victory-shader/src/emit/shader.rs index c545294..a0cf4bf 100644 --- a/crates/victory-shader/src/emit/shader.rs +++ b/crates/victory-shader/src/emit/shader.rs @@ -138,9 +138,10 @@ pub fn view_heap_limit() -> u32 { /// entry nothing wrote. On AMD a raw load through a view that is not raw (typed, structured, a /// constant buffer's) has no end: it reads whatever lies there, up to 4 GiB on, and loses the /// device where nothing does; and a texture's descriptor read as a buffer's is a wild address. -/// So the host keeps, for title slot `i`, a raw view of the same bytes at this place + `i` -/// (a constant buffer view of a constant buffer, a null raw view of everything else and of every -/// slot past the title's), and shaders translated while this is set read picked buffers there. +/// So the host keeps, for slot `i`, a raw view of the same bytes at this place + `i` (a constant +/// buffer view of a constant buffer in a title slot, a null raw view of everything else), and +/// shaders translated while this is set read picked buffers there, and tables' buffers that they +/// read as raw bytes. /// A power of two above every slot index. static RAW_VIEWS: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); diff --git a/crates/victory-shader/src/emit/size_tests.rs b/crates/victory-shader/src/emit/size_tests.rs index 0cb8696..9c78bf4 100644 --- a/crates/victory-shader/src/emit/size_tests.rs +++ b/crates/victory-shader/src/emit/size_tests.rs @@ -322,15 +322,17 @@ fn a_picked_buffer_is_read_at_its_raw_twin() { }; let (title, twin, last, texel) = ([1u32, 2, 3, 4], [5u32, 6, 7, 8], [9u32, 10, 11, 12], [[0.0; 4]; 1]); let raw = |data| View::Buffer { data, stride: Stride::Raw }; - let views = || { + // (The store goes through the table's third entry: its twin, too.) + let views = |twins: bool| { + let uav = |here: bool| if here { View::RawUav(2) } else { View::Texture(1, 1, &texel) }; [ View::Texture(1, 1, &texel), raw(&title), - View::RawUav(2), + uav(!twins), View::Texture(1, 1, &texel), View::Texture(1, 1, &texel), raw(&twin), - View::Texture(1, 1, &texel), + uav(twins), raw(&last), ] }; @@ -338,8 +340,32 @@ fn a_picked_buffer_is_read_at_its_raw_twin() { super::shader::RAW_VIEWS_HERE.set(Some(first)); let shader = build(&program(at), (8, 0)).expect("translates"); super::shader::RAW_VIEWS_HERE.set(None); - for (label, got) in run(&shader, &views()) { + for (label, got) in run(&shader, &views(first != 0)) { assert_eq!(got[..2], [0, expected], "{label}: twins from {first}, byte {at}"); } } } + +/// A table's buffer read as raw bytes is read at its raw twin too. +#[test] +fn a_tables_raw_buffer_is_read_at_its_raw_twin() { + let load = [ + 0xF408_0201u32, // s_load_dwordx4 s[8:11], s[2:3], 0 + 0xFA00_0000, + 0xE030_0004, // buffer_load_dword v5, off, s[8:11], 0 offset:4 + 0x8002_0500, + 0x7E0A_0505, // v_readfirstlane_b32 s5, v5 + 0xBE84_0380, // s_mov_b32 s4, 0 + ]; + let words = [TABLE, &load, STORE].concat(); + let (title, twin, texel) = ([1u32, 2, 3, 4], [5u32, 6, 7, 8], [[0.0; 4]; 1]); + let raw = |data| View::Buffer { data, stride: Stride::Raw }; + super::shader::RAW_VIEWS_HERE.set(Some(4)); + let shader = build(&words, (4, 0)).expect("translates"); + super::shader::RAW_VIEWS_HERE.set(None); + let other = || View::Texture(1, 1, &texel); + let views = [raw(&title), other(), other(), other(), raw(&twin), other(), View::RawUav(2)]; + for (label, got) in run(&shader, &views) { + assert_eq!(got[..2], [0, 6], "{label}"); + } +}