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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! Shader programs related types and functions.
//!
//! A shader `Program` is an object representing several operations. It’s a streaming program that
//! will operate on vertices, vertex patches, primitives and/or fragments.
//!
//! > *Note: shader programs don’t have to run on all those objects; they can be ran only on
//! vertices and fragments, for instance*.
//!
//! Creating a shader program is very simple. You need shader `Stage`s representing each step of the
//! processing. Here’s the actual mapping between the shader stage types and the processing unit:
//!
//! - `Stage<TessellationControlShader>`: ran on **tessellation parameters** ;
//! - `Stage<TessellationEvaluationShader>`: ran on **patches** ;
//! - `Stage<VertexShader>`: ran on **vertices** ;
//! - `Stage<GeometryShader>`: ran on **primitives** ;
//! - `Stage<FragmentShader>`: ran on **screen fragments**.
//!
//! You *have* to provide at least a `Stage<VertexShader>` and a `Stage<FragmentShader>`. If you
//! want tessellation processing, you need to provide both a `Stage<TessellationControlShader>` and
//! a `Stage<TessellationEvaluationShader>`. If you want primitives processing, you need to add a
//! `Stage<GeometryShader>`.
//!
//! In order to customize the behavior of your shader programs, you have access to *uniforms*. For
//! more details about them, see the documentation for the type `Uniform` and trait `Uniformable`.
//! When creating a new shader program, you have to provide code to declare its *uniform interface*.
//! The *uniform interface* refers to a type of your own that will be kept by the shader program and
//! exposed to you when you’ll express the need to update its uniforms. That *uniform interface* is
//! created via a closure you pass. That closure takes as arguments a `ProgramProxy` used to
//! retrieve `Uniform`s from the program being constructed. That pattern, that can be a bit
//! overwhelming at first, is important to keep things safe and functional. Keep in mind that you
//! can make the closure fail, so that you can notify a `Uniform` lookup failure, for instance.
//!
//! You can create a `Program` with its `new` associated function.
//!
//! # Example
//!
//! ```
//! // assume we have a vertex shader `vs` and fragment shader `fs`
//! let program = Program::new(None, &vs, None, &fs, |get_uni| {
//!   let resolution: Uniform<[f32; 2]> = try!(get_uni("resolution"));
//!   let time: Uniform<f32> = try!(get_uni("time"));
//!
//!   Ok((resolution, time))
//! });
//! ```

use std::marker::PhantomData;

use buffer::Binding;
use linear::{M22, M33, M44};
use shader::stage::{FragmentShader, GeometryShader, HasStage, Stage, TessellationControlShader,
                    TessellationEvaluationShader, VertexShader};
use texture::Unit;

/// Trait to implement to provide shader program features.
pub trait HasProgram: HasStage + HasUniform {
  type Program;

  /// Create a new program by linking it with stages.
  fn new_program(tess: Option<(&Self::AStage, &Self::AStage)>, vertex: &Self::AStage, geometry: Option<&Self::AStage>, fragment: &Self::AStage) -> Result<Self::Program, ProgramError>;
  /// Free a program.
  fn free_program(program: &mut Self::Program);
  /// Map a uniform name to its uniform representation. Can fail with `ProgramError`.
  fn map_uniform(program: &Self::Program, name: &str, ty: Type, dim: Dim) -> (Self::U, Option<UniformWarning>);
  /// Bulk update of uniforms. The update closure should contain the code used to update as many
  /// uniforms as wished.
  fn update_uniforms<F>(program: &Self::Program, f: F) where F: Fn();
}

/// A shader program with *uniform interface*.
#[derive(Debug)]
pub struct Program<C, T> where C: HasProgram {
  pub repr: C::Program,
  pub uniform_interface: T
}

impl<C, T> Drop for Program<C, T> where C: HasProgram {
  fn drop(&mut self) {
    C::free_program(&mut self.repr)
  }
}

impl<C, T> Program<C, T> where C: HasProgram {
  /// Create a new `Program` by linking it with shader stages and by providing a function to build
  /// its *uniform interface*, which the `Program` will hold for you.
  ///
  /// The *uniform interface* is any type you want. The idea is to bake `Uniform<_>` in your type so
  /// that you can access them later. To do so, you’re given an object of type `ProgramProxy`, which
  /// has a function `uniform`. That function can be used to lookup uniforms so that you can build
  /// your *uniform interface*.
  ///
  /// Use the `update` function to access the *uniform interface* back.
  pub fn new<GetUni>(tess: Option<(&Stage<C, TessellationControlShader>, &Stage<C, TessellationEvaluationShader>)>, vertex: &Stage<C, VertexShader>, geometry: Option<&Stage<C, GeometryShader>>, fragment: &Stage<C, FragmentShader>, get_uni: GetUni) -> Result<Self, ProgramError>
      where GetUni: Fn(ProgramProxy<C>) -> Result<T, UniformWarning> {
    let repr = try!(C::new_program(tess.map(|(tcs, tes)| (&tcs.repr, &tes.repr)), &vertex.repr, geometry.map(|g| &g.repr), &fragment.repr));
    let uniform_interface = try!(get_uni(ProgramProxy::new(&repr)).map_err(ProgramError::UniformWarning));

    Ok(Program {
      repr: repr,
      uniform_interface: uniform_interface
    })
  }

