Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/victory-gpu/src/faultbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pub(crate) enum What {
/// A store (the instruction at `a`) was done beside the guard of block
/// `b`.
Stored = 10,
/// A write fault opened block `a` without the guards' lock
/// (`VICTORY_FAULT_UNLOCKED`); b: other views of its page told as
/// written; c: nanoseconds in it.
Unlocked = 11,
}

#[repr(C)]
Expand Down
50 changes: 39 additions & 11 deletions crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,33 @@ unsafe impl Sync for Host {}
/// releases reach the host at once, from its own threads, while the host
/// runs a submission: freeing there is a GPU page fault on the freed
/// texture (a device hang).
static RETIRED: Mutex<std::collections::VecDeque<(u64, Retired)>> = Mutex::new(std::collections::VecDeque::new());
///
/// A queue to each thread that retires, of [`RETIRED_QUEUES`] taken in turn
/// ([`retired`]), each in the order its thread retired. One queue under one
/// lock was what the threads that record the title's lists waited for: eight
/// of them let go of some 20,000 slots' resources a frame, each a push of a
/// few nanoseconds, but a thread that finds the lock taken sleeps and is
/// woken 50 microseconds later or more. 3.3 of the 22 ms a frame each of
/// them was at work, and 0.7 ms of the submitting thread's frame (a profile
/// of the prologue's cell, RTX 3090 Ti, Ryzen 9 9950X3D, Series S profile).
static RETIRED: [Mutex<std::collections::VecDeque<(u64, Retired)>>; RETIRED_QUEUES] =
[const { Mutex::new(std::collections::VecDeque::new()) }; RETIRED_QUEUES];
const RETIRED_QUEUES: usize = 32;

/// This thread's queue of [`RETIRED`].
fn retired() -> &'static Mutex<std::collections::VecDeque<(u64, Retired)>> {
// `VICTORY_RETIRED_QUEUES=1`: a queue to each thread; off: one queue, as before.
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if !*ON.get_or_init(|| crate::switch::on("VICTORY_RETIRED_QUEUES")) {
return &RETIRED[0];
}
static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
thread_local! {
static MINE: usize = NEXT.fetch_add(1, Ordering::Relaxed) % RETIRED_QUEUES;
}
// (A thread that is ending has none any more: the first.)
&RETIRED[MINE.try_with(|mine| *mine).unwrap_or(0)]
}
/// Submissions started and finished (waited for).
static STARTED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
static FINISHED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
Expand Down Expand Up @@ -469,7 +495,7 @@ pub(crate) fn retire<T: 'static>(object: T) {
return;
}
RETIREMENTS[1].fetch_add(1, Ordering::Relaxed);
RETIRED
retired()
.lock()
.unwrap_or_else(|e| e.into_inner())
.push_back((started, Retired::Any(Box::new(object))));
Expand All @@ -485,7 +511,7 @@ pub(crate) fn retire_resource(object: ID3D12Resource) {
return;
}
RETIREMENTS[1].fetch_add(1, Ordering::Relaxed);
RETIRED.lock().unwrap_or_else(|e| e.into_inner()).push_back((started, Retired::Resource(object)));
retired().lock().unwrap_or_else(|e| e.into_inner()).push_back((started, Retired::Resource(object)));
}

/// How many submissions later than it could (the last one that may use it
Expand All @@ -503,18 +529,20 @@ fn bury_lag() -> u64 {
}

