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
48 changes: 47 additions & 1 deletion crates/victory-gpu/src/copystat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,10 +978,56 @@ fn log_memory() {
tracing::info!(
"memory, windows: the {:.0} MB of windows lie over {:.0} MB of title memory (the rest is \
memory that is in more than one window, and is copied once for each); host lists never \
reset since they were made: {}",
reset since they were made: {}; windows grown (VICTORY_WINDOW_GROW): {} since the start, \
{} taken over and still held {:.0} MB, {} slots of the heap and {} of draws' tables moved \
to the longer one",
mb(copies[3]),
mb(crate::shadow::window_cover()),
lists[3],
crate::shadow::GROWN[0].load(Relaxed),
crate::shadow::taken_over_held().0,
mb(crate::shadow::taken_over_held().1),
crate::shadow::GROWN[1].load(Relaxed),
crate::shadow::GROWN[2].load(Relaxed),
);
if crate::shadow::grow() {
log_heap_elsewhere();
}
}

/// The memory line on the slots the pass over the heap found showing another
/// copy than the one it brings up to date (`VICTORY_WINDOW_GROW`).
fn log_heap_elsewhere() {
use crate::draw::{
HEAP_ELSEWHERE_OUTSIDE, HEAP_ELSEWHERE_SLOTS, HEAP_ELSEWHERE_WHY, HEAP_LOOKS, HEAP_MADE_AGAIN_NULL,
HEAP_VIEWS_ELSEWHERE, HEAP_VIEWS_STILL_ELSEWHERE,
};
// Shown kind by found kind (`crate::shadow::copy_kind`).
let w = |shown: usize, found: usize| HEAP_ELSEWHERE_WHY[shown * 5 + found].load(Relaxed);
let (looks, passes) = (HEAP_LOOKS[0].load(Relaxed), HEAP_LOOKS[1].load(Relaxed));
let elsewhere = HEAP_VIEWS_ELSEWHERE.load(Relaxed);
let named = [w(0, 1), w(1, 1), w(2, 1), w(1, 2), w(1, 3), w(3, 1), w(3, 3), (0..5).map(|f| w(4, f)).sum::<u64>()];
tracing::info!(
"memory, heap slots elsewhere: {elsewhere} in {looks} looks over {passes} passes ({:.3} a pass), \
{} distinct slots; still elsewhere after being made again {}; begin outside their buffer {}; \
shown -> found: taken over -> window {}, window -> window {}, own bytes -> window {}, \
window -> own bytes {}, window -> title buffer {}, title buffer -> window {}, \
title buffer -> title buffer {}, let go -> any {}, other {}; made again on the longer window \
and left null {}",
elsewhere as f64 / passes.max(1) as f64,
HEAP_ELSEWHERE_SLOTS.lock().unwrap_or_else(|e| e.into_inner()).len(),
HEAP_VIEWS_STILL_ELSEWHERE.load(Relaxed),
HEAP_ELSEWHERE_OUTSIDE.load(Relaxed),
named[0],
named[1],
named[2],
named[3],
named[4],
named[5],
named[6],
named[7],
elsewhere.saturating_sub(named.iter().sum()),
HEAP_MADE_AGAIN_NULL.load(Relaxed),
);
}

Expand Down
47 changes: 42 additions & 5 deletions crates/victory-gpu/src/descriptor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4682,12 +4682,39 @@ impl HostHeap {
}

/// Whether host slot `index` shows `buffer`; `None` if it shows no
/// resource (diagnostics: a buffer view's slot must show the host copy
/// that is brought up to date for it).
#[cfg(feature = "diagnostics")]
/// resource (a buffer view's slot must show the host copy that is
/// brought up to date for it: with growing windows the pass over the
/// heap makes it again when it does not).
pub(crate) fn shows(&self, index: u32, buffer: &ID3D12Resource) -> Option<bool> {
let shown = self.kept_in(index)?;
Some(windows::core::Interface::as_raw(&shown) == windows::core::Interface::as_raw(buffer))
use windows::core::Interface;
let kept = self.kept.get(index as usize)?.lock().unwrap_or_else(|e| e.into_inner());
Some(kept.as_ref()?.as_raw() == buffer.as_raw())
}

