diff options
Diffstat (limited to 'games/00_triangle/src')
| -rw-r--r-- | games/00_triangle/src/main.rs | 63 | ||||
| -rw-r--r-- | games/00_triangle/src/shader.wgsl | 15 |
2 files changed, 70 insertions, 8 deletions
diff --git a/games/00_triangle/src/main.rs b/games/00_triangle/src/main.rs index e9de614..da4d548 100644 --- a/games/00_triangle/src/main.rs +++ b/games/00_triangle/src/main.rs @@ -1,5 +1,6 @@ use std::sync::Arc; +use wgpu::util::DeviceExt; use winit::{ application::ApplicationHandler, event::*, @@ -8,6 +9,51 @@ use winit::{ window::Window, }; +#[repr(C)] +#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)] +struct Vertex { + position: [f32; 3], + color: [f32; 3], +} + +impl Vertex { + fn desc() -> wgpu::VertexBufferLayout<'static> { + wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &[ + //position + wgpu::VertexAttribute { + offset: 0, + shader_location: 0, + format: wgpu::VertexFormat::Float32x3, + }, + //color + wgpu::VertexAttribute { + offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress, + shader_location: 1, + format: wgpu::VertexFormat::Float32x3, + }, + ], + } + } +} + +const VERTICES: &[Vertex] = &[ + Vertex { + position: [0.0, 0.5, 0.0], + color: [1.0, 0.0, 0.0], + }, + Vertex { + position: [-0.5, -0.5, 0.0], + color: [0.0, 1.0, 0.0], + }, + Vertex { + position: [0.5, -0.5, 0.0], + color: [0.0, 0.0, 1.0], + }, +]; + pub struct State { surface: wgpu::Surface<'static>, device: wgpu::Device, @@ -16,6 +62,8 @@ pub struct State { is_surface_configured: bool, window: Arc<Window>, render_pipeline: wgpu::RenderPipeline, + vertex_buffer: wgpu::Buffer, + num_vertices: u32, } impl State { @@ -86,7 +134,7 @@ impl State { vertex: wgpu::VertexState { module: &shader, entry_point: Some("vs_main"), - buffers: &[], + buffers: &[Some(Vertex::desc())], compilation_options: wgpu::PipelineCompilationOptions::default(), }, fragment: Some(wgpu::FragmentState { @@ -118,6 +166,14 @@ impl State { cache: None, }); + let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("Vertex Buffer"), + contents: bytemuck::cast_slice(VERTICES), + usage: wgpu::BufferUsages::VERTEX, + }); + + let num_vertices = VERTICES.len() as u32; + Ok(Self { surface, device, @@ -126,6 +182,8 @@ impl State { is_surface_configured: false, render_pipeline, window, + vertex_buffer, + num_vertices, }) } @@ -204,7 +262,8 @@ impl State { }); render_pass.set_pipeline(&self.render_pipeline); - render_pass.draw(0..3, 0..1); + render_pass.set_vertex_buffer(0, self.vertex_buffer.slice(..)); + render_pass.draw(0..self.num_vertices, 0..1); } self.queue.submit(std::iter::once(encoder.finish())); diff --git a/games/00_triangle/src/shader.wgsl b/games/00_triangle/src/shader.wgsl index 9ff0e3f..a341d15 100644 --- a/games/00_triangle/src/shader.wgsl +++ b/games/00_triangle/src/shader.wgsl @@ -1,19 +1,22 @@ // Vertex shader +struct VertexInput { + @location(0) position : vec3<f32>, @location(1) color : vec3<f32>, +}; + struct VertexOutput { - @builtin(position) clip_position : vec4<f32>, + @builtin(position) clip_position : vec4<f32>, @location(0) color : vec3<f32>, }; -@vertex fn vs_main(@builtin(vertex_index) in_vertex_index : u32, ) +@vertex fn vs_main(model: VertexInput,) -> VertexOutput { var out : VertexOutput; - let x = f32(1 - i32(in_vertex_index)) * 0.5; - let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5; - out.clip_position = vec4<f32>(x, y, 0.0, 1.0); + out.color = model.color; + out.clip_position = vec4<f32>(model.position, 1.0); return out; } // Fragment shader @fragment fn fs_main(in : VertexOutput) -> @location(0) vec4<f32> { - return vec4<f32>(0.3, 0.2, 0.1, 1.0); + return vec4<f32>(in.color, 1.0); } |
