mev 0.1.0

Metal Et Vulkan abstraction
Documentation
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
use std::{
    any::{type_name, TypeId},
    fmt::Debug,
    mem::{align_of, size_of, transmute, transmute_copy, MaybeUninit},
    ptr::copy_nonoverlapping,
};

use bytemuck::{AnyBitPattern, NoUninit, Pod, Zeroable};

use crate::generic::{VertexBinding, VertexFormat, Zero};

/// Type representable as a POD type with GPU compatible layout.
///
/// Deriving `DeviceRepr` generates type with `#[repr(C)]` and manual padding to make it compatible with GPU layout.
/// Deriving works for structs with fields that implement `DeviceRepr`.
pub trait DeviceRepr: Sized + 'static {
    /// A POD type that can represent same data with layout compatible with shaders.
    /// It is `Self` or another type with same data and manual padding.
    type Repr: bytemuck::Pod + Debug;

    /// A POD type with layout compatible with shaders.
    /// This is the same as `Self::Repr` but with tail padding up to `Self::ALIGN`.
    type ArrayRepr: bytemuck::Pod + Debug;

    /// Construct a `Self::Repr` from `&self`.
    fn as_repr(&self) -> Self::Repr;

    #[inline(always)]
    #[cold]
    fn make_array_repr(&self) -> Self::ArrayRepr {
        unimplemented!("<{} as DeviceRepr>::make_array_repr must be implemented if size of `ArrayRepr` is not equal to size of `Repr`", type_name::<Self>());
    }

    /// Construct a `Self::ArrayRepr` from `&self`.
    #[inline(always)]
    fn as_array_repr(&self) -> Self::ArrayRepr {
        if size_of::<Self::Repr>() == size_of::<Self::ArrayRepr>() {
            // Safety: transmuting between POD types with same size is safe.
            unsafe { bytemuck::cast(self.as_repr()) }
        }
        self.make_array_repr()
    }

    /// Return byte representation of `Repr`.
    #[inline(always)]
    fn as_bytes(repr: &Self::Repr) -> &[u8] {
        bytemuck::bytes_of(repr)
    }

    /// Return byte representation of `ArrayRepr` slice.
    #[inline(always)]
    fn as_array_bytes(repr: &[Self::ArrayRepr]) -> &[u8] {
        bytemuck::cast_slice(repr)
    }

    /// Alignment of repr type.
    const ALIGN: usize;

    /// Size of repr type.
    const SIZE: usize = size_of::<Self::Repr>();

    /// Size of array repr type.
    const ARRAY_SIZE: usize = size_of::<Self::ArrayRepr>();
}

/// A type that implements `DeviceRepr` and is compatible with GPU layout without padding.
///
/// It also requires the type to implement `bytemuck::Pod` since `DeviceRepr::Repr` is `Self` and it requires `bytemuck::Pod`.
///
/// Deriving `AutoDeviceRepr` generates `DeviceRepr` implementation that checks for padding
/// and panics at compile time if the type is not compatible with GPU layout.
/// It also requires that all fields implement `AutoDeviceRepr`.
pub trait AutoDeviceRepr: bytemuck::Pod + DeviceRepr<Repr = Self, ArrayRepr = Self> {}

impl<T> AutoDeviceRepr for T where T: bytemuck::Pod + DeviceRepr<Repr = Self, ArrayRepr = Self> {}

#[inline]
fn array_as_repr_slow<T: DeviceRepr, const N: usize>(array: &[T; N]) -> [T::ArrayRepr; N] {
    // Construct `ArrayRepr` from elements.
    let mut repr: MaybeUninit<[T::ArrayRepr; N]> = MaybeUninit::uninit();
    let ptr = repr.as_mut_ptr().cast::<T::ArrayRepr>();
    for idx in 0..N {
        unsafe {
            ptr.add(idx).write(array[idx].as_array_repr());
        }
    }
    unsafe { repr.assume_init() }
}

impl<T, const N: usize> DeviceRepr for [T; N]
where
    T: DeviceRepr,
{
    type Repr = [T::ArrayRepr; N];
    type ArrayRepr = [T::ArrayRepr; N];

    #[inline(always)]
    fn as_repr(&self) -> [T::ArrayRepr; N] {
        if TypeId::of::<Self>() == TypeId::of::<T::ArrayRepr>() {
            // Self is `ArrayRepr` so it is POD and can be copied.
            return unsafe { transmute_copy(self) };
        }
        if TypeId::of::<Self>() == TypeId::of::<T::Repr>()
            && size_of::<T::Repr>() == size_of::<T::ArrayRepr>()
        {
            // Self is `Repr` so it is POD and can be copied.
            // Size of `Repr` and `ArrayRepr` matches.
            return unsafe { transmute_copy(self) };
        }

        array_as_repr_slow(self)
    }

    #[inline(always)]
    fn as_array_repr(&self) -> [T::ArrayRepr; N] {
        self.as_repr()
    }

    const ALIGN: usize = T::ALIGN;
}

