use std::time::{Duration, Instant};
pub use strata_render::swapchain::{
PresentError, SoftwareSurfacePresenter, SurfacePresenter, SwapchainCompositor, SwapchainFormat,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum PresentMode {
Immediate,
Mailbox,
#[default]
Fifo,
FifoRelaxed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum RefreshRate {
#[default]
Hz60,
Hz90,
Hz120,
Hz144,
Custom(u32),
}
impl RefreshRate {
pub const fn as_hz(&self) -> u32 {
match self {
Self::Hz60 => 60,
Self::Hz90 => 90,
Self::Hz120 => 120,
Self::Hz144 => 144,
Self::Custom(hz) => *hz,
}
}
pub fn frame_interval(&self) -> Duration {
let hz = self.as_hz().max(1);
Duration::from_nanos(1_000_000_000 / hz as u64)
}
}
#[derive(Debug, Clone)]
pub struct SurfaceConfig {
pub width: u32,
pub height: u32,
pub scale_factor: f32,
pub format: SwapchainFormat,
pub present_mode: PresentMode,
pub refresh_rate: RefreshRate,
}
impl Default for SurfaceConfig {
fn default() -> Self {
Self {
width: 1280,
height: 800,
scale_factor: 1.0,
format: SwapchainFormat::Bgra8Unorm,
present_mode: PresentMode::Fifo,
refresh_rate: RefreshRate::Hz60,
}
}
}
impl SurfaceConfig {
pub fn physical_width(&self) -> u32 {
((self.width as f32 * self.scale_factor) + 0.5) as u32
}
pub fn physical_height(&self) -> u32 {
((self.height as f32 * self.scale_factor) + 0.5) as u32
}
pub fn physical_size(&self) -> (u32, u32) {
(self.physical_width(), self.physical_height())
}
pub fn buffer_byte_size(&self) -> usize {
let (pw, ph) = self.physical_size();
(pw as usize) * (ph as usize) * self.format.bytes_per_pixel()
}
}
#[derive(Debug, Clone)]
pub struct FramePacer {
pub target_refresh: RefreshRate,
pub frame_interval: Duration,
pub last_frame_instant: Option<Instant>,
pub average_frame_duration: Duration,
pub frame_count: u64,
}
impl FramePacer {
pub fn new(target_refresh: RefreshRate) -> Self {
let frame_interval = target_refresh.frame_interval();
Self {
target_refresh,
frame_interval,
last_frame_instant: None,
average_frame_duration: Duration::from_micros(500),
frame_count: 0,
}
}
pub fn begin_frame(&mut self) -> Instant {
Instant::now()
}
pub fn end_frame(&mut self, frame_start: Instant) -> Duration {
let duration = frame_start.elapsed();
self.frame_count += 1;
self.last_frame_instant = Some(Instant::now());
let alpha = 0.2;
let prev_micros = self.average_frame_duration.as_micros() as f64;
let curr_micros = duration.as_micros() as f64;
let new_micros = (alpha * curr_micros + (1.0 - alpha) * prev_micros).max(10.0);
self.average_frame_duration = Duration::from_micros(new_micros as u64);
duration
}
pub fn duration_until_next_deadline(&self) -> Duration {
if let Some(last) = self.last_frame_instant {
let elapsed = last.elapsed();
if elapsed < self.frame_interval {
self.frame_interval - elapsed
} else {
Duration::ZERO
}
} else {
Duration::ZERO
}
}
pub fn wait_vsync(&mut self) {
#[cfg(windows)]
{
unsafe {
extern "system" {
fn LoadLibraryA(lpLibFileName: *const u8) -> *mut std::ffi::c_void;
fn GetProcAddress(
hModule: *mut std::ffi::c_void,
lpProcName: *const u8,
) -> *mut std::ffi::c_void;
fn FreeLibrary(hLibModule: *mut std::ffi::c_void) -> i32;
}
let dwmapi = LoadLibraryA(b"dwmapi.dll\0".as_ptr());
if !dwmapi.is_null() {
let proc = GetProcAddress(dwmapi, b"DwmFlush\0".as_ptr());
if !proc.is_null() {
let dwm_flush: unsafe extern "system" fn() -> i32 =
std::mem::transmute(proc);
let _ = dwm_flush();
}
FreeLibrary(dwmapi);
}
}
}
let remaining = self.duration_until_next_deadline();
if remaining > Duration::from_micros(500) {
std::thread::sleep(remaining - Duration::from_micros(250));
}
while self.duration_until_next_deadline() > Duration::ZERO {
std::hint::spin_loop();
}
self.last_frame_instant = Some(Instant::now());
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SurfaceHandle {
pub window_id: u64,
pub raw_window_ptr: usize,
pub raw_display_ptr: usize,
pub width: u32,
pub height: u32,
pub scale_factor_snapped: u32, }
impl SurfaceHandle {
pub fn new_headless(width: u32, height: u32, scale_factor: f32) -> Self {
Self {
window_id: 1,
raw_window_ptr: 0,
raw_display_ptr: 0,
width,
height,
scale_factor_snapped: (scale_factor * 64.0) as u32,
}
}
pub fn scale_factor(&self) -> f32 {
self.scale_factor_snapped as f32 / 64.0
}
}