gpu: use AddressHash for Deferred::textures, fast-skip declared_writes, dedup card_before root bindings, and cache Batch::last_ahead

This commit is contained in:
theme613 committed 2026-11-28 20:03:01 +08:00
1 parent 3f3e525d55
commit 5d65b5d333
1 file changed
+47 -12
+47 -12
View File
@@ -2716,7 +2716,10 @@ struct Deferred {
/// wrote title memory (source, bytes): its host address.
filled: std::collections::HashMap<(usize, usize), u64, std::hash::BuildHasherDefault<AddressHash>>,
/// Textures whose contents the list brings up to date.
textures: std::collections::HashSet<usize>,
textures: std::collections::HashSet<usize, std::hash::BuildHasherDefault<AddressHash>>,
/// Whether `copies` contains at least one `Step::GpuWrite` (lets
/// `declared_writes` skip lists that only record refreshes/copies).
has_gpu_write: bool,
/// Bottom-of-pipe writes, done once the list has run: address, what,
/// bytes.
writes: Vec<(usize, crate::queue::Written, usize)>,
@@ -4329,7 +4332,11 @@ fn declared_writes(runs: &[&HostList]) -> Vec<Declared> {
let mut places: ByCopy<usize> = ByCopy::default();
let mut seen = std::collections::HashSet::<(usize, usize, usize), std::hash::BuildHasherDefault<AddressHash>>::default();
for list in runs {
for step in &list.deferred().copies {
let deferred = list.deferred();
if !deferred.has_gpu_write {
continue;
}
for step in &deferred.copies {
if let Step::GpuWrite(shadow, address, len) = step {
let key = std::sync::Arc::as_ptr(shadow) as usize;
if !seen.insert((key, *address, *len)) {
@@ -5023,16 +5030,27 @@ impl Host {
list.SetComputeRootSignature(&work.root);
list.SetPipelineState(&work.pso);
list.SetComputeRootUnorderedAccessView(3, work.differs.0.GetGPUVirtualAddress());
let mut last_shadow = 0usize;
let mut last_size = 0u32;
let mut passes = 0u64;
for r in plan.ranges.iter().filter(|r| r.compared) {
let (Some((_, Some(before), _)), size) = (r.shadow.card_sides(), r.shadow.card_hash_size()) else {
continue;
};
list.SetComputeRootShaderResourceView(1, r.shadow.buffer.GetGPUVirtualAddress());
list.SetComputeRootUnorderedAccessView(2, before.GetGPUVirtualAddress());
let key = Arc::as_ptr(&r.shadow) as usize;
if key != last_shadow {
let (Some((_, Some(before), _)), size) = (r.shadow.card_sides(), r.shadow.card_hash_size()) else {
continue;
};
list.SetComputeRootShaderResourceView(1, r.shadow.buffer.GetGPUVirtualAddress());
list.SetComputeRootUnorderedAccessView(2, before.GetGPUVirtualAddress());
last_shadow = key;
last_size = size as u32;
}
// (What is only looked at keeps its hashes: what
// changed there is the next declared range's to land.)
card_pass(list, &r.span, size as u32, r.at, if flown && !r.picked { 2 } else { 1 });
CARD_PASSES[2].fetch_add(1, Ordering::Relaxed);
card_pass(list, &r.span, last_size, r.at, if flown && !r.picked { 2 } else { 1 });
passes += 1;
}
if passes != 0 {
CARD_PASSES[2].fetch_add(passes, Ordering::Relaxed);
}
list.Close()?;
list.cast()
@@ -7718,6 +7736,7 @@ impl HostList {
let mut deferred = self.deferred();
let key = (std::sync::Arc::as_ptr(shadow) as usize, address, len);
if deferred.gpu_written.insert(key) || !declare_once() {
deferred.has_gpu_write = true;
deferred.copies.push(Step::GpuWrite(shadow.clone(), address, len));
}
}
@@ -8584,6 +8603,9 @@ struct Batch {
/// Copies (by address) brought up to date ahead of the steps, and the
/// range (from, to) ([`Batch::refresh_ahead`]).
ahead: ByCopy<(usize, usize)>,
/// One-entry cache of the last hit in `ahead` (`(key, from, to)`), avoiding
/// repeated HashMap lookups for consecutive refreshes on the same buffer.
last_ahead: (usize, usize, usize),
/// `ahead` holds everything the submission refreshes
/// ([`Batch::refresh_front`]).
front: bool,
@@ -8853,6 +8875,7 @@ impl Batch {
self.placed.clear();
self.fills.clear();
self.ahead.clear();
self.last_ahead = (0, 0, 0);
self.front = false;
self.spans.clear();
self.spans_at.clear();
@@ -8917,8 +8940,15 @@ impl Batch {
}
let end = address + len;
let key = std::sync::Arc::as_ptr(shadow) as usize;
if self.ahead.get(&key).is_some_and(|&(from, to)| from <= address && end <= to) {
return;
if self.last_ahead.0 == key && key != 0 {
if self.last_ahead.1 <= address && end <= self.last_ahead.2 {
return;
}
} else if let Some(&(from, to)) = self.ahead.get(&key) {
self.last_ahead = (key, from, to);
if from <= address && end <= to {
return;
}
}
match self.index.get(&key) {
Some(&at) => {
@@ -8938,7 +8968,11 @@ impl Batch {
// From here on it is refreshed around what the shaders write. (What
// is in front of all the steps was, up to here.)
if !self.front {
self.ahead.remove(&(std::sync::Arc::as_ptr(shadow) as usize));
let key = std::sync::Arc::as_ptr(shadow) as usize;
self.ahead.remove(&key);
if self.last_ahead.0 == key {
self.last_ahead = (0, 0, 0);
}
}
let Some(at) = self.index.remove(&(std::sync::Arc::as_ptr(shadow) as usize)) else {
return;
@@ -8981,6 +9015,7 @@ impl Batch {
fn title_written(&mut self) {
self.flush();
self.ahead.clear();
self.last_ahead = (0, 0, 0);
self.textures.clear();
}