summaryrefslogtreecommitdiff
path: root/src/vertex.rs
blob: 6514fd33224e85afed4f8dbad7dbe0fdcdf09018 (plain)
use std::mem::size_of;

use bytemuck::{Pod, Zeroable};

/// The vertices needed to form a square
pub const SQUARE: [Vertex; 4] = [
	Vertex::new(-0.5, -0.5),
	Vertex::new(0.5, -0.5),
	Vertex::new(-0.5, 0.5),
	Vertex::new(0.5, 0.5),
];

#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Pod, Zeroable)]
pub struct Vertex {
	position: [f32; 2],
}

impl Vertex {
	const fn new(x: f32, y: f32) -> Self {
		Self { position: [x, y] }
	}
}

impl Vertex {
	// whenever this is updated, please also update `sprite.wgsl`
	pub(crate) const ATTRIBUTES: [wgpu::VertexAttribute; 1] =
		wgpu::vertex_attr_array![0 => Float32x2];

	pub(crate) const fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
		wgpu::VertexBufferLayout {
			array_stride: size_of::<Self>() as wgpu::BufferAddress,
			step_mode: wgpu::VertexStepMode::Vertex,
			attributes: &Self::ATTRIBUTES,
		}
	}
}