-
Notifications
You must be signed in to change notification settings - Fork 38
/
main.rs
191 lines (164 loc) · 7.06 KB
/
main.rs
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
use std::iter;
use std::time::Instant;
use chrono::Timelike;
use egui::FontDefinitions;
use egui_wgpu_backend::{RenderPass, ScreenDescriptor};
use egui_winit_platform::{Platform, PlatformDescriptor};
use epi::*;
use futures_lite::future::block_on;
use winit::event::Event::*;
use winit::event_loop::ControlFlow;
const INITIAL_WIDTH: u32 = 1920;
const INITIAL_HEIGHT: u32 = 1080;
const OUTPUT_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8UnormSrgb;
/// A custom event type for the winit app.
enum Event {
RequestRedraw,
}
/// This is the repaint signal type that egui needs for requesting a repaint from another thread.
/// It sends the custom RequestRedraw event to the winit event loop.
struct ExampleRepaintSignal(std::sync::Mutex<winit::event_loop::EventLoopProxy<Event>>);
impl epi::RepaintSignal for ExampleRepaintSignal {
fn request_repaint(&self) {
self.0.lock().unwrap().send_event(Event::RequestRedraw).ok();
}
}
/// A simple egui + wgpu + winit based example.
fn main() {
let event_loop = winit::event_loop::EventLoop::with_user_event();
let window = winit::window::WindowBuilder::new()
.with_decorations(true)
.with_resizable(true)
.with_transparent(false)
.with_title("egui-wgpu_winit example")
.with_inner_size(winit::dpi::PhysicalSize {
width: INITIAL_WIDTH,
height: INITIAL_HEIGHT,
})
.build(&event_loop)
.unwrap();
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let surface = unsafe { instance.create_surface(&window) };
let adapter = block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
compatible_surface: Some(&surface),
}))
.unwrap();
let (mut device, mut queue) = block_on(adapter.request_device(
&wgpu::DeviceDescriptor {
features: wgpu::Features::default(),
limits: wgpu::Limits::default(),
label: None,
},
None,
))
.unwrap();
let size = window.inner_size();
let mut sc_desc = wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
format: OUTPUT_FORMAT,
width: size.width as u32,
height: size.height as u32,
present_mode: wgpu::PresentMode::Mailbox,
};
let mut swap_chain = device.create_swap_chain(&surface, &sc_desc);
let repaint_signal = std::sync::Arc::new(ExampleRepaintSignal(std::sync::Mutex::new(
event_loop.create_proxy(),
)));
// We use the egui_winit_platform crate as the platform.
let mut platform = Platform::new(PlatformDescriptor {
physical_width: size.width as u32,
physical_height: size.height as u32,
scale_factor: window.scale_factor(),
font_definitions: FontDefinitions::default(),
style: Default::default(),
});
// We use the egui_wgpu_backend crate as the render backend.
let mut egui_rpass = RenderPass::new(&device, OUTPUT_FORMAT);
// Display the demo application that ships with egui.
let mut demo_app = egui_demo_lib::WrapApp::default();
let start_time = Instant::now();
let mut previous_frame_time = None;
event_loop.run(move |event, _, control_flow| {
// Pass the winit events to the platform integration.
platform.handle_event(&event);
match event {
RedrawRequested(..) => {
platform.update_time(start_time.elapsed().as_secs_f64());
let output_frame = match swap_chain.get_current_frame() {
Ok(frame) => frame,
Err(e) => {
eprintln!("Dropped frame with error: {}", e);
return;
}
};
// Begin to draw the UI frame.
let egui_start = Instant::now();
platform.begin_frame();
let mut app_output = epi::backend::AppOutput::default();
let mut frame = epi::backend::FrameBuilder {
info: epi::IntegrationInfo {
web_info: None,
cpu_usage: previous_frame_time,
seconds_since_midnight: Some(seconds_since_midnight()),
native_pixels_per_point: Some(window.scale_factor() as _),
},
tex_allocator: Some(&mut egui_rpass),
output: &mut app_output,
repaint_signal: repaint_signal.clone(),
}
.build();
// Draw the demo application.
demo_app.update(&platform.context(), &mut frame);
// End the UI frame. We could now handle the output and draw the UI with the backend.
let (_output, paint_commands) = platform.end_frame();
let paint_jobs = platform.context().tessellate(paint_commands);
let frame_time = (Instant::now() - egui_start).as_secs_f64() as f32;
previous_frame_time = Some(frame_time);
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("encoder"),
});
// Upload all resources for the GPU.
let screen_descriptor = ScreenDescriptor {
physical_width: sc_desc.width,
physical_height: sc_desc.height,
scale_factor: window.scale_factor() as f32,
};
egui_rpass.update_texture(&device, &queue, &platform.context().texture());
egui_rpass.update_user_textures(&device, &queue);
egui_rpass.update_buffers(&mut device, &mut queue, &paint_jobs, &screen_descriptor);
// Record all render passes.
egui_rpass.execute(
&mut encoder,
&output_frame.output.view,
&paint_jobs,
&screen_descriptor,
Some(wgpu::Color::BLACK),
);
// Submit the commands.
queue.submit(iter::once(encoder.finish()));
*control_flow = ControlFlow::Poll;
}
MainEventsCleared | UserEvent(Event::RequestRedraw) => {
window.request_redraw();
}
WindowEvent { event, .. } => match event {
winit::event::WindowEvent::Resized(size) => {
sc_desc.width = size.width;
sc_desc.height = size.height;
swap_chain = device.create_swap_chain(&surface, &sc_desc);
}
winit::event::WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
_ => {}
},
_ => (),
}
});
}
/// Time of day as seconds since midnight. Used for clock in demo app.
pub fn seconds_since_midnight() -> f64 {
let time = chrono::Local::now().time();
time.num_seconds_from_midnight() as f64 + 1e-9 * (time.nanosecond() as f64)
}