Skip to main content

naga_types/
glsl.rs

1use core::{cmp::Ordering, fmt};
2
3// Must match code in glsl_built_in
4pub const FIRST_INSTANCE_BINDING: &str = "naga_vs_first_instance";
5
6/// List of supported `core` GLSL versions.
7pub const SUPPORTED_CORE_VERSIONS: &[u16] = &[140, 150, 330, 400, 410, 420, 430, 440, 450, 460];
8/// List of supported `es` GLSL versions.
9pub const SUPPORTED_ES_VERSIONS: &[u16] = &[300, 310, 320];
10
11/// A GLSL version.
12#[derive(Debug, Copy, Clone, PartialEq)]
13#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
14#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
15pub enum Version {
16    /// `core` GLSL.
17    Desktop(u16),
18    /// `es` GLSL.
19    Embedded { version: u16, is_webgl: bool },
20}
21
22impl Version {
23    /// Create a new gles version
24    pub const fn new_gles(version: u16) -> Self {
25        Self::Embedded {
26            version,
27            is_webgl: false,
28        }
29    }
30
31    /// Returns true if self is `Version::Embedded` (i.e. is a es version)
32    pub const fn is_es(&self) -> bool {
33        match *self {
34            Version::Desktop(_) => false,
35            Version::Embedded { .. } => true,
36        }
37    }
38
39    /// Returns true if targeting WebGL
40    pub const fn is_webgl(&self) -> bool {
41        match *self {
42            Version::Desktop(_) => false,
43            Version::Embedded { is_webgl, .. } => is_webgl,
44        }
45    }
46
47    /// Checks the list of currently supported versions and returns true if it contains the
48    /// specified version
49    ///
50    /// # Notes
51    /// As an invalid version number will never be added to the supported version list
52    /// so this also checks for version validity
53    pub fn is_supported(&self) -> bool {
54        match *self {
55            Version::Desktop(v) => SUPPORTED_CORE_VERSIONS.contains(&v),
56            Version::Embedded { version: v, .. } => SUPPORTED_ES_VERSIONS.contains(&v),
57        }
58    }
59
60    pub fn supports_io_locations(&self) -> bool {
61        *self >= Version::Desktop(330) || *self >= Version::new_gles(300)
62    }
63
64    /// Checks if the version supports all of the explicit layouts:
65    /// - `location=` qualifiers for bindings
66    /// - `binding=` qualifiers for resources
67    ///
68    /// Note: `location=` for vertex inputs and fragment outputs is supported
69    /// unconditionally for GLES 300.
70    pub fn supports_explicit_locations(&self) -> bool {
71        *self >= Version::Desktop(420) || *self >= Version::new_gles(310)
72    }
73
74    pub fn supports_early_depth_test(&self) -> bool {
75        *self >= Version::Desktop(130) || *self >= Version::new_gles(310)
76    }
77
78    pub fn supports_std140_layout(&self) -> bool {
79        *self >= Version::Desktop(140) || *self >= Version::new_gles(300)
80    }
81
82    pub fn supports_std430_layout(&self) -> bool {
83        // std430 is available from 400 via GL_ARB_shader_storage_buffer_object.
84        *self >= Version::Desktop(400) || *self >= Version::new_gles(310)
85    }
86
87    pub fn supports_fma_function(&self) -> bool {
88        *self >= Version::Desktop(400) || *self >= Version::new_gles(320)
89    }
90
91    pub fn supports_integer_functions(&self) -> bool {
92        *self >= Version::Desktop(400) || *self >= Version::new_gles(310)
93    }
94
95    pub fn supports_frexp_function(&self) -> bool {
96        *self >= Version::Desktop(400) || *self >= Version::new_gles(310)
97    }
98
99    pub fn supports_derivative_control(&self) -> bool {
100        *self >= Version::Desktop(450)
101    }
102
103    /// Returns true if `mix` with a boolean selector accepts non-float vectors.
104    pub fn supports_integer_mix(&self) -> bool {
105        *self >= Version::Desktop(450) || *self >= Version::new_gles(310)
106    }
107
108    // For supports_pack_unpack_4x8, supports_pack_unpack_snorm_2x16, supports_pack_unpack_unorm_2x16
109    // see:
110    // https://registry.khronos.org/OpenGL-Refpages/gl4/html/unpackUnorm.xhtml
111    // https://registry.khronos.org/OpenGL-Refpages/es3/html/unpackUnorm.xhtml
112    // https://registry.khronos.org/OpenGL-Refpages/gl4/html/packUnorm.xhtml
113    // https://registry.khronos.org/OpenGL-Refpages/es3/html/packUnorm.xhtml
114    pub fn supports_pack_unpack_4x8(&self) -> bool {
115        *self >= Version::Desktop(400) || *self >= Version::new_gles(310)
116    }
117    pub fn supports_pack_unpack_snorm_2x16(&self) -> bool {
118        *self >= Version::Desktop(420) || *self >= Version::new_gles(300)
119    }
120    pub fn supports_pack_unpack_unorm_2x16(&self) -> bool {
121        *self >= Version::Desktop(400) || *self >= Version::new_gles(300)
122    }
123
124    // https://registry.khronos.org/OpenGL-Refpages/gl4/html/unpackHalf2x16.xhtml
125    // https://registry.khronos.org/OpenGL-Refpages/gl4/html/packHalf2x16.xhtml
126    // https://registry.khronos.org/OpenGL-Refpages/es3/html/unpackHalf2x16.xhtml
127    // https://registry.khronos.org/OpenGL-Refpages/es3/html/packHalf2x16.xhtml
128    pub fn supports_pack_unpack_half_2x16(&self) -> bool {
129        *self >= Version::Desktop(420) || *self >= Version::new_gles(300)
130    }
131}
132
133impl PartialOrd for Version {
134    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
135        match (*self, *other) {
136            (Version::Desktop(x), Version::Desktop(y)) => Some(x.cmp(&y)),
137            (Version::Embedded { version: x, .. }, Version::Embedded { version: y, .. }) => {
138                Some(x.cmp(&y))
139            }
140            _ => None,
141        }
142    }
143}
144
145impl fmt::Display for Version {
146    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147        match *self {
148            Version::Desktop(v) => write!(f, "{v} core"),
149            Version::Embedded { version: v, .. } => write!(f, "{v} es"),
150        }
151    }
152}
153
154/// Mapping between resources and bindings.
155pub type BindingMap = alloc::collections::BTreeMap<crate::ResourceBinding, u8>;
156
157/// Separate type from `naga::ScalarKind` so that naga can easily add impl's
158#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
159#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
160#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
161pub enum GlslScalarKind {
162    Sint,
163    Uint,
164    Float,
165}
166
167/// Separate type from `naga::VectorSize` so that naga can easily add impl's
168#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
169#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
170#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
171pub enum GlslVectorSize {
172    Bi = 2,
173    Tri = 3,
174    Quad = 4,
175}
176impl GlslVectorSize {
177    pub fn alignment(&self) -> u32 {
178        match self {
179            Self::Bi => 2,
180            Self::Tri | Self::Quad => 4,
181        }
182    }
183}
184
185/// Separate type from `naga::Scalar` so that naga can easily add impl's
186#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
187#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
188#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
189pub struct GlslScalar {
190    pub kind: GlslScalarKind,
191    pub width: u8,
192}
193
194impl GlslScalar {
195    pub const F32: Self = Self {
196        kind: GlslScalarKind::Float,
197        width: 4,
198    };
199    pub const I32: Self = Self {
200        kind: GlslScalarKind::Sint,
201        width: 4,
202    };
203    pub const U32: Self = Self {
204        kind: GlslScalarKind::Uint,
205        width: 4,
206    };
207}
208
209/// A subset of `naga::TypeInner` so that uniforms can be analyzed without pulling in the entire naga IR.
210#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
211#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
212#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
213pub enum GlslUniformType {
214    Scalar(GlslScalar),
215    Vector {
216        size: GlslVectorSize,
217        scalar: GlslScalar,
218    },
219    Matrix {
220        columns: GlslVectorSize,
221        rows: GlslVectorSize,
222        scalar: GlslScalar,
223    },
224}
225impl GlslUniformType {
226    pub fn size(&self) -> u32 {
227        match self {
228            Self::Scalar(scalar) => scalar.width as u32,
229            Self::Vector { size, scalar } => *size as u32 * scalar.width as u32,
230            // matrices are treated as arrays of aligned columns
231            Self::Matrix {
232                columns,
233                rows,
234                scalar,
235            } => rows.alignment() * scalar.width as u32 * *columns as u32,
236        }
237    }
238}