ggmath/vector/generics/
alignment.rs

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
use std::{
    any::{type_name, TypeId},
    mem::{transmute, transmute_copy},
};

use super::*;

/// Sealed trait for the alignment rules of a vector.
/// - Doesn't affect the outer vector API, just the inner implementation.
/// - Use the [```Vec2```], [```Vec3```], [```Vec4```] type aliases for the default alignment.
///
/// Implemented by ```VecAligned``` and ```VecPacked```, each have different uses and advantages.
/// To understand them first understand the [Rust type-layout](<https://doc.rust-lang.org/reference/type-layout.html>).
///
/// ### [VecPacked]
///
/// ensures that the vector has the same type-layout as ```[T; N]```.
/// ```
/// assert_eq!(
///     size_of::<Vector<N, T, VecPacked>>(),
///     size_of::<T>() * N
/// );
/// assert_eq!(
///     align_of::<Vector<N, T, VecPacked>>(),
///     align_of::<T>()
/// );
/// ```
///
/// - [```Vec2P```], [```Vec3P```], and [```Vec4P```] are type aliases for packed vectors.
///
/// ### [VecAligned]
///
/// ensures that the vector is aligned to ```[T; N]```'s size.
/// ```
/// assert_eq!(
///     size_of::<Vector<N, T, VecAligned>>(),
///     (size_of::<T>() * N).next_power_of_two()
/// );
/// assert_eq!(
///     align_of::<Vector<N, T, VecAligned>>(),
///     (size_of::<T>() * N).next_power_of_two()
/// );
/// ```
///
/// - This means that the size and alignment of ```Vec3<T>``` is the same as ```Vec4<T>```'s.
/// - This means that ```size/align_of<Vec2> = size_of<T> * 2```, and ```size/align_of<Vec3> = size/align_of<Vec4> = size_of<T> * 4```.
///
/// - [```Vec2```], [```Vec3```], and [```Vec4```] are type aliases for aligned vectors.
///
/// ## How to pick
///
/// Sometimes a specific type-layout is required.
/// For example GPU uniform-structs have strict type-layout requirements, which include vectors following the ```VecAligned``` type-layout.
/// When neither alignment is required, choose based on their performance advantages/disadvantages:
///
/// - ```VecAligned``` results in faster computations because of SIMD registers which require the extra alignment.
/// - ```VecAligned``` may take more space because of the larger alignment, like where ```Vec3``` always takes the space of a ```Vec4```.
/// - ```VecPacked``` takes less space because of the minimal alignment and the lack of padding.
/// - ```VecPacked``` may result in slower computations because of the SIMD register's requirements.
///
/// Basically only use ```VecPacked``` for vectors that you don't perform much computation on.
/// On any other case use ```VecAligned```.
#[allow(private_bounds)]
pub trait VecAlignment: Seal + Sized + 'static + Send + Sync {
    /// Used by [```Vector```] to know its inner type.
    ///
    /// In ```VecPacked```: ```InnerVector = [T; N]```.
    /// for a packed vector, the compiler knows that ```packed_vector.0``` (the inner value) is always a ```[T; N]```.
    ///
    /// in ```VecAligned```: ```InnerVector = <ScalarCount<N> as VecLen>::InnerAlignedVector<T>```.
    /// for an aligned vector, the compiler doesn't know the inner type unless ```N``` is known.
    /// This is because Rust doesn't have a single generic type capable of representing different sizes and alignments.
    ///
    /// Basically ```Vector``` calls ```VecAlignment``` for its inner type,
    /// which calls ```VecLen``` for the final result.
    /// Why is the order ```Vector``` => ```VecAlignment``` => ```VecLen```?
    /// Why not ```Vector``` => ```VecLen``` => ```VecAlignment```?
    /// So that the compiler knows a packed vector is always an array.
    type InnerVector<const N: usize, T: Scalar>: Construct
    where
        ScalarCount<N>: VecLen;
}

/// See [```VecAlignment```].
/// ensures that the vector is aligned to ```[T; N]```'s size.
/// ```
/// assert_eq!(
///     size_of::<Vector<N, T, VecAligned>>(),
///     (size_of::<T>() * N).next_power_of_two()
/// );
/// assert_eq!(
///     align_of::<Vector<N, T, VecAligned>>(),
///     (size_of::<T>() * N).next_power_of_two()
/// );
/// ```
///
/// - This means that the size and alignment of ```Vec3<T>``` is the same as ```Vec4<T>```'s.
/// - This means that ```size/align_of<Vec2> = size_of<T> * 2```, and ```size/align_of<Vec3> = size/align_of<Vec4> = size_of<T> * 4```.
///
/// - [```Vec2```], [```Vec3```], and [```Vec4```] are type aliases for aligned vectors.
///
pub struct VecAligned;

impl VecAlignment for VecAligned {
    type InnerVector<const N: usize, T: Scalar>
        = <ScalarCount<N> as VecLen>::InnerAlignedVector<T>
    where
        ScalarCount<N>: VecLen;
}

