Skip to content
Open
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
7 changes: 6 additions & 1 deletion crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2756,13 +2756,18 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) {
}

let q_raw = queue as *mut c_void;
super::title_textures::refresh(q_raw);
type ExecFn = unsafe extern "system" fn(*mut c_void, u32, *const *mut c_void);
let exec: ExecFn = transmute_copy(&real::orig_fn(q_raw, PC_QUEUE_EXECUTE_COMMAND_LISTS));

// If this list was previously split mid-recording around a `Wait64`, flush all earlier closed
// segments in exact order before submitting `this`, then resume recording on `primary`.
let kickoff_split = super::title_list::reclaim_for_kickoff(this);
// page budget: evicted textures this list and the segments before it transition are mapped
// again, uploaded by the refresh
let mut lists = kickoff_split.as_ref().map_or_else(Vec::new, |(_, prior, _)| prior.clone());
lists.push(this);
super::title_textures::revive_listed(&lists);
super::title_textures::refresh(q_raw);
if let Some((_, ref prior, _)) = kickoff_split {
if !prior.is_empty() {
execute_with_waits_on(q_raw, prior, |chunk| {
Expand Down
6 changes: 4 additions & 2 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5085,8 +5085,6 @@ unsafe extern "system" fn q_execute_command_lists(this: *mut c_void, n: u32, lis
stop_if_device_removed();
return;
}
// textures in title memory the CPU wrote since: uploaded again first
super::title_textures::refresh(this);
// console-shaped lists run as the PC lists they were translated into (split title lists expand
// into all of their recorded PC segments in exact chronological order)
let mut mapped: Vec<*mut c_void> = Vec::with_capacity(n as usize);
Expand All @@ -5097,6 +5095,10 @@ unsafe extern "system" fn q_execute_command_lists(this: *mut c_void, n: u32, lis
mapped.push(l);
}
}
// page budget: evicted textures these lists transition are mapped again, uploaded below
super::title_textures::revive_listed(&mapped);
// textures in title memory the CPU wrote since: uploaded again first
super::title_textures::refresh(this);
super::list_x::note_queue_for_lists(this as usize, &mapped);
super::list_x::execute_with_waits_on(this, &mapped, |chunk| {
f(this, chunk.len() as u32, chunk.as_ptr());
Expand Down
146 changes: 146 additions & 0 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ pub(super) unsafe fn register(device: *mut c_void, res: *mut c_void, va: usize,
super::vram::unmap_superseded(r);
}
TEXTURES_GEN.fetch_add(1, Ordering::Release);
// page budget: its texels can be uploaded again from the title's memory
super::vram::set_evictable(res as usize);
Ok(())
}

Expand Down Expand Up @@ -464,6 +466,17 @@ pub fn note_gpu_write(res: usize) {
GPU_WRITTEN.fetch_add(1, Ordering::Relaxed);
}
}
drop(t);
// page budget: its texels are not in the title's memory, so the page budget keeps it; one
// already evicted is mapped again now, before the copy runs (the title's queue waits for
// the mapping), and is not uploaded (`gpu_written`). The title holds it while it records.
super::vram::pin(res);
if super::vram::any_evicted() {
let p = res as *mut c_void;
if let Some(r) = unsafe { ID3D12Resource::from_raw_borrowed(&p) } {
unsafe { super::vram::revive(r) };
}
}
}

static GPU_WRITTEN: AtomicU64 = AtomicU64::new(0);
Expand Down Expand Up @@ -570,6 +583,8 @@ pub fn submitted(lists: &[*mut c_void]) {
if notes.is_empty() {
return;
}
// page budget: a texture the title uses is recent
super::vram::touch(notes.iter().map(|&(res, _, _)| res));
let mut t = TEXTURES.lock().unwrap();
for (res, sub, after) in notes {
if let Some(e) = t.get_mut(res) {
Expand All @@ -579,6 +594,46 @@ pub fn submitted(lists: &[*mut c_void]) {
}
}

/// Page budget: before the PC lists `lists` are submitted (and before `refresh`), the evicted
/// textures their transitions name are mapped again and made pending, so this submit uploads
/// them whole before the lists sample them.
pub(super) fn revive_listed(lists: &[*mut c_void]) {
if !super::vram::any_evicted() {
return;
}
let mut named: Vec<usize> = {
let g = LIST_NOTES.lock().unwrap();
lists.iter().filter_map(|&l| g.get(&(l as usize))).flatten().map(|&(res, _, _)| res).filter(|&res| super::vram::is_evicted(res)).collect()
};
if named.is_empty() {
return;
}
named.sort_unstable();
named.dedup();
// a reference each while they are mapped (taken under the lock `forget` takes)
let live = {
let t = TEXTURES.lock().unwrap();
named.retain(|res| t.by_res.contains_key(res) || t.shadowed.contains_key(res));
unsafe { clone_resources(&named) }
};
let revived: Vec<usize> = live.iter().filter(|r| unsafe { super::vram::revive(r) }).map(|r| r.as_raw() as usize).collect();
if revived.is_empty() {
return;
}
for &res in &revived {
forget_content(res);
}
let mut t = TEXTURES.lock().unwrap();
for res in revived {
if let Some(e) = t.get_mut(res) {
if !e.pending && !e.gpu_written {
e.pending = true;
PENDING.fetch_add(1, Ordering::Relaxed);
}
}
}
}

/// Subresources of a texture kept in step (as `subresources` makes them).
fn subresource_count(tex: &xg::Texture) -> usize {
tex.texels().map_or(1, |s| s.array as usize * s.chain.levels.len())
Expand Down Expand Up @@ -776,6 +831,23 @@ unsafe fn refresh_once(queue: *mut c_void) {
})
.collect()
});
// page budget: a texture the CPU wrote again is recent, and one evicted is mapped again
// before its whole upload (one that cannot be mapped now stays evicted and is not uploaded)
let mut todo = todo;
if super::vram::any_evicted() {
todo.retain(|(r, _)| {
let key = r.as_raw() as usize;
if !super::vram::is_evicted(key) {
return true;
}
let back = super::vram::revive(r);
if back {
forget_content(key);
}
back
});
}
super::vram::touch(todo.iter().map(|(r, _)| r.as_raw() as usize));
upload_all(todo);
}

Expand All @@ -795,6 +867,12 @@ fn same_as_last_upload(res: usize, h: u64) -> bool {
LAST_CONTENT.lock().unwrap().insert(res, h) == Some(h)
}

/// Page budget: `res` was mapped again on fresh tiles, so its next upload
/// must not be left out as unchanged.
fn forget_content(res: usize) {
LAST_CONTENT.lock().unwrap().remove(&res);
}

/// A 64-bit hash of `b` for "the same bytes as before" (four lanes of multiply-rotate over
/// 8-byte words: gigabytes a second; not for anything adversarial).
fn content_hash(b: &[u8]) -> u64 {
Expand Down Expand Up @@ -1219,6 +1297,74 @@ mod tests {
forget(b);
}

/// Page budget: before a submit, an evicted texture its lists transition is mapped again and
/// uploaded whole (pending, its content hash forgotten), and a copy into an evicted texture
/// maps it again when it is recorded, with no upload over what the GPU writes (#640 review).
#[test]
fn page_budget_an_evicted_texture_is_mapped_again_before_the_work_that_uses_it() {
use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R8_UNORM, DXGI_SAMPLE_DESC};
let _vram_guard = super::super::vram::VRAM_TEST_LOCK.lock().unwrap();
let _guard = TEST_LOCK.lock().unwrap();
if super::super::backend() != super::super::Backend::Real {
crate::gpu::recomp::runner::skip_test("no GPU");
return;
}
unsafe {
let dev = super::super::real::device_raw();
// 512 x 512 R8: 4 tiles, the smallest texture the budget evicts
let pc = D3D12_RESOURCE_DESC { Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, Width: 512, Height: 512, DepthOrArraySize: 1, MipLevels: 1, Format: DXGI_FORMAT_R8_UNORM, SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, ..Default::default() };
let console = console_desc(3, 512, 512, 1, 1, DXGI_FORMAT_R8_UNORM.0 as u32, 1);
let riid = &ID3D12Resource::IID as *const windows::core::GUID as *const windows_sys::core::GUID;
let place_at = |va: usize| {
let mut p = std::ptr::null_mut();
let hr = super::super::vram::place_pages(dev, va, &pc, D3D12_RESOURCE_STATE_COMMON.0 as u32, std::ptr::null(), Some(512 * 512), riid, &mut p);
if !hr.is_some_and(|hr| hr >= 0) || p.is_null() {
return None;
}
Some(ID3D12Resource::from_raw(p))
};
let (Some(sampled), Some(copied)) = (place_at(0x63_0000_0000), place_at(0x63_1000_0000)) else {
crate::gpu::recomp::runner::skip_test("no reserved textures");
return;
};
let (a, b) = (sampled.as_raw() as usize, copied.as_raw() as usize);
register(dev, sampled.as_raw(), 0x63_0000_0000, &console, D3D12_RESOURCE_STATE_COMMON).unwrap();
register(dev, copied.as_raw(), 0x63_1000_0000, &console, D3D12_RESOURCE_STATE_COMMON).unwrap();
{
// both uploaded once already
let mut t = TEXTURES.lock().unwrap();
for r in [a, b] {
let e = t.get_mut(r).unwrap();
if e.pending {
e.pending = false;
PENDING.fetch_sub(1, Ordering::Relaxed);
}
}
}
LAST_CONTENT.lock().unwrap().insert(a, 7);
assert!(super::super::vram::evict_for_test(a) && super::super::vram::evict_for_test(b));

// a list that samples `a` is submitted: `a` is mapped again first, and uploaded whole
let list = 0x7E57_0640usize;
LIST_NOTES.lock().unwrap().entry(list).or_default().push((a, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
revive_listed(&[list as *mut c_void]);
assert!(!super::super::vram::is_evicted(a), "mapped again before the submit");
assert!(TEXTURES.lock().unwrap().get_mut(a).unwrap().pending, "its upload runs in this submit's refresh");
assert!(!LAST_CONTENT.lock().unwrap().contains_key(&a), "not left out as unchanged");

// a copy into `b` is recorded: mapped again now, and not uploaded over
assert!(super::super::vram::is_evicted(b));
note_gpu_write(b);
assert!(!super::super::vram::is_evicted(b), "mapped again before the copy runs");
assert!(!TEXTURES.lock().unwrap().get_mut(b).unwrap().pending, "the GPU's texels are not uploaded over");

list_reset(list);
forget(a);
forget(b);
drop((sampled, copied));
}
}

/// Real uploads, dirty-page detection and a source changing after registration:
/// bookkeeping alone cannot show that the bytes the GPU sees follow the mirror.
#[test]
Expand Down
Loading
Loading