1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 05:40:03 -04:00

Added shaders on GLSL 1.2 (#187)

* Added shaders on GLSL 1.2

- Used `glium::program` to create shaders
- Moved shaders code to its own sources and include it as str
- Added shaders implementation on GLSL which allows run egui on old hardware
  (Raspberry Pi 1/zero in game again)

* Moved webgl shaders code to sources in `shader` subdir

* Added GLSL ES shaders to glium backend to support OpenGL ES

* Described changes related to GLSL versions support
This commit is contained in:
Kayo Phoenix
2021-02-20 23:48:02 +05:00
committed by GitHub
parent ebc2486d22
commit c9919daa11
16 changed files with 413 additions and 200 deletions

View File

@@ -8,6 +8,7 @@ use {
glium::{
implement_vertex,
index::PrimitiveType,
program,
texture::{self, srgb_texture2d::SrgbTexture2d},
uniform,
uniforms::{MagnifySamplerFilter, SamplerWrapFunction},
@@ -15,53 +16,6 @@ use {
},
};
const VERTEX_SHADER_SOURCE: &str = r#"
#version 140
uniform vec2 u_screen_size;
in vec2 a_pos;
in vec4 a_srgba; // 0-255 sRGB
in vec2 a_tc;
out vec4 v_rgba;
out vec2 v_tc;
// 0-1 linear from 0-255 sRGB
vec3 linear_from_srgb(vec3 srgb) {
bvec3 cutoff = lessThan(srgb, vec3(10.31475));
vec3 lower = srgb / vec3(3294.6);
vec3 higher = pow((srgb + vec3(14.025)) / vec3(269.025), vec3(2.4));
return mix(higher, lower, cutoff);
}
vec4 linear_from_srgba(vec4 srgba) {
return vec4(linear_from_srgb(srgba.rgb), srgba.a / 255.0);
}
void main() {
gl_Position = vec4(
2.0 * a_pos.x / u_screen_size.x - 1.0,
1.0 - 2.0 * a_pos.y / u_screen_size.y,
0.0,
1.0);
// egui encodes vertex colors in gamma spaces, so we must decode the colors here:
v_rgba = linear_from_srgba(a_srgba);
v_tc = a_tc;
}
"#;
const FRAGMENT_SHADER_SOURCE: &str = r#"
#version 140
uniform sampler2D u_sampler;
in vec4 v_rgba;
in vec2 v_tc;
out vec4 f_color;
void main() {
// The texture sampler is sRGB aware, and glium already expects linear rgba output
// so no need for any sRGB conversions here:
f_color = v_rgba * texture(u_sampler, v_tc);
}
"#;
pub struct Painter {
program: glium::Program,
egui_texture: Option<SrgbTexture2d>,
@@ -83,9 +37,26 @@ struct UserTexture {
impl Painter {
pub fn new(facade: &dyn glium::backend::Facade) -> Painter {
let program =
glium::Program::from_source(facade, VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE, None)
.expect("Failed to compile shader");
let program = program! {
facade,
120 => {
vertex: include_str!("shader/vertex_120.glsl"),
fragment: include_str!("shader/fragment_120.glsl"),
},
140 => {
vertex: include_str!("shader/vertex_140.glsl"),
fragment: include_str!("shader/fragment_140.glsl"),
},
100 es => {
vertex: include_str!("shader/vertex_100es.glsl"),
fragment: include_str!("shader/fragment_100es.glsl"),
},
300 es => {
vertex: include_str!("shader/vertex_300es.glsl"),
fragment: include_str!("shader/fragment_300es.glsl"),
},
}
.expect("Failed to compile shader");
Painter {
program,