/// Types that can be passed as arguments to shaders.
/// Each element of the enum corresponds to a type that implements [`DataType`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ScalarType {
    Bool,
    Sint8,
    Uint8,
    Sint16,
    Uint16,
    Sint32,
    Uint32,
    Sint64,
    Uint64,
    Float16,
    Float32,
    Float64,
}

impl ScalarType {
    #[inline(always)]
    pub const fn size(&self) -> usize {
        match self {
            ScalarType::Bool => 1,
            ScalarType::Sint8 => 1,
            ScalarType::Uint8 => 1,
            ScalarType::Sint16 => 2,
            ScalarType::Uint16 => 2,
            ScalarType::Sint32 => 4,
            ScalarType::Uint32 => 4,
            ScalarType::Sint64 => 8,
            ScalarType::Uint64 => 8,
            ScalarType::Float16 => 2,
            ScalarType::Float32 => 4,
            ScalarType::Float64 => 8,
        }
    }
}

/// Scalar types compatible with shaders primitives.
pub trait Scalar: crate::private::Sealed + bytemuck::NoUninit + Sized + Debug + 'static {
    /// Type of the scalar.
    const TYPE: ScalarType;

    /// Scalar representation.
    type ScalarRepr: bytemuck::Pod + Debug;

    /// Convert to scalar representation.
    #[inline(always)]
    fn as_scalar_repr(&self) -> Self::ScalarRepr {
        assert_eq!(align_of::<Self>(), align_of::<Self::ScalarRepr>());
        assert_eq!(size_of::<Self>(), size_of::<Self::ScalarRepr>());
        bytemuck::cast(*self)
    }
}

/// Narrow traits for scalar types that can be used as vertex attributes.
pub trait VertexScalar: Scalar {}

const fn vertex_format<T: VertexScalar>(size: VectorSize) -> VertexFormat {
    match (T::TYPE, size) {
        (ScalarType::Sint64 | ScalarType::Uint64 | ScalarType::Float64, _) => {
            panic!("64-bit scalars do not implement VertexScalar")
        }
        (ScalarType::Bool, VectorSize::One) => VertexFormat::Uint8,
        (ScalarType::Sint8, VectorSize::One) => VertexFormat::Sint32,
        (ScalarType::Uint8, VectorSize::One) => VertexFormat::Uint32,
        (ScalarType::Sint16, VectorSize::One) => VertexFormat::Sint32,
        (ScalarType::Uint16, VectorSize::One) => VertexFormat::Uint32,
        (ScalarType::Float16, VectorSize::One) => VertexFormat::Float32,
        (ScalarType::Sint32, VectorSize::One) => VertexFormat::Sint32,
        (ScalarType::Uint32, VectorSize::One) => VertexFormat::Uint32,
        (ScalarType::Float32, VectorSize::One) => VertexFormat::Float32,
        (ScalarType::Bool, VectorSize::Two) => VertexFormat::Uint8x2,
        (ScalarType::Sint8, VectorSize::Two) => VertexFormat::Sint8x2,
        (ScalarType::Uint8, VectorSize::Two) => VertexFormat::Uint8x2,
        (ScalarType::Sint16, VectorSize::Two) => VertexFormat::Sint16x2,
        (ScalarType::Uint16, VectorSize::Two) => VertexFormat::Uint16x2,
        (ScalarType::Float16, VectorSize::Two) => VertexFormat::Float16x2,
        (ScalarType::Sint32, VectorSize::Two) => VertexFormat::Sint32x2,
        (ScalarType::Uint32, VectorSize::Two) => VertexFormat::Uint32x2,
        (ScalarType::Float32, VectorSize::Two) => VertexFormat::Float32x2,
        (ScalarType::Bool, VectorSize::Three) => VertexFormat::Uint8x3,
        (ScalarType::Sint8, VectorSize::Three) => VertexFormat::Sint8x3,
        (ScalarType::Uint8, VectorSize::Three) => VertexFormat::Uint8x3,
        (ScalarType::Sint16, VectorSize::Three) => VertexFormat::Sint16x3,
        (ScalarType::Uint16, VectorSize::Three) => VertexFormat::Uint16x3,
        (ScalarType::Float16, VectorSize::Three) => VertexFormat::Float16x3,
        (ScalarType::Sint32, VectorSize::Three) => VertexFormat::Sint32x3,
        (ScalarType::Uint32, VectorSize::Three) => VertexFormat::Uint32x3,
        (ScalarType::Float32, VectorSize::Three) => VertexFormat::Float32x3,
        (ScalarType::Bool, VectorSize::Four) => VertexFormat::Uint8x4,
        (ScalarType::Sint8, VectorSize::Four) => VertexFormat::Sint8x4,
        (ScalarType::Uint8, VectorSize::Four) => VertexFormat::Uint8x4,
        (ScalarType::Sint16, VectorSize::Four) => VertexFormat::Sint16x4,
        (ScalarType::Uint16, VectorSize::Four) => VertexFormat::Uint16x4,
        (ScalarType::Float16, VectorSize::Four) => VertexFormat::Float16x4,
        (ScalarType::Sint32, VectorSize::Four) => VertexFormat::Sint32x4,
        (ScalarType::Uint32, VectorSize::Four) => VertexFormat::Uint32x4,
        (ScalarType::Float32, VectorSize::Four) => VertexFormat::Float32x4,
    }
}

