From 4f8602c48e56d0e6ba653b8ffba43de015789670 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Fri, 21 Apr 2023 12:53:42 +0100 Subject: [PATCH] Move camera out of renderer --- src/app.rs | 60 ++++++++++++++++++++++++++++++++++++++++--------- src/renderer.rs | 59 +++--------------------------------------------- 2 files changed, 53 insertions(+), 66 deletions(-) diff --git a/src/app.rs b/src/app.rs index a6dfab4..d4534d2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -5,13 +5,12 @@ use winit::{ event_loop::{ControlFlow, EventLoop}, }; -use crate::{render::Context, renderer}; +use crate::{camera, render, renderer}; pub(crate) struct App { window: winit::window::Window, event_loop: EventLoop<()>, - render_ctx: Context, - renderer: renderer::Renderer, + render_ctx: render::Context, } impl App { @@ -25,18 +24,58 @@ impl App { .build(&event_loop) .unwrap(); - let render_ctx = Context::new(&window).await; - let renderer = renderer::Renderer::new(&render_ctx); + let render_ctx = render::Context::new(&window).await; Self { window, event_loop, render_ctx, - renderer, } } - pub fn run(mut self) { + pub fn run(self) { + let mut camera_controller = camera::CameraController::new( + &self.render_ctx, + camera::Camera::new( + glam::Vec3 { + x: 4.01, + y: 4.01, + z: 20.0, + }, + -90.0_f32.to_radians(), + 0.0_f32.to_radians(), + ), + camera::Projection::new( + self.render_ctx.size.width, + self.render_ctx.size.height, + 90.0_f32.to_radians(), + 0.01, + 100.0, + ), + 10.0, + 0.25, + ); + + let camera_bind_group_layout = render::BindGroupLayoutBuilder::new() + .with_label("camera_bind_group_layout") + .with_entry( + wgpu::ShaderStages::COMPUTE, + wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + None, + ) + .build(&self.render_ctx); + let camera_bind_group = render::BindGroupBuilder::new() + .with_label("camera_bind_group") + .with_layout(&camera_bind_group_layout) + .with_entry(camera_controller.get_buffer().as_entire_binding()) + .build(&self.render_ctx); + + let renderer = renderer::Renderer::new(&self.render_ctx, &camera_bind_group_layout); + let mut last_render_time = Instant::now(); self.event_loop .run(move |event, _, control_flow| match event { @@ -46,7 +85,7 @@ impl App { } if window_id == self.window.id() => match event { WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit, _ => { - self.renderer.input(&event); + camera_controller.process_events(&event); } }, Event::MainEventsCleared => { @@ -56,8 +95,9 @@ impl App { let now = Instant::now(); let dt = now - last_render_time; last_render_time = now; - self.renderer.update(dt, &self.render_ctx); - self.renderer.render(&self.render_ctx); + camera_controller.update(dt); + camera_controller.update_buffer(&self.render_ctx); + renderer.render(&self.render_ctx, &camera_bind_group); } _ => {} }); diff --git a/src/renderer.rs b/src/renderer.rs index d4d541a..e743127 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -13,12 +13,10 @@ pub(crate) struct Renderer { render_pipeline: wgpu::RenderPipeline, render_texture: Texture, voxel_texture: Texture, - camera_controller: camera::CameraController, - camera_bind_group: wgpu::BindGroup, } impl Renderer { - pub fn new(context: &Context) -> Self { + pub fn new(context: &Context, camera_bind_group_layout: &wgpu::BindGroupLayout) -> 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); @@ -71,45 +69,6 @@ impl Renderer { voxel_texture.update(&context, &data); log::info!("Creating camera..."); - let camera_controller = camera::CameraController::new( - context, - camera::Camera::new( - glam::Vec3 { - x: 4.01, - y: 4.01, - z: 20.0, - }, - -90.0_f32.to_radians(), - 0.0_f32.to_radians(), - ), - camera::Projection::new( - context.size.width, - context.size.height, - 90.0_f32.to_radians(), - 0.01, - 100.0, - ), - 10.0, - 0.25, - ); - - let camera_bind_group_layout = BindGroupLayoutBuilder::new() - .with_label("camera_bind_group_layout") - .with_entry( - wgpu::ShaderStages::COMPUTE, - wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, - has_dynamic_offset: false, - min_binding_size: None, - }, - None, - ) - .build(context); - let camera_bind_group = BindGroupBuilder::new() - .with_label("camera_bind_group") - .with_layout(&camera_bind_group_layout) - .with_entry(camera_controller.get_buffer().as_entire_binding()) - .build(context); log::info!("Creating render pipeline..."); let render_pipeline = @@ -192,13 +151,10 @@ impl Renderer { render_pipeline, render_texture, voxel_texture, - camera_controller, - camera_bind_group, } } - // pub fn render(&mut self, context: &Context, encoders: &[wgpu::CommandEncoder]) { - pub fn render(&mut self, context: &Context) { + pub fn render(&self, context: &Context, camera_bind_group: &wgpu::BindGroup) { let frame = context.surface.get_current_texture().unwrap(); let view = frame .texture @@ -213,7 +169,7 @@ impl Renderer { compute_pass.set_pipeline(&self.compute_pipeline); compute_pass.set_bind_group(0, &self.compute_bind_group, &[]); compute_pass.set_bind_group(1, &self.voxel_texture.bind_group, &[]); - compute_pass.set_bind_group(2, &self.camera_bind_group, &[]); + compute_pass.set_bind_group(2, camera_bind_group, &[]); compute_pass.dispatch_workgroups(size.width / 8, size.height / 8, 1); drop(compute_pass); @@ -246,13 +202,4 @@ impl Renderer { // context.queue.submit(command_buffers); // frame.present(); } - - pub fn input(&mut self, event: &WindowEvent) -> bool { - self.camera_controller.process_events(event) - } - - pub fn update(&mut self, dt: Duration, render_ctx: &Context) { - self.camera_controller.update(dt); - self.camera_controller.update_buffer(render_ctx) - } }