Skip to content
Open
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
9 changes: 9 additions & 0 deletions d3d12/crates/leonida-gpu/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,11 @@ impl Drop for SyncedHeaps {
#[cfg(windows)]
static SAMPLERS_AGAIN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// Serializes [`sync_heaps`] against tests that inspect a slot before
/// the next sync without racing the background sync thread.
#[cfg(test)]
static TEST_SYNC_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

/// Mirrors records copied directly into placed shader-visible heaps. Calls
/// to `Create*View` are already mirrored by [`mirror`]; raw CPU copies leave
/// only write-watch pages to reveal which slots changed.
Expand All @@ -382,6 +387,8 @@ pub(crate) fn sync_heaps() {
if !sync_on() {
return;
}
#[cfg(test)]
let _test_sync = TEST_SYNC_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let started = std::time::Instant::now();
let (mut pages, mut looked, mut mirrored, mut tabled, mut later) = (0u64, 0u64, 0u64, 0u64, 0u64);
let (mut views, mut bytes) = (0u64, 0u64);
Expand Down Expand Up @@ -2247,6 +2254,7 @@ mod tests {
let source = elsewhere.as_mut_ptr() as u64;
let sampler = [21, 3, 3, 3, 0, 1, 0, 0, 0, 0, 0, 0x7f7f_ffff, 0];
create_sampler(std::ptr::null(), &sampler, source);
let hold = TEST_SYNC_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// SAFETY: the heap slot and source both hold 16 descriptor bytes.
unsafe {
std::ptr::copy_nonoverlapping(
Expand All @@ -2256,6 +2264,7 @@ mod tests {
)
};
assert!(!host.made(5));
drop(hold);
sync_heaps();
assert!(host.made(5));
}
Expand Down
78 changes: 77 additions & 1 deletion d3d12/crates/leonida-gpu/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ struct Reclaim {
methods: &'static [*const (); 3],
references: AtomicU32,
piece: Piece,
raw: usize,
}

struct Methods([*const (); 3]);
Expand Down Expand Up @@ -710,6 +711,7 @@ unsafe extern "system" fn release(this: *mut c_void) -> u32 {
let gone = Box::from_raw(this.cast::<Reclaim>());
HELD.fetch_sub(gone.piece.len, Ordering::Relaxed);
lock(&TEXTURES).give(gone.piece, now());
forget_released(gone.raw);
}
left
}
Expand All @@ -718,6 +720,13 @@ unsafe extern "system" fn release(this: *mut c_void) -> u32 {
/// Textures made in the last second ([`fresh`]), and when.
static RECENT: Mutex<VecDeque<(usize, u64)>> = Mutex::new(VecDeque::new());

/// Removes a released pooled texture pointer from `RECENT` and `UNSEEN`
/// so a new resource allocated at the same address is not mistaken for it.
fn forget_released(raw: usize) {
RECENT.lock().unwrap_or_else(|e| e.into_inner()).retain(|r| r.0 != raw);
UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).remove(&raw);
}

/// Whether `texture` was made in a block in the last second.
pub(crate) fn fresh(texture: &ID3D12Resource) -> bool {
let raw = texture.as_raw() as usize;
Expand Down Expand Up @@ -788,7 +797,12 @@ pub(crate) fn begun_before(texture: &ID3D12Resource) -> Option<u64> {
/// Makes `texture` give `piece` back when the last reference to it goes.
fn give_back_with(texture: &ID3D12Resource, piece: Piece) {
const KEY: GUID = GUID::from_u128(0x6f0e2f0c_5a59_4b1e_9a3f_7f4d2c9be713);
let made = Box::into_raw(Box::new(Reclaim { methods: &RECLAIM.0, references: AtomicU32::new(1), piece }));
let made = Box::into_raw(Box::new(Reclaim {
methods: &RECLAIM.0,
references: AtomicU32::new(1),
piece,
raw: texture.as_raw() as usize,
}));
// SAFETY: a COM object of ours with one reference, which this takes and
// drops at the end; the texture takes its own.
unsafe {
Expand Down Expand Up @@ -1269,4 +1283,66 @@ mod tests {
let (piece, memory, _) = take_unlocked(&POOL, &GROWN, &BLOCKS, (65536, 65536, Ripe::ALL), || Some(3)).unwrap();
assert_eq!((piece.block, memory), (0, 3));
}

/// Releasing a pooled texture clears its raw pointer from `RECENT` and
/// `UNSEEN` so a new resource at the same address is not treated as fresh.
#[test]
fn released_texture_pointer_is_not_kept_in_recent_or_unseen() {
let raw = 0xDEAD_BEEF_0000usize;
RECENT.lock().unwrap_or_else(|e| e.into_inner()).push_back((raw, now()));
{
let mut unseen = UNSEEN.lock().unwrap_or_else(|e| e.into_inner());
note_unseen(&mut unseen, raw, 10, 5);
}
forget_released(raw);
assert!(!RECENT.lock().unwrap_or_else(|e| e.into_inner()).iter().any(|r| r.0 == raw));
assert!(unseen_begun(&UNSEEN.lock().unwrap_or_else(|e| e.into_inner()), raw, 5).is_none());
}

/// Dropping the last COM reference to a pooled texture runs `Reclaim::release`
/// and removes its raw pointer from `RECENT` and `UNSEEN`.
#[test]
fn dropping_last_pooled_texture_reference_forgets_recent_and_unseen() {
struct ForcePool(bool);
impl Drop for ForcePool {
fn drop(&mut self) {
TEST_POOL_FORCE.set(self.0);
}
}

let Some(host) = crate::host::host() else { return };
let _force = ForcePool(TEST_POOL_FORCE.replace(true));
let desc = D3D12_RESOURCE_DESC {
Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,
Width: 32,
Height: 32,
DepthOrArraySize: 1,
MipLevels: 1,
Format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT(28),
SampleDesc: windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
..Default::default()
};
// SAFETY: a valid 32x32 RGBA8_UNORM texture description.
let tex = unsafe { texture(&host.device, &desc, D3D12_RESOURCE_STATE_COMMON) }
.expect("pooled texture allocation");
let raw = tex.as_raw() as usize;
// Keep our UNSEEN entry pinned above any finished submission count so
// concurrent `made_now` calls cannot evict it before `Reclaim::release`.
UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).insert(raw, u64::MAX);
let second_ref = tex.clone();

assert!(fresh(&tex));
assert!(UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).contains_key(&raw));

// Dropping the first of two COM references must not trigger `Reclaim::release`.
drop(tex);
assert!(fresh(&second_ref));
assert!(UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).contains_key(&raw));

// Dropping the final COM reference destroys the resource and calls
// `Reclaim::release` -> `forget_released(gone.raw)`.
drop(second_ref);
assert!(!RECENT.lock().unwrap_or_else(|e| e.into_inner()).iter().any(|r| r.0 == raw));
assert!(!UNSEEN.lock().unwrap_or_else(|e| e.into_inner()).contains_key(&raw));
}
}
10 changes: 6 additions & 4 deletions d3d12/crates/leonida-gpu/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5187,10 +5187,12 @@ mod tests {
assert!(!g.protected.contains(at / BLOCK) && !g.clean.contains(at / BLOCK));
}
assert!(!published(at, at + BLOCK));
// SAFETY: our view.
unsafe { UnmapViewOfFile(view) };
// End the remapping window before returning `at` to Windows so another
// thread cannot reuse the address while `g.changing` still leaves its block alone.
drop(changing);
assert!(!guards().left_alone(at / BLOCK));
// SAFETY: our view.
unsafe { UnmapViewOfFile(view) };
}

/// Memory at an address where blocks were guarded or absent is not
Expand Down Expand Up @@ -5626,7 +5628,7 @@ mod tests {
use windows_sys::Win32::Foundation::CloseHandle;
use windows_sys::Win32::System::Memory::{MEMORY_MAPPED_VIEW_ADDRESS, UnmapViewOfFile};
for &view in views {
title_memory_remapped(view, bytes);
title_memory_unmapping(view, bytes);
// SAFETY: a view of the section mapped by `two_views`.
unsafe {
UnmapViewOfFile(MEMORY_MAPPED_VIEW_ADDRESS {
Expand Down Expand Up @@ -6442,8 +6444,8 @@ mod many_askers_tests {
{
let _changing = super::title_memory_remapping();
super::title_memory_unmapping(one, PAGE);
unsafe { UnmapViewOfFile(MEMORY_MAPPED_VIEW_ADDRESS { Value: one as *mut _ }) };
}
unsafe { UnmapViewOfFile(MEMORY_MAPPED_VIEW_ADDRESS { Value: one as *mut _ }) };
last = [last[asked]; VIEWS];
}
});
Expand Down
Loading