From 705f22b60d6bf720b4bd696f43e69f1ffa057921 Mon Sep 17 00:00:00 2001 From: Kanabenki Date: Wed, 9 Aug 2023 22:36:12 +0200 Subject: [PATCH] Add present mode setters on Pixels (#373) * Add present mode setters on Pixels * Add present mode getter on Pixels --- src/lib.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index bf283dba..538ae9e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -382,6 +382,39 @@ impl Pixels { Ok(()) } + /// Enable or disable Vsync. + /// + /// Vsync is enabled by default. It cannot be disabled on Web targets. + /// + /// The `wgpu` present mode will be set to `AutoVsync` when Vsync is enabled, or `AutoNoVsync` + /// when Vsync is disabled. To set the present mode to `Mailbox` or another value, use the + /// [`Pixels::set_present_mode`] method. + pub fn enable_vsync(&mut self, enable_vsync: bool) { + self.present_mode = if enable_vsync { + wgpu::PresentMode::AutoVsync + } else { + wgpu::PresentMode::AutoNoVsync + }; + self.reconfigure_surface(); + } + + /// Get the `wgpu` present mode. + /// + /// Returns the present mode currently in use by the surface, which can be changed through + /// [`Pixels::enable_vsync`] or [`Pixels::set_present_mode`]. + pub fn present_mode(&self) -> wgpu::PresentMode { + self.present_mode + } + + /// Set the `wgpu` present mode. + /// + /// This differs from [`Pixels::enable_vsync`] by allowing the present mode to be set to + /// any value. + pub fn set_present_mode(&mut self, present_mode: wgpu::PresentMode) { + self.present_mode = present_mode; + self.reconfigure_surface(); + } + /// Draw this pixel buffer to the configured [`SurfaceTexture`]. /// /// # Errors