diff --git a/README.md b/README.md index 37a2ad87..80a595d8 100644 --- a/README.md +++ b/README.md @@ -33,13 +33,13 @@ Settings go after the mode, or in `run.conf`: | Setting | Effect | |---------|--------| | `CONSOLE=series-s` | Series S profile | -| `VICTORY_OUTPUT=2560x1440` | Render size | +| `VICTORY_OUTPUT=2560x1440` | Output size: 1280x720, 1664x936, 1920x1080, 2560x1440 or 3840x2160. 2560x1440 by default; with FSR 4, 3840x2160 unless set | | `VICTORY_FULLSCREEN=1` | Borderless full screen | | `VICTORY_FULLSCREEN_RES=2560x1440` | Its size, if not the screen's | | `VICTORY_BVH_BUILDER=0` | No ray tracing | | `VICTORY_BVH_HARDWARE=0` | Ray tracing without the card's own node test | | `VICTORY_PAGES=0` | Title memory stays where it starts, RAM or card | -| `VICTORY_FSR_4K=performance` | FSR 4 to 4K, after `./setup.sh fsr4` | +| `VICTORY_FSR_4K=performance` | FSR 4 (quality, balanced or performance) to `VICTORY_OUTPUT`, 4K unless set, after `./setup.sh fsr4` | | `VICTORY_SKIP_BINK=1` | Skip the title's movies that play once (logos, intro); `all` skips the looping menu backgrounds too (they stay dark). Off by default | | `MUTE=1` | No sound | diff --git a/apps/victory/src/fsr_4k.rs b/apps/victory/src/fsr_4k.rs index d34fea26..f0e223b0 100644 --- a/apps/victory/src/fsr_4k.rs +++ b/apps/victory/src/fsr_4k.rs @@ -1,10 +1,29 @@ -//! Series X SDR 4K profiles for the title's PC FidelityFX dispatch. +//! Series X SDR profiles for the title's PC FidelityFX dispatch. use anyhow::{Context, Result, ensure}; use victory_loader::PreparedImage; const RETAIL_BUILD: &str = "b99f876b74d4a77f03af5f878223b27bd647b5c35a79d5decd9f716504239e93"; const SDR_MODE: u32 = 0x0615_c100; const STOCK_SDR_MODE: [u8; 16] = [0, 15, 0, 0, 112, 8, 0, 0, 60, 0, 0, 0, 1, 0, 0, 0]; +pub(super) const STOCK_OUTPUT: [u32; 2] = [3840, 2160]; + +/// The output FSR upscales to: `VICTORY_OUTPUT` when it is set (one of the +/// title's modes), else the stock 3840x2160. Unlike the title's own +/// upscaler, FSR keeps 4K as its default. +pub(super) fn requested_output() -> Result<[u32; 2]> { + match std::env::var("VICTORY_OUTPUT") { + Err(std::env::VarError::NotPresent) => Ok(STOCK_OUTPUT), + Ok(value) if value.trim() == "native" => Ok(STOCK_OUTPUT), + Ok(value) => victory_loader::resolution::parse_output(&value) + .map(|(w, h)| [w, h]) + .with_context(|| { + format!( + "VICTORY_OUTPUT={value}: expected one of the title's modes: 1280x720, 1664x936, 1920x1080, 2560x1440, 3840x2160" + ) + }), + Err(error) => Err(error.into()), + } +} #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(super) enum FsrPreset { @@ -61,15 +80,19 @@ impl FsrPreset { } } - pub(super) fn render_size(self) -> [u32; 2] { - [3840.0, 2160.0].map(|size| (size * self.render_scale()) as u32) + pub(super) fn render_size(self, output: [u32; 2]) -> [u32; 2] { + output.map(|size| (size as f32 * self.render_scale()) as u32) } } -pub(super) fn prepare_fsr_4k(image: &mut PreparedImage, preset: FsrPreset) -> Result<()> { +pub(super) fn prepare_fsr( + image: &mut PreparedImage, + preset: FsrPreset, + output: [u32; 2], +) -> Result<()> { ensure!( image.original_hash.to_hex().as_str() == RETAIL_BUILD, - "FSR at 4K is unavailable for this retail game build" + "FSR is unavailable for this retail game build" ); ensure!( *field::<16>(image, SDR_MODE)? == STOCK_SDR_MODE, @@ -101,6 +124,9 @@ pub(super) fn prepare_fsr_4k(image: &mut PreparedImage, preset: FsrPreset) -> Re "Unexpected Series X FSR scale instructions" ); } + if output != STOCK_OUTPUT { + victory_loader::resolution::prepare_output(image, output[0], output[1])?; + } // The original selector has already run its capability check. An // explicit FSR session uses renderer 7 every time it queries this. *field::<8>(image, 0x007e_9421)? = [0xb8, 7, 0, 0, 0, 0x90, 0x90, 0x90]; @@ -134,9 +160,9 @@ pub(super) fn prepare_fsr_4k(image: &mut PreparedImage, preset: FsrPreset) -> Re renderer = 7, preset = preset.name(), quality = preset.mode(), - expected_render = ?preset.render_size(), - output = ?[3840, 2160], - "Selected Series X FSR with the stock 4K frontend" + expected_render = ?preset.render_size(output), + ?output, + "Selected Series X FSR" ); Ok(()) } @@ -172,7 +198,7 @@ mod tests { let mut image = victory_loader::prepare(&bytes).unwrap(); let scanout = *field::<5>(&mut image, 0x0247_ad4a).unwrap(); let settings = *field::<8>(&mut image, 0x0580_48d4).unwrap(); - prepare_fsr_4k(&mut image, preset).unwrap(); + prepare_fsr(&mut image, preset, STOCK_OUTPUT).unwrap(); assert_eq!(*field::<16>(&mut image, SDR_MODE).unwrap(), STOCK_SDR_MODE); assert_eq!(*field::<5>(&mut image, 0x0247_ad4a).unwrap(), scanout); assert_eq!(*field::<8>(&mut image, 0x0580_48d4).unwrap(), settings); @@ -184,11 +210,39 @@ mod tests { *field::<4>(&mut image, 0x028e_f69b).unwrap(), u32::from(preset.mode()).to_le_bytes() ); - assert_eq!(preset.render_size(), size); + assert_eq!(preset.render_size(STOCK_OUTPUT), size); let disp = i32::from_le_bytes(*field::<4>(&mut image, 0x028e_f6b6).unwrap()); let reference = 0x028e_f6ba_u32.wrapping_add_signed(disp); let scale = f32::from_le_bytes(*field::<4>(&mut image, reference).unwrap()); assert_eq!([3840.0, 2160.0].map(|w| (w * scale) as u32), size); } } + + #[test] + fn fsr_below_4k_patches_title_output_and_keeps_the_fsr_preset() { + let Some(path) = std::env::var_os("VICTORY_DISPLAY_PROFILE_TEST_IMAGE") else { + return; + }; + let bytes = std::fs::read(path).unwrap(); + for (output, render) in [([2560, 1440], [1280, 720]), ([1920, 1080], [960, 540])] { + let mut image = victory_loader::prepare(&bytes).unwrap(); + let preset = FsrPreset::Performance; + prepare_fsr(&mut image, preset, output).unwrap(); + + assert_eq!( + *field::<4>(&mut image, 0x0224_352c).unwrap(), + output[0].to_le_bytes() + ); + assert_eq!( + *field::<4>(&mut image, 0x0224_3537).unwrap(), + output[1].to_le_bytes() + ); + assert_eq!(*field::<2>(&mut image, 0x0247_af2b).unwrap(), [0xeb, 0x01]); + assert_eq!( + *field::<4>(&mut image, 0x007e_9422).unwrap(), + 7u32.to_le_bytes() + ); + assert_eq!(preset.render_size(output), render); + } + } } diff --git a/apps/victory/src/run.rs b/apps/victory/src/run.rs index 6c15f9c9..e4330e6d 100644 --- a/apps/victory/src/run.rs +++ b/apps/victory/src/run.rs @@ -74,20 +74,28 @@ pub fn run( let image = if let Some(preset) = fsr { anyhow::ensure!( matches!(console, Console::SeriesX), - "FSR 4K requires Series X mode" + "FSR requires Series X mode" ); - fsr_4k::prepare_fsr_4k(&mut prepared, preset)?; + let output = fsr_4k::requested_output()?; + fsr_4k::prepare_fsr(&mut prepared, preset, output)?; let dir = cfg.fidelityfx_dir.as_deref().context( - "FSR 4K requires amd_fidelityfx_upscaler_dx12.dll. Set VICTORY_FIDELITYFX_DIR or [paths].fidelityfx_dir to its folder.", + "FSR requires amd_fidelityfx_upscaler_dx12.dll. Set VICTORY_FIDELITYFX_DIR or [paths].fidelityfx_dir to its folder.", )?; // Select the provider before the first lookup builds the import registry. victory_hle::modules::fidelityfx::use_host_dir(dir) .map_err(anyhow::Error::msg) .with_context(|| format!("loading FSR 4.1 from {}", dir.display()))?; + let [width, height] = output; + let display_mode = format!("{width}x{height}"); + let cache_mode = if output == fsr_4k::STOCK_OUTPUT { + "4k" + } else { + &display_mode + }; // SAFETY: Windows supports setting the environment across threads. // No title code has run or queried DXGI yet. - unsafe { std::env::set_var("VICTORY_DISPLAY_MODE", "3840x2160") }; - let cache = cfg.cache_subdir(&format!("images/fsr-{}-4k-sdr", preset.name()))?; + unsafe { std::env::set_var("VICTORY_DISPLAY_MODE", &display_mode) }; + let cache = cfg.cache_subdir(&format!("images/fsr-{}-{cache_mode}-sdr", preset.name()))?; victory_loader::load(&prepared, &cache)? } else { victory_loader::load(&prepared, &images)? diff --git a/crates/victory-gpu/src/host/panel.rs b/crates/victory-gpu/src/host/panel.rs index 838e2ad4..09e82f51 100644 --- a/crates/victory-gpu/src/host/panel.rs +++ b/crates/victory-gpu/src/host/panel.rs @@ -554,9 +554,10 @@ impl Upscaler { } } -/// From the switches the loader applies: `VICTORY_FSR_4K` (FSR 4 to 4K at its -/// preset's scale), else the title's own upscaler at `VICTORY_RENDER_SCALE` -/// (0.5 to 1.0; 2/3 without it, the title's Series X default). +/// From the switches the loader applies: `VICTORY_FSR_4K` (FSR 4 to the +/// output size, 4K unless `VICTORY_OUTPUT` is set, at its preset's scale), +/// else the title's own upscaler at `VICTORY_RENDER_SCALE` (0.5 to 1.0; 2/3 +/// without it, the title's Series X default). fn upscaler() -> &'static Upscaler { static IT: OnceLock = OnceLock::new(); IT.get_or_init(|| { diff --git a/crates/victory-loader/src/resolution.rs b/crates/victory-loader/src/resolution.rs index 1c2e6516..141d1a0f 100644 --- a/crates/victory-loader/src/resolution.rs +++ b/crates/victory-loader/src/resolution.rs @@ -65,7 +65,9 @@ pub(crate) fn asked_output() -> Option<(u32, u32)> { size } -pub(crate) fn parse_output(spec: &str) -> Option<(u32, u32)> { +/// `x` if it is one of the title's modes (1280x720, 1664x936, +/// 1920x1080, 2560x1440, 3840x2160). +pub fn parse_output(spec: &str) -> Option<(u32, u32)> { let (w, h) = spec.trim().split_once(['x', 'X'])?; let size = (w.trim().parse().ok()?, h.trim().parse().ok()?); MODES.contains(&size).then_some(size) @@ -171,6 +173,35 @@ pub(crate) fn pool_check_code() -> Vec { c } +fn output_patches(w: u32, h: u32) -> impl Iterator)> { + std::iter::once((&OUTPUT, output_code(w, h))) + .chain(((w, h) != (3840, 2160)).then(|| (&POOL_CHECK, pool_check_code()))) +} + +/// Applies the output patches to a prepared image before it is mapped. +pub fn prepare_output(image: &mut crate::PreparedImage, w: u32, h: u32) -> anyhow::Result<()> { + use anyhow::Context as _; + + anyhow::ensure!( + MODES.contains(&(w, h)), + "Unsupported title output size {w}x{h}" + ); + for (site, code) in output_patches(w, h) { + let start = image + .info + .rva_to_file_offset(site.at as u32) + .with_context(|| format!("Missing patch site for {}", site.what))? + as usize; + let bytes = image + .bytes + .get_mut(start..start + site.bytes.len()) + .with_context(|| format!("Incomplete patch site for {}", site.what))?; + anyhow::ensure!(bytes == site.bytes, "Unexpected bytes for {}", site.what); + bytes[..code.len()].copy_from_slice(&code); + } + Ok(()) +} + /// The title's own upscaler, its method +0x28f1e70: `mov dword [rcx+90h], /// 2/3` (the scale +0x7e9440 renders at). pub(crate) const OWN_SCALE: Site = Site { @@ -330,10 +361,7 @@ pub(crate) unsafe fn write(base: usize, size: usize, site: &Site, code: &[u8]) - pub unsafe fn apply_asked(base: usize, size: usize) { let mut asked: Vec<(&Site, Vec, String)> = Vec::new(); if let Some((w, h)) = asked_output() { - asked.push((&OUTPUT, output_code(w, h), format!("{w}x{h}"))); - if (w, h) != (3840, 2160) { - asked.push((&POOL_CHECK, pool_check_code(), format!("{w}x{h}"))); - } + asked.extend(output_patches(w, h).map(|(site, code)| (site, code, format!("{w}x{h}")))); } if let Some(s) = asked_render_scale() { asked.push((&OWN_SCALE, own_scale_code(s), format!("{s}"))); diff --git a/scripts/play.ps1 b/scripts/play.ps1 index 2e75a5dc..41b4c97d 100644 --- a/scripts/play.ps1 +++ b/scripts/play.ps1 @@ -1,7 +1,7 @@ # Starts the title on Windows with the settings this fork is played with. # scripts\play.ps1 -Exe # -RayTracing the title's own ray-traced indirect light: lit interiors, about a third slower -# -Fsr4K PC FidelityFX 4.1.2 at 3840x2160 (quality, balanced or performance) +# -Fsr4K PC FidelityFX 4.1.2 (quality, balanced or performance), to 3840x2160 or -More @{VICTORY_OUTPUT='2560x1440'} # -Build target\diag (the default, the build all measurements are of) or target\release # -More other settings: -More @{VICTORY_PANEL='1'} # Run it from a normal window, not as administrator: the driver then keeps its shader cache elsewhere.