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
//! # DO NOT USE THIS CRATE
//!
//! There are fundamental flaws with this method of defining padding. This crate is kept live
//! and not yanked for backwards compat only. For a crate that serves this exact purpose, look
//! at the wonderful [`glsl-layout`](https://docs.rs/glsl-layout/) which doesn't suffer from
//! the same problems.
//!
//! # Old Readme:
//!
//! Vector and Matrix types that are properly aligned for use in std140 uniforms.
//!
//! All the types in this library have the same alignment and size as the equivilant glsl type in the
//! default mode (std140).
//!
//! This fixes the padding within members of structs but padding between members needs to be minded.
//! The types in [`padding`] are there to make this easier.
//!
//! Vectors are constructable to/from an array of their underlying type. Matrices are constructable
//! to/from both 1d and 2d arrays as well as an array of the underlying _vector_ type. (eg. [`Mat2`] can be
//! constructed from `[Vec2; 2]`)
//!
//! # Features
//!
//! - `bytemuck` all types implement `Zeroable` and `Pod`.
//! - `mint` enable conversions to/from equivalent mint types.
//!
//! # Example
//!
//! For the following glsl:
//!
//! ```glsl
//! layout(set = 0, binding = 0) uniform Block {
//!     mat4 mvp;
//!     vec3 position;
//!     vec3 normal;
//!     vec2 uv;
//!     int constants[3];
//! };
//! ```
//!
//! This struct is rife with padding. However it's now easy to mind the padding:
//!
//! ```rust
//! use shader_types::{Vec2, Vec3, Mat4, ArrayMember};
//!
//! // Definition
//! #[repr(C)]
//! #[derive(Copy, Clone)]
//! struct UniformBlock {
//!     mvp: Mat4, // 16 align + 64 size
//!     position: Vec3, // 16 align + 12 size
//!     normal: Vec3, // 16 align + 12 size
//!     uv: Vec2, // 8 align + 8 size
//!     constants: [ArrayMember<i32>; 3] // 3x 16 align + 4 size
//! }
//!
//! fn generate_mvp() -> [f32; 16] {
//!     // ...
//! #     unsafe { std::mem::zeroed() }
//! }
//!
//! // Construction
//! let block = UniformBlock {
//!     // Anything that can be converted to a [f32; 16] or [[f32; 4]; 4] works
//!     mvp: Mat4::from(generate_mvp()),
//!     position: Vec3::new([0.0, 1.0, 2.0]), // `from` also works
//!     normal: Vec3::new([-2.0, 2.0, 3.0]),
//!     uv: Vec2::new([0.0, 1.0]),
//!     constants: [ArrayMember(0), ArrayMember(1), ArrayMember(2)]
//! };
//!
//! // Supports bytemuck with the `bytemuck` feature
//! unsafe impl bytemuck::Zeroable for UniformBlock {}
//! unsafe impl bytemuck::Pod for UniformBlock {}
//!
//! let block_u8: &[u8] = bytemuck::cast_slice(&[block]);
//! ```
//!
//! # MSRV
//!
//! Rust 1.34

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]

/// A single signed integer. Has size 4 and alignment 4.
pub type Int = i32;
/// A single unsigned integer. Has size 4 and alignment 4.
pub type Uint = u32;
/// A single single-precision floating point number. Has size 4 and alignment 4.
pub type Float = f32;
/// A single double-precision floating point number. Has size 8 and alignment 8.
pub type Double = f64;

/// A boolean value. Has size 4 and alignment 4.
#[repr(transparent)]
#[derive(Debug, Copy, Clone, Default, PartialEq, PartialOrd)]
pub struct Bool(u32);

impl Bool {
    pub fn new(value: bool) -> Self {
        Self(value as u32)
    }
}

impl From<Bool> for bool {
    fn from(other: Bool) -> Self {
        other.0 != 0
    }
}

impl From<bool> for Bool {
    fn from(other: bool) -> Self {
        Self(other as u32)
    }
}

#[cfg(feature = "bytemuck")]
unsafe impl bytemuck::Zeroable for Bool {}
#[cfg(feature = "bytemuck")]
unsafe impl bytemuck::Pod for Bool {}

