diff --git a/crates/eframe/src/epi.rs b/crates/eframe/src/epi.rs index 5ee29e45..e7b53691 100644 --- a/crates/eframe/src/epi.rs +++ b/crates/eframe/src/epi.rs @@ -614,6 +614,11 @@ pub struct Frame { #[cfg(feature = "glow")] pub(crate) gl: Option>, + /// 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 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, @@ -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`]. diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index f27d0112..fbf7b6dc 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -152,6 +152,9 @@ impl EpiIntegration { native_options: &crate::NativeOptions, storage: Option>, #[cfg(feature = "glow")] gl: Option>, + #[cfg(feature = "glow")] glow_register_native_texture: Option< + Box egui::TextureId>, + >, #[cfg(feature = "wgpu")] wgpu_render_state: Option, ) -> 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()), diff --git a/crates/eframe/src/native/glow_integration.rs b/crates/eframe/src/native/glow_integration.rs index 8b1f16ec..8dd0f05f 100644 --- a/crates/eframe/src/native/glow_integration.rs +++ b/crates/eframe/src/native/glow_integration.rs @@ -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 diff --git a/crates/eframe/src/native/wgpu_integration.rs b/crates/eframe/src/native/wgpu_integration.rs index 61bf157c..c17bba27 100644 --- a/crates/eframe/src/native/wgpu_integration.rs +++ b/crates/eframe/src/native/wgpu_integration.rs @@ -215,6 +215,8 @@ impl WgpuWinitApp { storage, #[cfg(feature = "glow")] None, + #[cfg(feature = "glow")] + None, wgpu_render_state.clone(), );