diff --git a/HANDOFF.md b/HANDOFF.md index 823ed23..1549f24 100644 --- a/HANDOFF.md +++ b/HANDOFF.md @@ -1187,9 +1187,21 @@ of the title (it writes to address 0 after a failed D3D12 call: `call 0x14068c76 range. Fixed on top: window 0's descriptor range (512 MB after its first 64 KiB, `Uma::desc_range`) is never the title's. XDefiant reserves 8 GiB + 4 MiB first thing, more than windows 1..2 hold; with the range at the top of window 0 that reservation took all of it, no console descriptor heap could be made ("no room in UMA") and every - frame was black. Tested: Minecraft, Unicorn Overlord, Fortnite, XDefiant. Open: Unicorn fills the coherent window - from its second second (its draws run on XCL's pool); XDefiant spends about a third of its time uploading CPU - writes (so before PR #40 too). + frame was black. Tested: Minecraft, Unicorn Overlord, Fortnite, XDefiant. + - Committed console textures (`real::place_console_texture`) take the coherent window only while it keeps + `COHERENT_KEPT` (128 MB) free; beyond that they are plain PC textures. Unicorn Overlord makes 2.2 GB of them + and had the 512 MB window full in its first second (no room for readback heaps); it now stops at 384 MB, the + picture unchanged. Putting them in the sparse windows instead made XDefiant render black (cache warm, no errors + in the log) – not understood yet; the GPU-owned ones go to `vram` arenas either way. + - XDefiant's menu runs at ~53 fps (~50 before PR #40). Its CPU cost of the sparse windows is the per-submit + write-watch scan over the mapped tiles (~2 GB): ~0.8 ms per submit at ~800 submits a second, then ~0.3 ms of + uploads (64-byte chunk runs of ~0.5 KB on average). Merging the runs or skipping scans would be faster, but a + merged run can overwrite what the GPU wrote next to a CPU-written chunk, and a skipped scan leaves the GPU stale + data. Read-only protection of cold tiles would make the scan cost follow the written pages, but the kernel's own + writes (ReadFile into title memory) then fail instead of faulting. + - Diagnostics: `XCL_TRACE_MEM=1` also logs each coherent-window allocation of 1 MB or more and each failed one, + with the XCL code that asked (`Uma::alloc` is `#[track_caller]`); the log has "N frames presented, X fps over + the last 300" for titles that call PresentX. - **Unicorn Overlord (SEGAofAmericaInc.Pa253b0c1Y 1.0.0.6) is playable (2026-11-22)**: logos, movies, menus and play with the right picture. It runs on a **newer console D3D12 runtime** (its own game OS) with the same SDK version (0x20004) as XDefiant / Minecraft but other vtable layouts: diff --git a/crates/xcl/src/gpu/uma.rs b/crates/xcl/src/gpu/uma.rs index f2fc8e7..9480182 100644 --- a/crates/xcl/src/gpu/uma.rs +++ b/crates/xcl/src/gpu/uma.rs @@ -460,30 +460,58 @@ impl Uma { /// Zeroed block of `size` bytes aligned to `align` (at least 64 KiB), first fit (window 0 /// first). + #[track_caller] pub fn alloc(&self, size: usize, align: usize) -> Option<*mut u8> { self.alloc_where(size, align, None, true) } /// `alloc` for reserved address space: not zeroed (a large reservation is not touched – /// what the title commits or maps there it writes; never-used memory is zero anyway). + #[track_caller] pub fn reserve(&self, size: usize, align: usize) -> Option<*mut u8> { self.alloc_where(size, align, None, false) } /// `alloc` inside window `k` (memory the console addresses with 32 bits, e.g. descriptor /// heaps: window 0). + #[track_caller] pub fn alloc_in(&self, k: usize, size: usize, align: usize) -> Option<*mut u8> { self.alloc_where(size, align, Some(k), true) } + /// `XCL_TRACE_MEM`: each committed-window allocation of 1 MB or more and each one that + /// fails, with the XCL code that asked for it (what fills the window). + #[track_caller] + fn trace(&self, size: usize, got: Option, used: usize) { + let at = std::panic::Location::caller(); + match got { + Some(p) => crate::xlog_info!("UMA: {} KB at {p:#x} for {}:{} ({} MB used)", size >> 10, at.file(), at.line(), used >> 20), + None => crate::xlog_info!("UMA: {} KB for {}:{} – no room ({} MB used)", size >> 10, at.file(), at.line(), used >> 20), + } + } + + fn tracing() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("XCL_TRACE_MEM").is_some()) + } + + #[track_caller] fn alloc_where(&self, size: usize, align: usize, window: Option, zero: bool) -> Option<*mut u8> { let size = (size.max(1) + GRAIN - 1) & !(GRAIN - 1); let align = align.max(GRAIN).next_power_of_two(); let mut g = self.inner.lock().unwrap(); - let (start, len, at) = g.free.iter().filter(|(&o, _)| window.is_none_or(|k| o / WINDOW == k)).find_map(|(&o, &l)| { + let found = g.free.iter().filter(|(&o, _)| window.is_none_or(|k| o / WINDOW == k)).find_map(|(&o, &l)| { let at = ((self.base + o + align - 1) & !(align - 1)) - self.base; (at + size <= o + l).then_some((o, l, at)) - })?; + }); + let Some((start, len, at)) = found else { + if Self::tracing() { + let used = g.used.values().sum(); + drop(g); + self.trace(size, None, used); + } + return None; + }; g.free.remove(&start); if at > start { g.free.insert(start, at - start); @@ -492,8 +520,12 @@ impl Uma { g.free.insert(at + size, start + len - at - size); } g.used.insert(at, size); + let used = if Self::tracing() && size >= 1 << 20 { Some(g.used.values().sum()) } else { None }; drop(g); let p = (self.base + at) as *mut u8; + if let Some(used) = used { + self.trace(size, Some(p as usize), used); + } if zero { unsafe { std::ptr::write_bytes(p, 0, size) }; } diff --git a/crates/xcl/src/hle/d3d12x/present.rs b/crates/xcl/src/hle/d3d12x/present.rs index d19a2c6..1ccb812 100644 --- a/crates/xcl/src/hle/d3d12x/present.rs +++ b/crates/xcl/src/hle/d3d12x/present.rs @@ -49,7 +49,15 @@ pub unsafe extern "system" fn present_x(queue: *mut c_void, plane_count: u32, pl return 0; } super::frame::mark_flipped((planes as *const u64).read_unaligned()); - BOUNDARIES.fetch_add(1, Ordering::Relaxed); + let frames = BOUNDARIES.fetch_add(1, Ordering::Relaxed) + 1; + if frames % 300 == 0 { + // the title's frame rate, for comparing runs (every 300 frames) + static LAST: Mutex> = Mutex::new(None); + let now = std::time::Instant::now(); + if let Some(t) = LAST.lock().unwrap().replace(now) { + crate::xlog_info!("D3D12.X: {frames} frames presented, {:.1} fps over the last 300", 300.0 / (now - t).as_secs_f64()); + } + } super::real::stop_if_device_removed(); if !PRESENTX.swap(true, Ordering::Relaxed) { crate::xlog_info!("D3D12.X: PresentX – the title presents its own frames ({plane_count} plane(s))"); diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index a6db0ef..a49d695 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -976,10 +976,18 @@ fn buffer_view(res: usize, format: windows::Win32::Graphics::Dxgi::Common::DXGI_ /// Console buffer → a placed resource in the unified memory heap, so its console address is /// what the recompiled shaders read through `mem`. None: not placed (fall back to committed). /// A committed texture of console code: on the console its memory is unified memory too – -/// titles write it through its GPU address (XDefiant clears one with memset). It gets UMA -/// memory of the console's size (XG; the PC's where XG has no model) and is created as a -/// texture at that address (`device_x::create_placed_resource_x`: kept in step with the -/// memory where XG lays it out); GetGPUVirtualAddress returns the address. +/// titles write it through its GPU address (XDefiant clears one with memset). It gets memory +/// of the console's size (XG; the PC's where XG has no model) and is created as a texture at +/// that address (`device_x::create_placed_resource_x`: kept in step with the memory where XG +/// lays it out); GetGPUVirtualAddress returns the address. The memory is the coherent +/// window's, as long as it keeps `COHERENT_KEPT` free; beyond that the texture is a plain PC +/// one (None). Unicorn Overlord's 2.2 GB of committed textures filled the 512 MB window in its +/// first second, leaving no room for readback heaps. (In the sparse windows instead, XDefiant +/// rendered black – not understood yet.) +/// bytes of the coherent window committed textures leave free: readback / write-back heaps +/// and buffers, XCL's blocks outside its arena pool +const COHERENT_KEPT: usize = 128 << 20; + unsafe fn place_console_texture(this: *mut c_void, desc: usize, state: usize, clear: usize, riid: *const GUID, ppv: *mut *mut c_void) -> Option { let u = crate::gpu::uma::get()?; if ppv.is_null() || !crate::gpu::shader::readable(desc, 0x38) { @@ -1003,7 +1011,11 @@ unsafe fn place_console_texture(this: *mut c_void, desc: usize, state: usize, cl return None; } let align = (align as usize).max(0x1_0000); - let p = u.alloc((size as usize).div_ceil(align) * align, align)? as usize; + let bytes = (size as usize).div_ceil(align) * align; + if u.size().saturating_sub(u.used()) < bytes + COHERENT_KEPT { + return None; + } + let p = u.alloc(bytes, align)? as usize; let hr = super::device_x::create_placed_resource_x(this, p as u64, desc as *const u8, state as u32, clear as *const c_void, riid, ppv); if hr < 0 || (*ppv).is_null() { u.free(p);