Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

gui: drop the swapchain frame latency for unfocused windows

RoversXc644ec574799
diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs
index b4440cc7..64b89408 100644
--- a/wezterm-gui/src/termwindow/mod.rs
+++ b/wezterm-gui/src/termwindow/mod.rs
@@ -2855,6 +2855,13 @@ impl TermWindow {
// paint promptly (leading edge) before the throttle kicks in.
self.unfocused_invalidate_due = None;
self.unfocused_next_allowed = Instant::now();
+ // An unfocused window gets by with one less swapchain drawable
+ // (one framebuffer-sized allocation); applied lazily at the next
+ // paint's reconfigure. Guarded: FocusChanged can arrive before
+ // the async webgpu setup finishes.
+ if let Some(webgpu) = self.webgpu.as_ref() {
+ webgpu.set_desired_frame_latency(if focused { 2 } else { 1 });
+ }
self.quad_generation += 1;
self.load_os_parameters();
@@ -3582,6 +3589,11 @@ impl TermWindow {
}
if let Some(webgpu) = webgpu {
myself.webgpu.replace(Rc::clone(&webgpu));
+ // Seed the swapchain latency from the current focus: a
+ // window spawned in the background starts at the reduced
+ // latency and only pays for the third drawable once it is
+ // actually focused.
+ webgpu.set_desired_frame_latency(if myself.focused.is_some() { 2 } else { 1 });
myself.created(RenderContext::WebGpu(Rc::clone(&webgpu)))?;
}
myself.apply_native_terminal_settings();
@@ -9377,7 +9389,6 @@ impl Drop for TermWindow {
}
}
-
/// How long a window must stay fully occluded before its lazily-rebuilt
/// caches are released. Long enough to survive a glance at another Space
/// or a brief cover-up; matches the sidebar's idle-release precedent.
diff --git a/wezterm-gui/src/termwindow/webgpu.rs b/wezterm-gui/src/termwindow/webgpu.rs
index 92a8519a..81aff326 100644
--- a/wezterm-gui/src/termwindow/webgpu.rs
+++ b/wezterm-gui/src/termwindow/webgpu.rs
@@ -1,7 +1,7 @@
use crate::quad::Vertex;
use anyhow::anyhow;
use config::{ConfigHandle, GpuInfo, WebGpuPowerPreference};
-use std::cell::RefCell;
+use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::sync::Arc;
use window::bitmaps::Texture2d;
@@ -75,6 +75,11 @@ pub struct WebGpuState {
pub texture_linear_sampler: wgpu::Sampler,
frame_uniforms: RefCell<FrameUniforms>,
atlas_bind_groups: RefCell<Option<AtlasBindGroups>>,
+ /// A frame-latency change waiting to be folded into the next
+ /// surface.configure. Reconfiguring drains the whole device queue on
+ /// the calling thread, so it is never done eagerly on focus change;
+ /// the next paint's resize() call picks it up.
+ pending_frame_latency: Cell<Option<u32>>,
pub handle: RawHandlePair,
}
@@ -676,9 +681,21 @@ impl WebGpuState {
texture_linear_sampler,
frame_uniforms: RefCell::new(FrameUniforms::default()),
atlas_bind_groups: RefCell::new(None),
+ pending_frame_latency: Cell::new(None),
})
}
+ /// Ask for a different swapchain frame latency. Applied lazily: the
+ /// next paint's resize() folds it into the surface.configure it
+ /// already makes, so the blocking queue drain is never paid twice.
+ pub fn set_desired_frame_latency(&self, latency: u32) {
+ if self.config.borrow().desired_maximum_frame_latency == latency {
+ self.pending_frame_latency.set(None);
+ } else {
+ self.pending_frame_latency.set(Some(latency));
+ }
+ }
+
/// The atlas texture only changes identity when the glyph cache is
/// rebuilt (atlas growth, DPI/font change), so the two sampler bind
/// groups are cached against it and rebuilt on identity change alone.
@@ -790,7 +807,8 @@ impl WebGpuState {
_ => {}
}
- if dims == *self.dimensions.borrow() {
+ let pending_latency = self.pending_frame_latency.get();
+ if dims == *self.dimensions.borrow() && pending_latency.is_none() {
return;
}
let old = *self.dimensions.borrow();
@@ -799,14 +817,29 @@ impl WebGpuState {
config.width = dims.pixel_width as u32;
config.height = dims.pixel_height as u32;
if config.width > 0 && config.height > 0 {
+ // The latency lands in the config only alongside the configure
+ // that applies it: writing it on the skipped zero-size path
+ // would desync the config from the real surface and make
+ // set_desired_frame_latency's equality check swallow a change
+ // that never took effect.
+ if let Some(latency) = pending_latency {
+ config.desired_maximum_frame_latency = latency;
+ }
gpu_debug(format!(
- "resize WebGpu surface {}x{} -> {}x{}",
- old.pixel_width, old.pixel_height, config.width, config.height
+ "resize WebGpu surface {}x{} -> {}x{} frame_latency={}",
+ old.pixel_width,
+ old.pixel_height,
+ config.width,
+ config.height,
+ config.desired_maximum_frame_latency
));
// Avoid reconfiguring with a 0 sized surface, as webgpu will
// panic in that case
// <https://github.com/wezterm/wezterm/issues/2881>
self.surface.configure(&self.device, &config);
+ // Consumed only by a real configure: a zero-sized (minimized)
+ // surface must keep the change pending for later.
+ self.pending_frame_latency.set(None);
}
}
}