est_render/gpu/shader/
types.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2pub enum ShaderTopology {
3    PointList,
4    LineList,
5    LineStrip,
6    TriangleList,
7    TriangleStrip,
8}
9
10impl Into<wgpu::PrimitiveTopology> for ShaderTopology {
11    fn into(self) -> wgpu::PrimitiveTopology {
12        match self {
13            ShaderTopology::PointList => wgpu::PrimitiveTopology::PointList,
14            ShaderTopology::LineList => wgpu::PrimitiveTopology::LineList,
15            ShaderTopology::LineStrip => wgpu::PrimitiveTopology::LineStrip,
16            ShaderTopology::TriangleList => wgpu::PrimitiveTopology::TriangleList,
17            ShaderTopology::TriangleStrip => wgpu::PrimitiveTopology::TriangleStrip,
18        }
19    }
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
23pub enum ShaderCullMode {
24    Front,
25    Back,
26}
27
28impl Into<wgpu::Face> for ShaderCullMode {
29    fn into(self) -> wgpu::Face {
30        match self {
31            ShaderCullMode::Front => wgpu::Face::Front,
32            ShaderCullMode::Back => wgpu::Face::Back,
33        }
34    }
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub enum ShaderPollygonMode {
39    Fill,
40    Line,
41    Point,
42}
43
44impl Into<wgpu::PolygonMode> for ShaderPollygonMode {
45    fn into(self) -> wgpu::PolygonMode {
46        match self {
47            ShaderPollygonMode::Fill => wgpu::PolygonMode::Fill,
48            ShaderPollygonMode::Line => wgpu::PolygonMode::Line,
49            ShaderPollygonMode::Point => wgpu::PolygonMode::Point,
50        }
51    }
52}
53
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
55pub enum ShaderFrontFace {
56    Clockwise,
57    CounterClockwise,
58}
59
60impl Into<wgpu::FrontFace> for ShaderFrontFace {
61    fn into(self) -> wgpu::FrontFace {
62        match self {
63            ShaderFrontFace::Clockwise => wgpu::FrontFace::Cw,
64            ShaderFrontFace::CounterClockwise => wgpu::FrontFace::Ccw,
65        }
66    }
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
70pub struct StorageAccess(u32);
71
72bitflags::bitflags! {
73    impl StorageAccess: u32 {
74        const READ = 0b0001;
75        const WRITE = 0b0010;
76        const ATOMIC = 0b0100;
77    }
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
81pub enum ShaderBindingType {
82    UniformBuffer(u32),
83    StorageBuffer(u32, StorageAccess),
84    StorageTexture(StorageAccess),
85    Sampler(bool),
86    Texture(bool),
87    PushConstant(u32),
88}
89
90impl std::fmt::Display for ShaderBindingType {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        match self {
93            ShaderBindingType::UniformBuffer(size) => write!(f, "UniformBuffer({})", size),
94            ShaderBindingType::StorageBuffer(size, access) => {
95                write!(f, "StorageBuffer({}, {:?})", size, access)
96            }
97            ShaderBindingType::StorageTexture(access) => {
98                write!(f, "StorageTexture({:?})", access)
99            }
100            ShaderBindingType::Sampler(is_compare) => {
101                write!(f, "Sampler({})", is_compare)
102            }
103            ShaderBindingType::Texture(is_storage) => {
104                write!(f, "Texture({})", is_storage)
105            }
106            ShaderBindingType::PushConstant(size) => write!(f, "PushConstant({})", size),
107        }
108    }
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
112pub enum IndexBufferSize {
113    U16,
114    U32,
115}
116
117impl Into<wgpu::IndexFormat> for IndexBufferSize {
118    fn into(self) -> wgpu::IndexFormat {
119        match self {
120            IndexBufferSize::U16 => wgpu::IndexFormat::Uint16,
121            IndexBufferSize::U32 => wgpu::IndexFormat::Uint32,
122        }
123    }
124}
125
126#[derive(Debug, Clone, PartialEq, Eq, Hash)]
127pub struct ShaderBindingInfo {
128    pub binding: u32,
129    pub group: u32,
130    pub name: String,
131    pub ty: ShaderBindingType,
132}
133
134#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
135pub enum VertexInputType {
136    Uint8,
137    Uint8x2,
138    Uint8x4,
139    Sint8,
140    Sint8x2,
141    Sint8x4,
142    Unorm8,
143    Unorm8x2,
144    Unorm8x4,
145    Snorm8,
146    Snorm8x2,
147    Snorm8x4,
148    Uint16,
149    Uint16x2,
150    Uint16x4,
151    Sint16,
152    Sint16x2,
153    Sint16x4,
154    Unorm16,
155    Unorm16x2,
156    Unorm16x4,
157    Snorm16,
158    Snorm16x2,
159    Snorm16x4,
160    Float16,
161    Float16x2,
162    Float16x4,
163    Uint32,
164    Uint32x2,
165    Uint32x3,
166    Uint32x4,
167    Sint32,
168    Sint32x2,
169    Sint32x3,
170    Sint32x4,
171    Float32,
172    Float32x2,
173    Float32x3,
174    Float32x4,
175}
176
177impl Into<wgpu::VertexFormat> for VertexInputType {
178    fn into(self) -> wgpu::VertexFormat {
179        match self {
180            VertexInputType::Uint8 => wgpu::VertexFormat::Uint8,
181            VertexInputType::Uint8x2 => wgpu::VertexFormat::Uint8x2,
182            VertexInputType::Uint8x4 => wgpu::VertexFormat::Uint8x4,
183            VertexInputType::Sint8 => wgpu::VertexFormat::Sint8,
184            VertexInputType::Sint8x2 => wgpu::VertexFormat::Sint8x2,
185            VertexInputType::Sint8x4 => wgpu::VertexFormat::Sint8x4,
186            VertexInputType::Unorm8 => wgpu::VertexFormat::Unorm8,
187            VertexInputType::Unorm8x2 => wgpu::VertexFormat::Unorm8x2,
188            VertexInputType::Unorm8x4 => wgpu::VertexFormat::Unorm8x4,
189            VertexInputType::Snorm8 => wgpu::VertexFormat::Snorm8,
190            VertexInputType::Snorm8x2 => wgpu::VertexFormat::Snorm8x2,
191            VertexInputType::Snorm8x4 => wgpu::VertexFormat::Snorm8x4,
192            VertexInputType::Uint16 => wgpu::VertexFormat::Uint16,
193            VertexInputType::Uint16x2 => wgpu::VertexFormat::Uint16x2,
194            VertexInputType::Uint16x4 => wgpu::VertexFormat::Uint16x4,
195            VertexInputType::Sint16 => wgpu::VertexFormat::Sint16,
196            VertexInputType::Sint16x2 => wgpu::VertexFormat::Sint16x2,
197            VertexInputType::Sint16x4 => wgpu::VertexFormat::Sint16x4,
198            VertexInputType::Unorm16 => wgpu::VertexFormat::Unorm16,
199            VertexInputType::Unorm16x2 => wgpu::VertexFormat::Unorm16x2,
200            VertexInputType::Unorm16x4 => wgpu::VertexFormat::Unorm16x4,
201            VertexInputType::Snorm16 => wgpu::VertexFormat::Snorm16,
202            VertexInputType::Snorm16x2 => wgpu::VertexFormat::Snorm16x2,
203            VertexInputType::Snorm16x4 => wgpu::VertexFormat::Snorm16x4,
204            VertexInputType::Float16 => wgpu::VertexFormat::Float16,
205            VertexInputType::Float16x2 => wgpu::VertexFormat::Float16x2,
206            VertexInputType::Float16x4 => wgpu::VertexFormat::Float16x4,
207            VertexInputType::Uint32 => wgpu::VertexFormat::Uint32,
208            VertexInputType::Uint32x2 => wgpu::VertexFormat::Uint32x2,
209            VertexInputType::Uint32x3 => wgpu::VertexFormat::Uint32x3,
210            VertexInputType::Uint32x4 => wgpu::VertexFormat::Uint32x4,
211            VertexInputType::Sint32 => wgpu::VertexFormat::Sint32,
212            VertexInputType::Sint32x2 => wgpu::VertexFormat::Sint32x2,
213            VertexInputType::Sint32x3 => wgpu::VertexFormat::Sint32x3,
214            VertexInputType::Sint32x4 => wgpu::VertexFormat::Sint32x4,
215            VertexInputType::Float32 => wgpu::VertexFormat::Float32,
216            VertexInputType::Float32x2 => wgpu::VertexFormat::Float32x2,
217            VertexInputType::Float32x3 => wgpu::VertexFormat::Float32x3,
218            VertexInputType::Float32x4 => wgpu::VertexFormat::Float32x4,
219        }
220    }
221}
222
223#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
224pub struct VertexInputAttribute {
225    pub shader_location: u32,
226    pub offset: u64,
227    pub format: VertexInputType,
228}
229
230#[derive(Debug, Clone, PartialEq, Eq, Hash)]
231pub struct VertexInputDesc {
232    pub stride: u64,
233    pub attributes: Vec<VertexInputAttribute>,
234}
235
236#[derive(Debug, Clone, Eq, Hash)]
237pub enum ShaderReflect {
238    Vertex {
239        entry_point: String,
240        input: Option<VertexInputReflection>,
241        bindings: Vec<ShaderBindingInfo>,
242    },
243    Fragment {
244        entry_point: String,
245        bindings: Vec<ShaderBindingInfo>,
246    },
247    VertexFragment {
248        vertex_entry_point: String,
249        vertex_input: Option<VertexInputReflection>,
250        fragment_entry_point: String,
251        bindings: Vec<ShaderBindingInfo>,
252    },
253    Compute {
254        entry_point: String,
255        bindings: Vec<ShaderBindingInfo>,
256    },
257}
258
259impl PartialEq for ShaderReflect {
260    fn eq(&self, other: &Self) -> bool {
261        match (self, other) {
262            (
263                ShaderReflect::Vertex {
264                    entry_point,
265                    input,
266                    bindings,
267                },
268                ShaderReflect::Vertex {
269                    entry_point: other_entry_point,
270                    input: other_input,
271                    bindings: other_bindings,
272                },
273            ) => {
274                entry_point == other_entry_point
275                    && input == other_input
276                    && bindings == other_bindings
277            }
278            (
279                ShaderReflect::Fragment {
280                    entry_point,
281                    bindings,
282                },
283                ShaderReflect::Fragment {
284                    entry_point: other_entry_point,
285                    bindings: other_bindings,
286                },
287            ) => entry_point == other_entry_point && bindings == other_bindings,
288            (
289                ShaderReflect::VertexFragment {
290                    vertex_entry_point,
291                    vertex_input,
292                    fragment_entry_point,
293                    bindings,
294                },
295                ShaderReflect::VertexFragment {
296                    vertex_entry_point: other_vertex_entry_point,
297                    vertex_input: other_vertex_input,
298                    fragment_entry_point: other_fragment_entry_point,
299                    bindings: other_bindings,
300                },
301            ) => {
302                vertex_entry_point == other_vertex_entry_point
303                    && vertex_input == other_vertex_input
304                    && fragment_entry_point == other_fragment_entry_point
305                    && bindings == other_bindings
306            }
307            (
308                ShaderReflect::Compute {
309                    entry_point,
310                    bindings,
311                },
312                ShaderReflect::Compute {
313                    entry_point: other_entry_point,
314                    bindings: other_bindings,
315                },
316            ) => entry_point == other_entry_point && bindings == other_bindings,
317            _ => false,
318        }
319    }
320}
321
322#[derive(Debug, Clone, PartialEq, Eq, Hash)]
323pub struct BindGroupLayout {
324    pub group: u32,
325    pub bindings: Vec<u32>,
326    pub layout: wgpu::BindGroupLayout,
327}
328
329#[derive(Debug, Clone, Eq, Hash)]
330pub struct VertexInputReflection {
331    pub name: String,
332    pub stride: u64,
333    pub attributes: Vec<(u32, u64, VertexInputType)>,
334}
335
336impl PartialEq for VertexInputReflection {
337    fn eq(&self, other: &Self) -> bool {
338        self.name == other.name
339            && self.stride == other.stride
340            && self.attributes == other.attributes
341    }
342}