macro_rules! define_vectors {
    ( $(( $name:ident, $mint_name:ident, $prim:ident * $count:literal, align: $align:literal, size: $size:literal ),)* ) => {
        $(
            define_vectors!(@impl
                $name,
                mint::$mint_name<$prim>,
                $prim,
                $count,
                $align,
                concat!(
                    "Vector of ", stringify!($count), " `", stringify!($prim), "` values. ",
                    "Has size ", stringify!($size), " and alignment ", stringify!($align), "."
                ),
                concat!(
                    "Construct a `", stringify!($name), "` from any type which is convertable into a ",
                    "`mint::", stringify!($mint_name), "<", stringify!($prim), ">`."
                )
            );
        )*
    };

    (@impl $name:ident, $mint_type:ty, $ty:ty, $count:literal, $align:literal, $doc:expr, $mint_doc:expr) => {
        #[doc = $doc]
        #[repr(C, align($align))]
        #[derive(Debug, Copy, Clone, Default, PartialEq, PartialOrd)]
        pub struct $name {
            pub inner: [$ty; $count],
        }

        #[cfg(feature = "bytemuck")]
        unsafe impl bytemuck::Zeroable for $name {}
        #[cfg(feature = "bytemuck")]
        unsafe impl bytemuck::Pod for $name {}

        impl $name {
            #[inline(always)]
            pub fn new(inner: [$ty; $count]) -> Self {
                Self {
                    inner,
                }
            }

            #[cfg(feature = "mint")]
            #[cfg_attr(docsrs, doc(cfg(feature = "mint")))]
            #[doc = $mint_doc]
            #[inline(always)]
            pub fn from_mint<T: Into<$mint_type>>(value: T) -> Self {
                Self::from(value.into())
            }
        }

        #[cfg(feature = "mint")]
        impl From<$mint_type> for $name {
            #[inline(always)]
            fn from(other: $mint_type) -> Self {
                // Mint's types do not implement From for arrays, only Into.
                let inner: [$ty; $count] = other.into();

                Self { inner }
            }
        }

        impl From<[$ty; $count]> for $name {
            #[inline(always)]
            fn from(inner: [$ty; $count]) -> Self {
                Self {
                    inner,
                }
            }
        }

        #[cfg(feature = "mint")]
        impl From<$name> for $mint_type {
            #[inline(always)]
            fn from(other: $name) -> Self {
                other.inner.into()
            }
        }

        impl From<$name> for [$ty; $count] {
            #[inline(always)]
            fn from(other: $name) -> Self {
                other.inner
            }
        }
    };
}

define_vectors! {
    (Vec2, Vector2, f32 * 2, align: 8, size: 8),
    (Vec3, Vector3, f32 * 3, align: 16, size: 12),
    (Vec4, Vector4, f32 * 4, align: 16, size: 16),
    (DVec2, Vector2, f64 * 2, align: 16, size: 16),
    (DVec3, Vector3, f64 * 3, align: 32, size: 24),
    (DVec4, Vector4, f64 * 4, align: 32, size: 32),
    (UVec2, Vector2, u32 * 2, align: 8, size: 8),
    (UVec3, Vector3, u32 * 3, align: 16, size: 12),
    (UVec4, Vector4, u32 * 4, align: 16, size: 16),
    (IVec2, Vector2, i32 * 2, align: 8, size: 8),
    (IVec3, Vector3, i32 * 3, align: 16, size: 12),
    (IVec4, Vector4, i32 * 4, align: 16, size: 16),
    (BVec2, Vector2, Bool * 2, align: 8, size: 8),
    (BVec3, Vector3, Bool * 3, align: 16, size: 12),
    (BVec4, Vector4, Bool * 4, align: 16, size: 16),
}

