Skip to main content

libdqg/renderer/
types.rs

1// ===== Simple enums =====
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum IndexFormat {
5    Uint16,
6    Uint32,
7}
8impl IndexFormat {
9    pub(crate) fn to_wgpu(self) -> wgpu::IndexFormat {
10        match self {
11            IndexFormat::Uint16 => wgpu::IndexFormat::Uint16,
12            IndexFormat::Uint32 => wgpu::IndexFormat::Uint32,
13        }
14    }
15}
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum VertexFormat {
19    Float32,
20    Float32x2,
21    Float32x3,
22    Float32x4,
23    Sint32,
24    Sint32x2,
25    Sint32x3,
26    Sint32x4,
27    Uint32,
28    Uint32x2,
29    Uint32x3,
30    Uint32x4,
31}
32impl VertexFormat {
33    pub(crate) fn to_wgpu(self) -> wgpu::VertexFormat {
34        match self {
35            VertexFormat::Float32 => wgpu::VertexFormat::Float32,
36            VertexFormat::Float32x2 => wgpu::VertexFormat::Float32x2,
37            VertexFormat::Float32x3 => wgpu::VertexFormat::Float32x3,
38            VertexFormat::Float32x4 => wgpu::VertexFormat::Float32x4,
39            VertexFormat::Sint32 => wgpu::VertexFormat::Sint32,
40            VertexFormat::Sint32x2 => wgpu::VertexFormat::Sint32x2,
41            VertexFormat::Sint32x3 => wgpu::VertexFormat::Sint32x3,
42            VertexFormat::Sint32x4 => wgpu::VertexFormat::Sint32x4,
43            VertexFormat::Uint32 => wgpu::VertexFormat::Uint32,
44            VertexFormat::Uint32x2 => wgpu::VertexFormat::Uint32x2,
45            VertexFormat::Uint32x3 => wgpu::VertexFormat::Uint32x3,
46            VertexFormat::Uint32x4 => wgpu::VertexFormat::Uint32x4,
47        }
48    }
49}
50
51#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52pub enum VertexStepMode {
53    Vertex,
54    Instance,
55}
56impl VertexStepMode {
57    pub(crate) fn to_wgpu(self) -> wgpu::VertexStepMode {
58        match self {
59            VertexStepMode::Vertex => wgpu::VertexStepMode::Vertex,
60            VertexStepMode::Instance => wgpu::VertexStepMode::Instance,
61        }
62    }
63}
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub enum PrimitiveTopology {
67    PointList,
68    LineList,
69    LineStrip,
70    TriangleList,
71    TriangleStrip,
72}
73impl PrimitiveTopology {
74    pub(crate) fn to_wgpu(self) -> wgpu::PrimitiveTopology {
75        match self {
76            PrimitiveTopology::PointList => wgpu::PrimitiveTopology::PointList,
77            PrimitiveTopology::LineList => wgpu::PrimitiveTopology::LineList,
78            PrimitiveTopology::LineStrip => wgpu::PrimitiveTopology::LineStrip,
79            PrimitiveTopology::TriangleList => wgpu::PrimitiveTopology::TriangleList,
80            PrimitiveTopology::TriangleStrip => wgpu::PrimitiveTopology::TriangleStrip,
81        }
82    }
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub enum PolygonMode {
87    Fill,
88    Line,
89    Point,
90}
91impl PolygonMode {
92    pub(crate) fn to_wgpu(self) -> wgpu::PolygonMode {
93        match self {
94            PolygonMode::Fill => wgpu::PolygonMode::Fill,
95            PolygonMode::Line => wgpu::PolygonMode::Line,
96            PolygonMode::Point => wgpu::PolygonMode::Point,
97        }
98    }
99}
100
101#[derive(Debug, Clone, Copy, PartialEq, Eq)]
102pub enum FrontFace {
103    Ccw,
104    Cw,
105}
106impl FrontFace {
107    pub(crate) fn to_wgpu(self) -> wgpu::FrontFace {
108        match self {
109            FrontFace::Ccw => wgpu::FrontFace::Ccw,
110            FrontFace::Cw => wgpu::FrontFace::Cw,
111        }
112    }
113}
114
115#[derive(Debug, Clone, Copy, PartialEq, Eq)]
116pub enum CullMode {
117    None,
118    Front,
119    Back,
120}
121impl CullMode {
122    pub(crate) fn to_wgpu(self) -> Option<wgpu::Face> {
123        match self {
124            CullMode::None => None,
125            CullMode::Front => Some(wgpu::Face::Front),
126            CullMode::Back => Some(wgpu::Face::Back),
127        }
128    }
129}
130
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132pub enum BlendFactor {
133    Zero,
134    One,
135    Src,
136    OneMinusSrc,
137    Dst,
138    OneMinusDst,
139    SrcAlpha,
140    OneMinusSrcAlpha,
141    DstAlpha,
142    OneMinusDstAlpha,
143    SrcAlphaSaturated,
144    Constant,
145    OneMinusConstant,
146}
147impl BlendFactor {
148    pub(crate) fn to_wgpu(self) -> wgpu::BlendFactor {
149        match self {
150            BlendFactor::Zero => wgpu::BlendFactor::Zero,
151            BlendFactor::One => wgpu::BlendFactor::One,
152            BlendFactor::Src => wgpu::BlendFactor::Src,
153            BlendFactor::OneMinusSrc => wgpu::BlendFactor::OneMinusSrc,
154            BlendFactor::Dst => wgpu::BlendFactor::Dst,
155            BlendFactor::OneMinusDst => wgpu::BlendFactor::OneMinusDst,
156            BlendFactor::SrcAlpha => wgpu::BlendFactor::SrcAlpha,
157            BlendFactor::DstAlpha => wgpu::BlendFactor::DstAlpha,
158            BlendFactor::OneMinusSrcAlpha => wgpu::BlendFactor::OneMinusSrcAlpha,
159            BlendFactor::OneMinusDstAlpha => wgpu::BlendFactor::OneMinusDstAlpha,
160            BlendFactor::SrcAlphaSaturated => wgpu::BlendFactor::SrcAlphaSaturated,
161            BlendFactor::Constant => wgpu::BlendFactor::Constant,
162            BlendFactor::OneMinusConstant => wgpu::BlendFactor::OneMinusConstant,
163        }
164    }
165}
166
167#[derive(Debug, Clone, Copy, PartialEq, Eq)]
168pub enum BlendOperation {
169    Add,
170    Subtract,
171    ReverseSubtract,
172    Min,
173    Max,
174}
175impl BlendOperation {
176    pub(crate) fn to_wgpu(self) -> wgpu::BlendOperation {
177        match self {
178            BlendOperation::Add => wgpu::BlendOperation::Add,
179            BlendOperation::Subtract => wgpu::BlendOperation::Subtract,
180            BlendOperation::ReverseSubtract => wgpu::BlendOperation::ReverseSubtract,
181            BlendOperation::Min => wgpu::BlendOperation::Min,
182            BlendOperation::Max => wgpu::BlendOperation::Max,
183        }
184    }
185}
186
187#[derive(Debug, Clone, Copy, PartialEq, Eq)]
188pub enum CompareFunction {
189    Never,
190    Less,
191    LessEqual,
192    Equal,
193    GreaterEqual,
194    Greater,
195    NotEqual,
196    Always,
197}
198impl CompareFunction {
199    pub(crate) fn to_wgpu(self) -> wgpu::CompareFunction {
200        match self {
201            CompareFunction::Never => wgpu::CompareFunction::Never,
202            CompareFunction::Less => wgpu::CompareFunction::Less,
203            CompareFunction::LessEqual => wgpu::CompareFunction::LessEqual,
204            CompareFunction::Equal => wgpu::CompareFunction::Equal,
205            CompareFunction::GreaterEqual => wgpu::CompareFunction::GreaterEqual,
206            CompareFunction::Greater => wgpu::CompareFunction::Greater,
207            CompareFunction::NotEqual => wgpu::CompareFunction::NotEqual,
208            CompareFunction::Always => wgpu::CompareFunction::Always,
209        }
210    }
211}
212
213#[derive(Debug, Clone, Copy, PartialEq, Eq)]
214pub enum StencilOperation {
215    Keep,
216    Zero,
217    Replace,
218    Invert,
219    IncrementClamp,
220    DecrementClamp,
221    IncrementWrap,
222    DecrementWrap,
223}
224impl StencilOperation {
225    pub(crate) fn to_wgpu(self) -> wgpu::StencilOperation {
226        match self {
227            StencilOperation::Keep => wgpu::StencilOperation::Keep,
228            StencilOperation::Zero => wgpu::StencilOperation::Zero,
229            StencilOperation::Replace => wgpu::StencilOperation::Replace,
230            StencilOperation::Invert => wgpu::StencilOperation::Invert,
231            StencilOperation::IncrementClamp => wgpu::StencilOperation::IncrementClamp,
232            StencilOperation::DecrementClamp => wgpu::StencilOperation::DecrementClamp,
233            StencilOperation::IncrementWrap => wgpu::StencilOperation::IncrementWrap,
234            StencilOperation::DecrementWrap => wgpu::StencilOperation::DecrementWrap,
235        }
236    }
237}
238
239#[derive(Debug, Clone, Copy, PartialEq, Eq)]
240pub enum BufferUsage {
241    Vertex,
242    Index,
243    Uniform,
244    Storage,
245    Indirect,
246    CopySrc,
247    CopyDst,
248    MapRead,
249    MapWrite,
250}
251impl BufferUsage {
252    pub(crate) fn to_wgpu(self) -> wgpu::BufferUsages {
253        match self {
254            BufferUsage::Vertex => wgpu::BufferUsages::VERTEX,
255            BufferUsage::Index => wgpu::BufferUsages::INDEX,
256            BufferUsage::Uniform => wgpu::BufferUsages::UNIFORM,
257            BufferUsage::Storage => wgpu::BufferUsages::STORAGE,
258            BufferUsage::Indirect => wgpu::BufferUsages::INDIRECT,
259            BufferUsage::CopySrc => wgpu::BufferUsages::COPY_SRC,
260            BufferUsage::CopyDst => wgpu::BufferUsages::COPY_DST,
261            BufferUsage::MapRead => wgpu::BufferUsages::MAP_READ,
262            BufferUsage::MapWrite => wgpu::BufferUsages::MAP_WRITE,
263        }
264    }
265}
266
267#[derive(Debug, Clone, Copy, PartialEq, Eq)]
268pub enum TextureFormat {
269    R8Unorm,
270    Rgba8Unorm,
271    Rgba8UnormSrgb,
272    Bgra8Unorm,
273    Bgra8UnormSrgb,
274    Depth32Float,
275    Depth24PlusStencil8,
276}
277impl TextureFormat {
278    pub(crate) fn from_wgpu(format: wgpu::TextureFormat) -> Self {
279        match format {
280            wgpu::TextureFormat::R8Unorm => TextureFormat::R8Unorm,
281            wgpu::TextureFormat::Rgba8Unorm => TextureFormat::Rgba8Unorm,
282            wgpu::TextureFormat::Rgba8UnormSrgb => TextureFormat::Rgba8UnormSrgb,
283            wgpu::TextureFormat::Bgra8Unorm => TextureFormat::Bgra8Unorm,
284            wgpu::TextureFormat::Bgra8UnormSrgb => TextureFormat::Bgra8UnormSrgb,
285            wgpu::TextureFormat::Depth32Float => TextureFormat::Depth32Float,
286            wgpu::TextureFormat::Depth24PlusStencil8 => TextureFormat::Depth24PlusStencil8,
287            _ => panic!("Unsupported texture format: {:?}", format),
288        }
289    }
290    pub(crate) fn to_wgpu(self) -> wgpu::TextureFormat {
291        match self {
292            TextureFormat::R8Unorm => wgpu::TextureFormat::R8Unorm,
293            TextureFormat::Rgba8Unorm => wgpu::TextureFormat::Rgba8Unorm,
294            TextureFormat::Rgba8UnormSrgb => wgpu::TextureFormat::Rgba8UnormSrgb,
295            TextureFormat::Bgra8Unorm => wgpu::TextureFormat::Bgra8Unorm,
296            TextureFormat::Bgra8UnormSrgb => wgpu::TextureFormat::Bgra8UnormSrgb,
297            TextureFormat::Depth32Float => wgpu::TextureFormat::Depth32Float,
298            TextureFormat::Depth24PlusStencil8 => wgpu::TextureFormat::Depth24PlusStencil8,
299        }
300    }
301}
302
303#[derive(Clone, Copy)]
304pub struct ColorWriteMask(wgpu::ColorWrites);
305impl ColorWriteMask {
306    pub const ALL: Self = Self(wgpu::ColorWrites::ALL);
307    pub fn to_wgpu(self) -> wgpu::ColorWrites {
308        self.0
309    }
310}
311
312// ===== Descriptor structs =====
313
314pub struct ShaderModuleDescriptor<'a> {
315    pub label: Option<&'a str>,
316    pub source: &'a str,
317}
318
319pub struct BufferDescriptor<'a> {
320    pub label: Option<&'a str>,
321    pub size: u64,
322    pub usage: BufferUsage,
323}
324
325pub struct BufferInitDescriptor<'a> {
326    pub label: Option<&'a str>,
327    pub contents: &'a [u8],
328    pub usage: BufferUsage,
329}
330
331pub struct RenderPipelineDescriptor<'a> {
332    pub label: Option<&'a str>,
333    pub vertex: VertexState<'a>,
334    pub fragment: Option<FragmentState<'a>>,
335    pub primitive: PrimitiveState,
336    pub depth_stencil: Option<DepthStencilState>,
337    pub multisample: MultisampleState,
338}
339
340pub struct VertexState<'a> {
341    pub module: &'a wgpu::ShaderModule,
342    pub entry_point: &'a str,
343    pub buffers: &'a [VertexBufferLayout<'a>],
344}
345
346pub struct VertexBufferLayout<'a> {
347    pub array_stride: u64,
348    pub step_mode: VertexStepMode,
349    pub attributes: &'a [VertexAttribute],
350}
351
352#[derive(Clone, Copy)]
353pub struct VertexAttribute {
354    pub format: VertexFormat,
355    pub offset: u64,
356    pub shader_location: u32,
357}
358
359pub struct FragmentState<'a> {
360    pub module: &'a wgpu::ShaderModule,
361    pub entry_point: &'a str,
362    pub targets: &'a [ColorTargetState],
363}
364
365#[derive(Clone)]
366pub struct ColorTargetState {
367    pub format: TextureFormat,
368    pub blend: Option<BlendState>,
369    pub write_mask: ColorWriteMask,
370}
371
372#[derive(Clone)]
373pub struct BlendState {
374    pub color: BlendComponent,
375    pub alpha: BlendComponent,
376}
377
378#[derive(Clone, Copy)]
379pub struct BlendComponent {
380    pub src_factor: BlendFactor,
381    pub dst_factor: BlendFactor,
382    pub operation: BlendOperation,
383}
384
385#[derive(Clone, Copy)]
386pub struct PrimitiveState {
387    pub topology: PrimitiveTopology,
388    pub strip_index_format: Option<IndexFormat>,
389    pub front_face: FrontFace,
390    pub cull_mode: CullMode,
391    pub unclipped_depth: bool,
392    pub polygon_mode: PolygonMode,
393}
394impl Default for PrimitiveState {
395    fn default() -> Self {
396        Self {
397            topology: PrimitiveTopology::TriangleList,
398            strip_index_format: None,
399            front_face: FrontFace::Ccw,
400            cull_mode: CullMode::None,
401            unclipped_depth: false,
402            polygon_mode: PolygonMode::Fill,
403        }
404    }
405}
406
407#[derive(Clone, Copy)]
408pub struct DepthStencilState {
409    pub format: TextureFormat,
410    pub depth_write_enabled: bool,
411    pub depth_compare: CompareFunction,
412    pub stencil: StencilState,
413    pub bias: DepthBiasState,
414}
415
416#[derive(Clone, Copy)]
417pub struct StencilState {
418    pub front: StencilFaceState,
419    pub back: StencilFaceState,
420    pub read_mask: u32,
421    pub write_mask: u32,
422}
423impl Default for StencilState {
424    fn default() -> Self {
425        Self {
426            front: StencilFaceState::default(),
427            back: StencilFaceState::default(),
428            read_mask: 0xFF,
429            write_mask: 0xFF,
430        }
431    }
432}
433
434#[derive(Clone, Copy)]
435pub struct StencilFaceState {
436    pub compare: CompareFunction,
437    pub fail_op: StencilOperation,
438    pub depth_fail_op: StencilOperation,
439    pub pass_op: StencilOperation,
440}
441impl Default for StencilFaceState {
442    fn default() -> Self {
443        Self {
444            compare: CompareFunction::Always,
445            fail_op: StencilOperation::Keep,
446            depth_fail_op: StencilOperation::Keep,
447            pass_op: StencilOperation::Keep,
448        }
449    }
450}
451
452#[derive(Clone, Copy)]
453pub struct DepthBiasState {
454    pub bias: i32,
455    pub slope_scale: f32,
456    pub clamp: f32,
457}
458impl Default for DepthBiasState {
459    fn default() -> Self {
460        Self { bias: 0, slope_scale: 0.0, clamp: 0.0 }
461    }
462}
463
464#[derive(Clone, Copy)]
465pub struct MultisampleState {
466    pub count: u32,
467    pub mask: u64,
468    pub alpha_to_coverage_enabled: bool,
469}
470impl Default for MultisampleState {
471    fn default() -> Self {
472        Self { count: 1, mask: !0, alpha_to_coverage_enabled: false }
473    }
474}