diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index 256b8e5..2652ad4 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -286,12 +286,13 @@ pub(crate) static SYNCED: [std::sync::atomic::AtomicU64; 6] = [const { std::sync /// Nothing while no heap page changed. #[cfg(windows)] pub(crate) fn sync_line(frames: f64) { - let [twins, as_is] = [0, 1].map(|i| host::TWINS[i].swap(0, std::sync::atomic::Ordering::Relaxed)); + let [twins, moved, lost] = [0, 1, 2].map(|i| host::TWINS[i].swap(0, std::sync::atomic::Ordering::Relaxed)); if twins > 0 { tracing::info!( - "raw twins of buffer views made per frame (VICTORY_RAW_VIEWS=0: none): {:.0}, of them {:.1} as the title's view is (it does not begin on 16 bytes)", + "raw twins of buffer views made per frame (VICTORY_RAW_VIEWS=0: none): {:.0}, of them {:.1} that do not begin on 16 bytes ({:.1} null)", twins as f64 / frames, - as_is as f64 / frames + moved as f64 / frames, + lost as f64 / frames ); } let [pages, looked, mirrored, micros, tabled, later] = [0, 1, 2, 3, 4, 5].map(|i| SYNCED[i].swap(0, std::sync::atomic::Ordering::Relaxed)); diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 03187e5..2a6372f 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -1252,7 +1252,8 @@ impl HostHeap { /// A plain point sampler at slot `index`, for sampler slots without a /// view. fn null_sampler(&self, index: u32) { - null_sampler(self.cpu(index)); + null_sampler(self.at(index)); + self.put(index); } } @@ -1662,7 +1663,8 @@ impl HostHeap { let Some(view) = view_at(key as u32, (key >> 32) as u32) else { continue; }; - if let Some(kept) = make(&view, self.cpu(slot)) { + if let Some(kept) = make(&view, self.at(slot)) { + self.put(slot); self.keep(slot, kept); remade += 1; } @@ -1770,7 +1772,8 @@ impl HostHeap { FLIPPED[1].fetch_add(1, Relaxed); continue; }; - if let Some(kept) = make(&view, self.cpu(f.index)) { + if let Some(kept) = make(&view, self.at(f.index)) { + self.put(f.index); *shown = f.key; self.keep(f.index, kept); flipped += 1; @@ -1799,9 +1802,10 @@ impl HostHeap { return; } match view_at(key as u32, (key >> 32) as u32) { - Some(view) => null_for(&view, self.cpu(index)), - None => null_view(self.cpu(index)), + Some(view) => null_for(&view, self.at(index)), + None => null_view(self.at(index)), } + self.put(index); self.clear_twin(index); self.keep(index, None); } @@ -2027,10 +2031,11 @@ 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.cpu(index)); + let kept = make(view, self.at(index)); if kept.is_none() { - null_for(view, self.cpu(index)); + null_for(view, self.at(index)); } + self.put(index); self.keep(index, kept.flatten()); } @@ -2039,14 +2044,15 @@ impl HostHeap { if self.sampler { self.null_sampler(index); } else { - null_view(self.cpu(index)); + null_view(self.at(index)); + self.put(index); } self.keep(index, None); } /// Where the raw twin of title slot `index` is. fn twin(&self, index: u32) -> Option { - (index < self.twins.len() as u32).then(|| self.cpu(self.raw + index)) + (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. @@ -2054,7 +2060,8 @@ impl HostHeap { if let Some(made) = self.twins.get(index as usize) && made.swap(false, std::sync::atomic::Ordering::Relaxed) { - null_buffer_view(self.cpu(self.raw + index)); + null_buffer_view(self.at(self.raw + index)); + self.put(self.raw + index); } } @@ -2098,15 +2105,17 @@ impl HostHeap { // SAFETY: a slot of our heap and a live buffer covering the range. unsafe { host.device - .CreateShaderResourceView(buffer, Some(&desc), self.cpu(index)) + .CreateShaderResourceView(buffer, Some(&desc), self.at(index)) }; + self.put(index); self.keep(index, Some(buffer.clone())); } /// Makes slot `index` (a dynamic one) a raw buffer view that reads /// zeros. pub(crate) fn null_raw_at(&self, index: u32) { - null_buffer_view(self.cpu(index)); + null_buffer_view(self.at(index)); + self.put(index); self.keep(index, None); } @@ -2136,9 +2145,10 @@ impl HostHeap { buffer, None::<&ID3D12Resource>, Some(&desc), - self.cpu(index), + self.at(index), ) }; + self.put(index); self.keep(index, Some(buffer.clone())); } @@ -2163,9 +2173,10 @@ impl HostHeap { None::<&ID3D12Resource>, None::<&ID3D12Resource>, Some(&desc), - self.cpu(index), + self.at(index), ) }; + self.put(index); self.keep(index, None); } @@ -2198,13 +2209,47 @@ impl HostHeap { // SAFETY: a slot of our heap; the memory is the caller's. unsafe { host.device - .CreateConstantBufferView(Some(&desc), self.cpu(index)) + .CreateConstantBufferView(Some(&desc), self.at(index)) }; + self.put(index); // After the descriptor: what it showed is let go when nothing shows // it any more. self.keep(index, holds.then(|| buffer.clone())); } + /// Where a descriptor for slot `index` is made, then [`HostHeap::put`]: + /// a slot of this thread's in a heap the card does not see + /// ([`staged`]), or the slot itself. + fn at(&self, index: u32) -> D3D12_CPU_DESCRIPTOR_HANDLE { + staged(self.sampler, 0).unwrap_or_else(|| self.cpu(index)) + } + + /// The descriptor this thread made last ([`HostHeap::at`]) goes to slot + /// `index`. + fn put(&self, index: u32) { + self.copy_to(index, 0); + } + + /// The raw twin this thread made last goes to that of slot `index`. + fn put_twin(&self, index: u32) { + if self.twin(index).is_some() { + self.copy_to(self.raw + index, 1); + } + } + + fn copy_to(&self, to: u32, which: u32) { + let (Some(from), Some(host)) = (staged(self.sampler, which), host()) else { + return; // made in place + }; + let kind = if self.sampler { + D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER + } else { + D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV + }; + // SAFETY: a slot of our heap, and one of this thread's heap slots. + unsafe { host.device.CopyDescriptorsSimple(1, self.cpu(to), from, kind) }; + } + fn cpu(&self, index: u32) -> D3D12_CPU_DESCRIPTOR_HANDLE { D3D12_CPU_DESCRIPTOR_HANDLE { ptr: self.cpu + index as usize * self.step as usize, @@ -2371,12 +2416,16 @@ impl HostHeap { let kept = if later { None } else { - make_twinned(&view, self.cpu(index), self.twin(index)) + make_twinned(&view, self.at(index), self.twin(index)) }; if kept.is_none() && !self.sampler { - null_for(&view, self.cpu(index)); + null_for(&view, self.at(index)); + } + 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); } @@ -2445,6 +2494,90 @@ impl HostHeap { } } +/// Slots of this thread's own in heaps the card does not see, two of views +/// and two of samplers (`which` 0 or 1): descriptors are made there and +/// copied whole into the heaps it does see. Made in place they are seen half +/// written: an RX 7900 XTX saw a texture of no size in 4 % of its reads of +/// slots whose views a thread made over and over (the driver clears the +/// descriptor and fills it step by step), and none in 8,500 million with the +/// same descriptors copied, three times as fast +/// (`indirect::tests::descriptors_written_under_readers`). Title threads and +/// our passes rewrite slots while submissions run whose shaders pick them; +/// three device losses of 2026-11-25 (play-1125-0203, n02, n03) were reads of +/// memory no view names. `VICTORY_VIEW_COPY=0`: made in place, as before. +fn staged(sampler: bool, which: u32) -> Option { + /// Threads at a time that have slots; more make views in place. + const THREADS: u32 = 4096; + struct Stage { + starts: [usize; 2], + steps: [usize; 2], + _heaps: [ID3D12DescriptorHeap; 2], + } + // SAFETY: descriptor heaps are free-threaded; each thread has slots of + // its own. + unsafe impl Send for Stage {} + // SAFETY: as above. + unsafe impl Sync for Stage {} + static STAGE: std::sync::OnceLock> = std::sync::OnceLock::new(); + /// Places given back, and how many were ever given out. + static PLACES: Mutex<(Vec, u32)> = Mutex::new((Vec::new(), 0)); + struct Place(Option); + impl Drop for Place { + fn drop(&mut self) { + if let Some(place) = self.0 { + PLACES.lock().unwrap_or_else(|e| e.into_inner()).0.push(place); + } + } + } + thread_local! { + static PLACE: Place = { + let mut places = PLACES.lock().unwrap_or_else(|e| e.into_inner()); + Place(places.0.pop().or_else(|| { + (places.1 < THREADS).then(|| { + places.1 += 1; + places.1 - 1 + }) + })) + }; + } + let stage = STAGE + .get_or_init(|| { + if std::env::var_os("VICTORY_VIEW_COPY").is_some_and(|v| v == "0") { + return None; + } + let host = host()?; + let make = |kind: D3D12_DESCRIPTOR_HEAP_TYPE| -> Option { + // SAFETY: plain heap creation. + unsafe { + host.device.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { + Type: kind, + NumDescriptors: 2 * THREADS, + ..Default::default() + }) + } + .inspect_err(|e| tracing::warn!(error = %e, "no heap to make views in: they are made in place")) + .ok() + }; + let kinds = [D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER]; + let heaps = [make(kinds[0])?, make(kinds[1])?]; + // SAFETY: live heaps, plain queries. + let (starts, steps) = unsafe { + ( + [0, 1].map(|i: usize| heaps[i].GetCPUDescriptorHandleForHeapStart().ptr), + kinds.map(|kind| host.device.GetDescriptorHandleIncrementSize(kind) as usize), + ) + }; + Some(Stage { starts, steps, _heaps: heaps }) + }) + .as_ref()?; + // (A thread that is ending has none any more.) + let place = PLACE.try_with(|p| p.0).ok().flatten()?; + let kind = usize::from(sampler); + Some(D3D12_CPU_DESCRIPTOR_HANDLE { + ptr: stage.starts[kind] + (2 * place + which) as usize * stage.steps[kind], + }) +} + /// Logs `what` the first time it happens. macro_rules! once { ($($arg:tt)*) => {{ @@ -2671,14 +2804,46 @@ fn buffer_view_ok(format: DXGI_FORMAT, first: u64, count: u32, stride: u32, raw: .is_some_and(|end| end <= width) } -/// Raw twins ([`raw_twins`]) made since the last frame line, and those of them that had -/// to be views as the title's slot has them (a raw view begins on 16 bytes). -pub(crate) static TWINS: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2]; +/// Raw twins ([`raw_twins`]) made since the last frame line, and those of them that +/// do not begin on 16 bytes, as a raw view must ([`move_raw`]), and those of +/// these that could not be moved there (null). +pub(crate) static TWINS: [std::sync::atomic::AtomicU64; 3] = [const { std::sync::atomic::AtomicU64::new(0) }; 3]; + +/// Moves the raw view at `at`, a slot of our heaps the card does not see, +/// which shows `bytes` from `start` on (GPU address), `by` bytes on: for a +/// twin whose view begins between two multiples of 16, where the host makes +/// no raw view. An AMD descriptor of a raw buffer is its address (48 bits), +/// its bytes and flags; only one that says just that is changed, and only +/// where what the handle points at is the descriptor. False: not moved. +fn move_raw(at: D3D12_CPU_DESCRIPTOR_HANDLE, start: u64, bytes: u32, by: u32) -> bool { + if !by.is_multiple_of(4) || by >= bytes { + return false; + } + let mut words = [0u8; 16]; + if !crate::host::read_into(at.ptr as *const u8, &mut words) { + return false; + } + let word = |i: usize| u32::from_le_bytes([words[4 * i], words[4 * i + 1], words[4 * i + 2], words[4 * i + 3]]); + let address = u64::from(word(0)) | u64::from(word(1)) << 32; + if address != start || word(2) != bytes || start >> 48 != 0 { + return false; + } + let moved = start + u64::from(by); + // SAFETY: this thread's own slot (it has slots of its own: the caller + // asks only for those), read just now. + unsafe { + let words = at.ptr as *mut u32; + words.write(moved as u32); + words.add(1).write((moved >> 32) as u32); + words.add(2).write(bytes - by); + } + true +} /// The bytes a buffer view the host takes ([`buffer_view_ok`]) shows as a raw -/// view (first dword, dwords), if it begins where one can; of a buffer of -/// `width` bytes. -fn twin_window(format: DXGI_FORMAT, first: u64, count: u32, stride: u32, raw: bool, width: u64) -> Option<(u64, u32)> { +/// view (first dword, dwords, and the bytes past that dword it begins: a raw +/// view begins on 16 bytes); of a buffer of `width` bytes. +fn twin_window(format: DXGI_FORMAT, first: u64, count: u32, stride: u32, raw: bool, width: u64) -> Option<(u64, u32, u32)> { use std::sync::atomic::Ordering::Relaxed; TWINS[0].fetch_add(1, Relaxed); let element = if raw { @@ -2688,15 +2853,17 @@ fn twin_window(format: DXGI_FORMAT, first: u64, count: u32, stride: u32, raw: bo } else { crate::resource::element(format.0 as u32).map_or(0, |(bytes, _)| bytes) }; - let start = first * element; - let dwords = (u64::from(count) * element).div_ceil(4).min((width - start.min(width)) / 4); - let window = (start % 16 == 0 && dwords > 0) - .then(|| u32::try_from(dwords).ok().map(|dwords| (start / 4, dwords))) - .flatten(); - if window.is_none() { + let (start, end) = (first * element, ((first + u64::from(count)) * element).next_multiple_of(4)); + let end = end.min(width & !3); + let from = start & !15; + if end <= start { + return None; + } + let dwords = u32::try_from((end - from) / 4).ok()?; + if from != start { TWINS[1].fetch_add(1, Relaxed); } - window + Some((from / 4, dwords, (start - from) as u32)) } fn buffer_srv( @@ -2776,7 +2943,8 @@ fn buffer_srv( .CreateShaderResourceView(&shadow.buffer, Some(&desc), at) }; if let Some(twin) = twin { - if let Some((first, count)) = twin_window(desc.Format, first, count, stride, raw, width) { + let window = twin_window(desc.Format, first, count, stride, raw, width); + if let Some((first, count, by)) = window { desc.Format = DXGI_FORMAT_R32_TYPELESS; desc.Anonymous.Buffer = D3D12_BUFFER_SRV { FirstElement: first, @@ -2784,12 +2952,19 @@ fn buffer_srv( StructureByteStride: 0, Flags: D3D12_BUFFER_SRV_FLAG_RAW, }; + // SAFETY: a live buffer and a slot of our heap. + let start = unsafe { + host.device + .CreateShaderResourceView(&shadow.buffer, Some(&desc), twin); + shadow.buffer.GetGPUVirtualAddress() + first * 4 + }; + if by > 0 && !(staged(false, 1).is_some_and(|s| s.ptr == twin.ptr) && move_raw(twin, start, count.saturating_mul(4), by)) { + TWINS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed); + null_buffer_view(twin); + } + } else { + null_buffer_view(twin); } - // SAFETY: a live buffer and a slot of our heap. - unsafe { - host.device - .CreateShaderResourceView(&shadow.buffer, Some(&desc), twin) - }; } Some(Some(shadow.buffer.clone())) } @@ -2891,7 +3066,8 @@ fn buffer_uav( .CreateUnorderedAccessView(&shadow.buffer, None::<&ID3D12Resource>, Some(&desc), at) }; if let Some(twin) = twin { - if let Some((first, count)) = twin_window(desc.Format, first, count, stride, raw, width) { + let window = twin_window(desc.Format, first, count, stride, raw, width); + if let Some((first, count, by)) = window { desc.Format = DXGI_FORMAT_R32_TYPELESS; desc.Anonymous.Buffer = D3D12_BUFFER_UAV { FirstElement: first, @@ -2900,12 +3076,19 @@ fn buffer_uav( CounterOffsetInBytes: 0, Flags: D3D12_BUFFER_UAV_FLAG_RAW, }; + // SAFETY: a live buffer and a slot of our heap. + let start = unsafe { + host.device + .CreateUnorderedAccessView(&shadow.buffer, None::<&ID3D12Resource>, Some(&desc), twin); + shadow.buffer.GetGPUVirtualAddress() + first * 4 + }; + if by > 0 && !(staged(false, 1).is_some_and(|s| s.ptr == twin.ptr) && move_raw(twin, start, count.saturating_mul(4), by)) { + TWINS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed); + null_buffer_view(twin); + } + } else { + null_buffer_view(twin); } - // SAFETY: a live buffer and a slot of our heap. - unsafe { - host.device - .CreateUnorderedAccessView(&shadow.buffer, None::<&ID3D12Resource>, Some(&desc), twin) - }; } Some(Some(shadow.buffer.clone())) } @@ -3709,15 +3892,94 @@ mod tests { #[test] fn raw_twins_show_the_views_bytes() { let (u16s, u32s, none) = (DXGI_FORMAT(57), DXGI_FORMAT(42), DXGI_FORMAT_UNKNOWN); - assert_eq!(twin_window(u32s, 4, 10, 0, false, 4096), Some((4, 10))); - assert_eq!(twin_window(u16s, 8, 7, 0, false, 4096), Some((4, 4))); + assert_eq!(twin_window(u32s, 4, 10, 0, false, 4096), Some((4, 10, 0))); + assert_eq!(twin_window(u16s, 8, 7, 0, false, 4096), Some((4, 4, 0))); // the last dword would reach past the buffer - assert_eq!(twin_window(u16s, 8, 7, 0, false, 30), Some((4, 3))); - assert_eq!(twin_window(none, 2, 3, 24, false, 4096), Some((12, 18))); - assert_eq!(twin_window(DXGI_FORMAT_R32_TYPELESS, 8, 5, 0, true, 4096), Some((8, 5))); - // where no raw view begins: the view as it is - assert_eq!(twin_window(none, 1, 3, 24, false, 4096), None); - assert_eq!(twin_window(u32s, 1, 10, 0, false, 4096), None); + assert_eq!(twin_window(u16s, 8, 7, 0, false, 30), Some((4, 3, 0))); + assert_eq!(twin_window(none, 2, 3, 24, false, 4096), Some((12, 18, 0))); + assert_eq!(twin_window(DXGI_FORMAT_R32_TYPELESS, 8, 5, 0, true, 4096), Some((8, 5, 0))); + // where no raw view begins: from the 16 bytes below + assert_eq!(twin_window(none, 1, 3, 24, false, 4096), Some((4, 20, 8))); + assert_eq!(twin_window(u32s, 1, 10, 0, false, 4096), Some((0, 11, 4))); + assert_eq!(twin_window(u32s, 1, 10, 0, false, 4), None); + } + + /// What a slot is made as is made beside the heap and copied in whole: the + /// slot's descriptor is the one the view has, and each thread makes its + /// own. + #[test] + fn views_are_made_beside_the_heap_and_copied_in() { + let Some(host) = host() else { + return; // no host GPU here + }; + let Some(heap) = HostHeap::new(false, 4) else { + return; + }; + let (Some(a), Some(b)) = (staged(false, 0), staged(false, 1)) else { + return; + }; + assert_ne!(a.ptr, b.ptr); + assert_ne!(Some(a.ptr), std::thread::spawn(|| staged(false, 0).map(|h| h.ptr)).join().unwrap()); + let (buffer, _) = host.shared_buffer(1 << 16).unwrap(); + let index = heap.dynamic(1).unwrap(); + let bytes = |at: D3D12_CPU_DESCRIPTOR_HANDLE| { + let mut bytes = [0u8; 32]; + crate::host::read_into(at.ptr as *const u8, &mut bytes).then_some(bytes) + }; + heap.raw_at(index, &buffer, 64, 1024); + let (slot, beside) = (bytes(heap.cpu(index)), bytes(staged(false, 0).unwrap())); + assert_eq!(slot, beside, "copied as it was made"); + heap.null_raw_at(index); + assert_ne!(bytes(heap.cpu(index)), slot, "another view since"); + // SAFETY: a plain query. + assert!(unsafe { host.device.GetDeviceRemovedReason() }.is_ok()); + } + + /// A raw view moved on by some dwords says so in its descriptor, on a + /// card whose descriptors are what we take them for; on another nothing + /// is changed. + #[test] + fn a_raw_view_is_moved_only_where_its_descriptor_says_where_it_is() { + let (Some(host), Some(at)) = (host(), staged(false, 1)) else { + return; // no host GPU here + }; + let (buffer, _) = host.shared_buffer(1 << 16).unwrap(); + let desc = 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: 8, + NumElements: 100, + StructureByteStride: 0, + Flags: D3D12_BUFFER_SRV_FLAG_RAW, + }, + }, + }; + // SAFETY: this thread's slot, a live buffer. + let start = unsafe { + host.device.CreateShaderResourceView(&buffer, Some(&desc), at); + buffer.GetGPUVirtualAddress() + 32 + }; + let words = || { + let mut bytes = [0u8; 32]; + assert!(crate::host::read_into(at.ptr as *const u8, &mut bytes)); + bytes + }; + let before = words(); + assert!(!move_raw(at, start + 16, 400, 8), "another address"); + assert!(!move_raw(at, start, 404, 8), "another size"); + assert!(!move_raw(at, start, 400, 2), "not a dword"); + assert_eq!(words(), before); + if move_raw(at, start, 400, 8) { + let moved = words(); + assert_eq!(moved[..8], (start + 8).to_le_bytes()); + assert_eq!(moved[8..12], 392u32.to_le_bytes()); + assert_eq!(moved[12..], before[12..]); + } else { + assert_eq!(words(), before); + } } /// Views that begin where no host view can: raw ones off 16 bytes, diff --git a/crates/victory-gpu/src/indirect.rs b/crates/victory-gpu/src/indirect.rs index 86e68fb..60b645c 100644 --- a/crates/victory-gpu/src/indirect.rs +++ b/crates/victory-gpu/src/indirect.rs @@ -1114,6 +1114,413 @@ void main(uint3 id : SV_DispatchThreadID, uint g : SV_GroupIndex) { println!("device: {:?}", unsafe { gpu.device.GetDeviceRemovedReason() }); } + /// Whether the card can read a texture's descriptor half written + /// (ignored: a measurement of the card, `VICTORY_BENCH_S` seconds a way, + /// 5). Two slots change between a texture of 64x64 and one of 2048x2048 + /// as fast as a thread can write them, while groups that each load the + /// descriptor anew ask its size and read its last texel: size and texel + /// of two textures are one descriptor half written. (Both lie in one heap + /// that is there as a whole: a read past the small one finds memory.) + #[test] + #[ignore] + fn descriptors_written_under_readers() { + let Some(mut gpu) = WarpHarness::on(std::env::var_os("VICTORY_BENCH_WARP").is_some()) else { + return; + }; + let seconds = std::env::var("VICTORY_BENCH_S").ok().and_then(|v| v.parse().ok()).unwrap_or(5u64); + const SOURCE: &str = r#" +#define RS "RootConstants(num32BitConstants=1,b0), DescriptorTable(SRV(t0,numDescriptors=2,flags=DESCRIPTORS_VOLATILE)), UAV(u0)" +cbuffer C : register(b0) { uint odd; } +Texture2D t[2] : register(t0); +RWByteAddressBuffer o : register(u0); +[RootSignature(RS)] +[numthreads(64, 1, 1)] +void main(uint3 g : SV_GroupID, uint i : SV_GroupIndex) { + uint w, h, n; + uint which = (g.x + odd) & 1; + t[which].GetDimensions(0, w, h, n); + uint r = (uint)(t[which].Load(int3(w - 1, h - 1, 0)).x * 255.0 + 0.5); + // 0: small, 1: large, 2: no size of the two, 3: small with another texel, 4: large with another + uint kind = (w == 64 && h == 64) ? (r == 17 ? 0 : 3) : (w == 2048 && h == 2048) ? (r == 34 ? 1 : 4) : 2; + if (i == 0) { + uint was; + o.InterlockedAdd(kind * 4, 1, was); + if (kind > 1) { o.Store(32, w); o.Store(36, h); o.Store(40, r); } + } +} +"#; + let pass = compile_cs(&gpu.device, SOURCE).expect("the pass must compile"); + let texture = |size: u64| D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: size, + Height: size as u32, + DepthOrArraySize: 1, + MipLevels: 1, + Format: DXGI_FORMAT(28), + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + ..Default::default() + }; + // SAFETY: plain creation; each texture in its own half of the heap. + let (heap, textures) = unsafe { + let mut heap: Option = None; + gpu.device + .CreateHeap( + &D3D12_HEAP_DESC { + SizeInBytes: 64 << 20, + Properties: D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, + Alignment: 0, + Flags: D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES, + }, + &mut heap, + ) + .unwrap(); + let heap = heap.unwrap(); + let textures = [(0u64, 64u64), (32 << 20, 2048)].map(|(at, size)| { + let mut made: Option = None; + gpu.device + .CreatePlacedResource(&heap, at, &texture(size), D3D12_RESOURCE_STATE_COPY_DEST, None, &mut made) + .unwrap(); + made.unwrap() + }); + (heap, textures) + }; + // Fill them: 17 and 34 in every byte. + for (texture, (size, byte)) in textures.iter().zip([(64u32, 17u8), (2048, 34)]) { + let pitch = size * 4; + let (from, data, _) = gpu.shared_buffer(u64::from(pitch) * u64::from(size)); + // SAFETY: the mapping holds the bytes; an open list, live resources. + unsafe { + std::ptr::write_bytes(data, byte, (pitch * size) as usize); + let src = D3D12_TEXTURE_COPY_LOCATION { + pResource: std::mem::transmute_copy(&from), + Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { + PlacedFootprint: D3D12_PLACED_SUBRESOURCE_FOOTPRINT { + Offset: 0, + Footprint: D3D12_SUBRESOURCE_FOOTPRINT { + Format: DXGI_FORMAT(28), + Width: size, + Height: size, + Depth: 1, + RowPitch: pitch, + }, + }, + }, + }; + let dst = D3D12_TEXTURE_COPY_LOCATION { + pResource: std::mem::transmute_copy(texture), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: 0 }, + }; + gpu.list.CopyTextureRegion(&dst, 0, 0, 0, &src, None); + gpu.list.ResourceBarrier(&[crate::host::transition( + texture, + D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, + )]); + } + gpu.submit_and_wait(); + } + let view_heap = |visible: bool| -> ID3D12DescriptorHeap { + // SAFETY: plain heap creation. + unsafe { + gpu.device.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { + Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + NumDescriptors: 4, + Flags: if visible { D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE } else { D3D12_DESCRIPTOR_HEAP_FLAG_NONE }, + NodeMask: 0, + }) + } + .unwrap() + }; + let (shown, kept) = (view_heap(true), view_heap(false)); + // SAFETY: live heaps. + let (to, from, step) = unsafe { + ( + shown.GetCPUDescriptorHandleForHeapStart().ptr, + kept.GetCPUDescriptorHandleForHeapStart().ptr, + gpu.device.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) as usize, + ) + }; + let at = |base: usize, i: usize| D3D12_CPU_DESCRIPTOR_HANDLE { ptr: base + i * step }; + for (i, texture) in textures.iter().enumerate() { + // SAFETY: slots of our heaps, live textures. + unsafe { + gpu.device.CreateShaderResourceView(texture, None, at(from, i)); + gpu.device.CreateShaderResourceView(texture, None, at(to, i)); + } + } + let bytes = |base: usize, i: usize| { + // SAFETY: a heap's handle; what it points at is read if it is memory. + let mut out = vec![0u8; step]; + crate::host::read_into((base + i * step) as *const u8, &mut out).then_some(out) + }; + println!("step {step}, the heaps' handles point at {:#x} and {:#x} (shown, kept)", to, from); + for i in 0..2 { + println!(" shown {i}: {:02x?}", bytes(to, i)); + println!(" kept {i}: {:02x?}", bytes(from, i)); + } + let same = bytes(to, 0).is_some() && bytes(to, 0) == bytes(from, 0) && bytes(to, 1) == bytes(from, 1); + println!("the shown heap's memory is the descriptors' own bytes: {same}"); + let images = [bytes(from, 0), bytes(from, 1)]; + // SAFETY: a live heap. + let table = unsafe { shown.GetGPUDescriptorHandleForHeapStart() }; + for way in ["view creation", "copies from a CPU heap", "one 32 byte store"] { + if way.starts_with("one") && !(same && step == 32) { + continue; + } + let (_out, out_ptr, out_gpu) = gpu.shared_buffer(1 << 12); + // SAFETY: the mapping. + unsafe { std::ptr::write_bytes(out_ptr, 0, 1 << 12) }; + let stop = std::sync::atomic::AtomicBool::new(false); + let writes = std::sync::atomic::AtomicU64::new(0); + std::thread::scope(|scope| { + let device = gpu.device.clone(); + let (stop, writes, textures, images) = (&stop, &writes, &textures, &images); + scope.spawn(move || { + let mut k = 0usize; + while !stop.load(std::sync::atomic::Ordering::Relaxed) { + for slot in 0..2 { + let which = (k + slot) & 1; + // SAFETY: slots of our heaps, live textures. + unsafe { + match way.as_bytes()[0] { + b'v' => device.CreateShaderResourceView(&textures[which], None, at(to, slot)), + b'c' => device.CopyDescriptorsSimple( + 1, + at(to, slot), + at(from, which), + D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + ), + _ => store32(to + slot * step, images[which].as_deref().unwrap()), + } + } + } + k += 1; + writes.fetch_add(2, std::sync::atomic::Ordering::Relaxed); + } + }); + let started = std::time::Instant::now(); + let mut odd = 0u32; + while started.elapsed().as_secs() < seconds { + // SAFETY: our open list, live objects. + unsafe { + gpu.list.SetDescriptorHeaps(&[Some(shown.clone())]); + gpu.list.SetComputeRootSignature(&pass.root); + gpu.list.SetPipelineState(&pass.pso); + gpu.list.SetComputeRootDescriptorTable(1, table); + gpu.list.SetComputeRootUnorderedAccessView(2, out_gpu); + for _ in 0..8 { + gpu.list.SetComputeRoot32BitConstants(0, 1, (&raw const odd).cast(), 0); + gpu.list.Dispatch(65_535, 1, 1); + odd += 1; + } + } + gpu.submit_and_wait(); + } + stop.store(true, std::sync::atomic::Ordering::Relaxed); + }); + // SAFETY: the mapping, the card is done. + let found: Vec = (0..11).map(|i| unsafe { out_ptr.cast::().add(i).read_volatile() }).collect(); + println!( + "{way}: {} writes; groups that saw the small texture {}, the large {}; no size of the two {}, the small size with another texel {}, the large size with another texel {}; the last such: {}x{} texel {}", + writes.load(std::sync::atomic::Ordering::Relaxed), + found[0], + found[1], + found[2], + found[3], + found[4], + found[8], + found[9], + found[10] + ); + // SAFETY: a plain query. + println!(" device: {:?}", unsafe { gpu.device.GetDeviceRemovedReason() }); + } + drop(heap); + } + + /// 32 bytes to `to` (32 byte aligned) in one store, then a fence. + /// + /// # Safety + /// `to` is 32 bytes of memory that may be written. + unsafe fn store32(to: usize, bytes: &[u8]) { + #[target_feature(enable = "avx")] + unsafe fn store(to: usize, bytes: *const u8) { + use std::arch::x86_64::*; + // SAFETY: the caller's. + unsafe { + _mm256_stream_si256(to as *mut __m256i, _mm256_loadu_si256(bytes.cast())); + _mm_sfence(); + } + } + assert!(std::is_x86_feature_detected!("avx") && bytes.len() == 32 && to.is_multiple_of(32)); + // SAFETY: asked for above; the caller's memory. + unsafe { store(to, bytes.as_ptr()) } + } + + /// What a texture read in another dimension than its view's gives + /// (ignored: a measurement of the card): a view of a small 2D texture + /// read as an array, a volume and a cube, and a small volume read as 2D, + /// far out. The texture lies at the start of a heap whose other megabytes + /// hold other bytes: a texel of those is a read past the texture. + #[test] + #[ignore] + fn textures_read_in_another_dimension() { + let Some(mut gpu) = WarpHarness::on(std::env::var_os("VICTORY_BENCH_WARP").is_some()) else { + return; + }; + const SOURCE: &str = r#" +#define RS "RootConstants(num32BitConstants=2,b0), DescriptorTable(SRV(t0,numDescriptors=4,flags=DESCRIPTORS_VOLATILE)), UAV(u0), StaticSampler(s0,filter=FILTER_MIN_MAG_MIP_POINT,addressU=TEXTURE_ADDRESS_WRAP,addressV=TEXTURE_ADDRESS_WRAP,addressW=TEXTURE_ADDRESS_WRAP)" +cbuffer C : register(b0) { uint how; uint far; } +Texture2D flat_ : register(t0); +Texture2DArray layers : register(t1); +Texture3D volume : register(t2); +TextureCubeArray cube : register(t3); +SamplerState s : register(s0); +RWByteAddressBuffer o : register(u0); +[RootSignature(RS)] +[numthreads(64, 1, 1)] +void main(uint3 t : SV_DispatchThreadID) { + uint2 at = uint2(t.x & 7, t.x >> 3); + float4 c = 0; + if (how == 0) c = flat_.Load(int3(at, 0)); + if (how == 1) c = layers.Load(int4(at, far, 0)); + if (how == 2) c = volume.Load(int4(at, far, 0)); + if (how == 3) c = layers.SampleLevel(s, float3((at + 0.5) / 64, far), 0); + if (how == 4) c = volume.SampleLevel(s, float3((at + 0.5) / 64, (far + 0.5) / 4096.0), 0); + if (how == 5) c = cube.SampleLevel(s, float4(1, (at + 0.5) / 64, far), 0); + if (how == 6) c = flat_.Load(int3(at, far)); + uint r = (uint)(c.x * 255.0 + 0.5), was; + // the texture's own bytes, zeros, anything else + o.InterlockedAdd(r == 17 ? 0 : r == 0 ? 4 : 8, 1, was); + if (r != 17 && r != 0) o.Store(12, r); +} +"#; + let pass = compile_cs(&gpu.device, SOURCE).expect("the pass must compile"); + // SAFETY: plain creation, each thing in its own part of the heap. + let (_heap, small) = unsafe { + let mut heap: Option = None; + gpu.device + .CreateHeap( + &D3D12_HEAP_DESC { + SizeInBytes: 256 << 20, + Properties: D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() }, + Alignment: 0, + Flags: D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES, + }, + &mut heap, + ) + .unwrap(); + let heap = heap.unwrap(); + let place = |at: u64, size: u64| { + let desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: size, + Height: size as u32, + DepthOrArraySize: 1, + MipLevels: 1, + Format: DXGI_FORMAT(28), + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + ..Default::default() + }; + let mut made: Option = None; + gpu.device + .CreatePlacedResource(&heap, at, &desc, D3D12_RESOURCE_STATE_COPY_DEST, None, &mut made) + .unwrap(); + made.unwrap() + }; + let small = place(0, 64); + // The rest: 34 in every byte. + let mut fill: Vec<(ID3D12Resource, u32, u8)> = vec![(small.clone(), 64, 17)]; + for k in 0..15u64 { + fill.push((place((1 + k) << 24, 2048), 2048, 34)); + } + for k in 1..16u64 { + fill.push((place(k << 20, 512), 512, 34)); + } + for (texture, size, byte) in fill { + let pitch = size * 4; + let (from, data, _) = gpu.shared_buffer(u64::from(pitch) * u64::from(size)); + std::ptr::write_bytes(data, byte, (pitch * size) as usize); + let src = D3D12_TEXTURE_COPY_LOCATION { + pResource: std::mem::transmute_copy(&from), + Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { + PlacedFootprint: D3D12_PLACED_SUBRESOURCE_FOOTPRINT { + Offset: 0, + Footprint: D3D12_SUBRESOURCE_FOOTPRINT { + Format: DXGI_FORMAT(28), + Width: size, + Height: size, + Depth: 1, + RowPitch: pitch, + }, + }, + }, + }; + let dst = D3D12_TEXTURE_COPY_LOCATION { + pResource: std::mem::transmute_copy(&texture), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: 0 }, + }; + gpu.list.CopyTextureRegion(&dst, 0, 0, 0, &src, None); + gpu.list.ResourceBarrier(&[crate::host::transition( + &texture, + D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, + )]); + gpu.submit_and_wait(); + } + (heap, small) + }; + // SAFETY: plain heap creation; slots of it, a live texture. + let heap: ID3D12DescriptorHeap = unsafe { + let heap: ID3D12DescriptorHeap = gpu + .device + .CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC { + Type: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, + NumDescriptors: 4, + Flags: D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, + NodeMask: 0, + }) + .unwrap(); + let step = gpu.device.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) as usize; + for i in 0..4 { + let at = D3D12_CPU_DESCRIPTOR_HANDLE { ptr: heap.GetCPUDescriptorHandleForHeapStart().ptr + i * step }; + // A 2D view of the small texture in every slot. + gpu.device.CreateShaderResourceView(&small, None, at); + } + heap + }; + for (how, name) in + ["2D", "array, loaded", "volume, loaded", "array, sampled", "volume, sampled", "cube array", "2D, a level it has not"] + .iter() + .enumerate() + { + for far in [0u32, 1, 7, 63, 1000, 4000] { + let (_out, out_ptr, out_gpu) = gpu.shared_buffer(1 << 12); + // SAFETY: the mapping; our open list, live objects. + unsafe { + std::ptr::write_bytes(out_ptr, 0, 1 << 12); + gpu.list.SetDescriptorHeaps(&[Some(heap.clone())]); + gpu.list.SetComputeRootSignature(&pass.root); + gpu.list.SetPipelineState(&pass.pso); + gpu.list.SetComputeRoot32BitConstants(0, 2, [how as u32, far].as_ptr().cast(), 0); + gpu.list.SetComputeRootDescriptorTable(1, heap.GetGPUDescriptorHandleForHeapStart()); + gpu.list.SetComputeRootUnorderedAccessView(2, out_gpu); + gpu.list.Dispatch(1, 1, 1); + } + gpu.submit_and_wait(); + // SAFETY: the mapping, the card is done. + let found: Vec = (0..4).map(|i| unsafe { out_ptr.cast::().add(i).read_volatile() }).collect(); + println!("{name} at {far}: its own texels {}, zeros {}, others {} (the last {})", found[0], found[1], found[2], found[3]); + } + } + // SAFETY: a plain query. + println!("device: {:?}", unsafe { gpu.device.GetDeviceRemovedReason() }); + } + /// The clamp compiles with the system's compiler and the host takes it. #[test] fn argument_clamp_builds() {