summaryrefslogtreecommitdiff
path: root/src/video_core
diff options
context:
space:
mode:
authorGravatar archshift2015-05-31 09:55:29 -0700
committerGravatar archshift2015-05-31 09:55:29 -0700
commit0414ad20cb82ef0565d08c4102b64ccbc89d5012 (patch)
tree3ec018dd373582c3f460ae0441f600ba5c445e48 /src/video_core
parentMerge pull request #832 from yuriks/refresh-rate-option (diff)
parentMove video_core/color.h to common/color.h (diff)
downloadyuzu-0414ad20cb82ef0565d08c4102b64ccbc89d5012.tar.gz
yuzu-0414ad20cb82ef0565d08c4102b64ccbc89d5012.tar.xz
yuzu-0414ad20cb82ef0565d08c4102b64ccbc89d5012.zip
Merge pull request #811 from archshift/commonify
Commonify video_core utility headers
Diffstat (limited to 'src/video_core')
-rw-r--r--src/video_core/CMakeLists.txt2
-rw-r--r--src/video_core/color.h215
-rw-r--r--src/video_core/debug_utils/debug_utils.cpp4
-rw-r--r--src/video_core/debug_utils/debug_utils.h3
-rw-r--r--src/video_core/math.h640
-rw-r--r--src/video_core/pica.h3
-rw-r--r--src/video_core/rasterizer.cpp2
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp3
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer_cache.cpp2
9 files changed, 9 insertions, 865 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt
index 0258a3255..5c7f4ae18 100644
--- a/src/video_core/CMakeLists.txt
+++ b/src/video_core/CMakeLists.txt
@@ -29,11 +29,9 @@ set(HEADERS
29 renderer_opengl/pica_to_gl.h 29 renderer_opengl/pica_to_gl.h
30 renderer_opengl/renderer_opengl.h 30 renderer_opengl/renderer_opengl.h
31 clipper.h 31 clipper.h
32 color.h
33 command_processor.h 32 command_processor.h
34 gpu_debugger.h 33 gpu_debugger.h
35 hwrasterizer_base.h 34 hwrasterizer_base.h
36 math.h
37 pica.h 35 pica.h
38 primitive_assembly.h 36 primitive_assembly.h
39 rasterizer.h 37 rasterizer.h
diff --git a/src/video_core/color.h b/src/video_core/color.h
deleted file mode 100644
index 4d2026eb0..000000000
--- a/src/video_core/color.h
+++ /dev/null
@@ -1,215 +0,0 @@
1// Copyright 2014 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "common/common_types.h"
8#include "common/swap.h"
9
10#include "video_core/math.h"
11
12namespace Color {
13
14/// Convert a 1-bit color component to 8 bit
15inline u8 Convert1To8(u8 value) {
16 return value * 255;
17}
18
19/// Convert a 4-bit color component to 8 bit
20inline u8 Convert4To8(u8 value) {
21 return (value << 4) | value;
22}
23
24/// Convert a 5-bit color component to 8 bit
25inline u8 Convert5To8(u8 value) {
26 return (value << 3) | (value >> 2);
27}
28
29/// Convert a 6-bit color component to 8 bit
30inline u8 Convert6To8(u8 value) {
31 return (value << 2) | (value >> 4);
32}
33
34/// Convert a 8-bit color component to 1 bit
35inline u8 Convert8To1(u8 value) {
36 return value >> 7;
37}
38
39/// Convert a 8-bit color component to 4 bit
40inline u8 Convert8To4(u8 value) {
41 return value >> 4;
42}
43
44/// Convert a 8-bit color component to 5 bit
45inline u8 Convert8To5(u8 value) {
46 return value >> 3;
47}
48
49/// Convert a 8-bit color component to 6 bit
50inline u8 Convert8To6(u8 value) {
51 return value >> 2;
52}
53
54/**
55 * Decode a color stored in RGBA8 format
56 * @param bytes Pointer to encoded source color
57 * @return Result color decoded as Math::Vec4<u8>
58 */
59inline const Math::Vec4<u8> DecodeRGBA8(const u8* bytes) {
60 return { bytes[3], bytes[2], bytes[1], bytes[0] };
61}
62
63/**
64 * Decode a color stored in RGB8 format
65 * @param bytes Pointer to encoded source color
66 * @return Result color decoded as Math::Vec4<u8>
67 */
68inline const Math::Vec4<u8> DecodeRGB8(const u8* bytes) {
69 return { bytes[2], bytes[1], bytes[0], 255 };
70}
71
72/**
73 * Decode a color stored in RGB565 format
74 * @param bytes Pointer to encoded source color
75 * @return Result color decoded as Math::Vec4<u8>
76 */
77inline const Math::Vec4<u8> DecodeRGB565(const u8* bytes) {
78 const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes);
79 return { Convert5To8((pixel >> 11) & 0x1F), Convert6To8((pixel >> 5) & 0x3F),
80 Convert5To8(pixel & 0x1F), 255 };
81}
82
83/**
84 * Decode a color stored in RGB5A1 format
85 * @param bytes Pointer to encoded source color
86 * @return Result color decoded as Math::Vec4<u8>
87 */
88inline const Math::Vec4<u8> DecodeRGB5A1(const u8* bytes) {
89 const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes);
90 return { Convert5To8((pixel >> 11) & 0x1F), Convert5To8((pixel >> 6) & 0x1F),
91 Convert5To8((pixel >> 1) & 0x1F), Convert1To8(pixel & 0x1) };
92}
93
94/**
95 * Decode a color stored in RGBA4 format
96 * @param bytes Pointer to encoded source color
97 * @return Result color decoded as Math::Vec4<u8>
98 */
99inline const Math::Vec4<u8> DecodeRGBA4(const u8* bytes) {
100 const u16_le pixel = *reinterpret_cast<const u16_le*>(bytes);
101 return { Convert4To8((pixel >> 12) & 0xF), Convert4To8((pixel >> 8) & 0xF),
102 Convert4To8((pixel >> 4) & 0xF), Convert4To8(pixel & 0xF) };
103}
104
105/**
106 * Decode a depth value stored in D16 format
107 * @param bytes Pointer to encoded source value
108 * @return Depth value as an u32
109 */
110inline u32 DecodeD16(const u8* bytes) {
111 return *reinterpret_cast<const u16_le*>(bytes);
112}
113
114/**
115 * Decode a depth value stored in D24 format
116 * @param bytes Pointer to encoded source value
117 * @return Depth value as an u32
118 */
119inline u32 DecodeD24(const u8* bytes) {
120 return (bytes[2] << 16) | (bytes[1] << 8) | bytes[0];
121}
122
123/**
124 * Decode a depth value and a stencil value stored in D24S8 format
125 * @param bytes Pointer to encoded source values
126 * @return Resulting values stored as a Math::Vec2
127 */
128inline const Math::Vec2<u32> DecodeD24S8(const u8* bytes) {
129 return { static_cast<u32>((bytes[2] << 16) | (bytes[1] << 8) | bytes[0]), bytes[3] };
130}
131
132/**
133 * Encode a color as RGBA8 format
134 * @param color Source color to encode
135 * @param bytes Destination pointer to store encoded color
136 */
137inline void EncodeRGBA8(const Math::Vec4<u8>& color, u8* bytes) {
138 bytes[3] = color.r();
139 bytes[2] = color.g();
140 bytes[1] = color.b();
141 bytes[0] = color.a();
142}
143
144/**
145 * Encode a color as RGB8 format
146 * @param color Source color to encode
147 * @param bytes Destination pointer to store encoded color
148 */
149inline void EncodeRGB8(const Math::Vec4<u8>& color, u8* bytes) {
150 bytes[2] = color.r();
151 bytes[1] = color.g();
152 bytes[0] = color.b();
153}
154
155/**
156 * Encode a color as RGB565 format
157 * @param color Source color to encode
158 * @param bytes Destination pointer to store encoded color
159 */
160inline void EncodeRGB565(const Math::Vec4<u8>& color, u8* bytes) {
161 *reinterpret_cast<u16_le*>(bytes) = (Convert8To5(color.r()) << 11) |
162 (Convert8To6(color.g()) << 5) | Convert8To5(color.b());
163}
164
165/**
166 * Encode a color as RGB5A1 format
167 * @param color Source color to encode
168 * @param bytes Destination pointer to store encoded color
169 */
170inline void EncodeRGB5A1(const Math::Vec4<u8>& color, u8* bytes) {
171 *reinterpret_cast<u16_le*>(bytes) = (Convert8To5(color.r()) << 11) |
172 (Convert8To5(color.g()) << 6) | (Convert8To5(color.b()) << 1) | Convert8To1(color.a());
173}
174
175/**
176 * Encode a color as RGBA4 format
177 * @param color Source color to encode
178 * @param bytes Destination pointer to store encoded color
179 */
180inline void EncodeRGBA4(const Math::Vec4<u8>& color, u8* bytes) {
181 *reinterpret_cast<u16_le*>(bytes) = (Convert8To4(color.r()) << 12) |
182 (Convert8To4(color.g()) << 8) | (Convert8To4(color.b()) << 4) | Convert8To4(color.a());
183}
184
185/**
186 * Encode a 16 bit depth value as D16 format
187 * @param value 16 bit source depth value to encode
188 * @param bytes Pointer where to store the encoded value
189 */
190inline void EncodeD16(u32 value, u8* bytes) {
191 *reinterpret_cast<u16_le*>(bytes) = value & 0xFFFF;
192}
193
194/**
195 * Encode a 24 bit depth value as D24 format
196 * @param value 24 bit source depth value to encode
197 * @param bytes Pointer where to store the encoded value
198 */
199inline void EncodeD24(u32 value, u8* bytes) {
200 bytes[0] = value & 0xFF;
201 bytes[1] = (value >> 8) & 0xFF;
202 bytes[2] = (value >> 16) & 0xFF;
203}
204
205/**
206 * Encode a 24 bit depth and 8 bit stencil values as D24S8 format
207 * @param depth 24 bit source depth value to encode
208 * @param stencil 8 bit source stencil value to encode
209 * @param bytes Pointer where to store the encoded value
210 */
211inline void EncodeD24S8(u32 depth, u8 stencil, u8* bytes) {
212 *reinterpret_cast<u32_le*>(bytes) = (stencil << 24) | depth;
213}
214
215} // namespace
diff --git a/src/video_core/debug_utils/debug_utils.cpp b/src/video_core/debug_utils/debug_utils.cpp
index b92cd1a7e..7b8ab72b6 100644
--- a/src/video_core/debug_utils/debug_utils.cpp
+++ b/src/video_core/debug_utils/debug_utils.cpp
@@ -17,11 +17,11 @@
17#include <nihstro/shader_binary.h> 17#include <nihstro/shader_binary.h>
18 18
19#include "common/assert.h" 19#include "common/assert.h"
20#include "common/color.h"
20#include "common/file_util.h" 21#include "common/file_util.h"
21#include "common/math_util.h" 22#include "common/math_util.h"
23#include "common/vector_math.h"
22 24
23#include "video_core/color.h"
24#include "video_core/math.h"
25#include "video_core/pica.h" 25#include "video_core/pica.h"
26#include "video_core/utils.h" 26#include "video_core/utils.h"
27#include "video_core/video_core.h" 27#include "video_core/video_core.h"
diff --git a/src/video_core/debug_utils/debug_utils.h b/src/video_core/debug_utils/debug_utils.h
index f361a5385..7926d64ec 100644
--- a/src/video_core/debug_utils/debug_utils.h
+++ b/src/video_core/debug_utils/debug_utils.h
@@ -12,7 +12,8 @@
12#include <mutex> 12#include <mutex>
13#include <vector> 13#include <vector>
14 14
15#include "video_core/math.h" 15#include "common/vector_math.h"
16
16#include "video_core/pica.h" 17#include "video_core/pica.h"
17 18
18namespace Pica { 19namespace Pica {
diff --git a/src/video_core/math.h b/src/video_core/math.h
deleted file mode 100644
index 4928c9bf2..000000000
--- a/src/video_core/math.h
+++ /dev/null
@@ -1,640 +0,0 @@
1// Licensed under GPLv2 or any later version
2// Refer to the license.txt file included.
3
4
5// Copyright 2014 Tony Wasserka
6// All rights reserved.
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are met:
10//
11// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// * Neither the name of the owner nor the names of its contributors may
17// be used to endorse or promote products derived from this software
18// without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#pragma once
33
34#include <cmath>
35
36namespace Math {
37
38template<typename T> class Vec2;
39template<typename T> class Vec3;
40template<typename T> class Vec4;
41
42template<typename T>
43static inline Vec2<T> MakeVec(const T& x, const T& y);
44template<typename T>
45static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z);
46template<typename T>
47static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w);
48
49
50template<typename T>
51class Vec2 {
52public:
53 T x;
54 T y;
55
56 T* AsArray() { return &x; }
57
58 Vec2() = default;
59 Vec2(const T a[2]) : x(a[0]), y(a[1]) {}
60 Vec2(const T& _x, const T& _y) : x(_x), y(_y) {}
61
62 template<typename T2>
63 Vec2<T2> Cast() const {
64 return Vec2<T2>((T2)x, (T2)y);
65 }
66
67 static Vec2 AssignToAll(const T& f)
68 {
69 return Vec2<T>(f, f);
70 }
71
72 void Write(T a[2])
73 {
74 a[0] = x; a[1] = y;
75 }
76
77 Vec2<decltype(T{}+T{})> operator +(const Vec2& other) const
78 {
79 return MakeVec(x+other.x, y+other.y);
80 }
81 void operator += (const Vec2 &other)
82 {
83 x+=other.x; y+=other.y;
84 }
85 Vec2<decltype(T{}-T{})> operator -(const Vec2& other) const
86 {
87 return MakeVec(x-other.x, y-other.y);
88 }
89 void operator -= (const Vec2& other)
90 {
91 x-=other.x; y-=other.y;
92 }
93 Vec2<decltype(-T{})> operator -() const
94 {
95 return MakeVec(-x,-y);
96 }
97 Vec2<decltype(T{}*T{})> operator * (const Vec2& other) const
98 {
99 return MakeVec(x*other.x, y*other.y);
100 }
101 template<typename V>
102 Vec2<decltype(T{}*V{})> operator * (const V& f) const
103 {
104 return MakeVec(x*f,y*f);
105 }
106 template<typename V>
107 void operator *= (const V& f)
108 {
109 x*=f; y*=f;
110 }
111 template<typename V>
112 Vec2<decltype(T{}/V{})> operator / (const V& f) const
113 {
114 return MakeVec(x/f,y/f);
115 }
116 template<typename V>
117 void operator /= (const V& f)
118 {
119 *this = *this / f;
120 }
121
122 T Length2() const
123 {
124 return x*x + y*y;
125 }
126
127 // Only implemented for T=float
128 float Length() const;
129 void SetLength(const float l);
130 Vec2 WithLength(const float l) const;
131 float Distance2To(Vec2 &other);
132 Vec2 Normalized() const;
133 float Normalize(); // returns the previous length, which is often useful
134
135 T& operator [] (int i) //allow vector[1] = 3 (vector.y=3)
136 {
137 return *((&x) + i);
138 }
139 T operator [] (const int i) const
140 {
141 return *((&x) + i);
142 }
143
144 void SetZero()
145 {
146 x=0; y=0;
147 }
148
149 // Common aliases: UV (texel coordinates), ST (texture coordinates)
150 T& u() { return x; }
151 T& v() { return y; }
152 T& s() { return x; }
153 T& t() { return y; }
154
155 const T& u() const { return x; }
156 const T& v() const { return y; }
157 const T& s() const { return x; }
158 const T& t() const { return y; }
159
160 // swizzlers - create a subvector of specific components
161 const Vec2 yx() const { return Vec2(y, x); }
162 const Vec2 vu() const { return Vec2(y, x); }
163 const Vec2 ts() const { return Vec2(y, x); }
164};
165
166template<typename T, typename V>
167Vec2<T> operator * (const V& f, const Vec2<T>& vec)
168{
169 return Vec2<T>(f*vec.x,f*vec.y);
170}
171
172typedef Vec2<float> Vec2f;
173
174template<typename T>
175class Vec3
176{
177public:
178 T x;
179 T y;
180 T z;
181
182 T* AsArray() { return &x; }
183
184 Vec3() = default;
185 Vec3(const T a[3]) : x(a[0]), y(a[1]), z(a[2]) {}
186 Vec3(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {}
187
188 template<typename T2>
189 Vec3<T2> Cast() const {
190 return MakeVec<T2>((T2)x, (T2)y, (T2)z);
191 }
192
193 // Only implemented for T=int and T=float
194 static Vec3 FromRGB(unsigned int rgb);
195 unsigned int ToRGB() const; // alpha bits set to zero
196
197 static Vec3 AssignToAll(const T& f)
198 {
199 return MakeVec(f, f, f);
200 }
201
202 void Write(T a[3])
203 {
204 a[0] = x; a[1] = y; a[2] = z;
205 }
206
207 Vec3<decltype(T{}+T{})> operator +(const Vec3 &other) const
208 {
209 return MakeVec(x+other.x, y+other.y, z+other.z);
210 }
211 void operator += (const Vec3 &other)
212 {
213 x+=other.x; y+=other.y; z+=other.z;
214 }
215 Vec3<decltype(T{}-T{})> operator -(const Vec3 &other) const
216 {
217 return MakeVec(x-other.x, y-other.y, z-other.z);
218 }
219 void operator -= (const Vec3 &other)
220 {
221 x-=other.x; y-=other.y; z-=other.z;
222 }
223 Vec3<decltype(-T{})> operator -() const
224 {
225 return MakeVec(-x,-y,-z);
226 }
227 Vec3<decltype(T{}*T{})> operator * (const Vec3 &other) const
228 {
229 return MakeVec(x*other.x, y*other.y, z*other.z);
230 }
231 template<typename V>
232 Vec3<decltype(T{}*V{})> operator * (const V& f) const
233 {
234 return MakeVec(x*f,y*f,z*f);
235 }
236 template<typename V>
237 void operator *= (const V& f)
238 {
239 x*=f; y*=f; z*=f;
240 }
241 template<typename V>
242 Vec3<decltype(T{}/V{})> operator / (const V& f) const
243 {
244 return MakeVec(x/f,y/f,z/f);
245 }
246 template<typename V>
247 void operator /= (const V& f)
248 {
249 *this = *this / f;
250 }
251
252 T Length2() const
253 {
254 return x*x + y*y + z*z;
255 }
256
257 // Only implemented for T=float
258 float Length() const;
259 void SetLength(const float l);
260 Vec3 WithLength(const float l) const;
261 float Distance2To(Vec3 &other);
262 Vec3 Normalized() const;
263 float Normalize(); // returns the previous length, which is often useful
264
265 T& operator [] (int i) //allow vector[2] = 3 (vector.z=3)
266 {
267 return *((&x) + i);
268 }
269 T operator [] (const int i) const
270 {
271 return *((&x) + i);
272 }
273
274 void SetZero()
275 {
276 x=0; y=0; z=0;
277 }
278
279 // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates)
280 T& u() { return x; }
281 T& v() { return y; }
282 T& w() { return z; }
283
284 T& r() { return x; }
285 T& g() { return y; }
286 T& b() { return z; }
287
288 T& s() { return x; }
289 T& t() { return y; }
290 T& q() { return z; }
291
292 const T& u() const { return x; }
293 const T& v() const { return y; }
294 const T& w() const { return z; }
295
296 const T& r() const { return x; }
297 const T& g() const { return y; }
298 const T& b() const { return z; }
299
300 const T& s() const { return x; }
301 const T& t() const { return y; }
302 const T& q() const { return z; }
303
304 // swizzlers - create a subvector of specific components
305 // e.g. Vec2 uv() { return Vec2(x,y); }
306 // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all component names (x<->r) and permutations (xy<->yx)
307#define _DEFINE_SWIZZLER2(a, b, name) const Vec2<T> name() const { return Vec2<T>(a, b); }
308#define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \
309 _DEFINE_SWIZZLER2(a, b, a##b); \
310 _DEFINE_SWIZZLER2(a, b, a2##b2); \
311 _DEFINE_SWIZZLER2(a, b, a3##b3); \
312 _DEFINE_SWIZZLER2(a, b, a4##b4); \
313 _DEFINE_SWIZZLER2(b, a, b##a); \
314 _DEFINE_SWIZZLER2(b, a, b2##a2); \
315 _DEFINE_SWIZZLER2(b, a, b3##a3); \
316 _DEFINE_SWIZZLER2(b, a, b4##a4)
317
318 DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t);
319 DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q);
320 DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q);
321#undef DEFINE_SWIZZLER2
322#undef _DEFINE_SWIZZLER2
323};
324
325template<typename T, typename V>
326Vec3<T> operator * (const V& f, const Vec3<T>& vec)
327{
328 return Vec3<T>(f*vec.x,f*vec.y,f*vec.z);
329}
330
331template<>
332inline float Vec3<float>::Length() const {
333 return std::sqrt(x * x + y * y + z * z);
334}
335
336template<>
337inline Vec3<float> Vec3<float>::Normalized() const {
338 return *this / Length();
339}
340
341
342typedef Vec3<float> Vec3f;
343
344template<typename T>
345class Vec4
346{
347public:
348 T x;
349 T y;
350 T z;
351 T w;
352
353 T* AsArray() { return &x; }
354
355 Vec4() = default;
356 Vec4(const T a[4]) : x(a[0]), y(a[1]), z(a[2]), w(a[3]) {}
357 Vec4(const T& _x, const T& _y, const T& _z, const T& _w) : x(_x), y(_y), z(_z), w(_w) {}
358
359 template<typename T2>
360 Vec4<T2> Cast() const {
361 return Vec4<T2>((T2)x, (T2)y, (T2)z, (T2)w);
362 }
363
364 // Only implemented for T=int and T=float
365 static Vec4 FromRGBA(unsigned int rgba);
366 unsigned int ToRGBA() const;
367
368 static Vec4 AssignToAll(const T& f) {
369 return Vec4<T>(f, f, f, f);
370 }
371
372 void Write(T a[4])
373 {
374 a[0] = x; a[1] = y; a[2] = z; a[3] = w;
375 }
376
377 Vec4<decltype(T{}+T{})> operator +(const Vec4& other) const
378 {
379 return MakeVec(x+other.x, y+other.y, z+other.z, w+other.w);
380 }
381 void operator += (const Vec4& other)
382 {
383 x+=other.x; y+=other.y; z+=other.z; w+=other.w;
384 }
385 Vec4<decltype(T{}-T{})> operator -(const Vec4 &other) const
386 {
387 return MakeVec(x-other.x, y-other.y, z-other.z, w-other.w);
388 }
389 void operator -= (const Vec4 &other)
390 {
391 x-=other.x; y-=other.y; z-=other.z; w-=other.w;
392 }
393 Vec4<decltype(-T{})> operator -() const
394 {
395 return MakeVec(-x,-y,-z,-w);
396 }
397 Vec4<decltype(T{}*T{})> operator * (const Vec4 &other) const
398 {
399 return MakeVec(x*other.x, y*other.y, z*other.z, w*other.w);
400 }
401 template<typename V>
402 Vec4<decltype(T{}*V{})> operator * (const V& f) const
403 {
404 return MakeVec(x*f,y*f,z*f,w*f);
405 }
406 template<typename V>
407 void operator *= (const V& f)
408 {
409 x*=f; y*=f; z*=f; w*=f;
410 }
411 template<typename V>
412 Vec4<decltype(T{}/V{})> operator / (const V& f) const
413 {
414 return MakeVec(x/f,y/f,z/f,w/f);
415 }
416 template<typename V>
417 void operator /= (const V& f)
418 {
419 *this = *this / f;
420 }
421
422 T Length2() const
423 {
424 return x*x + y*y + z*z + w*w;
425 }
426
427 // Only implemented for T=float
428 float Length() const;
429 void SetLength(const float l);
430 Vec4 WithLength(const float l) const;
431 float Distance2To(Vec4 &other);
432 Vec4 Normalized() const;
433 float Normalize(); // returns the previous length, which is often useful
434
435 T& operator [] (int i) //allow vector[2] = 3 (vector.z=3)
436 {
437 return *((&x) + i);
438 }
439 T operator [] (const int i) const
440 {
441 return *((&x) + i);
442 }
443
444 void SetZero()
445 {
446 x=0; y=0; z=0;
447 }
448
449 // Common alias: RGBA (colors)
450 T& r() { return x; }
451 T& g() { return y; }
452 T& b() { return z; }
453 T& a() { return w; }
454
455 const T& r() const { return x; }
456 const T& g() const { return y; }
457 const T& b() const { return z; }
458 const T& a() const { return w; }
459
460 // Swizzlers - Create a subvector of specific components
461 // e.g. Vec2 uv() { return Vec2(x,y); }
462
463 // _DEFINE_SWIZZLER2 defines a single such function
464 // DEFINE_SWIZZLER2_COMP1 defines one-component functions for all component names (x<->r)
465 // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and permutations (xy<->yx)
466#define _DEFINE_SWIZZLER2(a, b, name) const Vec2<T> name() const { return Vec2<T>(a, b); }
467#define DEFINE_SWIZZLER2_COMP1(a, a2) \
468 _DEFINE_SWIZZLER2(a, a, a##a); \
469 _DEFINE_SWIZZLER2(a, a, a2##a2)
470#define DEFINE_SWIZZLER2_COMP2(a, b, a2, b2) \
471 _DEFINE_SWIZZLER2(a, b, a##b); \
472 _DEFINE_SWIZZLER2(a, b, a2##b2); \
473 _DEFINE_SWIZZLER2(b, a, b##a); \
474 _DEFINE_SWIZZLER2(b, a, b2##a2)
475
476 DEFINE_SWIZZLER2_COMP2(x, y, r, g);
477 DEFINE_SWIZZLER2_COMP2(x, z, r, b);
478 DEFINE_SWIZZLER2_COMP2(x, w, r, a);
479 DEFINE_SWIZZLER2_COMP2(y, z, g, b);
480 DEFINE_SWIZZLER2_COMP2(y, w, g, a);
481 DEFINE_SWIZZLER2_COMP2(z, w, b, a);
482 DEFINE_SWIZZLER2_COMP1(x, r);
483 DEFINE_SWIZZLER2_COMP1(y, g);
484 DEFINE_SWIZZLER2_COMP1(z, b);
485 DEFINE_SWIZZLER2_COMP1(w, a);
486#undef DEFINE_SWIZZLER2_COMP1
487#undef DEFINE_SWIZZLER2_COMP2
488#undef _DEFINE_SWIZZLER2
489
490#define _DEFINE_SWIZZLER3(a, b, c, name) const Vec3<T> name() const { return Vec3<T>(a, b, c); }
491#define DEFINE_SWIZZLER3_COMP1(a, a2) \
492 _DEFINE_SWIZZLER3(a, a, a, a##a##a); \
493 _DEFINE_SWIZZLER3(a, a, a, a2##a2##a2)
494#define DEFINE_SWIZZLER3_COMP3(a, b, c, a2, b2, c2) \
495 _DEFINE_SWIZZLER3(a, b, c, a##b##c); \
496 _DEFINE_SWIZZLER3(a, c, b, a##c##b); \
497 _DEFINE_SWIZZLER3(b, a, c, b##a##c); \
498 _DEFINE_SWIZZLER3(b, c, a, b##c##a); \
499 _DEFINE_SWIZZLER3(c, a, b, c##a##b); \
500 _DEFINE_SWIZZLER3(c, b, a, c##b##a); \
501 _DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \
502 _DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \
503 _DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \
504 _DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \
505 _DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \
506 _DEFINE_SWIZZLER3(c, b, a, c2##b2##a2)
507
508 DEFINE_SWIZZLER3_COMP3(x, y, z, r, g, b);
509 DEFINE_SWIZZLER3_COMP3(x, y, w, r, g, a);
510 DEFINE_SWIZZLER3_COMP3(x, z, w, r, b, a);
511 DEFINE_SWIZZLER3_COMP3(y, z, w, g, b, a);
512 DEFINE_SWIZZLER3_COMP1(x, r);
513 DEFINE_SWIZZLER3_COMP1(y, g);
514 DEFINE_SWIZZLER3_COMP1(z, b);
515 DEFINE_SWIZZLER3_COMP1(w, a);
516#undef DEFINE_SWIZZLER3_COMP1
517#undef DEFINE_SWIZZLER3_COMP3
518#undef _DEFINE_SWIZZLER3
519};
520
521
522template<typename T, typename V>
523Vec4<decltype(V{}*T{})> operator * (const V& f, const Vec4<T>& vec)
524{
525 return MakeVec(f*vec.x,f*vec.y,f*vec.z,f*vec.w);
526}
527
528typedef Vec4<float> Vec4f;
529
530
531template<typename T>
532static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec2<T>& a, const Vec2<T>& b)
533{
534 return a.x*b.x + a.y*b.y;
535}
536
537template<typename T>
538static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec3<T>& a, const Vec3<T>& b)
539{
540 return a.x*b.x + a.y*b.y + a.z*b.z;
541}
542
543template<typename T>
544static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec4<T>& a, const Vec4<T>& b)
545{
546 return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
547}
548
549template<typename T>
550static inline Vec3<decltype(T{}*T{}-T{}*T{})> Cross(const Vec3<T>& a, const Vec3<T>& b)
551{
552 return MakeVec(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
553}
554
555// linear interpolation via float: 0.0=begin, 1.0=end
556template<typename X>
557static inline decltype(X{}*float{}+X{}*float{}) Lerp(const X& begin, const X& end, const float t)
558{
559 return begin*(1.f-t) + end*t;
560}
561
562// linear interpolation via int: 0=begin, base=end
563template<typename X, int base>
564static inline decltype((X{}*int{}+X{}*int{}) / base) LerpInt(const X& begin, const X& end, const int t)
565{
566 return (begin*(base-t) + end*t) / base;
567}
568
569// Utility vector factories
570template<typename T>
571static inline Vec2<T> MakeVec(const T& x, const T& y)
572{
573 return Vec2<T>{x, y};
574}
575
576template<typename T>
577static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z)
578{
579 return Vec3<T>{x, y, z};
580}
581
582template<typename T>
583static inline Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw)
584{
585 return MakeVec(x, y, zw[0], zw[1]);
586}
587
588template<typename T>
589static inline Vec3<T> MakeVec(const Vec2<T>& xy, const T& z)
590{
591 return MakeVec(xy[0], xy[1], z);
592}
593
594template<typename T>
595static inline Vec3<T> MakeVec(const T& x, const Vec2<T>& yz)
596{
597 return MakeVec(x, yz[0], yz[1]);
598}
599
600template<typename T>
601static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w)
602{
603 return Vec4<T>{x, y, z, w};
604}
605
606template<typename T>
607static inline Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w)
608{
609 return MakeVec(xy[0], xy[1], z, w);
610}
611
612template<typename T>
613static inline Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w)
614{
615 return MakeVec(x, yz[0], yz[1], w);
616}
617
618// NOTE: This has priority over "Vec2<Vec2<T>> MakeVec(const Vec2<T>& x, const Vec2<T>& y)".
619// Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error
620// out soon enough due to misuse of the returned structure.
621template<typename T>
622static inline Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw)
623{
624 return MakeVec(xy[0], xy[1], zw[0], zw[1]);
625}
626
627template<typename T>
628static inline Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w)
629{
630 return MakeVec(xyz[0], xyz[1], xyz[2], w);
631}
632
633template<typename T>
634static inline Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw)
635{
636 return MakeVec(x, yzw[0], yzw[1], yzw[2]);
637}
638
639
640} // namespace
diff --git a/src/video_core/pica.h b/src/video_core/pica.h
index 8ad47a928..684ec9818 100644
--- a/src/video_core/pica.h
+++ b/src/video_core/pica.h
@@ -15,8 +15,7 @@
15#include "common/common_funcs.h" 15#include "common/common_funcs.h"
16#include "common/common_types.h" 16#include "common/common_types.h"
17#include "common/logging/log.h" 17#include "common/logging/log.h"
18 18#include "common/vector_math.h"
19#include "math.h"
20 19
21namespace Pica { 20namespace Pica {
22 21
diff --git a/src/video_core/rasterizer.cpp b/src/video_core/rasterizer.cpp
index 113b573f8..59d156ee7 100644
--- a/src/video_core/rasterizer.cpp
+++ b/src/video_core/rasterizer.cpp
@@ -4,6 +4,7 @@
4 4
5#include <algorithm> 5#include <algorithm>
6 6
7#include "common/color.h"
7#include "common/common_types.h" 8#include "common/common_types.h"
8#include "common/math_util.h" 9#include "common/math_util.h"
9#include "common/profiler.h" 10#include "common/profiler.h"
@@ -13,7 +14,6 @@
13 14
14#include "debug_utils/debug_utils.h" 15#include "debug_utils/debug_utils.h"
15#include "math.h" 16#include "math.h"
16#include "color.h"
17#include "pica.h" 17#include "pica.h"
18#include "rasterizer.h" 18#include "rasterizer.h"
19#include "vertex_shader.h" 19#include "vertex_shader.h"
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index b51f8efdf..d31c46cca 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -2,10 +2,11 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "common/color.h"
6
5#include "core/settings.h" 7#include "core/settings.h"
6#include "core/hw/gpu.h" 8#include "core/hw/gpu.h"
7 9
8#include "video_core/color.h"
9#include "video_core/pica.h" 10#include "video_core/pica.h"
10#include "video_core/utils.h" 11#include "video_core/utils.h"
11#include "video_core/renderer_opengl/gl_rasterizer.h" 12#include "video_core/renderer_opengl/gl_rasterizer.h"
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
index 6f88a8b21..2e4110a88 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
@@ -4,13 +4,13 @@
4 4
5#include "common/make_unique.h" 5#include "common/make_unique.h"
6#include "common/math_util.h" 6#include "common/math_util.h"
7#include "common/vector_math.h"
7 8
8#include "core/memory.h" 9#include "core/memory.h"
9 10
10#include "video_core/renderer_opengl/gl_rasterizer_cache.h" 11#include "video_core/renderer_opengl/gl_rasterizer_cache.h"
11#include "video_core/renderer_opengl/pica_to_gl.h" 12#include "video_core/renderer_opengl/pica_to_gl.h"
12#include "video_core/debug_utils/debug_utils.h" 13#include "video_core/debug_utils/debug_utils.h"
13#include "video_core/math.h"
14 14
15RasterizerCacheOpenGL::~RasterizerCacheOpenGL() { 15RasterizerCacheOpenGL::~RasterizerCacheOpenGL() {
16 FullFlush(); 16 FullFlush();