GPU: only what programs using title memory write is withheld when their list misses

A list in which a program using title memory read or wrote memory with no
copy had every range its shaders declared withheld from title memory (and,
since ec9e750e, dropped from the host copies right after the submission),
whichever program declared it. The BVH builder's list also holds ordinary
programs (run light13, one list: ~1,000 builder dispatches beside
billboard_nobump:CS_ComputeImposterTransform x250, CSGenerateHairVB/IB,
bvh_skinning, CS_InstanceRotationFixup), so their output never reached title
memory and, after the drop, never reached the frame's later draws either.

Now each declared range records whether its program uses title memory (the
draw or dispatch being bound, `HostList::writer_uses_memory`), and only
those ranges are withheld when their list misses. Other programs write
through bounded views from their own inputs: their ranges go back as before
the withholding. An ordinary range overlapping a withheld range of the same
copy is withheld with it, since its copy-back would take the other's bytes
along.

Test: host::withholding::only_programs_using_title_memory_are_withheld (a
missing list with an ordinary write, a title-memory program's write and an
overlapping ordinary write; fails with the whole list withheld).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
MarvisandClaude Opus 5.5 committed 2026-11-25 13:21:09 +01:00
1 parent ea812894ee
commit 0a5be3c7c6
2 files changed
+104 -10

No files matched your search

