Skip to content

Commit

Permalink
Added window framerate limits to mitigate KhronosGroup/Vulkan-Docs#1158
Browse files Browse the repository at this point in the history
Fix debuging autoenabling after first render
  • Loading branch information
funmaker committed Sep 2, 2021
1 parent bdf5a5e commit 3327fd6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ validation = false
gpu_id = 0
ssaa = 1
msaa = 2
window_max_fps = 60

[camera]
driver = "openvr"
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct Config {
pub camera: CameraConfig,
/// Non VR mode
pub novr: NovrConfig,
/// Window max framerate (0 - unlimited, not recommended)
pub window_max_fps: u32,
}

#[derive(Deserialize, Serialize, Debug, Clone, FromArgs)]
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,11 @@ impl Renderer {
.then_signal_fence_and_flush()?
.wait(None)?;

debug::set_debug(false); // Hide internal OpenVR warnings
let debug = debug::debug();
if debug { debug::set_debug(false); } // Hide internal OpenVR warnings
vr.compositor.submit(openvr::Eye::Left, &self.eyes.textures.0, None, Some(pose))?;
vr.compositor.submit(openvr::Eye::Right, &self.eyes.textures.1, None, Some(pose))?;
debug::set_debug(true);
if debug { debug::set_debug(true); }

future = sync::now(self.device.clone())
.then_execute(self.queue.clone(), OpenVRCommandBuffer::end(self.eyes.resolved_image.clone(), self.device.clone(), self.queue.family())?)?
Expand Down
19 changes: 16 additions & 3 deletions src/renderer/window.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;
use std::error::Error;
use std::time::Duration;
use std::time::{Instant, Duration};
use std::fmt::{Debug, Formatter};
use err_derive::Error;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent, MouseButton, DeviceEvent};
Expand All @@ -20,12 +20,13 @@ use winit::window::Window as WinitWindow;

use super::{Renderer, RendererSwapchainError};
use crate::math::Vec2;
use crate::debug;
use crate::{debug, config};


pub struct Window {
event_loop: EventLoop<()>,
surface: Arc<Surface<WinitWindow>>,
last_present: Instant,
pub swapchain: (Arc<Swapchain<WinitWindow>>, Vec<Arc<SwapchainImage<WinitWindow>>>),
pub swapchain_regen_required: bool,
pub render_required: bool,
Expand Down Expand Up @@ -65,6 +66,7 @@ impl Window {
event_loop,
surface,
swapchain,
last_present: Instant::now(),
swapchain_regen_required: false,
render_required: true,
quit_required: false,
Expand Down Expand Up @@ -95,7 +97,17 @@ impl Window {
-> Result<Box<dyn GpuFuture>, WindowRenderError> {
let (ref mut swapchain, ref mut images) = self.swapchain;

let timeout = (!self.render_required).then_some(Duration::new(0, 0));
let timeout = if !self.render_required {
let max_fps = config::get().window_max_fps;

if max_fps != 0 && self.last_present.elapsed().as_secs_f32() < 1.0 / max_fps as f32 {
return Err(WindowRenderError::Later(future));
} else {
Some(Duration::new(0, 0))
}
} else {
None
};

let (image_num, suboptimal, acquire_future) = match swapchain::acquire_next_image(swapchain.clone(), timeout) {
Err(AcquireError::OutOfDate) => {
Expand Down Expand Up @@ -147,6 +159,7 @@ impl Window {
let command_buffer = builder.build()?;

self.render_required = false;
self.last_present = Instant::now();

Ok(Box::new(future.join(acquire_future)
.then_execute(queue.clone(), command_buffer)?
Expand Down

0 comments on commit 3327fd6

Please sign in to comment.