Fix errors with new versions

This commit is contained in:
Jarrod Doyle 2024-03-22 11:59:32 +00:00
parent 010921f4fa
commit 881de4e3b9
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
6 changed files with 126 additions and 86 deletions

View File

@ -296,7 +296,7 @@ fn grid_cast_ray(orig_ray_pos: vec3<f32>, ray_dir: vec3<f32>) -> HitInfo {
@compute @workgroup_size(8, 8, 1)
fn compute(@builtin(global_invocation_id) global_id: vec3<u32>) {
let img_coord = vec2<i32>(global_id.xy);
let img_coord = global_id.xy;
let img_dims = textureDimensions(output);
// This discards the extra pixels in cases where the image size isn't perfectly divisible by the kernel.xy

View File

@ -1,5 +1,9 @@
use std::time::Instant;
use winit::{dpi::PhysicalSize, event::Event, event_loop::EventLoop};
use std::{sync::Arc, time::Instant};
use winit::{
dpi::PhysicalSize,
event::{Event, WindowEvent},
event_loop::EventLoop,
};
use super::camera;
use crate::{
@ -7,22 +11,24 @@ use crate::{
voxel,
};
pub struct App {
pub struct App<'window> {
title: String,
event_loop: EventLoop<()>,
render_ctx: gfx::Context,
render_ctx: gfx::Context<'window>,
}
impl App {
impl<'window> App<'window> {
pub async fn new(width: u32, height: u32, title: &str) -> Self {
log::info!("Initialising window...");
let size = PhysicalSize::new(width, height);
let event_loop = EventLoop::new();
let window = winit::window::WindowBuilder::new()
.with_title(title)
.with_inner_size(size)
.build(&event_loop)
.unwrap();
let event_loop = EventLoop::new().unwrap();
let window = Arc::new(
winit::window::WindowBuilder::new()
.with_title(title)
.with_inner_size(size)
.build(&event_loop)
.unwrap(),
);
let render_ctx = gfx::Context::new(
window,
@ -80,16 +86,20 @@ impl App {
let mut cumulative_dt = 0.0;
let mut frames_accumulated = 0.0;
let mut last_render_time = Instant::now();
self.event_loop.run(move |event, _, control_flow| {
if !self.render_ctx.handle_window_event(&event, control_flow) {
match event {
Event::WindowEvent {
ref event,
window_id,
} if window_id == self.render_ctx.window.id() => {
camera_controller.process_events(event);
self.event_loop.run(|event, elwt| {
match event {
Event::WindowEvent { window_id, event }
if window_id == self.render_ctx.window.id() =>
{
if self.render_ctx.handle_window_event(&event, elwt) {
return;
}
Event::RedrawRequested(_) => {
if camera_controller.process_events(&event) {
return;
}
if let WindowEvent::RedrawRequested = event {
let now = Instant::now();
let dt = now - last_render_time;
last_render_time = now;
@ -114,10 +124,50 @@ impl App {
cumulative_dt = 0.0;
frames_accumulated = 0.0;
}
self.render_ctx.window.request_redraw();
}
_ => {}
}
_ => (),
}
// if !self.render_ctx.handle_window_event(&event, control_flow) {
// match event {
// Event::WindowEvent {
// ref event,
// window_id,
// } if window_id == self.render_ctx.window.id() => {
// camera_controller.process_events(event);
// }
// Event::RedrawRequested(_) => {
// let now = Instant::now();
// let dt = now - last_render_time;
// last_render_time = now;
// camera_controller.update(dt);
// camera_controller.update_buffer(&self.render_ctx);
// renderer.render(&self.render_ctx);
// renderer.update(&dt, &self.render_ctx);
// renderer.update_brickmap(&self.render_ctx, &mut world);
// // Simple framerate tracking
// self.render_ctx.window.set_title(&format!(
// "{}: {} fps",
// self.title,
// (1.0 / dt.as_secs_f32()).floor()
// ));
// cumulative_dt += dt.as_secs_f32();
// frames_accumulated += 1.0;
// if cumulative_dt >= 1.0 {
// let fps = frames_accumulated * 1.0 / cumulative_dt;
// let frame_time = cumulative_dt * 1000.0 / frames_accumulated;
// log::info!("FPS: {}, Frame Time: {}", fps.floor(), frame_time);
// cumulative_dt = 0.0;
// frames_accumulated = 0.0;
// }
// }
// _ => {}
// }
// }
});
}
}

View File

@ -1,6 +1,9 @@
use std::time::Duration;
use wgpu::util::DeviceExt;
use winit::event::{ElementState, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::{
event::{ElementState, KeyEvent, WindowEvent},
keyboard::{KeyCode, PhysicalKey},
};
use crate::gfx::Context;
@ -143,17 +146,17 @@ impl CameraController {
}
pub fn process_events(&mut self, event: &WindowEvent) -> bool {
let mut handled = true;
match event {
WindowEvent::Resized(physical_size) => {
self.projection
.resize(physical_size.width, physical_size.height);
true
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
event:
KeyEvent {
state,
virtual_keycode: Some(keycode),
physical_key: PhysicalKey::Code(keycode),
..
},
..
@ -164,51 +167,43 @@ impl CameraController {
};
match keycode {
VirtualKeyCode::W => {
KeyCode::KeyW => {
self.move_dirs_pressed.z = val;
true
}
VirtualKeyCode::S => {
KeyCode::KeyS => {
self.move_dirs_pressed.z = -val;
true
}
VirtualKeyCode::A => {
KeyCode::KeyA => {
self.move_dirs_pressed.x = -val;
true
}
VirtualKeyCode::D => {
KeyCode::KeyD => {
self.move_dirs_pressed.x = val;
true
}
VirtualKeyCode::Q => {
KeyCode::KeyQ => {
self.move_dirs_pressed.y = val;
true
}
VirtualKeyCode::E => {
KeyCode::KeyE => {
self.move_dirs_pressed.y = -val;
true
}
VirtualKeyCode::Up => {
KeyCode::ArrowUp => {
self.rot_dirs_pressed.y = val;
true
}
VirtualKeyCode::Down => {
KeyCode::ArrowDown => {
self.rot_dirs_pressed.y = -val;
true
}
VirtualKeyCode::Left => {
KeyCode::ArrowLeft => {
self.rot_dirs_pressed.x = -val;
true
}
VirtualKeyCode::Right => {
KeyCode::ArrowRight => {
self.rot_dirs_pressed.x = val;
true
}
_ => false,
_ => handled = false,
}
}
_ => false,
_ => handled = false,
}
handled
}
pub fn update(&mut self, dt: Duration) {

View File

@ -1,27 +1,30 @@
use std::sync::Arc;
use winit::{
dpi::PhysicalSize,
event::{Event, WindowEvent},
event_loop::ControlFlow,
event_loop::{ControlFlow, EventLoopWindowTarget},
window::Window,
};
pub struct Context {
pub window: Window,
pub struct Context<'window> {
pub window: Arc<Window>,
pub instance: wgpu::Instance,
pub size: PhysicalSize<u32>,
pub surface: wgpu::Surface,
pub surface: wgpu::Surface<'window>,
pub surface_config: wgpu::SurfaceConfiguration,
pub adapter: wgpu::Adapter,
pub device: wgpu::Device,
pub queue: wgpu::Queue,
}
impl Context {
pub async fn new(window: Window, limits: wgpu::Limits) -> Self {
impl<'window> Context<'window> {
pub async fn new(window: Arc<Window>, limits: wgpu::Limits) -> Self {
log::info!("Initialising WGPU context...");
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::VULKAN,
dx12_shader_compiler: Default::default(),
..Default::default()
});
// To be able to start drawing we need a few things:
@ -29,7 +32,7 @@ impl Context {
// - A GPU device to draw to the surface
// - A draw command queue
log::info!("Initialising window surface...");
let surface = unsafe { instance.create_surface(&window) }.unwrap();
let surface = unsafe { instance.create_surface(window.clone()) }.unwrap();
log::info!("Requesting GPU adapter...");
let adapter = instance
@ -47,8 +50,8 @@ impl Context {
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
limits,
required_features: wgpu::Features::empty(),
required_limits: limits,
},
None,
)
@ -74,7 +77,7 @@ impl Context {
}
}
pub fn resize(&mut self, new_size: PhysicalSize<u32>) {
pub fn resize_surface(&mut self, new_size: PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 {
self.size = new_size;
self.surface_config.width = new_size.width;
@ -85,33 +88,24 @@ impl Context {
pub fn handle_window_event(
&mut self,
event: &Event<()>,
control_flow: &mut ControlFlow,
event: &WindowEvent,
elwt: &EventLoopWindowTarget<()>,
) -> bool {
let mut handled = true;
match event {
Event::WindowEvent {
ref event,
window_id,
} if *window_id == self.window.id() => match event {
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
true
}
WindowEvent::Resized(physical_size) => {
self.resize(*physical_size);
false
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
self.resize(**new_inner_size);
true
}
_ => false,
},
Event::MainEventsCleared => {
self.window.request_redraw();
true
WindowEvent::CloseRequested => {
elwt.exit();
}
_ => false,
WindowEvent::Resized(physical_size) => {
self.resize_surface(*physical_size);
}
WindowEvent::ScaleFactorChanged { .. } => {
self.resize_surface(self.window.inner_size());
}
_ => handled = false,
}
handled
}
}

View File

@ -187,8 +187,8 @@ impl Texture {
let size = self.attributes.size;
let image_layout = wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: std::num::NonZeroU32::new(4 * size.width),
rows_per_image: std::num::NonZeroU32::new(size.height),
bytes_per_row: Some(4 * size.width),
rows_per_image: Some(size.height),
};
context

View File

@ -209,10 +209,11 @@ impl gfx::Renderer for VoxelRenderer {
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(self.clear_color),
store: true,
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
..Default::default()
});
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, &self.render_texture.bind_group, &[]);