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
use gl;
use gl::types::*;

use super::{Attachment, GLTexture};

#[derive(Debug, Hash)]
pub struct GLFramebuffer {
    id: GLuint,
}

impl GLFramebuffer {
    #[inline(always)]
    pub fn new(gl_texture: &GLTexture, buffers: &[Attachment], level: isize) -> Self {
        let framebuffer = GLFramebuffer {
            id: {
                let mut id = 0;
                unsafe {
                    gl::GenFramebuffers(1, &mut id);
                }
                id
            },
        };

        framebuffer.set(gl_texture, buffers, level);
        framebuffer
    }

    #[inline(always)]
    pub fn id(&self) -> GLuint {
        self.id
    }

    #[inline]
    pub fn bind(&self) -> &Self {
        unsafe {
            gl::BindFramebuffer(gl::FRAMEBUFFER, self.id);
        }
        self
    }
    #[inline]
    pub fn unbind(&self) -> &Self {
        unsafe {
            gl::BindFramebuffer(gl::FRAMEBUFFER, 0);
        }
        self
    }

    #[inline]
    pub fn set(&self, gl_texture: &GLTexture, buffers: &[Attachment], level: isize) {
        let gl_texture_id = gl_texture.id();

        let mut gl_enums = Vec::with_capacity(buffers.len());
        for i in 0..buffers.len() {
            gl_enums.push(buffers[i].into());
        }

        unsafe {
            gl::BindFramebuffer(gl::FRAMEBUFFER, self.id);
            gl::BindTexture(gl_texture.kind().into(), gl_texture_id);

            for i in 0..gl_enums.len() {
                gl::FramebufferTexture2D(
                    gl::FRAMEBUFFER,
                    gl_enums[i],
                    gl::TEXTURE_2D,
                    gl_texture_id,
                    level as GLint,
                );
            }
            gl::DrawBuffers(buffers.len() as GLint, gl_enums.as_ptr());

            match gl::CheckFramebufferStatus(gl::FRAMEBUFFER) {
                gl::FRAMEBUFFER_UNDEFINED => panic!("Check framebuffer status failed undefined"),
                gl::FRAMEBUFFER_INCOMPLETE_ATTACHMENT => {
                    panic!("Check framebuffer status failed incomplete attachment")
                }
                gl::FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT => {
                    panic!("Check framebuffer status failed incomplete missing attachment")
                }
                gl::FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER => {
                    panic!("Check framebuffer status failed incomplete draw buffer")
                }
                gl::FRAMEBUFFER_INCOMPLETE_READ_BUFFER => {
                    panic!("Check framebuffer status failed incomplete read buffer")
                }
                gl::FRAMEBUFFER_UNSUPPORTED => {
                    panic!("Check framebuffer status failed unsupported")
                }
                gl::FRAMEBUFFER_INCOMPLETE_MULTISAMPLE => {
                    panic!("Check framebuffer status failed incomplete multisample")
                }
                gl::FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS => {
                    panic!("Check framebuffer status failed incomplete layer targets")
                }
                // gl::FRAMEBUFFER_COMPLETE => (),
                _ => (),
            }
        }
    }
}

impl Drop for GLFramebuffer {
    #[inline]
    fn drop(&mut self) {
        if self.id != 0 {
            unsafe {
                gl::DeleteFramebuffers(1, &self.id);
            }
        }
    }
}