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-05-19 20:28:57 +00:00
|
|
|
Rect,
|
|
|
|
},
|
2020-04-20 21:33:16 +00:00
|
|
|
glium::{implement_vertex, index::PrimitiveType, program, texture, uniform, Frame, Surface},
|
2019-04-21 08:13:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub struct Painter {
|
|
|
|
program: glium::Program,
|
|
|
|
texture: texture::texture2d::Texture2d,
|
|
|
|
current_texture_id: Option<u64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
uniform vec2 u_tex_size;
|
|
|
|
in vec2 a_pos;
|
|
|
|
in vec4 a_color;
|
|
|
|
in vec2 a_tc;
|
|
|
|
out vec4 v_color;
|
|
|
|
out vec2 v_tc;
|
|
|
|
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_color = a_color / 255.0;
|
|
|
|
v_tc = a_tc / u_tex_size;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
|
|
|
|
fragment: "
|
|
|
|
#version 140
|
|
|
|
uniform sampler2D u_sampler;
|
|
|
|
in vec4 v_color;
|
|
|
|
in vec2 v_tc;
|
|
|
|
out vec4 f_color;
|
2020-04-16 21:10:42 +00:00
|
|
|
|
|
|
|
// glium expects linear output.
|
|
|
|
vec3 linear_from_srgb(vec3 srgb) {
|
|
|
|
bvec3 cutoff = lessThan(srgb, vec3(0.04045));
|
|
|
|
vec3 higher = pow((srgb + vec3(0.055)) / vec3(1.055), vec3(2.4));
|
|
|
|
vec3 lower = srgb / vec3(12.92);
|
|
|
|
return mix(higher, lower, cutoff);
|
|
|
|
}
|
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
void main() {
|
|
|
|
f_color = v_color;
|
2020-04-16 21:10:42 +00:00
|
|
|
f_color.rgb = linear_from_srgb(f_color.rgb);
|
2020-05-11 15:57:11 +00:00
|
|
|
f_color *= texture(u_sampler, v_tc).r;
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
"
|
|
|
|
},
|
|
|
|
|
|
|
|
110 => {
|
|
|
|
vertex: "
|
|
|
|
#version 110
|
|
|
|
uniform vec2 u_screen_size;
|
|
|
|
uniform vec2 u_tex_size;
|
|
|
|
attribute vec2 a_pos;
|
|
|
|
attribute vec4 a_color;
|
|
|
|
attribute vec2 a_tc;
|
|
|
|
varying vec4 v_color;
|
|
|
|
varying vec2 v_tc;
|
|
|
|
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_color = a_color / 255.0;
|
|
|
|
v_tc = a_tc / u_tex_size;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
|
|
|
|
fragment: "
|
|
|
|
#version 110
|
|
|
|
uniform sampler2D u_sampler;
|
|
|
|
varying vec4 v_color;
|
|
|
|
varying vec2 v_tc;
|
2020-04-16 21:10:42 +00:00
|
|
|
|
|
|
|
// glium expects linear output.
|
|
|
|
vec3 linear_from_srgb(vec3 srgb) {
|
|
|
|
bvec3 cutoff = lessThan(srgb, vec3(0.04045));
|
|
|
|
vec3 higher = pow((srgb + vec3(0.055)) / vec3(1.055), vec3(2.4));
|
|
|
|
vec3 lower = srgb / vec3(12.92);
|
|
|
|
return mix(higher, lower, cutoff);
|
|
|
|
}
|
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
void main() {
|
|
|
|
gl_FragColor = v_color;
|
2020-04-16 21:10:42 +00:00
|
|
|
gl_FragColor.rgb = linear_from_srgb(gl_FragColor.rgb);
|
2020-05-11 15:57:11 +00:00
|
|
|
gl_FragColor *= texture2D(u_sampler, v_tc).r;
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
},
|
|
|
|
|
|
|
|
100 => {
|
|
|
|
vertex: "
|
|
|
|
#version 100
|
|
|
|
uniform mediump vec2 u_screen_size;
|
|
|
|
uniform mediump vec2 u_tex_size;
|
|
|
|
attribute mediump vec2 a_pos;
|
|
|
|
attribute mediump vec4 a_color;
|
|
|
|
attribute mediump vec2 a_tc;
|
|
|
|
varying mediump vec4 v_color;
|
|
|
|
varying mediump vec2 v_tc;
|
|
|
|
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_color = a_color / 255.0;
|
|
|
|
v_tc = a_tc / u_tex_size;
|
|
|
|
}
|
|
|
|
",
|
|
|
|
|
|
|
|
fragment: "
|
|
|
|
#version 100
|
|
|
|
uniform sampler2D u_sampler;
|
|
|
|
varying mediump vec4 v_color;
|
|
|
|
varying mediump vec2 v_tc;
|
2020-04-16 21:10:42 +00:00
|
|
|
|
|
|
|
// glium expects linear output.
|
|
|
|
vec3 linear_from_srgb(vec3 srgb) {
|
|
|
|
bvec3 cutoff = lessThan(srgb, vec3(0.04045));
|
|
|
|
vec3 higher = pow((srgb + vec3(0.055)) / vec3(1.055), vec3(2.4));
|
|
|
|
vec3 lower = srgb / vec3(12.92);
|
|
|
|
return mix(higher, lower, cutoff);
|
|
|
|
}
|
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
void main() {
|
|
|
|
gl_FragColor = v_color;
|
2020-04-16 21:10:42 +00:00
|
|
|
gl_FragColor.rgb = linear_from_srgb(gl_FragColor.rgb);
|
2020-05-11 15:57:11 +00:00
|
|
|
gl_FragColor *= texture2D(u_sampler, v_tc).r;
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let pixels = vec![vec![255u8, 0u8], vec![0u8, 255u8]];
|
|
|
|
let format = texture::UncompressedFloatFormat::U8;
|
|
|
|
let mipmaps = texture::MipmapsOption::NoMipmap;
|
|
|
|
let texture =
|
|
|
|
texture::texture2d::Texture2d::with_format(facade, pixels, format, mipmaps).unwrap();
|
|
|
|
|
|
|
|
Painter {
|
|
|
|
program,
|
|
|
|
texture,
|
|
|
|
current_texture_id: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 08:22:35 +00:00
|
|
|
fn upload_texture(&mut self, facade: &dyn glium::backend::Facade, texture: &egui::Texture) {
|
2019-04-21 08:13:05 +00:00
|
|
|
if self.current_texture_id == Some(texture.id) {
|
|
|
|
return; // No change
|
|
|
|
}
|
|
|
|
|
|
|
|
let pixels: Vec<Vec<u8>> = texture
|
|
|
|
.pixels
|
|
|
|
.chunks(texture.width as usize)
|
|
|
|
.map(|row| row.to_vec())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let format = texture::UncompressedFloatFormat::U8;
|
|
|
|
let mipmaps = texture::MipmapsOption::NoMipmap;
|
|
|
|
self.texture =
|
|
|
|
texture::texture2d::Texture2d::with_format(facade, pixels, format, mipmaps).unwrap();
|
|
|
|
self.current_texture_id = Some(texture.id);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
) {
|
2019-04-21 08:13:05 +00:00
|
|
|
self.upload_texture(display, texture);
|
|
|
|
|
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 {
|
|
|
|
self.paint_job(&mut target, display, clip_rect, &triangles, texture)
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
|
|
|
target.finish().unwrap();
|
|
|
|
}
|
|
|
|
|
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-05-30 08:22:35 +00:00
|
|
|
texture: &egui::Texture,
|
2020-04-20 21:33:16 +00:00
|
|
|
) {
|
2019-04-21 08:13:05 +00:00
|
|
|
let vertex_buffer = {
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct Vertex {
|
|
|
|
a_pos: [f32; 2],
|
|
|
|
a_color: [u8; 4],
|
|
|
|
a_tc: [u16; 2],
|
|
|
|
}
|
|
|
|
implement_vertex!(Vertex, a_pos, a_color, a_tc);
|
|
|
|
|
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],
|
|
|
|
a_color: [v.color.r, v.color.g, v.color.b, v.color.a],
|
|
|
|
a_tc: [v.uv.0, v.uv.1],
|
|
|
|
})
|
|
|
|
.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;
|
|
|
|
|
|
|
|
let uniforms = uniform! {
|
|
|
|
u_screen_size: [width_points, height_points],
|
|
|
|
u_tex_size: [texture.width as f32, texture.height as f32],
|
|
|
|
u_sampler: &self.texture,
|
|
|
|
};
|
|
|
|
|
2020-05-11 15:57:11 +00:00
|
|
|
// Emilib outputs colors with premultiplied alpha:
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|