From 2fb1a1dc7c6daae4c24572d2ca3f7f0205f24070 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Wed, 15 Jun 2022 14:04:10 +0200 Subject: [PATCH] [REFACTOR] Begin refactor mesh --- src/render/mesh.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++ src/render/mod.rs | 11 +++++++- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/render/mesh.rs diff --git a/src/render/mesh.rs b/src/render/mesh.rs new file mode 100644 index 0000000..4ca81fa --- /dev/null +++ b/src/render/mesh.rs @@ -0,0 +1,63 @@ +use wgpu::{Device, util::DeviceExt, Queue}; + +use super::{Vertex, Renderable, Instance}; + +pub struct Mesh { + vertex_array: Vec, + index_array: Vec, + num_indices: u32, + instance_array: Vec, + texture_bind_group: Option, + vertex_buffer: Option, + index_buffer: Option, + instance_buffer: Option, +} + +impl Renderable for Mesh { + fn prepare(&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), + usage: wgpu::BufferUsages::VERTEX, + })); + + self.index_buffer = Some(device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Index Buffer"), + contents: bytemuck::cast_slice(&self.index_array), + usage: wgpu::BufferUsages::INDEX, + })); + self.num_indices = self.index_array.len() as u32; + + let instance_data = self.instance_array + .iter() + .map(Instance::to_raw) + .collect::>(); + self.instance_buffer = Some(device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Instance Buffer"), + contents: bytemuck::cast_slice(&instance_data), + usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + })); + } + + fn update_instances(&mut self, queue: &Queue) { + let instance_data = self + .instance_array + .iter() + .map(Instance::to_raw) + .collect::>(); + queue.write_buffer( + &self.instance_buffer.as_ref().unwrap(), + 0, + bytemuck::cast_slice(&instance_data), + ); + } + + fn render<'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(..).clone()); + 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.draw_indexed(0..self.index_array.len() as _, 0, 0..self.num_indices); + } +} \ No newline at end of file diff --git a/src/render/mod.rs b/src/render/mod.rs index c46f5e7..b9e59d4 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -12,4 +12,13 @@ pub use texture::Texture; mod instance; pub use instance::{ Instance, InstanceRaw -}; \ No newline at end of file +}; +use wgpu::{Device, Queue}; + +mod mesh; + +pub trait Renderable { + fn prepare(&mut self, device: &Device); + fn update_instances(&mut self, device: &Queue); + fn render<'a>(&'a self, render_pass: &mut wgpu::RenderPass<'a>); +} \ No newline at end of file