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
21 changes: 16 additions & 5 deletions crates/xcl/src/gpu/recomp/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ pub fn run_with_statics(
tex_back.push((i, rb));
}
list.Close().map_err(e)?;
gpu.queue.ExecuteCommandLists(&[Some(list.cast().map_err(e)?)]);
submit_list(&gpu.queue, &list)?;
gpu.value += 1;
wait_for_submission(dev, &gpu.queue, &gpu.fence, gpu.value, "kernel dispatch")?;
let mut p = std::ptr::null_mut();
Expand Down Expand Up @@ -1222,7 +1222,7 @@ fn draw_indexed_on(
};
list.CopyTextureRegion(&dst, 0, 0, 0, &src, None);
list.Close().map_err(e)?;
queue.ExecuteCommandLists(&[Some(list.cast().map_err(e)?)]);
submit_list(&queue, &list)?;
wait_for_submission(dev, &queue, &fence, 1, "draw")?;
let mut p = std::ptr::null_mut();
readback.Map(0, None, Some(&mut p)).map_err(e)?;
Expand Down Expand Up @@ -1425,7 +1425,7 @@ pub fn mesh_draw_split(
list.CopyTextureRegion(&dst, 0, 0, 0, &src, None);
list.CopyBufferRegion(&readback, total, &res_buf, 0, res_bytes);
list.Close().map_err(e)?;
gpu.queue.ExecuteCommandLists(&[Some(list.cast().map_err(e)?)]);
submit_list(&gpu.queue, &list)?;
gpu.value += 1;
wait_for_submission(dev, &gpu.queue, &gpu.fence, gpu.value, "mesh draw")?;
let mut q = std::ptr::null_mut();
Expand Down Expand Up @@ -1561,7 +1561,7 @@ fn run_in_region(dxil: &[u8], state: &KernelState, arena: &Arena) -> Result<Kern
}
list.CopyBufferRegion(&readback, 0, &res_buf, 0, res_bytes);
list.Close().map_err(e)?;
gpu.queue.ExecuteCommandLists(&[Some(list.cast().map_err(e)?)]);
submit_list(&gpu.queue, &list)?;
gpu.value += 1;
wait_for_submission(dev, &gpu.queue, &gpu.fence, gpu.value, "arena dispatch")?;
let mut q = std::ptr::null_mut();
Expand Down Expand Up @@ -1768,7 +1768,7 @@ fn dispatch_with(
list.ResourceBarrier(&[transition(&mem_buf, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE)]);
list.CopyBufferRegion(&readback, 0, &mem_buf, 0, mem_bytes);
list.Close().map_err(e)?;
queue.ExecuteCommandLists(&[Some(list.cast().map_err(e)?)]);
submit_list(queue, &list)?;
*value += 1;
wait_for_submission(dev, queue, fence, *value, "compute dispatch")?;
let mut q = std::ptr::null_mut();
Expand All @@ -1787,3 +1787,14 @@ fn dispatch_with(
Ok((out, ms))
}
}

