From fddb07d51efbfe0963f1b5302fe13255e0090673 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Tue, 18 Apr 2023 21:57:28 +0100 Subject: [PATCH] Move texture and context structures to new Render module --- src/app.rs | 6 +-- src/camera.rs | 6 +-- src/main.rs | 2 +- src/render.rs | 7 ++++ src/render/context.rs | 69 ++++++++++++++++++++++++++++++ src/{ => render}/texture.rs | 14 +++---- src/renderer.rs | 83 ++++--------------------------------- 7 files changed, 97 insertions(+), 90 deletions(-) create mode 100644 src/render.rs create mode 100644 src/render/context.rs rename src/{ => render}/texture.rs (95%) diff --git a/src/app.rs b/src/app.rs index 9398cef..a6dfab4 100644 --- a/src/app.rs +++ b/src/app.rs @@ -5,12 +5,12 @@ use winit::{ event_loop::{ControlFlow, EventLoop}, }; -use crate::renderer; +use crate::{render::Context, renderer}; pub(crate) struct App { window: winit::window::Window, event_loop: EventLoop<()>, - render_ctx: renderer::RenderContext, + render_ctx: Context, renderer: renderer::Renderer, } @@ -25,7 +25,7 @@ impl App { .build(&event_loop) .unwrap(); - let render_ctx = renderer::RenderContext::new(&window).await; + let render_ctx = Context::new(&window).await; let renderer = renderer::Renderer::new(&render_ctx); Self { diff --git a/src/camera.rs b/src/camera.rs index 781bb5b..763ec69 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -2,7 +2,7 @@ use std::time::Duration; use wgpu::util::DeviceExt; use winit::event::{ElementState, KeyboardInput, VirtualKeyCode, WindowEvent}; -use crate::renderer::RenderContext; +use crate::render::Context; #[repr(C)] #[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] @@ -103,7 +103,7 @@ pub(crate) struct CameraController { impl CameraController { pub fn new( - context: &RenderContext, + context: &Context, camera: Camera, projection: Projection, move_speed: f32, @@ -234,7 +234,7 @@ impl CameraController { // log::info!("Camera Pitch: {:?}", self.camera.pitch); } - pub fn update_buffer(&mut self, context: &RenderContext) { + pub fn update_buffer(&mut self, context: &Context) { self.uniform.update( self.camera.get_view_matrix(), self.projection.get_matrix(), diff --git a/src/main.rs b/src/main.rs index e1aedfd..ef18492 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod app; mod camera; +mod render; mod renderer; -mod texture; fn main() { env_logger::init(); diff --git a/src/render.rs b/src/render.rs new file mode 100644 index 0000000..bb348f5 --- /dev/null +++ b/src/render.rs @@ -0,0 +1,7 @@ +mod context; +mod texture; + +pub use self::{ + context::Context, + texture::{Texture, TextureBuilder}, +}; diff --git a/src/render/context.rs b/src/render/context.rs new file mode 100644 index 0000000..905c5a3 --- /dev/null +++ b/src/render/context.rs @@ -0,0 +1,69 @@ +use winit::{dpi::PhysicalSize, window::Window}; + +pub struct Context { + pub instance: wgpu::Instance, + pub size: PhysicalSize, + pub surface: wgpu::Surface, + 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) -> Self { + log::info!("Initialising WGPU context..."); + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { + backends: wgpu::Backends::VULKAN, + dx12_shader_compiler: Default::default(), + }); + + // To be able to start drawing 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 size = window.inner_size(); + let surface_config = surface + .get_default_config(&adapter, size.width, size.height) + .unwrap(); + surface.configure(&device, &surface_config); + + Self { + instance, + size, + surface, + surface_config, + adapter, + device, + queue, + } + } +} diff --git a/src/texture.rs b/src/render/texture.rs similarity index 95% rename from src/texture.rs rename to src/render/texture.rs index 7aa1c0d..e5ab86a 100644 --- a/src/texture.rs +++ b/src/render/texture.rs @@ -1,7 +1,7 @@ -use crate::renderer::RenderContext; - // TODO: Support mip-mapping and multi-sampling +use super::Context; + #[derive(Debug, Clone)] pub struct TextureAttributes { pub size: wgpu::Extent3d, @@ -35,7 +35,7 @@ impl Default for TextureAttributes { } } -pub(crate) struct TextureBuilder { +pub struct TextureBuilder { pub attributes: TextureAttributes, } @@ -97,12 +97,12 @@ impl TextureBuilder { } #[inline] - pub fn build(self, context: &RenderContext) -> Texture { + pub fn build(self, context: &Context) -> Texture { Texture::new(context, self.attributes) } } -pub(crate) struct Texture { +pub struct Texture { pub attributes: TextureAttributes, pub texture: wgpu::Texture, pub view: wgpu::TextureView, @@ -112,7 +112,7 @@ pub(crate) struct Texture { } impl Texture { - pub fn new(context: &RenderContext, attributes: TextureAttributes) -> Self { + pub fn new(context: &Context, attributes: TextureAttributes) -> Self { let texture = context.device.create_texture(&wgpu::TextureDescriptor { label: None, size: attributes.size, @@ -192,7 +192,7 @@ impl Texture { } } - pub fn update(&self, context: &RenderContext, data: &[u8]) { + pub fn update(&self, context: &Context, data: &[u8]) { log::info!("Updating texture contents..."); let copy_texture = wgpu::ImageCopyTexture { texture: &self.texture, diff --git a/src/renderer.rs b/src/renderer.rs index 9a476db..763f833 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,79 +1,10 @@ use std::time::Duration; - -use wgpu::util::DeviceExt; use winit::event::WindowEvent; -use winit::{dpi::PhysicalSize, window::Window}; -use crate::camera; -use crate::texture::{Texture, TextureBuilder}; - -pub(crate) struct RenderContext { - pub instance: wgpu::Instance, - pub size: PhysicalSize, - pub surface: wgpu::Surface, - pub surface_config: wgpu::SurfaceConfiguration, - pub adapter: wgpu::Adapter, - pub device: wgpu::Device, - pub queue: wgpu::Queue, -} - -impl RenderContext { - pub async fn new(window: &Window) -> Self { - log::info!("Initialising WGPU context..."); - let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { - backends: wgpu::Backends::VULKAN, - dx12_shader_compiler: Default::default(), - }); - - // To be able to start drawing 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 size = window.inner_size(); - let surface_config = surface - .get_default_config(&adapter, size.width, size.height) - .unwrap(); - surface.configure(&device, &surface_config); - - Self { - instance, - size, - surface, - surface_config, - adapter, - device, - queue, - } - } -} +use crate::{ + camera, + render::{Context, Texture, TextureBuilder}, +}; pub(crate) struct Renderer { clear_color: wgpu::Color, @@ -87,7 +18,7 @@ pub(crate) struct Renderer { } impl Renderer { - pub fn new(context: &RenderContext) -> Self { + pub fn new(context: &Context) -> Self { log::info!("Creating render shader..."); let shader_descriptor = wgpu::include_wgsl!("../assets/shaders/shader.wgsl"); let shader = context.device.create_shader_module(shader_descriptor); @@ -286,7 +217,7 @@ impl Renderer { } } - pub fn render(&mut self, context: &RenderContext) { + pub fn render(&mut self, context: &Context) { let frame = context.surface.get_current_texture().unwrap(); let view = frame .texture @@ -331,7 +262,7 @@ impl Renderer { self.camera_controller.process_events(event) } - pub fn update(&mut self, dt: Duration, render_ctx: &RenderContext) { + pub fn update(&mut self, dt: Duration, render_ctx: &Context) { self.camera_controller.update(dt); self.camera_controller.update_buffer(render_ctx) }