egui_glow/
shader_version.rs1#![expect(clippy::undocumented_unsafe_blocks)]
2#![expect(clippy::unwrap_used)] #![expect(unsafe_code)]
4
5use std::convert::TryInto as _;
6
7#[derive(Copy, Clone, Debug, PartialEq, Eq)]
9pub enum ShaderVersion {
10 Gl120,
11
12 Gl140,
14
15 Es100,
17
18 Es300,
20}
21
22impl ShaderVersion {
23 pub fn get(gl: &glow::Context) -> Self {
24 use glow::HasContext as _;
25 let shading_lang_string =
26 unsafe { gl.get_parameter_string(glow::SHADING_LANGUAGE_VERSION) };
27 let shader_version = Self::parse(&shading_lang_string);
28 log::debug!("Shader version: {shader_version:?} ({shading_lang_string:?}).");
29 shader_version
30 }
31
32 #[inline]
33 pub(crate) fn parse(glsl_ver: &str) -> Self {
34 let start = glsl_ver.find(|c| char::is_ascii_digit(&c)).unwrap();
35 let es = glsl_ver[..start].contains(" ES ");
36 let ver = glsl_ver[start..]
37 .split_once(' ')
38 .map_or(&glsl_ver[start..], |x| x.0);
39 let [maj, min]: [u8; 2] = ver
40 .splitn(3, '.')
41 .take(2)
42 .map(|x| x.parse().unwrap_or_default())
43 .collect::<Vec<u8>>()
44 .try_into()
45 .unwrap();
46 if es {
47 if maj >= 3 { Self::Es300 } else { Self::Es100 }
48 } else if maj > 1 || (maj == 1 && min >= 40) {
49 Self::Gl140
50 } else {
51 Self::Gl120
52 }
53 }
54
55 pub fn version_declaration(&self) -> &'static str {
57 match self {
58 Self::Gl120 => "#version 120\n",
59 Self::Gl140 => "#version 140\n",
60 Self::Es100 => "#version 100\n",
61 Self::Es300 => "#version 300 es\n",
62 }
63 }
64
65 pub fn is_new_shader_interface(&self) -> bool {
67 match self {
68 Self::Gl120 | Self::Es100 => false,
69 Self::Es300 | Self::Gl140 => true,
70 }
71 }
72
73 pub fn is_embedded(&self) -> bool {
74 match self {
75 Self::Gl120 | Self::Gl140 => false,
76 Self::Es100 | Self::Es300 => true,
77 }
78 }
79}
80
81#[test]
82fn test_shader_version() {
83 use ShaderVersion::{Es100, Es300, Gl120, Gl140};
84 for (s, v) in [
85 ("1.2 OpenGL foo bar", Gl120),
86 ("3.0", Gl140),
87 ("0.0", Gl120),
88 ("OpenGL ES GLSL 3.00 (WebGL2)", Es300),
89 ("OpenGL ES GLSL 1.00 (WebGL)", Es100),
90 ("OpenGL ES GLSL ES 1.00 foo bar", Es100),
91 ("WebGL GLSL ES 3.00 foo bar", Es300),
92 ("WebGL GLSL ES 3.00", Es300),
93 ("WebGL GLSL ES 1.0 foo bar", Es100),
94 ] {
95 assert_eq!(ShaderVersion::parse(s), v);
96 }
97}