[go: up one dir, main page]

glium 0.3.3

High-level and safe OpenGL wrapper.
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*!
Contains everything related to the internal handling of framebuffer objects.

This module allows creating framebuffer objects. However it **does not** check whether
the framebuffer object is complete (ie. if everything is valid). This is the module user's job.

Here are the rules taken from the official wiki:

Attachment Completeness

Each attachment point itself must be complete according to these rules. Empty attachments
(attachments with no image attached) are complete by default. If an image is attached, it must
adhere to the following rules:

The source object for the image still exists and has the same type it was attached with.
The image has a non-zero width and height (the height of a 1D image is assumed to be 1). The
  width/height must also be less than GL_MAX_FRAMEBUFFER_WIDTH and GL_MAX_FRAMEBUFFER_HEIGHT
  respectively (if GL 4.3/ARB_framebuffer_no_attachments).
The layer for 3D or array textures attachments is less than the depth of the texture. It must
  also be less than GL_MAX_FRAMEBUFFER_LAYERS (if GL 4.3/ARB_framebuffer_no_attachments).
The number of samples must be less than GL_MAX_FRAMEBUFFER_SAMPLES (if
  GL 4.3/ARB_framebuffer_no_attachments).
The image's format must match the attachment point's requirements, as defined above.
  Color-renderable formats for color attachments, etc.

Completeness Rules

These are the rules for framebuffer completeness. The order of these rules matters.

If the target​ of glCheckFramebufferStatus references the Default Framebuffer (ie: FBO object
  number 0 is bound), and the default framebuffer does not exist, then you will get
  GL_FRAMEBUFFER_UNDEFINEZ. If the default framebuffer exists, then you always get
  GL_FRAMEBUFFER_COMPLETE. The rest of the rules apply when an FBO is bound.
All attachments must be attachment complete. (GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT when false).
There must be at least one image attached to the FBO, or if OpenGL 4.3 or
  ARB_framebuffer_no_attachment is available, the GL_FRAMEBUFFER_DEFAULT_WIDTH and
  GL_FRAMEBUFFER_DEFAULT_HEIGHT parameters of the framebuffer must both be non-zero.
  (GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT when false).
Each draw buffers must either specify color attachment points that have images attached or
  must be GL_NONE. (GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER when false). Note that this test is
  not performed if OpenGL 4.1 or ARB_ES2_compatibility is available.
If the read buffer is set, then it must specify an attachment point that has an image
  attached. (GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER when false). Note that this test is not
  performed if OpenGL 4.1 or ARB_ES2_compatibility is available.
All images must have the same number of multisample samples.
  (GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE when false).
If a layered image is attached to one attachment, then all attachments must be layered
  attachments. The attached layers do not have to have the same number of layers, nor do the
  layers have to come from the same kind of texture (a cubemap color texture can be paired
  with an array depth texture) (GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS when false).

*/
use std::collections::HashMap;
use std::mem;
use std::cell::RefCell;

use GlObject;

use gl;
use context::CommandContext;
use version::Version;
use version::Api;

#[derive(Hash, Clone, PartialEq, Eq)]
pub struct FramebufferAttachments {
    pub colors: Vec<(u32, Attachment)>,
    pub depth_stencil: FramebufferDepthStencilAttachments,
}

#[derive(Hash, Clone, PartialEq, Eq)]
pub enum FramebufferDepthStencilAttachments {
    None,
    DepthAttachment(Attachment),
    StencilAttachment(Attachment),
    DepthAndStencilAttachments(Attachment, Attachment),
    DepthStencilAttachment(Attachment),
}

#[derive(Hash, Copy, Clone, PartialEq, Eq)]
pub enum Attachment {
    Texture {
        bind_point: gl::types::GLenum,      // must be GL_TEXTURE_3D, GL_TEXTURE_2D_ARRAY, etc.
        id: gl::types::GLuint,
        level: u32,
        layer: u32,
    },
    RenderBuffer(gl::types::GLuint),
}

