glium 0.5.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
use gl;
use GlObject;

use backend::Facade;
use version::Version;
use context::Context;
use ContextExt;
use TextureExt;
use version::Api;
use Rect;

use pixel_buffer::PixelBuffer;
use image_format::{self, TextureFormatRequest};
use texture::Texture2dDataSink;
use texture::{TextureFormat, ClientFormat};
use texture::{TextureCreationError, TextureMaybeSupportedCreationError};
use texture::{get_format, InternalFormat};

use libc;
use std::fmt;
use std::mem;
use std::ptr;
use std::borrow::Cow;
use std::cell::Cell;
use std::rc::Rc;

use ops;
use fbo;

/// A texture whose type isn't fixed at compile-time.
pub struct TextureAny {
    context: Rc<Context>,
    id: gl::types::GLuint,
    requested_format: TextureFormatRequest,

    /// Cache for the actual format of the texture. The outer Option is None if the format hasn't
    /// been checked yet. The inner Option is None if the format has been checkek but is unknown.
    actual_format: Cell<Option<Option<InternalFormat>>>,

    bind_point: gl::types::GLenum,
    ty: TextureType,
    width: u32,
    height: Option<u32>,
    depth: Option<u32>,
    array_size: Option<u32>,

    /// Number of mipmap levels (`1` means just the main texture, `0` is not valid)
    levels: u32,
}

/// Represents a specific mipmap of a texture.
#[derive(Copy, Clone)]
pub struct TextureAnyMipmap<'a> {
    /// The texture.
    texture: &'a TextureAny,

    /// Layer for array textures, or 0 for other textures.
    layer: u32,

    /// Mipmap level.
    level: u32,
}

/// Type of a texture.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[allow(missing_docs)]      // TODO: document and remove
pub enum TextureType {
    Texture1d,
    Texture1dArray,
    Texture2d,
    Texture2dArray,
    Texture2dMultisample,
    Texture2dMultisampleArray,
    Texture3d,
}