macro_rules! define_matrices {
    ( $(( $name:ident, $mint_name:ident, $prim_ty:ty, $row_ty:ty, $rows:literal * $cols:literal, align: $align:literal, size: $size:literal, pad: $pad:literal, [$($idx:literal),*] ),)* ) => {
        $(
            define_matrices!(@impl
                $name,
                mint::$mint_name<$prim_ty>,
                $align,
                $prim_ty,
                $row_ty,
                $rows,
                $cols,
                $pad,
                [$( $idx ),*],
                concat!(
                    "Matrix of `", stringify!($prim_ty), "` values with ", stringify!($rows), " rows and ", stringify!($cols), " columns. ",
                    "Has size ", stringify!($size), " and alignment ", stringify!($align), "."
                ),
                concat!(
                    "Construct a `", stringify!($name), "` from any type which is convertable into a ",
                    "`mint::", stringify!($mint_name), "<", stringify!($prim_ty), ">`."
                )
            );
        )*
    };

    (@impl $name:ident, $mint_type:ty, $align:literal, $inner_ty:ty, $ty:ty, $count_x:literal, $count_y:literal, $padding:literal, [$( $idx:literal ),*], $doc:expr, $mint_doc:expr) => {
        #[doc = $doc]
        #[repr(C, align($align))]
        #[derive(Debug, Copy, Clone, Default, PartialEq, PartialOrd)]
        pub struct $name {
            pub inner: [$ty; $count_y],
            _padding: [u8; $padding],
        }

        #[cfg(feature = "bytemuck")]
        unsafe impl bytemuck::Zeroable for $name {}
        #[cfg(feature = "bytemuck")]
        unsafe impl bytemuck::Pod for $name {}

        impl $name {
            #[inline(always)]
            pub fn new(inner: [$ty; $count_y]) -> Self {
                Self { inner, _padding: [0; $padding] }
            }

            #[cfg(feature = "mint")]
            #[cfg_attr(docsrs, doc(cfg(feature = "mint")))]
            #[doc = $mint_doc]
            #[inline(always)]
            pub fn from_mint<T: Into<$mint_type>>(value: T) -> Self {
                Self::from(value.into())
            }
        }

        #[cfg(feature = "mint")]
        impl From<$mint_type> for $name {
            #[inline(always)]
            fn from(other: $mint_type) -> Self {
                // Mint's types do not implement From for arrays, only Into.
                let as_arr: [$inner_ty; $count_x * $count_y] = other.into();
                as_arr.into()
            }
        }

        impl From<[$ty; $count_y]> for $name {
            #[inline(always)]
            fn from(inner: [$ty; $count_y]) -> Self {
                Self { inner, _padding: [0; $padding]  }
            }
        }

        impl From<[$inner_ty; $count_x * $count_y]> for $name {
            #[inline(always)]
            fn from(inner: [$inner_ty; $count_x * $count_y]) -> Self {
                let d2: [[$inner_ty; $count_x]; $count_y] = unsafe { core::mem::transmute(inner) };
                Self {
                    inner: [$(<$ty>::from(d2[$idx])),*],
                    _padding: [0; $padding],
                }
            }
        }

        impl From<[[$inner_ty; $count_x]; $count_y]> for $name {
            #[inline(always)]
            fn from(inner: [[$inner_ty; $count_x]; $count_y]) -> Self {
                Self {
                    inner: [$(<$ty>::from(inner[$idx])),*],
                    _padding: [0; $padding],
                }
            }
        }

        #[cfg(feature = "mint")]
        impl From<$name> for $mint_type {
            #[inline(always)]
            fn from(other: $name) -> Self {
                let as_arr = <[[$inner_ty; $count_x]; $count_y]>::from(other);
                as_arr.into()
            }
        }

        impl From<$name> for [$ty; $count_y] {
            #[inline(always)]
            fn from(other: $name) -> Self {
                other.inner
            }
        }

        impl From<$name> for [$inner_ty; $count_x * $count_y] {
            #[inline(always)]
            fn from(other: $name) -> Self {
                let d2: [[$inner_ty; $count_x]; $count_y] = [$(<[$inner_ty; $count_x]>::from(other.inner[$idx])),*];
                unsafe { core::mem::transmute(d2) }
            }
        }

        impl From<$name> for [[$inner_ty; $count_x]; $count_y] {
            #[inline(always)]
            fn from(other: $name) -> Self {
                [$(<[$inner_ty; $count_x]>::from(other.inner[$idx])),*]
            }
        }
    };
}

