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
//! Contains all of the OpenGL state types related to shader programs.

use std::ptr;
use std::error;
use std::fmt;
use std::borrow::BorrowMut;
use std::marker::PhantomData;
use std::ffi::CString;
use gl;
use gl::types::*;
use types::{GLObject, GLError};
use context::{AContext, BaseContext, ContextOf};
use program::{Program, ProgramAttrib, ProgramUniform};
use shader::Shader;
use uniform_data::{UniformData, UniformDatumType, UniformPrimitiveType};

unsafe fn _get_program_iv(program: &Program,
                          pname: GLenum,
                          params: *mut GLint)
{
    gl::GetProgramiv(program.id(), pname, params);
    dbg_gl_sanity_check! {
        GLError::InvalidEnum => "`pname` is not an accepted value",
        GLError::InvalidValue => "`program` is not a value generated by OpenGL",
        GLError::InvalidOperation => "`program` does not refer to a program object",
        _ => "Unknown error"
    }
}

/// Provides a safe interface for creating program objects. A
/// `ProgramBuilder` can be created using the [`gl.build_program`]
/// (trait.ContextProgramBuilderExt.html#method.build_program) method.
pub struct ProgramBuilder<'a, C>
    where C: AContext + 'a
{
    gl: &'a C,
    shaders: &'a [Shader]
}

impl<'a, C> ProgramBuilder<'a, C>
    where C: AContext
{
    /// Create a new program builder.
    pub fn new(gl: &'a C, shaders: &'a [Shader])
        -> Self
    {
        ProgramBuilder { gl: gl, shaders: shaders }
    }

    /// Create and link the program object with the provided shaders, or
    /// return an error.
    ///
    /// # Failures
    /// An error will be returned if there was an error linking the program
    /// object.
    ///
    /// # Panics
    /// This function will panic if an OpenGL
    /// error was generated with debug assertions enabled.
    pub fn try_unwrap(self) -> Result<Program, GLError> {
        unsafe {
            let mut program = try! {
                self.gl.create_program().or_else(|_| {
                    let msg = "Error creating OpenGL program";
                    Err(GLError::Message(msg.to_owned()))
                })
            };

            for shader in self.shaders {
                self.gl.attach_shader(&mut program, shader);
            }

            try!(self.gl.link_program(&mut program));
            Ok(program)
        }
    }

    /// Create and link the program object with the provided shaders,
    /// or panic.
    ///
    /// # Panics
    /// This function will panic if there was an error linking
    /// the program object or if an OpenGL error was generated with debug
    /// assertions enabled.
    pub fn unwrap(self) -> Program {
        self.try_unwrap().unwrap()
    }
}

/// The extension trait that adds the `build_program` method.
pub trait ContextProgramBuilderExt: AContext + Sized {
    /// Create a new program builder, providing a safe interface
    /// for constructing a program object. See the [`ProgramBuilder`]
    /// (struct.ProgramBuilder.html) docs for more details.
    fn build_program<'a>(&'a self, shaders: &'a [Shader])
        -> ProgramBuilder<'a, Self>
    {
        ProgramBuilder::new(self, shaders)
    }
}

impl<C: AContext> ContextProgramBuilderExt for C {

}

/// An extension trait that includes program-related OpenGL methods.
pub trait ContextProgramExt: BaseContext {
    /// Create a new program object that has no shaders attached, or return
    /// an error if a shader object could not be created.
    ///
    /// # Safety
    /// Most OpenGL function calls assume a program object will have
    /// already had shaders attached and linked using [`gl.attach_shader`]
    /// (trait.ContextProgramExt.html#method.attach_shader) and
    /// [`gl.link_program`](trait.ContextProgramExt.html#method.link_program),
    /// respectively. Violating this invariant is considered undefined
    /// behavior in glitter.
    ///
    /// # See also
    /// [`glCreateProgram`](http://docs.gl/es2/glCreateProgram) OpenGL docs
    ///
    /// [`gl.build_program`](trait.ContextProgramBuilderExt.html#method.build_program):
    /// A safe wrapper for creating a program object.
    unsafe fn create_program(&self) -> Result<Program, ()> {
        let id = gl::CreateProgram();
        if id > 0 {
            Ok(Program::from_raw(id))
        }
        else {
            Err(())
        }
    }

