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
418 changes: 381 additions & 37 deletions crates/victory-gpu/src/descriptor/host.rs

Large diffs are not rendered by default.

215 changes: 166 additions & 49 deletions crates/victory-gpu/src/draw.rs

Large diffs are not rendered by default.

444 changes: 444 additions & 0 deletions crates/victory-gpu/src/heap_worker.rs

Large diffs are not rendered by default.

109 changes: 107 additions & 2 deletions crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3601,6 +3601,22 @@ fn split<T>(lists: Vec<T>, order: Vec<usize>, cut: usize) -> Result<[Run<T>; 2],
Ok([(a, x), (b, y)])
}

/// Prepends `count` worker upload lists (stored at `first..first + count` in
/// `lists`) ahead of `order` and shifts every joined-part split boundary in
/// `parts` by `+count` so submission part cuts preserve title split points.
fn prepend_ahead_order(
order: &mut Vec<usize>,
parts: &[usize],
first: usize,
count: usize,
) -> Vec<usize> {
if count == 0 {
return parts.to_vec();
}
order.splice(0..0, first..first + count);
parts.iter().map(|&p| p + count).collect()
}

/// A fence signal on a queue ([`late`]): goes behind the flights in the
/// air, if there are any, and is set when they are settled. Whether it
/// did; if not the caller sets the fence now.
Expand Down Expand Up @@ -4218,7 +4234,7 @@ pub(crate) use stamps::{timeline_ready, timeline_restart, timeline_stamps};

#[cfg(test)]
mod late_tests {
use super::{continues, join_ranges, touches};
use super::{continues, join_ranges, prepend_ahead_order, touches};

/// Copies join only when both ends continue and the whole does not
/// overlap itself.
Expand Down Expand Up @@ -4266,6 +4282,36 @@ mod late_tests {
}
assert!(!touches(&[], 0, usize::MAX));
}

#[test]
fn prepend_ahead_lists_shifts_join_parts() {
let mut order = vec![0, 1, 2];
let parts = [1, 2];
let shifted = prepend_ahead_order(&mut order, &parts, 3, 2);
assert_eq!(order, [3, 4, 0, 1, 2]);
assert_eq!(shifted, [3, 4]);

let lists = ["ahead0", "ahead1", "title0", "title1", "title2"];
let firsts = [0, 1, 2, 3, 4];
let cuts: Vec<usize> = shifted
.iter()
.filter_map(|&n| firsts.get(n).copied())
.collect();
let mut batches = Vec::new();
let mut from = 0;
for to in cuts.into_iter().chain([lists.len()]) {
batches.push(&lists[from..to]);
from = to;
}
assert_eq!(
batches,
[
&["ahead0", "ahead1", "title0"][..],
&["title1"][..],
&["title2"][..],
]
);
}
}

/// Whether a title buffer copy that continues the one recorded before it
Expand Down Expand Up @@ -5663,6 +5709,14 @@ impl Host {
let _ = self.submit(&[list], &[], false);
}

/// Runs closed internal upload-only host lists synchronously in one
/// submission without draining title queues (`queue::before_sync`).
pub(crate) fn execute_sync_internal_many(&self, runs: &[&HostList]) {
if !runs.is_empty() {
let _ = self.submit(runs, &[], false);
}
}

/// A queue's submission with [`late`]: the host lists `lists`, taken
/// from the title's, go to the card in `order` and are not waited for.
/// They join the line of flights and are settled, with the others in
Expand Down Expand Up @@ -5699,7 +5753,26 @@ impl Host {
self.send_late(lists, order, parts);
}

