2021-06-02 02:59:17 +00:00
|
|
|
use crate::{
|
2021-06-26 22:35:07 +00:00
|
|
|
render_resource::{Texture, TextureView},
|
2021-06-21 23:28:52 +00:00
|
|
|
renderer::RenderDevice,
|
2021-06-02 02:59:17 +00:00
|
|
|
};
|
2021-06-26 22:35:07 +00:00
|
|
|
use bevy_ecs::prelude::ResMut;
|
2021-06-02 02:59:17 +00:00
|
|
|
use bevy_utils::HashMap;
|
2021-06-21 23:28:52 +00:00
|
|
|
use wgpu::{TextureDescriptor, TextureViewDescriptor};
|
2021-06-02 02:59:17 +00:00
|
|
|
|
2021-11-16 03:37:48 +00:00
|
|
|
/// The internal representation of a [`CachedTexture`] used to track whether it was recently used
|
|
|
|
/// and is currently taken.
|
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,
|
|
|
|
}
|
|
|
|
|
2021-11-16 03:37:48 +00:00
|
|
|
/// A cached GPU [`Texture`] with corresponding [`TextureView`].
|
|
|
|
/// This is useful for textures that are created repeatedly (each frame) in the rendering process
|
|
|
|
/// to reduce the amount of GPU memory allocations.
|
2021-06-02 02:59:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-16 03:37:48 +00:00
|
|
|
/// This resource caches textures that are created repeatedly in the rendering process and
|
|
|
|
/// are only required for one frame.
|
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 {
|
2021-11-16 03:37:48 +00:00
|
|
|
/// Retrieves a texture that matches the `descriptor`. If no matching one is found a new
|
|
|
|
/// [`CachedTexture`] is created.
|
2021-06-02 02:59:17 +00:00
|
|
|
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-11-16 03:37:48 +00:00
|
|
|
/// Updates the cache and only retains recently used textures.
|
2021-06-26 22:35:07 +00:00
|
|
|
pub fn update(&mut self) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 03:37:48 +00:00
|
|
|
/// Updates the [`TextureCache`] to only retains recently used textures.
|
2021-06-26 22:35:07 +00:00
|
|
|
pub fn update_texture_cache_system(mut texture_cache: ResMut<TextureCache>) {
|
|
|
|
texture_cache.update();
|
2021-06-02 02:59:17 +00:00
|
|
|
}
|