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
use gl::{self, types::*};
use state::StateRef;
#[cfg(feature = "gles")]
use std::ptr;


pub struct FrameBuffer{
    id: GLuint,
    gl: StateRef,
    #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    is_dsa: bool,
    #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    needs_intel_hack: bool,
}

impl Drop for FrameBuffer{
    fn drop(&mut self){
		unsafe{
			self.gl.DeleteFramebuffers(1, &self.id);
		}
    }
}

impl PartialEq for FrameBuffer {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for FrameBuffer {}

impl FrameBuffer{
    #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    pub fn new_dsa(gl: StateRef) -> FrameBuffer {
        let mut id: GLuint = 0;
        // This doesn't work on some intel cards in windows so override dsa detection
        #[cfg(target_os="windows")]
        let needs_intel_hack = gl.capabilities().vendor == Vendor::Intel;
        #[cfg(not(target_os="windows"))]
        let needs_intel_hack = false;
        unsafe{
            gl.CreateFramebuffers(1, &mut id);
            FrameBuffer{
                id,
                gl,
                is_dsa: true,
                needs_intel_hack,
            }
        }
    }

    pub fn new_bind(gl: StateRef) -> FrameBuffer {
        let mut id: GLuint = 0;
        unsafe{
            gl.GenFramebuffers(1, &mut id);
            FrameBuffer{
                id,
                gl,
                #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
                is_dsa: false,
                #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
                needs_intel_hack: false,
            }
        }
    }

    pub fn new(gl: StateRef) -> FrameBuffer {
        #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
        {
            if gl.capabilities().supports_dsa(){
                return FrameBuffer::new_dsa(gl)
            }
        }

        FrameBuffer::new_bind(gl)
    }

    pub fn id(&self) -> GLuint {
        self.id
    }

    pub fn bind(&self){
        unsafe{
            self.gl.BindFramebuffer(gl::FRAMEBUFFER, self.id);
        }
    }

    pub fn unbind(&self){
        unsafe{
            self.gl.BindFramebuffer(gl::FRAMEBUFFER, 0)
        }
    }

    pub fn check_status(&self) -> u32{
        #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
        unsafe{
            if self.is_dsa {
                return self.gl.CheckNamedFramebufferStatus(self.id, gl::FRAMEBUFFER)
            }
        }

        unsafe{
            let status = self.gl.CheckFramebufferStatus(gl::FRAMEBUFFER);
            status
        }
    }

    pub fn set_drawbuffers(&self, attachments: &[GLenum]){
        #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
        unsafe{
            if self.is_dsa {
                self.gl.NamedFramebufferDrawBuffers(
                    self.id,
                    attachments.len() as i32,
                    attachments.as_ptr());
                return;
            }
        }


        unsafe{
            #[cfg(not(feature="webgl"))]
            self.gl.DrawBuffers(attachments.len() as i32, attachments.as_ptr());

            #[cfg(feature="webgl")]
            self.gl.DrawBuffers(&attachments);
        }
    }

    pub fn set_no_drawbuffers(&self){
        #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
        unsafe{
            if self.is_dsa{
                self.gl.NamedFramebufferDrawBuffer(self.id, gl::NONE);
                return;
            }
        }

        unsafe{
            #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
            self.gl.DrawBuffer(gl::NONE);

            #[cfg(feature = "gles")]
            self.gl.DrawBuffers(0, ptr::null());

            #[cfg(feature = "webgl")]
            self.gl.DrawBuffers(&[]);
        }
    }

    #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    pub fn needs_intel_hack(&self) -> bool {
        self.needs_intel_hack
    }

    pub fn gl(&self) -> &StateRef {
        &self.gl
    }

    #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
    pub fn is_dsa(&self) -> bool {
        self.is_dsa
    }

    #[cfg(any(feature = "gles", feature="webgl"))]
    pub fn is_dsa(&self) -> bool{
        false
    }

    pub fn attach_texture(&self, texture: GLuint, textarget: GLenum, level: GLint, target: GLenum, attachment: GLenum) {
        unsafe {
            if self.is_dsa() {
                #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
                self.gl.NamedFramebufferTexture(self.id, attachment, texture, level)
            }else{
                self.gl.FramebufferTexture2D(target, attachment, textarget, texture, level)
            }
        }
    }

    pub fn attach_render_buffer(&self, renderbuffer: GLuint, target: GLenum, attachment: GLenum) {
        unsafe {
            if self.is_dsa() {
                #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
                self.gl.NamedFramebufferRenderbuffer(self.id, attachment, gl::RENDERBUFFER, renderbuffer)
            }else{
                self.gl.FramebufferRenderbuffer(target, attachment, gl::RENDERBUFFER, renderbuffer)
            }
        }
    }

    #[cfg(not(feature="webgl"))]
    pub fn attach_cubemap(&self, cubemap: GLuint, level: GLint, target: GLenum, attachment: GLenum) {
        unsafe{
            if self.is_dsa() {
                #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
                self.gl.NamedFramebufferTexture(self.id, attachment, cubemap, level)
            }else{
                self.gl.FramebufferTexture(target, attachment, cubemap, level)
            }
        }
    }

    pub fn attach_cubemap_face(&self, cubemap: GLuint, face: GLenum, level: GLint, target: GLenum, attachment: GLenum) {
        unsafe{
            if self.is_dsa() {
                #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
                if self.needs_intel_hack {
                    // glNamedFramebufferTextureLayer fails for cubemap faces on some intel cards in windows
                    // so we use the EXT version or just bind if that's not supported
                    if self.gl.capabilities().supports_dsa_ext(){
                        self.gl.NamedFramebufferTextureFaceEXT(
                            self.id,
                            attachment,
                            cubemap,
                            level,
                            face
                        );
                    }else{
                        self.bind();
                        self.gl.FramebufferTexture2D(
                            gl::FRAMEBUFFER,
                            attachment,
                            face,
                            cubemap,
                            level
                        );
                        self.unbind();
                    }
                }else{
                    self.gl.NamedFramebufferTextureLayer(
                        self.id(),
                        attachment,
                        cubemap,
                        level,
                        (face - gl::TEXTURE_CUBE_MAP_POSITIVE_X) as i32
                    );
                }
            }else{
                self.gl.FramebufferTexture2D(target, attachment, face, cubemap, level)
            }
        }
    }

    pub fn attach_texture_layer(&self, texture: GLuint, textarget: GLenum, level: GLint, attachment: GLenum, layer: GLint) {
        unsafe{
            if self.is_dsa() {
                #[cfg(all(not(feature = "gles"), not(feature="webgl")))]
                self.gl.NamedFramebufferTextureLayer(
                    self.id(),
                    attachment,
                    texture,
                    level,
                    layer,
                )
            }else{
                self.gl.FramebufferTextureLayer(
                    textarget,
                    attachment,
                    texture,
                    level,
                    layer
                )
            }
        }
    }

}