    /// Attach a shader to a program object.
    ///
    /// # Panics
    /// This function will panic if the provided shader is already attached
    /// to the program object.
    ///
    /// # See also
    /// [`glAttachShader`](http://docs.gl/es2/glAttachShader) OpenGL docs
    fn attach_shader(&self, program: &mut Program, shader: &Shader) {
        unsafe {
            gl::AttachShader(program.id(), shader.id());
            dbg_gl_error! {
                GLError::InvalidValue => "One of either `program` or `shader` is not an OpenGL object",
                GLError::InvalidOperation => "`shader` is already attached to `program`, `shader` is not a shader object, or `program` is not a program object",
                _ => "Unknown error"
            }
        }
    }

    /// Link the program object, so that it can be used for rendering. Returns
    /// an error if the program could not be linked.
    ///
    /// # Failures
    /// If the `GL_LINK_STATUS` after linking the program was not `GL_TRUE`,
    /// then an error object containing the program's info log will
    /// be returned. Refer to the [`glLinkProgram`]
    /// (http://docs.gl/es2/glLinkProgram) OpenGL docs for the possible
    /// causes of failure.
    ///
    /// # Panics
    /// This function will panic if an OpenGL error is generated and debug
    /// assertions are enabled.
    ///
    /// # See also
    /// [`glLinkProgram`](http://docs.gl/es2/glLinkProgram) OpenGL docs
    fn link_program(&self, program: &mut Program) -> Result<(), GLError> {
        let success = unsafe {
            gl::LinkProgram(program.id());
            dbg_gl_error! {
                GLError::InvalidValue => "`program` is not a value from OpenGL",
                GLError::InvalidOperation => "`program` is not a program object",
                _ => "Unknown error"
            }

            let mut link_status : GLint = 0;
            _get_program_iv(program,
                            gl::LINK_STATUS,
                            &mut link_status as *mut GLint);

            link_status == gl::TRUE as GLint
        };

        if success {
            Ok(())
        }
        else {
            let msg = match self.get_program_info_log(&program) {
                Some(s) => { s },
                None => { String::from("[Unknown program error]") }
            };
            Err(GLError::Message(msg))
        }
    }

    /// Return the information log for the program object, if any is
    /// available.
    ///
    /// # Note
    /// If the info log returned by the OpenGL driver contained an invalid
    /// UTF-8 sequence, `None` will be returned.
    ///
    /// # See also
    /// [`glGetProgramInfoLog`](http://docs.gl/es2/glGetProgramInfoLog) OpenGL docs
    fn get_program_info_log(&self, program: &Program) -> Option<String> {
        unsafe {
            let mut info_length : GLint = 0;
            _get_program_iv(program,
                            gl::INFO_LOG_LENGTH,
                            &mut info_length as *mut GLint);

            if info_length > 0 {
                let mut bytes = Vec::<u8>::with_capacity(info_length as usize);

                gl::GetProgramInfoLog(program.id(),
                                      info_length,
                                      ptr::null_mut(),
                                      bytes.as_mut_ptr() as *mut GLchar);
                dbg_gl_sanity_check! {
                    GLError::InvalidValue => "`program` is not a value generated by OpenGL, or `maxLength` < 0",
                    GLError::InvalidOperation => "`program` is not a program object",
                    _ => "Unknown error"
                }
                bytes.set_len((info_length - 1) as usize);

                String::from_utf8(bytes).ok()
            }
            else {
                None
            }
        }
    }

