Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add register_native_texture in eframe::Frame #4246

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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_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`].
Expand Down
5 changes: 5 additions & 0 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()),
Expand Down
6 changes: 5 additions & 1 deletion crates/eframe/src/native/glow_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions crates/eframe/src/native/wgpu_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ impl WgpuWinitApp {
storage,
#[cfg(feature = "glow")]
None,
#[cfg(feature = "glow")]
None,
wgpu_render_state.clone(),
);

Expand Down
Loading