define_matrices! {
    (Mat2x2, ColumnMatrix2, f32, Vec2, 2 * 2, align: 8, size: 16, pad: 0, [0, 1]),
    (Mat2x3, ColumnMatrix2x3, f32, Vec2, 2 * 3, align: 8, size: 32, pad: 8, [0, 1, 2]),
    (Mat2x4, ColumnMatrix2x4, f32, Vec2, 2 * 4, align: 8, size: 32, pad: 0, [0, 1, 2, 3]),

    (Mat3x2, ColumnMatrix3x2, f32, Vec3, 3 * 2, align: 16, size: 32, pad: 4, [0, 1]),
    (Mat3x3, ColumnMatrix3, f32, Vec3, 3 * 3, align: 16, size: 48, pad: 4, [0, 1, 2]),
    (Mat3x4, ColumnMatrix3x4, f32, Vec3, 3 * 4, align: 16, size: 64, pad: 4, [0, 1, 2, 3]),

    (Mat4x2, ColumnMatrix4x2, f32, Vec4, 4 * 2, align: 16, size: 32, pad: 0, [0, 1]),
    (Mat4x3, ColumnMatrix4x3, f32, Vec4, 4 * 3, align: 16, size: 48, pad: 0, [0, 1, 2]),
    (Mat4x4, ColumnMatrix4, f32, Vec4, 4 * 4, align: 16, size: 64, pad: 0, [0, 1, 2, 3]),

    (DMat2x2, ColumnMatrix2, f64, DVec2, 2 * 2, align: 16, size: 32, pad: 0, [0, 1]),
    (DMat2x3, ColumnMatrix2x3, f64, DVec2, 2 * 3, align: 16, size: 48, pad: 0, [0, 1, 2]),
    (DMat2x4, ColumnMatrix2x4, f64, DVec2, 2 * 4, align: 16, size: 64, pad: 0, [0, 1, 2, 3]),

    (DMat3x2, ColumnMatrix3x2, f64, DVec3, 3 * 2, align: 32, size: 64, pad: 0, [0, 1]),
    (DMat3x3, ColumnMatrix3, f64, DVec3, 3 * 3, align: 32, size: 96, pad: 0, [0, 1, 2]),
    (DMat3x4, ColumnMatrix3x4, f64, DVec3, 3 * 4, align: 32, size: 128, pad: 0, [0, 1, 2, 3]),

    (DMat4x2, ColumnMatrix4x2, f64, DVec4, 4 * 2, align: 32, size: 64, pad: 0, [0, 1]),
    (DMat4x3, ColumnMatrix4x3, f64, DVec4, 4 * 3, align: 32, size: 96, pad: 0, [0, 1, 2]),
    (DMat4x4, ColumnMatrix4, f64, DVec4, 4 * 4, align: 32, size: 128, pad: 0, [0, 1, 2, 3]),
}

/// Matrix of f32s with 2 columns and 2 rows. Alignment 8, size 16.
pub type Mat2 = Mat2x2;
/// Matrix of f32s with 3 columns and 3 rows. Alignment 16, size 48.
pub type Mat3 = Mat3x3;
/// Matrix of f32s with 4 columns and 4 rows. Alignment 16, size 64.
pub type Mat4 = Mat4x4;
/// Matrix of f64s with 2 columns and 3 rows. Alignment 16, size 48.
pub type DMat2 = DMat2x2;
/// Matrix of f64s with 3 columns and 3 rows. Alignment 32, size 96.
pub type DMat3 = DMat3x3;
/// Matrix of f64s with 4 columns and 4 rows. Alignment 32, size 128.
pub type DMat4 = DMat4x4;

/// Pads an element to be in an array in a shader.
///
/// All elements in arrays need to be aligned to 16 bytes. This automatically aligns your types to 16 bytes.
///
/// This glsl:
///
/// ```glsl
/// struct FloatArray {
///     float array[45];
/// };
/// ```
///
/// turns into:
///
/// ```rust
/// #[repr(C)]
/// struct FloatArray {
///     array: [shader_types::ArrayMember<f32>; 45]
/// }
/// ```
#[repr(C, align(16))]
#[derive(Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct ArrayMember<T>(pub T);

#[cfg(feature = "bytemuck")]
unsafe impl<T: bytemuck::Zeroable> bytemuck::Zeroable for ArrayMember<T> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: bytemuck::Pod> bytemuck::Pod for ArrayMember<T> {}

