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
//! Contains types that represent uniform data, which is used
//! for methods such as [`gl.set_uniform`]
//! (../context/program_context/trait.ContextProgramExt.html#method.set_uniform).

use std::slice;
use std::mem;

/// The basic value types that are composed in the [`UniformDatumTypes`]
/// (enum.UniformDatumType.html).
pub enum UniformPrimitiveType {
    /// A 32-bit floating point value.
    Float,

    /// A 32-bit signed integer value.
    Int
}

/// The basic types that can be used as uniform values in a program object.
pub enum UniformDatumType {
    /// A single scalar value, containing one primitive (essentially a vector
    /// of one component).
    Vec1(UniformPrimitiveType),

    /// A vector with 2 components.
    Vec2(UniformPrimitiveType),

    /// A vector with 3 components.
    Vec3(UniformPrimitiveType),

    /// A vector with 4 components.
    Vec4(UniformPrimitiveType),

    /// A 2x2 matrix of floating-point values.
    Matrix2x2,

    /// A 3x3 matrix of floating-point values.
    Matrix3x3,

    /// A 4x4 matrix of floating-point values.
    Matrix4x4
}

/// A type that can be set to a uniform value in a program object, using
/// the [`gl.set_uniform`]
/// (../context/program_context/trait.ContextProgramExt.html#method.set_uniform)
/// method. A `UniformData` type can be composed of one or more
/// [`UniformDatums`](trait.UniformDatum.html), and this is likely the type
/// that user types will implement.
pub trait UniformData {
    /// Return the type that this uniform data should be treated as.
    fn uniform_datum_type() -> UniformDatumType;

    /// Create a byte slice of uniform data from `self`.
    fn uniform_bytes(&self) -> &[u8];

    /// Return the number of uniform data elements that `self` contains.
    fn uniform_elements(&self) -> usize;
}



/// A single uniform value, which corresponds to a single
/// primitive GLSL uniform type.
///
/// # Safety
/// This type will be transmuted to a slice according to the value
/// returned by the [`uniform_datum_type`]
/// (trait.UniformDatum.html#tymethod.uniform_datum_type)
/// method. Implementing this method incorrectly will lead to memory
/// unsafety.
pub unsafe trait UniformDatum {
    /// Return the data type this datum corresponds to.
    ///
    /// # Safety
    /// An instance of this type must match the size and memory layout
    /// specified by the returned [`UniformDatumType`]
    /// (enum.UniformDatumType.html).
    fn uniform_datum_type() -> UniformDatumType;
}


/// A single, basic value that can be composed to make a [`UniformDatum`]
/// (trait.UniformDatum.html). Scalar values are an example of a `UniformPrimitive`.
///
/// # Safety
/// This type will be transmuted to a slice according to the value returned
/// by the [`uniform_primitive_type`]
/// (trait.UniformPrimitive.html#tymethod.uniform_primitive_type) method.
/// Implementing this method incorrectly will lead to memory unsafety.
pub unsafe trait UniformPrimitive {
    /// Return the data type this primitive corresponds to.
    ///
    /// # Safety
    /// An instance of this type must be the size of the
    /// [`UniformPrimitiveType`](enum.UniformPrimitiveType.html)
    /// returned by this function.
    fn uniform_primitive_type() -> UniformPrimitiveType;
}

unsafe impl UniformPrimitive for f32 {
    fn uniform_primitive_type() -> UniformPrimitiveType {
        UniformPrimitiveType::Float
    }
}

unsafe impl UniformPrimitive for i32 {
    fn uniform_primitive_type() -> UniformPrimitiveType {
        UniformPrimitiveType::Int
    }
}



unsafe impl<T: UniformPrimitive> UniformDatum for T {
    fn uniform_datum_type() -> UniformDatumType {
        UniformDatumType::Vec1(T::uniform_primitive_type())
    }
}

unsafe impl<T: UniformPrimitive> UniformDatum for [T; 1] {
    fn uniform_datum_type() -> UniformDatumType {
        UniformDatumType::Vec1(T::uniform_primitive_type())
    }
}

unsafe impl<T: UniformPrimitive> UniformDatum for [T; 2] {
    fn uniform_datum_type() -> UniformDatumType {
        UniformDatumType::Vec1(T::uniform_primitive_type())
    }
}

unsafe impl<T: UniformPrimitive> UniformDatum for [T; 3] {
    fn uniform_datum_type() -> UniformDatumType {
        UniformDatumType::Vec1(T::uniform_primitive_type())
    }
}

unsafe impl<T> UniformDatum for [T; 4] where T: UniformPrimitive {
    fn uniform_datum_type() -> UniformDatumType {
        UniformDatumType::Vec1(T::uniform_primitive_type())
    }
}

unsafe impl UniformDatum for [[f32; 2]; 2] {
    fn uniform_datum_type() -> UniformDatumType {
        UniformDatumType::Matrix2x2
    }
}

unsafe impl UniformDatum for [[f32; 3]; 3] {
    fn uniform_datum_type() -> UniformDatumType {
        UniformDatumType::Matrix3x3
    }
}

unsafe impl UniformDatum for [[f32; 4]; 4] {
    fn uniform_datum_type() -> UniformDatumType {
        UniformDatumType::Matrix4x4
    }
}

impl<T: UniformDatum> UniformData for T {
    fn uniform_datum_type() -> UniformDatumType {
        T::uniform_datum_type()
    }

    fn uniform_bytes(&self) -> &[u8] {
        unsafe {
            slice::from_raw_parts(mem::transmute(self), mem::size_of::<T>())
        }
    }

    fn uniform_elements(&self) -> usize {
        1
    }
}

impl<T: UniformDatum> UniformData for [T] {
    fn uniform_datum_type() -> UniformDatumType {
        T::uniform_datum_type()
    }

    fn uniform_bytes(&self) -> &[u8] {
        let size = mem::size_of::<T>() * self.len();
        unsafe {
            slice::from_raw_parts(mem::transmute(&self[0]), size)
        }
    }

    fn uniform_elements(&self) -> usize {
        self.len()
    }
}