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
use std::marker::PhantomData;
use std::ffi::CString;
use std::mem;
use std::ptr;
use std::str;
use std::iter::repeat;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use gl;
use gl::types::*;
use resource::{GLPrimitive, GPUVec};

#[path = "../error.rs"]
mod error;

/// Structure encapsulating a shader program.
pub struct Shader {
    program: GLuint,
    vshader: GLuint,
    fshader: GLuint
}

impl Shader {
    /// Creates a new shader program from two files containing the vertex and fragment shader.
    pub fn new(vshader_path: &Path, fshader_path: &Path) -> Option<Shader> {
        let mut vshader = String::new();
        let mut fshader = String::new();

        if File::open(vshader_path).map(|mut v| v.read_to_string(&mut vshader)).is_err() {
            return None;
        }

        if File::open(fshader_path).map(|mut f| f.read_to_string(&mut fshader)).is_err() {
            return None;
        }

        Some(Shader::new_from_str(&vshader[..], &fshader[..]))
    }

    /// Creates a new shader program from strings of the vertex and fragment shader.
    pub fn new_from_str(vshader: &str, fshader: &str) -> Shader {
        let (program, vshader, fshader) = load_shader_program(vshader, fshader);

        Shader {
            program: program,
            vshader: vshader,
            fshader: fshader
        }
    }

    /// Gets a uniform variable from the shader program.
    pub fn get_uniform<T: GLPrimitive>(&self, name: &str) -> Option<ShaderUniform<T>> {
        let c_str = CString::new(name.as_bytes()).unwrap();
        let location = unsafe { gl::GetUniformLocation(self.program, c_str.as_ptr()) };

        if unsafe { gl::GetError() } == 0 && location != -1 {
            Some(ShaderUniform { id: location as GLuint, data_type: PhantomData })
        }
        else {
            None
        }
    }

    /// Gets an attribute from the shader program.
    pub fn get_attrib<T: GLPrimitive>(&self, name: &str) -> Option<ShaderAttribute<T>> {
        let c_str = CString::new(name.as_bytes()).unwrap();
        let location = unsafe { gl::GetAttribLocation(self.program, c_str.as_ptr()) };

        if unsafe { gl::GetError() } == 0 && location != -1 {
            Some(ShaderAttribute { id: location as GLuint, data_type: PhantomData })
        }
        else {
            None
        }
    }

    /// Make this program active.
    pub fn use_program(&mut self) {
        verify!(gl::UseProgram(self.program));
    }
}

impl Drop for Shader {
    fn drop(&mut self) {
        verify!(gl::DeleteProgram(self.program));
        verify!(gl::DeleteShader(self.fshader));
        verify!(gl::DeleteShader(self.vshader));
    }
}

/// Structure encapsulating an uniform variable.
pub struct ShaderUniform<T> {
    id:        GLuint,
    data_type: PhantomData<T>
}

impl<T: GLPrimitive> ShaderUniform<T> {
    /// Upload a value to this variable.
    pub fn upload(&mut self, value: &T) {
        value.upload(self.id)
    }
}

/// Structure encapsulating an attribute.
pub struct ShaderAttribute<T> {
    id:        GLuint,
    data_type: PhantomData<T>
}

impl<T: GLPrimitive> ShaderAttribute<T> {
    /// Disable this attribute.
    pub fn disable(&mut self) {
        verify!(gl::DisableVertexAttribArray(self.id));
    }

    /// Enable this attribute.
    pub fn enable(&mut self) {
        verify!(gl::EnableVertexAttribArray(self.id));
    }

    /// Binds this attribute to a gpu vector.
    pub fn bind(&mut self, vector: &mut GPUVec<T>) {
        vector.bind();

        unsafe {
            verify!(gl::VertexAttribPointer(
                        self.id,
                        GLPrimitive::size(None::<T>) as i32,
                        GLPrimitive::gl_type(None::<T>),
                        gl::FALSE,
                        0,
                        ptr::null()));
        }
    }

    /// Binds this attribute to non contiguous parts of a gpu vector.
    pub fn bind_sub_buffer(&mut self, vector: &mut GPUVec<T>, strides: usize, start_index: usize) {
        vector.bind();

        unsafe {
            verify!(gl::VertexAttribPointer(
                        self.id,
                        GLPrimitive::size(None::<T>) as i32,
                        GLPrimitive::gl_type(None::<T>),
                        gl::FALSE,
                        ((strides + 1) * mem::size_of::<T>()) as GLint,
                        mem::transmute(start_index * mem::size_of::<T>())));
        }
    }
}

/// Loads a shader program using the given source codes for the vertex and fragment shader.
///
/// Fails after displaying opengl compilation errors if the shaders are invalid.
fn load_shader_program(vertex_shader: &str, fragment_shader: &str) -> (GLuint, GLuint, GLuint) {
    // Create and compile the vertex shader
    let vshader = verify!(gl::CreateShader(gl::VERTEX_SHADER));
    let vertex_shader = CString::new(vertex_shader.as_bytes()).unwrap();
    let fragment_shader = CString::new(fragment_shader.as_bytes()).unwrap();

    unsafe {
        verify!(gl::ShaderSource(vshader, 1, &vertex_shader.as_ptr(), ptr::null()));
        verify!(gl::CompileShader(vshader));
    }
    check_shader_error(vshader);

    // Create and compile the fragment shader
    let fshader = verify!(gl::CreateShader(gl::FRAGMENT_SHADER));
    unsafe {
        verify!(gl::ShaderSource(fshader, 1, &fragment_shader.as_ptr(), ptr::null()));
        verify!(gl::CompileShader(fshader));
    }

    check_shader_error(fshader);

    // Link the vertex and fragment shader into a shader program
    let program = verify!(gl::CreateProgram());
    verify!(gl::AttachShader(program, vshader));
    verify!(gl::AttachShader(program, fshader));
    verify!(gl::LinkProgram(program));

    (program, vshader, fshader)
}

/// Checks if a shader handle is valid.
///
/// If it is not valid, it fails with a descriptive error message.
fn check_shader_error(shader: GLuint) {
    let mut compiles: i32 = 0;

    unsafe{
        gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut compiles);

        if compiles == 0 {
            println!("Shader compilation failed.");
            let mut info_log_len = 0;

            gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut info_log_len);

            if info_log_len > 0 {
                // error check for fail to allocate memory omitted
                let mut chars_written = 0;
                let info_log: String = repeat(' ').take(info_log_len as usize).collect();

                let c_str = CString::new(info_log.as_bytes()).unwrap();
                gl::GetShaderInfoLog(shader, info_log_len, &mut chars_written, c_str.as_ptr() as *mut _);

                let bytes = c_str.as_bytes();
                let bytes = &bytes[.. bytes.len() - 1];
                panic!("Shader compilation failed: {}", str::from_utf8(bytes).unwrap());
            }
        }
    }
}