From 63f6740335b9c126b56ea013a1089f6256cb5411 Mon Sep 17 00:00:00 2001 From: pushkarreddyy <212133856+pushkarreddyy@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:58:34 +0500 Subject: [PATCH 1/2] GPU: find free dynamic descriptor runs across the cursor The first pass can miss a run that starts before the search cursor. The second pass needs up to n - 1 extra slots to see the whole run. Keep the physical heap boundary between passes so descriptors are always contiguous and recorded command lists can keep their slots. Backport the allocator fix and the corrected host-heap regression from the same upstream pull request. Based-on: https://github.com/Marvisak/VIctory/pull/974 Tested-on: RX 6800, Fedora Linux 45, Linux native Assisted-by: gpt-6.2 sol xhigh --- crates/victory-gpu/src/descriptor/host.rs | 35 +++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 43dba24285..2f33a84f93 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -207,8 +207,12 @@ impl HostHeap { let mut guard = self.next.lock().unwrap_or_else(|e| e.into_inner()); let (next, taken) = &mut *guard; 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); + // 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 = *next as usize; + 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; @@ -871,4 +875,31 @@ 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); + } } From 117c5ab246bfa185075d33ba3d0004895ffb6651 Mon Sep 17 00:00:00 2001 From: gmanka Date: Thu, 26 Nov 2026 23:01:18 +0500 Subject: [PATCH 2/2] GPU: remember impossible dynamic descriptor allocations Heavy Linux native frames can request thousands of dynamic tables after command lists have occupied the reserve. A failed search proves that larger requests cannot fit while the free runs only shrink. Remember that bound under the allocator lock to skip repeated walks. When a list releases slots, account for the free run that touches them. Small, separated releases keep large failures cached. A release that creates a large enough run admits the next allocation immediately, with no per-frame search limit. Command-list ownership is unchanged. Based-on: https://github.com/Marvisak/VIctory/pull/2394 Inspired-by: https://github.com/fxceless/victory-fork/commit/788ea4676ec7ddf11ff35ac213afa83d828cfa4c Tested-on: RX 6800 and Lavapipe, Fedora Linux 45, Linux native Assisted-by: gpt-6.2 sol xhigh --- crates/victory-gpu/src/descriptor/host.rs | 76 ++++++++++++++++++----- 1 file changed, 62 insertions(+), 14 deletions(-) diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 2f33a84f93..347fcc5991 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -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>>, - /// Where the search for free dynamic slots starts, and which are taken - /// (by lists not yet reset). - next: Mutex<(u32, Vec)>, + slots: Mutex, +} + +struct DynamicSlots { + next: usize, + /// Slots held by command lists until reset or release. + taken: Vec, + /// Requests at least this large cannot fit. + first_impossible: Option, } /// Host heaps' GPU ranges (start, bytes, bytes per slot, sampler), for @@ -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 @@ -204,12 +214,16 @@ 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; + // 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 = *next as usize; + 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); @@ -222,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) } @@ -252,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)); } } @@ -902,4 +929,25 @@ mod tests { 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)); + } }