diff options
Diffstat (limited to 'rust/crates/compositor/src/texture_pool.rs')
| -rw-r--r-- | rust/crates/compositor/src/texture_pool.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/rust/crates/compositor/src/texture_pool.rs b/rust/crates/compositor/src/texture_pool.rs new file mode 100644 index 00000000..f2c59ee7 --- /dev/null +++ b/rust/crates/compositor/src/texture_pool.rs @@ -0,0 +1,36 @@ +use std::collections::HashMap; + +use gpu::{GpuContext, wgpu}; + +type TextureKey = (u32, u32); + +#[derive(Default)] +pub struct TexturePool { + available: HashMap<TextureKey, Vec<wgpu::Texture>>, + in_use: Vec<(TextureKey, wgpu::Texture)>, +} + +impl TexturePool { + pub fn recycle_frame(&mut self) { + for (key, texture) in self.in_use.drain(..) { + self.available.entry(key).or_default().push(texture); + } + } + + pub fn acquire( + &mut self, + context: &GpuContext, + width: u32, + height: u32, + label: &'static str, + ) -> wgpu::Texture { + let key = (width, height); + let texture = self + .available + .get_mut(&key) + .and_then(Vec::pop) + .unwrap_or_else(|| context.create_render_texture(width, height, label)); + self.in_use.push((key, texture.clone())); + texture + } +} |
