Launcher: switch the game to DirectX 12 after a Vulkan crash at its start (#746)

A native crash in a graphics driver leaves no crash report, only the game's
last lines. When the game ends with no report after its Vulkan adapter line and
before its first frame (Windows only), set graphicsApi:dx12 in its graphics
settings (the game home's and the run's own, only where it was auto) and tell
the player to press Play again, instead of the crash notice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-19 07:43:12 +02:00
parent 21ac62a085
commit c2665ee7bd
3 changed files with 141 additions and 3 deletions
+106
View File
@@ -1008,6 +1008,63 @@ pub fn crash_summary(report: &str) -> Option<String> {
(!error.is_empty()).then(|| error.join(" "))
}
/// The line the game prints when it has drawn its first frame.
pub const GAME_FIRST_FRAME: &str = "Horizon: first frame";
/// Whether a game's output ends in a crash while its Vulkan renderer started: an adapter line
/// (`Horizon renderer: <GPU> (Vulkan, <type>)`) and no first frame. A native crash in a graphics
/// driver leaves no crash report, only those lines (#746).
pub fn vulkan_start_crashed<S: AsRef<str>>(lines: impl IntoIterator<Item = S>) -> bool {
let mut vulkan = false;
for line in lines {
let line = line.as_ref();
if line.starts_with(GAME_FIRST_FRAME) {
return false;
}
if let Some(adapter) = line.strip_prefix("Horizon renderer: ") {
// The adapter line, not one of the start's steps: "<GPU> (<backend>, <type>)".
if adapter.ends_with(')') && adapter.contains(" (") {
vulkan = adapter.contains(" (Vulkan, ");
}
}
}
vulkan
}
/// The game's graphics settings file in its home (the game's `graphics.rs`).
const GRAPHICS_FILE: &str = "horizon-graphics.txt";
/// Sets `graphicsApi:dx12` in `home`'s graphics settings when it is unset or `auto`, keeping
/// every other line. True when it changed; a player's own `vulkan` or `dx12` stays.
pub fn switch_to_dx12(home: &Path) -> Result<bool, String> {
let path = home.join(GRAPHICS_FILE);
let text = match fs::read_to_string(&path) {
Ok(text) => text,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
Err(e) => return Err(format!("{}: {e}", path.display())),
};
let mut lines: Vec<String> = Vec::new();
let mut found = false;
for line in text.lines() {
match line.split_once(':') {
Some((key, value)) if key.trim() == "graphicsApi" => {
if value.trim() != "auto" {
return Ok(false);
}
found = true;
lines.push("graphicsApi:dx12".to_owned());
}
_ => lines.push(line.to_owned()),
}
}
if !found {
lines.push("graphicsApi:dx12".to_owned());
}
fs::create_dir_all(home).map_err(|e| format!("{}: {e}", home.display()))?;
fs::write(&path, lines.join("\n") + "\n").map_err(|e| format!("{}: {e}", path.display()))?;
Ok(true)
}
/// The last `lines` lines of `file`, or why it cannot be read.
fn tail_of(file: &Path, lines: usize) -> Result<String, String> {
let bytes = fs::read(file).map_err(|e| format!("{}: {e}", file.display()))?;
@@ -1919,6 +1976,55 @@ mod tests {
.contains("application.name = \"Horizon\"")));
}
#[test]
fn a_crash_after_the_vulkan_adapter_line_and_before_the_first_frame_is_a_start_crash() {
let start = [
"Horizon: resources loaded in 11.3s; 35723 block states, 1658 items, 67 biomes",
"Horizon renderer: Intel(R) HD Graphics 530 (Vulkan, IntegratedGpu)",
];
assert!(vulkan_start_crashed(start));
// A step line after the adapter keeps it a start crash.
let step = [start[1], "Horizon renderer: creating the pipelines"];
assert!(vulkan_start_crashed(step));
// A crash after the first frame is the game's, not the renderer start's.
let later = [start[1], "Horizon: first frame after 812 ms"];
assert!(!vulkan_start_crashed(later));
// Already on DirectX 12, or no renderer yet: nothing to switch.
let dx12 = ["Horizon renderer: Intel(R) HD Graphics 530 (Dx12, IntegratedGpu)"];
assert!(!vulkan_start_crashed(dx12));
assert!(!vulkan_start_crashed(&start[..1]));
// Vulkan failed to open and DirectX 12 took over: its line is the last adapter.
assert!(!vulkan_start_crashed([start[1], dx12[0]]));
}
#[test]
fn a_renderer_start_crash_switches_auto_to_dx12_once() {
let home = std::env::temp_dir().join(format!("horizon-dx12-switch-{}", std::process::id()));
let _ = fs::remove_dir_all(&home);
// No settings file yet: one with the API alone.
assert_eq!(switch_to_dx12(&home), Ok(true));
assert_eq!(
fs::read_to_string(home.join(GRAPHICS_FILE)).unwrap(),
"graphicsApi:dx12\n"
);
// Already DirectX 12, or the player's own Vulkan: left alone.
assert_eq!(switch_to_dx12(&home), Ok(false));
fs::write(home.join(GRAPHICS_FILE), "sky:false\ngraphicsApi:vulkan\n").unwrap();
assert_eq!(switch_to_dx12(&home), Ok(false));
// Auto becomes DirectX 12 and the other settings stay.
fs::write(
home.join(GRAPHICS_FILE),
"sky:false\ngraphicsApi:auto\nfog:true\n",
)
.unwrap();
assert_eq!(switch_to_dx12(&home), Ok(true));
assert_eq!(
fs::read_to_string(home.join(GRAPHICS_FILE)).unwrap(),
"sky:false\ngraphicsApi:dx12\nfog:true\n"
);
let _ = fs::remove_dir_all(&home);
}
#[test]
fn extra_game_arguments_split_like_a_shell() {
assert!(split_args("").is_empty());
+34 -1
View File
@@ -131,6 +131,8 @@ struct Launcher {
client_error: Option<String>,
/// When the running client started, to tell its crash report from older ones.
client_started: Option<std::time::SystemTime>,
/// The folder the running game keeps its settings in: the game home, or a modpack's.
client_home: Option<std::path::PathBuf>,
crash: Option<Crash>,
server: Option<Running>,
/// Microsoft accounts (accounts.json), the sign-in under way and what it last showed.
@@ -261,6 +263,7 @@ impl Launcher {
client: None,
client_error: None,
client_started: None,
client_home: None,
crash: None,
server: None,
accounts: Accounts::load(),
@@ -449,6 +452,7 @@ impl Launcher {
}
}
backend::append_game_args(&mut cmd);
self.client_home = cmd.get_current_dir().map(|d| d.to_path_buf());
match backend::spawn(Job::Client, cmd, self.tx.clone()) {
Ok(r) => self.client = Some(r),
Err(e) => self.client_error = Some(e),
@@ -564,7 +568,15 @@ impl Launcher {
.client_started
.take()
.and_then(backend::crash_report_since);
if error.is_some() || report.is_some() {
if error.is_some() && report.is_none() && self.switched_to_dx12() {
self.client_error = Some(
"Horizon's Vulkan renderer crashed while starting on this \
graphics card, so the game now uses DirectX 12. Press Play \
to start it again. Video Settings > Graphics API switches \
back."
.into(),
);
} else if error.is_some() || report.is_some() {
let summary = report
.as_ref()
.and_then(|p| std::fs::read_to_string(p).ok())
@@ -627,6 +639,27 @@ impl Launcher {
}
}
/// After a crash with no report: when the game's output ends while its Vulkan renderer
/// started, a native crash in the graphics driver (#746), switches the game to DirectX 12
/// (Windows only; only where the player left the graphics API on Auto). True when it did.
fn switched_to_dx12(&self) -> bool {
if !cfg!(windows) || !backend::vulkan_start_crashed(&self.prepare_log) {
return false;
}
// The GPU is the same for every profile: the game home's setting and the crashed run's.
let home = backend::game_home();
let mut homes = vec![home.clone()];
homes.extend(self.client_home.clone().filter(|h| *h != home));
let mut switched = false;
for home in homes {
match backend::switch_to_dx12(&home) {
Ok(changed) => switched |= changed,
Err(e) => eprintln!("Horizon launcher: could not switch to DirectX 12: {e}"),
}
}
switched
}
fn selected(&self) -> Option<&String> {
self.settings
.selected
+1 -2
View File
@@ -22,8 +22,7 @@ pub fn process_started() -> Instant {
/// Frames further apart than this are the UI resting, not UI frame times.
const IDLE_GAP: Duration = Duration::from_millis(250);
/// The line the game prints when it has drawn its first frame.
pub const GAME_FIRST_FRAME: &str = "Horizon: first frame";
pub use horizon_launcher::backend::GAME_FIRST_FRAME;
pub struct Perf {
report: Report,