Skip to content

Commit

Permalink
vello_editor: Use PresentMode::Mailbox when available.
Browse files Browse the repository at this point in the history
AutoVsync/FIFO introduces unnecessary latency, especially on X11.
  • Loading branch information
xorgy committed Oct 22, 2024
1 parent 1fddc3a commit 9828c80
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions examples/vello_editor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,50 @@ 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),
PlainEditorOp::SetWidth(Some(size.width as f32 - 2f32 * text::INSET)),
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) {
Expand Down

0 comments on commit 9828c80

Please sign in to comment.