gfx_backend_gl/
conv.rs

1use crate::native::VertexAttribFunction;
2use hal::{format::Format, image as i, pso};
3
4/*
5pub fn _image_kind_to_gl(kind: i::Kind) -> t::GLenum {
6    match kind {
7        i::Kind::D1(_) => glow::TEXTURE_1D,
8        i::Kind::D1Array(_, _) => glow::TEXTURE_1D_ARRAY,
9        i::Kind::D2(_, _, i::AaMode::Single) => glow::TEXTURE_2D,
10        i::Kind::D2(_, _, _) => glow::TEXTURE_2D_MULTISAMPLE,
11        i::Kind::D2Array(_, _, _, i::AaMode::Single) => glow::TEXTURE_2D_ARRAY,
12        i::Kind::D2Array(_, _, _, _) => glow::TEXTURE_2D_MULTISAMPLE_ARRAY,
13        i::Kind::D3(_, _, _) => glow::TEXTURE_3D,
14        i::Kind::Cube(_) => glow::TEXTURE_CUBE_MAP,
15        i::Kind::CubeArray(_, _) => glow::TEXTURE_CUBE_MAP_ARRAY,
16    }
17}*/
18
19pub fn filter_to_gl(mag: i::Filter, min: i::Filter, mip: i::Filter) -> (u32, u32) {
20    use hal::image::Filter::*;
21
22    let mag_filter = match mag {
23        Nearest => glow::NEAREST,
24        Linear => glow::LINEAR,
25    };
26
27    let min_filter = match (min, mip) {
28        (Nearest, Nearest) => glow::NEAREST_MIPMAP_NEAREST,
29        (Nearest, Linear) => glow::NEAREST_MIPMAP_LINEAR,
30        (Linear, Nearest) => glow::LINEAR_MIPMAP_NEAREST,
31        (Linear, Linear) => glow::LINEAR_MIPMAP_LINEAR,
32    };
33
34    (min_filter, mag_filter)
35}
36
37pub fn wrap_to_gl(w: i::WrapMode) -> u32 {
38    match w {
39        i::WrapMode::Tile => glow::REPEAT,
40        i::WrapMode::Mirror => glow::MIRRORED_REPEAT,
41        i::WrapMode::Clamp => glow::CLAMP_TO_EDGE,
42        i::WrapMode::Border => glow::CLAMP_TO_BORDER,
43        i::WrapMode::MirrorClamp => glow::MIRROR_CLAMP_TO_EDGE,
44    }
45}
46
47pub fn input_assember_to_gl_primitive(ia: &pso::InputAssemblerDesc) -> u32 {
48    match (ia.primitive, ia.with_adjacency) {
49        (pso::Primitive::PointList, false) => glow::POINTS,
50        (pso::Primitive::PointList, true) => panic!("Points can't have adjacency info"),
51        (pso::Primitive::LineList, false) => glow::LINES,
52        (pso::Primitive::LineList, true) => glow::LINES_ADJACENCY,
53        (pso::Primitive::LineStrip, false) => glow::LINE_STRIP,
54        (pso::Primitive::LineStrip, true) => glow::LINE_STRIP_ADJACENCY,
55        (pso::Primitive::TriangleList, false) => glow::TRIANGLES,
56        (pso::Primitive::TriangleList, true) => glow::TRIANGLES_ADJACENCY,
57        (pso::Primitive::TriangleStrip, false) => glow::TRIANGLE_STRIP,
58        (pso::Primitive::TriangleStrip, true) => glow::TRIANGLE_STRIP_ADJACENCY,
59        (pso::Primitive::PatchList(_), false) => glow::PATCHES,
60        (pso::Primitive::PatchList(_), true) => panic!("Patches can't have adjacency info"),
61    }
62}
63
64pub struct FormatDescription {
65    pub tex_internal: u32,
66    pub tex_external: u32,
67    pub data_type: u32,
68    pub num_components: u8,
69    pub va_fun: VertexAttribFunction,
70}
71
72impl FormatDescription {
73    fn new(
74        tex_internal: u32,
75        tex_external: u32,
76        data_type: u32,
77        num_components: u8,
78        va_fun: VertexAttribFunction,
79    ) -> Self {
80        FormatDescription {
81            tex_internal,
82            tex_external,
83            data_type,
84            num_components,
85            va_fun,
86        }
87    }
88}
89
90pub fn describe_format(format: Format) -> Option<FormatDescription> {
91    use crate::native::VertexAttribFunction::*;
92    use hal::format::Format::*;
93    let _ = Double; //mark as used
94
95    // TODO: Add more formats and error handling for `None`
96    Some(match format {
97        R8Uint => FormatDescription::new(
98            glow::R8UI,
99            glow::RED_INTEGER,
100            glow::UNSIGNED_BYTE,
101            1,
102            Integer,
103        ),
104        R8Sint => FormatDescription::new(glow::R8I, glow::RED_INTEGER, glow::BYTE, 1, Integer),
105        R8Unorm => FormatDescription::new(glow::R8, glow::RED, glow::UNSIGNED_BYTE, 1, Float),
106        Rg8Uint => FormatDescription::new(
107            glow::RG8UI,
108            glow::RG_INTEGER,
109            glow::UNSIGNED_BYTE,
110            2,
111            Integer,
112        ),
113        Rg8Sint => FormatDescription::new(glow::RG8I, glow::RG_INTEGER, glow::BYTE, 2, Integer),
114        Rgba8Uint => FormatDescription::new(
115            glow::RGBA8UI,
116            glow::RGBA_INTEGER,
117            glow::UNSIGNED_BYTE,
118            4,
119            Integer,
120        ),
121        Rgba8Sint => {
122            FormatDescription::new(glow::RGBA8I, glow::RGBA_INTEGER, glow::BYTE, 4, Integer)
123        }
124        Rgba8Unorm => {
125            FormatDescription::new(glow::RGBA8, glow::RGBA, glow::UNSIGNED_BYTE, 4, Float)
126        }
127        Rgb8Srgb => FormatDescription::new(glow::SRGB8, glow::RGB, glow::UNSIGNED_BYTE, 3, Float),
128        Rgba8Srgb => FormatDescription::new(
129            glow::SRGB8_ALPHA8,
130            glow::RGBA,
131            glow::UNSIGNED_BYTE,
132            4,
133            Float,
134        ),
135        Bgra8Unorm => FormatDescription::new(glow::BGRA, glow::BGRA, glow::UNSIGNED_BYTE, 4, Float),
136        Bgra8Srgb => FormatDescription::new(
137            glow::SRGB8_ALPHA8,
138            glow::BGRA,
139            glow::UNSIGNED_BYTE,
140            4,
141            Float,
142        ),
143        R16Uint => FormatDescription::new(
144            glow::R16UI,
145            glow::RED_INTEGER,
146            glow::UNSIGNED_SHORT,
147            1,
148            Integer,
149        ),
150        R16Sint => FormatDescription::new(glow::R16I, glow::RED_INTEGER, glow::SHORT, 1, Integer),
151        R16Sfloat => FormatDescription::new(glow::R16F, glow::RED, glow::HALF_FLOAT, 1, Float),
152        R16Unorm => FormatDescription::new(glow::R16, glow::RED, glow::UNSIGNED_SHORT, 1, Float),
153        Rg16Uint => FormatDescription::new(
154            glow::RG16UI,
155            glow::RG_INTEGER,
156            glow::UNSIGNED_SHORT,
157            2,
158            Integer,
159        ),
160        Rg16Sint => FormatDescription::new(glow::RG16I, glow::RG_INTEGER, glow::SHORT, 2, Integer),
161        Rg16Unorm => FormatDescription::new(glow::RG16, glow::RG, glow::UNSIGNED_SHORT, 2, Float),
162        Rg16Sfloat => FormatDescription::new(glow::RG16F, glow::RG, glow::HALF_FLOAT, 2, Float),
163        Rgba16Uint => FormatDescription::new(
164            glow::RGBA16UI,
165            glow::RGBA_INTEGER,
166            glow::UNSIGNED_SHORT,
167            4,
168            Integer,
169        ),
170        Rgba16Sint => {
171            FormatDescription::new(glow::RGBA16I, glow::RGBA_INTEGER, glow::SHORT, 4, Integer)
172        }
173        Rgba16Sfloat => {
174            FormatDescription::new(glow::RGBA16F, glow::RGBA, glow::HALF_FLOAT, 4, Float)
175        }
176        Rgba16Unorm => {
177            FormatDescription::new(glow::RGBA16, glow::RGBA, glow::UNSIGNED_SHORT, 4, Float)
178        }
179        R32Uint => FormatDescription::new(
180            glow::R32UI,
181            glow::RED_INTEGER,
182            glow::UNSIGNED_INT,
183            1,
184            Integer,
185        ),
186        R32Sint => FormatDescription::new(glow::R32I, glow::RED_INTEGER, glow::INT, 1, Integer),
187        R32Sfloat => FormatDescription::new(glow::R32F, glow::RED, glow::FLOAT, 1, Float),
188        Rg32Uint => FormatDescription::new(
189            glow::RG32UI,
190            glow::RG_INTEGER,
191            glow::UNSIGNED_INT,
192            2,
193            Integer,
194        ),
195        Rg32Sint => FormatDescription::new(glow::R32I, glow::RG_INTEGER, glow::INT, 2, Integer),
196        Rg32Sfloat => FormatDescription::new(glow::RG32F, glow::RG, glow::FLOAT, 2, Float),
197        Rgb32Uint => FormatDescription::new(
198            glow::RGB32UI,
199            glow::RGB_INTEGER,
200            glow::UNSIGNED_INT,
201            3,
202            Integer,
203        ),
204        Rgb32Sint => FormatDescription::new(glow::RGB32I, glow::RGB_INTEGER, glow::INT, 3, Integer),
205        Rgb32Sfloat => FormatDescription::new(glow::RGB32F, glow::RGB, glow::FLOAT, 3, Float),
206        Rgba32Uint => FormatDescription::new(
207            glow::RGBA32UI,
208            glow::RGBA_INTEGER,
209            glow::UNSIGNED_INT,
210            4,
211            Integer,
212        ),
213        Rgba32Sint => {
214            FormatDescription::new(glow::RGBA32I, glow::RGBA_INTEGER, glow::INT, 4, Integer)
215        }
216        Rgba32Sfloat => FormatDescription::new(glow::RGBA32F, glow::RGBA, glow::FLOAT, 4, Float),
217        S8Uint => FormatDescription::new(glow::R8, glow::RED, glow::UNSIGNED_BYTE, 1, Integer),
218        D16Unorm => FormatDescription::new(
219            glow::DEPTH_COMPONENT16,
220            glow::DEPTH_COMPONENT,
221            glow::UNSIGNED_NORMALIZED,
222            1,
223            Float,
224        ),
225        D24UnormS8Uint => FormatDescription::new(
226            glow::DEPTH24_STENCIL8,
227            glow::DEPTH_STENCIL,
228            glow::UNSIGNED_INT,
229            2,
230            Float,
231        ),
232        D32Sfloat => FormatDescription::new(
233            glow::DEPTH_COMPONENT32F,
234            glow::DEPTH_COMPONENT,
235            glow::FLOAT,
236            1,
237            Float,
238        ),
239        D32SfloatS8Uint => FormatDescription::new(
240            glow::DEPTH32F_STENCIL8,
241            glow::DEPTH_STENCIL,
242            glow::UNSIGNED_INT,
243            1,
244            Float,
245        ),
246        X8D24Unorm => FormatDescription::new(
247            glow::DEPTH_COMPONENT24,
248            glow::DEPTH_STENCIL,
249            glow::UNSIGNED_NORMALIZED,
250            2,
251            Float,
252        ),
253
254        _ => return None,
255    })
256}
257
258#[cfg(feature = "cross")]
259pub fn map_naga_stage_to_cross(stage: naga::ShaderStage) -> spirv_cross::spirv::ExecutionModel {
260    use spirv_cross::spirv::ExecutionModel as Em;
261    match stage {
262        naga::ShaderStage::Vertex => Em::Vertex,
263        naga::ShaderStage::Fragment => Em::Fragment,
264        naga::ShaderStage::Compute => Em::GlCompute,
265    }
266}