/// Drops what waited for submissions up to `finished` ([`bury_lag`]). The
/// numbers come in order, but for threads that overtake each other: one of
/// those is dropped a little later than it could be.
/// numbers of a queue come in order, but for a submission begun between a
/// thread's two looks ([`retire`]): what follows such a one is dropped a
/// little later than it could be.
fn bury(finished: u64) {
let finished = finished.saturating_sub(bury_lag());
let done: Vec<(u64, Retired)> = {
let mut retired = RETIRED.lock().unwrap_or_else(|e| e.into_inner());
let mut done: Vec<(u64, Retired)> = Vec::new();
for queue in &RETIRED {
let mut retired = queue.lock().unwrap_or_else(|e| e.into_inner());
let n = retired.iter().take_while(|(at, _)| *at <= finished).count();
if n == 0 {
return;
if n != 0 {
done.extend(retired.drain(..n));
}
retired.drain(..n).collect()
};
}
// (Dropped with no queue's lock held: a release each.)
drop(done);
}

Expand Down
1 change: 1 addition & 0 deletions crates/victory-gpu/src/shader_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ fn build() -> [u8; 32] {
"VICTORY_MESH_OUTPUT_STRICT",
"VICTORY_BVH_FENCES",
"VICTORY_BVH_BOUNDS",
"VICTORY_WATERFALL_SHAPE",
] {
bytes.extend_from_slice(name.as_bytes());
bytes.push(b'=');
Expand Down
10 changes: 8 additions & 2 deletions crates/victory-gpu/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2113,6 +2113,12 @@ fn dword_runs(at: usize, title: &[u8], merged: &[u8], gap: usize) -> Vec<(usize,

/// [`dword_runs`] with no gap, the fast way where it can be: a frame compares
/// 12 MB this way and finds 80,000 runs.
/// `VICTORY_WORD_RUNS=1`: word_runs; off: dword_runs, as before.
fn word_runs_on() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| crate::switch::on("VICTORY_WORD_RUNS"))
}

fn word_runs(at: usize, title: &[u8], held: &[u8]) -> Vec<(usize, usize)> {
#[cfg(target_arch = "x86_64")]
if at % 4 == 0 && title.len() % 4 == 0 && title.len() == held.len() && std::arch::is_x86_feature_detected!("avx2") {
Expand Down Expand Up @@ -2215,7 +2221,7 @@ fn put_changed(at: usize, title: &[u8], merged: &[u8]) -> usize {
}
return merged.len();
}
let mut runs = dword_runs(at, title, merged, 0);
let mut runs = if word_runs_on() { word_runs(at, title, merged) } else { dword_runs(at, title, merged, 0) };
if runs.len() > 64 {
runs = dword_runs(at, title, merged, 32);
PUT[3].fetch_add(1, Ordering::Relaxed);
Expand Down Expand Up @@ -4047,7 +4053,7 @@ impl Shadow {
// refresh lie apart.
let held = unsafe { std::slice::from_raw_parts_mut(self.data.add(at - start), n) };
let was = crate::host::upcheck::on().then(|| held.to_vec());
let runs = if whole { vec![(0, n)] } else { dword_runs(at, held, title, 0) };
let runs = if whole { vec![(0, n)] } else if word_runs_on() { word_runs(at, held, title) } else { dword_runs(at, held, title, 0) };
for &(a, len) in &runs {
held[a..a + len].copy_from_slice(&title[a..a + len]);
self.changed(at - start + a, at - start + a + len);
Expand Down
26 changes: 26 additions & 0 deletions crates/victory-gpu/src/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ macro_rules! toggled {
}};
}

/// A switch that is off unless its variable is `1`, read once; with
/// `VICTORY_AB` naming it (diagnostics builds), on and off while the title
/// runs.
#[cfg(windows)]
#[macro_export]
#[doc(hidden)]
macro_rules! asked_for {
($name:literal) => {{
static ASKED: std::sync::OnceLock<Result<bool, u32>> = std::sync::OnceLock::new();
let asked = ASKED.get_or_init(|| {
#[cfg(feature = "diagnostics")]
if let Some(bit) = $crate::ab::bit($name) {
return Err(bit);
}
Ok(std::env::var_os($name).is_some_and(|v| v == "1"))
});
match *asked {
Ok(on) => on,
#[cfg(feature = "diagnostics")]
Err(bit) => $crate::ab::on(bit),
#[cfg(not(feature = "diagnostics"))]
Err(_) => false,
}
}};
}

#[cfg(test)]
mod tests {
use super::value_is_on;
Expand Down
26 changes: 2 additions & 24 deletions crates/victory-gpu/src/titlewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,20 @@ fn add(what: usize) {
DONE[what].fetch_add(1, Relaxed);
}

/// A switch that is off unless its variable is `1` (or `VICTORY_AB` names it:
/// on and off while the title runs).
macro_rules! asked_for {
($name:literal) => {{
static ASKED: std::sync::OnceLock<Result<bool, u32>> = std::sync::OnceLock::new();
let asked = ASKED.get_or_init(|| {
#[cfg(feature = "diagnostics")]
if let Some(bit) = crate::ab::bit($name) {
return Err(bit);
}
Ok(std::env::var_os($name).is_some_and(|v| v == "1"))
});
match *asked {
Ok(on) => on,
#[cfg(feature = "diagnostics")]
Err(bit) => crate::ab::on(bit),
#[cfg(not(feature = "diagnostics"))]
Err(_) => false,
}
}};
}

fn copies_on() -> bool {
#[cfg(test)]
if ON_IN_TEST.get() {
return true;
}
asked_for!("VICTORY_COPY_BESIDE")
crate::asked_for!("VICTORY_COPY_BESIDE")
}

fn stores_on() -> bool {
#[cfg(test)]
if ON_IN_TEST.get() {
return true;
}
asked_for!("VICTORY_STORE_BESIDE")
crate::asked_for!("VICTORY_STORE_BESIDE")
}

#[cfg(test)]
Expand Down
Loading
Loading