Use new RenderCtx and Renderer structs

This commit is contained in:
Jarrod Doyle 2023-04-07 19:33:06 +01:00
parent 6a9335089b
commit 3e001a930f
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 16 additions and 57 deletions

View File

@ -4,16 +4,13 @@ use winit::{
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
}; };
use crate::renderer;
pub(crate) struct AppWindow { pub(crate) struct AppWindow {
window: winit::window::Window, window: winit::window::Window,
event_loop: EventLoop<()>, event_loop: EventLoop<()>,
instance: wgpu::Instance, render_ctx: renderer::RenderContext,
size: PhysicalSize<u32>, renderer: renderer::Renderer,
surface: wgpu::Surface,
config: wgpu::SurfaceConfiguration,
adapter: wgpu::Adapter,
device: wgpu::Device,
queue: wgpu::Queue,
} }
impl AppWindow { impl AppWindow {
@ -27,63 +24,18 @@ impl AppWindow {
.build(&event_loop) .build(&event_loop)
.unwrap(); .unwrap();
log::info!("Initialising WGPU context..."); let render_ctx = renderer::RenderContext::new(&window).await;
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { let renderer = renderer::Renderer::new(&render_ctx);
backends: wgpu::Backends::VULKAN,
dx12_shader_compiler: Default::default(),
});
// To be able to start drawing to our window we need a few things:
// - A surface
// - 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();
log::info!("Requesting GPU adapter...");
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
force_fallback_adapter: false,
compatible_surface: Some(&surface),
})
.await
.unwrap();
log::info!("Checking GPU adapter meets requirements");
log::info!("Requesting GPU device...");
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
},
None,
)
.await
.unwrap();
log::info!("Configuring window surface...");
let config = surface
.get_default_config(&adapter, size.width, size.height)
.unwrap();
surface.configure(&device, &config);
Self { Self {
window, window,
event_loop, event_loop,
instance, render_ctx,
size, renderer,
surface,
config,
adapter,
device,
queue,
} }
} }
pub fn run(self) { pub fn run(mut self) {
self.event_loop self.event_loop
.run(move |event, _, control_flow| match event { .run(move |event, _, control_flow| match event {
Event::WindowEvent { Event::WindowEvent {
@ -93,6 +45,12 @@ impl AppWindow {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
_ => {} _ => {}
}, },
Event::MainEventsCleared => {
self.window.request_redraw();
}
Event::RedrawRequested(_) => {
self.renderer.render(&self.render_ctx);
}
_ => {} _ => {}
}); });
} }

View File

@ -1,4 +1,5 @@
mod app; mod app;
mod renderer;
fn main() { fn main() {
env_logger::init(); env_logger::init();