1
0
Fork 0

[Refactor] Renderable

This commit is contained in:
Florian RICHER 2022-06-16 17:24:57 +02:00
parent 2982f7cd39
commit 96d1e2441f
5 changed files with 25 additions and 14 deletions

View file

@ -106,8 +106,8 @@ impl DefaultMesh {
} }
impl Renderable for DefaultMesh { impl Renderable for DefaultMesh {
fn prepare(&mut self, device: &wgpu::Device) { fn initialize(&mut self, device: &wgpu::Device) {
self.mesh.prepare(device); self.mesh.initialize(device);
} }
fn update_instances(&mut self, device: &wgpu::Queue) { fn update_instances(&mut self, device: &wgpu::Queue) {
@ -119,8 +119,12 @@ impl Renderable for DefaultMesh {
self.mesh.update_instances(device); self.mesh.update_instances(device);
} }
fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) { fn prepare<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
self.mesh.render(render_pass); self.mesh.prepare(render_pass);
}
fn draw<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>) {
self.mesh.draw(render_pass);
} }
} }

View file

@ -95,7 +95,7 @@ impl Camera {
} }
impl Renderable for Camera { impl Renderable for Camera {
fn prepare(&mut self, device: &wgpu::Device) { fn initialize(&mut self, device: &wgpu::Device) {
self.update_uniform(); self.update_uniform();
self.buffer = Some( 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(), &[]); 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 { impl Controllable for Camera {

View file

@ -16,7 +16,7 @@ pub struct Mesh {
} }
impl Renderable for 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 { self.vertex_buffer = Some(device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Vertex Buffer"), label: Some("Vertex Buffer"),
contents: bytemuck::cast_slice(&self.vertex_array), 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_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(0, self.vertex_buffer.as_ref().unwrap().slice(..));
render_pass.set_vertex_buffer(1, self.instance_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); 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 _); render_pass.draw_indexed(0..self.num_indices as _, 0, 0..self.instance_array.len() as _);
} }
} }

View file

@ -17,7 +17,8 @@ mod mesh;
pub use mesh::Mesh; pub use mesh::Mesh;
pub trait Renderable { pub trait Renderable {
fn prepare(&mut self, device: &Device); fn initialize(&mut self, device: &Device);
fn update_instances(&mut self, queue: &Queue); 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>);
} }

View file

@ -84,7 +84,7 @@ impl State {
}); });
let mut camera = Camera::new(config.width as f32, config.height as f32, 0.2); let mut camera = Camera::new(config.width as f32, config.height as f32, 0.2);
camera.prepare(&device); camera.initialize(&device);
let depth_texture = let depth_texture =
Texture::create_depth_texture(&device, &config, "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); let mut mesh = DefaultMesh::new(diffuse_bind_group, diffuse_bind_group_pikachu);
mesh.prepare(&device); mesh.initialize(&device);
Self { Self {
surface, surface,
@ -235,8 +235,10 @@ impl State {
}); });
render_pass.set_pipeline(&self.render_pipeline); render_pass.set_pipeline(&self.render_pipeline);
self.camera.render(&mut render_pass); self.camera.prepare(&mut render_pass);
self.mesh.render(&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 // submit will accept anything that implements IntoIter