Skip to content
Draft
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper.
console shaders' hashes (`cs`, `vs` / `ps`, `ms` / `ps`, `vs` / `gs` / `ps`).
- `XCL_TEXTURE_LEVEL_PAGES=1` gives sampled title textures backing as their mip levels stream in.
Zero-filled mip uploads in a batch share one staging span and still clear any mapped tiles.
- `XCL_LAZY_TARGETS=1` defers binding 64 KiB video-memory tiles for GPU-owned placed targets until
their first command-list or upload use, avoiding VRAM allocation for targets created upfront but
never touched.
- `XCL_DSTORAGE_PARALLEL_DECOMPRESS=1` enables parallel CPU decompression for file-backed zlib
requests with virtual destinations. Defaults: four workers and a shared 32 MiB staging budget
(`XCL_DSTORAGE_WORKERS`, `XCL_DSTORAGE_MEMORY_BUDGET_MIB`). Requests retire in queue order.
Expand Down
239 changes: 217 additions & 22 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,38 +1302,97 @@ pub(super) fn bound_pso(this: *mut c_void) -> usize {
with(this, |s| s.pso)
}

/// Remap any superseded targets named in the first 256 slots of each descriptor table in `layout`
/// / `args`. Only called when `placements::any_superseded()` is true.
/// Remap any superseded targets named in the declared SRV/UAV descriptor ranges of `layout` /
/// `args`. Only called when `placements::any_superseded()` is true.
unsafe fn note_superseded_in_tables(u: &crate::gpu::uma::Uma, layout: Option<&Layout>, args: &[u8]) {
let Some(layout) = layout else { return };
const TABLE_SLOTS: usize = 256;
let mut table = [0u8; TABLE_SLOTS * 32];
for p in &layout.params {
let Param::Table { at } = *p else { continue };
let at = at as usize;
scan_superseded_in_tables_with(
layout,
args,
u.base(),
super::console_heap::view_heap_remaining_slots,
|addr, len| u.in_committed_window(addr, len),
|addr, buf| super::title_textures::read_committed_into(addr, buf),
super::console_heap::named_by_index,
super::placements::note_use,
);
}

unsafe fn scan_superseded_in_tables_with(
layout: &Layout,
args: &[u8],
uma_base: usize,
mut heap_remaining: impl FnMut(usize) -> Option<usize>,
mut in_committed: impl FnMut(usize, usize) -> bool,
mut read_committed: impl FnMut(usize, &mut [u8]),
mut named_by_index: impl FnMut(u32) -> usize,
mut on_use: impl FnMut(usize),
) {
for_each_declared_table_slot(layout, args, uma_base, heap_remaining, in_committed, read_committed, |slot| {
let index = u32::from_le_bytes(slot[..4].try_into().unwrap());
if slot[15] >> 4 >= 8 && index != 0 {
let res = named_by_index(index);
if res != 0 && res != super::console_heap::GONE {
on_use(res);
}
}
true
});
}

/// Calls `on_slot` with each 32-byte slot of the SRV/UAV ranges the tables of `layout` declare,
/// read in chunks of 256 slots. False: `on_slot` returned false and the walk stopped there.
unsafe fn for_each_declared_table_slot(
layout: &Layout,
args: &[u8],
uma_base: usize,
mut heap_remaining: impl FnMut(usize) -> Option<usize>,
mut in_committed: impl FnMut(usize, usize) -> bool,
mut read_committed: impl FnMut(usize, &mut [u8]),
mut on_slot: impl FnMut(&[u8]) -> bool,
) -> bool {
const CHUNK_SLOTS: usize = 256;
let mut chunk = [0u8; CHUNK_SLOTS * 32];
for (at, ranges) in &layout.table_ranges {
let at = *at as usize;
let Some(word) = args.get(at..at + 4) else { continue };
let offset = u32::from_le_bytes(word.try_into().unwrap()) as usize;
if offset == 0 {
continue;
}
let addr = u.base() + offset;
if u.in_committed_window(addr, table.len()) {
std::ptr::copy_nonoverlapping(addr as *const u8, table.as_mut_ptr(), table.len());
} else {
table.fill(0);
super::title_textures::read_committed_into(addr, &mut table);
}
for slot in table.chunks_exact(32) {
let index = u32::from_le_bytes(slot[..4].try_into().unwrap());
if slot[15] >> 4 < 8 || index == 0 {
continue;
}
let res = super::console_heap::named_by_index(index);
if res != 0 && res != super::console_heap::GONE {
super::placements::note_use(res);
let Some(table_base) = uma_base.checked_add(offset) else { continue };
let rem = heap_remaining(table_base);
for range in ranges {
let start = range.start as usize;
let total = match (rem, range.count) {
(Some(heap_rem), Some(c)) => (c as usize).min(heap_rem.saturating_sub(start)),
(Some(heap_rem), None) => heap_rem.saturating_sub(start),
(None, Some(c)) => c as usize,
// Unbounded range outside any registered view heap: no safe extent to scan.
(None, None) => 0,
};
let mut done = 0usize;
while done < total {
let n = (total - done).min(CHUNK_SLOTS);
let bytes = n * 32;
let Some(addr) = start.checked_add(done).and_then(|s| s.checked_mul(32)).and_then(|b| table_base.checked_add(b)) else { break };
let buf = &mut chunk[..bytes];
if in_committed(addr, bytes) {
std::ptr::copy_nonoverlapping(addr as *const u8, buf.as_mut_ptr(), bytes);
} else {
buf.fill(0);
read_committed(addr, buf);
}
for slot in buf.chunks_exact(32) {
if !on_slot(slot) {
return false;
}
}
done += n;
}
}
}
true
}

/// `XCL_DUMP_TARGETS`: the resources the first 16 slots of each descriptor table of `this`'s
Expand Down Expand Up @@ -1599,6 +1658,49 @@ pub(super) fn want_root_views(layout: Option<&Layout>, args: &[u8], path: super:
}
}

/// Bind tiles for any unmapped lazy GPU-owned targets referenced through root descriptor tables
/// before recording a translated draw or dispatch (`XCL_LAZY_TARGETS=1`).
#[must_use]
pub(super) unsafe fn ensure_table_lazy_targets(layout: Option<&Layout>, args: &[u8]) -> bool {
if !super::vram::has_unmapped_lazy_targets() {
return true;
}
let Some(l) = layout else { return true };
let Some(u) = crate::gpu::uma::get() else { return true };
ensure_table_lazy_targets_with(
l,
args,
u.base(),
super::console_heap::view_heap_remaining_slots,
|addr, len| u.in_committed_window(addr, len),
|addr, buf| super::title_textures::read_committed_into(addr, buf),
super::console_heap::named_by_index,
|res| super::vram::ensure_target_mapped(res),
)
}

unsafe fn ensure_table_lazy_targets_with(
layout: &Layout,
args: &[u8],
uma_base: usize,
heap_remaining: impl FnMut(usize) -> Option<usize>,
in_committed: impl FnMut(usize, usize) -> bool,
read_committed: impl FnMut(usize, &mut [u8]),
mut named_by_index: impl FnMut(u32) -> usize,
mut ensure_mapped: impl FnMut(usize) -> bool,
) -> bool {
for_each_declared_table_slot(layout, args, uma_base, heap_remaining, in_committed, read_committed, |slot| {
let w0 = u32::from_le_bytes(slot[..4].try_into().unwrap());
if slot[15] >> 4 < 8 || w0 >= super::console_heap::GLOBAL_MAX || (w0 & 1) != 0 {
return true;
}
[w0, w0 + 1].into_iter().all(|idx| {
let res = named_by_index(idx);
res == 0 || res == super::console_heap::GONE || ensure_mapped(res)
})
})
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct DispatchMeshSig {
companion_dispatch: usize,
Expand Down Expand Up @@ -5257,6 +5359,9 @@ unsafe fn bind_abi_with_root_resolved(
if super::placements::any_superseded() {
note_superseded_in_tables(u, layout.as_deref(), &args);
}
if !super::list_x::require_target_pages(this, ensure_table_lazy_targets(layout.as_deref(), &args)) {
return Err("could not bind lazy target pages for a root descriptor table".into());
}
std::ptr::copy_nonoverlapping(args[inline..].as_ptr(), table_at as *mut u8, args.len() - inline);
let io = prepared_abi_io(layout.as_deref(), &args, table_at, gfx, user_sgprs, u.base(), heaps, limits, groups, group_limit);
let io_bytes = io.iter().flat_map(|word| word.to_le_bytes()).collect::<Vec<_>>();
Expand Down Expand Up @@ -5327,6 +5432,96 @@ mod tests {
use std::sync::mpsc;
use std::time::Duration;

/// A superseded target is looked for in the SRV / UAV ranges a table declares, however far
/// from the table's start (the first 256 slots only, before: a view at slot 300 was never
/// seen), and not in the slots between them. An unbounded range ends with the view heap the
/// table lies in; outside any view heap it is not read (no extent known).
#[test]
fn superseded_targets_are_found_in_every_declared_table_range() {
use console_list::TableRange;
const SLOTS: usize = 512;
let (uma_base, offset) = (0x1_0000_0000usize, 0x4000u32);
let table = uma_base + offset as usize;
// T#s (type nibble 8 in byte 15) naming shared-heap indexes 10 + k, at slots 10, 300 and 400
let mut mem = vec![0u8; SLOTS * 32];
for k in [10usize, 300, 400] {
mem[32 * k..32 * k + 4].copy_from_slice(&(10 + k as u32).to_le_bytes());
mem[32 * k + 15] = 0x80;
}
let args = offset.to_le_bytes().to_vec();
let found = |ranges: Vec<TableRange>, heap_slots: Option<usize>| {
let layout = Layout { table_ranges: vec![(0, ranges)], ..Default::default() };
let mut used = Vec::new();
unsafe {
scan_superseded_in_tables_with(
&layout,
&args,
uma_base,
|at| heap_slots.map(|n| n - (at - table) / 32),
|_, _| false,
|at, buf| buf.copy_from_slice(&mem[at - table..at - table + buf.len()]),
|index| 0x7000 + index as usize,
|res| used.push(res),
);
}
used
};
// an SRV range of 8 at slot 300: the target there, not the one at slot 10
assert_eq!(found(vec![TableRange { start: 300, count: Some(8) }], Some(SLOTS)), vec![0x7000 + 310]);
// two ranges: both of their targets
assert_eq!(found(vec![TableRange { start: 0, count: Some(16) }, TableRange { start: 390, count: Some(16) }], Some(SLOTS)), vec![0x7000 + 20, 0x7000 + 410]);
// unbounded from 300: up to the view heap's end
assert_eq!(found(vec![TableRange { start: 300, count: None }], Some(SLOTS)), vec![0x7000 + 310, 0x7000 + 410]);
// unbounded outside any view heap: nothing read
assert!(found(vec![TableRange { start: 0, count: None }], None).is_empty());
// a bounded range is cut at the view heap's end
assert_eq!(found(vec![TableRange { start: 300, count: Some(1000) }], Some(350)), vec![0x7000 + 310]);
}

/// A lazy target bound through a descriptor table gets its tiles wherever its view lies in a
/// declared SRV / UAV range (the first 64 slots only, before: a view at slot 100 kept no
/// tiles), not in the slots between ranges; a failed mapping refuses the table.
#[test]
fn lazy_targets_are_mapped_in_every_declared_table_range() {
use console_list::TableRange;
const SLOTS: usize = 256;
let (uma_base, offset) = (0x1_0000_0000usize, 0x4000u32);
let table = uma_base + offset as usize;
// T#s (type nibble 8 in byte 15) naming even shared-heap indexes 2 * k, at slots 10, 100 and 200
let mut mem = vec![0u8; SLOTS * 32];
for k in [10usize, 100, 200] {
mem[32 * k..32 * k + 4].copy_from_slice(&(2 * k as u32).to_le_bytes());
mem[32 * k + 15] = 0x80;
}
let args = offset.to_le_bytes().to_vec();
let mapped = |ranges: Vec<TableRange>, ok: bool| {
let layout = Layout { table_ranges: vec![(0, ranges)], ..Default::default() };
let mut asked = Vec::new();
let done = unsafe {
ensure_table_lazy_targets_with(
&layout,
&args,
uma_base,
|at| Some(SLOTS - (at - table) / 32),
|_, _| false,
|at, buf| buf.copy_from_slice(&mem[at - table..at - table + buf.len()]),
|index| 0x7000 + index as usize,
|res| {
asked.push(res);
ok
},
)
};
(done, asked)
};
// a range of 8 at slot 96: the view at slot 100 (and its pair), not the one at slot 10
assert_eq!(mapped(vec![TableRange { start: 96, count: Some(8) }], true), (true, vec![0x7000 + 200, 0x7000 + 201]));
// unbounded from 0: every view up to the view heap's end
assert_eq!(mapped(vec![TableRange { start: 0, count: None }], true).1, vec![0x7000 + 20, 0x7000 + 21, 0x7000 + 200, 0x7000 + 201, 0x7000 + 400, 0x7000 + 401]);
// a mapping that fails stops the walk and refuses the table
assert_eq!(mapped(vec![TableRange { start: 96, count: Some(8) }], false), (false, vec![0x7000 + 200]));
}

#[test]
fn aligned_list_addresses_reach_many_bind_shards() {
let shards = (0..256).map(|index| state_shard_index(0x7100_0000 + index * 16)).collect::<HashSet<_>>();
Expand Down
9 changes: 9 additions & 0 deletions crates/xcl/src/hle/d3d12x/console_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,15 @@ fn finish_view_slot(h: &Heap, k: u32, page: Option<(u32, u32)>) -> Slot {
Slot { generation: h.generation, at: h.shadow + (k * SLOT) as usize, index, pc_srv: h.pc_cpu + (2 * k * h.inc) as u64, pc_uav: h.pc_cpu + uav, pc_gpu_uav, sampler: false, shared_srv, k, remaining: h.n - k }
}

/// Remaining 32-byte view slots from `addr` to the end of the containing registered view heap,
/// without allocating shared-heap pages. Returns `None` if `addr` is not inside a view heap.
pub fn view_heap_remaining_slots(addr: usize) -> Option<usize> {
let heaps = HEAPS.lock().unwrap();
let h = heaps.iter().find(|h| !h.sampler && addr >= h.shadow && addr < h.shadow + (h.n as usize) * (SLOT as usize))?;
let k = (addr - h.shadow) / (SLOT as usize);
Some((h.n as usize).saturating_sub(k))
}

/// The slot behind a console CPU handle (None: not one of XCL's console heaps). A view slot
/// of a heap in the shared heap gets its page's place there now if the page has none yet; one
/// whose page finds no room there gets index 0, the null SRV / UAV pair.
Expand Down
Loading
Loading