2022-04-29 06:17:49 +00:00
|
|
|
use super::epi_integration;
|
2022-04-30 08:44:35 +00:00
|
|
|
use crate::epi;
|
2022-01-24 16:08:27 +00:00
|
|
|
use egui_winit::winit;
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
struct RequestRepaintEvent;
|
|
|
|
|
2022-05-12 07:02:28 +00:00
|
|
|
#[cfg(feature = "glow")]
|
2021-10-18 21:13:32 +00:00
|
|
|
#[allow(unsafe_code)]
|
|
|
|
fn create_display(
|
2022-03-23 10:13:57 +00:00
|
|
|
native_options: &NativeOptions,
|
2022-01-24 16:08:27 +00:00
|
|
|
window_builder: winit::window::WindowBuilder,
|
|
|
|
event_loop: &winit::event_loop::EventLoop<RequestRepaintEvent>,
|
2021-10-18 21:13:32 +00:00
|
|
|
) -> (
|
|
|
|
glutin::WindowedContext<glutin::PossiblyCurrent>,
|
|
|
|
glow::Context,
|
|
|
|
) {
|
2022-04-13 09:06:13 +00:00
|
|
|
crate::profile_function!();
|
2021-10-18 21:13:32 +00:00
|
|
|
let gl_window = unsafe {
|
|
|
|
glutin::ContextBuilder::new()
|
2022-05-28 15:53:05 +00:00
|
|
|
.with_hardware_acceleration(native_options.hardware_acceleration)
|
2022-03-23 10:13:57 +00:00
|
|
|
.with_depth_buffer(native_options.depth_buffer)
|
|
|
|
.with_multisampling(native_options.multisampling)
|
2022-03-23 12:04:12 +00:00
|
|
|
.with_srgb(true)
|
2022-03-23 10:13:57 +00:00
|
|
|
.with_stencil_buffer(native_options.stencil_buffer)
|
|
|
|
.with_vsync(native_options.vsync)
|
2021-10-18 21:13:32 +00:00
|
|
|
.build_windowed(window_builder, event_loop)
|
|
|
|
.unwrap()
|
|
|
|
.make_current()
|
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
let gl = unsafe { glow::Context::from_loader_function(|s| gl_window.get_proc_address(s)) };
|
|
|
|
|
|
|
|
(gl_window, gl)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-10-19 19:40:55 +00:00
|
|
|
pub use epi::NativeOptions;
|
|
|
|
|
2021-10-18 21:13:32 +00:00
|
|
|
/// Run an egui app
|
2022-05-12 07:02:28 +00:00
|
|
|
#[cfg(feature = "glow")]
|
|
|
|
pub fn run_glow(
|
|
|
|
app_name: &str,
|
|
|
|
native_options: &epi::NativeOptions,
|
|
|
|
app_creator: epi::AppCreator,
|
|
|
|
) -> ! {
|
2022-04-29 06:17:49 +00:00
|
|
|
let storage = epi_integration::create_storage(app_name);
|
|
|
|
let window_settings = epi_integration::load_window_settings(storage.as_deref());
|
2022-05-12 07:02:28 +00:00
|
|
|
let event_loop = winit::event_loop::EventLoop::with_user_event();
|
|
|
|
|
2021-10-19 19:40:55 +00:00
|
|
|
let window_builder =
|
2022-04-29 06:17:49 +00:00
|
|
|
epi_integration::window_builder(native_options, &window_settings).with_title(app_name);
|
2022-03-23 10:13:57 +00:00
|
|
|
let (gl_window, gl) = create_display(native_options, window_builder, &event_loop);
|
2022-05-22 15:43:30 +00:00
|
|
|
let gl = std::sync::Arc::new(gl);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2022-04-29 06:17:49 +00:00
|
|
|
let mut painter = egui_glow::Painter::new(gl.clone(), None, "")
|
2022-02-01 11:27:39 +00:00
|
|
|
.unwrap_or_else(|error| panic!("some OpenGL error occurred {}\n", error));
|
2022-03-16 14:39:48 +00:00
|
|
|
|
2022-04-29 06:17:49 +00:00
|
|
|
let mut integration = epi_integration::EpiIntegration::new(
|
2022-05-22 18:24:41 +00:00
|
|
|
&event_loop,
|
2022-01-24 13:32:36 +00:00
|
|
|
painter.max_texture_side(),
|
2021-11-03 12:45:51 +00:00
|
|
|
gl_window.window(),
|
2022-03-25 20:19:31 +00:00
|
|
|
storage,
|
2022-05-12 07:02:28 +00:00
|
|
|
Some(gl.clone()),
|
2022-05-28 15:52:36 +00:00
|
|
|
#[cfg(feature = "wgpu")]
|
|
|
|
None,
|
2021-11-03 12:45:51 +00:00
|
|
|
);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2022-03-15 16:21:52 +00:00
|
|
|
{
|
2022-03-22 14:34:21 +00:00
|
|
|
let event_loop_proxy = egui::mutex::Mutex::new(event_loop.create_proxy());
|
2022-03-15 16:21:52 +00:00
|
|
|
integration.egui_ctx.set_request_repaint_callback(move || {
|
|
|
|
event_loop_proxy.lock().send_event(RequestRepaintEvent).ok();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-16 14:39:48 +00:00
|
|
|
let mut app = app_creator(&epi::CreationContext {
|
|
|
|
egui_ctx: integration.egui_ctx.clone(),
|
|
|
|
integration_info: integration.frame.info(),
|
2022-03-25 20:19:31 +00:00
|
|
|
storage: integration.frame.storage(),
|
2022-05-12 07:02:28 +00:00
|
|
|
gl: Some(gl.clone()),
|
2022-05-28 15:52:36 +00:00
|
|
|
#[cfg(feature = "wgpu")]
|
|
|
|
render_state: None,
|
2022-03-16 14:39:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if app.warm_up_enabled() {
|
|
|
|
integration.warm_up(app.as_mut(), gl_window.window());
|
|
|
|
}
|
|
|
|
|
2021-10-18 21:13:32 +00:00
|
|
|
let mut is_focused = true;
|
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
2022-05-12 07:02:28 +00:00
|
|
|
let window = gl_window.window();
|
|
|
|
|
2021-10-18 21:13:32 +00:00
|
|
|
let mut redraw = || {
|
2022-04-13 09:06:13 +00:00
|
|
|
#[cfg(feature = "puffin")]
|
|
|
|
puffin::GlobalProfiler::lock().new_frame();
|
|
|
|
crate::profile_scope!("frame");
|
2022-05-12 07:02:28 +00:00
|
|
|
|
|
|
|
let screen_size_in_pixels: [u32; 2] = window.inner_size().into();
|
2022-03-27 13:20:45 +00:00
|
|
|
|
2022-04-30 13:41:43 +00:00
|
|
|
egui_glow::painter::clear(
|
|
|
|
&gl,
|
|
|
|
screen_size_in_pixels,
|
|
|
|
app.clear_color(&integration.egui_ctx.style().visuals),
|
|
|
|
);
|
2022-03-27 13:20:45 +00:00
|
|
|
|
2022-02-22 16:13:53 +00:00
|
|
|
let egui::FullOutput {
|
|
|
|
platform_output,
|
|
|
|
needs_repaint,
|
|
|
|
textures_delta,
|
|
|
|
shapes,
|
2022-05-12 07:02:28 +00:00
|
|
|
} = integration.update(app.as_mut(), window);
|
2022-02-22 16:13:53 +00:00
|
|
|
|
2022-05-12 07:02:28 +00:00
|
|
|
integration.handle_platform_output(window, platform_output);
|
2022-02-22 16:13:53 +00:00
|
|
|
|
2022-04-13 09:06:13 +00:00
|
|
|
let clipped_primitives = {
|
|
|
|
crate::profile_scope!("tessellate");
|
|
|
|
integration.egui_ctx.tessellate(shapes)
|
|
|
|
};
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2022-03-27 13:20:45 +00:00
|
|
|
painter.paint_and_update_textures(
|
|
|
|
screen_size_in_pixels,
|
|
|
|
integration.egui_ctx.pixels_per_point(),
|
|
|
|
&clipped_primitives,
|
|
|
|
&textures_delta,
|
|
|
|
);
|
|
|
|
|
2022-04-13 09:06:13 +00:00
|
|
|
{
|
|
|
|
crate::profile_scope!("swap_buffers");
|
|
|
|
gl_window.swap_buffers().unwrap();
|
|
|
|
}
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2022-05-12 07:02:28 +00:00
|
|
|
*control_flow = if integration.should_quit() {
|
|
|
|
winit::event_loop::ControlFlow::Exit
|
|
|
|
} else if needs_repaint {
|
|
|
|
window.request_redraw();
|
|
|
|
winit::event_loop::ControlFlow::Poll
|
|
|
|
} else {
|
|
|
|
winit::event_loop::ControlFlow::Wait
|
|
|
|
};
|
|
|
|
|
|
|
|
integration.maybe_autosave(app.as_mut(), window);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2022-05-12 07:02:28 +00:00
|
|
|
if !is_focused {
|
|
|
|
// On Mac, a minimized Window uses up all CPU: https://github.com/emilk/egui/issues/325
|
|
|
|
// We can't know if we are minimized: https://github.com/rust-windowing/winit/issues/208
|
|
|
|
// But we know if we are focused (in foreground). When minimized, we are not focused.
|
|
|
|
// However, a user may want an egui with an animation in the background,
|
|
|
|
// so we still need to repaint quite fast.
|
|
|
|
crate::profile_scope!("bg_sleep");
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
|
|
}
|
2021-10-18 21:13:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match event {
|
|
|
|
// Platform-dependent event handlers to workaround a winit bug
|
|
|
|
// See: https://github.com/rust-windowing/winit/issues/987
|
|
|
|
// See: https://github.com/rust-windowing/winit/issues/1619
|
2022-01-24 16:08:27 +00:00
|
|
|
winit::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
|
|
|
|
winit::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2022-01-24 16:08:27 +00:00
|
|
|
winit::event::Event::WindowEvent { event, .. } => {
|
2022-05-12 07:02:28 +00:00
|
|
|
match &event {
|
|
|
|
winit::event::WindowEvent::Focused(new_focused) => {
|
|
|
|
is_focused = *new_focused;
|
|
|
|
}
|
|
|
|
winit::event::WindowEvent::Resized(physical_size) => {
|
|
|
|
// Resize with 0 width and height is used by winit to signal a minimize event on Windows.
|
|
|
|
// See: https://github.com/rust-windowing/winit/issues/208
|
|
|
|
// This solves an issue where the app would panic when minimizing on Windows.
|
|
|
|
if physical_size.width > 0 && physical_size.height > 0 {
|
|
|
|
gl_window.resize(*physical_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
winit::event::WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
|
|
|
gl_window.resize(**new_inner_size);
|
|
|
|
}
|
|
|
|
winit::event::WindowEvent::CloseRequested => {
|
|
|
|
*control_flow = winit::event_loop::ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
_ => {}
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 07:02:28 +00:00
|
|
|
integration.on_event(app.as_mut(), &event);
|
|
|
|
if integration.should_quit() {
|
|
|
|
*control_flow = winit::event_loop::ControlFlow::Exit;
|
2021-10-18 21:39:33 +00:00
|
|
|
}
|
2022-05-21 14:53:25 +00:00
|
|
|
window.request_redraw(); // TODO(emilk): ask egui if the events warrants a repaint instead
|
2022-05-12 07:02:28 +00:00
|
|
|
}
|
|
|
|
winit::event::Event::LoopDestroyed => {
|
|
|
|
integration.save(&mut *app, window);
|
|
|
|
app.on_exit(Some(&gl));
|
|
|
|
painter.destroy();
|
|
|
|
}
|
|
|
|
winit::event::Event::UserEvent(RequestRepaintEvent) => window.request_redraw(),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-21 14:53:25 +00:00
|
|
|
// TODO(emilk): merge with with the clone above
|
2022-05-12 07:02:28 +00:00
|
|
|
/// Run an egui app
|
|
|
|
#[cfg(feature = "wgpu")]
|
|
|
|
pub fn run_wgpu(
|
|
|
|
app_name: &str,
|
|
|
|
native_options: &epi::NativeOptions,
|
|
|
|
app_creator: epi::AppCreator,
|
|
|
|
) -> ! {
|
|
|
|
let storage = epi_integration::create_storage(app_name);
|
|
|
|
let window_settings = epi_integration::load_window_settings(storage.as_deref());
|
|
|
|
let event_loop = winit::event_loop::EventLoop::with_user_event();
|
|
|
|
|
|
|
|
let window = epi_integration::window_builder(native_options, &window_settings)
|
|
|
|
.with_title(app_name)
|
|
|
|
.build(&event_loop)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// SAFETY: `window` must outlive `painter`.
|
|
|
|
#[allow(unsafe_code)]
|
|
|
|
let mut painter = unsafe {
|
2022-05-22 18:24:41 +00:00
|
|
|
let mut painter = egui_wgpu::winit::Painter::new(
|
|
|
|
wgpu::Backends::PRIMARY | wgpu::Backends::GL,
|
|
|
|
wgpu::PowerPreference::HighPerformance,
|
|
|
|
wgpu::DeviceDescriptor {
|
|
|
|
label: None,
|
|
|
|
features: wgpu::Features::default(),
|
|
|
|
limits: wgpu::Limits::default(),
|
|
|
|
},
|
|
|
|
wgpu::PresentMode::Fifo,
|
|
|
|
native_options.multisampling.max(1) as _,
|
|
|
|
);
|
|
|
|
#[cfg(not(target_os = "android"))]
|
|
|
|
painter.set_window(Some(&window));
|
|
|
|
painter
|
2022-05-12 07:02:28 +00:00
|
|
|
};
|
|
|
|
|
2022-05-28 15:52:36 +00:00
|
|
|
let render_state = painter.get_render_state().expect("Uninitialized");
|
|
|
|
|
2022-05-12 07:02:28 +00:00
|
|
|
let mut integration = epi_integration::EpiIntegration::new(
|
2022-05-22 18:24:41 +00:00
|
|
|
&event_loop,
|
|
|
|
painter.max_texture_side().unwrap_or(2048),
|
2022-05-12 07:02:28 +00:00
|
|
|
&window,
|
|
|
|
storage,
|
|
|
|
#[cfg(feature = "glow")]
|
|
|
|
None,
|
2022-05-28 15:52:36 +00:00
|
|
|
Some(render_state.clone()),
|
2022-05-12 07:02:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
{
|
|
|
|
let event_loop_proxy = egui::mutex::Mutex::new(event_loop.create_proxy());
|
|
|
|
integration.egui_ctx.set_request_repaint_callback(move || {
|
|
|
|
event_loop_proxy.lock().send_event(RequestRepaintEvent).ok();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut app = app_creator(&epi::CreationContext {
|
|
|
|
egui_ctx: integration.egui_ctx.clone(),
|
|
|
|
integration_info: integration.frame.info(),
|
|
|
|
storage: integration.frame.storage(),
|
|
|
|
#[cfg(feature = "glow")]
|
|
|
|
gl: None,
|
2022-05-28 15:52:36 +00:00
|
|
|
render_state: Some(render_state),
|
2022-05-12 07:02:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if app.warm_up_enabled() {
|
|
|
|
integration.warm_up(app.as_mut(), &window);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut is_focused = true;
|
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
let window = &window;
|
|
|
|
|
|
|
|
let mut redraw = || {
|
|
|
|
#[cfg(feature = "puffin")]
|
|
|
|
puffin::GlobalProfiler::lock().new_frame();
|
|
|
|
crate::profile_scope!("frame");
|
|
|
|
|
|
|
|
let egui::FullOutput {
|
|
|
|
platform_output,
|
|
|
|
needs_repaint,
|
|
|
|
textures_delta,
|
|
|
|
shapes,
|
|
|
|
} = integration.update(app.as_mut(), window);
|
|
|
|
|
|
|
|
integration.handle_platform_output(window, platform_output);
|
|
|
|
|
|
|
|
let clipped_primitives = {
|
|
|
|
crate::profile_scope!("tessellate");
|
|
|
|
integration.egui_ctx.tessellate(shapes)
|
|
|
|
};
|
|
|
|
|
|
|
|
painter.paint_and_update_textures(
|
|
|
|
integration.egui_ctx.pixels_per_point(),
|
|
|
|
app.clear_color(&integration.egui_ctx.style().visuals),
|
|
|
|
&clipped_primitives,
|
|
|
|
&textures_delta,
|
|
|
|
);
|
|
|
|
|
|
|
|
*control_flow = if integration.should_quit() {
|
|
|
|
winit::event_loop::ControlFlow::Exit
|
|
|
|
} else if needs_repaint {
|
|
|
|
window.request_redraw();
|
|
|
|
winit::event_loop::ControlFlow::Poll
|
|
|
|
} else {
|
|
|
|
winit::event_loop::ControlFlow::Wait
|
|
|
|
};
|
|
|
|
|
|
|
|
integration.maybe_autosave(app.as_mut(), window);
|
|
|
|
|
|
|
|
if !is_focused {
|
|
|
|
// On Mac, a minimized Window uses up all CPU: https://github.com/emilk/egui/issues/325
|
|
|
|
// We can't know if we are minimized: https://github.com/rust-windowing/winit/issues/208
|
|
|
|
// But we know if we are focused (in foreground). When minimized, we are not focused.
|
|
|
|
// However, a user may want an egui with an animation in the background,
|
|
|
|
// so we still need to repaint quite fast.
|
|
|
|
crate::profile_scope!("bg_sleep");
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match event {
|
|
|
|
// Platform-dependent event handlers to workaround a winit bug
|
|
|
|
// See: https://github.com/rust-windowing/winit/issues/987
|
|
|
|
// See: https://github.com/rust-windowing/winit/issues/1619
|
|
|
|
winit::event::Event::RedrawEventsCleared if cfg!(windows) => redraw(),
|
|
|
|
winit::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
|
|
|
|
|
2022-05-22 18:24:41 +00:00
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
winit::event::Event::Resumed => unsafe {
|
|
|
|
painter.set_window(Some(&window));
|
|
|
|
},
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
winit::event::Event::Paused => unsafe {
|
|
|
|
painter.set_window(None);
|
|
|
|
},
|
|
|
|
|
2022-05-12 07:02:28 +00:00
|
|
|
winit::event::Event::WindowEvent { event, .. } => {
|
|
|
|
match &event {
|
|
|
|
winit::event::WindowEvent::Focused(new_focused) => {
|
|
|
|
is_focused = *new_focused;
|
|
|
|
}
|
|
|
|
winit::event::WindowEvent::Resized(physical_size) => {
|
|
|
|
// Resize with 0 width and height is used by winit to signal a minimize event on Windows.
|
|
|
|
// See: https://github.com/rust-windowing/winit/issues/208
|
|
|
|
// This solves an issue where the app would panic when minimizing on Windows.
|
|
|
|
if physical_size.width > 0 && physical_size.height > 0 {
|
|
|
|
painter.on_window_resized(physical_size.width, physical_size.height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
winit::event::WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
|
|
|
painter.on_window_resized(new_inner_size.width, new_inner_size.height);
|
|
|
|
}
|
|
|
|
winit::event::WindowEvent::CloseRequested => {
|
|
|
|
*control_flow = winit::event_loop::ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
};
|
2021-10-18 21:39:33 +00:00
|
|
|
|
2022-03-16 14:39:48 +00:00
|
|
|
integration.on_event(app.as_mut(), &event);
|
2021-11-03 12:45:51 +00:00
|
|
|
if integration.should_quit() {
|
2022-01-24 16:08:27 +00:00
|
|
|
*control_flow = winit::event_loop::ControlFlow::Exit;
|
2021-11-03 12:45:51 +00:00
|
|
|
}
|
2022-05-21 14:53:25 +00:00
|
|
|
window.request_redraw(); // TODO(emilk): ask egui if the events warrants a repaint instead
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
2022-01-24 16:08:27 +00:00
|
|
|
winit::event::Event::LoopDestroyed => {
|
2022-05-12 07:02:28 +00:00
|
|
|
integration.save(&mut *app, window);
|
|
|
|
|
|
|
|
#[cfg(feature = "glow")]
|
|
|
|
app.on_exit(None);
|
|
|
|
|
|
|
|
#[cfg(not(feature = "glow"))]
|
|
|
|
app.on_exit();
|
|
|
|
|
2022-03-14 12:25:11 +00:00
|
|
|
painter.destroy();
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
2022-05-12 07:02:28 +00:00
|
|
|
winit::event::Event::UserEvent(RequestRepaintEvent) => window.request_redraw(),
|
2021-10-18 21:13:32 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|