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
64
65
66
67
68
69
70
71
72
73
//! A cache to store and retrieve samplers


use {
    super::Sampler,
    crate::escape::Handle,
    rendy_core::hal::{image::SamplerDesc, Backend},
    std::{
        collections::hash_map::{Entry, HashMap},
        ops::{Deref, DerefMut},
    },
};

/// Sampler cache holds handlers to created samplers.

#[derive(Debug)]
pub struct SamplerCache<B: Backend> {
    samplers: HashMap<SamplerDesc, Handle<Sampler<B>>>,
}

impl<B> Default for SamplerCache<B>
where
    B: Backend,
{
    fn default() -> Self {
        SamplerCache {
            samplers: HashMap::default(),
        }
    }
}

impl<B> SamplerCache<B>
where
    B: Backend,
{
    /// Get sampler with specified paramters.

    /// Create new one using closure provided.

    pub fn get(
        &mut self,
        info: SamplerDesc,
        create: impl FnOnce() -> Result<Handle<Sampler<B>>, rendy_core::hal::device::AllocationError>,
    ) -> Result<Handle<Sampler<B>>, rendy_core::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()
            }
        })
    }

    /// Get sampler with specified paramters.

    /// Create new one using closure provided.

    /// Does not lock for writing if sampler exists.

    pub fn get_with_upgradable_lock<R, W, U>(
        read: R,
        upgrade: U,
        info: SamplerDesc,
        create: impl FnOnce() -> Result<Handle<Sampler<B>>, rendy_core::hal::device::AllocationError>,
    ) -> Result<Handle<Sampler<B>>, rendy_core::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)
    }
}