/// Manages all the framebuffer objects.
///
/// `cleanup` **must** be called when destroying the container, otherwise `Drop` will panic.
pub struct FramebuffersContainer {
    framebuffers: RefCell<HashMap<FramebufferAttachments, FrameBufferObject>>,
}

/// Frame buffer.
struct FrameBufferObject {
    id: gl::types::GLuint,
    current_read_buffer: gl::types::GLenum,
}

impl FramebuffersContainer {
    pub fn new() -> FramebuffersContainer {
        FramebuffersContainer {
            framebuffers: RefCell::new(HashMap::new()),
        }
    }

    pub fn purge_all(&self, ctxt: &mut CommandContext) {
        self.purge_if(|_| true, ctxt);
    }

    pub fn purge_texture(&self, texture: gl::types::GLuint, ctxt: &mut CommandContext) {
        self.purge_if(|a| {
            match a {
                &Attachment::Texture { id, .. } if id == texture => true,
                _ => false 
            }
        }, ctxt);
    }

    pub fn purge_renderbuffer(&self, renderbuffer: gl::types::GLuint,
                              ctxt: &mut CommandContext)
    {
        self.purge_if(|a| a == &Attachment::RenderBuffer(renderbuffer), ctxt);
    }

    fn purge_if<F>(&self, condition: F, mut ctxt: &mut CommandContext)
                   where F: Fn(&Attachment) -> bool
    {
        let mut framebuffers = self.framebuffers.borrow_mut();

        let mut attachments = Vec::new();
        for (key, _) in framebuffers.iter() {
            match key.depth_stencil {
                FramebufferDepthStencilAttachments::None => (),
                FramebufferDepthStencilAttachments::DepthAttachment(ref depth) => {
                    if condition(depth) {
                        attachments.push(key.clone());
                        continue;
                    }
                },
                FramebufferDepthStencilAttachments::StencilAttachment(ref stencil) => {
                    if condition(stencil) {
                        attachments.push(key.clone());
                        continue;
                    }
                },
                FramebufferDepthStencilAttachments::DepthAndStencilAttachments(ref depth,
                                                                               ref stencil) =>
                {
                    if condition(depth) || condition(stencil) {
                        attachments.push(key.clone());
                        continue;
                    }
                },
                FramebufferDepthStencilAttachments::DepthStencilAttachment(ref depth_stencil) => {
                    if condition(depth_stencil) {
                        attachments.push(key.clone());
                        continue;
                    }
                }
            }

            if key.colors.iter().find(|&&(_, ref id)| condition(id)).is_some() {
                attachments.push(key.clone());
                continue;
            }
        }

        for atch in attachments.into_iter() {
            framebuffers.remove(&atch).unwrap().destroy(ctxt);
        }
    }

    pub fn cleanup(self, ctxt: &mut CommandContext) {
        let mut other = HashMap::new();
        mem::swap(&mut *self.framebuffers.borrow_mut(), &mut other);

        for (_, obj) in other.into_iter() {
            obj.destroy(ctxt);
        }
    }

    pub fn get_framebuffer_for_drawing(&self, attachments: Option<&FramebufferAttachments>,
                                       ctxt: &mut CommandContext) -> gl::types::GLuint
    {
        if let Some(attachments) = attachments {
            self.get_framebuffer(attachments, ctxt)
        } else {
            0
        }
    }

    pub fn get_framebuffer_for_reading(&self, attachment: &Attachment, ctxt: &mut CommandContext)
                                       -> (gl::types::GLuint, gl::types::GLenum)
    {
        for (attachments, fbo) in self.framebuffers.borrow_mut().iter() {
            for &(key, ref atc) in attachments.colors.iter() {
                if atc == attachment {
                    return (fbo.get_id(), gl::COLOR_ATTACHMENT0 + key);
                }
            }
        }

        let attachments = FramebufferAttachments {
            colors: vec![(0, attachment.clone())],
            depth_stencil: FramebufferDepthStencilAttachments::None,
        };

        let framebuffer = self.get_framebuffer_for_drawing(Some(&attachments), ctxt);
        (framebuffer, gl::COLOR_ATTACHMENT0)
    }

