1
0
Fork 0

[INSTANCING] From tutorial

This commit is contained in:
Florian RICHER 2022-06-12 22:18:35 +02:00
parent 9165d8e93f
commit 1a9fc9fb63
4 changed files with 147 additions and 25 deletions

View file

@ -1,4 +1,5 @@
use super::vertex::Vertex;
use cgmath::prelude::*;
use wgpu::util::DeviceExt;
use winit::{event::WindowEvent, window::Window};
@ -27,6 +28,13 @@ const VERTICES: &[Vertex] = &[
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
const NUM_INSTANCES_PER_ROW: u32 = 10;
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 State {
pub surface: wgpu::Surface,
pub device: wgpu::Device,
@ -40,6 +48,8 @@ pub struct State {
camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup,
camera_controller: super::camera::CameraController,
instances: Vec<super::instance::Instance>,
instance_buffer: wgpu::Buffer,
// num_vertices: u32,
index_buffer: wgpu::Buffer,
num_indices: u32,
@ -182,17 +192,15 @@ impl State {
let mut camera_uniform = super::camera::CameraUniform::new();
camera_uniform.update_view_proj(&camera);
let camera_buffer = device.create_buffer_init(
&wgpu::util::BufferInitDescriptor {
label: Some("Camera Buffer"),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
}
);
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Camera Buffer"),
contents: bytemuck::cast_slice(&[camera_uniform]),
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
});
let camera_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
wgpu::BindGroupLayoutEntry {
let camera_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
@ -201,19 +209,16 @@ impl State {
min_binding_size: None,
},
count: None,
}
],
label: Some("camera_bind_group_layout"),
});
}],
label: Some("camera_bind_group_layout"),
});
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &camera_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: camera_buffer.as_entire_binding(),
}
],
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: camera_buffer.as_entire_binding(),
}],
label: Some("camera_bind_group"),
});
@ -226,13 +231,48 @@ impl State {
push_constant_ranges: &[],
});
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))
};
super::instance::Instance { position, rotation }
})
})
.collect::<Vec<_>>();
let instance_data = instances
.iter()
.map(super::instance::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,
});
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render Pipeline"),
layout: Some(&render_pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: "vs_main",
buffers: &[Vertex::desc()],
buffers: &[Vertex::desc(), super::instance::InstanceRaw::desc()],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
@ -262,7 +302,7 @@ impl State {
alpha_to_coverage_enabled: false,
},
multiview: None,
});
});
Self {
surface,
@ -282,6 +322,8 @@ impl State {
num_indices,
diffuse_bind_group,
diffuse_texture,
instances,
instance_buffer,
}
}
@ -301,7 +343,11 @@ impl State {
pub fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(&self.camera_buffer, 0, bytemuck::cast_slice(&[self.camera_uniform]));
self.queue.write_buffer(
&self.camera_buffer,
0,
bytemuck::cast_slice(&[self.camera_uniform]),
);
}
pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> {
@ -342,9 +388,10 @@ impl State {
render_pass.set_bind_group(0, &self.diffuse_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(1, self.instance_buffer.slice(..));
render_pass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
// render_pass.draw(0..self.num_vertices, 0..1);
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