diff --git a/crates/victory-gpu/src/descriptor.rs b/crates/victory-gpu/src/descriptor.rs index ab22f0751..476daa24a 100644 --- a/crates/victory-gpu/src/descriptor.rs +++ b/crates/victory-gpu/src/descriptor.rs @@ -171,6 +171,140 @@ fn sync_on() -> bool { *ON.get_or_init(|| std::env::var_os("VICTORY_HEAP_SYNC").is_none_or(|v| v != "0")) } +/// How [`sync_heaps`] keeps a heap the write watch has no tracker for, by +/// renderer port #2's marks alone (`VICTORY_HEAP_SYNC_MARKS=1`, the default: +/// [`Marks::Textures`]; `all`: [`Marks::All`]; `0`: such a heap is left out, as +/// before; A/B-able). +/// +/// Under Wine with renderer port #1 the title's view heap lies in the +/// graphics pages shown as private memory at home (`VICTORY_PORT_HOME`), +/// which neither the kernel's watch nor the guards cover: [`Heap::written`] +/// is `None`, and the heap was passed over without a word, so no record the +/// title stored as bytes reached the host heap but through a draw's own table. +/// A slot picked by index went on showing what it showed before, after the +/// thing it showed was gone too. What was seen of it: all plants missing for a +/// frame, 1.3 frames in 1,000, and at night the picture pulsed darker after it +/// (the street lights behind them, and the exposure). +/// +/// The textures are what it takes: with the records of textures alone made +/// none of 3,303 frames had the gap, with the others alone 2 of 3,370 and 6 of +/// 3,579 (runs o2a, o8a, k1a). They are 2,000 records a frame of 127,000 +/// marked; all of them cost the submitting thread twice the wait (3.2 against +/// 1.6 ms a frame, runs relA, relT). +#[cfg(windows)] +#[derive(Clone, Copy, PartialEq)] +enum Marks { + Off, + Textures, + All, +} + +#[cfg(windows)] +fn marks_alone() -> Marks { + static SET: std::sync::OnceLock = std::sync::OnceLock::new(); + let set = *SET.get_or_init(|| match std::env::var("VICTORY_HEAP_SYNC_MARKS").ok().as_deref() { + Some("0") => Marks::Off, + Some("all") => Marks::All, + _ => Marks::Textures, + }); + #[cfg(feature = "diagnostics")] + match crate::ab::pick("VICTORY_HEAP_SYNC_MARKS") { + Some(false) => return Marks::Off, + Some(true) if set == Marks::Off => return Marks::Textures, + _ => {} + } + set +} + +/// [`Marks::Textures`]: of the marked slots of the heap at `start` (and of the +/// pages of port #2's fallback), those that now hold a view of a texture +/// object, or of an object that is gone, are made. The others are a draw's own +/// (made when it is recorded), buffers (made for draws only) or no views. +/// Adds to `(slots looked at, records made, left to draws, pages of the fallback)`. +#[cfg(windows)] +fn sync_textures(host: &host::HostHeap, start: u64, count: u32, frame: u64, sums: &mut [u64; 4]) { + use std::sync::atomic::Ordering::Relaxed; + const STEP: u64 = 32; + /// Records read at a time. + const AT_A_TIME: u32 = 64; + /// Slots a job. + const JOB: u32 = 8192; + let mut runs: Vec<(u32, u32)> = crate::port_descriptors::take(start).unwrap_or_default(); + let told = runs.len(); + if crate::port_descriptors::ask_due(frame) { + crate::port_descriptors::asked(&[(start as usize, (start + u64::from(count) * STEP) as usize)]); + } + for page in crate::port_descriptors::to_check(frame) { + let first = (page as u64).saturating_sub(start) / STEP; + runs.push((first as u32, (crate::watch::PAGE as u64 / STEP) as u32)); + } + sums[3] += (runs.len() - told) as u64; + // Jobs of about as many slots each. + let mut jobs: Vec> = Vec::new(); + let (mut from, mut slots) = (0, 0); + for (n, &(_, len)) in runs.iter().enumerate() { + slots += len; + if slots >= JOB { + jobs.push(from..n + 1); + (from, slots) = (n + 1, 0); + } + } + if from < runs.len() { + jobs.push(from..runs.len()); + } + let totals = [const { std::sync::atomic::AtomicU64::new(0) }; 3]; + let runs = &runs; + crate::land::each_view(true, jobs.len(), &|n| { + let mut buffer = [[0u32; STEP as usize / 4]; AT_A_TIME as usize]; + let (mut looked, mut made, mut left) = (0u64, 0u64, 0u64); + for &(first, len) in &runs[jobs[n].clone()] { + let end = first.saturating_add(len).min(count); + let mut index = first; + while index < end { + // A draw's own table: the draw makes what it reads of it. + if host.copied_tabled(index, frame) { + (looked, left) = (looked + 1, left + 1); + index += 1; + continue; + } + let mut n = 1; + while n < AT_A_TIME.min(end - index) && !host.copied_tabled(index + n, frame) { + n += 1; + } + looked += u64::from(n); + let at = start + u64::from(index) * STEP; + // SAFETY: the title's records, read by a copy that fails where + // a page is not there, into this thread's own buffer. + let whole = unsafe { + crate::safecopy::copy(buffer.as_mut_ptr().cast(), at as *const u8, n as usize * STEP as usize) + }; + for (i, record) in buffer[..n as usize].iter().enumerate().filter(|_| whole) { + let [tag, entry, generation, ..] = *record; + if tag != VIEW_TAG || generation >> KIND_SHIFT != 0 { + continue; + } + let wanted = match view_at(entry, generation) { + Some(View::Shader(resource, _)) => crate::resource::buffer_bytes(resource as This).is_none(), + Some(_) => false, + None => !host.shows_none(index + i as u32), + }; + if wanted && !host.shows_record(index + i as u32, entry, generation) { + made += 1; + host.mirror(index + i as u32, at + i as u64 * STEP, false); + } + } + index += n; + } + } + for (to, n) in totals.iter().zip([looked, made, left]) { + to.fetch_add(n, Relaxed); + } + }); + for (to, n) in sums.iter_mut().zip(&totals) { + *to += n.load(Relaxed); + } +} + /// Whether a submission's [`sync_heaps`] is done by a thread of its own /// beside the rest of the preparing (`VICTORY_HEAP_SYNC_ASIDE=0`: in front of /// it, as before). @@ -389,11 +523,33 @@ pub(crate) fn sync_heaps() { for &(start, end, at) in heaps.iter() { // SAFETY: registered heaps are live (removed when dropped). let heap = unsafe { &*(at as *const Heap) }; - let (Some(Some(host)), Some(written), Some(step)) = - (heap.host.get(), &heap.written, increment(heap.desc.kind)) - else { + let (Some(Some(host)), Some(step)) = (heap.host.get(), increment(heap.desc.kind)) else { continue; }; + // No write watch for the heap's memory ([`marks_alone`]): only what + // renderer port #2's marks tell, and its fallback. + let written = heap.written.as_ref(); + if written.is_none() { + let marks = if crate::port_descriptors::adopted(start) { marks_alone() } else { Marks::Off }; + if marks == Marks::Off { + continue; + } + if marks == Marks::Textures && step == 32 && crate::safecopy::on() { + let mut sums = [0u64; 4]; + let began = std::time::Instant::now(); + let phase = crate::timeline::span("the heaps' records: marked textures"); + sync_textures(host, start, heap.desc.count.min(host.count()), frame, &mut sums); + phase.end(); + spent[2] += since(began); + // (A round counts as a page: the line is printed while there are some.) + pages += sums[3].max(1); + looked += sums[0]; + mirrored += sums[1]; + kinds[1] += sums[1]; + tabled += sums[2]; + continue; + } + } let step = u64::from(step); // Capped host heaps have no counterparts for the title slots reserved // for per-draw tables; raw writes there must not be mirrored. @@ -437,7 +593,11 @@ pub(crate) fn sync_heaps() { if crate::port_descriptors::ask_due(frame) { let began = std::time::Instant::now(); let phase = crate::timeline::span("the heaps' records: asking (renderer port 2's fallback)"); - let taken = written.take(start as usize, end as usize); + let taken = match written { + Some(written) => written.take(start as usize, end as usize), + // (Any page may have been: the fallback goes round them all.) + None => vec![(start as usize, end as usize)], + }; phase.end(); spent[0] += since(began); crate::port_descriptors::asked(&taken); @@ -445,7 +605,7 @@ pub(crate) fn sync_heaps() { for page in crate::port_descriptors::to_check(frame) { parts.push((page.max(start as usize), (page + crate::watch::PAGE).min(end as usize), true)); } - } else { + } else if let Some(written) = written { let began = std::time::Instant::now(); let phase = crate::timeline::span("the heaps' records: asking"); let taken = written.take(start as usize, end as usize); diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 264c19e07..2932e17cc 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -4719,6 +4719,11 @@ impl HostHeap { Some(view) } + /// Whether slot `index` shows the view of record `(entry, generation)`. + pub(crate) fn shows_record(&self, index: u32, entry: u32, generation: u32) -> bool { + self.made.get(index) == Some(u64::from(generation) << 32 | u64::from(entry)) + } + /// Whether slot `index` was made from no record: what [`HostHeap::mirror`] /// would find before it did nothing for one that holds no view. pub(crate) fn shows_none(&self, index: u32) -> bool {