Skip to content
Closed
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 @@ -282,6 +282,9 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder
- `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.
- `XCL_TEXTURE_LEVEL_PAGES=1` gives eligible single-slice sampled textures video memory for each
mip when an upload has texels for it. If a mip cannot get pages, XCL retries it the next time
the texture is selected for upload, even when its source bytes are unchanged.
- Every run writes `report.json` (called stubs, unknown D3D12.X slots and interfaces, XGameRuntime
classes asked for, shader statistics, audio formats, crash).
`d3d12.presents` counts presentations, not visible content. With `XCL_FRAME_DUMP=N` (`N > 1`),
Expand Down
36 changes: 27 additions & 9 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,9 +1132,10 @@ unsafe fn upload_all(todo: Vec<(ID3D12Resource, Upload)>) {
if super::present::trace_frame() {
note_content(va, &r, &subs);
}
if super::vram::level_pages() {
// (the queue waits for these mappings before the copies: `vram::before_execute` below)
map_levels_with_texels(&r, &subs);
if super::vram::level_pages() && !map_levels_with_texels(&r, &subs) {
// The copies into unmapped tiles will be dropped. Upload the backed levels,
// and let the next refresh that selects this texture retry the same bytes.
LAST_CONTENT.lock().unwrap().remove(&(r.as_raw() as usize));
}
batch.push((r, subs, states));
}
Expand Down Expand Up @@ -1167,14 +1168,15 @@ unsafe fn upload_all(todo: Vec<(ID3D12Resource, Upload)>) {
/// placed with lazy levels; any other: nothing to do. A level of zeros has no texels: a streamed
/// texture's larger levels before the title has memory behind them (read as zeros,
/// `read_committed`). It keeps no pages, reads as zeros as it would with them, and its copy goes
/// to tiles that are not mapped, which drops it (tiled resources tier 2).
unsafe fn map_levels_with_texels(r: &ID3D12Resource, subs: &[Subresource]) {
/// to tiles that are not mapped, which drops it (tiled resources tier 2). Returns whether all
/// levels with texels have pages. The upload queue waits for their mappings in `before_execute`.
unsafe fn map_levels_with_texels(r: &ID3D12Resource, subs: &[Subresource]) -> bool {
if !super::vram::has_lazy_levels(r.as_raw() as usize) {
return;
return true;
}
// (one slice: its subresources are its levels)
let present: Vec<u32> = subs.iter().enumerate().filter(|(_, s)| !all_zero(&s.data)).map(|(i, _)| i as u32).collect();
super::vram::map_levels(r, Some(&present));
super::vram::map_levels(r, Some(&present))
}

/// Whether every byte of `b` is zero (eight at a time: gigabytes a second where it is).
Expand Down Expand Up @@ -2378,7 +2380,8 @@ mod tests {
/// first upload, then pages for those levels only and their texels right, zeros where there
/// are none. With memory under the largest level too, a texture placed there again (as a
/// title does) takes that level's pages - the others are shared - and holds all of it. A
/// texture the GPU writes gets the pages of all its levels.
/// texture the GPU writes gets the pages of all its levels. If a level cannot get pages,
/// its texels can still be uploaded when the same source is selected again.
#[test]
fn a_placed_texture_gets_the_pages_of_a_level_with_its_texels() {
let _vram_guard = super::super::vram::VRAM_TEST_LOCK.lock().unwrap();
Expand Down Expand Up @@ -2494,8 +2497,23 @@ mod tests {
assert!(!VirtualAlloc((at + top) as *const c_void, size - top, MEM_COMMIT, PAGE_READWRITE).is_null());
std::ptr::copy_nonoverlapping(whole.as_ptr().add(top), (at + top) as *mut u8, size - top);
let full = place_tex(at);
let full_key = full.as_raw() as usize;
super::super::vram::FAIL_LEVEL_BIND_FOR_TEST.with(|failure| failure.set(Some((full_key, 0))));
refresh(queue.as_raw());
assert_eq!(dev_pages() - p0, ((all - top_tiles) as usize) << 16, "the failed mapping left mip 0 without pages");
assert!(read_sub(&full, 0).iter().all(|&b| b == 0), "the failed mip still reads as zeros");
assert!(read_sub(&full, 1) == want[1].data, "the other mips still upload");

// Offer the texture again without changing any of its source bytes.
{
let mut textures = TEXTURES.lock().unwrap();
let entry = textures.by_res.get_mut(&full_key).unwrap();
assert!(!entry.pending);
entry.pending = true;
adjust_pending(1);
}
refresh(queue.as_raw());
assert_eq!(dev_pages() - p0, (all as usize) << 16, "mip 0's pages come with its texels; the other levels' are shared");
assert_eq!(dev_pages() - p0, (all as usize) << 16, "retry maps mip 0 with the same source bytes; the other levels are shared");
for sub in 0..11u32 {
assert!(read_sub(&full, sub) == want[sub as usize].data, "level {sub} once all of it is there");
}
Expand Down
58 changes: 41 additions & 17 deletions crates/xcl/src/hle/d3d12x/vram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ pub(super) fn level_pages() -> bool {
#[cfg(test)]
pub(super) static LEVEL_PAGES_FOR_TEST: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

#[cfg(test)]
thread_local! {
pub(super) static FAIL_LEVEL_BIND_FOR_TEST: std::cell::Cell<Option<(usize, u32)>> = const { std::cell::Cell::new(None) };
}

/// Of the textures with lazy levels there are now: how many, the tiles they stand for, and the
/// tiles of them that have pages; and the level groups given pages so far.
static LEVEL_STATS: [std::sync::atomic::AtomicU64; 4] = [const { std::sync::atomic::AtomicU64::new(0) }; 4];
Expand Down Expand Up @@ -788,6 +793,16 @@ impl Pages {
/// `bind` for the tiles `range` of the resource's `total`, which start at subresource `sub`.
#[allow(clippy::too_many_arguments)]
unsafe fn bind_range(&mut self, dev: &ID3D12Device, res: &ID3D12Resource, va: usize, console_bytes: Option<u64>, total: u32, sub: u32, range: std::ops::Range<usize>) -> Option<(Vec<usize>, Vec<(usize, usize)>)> {
#[cfg(test)]
if FAIL_LEVEL_BIND_FOR_TEST.with(|failure| {
if failure.get() != Some((res.as_raw() as usize, sub)) {
return false;
}
failure.take();
true
}) {
return None;
}
let (first, own) = (total > 0).then(|| owned_pages(va, console_bytes, total as usize)).flatten()?;
let now = std::time::Instant::now();
self.sweep(now);
Expand Down Expand Up @@ -1280,35 +1295,45 @@ pub unsafe fn place_pages(
place_on_pages(dev, this, va, desc, state, clear, console_bytes, false, lazy, riid, ppv)
}

/// The levels `levels` of `res` (None: all of them) get their pages if they have none yet: a
/// texture placed with lazy levels (any other: nothing to do). Returns the levels that got pages
/// by this call - the queue the texture is used on next has to wait for that (`before_execute`),
/// and what the pages held before is another texture's.
pub(super) unsafe fn map_levels(res: &ID3D12Resource, levels: Option<&[u32]>) -> Vec<u32> {
/// Give the requested levels of a lazy texture pages (None: all levels). Returns whether every
/// requested level has pages. Other textures have nothing to map. The queue that uses the
/// texture waits for the mappings in `before_execute`.
pub(super) unsafe fn map_levels(res: &ID3D12Resource, levels: Option<&[u32]>) -> bool {
if levels.is_some_and(|levels| levels.is_empty()) {
return true;
}
let key = res.as_raw() as usize;
let mut g = PAGES.lock().unwrap();
for (&dev_key, pages) in g.iter_mut() {
let Some(pages) = pages.as_mut() else { continue };
let Some(t) = pages.targets.get(&key) else { continue };
let Some(lv) = t.levels.as_ref().filter(|_| t.mapped) else { return Vec::new() };
let Some(lv) = t.levels.as_ref() else { return true };
if !t.mapped {
return false;
}
let mut wanted: Vec<usize> = match levels {
Some(l) => l.iter().map(|&x| lv.group_of(x)).collect(),
None => (0..lv.groups.len()).collect(),
};
wanted.sort_unstable();
wanted.dedup();
let todo: Vec<(usize, u32, u32, u32, std::ops::Range<u32>)> =
wanted.into_iter().filter(|&k| !lv.groups[k].3).map(|k| (k, lv.groups[k].0, lv.groups[k].1, lv.groups[k].2, lv.levels_of(k))).collect();
let todo: Vec<(usize, u32, u32, u32)> = wanted.into_iter().filter_map(|k| {
let (_, first, count, mapped) = lv.groups[k];
(!mapped).then(|| (k, lv.levels_of(k).start, first, count))
}).collect();
if todo.is_empty() {
return Vec::new();
return true;
}
let (va, console_bytes, total) = (t.va, t.console_bytes, t.total);
let d = dev_key as *mut c_void;
let Some(dev) = ID3D12Device::from_raw_borrowed(&d) else { return Vec::new() };
let mut fresh = Vec::new();
for (k, sub, first, n, levels) in todo {
let Some((used, used_extra)) = pages.bind_range(dev, res, va, console_bytes, total, sub, first as usize..(first + n) as usize) else { continue };
let Some(t) = pages.targets.get_mut(&key) else { return fresh };
let Some(dev) = ID3D12Device::from_raw_borrowed(&d) else { return false };
let mut complete = true;
for (k, sub, first, n) in todo {
let Some((used, used_extra)) = pages.bind_range(dev, res, va, console_bytes, total, sub, first as usize..(first + n) as usize) else {
complete = false;
continue;
};
let Some(t) = pages.targets.get_mut(&key) else { return false };
t.used.extend(used);
t.used_extra.extend(used_extra);
if let Some(l) = t.levels.as_mut() {
Expand All @@ -1320,11 +1345,10 @@ pub(super) unsafe fn map_levels(res: &ID3D12Resource, levels: Option<&[u32]>) ->
let (textures, stand, mapped, _) = level_stats();
crate::xlog_info!("D3D12.X: level pages: {textures} textures standing for {} MB have pages for {} MB ({groups} levels given pages so far)", stand >> 4, mapped >> 4);
}
fresh.extend(levels);
}
return fresh;
return complete;
}
Vec::new()
true
}

/// Whether `resource` is a texture whose levels get their pages one by one (`map_levels`).
Expand Down
Loading