  pub fn update<F>(&self, f: F) where F: Fn(&T) {
    C::update_uniforms(&self.repr, || { f(&self.uniform_interface) })
  }
}

/// `Program` proxy used to map uniforms. When building a `Program`, as the `Program` doesn’t exist
/// yet, a `ProgramProxy` is passed to act as it was the `Program`.
#[derive(Debug)]
pub struct ProgramProxy<'a, C> where C: 'a + HasProgram {
  repr: &'a C::Program
}

impl<'a, C> ProgramProxy<'a, C> where C: HasProgram {
  fn new(program: &'a C::Program) -> Self {
    ProgramProxy {
      repr: program
    }
  }

  /// Map a uniform name to a uniformable value.
  pub fn uniform<T>(&self, name: &str) -> (Uniform<C, T>, Option<UniformWarning>) where T: Uniformable<C> {
    // gather information about the uniform so that backend can proceed with runtime reification and
    // validation
    let ty = T::reify_type();
    let dim = T::dim();

    let (u, w) = C::map_uniform(self.repr, name, ty, dim);
    (Uniform::new(u), w)
  }
}

#[derive(Clone, Debug)]
pub enum ProgramError {
  LinkFailed(String),
  UniformWarning(UniformWarning)
}

#[derive(Clone, Debug)]
pub enum UniformWarning {
  Inactive(String),
  TypeMismatch(String)
}

// ---------------------------------------
// -- Uniforms ---------------------------

pub trait HasUniform {
  /// Uniform representation.
  type U;

  // integral
  fn update1_i32(uniform: &Self::U, x: i32);
  fn update2_i32(uniform: &Self::U, xy: [i32; 2]);
  fn update3_i32(uniform: &Self::U, xyz: [i32; 3]);
  fn update4_i32(uniform: &Self::U, xyzw: [i32; 4]);
  fn update1_slice_i32(uniform: &Self::U, x: &[i32]);
  fn update2_slice_i32(uniform: &Self::U, xy: &[[i32; 2]]);
  fn update3_slice_i32(uniform: &Self::U, xyz: &[[i32; 3]]);
  fn update4_slice_i32(uniform: &Self::U, xyzw: &[[i32; 4]]);
  // unsigned
  fn update1_u32(uniform: &Self::U, x: u32);
  fn update2_u32(uniform: &Self::U, xy: [u32; 2]);
  fn update3_u32(uniform: &Self::U, xyz: [u32; 3]);
  fn update4_u32(uniform: &Self::U, xyzw: [u32; 4]);
  fn update1_slice_u32(uniform: &Self::U, x: &[u32]);
  fn update2_slice_u32(uniform: &Self::U, xy: &[[u32; 2]]);
  fn update3_slice_u32(uniform: &Self::U, xyz: &[[u32; 3]]);
  fn update4_slice_u32(uniform: &Self::U, xyzw: &[[u32; 4]]);
  // floating
  fn update1_f32(uniform: &Self::U, x: f32);
  fn update2_f32(uniform: &Self::U, xy: [f32; 2]);
  fn update3_f32(uniform: &Self::U, xyz: [f32; 3]);
  fn update4_f32(uniform: &Self::U, xyzw: [f32; 4]);
  fn update1_slice_f32(uniform: &Self::U, x: &[f32]);
  fn update2_slice_f32(uniform: &Self::U, xy: &[[f32; 2]]);
  fn update3_slice_f32(uniform: &Self::U, xyz: &[[f32; 3]]);
  fn update4_slice_f32(uniform: &Self::U, xyzw: &[[f32; 4]]);
  fn update22_f32(uniform: &Self::U, x: M22);
  fn update33_f32(uniform: &Self::U, x: M33);
  fn update44_f32(uniform: &Self::U, x: M44);
  fn update22_slice_f32(uniform: &Self::U, x: &[M22]);
  fn update33_slice_f32(uniform: &Self::U, x: &[M33]);
  fn update44_slice_f32(uniform: &Self::U, x: &[M44]);
  // boolean
  fn update1_bool(uniform: &Self::U, x: bool);
  fn update2_bool(uniform: &Self::U, xy: [bool; 2]);
  fn update3_bool(uniform: &Self::U, xyz: [bool; 3]);
  fn update4_bool(uniform: &Self::U, xyzw: [bool; 4]);
  fn update1_slice_bool(uniform: &Self::U, x: &[bool]);
  fn update2_slice_bool(uniform: &Self::U, xy: &[[bool; 2]]);
  fn update3_slice_bool(uniform: &Self::U, xyz: &[[bool; 3]]);
  fn update4_slice_bool(uniform: &Self::U, xyzw: &[[bool; 4]]);
  // textures
  fn update_texture_unit(uniform: &Self::U, unit: u32);
  // uniform buffers
  fn update_buffer_binding(uniform_block: &Self::U, binding: u32);
}