    fn get_framebuffer(&self, framebuffer: &FramebufferAttachments,
                       ctxt: &mut CommandContext) -> gl::types::GLuint
    {
        let mut framebuffers = self.framebuffers.borrow_mut();

        if let Some(value) = framebuffers.get(framebuffer) {
            return value.id;
        }

        let new_fbo = FrameBufferObject::new(ctxt, framebuffer);
        let new_fbo_id = new_fbo.id.clone();
        framebuffers.insert(framebuffer.clone(), new_fbo);
        new_fbo_id
    }
}

impl Drop for FramebuffersContainer {
    fn drop(&mut self) {
        if self.framebuffers.borrow_mut().len() != 0 {
            panic!()
        }
    }
}

impl FrameBufferObject {
    /// Builds a new FBO.
    fn new(mut ctxt: &mut CommandContext, attachments: &FramebufferAttachments) -> FrameBufferObject {
        if attachments.colors.len() > ctxt.capabilities.max_draw_buffers as usize {
            panic!("Trying to attach {} color buffers, but the hardware only supports {}",
                   attachments.colors.len(), ctxt.capabilities.max_draw_buffers);
        }

        let id = unsafe {
            let mut id = mem::uninitialized();

            if ctxt.version >= &Version(Api::Gl, 4, 5) ||
                ctxt.extensions.gl_arb_direct_state_access
            {
                ctxt.gl.CreateFramebuffers(1, &mut id);
            } else if ctxt.version >= &Version(Api::Gl, 3, 0) ||
                ctxt.version >= &Version(Api::GlEs, 2, 0)
            {
                ctxt.gl.GenFramebuffers(1, &mut id);
                bind_framebuffer(&mut ctxt, id, true, false);
            } else {
                ctxt.gl.GenFramebuffersEXT(1, &mut id);
                bind_framebuffer(&mut ctxt, id, true, false);
            }

            let mut raw_attachments: Vec<gl::types::GLenum> = Vec::new();

            for &(slot, atchmnt) in attachments.colors.iter() {
                attach(&mut ctxt, gl::COLOR_ATTACHMENT0 + slot as u32, id, atchmnt);
                raw_attachments.push(gl::COLOR_ATTACHMENT0 + slot as u32);
            }

            match attachments.depth_stencil {
                FramebufferDepthStencilAttachments::None => (),
                FramebufferDepthStencilAttachments::DepthAttachment(depth) => {
                    attach(&mut ctxt, gl::DEPTH_ATTACHMENT, id, depth);
                },
                FramebufferDepthStencilAttachments::StencilAttachment(stencil) => {
                    attach(&mut ctxt, gl::STENCIL_ATTACHMENT, id, stencil);
                },
                FramebufferDepthStencilAttachments::DepthAndStencilAttachments(depth,
                                                                               stencil) =>
                {
                    attach(&mut ctxt, gl::DEPTH_ATTACHMENT, id, depth);
                    attach(&mut ctxt, gl::STENCIL_ATTACHMENT, id, stencil);
                },
                FramebufferDepthStencilAttachments::DepthStencilAttachment(depth_stencil) => {
                    attach(&mut ctxt, gl::DEPTH_STENCIL_ATTACHMENT, id, depth_stencil);
                },
            };

            if ctxt.version >= &Version(Api::Gl, 4, 5) ||
               ctxt.extensions.gl_arb_direct_state_access
            {
                ctxt.gl.NamedFramebufferDrawBuffers(id, raw_attachments.len()
                                                    as gl::types::GLsizei,
                                                    raw_attachments.as_ptr());

            } else if ctxt.version >= &Version(Api::Gl, 2, 0) ||
                      ctxt.version >= &Version(Api::GlEs, 3, 0)
            {
                bind_framebuffer(&mut ctxt, id, true, false);
                ctxt.gl.DrawBuffers(raw_attachments.len() as gl::types::GLsizei,
                                    raw_attachments.as_ptr());

            } else if ctxt.version >= &Version(Api::GlEs, 2, 0) {
                assert_eq!(raw_attachments, &[gl::COLOR_ATTACHMENT0]);

            } else {
                unimplemented!();       // FIXME: use an extension
            }

            id
        };

        FrameBufferObject {
            id: id,
            current_read_buffer: gl::BACK,
        }
    }

