From 51cc6930ac3f35cebb00fce9309dda46a0f9d681 Mon Sep 17 00:00:00 2001 From: Jean Mertz Date: Sun, 17 May 2020 22:28:10 +0200 Subject: [PATCH] expose vsync toggle --- README.md | 1 + examples/color.rs | 1 + examples/counter.rs | 1 + examples/gamepad.rs | 1 + examples/image.rs | 1 + examples/input.rs | 1 + examples/mesh.rs | 1 + examples/particles.rs | 1 + examples/progress.rs | 1 + examples/rectangle.rs | 1 + examples/snake.rs | 4 +++- examples/ui.rs | 1 + src/graphics/backend_gfx/mod.rs | 8 ++++---- src/graphics/backend_gfx/surface.rs | 7 ++++--- src/graphics/backend_wgpu/mod.rs | 8 +++++--- src/graphics/backend_wgpu/surface.rs | 17 ++++++++++++++--- src/graphics/window.rs | 3 +-- src/graphics/window/settings.rs | 5 ++++- src/lib.rs | 1 + tests/graphics.rs | 1 + 20 files changed, 48 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e7c5205..a791fcb 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ fn main() -> Result<()> { resizable: true, fullscreen: false, maximized: false, + vsync: false, }) } diff --git a/examples/color.rs b/examples/color.rs index dbf8950..57d9da6 100644 --- a/examples/color.rs +++ b/examples/color.rs @@ -12,6 +12,7 @@ fn main() -> Result<()> { resizable: false, fullscreen: false, maximized: false, + vsync: false, }) } diff --git a/examples/counter.rs b/examples/counter.rs index 102b5fa..baf38fa 100644 --- a/examples/counter.rs +++ b/examples/counter.rs @@ -16,6 +16,7 @@ pub fn main() -> Result<()> { resizable: false, fullscreen: false, maximized: false, + vsync: false, }) } diff --git a/examples/gamepad.rs b/examples/gamepad.rs index 96e0e1a..531d4fa 100644 --- a/examples/gamepad.rs +++ b/examples/gamepad.rs @@ -14,6 +14,7 @@ fn main() -> Result<()> { resizable: false, fullscreen: false, maximized: false, + vsync: false, }) } diff --git a/examples/image.rs b/examples/image.rs index 704eaaf..c2732bd 100644 --- a/examples/image.rs +++ b/examples/image.rs @@ -15,6 +15,7 @@ pub fn main() -> Result<()> { resizable: false, fullscreen: false, maximized: false, + vsync: false, }) } diff --git a/examples/input.rs b/examples/input.rs index 75615b9..f904d99 100644 --- a/examples/input.rs +++ b/examples/input.rs @@ -19,6 +19,7 @@ fn main() -> Result<()> { resizable: false, fullscreen: false, maximized: false, + vsync: false, }) } diff --git a/examples/mesh.rs b/examples/mesh.rs index 83ffd7f..4b34583 100644 --- a/examples/mesh.rs +++ b/examples/mesh.rs @@ -19,6 +19,7 @@ fn main() -> Result<()> { resizable: false, fullscreen: false, maximized: false, + vsync: false, }) } diff --git a/examples/particles.rs b/examples/particles.rs index 073d656..fb5ef6b 100644 --- a/examples/particles.rs +++ b/examples/particles.rs @@ -21,6 +21,7 @@ fn main() -> Result<()> { resizable: false, fullscreen: false, maximized: false, + vsync: false, }) } diff --git a/examples/progress.rs b/examples/progress.rs index ff56b5e..86c244a 100644 --- a/examples/progress.rs +++ b/examples/progress.rs @@ -15,6 +15,7 @@ pub fn main() -> Result<()> { resizable: false, fullscreen: false, maximized: false, + vsync: false, }) } diff --git a/examples/rectangle.rs b/examples/rectangle.rs index ae821fc..7ae0fe6 100644 --- a/examples/rectangle.rs +++ b/examples/rectangle.rs @@ -11,6 +11,7 @@ fn main() -> coffee::Result<()> { resizable: true, fullscreen: false, maximized: false, + vsync: false, }) } diff --git a/examples/snake.rs b/examples/snake.rs index 59bd1e3..4cef685 100644 --- a/examples/snake.rs +++ b/examples/snake.rs @@ -1,7 +1,8 @@ extern crate coffee; use coffee::graphics::{ - Color, Font, Frame, Mesh, Point, Rectangle, Shape, Text, Window, WindowSettings, + Color, Font, Frame, Mesh, Point, Rectangle, Shape, Text, Window, + WindowSettings, }; use coffee::input::keyboard::KeyCode; use coffee::input::{self, keyboard, Input}; @@ -18,6 +19,7 @@ fn main() { resizable: false, maximized: false, fullscreen: false, + vsync: false, }) .expect("An error occured while starting the game"); } diff --git a/examples/ui.rs b/examples/ui.rs index 0d1c00c..f54394e 100644 --- a/examples/ui.rs +++ b/examples/ui.rs @@ -15,6 +15,7 @@ fn main() -> Result<()> { resizable: false, fullscreen: false, maximized: false, + vsync: false, }) } diff --git a/src/graphics/backend_gfx/mod.rs b/src/graphics/backend_gfx/mod.rs index 98ef7ef..c9d84c1 100644 --- a/src/graphics/backend_gfx/mod.rs +++ b/src/graphics/backend_gfx/mod.rs @@ -16,7 +16,7 @@ pub use types::TargetView; use gfx::{self, Device}; use gfx_device_gl as gl; -use crate::graphics::{Color, Transformation}; +use crate::graphics::{Color, Transformation, WindowSettings}; use crate::Result; /// A link between your game and a graphics processor. @@ -40,11 +40,11 @@ pub struct Gpu { impl Gpu { pub(super) fn for_window( - builder: winit::window::WindowBuilder, - events_loop: &winit::event_loop::EventLoop<()>, + settings: WindowSettings, + event_loop: &winit::event_loop::EventLoop<()>, ) -> Result<(Gpu, Surface)> { let (surface, device, mut factory) = - Surface::new(builder, events_loop)?; + Surface::new(settings, event_loop)?; let mut encoder: gfx::Encoder = factory.create_command_buffer().into(); diff --git a/src/graphics/backend_gfx/surface.rs b/src/graphics/backend_gfx/surface.rs index c310425..1a93572 100644 --- a/src/graphics/backend_gfx/surface.rs +++ b/src/graphics/backend_gfx/surface.rs @@ -1,7 +1,7 @@ use gfx_device_gl as gl; use super::{format, Gpu, TargetView}; -use crate::{Error, Result}; +use crate::{graphics::WindowSettings, Error, Result}; pub struct Surface { context: glutin::WindowedContext, @@ -10,7 +10,7 @@ pub struct Surface { impl Surface { pub(super) fn new( - builder: winit::window::WindowBuilder, + settings: WindowSettings, event_loop: &winit::event_loop::EventLoop<()>, ) -> Result<(Self, gl::Device, gl::Factory)> { let gl_builder = glutin::ContextBuilder::new() @@ -19,8 +19,9 @@ impl Surface { .with_multisampling(0) // 24 color bits, 8 alpha bits .with_pixel_format(24, 8) - .with_vsync(true); + .with_vsync(settings.vsync); + let builder = settings.into_builder(event_loop); let (context, device, factory, target, _depth) = init_raw( builder, gl_builder, diff --git a/src/graphics/backend_wgpu/mod.rs b/src/graphics/backend_wgpu/mod.rs index 56b3e86..1c9a6a4 100644 --- a/src/graphics/backend_wgpu/mod.rs +++ b/src/graphics/backend_wgpu/mod.rs @@ -12,7 +12,7 @@ pub use texture::Texture; pub use triangle::Vertex; pub use types::TargetView; -use crate::graphics::{Color, Transformation}; +use crate::graphics::{Color, Transformation, WindowSettings}; use crate::{Error, Result}; #[allow(missing_debug_implementations)] @@ -27,9 +27,11 @@ pub struct Gpu { impl Gpu { pub(super) fn for_window( - builder: winit::window::WindowBuilder, + settings: WindowSettings, event_loop: &winit::event_loop::EventLoop<()>, ) -> Result<(Gpu, Surface)> { + let vsync = settings.vsync; + let builder = settings.into_builder(event_loop); let window = builder .build(event_loop) .map_err(|error| Error::WindowCreation(error.to_string()))?; @@ -57,7 +59,7 @@ impl Gpu { (device, queue) }); - let surface = Surface::new(window, &device); + let surface = Surface::new(window, &device, vsync); let quad_pipeline = quad::Pipeline::new(&mut device); let triangle_pipeline = triangle::Pipeline::new(&mut device); diff --git a/src/graphics/backend_wgpu/surface.rs b/src/graphics/backend_wgpu/surface.rs index c2363b5..5695f48 100644 --- a/src/graphics/backend_wgpu/surface.rs +++ b/src/graphics/backend_wgpu/surface.rs @@ -6,17 +6,20 @@ pub struct Surface { swap_chain: wgpu::SwapChain, extent: wgpu::Extent3d, output: Option, + vsync: bool, } impl Surface { pub fn new( window: winit::window::Window, device: &wgpu::Device, + vsync: bool, ) -> Surface { let surface = wgpu::Surface::create(&window); let size = window.inner_size(); - let (swap_chain, extent) = new_swap_chain(device, &surface, size); + let (swap_chain, extent) = + new_swap_chain(device, &surface, size, vsync); Surface { window, @@ -24,6 +27,7 @@ impl Surface { swap_chain, extent, output: None, + vsync, } } @@ -50,7 +54,7 @@ impl Surface { size: winit::dpi::PhysicalSize, ) { let (swap_chain, extent) = - new_swap_chain(&gpu.device, &self.surface, size); + new_swap_chain(&gpu.device, &self.surface, size, self.vsync); self.swap_chain = swap_chain; self.extent = extent; @@ -82,7 +86,14 @@ fn new_swap_chain( device: &wgpu::Device, surface: &wgpu::Surface, size: winit::dpi::PhysicalSize, + vsync: bool, ) -> (wgpu::SwapChain, wgpu::Extent3d) { + let present_mode = if vsync { + wgpu::PresentMode::Mailbox + } else { + wgpu::PresentMode::Fifo + }; + let swap_chain = device.create_swap_chain( surface, &wgpu::SwapChainDescriptor { @@ -90,7 +101,7 @@ fn new_swap_chain( format: wgpu::TextureFormat::Bgra8UnormSrgb, width: size.width, height: size.height, - present_mode: wgpu::PresentMode::Mailbox, + present_mode, }, ); diff --git a/src/graphics/window.rs b/src/graphics/window.rs index 995b74c..f52faa1 100644 --- a/src/graphics/window.rs +++ b/src/graphics/window.rs @@ -33,8 +33,7 @@ impl Window { let (width, height) = settings.size; let is_fullscreen = settings.fullscreen; - let (gpu, surface) = - Gpu::for_window(settings.into_builder(event_loop), event_loop)?; + let (gpu, surface) = Gpu::for_window(settings, event_loop)?; Ok(Window { is_fullscreen, diff --git a/src/graphics/window/settings.rs b/src/graphics/window/settings.rs index 4aebb1b..df24b28 100644 --- a/src/graphics/window/settings.rs +++ b/src/graphics/window/settings.rs @@ -17,10 +17,13 @@ pub struct Settings { /// Defines whether or not the window should start maximized. pub maximized: bool, + + /// Defines whether or not to enable vsync. + pub vsync: bool, } impl Settings { - pub(super) fn into_builder( + pub(crate) fn into_builder( self, events_loop: &winit::event_loop::EventLoop<()>, ) -> winit::window::WindowBuilder { diff --git a/src/lib.rs b/src/lib.rs index 9b6bcdc..e3c5363 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,6 +42,7 @@ //! resizable: true, //! fullscreen: false, //! maximized: false, +//! vsync: false, //! }) //! } //! diff --git a/tests/graphics.rs b/tests/graphics.rs index 9912772..16d450c 100644 --- a/tests/graphics.rs +++ b/tests/graphics.rs @@ -23,6 +23,7 @@ fn graphics() -> Result<()> { resizable: false, fullscreen: false, maximized: false, + vsync: false, }) }