/// A shader uniform. `Uniform<C, T>` doesn’t hold any value. It’s more like a mapping between the
/// host code and the shader the uniform was retrieved from.
#[derive(Debug)]
pub struct Uniform<C, T> where C: HasUniform, T: Uniformable<C> {
  pub repr: C::U,
  _t: PhantomData<*const T>
}

impl<C, T> Uniform<C, T> where C: HasUniform, T: Uniformable<C> {
  pub fn new(repr: C::U) -> Uniform<C, T> {
    Uniform {
      repr: repr,
      _t: PhantomData
    }
  }

  pub fn update(&self, x: T) {
    T::update(self, x);
  }
}

/// Wrapper over `Uniform`, discarding everything but update.
///
/// Among its features, this type enables you to `contramap` a function to build more interesting
/// `UniformUpdate`.
///
/// Use `From` or `Into` to build a `UniformUpdate`.
pub struct UniformUpdate<'a, T> {
  update_closure: Box<Fn(T) + 'a>
}

impl<'a, C, T> From<Uniform<C, T>> for UniformUpdate<'a, T> where C: 'a + HasUniform, T: 'a + Uniformable<C> {
  fn from(u: Uniform<C, T>) -> Self {
    UniformUpdate {
      update_closure: Box::new(move |x| {
        u.update(x);
      })
    }
  }
}

impl<'a, T> UniformUpdate<'a, T> where T: 'a {
  /// Update the underlying `Uniform`.
  pub fn update(&self, x: T) {
    (self.update_closure)(x)
  }

  /// Apply a contravariant functor.
  pub fn contramap<F, Q>(self, f: F) -> UniformUpdate<'a, Q> where F: 'a + Fn(Q) -> T {
    UniformUpdate {
      update_closure: Box::new(move |x| {
        (self.update_closure)(f(x))
      })
    }
  }
}

/// Type of a uniform.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Type {
  Integral,
  Unsigned,
  Floating,
  Boolean,
  TextureUnit,
  BufferBinding
}

/// Dimension of the uniform.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Dim {
  Dim1,
  Dim2,
  Dim3,
  Dim4,
  Dim22,
  Dim33,
  Dim44,
  Cubemap
}

/// Types that can behave as `Uniform`.
pub trait Uniformable<C>: Sized where C: HasUniform {
  /// Update the uniform with a new value.
  fn update(u: &Uniform<C, Self>, x: Self);
  /// Retrieve the `Type` of the uniform.
  fn reify_type() -> Type;
  /// Retrieve the `Dim` of the uniform.
  fn dim() -> Dim;
}

impl<C> Uniformable<C> for i32 where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update1_i32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Integral }

  fn dim() -> Dim { Dim::Dim1 }
}

impl<C> Uniformable<C> for [i32; 2] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update2_i32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Integral }

  fn dim() -> Dim { Dim::Dim2 }
}

impl<C> Uniformable<C> for [i32; 3] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update3_i32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Integral }

  fn dim() -> Dim { Dim::Dim3 }
}

impl<C> Uniformable<C> for [i32; 4] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update4_i32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Integral }

  fn dim() -> Dim { Dim::Dim4 }
}

impl<'a, C> Uniformable<C> for &'a [i32] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update1_slice_i32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Integral }

  fn dim() -> Dim { Dim::Dim1 }
}

impl<'a, C> Uniformable<C> for &'a [[i32; 2]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update2_slice_i32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Integral }

  fn dim() -> Dim { Dim::Dim2 }
}

impl<'a, C> Uniformable<C> for &'a [[i32; 3]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update3_slice_i32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Integral }

  fn dim() -> Dim { Dim::Dim3 }
}

impl<'a, C> Uniformable<C> for &'a [[i32; 4]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update4_slice_i32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Integral }

  fn dim() -> Dim { Dim::Dim4 }
}

