Skip to main content

agpu/
vertex.rs

1//! Vertex types for the agpu shape renderer.
2
3use crate::types::{
4    BufferAddress, VertexAttribute, VertexBufferLayout, VertexFormat, VertexStepMode,
5};
6
7/// A 2D vertex with position and RGBA colour.
8#[repr(C)]
9#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
10pub struct Vertex {
11    pub position: [f32; 2],
12    pub color: [f32; 4],
13}
14
15impl Vertex {
16    pub const LAYOUT: VertexBufferLayout<'static> = VertexBufferLayout {
17        array_stride: std::mem::size_of::<Vertex>() as BufferAddress,
18        step_mode: VertexStepMode::Vertex,
19        attributes: &[
20            // position
21            VertexAttribute {
22                offset: 0,
23                shader_location: 0,
24                format: VertexFormat::Float32x2,
25            },
26            // color
27            VertexAttribute {
28                offset: std::mem::size_of::<[f32; 2]>() as BufferAddress,
29                shader_location: 1,
30                format: VertexFormat::Float32x4,
31            },
32        ],
33    };
34
35    #[inline]
36    pub fn new(x: f32, y: f32, r: f32, g: f32, b: f32, a: f32) -> Self {
37        Self {
38            position: [x, y],
39            color: [r, g, b, a],
40        }
41    }
42}