impl<T> DeviceRepr for T
where
    T: Scalar,
{
    type Repr = T::ScalarRepr;
    type ArrayRepr = T::ScalarRepr;

    #[inline(always)]
    fn as_repr(&self) -> Self::Repr {
        self.as_scalar_repr()
    }

    #[inline(always)]
    fn as_array_repr(&self) -> Self::ArrayRepr {
        self.as_scalar_repr()
    }

    const ALIGN: usize = align_of::<Self>();
}

impl crate::private::Sealed for bool {}

impl Scalar for bool {
    const TYPE: ScalarType = ScalarType::Bool;
    type ScalarRepr = u8;
}

impl crate::private::Sealed for i8 {}

impl Scalar for i8 {
    const TYPE: ScalarType = ScalarType::Sint8;
    type ScalarRepr = Self;

    #[inline(always)]
    fn as_scalar_repr(&self) -> Self {
        *self
    }
}

impl VertexScalar for i8 {}

impl crate::private::Sealed for u8 {}

impl Scalar for u8 {
    const TYPE: ScalarType = ScalarType::Uint8;
    type ScalarRepr = Self;

    #[inline(always)]
    fn as_scalar_repr(&self) -> Self {
        *self
    }
}

impl VertexScalar for u8 {}

impl crate::private::Sealed for i16 {}

impl Scalar for i16 {
    const TYPE: ScalarType = ScalarType::Sint16;
    type ScalarRepr = Self;

    #[inline(always)]
    fn as_scalar_repr(&self) -> Self {
        *self
    }
}

impl VertexScalar for i16 {}

impl crate::private::Sealed for u16 {}

impl Scalar for u16 {
    const TYPE: ScalarType = ScalarType::Uint16;
    type ScalarRepr = Self;

    #[inline(always)]
    fn as_scalar_repr(&self) -> Self {
        *self
    }
}

impl VertexScalar for u16 {}

impl crate::private::Sealed for i32 {}

impl Scalar for i32 {
    const TYPE: ScalarType = ScalarType::Sint32;
    type ScalarRepr = Self;

    #[inline(always)]
    fn as_scalar_repr(&self) -> Self {
        *self
    }
}

impl VertexScalar for i32 {}

impl crate::private::Sealed for u32 {}

impl Scalar for u32 {
    const TYPE: ScalarType = ScalarType::Uint32;
    type ScalarRepr = Self;

    #[inline(always)]
    fn as_scalar_repr(&self) -> Self {
        *self
    }
}

impl VertexScalar for u32 {}

impl crate::private::Sealed for i64 {}

impl Scalar for i64 {
    const TYPE: ScalarType = ScalarType::Sint64;
    type ScalarRepr = Self;

    #[inline(always)]
    fn as_scalar_repr(&self) -> Self {
        *self
    }
}

impl crate::private::Sealed for u64 {}

impl Scalar for u64 {
    const TYPE: ScalarType = ScalarType::Uint64;
    type ScalarRepr = Self;

    #[inline(always)]
    fn as_scalar_repr(&self) -> Self {
        *self
    }
}

impl crate::private::Sealed for f32 {}

impl Scalar for f32 {
    const TYPE: ScalarType = ScalarType::Float32;
    type ScalarRepr = Self;

    #[inline(always)]
    fn as_scalar_repr(&self) -> Self {
        *self
    }
}