    fn destroy(self, mut ctxt: &mut CommandContext) {
        unsafe {
            // unbinding framebuffer
            if ctxt.version >= &Version(Api::Gl, 3, 0) {
                if ctxt.state.draw_framebuffer == self.id && ctxt.state.read_framebuffer == self.id {
                    ctxt.gl.BindFramebuffer(gl::FRAMEBUFFER, 0);
                    ctxt.state.draw_framebuffer = 0;
                    ctxt.state.read_framebuffer = 0;

                } else if ctxt.state.draw_framebuffer == self.id {
                    ctxt.gl.BindFramebuffer(gl::DRAW_FRAMEBUFFER, 0);
                    ctxt.state.draw_framebuffer = 0;

                } else if ctxt.state.read_framebuffer == self.id {
                    ctxt.gl.BindFramebuffer(gl::READ_FRAMEBUFFER, 0);
                    ctxt.state.read_framebuffer = 0;
                }

            } else if ctxt.version >= &Version(Api::GlEs, 2, 0) {
                if ctxt.state.draw_framebuffer == self.id || ctxt.state.read_framebuffer == self.id {
                    ctxt.gl.BindFramebuffer(gl::FRAMEBUFFER, 0);
                    ctxt.state.draw_framebuffer = 0;
                    ctxt.state.read_framebuffer = 0;
                }

            } else if ctxt.extensions.gl_ext_framebuffer_object {
                if ctxt.state.draw_framebuffer == self.id || ctxt.state.read_framebuffer == self.id {
                    ctxt.gl.BindFramebufferEXT(gl::FRAMEBUFFER_EXT, 0);
                    ctxt.state.draw_framebuffer = 0;
                    ctxt.state.read_framebuffer = 0;
                }

            } else {
                unreachable!();
            }

            // deleting
            if ctxt.version >= &Version(Api::Gl, 3, 0) ||
                ctxt.version >= &Version(Api::GlEs, 2, 0)
            {
                ctxt.gl.DeleteFramebuffers(1, [ self.id ].as_ptr());
            } else if ctxt.extensions.gl_ext_framebuffer_object {
                ctxt.gl.DeleteFramebuffersEXT(1, [ self.id ].as_ptr());
            } else {
                unreachable!();
            }
        }
    }
}

impl GlObject for FrameBufferObject {
    type Id = gl::types::GLuint;
    fn get_id(&self) -> gl::types::GLuint {
        self.id
    }
}

