diff --git a/d3d12/README.md b/d3d12/README.md index 7f149eae5e..7d8f2ae254 100644 --- a/d3d12/README.md +++ b/d3d12/README.md @@ -262,7 +262,7 @@ below. Known faults left: the common room by its sunlit windows still goes beige and hazy (sky light in the fog); smearing when the camera moves fast at half scale (the title's own temporal upscaler); two orange glows on some corridor walls. -Render scale 1.0 on Series S has not been tested. +Render scale 1.0 on Series S runs since the loader gives two of its budgets the other consoles' sizes (below). Also in, off by default: @@ -498,7 +498,7 @@ subtitles stay sharp either way. Series S outputs 1920x1080 and Series X | Script | Console | World drawn at | Render scale | Tested on the RTX 5070 | |---|---|---|---|---| -| `play-S-1080p.ps1` | Series S | 1920x1080 | 1.0 | **crashes at start** (the title stops at a breakpoint; also reported on an RTX 2080) | +| `play-S-1080p.ps1` | Series S | 1920x1080 | 1.0 | not yet; it crashed at start until the loader gave Series S larger budgets (below), and runs since on an RTX 3060 (11-22 fps in the world) | | `play-S-720p.ps1` | Series S | 1280x720 | 0.667 | not yet | | `play-S-540p.ps1` | Series S | 960x540 | 0.5 | yes: the default, about 18-22 fps | | `play-S-480p.ps1` | Series S | 853x480 | 0.444 | not yet | @@ -524,9 +524,20 @@ What to expect: - **Lower is softer and smears more.** The title's temporal upscaler rebuilds detail from earlier frames. At low resolutions and low frame rates, fast camera moves (driving, turning quickly) leave visible trails. -- **Series S at 1080p (render scale 1.0) does not work yet.** The title stops - at a breakpoint as soon as it starts, on a 12 GB card too, so it is not video - memory. Use 720p or lower on Series S. +- **Series S above 720p (render scale above 2/3) needs larger budgets.** The + title sizes two of its memory budgets per console, for Series S' own 720p: + its render-target pool (1277 MiB, 2199 on the other consoles) and a heap of + 64 KiB pages (297 MiB, 710 on the others). At 1080p the first allocation that + does not fit stops the title at its own breakpoint as soon as it starts, on a + 12 GB card too: it is not video memory. Whenever Series S can render above + its own 2/3 by more than a thousandth, so that 2/3 itself (0.6667) keeps the + console's budgets (a held scale above it with `-Resolution`, or the title's own + dynamic resolution without a held scale, that is its own upscaler as `-Lowest` + uses it or a lowest scale from F1, since that one rises up to 1.0 on Series S + whatever the lowest), the loader gives both the other consoles' + sizes, both or neither + (`leonida-loader/src/hooks.rs`, `SERIES_S_LARGER`), and 1080p runs (RTX 3060, + 2026-12-01: five minutes into the world, 1920x1080 rendered, 11-22 fps). - Series S cannot go above 1080p: its output is 1080p, and the script says so and stops if asked for more. - **16 GB PCs: turned on for you, best with resizable BAR enabled.** diff --git a/d3d12/crates/leonida-loader/src/hooks.rs b/d3d12/crates/leonida-loader/src/hooks.rs index 4970ff9cfc..bb52eb5e90 100644 --- a/d3d12/crates/leonida-loader/src/hooks.rs +++ b/d3d12/crates/leonida-loader/src/hooks.rs @@ -173,6 +173,60 @@ impl OwnedPatch { } } +/// Leonida: the title's memory pools come from a table of nine sizes per console (`.rdata`, the row at 0x6186250 for +/// Series S and the one 0x50 on for the other consoles, copied at start by the function at 0x1e1ced0). Each row is +/// 8 bytes, then its nine sizes of 8 bytes each from +8 (the copy reads `[row + 0x8]`), so the third size, at +/// 0x6186268 for Series S, is the pool whose allocator (`.data` 0x71c2808, made at 0x1e1d418) the render targets come from: 1277 MiB on +/// Series S, 2199 MiB on the others. A Series S render scale above its own 2/3 does not fit it: at 1.0 an +/// allocation of 1 MiB (64 KiB aligned) fails at the first pipelines and the allocator, which treats a failure as +/// fatal, stops the title at its breakpoint (0x2219650, called from 0x4fac40e). Given the other consoles' size, it +/// fits. With [`SERIES_S_TRANSIENT_HEAP`] too, Series S at 1080p ran five minutes into the world (RTX 3060, +/// 2026-12-01), where before both it stopped as soon as it started. +const SERIES_S_TARGET_POOL: Patch<'static> = Patch { + what: "Series S: the render-target pool as large as the other consoles'", + rva: 0x6186268, + expect: &[0x00, 0x00, 0xD0, 0x4F, 0x00, 0x00, 0x00, 0x00], + new: &[0x00, 0x00, 0x70, 0x89, 0x00, 0x00, 0x00, 0x00], +}; + +/// The second Series S budget such a scale outgrows: a heap counted in 64 KiB pages, 0x1290 (297 MiB) on Series S +/// and 0x2c60 (710 MiB) on the others (`mov eax, 0x1290` then a `cmovne` at 0x1d488ac, in the function at 0x1d488a0 +/// that the start at 0x5b056a0 keeps in `.data` 0x75eaeb8). The check at 0x2473e83 compares a resource's size with it +/// and stops the title at the breakpoint at 0x2473e88 when it is larger: at scale 1.0 a size of 432 MiB, once the +/// pool above was large enough. Given the other consoles' count, it fits. +const SERIES_S_TRANSIENT_HEAP: Patch<'static> = Patch { + what: "Series S: the 64 KiB-page heap as large as the other consoles'", + rva: 0x1d488ac, + expect: &[0xB8, 0x90, 0x12, 0x00, 0x00], + new: &[0xB8, 0x60, 0x2C, 0x00, 0x00], +}; + +/// The Series S budgets a scale above its own 2/3 outgrows, given the other consoles' sizes. +const SERIES_S_LARGER: [&Patch<'static>; 2] = [&SERIES_S_TARGET_POOL, &SERIES_S_TRANSIENT_HEAP]; + +/// Whether the highest scale the title can render at needs [`SERIES_S_LARGER`]: one above Series S' own 2/3 +/// (`apply_levers` says which scale that is). +fn needs_series_s_larger(scale: f32) -> bool { + scale > 2.0 / 3.0 + 1e-3 +} + +/// Writes [`SERIES_S_LARGER`] into the image at `base` when `scale` needs it: both budgets or neither, so a build +/// where one place differs keeps the console's own pair. Returns how many places were written. +/// +/// # Safety +/// As `disarm_watchdog`. +unsafe fn series_s_budgets(base: usize, scale: f32) -> usize { + if !needs_series_s_larger(scale) { + return 0; + } + let pair: Vec = SERIES_S_LARGER + .iter() + .map(|p| OwnedPatch { what: p.what, rva: p.rva, expect: p.expect.to_vec(), new: p.new.to_vec() }) + .collect(); + // SAFETY: forwarded from this function's contract. + unsafe { apply_all(base, &pair) } +} + /// The upscaler choice: `mov eax, 6` (own upscaler) then `cmovne eax, ecx` /// (FSR when the profile asks for it). NOPing the cmovne keeps 6. const OWN_UPSCALER: Patch<'static> = Patch { @@ -380,15 +434,8 @@ fn parse_scale(text: &str) -> Option { /// # Safety /// As `disarm_watchdog`. pub(crate) unsafe fn resolution_levers(base: usize, hash: &blake3::Hash) -> usize { - let scale = std::env::var(RENDER_SCALE_ENV).ok().filter(|v| !v.trim().is_empty()); - let min = std::env::var(RENDER_SCALE_MIN_ENV).ok().filter(|v| !v.trim().is_empty()); - let pinned = scale.as_deref().and_then(parse_scale).is_some(); - let own = std::env::var_os(OWN_UPSCALER_ENV).is_some_and(|v| v != "0"); - let fsr = std::env::var_os(FSR_UPSCALER_ENV).is_some_and(|v| v == "1"); - let output = std::env::var(OUTPUT_ENV).ok().filter(|v| !v.trim().is_empty()).or_else(|| { - std::env::var_os(OUTPUT_1440_ENV).is_some_and(|v| v == "1").then(|| "2560x1440".to_owned()) - }); - if scale.is_none() && min.is_none() && !own && !fsr && output.is_none() { + let levers = Levers::from_env(); + if levers.is_empty() { return 0; } // Every image the loader maps comes here (the title's DLLs too); only @@ -397,9 +444,51 @@ pub(crate) unsafe fn resolution_levers(base: usize, hash: &blake3::Hash) -> usiz tracing::debug!("resolution levers: not the 1.0.2.1 executable; nothing patched"); return 0; } + // SAFETY: forwarded from this function's contract. + unsafe { apply_levers(base, &levers) } +} + +/// The resolution levers the environment asks for, as [`resolution_levers`] reads them. +#[derive(Default)] +struct Levers { + scale: Option, + min: Option, + own: bool, + fsr: bool, + output: Option, +} + +impl Levers { + fn from_env() -> Self { + let set = |var: &str| std::env::var(var).ok().filter(|v| !v.trim().is_empty()); + Levers { + scale: set(RENDER_SCALE_ENV), + min: set(RENDER_SCALE_MIN_ENV), + own: std::env::var_os(OWN_UPSCALER_ENV).is_some_and(|v| v != "0"), + fsr: std::env::var_os(FSR_UPSCALER_ENV).is_some_and(|v| v == "1"), + output: set(OUTPUT_ENV).or_else(|| { + std::env::var_os(OUTPUT_1440_ENV).is_some_and(|v| v == "1").then(|| "2560x1440".to_owned()) + }), + } + } + + fn is_empty(&self) -> bool { + self.scale.is_none() && self.min.is_none() && !self.own && !self.fsr && self.output.is_none() + } +} + +/// Writes `levers` into the 1.0.2.1 image at `base`. Returns how many places were written. +/// +/// # Safety +/// As `disarm_watchdog`. +unsafe fn apply_levers(base: usize, levers: &Levers) -> usize { + let Levers { scale, min, own, fsr, output } = levers; + let (own, fsr) = (*own, *fsr); + let pinned_scale = scale.as_deref().and_then(parse_scale); + let pinned = pinned_scale.is_some(); let mut applied = 0; if let Some(text) = output { - match parse_output(&text) { + match parse_output(text) { // The title's own output. Some((3840, 2160)) => {} Some((width, height)) => { @@ -421,7 +510,7 @@ pub(crate) unsafe fn resolution_levers(base: usize, hash: &blake3::Hash) -> usiz applied += usize::from(unsafe { apply(base, &FSR_UPSCALER) }); } if let Some(text) = scale { - match parse_scale(&text) { + match parse_scale(text) { Some(scale) => { // With the own upscaler forced, FSR is left as it is. for p in render_scale_patches(base, scale, !own) { @@ -432,9 +521,11 @@ pub(crate) unsafe fn resolution_levers(base: usize, hash: &blake3::Hash) -> usiz None => tracing::warn!(text, "{RENDER_SCALE_ENV} wants a number in 0.2..=1.0"), } } - if let Some(text) = min.filter(|_| !pinned) { - match parse_scale(&text) { + let mut dynamic = false; + if let Some(text) = min.as_ref().filter(|_| !pinned) { + match parse_scale(text) { Some(min) => { + dynamic = true; // The minimum alone (the first of the render-scale patches); the title's own choice stays above it. if let Some(p) = render_scale_patches(base, min, false).into_iter().next() { // SAFETY: forwarded from this function's contract. @@ -446,6 +537,16 @@ pub(crate) unsafe fn resolution_levers(base: usize, hash: &blake3::Hash) -> usiz None => tracing::warn!(text, "{RENDER_SCALE_MIN_ENV} wants a number in 0.2..=1.0"), } } + // Series S' budgets, for the highest scale the title can render at. A pinned scale is that scale. Otherwise the + // title's own dynamic resolution rises up to its maximum, max(minimum, 1.0) on Series S (the minimum stored at + // 0x28f1e78, the maximum's `vmaxss` at 0x28f1e87, 1.0 from 0x1fb4300 there and 2/3 elsewhere), so with its + // upscaler forced or a lowest scale asked for the ceiling is 1.0 whatever the lowest. Only Series S' budgets + // change: the other consoles' hold any of these already. + let ceiling = pinned_scale.or_else(|| (own || dynamic).then_some(1.0)); + if let Some(top) = ceiling { + // SAFETY: forwarded from this function's contract. + applied += unsafe { series_s_budgets(base, top) }; + } applied } @@ -646,9 +747,11 @@ mod tests { let offset = s.file_offset as usize + rva - s.rva as usize; &data[offset..offset + len] }; - for p in [&FSR_UPSCALER, &OWN_UPSCALER] { + for p in [&FSR_UPSCALER, &OWN_UPSCALER, &SERIES_S_TARGET_POOL, &SERIES_S_TRANSIENT_HEAP] { assert_eq!(at(p.rva, p.expect.len()), p.expect, "{}", p.what); } + // The other consoles' row: what the Series S pool is given. + assert_eq!(at(SERIES_S_TARGET_POOL.rva + 0x50, 8), SERIES_S_TARGET_POOL.new, "the other consoles' pool"); // The image's base does not matter to these: the vtable slot (a relocated pointer) is left out. let scales = render_scale_patches(0, 0.5, true); for p in output_patches(2560, 1440).iter().chain(scales.iter().filter(|p| p.rva != 0x6300ba8)) { @@ -665,6 +768,146 @@ mod tests { } } + /// Series S' own scale and those below it keep the console's budgets; any scale above it gets the larger pair. + #[test] + fn only_a_scale_above_series_s_own_grows_its_budgets() { + for scale in [0.3333, 0.5, 0.6, 0.6667, 2.0 / 3.0] { + assert!(!needs_series_s_larger(scale), "{scale}"); + } + for scale in [0.75, 0.8, 1.0] { + assert!(needs_series_s_larger(scale), "{scale}"); + } + assert_eq!(u64::from_le_bytes(SERIES_S_TARGET_POOL.expect.try_into().unwrap()), 1277 << 20); + assert_eq!(u64::from_le_bytes(SERIES_S_TARGET_POOL.new.try_into().unwrap()), 2199 << 20); + // The heap's page counts: 297 MiB on Series S, 710 MiB on the others. + let pages = |b: &[u8]| u32::from_le_bytes(b[1..5].try_into().unwrap()) as u64; + assert_eq!(pages(SERIES_S_TRANSIENT_HEAP.expect), 0x1290); + assert_eq!(pages(SERIES_S_TRANSIENT_HEAP.new), 0x2c60); + } + + /// The budgets land in an image: both at a scale above 2/3, neither at Series S' own, and neither when one + /// place holds other bytes (an image of zeros with the two places written, as far as the pool's place). + #[test] + fn the_series_s_budgets_are_written_both_or_neither() { + let image = || { + let mut bytes = vec![0u8; SERIES_S_TARGET_POOL.rva + 8]; + for p in SERIES_S_LARGER { + bytes[p.rva..p.rva + p.expect.len()].copy_from_slice(p.expect); + } + bytes + }; + let at = |bytes: &[u8], p: &Patch<'_>| bytes[p.rva..p.rva + p.expect.len()].to_vec(); + for (scale, written) in [(1.0, 2), (0.75, 2), (0.6667, 0), (0.5, 0)] { + let mut bytes = image(); + // SAFETY: the buffer holds every place written; the patches only change bytes the buffer owns. + let done = unsafe { series_s_budgets(bytes.as_mut_ptr() as usize, scale) }; + assert_eq!(done, written, "{scale}"); + for p in SERIES_S_LARGER { + assert_eq!(at(&bytes, p), if written == 2 { p.new } else { p.expect }, "{scale}: {}", p.what); + } + } + // The heap's place holds other bytes: the pool is left as it was too. + let mut bytes = image(); + let heap = SERIES_S_TRANSIENT_HEAP.rva; + bytes[heap] = 0x90; + // SAFETY: as above. + assert_eq!(unsafe { series_s_budgets(bytes.as_mut_ptr() as usize, 1.0) }, 0); + assert_eq!(at(&bytes, &SERIES_S_TARGET_POOL), SERIES_S_TARGET_POOL.expect); + } + + /// The levers give Series S its budgets for the highest scale the title can render at: a pinned one, else the + /// 1.0 its own upscaler may rise to there. Run through `apply_levers`, as `resolution_levers` does after its + /// checks, on an image of zeros with the two places written (as far as the FSR vtable slot the levers read). + #[test] + fn the_levers_give_series_s_its_budgets_for_the_highest_scale_it_can_reach() { + let written = |levers: Levers| -> bool { + let mut bytes = vec![0u8; 0x640_0000]; + for p in SERIES_S_LARGER { + bytes[p.rva..p.rva + p.expect.len()].copy_from_slice(p.expect); + } + // SAFETY: the buffer holds every place the levers read or write; only its bytes change. + unsafe { apply_levers(bytes.as_mut_ptr() as usize, &levers) }; + SERIES_S_LARGER.iter().all(|p| bytes[p.rva..p.rva + p.new.len()] == *p.new) + }; + let s = |v: &str| Some(v.to_owned()); + // -Resolution 1080 and -Lowest 1080, with the own upscaler the launcher forces for both. + assert!(written(Levers { scale: s("1"), own: true, ..Default::default() })); + assert!(written(Levers { min: s("1"), own: true, ..Default::default() })); + // -Lowest 720: the lowest is Series S' own 2/3, but the title may rise to 1.0 above it. + assert!(written(Levers { min: s("0.6667"), own: true, ..Default::default() })); + // The own upscaler alone (the F1 menu's OWN UPSCALER). + assert!(written(Levers { own: true, ..Default::default() })); + // A lowest scale alone (the F1 menu's LOWEST SCALE without OWN UPSCALER): the maximum is still + // max(lowest, 1.0) there. One the levers refuse asks for nothing. + assert!(written(Levers { min: s("0.6667"), ..Default::default() })); + assert!(written(Levers { min: s("0.6667"), fsr: true, ..Default::default() })); + assert!(!written(Levers { min: s("half"), ..Default::default() })); + // A pinned scale at Series S' own 2/3 or below is the most the title renders at; a pinned scale wins over a + // lowest one. + assert!(!written(Levers { scale: s("0.6667"), own: true, ..Default::default() })); + assert!(!written(Levers { scale: s("0.5"), min: s("1"), own: true, ..Default::default() })); + // Neither the own upscaler nor a scale. + assert!(!written(Levers { fsr: true, ..Default::default() })); + } + + /// `resolution_levers`, as the loader calls it: it reads the levers from the environment and patches only the + /// 1.0.2.1 executable (a hash that starts as that build's). The environment is set under a lock and put back. + #[test] + fn the_loader_entry_reads_the_environment_and_patches_only_that_build() { + static ENV: std::sync::Mutex<()> = std::sync::Mutex::new(()); + let _held = ENV.lock().unwrap_or_else(|e| e.into_inner()); + let vars = [ + RENDER_SCALE_ENV, + RENDER_SCALE_MIN_ENV, + OWN_UPSCALER_ENV, + FSR_UPSCALER_ENV, + OUTPUT_ENV, + OUTPUT_1440_ENV, + ]; + let saved: Vec<_> = vars.iter().map(std::env::var_os).collect(); + let set = |pairs: &[(&str, &str)]| { + for v in vars { + // SAFETY: the only test that changes these, under `ENV`; nothing else in this process sets them. + unsafe { std::env::remove_var(v) }; + } + for (v, value) in pairs { + // SAFETY: as above. + unsafe { std::env::set_var(v, value) }; + } + }; + let mut prefix = [0u8; 32]; + prefix[..8].copy_from_slice(&[0xb9, 0x9f, 0x87, 0x6b, 0x74, 0xd4, 0xa7, 0x7f]); + let build = blake3::Hash::from(prefix); + let other = blake3::Hash::from([0u8; 32]); + let written = |hash: &blake3::Hash| -> bool { + let mut bytes = vec![0u8; 0x640_0000]; + for p in SERIES_S_LARGER { + bytes[p.rva..p.rva + p.expect.len()].copy_from_slice(p.expect); + } + // SAFETY: the buffer holds every place the levers read or write; only its bytes change. + unsafe { resolution_levers(bytes.as_mut_ptr() as usize, hash) }; + SERIES_S_LARGER.iter().all(|p| bytes[p.rva..p.rva + p.new.len()] == *p.new) + }; + // -Resolution 1080 on Series S, as the launcher sets it. + set(&[(RENDER_SCALE_ENV, "1"), (OWN_UPSCALER_ENV, "1")]); + let (at_build, at_other) = (written(&build), written(&other)); + // Nothing asked for. + set(&[]); + let unasked = written(&build); + for (v, value) in vars.iter().zip(saved) { + // SAFETY: as above. + unsafe { + match value { + Some(value) => std::env::set_var(v, value), + None => std::env::remove_var(v), + } + } + } + assert!(at_build, "the 1.0.2.1 executable with -Resolution 1080"); + assert!(!at_other, "another image"); + assert!(!unasked, "no lever set"); + } + /// The game step's loads are pointed from the title's 1/15 s at the constant asked for, the displacement /// counted from the next instruction; the two constants are checked by bytes put back unchanged. #[test]