blob: 1281aa40cbf302b9e9c5d2b053b9839aae71e984 [file] [log] [blame]
Mathias Agopian595ea772013-08-21 23:10:41 -07001/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy5d4bae72016-11-08 09:49:25 -080017#ifndef UI_VEC4_H_
18#define UI_VEC4_H_
Mathias Agopian595ea772013-08-21 23:10:41 -070019
Romain Guy5d4bae72016-11-08 09:49:25 -080020#include <ui/vec3.h>
21#include <ui/half.h>
Mathias Agopian595ea772013-08-21 23:10:41 -070022#include <stdint.h>
23#include <sys/types.h>
24
Mathias Agopian595ea772013-08-21 23:10:41 -070025
26namespace android {
27// -------------------------------------------------------------------------------------
28
Romain Guy5d4bae72016-11-08 09:49:25 -080029namespace details {
30
Mathias Agopian595ea772013-08-21 23:10:41 -070031template <typename T>
Romain Guy5d4bae72016-11-08 09:49:25 -080032class TVec4 : public TVecProductOperators<TVec4, T>,
33 public TVecAddOperators<TVec4, T>,
34 public TVecUnaryOperators<TVec4, T>,
35 public TVecComparisonOperators<TVec4, T>,
36 public TVecFunctions<TVec4, T>,
37 public TVecDebug<TVec4, T> {
Mathias Agopian595ea772013-08-21 23:10:41 -070038public:
39 enum no_init { NO_INIT };
40 typedef T value_type;
41 typedef T& reference;
42 typedef T const& const_reference;
43 typedef size_t size_type;
44
45 union {
46 struct { T x, y, z, w; };
47 struct { T s, t, p, q; };
48 struct { T r, g, b, a; };
Romain Guy5d4bae72016-11-08 09:49:25 -080049 TVec2<T> xy;
50 TVec2<T> st;
51 TVec2<T> rg;
52 TVec3<T> xyz;
53 TVec3<T> stp;
54 TVec3<T> rgb;
Mathias Agopian595ea772013-08-21 23:10:41 -070055 };
56
Romain Guy5d4bae72016-11-08 09:49:25 -080057 static constexpr size_t SIZE = 4;
58 inline constexpr size_type size() const { return SIZE; }
Mathias Agopian595ea772013-08-21 23:10:41 -070059
60 // array access
Romain Guy5d4bae72016-11-08 09:49:25 -080061 inline constexpr T const& operator[](size_t i) const {
62#if __cplusplus >= 201402L
63 // only possible in C++0x14 with constexpr
64 assert(i < SIZE);
65#endif
66 return (&x)[i];
67 }
68
69 inline T& operator[](size_t i) {
70 assert(i < SIZE);
71 return (&x)[i];
72 }
Mathias Agopian595ea772013-08-21 23:10:41 -070073
74 // -----------------------------------------------------------------------
Romain Guy5d4bae72016-11-08 09:49:25 -080075 // we want the compiler generated versions for these...
76 TVec4(const TVec4&) = default;
77 ~TVec4() = default;
78 TVec4& operator = (const TVec4&) = default;
Mathias Agopian595ea772013-08-21 23:10:41 -070079
80 // constructors
81
82 // leaves object uninitialized. use with caution.
Romain Guy5d4bae72016-11-08 09:49:25 -080083 explicit
84 constexpr TVec4(no_init) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070085
86 // default constructor
Romain Guy5d4bae72016-11-08 09:49:25 -080087 constexpr TVec4() : x(0), y(0), z(0), w(0) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070088
89 // handles implicit conversion to a tvec4. must not be explicit.
Romain Guy5d4bae72016-11-08 09:49:25 -080090 template<typename A, typename = typename std::enable_if<std::is_arithmetic<A>::value >::type>
91 constexpr TVec4(A v) : x(v), y(v), z(v), w(v) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070092
93 template<typename A, typename B, typename C, typename D>
Romain Guy5d4bae72016-11-08 09:49:25 -080094 constexpr TVec4(A x, B y, C z, D w) : x(x), y(y), z(z), w(w) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070095
96 template<typename A, typename B, typename C>
Romain Guy5d4bae72016-11-08 09:49:25 -080097 constexpr TVec4(const TVec2<A>& v, B z, C w) : x(v.x), y(v.y), z(z), w(w) { }
Mathias Agopian595ea772013-08-21 23:10:41 -070098
99 template<typename A, typename B>
Romain Guy5d4bae72016-11-08 09:49:25 -0800100 constexpr TVec4(const TVec3<A>& v, B w) : x(v.x), y(v.y), z(v.z), w(w) { }
Mathias Agopian595ea772013-08-21 23:10:41 -0700101
102 template<typename A>
Romain Guy5d4bae72016-11-08 09:49:25 -0800103 explicit
104 constexpr TVec4(const TVec4<A>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
Mathias Agopian595ea772013-08-21 23:10:41 -0700105};
106
Romain Guy5d4bae72016-11-08 09:49:25 -0800107} // namespace details
Mathias Agopian595ea772013-08-21 23:10:41 -0700108
109// ----------------------------------------------------------------------------------------
Mathias Agopian595ea772013-08-21 23:10:41 -0700110
Romain Guy5d4bae72016-11-08 09:49:25 -0800111typedef details::TVec4<double> double4;
112typedef details::TVec4<float> float4;
113typedef details::TVec4<float> vec4;
114typedef details::TVec4<half> half4;
115typedef details::TVec4<int32_t> int4;
116typedef details::TVec4<uint32_t> uint4;
117typedef details::TVec4<int16_t> short4;
118typedef details::TVec4<uint16_t> ushort4;
119typedef details::TVec4<int8_t> byte4;
120typedef details::TVec4<uint8_t> ubyte4;
121
122// ----------------------------------------------------------------------------------------
123} // namespace android
124
125#endif // UI_VEC4_H_