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
use std::ptr;
use std::fmt;
use std::error::Error;

use pixel_buffer::PixelBuffer;
use texture::ClientFormat;
use texture::PixelValue;
use image_format::{TextureFormatRequest, TextureFormat};

use fbo;
use fbo::FramebuffersContainer;

use buffer::BufferAny;
use BufferExt;
use Rect;
use context::CommandContext;
use gl;

use version::Version;
use version::Api;

/// A source for reading pixels.
pub enum Source<'a> {
    /// A regular framebuffer attachment.
    Attachment(&'a fbo::RegularAttachment<'a>),
    // TODO: use a Rust enum
    DefaultFramebuffer(gl::types::GLenum),
}

impl<'a> From<&'a fbo::RegularAttachment<'a>> for Source<'a> {
    #[inline]
    fn from(a: &'a fbo::RegularAttachment<'a>) -> Source<'a> {
        Source::Attachment(a)
    }
}

/// A destination for reading pixels.
pub enum Destination<'a, P> where P: PixelValue {
    Memory(&'a mut Vec<P>),
    PixelBuffer(&'a PixelBuffer<P>),
    // TODO: texture with glCopyTexSubImage2D
}

impl<'a, P> From<&'a mut Vec<P>> for Destination<'a, P> where P: PixelValue {
    #[inline]
    fn from(mem: &'a mut Vec<P>) -> Destination<'a, P> {
        Destination::Memory(mem)
    }
}

impl<'a, P> From<&'a PixelBuffer<P>> for Destination<'a, P> where P: PixelValue {
    #[inline]
    fn from(pb: &'a PixelBuffer<P>) -> Destination<'a, P> {
        Destination::PixelBuffer(pb)
    }
}

/// Error that can happen while reading.
#[derive(Debug)]
pub enum ReadError {
    /// The implementation doesn't support converting to the requested output format.
    ///
    /// OpenGL supports every possible format, but OpenGL ES only supports `(u8, u8, u8, u8)` and
    /// an implementation-defined format.
    OutputFormatNotSupported,

    /// The implementation doesn't support reading a depth, depth-stencil or stencil attachment.
    ///
    /// OpenGL ES only supports reading from color buffers by default. There are extensions that
    /// allow reading other types of attachments.
    AttachmentTypeNotSupported,

    /// Clamping the values is not supported by the implementation.
    ClampingNotSupported,

    // TODO: context lost
}

impl fmt::Display for ReadError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        use self::ReadError::*;
        let desc = match *self {
            OutputFormatNotSupported =>
                "The implementation doesn't support converting to the requested output format",
            AttachmentTypeNotSupported =>
                "The implementation doesn't support reading a depth, depth-stencil or stencil attachment",
            ClampingNotSupported =>
                "Clamping the values is not supported by the implementation",
        };
        fmt.write_str(desc)
    }
}

impl Error for ReadError {}

