Skip to content
Merged
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
157 changes: 154 additions & 3 deletions crates/xcl/src/hle/d3d12x/console_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ pub fn global_heap() -> Option<ID3D12DescriptorHeap> {

/// Whether `obj` (a title's view heap) has its descriptors in the shared heap.
pub fn in_global(obj: usize) -> bool {
HEAPS.lock().unwrap().iter().any(|h| h.obj == obj && !h.pages.is_empty())
HEAPS.lock().unwrap().iter().any(|h| h.obj == obj && !h.sampler && !h.pages.is_empty())
}

/// Memory for a heap of `bytes` from the title's allocation hook – where the title expects the
Expand Down Expand Up @@ -569,7 +569,7 @@ pub fn census() {
let w = unsafe { std::slice::from_raw_parts((h.shadow + (k * SAMPLER_SLOT) as usize) as *const u32, 4) };
if w.iter().all(|&x| x == 0) {
empty += 1;
} else if w[3] & 0xFFF == k {
} else if w[3] & 0xFFF == sampler_own_index(&h.pages, k) {
own += 1;
} else {
other += 1;
Expand Down Expand Up @@ -647,6 +647,14 @@ fn sampler_unmapped_index(hosted: bool, k: u32) -> u32 {
if hosted { 0 } else { k }
}

/// Expected PC sampler index for slot `k` of a sampler heap with shared-page table `pages`.
fn sampler_own_index(pages: &[u32], k: u32) -> u32 {
match pages.get((k / SAMPLER_PAGE_SLOTS) as usize) {
Some(&f) if f != UNMAPPED => f + (k % SAMPLER_PAGE_SLOTS),
_ => sampler_unmapped_index(!pages.is_empty(), k),
}
}

struct GlobalSampler {
dev: ID3D12Device,
heap: ID3D12DescriptorHeap,
Expand Down Expand Up @@ -749,8 +757,11 @@ fn sampler_page_of(h: &mut Heap, k: u32) -> Option<(u32, u32)> {
}

fn release_global_sampler(first: u32, count: u32) {
if count == 0 {
return;
}
if let Some(s) = GLOBAL_SAMPLER.lock().unwrap().as_mut() {
s.free.push((first, count));
merge_release(&mut s.free, first, count);
}
}

Expand Down Expand Up @@ -1025,4 +1036,144 @@ mod tests {
assert_eq!(sampler_unmapped_index(true, 42), 0);
assert_eq!(sampler_unmapped_index(false, 42), 42);
}

#[test]
fn release_global_sampler_merges_adjacent_ranges_and_ignores_zero_count() {
const CHILD: &str = "XCL_TEST_RELEASE_GLOBAL_SAMPLER_CHILD";
if std::env::var_os(CHILD).is_none() {
let output = std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", "hle::d3d12x::console_heap::tests::release_global_sampler_merges_adjacent_ranges_and_ignores_zero_count", "--nocapture"])
.env(CHILD, "1")
.output()
.unwrap();
assert!(output.status.success(), "{}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr));
return;
}
use std::ffi::c_void;
#[repr(C)]
struct Object { vt: *const usize }
unsafe extern "system" fn retain(_this: *mut c_void) -> u32 { 1 }
let mut vt = [0usize; 4];
vt[1] = retain as usize;
vt[2] = retain as usize;
let mut device = Object { vt: vt.as_ptr() };
let mut heap = Object { vt: vt.as_ptr() };
unsafe {
*GLOBAL_SAMPLER.lock().unwrap() = Some(GlobalSampler {
dev: ID3D12Device::from_raw(&mut device as *mut _ as *mut c_void),
heap: ID3D12DescriptorHeap::from_raw(&mut heap as *mut _ as *mut c_void),
cpu: 0x1000, inc: 32, cap: 17, next: 1, free: Vec::new(),
});
assert_eq!(reserve_global_sampler(4), Some(1));
assert_eq!(reserve_global_sampler(4), Some(5));
assert_eq!(reserve_global_sampler(4), Some(9));
assert_eq!(reserve_global_sampler(4), Some(13));
assert_eq!(reserve_global_sampler(1), None);
// Adjacent out-of-order releases coalesce and zero-count releases are ignored.
release_global_sampler(5, 4);
release_global_sampler(1, 4);
release_global_sampler(1, 0);
assert_eq!(GLOBAL_SAMPLER.lock().unwrap().as_ref().unwrap().free, vec![(1, 8)]);
assert_eq!(reserve_global_sampler(8), Some(1));
// Coalesced range ending at cursor straddles cleanly into the never-used tail.
{
let mut g = GLOBAL_SAMPLER.lock().unwrap();
let s = g.as_mut().unwrap();
s.next = 13;
s.free.clear();
}
release_global_sampler(9, 2);
release_global_sampler(11, 2);
assert_eq!(reserve_global_sampler(8), Some(9));
let g = GLOBAL_SAMPLER.lock().unwrap();
let s = g.as_ref().unwrap();
assert_eq!(s.next, 17);
assert!(s.free.is_empty());
drop(g);
*GLOBAL_SAMPLER.lock().unwrap() = None;
}
}

#[test]
fn in_global_excludes_shared_sampler_heaps() {
const CHILD: &str = "XCL_TEST_IN_GLOBAL_SAMPLER_CHILD";
if std::env::var_os(CHILD).is_none() {
let output = std::process::Command::new(std::env::current_exe().unwrap())
.args(["--exact", "hle::d3d12x::console_heap::tests::in_global_excludes_shared_sampler_heaps", "--nocapture"])
.env(CHILD, "1")
.output()
.unwrap();
assert!(output.status.success(), "{}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr));
return;
}
use std::ffi::c_void;
#[repr(C)]
struct Object { vt: *const usize }
unsafe extern "system" fn retain(_this: *mut c_void) -> u32 { 1 }
let mut vt = [0usize; 4];
vt[1] = retain as usize;
vt[2] = retain as usize;
let mut device = Object { vt: vt.as_ptr() };
let mut view_heap = Object { vt: vt.as_ptr() };
let mut sampler_heap = Object { vt: vt.as_ptr() };
unsafe {
let dev = ID3D12Device::from_raw(&mut device as *mut _ as *mut c_void);
*GLOBAL.lock().unwrap() = Some(Global {
dev: dev.clone(),
heap: ID3D12DescriptorHeap::from_raw(&mut view_heap as *mut _ as *mut c_void),
cpu: 0x1000, gpu: 0x2000, inc: 32, cap: 1024, next: 64, free: Vec::new(),
});
*GLOBAL_SAMPLER.lock().unwrap() = Some(GlobalSampler {
dev,
heap: ID3D12DescriptorHeap::from_raw(&mut sampler_heap as *mut _ as *mut c_void),
cpu: 0x4000, inc: 32, cap: 17, next: 1, free: Vec::new(),
});
if crate::gpu::uma::get().is_some() {
assert!(register(0xC100, 16, 0x9000, 0, 16, true, true, true));
assert!(register(0xC101, 16, 0x1000, 0, 32, false, true, true));
assert!(register(0xC102, 4, 0xA000, 0, 16, true, false, true));
assert!(!in_global(0xC100) && in_global_sampler(0xC100));
assert!(in_global(0xC101) && !in_global_sampler(0xC101));
assert!(!in_global(0xC102) && !in_global_sampler(0xC102));
unregister(0xC100);
unregister(0xC101);
unregister(0xC102);
// Adjacent 4-slot sampler heaps coalesce in GLOBAL_SAMPLER on unregister.
assert!(register(0xC110, 4, 0x9000, 0, 16, true, true, true));
assert!(register(0xC111, 4, 0x9100, 0, 16, true, true, true));
assert_eq!(slot(start(0xC110).unwrap()).unwrap().index, 1);
assert_eq!(slot(start(0xC111).unwrap()).unwrap().index, 5);
unregister(0xC110);
unregister(0xC111);
assert_eq!(GLOBAL_SAMPLER.lock().unwrap().as_ref().unwrap().free, vec![(1, 8)]);
} else {
let mut heaps = HEAPS.lock().unwrap();
heaps.push(Heap { obj: 0xC100, shadow: 0, n: 16, pc_cpu: 0, pc_gpu: 0, inc: 16, sampler: true, pages: vec![UNMAPPED; 2], visible: true, memory: Memory::Uma });
heaps.push(Heap { obj: 0xC101, shadow: 0, n: 16, pc_cpu: 0, pc_gpu: 0, inc: 32, sampler: false, pages: vec![UNMAPPED], visible: true, memory: Memory::Uma });
drop(heaps);
assert!(!in_global(0xC100) && in_global_sampler(0xC100));
assert!(in_global(0xC101) && !in_global_sampler(0xC101));
HEAPS.lock().unwrap().clear();
}
*GLOBAL_SAMPLER.lock().unwrap() = None;
*GLOBAL.lock().unwrap() = None;
}
}

#[test]
fn sampler_own_index_tracks_mapped_pages_and_fallbacks() {
// Hosted shared sampler heap with two mapped pages at GLOBAL_SAMPLER indices 1 and 17.
let pages = [1u32, 17u32];
assert_eq!(sampler_own_index(&pages, 0), 1);
assert_eq!(sampler_own_index(&pages, SAMPLER_PAGE_SLOTS - 1), SAMPLER_PAGE_SLOTS);
assert_eq!(sampler_own_index(&pages, SAMPLER_PAGE_SLOTS), 17);
assert_eq!(sampler_own_index(&pages, SAMPLER_PAGE_SLOTS + 5), 22);
// Unmapped or out-of-range page in a hosted shared sampler heap maps to reserved slot 0.
assert_eq!(sampler_own_index(&[UNMAPPED], 0), 0);
assert_eq!(sampler_own_index(&[1, UNMAPPED], SAMPLER_PAGE_SLOTS + 1), 0);
assert_eq!(sampler_own_index(&[1], SAMPLER_PAGE_SLOTS * 2), 0);
// Unhosted sampler heap (empty pages) keeps local slot index k.
assert_eq!(sampler_own_index(&[], 0), 0);
assert_eq!(sampler_own_index(&[], 42), 42);
}
}
Loading