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 = load_glium_image(png_data);
|
||||||
let image_size = egui::Vec2::new(image.width as f32, image.height as f32);
|
let image_size = egui::Vec2::new(image.width as f32, image.height as f32);
|
||||||
// Load to gpu memory
|
// 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
|
// 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| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
let mut redraw = || {
|
let mut redraw = || {
|
||||||
|
|
|
@ -13,6 +13,7 @@ use {
|
||||||
uniform,
|
uniform,
|
||||||
uniforms::{MagnifySamplerFilter, SamplerWrapFunction},
|
uniforms::{MagnifySamplerFilter, SamplerWrapFunction},
|
||||||
},
|
},
|
||||||
|
std::rc::Rc,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Painter {
|
pub struct Painter {
|
||||||
|
@ -30,8 +31,9 @@ struct UserTexture {
|
||||||
/// This is the format glium likes.
|
/// This is the format glium likes.
|
||||||
pixels: Vec<Vec<(u8, u8, u8, u8)>>,
|
pixels: Vec<Vec<(u8, u8, u8, u8)>>,
|
||||||
|
|
||||||
/// Lazily uploaded
|
/// Lazily uploaded from [`Self::pixels`],
|
||||||
gl_texture: Option<SrgbTexture2d>,
|
/// or owned by the user via `register_native_texture`.
|
||||||
|
gl_texture: Option<Rc<SrgbTexture2d>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Painter {
|
impl Painter {
|
||||||
|
@ -242,20 +244,9 @@ impl Painter {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deprecated = "Use: `NativeTexture::register_native_texture` instead"]
|
#[deprecated = "Use: `NativeTexture::register_native_texture` instead"]
|
||||||
pub fn register_glium_texture(
|
pub fn register_glium_texture(&mut self, texture: Rc<SrgbTexture2d>) -> egui::TextureId {
|
||||||
&mut self,
|
use epi::NativeTexture as _;
|
||||||
texture: glium::texture::SrgbTexture2d,
|
self.register_native_texture(texture)
|
||||||
) -> 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 set_user_texture(
|
pub fn set_user_texture(
|
||||||
|
@ -302,7 +293,8 @@ impl Painter {
|
||||||
.get(id as usize)?
|
.get(id as usize)?
|
||||||
.as_ref()?
|
.as_ref()?
|
||||||
.gl_texture
|
.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 pixels = std::mem::take(&mut user_texture.pixels);
|
||||||
let format = texture::SrgbFormat::U8U8U8U8;
|
let format = texture::SrgbFormat::U8U8U8U8;
|
||||||
let mipmaps = texture::MipmapsOption::NoMipmap;
|
let mipmaps = texture::MipmapsOption::NoMipmap;
|
||||||
user_texture.gl_texture =
|
user_texture.gl_texture = Some(
|
||||||
Some(SrgbTexture2d::with_format(facade, pixels, format, mipmaps).unwrap());
|
SrgbTexture2d::with_format(facade, pixels, format, mipmaps)
|
||||||
|
.unwrap()
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl epi::NativeTexture for Painter {
|
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 {
|
fn register_native_texture(&mut self, native: Self::Texture) -> egui::TextureId {
|
||||||
let id = self.alloc_user_texture();
|
let id = self.alloc_user_texture();
|
||||||
|
|
Loading…
Reference in a new issue