From 7bd6f83f18af7317f75e7a32413a9a9eb747db7f Mon Sep 17 00:00:00 2001 From: Rusty-Cube <183405087+Rusty-Cube@users.noreply.github.com> Date: Sun, 6 Oct 2024 20:50:10 +0200 Subject: [PATCH] Free textures after submitting queue instead of before with wgpu renderer (#5226) * Closes #5224 I'm unfamiliar with wgpu, so I'd like someone to confirm, that calling `wgpu::Texture` _after_ `wgpu::Queue::submit` is in fact the right thing to do. --------- Co-authored-by: Andreas Reich --- crates/egui-wgpu/src/winit.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/crates/egui-wgpu/src/winit.rs b/crates/egui-wgpu/src/winit.rs index 26b3c86a..189afbf9 100644 --- a/crates/egui-wgpu/src/winit.rs +++ b/crates/egui-wgpu/src/winit.rs @@ -668,13 +668,6 @@ impl Painter { ); } - { - let mut renderer = render_state.renderer.write(); - for id in &textures_delta.free { - renderer.free_texture(id); - } - } - let encoded = { crate::profile_scope!("CommandEncoder::finish"); encoder.finish() @@ -691,6 +684,16 @@ impl Painter { vsync_sec += start.elapsed().as_secs_f32(); }; + // Free textures marked for destruction **after** queue submit since they might still be used in the current frame. + // Calling `wgpu::Texture::destroy` on a texture that is still in use would invalidate the command buffer(s) it is used in. + // However, once we called `wgpu::Queue::submit`, it is up for wgpu to determine how long the underlying gpu resource has to live. + { + let mut renderer = render_state.renderer.write(); + for id in &textures_delta.free { + renderer.free_texture(id); + } + } + let screenshot = if capture { self.screen_capture_state .as_ref()