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
74
75
76
77
78
79
80
81
82
use crate::filter_chain::filter_impl::FilterChainImpl;
use crate::filter_chain::inner::FilterChainDispatch;
use crate::gl::GLInterface;
use crate::FilterChainGL;
use librashader_runtime::parameters::FilterChainParameters;

impl AsRef<dyn FilterChainParameters + 'static> for FilterChainDispatch {
    fn as_ref<'a>(&'a self) -> &'a (dyn FilterChainParameters + 'static) {
        match self {
            FilterChainDispatch::DirectStateAccess(p) => p,
            FilterChainDispatch::Compatibility(p) => p,
        }
    }
}

impl AsMut<dyn FilterChainParameters + 'static> for FilterChainDispatch {
    fn as_mut<'a>(&'a mut self) -> &'a mut (dyn FilterChainParameters + 'static) {
        match self {
            FilterChainDispatch::DirectStateAccess(p) => p,
            FilterChainDispatch::Compatibility(p) => p,
        }
    }
}

impl FilterChainParameters for FilterChainGL {
    fn get_enabled_pass_count(&self) -> usize {
        self.filter.as_ref().get_enabled_pass_count()
    }

    fn set_enabled_pass_count(&mut self, count: usize) {
        self.filter.as_mut().set_enabled_pass_count(count)
    }

    fn enumerate_parameters(&self) -> ::librashader_common::map::halfbrown::Iter<String, f32> {
        self.filter.as_ref().enumerate_parameters()
    }

    fn get_parameter(&self, parameter: &str) -> Option<f32> {
        self.filter.as_ref().get_parameter(parameter)
    }

    fn set_parameter(&mut self, parameter: &str, new_value: f32) -> Option<f32> {
        self.filter.as_mut().set_parameter(parameter, new_value)
    }
}

impl<T: GLInterface> FilterChainParameters for FilterChainImpl<T> {
    fn get_enabled_pass_count(&self) -> usize {
        self.common.config.passes_enabled
    }

    fn set_enabled_pass_count(&mut self, count: usize) {
        self.common.config.passes_enabled = count
    }

    fn enumerate_parameters(&self) -> ::librashader_common::map::halfbrown::Iter<String, f32> {
        self.common.config.parameters.iter()
    }

    fn get_parameter(&self, parameter: &str) -> Option<f32> {
        self.common
            .config
            .parameters
            .get::<str>(parameter.as_ref())
            .copied()
    }

    fn set_parameter(&mut self, parameter: &str, new_value: f32) -> Option<f32> {
        if let Some(value) = self
            .common
            .config
            .parameters
            .get_mut::<str>(parameter.as_ref())
        {
            let old = *value;
            *value = new_value;
            Some(old)
        } else {
            None
        }
    }
}