From 9828c80334d0a79349d92882e55092670a21bfa2 Mon Sep 17 00:00:00 2001 From: Aaron Muir Hamilton Date: Tue, 22 Oct 2024 14:40:51 -0400 Subject: [PATCH] vello_editor: Use PresentMode::Mailbox when available. AutoVsync/FIFO introduces unnecessary latency, especially on X11. --- examples/vello_editor/src/main.rs | 36 +++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/examples/vello_editor/src/main.rs b/examples/vello_editor/src/main.rs index e00d8dc..4735914 100644 --- a/examples/vello_editor/src/main.rs +++ b/examples/vello_editor/src/main.rs @@ -62,15 +62,7 @@ impl ApplicationHandler for SimpleVelloApp<'_> { .take() .unwrap_or_else(|| create_winit_window(event_loop)); - // Create a vello Surface let size = window.inner_size(); - let surface_future = self.context.create_surface( - window.clone(), - size.width, - size.height, - wgpu::PresentMode::AutoVsync, - ); - let surface = pollster::block_on(surface_future).expect("Error creating surface"); self.editor.transact([ PlainEditorOp::SetScale(1.0), @@ -78,16 +70,42 @@ impl ApplicationHandler for SimpleVelloApp<'_> { PlainEditorOp::SetText(text::LOREM.into()), ]); + // Create a vello Surface + let surface_future = { + let surface = self + .context + .instance + .create_surface(wgpu::SurfaceTarget::from(window.clone())) + .expect("Error creating surface"); + let dev_id = pollster::block_on(self.context.device(Some(&surface))) + .expect("No compatible device"); + let device_handle = &self.context.devices[dev_id]; + let capabilities = surface.get_capabilities(device_handle.adapter()); + let present_mode = if capabilities + .present_modes + .contains(&wgpu::PresentMode::Mailbox) + { + wgpu::PresentMode::Mailbox + } else { + wgpu::PresentMode::AutoVsync + }; + + self.context + .create_render_surface(surface, size.width, size.height, present_mode) + }; + let surface = pollster::block_on(surface_future).expect("Error creating surface"); + // Create a vello Renderer for the surface (using its device id) self.renderers .resize_with(self.context.devices.len(), || None); + self.renderers[surface.dev_id] .get_or_insert_with(|| create_vello_renderer(&self.context, &surface)); // Save the Window and Surface to a state variable self.state = RenderState::Active(ActiveRenderState { window, surface }); - event_loop.set_control_flow(ControlFlow::Poll); + event_loop.set_control_flow(ControlFlow::Wait); } fn suspended(&mut self, event_loop: &ActiveEventLoop) {