2019-11-24 15:27:12 +00:00
|
|
|
#![allow(deprecated)] // legacy implement_vertex macro
|
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
use {
|
2020-05-30 08:22:35 +00:00
|
|
|
egui::{
|
2020-07-17 08:29:21 +00:00
|
|
|
math::clamp,
|
2020-07-18 22:01:13 +00:00
|
|
|
paint::{PaintJobs, Triangles},
|
2020-09-11 06:56:47 +00:00
|
|
|
Rect, Srgba,
|
2020-05-19 20:28:57 +00:00
|
|
|
},
|
2020-09-09 13:24:09 +00:00
|
|
|
glium::{
|
2020-09-11 06:56:47 +00:00
|
|
|
implement_vertex,
|
|
|
|
index::PrimitiveType,
|
|
|
|
program,
|
|
|
|
texture::{self, srgb_texture2d::SrgbTexture2d},
|
|
|
|
uniform,
|
|
|
|
uniforms::SamplerWrapFunction,
|
|
|
|
Frame, Surface,
|
2020-09-09 13:24:09 +00:00
|
|
|
},
|
2019-04-21 08:13:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub struct Painter {
|
|
|
|
program: glium::Program,
|
2020-09-11 06:56:47 +00:00
|
|
|
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>,
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Painter {
|
2019-11-02 08:50:49 +00:00
|
|
|
pub fn new(facade: &dyn glium::backend::Facade) -> Painter {
|
2019-04-21 08:13:05 +00:00
|
|
|
let program = program!(facade,
|
|
|
|
140 => {
|
|
|
|
vertex: "
|
|
|
|
#version 140
|
|
|
|
uniform vec2 u_screen_size;
|
|
|
|
in vec2 a_pos;
|
2020-08-29 10:04:10 +00:00
|
|
|
in vec4 a_srgba;
|
2019-04-21 08:13:05 +00:00
|
|
|
in vec2 a_tc;
|
2020-08-29 10:04:10 +00:00
|
|
|
out vec4 v_rgba;
|
2019-04-21 08:13:05 +00:00
|
|
|
out vec2 v_tc;
|
2020-08-29 10:04:10 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
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);
|
2020-08-29 10:04:10 +00:00
|
|
|
v_rgba = linear_from_srgba(a_srgba);
|
2020-09-09 15:14:42 +00:00
|
|
|
v_tc = a_tc;
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
|
|
|
|
fragment: "
|
|
|
|
#version 140
|
|
|
|
uniform sampler2D u_sampler;
|
2020-08-29 10:04:10 +00:00
|
|
|
in vec4 v_rgba;
|
2019-04-21 08:13:05 +00:00
|
|
|
in vec2 v_tc;
|
|
|
|
out vec4 f_color;
|
2020-04-16 21:10:42 +00:00
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
void main() {
|
2020-08-29 10:04:10 +00:00
|
|
|
// glium expects linear rgba
|
2020-09-11 06:56:47 +00:00
|
|
|
f_color = v_rgba * texture(u_sampler, v_tc);
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
"
|
|
|
|
},
|
|
|
|
|
|
|
|
110 => {
|
|
|
|
vertex: "
|
|
|
|
#version 110
|
|
|
|
uniform vec2 u_screen_size;
|
|
|
|
attribute vec2 a_pos;
|
2020-08-29 10:04:10 +00:00
|
|
|
attribute vec4 a_srgba;
|
2019-04-21 08:13:05 +00:00
|
|
|
attribute vec2 a_tc;
|
2020-08-29 10:04:10 +00:00
|
|
|
varying vec4 v_rgba;
|
2019-04-21 08:13:05 +00:00
|
|
|
varying vec2 v_tc;
|
2020-08-29 10:04:10 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
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);
|
2020-08-29 10:04:10 +00:00
|
|
|
v_rgba = linear_from_srgba(a_srgba);
|
2020-09-09 15:14:42 +00:00
|
|
|
v_tc = a_tc;
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
|
|
|
|
fragment: "
|
|
|
|
#version 110
|
|
|
|
uniform sampler2D u_sampler;
|
2020-08-29 10:04:10 +00:00
|
|
|
varying vec4 v_rgba;
|
2019-04-21 08:13:05 +00:00
|
|
|
varying vec2 v_tc;
|
2020-04-16 21:10:42 +00:00
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
void main() {
|
2020-08-29 10:04:10 +00:00
|
|
|
// glium expects linear rgba
|
2020-09-11 06:56:47 +00:00
|
|
|
gl_FragColor = v_rgba * texture2D(u_sampler, v_tc);
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
},
|
|
|
|
|
|
|
|
100 => {
|
|
|
|
vertex: "
|
|
|
|
#version 100
|
|
|
|
uniform mediump vec2 u_screen_size;
|
|
|
|
attribute mediump vec2 a_pos;
|
2020-08-29 10:04:10 +00:00
|
|
|
attribute mediump vec4 a_srgba;
|
2019-04-21 08:13:05 +00:00
|
|
|
attribute mediump vec2 a_tc;
|
2020-08-29 10:04:10 +00:00
|
|
|
varying mediump vec4 v_rgba;
|
2019-04-21 08:13:05 +00:00
|
|
|
varying mediump vec2 v_tc;
|
2020-08-29 10:04:10 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
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);
|
2020-08-29 10:04:10 +00:00
|
|
|
v_rgba = linear_from_srgba(a_srgba);
|
2020-09-09 15:14:42 +00:00
|
|
|
v_tc = a_tc;
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
|
|
|
|
fragment: "
|
|
|
|
#version 100
|
|
|
|
uniform sampler2D u_sampler;
|
2020-08-29 10:04:10 +00:00
|
|
|
varying mediump vec4 v_rgba;
|
2019-04-21 08:13:05 +00:00
|
|
|
varying mediump vec2 v_tc;
|
2020-04-16 21:10:42 +00:00
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
void main() {
|
2020-08-29 10:04:10 +00:00
|
|
|
// glium expects linear rgba
|
2020-09-11 06:56:47 +00:00
|
|
|
gl_FragColor = v_rgba * texture2D(u_sampler, v_tc);
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let pixels = vec![vec![255u8, 0u8], vec![0u8, 255u8]];
|
2020-09-11 06:56:47 +00:00
|
|
|
let format = texture::SrgbFormat::U8U8U8U8;
|
2019-04-21 08:13:05 +00:00
|
|
|
let mipmaps = texture::MipmapsOption::NoMipmap;
|
2020-09-11 06:56:47 +00:00
|
|
|
let egui_texture = SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap();
|
2019-04-21 08:13:05 +00:00
|
|
|
|
|
|
|
Painter {
|
|
|
|
program,
|
2020-09-11 06:56:47 +00:00
|
|
|
egui_texture,
|
|
|
|
egui_texture_version: None,
|
|
|
|
user_textures: Default::default(),
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 06:56:47 +00:00
|
|
|
pub fn new_user_texture(&mut self, size: (usize, usize), pixels: &[Srgba]) -> egui::TextureId {
|
|
|
|
assert_eq!(size.0 * size.1, pixels.len());
|
|
|
|
|
|
|
|
let pixels: Vec<Vec<(u8, u8, u8, u8)>> = pixels
|
|
|
|
.chunks(size.0 as usize)
|
|
|
|
.map(|row| row.iter().map(|srgba| srgba.to_tuple()).collect())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let id = egui::TextureId::User(self.user_textures.len() as u64);
|
|
|
|
self.user_textures.push(UserTexture {
|
|
|
|
pixels,
|
|
|
|
texture: None,
|
|
|
|
});
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn upload_egui_texture(
|
|
|
|
&mut self,
|
|
|
|
facade: &dyn glium::backend::Facade,
|
|
|
|
texture: &egui::Texture,
|
|
|
|
) {
|
|
|
|
if self.egui_texture_version == Some(texture.version) {
|
2019-04-21 08:13:05 +00:00
|
|
|
return; // No change
|
|
|
|
}
|
|
|
|
|
2020-09-11 06:56:47 +00:00
|
|
|
let pixels: Vec<Vec<(u8, u8, u8, u8)>> = texture
|
2019-04-21 08:13:05 +00:00
|
|
|
.pixels
|
|
|
|
.chunks(texture.width as usize)
|
2020-09-11 06:56:47 +00:00
|
|
|
.map(|row| {
|
|
|
|
row.iter()
|
|
|
|
.map(|&a| Srgba::white_alpha(a).to_tuple())
|
|
|
|
.collect()
|
|
|
|
})
|
2019-04-21 08:13:05 +00:00
|
|
|
.collect();
|
|
|
|
|
2020-09-11 06:56:47 +00:00
|
|
|
let format = texture::SrgbFormat::U8U8U8U8;
|
2019-04-21 08:13:05 +00:00
|
|
|
let mipmaps = texture::MipmapsOption::NoMipmap;
|
2020-09-11 06:56:47 +00:00
|
|
|
self.egui_texture = SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap();
|
|
|
|
self.egui_texture_version = Some(texture.version);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn upload_user_textures(&mut self, facade: &dyn glium::backend::Facade) {
|
|
|
|
for user_texture in &mut self.user_textures {
|
|
|
|
if user_texture.texture.is_none() {
|
|
|
|
let pixels = std::mem::take(&mut user_texture.pixels);
|
|
|
|
let format = texture::SrgbFormat::U8U8U8U8;
|
|
|
|
let mipmaps = texture::MipmapsOption::NoMipmap;
|
|
|
|
user_texture.texture =
|
|
|
|
Some(SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap());
|
|
|
|
}
|
|
|
|
}
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 22:01:13 +00:00
|
|
|
pub fn paint_jobs(
|
2020-04-20 21:33:16 +00:00
|
|
|
&mut self,
|
|
|
|
display: &glium::Display,
|
2020-07-18 22:01:13 +00:00
|
|
|
jobs: PaintJobs,
|
2020-05-30 08:22:35 +00:00
|
|
|
texture: &egui::Texture,
|
2020-04-20 21:33:16 +00:00
|
|
|
) {
|
2020-09-11 06:56:47 +00:00
|
|
|
self.upload_egui_texture(display, texture);
|
|
|
|
self.upload_user_textures(display);
|
2019-04-21 08:13:05 +00:00
|
|
|
|
2020-04-20 21:33:16 +00:00
|
|
|
let mut target = display.draw();
|
|
|
|
target.clear_color(0.0, 0.0, 0.0, 0.0);
|
2020-07-18 22:01:13 +00:00
|
|
|
for (clip_rect, triangles) in jobs {
|
2020-09-09 15:14:42 +00:00
|
|
|
self.paint_job(&mut target, display, clip_rect, &triangles)
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
|
|
|
target.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
2020-09-11 06:56:47 +00:00
|
|
|
fn get_texture(&self, texture_id: egui::TextureId) -> &SrgbTexture2d {
|
|
|
|
match texture_id {
|
|
|
|
egui::TextureId::Egui => &self.egui_texture,
|
|
|
|
egui::TextureId::User(id) => {
|
|
|
|
let id = id as usize;
|
|
|
|
assert!(id < self.user_textures.len());
|
|
|
|
let texture = self.user_textures[id].texture.as_ref();
|
|
|
|
texture.expect("Should have been uploaded")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 05:20:27 +00:00
|
|
|
#[inline(never)] // Easier profiling
|
2020-07-18 22:01:13 +00:00
|
|
|
fn paint_job(
|
2020-04-20 21:33:16 +00:00
|
|
|
&mut self,
|
|
|
|
target: &mut Frame,
|
|
|
|
display: &glium::Display,
|
2020-05-05 17:12:00 +00:00
|
|
|
clip_rect: Rect,
|
2020-05-19 18:54:02 +00:00
|
|
|
triangles: &Triangles,
|
2020-04-20 21:33:16 +00:00
|
|
|
) {
|
2020-08-21 16:53:43 +00:00
|
|
|
debug_assert!(triangles.is_valid());
|
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
let vertex_buffer = {
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct Vertex {
|
|
|
|
a_pos: [f32; 2],
|
2020-09-09 15:14:42 +00:00
|
|
|
a_tc: [f32; 2],
|
2020-08-29 10:04:10 +00:00
|
|
|
a_srgba: [u8; 4],
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
2020-09-09 15:14:42 +00:00
|
|
|
implement_vertex!(Vertex, a_pos, a_tc, a_srgba);
|
2019-04-21 08:13:05 +00:00
|
|
|
|
2020-05-19 18:54:02 +00:00
|
|
|
let vertices: Vec<Vertex> = triangles
|
2019-04-21 08:13:05 +00:00
|
|
|
.vertices
|
|
|
|
.iter()
|
|
|
|
.map(|v| Vertex {
|
|
|
|
a_pos: [v.pos.x, v.pos.y],
|
2020-09-09 15:14:42 +00:00
|
|
|
a_tc: [v.uv.x, v.uv.y],
|
2020-09-11 06:56:47 +00:00
|
|
|
a_srgba: v.color.to_array(),
|
2019-04-21 08:13:05 +00:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
glium::VertexBuffer::new(display, &vertices).unwrap()
|
|
|
|
};
|
|
|
|
|
2020-05-19 18:54:02 +00:00
|
|
|
let indices: Vec<u32> = triangles.indices.iter().map(|idx| *idx as u32).collect();
|
2019-04-21 08:13:05 +00:00
|
|
|
|
|
|
|
let index_buffer =
|
|
|
|
glium::IndexBuffer::new(display, PrimitiveType::TrianglesList, &indices).unwrap();
|
|
|
|
|
2020-07-21 22:36:17 +00:00
|
|
|
let pixels_per_point = display.gl_window().window().scale_factor() as f32;
|
2019-04-21 08:13:05 +00:00
|
|
|
let (width_pixels, height_pixels) = display.get_framebuffer_dimensions();
|
|
|
|
let width_points = width_pixels as f32 / pixels_per_point;
|
|
|
|
let height_points = height_pixels as f32 / pixels_per_point;
|
|
|
|
|
2020-09-11 06:56:47 +00:00
|
|
|
let texture = self.get_texture(triangles.texture_id);
|
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
let uniforms = uniform! {
|
|
|
|
u_screen_size: [width_points, height_points],
|
2020-09-11 06:56:47 +00:00
|
|
|
u_sampler: texture.sampled().wrap_function(SamplerWrapFunction::Clamp),
|
2019-04-21 08:13:05 +00:00
|
|
|
};
|
|
|
|
|
2020-09-09 12:24:13 +00:00
|
|
|
// Egui outputs colors with premultiplied alpha:
|
2020-05-11 15:57:11 +00:00
|
|
|
let blend_func = glium::BlendingFunction::Addition {
|
|
|
|
source: glium::LinearBlendingFactor::One,
|
|
|
|
destination: glium::LinearBlendingFactor::OneMinusSourceAlpha,
|
|
|
|
};
|
|
|
|
let blend = glium::Blend {
|
|
|
|
color: blend_func,
|
|
|
|
alpha: blend_func,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2020-07-17 08:29:21 +00:00
|
|
|
let clip_min_x = pixels_per_point * clip_rect.min.x;
|
|
|
|
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_y = pixels_per_point * clip_rect.max.y;
|
|
|
|
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_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_min_x = clip_min_x.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_y = clip_max_y.round() as u32;
|
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
let params = glium::DrawParameters {
|
2020-05-11 15:57:11 +00:00
|
|
|
blend,
|
2020-07-17 08:29:21 +00:00
|
|
|
scissor: Some(glium::Rect {
|
|
|
|
left: clip_min_x,
|
|
|
|
bottom: height_pixels - clip_max_y,
|
|
|
|
width: clip_max_x - clip_min_x,
|
|
|
|
height: clip_max_y - clip_min_y,
|
|
|
|
}),
|
2019-04-21 08:13:05 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
target
|
|
|
|
.draw(
|
|
|
|
&vertex_buffer,
|
|
|
|
&index_buffer,
|
|
|
|
&self.program,
|
|
|
|
&uniforms,
|
|
|
|
¶ms,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|