scissor attempt

This commit is contained in:
Aleksander
2025-09-23 17:43:19 +02:00
parent 5e6852e5d0
commit 8d41d8bbd1
8 changed files with 143 additions and 70 deletions

View File

@@ -23,7 +23,7 @@ use wgui::{
EventListenerCollection, MouseButtonIndex, MouseDownEvent, MouseMotionEvent, MouseUpEvent,
MouseWheelEvent,
},
gfx::WGfx,
gfx::{WGfx, cmd::WGfxClearMode},
renderer_vk::{self},
};
use winit::{
@@ -32,12 +32,16 @@ use winit::{
keyboard::{KeyCode, PhysicalKey},
};
use crate::testbed::{
TestbedUpdateParams, testbed_dashboard::TestbedDashboard, testbed_generic::TestbedGeneric,
use crate::{
rate_limiter::RateLimiter,
testbed::{
TestbedUpdateParams, testbed_dashboard::TestbedDashboard, testbed_generic::TestbedGeneric,
},
};
mod assets;
mod profiler;
mod rate_limiter;
mod testbed;
mod timestep;
mod vulkan;
@@ -117,6 +121,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut timestep = Timestep::new();
timestep.set_tps(60.0);
let mut limiter = RateLimiter::new();
#[allow(deprecated)]
event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Poll);
@@ -291,6 +297,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
log::trace!("drawing frame {frame_index}");
frame_index += 1;
limiter.start(120); // max 120 fps
profiler.start();
{
@@ -301,7 +308,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
recreate = true;
return;
}
Err(e) => panic!("failed to acquire next image: {e}"),
Err(e) => {
log::error!("failed to acquire next image: {e}");
return;
}
};
let tgt = images[image_index as usize].clone();
@@ -309,7 +319,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd_buf = gfx
.create_gfx_command_buffer(CommandBufferUsage::OneTimeSubmit)
.unwrap();
cmd_buf.begin_rendering(tgt).unwrap();
cmd_buf
.begin_rendering(tgt, WGfxClearMode::Clear([0.0, 0.0, 0.0, 0.1]))
.unwrap();
let primitives = wgui::drawing::draw(&testbed.layout().borrow_mut()).unwrap();
render_context
@@ -334,6 +346,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
profiler.end();
limiter.end();
}
Event::AboutToWait => {
// should be limited to vsync

View File

@@ -1,10 +1,4 @@
use std::{sync::LazyLock, time::Instant};
static TIME_START: LazyLock<Instant> = LazyLock::new(Instant::now);
pub fn get_micros() -> u64 {
TIME_START.elapsed().as_micros() as u64
}
use crate::timestep::get_micros;
pub struct Profiler {
interval_us: u64,

37
uidev/src/rate_limiter.rs Normal file
View File

@@ -0,0 +1,37 @@
use crate::timestep::get_micros;
#[derive(Default)]
pub struct RateLimiter {
rate: u16,
start_us: u64,
end_us: u64,
}
impl RateLimiter {
pub fn new() -> Self {
Self {
rate: 0,
end_us: 0,
start_us: 0,
}
}
pub fn start(&mut self, rate: u16) {
self.rate = rate;
self.start_us = get_micros();
}
pub fn end(&mut self) {
if self.rate == 0 {
return;
}
self.end_us = get_micros();
let microseconds = self.end_us - self.start_us;
let frametime_microseconds = ((1000.0 / self.rate as f32) * 1000.0) as u64;
let delay = frametime_microseconds as i64 - microseconds as i64;
if delay > 0 {
std::thread::sleep(std::time::Duration::from_micros(delay as u64));
}
}
}