impl<C> Uniformable<C> for u32 where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update1_u32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Unsigned }

  fn dim() -> Dim { Dim::Dim1 }
}

impl<C> Uniformable<C> for [u32; 2] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update2_u32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Unsigned }

  fn dim() -> Dim { Dim::Dim2 }
}

impl<C> Uniformable<C> for [u32; 3] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update3_u32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Unsigned }

  fn dim() -> Dim { Dim::Dim3 }
}

impl<C> Uniformable<C> for [u32; 4] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update4_u32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Unsigned }

  fn dim() -> Dim { Dim::Dim4 }
}

impl<'a, C> Uniformable<C> for &'a [u32] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update1_slice_u32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Unsigned }

  fn dim() -> Dim { Dim::Dim1 }
}

impl<'a, C> Uniformable<C> for &'a [[u32; 2]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update2_slice_u32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Unsigned }

  fn dim() -> Dim { Dim::Dim2 }
}

impl<'a, C> Uniformable<C> for &'a [[u32; 3]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update3_slice_u32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Unsigned }

  fn dim() -> Dim { Dim::Dim3 }
}

impl<'a, C> Uniformable<C> for &'a [[u32; 4]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update4_slice_u32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Unsigned }

  fn dim() -> Dim { Dim::Dim4 }
}

impl<C> Uniformable<C> for f32 where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update1_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim1 }
}

impl<C> Uniformable<C> for [f32; 2] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update2_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim2 }
}

impl<C> Uniformable<C> for [f32; 3] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update3_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim3 }
}

impl<C> Uniformable<C> for [f32; 4] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update4_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim4 }
}

impl<'a, C> Uniformable<C> for &'a [f32] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update1_slice_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim1 }
}

impl<'a, C> Uniformable<C> for &'a [[f32; 2]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update2_slice_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim2 }
}

impl<'a, C> Uniformable<C> for &'a [[f32; 3]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update3_slice_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim3 }
}

impl<'a, C> Uniformable<C> for &'a [[f32; 4]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update4_slice_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim4 }
}

impl<C> Uniformable<C> for M22 where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update22_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim22 }
}

impl<C> Uniformable<C> for M33 where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update33_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim33 }
}

impl<C> Uniformable<C> for M44 where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update44_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim44 }
}

impl<'a, C> Uniformable<C> for &'a [M22] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update22_slice_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim22 }
}

impl<'a, C> Uniformable<C> for &'a [M33] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update33_slice_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim33 }
}

impl<'a, C> Uniformable<C> for &'a [M44] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update44_slice_f32(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Floating }

  fn dim() -> Dim { Dim::Dim44 }
}

impl<C> Uniformable<C> for bool where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update1_bool(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Boolean }

  fn dim() -> Dim { Dim::Dim1 }
}

impl<C> Uniformable<C> for [bool; 2] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update2_bool(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Boolean }

  fn dim() -> Dim { Dim::Dim2 }
}

impl<C> Uniformable<C> for [bool; 3] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update3_bool(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Boolean }

  fn dim() -> Dim { Dim::Dim3 }
}

impl<C> Uniformable<C> for [bool; 4] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update4_bool(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Boolean }

  fn dim() -> Dim { Dim::Dim4 }
}

impl<'a, C> Uniformable<C> for &'a [bool] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update1_slice_bool(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Boolean }

  fn dim() -> Dim { Dim::Dim1 }
}

impl<'a, C> Uniformable<C> for &'a [[bool; 2]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update2_slice_bool(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Boolean }

  fn dim() -> Dim { Dim::Dim2 }
}

impl<'a, C> Uniformable<C> for &'a [[bool; 3]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update3_slice_bool(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Boolean }

  fn dim() -> Dim { Dim::Dim3 }
}

impl<'a, C> Uniformable<C> for &'a [[bool; 4]] where C: HasUniform {
  fn update(u: &Uniform<C, Self>, x: Self) {
    C::update4_slice_bool(&u.repr, x)
  }

  fn reify_type() -> Type { Type::Boolean }

  fn dim() -> Dim { Dim::Dim4 }
}

impl<C> Uniformable<C> for Unit where C: HasUniform {
  fn update(u: &Uniform<C, Self>, unit: Self) {
    C::update_texture_unit(&u.repr, *unit);
  }

  fn reify_type() -> Type { Type::TextureUnit }

  fn dim() -> Dim { Dim::Dim1 }
}

impl<C> Uniformable<C> for Binding where C: HasUniform {
  fn update(u: &Uniform<C, Self>, binding: Self) {
    C::update_buffer_binding(&u.repr, *binding);
  }

  fn reify_type() -> Type { Type::BufferBinding }

  fn dim() -> Dim { Dim::Dim1 }
}