diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index ba35ed9..e0eb7a6 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -2441,6 +2441,46 @@ void main(uint3 id : SV_DispatchThreadID) { assert_eq!(crate::texture::uploads(object), 1, "uploaded into the new one"); } + /// Textures brought up in one submission share staging space (parts + /// of one upload buffer): each reads its own bytes. + #[test] + fn textures_uploaded_together_each_read_their_own_bytes() { + let Some(host) = crate::host::host() else { + return; // no host GPU here + }; + let slots = Slots::new(2); + let read = compile_cs(READ); + let outs: Vec<_> = (0..2).map(|_| host.shared_buffer(64 * 64 * 16).unwrap()).collect(); + let pool = crate::watch::tests::pool(2); + let views: Vec = (0..2usize) + .map(|k| { + let address = (pool + k * crate::watch::PAGE) as u64; + // SAFETY: the texture's memory in our pool, 256-byte rows of 64 texels. + unsafe { std::ptr::write_bytes(address as *mut u8, 0x40 * (k as u8 + 1), 64 * 64 * 4) }; + let object = texture_at_address(address, &texture_words(64, 64, 28, 0x100, 0)); + let mut desc = [0u32; 10]; + desc[..6].copy_from_slice(&[28, 4, 0x1688, 0, 0, 1]); + View::Shader(object as usize, Some(desc)) + }) + .collect(); + let lists: Vec<_> = (0..2usize) + .map(|k| { + slots.make(k as u32, &views[k]); + let list = host.new_list().unwrap(); + readable(&list, &used(ResourceType::Srv), &views[k], false); + run(&list, &slots, &read, k as u32, Some(&outs[k].0), (8, 8)); + list.close(); + list + }) + .collect(); + host.execute(&[&lists[0], &lists[1]]); + for (k, (_, data)) in outs.iter().enumerate() { + // SAFETY: inside the mapped output. + let texel = unsafe { (*data as *const [f32; 4]).read() }; + assert!((texel[0] - (0x40 * (k + 1)) as f32 / 255.0).abs() < 1e-6, "texture {k}: {}", texel[0]); + } + } + /// A texture written in a page that holds only its own bytes is uploaded /// without hashing them (a movie frame rewritten every frame), so even /// the same bytes again are uploaded. diff --git a/crates/victory-gpu/src/texture.rs b/crates/victory-gpu/src/texture.rs index 1f35306..f59b08c 100644 --- a/crates/victory-gpu/src/texture.rs +++ b/crates/victory-gpu/src/texture.rs @@ -118,6 +118,11 @@ const KEPT_BYTES: u64 = 64 << 20; /// Called before a submission's uploads: the buffers of the last one are /// free again (the oldest dropped past `KEPT_BYTES`). pub(crate) fn recycle() { + { + let mut c = CHUNKS.lock().unwrap_or_else(|e| e.into_inner()); + (c.current, c.used) = (0, 0); + c.all.truncate(CHUNKS_KEPT); + } let mut b = BUFFERS.lock().unwrap_or_else(|e| e.into_inner()); let b = &mut *b; for e in b.used.drain(..) { @@ -156,6 +161,52 @@ fn staging_buffer(size: u64) -> Option<(ID3D12Resource, *mut u8)> { Some(made) } +/// Staging space carved from large upload buffers, front to back: most +/// textures take a part of one instead of a buffer of their own (making +/// upload buffers was 10 % of the submitting thread in a new shot, run +/// evict5). Rewound at each submission ([`recycle`]): the last one has run. +struct Chunks { + /// (buffer, mapping), [`CHUNK`] bytes each. + all: Vec<(ID3D12Resource, usize)>, + /// The chunk being carved and the bytes taken from it. + current: usize, + used: u64, +} + +// SAFETY: the mappings are persistent mappings of the upload buffers. +unsafe impl Send for Chunks {} + +static CHUNKS: Mutex = Mutex::new(Chunks { all: Vec::new(), current: 0, used: 0 }); + +const CHUNK: u64 = 32 << 20; +/// Chunks kept between submissions (more are made while one needs them). +const CHUNKS_KEPT: usize = 16; + +/// `size` bytes of staging space: a buffer, the offset of the space in it +/// (a multiple of 512, as texture copies need) and its mapping there. +/// Space larger than a chunk is a buffer of its own ([`staging_buffer`]). +fn staging_space(size: u64) -> Option<(ID3D12Resource, u64, *mut u8)> { + if size > CHUNK { + return staging_buffer(size).map(|(b, data)| (b, 0, data)); + } + let mut c = CHUNKS.lock().unwrap_or_else(|e| e.into_inner()); + loop { + if let Some((buffer, data)) = c.all.get(c.current) { + let at = c.used.next_multiple_of(512); + if at + size <= CHUNK { + let made = (buffer.clone(), at, (*data + at as usize) as *mut u8); + c.used = at + size; + return Some(made); + } + (c.current, c.used) = (c.current + 1, 0); + continue; + } + let (buffer, data) = host()?.upload_buffer(CHUNK)?; + crate::host::name(&buffer, || "texture staging chunk".into()); + c.all.push((buffer, data as usize)); + } +} + // SAFETY: the title memory is only read, under `last`'s lock. unsafe impl Send for Staging {} // SAFETY: as above. @@ -324,7 +375,7 @@ pub(crate) fn record_upload( list: &HostList, copy: &Arc, staging: &Staging, - (buffer, subresources): &(ID3D12Resource, Vec), + (buffer, at, subresources): &Upload, ) { const COPY_DEST: u32 = 0x400; const SHADER_RESOURCE: u32 = 0x40 | 0x80; @@ -335,7 +386,7 @@ pub(crate) fn record_upload( } }; move_to(COPY_DEST); - copies(list, ©.texture, staging, buffer, subresources, String::new); + copies(list, ©.texture, staging, (buffer, *at), subresources, String::new); move_to(SHADER_RESOURCE); list.keep(buffer); staging.uploads.fetch_add(1, std::sync::atomic::Ordering::Relaxed); @@ -367,13 +418,15 @@ fn copies( list: &HostList, texture: &ID3D12Resource, staging: &Staging, - buffer: &ID3D12Resource, + (buffer, at): (&ID3D12Resource, u64), subresources: &[u32], name: impl Fn() -> String, ) { for &i in subresources { let i = i as usize; - let footprint = &staging.footprints[i]; + let mut footprint = staging.footprints[i]; + footprint.Offset += at; + let footprint = &footprint; #[cfg(feature = "diagnostics")] { let f = footprint.Footprint; @@ -423,9 +476,9 @@ fn copies( } } -/// What a refresh gives: a staging buffer and the subresources detiled -/// into it. -pub(crate) type Upload = (ID3D12Resource, Vec); +/// What a refresh gives: a staging buffer, where its space starts in it, +/// and the subresources detiled into it. +pub(crate) type Upload = (ID3D12Resource, u64, Vec); /// [`Staging::refresh`] of each of `work`, those whose memory may have /// changed on several threads at once (reading title memory and detiling @@ -474,7 +527,7 @@ impl Staging { /// A staging buffer with the texture memory detiled into it if it /// changed since the last time (and is mapped at all), and the /// subresources that changed (only those are detiled and to be copied). - pub(crate) fn refresh(&self) -> Option<(ID3D12Resource, Vec)> { + pub(crate) fn refresh(&self) -> Option { use std::sync::atomic::Ordering::Relaxed; let started = std::time::Instant::now(); let (read, buffer) = self.refresh_now(); @@ -486,7 +539,7 @@ impl Staging { /// Returns the bytes of title memory read, and the buffer it detiled /// into with the subresources detiled. - fn refresh_now(&self) -> (usize, Option<(ID3D12Resource, Vec)>) { + fn refresh_now(&self) -> (usize, Option) { let all = || (0..self.footprints.len() as u32).collect::>(); let size = self.layout.size as usize; let start = self.memory as usize; @@ -558,7 +611,7 @@ impl Staging { } #[cfg(feature = "diagnostics")] crate::copystat::add(crate::copystat::C::TexWatched, bytes as u64); - return (bytes, buffer.map(|b| (b, fresh))); + return (bytes, buffer.map(|(b, at)| (b, at, fresh))); } if !committed(self.memory, size) { #[cfg(feature = "diagnostics")] @@ -583,14 +636,14 @@ impl Staging { last.hash = buffer.is_some().then_some(hash); #[cfg(feature = "diagnostics")] crate::copystat::add(crate::copystat::C::TexChanged, size as u64); - (size, buffer.map(|b| (b, every))) + (size, buffer.map(|(b, at)| (b, at, every))) } /// Detiles `subresources` of `tiled` (the texture's memory) into a /// staging buffer (`None` if none could be had or a subresource could /// not be read). - fn detile(&self, tiled: &[u8], subresources: &[u32]) -> Option { - let (buffer, data) = staging_buffer(self.size)?; + fn detile(&self, tiled: &[u8], subresources: &[u32]) -> Option<(ID3D12Resource, u64)> { + let (buffer, at, data) = staging_space(self.size)?; #[cfg(feature = "diagnostics")] if tiled.iter().all(|&b| b == 0) { tracing::debug!( @@ -619,7 +672,7 @@ impl Staging { return None; } } - Some(buffer) + Some((buffer, at)) } }