Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Replace VSync with PresentMode #3812

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions crates/bevy_render/src/view/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*;
use bevy_utils::{tracing::debug, HashMap, HashSet};
use bevy_window::{RawWindowHandleWrapper, WindowId, Windows};
use bevy_window::{PresentMode, RawWindowHandleWrapper, WindowId, Windows};
use std::ops::{Deref, DerefMut};
use wgpu::TextureFormat;

Expand Down Expand Up @@ -43,7 +43,7 @@ pub struct ExtractedWindow {
pub handle: RawWindowHandleWrapper,
pub physical_width: u32,
pub physical_height: u32,
pub vsync: bool,
pub present_mode: PresentMode,
pub swap_chain_texture: Option<TextureView>,
pub size_changed: bool,
}
Expand Down Expand Up @@ -83,7 +83,7 @@ fn extract_windows(mut render_world: ResMut<RenderWorld>, windows: Res<Windows>)
handle: window.raw_window_handle(),
physical_width: new_width,
physical_height: new_height,
vsync: window.vsync(),
present_mode: window.present_mode(),
swap_chain_texture: None,
size_changed: false,
});
Expand Down Expand Up @@ -138,10 +138,10 @@ pub fn prepare_windows(
width: window.physical_width,
height: window.physical_height,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
present_mode: if window.vsync {
wgpu::PresentMode::Fifo
} else {
wgpu::PresentMode::Immediate
present_mode: match window.present_mode {
PresentMode::Fifo => wgpu::PresentMode::Fifo,
PresentMode::Mailbox => wgpu::PresentMode::Mailbox,
PresentMode::Immediate => wgpu::PresentMode::Immediate,
},
};

Expand Down
43 changes: 32 additions & 11 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ use raw_window_handle::RawWindowHandle;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct WindowId(Uuid);

/// Behavior of the presentation engine based on frame rate.
aloucks marked this conversation as resolved.
Show resolved Hide resolved
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum PresentMode {
aloucks marked this conversation as resolved.
Show resolved Hide resolved
/// The presentation engine does **not** wait for a vertical blanking period and
/// the request is presented immediately. This is a low-latency presentation mode,
/// but visible tearing may be observed. Will fallback to `Fifo` if unavailable on the
/// selected platform and backend. Not optimal for mobile.
aloucks marked this conversation as resolved.
Show resolved Hide resolved
Immediate = 0,
aloucks marked this conversation as resolved.
Show resolved Hide resolved
/// The presentation engine waits for the next vertical blanking period to update
/// the current image, but frames may be submitted without delay. This is a low-latency
/// presentation mode and visible tearing will **not** be observed. Will fallback to `Fifo`
/// if unavailable on the selected platform and backend. Not optimal for mobile.
Mailbox = 1,
/// The presentation engine waits for the next vertical blanking period to update
/// the current image. The framerate will be capped at the display refresh rate,
/// corresponding to the `VSync`. Tearing cannot be observed. Optimal for mobile.
Fifo = 2,
}

impl WindowId {
pub fn new() -> Self {
WindowId(Uuid::new_v4())
Expand Down Expand Up @@ -121,7 +141,7 @@ pub struct Window {
scale_factor_override: Option<f64>,
backend_scale_factor: f64,
title: String,
vsync: bool,
present_mode: PresentMode,
resizable: bool,
decorations: bool,
cursor_icon: CursorIcon,
Expand Down Expand Up @@ -152,8 +172,8 @@ pub enum WindowCommand {
logical_resolution: (f32, f32),
scale_factor: f64,
},
SetVsync {
vsync: bool,
SetPresentMode {
present_mode: PresentMode,
},
SetResizable {
resizable: bool,
Expand Down Expand Up @@ -222,7 +242,7 @@ impl Window {
scale_factor_override: window_descriptor.scale_factor_override,
backend_scale_factor: scale_factor,
title: window_descriptor.title.clone(),
vsync: window_descriptor.vsync,
present_mode: window_descriptor.present_mode,
resizable: window_descriptor.resizable,
decorations: window_descriptor.decorations,
cursor_visible: window_descriptor.cursor_visible,
Expand Down Expand Up @@ -425,14 +445,15 @@ impl Window {
}

#[inline]
pub fn vsync(&self) -> bool {
self.vsync
pub fn present_mode(&self) -> PresentMode {
self.present_mode
}

#[inline]
pub fn set_vsync(&mut self, vsync: bool) {
self.vsync = vsync;
self.command_queue.push(WindowCommand::SetVsync { vsync });
pub fn set_present_mode(&mut self, present_mode: PresentMode) {
self.present_mode = present_mode;
self.command_queue
.push(WindowCommand::SetPresentMode { present_mode });
}

#[inline]
Expand Down Expand Up @@ -557,7 +578,7 @@ pub struct WindowDescriptor {
pub resize_constraints: WindowResizeConstraints,
pub scale_factor_override: Option<f64>,
pub title: String,
pub vsync: bool,
pub present_mode: PresentMode,
pub resizable: bool,
pub decorations: bool,
pub cursor_visible: bool,
Expand All @@ -584,7 +605,7 @@ impl Default for WindowDescriptor {
position: None,
resize_constraints: WindowResizeConstraints::default(),
scale_factor_override: None,
vsync: true,
present_mode: PresentMode::Fifo,
resizable: true,
decorations: true,
cursor_locked: false,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn change_window(world: &mut World) {
.to_physical::<f64>(scale_factor),
);
}
bevy_window::WindowCommand::SetVsync { .. } => (),
bevy_window::WindowCommand::SetPresentMode { .. } => (),
bevy_window::WindowCommand::SetResizable { resizable } => {
let window = winit_windows.get_window(id).unwrap();
window.set_resizable(resizable);
Expand Down
8 changes: 6 additions & 2 deletions examples/ios/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use bevy::{input::touch::TouchPhase, prelude::*, window::WindowMode};
use bevy::{
input::touch::TouchPhase,
prelude::*,
window::{PresentMode, WindowMode},
};

// the `bevy_main` proc_macro generates the required ios boilerplate
#[bevy_main]
fn main() {
App::new()
.insert_resource(WindowDescriptor {
vsync: true,
present_mode: PresentMode::Fifo,
aloucks marked this conversation as resolved.
Show resolved Hide resolved
resizable: false,
mode: WindowMode::BorderlessFullscreen,
..Default::default()
Expand Down
3 changes: 2 additions & 1 deletion examples/tools/bevymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy::{
core::FixedTimestep,
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
window::PresentMode,
};
use rand::random;

Expand All @@ -28,7 +29,7 @@ fn main() {
title: "BevyMark".to_string(),
width: 800.,
height: 600.,
vsync: false,
present_mode: PresentMode::Mailbox,
resizable: true,
..Default::default()
})
Expand Down
3 changes: 2 additions & 1 deletion examples/ui/text_debug.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use bevy::{
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin},
prelude::*,
window::PresentMode,
};

/// This example is for debugging text layout
fn main() {
App::new()
.insert_resource(WindowDescriptor {
vsync: false,
present_mode: PresentMode::Immediate,
..Default::default()
})
.add_plugins(DefaultPlugins)
Expand Down
4 changes: 2 additions & 2 deletions examples/window/multiple_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy::{
renderer::RenderContext,
RenderApp, RenderStage,
},
window::{CreateWindow, WindowId},
window::{CreateWindow, PresentMode, WindowId},
};

/// This example creates a second window and draws a mesh from two different cameras, one in each window
Expand Down Expand Up @@ -57,7 +57,7 @@ fn create_new_window(
descriptor: WindowDescriptor {
width: 800.,
height: 600.,
vsync: false,
present_mode: PresentMode::Immediate,
title: "Second window".to_string(),
..Default::default()
},
Expand Down
4 changes: 2 additions & 2 deletions examples/window/window_settings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::{prelude::*, window::PresentMode};

/// This example illustrates how to customize the default window settings
fn main() {
Expand All @@ -7,7 +7,7 @@ fn main() {
title: "I am a window!".to_string(),
width: 500.,
height: 300.,
vsync: true,
present_mode: PresentMode::Fifo,
..Default::default()
})
.add_plugins(DefaultPlugins)
Expand Down