/// What slot `index` views (diagnostics).
#[cfg(feature = "diagnostics")]
pub(crate) fn shown(&self, index: u32) -> Option<ID3D12Resource> {
self.kept_in(index)
}

/// Whether host slot `index` shows a window of title memory that a longer
/// one took the place of (`crate::shadow::grow`).
pub(crate) fn shows_taken_over(&self, index: u32) -> bool {
crate::shadow::grow()
&& crate::shadow::any_taken_over()
&& self.kept_in(index).is_some_and(|shown| crate::shadow::taken_over(&shown))
}

/// Host slot `index` is made again by the next [`HostHeap::mirror`] of
/// it, as if the title had written it: what it showed is retired when
/// the new view is kept.
pub(crate) fn make_again(&self, index: u32) {
if index >= self.count {
return;
}
let _held = self.made.lock(index);
self.quick_is(index, 0);
self.made.set(index, 0);
}

/// What the queue of textures picked by index knows of title slot
Expand Down Expand Up @@ -4947,6 +4974,16 @@ pub(crate) fn buffer_copy(view: &View) -> Option<(std::sync::Arc<crate::shadow::
Some((view_home(shadow, address, len, view_elements(view)?), address, len))
}

/// Whether buffer view `view` begins outside the buffer it is of: [`make`]
/// and [`buffer_copy`] then look the copy up differently (diagnostics).
#[cfg(feature = "diagnostics")]
pub(crate) fn view_outside_buffer(view: &View) -> bool {
match (buffer_range(view), buffer_extent(view)) {
(Some((address, _)), Some((base, width))) => address < base || address - base >= width.max(1),
_ => false,
}
}

/// [`buffer_range`] of an unordered access view of memory at `base`.
fn unordered_range(base: u64, words: &[u32; 10]) -> Option<(u64, u64)> {
let desc = uav_desc(words);
Expand Down
128 changes: 123 additions & 5 deletions crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,9 +768,33 @@ type RootSlot = (u32, Option<std::sync::Arc<crate::shadow::Shadow>>);
/// the copy the pass brought up to date for them, since the start
/// (diagnostics; for the memory line). It stays 0 while the pass and the
/// slots agree; with `VICTORY_VIEW_EXTENT=0` it counts the views that were
/// read stale.
/// read stale. With growing windows (`crate::shadow::grow`) the pass then
/// makes the slot again; what is counted is what it found before that.
#[cfg(feature = "diagnostics")]
pub(crate) static HEAP_VIEWS_ELSEWHERE: AtomicU64 = AtomicU64::new(0);
// The counters below are kept with growing windows only (diagnostics, for
// the memory line "heap slots elsewhere").
/// Of [`HEAP_VIEWS_ELSEWHERE`], the ones whose slot made again still showed
/// another copy: `make` and `buffer_copy` look the copy up differently.
#[cfg(feature = "diagnostics")]
pub(crate) static HEAP_VIEWS_STILL_ELSEWHERE: AtomicU64 = AtomicU64::new(0);
/// The heap pass's slots that showed another copy, by (kind of the copy
/// shown) * 5 + (kind of the copy found), `crate::shadow::copy_kind`.
#[cfg(feature = "diagnostics")]
pub(crate) static HEAP_ELSEWHERE_WHY: [AtomicU64; 25] = [const { AtomicU64::new(0) }; 25];
/// Of them, the views that begin outside their buffer.
#[cfg(feature = "diagnostics")]
pub(crate) static HEAP_ELSEWHERE_OUTSIDE: AtomicU64 = AtomicU64::new(0);
/// Slots made again on the longer window that show nothing since.
#[cfg(feature = "diagnostics")]
pub(crate) static HEAP_MADE_AGAIN_NULL: AtomicU64 = AtomicU64::new(0);
/// The distinct slots that showed another copy.
#[cfg(feature = "diagnostics")]
pub(crate) static HEAP_ELSEWHERE_SLOTS: std::sync::Mutex<std::collections::BTreeSet<u32>> =
std::sync::Mutex::new(std::collections::BTreeSet::new());
/// Slots looked at, and passes, since the start.
#[cfg(feature = "diagnostics")]
pub(crate) static HEAP_LOOKS: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2];
/// Passes over the view heap's buffer views for shaders that pick them by
/// index ([`State::heap_buffers`]), and the microseconds they took.
pub(crate) static HEAP_BUFFER_PASSES: AtomicU64 = AtomicU64::new(0);
Expand Down Expand Up @@ -880,6 +904,14 @@ impl<T> HeapRanges<T> {
fn is_none(&self, slot: u32) -> bool {
self.index.get(slot as usize).is_none_or(|&idx| idx == u32::MAX)
}

/// The slots whose kept copy `f` picks.
fn slots_where(&self, f: impl Fn(&T) -> bool) -> Vec<u32> {
let mut slots: Vec<u32> = self.dense.iter().filter(|(_, (copy, ..))| f(copy)).map(|&(slot, _)| slot).collect();
// (`dense` is in no order once a slot was removed.)
slots.sort_unstable();
slots
}
}

