summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/video_core/CMakeLists.txt3
-rw-r--r--src/video_core/math.h578
-rw-r--r--src/video_core/video_core.vcxproj1
-rw-r--r--src/video_core/video_core.vcxproj.filters1
4 files changed, 582 insertions, 1 deletions
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt
index e43e6e1bb..2503b9d18 100644
--- a/src/video_core/CMakeLists.txt
+++ b/src/video_core/CMakeLists.txt
@@ -2,8 +2,9 @@ set(SRCS video_core.cpp
2 utils.cpp 2 utils.cpp
3 renderer_opengl/renderer_opengl.cpp) 3 renderer_opengl/renderer_opengl.cpp)
4 4
5set(HEADERS video_core.h 5set(HEADERS math.h
6 utils.h 6 utils.h
7 video_core.h
7 renderer_base.h 8 renderer_base.h
8 renderer_opengl/renderer_opengl.h) 9 renderer_opengl/renderer_opengl.h)
9 10
diff --git a/src/video_core/math.h b/src/video_core/math.h
new file mode 100644
index 000000000..7030f2cfb
--- /dev/null
+++ b/src/video_core/math.h
@@ -0,0 +1,578 @@
1// Licensed under GPLv2
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
42
43template<typename T>
44class Vec2 {
45public:
46 struct {
47 T x,y;
48 };
49
50 T* AsArray() { return &x; }
51
52 Vec2() = default;
53 Vec2(const T a[2]) : x(a[0]), y(a[1]) {}
54 Vec2(const T& _x, const T& _y) : x(_x), y(_y) {}
55
56 template<typename T2>
57 Vec2<T2> Cast() const {
58 return Vec2<T2>((T2)x, (T2)y);
59 }
60
61 static Vec2 AssignToAll(const T& f)
62 {
63 return Vec2<T>(f, f);
64 }
65
66 void Write(T a[2])
67 {
68 a[0] = x; a[1] = y;
69 }
70
71 Vec2 operator +(const Vec2& other) const
72 {
73 return Vec2(x+other.x, y+other.y);
74 }
75 void operator += (const Vec2 &other)
76 {
77 x+=other.x; y+=other.y;
78 }
79 Vec2 operator -(const Vec2& other) const
80 {
81 return Vec2(x-other.x, y-other.y);
82 }
83 void operator -= (const Vec2& other)
84 {
85 x-=other.x; y-=other.y;
86 }
87 Vec2 operator -() const
88 {
89 return Vec2(-x,-y);
90 }
91 Vec2 operator * (const Vec2& other) const
92 {
93 return Vec2(x*other.x, y*other.y);
94 }
95 template<typename V>
96 Vec2 operator * (const V& f) const
97 {
98 return Vec2(x*f,y*f);
99 }
100 template<typename V>
101 void operator *= (const V& f)
102 {
103 x*=f; y*=f;
104 }
105 template<typename V>
106 Vec2 operator / (const V& f) const
107 {
108 return Vec2(x/f,y/f);
109 }
110 template<typename V>
111 void operator /= (const V& f)
112 {
113 *this = *this / f;
114 }
115
116 T Length2() const
117 {
118 return x*x + y*y;
119 }
120
121 // Only implemented for T=float
122 float Length() const;
123 void SetLength(const float l);
124 Vec2 WithLength(const float l) const;
125 float Distance2To(Vec2 &other);
126 Vec2 Normalized() const;
127 float Normalize(); // returns the previous length, which is often useful
128
129 T& operator [] (int i) //allow vector[1] = 3 (vector.y=3)
130 {
131 return *((&x) + i);
132 }
133 T operator [] (const int i) const
134 {
135 return *((&x) + i);
136 }
137
138 void SetZero()
139 {
140 x=0; y=0;
141 }
142
143 // Common aliases: UV (texel coordinates), ST (texture coordinates)
144 T& u() { return x; }
145 T& v() { return y; }
146 T& s() { return x; }
147 T& t() { return y; }
148
149 const T& u() const { return x; }
150 const T& v() const { return y; }
151 const T& s() const { return x; }
152 const T& t() const { return y; }
153
154 // swizzlers - create a subvector of specific components
155 Vec2 yx() const { return Vec2(y, x); }
156 Vec2 vu() const { return Vec2(y, x); }
157 Vec2 ts() const { return Vec2(y, x); }
158
159 // Inserters to add new elements to effectively create larger vectors containing this Vec2
160 Vec3<T> InsertBeforeX(const T& value) {
161 return Vec3<T>(value, x, y);
162 }
163 Vec3<T> InsertBeforeY(const T& value) {
164 return Vec3<T>(x, value, y);
165 }
166 Vec3<T> Append(const T& value) {
167 return Vec3<T>(x, y, value);
168 }
169};
170
171template<typename T, typename V>
172Vec2<T> operator * (const V& f, const Vec2<T>& vec)
173{
174 return Vec2<T>(f*vec.x,f*vec.y);
175}
176
177typedef Vec2<float> Vec2f;
178
179template<typename T>
180class Vec3
181{
182public:
183 struct
184 {
185 T x,y,z;
186 };
187
188 T* AsArray() { return &x; }
189
190 Vec3() = default;
191 Vec3(const T a[3]) : x(a[0]), y(a[1]), z(a[2]) {}
192 Vec3(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {}
193
194 template<typename T2>
195 Vec3<T2> Cast() const {
196 return Vec3<T2>((T2)x, (T2)y, (T2)z);
197 }
198
199 // Only implemented for T=int and T=float
200 static Vec3 FromRGB(unsigned int rgb);
201 unsigned int ToRGB() const; // alpha bits set to zero
202
203 static Vec3 AssignToAll(const T& f)
204 {
205 return Vec3<T>(f, f, f);
206 }
207
208 void Write(T a[3])
209 {
210 a[0] = x; a[1] = y; a[2] = z;
211 }
212
213 Vec3 operator +(const Vec3 &other) const
214 {
215 return Vec3(x+other.x, y+other.y, z+other.z);
216 }
217 void operator += (const Vec3 &other)
218 {
219 x+=other.x; y+=other.y; z+=other.z;
220 }
221 Vec3 operator -(const Vec3 &other) const
222 {
223 return Vec3(x-other.x, y-other.y, z-other.z);
224 }
225 void operator -= (const Vec3 &other)
226 {
227 x-=other.x; y-=other.y; z-=other.z;
228 }
229 Vec3 operator -() const
230 {
231 return Vec3(-x,-y,-z);
232 }
233 Vec3 operator * (const Vec3 &other) const
234 {
235 return Vec3(x*other.x, y*other.y, z*other.z);
236 }
237 template<typename V>
238 Vec3 operator * (const V& f) const
239 {
240 return Vec3(x*f,y*f,z*f);
241 }
242 template<typename V>
243 void operator *= (const V& f)
244 {
245 x*=f; y*=f; z*=f;
246 }
247 template<typename V>
248 Vec3 operator / (const V& f) const
249 {
250 return Vec3(x/f,y/f,z/f);
251 }
252 template<typename V>
253 void operator /= (const V& f)
254 {
255 *this = *this / f;
256 }
257
258 T Length2() const
259 {
260 return x*x + y*y + z*z;
261 }
262
263 // Only implemented for T=float
264 float Length() const;
265 void SetLength(const float l);
266 Vec3 WithLength(const float l) const;
267 float Distance2To(Vec3 &other);
268 Vec3 Normalized() const;
269 float Normalize(); // returns the previous length, which is often useful
270
271 T& operator [] (int i) //allow vector[2] = 3 (vector.z=3)
272 {
273 return *((&x) + i);
274 }
275 T operator [] (const int i) const
276 {
277 return *((&x) + i);
278 }
279
280 void SetZero()
281 {
282 x=0; y=0; z=0;
283 }
284
285 // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates)
286 T& u() { return x; }
287 T& v() { return y; }
288 T& w() { return z; }
289
290 T& r() { return x; }
291 T& g() { return y; }
292 T& b() { return z; }
293
294 T& s() { return x; }
295 T& t() { return y; }
296 T& q() { return z; }
297
298 const T& u() const { return x; }
299 const T& v() const { return y; }
300 const T& w() const { return z; }
301
302 const T& r() const { return x; }
303 const T& g() const { return y; }
304 const T& b() const { return z; }
305
306 const T& s() const { return x; }
307 const T& t() const { return y; }
308 const T& q() const { return z; }
309
310 // swizzlers - create a subvector of specific components
311 // e.g. Vec2 uv() { return Vec2(x,y); }
312 // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all component names (x<->r) and permutations (xy<->yx)
313#define _DEFINE_SWIZZLER2(a, b, name) Vec2<T> name() const { return Vec2<T>(a, b); }
314#define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \
315 _DEFINE_SWIZZLER2(a, b, a##b); \
316 _DEFINE_SWIZZLER2(a, b, a2##b2); \
317 _DEFINE_SWIZZLER2(a, b, a3##b3); \
318 _DEFINE_SWIZZLER2(a, b, a4##b4); \
319 _DEFINE_SWIZZLER2(b, a, b##a); \
320 _DEFINE_SWIZZLER2(b, a, b2##a2); \
321 _DEFINE_SWIZZLER2(b, a, b3##a3); \
322 _DEFINE_SWIZZLER2(b, a, b4##a4);
323
324 DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t);
325 DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q);
326 DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q);
327#undef DEFINE_SWIZZLER2
328#undef _DEFINE_SWIZZLER2
329
330 // Inserters to add new elements to effectively create larger vectors containing this Vec2
331 Vec4<T> InsertBeforeX(const T& value) {
332 return Vec4<T>(value, x, y, z);
333 }
334 Vec4<T> InsertBeforeY(const T& value) {
335 return Vec4<T>(x, value, y, z);
336 }
337 Vec4<T> InsertBeforeZ(const T& value) {
338 return Vec4<T>(x, y, value, z);
339 }
340 Vec4<T> Append(const T& value) {
341 return Vec4<T>(x, y, z, value);
342 }
343};
344
345template<typename T, typename V>
346Vec3<T> operator * (const V& f, const Vec3<T>& vec)
347{
348 return Vec3<T>(f*vec.x,f*vec.y,f*vec.z);
349}
350
351typedef Vec3<float> Vec3f;
352
353template<typename T>
354class Vec4
355{
356public:
357 struct
358 {
359 T x,y,z,w;
360 };
361
362 T* AsArray() { return &x; }
363
364 Vec4() = default;
365 Vec4(const T a[4]) : x(a[0]), y(a[1]), z(a[2]), w(a[3]) {}
366 Vec4(const T& _x, const T& _y, const T& _z, const T& _w) : x(_x), y(_y), z(_z), w(_w) {}
367
368 template<typename T2>
369 Vec4<T2> Cast() const {
370 return Vec4<T2>((T2)x, (T2)y, (T2)z, (T2)w);
371 }
372
373 // Only implemented for T=int and T=float
374 static Vec4 FromRGBA(unsigned int rgba);
375 unsigned int ToRGBA() const;
376
377 static Vec4 AssignToAll(const T& f) {
378 return Vec4<T>(f, f, f, f);
379 }
380
381 void Write(T a[4])
382 {
383 a[0] = x; a[1] = y; a[2] = z; a[3] = w;
384 }
385
386 Vec4 operator +(const Vec4& other) const
387 {
388 return Vec4(x+other.x, y+other.y, z+other.z, w+other.w);
389 }
390 void operator += (const Vec4& other)
391 {
392 x+=other.x; y+=other.y; z+=other.z; w+=other.w;
393 }
394 Vec4 operator -(const Vec4 &other) const
395 {
396 return Vec4(x-other.x, y-other.y, z-other.z, w-other.w);
397 }
398 void operator -= (const Vec4 &other)
399 {
400 x-=other.x; y-=other.y; z-=other.z; w-=other.w;
401 }
402 Vec4 operator -() const
403 {
404 return Vec4(-x,-y,-z,-w);
405 }
406 Vec4 operator * (const Vec4 &other) const
407 {
408 return Vec4(x*other.x, y*other.y, z*other.z, w*other.w);
409 }
410 template<typename V>
411 Vec4 operator * (const V& f) const
412 {
413 return Vec4(x*f,y*f,z*f,w*f);
414 }
415 template<typename V>
416 void operator *= (const V& f)
417 {
418 x*=f; y*=f; z*=f; w*=f;
419 }
420 template<typename V>
421 Vec4 operator / (const V& f) const
422 {
423 return Vec4(x/f,y/f,z/f,w/f);
424 }
425 template<typename V>
426 void operator /= (const V& f)
427 {
428 *this = *this / f;
429 }
430
431 T Length2() const
432 {
433 return x*x + y*y + z*z + w*w;
434 }
435
436 // Only implemented for T=float
437 float Length() const;
438 void SetLength(const float l);
439 Vec4 WithLength(const float l) const;
440 float Distance2To(Vec4 &other);
441 Vec4 Normalized() const;
442 float Normalize(); // returns the previous length, which is often useful
443
444 T& operator [] (int i) //allow vector[2] = 3 (vector.z=3)
445 {
446 return *((&x) + i);
447 }
448 T operator [] (const int i) const
449 {
450 return *((&x) + i);
451 }
452
453 void SetZero()
454 {
455 x=0; y=0; z=0;
456 }
457
458 // Common alias: RGBA (colors)
459 T& r() { return x; }
460 T& g() { return y; }
461 T& b() { return z; }
462 T& a() { return w; }
463
464 const T& r() const { return x; }
465 const T& g() const { return y; }
466 const T& b() const { return z; }
467 const T& a() const { return w; }
468
469 // swizzlers - create a subvector of specific components
470 // e.g. Vec2 uv() { return Vec2(x,y); }
471 // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all component names (x<->r) and permutations (xy<->yx)
472#define _DEFINE_SWIZZLER2(a, b, name) Vec2<T> name() const { return Vec2<T>(a, b); }
473#define DEFINE_SWIZZLER2(a, b, a2, b2) \
474 _DEFINE_SWIZZLER2(a, b, a##b); \
475 _DEFINE_SWIZZLER2(a, b, a2##b2); \
476 _DEFINE_SWIZZLER2(b, a, b##a); \
477 _DEFINE_SWIZZLER2(b, a, b2##a2);
478
479 DEFINE_SWIZZLER2(x, y, r, g);
480 DEFINE_SWIZZLER2(x, z, r, b);
481 DEFINE_SWIZZLER2(x, w, r, a);
482 DEFINE_SWIZZLER2(y, z, g, b);
483 DEFINE_SWIZZLER2(y, w, g, a);
484 DEFINE_SWIZZLER2(z, w, b, a);
485#undef DEFINE_SWIZZLER2
486#undef _DEFINE_SWIZZLER2
487
488#define _DEFINE_SWIZZLER3(a, b, c, name) Vec3<T> name() const { return Vec3<T>(a, b, c); }
489#define DEFINE_SWIZZLER3(a, b, c, a2, b2, c2) \
490 _DEFINE_SWIZZLER3(a, b, c, a##b##c); \
491 _DEFINE_SWIZZLER3(a, c, b, a##c##b); \
492 _DEFINE_SWIZZLER3(b, a, c, b##a##c); \
493 _DEFINE_SWIZZLER3(b, c, a, b##c##a); \
494 _DEFINE_SWIZZLER3(c, a, b, c##a##b); \
495 _DEFINE_SWIZZLER3(c, b, a, c##b##a); \
496 _DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \
497 _DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \
498 _DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \
499 _DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \
500 _DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \
501 _DEFINE_SWIZZLER3(c, b, a, c2##b2##a2);
502
503 DEFINE_SWIZZLER3(x, y, z, r, g, b);
504 DEFINE_SWIZZLER3(x, y, w, r, g, a);
505 DEFINE_SWIZZLER3(x, z, w, r, b, a);
506 DEFINE_SWIZZLER3(y, z, w, g, b, a);
507#undef DEFINE_SWIZZLER3
508#undef _DEFINE_SWIZZLER3
509};
510
511
512template<typename T, typename V>
513Vec4<T> operator * (const V& f, const Vec4<T>& vec)
514{
515 return Vec4<T>(f*vec.x,f*vec.y,f*vec.z,f*vec.w);
516}
517
518typedef Vec4<float> Vec4f;
519
520
521template<typename T>
522static inline T Dot(const Vec2<T>& a, const Vec2<T>& b)
523{
524 return a.x*b.x + a.y*b.y;
525}
526
527template<typename T>
528static inline T Dot(const Vec3<T>& a, const Vec3<T>& b)
529{
530 return a.x*b.x + a.y*b.y + a.z*b.z;
531}
532
533template<typename T>
534static inline T Dot(const Vec4<T>& a, const Vec4<T>& b)
535{
536 return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
537}
538
539template<typename T>
540static inline Vec3<T> Cross(const Vec3<T>& a, const Vec3<T>& b)
541{
542 return Vec3<T>(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
543}
544
545// linear interpolation via float: 0.0=begin, 1.0=end
546template<typename X>
547static inline X Lerp(const X& begin, const X& end, const float t)
548{
549 return begin*(1.f-t) + end*t;
550}
551
552// linear interpolation via int: 0=begin, base=end
553template<typename X, int base>
554static inline X LerpInt(const X& begin, const X& end, const int t)
555{
556 return (begin*(base-t) + end*t) / base;
557}
558
559// Utility vector factories
560template<typename T>
561static inline Vec2<T> MakeVec2(const T& x, const T& y)
562{
563 return Vec2<T>{x, y};
564}
565
566template<typename T>
567static inline Vec3<T> MakeVec3(const T& x, const T& y, const T& z)
568{
569 return Vec3<T>{x, y, z};
570}
571
572template<typename T>
573static inline Vec4<T> MakeVec4(const T& x, const T& y, const T& z, const T& w)
574{
575 return Vec4<T>{x, y, z, w};
576}
577
578} // namespace
diff --git a/src/video_core/video_core.vcxproj b/src/video_core/video_core.vcxproj
index d77be2bef..2dbfc68dd 100644
--- a/src/video_core/video_core.vcxproj
+++ b/src/video_core/video_core.vcxproj
@@ -25,6 +25,7 @@
25 </ItemGroup> 25 </ItemGroup>
26 <ItemGroup> 26 <ItemGroup>
27 <ClInclude Include="gpu_debugger.h" /> 27 <ClInclude Include="gpu_debugger.h" />
28 <ClInclude Include="math.h" />
28 <ClInclude Include="pica.h" /> 29 <ClInclude Include="pica.h" />
29 <ClInclude Include="renderer_base.h" /> 30 <ClInclude Include="renderer_base.h" />
30 <ClInclude Include="utils.h" /> 31 <ClInclude Include="utils.h" />
diff --git a/src/video_core/video_core.vcxproj.filters b/src/video_core/video_core.vcxproj.filters
index b89ac1ac4..b42823d2a 100644
--- a/src/video_core/video_core.vcxproj.filters
+++ b/src/video_core/video_core.vcxproj.filters
@@ -17,6 +17,7 @@
17 <Filter>renderer_opengl</Filter> 17 <Filter>renderer_opengl</Filter>
18 </ClInclude> 18 </ClInclude>
19 <ClInclude Include="gpu_debugger.h" /> 19 <ClInclude Include="gpu_debugger.h" />
20 <ClInclude Include="math.h" />
20 <ClInclude Include="pica.h" /> 21 <ClInclude Include="pica.h" />
21 <ClInclude Include="renderer_base.h" /> 22 <ClInclude Include="renderer_base.h" />
22 <ClInclude Include="utils.h" /> 23 <ClInclude Include="utils.h" />