Invalidate text uploads after atlas trimming

This commit is contained in:
Héctor Ramón Jiménez 2024-04-30 21:59:46 +02:00
parent cfe4ddb866
commit c51b85e7ab
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
4 changed files with 30 additions and 13 deletions

View file

@ -10,3 +10,4 @@ iced.workspace = true
iced.features = ["canvas", "tokio", "debug"] iced.features = ["canvas", "tokio", "debug"]
rand = "0.8" rand = "0.8"
tracing-subscriber = "0.3"

View file

@ -9,6 +9,8 @@ use iced::{
use std::cell::RefCell; use std::cell::RefCell;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
tracing_subscriber::fmt::init();
iced::program("The Matrix - Iced", TheMatrix::update, TheMatrix::view) iced::program("The Matrix - Iced", TheMatrix::update, TheMatrix::view)
.subscription(TheMatrix::subscription) .subscription(TheMatrix::subscription)
.antialiasing(true) .antialiasing(true)

View file

@ -71,6 +71,7 @@ struct Upload {
buffer_cache: BufferCache, buffer_cache: BufferCache,
transformation: Transformation, transformation: Transformation,
version: usize, version: usize,
group_version: usize,
text: rc::Weak<[Text]>, text: rc::Weak<[Text]>,
_atlas: rc::Weak<()>, _atlas: rc::Weak<()>,
} }
@ -83,8 +84,9 @@ pub struct Storage {
struct Group { struct Group {
atlas: glyphon::TextAtlas, atlas: glyphon::TextAtlas,
previous_uploads: usize, version: usize,
handle: Rc<()>, should_trim: bool,
handle: Rc<()>, // Keeps track of active uploads
} }
impl Storage { impl Storage {
@ -117,13 +119,18 @@ impl Storage {
let group_count = self.groups.len(); let group_count = self.groups.len();
let group = self.groups.entry(cache.group).or_insert_with(|| { let group = self.groups.entry(cache.group).or_insert_with(|| {
log::info!("New text atlas created (total: {})", group_count + 1); log::debug!(
"New text atlas: {:?} (total: {})",
cache.group,
group_count + 1
);
Group { Group {
atlas: glyphon::TextAtlas::with_color_mode( atlas: glyphon::TextAtlas::with_color_mode(
device, queue, format, COLOR_MODE, device, queue, format, COLOR_MODE,
), ),
previous_uploads: 0, version: 0,
should_trim: false,
handle: Rc::new(()), handle: Rc::new(()),
} }
}); });
@ -134,6 +141,7 @@ impl Storage {
if !cache.text.is_empty() if !cache.text.is_empty()
&& (upload.version != cache.version && (upload.version != cache.version
|| upload.group_version != group.version
|| upload.transformation != new_transformation) || upload.transformation != new_transformation)
{ {
let _ = prepare( let _ = prepare(
@ -151,9 +159,11 @@ impl Storage {
upload.text = Rc::downgrade(&cache.text); upload.text = Rc::downgrade(&cache.text);
upload.version = cache.version; upload.version = cache.version;
upload.group_version = group.version;
upload.transformation = new_transformation; upload.transformation = new_transformation;
upload.buffer_cache.trim(); upload.buffer_cache.trim();
group.should_trim = true;
} }
} }
hash_map::Entry::Vacant(entry) => { hash_map::Entry::Vacant(entry) => {
@ -184,11 +194,12 @@ impl Storage {
buffer_cache, buffer_cache,
transformation: new_transformation, transformation: new_transformation,
version: 0, version: 0,
group_version: group.version,
text: Rc::downgrade(&cache.text), text: Rc::downgrade(&cache.text),
_atlas: Rc::downgrade(&group.handle), _atlas: Rc::downgrade(&group.handle),
}); });
log::info!( log::debug!(
"New text upload: {} (total: {})", "New text upload: {} (total: {})",
cache.id.0, cache.id.0,
self.uploads.len() self.uploads.len()
@ -201,18 +212,21 @@ impl Storage {
self.uploads self.uploads
.retain(|_id, upload| upload.text.strong_count() > 0); .retain(|_id, upload| upload.text.strong_count() > 0);
self.groups.retain(|_id, group| { self.groups.retain(|id, group| {
let uploads_alive = Rc::weak_count(&group.handle); if Rc::weak_count(&group.handle) == 0 {
log::debug!("Dropping text atlas: {id:?}");
if uploads_alive == 0 {
return false; return false;
} }
if uploads_alive < group.previous_uploads { if group.should_trim {
group.atlas.trim(); log::debug!("Trimming text atlas: {id:?}");
}
group.previous_uploads = uploads_alive; group.atlas.trim();
group.version += 1;
group.should_trim = false;
}
true true
}); });

View file

@ -138,7 +138,7 @@ impl Storage {
batch: Rc::downgrade(&cache.batch), batch: Rc::downgrade(&cache.batch),
}); });
log::info!( log::debug!(
"New mesh upload: {} (total: {})", "New mesh upload: {} (total: {})",
cache.id.0, cache.id.0,
self.uploads.len() self.uploads.len()