Files
ab_glyph_rasterizer
adler
adler32
andrew
bitflags
bytemuck
byteorder
calloop
cfg_if
color_quant
crc32fast
crossbeam_channel
crossbeam_deque
crossbeam_epoch
crossbeam_utils
deflate
dlib
downcast_rs
draw_state
either
event_loop
float
fnv
gfx
gfx_core
gfx_device_gl
gfx_gl
gfx_graphics
gfx_texture
gif
gl
glutin
glutin_egl_sys
glutin_glx_sys
glutin_window
graphics
graphics_api_version
image
input
instant
interpolation
iovec
jpeg_decoder
lazy_static
lazycell
libc
libloading
lock_api
log
maybe_uninit
memchr
memmap2
memoffset
miniz_oxide
mio
mio_extras
net2
nix
nom
num_cpus
num_integer
num_iter
num_rational
num_traits
once_cell
osmesa_sys
owned_ttf_parser
parking_lot
parking_lot_core
percent_encoding
piston
piston_window
png
proc_macro2
quote
raw_window_handle
rayon
rayon_core
read_color
rusttype
same_file
scoped_threadpool
scoped_tls
scopeguard
serde
serde_derive
shader_version
shaders_graphics2d
colored
textured
textured_color
shared_library
slab
smallvec
smithay_client_toolkit
spin_sleep
syn
texture
tiff
ttf_parser
unicode_xid
vecmath
viewport
walkdir
wayland_client
wayland_commons
wayland_cursor
wayland_egl
wayland_protocols
wayland_sys
weezl
window
winit
x11_dl
xcursor
xdg
xml
  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
//! Models versions of OpenGL Shader Language (GLSL)

use { OpenGL, PickShader, Shaders };
use std::str::FromStr;
use std::fmt;
use std::error::Error;

/// For OpenGL version 3.3 and above,
/// the GLSL version is the same as the OpenGL version.
///
/// Source: http://www.opengl.org/wiki/Core_Language_%28GLSL%29
#[allow(missing_docs)]
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
pub enum GLSL {
    V1_10,
    V1_20,
    V1_30,
    V1_40,
    V1_50,
    V3_30,
    V4_00,
    V4_10,
    V4_20,
    V4_30,
    V4_40,
    V4_50
}

impl GLSL {
    /// Gets OpenGL version associated with GLSL.
    #[allow(non_snake_case)]
    pub fn to_opengl(&self) -> OpenGL {
        match *self {
            GLSL::V1_10 => OpenGL::V2_0,
            GLSL::V1_20 => OpenGL::V2_1,
            GLSL::V1_30 => OpenGL::V3_0,
            GLSL::V1_40 => OpenGL::V3_1,
            GLSL::V1_50 => OpenGL::V3_2,
            GLSL::V3_30 => OpenGL::V3_3,
            GLSL::V4_00 => OpenGL::V4_0,
            GLSL::V4_10 => OpenGL::V4_1,
            GLSL::V4_20 => OpenGL::V4_2,
            GLSL::V4_30 => OpenGL::V4_3,
            GLSL::V4_40 => OpenGL::V4_4,
            GLSL::V4_50 => OpenGL::V4_5
        }
    }
}

impl PickShader for GLSL {
    fn pick_shader<'a, S: ?Sized>(self, shaders: &Shaders<'a, Self, S>) -> Option<&'a S> {
        // OpenGL since 3.2 in core profile doesn't support GLSL lower than 1.50.
        // Since there are no compatible shader in this case, it will return `None`.
        let low = if self < GLSL::V1_50 {
            GLSL::V1_10
        } else {
            GLSL::V1_50
        };
        shaders.0.iter()
               .skip_while(|&(v, _)| *v < low)
               .take_while(|&(v, _)| *v <= self)
               .last().map(|(_, &s)| s)
    }
}


impl FromStr for GLSL {
    type Err = ParseGLSLError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "1.10" => Ok(GLSL::V1_10),
            "1.20" => Ok(GLSL::V1_20),
            "1.30" => Ok(GLSL::V1_30),
            "1.40" => Ok(GLSL::V1_40),
            "1.50" => Ok(GLSL::V1_50),
            "3.30" => Ok(GLSL::V3_30),
            "4.00" => Ok(GLSL::V4_00),
            "4.10" => Ok(GLSL::V4_10),
            "4.20" => Ok(GLSL::V4_20),
            "4.30" => Ok(GLSL::V4_30),
            "4.40" => Ok(GLSL::V4_40),
            "4.50" => Ok(GLSL::V4_50),
            error => Err(ParseGLSLError{input: error.into()}),
        }
    }
}


/// Represents an error while trying to get `GLSL` from `&str`.
#[derive(Debug)]
pub struct ParseGLSLError{
    input: String
}

impl fmt::Display for ParseGLSLError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "`{}` is not a valid GLSL version", self.input)
    }
}

impl Error for ParseGLSLError {
    fn description(&self) -> &str {
        "Invalid GLSL version"
    }
}