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
use std::cell::Cell;
use web_sys::{WebGl2RenderingContext, WebGlProgram, WebGlUniformLocation};

/// WebGL2 uniform.
///
/// This associates an identifier for a WebGL2 uniform with a data value that
/// can be accessed and modified using inner mutability.
///
/// Usually, the type `T` would be [`Copy`].
pub struct Uniform<T> {
    name: String,
    data: Cell<T>,
}

impl<T> Uniform<T> {
    /// Creates a new WebGL2 uniform.
    ///
    /// The `name` corresponds to the identifier of the uniform, and `data`
    /// gives its initial value.
    pub fn new(name: String, data: T) -> Uniform<T> {
        Uniform {
            name,
            data: Cell::new(data),
        }
    }

    /// Returns the name (identifier) of the uniform.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Modifies the data of the uniform.
    ///
    /// This function sets the value of the uniform to `value`.
    pub fn set_data(&self, value: T) {
        self.data.set(value)
    }
}

impl<T: Copy> Uniform<T> {
    /// Returns the data of the uniform.
    ///
    /// This function returns a copy of the value of the uniform.
    pub fn get_data(&self) -> T {
        self.data.get()
    }
}

/// Trait that abstracts WebGL2 uniforms.
///
/// This trait is implemented by objects that represent a WebGL2 uniform and its
/// value, and which know how to set the value of the uniform if given a WebGL2
/// program with such uniform.
pub trait UniformValue {
    /// Set the value of the uniform.
    ///
    /// If the `program` contains the uniform represented by `self`, this
    /// function sets the value of the uniform to the value stored by `self`. If
    /// the program does not contain the uniform, this function does nothing.
    fn set_uniform(&self, gl: &WebGl2RenderingContext, program: &WebGlProgram);
}

impl<T: UniformType + Copy> UniformValue for Uniform<T> {
    fn set_uniform(&self, gl: &WebGl2RenderingContext, program: &WebGlProgram) {
        if let Some(location) = gl.get_uniform_location(program, self.name()) {
            self.get_data().uniform(gl, Some(&location))
        }
    }
}

/// Trait that links native Rust types with WebGL2 uniform types.
pub trait UniformType {
    /// Sets the value of the uniform.
    ///
    /// This function sets the value of the WebGL2 uniform in `location` to the
    /// value of `self` using one of the `uniform{1,2,3,4}{f,i,ui}` WebGL2
    /// functions as appropriate.
    fn uniform(&self, gl: &WebGl2RenderingContext, location: Option<&WebGlUniformLocation>);
}

macro_rules! impl_uniform {
    ($t:ty, $fun:ident, $sel:ident, $($things:expr),+) => {
        #[doc = concat!("Uniform type corresponding to `", stringify!($fun), "`.")]
	impl UniformType for $t {
	    fn uniform(&$sel, gl: &WebGl2RenderingContext, location: Option<&WebGlUniformLocation>) {
		gl.$fun(location, $($things,)+)
	    }
	}
    }
}

impl_uniform!(f32, uniform1f, self, *self);
impl_uniform!(i32, uniform1i, self, *self);
impl_uniform!(u32, uniform1ui, self, *self);
impl_uniform!((f32, f32), uniform2f, self, self.0, self.1);
impl_uniform!((i32, i32), uniform2i, self, self.0, self.1);
impl_uniform!((u32, u32), uniform2ui, self, self.0, self.1);
impl_uniform!((f32, f32, f32), uniform3f, self, self.0, self.1, self.2);
impl_uniform!((i32, i32, i32), uniform3i, self, self.0, self.1, self.2);
impl_uniform!((u32, u32, u32), uniform3ui, self, self.0, self.1, self.2);
impl_uniform!(
    (f32, f32, f32, f32),
    uniform4f,
    self,
    self.0,
    self.1,
    self.2,
    self.3
);
impl_uniform!(
    (i32, i32, i32, i32),
    uniform4i,
    self,
    self.0,
    self.1,
    self.2,
    self.3
);
impl_uniform!(
    (u32, u32, u32, u32),
    uniform4ui,
    self,
    self.0,
    self.1,
    self.2,
    self.3
);