impl VertexScalar for f32 {}

impl crate::private::Sealed for f64 {}

impl Scalar for f64 {
    const TYPE: ScalarType = ScalarType::Float64;
    type ScalarRepr = Self;

    #[inline(always)]
    fn as_scalar_repr(&self) -> Self {
        *self
    }
}

/// Supported sizes of vectors.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum VectorSize {
    One = 1,
    Two = 2,
    Three = 3,
    Four = 4,
}

/// Device data types.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DataType {
    /// Scalar type of the data type.
    pub scalar: ScalarType,

    /// Number of columns in the data type.
    pub columns: VectorSize,

    /// Number of rows in the data type.
    pub rows: VectorSize,
}

/// Values that can be passed as attributes to shaders.
/// This trait is sealed and cannot be implemented in other crates.
pub trait VertexAttributes: crate::private::Sealed + 'static {
    const FORMAT: VertexFormat;
    const COUNT: VectorSize;
}

impl<T> VertexAttributes for T
where
    T: VertexScalar,
{
    const FORMAT: VertexFormat = vertex_format::<T>(VectorSize::One);
    const COUNT: VectorSize = VectorSize::One;
}

/// Vector data type.
/// Element type should be `Scalar`, then it can be used as device data type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types)]
pub struct vec<T, const N: usize>(pub [T; N]);

unsafe impl<T, const N: usize> Zeroable for vec<T, N> where T: Zeroable {}
unsafe impl<T, const N: usize> Pod for vec<T, N> where T: Pod {}

impl<T, const N: usize> From<vec<T, N>> for [T; N] {
    #[inline(always)]
    fn from(v: vec<T, N>) -> Self {
        v.0
    }
}

impl<T, const N: usize> From<[T; N]> for vec<T, N> {
    #[inline(always)]
    fn from(v: [T; N]) -> Self {
        vec(v)
    }
}

impl<T, const N: usize> From<&[T; N]> for vec<T, N>
where
    T: Copy,
{
    #[inline(always)]
    fn from(v: &[T; N]) -> Self {
        vec(*v)
    }
}

impl<T, const N: usize> Zero for vec<T, N>
where
    T: Zero,
{
    const ZERO: Self = vec([T::ZERO; N]);
}

/// Matrix data type.
/// Element type should be `Scalar`, then it can be used as device data type.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types)]
pub struct mat<T, const N: usize, const M: usize>(pub [vec<T, M>; N]);

impl<T, const N: usize, const M: usize> Zero for mat<T, N, M>
where
    T: Zero,
{
    const ZERO: Self = mat([const { vec([T::ZERO; M]) }; N]);
}

unsafe impl<T, const N: usize, const M: usize> Zeroable for mat<T, N, M> where T: Zeroable {}
unsafe impl<T, const N: usize, const M: usize> Pod for mat<T, N, M> where T: Pod {}

impl<T, const N: usize, const M: usize> From<mat<T, N, M>> for [[T; M]; N] {
    #[inline(always)]
    fn from(m: mat<T, N, M>) -> Self {
        m.0.map(From::from)
    }
}

impl<T, const N: usize, const M: usize> From<[[T; M]; N]> for mat<T, N, M> {
    #[inline(always)]
    fn from(m: [[T; M]; N]) -> Self {
        mat(m.map(From::from))
    }
}

impl<T, const N: usize, const M: usize> From<&[[T; M]; N]> for mat<T, N, M>
where
    T: Copy,
{
    #[inline(always)]
    fn from(m: &[[T; M]; N]) -> Self {
        mat(m.map(From::from))
    }
}

/// Vector type of two elements.
#[allow(non_camel_case_types)]
pub type vec2<T = f32> = vec<T, 2>;

/// Construct a `vec2`.
#[inline(always)]
pub fn vec2<T>(x: T, y: T) -> vec2<T> {
    vec([x, y])
}

/// Vector type of three elements.
#[allow(non_camel_case_types)]
pub type vec3<T = f32> = vec<T, 3>;

/// Construct a `vec3`.
#[inline(always)]
pub fn vec3<T>(x: T, y: T, z: T) -> vec3<T> {
    vec([x, y, z])
}

/// Vector type of four elements.
#[allow(non_camel_case_types)]
pub type vec4<T = f32> = vec<T, 4>;

/// Construct a `vec4`.
#[inline(always)]
pub fn vec4<T>(x: T, y: T, z: T, w: T) -> vec4<T> {
    vec([x, y, z, w])
}

/// Square matrix type of two columns and two rows.
#[allow(non_camel_case_types)]
pub type mat2<T = f32> = mat<T, 2, 2>;