pub fn bind_framebuffer(ctxt: &mut CommandContext, fbo_id: gl::types::GLuint,
                        draw: bool, read: bool)
{
    if draw && ctxt.state.draw_framebuffer != fbo_id {
        unsafe {
            if ctxt.version >= &Version(Api::Gl, 3, 0) {
                ctxt.gl.BindFramebuffer(gl::DRAW_FRAMEBUFFER, fbo_id);
                ctxt.state.draw_framebuffer = fbo_id;
            } else if ctxt.version >= &Version(Api::GlEs, 2, 0) {
                ctxt.gl.BindFramebuffer(gl::FRAMEBUFFER, fbo_id);
                ctxt.state.draw_framebuffer = fbo_id;
                ctxt.state.read_framebuffer = fbo_id;
            } else {
                ctxt.gl.BindFramebufferEXT(gl::FRAMEBUFFER_EXT, fbo_id);
                ctxt.state.draw_framebuffer = fbo_id;
                ctxt.state.read_framebuffer = fbo_id;
            }
        }
    }

    if read && ctxt.state.read_framebuffer != fbo_id {
        unsafe {
            if ctxt.version >= &Version(Api::Gl, 3, 0) {
                ctxt.gl.BindFramebuffer(gl::READ_FRAMEBUFFER, fbo_id);
                ctxt.state.read_framebuffer = fbo_id;
            } else if ctxt.version >= &Version(Api::GlEs, 2, 0) {
                ctxt.gl.BindFramebuffer(gl::FRAMEBUFFER, fbo_id);
                ctxt.state.draw_framebuffer = fbo_id;
                ctxt.state.read_framebuffer = fbo_id;
            } else {
                ctxt.gl.BindFramebufferEXT(gl::FRAMEBUFFER_EXT, fbo_id);
                ctxt.state.draw_framebuffer = fbo_id;
                ctxt.state.read_framebuffer = fbo_id;
            }
        }
    }
}

