[egui_glium] clean up the code for the glium OpenGL painter
This commit is contained in:
parent
deb1c33760
commit
6e7507373a
1 changed files with 74 additions and 156 deletions
|
@ -9,7 +9,6 @@ use {
|
||||||
glium::{
|
glium::{
|
||||||
implement_vertex,
|
implement_vertex,
|
||||||
index::PrimitiveType,
|
index::PrimitiveType,
|
||||||
program,
|
|
||||||
texture::{self, srgb_texture2d::SrgbTexture2d},
|
texture::{self, srgb_texture2d::SrgbTexture2d},
|
||||||
uniform,
|
uniform,
|
||||||
uniforms::SamplerWrapFunction,
|
uniforms::SamplerWrapFunction,
|
||||||
|
@ -17,33 +16,11 @@ use {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Painter {
|
const VERTEX_SHADER_SOURCE: &str = r#"
|
||||||
program: glium::Program,
|
|
||||||
egui_texture: SrgbTexture2d,
|
|
||||||
egui_texture_version: Option<u64>,
|
|
||||||
|
|
||||||
user_textures: Vec<UserTexture>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct UserTexture {
|
|
||||||
/// Pending upload (will be emptied later).
|
|
||||||
/// This is the format glium likes.
|
|
||||||
pixels: Vec<Vec<(u8, u8, u8, u8)>>,
|
|
||||||
|
|
||||||
/// Lazily uploaded
|
|
||||||
texture: Option<SrgbTexture2d>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Painter {
|
|
||||||
pub fn new(facade: &dyn glium::backend::Facade) -> Painter {
|
|
||||||
let program = program!(facade,
|
|
||||||
140 => {
|
|
||||||
vertex: "
|
|
||||||
#version 140
|
#version 140
|
||||||
uniform vec2 u_screen_size;
|
uniform vec2 u_screen_size;
|
||||||
in vec2 a_pos;
|
in vec2 a_pos;
|
||||||
in vec4 a_srgba;
|
in vec4 a_srgba; // 0-255 sRGB
|
||||||
in vec2 a_tc;
|
in vec2 a_tc;
|
||||||
out vec4 v_rgba;
|
out vec4 v_rgba;
|
||||||
out vec2 v_tc;
|
out vec2 v_tc;
|
||||||
|
@ -69,9 +46,9 @@ impl Painter {
|
||||||
v_rgba = linear_from_srgba(a_srgba);
|
v_rgba = linear_from_srgba(a_srgba);
|
||||||
v_tc = a_tc;
|
v_tc = a_tc;
|
||||||
}
|
}
|
||||||
",
|
"#;
|
||||||
|
|
||||||
fragment: "
|
const FRAGMENT_SHADER_SOURCE: &str = r#"
|
||||||
#version 140
|
#version 140
|
||||||
uniform sampler2D u_sampler;
|
uniform sampler2D u_sampler;
|
||||||
in vec4 v_rgba;
|
in vec4 v_rgba;
|
||||||
|
@ -79,114 +56,39 @@ impl Painter {
|
||||||
out vec4 f_color;
|
out vec4 f_color;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
// glium expects linear rgba
|
// 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);
|
f_color = v_rgba * texture(u_sampler, v_tc);
|
||||||
}
|
}
|
||||||
"
|
"#;
|
||||||
},
|
|
||||||
|
|
||||||
110 => {
|
pub struct Painter {
|
||||||
vertex: "
|
program: glium::Program,
|
||||||
#version 110
|
egui_texture: Option<SrgbTexture2d>,
|
||||||
uniform vec2 u_screen_size;
|
egui_texture_version: Option<u64>,
|
||||||
attribute vec2 a_pos;
|
|
||||||
attribute vec4 a_srgba;
|
|
||||||
attribute vec2 a_tc;
|
|
||||||
varying vec4 v_rgba;
|
|
||||||
varying vec2 v_tc;
|
|
||||||
|
|
||||||
// 0-1 linear from 0-255 sRGB
|
user_textures: Vec<UserTexture>,
|
||||||
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) {
|
#[derive(Default)]
|
||||||
return vec4(linear_from_srgb(srgba.rgb), srgba.a / 255.0);
|
struct UserTexture {
|
||||||
}
|
/// Pending upload (will be emptied later).
|
||||||
|
/// This is the format glium likes.
|
||||||
|
pixels: Vec<Vec<(u8, u8, u8, u8)>>,
|
||||||
|
|
||||||
void main() {
|
/// Lazily uploaded
|
||||||
gl_Position = vec4(
|
texture: Option<SrgbTexture2d>,
|
||||||
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);
|
|
||||||
v_rgba = linear_from_srgba(a_srgba);
|
|
||||||
v_tc = a_tc;
|
|
||||||
}
|
|
||||||
",
|
|
||||||
|
|
||||||
fragment: "
|
impl Painter {
|
||||||
#version 110
|
pub fn new(facade: &dyn glium::backend::Facade) -> Painter {
|
||||||
uniform sampler2D u_sampler;
|
let program =
|
||||||
varying vec4 v_rgba;
|
glium::Program::from_source(facade, VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE, None)
|
||||||
varying vec2 v_tc;
|
.expect("Failed to compile shader");
|
||||||
|
|
||||||
void main() {
|
|
||||||
// glium expects linear rgba
|
|
||||||
gl_FragColor = v_rgba * texture2D(u_sampler, v_tc);
|
|
||||||
}
|
|
||||||
",
|
|
||||||
},
|
|
||||||
|
|
||||||
100 => {
|
|
||||||
vertex: "
|
|
||||||
#version 100
|
|
||||||
uniform mediump vec2 u_screen_size;
|
|
||||||
attribute mediump vec2 a_pos;
|
|
||||||
attribute mediump vec4 a_srgba;
|
|
||||||
attribute mediump vec2 a_tc;
|
|
||||||
varying mediump vec4 v_rgba;
|
|
||||||
varying mediump 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);
|
|
||||||
v_rgba = linear_from_srgba(a_srgba);
|
|
||||||
v_tc = a_tc;
|
|
||||||
}
|
|
||||||
",
|
|
||||||
|
|
||||||
fragment: "
|
|
||||||
#version 100
|
|
||||||
uniform sampler2D u_sampler;
|
|
||||||
varying mediump vec4 v_rgba;
|
|
||||||
varying mediump vec2 v_tc;
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
// glium expects linear rgba
|
|
||||||
gl_FragColor = v_rgba * texture2D(u_sampler, v_tc);
|
|
||||||
}
|
|
||||||
",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let pixels = vec![vec![255u8, 0u8], vec![0u8, 255u8]];
|
|
||||||
let format = texture::SrgbFormat::U8U8U8U8;
|
|
||||||
let mipmaps = texture::MipmapsOption::NoMipmap;
|
|
||||||
let egui_texture = SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap();
|
|
||||||
|
|
||||||
Painter {
|
Painter {
|
||||||
program,
|
program,
|
||||||
egui_texture,
|
egui_texture: None,
|
||||||
egui_texture_version: None,
|
egui_texture_version: None,
|
||||||
user_textures: Default::default(),
|
user_textures: Default::default(),
|
||||||
}
|
}
|
||||||
|
@ -229,11 +131,12 @@ impl Painter {
|
||||||
|
|
||||||
let format = texture::SrgbFormat::U8U8U8U8;
|
let format = texture::SrgbFormat::U8U8U8U8;
|
||||||
let mipmaps = texture::MipmapsOption::NoMipmap;
|
let mipmaps = texture::MipmapsOption::NoMipmap;
|
||||||
self.egui_texture = SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap();
|
self.egui_texture =
|
||||||
|
Some(SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap());
|
||||||
self.egui_texture_version = Some(texture.version);
|
self.egui_texture_version = Some(texture.version);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn upload_user_textures(&mut self, facade: &dyn glium::backend::Facade) {
|
fn upload_pending_user_textures(&mut self, facade: &dyn glium::backend::Facade) {
|
||||||
for user_texture in &mut self.user_textures {
|
for user_texture in &mut self.user_textures {
|
||||||
if user_texture.texture.is_none() {
|
if user_texture.texture.is_none() {
|
||||||
let pixels = std::mem::take(&mut user_texture.pixels);
|
let pixels = std::mem::take(&mut user_texture.pixels);
|
||||||
|
@ -245,6 +148,7 @@ impl Painter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Main entry-point for painting a frame
|
||||||
pub fn paint_jobs(
|
pub fn paint_jobs(
|
||||||
&mut self,
|
&mut self,
|
||||||
display: &glium::Display,
|
display: &glium::Display,
|
||||||
|
@ -252,7 +156,7 @@ impl Painter {
|
||||||
texture: &egui::Texture,
|
texture: &egui::Texture,
|
||||||
) {
|
) {
|
||||||
self.upload_egui_texture(display, texture);
|
self.upload_egui_texture(display, texture);
|
||||||
self.upload_user_textures(display);
|
self.upload_pending_user_textures(display);
|
||||||
|
|
||||||
let mut target = display.draw();
|
let mut target = display.draw();
|
||||||
target.clear_color(0.0, 0.0, 0.0, 0.0);
|
target.clear_color(0.0, 0.0, 0.0, 0.0);
|
||||||
|
@ -264,7 +168,7 @@ impl Painter {
|
||||||
|
|
||||||
fn get_texture(&self, texture_id: egui::TextureId) -> &SrgbTexture2d {
|
fn get_texture(&self, texture_id: egui::TextureId) -> &SrgbTexture2d {
|
||||||
match texture_id {
|
match texture_id {
|
||||||
egui::TextureId::Egui => &self.egui_texture,
|
egui::TextureId::Egui => self.egui_texture.as_ref().unwrap(),
|
||||||
egui::TextureId::User(id) => {
|
egui::TextureId::User(id) => {
|
||||||
let id = id as usize;
|
let id = id as usize;
|
||||||
assert!(id < self.user_textures.len());
|
assert!(id < self.user_textures.len());
|
||||||
|
@ -303,11 +207,13 @@ impl Painter {
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
// TODO: we should probably reuse the `VertexBuffer` instead of allocating a new one each frame.
|
||||||
glium::VertexBuffer::new(display, &vertices).unwrap()
|
glium::VertexBuffer::new(display, &vertices).unwrap()
|
||||||
};
|
};
|
||||||
|
|
||||||
let indices: Vec<u32> = triangles.indices.iter().map(|idx| *idx as u32).collect();
|
let indices: Vec<u32> = triangles.indices.iter().map(|idx| *idx as u32).collect();
|
||||||
|
|
||||||
|
// TODO: we should probably reuse the `IndexBuffer` instead of allocating a new one each frame.
|
||||||
let index_buffer =
|
let index_buffer =
|
||||||
glium::IndexBuffer::new(display, PrimitiveType::TrianglesList, &indices).unwrap();
|
glium::IndexBuffer::new(display, PrimitiveType::TrianglesList, &indices).unwrap();
|
||||||
|
|
||||||
|
@ -324,24 +230,36 @@ impl Painter {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Egui outputs colors with premultiplied alpha:
|
// Egui outputs colors with premultiplied alpha:
|
||||||
let blend_func = glium::BlendingFunction::Addition {
|
let color_blend_func = glium::BlendingFunction::Addition {
|
||||||
source: glium::LinearBlendingFactor::One,
|
source: glium::LinearBlendingFactor::One,
|
||||||
destination: glium::LinearBlendingFactor::OneMinusSourceAlpha,
|
destination: glium::LinearBlendingFactor::OneMinusSourceAlpha,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Less important, but this is technically the correct alpha blend function
|
||||||
|
// when you want to make use of the framebuffer alpha (for screenshots, compositing, etc).
|
||||||
|
let alpha_blend_func = glium::BlendingFunction::Addition {
|
||||||
|
source: glium::LinearBlendingFactor::OneMinusDestinationAlpha,
|
||||||
|
destination: glium::LinearBlendingFactor::One,
|
||||||
|
};
|
||||||
|
|
||||||
let blend = glium::Blend {
|
let blend = glium::Blend {
|
||||||
color: blend_func,
|
color: color_blend_func,
|
||||||
alpha: blend_func,
|
alpha: alpha_blend_func,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Transform clip rect to physical pixels:
|
||||||
let clip_min_x = pixels_per_point * clip_rect.min.x;
|
let clip_min_x = pixels_per_point * clip_rect.min.x;
|
||||||
let clip_min_y = pixels_per_point * clip_rect.min.y;
|
let clip_min_y = pixels_per_point * clip_rect.min.y;
|
||||||
let clip_max_x = pixels_per_point * clip_rect.max.x;
|
let clip_max_x = pixels_per_point * clip_rect.max.x;
|
||||||
let clip_max_y = pixels_per_point * clip_rect.max.y;
|
let clip_max_y = pixels_per_point * clip_rect.max.y;
|
||||||
|
|
||||||
|
// Make sure clip rect can fit withing an `u32`:
|
||||||
let clip_min_x = clamp(clip_min_x, 0.0..=width_pixels as f32);
|
let clip_min_x = clamp(clip_min_x, 0.0..=width_pixels as f32);
|
||||||
let clip_min_y = clamp(clip_min_y, 0.0..=height_pixels as f32);
|
let clip_min_y = clamp(clip_min_y, 0.0..=height_pixels as f32);
|
||||||
let clip_max_x = clamp(clip_max_x, clip_min_x..=width_pixels as f32);
|
let clip_max_x = clamp(clip_max_x, clip_min_x..=width_pixels as f32);
|
||||||
let clip_max_y = clamp(clip_max_y, clip_min_y..=height_pixels as f32);
|
let clip_max_y = clamp(clip_max_y, clip_min_y..=height_pixels as f32);
|
||||||
|
|
||||||
let clip_min_x = clip_min_x.round() as u32;
|
let clip_min_x = clip_min_x.round() as u32;
|
||||||
let clip_min_y = clip_min_y.round() as u32;
|
let clip_min_y = clip_min_y.round() as u32;
|
||||||
let clip_max_x = clip_max_x.round() as u32;
|
let clip_max_x = clip_max_x.round() as u32;
|
||||||
|
|
Loading…
Reference in a new issue