fast3d/output/
texture_cache.rs

1use fast3d_gbi::defines::{ComponentSize, ImageFormat};
2use std::hash::Hash;
3use std::num::NonZeroUsize;
4
5use crate::output::models::OutputTexture;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub struct TextureCacheId(pub TextureConfig);
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub struct TextureConfig {
12    pub game_address: usize,
13    pub format: ImageFormat,
14    pub size: ComponentSize,
15}
16
17pub struct TextureCache {
18    cache: lru::LruCache<TextureCacheId, OutputTexture>,
19}
20
21impl TextureCache {
22    pub fn new(capacity: usize) -> Self {
23        Self {
24            cache: lru::LruCache::new(NonZeroUsize::new(capacity).unwrap()),
25        }
26    }
27
28    pub fn contains(
29        &mut self,
30        game_address: usize,
31        format: ImageFormat,
32        size: ComponentSize,
33    ) -> Option<TextureCacheId> {
34        let texture_cache_id = TextureCacheId(TextureConfig {
35            game_address,
36            format,
37            size,
38        });
39
40        if let Some(_texture) = self.cache.get(&texture_cache_id) {
41            return Some(texture_cache_id);
42        }
43
44        None
45    }
46
47    pub fn get(&mut self, texture_cache_id: TextureCacheId) -> Option<&OutputTexture> {
48        if let Some(texture) = self.cache.get(&texture_cache_id) {
49            return Some(texture);
50        }
51
52        None
53    }
54
55    pub fn get_mut(&mut self, texture_cache_id: TextureCacheId) -> Option<&mut OutputTexture> {
56        if let Some(texture) = self.cache.get_mut(&texture_cache_id) {
57            return Some(texture);
58        }
59
60        None
61    }
62
63    #[allow(clippy::too_many_arguments)]
64    pub fn insert(
65        &mut self,
66        game_address: usize,
67        format: ImageFormat,
68        size: ComponentSize,
69        width: u32,
70        height: u32,
71        uls: u16,
72        ult: u16,
73        data: Vec<u8>,
74    ) -> TextureCacheId {
75        let texture = OutputTexture::new(game_address, format, size, width, height, uls, ult, data);
76        let tex_cache_id = TextureCacheId(TextureConfig {
77            game_address,
78            format,
79            size,
80        });
81
82        if let Some(_evicted_item) = self.cache.push(tex_cache_id, texture) {
83            // TODO: handle evicted item by removing it from the GPU
84        }
85
86        tex_cache_id
87    }
88}