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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use super::*;
use super::errors::*;

pub trait Framebuffer {
    fn get_id(&self) -> FramebufferId;

    fn state(&self) -> &FramebufferState;
    fn mut_state(&mut self) -> &mut FramebufferState;

    fn get_state(&self) -> FramebufferState;
    fn set_state(&mut self, FramebufferState);

    fn bind(&self) -> ();

    fn set_viewport<'a>(&'a mut self, viewport: Viewport) {
        self.mut_state().viewport = viewport;
    }

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

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

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

    fn set_depth_test<'a>(&'a mut self, depth_test: DepthTest) {
        self.mut_state().depth_test = depth_test;
    }

    fn set_depth_mask<'a>(&'a mut self, depth_mask: DepthMask) {
        self.mut_state().depth_mask = depth_mask;
    }

    fn set_depth_range<'a>(&'a mut self, depth_range: DepthRange) {
        self.mut_state().depth_range = depth_range;
    }

    fn set_logic_operation<'a>(&'a mut self, logic_operation: LogicOperation) {
        self.mut_state().logic_operation = logic_operation;
    }

    fn set_blending_equation<'a>(
        &'a mut self,
        blending_equation_rgb: BlendingEquation,
        blending_equation_alpha: BlendingEquation,
    ) {
        self.mut_state().blending_equation_rgb = blending_equation_rgb;
        self.mut_state().blending_equation_alpha = blending_equation_alpha;
    }

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

    fn set_face_orientation<'a>(&'a mut self, face_orientation: FaceOrientation) {
        self.mut_state().face_orientation = face_orientation;
    }

    fn set_cull_face<'a>(&'a mut self, cull_face: Face) {
        self.mut_state().cull_face = cull_face;
    }

    fn set_clear_color<'a>(&'a mut self, clear_color: ClearColor) {
        self.mut_state().clear_color = clear_color;
    }

    fn set_clear_depth<'a>(&'a mut self, clear_depth: f64) {
        self.mut_state().clear_depth = clear_depth;
    }

    fn set_color_mask<'a>(&'a mut self, color_mask: [bool; 4]) {
        self.mut_state().color_mask = color_mask;
    }

    fn set_clip_control_origin<'a>(&'a mut self, clip_control_origin: ClipControlOrigin) {
        self.mut_state().clip_control_origin = clip_control_origin;
    }

    fn set_clip_control_depth<'a>(&'a mut self, clip_control_depth: ClipControlDepth) {
        self.mut_state().clip_control_depth = clip_control_depth;
    }
}

#[derive(Debug)]
pub struct DefaultFramebuffer {
    state: FramebufferState,
}

impl DefaultFramebuffer {
    pub fn new() -> DefaultFramebuffer {
        let mut default_framebuffer = DefaultFramebuffer {
            state: FramebufferState::default(),
        };

        default_framebuffer.set_depth_test(DepthTest::Greater);
        default_framebuffer.set_clear_depth(0.0f64);
        default_framebuffer.set_clip_control_origin(ClipControlOrigin::LowerLeft);
        default_framebuffer.set_clip_control_depth(ClipControlDepth::ZeroToOne);

        default_framebuffer
    }
}

impl Framebuffer for DefaultFramebuffer {
    fn get_id(&self) -> FramebufferId {
        DEFAULT_FRAMEBUFFER_ID
    }

    fn state(&self) -> &FramebufferState {
        &self.state
    }

    fn mut_state(&mut self) -> &mut FramebufferState {
        &mut self.state
    }

    fn get_state(&self) -> FramebufferState {
        self.state
    }

    fn set_state(&mut self, state: FramebufferState) {
        self.state = state;
    }

    #[allow(unused_must_use)]
    fn bind(&self) {
        unsafe {
            bind_framebuffer(FramebufferTarget::Framebuffer, self.get_id());
        }
    }
}

#[derive(Debug)]
pub struct GeneralFramebuffer {
    id: FramebufferId,
    state: FramebufferState,
}

impl Framebuffer for GeneralFramebuffer {
    fn get_id(&self) -> FramebufferId {
        self.id
    }

    fn state(&self) -> &FramebufferState {
        &self.state
    }

    fn mut_state(&mut self) -> &mut FramebufferState {
        &mut self.state
    }

    fn get_state(&self) -> FramebufferState {
        self.state
    }

    fn set_state(&mut self, state: FramebufferState) {
        self.state = state;
    }

    #[allow(unused_must_use)]
    fn bind(&self) {
        unsafe {
            bind_framebuffer(FramebufferTarget::Framebuffer, self.id);
        }
    }
}

