306: Improve example friendliness: Don't busy-redraw in examples r=kvark a=khoek

Looking around for solid Rust graphics libraries, I was a bit shocked when the `wgpu-rs` examples made my whole X desktop environment in Ubuntu laggy---maximizing/minimizing other windows or moving any of them had very noticeable latency when an example was running.

This almost turned me off, but after playing around I found that this was just because of the
```rust
match event {
            event::Event::MainEventsCleared => window.request_redraw(),
            ...
```
in all of the examples. Trying to add the least code possible I've replaced a `ControlFlow::Poll` with `ControlFlow::WaitUntil(...)` in the event loops and capped the redraws-per-second below 50, which completely solves this problem for me (plus the window resize response on the examples themselves are much improved, etc.).

I was just going for an unintrusive fix---this is by no means a perfect solution, but I think in the worst case it won't be any worse that what was there originally. Plus, I think it will make people like me who try to start by copying an example more likely to stick around in the short term.

Co-authored-by: Keeley Hoek <keeley@hoek.io>
This commit is contained in:
bors[bot]
2020-05-11 21:35:47 +00:00
committed by GitHub
2 changed files with 24 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
use std::time;
use winit::{
event::{self, WindowEvent},
event_loop::{ControlFlow, EventLoop},
@@ -100,16 +101,37 @@ async fn run_async<E: Example>(event_loop: EventLoop<()>, window: Window) {
queue.submit(init_command_buf);
}
#[cfg(not(target_arch = "wasm32"))]
let mut last_update_inst = time::Instant::now();
log::info!("Entering render loop...");
event_loop.run(move |event, _, control_flow| {
let _ = (&instance, &adapter); // force ownership by the closure
*control_flow = if cfg!(feature = "metal-auto-capture") {
ControlFlow::Exit
} else {
ControlFlow::Poll
#[cfg(not(target_arch = "wasm32"))]
{
ControlFlow::WaitUntil(time::Instant::now() + time::Duration::from_millis(10))
}
#[cfg(target_arch = "wasm32")]
{
ControlFlow::Poll
}
};
match event {
event::Event::MainEventsCleared => window.request_redraw(),
event::Event::MainEventsCleared => {
#[cfg(not(target_arch = "wasm32"))]
{
if last_update_inst.elapsed() > time::Duration::from_millis(20) {
window.request_redraw();
last_update_inst = time::Instant::now();
}
}
#[cfg(target_arch = "wasm32")]
window.request_redraw();
}
event::Event::WindowEvent {
event: WindowEvent::Resized(size),
..

View File

@@ -100,7 +100,6 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
*control_flow = ControlFlow::Poll;
match event {
Event::MainEventsCleared => window.request_redraw(),
Event::WindowEvent {
event: WindowEvent::Resized(size),
..