diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index 09845f6..925ed16 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 => { @@ -2022,6 +2081,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.