diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index abf6bc9..6bedd50 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -8961,7 +8961,18 @@ impl Host { } }; } + let state_u32 = state; let p = presenter.as_mut()?; + if planes.is_some() && p.composer.is_none() { + match crate::compose::Composer::new(self, format) { + Ok(c) => p.composer = Some(c), + Err(e) => { + tracing::warn!(error = %e, "two-plane composer not made; showing base plane only"); + drop(presenter); + return self.present_planes(texture, state_u32, None, frame); + } + } + } if (p.width, p.height, p.format) != (width, height, format) { // Presents sent ahead hold back buffers until they have run. settle_now(WHY_SYNC); @@ -8991,7 +9002,17 @@ impl Host { } } } - let state = D3D12_RESOURCE_STATES(state as i32); + if planes.is_some() && p.composer.is_none() { + match crate::compose::Composer::new(self, format) { + Ok(c) => p.composer = Some(c), + Err(e) => { + tracing::warn!(error = %e, "two-plane composer not made after resize; showing base plane only"); + drop(presenter); + return self.present_planes(texture, state_u32, None, frame); + } + } + } + let state = D3D12_RESOURCE_STATES(state_u32 as i32); // SAFETY: our swap chain, list and live source texture(s). unsafe { let index = p.swap_chain.GetCurrentBackBufferIndex(); @@ -9025,12 +9046,6 @@ impl Host { let list = ©.list; let composed = if let Some((over, over_state, base_fmt, over_fmt)) = planes { copy.keep(over); - if p.composer.is_none() { - match crate::compose::Composer::new(self, format) { - Ok(c) => p.composer = Some(c), - Err(e) => tracing::warn!(error = %e, "two-plane composer not made; showing base plane only"), - } - } if let Some(composer) = p.composer.as_mut() { let mut pre = Vec::with_capacity(3); if state != D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE { @@ -9042,14 +9057,31 @@ impl Host { pre.push(transition(&back, D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET)); list.ResourceBarrier(&pre); let compose_slot = slot.map_or(0, |i| i + 1); - let _ = composer.record( + if let Err(err) = composer.record( self, compose_slot, list, [(texture, base_fmt), (over, over_fmt)], &back, (width, height), - ); + ) { + copy.close(); + drop(back); + drop(presenter); + tracing::warn!(%err, "could not record display composite; presenting the base plane"); + return self.present_planes(texture, state_u32, None, frame); + } + static ONCE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + if !ONCE.swap(true, Ordering::Relaxed) { + tracing::info!( + frame, + width, + height, + base_format = base_fmt.0, + overlay_format = over_fmt.0, + "display output: compositing two planes (overlay active)" + ); + } let mut post = Vec::with_capacity(3); if state != D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE { post.push(transition(texture, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, state)); @@ -9071,14 +9103,16 @@ impl Host { false }; if !composed { - list.ResourceBarrier(&[ - transition(texture, state, D3D12_RESOURCE_STATE_COPY_SOURCE), - transition( - &back, - D3D12_RESOURCE_STATE_PRESENT, - D3D12_RESOURCE_STATE_COPY_DEST, - ), - ]); + let mut pre = Vec::with_capacity(2); + if state != D3D12_RESOURCE_STATE_COPY_SOURCE { + pre.push(transition(texture, state, D3D12_RESOURCE_STATE_COPY_SOURCE)); + } + pre.push(transition( + &back, + D3D12_RESOURCE_STATE_PRESENT, + D3D12_RESOURCE_STATE_COPY_DEST, + )); + list.ResourceBarrier(&pre); list.CopyResource(&back, texture); } #[cfg(feature = "diagnostics")] @@ -9125,14 +9159,16 @@ impl Host { D3D12_RESOURCE_STATE_PRESENT, )]); } else { - list.ResourceBarrier(&[ - transition(texture, D3D12_RESOURCE_STATE_COPY_SOURCE, state), - transition( - &back, - D3D12_RESOURCE_STATE_COPY_DEST, - D3D12_RESOURCE_STATE_PRESENT, - ), - ]); + let mut post = Vec::with_capacity(2); + if state != D3D12_RESOURCE_STATE_COPY_SOURCE { + post.push(transition(texture, D3D12_RESOURCE_STATE_COPY_SOURCE, state)); + } + post.push(transition( + &back, + D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_STATE_PRESENT, + )); + list.ResourceBarrier(&post); } copy.close(); let flown = match slot { diff --git a/crates/victory-gpu/src/queue.rs b/crates/victory-gpu/src/queue.rs index 17b0a36..2a908ec 100644 --- a/crates/victory-gpu/src/queue.rs +++ b/crates/victory-gpu/src/queue.rs @@ -651,6 +651,42 @@ pub(crate) struct Plane { count: u32, resources: *const This, } +/// Retail PresentX adds source-rectangle and destination pointers, a color +/// space and seven option words after the common prefix. Older callers pass +/// only the prefix for single-plane presents. +#[cfg(windows)] +#[repr(C)] +struct RetailPlane { + prefix: Plane, + source_rects: *const (), + destination: *const (), + color_space: u32, + options: [u32; 7], +} +#[cfg(windows)] +const _: () = assert!(size_of::() == 72); + +#[cfg(windows)] +fn overlay_resource(parameters: *const Plane) -> Option { + // Read only the second prefix at the 72-byte retail PresentX stride. A + // caller using a different layout can still present its base plane safely. + let second = parameters.cast::().wrapping_add(1).cast::(); + let mut prefix = [0; size_of::()]; + if !crate::host::read_into(second, &mut prefix) { + return None; + } + // SAFETY: the prefix has the C layout above and its bytes were copied. + let plane = unsafe { prefix.as_ptr().cast::().read_unaligned() }; + let mut pointer = [0; size_of::()]; + if plane.count == 0 + || plane.resources.is_null() + || !crate::host::read_into(plane.resources.cast(), &mut pointer) + { + return None; + } + let resource = usize::from_ne_bytes(pointer) as This; + crate::resource::is_resource(resource).then_some(resource) +} /// Submission tagged with the frame it belongs to `(count, lists, frame /// token, flags)`; frames are paced elsewhere, so it runs as a plain one. extern "system" fn execute_x(this: This, count: u32, lists: *const This, _token: u64, _flags: u32) { @@ -929,26 +965,33 @@ extern "system" fn present( #[cfg(windows)] if behind::on() && !this.is_null() { const COPY_SOURCE: u32 = 0x800; - let take_plane = |index: usize| -> Option<(usize, std::sync::Arc, u32)> { - if planes as usize <= index || plane_parameters.is_null() { - return None; - } + let base_res = if planes > 0 // SAFETY: the title passes `planes` plane descriptions. - let plane = unsafe { plane_parameters.add(index).as_ref()? }; - if plane.count == 0 || plane.resources.is_null() { - return None; - } + && let Some(plane) = unsafe { plane_parameters.as_ref() } + && plane.count > 0 + && !plane.resources.is_null() + { // SAFETY: the plane lists `count` resources. - let resource = unsafe { *plane.resources }; + Some(unsafe { *plane.resources }) + } else { + None + }; + let take_copy = |resource: This| { crate::resource::host_copy(resource).map(|copy| { let state = crate::resource::host_transition(resource, COPY_SOURCE).map_or(COPY_SOURCE, |(_, before)| before); crate::resource::host_transition(resource, state); + // (Lists not translated yet leave it in no state yet: the + // one it is in when its turn in the line comes.) let state = if crate::list::thin::wanted() { u32::MAX } else { state }; (resource as usize, copy, state) }) }; - let texture = take_plane(0); - let overlay = take_plane(1).filter(|o| texture.as_ref().is_none_or(|t| t.0 != o.0)); + let texture = base_res.and_then(take_copy); + let overlay = (planes > 1 && !plane_parameters.is_null()) + .then(|| overlay_resource(plane_parameters)) + .flatten() + .filter(|&over_res| base_res != Some(over_res)) + .and_then(take_copy); let resource = texture.as_ref().map_or(0, |t| t.0); behind::present( this, @@ -975,26 +1018,17 @@ extern "system" fn present( const COPY_SOURCE: u32 = 0x800; let state = crate::resource::host_transition(resource, COPY_SOURCE) .map_or(COPY_SOURCE, |(_, before)| before); - let overlay = if planes > 1 - // SAFETY: `planes > 1`, so index 1 is within `plane_parameters`. - && let Some(over_plane) = unsafe { plane_parameters.add(1).as_ref() } - && over_plane.count > 0 - && !over_plane.resources.is_null() - { - // SAFETY: `over_plane.count > 0`, so `*over_plane.resources` is valid. - let over_res = unsafe { *over_plane.resources }; - if over_res != resource { + let overlay = (planes > 1) + .then(|| overlay_resource(plane_parameters)) + .flatten() + .filter(|&over_res| over_res != resource) + .and_then(|over_res| { crate::resource::host_resource(over_res).map(|over_tex| { let over_state = crate::resource::host_transition(over_res, COPY_SOURCE) .map_or(COPY_SOURCE, |(_, before)| before); (over_res, over_tex, over_state) }) - } else { - None - } - } else { - None - }; + }); show_planes( texture, state, diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs index 437e3ca..8531674 100644 --- a/crates/victory-gpu/src/resource.rs +++ b/crates/victory-gpu/src/resource.rs @@ -2379,6 +2379,14 @@ pub(crate) fn host_resource<'a>( Some(unsafe { &*std::ptr::from_ref(&host.texture) }) } +/// Whether `this` is a valid title resource created by this runtime. +#[cfg(windows)] +pub(crate) fn is_resource(this: This) -> bool { + let mut table = [0; size_of::()]; + crate::host::read_into(this.cast(), &mut table) + && usize::from_ne_bytes(table) == RESOURCE_FUNCTIONS.0.as_ptr() as usize +} + /// A title texture's host copy, shared, for a list to hold. #[cfg(windows)] pub(crate) fn host_copy(this: This) -> Option> {