Sets of heap slots are bits, the heaps' records have threads of their own, what is mirrored of them is counted

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 14:01:36 -06:00
1 parent 79615b78a5
commit c3e643ebbd
4 files changed
+134 -31

No files matched your search

+89 -17
View File
@@ -13,6 +13,65 @@ use windows::Win32::Graphics::Dxgi::Common::*;
use super::{VIEW_TAG, View, view_at};
use crate::host::host;
/// A set of a heap's slots, a bit for each: no lock (a pass that went through
/// a set of 70,000 under its lock kept everyone who writes slots waiting for
/// 5 ms of a frame, timeline thin-6).
struct Slots(Vec<std::sync::atomic::AtomicU64>);
impl Slots {
fn new(count: usize) -> Self {
let mut bits = Vec::new();
bits.resize_with(count.div_ceil(64), Default::default);
Slots(bits)
}
fn put(&self, slot: u32) {
if let Some(bits) = self.0.get(slot as usize / 64) {
bits.fetch_or(1 << (slot % 64), std::sync::atomic::Ordering::AcqRel);
}
}
/// Whether it was there.
fn remove(&self, slot: u32) -> bool {
use std::sync::atomic::Ordering::{AcqRel, Acquire};
let bit = 1 << (slot % 64);
self.0.get(slot as usize / 64).is_some_and(|bits| {
bits.load(Acquire) & bit != 0 && bits.fetch_and(!bit, AcqRel) & bit != 0
})
}
fn of(n: usize, mut bits: u64, out: &mut Vec<u32>, which: &impl Fn(u32) -> bool) {
while bits != 0 {
let slot = n as u32 * 64 + bits.trailing_zeros();
bits &= bits - 1;
if which(slot) {
out.push(slot);
}
}
}
/// Those that `which` says, in order.
fn all(&self, which: impl Fn(u32) -> bool) -> Vec<u32> {
let mut out = Vec::new();
for (n, bits) in self.0.iter().enumerate() {
Self::of(n, bits.load(std::sync::atomic::Ordering::Acquire), &mut out, &which);
}
out
}
/// All of them, and none is left.
fn take(&self) -> Vec<u32> {
use std::sync::atomic::Ordering::{AcqRel, Acquire};
let mut out = Vec::new();
for (n, bits) in self.0.iter().enumerate() {
if bits.load(Acquire) != 0 {
Self::of(n, bits.swap(0, AcqRel), &mut out, &|_| true);
}
}
out
}
}
/// 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.
@@ -123,10 +182,10 @@ pub(crate) struct HostHeap {
/// (and its memory a host copy) only when something reads it: a draw
/// through a table, or [`HostHeap::buffer_slots`] for shaders that pick
/// buffers from the whole heap.
buffers: Mutex<std::collections::BTreeSet<u32>>,
buffers: Slots,
/// Of those and of the slots that left the set: the ones written since
/// [`HostHeap::buffer_changes`] last took them.
changed: Mutex<std::collections::HashSet<u32>>,
changed: Slots,
/// Title slots holding a view of a texture the title may fill itself (a
/// placed texture, a texture object), with the frame from which they
/// are due: such a texture is uploaded when a draw reads it through a
@@ -1487,8 +1546,8 @@ impl HostHeap {
free: std::sync::atomic::AtomicU32::new(extra),
too_long: std::sync::atomic::AtomicU32::new(u32::MAX),
strides,
buffers: Mutex::default(),
changed: Mutex::default(),
buffers: Slots::new(count as usize),
changed: Slots::new(count as usize),
textures: Mutex::default(),
tabled: (0..if sampler { 0 } else { count })
.map(|_| std::sync::atomic::AtomicU32::new(0))
@@ -2351,22 +2410,19 @@ impl HostHeap {
/// The title slots that hold buffer views.
pub(crate) fn buffer_slots(&self) -> Vec<u32> {
let buffers = self.buffers.lock().unwrap_or_else(|e| e.into_inner());
buffers.iter().copied().collect()
self.buffers.all(|_| true)
}
/// Of [`HostHeap::buffer_slots`], those of one of `parts` parts: every
/// slot is in the same part each time.
pub(crate) fn buffer_slots_of(&self, part: u32, parts: u32) -> Vec<u32> {
let buffers = self.buffers.lock().unwrap_or_else(|e| e.into_inner());
buffers.iter().copied().filter(|slot| slot % parts == part).collect()
self.buffers.all(|slot| slot % parts == part)
}
/// The title slots whose buffer view was written, or went, since the
/// last call.
pub(crate) fn buffer_changes(&self) -> Vec<u32> {
let mut changed = self.changed.lock().unwrap_or_else(|e| e.into_inner());
std::mem::take(&mut *changed).into_iter().collect()
self.changed.take()
}
/// Records what title slot `index` now holds for shaders that pick
@@ -2455,18 +2511,14 @@ impl HostHeap {
// dword, either value is one the slot had).
unsafe { (*data as *mut u32).add(index as usize).write_volatile(stride.unwrap_or(0) | kind) };
}
let mut buffers = self.buffers.lock().unwrap_or_else(|e| e.into_inner());
let was = if stride.is_some() {
buffers.insert(index);
self.buffers.put(index);
true
} else {
buffers.remove(&index)
self.buffers.remove(index)
};
if was {
self.changed
.lock()
.unwrap_or_else(|e| e.into_inner())
.insert(index);
self.changed.put(index);
}
stale
}
@@ -2889,6 +2941,13 @@ impl HostHeap {
stamp != 0 && now.wrapping_sub(stamp) & !TABLED_READ <= COPIED_FRAMES
}
/// Whether a slot within 64 of `index` is a table's lately (counted for
/// the log: such a slot is likely one of a table not read yet).
pub(crate) fn near_tabled(&self, index: u32, frame: u64) -> bool {
let first = index & !63;
(first..first + 64).any(|i| i < self.count && self.copied_tabled(i, frame))
}
/// New records the title copied into slots `slots` in `frame` wait for
/// the draw that may read them through a table.
pub(crate) fn copied_wait(&self, frame: u64, slots: Vec<u32>) {
@@ -5267,6 +5326,19 @@ mod tests {
assert_eq!(slots(u64::MAX), [2]);
}
#[test]
fn a_set_of_slots_holds_what_was_put_in() {
let set = Slots::new(200);
for slot in [199, 0, 64, 63, 130, 64] {
set.put(slot);
}
assert_eq!(set.all(|_| true), [0, 63, 64, 130, 199]);
assert_eq!(set.all(|slot| slot % 2 == 0), [0, 64, 130]);
assert!(set.remove(64) && !set.remove(64) && !set.remove(5));
assert_eq!(set.take(), [0, 63, 130, 199]);
assert!(set.take().is_empty());
}
/// A slot a draw read through a table leaves the queue; the title's
/// next write of it waits for its draw, and is due after the grace
/// frames if none reads it.