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

Make image Cache eviction strategy less aggressive in iced_wgpu #2403

Merged
merged 1 commit into from
Apr 24, 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
15 changes: 11 additions & 4 deletions wgpu/src/image/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl Atlas {
entry
};

log::info!("Allocated atlas entry: {entry:?}");
log::debug!("Allocated atlas entry: {entry:?}");

// It is a webgpu requirement that:
// BufferCopyView.layout.bytes_per_row % wgpu::COPY_BYTES_PER_ROW_ALIGNMENT == 0
Expand Down Expand Up @@ -147,13 +147,20 @@ impl Atlas {
}
}

log::info!("Current atlas: {self:?}");
if log::log_enabled!(log::Level::Debug) {
log::debug!(
"Atlas layers: {} (busy: {}, allocations: {})",
self.layer_count(),
self.layers.iter().filter(|layer| !layer.is_empty()).count(),
self.layers.iter().map(Layer::allocations).sum::<usize>(),
);
}

Some(entry)
}

pub fn remove(&mut self, entry: &Entry) {
log::info!("Removing atlas entry: {entry:?}");
log::debug!("Removing atlas entry: {entry:?}");

match entry {
Entry::Contiguous(allocation) => {
Expand Down Expand Up @@ -266,7 +273,7 @@ impl Atlas {
}

fn deallocate(&mut self, allocation: &Allocation) {
log::info!("Deallocating atlas: {allocation:?}");
log::debug!("Deallocating atlas: {allocation:?}");

match allocation {
Allocation::Full { layer } => {
Expand Down
4 changes: 4 additions & 0 deletions wgpu/src/image/atlas/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ impl Allocator {
pub fn is_empty(&self) -> bool {
self.allocations == 0
}

pub fn allocations(&self) -> usize {
self.allocations
}
}

pub struct Region {
Expand Down
8 changes: 8 additions & 0 deletions wgpu/src/image/atlas/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ impl Layer {
pub fn is_empty(&self) -> bool {
matches!(self, Layer::Empty)
}

pub fn allocations(&self) -> usize {
match self {
Layer::Empty => 0,
Layer::Busy(allocator) => allocator.allocations(),
Layer::Full => 1,
}
}
}
2 changes: 1 addition & 1 deletion wgpu/src/image/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl Pipeline {
let texture_version = cache.layer_count();

if self.texture_version != texture_version {
log::info!("Atlas has grown. Recreating bind group...");
log::debug!("Atlas has grown. Recreating bind group...");

self.texture =
cache.create_bind_group(device, &self.texture_layout);
Expand Down
9 changes: 9 additions & 0 deletions wgpu/src/image/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl Memory {
pub struct Cache {
map: FxHashMap<u64, Memory>,
hits: FxHashSet<u64>,
should_trim: bool,
}

impl Cache {
Expand All @@ -55,6 +56,8 @@ impl Cache {
Err(_) => Memory::Invalid,
};

self.should_trim = true;

self.insert(handle, memory);
self.get(handle).unwrap()
}
Expand Down Expand Up @@ -86,6 +89,11 @@ impl Cache {

/// Trim cache misses from cache
pub fn trim(&mut self, atlas: &mut Atlas) {
// Only trim if new entries have landed in the `Cache`
if !self.should_trim {
return;
}

let hits = &self.hits;

self.map.retain(|k, memory| {
Expand All @@ -101,6 +109,7 @@ impl Cache {
});

self.hits.clear();
self.should_trim = false;
}

fn get(&mut self, handle: &image::Handle) -> Option<&mut Memory> {
Expand Down
8 changes: 8 additions & 0 deletions wgpu/src/image/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct Cache {
rasterized: FxHashMap<(u64, u32, u32, ColorFilter), atlas::Entry>,
svg_hits: FxHashSet<u64>,
rasterized_hits: FxHashSet<(u64, u32, u32, ColorFilter)>,
should_trim: bool,
}

type ColorFilter = Option<[u8; 4]>;
Expand Down Expand Up @@ -76,6 +77,8 @@ impl Cache {
}
}

self.should_trim = true;

let _ = self.svgs.insert(handle.id(), svg);
self.svgs.get(&handle.id()).unwrap()
}
Expand Down Expand Up @@ -176,6 +179,10 @@ impl Cache {

/// Load svg and upload raster data
pub fn trim(&mut self, atlas: &mut Atlas) {
if !self.should_trim {
return;
}

let svg_hits = &self.svg_hits;
let rasterized_hits = &self.rasterized_hits;

Expand All @@ -191,6 +198,7 @@ impl Cache {
});
self.svg_hits.clear();
self.rasterized_hits.clear();
self.should_trim = false;
}
}

Expand Down
Loading