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/src/main.rs | 63 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) (limited to 'games/00_triangle/src/main.rs') 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())); -- cgit v1.2.3