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
use std::mem;
use std::slice;
use std::ops::Deref;
use nalgebra as na;
use na::{Dim, U1, U2, U3, U4};

pub use shaderdata_derive::*;

#[derive(Clone)]
pub struct Data{
    data: Vec<u8>,
}

impl Data{
    pub fn new() -> Data {
        Self::default()
    }

    pub fn with_capacity(capacity: usize) -> Data {
        Data{
            data: Vec::with_capacity(capacity),
        }
    }

    pub fn push<T: WriteStd140>(&mut self, data: T){
        data.write_std140(self)
    }

    pub fn clear(&mut self){
        self.data.clear();
    }

    pub fn bytes(&self) -> usize{
        self.data.len()
    }

    pub fn pad_bytes(&mut self, bytes: usize) {
        self.data.resize(self.data.len() + bytes, 0)
    }

    pub fn pad<T>(&mut self) {
        unsafe{
            self.data.extend_from_slice(slice::from_raw_parts(
                mem::MaybeUninit::<T>::uninit().as_ptr() as *const u8,
                mem::size_of::<T>()
            ))
        };
    }

    pub fn start_struct(&mut self) -> StructBuilder {
        StructBuilder {
            initial_len: self.bytes(),
            data: self,
        }
    }
}

impl Deref for Data{
    type Target = [u8];
    fn deref(&self) -> &[u8]{
        &self.data
    }
}

impl Default for Data{
    fn default() -> Data{
        Data{
            data: vec![],
        }
    }
}

impl PartialEq for Data{
    fn eq(&self, other: &Data) -> bool{
        self.data.eq(&other.data)
    }
}

pub struct StructBuilder<'a> {
    data: &'a mut Data,
    initial_len: usize,
}

impl<'a> Drop for StructBuilder<'a> {
    fn drop(&mut self) {
        let total = self.data.bytes() - self.initial_len;
        let rem = total % 16;
        if rem != 0 {
            let padding = 16 - rem;
            self.data.pad_bytes(padding);
        }
    }
}

impl<'a> StructBuilder<'a>{
    pub fn push<T: WriteStd140>(&mut self, data: T){
        self.data.push(data)
    }

    pub fn pad_bytes(&mut self, bytes: usize) {
        self.data.pad_bytes(bytes)
    }

    pub fn pad<T>(&mut self) {
        self.data.pad::<T>()
    }

    pub fn finish(self) {
        std::mem::drop(self)
    }
}


pub trait Std140: 'static {
    const ALIGNMENT: usize;
}

impl Std140 for f32 {
    const ALIGNMENT: usize = 4;
}

impl Std140 for i32 {
    const ALIGNMENT: usize = 4;
}

impl Std140 for u32 {
    const ALIGNMENT: usize = 4;
}

impl<T: na::Scalar + Std140> Std140 for na::Vector2<T> {
    const ALIGNMENT: usize = mem::size_of::<T>() * 2;
}

impl<T: na::Scalar + Std140> Std140 for (na::Vector2<T>, na::Vector2<T>) {
    const ALIGNMENT: usize = mem::size_of::<T>() * 4;
}

impl<T: na::Scalar + Std140> Std140 for na::Point2<T> {
    const ALIGNMENT: usize = mem::size_of::<T>() * 2;
}

impl<T: na::Scalar + Std140> Std140 for [T;2] {
    const ALIGNMENT: usize = mem::size_of::<T>() * 2;
}

impl<T: na::Scalar + Std140> Std140 for na::Vector3<T> {
    const ALIGNMENT: usize = mem::size_of::<T>() * 4;
}

impl<T: na::Scalar + Std140> Std140 for na::Point3<T> {
    const ALIGNMENT: usize = mem::size_of::<T>() * 4;
}

impl<T: na::Scalar + Std140> Std140 for [T;3] {
    const ALIGNMENT: usize = mem::size_of::<T>() * 4;
}

impl<T: na::Scalar + Std140> Std140 for na::Vector4<T> {
    const ALIGNMENT: usize = mem::size_of::<T>() * 4;
}

impl<T: na::Scalar + Std140> Std140 for na::Point4<T> {
    const ALIGNMENT: usize = mem::size_of::<T>() * 4;
}

impl<T: na::Scalar + Std140> Std140 for [T;4] {
    const ALIGNMENT: usize = mem::size_of::<T>() * 4;
}

