Browse Source

Fewer empty staging blocks kept; the raw twins made are counted

Run crash-1 held 5.2 GB of staging blocks (n02: 0.9): at most 8 empty ones
stay (512 MB of the PC's memory). With the frame line: raw twins made per
frame, and those that had to be views as the title's are.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
local 2 ngày trước cách đây
mục cha
commit
d77556c791

+ 8 - 0
crates/victory-gpu/src/descriptor.rs

@@ -286,6 +286,14 @@ 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));
+    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)",
+            twins as f64 / frames,
+            as_is 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));
     if pages == 0 {
         return;

+ 1 - 1
crates/victory-gpu/src/descriptor/host.rs

@@ -2542,7 +2542,7 @@ 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 start, and those of them that had
+/// 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];
 

+ 13 - 9
crates/victory-gpu/src/pool.rs

@@ -31,6 +31,9 @@ const BLOCK: u64 = 64 << 20;
 const SPARE: usize = 2;
 /// The same while the card has room ([`room`]): 4 GiB of them.
 const SPARE_KEPT: usize = 64;
+/// The same of blocks of staging buffers, which are the PC's memory: run
+/// crash-1 held 5.2 GB of them.
+const STAGING_KEPT: usize = 8;
 /// How many are kept now.
 static SPARES: AtomicUsize = AtomicUsize::new(SPARE);
 
@@ -125,13 +128,14 @@ struct Pool<B> {
     blocks: Vec<Option<Block<B>>>,
     /// Pieces given back, and when each may be used again.
     resting: VecDeque<(u64, Piece)>,
-    /// The empty blocks it was last told to keep.
+    /// The empty blocks it was last told to keep, and the most it keeps.
     spare: usize,
+    most: usize,
 }
 
 impl<B: Clone> Pool<B> {
-    const fn new() -> Pool<B> {
-        Pool { blocks: Vec::new(), resting: VecDeque::new(), spare: 0 }
+    const fn new(most: usize) -> Pool<B> {
+        Pool { blocks: Vec::new(), resting: VecDeque::new(), spare: 0, most }
     }
 
     /// A piece of `len` bytes and the memory it lies in, and whether that
@@ -190,7 +194,7 @@ impl<B: Clone> Pool<B> {
             }
         }
         // (Without a piece given back too, once there is less room.)
-        let most = SPARES.load(Ordering::Relaxed);
+        let most = SPARES.load(Ordering::Relaxed).min(self.most);
         if any || most < self.spare {
             self.spare = most;
             let mut spare = 0;
@@ -216,8 +220,8 @@ struct Upload(ID3D12Resource, *mut u8);
 // SAFETY: the mapping is written by the owners of its pieces, each its own.
 unsafe impl Send for Upload {}
 
-static TEXTURES: Mutex<Pool<ID3D12Heap>> = Mutex::new(Pool::new());
-static STAGING: Mutex<Pool<Upload>> = Mutex::new(Pool::new());
+static TEXTURES: Mutex<Pool<ID3D12Heap>> = Mutex::new(Pool::new(SPARE_KEPT));
+static STAGING: Mutex<Pool<Upload>> = Mutex::new(Pool::new(STAGING_KEPT));
 
 fn lock<T>(m: &'static Mutex<T>) -> std::sync::MutexGuard<'static, T> {
     m.lock().unwrap_or_else(|e| e.into_inner())
@@ -413,7 +417,7 @@ mod tests {
 
     #[test]
     fn pieces_never_overlap_and_rest_before_they_are_used_again() {
-        let mut pool: Pool<u32> = Pool::new();
+        let mut pool: Pool<u32> = Pool::new(SPARE);
         let made = std::cell::Cell::new(0);
         let make = || {
             made.set(made.get() + 1);
@@ -440,10 +444,10 @@ mod tests {
             pool.give(p, 20);
         }
         pool.rested(20 + REST);
-        assert_eq!(pool.blocks(), blocks.min(SPARES.load(Ordering::Relaxed)));
+        assert_eq!(pool.blocks(), blocks.min(SPARE));
         // Too big for a block, and a block that cannot be made.
         assert!(pool.take(BLOCK, 65536, 30, make).is_none());
-        let mut none: Pool<u32> = Pool::new();
+        let mut none: Pool<u32> = Pool::new(SPARE);
         assert!(none.take(65536, 65536, 0, || None).is_none());
     }