unsafe fn attach(ctxt: &mut CommandContext, slot: gl::types::GLenum,
                 id: gl::types::GLuint, attachment: Attachment)
{
    if ctxt.version >= &Version(Api::Gl, 4, 5) || ctxt.extensions.gl_arb_direct_state_access {
        match attachment {
            Attachment::Texture { id: tex_id, level, layer, .. } => {
                if layer == 0 {
                    ctxt.gl.NamedFramebufferTexture(id, slot, tex_id,
                                                    level as gl::types::GLint);
                } else {
                    ctxt.gl.NamedFramebufferTextureLayer(id, slot, tex_id,
                                                         level as gl::types::GLint,
                                                         layer as gl::types::GLint);
                }
            },
            Attachment::RenderBuffer(buf_id) => {
                ctxt.gl.NamedFramebufferRenderbuffer(id, slot, gl::RENDERBUFFER,
                                                     buf_id);
            },
        }

    } else if ctxt.extensions.gl_ext_direct_state_access &&
              ctxt.extensions.gl_ext_geometry_shader4
    {
        match attachment {
            Attachment::Texture { id: tex_id, level, layer, .. } => {
                if layer == 0 {
                    ctxt.gl.NamedFramebufferTextureEXT(id, slot, tex_id,
                                                       level as gl::types::GLint);
                } else {
                    ctxt.gl.NamedFramebufferTextureLayerEXT(id, slot, tex_id,
                                                            level as gl::types::GLint,
                                                            layer as gl::types::GLint);
                }
            },
            Attachment::RenderBuffer(buf_id) => {
                ctxt.gl.NamedFramebufferRenderbufferEXT(id, slot, gl::RENDERBUFFER,
                                                        buf_id);
            },
        }

    } else if ctxt.version >= &Version(Api::Gl, 3, 2) {
        bind_framebuffer(ctxt, id, true, false);

        match attachment {
            Attachment::Texture { id: tex_id, level, layer, .. } => {
                if layer == 0 {
                    ctxt.gl.FramebufferTexture(gl::DRAW_FRAMEBUFFER,
                                               slot, tex_id, level as gl::types::GLint);
                } else {
                    ctxt.gl.FramebufferTextureLayer(gl::DRAW_FRAMEBUFFER,
                                                    slot, tex_id,
                                                    level as gl::types::GLint,
                                                    layer as gl::types::GLint);
                }
            },
            Attachment::RenderBuffer(buf_id) => {
                ctxt.gl.FramebufferRenderbuffer(gl::DRAW_FRAMEBUFFER, slot,
                                                gl::RENDERBUFFER, buf_id);
            },
        }

    } else if ctxt.version >= &Version(Api::Gl, 3, 0) {
        bind_framebuffer(ctxt, id, true, false);

        match attachment {
            Attachment::Texture { bind_point, id: tex_id, level, layer } => {
                match bind_point {
                    gl::TEXTURE_1D | gl::TEXTURE_RECTANGLE => {
                        assert!(layer == 0);
                        ctxt.gl.FramebufferTexture1D(gl::DRAW_FRAMEBUFFER,
                                                     slot, bind_point, tex_id,
                                                     level as gl::types::GLint);
                    },
                    gl::TEXTURE_2D | gl::TEXTURE_2D_MULTISAMPLE | gl::TEXTURE_1D_ARRAY => {
                        assert!(layer == 0);
                        ctxt.gl.FramebufferTexture2D(gl::DRAW_FRAMEBUFFER,
                                                     slot, bind_point, tex_id,
                                                     level as gl::types::GLint);
                    },
                    gl::TEXTURE_3D | gl::TEXTURE_2D_ARRAY | gl::TEXTURE_2D_MULTISAMPLE_ARRAY => {
                        ctxt.gl.FramebufferTextureLayer(gl::DRAW_FRAMEBUFFER,
                                                        slot, tex_id,
                                                        level as gl::types::GLint,
                                                        layer as gl::types::GLint);
                    },
                    _ => unreachable!()
                }
            },
            Attachment::RenderBuffer(buf_id) => {
                ctxt.gl.FramebufferRenderbuffer(gl::DRAW_FRAMEBUFFER, slot,
                                                gl::RENDERBUFFER, buf_id);
            },
        }

    } else if ctxt.version >= &Version(Api::GlEs, 2, 0) {
        bind_framebuffer(ctxt, id, true, true);

        match attachment {
            Attachment::Texture { bind_point, id: tex_id, level, layer } => {
                match bind_point {
                    gl::TEXTURE_2D => {
                        assert!(layer == 0);
                        ctxt.gl.FramebufferTexture2D(gl::FRAMEBUFFER,
                                                     slot, bind_point, tex_id,
                                                     level as gl::types::GLint);
                    },
                    _ => unreachable!()
                }
            },
            Attachment::RenderBuffer(buf_id) => {
                ctxt.gl.FramebufferRenderbuffer(gl::DRAW_FRAMEBUFFER, slot,
                                                gl::RENDERBUFFER, buf_id);
            },
        }

    } else if ctxt.extensions.gl_ext_framebuffer_object {
        bind_framebuffer(ctxt, id, true, true);

        match attachment {
            Attachment::Texture { bind_point, id: tex_id, level, layer } => {
                match bind_point {
                    gl::TEXTURE_1D | gl::TEXTURE_RECTANGLE => {
                        assert!(layer == 0);
                        ctxt.gl.FramebufferTexture1DEXT(gl::FRAMEBUFFER_EXT,
                                                        slot, bind_point, tex_id,
                                                        level as gl::types::GLint);
                    },
                    gl::TEXTURE_2D | gl::TEXTURE_2D_MULTISAMPLE | gl::TEXTURE_1D_ARRAY => {
                        assert!(layer == 0);
                        ctxt.gl.FramebufferTexture2DEXT(gl::FRAMEBUFFER_EXT,
                                                        slot, bind_point, tex_id,
                                                        level as gl::types::GLint);
                    },
                    gl::TEXTURE_3D | gl::TEXTURE_2D_ARRAY | gl::TEXTURE_2D_MULTISAMPLE_ARRAY => {
                        ctxt.gl.FramebufferTexture3DEXT(gl::FRAMEBUFFER_EXT,
                                                        slot, bind_point, tex_id,
                                                        level as gl::types::GLint,
                                                        layer as gl::types::GLint);
                    },
                    _ => unreachable!()
                }
            },
            Attachment::RenderBuffer(buf_id) => {
                ctxt.gl.FramebufferRenderbufferEXT(gl::DRAW_FRAMEBUFFER, slot,
                                                   gl::RENDERBUFFER, buf_id);
            },
        }

    } else {
        unreachable!();
    }
}