/// Construct a `mat2`.
#[inline(always)]
pub fn mat2<T>(x: vec2<T>, y: vec2<T>) -> mat2<T> {
    mat([x, y])
}

/// Square matrix type of three columns and three rows.
#[allow(non_camel_case_types)]
pub type mat3<T = f32> = mat<T, 3, 3>;

/// Construct a `mat3`.
#[inline(always)]
pub fn mat3<T>(x: vec3<T>, y: vec3<T>, z: vec3<T>) -> mat3<T> {
    mat([x, y, z])
}

/// Square matrix type of four columns and four rows.
#[allow(non_camel_case_types)]
pub type mat4<T = f32> = mat<T, 4, 4>;

/// Construct a `mat4`.
#[inline(always)]
pub fn mat4<T>(x: vec4<T>, y: vec4<T>, z: vec4<T>, w: vec4<T>) -> mat4<T> {
    mat([x, y, z, w])
}

/// Square matrix type of two columns and two rows.
#[allow(non_camel_case_types)]
pub type mat2x2<T = f32> = mat<T, 2, 2>;

/// Construct a `mat2x2`.
#[inline(always)]
pub fn mat2x2<T>(x: vec2<T>, y: vec2<T>) -> mat2x2<T> {
    mat([x, y])
}

/// Matrix type of two columns and three rows.
#[allow(non_camel_case_types)]
pub type mat2x3<T = f32> = mat<T, 2, 3>;

/// Construct a `mat2x3`.
#[inline(always)]
pub fn mat2x3<T>(x: vec3<T>, y: vec3<T>) -> mat2x3<T> {
    mat([x, y])
}

/// Matrix type of two columns and four rows.
#[allow(non_camel_case_types)]
pub type mat2x4<T = f32> = mat<T, 2, 4>;

/// Construct a `mat2x4`.
#[inline(always)]
pub fn mat2x4<T>(x: vec4<T>, y: vec4<T>) -> mat2x4<T> {
    mat([x, y])
}

/// Matrix type of three columns and two rows.
#[allow(non_camel_case_types)]
pub type mat3x2<T = f32> = mat<T, 3, 2>;

/// Construct a `mat3x2`.
#[inline(always)]
pub fn mat3x2<T>(x: vec2<T>, y: vec2<T>, z: vec2<T>) -> mat3x2<T> {
    mat([x, y, z])
}

/// Square matrix type of three columns and three rows.
#[allow(non_camel_case_types)]
pub type mat3x3<T = f32> = mat<T, 3, 3>;

/// Construct a `mat3x3`.
#[inline(always)]
pub fn mat3x3<T>(x: vec3<T>, y: vec3<T>, z: vec3<T>) -> mat3x3<T> {
    mat([x, y, z])
}

/// Matrix type of three columns and four rows.
#[allow(non_camel_case_types)]
pub type mat3x4<T = f32> = mat<T, 3, 4>;

/// Construct a `mat3x4`.
#[inline(always)]
pub fn mat3x4<T>(x: vec4<T>, y: vec4<T>, z: vec4<T>) -> mat3x4<T> {
    mat([x, y, z])
}

/// Matrix type of four columns and two rows.
#[allow(non_camel_case_types)]
pub type mat4x2<T = f32> = mat<T, 4, 2>;

/// Construct a `mat4x2`.
#[inline(always)]
pub fn mat4x2<T>(x: vec2<T>, y: vec2<T>, z: vec2<T>, w: vec2<T>) -> mat4x2<T> {
    mat([x, y, z, w])
}

/// Matrix type of four columns and three rows.
#[allow(non_camel_case_types)]
pub type mat4x3<T = f32> = mat<T, 4, 3>;

/// Construct a `mat4x3`.
#[inline(always)]
pub fn mat4x3<T>(x: vec3<T>, y: vec3<T>, z: vec3<T>, w: vec3<T>) -> mat4x3<T> {
    mat([x, y, z, w])
}

/// Square matrix type of four columns and four rows.
#[allow(non_camel_case_types)]
pub type mat4x4<T = f32> = mat<T, 4, 4>;

/// Construct a `mat4x4`.
#[inline(always)]
pub fn mat4x4<T>(x: vec4<T>, y: vec4<T>, z: vec4<T>, w: vec4<T>) -> mat4x4<T> {
    mat([x, y, z, w])
}

impl<T> crate::private::Sealed for vec<T, 1> where T: Scalar {}
impl<T> crate::private::Sealed for vec<T, 2> where T: Scalar {}
impl<T> crate::private::Sealed for vec<T, 3> where T: Scalar {}
impl<T> crate::private::Sealed for vec<T, 4> where T: Scalar {}

