From 29016670a0684e389e6d0680bf6d510131e1049e Mon Sep 17 00:00:00 2001 From: Alfredo Peres Date: Tue, 1 Dec 2026 11:40:24 -0300 Subject: [PATCH 1/5] d3d12-loader: Series S above 720p gets the other consoles' memory budgets (D3D12-SERIES-S-NATIVE-BUDGETS) Series S at render scale 1.0 (1080p, `-SeriesS -Resolution 1080`, `play-S-1080p.ps1`) stopped the title at its own breakpoint as soon as it started. The README listed it as "does not work yet". The title sizes two memory budgets per console, both for Series S' own 2/3 scale, and at 1.0 each in turn is too small: - The render-target pool, the third of nine pool sizes in the per-console table in .rdata: 1277 MiB on Series S, 2199 MiB on the others. Its allocator treats a failed allocation as fatal, and a 1 MiB one failed. - A heap counted in 64 KiB pages, `mov eax, 0x1290` then `cmovne` with 0x2c60: 297 MiB on Series S, 710 MiB on the others. A check compares a 432 MiB resource with it and stops at a breakpoint. With a pinned render scale above 2/3, resolution_levers now writes the other consoles' sizes into both places. Only Series S' row and its immediate change; the other consoles already hold such a scale. The retail-bytes test checks both places and the other consoles' pool size. RTX 3060, driver 621.40: Series S at 1080p ran five minutes into the world, rendering 1920x1080, at 11-22 fps, with no crash. Before, it stopped about 8 s in, once at each budget. Co-Authored-By: Claude Opus 5.5 --- d3d12/README.md | 16 ++++-- d3d12/crates/leonida-loader/src/hooks.rs | 63 +++++++++++++++++++++++- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/d3d12/README.md b/d3d12/README.md index 7f149eae5e..330ab05717 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,15 @@ 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. With a render scale above 2/3 on + Series S the loader gives both the other consoles' sizes + (`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..c874cf2c71 100644 --- a/d3d12/crates/leonida-loader/src/hooks.rs +++ b/d3d12/crates/leonida-loader/src/hooks.rs @@ -173,6 +173,41 @@ 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). The third +/// 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 a pinned render scale needs [`SERIES_S_LARGER`]: one above Series S' own 2/3. +fn needs_series_s_target_pool(scale: f32) -> bool { + scale > 2.0 / 3.0 + 1e-3 +} + /// 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 { @@ -428,6 +463,13 @@ pub(crate) unsafe fn resolution_levers(base: usize, hash: &blake3::Hash) -> usiz // SAFETY: forwarded from this function's contract. applied += usize::from(unsafe { apply(base, &p.borrowed()) }); } + // Only Series S' budgets change: the other consoles' hold such a scale already. + if needs_series_s_target_pool(scale) { + for p in SERIES_S_LARGER { + // SAFETY: forwarded from this function's contract. + applied += usize::from(unsafe { apply(base, p) }); + } + } } None => tracing::warn!(text, "{RENDER_SCALE_ENV} wants a number in 0.2..=1.0"), } @@ -646,9 +688,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)) { @@ -667,6 +711,23 @@ mod tests { /// 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. + /// Series S' own scale and those below it keep the console's pool; any scale above it gets the larger one. + #[test] + fn only_a_scale_above_series_s_own_grows_its_target_pool() { + for scale in [0.3333, 0.5, 0.6, 0.6667, 2.0 / 3.0] { + assert!(!needs_series_s_target_pool(scale), "{scale}"); + } + for scale in [0.75, 0.8, 1.0] { + assert!(needs_series_s_target_pool(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); + } + #[test] fn the_game_step_loads_point_at_the_ceiling_asked_for() { assert_eq!(parse_max_step(""), Some(None)); From 0cbc9e93abb08a5efc6ea6e5489c1324c00a55ce Mon Sep 17 00:00:00 2001 From: Alfredo Peres Date: Tue, 1 Dec 2026 13:52:57 -0300 Subject: [PATCH 2/5] d3d12-loader: the Series S budgets for -Lowest too, both or neither, and a test that they land Review of 29016670a (KelboLive): - The budgets were applied only for a pinned LEONIDA_RENDER_SCALE. -Lowest 1080 on Series S sets LEONIDA_RENDER_SCALE_MIN to 1.0, and the title renders at or above it. The minimum path now applies them too. - They are written by series_s_budgets through apply_all, so a build where one place differs keeps the console's own pair instead of a raised pool alone. - the_series_s_budgets_are_written_both_or_neither writes them into an image buffer: - both are written at 1.0 and at 0.75, and neither at 2/3 or 0.5; - neither is written when the heap's place holds other bytes. With the helper made to write nothing, it fails. - The game-step test has its doc comment back. The threshold test is named for both budgets. - The README says -Lowest is covered. Co-Authored-By: Claude Opus 5.5 --- d3d12/README.md | 3 +- d3d12/crates/leonida-loader/src/hooks.rs | 75 +++++++++++++++++++----- 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/d3d12/README.md b/d3d12/README.md index 330ab05717..b844cadcd0 100644 --- a/d3d12/README.md +++ b/d3d12/README.md @@ -530,7 +530,8 @@ What to expect: 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. With a render scale above 2/3 on - Series S the loader gives both the other consoles' sizes + Series S, held (`-Resolution`) or the dynamic resolution's lowest + (`-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 diff --git a/d3d12/crates/leonida-loader/src/hooks.rs b/d3d12/crates/leonida-loader/src/hooks.rs index c874cf2c71..57e0d07c85 100644 --- a/d3d12/crates/leonida-loader/src/hooks.rs +++ b/d3d12/crates/leonida-loader/src/hooks.rs @@ -203,11 +203,29 @@ const SERIES_S_TRANSIENT_HEAP: Patch<'static> = Patch { /// 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 a pinned render scale needs [`SERIES_S_LARGER`]: one above Series S' own 2/3. -fn needs_series_s_target_pool(scale: f32) -> bool { +/// Whether a render scale needs [`SERIES_S_LARGER`]: one above Series S' own 2/3, pinned (`-Resolution`) or the +/// lowest of the title's dynamic resolution (`-Lowest`), which it renders at or above. +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 { @@ -464,12 +482,8 @@ pub(crate) unsafe fn resolution_levers(base: usize, hash: &blake3::Hash) -> usiz applied += usize::from(unsafe { apply(base, &p.borrowed()) }); } // Only Series S' budgets change: the other consoles' hold such a scale already. - if needs_series_s_target_pool(scale) { - for p in SERIES_S_LARGER { - // SAFETY: forwarded from this function's contract. - applied += usize::from(unsafe { apply(base, p) }); - } - } + // SAFETY: forwarded from this function's contract. + applied += unsafe { series_s_budgets(base, scale) }; } None => tracing::warn!(text, "{RENDER_SCALE_ENV} wants a number in 0.2..=1.0"), } @@ -484,6 +498,9 @@ pub(crate) unsafe fn resolution_levers(base: usize, hash: &blake3::Hash) -> usiz applied += usize::from(done); tracing::info!(min, applied = done, "the title's own dynamic resolution: its lowest render scale set, the scale above it left to the title ({RENDER_SCALE_MIN_ENV})"); } + // The title renders at this scale or above, so Series S needs the same budgets as when pinned. + // SAFETY: forwarded from this function's contract. + applied += unsafe { series_s_budgets(base, min) }; } None => tracing::warn!(text, "{RENDER_SCALE_MIN_ENV} wants a number in 0.2..=1.0"), } @@ -709,16 +726,14 @@ mod tests { } } - /// 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. - /// Series S' own scale and those below it keep the console's pool; any scale above it gets the larger one. + /// 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_target_pool() { + 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_target_pool(scale), "{scale}"); + assert!(!needs_series_s_larger(scale), "{scale}"); } for scale in [0.75, 0.8, 1.0] { - assert!(needs_series_s_target_pool(scale), "{scale}"); + 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); @@ -728,6 +743,38 @@ mod tests { 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 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] fn the_game_step_loads_point_at_the_ceiling_asked_for() { assert_eq!(parse_max_step(""), Some(None)); From de550f7cbfecfa094fdc80a2a384d5b314ba525b Mon Sep 17 00:00:00 2001 From: Alfredo Peres Date: Tue, 1 Dec 2026 14:21:31 -0300 Subject: [PATCH 3/5] d3d12-loader: the Series S budgets follow the highest scale the title can reach Review of 0cbc9e93a (KelboLive): - The budgets followed the lowest scale, so -Lowest 720 on Series S (LEONIDA_RENDER_SCALE_MIN=0.6667) got none. The title's own upscaler stores its minimum at 0x28f1e78 and its maximum at 0x28f1e87 as max(minimum, f()). f() at 0x1fb4300 is 1.0 on Series S and 2/3 elsewhere. So with that upscaler forced and no pinned scale, Series S may render up to 1.0 whatever the lowest. The budgets now follow the ceiling: - a pinned scale is its own ceiling; - the own upscaler with no pinned scale has a ceiling of 1.0, as with -Lowest or the F1 menu's OWN UPSCALER; - with neither, the ceiling is the lowest asked for. - The call sites are covered now. resolution_levers reads the environment (Levers::from_env), checks the build and calls apply_levers. the_levers_give_series_s_its_budgets_for_the_highest_scale_it_can_reach runs apply_levers on an image buffer for -Resolution 1080, -Lowest 1080, -Lowest 720, the own upscaler alone, pinned 2/3, a pinned scale over a lowest one, and FSR alone. It fails when the budgets are dropped from apply_levers, and with the old floor rule. cargo test -p leonida-loader --lib: 87 passed. cargo clippy -p leonida-loader --all-targets -- -D warnings: clean. Co-Authored-By: Claude Opus 5.5 --- d3d12/README.md | 8 +- d3d12/crates/leonida-loader/src/hooks.rs | 113 ++++++++++++++++++----- 2 files changed, 97 insertions(+), 24 deletions(-) diff --git a/d3d12/README.md b/d3d12/README.md index b844cadcd0..b877e64e95 100644 --- a/d3d12/README.md +++ b/d3d12/README.md @@ -529,9 +529,11 @@ What to expect: 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. With a render scale above 2/3 on - Series S, held (`-Resolution`) or the dynamic resolution's lowest - (`-Lowest`), the loader gives both the other consoles' sizes, both or neither + 12 GB card too: it is not video memory. Whenever Series S can render above + 2/3 (a held scale above it with `-Resolution`, or the title's own upscaler + without a held scale, as `-Lowest` uses it, 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 diff --git a/d3d12/crates/leonida-loader/src/hooks.rs b/d3d12/crates/leonida-loader/src/hooks.rs index 57e0d07c85..752448a1ca 100644 --- a/d3d12/crates/leonida-loader/src/hooks.rs +++ b/d3d12/crates/leonida-loader/src/hooks.rs @@ -203,8 +203,8 @@ const SERIES_S_TRANSIENT_HEAP: Patch<'static> = Patch { /// 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 a render scale needs [`SERIES_S_LARGER`]: one above Series S' own 2/3, pinned (`-Resolution`) or the -/// lowest of the title's dynamic resolution (`-Lowest`), which it renders at or above. +/// 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 } @@ -433,15 +433,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 @@ -450,9 +443,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)) => { @@ -474,23 +509,22 @@ 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) { // SAFETY: forwarded from this function's contract. applied += usize::from(unsafe { apply(base, &p.borrowed()) }); } - // Only Series S' budgets change: the other consoles' hold such a scale already. - // SAFETY: forwarded from this function's contract. - applied += unsafe { series_s_budgets(base, scale) }; } 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 lowest = None; + if let Some(text) = min.as_ref().filter(|_| !pinned) { + match parse_scale(text) { Some(min) => { + lowest = Some(min); // 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. @@ -498,13 +532,20 @@ pub(crate) unsafe fn resolution_levers(base: usize, hash: &blake3::Hash) -> usiz applied += usize::from(done); tracing::info!(min, applied = done, "the title's own dynamic resolution: its lowest render scale set, the scale above it left to the title ({RENDER_SCALE_MIN_ENV})"); } - // The title renders at this scale or above, so Series S needs the same budgets as when pinned. - // SAFETY: forwarded from this function's contract. - applied += unsafe { series_s_budgets(base, min) }; } 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 upscaler 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 it forced the + // ceiling is 1.0 whatever the lowest; without it, the lowest asked for. Only Series S' budgets change: the + // other consoles' hold any of these already. + let ceiling = pinned_scale.or_else(|| own.then_some(1.0)).or(lowest); + if let Some(top) = ceiling { + // SAFETY: forwarded from this function's contract. + applied += unsafe { series_s_budgets(base, top) }; + } applied } @@ -773,6 +814,36 @@ mod tests { 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 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() })); + } + /// 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] From 8677729c94be264210a101dc98bb83c51bf59d31 Mon Sep 17 00:00:00 2001 From: Alfredo Peres Date: Tue, 1 Dec 2026 15:50:09 -0300 Subject: [PATCH 4/5] d3d12-loader: say where the pool's size sits in its row, and the thousandth above 2/3 Review of de550f7cb (imShangLi): - The comment called the pool "the third" of the row at 0x6186250, while the patch is 0x18 further on, which reads as the fourth. Each row is an 8-byte header followed by its nine sizes from +8 (the copy reads [row + 0x8]), so 0x6186268 is the third size. The comment says so now. - The README said "above 2/3"; the code wants more than 2/3 + 0.001, so that Series S' own 2/3 (0.6667) keeps the console's budgets. The README says that now. No code change. cargo test -p leonida-loader --lib hooks: 15 passed. Co-Authored-By: Claude Opus 5.5 --- d3d12/README.md | 3 ++- d3d12/crates/leonida-loader/src/hooks.rs | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/d3d12/README.md b/d3d12/README.md index b877e64e95..a19f3290ae 100644 --- a/d3d12/README.md +++ b/d3d12/README.md @@ -530,7 +530,8 @@ What to expect: 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 - 2/3 (a held scale above it with `-Resolution`, or the title's own upscaler + 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 upscaler without a held scale, as `-Lowest` uses it, 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 diff --git a/d3d12/crates/leonida-loader/src/hooks.rs b/d3d12/crates/leonida-loader/src/hooks.rs index 752448a1ca..ffe894e55c 100644 --- a/d3d12/crates/leonida-loader/src/hooks.rs +++ b/d3d12/crates/leonida-loader/src/hooks.rs @@ -174,8 +174,9 @@ 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). The third -/// is the pool whose allocator (`.data` 0x71c2808, made at 0x1e1d418) the render targets come from: 1277 MiB on +/// 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 From 61a47216ce61346988a83b8b91cf003ef0fde3d4 Mon Sep 17 00:00:00 2001 From: Alfredo Peres Date: Tue, 1 Dec 2026 16:45:24 -0300 Subject: [PATCH 5/5] d3d12-loader: test the levers' entry, and a lowest scale alone reaches 1.0 on Series S Review of 8677729c9 (KelboLive): - resolution_levers, the loader's only entry to the levers (windows.rs), was not pinned: replacing its apply_levers call with nothing still passed. A test now calls it with the environment set under a lock and put back, with a hash that starts as the 1.0.2.1 build's and with another one, and with no lever set. - A lowest scale with neither a pinned scale nor LEONIDA_OWN_UPSCALER (F1's LOWEST SCALE alone) still took the lowest as the ceiling. The title's own dynamic resolution rises to max(lowest, 1.0) on Series S whichever way it is reached, so a lowest scale now has a ceiling of 1.0 too. Both shown to fail without the fix: dropping the apply_levers call fails the new entry test; the old ceiling fails the lowest-alone case. cargo test --locked -p leonida-loader --lib with LEONIDA_GAME_DIR: 88 passed; clippy -D warnings clean. Co-Authored-By: Claude Opus 5.5 --- d3d12/README.md | 7 ++- d3d12/crates/leonida-loader/src/hooks.rs | 77 +++++++++++++++++++++--- 2 files changed, 74 insertions(+), 10 deletions(-) diff --git a/d3d12/README.md b/d3d12/README.md index a19f3290ae..7d8f2ae254 100644 --- a/d3d12/README.md +++ b/d3d12/README.md @@ -531,9 +531,10 @@ What to expect: 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 upscaler - without a held scale, as `-Lowest` uses it, since that one rises up to 1.0 - on Series S whatever the lowest), the loader gives both the other consoles' + 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). diff --git a/d3d12/crates/leonida-loader/src/hooks.rs b/d3d12/crates/leonida-loader/src/hooks.rs index ffe894e55c..bb52eb5e90 100644 --- a/d3d12/crates/leonida-loader/src/hooks.rs +++ b/d3d12/crates/leonida-loader/src/hooks.rs @@ -521,11 +521,11 @@ unsafe fn apply_levers(base: usize, levers: &Levers) -> usize { None => tracing::warn!(text, "{RENDER_SCALE_ENV} wants a number in 0.2..=1.0"), } } - let mut lowest = None; + let mut dynamic = false; if let Some(text) = min.as_ref().filter(|_| !pinned) { match parse_scale(text) { Some(min) => { - lowest = 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. @@ -538,11 +538,11 @@ unsafe fn apply_levers(base: usize, levers: &Levers) -> usize { } } // Series S' budgets, for the highest scale the title can render at. A pinned scale is that scale. Otherwise the - // title's own upscaler 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 it forced the - // ceiling is 1.0 whatever the lowest; without it, the lowest asked for. Only Series S' budgets change: the - // other consoles' hold any of these already. - let ceiling = pinned_scale.or_else(|| own.then_some(1.0)).or(lowest); + // 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) }; @@ -837,6 +837,11 @@ mod tests { 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() })); @@ -845,6 +850,64 @@ mod tests { 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]