/// Builds a new texture.
pub fn new_texture<'a, F, P>(facade: &F, format: TextureFormatRequest,
                             data: Option<(ClientFormat, Cow<'a, [P]>)>, generate_mipmaps: bool,
                             width: u32, height: Option<u32>, depth: Option<u32>,
                             array_size: Option<u32>, samples: Option<u32>)
                             -> Result<TextureAny, TextureMaybeSupportedCreationError>
                             where P: Send + Clone + 'a, F: Facade
{
    if let Some((client_format, ref data)) = data {
        if width as usize * height.unwrap_or(1) as usize * depth.unwrap_or(1) as usize *
            array_size.unwrap_or(1) as usize * client_format.get_size() !=
            data.len() * mem::size_of::<P>()
        {
            panic!("Texture data size mismatch");
        }
    }

    // checking non-power-of-two
    if facade.get_context().get_version() < &Version(Api::Gl, 2, 0) &&
        !facade.get_context().get_extensions().gl_arb_texture_non_power_of_two
    {
        if !width.is_power_of_two() || !height.unwrap_or(2).is_power_of_two() ||
            !depth.unwrap_or(2).is_power_of_two() || !array_size.unwrap_or(2).is_power_of_two()
        {
            let ce = TextureCreationError::DimensionsNotSupported;
            return Err(TextureMaybeSupportedCreationError::CreationError(ce));
        }
    }

    let (stored_ty, texture_type) = if height.is_none() && depth.is_none() {
        assert!(samples.is_none());
        if array_size.is_none() {
            (TextureType::Texture1d, gl::TEXTURE_1D)
        } else {
            (TextureType::Texture1dArray, gl::TEXTURE_1D_ARRAY)
        }

    } else if depth.is_none() {
        match (array_size.is_some(), samples.is_some()) {
            (false, false) => (TextureType::Texture2d, gl::TEXTURE_2D),
            (true, false) => (TextureType::Texture2dArray, gl::TEXTURE_2D_ARRAY),
            (false, true) => (TextureType::Texture2dMultisample, gl::TEXTURE_2D_MULTISAMPLE),
            (true, true) => (TextureType::Texture2dMultisampleArray, gl::TEXTURE_2D_MULTISAMPLE_ARRAY),
        }

    } else {
        assert!(samples.is_none());
        (TextureType::Texture3d, gl::TEXTURE_3D)
    };

    let generate_mipmaps = generate_mipmaps && match format {
        TextureFormatRequest::AnyFloatingPoint |
        TextureFormatRequest::Specific(TextureFormat::UncompressedFloat(_)) |
        TextureFormatRequest::AnyIntegral |
        TextureFormatRequest::Specific(TextureFormat::UncompressedIntegral(_)) |
        TextureFormatRequest::AnyUnsigned |
        TextureFormatRequest::Specific(TextureFormat::UncompressedUnsigned(_)) => true,
        _ => false,
    };

    let texture_levels = if generate_mipmaps {
        let max = ::std::cmp::max(width, ::std::cmp::max(height.unwrap_or(1),
                             depth.unwrap_or(1))) as f32;

        match max {
            0.0 => 1,
            a => 1 + a.log2() as gl::types::GLsizei
        }

    } else {
        1
    };

    let (teximg_internal_format, storage_internal_format) =
        try!(image_format::format_request_to_glenum(facade.get_context(), data.as_ref().map(|&(c, _)| c), format));

    let (client_format, client_type) = match (&data, format) {
        (&Some((client_format, _)), f) => image_format::client_format_to_glenum(facade.get_context(), client_format, f),
        (&None, TextureFormatRequest::AnyDepth) => (gl::DEPTH_COMPONENT, gl::FLOAT),
        (&None, TextureFormatRequest::Specific(TextureFormat::DepthFormat(_))) => (gl::DEPTH_COMPONENT, gl::FLOAT),
        (&None, TextureFormatRequest::AnyDepthStencil) => (gl::DEPTH_STENCIL, gl::UNSIGNED_INT_24_8),
        (&None, TextureFormatRequest::Specific(TextureFormat::DepthStencilFormat(_))) => (gl::DEPTH_STENCIL, gl::UNSIGNED_INT_24_8),
        (&None, _) => (gl::RGBA, gl::UNSIGNED_BYTE),
    };

    let mut ctxt = facade.get_context().make_current();

    let id = unsafe {
        let data = data;
        let data_raw = if let Some((_, ref data)) = data {
            data.as_ptr() as *const libc::c_void
        } else {
            ptr::null()
        };

        if ctxt.state.pixel_store_unpack_alignment != 1 {
            ctxt.state.pixel_store_unpack_alignment = 1;
            ctxt.gl.PixelStorei(gl::UNPACK_ALIGNMENT, 1);
        }

        if ctxt.state.pixel_unpack_buffer_binding != 0 {
            ctxt.state.pixel_unpack_buffer_binding = 0;
            ctxt.gl.BindBuffer(gl::PIXEL_UNPACK_BUFFER, 0);
        }

        let id: gl::types::GLuint = mem::uninitialized();
        ctxt.gl.GenTextures(1, mem::transmute(&id));

        {
            ctxt.gl.BindTexture(texture_type, id);
            let act = ctxt.state.active_texture as usize;
            ctxt.state.texture_units[act].texture = id;
        }

        ctxt.gl.TexParameteri(texture_type, gl::TEXTURE_WRAP_S, gl::REPEAT as i32);
        if height.is_some() || depth.is_some() || array_size.is_some() {
            ctxt.gl.TexParameteri(texture_type, gl::TEXTURE_WRAP_T, gl::REPEAT as i32);
        }
        if depth.is_some() || array_size.is_some() {
            ctxt.gl.TexParameteri(texture_type, gl::TEXTURE_WRAP_R, gl::REPEAT as i32);
        }
        ctxt.gl.TexParameteri(texture_type, gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32);
        if generate_mipmaps {
            ctxt.gl.TexParameteri(texture_type, gl::TEXTURE_MIN_FILTER,
                                  gl::LINEAR_MIPMAP_LINEAR as i32);
        } else {
            ctxt.gl.TexParameteri(texture_type, gl::TEXTURE_MIN_FILTER,
                                  gl::LINEAR as i32);
        }

        if !generate_mipmaps && (ctxt.version >= &Version(Api::Gl, 1, 2) ||
                                 ctxt.version >= &Version(Api::GlEs, 3, 0))
        {
            ctxt.gl.TexParameteri(texture_type, gl::TEXTURE_BASE_LEVEL, 0);
            ctxt.gl.TexParameteri(texture_type, gl::TEXTURE_MAX_LEVEL, 0);
        }

        if texture_type == gl::TEXTURE_3D || texture_type == gl::TEXTURE_2D_ARRAY {
            let mut data_raw = data_raw;

            let width = match width as gl::types::GLsizei {
                0 => { data_raw = ptr::null(); 1 },
                a => a
            };

            let height = match height.unwrap() as gl::types::GLsizei {
                0 => { data_raw = ptr::null(); 1 },
                a => a
            };

            let depth = match depth.or(array_size).unwrap() as gl::types::GLsizei {
                0 => { data_raw = ptr::null(); 1 },
                a => a
            };

            if storage_internal_format.is_some() && (ctxt.version >= &Version(Api::Gl, 4, 2) || ctxt.extensions.gl_arb_texture_storage) {
                ctxt.gl.TexStorage3D(texture_type, texture_levels,
                                     storage_internal_format.unwrap() as gl::types::GLenum,
                                     width, height, depth);

                if !data_raw.is_null() {
                    ctxt.gl.TexSubImage3D(texture_type, 0, 0, 0, 0, width, height, depth,
                                          client_format, client_type, data_raw);
                }

            } else {
                ctxt.gl.TexImage3D(texture_type, 0, teximg_internal_format as i32, width,
                                   height, depth, 0, client_format as u32, client_type,
                                   data_raw);
            }

        } else if texture_type == gl::TEXTURE_2D || texture_type == gl::TEXTURE_1D_ARRAY {
            let mut data_raw = data_raw;

            let width = match width as gl::types::GLsizei {
                0 => { data_raw = ptr::null(); 1 },
                a => a
            };

            let height = match height.or(array_size).unwrap() as gl::types::GLsizei {
                0 => { data_raw = ptr::null(); 1 },
                a => a
            };

            if storage_internal_format.is_some() && (ctxt.version >= &Version(Api::Gl, 4, 2) || ctxt.extensions.gl_arb_texture_storage) {
                ctxt.gl.TexStorage2D(texture_type, texture_levels,
                                     storage_internal_format.unwrap() as gl::types::GLenum,
                                     width, height);

                if !data_raw.is_null() {
                    ctxt.gl.TexSubImage2D(texture_type, 0, 0, 0, width, height, client_format,
                                          client_type, data_raw);
                }

            } else {
                ctxt.gl.TexImage2D(texture_type, 0, teximg_internal_format as i32, width,
                                   height, 0, client_format as u32, client_type, data_raw);
            }

        } else if texture_type == gl::TEXTURE_2D_MULTISAMPLE {
            assert!(data_raw.is_null());

            let width = match width as gl::types::GLsizei {
                0 => 1,
                a => a
            };

            let height = match height.unwrap() as gl::types::GLsizei {
                0 => 1,
                a => a
            };

            if storage_internal_format.is_some() && (ctxt.version >= &Version(Api::Gl, 4, 2) || ctxt.extensions.gl_arb_texture_storage) {
                ctxt.gl.TexStorage2DMultisample(gl::TEXTURE_2D_MULTISAMPLE,
                                                samples.unwrap() as gl::types::GLsizei,
                                                storage_internal_format.unwrap() as gl::types::GLenum,
                                                width, height, gl::TRUE);

            } else if ctxt.version >= &Version(Api::Gl, 3, 2) || ctxt.extensions.gl_arb_texture_multisample {
                ctxt.gl.TexImage2DMultisample(gl::TEXTURE_2D_MULTISAMPLE,
                                              samples.unwrap() as gl::types::GLsizei,
                                              teximg_internal_format as gl::types::GLenum,
                                              width, height, gl::TRUE);

            } else {
                unreachable!();
            }

        } else if texture_type == gl::TEXTURE_2D_MULTISAMPLE_ARRAY {
            assert!(data_raw.is_null());

            let width = match width as gl::types::GLsizei {
                0 => 1,
                a => a
            };

            let height = match height.unwrap() as gl::types::GLsizei {
                0 => 1,
                a => a
            };

            if storage_internal_format.is_some() && (ctxt.version >= &Version(Api::Gl, 4, 2) || ctxt.extensions.gl_arb_texture_storage) {
                ctxt.gl.TexStorage3DMultisample(gl::TEXTURE_2D_MULTISAMPLE_ARRAY,
                                                samples.unwrap() as gl::types::GLsizei,
                                                storage_internal_format.unwrap() as gl::types::GLenum,
                                                width, height, array_size.unwrap() as gl::types::GLsizei,
                                                gl::TRUE);

            } else if ctxt.version >= &Version(Api::Gl, 3, 2) || ctxt.extensions.gl_arb_texture_multisample {
                ctxt.gl.TexImage3DMultisample(gl::TEXTURE_2D_MULTISAMPLE_ARRAY,
                                              samples.unwrap() as gl::types::GLsizei,
                                              teximg_internal_format as gl::types::GLenum,
                                              width, height, array_size.unwrap() as gl::types::GLsizei,
                                              gl::TRUE);

            } else {
                unreachable!();
            }

        } else if texture_type == gl::TEXTURE_1D {
            let mut data_raw = data_raw;

            let width = match width as gl::types::GLsizei {
                0 => { data_raw = ptr::null(); 1 },
                a => a
            };

            if storage_internal_format.is_some() && (ctxt.version >= &Version(Api::Gl, 4, 2) || ctxt.extensions.gl_arb_texture_storage) {
                ctxt.gl.TexStorage1D(texture_type, texture_levels,
                                     storage_internal_format.unwrap() as gl::types::GLenum,
                                     width);

                if !data_raw.is_null() {
                    ctxt.gl.TexSubImage1D(texture_type, 0, 0, width, client_format,
                                          client_type, data_raw);
                }

            } else {
                ctxt.gl.TexImage1D(texture_type, 0, teximg_internal_format as i32, width,
                                   0, client_format as u32, client_type, data_raw);
            }

        } else {
            unreachable!();
        }

        // only generate mipmaps for color textures
        if generate_mipmaps {
            if ctxt.version >= &Version(Api::Gl, 3, 0) ||
               ctxt.version >= &Version(Api::GlEs, 2, 0)
            {
                ctxt.gl.GenerateMipmap(texture_type);
            } else if ctxt.extensions.gl_ext_framebuffer_object {
                ctxt.gl.GenerateMipmapEXT(texture_type);
            } else {
                unreachable!();
            }
        }

        id
    };

    Ok(TextureAny {
        context: facade.get_context().clone(),
        id: id,
        requested_format: format,
        actual_format: Cell::new(None),
        bind_point: texture_type,
        width: width,
        height: height,
        depth: depth,
        array_size: array_size,
        ty: stored_ty,
        levels: texture_levels as u32,
    })
}

