From 3f0ed9b8b35dba7732bd5b3ba8ab7997b9967079 Mon Sep 17 00:00:00 2001 From: jeroenhaentjens <95549851+jeroenhaentjens@users.noreply.github.com> Date: Thu, 26 Nov 2026 20:40:07 +0100 Subject: [PATCH] D3D12.X: report retained texture upload staging capacity Track the upload buffer capacity held by active batches and idle queue uploaders, including the peak across both. This makes the staging owner's contribution to world-load memory pressure measurable without counting texture payload views twice. The measurement is additive to XCL_ACTIVITY and does not change upload behavior. Full GPU-backed workspace tests passed on the Quadro RTX 3000; no retail smoke run is available yet. Assisted-by: OpenAI Codex (GPT-6) --- crates/xcl/src/gpu/texture.rs | 52 +++++++++++++++++++++++++++++++++-- crates/xcl/src/host.rs | 5 ++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/gpu/texture.rs b/crates/xcl/src/gpu/texture.rs index cde3561492..a0c2eaf818 100644 --- a/crates/xcl/src/gpu/texture.rs +++ b/crates/xcl/src/gpu/texture.rs @@ -213,6 +213,7 @@ struct Uploader { value: u64, /// (buffer, its mapped bytes, capacity) staging: Option<(ID3D12Resource, *mut u8, u64)>, + idle: bool, } // D3D12 objects are free-threaded; the mapped pointer is only used by the holder of the uploader @@ -221,6 +222,31 @@ unsafe impl Send for Uploader {} /// queue (raw pointer) → its uploader, while no batch uses it static UPLOADERS: std::sync::Mutex>> = std::sync::Mutex::new(None); +struct StagingBytes { + active: u64, + idle: u64, + peak: u64, +} + +static STAGING_BYTES: std::sync::Mutex = std::sync::Mutex::new(StagingBytes { active: 0, idle: 0, peak: 0 }); + +/// Capacity of the queue upload buffers, including buffers held by running batches. +/// This uploader reserves no unallocated capacity. +pub fn upload_staging_bytes() -> (u64, u64, u64) { + let s = STAGING_BYTES.lock().unwrap(); + (s.active, s.idle, s.peak) +} + +impl Drop for Uploader { + fn drop(&mut self) { + if let Some((_, _, cap)) = &self.staging { + let mut s = STAGING_BYTES.lock().unwrap(); + let bucket = if self.idle { &mut s.idle } else { &mut s.active }; + *bucket -= *cap; + } + } +} + /// the smallest upload buffer made, and the placement alignment of texture data in it const STAGING_MIN: u64 = 4 << 20; const PLACEMENT: u64 = D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT as u64; @@ -241,7 +267,7 @@ impl Uploader { let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).map_err(err)?; list.Close().map_err(err)?; let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).map_err(err)?; - Ok(Uploader { alloc, list, fence, value: 0, staging: None }) + Ok(Uploader { alloc, list, fence, value: 0, staging: None, idle: false }) } /// An upload buffer of at least `bytes`, mapped: the one kept, or a bigger one. @@ -254,11 +280,17 @@ impl Uploader { let cap = staging_capacity(bytes).ok_or("texture upload buffer size overflow")?; // A cached uploader has passed its last copy fence. Return this reference before // acquiring the replacement so the growth path does not keep two buffers itself. - drop(self.staging.take()); + if let Some(old) = self.staging.take() { + STAGING_BYTES.lock().unwrap().active -= old.2; + drop(old); + } let b = buffer(dev, cap)?; let mut p = std::ptr::null_mut(); b.Map(0, None, Some(&mut p)).map_err(err)?; self.staging = Some((b.clone(), p as *mut u8, cap)); + let mut s = STAGING_BYTES.lock().unwrap(); + s.active += cap; + s.peak = s.peak.max(s.active + s.idle); Ok((b, p as *mut u8)) } } @@ -277,6 +309,14 @@ pub unsafe fn upload_batch(dev: &ID3D12Device, queue: &ID3D12CommandQueue, items Some(u) => u, None => Uploader::new(dev)?, }; + if up.idle { + if let Some((_, _, cap)) = &up.staging { + let mut s = STAGING_BYTES.lock().unwrap(); + s.idle -= *cap; + s.active += *cap; + } + up.idle = false; + } // where each texture's texels go in the upload buffer let mut failed = Vec::new(); let mut planned = Vec::with_capacity(items.len()); @@ -317,7 +357,13 @@ pub unsafe fn upload_batch(dev: &ID3D12Device, queue: &ID3D12CommandQueue, items Ok(failed) } -fn put_back(key: usize, up: Uploader) { +fn put_back(key: usize, mut up: Uploader) { + if let Some((_, _, cap)) = &up.staging { + let mut s = STAGING_BYTES.lock().unwrap(); + s.active -= *cap; + s.idle += *cap; + } + up.idle = true; UPLOADERS.lock().unwrap().get_or_insert_with(Default::default).insert(key, up); } diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index a9412c658c..0209a08e34 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -89,6 +89,11 @@ pub fn host_main(boot_path: &Path) -> i32 { mb(pools as u64), mb(pools_mapped as u64) ); + let (active, idle, peak) = crate::gpu::texture::upload_staging_bytes(); + crate::xlog_info!( + "texture upload staging: 0 MB reserved, {} MB active, {} MB idle; {} MB peak held", + mb(active), mb(idle), mb(peak) + ); last = now; } });