est_render/gpu/texture/
types.rs

1#[derive(Clone, Hash, Copy, PartialEq, Eq, PartialOrd, Ord)]
2pub struct TextureUsage(u32);
3
4bitflags::bitflags! {
5    impl TextureUsage: u32 {
6        const None = 0b00000000;
7        const Sampler = 0b00000001;
8        const Storage = 0b00000010;
9        const RenderAttachment = 0b00000100;
10    }
11}
12
13impl Into<wgpu::TextureUsages> for TextureUsage {
14    fn into(self) -> wgpu::TextureUsages {
15        let mut usage = wgpu::TextureUsages::empty();
16        if self.contains(TextureUsage::Sampler) {
17            usage |= wgpu::TextureUsages::TEXTURE_BINDING;
18        }
19        if self.contains(TextureUsage::Storage) {
20            usage |= wgpu::TextureUsages::STORAGE_BINDING;
21        }
22        if self.contains(TextureUsage::RenderAttachment) {
23            usage |= wgpu::TextureUsages::RENDER_ATTACHMENT;
24        }
25        usage
26    }
27}
28
29#[derive(Clone, Debug, Hash, Copy, PartialEq, Eq, PartialOrd, Ord)]
30pub enum SampleCount {
31    SampleCount1,
32    SampleCount2,
33    SampleCount4,
34    SampleCount8,
35}
36
37impl Into<u32> for SampleCount {
38    fn into(self) -> u32 {
39        match self {
40            SampleCount::SampleCount1 => 1,
41            SampleCount::SampleCount2 => 2,
42            SampleCount::SampleCount4 => 4,
43            SampleCount::SampleCount8 => 8,
44        }
45    }
46}
47
48#[derive(Clone, Debug, Hash, Copy, PartialEq, Eq, PartialOrd, Ord)]
49pub enum BlendOperation {
50    Add,
51    Subtract,
52    ReverseSubtract,
53    Min,
54    Max,
55}
56
57#[derive(Clone, Debug, Hash, Copy, PartialEq, Eq, PartialOrd, Ord)]
58pub enum BlendFactor {
59    Zero,
60    One,
61    SrcColor,
62    OneMinusSrcColor,
63    DstColor,
64    OneMinusDstColor,
65    SrcAlpha,
66    OneMinusSrcAlpha,
67    DstAlpha,
68    OneMinusDstAlpha,
69    ConstantColor,
70    OneMinusConstantColor,
71    SrcAlphaSaturated,
72    BlendColor,
73    OneMinusBlendColor,
74}
75
76#[derive(Clone, Debug, Hash, Copy, PartialEq, Eq, PartialOrd, Ord)]
77pub struct BlendState {
78    pub color_blend: BlendOperation,
79    pub alpha_blend: BlendOperation,
80    pub color_src_factor: BlendFactor,
81    pub color_dst_factor: BlendFactor,
82    pub alpha_src_factor: BlendFactor,
83    pub alpha_dst_factor: BlendFactor,
84    pub color_blend_constant: [u32; 4],
85}
86
87impl BlendState {
88    pub fn new(
89        color_blend: BlendOperation,
90        alpha_blend: BlendOperation,
91        color_src_factor: BlendFactor,
92        color_dst_factor: BlendFactor,
93        alpha_src_factor: BlendFactor,
94        alpha_dst_factor: BlendFactor,
95        color_blend_constant: [u32; 4],
96    ) -> Self {
97        Self {
98            color_blend,
99            alpha_blend,
100            color_src_factor,
101            color_dst_factor,
102            alpha_src_factor,
103            alpha_dst_factor,
104            color_blend_constant,
105        }
106    }
107
108    pub(crate) fn from_wgpu(
109        state: Option<wgpu::BlendState>,
110        color_write_mask: Option<wgpu::ColorWrites>,
111    ) -> Self {
112        let mut write_mask = [0x00, 0x00, 0x00, 0x00];
113        if let Some(mask) = color_write_mask {
114            if mask.contains(wgpu::ColorWrites::RED) {
115                write_mask[0] = 0xFF;
116            }
117            if mask.contains(wgpu::ColorWrites::GREEN) {
118                write_mask[1] = 0xFF;
119            }
120            if mask.contains(wgpu::ColorWrites::BLUE) {
121                write_mask[2] = 0xFF;
122            }
123            if mask.contains(wgpu::ColorWrites::ALPHA) {
124                write_mask[3] = 0xFF;
125            }
126        }
127
128        let mut blend = Self::NONE;
129        if let Some(state) = state {
130            blend.color_blend = Self::wgpu_op_to_blend_op(state.color.operation);
131            blend.alpha_blend = Self::wgpu_op_to_blend_op(state.alpha.operation);
132            blend.color_src_factor = Self::wgpu_factor_to_blend_factor(state.color.src_factor);
133            blend.color_dst_factor = Self::wgpu_factor_to_blend_factor(state.color.dst_factor);
134            blend.alpha_src_factor = Self::wgpu_factor_to_blend_factor(state.alpha.src_factor);
135            blend.alpha_dst_factor = Self::wgpu_factor_to_blend_factor(state.alpha.dst_factor);
136        }
137
138        blend.color_blend_constant = write_mask;
139
140        blend
141    }
142
143    pub(crate) fn wgpu_op_to_blend_op(op: wgpu::BlendOperation) -> BlendOperation {
144        match op {
145            wgpu::BlendOperation::Add => BlendOperation::Add,
146            wgpu::BlendOperation::Subtract => BlendOperation::Subtract,
147            wgpu::BlendOperation::ReverseSubtract => BlendOperation::ReverseSubtract,
148            wgpu::BlendOperation::Min => BlendOperation::Min,
149            wgpu::BlendOperation::Max => BlendOperation::Max,
150        }
151    }
152
153    pub(crate) fn wgpu_factor_to_blend_factor(factor: wgpu::BlendFactor) -> BlendFactor {
154        match factor {
155            wgpu::BlendFactor::Zero => BlendFactor::Zero,
156            wgpu::BlendFactor::One => BlendFactor::One,
157            wgpu::BlendFactor::Src => BlendFactor::SrcColor,
158            wgpu::BlendFactor::OneMinusSrc => BlendFactor::OneMinusSrcColor,
159            wgpu::BlendFactor::Dst => BlendFactor::DstColor,
160            wgpu::BlendFactor::OneMinusDst => BlendFactor::OneMinusDstColor,
161            wgpu::BlendFactor::SrcAlpha => BlendFactor::SrcAlpha,
162            wgpu::BlendFactor::OneMinusSrcAlpha => BlendFactor::OneMinusSrcAlpha,
163            wgpu::BlendFactor::DstAlpha => BlendFactor::DstAlpha,
164            wgpu::BlendFactor::OneMinusDstAlpha => BlendFactor::OneMinusDstAlpha,
165            wgpu::BlendFactor::SrcAlphaSaturated => BlendFactor::SrcAlphaSaturated,
166            wgpu::BlendFactor::Constant => BlendFactor::ConstantColor,
167            wgpu::BlendFactor::OneMinusConstant => BlendFactor::OneMinusConstantColor,
168            _ => {
169                panic!("Unsupported blend factor: {:?}", factor);
170            }
171        }
172    }
173
174    pub(crate) fn blend_op_convert(op: BlendOperation) -> wgpu::BlendOperation {
175        match op {
176            BlendOperation::Add => wgpu::BlendOperation::Add,
177            BlendOperation::Subtract => wgpu::BlendOperation::Subtract,
178            BlendOperation::ReverseSubtract => wgpu::BlendOperation::ReverseSubtract,
179            BlendOperation::Min => wgpu::BlendOperation::Min,
180            BlendOperation::Max => wgpu::BlendOperation::Max,
181        }
182    }
183
184    pub(crate) fn blend_factor_convert(op: BlendFactor) -> wgpu::BlendFactor {
185        match op {
186            BlendFactor::Zero => wgpu::BlendFactor::Zero,
187            BlendFactor::One => wgpu::BlendFactor::One,
188            BlendFactor::SrcColor => wgpu::BlendFactor::Src,
189            BlendFactor::OneMinusSrcColor => wgpu::BlendFactor::OneMinusSrc,
190            BlendFactor::DstColor => wgpu::BlendFactor::Dst,
191            BlendFactor::OneMinusDstColor => wgpu::BlendFactor::OneMinusDst,
192            BlendFactor::SrcAlpha => wgpu::BlendFactor::SrcAlpha,
193            BlendFactor::OneMinusSrcAlpha => wgpu::BlendFactor::OneMinusSrcAlpha,
194            BlendFactor::DstAlpha => wgpu::BlendFactor::DstAlpha,
195            BlendFactor::OneMinusDstAlpha => wgpu::BlendFactor::OneMinusDstAlpha,
196            BlendFactor::SrcAlphaSaturated => wgpu::BlendFactor::SrcAlphaSaturated,
197            BlendFactor::BlendColor => wgpu::BlendFactor::Constant,
198            BlendFactor::OneMinusBlendColor => wgpu::BlendFactor::OneMinusConstant,
199            BlendFactor::ConstantColor => wgpu::BlendFactor::Constant,
200            BlendFactor::OneMinusConstantColor => wgpu::BlendFactor::OneMinusConstant,
201        }
202    }
203
204    pub const NONE: Self = Self {
205        color_blend: BlendOperation::Add,
206        alpha_blend: BlendOperation::Add,
207        color_src_factor: BlendFactor::One,
208        color_dst_factor: BlendFactor::Zero,
209        alpha_src_factor: BlendFactor::One,
210        alpha_dst_factor: BlendFactor::Zero,
211        color_blend_constant: [0xFF, 0xFF, 0xFF, 0xFF],
212    };
213
214    pub const ALPHA_BLEND: Self = Self {
215        color_blend: BlendOperation::Add,
216        alpha_blend: BlendOperation::Add,
217        color_src_factor: BlendFactor::SrcAlpha,
218        color_dst_factor: BlendFactor::OneMinusSrcAlpha,
219        alpha_src_factor: BlendFactor::One,
220        alpha_dst_factor: BlendFactor::Zero,
221        color_blend_constant: [0xFF, 0xFF, 0xFF, 0xFF],
222    };
223
224    pub const ADDITIVE_BLEND: Self = Self {
225        color_blend: BlendOperation::Add,
226        alpha_blend: BlendOperation::Add,
227        color_src_factor: BlendFactor::One,
228        color_dst_factor: BlendFactor::One,
229        alpha_src_factor: BlendFactor::One,
230        alpha_dst_factor: BlendFactor::Zero,
231        color_blend_constant: [0xFF, 0xFF, 0xFF, 0xFF],
232    };
233
234    pub const MULTIPLY_BLEND: Self = Self {
235        color_blend: BlendOperation::Add,
236        alpha_blend: BlendOperation::Add,
237        color_src_factor: BlendFactor::DstColor,
238        color_dst_factor: BlendFactor::Zero,
239        alpha_src_factor: BlendFactor::DstAlpha,
240        alpha_dst_factor: BlendFactor::Zero,
241        color_blend_constant: [0xFF, 0xFF, 0xFF, 0xFF],
242    };
243
244    pub const MODULATE_BLEND: Self = Self {
245        color_blend: BlendOperation::Add,
246        alpha_blend: BlendOperation::Add,
247        color_src_factor: BlendFactor::SrcColor,
248        color_dst_factor: BlendFactor::DstColor,
249        alpha_src_factor: BlendFactor::SrcAlpha,
250        alpha_dst_factor: BlendFactor::DstAlpha,
251        color_blend_constant: [0xFF, 0xFF, 0xFF, 0xFF],
252    };
253
254    pub(crate) fn create_wgpu_blend_state(&self) -> wgpu::BlendState {
255        wgpu::BlendState {
256            color: wgpu::BlendComponent {
257                src_factor: Self::blend_factor_convert(self.color_src_factor),
258                dst_factor: Self::blend_factor_convert(self.color_dst_factor),
259                operation: Self::blend_op_convert(self.color_blend),
260            },
261            alpha: wgpu::BlendComponent {
262                src_factor: Self::blend_factor_convert(self.alpha_src_factor),
263                dst_factor: Self::blend_factor_convert(self.alpha_dst_factor),
264                operation: Self::blend_op_convert(self.alpha_blend),
265            },
266        }
267    }
268
269    pub(crate) fn create_wgpu_color_write_mask(&self) -> wgpu::ColorWrites {
270        let write_mask = self.color_blend_constant[0]
271            | self.color_blend_constant[1] << 8
272            | self.color_blend_constant[2] << 16
273            | self.color_blend_constant[3] << 24;
274
275        let mut mask = wgpu::ColorWrites::empty();
276        if write_mask & 0x00000001 != 0 {
277            mask |= wgpu::ColorWrites::RED;
278        }
279        if write_mask & 0x00000100 != 0 {
280            mask |= wgpu::ColorWrites::GREEN;
281        }
282        if write_mask & 0x00010000 != 0 {
283            mask |= wgpu::ColorWrites::BLUE;
284        }
285        if write_mask & 0x01000000 != 0 {
286            mask |= wgpu::ColorWrites::ALPHA;
287        }
288        if mask.is_empty() {
289            wgpu::ColorWrites::ALL
290        } else {
291            mask
292        }
293    }
294}
295
296impl Into<wgpu::BlendState> for BlendState {
297    fn into(self) -> wgpu::BlendState {
298        self.create_wgpu_blend_state()
299    }
300}
301
302impl Into<wgpu::ColorWrites> for BlendState {
303    fn into(self) -> wgpu::ColorWrites {
304        self.create_wgpu_color_write_mask()
305    }
306}
307
308#[derive(Clone, Hash, Copy, PartialEq, Eq, PartialOrd, Ord)]
309pub enum AddressMode {
310    ClampToEdge = 0,
311    Repeat = 1,
312    MirrorRepeat = 2,
313    ClampToBorder = 3,
314}
315
316impl Into<wgpu::AddressMode> for AddressMode {
317    fn into(self) -> wgpu::AddressMode {
318        match self {
319            AddressMode::ClampToEdge => wgpu::AddressMode::ClampToEdge,
320            AddressMode::Repeat => wgpu::AddressMode::Repeat,
321            AddressMode::MirrorRepeat => wgpu::AddressMode::MirrorRepeat,
322            AddressMode::ClampToBorder => wgpu::AddressMode::ClampToBorder,
323        }
324    }
325}
326
327#[derive(Clone, Hash, Copy, PartialEq, Eq, PartialOrd, Ord)]
328pub enum FilterMode {
329    Nearest,
330    Linear,
331}
332
333impl Into<wgpu::FilterMode> for FilterMode {
334    fn into(self) -> wgpu::FilterMode {
335        match self {
336            FilterMode::Nearest => wgpu::FilterMode::Nearest,
337            FilterMode::Linear => wgpu::FilterMode::Linear,
338        }
339    }
340}
341
342#[derive(Clone, Hash, Copy, PartialEq, Eq, PartialOrd, Ord)]
343pub enum CompareFunction {
344    Never,
345    Less,
346    Equal,
347    LessEqual,
348    Greater,
349    NotEqual,
350    GreaterEqual,
351    Always,
352}
353
354impl Into<wgpu::CompareFunction> for CompareFunction {
355    fn into(self) -> wgpu::CompareFunction {
356        match self {
357            CompareFunction::Never => wgpu::CompareFunction::Never,
358            CompareFunction::Less => wgpu::CompareFunction::Less,
359            CompareFunction::Equal => wgpu::CompareFunction::Equal,
360            CompareFunction::LessEqual => wgpu::CompareFunction::LessEqual,
361            CompareFunction::Greater => wgpu::CompareFunction::Greater,
362            CompareFunction::NotEqual => wgpu::CompareFunction::NotEqual,
363            CompareFunction::GreaterEqual => wgpu::CompareFunction::GreaterEqual,
364            CompareFunction::Always => wgpu::CompareFunction::Always,
365        }
366    }
367}
368
369#[derive(Clone, Hash, Copy, PartialEq, Eq, PartialOrd, Ord)]
370pub enum SamplerBorderColor {
371    TransparentBlack,
372    OpaqueBlack,
373    OpaqueWhite,
374}
375
376impl Into<wgpu::SamplerBorderColor> for SamplerBorderColor {
377    fn into(self) -> wgpu::SamplerBorderColor {
378        match self {
379            SamplerBorderColor::TransparentBlack => wgpu::SamplerBorderColor::TransparentBlack,
380            SamplerBorderColor::OpaqueBlack => wgpu::SamplerBorderColor::OpaqueBlack,
381            SamplerBorderColor::OpaqueWhite => wgpu::SamplerBorderColor::OpaqueWhite,
382        }
383    }
384}
385
386#[derive(Clone, Copy)]
387pub struct TextureSampler {
388    pub address_mode_u: AddressMode,
389    pub address_mode_v: AddressMode,
390    pub address_mode_w: AddressMode,
391    pub mag_filter: FilterMode,
392    pub min_filter: FilterMode,
393    pub mipmap_filter: FilterMode,
394    pub lod_min_clamp: f32,
395    pub lod_max_clamp: f32,
396    pub compare: Option<CompareFunction>,
397    pub anisotropy_clamp: Option<u16>,
398    pub border_color: Option<SamplerBorderColor>,
399}
400
401impl TextureSampler {
402    pub fn new(
403        address_mode_u: AddressMode,
404        address_mode_v: AddressMode,
405        address_mode_w: AddressMode,
406        mag_filter: FilterMode,
407        min_filter: FilterMode,
408        mipmap_filter: FilterMode,
409        lod_min_clamp: f32,
410        lod_max_clamp: f32,
411        compare: Option<CompareFunction>,
412        anisotropy_clamp: Option<u16>,
413        border_color: Option<SamplerBorderColor>,
414    ) -> Self {
415        Self {
416            address_mode_u,
417            address_mode_v,
418            address_mode_w,
419            mag_filter,
420            min_filter,
421            mipmap_filter,
422            lod_min_clamp,
423            lod_max_clamp,
424            compare,
425            anisotropy_clamp,
426            border_color,
427        }
428    }
429
430    pub fn make_wgpu(&self, device: &wgpu::Device) -> wgpu::Sampler {
431        let desc = wgpu::SamplerDescriptor {
432            label: Some("texture sampler"),
433            address_mode_u: self.address_mode_u.into(),
434            address_mode_v: self.address_mode_v.into(),
435            address_mode_w: self.address_mode_w.into(),
436            mag_filter: self.mag_filter.into(),
437            min_filter: self.min_filter.into(),
438            mipmap_filter: self.mipmap_filter.into(),
439            lod_min_clamp: self.lod_min_clamp,
440            lod_max_clamp: self.lod_max_clamp,
441            compare: self.compare.map(|x| x.into()),
442            anisotropy_clamp: self.anisotropy_clamp.unwrap_or(1u16),
443            border_color: self.border_color.map(|x| x.into()),
444        };
445
446        device.create_sampler(&desc)
447    }
448
449    pub const DEFAULT: Self = Self {
450        address_mode_u: AddressMode::ClampToEdge,
451        address_mode_v: AddressMode::ClampToEdge,
452        address_mode_w: AddressMode::ClampToEdge,
453        mag_filter: FilterMode::Linear,
454        min_filter: FilterMode::Linear,
455        mipmap_filter: FilterMode::Nearest,
456        lod_min_clamp: 0.0,
457        lod_max_clamp: 1000.0,
458        compare: None,
459        anisotropy_clamp: None,
460        border_color: None,
461    };
462}
463
464impl Eq for TextureSampler {}
465
466impl PartialEq for TextureSampler {
467    fn eq(&self, other: &Self) -> bool {
468        self.address_mode_u == other.address_mode_u
469            && self.address_mode_v == other.address_mode_v
470            && self.address_mode_w == other.address_mode_w
471            && self.mag_filter == other.mag_filter
472            && self.min_filter == other.min_filter
473            && self.mipmap_filter == other.mipmap_filter
474            && self.lod_min_clamp == other.lod_min_clamp
475            && self.lod_max_clamp == other.lod_max_clamp
476            && self.compare == other.compare
477            && self.anisotropy_clamp == other.anisotropy_clamp
478            && self.border_color == other.border_color
479    }
480}
481
482#[derive(Debug, Clone, Hash, Copy, PartialEq, Eq, PartialOrd, Ord)]
483pub enum TextureFormat {
484    // Normal 8 bit formats
485    /// Red channel only. 8 bit integer per channel. [0, 255] converted to/from float [0, 1] in shader.
486    R8Unorm,
487    /// Red channel only. 8 bit integer per channel. [-127, 127] converted to/from float [-1, 1] in shader.
488    R8Snorm,
489    /// Red channel only. 8 bit integer per channel. Unsigned in shader.
490    R8Uint,
491    /// Red channel only. 8 bit integer per channel. Signed in shader.
492    R8Sint,
493
494    // Normal 16 bit formats
495    /// Red channel only. 16 bit integer per channel. Unsigned in shader.
496    R16Uint,
497    /// Red channel only. 16 bit integer per channel. Signed in shader.
498    R16Sint,
499    /// Red channel only. 16 bit float per channel. Float in shader.
500    R16Float,
501    /// Red and green channels. 8 bit integer per channel. [0, 255] converted to/from float [0, 1] in shader.
502    Rg8Unorm,
503    /// Red and green channels. 8 bit integer per channel. [-127, 127] converted to/from float [-1, 1] in shader.
504    Rg8Snorm,
505    /// Red and green channels. 8 bit integer per channel. Unsigned in shader.
506    Rg8Uint,
507    /// Red and green channels. 8 bit integer per channel. Signed in shader.
508    Rg8Sint,
509
510    // Normal 32 bit formats
511    /// Red channel only. 32 bit integer per channel. Unsigned in shader.
512    R32Uint,
513    /// Red channel only. 32 bit integer per channel. Signed in shader.
514    R32Sint,
515    /// Red channel only. 32 bit float per channel. Float in shader.
516    R32Float,
517    /// Red and green channels. 16 bit integer per channel. Unsigned in shader.
518    Rg16Uint,
519    /// Red and green channels. 16 bit integer per channel. Signed in shader.
520    Rg16Sint,
521    /// Red and green channels. 16 bit float per channel. Float in shader.
522    Rg16Float,
523    /// Red, green, blue, and alpha channels. 8 bit integer per channel. [0, 255] converted to/from float [0, 1] in shader.
524    Rgba8Unorm,
525    /// Red, green, blue, and alpha channels. 8 bit integer per channel. Srgb-color [0, 255] converted to/from linear-color float [0, 1] in shader.
526    Rgba8UnormSrgb,
527    /// Red, green, blue, and alpha channels. 8 bit integer per channel. [-127, 127] converted to/from float [-1, 1] in shader.
528    Rgba8Snorm,
529    /// Red, green, blue, and alpha channels. 8 bit integer per channel. Unsigned in shader.
530    Rgba8Uint,
531    /// Red, green, blue, and alpha channels. 8 bit integer per channel. Signed in shader.
532    Rgba8Sint,
533    /// Blue, green, red, and alpha channels. 8 bit integer per channel. [0, 255] converted to/from float [0, 1] in shader.
534    Bgra8Unorm,
535    /// Blue, green, red, and alpha channels. 8 bit integer per channel. Srgb-color [0, 255] converted to/from linear-color float [0, 1] in shader.
536    Bgra8UnormSrgb,
537
538    // Packed 32 bit formats
539    /// Packed unsigned float with 9 bits mantisa for each RGB component, then a common 5 bits exponent
540    Rgb9e5Ufloat,
541    /// Red, green, blue, and alpha channels. 10 bit integer for RGB channels, 2 bit integer for alpha channel. Unsigned in shader.
542    Rgb10a2Uint,
543    /// Red, green, blue, and alpha channels. 10 bit integer for RGB channels, 2 bit integer for alpha channel. [0, 1023] ([0, 3] for alpha) converted to/from float [0, 1] in shader.
544    Rgb10a2Unorm,
545    /// Red, green, and blue channels. 11 bit float with no sign bit for RG channels. 10 bit float with no sign bit for blue channel. Float in shader.
546    Rg11b10Ufloat,
547
548    // Normal 64 bit formats
549    /// Red and green channels. 32 bit integer per channel. Unsigned in shader.
550    Rg32Uint,
551    /// Red and green channels. 32 bit integer per channel. Signed in shader.
552    Rg32Sint,
553    /// Red and green channels. 32 bit float per channel. Float in shader.
554    Rg32Float,
555    /// Red, green, blue, and alpha channels. 16 bit integer per channel. Unsigned in shader.
556    Rgba16Uint,
557    /// Red, green, blue, and alpha channels. 16 bit integer per channel. Signed in shader.
558    Rgba16Sint,
559    /// Red, green, blue, and alpha channels. 16 bit float per channel. Float in shader.
560    Rgba16Float,
561
562    // Normal 128 bit formats
563    /// Red, green, blue, and alpha channels. 32 bit integer per channel. Unsigned in shader.
564    Rgba32Uint,
565    /// Red, green, blue, and alpha channels. 32 bit integer per channel. Signed in shader.
566    Rgba32Sint,
567    /// Red, green, blue, and alpha channels. 32 bit float per channel. Float in shader.
568    Rgba32Float,
569
570    // Depth and stencil formats
571    /// Stencil format with 8 bit integer stencil.
572    Stencil8,
573    /// Special depth format with 16 bit integer depth.
574    Depth16Unorm,
575    /// Special depth format with at least 24 bit integer depth.
576    Depth24Plus,
577    /// Special depth/stencil format with at least 24 bit integer depth and 8 bits integer stencil.
578    Depth24PlusStencil8,
579    /// Special depth format with 32 bit floating point depth.
580    Depth32Float,
581    /// Special depth/stencil format with 32 bit floating point depth and 8 bits integer stencil.
582    Depth32FloatStencil8,
583}
584
585impl TextureFormat {
586    pub fn get_size(&self) -> u32 {
587        match self {
588            TextureFormat::R8Unorm => 1,
589            TextureFormat::R8Snorm => 1,
590            TextureFormat::R8Uint => 1,
591            TextureFormat::R8Sint => 1,
592            TextureFormat::R16Uint => 2,
593            TextureFormat::R16Sint => 2,
594            TextureFormat::R16Float => 2,
595            TextureFormat::Rg8Unorm => 2,
596            TextureFormat::Rg8Snorm => 2,
597            TextureFormat::Rg8Uint => 2,
598            TextureFormat::Rg8Sint => 2,
599            TextureFormat::R32Uint => 4,
600            TextureFormat::R32Sint => 4,
601            TextureFormat::R32Float => 4,
602            TextureFormat::Rg16Uint => 4,
603            TextureFormat::Rg16Sint => 4,
604            TextureFormat::Rg16Float => 4,
605            TextureFormat::Rgba8Unorm => 4,
606            TextureFormat::Rgba8UnormSrgb => 4,
607            TextureFormat::Rgba8Snorm => 4,
608            TextureFormat::Rgba8Uint => 4,
609            TextureFormat::Rgba8Sint => 4,
610            TextureFormat::Bgra8Unorm => 4,
611            TextureFormat::Bgra8UnormSrgb => 4,
612            TextureFormat::Rgb9e5Ufloat => 4,
613            TextureFormat::Rgb10a2Uint => 4,
614            TextureFormat::Rgb10a2Unorm => 4,
615            TextureFormat::Rg11b10Ufloat => 4,
616            TextureFormat::Rg32Uint => 8,
617            TextureFormat::Rg32Sint => 8,
618            TextureFormat::Rg32Float => 8,
619            TextureFormat::Rgba16Uint => 8,
620            TextureFormat::Rgba16Sint => 8,
621            TextureFormat::Rgba16Float => 8,
622            TextureFormat::Rgba32Uint => 16,
623            TextureFormat::Rgba32Sint => 16,
624            TextureFormat::Rgba32Float => 16,
625            TextureFormat::Stencil8 => 1,
626            TextureFormat::Depth16Unorm => 2,
627            TextureFormat::Depth24Plus => 3,
628            TextureFormat::Depth24PlusStencil8 => 4,
629            TextureFormat::Depth32Float => 4,
630            TextureFormat::Depth32FloatStencil8 => 5,
631        }
632    }
633}
634
635impl Into<wgpu::TextureFormat> for TextureFormat {
636    fn into(self) -> wgpu::TextureFormat {
637        match self {
638            TextureFormat::R8Unorm => wgpu::TextureFormat::R8Unorm,
639            TextureFormat::R8Snorm => wgpu::TextureFormat::R8Snorm,
640            TextureFormat::R8Uint => wgpu::TextureFormat::R8Uint,
641            TextureFormat::R8Sint => wgpu::TextureFormat::R8Sint,
642            TextureFormat::R16Uint => wgpu::TextureFormat::R16Uint,
643            TextureFormat::R16Sint => wgpu::TextureFormat::R16Sint,
644            TextureFormat::R16Float => wgpu::TextureFormat::R16Float,
645            TextureFormat::Rg8Unorm => wgpu::TextureFormat::Rg8Unorm,
646            TextureFormat::Rg8Snorm => wgpu::TextureFormat::Rg8Snorm,
647            TextureFormat::Rg8Uint => wgpu::TextureFormat::Rg8Uint,
648            TextureFormat::Rg8Sint => wgpu::TextureFormat::Rg8Sint,
649            TextureFormat::R32Uint => wgpu::TextureFormat::R32Uint,
650            TextureFormat::R32Sint => wgpu::TextureFormat::R32Sint,
651            TextureFormat::R32Float => wgpu::TextureFormat::R32Float,
652            TextureFormat::Rg16Uint => wgpu::TextureFormat::Rg16Uint,
653            TextureFormat::Rg16Sint => wgpu::TextureFormat::Rg16Sint,
654            TextureFormat::Rg16Float => wgpu::TextureFormat::Rg16Float,
655            TextureFormat::Rgba8Unorm => wgpu::TextureFormat::Rgba8Unorm,
656            TextureFormat::Rgba8UnormSrgb => wgpu::TextureFormat::Rgba8UnormSrgb,
657            TextureFormat::Rgba8Snorm => wgpu::TextureFormat::Rgba8Snorm,
658            TextureFormat::Rgba8Uint => wgpu::TextureFormat::Rgba8Uint,
659            TextureFormat::Rgba8Sint => wgpu::TextureFormat::Rgba8Sint,
660            TextureFormat::Bgra8Unorm => wgpu::TextureFormat::Bgra8Unorm,
661            TextureFormat::Bgra8UnormSrgb => wgpu::TextureFormat::Bgra8UnormSrgb,
662            TextureFormat::Rgb9e5Ufloat => wgpu::TextureFormat::Rgb9e5Ufloat,
663            TextureFormat::Rgb10a2Uint => wgpu::TextureFormat::Rgb10a2Uint,
664            TextureFormat::Rgb10a2Unorm => wgpu::TextureFormat::Rgb10a2Unorm,
665            TextureFormat::Rg11b10Ufloat => wgpu::TextureFormat::Rg11b10Ufloat,
666            TextureFormat::Rg32Uint => wgpu::TextureFormat::Rg32Uint,
667            TextureFormat::Rg32Sint => wgpu::TextureFormat::Rg32Sint,
668            TextureFormat::Rg32Float => wgpu::TextureFormat::Rg32Float,
669            TextureFormat::Rgba16Uint => wgpu::TextureFormat::Rgba16Uint,
670            TextureFormat::Rgba16Sint => wgpu::TextureFormat::Rgba16Sint,
671            TextureFormat::Rgba16Float => wgpu::TextureFormat::Rgba16Float,
672            TextureFormat::Rgba32Uint => wgpu::TextureFormat::Rgba32Uint,
673            TextureFormat::Rgba32Sint => wgpu::TextureFormat::Rgba32Sint,
674            TextureFormat::Rgba32Float => wgpu::TextureFormat::Rgba32Float,
675            TextureFormat::Stencil8 => wgpu::TextureFormat::Stencil8,
676            TextureFormat::Depth16Unorm => wgpu::TextureFormat::Depth16Unorm,
677            TextureFormat::Depth24Plus => wgpu::TextureFormat::Depth24Plus,
678            TextureFormat::Depth24PlusStencil8 => wgpu::TextureFormat::Depth24PlusStencil8,
679            TextureFormat::Depth32Float => wgpu::TextureFormat::Depth32Float,
680            TextureFormat::Depth32FloatStencil8 => wgpu::TextureFormat::Depth32FloatStencil8,
681        }
682    }
683}
684
685impl From<wgpu::TextureFormat> for TextureFormat {
686    fn from(format: wgpu::TextureFormat) -> Self {
687        match format {
688            wgpu::TextureFormat::R8Unorm => TextureFormat::R8Unorm,
689            wgpu::TextureFormat::R8Snorm => TextureFormat::R8Snorm,
690            wgpu::TextureFormat::R8Uint => TextureFormat::R8Uint,
691            wgpu::TextureFormat::R8Sint => TextureFormat::R8Sint,
692            wgpu::TextureFormat::R16Uint => TextureFormat::R16Uint,
693            wgpu::TextureFormat::R16Sint => TextureFormat::R16Sint,
694            wgpu::TextureFormat::R16Float => TextureFormat::R16Float,
695            wgpu::TextureFormat::Rg8Unorm => TextureFormat::Rg8Unorm,
696            wgpu::TextureFormat::Rg8Snorm => TextureFormat::Rg8Snorm,
697            wgpu::TextureFormat::Rg8Uint => TextureFormat::Rg8Uint,
698            wgpu::TextureFormat::Rg8Sint => TextureFormat::Rg8Sint,
699            wgpu::TextureFormat::R32Uint => TextureFormat::R32Uint,
700            wgpu::TextureFormat::R32Sint => TextureFormat::R32Sint,
701            wgpu::TextureFormat::R32Float => TextureFormat::R32Float,
702            wgpu::TextureFormat::Rg16Uint => TextureFormat::Rg16Uint,
703            wgpu::TextureFormat::Rg16Sint => TextureFormat::Rg16Sint,
704            wgpu::TextureFormat::Rg16Float => TextureFormat::Rg16Float,
705            wgpu::TextureFormat::Rgba8Unorm => TextureFormat::Rgba8Unorm,
706            wgpu::TextureFormat::Rgba8UnormSrgb => TextureFormat::Rgba8UnormSrgb,
707            wgpu::TextureFormat::Rgba8Snorm => TextureFormat::Rgba8Snorm,
708            wgpu::TextureFormat::Rgba8Uint => TextureFormat::Rgba8Uint,
709            wgpu::TextureFormat::Rgba8Sint => TextureFormat::Rgba8Sint,
710            wgpu::TextureFormat::Bgra8Unorm => TextureFormat::Bgra8Unorm,
711            wgpu::TextureFormat::Bgra8UnormSrgb => TextureFormat::Bgra8UnormSrgb,
712            wgpu::TextureFormat::Rgb9e5Ufloat => TextureFormat::Rgb9e5Ufloat,
713            wgpu::TextureFormat::Rgb10a2Uint => TextureFormat::Rgb10a2Uint,
714            wgpu::TextureFormat::Rgb10a2Unorm => TextureFormat::Rgb10a2Unorm,
715            wgpu::TextureFormat::Rg11b10Ufloat => TextureFormat::Rg11b10Ufloat,
716            wgpu::TextureFormat::Rg32Uint => TextureFormat::Rg32Uint,
717            wgpu::TextureFormat::Rg32Sint => TextureFormat::Rg32Sint,
718            wgpu::TextureFormat::Rg32Float => TextureFormat::Rg32Float,
719            wgpu::TextureFormat::Rgba16Uint => TextureFormat::Rgba16Uint,
720            wgpu::TextureFormat::Rgba16Sint => TextureFormat::Rgba16Sint,
721            wgpu::TextureFormat::Rgba16Float => TextureFormat::Rgba16Float,
722            wgpu::TextureFormat::Rgba32Uint => TextureFormat::Rgba32Uint,
723            wgpu::TextureFormat::Rgba32Sint => TextureFormat::Rgba32Sint,
724            wgpu::TextureFormat::Rgba32Float => TextureFormat::Rgba32Float,
725            wgpu::TextureFormat::Stencil8 => TextureFormat::Stencil8,
726            wgpu::TextureFormat::Depth16Unorm => TextureFormat::Depth16Unorm,
727            wgpu::TextureFormat::Depth24Plus => TextureFormat::Depth24Plus,
728            wgpu::TextureFormat::Depth24PlusStencil8 => TextureFormat::Depth24PlusStencil8,
729            wgpu::TextureFormat::Depth32Float => TextureFormat::Depth32Float,
730            wgpu::TextureFormat::Depth32FloatStencil8 => TextureFormat::Depth32FloatStencil8,
731            _ => panic!("Unsupported texture format"),
732        }
733    }
734}