diff --git a/apps/victory/src/native_1440p.rs b/apps/victory/src/native_1440p.rs index 1fa28e292..91f5537fa 100644 --- a/apps/victory/src/native_1440p.rs +++ b/apps/victory/src/native_1440p.rs @@ -2,10 +2,20 @@ //! 1.0.2.1): the title presents at 3840x2160 on Series X, its scene drawn at //! 2560x1440 and scaled up by its own temporal pass. With 1440p asked for //! (`VICTORY_NATIVE_1440P=1`, or `VICTORY_RENDER_1080P=1` for a 1920x1080 -//! scene scaled to 1440p), the image's SDR display mode, the scanout -//! reservation, the temporal scene scale and the Settings viewport load are -//! patched before it is mapped: the title then makes a 1440p output, and no -//! 4K pass runs. +//! scene scaled to 1440p), the title's own settings get what Series S's +//! preset gives them, with a 2560x1440 output, and the UI memory pool and the +//! temporal scene scale are patched before the image is mapped. The SDR +//! display mode stays at stock 3840x2160, as Series S keeps it. The title then +//! makes a 1440p output, lays its UI and pause map out on the 3840x2160 size +//! they are authored for, scales them onto the 2560x1440 targets, and runs no +//! 4K pass. +//! +//! Series S's preset sets three video settings: the legacy UI flag (1), the +//! legacy UI size (3840x2160) and the output size (1920x1080). Every reader +//! (the UI layout, the title's screen size `0x92D7924`, the pause map's +//! target size) takes what it needs from them. Series X's preset has the flag +//! at 0 and no legacy size, so with a 1440p output its menus and HUD were laid +//! out on a 2560x1440 stage with their 4K-sized content, 1.5 times too large. //! //! Ported from DexNom's fork (DexNom/VIctory, `apps/victory/src/native_1440p.rs`, //! GPL-3.0-or-later), its native 1440p part only. Every field is checked @@ -28,57 +38,120 @@ pub(super) const WIDTH: u32 = 2560; pub(super) const HEIGHT: u32 = 1440; const SDR_MODE: u32 = 0x0615_c100; const STOCK_SDR_MODE: &str = "abfb5438d8153b6a0b8d08f33f4f0066e5383925b55aacc5f669131f3f51d4cf"; -const SCANOUT_RESERVATION: u32 = 0x0247_ad4a; +const LEGACY_UI_WIDTH: u32 = 3840; +const LEGACY_UI_HEIGHT: u32 = 2160; +/// The UI memory pool's size in the UI setup at `0x247AD10`: `mov ecx, +/// 0x4000000`, the Series X value (Series S: `0x5200000`, picked by +/// `cmovne` on the title's Series S check). +const UI_POOL: u32 = 0x0247_ad4a; const TEMPORAL_SCALE: u32 = 0x028f_1e7e; -const UI_VIEWPORT_LOAD: u32 = 0x0580_48d4; +/// The settings collection's copy (`0x1D70750`), which every settings store +/// goes through (the init's apply, the runtime updates, the backups): its +/// last store of the video block and the `vzeroupper` after it (11 bytes). +/// The video settings start at collection `+0x450`, so the legacy UI flag is +/// at `+0x638` (settings `+0x1E8`, byte), the legacy UI size at +/// `+0x63C/+0x640` (`+0x1EC/+0x1F0`) and the output size at `+0x654/+0x658` +/// (`+0x204/+0x208`). +const SETTINGS_COPY: u32 = 0x01d7_0b98; +const STOCK_SETTINGS_COPY: &str = "9d8829e6720f7624fd7004b8905ccfd6e1d9c6610ec186100f8350e091344038"; +const SETTINGS_COPY_LEN: usize = 11; +/// Padding (`int3`, 60 bytes from `0x55FF284`) between two functions, inside +/// the mapped code. +const SETTINGS_CAVE: u32 = 0x055f_f285; +const SETTINGS_CAVE_LEN: usize = 59; -/// Patches `image` for a 2560x1440 output; the scene is drawn at 2560x1440, -/// or at 1920x1080 with `render_1080p`. Returns the scene's size. -pub(super) fn prepare(image: &mut PreparedImage, render_1080p: bool) -> Result<(u32, u32)> { - ensure!( - image.original_hash.to_hex().as_str() == RETAIL_BUILD, - "1440p output is only known for the retail build {RETAIL_BUILD}" - ); +/// Our code for the cave: the copy's store (as `vmovdqu`, the same store), +/// then the three settings as Series S has them, with a 2560x1440 output, and +/// the jump back after the patched site. +fn settings_cave_bytes() -> Result> { + let pair = |w: u32, h: u32| (u64::from(h) << 32 | u64::from(w)).to_le_bytes(); + let mut cave = Vec::with_capacity(SETTINGS_CAVE_LEN); + cave.extend_from_slice(&[0xc5, 0xfe, 0x7f, 0x86, 0x38, 0x06, 0, 0]); // vmovdqu [rsi+0x638], ymm0 + cave.extend_from_slice(&[0xc6, 0x86, 0x38, 0x06, 0, 0, 0x01]); // mov byte [rsi+0x638], 1 + cave.extend_from_slice(&[0x48, 0xb8]); // mov rax, legacy size + cave.extend_from_slice(&pair(LEGACY_UI_WIDTH, LEGACY_UI_HEIGHT)); + cave.extend_from_slice(&[0x48, 0x89, 0x86, 0x3c, 0x06, 0, 0]); // mov [rsi+0x63c], rax + cave.extend_from_slice(&[0x48, 0xb8]); // mov rax, output size + cave.extend_from_slice(&pair(WIDTH, HEIGHT)); + cave.extend_from_slice(&[0x48, 0x89, 0x86, 0x54, 0x06, 0, 0]); // mov [rsi+0x654], rax + cave.extend_from_slice(&[0xc5, 0xf8, 0x77]); // vzeroupper + let back = i64::from(SETTINGS_COPY) + SETTINGS_COPY_LEN as i64 - (i64::from(SETTINGS_CAVE) + cave.len() as i64 + 5); + cave.push(0xe9); // jmp back + cave.extend_from_slice(&i32::try_from(back)?.to_le_bytes()); + ensure!(cave.len() <= SETTINGS_CAVE_LEN, "settings cave code too long"); + Ok(cave) +} - let mode = field::<16>(image, SDR_MODE)?; - ensure!(same(&mode[..], STOCK_SDR_MODE), "unexpected Series X SDR mode"); - mode[..4].copy_from_slice(&WIDTH.to_le_bytes()); - mode[4..8].copy_from_slice(&HEIGHT.to_le_bytes()); +/// `jmp cave` and NOPs over the copy's last store. +fn settings_copy_bytes() -> Result<[u8; SETTINGS_COPY_LEN]> { + let mut site = [0x90; SETTINGS_COPY_LEN]; + site[0] = 0xe9; + let to = i64::from(SETTINGS_CAVE) - (i64::from(SETTINGS_COPY) + 5); + site[1..5].copy_from_slice(&i32::try_from(to)?.to_le_bytes()); + Ok(site) +} - // Two 64 KiB-aligned, tiled RGBA8 scanout buffers. The reservation is - // rounded to 2 MiB, matching the title's allocation check. +/// Bytes of one 64 KiB-aligned, 64 KiB-tiled RGBA8 UI surface. +fn ui_surface_bytes(width: u32, height: u32) -> Result { let texture = Texture::new(&TextureDesc { dimension: Dimension::Tex2d, - width: WIDTH, - height: HEIGHT, + width, + height, depth_or_array: 1, mips: 1, format: 28, samples: 1, swizzle: Swizzle::SW_64KB_D_X, }) - .map_err(|error| anyhow::anyhow!("1440p scanout layout: {error:?}"))?; - let pool_size = - u32::try_from((texture.size.next_multiple_of(64 * 1024) * 2).next_multiple_of(2 << 20))?; - let reservation = field::<5>(image, SCANOUT_RESERVATION)?; - ensure!(*reservation == [0xb9, 0, 0, 0, 4], "unexpected Series X scanout reservation"); - reservation[1..].copy_from_slice(&pool_size.to_le_bytes()); + .map_err(|error| anyhow::anyhow!("UI surface layout ({width}x{height}): {error:?}"))?; + Ok(texture.size.next_multiple_of(64 * 1024)) +} + +/// Patches `image` for a 2560x1440 output; the scene is drawn at 2560x1440, +/// or at 1920x1080 with `render_1080p`. Returns the scene's size. +pub(super) fn prepare(image: &mut PreparedImage, render_1080p: bool) -> Result<(u32, u32)> { + ensure!( + image.original_hash.to_hex().as_str() == RETAIL_BUILD, + "1440p output is only known for the retail build {RETAIL_BUILD}" + ); + + // The SDR mode stays at stock 3840x2160: the pause map's targets and the + // title's screen size follow the layout size, which stays 4K as on + // Series S. Only checked here. + ensure!(same(&field::<16>(image, SDR_MODE)?[..], STOCK_SDR_MODE), "unexpected Series X SDR mode"); + + // With the legacy UI flag set, the UI setup makes two 3840x2160 legacy + // UI surfaces and two 2560x1440 UI targets in one pool, and asserts that + // no more than 0x1FFFFF of it is left over (`0x247AF25`). Rounded to + // 2 MiB: 0x5E00000 (94 MiB). The stock Series X 64 MiB had no room for + // the legacy surfaces (the title's allocator asserted, `int3` at + // `0x56A2AFD`). + let pool_size = u32::try_from( + (ui_surface_bytes(LEGACY_UI_WIDTH, LEGACY_UI_HEIGHT)? * 2 + ui_surface_bytes(WIDTH, HEIGHT)? * 2) + .next_multiple_of(2 << 20), + )?; + let pool = field::<5>(image, UI_POOL)?; + ensure!(*pool == [0xb9, 0, 0, 0, 4], "unexpected Series X UI pool size"); + pool[1..].copy_from_slice(&pool_size.to_le_bytes()); + + // The output settings as Series S's preset has them (legacy UI flag 1, + // legacy UI size 3840x2160), with a 2560x1440 output, written into the + // title's settings each time they are stored. The rest of the preset + // stays Series X. + let site = field::(image, SETTINGS_COPY)?; + ensure!(same(&site[..], STOCK_SETTINGS_COPY), "unexpected settings copy"); + let cave = field::(image, SETTINGS_CAVE)?; + ensure!(cave.iter().all(|b| *b == 0xcc), "settings cave is not free padding"); + let code = settings_cave_bytes()?; + cave[..code.len()].copy_from_slice(&code); + *field::(image, SETTINGS_COPY)? = settings_copy_bytes()?; let scale = field::<4>(image, TEMPORAL_SCALE)?; ensure!(*scale == (2.0f32 / 3.0).to_le_bytes(), "unexpected Series X temporal scene scale"); let (render_width, render_height) = if render_1080p { (1920, 1080) } else { (WIDTH, HEIGHT) }; *scale = (render_width as f32 / WIDTH as f32).to_le_bytes(); - - // Settings lays out its preview panels with a viewport setter of its - // own that loads the legacy UI size (3840x2160) even when a 1440p UI - // surface was made: it reads the selected SDR mode instead. - let viewport = field::<8>(image, UI_VIEWPORT_LOAD)?; - ensure!( - *viewport == [0xc5, 0xfb, 0x10, 0x0d, 0x10, 0xf0, 0x13, 0x05], - "unexpected Settings viewport size load" - ); - let displacement = (SDR_MODE as i32) - (UI_VIEWPORT_LOAD as i32 + 8); - viewport[4..].copy_from_slice(&displacement.to_le_bytes()); + // The Settings preview panels' viewport (`0x58048D4`) loads the legacy + // UI size, which is right with the legacy flag set: left stock. Ok((render_width, render_height)) } @@ -171,14 +244,19 @@ mod tests { for (render_1080p, scene) in [(false, (2560, 1440)), (true, (1920, 1080))] { let mut image = victory_loader::prepare(&bytes).unwrap(); assert_eq!(prepare(&mut image, render_1080p).unwrap(), scene); - let mode = *field::<16>(&mut image, SDR_MODE).unwrap(); - assert_eq!(mode[..4], WIDTH.to_le_bytes()); - assert_eq!(mode[4..8], HEIGHT.to_le_bytes()); + assert!(same(&field::<16>(&mut image, SDR_MODE).unwrap()[..], STOCK_SDR_MODE)); let scale = f32::from_le_bytes(*field::<4>(&mut image, TEMPORAL_SCALE).unwrap()); assert_eq!((WIDTH as f32 * scale) as u32, scene.0); - let load = *field::<8>(&mut image, UI_VIEWPORT_LOAD).unwrap(); - let disp = i32::from_le_bytes(load[4..].try_into().unwrap()); - assert_eq!(UI_VIEWPORT_LOAD.wrapping_add(8).wrapping_add_signed(disp), SDR_MODE); + // The UI pool holds two legacy surfaces and two 1440p targets. + assert_eq!(*field::<5>(&mut image, UI_POOL).unwrap(), [0xb9, 0x00, 0x00, 0xe0, 0x05]); + // The settings copy jumps to the cave, whose jump comes back + // right after the patched site. + assert_eq!(*field::(&mut image, SETTINGS_COPY).unwrap(), settings_copy_bytes().unwrap()); + let code = settings_cave_bytes().unwrap(); + let cave = *field::(&mut image, SETTINGS_CAVE).unwrap(); + assert_eq!(cave[..code.len()], code[..]); + let back = i32::from_le_bytes(code[code.len() - 4..].try_into().unwrap()); + assert_eq!((SETTINGS_CAVE + code.len() as u32).wrapping_add_signed(back), SETTINGS_COPY + SETTINGS_COPY_LEN as u32); // Patched once, the image is no longer the stock one: refused. assert!(prepare(&mut image, render_1080p).is_err()); } diff --git a/apps/victory/src/run.rs b/apps/victory/src/run.rs index ad6ac5908..be0ef871c 100644 --- a/apps/victory/src/run.rs +++ b/apps/victory/src/run.rs @@ -172,10 +172,11 @@ pub fn run( } let (w, h) = native_1440p::prepare(&mut prepared, render_1080p && upscaler.is_none()) .map_err(|e| if victory_game::identify(&hash).is_some() { e } else { e.context(changed_copy(exe)) })?; - // SAFETY: nothing else runs yet; the title and the display mode list - // read these later. + // The display mode reported to the title stays the 3840x2160 default + // (`dxgi::display_size`), as on Series S: the output is 1440p from the + // title's own settings (`native_1440p::prepare`). + // SAFETY: nothing else runs yet; the title reads this later. unsafe { - std::env::set_var("VICTORY_DISPLAY_MODE", format!("{}x{}", native_1440p::WIDTH, native_1440p::HEIGHT)); std::env::set_var("VICTORY_NATIVE_1440P", "1"); } match upscaler.map(|mode| (mode, native_1440p::prepare_upscaler(&mut prepared, mode))) {