fixed uncontrollable growth of dynamic surface cache

This commit is contained in:
Dmitry Stepanov
2026-07-04 20:52:22 +03:00
parent 1ece798586
commit 60d316fa71
2 changed files with 23 additions and 4 deletions
+22 -4
View File
@@ -29,6 +29,7 @@ use crate::{
},
};
use fxhash::FxHashMap;
use fyrox_resource::entry::TimedEntry;
use fyrox_resource::untyped::ResourceKind;
use std::{
ops::{Deref, DerefMut},
@@ -223,10 +224,12 @@ impl<T> TemporaryCache<T> {
/// as 2D sprites, tile maps, etc.
#[derive(Default)]
pub struct DynamicSurfaceCache {
cache: FxHashMap<u64, SurfaceResource>,
cache: FxHashMap<u64, TimedEntry<SurfaceResource>>,
}
impl DynamicSurfaceCache {
const TTL: f32 = 1.0;
/// Creates a new empty cache.
pub fn new() -> Self {
Self::default()
@@ -239,8 +242,9 @@ impl DynamicSurfaceCache {
unique_id: u64,
layout: &[VertexAttributeDescriptor],
) -> SurfaceResource {
if let Some(surface) = self.cache.get(&unique_id) {
surface.clone()
if let Some(surface) = self.cache.get_mut(&unique_id) {
surface.time_to_live = Self::TTL;
surface.value.clone()
} else {
let default_capacity = 4096;
@@ -261,7 +265,13 @@ impl DynamicSurfaceCache {
SurfaceData::new(vertex_buffer, triangle_buffer),
);
self.cache.insert(unique_id, surface.clone());
self.cache.insert(
unique_id,
TimedEntry {
value: surface.clone(),
time_to_live: Self::TTL,
},
);
surface
}
@@ -275,4 +285,12 @@ impl DynamicSurfaceCache {
surface_data.geometry_buffer.modify().clear();
}
}
/// Updates the cache entries and removes ones that wasn't used for some time.
pub fn update(&mut self, dt: f32) {
self.cache.retain(|_, entry| {
entry.time_to_live -= dt;
entry.time_to_live > 0.0
})
}
}
+1
View File
@@ -924,6 +924,7 @@ impl Renderer {
self.update_texture_cache(resource_manager, dt);
self.update_shader_cache(dt);
self.geometry_cache.update(dt);
self.dynamic_surface_cache.update(dt);
}
fn render_scene_observer(