    /// Retrieve a program attribute's index by name, or return an error
    /// if the attribute was not found in the program.
    ///
    /// # Panics
    /// This function will panic if an OpenGL error was generated and
    /// debug assertions are enabled.
    ///
    /// # See also
    /// [`glGetAttribLocation`](http://docs.gl/es2/glGetAttribLocation) OpenGL
    /// docs
    fn get_attrib_location<'a>(&self, program: &Program, name: &'a str)
        -> Result<ProgramAttrib, UnknownProgramAttrib<'a>>
    {
        let err = Err(UnknownProgramAttrib { name: name });

        let c_str = match CString::new(name) {
            Ok(s) => { s },
            Err(_) => { return err }
        };

        let str_ptr = c_str.as_ptr() as *const GLchar;
        unsafe {
            let index = gl::GetAttribLocation(program.id(), str_ptr);
            dbg_gl_error! {
                GLError::InvalidOperation => "`program` has not been linked, `program` is not a program object, or `program` is not a value generated by OpenGL",
                _ => "Unknown error"
            }

            if index >= 0 {
                Ok(ProgramAttrib { gl_index: index as GLuint })
            }
            else {
                err
            }
        }
    }

    /// Retrieve a program uniform's index by name, or return an error
    /// if the uniform was not found within the program.
    ///
    /// # Panics
    /// This function will panic if an OpenGL error was generated and
    /// debug assertions are enabled.
    ///
    /// # See also
    /// [`glGetUniformLocation`](http://docs.gl/es2/glGetUniformLocation)
    /// OpenGL docs
    fn get_uniform_location<'a>(&self, program: &Program, name: &'a str)
        -> Result<ProgramUniform, UnknownProgramUniform<'a>>
    {
        let err = Err(UnknownProgramUniform { name: name });

        let c_str = match CString::new(name) {
            Ok(s) => { s },
            Err(_) => { return err; }
        };

        let str_ptr = c_str.as_ptr() as *const GLchar;
        unsafe {
            let index = gl::GetUniformLocation(program.id(), str_ptr);
            dbg_gl_error! {
                GLError::InvalidValue => "`program` is not a value generated by OpenGL",
                GLError::InvalidOperation => "`program` is not a program object, or has not been successfully linked",
                _ => "Unknown error"
            }

            if index >= 0 {
                Ok(ProgramUniform { gl_index: index as GLuint })
            }
            else {
                err
            }
        }
    }

    /// Set the value of a uniform variable within the provided program
    /// object binding.
    ///
    /// - `_gl_program`: The program binding to change.
    /// - `uniform`: The location of the uniform variable. This value
    ///              can be retrieved using [`gl.get_uniform_location`]
    ///              (trait.ContextProgramExt.html#method.get_uniform_location)
    ///              method.
    /// - `val`: The value to set the uniform variable to. See the
    ///          [`UniformData`](../../uniform_data/trait.UniformData.html)
    ///          docs for more details about the types of uniform data.
    ///
    /// # Panics
    /// This function will panic if an OpenGL error is generated and
    /// debug assertions are enabled.
    ///
    /// # See also
    /// [`glUniform`](http://docs.gl/es2/glUniform) OpenGL docs
    fn set_uniform<T>(&self,
                      _gl_program: &ProgramBinding,
                      uniform: ProgramUniform,
                      val: T)
        where T: UniformData
    {
        let idx = uniform.gl_index as GLint;
        let count = val.uniform_elements() as GLsizei;
        let ptr = val.uniform_bytes().as_ptr();
        unsafe {
            match T::uniform_datum_type() {
                UniformDatumType::Vec1(p) => {
                    match p {
                        UniformPrimitiveType::Float => {
                            gl::Uniform1fv(idx, count, ptr as *const GLfloat);
                        },
                        UniformPrimitiveType::Int => {
                            gl::Uniform1iv(idx, count, ptr as *const GLint);
                        }
                    }
                },
                UniformDatumType::Vec2(p) => {
                    match p {
                        UniformPrimitiveType::Float => {
                            gl::Uniform2fv(idx, count, ptr as *const GLfloat);
                        },
                        UniformPrimitiveType::Int => {
                            gl::Uniform2iv(idx, count, ptr as *const GLint);
                        }
                    }
                },
                UniformDatumType::Vec3(p) => {
                    match p {
                        UniformPrimitiveType::Float => {
                            gl::Uniform3fv(idx, count, ptr as *const GLfloat);
                        },
                        UniformPrimitiveType::Int => {
                            gl::Uniform3iv(idx, count, ptr as *const GLint);
                        }
                    }
                },
                UniformDatumType::Vec4(p) => {
                    match p {
                        UniformPrimitiveType::Float => {
                            gl::Uniform4fv(idx, count, ptr as *const GLfloat);
                        },
                        UniformPrimitiveType::Int => {
                            gl::Uniform4iv(idx, count, ptr as *const GLint);
                        }
                    }
                },
                UniformDatumType::Matrix2x2 => {
                    gl::UniformMatrix2fv(idx,
                                         count,
                                         gl::FALSE,
                                         ptr as *const GLfloat);
                },
                UniformDatumType::Matrix3x3 => {
                    gl::UniformMatrix3fv(idx,
                                         count,
                                         gl::FALSE,
                                         ptr as *const GLfloat);
                },
                UniformDatumType::Matrix4x4 => {
                    gl::UniformMatrix4fv(idx,
                                         count,
                                         gl::FALSE,
                                         ptr as *const GLfloat);
                },
            }

            dbg_gl_error! {
                GLError::InvalidOperation => "Invalid uniform operation",
                GLError::InvalidValue => "`count` < 0 or `transpose` is not GL_FALSE",
                _ => "Unknown error"
            }
        }
    }
}

