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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use raw::*;

const ENABLE_OPTIONS_INDICES: [EnableOption; 15] = [
    EnableOption::Blend,
    EnableOption::ColorLogicOperation,
    EnableOption::CullFace,
    EnableOption::DepthClamp,
    EnableOption::DepthTest,
    EnableOption::Dither,
    EnableOption::FramebufferSRGB,
    EnableOption::LineSmooth,
    EnableOption::Multisample,
    EnableOption::SampleShading,
    EnableOption::SampleMask,
    EnableOption::ScissorTest,
    EnableOption::StencilTest,
    EnableOption::TextureCubeMapSeamless,
    EnableOption::ProgramPointSize,
];


#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct FramebufferState {
    pub viewport: Viewport,

    pub options: [bool; 15],

    pub stencil_test: [StencilTest; 2],

    pub depth_test: DepthTest,
    pub depth_mask: DepthMask,
    pub depth_range: DepthRange,

    pub logic_operation: LogicOperation,

    pub blending_equation_rgb: BlendingEquation,
    pub blending_equation_alpha: BlendingEquation,
    pub linear_blending_factors: Option<[LinearBlendingFactor; 4]>,

    pub face_orientation: FaceOrientation,
    pub cull_face: Face,

    pub clear_color: ClearColor,
}

impl FramebufferState {
    pub fn sync(&mut self, other: &FramebufferState) {
        if self == other {
            return;
        }

        if self.viewport != other.viewport {
            viewport(other.viewport);
        }

        for i in 0..15 {
            if self.options[i] != other.options[i] {
                match other.options[i] {
                    true => enable(ENABLE_OPTIONS_INDICES[i]),
                    false => disable(ENABLE_OPTIONS_INDICES[i]),
                }
            }
        }

        if self.stencil_test[0] != other.stencil_test[0] {
            stencil_func_separate(Face::Front, other.stencil_test[0]);
        }

        if self.stencil_test[1] != other.stencil_test[1] {
            stencil_func_separate(Face::Front, other.stencil_test[1]);
        }

        if self.depth_test != other.depth_test {
            depth_function(other.depth_test);
        }

        if self.depth_mask != other.depth_mask {
            depth_mask(other.depth_mask);
        }

        if self.depth_range != other.depth_range {
            depth_range(other.depth_range);
        }

        if self.logic_operation != other.logic_operation {
            logic_op(other.logic_operation);
        }

        if self.blending_equation_rgb != other.blending_equation_rgb ||
            self.blending_equation_alpha != other.blending_equation_alpha
        {
            blend_equation_separate(other.blending_equation_rgb, other.blending_equation_alpha);
        }

        if self.linear_blending_factors != other.linear_blending_factors {
            if let Some(blending_factors) = other.linear_blending_factors {
                blend_func_separate(
                    blending_factors[0],
                    blending_factors[1],
                    blending_factors[2],
                    blending_factors[3],
                );
            }
        }

        if self.face_orientation != other.face_orientation {
            front_face(other.face_orientation);
        }

        if self.cull_face != other.cull_face {
            cull_face(other.cull_face);
        }

        if self.clear_color != other.clear_color {
            clear_color(other.clear_color);
        }

        *self = *other;
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub struct FramebufferStateBuilder {
    viewport: Viewport,

    options: [bool; 15],

    stencil_test: [StencilTest; 2],

    depth_test: DepthTest,
    depth_mask: DepthMask,
    depth_range: DepthRange,

    logic_operation: LogicOperation,

    blending_equation_rgb: BlendingEquation,
    blending_equation_alpha: BlendingEquation,
    linear_blending_factors: Option<[LinearBlendingFactor; 4]>,

    face_orientation: FaceOrientation,
    cull_face: Face,

    clear_color: ClearColor,
}

impl FramebufferStateBuilder {
    pub fn new() -> FramebufferStateBuilder {
        FramebufferStateBuilder::default()
    }

    pub fn viewport<'a>(&'a mut self, viewport: Viewport) -> &'a mut FramebufferStateBuilder {
        self.viewport = viewport;
        self
    }

    pub fn enable<'a>(&'a mut self, option: EnableOption) -> &'a mut FramebufferStateBuilder {
        self.options[usize::from(option)] = true;
        self
    }

    pub fn disable<'a>(&'a mut self, option: EnableOption) -> &'a mut FramebufferStateBuilder {
        self.options[usize::from(option)] = false;
        self
    }

    pub fn stencil_test<'a>(
        &'a mut self,
        face: Face,
        stencil_test: StencilTest,
    ) -> &'a mut FramebufferStateBuilder {
        match face {
            Face::Front => self.stencil_test[0] = stencil_test,
            Face::Back => self.stencil_test[1] = stencil_test,
            Face::FrontBack => {
                self.stencil_test[0] = stencil_test;
                self.stencil_test[1] = stencil_test;
            }
        }
        self
    }

    pub fn depth_test<'a>(&'a mut self, depth_test: DepthTest) -> &'a mut FramebufferStateBuilder {
        self.depth_test = depth_test;
        self
    }

    pub fn depth_mask<'a>(&'a mut self, depth_mask: DepthMask) -> &'a mut FramebufferStateBuilder {
        self.depth_mask = depth_mask;
        self
    }

    pub fn depth_range<'a>(
        &'a mut self,
        depth_range: DepthRange,
    ) -> &'a mut FramebufferStateBuilder {
        self.depth_range = depth_range;
        self
    }

    pub fn logic_operation<'a>(
        &'a mut self,
        logic_operation: LogicOperation,
    ) -> &'a mut FramebufferStateBuilder {
        self.logic_operation = logic_operation;
        self
    }

    pub fn blending_equation<'a>(
        &'a mut self,
        blending_equation_rgb: BlendingEquation,
        blending_equation_alpha: BlendingEquation,
    ) -> &'a mut FramebufferStateBuilder {
        self.blending_equation_rgb = blending_equation_rgb;
        self.blending_equation_alpha = blending_equation_alpha;
        self
    }

    pub fn linear_blending_factors<'a>(
        &'a mut self,
        source_rgb: LinearBlendingFactor,
        destination_rgb: LinearBlendingFactor,
        source_alpha: LinearBlendingFactor,
        destination_alpha: LinearBlendingFactor,
    ) -> &'a mut FramebufferStateBuilder {
        self.linear_blending_factors =
            Some(
                [source_rgb, destination_rgb, source_alpha, destination_alpha],
            );
        self
    }

    pub fn face_orientation<'a>(
        &'a mut self,
        face_orientation: FaceOrientation,
    ) -> &'a mut FramebufferStateBuilder {
        self.face_orientation = face_orientation;
        self
    }

    pub fn cull_face<'a>(&'a mut self, cull_face: Face) -> &'a mut FramebufferStateBuilder {
        self.cull_face = cull_face;
        self
    }

    pub fn clear_color<'a>(
        &'a mut self,
        clear_color: ClearColor,
    ) -> &'a mut FramebufferStateBuilder {
        self.clear_color = clear_color;
        self
    }

    pub fn build(&mut self) -> FramebufferState {
        FramebufferState {
            viewport: self.viewport,
            options: self.options,
            stencil_test: self.stencil_test,
            depth_test: self.depth_test,
            depth_mask: self.depth_mask,
            depth_range: self.depth_range,
            logic_operation: self.logic_operation,
            blending_equation_rgb: self.blending_equation_rgb,
            blending_equation_alpha: self.blending_equation_alpha,
            linear_blending_factors: self.linear_blending_factors,
            face_orientation: self.face_orientation,
            cull_face: self.cull_face,
            clear_color: self.clear_color,
        }
    }
}

impl Default for FramebufferState {
    fn default() -> FramebufferState {
        let mut options = [false; 15];
        options[usize::from(EnableOption::Dither)] = true;
        options[usize::from(EnableOption::Multisample)] = true;

        FramebufferState {
            viewport: Viewport::default(),

            options: options,

            stencil_test: [StencilTest::default(); 2],

            depth_test: DepthTest::default(),
            depth_mask: DepthMask::default(),
            depth_range: DepthRange::default(),

            logic_operation: LogicOperation::default(),

            blending_equation_rgb: BlendingEquation::default(),
            blending_equation_alpha: BlendingEquation::default(),
            linear_blending_factors: None,

            face_orientation: FaceOrientation::default(),
            cull_face: Face::default(),

            clear_color: ClearColor::default(),
        }
    }
}