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
15 changes: 15 additions & 0 deletions crates/victory-gpu/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,21 @@ pub(crate) fn view(handle: u64) -> Option<View> {
view_at(i, generation)
}

/// The view recorded at `handle`, and what says which record that is
/// (`host::HostHeap::publish`).
#[cfg(windows)]
pub(crate) fn view_and_key(handle: u64) -> Option<(View, u64)> {
if handle == 0 {
return None;
}
// SAFETY: as in `view`.
let [tag, i, generation] = unsafe { (handle as *const [u32; 3]).read_unaligned() };
if tag != VIEW_TAG {
return None;
}
Some((view_at(i, generation)?, u64::from(generation) << 32 | u64::from(i)))
}

/// A description of `N` words, if the title passes one.
fn words<const N: usize>(desc: *const [u32; N]) -> Option<[u32; N]> {
// SAFETY: the title passes a description or null.
Expand Down
59 changes: 59 additions & 0 deletions crates/victory-gpu/src/descriptor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ struct Waiting {
checked: (u64, u64, u64, u8, u64),
}

thread_local! {
/// This thread is inside [`HostHeap::publish`].
static PUBLISHING: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

/// The queue of title slots whose texture view waits to be brought up to
/// date for shaders that pick textures by index: which are due, and in
/// which order (no host heap needed, so it is tested without one).
Expand Down Expand Up @@ -1489,6 +1494,9 @@ impl HostHeap {
/// `texture`, whether it now holds a view of a texture the title may
/// fill; `seen` as for [`HostHeap::note_buffer`].
fn queue_texture(&self, index: u32, texture: bool, seen: bool, frame: u64) {
if PUBLISHING.get() {
return;
}
let stamp = if tabled_out() { self.tabled_stamp(index) } else { 0 };
let now = (frame as u32).wrapping_add(1) & !TABLED_READ;
// A table's slot lately: the draw that reads it uploads its
Expand Down Expand Up @@ -1918,6 +1926,23 @@ impl HostHeap {
(looked, due.len() as u64)
}

/// Makes host slot `index` from the record at `handle` if it is still
/// the one with `key`, whose texture's upload is in the queue now
/// (`crate::heap_worker`). The slot's place in the queue of textures
/// picked by index was settled when that upload was recorded, and stays
/// as it is. False if the title wrote the slot again.
pub(crate) fn publish(&self, index: u32, handle: u64, key: u64) -> bool {
// SAFETY: the handle is a descriptor slot in title memory.
let [tag, i, generation] = unsafe { (handle as *const [u32; 3]).read_unaligned() };
if tag != VIEW_TAG || u64::from(generation) << 32 | u64::from(i) != key {
return false;
}
PUBLISHING.set(true);
let shown = self.mirror(index, handle, true).is_some();
PUBLISHING.set(false);
shown
}

/// Makes host slot `index` from the record the title has at `handle`
/// if it changed since, and returns the view recorded there. Buffer
/// views are made only for draws (`for_draw`), which also copy their
Expand Down Expand Up @@ -3454,6 +3479,40 @@ mod tests {
assert_eq!(slots(62), [1]);
}

/// A slot whose texture was uploaded ahead shows it when it is
/// published, and does not go back into the queue of textures picked by
/// index for that; one the title wrote again is left alone.
#[test]
fn a_published_slot_stays_out_of_the_queue() {
let Some(heap) = HostHeap::new(false, 8) else {
return; // no host GPU
};
// 64x64 BC7, one mip, at an address no test and no title uses.
let view = |at: u64| {
let mut desc = [0u32; 16];
(desc[0], desc[1], desc[2], desc[5]) = (98, 4, 5768, 1);
(desc[10], desc[11]) = (at as u32, (at >> 32) as u32);
View::Placed(false, [3, 0, 0, 0, 64, 0, 64, 0x1_0001, 98, 1, 0, 0x105, 0, 0], desc)
};
let title = [0u32; 8];
let handle = title.as_ptr() as u64;
crate::descriptor::record(handle, view(0x7fec_0000_0000));
let (first, key) = crate::descriptor::view_and_key(handle).unwrap();
assert!(heap.note_buffer(1, Some(&first), false).is_empty());
let (index, write) = heap.texture_slots_due(u64::MAX, 8).0[0];
assert_eq!(index, 1);
// Its upload was recorded: out of the queue, the slot not made yet.
heap.texture_slot_done(1, write, None);
assert!(!heap.made(1));
assert!(heap.publish(1, handle, key));
assert!(heap.made(1));
assert!(heap.texture_slots_due(u64::MAX, 8).0.is_empty());
// Written again since the upload was recorded.
crate::descriptor::record(handle, view(0x7feb_0000_0000));
assert!(!heap.publish(1, handle, key));
assert_ne!(crate::descriptor::view_and_key(handle).unwrap().1, key);
}

/// `VICTORY_STALE_TEXTURES=0`: nothing is ever stale, and a view that
/// overlaps others is left out of the spans, as before.
#[test]
Expand Down
82 changes: 69 additions & 13 deletions crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@ pub(crate) static HEAP_VIEWS_ELSEWHERE: AtomicU64 = AtomicU64::new(0);
/// index ([`State::heap_buffers`]), and the microseconds they took.
pub(crate) static HEAP_BUFFER_PASSES: AtomicU64 = AtomicU64::new(0);
pub(crate) static HEAP_BUFFER_MICROS: AtomicU64 = AtomicU64::new(0);
/// Where the pass over textures picked by index records its uploads: into
/// a list of the title's, or into lists of the thread that runs it ahead.
pub(crate) enum Into<'a> {
List(&'a HostList),
Ahead(&'a mut crate::heap_worker::Batch),
}

/// Of [`HEAP_BUFFER_MICROS`], the microseconds of the pass over textures
/// picked by index that runs inside it (`heap_textures`), and the buffer
/// slots the pass over buffer views looked at; since the last frame line.
Expand Down Expand Up @@ -1505,7 +1512,9 @@ impl State {
shadow.refresh(list, *from, to - from);
}
let textures_began = std::time::Instant::now();
self.heap_textures(list, heap, host);
if !crate::heap_worker::kick(heap) {
Self::heap_textures(Into::List(list), heap, host);
}
HEAP_PASS_PARTS[0].fetch_add(textures_began.elapsed().as_micros() 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);
Expand Down Expand Up @@ -1571,6 +1580,11 @@ impl State {
return;
};
let bake = bake && left > 0;
// What can wait for a later list is the worker's.
if !bake && crate::heap_worker::kick(heap) {
return;
}
let _pass = crate::heap_worker::on().then(crate::heap_worker::urgent);
let n = EARLY_TEXTURE_PASSES[usize::from(bake)].fetch_add(1, Ordering::Relaxed) + 1;
if bake && n.is_power_of_two() {
let [slots, due, ..] = HEAP_TEXTURES_QUEUE.each_ref().map(|c| c.load(Ordering::Relaxed));
Expand All @@ -1585,7 +1599,7 @@ impl State {
}
let began = std::time::Instant::now();
PASS_BUDGET.set(Some(budget));
self.heap_textures(list, heap, host);
Self::heap_textures(Into::List(list), heap, host);
PASS_BUDGET.set(None);
if bake {
BAKED_MS.fetch_add(began.elapsed().as_millis() as u64, Ordering::Relaxed);
Expand Down Expand Up @@ -1681,9 +1695,11 @@ impl State {
/// in place of others, and contents changed with no descriptor write,
/// are missed (hook the page-mapping events for those).
/// `VICTORY_HEAP_TEXTURES=0` turns it off.
fn heap_textures(
&self,
list: &HostList,
/// On a thread of its own the pass records into lists of its own, and
/// the slots show what they upload from then on
/// (`crate::heap_worker`).
pub(crate) fn heap_textures(
mut into: Into<'_>,
heap: &crate::descriptor::Heap,
host: &crate::descriptor::host::HostHeap,
) {
Expand Down Expand Up @@ -1756,6 +1772,8 @@ impl State {
static PASSED: AtomicU64 = AtomicU64::new(0);
let now = crate::host::now_ms();
let took = now.saturating_sub(PASSED.swap(now, Ordering::Relaxed)).max(1);
// The worker may run two passes in a frame: no shorter than that.
let took = if matches!(into, Into::Ahead(_)) { took.max(100) } else { took };
let most = (4000 / took).clamp(16, 256);
// What a table entry of a texture a shader samples says.
let sampled = UsedResource {
Expand Down Expand Up @@ -1796,6 +1814,25 @@ impl State {
if SPENT.load(Ordering::Relaxed) >= bytes_at_most || began.elapsed() > span {
break;
}
let ahead = matches!(into, Into::Ahead(_));
if ahead && crate::heap_worker::make_way() {
break;
}
// The record the slot has now, and the texture it names held
// while a thread of ours works on it. (Released meanwhile, or
// not a texture: nothing to upload.)
let record = heap.handle(index).and_then(crate::descriptor::view_and_key);
let _held = match &record {
Some((View::Shader(object, _), _)) if ahead => {
let held = crate::resource::hold(*object as This);
if held.is_none() {
host.texture_slot_done(index, write, None);
continue;
}
held
}
_ => None,
};
// A placed texture whose memory is not mapped (not streamed in
// yet, or streamed out again: the heap keeps tens of thousands
// of such views) gets no host texture; looked at again soon (a
Expand Down Expand Up @@ -1840,9 +1877,9 @@ impl State {
}
}
let mut partly = None;
if let Some(View::Placed(false, resource, desc)) = heap.handle(index).and_then(crate::descriptor::view) {
if let Some((View::Placed(false, resource, desc), _)) = &record {
let address = u64::from(desc[10]) | u64::from(desc[11]) << 32;
let len = crate::resource::placed_bytes(&resource).unwrap_or(1);
let len = crate::resource::placed_bytes(resource).unwrap_or(1);
let mapped = crate::texture::mapped_bytes(address as *const u8, len as usize);
if mapped == 0 {
HEAP_TEXTURES_AWAY.fetch_add(1, Ordering::Relaxed);
Expand All @@ -1867,9 +1904,9 @@ impl State {
// host's own contents: nothing to bring up to date, and this
// pass is no use of it (its host copy is made when something
// uses it, and its slots then: `descriptor::make_waiting`).
if let Some(View::Shader(object, _)) = heap.handle(index).and_then(crate::descriptor::view)
&& crate::resource::buffer_bytes(object as This).is_none()
&& !crate::resource::title_fills(object as This)
if let Some((View::Shader(object, _), _)) = &record
&& crate::resource::buffer_bytes(*object as This).is_none()
&& !crate::resource::title_fills(*object as This)
{
host.texture_slot_done(index, write, None);
continue;
Expand Down Expand Up @@ -1914,7 +1951,12 @@ impl State {
);
}
}
let view = heap.handle(index).and_then(|handle| host.mirror(index, handle, true));
// On the worker the slot shows the view when the upload is in
// the queue ([`crate::heap_worker::Batch::publish`]).
let view = match &into {
Into::List(_) => heap.handle(index).and_then(|handle| host.mirror(index, handle, true)),
Into::Ahead(_) => record.as_ref().map(|(view, _)| view.clone()),
};
let placed = view.as_ref().and_then(crate::descriptor::host::placed_texture);
let resource = match placed.as_ref().or(view.as_ref()) {
Some(&View::Shader(resource, _)) => resource as This,
Expand All @@ -1934,7 +1976,18 @@ impl State {
// submission that samples it.
let tabled = crate::texture::is_staged(resource);
if let Some(view) = &view {
readable(list, sampled.kind, view);
match &mut into {
Into::List(list) => readable(list, sampled.kind, view),
Into::Ahead(batch) => {
let Some(list) = batch.list() else {
return;
};
readable(list, sampled.kind, view);
if let (Some(handle), Some((_, key))) = (heap.handle(index), &record) {
batch.publish(index, handle, *key);
}
}
}
}
// A texture view the host refuses removes the device: name it.
static GONE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
Expand Down Expand Up @@ -1980,6 +2033,9 @@ impl State {
};
crate::texture::unstage(resource);
SPENT.fetch_add(bytes, Ordering::Relaxed);
if let Into::Ahead(batch) = &mut into {
batch.uploaded(bytes);
}
since_asked += bytes;
if room && since_asked >= 32 << 20 {
since_asked = 0;
Expand Down Expand Up @@ -2403,7 +2459,7 @@ impl State {
let draws: u32 = census.1.values().sum();
if draws > 0 {
let mut rects: Vec<([i32; 4], u32)> = census.1.iter().map(|(r, n)| (*r, *n)).collect();
rects.sort_unstable_by(|a, b| b.1.cmp(&a.1));
rects.sort_unstable_by_key(|a| std::cmp::Reverse(a.1));
let scissors: Vec<String> = rects
.iter()
.take(5)
Expand Down
Loading
Loading