impl<T, const N: usize> crate::private::Sealed for mat<T, 1, N> where
    vec<T, N>: crate::private::Sealed
{
}

impl<T, const N: usize> crate::private::Sealed for mat<T, 2, N> where
    vec<T, N>: crate::private::Sealed
{
}

impl<T, const N: usize> crate::private::Sealed for mat<T, 3, N> where
    vec<T, N>: crate::private::Sealed
{
}

impl<T, const N: usize> crate::private::Sealed for mat<T, 4, N> where
    vec<T, N>: crate::private::Sealed
{
}

impl<T> VertexAttributes for vec2<T>
where
    T: VertexScalar,
{
    const FORMAT: VertexFormat = vertex_format::<T>(VectorSize::Two);
    const COUNT: VectorSize = VectorSize::One;
}

impl<T> VertexAttributes for vec3<T>
where
    T: VertexScalar,
{
    const FORMAT: VertexFormat = vertex_format::<T>(VectorSize::Three);
    const COUNT: VectorSize = VectorSize::One;
}

impl<T> VertexAttributes for vec4<T>
where
    T: VertexScalar,
{
    const FORMAT: VertexFormat = vertex_format::<T>(VectorSize::Four);
    const COUNT: VectorSize = VectorSize::One;
}

impl<T, const M: usize> VertexAttributes for mat<T, 2, M>
where
    vec<T, M>: VertexAttributes,
{
    const FORMAT: VertexFormat = <vec<T, M> as VertexAttributes>::FORMAT;
    const COUNT: VectorSize = VectorSize::Two;
}

impl<T, const M: usize> VertexAttributes for mat<T, 3, M>
where
    vec<T, M>: VertexAttributes,
{
    const FORMAT: VertexFormat = <vec<T, M> as VertexAttributes>::FORMAT;
    const COUNT: VectorSize = VectorSize::Three;
}

impl<T, const M: usize> VertexAttributes for mat<T, 4, M>
where
    vec<T, M>: VertexAttributes,
{
    const FORMAT: VertexFormat = <vec<T, M> as VertexAttributes>::FORMAT;
    const COUNT: VectorSize = VectorSize::Four;
}

impl<T> DeviceRepr for vec2<T>
where
    T: Scalar,
{
    type Repr = vec2<T::ScalarRepr>;
    type ArrayRepr = vec2<T::ScalarRepr>;

    #[inline(always)]
    fn as_repr(&self) -> Self::Repr {
        vec(self.0.as_repr())
    }

    const ALIGN: usize = size_of::<vec2<T::ScalarRepr>>();
}

impl<T> DeviceRepr for vec3<T>
where
    T: Scalar,
{
    type Repr = vec3<T::ScalarRepr>;
    type ArrayRepr = vec4<T::ScalarRepr>;

    #[inline(always)]
    fn as_repr(&self) -> Self::Repr {
        vec(self.0.as_repr())
    }

    #[inline(always)]
    fn make_array_repr(&self) -> vec4<T::ScalarRepr> {
        let [a, b, c] = self.0.as_repr();
        vec([a, b, c, Zeroable::zeroed()])
    }

    const ALIGN: usize = size_of::<vec4<T::ScalarRepr>>();
}

impl<T> DeviceRepr for vec4<T>
where
    T: Scalar,
{
    type Repr = vec4<T::ScalarRepr>;
    type ArrayRepr = vec4<T::ScalarRepr>;

    #[inline(always)]
    fn as_repr(&self) -> Self::Repr {
        vec(self.0.as_repr())
    }

    const ALIGN: usize = size_of::<vec4<T::ScalarRepr>>();
}

impl<T, const N: usize> DeviceRepr for mat<T, N, 2>
where
    T: Scalar,
{
    type Repr = mat<T::ScalarRepr, N, 2>;
    type ArrayRepr = mat<T::ScalarRepr, N, 2>;

    #[inline(always)]
    fn as_repr(&self) -> Self::Repr {
        mat(self.0.as_repr())
    }

    const ALIGN: usize = size_of::<vec2<T::ScalarRepr>>();
}

impl<T, const N: usize> DeviceRepr for mat<T, N, 3>
where
    T: Scalar,
{
    type Repr = mat<T::ScalarRepr, N, 4>;
    type ArrayRepr = mat<T::ScalarRepr, N, 4>;

    #[inline(always)]
    fn as_repr(&self) -> Self::Repr {
        mat(self.0.as_repr())
    }

    const ALIGN: usize = size_of::<vec4<T::ScalarRepr>>();
}

