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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Framebuffers

use __gl;
use __gl::types::{GLenum, GLuint};

use debug::{Object, ObjectType};
use device::Device;
use error::Result;
use ImageView;
use Region;

/// Attachment clearing description.
pub enum ClearAttachment {
    ColorInt(usize, [i32; 4]),
    ColorUint(usize, [u32; 4]),
    ColorFloat(usize, [f32; 4]),
    Depth(f32),
    Stencil(i32),
    DepthStencil(f32, i32),
}

/// Attachment reference.
pub enum Attachment {
    Color(usize),
    Depth,
    Stencil,
    DepthStencil,
}

///
pub enum AttachmentView<'a> {
    Image(&'a ImageView),
    Renderbuffer(&'a Renderbuffer),
}

/// Framebuffer handle.
#[repr(transparent)]
pub struct Framebuffer(GLuint);

impl Framebuffer {
    /// Default framebuffer handle.
    ///
    /// Thie is the base framebuffer associated with the context.
    /// It also represents the internal swapchain for presentation.
    pub const DEFAULT: &'static Self = &Framebuffer(0);
}

impl Object for Framebuffer {
    const TYPE: ObjectType = ObjectType::Framebuffer;
    fn handle(&self) -> GLuint {
        self.0
    }
}

/// Renderbuffer handle.
#[repr(transparent)]
pub struct Renderbuffer(GLuint);

impl Object for Renderbuffer {
    const TYPE: ObjectType = ObjectType::Renderbuffer;
    fn handle(&self) -> GLuint {
        self.0
    }
}

impl Device {
    /// Create a new framebuffer.
    pub fn create_framebuffer(&self) -> Result<Framebuffer> {
        let mut framebuffer = 0;
        unsafe {
            self.0.CreateFramebuffers(1, &mut framebuffer);
        }
        self.get_error()?;

        Ok(Framebuffer(framebuffer))
    }

    /// Delete a framebuffer.
    pub fn delete_framebuffer(&self, framebuffer: Framebuffer) {
        self.delete_framebuffers(&[framebuffer])
    }

    /// Delete multiple framebuffers.
    pub fn delete_framebuffers(&self, framebuffers: &[Framebuffer]) {
        unsafe {
            self.0.DeleteFramebuffers(
                framebuffers.len() as _,
                framebuffers.as_ptr() as *const _, // newtype
            );
        }
    }

    /// Create a new framebuffer.
    pub fn create_renderbuffer(&self) -> Result<Renderbuffer> {
        let mut renderbuffer = 0;
        unsafe {
            self.0.CreateRenderbuffers(1, &mut renderbuffer);
        }
        self.get_error()?;

        Ok(Renderbuffer(renderbuffer))
    }

    /// Delete a renderbuffer.
    pub fn delete_renderbuffer(&self, renderbuffer: Renderbuffer) {
        self.delete_renderbuffers(&[renderbuffer])
    }

    /// Delete multiple renderbuffers.
    pub fn delete_renderbuffers(&self, renderbuffers: &[Renderbuffer]) {
        unsafe {
            self.0.DeleteRenderbuffers(
                renderbuffers.len() as _,
                renderbuffers.as_ptr() as *const _, // newtype
            );
        }
    }

    /// Clear framebuffer attachment.
    pub fn clear_attachment(&self, fb: &Framebuffer, cv: ClearAttachment) {
        unsafe {
            match cv {
                ClearAttachment::ColorInt(id, color) => {
                    self.0
                        .ClearNamedFramebufferiv(fb.0, __gl::COLOR, id as _, color.as_ptr());
                }
                ClearAttachment::ColorUint(id, color) => {
                    self.0
                        .ClearNamedFramebufferuiv(fb.0, __gl::COLOR, id as _, color.as_ptr());
                }
                ClearAttachment::ColorFloat(id, color) => {
                    self.0
                        .ClearNamedFramebufferfv(fb.0, __gl::COLOR, id as _, color.as_ptr());
                }
                ClearAttachment::Depth(depth) => {
                    self.0
                        .ClearNamedFramebufferfv(fb.0, __gl::DEPTH, 0, &depth as *const _);
                }
                ClearAttachment::Stencil(stencil) => {
                    self.0
                        .ClearNamedFramebufferiv(fb.0, __gl::STENCIL, 0, &stencil as *const _);
                }
                ClearAttachment::DepthStencil(depth, stencil) => {
                    self.0
                        .ClearNamedFramebufferfi(fb.0, __gl::DEPTH_STENCIL, 0, depth, stencil);
                }
            }
        }
    }

    ///
    pub fn invalidate_attachments(
        &self,
        framebuffer: &Framebuffer,
        attachments: &[Attachment],
        region: Region,
    ) {
        let attachments = attachments
            .iter()
            .map(|att| match att {
                Attachment::Color(slot) => __gl::COLOR_ATTACHMENT0 + *slot as u32,
                Attachment::Depth => __gl::DEPTH_ATTACHMENT,
                Attachment::Stencil => __gl::STENCIL_ATTACHMENT,
                Attachment::DepthStencil => __gl::DEPTH_STENCIL_ATTACHMENT,
            })
            .collect::<Vec<_>>();

        unsafe {
            self.0.InvalidateNamedFramebufferSubData(
                framebuffer.0,
                attachments.len() as _,
                attachments.as_ptr(),
                region.x,
                region.y,
                region.w,
                region.h,
            )
        }
    }

    /// Bind a framebuffer for draw commands.
    pub fn bind_framebuffer(&self, framebuffer: &Framebuffer) {
        unsafe {
            self.0
                .BindFramebuffer(__gl::DRAW_FRAMEBUFFER, framebuffer.0);
        }
    }

    /// Bind attachments to the framebuffer.
    ///
    /// All previously bound attachments become invalid.
    pub fn bind_attachments(
        &self,
        framebuffer: &Framebuffer,
        color_attachments: &[AttachmentView],
        depth_stencil_attachment: Option<AttachmentView>,
    ) {
        assert_ne!(
            framebuffer.0, 0,
            "The default framebuffer can't be changed."
        );

        let bind_attachment_view = |attachment: GLenum, view: &AttachmentView| unsafe {
            match *view {
                AttachmentView::Image(image) => {
                    self.0
                        .NamedFramebufferTexture(framebuffer.0, attachment, image.0, 0);
                }
                AttachmentView::Renderbuffer(_) => unimplemented!(),
            }
        };

        for (i, attachment) in color_attachments.iter().enumerate() {
            bind_attachment_view((__gl::COLOR_ATTACHMENT0 as usize + i) as _, attachment);
        }

        for attachment in depth_stencil_attachment {
            bind_attachment_view(__gl::DEPTH_STENCIL_ATTACHMENT, &attachment);
        }
    }

    /// Specify color attachments.
    ///
    /// Defines the color render targets for the next draw calls.
    /// This builds the link between fragment outputs in the fragment shader
    /// and attachments bound on the framebuffer.
    pub fn set_color_attachments(&self, framebuffer: &Framebuffer, attachments: &[u32]) {
        assert_ne!(
            framebuffer.0, 0,
            "The default framebuffer can't be changed."
        );

        let attachments = attachments
            .iter()
            .map(|i| i + __gl::COLOR_ATTACHMENT0)
            .collect::<Vec<_>>();
        unsafe {
            self.0.NamedFramebufferDrawBuffers(
                framebuffer.0,
                attachments.len() as _,
                attachments.as_ptr(),
            );
        }
    }
}