/// Submit `list` to `queue`. Under Wine, if another test (e.g. `real::init()`) adopted the
/// process-wide singleton `ID3D12Device` after `queue` was created, `list` carries XCL's
/// extended vtable while `queue` does not go through `real::q_execute_command_lists`, so
/// restore `list`'s native vtable for the call.
unsafe fn submit_list(queue: &ID3D12CommandQueue, list: &ID3D12GraphicsCommandList) -> Result<(), String> {
let cmd: ID3D12CommandList = list.cast().map_err(e)?;
let _guard = crate::hle::d3d12x::real::UnadoptGuard::new(cmd.as_raw());
queue.ExecuteCommandLists(&[Some(cmd)]);
Ok(())
}
64 changes: 56 additions & 8 deletions crates/xcl/src/gpu/uma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,14 @@ impl Uma {
}
let mut win_allocs = Vec::with_capacity(1 + coh_wins);
win_allocs.push(base);
// Under Wine without userfaultfd, MEM_WRITE_WATCH uses mprotect(PROT_READ), which
// triggers amdgpu userptr MMU-notifier evictions and ~100ms GPU stalls on the
// coherent window imported via OpenExistingHeapFromAddress.
let c_flags = if crate::util::under_wine() { MEM_RESERVE | MEM_COMMIT } else { MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH };
let mut ok = true;
for (c, &c_size) in coh_sizes.iter().enumerate() {
let c_base = base + (sparse_wins + c) * WINDOW;
let cp = unsafe { VirtualAlloc(c_base as *const std::ffi::c_void, c_size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE) };
let cp = unsafe { VirtualAlloc(c_base as *const std::ffi::c_void, c_size, c_flags, PAGE_READWRITE) };
if cp as usize != c_base {
if !cp.is_null() {
unsafe { VirtualFree(cp, 0, MEM_RELEASE) };
Expand Down Expand Up @@ -490,7 +494,8 @@ impl Uma {
for k in 0..windows {
let want = base + k * WINDOW;
// write-watched: textures the title keeps here are uploaded again after CPU writes
let p = unsafe { VirtualAlloc(want as *const std::ffi::c_void, size, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE) };
let c_flags = if crate::util::under_wine() { MEM_RESERVE | MEM_COMMIT } else { MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH };
let p = unsafe { VirtualAlloc(want as *const std::ffi::c_void, size, c_flags, PAGE_READWRITE) };
if p.is_null() {
break;
}
Expand Down Expand Up @@ -561,7 +566,9 @@ impl Uma {
match self.kinds[k] {
WindowKind::None => k += 1,
WindowKind::Committed(s) => {
out.push((self.base + k * WINDOW, s));
if !crate::util::under_wine() {
out.push((self.base + k * WINDOW, s));
}
k += 1;
}
WindowKind::Sparse => {
Expand Down Expand Up @@ -782,12 +789,45 @@ fn coherent_file() -> std::path::PathBuf {
crate::state::data_dir().join("cache").join("uma_coherent.txt")
}

/// Whether this process is running under Wine on an AMD GPU (`amdgpu`, PCI vendor `0x1002`).
/// On `amdgpu`, a multi-GiB `userptr` BO imported via `OpenExistingHeapFromAddress` stalls
/// every Vulkan submission (~150 ms/frame), whereas NVIDIA Linux does not hit that stall and
/// benefits from a larger coherent window.
fn wine_amd_gpu() -> bool {
static AMD: OnceLock<bool> = OnceLock::new();
*AMD.get_or_init(|| {
if !crate::util::under_wine() {
return false;
}
use windows::Win32::Graphics::Dxgi::{CreateDXGIFactory1, IDXGIFactory1, DXGI_ADAPTER_FLAG_SOFTWARE};
let Ok(factory) = (unsafe { CreateDXGIFactory1::<IDXGIFactory1>() }) else {
return false;
};
let mut i = 0u32;
while let Ok(adapter) = unsafe { factory.EnumAdapters1(i) } {
i += 1;
if let Ok(desc) = unsafe { adapter.GetDesc1() } {
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE.0 as u32) == 0 {
return desc.VendorId == 0x1002;
}
}
}
false
})
}

/// Bytes the coherent window(s) are made with for a runtime layout of `windows` windows.
pub fn coherent_hint_for(windows: usize) -> usize {
let cap = max_coherent_bytes(windows);
if let Some(mb) = std::env::var("XCL_COHERENT_MB").ok().and_then(|v| v.parse::<usize>().ok()) {
return (mb << 20).clamp(GRAIN, cap);
}
// Under Wine/vkd3d-proton on Linux (amdgpu), a multi-GiB userptr BO imported via
// OpenExistingHeapFromAddress stalls every GPU submission (~150 ms/frame); keep the
// coherent window at 512 MiB and place overflow title GPU heaps in the sparse VRAM windows.
if wine_amd_gpu() {
return RUNTIME_COMMITTED_CAP;
}
let title = std::fs::read_to_string(coherent_file()).ok().and_then(|s| s.trim().parse::<usize>().ok()).unwrap_or(0);
// only with the commit for all of it: the title's ~13 GB, video memory for its render targets
// and textures (WDDM charges commit for each video-memory allocation too) and the window
Expand Down Expand Up @@ -1101,8 +1141,14 @@ mod tests {
assert_eq!(u.sparse_release(y), Some(GRAIN));
assert_eq!(u.sparse_release(desc), Some(GRAIN));
assert_eq!(u.sparse_used(), 0);
// Contiguous sparse windows 0..2 are reported as one OS watch region; window 3 as another.
assert_eq!(u.watch_regions(), vec![(u.base(), 3 * WINDOW), (u.base() + 3 * WINDOW, 16 << 20)]);
// Contiguous sparse windows 0..2 are reported as one OS watch region; window 3 as another
// on native Windows (excluded under Wine to avoid amdgpu userptr MMU-notifier stalls).
let expected = if crate::util::under_wine() {
vec![(u.base(), 3 * WINDOW)]
} else {
vec![(u.base(), 3 * WINDOW), (u.base() + 3 * WINDOW, 16 << 20)]
};
assert_eq!(u.watch_regions(), expected);
}

#[test]
Expand Down Expand Up @@ -1135,13 +1181,15 @@ mod tests {
assert_eq!(u.sparse_release(d), Some(ld));
assert_eq!(u.sparse_release(r), Some(big));
assert_eq!(u.sparse_used(), 0);
assert_eq!(
u.watch_regions(),
let expected = if crate::util::under_wine() {
vec![(u.base(), MAX_RUNTIME_SPARSE_WINDOWS * WINDOW)]
} else {
vec![
(u.base(), MAX_RUNTIME_SPARSE_WINDOWS * WINDOW),
(u.base() + MAX_RUNTIME_SPARSE_WINDOWS * WINDOW, 16 << 20),
]
);
};
assert_eq!(u.watch_regions(), expected);
}
}

Expand Down
26 changes: 20 additions & 6 deletions crates/xcl/src/hle/d3d12x/alloc_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub unsafe fn executed(queue: *mut c_void, lists: &[*mut c_void]) {
let key = queue as usize;
if !s.fences.contains_key(&key) {
let mut dev: Option<ID3D12Device> = None;
let Some(f) = q.GetDevice(&mut dev).ok().and(dev).and_then(|d| d.CreateFence::<ID3D12Fence>(0, D3D12_FENCE_FLAG_NONE).ok()) else { return };
let Some(f) = q.GetDevice(&mut dev).ok().and(dev).and_then(|d| super::real::create_raw_fence(&d).ok()) else { return };
s.fences.insert(key, (f, 0));
}
let (fence, value) = {
Expand Down Expand Up @@ -443,12 +443,15 @@ mod tests {
if !super::super::real::is_adopted(a) {
super::super::real::adopt(a, super::super::real::Kind::CommandAllocator);
}
let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap();
let list: ID3D12GraphicsCommandList = {
let _alloc_guard = super::super::real::UnadoptGuard::new(a);
dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap()
};
let l = list.as_raw();
created_on(l as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0);
list.Close().unwrap();
let lists = [Some(list.cast::<ID3D12CommandList>().unwrap())];
let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
let fence: ID3D12Fence = super::super::real::create_raw_fence(&dev).unwrap();
let done = |v: u64| {
queue.Signal(&fence, v).unwrap();
let start = std::time::Instant::now();
Expand All @@ -458,7 +461,10 @@ mod tests {
assert!(fence.GetCompletedValue() >= v, "the queue finishes");
};

queue.ExecuteCommandLists(&lists);
{
let _list_guard = super::super::real::UnadoptGuard::new(l);
queue.ExecuteCommandLists(&lists);
}
executed(queue.as_raw(), &[l]);
done(1);

Expand All @@ -467,7 +473,10 @@ mod tests {
let second = current(a as usize);
assert_ne!(second, a as usize, "the list still holds its recording: another PC allocator takes the place");
// … and submits the recording again
queue.ExecuteCommandLists(&lists);
{
let _list_guard = super::super::real::UnadoptGuard::new(l);
queue.ExecuteCommandLists(&lists);
}
executed(queue.as_raw(), &[l]);
done(2);
assert!(dev.GetDeviceRemovedReason().is_ok(), "the recording was still there");
Expand All @@ -476,10 +485,14 @@ mod tests {
let second_alloc = ID3D12CommandAllocator::from_raw_borrowed(&(second as *mut c_void)).unwrap().clone();
assert_eq!(reset_list(l as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0, |pc| {
assert_eq!(pc, second);
let _alloc_guard = super::super::real::UnadoptGuard::new(pc as *mut c_void);
list.Reset(&second_alloc, None).map_or_else(|e| e.code().0, |_| 0)
}), 0);
list.Close().unwrap();
queue.ExecuteCommandLists(&lists);
{
let _list_guard = super::super::real::UnadoptGuard::new(l);
queue.ExecuteCommandLists(&lists);
}
executed(queue.as_raw(), &[l]);
done(3);

Expand All @@ -492,6 +505,7 @@ mod tests {
// it stays (the next allocator at its address is its own)
assert_eq!(reset_list(l as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0, |pc| {
assert_eq!(pc, a as usize);
let _alloc_guard = super::super::real::UnadoptGuard::new(pc as *mut c_void);
list.Reset(&alloc, None).map_or_else(|e| e.code().0, |_| 0)
}), 0);
list.Close().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/hle/d3d12x/cpu_writes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub(super) unsafe fn after(queue: *mut c_void, writes: Vec<Write>) {
return;
}
let Some(dev) = dev else { return };
let Ok(fence) = dev.CreateFence::<ID3D12Fence>(0, D3D12_FENCE_FLAG_NONE) else { return };
let Ok(fence) = super::real::create_raw_fence(&dev) else { return };
v.insert(QueueFence { fence, value: 0 })
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/hle/d3d12x/late_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Held {
let key = q.as_raw() as usize;
if !self.fences.contains_key(&key) {
let mut dev: Option<ID3D12Device> = None;
let made = q.GetDevice(&mut dev).ok().and(dev).and_then(|d| d.CreateFence::<ID3D12Fence>(0, D3D12_FENCE_FLAG_NONE).ok());
let made = q.GetDevice(&mut dev).ok().and(dev).and_then(|d| super::real::create_raw_fence(&d).ok());
let Some(f) = made else {
ok = false;
break;
Expand Down
Loading
Loading