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
62 changes: 62 additions & 0 deletions crates/xcl/src/hle/d3d12x/console_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,42 @@ pub fn resource_at(at: usize) -> Option<usize> {
RES.lock().unwrap().as_ref()?.get(&at).copied()
}

/// shared-heap SRV index → the texture on pages its view names (page budget, census). A T#
/// keeps that index wherever the title copies it, so the slots holding it name the texture.
static INDEX_RES: Mutex<Option<std::collections::HashMap<u32, usize>>> = Mutex::new(None);

/// The SRV made for slot `s` names `res`, a texture on pages. False: the slot's heap is not
/// in the shared heap, so its T# cannot be followed.
pub fn note_index(s: &Slot, res: usize) -> bool {
if s.sampler || s.shared_srv == 0 {
return false;
}
INDEX_RES.lock().unwrap().get_or_insert_with(Default::default).insert(s.index, res);
true
}

/// The textures on pages whose T# is in a slot now: (in a shader-visible view heap, only in
/// CPU-only ones).
pub fn referenced() -> (Vec<usize>, Vec<usize>) {
let index = INDEX_RES.lock().unwrap();
let Some(index) = index.as_ref().filter(|m| !m.is_empty()) else { return (Vec::new(), Vec::new()) };
let heaps = HEAPS.lock().unwrap();
let (mut visible, mut cpu) = (std::collections::HashSet::new(), std::collections::HashSet::new());
for h in heaps.iter().filter(|h| !h.sampler) {
let set = if h.visible { &mut visible } else { &mut cpu };
for k in 0..h.n {
let w = unsafe { std::slice::from_raw_parts((h.shadow + (k * SLOT) as usize) as *const u32, 4) };
if w[3] >> 28 >= 8 {
if let Some(&r) = index.get(&w[0]) {
set.insert(r);
}
}
}
}
let cpu = cpu.difference(&visible).copied().collect();
(visible.into_iter().collect(), cpu)
}

