diff --git a/README.md b/README.md index 978c73b613..473b7ecb01 100644 --- a/README.md +++ b/README.md @@ -267,6 +267,10 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper. circumventing DRM. - **The game folder is never modified** – writes go to the overlay. - **Switches for how a title runs** (set them in the environment of `xcl run`): + - `XCL_UPLOAD_KEEP_MB`: the most completed texture-upload buffers to keep for reuse, in MiB + (default 512, range 0–4096; `0` releases them all). This limits idle system-memory upload + heaps; buffers still in use and textures in video memory are unaffected. The activity log + reports idle upload memory separately from uploads waiting for completion. - `XCL_SKIP_BINK`: skips the Bink 2 movies of a title that ships a `bink2*.dll` (its logos and intros), by taking them straight to their last frame without decoding. Off when unset or `0`, `off`, `false` or `no`; `all` also skips looping movies; any other value (`1`, `intro` …) diff --git a/crates/xcl/src/hle/d3d12x/texture_upload.rs b/crates/xcl/src/hle/d3d12x/texture_upload.rs index 9a0a0cb2fa..ac9ddc6d25 100644 --- a/crates/xcl/src/hle/d3d12x/texture_upload.rs +++ b/crates/xcl/src/hle/d3d12x/texture_upload.rs @@ -790,7 +790,21 @@ fn capacity(bytes: u64) -> Option { } } -const FREE_BYTES: u64 = 512 << 20; +/// The most bytes of idle upload buffers kept for the next batches (`XCL_UPLOAD_KEEP_MB`, default +/// 512): memory of the system's (upload heaps), the process's commit and RAM while kept. +fn free_bytes() -> u64 { + static V: std::sync::OnceLock = std::sync::OnceLock::new(); + *V.get_or_init(|| keep_bytes_of(std::env::var("XCL_UPLOAD_KEEP_MB").ok().as_deref())) +} + +fn keep_bytes_of(v: Option<&str>) -> u64 { + v.and_then(|v| v.trim().parse::().ok()).map_or(512, |mb| mb.min(4096)) << 20 +} + +/// Bytes of the upload buffers kept idle now. +pub(super) fn idle_bytes() -> u64 { + FREE.lock().unwrap().iter().map(|j| j.bytes).sum() +} /// A job whose buffer takes `bytes` bytes. unsafe fn take(dev: &ID3D12Device, bytes: u64) -> Result { @@ -838,7 +852,7 @@ pub(super) fn give_back(job: Job) { let mut g = FREE.lock().unwrap(); g.push(job); // (the biggest goes first: one burst does not keep its buffers for good) - while g.iter().map(|j| j.bytes).sum::() > FREE_BYTES { + while g.iter().map(|j| j.bytes).sum::() > free_bytes() { let Some(i) = g.iter().enumerate().max_by_key(|(_, j)| j.bytes).map(|(i, _)| i) else { break }; g.swap_remove(i); } @@ -880,6 +894,66 @@ pub(super) unsafe fn submit(dev: &ID3D12Device, queue: &ID3D12CommandQueue, item mod tests { use super::*; + #[test] + fn the_idle_upload_buffers_kept_follow_the_switch() { + assert_eq!(keep_bytes_of(None), 512 << 20, "unset: as before"); + assert_eq!(keep_bytes_of(Some(" 64 ")), 64 << 20); + assert_eq!(keep_bytes_of(Some("0")), 0, "none kept"); + assert_eq!(keep_bytes_of(Some("lots")), 512 << 20); + assert_eq!(keep_bytes_of(Some("100000")), 4096 << 20); + } + + /// Exercise actual buffer allocation, return and reuse in separate processes: neither the + /// process-wide pool nor the cached switch may interfere with another test. WARP suffices; + /// the closed lists are never submitted and no GPU work is queued. + #[test] + fn idle_upload_limit_releases_buffers_and_keeps_reusable_ones() { + const CHILD: &str = "XCL_TEST_IDLE_UPLOAD_LIMIT"; + let Ok(limit) = std::env::var(CHILD) else { + for limit in ["0", "2"] { + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "hle::d3d12x::texture_upload::tests::idle_upload_limit_releases_buffers_and_keeps_reusable_ones", "--nocapture", "--test-threads=1"]) + .env(CHILD, limit) + .env("XCL_UPLOAD_KEEP_MB", limit) + .env("XCL_ADAPTER", "warp") + .output().unwrap(); + print!("{}", String::from_utf8_lossy(&output.stdout)); + eprint!("{}", String::from_utf8_lossy(&output.stderr)); + assert!(output.status.success(), "idle upload limit {limit} MiB: {}", output.status); + } + return; + }; + unsafe { + let Some(dev) = test_device() else { return }; + let mib = 1 << 20; + assert_eq!(idle_bytes(), 0); + let a = take(&dev, mib).unwrap(); + let b = take(&dev, mib).unwrap(); + let active = take(&dev, 4 * mib).unwrap(); + let small_buffers = [a.buf.as_raw(), b.buf.as_raw()]; + *active.ptr = 0x57; + *active.ptr.add(active.bytes as usize - 1) = 0x93; + give_back(a); + give_back(b); + let kept = if limit == "0" { 0 } else { 2 * mib }; + assert_eq!(idle_bytes(), kept); + assert_eq!(*active.ptr, 0x57, "checked-out buffers remain usable"); + assert_eq!(*active.ptr.add(active.bytes as usize - 1), 0x93); + give_back(active); + assert_eq!(idle_bytes(), kept, "the larger idle buffer is released first"); + if kept != 0 { + let reused = take(&dev, mib).unwrap(); + assert!(small_buffers.contains(&reused.buf.as_raw())); + assert_eq!(idle_bytes(), mib); + give_back(reused); + assert_eq!(idle_bytes(), kept); + } + FREE.lock().unwrap().clear(); + assert_eq!(idle_bytes(), 0); + dev.GetDeviceRemovedReason().unwrap(); + } + } + /// The written pages of a texture are compared one by one: a page never compared has changed, /// one that holds what it held has not, the bytes next to the texture on its first and last /// page are not the texture's, and a texture nothing is known of changed as a whole. diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index ceb09e13b8..4848894290 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -2130,6 +2130,13 @@ unsafe impl Send for QueueUploads {} static ON_QUEUE: Mutex> = Mutex::new(BTreeMap::new()); +/// The memory line of the texture upload buffers (`XCL_ACTIVITY`): system memory the process holds +/// for them, idle, and submitted but not yet taken back (`reclaim`: done on the GPU or not). +pub fn upload_line() -> String { + let in_flight: u64 = ON_QUEUE.lock().unwrap().values().map(|q| q.bytes).sum(); + format!("texture upload buffers: {} MB idle, {} MB submitted and not taken back yet", texture_upload::idle_bytes() >> 20, in_flight >> 20) +} + /// upload buffers the GPU has not read yet, before a submit waits for it: a moment past the first, /// in earnest past the second (a title's queue can stand at a wait that another submit ends) const IN_FLIGHT_BYTES: u64 = 512 << 20; diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index 2d4f7ab989..6e25c7622d 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -105,6 +105,7 @@ pub fn host_main(boot_path: &Path) -> i32 { let mut views = crate::hle::d3d12x::view_want::totals(); let mut tile_calls = crate::hle::d3d12x::sparse::tile_calls(); let mut last_tick = std::time::Instant::now(); + let mut tick = 0u32; loop { std::thread::sleep(std::time::Duration::from_secs(2)); let window_ms = last_tick.elapsed().as_millis() as u64; @@ -178,6 +179,10 @@ pub fn host_main(boot_path: &Path) -> i32 { if pages != 0 { crate::xlog_info!("{}", crate::hle::d3d12x::vram::page_owners()); } + tick += 1; + if tick % 10 == 0 { + crate::xlog_info!("{}", crate::hle::d3d12x::title_textures::upload_line()); + } // past the budget Windows moves the process's video memory off the card and // every frame pays for bringing it back; a run at the card's limit is also // where the driver gave up (the guard script ends a run that stays there)