Skip to main content

pebble/wgpu/
vertex_format.rs

1//! Mirrors `wgpu::VertexFormat`/`VertexStepMode`/`VertexAttribute` so a
2//! custom vertex struct's layout (beyond the built-in
3//! [`Vertex`](super::mesh::Vertex)/[`InstanceVertex`](super::mesh::InstanceVertex))
4//! can be built with zero raw wgpu. Unlike `wgpu::VertexBufferLayout` (which
5//! borrows a `'static` attribute slice), [`VertexBufferLayout`] owns its
6//! attributes — simpler to build, converted to wgpu's borrowed shape
7//! internally by [`build_material`](super::material::build_material) in a
8//! scope that doesn't need `'static`.
9
10/// Format of a single vertex attribute — mirrors `wgpu::VertexFormat`
11/// exactly (same variants, same names).
12#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
13pub enum VertexFormat {
14    Uint8,
15    Uint8x2,
16    Uint8x4,
17    Sint8,
18    Sint8x2,
19    Sint8x4,
20    Unorm8,
21    Unorm8x2,
22    Unorm8x4,
23    Snorm8,
24    Snorm8x2,
25    Snorm8x4,
26    Uint16,
27    Uint16x2,
28    Uint16x4,
29    Sint16,
30    Sint16x2,
31    Sint16x4,
32    Unorm16,
33    Unorm16x2,
34    Unorm16x4,
35    Snorm16,
36    Snorm16x2,
37    Snorm16x4,
38    Float16,
39    Float16x2,
40    Float16x4,
41    Float32,
42    Float32x2,
43    Float32x3,
44    Float32x4,
45    Uint32,
46    Uint32x2,
47    Uint32x3,
48    Uint32x4,
49    Sint32,
50    Sint32x2,
51    Sint32x3,
52    Sint32x4,
53    Float64,
54    Float64x2,
55    Float64x3,
56    Float64x4,
57    Unorm10_10_10_2,
58    Unorm8x4Bgra,
59}
60
61impl From<VertexFormat> for wgpu::VertexFormat {
62    fn from(format: VertexFormat) -> Self {
63        match format {
64            VertexFormat::Uint8 => Self::Uint8,
65            VertexFormat::Uint8x2 => Self::Uint8x2,
66            VertexFormat::Uint8x4 => Self::Uint8x4,
67            VertexFormat::Sint8 => Self::Sint8,
68            VertexFormat::Sint8x2 => Self::Sint8x2,
69            VertexFormat::Sint8x4 => Self::Sint8x4,
70            VertexFormat::Unorm8 => Self::Unorm8,
71            VertexFormat::Unorm8x2 => Self::Unorm8x2,
72            VertexFormat::Unorm8x4 => Self::Unorm8x4,
73            VertexFormat::Snorm8 => Self::Snorm8,
74            VertexFormat::Snorm8x2 => Self::Snorm8x2,
75            VertexFormat::Snorm8x4 => Self::Snorm8x4,
76            VertexFormat::Uint16 => Self::Uint16,
77            VertexFormat::Uint16x2 => Self::Uint16x2,
78            VertexFormat::Uint16x4 => Self::Uint16x4,
79            VertexFormat::Sint16 => Self::Sint16,
80            VertexFormat::Sint16x2 => Self::Sint16x2,
81            VertexFormat::Sint16x4 => Self::Sint16x4,
82            VertexFormat::Unorm16 => Self::Unorm16,
83            VertexFormat::Unorm16x2 => Self::Unorm16x2,
84            VertexFormat::Unorm16x4 => Self::Unorm16x4,
85            VertexFormat::Snorm16 => Self::Snorm16,
86            VertexFormat::Snorm16x2 => Self::Snorm16x2,
87            VertexFormat::Snorm16x4 => Self::Snorm16x4,
88            VertexFormat::Float16 => Self::Float16,
89            VertexFormat::Float16x2 => Self::Float16x2,
90            VertexFormat::Float16x4 => Self::Float16x4,
91            VertexFormat::Float32 => Self::Float32,
92            VertexFormat::Float32x2 => Self::Float32x2,
93            VertexFormat::Float32x3 => Self::Float32x3,
94            VertexFormat::Float32x4 => Self::Float32x4,
95            VertexFormat::Uint32 => Self::Uint32,
96            VertexFormat::Uint32x2 => Self::Uint32x2,
97            VertexFormat::Uint32x3 => Self::Uint32x3,
98            VertexFormat::Uint32x4 => Self::Uint32x4,
99            VertexFormat::Sint32 => Self::Sint32,
100            VertexFormat::Sint32x2 => Self::Sint32x2,
101            VertexFormat::Sint32x3 => Self::Sint32x3,
102            VertexFormat::Sint32x4 => Self::Sint32x4,
103            VertexFormat::Float64 => Self::Float64,
104            VertexFormat::Float64x2 => Self::Float64x2,
105            VertexFormat::Float64x3 => Self::Float64x3,
106            VertexFormat::Float64x4 => Self::Float64x4,
107            VertexFormat::Unorm10_10_10_2 => Self::Unorm10_10_10_2,
108            VertexFormat::Unorm8x4Bgra => Self::Unorm8x4Bgra,
109        }
110    }
111}
112
113/// Whether a vertex buffer is indexed by vertex or by instance — mirrors
114/// `wgpu::VertexStepMode`.
115#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
116pub enum VertexStepMode {
117    Vertex,
118    Instance,
119}
120
121impl From<VertexStepMode> for wgpu::VertexStepMode {
122    fn from(mode: VertexStepMode) -> Self {
123        match mode {
124            VertexStepMode::Vertex => Self::Vertex,
125            VertexStepMode::Instance => Self::Instance,
126        }
127    }
128}
129
130/// One vertex attribute within a [`VertexBufferLayout`] — mirrors
131/// `wgpu::VertexAttribute`.
132#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
133pub struct VertexAttribute {
134    pub format: VertexFormat,
135    pub offset: u64,
136    pub shader_location: u32,
137}
138
139impl From<VertexAttribute> for wgpu::VertexAttribute {
140    fn from(attr: VertexAttribute) -> Self {
141        Self {
142            format: attr.format.into(),
143            offset: attr.offset,
144            shader_location: attr.shader_location,
145        }
146    }
147}
148
149/// Layout of one vertex buffer's attributes — owns its attribute list
150/// (unlike `wgpu::VertexBufferLayout`, which borrows a `'static` slice), so
151/// it can be built and passed around without lifetime ceremony.
152#[derive(Clone, Debug, PartialEq)]
153pub struct VertexBufferLayout {
154    pub array_stride: u64,
155    pub step_mode: VertexStepMode,
156    pub attributes: Vec<VertexAttribute>,
157}