pub fn write(s: &Slot, words: &[u32]) {
let n = words.len().min(if s.sampler { 4 } else { 8 });
unsafe { std::ptr::copy_nonoverlapping(words.as_ptr(), s.at as *mut u32, n) };
Expand Down Expand Up @@ -504,6 +540,32 @@ mod tests {
assert!(free.is_empty());
}

#[test]
fn textures_are_referenced_by_the_heaps_their_t_is_in() {
let Some(_) = crate::gpu::uma::get() else { return };
let (a, b, c) = (0x7FFF_0001u32, 0x7FFF_0003, 0x7FFF_0005);
INDEX_RES.lock().unwrap().get_or_insert_with(Default::default).extend([(a, 0xAAA0), (b, 0xBBB0), (c, 0xCCC0)]);
assert!(register(0xC1, 4, 0x2000, 0x6000, 32, false, false, true));
assert!(register(0xC2, 4, 0x3000, 0, 32, false, false, false));
let (v, cpu) = (start(0xC1).unwrap(), start(0xC2).unwrap());
let image = |i: u32| [i, 0x0470_0000, 0, 9 << 28, 0, 0, 0, 0];
// a: in both kinds of heap; b: only in the CPU-only one; c: as a buffer V#'s word 0
write(&slot(v).unwrap(), &image(a));
write(&slot(v + 32).unwrap(), &[c, 0, 64, 0x3101_6FAC, 0, 0, 0, 0]);
write(&slot(cpu).unwrap(), &image(a));
write(&slot(cpu + 32).unwrap(), &image(b));
let (visible, cpu_only) = referenced();
assert!(visible.contains(&0xAAA0) && !visible.contains(&0xBBB0) && !visible.contains(&0xCCC0), "{visible:x?}");
assert!(cpu_only.contains(&0xBBB0) && !cpu_only.contains(&0xAAA0) && !cpu_only.contains(&0xCCC0), "{cpu_only:x?}");
unregister(0xC1);
unregister(0xC2);
if let Some(m) = INDEX_RES.lock().unwrap().as_mut() {
for i in [a, b, c] {
m.remove(&i);
}
}
}

#[test]
fn slots_copy_and_release() {
let Some(u) = crate::gpu::uma::get() else { return };
Expand Down
6 changes: 6 additions & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,12 @@ unsafe extern "system" fn dev_create_srv_x(this: *mut c_void, res: *mut c_void,
f(this, res, desc, slot.pc_srv);
super::console_heap::publish(&slot, false);
super::console_heap::note_resource(&slot, res as usize);
if super::vram::tracking() && super::vram::is_placed_texture(res as usize) {
// a view the heaps cannot be followed to: never taken for unused
if !super::console_heap::note_index(&slot, res as usize) {
super::vram::pin(res as usize);
}
}
if super::present::trace_frame() && super::title_textures::address(res as usize).is_some() {
static N: AtomicU64 = AtomicU64::new(0);
if N.fetch_add(1, Ordering::Relaxed) < 24 {
Expand Down
97 changes: 97 additions & 0 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@ 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
super::vram::pin(res);
}

static GPU_WRITTEN: AtomicU64 = AtomicU64::new(0);
Expand Down Expand Up @@ -557,6 +560,58 @@ pub fn list_reset(list: usize) {
}
}

/// Page budget: evicted textures `revive` are mapped again and uploaded whole at the next look.
/// One that could not be mapped stays evicted, for the next use to try again.
fn map_again(revive: &[usize]) {
let mut mapped = Vec::with_capacity(revive.len());
for &res in revive {
// released since it was taken from the evicted (its pages forgotten): not touched
if !super::vram::is_placed_texture(res) {
continue;
}
let p = res as *mut c_void;
if !unsafe { ID3D12Resource::from_raw_borrowed(&p).is_some_and(|r| super::vram::remap_if_unmapped(r)) } {
super::vram::evicted_again(res);
continue;
}
forget_content(res);
mapped.push(res);
}
if mapped.is_empty() {
return;
}
let mut t = TEXTURES.lock().unwrap();
for res in mapped {
if let Some(e) = t.get_mut(res) {
if !e.pending {
e.pending = true;
PENDING.fetch_add(1, Ordering::Relaxed);
}
}
}
}

/// Page budget: about once a second, the textures whose T# is in a shader-visible view heap
/// are in use (a title that only samples a texture submits no transition for it); evicted ones
/// among them are mapped again before the title's next work.
fn look_at_view_heaps() {
if !super::vram::tracking() || super::console_heap::global_heap().is_none() {
return;
}
static LAST: Mutex<Option<std::time::Instant>> = Mutex::new(None);
{
let mut last = LAST.lock().unwrap();
let now = std::time::Instant::now();
if last.is_some_and(|l| now.duration_since(l) < std::time::Duration::from_secs(1)) {
return;
}
*last = Some(now);
}
let (visible, cpu_only) = super::console_heap::referenced();
let back = super::vram::seen(&visible, &cpu_only);
map_again(&back);
}

/// The PC lists `lists` were submitted, in this order: the textures kept in step are in the
/// states their transitions leave them in.
pub fn submitted(lists: &[*mut c_void]) {
Expand All @@ -570,6 +625,16 @@ pub fn submitted(lists: &[*mut c_void]) {
if notes.is_empty() {
return;
}
// page budget: a texture the title uses is recent; an evicted one is
// mapped again and uploaded whole at the next submit
let mut revive = Vec::new();
for &(res, _, _) in &notes {
super::vram::touch(res);
if super::vram::any_evicted() && super::vram::take_evicted(res) {
revive.push(res);
}
}
map_again(&revive);
let mut t = TEXTURES.lock().unwrap();
for (res, sub, after) in notes {
if let Some(e) = t.get_mut(res) {
Expand Down Expand Up @@ -727,6 +792,9 @@ unsafe fn refresh_once(queue: *mut c_void) {
crate::xlog_info!("frame trace: title textures: {with} with data in memory, {without} all zero; {dirty_len} dirty pages this look");
}
}
// page budget: textures in use by their T#; evicted ones are mapped again (before the
// queue waits for the mappings) and uploaded below
look_at_view_heaps();
// the title's own memory: what the CPU wrote goes to its video memory first
if let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&queue) {
DIRTY_BUF.with(|dcell| {
Expand Down Expand Up @@ -765,6 +833,16 @@ unsafe fn refresh_once(queue: *mut c_void) {
})
.collect()
});
// page budget: a texture the CPU wrote again is mapped again before its
// whole upload
if super::vram::any_evicted() {
for (r, _) in &todo {
if super::vram::take_evicted(r.as_raw() as usize) {
super::vram::remap_if_unmapped(r);
forget_content(r.as_raw() as usize);
}
}
}
upload_all(todo);
}

Expand All @@ -784,6 +862,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 @@ -1147,6 +1231,19 @@ mod tests {
forget(b);
}

/// Page budget: a texture mapped again after an eviction sits on fresh
/// tiles, so unchanged memory is uploaded again.
#[test]
fn a_texture_mapped_again_after_eviction_is_uploaded_even_if_unchanged() {
let a = 0x7E57_0000_3000usize;
let h = content_hash(&[7u8; 4096]);
assert!(!same_as_last_upload(a, h));
assert!(same_as_last_upload(a, h), "unchanged: left out");
forget_content(a);
assert!(!same_as_last_upload(a, h), "mapped again: uploaded whole");
forget_content(a);
}

/// 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