From a0392e676bf8b41867b2781f3ca13117278d7e20 Mon Sep 17 00:00:00 2001 From: Werner Date: Wed, 3 Nov 2021 19:16:23 -0300 Subject: [PATCH] Instancing --- Shaders/Texture.wgsl | 18 ++++++++++- Source/Instance.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++ Source/Main.rs | 49 ++++++++++++++++++++++++++++-- 3 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 Source/Instance.rs diff --git a/Shaders/Texture.wgsl b/Shaders/Texture.wgsl index 11f288f..225f93b 100644 --- a/Shaders/Texture.wgsl +++ b/Shaders/Texture.wgsl @@ -1,5 +1,12 @@ // Vertex shader +struct InstanceInput { + [[location(5)]] model_matrix_0: vec4; + [[location(6)]] model_matrix_1: vec4; + [[location(7)]] model_matrix_2: vec4; + [[location(8)]] model_matrix_3: vec4; +}; + [[block]] struct CameraUniform { view_proj: mat4x4; @@ -21,10 +28,19 @@ struct VertexOutput { [[stage(vertex)]] fn main( model: VertexInput, + instance: InstanceInput, ) -> VertexOutput { + let model_matrix = mat4x4( + instance.model_matrix_0, + instance.model_matrix_1, + instance.model_matrix_2, + instance.model_matrix_3, + ); + var out: VertexOutput; out.tex_coords = model.tex_coords; - out.clip_position = camera.view_proj * vec4(model.position, 1.0); // 3. + out.clip_position = camera.view_proj * model_matrix * vec4(model.position, 1.0); + return out; } diff --git a/Source/Instance.rs b/Source/Instance.rs new file mode 100644 index 0000000..3b65423 --- /dev/null +++ b/Source/Instance.rs @@ -0,0 +1,71 @@ +use bytemuck::{Pod, Zeroable}; +use std::mem; + +pub const NUM_INSTANCES_PER_ROW: u32 = 10; +pub const NUM_INSTANCES: u32 = NUM_INSTANCES_PER_ROW * NUM_INSTANCES_PER_ROW; + +pub const INSTANCE_DISPLACEMENT: cgmath::Vector3 = cgmath::Vector3::new( + NUM_INSTANCES_PER_ROW as f32 * 0.5, + 0.0, + NUM_INSTANCES_PER_ROW as f32 * 0.5, +); + +pub struct Instance { + pub position: cgmath::Vector3, + pub rotation: cgmath::Quaternion, +} + +impl Instance { + pub fn to_raw(&self) -> InstanceRaw { + InstanceRaw { + model: (cgmath::Matrix4::from_translation(self.position) + * cgmath::Matrix4::from(self.rotation)) + .into(), + } + } +} + +#[repr(C)] +#[derive(Copy, Clone, Pod, Zeroable)] +pub struct InstanceRaw { + pub model: [[f32; 4]; 4], +} + +impl InstanceRaw { + pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> { + wgpu::VertexBufferLayout { + array_stride: mem::size_of::() as wgpu::BufferAddress, + // We need to switch from using a step mode of Vertex to Instance + // This means that our shaders will only change to use the next + // instance when the shader starts processing a new instance + step_mode: wgpu::VertexStepMode::Instance, + attributes: &[ + wgpu::VertexAttribute { + offset: 0, + // While our vertex shader only uses locations 0, and 1 now, in later tutorials we'll + // be using 2, 3, and 4, for Vertex. We'll start at slot 5 not conflict with them later + shader_location: 5, + format: wgpu::VertexFormat::Float32x4, + }, + // A mat4 takes up 4 vertex slots as it is technically 4 vec4s. We need to define a slot + // for each vec4. We'll have to reassemble the mat4 in + // the shader. + wgpu::VertexAttribute { + offset: mem::size_of::<[f32; 4]>() as wgpu::BufferAddress, + shader_location: 6, + format: wgpu::VertexFormat::Float32x4, + }, + wgpu::VertexAttribute { + offset: mem::size_of::<[f32; 8]>() as wgpu::BufferAddress, + shader_location: 7, + format: wgpu::VertexFormat::Float32x4, + }, + wgpu::VertexAttribute { + offset: mem::size_of::<[f32; 12]>() as wgpu::BufferAddress, + shader_location: 8, + format: wgpu::VertexFormat::Float32x4, + }, + ], + } + } +} diff --git a/Source/Main.rs b/Source/Main.rs index 1f377d5..22600fb 100644 --- a/Source/Main.rs +++ b/Source/Main.rs @@ -4,11 +4,16 @@ mod _Camera; use _Camera::*; +#[path = "Instance.rs"] +mod _Instance; +use _Instance::*; + #[path = "Texture.rs"] mod _Texture; use _Texture::*; use bytemuck::{Pod, Zeroable}; +use cgmath::prelude::*; use image::GenericImageView; use std::mem; use wgpu::util::DeviceExt; @@ -92,6 +97,8 @@ struct State { camera_buffer: wgpu::Buffer, camera_bind_group: wgpu::BindGroup, camera_controller: CameraController, + instances: Vec, + instance_buffer: wgpu::Buffer, } impl State { @@ -305,7 +312,7 @@ impl State { vertex: wgpu::VertexState { module: &shader, entry_point: "main", - buffers: &[Vertex::desc()], + buffers: &[Vertex::desc(), InstanceRaw::desc()], }, fragment: Some(wgpu::FragmentState { // 3. @@ -354,6 +361,41 @@ impl State { let num_indices = INDICES.len() as u32; + // Instances + + let instances = (0..NUM_INSTANCES_PER_ROW) + .flat_map(|z| { + (0..NUM_INSTANCES_PER_ROW).map(move |x| { + let position = cgmath::Vector3 { + x: x as f32, + y: 0.0, + z: z as f32, + } - INSTANCE_DISPLACEMENT; + + let rotation = if position.is_zero() { + // this is needed so an object at (0, 0, 0) won't get scaled to zero + // as Quaternions can effect scale if they're not created correctly + cgmath::Quaternion::from_axis_angle( + cgmath::Vector3::unit_z(), + cgmath::Deg(0.0), + ) + } else { + cgmath::Quaternion::from_axis_angle(position.normalize(), cgmath::Deg(45.0)) + }; + + Instance { position, rotation } + }) + }) + .collect::>(); + + let instance_data = instances.iter().map(Instance::to_raw).collect::>(); + + let instance_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Instance Buffer"), + contents: bytemuck::cast_slice(&instance_data), + usage: wgpu::BufferUsages::VERTEX, + }); + Self { surface, device, @@ -371,6 +413,8 @@ impl State { camera_buffer, camera_bind_group, camera_controller, + instances, + instance_buffer, } } @@ -437,9 +481,10 @@ impl State { render_pass.set_bind_group(1, &self.camera_bind_group, &[]); render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..)); + render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..)); render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16); - render_pass.draw_indexed(0..self.num_indices, 0, 0..1); + render_pass.draw_indexed(0..self.num_indices, 0, 0..self.instances.len() as _); } // submit will accept anything that implements IntoIter