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 <noreply@anthropic.com>
This commit is contained in:
Alfredo Peres
2026-12-01 14:21:31 -03:00
parent 0cbc9e93ab
commit de550f7cbf
2 changed files with 97 additions and 24 deletions

View File

@ -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

View File

@ -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<f32> {
/// # 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<String>,
min: Option<String>,
own: bool,
fsr: bool,
output: Option<String>,
}
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]