From 82983884e0f4e35e506dc84b334dda0a8fa12c35 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 10 Mar 2022 14:55:29 +0100 Subject: [PATCH 1/7] Add Shape::Callback to do custom rendering inside of an egui UI --- CHANGELOG.md | 6 +- Cargo.lock | 2 + README.md | 4 +- eframe/Cargo.toml | 2 + eframe/examples/custom_3d.rs | 193 ++++++++++++++++++++++++++++++++++ egui/src/context.rs | 21 ++-- egui/src/introspection.rs | 6 +- egui/src/lib.rs | 10 +- egui_demo_lib/src/lib.rs | 11 +- egui_glium/src/lib.rs | 4 +- egui_glium/src/painter.rs | 25 +++-- egui_glow/src/epi_backend.rs | 4 +- egui_glow/src/painter.rs | 123 +++++++++++++++------- egui_glow/src/post_process.rs | 4 + egui_glow/src/winit.rs | 6 +- egui_web/src/backend.rs | 13 ++- egui_web/src/glow_wrapping.rs | 10 +- egui_web/src/lib.rs | 4 +- egui_web/src/painter.rs | 8 +- epaint/CHANGELOG.md | 1 + epaint/src/lib.rs | 22 ++-- epaint/src/shape.rs | 73 ++++++++++++- epaint/src/shape_transform.rs | 3 + epaint/src/stats.rs | 31 +++--- epaint/src/tessellator.rs | 110 +++++++++++++------ epi/src/lib.rs | 7 ++ 26 files changed, 555 insertions(+), 148 deletions(-) create mode 100644 eframe/examples/custom_3d.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index acfcfad0565..610143f1697 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,12 +7,14 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w ## Unreleased ### Added ⭐ -* Added `Frame::canvas` ([1362](https://github.com/emilk/egui/pull/1362)). +* Add `Shape::Callback` for backend-specific painting ([#1351](https://github.com/emilk/egui/pull/1351)). +* Added `Frame::canvas` ([#1362](https://github.com/emilk/egui/pull/1362)). ### Changed 🔧 +* `ClippedMesh` has been replaced with `ClippedPrimitive` ([#1351](https://github.com/emilk/egui/pull/1351)). ### Fixed 🐛 -* Fixed ComboBoxes always being rendered left-aligned ([1304](https://github.com/emilk/egui/pull/1304)). +* Fixed ComboBoxes always being rendered left-aligned ([#1304](https://github.com/emilk/egui/pull/1304)). ## 0.17.0 - 2022-02-22 - Improved font selection and image handling diff --git a/Cargo.lock b/Cargo.lock index 593823c2485..555503ef686 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -991,7 +991,9 @@ dependencies = [ "egui_web", "ehttp", "epi", + "glow", "image", + "parking_lot 0.12.0", "poll-promise", "rfd", ] diff --git a/README.md b/README.md index 83b4486e333..76f53ca82b5 100644 --- a/README.md +++ b/README.md @@ -204,9 +204,9 @@ loop { let full_output = egui_ctx.run(raw_input, |egui_ctx| { my_app.ui(egui_ctx); // add panels, windows and widgets to `egui_ctx` here }); - let clipped_meshes = egui_ctx.tessellate(full_output.shapes); // creates triangles to paint + let clipped_primitives = egui_ctx.tessellate(full_output.shapes); // creates triangles to paint - my_integration.paint(&full_output.textures_delta, clipped_meshes); + my_integration.paint(&full_output.textures_delta, clipped_primitives); let platform_output = full_output.platform_output; my_integration.set_cursor_icon(platform_output.cursor_icon); diff --git a/eframe/Cargo.toml b/eframe/Cargo.toml index df6cc289ddd..a502ce465cb 100644 --- a/eframe/Cargo.toml +++ b/eframe/Cargo.toml @@ -66,9 +66,11 @@ egui_web = { version = "0.17.0", path = "../egui_web", default-features = false # For examples: egui_extras = { path = "../egui_extras", features = ["image", "svg"] } ehttp = "0.2" +glow = "0.11" image = { version = "0.24", default-features = false, features = [ "jpeg", "png", ] } +parking_lot = "0.12" poll-promise = "0.1" rfd = "0.8" diff --git a/eframe/examples/custom_3d.rs b/eframe/examples/custom_3d.rs new file mode 100644 index 00000000000..53dd850e2fa --- /dev/null +++ b/eframe/examples/custom_3d.rs @@ -0,0 +1,193 @@ +//! This demo shows how to embed 3D rendering using [`glow`](https://github.com/grovesNL/glow) in `eframe`. +//! +//! This is very advanced usage, and you need to be careful. +//! +//! If you want an easier way to show 3D graphics with egui, take a look at: +//! * [`bevy_egui`](https://github.com/mvlabat/bevy_egui) +//! * [`three-d`](https://github.com/asny/three-d) + +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release + +use eframe::{egui, epi}; + +use parking_lot::Mutex; +use std::sync::Arc; + +#[derive(Default)] +struct MyApp { + rotating_triangle: Arc>>, + angle: f32, +} + +impl epi::App for MyApp { + fn name(&self) -> &str { + "Custom 3D painting inside an egui window" + } + + fn update(&mut self, ctx: &egui::Context, _frame: &epi::Frame) { + egui::CentralPanel::default().show(ctx, |ui| { + ui.heading("Here is some 3D stuff:"); + + egui::ScrollArea::both().show(ui, |ui| { + egui::Frame::dark_canvas(ui.style()).show(ui, |ui| { + self.custom_painting(ui); + }); + ui.label("Drag to rotate!"); + }); + }); + + let mut frame = egui::Frame::window(&*ctx.style()); + frame.fill = frame.fill.linear_multiply(0.5); // transparent + egui::Window::new("3D stuff in a window") + .frame(frame) + .show(ctx, |ui| { + self.custom_painting(ui); + }); + } +} + +impl MyApp { + fn custom_painting(&mut self, ui: &mut egui::Ui) { + let (rect, response) = + ui.allocate_exact_size(egui::Vec2::splat(256.0), egui::Sense::drag()); + + self.angle += response.drag_delta().x * 0.01; + + let angle = self.angle; + let rotating_triangle = self.rotating_triangle.clone(); + + let callback = egui::epaint::PaintCallback { + rect, + callback: std::sync::Arc::new(move |render_ctx| { + if let Some(gl) = render_ctx.downcast_ref::() { + let mut rotating_triangle = rotating_triangle.lock(); + let rotating_triangle = + rotating_triangle.get_or_insert_with(|| RotatingTriangle::new(gl)); + rotating_triangle.paint(gl, angle); + } else { + eprintln!("Can't do custom painting because we are not using a glow context"); + } + }), + }; + ui.painter().add(callback); + } +} + +struct RotatingTriangle { + program: glow::Program, + vertex_array: glow::VertexArray, +} + +impl RotatingTriangle { + fn new(gl: &glow::Context) -> Self { + use glow::HasContext as _; + + let shader_version = if cfg!(target_arch = "wasm32") { + "#version 300 es" + } else { + "#version 410" + }; + + unsafe { + let program = gl.create_program().expect("Cannot create program"); + + let (vertex_shader_source, fragment_shader_source) = ( + r#" + const vec2 verts[3] = vec2[3]( + vec2(0.0, 1.0), + vec2(-1.0, -1.0), + vec2(1.0, -1.0) + ); + const vec4 colors[3] = vec4[3]( + vec4(1.0, 0.0, 0.0, 1.0), + vec4(0.0, 1.0, 0.0, 1.0), + vec4(0.0, 0.0, 1.0, 1.0) + ); + out vec4 v_color; + uniform float u_angle; + void main() { + v_color = colors[gl_VertexID]; + gl_Position = vec4(verts[gl_VertexID], 0.0, 1.0); + gl_Position.x *= cos(u_angle); + } + "#, + r#" + precision mediump float; + in vec4 v_color; + out vec4 out_color; + void main() { + out_color = v_color; + } + "#, + ); + + let shader_sources = [ + (glow::VERTEX_SHADER, vertex_shader_source), + (glow::FRAGMENT_SHADER, fragment_shader_source), + ]; + + let shaders: Vec<_> = shader_sources + .iter() + .map(|(shader_type, shader_source)| { + let shader = gl + .create_shader(*shader_type) + .expect("Cannot create shader"); + gl.shader_source(shader, &format!("{}\n{}", shader_version, shader_source)); + gl.compile_shader(shader); + if !gl.get_shader_compile_status(shader) { + panic!("{}", gl.get_shader_info_log(shader)); + } + gl.attach_shader(program, shader); + shader + }) + .collect(); + + gl.link_program(program); + if !gl.get_program_link_status(program) { + panic!("{}", gl.get_program_info_log(program)); + } + + for shader in shaders { + gl.detach_shader(program, shader); + gl.delete_shader(shader); + } + + let vertex_array = gl + .create_vertex_array() + .expect("Cannot create vertex array"); + + Self { + program, + vertex_array, + } + } + } + + // TODO: figure out how to call this in a nice way + #[allow(unused)] + fn destroy(self, gl: &glow::Context) { + use glow::HasContext as _; + unsafe { + gl.delete_program(self.program); + gl.delete_vertex_array(self.vertex_array); + } + } + + fn paint(&self, gl: &glow::Context, angle: f32) { + use glow::HasContext as _; + unsafe { + gl.use_program(Some(self.program)); + gl.uniform_1_f32( + gl.get_uniform_location(self.program, "u_angle").as_ref(), + angle, + ); + gl.bind_vertex_array(Some(self.vertex_array)); + gl.draw_arrays(glow::TRIANGLES, 0, 3); + } + } +} + +fn main() { + let options = eframe::NativeOptions::default(); + eframe::run_native(Box::new(MyApp::default()), options); +} diff --git a/egui/src/context.rs b/egui/src/context.rs index c0c261fc5d8..fef366f68aa 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -122,7 +122,7 @@ impl ContextImpl { /// /// ``` no_run /// # fn handle_platform_output(_: egui::PlatformOutput) {} -/// # fn paint(textures_detla: egui::TexturesDelta, _: Vec) {} +/// # fn paint(textures_detla: egui::TexturesDelta, _: Vec) {} /// let mut ctx = egui::Context::default(); /// /// // Game loop: @@ -137,8 +137,8 @@ impl ContextImpl { /// }); /// }); /// handle_platform_output(full_output.platform_output); -/// let clipped_meshes = ctx.tessellate(full_output.shapes); // create triangles to paint -/// paint(full_output.textures_delta, clipped_meshes); +/// let clipped_primitives = ctx.tessellate(full_output.shapes); // create triangles to paint +/// paint(full_output.textures_delta, clipped_primitives); /// } /// ``` #[derive(Clone)] @@ -773,7 +773,7 @@ impl Context { } /// Tessellate the given shapes into triangle meshes. - pub fn tessellate(&self, shapes: Vec) -> Vec { + pub fn tessellate(&self, shapes: Vec) -> Vec { // A tempting optimization is to reuse the tessellation from last frame if the // shapes are the same, but just comparing the shapes takes about 50% of the time // it takes to tessellate them, so it is not a worth optimization. @@ -782,13 +782,13 @@ impl Context { tessellation_options.pixels_per_point = self.pixels_per_point(); tessellation_options.aa_size = 1.0 / self.pixels_per_point(); let paint_stats = PaintStats::from_shapes(&shapes); - let clipped_meshes = tessellator::tessellate_shapes( + let clipped_primitives = tessellator::tessellate_shapes( shapes, tessellation_options, self.fonts().font_image_size(), ); - self.write().paint_stats = paint_stats.with_clipped_meshes(&clipped_meshes); - clipped_meshes + self.write().paint_stats = paint_stats.with_clipped_primitives(&clipped_primitives); + clipped_primitives } // --------------------------------------------------------------------- @@ -1246,3 +1246,10 @@ impl Context { self.set_style(style); } } + +#[cfg(test)] +#[test] +fn context_impl_send_sync() { + fn assert_send_sync() {} + assert_send_sync::(); +} diff --git a/egui/src/introspection.rs b/egui/src/introspection.rs index e5f814cc957..e5b9c7ea168 100644 --- a/egui/src/introspection.rs +++ b/egui/src/introspection.rs @@ -91,9 +91,10 @@ impl Widget for &epaint::stats::PaintStats { shape_path, shape_mesh, shape_vec, + num_callbacks, text_shape_vertices, text_shape_indices, - clipped_meshes, + clipped_primitives, vertices, indices, } = self; @@ -104,6 +105,7 @@ impl Widget for &epaint::stats::PaintStats { label(ui, shape_path, "paths"); label(ui, shape_mesh, "nested meshes"); label(ui, shape_vec, "nested shapes"); + ui.label(format!("{} callbacks", num_callbacks)); ui.add_space(10.0); ui.label("Text shapes:"); @@ -113,7 +115,7 @@ impl Widget for &epaint::stats::PaintStats { ui.add_space(10.0); ui.label("Tessellated (and culled):"); - label(ui, clipped_meshes, "clipped_meshes") + label(ui, clipped_primitives, "clipped_primitives") .on_hover_text("Number of separate clip rectangles"); label(ui, vertices, "vertices"); label(ui, indices, "indices").on_hover_text("Three 32-bit indices per triangles"); diff --git a/egui/src/lib.rs b/egui/src/lib.rs index 8c05fc1a076..07e283e42ca 100644 --- a/egui/src/lib.rs +++ b/egui/src/lib.rs @@ -112,7 +112,7 @@ //! ``` no_run //! # fn handle_platform_output(_: egui::PlatformOutput) {} //! # fn gather_input() -> egui::RawInput { egui::RawInput::default() } -//! # fn paint(textures_detla: egui::TexturesDelta, _: Vec) {} +//! # fn paint(textures_detla: egui::TexturesDelta, _: Vec) {} //! let mut ctx = egui::Context::default(); //! //! // Game loop: @@ -128,8 +128,8 @@ //! }); //! }); //! handle_platform_output(full_output.platform_output); -//! let clipped_meshes = ctx.tessellate(full_output.shapes); // create triangles to paint -//! paint(full_output.textures_delta, clipped_meshes); +//! let clipped_primitives = ctx.tessellate(full_output.shapes); // create triangles to paint +//! paint(full_output.textures_delta, clipped_primitives); //! } //! ``` //! @@ -386,8 +386,8 @@ pub use epaint::{ color, mutex, text::{FontData, FontDefinitions, FontFamily, FontId, FontTweak}, textures::TexturesDelta, - AlphaImage, ClippedMesh, Color32, ColorImage, ImageData, Mesh, Rgba, Rounding, Shape, Stroke, - TextureHandle, TextureId, + AlphaImage, ClippedPrimitive, Color32, ColorImage, ImageData, Mesh, Rgba, Rounding, Shape, + Stroke, TextureHandle, TextureId, }; pub mod text { diff --git a/egui_demo_lib/src/lib.rs b/egui_demo_lib/src/lib.rs index 96112481669..ca15d3482f6 100644 --- a/egui_demo_lib/src/lib.rs +++ b/egui_demo_lib/src/lib.rs @@ -148,8 +148,8 @@ fn test_egui_e2e() { let full_output = ctx.run(raw_input.clone(), |ctx| { demo_windows.ui(ctx); }); - let clipped_meshes = ctx.tessellate(full_output.shapes); - assert!(!clipped_meshes.is_empty()); + let clipped_primitives = ctx.tessellate(full_output.shapes); + assert!(!clipped_primitives.is_empty()); } } @@ -167,8 +167,11 @@ fn test_egui_zero_window_size() { let full_output = ctx.run(raw_input.clone(), |ctx| { demo_windows.ui(ctx); }); - let clipped_meshes = ctx.tessellate(full_output.shapes); - assert!(clipped_meshes.is_empty(), "There should be nothing to show"); + let clipped_primitives = ctx.tessellate(full_output.shapes); + assert!( + clipped_primitives.is_empty(), + "There should be nothing to show" + ); } } diff --git a/egui_glium/src/lib.rs b/egui_glium/src/lib.rs index db6bf66a1c8..ce5b67864f5 100644 --- a/egui_glium/src/lib.rs +++ b/egui_glium/src/lib.rs @@ -159,12 +159,12 @@ impl EguiGlium { pub fn paint(&mut self, display: &glium::Display, target: &mut T) { let shapes = std::mem::take(&mut self.shapes); let textures_delta = std::mem::take(&mut self.textures_delta); - let clipped_meshes = self.egui_ctx.tessellate(shapes); + let clipped_primitives = self.egui_ctx.tessellate(shapes); self.painter.paint_and_update_textures( display, target, self.egui_ctx.pixels_per_point(), - clipped_meshes, + clipped_primitives, &textures_delta, ); } diff --git a/egui_glium/src/painter.rs b/egui_glium/src/painter.rs index 5299a11f5b1..0f779c5f3d3 100644 --- a/egui_glium/src/painter.rs +++ b/egui_glium/src/painter.rs @@ -1,6 +1,8 @@ #![allow(deprecated)] // legacy implement_vertex macro #![allow(semicolon_in_expressions_from_macros)] // glium::program! macro +use egui::epaint::Primitive; + use { ahash::AHashMap, egui::{emath::Rect, epaint::Mesh}, @@ -68,14 +70,14 @@ impl Painter { display: &glium::Display, target: &mut T, pixels_per_point: f32, - clipped_meshes: Vec, + clipped_primitives: Vec, textures_delta: &egui::TexturesDelta, ) { for (id, image_delta) in &textures_delta.set { self.set_texture(display, *id, image_delta); } - self.paint_meshes(display, target, pixels_per_point, clipped_meshes); + self.paint_primitives(display, target, pixels_per_point, clipped_primitives); for &id in &textures_delta.free { self.free_texture(id); @@ -85,15 +87,26 @@ impl Painter { /// Main entry-point for painting a frame. /// You should call `target.clear_color(..)` before /// and `target.finish()` after this. - pub fn paint_meshes( + pub fn paint_primitives( &mut self, display: &glium::Display, target: &mut T, pixels_per_point: f32, - clipped_meshes: Vec, + clipped_primitives: Vec, ) { - for egui::ClippedMesh(clip_rect, mesh) in clipped_meshes { - self.paint_mesh(target, display, pixels_per_point, clip_rect, &mesh); + for egui::ClippedPrimitive { + clip_rect, + primitive, + } in clipped_primitives + { + match primitive { + Primitive::Mesh(mesh) => { + self.paint_mesh(target, display, pixels_per_point, clip_rect, &mesh); + } + Primitive::Callback(_) => { + panic!("Custom rendering callbacks are not implemented in egui_glium"); + } + } } } diff --git a/egui_glow/src/epi_backend.rs b/egui_glow/src/epi_backend.rs index 878a5e1cba6..9a9efdcc1f5 100644 --- a/egui_glow/src/epi_backend.rs +++ b/egui_glow/src/epi_backend.rs @@ -92,7 +92,7 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { integration.handle_platform_output(gl_window.window(), platform_output); - let clipped_meshes = integration.egui_ctx.tessellate(shapes); + let clipped_primitives = integration.egui_ctx.tessellate(shapes); // paint: { @@ -107,7 +107,7 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { &gl, gl_window.window().inner_size().into(), integration.egui_ctx.pixels_per_point(), - clipped_meshes, + clipped_primitives, &textures_delta, ); diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index c8c59b0b066..165b2ffafb5 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use egui::{ emath::Rect, - epaint::{Color32, Mesh, Vertex}, + epaint::{Color32, Mesh, Primitive, Vertex}, }; use glow::HasContext; use memoffset::offset_of; @@ -275,14 +275,14 @@ impl Painter { gl: &glow::Context, inner_size: [u32; 2], pixels_per_point: f32, - clipped_meshes: Vec, + clipped_primitives: Vec, textures_delta: &egui::TexturesDelta, ) { for (id, image_delta) in &textures_delta.set { self.set_texture(gl, *id, image_delta); } - self.paint_meshes(gl, inner_size, pixels_per_point, clipped_meshes); + self.paint_primitives(gl, inner_size, pixels_per_point, clipped_primitives); for &id in &textures_delta.free { self.free_texture(gl, id); @@ -308,12 +308,12 @@ impl Painter { /// /// 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( + pub fn paint_primitives( &mut self, gl: &glow::Context, inner_size: [u32; 2], pixels_per_point: f32, - clipped_meshes: Vec, + clipped_primitives: Vec, ) { self.assert_not_destroyed(); @@ -323,8 +323,52 @@ impl Painter { } } let size_in_pixels = unsafe { self.prepare_painting(inner_size, gl, pixels_per_point) }; - for egui::ClippedMesh(clip_rect, mesh) in clipped_meshes { - self.paint_mesh(gl, size_in_pixels, pixels_per_point, clip_rect, &mesh); + + for egui::ClippedPrimitive { + clip_rect, + primitive, + } in clipped_primitives + { + set_clip_rect(gl, size_in_pixels, pixels_per_point, clip_rect); + + match primitive { + Primitive::Mesh(mesh) => { + self.paint_mesh(gl, &mesh); + } + Primitive::Callback(callback) => { + if callback.rect.is_positive() { + // 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 { + 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, + ); + } + + callback.call(gl); + + // Restore state: + unsafe { + if let Some(ref mut post_process) = self.post_process { + post_process.bind(gl); + } + self.prepare_painting(inner_size, gl, pixels_per_point) + }; + } + } + } } unsafe { self.vertex_array.unbind_vertex_array(gl); @@ -341,14 +385,7 @@ impl Painter { } #[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, - ) { + fn paint_mesh(&mut self, gl: &glow::Context, mesh: &Mesh) { debug_assert!(mesh.is_valid()); if let Some(texture) = self.get_texture(mesh.texture_id) { unsafe { @@ -369,30 +406,7 @@ impl Painter { 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, @@ -626,3 +640,36 @@ impl epi::NativeTexture for Painter { } } } + +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; + + // 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, + ); + } +} diff --git a/egui_glow/src/post_process.rs b/egui_glow/src/post_process.rs index e12c1a0e28f..569f54364b4 100644 --- a/egui_glow/src/post_process.rs +++ b/egui_glow/src/post_process.rs @@ -190,6 +190,10 @@ impl PostProcess { gl.clear(glow::COLOR_BUFFER_BIT); } + pub(crate) unsafe fn bind(&self, gl: &glow::Context) { + gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.fbo)); + } + pub(crate) unsafe fn end(&self, gl: &glow::Context) { gl.bind_framebuffer(glow::FRAMEBUFFER, None); gl.disable(glow::SCISSOR_TEST); diff --git a/egui_glow/src/winit.rs b/egui_glow/src/winit.rs index 8be08c88197..835e6a3804d 100644 --- a/egui_glow/src/winit.rs +++ b/egui_glow/src/winit.rs @@ -71,13 +71,13 @@ impl EguiGlow { self.painter.set_texture(gl, id, &image_delta); } - let clipped_meshes = self.egui_ctx.tessellate(shapes); + let clipped_primitives = self.egui_ctx.tessellate(shapes); let dimensions: [u32; 2] = window.inner_size().into(); - self.painter.paint_meshes( + self.painter.paint_primitives( gl, dimensions, self.egui_ctx.pixels_per_point(), - clipped_meshes, + clipped_primitives, ); for id in textures_delta.free.drain(..) { diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index ec11a18d8ab..e4292907451 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -245,7 +245,7 @@ impl AppRunner { /// Returns `true` if egui requests a repaint. /// /// Call [`Self::paint`] later to paint - pub fn logic(&mut self) -> Result<(bool, Vec), JsValue> { + pub fn logic(&mut self) -> Result<(bool, Vec), JsValue> { let frame_start = now_sec(); resize_canvas_to_screen_size(self.canvas_id(), self.app.max_size_points()); @@ -264,7 +264,7 @@ impl AppRunner { self.handle_platform_output(platform_output); self.textures_delta.append(textures_delta); - let clipped_meshes = self.egui_ctx.tessellate(shapes); + let clipped_primitives = self.egui_ctx.tessellate(shapes); { let app_output = self.frame.take_app_output(); @@ -278,17 +278,20 @@ impl AppRunner { } self.frame.lock().info.cpu_usage = Some((now_sec() - frame_start) as f32); - Ok((needs_repaint, clipped_meshes)) + Ok((needs_repaint, clipped_primitives)) } /// Paint the results of the last call to [`Self::logic`]. - pub fn paint(&mut self, clipped_meshes: Vec) -> Result<(), JsValue> { + pub fn paint( + &mut self, + clipped_primitives: Vec, + ) -> Result<(), JsValue> { let textures_delta = std::mem::take(&mut self.textures_delta); self.painter.clear(self.app.clear_color()); self.painter.paint_and_update_textures( - clipped_meshes, + clipped_primitives, self.egui_ctx.pixels_per_point(), &textures_delta, )?; diff --git a/egui_web/src/glow_wrapping.rs b/egui_web/src/glow_wrapping.rs index c21a38b710c..47fb3dd6868 100644 --- a/egui_web/src/glow_wrapping.rs +++ b/egui_web/src/glow_wrapping.rs @@ -1,4 +1,4 @@ -use egui::{ClippedMesh, Rgba}; +use egui::{ClippedPrimitive, Rgba}; use egui_glow::glow; use wasm_bindgen::JsCast; use wasm_bindgen::JsValue; @@ -58,17 +58,17 @@ impl crate::WebPainter for WrappedGlowPainter { egui_glow::painter::clear(&self.glow_ctx, canvas_dimension, clear_color) } - fn paint_meshes( + fn paint_primitives( &mut self, - clipped_meshes: Vec, + clipped_primitives: Vec, pixels_per_point: f32, ) -> Result<(), JsValue> { let canvas_dimension = [self.canvas.width(), self.canvas.height()]; - self.painter.paint_meshes( + self.painter.paint_primitives( &self.glow_ctx, canvas_dimension, pixels_per_point, - clipped_meshes, + clipped_primitives, ); Ok(()) } diff --git a/egui_web/src/lib.rs b/egui_web/src/lib.rs index 4177ee8ba16..b4bf6f2fff2 100644 --- a/egui_web/src/lib.rs +++ b/egui_web/src/lib.rs @@ -349,8 +349,8 @@ fn paint_and_schedule(runner_ref: &AppRunnerRef, panicked: Arc) -> R fn paint_if_needed(runner_ref: &AppRunnerRef) -> Result<(), JsValue> { let mut runner_lock = runner_ref.lock(); if runner_lock.needs_repaint.fetch_and_clear() { - let (needs_repaint, clipped_meshes) = runner_lock.logic()?; - runner_lock.paint(clipped_meshes)?; + let (needs_repaint, clipped_primitives) = runner_lock.logic()?; + runner_lock.paint(clipped_primitives)?; if needs_repaint { runner_lock.needs_repaint.set_true(); } diff --git a/egui_web/src/painter.rs b/egui_web/src/painter.rs index 11fcd4dd4ae..90e0b6e4c1b 100644 --- a/egui_web/src/painter.rs +++ b/egui_web/src/painter.rs @@ -16,15 +16,15 @@ pub trait WebPainter { fn clear(&mut self, clear_color: egui::Rgba); - fn paint_meshes( + fn paint_primitives( &mut self, - clipped_meshes: Vec, + clipped_primitives: Vec, pixels_per_point: f32, ) -> Result<(), JsValue>; fn paint_and_update_textures( &mut self, - clipped_meshes: Vec, + clipped_primitives: Vec, pixels_per_point: f32, textures_delta: &egui::TexturesDelta, ) -> Result<(), JsValue> { @@ -32,7 +32,7 @@ pub trait WebPainter { self.set_texture(*id, image_delta); } - self.paint_meshes(clipped_meshes, pixels_per_point)?; + self.paint_primitives(clipped_primitives, pixels_per_point)?; for &id in &textures_delta.free { self.free_texture(id); diff --git a/epaint/CHANGELOG.md b/epaint/CHANGELOG.md index eb83894ce92..9a3007048b1 100644 --- a/epaint/CHANGELOG.md +++ b/epaint/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to the epaint crate will be documented in this file. ## Unreleased +* Add `Shape::Callback` for backend-specific painting ([#1351](https://github.com/emilk/egui/pull/1351)). ## 0.17.0 - 2022-02-22 diff --git a/epaint/src/lib.rs b/epaint/src/lib.rs index 55e6b7b7672..e75d30265f5 100644 --- a/epaint/src/lib.rs +++ b/epaint/src/lib.rs @@ -110,7 +110,7 @@ pub use { image::{AlphaImage, ColorImage, ImageData, ImageDelta}, mesh::{Mesh, Mesh16, Vertex}, shadow::Shadow, - shape::{CircleShape, PathShape, RectShape, Rounding, Shape, TextShape}, + shape::{CircleShape, PaintCallback, PathShape, RectShape, Rounding, Shape, TextShape}, stats::PaintStats, stroke::Stroke, tessellator::{tessellate_shapes, TessellationOptions, Tessellator}, @@ -166,18 +166,24 @@ pub struct ClippedShape( pub Shape, ); -/// A [`Mesh`] within a clip rectangle. +/// A [`Mesh`] or [`PaintCallback`] within a clip rectangle. /// /// Everything is using logical points. #[derive(Clone, Debug)] -#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -pub struct ClippedMesh( +pub struct ClippedPrimitive { /// Clip / scissor rectangle. /// Only show the part of the [`Mesh`] that falls within this. - pub emath::Rect, - /// The shape - pub Mesh, -); + pub clip_rect: emath::Rect, + /// What to paint - either a [`Mesh`] or a [`PaintCallback`]. + pub primitive: Primitive, +} + +/// A rendering primitive - either a [`Mesh`] or a [`PaintCallback`]. +#[derive(Clone, Debug)] +pub enum Primitive { + Mesh(Mesh), + Callback(PaintCallback), +} // ---------------------------------------------------------------------------- diff --git a/epaint/src/shape.rs b/epaint/src/shape.rs index 40c9b3fe1b9..7154675395f 100644 --- a/epaint/src/shape.rs +++ b/epaint/src/shape.rs @@ -1,10 +1,13 @@ +//! The different shapes that can be painted. + use crate::{ text::{FontId, Fonts, Galley}, Color32, Mesh, Stroke, }; -use crate::{CubicBezierShape, QuadraticBezierShape}; use emath::*; +pub use crate::{CubicBezierShape, QuadraticBezierShape}; + /// A paint primitive such as a circle or a piece of text. /// Coordinates are all screen space points (not physical pixels). #[must_use = "Add a Shape to a Painter"] @@ -29,6 +32,16 @@ pub enum Shape { Mesh(Mesh), QuadraticBezier(QuadraticBezierShape), CubicBezier(CubicBezierShape), + + /// Backend-specific painting. + Callback(PaintCallback), +} + +#[cfg(test)] +#[test] +fn shape_impl_send_sync() { + fn assert_send_sync() {} + assert_send_sync::(); } impl From> for Shape { @@ -196,6 +209,7 @@ impl Shape { Self::Mesh(mesh) => mesh.calc_bounds(), Self::QuadraticBezier(bezier) => bezier.visual_bounding_rect(), Self::CubicBezier(bezier) => bezier.visual_bounding_rect(), + Self::Callback(custom) => custom.rect, } } } @@ -252,6 +266,9 @@ impl Shape { *p += delta; } } + Shape::Callback(shape) => { + shape.rect = shape.rect.translate(delta); + } } } } @@ -616,3 +633,57 @@ fn dashes_from_line( position_on_segment -= segment_length; }); } + +// ---------------------------------------------------------------------------- + +/// If you want to paint some 3D shapes inside an egui region, you can use this. +/// +/// This is advanced usage, and is backend specific. +#[derive(Clone)] +pub struct PaintCallback { + /// Where to paint. + pub rect: Rect, + + /// Paint something custom using. + /// + /// The argument is the render context, and what it contains depends on the backend. + /// In `eframe` it will be a `glow::Context`. + /// + /// The rendering backend is responsible for first setting the active viewport to [`Self::rect`]. + /// The rendering backend is also responsible for restoring any state it needs, + /// such as the bound shader program and vertex array. + pub callback: std::sync::Arc, +} + +impl PaintCallback { + #[inline] + pub fn call(&self, render_ctx: &dyn std::any::Any) { + (self.callback)(render_ctx); + } +} + +impl std::fmt::Debug for PaintCallback { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CustomShape") + .field("rect", &self.rect) + .finish_non_exhaustive() + } +} + +impl std::cmp::PartialEq for PaintCallback { + fn eq(&self, other: &Self) -> bool { + // As I understand it, the problem this clippy tried to protect against can only happen + // if we do dynamic casts back and forth on the pointers, and we don't do that. + #[allow(clippy::vtable_address_comparisons)] + { + self.rect.eq(&other.rect) && std::sync::Arc::ptr_eq(&self.callback, &other.callback) + } + } +} + +impl From for Shape { + #[inline(always)] + fn from(shape: PaintCallback) -> Self { + Self::Callback(shape) + } +} diff --git a/epaint/src/shape_transform.rs b/epaint/src/shape_transform.rs index c033a8ee4b2..a4c060db74e 100644 --- a/epaint/src/shape_transform.rs +++ b/epaint/src/shape_transform.rs @@ -51,5 +51,8 @@ pub fn adjust_colors(shape: &mut Shape, adjust_color: &impl Fn(&mut Color32)) { adjust_color(&mut bezier.fill); adjust_color(&mut bezier.stroke.color); } + Shape::Callback(_) => { + // Can't tint user callback code + } } } diff --git a/epaint/src/stats.rs b/epaint/src/stats.rs index 322c0ddf13e..2316d905a32 100644 --- a/epaint/src/stats.rs +++ b/epaint/src/stats.rs @@ -162,12 +162,13 @@ pub struct PaintStats { pub shape_path: AllocInfo, pub shape_mesh: AllocInfo, pub shape_vec: AllocInfo, + pub num_callbacks: usize, pub text_shape_vertices: AllocInfo, pub text_shape_indices: AllocInfo, /// Number of separate clip rectangles - pub clipped_meshes: AllocInfo, + pub clipped_primitives: AllocInfo, pub vertices: AllocInfo, pub indices: AllocInfo, } @@ -215,27 +216,25 @@ impl PaintStats { Shape::Mesh(mesh) => { self.shape_mesh += AllocInfo::from_mesh(mesh); } + Shape::Callback(_) => { + self.num_callbacks += 1; + } } } - pub fn with_clipped_meshes(mut self, clipped_meshes: &[crate::ClippedMesh]) -> Self { - self.clipped_meshes += AllocInfo::from_slice(clipped_meshes); - for ClippedMesh(_, indices) in clipped_meshes { - self.vertices += AllocInfo::from_slice(&indices.vertices); - self.indices += AllocInfo::from_slice(&indices.indices); + pub fn with_clipped_primitives( + mut self, + clipped_primitives: &[crate::ClippedPrimitive], + ) -> Self { + self.clipped_primitives += AllocInfo::from_slice(clipped_primitives); + for clipped_primitive in clipped_primitives { + if let Primitive::Mesh(mesh) = &clipped_primitive.primitive { + self.vertices += AllocInfo::from_slice(&mesh.vertices); + self.indices += AllocInfo::from_slice(&mesh.indices); + } } self } - - // pub fn total(&self) -> AllocInfo { - // self.shapes - // + self.shape_text - // + self.shape_path - // + self.shape_mesh - // + self.clipped_meshes - // + self.vertices - // + self.indices - // } } fn megabytes(size: usize) -> String { diff --git a/epaint/src/tessellator.rs b/epaint/src/tessellator.rs index cc1c75fc190..f46af30dc69 100644 --- a/epaint/src/tessellator.rs +++ b/epaint/src/tessellator.rs @@ -781,6 +781,9 @@ impl Tessellator { self.tessellate_quadratic_bezier(quadratic_shape, out); } Shape::CubicBezier(cubic_shape) => self.tessellate_cubic_bezier(cubic_shape, out), + Shape::Callback(_) => { + panic!("Shape::Callback passed to Tessellator"); + } } } @@ -1046,58 +1049,97 @@ pub fn tessellate_shapes( shapes: Vec, options: TessellationOptions, tex_size: [usize; 2], -) -> Vec { +) -> Vec { let mut tessellator = Tessellator::from_options(options); - let mut clipped_meshes: Vec = Vec::default(); + let mut clipped_primitives: Vec = Vec::default(); - for ClippedShape(clip_rect, shape) in shapes { - if !clip_rect.is_positive() { + for ClippedShape(new_clip_rect, new_shape) in shapes { + if !new_clip_rect.is_positive() { continue; // skip empty clip rectangles } - let start_new_mesh = match clipped_meshes.last() { - None => true, - Some(cm) => cm.0 != clip_rect || cm.1.texture_id != shape.texture_id(), - }; + if let Shape::Callback(callback) = new_shape { + clipped_primitives.push(ClippedPrimitive { + clip_rect: new_clip_rect, + primitive: Primitive::Callback(callback), + }); + } else { + let start_new_mesh = match clipped_primitives.last() { + None => true, + Some(output_clipped_primitive) => { + output_clipped_primitive.clip_rect != new_clip_rect + || if let Primitive::Mesh(output_mesh) = &output_clipped_primitive.primitive + { + output_mesh.texture_id != new_shape.texture_id() + } else { + true + } + } + }; - if start_new_mesh { - clipped_meshes.push(ClippedMesh(clip_rect, Mesh::default())); - } + if start_new_mesh { + clipped_primitives.push(ClippedPrimitive { + clip_rect: new_clip_rect, + primitive: Primitive::Mesh(Mesh::default()), + }); + } - let out = &mut clipped_meshes.last_mut().unwrap().1; - tessellator.clip_rect = clip_rect; - tessellator.tessellate_shape(tex_size, shape, out); - } + let out = clipped_primitives.last_mut().unwrap(); - if options.debug_paint_clip_rects { - for ClippedMesh(clip_rect, mesh) in &mut clipped_meshes { - if mesh.texture_id == TextureId::default() { - tessellator.clip_rect = Rect::EVERYTHING; - tessellator.tessellate_shape( - tex_size, - Shape::rect_stroke( - *clip_rect, - 0.0, - Stroke::new(2.0, Color32::from_rgb(150, 255, 150)), - ), - mesh, - ); + if let Primitive::Mesh(out_mesh) = &mut out.primitive { + tessellator.clip_rect = new_clip_rect; + tessellator.tessellate_shape(tex_size, new_shape, out_mesh); } else { - // TODO: create a new `ClippedMesh` just for the painted clip rectangle + unreachable!(); } } } + if options.debug_paint_clip_rects { + clipped_primitives = add_clip_rects(&mut tessellator, tex_size, clipped_primitives); + } + if options.debug_ignore_clip_rects { - for ClippedMesh(clip_rect, _) in &mut clipped_meshes { - *clip_rect = Rect::EVERYTHING; + for clipped_primitive in &mut clipped_primitives { + clipped_primitive.clip_rect = Rect::EVERYTHING; } } - for ClippedMesh(_, mesh) in &clipped_meshes { - crate::epaint_assert!(mesh.is_valid(), "Tessellator generated invalid Mesh"); + for clipped_primitive in &clipped_primitives { + if let Primitive::Mesh(mesh) = &clipped_primitive.primitive { + crate::epaint_assert!(mesh.is_valid(), "Tessellator generated invalid Mesh"); + } } - clipped_meshes + clipped_primitives +} + +fn add_clip_rects( + tessellator: &mut Tessellator, + tex_size: [usize; 2], + clipped_primitives: Vec, +) -> Vec { + tessellator.clip_rect = Rect::EVERYTHING; + let stroke = Stroke::new(2.0, Color32::from_rgb(150, 255, 150)); + + clipped_primitives + .into_iter() + .flat_map(|clipped_primitive| { + let mut clip_rect_mesh = Mesh::default(); + tessellator.tessellate_shape( + tex_size, + Shape::rect_stroke(clipped_primitive.clip_rect, 0.0, stroke), + &mut clip_rect_mesh, + ); + + [ + clipped_primitive, + ClippedPrimitive { + clip_rect: Rect::EVERYTHING, // whatever + primitive: Primitive::Mesh(clip_rect_mesh), + }, + ] + }) + .collect() } diff --git a/epi/src/lib.rs b/epi/src/lib.rs index ecda8514c93..88171aaac84 100644 --- a/epi/src/lib.rs +++ b/epi/src/lib.rs @@ -361,6 +361,13 @@ impl Frame { } } +#[cfg(test)] +#[test] +fn frame_impl_send_sync() { + fn assert_send_sync() {} + assert_send_sync::(); +} + /// Information about the web environment (if applicable). #[derive(Clone, Debug)] pub struct WebInfo { From 49ee891fd7ffb66b50434e10ddf160ef932e0025 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 11 Mar 2022 15:30:35 +0100 Subject: [PATCH 2/7] Use Rc everywhere --- egui_glow/examples/pure_glow.rs | 7 +- egui_glow/src/epi_backend.rs | 6 +- egui_glow/src/painter.rs | 183 ++++++++++++++++---------------- egui_glow/src/post_process.rs | 95 +++++++++-------- egui_glow/src/winit.rs | 13 ++- egui_web/src/glow_wrapping.rs | 29 +++-- 6 files changed, 167 insertions(+), 166 deletions(-) diff --git a/egui_glow/examples/pure_glow.rs b/egui_glow/examples/pure_glow.rs index a6196dbd6c3..870f412153a 100644 --- a/egui_glow/examples/pure_glow.rs +++ b/egui_glow/examples/pure_glow.rs @@ -43,8 +43,9 @@ fn main() { let event_loop = glutin::event_loop::EventLoop::with_user_event(); let (gl_window, gl) = create_display(&event_loop); + let gl = std::rc::Rc::new(gl); - let mut egui_glow = egui_glow::EguiGlow::new(gl_window.window(), &gl); + let mut egui_glow = egui_glow::EguiGlow::new(gl_window.window(), gl.clone()); event_loop.run(move |event, _, control_flow| { let mut redraw = || { @@ -78,7 +79,7 @@ fn main() { // draw things behind egui here - egui_glow.paint(gl_window.window(), &gl); + egui_glow.paint(gl_window.window()); // draw things on top of egui here @@ -108,7 +109,7 @@ fn main() { gl_window.window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead } glutin::event::Event::LoopDestroyed => { - egui_glow.destroy(&gl); + egui_glow.destroy(); } _ => (), diff --git a/egui_glow/src/epi_backend.rs b/egui_glow/src/epi_backend.rs index 9a9efdcc1f5..60b0c54cb3d 100644 --- a/egui_glow/src/epi_backend.rs +++ b/egui_glow/src/epi_backend.rs @@ -54,12 +54,13 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { egui_winit::epi::window_builder(native_options, &window_settings).with_title(app.name()); let event_loop = winit::event_loop::EventLoop::with_user_event(); let (gl_window, gl) = create_display(window_builder, &event_loop); + let gl = std::rc::Rc::new(gl); let repaint_signal = std::sync::Arc::new(GlowRepaintSignal(std::sync::Mutex::new( event_loop.create_proxy(), ))); - let mut painter = crate::Painter::new(&gl, None, "") + let mut painter = crate::Painter::new(gl.clone(), None, "") .unwrap_or_else(|error| panic!("some OpenGL error occurred {}\n", error)); let mut integration = egui_winit::epi::EpiIntegration::new( "egui_glow", @@ -104,7 +105,6 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { gl.clear(glow::COLOR_BUFFER_BIT); } painter.paint_and_update_textures( - &gl, gl_window.window().inner_size().into(), integration.egui_ctx.pixels_per_point(), clipped_primitives, @@ -153,7 +153,7 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { } winit::event::Event::LoopDestroyed => { integration.on_exit(gl_window.window()); - painter.destroy(&gl); + painter.destroy(); } winit::event::Event::UserEvent(RequestRepaintEvent) => { gl_window.window().request_redraw(); diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index 165b2ffafb5..8023b515e88 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -1,6 +1,6 @@ #![allow(unsafe_code)] -use std::collections::HashMap; +use std::{collections::HashMap, rc::Rc}; use egui::{ emath::Rect, @@ -24,6 +24,8 @@ const FRAG_SRC: &str = include_str!("shader/fragment.glsl"); /// 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: Rc, + max_texture_side: usize, program: glow::Program, @@ -86,16 +88,16 @@ impl Painter { /// * failed to create postprocess on webgl with `sRGB` support /// * failed to create buffer pub fn new( - gl: &glow::Context, + gl: Rc, pp_fb_extent: Option<[i32; 2]>, shader_prefix: &str, ) -> Result { - check_for_gl_error(gl, "before Painter::new"); + check_for_gl_error(&gl, "before Painter::new"); let max_texture_side = unsafe { gl.get_parameter_i32(glow::MAX_TEXTURE_SIZE) } as usize; - let support_vao = crate::misc_util::supports_vao(gl); - let shader_version = ShaderVersion::get(gl); + let support_vao = crate::misc_util::supports_vao(&gl); + let shader_version = ShaderVersion::get(&gl); let is_webgl_1 = shader_version == ShaderVersion::Es100; let header = shader_version.version(); tracing::debug!("Shader header: {:?}.", header); @@ -110,7 +112,7 @@ impl Painter { // install post process to correct sRGB color: ( Some(PostProcess::new( - gl, + gl.clone(), shader_prefix, support_vao, is_webgl_1, @@ -134,7 +136,7 @@ impl Painter { unsafe { let vert = compile_shader( - gl, + &gl, glow::VERTEX_SHADER, &format!( "{}\n{}\n{}\n{}", @@ -145,7 +147,7 @@ impl Painter { ), )?; let frag = compile_shader( - gl, + &gl, glow::FRAGMENT_SHADER, &format!( "{}\n{}\n{}\n{}\n{}", @@ -156,7 +158,7 @@ impl Painter { FRAG_SRC ), )?; - let program = link_program(gl, [vert, frag].iter())?; + let program = link_program(&gl, [vert, frag].iter())?; gl.detach_shader(program, vert); gl.detach_shader(program, frag); gl.delete_shader(vert); @@ -170,12 +172,12 @@ impl Painter { 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 mut vertex_array = if support_vao { - crate::misc_util::VAO::native(gl) + crate::misc_util::VAO::native(&gl) } else { crate::misc_util::VAO::emulated() }; - vertex_array.bind_vertex_array(gl); - vertex_array.bind_buffer(gl, &vertex_buffer); + vertex_array.bind_vertex_array(&gl); + vertex_array.bind_buffer(&gl, &vertex_buffer); let stride = std::mem::size_of::() as i32; let position_buffer_info = vao_emulate::BufferInfo { location: a_pos_loc, @@ -201,12 +203,13 @@ impl Painter { 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); - check_for_gl_error(gl, "after Painter::new"); + 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); + check_for_gl_error(&gl, "after Painter::new"); Ok(Painter { + gl, max_texture_side, program, u_screen_size, @@ -228,6 +231,11 @@ impl Painter { } } + /// Access the shared glow context. + pub fn gl(&self) -> &std::rc::Rc { + &self.gl + } + pub fn max_texture_side(&self) -> usize { self.max_texture_side } @@ -235,16 +243,15 @@ impl Painter { unsafe fn prepare_painting( &mut self, [width_in_pixels, height_in_pixels]: [u32; 2], - gl: &glow::Context, pixels_per_point: f32, ) -> (u32, u32) { - gl.enable(glow::SCISSOR_TEST); + self.gl.enable(glow::SCISSOR_TEST); // egui outputs mesh in both winding orders - gl.disable(glow::CULL_FACE); + self.gl.disable(glow::CULL_FACE); - gl.enable(glow::BLEND); - gl.blend_equation(glow::FUNC_ADD); - gl.blend_func_separate( + self.gl.enable(glow::BLEND); + self.gl.blend_equation(glow::FUNC_ADD); + self.gl.blend_func_separate( // egui outputs colors with premultiplied alpha: glow::ONE, glow::ONE_MINUS_SRC_ALPHA, @@ -257,35 +264,37 @@ impl Painter { 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)); + self.gl + .viewport(0, 0, width_in_pixels as i32, height_in_pixels as i32); + self.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); - self.vertex_array.bind_vertex_array(gl); + 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.vertex_array.bind_vertex_array(&self.gl); - gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.element_array_buffer)); + self.gl + .bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.element_array_buffer)); (width_in_pixels, height_in_pixels) } pub fn paint_and_update_textures( &mut self, - gl: &glow::Context, inner_size: [u32; 2], pixels_per_point: f32, clipped_primitives: Vec, textures_delta: &egui::TexturesDelta, ) { for (id, image_delta) in &textures_delta.set { - self.set_texture(gl, *id, image_delta); + self.set_texture(*id, image_delta); } - self.paint_primitives(gl, inner_size, pixels_per_point, clipped_primitives); + self.paint_primitives(inner_size, pixels_per_point, clipped_primitives); for &id in &textures_delta.free { - self.free_texture(gl, id); + self.free_texture(id); } } @@ -310,7 +319,6 @@ impl Painter { /// of the effects your program might have on this code. Look at the source if in doubt. pub fn paint_primitives( &mut self, - gl: &glow::Context, inner_size: [u32; 2], pixels_per_point: f32, clipped_primitives: Vec, @@ -319,21 +327,21 @@ impl Painter { 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); + post_process.begin(inner_size[0] as i32, inner_size[1] as i32); } } - let size_in_pixels = unsafe { self.prepare_painting(inner_size, gl, pixels_per_point) }; + let size_in_pixels = unsafe { self.prepare_painting(inner_size, pixels_per_point) }; for egui::ClippedPrimitive { clip_rect, primitive, } in clipped_primitives { - set_clip_rect(gl, size_in_pixels, pixels_per_point, clip_rect); + set_clip_rect(&self.gl, size_in_pixels, pixels_per_point, clip_rect); match primitive { Primitive::Mesh(mesh) => { - self.paint_mesh(gl, &mesh); + self.paint_mesh(&mesh); } Primitive::Callback(callback) => { if callback.rect.is_positive() { @@ -349,7 +357,7 @@ impl Painter { let rect_max_y = rect_max_y.round() as i32; unsafe { - gl.viewport( + self.gl.viewport( rect_min_x, size_in_pixels.1 as i32 - rect_max_y, rect_max_x - rect_min_x, @@ -357,57 +365,59 @@ impl Painter { ); } - callback.call(gl); + callback.call(&self.gl); // Restore state: unsafe { if let Some(ref mut post_process) = self.post_process { - post_process.bind(gl); + post_process.bind(); } - self.prepare_painting(inner_size, gl, pixels_per_point) + self.prepare_painting(inner_size, pixels_per_point) }; } } } } unsafe { - self.vertex_array.unbind_vertex_array(gl); - gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None); + self.vertex_array.unbind_vertex_array(&self.gl); + self.gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None); if let Some(ref post_process) = self.post_process { - post_process.end(gl); + post_process.end(); } - gl.disable(glow::SCISSOR_TEST); + self.gl.disable(glow::SCISSOR_TEST); - check_for_gl_error(gl, "painting"); + check_for_gl_error(&self.gl, "painting"); } } #[inline(never)] // Easier profiling - fn paint_mesh(&mut self, gl: &glow::Context, mesh: &Mesh) { + fn paint_mesh(&mut self, mesh: &Mesh) { debug_assert!(mesh.is_valid()); if let Some(texture) = self.get_texture(mesh.texture_id) { unsafe { - gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.vertex_buffer)); - gl.buffer_data_u8_slice( + self.gl + .bind_buffer(glow::ARRAY_BUFFER, Some(self.vertex_buffer)); + self.gl.buffer_data_u8_slice( glow::ARRAY_BUFFER, bytemuck::cast_slice(&mesh.vertices), glow::STREAM_DRAW, ); - gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.element_array_buffer)); - gl.buffer_data_u8_slice( + 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, ); - gl.bind_texture(glow::TEXTURE_2D, Some(texture)); + self.gl.bind_texture(glow::TEXTURE_2D, Some(texture)); } unsafe { - gl.draw_elements( + self.gl.draw_elements( glow::TRIANGLES, mesh.indices.len() as i32, glow::UNSIGNED_INT, @@ -425,20 +435,15 @@ impl Painter { // ------------------------------------------------------------------------ - pub fn set_texture( - &mut self, - gl: &glow::Context, - tex_id: egui::TextureId, - delta: &egui::epaint::ImageDelta, - ) { + pub fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta) { self.assert_not_destroyed(); let glow_texture = *self .textures .entry(tex_id) - .or_insert_with(|| unsafe { gl.create_texture().unwrap() }); + .or_insert_with(|| unsafe { self.gl.create_texture().unwrap() }); unsafe { - gl.bind_texture(glow::TEXTURE_2D, Some(glow_texture)); + self.gl.bind_texture(glow::TEXTURE_2D, Some(glow_texture)); } match &delta.image { @@ -451,7 +456,7 @@ impl Painter { let data: &[u8] = bytemuck::cast_slice(image.pixels.as_ref()); - self.upload_texture_srgb(gl, delta.pos, image.size, data); + self.upload_texture_srgb(delta.pos, image.size, data); } egui::ImageData::Alpha(image) => { assert_eq!( @@ -470,43 +475,37 @@ impl Painter { .flat_map(|a| a.to_array()) .collect(); - self.upload_texture_srgb(gl, delta.pos, image.size, &data); + self.upload_texture_srgb(delta.pos, image.size, &data); } }; } - fn upload_texture_srgb( - &mut self, - gl: &glow::Context, - pos: Option<[usize; 2]>, - [w, h]: [usize; 2], - data: &[u8], - ) { + fn upload_texture_srgb(&mut self, pos: Option<[usize; 2]>, [w, h]: [usize; 2], data: &[u8]) { assert_eq!(data.len(), w * h * 4); assert!(w >= 1 && h >= 1); unsafe { - gl.tex_parameter_i32( + self.gl.tex_parameter_i32( glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, self.texture_filter.glow_code() as i32, ); - gl.tex_parameter_i32( + self.gl.tex_parameter_i32( glow::TEXTURE_2D, glow::TEXTURE_MIN_FILTER, self.texture_filter.glow_code() as i32, ); - gl.tex_parameter_i32( + self.gl.tex_parameter_i32( glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::CLAMP_TO_EDGE as i32, ); - gl.tex_parameter_i32( + self.gl.tex_parameter_i32( glow::TEXTURE_2D, glow::TEXTURE_WRAP_T, glow::CLAMP_TO_EDGE as i32, ); - check_for_gl_error(gl, "tex_parameter"); + check_for_gl_error(&self.gl, "tex_parameter"); let (internal_format, src_format) = if self.is_webgl_1 { let format = if self.srgb_support { @@ -519,11 +518,11 @@ impl Painter { (glow::SRGB8_ALPHA8, glow::RGBA) }; - gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1); + self.gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1); let level = 0; if let Some([x, y]) = pos { - gl.tex_sub_image_2d( + self.gl.tex_sub_image_2d( glow::TEXTURE_2D, level, x as _, @@ -534,10 +533,10 @@ impl Painter { glow::UNSIGNED_BYTE, glow::PixelUnpackData::Slice(data), ); - check_for_gl_error(gl, "tex_sub_image_2d"); + check_for_gl_error(&self.gl, "tex_sub_image_2d"); } else { let border = 0; - gl.tex_image_2d( + self.gl.tex_image_2d( glow::TEXTURE_2D, level, internal_format as _, @@ -548,14 +547,14 @@ impl Painter { glow::UNSIGNED_BYTE, Some(data), ); - check_for_gl_error(gl, "tex_image_2d"); + check_for_gl_error(&self.gl, "tex_image_2d"); } } } - pub fn free_texture(&mut self, gl: &glow::Context, tex_id: egui::TextureId) { + pub fn free_texture(&mut self, tex_id: egui::TextureId) { if let Some(old_tex) = self.textures.remove(&tex_id) { - unsafe { gl.delete_texture(old_tex) }; + unsafe { self.gl.delete_texture(old_tex) }; } } @@ -563,27 +562,27 @@ impl Painter { self.textures.get(&texture_id).copied() } - unsafe fn destroy_gl(&self, gl: &glow::Context) { - gl.delete_program(self.program); + unsafe fn destroy_gl(&self) { + self.gl.delete_program(self.program); for tex in self.textures.values() { - gl.delete_texture(*tex); + self.gl.delete_texture(*tex); } - gl.delete_buffer(self.vertex_buffer); - gl.delete_buffer(self.element_array_buffer); + self.gl.delete_buffer(self.vertex_buffer); + self.gl.delete_buffer(self.element_array_buffer); for t in &self.textures_to_destroy { - gl.delete_texture(*t); + 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, gl: &glow::Context) { + pub fn destroy(&mut self) { if !self.destroyed { unsafe { - self.destroy_gl(gl); + self.destroy_gl(); if let Some(ref post_process) = self.post_process { - post_process.destroy(gl); + post_process.destroy(); } } self.destroyed = true; diff --git a/egui_glow/src/post_process.rs b/egui_glow/src/post_process.rs index 569f54364b4..0706b2a609d 100644 --- a/egui_glow/src/post_process.rs +++ b/egui_glow/src/post_process.rs @@ -6,6 +6,7 @@ use glow::HasContext; /// 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::rc::Rc, pos_buffer: glow::Buffer, index_buffer: glow::Buffer, vertex_array: crate::misc_util::VAO, @@ -18,7 +19,7 @@ pub(crate) struct PostProcess { impl PostProcess { pub(crate) unsafe fn new( - gl: &glow::Context, + gl: std::rc::Rc, shader_prefix: &str, need_to_emulate_vao: bool, is_webgl_1: bool, @@ -76,7 +77,7 @@ impl PostProcess { glow::UNSIGNED_BYTE, None, ); - check_for_gl_error(gl, "post process texture initialization"); + check_for_gl_error(&gl, "post process texture initialization"); gl.framebuffer_texture_2d( glow::FRAMEBUFFER, @@ -89,7 +90,7 @@ impl PostProcess { gl.bind_framebuffer(glow::FRAMEBUFFER, None); let vert_shader = compile_shader( - gl, + &gl, glow::VERTEX_SHADER, &format!( "{}\n{}", @@ -98,7 +99,7 @@ impl PostProcess { ), )?; let frag_shader = compile_shader( - gl, + &gl, glow::FRAGMENT_SHADER, &format!( "{}\n{}", @@ -106,7 +107,7 @@ impl PostProcess { include_str!("shader/post_fragment_100es.glsl") ), )?; - let program = link_program(gl, [vert_shader, frag_shader].iter())?; + let program = link_program(&gl, [vert_shader, frag_shader].iter())?; let positions = vec![0.0f32, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]; @@ -126,10 +127,10 @@ impl PostProcess { let mut vertex_array = if need_to_emulate_vao { crate::misc_util::VAO::emulated() } else { - crate::misc_util::VAO::native(gl) + crate::misc_util::VAO::native(&gl) }; - vertex_array.bind_vertex_array(gl); - vertex_array.bind_buffer(gl, &pos_buffer); + vertex_array.bind_vertex_array(&gl); + vertex_array.bind_buffer(&gl, &pos_buffer); let buffer_info_a_pos = BufferInfo { location: a_pos_loc, vector_size: 2, @@ -138,16 +139,17 @@ impl PostProcess { stride: 0, offset: 0, }; - vertex_array.add_new_attribute(gl, buffer_info_a_pos); + vertex_array.add_new_attribute(&gl, buffer_info_a_pos); 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); - check_for_gl_error(gl, "post process initialization"); + check_for_gl_error(&gl, "post process initialization"); Ok(PostProcess { + gl, pos_buffer, index_buffer, vertex_array, @@ -159,17 +161,17 @@ impl PostProcess { }) } - pub(crate) unsafe fn begin(&mut self, gl: &glow::Context, width: i32, height: i32) { + pub(crate) unsafe fn begin(&mut self, width: i32, height: i32) { if (width, height) != self.texture_size { - gl.bind_texture(glow::TEXTURE_2D, Some(self.texture)); - gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, 1); + self.gl.bind_texture(glow::TEXTURE_2D, Some(self.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) }; - gl.tex_image_2d( + self.gl.tex_image_2d( glow::TEXTURE_2D, 0, internal_format as i32, @@ -181,44 +183,49 @@ impl PostProcess { None, ); - gl.bind_texture(glow::TEXTURE_2D, None); + self.gl.bind_texture(glow::TEXTURE_2D, None); self.texture_size = (width, height); } - gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.fbo)); - gl.clear_color(0.0, 0.0, 0.0, 0.0); - gl.clear(glow::COLOR_BUFFER_BIT); + self.gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.fbo)); + self.gl.clear_color(0.0, 0.0, 0.0, 0.0); + self.gl.clear(glow::COLOR_BUFFER_BIT); } - pub(crate) unsafe fn bind(&self, gl: &glow::Context) { - gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.fbo)); + pub(crate) unsafe fn bind(&self) { + self.gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.fbo)); } - pub(crate) unsafe fn end(&self, gl: &glow::Context) { - gl.bind_framebuffer(glow::FRAMEBUFFER, None); - gl.disable(glow::SCISSOR_TEST); - - gl.use_program(Some(self.program)); - - gl.active_texture(glow::TEXTURE0); - gl.bind_texture(glow::TEXTURE_2D, Some(self.texture)); - let u_sampler_loc = gl.get_uniform_location(self.program, "u_sampler").unwrap(); - gl.uniform_1_i32(Some(&u_sampler_loc), 0); - self.vertex_array.bind_vertex_array(gl); - - gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.index_buffer)); - gl.draw_elements(glow::TRIANGLES, 6, glow::UNSIGNED_BYTE, 0); - self.vertex_array.unbind_vertex_array(gl); - gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None); - gl.bind_texture(glow::TEXTURE_2D, None); - gl.use_program(None); + 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.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.vertex_array.bind_vertex_array(&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.vertex_array.unbind_vertex_array(&self.gl); + self.gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, None); + self.gl.bind_texture(glow::TEXTURE_2D, None); + self.gl.use_program(None); } - pub(crate) unsafe fn destroy(&self, gl: &glow::Context) { - gl.delete_buffer(self.pos_buffer); - gl.delete_buffer(self.index_buffer); - gl.delete_program(self.program); - gl.delete_framebuffer(self.fbo); - gl.delete_texture(self.texture); + 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.texture); } } diff --git a/egui_glow/src/winit.rs b/egui_glow/src/winit.rs index 835e6a3804d..b8e9c625bde 100644 --- a/egui_glow/src/winit.rs +++ b/egui_glow/src/winit.rs @@ -12,7 +12,7 @@ pub struct EguiGlow { } impl EguiGlow { - pub fn new(window: &winit::window::Window, gl: &glow::Context) -> Self { + pub fn new(window: &winit::window::Window, gl: std::rc::Rc) -> Self { let painter = crate::Painter::new(gl, None, "") .map_err(|error| { tracing::error!("error occurred in initializing painter:\n{}", error); @@ -63,30 +63,29 @@ impl EguiGlow { } /// Paint the results of the last call to [`Self::run`]. - pub fn paint(&mut self, window: &winit::window::Window, gl: &glow::Context) { + 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(gl, id, &image_delta); + 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( - gl, dimensions, self.egui_ctx.pixels_per_point(), clipped_primitives, ); for id in textures_delta.free.drain(..) { - self.painter.free_texture(gl, id); + self.painter.free_texture(id); } } /// Call to release the allocated graphics resources. - pub fn destroy(&mut self, gl: &glow::Context) { - self.painter.destroy(gl); + pub fn destroy(&mut self) { + self.painter.destroy(); } } diff --git a/egui_web/src/glow_wrapping.rs b/egui_web/src/glow_wrapping.rs index 47fb3dd6868..9b879c129c7 100644 --- a/egui_web/src/glow_wrapping.rs +++ b/egui_web/src/glow_wrapping.rs @@ -7,7 +7,6 @@ use web_sys::HtmlCanvasElement; use web_sys::{WebGl2RenderingContext, WebGlRenderingContext}; pub(crate) struct WrappedGlowPainter { - pub(crate) glow_ctx: glow::Context, pub(crate) canvas: HtmlCanvasElement, pub(crate) canvas_id: String, pub(crate) painter: egui_glow::Painter, @@ -17,14 +16,14 @@ impl WrappedGlowPainter { pub fn new(canvas_id: &str) -> Result { let canvas = crate::canvas_element_or_die(canvas_id); - let (glow_ctx, shader_prefix) = init_glow_context_from_canvas(&canvas)?; + let (gl, shader_prefix) = init_glow_context_from_canvas(&canvas)?; + let gl = std::rc::Rc::new(gl); let dimension = [canvas.width() as i32, canvas.height() as i32]; - let painter = egui_glow::Painter::new(&glow_ctx, Some(dimension), shader_prefix) + let painter = egui_glow::Painter::new(gl, Some(dimension), shader_prefix) .map_err(|error| format!("Error starting glow painter: {}", error))?; Ok(Self { - glow_ctx, canvas, canvas_id: canvas_id.to_owned(), painter, @@ -46,16 +45,16 @@ impl crate::WebPainter for WrappedGlowPainter { } fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta) { - self.painter.set_texture(&self.glow_ctx, tex_id, delta); + self.painter.set_texture(tex_id, delta); } fn free_texture(&mut self, tex_id: egui::TextureId) { - self.painter.free_texture(&self.glow_ctx, tex_id); + self.painter.free_texture(tex_id); } fn clear(&mut self, clear_color: Rgba) { let canvas_dimension = [self.canvas.width(), self.canvas.height()]; - egui_glow::painter::clear(&self.glow_ctx, canvas_dimension, clear_color) + egui_glow::painter::clear(self.painter.gl(), canvas_dimension, clear_color) } fn paint_primitives( @@ -64,12 +63,8 @@ impl crate::WebPainter for WrappedGlowPainter { pixels_per_point: f32, ) -> Result<(), JsValue> { let canvas_dimension = [self.canvas.width(), self.canvas.height()]; - self.painter.paint_primitives( - &self.glow_ctx, - canvas_dimension, - pixels_per_point, - clipped_primitives, - ); + self.painter + .paint_primitives(canvas_dimension, pixels_per_point, clipped_primitives); Ok(()) } } @@ -115,9 +110,9 @@ fn init_webgl1(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static st "" }; - let glow_ctx = glow::Context::from_webgl1_context(gl1_ctx); + let gl = glow::Context::from_webgl1_context(gl1_ctx); - Some((glow_ctx, shader_prefix)) + Some((gl, shader_prefix)) } fn init_webgl2(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static str)> { @@ -131,10 +126,10 @@ fn init_webgl2(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static st let gl2_ctx = gl2_ctx .dyn_into::() .unwrap(); - let glow_ctx = glow::Context::from_webgl2_context(gl2_ctx); + let gl = glow::Context::from_webgl2_context(gl2_ctx); let shader_prefix = ""; - Some((glow_ctx, shader_prefix)) + Some((gl, shader_prefix)) } trait DummyWebGLConstructor { From 79678d83422741e88c854a067b20cba791810085 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 11 Mar 2022 15:55:10 +0100 Subject: [PATCH 3/7] Pass `egui_glow::Painter` to `PaintCallback` --- eframe/examples/custom_3d.rs | 8 ++++---- egui_glow/src/painter.rs | 13 ++++++++----- epaint/src/shape.rs | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/eframe/examples/custom_3d.rs b/eframe/examples/custom_3d.rs index 53dd850e2fa..548918f744e 100644 --- a/eframe/examples/custom_3d.rs +++ b/eframe/examples/custom_3d.rs @@ -59,11 +59,11 @@ impl MyApp { let callback = egui::epaint::PaintCallback { rect, callback: std::sync::Arc::new(move |render_ctx| { - if let Some(gl) = render_ctx.downcast_ref::() { + if let Some(painter) = render_ctx.downcast_ref::() { let mut rotating_triangle = rotating_triangle.lock(); - let rotating_triangle = - rotating_triangle.get_or_insert_with(|| RotatingTriangle::new(gl)); - rotating_triangle.paint(gl, angle); + let rotating_triangle = rotating_triangle + .get_or_insert_with(|| RotatingTriangle::new(painter.gl())); + rotating_triangle.paint(painter.gl(), angle); } else { eprintln!("Can't do custom painting because we are not using a glow context"); } diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index 8023b515e88..b0f307a9cb6 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -19,7 +19,10 @@ pub use glow::Context; const VERT_SRC: &str = include_str!("shader/vertex.glsl"); const FRAG_SRC: &str = include_str!("shader/fragment.glsl"); -/// OpenGL painter +/// 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. @@ -365,7 +368,7 @@ impl Painter { ); } - callback.call(&self.gl); + callback.call(self); // Restore state: unsafe { @@ -558,7 +561,8 @@ impl Painter { } } - fn get_texture(&self, texture_id: egui::TextureId) -> Option { + /// Get the [`glow::Texture`] bound to a [`egui::TextureId`]. + pub fn get_texture(&self, texture_id: egui::TextureId) -> Option { self.textures.get(&texture_id).copied() } @@ -574,9 +578,8 @@ impl Painter { } } - /// This function must be called before Painter is dropped, as Painter has some OpenGL objects + /// 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 { diff --git a/epaint/src/shape.rs b/epaint/src/shape.rs index 7154675395f..c8c6a306b1e 100644 --- a/epaint/src/shape.rs +++ b/epaint/src/shape.rs @@ -647,7 +647,7 @@ pub struct PaintCallback { /// Paint something custom using. /// /// The argument is the render context, and what it contains depends on the backend. - /// In `eframe` it will be a `glow::Context`. + /// In `eframe` it will be `egui_glow::Painter`. /// /// The rendering backend is responsible for first setting the active viewport to [`Self::rect`]. /// The rendering backend is also responsible for restoring any state it needs, From 523e705ea7ada2e539fdd33db9f65d5cce5ec85b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 12 Mar 2022 08:04:56 +0100 Subject: [PATCH 4/7] Remove trait WebPainter --- egui_web/src/backend.rs | 16 ++++--------- egui_web/src/glow_wrapping.rs | 37 +++++++++++++++++++++--------- egui_web/src/lib.rs | 2 -- egui_web/src/painter.rs | 43 ----------------------------------- 4 files changed, 30 insertions(+), 68 deletions(-) delete mode 100644 egui_web/src/painter.rs diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index e4292907451..c50f0660984 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -1,18 +1,10 @@ -use crate::*; +use crate::{glow_wrapping::WrappedGlowPainter, *}; use egui::TexturesDelta; pub use egui::{pos2, Color32}; // ---------------------------------------------------------------------------- -fn create_painter(canvas_id: &str) -> Result, JsValue> { - Ok(Box::new( - crate::glow_wrapping::WrappedGlowPainter::new(canvas_id).map_err(JsValue::from)?, - )) -} - -// ---------------------------------------------------------------------------- - /// Data gathered between frames. #[derive(Default)] pub struct WebInput { @@ -140,7 +132,7 @@ fn test_parse_query() { pub struct AppRunner { pub(crate) frame: epi::Frame, egui_ctx: egui::Context, - painter: Box, + painter: WrappedGlowPainter, pub(crate) input: WebInput, app: Box, pub(crate) needs_repaint: std::sync::Arc, @@ -154,7 +146,7 @@ pub struct AppRunner { impl AppRunner { pub fn new(canvas_id: &str, app: Box) -> Result { - let painter = create_painter(canvas_id)?; + let painter = WrappedGlowPainter::new(canvas_id).map_err(JsValue::from)?; let prefer_dark_mode = crate::prefer_dark_mode(); @@ -162,7 +154,7 @@ impl AppRunner { let frame = epi::Frame::new(epi::backend::FrameData { info: epi::IntegrationInfo { - name: painter.name(), + name: "egui_web", web_info: Some(epi::WebInfo { location: web_location(), }), diff --git a/egui_web/src/glow_wrapping.rs b/egui_web/src/glow_wrapping.rs index 9b879c129c7..c70ba6ce464 100644 --- a/egui_web/src/glow_wrapping.rs +++ b/egui_web/src/glow_wrapping.rs @@ -31,33 +31,29 @@ impl WrappedGlowPainter { } } -impl crate::WebPainter for WrappedGlowPainter { - fn name(&self) -> &'static str { - "egui_web" - } - - fn max_texture_side(&self) -> usize { +impl WrappedGlowPainter { + pub fn max_texture_side(&self) -> usize { self.painter.max_texture_side() } - fn canvas_id(&self) -> &str { + pub fn canvas_id(&self) -> &str { &self.canvas_id } - fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta) { + pub fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta) { self.painter.set_texture(tex_id, delta); } - fn free_texture(&mut self, tex_id: egui::TextureId) { + pub fn free_texture(&mut self, tex_id: egui::TextureId) { self.painter.free_texture(tex_id); } - fn clear(&mut self, clear_color: Rgba) { + pub fn clear(&mut self, clear_color: Rgba) { let canvas_dimension = [self.canvas.width(), self.canvas.height()]; egui_glow::painter::clear(self.painter.gl(), canvas_dimension, clear_color) } - fn paint_primitives( + pub fn paint_primitives( &mut self, clipped_primitives: Vec, pixels_per_point: f32, @@ -67,6 +63,25 @@ impl crate::WebPainter for WrappedGlowPainter { .paint_primitives(canvas_dimension, pixels_per_point, clipped_primitives); Ok(()) } + + pub fn paint_and_update_textures( + &mut self, + clipped_primitives: Vec, + pixels_per_point: f32, + textures_delta: &egui::TexturesDelta, + ) -> Result<(), JsValue> { + for (id, image_delta) in &textures_delta.set { + self.set_texture(*id, image_delta); + } + + self.paint_primitives(clipped_primitives, pixels_per_point)?; + + for &id in &textures_delta.free { + self.free_texture(id); + } + + Ok(()) + } } /// Returns glow context and shader prefix. diff --git a/egui_web/src/lib.rs b/egui_web/src/lib.rs index b4bf6f2fff2..a7c3068ab6b 100644 --- a/egui_web/src/lib.rs +++ b/egui_web/src/lib.rs @@ -17,7 +17,6 @@ pub mod backend; mod glow_wrapping; mod input; -mod painter; pub mod screen_reader; mod text_agent; @@ -28,7 +27,6 @@ pub use wasm_bindgen; pub use web_sys; use input::*; -pub use painter::WebPainter; use web_sys::EventTarget; use std::collections::BTreeMap; diff --git a/egui_web/src/painter.rs b/egui_web/src/painter.rs deleted file mode 100644 index 90e0b6e4c1b..00000000000 --- a/egui_web/src/painter.rs +++ /dev/null @@ -1,43 +0,0 @@ -use wasm_bindgen::prelude::JsValue; - -/// What is needed to paint egui. -pub trait WebPainter { - fn name(&self) -> &'static str; - - /// Max size of one side of a texture. - fn max_texture_side(&self) -> usize; - - /// id of the canvas html element containing the rendering - fn canvas_id(&self) -> &str; - - fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta); - - fn free_texture(&mut self, tex_id: egui::TextureId); - - fn clear(&mut self, clear_color: egui::Rgba); - - fn paint_primitives( - &mut self, - clipped_primitives: Vec, - pixels_per_point: f32, - ) -> Result<(), JsValue>; - - fn paint_and_update_textures( - &mut self, - clipped_primitives: Vec, - pixels_per_point: f32, - textures_delta: &egui::TexturesDelta, - ) -> Result<(), JsValue> { - for (id, image_delta) in &textures_delta.set { - self.set_texture(*id, image_delta); - } - - self.paint_primitives(clipped_primitives, pixels_per_point)?; - - for &id in &textures_delta.free { - self.free_texture(id); - } - - Ok(()) - } -} From 7ac08119dd4cfae2582ef56e8a09bcaedaa613f6 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 13 Mar 2022 22:17:22 +0100 Subject: [PATCH 5/7] Add glow::Context to epi::App::setup --- Cargo.lock | 2 ++ eframe/examples/custom_font.rs | 1 + egui-winit/Cargo.toml | 10 +++++++--- egui-winit/src/epi.rs | 7 ++++--- egui_demo_lib/src/apps/demo/app.rs | 1 + egui_demo_lib/src/wrap_app.rs | 1 + egui_glow/Cargo.toml | 2 +- egui_glow/src/epi_backend.rs | 1 + egui_web/src/backend.rs | 9 ++++----- epi/Cargo.toml | 1 + epi/src/lib.rs | 17 ++++++++++++++--- 11 files changed, 37 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 555503ef686..55dee5c1b43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1018,6 +1018,7 @@ dependencies = [ "dark-light", "egui", "epi", + "glow", "instant", "serde", "tracing", @@ -1219,6 +1220,7 @@ version = "0.17.0" dependencies = [ "directories-next", "egui", + "glow", "ron", "serde", "tracing", diff --git a/eframe/examples/custom_font.rs b/eframe/examples/custom_font.rs index 8ba2ced48d8..0192d32ca1f 100644 --- a/eframe/examples/custom_font.rs +++ b/eframe/examples/custom_font.rs @@ -22,6 +22,7 @@ impl epi::App for MyApp { ctx: &egui::Context, _frame: &epi::Frame, _storage: Option<&dyn epi::Storage>, + _gl: &std::rc::Rc, ) { // Start with the default fonts (we will be adding to them rather than replacing them). let mut fonts = egui::FontDefinitions::default(); diff --git a/egui-winit/Cargo.toml b/egui-winit/Cargo.toml index 1bf48fdad07..a7cde96f905 100644 --- a/egui-winit/Cargo.toml +++ b/egui-winit/Cargo.toml @@ -24,6 +24,12 @@ default = ["clipboard", "dark-light", "links"] # if disabled a clipboard will be simulated so you can still copy/paste within the egui app. clipboard = ["copypasta"] +# implement bytemuck on most types. +convert_bytemuck = ["egui/convert_bytemuck"] + +# Only for `egui_glow` - the official eframe/epi backend. +epi_backend = ["epi", "glow"] + # enable opening links in a browser when an egui hyperlink is clicked. links = ["webbrowser"] @@ -36,9 +42,6 @@ persistence = [ ] # can't add epi/persistence here because of https://github.com/rust-lang/cargo/issues/8832 serialize = ["egui/serialize", "serde"] -# implement bytemuck on most types. -convert_bytemuck = ["egui/convert_bytemuck"] - [dependencies] egui = { version = "0.17.0", path = "../egui", default-features = false, features = [ @@ -53,6 +56,7 @@ epi = { version = "0.17.0", path = "../epi", optional = true } copypasta = { version = "0.7", optional = true } dark-light = { version = "0.2.1", optional = true } # detect dark mode system preference +glow = { version = "0.11", optional = true } serde = { version = "1.0", optional = true, features = ["derive"] } webbrowser = { version = "0.6", optional = true } diff --git a/egui-winit/src/epi.rs b/egui-winit/src/epi.rs index fd6711319fa..d257ca21edd 100644 --- a/egui-winit/src/epi.rs +++ b/egui-winit/src/epi.rs @@ -232,6 +232,7 @@ impl EpiIntegration { integration_name: &'static str, max_texture_side: usize, window: &winit::window::Window, + gl: &std::rc::Rc, repaint_signal: std::sync::Arc, persistence: crate::epi::Persistence, app: Box, @@ -271,7 +272,7 @@ impl EpiIntegration { can_drag_window: false, }; - slf.setup(window); + slf.setup(window, gl); if slf.app.warm_up_enabled() { slf.warm_up(window); } @@ -279,9 +280,9 @@ impl EpiIntegration { slf } - fn setup(&mut self, window: &winit::window::Window) { + fn setup(&mut self, window: &winit::window::Window, gl: &std::rc::Rc) { self.app - .setup(&self.egui_ctx, &self.frame, self.persistence.storage()); + .setup(&self.egui_ctx, &self.frame, self.persistence.storage(), gl); let app_output = self.frame.take_app_output(); if app_output.quit { diff --git a/egui_demo_lib/src/apps/demo/app.rs b/egui_demo_lib/src/apps/demo/app.rs index 063f29e751b..5544a51ad39 100644 --- a/egui_demo_lib/src/apps/demo/app.rs +++ b/egui_demo_lib/src/apps/demo/app.rs @@ -19,6 +19,7 @@ impl epi::App for DemoApp { _ctx: &egui::Context, _frame: &epi::Frame, _storage: Option<&dyn epi::Storage>, + _gl: &std::rc::Rc, ) { #[cfg(feature = "persistence")] if let Some(storage) = _storage { diff --git a/egui_demo_lib/src/wrap_app.rs b/egui_demo_lib/src/wrap_app.rs index ad752886bf6..0a1195978e9 100644 --- a/egui_demo_lib/src/wrap_app.rs +++ b/egui_demo_lib/src/wrap_app.rs @@ -47,6 +47,7 @@ impl epi::App for WrapApp { _ctx: &egui::Context, _frame: &epi::Frame, _storage: Option<&dyn epi::Storage>, + _gl: &std::rc::Rc, ) { #[cfg(feature = "persistence")] if let Some(storage) = _storage { diff --git a/egui_glow/Cargo.toml b/egui_glow/Cargo.toml index aa6ed64835b..924210a1eb9 100644 --- a/egui_glow/Cargo.toml +++ b/egui_glow/Cargo.toml @@ -69,7 +69,7 @@ tracing = "0.1" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] egui-winit = { version = "0.17.0", path = "../egui-winit", optional = true, default-features = false, features = [ "dark-light", - "epi", + "epi_backend", ] } glutin = { version = "0.28.0", optional = true } diff --git a/egui_glow/src/epi_backend.rs b/egui_glow/src/epi_backend.rs index 60b0c54cb3d..bbf8755066a 100644 --- a/egui_glow/src/epi_backend.rs +++ b/egui_glow/src/epi_backend.rs @@ -66,6 +66,7 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { "egui_glow", painter.max_texture_side(), gl_window.window(), + &gl, repaint_signal, persistence, app, diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index c50f0660984..0da078cf7e7 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -193,11 +193,10 @@ impl AppRunner { runner.input.raw.max_texture_side = Some(runner.painter.max_texture_side()); - { - runner - .app - .setup(&runner.egui_ctx, &runner.frame, Some(&runner.storage)); - } + let gl = runner.painter.painter.gl(); + runner + .app + .setup(&runner.egui_ctx, &runner.frame, Some(&runner.storage), gl); Ok(runner) } diff --git a/epi/Cargo.toml b/epi/Cargo.toml index 398e426cedd..4e894a62114 100644 --- a/epi/Cargo.toml +++ b/epi/Cargo.toml @@ -31,6 +31,7 @@ persistence = ["ron", "serde", "egui/persistence"] egui = { version = "0.17.0", path = "../egui", default-features = false, features = [ "single_threaded", ] } +glow = "0.11" tracing = "0.1" directories-next = { version = "2", optional = true } diff --git a/epi/src/lib.rs b/epi/src/lib.rs index 88171aaac84..033a4a7880c 100644 --- a/epi/src/lib.rs +++ b/epi/src/lib.rs @@ -94,6 +94,7 @@ pub mod file_storage; pub use egui; // Re-export for user convenience +pub use glow; // Re-export for user convenience use std::sync::{Arc, Mutex}; @@ -112,13 +113,23 @@ pub trait App { /// or call [`Frame::request_repaint`] at any time (e.g. from another thread). fn update(&mut self, ctx: &egui::Context, frame: &Frame); - /// Called once before the first frame. + /// Called exactly once at startup, before any call to [`Self::update`]. /// /// Allows you to do setup code, e.g to call [`egui::Context::set_fonts`], /// [`egui::Context::set_visuals`] etc. /// - /// Also allows you to restore state, if there is a storage (required the "persistence" feature). - fn setup(&mut self, _ctx: &egui::Context, _frame: &Frame, _storage: Option<&dyn Storage>) {} + /// Also allows you to restore state, if there is a storage (requires the "persistence" feature). + /// + /// The [`glow::Context`] allows you to initialize OpenGL resources (e.g. shaders) that + /// you might want to use later from a [`egui::PaintCallback`]. + fn setup( + &mut self, + _ctx: &egui::Context, + _frame: &Frame, + _storage: Option<&dyn Storage>, + _gl: &std::rc::Rc, + ) { + } /// Called on shutdown, and perhaps at regular intervals. Allows you to save state. /// From 3f9e3c7c2c283611aa1ac0c8cf30859ef7e918d7 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 14 Mar 2022 10:01:49 +0100 Subject: [PATCH 6/7] Some cleanup --- egui_glium/src/lib.rs | 2 +- egui_glium/src/painter.rs | 6 +++--- egui_glow/src/epi_backend.rs | 2 +- egui_glow/src/painter.rs | 6 +++--- egui_glow/src/winit.rs | 2 +- egui_web/src/backend.rs | 5 +---- egui_web/src/glow_wrapping.rs | 4 ++-- egui_web/src/lib.rs | 2 +- epaint/src/shape.rs | 4 ++-- 9 files changed, 15 insertions(+), 18 deletions(-) diff --git a/egui_glium/src/lib.rs b/egui_glium/src/lib.rs index ce5b67864f5..9e6760e5392 100644 --- a/egui_glium/src/lib.rs +++ b/egui_glium/src/lib.rs @@ -164,7 +164,7 @@ impl EguiGlium { display, target, self.egui_ctx.pixels_per_point(), - clipped_primitives, + &clipped_primitives, &textures_delta, ); } diff --git a/egui_glium/src/painter.rs b/egui_glium/src/painter.rs index 0f779c5f3d3..4eaf87c8717 100644 --- a/egui_glium/src/painter.rs +++ b/egui_glium/src/painter.rs @@ -70,7 +70,7 @@ impl Painter { display: &glium::Display, target: &mut T, pixels_per_point: f32, - clipped_primitives: Vec, + clipped_primitives: &[egui::ClippedPrimitive], textures_delta: &egui::TexturesDelta, ) { for (id, image_delta) in &textures_delta.set { @@ -92,7 +92,7 @@ impl Painter { display: &glium::Display, target: &mut T, pixels_per_point: f32, - clipped_primitives: Vec, + clipped_primitives: &[egui::ClippedPrimitive], ) { for egui::ClippedPrimitive { clip_rect, @@ -116,7 +116,7 @@ impl Painter { target: &mut T, display: &glium::Display, pixels_per_point: f32, - clip_rect: Rect, + clip_rect: &Rect, mesh: &Mesh, ) { debug_assert!(mesh.is_valid()); diff --git a/egui_glow/src/epi_backend.rs b/egui_glow/src/epi_backend.rs index bbf8755066a..57c3f46ef92 100644 --- a/egui_glow/src/epi_backend.rs +++ b/egui_glow/src/epi_backend.rs @@ -108,7 +108,7 @@ pub fn run(app: Box, native_options: &epi::NativeOptions) -> ! { painter.paint_and_update_textures( gl_window.window().inner_size().into(), integration.egui_ctx.pixels_per_point(), - clipped_primitives, + &clipped_primitives, &textures_delta, ); diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index b0f307a9cb6..ef707129344 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -287,7 +287,7 @@ impl Painter { &mut self, inner_size: [u32; 2], pixels_per_point: f32, - clipped_primitives: Vec, + clipped_primitives: &[egui::ClippedPrimitive], textures_delta: &egui::TexturesDelta, ) { for (id, image_delta) in &textures_delta.set { @@ -324,7 +324,7 @@ impl Painter { &mut self, inner_size: [u32; 2], pixels_per_point: f32, - clipped_primitives: Vec, + clipped_primitives: &[egui::ClippedPrimitive], ) { self.assert_not_destroyed(); @@ -340,7 +340,7 @@ impl Painter { primitive, } in clipped_primitives { - set_clip_rect(&self.gl, size_in_pixels, pixels_per_point, clip_rect); + set_clip_rect(&self.gl, size_in_pixels, pixels_per_point, *clip_rect); match primitive { Primitive::Mesh(mesh) => { diff --git a/egui_glow/src/winit.rs b/egui_glow/src/winit.rs index b8e9c625bde..547e9065be3 100644 --- a/egui_glow/src/winit.rs +++ b/egui_glow/src/winit.rs @@ -76,7 +76,7 @@ impl EguiGlow { self.painter.paint_primitives( dimensions, self.egui_ctx.pixels_per_point(), - clipped_primitives, + &clipped_primitives, ); for id in textures_delta.free.drain(..) { diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index 0da078cf7e7..50313303b8b 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -273,10 +273,7 @@ impl AppRunner { } /// Paint the results of the last call to [`Self::logic`]. - pub fn paint( - &mut self, - clipped_primitives: Vec, - ) -> Result<(), JsValue> { + pub fn paint(&mut self, clipped_primitives: &[egui::ClippedPrimitive]) -> Result<(), JsValue> { let textures_delta = std::mem::take(&mut self.textures_delta); self.painter.clear(self.app.clear_color()); diff --git a/egui_web/src/glow_wrapping.rs b/egui_web/src/glow_wrapping.rs index c70ba6ce464..c589aad1400 100644 --- a/egui_web/src/glow_wrapping.rs +++ b/egui_web/src/glow_wrapping.rs @@ -55,7 +55,7 @@ impl WrappedGlowPainter { pub fn paint_primitives( &mut self, - clipped_primitives: Vec, + clipped_primitives: &[ClippedPrimitive], pixels_per_point: f32, ) -> Result<(), JsValue> { let canvas_dimension = [self.canvas.width(), self.canvas.height()]; @@ -66,7 +66,7 @@ impl WrappedGlowPainter { pub fn paint_and_update_textures( &mut self, - clipped_primitives: Vec, + clipped_primitives: &[egui::ClippedPrimitive], pixels_per_point: f32, textures_delta: &egui::TexturesDelta, ) -> Result<(), JsValue> { diff --git a/egui_web/src/lib.rs b/egui_web/src/lib.rs index a7c3068ab6b..062c9ac0d4f 100644 --- a/egui_web/src/lib.rs +++ b/egui_web/src/lib.rs @@ -348,7 +348,7 @@ fn paint_and_schedule(runner_ref: &AppRunnerRef, panicked: Arc) -> R let mut runner_lock = runner_ref.lock(); if runner_lock.needs_repaint.fetch_and_clear() { let (needs_repaint, clipped_primitives) = runner_lock.logic()?; - runner_lock.paint(clipped_primitives)?; + runner_lock.paint(&clipped_primitives)?; if needs_repaint { runner_lock.needs_repaint.set_true(); } diff --git a/epaint/src/shape.rs b/epaint/src/shape.rs index c8c6a306b1e..1d846fb21e8 100644 --- a/epaint/src/shape.rs +++ b/epaint/src/shape.rs @@ -672,8 +672,8 @@ impl std::fmt::Debug for PaintCallback { impl std::cmp::PartialEq for PaintCallback { fn eq(&self, other: &Self) -> bool { - // As I understand it, the problem this clippy tried to protect against can only happen - // if we do dynamic casts back and forth on the pointers, and we don't do that. + // As I understand it, the problem this clippy is trying to protect against + // can only happen if we do dynamic casts back and forth on the pointers, and we don't do that. #[allow(clippy::vtable_address_comparisons)] { self.rect.eq(&other.rect) && std::sync::Arc::ptr_eq(&self.callback, &other.callback) From bff72bfcaa78458cb21dd46a61c0286d4f6ba48a Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 14 Mar 2022 10:18:48 +0100 Subject: [PATCH 7/7] clippy --- egui_glium/src/painter.rs | 2 +- egui_glow/src/painter.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/egui_glium/src/painter.rs b/egui_glium/src/painter.rs index 4eaf87c8717..11d45fff867 100644 --- a/egui_glium/src/painter.rs +++ b/egui_glium/src/painter.rs @@ -101,7 +101,7 @@ impl Painter { { match primitive { Primitive::Mesh(mesh) => { - self.paint_mesh(target, display, pixels_per_point, clip_rect, &mesh); + self.paint_mesh(target, display, pixels_per_point, clip_rect, mesh); } Primitive::Callback(_) => { panic!("Custom rendering callbacks are not implemented in egui_glium"); diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index ef707129344..8e68836c08b 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -344,7 +344,7 @@ impl Painter { match primitive { Primitive::Mesh(mesh) => { - self.paint_mesh(&mesh); + self.paint_mesh(mesh); } Primitive::Callback(callback) => { if callback.rect.is_positive() {