The heaps' records are brought up to date beside the preparing (VICTORY_HEAP_SYNC_ASIDE=0); a made slot is told without the lock (VICTORY_QUICK_MIRROR=0)
Changed pages are compared sixteen records at a time. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
d4943c43fb
commit
30317314af
3 files changed
+201
-20
No files matched your search
@@ -161,6 +161,96 @@ fn mirror(handle: u64) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether [`sync_heaps`] is done (`VICTORY_HEAP_SYNC=0`: not).
|
||||
#[cfg(windows)]
|
||||
fn sync_on() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var_os("VICTORY_HEAP_SYNC").is_none_or(|v| v != "0"))
|
||||
}
|
||||
|
||||
/// 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).
|
||||
///
|
||||
/// It took 13 ms a frame of the submitting thread (session play-1125-0203:
|
||||
/// 230 changed heap pages and 470,000 slots in 30 submissions). What it makes
|
||||
/// are the host heap's descriptors, which the card reads: it has to be done
|
||||
/// before the lists go to the queue, and nothing of the preparing looks at
|
||||
/// them (what a draw reads through a table it made itself, when recorded).
|
||||
#[cfg(windows)]
|
||||
fn sync_aside() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| sync_on() && std::env::var_os("VICTORY_HEAP_SYNC_ASIDE").is_none_or(|v| v != "0"))
|
||||
}
|
||||
|
||||
/// The thread's work: rounds asked for, rounds done.
|
||||
#[cfg(windows)]
|
||||
static SYNC_ROUNDS: (std::sync::Mutex<(u64, u64)>, std::sync::Condvar) = (std::sync::Mutex::new((0, 0)), std::sync::Condvar::new());
|
||||
|
||||
/// [`sync_heaps`], begun; done when it is dropped (with [`sync_aside`]), or
|
||||
/// when this returns.
|
||||
#[cfg(windows)]
|
||||
pub(crate) struct SyncedHeaps(Option<u64>);
|
||||
|
||||
#[cfg(windows)]
|
||||
pub(crate) fn begin_sync_heaps() -> SyncedHeaps {
|
||||
if !sync_aside() {
|
||||
sync_heaps();
|
||||
return SyncedHeaps(None);
|
||||
}
|
||||
static STARTED: std::sync::Once = std::sync::Once::new();
|
||||
static THREAD: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||
STARTED.call_once(|| {
|
||||
let made = std::thread::Builder::new().name("victory-heap-sync".into()).spawn(|| {
|
||||
let rounds = || SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner());
|
||||
/// A round is over, however it ends.
|
||||
struct Over;
|
||||
impl Drop for Over {
|
||||
fn drop(&mut self) {
|
||||
SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner()).1 += 1;
|
||||
SYNC_ROUNDS.1.notify_all();
|
||||
}
|
||||
}
|
||||
loop {
|
||||
let mut r = rounds();
|
||||
while r.0 == r.1 {
|
||||
r = SYNC_ROUNDS.1.wait(r).unwrap_or_else(|e| e.into_inner());
|
||||
}
|
||||
drop(r);
|
||||
let _over = Over;
|
||||
let _ = std::panic::catch_unwind(sync_heaps);
|
||||
}
|
||||
});
|
||||
THREAD.store(made.is_ok(), std::sync::atomic::Ordering::Release);
|
||||
});
|
||||
if !THREAD.load(std::sync::atomic::Ordering::Acquire) {
|
||||
sync_heaps();
|
||||
return SyncedHeaps(None);
|
||||
}
|
||||
let mut rounds = SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner());
|
||||
rounds.0 += 1;
|
||||
let mine = rounds.0;
|
||||
drop(rounds);
|
||||
SYNC_ROUNDS.1.notify_all();
|
||||
SyncedHeaps(Some(mine))
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl Drop for SyncedHeaps {
|
||||
fn drop(&mut self) {
|
||||
let Some(mine) = self.0 else {
|
||||
return;
|
||||
};
|
||||
let began = std::time::Instant::now();
|
||||
let mut rounds = SYNC_ROUNDS.0.lock().unwrap_or_else(|e| e.into_inner());
|
||||
while rounds.1 < mine {
|
||||
rounds = SYNC_ROUNDS.1.wait(rounds).unwrap_or_else(|e| e.into_inner());
|
||||
}
|
||||
drop(rounds);
|
||||
SYNCED[6].fetch_add(began.elapsed().as_micros() as u64, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
/// Mirrors records copied directly into placed shader-visible heaps. Calls
|
||||
/// to `Create*View` are already mirrored by [`mirror`]; raw CPU copies leave
|
||||
/// only write-watch pages to reveal which slots changed.
|
||||
@@ -173,8 +263,7 @@ fn mirror(handle: u64) {
|
||||
#[cfg(windows)]
|
||||
pub(crate) fn sync_heaps() {
|
||||
use std::sync::atomic::Ordering::Relaxed;
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
if !*ON.get_or_init(|| std::env::var_os("VICTORY_HEAP_SYNC").is_none_or(|v| v != "0")) {
|
||||
if !sync_on() {
|
||||
return;
|
||||
}
|
||||
let started = std::time::Instant::now();
|
||||
@@ -278,28 +367,30 @@ pub(crate) fn sync_heaps() {
|
||||
/// shader-visible heaps looked at, slots on them, slots whose record was
|
||||
/// not the one last seen and was mirrored, the microseconds it took; then
|
||||
/// new records left to the draws that read them through a table, and new
|
||||
/// records that waited for one ([`host::HostHeap::copied_wait`]).
|
||||
/// records that waited for one ([`host::HostHeap::copied_wait`]); and the
|
||||
/// microseconds submissions waited for it ([`sync_aside`]).
|
||||
#[cfg(windows)]
|
||||
pub(crate) static SYNCED: [std::sync::atomic::AtomicU64; 6] = [const { std::sync::atomic::AtomicU64::new(0) }; 6];
|
||||
pub(crate) static SYNCED: [std::sync::atomic::AtomicU64; 7] = [const { std::sync::atomic::AtomicU64::new(0) }; 7];
|
||||
|
||||
/// With the frame line: what [`sync_heaps`] did per frame over `frames`.
|
||||
/// Nothing while no heap page changed.
|
||||
#[cfg(windows)]
|
||||
pub(crate) fn sync_line(frames: f64) {
|
||||
let [pages, looked, mirrored, micros, tabled, later] = [0, 1, 2, 3, 4, 5].map(|i| SYNCED[i].swap(0, std::sync::atomic::Ordering::Relaxed));
|
||||
let [pages, looked, mirrored, micros, tabled, later, waited] = [0, 1, 2, 3, 4, 5, 6].map(|i| SYNCED[i].swap(0, std::sync::atomic::Ordering::Relaxed));
|
||||
if pages == 0 {
|
||||
return;
|
||||
}
|
||||
tracing::info!(
|
||||
"descriptor records the title copied into its heap as bytes (VICTORY_HEAP_SYNC=0: not looked for): per frame \
|
||||
{:.1} changed heap pages, {:.0} slots looked at, {:.1} whose record was new and was mirrored, {:.1} new ones of \
|
||||
draws' tables left to the draws, {:.1} that waited for a draw first, {:.2} ms",
|
||||
draws' tables left to the draws, {:.1} that waited for a draw first, {:.2} ms ({:.2} ms waited for)",
|
||||
pages as f64 / frames,
|
||||
looked as f64 / frames,
|
||||
mirrored as f64 / frames,
|
||||
tabled as f64 / frames,
|
||||
later as f64 / frames,
|
||||
micros as f64 / 1000.0 / frames
|
||||
micros as f64 / 1000.0 / frames,
|
||||
waited as f64 / 1000.0 / frames
|
||||
);
|
||||
}
|
||||
|
||||
@@ -320,6 +411,15 @@ fn changed_slots(
|
||||
first.min(last)..last
|
||||
}
|
||||
|
||||
/// `f` of the title heap at `heap`, if that heap still exists; it stays
|
||||
/// until `f` is done.
|
||||
#[cfg(windows)]
|
||||
pub(crate) fn with_heap<T>(heap: usize, f: impl FnOnce(&Heap) -> T) -> Option<T> {
|
||||
let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
|
||||
// SAFETY: registered heaps are live (removed when dropped).
|
||||
heaps.iter().any(|&(_, _, h)| h == heap).then(|| f(unsafe { &*(heap as *const Heap) }))
|
||||
}
|
||||
|
||||
/// Gives back dynamic slots of the host heap behind the title heap at
|
||||
/// `heap`, if that heap still exists.
|
||||
#[cfg(windows)]
|
||||
|
||||
@@ -26,6 +26,9 @@ pub(crate) struct HostHeap {
|
||||
sampler: bool,
|
||||
/// The record (view index and generation) each slot was last made from.
|
||||
made: Mutex<Vec<u64>>,
|
||||
/// The same for a slot whose descriptor is made and stays (else 0): what
|
||||
/// [`HostHeap::mirror`] asks first, without the lock. Changed under it.
|
||||
quick: Vec<std::sync::atomic::AtomicU64>,
|
||||
/// The records of the title's heap as the pass over records copied in
|
||||
/// as bytes last saw them ([`HostHeap::copied_changes`]). Made on first
|
||||
/// use.
|
||||
@@ -1070,6 +1073,7 @@ impl HostHeap {
|
||||
dynamic: extra,
|
||||
sampler,
|
||||
made: Mutex::new(vec![0; count as usize]),
|
||||
quick: (0..count).map(|_| std::sync::atomic::AtomicU64::new(0)).collect(),
|
||||
copied: Mutex::new(Vec::new()),
|
||||
waiting: Mutex::default(),
|
||||
away: Mutex::default(),
|
||||
@@ -1606,6 +1610,7 @@ impl HostHeap {
|
||||
/// Makes host slot `index` null again, as it is before its view is
|
||||
/// made: a null view of the kind it held.
|
||||
fn unmake(&self, made: &mut [u64], index: u32) {
|
||||
self.quick_is(index, 0);
|
||||
let Some(key) = made.get_mut(index as usize).map(std::mem::take) else {
|
||||
return;
|
||||
};
|
||||
@@ -2019,15 +2024,7 @@ impl HostHeap {
|
||||
return;
|
||||
};
|
||||
let records = &records[..last.len()];
|
||||
if last == records {
|
||||
return;
|
||||
}
|
||||
for (n, (old, new)) in last.chunks_exact_mut(step).zip(records.chunks_exact(step)).enumerate() {
|
||||
if old != new {
|
||||
old.copy_from_slice(new);
|
||||
out.push(first + n as u32);
|
||||
}
|
||||
}
|
||||
changed_records(last, records, step, first, out);
|
||||
}
|
||||
|
||||
/// Whether new records of draws' tables are left to the draws
|
||||
@@ -2099,12 +2096,24 @@ impl HostHeap {
|
||||
let [tag, i, generation] = unsafe { (handle as *const [u32; 3]).read_unaligned() };
|
||||
// Generations start at 1, so a made slot's key is never 0.
|
||||
let key = u64::from(generation) << 32 | u64::from(i);
|
||||
// Made from this record, and nothing has changed it: most of the
|
||||
// asks, from five recording threads, behind one lock while another
|
||||
// held it to make a descriptor (15 % of a recording thread's time in
|
||||
// the opening, profile cpu-1).
|
||||
if tag == VIEW_TAG
|
||||
&& quick_mirror()
|
||||
&& self.quick.get(index as usize).is_some_and(|k| k.load(std::sync::atomic::Ordering::Acquire) == key)
|
||||
&& let Some(view) = view_at(i, generation)
|
||||
{
|
||||
return Some(view);
|
||||
}
|
||||
let mut made = self.made.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let view = (tag == VIEW_TAG).then(|| view_at(i, generation)).flatten();
|
||||
let Some(view) = view else {
|
||||
// Not a view of ours (never written, or of a resource the
|
||||
// title released): nothing to read.
|
||||
if made[index as usize] != 0 {
|
||||
self.quick_is(index, 0);
|
||||
made[index as usize] = 0;
|
||||
self.make_null(index);
|
||||
let _ = self.note_buffer(index, None, false);
|
||||
@@ -2147,6 +2156,7 @@ impl HostHeap {
|
||||
KEPT_VIEWS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
return Some(view);
|
||||
}
|
||||
self.quick_is(index, 0);
|
||||
made[index as usize] = if later { 0 } else { key };
|
||||
let kept = if later {
|
||||
None
|
||||
@@ -2157,10 +2167,19 @@ impl HostHeap {
|
||||
null_for(&view, self.cpu(index));
|
||||
}
|
||||
self.keep(index, kept.flatten());
|
||||
self.quick_is(index, made[index as usize]);
|
||||
}
|
||||
Some(view)
|
||||
}
|
||||
|
||||
/// Slot `index` is made from record `key` and stays so (0: not). Under
|
||||
/// the lock of `made`.
|
||||
fn quick_is(&self, index: u32, key: u64) {
|
||||
if let Some(quick) = self.quick.get(index as usize) {
|
||||
quick.store(key, std::sync::atomic::Ordering::Release);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether slot `index` is backed by a made host descriptor (tests).
|
||||
#[cfg(test)]
|
||||
pub(crate) fn made(&self, index: u32) -> bool {
|
||||
@@ -2987,6 +3006,65 @@ fn keep_views() -> bool {
|
||||
*ON.get_or_init(|| std::env::var_os("VICTORY_KEEP_VIEWS").is_none_or(|v| v != "0"))
|
||||
}
|
||||
|
||||
/// Whether a slot already made from its record is told so without the lock
|
||||
/// (`VICTORY_QUICK_MIRROR=0`: with it, as before).
|
||||
fn quick_mirror() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var_os("VICTORY_QUICK_MIRROR").is_none_or(|v| v != "0"))
|
||||
}
|
||||
|
||||
/// The records (`step` bytes each, the first is slot `first`) that differ
|
||||
/// between `last` and `records` (of one length) go into `out` and `last`.
|
||||
/// Most of a changed page is as it was: it is looked at sixteen records at a
|
||||
/// time.
|
||||
fn changed_records(last: &mut [u8], records: &[u8], step: usize, first: u32, out: &mut Vec<u32>) {
|
||||
const AT_A_TIME: usize = 16;
|
||||
let block = AT_A_TIME * step;
|
||||
for (b, (old, new)) in last.chunks_mut(block).zip(records.chunks(block)).enumerate() {
|
||||
if old == new {
|
||||
continue;
|
||||
}
|
||||
for (n, (old, new)) in old.chunks_exact_mut(step).zip(new.chunks_exact(step)).enumerate() {
|
||||
if old != new {
|
||||
old.copy_from_slice(new);
|
||||
out.push(first + (b * AT_A_TIME + n) as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod changed_records_tests {
|
||||
use super::changed_records;
|
||||
|
||||
#[test]
|
||||
fn changed_records_are_those_that_differ_one_by_one() {
|
||||
for (step, count) in [(32usize, 128usize), (32, 2048), (16, 77), (32, 5), (32, 17)] {
|
||||
let mut seed = 7u64;
|
||||
let mut random = move || {
|
||||
seed = seed.wrapping_mul(0x5851_F42D_4C95_7F2D).wrapping_add(0x1405_7B7E_F767_814F);
|
||||
(seed >> 33) as usize
|
||||
};
|
||||
let mut last: Vec<u8> = (0..step * count).map(|_| random() as u8).collect();
|
||||
let mut now = last.clone();
|
||||
let mut differ = Vec::new();
|
||||
for slot in 0..count {
|
||||
if random() % 9 == 0 {
|
||||
now[slot * step + random() % step] ^= 1 + (random() % 255) as u8;
|
||||
differ.push(1000 + slot as u32);
|
||||
}
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
changed_records(&mut last, &now, step, 1000, &mut out);
|
||||
assert_eq!(out, differ);
|
||||
assert_eq!(last, now);
|
||||
out.clear();
|
||||
changed_records(&mut last, &now, step, 1000, &mut out);
|
||||
assert!(out.is_empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether both are shader views of one texture the title placed in
|
||||
/// memory: the same address and the same resource description, whatever
|
||||
/// part of it each shows.
|
||||
|
||||
@@ -4852,7 +4852,7 @@ impl Host {
|
||||
}
|
||||
// Descriptors the title copied into placed heaps directly, before
|
||||
// command lists that index those slots are submitted.
|
||||
crate::descriptor::sync_heaps();
|
||||
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>;
|
||||
let runs = match &own {
|
||||
@@ -4885,9 +4885,6 @@ impl Host {
|
||||
SWEPT.store(now_ms(), Ordering::Relaxed);
|
||||
}
|
||||
let behind = late && flying();
|
||||
// The slots that waited for a texture's host copy, before the
|
||||
// lists that may read them run.
|
||||
crate::descriptor::make_waiting();
|
||||
let this_submission = STARTED.fetch_add(1, Ordering::AcqRel) + 1;
|
||||
// The ranges shaders write are counted for it (with the relay).
|
||||
crate::shadow::preparing(this_submission);
|
||||
@@ -5019,6 +5016,12 @@ 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")]
|
||||
|
||||
Reference in new issue
Block a user