impl<'a> TextureAnyMipmap<'a> {
    /// Returns the texture.
    pub fn get_texture(&self) -> &'a TextureAny {
        self.texture
    }

    /// Returns the level of the texture.
    pub fn get_level(&self) -> u32 {
        self.level
    }

    /// Returns the layer of the texture.
    pub fn get_layer(&self) -> u32 {
        self.layer
    }
}

/// Changes some parts of the texture.
pub fn upload_texture<'a, P>(tex: &TextureAny, x_offset: u32, y_offset: u32, z_offset: u32,
                             (format, data): (ClientFormat, Cow<'a, [P]>), width: u32,
                             height: Option<u32>, depth: Option<u32>, level: u32,
                             regen_mipmaps: bool)
                             where P: Send + Copy + Clone + 'a
{
    let id = tex.id;
    let bind_point = tex.bind_point;
    let regen_mipmaps = regen_mipmaps && tex.levels >= 2;

    assert!(x_offset <= tex.width);
    assert!(y_offset <= tex.height.unwrap_or(1));
    assert!(z_offset <= tex.depth.unwrap_or(1));
    assert!(x_offset + width <= tex.width);
    assert!(y_offset + height.unwrap_or(1) <= tex.height.unwrap_or(1));
    assert!(z_offset + depth.unwrap_or(1) <= tex.depth.unwrap_or(1));

    let (client_format, client_type) = image_format::client_format_to_glenum(&tex.context, format,
                                                                             tex.requested_format);

    let mut ctxt = tex.context.make_current();

    unsafe {
        if ctxt.state.pixel_store_unpack_alignment != 1 {
            ctxt.state.pixel_store_unpack_alignment = 1;
            ctxt.gl.PixelStorei(gl::UNPACK_ALIGNMENT, 1);
        }

        if ctxt.state.pixel_unpack_buffer_binding != 0 {
            ctxt.state.pixel_unpack_buffer_binding = 0;
            ctxt.gl.BindBuffer(gl::PIXEL_UNPACK_BUFFER, 0);
        }

        {
            ctxt.gl.BindTexture(bind_point, id);
            let act = ctxt.state.active_texture as usize;
            ctxt.state.texture_units[act].texture = id;
        }

        if bind_point == gl::TEXTURE_3D || bind_point == gl::TEXTURE_2D_ARRAY {
            unimplemented!();

        } else if bind_point == gl::TEXTURE_2D || bind_point == gl::TEXTURE_1D_ARRAY {
            assert!(z_offset == 0);
            ctxt.gl.TexSubImage2D(bind_point, level as gl::types::GLint,
                                  x_offset as gl::types::GLint,
                                  y_offset as gl::types::GLint,
                                  width as gl::types::GLsizei,
                                  height.unwrap_or(1) as gl::types::GLsizei,
                                  client_format, client_type,
                                  data.as_ptr() as *const libc::c_void);

        } else {
            assert!(z_offset == 0);
            assert!(y_offset == 0);

            unimplemented!();
        }

        // regenerate mipmaps if there are some
        if regen_mipmaps {
            if ctxt.version >= &Version(Api::Gl, 3, 0) {
                ctxt.gl.GenerateMipmap(bind_point);
            } else {
                ctxt.gl.GenerateMipmapEXT(bind_point);
            }
        }
    }
}