impl<T, const N: usize> DeviceRepr for mat<T, N, 4>
where
    T: Scalar,
{
    type Repr = mat<T::ScalarRepr, N, 4>;
    type ArrayRepr = mat<T::ScalarRepr, N, 4>;

    #[inline(always)]
    fn as_repr(&self) -> Self::Repr {
        mat(self.0.as_repr())
    }

    const ALIGN: usize = size_of::<vec4<T::ScalarRepr>>();
}

/// Boolean vector type of two elements.
#[allow(non_camel_case_types)]
pub type bvec2 = vec2<bool>;

/// Boolean vector type of three elements.
#[allow(non_camel_case_types)]
pub type bvec3 = vec3<bool>;

/// Boolean vector type of four elements.
#[allow(non_camel_case_types)]
pub type bvec4 = vec4<bool>;

/// Square boolean matrix type of two columns and two rows.
#[allow(non_camel_case_types)]
pub type bmat2 = mat2<bool>;

/// Square boolean matrix type of three columns and three rows.
#[allow(non_camel_case_types)]
pub type bmat3 = mat3<bool>;

/// Square boolean matrix type of four columns and four rows.
#[allow(non_camel_case_types)]
pub type bmat4 = mat4<bool>;

/// Square boolean matrix type of two columns and two rows.
#[allow(non_camel_case_types)]
pub type bmat2x2 = mat2x2<bool>;

/// Boolean matrix type of two columns and three rows.
#[allow(non_camel_case_types)]
pub type bmat2x3 = mat2x3<bool>;

/// Boolean matrix type of two columns and four rows.
#[allow(non_camel_case_types)]
pub type bmat2x4 = mat2x4<bool>;

/// Boolean matrix type of three columns and two rows.
#[allow(non_camel_case_types)]
pub type bmat3x2 = mat3x2<bool>;

/// Square boolean matrix type of three columns and three rows.
#[allow(non_camel_case_types)]
pub type bmat3x3 = mat3x3<bool>;

/// Boolean matrix type of three columns and four rows.
#[allow(non_camel_case_types)]
pub type bmat3x4 = mat3x4<bool>;

/// Boolean matrix type of four columns and two rows.
#[allow(non_camel_case_types)]
pub type bmat4x2 = mat4x2<bool>;

/// Boolean matrix type of four columns and three rows.
#[allow(non_camel_case_types)]
pub type bmat4x3 = mat4x3<bool>;

/// Square boolean matrix type of four columns and four rows.
#[allow(non_camel_case_types)]
pub type bmat4x4 = mat4x4<bool>;

/// Signed integer vector type of two elements.
#[allow(non_camel_case_types)]
pub type ivec2 = vec2<i32>;

/// Signed integer vector type of three elements.
#[allow(non_camel_case_types)]
pub type ivec3 = vec3<i32>;

/// Signed integer vector type of four elements.
#[allow(non_camel_case_types)]
pub type ivec4 = vec4<i32>;

/// Square signed integer matrix type of two columns and two rows.
#[allow(non_camel_case_types)]
pub type imat2 = mat2<i32>;

/// Square signed integer matrix type of three columns and three rows.
#[allow(non_camel_case_types)]
pub type imat3 = mat3<i32>;

/// Square signed integer matrix type of four columns and four rows.
#[allow(non_camel_case_types)]
pub type imat4 = mat4<i32>;

/// Square signed integer matrix type of two columns and two rows.
#[allow(non_camel_case_types)]
pub type imat2x2 = mat2x2<i32>;

/// Signed integer matrix type of two columns and three rows.
#[allow(non_camel_case_types)]
pub type imat2x3 = mat2x3<i32>;

/// Signed integer matrix type of two columns and four rows.
#[allow(non_camel_case_types)]
pub type imat2x4 = mat2x4<i32>;

/// Signed integer matrix type of three columns and two rows.
#[allow(non_camel_case_types)]
pub type imat3x2 = mat3x2<i32>;

/// Square signed integer matrix type of three columns and three rows.
#[allow(non_camel_case_types)]
pub type imat3x3 = mat3x3<i32>;

/// Signed integer matrix type of three columns and four rows.
#[allow(non_camel_case_types)]
pub type imat3x4 = mat3x4<i32>;

/// Signed integer matrix type of four columns and two rows.
#[allow(non_camel_case_types)]
pub type imat4x2 = mat4x2<i32>;

/// Signed integer matrix type of four columns and three rows.
#[allow(non_camel_case_types)]
pub type imat4x3 = mat4x3<i32>;

