pub struct TextureCache {
pub width: u32,
pub height: u32,
pub tile_size: u32,
/* private fields */
}Expand description
2D tile-based LRU texture cache.
The simulated texture has width × height texels divided into
tile_size × tile_size tiles. At most capacity tiles reside in the
cache simultaneously.
§Cache line model
Each cache “line” corresponds to one complete tile. On a miss the whole
tile is loaded from simulated main memory. The tile’s data is a flat
Vec<u8> with tile_size * tile_size bytes (one byte per texel).
Fields§
§width: u32Width of the texture in texels.
height: u32Height of the texture in texels.
tile_size: u32Edge length (in texels) of each square tile / cache line.
Implementations§
Source§impl TextureCache
impl TextureCache
Sourcepub fn new(
width: u32,
height: u32,
tile_size: u32,
capacity: usize,
texture_data: Vec<u8>,
) -> Result<Self, CacheError>
pub fn new( width: u32, height: u32, tile_size: u32, capacity: usize, texture_data: Vec<u8>, ) -> Result<Self, CacheError>
Create a new texture cache backed by a flat texture buffer.
texture_data must have exactly width * height bytes. If it is
shorter it is padded with zeros; if longer, the excess is ignored.
§Errors
Returns an error if width, height, tile_size, or capacity is 0.
Sourcepub fn fetch(&mut self, x: u32, y: u32) -> Result<u8, CacheError>
pub fn fetch(&mut self, x: u32, y: u32) -> Result<u8, CacheError>
Fetch the texel value at (x, y).
If the tile containing (x, y) is not in the cache, it is loaded (a
miss); otherwise the cached copy is used (a hit).
§Errors
Returns CacheError::OutOfBounds if x >= width or y >= height.
Sourcepub fn prefetch(&mut self, x: u32, y: u32) -> Result<(), CacheError>
pub fn prefetch(&mut self, x: u32, y: u32) -> Result<(), CacheError>
Prefetch the tile containing (x, y) into the cache.
If the tile is already cached, this is a no-op (the hit counter is not incremented). If it is not cached, it is loaded and the prefetch counter is incremented.
§Errors
Returns CacheError::OutOfBounds if x >= width or y >= height.
Sourcepub fn flush(&mut self)
pub fn flush(&mut self)
Invalidate (evict) all tiles from the cache.
Statistics are preserved; only the tile storage is cleared.
Sourcepub fn invalidate_tile(&mut self, key: TileKey) -> bool
pub fn invalidate_tile(&mut self, key: TileKey) -> bool
Invalidate a specific tile if present.
Returns true if the tile was in the cache and was removed.
Sourcepub fn resident_count(&self) -> usize
pub fn resident_count(&self) -> usize
Number of tiles currently resident in the cache.
Sourcepub fn is_resident(&self, key: TileKey) -> bool
pub fn is_resident(&self, key: TileKey) -> bool
Whether a specific tile is currently resident in the cache.
Sourcepub fn stats(&self) -> &TexCacheStats
pub fn stats(&self) -> &TexCacheStats
A reference to the current aggregate statistics.
Sourcepub fn reset_stats(&mut self)
pub fn reset_stats(&mut self)
Reset all statistics counters to zero.
Auto Trait Implementations§
impl Freeze for TextureCache
impl RefUnwindSafe for TextureCache
impl Send for TextureCache
impl Sync for TextureCache
impl Unpin for TextureCache
impl UnsafeUnpin for TextureCache
impl UnwindSafe for TextureCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more