-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSwizzle.h
247 lines (208 loc) · 15 KB
/
Swizzle.h
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
/**
* GLSL++
*
* Dan Israel Malta
**/
#pragma once
#include "common.h"
#include <array>
namespace GLSLCPP {
/**
* \brief 'swizzling' class
*
* @param {VECTOR, in} swizzled element, must be of type 'VectorBase'
* @param {T, in} swizzled element type, i.e. - VectorBase<T,..>
* @param {size_t, in} swizzled element length, i.e. - VectorBase<..,N>
* @param {bool, in} true - all indices's are unique, false - there is a duplicate index
* @param {Indexes..., in} elements to swizzle, given as indices's in array
* their length should be equal to the underlying VectorBase, i.e.:
* length(Indexes...) = N in VectroBase<T,N>
* and their value must be smaller then N.
**/
template<class VECTOR, typename T, std::size_t N, bool Unique, std::size_t... Indexes> class Swizzle {
static_assert(std::is_arithmetic<T>::value, "Swizzle must operate on arithmetic types.");
static_assert(is_Vector<VECTOR>::value, "Swizzle must operate on objects of type VectorBase.");
static_assert(N == sizeof...(Indexes), "Swizzle must operate on objects of type VectorBase.");
// properties
private:
alignas(std::aligned_storage<sizeof(T), alignof(T)>::type) std::array<T, N> m_pack{};
// constructor
public:
// default construction from other swizzle (checked that compiler generate reasonable constructors)
Swizzle(const Swizzle&) = default;
Swizzle& operator=(const Swizzle&) = default;
Swizzle(Swizzle&&) noexcept = default;
Swizzle& operator=(Swizzle&&) noexcept = default;
// copy semantics with vectors
template<typename U> Swizzle(const U& xi_rhs, REQUIRE(Is_VectorOfLength_v<U, N>)) {
constexpr std::size_t indexes[] = { Indexes... };
static_for<0, N>([&](std::size_t i) {
const std::size_t index{ indexes[i] };
assert(index < N && " Swizzle indices's are defined such that they point to non existent members, i.e. - they are larger then N.");
m_pack[index] = static_cast<T>(xi_rhs[i]);
});
}
template<typename U, REQUIRE(Is_VectorOfLength_v<U, N>)> Swizzle& operator=(const U& xi_rhs) {
constexpr std::size_t indexes[] = { Indexes... };
static_for<0, N>([&](std::size_t i) {
const std::size_t index{ indexes[i] };
assert(index < N && " Swizzle indices's are defined such that they point to non existent members, i.e. - they are larger then N.");
m_pack[index] = static_cast<T>(xi_rhs[i]);
});
return *this;
}
// move semantics with vectors
template<typename U> explicit constexpr Swizzle(U&& xi_rhs, REQUIRE(Is_VectorOfLength_v<U, N>)) noexcept {
constexpr std::size_t indexes[] = { Indexes... };
auto rhs = FWD(xi_rhs);
static_for<0, N>([&](std::size_t i) {
const std::size_t index{ indexes[i] };
assert(index < N && " Swizzle indices's are defined such that they point to non existent members, i.e. - they are larger then N.");
m_pack[index] = static_cast<T>(rhs[i]);
});
}
template<typename U, REQUIRE(Is_VectorOfLength_v<U, N>)> Swizzle& operator=(U&& xi_rhs) noexcept {
constexpr std::size_t indexes[] = { Indexes... };
auto rhs = FWD(xi_rhs);
static_for<0, N>([&](std::size_t i) {
const std::size_t index{ indexes[i] };
assert(index < N && " Swizzle indices's are defined such that they point to non existent members, i.e. - they are larger then N.");
m_pack[index] = static_cast<T>(rhs[i]);
});
return *this;
}
// methods
public:
// get ordered pack
template<typename U, REQUIRE(is_ArithmeticConvertible_v<U, T>)> void Pack(U xo_pack[N]) {
// unpack parameters
constexpr std::size_t indexes[N] = { Indexes... };
// fill internal data structure
static_for<0, N>([&](std::size_t i) {
xo_pack[i] = static_cast<U>(m_pack[indexes[i]]);
});
}
// swizzle size
constexpr std::size_t length() const { return N; }
// return true if indices's are unique
constexpr bool isUnique() const { return Unique; }
// conversion operations
public:
#define M_CAST_GLSL_VECTOR(NAME) \
operator NAME() const { \
return NAME(m_pack[Indexes]...); \
}
M_CAST_GLSL_VECTOR(VECTOR);
M_CAST_GLSL_VECTOR(Vector2<T>);
M_CAST_GLSL_VECTOR(Vector3<T>);
M_CAST_GLSL_VECTOR(Vector4<T>);
#undef M_CAST_GLSL_VECTOR
// assignment operation overload
public:
// extract as a VECTOR (only when assigned swizzle indices's are unique)
template<bool unq = Unique, REQUIRE(unq == true)>
VECTOR& operator=(VECTOR&& xi_rhs) noexcept {
constexpr std::size_t indexes[] = { Indexes... };
static_for<0, N>([&](std::size_t i) {
m_pack[indexes[i]] = xi_rhs[i];
});
return *reinterpret_cast<VECTOR*>(this);
}
template<bool unq = Unique, REQUIRE(unq == true)>
VECTOR& operator=(const VECTOR& xi_rhs) {
constexpr std::size_t indexes[] = { Indexes... };
static_for<0, N>([&](std::size_t i) {
m_pack[indexes[i]] = xi_rhs[i];
});
return *reinterpret_cast<VECTOR*>(this);
}
// compound operator overload
public:
#define M_COMPOUND(OP) \
template<typename U, bool unq = Unique, REQUIRE((unq == true) && is_ArithmeticConvertible_v<U, T>)> \
Swizzle& operator OP (const U rhs) { \
constexpr std::size_t indexes[] = { Indexes... }; \
static_for<0, N>([&](std::size_t i) { \
m_pack[indexes[i]] OP static_cast<T>(rhs); \
}); \
return *this; \
} \
template<bool unq = Unique, REQUIRE(unq == true)> \
Swizzle& operator OP (VECTOR&& rhs) { \
constexpr std::size_t indexes[] = { Indexes... }; \
static_for<0, N>([&](std::size_t i) { \
m_pack[indexes[i]] OP static_cast<T>(rhs[i]); \
}); \
return *this; \
} \
template<typename U, bool unq = Unique, REQUIRE(unq == true && Is_VectorOfLength_v<U, N>)> \
Swizzle& operator OP (const U& xi_rhs) { \
constexpr std::size_t indexes[] = { Indexes... }; \
auto rhs = FWD(static_cast<U>(xi_rhs)); \
static_for<0, N>([&](std::size_t i) { \
m_pack[indexes[i]] OP static_cast<T>(rhs[i]); \
}); \
return *this; \
} \
template<bool un = Unique, class VECTOR, typename U, bool unq, std::size_t ... indexes, REQUIRE(un == true)> \
Swizzle& operator OP (Swizzle<VECTOR, U, N, unq, indexes...>& xi_rhs) { \
T pack[N]; \
xi_rhs.Pack(pack); \
static_for<0, N>([&](std::size_t i) { \
m_pack[i] OP std::move(pack[i]); \
}); \
return *this; \
}
M_COMPOUND(-= );
M_COMPOUND(+= );
M_COMPOUND(*= );
M_COMPOUND(/= );
M_COMPOUND(&= );
M_COMPOUND(|= );
M_COMPOUND(^= );
M_COMPOUND(>>= );
M_COMPOUND(<<= );
#undef M_COMPOUND
};
/**
* numerical operator overload
**/
#define M_NUMERICAL_OPERATOR(OP, AOP) \
template<class VECTOR, typename T, std::size_t N, bool Unique, std::size_t ... Indexes, bool Unique2, std::size_t ... Indexes2> \
constexpr inline Swizzle<VECTOR, T, N, Unique, Indexes...> operator OP (const Swizzle<VECTOR, T, N, Unique, Indexes...>& xi_lhs, \
const Swizzle<VECTOR, T, N, Unique2, Indexes2...>& xi_rhs) { \
VectorBase<T, N>&& lhs(xi_lhs), \
rhs(xi_rhs); \
lhs AOP rhs; \
return Swizzle<VECTOR, T, N, Unique, Indexes...>(std::move(lhs)); \
} \
template<class VECTOR, typename T, std::size_t N, bool Unique, std::size_t ... Indexes, bool Unique2, std::size_t ... Indexes2> \
constexpr inline Swizzle<VECTOR, T, N, Unique, Indexes...> operator OP (Swizzle<VECTOR, T, N, Unique, Indexes...>&& xi_lhs, \
const Swizzle<VECTOR, T, N, Unique2, Indexes2...>& xi_rhs) { \
VectorBase<T, N>&& lhs(std::move(xi_lhs)), \
rhs(xi_rhs); \
lhs AOP rhs; \
return Swizzle<VECTOR, T, N, Unique, Indexes...>(std::move(lhs)); \
} \
template<class VECTOR, typename T, std::size_t N, bool Unique, std::size_t ... Indexes, bool Unique2, std::size_t ... Indexes2> \
constexpr inline Swizzle<VECTOR, T, N, Unique, Indexes...> operator OP (const Swizzle<VECTOR, T, N, Unique, Indexes...>& xi_lhs, \
Swizzle<VECTOR, T, N, Unique2, Indexes2...>&& xi_rhs) { \
VectorBase<T, N>&& lhs(xi_lhs), \
rhs(std::move(xi_rhs)); \
lhs AOP rhs; \
return Swizzle<VECTOR, T, N, Unique, Indexes...>(std::move(lhs)); \
} \
template<class VECTOR, typename T, std::size_t N, bool Unique, std::size_t ... Indexes, bool Unique2, std::size_t ... Indexes2> \
constexpr inline Swizzle<VECTOR, T, N, Unique, Indexes...> operator OP (Swizzle<VECTOR, T, N, Unique, Indexes...>&& xi_lhs, \
Swizzle<VECTOR, T, N, Unique2, Indexes2...>&& xi_rhs) { \
VectorBase<T, N>&& lhs(std::move(xi_lhs)), \
rhs(std::move(xi_rhs)); \
lhs AOP rhs; \
return Swizzle<VECTOR, T, N, Unique, Indexes...>(std::move(lhs)); \
}
M_NUMERICAL_OPERATOR(+, +=);
M_NUMERICAL_OPERATOR(-, -=);
M_NUMERICAL_OPERATOR(*, *=);
M_NUMERICAL_OPERATOR(/ , /=);
#undef M_NUMERICAL_OPERATOR
}; // namespace GLSLCPP