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: bound the number of frames in flight on the WebGpu path

RoversX0c12ef2fca57
diff --git a/wezterm-gui/src/termwindow/render/draw.rs b/wezterm-gui/src/termwindow/render/draw.rs
index 602afc38..0d571df4 100644
--- a/wezterm-gui/src/termwindow/render/draw.rs
+++ b/wezterm-gui/src/termwindow/render/draw.rs
@@ -216,6 +216,9 @@ pub(crate) fn draw_webgpu_layers(
card_scratch: &mut Option<CardScratch>,
frame_verts: &mut Vec<crate::quad::Vertex>,
) -> anyhow::Result<()> {
+ // Back-pressure: do not let this frame's uploads pile onto a device that
+ // has not finished the frame before last. See `WebGpuState::in_flight_submissions`.
+ webgpu.wait_for_frame_slot();
let acquire_start = crate::perf::now();
let output = webgpu.surface.get_current_texture()?;
crate::perf::log_duration("webgpu_surface_acquire", acquire_start);
@@ -507,7 +510,8 @@ pub(crate) fn draw_webgpu_layers(
crate::perf::log_counter("webgpu_draw_calls", draw_calls);
crate::perf::log_counter("webgpu_vertices", vertices_total);
let submit_start = crate::perf::now();
- webgpu.queue.submit(std::iter::once(encoder.finish()));
+ let submission = webgpu.queue.submit(std::iter::once(encoder.finish()));
+ webgpu.note_submission(submission);
if crate::framedump::should_dump(window_label) {
if let Err(err) = crate::framedump::dump_texture(
&webgpu.device,
diff --git a/wezterm-gui/src/termwindow/webgpu.rs b/wezterm-gui/src/termwindow/webgpu.rs
index c655b09c..ac1a89b5 100644
--- a/wezterm-gui/src/termwindow/webgpu.rs
+++ b/wezterm-gui/src/termwindow/webgpu.rs
@@ -70,9 +70,25 @@ pub struct WebGpuState {
/// 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>>,
+ /// Submission indices of the most recent frames, oldest first, so the
+ /// paint path can bound how far the queue runs ahead of the device.
+ ///
+ /// Nothing else applies back-pressure: `queue.submit` never blocks, the
+ /// swapchain's frame latency only bounds *presents*, and every frame's
+ /// `write_buffer` calls mint staging buffers that wgpu cannot free until
+ /// that frame's fence completes. On a device that cannot keep up (WARP,
+ /// a busy integrated GPU) the backlog grew without limit: measured at
+ /// ~460 MB/min of staging under three fast-refreshing btop panes, with
+ /// the device up to 9 s behind. Waiting for the frame before last keeps
+ /// at most two frames of uploads alive.
+ in_flight_submissions: RefCell<std::collections::VecDeque<wgpu::SubmissionIndex>>,
pub handle: RawHandlePair,
}
+/// How many frames may be submitted ahead of the device before the paint
+/// path blocks on the oldest one. Two matches `desired_maximum_frame_latency`.
+pub const MAX_FRAMES_IN_FLIGHT: usize = 2;
+
pub struct RawHandlePair {
window: RawWindowHandle,
display: RawDisplayHandle,
@@ -659,9 +675,41 @@ impl WebGpuState {
frame_uniforms: RefCell::new(FrameUniforms::default()),
atlas_bind_groups: RefCell::new(None),
pending_frame_latency: Cell::new(None),
+ in_flight_submissions: RefCell::new(std::collections::VecDeque::with_capacity(
+ MAX_FRAMES_IN_FLIGHT + 1,
+ )),
})
}
+ /// Block until the device has finished the oldest frame still in flight,
+ /// when `MAX_FRAMES_IN_FLIGHT` frames are already queued ahead of it.
+ /// Call once per frame, before the frame's first `write_buffer`, so the
+ /// backlog of upload staging never exceeds that many frames.
+ pub fn wait_for_frame_slot(&self) {
+ let oldest = {
+ let mut queue = self.in_flight_submissions.borrow_mut();
+ if queue.len() < MAX_FRAMES_IN_FLIGHT {
+ return;
+ }
+ queue.pop_front()
+ };
+ if let Some(index) = oldest {
+ let started = crate::perf::now();
+ if let Err(err) = self
+ .device
+ .poll(wgpu::PollType::WaitForSubmissionIndex(index))
+ {
+ log::warn!("waiting for an in-flight frame submission: {err:?}");
+ }
+ crate::perf::log_duration("webgpu_frame_slot_wait", started);
+ }
+ }
+
+ /// Record a frame's submission index so `wait_for_frame_slot` can wait on it.
+ pub fn note_submission(&self, index: wgpu::SubmissionIndex) {
+ self.in_flight_submissions.borrow_mut().push_back(index);
+ }
+
/// 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.