use crate::cache::{Cache, HasSize, HashLookup, Lru, MaxSize};
use crate::draw::texture::{ColorFormat, Texture};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TextureKey {
ptr: usize,
len: usize,
width: u16,
height: u16,
format: ColorFormat,
}
impl TextureKey {
pub fn from(src: &Texture) -> Self {
let buf = src.buf.as_slice();
Self {
ptr: buf.as_ptr() as usize,
len: buf.len(),
width: src.width,
height: src.height,
format: src.format,
}
}
}
pub struct CachedTexture(pub wgpu::Texture);
impl HasSize for CachedTexture {
fn cache_size(&self) -> usize {
let size = self.0.size();
(size.width as usize) * (size.height as usize) * 4
}
}
const TEXTURE_BUDGET: usize = 16 * 1024 * 1024;
pub type TexturePool = Cache<TextureKey, CachedTexture, Lru, HashLookup<TextureKey>>;
pub fn new_pool() -> TexturePool {
Cache::builder()
.max_size(MaxSize::Bytes(TEXTURE_BUDGET))
.build()
}