-
Notifications
You must be signed in to change notification settings - Fork 30
/
precomp.h
1463 lines (1390 loc) · 77.9 KB
/
precomp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Template, IGAD version 2
// IGAD/NHTV/UU - Jacco Bikker - 2006-2021
// add your includes to this file instead of to individual .cpp files
// to enjoy the benefits of precompiled headers:
// - fast compilation
// - solve issues with the order of header files once (here)
// do not include headers in header files (ever).
// C++ headers
#include <chrono>
#include <fstream>
#include <vector>
#include <list>
#include <string>
#include <thread>
#include <math.h>
#include <algorithm>
#include <assert.h>
#include <io.h>
#include "lib/stb_image.h"
// header for AVX, and every technology before it.
// if your CPU does not support this (unlikely), include the appropriate header instead.
// see: https://stackoverflow.com/a/11228864/2844473
#include <immintrin.h>
// clang-format off
// "leak" common namespaces to all compilation units. This is not standard
// C++ practice but a simplification for template projects.
using namespace std;
// windows
#define NOMINMAX
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#endif
#define NOGDICAPMASKS
// #define NOVIRTUALKEYCODES
#define NOWINMESSAGES
#define NOWINSTYLES
#define NOSYSMETRICS
#define NOMENUS
#define NOICONS
#define NOKEYSTATES
#define NOSYSCOMMANDS
#define NORASTEROPS
#define NOSHOWWINDOW
#define OEMRESOURCE
#define NOATOM
#define NOCLIPBOARD
#define NOCOLOR
#define NOCTLMGR
#define NODRAWTEXT
#define NOKERNEL
#define NONLS
#define NOMEMMGR
#define NOMETAFILE
#define NOMINMAX
#define NOMSG
#define NOOPENFILE
#define NOSCROLL
#define NOSERVICE
#define NOSOUND
#define NOTEXTMETRIC
#define NOWH
#define NOWINOFFSETS
#define NOCOMM
#define NOKANJI
#define NOHELP
#define NOPROFILER
#define NODEFERWINDOWPOS
#define NOMCX
#define NOIME
// #include "windows.h"
#include "lib/win_tiny.h" // windows.h replacement for faster builds
// OpenCL headers
#define CL_USE_DEPRECATED_OPENCL_2_0_APIS // safe; see https://stackoverflow.com/a/28500846
#include "cl/cl.h"
#include <cl/cl_gl_ext.h>
// GLFW
#define GLFW_USE_CHDIR 0
#define GLFW_EXPOSE_NATIVE_WIN32
#define GLFW_EXPOSE_NATIVE_WGL
#include <glad.h>
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
// zlib
#include "zlib.h"
// basic types
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned short ushort;
#ifdef _MSC_VER
typedef unsigned char BYTE; // for freeimage.h
typedef unsigned short WORD; // for freeimage.h
typedef unsigned long DWORD; // for freeimage.h
typedef int BOOL; // for freeimage.h
#endif
namespace Tmpl8
{
// 32-bit surface container
class Surface
{
enum { OWNER = 1 };
public:
// constructor / destructor
Surface() = default;
Surface( int w, int h, uint* buffer );
Surface( int w, int h );
Surface( const char* file );
~Surface();
// operations
void InitCharset();
void SetChar( int c, const char* c1, const char* c2, const char* c3, const char* c4, const char* c5 );
void Print( const char* t, int x1, int y1, uint c );
void Clear( uint c );
void Line( float x1, float y1, float x2, float y2, uint c );
void Plot( int x, int y, uint c );
void LoadImage( const char* file );
void CopyTo( Surface* dst, int x, int y );
void Box( int x1, int y1, int x2, int y2, uint color );
void Bar( int x1, int y1, int x2, int y2, uint color );
// attributes
uint* pixels = 0;
int width = 0, height = 0;
bool ownBuffer = false;
};
};
// namespaces
using namespace Tmpl8;
// aligned memory allocations
#ifdef _MSC_VER
#define ALIGN( x ) __declspec( align( x ) )
#define MALLOC64( x ) ( ( x ) == 0 ? 0 : _aligned_malloc( ( x ), 64 ) )
#define FREE64( x ) _aligned_free( x )
#else
#define ALIGN( x ) __attribute__( ( aligned( x ) ) )
#define MALLOC64( x ) ( ( x ) == 0 ? 0 : aligned_alloc( 64, ( x ) ) )
#define FREE64( x ) free( x )
#endif
#if defined(__GNUC__) && (__GNUC__ >= 4)
#define CHECK_RESULT __attribute__ ((warn_unused_result))
#elif defined(_MSC_VER) && (_MSC_VER >= 1700)
#define CHECK_RESULT _Check_return_
#else
#define CHECK_RESULT
#endif
// vector type placeholders, carefully matching OpenCL's layout and alignment
struct ALIGN( 8 ) int2
{
int2() = default;
int2( const int a, const int b ) : x( a ), y( b ) {}
int2( const int a ) : x( a ), y( a ) {}
union { struct { int x, y; }; int cell[2]; };
int operator [] ( const int n ) const { return cell[n]; }
};
struct ALIGN( 8 ) uint2
{
uint2() = default;
uint2( const int a, const int b ) : x( a ), y( b ) {}
uint2( const uint a ) : x( a ), y( a ) {}
union { struct { uint x, y; }; uint cell[2]; };
uint operator [] ( const int n ) const { return cell[n]; }
};
struct ALIGN( 8 ) float2
{
float2() = default;
float2( const float a, const float b ) : x( a ), y( b ) {}
float2( const float a ) : x( a ), y( a ) {}
union { struct { float x, y; }; float cell[2]; };
float operator [] ( const int n ) const { return cell[n]; }
};
struct int3;
struct ALIGN( 16 ) int4
{
int4() = default;
int4( const int a, const int b, const int c, const int d ) : x( a ), y( b ), z( c ), w( d ) {}
int4( const int a ) : x( a ), y( a ), z( a ), w( a ) {}
int4( const int3 & a, const int d );
union { struct { int x, y, z, w; }; int cell[4]; };
int operator [] ( const int n ) const { return cell[n]; }
};
struct ALIGN( 16 ) int3
{
int3() = default;
int3( const int a, const int b, const int c ) : x( a ), y( b ), z( c ) {}
int3( const int a ) : x( a ), y( a ), z( a ) {}
int3( const int4 a ) : x( a.x ), y( a.y ), z( a.z ) {}
union { struct { int x, y, z; int dummy; }; int cell[4]; };
int operator [] ( const int n ) const { return cell[n]; }
};
struct uint3;
struct ALIGN( 16 ) uint4
{
uint4() = default;
uint4( const uint a, const uint b, const uint c, const uint d ) : x( a ), y( b ), z( c ), w( d ) {}
uint4( const uint a ) : x( a ), y( a ), z( a ), w( a ) {}
uint4( const uint3 & a, const uint d );
union { struct { uint x, y, z, w; }; uint cell[4]; };
uint operator [] ( const int n ) const { return cell[n]; }
};
struct ALIGN( 16 ) uint3
{
uint3() = default;
uint3( const uint a, const uint b, const uint c ) : x( a ), y( b ), z( c ) {}
uint3( const uint a ) : x( a ), y( a ), z( a ) {}
uint3( const uint4 a ) : x( a.x ), y( a.y ), z( a.z ) {}
union { struct { uint x, y, z; uint dummy; }; uint cell[4]; };
uint operator [] ( const int n ) const { return cell[n]; }
};
struct float3;
struct ALIGN( 16 ) float4
{
float4() = default;
float4( const float a, const float b, const float c, const float d ) : x( a ), y( b ), z( c ), w( d ) {}
float4( const float a ) : x( a ), y( a ), z( a ), w( a ) {}
float4( const float3 & a, const float d );
union { struct { float x, y, z, w; }; float cell[4]; };
float operator [] ( const int n ) const { return cell[n]; }
};
struct float3
{
float3() = default;
float3( const float a, const float b, const float c ) : x( a ), y( b ), z( c ) {}
float3( const float a ) : x( a ), y( a ), z( a ) {}
float3( const float4 a ) : x( a.x ), y( a.y ), z( a.z ) {}
float3( const uint3 a ) : x( (float)a.x ), y( (float)a.y ), z( (float)a.z ) {}
union { struct { float x, y, z; }; float cell[3]; };
float operator [] ( const int n ) const { return cell[n]; }
};
struct ALIGN( 4 ) uchar4
{
uchar4() = default;
uchar4( const uchar a, const uchar b, const uchar c, const uchar d ) : x( a ), y( b ), z( c ), w( d ) {}
uchar4( const uchar a ) : x( a ), y( a ), z( a ), w( a ) {}
union { struct { uchar x, y, z, w; }; uchar cell[4]; };
uchar operator [] ( const int n ) const { return cell[n]; }
};
// fatal error reporting (with a pretty window)
#define FATALERROR( fmt, ... ) FatalError( "Error on line %d of %s: " fmt "\n", __LINE__, __FILE__, ##__VA_ARGS__ )
#define FATALERROR_IF( condition, fmt, ... ) do { if ( ( condition ) ) FATALERROR( fmt, ##__VA_ARGS__ ); } while ( 0 )
#define FATALERROR_IN( prefix, errstr, fmt, ... ) FatalError( prefix " returned error '%s' at %s:%d" fmt "\n", errstr, __FILE__, __LINE__, ##__VA_ARGS__ );
#define FATALERROR_IN_CALL( stmt, error_parser, fmt, ... ) do { auto ret = ( stmt ); if ( ret ) FATALERROR_IN( #stmt, error_parser( ret ), fmt, ##__VA_ARGS__ ) } while ( 0 )
// OpenGL texture wrapper
class GLTexture
{
public:
enum { DEFAULT = 0, FLOAT = 1, INTTARGET = 2 };
// constructor / destructor
GLTexture( uint width, uint height, uint type = DEFAULT );
~GLTexture();
// methods
void Bind( const uint slot = 0 );
void CopyFrom( Tmpl8::Surface* src );
void CopyTo( Tmpl8::Surface* dst );
public:
// public data members
GLuint ID = 0;
uint width = 0, height = 0;
};
// template function access
GLTexture* GetRenderTarget();
// shader wrapper
class mat4;
class Shader
{
public:
// constructor / destructor
Shader( const char* vfile, const char* pfile, bool fromString );
~Shader();
// methods
void Init( const char* vfile, const char* pfile );
void Compile( const char* vtext, const char* ftext );
void Bind();
void SetInputTexture( uint slot, const char* name, GLTexture* texture );
void SetInputMatrix( const char* name, const mat4& matrix );
void SetFloat( const char* name, const float v );
void SetInt( const char* name, const int v );
void SetUInt( const char* name, const uint v );
void Unbind();
private:
// data members
uint vertex = 0; // vertex shader identifier
uint pixel = 0; // fragment shader identifier
uint ID = 0; // shader program identifier
};
// generic error checking for OpenGL code
#define CheckGL() { _CheckGL( __FILE__, __LINE__ ); }
// forward declarations of helper functions
void _CheckGL( const char* f, int l );
GLuint CreateVBO( const GLfloat* data, const uint size );
void BindVBO( const uint idx, const uint N, const GLuint id );
void CheckShader( GLuint shader, const char* vshader, const char* fshader );
void CheckProgram( GLuint id, const char* vshader, const char* fshader );
void DrawQuad();
// timer
struct Timer
{
Timer() { reset(); }
float elapsed() const
{
chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - start);
return (float)time_span.count();
}
void reset() { start = chrono::high_resolution_clock::now(); }
chrono::high_resolution_clock::time_point start;
};
// swap
template <class T> void Swap( T& x, T& y ) { T t; t = x, x = y, y = t; }
// Nils's jobmanager
class Job
{
public:
virtual void Main() = 0;
protected:
friend class JobThread;
void RunCodeWrapper();
};
class JobThread
{
public:
void CreateAndStartThread( unsigned int threadId );
void Go();
void BackgroundTask();
HANDLE m_GoSignal, m_ThreadHandle;
int m_ThreadID;
};
class JobManager // singleton class!
{
protected:
JobManager( unsigned int numThreads );
public:
~JobManager();
static void CreateJobManager( unsigned int numThreads );
static JobManager* GetJobManager();
static void GetProcessorCount( uint& cores, uint& logical );
void AddJob2( Job* a_Job );
unsigned int GetNumThreads() { return m_NumThreads; }
void RunJobs();
void ThreadDone( unsigned int n );
int MaxConcurrent() { return m_NumThreads; }
protected:
friend class JobThread;
Job* GetNextJob();
static JobManager* m_JobManager;
Job* m_JobList[256];
CRITICAL_SECTION m_CS;
HANDLE m_ThreadDone[64];
unsigned int m_NumThreads, m_JobCount;
JobThread* m_JobThreadList;
};
// pixel operations
inline uint ScaleColor( const uint c, const uint scale )
{
const uint rb = (((c & 0xff00ff) * scale) >> 8) & 0x00ff00ff;
const uint ag = (((c & 0xff00ff00) >> 8) * scale) & 0xff00ff00;
return rb + ag;
}
inline uint AddBlend( const uint c1, const uint c2 )
{
const uint r1 = (c1 >> 16) & 255, r2 = (c2 >> 16) & 255;
const uint g1 = (c1 >> 8) & 255, g2 = (c2 >> 8) & 255;
const uint b1 = c1 & 255, b2 = c2 & 255;
const uint r = min( 255u, r1 + r2 );
const uint g = min( 255u, g1 + g2 );
const uint b = min( 255u, b1 + b2 );
return (r << 16) + (g << 8) + b;
}
// random numbers
uint RandomUInt();
uint RandomUInt( uint& seed );
float RandomFloat();
float RandomFloat( uint& seed );
float Rand( float range );
// Perlin noise
float noise2D( const float x, const float y );
// forward declaration of helper functions
void FatalError( const char* fmt, ... );
bool FileIsNewer( const char* file1, const char* file2 );
bool FileExists( const char* f );
bool RemoveFile( const char* f );
string TextFileRead( const char* _File );
int LineCount( const string s );
void TextFileWrite( const string& text, const char* _File );
// math
inline float fminf( float a, float b ) { return a < b ? a : b; }
inline float fmaxf( float a, float b ) { return a > b ? a : b; }
inline float rsqrtf( float x ) { return 1.0f / sqrtf( x ); }
inline float sqrf( float x ) { return x * x; }
inline int sqr( int x ) { return x * x; }
inline float2 make_float2( const float a, float b ) { float2 f2; f2.x = a, f2.y = b; return f2; }
inline float2 make_float2( const float s ) { return make_float2( s, s ); }
inline float2 make_float2( const float3& a ) { return make_float2( a.x, a.y ); }
inline float2 make_float2( const int2& a ) { return make_float2( float( a.x ), float( a.y ) ); } // explicit casts prevent gcc warnings
inline float2 make_float2( const uint2& a ) { return make_float2( float( a.x ), float( a.y ) ); }
inline int2 make_int2( const int a, const int b ) { int2 i2; i2.x = a, i2.y = b; return i2; }
inline int2 make_int2( const int s ) { return make_int2( s, s ); }
inline int2 make_int2( const int3& a ) { return make_int2( a.x, a.y ); }
inline int2 make_int2( const uint2& a ) { return make_int2( int( a.x ), int( a.y ) ); }
inline int2 make_int2( const float2& a ) { return make_int2( int( a.x ), int( a.y ) ); }
inline uint2 make_uint2( const uint a, const uint b ) { uint2 u2; u2.x = a, u2.y = b; return u2; }
inline uint2 make_uint2( const uint s ) { return make_uint2( s, s ); }
inline uint2 make_uint2( const uint3& a ) { return make_uint2( a.x, a.y ); }
inline uint2 make_uint2( const int2& a ) { return make_uint2( uint( a.x ), uint( a.y ) ); }
inline float3 make_float3( const float& a, const float& b, const float& c ) { float3 f3; f3.x = a, f3.y = b, f3.z = c; return f3; }
inline float3 make_float3( const float& s ) { return make_float3( s, s, s ); }
inline float3 make_float3( const float2& a ) { return make_float3( a.x, a.y, 0.0f ); }
inline float3 make_float3( const float2& a, const float& s ) { return make_float3( a.x, a.y, s ); }
inline float3 make_float3( const float4& a ) { return make_float3( a.x, a.y, a.z ); }
inline float3 make_float3( const int3& a ) { return make_float3( float( a.x ), float( a.y ), float( a.z ) ); }
inline float3 make_float3( const uint3& a ) { return make_float3( float( a.x ), float( a.y ), float( a.z ) ); }
inline int3 make_int3( const int& a, const int& b, const int& c ) { int3 i3; i3.x = a, i3.y = b, i3.z = c; return i3; }
inline int3 make_int3( const int& s ) { return make_int3( s, s, s ); }
inline int3 make_int3( const int2& a ) { return make_int3( a.x, a.y, 0 ); }
inline int3 make_int3( const int2& a, const int& s ) { return make_int3( a.x, a.y, s ); }
inline int3 make_int3( const uint3& a ) { return make_int3( int( a.x ), int( a.y ), int( a.z ) ); }
inline int3 make_int3( const float3& a ) { return make_int3( int( a.x ), int( a.y ), int( a.z ) ); }
inline int3 make_int3( const float4& a ) { return make_int3( int( a.x ), int( a.y ), int( a.z ) ); }
inline uint3 make_uint3( const uint a, uint b, uint c ) { uint3 u3; u3.x = a, u3.y = b, u3.z = c; return u3; }
inline uint3 make_uint3( const uint s ) { return make_uint3( s, s, s ); }
inline uint3 make_uint3( const uint2& a ) { return make_uint3( a.x, a.y, 0 ); }
inline uint3 make_uint3( const uint2& a, const uint s ) { return make_uint3( a.x, a.y, s ); }
inline uint3 make_uint3( const uint4& a ) { return make_uint3( a.x, a.y, a.z ); }
inline uint3 make_uint3( const int3& a ) { return make_uint3( uint( a.x ), uint( a.y ), uint( a.z ) ); }
inline float4 make_float4( const float a, const float b, const float c, const float d ) { float4 f4; f4.x = a, f4.y = b, f4.z = c, f4.w = d; return f4; }
inline float4 make_float4( const float s ) { return make_float4( s, s, s, s ); }
inline float4 make_float4( const float3& a ) { return make_float4( a.x, a.y, a.z, 0.0f ); }
inline float4 make_float4( const float3& a, const float w ) { return make_float4( a.x, a.y, a.z, w ); }
inline float4 make_float4( const int3& a, const float w ) { return make_float4( (float)a.x, (float)a.y, (float)a.z, w ); }
inline float4 make_float4( const int4& a ) { return make_float4( float( a.x ), float( a.y ), float( a.z ), float( a.w ) ); }
inline float4 make_float4( const uint4& a ) { return make_float4( float( a.x ), float( a.y ), float( a.z ), float( a.w ) ); }
inline int4 make_int4( const int a, const int b, const int c, const int d ) { int4 i4; i4.x = a, i4.y = b, i4.z = c, i4.w = d; return i4; }
inline int4 make_int4( const int s ) { return make_int4( s, s, s, s ); }
inline int4 make_int4( const int3& a ) { return make_int4( a.x, a.y, a.z, 0 ); }
inline int4 make_int4( const int3& a, const int w ) { return make_int4( a.x, a.y, a.z, w ); }
inline int4 make_int4( const uint4& a ) { return make_int4( int( a.x ), int( a.y ), int( a.z ), int( a.w ) ); }
inline int4 make_int4( const float4& a ) { return make_int4( int( a.x ), int( a.y ), int( a.z ), int( a.w ) ); }
inline uint4 make_uint4( const uint a, const uint b, const uint c, const uint d ) { uint4 u4; u4.x = a, u4.y = b, u4.z = c, u4.w = d; return u4; }
inline uint4 make_uint4( const uint s ) { return make_uint4( s, s, s, s ); }
inline uint4 make_uint4( const uint3& a ) { return make_uint4( a.x, a.y, a.z, 0 ); }
inline uint4 make_uint4( const uint3& a, const uint w ) { return make_uint4( a.x, a.y, a.z, w ); }
inline uint4 make_uint4( const int4& a ) { return make_uint4( uint( a.x ), uint( a.y ), uint( a.z ), uint( a.w ) ); }
inline uchar4 make_uchar4( const uchar a, const uchar b, const uchar c, const uchar d ) { uchar4 c4; c4.x = a, c4.y = b, c4.z = c, c4.w = d; return c4; }
inline float2 operator-( const float2& a ) { return make_float2( -a.x, -a.y ); }
inline int2 operator-( const int2& a ) { return make_int2( -a.x, -a.y ); }
inline float3 operator-( const float3& a ) { return make_float3( -a.x, -a.y, -a.z ); }
inline int3 operator-( const int3& a ) { return make_int3( -a.x, -a.y, -a.z ); }
inline float4 operator-( const float4& a ) { return make_float4( -a.x, -a.y, -a.z, -a.w ); }
inline int4 operator-( const int4& a ) { return make_int4( -a.x, -a.y, -a.z, -a.w ); }
inline int2 operator << ( const int2& a, int b ) { return make_int2( a.x << b, a.y << b ); }
inline int2 operator >> ( const int2& a, int b ) { return make_int2( a.x >> b, a.y >> b ); }
inline int3 operator << ( const int3& a, int b ) { return make_int3( a.x << b, a.y << b, a.z << b ); }
inline int3 operator >> ( const int3& a, int b ) { return make_int3( a.x >> b, a.y >> b, a.z >> b ); }
inline int4 operator << ( const int4& a, int b ) { return make_int4( a.x << b, a.y << b, a.z << b, a.w << b ); }
inline int4 operator >> ( const int4& a, int b ) { return make_int4( a.x >> b, a.y >> b, a.z >> b, a.w >> b ); }
inline float2 operator+( const float2& a, const float2& b ) { return make_float2( a.x + b.x, a.y + b.y ); }
inline float2 operator+( const float2& a, const int2& b ) { return make_float2( a.x + (float)b.x, a.y + (float)b.y ); }
inline float2 operator+( const float2& a, const uint2& b ) { return make_float2( a.x + (float)b.x, a.y + (float)b.y ); }
inline float2 operator+( const int2& a, const float2& b ) { return make_float2( (float)a.x + b.x, (float)a.y + b.y ); }
inline float2 operator+( const uint2& a, const float2& b ) { return make_float2( (float)a.x + b.x, (float)a.y + b.y ); }
inline void operator+=( float2& a, const float2& b ) { a.x += b.x; a.y += b.y; }
inline void operator+=( float2& a, const int2& b ) { a.x += (float)b.x; a.y += (float)b.y; }
inline void operator+=( float2& a, const uint2& b ) { a.x += (float)b.x; a.y += (float)b.y; }
inline float2 operator+( const float2& a, float b ) { return make_float2( a.x + b, a.y + b ); }
inline float2 operator+( const float2& a, int b ) { return make_float2( a.x + (float)b, a.y + (float)b ); }
inline float2 operator+( const float2& a, uint b ) { return make_float2( a.x + (float)b, a.y + (float)b ); }
inline float2 operator+( float b, const float2& a ) { return make_float2( a.x + b, a.y + b ); }
inline void operator+=( float2& a, float b ) { a.x += b; a.y += b; }
inline void operator+=( float2& a, int b ) { a.x += (float)b; a.y += (float)b; }
inline void operator+=( float2& a, uint b ) { a.x += (float)b; a.y += (float)b; }
inline int2 operator+( const int2& a, const int2& b ) { return make_int2( a.x + b.x, a.y + b.y ); }
inline void operator+=( int2& a, const int2& b ) { a.x += b.x; a.y += b.y; }
inline int2 operator+( const int2& a, int b ) { return make_int2( a.x + b, a.y + b ); }
inline int2 operator+( int b, const int2& a ) { return make_int2( a.x + b, a.y + b ); }
inline void operator+=( int2& a, int b ) { a.x += b; a.y += b; }
inline uint2 operator+( const uint2& a, const uint2& b ) { return make_uint2( a.x + b.x, a.y + b.y ); }
inline void operator+=( uint2& a, const uint2& b ) { a.x += b.x; a.y += b.y; }
inline uint2 operator+( const uint2& a, uint b ) { return make_uint2( a.x + b, a.y + b ); }
inline uint2 operator+( uint b, const uint2& a ) { return make_uint2( a.x + b, a.y + b ); }
inline void operator+=( uint2& a, uint b ) { a.x += b; a.y += b; }
inline float3 operator+( const float3& a, const float3& b ) { return make_float3( a.x + b.x, a.y + b.y, a.z + b.z ); }
inline float3 operator+( const float3& a, const int3& b ) { return make_float3( a.x + (float)b.x, a.y + (float)b.y, a.z + (float)b.z ); }
inline float3 operator+( const float3& a, const uint3& b ) { return make_float3( a.x + (float)b.x, a.y + (float)b.y, a.z + (float)b.z ); }
inline float3 operator+( const int3& a, const float3& b ) { return make_float3( (float)a.x + b.x, (float)a.y + b.y, (float)a.z + b.z ); }
inline float3 operator+( const uint3& a, const float3& b ) { return make_float3( (float)a.x + b.x, (float)a.y + b.y, (float)a.z + b.z ); }
inline void operator+=( float3& a, const float3& b ) { a.x += b.x; a.y += b.y; a.z += b.z; }
inline void operator+=( float3& a, const int3& b ) { a.x += (float)b.x; a.y += (float)b.y; a.z += (float)b.z; }
inline void operator+=( float3& a, const uint3& b ) { a.x += (float)b.x; a.y += (float)b.y; a.z += (float)b.z; }
inline float3 operator+( const float3& a, float b ) { return make_float3( a.x + b, a.y + b, a.z + b ); }
inline float3 operator+( const float3& a, int b ) { return make_float3( a.x + (float)b, a.y + (float)b, a.z + (float)b ); }
inline float3 operator+( const float3& a, uint b ) { return make_float3( a.x + (float)b, a.y + (float)b, a.z + (float)b ); }
inline void operator+=( float3& a, float b ) { a.x += b; a.y += b; a.z += b; }
inline void operator+=( float3& a, int b ) { a.x += (float)b; a.y += (float)b; a.z += (float)b; }
inline void operator+=( float3& a, uint b ) { a.x += (float)b; a.y += (float)b; a.z += (float)b; }
inline int3 operator+( const int3& a, const int3& b ) { return make_int3( a.x + b.x, a.y + b.y, a.z + b.z ); }
inline void operator+=( int3& a, const int3& b ) { a.x += b.x; a.y += b.y; a.z += b.z; }
inline int3 operator+( const int3& a, int b ) { return make_int3( a.x + b, a.y + b, a.z + b ); }
inline void operator+=( int3& a, int b ) { a.x += b; a.y += b; a.z += b; }
inline uint3 operator+( const uint3& a, const uint3& b ) { return make_uint3( a.x + b.x, a.y + b.y, a.z + b.z ); }
inline void operator+=( uint3& a, const uint3& b ) { a.x += b.x; a.y += b.y; a.z += b.z; }
inline uint3 operator+( const uint3& a, uint b ) { return make_uint3( a.x + b, a.y + b, a.z + b ); }
inline void operator+=( uint3& a, uint b ) { a.x += b; a.y += b; a.z += b; }
inline int3 operator+( int b, const int3& a ) { return make_int3( a.x + b, a.y + b, a.z + b ); }
inline uint3 operator+( uint b, const uint3& a ) { return make_uint3( a.x + b, a.y + b, a.z + b ); }
inline float3 operator+( float b, const float3& a ) { return make_float3( a.x + b, a.y + b, a.z + b ); }
inline float4 operator+( const float4& a, const float4& b ) { return make_float4( a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w ); }
inline float4 operator+( const float4& a, const int4& b ) { return make_float4( a.x + (float)b.x, a.y + (float)b.y, a.z + (float)b.z, a.w + (float)b.w ); }
inline float4 operator+( const float4& a, const uint4& b ) { return make_float4( a.x + (float)b.x, a.y + (float)b.y, a.z + (float)b.z, a.w + (float)b.w ); }
inline float4 operator+( const int4& a, const float4& b ) { return make_float4( (float)a.x + b.x, (float)a.y + b.y, (float)a.z + b.z, (float)a.w + b.w ); }
inline float4 operator+( const uint4& a, const float4& b ) { return make_float4( (float)a.x + b.x, (float)a.y + b.y, (float)a.z + b.z, (float)a.w + b.w ); }
inline void operator+=( float4& a, const float4& b ) { a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w; }
inline void operator+=( float4& a, const int4& b ) { a.x += (float)b.x; a.y += (float)b.y; a.z += (float)b.z; a.w += (float)b.w; }
inline void operator+=( float4& a, const uint4& b ) { a.x += (float)b.x; a.y += (float)b.y; a.z += (float)b.z; a.w += (float)b.w; }
inline float4 operator+( const float4& a, float b ) { return make_float4( a.x + b, a.y + b, a.z + b, a.w + b ); }
inline float4 operator+( const float4& a, int b ) { return make_float4( a.x + (float)b, a.y + (float)b, a.z + (float)b, a.w + (float)b ); }
inline float4 operator+( const float4& a, uint b ) { return make_float4( a.x + (float)b, a.y + (float)b, a.z + (float)b, a.w + (float)b ); }
inline float4 operator+( float b, const float4& a ) { return make_float4( a.x + b, a.y + b, a.z + b, a.w + b ); }
inline void operator+=( float4& a, float b ) { a.x += b; a.y += b; a.z += b; a.w += b; }
inline void operator+=( float4& a, int b ) { a.x += (float)b; a.y += (float)b; a.z += (float)b; a.w += (float)b; }
inline void operator+=( float4& a, uint b ) { a.x += (float)b; a.y += (float)b; a.z += (float)b; a.w += (float)b; }
inline int4 operator+( const int4& a, const int4& b ) { return make_int4( a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w ); }
inline void operator+=( int4& a, const int4& b ) { a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w; }
inline int4 operator+( const int4& a, int b ) { return make_int4( a.x + b, a.y + b, a.z + b, a.w + b ); }
inline int4 operator+( int b, const int4& a ) { return make_int4( a.x + b, a.y + b, a.z + b, a.w + b ); }
inline void operator+=( int4& a, int b ) { a.x += b; a.y += b; a.z += b; a.w += b; }
inline uint4 operator+( const uint4& a, const uint4& b ) { return make_uint4( a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w ); }
inline void operator+=( uint4& a, const uint4& b ) { a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w; }
inline uint4 operator+( const uint4& a, uint b ) { return make_uint4( a.x + b, a.y + b, a.z + b, a.w + b ); }
inline uint4 operator+( uint b, const uint4& a ) { return make_uint4( a.x + b, a.y + b, a.z + b, a.w + b ); }
inline void operator+=( uint4& a, uint b ) { a.x += b; a.y += b; a.z += b; a.w += b; }
inline float2 operator-( const float2& a, const float2& b ) { return make_float2( a.x - b.x, a.y - b.y ); }
inline float2 operator-( const float2& a, const int2& b ) { return make_float2( a.x - (float)b.x, a.y - (float)b.y ); }
inline float2 operator-( const float2& a, const uint2& b ) { return make_float2( a.x - (float)b.x, a.y - (float)b.y ); }
inline float2 operator-( const int2& a, const float2& b ) { return make_float2( (float)a.x - b.x, (float)a.y - b.y ); }
inline float2 operator-( const uint2& a, const float2& b ) { return make_float2( (float)a.x - b.x, (float)a.y - b.y ); }
inline void operator-=( float2& a, const float2& b ) { a.x -= b.x; a.y -= b.y; }
inline void operator-=( float2& a, const int2& b ) { a.x -= (float)b.x; a.y -= (float)b.y; }
inline void operator-=( float2& a, const uint2& b ) { a.x -= (float)b.x; a.y -= (float)b.y; }
inline float2 operator-( const float2& a, float b ) { return make_float2( a.x - b, a.y - b ); }
inline float2 operator-( const float2& a, int b ) { return make_float2( a.x - (float)b, a.y - (float)b ); }
inline float2 operator-( const float2& a, uint b ) { return make_float2( a.x - (float)b, a.y - (float)b ); }
inline float2 operator-( float b, const float2& a ) { return make_float2( b - a.x, b - a.y ); }
inline void operator-=( float2& a, float b ) { a.x -= b; a.y -= b; }
inline void operator-=( float2& a, int b ) { a.x -= (float)b; a.y -= (float)b; }
inline void operator-=( float2& a, uint b ) { a.x -= (float)b; a.y -= (float)b; }
inline int2 operator-( const int2& a, const int2& b ) { return make_int2( a.x - b.x, a.y - b.y ); }
inline void operator-=( int2& a, const int2& b ) { a.x -= b.x; a.y -= b.y; }
inline int2 operator-( const int2& a, int b ) { return make_int2( a.x - b, a.y - b ); }
inline int2 operator-( int b, const int2& a ) { return make_int2( b - a.x, b - a.y ); }
inline void operator-=( int2& a, int b ) { a.x -= b; a.y -= b; }
inline uint2 operator-( const uint2& a, const uint2& b ) { return make_uint2( a.x - b.x, a.y - b.y ); }
inline void operator-=( uint2& a, const uint2& b ) { a.x -= b.x; a.y -= b.y; }
inline uint2 operator-( const uint2& a, uint b ) { return make_uint2( a.x - b, a.y - b ); }
inline uint2 operator-( uint b, const uint2& a ) { return make_uint2( b - a.x, b - a.y ); }
inline void operator-=( uint2& a, uint b ) { a.x -= b; a.y -= b; }
inline float3 operator-( const float3& a, const float3& b ) { return make_float3( a.x - b.x, a.y - b.y, a.z - b.z ); }
inline float3 operator-( const float3& a, const int3& b ) { return make_float3( a.x - (float)b.x, a.y - (float)b.y, a.z - (float)b.z ); }
inline float3 operator-( const float3& a, const uint3& b ) { return make_float3( a.x - (float)b.x, a.y - (float)b.y, a.z - (float)b.z ); }
inline float3 operator-( const int3& a, const float3& b ) { return make_float3( (float)a.x - b.x, (float)a.y - b.y, (float)a.z - b.z ); }
inline float3 operator-( const uint3& a, const float3& b ) { return make_float3( (float)a.x - b.x, (float)a.y - b.y, (float)a.z - b.z ); }
inline void operator-=( float3& a, const float3& b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; }
inline void operator-=( float3& a, const int3& b ) { a.x -= (float)b.x; a.y -= (float)b.y; a.z -= (float)b.z; }
inline void operator-=( float3& a, const uint3& b ) { a.x -= (float)b.x; a.y -= (float)b.y; a.z -= (float)b.z; }
inline float3 operator-( const float3& a, float b ) { return make_float3( a.x - b, a.y - b, a.z - b ); }
inline float3 operator-( const float3& a, int b ) { return make_float3( a.x - (float)b, a.y - (float)b, a.z - (float)b ); }
inline float3 operator-( const float3& a, uint b ) { return make_float3( a.x - (float)b, a.y - (float)b, a.z - (float)b ); }
inline float3 operator-( float b, const float3& a ) { return make_float3( b - a.x, b - a.y, b - a.z ); }
inline void operator-=( float3& a, float b ) { a.x -= b; a.y -= b; a.z -= b; }
inline void operator-=( float3& a, int b ) { a.x -= (float)b; a.y -= (float)b; a.z -= (float)b; }
inline void operator-=( float3& a, uint b ) { a.x -= (float)b; a.y -= (float)b; a.z -= (float)b; }
inline int3 operator-( const int3& a, const int3& b ) { return make_int3( a.x - b.x, a.y - b.y, a.z - b.z ); }
inline void operator-=( int3& a, const int3& b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; }
inline int3 operator-( const int3& a, int b ) { return make_int3( a.x - b, a.y - b, a.z - b ); }
inline int3 operator-( int b, const int3& a ) { return make_int3( b - a.x, b - a.y, b - a.z ); }
inline void operator-=( int3& a, int b ) { a.x -= b; a.y -= b; a.z -= b; }
inline uint3 operator-( const uint3& a, const uint3& b ) { return make_uint3( a.x - b.x, a.y - b.y, a.z - b.z ); }
inline void operator-=( uint3& a, const uint3& b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; }
inline uint3 operator-( const uint3& a, uint b ) { return make_uint3( a.x - b, a.y - b, a.z - b ); }
inline uint3 operator-( uint b, const uint3& a ) { return make_uint3( b - a.x, b - a.y, b - a.z ); }
inline void operator-=( uint3& a, uint b ) { a.x -= b; a.y -= b; a.z -= b; }
inline float4 operator-( const float4& a, const float4& b ) { return make_float4( a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w ); }
inline float4 operator-( const float4& a, const int4& b ) { return make_float4( a.x - (float)b.x, a.y - (float)b.y, a.z - (float)b.z, a.w - (float)b.w ); }
inline float4 operator-( const float4& a, const uint4& b ) { return make_float4( a.x - (float)b.x, a.y - (float)b.y, a.z - (float)b.z, a.w - (float)b.w ); }
inline float4 operator-( const int4& a, const float4& b ) { return make_float4( (float)a.x - b.x, (float)a.y - b.y, (float)a.z - b.z, (float)a.w - b.w ); }
inline float4 operator-( const uint4& a, const float4& b ) { return make_float4( (float)a.x - b.x, (float)a.y - b.y, (float)a.z - b.z, (float)a.w - b.w ); }
inline void operator-=( float4& a, const float4& b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w; }
inline void operator-=( float4& a, const int4& b ) { a.x -= (float)b.x; a.y -= (float)b.y; a.z -= (float)b.z; a.w -= (float)b.w; }
inline void operator-=( float4& a, const uint4& b ) { a.x -= (float)b.x; a.y -= (float)b.y; a.z -= (float)b.z; a.w -= (float)b.w; }
inline float4 operator-( const float4& a, float b ) { return make_float4( a.x - b, a.y - b, a.z - b, a.w - b ); }
inline float4 operator-( const float4& a, int b ) { return make_float4( a.x - (float)b, a.y - (float)b, a.z - (float)b, a.w - (float)b ); }
inline float4 operator-( const float4& a, uint b ) { return make_float4( a.x - (float)b, a.y - (float)b, a.z - (float)b, a.w - (float)b ); }
inline void operator-=( float4& a, float b ) { a.x -= b; a.y -= b; a.z -= b; a.w -= b; }
inline void operator-=( float4& a, int b ) { a.x -= (float)b; a.y -= (float)b; a.z -= (float)b; a.w -= (float)b; }
inline void operator-=( float4& a, uint b ) { a.x -= (float)b; a.y -= (float)b; a.z -= (float)b; a.w -= (float)b; }
inline int4 operator-( const int4& a, const int4& b ) { return make_int4( a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w ); }
inline void operator-=( int4& a, const int4& b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w; }
inline int4 operator-( const int4& a, int b ) { return make_int4( a.x - b, a.y - b, a.z - b, a.w - b ); }
inline int4 operator-( int b, const int4& a ) { return make_int4( b - a.x, b - a.y, b - a.z, b - a.w ); }
inline void operator-=( int4& a, int b ) { a.x -= b; a.y -= b; a.z -= b; a.w -= b; }
inline uint4 operator-( const uint4& a, const uint4& b ) { return make_uint4( a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w ); }
inline void operator-=( uint4& a, const uint4& b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w; }
inline uint4 operator-( const uint4& a, uint b ) { return make_uint4( a.x - b, a.y - b, a.z - b, a.w - b ); }
inline uint4 operator-( uint b, const uint4& a ) { return make_uint4( b - a.x, b - a.y, b - a.z, b - a.w ); }
inline void operator-=( uint4& a, uint b ) { a.x -= b; a.y -= b; a.z -= b; a.w -= b; }
inline float2 operator*( const float2& a, const float2& b ) { return make_float2( a.x * b.x, a.y * b.y ); }
inline void operator*=( float2& a, const float2& b ) { a.x *= b.x; a.y *= b.y; }
inline float2 operator*( const float2& a, float b ) { return make_float2( a.x * b, a.y * b ); }
inline float2 operator*( float b, const float2& a ) { return make_float2( b * a.x, b * a.y ); }
inline void operator*=( float2& a, float b ) { a.x *= b; a.y *= b; }
inline int2 operator*( const int2& a, const int2& b ) { return make_int2( a.x * b.x, a.y * b.y ); }
inline void operator*=( int2& a, const int2& b ) { a.x *= b.x; a.y *= b.y; }
inline int2 operator*( const int2& a, int b ) { return make_int2( a.x * b, a.y * b ); }
inline int2 operator*( int b, const int2& a ) { return make_int2( b * a.x, b * a.y ); }
inline void operator*=( int2& a, int b ) { a.x *= b; a.y *= b; }
inline uint2 operator*( const uint2& a, const uint2& b ) { return make_uint2( a.x * b.x, a.y * b.y ); }
inline void operator*=( uint2& a, const uint2& b ) { a.x *= b.x; a.y *= b.y; }
inline uint2 operator*( const uint2& a, uint b ) { return make_uint2( a.x * b, a.y * b ); }
inline uint2 operator*( uint b, const uint2& a ) { return make_uint2( b * a.x, b * a.y ); }
inline void operator*=( uint2& a, uint b ) { a.x *= b; a.y *= b; }
inline float3 operator*( const float3& a, const float3& b ) { return make_float3( a.x * b.x, a.y * b.y, a.z * b.z ); }
inline void operator*=( float3& a, const float3& b ) { a.x *= b.x; a.y *= b.y; a.z *= b.z; }
inline float3 operator*( const float3& a, float b ) { return make_float3( a.x * b, a.y * b, a.z * b ); }
inline float3 operator*( float b, const float3& a ) { return make_float3( b * a.x, b * a.y, b * a.z ); }
inline void operator*=( float3& a, float b ) { a.x *= b; a.y *= b; a.z *= b; }
inline int3 operator*( const int3& a, const int3& b ) { return make_int3( a.x * b.x, a.y * b.y, a.z * b.z ); }
inline void operator*=( int3& a, const int3& b ) { a.x *= b.x; a.y *= b.y; a.z *= b.z; }
inline int3 operator*( const int3& a, int b ) { return make_int3( a.x * b, a.y * b, a.z * b ); }
inline int3 operator*( int b, const int3& a ) { return make_int3( b * a.x, b * a.y, b * a.z ); }
inline void operator*=( int3& a, int b ) { a.x *= b; a.y *= b; a.z *= b; }
inline uint3 operator*( const uint3& a, const uint3& b ) { return make_uint3( a.x * b.x, a.y * b.y, a.z * b.z ); }
inline void operator*=( uint3& a, const uint3& b ) { a.x *= b.x; a.y *= b.y; a.z *= b.z; }
inline uint3 operator*( const uint3& a, uint b ) { return make_uint3( a.x * b, a.y * b, a.z * b ); }
inline uint3 operator*( uint b, const uint3& a ) { return make_uint3( b * a.x, b * a.y, b * a.z ); }
inline void operator*=( uint3& a, uint b ) { a.x *= b; a.y *= b; a.z *= b; }
inline float4 operator*( const float4& a, const float4& b ) { return make_float4( a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w ); }
inline void operator*=( float4& a, const float4& b ) { a.x *= b.x; a.y *= b.y; a.z *= b.z; a.w *= b.w; }
inline float4 operator*( const float4& a, float b ) { return make_float4( a.x * b, a.y * b, a.z * b, a.w * b ); }
inline float4 operator*( float b, const float4& a ) { return make_float4( b * a.x, b * a.y, b * a.z, b * a.w ); }
inline void operator*=( float4& a, float b ) { a.x *= b; a.y *= b; a.z *= b; a.w *= b; }
inline int4 operator*( const int4& a, const int4& b ) { return make_int4( a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w ); }
inline void operator*=( int4& a, const int4& b ) { a.x *= b.x; a.y *= b.y; a.z *= b.z; a.w *= b.w; }
inline int4 operator*( const int4& a, int b ) { return make_int4( a.x * b, a.y * b, a.z * b, a.w * b ); }
inline int4 operator*( int b, const int4& a ) { return make_int4( b * a.x, b * a.y, b * a.z, b * a.w ); }
inline void operator*=( int4& a, int b ) { a.x *= b; a.y *= b; a.z *= b; a.w *= b; }
inline uint4 operator*( const uint4& a, const uint4& b ) { return make_uint4( a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w ); }
inline void operator*=( uint4& a, const uint4& b ) { a.x *= b.x; a.y *= b.y; a.z *= b.z; a.w *= b.w; }
inline uint4 operator*( const uint4& a, uint b ) { return make_uint4( a.x * b, a.y * b, a.z * b, a.w * b ); }
inline uint4 operator*( uint b, const uint4& a ) { return make_uint4( b * a.x, b * a.y, b * a.z, b * a.w ); }
inline void operator*=( uint4& a, uint b ) { a.x *= b; a.y *= b; a.z *= b; a.w *= b; }
inline float2 operator/( const float2& a, const float2& b ) { return make_float2( a.x / b.x, a.y / b.y ); }
inline void operator/=( float2& a, const float2& b ) { a.x /= b.x; a.y /= b.y; }
inline float2 operator/( const float2& a, float b ) { return make_float2( a.x / b, a.y / b ); }
inline void operator/=( float2& a, float b ) { a.x /= b; a.y /= b; }
inline float2 operator/( float b, const float2& a ) { return make_float2( b / a.x, b / a.y ); }
inline float3 operator/( const float3& a, const float3& b ) { return make_float3( a.x / b.x, a.y / b.y, a.z / b.z ); }
inline void operator/=( float3& a, const float3& b ) { a.x /= b.x; a.y /= b.y; a.z /= b.z; }
inline float3 operator/( const float3& a, float b ) { return make_float3( a.x / b, a.y / b, a.z / b ); }
inline void operator/=( float3& a, float b ) { a.x /= b; a.y /= b; a.z /= b; }
inline float3 operator/( float b, const float3& a ) { return make_float3( b / a.x, b / a.y, b / a.z ); }
inline float4 operator/( const float4& a, const float4& b ) { return make_float4( a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w ); }
inline void operator/=( float4& a, const float4& b ) { a.x /= b.x; a.y /= b.y; a.z /= b.z; a.w /= b.w; }
inline float4 operator/( const float4& a, float b ) { return make_float4( a.x / b, a.y / b, a.z / b, a.w / b ); }
inline void operator/=( float4& a, float b ) { a.x /= b; a.y /= b; a.z /= b; a.w /= b; }
inline float4 operator/( float b, const float4& a ) { return make_float4( b / a.x, b / a.y, b / a.z, b / a.w ); }
inline float2 fminf( const float2& a, const float2& b ) { return make_float2( fminf( a.x, b.x ), fminf( a.y, b.y ) ); }
inline float3 fminf( const float3& a, const float3& b ) { return make_float3( fminf( a.x, b.x ), fminf( a.y, b.y ), fminf( a.z, b.z ) ); }
inline float4 fminf( const float4& a, const float4& b ) { return make_float4( fminf( a.x, b.x ), fminf( a.y, b.y ), fminf( a.z, b.z ), fminf( a.w, b.w ) ); }
inline int2 min( const int2& a, const int2& b ) { return make_int2( min( a.x, b.x ), min( a.y, b.y ) ); }
inline int3 min( const int3& a, const int3& b ) { return make_int3( min( a.x, b.x ), min( a.y, b.y ), min( a.z, b.z ) ); }
inline int4 min( const int4& a, const int4& b ) { return make_int4( min( a.x, b.x ), min( a.y, b.y ), min( a.z, b.z ), min( a.w, b.w ) ); }
inline uint2 min( const uint2& a, const uint2& b ) { return make_uint2( min( a.x, b.x ), min( a.y, b.y ) ); }
inline uint3 min( const uint3& a, const uint3& b ) { return make_uint3( min( a.x, b.x ), min( a.y, b.y ), min( a.z, b.z ) ); }
inline uint4 min( const uint4& a, const uint4& b ) { return make_uint4( min( a.x, b.x ), min( a.y, b.y ), min( a.z, b.z ), min( a.w, b.w ) ); }
inline float2 fmaxf( const float2& a, const float2& b ) { return make_float2( fmaxf( a.x, b.x ), fmaxf( a.y, b.y ) ); }
inline float3 fmaxf( const float3& a, const float3& b ) { return make_float3( fmaxf( a.x, b.x ), fmaxf( a.y, b.y ), fmaxf( a.z, b.z ) ); }
inline float4 fmaxf( const float4& a, const float4& b ) { return make_float4( fmaxf( a.x, b.x ), fmaxf( a.y, b.y ), fmaxf( a.z, b.z ), fmaxf( a.w, b.w ) ); }
inline int2 max( const int2& a, const int2& b ) { return make_int2( max( a.x, b.x ), max( a.y, b.y ) ); }
inline int3 max( const int3& a, const int3& b ) { return make_int3( max( a.x, b.x ), max( a.y, b.y ), max( a.z, b.z ) ); }
inline int4 max( const int4& a, const int4& b ) { return make_int4( max( a.x, b.x ), max( a.y, b.y ), max( a.z, b.z ), max( a.w, b.w ) ); }
inline uint2 max( const uint2& a, const uint2& b ) { return make_uint2( max( a.x, b.x ), max( a.y, b.y ) ); }
inline uint3 max( const uint3& a, const uint3& b ) { return make_uint3( max( a.x, b.x ), max( a.y, b.y ), max( a.z, b.z ) ); }
inline uint4 max( const uint4& a, const uint4& b ) { return make_uint4( max( a.x, b.x ), max( a.y, b.y ), max( a.z, b.z ), max( a.w, b.w ) ); }
inline float lerp( float a, float b, float t ) { return a + t * (b - a); }
inline float2 lerp( const float2& a, const float2& b, float t ) { return a + t * (b - a); }
inline float3 lerp( const float3& a, const float3& b, float t ) { return a + t * (b - a); }
inline float4 lerp( const float4& a, const float4& b, float t ) { return a + t * (b - a); }
inline float clamp( float f, float a, float b ) { return fmaxf( a, fminf( f, b ) ); }
inline int clamp( int f, int a, int b ) { return max( a, min( f, b ) ); }
inline uint clamp( uint f, uint a, uint b ) { return max( a, min( f, b ) ); }
inline float2 clamp( const float2& v, float a, float b ) { return make_float2( clamp( v.x, a, b ), clamp( v.y, a, b ) ); }
inline float2 clamp( const float2& v, const float2& a, const float2& b ) { return make_float2( clamp( v.x, a.x, b.x ), clamp( v.y, a.y, b.y ) ); }
inline float3 clamp( const float3& v, float a, float b ) { return make_float3( clamp( v.x, a, b ), clamp( v.y, a, b ), clamp( v.z, a, b ) ); }
inline float3 clamp( const float3& v, const float3& a, const float3& b ) { return make_float3( clamp( v.x, a.x, b.x ), clamp( v.y, a.y, b.y ), clamp( v.z, a.z, b.z ) ); }
inline float4 clamp( const float4& v, float a, float b ) { return make_float4( clamp( v.x, a, b ), clamp( v.y, a, b ), clamp( v.z, a, b ), clamp( v.w, a, b ) ); }
inline float4 clamp( const float4& v, const float4& a, const float4& b ) { return make_float4( clamp( v.x, a.x, b.x ), clamp( v.y, a.y, b.y ), clamp( v.z, a.z, b.z ), clamp( v.w, a.w, b.w ) ); }
inline int2 clamp( const int2& v, int a, int b ) { return make_int2( clamp( v.x, a, b ), clamp( v.y, a, b ) ); }
inline int2 clamp( const int2& v, const int2& a, const int2& b ) { return make_int2( clamp( v.x, a.x, b.x ), clamp( v.y, a.y, b.y ) ); }
inline int3 clamp( const int3& v, int a, int b ) { return make_int3( clamp( v.x, a, b ), clamp( v.y, a, b ), clamp( v.z, a, b ) ); }
inline int3 clamp( const int3& v, const int3& a, const int3& b ) { return make_int3( clamp( v.x, a.x, b.x ), clamp( v.y, a.y, b.y ), clamp( v.z, a.z, b.z ) ); }
inline int4 clamp( const int4& v, int a, int b ) { return make_int4( clamp( v.x, a, b ), clamp( v.y, a, b ), clamp( v.z, a, b ), clamp( v.w, a, b ) ); }
inline int4 clamp( const int4& v, const int4& a, const int4& b ) { return make_int4( clamp( v.x, a.x, b.x ), clamp( v.y, a.y, b.y ), clamp( v.z, a.z, b.z ), clamp( v.w, a.w, b.w ) ); }
inline uint2 clamp( const uint2& v, uint a, uint b ) { return make_uint2( clamp( v.x, a, b ), clamp( v.y, a, b ) ); }
inline uint2 clamp( const uint2& v, const uint2& a, const uint2& b ) { return make_uint2( clamp( v.x, a.x, b.x ), clamp( v.y, a.y, b.y ) ); }
inline uint3 clamp( const uint3& v, uint a, uint b ) { return make_uint3( clamp( v.x, a, b ), clamp( v.y, a, b ), clamp( v.z, a, b ) ); }
inline uint3 clamp( const uint3& v, const uint3& a, const uint3& b ) { return make_uint3( clamp( v.x, a.x, b.x ), clamp( v.y, a.y, b.y ), clamp( v.z, a.z, b.z ) ); }
inline uint4 clamp( const uint4& v, uint a, uint b ) { return make_uint4( clamp( v.x, a, b ), clamp( v.y, a, b ), clamp( v.z, a, b ), clamp( v.w, a, b ) ); }
inline uint4 clamp( const uint4& v, const uint4& a, const uint4& b ) { return make_uint4( clamp( v.x, a.x, b.x ), clamp( v.y, a.y, b.y ), clamp( v.z, a.z, b.z ), clamp( v.w, a.w, b.w ) ); }
inline float dot( const float2& a, const float2& b ) { return a.x * b.x + a.y * b.y; }
inline float dot( const float3& a, const float3& b ) { return a.x * b.x + a.y * b.y + a.z * b.z; }
inline float dot( const float4& a, const float4& b ) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; }
inline int dot( const int2& a, const int2& b ) { return a.x * b.x + a.y * b.y; }
inline int dot( const int3& a, const int3& b ) { return a.x * b.x + a.y * b.y + a.z * b.z; }
inline int dot( const int4& a, const int4& b ) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; }
inline uint dot( const uint2& a, const uint2& b ) { return a.x * b.x + a.y * b.y; }
inline uint dot( const uint3& a, const uint3& b ) { return a.x * b.x + a.y * b.y + a.z * b.z; }
inline uint dot( const uint4& a, const uint4& b ) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; }
inline float sqrLength( const float2& v ) { return dot( v, v ); }
inline float sqrLength( const float3& v ) { return dot( v, v ); }
inline float sqrLength( const float4& v ) { return dot( v, v ); }
inline float length( const float2& v ) { return sqrtf( dot( v, v ) ); }
inline float length( const float3& v ) { return sqrtf( dot( v, v ) ); }
inline float length( const float4& v ) { return sqrtf( dot( v, v ) ); }
inline float length( const int2& v ) { return sqrtf( (float)dot( v, v ) ); }
inline float length( const int3& v ) { return sqrtf( (float)dot( v, v ) ); }
inline float length( const int4& v ) { return sqrtf( (float)dot( v, v ) ); }
inline float2 normalize( const float2& v ) { float invLen = rsqrtf( dot( v, v ) ); return v * invLen; }
inline float3 normalize( const float3& v ) { float invLen = rsqrtf( dot( v, v ) ); return v * invLen; }
inline float4 normalize( const float4& v ) { float invLen = rsqrtf( dot( v, v ) ); return v * invLen; }
inline uint dominantAxis( const float2& v ) { float x = fabs( v.x ), y = fabs( v.y ); return x > y ? 0 : 1; } // for coherent grid traversal
inline uint dominantAxis( const float3& v ) { float x = fabs( v.x ), y = fabs( v.y ), z = fabs( v.z ); float m = max( max( x, y ), z ); return m == x ? 0 : (m == y ? 1 : 2); }
inline float2 floorf( const float2& v ) { return make_float2( floorf( v.x ), floorf( v.y ) ); }
inline float3 floorf( const float3& v ) { return make_float3( floorf( v.x ), floorf( v.y ), floorf( v.z ) ); }
inline float4 floorf( const float4& v ) { return make_float4( floorf( v.x ), floorf( v.y ), floorf( v.z ), floorf( v.w ) ); }
inline float fracf( float v ) { return v - floorf( v ); }
inline float2 fracf( const float2& v ) { return make_float2( fracf( v.x ), fracf( v.y ) ); }
inline float3 fracf( const float3& v ) { return make_float3( fracf( v.x ), fracf( v.y ), fracf( v.z ) ); }
inline float4 fracf( const float4& v ) { return make_float4( fracf( v.x ), fracf( v.y ), fracf( v.z ), fracf( v.w ) ); }
inline float2 fmodf( const float2& a, const float2& b ) { return make_float2( fmodf( a.x, b.x ), fmodf( a.y, b.y ) ); }
inline float3 fmodf( const float3& a, const float3& b ) { return make_float3( fmodf( a.x, b.x ), fmodf( a.y, b.y ), fmodf( a.z, b.z ) ); }
inline float4 fmodf( const float4& a, const float4& b ) { return make_float4( fmodf( a.x, b.x ), fmodf( a.y, b.y ), fmodf( a.z, b.z ), fmodf( a.w, b.w ) ); }
inline float2 fabs( const float2& v ) { return make_float2( fabs( v.x ), fabs( v.y ) ); }
inline float3 fabs( const float3& v ) { return make_float3( fabs( v.x ), fabs( v.y ), fabs( v.z ) ); }
inline float4 fabs( const float4& v ) { return make_float4( fabs( v.x ), fabs( v.y ), fabs( v.z ), fabs( v.w ) ); }
inline int2 abs( const int2& v ) { return make_int2( abs( v.x ), abs( v.y ) ); }
inline int3 abs( const int3& v ) { return make_int3( abs( v.x ), abs( v.y ), abs( v.z ) ); }
inline int4 abs( const int4& v ) { return make_int4( abs( v.x ), abs( v.y ), abs( v.z ), abs( v.w ) ); }
inline float3 reflect( const float3& i, const float3& n ) { return i - 2.0f * n * dot( n, i ); }
inline float3 cross( const float3& a, const float3& b ) { return make_float3( a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x ); }
inline float smoothstep( float a, float b, float x )
{
float y = clamp( (x - a) / (b - a), 0.0f, 1.0f );
return (y * y * (3.0f - (2.0f * y)));
}
inline float2 smoothstep( float2 a, float2 b, float2 x )
{
float2 y = clamp( (x - a) / (b - a), 0.0f, 1.0f );
return (y * y * (make_float2( 3.0f ) - (make_float2( 2.0f ) * y)));
}
inline float3 smoothstep( float3 a, float3 b, float3 x )
{
float3 y = clamp( (x - a) / (b - a), 0.0f, 1.0f );
return (y * y * (make_float3( 3.0f ) - (make_float3( 2.0f ) * y)));
}
inline float4 smoothstep( float4 a, float4 b, float4 x )
{
float4 y = clamp( (x - a) / (b - a), 0.0f, 1.0f );
return (y * y * (make_float4( 3.0f ) - (make_float4( 2.0f ) * y)));
}
// matrix class
class mat4
{
public:
mat4() = default;
float cell[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
float& operator [] ( const int idx ) { return cell[idx]; }
float operator()( const int i, const int j ) const { return cell[i * 4 + j]; }
float& operator()( const int i, const int j ) { return cell[i * 4 + j]; }
mat4& operator += ( const mat4& a )
{
for (int i = 0; i < 16; i++) cell[i] += a.cell[i];
return *this;
}
bool operator==( const mat4& m )
{
for (int i = 0; i < 16; i++) if (m.cell[i] != cell[i]) return false; return true;
}
float3 GetTranslation() const { return make_float3( cell[3], cell[7], cell[11] ); }
static mat4 FromColumnMajor( const mat4& T )
{
mat4 M;
M.cell[0] = T.cell[0], M.cell[1] = T.cell[4], M.cell[2] = T.cell[8], M.cell[3] = T.cell[12];
M.cell[4] = T.cell[1], M.cell[5] = T.cell[5], M.cell[6] = T.cell[9], M.cell[7] = T.cell[13];
M.cell[8] = T.cell[2], M.cell[9] = T.cell[6], M.cell[10] = T.cell[10], M.cell[11] = T.cell[14];
M.cell[12] = T.cell[3], M.cell[13] = T.cell[7], M.cell[14] = T.cell[11], M.cell[15] = T.cell[15];
return M;
}
constexpr static mat4 Identity() { return mat4{}; }
static mat4 ZeroMatrix() { mat4 r; memset( r.cell, 0, 64 ); return r; }
static mat4 RotateX( const float a ) { mat4 r; r.cell[5] = cosf( a ); r.cell[6] = -sinf( a ); r.cell[9] = sinf( a ); r.cell[10] = cosf( a ); return r; };
static mat4 RotateY( const float a ) { mat4 r; r.cell[0] = cosf( a ); r.cell[2] = sinf( a ); r.cell[8] = -sinf( a ); r.cell[10] = cosf( a ); return r; };
static mat4 RotateZ( const float a ) { mat4 r; r.cell[0] = cosf( a ); r.cell[1] = -sinf( a ); r.cell[4] = sinf( a ); r.cell[5] = cosf( a ); return r; };
static mat4 Scale( const float s ) { mat4 r; r.cell[0] = r.cell[5] = r.cell[10] = s; return r; }
static mat4 Scale( const float3 s ) { mat4 r; r.cell[0] = s.x, r.cell[5] = s.y, r.cell[10] = s.z; return r; }
static mat4 Scale( const float4 s ) { mat4 r; r.cell[0] = s.x, r.cell[5] = s.y, r.cell[10] = s.z, r.cell[15] = s.w; return r; }
static mat4 Rotate( const float3& u, const float a ) { return Rotate( u.x, u.y, u.z, a ); }
static mat4 Rotate( const float x, const float y, const float z, const float a )
{
const float c = cosf( a ), l_c = 1 - c, s = sinf( a );
// row major
mat4 m;
m[0] = x * x + (1 - x * x) * c, m[1] = x * y * l_c + z * s, m[2] = x * z * l_c - y * s, m[3] = 0;
m[4] = x * y * l_c - z * s, m[5] = y * y + (1 - y * y) * c, m[6] = y * z * l_c + x * s, m[7] = 0;
m[8] = x * z * l_c + y * s, m[9] = y * z * l_c - x * s, m[10] = z * z + (1 - z * z) * c, m[11] = 0;
m[12] = m[13] = m[14] = 0, m[15] = 1;
return m;
}
static mat4 LookAt( const float3 P, const float3 T )
{
const float3 z = normalize( T - P );
const float3 x = normalize( cross( z, make_float3( 0, 1, 0 ) ) );
const float3 y = cross( x, z );
mat4 M = Translate( P );
M[0] = x.x, M[4] = x.y, M[8] = x.z;
M[1] = y.x, M[5] = y.y, M[9] = y.z;
M[2] = z.x, M[6] = z.y, M[10] = z.z;
return M;
}
static mat4 LookAt( const float3& pos, const float3& look, const float3& up )
{
// PBRT's lookat
mat4 cameraToWorld;
// initialize fourth column of viewing matrix
cameraToWorld( 0, 3 ) = pos.x;
cameraToWorld( 1, 3 ) = pos.y;
cameraToWorld( 2, 3 ) = pos.z;
cameraToWorld( 3, 3 ) = 1;
// initialize first three columns of viewing matrix
float3 dir = normalize( look - pos );
float3 right = cross( normalize( up ), dir );
if (dot( right, right ) == 0)
{
printf(
"\"up\" vector (%f, %f, %f) and viewing direction (%f, %f, %f) "
"passed to LookAt are pointing in the same direction. Using "
"the identity transformation.\n",
up.x, up.y, up.z, dir.x, dir.y, dir.z );
return mat4();
}
right = normalize( right );
float3 newUp = cross( dir, right );
cameraToWorld( 0, 0 ) = right.x, cameraToWorld( 1, 0 ) = right.y;
cameraToWorld( 2, 0 ) = right.z, cameraToWorld( 3, 0 ) = 0.;
cameraToWorld( 0, 1 ) = newUp.x, cameraToWorld( 1, 1 ) = newUp.y;
cameraToWorld( 2, 1 ) = newUp.z, cameraToWorld( 3, 1 ) = 0.;
cameraToWorld( 0, 2 ) = dir.x, cameraToWorld( 1, 2 ) = dir.y;
cameraToWorld( 2, 2 ) = dir.z, cameraToWorld( 3, 2 ) = 0.;
return cameraToWorld.Inverted();
}
static mat4 Translate( const float x, const float y, const float z ) { mat4 r; r.cell[3] = x; r.cell[7] = y; r.cell[11] = z; return r; };
static mat4 Translate( const float3 P ) { mat4 r; r.cell[3] = P.x; r.cell[7] = P.y; r.cell[11] = P.z; return r; };
float Trace3() const { return cell[0] + cell[5] + cell[10]; }
CHECK_RESULT mat4 Transposed() const
{
mat4 M;
M[0] = cell[0], M[1] = cell[4], M[2] = cell[8];
M[4] = cell[1], M[5] = cell[5], M[6] = cell[9];
M[8] = cell[2], M[9] = cell[6], M[10] = cell[10];
return M;
}
CHECK_RESULT mat4 Inverted() const
{
// from MESA, via http://stackoverflow.com/questions/1148309/inverting-a-4x4-matrix
const float inv[16] = {
cell[5] * cell[10] * cell[15] - cell[5] * cell[11] * cell[14] - cell[9] * cell[6] * cell[15] +
cell[9] * cell[7] * cell[14] + cell[13] * cell[6] * cell[11] - cell[13] * cell[7] * cell[10],
-cell[1] * cell[10] * cell[15] + cell[1] * cell[11] * cell[14] + cell[9] * cell[2] * cell[15] -
cell[9] * cell[3] * cell[14] - cell[13] * cell[2] * cell[11] + cell[13] * cell[3] * cell[10],
cell[1] * cell[6] * cell[15] - cell[1] * cell[7] * cell[14] - cell[5] * cell[2] * cell[15] +
cell[5] * cell[3] * cell[14] + cell[13] * cell[2] * cell[7] - cell[13] * cell[3] * cell[6],
-cell[1] * cell[6] * cell[11] + cell[1] * cell[7] * cell[10] + cell[5] * cell[2] * cell[11] -
cell[5] * cell[3] * cell[10] - cell[9] * cell[2] * cell[7] + cell[9] * cell[3] * cell[6],
-cell[4] * cell[10] * cell[15] + cell[4] * cell[11] * cell[14] + cell[8] * cell[6] * cell[15] -
cell[8] * cell[7] * cell[14] - cell[12] * cell[6] * cell[11] + cell[12] * cell[7] * cell[10],
cell[0] * cell[10] * cell[15] - cell[0] * cell[11] * cell[14] - cell[8] * cell[2] * cell[15] +
cell[8] * cell[3] * cell[14] + cell[12] * cell[2] * cell[11] - cell[12] * cell[3] * cell[10],
-cell[0] * cell[6] * cell[15] + cell[0] * cell[7] * cell[14] + cell[4] * cell[2] * cell[15] -
cell[4] * cell[3] * cell[14] - cell[12] * cell[2] * cell[7] + cell[12] * cell[3] * cell[6],
cell[0] * cell[6] * cell[11] - cell[0] * cell[7] * cell[10] - cell[4] * cell[2] * cell[11] +
cell[4] * cell[3] * cell[10] + cell[8] * cell[2] * cell[7] - cell[8] * cell[3] * cell[6],
cell[4] * cell[9] * cell[15] - cell[4] * cell[11] * cell[13] - cell[8] * cell[5] * cell[15] +
cell[8] * cell[7] * cell[13] + cell[12] * cell[5] * cell[11] - cell[12] * cell[7] * cell[9],
-cell[0] * cell[9] * cell[15] + cell[0] * cell[11] * cell[13] + cell[8] * cell[1] * cell[15] -
cell[8] * cell[3] * cell[13] - cell[12] * cell[1] * cell[11] + cell[12] * cell[3] * cell[9],
cell[0] * cell[5] * cell[15] - cell[0] * cell[7] * cell[13] - cell[4] * cell[1] * cell[15] +
cell[4] * cell[3] * cell[13] + cell[12] * cell[1] * cell[7] - cell[12] * cell[3] * cell[5],
-cell[0] * cell[5] * cell[11] + cell[0] * cell[7] * cell[9] + cell[4] * cell[1] * cell[11] -
cell[4] * cell[3] * cell[9] - cell[8] * cell[1] * cell[7] + cell[8] * cell[3] * cell[5],
-cell[4] * cell[9] * cell[14] + cell[4] * cell[10] * cell[13] + cell[8] * cell[5] * cell[14] -
cell[8] * cell[6] * cell[13] - cell[12] * cell[5] * cell[10] + cell[12] * cell[6] * cell[9],
cell[0] * cell[9] * cell[14] - cell[0] * cell[10] * cell[13] - cell[8] * cell[1] * cell[14] +
cell[8] * cell[2] * cell[13] + cell[12] * cell[1] * cell[10] - cell[12] * cell[2] * cell[9],
-cell[0] * cell[5] * cell[14] + cell[0] * cell[6] * cell[13] + cell[4] * cell[1] * cell[14] -
cell[4] * cell[2] * cell[13] - cell[12] * cell[1] * cell[6] + cell[12] * cell[2] * cell[5],
cell[0] * cell[5] * cell[10] - cell[0] * cell[6] * cell[9] - cell[4] * cell[1] * cell[10] +
cell[4] * cell[2] * cell[9] + cell[8] * cell[1] * cell[6] - cell[8] * cell[2] * cell[5]
};
const float det = cell[0] * inv[0] + cell[1] * inv[4] + cell[2] * inv[8] + cell[3] * inv[12];
mat4 retVal;
if (det != 0)
{
const float invdet = 1.0f / det;
for (int i = 0; i < 16; i++) retVal.cell[i] = inv[i] * invdet;
}
return retVal;
}
CHECK_RESULT mat4 Inverted3x3() const
{
// via https://stackoverflow.com/questions/983999/simple-3x3-matrix-inverse-code-c
const float invdet = 1.0f / (cell[0] * (cell[5] * cell[10] - cell[6] * cell[9]) -
cell[4] * (cell[1] * cell[10] - cell[9] * cell[2]) +
cell[8] * (cell[1] * cell[6] - cell[5] * cell[2]));
mat4 R;
R.cell[0] = (cell[5] * cell[10] - cell[6] * cell[9]) * invdet;
R.cell[4] = (cell[8] * cell[6] - cell[4] * cell[10]) * invdet;
R.cell[8] = (cell[4] * cell[9] - cell[8] * cell[5]) * invdet;
R.cell[1] = (cell[9] * cell[2] - cell[1] * cell[10]) * invdet;
R.cell[5] = (cell[0] * cell[10] - cell[8] * cell[2]) * invdet;
R.cell[9] = (cell[1] * cell[8] - cell[0] * cell[9]) * invdet;
R.cell[2] = (cell[1] * cell[6] - cell[2] * cell[5]) * invdet;
R.cell[6] = (cell[2] * cell[4] - cell[0] * cell[6]) * invdet;
R.cell[10] = (cell[0] * cell[5] - cell[1] * cell[4]) * invdet;
return R;
}
inline float3 TransformVector( const float3& v ) const
{
return make_float3( cell[0] * v.x + cell[1] * v.y + cell[2] * v.z,
cell[4] * v.x + cell[5] * v.y + cell[6] * v.z,
cell[8] * v.x + cell[9] * v.y + cell[10] * v.z );
}
inline float3 TransformPoint( const float3& v ) const
{
const float3 res = make_float3(
cell[0] * v.x + cell[1] * v.y + cell[2] * v.z + cell[3],