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
14 changes: 14 additions & 0 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,22 @@ pub(super) unsafe extern "system" fn set_graphics_root_constants(this: *mut c_vo
/// #40 / #42)
pub(super) unsafe extern "system" fn set_graphics_root_descriptor(this: *mut c_void, i: u32, va: u64) {
note_address(va);
let va = readable_root(va);
put_in(this, true, i, 0, &va.to_le_bytes());
}

/// The address a root descriptor at `va` gets (`sparse::gpu_view`): a root descriptor has no
/// size, so as much of the tile is taken as lies in the view. A read through it is in step with
/// the title's memory, a write is not (a root UAV in a GPU view stays what it was before).
fn readable_root(va: u64) -> u64 {
for len in [0x1_0000usize, 0x1000] {
if let Some(b) = super::sparse::gpu_view::base_for(va as usize, len) {
return b as u64;
}
}
va
}

/// `XCL_TRACE_ADDRESSES=1`: root descriptor addresses of console code – in unified memory
/// (translated shaders see them) or not – counted and logged
fn note_address(va: u64) {
Expand Down Expand Up @@ -836,6 +849,7 @@ pub(super) unsafe extern "system" fn set_compute_root_constants(this: *mut c_voi
/// #39 / #41): the 64-bit GPU address
pub(super) unsafe extern "system" fn set_compute_root_descriptor(this: *mut c_void, i: u32, va: u64) {
note_address(va);
let va = readable_root(va);
put(this, i, 0, &va.to_le_bytes());
}

Expand Down
25 changes: 19 additions & 6 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1928,10 +1928,15 @@ unsafe extern "system" fn dev_create_cbv_x(this: *mut c_void, desc: *const D3D12
};
let (va, size) = desc.as_ref().map_or((0, 0), |d| (d.BufferLocation, d.SizeInBytes));
let in_uma = crate::gpu::uma::get().is_some_and(|u| u.contains(va as usize));
if !in_uma {
let mirrored = if in_uma { None } else { super::sparse::gpu_view::base_for(va as usize, size as usize) };
if !in_uma && mirrored.is_none() {
note_buffer_view();
}
let (va, size) = if in_uma { (va, size) } else { (0, 0) };
let (va, size) = match mirrored {
Some(b) => (b as u64, size),
None if in_uma => (va, size),
None => (0, 0),
};
super::console_heap::write(&slot, &crate::gpu::desc::cbv_words(va, size));
}

Expand Down Expand Up @@ -2380,7 +2385,7 @@ unsafe extern "system" fn dev_create_srv_at_x(this: *mut c_void, rdesc: *const u
let Some(slot) = super::console_heap::slot(cpu) else { return };
let b = &v.Anonymous.Buffer;
let raw = b.Flags.0 & D3D12_BUFFER_SRV_FLAG_RAW.0 != 0;
let words = buffer_words_at(va, v.Format, b.FirstElement, b.NumElements, b.StructureByteStride, raw);
let words = buffer_words_at(va, v.Format, b.FirstElement, b.NumElements, b.StructureByteStride, raw, true);
return super::console_heap::write(&slot, &words);
}
dev_create_srv_x(this, resource_at(this, rdesc, va), &v, cpu);
Expand All @@ -2398,14 +2403,15 @@ unsafe extern "system" fn dev_create_uav_at_x(this: *mut c_void, rdesc: *const u
let Some(slot) = super::console_heap::slot(cpu) else { return };
let b = &v.Anonymous.Buffer;
let raw = b.Flags.0 & D3D12_BUFFER_UAV_FLAG_RAW.0 != 0;
let words = buffer_words_at(va, v.Format, b.FirstElement, b.NumElements, b.StructureByteStride, raw);
let words = buffer_words_at(va, v.Format, b.FirstElement, b.NumElements, b.StructureByteStride, raw, false);
return super::console_heap::write(&slot, &words);
}
dev_create_uav_x(this, resource_at(this, rdesc, va), std::ptr::null_mut(), &v, cpu);
}

/// V# of `n` elements from element `first` of the buffer at `va`.
fn buffer_words_at(va: u64, format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT, first: u64, n: u32, stride: u32, raw: bool) -> [u32; 4] {
/// (`read_only`: a view of a GPU view may get a readable address, `sparse::gpu_view`)
fn buffer_words_at(va: u64, format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT, first: u64, n: u32, stride: u32, raw: bool, read_only: bool) -> [u32; 4] {
let fmt = crate::gpu::desc::gfx10_format(format).unwrap_or(22);
let (stride, elem) = match (raw, stride) {
(true, _) => (0, 4),
Expand All @@ -2416,7 +2422,14 @@ fn buffer_words_at(va: u64, format: windows::Win32::Graphics::Dxgi::Common::DXGI
(false, s) => (s, s),
};
let records = if stride == 0 { n.saturating_mul(4) } else { n };
crate::gpu::desc::buffer_words(va + first * elem as u64, stride, records, fmt)
let mut base = va + first * elem as u64;
if read_only {
let len = (n as u64).saturating_mul(elem as u64);
if let Some(b) = super::sparse::gpu_view::base_for(base as usize, len as usize) {
base = b as u64;
}
}
crate::gpu::desc::buffer_words(base, stride, records, fmt)
}

fn resolve_uav_array_size(array_size: u32, total_slices: u32, first_slice: u32) -> u32 {
Expand Down
19 changes: 18 additions & 1 deletion crates/xcl/src/hle/d3d12x/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use windows_sys::Win32::System::Memory::{
PAGE_NOACCESS, PAGE_READWRITE,
};

pub mod gpu_view;

const TILE: usize = 0x1_0000;
/// tiles of a video-memory pool heap (64 MiB)
const POOL_TILES: u32 = 1024;
Expand Down Expand Up @@ -1122,6 +1124,15 @@ impl State {
/// Asynchronously copy `runs` (address, bytes) from the CPU view into sparse VRAM tiles via
/// the 64 MiB ring staging buffer (zero CPU stall on the common path).
unsafe fn upload_dirty(&mut self, runs: &[(usize, usize)]) {
self.upload_runs(runs, &|at, p, n| {
std::ptr::copy_nonoverlapping(at as *const u8, p, n);
true
});
}

/// Copies `runs` (address in a window, bytes) into video memory; `read(address, to, bytes)`
/// gives their bytes (false: the run's piece is left as it is).
unsafe fn upload_runs(&mut self, runs: &[(usize, usize)], read: &dyn Fn(usize, *mut u8, usize) -> bool) {
if runs.is_empty() {
return;
}
Expand Down Expand Up @@ -1168,7 +1179,10 @@ impl State {
}
};
let dst_off = (aligned as usize) % STAGING;
std::ptr::copy_nonoverlapping(cur as *const u8, p.add(dst_off), chunk);
if !read(cur, p.add(dst_off), chunk) {
done += chunk;
continue;
}
let staging_res = &self.staging.as_ref().unwrap().0;
self.slots[slot_idx].list.CopyBufferRegion(&self.windows[wi].1, (cur - b) as u64, staging_res, dst_off as u64, chunk as u64);
self.ring_write = aligned + chunk as u64;
Expand Down Expand Up @@ -1511,6 +1525,9 @@ pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page:
if !dirty_runs.is_empty() {
s.upload_dirty(&dirty_runs);
}
if gpu_view::on() {
gpu_view::sync(s, dirty);
}
PHASES[2].fetch_add(t_scanned.elapsed().as_micros() as u64, Ordering::Relaxed);
{
static CALLS: AtomicU64 = AtomicU64::new(0);
Expand Down
Loading
Loading