/// Returns the `Context` associated with this texture.
pub fn get_context(tex: &TextureAny) -> &Rc<Context> {
    &tex.context
}

/// Returns the bind point of this texture.
///
/// The returned GLenum is guaranteed to be supported by the context.
pub fn get_bind_point(tex: &TextureAny) -> gl::types::GLenum {
    tex.bind_point
}

impl TextureAny {
    /// UNSTABLE. Reads the content of a mipmap level of the texture.
    // TODO: this function only works for level 0 right now
    //       width/height need adjustements
    pub fn read<T>(&self, level: u32) -> T
                   where T: Texture2dDataSink<(u8, u8, u8, u8)>
            // TODO: remove Clone for P
    {
        assert_eq!(level, 0);   // TODO:

        let attachment = fbo::Attachment::TextureLayer {
            texture: self,
            layer: 0,
            level: 0,
        };

        let rect = Rect {
            bottom: 0,
            left: 0,
            width: self.width,
            height: self.height.unwrap_or(1),
        };

        let mut ctxt = self.context.make_current();

        let mut data = Vec::with_capacity(0);
        ops::read(&mut ctxt, ops::Source::Attachment(&attachment, &self.context.get_framebuffer_objects()), &rect, &mut data);
        T::from_raw(Cow::Owned(data), self.width, self.height.unwrap_or(1))
    }

