Move texture and context structures to new Render module

This commit is contained in:
Jarrod Doyle 2023-04-18 21:57:28 +01:00
parent 9554f3ba7f
commit fddb07d51e
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
7 changed files with 97 additions and 90 deletions

View File

@ -5,12 +5,12 @@ use winit::{
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
}; };
use crate::renderer; use crate::{render::Context, renderer};
pub(crate) struct App { pub(crate) struct App {
window: winit::window::Window, window: winit::window::Window,
event_loop: EventLoop<()>, event_loop: EventLoop<()>,
render_ctx: renderer::RenderContext, render_ctx: Context,
renderer: renderer::Renderer, renderer: renderer::Renderer,
} }
@ -25,7 +25,7 @@ impl App {
.build(&event_loop) .build(&event_loop)
.unwrap(); .unwrap();
let render_ctx = renderer::RenderContext::new(&window).await; let render_ctx = Context::new(&window).await;
let renderer = renderer::Renderer::new(&render_ctx); let renderer = renderer::Renderer::new(&render_ctx);
Self { Self {

View File

@ -2,7 +2,7 @@ use std::time::Duration;
use wgpu::util::DeviceExt; use wgpu::util::DeviceExt;
use winit::event::{ElementState, KeyboardInput, VirtualKeyCode, WindowEvent}; use winit::event::{ElementState, KeyboardInput, VirtualKeyCode, WindowEvent};
use crate::renderer::RenderContext; use crate::render::Context;
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] #[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
@ -103,7 +103,7 @@ pub(crate) struct CameraController {
impl CameraController { impl CameraController {
pub fn new( pub fn new(
context: &RenderContext, context: &Context,
camera: Camera, camera: Camera,
projection: Projection, projection: Projection,
move_speed: f32, move_speed: f32,
@ -234,7 +234,7 @@ impl CameraController {
// log::info!("Camera Pitch: {:?}", self.camera.pitch); // 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.uniform.update(
self.camera.get_view_matrix(), self.camera.get_view_matrix(),
self.projection.get_matrix(), self.projection.get_matrix(),

View File

@ -1,7 +1,7 @@
mod app; mod app;
mod camera; mod camera;
mod render;
mod renderer; mod renderer;
mod texture;
fn main() { fn main() {
env_logger::init(); env_logger::init();

7
src/render.rs Normal file
View File

@ -0,0 +1,7 @@
mod context;
mod texture;
pub use self::{
context::Context,
texture::{Texture, TextureBuilder},
};

69
src/render/context.rs Normal file
View File

@ -0,0 +1,69 @@
use winit::{dpi::PhysicalSize, window::Window};
pub struct Context {
pub instance: wgpu::Instance,
pub size: PhysicalSize<u32>,
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,
}
}
}

View File

@ -1,7 +1,7 @@
use crate::renderer::RenderContext;
// TODO: Support mip-mapping and multi-sampling // TODO: Support mip-mapping and multi-sampling
use super::Context;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TextureAttributes { pub struct TextureAttributes {
pub size: wgpu::Extent3d, pub size: wgpu::Extent3d,
@ -35,7 +35,7 @@ impl Default for TextureAttributes {
} }
} }
pub(crate) struct TextureBuilder { pub struct TextureBuilder {
pub attributes: TextureAttributes, pub attributes: TextureAttributes,
} }
@ -97,12 +97,12 @@ impl TextureBuilder {
} }
#[inline] #[inline]
pub fn build(self, context: &RenderContext) -> Texture { pub fn build(self, context: &Context) -> Texture {
Texture::new(context, self.attributes) Texture::new(context, self.attributes)
} }
} }
pub(crate) struct Texture { pub struct Texture {
pub attributes: TextureAttributes, pub attributes: TextureAttributes,
pub texture: wgpu::Texture, pub texture: wgpu::Texture,
pub view: wgpu::TextureView, pub view: wgpu::TextureView,
@ -112,7 +112,7 @@ pub(crate) struct Texture {
} }
impl 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 { let texture = context.device.create_texture(&wgpu::TextureDescriptor {
label: None, label: None,
size: attributes.size, 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..."); log::info!("Updating texture contents...");
let copy_texture = wgpu::ImageCopyTexture { let copy_texture = wgpu::ImageCopyTexture {
texture: &self.texture, texture: &self.texture,

View File

@ -1,79 +1,10 @@
use std::time::Duration; use std::time::Duration;
use wgpu::util::DeviceExt;
use winit::event::WindowEvent; use winit::event::WindowEvent;
use winit::{dpi::PhysicalSize, window::Window};
use crate::camera; use crate::{
use crate::texture::{Texture, TextureBuilder}; camera,
render::{Context, Texture, TextureBuilder},
pub(crate) struct RenderContext { };
pub instance: wgpu::Instance,
pub size: PhysicalSize<u32>,
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,
}
}
}
pub(crate) struct Renderer { pub(crate) struct Renderer {
clear_color: wgpu::Color, clear_color: wgpu::Color,
@ -87,7 +18,7 @@ pub(crate) struct Renderer {
} }
impl Renderer { impl Renderer {
pub fn new(context: &RenderContext) -> Self { pub fn new(context: &Context) -> Self {
log::info!("Creating render shader..."); log::info!("Creating render shader...");
let shader_descriptor = wgpu::include_wgsl!("../assets/shaders/shader.wgsl"); let shader_descriptor = wgpu::include_wgsl!("../assets/shaders/shader.wgsl");
let shader = context.device.create_shader_module(shader_descriptor); 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 frame = context.surface.get_current_texture().unwrap();
let view = frame let view = frame
.texture .texture
@ -331,7 +262,7 @@ impl Renderer {
self.camera_controller.process_events(event) 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(dt);
self.camera_controller.update_buffer(render_ctx) self.camera_controller.update_buffer(render_ctx)
} }