Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions crates/xtp-gpu/src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,6 @@ impl Registry {
/// of one, finds nothing there (its instances are inactive, its rays miss)
/// instead of a structure in released memory.
pub(crate) fn release_buffer(owner: Option<usize>, start: u64, end: u64) {
#[cfg(test)]
tests::released(start, end);
let (previous, slots) = registry().retire(start, end, Some(owner));
drop(previous);
for slot in slots {
Expand Down
88 changes: 32 additions & 56 deletions crates/xtp-gpu/src/rt/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,71 +921,47 @@ fn rt_spares_serve_later_generations_once_free() {
assert!(table.spares.is_empty());
}

/// The ranges of title memory released so far ([`release_buffer`]).
static RELEASED: Mutex<Vec<(u64, u64)>> = Mutex::new(Vec::new());

/// Notes a release of title memory `[start, end)`.
pub(super) fn released(start: u64, end: u64) {
RELEASED
.lock()
.unwrap_or_else(|e| e.into_inner())
.push((start, end));
}

/// The releases of title memory so far whose range holds `at`. Another
/// test's report of a remap of all title memory (`watch`'s tests report
/// one over the whole address space) retires every structure and spare of
/// the registry, whichever test made them.
fn releases_over(at: u64) -> usize {
(RELEASED.lock().unwrap_or_else(|e| e.into_inner()).iter())
.filter(|&&(start, end)| (start..end).contains(&at))
.count()
}

/// A TLAS rebuilt every frame at one title address alternates between two
/// results instead of allocating one a frame: the last generation stays in
/// its slot until the next is submitted, the one before is free again.
/// The reused result traces as the last build. A try that a release of
/// the TLAS's memory cut into, which retired its generations, runs again.
/// The reused result traces as the last build.
#[test]
fn rt_tlas_rebuilds_reuse_a_result_nothing_holds() {
let test = "rt_tlas_rebuilds_reuse_a_result_nothing_holds";
let Some((gpu, rays)) = dxr(test) else {
return;
};
let (first, rebuilt, traced) = (0..5)
.find_map(|_| {
let world = World::new();
let tlas = world.tlas.address();
let releases = releases_over(tlas);
let generation = || registry().structures.get(&tlas).map(|s| s.result.gpu());
let first = generation()?;
let rebuilt = (0..4)
.map(|_| {
let list = TitleList::new();
// SAFETY: the title list and the instance buffer are live.
unsafe {
assert!(Builder.build(list.this(), &tlas_desc(tlas, &world._inputs[2])))
};
list.run();
generation()
})
.collect::<Option<Vec<u64>>>()?;
let traced = rays.then(|| {
let slot = Descriptor::new(tlas);
let list = TitleList::new();
let out = unwritten();
trace(gpu.host(), &list, Scene::Descriptor(slot.handle()), &out);
list.run();
out.words()
});
(releases_over(tlas) == releases).then_some((first, rebuilt, traced))
})
.expect("five tries, each cut into by a release of all title memory");
assert_ne!(rebuilt[0], first, "the first generation is still published");
assert_eq!(rebuilt, [rebuilt[0], first, rebuilt[0], first]);
if let Some(traced) = traced {
assert_eq!(traced, hit());
let world = World::new();
let tlas = world.tlas.address();
// A new allocation can get the same GPU address. A weak reference
// identifies the result without keeping it in use.
let generation = || Arc::downgrade(&registry().structures[&tlas].result);
let rebuild = || {
let list = TitleList::new();
// SAFETY: the title list and the instance buffer are live.
unsafe { assert!(Builder.build(list.this(), &tlas_desc(tlas, &world._inputs[2]))) };
list.run();
generation()
};
let first = generation();
let second = rebuild();
assert!(
!first.ptr_eq(&second),
"the first generation is still published"
);

// A watch test can reset its guards while the first result is a spare.
drop(crate::watch::tests::serial());
for expected in [&first, &second, &first] {
assert!(rebuild().ptr_eq(expected), "reuse the retired result");
}
if rays {
let slot = Descriptor::new(tlas);
let list = TitleList::new();
let out = unwritten();
trace(gpu.host(), &list, Scene::Descriptor(slot.handle()), &out);
list.run();
assert_eq!(out.words(), hit());
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/xtp-gpu/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4985,8 +4985,8 @@ mod tests {
}

/// Held by the tests that count on what reads learned of pages
/// ([`Pages`]): the guard's tests report all title memory changed
/// (`crate::watch::tests::serial`), which forgets it.
/// ([`Pages`]): the guard tests reset the same page-read cache in
/// [`crate::watch::tests::serial`].
fn learned() -> std::sync::MutexGuard<'static, ()> {
crate::watch::tests::serial()
}
Expand Down
10 changes: 8 additions & 2 deletions crates/xtp-gpu/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,10 @@ pub fn title_memory_remapped(at: usize, len: usize) {
// guards' lock, which reads may wait for).
drop(crate::shadow::title_changing(at, len));
crate::resource::memory_remapped(at, len);
let range = blocks(at, len);
forget_guards(blocks(at, len));
}

fn forget_guards(range: Range<usize>) {
let mut lock = settled(range.clone());
let g = &mut *lock;
g.owners.forget(range.clone());
Expand Down Expand Up @@ -1412,7 +1415,10 @@ pub(crate) mod tests {
pub(crate) fn serial() -> std::sync::MutexGuard<'static, ()> {
static SERIAL: Mutex<()> = Mutex::new(());
let serial = SERIAL.lock().unwrap_or_else(|e| e.into_inner());
title_memory_remapped(0, usize::MAX);
// Reset the guards and the page-read cache these tests share. A
// title remap would also evict live GPU resources in parallel tests.
drop(crate::shadow::title_changing(0, usize::MAX));
forget_guards(blocks(0, usize::MAX));
same().clear();
serial
}
Expand Down