impl<T: na::Scalar + Std140> Std140 for na::Matrix4<T> {
    const ALIGNMENT: usize = mem::size_of::<T>() * 4;
}

impl<T: Std140> Std140 for na::Unit<T> {
    const ALIGNMENT: usize = T::ALIGNMENT;
}

impl<T: Std140> Std140 for mem::MaybeUninit<T>{
    const ALIGNMENT: usize = T::ALIGNMENT;
}

impl<T: Std140> Std140 for angle::Rad<T>{
    const ALIGNMENT: usize = T::ALIGNMENT;
}

impl<T: Std140> Std140 for angle::Deg<T>{
    const ALIGNMENT: usize = T::ALIGNMENT;
}

impl<T: Std140, S: 'static> Std140 for color::Rgb<T, S> {
    const ALIGNMENT: usize = mem::size_of::<T>() * 4;
}

impl<T: Std140, S: 'static> Std140 for color::Rg<T, S> {
    const ALIGNMENT: usize = mem::size_of::<T>() * 2;
}

impl<T: Std140, S: 'static> Std140 for color::AlphaColor<T, color::Rgb<T, S>> {
    const ALIGNMENT: usize = mem::size_of::<T>() * 4;
}

pub trait WriteStd140 {
    fn write_std140(&self, data: &mut Data);
}

impl<T: Std140> WriteStd140 for T {
    fn write_std140(&self, data: &mut Data) {
        if data.len() % T::ALIGNMENT != 0 {
            let padding = T::ALIGNMENT - data.len() % T::ALIGNMENT;
            data.pad_bytes(padding);
        }

        unsafe{
            data.data.extend_from_slice(slice::from_raw_parts(
                self as *const T as *const u8,
                mem::size_of::<T>()
            ));
        }
    }
}

impl<T: na::Scalar + Std140 + Copy> WriteStd140 for na::Matrix3<T> {
    fn write_std140(&self, data: &mut Data) {
        data.push(*self.column(0).as_ref());
        data.push(*self.column(1).as_ref());
        data.push(*self.column(2).as_ref());
    }
}

impl<T: WriteStd140> WriteStd140 for Vec<T> {
    fn write_std140(&self, data: &mut Data) {
        for item in self {
            item.write_std140(data);
        }
    }
}

#[test]
fn test() {
    #[derive(Std140, Clone, Copy)]
    struct Std140Struct {
        field0: na::Vector3<f32>,
        field1: f32,
        field2: na::Vector3<f32>,
        field3: f32,
        field4: f32,
    }

    let v = unsafe{ vec![mem::MaybeUninit::<Std140Struct>::uninit().assume_init();10] };
    let mut data = Data::new();
    data.push(v);
    assert_eq!(data.len(), 12 * 4 * 10);
}

#[test]
fn test_manual_padding() {
    let mut data = Data::new();
    data.push(0f32);
    data.push(0f32);
    data.push(na::Vector2::new(0f32, 0f32));
    data.push(na::Vector2::new(0f32, 0f32));
    data.push(0f32);
    data.push(0f32);
    data.push(na::one::<na::Matrix4<f32>>());
    data.push(0i32);
    data.pad::<[f32;3]>();

    assert_eq!(data.bytes(), 28 * 4);
}

#[test]
fn test_struct_buider() {
    let mut data = Data::new();
    let mut struct_data = data.start_struct();
    struct_data.push(na::Vector4::new(0f32, 0f32, 0f32, 0f32));
    struct_data.push(na::Vector3::new(0f32, 0f32, 0f32));
    struct_data.push(0f32);
    struct_data.push(0f32);
    struct_data.push(0f32);
    struct_data.push(na::Vector4::new(0f32, 0f32, 0f32, 0f32));
    struct_data.push(na::Vector4::new(0f32, 0f32, 0f32, 0f32));
    struct_data.push(0f32);
    struct_data.push(na::Vector4::new(0f32, 0f32, 0f32, 0f32));
    struct_data.push(na::Vector4::new(0f32, 0f32, 0f32, 0f32));
    struct_data.push(na::Vector4::new(0i32, 0i32, 0i32, 0i32));
    struct_data.push(na::Vector4::new(0i32, 0i32, 0i32, 0i32));
    struct_data.push(na::one::<na::Matrix4<f32>>());
    std::mem::drop(struct_data);
    assert_eq!(data.bytes(),  56 * 4);
}