egui_glium: allow sharing a native glium texture using Rc
This commit is contained in:
parent
cfb6b31914
commit
cf17cb2065
2 changed files with 19 additions and 22 deletions
|
@ -47,9 +47,11 @@ fn main() {
|
|||
let image = load_glium_image(png_data);
|
||||
let image_size = egui::Vec2::new(image.width as f32, image.height as f32);
|
||||
// Load to gpu memory
|
||||
let native_texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap();
|
||||
let glium_texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap();
|
||||
// Allow us to share the texture with egui:
|
||||
let glium_texture = std::rc::Rc::new(glium_texture);
|
||||
// Allocate egui's texture id for GL texture
|
||||
let texture_id = egui.painter_mut().register_native_texture(native_texture);
|
||||
let texture_id = egui.painter_mut().register_native_texture(glium_texture);
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
let mut redraw = || {
|
||||
|
|
|
@ -13,6 +13,7 @@ use {
|
|||
uniform,
|
||||
uniforms::{MagnifySamplerFilter, SamplerWrapFunction},
|
||||
},
|
||||
std::rc::Rc,
|
||||
};
|
||||
|
||||
pub struct Painter {
|
||||
|
@ -30,8 +31,9 @@ struct UserTexture {
|
|||
/// This is the format glium likes.
|
||||
pixels: Vec<Vec<(u8, u8, u8, u8)>>,
|
||||
|
||||
/// Lazily uploaded
|
||||
gl_texture: Option<SrgbTexture2d>,
|
||||
/// Lazily uploaded from [`Self::pixels`],
|
||||
/// or owned by the user via `register_native_texture`.
|
||||
gl_texture: Option<Rc<SrgbTexture2d>>,
|
||||
}
|
||||
|
||||
impl Painter {
|
||||
|
@ -242,20 +244,9 @@ impl Painter {
|
|||
}
|
||||
|
||||
#[deprecated = "Use: `NativeTexture::register_native_texture` instead"]
|
||||
pub fn register_glium_texture(
|
||||
&mut self,
|
||||
texture: glium::texture::SrgbTexture2d,
|
||||
) -> egui::TextureId {
|
||||
let id = self.alloc_user_texture();
|
||||
if let egui::TextureId::User(id) = id {
|
||||
if let Some(Some(user_texture)) = self.user_textures.get_mut(id as usize) {
|
||||
*user_texture = UserTexture {
|
||||
pixels: vec![],
|
||||
gl_texture: Some(texture),
|
||||
}
|
||||
}
|
||||
}
|
||||
id
|
||||
pub fn register_glium_texture(&mut self, texture: Rc<SrgbTexture2d>) -> egui::TextureId {
|
||||
use epi::NativeTexture as _;
|
||||
self.register_native_texture(texture)
|
||||
}
|
||||
|
||||
pub fn set_user_texture(
|
||||
|
@ -302,7 +293,8 @@ impl Painter {
|
|||
.get(id as usize)?
|
||||
.as_ref()?
|
||||
.gl_texture
|
||||
.as_ref(),
|
||||
.as_ref()
|
||||
.map(|rc| rc.as_ref()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -312,15 +304,18 @@ impl Painter {
|
|||
let pixels = std::mem::take(&mut user_texture.pixels);
|
||||
let format = texture::SrgbFormat::U8U8U8U8;
|
||||
let mipmaps = texture::MipmapsOption::NoMipmap;
|
||||
user_texture.gl_texture =
|
||||
Some(SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap());
|
||||
user_texture.gl_texture = Some(
|
||||
SrgbTexture2d::with_format(facade, pixels, format, mipmaps)
|
||||
.unwrap()
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl epi::NativeTexture for Painter {
|
||||
type Texture = glium::texture::srgb_texture2d::SrgbTexture2d;
|
||||
type Texture = Rc<SrgbTexture2d>;
|
||||
|
||||
fn register_native_texture(&mut self, native: Self::Texture) -> egui::TextureId {
|
||||
let id = self.alloc_user_texture();
|
||||
|
|
Loading…
Reference in a new issue