Merge pull request #715 from ludovic111/fix/vsync-windowed

🤖 Agent: merged by Ludovic's AI agent: verify passed, parity proven (CI or perf-frame-2's local run), before/after numbers and screenshots in the PR body.
This commit is contained in:
Ludovic Marie
2026-10-17 21:42:36 +02:00
committed by GitHub
5 changed files with 49 additions and 27 deletions
+11 -24
View File
@@ -1847,30 +1847,12 @@ impl Renderer {
})
.create_view(&Default::default())
}
/// VSync on: tear-free presentation (Mailbox, else Fifo); off: Immediate when available.
/// VSync, unless Horizon's present-mode setting names a supported mode.
/// VSync on presents with Fifo, capped to the display's refresh rate in a
/// window and in fullscreen alike; Horizon's present-mode setting applies
/// only with VSync off (`window::chosen_present_mode`).
pub fn set_present_mode(&mut self, vsync: bool, mode: crate::graphics::PresentMode) {
use crate::graphics::PresentMode as P;
let wanted = match mode {
P::Auto => None,
P::Fifo => Some(wgpu::PresentMode::Fifo),
P::Mailbox => Some(wgpu::PresentMode::Mailbox),
P::Immediate => Some(wgpu::PresentMode::Immediate),
};
match wanted.filter(|m| self.present_modes.contains(m)) {
Some(m) => {
if self.config.present_mode != m
&& let Some(surface) = &self.surface
{
self.config.present_mode = m;
surface.configure(&self.device, &self.config);
}
}
None => self.set_vsync(vsync),
}
}
pub fn set_vsync(&mut self, vsync: bool) {
use crate::window::{PresentMode, vsync_present_mode};
use crate::window::{PresentMode, chosen_present_mode};
let supported: Vec<PresentMode> = self
.present_modes
.iter()
@@ -1881,8 +1863,13 @@ impl Renderer {
_ => None,
})
.collect();
// GpuSurface.PresentMode.getSupportedVsyncMode.
let mode = match vsync_present_mode(&supported, vsync) {
let setting = match mode {
P::Auto => None,
P::Fifo => Some(PresentMode::Fifo),
P::Mailbox => Some(PresentMode::Mailbox),
P::Immediate => Some(PresentMode::Immediate),
};
let mode = match chosen_present_mode(&supported, vsync, setting) {
Some(PresentMode::Mailbox) => wgpu::PresentMode::Mailbox,
Some(PresentMode::Immediate) => wgpu::PresentMode::Immediate,
_ => wgpu::PresentMode::Fifo,
+3 -1
View File
@@ -53,7 +53,9 @@ impl Preset {
/// How the swapchain presents frames.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum PresentMode {
/// Follow vanilla's VSync option (Mailbox or Fifo on, Immediate off).
/// Vanilla's choice: Fifo with VSync on, else the first of Immediate,
/// Mailbox and Fifo the surface supports. The other modes apply only
/// with VSync off: VSync on always presents with Fifo.
#[default]
Auto,
Fifo,
+1 -1
View File
@@ -211,7 +211,7 @@ pub fn rows(level: Level) -> Vec<Row> {
h(
H::PresentMode,
None,
"How frames reach the screen. Auto follows VSync.",
"How frames reach the screen while VSync is off.",
),
h(
H::Pacing,
+33
View File
@@ -128,6 +128,20 @@ pub fn vsync_present_mode(supported: &[PresentMode], vsync: bool) -> Option<Pres
preferred.iter().copied().find(|m| supported.contains(m))
}
/// The present mode for the VSync option and Horizon's present-mode setting
/// (`None` for Auto). VSync on always means Fifo, in a window and in
/// fullscreen alike, as in vanilla; the setting chooses only while VSync is
/// off, when the surface supports it, and otherwise vanilla's choice applies.
pub fn chosen_present_mode(
supported: &[PresentMode],
vsync: bool,
setting: Option<PresentMode>,
) -> Option<PresentMode> {
setting
.filter(|m| !vsync && supported.contains(m))
.or_else(|| vsync_present_mode(supported, vsync))
}
/// `FramerateLimiter.limitDisplayFPS` with an injectable clock so the sleep
/// and spin decisions are testable.
#[derive(Clone, Debug)]
@@ -415,4 +429,23 @@ mod tests {
Some(PresentMode::Fifo)
);
}
#[test]
fn vsync_on_waits_for_the_display_whatever_the_present_mode() {
use PresentMode::*;
let all = [Mailbox, Fifo, Immediate];
for setting in [None, Some(Fifo), Some(Mailbox), Some(Immediate)] {
assert_eq!(chosen_present_mode(&all, true, setting), Some(Fifo));
}
assert_eq!(chosen_present_mode(&all, false, None), Some(Immediate));
assert_eq!(
chosen_present_mode(&all, false, Some(Mailbox)),
Some(Mailbox)
);
assert_eq!(chosen_present_mode(&all, false, Some(Fifo)), Some(Fifo));
// Metal has no Mailbox: vanilla's choice instead.
assert_eq!(
chosen_present_mode(&[Fifo, Immediate], false, Some(Mailbox)),
Some(Immediate)
);
}
}
+1 -1
View File
@@ -25,7 +25,7 @@ Every row has a one-line description and its cost in frame time (none, low, medi
| Performance | battery mode, worker threads, upload budget per frame | `horizon-graphics.txt` |
| Overlay | performance overlay: FPS, frame/CPU/GPU times, frame-time graph | `horizon-graphics.txt` |
`options.txt` keeps exactly vanilla's keys and values, so the options harness still compares it with vanilla; Horizon's settings live in `horizon-graphics.txt` beside it. Their defaults render as vanilla does: cave culling on, no upload budgets, sky, fog and weather on, 100% resolution, no FXAA, the present mode following VSync. Frame pacing defaults to Efficient: the limiter sleeps through the last half millisecond where vanilla spins, which changes timing, not pixels.
`options.txt` keeps exactly vanilla's keys and values, so the options harness still compares it with vanilla; Horizon's settings live in `horizon-graphics.txt` beside it. Their defaults render as vanilla does: cave culling on, no upload budgets, sky, fog and weather on, 100% resolution, no FXAA, the present mode following VSync. VSync on always waits for the display (Fifo), in a window and in fullscreen, as in vanilla; the present mode setting chooses how frames are shown only while VSync is off. Frame pacing defaults to Efficient: the limiter sleeps through the last half millisecond where vanilla spins, which changes timing, not pixels.
**Battery mode** (Performance, off by default) is for laptops and handhelds: at most 60 frames per second in a world, 30 in menus without a world or while another window has focus, and half the automatic worker threads (chunks load slower). It changes only when frames are drawn: what they show and the game's 20 ticks per second stay as they are.