Move texture and context structures to new Render module
This commit is contained in:
parent
9554f3ba7f
commit
fddb07d51e
|
@ -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 {
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
mod app;
|
||||
mod camera;
|
||||
mod render;
|
||||
mod renderer;
|
||||
mod texture;
|
||||
|
||||
fn main() {
|
||||
env_logger::init();
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
mod context;
|
||||
mod texture;
|
||||
|
||||
pub use self::{
|
||||
context::Context,
|
||||
texture::{Texture, TextureBuilder},
|
||||
};
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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,
|
|
@ -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<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,
|
||||
}
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue