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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::{hash};
use crate::gpu_cache::{GpuCacheHandle};
use crate::frame_builder::FrameBuildingState;
use crate::gpu_cache::GpuDataRequest;
use crate::intern;
use api::{ComponentTransferFuncType};


pub type FilterDataHandle = intern::Handle<FilterDataIntern>;

#[derive(Debug, Clone, MallocSizeOf, PartialEq)]
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub enum SFilterDataComponent {
    Identity,
    Table(Vec<f32>),
    Discrete(Vec<f32>),
    Linear(f32, f32),
    Gamma(f32, f32, f32),
}

impl Eq for SFilterDataComponent {}

impl hash::Hash for SFilterDataComponent {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        match self {
            SFilterDataComponent::Identity => {
                0.hash(state);
            }
            SFilterDataComponent::Table(values) => {
                1.hash(state);
                values.len().hash(state);
                for val in values {
                    val.to_bits().hash(state);
                }
            }
            SFilterDataComponent::Discrete(values) => {
                2.hash(state);
                values.len().hash(state);
                for val in values {
                    val.to_bits().hash(state);
                }
            }
            SFilterDataComponent::Linear(a, b) => {
                3.hash(state);
                a.to_bits().hash(state);
                b.to_bits().hash(state);
            }
            SFilterDataComponent::Gamma(a, b, c) => {
                4.hash(state);
                a.to_bits().hash(state);
                b.to_bits().hash(state);
                c.to_bits().hash(state);
            }
        }
    }
}

impl SFilterDataComponent {
    pub fn to_int(&self) -> u32 {
        match self {
            SFilterDataComponent::Identity => 0,
            SFilterDataComponent::Table(_) => 1,
            SFilterDataComponent::Discrete(_) => 2,
            SFilterDataComponent::Linear(_, _) => 3,
            SFilterDataComponent::Gamma(_, _, _) => 4,
        }
    }

    pub fn from_functype_values(
        func_type: ComponentTransferFuncType,
        values: &[f32],
    ) -> SFilterDataComponent {
        match func_type {
            ComponentTransferFuncType::Identity => SFilterDataComponent::Identity,
            ComponentTransferFuncType::Table => SFilterDataComponent::Table(values.to_vec()),
            ComponentTransferFuncType::Discrete => SFilterDataComponent::Discrete(values.to_vec()),
            ComponentTransferFuncType::Linear => SFilterDataComponent::Linear(values[0], values[1]),
            ComponentTransferFuncType::Gamma => SFilterDataComponent::Gamma(values[0], values[1], values[2]),
        }
    }
}

#[derive(Debug, Clone, MallocSizeOf, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub struct SFilterData {
    pub r_func: SFilterDataComponent,
    pub g_func: SFilterDataComponent,
    pub b_func: SFilterDataComponent,
    pub a_func: SFilterDataComponent,
}

#[derive(Debug, Clone, MallocSizeOf, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub struct SFilterDataKey {
    pub data: SFilterData,
}

impl intern::InternDebug for SFilterDataKey {}

#[derive(Debug)]
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
#[derive(MallocSizeOf)]
pub struct SFilterDataTemplate {
    pub data: SFilterData,
    pub gpu_cache_handle: GpuCacheHandle,
}

impl From<SFilterDataKey> for SFilterDataTemplate {
    fn from(item: SFilterDataKey) -> Self {
        SFilterDataTemplate {
            data: item.data,
            gpu_cache_handle: GpuCacheHandle::new(),
        }
    }
}

impl SFilterData {
    pub fn is_identity(&self) -> bool {
        self.r_func == SFilterDataComponent::Identity
            && self.g_func == SFilterDataComponent::Identity
            && self.b_func == SFilterDataComponent::Identity
            && self.a_func == SFilterDataComponent::Identity
    }

    pub fn update(&self, mut request: GpuDataRequest) {
        push_component_transfer_data(&self.r_func, &mut request);
        push_component_transfer_data(&self.g_func, &mut request);
        push_component_transfer_data(&self.b_func, &mut request);
        push_component_transfer_data(&self.a_func, &mut request);
        assert!(!self.is_identity());
    }
}

impl SFilterDataTemplate {
    /// Update the GPU cache for a given filter data template. This may be called multiple
    /// times per frame, by each primitive reference that refers to this interned
    /// template. The initial request call to the GPU cache ensures that work is only
    /// done if the cache entry is invalid (due to first use or eviction).
    pub fn update(
        &mut self,
        frame_state: &mut FrameBuildingState,
    ) {
        if let Some(request) = frame_state.gpu_cache.request(&mut self.gpu_cache_handle) {
            self.data.update(request);
        }
    }
}

#[derive(Copy, Clone, Debug, MallocSizeOf)]
#[cfg_attr(any(feature = "serde"), derive(Deserialize, Serialize))]
pub enum FilterDataIntern {}

impl intern::Internable for FilterDataIntern {
    type Key = SFilterDataKey;
    type StoreData = SFilterDataTemplate;
    type InternData = ();
    const PROFILE_COUNTER: usize = crate::profiler::INTERNED_FILTER_DATA;
}

fn push_component_transfer_data(
    func_comp: &SFilterDataComponent,
    request: &mut GpuDataRequest,
) {
    match func_comp {
        SFilterDataComponent::Identity => {}
        SFilterDataComponent::Table(values) |
        SFilterDataComponent::Discrete(values) => {
            // Push a 256 entry lookup table.
            assert!(values.len() > 0);
            for i in 0 .. 64 {
                let mut arr = [0.0 ; 4];
                for j in 0 .. 4 {
                    if (values.len() == 1) || (i == 63 && j == 3) {
                        arr[j] = values[values.len()-1];
                    } else {
                        let c = ((4*i + j) as f32)/255.0;
                        match func_comp {
                            SFilterDataComponent::Table(_) => {
                                let n = (values.len()-1) as f32;
                                let k = (n * c).floor() as u32;
                                let ku = k as usize;
                                assert!(ku < values.len()-1);
                                arr[j] = values[ku] + (c*n - (k as f32)) * (values[ku+1] - values[ku]);
                            }
                            SFilterDataComponent::Discrete(_) => {
                                let n = values.len() as f32;
                                let k = (n * c).floor() as usize;
                                assert!(k < values.len());
                                arr[j] = values[k];
                            }
                            SFilterDataComponent::Identity |
                            SFilterDataComponent::Linear(_,_) |
                            SFilterDataComponent::Gamma(_,_,_) => {
                                unreachable!();
                            }
                        }

                    }
                }

                request.push(arr);
            }
        }
        SFilterDataComponent::Linear(a, b) => {
            request.push([*a, *b, 0.0, 0.0]);
        }
        SFilterDataComponent::Gamma(a, b, c) => {
            request.push([*a, *b, *c, 0.0]);
        }
    }
}