Skip to content

Commit

Permalink
Add register_native_texture in eframe::Frame (#4246)
Browse files Browse the repository at this point in the history
<!--
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 #4243

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
  • Loading branch information
Chaojimengnan and emilk authored Mar 27, 2024
1 parent 3c029a4 commit 570e7cf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
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_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`].
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

0 comments on commit 570e7cf

Please sign in to comment.