D3D12.X: video-memory arenas never overlap

arena_for reused an arena only if it held the whole placement, and a new
one took the 32 MiB blocks below the address up to 128 MiB with no regard
for the arenas already there. A placement straddling an arena (or a new
arena growing into one) gave two heaps over the same title addresses, so
resources at one address could land in different heaps and stop aliasing.

The layout is now a pure function (plan) over the arena ranges:
- a placement inside an arena reuses it;
- a new arena that overlaps none grows to 128 MiB but stops at the next
  arena;
- a placement that overlaps arenas gets one arena over the union of its
  range and all of them (to closure), and those leave the list. Their
  resources keep their heaps; later placements at those addresses use the
  new arena.

Tests (vram::tests): the straddling sequence from #104, a new arena
stopping at the next one, a placement merging two arenas over a gap.
This commit is contained in:
gefydev
2026-11-23 10:25:15 -03:00
parent f311ca5b96
commit 70d7e0a9d1
+123 -8
View File
@@ -54,15 +54,55 @@ pub fn gpu_owned(flags: u32) -> bool {
flags & 0x7 != 0
}
/// The arena holding `[va, va + bytes)` – an existing one, or a new one from the 32 MiB below
/// `va`, 128 MiB or what the resource needs (None: no video memory).
/// Where a placement at `[va, va + bytes)` goes, given the arenas' ranges (base, size).
#[derive(Debug, PartialEq)]
enum Plan {
/// into arena `index`, at `offset`
Reuse { index: usize, offset: usize },
/// into a new arena `[base, base + size)`, which takes the place of the arenas `replaces`
/// (indices, ascending) in the list
New { base: usize, size: usize, replaces: Vec<usize> },
}
/// The arenas never overlap, so an address has one arena and resources at one address share
/// memory. A new arena takes the 32 MiB blocks the range needs and, while it overlaps none,
/// grows to 128 MiB – no further than the next arena. A range that straddles arenas gets one
/// arena over the union of the range and every arena it overlaps; those arenas leave the list
/// (their resources keep their heaps, but later placements at their addresses use the new one).
fn plan(arenas: &[(usize, usize)], va: usize, bytes: usize) -> Plan {
if let Some(index) = arenas.iter().position(|&(b, n)| b <= va && va + bytes <= b + n) {
return Plan::Reuse { index, offset: va - arenas[index].0 };
}
let (mut lo, mut hi) = (va & !(ARENA_ALIGN - 1), (va + bytes).div_ceil(ARENA_ALIGN) * ARENA_ALIGN);
let overlaps = |lo: usize, hi: usize| -> Vec<usize> { (0..arenas.len()).filter(|&i| arenas[i].0 < hi && lo < arenas[i].0 + arenas[i].1).collect() };
let mut replaces = overlaps(lo, hi);
if replaces.is_empty() {
let next = arenas.iter().map(|&(b, _)| b).filter(|&b| b >= hi).min().unwrap_or(usize::MAX);
hi = hi.max(lo.saturating_add(ARENA)).min(next);
return Plan::New { base: lo, size: hi - lo, replaces };
}
// the union grows as it takes in arenas, which may overlap further ones
loop {
for &i in &replaces {
lo = lo.min(arenas[i].0);
hi = hi.max(arenas[i].0 + arenas[i].1);
}
let more = overlaps(lo, hi);
if more.len() == replaces.len() {
return Plan::New { base: lo, size: hi - lo, replaces };
}
replaces = more;
}
}
/// The arena holding `[va, va + bytes)` (None: no video memory).
unsafe fn arena_for(dev: &ID3D12Device, va: usize, bytes: usize) -> Option<(ID3D12Heap, u64)> {
let mut g = ARENAS.lock().unwrap();
if let Some(a) = g.iter().find(|a| a.base <= va && va + bytes <= a.base + a.size) {
return Some((a.heap.clone(), (va - a.base) as u64));
}
let base = va & !(ARENA_ALIGN - 1);
let size = (va + bytes - base).div_ceil(ARENA_ALIGN).max(ARENA / ARENA_ALIGN) * ARENA_ALIGN;
let ranges: Vec<(usize, usize)> = g.iter().map(|a| (a.base, a.size)).collect();
let (base, size, replaces) = match plan(&ranges, va, bytes) {
Plan::Reuse { index, offset } => return Some((g[index].heap.clone(), offset as u64)),
Plan::New { base, size, replaces } => (base, size, replaces),
};
let desc = D3D12_HEAP_DESC {
SizeInBytes: size as u64,
Properties: D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() },
@@ -76,7 +116,20 @@ unsafe fn arena_for(dev: &ID3D12Device, va: usize, bytes: usize) -> Option<(ID3D
return None;
}
let heap = heap?;
crate::xlog_info!("D3D12.X: video-memory arena {} for title addresses {base:#x}..{:#x}", g.len(), base + size);
if replaces.is_empty() {
crate::xlog_info!("D3D12.X: video-memory arena for title addresses {base:#x}..{:#x}", base + size);
} else {
// the placed resources hold their heaps: the old arenas live on as long as they do
crate::xlog_info!(
"D3D12.X: video-memory arena for title addresses {base:#x}..{:#x} replaces {} arena(s) a placement at {va:#x}..{:#x} straddled (resources already in them no longer alias later ones)",
base + size,
replaces.len(),
va + bytes
);
for &i in replaces.iter().rev() {
g.remove(i);
}
}
g.push(Arena { base, size, heap: heap.clone() });
Some((heap, (va - base) as u64))
}
@@ -106,3 +159,65 @@ pub unsafe fn place(this: *mut c_void, va: usize, desc: &D3D12_RESOURCE_DESC, st
PLACED_TEXTURES.lock().unwrap().get_or_insert_with(HashSet::new).insert(*ppv as usize);
Some(hr)
}
#[cfg(test)]
mod tests {
use super::*;
const MIB: usize = 1 << 20;
/// Applies placements to a list of arena ranges as `arena_for` does; returns the arena
/// (base) each placement landed in.
fn place_all(seq: &[(usize, usize)]) -> (Vec<(usize, usize)>, Vec<usize>) {
let mut arenas: Vec<(usize, usize)> = Vec::new();
let mut landed = Vec::new();
for &(va, bytes) in seq {
match plan(&arenas, va, bytes) {
Plan::Reuse { index, offset } => {
assert_eq!(arenas[index].0 + offset, va);
landed.push(arenas[index].0);
}
Plan::New { base, size, replaces } => {
assert!(base <= va && va + bytes <= base + size, "{va:#x} in {base:#x}+{size:#x}");
assert_eq!((base % ARENA_ALIGN, size % ARENA_ALIGN), (0, 0));
for &i in replaces.iter().rev() {
arenas.remove(i);
}
arenas.push((base, size));
landed.push(base);
}
}
for (i, a) in arenas.iter().enumerate() {
for b in &arenas[i + 1..] {
assert!(a.0 + a.1 <= b.0 || b.0 + b.1 <= a.0, "arenas {a:x?} and {b:x?} overlap");
}
}
}
(arenas, landed)
}
#[test]
fn a_straddling_placement_gets_one_arena_over_both() {
// #104: 16 MiB at 0x1_0400_0000, then 48 MiB at 0x1_0200_0000 (ends inside the first arena)
let (arenas, landed) = place_all(&[(0x1_0400_0000, 16 * MIB), (0x1_0200_0000, 48 * MIB), (0x1_0400_0000, 16 * MIB)]);
assert_eq!(arenas, [(0x1_0200_0000, 0x1_0C00_0000 - 0x1_0200_0000)]);
// the third placement, at the first one's address, shares the new arena with the second
assert_eq!(landed[1], landed[2]);
}
#[test]
fn a_new_arena_stops_at_the_next_one() {
// 0x1_0400_0000 + 128 MiB first; then 16 MiB below it fits in the 32 MiB gap
let (arenas, landed) = place_all(&[(0x1_0400_0000, 16 * MIB), (0x1_0200_0000, 16 * MIB)]);
assert_eq!(arenas, [(0x1_0400_0000, ARENA), (0x1_0200_0000, 32 * MIB)]);
assert_eq!(landed, [0x1_0400_0000, 0x1_0200_0000]);
}
#[test]
fn a_placement_over_several_arenas_merges_them_all() {
// two arenas with a gap, then one placement over the gap into both
let a = 0x2_0000_0000;
let (arenas, _) = place_all(&[(a, MIB), (a + 160 * MIB, MIB), (a + 100 * MIB, 100 * MIB), (a + 50 * MIB, MIB)]);
assert_eq!(arenas, [(a, 160 * MIB + ARENA)]);
}
}