Add Shape::Callback to do custom rendering inside of an egui UI (#1351)

* Add Shape::Callback to do custom rendering inside of an egui UI
* Use Rc<glow::Context> everywhere
* Remove trait WebPainter
* Add glow::Context to epi::App::setup
This commit is contained in:
Emil Ernerfeldt 2022-03-14 13:25:11 +01:00 committed by GitHub
parent 002158050b
commit 6aee4997d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 777 additions and 385 deletions

View file

@ -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

4
Cargo.lock generated
View file

@ -991,7 +991,9 @@ dependencies = [
"egui_web",
"ehttp",
"epi",
"glow",
"image",
"parking_lot 0.12.0",
"poll-promise",
"rfd",
]
@ -1016,6 +1018,7 @@ dependencies = [
"dark-light",
"egui",
"epi",
"glow",
"instant",
"serde",
"tracing",
@ -1217,6 +1220,7 @@ version = "0.17.0"
dependencies = [
"directories-next",
"egui",
"glow",
"ron",
"serde",
"tracing",

View file

@ -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);

View file

@ -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"

View file

@ -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<Mutex<Option<RotatingTriangle>>>,
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(painter) = render_ctx.downcast_ref::<egui_glow::Painter>() {
let mut rotating_triangle = rotating_triangle.lock();
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");
}
}),
};
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);
}

View file

@ -22,6 +22,7 @@ impl epi::App for MyApp {
ctx: &egui::Context,
_frame: &epi::Frame,
_storage: Option<&dyn epi::Storage>,
_gl: &std::rc::Rc<epi::glow::Context>,
) {
// Start with the default fonts (we will be adding to them rather than replacing them).
let mut fonts = egui::FontDefinitions::default();

View file

@ -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 }

View file

@ -232,6 +232,7 @@ impl EpiIntegration {
integration_name: &'static str,
max_texture_side: usize,
window: &winit::window::Window,
gl: &std::rc::Rc<glow::Context>,
repaint_signal: std::sync::Arc<dyn epi::backend::RepaintSignal>,
persistence: crate::epi::Persistence,
app: Box<dyn epi::App>,
@ -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<glow::Context>) {
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 {

View file

@ -122,7 +122,7 @@ impl ContextImpl {
///
/// ``` no_run
/// # fn handle_platform_output(_: egui::PlatformOutput) {}
/// # fn paint(textures_detla: egui::TexturesDelta, _: Vec<egui::ClippedMesh>) {}
/// # fn paint(textures_detla: egui::TexturesDelta, _: Vec<egui::ClippedPrimitive>) {}
/// 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<ClippedShape>) -> Vec<ClippedMesh> {
pub fn tessellate(&self, shapes: Vec<ClippedShape>) -> Vec<ClippedPrimitive> {
// 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<T: Send + Sync>() {}
assert_send_sync::<Context>();
}

View file

@ -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");

View file

@ -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<egui::ClippedMesh>) {}
//! # fn paint(textures_detla: egui::TexturesDelta, _: Vec<egui::ClippedPrimitive>) {}
//! 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 {

View file

@ -19,6 +19,7 @@ impl epi::App for DemoApp {
_ctx: &egui::Context,
_frame: &epi::Frame,
_storage: Option<&dyn epi::Storage>,
_gl: &std::rc::Rc<epi::glow::Context>,
) {
#[cfg(feature = "persistence")]
if let Some(storage) = _storage {

View file

@ -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"
);
}
}

View file

@ -47,6 +47,7 @@ impl epi::App for WrapApp {
_ctx: &egui::Context,
_frame: &epi::Frame,
_storage: Option<&dyn epi::Storage>,
_gl: &std::rc::Rc<epi::glow::Context>,
) {
#[cfg(feature = "persistence")]
if let Some(storage) = _storage {

View file

@ -159,12 +159,12 @@ impl EguiGlium {
pub fn paint<T: glium::Surface>(&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,
);
}

View file

@ -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<egui::ClippedMesh>,
clipped_primitives: &[egui::ClippedPrimitive],
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<T: glium::Surface>(
pub fn paint_primitives<T: glium::Surface>(
&mut self,
display: &glium::Display,
target: &mut T,
pixels_per_point: f32,
clipped_meshes: Vec<egui::ClippedMesh>,
clipped_primitives: &[egui::ClippedPrimitive],
) {
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");
}
}
}
}
@ -103,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());

View file

@ -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 }

View file

@ -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();
}
_ => (),

View file

