Skip to content

Commit

Permalink
Add WgpuConfiguration::desired_maximum_frame_latency, default to 1
Browse files Browse the repository at this point in the history
This should lead to lower latency in wgpu apps
  • Loading branch information
emilk committed Jan 23, 2024
1 parent aa22704 commit 2bb7b30
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
27 changes: 24 additions & 3 deletions crates/egui-wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ pub struct WgpuConfiguration {
/// Present mode used for the primary surface.
pub present_mode: wgpu::PresentMode,

/// Desired maximum number of frames that the presentation engine should queue in advance.
///
/// Use `1` for low-latency, and `2` for high-throughput.
///
/// See [`wgpu::SurfaceConfiguration::desired_num_frames`] for details.
pub desired_maximum_frame_latency: u32,

/// Power preference for the adapter.
pub power_preference: wgpu::PowerPreference,

Expand All @@ -237,10 +244,22 @@ pub struct WgpuConfiguration {

impl std::fmt::Debug for WgpuConfiguration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self {
supported_backends,
device_descriptor: _,
present_mode,
desired_maximum_frame_latency,
power_preference,
on_surface_error: _,
} = self;
f.debug_struct("WgpuConfiguration")
.field("supported_backends", &self.supported_backends)
.field("present_mode", &self.present_mode)
.field("power_preference", &self.power_preference)
.field("supported_backends", &supported_backends)
.field("present_mode", &present_mode)
.field(
"desired_maximum_frame_latency",
&desired_maximum_frame_latency,
)
.field("power_preference", &power_preference)
.finish_non_exhaustive()
}
}
Expand Down Expand Up @@ -274,6 +293,8 @@ impl Default for WgpuConfiguration {

present_mode: wgpu::PresentMode::AutoVsync,

desired_maximum_frame_latency: 1, // 1 = low-latency; good for GUIs.

power_preference: wgpu::util::power_preference_from_env()
.unwrap_or(wgpu::PowerPreference::HighPerformance),

Expand Down
14 changes: 5 additions & 9 deletions crates/egui-wgpu/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Painter {
fn configure_surface(
surface_state: &SurfaceState,
render_state: &RenderState,
present_mode: wgpu::PresentMode,
config: &WgpuConfiguration,
) {
crate::profile_function!();

Expand All @@ -158,10 +158,10 @@ impl Painter {
surface_state.surface.configure(
&render_state.device,
&wgpu::SurfaceConfiguration {
// TODO(emilk): expose `desired_maximum_frame_latency` to eframe users
usage,
format: render_state.target_format,
present_mode,
present_mode: config.present_mode,
desired_maximum_frame_latency: config.desired_maximum_frame_latency,
alpha_mode: surface_state.alpha_mode,
view_formats: vec![render_state.target_format],
..surface_state
Expand Down Expand Up @@ -328,7 +328,7 @@ impl Painter {
surface_state.width = width;
surface_state.height = height;

Self::configure_surface(surface_state, render_state, self.configuration.present_mode);
Self::configure_surface(surface_state, render_state, &self.configuration);

if let Some(depth_format) = self.depth_format {
self.depth_texture_view.insert(
Expand Down Expand Up @@ -525,11 +525,7 @@ impl Painter {
Ok(frame) => frame,
Err(err) => match (*self.configuration.on_surface_error)(err) {
SurfaceErrorAction::RecreateSurface => {
Self::configure_surface(
surface_state,
render_state,
self.configuration.present_mode,
);
Self::configure_surface(surface_state, render_state, &self.configuration);
return None;
}
SurfaceErrorAction::SkipFrame => {
Expand Down

0 comments on commit 2bb7b30

Please sign in to comment.