steamengine_renderer/
vertex.rs

1use wgpu::VertexBufferLayout;
2/// This trait is the layout of one vertex
3/// ## Example
4/// ```rust
5///
6/// // The vertex has a position and color values
7/// #[repr(C)]
8/// #[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
9/// pub struct Vertex3DColor {
10///     pub position: [f32; 3],
11///     pub color: [f32; 3],
12/// }
13/// impl Vertex for Vertex3DColor {
14/// // This is the description of the vertex
15///
16///   const ATTRIBS: [wgpu::VertexAttribute; 2] =
17///      wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3];
18///
19///     fn desc() -> wgpu::VertexBufferLayout<'static> {
20///        use std::mem;
21///
22///        wgpu::VertexBufferLayout {
23///            array_stride: mem::size_of::<Self>() as wgpu::BufferAddress,
24///            step_mode: wgpu::VertexStepMode::Vertex,
25///            attributes: &Self::ATTRIBS,
26///        }
27///     }
28/// }
29/// ```
30pub trait Vertex: Copy + Clone + bytemuck::Pod + bytemuck::Zeroable {
31    fn desc() -> VertexBufferLayout<'static>;
32}
33
34#[repr(C)]
35#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
36pub struct VertexBasicWithTexture {
37    pub position: [f32; 3],
38    pub tex_coords: [f32; 2],
39}
40impl Vertex for VertexBasicWithTexture {
41    fn desc() -> wgpu::VertexBufferLayout<'static> {
42        VertexBufferLayout {
43            array_stride: std::mem::size_of::<Self>() as wgpu::BufferAddress,
44            step_mode: wgpu::VertexStepMode::Vertex,
45            attributes: &[
46                wgpu::VertexAttribute {
47                    offset: 0,
48                    shader_location: 0,
49                    format: wgpu::VertexFormat::Float32x3,
50                },
51                wgpu::VertexAttribute {
52                    offset: std::mem::size_of::<[f32; 3]>() as wgpu::BufferAddress,
53                    shader_location: 1,
54                    format: wgpu::VertexFormat::Float32x2,
55                },
56            ],
57        }
58    }
59}