bevy/pipelined/bevy_render2/src/texture/texture_cache.rs

93 lines
3 KiB
Rust
Raw Normal View History

2021-06-02 02:59:17 +00:00
use crate::{
2021-06-21 23:28:52 +00:00
render_resource::{Texture, TextureView, TextureViewId},
renderer::RenderDevice,
2021-06-02 02:59:17 +00:00
};
use bevy_ecs::prelude::{Res, ResMut};
use bevy_utils::HashMap;
2021-06-21 23:28:52 +00:00
use wgpu::{TextureDescriptor, TextureViewDescriptor};
2021-06-02 02:59:17 +00:00
struct CachedTextureMeta {
2021-06-21 23:28:52 +00:00
texture: Texture,
default_view: TextureView,
2021-06-02 02:59:17 +00:00
taken: bool,
frames_since_last_use: usize,
}
pub struct CachedTexture {
2021-06-21 23:28:52 +00:00
pub texture: Texture,
pub default_view: TextureView,
2021-06-02 02:59:17 +00:00
}
#[derive(Default)]
pub struct TextureCache {
2021-06-21 23:28:52 +00:00
textures: HashMap<wgpu::TextureDescriptor<'static>, Vec<CachedTextureMeta>>,
2021-06-02 02:59:17 +00:00
}
impl TextureCache {
pub fn get(
&mut self,
2021-06-21 23:28:52 +00:00
render_device: &RenderDevice,
descriptor: TextureDescriptor<'static>,
2021-06-02 02:59:17 +00:00
) -> CachedTexture {
match self.textures.entry(descriptor) {
std::collections::hash_map::Entry::Occupied(mut entry) => {
for texture in entry.get_mut().iter_mut() {
if !texture.taken {
texture.frames_since_last_use = 0;
texture.taken = true;
return CachedTexture {
2021-06-21 23:28:52 +00:00
texture: texture.texture.clone(),
default_view: texture.default_view.clone(),
2021-06-02 02:59:17 +00:00
};
}
}
2021-06-21 23:28:52 +00:00
let texture = render_device.create_texture(&entry.key().clone());
let default_view = texture.create_view(&TextureViewDescriptor::default());
2021-06-02 02:59:17 +00:00
entry.get_mut().push(CachedTextureMeta {
2021-06-21 23:28:52 +00:00
texture: texture.clone(),
default_view: default_view.clone(),
2021-06-02 02:59:17 +00:00
frames_since_last_use: 0,
taken: true,
});
CachedTexture {
2021-06-21 23:28:52 +00:00
texture,
default_view,
2021-06-02 02:59:17 +00:00
}
}
std::collections::hash_map::Entry::Vacant(entry) => {
2021-06-21 23:28:52 +00:00
let texture = render_device.create_texture(entry.key());
let default_view = texture.create_view(&TextureViewDescriptor::default());
2021-06-02 02:59:17 +00:00
entry.insert(vec![CachedTextureMeta {
2021-06-21 23:28:52 +00:00
texture: texture.clone(),
default_view: default_view.clone(),
2021-06-02 02:59:17 +00:00
taken: true,
frames_since_last_use: 0,
}]);
CachedTexture {
2021-06-21 23:28:52 +00:00
texture,
default_view,
2021-06-02 02:59:17 +00:00
}
}
}
}
2021-06-21 23:28:52 +00:00
pub fn update(&mut self, device: &RenderDevice) {
2021-06-02 02:59:17 +00:00
for textures in self.textures.values_mut() {
for texture in textures.iter_mut() {
texture.frames_since_last_use += 1;
texture.taken = false;
}
2021-06-21 23:28:52 +00:00
textures.retain(|texture| texture.frames_since_last_use < 3);
2021-06-02 02:59:17 +00:00
}
}
}
pub fn update_texture_cache_system(
mut texture_cache: ResMut<TextureCache>,
2021-06-21 23:28:52 +00:00
render_device: Res<RenderDevice>,
2021-06-02 02:59:17 +00:00
) {
2021-06-21 23:28:52 +00:00
texture_cache.update(&render_device);
2021-06-02 02:59:17 +00:00
}