2019-11-24 15:27:12 +00:00
|
|
|
#![allow(deprecated)] // legacy implement_vertex macro
|
2021-09-28 15:33:28 +00:00
|
|
|
#![allow(semicolon_in_expressions_from_macros)] // glium::program! macro
|
2019-11-24 15:27:12 +00:00
|
|
|
|
2019-04-21 08:13:05 +00:00
|
|
|
use {
|
2021-02-14 09:53:39 +00:00
|
|
|
egui::{
|
2021-03-21 16:47:03 +00:00
|
|
|
emath::Rect,
|
2021-02-14 09:53:39 +00:00
|
|
|
epaint::{Color32, Mesh},
|
|
|
|
},
|
2020-09-09 13:24:09 +00:00
|
|
|
glium::{
|
2020-09-11 06:56:47 +00:00
|
|
|
implement_vertex,
|
|
|
|
index::PrimitiveType,
|
2021-02-20 18:48:02 +00:00
|
|
|
program,
|
2020-09-11 06:56:47 +00:00
|
|
|
texture::{self, srgb_texture2d::SrgbTexture2d},
|
|
|
|
uniform,
|
2021-01-09 09:27:32 +00:00
|
|
|
uniforms::{MagnifySamplerFilter, SamplerWrapFunction},
|
2020-09-09 13:24:09 +00:00
|
|
|
},
|
2021-12-26 20:21:28 +00:00
|
|
|
std::{collections::HashMap, rc::Rc},
|
2019-04-21 08:13:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub struct Painter {
|
|
|
|
program: glium::Program,
|
2020-09-21 17:19:24 +00:00
|
|
|
egui_texture: Option<SrgbTexture2d>,
|
2020-09-11 06:56:47 +00:00
|
|
|
egui_texture_version: Option<u64>,
|
|
|
|
|
2021-12-26 20:21:28 +00:00
|
|
|
/// Index is the same as in [`egui::TextureId::User`].
|
|
|
|
user_textures: HashMap<u64, Rc<SrgbTexture2d>>,
|
2020-09-11 06:56:47 +00:00
|
|
|
|
2021-12-28 12:22:01 +00:00
|
|
|
#[cfg(feature = "epi")]
|
|
|
|
next_native_tex_id: u64, // TODO: 128-bit texture space?
|
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 {
|
2021-02-20 18:48:02 +00:00
|
|
|
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");
|
2019-04-21 08:13:05 +00:00
|
|
|
|
|
|
|
Painter {
|
|
|
|
program,
|
2020-09-21 17:19:24 +00:00
|
|
|
egui_texture: None,
|
2020-09-11 06:56:47 +00:00
|
|
|
egui_texture_version: None,
|
|
|
|
user_textures: Default::default(),
|
2021-12-28 12:22:01 +00:00
|
|
|
#[cfg(feature = "epi")]
|
2021-12-26 20:21:28 +00:00
|
|
|
next_native_tex_id: 1 << 32,
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-11 08:35:47 +00:00
|
|
|
pub fn upload_egui_texture(
|
2020-09-11 06:56:47 +00:00
|
|
|
&mut self,
|
|
|
|
facade: &dyn glium::backend::Facade,
|
2021-12-28 20:02:23 +00:00
|
|
|
font_image: &egui::FontImage,
|
2020-09-11 06:56:47 +00:00
|
|
|
) {
|
2021-12-28 20:02:23 +00:00
|
|
|
if self.egui_texture_version == Some(font_image.version) {
|
2019-04-21 08:13:05 +00:00
|
|
|
return; // No change
|
|
|
|
}
|
|
|
|
|
2021-12-28 20:02:23 +00:00
|
|
|
let pixels: Vec<Vec<(u8, u8, u8, u8)>> = font_image
|
2019-04-21 08:13:05 +00:00
|
|
|
.pixels
|
2021-12-28 20:02:23 +00:00
|
|
|
.chunks(font_image.width as usize)
|
2020-09-11 06:56:47 +00:00
|
|
|
.map(|row| {
|
|
|
|
row.iter()
|
2021-01-03 17:03:11 +00:00
|
|
|
.map(|&a| Color32::from_white_alpha(a).to_tuple())
|
2020-09-11 06:56:47 +00:00
|
|
|
.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-21 17:19:24 +00:00
|
|
|
self.egui_texture =
|
|
|
|
Some(SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap());
|
2021-12-28 20:02:23 +00:00
|
|
|
self.egui_texture_version = Some(font_image.version);
|
2020-09-11 06:56:47 +00:00
|
|
|
}
|
|
|
|
|
2021-04-25 15:02:27 +00:00
|
|
|
/// Main entry-point for painting a frame.
|
|
|
|
/// You should call `target.clear_color(..)` before
|
|
|
|
/// and `target.finish()` after this.
|
2021-09-02 12:44:50 +00:00
|
|
|
pub fn paint_meshes<T: glium::Surface>(
|
2020-04-20 21:33:16 +00:00
|
|
|
&mut self,
|
|
|
|
display: &glium::Display,
|
2021-09-02 12:44:50 +00:00
|
|
|
target: &mut T,
|
2020-10-17 21:54:46 +00:00
|
|
|
pixels_per_point: f32,
|
2021-01-25 20:43:17 +00:00
|
|
|
cipped_meshes: Vec<egui::ClippedMesh>,
|
2021-12-28 20:02:23 +00:00
|
|
|
font_image: &egui::FontImage,
|
2020-04-20 21:33:16 +00:00
|
|
|
) {
|
2021-12-28 20:02:23 +00:00
|
|
|
self.upload_egui_texture(display, font_image);
|
2019-04-21 08:13:05 +00:00
|
|
|
|
2021-01-25 20:43:17 +00:00
|
|
|
for egui::ClippedMesh(clip_rect, mesh) in cipped_meshes {
|
2021-10-20 14:43:40 +00:00
|
|
|
self.paint_mesh(target, display, pixels_per_point, clip_rect, &mesh);
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 05:20:27 +00:00
|
|
|
#[inline(never)] // Easier profiling
|
2021-12-26 20:21:28 +00:00
|
|
|
fn paint_mesh<T: glium::Surface>(
|
2020-04-20 21:33:16 +00:00
|
|
|
&mut self,
|
2021-09-02 12:44:50 +00:00
|
|
|
target: &mut T,
|
2020-04-20 21:33:16 +00:00
|
|
|
display: &glium::Display,
|
2020-10-17 21:54:46 +00:00
|
|
|
pixels_per_point: f32,
|
2020-05-05 17:12:00 +00:00
|
|
|
clip_rect: Rect,
|
2021-01-25 20:23:24 +00:00
|
|
|
mesh: &Mesh,
|
2020-04-20 21:33:16 +00:00
|
|
|
) {
|
2021-01-25 20:23:24 +00:00
|
|
|
debug_assert!(mesh.is_valid());
|
2020-08-21 16:53:43 +00:00
|
|
|
|
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
|
|
|
|
2021-01-25 20:23:24 +00:00
|
|
|
let vertices: Vec<Vertex> = mesh
|
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();
|
|
|
|
|
2020-09-21 17:19:24 +00:00
|
|
|
// TODO: we should probably reuse the `VertexBuffer` instead of allocating a new one each frame.
|
2019-04-21 08:13:05 +00:00
|
|
|
glium::VertexBuffer::new(display, &vertices).unwrap()
|
|
|
|
};
|
|
|
|
|
2020-09-21 17:19:24 +00:00
|
|
|
// TODO: we should probably reuse the `IndexBuffer` instead of allocating a new one each frame.
|
2019-04-21 08:13:05 +00:00
|
|
|
let index_buffer =
|
2021-04-05 08:21:17 +00:00
|
|
|
glium::IndexBuffer::new(display, PrimitiveType::TrianglesList, &mesh.indices).unwrap();
|
2019-04-21 08:13:05 +00:00
|
|
|
|
2020-10-17 21:54:46 +00:00
|
|
|
let (width_in_pixels, height_in_pixels) = display.get_framebuffer_dimensions();
|
|
|
|
let width_in_points = width_in_pixels as f32 / pixels_per_point;
|
|
|
|
let height_in_points = height_in_pixels as f32 / pixels_per_point;
|
2019-04-21 08:13:05 +00:00
|
|
|
|
2021-01-25 20:23:24 +00:00
|
|
|
if let Some(texture) = self.get_texture(mesh.texture_id) {
|
2021-01-17 13:48:59 +00:00
|
|
|
// The texture coordinates for text are so that both nearest and linear should work with the egui font texture.
|
2021-01-09 09:27:32 +00:00
|
|
|
// For user textures linear sampling is more likely to be the right choice.
|
|
|
|
let filter = MagnifySamplerFilter::Linear;
|
|
|
|
|
2020-11-18 20:38:29 +00:00
|
|
|
let uniforms = uniform! {
|
|
|
|
u_screen_size: [width_in_points, height_in_points],
|
2021-01-09 09:27:32 +00:00
|
|
|
u_sampler: texture.sampled().magnify_filter(filter).wrap_function(SamplerWrapFunction::Clamp),
|
2020-11-18 20:38:29 +00:00
|
|
|
};
|
|
|
|
|
2021-01-17 13:48:59 +00:00
|
|
|
// egui outputs colors with premultiplied alpha:
|
2020-11-18 20:38:29 +00:00
|
|
|
let color_blend_func = glium::BlendingFunction::Addition {
|
|
|
|
source: glium::LinearBlendingFactor::One,
|
|
|
|
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 {
|
|
|
|
color: color_blend_func,
|
|
|
|
alpha: alpha_blend_func,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2021-01-25 20:23:24 +00:00
|
|
|
// egui outputs mesh in both winding orders:
|
2021-01-17 01:06:26 +00:00
|
|
|
let backface_culling = glium::BackfaceCullingMode::CullingDisabled;
|
|
|
|
|
2020-11-18 20:38:29 +00:00
|
|
|
// Transform clip rect to physical pixels:
|
|
|
|
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;
|
|
|
|
|
2021-02-21 09:12:08 +00:00
|
|
|
// Make sure clip rect can fit within a `u32`:
|
2021-03-21 16:47:03 +00:00
|
|
|
let clip_min_x = clip_min_x.clamp(0.0, width_in_pixels as f32);
|
|
|
|
let clip_min_y = clip_min_y.clamp(0.0, height_in_pixels as f32);
|
|
|
|
let clip_max_x = clip_max_x.clamp(clip_min_x, width_in_pixels as f32);
|
|
|
|
let clip_max_y = clip_max_y.clamp(clip_min_y, height_in_pixels as f32);
|
2020-11-18 20:38:29 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
let params = glium::DrawParameters {
|
|
|
|
blend,
|
2021-01-17 01:06:26 +00:00
|
|
|
backface_culling,
|
2020-11-18 20:38:29 +00:00
|
|
|
scissor: Some(glium::Rect {
|
|
|
|
left: clip_min_x,
|
|
|
|
bottom: height_in_pixels - clip_max_y,
|
|
|
|
width: clip_max_x - clip_min_x,
|
|
|
|
height: clip_max_y - clip_min_y,
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
target
|
|
|
|
.draw(
|
|
|
|
&vertex_buffer,
|
|
|
|
&index_buffer,
|
|
|
|
&self.program,
|
|
|
|
&uniforms,
|
|
|
|
¶ms,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
2020-11-20 18:50:47 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
2021-07-29 20:20:22 +00:00
|
|
|
|
2021-12-28 12:22:01 +00:00
|
|
|
#[cfg(feature = "epi")]
|
2021-12-26 20:21:28 +00:00
|
|
|
pub fn set_texture(
|
2020-11-20 18:50:47 +00:00
|
|
|
&mut self,
|
2021-12-26 20:21:28 +00:00
|
|
|
facade: &dyn glium::backend::Facade,
|
|
|
|
tex_id: u64,
|
|
|
|
image: &epi::Image,
|
2020-11-20 18:50:47 +00:00
|
|
|
) {
|
2021-07-29 20:20:22 +00:00
|
|
|
assert_eq!(
|
2021-12-26 20:21:28 +00:00
|
|
|
image.size[0] * image.size[1],
|
|
|
|
image.pixels.len(),
|
2021-07-29 20:20:22 +00:00
|
|
|
"Mismatch between texture size and texel count"
|
|
|
|
);
|
2020-11-20 18:50:47 +00:00
|
|
|
|
2021-12-26 20:21:28 +00:00
|
|
|
let pixels: Vec<Vec<(u8, u8, u8, u8)>> = image
|
|
|
|
.pixels
|
|
|
|
.chunks(image.size[0] as usize)
|
|
|
|
.map(|row| row.iter().map(|srgba| srgba.to_tuple()).collect())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let format = texture::SrgbFormat::U8U8U8U8;
|
|
|
|
let mipmaps = texture::MipmapsOption::NoMipmap;
|
|
|
|
let gl_texture = SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap();
|
|
|
|
|
|
|
|
self.user_textures.insert(tex_id, gl_texture.into());
|
2020-11-20 18:50:47 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 20:21:28 +00:00
|
|
|
pub fn free_texture(&mut self, tex_id: u64) {
|
|
|
|
self.user_textures.remove(&tex_id);
|
2020-11-20 18:50:47 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 20:21:28 +00:00
|
|
|
fn get_texture(&self, texture_id: egui::TextureId) -> Option<&SrgbTexture2d> {
|
2020-11-20 18:50:47 +00:00
|
|
|
match texture_id {
|
|
|
|
egui::TextureId::Egui => self.egui_texture.as_ref(),
|
2021-12-26 20:21:28 +00:00
|
|
|
egui::TextureId::User(id) => self.user_textures.get(&id).map(|rc| rc.as_ref()),
|
2020-11-20 18:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-21 08:13:05 +00:00
|
|
|
}
|
2021-09-05 09:00:45 +00:00
|
|
|
|
2021-10-19 19:40:55 +00:00
|
|
|
#[cfg(feature = "epi")]
|
2021-09-05 09:00:45 +00:00
|
|
|
impl epi::NativeTexture for Painter {
|
2021-09-20 20:52:29 +00:00
|
|
|
type Texture = Rc<SrgbTexture2d>;
|
2021-09-05 09:00:45 +00:00
|
|
|
|
|
|
|
fn register_native_texture(&mut self, native: Self::Texture) -> egui::TextureId {
|
2021-12-26 20:21:28 +00:00
|
|
|
let id = self.next_native_tex_id;
|
|
|
|
self.next_native_tex_id += 1;
|
|
|
|
self.user_textures.insert(id, native);
|
|
|
|
egui::TextureId::User(id as u64)
|
2021-09-05 09:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn replace_native_texture(&mut self, id: egui::TextureId, replacing: Self::Texture) {
|
|
|
|
if let egui::TextureId::User(id) = id {
|
2021-12-26 20:21:28 +00:00
|
|
|
self.user_textures.insert(id, replacing);
|
2021-09-05 09:00:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|