1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
}
}
|