From 35010049f6f1b0fdfee153a9c6a20a381da8c9cb Mon Sep 17 00:00:00 2001 From: Lily Lyons Date: Thu, 30 Nov 2023 00:50:12 -0800 Subject: [PATCH] fix(image cache): :bug: properly unload textures --- crates/components/src/map_view.rs | 14 ++++---------- crates/components/src/tilepicker.rs | 2 -- crates/graphics/src/atlas_cache.rs | 2 +- crates/graphics/src/event.rs | 1 - crates/graphics/src/map.rs | 2 -- crates/graphics/src/plane.rs | 1 - crates/graphics/src/sprite/mod.rs | 1 - crates/graphics/src/texture_loader.rs | 14 +++++++++++++- crates/graphics/src/tiles/atlas.rs | 2 +- crates/graphics/src/tiles/mod.rs | 1 - 10 files changed, 19 insertions(+), 21 deletions(-) diff --git a/crates/components/src/map_view.rs b/crates/components/src/map_view.rs index fa2294e2..dbafdb7d 100644 --- a/crates/components/src/map_view.rs +++ b/crates/components/src/map_view.rs @@ -17,7 +17,6 @@ use itertools::Itertools; -#[derive(Debug)] pub struct MapView { /// Toggle to display the visible region in-game. pub visible_display: bool, @@ -92,25 +91,20 @@ impl MapView { let events = map .events .iter() - .map(|(id, e)| { + .map(|(id, e)| -> anyhow::Result<_> { let sprite = luminol_graphics::Event::new( &update_state.graphics, update_state.filesystem, e, &atlas, - ); + )?; let preview_sprite = luminol_graphics::Event::new( &update_state.graphics, update_state.filesystem, e, &atlas, - ); - let Ok(sprite) = sprite else { - return Err(sprite.unwrap_err()); - }; - let Ok(preview_sprite) = preview_sprite else { - return Err(preview_sprite.unwrap_err()); - }; + )?; + Ok(if let Some(sprite) = sprite { preview_sprite.map(|preview_sprite| (id, (sprite, preview_sprite))) } else { diff --git a/crates/components/src/tilepicker.rs b/crates/components/src/tilepicker.rs index 2adbd2fb..d090ec94 100644 --- a/crates/components/src/tilepicker.rs +++ b/crates/components/src/tilepicker.rs @@ -19,7 +19,6 @@ use itertools::Itertools; use std::sync::Arc; use std::time::Duration; -#[derive(Debug)] pub struct Tilepicker { pub selected_tiles_left: i16, pub selected_tiles_top: i16, @@ -33,7 +32,6 @@ pub struct Tilepicker { ani_time: Option, } -#[derive(Debug)] struct Resources { tiles: luminol_graphics::tiles::Tiles, collision: luminol_graphics::collision::Collision, diff --git a/crates/graphics/src/atlas_cache.rs b/crates/graphics/src/atlas_cache.rs index 5617df05..eb7302fa 100644 --- a/crates/graphics/src/atlas_cache.rs +++ b/crates/graphics/src/atlas_cache.rs @@ -16,7 +16,7 @@ // along with Luminol. If not, see . use crate::{tiles::Atlas, GraphicsState}; -#[derive(Default, Debug)] +#[derive(Default)] pub struct Cache { atlases: dashmap::DashMap, } diff --git a/crates/graphics/src/event.rs b/crates/graphics/src/event.rs index 677e8c45..21c1304c 100644 --- a/crates/graphics/src/event.rs +++ b/crates/graphics/src/event.rs @@ -19,7 +19,6 @@ use std::sync::Arc; use crate::{quad::Quad, sprite::Sprite, tiles::Atlas, viewport::Viewport, GraphicsState}; -#[derive(Debug)] pub struct Event { sprite: Arc, viewport: Arc, diff --git a/crates/graphics/src/map.rs b/crates/graphics/src/map.rs index fda93d3d..f8b44fb1 100644 --- a/crates/graphics/src/map.rs +++ b/crates/graphics/src/map.rs @@ -21,7 +21,6 @@ use std::time::Duration; use crate::{collision::Collision, tiles::Tiles, viewport::Viewport, GraphicsState, Plane}; -#[derive(Debug)] pub struct Map { resources: Arc, viewport: Arc, @@ -33,7 +32,6 @@ pub struct Map { pub enabled_layers: Vec, } -#[derive(Debug)] struct Resources { tiles: Tiles, panorama: Option, diff --git a/crates/graphics/src/plane.rs b/crates/graphics/src/plane.rs index 4836a879..bab294b2 100644 --- a/crates/graphics/src/plane.rs +++ b/crates/graphics/src/plane.rs @@ -18,7 +18,6 @@ use crate::{quad::Quad, sprite::Sprite, viewport::Viewport, GraphicsState, Texture}; use std::sync::Arc; -#[derive(Debug)] pub struct Plane { sprite: Sprite, } diff --git a/crates/graphics/src/sprite/mod.rs b/crates/graphics/src/sprite/mod.rs index 04968cae..73ea0a02 100644 --- a/crates/graphics/src/sprite/mod.rs +++ b/crates/graphics/src/sprite/mod.rs @@ -26,7 +26,6 @@ pub(crate) mod graphic; pub(crate) mod shader; mod vertices; -#[derive(Debug)] pub struct Sprite { pub texture: Arc, pub graphic: graphic::Graphic, diff --git a/crates/graphics/src/texture_loader.rs b/crates/graphics/src/texture_loader.rs index 22425ba6..4c18f0e8 100644 --- a/crates/graphics/src/texture_loader.rs +++ b/crates/graphics/src/texture_loader.rs @@ -39,11 +39,19 @@ pub struct TextureLoader { render_state: egui_wgpu::RenderState, } -#[derive(Debug)] pub struct Texture { pub texture: wgpu::Texture, pub view: wgpu::TextureView, pub texture_id: egui::TextureId, + + render_state: egui_wgpu::RenderState, +} + +impl Drop for Texture { + fn drop(&mut self) { + let mut renderer = self.render_state.renderer.write(); + renderer.free_texture(&self.texture_id); + } } pub const TEXTURE_LOADER_ID: &str = egui::load::generate_loader_id!(TextureLoader); @@ -165,6 +173,8 @@ impl TextureLoader { texture, view, texture_id, + + render_state: self.render_state.clone(), }), ); } @@ -227,6 +237,8 @@ impl TextureLoader { texture, view, texture_id, + + render_state: self.render_state.clone(), }); self.loaded_textures.insert(path, texture.clone()); texture diff --git a/crates/graphics/src/tiles/atlas.rs b/crates/graphics/src/tiles/atlas.rs index 98068844..cda0398d 100644 --- a/crates/graphics/src/tiles/atlas.rs +++ b/crates/graphics/src/tiles/atlas.rs @@ -43,7 +43,7 @@ pub const AUTOTILE_FRAME_WIDTH: u32 = AUTOTILE_FRAME_COLS * TILE_SIZE; // This i use image::GenericImageView; use std::sync::Arc; -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct Atlas { pub atlas_texture: Arc, pub autotile_width: u32, diff --git a/crates/graphics/src/tiles/mod.rs b/crates/graphics/src/tiles/mod.rs index c9dcf038..74d62f99 100644 --- a/crates/graphics/src/tiles/mod.rs +++ b/crates/graphics/src/tiles/mod.rs @@ -35,7 +35,6 @@ mod instance; pub(crate) mod opacity; pub(crate) mod shader; -#[derive(Debug)] pub struct Tiles { pub autotiles: Autotiles, pub atlas: Atlas,