fn send_late(&self, lists: Vec<crate::list::Detached>, order: Vec<usize>, parts: &[usize]) {
fn send_late(
&self,
mut lists: Vec<crate::list::Detached>,
mut order: Vec<usize>,
parts: &[usize],
) {
// Textures uploaded ahead (`crate::heap_worker`): their lists run
// before the title's, and joined-part split boundaries shift by their
// count so submission part cuts preserve title split points.
let ahead = crate::heap_worker::take_ready_for_late();
let shifted_parts: Vec<usize>;
let parts = if !ahead.is_empty() {
let first = lists.len();
let count = ahead.len();
lists.extend(ahead.into_iter().map(crate::list::Detached::ours));
shifted_parts = prepend_ahead_order(&mut order, parts, first, count);
&shifted_parts[..]
} else {
parts
};
let sent = {
let runs: Vec<&HostList> = order.iter().map(|&i| lists[i].host()).collect();
self.submit(&runs, parts, true)
Expand Down Expand Up @@ -8065,6 +8138,24 @@ impl HostList {
deferred.prelude_uploads.push(upload);
}

/// Detiles the textures the list uploads now, ahead of its submission
/// ([`crate::texture::Staging::refresh_ahead`]); how many were left.
pub(crate) fn refresh_textures_ahead(&self) -> u64 {
let textures: Vec<_> = self
.deferred()
.copies
.iter()
.filter_map(|step| match step {
Step::Texture(staging) => Some(staging.clone()),
_ => None,
})
.collect();
textures
.iter()
.filter(|staging| !staging.refresh_ahead())
.count() as u64
}

/// At submission, after the list's steps: the textures whose upload its
/// prelude records, each with whether it is the first upload into its
/// host copy. Their staging buffers are refreshed first (those the
Expand Down Expand Up @@ -9861,6 +9952,20 @@ impl Meter {
let picked = crate::draw::HEAP_BUFFER_PASSES.swap(0, Ordering::Relaxed) as f64 / frames;
let picked_ms =
crate::draw::HEAP_BUFFER_MICROS.swap(0, Ordering::Relaxed) as f64 / 1e3 / frames;
{
let [busy, detiling, lists, shown, again, left] =
std::array::from_fn::<f64, 6, _>(|i| {
crate::heap_worker::STATS[i].swap(0, Ordering::Relaxed) as f64 / frames
});
if busy > 0.0 {
tracing::info!(
"textures picked by index, their own thread (VICTORY_HEAP_WORKER), per frame: {:.1} ms at work, of them {:.1} detiling; \
{lists:.1} lists, {shown:.0} slots shown, {again:.1} written again before, {left:.1} textures left to the submission",
busy / 1e6,
detiling / 1e6
);
}
}
// Copies between depth textures and their colour twins, each way.
let [twin_to_depth, twin_to_colour] =
[2, 3].map(|i| crate::resource::TWINS[i].swap(0, Ordering::Relaxed) as f64 / frames);
Expand Down
2 changes: 2 additions & 0 deletions crates/victory-gpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ mod book;
#[cfg(windows)]
mod heap_marks;
#[cfg(windows)]
mod heap_worker;
#[cfg(windows)]
mod host;
/// The F1 settings menu is shown (`host/menu.rs`): the title gets no keyboard meanwhile
/// (read by the input module).
Expand Down
11 changes: 10 additions & 1 deletion crates/victory-gpu/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ pub(crate) struct Detached {

#[cfg(windows)]
impl Detached {
/// A closed list of ours, to run among a title's.
pub(crate) fn ours(host: crate::host::HostList) -> Self {
Detached {
host,
_uploads: Vec::new(),
_state: Default::default(),
}
}

pub(crate) fn host(&self) -> &crate::host::HostList {
&self.host
}
Expand Down Expand Up @@ -351,7 +360,7 @@ fn pools() -> bool {
/// A host list open for recording: one that has run, as a title's list is
/// after its reset, or a new one.
#[cfg(windows)]
fn fresh_list() -> Option<crate::host::HostList> {
pub(crate) fn fresh_list() -> Option<crate::host::HostList> {
let idle = {
let mut idle = IDLE.lock().unwrap_or_else(|e| e.into_inner());
let taken = idle.0.pop();
Expand Down
9 changes: 9 additions & 0 deletions crates/victory-gpu/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,15 @@ impl Drop for Held {
}
}

/// Holds title texture `this` alive, for work on a thread the title knows
/// nothing of. `None` if it is no texture, or is being released.
#[cfg(windows)]
pub(crate) fn hold(this: This) -> Option<Held> {
// A texture leaves the table before it is freed, under this lock.
let all = textures();
(all.contains(&(this as usize)) && crate::object::add_ref_alive(this)).then(|| Held(this))
}

/// The title textures the host renders to or writes (targets, depth,
/// unordered access), by the address of their memory: what a view the
/// title places in that memory in another format shows ([`alias_of`]). A
Expand Down
56 changes: 52 additions & 4 deletions crates/victory-gpu/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ struct Last {
/// first checks, and after the hash found a change the guard had not
/// seen ([`hot_textures`]). 0: not checked under the guard yet.
hot_until: u64,
/// The frame it was read in ahead of its submission
/// ([`Staging::refresh_ahead`]).
ahead: Option<u64>,
}

/// Frames a guarded texture is hashed at every check from its first check,
Expand Down Expand Up @@ -1474,26 +1477,27 @@ impl Staging {
// the hash, which is taken every eighth frame only, by the
// texture's address, so the textures take turns.
// `VICTORY_TEXTURE_GUARD=0` hashes at every check, as before.
static GUARD: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
// The guard saw no write: a change the hash finds came another way.
let mut blind = false;
if !alone
&& *GUARD.get_or_init(|| std::env::var_os("VICTORY_TEXTURE_GUARD").is_none_or(|v| v != "0"))
&& Self::guard_on()
&& let Some((now, written)) = waited(1, || crate::watch::writes(start, start + size, true))
{
const HASH_EVERY: u64 = 8;
let unwritten = last.asked != 0 && written.iter().all(|&(_, w)| w <= last.asked);
last.asked = now;
let frame = crate::frame::current();
let stale_ahead = last.ahead.is_none_or(|at| frame > at + 1);
let turn = frame
.wrapping_add((start >> 16) as u64)
.is_multiple_of(HASH_EVERY);
.is_multiple_of(HASH_EVERY)
&& stale_ahead;
// A texture starts hot: one written that way from its first
// frames on changes before the hash has ever caught it.
if last.hot_until == 0 {
last.hot_until = frame + HOT_FRAMES;
}
let hot = hot_textures() && frame < last.hot_until;
let hot = hot_textures() && frame < last.hot_until && stale_ahead;
if !exact && unwritten && !turn && !hot {
return 0;
}
Expand Down Expand Up @@ -1611,6 +1615,50 @@ impl Staging {
size
}

fn guard_on() -> bool {
static GUARD: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*GUARD.get_or_init(|| std::env::var_os("VICTORY_TEXTURE_GUARD").is_none_or(|v| v != "0"))
}

/// Detiles the texture memory into the staging buffer ahead of the
/// submission, on the thread of `crate::heap_worker`: read through the
/// kernel, so that the runtime's remapping never waits for it, whole or
/// not at all (the submission then does it, as ever). False if it was
/// not read. What the title writes after this is found at submission
/// (unless `self.once` or `VICTORY_TEXTURE_GUARD=0`, which skip
/// write-watching as `refresh_timed` does).
pub(crate) fn refresh_ahead(&self) -> bool {
let size = self.read;
let start = self.memory as usize;
let mut last = self.last.lock().unwrap_or_else(|e| e.into_inner());
let last = &mut *last;
if last.hash.is_some() || last.asked != 0 {
// Detiled before: the submission's check is cheap.
return true;
}
let alone = self.once.load(std::sync::atomic::Ordering::Relaxed);
let now = if alone || !Self::guard_on() {
0
} else {
let Some((now, _)) = crate::watch::writes(start, start + size, false)
.or_else(|| crate::watch::writes(start, start + size, true))
else {
return false;
};
now
};
with_read(size, |read| {
if !crate::host::read_into(self.memory, read) {
return false;
}
last.hash = Some(digest(read));
self.detile(read);
last.asked = now;
last.ahead = Some(crate::frame::current());
true
})
}

/// A refresh while the runtime is remapping title memory ([`no_wait`]):
/// the texture is read through the kernel, which fails if a page of it
/// is not there at this moment. Then a staging buffer that was filled
Expand Down