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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use {
super::Sampler,
crate::escape::Handle,
gfx_hal::{image::SamplerInfo, Backend},
std::{
collections::hash_map::{Entry, HashMap},
ops::{Deref, DerefMut},
},
};
#[derive(Debug, derivative::Derivative)]
#[derivative(Default(bound = ""))]
pub struct SamplerCache<B: Backend> {
samplers: HashMap<SamplerInfo, Handle<Sampler<B>>>,
}
impl<B> SamplerCache<B>
where
B: Backend,
{
pub fn get(
&mut self,
info: SamplerInfo,
create: impl FnOnce() -> Result<Handle<Sampler<B>>, gfx_hal::device::AllocationError>,
) -> Result<Handle<Sampler<B>>, gfx_hal::device::AllocationError> {
Ok(match self.samplers.entry(info) {
Entry::Occupied(occupied) => occupied.get().clone(),
Entry::Vacant(vacant) => {
let sampler = create()?;
vacant.insert(sampler).clone()
}
})
}
pub fn get_with_upgradable_lock<R, W, U>(
read: R,
upgrade: U,
info: SamplerInfo,
create: impl FnOnce() -> Result<Handle<Sampler<B>>, gfx_hal::device::AllocationError>,
) -> Result<Handle<Sampler<B>>, gfx_hal::device::AllocationError>
where
R: Deref<Target = Self>,
W: DerefMut<Target = Self>,
U: FnOnce(R) -> W,
{
if let Some(sampler) = read.samplers.get(&info) {
return Ok(sampler.clone());
}
let sampler = create()?;
{
upgrade(read).samplers.insert(info, sampler.clone());
}
Ok(sampler)
}
}