Host heaps' slots are made under locks of their own; the heaps' records and the buffer views picked by index are made on several threads (VICTORY_VIEW_THREADS)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
4fa965582f
commit
4d4d9bcee6
5 files changed
+206
-107
No files matched your search
@@ -13,6 +13,45 @@ use windows::Win32::Graphics::Dxgi::Common::*;
|
||||
use super::{VIEW_TAG, View, view_at};
|
||||
use crate::host::host;
|
||||
|
||||
/// The record each slot of a heap was last made from, and a lock for every
|
||||
/// few hundredth of the slots: whoever makes a slot holds its lock, and no
|
||||
/// other.
|
||||
///
|
||||
/// One lock for the heap was what everything of a frame waited for once the
|
||||
/// lists were translated by threads of ours: half of what those did, two
|
||||
/// thirds of the heaps' records and half of the pass over the buffer views
|
||||
/// (profile thin-2).
|
||||
struct Made {
|
||||
keys: Vec<std::sync::atomic::AtomicU64>,
|
||||
locks: [Mutex<()>; Self::LOCKS],
|
||||
}
|
||||
|
||||
impl Made {
|
||||
const LOCKS: usize = 512;
|
||||
|
||||
fn new(count: usize) -> Self {
|
||||
let mut keys = Vec::new();
|
||||
keys.resize_with(count, Default::default);
|
||||
Made { keys, locks: [const { Mutex::new(()) }; Self::LOCKS] }
|
||||
}
|
||||
|
||||
/// The lock slot `index` is made under. Neighbours have different ones.
|
||||
fn lock(&self, index: u32) -> std::sync::MutexGuard<'_, ()> {
|
||||
self.locks[index as usize % Self::LOCKS].lock().unwrap_or_else(|e| e.into_inner())
|
||||
}
|
||||
|
||||
fn get(&self, index: u32) -> Option<u64> {
|
||||
Some(self.keys.get(index as usize)?.load(std::sync::atomic::Ordering::Acquire))
|
||||
}
|
||||
|
||||
/// Under the slot's lock.
|
||||
fn set(&self, index: u32, key: u64) {
|
||||
if let Some(k) = self.keys.get(index as usize) {
|
||||
k.store(key, std::sync::atomic::Ordering::Release);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct HostHeap {
|
||||
pub(crate) heap: ID3D12DescriptorHeap,
|
||||
cpu: usize,
|
||||
@@ -33,7 +72,7 @@ pub(crate) struct HostHeap {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
written: Vec<std::sync::atomic::AtomicU32>,
|
||||
/// The record (view index and generation) each slot was last made from.
|
||||
made: Mutex<Vec<u64>>,
|
||||
made: Made,
|
||||
/// 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>,
|
||||
@@ -1406,7 +1445,7 @@ impl HostHeap {
|
||||
written: (0..if sampler || !crate::book::on() { 0 } else { size })
|
||||
.map(|_| std::sync::atomic::AtomicU32::new(0))
|
||||
.collect(),
|
||||
made: Mutex::new(vec![0; count as usize]),
|
||||
made: Made::new(count as usize),
|
||||
quick: (0..count).map(|_| std::sync::atomic::AtomicU64::new(0)).collect(),
|
||||
copied: Mutex::new(Vec::new()),
|
||||
waiting: Mutex::default(),
|
||||
@@ -1802,11 +1841,12 @@ impl HostHeap {
|
||||
// from a view the host could not make (`make` gave nothing, the
|
||||
// slot is null and keeps nothing). The host copy of such a
|
||||
// texture is held by no slot, may have been let go, and would be
|
||||
// made anew here, with the card full. `made` first, then `kept`,
|
||||
// as `mirror` takes them.
|
||||
let made = self.made.lock().unwrap_or_else(|e| e.into_inner());
|
||||
made.get(index as usize).is_some_and(|&made| made != 0 && made == key)
|
||||
&& self.kept_in(index).is_some()
|
||||
// made anew here, with the card full.
|
||||
if key == 0 || self.made.get(index) != Some(key) {
|
||||
return false;
|
||||
}
|
||||
let _made = self.made.lock(index);
|
||||
self.made.get(index) == Some(key) && self.kept_in(index).is_some()
|
||||
}
|
||||
|
||||
/// Levels on demand (`crate::tiles`): makes again the host slots of the
|
||||
@@ -1897,15 +1937,12 @@ impl HostHeap {
|
||||
let placed = self.placed.lock().unwrap_or_else(|e| e.into_inner());
|
||||
starts.iter().flat_map(|&start| placed.slots_at(start)).collect()
|
||||
};
|
||||
let made = self.made.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut remade = 0;
|
||||
for slot in slots {
|
||||
let Some(&key) = made.get(slot as usize) else {
|
||||
let _made = self.made.lock(slot);
|
||||
let Some(key) = self.made.get(slot).filter(|&key| key != 0) else {
|
||||
continue;
|
||||
};
|
||||
if key == 0 {
|
||||
continue;
|
||||
}
|
||||
let Some(view) = view_at(key as u32, (key >> 32) as u32) else {
|
||||
continue;
|
||||
};
|
||||
@@ -1935,8 +1972,8 @@ impl HostHeap {
|
||||
let wait = *WAIT.get_or_init(|| {
|
||||
std::env::var("VICTORY_TEXTURE_AWAY_MS").ok().and_then(|v| v.trim().parse().ok()).unwrap_or(AWAY_MS)
|
||||
});
|
||||
let mut made = self.made.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let Some(key) = made.get(index as usize).copied().filter(|&key| key != 0) else {
|
||||
let _made = self.made.lock(index);
|
||||
let Some(key) = self.made.get(index).filter(|&key| key != 0) else {
|
||||
return false;
|
||||
};
|
||||
let now = crate::host::now_ms();
|
||||
@@ -1954,7 +1991,7 @@ impl HostHeap {
|
||||
}
|
||||
}
|
||||
}
|
||||
self.unmake(&mut made, index);
|
||||
self.unmake(index);
|
||||
STALE[4].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
true
|
||||
}
|
||||
@@ -1963,8 +2000,7 @@ impl HostHeap {
|
||||
/// at `handle` views, by an earlier view of it.
|
||||
pub(crate) fn texture_slot_shows_other(&self, index: u32, handle: u64) -> bool {
|
||||
let key = record_key(handle);
|
||||
let made = self.made.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let Some(&shown) = made.get(index as usize) else {
|
||||
let Some(shown) = self.made.get(index) else {
|
||||
return false;
|
||||
};
|
||||
if key == 0 || shown == 0 || shown == key {
|
||||
@@ -1992,33 +2028,31 @@ impl HostHeap {
|
||||
let (true, Some(view)) = (key != 0, view) else {
|
||||
return;
|
||||
};
|
||||
{
|
||||
let mut made = self.made.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let Some(&shown) = made.get(index as usize) else {
|
||||
let stale = {
|
||||
let _made = self.made.lock(index);
|
||||
let Some(shown) = self.made.get(index) else {
|
||||
return;
|
||||
};
|
||||
if shown == key {
|
||||
return;
|
||||
}
|
||||
// (As `mirror`.)
|
||||
for stale in self.note_buffer(index, Some(&view), true) {
|
||||
if stale != index {
|
||||
self.unmake(&mut made, stale);
|
||||
}
|
||||
}
|
||||
let stale = self.note_buffer(index, Some(&view), true);
|
||||
let old = view_at(shown as u32, (shown >> 32) as u32);
|
||||
if shown == 0 || !old.is_some_and(|old| same_placed_texture(&old, &view)) {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
note_before(index, shown, "neutral");
|
||||
self.quick_is(index, 0);
|
||||
made[index as usize] = 0;
|
||||
self.made.set(index, 0);
|
||||
let kept = neutral_for(&view, self.at(index));
|
||||
self.put(index);
|
||||
self.clear_twin(index);
|
||||
LATE[3].fetch_add(u64::from(kept.is_some()), Relaxed);
|
||||
self.keep(index, kept);
|
||||
}
|
||||
}
|
||||
stale
|
||||
};
|
||||
self.unmake_stale(index, stale);
|
||||
self.flip(list, index, handle, Some(crate::frame::current()));
|
||||
}
|
||||
|
||||
@@ -2065,18 +2099,18 @@ impl HostHeap {
|
||||
ready
|
||||
};
|
||||
let mut flipped = 0;
|
||||
let mut made = self.made.lock().unwrap_or_else(|e| e.into_inner());
|
||||
for f in ready {
|
||||
let Some(shown) = made.get_mut(f.index as usize) else {
|
||||
let _made = self.made.lock(f.index);
|
||||
let Some(shown) = self.made.get(f.index) else {
|
||||
continue;
|
||||
};
|
||||
if *shown == f.key {
|
||||
if shown == f.key {
|
||||
continue;
|
||||
}
|
||||
// Written again since, or made null: that write is in the queue.
|
||||
// (A slot shown late is null until now.)
|
||||
let view = view_at(f.key as u32, (f.key >> 32) as u32);
|
||||
let there = *shown != 0 || f.late.is_some();
|
||||
let there = shown != 0 || f.late.is_some();
|
||||
let (true, true, Some(view)) = (f.key != 0 && record_key(f.handle) == f.key, there, view) else {
|
||||
FLIPPED[1].fetch_add(1, Relaxed);
|
||||
LATE[4].fetch_add(u64::from(f.late.is_some()), Relaxed);
|
||||
@@ -2095,9 +2129,9 @@ impl HostHeap {
|
||||
}
|
||||
if let Some(kept) = make(&view, self.at(f.index)) {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
note_before(f.index, *shown, if f.late.is_some() { "shown after its upload" } else { "flip" });
|
||||
note_before(f.index, shown, if f.late.is_some() { "shown after its upload" } else { "flip" });
|
||||
self.put(f.index);
|
||||
*shown = f.key;
|
||||
self.made.set(f.index, f.key);
|
||||
self.keep(f.index, kept);
|
||||
if let Some(since) = f.late {
|
||||
let frames = crate::frame::current().saturating_sub(since);
|
||||
@@ -2131,12 +2165,13 @@ 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) {
|
||||
/// made: a null view of the kind it held. Under its lock ([`Made`]).
|
||||
fn unmake(&self, index: u32) {
|
||||
self.quick_is(index, 0);
|
||||
let Some(key) = made.get_mut(index as usize).map(std::mem::take) else {
|
||||
let Some(key) = self.made.get(index) else {
|
||||
return;
|
||||
};
|
||||
self.made.set(index, 0);
|
||||
if key == 0 {
|
||||
return;
|
||||
}
|
||||
@@ -2149,6 +2184,21 @@ impl HostHeap {
|
||||
self.keep(index, None);
|
||||
}
|
||||
|
||||
/// The slots [`HostHeap::note_buffer`] found stale for slot `index`, with
|
||||
/// what they were made from then, become null. None of their locks is
|
||||
/// held here; one made from another record meanwhile is left as it is.
|
||||
fn unmake_stale(&self, index: u32, stale: Vec<(u32, u64)>) {
|
||||
for (slot, key) in stale {
|
||||
if slot == index || key == 0 {
|
||||
continue;
|
||||
}
|
||||
let _made = self.made.lock(slot);
|
||||
if self.made.get(slot) == Some(key) {
|
||||
self.unmake(slot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Title slot `index` is a table's ([`HostHeap::tabled`]). `read`: a
|
||||
/// draw read it through the table and brought what it views up to
|
||||
/// date, so the slot leaves the queue of textures picked by index; the
|
||||
@@ -2258,9 +2308,11 @@ impl HostHeap {
|
||||
/// `seen`: the slot was not written now, a draw or a pass made its
|
||||
/// host slot from what it held.
|
||||
/// Returns the title slots whose placed texture view the write made
|
||||
/// stale ([`Spans`]): out of the queue of textures already, their host
|
||||
/// slots are the caller's to make null (it holds `made`).
|
||||
fn note_buffer(&self, index: u32, view: Option<&View>, seen: bool) -> Vec<u32> {
|
||||
/// stale ([`Spans`]), each with the record its host slot is made from:
|
||||
/// out of the queue of textures already, their host slots are the
|
||||
/// caller's to make null when it has let go of its own slot's lock
|
||||
/// ([`HostHeap::unmake_stale`]).
|
||||
fn note_buffer(&self, index: u32, view: Option<&View>, seen: bool) -> Vec<(u32, u64)> {
|
||||
if self.sampler {
|
||||
return Vec::new();
|
||||
}
|
||||
@@ -2310,7 +2362,9 @@ impl HostHeap {
|
||||
placed.rule = stale_rule();
|
||||
let stale = placed.note(index, start, end, !seen);
|
||||
placed.shows(index, first, held);
|
||||
stale
|
||||
// (Under the lock of `placed`: a later write of one of
|
||||
// them comes by here before it makes the slot.)
|
||||
stale.into_iter().map(|slot| (slot, self.made.get(slot).unwrap_or(0))).collect()
|
||||
}
|
||||
None => {
|
||||
placed.forget(index);
|
||||
@@ -2321,7 +2375,7 @@ impl HostHeap {
|
||||
if !stale.is_empty() {
|
||||
STALE[0].fetch_add(stale.len() as u64, std::sync::atomic::Ordering::Relaxed);
|
||||
let mut textures = self.textures.lock().unwrap_or_else(|e| e.into_inner());
|
||||
for &slot in &stale {
|
||||
for &(slot, _) in &stale {
|
||||
textures.remove(slot);
|
||||
}
|
||||
}
|
||||
@@ -2821,14 +2875,15 @@ impl HostHeap {
|
||||
{
|
||||
return Some(view);
|
||||
}
|
||||
let mut made = self.made.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let held = self.made.lock(index);
|
||||
let shown = self.made.get(index).unwrap_or(0);
|
||||
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 {
|
||||
if shown != 0 {
|
||||
self.quick_is(index, 0);
|
||||
made[index as usize] = 0;
|
||||
self.made.set(index, 0);
|
||||
self.make_null(index);
|
||||
let _ = self.note_buffer(index, None, false);
|
||||
} else if for_draw {
|
||||
@@ -2837,15 +2892,12 @@ impl HostHeap {
|
||||
}
|
||||
return None;
|
||||
};
|
||||
if made[index as usize] != key {
|
||||
if shown != key {
|
||||
// Views of other textures in the memory this one describes
|
||||
// are stale: their host slots read nothing from here on, and
|
||||
// let go of the host copies they held.
|
||||
for stale in self.note_buffer(index, Some(&view), for_draw) {
|
||||
if stale != index {
|
||||
self.unmake(&mut made, stale);
|
||||
}
|
||||
}
|
||||
// (When this slot's lock is let go: a lock at a time.)
|
||||
let stale = self.note_buffer(index, Some(&view), for_draw);
|
||||
// A view of a texture object that has no host copy yet waits
|
||||
// for it, as buffer views wait for a draw: the slot is made
|
||||
// when the texture is first used (`super::make_waiting`), or
|
||||
@@ -2864,18 +2916,20 @@ impl HostHeap {
|
||||
// time it got sharper.
|
||||
if later
|
||||
&& keep_views()
|
||||
&& view_at(made[index as usize] as u32, (made[index as usize] >> 32) as u32)
|
||||
&& view_at(shown as u32, (shown >> 32) as u32)
|
||||
.is_some_and(|old| same_placed_texture(&old, &view))
|
||||
{
|
||||
KEPT_VIEWS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
drop(held);
|
||||
self.unmake_stale(index, stale);
|
||||
return Some(view);
|
||||
}
|
||||
#[cfg(feature = "diagnostics")]
|
||||
if !self.sampler && is_texture_view(&view) {
|
||||
note_before(index, made[index as usize], if later { "null, waits" } else { "at once" });
|
||||
note_before(index, shown, if later { "null, waits" } else { "at once" });
|
||||
}
|
||||
self.quick_is(index, 0);
|
||||
made[index as usize] = if later { 0 } else { key };
|
||||
self.made.set(index, if later { 0 } else { key });
|
||||
let kept = if later {
|
||||
None
|
||||
} else {
|
||||
@@ -2889,7 +2943,9 @@ impl HostHeap {
|
||||
}
|
||||
self.twin_made(index, kept.is_some() && twinned(&view));
|
||||
self.keep(index, kept.flatten());
|
||||
self.quick_is(index, made[index as usize]);
|
||||
self.quick_is(index, if later { 0 } else { key });
|
||||
drop(held);
|
||||
self.unmake_stale(index, stale);
|
||||
}
|
||||
Some(view)
|
||||
}
|
||||
@@ -2905,11 +2961,7 @@ impl HostHeap {
|
||||
/// Whether slot `index` is backed by a made host descriptor (tests).
|
||||
#[cfg(test)]
|
||||
pub(crate) fn made(&self, index: u32) -> bool {
|
||||
self.made
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.get(index as usize)
|
||||
.is_some_and(|&key| key != 0)
|
||||
self.made.get(index).is_some_and(|key| key != 0)
|
||||
}
|
||||
|
||||
/// Whether host slot `index` shows `buffer`; `None` if it shows no
|
||||
@@ -2944,7 +2996,7 @@ impl HostHeap {
|
||||
/// a slot never made or left null.
|
||||
#[cfg(feature = "diagnostics")]
|
||||
pub(crate) fn describe_slot(&self, index: u32) -> Option<String> {
|
||||
let key = *self.made.lock().unwrap_or_else(|e| e.into_inner()).get(index as usize)?;
|
||||
let key = self.made.get(index)?;
|
||||
if key == 0 {
|
||||
return Some("null".into());
|
||||
}
|
||||
@@ -4920,7 +4972,7 @@ mod tests {
|
||||
assert_eq!(slots(50), []);
|
||||
assert_eq!(slots(51), [1]);
|
||||
// A wider texture written at the same address: slot 1 is stale.
|
||||
assert_eq!(heap.note_buffer(2, Some(&view(at, 128)), false), [1]);
|
||||
assert_eq!(heap.note_buffer(2, Some(&view(at, 128)), false), [(1, 0)]);
|
||||
assert_eq!(slots(u64::MAX), [2]);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user