Преглед изворни кода

The pass over the heap's buffer views is made beside the recording (VICTORY_HEAP_PASS_ASIDE=0: in it)

12 to 17 ms of one recording thread at the frame's first list that needs it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
local пре 2 дана
родитељ
комит
f672130364
3 измењених фајлова са 372 додато и 219 уклоњено
  1. 165 140
      crates/victory-gpu/src/draw.rs
  2. 109 79
      crates/victory-gpu/src/host.rs
  3. 98 0
      crates/victory-gpu/src/host/aside.rs

+ 165 - 140
crates/victory-gpu/src/draw.rs

@@ -678,6 +678,161 @@ fn set(roots: &mut Vec<Root>, slot: u32, root: Root) {
     roots[slot] = root;
 }
 
+/// The pass over the buffer views of `heap` that shaders pick by index
+/// ([`State::heap_buffers`]): their host slots are made, and the host copies
+/// they show are to be refreshed.
+fn heap_buffer_pass(heap: &crate::descriptor::Heap, host: &crate::descriptor::host::HostHeap, frame: u64) -> Vec<crate::host::aside::Refresh> {
+    let started = std::time::Instant::now();
+    // Per host heap: the host copy and range each slot's buffer view
+    // reads, kept from pass to pass. Only the slots written since the
+    // last pass are looked at again (83,000 slots a pass otherwise, 0.1 s
+    // at best); all of them every 16 passes, because a view of a
+    // released resource goes stale without a write, and a slot written
+    // before its memory had a host copy is only made then.
+    type Ranges = HeapRanges<std::sync::Arc<crate::shadow::Shadow>>;
+    static RANGES: std::sync::Mutex<Option<std::collections::HashMap<usize, (u64, Ranges)>>> =
+        std::sync::Mutex::new(None);
+    let mut kept = RANGES.lock().unwrap_or_else(|e| e.into_inner());
+    let (passes, ranges) = kept
+        .get_or_insert_with(Default::default)
+        .entry(std::ptr::from_ref(host) as usize)
+        .or_insert_with(|| (0, HeapRanges(Vec::new())));
+    let changed = host.buffer_changes();
+    let all = *passes == 0 || (*passes % HEAP_ROUND == 0 && !heap_spread());
+    let slots = if all {
+        ranges.0.clear();
+        host.buffer_slots()
+    } else if heap_spread() {
+        // The written ones, and this pass's part of the others.
+        let (part, parts) = ((*passes % HEAP_ROUND) as u32, HEAP_ROUND as u32);
+        ranges.forget_part(part, parts);
+        let mut slots = host.buffer_slots_of(part, parts);
+        slots.extend(changed);
+        slots
+    } else {
+        changed
+    };
+    *passes += 1;
+    // Whether the device was there before the pass: a view that removes
+    // it is named below.
+    static GONE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
+    let mut alive = !GONE.load(Ordering::Relaxed) && crate::descriptor::host::device_removed().is_none();
+    let mut unordered = Vec::new();
+    for (n, &index) in slots.iter().enumerate() {
+        ranges.set(index, None);
+        // A draw's table's: the draw brings what it reads up to date.
+        if host.copied_tabled(index, frame) {
+            continue;
+        }
+        let Some(handle) = heap.handle(index) else {
+            continue;
+        };
+        let Some(view) = host.mirror(index, handle, true) else {
+            continue;
+        };
+        // Every 64 views while the device lives (a cheap query).
+        if alive
+            && n % 64 == 63
+            && let Some(why) = crate::descriptor::host::device_removed()
+        {
+            alive = false;
+            GONE.store(true, Ordering::Relaxed);
+            let last: Vec<String> = slots[n - 63..=n]
+                .iter()
+                .filter_map(|&i| crate::descriptor::view(heap.handle(i)?).map(|v| format!("{i}: {v:?}")))
+                .map(|s| s.chars().take(200).collect())
+                .collect();
+            tracing::error!(why, slot = index, "the host device went while buffer views of the heap were made; one of: {last:#?}");
+            crate::host::log_removal_context();
+        }
+        // The copy the slot shows (`buffer_copy`), not the one the
+        // view's range alone would find.
+        let Some((shadow, address, len)) = crate::descriptor::host::buffer_copy(&view) else {
+            continue;
+        };
+        #[cfg(feature = "diagnostics")]
+        if host.shows(index, &shadow.buffer) == Some(false) {
+            HEAP_VIEWS_ELSEWHERE.fetch_add(1, Ordering::Relaxed);
+        }
+        if crate::picked::on() && view_kind(&view) == Some(ResourceType::Uav) {
+            unordered.push((shadow.clone(), address, address + len));
+        }
+        ranges.set(index, Some((shadow, address, address + len)));
+    }
+    if crate::picked::on() {
+        crate::picked::shown(unordered, all);
+    }
+    #[cfg(feature = "diagnostics")]
+    let looked = std::time::Instant::now();
+    // Per host copy, the span its picked views cover.
+    let mut spans: crate::host::ByCopy<(std::sync::Arc<crate::shadow::Shadow>, u64, u64)> = Default::default();
+    let mut views = 0u32;
+    for (shadow, from, to) in ranges.ranges() {
+        views += 1;
+        let key = std::sync::Arc::as_ptr(shadow) as usize;
+        let span = spans.entry(key).or_insert_with(|| (shadow.clone(), *from, *to));
+        span.1 = span.1.min(*from);
+        span.2 = span.2.max(*to);
+    }
+    drop(kept);
+    let bytes: u64 = spans.values().map(|s| s.2 - s.1).sum();
+    // Memory that is not write-watched is copied whole by every refresh.
+    // It was once refreshed only every 120 frames (unmapped pages read
+    // one by one cost 14 GB a frame in run 11, before they were
+    // remembered): streamed geometry then drew from stale buffers, and
+    // the garbage ended runs (the card stopped answering 14-90 frames
+    // into the opening cutscene; with every frame it ran on). The pass
+    // costs some 30 ms a frame this way.
+    static WHOLE: std::sync::Mutex<Option<std::collections::HashMap<usize, u64>>> = std::sync::Mutex::new(None);
+    let mut whole = WHOLE.lock().unwrap_or_else(|e| e.into_inner());
+    let whole = whole.get_or_insert_with(Default::default);
+    let mut found = Vec::with_capacity(spans.len());
+    for (shadow, from, to) in spans.values() {
+        if shadow.unwatched() {
+            // By the copy's number: a copy made where one let go was
+            // (`shadow::let_go_idle`) has never been refreshed.
+            let key = shadow.id() as usize;
+            // `VICTORY_UNWATCHED_FRAMES` sets the interval (1: every frame).
+            static EVERY: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
+            let every = *EVERY.get_or_init(|| {
+                std::env::var("VICTORY_UNWATCHED_FRAMES")
+                    .ok()
+                    .and_then(|v| v.parse().ok())
+                    .unwrap_or(1)
+            });
+            match whole.get(&key) {
+                Some(&last) if frame < last + every => continue,
+                _ => {
+                    whole.insert(key, frame);
+                }
+            }
+        }
+        found.push((shadow.clone(), *from, (*to).min(*from + shadow.left(*from))));
+    }
+    #[cfg(feature = "diagnostics")]
+    let refreshed = std::time::Instant::now();
+    crate::watch::look_at_holes();
+    #[cfg(feature = "diagnostics")]
+    for (sum, time) in HEAP_SPLIT.iter().zip([looked - started, refreshed - looked]) {
+        sum.fetch_add(time.as_nanos() as u64, Ordering::Relaxed);
+    }
+    static LOGGED: AtomicU64 = AtomicU64::new(0);
+    // The first pass and every growth of the set by half.
+    let seen = LOGGED.load(Ordering::Relaxed);
+    if u64::from(views) > seen + seen / 2 {
+        LOGGED.store(u64::from(views), Ordering::Relaxed);
+        tracing::info!(
+            slots = slots.len(),
+            views,
+            copies = spans.len(),
+            mb = bytes >> 20,
+            ms = started.elapsed().as_millis(),
+            "buffer views of the heap made ready for shaders that pick them by index"
+        );
+    }
+    found
+}
+
 impl State {
     pub(crate) fn reset(&mut self, pipeline: This) {
         *self = State::default();
@@ -1609,155 +1764,25 @@ impl State {
             return Some(gpu);
         }
         let started = std::time::Instant::now();
-        // Per host heap: the host copy and range each slot's buffer view
-        // reads, kept from pass to pass. Only the slots written since the
-        // last pass are looked at again (83,000 slots a pass otherwise, 0.1 s
-        // at best); all of them every 16 passes, because a view of a
-        // released resource goes stale without a write, and a slot written
-        // before its memory had a host copy is only made then.
-        type Ranges = HeapRanges<std::sync::Arc<crate::shadow::Shadow>>;
-        static RANGES: std::sync::Mutex<Option<std::collections::HashMap<usize, (u64, Ranges)>>> =
-            std::sync::Mutex::new(None);
-        let mut kept = RANGES.lock().unwrap_or_else(|e| e.into_inner());
-        let (passes, ranges) = kept
-            .get_or_insert_with(Default::default)
-            .entry(std::ptr::from_ref(host) as usize)
-            .or_insert_with(|| (0, HeapRanges(Vec::new())));
-        let changed = host.buffer_changes();
-        let all = *passes == 0 || (*passes % HEAP_ROUND == 0 && !heap_spread());
-        let slots = if all {
-            ranges.0.clear();
-            host.buffer_slots()
-        } else if heap_spread() {
-            // The written ones, and this pass's part of the others.
-            let (part, parts) = ((*passes % HEAP_ROUND) as u32, HEAP_ROUND as u32);
-            ranges.forget_part(part, parts);
-            let mut slots = host.buffer_slots_of(part, parts);
-            slots.extend(changed);
-            slots
+        if crate::host::aside::on() {
+            let at = std::ptr::from_ref(heap) as usize;
+            list.refreshes_coming(move || {
+                crate::descriptor::with_heap(at, |heap| heap.host().map(|host| heap_buffer_pass(heap, host, frame)))
+                    .flatten()
+                    .unwrap_or_default()
+            });
         } else {
-            changed
-        };
-        *passes += 1;
-        // Whether the device was there before the pass: a view that removes
-        // it is named below.
-        static GONE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
-        let mut alive = !GONE.load(Ordering::Relaxed) && crate::descriptor::host::device_removed().is_none();
-        let mut unordered = Vec::new();
-        for (n, &index) in slots.iter().enumerate() {
-            ranges.set(index, None);
-            // A draw's table's: the draw brings what it reads up to date.
-            if host.copied_tabled(index, frame) {
-                continue;
+            for (shadow, from, to) in heap_buffer_pass(heap, host, frame) {
+                shadow.refresh(list, from, to - from);
             }
-            let Some(handle) = heap.handle(index) else {
-                continue;
-            };
-            let Some(view) = host.mirror(index, handle, true) else {
-                continue;
-            };
-            // Every 64 views while the device lives (a cheap query).
-            if alive
-                && n % 64 == 63
-                && let Some(why) = crate::descriptor::host::device_removed()
-            {
-                alive = false;
-                GONE.store(true, Ordering::Relaxed);
-                let last: Vec<String> = slots[n - 63..=n]
-                    .iter()
-                    .filter_map(|&i| crate::descriptor::view(heap.handle(i)?).map(|v| format!("{i}: {v:?}")))
-                    .map(|s| s.chars().take(200).collect())
-                    .collect();
-                tracing::error!(why, slot = index, "the host device went while buffer views of the heap were made; one of: {last:#?}");
-                crate::host::log_removal_context();
-            }
-            // The copy the slot shows (`buffer_copy`), not the one the
-            // view's range alone would find.
-            let Some((shadow, address, len)) = crate::descriptor::host::buffer_copy(&view) else {
-                continue;
-            };
-            #[cfg(feature = "diagnostics")]
-            if host.shows(index, &shadow.buffer) == Some(false) {
-                HEAP_VIEWS_ELSEWHERE.fetch_add(1, Ordering::Relaxed);
-            }
-            if crate::picked::on() && view_kind(&view) == Some(ResourceType::Uav) {
-                unordered.push((shadow.clone(), address, address + len));
-            }
-            ranges.set(index, Some((shadow, address, address + len)));
-        }
-        if crate::picked::on() {
-            crate::picked::shown(unordered, all);
-        }
-        #[cfg(feature = "diagnostics")]
-        let looked = std::time::Instant::now();
-        // Per host copy, the span its picked views cover.
-        let mut spans: crate::host::ByCopy<(std::sync::Arc<crate::shadow::Shadow>, u64, u64)> = Default::default();
-        let mut views = 0u32;
-        for (shadow, from, to) in ranges.ranges() {
-            views += 1;
-            let key = std::sync::Arc::as_ptr(shadow) as usize;
-            let span = spans.entry(key).or_insert_with(|| (shadow.clone(), *from, *to));
-            span.1 = span.1.min(*from);
-            span.2 = span.2.max(*to);
-        }
-        drop(kept);
-        let bytes: u64 = spans.values().map(|s| s.2 - s.1).sum();
-        // Memory that is not write-watched is copied whole by every refresh.
-        // It was once refreshed only every 120 frames (unmapped pages read
-        // one by one cost 14 GB a frame in run 11, before they were
-        // remembered): streamed geometry then drew from stale buffers, and
-        // the garbage ended runs (the card stopped answering 14-90 frames
-        // into the opening cutscene; with every frame it ran on). The pass
-        // costs some 30 ms a frame this way.
-        static WHOLE: std::sync::Mutex<Option<std::collections::HashMap<usize, u64>>> = std::sync::Mutex::new(None);
-        let mut whole = WHOLE.lock().unwrap_or_else(|e| e.into_inner());
-        let whole = whole.get_or_insert_with(Default::default);
-        for (shadow, from, to) in spans.values() {
-            if shadow.unwatched() {
-                // By the copy's number: a copy made where one let go was
-                // (`shadow::let_go_idle`) has never been refreshed.
-                let key = shadow.id() as usize;
-                // `VICTORY_UNWATCHED_FRAMES` sets the interval (1: every frame).
-                static EVERY: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
-                let every = *EVERY.get_or_init(|| {
-                    std::env::var("VICTORY_UNWATCHED_FRAMES")
-                        .ok()
-                        .and_then(|v| v.parse().ok())
-                        .unwrap_or(1)
-                });
-                match whole.get(&key) {
-                    Some(&last) if frame < last + every => continue,
-                    _ => {
-                        whole.insert(key, frame);
-                    }
-                }
-            }
-            shadow.refresh(list, *from, to - from);
         }
         #[cfg(feature = "diagnostics")]
         let refreshed = std::time::Instant::now();
-        crate::watch::look_at_holes();
         self.heap_textures(list, heap, host);
         #[cfg(feature = "diagnostics")]
-        for (sum, time) in HEAP_SPLIT.iter().zip([looked - started, refreshed - looked, refreshed.elapsed()]) {
-            sum.fetch_add(time.as_nanos() as u64, Ordering::Relaxed);
-        }
+        HEAP_SPLIT[2].fetch_add(refreshed.elapsed().as_nanos() as u64, Ordering::Relaxed);
         HEAP_BUFFER_PASSES.fetch_add(1, Ordering::Relaxed);
         HEAP_BUFFER_MICROS.fetch_add(started.elapsed().as_micros() as u64, Ordering::Relaxed);
-        static LOGGED: AtomicU64 = AtomicU64::new(0);
-        // The first pass and every growth of the set by half.
-        let seen = LOGGED.load(Ordering::Relaxed);
-        if u64::from(views) > seen + seen / 2 {
-            LOGGED.store(u64::from(views), Ordering::Relaxed);
-            tracing::info!(
-                slots = slots.len(),
-                views,
-                copies = spans.len(),
-                mb = bytes >> 20,
-                ms = started.elapsed().as_millis(),
-                "buffer views of the heap made ready for shaders that pick them by index"
-            );
-        }
         Some(gpu)
     }
 

+ 109 - 79
crates/victory-gpu/src/host.rs

@@ -903,6 +903,9 @@ pub(crate) struct HostList {
     /// Scratch buffers of the batched clamps (`crate::indirect::batch`), and
     /// how many batches of this recording have a place in them.
     batch_args: Mutex<(Vec<ID3D12Resource>, u32)>,
+    /// Refreshes of host copies that a thread of ours is still finding, and
+    /// where among the list's steps they belong ([`aside`]).
+    coming: Mutex<Option<(usize, std::sync::Arc<aside::Coming>)>>,
     /// Counts the list in [`ALIVE`] while it lives.
     _alive: Counted,
     /// The texts of the markers `note_op` set, in order (for device-removed
@@ -3154,14 +3157,15 @@ fn late_line(frames: f64) {
     }
 }
 
+pub(crate) mod aside;
 mod card_steps;
 mod place;
 mod send;
+pub(crate) use send::{goes_as_words, words_under};
 mod torn;
 pub(crate) mod upcheck;
 
 #[cfg(test)]
-pub(crate) use send::{goes_as_words, words_under};
 mod late_tests {
     use super::{continues, join_ranges, touches};
 
@@ -3546,6 +3550,7 @@ impl Host {
                 card_budget: OnceLock::new(),
                 indirect_args: Mutex::default(),
                 batch_args: Mutex::default(),
+                coming: Mutex::default(),
                 _alive: Counted::new(),
                 #[cfg(feature = "diagnostics")]
                 notes,
@@ -4830,22 +4835,22 @@ impl Host {
         let mut submitted = self.submitted.lock().unwrap_or_else(|e| e.into_inner());
         let was_working = working(true);
         submitting(1, runs.len());
+        #[cfg(feature = "diagnostics")]
+        let ahead = Split::begin();
+        #[cfg(feature = "diagnostics")]
+        SPLIT[11].fetch_add(called.elapsed().as_nanos() as u64, Ordering::Relaxed);
         // Flights in the air ([`late`]): a submission that is waited for
         // settles them first, one sent ahead only if preparing it reads
         // what they write.
         // Or the reads are done on the card, behind the flights.
         let own = if late { self.card_steps(runs) } else { None };
         #[cfg(feature = "diagnostics")]
-        let ahead = Split::begin();
-        #[cfg(feature = "diagnostics")]
-        SPLIT[11].fetch_add(called.elapsed().as_nanos() as u64, Ordering::Relaxed);
+        ahead.part(12);
         if !late {
             self.settle(&mut submitted, WHY_SYNC);
         } else if own.is_none()
             && let Some(why) = late_blocked(runs)
         {
-        #[cfg(feature = "diagnostics")]
-        ahead.part(12);
             if relay_exact() && why == WHY_READ {
                 // Only until the flights that may write what is read have
                 // landed: their copy-back's end, not the card's other work.
@@ -4856,13 +4861,13 @@ impl Host {
                 self.settle(&mut submitted, why);
             }
         }
+        #[cfg(feature = "diagnostics")]
+        ahead.part(13);
         // Descriptors the title copied into placed heaps directly, before
         // command lists that index those slots are submitted.
         let heaps_synced = crate::descriptor::begin_sync_heaps();
         // That list runs first, and is one of the submission's from here on.
         let with: Vec<&HostList>;
-        #[cfg(feature = "diagnostics")]
-        ahead.part(13);
         let runs = match &own {
             Some(own) => {
                 with = std::iter::once(own.list()).chain(runs.iter().copied()).collect();
@@ -4898,13 +4903,13 @@ impl Host {
         crate::shadow::preparing(this_submission);
         let started = std::time::Instant::now();
         #[cfg(feature = "diagnostics")]
+        ahead.part(14);
+        #[cfg(feature = "diagnostics")]
         let lock = (started - called).as_micros() as u64;
         #[cfg(feature = "diagnostics")]
         crate::copystat::add(crate::copystat::C::Submissions, 1);
         let mut lists: Vec<Option<ID3D12CommandList>> = Vec::with_capacity(2 * runs.len());
         // Where each run's lists start in `lists`.
-        ahead.part(14);
-        #[cfg(feature = "diagnostics")]
         let mut firsts = Vec::with_capacity(runs.len());
         let mut batch = Batch::default();
         #[cfg(feature = "diagnostics")]
@@ -4968,12 +4973,12 @@ impl Host {
             }
             lists.push(list.list.cast().ok());
         }
+        batch.fill();
         batch.flush();
         crate::watch::ask_once_end();
         #[cfg(feature = "diagnostics")]
         split.part(2);
         // The snapshots of the ranges shaders may write, if they were left
-        batch.fill();
         // for now (`crate::shadow::wide_back`): the refreshes are done, the
         // lists have not gone to the card.
         {
@@ -5011,6 +5016,12 @@ impl Host {
         }
         #[cfg(feature = "diagnostics")]
         split.part(4);
+        drop(heaps_synced);
+        // The slots that waited for a texture's host copy, before the
+        // lists that may read them run.
+        crate::descriptor::make_waiting();
+        #[cfg(feature = "diagnostics")]
+        split.part(15);
         let prepared = std::time::Instant::now();
         if late {
             *submitted += 1;
@@ -5027,12 +5038,6 @@ impl Host {
             split.part(5);
             let back = crate::picked::with(declared_writes(runs), &picked.written);
             let writes = runs.iter().flat_map(|l| l.deferred().writes.clone()).collect();
-        drop(heaps_synced);
-        // The slots that waited for a texture's host copy, before the
-        // lists that may read them run.
-        crate::descriptor::make_waiting();
-        #[cfg(feature = "diagnostics")]
-        split.part(15);
             SUBMISSION_US[0].fetch_add((prepared - started).saturating_sub(settled).as_micros() as u64, Ordering::Relaxed);
             submitting(0, 0);
             #[cfg(feature = "diagnostics")]
@@ -5485,6 +5490,7 @@ impl HostList {
         }
         self.batches_common();
         self.card_budget_out();
+        self.take_coming();
         #[cfg(feature = "diagnostics")]
         if let Some((heap, back)) = self.stamps() {
             let count = (self.notes.lock().unwrap_or_else(|e| e.into_inner()).len() as u32).min(STAMPS - 1);
@@ -5502,12 +5508,35 @@ impl HostList {
         }
     }
 
+    /// `work` finds, on a thread of ours, refreshes that belong at this
+    /// place among the list's steps; they are there when the list is closed.
+    pub(crate) fn refreshes_coming(&self, work: impl FnOnce() -> Vec<aside::Refresh> + Send + 'static) {
+        self.take_coming();
+        let at = self.deferred().copies.len();
+        let coming = aside::begin(work);
+        *self.coming.lock().unwrap_or_else(|e| e.into_inner()) = Some((at, coming));
+    }
+
+    /// The refreshes that were still to come are put among the steps.
+    fn take_coming(&self) {
+        let Some((at, coming)) = self.coming.lock().unwrap_or_else(|e| e.into_inner()).take() else {
+            return;
+        };
+        let found = coming.wait();
+        let mut deferred = self.deferred();
+        let at = at.min(deferred.copies.len());
+        deferred
+            .copies
+            .splice(at..at, found.into_iter().map(|(shadow, from, to)| Step::Refresh(shadow, from as usize, (to - from) as usize)));
+    }
+
     /// Only after the list's last submission finished (every one is waited
     /// for).
     pub(crate) fn reset(&self) {
         #[cfg(feature = "diagnostics")]
         let started = std::time::Instant::now();
         self._alive.used();
+        *self.coming.lock().unwrap_or_else(|e| e.into_inner()) = None;
         *self.deferred() = Deferred::default();
         self.states.lock().unwrap_or_else(|e| e.into_inner()).clear();
         self.twins.lock().unwrap_or_else(|e| e.into_inner()).clear();
@@ -6367,6 +6396,7 @@ impl HostList {
                     {
                         copies += 1;
                     }
+                    batch.fill();
                     batch.title_written();
                     copy_mapped(dst as *mut u8, src as *const u8, len, true);
                     // With the submission's one asking: the journal learns it.
@@ -6379,24 +6409,29 @@ impl HostList {
                     (started, len)
                 }
                 &Step::Fill(dst, src, len, _) => {
-                    let started = std::time::Instant::now();
                     #[cfg(feature = "diagnostics")]
                     {
                         copies += 1;
                     }
+                    if wide_fills() {
+                        batch.fills.push((dst, src, len));
+                        bytes += len as u64;
+                        continue;
+                    }
+                    let started = std::time::Instant::now();
                     copy_mapped(dst as *mut u8, src as *const u8, len, false);
                     (started, len)
                 }
                 Step::Placed(p) => {
                     let started = std::time::Instant::now();
                     let free = !touches(air, p.src, p.src + p.len) && !touches(air, p.dst, p.dst + p.len);
+                    batch.fill();
                     if free {
                         batch.title_written();
                     }
                     if let Some(bytes) = p.prepare(free) {
                         batch.placed.push((p.shadow.clone(), p.dst, bytes));
                     }
-                    batch.fill();
                     (started, p.len)
                 }
                 Step::Refresh(shadow, address, len) => {
@@ -6409,23 +6444,17 @@ impl HostList {
                     continue;
                 }
                 Step::Run(work) | Step::Host(work, _) => {
+                    batch.fill();
                     #[cfg(feature = "diagnostics")]
                     let started = std::time::Instant::now();
                     if matches!(step, Step::Run(_)) {
                         batch.title_written();
-                    if wide_fills() {
-                        batch.fills.push((dst, src, len));
-                        bytes += len as u64;
-                        continue;
-                    }
-                    let started = std::time::Instant::now();
                     }
                     work();
                     // It wrote title memory, where is not known here: the
                     // submission's one asking ends, later refreshes ask anew.
                     if matches!(step, Step::Run(_)) {
                         crate::watch::ask_once_end();
-                    batch.fill();
                     }
                     #[cfg(feature = "diagnostics")]
                     {
@@ -6444,7 +6473,6 @@ impl HostList {
                     #[cfg(feature = "diagnostics")]
                     let snapshot = std::time::Instant::now();
                     shadow.writing(*address, *len);
-                    batch.fill();
                     #[cfg(feature = "diagnostics")]
                     {
                         add(C::GpuWrites, 1);
@@ -6548,6 +6576,7 @@ impl Split {
 fn split_line(frames: f64) {
     let ms: Vec<f64> = SPLIT.iter().map(|n| n.swap(0, Ordering::Relaxed) as f64 / 1e6 / frames).collect();
     let f: Vec<f64> = FLUSHES.iter().map(|n| n.swap(0, Ordering::Relaxed) as f64 / frames).collect();
+    let e: Vec<f64> = crate::watch::early_asks().iter().map(|&n| n as f64 / frames).collect();
     let a: Vec<f64> = crate::watch::asks().iter().map(|&n| n as f64 / frames).collect();
     let h: Vec<f64> = crate::draw::HEAP_SPLIT.iter().map(|n| n.swap(0, Ordering::Relaxed) as f64 / 1e6 / frames).collect();
     let s: Vec<f64> = crate::shadow::send_stats().iter().map(|&n| n as f64 / frames).collect();
@@ -6574,6 +6603,13 @@ fn split_line(frames: f64) {
         ms[4],
         ms[5],
         if crate::watch::light() { "on" } else { "off" },
+        ms[11],
+        ms[12],
+        ms[13],
+        ms[14],
+        ms[15],
+        e[0],
+        e[1] / 1e3,
     );
     tracing::info!(
         "asking about title memory per frame, all threads: {:.0} kernel askings over {:.0} MB ({:.1} ms, {:.0} pages of 4 KiB found \
@@ -6588,7 +6624,6 @@ fn split_line(frames: f64) {
         a[10],
         a[9],
         a[2],
-    let e: Vec<f64> = crate::watch::early_asks().iter().map(|&n| n as f64 / frames).collect();
         a[3],
         a[4],
         a[5],
@@ -6601,10 +6636,12 @@ fn split_line(frames: f64) {
         s[3],
     );
     tracing::info!(
-        "the pass over the heap's views per frame: {:.1} ms looking at slots, {:.1} their copies' spans and refreshes, {:.1} textures",
+        "the pass over the heap's views per frame: {:.1} ms looking at slots, {:.1} their copies' spans and refreshes, {:.1} textures; the \
+         first two beside the recording (VICTORY_HEAP_PASS_ASIDE), {:.1} ms waited for at the lists' close",
         h[0],
         h[1],
         h[2],
+        aside::WAITED.swap(0, Ordering::Relaxed) as f64 / 1e3 / frames,
     );
 }
 
@@ -6613,13 +6650,6 @@ fn split_line(frames: f64) {
 ///
 /// A submission brings some sixty copies up to date, one after the other
 /// on the submitting thread: for each, the pages the title wrote are asked
-        ms[11],
-        ms[12],
-        ms[13],
-        ms[14],
-        ms[15],
-        e[0],
-        e[1] / 1e3,
 /// for and read through the kernel into the copy. That is the frame line's
 /// "copies", 45-50 ms a frame, and the copies have nothing to do with one
 /// another: each refresh writes its own copy and its own notes, and reads
@@ -6685,6 +6715,22 @@ struct Batch {
     /// What copies at their place in the submission's lists put into title
     /// memory ([`place`]): the host copy, the title address, the bytes.
     placed: Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, Vec<u8>)>,
+    /// Fills not done yet (destination, source, bytes): [`wide_fills`].
+    fills: Vec<(usize, usize, usize)>,
+}
+
+/// Whether the fills of a submission's upload memory are done together on
+/// several threads (`VICTORY_WIDE_FILLS=0`: one after the other, where the
+/// lists name them).
+///
+/// They are 7,000 to 12,000 a frame, some 500 bytes each, 5 ms of the
+/// submitting thread. Each reads title memory and writes memory of its own
+/// that the card alone reads, when the lists run; they are done before the
+/// next step that writes title memory or runs code of ours, and before the
+/// submission goes on from its steps.
+fn wide_fills() -> bool {
+    static ON: OnceLock<bool> = OnceLock::new();
+    *ON.get_or_init(|| std::env::var_os("VICTORY_WIDE_FILLS").is_none_or(|v| v != "0") && wide_refresh())
 }
 
 /// Whether a submission's textures are brought up to date on several
@@ -6715,22 +6761,6 @@ impl Batch {
                 (r.1, r.2) = (r.1.min(address), r.2.max(end));
             }
             None => {
-    /// Fills not done yet (destination, source, bytes): [`wide_fills`].
-    fills: Vec<(usize, usize, usize)>,
-}
-
-/// Whether the fills of a submission's upload memory are done together on
-/// several threads (`VICTORY_WIDE_FILLS=0`: one after the other, where the
-/// lists name them).
-///
-/// They are 7,000 to 12,000 a frame, some 500 bytes each, 5 ms of the
-/// submitting thread. Each reads title memory and writes memory of its own
-/// that the card alone reads, when the lists run; they are done before the
-/// next step that writes title memory or runs code of ours, and before the
-/// submission goes on from its steps.
-fn wide_fills() -> bool {
-    static ON: OnceLock<bool> = OnceLock::new();
-    *ON.get_or_init(|| std::env::var_os("VICTORY_WIDE_FILLS").is_none_or(|v| v != "0") && wide_refresh())
                 self.index.insert(key, self.refreshes.len());
                 self.refreshes.push((shadow.clone(), address, end));
             }
@@ -6786,6 +6816,33 @@ fn wide_fills() -> bool {
         self.textures.clear();
     }
 
+    /// The fills noted are done.
+    fn fill(&mut self) {
+        // Below this many the threads cost more than they save.
+        const FEW: usize = 96;
+        const PART: usize = 64;
+        if self.fills.is_empty() {
+            return;
+        }
+        let started = std::time::Instant::now();
+        if self.fills.len() < FEW {
+            for (dst, src, len) in self.fills.drain(..) {
+                copy_mapped(dst as *mut u8, src as *const u8, len, false);
+            }
+        } else {
+            let fills = std::mem::take(&mut self.fills);
+            crate::shadow::wide_each(fills.len().div_ceil(PART), move |i| {
+                for &(dst, src, len) in fills.iter().skip(i * PART).take(PART) {
+                    copy_mapped(dst as *mut u8, src as *const u8, len, false);
+                }
+            });
+        }
+        let took = started.elapsed();
+        PREPARING[0].fetch_add(took.as_micros() as u64, Ordering::Relaxed);
+        #[cfg(feature = "diagnostics")]
+        SPLIT[7].fetch_add(took.as_nanos() as u64, Ordering::Relaxed);
+    }
+
     /// The textures due are brought up to date.
     fn refresh_textures(&mut self) {
         if self.due.len() < 2 || !wide_textures() {
@@ -6816,33 +6873,6 @@ fn wide_fills() -> bool {
             FLUSHES[0].fetch_add(1, Ordering::Relaxed);
             FLUSHES[1].fetch_add(all as u64, Ordering::Relaxed);
         }
-    /// The fills noted are done.
-    fn fill(&mut self) {
-        // Below this many the threads cost more than they save.
-        const FEW: usize = 96;
-        const PART: usize = 64;
-        if self.fills.is_empty() {
-            return;
-        }
-        let started = std::time::Instant::now();
-        if self.fills.len() < FEW {
-            for (dst, src, len) in self.fills.drain(..) {
-                copy_mapped(dst as *mut u8, src as *const u8, len, false);
-            }
-        } else {
-            let fills = std::mem::take(&mut self.fills);
-            crate::shadow::wide_each(fills.len().div_ceil(PART), move |i| {
-                for &(dst, src, len) in fills.iter().skip(i * PART).take(PART) {
-                    copy_mapped(dst as *mut u8, src as *const u8, len, false);
-                }
-            });
-        }
-        let took = started.elapsed();
-        PREPARING[0].fetch_add(took.as_micros() as u64, Ordering::Relaxed);
-        #[cfg(feature = "diagnostics")]
-        SPLIT[7].fetch_add(took.as_nanos() as u64, Ordering::Relaxed);
-    }
-
     }
 
     fn flush_all(&mut self) {

+ 98 - 0
crates/victory-gpu/src/host/aside.rs

@@ -0,0 +1,98 @@
+//! Work for a list that a thread of ours does while the title records on:
+//! the pass over the heap's buffer views (`crate::draw`), which took 12 to 17
+//! ms of one recording thread at the frame's first list that needs it, with
+//! the submission of that list, and of those behind it, waiting.
+//! `VICTORY_HEAP_PASS_ASIDE=0`: done where it is needed, as before.
+//!
+//! What it finds are refreshes of host copies, which are steps of the list
+//! for its submission, and what it makes are descriptors, which the card
+//! reads when the list runs: both are there when the list is closed
+//! (`HostList::close` waits), and the refreshes stand at the place in the
+//! list's steps where the pass was begun.
+
+use super::*;
+
+/// A host copy, and the title memory of it (from, to).
+pub(crate) type Refresh = (std::sync::Arc<crate::shadow::Shadow>, u64, u64);
+
+/// What is being found, until it is.
+pub(crate) struct Coming(Mutex<Option<Vec<Refresh>>>, std::sync::Condvar);
+
+/// Since the last frame line: microseconds lists waited at their close.
+pub(crate) static WAITED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
+
+impl Coming {
+    /// What was found.
+    pub(crate) fn wait(&self) -> Vec<Refresh> {
+        let began = std::time::Instant::now();
+        let mut found = self.0.lock().unwrap_or_else(|e| e.into_inner());
+        loop {
+            if let Some(found) = found.take() {
+                WAITED.fetch_add(began.elapsed().as_micros() as u64, Ordering::Relaxed);
+                return found;
+            }
+            found = self.1.wait(found).unwrap_or_else(|e| e.into_inner());
+        }
+    }
+
+    fn found(&self, what: Vec<Refresh>) {
+        *self.0.lock().unwrap_or_else(|e| e.into_inner()) = Some(what);
+        self.1.notify_all();
+    }
+}
+
+pub(crate) fn on() -> bool {
+    static ON: OnceLock<bool> = OnceLock::new();
+    *ON.get_or_init(|| std::env::var_os("VICTORY_HEAP_PASS_ASIDE").is_none_or(|v| v != "0"))
+}
+
+type Job = Box<dyn FnOnce() + Send>;
+
+/// `work` begins on the thread for it, one after another; here and now if
+/// there is no such thread.
+pub(crate) fn begin(work: impl FnOnce() -> Vec<Refresh> + Send + 'static) -> std::sync::Arc<Coming> {
+    static THREAD: OnceLock<Option<Mutex<std::sync::mpsc::Sender<Job>>>> = OnceLock::new();
+    let coming = std::sync::Arc::new(Coming(Mutex::new(None), std::sync::Condvar::new()));
+    let told = coming.clone();
+    let job: Job = Box::new(move || {
+        // (However it ends, the list that waits goes on.)
+        let found = std::panic::catch_unwind(std::panic::AssertUnwindSafe(work)).unwrap_or_default();
+        told.found(found);
+    });
+    let thread = THREAD.get_or_init(|| {
+        let (send, jobs) = std::sync::mpsc::channel::<Job>();
+        std::thread::Builder::new()
+            .name("victory-aside".into())
+            .spawn(move || jobs.into_iter().for_each(|job| job()))
+            .ok()
+            .map(|_| Mutex::new(send))
+    });
+    match thread {
+        Some(send) => {
+            if let Err(job) = send.lock().unwrap_or_else(|e| e.into_inner()).send(job) {
+                (job.0)();
+            }
+        }
+        None => job(),
+    }
+    coming
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn what_is_found_is_waited_for_and_a_panic_lets_the_list_go_on() {
+        let (go, wait) = std::sync::mpsc::channel::<()>();
+        let coming = begin(move || {
+            let _ = wait.recv();
+            Vec::new()
+        });
+        assert!(coming.0.lock().unwrap().is_none());
+        go.send(()).unwrap();
+        assert!(coming.wait().is_empty());
+        assert!(begin(|| panic!("a panic of the work")).wait().is_empty());
+        assert!(begin(Vec::new).wait().is_empty());
+    }
+}