Fix Series S two-plane black screen by reading RetailPlane at 72-byte stride

This commit is contained in:
codec_d committed 2026-11-25 14:55:26 -08:00
1 parent f5847e6248
commit 45e90f4de8
3 files changed
+129 -51

No files matched your search

+61 -25
View File
@@ -8961,7 +8961,18 @@ impl Host {
} }
}; };
} }
let state_u32 = state;
let p = presenter.as_mut()?; 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) { if (p.width, p.height, p.format) != (width, height, format) {
// Presents sent ahead hold back buffers until they have run. // Presents sent ahead hold back buffers until they have run.
settle_now(WHY_SYNC); 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). // SAFETY: our swap chain, list and live source texture(s).
unsafe { unsafe {
let index = p.swap_chain.GetCurrentBackBufferIndex(); let index = p.swap_chain.GetCurrentBackBufferIndex();
@@ -9025,12 +9046,6 @@ impl Host {
let list = &copy.list; let list = &copy.list;
let composed = if let Some((over, over_state, base_fmt, over_fmt)) = planes { let composed = if let Some((over, over_state, base_fmt, over_fmt)) = planes {
copy.keep(over); 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() { if let Some(composer) = p.composer.as_mut() {
let mut pre = Vec::with_capacity(3); let mut pre = Vec::with_capacity(3);
if state != D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE { 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)); pre.push(transition(&back, D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));
list.ResourceBarrier(&pre); list.ResourceBarrier(&pre);
let compose_slot = slot.map_or(0, |i| i + 1); let compose_slot = slot.map_or(0, |i| i + 1);
let _ = composer.record( if let Err(err) = composer.record(
self, self,
compose_slot, compose_slot,
list, list,
[(texture, base_fmt), (over, over_fmt)], [(texture, base_fmt), (over, over_fmt)],
&back, &back,
(width, height), (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); let mut post = Vec::with_capacity(3);
if state != D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE { if state != D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE {
post.push(transition(texture, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, state)); post.push(transition(texture, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, state));
@@ -9071,14 +9103,16 @@ impl Host {
false false
}; };
if !composed { if !composed {
list.ResourceBarrier(&[ let mut pre = Vec::with_capacity(2);
transition(texture, state, D3D12_RESOURCE_STATE_COPY_SOURCE), if state != D3D12_RESOURCE_STATE_COPY_SOURCE {
transition( pre.push(transition(texture, state, D3D12_RESOURCE_STATE_COPY_SOURCE));
&back, }
D3D12_RESOURCE_STATE_PRESENT, pre.push(transition(
D3D12_RESOURCE_STATE_COPY_DEST, &back,
), D3D12_RESOURCE_STATE_PRESENT,
]); D3D12_RESOURCE_STATE_COPY_DEST,
));
list.ResourceBarrier(&pre);
list.CopyResource(&back, texture); list.CopyResource(&back, texture);
} }
#[cfg(feature = "diagnostics")] #[cfg(feature = "diagnostics")]
@@ -9125,14 +9159,16 @@ impl Host {
D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_PRESENT,
)]); )]);
} else { } else {
list.ResourceBarrier(&[ let mut post = Vec::with_capacity(2);
transition(texture, D3D12_RESOURCE_STATE_COPY_SOURCE, state), if state != D3D12_RESOURCE_STATE_COPY_SOURCE {
transition( post.push(transition(texture, D3D12_RESOURCE_STATE_COPY_SOURCE, state));
&back, }
D3D12_RESOURCE_STATE_COPY_DEST, post.push(transition(
D3D12_RESOURCE_STATE_PRESENT, &back,
), D3D12_RESOURCE_STATE_COPY_DEST,
]); D3D12_RESOURCE_STATE_PRESENT,
));
list.ResourceBarrier(&post);
} }
copy.close(); copy.close();
let flown = match slot { let flown = match slot {
+60 -26
View File
@@ -651,6 +651,42 @@ pub(crate) struct Plane {
count: u32, count: u32,
resources: *const This, 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::<RetailPlane>() == 72);
#[cfg(windows)]
fn overlay_resource(parameters: *const Plane) -> Option<This> {
// 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::<RetailPlane>().wrapping_add(1).cast::<u8>();
let mut prefix = [0; size_of::<Plane>()];
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::<Plane>().read_unaligned() };
let mut pointer = [0; size_of::<This>()];
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 /// Submission tagged with the frame it belongs to `(count, lists, frame
/// token, flags)`; frames are paced elsewhere, so it runs as a plain one. /// 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) { 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)] #[cfg(windows)]
if behind::on() && !this.is_null() { if behind::on() && !this.is_null() {
const COPY_SOURCE: u32 = 0x800; const COPY_SOURCE: u32 = 0x800;
let take_plane = |index: usize| -> Option<(usize, std::sync::Arc<crate::resource::HostTexture>, u32)> { let base_res = if planes > 0
if planes as usize <= index || plane_parameters.is_null() {
return None;
}
// SAFETY: the title passes `planes` plane descriptions. // SAFETY: the title passes `planes` plane descriptions.
let plane = unsafe { plane_parameters.add(index).as_ref()? }; && let Some(plane) = unsafe { plane_parameters.as_ref() }
if plane.count == 0 || plane.resources.is_null() { && plane.count > 0
return None; && !plane.resources.is_null()
} {
// SAFETY: the plane lists `count` resources. // 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| { crate::resource::host_copy(resource).map(|copy| {
let state = crate::resource::host_transition(resource, COPY_SOURCE).map_or(COPY_SOURCE, |(_, before)| before); let state = crate::resource::host_transition(resource, COPY_SOURCE).map_or(COPY_SOURCE, |(_, before)| before);
crate::resource::host_transition(resource, state); 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 }; let state = if crate::list::thin::wanted() { u32::MAX } else { state };
(resource as usize, copy, state) (resource as usize, copy, state)
}) })
}; };
let texture = take_plane(0); let texture = base_res.and_then(take_copy);
let overlay = take_plane(1).filter(|o| texture.as_ref().is_none_or(|t| t.0 != o.0)); 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); let resource = texture.as_ref().map_or(0, |t| t.0);
behind::present( behind::present(
this, this,
@@ -975,26 +1018,17 @@ extern "system" fn present(
const COPY_SOURCE: u32 = 0x800; const COPY_SOURCE: u32 = 0x800;
let state = crate::resource::host_transition(resource, COPY_SOURCE) let state = crate::resource::host_transition(resource, COPY_SOURCE)
.map_or(COPY_SOURCE, |(_, before)| before); .map_or(COPY_SOURCE, |(_, before)| before);
let overlay = if planes > 1 let overlay = (planes > 1)
// SAFETY: `planes > 1`, so index 1 is within `plane_parameters`. .then(|| overlay_resource(plane_parameters))
&& let Some(over_plane) = unsafe { plane_parameters.add(1).as_ref() } .flatten()
&& over_plane.count > 0 .filter(|&over_res| over_res != resource)
&& !over_plane.resources.is_null() .and_then(|over_res| {
{
// SAFETY: `over_plane.count > 0`, so `*over_plane.resources` is valid.
let over_res = unsafe { *over_plane.resources };
if over_res != resource {
crate::resource::host_resource(over_res).map(|over_tex| { crate::resource::host_resource(over_res).map(|over_tex| {
let over_state = crate::resource::host_transition(over_res, COPY_SOURCE) let over_state = crate::resource::host_transition(over_res, COPY_SOURCE)
.map_or(COPY_SOURCE, |(_, before)| before); .map_or(COPY_SOURCE, |(_, before)| before);
(over_res, over_tex, over_state) (over_res, over_tex, over_state)
}) })
} else { });
None
}
} else {
None
};
show_planes( show_planes(
texture, texture,
state, state,
+8
View File
@@ -2379,6 +2379,14 @@ pub(crate) fn host_resource<'a>(
Some(unsafe { &*std::ptr::from_ref(&host.texture) }) 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::<usize>()];
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. /// A title texture's host copy, shared, for a list to hold.
#[cfg(windows)] #[cfg(windows)]
pub(crate) fn host_copy(this: This) -> Option<std::sync::Arc<HostTexture>> { pub(crate) fn host_copy(this: This) -> Option<std::sync::Arc<HostTexture>> {