mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
wgpu: Allow configuring VSync and frame latency at runtime (#8114)
Let apps change present_mode and desired_maximum_frame_latency at
runtime instead of only at startup.
API changes (egui-wgpu):
- New SurfaceConfig { present_mode, desired_maximum_frame_latency }.
- WgpuConfiguration now nests these as pub surface: SurfaceConfig (was
two top-level fields).
- RenderState gains pub surface_config: SurfaceConfig — the
currently-requested value.
API additions (eframe):
- Frame::wgpu_surface_config() / Frame::set_wgpu_surface_config(...) for
get/set.
- SurfaceConfig re-exported as eframe::SurfaceConfig.
How it works:
The wgpu painter compares render_state.surface_config to its
currently-applied values each paint. If they differ it updates its
config and flips
needs_reconfigure on every surface, piggybacking on the existing
deferred-reconfigure pathway.
Demo:
The backend panel (egui_demo_app) gets dropdowns for present mode and
desired max frame latency, wired through the new Frame accessors.
<img width="282" height="172" alt="image"
src="https://github.com/user-attachments/assets/0b1274b2-7e4e-4413-969b-0a014c415f79"
/>
This commit is contained in:
@@ -310,6 +310,11 @@ fn integration_ui(ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
|
||||
ui.end_row();
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(mut cfg) = _frame.wgpu_surface_config() {
|
||||
wgpu_surface_config_ui(ui, &mut cfg);
|
||||
_frame.set_wgpu_surface_config(cfg);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -357,6 +362,52 @@ fn integration_ui(ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "wgpu")]
|
||||
fn wgpu_surface_config_ui(ui: &mut egui::Ui, cfg: &mut eframe::SurfaceConfig) {
|
||||
use eframe::wgpu::PresentMode;
|
||||
|
||||
egui::Grid::new("wgpu_surface_config")
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Present mode:");
|
||||
egui::ComboBox::from_id_salt("wgpu_present_mode")
|
||||
.selected_text(format!("{:?}", cfg.present_mode))
|
||||
.show_ui(ui, |ui| {
|
||||
for mode in [
|
||||
PresentMode::AutoVsync,
|
||||
PresentMode::AutoNoVsync,
|
||||
PresentMode::Fifo,
|
||||
PresentMode::FifoRelaxed,
|
||||
PresentMode::Immediate,
|
||||
PresentMode::Mailbox,
|
||||
] {
|
||||
ui.selectable_value(&mut cfg.present_mode, mode, format!("{mode:?}"));
|
||||
}
|
||||
});
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Desired max frame latency:");
|
||||
egui::ComboBox::from_id_salt("wgpu_desired_max_frame_latency")
|
||||
.selected_text(match cfg.desired_maximum_frame_latency {
|
||||
None => "Default".to_owned(),
|
||||
Some(n) => n.to_string(),
|
||||
})
|
||||
.show_ui(ui, |ui| {
|
||||
ui.weak("Lower value = lower latency");
|
||||
ui.selectable_value(&mut cfg.desired_maximum_frame_latency, None, "Default");
|
||||
for n in [0_u32, 1, 2, 3] {
|
||||
ui.selectable_value(
|
||||
&mut cfg.desired_maximum_frame_latency,
|
||||
Some(n),
|
||||
n.to_string(),
|
||||
);
|
||||
}
|
||||
ui.weak("Higher value = higher throughput/FPS");
|
||||
});
|
||||
ui.end_row();
|
||||
});
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
||||
|
||||
Reference in New Issue
Block a user