From 0a2da9194c08cdb49a233c4d951b34d9949e00e6 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 27 Nov 2026 19:16:18 +0100 Subject: The vertex buffer Theoretically I could stop here for now, but because of best practices and forward compatibility I'll also finish the index buffer implementation before moving on to Pong --- games/00_triangle/Cargo.toml | 3 +- games/00_triangle/src/main.rs | 63 +++++++++++++++++++++++++++++++++++++-- games/00_triangle/src/shader.wgsl | 15 ++++++---- 3 files changed, 72 insertions(+), 9 deletions(-) (limited to 'games/00_triangle') diff --git a/games/00_triangle/Cargo.toml b/games/00_triangle/Cargo.toml index 646903a..e165e4b 100644 --- a/games/00_triangle/Cargo.toml +++ b/games/00_triangle/Cargo.toml @@ -10,4 +10,5 @@ winit = { version = "0.30", default-features = false, features = ["wayland", "rw env_logger = "0.10" log = "0.4" wgpu = "30.0" -pollster = "0.3" \ No newline at end of file +pollster = "0.3" +bytemuck = { version = "1.24", features = [ "derive" ] } 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::() 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, 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, @location(1) color : vec3, +}; + struct VertexOutput { - @builtin(position) clip_position : vec4, + @builtin(position) clip_position : vec4, @location(0) color : vec3, }; -@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(x, y, 0.0, 1.0); + out.color = model.color; + out.clip_position = vec4(model.position, 1.0); return out; } // Fragment shader @fragment fn fs_main(in : VertexOutput) -> @location(0) vec4 { - return vec4(0.3, 0.2, 0.1, 1.0); + return vec4(in.color, 1.0); } -- cgit v1.2.3