bevy/crates/bevy_render/src/texture/texture_cache.rs

101 lines
3.5 KiB
Rust
Raw Normal View History

2021-06-02 02:59:17 +00:00
use crate::{
render_resource::{Texture, TextureView},
2021-06-21 23:28:52 +00:00
renderer::RenderDevice,
2021-06-02 02:59:17 +00:00
};
use bevy_ecs::prelude::ResMut;
Proper prehashing (#3963) For some keys, it is too expensive to hash them on every lookup. Historically in Bevy, we have regrettably done the "wrong" thing in these cases (pre-computing hashes, then re-hashing them) because Rust's built in hashed collections don't give us the tools we need to do otherwise. Doing this is "wrong" because two different values can result in the same hash. Hashed collections generally get around this by falling back to equality checks on hash collisions. You can't do that if the key _is_ the hash. Additionally, re-hashing a hash increase the odds of collision! #3959 needs pre-hashing to be viable, so I decided to finally properly solve the problem. The solution involves two different changes: 1. A new generalized "pre-hashing" solution in bevy_utils: `Hashed<T>` types, which store a value alongside a pre-computed hash. And `PreHashMap<K, V>` (which uses `Hashed<T>` internally) . `PreHashMap` is just an alias for a normal HashMap that uses `Hashed<T>` as the key and a new `PassHash` implementation as the Hasher. 2. Replacing the `std::collections` re-exports in `bevy_utils` with equivalent `hashbrown` impls. Avoiding re-hashes requires the `raw_entry_mut` api, which isn't stabilized yet (and may never be ... `entry_ref` has favor now, but also isn't available yet). If std's HashMap ever provides the tools we need, we can move back to that. The latest version of `hashbrown` adds support for the `entity_ref` api, so we can move to that in preparation for an std migration, if thats the direction they seem to be going in. Note that adding hashbrown doesn't increase our dependency count because it was already in our tree. In addition to providing these core tools, I also ported the "table identity hashing" in `bevy_ecs` to `raw_entry_mut`, which was a particularly egregious case. The biggest outstanding case is `AssetPathId`, which stores a pre-hash. We need AssetPathId to be cheaply clone-able (and ideally Copy), but `Hashed<AssetPath>` requires ownership of the AssetPath, which makes cloning ids way more expensive. We could consider doing `Hashed<Arc<AssetPath>>`, but cloning an arc is still a non-trivial expensive that needs to be considered. I would like to handle this in a separate PR. And given that we will be re-evaluating the Bevy Assets implementation in the very near future, I'd prefer to hold off until after that conversation is concluded.
2022-02-18 03:26:01 +00:00
use bevy_utils::{Entry, HashMap};
2021-06-21 23:28:52 +00:00
use wgpu::{TextureDescriptor, TextureViewDescriptor};
2021-06-02 02:59:17 +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,
}
/// 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
}
/// 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 {
/// 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) {
Proper prehashing (#3963) For some keys, it is too expensive to hash them on every lookup. Historically in Bevy, we have regrettably done the "wrong" thing in these cases (pre-computing hashes, then re-hashing them) because Rust's built in hashed collections don't give us the tools we need to do otherwise. Doing this is "wrong" because two different values can result in the same hash. Hashed collections generally get around this by falling back to equality checks on hash collisions. You can't do that if the key _is_ the hash. Additionally, re-hashing a hash increase the odds of collision! #3959 needs pre-hashing to be viable, so I decided to finally properly solve the problem. The solution involves two different changes: 1. A new generalized "pre-hashing" solution in bevy_utils: `Hashed<T>` types, which store a value alongside a pre-computed hash. And `PreHashMap<K, V>` (which uses `Hashed<T>` internally) . `PreHashMap` is just an alias for a normal HashMap that uses `Hashed<T>` as the key and a new `PassHash` implementation as the Hasher. 2. Replacing the `std::collections` re-exports in `bevy_utils` with equivalent `hashbrown` impls. Avoiding re-hashes requires the `raw_entry_mut` api, which isn't stabilized yet (and may never be ... `entry_ref` has favor now, but also isn't available yet). If std's HashMap ever provides the tools we need, we can move back to that. The latest version of `hashbrown` adds support for the `entity_ref` api, so we can move to that in preparation for an std migration, if thats the direction they seem to be going in. Note that adding hashbrown doesn't increase our dependency count because it was already in our tree. In addition to providing these core tools, I also ported the "table identity hashing" in `bevy_ecs` to `raw_entry_mut`, which was a particularly egregious case. The biggest outstanding case is `AssetPathId`, which stores a pre-hash. We need AssetPathId to be cheaply clone-able (and ideally Copy), but `Hashed<AssetPath>` requires ownership of the AssetPath, which makes cloning ids way more expensive. We could consider doing `Hashed<Arc<AssetPath>>`, but cloning an arc is still a non-trivial expensive that needs to be considered. I would like to handle this in a separate PR. And given that we will be re-evaluating the Bevy Assets implementation in the very near future, I'd prefer to hold off until after that conversation is concluded.
2022-02-18 03:26:01 +00:00
Entry::Occupied(mut entry) => {
2021-06-02 02:59:17 +00:00
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
}
}
Proper prehashing (#3963) For some keys, it is too expensive to hash them on every lookup. Historically in Bevy, we have regrettably done the "wrong" thing in these cases (pre-computing hashes, then re-hashing them) because Rust's built in hashed collections don't give us the tools we need to do otherwise. Doing this is "wrong" because two different values can result in the same hash. Hashed collections generally get around this by falling back to equality checks on hash collisions. You can't do that if the key _is_ the hash. Additionally, re-hashing a hash increase the odds of collision! #3959 needs pre-hashing to be viable, so I decided to finally properly solve the problem. The solution involves two different changes: 1. A new generalized "pre-hashing" solution in bevy_utils: `Hashed<T>` types, which store a value alongside a pre-computed hash. And `PreHashMap<K, V>` (which uses `Hashed<T>` internally) . `PreHashMap` is just an alias for a normal HashMap that uses `Hashed<T>` as the key and a new `PassHash` implementation as the Hasher. 2. Replacing the `std::collections` re-exports in `bevy_utils` with equivalent `hashbrown` impls. Avoiding re-hashes requires the `raw_entry_mut` api, which isn't stabilized yet (and may never be ... `entry_ref` has favor now, but also isn't available yet). If std's HashMap ever provides the tools we need, we can move back to that. The latest version of `hashbrown` adds support for the `entity_ref` api, so we can move to that in preparation for an std migration, if thats the direction they seem to be going in. Note that adding hashbrown doesn't increase our dependency count because it was already in our tree. In addition to providing these core tools, I also ported the "table identity hashing" in `bevy_ecs` to `raw_entry_mut`, which was a particularly egregious case. The biggest outstanding case is `AssetPathId`, which stores a pre-hash. We need AssetPathId to be cheaply clone-able (and ideally Copy), but `Hashed<AssetPath>` requires ownership of the AssetPath, which makes cloning ids way more expensive. We could consider doing `Hashed<Arc<AssetPath>>`, but cloning an arc is still a non-trivial expensive that needs to be considered. I would like to handle this in a separate PR. And given that we will be re-evaluating the Bevy Assets implementation in the very near future, I'd prefer to hold off until after that conversation is concluded.
2022-02-18 03:26:01 +00:00
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
}
}
}
}
/// Updates the cache and only retains recently used textures.
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
}
}
}
/// Updates the [`TextureCache`] to only retains recently used textures.
pub fn update_texture_cache_system(mut texture_cache: ResMut<TextureCache>) {
texture_cache.update();
2021-06-02 02:59:17 +00:00
}