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