gpu: incremental HeapRanges spans, chunked wide_each refreshes, and zero-alloc prelude/front_spans

This commit is contained in:
theme613 committed 2026-11-28 16:07:21 +08:00
1 parent 6f34121d31
commit 8503191734
3 files changed
+361 -159

No files matched your search

+161 -105
View File
@@ -7362,97 +7362,115 @@ impl HostList {
const COPY_SOURCE: u32 = 0x800;
let host_state = |state: u32| D3D12_RESOURCE_STATES(state as i32);
let states = self.states.lock().unwrap_or_else(|e| e.into_inner());
// The copies (depth texture, twin, whether into the twin); the
// sides are then as this list leaves them.
let mut syncs = Vec::new();
let twins = self.twins.lock().unwrap_or_else(|e| e.into_inner());
for u in twins.values() {
let real = u.twin.behind.load(Ordering::Relaxed);
let mut behind = [real & 1 != 0, real & 2 != 0];
for side in [0, 1] {
if u.needs[side] && behind[side] {
syncs.push((u.depth.clone(), u.twin.clone(), side == 1));
behind = [false; 2];
// Fast path (~95% of lists): no depth-twin syncs and no prelude
// texture uploads, so avoid allocating sync/aliasing/copying HashMap.
let (syncs, to_copy, aliasing, barriers) = if twins.is_empty() && uploads.is_empty() {
drop(twins);
let mut barriers = Vec::new();
for (_, (first, last, copy)) in states.iter() {
let real = copy.state.swap(*last, Ordering::Relaxed);
if real != *first {
barriers.push(transition(&copy.texture, host_state(real), host_state(*first)));
}
}
let left = [0, 1].map(|side| u.behind[side].unwrap_or(behind[side]));
let left = u32::from(left[0]) | (u32::from(left[1]) << 1);
u.twin.behind.store(left, Ordering::Relaxed);
}
drop(twins);
// A copy runs with its textures in the copy states: each moves
// there from its real state, and from there to the state the list
// starts with (back to its real state if the list does not use it).
let mut to_copy = Vec::new();
let mut barriers = Vec::new();
let mut copying = std::collections::HashMap::new();
for (depth, twin, colour) in &syncs {
let (from, to) = if *colour {
(depth, &twin.colour)
} else {
(&twin.colour, depth)
};
for (texture, state) in [(from, COPY_SOURCE), (to, COPY_DEST)] {
let key = std::sync::Arc::as_ptr(texture) as usize;
let real = texture.state.load(Ordering::Relaxed);
copying.insert(key, state);
if real != state {
to_copy.push(transition(
&texture.texture,
host_state(real),
host_state(state),
));
if !states.contains_key(&key) {
barriers.push(transition(
if barriers.is_empty() {
return None;
}
(Vec::new(), Vec::new(), Vec::new(), barriers)
} else {
// The copies (depth texture, twin, whether into the twin); the
// sides are then as this list leaves them.
let mut syncs = Vec::new();
for u in twins.values() {
let real = u.twin.behind.load(Ordering::Relaxed);
let mut behind = [real & 1 != 0, real & 2 != 0];
for side in [0, 1] {
if u.needs[side] && behind[side] {
syncs.push((u.depth.clone(), u.twin.clone(), side == 1));
behind = [false; 2];
}
}
let left = [0, 1].map(|side| u.behind[side].unwrap_or(behind[side]));
let left = u32::from(left[0]) | (u32::from(left[1]) << 1);
u.twin.behind.store(left, Ordering::Relaxed);
}
drop(twins);
// A copy runs with its textures in the copy states: each moves
// there from its real state, and from there to the state the list
// starts with (back to its real state if the list does not use it).
let mut to_copy = Vec::new();
let mut barriers = Vec::new();
let mut copying: ByCopy<u32> = ByCopy::default();
for (depth, twin, colour) in &syncs {
let (from, to) = if *colour {
(depth, &twin.colour)
} else {
(&twin.colour, depth)
};
for (texture, state) in [(from, COPY_SOURCE), (to, COPY_DEST)] {
let key = std::sync::Arc::as_ptr(texture) as usize;
let real = texture.state.load(Ordering::Relaxed);
copying.insert(key, state);
if real != state {
to_copy.push(transition(
&texture.texture,
host_state(state),
host_state(real),
host_state(state),
));
if !states.contains_key(&key) {
barriers.push(transition(
&texture.texture,
host_state(state),
host_state(real),
));
}
}
}
}
}
// The uploads decided at submission: each host copy to the copy
// destination state, and from there to the state the list starts
// with (back to its real state if the list does not use it).
let mut aliasing = Vec::new();
for (upload, first_upload) in uploads {
let copy = &upload.copy;
let key = std::sync::Arc::as_ptr(copy) as usize;
// A texture cut from a block may lie where another did: what the
// card holds of that one is done with before the first upload
// into this one.
if crate::pool::on() && *first_upload {
aliasing.push(D3D12_RESOURCE_BARRIER {
Type: D3D12_RESOURCE_BARRIER_TYPE_ALIASING,
Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE,
Anonymous: D3D12_RESOURCE_BARRIER_0 {
Aliasing: std::mem::ManuallyDrop::new(D3D12_RESOURCE_ALIASING_BARRIER {
pResourceBefore: std::mem::ManuallyDrop::new(None),
// SAFETY: a borrowed pointer the barrier never releases.
pResourceAfter: unsafe { std::mem::transmute_copy(&copy.texture) },
}),
},
});
}
let real = copying.get(&key).copied().unwrap_or_else(|| copy.state.load(Ordering::Relaxed));
if real != COPY_DEST {
to_copy.push(transition(&copy.texture, host_state(real), host_state(COPY_DEST)));
if !states.contains_key(&key) {
barriers.push(transition(&copy.texture, host_state(COPY_DEST), host_state(real)));
// The uploads decided at submission: each host copy to the copy
// destination state, and from there to the state the list starts
// with (back to its real state if the list does not use it).
let mut aliasing = Vec::new();
for (upload, first_upload) in uploads {
let copy = &upload.copy;
let key = std::sync::Arc::as_ptr(copy) as usize;
// A texture cut from a block may lie where another did: what the
// card holds of that one is done with before the first upload
// into this one.
if crate::pool::on() && *first_upload {
aliasing.push(D3D12_RESOURCE_BARRIER {
Type: D3D12_RESOURCE_BARRIER_TYPE_ALIASING,
Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE,
Anonymous: D3D12_RESOURCE_BARRIER_0 {
Aliasing: std::mem::ManuallyDrop::new(D3D12_RESOURCE_ALIASING_BARRIER {
pResourceBefore: std::mem::ManuallyDrop::new(None),
// SAFETY: a borrowed pointer the barrier never releases.
pResourceAfter: unsafe { std::mem::transmute_copy(&copy.texture) },
}),
},
});
}
let real = copying.get(&key).copied().unwrap_or_else(|| copy.state.load(Ordering::Relaxed));
if real != COPY_DEST {
to_copy.push(transition(&copy.texture, host_state(real), host_state(COPY_DEST)));
if !states.contains_key(&key) {
barriers.push(transition(&copy.texture, host_state(COPY_DEST), host_state(real)));
}
}
copying.insert(key, COPY_DEST);
}
copying.insert(key, COPY_DEST);
}
barriers.extend(states.iter().filter_map(|(key, (first, last, copy))| {
let real = copy.state.swap(*last, Ordering::Relaxed);
let real = copying.get(key).copied().unwrap_or(real);
(real != *first)
.then(|| transition(&copy.texture, host_state(real), host_state(*first)))
}));
if barriers.is_empty() && syncs.is_empty() && uploads.is_empty() {
return None;
}
barriers.extend(states.iter().filter_map(|(key, (first, last, copy))| {
let real = copy.state.swap(*last, Ordering::Relaxed);
let real = if copying.is_empty() { real } else { copying.get(key).copied().unwrap_or(real) };
(real != *first)
.then(|| transition(&copy.texture, host_state(real), host_state(*first)))
}));
if barriers.is_empty() && syncs.is_empty() && uploads.is_empty() {
return None;
}
(syncs, to_copy, aliasing, barriers)
};
let mut held = self.prelude.lock().unwrap_or_else(|e| e.into_inner());
if held.is_none() {
let kept = if prelude_pool() { PRELUDES.lock().unwrap_or_else(|e| e.into_inner()).pop() } else { None };
@@ -8578,6 +8596,9 @@ struct Batch {
/// [`Batch::refresh_front`]'s walk over the steps.
ranges: Vec<(usize, usize)>,
touches: Vec<Touch>,
front_at: ByCopy<usize>,
front_spans_buf: Vec<(usize, usize, usize)>,
front_given: Vec<u32>,
}
thread_local! {
@@ -8624,12 +8645,15 @@ fn refresh_front() -> bool {
/// What a part refreshes of a copy it has given to its shaders stays where it
/// stands: around what they write. What a later part refreshes is the
/// title's bytes of before either.
fn front_spans(steps: impl Iterator<Item = Touch>) -> Vec<(usize, usize, usize)> {
let mut at: ByCopy<usize> = ByCopy::default();
// (Copy, from, to), in the order they come, and the part that gave it to
// shaders last.
let mut spans: Vec<(usize, usize, usize)> = Vec::new();
let mut given: Vec<u32> = Vec::new();
fn front_spans_into(
steps: impl Iterator<Item = Touch>,
at: &mut ByCopy<usize>,
spans: &mut Vec<(usize, usize, usize)>,
given: &mut Vec<u32>,
) {
at.clear();
spans.clear();
given.clear();
let (mut part, mut last) = (1, usize::MAX);
for step in steps {
match step {
@@ -8663,6 +8687,12 @@ fn front_spans(steps: impl Iterator<Item = Touch>) -> Vec<(usize, usize, usize)>
}
}
spans.retain(|s| s.1 < s.2);
}
#[cfg(test)]
fn front_spans(steps: impl Iterator<Item = Touch>) -> Vec<(usize, usize, usize)> {
let (mut at, mut spans, mut given) = (ByCopy::default(), Vec::new(), Vec::new());
front_spans_into(steps, &mut at, &mut spans, &mut given);
spans
}
@@ -8828,6 +8858,9 @@ impl Batch {
self.spans_at.clear();
self.ranges.clear();
self.touches.clear();
self.front_at.clear();
self.front_spans_buf.clear();
self.front_given.clear();
SPARE_BATCH.set(Some(self));
}
@@ -8983,10 +9016,16 @@ impl Batch {
}
}
}
let found = front_spans(self.touches.drain(..));
let spans: Vec<_> = found
.into_iter()
.filter_map(|(key, from, to)| Some((self.spans.get(*self.spans_at.get(&key)?)?.0.clone(), from, to)))
front_spans_into(
self.touches.drain(..),
&mut self.front_at,
&mut self.front_spans_buf,
&mut self.front_given,
);
let spans: Vec<_> = self
.front_spans_buf
.iter()
.filter_map(|&(key, from, to)| Some((self.spans.get(*self.spans_at.get(&key)?)?.0.clone(), from, to)))
.collect();
self.front = true;
for (shadow, from, to) in &spans {
@@ -9013,18 +9052,28 @@ impl Batch {
crate::watch::ask_ahead(&ranges);
}
FLUSHES[3].fetch_add(spans.len() as u64, Ordering::Relaxed);
crate::shadow::wide_each(spans.len() + due.len(), move |i| {
// The textures first: they are the longer pieces.
let Some(i) = i.checked_sub(due.len()) else {
const SPAN_CHUNK: usize = 32;
let span_chunks = spans.len().div_ceil(SPAN_CHUNK);
let due_len = due.len();
crate::shadow::wide_each(due_len + span_chunks, move |i| {
// The textures first (1 by 1): they are the longer pieces.
let Some(chunk_idx) = i.checked_sub(due_len) else {
due[i].refresh();
return;
};
let (shadow, from, to) = &spans[i];
if many && shadow.nothing_new(*from, *to - *from) {
FLUSHES[2].fetch_add(1, Ordering::Relaxed);
return;
let start = chunk_idx * SPAN_CHUNK;
let end = (start + SPAN_CHUNK).min(spans.len());
let mut skipped = 0u64;
for (shadow, from, to) in &spans[start..end] {
if many && shadow.nothing_new(*from, *to - *from) {
skipped += 1;
continue;
}
Self::update(shadow, *from, *to);
}
if skipped > 0 {
FLUSHES[2].fetch_add(skipped, Ordering::Relaxed);
}
Self::update(shadow, *from, *to);
});
if ahead {
crate::watch::ask_ahead_done();
@@ -9176,15 +9225,22 @@ impl Batch {
let before = PREPARING[0].load(Ordering::Relaxed);
FLUSHES[3].fetch_add(self.refreshes.len() as u64, Ordering::Relaxed);
if light {
// A number to a free thread, not a job to each copy.
// Chunked work-stealing avoids per-item atomic contention.
let refreshes = std::mem::take(&mut self.refreshes);
crate::shadow::wide_each(refreshes.len(), move |i| {
let (shadow, from, to) = &refreshes[i];
if refreshes.len() > FEW && shadow.nothing_new(*from, *to - *from) {
FLUSHES[2].fetch_add(1, Ordering::Relaxed);
return;
let many = refreshes.len() > FEW;
let chunk = if many { 32 } else { 1 };
crate::shadow::wide_each_chunked(refreshes.len(), chunk, move |range| {
let mut skipped = 0u64;
for (shadow, from, to) in &refreshes[range] {
if many && shadow.nothing_new(*from, *to - *from) {
skipped += 1;
continue;
}
Self::update(shadow, *from, *to);
}
if skipped > 0 {
FLUSHES[2].fetch_add(skipped, Ordering::Relaxed);
}
Self::update(shadow, *from, *to);
});
} else {
let jobs: Vec<Box<dyn FnOnce() + Send>> = self