From 96d1e2441f673d9c4a3fc60d5aebbfae5a828346 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Thu, 16 Jun 2022 17:24:57 +0200 Subject: [PATCH] [Refactor] Renderable --- src/meshs/default_mesh.rs | 12 ++++++++---- src/render/camera.rs | 6 ++++-- src/render/mesh.rs | 6 ++++-- src/render/mod.rs | 5 +++-- src/state.rs | 10 ++++++---- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/meshs/default_mesh.rs b/src/meshs/default_mesh.rs index 34e1196..fe1b501 100644 --- a/src/meshs/default_mesh.rs +++ b/src/meshs/default_mesh.rs @@ -106,8 +106,8 @@ impl DefaultMesh { } impl Renderable for DefaultMesh { - fn prepare(&mut self, device: &wgpu::Device) { - self.mesh.prepare(device); + fn initialize(&mut self, device: &wgpu::Device) { + self.mesh.initialize(device); } fn update_instances(&mut self, device: &wgpu::Queue) { @@ -119,8 +119,12 @@ impl Renderable for DefaultMesh { self.mesh.update_instances(device); } - fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { - self.mesh.render(render_pass); + fn prepare<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { + self.mesh.prepare(render_pass); + } + + fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { + self.mesh.draw(render_pass); } } diff --git a/src/render/camera.rs b/src/render/camera.rs index 1f7cfd6..0cec10a 100644 --- a/src/render/camera.rs +++ b/src/render/camera.rs @@ -95,7 +95,7 @@ impl Camera { } impl Renderable for Camera { - fn prepare(&mut self, device: &wgpu::Device) { + fn initialize(&mut self, device: &wgpu::Device) { self.update_uniform(); self.buffer = Some( @@ -142,9 +142,11 @@ impl Renderable for Camera { ); } - fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { + fn prepare<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { render_pass.set_bind_group(1, &self.bind_group.as_ref().unwrap(), &[]); } + + fn draw<'a>(&'a self, _render_pass: &mut wgpu::RenderPass<'a>) { } } impl Controllable for Camera { diff --git a/src/render/mesh.rs b/src/render/mesh.rs index 93b3b47..9d8a959 100644 --- a/src/render/mesh.rs +++ b/src/render/mesh.rs @@ -16,7 +16,7 @@ pub struct Mesh { } impl Renderable for Mesh { - fn prepare(&mut self, device: &Device) { + fn initialize(&mut self, device: &Device) { self.vertex_buffer = Some(device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Vertex Buffer"), contents: bytemuck::cast_slice(&self.vertex_array), @@ -54,12 +54,14 @@ impl Renderable for Mesh { ); } - fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { + fn prepare<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { render_pass.set_bind_group(0, &self.texture_bind_group.as_ref().unwrap(), &[]); render_pass.set_vertex_buffer(0, self.vertex_buffer.as_ref().unwrap().slice(..)); render_pass.set_vertex_buffer(1, self.instance_buffer.as_ref().unwrap().slice(..)); render_pass.set_index_buffer(self.index_buffer.as_ref().unwrap().slice(..), wgpu::IndexFormat::Uint16); + } + fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { render_pass.draw_indexed(0..self.num_indices as _, 0, 0..self.instance_array.len() as _); } } \ No newline at end of file diff --git a/src/render/mod.rs b/src/render/mod.rs index b919771..5739d00 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -17,7 +17,8 @@ mod mesh; pub use mesh::Mesh; pub trait Renderable { - fn prepare(&mut self, device: &Device); + fn initialize(&mut self, device: &Device); fn update_instances(&mut self, queue: &Queue); - fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>); + fn prepare<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>); + fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>); } \ No newline at end of file diff --git a/src/state.rs b/src/state.rs index 6d6318f..6532a92 100644 --- a/src/state.rs +++ b/src/state.rs @@ -84,7 +84,7 @@ impl State { }); let mut camera = Camera::new(config.width as f32, config.height as f32, 0.2); - camera.prepare(&device); + camera.initialize(&device); let depth_texture = Texture::create_depth_texture(&device, &config, "depth_texture"); @@ -155,7 +155,7 @@ impl State { ); let mut mesh = DefaultMesh::new(diffuse_bind_group, diffuse_bind_group_pikachu); - mesh.prepare(&device); + mesh.initialize(&device); Self { surface, @@ -235,8 +235,10 @@ impl State { }); render_pass.set_pipeline(&self.render_pipeline); - self.camera.render(&mut render_pass); - self.mesh.render(&mut render_pass); + self.camera.prepare(&mut render_pass); + self.mesh.prepare(&mut render_pass); + self.camera.draw(&mut render_pass); + self.mesh.draw(&mut render_pass); } // submit will accept anything that implements IntoIter