Add `register_native_texture` in `eframe::Frame` (#4246)

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo cranky`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* Closes https://github.com/emilk/egui/issues/4243

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
lomekragow 2024-03-27 23:35:25 +08:00 committed by GitHub
parent 3c029a45ac
commit 570e7cf71b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 1 deletions

View File

@ -614,6 +614,11 @@ pub struct Frame {
#[cfg(feature = "glow")]
pub(crate) gl: Option<std::sync::Arc<glow::Context>>,
/// Used to convert user custom [`glow::Texture`] to [`egui::TextureId`]
#[cfg(all(feature = "glow", not(target_arch = "wasm32")))]
pub(crate) glow_register_native_texture:
Option<Box<dyn FnMut(glow::Texture) -> egui::TextureId>>,
/// Can be used to manage GPU resources for custom rendering with WGPU using [`egui::PaintCallback`]s.
#[cfg(feature = "wgpu")]
pub(crate) wgpu_render_state: Option<egui_wgpu::RenderState>,
@ -690,6 +695,15 @@ impl Frame {
self.gl.as_ref()
}
/// Register your own [`glow::Texture`],
/// and then you can use the returned [`egui::TextureId`] to render your texture with [`egui`].
///
/// This function will take the ownership of your [`glow::Texture`], so please do not delete your [`glow::Texture`] after registering.
#[cfg(all(feature = "glow", not(target_arch = "wasm32")))]
pub fn register_native_glow_texture(&mut self, native: glow::Texture) -> egui::TextureId {
self.glow_register_native_texture.as_mut().unwrap()(native)
}
/// The underlying WGPU render state.
///
/// Only available when compiling with the `wgpu` feature and using [`Renderer::Wgpu`].

View File

@ -152,6 +152,9 @@ impl EpiIntegration {
native_options: &crate::NativeOptions,
storage: Option<Box<dyn epi::Storage>>,
#[cfg(feature = "glow")] gl: Option<std::sync::Arc<glow::Context>>,
#[cfg(feature = "glow")] glow_register_native_texture: Option<
Box<dyn FnMut(glow::Texture) -> egui::TextureId>,
>,
#[cfg(feature = "wgpu")] wgpu_render_state: Option<egui_wgpu::RenderState>,
) -> Self {
let frame = epi::Frame {
@ -162,6 +165,8 @@ impl EpiIntegration {
storage,
#[cfg(feature = "glow")]
gl,
#[cfg(feature = "glow")]
glow_register_native_texture,
#[cfg(feature = "wgpu")]
wgpu_render_state,
raw_display_handle: window.display_handle().map(|h| h.as_raw()),

View File

@ -217,6 +217,7 @@ impl GlowWinitApp {
let system_theme =
winit_integration::system_theme(&glutin.window(ViewportId::ROOT), &self.native_options);
let painter = Rc::new(RefCell::new(painter));
let integration = EpiIntegration::new(
egui_ctx,
@ -226,6 +227,10 @@ impl GlowWinitApp {
&self.native_options,
storage,
Some(gl.clone()),
Some(Box::new({
let painter = painter.clone();
move |native| painter.borrow_mut().register_native_texture(native)
})),
#[cfg(feature = "wgpu")]
None,
);
@ -302,7 +307,6 @@ impl GlowWinitApp {
};
let glutin = Rc::new(RefCell::new(glutin));
let painter = Rc::new(RefCell::new(painter));
{
// Create weak pointers so that we don't keep

View File

@ -215,6 +215,8 @@ impl WgpuWinitApp {
storage,
#[cfg(feature = "glow")]
None,
#[cfg(feature = "glow")]
None,
wgpu_render_state.clone(),
);