From 0a755f0ccbad1c7090a500715505571dfbb95927 Mon Sep 17 00:00:00 2001 From: Dmitry Omelchenko <35579597+Ariorh1337@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:18:20 +0100 Subject: [PATCH 1/2] D3D12.X: a copy back after a submission does not wait for the title `readback_after_execute` made the sparse queue GPU-wait for the title's queue and then waited on the CPU for the copy, on the thread that was submitting. The title's queues can wait for work that same thread submits next (four compute queues waiting for a Write64 on q1 whose list is recorded but not submitted yet). So the copy waited its 5 s on every such submission, and the uploads queued behind it on the sparse queue waited too (`before_execute` waiting for ring room). In thread dumps of a retail title on an RTX 5090, the submitting thread is in `readback_ranges` -> `wait_for` while another title thread waits for the sparse state's lock in `commit_range`. lucsoft found the same cycle on RDNA3 / Linux (#505). The copies now run on a lane per title queue: a fence the title's queue signals after its lists, and a queue of its own that GPU-waits for that fence. The submitting thread returns. With one fence for all title queues, a later signal on another queue let a copy start before its own queue's lists ran (CharlyRien); with one queue for all, a title queue waiting for work still to come held back the copies of the others. The lanes (8) are made in `init`, before the device's methods are hooked: made later, a lane's queue was adopted as one of the title's, and the e2e title under the debug layer read 0 for what its lists wrote. A copy back's allocator and list are made with the device's own methods, past its hooks (`real::create_own_list`), as `init`'s objects are: made through the adopted device, the list took XCL's list hooks on the lane's queue, and vkd3d-proton crashed at its Close (St5sz2Jk, RDNA4 / Wine). Tiles quarantined behind the title queues' marks (#2345) also wait for the copies back still to run on a lane: they read the windows after the title's work. A lane serves a title queue while that queue has copies back in flight; with all lanes busy (or none made), the old path runs. Readback buffers are sized to the copy (64 KiB and up) and reused; idle ones above 256 MB are given back (each holds commit, and with the title's queues stalled every submit's copies wait, one buffer each; found by Chrysana's RDNA4 run). Each time the buffers pass another doubling from 64 MB, their size is logged. The data lands in the title's memory once the copy is done, before the title can see the work it follows is done: - on the next submit, and from a thread that looks every 0.5 ms; - before Map's readback (waiting for the copy once the title's lists it follows are done), and in the CPU memory waits of Wait64BitValueX, which only try the sparse state's lock: its holder can be waiting for work the waiting thread is to submit; - in the title's fence observations: `GetCompletedValue` settles the copies first. `SetEventOnCompletion` hands the fence an event of XCL's own, and a thread-pool wait on it settles them, then sets the title's event. These only try the lock; a copy whose title work is done but which still runs is waited for with the lock let go, then landed. Waiting for it with the lock held stood every submit behind a copy that stood behind a stalled GPU (lucsoft, RDNA3). Until they landed, the title is not told its work is done: the thread-pool wait tries again every millisecond before it sets the title's event (10 s at most), and `GetCompletedValue` gives no more than the value the fence was last seen at with its copies landed (its initial value at first). Before, a lock that could not be had let the title read stale memory (bmjask). Lanes land in their own order, so an older copy back of one lane can land after a newer one of another: it leaves the bytes the newer one landed. A CPU wait for a copy uses its lane's event, not the sparse state's: a wait that gave up leaves its registration behind, which set the shared event later and ended a tile-queue wait early (theaustinayers, in #2057). Tests: - `a_copy_back_does_not_wait_for_the_title_queue` waits 5.0 s on the old path and under 1 s with this change; - `a_copy_back_waits_for_its_own_title_queue`: two title queues, the first blocked; with one lane for both, the first one's copy lands before its list ran; - `an_older_copy_back_does_not_overwrite_a_newer_one`. - `a_copy_still_running_after_its_title_work_is_waited_for_outside_the_lock`. - `idle_copy_back_buffers_above_the_cap_are_given_back`. - `a_copy_back_list_of_an_adopted_device_is_not_adopted`. - `quarantined_tiles_wait_for_copies_back_still_to_run`. - `a_title_fence_shows_its_work_done_only_once_copies_back_can_land`: an adopted fence, the sparse state held by another thread. - `a_fence_value_goes_past_its_landed_copies_only_once_they_land`. They run on a WARP device of their own: they block a queue on purpose, and on the GPU's device, which the tests in the process share, other queue tests then hung in the driver releasing resources (upipibi; reproduced in 3 of 3 runs of the d3d12x queue tests). Without the fence hooks, the e2e test title read 0 for its Write32BitValueBottomOfPipeX after its fence wait. With them, both e2e tests pass. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/tests.rs | 4 + crates/xcl/src/hle/d3d12x/list_x.rs | 2 + crates/xcl/src/hle/d3d12x/real.rs | 162 ++++- crates/xcl/src/hle/d3d12x/sparse.rs | 925 +++++++++++++++++++++++++++- 4 files changed, 1058 insertions(+), 35 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs index 0b018edc3..ff00c5899 100644 --- a/crates/xcl/src/gpu/recomp/tests.rs +++ b/crates/xcl/src/gpu/recomp/tests.rs @@ -4486,6 +4486,9 @@ fn gpu_sparse_vram_descriptors_buffers_and_xmem() { // Submit 1: reads slot 0 -> in_a (sparse VRAM) + arg_table constant 77 (Window 3) record_and_run(1, true); + // the copy back of what the shader stored lands asynchronously: before a CPU read the + // title's lists are done for, XCL settles it (as in Map and the CPU memory waits) + crate::hle::d3d12x::sparse::settle_readbacks(); for l in 0..64usize { let got = *((out_buf + 4 * l) as *const u32); assert_eq!(got, 1000 + l as u32 + 77, "submit 1 lane {l} mismatch"); @@ -4496,6 +4499,7 @@ fn gpu_sparse_vram_descriptors_buffers_and_xmem() { *((in_b + 4 * 3) as *mut u32) = 9999; crate::hle::d3d12x::console_heap::write(&slot0, &vsharp(in_b)); record_and_run(2, false); + crate::hle::d3d12x::sparse::settle_readbacks(); for l in 0..64usize { let expected = if l == 3 { 9999 + 13 } else { 5000 + 2 * l as u32 + 13 }; let got = *((out_buf + 4 * l) as *const u32); diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 096620f6f..7507b1489 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -301,6 +301,8 @@ impl PendingWait { Duration::from_millis(1) }; std::thread::sleep(step); + // a value a list wrote into a sparse window reaches this memory with its copy back + super::sparse::settle_readbacks_try(); if self.is_satisfied() { return true; } diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 7456239e1..51c062988 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -2913,7 +2913,11 @@ fn overrides(kind: Kind) -> Vec<(&'static str, usize)> { Kind::PipelineState => v.push(("Release", pso_release as usize)), Kind::DescriptorHeap => v.push(("Release", heap_release as usize)), Kind::Heap => v.push(("Release", mem_heap_release as usize)), - Kind::Fence => v.push(("Signal", fence_signal as usize)), + Kind::Fence => v.extend_from_slice(&[ + ("Signal", fence_signal as usize), + ("GetCompletedValue", fence_get_completed_value as usize), + ("SetEventOnCompletion", fence_set_event_on_completion as usize), + ]), _ => {} } v @@ -3190,6 +3194,39 @@ pub(super) unsafe fn create_raw_pc_command_list( }; (hr >= 0 && !out.is_null()).then_some(out) } +/// A command allocator and a closed list of XCL's own, made with the device's own methods past +/// XCL's hooks, as the objects made before the device is adopted are: neither is adopted, so +/// none of the list hooks (`cl_close`, `cl_reset`, the copy hooks) run for work XCL submits on +/// a queue of its own. +pub(super) unsafe fn create_own_list(dev: &ID3D12Device, ty: D3D12_COMMAND_LIST_TYPE) -> Option<(ID3D12CommandAllocator, ID3D12GraphicsCommandList)> { + let d = dev.as_raw(); + if !is_adopted(d) { + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(ty).ok()?; + let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, ty, &alloc, None).ok()?; + list.Close().ok()?; + return Some((alloc, list)); + } + let iface = layout::find("ID3D12Device")?; + type Alloc = unsafe extern "system" fn(*mut c_void, D3D12_COMMAND_LIST_TYPE, *const GUID, *mut *mut c_void) -> HRESULT; + type List = unsafe extern "system" fn(*mut c_void, u32, D3D12_COMMAND_LIST_TYPE, *mut c_void, *mut c_void, *const GUID, *mut *mut c_void) -> HRESULT; + let create_alloc: Alloc = std::mem::transmute(orig_fn(d, layout::slot(iface, "CreateCommandAllocator")?)); + let create_list: List = std::mem::transmute(orig_fn(d, layout::slot(iface, "CreateCommandList")?)); + let mut a = std::ptr::null_mut(); + let iid = &ID3D12CommandAllocator::IID as *const windows::core::GUID as *const GUID; + if create_alloc(d, ty, iid, &mut a) < 0 || a.is_null() { + return None; + } + let alloc = ID3D12CommandAllocator::from_raw(a); + let mut l = std::ptr::null_mut(); + let iid = &ID3D12GraphicsCommandList::IID as *const windows::core::GUID as *const GUID; + if create_list(d, 0, ty, a, std::ptr::null_mut(), iid, &mut l) < 0 || l.is_null() { + return None; + } + let list = ID3D12GraphicsCommandList::from_raw(l); + list.Close().ok()?; + Some((alloc, list)) +} + creator!(dev_create_descriptor_heap, "CreateDescriptorHeap", Kind::DescriptorHeap, [a1]); creator!(dev_create_committed_resource, "CreateCommittedResource", Kind::Resource, [a1, a2, a3, a4, a5]); creator!(dev_create_committed_resource1, "CreateCommittedResource1", Kind::Resource, [a1, a2, a3, a4, a5, a6]); @@ -3565,6 +3602,7 @@ static FENCES: std::sync::RwLock> = std::sync::RwLock::new(Vec::new() unsafe extern "system" fn dev_create_fence(this: *mut c_void, value: usize, flags: usize, riid: *const GUID, ppv: *mut *mut c_void) -> HRESULT { let hr = dev_create_fence_pc(this, value, flags, riid, ppv); if hr >= 0 && !ppv.is_null() && !(*ppv).is_null() { + set_fence_landed(*ppv as usize, value as u64); let add: RefFn = std::mem::transmute(*vt(*ppv).add(1)); add(*ppv); let n = { @@ -7170,6 +7208,118 @@ unsafe extern "system" fn q_signal_x(this: *mut c_void, fence: *mut c_void, valu hr } +/// Per title fence, the highest value it was seen at with the copies back that value covers +/// landed (seeded with its initial value at CreateFence) +static FENCE_LANDED: Mutex>> = Mutex::new(None); + +/// A fence made (again at this address): its initial value +fn set_fence_landed(fence: usize, v: u64) { + FENCE_LANDED.lock().unwrap().get_or_insert_with(HashMap::new).insert(fence, v); +} + +/// `fence` was seen at `v` with its copies back landed +fn note_fence_landed(fence: usize, v: u64) { + let mut g = FENCE_LANDED.lock().unwrap(); + let e = g.get_or_insert_with(HashMap::new).entry(fence).or_insert(v); + *e = (*e).max(v); +} + +/// What `GetCompletedValue` tells the title: `v` once the copies back it covers landed; +/// otherwise the fence where it was last seen with them landed (`landed`), and the title looks +/// again – it is not blocked, so a holder of the sparse state waiting for this thread can go on +fn completed_for_title(v: u64, settled: bool, landed: Option) -> u64 { + if settled { + v + } else { + landed.map_or(v, |l| l.min(v)) + } +} + +/// Fence::GetCompletedValue. What the title's lists wrote into its sparse memory comes back +/// asynchronously (`sparse::readback_after_execute`): the copies back the value covers land +/// first, and while they cannot (the sparse state is busy) the value does not go past them. +unsafe extern "system" fn fence_get_completed_value(this: *mut c_void) -> u64 { + let f: unsafe extern "system" fn(*mut c_void) -> u64 = std::mem::transmute(orig_fn(this, slot_of(this, "GetCompletedValue"))); + let v = f(this); + if !super::sparse::readbacks_async() { + return v; + } + let settled = super::sparse::settle_readbacks_try(); + if settled { + note_fence_landed(this as usize, v); + } + let landed = FENCE_LANDED.lock().unwrap().as_ref().and_then(|m| m.get(&(this as usize)).copied()); + completed_for_title(v, settled, landed) +} + +/// A `fence_set_event_on_completion` wait: the title's event, XCL's own event the fence sets, and +/// the thread-pool wait on it (0 until registered) +struct FenceWait { + own: *mut c_void, + title: *mut c_void, + wait: AtomicUsize, + /// the fence and the value waited for + fence: usize, + value: u64, +} + +unsafe extern "system" fn fence_wait_fired(ctx: *mut c_void, _timed_out: bool) { + use windows_sys::Win32::System::Threading::{SetEvent, UnregisterWaitEx}; + let w = ctx as *mut FenceWait; + // the title's event only once the copies back the value covers landed (on this pool thread, + // which may wait) + if super::sparse::settle_readbacks_until_landed(std::time::Duration::from_secs(10)) { + note_fence_landed((*w).fence, (*w).value); + } + SetEvent((*w).title); + let mut wh = (*w).wait.load(Ordering::Acquire); + while wh == 0 { + std::hint::spin_loop(); + wh = (*w).wait.load(Ordering::Acquire); + } + // no completion event: the registration goes once this callback returns + UnregisterWaitEx(wh as *mut c_void, std::ptr::null_mut()); + windows_sys::Win32::Foundation::CloseHandle((*w).own); + drop(Box::from_raw(w)); +} + +/// Fence::SetEventOnCompletion. Once copies back land asynchronously, the title's event is set +/// after the fence is reached and the copies back it covers landed: the fence sets an event of +/// XCL's own, and a thread-pool wait on that settles them and sets the title's. A null event +/// (a blocking wait) settles them after it. +unsafe extern "system" fn fence_set_event_on_completion(this: *mut c_void, value: u64, event: *mut c_void) -> HRESULT { + use windows_sys::Win32::System::Threading::{CreateEventW, RegisterWaitForSingleObject, UnregisterWaitEx, INFINITE, WT_EXECUTELONGFUNCTION, WT_EXECUTEONLYONCE}; + let f: unsafe extern "system" fn(*mut c_void, u64, *mut c_void) -> HRESULT = std::mem::transmute(orig_fn(this, slot_of(this, "SetEventOnCompletion"))); + if event.is_null() || !super::sparse::readbacks_async() { + let hr = f(this, value, event); + if event.is_null() && hr >= 0 && super::sparse::settle_readbacks_until_landed(std::time::Duration::from_secs(10)) { + note_fence_landed(this as usize, value); + } + return hr; + } + let own = CreateEventW(std::ptr::null(), 0, 0, std::ptr::null()); + if own.is_null() { + return f(this, value, event); + } + let w = Box::into_raw(Box::new(FenceWait { own, title: event, wait: AtomicUsize::new(0), fence: this as usize, value })); + let mut wh = std::ptr::null_mut(); + if RegisterWaitForSingleObject(&mut wh, own, Some(fence_wait_fired), w as *const c_void, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION) == 0 { + windows_sys::Win32::Foundation::CloseHandle(own); + drop(Box::from_raw(w)); + return f(this, value, event); + } + (*w).wait.store(wh as usize, Ordering::Release); + let hr = f(this, value, own); + if hr < 0 { + // the event will not be set: take the wait back (waiting for a running callback) + if UnregisterWaitEx(wh, windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE) != 0 { + windows_sys::Win32::Foundation::CloseHandle(own); + drop(Box::from_raw(w)); + } + } + hr +} + /// Fence::Signal (CPU) unsafe extern "system" fn fence_signal(this: *mut c_void, value: u64) -> HRESULT { let f: unsafe extern "system" fn(*mut c_void, u64) -> HRESULT = std::mem::transmute(orig_fn(this, slot_of(this, "Signal"))); @@ -8090,6 +8240,16 @@ mod console_format_query_tests { mod map_tests { use super::*; + /// GetCompletedValue gives the title the fence's value once its copies back landed, and + /// otherwise no more than where it was last seen with them landed. + #[test] + fn a_fence_value_goes_past_its_landed_copies_only_once_they_land() { + assert_eq!(completed_for_title(9, true, Some(4)), 9); + assert_eq!(completed_for_title(9, false, Some(4)), 4); + assert_eq!(completed_for_title(3, false, Some(4)), 3, "never above the fence itself"); + assert_eq!(completed_for_title(9, false, None), 9, "a fence XCL did not see made"); + } + /// A title's Map reaches the PC's only for a resource the PC can map: a buffer in an upload, /// readback or CPU-visible custom heap. One in a DEFAULT heap, a custom heap without CPU pages /// or a reserved one gets its title address instead (the PC's Map on it is an invalid call). diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index b7560f861..f0dcf4b46 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -47,6 +47,10 @@ const UPLOAD_SLOTS: usize = 16; const ZERO_BUF: usize = 1 << 20; /// bytes of the GPU→CPU readback staging buffer const READBACK_STAGING: usize = 16 << 20; +/// copy-back lanes (title queues with copies back in flight at once; more take the CPU-waited path) +const RB_LANES: usize = 8; +/// bytes of idle copy-back buffers kept for reuse (system memory: each counts toward commit) +const RB_POOL_MAX: usize = 256 << 20; const PAGE: usize = 0x1000; const PAGES_PER_TILE: usize = TILE / PAGE; @@ -203,6 +207,53 @@ struct RetiredTiles { marks: Option>, } +/// A copy back on a lane's queue: its list, its readback buffer, the lane's `src` value it waits +/// for, the lane's `done` value that marks it done, its place among all copies back (`seq`), and +/// the copies into the title's memory (address, offset in `buf`, bytes). +struct RbJob { + alloc: ID3D12CommandAllocator, + list: ID3D12GraphicsCommandList, + buf: ID3D12Resource, + ptr: *mut u8, + cap: usize, + rb_v: u64, + done_v: u64, + seq: u64, + copies: Vec<(usize, usize, usize)>, +} + +/// The copies back after one title queue's submissions. Each title queue has its own fence, so +/// a copy waits for its own queue's lists only (with one fence for all, a later signal on +/// another queue let it start early), and its own queue, so a title queue that waits for work +/// still to come does not hold back the copies of the others. Made in `init`, before the +/// device's methods are hooked: made later, the queue would be adopted as one of the title's. +struct RbLane { + /// the title's queue it serves while it has copies back in flight (0: none yet) + title: usize, + /// signalled on the title's queue after its lists, waited for on `q` + src: ID3D12Fence, + src_value: u64, + q: ID3D12CommandQueue, + /// signalled on `q` after each copy back + done: ID3D12Fence, + done_value: u64, + /// for CPU waits on `done` (not `State::event`: a wait that gave up leaves its registration + /// behind, which sets the event later and would end another wait early) + event: HANDLE, + /// submitted copies back, oldest first + jobs: VecDeque, +} + +/// How far `drain_readbacks` goes +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Drain { + /// only copies the GPU has finished + Finished, + /// also those whose title work is done (waiting for the copy itself): before a CPU read + /// the title made sure of, e.g. after its wait for the value a list wrote + TitleDone, +} + struct State { dev: ID3D12Device, queue: ID3D12CommandQueue, @@ -210,9 +261,22 @@ struct State { value: u64, /// reusable auto-reset Win32 event for fence waits (no per-wait CreateEventW/CloseHandle) event: HANDLE, - /// fence signalled on title queues so `queue` waits before copying GPU writes back to CPU + /// fence signalled on title queues after their lists, so a copy back waits for them rb_fence: ID3D12Fence, rb_value: u64, + /// copies back after the title's submissions (`readback_after_execute`): on a lane per title + /// queue (`rb_lanes`; with none free, on `queue`, waited for on the CPU), landing in the + /// title's memory once done (`drain_readbacks`). The title's queues can wait for work the + /// title submits after this one, from the same thread: waiting here for its lists stopped + /// that submission – and, on `queue`, every upload behind it (`before_execute` waiting for + /// ring room). + rb_lanes: Vec, + /// the next copy back's `seq`, and the ranges landed (start, end, seq) that an older copy + /// back still in flight on another lane must not overwrite + rb_seq: u64, + rb_landed: Vec<(usize, usize, u64)>, + /// finished copies back to reuse + rb_pool: Vec, /// (window start, reserved buffer) of each sparse window windows: Vec<(usize, ID3D12Resource)>, /// video-memory pool heaps (None: given back, its index free for the next one) @@ -526,6 +590,7 @@ pub unsafe fn init(dev: &ID3D12Device) -> Vec<(usize, usize, u64)> { let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE)?; let rb_fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE)?; let event = windows::Win32::System::Threading::CreateEventW(None, false, false, None)?; + let rb_lanes: Vec = (0..RB_LANES).map_while(|_| rb_lane_new(dev).ok()).collect(); let mut slots = Vec::with_capacity(UPLOAD_SLOTS); for _ in 0..UPLOAD_SLOTS { let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT)?; @@ -549,6 +614,10 @@ pub unsafe fn init(dev: &ID3D12Device) -> Vec<(usize, usize, u64)> { event, rb_fence, rb_value: 0, + rb_lanes, + rb_seq: 0, + rb_landed: Vec::new(), + rb_pool: Vec::new(), windows, pools: Vec::new(), free: Vec::new(), @@ -1018,9 +1087,220 @@ impl State { } } + /// `len` bytes from readback memory `p + off` into the title's `dst` (committed pages only) + unsafe fn copy_back(&mut self, p: *mut u8, dst: usize, off: usize, len: usize) { + let mut at = dst; + let mut src_off = off; + let end = dst + len; + while at < end { + let pg = at & !(PAGE - 1); + let t = pg & !(TILE - 1); + let idx = (pg - t) / PAGE; + let step = (pg + PAGE).min(end) - at; + if self.mapped.get(&t).is_some_and(|e| e.holes & (1u16 << idx) == 0) { + std::ptr::copy_nonoverlapping(p.add(src_off), at as *mut u8, step); + } + at += step; + src_off += step; + } + self.sync_sent_range(dst, len); + } + + /// A copy back's list and a readback buffer of at least `need` bytes (reused when one fits) + unsafe fn rb_job(&mut self, need: usize) -> Option { + if let Some(i) = self.rb_pool.iter().enumerate().filter(|(_, j)| j.cap >= need).min_by_key(|(_, j)| j.cap).map(|(i, _)| i) { + return Some(self.rb_pool.swap_remove(i)); + } + let cap = need.max(64 << 10).next_power_of_two().min(READBACK_STAGING); + self.note_rb_bytes(cap); + // past the device's hooks, as `init`'s own lists are: an adopted list took XCL's list + // hooks on the lane's queue, and crashed vkd3d-proton at its Close (#1993 review) + let (alloc, list) = super::real::create_own_list(&self.dev, D3D12_COMMAND_LIST_TYPE_DIRECT)?; + let _ = list.SetName(windows::core::w!("xcl sparse readback list")); + let mut d = buffer_desc(cap as u64); + d.Flags = D3D12_RESOURCE_FLAG_NONE; + let mut r: Option = None; + self.dev + .CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_READBACK, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &d, D3D12_RESOURCE_STATE_COPY_DEST, None, &mut r) + .ok()?; + let buf = r?; + let mut p: *mut c_void = std::ptr::null_mut(); + buf.Map(0, None, Some(&mut p)).ok()?; + Some(RbJob { alloc, list, buf, ptr: p as *mut u8, cap, rb_v: 0, done_v: 0, seq: 0, copies: Vec::new() }) + } + + /// The lane of the title's `queue`: the one serving it, or else an unused one, or else a free + /// one (no copies back in flight; its fences only go up, so it can serve another queue). + /// None: all are busy. + fn rb_lane(&mut self, queue: &ID3D12CommandQueue) -> Option { + let title = queue.as_raw() as usize; + let lanes = &self.rb_lanes; + let i = lanes.iter().position(|l| l.title == title).or_else(|| lanes.iter().position(|l| l.title == 0)).or_else(|| lanes.iter().position(|l| l.jobs.is_empty()))?; + self.rb_lanes[i].title = title; + Some(i) + } + + /// Land a finished copy back in the title's memory, except the bytes a newer one (from + /// another lane) landed already. + unsafe fn land(&mut self, mut job: RbJob) { + let copies = std::mem::take(&mut job.copies); + for &(dst, off, len) in &copies { + let end = dst + len; + let mut newer: Vec<(usize, usize)> = self.rb_landed.iter().filter(|&&(a, b, seq)| seq > job.seq && a < end && b > dst).map(|&(a, b, _)| (a.max(dst), b.min(end))).collect(); + newer.sort_unstable(); + let mut at = dst; + for (a, b) in newer.into_iter().chain(std::iter::once((end, end))) { + if a > at { + self.copy_back(job.ptr, at, off + (at - dst), a - at); + } + at = at.max(b); + } + self.rb_landed.push((dst, end, job.seq)); + } + RB_PENDING.fetch_sub(1, Ordering::Release); + job.copies = copies; + job.copies.clear(); + self.rb_pool.push(job); + self.trim_rb_pool(RB_POOL_MAX); + } + + /// Idle copy-back buffers above `max` bytes go, the largest first. In flight they cannot: + /// with the title's queues stalled the copies of every submit wait, one buffer each. + fn trim_rb_pool(&mut self, max: usize) { + let mut total: usize = self.rb_pool.iter().map(|j| j.cap).sum(); + while total > max { + let Some(i) = self.rb_pool.iter().enumerate().max_by_key(|(_, j)| j.cap).map(|(i, _)| i) else { break }; + total -= self.rb_pool.swap_remove(i).cap; + } + } + + /// Logs the copy-back buffers' bytes (idle and in flight) each time they pass another + /// doubling from 64 MB, with a new buffer of `adding` bytes about to be made. + fn note_rb_bytes(&self, adding: usize) { + static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(64 << 20); + let idle: usize = self.rb_pool.iter().map(|j| j.cap).sum(); + let flight: usize = self.rb_lanes.iter().flat_map(|l| l.jobs.iter()).map(|j| j.cap).sum(); + let total = idle + flight + adding; + let next = NEXT.load(Ordering::Relaxed); + if total >= next && NEXT.compare_exchange(next, next * 2, Ordering::Relaxed, Ordering::Relaxed).is_ok() { + crate::xlog_info!("sparse: copy-back buffers {} MB ({} MB in flight, {} MB idle)", total >> 20, flight >> 20, idle >> 20); + } + } + + /// Copies back whose title work is done but whose copy has not finished: each lane's done + /// fence and the value its last such copy reaches. + unsafe fn copies_after_title(&self) -> Vec<(ID3D12Fence, u64)> { + let mut out = Vec::new(); + for l in &self.rb_lanes { + let (src, done) = (l.src.GetCompletedValue(), l.done.GetCompletedValue()); + if let Some(v) = l.jobs.iter().filter(|j| j.rb_v <= src && j.done_v > done).map(|j| j.done_v).max() { + out.push((l.done.clone(), v)); + } + } + out + } + + /// Land submitted copies back in the title's memory, oldest first on each lane, as far as + /// `how` says. + unsafe fn drain_readbacks(&mut self, how: Drain) { + for i in 0..self.rb_lanes.len() { + while let Some((rb_v, done_v)) = self.rb_lanes[i].jobs.front().map(|j| (j.rb_v, j.done_v)) { + let (src, done) = (self.rb_lanes[i].src.clone(), self.rb_lanes[i].done.clone()); + if done.GetCompletedValue() < done_v { + if how == Drain::Finished || src.GetCompletedValue() < rb_v { + break; + } + // the title's lists are done: only the copy itself is left (5 s at most, + // whatever an earlier registration on the lane's event does) + let (event, t0) = (self.rb_lanes[i].event, std::time::Instant::now()); + while done.GetCompletedValue() < done_v && t0.elapsed().as_millis() < 5000 { + let _ = done.SetEventOnCompletion(done_v, event); + windows::Win32::System::Threading::WaitForSingleObject(event, 5000u32.saturating_sub(t0.elapsed().as_millis() as u32).max(1)); + } + if done.GetCompletedValue() < done_v { + crate::xlog_warn!("sparse: a copy back has not finished 5 s after the title's work it follows"); + break; + } + } + let job = self.rb_lanes[i].jobs.pop_front().expect("front"); + self.land(job); + } + } + // a landed range matters only to older copies back still in flight + match self.rb_lanes.iter().filter_map(|l| l.jobs.front().map(|j| j.seq)).min() { + Some(oldest) => self.rb_landed.retain(|&(_, _, seq)| seq > oldest), + None => self.rb_landed.clear(), + } + } + + /// `readback_ranges` without a CPU wait: the copies run on lane `li`'s queue, after the + /// title's queue signalled the lane's `src` = `rb_v`, and land in the title's memory in + /// `drain_readbacks`. + unsafe fn readback_async(&mut self, li: usize, ranges: &[(usize, usize)], rb_v: u64) { + let (q, done) = (self.rb_lanes[li].q.clone(), self.rb_lanes[li].done.clone()); + // the mapped parts, in readback-buffer sized batches: (window, address, bytes) + let mut batches: Vec> = vec![Vec::new()]; + let mut used = 0usize; + for &(a, n) in ranges { + let mut at = 0usize; + while at < n { + let cur = a + at; + let tile = cur & !(TILE - 1); + let Some(wi) = self.window_index(cur) else { break }; + let to_tile_end = (tile + TILE) - cur; + if !self.mapped.contains_key(&tile) { + at += (n - at).min(to_tile_end); + continue; + } + if used == READBACK_STAGING { + batches.push(Vec::new()); + used = 0; + } + let to_win_end = (self.windows[wi].0 + crate::gpu::uma::WINDOW) - cur; + let chunk = (n - at).min(READBACK_STAGING - used).min(to_tile_end).min(to_win_end); + batches.last_mut().expect("batch").push((wi, cur, chunk)); + used += chunk; + at += chunk; + } + } + for batch in batches.into_iter().filter(|b| !b.is_empty()) { + let need = batch.iter().map(|c| c.2).sum::(); + let Some(mut job) = self.rb_job(need) else { + crate::xlog_warn!("sparse: no readback buffer for a copy back of {need} bytes – left out"); + return; + }; + let _ = job.alloc.Reset(); + let _ = job.list.Reset(&job.alloc, None); + let mut off = 0usize; + for (wi, cur, chunk) in batch { + let (base, res) = (self.windows[wi].0, self.windows[wi].1.clone()); + job.list.CopyBufferRegion(&job.buf, off as u64, &res, (cur - base) as u64, chunk as u64); + job.copies.push((cur, off, chunk)); + off += chunk; + } + if job.list.Close().is_ok() { + if let Ok(l) = job.list.cast::() { + super::title_textures::own_work(|| q.ExecuteCommandLists(&[Some(l)])); + } + } + let lane = &mut self.rb_lanes[li]; + lane.done_value += 1; + let _ = q.Signal(&done, lane.done_value); + job.rb_v = rb_v; + job.done_v = lane.done_value; + job.seq = self.rb_seq; + self.rb_seq += 1; + lane.jobs.push_back(job); + RB_PENDING.fetch_add(1, Ordering::Release); + RB_ASYNC.store(true, Ordering::Relaxed); + } + } + /// Copy `ranges` (address, bytes) from sparse VRAM back into the CPU view and refresh /// `sent` hashes. Caller has already ordered `self.queue` after any GPU writes if needed. unsafe fn readback_ranges(&mut self, ranges: &[(usize, usize)]) { + // copies back of earlier submissions first: their data is older than this one's + self.drain_readbacks(Drain::TitleDone); let Some((rb, p)) = self.readback_buf() else { return }; let mut pending: Vec<(usize, usize, usize)> = Vec::new(); // (va, rb_off, chunk) let mut used = 0usize; @@ -1041,22 +1321,7 @@ impl State { s.reclaim_ring(); } for &(dst, off, len) in pending.iter() { - // copy only committed (non-hole) bytes - let mut at = dst; - let mut src_off = off; - let end = dst + len; - while at < end { - let pg = at & !(PAGE - 1); - let t = pg & !(TILE - 1); - let idx = (pg - t) / PAGE; - let step = (pg + PAGE).min(end) - at; - if s.mapped.get(&t).is_some_and(|e| e.holes & (1u16 << idx) == 0) { - std::ptr::copy_nonoverlapping(p.add(src_off), at as *mut u8, step); - } - at += step; - src_off += step; - } - s.sync_sent_range(dst, len); + s.copy_back(p, dst, off, len); } pending.clear(); *used = 0; @@ -1251,28 +1516,175 @@ pub unsafe fn readback_after_execute(queue: &ID3D12CommandQueue, ranges: &[(usiz let mut g = lock_state(); let Some(s) = g.as_mut() else { return }; s.note_queue(queue); - let mut filtered: Vec<(usize, usize)> = ranges.iter().copied().filter(|&(a, n)| n > 0 && s.window_index(a).is_some()).collect(); - if filtered.is_empty() { + s.after_execute(queue, ranges); +} + +/// copies back submitted and not landed yet, and whether any ran on the readback queue +static RB_PENDING: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); +static RB_ASYNC: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + +/// Whether copies back land asynchronously (any ran on the readback queue so far) +pub fn readbacks_async() -> bool { + RB_ASYNC.load(Ordering::Relaxed) +} + +/// Copies back that are done on the GPU, into the title's memory, and those whose title work +/// is done (waiting for the copy): before a CPU read of memory the title's lists wrote. +pub unsafe fn settle_readbacks() { + if RB_PENDING.load(Ordering::Acquire) == 0 { return; } - filtered.sort_unstable(); - let mut merged: Vec<(usize, usize)> = Vec::with_capacity(filtered.len()); - for (a, n) in filtered { - match merged.last_mut() { - Some(last) if a <= last.0.saturating_add(last.1) => { - let end = (last.0 + last.1).max(a + n); - last.1 = end - last.0; - } - _ => merged.push((a, n)), + if let Some(s) = STATE.lock().unwrap().as_mut() { + s.drain_readbacks(Drain::TitleDone); + } +} + +/// `settle_readbacks` from a fence observation or a CPU wait for a value in memory: the state's +/// lock is only tried, as its holder may be waiting for a fence with the lock held, and for work +/// this thread is to submit or signal. A copy whose title work is done but which still runs is +/// waited for with the lock let go (5 s at most), then landed: waiting with it held stood every +/// submit behind a copy that stood behind a stalled GPU (lucsoft, RDNA3). +/// True when every copy back whose title work is done has landed (none in flight, or no sparse +/// state); false when the lock could not be had or such a copy did not finish in time: the +/// caller must not tell the title its work is done yet (bmjask's review). +pub unsafe fn settle_readbacks_try() -> bool { + if RB_PENDING.load(Ordering::Acquire) == 0 { + return true; + } + let running = match with_state_try(std::time::Duration::from_millis(2), |s| { + s.drain_readbacks(Drain::Finished); + s.copies_after_title() + }) { + Some(Some(running)) => running, + Some(None) => return true, + None => return false, + }; + if running.is_empty() { + return true; + } + let t0 = std::time::Instant::now(); + let mut finished = true; + for (done, v) in &running { + wait_fence(done, *v, std::time::Duration::from_secs(5).saturating_sub(t0.elapsed())); + finished &= done.GetCompletedValue() >= *v; + } + finished && with_state_try(std::time::Duration::from_millis(20), |s| s.drain_readbacks(Drain::Finished)).is_some() +} + +/// `settle_readbacks_try` again every millisecond until it lands what it must, `limit` at most +/// (then the title is told anyway, as a copy that never finishes would hold it forever). For +/// callers that may wait: a holder of the state's lock that waits for work this thread is to +/// submit lets it go within its own 5 s. +pub unsafe fn settle_readbacks_until_landed(limit: std::time::Duration) -> bool { + let t0 = std::time::Instant::now(); + loop { + if settle_readbacks_try() { + return true; + } + if t0.elapsed() >= limit { + static ONCE: std::sync::Once = std::sync::Once::new(); + ONCE.call_once(|| crate::xlog_warn!("sparse: copies back not landed after {} s – the title's fence wait is let go anyway", limit.as_secs())); + return false; + } + std::thread::sleep(std::time::Duration::from_millis(1)); + } +} + +/// `f` on the sparse state if its lock can be had within `patience` (Some(None): there is no +/// sparse state) +fn with_state_try(patience: std::time::Duration, f: impl FnOnce(&mut State) -> R) -> Option> { + let t0 = std::time::Instant::now(); + loop { + if let Ok(mut g) = STATE.try_lock() { + return Some(g.as_mut().map(f)); } + if t0.elapsed() >= patience { + return None; + } + std::thread::yield_now(); + } +} + +/// Waits until `fence` reaches `v`, `limit` at most, on an event of this thread's own (looking +/// again after each wake: a wait that gave up earlier can set the event late) +unsafe fn wait_fence(fence: &ID3D12Fence, v: u64, limit: std::time::Duration) { + thread_local! { + static EVENT: HANDLE = unsafe { windows::Win32::System::Threading::CreateEventW(None, false, false, None).unwrap_or_default() }; } - s.rb_value += 1; - let v = s.rb_value; - if queue.Signal(&s.rb_fence, v).is_ok() { - let _ = s.queue.Wait(&s.rb_fence, v); - super::list_x::note_internal_wait(s.queue.as_raw() as usize, "sparse readback behind a title queue", &s.rb_fence, v); + let event = EVENT.with(|e| *e); + let t0 = std::time::Instant::now(); + while fence.GetCompletedValue() < v && t0.elapsed() < limit && !event.is_invalid() { + let _ = fence.SetEventOnCompletion(v, event); + let left = limit.saturating_sub(t0.elapsed()).as_millis().clamp(1, u32::MAX as u128) as u32; + windows::Win32::System::Threading::WaitForSingleObject(event, left); + } +} + +/// A copy-back lane's queue and fences +unsafe fn rb_lane_new(dev: &ID3D12Device) -> windows::core::Result { + let q: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC::default())?; + let _ = q.SetName(windows::core::w!("xcl sparse readback queue")); + let src: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE)?; + let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE)?; + let event = windows::Win32::System::Threading::CreateEventW(None, false, false, None)?; + Ok(RbLane { title: 0, src, src_value: 0, q, done, done_value: 0, event, jobs: VecDeque::new() }) +} + +/// The thread that moves finished copies back into the title's memory, so it sees what its +/// lists wrote without submitting again (started with the first copy back). +fn start_readback_thread() { + static STARTED: std::sync::Once = std::sync::Once::new(); + STARTED.call_once(|| { + let _ = std::thread::Builder::new().name("xcl-sparse-readback".into()).spawn(|| loop { + std::thread::sleep(std::time::Duration::from_micros(500)); + if let Ok(mut g) = STATE.try_lock() { + if let Some(s) = g.as_mut().filter(|s| s.rb_lanes.iter().any(|l| !l.jobs.is_empty())) { + unsafe { s.drain_readbacks(Drain::Finished) }; + } + } + }); + }); +} + +impl State { + /// `readback_after_execute` on this state + unsafe fn after_execute(&mut self, queue: &ID3D12CommandQueue, ranges: &[(usize, usize)]) { + let s = self; + let mut filtered: Vec<(usize, usize)> = ranges.iter().copied().filter(|&(a, n)| n > 0 && s.window_index(a).is_some()).collect(); + if filtered.is_empty() { + return; + } + filtered.sort_unstable(); + let mut merged: Vec<(usize, usize)> = Vec::with_capacity(filtered.len()); + for (a, n) in filtered { + match merged.last_mut() { + Some(last) if a <= last.0.saturating_add(last.1) => { + let end = (last.0 + last.1).max(a + n); + last.1 = end - last.0; + } + _ => merged.push((a, n)), + } + } + if let Some(li) = s.rb_lane(queue) { + let lane = &mut s.rb_lanes[li]; + lane.src_value += 1; + let v = lane.src_value; + if queue.Signal(&lane.src, v).is_ok() { + let _ = lane.q.Wait(&lane.src, v); + super::list_x::note_internal_wait(lane.q.as_raw() as usize, "sparse copy back behind its title queue", &lane.src, v); + } + s.readback_async(li, &merged, v); + start_readback_thread(); + return; + } + s.rb_value += 1; + let v = s.rb_value; + if queue.Signal(&s.rb_fence, v).is_ok() { + let _ = s.queue.Wait(&s.rb_fence, v); + super::list_x::note_internal_wait(s.queue.as_raw() as usize, "sparse readback behind a title queue", &s.rb_fence, v); + } + s.readback_ranges(&merged); } - s.readback_ranges(&merged); } /// Synchronize `[va, va + len)` in a sparse window for a CPU read (`ID3D12Resource::Map` with @@ -1469,6 +1881,12 @@ impl State { } } } + // copies back still to run read the windows too, on their lanes' queues + if ok { + for l in self.rb_lanes.iter().filter(|l| !l.jobs.is_empty()) { + marks.push((l.done.clone(), l.done_value)); + } + } self.quarantined.push_back(RetiredTiles { tiles, marks: ok.then_some(marks) }); } @@ -1556,6 +1974,7 @@ pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page: let mut g = lock_state(); let Some(s) = g.as_mut() else { return }; s.note_queue(queue); + s.drain_readbacks(Drain::Finished); // the title's work sees what was decommitted unmapped, as it was let flushed = s.flush_parked(); let drained = s.drain_quarantined(); @@ -1733,6 +2152,428 @@ fn forget_in(s: &mut State, res: usize) { mod tests { use super::*; + /// A WARP device and a sparse state with one window of one tile, committed and mapped, whose + /// copies back run on lanes: the state, the device, the window's reserved buffer and its + /// address (None: no WARP device). These tests block a queue of their device on purpose; + /// on the GPU's device, which every test in the process shares, a D3D12 call made then can + /// wait in the driver for another test's resource release, which waits for that queue. + unsafe fn lane_state() -> Option<(State, ID3D12Device, ID3D12Resource, usize)> { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + use windows_sys::Win32::System::Memory::MEM_RESERVE; + let factory: IDXGIFactory4 = CreateDXGIFactory1().ok()?; + let warp: IDXGIAdapter = factory.EnumWarpAdapter().ok()?; + let mut dev: Option = None; + D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_11_0, &mut dev).ok()?; + let dev = dev?; + let base = VirtualAlloc(std::ptr::null(), TILE, MEM_RESERVE, PAGE_NOACCESS) as usize; + assert!(base != 0 && base % TILE == 0); + assert!(!VirtualAlloc(base as *const c_void, TILE, MEM_COMMIT, PAGE_READWRITE).is_null()); + let mut reserved: Option = None; + dev.CreateReservedResource(&buffer_desc(TILE as u64), D3D12_RESOURCE_STATE_COMMON, None, &mut reserved).expect("CreateReservedResource"); + let reserved = reserved.expect("reserved buffer"); + let rb_alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).expect("rb_alloc"); + let rb_list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &rb_alloc, None).expect("rb_list"); + rb_list.Close().expect("rb_list close"); + let mut s = State { + dev: dev.clone(), + queue: dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC::default()).expect("queue"), + fence: dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).expect("fence"), + value: 0, + event: windows::Win32::System::Threading::CreateEventW(None, false, false, None).expect("event"), + rb_fence: dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).expect("rb_fence"), + rb_value: 0, + rb_lanes: (0..2).map(|_| rb_lane_new(&dev).expect("lane")).collect(), + rb_seq: 0, + rb_landed: Vec::new(), + rb_pool: Vec::new(), + windows: vec![(base, reserved.clone())], + pools: Vec::new(), + free: Vec::new(), + retired: Vec::new(), + mapped: FxHashMap::default(), + pinned: FxHashMap::default(), + parked: FxHashMap::default(), + quarantined: VecDeque::new(), + queue_fences: FxHashMap::default(), + buffers: FxHashMap::default(), + to_zero: Vec::new(), + staging: None, + ring_write: 0, + ring_submitted: 0, + ring_free: 0, + inflight: VecDeque::new(), + zero_buf: None, + slots: Vec::new(), + next_slot: 0, + readback: None, + rb_alloc, + rb_list, + rb_last_fence: 0, + waited: FxHashMap::default(), + runs: None, + scratch_runs: Vec::new(), + }; + s.commit_range(base, TILE); + assert!(s.mapped.contains_key(&base)); + Some((s, dev, reserved, base)) + } + + /// A title queue whose next list copies 256 bytes of `0x{tag}_0000 | i` dwords to `at` in + /// the window: behind the tile mapping and, with `gate`, behind `gate` reaching 1. Two + /// readback buffers are made here, before the queue blocks. + struct TitleCopy { + queue: ID3D12CommandQueue, + _keep: (ID3D12Resource, ID3D12CommandAllocator, ID3D12GraphicsCommandList), + } + + unsafe fn title_copy(s: &mut State, dev: &ID3D12Device, reserved: &ID3D12Resource, at: u64, tag: u32, gate: Option<&ID3D12Fence>) -> TitleCopy { + let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC::default()).expect("title queue"); + let jobs = [s.rb_job(256).expect("readback buffer"), s.rb_job(256).expect("readback buffer")]; + s.rb_pool.extend(jobs); + let mut up: Option = None; + let mut d = buffer_desc(256); + d.Flags = D3D12_RESOURCE_FLAG_NONE; + dev.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_UPLOAD, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &d, D3D12_RESOURCE_STATE_GENERIC_READ, None, &mut up).expect("upload buffer"); + let up = up.expect("upload buffer"); + let mut p: *mut c_void = std::ptr::null_mut(); + up.Map(0, None, Some(&mut p)).expect("Map"); + for i in 0..64u32 { + *(p as *mut u32).add(i as usize) = (tag << 16) | i; + } + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).expect("alloc"); + let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).expect("list"); + list.CopyBufferRegion(reserved, at, &up, 0, 256); + list.Close().expect("close"); + queue.Wait(&s.fence, s.value).expect("wait for the tile mapping"); + if let Some(g) = gate { + queue.Wait(g, 1).expect("wait for the gate"); + } + queue.ExecuteCommandLists(&[Some(list.cast::().unwrap())]); + TitleCopy { queue, _keep: (up, alloc, list) } + } + + /// Opens `gate` after 20 s if the test has not, so that a test stuck in a D3D12 call + /// fails instead of hanging the queue tests that share the device + fn open_gate_later(gate: &ID3D12Fence) -> std::sync::mpsc::Sender<()> { + struct Gate(ID3D12Fence); + unsafe impl Send for Gate {} + let g = Gate(gate.clone()); + let (tx, rx) = std::sync::mpsc::channel::<()>(); + std::thread::spawn(move || { + let g = g; + if rx.recv_timeout(std::time::Duration::from_secs(20)).is_err() { + unsafe { + let _ = g.0.Signal(1); + } + } + }); + tx + } + + /// Waits (up to 5 s) until the title's `queue` is past its last submission + unsafe fn wait_title(s: &State, queue: &ID3D12CommandQueue) { + let lane = s.rb_lanes.iter().find(|l| l.title == queue.as_raw() as usize).expect("the queue's lane"); + if lane.src.GetCompletedValue() < lane.src_value { + lane.src.SetEventOnCompletion(lane.src_value, s.event).expect("SetEventOnCompletion"); + windows::Win32::System::Threading::WaitForSingleObject(s.event, 5000); + } + assert!(lane.src.GetCompletedValue() >= lane.src_value, "the title's queue did not finish"); + } + + /// A copy back after a title submission does not wait for the title's queue: that queue can + /// be waiting for work the title submits next, from the thread that is in here. The copy + /// lands once the title's work is done (`drain_readbacks`). Before, the sparse queue + /// GPU-waited for the title's queue and this thread waited for the copy (5 s, then on). + #[test] + fn a_copy_back_does_not_wait_for_the_title_queue() { + use windows_sys::Win32::System::Memory::MEM_RELEASE; + unsafe { + let Some((mut s, dev, reserved, base)) = lane_state() else { + crate::gpu::recomp::runner::skip_test("no WARP device"); + return; + }; + std::ptr::write_bytes(base as *mut u8, 0, 256); + let gate: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).expect("gate"); + let opened = open_gate_later(&gate); + let title = title_copy(&mut s, &dev, &reserved, 0, 0xC0DE, Some(&gate)); + + let t0 = std::time::Instant::now(); + s.after_execute(&title.queue, &[(base, 256)]); + let took = t0.elapsed(); + assert!(took < std::time::Duration::from_secs(1), "the copy back waited {took:?} for the title's queue"); + s.drain_readbacks(Drain::Finished); + assert_eq!(*(base as *const u32), 0, "nothing lands before the title's work is done"); + + // the title's queue goes on: the copy back lands once it is done + gate.Signal(1).expect("gate signal"); + let _ = opened.send(()); + wait_title(&s, &title.queue); + s.drain_readbacks(Drain::TitleDone); + for i in 0..64u32 { + assert_eq!(*(base as *const u32).add(i as usize), 0xC0DE_0000 | i, "dword {i} after the copy back"); + } + assert!(s.rb_lanes[0].jobs.is_empty(), "the copy back was consumed"); + assert_eq!(s.rb_pool.len(), 2, "its buffer is kept for the next one"); + drop(s); + VirtualFree(base as *mut c_void, 0, MEM_RELEASE); + } + } + + /// Two title queues: the second one's work and signal do not let the first one's copy back + /// start before the first one's lists ran (with one fence for all queues, the second + /// queue's later value met the first copy's wait). Each lands once its own queue is done. + #[test] + fn a_copy_back_waits_for_its_own_title_queue() { + use windows_sys::Win32::System::Memory::MEM_RELEASE; + const UNTOUCHED: u32 = 0x5555_5555; + unsafe { + let Some((mut s, dev, reserved, base)) = lane_state() else { + crate::gpu::recomp::runner::skip_test("no WARP device"); + return; + }; + let words = base as *mut u32; + for i in 0..128 { + *words.add(i) = UNTOUCHED; + } + let gate: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).expect("gate"); + let opened = open_gate_later(&gate); + // A: blocked on the gate, writes 0..256; B: runs at once, writes 256..512 + let a = title_copy(&mut s, &dev, &reserved, 0, 0xA0A0, Some(&gate)); + let b = title_copy(&mut s, &dev, &reserved, 256, 0xB0B0, None); + s.after_execute(&a.queue, &[(base, 256)]); + s.after_execute(&b.queue, &[(base + 256, 256)]); + assert_eq!(s.rb_lanes.len(), 2, "a lane for each title queue"); + + wait_title(&s, &b.queue); + s.drain_readbacks(Drain::TitleDone); + for i in 0..64usize { + assert_eq!(*words.add(64 + i), 0xB0B0_0000 | i as u32, "B's dword {i} once B is done"); + assert_eq!(*words.add(i), UNTOUCHED, "A's dword {i} landed before A's lists ran"); + } + assert_eq!(s.rb_lanes[0].jobs.len(), 1, "A's copy back waits for A"); + + gate.Signal(1).expect("gate signal"); + let _ = opened.send(()); + wait_title(&s, &a.queue); + s.drain_readbacks(Drain::TitleDone); + for i in 0..64usize { + assert_eq!(*words.add(i), 0xA0A0_0000 | i as u32, "A's dword {i} once A is done"); + assert_eq!(*words.add(64 + i), 0xB0B0_0000 | i as u32, "B's dword {i} after A's copy back"); + } + assert!(s.rb_lanes.iter().all(|l| l.jobs.is_empty())); + assert!(s.rb_landed.is_empty(), "nothing in flight is older than what landed"); + drop((a, b, s)); + VirtualFree(base as *mut c_void, 0, MEM_RELEASE); + } + } + + /// A copy whose title work is done but which still runs is found without waiting + /// (`copies_after_title`), is not landed by `Drain::Finished`, and lands once its fence is + /// waited for: what `settle_readbacks_try` does with the state's lock let go in between. + #[test] + fn a_copy_still_running_after_its_title_work_is_waited_for_outside_the_lock() { + use windows_sys::Win32::System::Memory::MEM_RELEASE; + unsafe { + let Some((mut s, dev, reserved, base)) = lane_state() else { + crate::gpu::recomp::runner::skip_test("no WARP device"); + return; + }; + std::ptr::write_bytes(base as *mut u8, 0, 256); + // the lane's copy queue stands behind a gate; the title's queue runs at once + let gate: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).expect("gate"); + let opened = open_gate_later(&gate); + s.rb_lanes[0].q.Wait(&gate, 1).expect("lane wait"); + let title = title_copy(&mut s, &dev, &reserved, 0, 0xD0D0, None); + s.after_execute(&title.queue, &[(base, 256)]); + wait_title(&s, &title.queue); + + let running = s.copies_after_title(); + assert_eq!(running.len(), 1, "the title's work is done and its copy is not"); + s.drain_readbacks(Drain::Finished); + assert_eq!(*(base as *const u32), 0, "nothing lands before the copy is done"); + + gate.Signal(1).expect("gate signal"); + let _ = opened.send(()); + let t0 = std::time::Instant::now(); + for (done, v) in &running { + wait_fence(done, *v, std::time::Duration::from_secs(5)); + } + assert!(t0.elapsed() < std::time::Duration::from_secs(5), "the copy did not finish"); + s.drain_readbacks(Drain::Finished); + for i in 0..64u32 { + assert_eq!(*(base as *const u32).add(i as usize), 0xD0D0_0000 | i, "dword {i} after the wait"); + } + assert!(s.copies_after_title().is_empty()); + drop((title, s)); + VirtualFree(base as *mut c_void, 0, MEM_RELEASE); + } + } + + /// Quarantined tiles also wait for the copies back still to run on a lane's queue: they + /// read the windows after the title's work, which the title queues' marks already cover. + #[test] + fn quarantined_tiles_wait_for_copies_back_still_to_run() { + use windows_sys::Win32::System::Memory::MEM_RELEASE; + unsafe { + let Some((mut s, dev, reserved, base)) = lane_state() else { + crate::gpu::recomp::runner::skip_test("no WARP device"); + return; + }; + let gate: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).expect("gate"); + let opened = open_gate_later(&gate); + s.rb_lanes[0].q.Wait(&gate, 1).expect("lane wait"); + let title = title_copy(&mut s, &dev, &reserved, 0, 0xE0E0, None); + s.after_execute(&title.queue, &[(base, 256)]); + wait_title(&s, &title.queue); + let running = s.copies_after_title(); + assert_eq!(running.len(), 1, "the title's work is done and its copy is not"); + + let tile = (u32::MAX, 7); + s.quarantine_tiles(vec![tile]); + assert!(!s.drain_quarantined(), "not freed while a copy back still reads the windows"); + assert!(!s.free.contains(&tile)); + + gate.Signal(1).expect("gate signal"); + let _ = opened.send(()); + for (done, v) in &running { + wait_fence(done, *v, std::time::Duration::from_secs(5)); + } + assert!(s.drain_quarantined(), "freed once the copy back is done"); + assert!(s.free.contains(&tile)); + s.drain_readbacks(Drain::Finished); + drop((title, s)); + VirtualFree(base as *mut c_void, 0, MEM_RELEASE); + } + } + + /// A title fence on an adopted device does not show its work done while a copy back is in + /// flight and the sparse state is held by another thread: GetCompletedValue stays where the + /// fence was last seen with its copies landed (its initial value here), and the title's + /// SetEventOnCompletion event is not set. Both come once the state can be had. + #[test] + fn a_title_fence_shows_its_work_done_only_once_copies_back_can_land() { + use windows::Win32::Foundation::{WAIT_OBJECT_0, WAIT_TIMEOUT}; + use windows::Win32::System::Threading::{CreateEventW, WaitForSingleObject}; + use windows_sys::Win32::System::Memory::MEM_RELEASE; + unsafe { + let Some((s, dev, _reserved, base)) = lane_state() else { + crate::gpu::recomp::runner::skip_test("no WARP device"); + return; + }; + drop(s); + super::super::real::adopt(dev.as_raw(), super::super::real::Kind::Device); + let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).expect("fence"); + assert!(super::super::real::is_adopted(fence.as_raw()), "the fence is adopted"); + fence.Signal(5).expect("signal"); + + // a copy back in flight; another thread holds the sparse state + RB_PENDING.fetch_add(1, Ordering::Release); + let was_async = RB_ASYNC.swap(true, Ordering::Relaxed); + let (held_tx, held_rx) = std::sync::mpsc::channel::<()>(); + let (go_tx, go_rx) = std::sync::mpsc::channel::<()>(); + let holder = std::thread::spawn(move || { + let _g = STATE.lock().unwrap(); + let _ = held_tx.send(()); + let _ = go_rx.recv_timeout(std::time::Duration::from_secs(10)); + }); + held_rx.recv().expect("state held"); + assert_eq!(fence.GetCompletedValue(), 0, "the fence's work shows as done before its copies back landed"); + let event = CreateEventW(None, false, false, None).expect("event"); + fence.SetEventOnCompletion(5, event).expect("SetEventOnCompletion"); + assert_eq!(WaitForSingleObject(event, 300), WAIT_TIMEOUT, "the title's event was set before its copies back landed"); + + let _ = go_tx.send(()); + holder.join().unwrap(); + assert_eq!(WaitForSingleObject(event, 5000), WAIT_OBJECT_0, "the title's event once the state can be had"); + assert_eq!(fence.GetCompletedValue(), 5); + RB_PENDING.fetch_sub(1, Ordering::Release); + RB_ASYNC.store(was_async, Ordering::Relaxed); + let _ = windows::Win32::Foundation::CloseHandle(event); + VirtualFree(base as *mut c_void, 0, MEM_RELEASE); + } + } + + /// Idle copy-back buffers above the cap go, the largest first; under it they stay. + #[test] + fn idle_copy_back_buffers_above_the_cap_are_given_back() { + unsafe { + let Some((mut s, _dev, _reserved, base)) = lane_state() else { + crate::gpu::recomp::runner::skip_test("no WARP device"); + return; + }; + let jobs = [s.rb_job(64 << 10).expect("buffer"), s.rb_job(128 << 10).expect("buffer"), s.rb_job(64 << 10).expect("buffer")]; + s.rb_pool.extend(jobs); + s.trim_rb_pool(256 << 10); + assert_eq!(s.rb_pool.iter().map(|j| j.cap).sum::(), 256 << 10, "under the cap nothing goes"); + s.trim_rb_pool(160 << 10); + let mut caps: Vec = s.rb_pool.iter().map(|j| j.cap).collect(); + caps.sort_unstable(); + assert_eq!(caps, vec![64 << 10, 64 << 10], "the largest went first, until the rest fit"); + drop(s); + VirtualFree(base as *mut c_void, 0, windows_sys::Win32::System::Memory::MEM_RELEASE); + } + } + + /// A copy back's allocator and list are made past the device's hooks: on an adopted device + /// they are not adopted (an adopted list ran XCL's list hooks on the lane's queue and + /// crashed vkd3d-proton at its Close), and the list still records and runs a copy. + #[test] + fn a_copy_back_list_of_an_adopted_device_is_not_adopted() { + unsafe { + let Some((mut s, dev, _reserved, base)) = lane_state() else { + crate::gpu::recomp::runner::skip_test("no WARP device"); + return; + }; + super::super::real::adopt(dev.as_raw(), super::super::real::Kind::Device); + assert!(super::super::real::is_adopted(dev.as_raw()), "the device is adopted"); + let job = s.rb_job(256).expect("readback buffer"); + assert!(!super::super::real::is_adopted(job.list.as_raw()), "the copy back's list is not adopted"); + assert!(!super::super::real::is_adopted(job.alloc.as_raw()), "its allocator is not adopted"); + job.alloc.Reset().expect("allocator Reset"); + job.list.Reset(&job.alloc, None).expect("list Reset"); + job.list.Close().expect("list Close"); + drop(job); + drop(s); + VirtualFree(base as *mut c_void, 0, windows_sys::Win32::System::Memory::MEM_RELEASE); + } + } + + /// Copies back land per lane, so an older one can land after a newer one of another lane: + /// it leaves the bytes the newer one landed. + #[test] + fn an_older_copy_back_does_not_overwrite_a_newer_one() { + use windows_sys::Win32::System::Memory::MEM_RELEASE; + unsafe { + let Some((mut s, _dev, _reserved, base)) = lane_state() else { + crate::gpu::recomp::runner::skip_test("no WARP device"); + return; + }; + let words = base as *mut u32; + let job = |s: &mut State, seq: u64, at: usize, n: usize, tag: u32| { + let mut j = s.rb_job(n).expect("readback buffer"); + for i in 0..n / 4 { + *(j.ptr as *mut u32).add(i) = (tag << 16) | i as u32; + } + j.seq = seq; + j.copies.push((at, 0, n)); + RB_PENDING.fetch_add(1, Ordering::Release); + j + }; + // older: 0..512; newer: 128..256, landed first + let older = job(&mut s, 0, base, 512, 0x0100); + let newer = job(&mut s, 1, base + 128, 128, 0x0200); + s.land(newer); + s.land(older); + for i in 0..128usize { + let want = if (32..64).contains(&i) { 0x0200_0000 | (i - 32) as u32 } else { 0x0100_0000 | i as u32 }; + assert_eq!(*words.add(i), want, "dword {i}"); + } + drop(s); + VirtualFree(base as *mut c_void, 0, MEM_RELEASE); + } + } + /// `queue()` answers while the state's lock is held: a resource's last `Release` asks for /// it (`late_release::drain_known`) from inside a D3D12 call that the lock's holder can be /// waiting for (a list's `Reset` against an upload's allocator `Reset`, with `XCL_DRED=1`). @@ -1840,6 +2681,10 @@ mod tests { event, rb_fence, rb_value: 0, + rb_lanes: Vec::new(), + rb_seq: 0, + rb_landed: Vec::new(), + rb_pool: Vec::new(), windows: Vec::new(), pools: Vec::new(), free: Vec::new(), @@ -2048,6 +2893,10 @@ mod tests { event, rb_fence, rb_value: 0, + rb_lanes: Vec::new(), + rb_seq: 0, + rb_landed: Vec::new(), + rb_pool: Vec::new(), windows: vec![(base, reserved)], pools: Vec::new(), free: Vec::new(), @@ -2145,6 +2994,10 @@ mod tests { event, rb_fence, rb_value: 0, + rb_lanes: Vec::new(), + rb_seq: 0, + rb_landed: Vec::new(), + rb_pool: Vec::new(), windows: vec![(base, reserved)], pools: Vec::new(), free: Vec::new(), @@ -2270,6 +3123,10 @@ mod tests { event, rb_fence, rb_value: 0, + rb_lanes: Vec::new(), + rb_seq: 0, + rb_landed: Vec::new(), + rb_pool: Vec::new(), windows: vec![(base, reserved)], pools: Vec::new(), free: Vec::new(), From 2f490889758f32dfafd9c0593dd1125332168c7d Mon Sep 17 00:00:00 2001 From: Dmitry Omelchenko <35579597+Ariorh1337@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:37:10 +0100 Subject: [PATCH 2/2] kernelx: XCL_COHERENT_HEAPS=sparse:N puts big GPU heaps in video memory A title's GPU heaps (committed with a PAGE_GRAPHICS_* protection) go to the coherent window, process memory the GPU reaches over the bus. On an RTX 5090 a shader's dependent reads there are ~30x slower than from video memory (no L2 hits, about 2.4 us each). Its store instructions whose lanes leave gaps (per-lane dwordx2/x4, 8- and 16-byte formats) run at ~0.1 GB/s, against ~50 GB/s for lane-contiguous ones and >1 TB/s in video memory. The retail title of #505 keeps 4.4 GB of heaps there (1172, 2199, 563 and 596 MB), and its world passes take 0.4-2.7 s each. Keeping the heaps out of the window was reproduced as removing the stall on an RTX 5080, an RTX 3080 and RDNA3 / Linux (#505), but there the shaders read zeros. With `XCL_COHERENT_HEAPS=sparse` every such heap, with `sparse:N` those of at least N MB, goes to the sparse windows instead: video memory, with the CPU's writes uploaded before each submission, like the title's other graphics memory. A heap that goes there is not counted in the coherent window's size for the next run (`note_coherent_need`). Off by default: it costs a second copy in video memory and the write-watch scan of it, and it needs the asynchronous copies back of the commit below. Without them, the copies back of the title's lists' writes into these heaps wait for the submitting thread. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/kernelx.rs | 44 ++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 4144a1144..836c8ac07 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -329,11 +329,24 @@ pub unsafe extern "system" fn VirtualAlloc(addr: *const c_void, size: usize, t: // read zeros, in the sparse windows video memory would have doubled them). The window is as // large as what the title asked for last time (`uma::coherent_hint`). if addr.is_null() && t & 0x1000 != 0 && p & PAGE_GRAPHICS_ANY != 0 && size <= crate::gpu::uma::MAX_SIZE && std::env::var("XCL_COHERENT_HEAPS").map_or(true, |v| v != "0") { + let align = if t & 0x2000_0000 != 0 && t & 0x0040_0000 == 0 { 2 << 20 } else { crate::gpu::uma::GRAIN }; + // `XCL_COHERENT_HEAPS=sparse[:N]`: heaps of at least N MB (all with no N) go to the + // sparse windows instead – video memory, the CPU's writes uploaded before each + // submission (`d3d12x::sparse`). In the coherent window the GPU reads them over the bus, + // uncached, and on NVIDIA its writes that do not fill whole lines run at ~0.1 GB/s + // (#505). Costs a second copy in video memory and the write-watch scan of it. + if sparse_heaps_min().is_some_and(|min| size >= min) { + let rounded = size.max(1).div_ceil(align) * align; + if let Some((q, _)) = crate::hle::d3d12x::sparse::alloc(rounded, align) { + crate::xlog_info!("kernelx: a GPU heap of {} MB in the sparse windows (XCL_COHERENT_HEAPS)", size >> 20); + trace_mem("VirtualAlloc (graphics heap, sparse VRAM)", 0, rounded, t as u64, p, q); + return q as *mut c_void; + } + } static ASKED: AtomicUsize = AtomicUsize::new(0); let asked = ASKED.fetch_add(size, Ordering::Relaxed) + size; crate::gpu::uma::note_coherent_need(asked); if let Some(u) = uma { - let align = if t & 0x2000_0000 != 0 && t & 0x0040_0000 == 0 { 2 << 20 } else { crate::gpu::uma::GRAIN }; // what XCL keeps of the window for itself (readback heaps; its per-draw arguments have // a pool of their own) const KEEP: usize = 96 << 20; @@ -363,6 +376,21 @@ pub unsafe extern "system" fn VirtualAlloc(addr: *const c_void, size: usize, t: q } +/// `XCL_COHERENT_HEAPS=sparse` (every GPU heap) or `sparse:N` (heaps of at least N MB): the +/// smallest heap the sparse windows take instead of the coherent window. Read once. +fn sparse_heaps_min() -> Option { + static MIN: std::sync::OnceLock> = std::sync::OnceLock::new(); + *MIN.get_or_init(|| std::env::var("XCL_COHERENT_HEAPS").ok().and_then(|v| parse_sparse_heaps(&v))) +} + +/// `sparse` -> 0, `sparse:N` -> N MB in bytes; anything else -> None +fn parse_sparse_heaps(v: &str) -> Option { + match v.trim().strip_prefix("sparse")? { + "" => Some(0), + rest => rest.strip_prefix(':')?.trim().parse::().ok().and_then(|mb| mb.checked_mul(1 << 20)), + } +} + /// Whether a VirtualAlloc that reached the plain-memory fallback is GPU-visible title memory /// (a reservation with MEM_GRAPHICS, MEM_LARGE_PAGES / MEM_64K_PAGES or a PAGE_GRAPHICS_* /// protection, at no fixed address), which is reserved write-watched (`reserve_watched`). @@ -2087,6 +2115,20 @@ pub unsafe extern "system" fn DisableThreadLibraryCalls(h: usize) -> BOOL { mod tests { use super::*; + /// `XCL_COHERENT_HEAPS=sparse[:N]`: which heaps go to the sparse windows + #[test] + fn sparse_heaps_setting() { + assert_eq!(parse_sparse_heaps("sparse"), Some(0), "every heap"); + assert_eq!(parse_sparse_heaps("sparse:1024"), Some(1024 << 20)); + assert_eq!(parse_sparse_heaps(" sparse: 512 "), Some(512 << 20)); + assert_eq!(parse_sparse_heaps("0"), None, "heaps off the coherent window altogether: not this"); + assert_eq!(parse_sparse_heaps("1"), None); + assert_eq!(parse_sparse_heaps("sparse:"), None); + assert_eq!(parse_sparse_heaps("sparse:big"), None); + assert_eq!(parse_sparse_heaps("sparsex"), None); + assert_eq!(parse_sparse_heaps(&format!("sparse:{}", usize::MAX)), None, "no overflow"); + } + fn real_sleep(ms: u32) { std::thread::sleep(std::time::Duration::from_millis(ms as u64)); }