Skip to content

Commit 1df41b7

Browse files
authored
Expose desired_maximum_frame_latency through window creation (#12954)
# Objective - Closes #12930. ## Solution - Add a corresponding optional field on `Window` and `ExtractedWindow` --- ## Changelog ### Added - `wgpu`'s `desired_maximum_frame_latency` is exposed through window creation. This can be used to override the default maximum number of queued frames on the GPU (currently 2). ## Migration Guide - The `desired_maximum_frame_latency` field must be added to instances of `Window` and `ExtractedWindow` where all fields are explicitly specified.
1 parent e9fb82a commit 1df41b7

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

crates/bevy_render/src/view/window/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use bevy_window::{
1515
CompositeAlphaMode, PresentMode, PrimaryWindow, RawHandleWrapper, Window, WindowClosed,
1616
};
1717
use std::{
18+
num::NonZeroU32,
1819
ops::{Deref, DerefMut},
1920
sync::PoisonError,
2021
};
@@ -66,6 +67,7 @@ pub struct ExtractedWindow {
6667
pub physical_width: u32,
6768
pub physical_height: u32,
6869
pub present_mode: PresentMode,
70+
pub desired_maximum_frame_latency: Option<NonZeroU32>,
6971
/// Note: this will not always be the swap chain texture view. When taking a screenshot,
7072
/// this will point to an alternative texture instead to allow for copying the render result
7173
/// to CPU memory.
@@ -136,6 +138,7 @@ fn extract_windows(
136138
physical_width: new_width,
137139
physical_height: new_height,
138140
present_mode: window.present_mode,
141+
desired_maximum_frame_latency: window.desired_maximum_frame_latency,
139142
swap_chain_texture: None,
140143
swap_chain_texture_view: None,
141144
size_changed: false,
@@ -429,6 +432,12 @@ pub fn need_surface_configuration(
429432
false
430433
}
431434

435+
// 2 is wgpu's default/what we've been using so far.
436+
// 1 is the minimum, but may cause lower framerates due to the cpu waiting for the gpu to finish
437+
// all work for the previous frame before starting work on the next frame, which then means the gpu
438+
// has to wait for the cpu to finish to start on the next frame.
439+
const DEFAULT_DESIRED_MAXIMUM_FRAME_LATENCY: u32 = 2;
440+
432441
/// Creates window surfaces.
433442
pub fn create_surfaces(
434443
// By accessing a NonSend resource, we tell the scheduler to put this system on the main thread,
@@ -488,12 +497,10 @@ pub fn create_surfaces(
488497
PresentMode::AutoVsync => wgpu::PresentMode::AutoVsync,
489498
PresentMode::AutoNoVsync => wgpu::PresentMode::AutoNoVsync,
490499
},
491-
// TODO: Expose this as a setting somewhere
492-
// 2 is wgpu's default/what we've been using so far.
493-
// 1 is the minimum, but may cause lower framerates due to the cpu waiting for the gpu to finish
494-
// all work for the previous frame before starting work on the next frame, which then means the gpu
495-
// has to wait for the cpu to finish to start on the next frame.
496-
desired_maximum_frame_latency: 2,
500+
desired_maximum_frame_latency: window
501+
.desired_maximum_frame_latency
502+
.map(NonZeroU32::get)
503+
.unwrap_or(DEFAULT_DESIRED_MAXIMUM_FRAME_LATENCY),
497504
alpha_mode: match window.alpha_mode {
498505
CompositeAlphaMode::Auto => wgpu::CompositeAlphaMode::Auto,
499506
CompositeAlphaMode::Opaque => wgpu::CompositeAlphaMode::Opaque,

crates/bevy_window/src/window.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::num::NonZeroU32;
2+
13
use bevy_ecs::{
24
entity::{Entity, EntityMapper, MapEntities},
35
prelude::{Component, ReflectComponent},
@@ -270,6 +272,15 @@ pub struct Window {
270272
///
271273
/// - Only supported on Windows.
272274
pub skip_taskbar: bool,
275+
/// Optional hint given to the rendering API regarding the maximum number of queued frames admissible on the GPU.
276+
///
277+
/// Given values are usually within the 1-3 range. If not provided, this will default to 2.
278+
///
279+
/// See [`wgpu::SurfaceConfiguration::desired_maximum_frame_latency`].
280+
///
281+
/// [`wgpu::SurfaceConfiguration::desired_maximum_frame_latency`]:
282+
/// https://docs.rs/wgpu/latest/wgpu/type.SurfaceConfiguration.html#structfield.desired_maximum_frame_latency
283+
pub desired_maximum_frame_latency: Option<NonZeroU32>,
273284
}
274285

275286
impl Default for Window {
@@ -299,6 +310,7 @@ impl Default for Window {
299310
window_theme: None,
300311
visible: true,
301312
skip_taskbar: false,
313+
desired_maximum_frame_latency: None,
302314
}
303315
}
304316
}

0 commit comments

Comments
 (0)