diff --git a/simulator/Cargo.toml b/simulator/Cargo.toml index 2949409..d4904cd 100644 --- a/simulator/Cargo.toml +++ b/simulator/Cargo.toml @@ -11,4 +11,5 @@ log = "0.4" env_logger = "0.11.8" bytemuck = "1.23.0" wgpu = "25.0" -pollster = "0.4.0" \ No newline at end of file +pollster = "0.4.0" +cgmath = "0.18.0" \ No newline at end of file diff --git a/simulator/src/main.rs b/simulator/src/main.rs index b2d913c..eefe249 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -1,6 +1,7 @@ use std::cmp::max; use std::sync::Arc; - +use cgmath::num_traits::ToPrimitive; +use cgmath::Rotation3; use pollster::FutureExt; use wgpu::util::DeviceExt; use wgpu::{Adapter, Device, Instance, PresentMode, Queue, Surface, SurfaceCapabilities}; @@ -17,6 +18,39 @@ pub async fn run() { let _ = event_loop.run_app(&mut window_state); } +struct RenderInstance { + position: cgmath::Vector3, + rotation: cgmath::Quaternion, +} + +impl RenderInstance { + fn to_raw(&self) -> InstanceRaw { + let model = cgmath::Matrix4::from_translation(self.position) * cgmath::Matrix4::from(self.rotation); + InstanceRaw { model: model.into() } + } +} + +#[repr(C)] +#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] +struct InstanceRaw { + model: [[f32; 4]; 4], +} + +impl InstanceRaw { + fn desc() -> wgpu::VertexBufferLayout<'static> { + wgpu::VertexBufferLayout { + array_stride: size_of::() as wgpu::BufferAddress, + step_mode: wgpu::VertexStepMode::Instance, + attributes: &[ + wgpu::VertexAttribute { offset: 0, shader_location: 5, format: wgpu::VertexFormat::Float32x4 }, + wgpu::VertexAttribute { offset: size_of::<[f32; 4]>() as wgpu::BufferAddress, shader_location: 6, format: wgpu::VertexFormat::Float32x4 }, + wgpu::VertexAttribute { offset: size_of::<[f32; 8]>() as wgpu::BufferAddress, shader_location: 7, format: wgpu::VertexFormat::Float32x4 }, + wgpu::VertexAttribute { offset: size_of::<[f32; 12]>() as wgpu::BufferAddress, shader_location: 8, format: wgpu::VertexFormat::Float32x4 }, + ], + } + } +} + #[repr(C)] #[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)] struct Vertex { @@ -115,6 +149,9 @@ struct State<'a> { vertex_buffer: wgpu::Buffer, index_buffer: wgpu::Buffer, + instances: Vec, + instance_buffer: wgpu::Buffer, + num_vertices: u32, num_indices: u32, } @@ -138,6 +175,23 @@ impl<'a> State<'a> { let num_vertices = VERTICES.len() as u32; let num_indices = INDICES.len() as u32; + let instances = vec![ + RenderInstance { + position: cgmath::Vector3::new(0.0, 0.0, 0.0), + rotation: cgmath::Quaternion::from_angle_z(cgmath::Deg(0.0)), + }, + RenderInstance { + position: cgmath::Vector3::new(1.0, 1.0, 0.0), + rotation: cgmath::Quaternion::from_angle_z(cgmath::Deg(45.0)), + }, + ]; + let instance_data: Vec = instances.iter().map(RenderInstance::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, @@ -149,6 +203,9 @@ impl<'a> State<'a> { render_pipeline, vertex_buffer, index_buffer, + + instances, + instance_buffer, num_vertices, num_indices, @@ -202,7 +259,7 @@ impl<'a> State<'a> { vertex: wgpu::VertexState { module: &shader, entry_point: Some("vs_main"), - buffers: &[Vertex::desc()], + buffers: &[Vertex::desc(), InstanceRaw::desc()], compilation_options: Default::default(), }, fragment: Some(wgpu::FragmentState { @@ -326,8 +383,9 @@ impl<'a> State<'a> { render_pass.set_pipeline(&self.render_pipeline); 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 u32); } self.queue.submit(std::iter::once(encoder.finish())); diff --git a/simulator/src/shader.wgsl b/simulator/src/shader.wgsl index 04fd28b..e97d14b 100644 --- a/simulator/src/shader.wgsl +++ b/simulator/src/shader.wgsl @@ -3,31 +3,33 @@ struct VertexInput { @location(1) color: vec3, }; -struct VertexOutput { - @builtin(position) clip_position: vec4, +struct InstanceInput { + @location(5) model_row0: vec4, + @location(6) model_row1: vec4, + @location(7) model_row2: vec4, + @location(8) model_row3: vec4, +}; + +struct VSOutput { + @builtin(position) position: vec4, @location(0) color: vec3, }; @vertex -fn vs_main( - model: VertexInput, -) -> VertexOutput { - var out: VertexOutput; - out.color = model.color; - out.clip_position = vec4(model.position, 1.0); +fn vs_main(vertex: VertexInput, instance: InstanceInput) -> VSOutput { + var out: VSOutput; + let model = mat4x4( + instance.model_row0, + instance.model_row1, + instance.model_row2, + instance.model_row3 + ); + out.position = model * vec4(vertex.position, 1.0); + out.color = vertex.color; return out; } -fn srgb_to_linear(c: f32) -> f32 { - return pow((c + 0.055) / 1.055, 2.4); -} - @fragment -fn fs_main(in: VertexOutput) -> @location(0) vec4 { - let linear_color = vec3( - srgb_to_linear(in.color.r), - srgb_to_linear(in.color.g), - srgb_to_linear(in.color.b), - ); - return vec4(linear_color, 1.0); -} +fn fs_main(input: VSOutput) -> @location(0) vec4 { + return vec4(input.color, 1.0); +} \ No newline at end of file