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 Options::reduce_texture_memory to free up RAM #4431

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -235,6 +235,19 @@ pub struct Options {

/// Controls the speed at which we zoom in when doing ctrl/cmd + scroll.
pub scroll_zoom_speed: f32,

/// 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 @@ -260,6 +273,7 @@ impl Default for Options {
// Input:
line_scroll_speed,
scroll_zoom_speed: 1.0 / 200.0,
reduce_texture_memory: false,
}
}
}
Expand All @@ -279,6 +293,7 @@ impl Options {

line_scroll_speed,
scroll_zoom_speed,
reduce_texture_memory,
} = self;

use crate::Widget as _;
Expand All @@ -297,6 +312,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
Loading