1use core::{cmp::Ordering, fmt};
2
3pub const FIRST_INSTANCE_BINDING: &str = "naga_vs_first_instance";
5
6pub const SUPPORTED_CORE_VERSIONS: &[u16] = &[140, 150, 330, 400, 410, 420, 430, 440, 450, 460];
8pub const SUPPORTED_ES_VERSIONS: &[u16] = &[300, 310, 320];
10
11#[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 Desktop(u16),
18 Embedded { version: u16, is_webgl: bool },
20}
21
22impl Version {
23 pub const fn new_gles(version: u16) -> Self {
25 Self::Embedded {
26 version,
27 is_webgl: false,
28 }
29 }
30
31 pub const fn is_es(&self) -> bool {
33 match *self {
34 Version::Desktop(_) => false,
35 Version::Embedded { .. } => true,
36 }
37 }
38
39 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 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 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 *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 pub fn supports_integer_mix(&self) -> bool {
105 *self >= Version::Desktop(450) || *self >= Version::new_gles(310)
106 }
107
108 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 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
154pub type BindingMap = alloc::collections::BTreeMap<crate::ResourceBinding, u8>;
156
157#[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#[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#[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#[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 Self::Matrix {
232 columns,
233 rows,
234 scalar,
235 } => rows.alignment() * scalar.width as u32 * *columns as u32,
236 }
237 }
238}