/// Pads a structure for use with dynamic offsets in graphics apis.
///
/// All dynamic offsets need to be aligned to 256 bytes. This automatically aligns your types to 256s.
///
/// Given a shader of:
///
/// ```glsl
/// uniform Uniforms {
///     mat4 mvp;
///     mat4 mv;
/// };
/// ```
///
/// An array of rust structs can be made and used:
///
/// ```rust
/// use shader_types::{Mat4, DynamicOffsetMember};
/// # use std::mem::size_of;
///
/// // Implementations don't matter
/// fn generate_mvp(_: usize) -> [f32; 16] {
///     // ...
/// #     unsafe { std::mem::zeroed() }
/// }
/// fn generate_mv(_: usize) -> [f32; 16] {
///     // ...
/// #     unsafe { std::mem::zeroed() }
/// }
/// fn set_uniform_buffer(_: &[DynamicOffsetMember<Uniforms>]) {
///     // ...
/// }
/// fn bind_uniform_with_offset(_: usize) {
///     // ...
/// }
/// fn render_object(_: usize) {
///     // ...
/// }
///
/// #[repr(C)]
/// struct Uniforms {
///     mvp: Mat4,
///     mv: Mat4,
/// }
///
/// // Generate buffer
/// let mut vec: Vec<DynamicOffsetMember<Uniforms>> = Vec::new();
/// for obj_idx in 0..10 {
///     vec.push(DynamicOffsetMember(Uniforms {
///         mvp: Mat4::from(generate_mvp(obj_idx)),
///         mv: Mat4::from(generate_mv(obj_idx)),
///     }))
/// }
///
/// // Use Buffer
/// set_uniform_buffer(&vec);
/// for obj_idx in 0..10 {
///     let offset = obj_idx * size_of::<DynamicOffsetMember<Uniforms>>();
///     // Offset must be aligned by 256
///     assert_eq!(offset % 256, 0);
///     bind_uniform_with_offset(offset);
///     render_object(obj_idx);
/// }
/// ```
#[repr(C, align(256))]
#[derive(Debug, Copy, Clone, Default, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct DynamicOffsetMember<T>(pub T);

#[cfg(feature = "bytemuck")]
unsafe impl<T: bytemuck::Zeroable> bytemuck::Zeroable for DynamicOffsetMember<T> {}
#[cfg(feature = "bytemuck")]
unsafe impl<T: bytemuck::Pod> bytemuck::Pod for DynamicOffsetMember<T> {}

/// Correctly sized padding helpers.
pub mod padding {
    macro_rules! define_padding {
        ($name:ident, $count:literal <- $doc:literal) => {
            #[doc = $doc]
            #[repr(C)]
            #[derive(Debug, Copy, Clone, Default, PartialEq, PartialOrd)]
            pub struct $name {
                _padding: [u8; $count],
            }

            #[cfg(feature = "bytemuck")]
            unsafe impl bytemuck::Zeroable for $name {}
            #[cfg(feature = "bytemuck")]
            unsafe impl bytemuck::Pod for $name {}

            impl $name {
                #[inline(always)]
                pub fn new() -> Self {
                    Self::default()
                }
            }
        };
    }

    define_padding!(Pad1Float, 4 <- "Padding the size of a single float/uint/int. 4 bytes.");
    define_padding!(Pad2Float, 8 <- "Padding the size of two floats/uints/ints. 8 bytes.");
    define_padding!(Pad3Float, 12 <- "Padding the size of three floats/uints/ints. 12 bytes.");
    define_padding!(Pad4Float, 16 <- "Padding the size of four floats/uints/ints. 16 bytes.");
    define_padding!(Pad1Double, 8 <- "Padding the size of a single double. 8 bytes.");
    define_padding!(Pad2Double, 16 <- "Padding the size of two doubles. 16 bytes.");
    define_padding!(Pad3Double, 24 <- "Padding the size of three doubles. 24 bytes.");
    define_padding!(Pad4Double, 32 <- "Padding the size of four doubles. 32 bytes.");
}

#[cfg(test)]
mod test {
    use crate::*;
    use core::mem::size_of;
    use glsl_layout::AsStd140;
    use std::ptr::null;

    #[repr(C)]
    struct Test1 {
        a: Vec3,
        b: u32,
    }

    #[repr(C)]
    struct Test2 {
        a: std140::vec3,
        b: std140::uint,
    }

    #[derive(AsStd140)]
    struct Test3 {
        a: glsl_layout::vec3,
        b: glsl_layout::int,
    }

    type Test3U = <Test3 as AsStd140>::Std140;

    #[test]
    fn sizes() {
        assert_eq!((&unsafe { &*null::<Test3U>() }.b) as *const i32 as usize, 12);
        assert_eq!(size_of::<Test3U>(), 16);
    }
}