impl GeneralFramebuffer {
    pub fn new() -> GeneralFramebuffer {
        let id;

        unsafe {
            id = create_framebuffer();
        };

        let mut general_framebuffer = GeneralFramebuffer {
            id: id,
            state: FramebufferState::default(),
        };

        general_framebuffer.set_depth_test(DepthTest::Greater);
        general_framebuffer.set_clear_depth(0.0f64);
        general_framebuffer.set_clip_control_origin(ClipControlOrigin::LowerLeft);
        general_framebuffer.set_clip_control_depth(ClipControlDepth::ZeroToOne);

        general_framebuffer
    }

    pub fn attach_texture(
        &self,
        attachment: FramebufferAttachment,
        texture_option: &DrawTextureAttachOption,
    ) -> Result<()> {
        unsafe {
            match *texture_option {
                DrawTextureAttachOption::AttachTexture(ref texture) => {
                    named_framebuffer_texture(self.id, attachment, texture.get_id(), 0)
                        .chain_err(|| "Could not attach texture to framebuffer")?;
                }
                DrawTextureAttachOption::AttachTextureLayer {
                    ref texture,
                    level,
                    layer,
                } => {
                    named_framebuffer_texture_layer(self.id, attachment, texture.get_id(), level, layer)
                        .chain_err(|| format!("Could not attach texture layer to framebuffer"))?;
                }
                DrawTextureAttachOption::None => {
                    named_framebuffer_texture(self.id, attachment, TextureId::empty(), 0)
                        .chain_err(|| "Could not detach texture from framebuffer")?;
                }
                DrawTextureAttachOption::Keep => {}
            }
        }

        Ok(())
    }

    pub fn attach_textures(&self, textures: &DrawTextureTarget) -> Result<()> {
        // Take all color attachments in attachments description
        let color_attachments = [
            &textures.color0,
            &textures.color1,
            &textures.color2,
            &textures.color3,
            &textures.color4,
            &textures.color5,
            &textures.color6,
            &textures.color7,
        ];

        // Enable all fragment shader outputs and match their location id to
        // the same attachment point id
        // layout(location = 0) => ColorAttachment0
        let mut draw_buffers = [
            FramebufferDrawBuffer::ColorAttachment0,
            FramebufferDrawBuffer::ColorAttachment1,
            FramebufferDrawBuffer::ColorAttachment2,
            FramebufferDrawBuffer::ColorAttachment3,
            FramebufferDrawBuffer::ColorAttachment4,
            FramebufferDrawBuffer::ColorAttachment5,
            FramebufferDrawBuffer::ColorAttachment6,
            FramebufferDrawBuffer::ColorAttachment7,
        ];

        // Iterate over each attachment point
        for (i, color_attachment_option) in color_attachments.iter().enumerate() {
            match **color_attachment_option {
                // If output is explicitly set to None, then let it be
                DrawTextureAttachOption::None => draw_buffers[i] = FramebufferDrawBuffer::None,
                _ => {}
            }
        }

        // Call to set draw buffers described above
        unsafe {
            named_framebuffer_draw_buffers(self.id, &draw_buffers).chain_err(|| "Could not attach draw buffers")?;
        }

        // Attach Depth and Stencil textures
        match textures.depth_stencil {
            DepthStencilOption::Separate {
                ref depth,
                ref stencil,
            } => {
                self.attach_texture(FramebufferAttachment::DepthAttachment, &depth)
                    .chain_err(|| "Could not attach depth texture")?;
                self.attach_texture(FramebufferAttachment::StencilAttachment, &stencil)
                    .chain_err(|| "Could not attach stencil texture")?;;
            }
            DepthStencilOption::Together(ref depth_stencil) => {
                self.attach_texture(
                    FramebufferAttachment::DepthStencilAttachment,
                    &depth_stencil,
                ).chain_err(|| "Could not attach depth stencil texture")?;;
            }
        }

        let color_attachments_enums = [
            FramebufferAttachment::ColorAttachment0,
            FramebufferAttachment::ColorAttachment1,
            FramebufferAttachment::ColorAttachment2,
            FramebufferAttachment::ColorAttachment3,
            FramebufferAttachment::ColorAttachment4,
            FramebufferAttachment::ColorAttachment5,
            FramebufferAttachment::ColorAttachment6,
            FramebufferAttachment::ColorAttachment7,
        ];

        for (i, color_attachment_option) in color_attachments.iter().enumerate() {
            self.attach_texture(color_attachments_enums[i], color_attachment_option)
                .chain_err(|| format!("Could not attach color{} texture", i))?;
        }

        unsafe {
            check_named_framebuffer_status(self.get_id(), FramebufferTarget::Framebuffer)
                .chain_err(|| "Framebuffer is not complete")?;
        }

        Ok(())
    }

    pub fn set_parameter(&mut self, parameter: FramebufferParameter, value: u32) {
        unsafe {
            framebuffer_parameter(self.id, parameter, value).unwrap();
        }
    }
}

impl Drop for GeneralFramebuffer {
    fn drop(&mut self) {
        unsafe {
            delete_framebuffer(self.id.into());
        }
    }
}