diff --git a/crates/viport-gpu/src/descriptor/host.rs b/crates/viport-gpu/src/descriptor/host.rs index 2492fef..90b2cea 100644 --- a/crates/viport-gpu/src/descriptor/host.rs +++ b/crates/viport-gpu/src/descriptor/host.rs @@ -696,7 +696,10 @@ impl HostHeap { PLACED_WHY[2].fetch_add(1, Relaxed); return false; }; - if !is_placed_texture(&view) && !(lazy_views() && is_placed_buffer_view(&view)) { + if !is_placed_texture(&view) + && !is_unmade_texture(&view) + && !(lazy_views() && is_placed_buffer_view(&view)) + { PLACED_WHY[3].fetch_add(1, Relaxed); return false; } @@ -777,7 +780,7 @@ impl HostHeap { made[index as usize] = Slot { made: 0, parked: key, lazy }; return Some(view); } - let later = !for_draw && is_placed_texture(&view) || lazy; + let later = !for_draw && (is_placed_texture(&view) || is_unmade_texture(&view)) || lazy; made[index as usize] = if later { Slot { made: 0, parked: key, lazy } } else { @@ -1420,6 +1423,13 @@ fn is_placed_texture(view: &View) -> bool { matches!(view, View::Placed(_, resource, _) if resource[0] != 1) } +/// A shader view of a title texture whose host copy is not made yet: made, +/// with the copy, when a shader reads its slot (as placed textures are), +/// since the title writes a view of every texture it streams in. +fn is_unmade_texture(view: &View) -> bool { + matches!(view, View::Shader(resource, _) | View::Unordered(resource, ..) if crate::resource::host_pending(*resource as crate::object::This)) +} + fn is_placed_buffer_view(view: &View) -> bool { matches!(view, View::Placed(false, resource, _) if resource[0] == 1) } diff --git a/crates/viport-gpu/src/resource.rs b/crates/viport-gpu/src/resource.rs index c9c62f0..33019a2 100644 --- a/crates/viport-gpu/src/resource.rs +++ b/crates/viport-gpu/src/resource.rs @@ -813,7 +813,39 @@ struct Resource { /// Textures also live on the host GPU (decision 0007); buffers do not /// yet. #[cfg(windows)] - host: Option>, + host: std::sync::OnceLock>>, + /// What the host texture is made with on first need (state, clear + /// value), when made late (`VIPORT_LAZY_HOST_TEXTURES`): every D3D12 + /// resource is charged to process commit when created, resident or not. + #[cfg(windows)] + make: Option<(u32, Option<[u32; 5]>)>, +} + +#[cfg(windows)] +impl Resource { + /// The host texture, made now if it was left for first need. + fn host(&self) -> Option<&std::sync::Arc> { + self.host + .get_or_init(|| { + let (state, clear) = self.make?; + let clear = clear + .as_ref() + .map_or(std::ptr::null(), |c| c.as_ptr().cast()); + shared_texture(self.data() as u64, &self.desc, state, clear) + }) + .as_ref() + } + + /// The host texture if it was made. + fn host_made(&self) -> Option<&std::sync::Arc> { + self.host.get().and_then(Option::as_ref) + } +} + +#[cfg(windows)] +fn lazy_host_textures() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| crate::switch::on("VIPORT_LAZY_HOST_TEXTURES")) } /// A title texture's host copy and the state it is in. The title's barriers @@ -1036,7 +1068,7 @@ pub(crate) mod life { #[cfg(windows)] pub(crate) fn host_transition(this: This, wanted: u32) -> Option<(*mut c_void, u32)> { use windows::core::Interface; - let host = resource(this).host.as_ref()?; + let host = resource(this).host()?; let before = host .state .swap(wanted, std::sync::atomic::Ordering::Relaxed); @@ -1051,7 +1083,19 @@ pub(crate) fn host_resource<'a>( if this.is_null() { return None; } - resource(this).host.as_deref().map(|h| &h.texture) + resource(this).host().map(|h| &h.texture) +} + +/// Whether a title texture's host copy is left for first need and not made +/// yet (`VIPORT_LAZY_HOST_TEXTURES`): its shader views in heaps wait for a +/// shader to read them. +#[cfg(windows)] +pub(crate) fn host_pending(this: This) -> bool { + if this.is_null() { + return false; + } + let r = resource(this); + r.make.is_some() && r.host.get().is_none() } /// A title texture's host copy, shared, for a list to hold. @@ -1060,7 +1104,7 @@ pub(crate) fn host_copy(this: This) -> Option> { if this.is_null() { return None; } - resource(this).host.clone() + resource(this).host().cloned() } /// The host description of a title texture: the same, without the @@ -1250,7 +1294,7 @@ fn takes(t: &HostTexture, d: &ResourceDesc) -> bool { #[cfg(windows)] fn current(this: This) -> bool { let r = resource(this); - let (Some(h), Some(k)) = (&r.host, memory_key(r.data() as u64, &r.desc)) else { + let (Some(h), Some(k)) = (r.host_made(), memory_key(r.data() as u64, &r.desc)) else { return true; }; let map = MEMORY.lock().unwrap_or_else(|e| e.into_inner()); @@ -1464,17 +1508,26 @@ fn hand_out_resource( heap_flags, backing, #[cfg(windows)] - host: None, + host: std::sync::OnceLock::new(), + #[cfg(windows)] + make: None, }); #[cfg(windows)] - { - resource.host = shared_texture(resource.data() as u64, &desc, state, clear); + if desc.dimension != BUFFER && lazy_host_textures() && host_desc(&desc).is_some() { + // SAFETY: the title passes a whole clear value or null. + let clear = + (!clear.is_null()).then(|| unsafe { clear.cast::<[u32; 5]>().read_unaligned() }); + resource.make = Some((state, clear)); + } else { + let _ = resource + .host + .set(shared_texture(resource.data() as u64, &desc, state, clear)); } #[cfg(windows)] if desc.dimension == BUFFER { let at = std::ptr::from_ref::(&resource) as usize; crate::shadow::register(at, resource.data() as u64, desc.width); - } else if resource.host.is_some() { + } else if resource.make.is_some() || resource.host_made().is_some() { let at = std::ptr::from_ref::(&resource) as usize; let mut map = TEXTURES_AT.lock().unwrap_or_else(|e| e.into_inner()); map.entry(resource.data() as u64).or_default().push(at); @@ -1519,7 +1572,7 @@ pub(crate) fn alias_source(placed: This) -> Option { let a = resource(r); a.desc.flags & WRITTEN != 0 && bytes(&a.desc) == Some(want) - && matches!((&a.host, &b.host), (Some(x), Some(y)) if !std::sync::Arc::ptr_eq(x, y)) + && matches!((a.host_made(), b.host()), (Some(x), Some(y)) if !std::sync::Arc::ptr_eq(x, y)) }) } @@ -1528,8 +1581,7 @@ impl Drop for Resource { fn drop(&mut self) { #[cfg(feature = "diagnostics")] if self - .host - .as_ref() + .host_made() .is_some_and(|h| std::sync::Arc::strong_count(h) > 1) { // Before lists held their textures, this freed a texture that @@ -1545,7 +1597,7 @@ impl Drop for Resource { } } #[cfg(feature = "diagnostics")] - if let Some(h) = &self.host { + if let Some(h) = self.host_made() { use windows::core::Interface; life::note(h.texture.as_raw() as usize, life::RELEASED, 0, life::holders(&h.texture)); } @@ -1770,7 +1822,7 @@ pub(crate) fn texture_at(address: u64, desc: &[u32; 14]) -> Option { return None; } map.insert((address, *desc), out as usize); - resource(out).host.is_some().then_some(out) + resource(out).host().is_some().then_some(out) } pub(crate) extern "system" fn create_placed(