From 2a8eed9bcc44908e4c2450d07a1e8c5763148c4f Mon Sep 17 00:00:00 2001 From: hussainmussa Date: Thu, 26 Nov 2026 22:40:51 +0200 Subject: [PATCH] FidelityFX: -Fsr4K takes FSR 3.1 where the SDK offers no 4.1.2 (#323) select_version took only a provider named 4.1.2 and failed otherwise, so -Fsr4K could not start on a card AMD's SDK offers no FSR 4 for. It now takes 4.1.2 where there is one and the newest 3.1.x otherwise, and the log says when 3.1 stands in. With neither, the run stops before the title starts, names what was offered and says to run without -Fsr4K. The check after context creation compares against the provider that was asked for, rather than against 4.1.2. Tested on an RTX 4070 (Windows 11, Rust 1.98.1) with AMD's signed amd_fidelityfx_upscaler_dx12.dll 4.1.2.4555 (SDK v2.4.0): - before: tests/fidelityfx.rs fails, "no FSR 4.1.2 provider for this adapter (available: 3.1.6, 2.3.4)" - after: provider 3.1.6, context made, two frames dispatched - cargo test -p victory-hle --lib fidelityfx: 5 passed Not run against the title: FSR 3.1's picture in the game is not checked. Co-Authored-By: Claude Fable 5.2 --- README.md | 4 + apps/victory/src/run.rs | 2 +- crates/victory-hle/src/modules/fidelityfx.rs | 82 +++++++++++++++++--- crates/victory-hle/tests/fidelityfx.rs | 8 +- scripts/play.ps1 | 2 +- 5 files changed, 80 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b4d41cc3..a48765d0 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,10 @@ For PC FSR 4.1.2, set `[paths].fidelityfx_dir` in `victory.toml` or the retail 1.0.2.1 Series X 4K SDR profile. `balanced` selects 2258×1270 and `performance` selects 1920×1080. All three output 3840×2160. With `victory run` directly, set `VICTORY_FSR_4K` to the preset name. +AMD's SDK offers FSR 4.1.2 for some cards only. Where it does not, the +newest FSR 3.1 it offers is used in its place and the log says so; its +picture differs from FSR 4's. Where it offers neither, the run stops +before the title starts and names the versions that were offered. The log reports the provider at context creation and the actual input and output sizes at the first successful temporal dispatch. The title profile and PC FidelityFX bridge were ported from [DexNom/VIctory](https://github.com/DexNom/VIctory/commit/ab34a8b21477c94da2a6cf07249138e8bc84602e). diff --git a/apps/victory/src/run.rs b/apps/victory/src/run.rs index e438aef4..ed9b7edd 100644 --- a/apps/victory/src/run.rs +++ b/apps/victory/src/run.rs @@ -77,7 +77,7 @@ pub fn run( // 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.2 from {}", dir.display()))?; + .with_context(|| format!("loading the PC FSR upscaler from {}", dir.display()))?; // 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") }; diff --git a/crates/victory-hle/src/modules/fidelityfx.rs b/crates/victory-hle/src/modules/fidelityfx.rs index 3c0f11cb..a11b155f 100644 --- a/crates/victory-hle/src/modules/fidelityfx.rs +++ b/crates/victory-hle/src/modules/fidelityfx.rs @@ -157,6 +157,8 @@ struct Host { dispatch: Call, device: ID3D12Device, provider_id: u64, + /// The provider's version as the SDK names it (`4.1.2`, `3.1.5`). + provider_name: String, } struct Library(windows_sys::Win32::Foundation::HMODULE); @@ -235,6 +237,12 @@ pub fn use_host_dir(dir: &Path) -> Result<(), String> { let (host, version) = load(dir)?; HOST.set(host) .map_err(|_| String::from("FidelityFX provider already initialized"))?; + if !is_fsr_4_1_2(&version) { + tracing::warn!( + version, + "FidelityFX: the SDK offers this adapter no FSR 4.1.2; FSR 3.1 is used in its place (its picture differs from 4's)" + ); + } tracing::info!(folder = %dir.display(), version, "FidelityFX: selected PC upscaler"); Ok(()) } @@ -288,6 +296,7 @@ fn load(dir: &Path) -> Result<(Host, String), String> { _library: library, device, provider_id, + provider_name: version.clone(), } }; Ok((host, version)) @@ -330,23 +339,49 @@ fn select_version(query_fn: Call, device: &ID3D12Device) -> Result<(u64, String) "upscaler version names could not be queried (code {code})" )); } - let mut available = Vec::new(); + let mut offered = Vec::new(); for (id, name) in ids.into_iter().zip(names).take(count as usize) { if !name.is_null() { // SAFETY: NUL-terminated name owned by the SDK. let name = unsafe { CStr::from_ptr(name) } .to_string_lossy() .into_owned(); - if is_fsr_4_1_2(&name) { - return Ok((id, name)); - } - available.push(name); + offered.push((id, name)); + } + } + choose(offered) +} + +/// The patch number of an FSR 3.1 provider's name (`3.1.5` → 5); `None` for +/// any other version (`3.10.0`, `2.3.4`, `4.1.2`). +fn fsr_3_1_patch(name: &str) -> Option { + let patch = name.strip_prefix("3.1.")?; + let digits = patch.len() - patch.trim_start_matches(|c: char| c.is_ascii_digit()).len(); + patch[..digits].parse().ok() +} + +/// The provider to ask for, of those the SDK offers for the adapter: FSR +/// 4.1.2 where there is one, otherwise the newest FSR 3.1 (which runs on any +/// D3D12 card; AMD offers 4 on few). With neither, what was offered is said. +fn choose(offered: Vec<(u64, String)>) -> Result<(u64, String), String> { + if let Some(at) = offered.iter().position(|(_, name)| is_fsr_4_1_2(name)) { + return Ok(offered.into_iter().nth(at).unwrap()); + } + let newest = offered + .iter() + .enumerate() + .filter_map(|(at, (_, name))| fsr_3_1_patch(name).map(|patch| (patch, at))) + .max_by_key(|&(patch, _)| patch); + match newest { + Some((_, at)) => Ok(offered.into_iter().nth(at).unwrap()), + None => { + let names: Vec<&str> = offered.iter().map(|(_, name)| name.as_str()).collect(); + Err(format!( + "the SDK offers this adapter neither FSR 4.1.2 nor FSR 3.1 (offered: {}); run without -Fsr4K (VICTORY_FSR_4K)", + if names.is_empty() { "nothing".into() } else { names.join(", ") } + )) } } - Err(format!( - "no FSR 4.1.2 provider for this adapter (available: {})", - available.join(", ") - )) } /// Puts the host backend after the title's upscaler description, in place @@ -442,12 +477,13 @@ extern "C" fn ffxCreateContext( .then(|| unsafe { CStr::from_ptr(version.name) }.to_string_lossy()); if query_code != OK || version.id != host.provider_id - || !name.as_deref().is_some_and(is_fsr_4_1_2) + || name.as_deref() != Some(host.provider_name.as_str()) { tracing::warn!( query_code, provider = name.as_deref().unwrap_or("?"), - "FidelityFX created a different provider than the requested 4.1.2" + requested = host.provider_name, + "FidelityFX created a different provider than the one requested" ); // SAFETY: the PC provider just created the context with this allocator. unsafe { (host.destroy)(context, memory) }; @@ -827,6 +863,30 @@ mod tests { ); } + fn offered(names: &[&str]) -> Vec<(u64, String)> { + names.iter().zip(1..).map(|(name, id)| (id, name.to_string())).collect() + } + + /// FSR 4.1.2 where the SDK offers it; otherwise the newest 3.1, which + /// is what a card without FSR 4 is offered; with neither, said so. + #[test] + fn the_provider_is_4_1_2_or_else_the_newest_3_1() { + assert_eq!(choose(offered(&["3.1.5", "4.1.2", "2.3.4"])), Ok((2, "4.1.2".into()))); + assert_eq!(choose(offered(&["4.1.2 (ML)", "3.1.5"])), Ok((1, "4.1.2 (ML)".into()))); + assert_eq!(choose(offered(&["2.3.4", "3.1.4", "3.1.5"])), Ok((3, "3.1.5".into()))); + assert_eq!(choose(offered(&["3.1.10", "3.1.9"])), Ok((1, "3.1.10".into()))); + // Other versions are not taken for either. + assert_eq!(choose(offered(&["4.1.20", "3.1.5", "3.10.0"])), Ok((2, "3.1.5".into()))); + let none = choose(offered(&["2.3.4", "4.0.3"])).unwrap_err(); + assert!(none.contains("2.3.4, 4.0.3") && none.contains("-Fsr4K"), "{none}"); + assert!(choose(Vec::new()).unwrap_err().contains("nothing")); + assert_eq!(fsr_3_1_patch("3.1.5"), Some(5)); + assert_eq!(fsr_3_1_patch("3.1.5-beta"), Some(5)); + assert_eq!(fsr_3_1_patch("3.1."), None); + assert_eq!(fsr_3_1_patch("3.10.0"), None); + assert_eq!(fsr_3_1_patch("4.1.2"), None); + } + #[test] fn resource_states_read_as_the_pc_backend_reads_them() { assert_eq!(host_state(0xc), Some(0xc0)); diff --git a/crates/victory-hle/tests/fidelityfx.rs b/crates/victory-hle/tests/fidelityfx.rs index 84126a0b..bd52d2f3 100644 --- a/crates/victory-hle/tests/fidelityfx.rs +++ b/crates/victory-hle/tests/fidelityfx.rs @@ -490,11 +490,9 @@ fn the_titles_upscaler_runs_on_the_host() { eprintln!("provider {:#x} {:?}", provider.id, unsafe { std::ffi::CStr::from_ptr(provider.name) }); - assert!( - unsafe { std::ffi::CStr::from_ptr(provider.name) } - .to_bytes() - .starts_with(b"4.1.2") - ); + // FSR 4.1.2 where the SDK offers it for this card, FSR 3.1 otherwise. + let name = unsafe { std::ffi::CStr::from_ptr(provider.name) }.to_bytes(); + assert!(name.starts_with(b"4.1.2") || name.starts_with(b"3.1.")); // Two frames: the first resets the history. for reset in [1, 0] { upscaling.reset = reset; diff --git a/scripts/play.ps1 b/scripts/play.ps1 index 5131464b..7f3e3da4 100644 --- a/scripts/play.ps1 +++ b/scripts/play.ps1 @@ -2,7 +2,7 @@ # scripts\play.ps1 -Exe # -Safe without the newer, faster ways (the first thing to try when something is wrong) # -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 at 3840x2160 (quality, balanced or performance): FSR 4.1.2 where AMD's SDK offers it for the card, FSR 3.1 otherwise # -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.