1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::global::ProceduralTextureHandle;
use crate::internal::conversion::*;
use crate::internal::wit;

#[derive(Clone, Copy)]
pub enum Format {
    R8Unorm,
    R8Snorm,
    R8Uint,
    R8Sint,
    R16Uint,
    R16Sint,
    R16Unorm,
    R16Snorm,
    R16Float,
    Rg8Unorm,
    Rg8Snorm,
    Rg8Uint,
    Rg8Sint,
    R32Uint,
    R32Sint,
    R32Float,
    Rg16Uint,
    Rg16Sint,
    Rg16Unorm,
    Rg16Snorm,
    Rg16Float,
    Rgba8Unorm,
    Rgba8UnormSrgb,
    Rgba8Snorm,
    Rgba8Uint,
    Rgba8Sint,
    Bgra8Unorm,
    Bgra8UnormSrgb,
    Rgb9e5Ufloat,
    Rgb10a2Unorm,
    Rg11b10Float,
    Rg32Uint,
    Rg32Sint,
    Rg32Float,
    Rgba16Uint,
    Rgba16Sint,
    Rgba16Unorm,
    Rgba16Snorm,
    Rgba16Float,
    Rgba32Uint,
    Rgba32Sint,
    Rgba32Float,
}

impl IntoBindgen for Format {
    type Item = wit::client_texture::Format;

    fn into_bindgen(self) -> Self::Item {
        match self {
            Format::R8Unorm => Self::Item::R8Unorm,
            Format::R8Snorm => Self::Item::R8Snorm,
            Format::R8Uint => Self::Item::R8Uint,
            Format::R8Sint => Self::Item::R8Sint,
            Format::R16Uint => Self::Item::R16Uint,
            Format::R16Sint => Self::Item::R16Sint,
            Format::R16Unorm => Self::Item::R16Unorm,
            Format::R16Snorm => Self::Item::R16Snorm,
            Format::R16Float => Self::Item::R16Float,
            Format::Rg8Unorm => Self::Item::Rg8Unorm,
            Format::Rg8Snorm => Self::Item::Rg8Snorm,
            Format::Rg8Uint => Self::Item::Rg8Uint,
            Format::Rg8Sint => Self::Item::Rg8Sint,
            Format::R32Uint => Self::Item::R32Uint,
            Format::R32Sint => Self::Item::R32Sint,
            Format::R32Float => Self::Item::R32Float,
            Format::Rg16Uint => Self::Item::Rg16Uint,
            Format::Rg16Sint => Self::Item::Rg16Sint,
            Format::Rg16Unorm => Self::Item::Rg16Unorm,
            Format::Rg16Snorm => Self::Item::Rg16Snorm,
            Format::Rg16Float => Self::Item::Rg16Float,
            Format::Rgba8Unorm => Self::Item::Rgba8Unorm,
            Format::Rgba8UnormSrgb => Self::Item::Rgba8UnormSrgb,
            Format::Rgba8Snorm => Self::Item::Rgba8Snorm,
            Format::Rgba8Uint => Self::Item::Rgba8Uint,
            Format::Rgba8Sint => Self::Item::Rgba8Sint,
            Format::Bgra8Unorm => Self::Item::Bgra8Unorm,
            Format::Bgra8UnormSrgb => Self::Item::Bgra8UnormSrgb,
            Format::Rgb9e5Ufloat => Self::Item::Rgb9e5Ufloat,
            Format::Rgb10a2Unorm => Self::Item::Rgb10a2Unorm,
            Format::Rg11b10Float => Self::Item::Rg11b10Float,
            Format::Rg32Uint => Self::Item::Rg32Uint,
            Format::Rg32Sint => Self::Item::Rg32Sint,
            Format::Rg32Float => Self::Item::Rg32Float,
            Format::Rgba16Uint => Self::Item::Rgba16Uint,
            Format::Rgba16Sint => Self::Item::Rgba16Sint,
            Format::Rgba16Unorm => Self::Item::Rgba16Unorm,
            Format::Rgba16Snorm => Self::Item::Rgba16Snorm,
            Format::Rgba16Float => Self::Item::Rgba16Float,
            Format::Rgba32Uint => Self::Item::Rgba32Uint,
            Format::Rgba32Sint => Self::Item::Rgba32Sint,
            Format::Rgba32Float => Self::Item::Rgba32Float,
        }
    }
}

#[derive(Clone)]
pub struct Descriptor2D<'a> {
    pub width: u32,
    pub height: u32,
    pub format: Format,
    pub data: &'a [u8],
}

impl<'a> IntoBindgen for &'a Descriptor2D<'a> {
    type Item = wit::client_texture::Descriptor2d;

    fn into_bindgen(self) -> Self::Item {
        Self::Item {
            width: self.width,
            height: self.height,
            format: self.format.into_bindgen(),
            data: self.data.to_vec(),
        }
    }
}

pub fn create_2d(desc: &Descriptor2D) -> ProceduralTextureHandle {
    wit::client_texture::create2d(&desc.into_bindgen()).from_bindgen()
}

pub fn destroy(handle: ProceduralTextureHandle) {
    wit::client_texture::destroy(handle.into_bindgen());
}