    /// UNSTABLE. Reads the content of a mipmap level of the texture to a pixel buffer.
    // TODO: this function only works for level 0 right now
    //       width/height need adjustements
    pub fn read_to_pixel_buffer(&self, level: u32) -> PixelBuffer<(u8, u8, u8, u8)> {
        assert_eq!(level, 0);   // TODO:

        let size = self.width as usize * self.height.unwrap_or(1) as usize * 4;

        let attachment = fbo::Attachment::TextureLayer {
            texture: self,
            layer: 0,
            level: 0,
        };

        let rect = Rect {
            bottom: 0,
            left: 0,
            width: self.width,
            height: self.height.unwrap_or(1),
        };

        let pb = PixelBuffer::new_empty(&self.context, size);

        let mut ctxt = self.context.make_current();
        ops::read(&mut ctxt, ops::Source::Attachment(&attachment, &self.context.get_framebuffer_objects()), &rect, &pb);
        pb
    }

    /// UNSTABLE. Returns the `Context` associated with this texture.
    pub fn get_context(&self) -> &Rc<Context> {
        &self.context
    }

    /// Returns the width of the texture.
    pub fn get_width(&self) -> u32 {
        self.width
    }

    /// Returns the height of the texture.
    pub fn get_height(&self) -> Option<u32> {
        self.height.clone()
    }

