diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index cf0d5e2..c9b2d15 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -1086,6 +1086,11 @@ struct Views { /// The shapes of views of placed memory, and where each is. shapes: Vec, shape_index: std::collections::BTreeMap, + /// Entries of views that no resource releases (constant buffer views + /// and views of placed memory that did not fit a record), oldest first, + /// with the generation each was made at: past [`ownerless_cap`] the + /// oldest is freed, so they cannot grow for the whole run. + ownerless: std::collections::VecDeque<(u32, u32)>, } /// Read far more often than written: every slot a draw reads through a @@ -1099,7 +1104,27 @@ static VIEWS: std::sync::RwLock = std::sync::RwLock::new(Views { free: Vec::new(), shapes: Vec::new(), shape_index: std::collections::BTreeMap::new(), + ownerless: std::collections::VecDeque::new(), }); + +/// How many table entries of views without a resource are kept +/// ([`Views::ownerless`]). Once the shapes of placed views are full (the +/// title makes far more of them than it has kinds of textures: 4,194,304 +/// after half an hour of play), every placed view it writes becomes such an +/// entry, some 15,000 a second: kept for the run they took 16.7 GB after 70 +/// minutes and the frame rate fell by half. A descriptor names the view it +/// was written with for a frame or a few, so the oldest of a million are +/// long out of use; one naming a freed entry reads as stale (no view). +/// `VICTORY_OWNERLESS_VIEWS=` keeps `n` (0: all, as before). +fn ownerless_cap() -> usize { + static CAP: std::sync::OnceLock = std::sync::OnceLock::new(); + *CAP.get_or_init(|| { + std::env::var("VICTORY_OWNERLESS_VIEWS") + .ok() + .and_then(|v| v.trim().parse().ok()) + .unwrap_or(1 << 20) + }) +} const VIEW_TAG: u32 = u32::from_le_bytes(*b"vVIE"); /// A record is the tag and two words, `i` and `generation` (the host keeps @@ -1228,6 +1253,40 @@ impl Views { /// The record words of `view` as a table entry: its entry (a new or a /// freed one if the view is not there yet) and that entry's generation. fn entry(&mut self, view: View) -> [u32; 2] { + self.entry_kept(view, ownerless_cap()) + } + + /// [`entry`](Self::entry), keeping at most `cap` entries of views + /// without a resource (0: all). + fn entry_kept(&mut self, view: View, cap: usize) -> [u32; 2] { + let ownerless = view.resources() == [0, 0] && matches!(view, View::Constant(..) | View::Placed(..)); + let fresh = !self.index.contains_key(&view); + let words = self.entry_made(view); + if ownerless && fresh { + self.ownerless.push_back((words[0], words[1])); + while cap != 0 && self.ownerless.len() > cap { + let (i, generation) = self.ownerless.pop_front().expect("longer than cap"); + self.free_ownerless(i, generation); + } + } + words + } + + /// Frees entry `i` if it still holds the view it had at `generation` + /// (it may have been freed and made again for another view since). + fn free_ownerless(&mut self, i: u32, generation: u32) { + let Some((view, current)) = self.list.get(i as usize).cloned() else { + return; + }; + if current != generation || self.index.get(&view) != Some(&i) { + return; + } + self.index.remove(&view); + self.list[i as usize].1 = ((generation + 1) & GENERATIONS).max(1); + self.free.push(i); + } + + fn entry_made(&mut self, view: View) -> [u32; 2] { let i = match self.index.get(&view) { Some(&i) => i, None => { @@ -1575,6 +1634,56 @@ pub(crate) extern "system" fn create_uav( record(handle, view); } +/// Copy only the fields of a placed resource description. Padding can differ +/// between two views of the same texture and the caller may leave it unwritten. +fn placed_resource_words(desc: *const [u32; 14]) -> Option<[u32; 14]> { + if desc.is_null() { + return None; + } + let src = desc.cast::(); + let mut fields = [0; 14]; + // SAFETY: the caller supplies a 56-byte ResourceDesc, possibly unaligned. + // Dimension occupies word 0. Alignment through Flags occupy words 2..13. + unsafe { + fields[0] = src.read_unaligned(); + fields[2..13].copy_from_slice(&src.add(2).cast::<[u32; 11]>().read_unaligned()); + } + Some(fields) +} + +/// The words of a placed view's description with those its kind does not +/// use set to zero. The first ten words are the PC's view description: for a +/// shader resource view, the format, the dimension, the component mapping, +/// a word of padding (the union that follows starts on 8 bytes) and the +/// union's six words; for an unordered access view, the format, the +/// dimension and the union's eight words. Each dimension uses only the head +/// of the union, and the title leaves the rest unwritten: run vt-shapes saw +/// 131,392 values in word 3 and 36,413 in word 9 of a million shapes, each +/// a new shape for the same view. Words 10 and on (the address and the +/// console's own fields) are kept; an unknown dimension keeps all. +fn placed_view_words(desc: *const [u32; 16], unordered: bool) -> Option<[u32; 16]> { + let mut d = words(desc)?; + // Words of the union each dimension uses (D3D12_BUFFER_SRV and the like). + let (start, used) = if unordered { + // D3D12_UAV_DIMENSION: buffer, 1D, 1D array, 2D, 2D array, 2D MS, + // 2D MS array, 3D. + const USED: [usize; 9] = [0, 7, 1, 3, 2, 4, 0, 2, 3]; + (2, USED.get(d[1] as usize).copied()) + } else { + // D3D12_SRV_DIMENSION: buffer, 1D, 1D array, 2D, 2D array, 2D MS, + // 2D MS array, 3D, cube, cube array, acceleration structure. + const USED: [usize; 12] = [0, 5, 3, 5, 4, 6, 0, 2, 3, 3, 5, 2]; + (4, USED.get(d[1] as usize).copied()) + }; + if let Some(used) = used.filter(|_| d[1] != 0) { + if !unordered { + d[3] = 0; + } + d[start + used..10].fill(0); + } + Some(d) +} + /// Device methods 0x230 and 0x238: shader resource and unordered access /// views of memory the title placed itself, `(resource desc, view desc, /// handle)`. @@ -1584,7 +1693,7 @@ pub(crate) extern "system" fn create_placed_srv( desc: *const [u32; 16], handle: u64, ) { - if let (Some(resource), Some(desc)) = (words(resource), words(desc)) { + if let (Some(resource), Some(desc)) = (placed_resource_words(resource), placed_view_words(desc, false)) { record(handle, View::Placed(false, resource, desc)); } } @@ -1595,7 +1704,7 @@ pub(crate) extern "system" fn create_placed_uav( desc: *const [u32; 16], handle: u64, ) { - if let (Some(resource), Some(desc)) = (words(resource), words(desc)) { + if let (Some(resource), Some(desc)) = (placed_resource_words(resource), placed_view_words(desc, true)) { record(handle, View::Placed(true, resource, desc)); } } @@ -1920,6 +2029,76 @@ mod tests { unsafe { VirtualFree(memory, 0, MEM_RELEASE) }; } + /// Two placed views alike but for the words their kind does not use + /// (the padding after the component mapping, the union's unused tail) + /// are one record; a used word still tells them apart. + #[test] + fn placed_views_ignore_unused_view_words() { + let mut slot = [0u32; 8]; + let handle = slot.as_mut_ptr() as u64; + let null = std::ptr::null(); + let resource = [1, 0, 0, 0, 0x4000, 0, 1, 0x10001, 0, 1, 0, 1, 0, 0]; + // A structured buffer view: words 4-5 first element, 6 count, 7 + // stride, 8 flags; 3 and 9 are not used. + let srv = [0, 1, 0x1688, 0, 16, 0, 64, 16, 0, 0, 0x1000, 0x6, 0, 0, 0, 0]; + // An unordered buffer view: words 2-3 first element, 4 count, 5 + // stride, 6-7 counter offset, 8 flags; 9 is not used. + let uav = [0, 1, 16, 0, 64, 16, 0, 0, 0, 0, 0x1000, 0x6, 0, 0, 0, 0]; + for (unordered, create, desc, unused, used) in [ + (false, create_placed_srv as extern "system" fn(_, _, _, _), srv, [3usize, 9], 6usize), + (true, create_placed_uav, uav, [9, 9], 4), + ] { + create(null, &resource, &desc, handle); + let first = slot; + assert_eq!(view(handle), Some(View::Placed(unordered, resource, desc))); + let mut noisy = desc; + for w in unused { + noisy[w] = 0xdead_beef; + } + create(null, &resource, &noisy, handle); + assert_eq!(slot, first, "unused words left out"); + let mut other = desc; + other[used] += 1; + create(null, &resource, &other, handle); + assert_ne!(slot, first, "a used word counts"); + } + // A 2D texture view uses four words of the union (4-7): 8 and 9 go. + let mut tex = [28, 4, 0x1688, 0x77, 0, 1, 0, 0, 0x55, 0x66, 0x2000, 0x6, 0, 0, 0, 0]; + create_placed_srv(null, &resource, &tex, handle); + tex[3] = 0; + (tex[8], tex[9]) = (0, 0); + assert_eq!(view(handle), Some(View::Placed(false, resource, tex))); + } + + #[test] + fn placed_views_share_resources_across_description_padding() { + let mut slot = [0u32; 8]; + let handle = slot.as_mut_ptr() as u64; + let view_desc = [0u32; 16]; + let null = std::ptr::null(); + let mut resource = [3, 0, 0, 0, 8, 0, 8, 0x10001, 28, 1, 0, 0, 0, 0]; + let expected = resource; + for (unordered, create) in [ + (false, create_placed_srv as extern "system" fn(_, _, _, _)), + (true, create_placed_uav), + ] { + create(null, &expected, &view_desc, handle); + let first = slot; + resource[1] = 0xaaaa_aaaa; + resource[13] = 0xbbbb_bbbb; + create(null, &resource, &view_desc, handle); + assert_eq!(slot, first); + assert_eq!( + view(handle), + Some(View::Placed(unordered, expected, view_desc)) + ); + let mut other = resource; + other[12] = 4; + create(null, &other, &view_desc, handle); + assert_ne!(slot, first); + } + } + #[test] fn views_survive_copies() { let null = std::ptr::null(); @@ -1976,6 +2155,50 @@ mod tests { assert_ne!(constant_words(0x1000, 1 << 14), constant_words(0x1000, 64)); } + /// Entries of views no resource releases stay under their cap: the + /// oldest go stale, the newest still name their views, entries are + /// reused rather than added, and views of resources are never dropped. + #[test] + fn ownerless_entries_are_capped() { + let mut views = Views { + list: Vec::new(), + index: std::collections::BTreeMap::new(), + by_resource: std::collections::BTreeMap::new(), + free: Vec::new(), + shapes: Vec::new(), + shape_index: std::collections::BTreeMap::new(), + ownerless: std::collections::VecDeque::new(), + }; + let placed = |n: u32| View::Placed(false, [n; 14], [n; 16]); + let named = |views: &Views, words: [u32; 2], view: &View| { + views.list.get(words[0] as usize).is_some_and(|(v, g)| *g == words[1] && v == view) + }; + let words: Vec<[u32; 2]> = (0..10).map(|n| views.entry_kept(placed(n), 4)).collect(); + assert_eq!(views.index.len(), 4, "four kept"); + assert!(views.list.len() <= 5, "entries reused, not added: {}", views.list.len()); + for n in 0..6 { + assert!(!named(&views, words[n], &placed(n as u32)), "entry {n} stale"); + } + for n in 6..10 { + assert!(named(&views, words[n], &placed(n as u32)), "entry {n} kept"); + } + // The same view written again keeps its words. + assert_eq!(views.entry_kept(placed(9), 4), words[9]); + // A view of a resource is freed with the resource only. + let shader = View::Shader(0x1234, None); + let kept = views.entry_kept(shader.clone(), 4); + for n in 10..40 { + views.entry_kept(placed(n), 4); + } + assert!(named(&views, kept, &shader)); + assert!(views.list.len() <= 6); + // 0 keeps them all. + for n in 40..60 { + views.entry_kept(placed(n), 0); + } + assert_eq!(views.index.len(), 1 + 4 + 20); + } + /// The leak of run 65: a title that writes constant buffer views and /// views of placed memory at ever new addresses adds nothing to the /// table; one shape stands for all the placed views alike. @@ -1987,8 +2210,9 @@ mod tests { let null = std::ptr::null(); let slots = [0u64; 4]; let at = slots.as_ptr() as u64; - // A resource description and a view no other test uses. - let resource: [u32; 14] = std::array::from_fn(|k| 0x5ee0_0000 + k as u32); + // A resource description and a view no other test uses; words 1 and + // 13 are the description's padding, which placed views keep as zero. + let resource: [u32; 14] = std::array::from_fn(|k| if k == 1 || k == 13 { 0 } else { 0x5ee0_0000 + k as u32 }); let mut desc: [u32; 16] = std::array::from_fn(|k| 0x7a70_0000 + k as u32); let mine = |v: &View| match v { View::Constant(address, _) => (0x6_0000_0000..0x6_1000_0000).contains(address), diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index ab2931a..2f0395d 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -121,6 +121,9 @@ pub(crate) struct AllocationInfo { const _: () = assert!(size_of::() == 20); const _: () = assert!(size_of::() == 48); +// placed_resource_words copies the two field spans on either side of the padding. +const _: () = assert!(std::mem::offset_of!(ResourceDesc, alignment) == 8); +const _: () = assert!(std::mem::offset_of!(ResourceDesc, flags) == 48); const _: () = assert!(size_of::() == 56); const BUFFER: u32 = 1;