#[cfg(test)]
Expand Down Expand Up @@ -912,6 +944,22 @@ mod heap_range_tests {
assert_eq!(ranges.ranges().count(), SLOTS.div_ceil(3) as usize - 1);
}

/// The slots whose kept copy is a window taken over are found, and
/// those forgotten for this pass's part are not.
#[test]
fn the_slots_on_a_window_taken_over_are_found() {
let mut ranges = HeapRanges::default();
ranges.set(2, Some((false, 0, 1)));
ranges.set(5, Some((true, 0, 1)));
ranges.set(9, Some((true, 4, 8)));
ranges.set(12, None);
assert_eq!(ranges.slots_where(|&taken| taken), [5, 9]);
assert_eq!(ranges.slots_where(|&taken| !taken), [2]);
// Part 1 of 4 is 1, 5, 9: the pass looks at those anyway.
ranges.forget_part(1, 4);
assert!(ranges.slots_where(|&taken| taken).is_empty());
}

/// The spans of 4,400 copies from the ranges of 72,000 slots, as a pass
/// makes them, by slot and from the table they were in. Run with
/// `--ignored --nocapture`.
Expand Down Expand Up @@ -1403,7 +1451,7 @@ fn heap_buffer_pass(heap: &crate::descriptor::Heap, host: &crate::descriptor::ho
let BufferSweep { passes, ranges, unwatched, last_spans } = &mut *kept;
let changed = host.buffer_changes();
let all = *passes == 0 || (*passes % HEAP_ROUND == 0 && !heap_spread());
let slots = if all {
let mut slots = if all {
ranges.clear();
host.buffer_slots()
} else if heap_spread() {
Expand All @@ -1417,6 +1465,20 @@ fn heap_buffer_pass(heap: &crate::descriptor::Heap, host: &crate::descriptor::ho
changed
};
*passes += 1;
#[cfg(feature = "diagnostics")]
if crate::shadow::grow() {
HEAP_LOOKS[1].fetch_add(1, Ordering::Relaxed);
}
// Slots kept on a window a longer one took the place of are looked at
// now, not at their turn in the round (up to HEAP_ROUND passes later):
// `look` makes them again on the longer one (`crate::shadow::grow`).
// Meanwhile what shaders write through the one is not in the other
// before the copy back and a refresh, a frame late.
if !all && crate::shadow::grow() && crate::shadow::any_taken_over() {
slots.extend(ranges.slots_where(|copy| copy.is_taken_over()));
slots.sort_unstable();
slots.dedup();
}
// Whether the device was there before the pass: a view that removes
// it is named below.
static GONE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
Expand All @@ -1436,17 +1498,57 @@ fn heap_buffer_pass(heap: &crate::descriptor::Heap, host: &crate::descriptor::ho
// What a slot of the title's shows: its copy and range, and whether it is
// an unordered view.
let look = |index: u32| {
#[cfg(feature = "diagnostics")]
if crate::shadow::grow() {
HEAP_LOOKS[0].fetch_add(1, Ordering::Relaxed);
}
// A draw's table's: the draw brings what it reads up to date.
if host.copied_tabled(index, frame) {
return None;
}
let view = host.mirror(index, heap.handle(index)?, true)?;
let mut view = host.mirror(index, heap.handle(index)?, true)?;
// A slot made on a window a longer one took the place of is made
// again on the longer one (`crate::shadow::grow`).
if host.shows_taken_over(index) {
host.make_again(index);
view = host.mirror(index, heap.handle(index)?, true)?;
crate::shadow::GROWN[1].fetch_add(1, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
if host.shown(index).is_none() {
HEAP_MADE_AGAIN_NULL.fetch_add(1, Ordering::Relaxed);
}
}
// The copy the slot shows (`buffer_copy`), not the one the
// view's range alone would find.
let (shadow, address, len) = crate::descriptor::host::buffer_copy(&view)?;
let (mut shadow, mut address, mut len) = crate::descriptor::host::buffer_copy(&view)?;
#[cfg(feature = "diagnostics")]
if host.shows(index, &shadow.buffer) == Some(false) {
HEAP_VIEWS_ELSEWHERE.fetch_add(1, Ordering::Relaxed);
if crate::shadow::grow() {
if let Some(shown) = host.shown(index) {
let why = crate::shadow::copy_kind(&shown) * 5 + crate::shadow::copy_kind(&shadow.buffer);
HEAP_ELSEWHERE_WHY[why].fetch_add(1, Ordering::Relaxed);
}
if crate::descriptor::host::view_outside_buffer(&view) {
HEAP_ELSEWHERE_OUTSIDE.fetch_add(1, Ordering::Relaxed);
}
HEAP_ELSEWHERE_SLOTS.lock().unwrap_or_else(|e| e.into_inner()).insert(index);
}
}
// With growing windows the slot may show another copy than the one
// brought up to date for it: a takeover between the look above and
// here, a view's own bytes whose window moved (`view_home` counts
// from the window's start), a title buffer made or let go since the
// slot was. Made again once, on what is found now; what it showed is
// retired by `keep`.
if crate::shadow::grow() && host.shows(index, &shadow.buffer) == Some(false) {
host.make_again(index);
view = host.mirror(index, heap.handle(index)?, true)?;
(shadow, address, len) = crate::descriptor::host::buffer_copy(&view)?;
#[cfg(feature = "diagnostics")]
if host.shows(index, &shadow.buffer) == Some(false) {
HEAP_VIEWS_STILL_ELSEWHERE.fetch_add(1, Ordering::Relaxed);
}
}
let written = crate::picked::on() && view_kind(&view) == Some(ResourceType::Uav);
Some(((shadow, address, address + len), written))
Expand Down Expand Up @@ -4915,7 +5017,23 @@ impl State {
let (Some(host), Some(index)) = (heap.host(), heap.index(handle)) else {
return;
};
if let Some(view) = host.mirror(index, handle, true) {
if let Some(mut view) = host.mirror(index, handle, true) {
// A slot made on a window a longer one took the place of is made
// again on the longer one here, as the pass over the heap does
// for the slots it looks at (`crate::shadow::grow`): the pass
// leaves a draw's table to the draw, and the slot would show the
// shorter window until the title wrote it again, while root
// views, vertex buffers and the slots made since use the longer
// one. What shaders write through the one is in the other only
// after the copy back and a refresh: a frame late.
if host.shows_taken_over(index) {
host.make_again(index);
match host.mirror(index, handle, true) {
Some(again) => view = again,
None => return,
}
crate::shadow::GROWN[2].fetch_add(1, Ordering::Relaxed);
}
readable(list, u.kind, &view);
// Up to date by this draw: not the texture pass's to make.
if crate::descriptor::host::is_texture_view(&view) {
Expand Down
Loading
Loading