-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathpure_glow.rs
More file actions
339 lines (296 loc) · 12.4 KB
/
pure_glow.rs
File metadata and controls
339 lines (296 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! Example how to use pure `egui_glow`.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![expect(rustdoc::missing_crate_level_docs, clippy::unwrap_used)] // it's an example
#![expect(clippy::undocumented_unsafe_blocks)]
#![expect(unsafe_code)]
use std::num::NonZeroU32;
use std::sync::Arc;
use egui_winit::winit;
use winit::raw_window_handle::HasWindowHandle as _;
/// The majority of `GlutinWindowContext` is taken from `eframe`
struct GlutinWindowContext {
window: winit::window::Window,
gl_context: glutin::context::PossiblyCurrentContext,
gl_display: glutin::display::Display,
gl_surface: glutin::surface::Surface<glutin::surface::WindowSurface>,
}
impl GlutinWindowContext {
// refactor this function to use `glutin-winit` crate eventually.
// preferably add android support at the same time.
#[expect(unsafe_code)]
unsafe fn new(event_loop: &winit::event_loop::ActiveEventLoop) -> Self {
use glutin::context::NotCurrentGlContext as _;
use glutin::display::GetGlDisplay as _;
use glutin::display::GlDisplay as _;
use glutin::prelude::GlSurface as _;
let winit_window_builder = winit::window::WindowAttributes::default()
.with_resizable(true)
.with_inner_size(winit::dpi::LogicalSize {
width: 800.0,
height: 600.0,
})
.with_title("egui_glow example") // Keep hidden until we've painted something. See https://github.com/emilk/egui/pull/2279
.with_visible(false);
let config_template_builder = glutin::config::ConfigTemplateBuilder::new()
.prefer_hardware_accelerated(None)
.with_depth_size(0)
.with_stencil_size(0)
.with_transparency(false);
log::debug!("trying to get gl_config");
let (mut window, gl_config) =
glutin_winit::DisplayBuilder::new() // let glutin-winit helper crate handle the complex parts of opengl context creation
.with_preference(glutin_winit::ApiPreference::FallbackEgl) // https://github.com/emilk/egui/issues/2520#issuecomment-1367841150
.with_window_attributes(Some(winit_window_builder.clone()))
.build(
event_loop,
config_template_builder,
|mut config_iterator| {
config_iterator.next().expect(
"failed to find a matching configuration for creating glutin config",
)
},
)
.expect("failed to create gl_config");
let gl_display = gl_config.display();
log::debug!("found gl_config: {gl_config:?}");
let raw_window_handle = window.as_ref().map(|w| {
w.window_handle()
.expect("failed to get window handle")
.as_raw()
});
log::debug!("raw window handle: {raw_window_handle:?}");
let context_attributes =
glutin::context::ContextAttributesBuilder::new().build(raw_window_handle);
// by default, glutin will try to create a core opengl context. but, if it is not available, try to create a gl-es context using this fallback attributes
let fallback_context_attributes = glutin::context::ContextAttributesBuilder::new()
.with_context_api(glutin::context::ContextApi::Gles(None))
.build(raw_window_handle);
let not_current_gl_context = unsafe {
gl_display
.create_context(&gl_config, &context_attributes)
.unwrap_or_else(|_| {
log::debug!("failed to create gl_context with attributes: {context_attributes:?}. retrying with fallback context attributes: {fallback_context_attributes:?}");
gl_config
.display()
.create_context(&gl_config, &fallback_context_attributes)
.expect("failed to create context even with fallback attributes")
})
};
// this is where the window is created, if it has not been created while searching for suitable gl_config
let window = window.take().unwrap_or_else(|| {
log::debug!("window doesn't exist yet. creating one now with finalize_window");
glutin_winit::finalize_window(event_loop, winit_window_builder.clone(), &gl_config)
.expect("failed to finalize glutin window")
});
let (width, height): (u32, u32) = window.inner_size().into();
let width = NonZeroU32::new(width).unwrap_or(NonZeroU32::MIN);
let height = NonZeroU32::new(height).unwrap_or(NonZeroU32::MIN);
let surface_attributes =
glutin::surface::SurfaceAttributesBuilder::<glutin::surface::WindowSurface>::new()
.build(
window
.window_handle()
.expect("failed to get window handle")
.as_raw(),
width,
height,
);
log::debug!("creating surface with attributes: {surface_attributes:?}");
let gl_surface = unsafe {
gl_display
.create_window_surface(&gl_config, &surface_attributes)
.unwrap()
};
log::debug!("surface created successfully: {gl_surface:?}.making context current");
let gl_context = not_current_gl_context.make_current(&gl_surface).unwrap();
gl_surface
.set_swap_interval(
&gl_context,
glutin::surface::SwapInterval::Wait(NonZeroU32::MIN),
)
.unwrap();
Self {
window,
gl_context,
gl_display,
gl_surface,
}
}
fn window(&self) -> &winit::window::Window {
&self.window
}
fn resize(&self, physical_size: winit::dpi::PhysicalSize<u32>) {
use glutin::surface::GlSurface as _;
self.gl_surface.resize(
&self.gl_context,
physical_size.width.try_into().unwrap(),
physical_size.height.try_into().unwrap(),
);
}
fn swap_buffers(&self) -> glutin::error::Result<()> {
use glutin::surface::GlSurface as _;
self.gl_surface.swap_buffers(&self.gl_context)
}
fn get_proc_address(&self, addr: &std::ffi::CStr) -> *const std::ffi::c_void {
use glutin::display::GlDisplay as _;
self.gl_display.get_proc_address(addr)
}
}
#[derive(Debug)]
pub enum UserEvent {
Redraw(std::time::Duration),
}
struct GlowApp {
proxy: winit::event_loop::EventLoopProxy<UserEvent>,
gl_window: Option<GlutinWindowContext>,
gl: Option<Arc<glow::Context>>,
egui_glow: Option<egui_glow::EguiGlow>,
repaint_delay: std::time::Duration,
clear_color: [f32; 3],
}
impl GlowApp {
fn new(proxy: winit::event_loop::EventLoopProxy<UserEvent>) -> Self {
Self {
proxy,
gl_window: None,
gl: None,
egui_glow: None,
repaint_delay: std::time::Duration::MAX,
clear_color: [0.1, 0.1, 0.1],
}
}
}
impl winit::application::ApplicationHandler<UserEvent> for GlowApp {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
let (gl_window, gl) = create_display(event_loop);
let gl = std::sync::Arc::new(gl);
gl_window.window().set_visible(true);
let egui_glow = egui_glow::EguiGlow::new(event_loop, Arc::clone(&gl), None, None, true);
let event_loop_proxy = egui::mutex::Mutex::new(self.proxy.clone());
egui_glow
.egui_ctx
.set_request_repaint_callback(move |info| {
event_loop_proxy
.lock()
.send_event(UserEvent::Redraw(info.delay))
.expect("Cannot send event");
});
self.gl_window = Some(gl_window);
self.gl = Some(gl);
self.egui_glow = Some(egui_glow);
}
fn window_event(
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
_window_id: winit::window::WindowId,
event: winit::event::WindowEvent,
) {
let mut redraw = || {
let mut quit = false;
self.egui_glow
.as_mut()
.unwrap()
.run(self.gl_window.as_mut().unwrap().window(), |ui| {
egui::Panel::left("my_side_panel").show(ui, |ui| {
ui.heading("Hello World!");
if ui.button("Quit").clicked() {
quit = true;
}
ui.color_edit_button_rgb(self.clear_color.as_mut().try_into().unwrap());
});
});
if quit {
event_loop.exit();
} else {
event_loop.set_control_flow(if self.repaint_delay.is_zero() {
self.gl_window.as_mut().unwrap().window().request_redraw();
winit::event_loop::ControlFlow::Poll
} else if let Some(repaint_after_instant) =
std::time::Instant::now().checked_add(self.repaint_delay)
{
winit::event_loop::ControlFlow::WaitUntil(repaint_after_instant)
} else {
winit::event_loop::ControlFlow::Wait
});
}
{
unsafe {
use glow::HasContext as _;
self.gl.as_mut().unwrap().clear_color(
self.clear_color[0],
self.clear_color[1],
self.clear_color[2],
1.0,
);
self.gl.as_mut().unwrap().clear(glow::COLOR_BUFFER_BIT);
}
// draw things behind egui here
self.egui_glow
.as_mut()
.unwrap()
.paint(self.gl_window.as_mut().unwrap().window());
// draw things on top of egui here
self.gl_window.as_mut().unwrap().swap_buffers().unwrap();
self.gl_window.as_mut().unwrap().window().set_visible(true);
}
};
use winit::event::WindowEvent;
if matches!(event, WindowEvent::CloseRequested | WindowEvent::Destroyed) {
event_loop.exit();
return;
}
if matches!(event, WindowEvent::RedrawRequested) {
redraw();
return;
}
if let winit::event::WindowEvent::Resized(physical_size) = &event {
self.gl_window.as_mut().unwrap().resize(*physical_size);
}
let event_response = self
.egui_glow
.as_mut()
.unwrap()
.on_window_event(self.gl_window.as_mut().unwrap().window(), &event);
if event_response.repaint {
self.gl_window.as_mut().unwrap().window().request_redraw();
}
}
fn user_event(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop, event: UserEvent) {
match event {
UserEvent::Redraw(delay) => self.repaint_delay = delay,
}
}
fn new_events(
&mut self,
_event_loop: &winit::event_loop::ActiveEventLoop,
cause: winit::event::StartCause,
) {
if let winit::event::StartCause::ResumeTimeReached { .. } = &cause {
self.gl_window.as_mut().unwrap().window().request_redraw();
}
}
fn exiting(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop) {
self.egui_glow.as_mut().unwrap().destroy();
}
}
fn main() {
let event_loop = winit::event_loop::EventLoop::<UserEvent>::with_user_event()
.build()
.unwrap();
let proxy = event_loop.create_proxy();
let mut app = GlowApp::new(proxy);
event_loop.run_app(&mut app).expect("failed to run app");
}
fn create_display(
event_loop: &winit::event_loop::ActiveEventLoop,
) -> (GlutinWindowContext, glow::Context) {
let glutin_window_context = unsafe { GlutinWindowContext::new(event_loop) };
let gl = unsafe {
glow::Context::from_loader_function(|s| {
let s = std::ffi::CString::new(s)
.expect("failed to construct C string from string for gl proc address");
glutin_window_context.get_proc_address(&s)
})
};
(glutin_window_context, gl)
}