| Flutter Impeller |

half.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_IMPELLER_GEOMETRY_HALF_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_HALF_H_
7
8 #include <cstdint>
9
10 #include "flutter/fml/build_config.h"
11
12 #include "impeller/geometry/color.h"
13 #include "impeller/geometry/point.h"
14 #include "impeller/geometry/scalar.h"
15 #include "impeller/geometry/vector.h"
16
17 // NOLINTBEGIN(google-explicit-constructor)
18
19 #ifdef FML_OS_WIN
20 using InternalHalf = uint16_t;
21 #else
22 using InternalHalf = _Float16;
23 #endif
24
25 namespace impeller {
26
27 /// @brief Convert a scalar to a half precision float.
28 ///
29 /// See also: https://clang.llvm.org/docs/LanguageExtensions.html
30 /// This is not currently supported on Windows toolchains.
31 inline constexpr InternalHalf ScalarToHalf(Scalar f) {
32 #ifdef FML_OS_WIN
33 return static_cast<InternalHalf>(0);
34 #else
35 return static_cast<InternalHalf>(f);
36 #endif
37 }
38
39 /// @brief A storage only class for half precision floating point.
40 struct Half {
41 InternalHalf x = 0;
42
43 constexpr Half() {}
44
45 constexpr Half(double value) : x(ScalarToHalf(static_cast<Scalar>(value))) {}
46
47 constexpr Half(Scalar value) : x(ScalarToHalf(value)) {}
48
49 constexpr Half(int value) : x(ScalarToHalf(static_cast<Scalar>(value))) {}
50
51 constexpr Half(InternalHalf x) : x(x) {}
52
53 constexpr bool operator==(const Half& v) const { return v.x == x; }
54
55 constexpr bool operator!=(const Half& v) const { return v.x != x; }
56 };
57
58 /// @brief A storage only class for half precision floating point vector 4.
59 struct HalfVector4 {
60 union {
61 struct {
62 InternalHalf x = 0;
63 InternalHalf y = 0;
64 InternalHalf z = 0;
65 InternalHalf w = 0;
66 };
67 InternalHalf e[4];
68 };
69
70 constexpr HalfVector4() {}
71
72 constexpr HalfVector4(const Color& a)
73 : x(ScalarToHalf(a.red)),
74 y(ScalarToHalf(a.green)),
75 z(ScalarToHalf(a.blue)),
76 w(ScalarToHalf(a.alpha)) {}
77
78 constexpr HalfVector4(const Vector4& a)
79 : x(ScalarToHalf(a.x)),
80 y(ScalarToHalf(a.y)),
81 z(ScalarToHalf(a.z)),
82 w(ScalarToHalf(a.w)) {}
83
84 constexpr HalfVector4(InternalHalf x,
85 InternalHalf y,
86 InternalHalf z,
87 InternalHalf w)
88 : x(x), y(y), z(z), w(w) {}
89
90 constexpr bool operator==(const HalfVector4& v) const {
91 return v.x == x && v.y == y && v.z == z && v.w == w;
92 }
93
94 constexpr bool operator!=(const HalfVector4& v) const {
95 return v.x != x || v.y != y || v.z != z || v.w != w;
96 }
97 };
98
99 /// @brief A storage only class for half precision floating point vector 3.
100 struct HalfVector3 {
101 union {
102 struct {
103 InternalHalf x = 0;
104 InternalHalf y = 0;
105 InternalHalf z = 0;
106 };
107 InternalHalf e[3];
108 };
109
110 constexpr HalfVector3() {}
111
112 constexpr HalfVector3(const Vector3& a)
113 : x(ScalarToHalf(a.x)), y(ScalarToHalf(a.y)), z(ScalarToHalf(a.z)) {}
114
115 constexpr HalfVector3(InternalHalf x, InternalHalf y, InternalHalf z)
116 : x(x), y(y), z(z) {}
117
118 constexpr bool operator==(const HalfVector3& v) const {
119 return v.x == x && v.y == y && v.z == z;
120 }
121
122 constexpr bool operator!=(const HalfVector3& v) const {
123 return v.x != x || v.y != y || v.z != z;
124 }
125 };
126
127 /// @brief A storage only class for half precision floating point vector 2.
128 struct HalfVector2 {
129 union {
130 struct {
131 InternalHalf x = 0;
132 InternalHalf y = 0;
133 };
134 InternalHalf e[2];
135 };
136
137 constexpr HalfVector2() {}
138
139 constexpr HalfVector2(const Vector2& a)
140 : x(ScalarToHalf(a.x)), y(ScalarToHalf(a.y)) {}
141
142 constexpr HalfVector2(InternalHalf x, InternalHalf y) : x(x), y(y){};
143
144 constexpr bool operator==(const HalfVector2& v) const {
145 return v.x == x && v.y == y;
146 }
147
148 constexpr bool operator!=(const HalfVector2& v) const {
149 return v.x != x || v.y != y;
150 }
151 };
152
153 static_assert(sizeof(Half) == sizeof(uint16_t));
154 static_assert(sizeof(HalfVector2) == 2 * sizeof(Half));
155 static_assert(sizeof(HalfVector3) == 3 * sizeof(Half));
156 static_assert(sizeof(HalfVector4) == 4 * sizeof(Half));
157
158 } // namespace impeller
159
160 namespace std {
161
162 inline std::ostream& operator<<(std::ostream& out, const impeller::Half& p) {
163 out << "(" << static_cast<impeller::Scalar>(p.x) << ")";
164 return out;
165 }
166
167 inline std::ostream& operator<<(std::ostream& out,
168 const impeller::HalfVector2& p) {
169 out << "(" << static_cast<impeller::Scalar>(p.x) << ", "
170 << static_cast<impeller::Scalar>(p.y) << ")";
171 return out;
172 }
173
174 inline std::ostream& operator<<(std::ostream& out,
175 const impeller::HalfVector3& p) {
176 out << "(" << static_cast<impeller::Scalar>(p.x) << ", "
177 << static_cast<impeller::Scalar>(p.y) << ", "
178 << static_cast<impeller::Scalar>(p.z) << ")";
179 return out;
180 }
181
182 inline std::ostream& operator<<(std::ostream& out,
183 const impeller::HalfVector4& p) {
184 out << "(" << static_cast<impeller::Scalar>(p.x) << ", "
185 << static_cast<impeller::Scalar>(p.y) << ", "
186 << static_cast<impeller::Scalar>(p.z) << ", "
187 << static_cast<impeller::Scalar>(p.w) << ")";
188 return out;
189 }
190
191 // NOLINTEND(google-explicit-constructor)
192
193 } // namespace std
194
195 #endif // FLUTTER_IMPELLER_GEOMETRY_HALF_H_
impeller::HalfVector4::HalfVector4
constexpr HalfVector4(const Color &a)
Definition: half.h:72
impeller::Half::x
InternalHalf x
Definition: half.h:41
point.h
impeller::HalfVector3::HalfVector3
constexpr HalfVector3(const Vector3 &a)
Definition: half.h:112
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::HalfVector3::operator==
constexpr bool operator==(const HalfVector3 &v) const
Definition: half.h:118
impeller::HalfVector2::x
InternalHalf x
Definition: half.h:131
impeller::HalfVector4::operator==
constexpr bool operator==(const HalfVector4 &v) const
Definition: half.h:90
impeller::HalfVector2
A storage only class for half precision floating point vector 2.
Definition: half.h:128
impeller::HalfVector3::y
InternalHalf y
Definition: half.h:104
impeller::Color
Definition: color.h:124
impeller::HalfVector2::HalfVector2
constexpr HalfVector2()
Definition: half.h:137
impeller::HalfVector2::HalfVector2
constexpr HalfVector2(const Vector2 &a)
Definition: half.h:139
impeller::Vector4
Definition: vector.h:232
impeller::HalfVector4::HalfVector4
constexpr HalfVector4(InternalHalf x, InternalHalf y, InternalHalf z, InternalHalf w)
Definition: half.h:84
impeller::HalfVector2::operator!=
constexpr bool operator!=(const HalfVector2 &v) const
Definition: half.h:148
std::operator<<
std::ostream & operator<<(std::ostream &out, const impeller::Color &c)
Definition: color.h:951
impeller::Half::Half
constexpr Half(InternalHalf x)
Definition: half.h:51
impeller::HalfVector2::e
InternalHalf e[2]
Definition: half.h:134
impeller::HalfVector3::HalfVector3
constexpr HalfVector3()
Definition: half.h:110
impeller::Half::Half
constexpr Half(double value)
Definition: half.h:45
impeller::HalfVector4::x
InternalHalf x
Definition: half.h:62
impeller::HalfVector3::x
InternalHalf x
Definition: half.h:103
impeller::HalfVector3::HalfVector3
constexpr HalfVector3(InternalHalf x, InternalHalf y, InternalHalf z)
Definition: half.h:115
impeller::HalfVector4::e
InternalHalf e[4]
Definition: half.h:67
impeller::Half
A storage only class for half precision floating point.
Definition: half.h:40
impeller::Half::operator==
constexpr bool operator==(const Half &v) const
Definition: half.h:53
impeller::ScalarToHalf
constexpr InternalHalf ScalarToHalf(Scalar f)
Convert a scalar to a half precision float.
Definition: half.h:31
impeller::HalfVector2::HalfVector2
constexpr HalfVector2(InternalHalf x, InternalHalf y)
Definition: half.h:142
InternalHalf
_Float16 InternalHalf
Definition: half.h:22
impeller::Half::Half
constexpr Half()
Definition: half.h:43
impeller::HalfVector3
A storage only class for half precision floating point vector 3.
Definition: half.h:100
impeller::HalfVector4::y
InternalHalf y
Definition: half.h:63
impeller::HalfVector4::z
InternalHalf z
Definition: half.h:64
scalar.h
impeller::HalfVector4::HalfVector4
constexpr HalfVector4(const Vector4 &a)
Definition: half.h:78
impeller::HalfVector4::operator!=
constexpr bool operator!=(const HalfVector4 &v) const
Definition: half.h:94
vector.h
impeller::Half::Half
constexpr Half(Scalar value)
Definition: half.h:47
std
Definition: comparable.h:95
impeller::TPoint< Scalar >
impeller::HalfVector4::HalfVector4
constexpr HalfVector4()
Definition: half.h:70
impeller::HalfVector4
A storage only class for half precision floating point vector 4.
Definition: half.h:59
impeller::HalfVector3::z
InternalHalf z
Definition: half.h:105
color.h
impeller::Half::Half
constexpr Half(int value)
Definition: half.h:49
impeller::HalfVector2::operator==
constexpr bool operator==(const HalfVector2 &v) const
Definition: half.h:144
impeller::Half::operator!=
constexpr bool operator!=(const Half &v) const
Definition: half.h:55
impeller
Definition: aiks_context.cc:10
impeller::HalfVector4::w
InternalHalf w
Definition: half.h:65
impeller::HalfVector2::y
InternalHalf y
Definition: half.h:132
impeller::HalfVector3::operator!=
constexpr bool operator!=(const HalfVector3 &v) const
Definition: half.h:122
impeller::Vector3
Definition: vector.h:20
impeller::HalfVector3::e
InternalHalf e[3]
Definition: half.h:107