Skip to content
Merged
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/gpu/uma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ pub struct Uma {
/// cannot take it – a title that commits all of unified memory at start (Unicorn
/// Overlord: 2046 of 2048 MB) left no room for draw arguments, and draws were skipped.
pool: Mutex<(usize, usize, Vec<usize>)>,
/// windows remade as driver heaps (`hle::d3d12x::uma_driver`), one bit each
driver: std::sync::atomic::AtomicU32,
}

/// bytes of XCL's arena pool (`Uma::new`)
Expand Down Expand Up @@ -301,6 +303,7 @@ impl Uma {
inner: Mutex::new(inner),
sparse_free: Mutex::new(sparse_free),
pool: Mutex::new((0, 0, Vec::new())),
driver: Default::default(),
};
u.reserve_pool(POOL);
return Some(u);
Expand Down Expand Up @@ -377,6 +380,16 @@ impl Uma {
self.window_of(p).is_some_and(|k| self.is_committed_window(k))
}

/// Window `k` is a driver heap now (`hle::d3d12x::uma_driver`).
pub fn set_driver_window(&self, k: usize) {
self.driver.fetch_or(1 << k, std::sync::atomic::Ordering::Relaxed);
}

/// Whether window `k` is a driver heap (`set_driver_window`).
pub fn is_driver_window(&self, k: usize) -> bool {
k < 32 && self.driver.load(std::sync::atomic::Ordering::Relaxed) & (1 << k) != 0
}