/// Reads pixels from the source into the destination.
///
/// Panics if the destination is not large enough.
///
/// The `(u8, u8, u8, u8)` format is guaranteed to be supported.
// TODO: differentiate between GL_* and GL_*_INTEGER
#[inline]
pub fn read<'a, S, D, T>(mut ctxt: &mut CommandContext, source: S, rect: &Rect, dest: D,
                         clamp: bool) -> Result<(), ReadError>
                         where S: Into<Source<'a>>, D: Into<Destination<'a, T>>,
                               T: PixelValue
{
    let source = source.into();
    let dest = dest.into();
    let output_pixel_format = <T as PixelValue>::get_format();

    let pixels_to_read = rect.width * rect.height;

    // checking that the output format is supported
    // OpenGL supported everything, while OpenGL ES only supports U8U8U8U8 plus an additional
    // implementation-defined format
    if ctxt.version >= &Version(Api::GlEs, 2, 0) && output_pixel_format != ClientFormat::U8U8U8U8 {
        // TODO: GLES is guaranteed to support GL_RGBA and an implementation-defined format
        //       queried with GL_IMPLEMENTATION_COLOR_READ_FORMAT. We only handle GL_RGBA.
        return Err(ReadError::OutputFormatNotSupported);
    }

    // handling clamping
    if ctxt.version >= &Version(Api::Gl, 3, 0) {
        unsafe {
            if clamp && ctxt.state.clamp_color != gl::TRUE as gl::types::GLenum {
                ctxt.gl.ClampColor(gl::CLAMP_READ_COLOR, gl::TRUE as gl::types::GLenum);
                ctxt.state.clamp_color = gl::TRUE as gl::types::GLenum;

            } else if !clamp && ctxt.state.clamp_color != gl::FALSE as gl::types::GLenum {
                ctxt.gl.ClampColor(gl::CLAMP_READ_COLOR, gl::FALSE as gl::types::GLenum);
                ctxt.state.clamp_color = gl::FALSE as gl::types::GLenum;
            }
        }
    } else {
        if clamp {
            return Err(ReadError::ClampingNotSupported);
        }
    }

    // TODO: check dimensions?

    // binding framebuffer
    match source {
        Source::Attachment(attachment) => {
            unsafe { FramebuffersContainer::bind_framebuffer_for_reading(&mut ctxt, attachment) };
        },
        Source::DefaultFramebuffer(read_buffer) => {
            FramebuffersContainer::bind_default_framebuffer_for_reading(&mut ctxt, read_buffer);
        },
    };

    // determining what kind of data we are reading
    enum ReadSourceType { Color, Depth, Stencil, DepthStencil }
    let (integer, read_src_type) = match source {
        Source::Attachment(attachment) => {
            match attachment {
                &fbo::RegularAttachment::Texture(ref tex) => {
                    let integer = match tex.get_texture().get_requested_format() {
                        TextureFormatRequest::Specific(TextureFormat::UncompressedIntegral(_)) => true,
                        TextureFormatRequest::Specific(TextureFormat::UncompressedUnsigned(_)) => true,
                        TextureFormatRequest::AnyIntegral => true,
                        TextureFormatRequest::AnyUnsigned => true,
                        _ => false,
                    };

                    (integer, ReadSourceType::Color)       // FIXME: wrong
                },
                &fbo::RegularAttachment::RenderBuffer(ref rb) => {
                    (false, ReadSourceType::Color)       // FIXME: wrong
                },
            }
        },
        Source::DefaultFramebuffer(read_buffer) => {
            (false, ReadSourceType::Color)       // FIXME: wrong
        },
    };

    // OpenGL ES doesn't support reading from depth, stencil or depth-stencil attachments by default
    if ctxt.version >= &Version(Api::GlEs, 2, 0) {
        match read_src_type {
            ReadSourceType::Color => (),
            ReadSourceType::Depth => if !ctxt.extensions.gl_nv_read_depth {
                return Err(ReadError::AttachmentTypeNotSupported);
            },
            ReadSourceType::DepthStencil => if !ctxt.extensions.gl_nv_read_depth_stencil {
                return Err(ReadError::AttachmentTypeNotSupported);
            },
            ReadSourceType::Stencil => if !ctxt.extensions.gl_nv_read_stencil {
                return Err(ReadError::AttachmentTypeNotSupported);
            },
        }
    }

    // obtaining the client format and client type to be passed to `glReadPixels`
    let (format, gltype) = match read_src_type {
        ReadSourceType::Color => {
            client_format_to_gl_enum(&output_pixel_format, integer)
        },
        ReadSourceType::Depth => {
            unimplemented!()        // TODO:
            // TODO: NV_depth_buffer_float2
            //(gl::DEPTH_COMPONENT, )
        },
        ReadSourceType::DepthStencil => unimplemented!(),        // FIXME: only 24_8 is possible and there's no client format in the enum that corresponds to 24_8
        ReadSourceType::Stencil => {
            unimplemented!()        // TODO:
            //(gl::STENCIL_INDEX, )
        },
    };

    // reading
    unsafe {
        match dest {
            Destination::Memory(dest) => {
                let mut buf = Vec::with_capacity(pixels_to_read as usize);

                BufferAny::unbind_pixel_pack(ctxt);

                // adjusting data alignement
                let ptr = buf.as_mut_ptr() as *mut D;
                let ptr = ptr as usize;
                if (ptr % 8) == 0 {
                } else if (ptr % 4) == 0 && ctxt.state.pixel_store_pack_alignment != 4 {
                    ctxt.state.pixel_store_pack_alignment = 4;
                    ctxt.gl.PixelStorei(gl::PACK_ALIGNMENT, 4);
                } else if (ptr % 2) == 0 && ctxt.state.pixel_store_pack_alignment > 2 {
                    ctxt.state.pixel_store_pack_alignment = 2;
                    ctxt.gl.PixelStorei(gl::PACK_ALIGNMENT, 2);
                } else if ctxt.state.pixel_store_pack_alignment != 1 {
                    ctxt.state.pixel_store_pack_alignment = 1;
                    ctxt.gl.PixelStorei(gl::PACK_ALIGNMENT, 1);
                }

                ctxt.gl.ReadPixels(rect.left as gl::types::GLint, rect.bottom as gl::types::GLint,
                                   rect.width as gl::types::GLsizei,
                                   rect.height as gl::types::GLsizei, format, gltype,
                                   buf.as_mut_ptr() as *mut _);
                buf.set_len(pixels_to_read as usize);

                *dest = buf;
            },

            Destination::PixelBuffer(pixel_buffer) => {
                assert!(pixel_buffer.len() >= pixels_to_read as usize);

                pixel_buffer.prepare_and_bind_for_pixel_pack(&mut ctxt);
                ctxt.gl.ReadPixels(rect.left as gl::types::GLint, rect.bottom as gl::types::GLint,
                                   rect.width as gl::types::GLsizei,
                                   rect.height as gl::types::GLsizei, format, gltype,
                                   ptr::null_mut());

                ::pixel_buffer::store_infos(pixel_buffer, (rect.width, rect.height));
            }
        }
    };

    Ok(())
}