impl<C: BaseContext> ContextProgramExt for C {

}



/// An OpenGL context that has a free program binding.
pub trait ProgramContext: AContext {
    /// The type of binder this context contains.
    type Binder: BorrowMut<ProgramBinder>;

    /// The OpenGL context that will be returned after binding a program.
    type Rest: AContext;

    /// Split the context into a binder and the remaining context.
    fn split_program(self) -> (Self::Binder, Self::Rest);

    /// Bind a program to this context's program, returning a new
    /// context and a binding.
    fn use_program<'a>(self, program: &'a mut Program)
        -> (ProgramBinding<'a>, Self::Rest)
        where Self: Sized
    {
        let (mut binder, rest) = self.split_program();
        (binder.borrow_mut().bind(program), rest)
    }
}

impl<B, F, P, R, T> ProgramContext for ContextOf<B, F, P, R, T>
    where P: BorrowMut<ProgramBinder>
{
    type Binder = P;
    type Rest = ContextOf<B, F, (), R, T>;

    fn split_program(self) -> (Self::Binder, Self::Rest) {
        self.swap_program(())
    }
}

impl<'a, B, F, P, R, T> ProgramContext for &'a mut ContextOf<B, F, P, R, T>
    where &'a mut P: BorrowMut<ProgramBinder>
{
    type Binder = &'a mut P;
    type Rest = ContextOf<&'a mut B, &'a mut F, (), &'a mut R, &'a mut T>;

    fn split_program(self) -> (Self::Binder, Self::Rest) {
        let gl = self.borrowed_mut();
        gl.swap_program(())
    }
}



/// Represents a program that has been bound to the context.
pub struct ProgramBinding<'a> {
    _phantom_ref: PhantomData<&'a mut Program>,
    _phantom_ptr: PhantomData<*mut ()>
}

/// The OpenGL state representing the active program target.
pub struct ProgramBinder {
    _phantom: PhantomData<*mut ()>
}

impl ProgramBinder {
    /// Get the current program binder.
    pub unsafe fn current() -> Self {
        ProgramBinder {
            _phantom: PhantomData
        }
    }

    /// Bind a program to the context, returning a binding.
    pub fn bind<'a>(&mut self, program: &'a mut Program) -> ProgramBinding<'a>
    {
        let binding = ProgramBinding {
            _phantom_ref: PhantomData,
            _phantom_ptr: PhantomData
        };
        unsafe {
            gl::UseProgram(program.id());
            dbg_gl_error! {
                GLError::InvalidValue => "`program` is neither 0 nor an object generated by OpenGL",
                GLError::InvalidOperation => "`program` is not a program object or `program` could not be made part of the current state",
                _ => "Unknown error"
            }
        }
        binding
    }
}



/// An error that represents a program attribute that could not be found.
#[derive(Debug)]
pub struct UnknownProgramAttrib<'a> {
    name: &'a str
}

impl<'a> fmt::Display for UnknownProgramAttrib<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Unknown program attribute: {:?}", self.name)
    }
}

impl<'a> error::Error for UnknownProgramAttrib<'a> {
    fn description(&self) -> &str {
        "The desired program attribute was not found"
    }
}



/// An error that represents a program uniform that could not be found.
#[derive(Debug)]
pub struct UnknownProgramUniform<'a> {
    name: &'a str
}

impl<'a> fmt::Display for UnknownProgramUniform<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Unknown program uniform: {:?}", self.name)
    }
}

impl<'a> error::Error for UnknownProgramUniform<'a> {
    fn description(&self) -> &str {
        "The desired program uniform was not found"
    }
}