Path: blob/main/crates/bevy_render/src/texture/texture_cache.rs
6596 views
use crate::{1render_resource::{Texture, TextureView},2renderer::RenderDevice,3};4use bevy_ecs::{prelude::ResMut, resource::Resource};5use bevy_platform::collections::{hash_map::Entry, HashMap};6use wgpu::{TextureDescriptor, TextureViewDescriptor};78/// The internal representation of a [`CachedTexture`] used to track whether it was recently used9/// and is currently taken.10struct CachedTextureMeta {11texture: Texture,12default_view: TextureView,13taken: bool,14frames_since_last_use: usize,15}1617/// A cached GPU [`Texture`] with corresponding [`TextureView`].18///19/// This is useful for textures that are created repeatedly (each frame) in the rendering process20/// to reduce the amount of GPU memory allocations.21#[derive(Clone)]22pub struct CachedTexture {23pub texture: Texture,24pub default_view: TextureView,25}2627/// This resource caches textures that are created repeatedly in the rendering process and28/// are only required for one frame.29#[derive(Resource, Default)]30pub struct TextureCache {31textures: HashMap<TextureDescriptor<'static>, Vec<CachedTextureMeta>>,32}3334impl TextureCache {35/// Retrieves a texture that matches the `descriptor`. If no matching one is found a new36/// [`CachedTexture`] is created.37pub fn get(38&mut self,39render_device: &RenderDevice,40descriptor: TextureDescriptor<'static>,41) -> CachedTexture {42match self.textures.entry(descriptor) {43Entry::Occupied(mut entry) => {44for texture in entry.get_mut().iter_mut() {45if !texture.taken {46texture.frames_since_last_use = 0;47texture.taken = true;48return CachedTexture {49texture: texture.texture.clone(),50default_view: texture.default_view.clone(),51};52}53}5455let texture = render_device.create_texture(&entry.key().clone());56let default_view = texture.create_view(&TextureViewDescriptor::default());57entry.get_mut().push(CachedTextureMeta {58texture: texture.clone(),59default_view: default_view.clone(),60frames_since_last_use: 0,61taken: true,62});63CachedTexture {64texture,65default_view,66}67}68Entry::Vacant(entry) => {69let texture = render_device.create_texture(entry.key());70let default_view = texture.create_view(&TextureViewDescriptor::default());71entry.insert(vec![CachedTextureMeta {72texture: texture.clone(),73default_view: default_view.clone(),74taken: true,75frames_since_last_use: 0,76}]);77CachedTexture {78texture,79default_view,80}81}82}83}8485/// Returns `true` if the texture cache contains no textures.86pub fn is_empty(&self) -> bool {87self.textures.is_empty()88}8990/// Updates the cache and only retains recently used textures.91pub fn update(&mut self) {92self.textures.retain(|_, textures| {93for texture in textures.iter_mut() {94texture.frames_since_last_use += 1;95texture.taken = false;96}9798textures.retain(|texture| texture.frames_since_last_use < 3);99!textures.is_empty()100});101}102}103104/// Updates the [`TextureCache`] to only retains recently used textures.105pub fn update_texture_cache_system(mut texture_cache: ResMut<TextureCache>) {106texture_cache.update();107}108109110