2021-10-18 21:13:32 +00:00
|
|
|
#![allow(unsafe_code)]
|
|
|
|
|
|
|
|
use egui::{
|
|
|
|
emath::Rect,
|
|
|
|
epaint::{Color32, Mesh, Vertex},
|
|
|
|
};
|
2021-11-03 18:17:07 +00:00
|
|
|
pub use glow::Context;
|
|
|
|
|
2021-10-18 21:13:32 +00:00
|
|
|
use memoffset::offset_of;
|
|
|
|
|
2021-11-03 18:17:07 +00:00
|
|
|
use glow::HasContext;
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2021-11-03 18:17:07 +00:00
|
|
|
use crate::misc_util::{
|
|
|
|
as_u8_slice, compile_shader, glow_debug_print, link_program, srgbtexture2d,
|
|
|
|
};
|
|
|
|
use crate::post_process::PostProcess;
|
|
|
|
use crate::shader_version::ShaderVersion;
|
|
|
|
use crate::vao_emulate;
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
const VERT_SRC: &str = include_str!("shader/vertex.glsl");
|
|
|
|
const FRAG_SRC: &str = include_str!("shader/fragment.glsl");
|
|
|
|
|
|
|
|
/// OpenGL painter
|
|
|
|
///
|
|
|
|
/// This struct must be destroyed with [`Painter::destroy`] before dropping, to ensure OpenGL
|
|
|
|
/// objects have been properly deleted and are not leaked.
|
|
|
|
pub struct Painter {
|
2021-11-03 18:17:07 +00:00
|
|
|
program: glow::Program,
|
2021-10-18 21:13:32 +00:00
|
|
|
u_screen_size: glow::UniformLocation,
|
|
|
|
u_sampler: glow::UniformLocation,
|
2021-11-03 18:17:07 +00:00
|
|
|
egui_texture: Option<glow::Texture>,
|
2021-10-18 21:13:32 +00:00
|
|
|
egui_texture_version: Option<u64>,
|
2021-11-03 18:17:07 +00:00
|
|
|
is_webgl_1: bool,
|
|
|
|
is_embedded: bool,
|
|
|
|
vertex_array: crate::misc_util::VAO,
|
|
|
|
srgb_support: bool,
|
2021-10-18 21:13:32 +00:00
|
|
|
/// `None` means unallocated (freed) slot.
|
2021-11-03 18:17:07 +00:00
|
|
|
pub(crate) user_textures: Vec<Option<UserTexture>>,
|
|
|
|
post_process: Option<PostProcess>,
|
|
|
|
vertex_buffer: glow::Buffer,
|
|
|
|
element_array_buffer: glow::Buffer,
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
// Stores outdated OpenGL textures that are yet to be deleted
|
2021-11-03 18:17:07 +00:00
|
|
|
old_textures: Vec<glow::Texture>,
|
2021-10-20 10:29:35 +00:00
|
|
|
// Only used in debug builds, to make sure we are destroyed correctly.
|
2021-10-18 21:13:32 +00:00
|
|
|
destroyed: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2021-11-03 18:17:07 +00:00
|
|
|
pub(crate) struct UserTexture {
|
2021-10-18 21:13:32 +00:00
|
|
|
/// Pending upload (will be emptied later).
|
|
|
|
/// This is the format glow likes.
|
2021-11-03 18:17:07 +00:00
|
|
|
pub(crate) data: Vec<u8>,
|
|
|
|
pub(crate) size: (usize, usize),
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
/// Lazily uploaded
|
2021-11-03 18:17:07 +00:00
|
|
|
pub(crate) gl_texture: Option<glow::Texture>,
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Painter {
|
2021-11-03 18:17:07 +00:00
|
|
|
/// Create painter.
|
|
|
|
///
|
|
|
|
/// Set `pp_fb_extent` to the framebuffer size to enable `sRGB` support on OpenGL ES and WebGL.
|
2021-11-13 11:32:01 +00:00
|
|
|
/// Set `shader_prefix` if you want to turn on shader workaround e.g. `"#define EPIPHANY_WORKAROUND\n"`.
|
|
|
|
///
|
|
|
|
/// this fix [Everything is super dark in epiphany](https://github.com/emilk/egui/issues/794)
|
2021-11-03 18:17:07 +00:00
|
|
|
/// # Errors
|
|
|
|
/// will return `Err` below cases
|
|
|
|
/// * failed to compile shader
|
|
|
|
/// * failed to create postprocess on webgl with `sRGB` support
|
|
|
|
/// * failed to create buffer
|
2021-11-13 11:32:01 +00:00
|
|
|
pub fn new(
|
|
|
|
gl: &glow::Context,
|
|
|
|
pp_fb_extent: Option<[i32; 2]>,
|
|
|
|
shader_prefix: &str,
|
|
|
|
) -> Result<Painter, String> {
|
|
|
|
let support_vao = unsafe { crate::misc_util::support_vao(gl) };
|
2021-11-03 18:17:07 +00:00
|
|
|
let shader_version = ShaderVersion::get(gl);
|
|
|
|
let is_webgl_1 = shader_version == ShaderVersion::Es100;
|
|
|
|
let header = shader_version.version();
|
|
|
|
glow_debug_print(header);
|
|
|
|
let srgb_support = gl.supported_extensions().contains("EXT_sRGB");
|
2021-11-13 11:32:01 +00:00
|
|
|
|
2021-11-03 18:17:07 +00:00
|
|
|
let (post_process, srgb_support_define) = match (shader_version, srgb_support) {
|
|
|
|
//WebGL2 support sRGB default
|
|
|
|
(ShaderVersion::Es300, _) | (ShaderVersion::Es100, true) => unsafe {
|
|
|
|
//Add sRGB support marker for fragment shader
|
|
|
|
if let Some([width, height]) = pp_fb_extent {
|
|
|
|
glow_debug_print("WebGL with sRGB enabled so turn on post process");
|
|
|
|
//install post process to correct sRGB color
|
|
|
|
(
|
|
|
|
Some(PostProcess::new(
|
|
|
|
gl,
|
2021-11-13 11:32:01 +00:00
|
|
|
shader_prefix,
|
|
|
|
support_vao,
|
2021-11-03 18:17:07 +00:00
|
|
|
is_webgl_1,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
)?),
|
|
|
|
"#define SRGB_SUPPORTED",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
glow_debug_print("WebGL or OpenGL ES detected but PostProcess disabled because dimension is None");
|
|
|
|
(None, "")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
//WebGL1 without sRGB support disable postprocess and use fallback shader
|
|
|
|
(ShaderVersion::Es100, false) => (None, ""),
|
|
|
|
//OpenGL 2.1 or above always support sRGB so add sRGB support marker
|
|
|
|
_ => (None, "#define SRGB_SUPPORTED"),
|
|
|
|
};
|
2021-10-20 10:29:35 +00:00
|
|
|
|
2021-10-18 21:13:32 +00:00
|
|
|
unsafe {
|
2021-11-03 18:17:07 +00:00
|
|
|
let vert = compile_shader(
|
|
|
|
gl,
|
|
|
|
glow::VERTEX_SHADER,
|
|
|
|
&format!(
|
2021-11-13 11:32:01 +00:00
|
|
|
"{}\n{}\n{}\n{}",
|
2021-11-03 18:17:07 +00:00
|
|
|
header,
|
2021-11-13 11:32:01 +00:00
|
|
|
shader_prefix,
|
2021-11-03 18:17:07 +00:00
|
|
|
shader_version.is_new_shader_interface(),
|
|
|
|
VERT_SRC
|
|
|
|
),
|
|
|
|
)?;
|
|
|
|
let frag = compile_shader(
|
|
|
|
gl,
|
|
|
|
glow::FRAGMENT_SHADER,
|
|
|
|
&format!(
|
2021-11-13 11:32:01 +00:00
|
|
|
"{}\n{}\n{}\n{}\n{}",
|
2021-11-03 18:17:07 +00:00
|
|
|
header,
|
2021-11-13 11:32:01 +00:00
|
|
|
shader_prefix,
|
2021-11-03 18:17:07 +00:00
|
|
|
srgb_support_define,
|
|
|
|
shader_version.is_new_shader_interface(),
|
|
|
|
FRAG_SRC
|
|
|
|
),
|
|
|
|
)?;
|
|
|
|
let program = link_program(gl, [vert, frag].iter())?;
|
2021-10-20 10:29:35 +00:00
|
|
|
gl.detach_shader(program, vert);
|
|
|
|
gl.detach_shader(program, frag);
|
|
|
|
gl.delete_shader(vert);
|
|
|
|
gl.delete_shader(frag);
|
2021-10-18 21:13:32 +00:00
|
|
|
let u_screen_size = gl.get_uniform_location(program, "u_screen_size").unwrap();
|
|
|
|
let u_sampler = gl.get_uniform_location(program, "u_sampler").unwrap();
|
2021-11-03 18:17:07 +00:00
|
|
|
let vertex_buffer = gl.create_buffer()?;
|
|
|
|
let element_array_buffer = gl.create_buffer()?;
|
2021-10-18 21:13:32 +00:00
|
|
|
gl.bind_buffer(glow::ARRAY_BUFFER, Some(vertex_buffer));
|
|
|
|
let a_pos_loc = gl.get_attrib_location(program, "a_pos").unwrap();
|
|
|
|
let a_tc_loc = gl.get_attrib_location(program, "a_tc").unwrap();
|
|
|
|
let a_srgba_loc = gl.get_attrib_location(program, "a_srgba").unwrap();
|
2021-11-13 11:32:01 +00:00
|
|
|
let mut vertex_array = if support_vao {
|
2021-11-03 18:17:07 +00:00
|
|
|
crate::misc_util::VAO::native(gl)
|
2021-11-13 11:32:01 +00:00
|
|
|
} else {
|
|
|
|
crate::misc_util::VAO::emulated()
|
2021-11-03 18:17:07 +00:00
|
|
|
};
|
|
|
|
vertex_array.bind_vertex_array(gl);
|
|
|
|
vertex_array.bind_buffer(gl, &vertex_buffer);
|
|
|
|
let stride = std::mem::size_of::<Vertex>() as i32;
|
|
|
|
let position_buffer_info = vao_emulate::BufferInfo {
|
|
|
|
location: a_pos_loc,
|
|
|
|
vector_size: 2,
|
|
|
|
data_type: glow::FLOAT,
|
|
|
|
normalized: false,
|
|
|
|
stride,
|
|
|
|
offset: offset_of!(Vertex, pos) as i32,
|
|
|
|
};
|
|
|
|
let tex_coord_buffer_info = vao_emulate::BufferInfo {
|
|
|
|
location: a_tc_loc,
|
|
|
|
vector_size: 2,
|
|
|
|
data_type: glow::FLOAT,
|
|
|
|
normalized: false,
|
|
|
|
stride,
|
|
|
|
offset: offset_of!(Vertex, uv) as i32,
|
|
|
|
};
|
|
|
|
let color_buffer_info = vao_emulate::BufferInfo {
|
|
|
|
location: a_srgba_loc,
|
|
|
|
vector_size: 4,
|
|
|
|
data_type: glow::UNSIGNED_BYTE,
|
|
|
|
normalized: false,
|
|
|
|
stride,
|
|
|
|
offset: offset_of!(Vertex, color) as i32,
|
|
|
|
};
|
|
|
|
vertex_array.add_new_attribute(gl, position_buffer_info);
|
|
|
|
vertex_array.add_new_attribute(gl, tex_coord_buffer_info);
|
|
|
|
vertex_array.add_new_attribute(gl, color_buffer_info);
|
2021-10-18 21:13:32 +00:00
|
|
|
assert_eq!(gl.get_error(), glow::NO_ERROR, "OpenGL error occurred!");
|
|
|
|
|
2021-11-03 18:17:07 +00:00
|
|
|
Ok(Painter {
|
2021-10-18 21:13:32 +00:00
|
|
|
program,
|
|
|
|
u_screen_size,
|
|
|
|
u_sampler,
|
|
|
|
egui_texture: None,
|
|
|
|
egui_texture_version: None,
|
2021-11-03 18:17:07 +00:00
|
|
|
is_webgl_1,
|
|
|
|
is_embedded: matches!(shader_version, ShaderVersion::Es100 | ShaderVersion::Es300),
|
2021-10-18 21:13:32 +00:00
|
|
|
vertex_array,
|
2021-11-03 18:17:07 +00:00
|
|
|
srgb_support,
|
|
|
|
user_textures: Default::default(),
|
|
|
|
post_process,
|
2021-10-18 21:13:32 +00:00
|
|
|
vertex_buffer,
|
|
|
|
element_array_buffer,
|
|
|
|
old_textures: Vec::new(),
|
|
|
|
destroyed: false,
|
2021-11-03 18:17:07 +00:00
|
|
|
})
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn upload_egui_texture(&mut self, gl: &glow::Context, texture: &egui::Texture) {
|
|
|
|
self.assert_not_destroyed();
|
|
|
|
|
|
|
|
if self.egui_texture_version == Some(texture.version) {
|
|
|
|
return; // No change
|
|
|
|
}
|
2021-11-03 18:17:07 +00:00
|
|
|
let gamma = if self.is_embedded && self.post_process.is_none() {
|
|
|
|
1.0 / 2.2
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
};
|
2021-10-18 21:13:32 +00:00
|
|
|
let pixels: Vec<u8> = texture
|
2021-11-03 18:17:07 +00:00
|
|
|
.srgba_pixels(gamma)
|
|
|
|
.flat_map(|a| Vec::from(a.to_array()))
|
2021-10-18 21:13:32 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
if let Some(old_tex) = std::mem::replace(
|
|
|
|
&mut self.egui_texture,
|
2021-11-03 18:17:07 +00:00
|
|
|
Some(srgbtexture2d(
|
|
|
|
gl,
|
|
|
|
self.is_webgl_1,
|
|
|
|
self.srgb_support,
|
|
|
|
&pixels,
|
|
|
|
texture.width,
|
|
|
|
texture.height,
|
|
|
|
)),
|
2021-10-18 21:13:32 +00:00
|
|
|
) {
|
|
|
|
unsafe {
|
|
|
|
gl.delete_texture(old_tex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.egui_texture_version = Some(texture.version);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn prepare_painting(
|
|
|
|
&mut self,
|
2021-11-03 18:17:07 +00:00
|
|
|
[width_in_pixels, height_in_pixels]: [u32; 2],
|
2021-10-18 21:13:32 +00:00
|
|
|
gl: &glow::Context,
|
|
|
|
pixels_per_point: f32,
|
|
|
|
) -> (u32, u32) {
|
|
|
|
gl.enable(glow::SCISSOR_TEST);
|
2021-10-20 10:29:35 +00:00
|
|
|
// egui outputs mesh in both winding orders
|
2021-10-18 21:13:32 +00:00
|
|
|
gl.disable(glow::CULL_FACE);
|
|
|
|
|
|
|
|
gl.enable(glow::BLEND);
|
|
|
|
gl.blend_equation(glow::FUNC_ADD);
|
|
|
|
gl.blend_func_separate(
|
|
|
|
// egui outputs colors with premultiplied alpha:
|
|
|
|
glow::ONE,
|
|
|
|
glow::ONE_MINUS_SRC_ALPHA,
|
|
|
|
// 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).
|
|
|
|
glow::ONE_MINUS_DST_ALPHA,
|
|
|
|
glow::ONE,
|
|
|
|
);
|
|
|
|
|
|
|
|
let width_in_points = width_in_pixels as f32 / pixels_per_point;
|
|
|
|
let height_in_points = height_in_pixels as f32 / pixels_per_point;
|
|
|
|
|
|
|
|
gl.viewport(0, 0, width_in_pixels as i32, height_in_pixels as i32);
|
|
|
|
|
|
|
|
gl.use_program(Some(self.program));
|
|
|
|
|
|
|
|
gl.uniform_2_f32(Some(&self.u_screen_size), width_in_points, height_in_points);
|
|
|
|
gl.uniform_1_i32(Some(&self.u_sampler), 0);
|
|
|
|
gl.active_texture(glow::TEXTURE0);
|
2021-11-03 18:17:07 +00:00
|
|
|
self.vertex_array.bind_vertex_array(gl);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
|
|
|
gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.element_array_buffer));
|
|
|
|
|
|
|
|
(width_in_pixels, height_in_pixels)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Main entry-point for painting a frame.
|
|
|
|
/// You should call `target.clear_color(..)` before
|
|
|
|
/// and `target.finish()` after this.
|
|
|
|
///
|
|
|
|
/// The following OpenGL features will be set:
|
|
|
|
/// - Scissor test will be enabled
|
|
|
|
/// - Cull face will be disabled
|
|
|
|
/// - Blend will be enabled
|
|
|
|
///
|
|
|
|
/// The scissor area and blend parameters will be changed.
|
|
|
|
///
|
2021-11-03 18:17:07 +00:00
|
|
|
/// As well as this, the following objects will be unset:
|
2021-10-18 21:13:32 +00:00
|
|
|
/// - Vertex Buffer
|
|
|
|
/// - Element Buffer
|
|
|
|
/// - Texture (and active texture will be set to 0)
|
|
|
|
/// - Program
|
|
|
|
///
|
|
|
|
/// Please be mindful of these effects when integrating into your program, and also be mindful
|
|
|
|
/// of the effects your program might have on this code. Look at the source if in doubt.
|
|
|
|
pub fn paint_meshes(
|
|
|
|
&mut self,
|
2021-11-03 18:17:07 +00:00
|
|
|
inner_size: [u32; 2],
|
2021-10-18 21:13:32 +00:00
|
|
|
gl: &glow::Context,
|
|
|
|
pixels_per_point: f32,
|
|
|
|
clipped_meshes: Vec<egui::ClippedMesh>,
|
|
|
|
) {
|
2021-11-03 18:17:07 +00:00
|
|
|
//chimera of egui_glow and egui_web
|
2021-10-18 21:13:32 +00:00
|
|
|
self.assert_not_destroyed();
|
|
|
|
|
|
|
|
self.upload_pending_user_textures(gl);
|
2021-11-03 18:17:07 +00:00
|
|
|
if let Some(ref mut post_process) = self.post_process {
|
|
|
|
unsafe {
|
|
|
|
post_process.begin(gl, inner_size[0] as i32, inner_size[1] as i32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let size_in_pixels = unsafe { self.prepare_painting(inner_size, gl, pixels_per_point) };
|
2021-10-18 21:13:32 +00:00
|
|
|
for egui::ClippedMesh(clip_rect, mesh) in clipped_meshes {
|
2021-10-20 14:43:40 +00:00
|
|
|
self.paint_mesh(gl, size_in_pixels, pixels_per_point, clip_rect, &mesh);
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
2021-11-03 18:17:07 +00:00
|
|
|
unsafe {
|
|
|
|
self.vertex_array.unbind_vertex_array(gl);
|
|
|
|
gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2021-11-03 18:17:07 +00:00
|
|
|
if let Some(ref post_process) = self.post_process {
|
|
|
|
post_process.end(gl);
|
|
|
|
}
|
|
|
|
assert_eq!(glow::NO_ERROR, gl.get_error(), "GL error occurred!");
|
|
|
|
}
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(never)] // Easier profiling
|
|
|
|
fn paint_mesh(
|
|
|
|
&mut self,
|
|
|
|
gl: &glow::Context,
|
|
|
|
size_in_pixels: (u32, u32),
|
|
|
|
pixels_per_point: f32,
|
|
|
|
clip_rect: Rect,
|
|
|
|
mesh: &Mesh,
|
|
|
|
) {
|
|
|
|
debug_assert!(mesh.is_valid());
|
|
|
|
if let Some(texture) = self.get_texture(mesh.texture_id) {
|
|
|
|
unsafe {
|
2021-11-03 18:17:07 +00:00
|
|
|
gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.vertex_buffer));
|
2021-10-18 21:13:32 +00:00
|
|
|
gl.buffer_data_u8_slice(
|
|
|
|
glow::ARRAY_BUFFER,
|
|
|
|
as_u8_slice(mesh.vertices.as_slice()),
|
|
|
|
glow::STREAM_DRAW,
|
|
|
|
);
|
|
|
|
|
2021-11-03 18:17:07 +00:00
|
|
|
gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.element_array_buffer));
|
2021-10-18 21:13:32 +00:00
|
|
|
gl.buffer_data_u8_slice(
|
|
|
|
glow::ELEMENT_ARRAY_BUFFER,
|
|
|
|
as_u8_slice(mesh.indices.as_slice()),
|
|
|
|
glow::STREAM_DRAW,
|
|
|
|
);
|
|
|
|
|
|
|
|
gl.bind_texture(glow::TEXTURE_2D, Some(texture));
|
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Make sure clip rect can fit within a `u32`:
|
|
|
|
let clip_min_x = clip_min_x.clamp(0.0, size_in_pixels.0 as f32);
|
|
|
|
let clip_min_y = clip_min_y.clamp(0.0, size_in_pixels.1 as f32);
|
|
|
|
let clip_max_x = clip_max_x.clamp(clip_min_x, size_in_pixels.0 as f32);
|
|
|
|
let clip_max_y = clip_max_y.clamp(clip_min_y, size_in_pixels.1 as f32);
|
|
|
|
|
|
|
|
let clip_min_x = clip_min_x.round() as i32;
|
|
|
|
let clip_min_y = clip_min_y.round() as i32;
|
|
|
|
let clip_max_x = clip_max_x.round() as i32;
|
|
|
|
let clip_max_y = clip_max_y.round() as i32;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
gl.scissor(
|
|
|
|
clip_min_x,
|
|
|
|
size_in_pixels.1 as i32 - clip_max_y,
|
|
|
|
clip_max_x - clip_min_x,
|
|
|
|
clip_max_y - clip_min_y,
|
|
|
|
);
|
|
|
|
gl.draw_elements(
|
|
|
|
glow::TRIANGLES,
|
|
|
|
mesh.indices.len() as i32,
|
|
|
|
glow::UNSIGNED_INT,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// user textures: this is an experimental feature.
|
|
|
|
// No need to implement this in your egui integration!
|
|
|
|
|
|
|
|
pub fn alloc_user_texture(&mut self) -> egui::TextureId {
|
|
|
|
self.assert_not_destroyed();
|
|
|
|
|
|
|
|
for (i, tex) in self.user_textures.iter_mut().enumerate() {
|
|
|
|
if tex.is_none() {
|
|
|
|
*tex = Some(Default::default());
|
|
|
|
return egui::TextureId::User(i as u64);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let id = egui::TextureId::User(self.user_textures.len() as u64);
|
|
|
|
self.user_textures.push(Some(Default::default()));
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
|
|
|
/// register glow texture as egui texture
|
|
|
|
/// Usable for render to image rectangle
|
2021-11-03 18:17:07 +00:00
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
|
|
|
pub fn register_glow_texture(&mut self, texture: glow::Texture) -> egui::TextureId {
|
2021-10-18 21:13:32 +00:00
|
|
|
self.assert_not_destroyed();
|
|
|
|
|
|
|
|
let id = self.alloc_user_texture();
|
|
|
|
if let egui::TextureId::User(id) = id {
|
|
|
|
if let Some(Some(user_texture)) = self.user_textures.get_mut(id as usize) {
|
|
|
|
if let UserTexture {
|
|
|
|
gl_texture: Some(old_tex),
|
|
|
|
..
|
|
|
|
} = std::mem::replace(
|
|
|
|
user_texture,
|
|
|
|
UserTexture {
|
|
|
|
data: vec![],
|
|
|
|
size: (0, 0),
|
|
|
|
gl_texture: Some(texture),
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
self.old_textures.push(old_tex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_user_texture(
|
|
|
|
&mut self,
|
|
|
|
id: egui::TextureId,
|
|
|
|
size: (usize, usize),
|
|
|
|
pixels: &[Color32],
|
|
|
|
) {
|
|
|
|
self.assert_not_destroyed();
|
|
|
|
assert_eq!(
|
|
|
|
size.0 * size.1,
|
|
|
|
pixels.len(),
|
2021-11-03 18:17:07 +00:00
|
|
|
"Mismatch between size and texel count"
|
2021-10-18 21:13:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if let egui::TextureId::User(id) = id {
|
|
|
|
if let Some(Some(user_texture)) = self.user_textures.get_mut(id as usize) {
|
|
|
|
let data: Vec<u8> = pixels
|
|
|
|
.iter()
|
|
|
|
.flat_map(|srgba| Vec::from(srgba.to_array()))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if let UserTexture {
|
|
|
|
gl_texture: Some(old_tex),
|
|
|
|
..
|
|
|
|
} = std::mem::replace(
|
|
|
|
user_texture,
|
|
|
|
UserTexture {
|
|
|
|
data,
|
|
|
|
size,
|
|
|
|
gl_texture: None,
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
self.old_textures.push(old_tex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn free_user_texture(&mut self, id: egui::TextureId) {
|
|
|
|
self.assert_not_destroyed();
|
|
|
|
|
|
|
|
if let egui::TextureId::User(id) = id {
|
|
|
|
let index = id as usize;
|
|
|
|
if index < self.user_textures.len() {
|
|
|
|
self.user_textures[index] = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:17:07 +00:00
|
|
|
pub fn get_texture(&self, texture_id: egui::TextureId) -> Option<glow::Texture> {
|
2021-10-18 21:13:32 +00:00
|
|
|
self.assert_not_destroyed();
|
|
|
|
|
|
|
|
match texture_id {
|
|
|
|
egui::TextureId::Egui => self.egui_texture,
|
|
|
|
egui::TextureId::User(id) => self.user_textures.get(id as usize)?.as_ref()?.gl_texture,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn upload_pending_user_textures(&mut self, gl: &glow::Context) {
|
|
|
|
self.assert_not_destroyed();
|
|
|
|
|
|
|
|
for user_texture in self.user_textures.iter_mut().flatten() {
|
|
|
|
if user_texture.gl_texture.is_none() {
|
|
|
|
let data = std::mem::take(&mut user_texture.data);
|
|
|
|
user_texture.gl_texture = Some(srgbtexture2d(
|
|
|
|
gl,
|
2021-11-03 18:17:07 +00:00
|
|
|
self.is_webgl_1,
|
|
|
|
self.srgb_support,
|
2021-10-18 21:13:32 +00:00
|
|
|
&data,
|
|
|
|
user_texture.size.0,
|
|
|
|
user_texture.size.1,
|
|
|
|
));
|
|
|
|
user_texture.size = (0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for t in self.old_textures.drain(..) {
|
|
|
|
unsafe {
|
|
|
|
gl.delete_texture(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn destroy_gl(&self, gl: &glow::Context) {
|
|
|
|
gl.delete_program(self.program);
|
|
|
|
if let Some(tex) = self.egui_texture {
|
|
|
|
gl.delete_texture(tex);
|
|
|
|
}
|
|
|
|
for tex in self.user_textures.iter().flatten() {
|
|
|
|
if let Some(t) = tex.gl_texture {
|
|
|
|
gl.delete_texture(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gl.delete_buffer(self.vertex_buffer);
|
|
|
|
gl.delete_buffer(self.element_array_buffer);
|
|
|
|
for t in &self.old_textures {
|
|
|
|
gl.delete_texture(*t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function must be called before Painter is dropped, as Painter has some OpenGL objects
|
|
|
|
/// that should be deleted.
|
|
|
|
|
2021-11-03 18:17:07 +00:00
|
|
|
pub fn destroy(&mut self, gl: &glow::Context) {
|
|
|
|
debug_assert!(!self.destroyed, "Only destroy once!");
|
2021-10-18 21:13:32 +00:00
|
|
|
unsafe {
|
|
|
|
self.destroy_gl(gl);
|
2021-11-03 18:17:07 +00:00
|
|
|
if let Some(ref post_process) = self.post_process {
|
|
|
|
post_process.destroy(gl);
|
|
|
|
}
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
2021-10-20 10:29:35 +00:00
|
|
|
self.destroyed = true;
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_not_destroyed(&self) {
|
2021-11-03 18:17:07 +00:00
|
|
|
debug_assert!(!self.destroyed, "the egui glow has already been destroyed!");
|
2021-10-18 21:13:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-03 18:35:20 +00:00
|
|
|
|
2021-11-03 18:17:07 +00:00
|
|
|
pub fn clear(gl: &glow::Context, dimension: [u32; 2], clear_color: egui::Rgba) {
|
|
|
|
unsafe {
|
|
|
|
gl.disable(glow::SCISSOR_TEST);
|
|
|
|
|
|
|
|
gl.viewport(0, 0, dimension[0] as i32, dimension[1] as i32);
|
2021-10-18 21:13:32 +00:00
|
|
|
|
2021-11-03 18:17:07 +00:00
|
|
|
let clear_color: Color32 = clear_color.into();
|
|
|
|
gl.clear_color(
|
|
|
|
clear_color[0] as f32 / 255.0,
|
|
|
|
clear_color[1] as f32 / 255.0,
|
|
|
|
clear_color[2] as f32 / 255.0,
|
|
|
|
clear_color[3] as f32 / 255.0,
|
|
|
|
);
|
|
|
|
gl.clear(glow::COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 18:35:20 +00:00
|
|
|
|
2021-10-18 21:13:32 +00:00
|
|
|
impl Drop for Painter {
|
|
|
|
fn drop(&mut self) {
|
2021-10-20 10:29:35 +00:00
|
|
|
debug_assert!(
|
2021-10-18 21:13:32 +00:00
|
|
|
self.destroyed,
|
2021-10-20 10:29:35 +00:00
|
|
|
"Make sure to call destroy() before dropping to avoid leaking OpenGL objects!"
|
2021-10-18 21:13:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-11-03 18:17:07 +00:00
|
|
|
|
2021-11-03 18:35:20 +00:00
|
|
|
#[cfg(feature = "epi")]
|
2021-11-03 18:17:07 +00:00
|
|
|
impl epi::TextureAllocator for Painter {
|
|
|
|
fn alloc_srgba_premultiplied(
|
|
|
|
&mut self,
|
|
|
|
size: (usize, usize),
|
|
|
|
srgba_pixels: &[Color32],
|
|
|
|
) -> egui::TextureId {
|
|
|
|
let id = self.alloc_user_texture();
|
|
|
|
self.set_user_texture(id, size, srgba_pixels);
|
|
|
|
id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn free(&mut self, id: egui::TextureId) {
|
|
|
|
self.free_user_texture(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:35:20 +00:00
|
|
|
#[cfg(feature = "epi")]
|
2021-11-03 18:17:07 +00:00
|
|
|
impl epi::NativeTexture for Painter {
|
|
|
|
type Texture = glow::Texture;
|
|
|
|
|
2021-11-03 18:35:20 +00:00
|
|
|
fn register_native_texture(&mut self, native: Self::Texture) -> egui::TextureId {
|
2021-11-03 18:17:07 +00:00
|
|
|
self.register_glow_texture(native)
|
|
|
|
}
|
|
|
|
|
2021-11-03 18:35:20 +00:00
|
|
|
fn replace_native_texture(&mut self, id: egui::TextureId, replacing: Self::Texture) {
|
2021-11-03 18:17:07 +00:00
|
|
|
if let egui::TextureId::User(id) = id {
|
|
|
|
if let Some(Some(user_texture)) = self.user_textures.get_mut(id as usize) {
|
|
|
|
*user_texture = UserTexture {
|
|
|
|
data: vec![],
|
|
|
|
gl_texture: Some(replacing),
|
|
|
|
size: (0, 0),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|