fn client_format_to_gl_enum(format: &ClientFormat, integer: bool)
                            -> (gl::types::GLenum, gl::types::GLenum)
{
    let (format, ty) = match *format {
        ClientFormat::U8 => (gl::RED, gl::UNSIGNED_BYTE),
        ClientFormat::U8U8 => (gl::RG, gl::UNSIGNED_BYTE),
        ClientFormat::U8U8U8 => (gl::RGB, gl::UNSIGNED_BYTE),
        ClientFormat::U8U8U8U8 => (gl::RGBA, gl::UNSIGNED_BYTE),
        ClientFormat::I8 => (gl::RED, gl::BYTE),
        ClientFormat::I8I8 => (gl::RG, gl::BYTE),
        ClientFormat::I8I8I8 => (gl::RGB, gl::BYTE),
        ClientFormat::I8I8I8I8 => (gl::RGBA, gl::BYTE),
        ClientFormat::U16 => (gl::RED, gl::UNSIGNED_SHORT),
        ClientFormat::U16U16 => (gl::RG, gl::UNSIGNED_SHORT),
        ClientFormat::U16U16U16 => (gl::RGB, gl::UNSIGNED_SHORT),
        ClientFormat::U16U16U16U16 => (gl::RGBA, gl::UNSIGNED_SHORT),
        ClientFormat::I16 => (gl::RED, gl::SHORT),
        ClientFormat::I16I16 => (gl::RG, gl::SHORT),
        ClientFormat::I16I16I16 => (gl::RGB, gl::SHORT),
        ClientFormat::I16I16I16I16 => (gl::RGBA, gl::SHORT),
        ClientFormat::U32 => (gl::RED, gl::UNSIGNED_INT),
        ClientFormat::U32U32 => (gl::RG, gl::UNSIGNED_INT),
        ClientFormat::U32U32U32 => (gl::RGB, gl::UNSIGNED_INT),
        ClientFormat::U32U32U32U32 => (gl::RGBA, gl::UNSIGNED_INT),
        ClientFormat::I32 => (gl::RED, gl::INT),
        ClientFormat::I32I32 => (gl::RG, gl::INT),
        ClientFormat::I32I32I32 => (gl::RGB, gl::INT),
        ClientFormat::I32I32I32I32 => (gl::RGBA, gl::INT),
        ClientFormat::U3U3U2 => (gl::RGB, gl::UNSIGNED_BYTE_3_3_2),
        ClientFormat::U5U6U5 => (gl::RGB, gl::UNSIGNED_SHORT_5_6_5),
        ClientFormat::U4U4U4U4 => (gl::RGBA, gl::UNSIGNED_SHORT_4_4_4_4),
        ClientFormat::U5U5U5U1 => (gl::RGBA, gl::UNSIGNED_SHORT_5_5_5_1),
        ClientFormat::U10U10U10U2 => (gl::RGBA, gl::UNSIGNED_INT_10_10_10_2),
        ClientFormat::F16 => (gl::RED, gl::HALF_FLOAT),
        ClientFormat::F16F16 => (gl::RG, gl::HALF_FLOAT),
        ClientFormat::F16F16F16 => (gl::RGB, gl::HALF_FLOAT),
        ClientFormat::F16F16F16F16 => (gl::RGBA, gl::HALF_FLOAT),
        ClientFormat::F32 => (gl::RED, gl::FLOAT),
        ClientFormat::F32F32 => (gl::RG, gl::FLOAT),
        ClientFormat::F32F32F32 => (gl::RGB, gl::FLOAT),
        ClientFormat::F32F32F32F32 => (gl::RGBA, gl::FLOAT),
    };

    let format = if integer {
        match format {
            gl::RED => gl::RED_INTEGER,
            gl::RG => gl::RG_INTEGER,
            gl::RGB => gl::RGB_INTEGER,
            gl::RGBA => gl::RGBA_INTEGER,
            _ => unreachable!()
        }
    } else {
        format
    };

    (format, ty)
}