/// See [```VecAlignment```].
/// ensures that the vector has the same type-layout as ```[T; N]```.
/// ```
/// assert_eq!(
///     size_of::<Vector<N, T, VecPacked>>(),
///     size_of::<T>() * N
/// );
/// assert_eq!(
///     align_of::<Vector<N, T, VecPacked>>(),
///     align_of::<T>()
/// );
/// ```
///
/// - [```Vec2P```], [```Vec3P```], and [```Vec4P```] are type aliases for packed vectors.
pub struct VecPacked;

impl VecAlignment for VecPacked {
    type InnerVector<const N: usize, T: Scalar> = [T; N]
    where
        ScalarCount<N>: VecLen;
}

pub enum AlignmentResolvedVector<const N: usize, T: Scalar>
where
    ScalarCount<N>: VecLen,
{
    Aligned(Vector<N, T, VecAligned>),
    Packed(Vector<N, T, VecPacked>),
}
pub enum AlignmentResolvedVectorRef<'a, const N: usize, T: Scalar>
where
    ScalarCount<N>: VecLen,
{
    Aligned(&'a Vector<N, T, VecAligned>),
    Packed(&'a Vector<N, T, VecPacked>),
}
pub enum AlignmentResolvedVectorMut<'a, const N: usize, T: Scalar>
where
    ScalarCount<N>: VecLen,
{
    Aligned(&'a mut Vector<N, T, VecAligned>),
    Packed(&'a mut Vector<N, T, VecPacked>),
}

impl<const N: usize, T: Scalar, A: VecAlignment> Vector<N, T, A>
where
    ScalarCount<N>: VecLen,
{
    /// Creates an aligned vector from ```self```.
    /// - Cost: Nothing if ```self``` is already aligned. If ```self``` is packed, moves the vector to satisfy the alignment.
    #[inline(always)]
    pub fn into_aligned(self) -> Vector<N, T, VecAligned> {
        self.into_alignment()
    }
    /// Creates a packed vector from ```self```.
    /// - Cost: Nothing since an aligned vector also satisfies packed alignment.
    #[inline(always)]
    pub fn into_packed(self) -> Vector<N, T, VecPacked> {
        self.into_alignment()
    }
    /// Creates a vector with the specified alignment from ```self```.
    ///
    /// Cost:
    /// - VecAligned -> VecAligned - Nothing.
    /// - VecAligned -> VecPacked - Nothing.
    /// - VecPacked -> VecAligned - Moves the vector to satisfy the alignment.
    /// - VecPacked -> VecPacked - Nothing.
    #[inline(always)]
    pub fn into_alignment<AOutput: VecAlignment>(self) -> Vector<N, T, AOutput> {
        Vector::from_array(self.into_array())
    }

    #[inline(always)]
    pub fn resolve_alignment(self) -> AlignmentResolvedVector<N, T> {
        unsafe {
            if TypeId::of::<A>() == TypeId::of::<VecAligned>() {
                AlignmentResolvedVector::Aligned(transmute_copy(&self))
            } else if TypeId::of::<A>() == TypeId::of::<VecPacked>() {
                AlignmentResolvedVector::Packed(transmute_copy(&self))
            } else {
                panic!("invalid VecAlignment: {}", type_name::<A>())
            }
        }
    }
    #[inline(always)]
    pub fn resolve_alignment_ref(&self) -> AlignmentResolvedVectorRef<N, T> {
        unsafe {
            if TypeId::of::<A>() == TypeId::of::<VecAligned>() {
                AlignmentResolvedVectorRef::Aligned(transmute(self))
            } else if TypeId::of::<A>() == TypeId::of::<VecPacked>() {
                AlignmentResolvedVectorRef::Packed(transmute(self))
            } else {
                panic!("invalid VecAlignment: {}", type_name::<A>())
            }
        }
    }
    #[inline(always)]
    pub fn resolve_alignment_mut(&mut self) -> AlignmentResolvedVectorMut<N, T> {
        unsafe {
            if TypeId::of::<A>() == TypeId::of::<VecAligned>() {
                AlignmentResolvedVectorMut::Aligned(transmute(self))
            } else if TypeId::of::<A>() == TypeId::of::<VecPacked>() {
                AlignmentResolvedVectorMut::Packed(transmute(self))
            } else {
                panic!("invalid VecAlignment: {}", type_name::<A>())
            }
        }
    }

    #[inline(always)]
    pub fn from_resolved_alignment_fns(
        f_aligned: impl FnOnce() -> Vector<N, T, VecAligned>,
        f_packed: impl FnOnce() -> Vector<N, T, VecPacked>,
    ) -> Self {
        unsafe {
            if TypeId::of::<A>() == TypeId::of::<VecAligned>() {
                transmute_copy(&f_aligned())
            } else if TypeId::of::<A>() == TypeId::of::<VecPacked>() {
                transmute_copy(&f_packed())
            } else {
                panic!("invalid VecAlignment: {}", type_name::<A>())
            }
        }
    }
}

trait Seal {}
impl Seal for VecAligned {}
impl Seal for VecPacked {}