/// Whether `[p, p + len)` lies in a committed coherent window – memory that is committed and
/// read-write as a whole, so it needs no `VirtualQuery` (which walks a region to its end to
/// size it: on a 4 GiB window ~40 ms a call, a retail game's DirectStorage requests crawled).
Expand Down Expand Up @@ -512,6 +525,7 @@ impl Uma {
inner: Mutex::new(inner),
sparse_free: Mutex::new(Default::default()),
pool: Mutex::new((0, 0, Vec::new())),
driver: Default::default(),
});
}
for p in got {
Expand Down
18 changes: 14 additions & 4 deletions crates/xcl/src/hle/d3d12x/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod sparse;
mod standin;
pub mod title_list;
pub mod title_textures;
mod uma_driver;
pub mod vram;

use std::ffi::c_void;
Expand Down Expand Up @@ -72,15 +73,16 @@ pub enum Backend {
}

/// Decided once: the real backend unless disabled or the driver refuses.
pub fn backend() -> Backend {
static B: OnceLock<Backend> = OnceLock::new();
*B.get_or_init(|| {
static BACKEND: OnceLock<Backend> = OnceLock::new();

fn backend_with_remap(allow_driver_remap: bool) -> Backend {
*BACKEND.get_or_init(|| {
let cfg = crate::state::config();
if cfg.graphics.eq_ignore_ascii_case("null") {
crate::xlog_info!("D3D12.X: backend 'null' (from the config)");
return Backend::Null;
}
match real::init() {
match real::init_with_remap(allow_driver_remap) {
Ok(desc) => {
crate::xlog_info!("D3D12.X: backend 'real' – {desc}");
Backend::Real
Expand All @@ -93,6 +95,14 @@ pub fn backend() -> Backend {
})
}

pub fn backend() -> Backend {
backend_with_remap(false)
}

pub(crate) fn backend_before_guest() -> Backend {
backend_with_remap(true)
}

/// Call a function of the system d3d12.dll by name.
unsafe fn sys(name: &str) -> Option<usize> {
crate::resolver::system_proc("d3d12.dll", &crate::pe::ImportName::Name { hint: 0, name: name.into() })
Expand Down
32 changes: 25 additions & 7 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,22 @@ unsafe fn check_uma_import(device: &ID3D12Device, u: &crate::gpu::uma::Uma, bufs
Ok(format!("ok, {} window(s), {} µs", probes.len(), t0.elapsed().as_micros()))
}

unsafe fn open_uma(device: &ID3D12Device) {
unsafe fn open_uma(device: &ID3D12Device, allow_driver_remap: bool) {
let Some(u) = crate::gpu::uma::get() else {
*UMA_ERROR.lock().unwrap() = "no UMA region".into();
return;
};
let open = |k: usize| -> Result<(usize, u64, usize), String> {
let (base, size) = u.window(k);
if allow_driver_remap && super::uma_driver::wanted(device) {
match super::uma_driver::remake(device, base, size) {
Ok((heap, buf)) => {
u.set_driver_window(k);
return Ok(super::uma_driver::raw(heap, buf));
}
Err(e) => crate::xlog_warn!("D3D12.X: coherent window {k} stays imported: {e}"),
}
}
let dev3: ID3D12Device3 = device.cast().map_err(|e| format!("ID3D12Device3: {e}"))?;
let heap: ID3D12Heap = dev3.OpenExistingHeapFromAddress(base as *const c_void).map_err(|e| format!("OpenExistingHeapFromAddress (window {k}): {e}"))?;
let desc = D3D12_RESOURCE_DESC {
Expand Down Expand Up @@ -469,6 +478,10 @@ fn graphics_wave32(device: *mut c_void) -> bool {
/// by default the high-performance GPU → WARP); once – later calls
/// return the existing device's adapter.
pub fn init() -> Result<String, String> {
init_with_remap(false)
}

pub(super) fn init_with_remap(allow_driver_remap: bool) -> Result<String, String> {
if !device_raw().is_null() {
return Ok(ADAPTER.lock().map(|a| a.clone()).unwrap_or_default());
}
Expand Down Expand Up @@ -545,7 +558,7 @@ pub fn init() -> Result<String, String> {
let _ = q.RegisterMessageCallback(Some(debug_message), D3D12_MESSAGE_CALLBACK_FLAG_NONE, std::ptr::null_mut(), &mut cookie);
}
}
open_uma(&device);
open_uma(&device, allow_driver_remap);
super::device_x::probe_dynamic_depth_bias(&device);
let raw = device.into_raw(); // one reference kept for the process lifetime
adopt(raw, Kind::Device);
Expand Down Expand Up @@ -1393,9 +1406,12 @@ unsafe fn place_console_buffer_sparse(this: *mut c_void, d: &D3D12_RESOURCE_DESC

/// Sanitize a console buffer description and initial state for placement in a coherent
/// cross-adapter UMA heap (`OpenExistingHeapFromAddress`).
fn sanitize_coherent_buffer(desc: &D3D12_RESOURCE_DESC, state: usize) -> (D3D12_RESOURCE_DESC, u32) {
fn sanitize_coherent_buffer(desc: &D3D12_RESOURCE_DESC, state: usize, cross_adapter: bool) -> (D3D12_RESOURCE_DESC, u32) {
let mut d = *desc;
d.Flags = D3D12_RESOURCE_FLAGS(d.Flags.0 & 0xFF) | D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER;
d.Flags = D3D12_RESOURCE_FLAGS(d.Flags.0 & 0xFF);
if cross_adapter {
d.Flags |= D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER;
}
d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
d.Alignment = 0;
(d, state as u32 & PC_RESOURCE_STATES)
Expand Down Expand Up @@ -1432,8 +1448,8 @@ unsafe fn place_console_buffer(this: *mut c_void, props: usize, desc: usize, sta
return None;
};
// the heap from OpenExistingHeapFromAddress is cross-adapter: row-major + sanitized flags & state
let (d, st) = sanitize_coherent_buffer(&d, state);
let k = u.window_of(p)?;
let (d, st) = sanitize_coherent_buffer(&d, state, !u.is_driver_window(k));
let heap = (*UMA_HEAPS.lock().unwrap().get(k)?)?;
let hr = dev_create_placed_resource(this, heap, p - u.window(k).0, &d as *const _ as usize, st as usize, clear, riid, ppv);
if hr < 0 || (*ppv).is_null() {
Expand Down Expand Up @@ -1490,7 +1506,9 @@ pub(super) unsafe fn place_at_title_address(this: *mut c_void, va: usize, desc:
return Some(hr);
}
let heap = (*UMA_HEAPS.lock().unwrap().get(k)?)?;
d.Flags |= D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER;
if !u.is_driver_window(k) {
d.Flags |= D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER;
}
d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
d.Alignment = 0;
d.Width = (delta + bytes) as u64;
Expand Down Expand Up @@ -7810,7 +7828,7 @@ mod readback_routing_tests {
};
let console_state = 0x0010_0000usize | (D3D12_RESOURCE_STATE_COPY_DEST.0 as usize);

let (clean, st) = sanitize_coherent_buffer(&raw_desc, console_state);
let (clean, st) = sanitize_coherent_buffer(&raw_desc, console_state, true);
assert_eq!(clean.Alignment, 0);
assert_eq!(clean.Layout, D3D12_TEXTURE_LAYOUT_ROW_MAJOR);
assert_eq!(
Expand Down
Loading
Loading