@ -54,17 +54,19 @@ pub fn run(app: Box<dyn epi::App>, 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",
painter.max_texture_side(),
gl_window.window(),
&gl,
repaint_signal,
persistence,
app,
@ -92,7 +94,7 @@ pub fn run(app: Box<dyn epi::App>, 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:
{
@ -104,10 +106,9 @@ pub fn run(app: Box<dyn epi::App>, 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_meshes,
&clipped_primitives,
&textures_delta,
);
@ -153,7 +154,7 @@ pub fn run(app: Box<dyn epi::App>, 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();

View file

@ -1,10 +1,10 @@
#![allow(unsafe_code)]
use std::collections::HashMap;
use std::{collections::HashMap, rc::Rc};
use egui::{
emath::Rect,
epaint::{Color32, Mesh, Vertex},
epaint::{Color32, Mesh, Primitive, Vertex},
};
use glow::HasContext;
use memoffset::offset_of;
@ -19,11 +19,16 @@ 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.
pub struct Painter {
gl: Rc<glow::Context>,
max_texture_side: usize,
program: glow::Program,
@ -86,16 +91,16 @@ impl Painter {
/// * failed to create postprocess on webgl with `sRGB` support
/// * failed to create buffer
pub fn new(
gl: &glow::Context,
gl: Rc<glow::Context>,
pp_fb_extent: Option<[i32; 2]>,
shader_prefix: &str,
) -> Result<Painter, String> {
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 +115,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 +139,7 @@ impl Painter {
unsafe {
let vert = compile_shader(
gl,
&gl,
glow::VERTEX_SHADER,
&format!(
"{}\n{}\n{}\n{}",
@ -145,7 +150,7 @@ impl Painter {
),
)?;
let frag = compile_shader(
gl,
&gl,
glow::FRAGMENT_SHADER,
&format!(
"{}\n{}\n{}\n{}\n{}",
@ -156,7 +161,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 +175,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::<Vertex>() as i32;
let position_buffer_info = vao_emulate::BufferInfo {
location: a_pos_loc,
@ -201,12 +206,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 +234,11 @@ impl Painter {
}
}
/// Access the shared glow context.
pub fn gl(&self) -> &std::rc::Rc<glow::Context> {
&self.gl
}
pub fn max_texture_side(&self) -> usize {
self.max_texture_side
}
@ -235,16 +246,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 +267,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_meshes: Vec<egui::ClippedMesh>,
clipped_primitives: &[egui::ClippedPrimitive],
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_meshes(gl, inner_size, pixels_per_point, clipped_meshes);
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);
}
}
@ -308,92 +320,107 @@ 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<egui::ClippedMesh>,
clipped_primitives: &[egui::ClippedPrimitive],
) {
self.assert_not_destroyed();
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) };
for egui::ClippedMesh(clip_rect, mesh) in clipped_meshes {
self.paint_mesh(gl, size_in_pixels, pixels_per_point, clip_rect, &mesh);
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(&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() {
// 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,
);
}
callback.call(self);
// Restore state:
unsafe {
if let Some(ref mut post_process) = self.post_process {
post_process.bind();
}
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,
size_in_pixels: (u32, u32),
pixels_per_point: f32,
clip_rect: Rect,
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));
}
// 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(
self.gl.draw_elements(
glow::TRIANGLES,
mesh.indices.len() as i32,
glow::UNSIGNED_INT,
@ -411,20 +438,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 {
@ -437,7 +459,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!(
@ -456,43 +478,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 {
@ -505,11 +521,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 _,
@ -520,10 +536,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 _,
@ -534,42 +550,42 @@ 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) };
}
}
fn get_texture(&self, texture_id: egui::TextureId) -> Option<glow::Texture> {
/// Get the [`glow::Texture`] bound to a [`egui::TextureId`].
pub fn get_texture(&self, texture_id: egui::TextureId) -> Option<glow::Texture> {
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
/// 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;
@ -626,3 +642,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,
);
}
}

View file

@ -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<glow::Context>,
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<glow::Context>,
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,40 +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 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 bind(&self) {
self.gl.bind_framebuffer(glow::FRAMEBUFFER, Some(self.fbo));
}
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 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) {
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);
}
}

View file

@ -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<glow::Context>) -> 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_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(
gl,
self.painter.paint_primitives(
dimensions,
self.egui_ctx.pixels_per_point(),
clipped_meshes,
&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();
}
}

View file

@ -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<Box<dyn WebPainter>, 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<dyn WebPainter>,
painter: WrappedGlowPainter,
pub(crate) input: WebInput,
app: Box<dyn epi::App>,
pub(crate) needs_repaint: std::sync::Arc<NeedRepaint>,
@ -154,7 +146,7 @@ pub struct AppRunner {
impl AppRunner {
pub fn new(canvas_id: &str, app: Box<dyn epi::App>) -> Result<Self, JsValue> {
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(),
}),
@ -201,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)
}
@ -245,7 +236,7 @@ impl AppRunner {
/// Returns `true` if egui requests a repaint.
///
/// Call [`Self::paint`] later to paint
pub fn logic(&mut self) -> Result<(bool, Vec<egui::ClippedMesh>), JsValue> {
pub fn logic(&mut self) -> Result<(bool, Vec<egui::ClippedPrimitive>), JsValue> {
let frame_start = now_sec();
resize_canvas_to_screen_size(self.canvas_id(), self.app.max_size_points());
@ -264,7 +255,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 +269,17 @@ 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<egui::ClippedMesh>) -> 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());
self.painter.paint_and_update_textures(
clipped_meshes,
clipped_primitives,
self.egui_ctx.pixels_per_point(),
&textures_delta,
)?;

View file

@ -1,4 +1,4 @@
use egui::{ClippedMesh, Rgba};
use egui::{ClippedPrimitive, Rgba};
use egui_glow::glow;
use wasm_bindgen::JsCast;
use wasm_bindgen::JsValue;
@ -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<Self, String> {
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,
@ -32,44 +31,55 @@ 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) {
self.painter.set_texture(&self.glow_ctx, tex_id, delta);
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) {
self.painter.free_texture(&self.glow_ctx, tex_id);
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.glow_ctx, canvas_dimension, clear_color)
egui_glow::painter::clear(self.painter.gl(), canvas_dimension, clear_color)
}
fn paint_meshes(
pub fn paint_primitives(
&mut self,
clipped_meshes: Vec<ClippedMesh>,
clipped_primitives: &[ClippedPrimitive],
pixels_per_point: f32,
) -> Result<(), JsValue> {
let canvas_dimension = [self.canvas.width(), self.canvas.height()];
self.painter.paint_meshes(
&self.glow_ctx,
canvas_dimension,
pixels_per_point,
clipped_meshes,
);
self.painter
.paint_primitives(canvas_dimension, pixels_per_point, clipped_primitives);
Ok(())
}
pub fn paint_and_update_textures(
&mut self,
clipped_primitives: &[egui::ClippedPrimitive],
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(())
}
}
@ -115,9 +125,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 +141,10 @@ fn init_webgl2(canvas: &HtmlCanvasElement) -> Option<(glow::Context, &'static st
let gl2_ctx = gl2_ctx
.dyn_into::<web_sys::WebGl2RenderingContext>()
.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 {

View file

@ -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;
@ -349,8 +347,8 @@ fn paint_and_schedule(runner_ref: &AppRunnerRef, panicked: Arc<AtomicBool>) -> 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();
}

View file

@ -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_meshes(
&mut self,
clipped_meshes: Vec<egui::ClippedMesh>,
pixels_per_point: f32,
) -> Result<(), JsValue>;
fn paint_and_update_textures(
&mut self,
clipped_meshes: Vec<egui::ClippedMesh>,
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_meshes(clipped_meshes, pixels_per_point)?;
for &id in &textures_delta.free {
self.free_texture(id);
}
Ok(())
}
}