    /// Returns the depth of the texture.
    pub fn get_depth(&self) -> Option<u32> {
        self.depth.clone()
    }

    /// Returns the array size of the texture.
    pub fn get_array_size(&self) -> Option<u32> {
        self.array_size.clone()
    }

    /// Returns the number of mipmap levels of the texture.
    pub fn get_mipmap_levels(&self) -> u32 {
        self.levels
    }

    /// Returns the type of the texture (1D, 2D, 3D, etc.).
    pub fn get_texture_type(&self) -> TextureType {
        self.ty
    }

    /// Determines the internal format of this texture.
    ///
    /// Returns `None` if the backend doesn't allow querying the actual format.
    pub fn get_internal_format_if_supported(&self) -> Option<InternalFormat> {
        if let Some(format) = self.actual_format.get() {
            format

        } else {
            let mut ctxt = self.context.make_current();
            let format = get_format::get_format_if_supported(&mut ctxt, self);
            self.actual_format.set(Some(format.clone()));
            format
        }
    }

    /// Returns a structure that represents a specific mipmap of the texture.
    ///
    /// Returns `None` if out of range.
    pub fn mipmap(&self, layer: u32, level: u32) -> Option<TextureAnyMipmap> {
        if layer >= self.array_size.unwrap_or(1) {
            return None;
        }

        if level >= self.levels {
            return None;
        }

        Some(TextureAnyMipmap {
            texture: self,
            layer: layer,
            level: level,
        })
    }
}

impl TextureExt for TextureAny {
    fn get_bind_point(&self) -> gl::types::GLenum {
        self.bind_point
    }
}

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

impl fmt::Debug for TextureAny {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        write!(fmt, "Texture #{} (dimensions: {}x{}x{}x{})", self.id,
               self.width, self.height.unwrap_or(1), self.depth.unwrap_or(1),
               self.array_size.unwrap_or(1))
    }
}

impl Drop for TextureAny {
    fn drop(&mut self) {
        let mut ctxt = self.context.make_current();

        // removing FBOs which contain this texture
        self.context.get_framebuffer_objects()
                    .purge_texture(self.id, &mut ctxt);

        // resetting the bindings
        for tex_unit in &mut ctxt.state.texture_units {
            if tex_unit.texture == self.id {
                tex_unit.texture = 0;
            }
        }

        unsafe { ctxt.gl.DeleteTextures(1, [ self.id ].as_ptr()); }
    }
}