Bug 1705024 - Evict unused cached render tasks after 10 frames. r=gfx-reviewers,gw

Cached render tasks are usually much cheaper to produce than texture uploads so it's good to make sure lingering unused render tasks don't create unnecessary pressure on the texture cache. It also avoids things like animated gradients from filling the texture cache in a second.

Depends on D115206

Differential Revision: https://phabricator.services.mozilla.com/D115207
This commit is contained in:
Nicolas Silva 2021-05-19 08:05:33 +00:00
Родитель ce66e50500
Коммит dba96d50db
1 изменённых файлов: 18 добавлений и 1 удалений

Просмотреть файл

@ -69,6 +69,7 @@ pub struct RenderTaskCacheEntry {
user_data: Option<[f32; 4]>,
target_kind: RenderTargetKind,
is_opaque: bool,
frame_id: u64,
pub handle: TextureCacheHandle,
/// If a render task was generated for this cache entry on _this_ frame,
/// we need to track the task id here. This allows us to hook it up as
@ -89,6 +90,7 @@ pub enum RenderTaskCacheMarker {}
pub struct RenderTaskCache {
map: FastHashMap<RenderTaskCacheKey, FreeListHandle<RenderTaskCacheMarker>>,
cache_entries: FreeList<RenderTaskCacheEntry, RenderTaskCacheMarker>,
frame_id: u64,
}
pub type RenderTaskCacheEntryHandle = WeakFreeListHandle<RenderTaskCacheMarker>;
@ -98,6 +100,7 @@ impl RenderTaskCache {
RenderTaskCache {
map: FastHashMap::default(),
cache_entries: FreeList::new(),
frame_id: 0,
}
}
@ -110,6 +113,7 @@ impl RenderTaskCache {
&mut self,
texture_cache: &mut TextureCache,
) {
self.frame_id += 1;
profile_scope!("begin_frame");
// Drop any items from the cache that have been
// evicted from the texture cache.
@ -125,15 +129,25 @@ impl RenderTaskCache {
// from here so that this hash map doesn't
// grow indefinitely!
let cache_entries = &mut self.cache_entries;
let frame_id = self.frame_id;
self.map.retain(|_, handle| {
let retain = texture_cache.is_allocated(
let mut retain = texture_cache.is_allocated(
&cache_entries.get(handle).handle,
);
if retain {
let entry = cache_entries.get_mut(&handle);
if frame_id > entry.frame_id + 10 {
texture_cache.evict_handle(&entry.handle);
retain = false;
}
}
if !retain {
let handle = mem::replace(handle, FreeListHandle::invalid());
cache_entries.free(handle);
}
retain
});
@ -223,6 +237,7 @@ impl RenderTaskCache {
where
F: FnOnce(&mut RenderTaskGraphBuilder) -> Result<RenderTaskId, ()>,
{
let frame_id = self.frame_id;
let size = key.size;
// Get the texture cache handle for this cache key,
// or create one.
@ -233,11 +248,13 @@ impl RenderTaskCache {
user_data,
target_kind: RenderTargetKind::Color, // will be set below.
is_opaque,
frame_id,
render_task_id: None,
};
cache_entries.insert(entry)
});
let cache_entry = cache_entries.get_mut(entry_handle);
cache_entry.frame_id = self.frame_id;
// Check if this texture cache handle is valid.
if texture_cache.request(&cache_entry.handle, gpu_cache) {