diff --git a/apps/victory/src/main.rs b/apps/victory/src/main.rs index 790704aa8..94b3893d6 100644 --- a/apps/victory/src/main.rs +++ b/apps/victory/src/main.rs @@ -628,8 +628,10 @@ fn main() -> Result<()> { println!("The shader cache was not cleared: {e:#}. Starting the game."); } // An explicit request also runs when the automatic preload already completed. - if preload || ready.warmup_mark.is_some() { - play::preload(&ready.exe, ready.warmup_mark.as_deref()); + // (After the clearing above: a cache left without made pipelines is preloaded as well, #4176.) + let mark = play::preload_needed(&ready); + if preload || mark.is_some() { + play::preload(&ready.exe, mark.as_deref()); } victory_core::logging::init("info"); // F1's MFG UNLOCKER: the mod, before the layer makes its device or loads Streamline. diff --git a/apps/victory/src/play.rs b/apps/victory/src/play.rs index 81697cb95..9347f919e 100644 --- a/apps/victory/src/play.rs +++ b/apps/victory/src/play.rs @@ -1277,10 +1277,14 @@ fn memory_warning(ram: u64, files: &[String]) -> Option { /// every shader was made before the game ran: `%LOCALAPPDATA%\victory\warmed\`. /// `None`: the exe could not be read. fn warmup_mark(home: &Path, program: &Path) -> Option { + mark_path(home, program).filter(|mark| !mark.is_file()) +} + +/// The warmed mark for this exe's bytes, there or not. +fn mark_path(home: &Path, program: &Path) -> Option { let bytes = std::fs::read(program).ok()?; let hash = blake3::hash(&bytes).to_hex(); - let mark = home.join("warmed").join(&hash.as_str()[..16]); - (!mark.is_file()).then_some(mark) + Some(home.join("warmed").join(&hash.as_str()[..16])) } /// Keep paths as environment values when PowerShell starts the launcher again. @@ -1316,6 +1320,41 @@ pub(crate) struct Ready { /// The first start of this victory.exe: every shader is made before the /// game runs (`preload`), since the driver has none of them yet. pub(crate) warmup_mark: Option, + /// That mark's path even when it is there (`None` with the warm-up restart): + /// a cache without made pipelines is preloaded too ([`preload_needed`]). + pub(crate) preload_mark: Option, +} + +/// The mark to preload with: this exe's first start, or a shader cache whose +/// pipeline book has no host pipelines made from it (deleted by hand, cleared, +/// or of an earlier translator). +pub(crate) fn preload_needed(ready: &Ready) -> Option { + if ready.warmup_mark.is_some() { + return ready.warmup_mark.clone(); + } + let cfg = victory_core::Config::load().ok()?; + let empty = needs_pipelines(&cfg.cache_dir.join("pipelines")); + if empty && ready.preload_mark.is_some() { + println!("The shader cache has no pipelines made for this version: preloading shaders before the game starts."); + println!("This can take a while."); + } + empty.then(|| ready.preload_mark.clone()).flatten() +} + +/// The title's pipeline book is there (what a preload makes from) but no host +/// pipelines were made from it. Without a book a preload has nothing to make, +/// and the game records one as it plays. +fn needs_pipelines(pipelines: &Path) -> bool { + fn made_in(dir: &Path) -> bool { + std::fs::read_dir(dir).into_iter().flatten().flatten().any(|e| { + if e.file_type().is_ok_and(|t| t.is_dir()) { + made_in(&e.path()) + } else { + e.file_name().to_string_lossy().starts_with("host-pipelines.bin") && e.metadata().is_ok_and(|m| m.len() > 0) + } + }) + } + pipelines.join("known-pipelines.bin").is_file() && !made_in(pipelines) } /// Finds the title, puts the settings into the environment and names the @@ -1419,15 +1458,13 @@ pub(crate) fn prepare(o: &Play) -> Result { // before the game runs (main.rs calls `preload`), so the driver has them all before the // title's first frame instead of making them while the world loads. // `--set VICTORY_WARMUP_RESTART=`: the warm-up restart instead. - // Only with DLSS Frame Generation ready or on: its swap chain (Streamline's) is what held - // a first run at 14 fps (#515, #574); without it a first run plays at full speed. - let warmup_mark = if !s.contains_key("VICTORY_WARMUP_RESTART") - && s.get("VICTORY_FRAMEGEN").is_some_and(|v| v == "1") - { - warmup_mark(&home, &program) - } else { - None - }; + // With or without frame generation: draws wait for their pipelines + // (VICTORY_PIPELINE_WAIT_ALL), so a start that makes them in the world stutters (#4176). + // A named VICTORY_CACHE_DIR (benches, tests) preloads only when its pipelines are missing. + let restart = s.contains_key("VICTORY_WARMUP_RESTART"); + let named_cache = std::env::var_os("VICTORY_CACHE_DIR").is_some(); + let warmup_mark = if restart || named_cache { None } else { warmup_mark(&home, &program) }; + let preload_mark = (!restart).then(|| mark_path(&home, &program)).flatten(); if warmup_mark.is_some() { println!("First start of this version of victory.exe: preloading shaders before the game starts."); println!("This can take a while and is recorded once it succeeds."); @@ -1464,7 +1501,7 @@ pub(crate) fn prepare(o: &Play) -> Result { } } keep_ram(); - Ok(Ready { exe, series_s, warmup_mark }) + Ok(Ready { exe, series_s, warmup_mark, preload_mark }) } /// `VICTORY_KEEP_RAM_GB=` (unset: nothing): a hard minimum of `n` GB for @@ -1875,6 +1912,20 @@ mod tests { assert_eq!((get("VICTORY_MENU_CONSOLE"), get("VICTORY_MENU_FPS")), (Some("series-x"), Some("1000"))); } + #[test] + fn a_book_without_made_pipelines_is_preloaded() { + let dir = std::env::temp_dir().join(format!("victory-needs-pipelines-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(dir.join("adapters/gpu0")).unwrap(); + // No book: nothing to make. + assert!(!needs_pipelines(&dir)); + std::fs::write(dir.join("known-pipelines.bin"), [1u8; 8]).unwrap(); + assert!(needs_pipelines(&dir)); + std::fs::write(dir.join("adapters/gpu0/host-pipelines.bin"), [1u8; 8]).unwrap(); + assert!(!needs_pipelines(&dir)); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn without_nvidias_files_there_is_no_dlss() { let (s, _) = settings(&options(), &[], None);