Skip to content
Open
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
109 changes: 94 additions & 15 deletions crates/victory-gpu/src/descriptor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ pub(crate) struct HostHeap {
/// released the resource may still be read, and a freed host resource
/// read there is a GPU page fault (the console reads stale memory).
kept: Mutex<Vec<Option<ID3D12Resource>>>,
/// Where the search for free dynamic slots starts, and which are taken
/// (by lists not yet reset).
next: Mutex<(u32, Vec<bool>)>,
slots: Mutex<DynamicSlots>,
}

struct DynamicSlots {
next: usize,
/// Slots held by command lists until reset or release.
taken: Vec<bool>,
/// Requests at least this large cannot fit.
first_impossible: Option<usize>,
}

/// Host heaps' GPU ranges (start, bytes, bytes per slot, sampler), for
Expand Down Expand Up @@ -122,7 +128,11 @@ impl HostHeap {
sampler,
made: Mutex::new(vec![0; count as usize]),
kept: Mutex::new(vec![None; (count + extra) as usize]),
next: Mutex::new((0, vec![false; DYNAMIC as usize])),
slots: Mutex::new(DynamicSlots {
next: 0,
taken: vec![false; DYNAMIC as usize],
first_impossible: None,
}),
};
// A slot never written is undefined: shaders reading one (a slot
// the title wrote in a way we do not know, or a table entry no
Expand Down Expand Up @@ -204,11 +214,19 @@ impl HostHeap {
if self.sampler || n == 0 || n > DYNAMIC {
return None;
}
let mut guard = self.next.lock().unwrap_or_else(|e| e.into_inner());
let (next, taken) = &mut *guard;
let mut slots = self.slots.lock().unwrap_or_else(|e| e.into_inner());
let n = n as usize;
// Search from `next` on, then from the start.
let starts = (*next as usize..DYNAMIC as usize).chain(0..*next as usize);
// Allocations cannot lengthen any free run. A smaller request may
// still fit. A release can raise the bound.
if slots.first_impossible.is_some_and(|bound| n >= bound) {
return None;
}
// Search from `next` on, then from the start (extending past cursor by
// n - 1 so contiguous runs crossing the cursor are not missed).
let cursor = slots.next;
let end = DYNAMIC as usize;
let second_end = cursor.saturating_add(n.saturating_sub(1)).min(end);
let starts = (cursor..end).chain(0..second_end);
let mut run = 0;
let mut found = None;
let mut last = usize::MAX;
Expand All @@ -218,19 +236,20 @@ impl HostHeap {
run = 0;
}
last = at;
run = if taken[at] { 0 } else { run + 1 };
run = if slots.taken[at] { 0 } else { run + 1 };
if run == n {
found = Some(at + 1 - n);
break;
}
}
let Some(first) = found else {
slots.first_impossible = Some(n);
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| tracing::warn!(n, "no free dynamic descriptor slots: a table is not made"));
return None;
};
taken[first..first + n].fill(true);
*next = ((first + n) % DYNAMIC as usize) as u32;
slots.taken[first..first + n].fill(true);
slots.next = (first + n) % end;
Some(self.count + first as u32)
}

Expand All @@ -248,10 +267,22 @@ impl HostHeap {
let Some(at) = first.checked_sub(self.count) else {
return;
};
let mut guard = self.next.lock().unwrap_or_else(|e| e.into_inner());
let end = (at + n).min(DYNAMIC) as usize;
if let Some(slots) = guard.1.get_mut(at as usize..end) {
slots.fill(false);
let mut slots = self.slots.lock().unwrap_or_else(|e| e.into_inner());
let end = at.saturating_add(n).min(DYNAMIC) as usize;
let at = at as usize;
if at >= end {
return;
}
slots.taken[at..end].fill(false);
if let Some(bound) = slots.first_impossible {
// Only the free run touching the release can have grown.
let left = slots.taken[..at]
.iter()
.rev()
.take_while(|&&taken| !taken)
.count();
let right = slots.taken[end..].iter().take_while(|&&taken| !taken).count();
slots.first_impossible = Some(bound.max(left + (end - at) + right + 1));
}
}

Expand Down Expand Up @@ -871,4 +902,52 @@ mod tests {
assert_eq!(heap.dynamic(50), Some(4));
assert_eq!(heap.dynamic(40), Some(4 + DYNAMIC - 40));
}

#[test]
fn dynamic_search_can_find_a_run_across_the_cursor() {
let Some(heap) = HostHeap::new(false, 4) else {
eprintln!("no host descriptor heap; skipped");
return;
};
let first = heap.dynamic(1).unwrap();
let left = heap.dynamic(2).unwrap();
let right = heap.dynamic(2).unwrap();
let rest = heap.dynamic(DYNAMIC - 5).unwrap();
assert_eq!((first, left, right, rest), (4, 5, 7, 9));

// Reallocating the left half puts the cursor back in the middle.
heap.release(left, 2);
assert_eq!(heap.dynamic(2), Some(left));
heap.release(left, 2);
heap.release(right, 2);
assert_eq!(heap.dynamic(4), Some(left));
assert_eq!(heap.dynamic(1), None);

// A free slot at each physical end does not form a two-slot range.
heap.release(first, 1);
let last = first + DYNAMIC - 1;
heap.release(last, 1);
assert_eq!(heap.dynamic(2), None);
}

#[test]
fn failed_dynamic_requests_still_allow_smaller_requests_and_releases() {
let Some(heap) = HostHeap::new(false, 4) else {
eprintln!("no host descriptor heap; skipped");
return;
};
let first = heap.dynamic(DYNAMIC - 8).unwrap();
assert_eq!(heap.dynamic(10), None);
assert_eq!(heap.dynamic(12), None);
assert_eq!(heap.dynamic(8), Some(first + DYNAMIC - 8));
assert_eq!(heap.dynamic(1), None);
assert_eq!(heap.dynamic(8), None);
heap.release(first, 1);
heap.release(first + 2, 1);
assert_eq!(heap.dynamic(2), None);
assert_eq!(heap.slots.lock().unwrap().first_impossible, Some(2));
// Releasing the gap creates a three-slot run in this same frame.
heap.release(first + 1, 1);
assert_eq!(heap.dynamic(3), Some(first));
}
}