/[pcsx2_0.9.7]/trunk/plugins/zzogl-pg/opengl/ZZoglMath.h
ViewVC logotype

Contents of /trunk/plugins/zzogl-pg/opengl/ZZoglMath.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download)
Thu Dec 23 12:02:12 2010 UTC (9 years, 2 months ago) by william
File MIME type: text/plain
File size: 10258 byte(s)
re-commit (had local access denied errors when committing)
1 /* ZZ Open GL graphics plugin
2 * Copyright (c)2009-2010 zeydlitz@gmail.com, arcum42@gmail.com
3 * Based on Zerofrog's ZeroGS KOSMOS (c)2005-2008
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #ifndef ZZOGLMATH_H_INCLUDED
21 #define ZZOGLMATH_H_INCLUDED
22
23 //Remind me to check and see if this is necessary, and what uses it. --arcum42
24 #ifndef _WIN32
25 #include <alloca.h>
26 #endif
27
28 #include <assert.h>
29 #include "Pcsx2Defs.h"
30
31 //#define ZZ_MMATH
32
33 #ifndef ZZ_MMATH
34
35 template <class T>
36 class Vector4
37 {
38 public:
39 T x, y, z, w;
40
41 Vector4(T x1 = 0, T y1 = 0, T z1 = 0, T w1 = 0)
42 {
43 x = x1;
44 y = y1;
45 z = z1;
46 w = w1;
47 }
48
49 Vector4(Vector4<T> &f)
50 {
51 x = f.x;
52 y = f.y;
53 z = f.z;
54 w = f.w;
55 }
56
57 Vector4(T* f)
58 {
59 x = f[0];
60 y = f[1];
61 z = f[2];
62 w = f[3]; // For some reason, the old code set this to 0.
63 }
64
65 T& operator[](int i)
66 {
67 switch(i)
68 {
69 case 0: return x;
70 case 1: return y;
71 case 2: return z;
72 case 3: return w;
73 default: assert(0);
74 }
75 }
76
77 operator T*()
78 {
79 return (T*) this;
80 }
81
82 operator const T*() const
83 {
84 return (const T*) this;
85 }
86
87 Vector4<T>& operator =(const Vector4<T>& v)
88 {
89 x = v.x;
90 y = v.y;
91 z = v.z;
92 w = v.w;
93 return *this;
94 }
95
96 bool operator ==(const Vector4<T>& v)
97 {
98 return !!( x == v.x &&
99 y == v.y &&
100 z == v.z &&
101 w == v.w );
102 }
103
104 Vector4<T> operator +(const Vector4<T>& v) const
105 {
106 return Vector4<T>(x + v.x, y + v.y, z + v.z, w + v.w);
107 }
108
109 Vector4<T> operator -(const Vector4<T>& v) const
110 {
111 return Vector4<T>(x - v.x, y - v.y, z - v.z, w - v.w);
112 }
113
114 Vector4<T> operator *(const Vector4<T>& v) const
115 {
116 return Vector4<T>(x * v.x, y * v.y, z * v.z, w * v.w);
117 }
118
119 Vector4<T> operator /(const Vector4<T>& v) const
120 {
121 return Vector4<T>(x / v.x, y / v.y, z / v.z, w / v.w);
122 }
123 Vector4<T> operator +(T val) const
124 {
125 return Vector4<T>(x + val, y + val, z + val, w + val);
126 }
127
128 Vector4<T> operator -(T val) const
129 {
130 return Vector4<T>(x - val, y - val, z - val, w - val);
131 }
132
133 Vector4<T> operator *(T val) const
134 {
135 return Vector4<T>(x * val, y * val, z * val, w * val);
136 }
137
138 Vector4<T> operator /(T val) const
139 {
140 return Vector4<T>(x / val, y / val, z / val, w / val);
141 }
142
143 Vector4<T>& operator +=(const Vector4<T>& v)
144 {
145 *this = *this + v;
146 return *this;
147 }
148
149 Vector4<T>& operator -=(const Vector4<T>& v)
150 {
151 *this = *this - v;
152 return *this;
153 }
154
155 Vector4<T>& operator *=(const Vector4<T>& v)
156 {
157 *this = *this * v;
158 return *this;
159 }
160
161 Vector4<T>& operator /=(const Vector4<T>& v)
162 {
163 *this = *this - v;
164 return *this;
165 }
166
167 Vector4<T>& operator +=(T val)
168 {
169 *this = *this + (T)val;
170 return *this;
171 }
172
173 Vector4<T>& operator -=(T val)
174 {
175 *this = *this - (T)val;
176 return *this;
177 }
178
179 Vector4<T>& operator *=(T val)
180 {
181 *this = *this * (T)val;
182 return *this;
183 }
184
185 Vector4<T>& operator /=(T val)
186 {
187 *this = *this / (T)val;
188 return *this;
189 }
190
191 // Probably doesn't belong here, but I'll leave it in for the moment.
192 void SetColor(u32 color)
193 {
194 x = (color & 0xff) / 255.0f;
195 y = ((color >> 8) & 0xff) / 255.0f;
196 z = ((color >> 16) & 0xff) / 255.0f;
197 }
198
199 bool equal_vectors(const Vector4<T>& v)
200 {
201 if (abs(x - v.x) + abs(y - v.y) + abs(z - v.z) + abs(w - v.w) < 0.01)
202 return true;
203 else
204 return false;
205 }
206
207 };
208
209 typedef Vector4<float> float4;
210
211 #else
212
213 // Reimplement, swiping a bunch of code from GSdx and adapting it. (specifically GSVector.h)
214 // This doesn't include more then half of the functions in there, as well as some of the structs...
215 #include <xmmintrin.h>
216
217 #include "Pcsx2Types.h"
218
219 class float4
220 {
221 public:
222 union
223 {
224 struct {float x, y, z, w;};
225 struct {float r, g, b, a;};
226 struct {float left, top, right, bottom;};
227 float v[4];
228 float f32[4];
229 s8 _s8[16];
230 s16 _s16[8];
231 s32 _s32[4];
232 s64 _s64[2];
233 u8 _u8[16];
234 u16 _u16[8];
235 u32 _u32[4];
236 u64 _u64[2];
237 __m128 m;
238 };
239
240 float4()
241 {
242 m = _mm_setzero_ps();
243 }
244
245 float4(float x, float y, float z, float w = 0)
246 {
247 m = _mm_set_ps(w, z, y, x);
248 }
249
250 float4(float4 &f)
251 {
252 m = f.m;
253 }
254
255 float4(float x, float y)
256 {
257 m = _mm_unpacklo_ps(_mm_load_ss(&x), _mm_load_ss(&y));
258 }
259
260 float4(int x, int y)
261 {
262 m = _mm_cvtepi32_ps(_mm_unpacklo_epi32(_mm_cvtsi32_si128(x), _mm_cvtsi32_si128(y)));
263 }
264
265 explicit float4(float f)
266 {
267 m = _mm_set1_ps(f);
268 }
269
270 explicit float4(__m128 m)
271 {
272 this->m = m;
273 }
274
275 float4(float* f)
276 {
277 x = f[0];
278 y = f[1];
279 z = f[2];
280 w = f[3]; // For some reason, the old code set this to 0.
281 }
282
283 float& operator[](int i)
284 {
285 switch(i)
286 {
287 case 0: return x;
288 case 1: return y;
289 case 2: return z;
290 case 3: return w;
291 default: assert(0);
292 }
293 }
294
295 operator float*()
296 {
297 return (float*) this;
298 }
299
300 operator const float*() const
301 {
302 return (const float*) this;
303 }
304
305 void operator = (float f)
306 {
307 m = _mm_set1_ps(f);
308 }
309
310 void operator = (__m128 m)
311 {
312 this->m = m;
313 }
314
315
316 void operator += (const float4& v)
317 {
318 m = _mm_add_ps(m, v.m);
319 }
320
321 void operator -= (const float4& v)
322 {
323 m = _mm_sub_ps(m, v.m);
324 }
325
326 void operator *= (const float4& v)
327 {
328 m = _mm_mul_ps(m, v.m);
329 }
330
331 void operator /= (const float4& v)
332 {
333 m = _mm_div_ps(m, v.m);
334 }
335
336 void operator += (float f)
337 {
338 *this += float4(f);
339 }
340
341 void operator -= (float f)
342 {
343 *this -= float4(f);
344 }
345
346 void operator *= (float f)
347 {
348 *this *= float4(f);
349 }
350
351 void operator /= (float f)
352 {
353 *this /= float4(f);
354 }
355
356 void operator &= (const float4& v)
357 {
358 m = _mm_and_ps(m, v.m);
359 }
360
361 void operator |= (const float4& v)
362 {
363 m = _mm_or_ps(m, v.m);
364 }
365
366 void operator ^= (const float4& v)
367 {
368 m = _mm_xor_ps(m, v.m);
369 }
370
371 friend float4 operator + (const float4& v1, const float4& v2)
372 {
373 return float4(_mm_add_ps(v1.m, v2.m));
374 }
375
376 friend float4 operator - (const float4& v1, const float4& v2)
377 {
378 return float4(_mm_sub_ps(v1.m, v2.m));
379 }
380
381 friend float4 operator * (const float4& v1, const float4& v2)
382 {
383 return float4(_mm_mul_ps(v1.m, v2.m));
384 }
385
386 friend float4 operator / (const float4& v1, const float4& v2)
387 {
388 return float4(_mm_div_ps(v1.m, v2.m));
389 }
390
391 friend float4 operator + (const float4& v, float f)
392 {
393 return v + float4(f);
394 }
395
396 friend float4 operator - (const float4& v, float f)
397 {
398 return v - float4(f);
399 }
400
401 friend float4 operator * (const float4& v, float f)
402 {
403 return v * float4(f);
404 }
405
406 friend float4 operator / (const float4& v, float f)
407 {
408 return v / float4(f);
409 }
410
411 friend float4 operator & (const float4& v1, const float4& v2)
412 {
413 return float4(_mm_and_ps(v1.m, v2.m));
414 }
415
416 friend float4 operator | (const float4& v1, const float4& v2)
417 {
418 return float4(_mm_or_ps(v1.m, v2.m));
419 }
420
421 friend float4 operator ^ (const float4& v1, const float4& v2)
422 {
423 return float4(_mm_xor_ps(v1.m, v2.m));
424 }
425
426 friend float4 operator == (const float4& v1, const float4& v2)
427 {
428 return float4(_mm_cmpeq_ps(v1.m, v2.m));
429 }
430
431 friend float4 operator != (const float4& v1, const float4& v2)
432 {
433 return float4(_mm_cmpneq_ps(v1.m, v2.m));
434 }
435
436 friend float4 operator > (const float4& v1, const float4& v2)
437 {
438 return float4(_mm_cmpgt_ps(v1.m, v2.m));
439 }
440
441 friend float4 operator < (const float4& v1, const float4& v2)
442 {
443 return float4(_mm_cmplt_ps(v1.m, v2.m));
444 }
445
446 friend float4 operator >= (const float4& v1, const float4& v2)
447 {
448 return float4(_mm_cmpge_ps(v1.m, v2.m));
449 }
450
451 friend float4 operator <= (const float4& v1, const float4& v2)
452 {
453 return float4(_mm_cmple_ps(v1.m, v2.m));
454 }
455
456 bool equal_vectors(const float4& v)
457 {
458 if (abs(x - v.x) + abs(y - v.y) + abs(z - v.z) + abs(w - v.w) < 0.01)
459 return true;
460 else
461 return false;
462 }
463
464 // This looked interesting, so I thought I'd include it...
465
466 template<int i> float4 shuffle() const
467 {
468 return float4(_mm_shuffle_ps(m, m, _MM_SHUFFLE(i, i, i, i)));
469 }
470
471 #define VECTOR4_SHUFFLE_4(xs, xn, ys, yn, zs, zn, ws, wn) \
472 float4 xs##ys##zs##ws() const {return float4(_mm_shuffle_ps(m, m, _MM_SHUFFLE(wn, zn, yn, xn)));} \
473 float4 xs##ys##zs##ws(const float4& v) const {return float4(_mm_shuffle_ps(m, v.m, _MM_SHUFFLE(wn, zn, yn, xn)));} \
474
475 #define VECTOR4_SHUFFLE_3(xs, xn, ys, yn, zs, zn) \
476 VECTOR4_SHUFFLE_4(xs, xn, ys, yn, zs, zn, x, 0) \
477 VECTOR4_SHUFFLE_4(xs, xn, ys, yn, zs, zn, y, 1) \
478 VECTOR4_SHUFFLE_4(xs, xn, ys, yn, zs, zn, z, 2) \
479 VECTOR4_SHUFFLE_4(xs, xn, ys, yn, zs, zn, w, 3) \
480
481 #define VECTOR4_SHUFFLE_2(xs, xn, ys, yn) \
482 VECTOR4_SHUFFLE_3(xs, xn, ys, yn, x, 0) \
483 VECTOR4_SHUFFLE_3(xs, xn, ys, yn, y, 1) \
484 VECTOR4_SHUFFLE_3(xs, xn, ys, yn, z, 2) \
485 VECTOR4_SHUFFLE_3(xs, xn, ys, yn, w, 3) \
486
487 #define VECTOR4_SHUFFLE_1(xs, xn) \
488 float4 xs##4() const {return float4(_mm_shuffle_ps(m, m, _MM_SHUFFLE(xn, xn, xn, xn)));} \
489 float4 xs##4(const float4& v) const {return float4(_mm_shuffle_ps(m, v.m, _MM_SHUFFLE(xn, xn, xn, xn)));} \
490 VECTOR4_SHUFFLE_2(xs, xn, x, 0) \
491 VECTOR4_SHUFFLE_2(xs, xn, y, 1) \
492 VECTOR4_SHUFFLE_2(xs, xn, z, 2) \
493 VECTOR4_SHUFFLE_2(xs, xn, w, 3) \
494
495 VECTOR4_SHUFFLE_1(x, 0)
496 VECTOR4_SHUFFLE_1(y, 1)
497 VECTOR4_SHUFFLE_1(z, 2)
498 VECTOR4_SHUFFLE_1(w, 3)
499
500 // Probably doesn't belong here, but I'll leave it in for the moment.
501 void SetColor(u32 color)
502 {
503 x = (color & 0xff) / 255.0f;
504 y = ((color >> 8) & 0xff) / 255.0f;
505 z = ((color >> 16) & 0xff) / 255.0f;
506 }
507 };
508
509 #endif
510
511 #endif

  ViewVC Help
Powered by ViewVC 1.1.22