mirror of
https://github.com/guilhermewerner/wgpu-renderer
synced 2025-06-15 13:24:20 +00:00
Instancing
This commit is contained in:
@ -1,5 +1,12 @@
|
|||||||
// Vertex shader
|
// Vertex shader
|
||||||
|
|
||||||
|
struct InstanceInput {
|
||||||
|
[[location(5)]] model_matrix_0: vec4<f32>;
|
||||||
|
[[location(6)]] model_matrix_1: vec4<f32>;
|
||||||
|
[[location(7)]] model_matrix_2: vec4<f32>;
|
||||||
|
[[location(8)]] model_matrix_3: vec4<f32>;
|
||||||
|
};
|
||||||
|
|
||||||
[[block]]
|
[[block]]
|
||||||
struct CameraUniform {
|
struct CameraUniform {
|
||||||
view_proj: mat4x4<f32>;
|
view_proj: mat4x4<f32>;
|
||||||
@ -21,10 +28,19 @@ struct VertexOutput {
|
|||||||
[[stage(vertex)]]
|
[[stage(vertex)]]
|
||||||
fn main(
|
fn main(
|
||||||
model: VertexInput,
|
model: VertexInput,
|
||||||
|
instance: InstanceInput,
|
||||||
) -> VertexOutput {
|
) -> VertexOutput {
|
||||||
|
let model_matrix = mat4x4<f32>(
|
||||||
|
instance.model_matrix_0,
|
||||||
|
instance.model_matrix_1,
|
||||||
|
instance.model_matrix_2,
|
||||||
|
instance.model_matrix_3,
|
||||||
|
);
|
||||||
|
|
||||||
var out: VertexOutput;
|
var out: VertexOutput;
|
||||||
out.tex_coords = model.tex_coords;
|
out.tex_coords = model.tex_coords;
|
||||||
out.clip_position = camera.view_proj * vec4<f32>(model.position, 1.0); // 3.
|
out.clip_position = camera.view_proj * model_matrix * vec4<f32>(model.position, 1.0);
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
71
Source/Instance.rs
Normal file
71
Source/Instance.rs
Normal file
@ -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<f32> = 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<f32>,
|
||||||
|
pub rotation: cgmath::Quaternion<f32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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::<InstanceRaw>() 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,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,11 +4,16 @@
|
|||||||
mod _Camera;
|
mod _Camera;
|
||||||
use _Camera::*;
|
use _Camera::*;
|
||||||
|
|
||||||
|
#[path = "Instance.rs"]
|
||||||
|
mod _Instance;
|
||||||
|
use _Instance::*;
|
||||||
|
|
||||||
#[path = "Texture.rs"]
|
#[path = "Texture.rs"]
|
||||||
mod _Texture;
|
mod _Texture;
|
||||||
use _Texture::*;
|
use _Texture::*;
|
||||||
|
|
||||||
use bytemuck::{Pod, Zeroable};
|
use bytemuck::{Pod, Zeroable};
|
||||||
|
use cgmath::prelude::*;
|
||||||
use image::GenericImageView;
|
use image::GenericImageView;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use wgpu::util::DeviceExt;
|
use wgpu::util::DeviceExt;
|
||||||
@ -92,6 +97,8 @@ struct State {
|
|||||||
camera_buffer: wgpu::Buffer,
|
camera_buffer: wgpu::Buffer,
|
||||||
camera_bind_group: wgpu::BindGroup,
|
camera_bind_group: wgpu::BindGroup,
|
||||||
camera_controller: CameraController,
|
camera_controller: CameraController,
|
||||||
|
instances: Vec<Instance>,
|
||||||
|
instance_buffer: wgpu::Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
@ -305,7 +312,7 @@ impl State {
|
|||||||
vertex: wgpu::VertexState {
|
vertex: wgpu::VertexState {
|
||||||
module: &shader,
|
module: &shader,
|
||||||
entry_point: "main",
|
entry_point: "main",
|
||||||
buffers: &[Vertex::desc()],
|
buffers: &[Vertex::desc(), InstanceRaw::desc()],
|
||||||
},
|
},
|
||||||
fragment: Some(wgpu::FragmentState {
|
fragment: Some(wgpu::FragmentState {
|
||||||
// 3.
|
// 3.
|
||||||
@ -354,6 +361,41 @@ impl State {
|
|||||||
|
|
||||||
let num_indices = INDICES.len() as u32;
|
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::<Vec<_>>();
|
||||||
|
|
||||||
|
let instance_data = instances.iter().map(Instance::to_raw).collect::<Vec<_>>();
|
||||||
|
|
||||||
|
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 {
|
Self {
|
||||||
surface,
|
surface,
|
||||||
device,
|
device,
|
||||||
@ -371,6 +413,8 @@ impl State {
|
|||||||
camera_buffer,
|
camera_buffer,
|
||||||
camera_bind_group,
|
camera_bind_group,
|
||||||
camera_controller,
|
camera_controller,
|
||||||
|
instances,
|
||||||
|
instance_buffer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -437,9 +481,10 @@ impl State {
|
|||||||
render_pass.set_bind_group(1, &self.camera_bind_group, &[]);
|
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(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.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
|
// submit will accept anything that implements IntoIter
|
||||||
|
Reference in New Issue
Block a user