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

+60 -26
View File
@@ -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::<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
/// 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<crate::resource::HostTexture>, 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,