/// Square signed integer matrix type of four columns and four rows.
#[allow(non_camel_case_types)]
pub type imat4x4 = mat4x4<i32>;

/// Unsigned integer vector type of two elements.
#[allow(non_camel_case_types)]
pub type uvec2 = vec2<u32>;

/// Unsigned integer vector type of three elements.
#[allow(non_camel_case_types)]
pub type uvec3 = vec3<u32>;

/// Unsigned integer vector type of four elements.
#[allow(non_camel_case_types)]
pub type uvec4 = vec4<u32>;

/// Square unsigned integer matrix type of two columns and two rows.
#[allow(non_camel_case_types)]
pub type umat2 = mat2<u32>;

/// Square unsigned integer matrix type of three columns and three rows.
#[allow(non_camel_case_types)]
pub type umat3 = mat3<u32>;

/// Square unsigned integer matrix type of four columns and four rows.
#[allow(non_camel_case_types)]
pub type umat4 = mat4<u32>;

/// Square unsigned integer matrix type of two columns and two rows.
#[allow(non_camel_case_types)]
pub type umat2x2 = mat2x2<u32>;

/// Unsigned integer matrix type of two columns and three rows.
#[allow(non_camel_case_types)]
pub type umat2x3 = mat2x3<u32>;

/// Unsigned integer matrix type of two columns and four rows.
#[allow(non_camel_case_types)]
pub type umat2x4 = mat2x4<u32>;

/// Unsigned integer matrix type of three columns and two rows.
#[allow(non_camel_case_types)]
pub type umat3x2 = mat3x2<u32>;

/// Square unsigned integer matrix type of three columns and three rows.
#[allow(non_camel_case_types)]
pub type umat3x3 = mat3x3<u32>;

/// Unsigned integer matrix type of three columns and four rows.
#[allow(non_camel_case_types)]
pub type umat3x4 = mat3x4<u32>;

/// Unsigned integer matrix type of four columns and two rows.
#[allow(non_camel_case_types)]
pub type umat4x2 = mat4x2<u32>;

/// Unsigned integer matrix type of four columns and three rows.
#[allow(non_camel_case_types)]
pub type umat4x3 = mat4x3<u32>;

/// Square unsigned integer matrix type of four columns and four rows.
#[allow(non_camel_case_types)]
pub type umat4x4 = mat4x4<u32>;

/// Double precision floating point vector type of two elements.
#[allow(non_camel_case_types)]
pub type dvec2 = vec2<f64>;

/// Double precision floating point vector type of three elements.
#[allow(non_camel_case_types)]
pub type dvec3 = vec3<f64>;

/// Double precision floating point vector type of four elements.
#[allow(non_camel_case_types)]
pub type dvec4 = vec4<f64>;

/// Square double precision floating point matrix type of two columns and two rows.
#[allow(non_camel_case_types)]
pub type dmat2 = mat2<f64>;

/// Square double precision floating point matrix type of three columns and three rows.
#[allow(non_camel_case_types)]
pub type dmat3 = mat3<f64>;

/// Square double precision floating point matrix type of four columns and four rows.
#[allow(non_camel_case_types)]
pub type dmat4 = mat4<f64>;

/// Square double precision floating point matrix type of two columns and two rows.
#[allow(non_camel_case_types)]
pub type dmat2x2 = mat2x2<f64>;

/// Double precision floating point matrix type of two columns and three rows.
#[allow(non_camel_case_types)]
pub type dmat2x3 = mat2x3<f64>;

/// Double precision floating point matrix type of two columns and four rows.
#[allow(non_camel_case_types)]
pub type dmat2x4 = mat2x4<f64>;

/// Double precision floating point matrix type of three columns and two rows.
#[allow(non_camel_case_types)]
pub type dmat3x2 = mat3x2<f64>;

/// Square double precision floating point matrix type of three columns and three rows.
#[allow(non_camel_case_types)]
pub type dmat3x3 = mat3x3<f64>;

/// Double precision floating point matrix type of three columns and four rows.
#[allow(non_camel_case_types)]
pub type dmat3x4 = mat3x4<f64>;

/// Double precision floating point matrix type of four columns and two rows.
#[allow(non_camel_case_types)]
pub type dmat4x2 = mat4x2<f64>;

/// Double precision floating point matrix type of four columns and three rows.
#[allow(non_camel_case_types)]
pub type dmat4x3 = mat4x3<f64>;

/// Square double precision floating point matrix type of four columns and four rows.
#[allow(non_camel_case_types)]
pub type dmat4x4 = mat4x4<f64>;