Skip to main content

easy_gpu/assets/
vertex_layout.rs

1use wgpu::{VertexAttribute, VertexBufferLayout, VertexFormat, VertexStepMode};
2
3pub struct BufferLayout {
4    attributes: Vec<VertexAttribute>,
5    stride: u64,
6    step_mode: VertexStepMode,
7}
8
9impl Default for BufferLayout {
10    fn default() -> Self {
11        Self::new()
12    }
13}
14
15impl BufferLayout{
16    pub fn new()->Self{
17        Self{
18            attributes: vec![],
19            stride: 0,
20            step_mode: VertexStepMode::Vertex,
21        }
22    }
23
24    pub fn attribute(mut self,shader_location: u32,offset: u64,format: VertexFormat)-> Self{
25        self.attributes.push(VertexAttribute{
26            format,
27            offset,
28            shader_location,
29        });
30        self
31    }
32
33    pub fn stride(mut self, stride: u64)->Self{
34        self.stride = stride;
35        self
36    }
37    pub fn step_mode(mut self,mode: VertexStepMode)->Self{
38        self.step_mode = mode;
39        self
40    }
41
42    pub(crate) fn to_wgpu_layout(&self)->wgpu::VertexBufferLayout<'static>{
43        let attrs: &'static [wgpu::VertexAttribute] =
44            Box::leak(self.attributes.clone().into_boxed_slice());
45        VertexBufferLayout{
46            array_stride: self.stride,
47            step_mode: self.step_mode,
48            attributes: attrs,
49        }
50    }
51}
52
53pub trait GpuInstance: bytemuck::Pod + bytemuck::Zeroable{
54    fn buffer_layout()->BufferLayout;
55}
56
57pub trait GpuVertex: bytemuck::Pod + bytemuck::Zeroable{
58    fn buffer_layout()->BufferLayout;
59}