mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 22:00:03 -04:00
Move all crates into a crates directory (#1940)
This commit is contained in:
132
crates/egui_glow/src/lib.rs
Normal file
132
crates/egui_glow/src/lib.rs
Normal file
@@ -0,0 +1,132 @@
|
||||
//! [`egui`] bindings for [`glow`](https://github.com/grovesNL/glow).
|
||||
//!
|
||||
//! The main types you want to look are are [`Painter`] and [`EguiGlow`].
|
||||
//!
|
||||
//! If you are writing an app, you may want to look at [`eframe`](https://docs.rs/eframe) instead.
|
||||
//!
|
||||
//! ## Feature flags
|
||||
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
|
||||
//!
|
||||
|
||||
#![allow(clippy::float_cmp)]
|
||||
#![allow(clippy::manual_range_contains)]
|
||||
|
||||
pub mod painter;
|
||||
pub use glow;
|
||||
pub use painter::{CallbackFn, Painter};
|
||||
mod misc_util;
|
||||
mod post_process;
|
||||
mod shader_version;
|
||||
mod vao;
|
||||
|
||||
#[cfg(all(not(target_arch = "wasm32"), feature = "winit"))]
|
||||
pub mod winit;
|
||||
#[cfg(all(not(target_arch = "wasm32"), feature = "winit"))]
|
||||
pub use winit::*;
|
||||
|
||||
/// Check for OpenGL error and report it using `tracing::error`.
|
||||
///
|
||||
/// Only active in debug builds!
|
||||
///
|
||||
/// ``` no_run
|
||||
/// # let glow_context = todo!();
|
||||
/// use egui_glow::check_for_gl_error;
|
||||
/// check_for_gl_error!(glow_context);
|
||||
/// check_for_gl_error!(glow_context, "during painting");
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! check_for_gl_error {
|
||||
($gl: expr) => {{
|
||||
if cfg!(debug_assertions) {
|
||||
$crate::check_for_gl_error_impl($gl, file!(), line!(), "")
|
||||
}
|
||||
}};
|
||||
($gl: expr, $context: literal) => {{
|
||||
if cfg!(debug_assertions) {
|
||||
$crate::check_for_gl_error_impl($gl, file!(), line!(), $context)
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
/// Check for OpenGL error and report it using `tracing::error`.
|
||||
///
|
||||
/// WARNING: slow! Only use during setup!
|
||||
///
|
||||
/// ``` no_run
|
||||
/// # let glow_context = todo!();
|
||||
/// use egui_glow::check_for_gl_error_even_in_release;
|
||||
/// check_for_gl_error_even_in_release!(glow_context);
|
||||
/// check_for_gl_error_even_in_release!(glow_context, "during painting");
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! check_for_gl_error_even_in_release {
|
||||
($gl: expr) => {{
|
||||
$crate::check_for_gl_error_impl($gl, file!(), line!(), "")
|
||||
}};
|
||||
($gl: expr, $context: literal) => {{
|
||||
$crate::check_for_gl_error_impl($gl, file!(), line!(), $context)
|
||||
}};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn check_for_gl_error_impl(gl: &glow::Context, file: &str, line: u32, context: &str) {
|
||||
use glow::HasContext as _;
|
||||
#[allow(unsafe_code)]
|
||||
let error_code = unsafe { gl.get_error() };
|
||||
if error_code != glow::NO_ERROR {
|
||||
let error_str = match error_code {
|
||||
glow::INVALID_ENUM => "GL_INVALID_ENUM",
|
||||
glow::INVALID_VALUE => "GL_INVALID_VALUE",
|
||||
glow::INVALID_OPERATION => "GL_INVALID_OPERATION",
|
||||
glow::STACK_OVERFLOW => "GL_STACK_OVERFLOW",
|
||||
glow::STACK_UNDERFLOW => "GL_STACK_UNDERFLOW",
|
||||
glow::OUT_OF_MEMORY => "GL_OUT_OF_MEMORY",
|
||||
glow::INVALID_FRAMEBUFFER_OPERATION => "GL_INVALID_FRAMEBUFFER_OPERATION",
|
||||
glow::CONTEXT_LOST => "GL_CONTEXT_LOST",
|
||||
0x8031 => "GL_TABLE_TOO_LARGE1",
|
||||
0x9242 => "CONTEXT_LOST_WEBGL",
|
||||
_ => "<unknown>",
|
||||
};
|
||||
|
||||
if context.is_empty() {
|
||||
tracing::error!(
|
||||
"GL error, at {}:{}: {} (0x{:X}). Please file a bug at https://github.com/emilk/egui/issues",
|
||||
file,
|
||||
line,
|
||||
error_str,
|
||||
error_code,
|
||||
);
|
||||
} else {
|
||||
tracing::error!(
|
||||
"GL error, at {}:{} ({}): {} (0x{:X}). Please file a bug at https://github.com/emilk/egui/issues",
|
||||
file,
|
||||
line,
|
||||
context,
|
||||
error_str,
|
||||
error_code,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Profiling macro for feature "puffin"
|
||||
macro_rules! profile_function {
|
||||
($($arg: tt)*) => {
|
||||
#[cfg(feature = "puffin")]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
puffin::profile_function!($($arg)*);
|
||||
};
|
||||
}
|
||||
pub(crate) use profile_function;
|
||||
|
||||
/// Profiling macro for feature "puffin"
|
||||
macro_rules! profile_scope {
|
||||
($($arg: tt)*) => {
|
||||
#[cfg(feature = "puffin")]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
puffin::profile_scope!($($arg)*);
|
||||
};
|
||||
}
|
||||
pub(crate) use profile_scope;
|
||||
40
crates/egui_glow/src/misc_util.rs
Normal file
40
crates/egui_glow/src/misc_util.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use glow::HasContext as _;
|
||||
|
||||
pub(crate) unsafe fn compile_shader(
|
||||
gl: &glow::Context,
|
||||
shader_type: u32,
|
||||
source: &str,
|
||||
) -> Result<glow::Shader, String> {
|
||||
let shader = gl.create_shader(shader_type)?;
|
||||
|
||||
gl.shader_source(shader, source);
|
||||
|
||||
gl.compile_shader(shader);
|
||||
|
||||
if gl.get_shader_compile_status(shader) {
|
||||
Ok(shader)
|
||||
} else {
|
||||
Err(gl.get_shader_info_log(shader))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn link_program<'a, T: IntoIterator<Item = &'a glow::Shader>>(
|
||||
gl: &glow::Context,
|
||||
shaders: T,
|
||||
) -> Result<glow::Program, String> {
|
||||
let program = gl.create_program()?;
|
||||
|
||||
for shader in shaders {
|
||||
gl.attach_shader(program, *shader);
|
||||
}
|
||||
|
||||
gl.link_program(program);
|
||||
|
||||
if gl.get_program_link_status(program) {
|
||||
Ok(program)
|
||||
} else {
|
||||
Err(gl.get_program_info_log(program))
|
||||
}
|
||||
}
|
||||
768
crates/egui_glow/src/painter.rs
Normal file
768
crates/egui_glow/src/painter.rs
Normal file
@@ -0,0 +1,768 @@
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use egui::{
|
||||
emath::Rect,
|
||||
epaint::{Color32, Mesh, PaintCallbackInfo, Primitive, Vertex},
|
||||
};
|
||||
use glow::HasContext as _;
|
||||
use memoffset::offset_of;
|
||||
|
||||
use crate::check_for_gl_error;
|
||||
use crate::misc_util::{compile_shader, link_program};
|
||||
use crate::post_process::PostProcess;
|
||||
use crate::shader_version::ShaderVersion;
|
||||
use crate::vao;
|
||||
|
||||
pub use glow::Context;
|
||||
|
||||
const VERT_SRC: &str = include_str!("shader/vertex.glsl");
|
||||
const FRAG_SRC: &str = include_str!("shader/fragment.glsl");
|
||||
|
||||
pub type TextureFilter = egui::TextureFilter;
|
||||
|
||||
trait TextureFilterExt {
|
||||
fn glow_code(&self) -> u32;
|
||||
}
|
||||
|
||||
impl TextureFilterExt for TextureFilter {
|
||||
fn glow_code(&self) -> u32 {
|
||||
match self {
|
||||
TextureFilter::Linear => glow::LINEAR,
|
||||
TextureFilter::Nearest => glow::NEAREST,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An OpenGL painter using [`glow`].
|
||||
///
|
||||
/// This is responsible for painting egui and managing egui textures.
|
||||
/// You can access the underlying [`glow::Context`] with [`Self::gl`].
|
||||
///
|
||||
/// 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 {
|
||||
gl: Arc<glow::Context>,
|
||||
|
||||
max_texture_side: usize,
|
||||
|
||||
program: glow::Program,
|
||||
u_screen_size: glow::UniformLocation,
|
||||
u_sampler: glow::UniformLocation,
|
||||
is_webgl_1: bool,
|
||||
is_embedded: bool,
|
||||
vao: crate::vao::VertexArrayObject,
|
||||
srgb_support: bool,
|
||||
post_process: Option<PostProcess>,
|
||||
vbo: glow::Buffer,
|
||||
element_array_buffer: glow::Buffer,
|
||||
|
||||
textures: HashMap<egui::TextureId, glow::Texture>,
|
||||
|
||||
next_native_tex_id: u64,
|
||||
|
||||
/// Stores outdated OpenGL textures that are yet to be deleted
|
||||
textures_to_destroy: Vec<glow::Texture>,
|
||||
|
||||
/// Used to make sure we are destroyed correctly.
|
||||
destroyed: bool,
|
||||
}
|
||||
|
||||
/// A callback function that can be used to compose an [`egui::PaintCallback`] for custom rendering
|
||||
/// with [`glow`].
|
||||
///
|
||||
/// The callback is passed, the [`egui::PaintCallbackInfo`] and the [`Painter`] which can be used to
|
||||
/// access the OpenGL context.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// See the [`custom3d_glow`](https://github.com/emilk/egui/blob/master/egui_demo_app/src/apps/custom3d_wgpu.rs) demo source for a detailed usage example.
|
||||
pub struct CallbackFn {
|
||||
f: Box<dyn Fn(PaintCallbackInfo, &Painter) + Sync + Send>,
|
||||
}
|
||||
|
||||
impl CallbackFn {
|
||||
pub fn new<F: Fn(PaintCallbackInfo, &Painter) + Sync + Send + 'static>(callback: F) -> Self {
|
||||
let f = Box::new(callback);
|
||||
CallbackFn { f }
|
||||
}
|
||||
}
|
||||
|
||||
impl Painter {
|
||||
/// Create painter.
|
||||
///
|
||||
/// Set `pp_fb_extent` to the framebuffer size to enable `sRGB` support on OpenGL ES and WebGL.
|
||||
///
|
||||
/// Set `shader_prefix` if you want to turn on shader workaround e.g. `"#define APPLY_BRIGHTENING_GAMMA\n"`
|
||||
/// (see <https://github.com/emilk/egui/issues/794>).
|
||||
///
|
||||
/// # Errors
|
||||
/// will return `Err` below cases
|
||||
/// * failed to compile shader
|
||||
/// * failed to create postprocess on webgl with `sRGB` support
|
||||
/// * failed to create buffer
|
||||
pub fn new(
|
||||
gl: Arc<glow::Context>,
|
||||
pp_fb_extent: Option<[i32; 2]>,
|
||||
shader_prefix: &str,
|
||||
) -> Result<Painter, String> {
|
||||
crate::profile_function!();
|
||||
crate::check_for_gl_error_even_in_release!(&gl, "before Painter::new");
|
||||
|
||||
let max_texture_side = unsafe { gl.get_parameter_i32(glow::MAX_TEXTURE_SIZE) } as usize;
|
||||
|
||||
let shader_version = ShaderVersion::get(&gl);
|
||||
let is_webgl_1 = shader_version == ShaderVersion::Es100;
|
||||
let header = shader_version.version();
|
||||
tracing::debug!("Shader header: {:?}.", header);
|
||||
let srgb_support = gl.supported_extensions().contains("EXT_sRGB");
|
||||
|
||||
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(size) = pp_fb_extent {
|
||||
tracing::debug!("WebGL with sRGB enabled. Turning on post processing for linear framebuffer blending.");
|
||||
// install post process to correct sRGB color:
|
||||
(
|
||||
Some(PostProcess::new(
|
||||
gl.clone(),
|
||||
shader_prefix,
|
||||
is_webgl_1,
|
||||
size,
|
||||
)?),
|
||||
"#define SRGB_SUPPORTED",
|
||||
)
|
||||
} else {
|
||||
tracing::debug!("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"),
|
||||
};
|
||||
|
||||
unsafe {
|
||||
let vert = compile_shader(
|
||||
&gl,
|
||||
glow::VERTEX_SHADER,
|
||||
&format!(
|
||||
"{}\n{}\n{}\n{}",
|
||||
header,
|
||||
shader_prefix,
|
||||
shader_version.is_new_shader_interface(),
|
||||
VERT_SRC
|
||||
),
|
||||
)?;
|
||||
let frag = compile_shader(
|
||||
&gl,
|
||||
glow::FRAGMENT_SHADER,
|
||||
&format!(
|
||||
"{}\n{}\n{}\n{}\n{}",
|
||||
header,
|
||||
shader_prefix,
|
||||
srgb_support_define,
|
||||
shader_version.is_new_shader_interface(),
|
||||
FRAG_SRC
|
||||
),
|
||||
)?;
|
||||
let program = link_program(&gl, [vert, frag].iter())?;
|
||||
gl.detach_shader(program, vert);
|
||||
gl.detach_shader(program, frag);
|
||||
gl.delete_shader(vert);
|
||||
gl.delete_shader(frag);
|
||||
let u_screen_size = gl.get_uniform_location(program, "u_screen_size").unwrap();
|
||||
let u_sampler = gl.get_uniform_location(program, "u_sampler").unwrap();
|
||||
|
||||
let vbo = gl.create_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();
|
||||
|
||||
let stride = std::mem::size_of::<Vertex>() as i32;
|
||||
let buffer_infos = vec![
|
||||
vao::BufferInfo {
|
||||
location: a_pos_loc,
|
||||
vector_size: 2,
|
||||
data_type: glow::FLOAT,
|
||||
normalized: false,
|
||||
stride,
|
||||
offset: offset_of!(Vertex, pos) as i32,
|
||||
},
|
||||
vao::BufferInfo {
|
||||
location: a_tc_loc,
|
||||
vector_size: 2,
|
||||
data_type: glow::FLOAT,
|
||||
normalized: false,
|
||||
stride,
|
||||
offset: offset_of!(Vertex, uv) as i32,
|
||||
},
|
||||
vao::BufferInfo {
|
||||
location: a_srgba_loc,
|
||||
vector_size: 4,
|
||||
data_type: glow::UNSIGNED_BYTE,
|
||||
normalized: false,
|
||||
stride,
|
||||
offset: offset_of!(Vertex, color) as i32,
|
||||
},
|
||||
];
|
||||
let vao = crate::vao::VertexArrayObject::new(&gl, vbo, buffer_infos);
|
||||
|
||||
let element_array_buffer = gl.create_buffer()?;
|
||||
|
||||
crate::check_for_gl_error_even_in_release!(&gl, "after Painter::new");
|
||||
|
||||
Ok(Painter {
|
||||
gl,
|
||||
max_texture_side,
|
||||
program,
|
||||
u_screen_size,
|
||||
u_sampler,
|
||||
is_webgl_1,
|
||||
is_embedded: matches!(shader_version, ShaderVersion::Es100 | ShaderVersion::Es300),
|
||||
vao,
|
||||
srgb_support,
|
||||
post_process,
|
||||
vbo,
|
||||
element_array_buffer,
|
||||
textures: Default::default(),
|
||||
next_native_tex_id: 1 << 32,
|
||||
textures_to_destroy: Vec::new(),
|
||||
destroyed: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Access the shared glow context.
|
||||
pub fn gl(&self) -> &Arc<glow::Context> {
|
||||
&self.gl
|
||||
}
|
||||
|
||||
pub fn max_texture_side(&self) -> usize {
|
||||
self.max_texture_side
|
||||
}
|
||||
|
||||
/// The framebuffer we use as an intermediate render target,
|
||||
/// or `None` if we are painting to the screen framebuffer directly.
|
||||
///
|
||||
/// This is the framebuffer that is bound when [`egui::Shape::Callback`] is called,
|
||||
/// and is where any callbacks should ultimately render onto.
|
||||
///
|
||||
/// So if in a [`egui::Shape::Callback`] you need to use an offscreen FBO, you should
|
||||
/// then restore to this afterwards with
|
||||
/// `gl.bind_framebuffer(glow::FRAMEBUFFER, painter.intermediate_fbo());`
|
||||
pub fn intermediate_fbo(&self) -> Option<glow::Framebuffer> {
|
||||
self.post_process.as_ref().map(|pp| pp.fbo())
|
||||
}
|
||||
|
||||
unsafe fn prepare_painting(
|
||||
&mut self,
|
||||
[width_in_pixels, height_in_pixels]: [u32; 2],
|
||||
pixels_per_point: f32,
|
||||
) -> (u32, u32) {
|
||||
self.gl.enable(glow::SCISSOR_TEST);
|
||||
// egui outputs mesh in both winding orders
|
||||
self.gl.disable(glow::CULL_FACE);
|
||||
self.gl.disable(glow::DEPTH_TEST);
|
||||
|
||||
self.gl.color_mask(true, true, true, true);
|
||||
|
||||
self.gl.enable(glow::BLEND);
|
||||
self.gl
|
||||
.blend_equation_separate(glow::FUNC_ADD, glow::FUNC_ADD);
|
||||
self.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,
|
||||
);
|
||||
|
||||
if !cfg!(target_arch = "wasm32") {
|
||||
self.gl.enable(glow::FRAMEBUFFER_SRGB);
|
||||
check_for_gl_error!(&self.gl, "FRAMEBUFFER_SRGB");
|
||||
}
|
||||
|
||||
let width_in_points = width_in_pixels as f32 / pixels_per_point;
|
||||
let height_in_points = height_in_pixels as f32 / pixels_per_point;
|
||||
|
||||
self.gl
|
||||
.viewport(0, 0, width_in_pixels as i32, height_in_pixels as i32);
|
||||
self.gl.use_program(Some(self.program));
|
||||
|
||||
self.gl
|
||||
.uniform_2_f32(Some(&self.u_screen_size), width_in_points, height_in_points);
|
||||
self.gl.uniform_1_i32(Some(&self.u_sampler), 0);
|
||||
self.gl.active_texture(glow::TEXTURE0);
|
||||
|
||||
self.vao.bind(&self.gl);
|
||||
self.gl
|
||||
.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.element_array_buffer));
|
||||
|
||||
check_for_gl_error!(&self.gl, "prepare_painting");
|
||||
|
||||
(width_in_pixels, height_in_pixels)
|
||||
}
|
||||
|
||||
/// You are expected to have cleared the color buffer before calling this.
|
||||
pub fn paint_and_update_textures(
|
||||
&mut self,
|
||||
screen_size_px: [u32; 2],
|
||||
pixels_per_point: f32,
|
||||
clipped_primitives: &[egui::ClippedPrimitive],
|
||||
textures_delta: &egui::TexturesDelta,
|
||||
) {
|
||||
crate::profile_function!();
|
||||
for (id, image_delta) in &textures_delta.set {
|
||||
self.set_texture(*id, image_delta);
|
||||
}
|
||||
|
||||
self.paint_primitives(screen_size_px, pixels_per_point, clipped_primitives);
|
||||
|
||||
for &id in &textures_delta.free {
|
||||
self.free_texture(id);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// As well as this, the following objects will be unset:
|
||||
/// - 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_primitives(
|
||||
&mut self,
|
||||
screen_size_px: [u32; 2],
|
||||
pixels_per_point: f32,
|
||||
clipped_primitives: &[egui::ClippedPrimitive],
|
||||
) {
|
||||
crate::profile_function!();
|
||||
self.assert_not_destroyed();
|
||||
|
||||
if let Some(ref mut post_process) = self.post_process {
|
||||
unsafe {
|
||||
post_process.begin(screen_size_px[0] as i32, screen_size_px[1] as i32);
|
||||
post_process.bind();
|
||||
self.gl.disable(glow::SCISSOR_TEST);
|
||||
self.gl
|
||||
.viewport(0, 0, screen_size_px[0] as i32, screen_size_px[1] as i32);
|
||||
// use the same clear-color as was set for the screen framebuffer.
|
||||
self.gl.clear(glow::COLOR_BUFFER_BIT);
|
||||
}
|
||||
}
|
||||
let size_in_pixels = unsafe { self.prepare_painting(screen_size_px, pixels_per_point) };
|
||||
|
||||
for egui::ClippedPrimitive {
|
||||
clip_rect,
|
||||
primitive,
|
||||
} in clipped_primitives
|
||||
{
|
||||
set_clip_rect(&self.gl, size_in_pixels, pixels_per_point, *clip_rect);
|
||||
|
||||
match primitive {
|
||||
Primitive::Mesh(mesh) => {
|
||||
self.paint_mesh(mesh);
|
||||
}
|
||||
Primitive::Callback(callback) => {
|
||||
if callback.rect.is_positive() {
|
||||
crate::profile_scope!("callback");
|
||||
// Transform callback rect to physical pixels:
|
||||
let rect_min_x = pixels_per_point * callback.rect.min.x;
|
||||
let rect_min_y = pixels_per_point * callback.rect.min.y;
|
||||
let rect_max_x = pixels_per_point * callback.rect.max.x;
|
||||
let rect_max_y = pixels_per_point * callback.rect.max.y;
|
||||
|
||||
let rect_min_x = rect_min_x.round() as i32;
|
||||
let rect_min_y = rect_min_y.round() as i32;
|
||||
let rect_max_x = rect_max_x.round() as i32;
|
||||
let rect_max_y = rect_max_y.round() as i32;
|
||||
|
||||
unsafe {
|
||||
self.gl.viewport(
|
||||
rect_min_x,
|
||||
size_in_pixels.1 as i32 - rect_max_y,
|
||||
rect_max_x - rect_min_x,
|
||||
rect_max_y - rect_min_y,
|
||||
);
|
||||
}
|
||||
|
||||
let info = egui::PaintCallbackInfo {
|
||||
viewport: callback.rect,
|
||||
clip_rect: *clip_rect,
|
||||
pixels_per_point,
|
||||
screen_size_px,
|
||||
};
|
||||
|
||||
if let Some(callback) = callback.callback.downcast_ref::<CallbackFn>() {
|
||||
(callback.f)(info, self);
|
||||
} else {
|
||||
tracing::warn!("Warning: Unsupported render callback. Expected egui_glow::CallbackFn");
|
||||
}
|
||||
|
||||
check_for_gl_error!(&self.gl, "callback");
|
||||
|
||||
// Restore state:
|
||||
unsafe {
|
||||
if let Some(ref mut post_process) = self.post_process {
|
||||
post_process.bind();
|
||||
}
|
||||
self.prepare_painting(screen_size_px, pixels_per_point)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
self.vao.unbind(&self.gl);
|
||||
self.gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None);
|
||||
|
||||
if let Some(ref post_process) = self.post_process {
|
||||
post_process.end();
|
||||
}
|
||||
|
||||
self.gl.disable(glow::SCISSOR_TEST);
|
||||
|
||||
check_for_gl_error!(&self.gl, "painting");
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(never)] // Easier profiling
|
||||
fn paint_mesh(&mut self, mesh: &Mesh) {
|
||||
debug_assert!(mesh.is_valid());
|
||||
if let Some(texture) = self.texture(mesh.texture_id) {
|
||||
unsafe {
|
||||
self.gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.vbo));
|
||||
self.gl.buffer_data_u8_slice(
|
||||
glow::ARRAY_BUFFER,
|
||||
bytemuck::cast_slice(&mesh.vertices),
|
||||
glow::STREAM_DRAW,
|
||||
);
|
||||
|
||||
self.gl
|
||||
.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.element_array_buffer));
|
||||
self.gl.buffer_data_u8_slice(
|
||||
glow::ELEMENT_ARRAY_BUFFER,
|
||||
bytemuck::cast_slice(&mesh.indices),
|
||||
glow::STREAM_DRAW,
|
||||
);
|
||||
|
||||
self.gl.bind_texture(glow::TEXTURE_2D, Some(texture));
|
||||
}
|
||||
|
||||
unsafe {
|
||||
self.gl.draw_elements(
|
||||
glow::TRIANGLES,
|
||||
mesh.indices.len() as i32,
|
||||
glow::UNSIGNED_INT,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
check_for_gl_error!(&self.gl, "paint_mesh");
|
||||
} else {
|
||||
tracing::warn!("Failed to find texture {:?}", mesh.texture_id);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
pub fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta) {
|
||||
crate::profile_function!();
|
||||
|
||||
self.assert_not_destroyed();
|
||||
|
||||
let glow_texture = *self
|
||||
.textures
|
||||
.entry(tex_id)
|
||||
.or_insert_with(|| unsafe { self.gl.create_texture().unwrap() });
|
||||
unsafe {
|
||||
self.gl.bind_texture(glow::TEXTURE_2D, Some(glow_texture));
|
||||
}
|
||||
|
||||
match &delta.image {
|
||||
egui::ImageData::Color(image) => {
|
||||
assert_eq!(
|
||||
image.width() * image.height(),
|
||||
image.pixels.len(),
|
||||
"Mismatch between texture size and texel count"
|
||||
);
|
||||
|
||||
let data: &[u8] = bytemuck::cast_slice(image.pixels.as_ref());
|
||||
|
||||
self.upload_texture_srgb(delta.pos, image.size, delta.filter, data);
|
||||
}
|
||||
egui::ImageData::Font(image) => {
|
||||
assert_eq!(
|
||||
image.width() * image.height(),
|
||||
image.pixels.len(),
|
||||
"Mismatch between texture size and texel count"
|
||||
);
|
||||
|
||||
let gamma = if self.is_embedded && self.post_process.is_none() {
|
||||
1.0 / 2.2
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
let data: Vec<u8> = image
|
||||
.srgba_pixels(gamma)
|
||||
.flat_map(|a| a.to_array())
|
||||
.collect();
|
||||
|
||||
self.upload_texture_srgb(delta.pos, image.size, delta.filter, &data);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn upload_texture_srgb(
|
||||
&mut self,
|
||||
pos: Option<[usize; 2]>,
|
||||
[w, h]: [usize; 2],
|
||||
texture_filter: TextureFilter,
|
||||
data: &[u8],
|
||||
) {
|
||||
assert_eq!(data.len(), w * h * 4);
|
||||
assert!(
|
||||
w >= 1 && h >= 1,
|
||||
"Got a texture image of size {}x{}. A texture must at least be one texel wide.",
|
||||
w,
|
||||
h
|
||||
);
|
||||
assert!(
|
||||
w <= self.max_texture_side && h <= self.max_texture_side,
|
||||
"Got a texture image of size {}x{}, but the maximum supported texture side is only {}",
|
||||
w,
|
||||
h,
|
||||
self.max_texture_side
|
||||
);
|
||||
|
||||
unsafe {
|
||||
self.gl.tex_parameter_i32(
|
||||
glow::TEXTURE_2D,
|
||||
glow::TEXTURE_MAG_FILTER,
|
||||
texture_filter.glow_code() as i32,
|
||||
);
|
||||
self.gl.tex_parameter_i32(
|
||||
glow::TEXTURE_2D,
|
||||
glow::TEXTURE_MIN_FILTER,
|
||||
texture_filter.glow_code() as i32,
|
||||
);
|
||||
|
||||
self.gl.tex_parameter_i32(
|
||||
glow::TEXTURE_2D,
|
||||
glow::TEXTURE_WRAP_S,
|
||||
glow::CLAMP_TO_EDGE as i32,
|
||||
);
|
||||
self.gl.tex_parameter_i32(
|
||||
glow::TEXTURE_2D,
|
||||
glow::TEXTURE_WRAP_T,
|
||||
glow::CLAMP_TO_EDGE as i32,
|
||||
);
|
||||
check_for_gl_error!(&self.gl, "tex_parameter");
|
||||
|
||||
let (internal_format, src_format) = if self.is_webgl_1 {
|
||||
let format = if self.srgb_support {
|
||||
glow::SRGB_ALPHA
|
||||
} else {
|
||||
glow::RGBA
|
||||
};
|
||||
(format, format)
|
||||
} else {
|
||||
(glow::SRGB8_ALPHA8, glow::RGBA)
|
||||
};
|
||||
|
||||
self.gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1);
|
||||
|
||||
let level = 0;
|
||||
if let Some([x, y]) = pos {
|
||||
self.gl.tex_sub_image_2d(
|
||||
glow::TEXTURE_2D,
|
||||
level,
|
||||
x as _,
|
||||
y as _,
|
||||
w as _,
|
||||
h as _,
|
||||
src_format,
|
||||
glow::UNSIGNED_BYTE,
|
||||
glow::PixelUnpackData::Slice(data),
|
||||
);
|
||||
check_for_gl_error!(&self.gl, "tex_sub_image_2d");
|
||||
} else {
|
||||
let border = 0;
|
||||
self.gl.tex_image_2d(
|
||||
glow::TEXTURE_2D,
|
||||
level,
|
||||
internal_format as _,
|
||||
w as _,
|
||||
h as _,
|
||||
border,
|
||||
src_format,
|
||||
glow::UNSIGNED_BYTE,
|
||||
Some(data),
|
||||
);
|
||||
check_for_gl_error!(&self.gl, "tex_image_2d");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn free_texture(&mut self, tex_id: egui::TextureId) {
|
||||
if let Some(old_tex) = self.textures.remove(&tex_id) {
|
||||
unsafe { self.gl.delete_texture(old_tex) };
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the [`glow::Texture`] bound to a [`egui::TextureId`].
|
||||
pub fn texture(&self, texture_id: egui::TextureId) -> Option<glow::Texture> {
|
||||
self.textures.get(&texture_id).copied()
|
||||
}
|
||||
|
||||
#[deprecated = "renamed 'texture'"]
|
||||
pub fn get_texture(&self, texture_id: egui::TextureId) -> Option<glow::Texture> {
|
||||
self.texture(texture_id)
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_pass_by_value)] // False positive
|
||||
pub fn register_native_texture(&mut self, native: glow::Texture) -> egui::TextureId {
|
||||
self.assert_not_destroyed();
|
||||
let id = egui::TextureId::User(self.next_native_tex_id);
|
||||
self.next_native_tex_id += 1;
|
||||
self.textures.insert(id, native);
|
||||
id
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_pass_by_value)] // False positive
|
||||
pub fn replace_native_texture(&mut self, id: egui::TextureId, replacing: glow::Texture) {
|
||||
if let Some(old_tex) = self.textures.insert(id, replacing) {
|
||||
self.textures_to_destroy.push(old_tex);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn destroy_gl(&self) {
|
||||
self.gl.delete_program(self.program);
|
||||
for tex in self.textures.values() {
|
||||
self.gl.delete_texture(*tex);
|
||||
}
|
||||
self.gl.delete_buffer(self.vbo);
|
||||
self.gl.delete_buffer(self.element_array_buffer);
|
||||
for t in &self.textures_to_destroy {
|
||||
self.gl.delete_texture(*t);
|
||||
}
|
||||
}
|
||||
|
||||
/// This function must be called before [`Painter`] is dropped, as [`Painter`] has some OpenGL objects
|
||||
/// that should be deleted.
|
||||
pub fn destroy(&mut self) {
|
||||
if !self.destroyed {
|
||||
unsafe {
|
||||
self.destroy_gl();
|
||||
if let Some(ref post_process) = self.post_process {
|
||||
post_process.destroy();
|
||||
}
|
||||
}
|
||||
self.destroyed = true;
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_not_destroyed(&self) {
|
||||
assert!(!self.destroyed, "the egui glow has already been destroyed!");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear(gl: &glow::Context, screen_size_in_pixels: [u32; 2], clear_color: egui::Rgba) {
|
||||
crate::profile_function!();
|
||||
unsafe {
|
||||
gl.disable(glow::SCISSOR_TEST);
|
||||
|
||||
gl.viewport(
|
||||
0,
|
||||
0,
|
||||
screen_size_in_pixels[0] as i32,
|
||||
screen_size_in_pixels[1] as i32,
|
||||
);
|
||||
|
||||
if true {
|
||||
// verified to be correct on eframe native (on Mac).
|
||||
gl.clear_color(
|
||||
clear_color[0],
|
||||
clear_color[1],
|
||||
clear_color[2],
|
||||
clear_color[3],
|
||||
);
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Painter {
|
||||
fn drop(&mut self) {
|
||||
if !self.destroyed {
|
||||
tracing::warn!(
|
||||
"You forgot to call destroy() on the egui glow painter. Resources will leak!"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_clip_rect(
|
||||
gl: &glow::Context,
|
||||
size_in_pixels: (u32, u32),
|
||||
pixels_per_point: f32,
|
||||
clip_rect: Rect,
|
||||
) {
|
||||
// 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;
|
||||
|
||||
// Round to integer:
|
||||
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;
|
||||
|
||||
// Clamp:
|
||||
let clip_min_x = clip_min_x.clamp(0, size_in_pixels.0 as i32);
|
||||
let clip_min_y = clip_min_y.clamp(0, size_in_pixels.1 as i32);
|
||||
let clip_max_x = clip_max_x.clamp(clip_min_x, size_in_pixels.0 as i32);
|
||||
let clip_max_y = clip_max_y.clamp(clip_min_y, size_in_pixels.1 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,
|
||||
);
|
||||
}
|
||||
}
|
||||
284
crates/egui_glow/src/post_process.rs
Normal file
284
crates/egui_glow/src/post_process.rs
Normal file
@@ -0,0 +1,284 @@
|
||||
#![allow(unsafe_code)]
|
||||
use crate::check_for_gl_error;
|
||||
use crate::misc_util::{compile_shader, link_program};
|
||||
use crate::vao::BufferInfo;
|
||||
use glow::HasContext as _;
|
||||
|
||||
/// Uses a framebuffer to render everything in linear color space and convert it back to `sRGB`
|
||||
/// in a separate "post processing" step
|
||||
pub(crate) struct PostProcess {
|
||||
gl: std::sync::Arc<glow::Context>,
|
||||
pos_buffer: glow::Buffer,
|
||||
index_buffer: glow::Buffer,
|
||||
vao: crate::vao::VertexArrayObject,
|
||||
is_webgl_1: bool,
|
||||
color_texture: glow::Texture,
|
||||
depth_renderbuffer: Option<glow::Renderbuffer>,
|
||||
texture_size: (i32, i32),
|
||||
fbo: glow::Framebuffer,
|
||||
program: glow::Program,
|
||||
}
|
||||
|
||||
impl PostProcess {
|
||||
pub(crate) unsafe fn new(
|
||||
gl: std::sync::Arc<glow::Context>,
|
||||
shader_prefix: &str,
|
||||
is_webgl_1: bool,
|
||||
[width, height]: [i32; 2],
|
||||
) -> Result<PostProcess, String> {
|
||||
gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1);
|
||||
|
||||
let fbo = gl.create_framebuffer()?;
|
||||
|
||||
gl.bind_framebuffer(glow::FRAMEBUFFER, Some(fbo));
|
||||
|
||||
// ----------------------------------------------
|
||||
// Set up color tesxture:
|
||||
|
||||
let color_texture = gl.create_texture()?;
|
||||
gl.bind_texture(glow::TEXTURE_2D, Some(color_texture));
|
||||
gl.tex_parameter_i32(
|
||||
glow::TEXTURE_2D,
|
||||
glow::TEXTURE_WRAP_S,
|
||||
glow::CLAMP_TO_EDGE as i32,
|
||||
);
|
||||
gl.tex_parameter_i32(
|
||||
glow::TEXTURE_2D,
|
||||
glow::TEXTURE_WRAP_T,
|
||||
glow::CLAMP_TO_EDGE as i32,
|
||||
);
|
||||
gl.tex_parameter_i32(
|
||||
glow::TEXTURE_2D,
|
||||
glow::TEXTURE_MIN_FILTER,
|
||||
glow::NEAREST as i32,
|
||||
);
|
||||
gl.tex_parameter_i32(
|
||||
glow::TEXTURE_2D,
|
||||
glow::TEXTURE_MAG_FILTER,
|
||||
glow::NEAREST as i32,
|
||||
);
|
||||
|
||||
let (internal_format, format) = if is_webgl_1 {
|
||||
(glow::SRGB_ALPHA, glow::SRGB_ALPHA)
|
||||
} else {
|
||||
(glow::SRGB8_ALPHA8, glow::RGBA)
|
||||
};
|
||||
|
||||
gl.tex_image_2d(
|
||||
glow::TEXTURE_2D,
|
||||
0,
|
||||
internal_format as i32,
|
||||
width,
|
||||
height,
|
||||
0,
|
||||
format,
|
||||
glow::UNSIGNED_BYTE,
|
||||
None,
|
||||
);
|
||||
crate::check_for_gl_error_even_in_release!(&gl, "post process texture initialization");
|
||||
|
||||
gl.framebuffer_texture_2d(
|
||||
glow::FRAMEBUFFER,
|
||||
glow::COLOR_ATTACHMENT0,
|
||||
glow::TEXTURE_2D,
|
||||
Some(color_texture),
|
||||
0,
|
||||
);
|
||||
gl.bind_texture(glow::TEXTURE_2D, None);
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// Depth buffer - we only need this when embedding 3D within egui using `egui::PaintCallback`.
|
||||
// TODO(emilk): add a setting to enable/disable the depth buffer.
|
||||
|
||||
let with_depth_buffer = true;
|
||||
let depth_renderbuffer = if with_depth_buffer {
|
||||
let depth_renderbuffer = gl.create_renderbuffer()?;
|
||||
gl.bind_renderbuffer(glow::RENDERBUFFER, Some(depth_renderbuffer));
|
||||
gl.renderbuffer_storage(glow::RENDERBUFFER, glow::DEPTH_COMPONENT16, width, height);
|
||||
gl.bind_renderbuffer(glow::RENDERBUFFER, None);
|
||||
Some(depth_renderbuffer)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------
|
||||
|
||||
gl.bind_framebuffer(glow::FRAMEBUFFER, None);
|
||||
|
||||
// ---------------------------------------------------------
|
||||
|
||||
let vert_shader = compile_shader(
|
||||
&gl,
|
||||
glow::VERTEX_SHADER,
|
||||
&format!(
|
||||
"{}\n{}",
|
||||
shader_prefix,
|
||||
include_str!("shader/post_vertex_100es.glsl")
|
||||
),
|
||||
)?;
|
||||
let frag_shader = compile_shader(
|
||||
&gl,
|
||||
glow::FRAGMENT_SHADER,
|
||||
&format!(
|
||||
"{}\n{}",
|
||||
shader_prefix,
|
||||
include_str!("shader/post_fragment_100es.glsl")
|
||||
),
|
||||
)?;
|
||||
let program = link_program(&gl, [vert_shader, frag_shader].iter())?;
|
||||
|
||||
let positions: Vec<f32> = vec![0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0];
|
||||
|
||||
let indices: Vec<u8> = vec![0, 1, 2, 1, 2, 3];
|
||||
|
||||
let pos_buffer = gl.create_buffer()?;
|
||||
gl.bind_buffer(glow::ARRAY_BUFFER, Some(pos_buffer));
|
||||
gl.buffer_data_u8_slice(
|
||||
glow::ARRAY_BUFFER,
|
||||
bytemuck::cast_slice(&positions),
|
||||
glow::STATIC_DRAW,
|
||||
);
|
||||
|
||||
let a_pos_loc = gl
|
||||
.get_attrib_location(program, "a_pos")
|
||||
.ok_or_else(|| "failed to get location of a_pos".to_owned())?;
|
||||
let vao = crate::vao::VertexArrayObject::new(
|
||||
&gl,
|
||||
pos_buffer,
|
||||
vec![BufferInfo {
|
||||
location: a_pos_loc,
|
||||
vector_size: 2,
|
||||
data_type: glow::FLOAT,
|
||||
normalized: false,
|
||||
stride: 0,
|
||||
offset: 0,
|
||||
}],
|
||||
);
|
||||
|
||||
let index_buffer = gl.create_buffer()?;
|
||||
gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(index_buffer));
|
||||
gl.buffer_data_u8_slice(glow::ELEMENT_ARRAY_BUFFER, &indices, glow::STATIC_DRAW);
|
||||
|
||||
gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None);
|
||||
crate::check_for_gl_error_even_in_release!(&gl, "post process initialization");
|
||||
|
||||
Ok(PostProcess {
|
||||
gl,
|
||||
pos_buffer,
|
||||
index_buffer,
|
||||
vao,
|
||||
is_webgl_1,
|
||||
color_texture,
|
||||
depth_renderbuffer,
|
||||
texture_size: (width, height),
|
||||
fbo,
|
||||
program,
|
||||
})
|
||||
}
|
||||
|
||||
/// What we render to.
|
||||
pub(crate) fn fbo(&self) -> glow::Framebuffer {
|
||||
self.fbo
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn begin(&mut self, width: i32, height: i32) {
|
||||
if (width, height) != self.texture_size {
|
||||
self.gl
|
||||
.bind_texture(glow::TEXTURE_2D, Some(self.color_texture));
|
||||
self.gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1);
|
||||
let (internal_format, format) = if self.is_webgl_1 {
|
||||
(glow::SRGB_ALPHA, glow::SRGB_ALPHA)
|
||||
} else {
|
||||
(glow::SRGB8_ALPHA8, glow::RGBA)
|
||||
};
|
||||
self.gl.tex_image_2d(
|
||||
glow::TEXTURE_2D,
|
||||
0,
|
||||
internal_format as i32,
|
||||
width,
|
||||
height,
|
||||
0,
|
||||
format,
|
||||
glow::UNSIGNED_BYTE,
|
||||
None,
|
||||
);
|
||||
self.gl.bind_texture(glow::TEXTURE_2D, None);
|
||||
|
||||
if let Some(depth_renderbuffer) = self.depth_renderbuffer {
|
||||
self.gl
|
||||
.bind_renderbuffer(glow::RENDERBUFFER, Some(depth_renderbuffer));
|
||||
self.gl.renderbuffer_storage(
|
||||
glow::RENDERBUFFER,
|
||||
glow::DEPTH_COMPONENT16,
|
||||
width,
|
||||
height,
|
||||
);
|
||||
self.gl.bind_renderbuffer(glow::RENDERBUFFER, None);
|
||||
}
|
||||
|
||||
self.texture_size = (width, height);
|
||||
}
|
||||
|
||||
check_for_gl_error!(&self.gl, "PostProcess::begin");
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn bind(&self) {
|
||||
self.gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.fbo));
|
||||
|
||||
self.gl.framebuffer_texture_2d(
|
||||
glow::FRAMEBUFFER,
|
||||
glow::COLOR_ATTACHMENT0,
|
||||
glow::TEXTURE_2D,
|
||||
Some(self.color_texture),
|
||||
0,
|
||||
);
|
||||
|
||||
self.gl.framebuffer_renderbuffer(
|
||||
glow::FRAMEBUFFER,
|
||||
glow::DEPTH_ATTACHMENT,
|
||||
glow::RENDERBUFFER,
|
||||
self.depth_renderbuffer,
|
||||
);
|
||||
|
||||
check_for_gl_error!(&self.gl, "PostProcess::bind");
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn end(&self) {
|
||||
self.gl.bind_framebuffer(glow::FRAMEBUFFER, None);
|
||||
self.gl.disable(glow::SCISSOR_TEST);
|
||||
|
||||
self.gl.use_program(Some(self.program));
|
||||
|
||||
self.gl.active_texture(glow::TEXTURE0);
|
||||
self.gl
|
||||
.bind_texture(glow::TEXTURE_2D, Some(self.color_texture));
|
||||
let u_sampler_loc = self
|
||||
.gl
|
||||
.get_uniform_location(self.program, "u_sampler")
|
||||
.unwrap();
|
||||
self.gl.uniform_1_i32(Some(&u_sampler_loc), 0);
|
||||
self.vao.bind(&self.gl);
|
||||
|
||||
self.gl
|
||||
.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.index_buffer));
|
||||
self.gl
|
||||
.draw_elements(glow::TRIANGLES, 6, glow::UNSIGNED_BYTE, 0);
|
||||
self.vao.unbind(&self.gl);
|
||||
self.gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None);
|
||||
self.gl.bind_texture(glow::TEXTURE_2D, None);
|
||||
self.gl.use_program(None);
|
||||
|
||||
check_for_gl_error!(&self.gl, "PostProcess::end");
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn destroy(&self) {
|
||||
self.gl.delete_buffer(self.pos_buffer);
|
||||
self.gl.delete_buffer(self.index_buffer);
|
||||
self.gl.delete_program(self.program);
|
||||
self.gl.delete_framebuffer(self.fbo);
|
||||
self.gl.delete_texture(self.color_texture);
|
||||
if let Some(depth_renderbuffer) = self.depth_renderbuffer {
|
||||
self.gl.delete_renderbuffer(depth_renderbuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
76
crates/egui_glow/src/shader/fragment.glsl
Normal file
76
crates/egui_glow/src/shader/fragment.glsl
Normal file
@@ -0,0 +1,76 @@
|
||||
#ifdef GL_ES
|
||||
precision mediump float;
|
||||
#endif
|
||||
|
||||
uniform sampler2D u_sampler;
|
||||
|
||||
#ifdef NEW_SHADER_INTERFACE
|
||||
in vec4 v_rgba;
|
||||
in vec2 v_tc;
|
||||
out vec4 f_color;
|
||||
// a dirty hack applied to support webGL2
|
||||
#define gl_FragColor f_color
|
||||
#define texture2D texture
|
||||
#else
|
||||
varying vec4 v_rgba;
|
||||
varying vec2 v_tc;
|
||||
#endif
|
||||
|
||||
#ifdef SRGB_SUPPORTED
|
||||
void main() {
|
||||
// The texture sampler is sRGB aware, and OpenGL already expects linear rgba output
|
||||
// so no need for any sRGB conversions here:
|
||||
gl_FragColor = v_rgba * texture2D(u_sampler, v_tc);
|
||||
}
|
||||
#else
|
||||
// 0-255 sRGB from 0-1 linear
|
||||
vec3 srgb_from_linear(vec3 rgb) {
|
||||
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
|
||||
vec3 lower = rgb * vec3(3294.6);
|
||||
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
|
||||
return mix(higher, lower, vec3(cutoff));
|
||||
}
|
||||
|
||||
vec4 srgba_from_linear(vec4 rgba) {
|
||||
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
|
||||
}
|
||||
|
||||
// 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, vec3(cutoff));
|
||||
}
|
||||
|
||||
vec4 linear_from_srgba(vec4 srgba) {
|
||||
return vec4(linear_from_srgb(srgba.rgb), srgba.a / 255.0);
|
||||
}
|
||||
|
||||
void main() {
|
||||
// We must decode the colors, since WebGL1 doesn't come with sRGBA textures:
|
||||
vec4 texture_rgba = linear_from_srgba(texture2D(u_sampler, v_tc) * 255.0);
|
||||
/// Multiply vertex color with texture color (in linear space).
|
||||
gl_FragColor = v_rgba * texture_rgba;
|
||||
|
||||
// WebGL1 doesn't support linear blending in the framebuffer,
|
||||
// so we do a hack here where we change the premultiplied alpha
|
||||
// to do the multiplication in gamma space instead:
|
||||
|
||||
// Unmultiply alpha:
|
||||
if (gl_FragColor.a > 0.0) {
|
||||
gl_FragColor.rgb /= gl_FragColor.a;
|
||||
}
|
||||
|
||||
// Empiric tweak to make e.g. shadows look more like they should:
|
||||
gl_FragColor.a *= sqrt(gl_FragColor.a);
|
||||
|
||||
// To gamma:
|
||||
gl_FragColor = srgba_from_linear(gl_FragColor) / 255.0;
|
||||
|
||||
// Premultiply alpha, this time in gamma space:
|
||||
if (gl_FragColor.a > 0.0) {
|
||||
gl_FragColor.rgb *= gl_FragColor.a;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
26
crates/egui_glow/src/shader/post_fragment_100es.glsl
Normal file
26
crates/egui_glow/src/shader/post_fragment_100es.glsl
Normal file
@@ -0,0 +1,26 @@
|
||||
precision mediump float;
|
||||
uniform sampler2D u_sampler;
|
||||
varying vec2 v_tc;
|
||||
|
||||
// 0-255 sRGB from 0-1 linear
|
||||
vec3 srgb_from_linear(vec3 rgb) {
|
||||
bvec3 cutoff = lessThan(rgb, vec3(0.0031308));
|
||||
vec3 lower = rgb * vec3(3294.6);
|
||||
vec3 higher = vec3(269.025) * pow(rgb, vec3(1.0 / 2.4)) - vec3(14.025);
|
||||
return mix(higher, lower, vec3(cutoff));
|
||||
}
|
||||
|
||||
// 0-255 sRGBA from 0-1 linear
|
||||
vec4 srgba_from_linear(vec4 rgba) {
|
||||
return vec4(srgb_from_linear(rgba.rgb), 255.0 * rgba.a);
|
||||
}
|
||||
|
||||
void main() {
|
||||
gl_FragColor = texture2D(u_sampler, v_tc);
|
||||
|
||||
gl_FragColor = srgba_from_linear(gl_FragColor) / 255.0;
|
||||
|
||||
#ifdef APPLY_BRIGHTENING_GAMMA
|
||||
gl_FragColor = vec4(pow(gl_FragColor.rgb, vec3(1.0/2.2)), gl_FragColor.a);
|
||||
#endif
|
||||
}
|
||||
8
crates/egui_glow/src/shader/post_vertex_100es.glsl
Normal file
8
crates/egui_glow/src/shader/post_vertex_100es.glsl
Normal file
@@ -0,0 +1,8 @@
|
||||
precision mediump float;
|
||||
attribute vec2 a_pos;
|
||||
varying vec2 v_tc;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(a_pos * 2. - 1., 0.0, 1.0);
|
||||
v_tc = a_pos;
|
||||
}
|
||||
43
crates/egui_glow/src/shader/vertex.glsl
Normal file
43
crates/egui_glow/src/shader/vertex.glsl
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifdef NEW_SHADER_INTERFACE
|
||||
#define I in
|
||||
#define O out
|
||||
#define V(x) x
|
||||
#else
|
||||
#define I attribute
|
||||
#define O varying
|
||||
#define V(x) vec3(x)
|
||||
#endif
|
||||
|
||||
#ifdef GL_ES
|
||||
precision mediump float;
|
||||
#endif
|
||||
|
||||
uniform vec2 u_screen_size;
|
||||
I vec2 a_pos;
|
||||
I vec4 a_srgba; // 0-255 sRGB
|
||||
I vec2 a_tc;
|
||||
O vec4 v_rgba;
|
||||
O 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, V(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);
|
||||
// egui encodes vertex colors in gamma space, so we must decode the colors here:
|
||||
v_rgba = linear_from_srgba(a_srgba);
|
||||
v_tc = a_tc;
|
||||
}
|
||||
88
crates/egui_glow/src/shader_version.rs
Normal file
88
crates/egui_glow/src/shader_version.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use std::convert::TryInto;
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
#[allow(dead_code)]
|
||||
pub(crate) enum ShaderVersion {
|
||||
Gl120,
|
||||
Gl140,
|
||||
Es100,
|
||||
Es300,
|
||||
}
|
||||
|
||||
impl ShaderVersion {
|
||||
pub(crate) fn get(gl: &glow::Context) -> Self {
|
||||
use glow::HasContext as _;
|
||||
let shading_lang_string =
|
||||
unsafe { gl.get_parameter_string(glow::SHADING_LANGUAGE_VERSION) };
|
||||
let shader_version = Self::parse(&shading_lang_string);
|
||||
tracing::debug!(
|
||||
"Shader version: {:?} ({:?}).",
|
||||
shader_version,
|
||||
shading_lang_string
|
||||
);
|
||||
shader_version
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn parse(glsl_ver: &str) -> Self {
|
||||
let start = glsl_ver.find(|c| char::is_ascii_digit(&c)).unwrap();
|
||||
let es = glsl_ver[..start].contains(" ES ");
|
||||
let ver = glsl_ver[start..]
|
||||
.split_once(' ')
|
||||
.map_or(&glsl_ver[start..], |x| x.0);
|
||||
let [maj, min]: [u8; 2] = ver
|
||||
.splitn(3, '.')
|
||||
.take(2)
|
||||
.map(|x| x.parse().unwrap_or_default())
|
||||
.collect::<Vec<u8>>()
|
||||
.try_into()
|
||||
.unwrap();
|
||||
if es {
|
||||
if maj >= 3 {
|
||||
Self::Es300
|
||||
} else {
|
||||
Self::Es100
|
||||
}
|
||||
} else if maj > 1 || (maj == 1 && min >= 40) {
|
||||
Self::Gl140
|
||||
} else {
|
||||
Self::Gl120
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn version(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Gl120 => "#version 120\n",
|
||||
Self::Gl140 => "#version 140\n",
|
||||
Self::Es100 => "#version 100\n",
|
||||
Self::Es300 => "#version 300 es\n",
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_new_shader_interface(&self) -> &'static str {
|
||||
match self {
|
||||
ShaderVersion::Es300 | ShaderVersion::Gl140 => "#define NEW_SHADER_INTERFACE\n",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_shader_version() {
|
||||
use ShaderVersion::{Es100, Es300, Gl120, Gl140};
|
||||
for (s, v) in [
|
||||
("1.2 OpenGL foo bar", Gl120),
|
||||
("3.0", Gl140),
|
||||
("0.0", Gl120),
|
||||
("OpenGL ES GLSL 3.00 (WebGL2)", Es300),
|
||||
("OpenGL ES GLSL 1.00 (WebGL)", Es100),
|
||||
("OpenGL ES GLSL ES 1.00 foo bar", Es100),
|
||||
("WebGL GLSL ES 3.00 foo bar", Es300),
|
||||
("WebGL GLSL ES 3.00", Es300),
|
||||
("WebGL GLSL ES 1.0 foo bar", Es100),
|
||||
] {
|
||||
assert_eq!(ShaderVersion::parse(s), v);
|
||||
}
|
||||
}
|
||||
154
crates/egui_glow/src/vao.rs
Normal file
154
crates/egui_glow/src/vao.rs
Normal file
@@ -0,0 +1,154 @@
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use glow::HasContext as _;
|
||||
|
||||
use crate::check_for_gl_error;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct BufferInfo {
|
||||
pub location: u32, //
|
||||
pub vector_size: i32,
|
||||
pub data_type: u32, //GL_FLOAT,GL_UNSIGNED_BYTE
|
||||
pub normalized: bool,
|
||||
pub stride: i32,
|
||||
pub offset: i32,
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/// Wrapper around either Emulated VAO or GL's VAO.
|
||||
pub(crate) struct VertexArrayObject {
|
||||
// If `None`, we emulate VAO:s.
|
||||
vao: Option<crate::glow::VertexArray>,
|
||||
vbo: glow::Buffer,
|
||||
buffer_infos: Vec<BufferInfo>,
|
||||
}
|
||||
|
||||
impl VertexArrayObject {
|
||||
#[allow(clippy::needless_pass_by_value)] // false positive
|
||||
pub(crate) unsafe fn new(
|
||||
gl: &glow::Context,
|
||||
vbo: glow::Buffer,
|
||||
buffer_infos: Vec<BufferInfo>,
|
||||
) -> Self {
|
||||
let vao = if supports_vao(gl) {
|
||||
let vao = gl.create_vertex_array().unwrap();
|
||||
check_for_gl_error!(gl, "create_vertex_array");
|
||||
|
||||
// Store state in the VAO:
|
||||
gl.bind_vertex_array(Some(vao));
|
||||
gl.bind_buffer(glow::ARRAY_BUFFER, Some(vbo));
|
||||
|
||||
for attribute in &buffer_infos {
|
||||
gl.vertex_attrib_pointer_f32(
|
||||
attribute.location,
|
||||
attribute.vector_size,
|
||||
attribute.data_type,
|
||||
attribute.normalized,
|
||||
attribute.stride,
|
||||
attribute.offset,
|
||||
);
|
||||
check_for_gl_error!(gl, "vertex_attrib_pointer_f32");
|
||||
gl.enable_vertex_attrib_array(attribute.location);
|
||||
check_for_gl_error!(gl, "enable_vertex_attrib_array");
|
||||
}
|
||||
|
||||
gl.bind_vertex_array(None);
|
||||
|
||||
Some(vao)
|
||||
} else {
|
||||
tracing::debug!("VAO not supported");
|
||||
None
|
||||
};
|
||||
|
||||
Self {
|
||||
vao,
|
||||
vbo,
|
||||
buffer_infos,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn bind(&self, gl: &glow::Context) {
|
||||
if let Some(vao) = self.vao {
|
||||
gl.bind_vertex_array(Some(vao));
|
||||
check_for_gl_error!(gl, "bind_vertex_array");
|
||||
} else {
|
||||
gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.vbo));
|
||||
check_for_gl_error!(gl, "bind_buffer");
|
||||
|
||||
for attribute in &self.buffer_infos {
|
||||
gl.vertex_attrib_pointer_f32(
|
||||
attribute.location,
|
||||
attribute.vector_size,
|
||||
attribute.data_type,
|
||||
attribute.normalized,
|
||||
attribute.stride,
|
||||
attribute.offset,
|
||||
);
|
||||
check_for_gl_error!(gl, "vertex_attrib_pointer_f32");
|
||||
gl.enable_vertex_attrib_array(attribute.location);
|
||||
check_for_gl_error!(gl, "enable_vertex_attrib_array");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn unbind(&self, gl: &glow::Context) {
|
||||
if self.vao.is_some() {
|
||||
gl.bind_vertex_array(None);
|
||||
} else {
|
||||
gl.bind_buffer(glow::ARRAY_BUFFER, None);
|
||||
for attribute in &self.buffer_infos {
|
||||
gl.disable_vertex_attrib_array(attribute.location);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
fn supports_vao(gl: &glow::Context) -> bool {
|
||||
const WEBGL_PREFIX: &str = "WebGL ";
|
||||
const OPENGL_ES_PREFIX: &str = "OpenGL ES ";
|
||||
|
||||
let version_string = unsafe { gl.get_parameter_string(glow::VERSION) };
|
||||
tracing::debug!("GL version: {:?}.", version_string);
|
||||
|
||||
// Examples:
|
||||
// * "WebGL 2.0 (OpenGL ES 3.0 Chromium)"
|
||||
// * "WebGL 2.0"
|
||||
|
||||
if let Some(pos) = version_string.rfind(WEBGL_PREFIX) {
|
||||
let version_str = &version_string[pos + WEBGL_PREFIX.len()..];
|
||||
if version_str.contains("1.0") {
|
||||
// need to test OES_vertex_array_object .
|
||||
let supported_extensions = gl.supported_extensions();
|
||||
tracing::debug!("Supported OpenGL extensions: {:?}", supported_extensions);
|
||||
supported_extensions.contains("OES_vertex_array_object")
|
||||
} else {
|
||||
true
|
||||
}
|
||||
} else if version_string.contains(OPENGL_ES_PREFIX) {
|
||||
// glow targets es2.0+ so we don't concern about OpenGL ES-CM,OpenGL ES-CL
|
||||
if version_string.contains("2.0") {
|
||||
// need to test OES_vertex_array_object .
|
||||
let supported_extensions = gl.supported_extensions();
|
||||
tracing::debug!("Supported OpenGL extensions: {:?}", supported_extensions);
|
||||
supported_extensions.contains("OES_vertex_array_object")
|
||||
} else {
|
||||
true
|
||||
}
|
||||
} else {
|
||||
// from OpenGL 3 vao into core
|
||||
if version_string.starts_with('2') {
|
||||
// I found APPLE_vertex_array_object , GL_ATI_vertex_array_object ,ARB_vertex_array_object
|
||||
// but APPLE's and ATI's very old extension.
|
||||
let supported_extensions = gl.supported_extensions();
|
||||
tracing::debug!("Supported OpenGL extensions: {:?}", supported_extensions);
|
||||
supported_extensions.contains("ARB_vertex_array_object")
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
94
crates/egui_glow/src/winit.rs
Normal file
94
crates/egui_glow/src/winit.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
pub use egui_winit;
|
||||
use egui_winit::winit;
|
||||
|
||||
/// Use [`egui`] from a [`glow`] app based on [`winit`].
|
||||
pub struct EguiGlow {
|
||||
pub egui_ctx: egui::Context,
|
||||
pub egui_winit: egui_winit::State,
|
||||
pub painter: crate::Painter,
|
||||
|
||||
shapes: Vec<egui::epaint::ClippedShape>,
|
||||
textures_delta: egui::TexturesDelta,
|
||||
}
|
||||
|
||||
impl EguiGlow {
|
||||
pub fn new<E>(
|
||||
event_loop: &winit::event_loop::EventLoopWindowTarget<E>,
|
||||
gl: std::sync::Arc<glow::Context>,
|
||||
) -> Self {
|
||||
let painter = crate::Painter::new(gl, None, "")
|
||||
.map_err(|error| {
|
||||
tracing::error!("error occurred in initializing painter:\n{}", error);
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
Self {
|
||||
egui_ctx: Default::default(),
|
||||
egui_winit: egui_winit::State::new(event_loop),
|
||||
painter,
|
||||
shapes: Default::default(),
|
||||
textures_delta: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if egui wants exclusive use of this event
|
||||
/// (e.g. a mouse click on an egui window, or entering text into a text field).
|
||||
/// For instance, if you use egui for a game, you want to first call this
|
||||
/// and only when this returns `false` pass on the events to your game.
|
||||
///
|
||||
/// Note that egui uses `tab` to move focus between elements, so this will always return `true` for tabs.
|
||||
pub fn on_event(&mut self, event: &winit::event::WindowEvent<'_>) -> bool {
|
||||
self.egui_winit.on_event(&self.egui_ctx, event)
|
||||
}
|
||||
|
||||
/// Returns the `Duration` of the timeout after which egui should be repainted even if there's no new events.
|
||||
///
|
||||
/// Call [`Self::paint`] later to paint.
|
||||
pub fn run(
|
||||
&mut self,
|
||||
window: &winit::window::Window,
|
||||
run_ui: impl FnMut(&egui::Context),
|
||||
) -> std::time::Duration {
|
||||
let raw_input = self.egui_winit.take_egui_input(window);
|
||||
let egui::FullOutput {
|
||||
platform_output,
|
||||
repaint_after,
|
||||
textures_delta,
|
||||
shapes,
|
||||
} = self.egui_ctx.run(raw_input, run_ui);
|
||||
|
||||
self.egui_winit
|
||||
.handle_platform_output(window, &self.egui_ctx, platform_output);
|
||||
|
||||
self.shapes = shapes;
|
||||
self.textures_delta.append(textures_delta);
|
||||
repaint_after
|
||||
}
|
||||
|
||||
/// Paint the results of the last call to [`Self::run`].
|
||||
pub fn paint(&mut self, window: &winit::window::Window) {
|
||||
let shapes = std::mem::take(&mut self.shapes);
|
||||
let mut textures_delta = std::mem::take(&mut self.textures_delta);
|
||||
|
||||
for (id, image_delta) in textures_delta.set {
|
||||
self.painter.set_texture(id, &image_delta);
|
||||
}
|
||||
|
||||
let clipped_primitives = self.egui_ctx.tessellate(shapes);
|
||||
let dimensions: [u32; 2] = window.inner_size().into();
|
||||
self.painter.paint_primitives(
|
||||
dimensions,
|
||||
self.egui_ctx.pixels_per_point(),
|
||||
&clipped_primitives,
|
||||
);
|
||||
|
||||
for id in textures_delta.free.drain(..) {
|
||||
self.painter.free_texture(id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Call to release the allocated graphics resources.
|
||||
pub fn destroy(&mut self) {
|
||||
self.painter.destroy();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user