Skip to content

Commit

Permalink
egui: Added reduce_texture_memory to Options
Browse files Browse the repository at this point in the history
If `true`, `egui` will discard the loaded image data after
the texture is loaded onto the GPU to reduce memory usage.
  • Loading branch information
varphone committed Apr 29, 2024
1 parent 2fabde1 commit 5d5e222
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions crates/egui/src/load/texture_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ impl TextureLoader for DefaultTextureLoader {
let handle = ctx.load_texture(uri, image, texture_options);
let texture = SizedTexture::from_handle(&handle);
cache.insert((uri.into(), texture_options), handle);
let reduce_texture_memory = ctx.options(|o| o.reduce_texture_memory);
if reduce_texture_memory {
let loaders = ctx.loaders();
loaders.include.forget(uri);
for loader in loaders.bytes.lock().iter().rev() {
loader.forget(uri);
}
for loader in loaders.image.lock().iter().rev() {
loader.forget(uri);
}
}
Ok(TexturePoll::Ready { texture })
}
}
Expand Down
17 changes: 17 additions & 0 deletions crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ pub struct Options {
///
/// By default this is `true` in debug builds.
pub warn_on_id_clash: bool,

/// If `true`, `egui` will discard the loaded image data after
/// the texture is loaded onto the GPU to reduce memory usage.
///
/// In modern GPU rendering, the texture data is not required after the texture is loaded.
///
/// This is beneficial when using a large number or resolution of images and there is no need to
/// retain the image data, potentially saving a significant amount of memory.
///
/// The drawback is that it becomes impossible to serialize the loaded images or render in non-GPU systems.
///
/// Default is `false`.
pub reduce_texture_memory: bool,
}

impl Default for Options {
Expand All @@ -240,6 +253,7 @@ impl Default for Options {
screen_reader: false,
preload_font_glyphs: true,
warn_on_id_clash: cfg!(debug_assertions),
reduce_texture_memory: false,
}
}
}
Expand All @@ -256,6 +270,7 @@ impl Options {
screen_reader: _, // needs to come from the integration
preload_font_glyphs: _,
warn_on_id_clash,
reduce_texture_memory,
} = self;

use crate::Widget as _;
Expand All @@ -274,6 +289,8 @@ impl Options {
);

ui.checkbox(warn_on_id_clash, "Warn if two widgets have the same Id");

ui.checkbox(reduce_texture_memory, "Reduce texture memory");
});

use crate::containers::*;
Expand Down
5 changes: 5 additions & 0 deletions crates/egui_demo_app/src/apps/image_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ impl eframe::App for ImageViewer {
ui.add_space(5.0);
ui.label("Aspect ratio is maintained by scaling both sides as necessary");
ui.checkbox(&mut self.maintain_aspect_ratio, "Maintain aspect ratio");

// forget all images
if ui.button("Forget all images").clicked() {
ui.ctx().forget_all_images();
}
});

egui::CentralPanel::default().show(ctx, |ui| {
Expand Down

0 comments on commit 5d5e222

Please sign in to comment.