+3
View File
@@ -509,6 +509,9 @@ impl State {
/// False if the draw or dispatch must not run: a table it reads could
/// not be made.
fn bind(&mut self, list: &HostList, p: &HostPipeline, compute: bool) -> bool {
// What its shaders write is withheld only if the program uses title
// memory and a list using it misses (`crate::memory::withholding`).
let _writer = list.writer_uses_memory(p.stages.iter().any(|s| s.memory.is_some()));
let l = &list.list;
// A depth copy of ours (`crate::twin`) since the last one ran a
// compute pipeline.
+101 -10
View File
@@ -759,6 +759,9 @@ pub(crate) struct HostList {
/// A list of ours (uploads), whose uses are no use of the title's
/// (`crate::resource::evict`).
ours: std::sync::atomic::AtomicBool,
/// The draw or dispatch being recorded runs a program using title
/// memory ([`HostList::writer_uses_memory`]).
memory_writer: std::sync::atomic::AtomicBool,
/// The list that runs those transitions just before this one.
prelude: (ID3D12CommandAllocator, ID3D12GraphicsCommandList),
/// The pipelines the list binds, for reports of slow submissions.
@@ -913,8 +916,10 @@ enum Step {
/// Work that writes host memory only.
Host(Box<dyn Fn() + Send>),
/// Shaders write a range (address, bytes) of a buffer's host copy;
/// whether they mark the pages they write (no snapshot then).
GpuWrite(std::sync::Arc<crate::shadow::Shadow>, usize, usize, bool),
/// whether they mark the pages they write (no snapshot then), and
/// whether their program uses title memory (then the range is withheld
/// when such a list misses, `crate::memory::withholding`).
GpuWrite(std::sync::Arc<crate::shadow::Shadow>, usize, usize, bool, bool),
/// Brings a texture's staging buffer up to date: once per submission,
/// whichever lists sample it, until a `Copy` or `Run` intervenes.
Texture(std::sync::Arc<crate::texture::Staging>, std::sync::Arc<crate::resource::HostTexture>),
@@ -1061,6 +1066,7 @@ impl Host {
twins: Mutex::default(),
rebind: false.into(),
ours: false.into(),
memory_writer: false.into(),
prelude: (prelude_allocator, prelude),
pipelines: Mutex::default(),
id,
@@ -1266,9 +1272,13 @@ impl Host {
let mut index: std::collections::HashMap<(usize, usize, usize), usize> = std::collections::HashMap::new();
// Shaders that read memory with no copy computed something else than
// the console's: it stays out of title memory, where it broke the
// title's own data (runs c1, c4), and so does what every list using
// title memory wrote while any misses (`crate::memory::withholding`).
// Only such lists have checks; all are asked before anything goes back.
// title's own data (runs c1, c4), and so does what every program
// using title memory wrote while any misses
// (`crate::memory::withholding`). Only lists with such programs have
// checks; all are asked before anything goes back. Other programs in
// those lists write through bounded views from their own inputs:
// their ranges go back (the imposter transforms beside the BVH
// builder in one list, run light13).
let missed: Vec<bool> = runs
.iter()
.map(|list| list.deferred().missed.iter().any(|check| check()))
@@ -1279,18 +1289,32 @@ impl Host {
let deferred = list.deferred();
let missed = missed || withholding && !deferred.missed.is_empty();
for step in &deferred.copies {
if let Step::GpuWrite(shadow, address, len, _) = step {
if let Step::GpuWrite(shadow, address, len, _, memory) = step {
let key = (std::sync::Arc::as_ptr(shadow) as usize, *address, *len);
// A range both kinds declare is withheld whole.
let goes_back = !(missed && *memory);
match index.get(&key) {
Some(&i) => back[i].3 &= !missed,
Some(&i) => back[i].3 &= goes_back,
None => {
index.insert(key, back.len());
back.push((shadow.clone(), *address, *len, !missed));
back.push((shadow.clone(), *address, *len, goes_back));
}
}
}
}
}
// A range overlapping a withheld one of the same copy is withheld
// too: its copy-back would take the other's bytes along (pages a
// program using title memory and another both wrote).
let held: Vec<(usize, usize, usize)> = back
.iter()
.filter(|b| !b.3)
.map(|(s, a, n, _)| (std::sync::Arc::as_ptr(s) as usize, *a, *n))
.collect();
for b in back.iter_mut().filter(|b| b.3) {
let copy = std::sync::Arc::as_ptr(&b.0) as usize;
b.3 = !held.iter().any(|&(c, a, n)| c == copy && a < b.1 + b.2 && b.1 < a + n);
}
// Pages shaders marked first: the copy-backs below end what the
// copies keep for them.
let withheld: Vec<(std::sync::Arc<crate::shadow::Shadow>, usize, usize)> = back
@@ -1797,6 +1821,19 @@ impl HostList {
self.ours.store(true, Ordering::Relaxed);
}
/// What the list's shaders write from now until the guard goes is
/// written by a program using title memory (`uses`) or not.
pub(crate) fn writer_uses_memory(&self, uses: bool) -> impl Drop + '_ {
struct Reset<'a>(&'a std::sync::atomic::AtomicBool);
impl Drop for Reset<'_> {
fn drop(&mut self) {
self.0.store(false, Ordering::Relaxed);
}
}
self.memory_writer.store(uses, Ordering::Relaxed);
Reset(&self.memory_writer)
}
/// A copy of ours replaced the compute root signature and pipeline.
pub(crate) fn need_rebind(&self) {
self.rebind.store(true, Ordering::Relaxed);
@@ -1977,9 +2014,10 @@ impl HostList {
len: usize,
marked: bool,
) {
let memory = self.memory_writer.load(Ordering::Relaxed);
self.deferred()
.copies
.push(Step::GpuWrite(shadow.clone(), address, len, marked));
.push(Step::GpuWrite(shadow.clone(), address, len, marked, memory));
}
/// Shaders of the list take views from the heap itself
@@ -2197,7 +2235,7 @@ impl HostList {
}
continue;
}
Step::GpuWrite(shadow, address, len, marked) => {
Step::GpuWrite(shadow, address, len, marked, _) => {
// The shaders start from the title's data, which a
// snapshot must hold (marked ranges keep none).
if !marked {
@@ -3391,3 +3429,56 @@ mod batch { // `mod tests` is already taken at the top of this file
}
}
#[cfg(test)]
mod withholding {
/// A list in which a program using title memory missed memory: what
/// that program wrote is withheld from title memory and leaves the
/// copies at once; what an ordinary program wrote in the same list goes
/// back and stays in its copy (run light13: the imposter transforms in
/// the BVH builder's list); an ordinary range overlapping a withheld one
/// is withheld with it, since its copy-back would take the other's bytes
/// along.
#[test]
fn only_programs_using_title_memory_are_withheld() {
use crate::watch::PAGE;
let Some(host) = super::host() else {
return; // no host GPU here
};
let pools = [crate::watch::tests::pool(1), crate::watch::tests::pool(1)];
for &p in &pools {
// SAFETY: our pool, not guarded yet.
unsafe { ((p + 16) as *mut u8).write_volatile(0x11) };
crate::shadow::register(p, p as u64, PAGE as u64);
}
let copies: Vec<_> = pools.iter().map(|&p| crate::shadow::find(p as u64, PAGE as u64).unwrap()).collect();
let list = host.new_list().unwrap();
// (copy, start, bytes, the writer uses title memory, byte written at +0 and +16)
let writes = [(0, 0, 32, false, 0x22), (1, 16, 16, true, 0x33), (1, 0, 32, false, 0x44)];
for &(k, at, len, memory, value) in &writes {
let _writer = list.writer_uses_memory(memory);
let (copy, address) = (copies[k].clone(), (pools[k] + at) as u64);
copy.refresh(&list, address, len);
copy.gpu_writes(&list, address, len, false);
// The shader's write, after the step that keeps the range's bytes.
list.defer_host(move || copy.write_host(address, &[value]));
}
list.defer_missed(|| true);
list.close();
host.execute(&[&list]);
// SAFETY: bytes of our pools.
let title = |k: usize, at: usize| unsafe { ((pools[k] + at) as *const u8).read_volatile() };
let copy = |k: usize, at: usize| {
copies[k].update(pools[k], PAGE);
copies[k].read_host((pools[k] + at) as u64, 1)[0] & 0xff
};
assert_eq!((title(0, 0), copy(0, 0)), (0x22, 0x22), "an ordinary program's write goes back and stays");
assert_eq!((title(1, 16), copy(1, 16)), (0x11, 0x11), "withheld, then dropped from the copy");
assert_eq!((title(1, 0), copy(1, 0)), (0, 0), "overlapping a withheld range: withheld with it");
for (&p, copy) in pools.iter().zip(copies) {
crate::shadow::unregister(p, p as u64);
drop(copy);
crate::watch::tests::free(p);
}
}
}