1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 05:10:03 -04:00

Re-implement PaintCallbacks With Support for WGPU (#1684)

* Re-implement PaintCallbacks With Support for WGPU

This makes breaking changes to the PaintCallback system, but makes it
flexible enough to support both the WGPU and glow backends with custom
rendering.

Also adds a WGPU equivalent to the glow demo for custom painting.
This commit is contained in:
Zicklag
2022-05-28 10:52:36 -05:00
committed by GitHub
parent 8173093c67
commit 1d9524cc59
22 changed files with 595 additions and 130 deletions

View File

@@ -9,7 +9,7 @@
pub mod painter;
pub use glow;
pub use painter::Painter;
pub use painter::{CallbackFn, Painter};
mod misc_util;
mod post_process;
mod shader_version;

View File

@@ -4,7 +4,7 @@ use std::{collections::HashMap, sync::Arc};
use egui::{
emath::Rect,
epaint::{Color32, Mesh, Primitive, Vertex},
epaint::{Color32, Mesh, PaintCallbackInfo, Primitive, Vertex},
};
use glow::HasContext as _;
use memoffset::offset_of;
@@ -68,6 +68,29 @@ pub struct Painter {
destroyed: bool,
}
/// A callback function that can be used to compose an [`egui::PaintCallback`] for custom rendering
/// with [`glow`].
///
/// The callback is passed, the [`egui::PaintCallbackInfo`] and the [`Painter`] which can be used to
/// access the OpenGL context.
///
/// # Example
///
/// See the [custom3d_glow] demo source for a detailed usage example.
///
/// [custom3d_glow]:
/// https://github.com/emilk/egui/blob/master/egui_demo_app/src/apps/custom3d_wgpu.rs
pub struct CallbackFn {
f: Box<dyn Fn(PaintCallbackInfo, &Painter) + Sync + Send>,
}
impl CallbackFn {
pub fn new<F: Fn(PaintCallbackInfo, &Painter) + Sync + Send + 'static>(callback: F) -> Self {
let f = Box::new(callback);
CallbackFn { f }
}
}
impl Painter {
/// Create painter.
///
@@ -381,7 +404,11 @@ impl Painter {
screen_size_px,
};
callback.call(&info, self);
if let Some(callback) = callback.callback.downcast_ref::<CallbackFn>() {
(callback.f)(info, self);
} else {
tracing::warn!("Warning: Unsupported render callback. Expected egui_glow::CallbackFn");
}
check_for_gl_error!(&self.gl, "callback");