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
use gleam::gl::types::GLenum;
use gleam::gl;
use GLContextAttributes;

/// This structure is here to allow
/// cross-platform formatting
pub struct GLFormats {
    pub color_renderbuffer: GLenum,
    pub texture_internal: GLenum,
    pub texture: GLenum,
    pub texture_type: GLenum,
    pub depth: GLenum,
    pub stencil: GLenum
}

impl GLFormats {
    // In the future we may use extension detection et-al to improve this, for now
    // platform dependent.
    //
    // FIXME: In linux with GLES2 texture attachments create INVALID_ENUM errors.
    // I suspect that it's because of texture formats, but I need time to debugit.
    #[cfg(not(target_os="android"))]
    pub fn detect(attrs: &GLContextAttributes) -> GLFormats {
        if attrs.alpha {
            GLFormats {
                color_renderbuffer: gl::RGBA8,
                texture_internal: gl::RGBA,
                texture: gl::RGBA,
                texture_type: gl::UNSIGNED_BYTE,
                depth: gl::DEPTH_COMPONENT24,
                stencil: gl::STENCIL_INDEX8,
            }
        } else {
            GLFormats {
                color_renderbuffer: gl::RGB8,
                texture_internal: gl::RGB8,
                texture: gl::RGB,
                texture_type: gl::UNSIGNED_BYTE,
                depth: gl::DEPTH_COMPONENT24,
                stencil: gl::STENCIL_INDEX8,
            }
        }
    }

    #[cfg(target_os="android")]
    pub fn detect(attrs: &GLContextAttributes) -> GLFormats {
        if attrs.alpha {
            GLFormats {
                color_renderbuffer: gl::RGBA4,
                texture_internal: gl::RGBA,
                texture: gl::RGBA,
                texture_type: gl::UNSIGNED_SHORT_4_4_4_4,
                depth: gl::DEPTH_COMPONENT16,
                stencil: gl::STENCIL_INDEX8,
            }
        } else {
            GLFormats {
                color_renderbuffer: gl::RGB565,
                texture_internal: gl::RGB,
                texture: gl::RGB,
                texture_type: gl::UNSIGNED_SHORT_4_4_4_4,
                depth: gl::DEPTH_COMPONENT16,
                stencil: gl::STENCIL_INDEX8,
            }
        }
    }
}