From 35eed08d5feebd25e87668bc1fa69a1d4e0a9a0e Mon Sep 17 00:00:00 2001 From: Steins7 Date: Wed, 3 Feb 2021 11:52:22 +0100 Subject: [PATCH] Improved input lag * switched to sync_channel in WinitWindow + added framerate math (commented) * improved gpu selection algorithm --- main.rs | 2 +- src/controller.rs | 18 +++++++++++++++++- src/io/winit_window.rs | 13 ++++++++----- src/renderer/gpu.rs | 12 +++++++++++- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/main.rs b/main.rs index 2f78ccf..707e91b 100644 --- a/main.rs +++ b/main.rs @@ -25,7 +25,7 @@ fn setup_logger() -> Result<(), fern::InitError> { message )) }) - .level(log::LevelFilter::Trace) + .level(log::LevelFilter::Debug) .chain(std::io::stdout()) .chain(fern::log_file("output.log")?) .apply()?; diff --git a/src/controller.rs b/src/controller.rs index e747600..df6b70d 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -45,7 +45,7 @@ where } pub fn run(&mut self) { - use std::time::Duration; + use std::time::Duration; //, SystemTime}; use crate::{ subengine::subengine_controller::SubengineCommand, @@ -56,6 +56,8 @@ where let mut input_keys: Vec = Vec::new(); loop { +// let frame_start = SystemTime::now(); + for pipeline in &mut self.pipelines { for input in &pipeline.inputs { match input.borrow().read(Duration::from_millis(1)) { @@ -114,6 +116,20 @@ where }; }; }; + +// let frame_time = SystemTime::now() +// .duration_since(frame_start) +// .unwrap(); +// +// let sleep_time = Duration::from_secs_f32(1.0/60.0) +// .checked_sub(frame_time) +// .unwrap_or_else(|| { +// info!("Engine overloaded !"); +// Duration::from_secs(0) +// }); +// std::thread::sleep(sleep_time); +// +// info!("FPS : {}", 1.0/frame_time.as_secs_f32());// + sleep_time.as_secs_f32())); } } } diff --git a/src/io/winit_window.rs b/src/io/winit_window.rs index 3b3623d..809734d 100644 --- a/src/io/winit_window.rs +++ b/src/io/winit_window.rs @@ -61,7 +61,7 @@ impl WinitWindow { //Since we can't move the EventLoop from one thread to another, we need to create it in the //right thread and then move the Window back to the main thread instead let cloned_name = name.clone(); - let (tx, rx) = mpsc::channel(); + let (tx, rx) = mpsc::sync_channel(1); let (tmp_tx, tmp_rx) = mpsc::sync_channel(1); let builder = thread::Builder::new().name(title.into()); //the EventLoop hijacks the thread so there is no need to join it later... @@ -92,23 +92,25 @@ impl WinitWindow { match event { Event::LoopDestroyed => { - let _ = tx.send(Key::Closed).unwrap(); + tx.send(Key::Closed).unwrap(); debug!("Closed EventLoop"); return; }, + Event::WindowEvent{window_id: _, event} => match event { event::WindowEvent::CloseRequested => { debug!("Close requested"); - let _ = tx.send(Key::Close).unwrap(); + tx.send(Key::Close).unwrap(); }, event::WindowEvent::CursorMoved{position, ..} => { - tx.send(Key::MouseMove{ + let _ = tx.try_send(Key::MouseMove{ x: position.x, y: position.y, - }).unwrap(); + }); }, _ => (), }, + Event::UserEvent(signal) => match signal { Signal::Exit => { debug!("Stopping input thread..."); @@ -118,6 +120,7 @@ impl WinitWindow { tx.send(Key::Test).unwrap(); }, }, + _ => (), }}) }); diff --git a/src/renderer/gpu.rs b/src/renderer/gpu.rs index 31fcc7f..5284502 100644 --- a/src/renderer/gpu.rs +++ b/src/renderer/gpu.rs @@ -88,6 +88,7 @@ where use gfx_hal::{ Instance, window::Surface, + adapter::DeviceType, }; // dry run to print all adapters for debug purposes @@ -103,7 +104,7 @@ where let adapter = instance .enumerate_adapters() .into_iter() - .find(|a| { + .filter(|a| { a.queue_families .iter() .any(|qf| @@ -113,7 +114,16 @@ where .all(|surface| surface.supports_queue_family(&qf) ))}) + .max_by_key(|adapter| { + match adapter.info.device_type { + DeviceType::DiscreteGpu => 4, + DeviceType::IntegratedGpu => 3, + DeviceType::VirtualGpu => 2, + DeviceType::Other => 1, + DeviceType::Cpu => 0, + }}) .ok_or("Could not find a graphical adapter")?; + info!("Selected adapter : {}", adapter.info.name); adapter