Update egui (#264)

This commit is contained in:
Jay Oster 2022-03-20 13:24:20 -07:00 committed by GitHub
parent 94a2cc2dbd
commit ebd2bdbd04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 15 deletions

View file

@ -10,9 +10,9 @@ optimize = ["log/release_max_level_warn"]
default = ["optimize"] default = ["optimize"]
[dependencies] [dependencies]
egui = "0.16" egui = "0.17"
egui_wgpu_backend = "0.16" egui_wgpu_backend = "0.17"
egui-winit = { version = "0.16", default-features = false, features = ["links"] } egui-winit = { version = "0.17", default-features = false, features = ["links"] }
env_logger = "0.9" env_logger = "0.9"
log = "0.4" log = "0.4"
pixels = { path = "../.." } pixels = { path = "../.." }

View file

@ -1,4 +1,4 @@
use egui::{ClippedMesh, CtxRef}; use egui::{ClippedMesh, Context, TexturesDelta};
use egui_wgpu_backend::{BackendError, RenderPass, ScreenDescriptor}; use egui_wgpu_backend::{BackendError, RenderPass, ScreenDescriptor};
use pixels::{wgpu, PixelsContext}; use pixels::{wgpu, PixelsContext};
use winit::window::Window; use winit::window::Window;
@ -6,11 +6,12 @@ use winit::window::Window;
/// Manages all state required for rendering egui over `Pixels`. /// Manages all state required for rendering egui over `Pixels`.
pub(crate) struct Framework { pub(crate) struct Framework {
// State for egui. // State for egui.
egui_ctx: CtxRef, egui_ctx: Context,
egui_state: egui_winit::State, egui_state: egui_winit::State,
screen_descriptor: ScreenDescriptor, screen_descriptor: ScreenDescriptor,
rpass: RenderPass, rpass: RenderPass,
paint_jobs: Vec<ClippedMesh>, paint_jobs: Vec<ClippedMesh>,
textures: TexturesDelta,
// State for the GUI // State for the GUI
gui: Gui, gui: Gui,
@ -25,14 +26,17 @@ struct Gui {
impl Framework { impl Framework {
/// Create egui. /// Create egui.
pub(crate) fn new(width: u32, height: u32, scale_factor: f32, pixels: &pixels::Pixels) -> Self { pub(crate) fn new(width: u32, height: u32, scale_factor: f32, pixels: &pixels::Pixels) -> Self {
let egui_ctx = CtxRef::default(); let max_texture_size = pixels.device().limits().max_texture_dimension_2d as usize;
let egui_state = egui_winit::State::from_pixels_per_point(scale_factor);
let egui_ctx = Context::default();
let egui_state = egui_winit::State::from_pixels_per_point(max_texture_size, scale_factor);
let screen_descriptor = ScreenDescriptor { let screen_descriptor = ScreenDescriptor {
physical_width: width, physical_width: width,
physical_height: height, physical_height: height,
scale_factor, scale_factor,
}; };
let rpass = RenderPass::new(pixels.device(), pixels.render_texture_format(), 1); let rpass = RenderPass::new(pixels.device(), pixels.render_texture_format(), 1);
let textures = TexturesDelta::default();
let gui = Gui::new(); let gui = Gui::new();
Self { Self {
@ -41,6 +45,7 @@ impl Framework {
screen_descriptor, screen_descriptor,
rpass, rpass,
paint_jobs: Vec::new(), paint_jobs: Vec::new(),
textures,
gui, gui,
} }
} }
@ -67,14 +72,15 @@ impl Framework {
pub(crate) fn prepare(&mut self, window: &Window) { pub(crate) fn prepare(&mut self, window: &Window) {
// Run the egui frame and create all paint jobs to prepare for rendering. // Run the egui frame and create all paint jobs to prepare for rendering.
let raw_input = self.egui_state.take_egui_input(window); let raw_input = self.egui_state.take_egui_input(window);
let (output, paint_commands) = self.egui_ctx.run(raw_input, |egui_ctx| { let output = self.egui_ctx.run(raw_input, |egui_ctx| {
// Draw the demo application. // Draw the demo application.
self.gui.ui(egui_ctx); self.gui.ui(egui_ctx);
}); });
self.textures.append(output.textures_delta);
self.egui_state self.egui_state
.handle_output(window, &self.egui_ctx, output); .handle_platform_output(window, &self.egui_ctx, output.platform_output);
self.paint_jobs = self.egui_ctx.tessellate(paint_commands); self.paint_jobs = self.egui_ctx.tessellate(output.shapes);
} }
/// Render egui. /// Render egui.
@ -86,9 +92,7 @@ impl Framework {
) -> Result<(), BackendError> { ) -> Result<(), BackendError> {
// Upload all resources to the GPU. // Upload all resources to the GPU.
self.rpass self.rpass
.update_texture(&context.device, &context.queue, &self.egui_ctx.font_image()); .add_textures(&context.device, &context.queue, &self.textures)?;
self.rpass
.update_user_textures(&context.device, &context.queue);
self.rpass.update_buffers( self.rpass.update_buffers(
&context.device, &context.device,
&context.queue, &context.queue,
@ -103,7 +107,11 @@ impl Framework {
&self.paint_jobs, &self.paint_jobs,
&self.screen_descriptor, &self.screen_descriptor,
None, None,
) )?;
// Cleanup
let textures = std::mem::take(&mut self.textures);
self.rpass.remove_textures(textures)
} }
} }
@ -114,7 +122,7 @@ impl Gui {
} }
/// Create the UI using egui. /// Create the UI using egui.
fn ui(&mut self, ctx: &CtxRef) { fn ui(&mut self, ctx: &Context) {
egui::TopBottomPanel::top("menubar_container").show(ctx, |ui| { egui::TopBottomPanel::top("menubar_container").show(ctx, |ui| {
egui::menu::bar(ui, |ui| { egui::menu::bar(ui, |ui| {
ui.menu_button("File", |ui| { ui.menu_button("File", |ui| {