View file

@ -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

View file

@ -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),
}
// ----------------------------------------------------------------------------

View file

@ -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<T: Send + Sync>() {}
assert_send_sync::<Shape>();
}
impl From<Vec<Shape>> 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 `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,
/// such as the bound shader program and vertex array.
pub callback: std::sync::Arc<dyn Fn(&dyn std::any::Any) + Send + Sync>,
}
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 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)
}
}
}
impl From<PaintCallback> for Shape {
#[inline(always)]
fn from(shape: PaintCallback) -> Self {
Self::Callback(shape)
}
}

View file

@ -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
}
}
}

View file

@ -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 {

View file

@ -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<ClippedShape>,
options: TessellationOptions,
tex_size: [usize; 2],
) -> Vec<ClippedMesh> {
) -> Vec<ClippedPrimitive> {
let mut tessellator = Tessellator::from_options(options);
let mut clipped_meshes: Vec<ClippedMesh> = Vec::default();
let mut clipped_primitives: Vec<ClippedPrimitive> = 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<ClippedPrimitive>,
) -> Vec<ClippedPrimitive> {
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()
}

View file

@ -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 }

View file

@ -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<glow::Context>,
) {
}
/// Called on shutdown, and perhaps at regular intervals. Allows you to save state.
///
@ -361,6 +372,13 @@ impl Frame {
}
}
#[cfg(test)]
#[test]
fn frame_impl_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<Frame>();
}
/// Information about the web environment (if applicable).
#[derive(Clone, Debug)]
pub struct WebInfo {