summaryrefslogtreecommitdiffstats
path: root/Common/interface/BasicMath.hpp
blob: 9b882a469255fa3dbd4a425ddbc18d5d093a73e2 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
/*
 *  Copyright 2019-2021 Diligent Graphics LLC
 *  Copyright 2015-2019 Egor Yusov
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  In no event and under no legal theory, whether in tort (including negligence), 
 *  contract, or otherwise, unless required by applicable law (such as deliberate 
 *  and grossly negligent acts) or agreed to in writing, shall any Contributor be
 *  liable for any damages, including any direct, indirect, special, incidental, 
 *  or consequential damages of any character arising as a result of this License or 
 *  out of the use or inability to use the software (including but not limited to damages 
 *  for loss of goodwill, work stoppage, computer failure or malfunction, or any and 
 *  all other commercial damages or losses), even if such Contributor has been advised 
 *  of the possibility of such damages.
 */

#pragma once

/// The library uses Direct3D-style math:
///
///   - Matrices are multiplied left to right in the order corresponding transforms are applied:
///     - WorldViewProj = World * View * Proj
///   - Vectors are row-vectors and multiplied by matrices as v * m:
///     - ClipPos = WorldPos * WorldViewProj
///   - Matrices are stored using row-major layout: m = {row0, row1, row2, row3}
///     - Note that GL-style math libraries use column-vectors and column-major matrix layout.
///       As a result, matrices that perform similar transforms use exactly the same element
///       order. However, matrix multiplication order is reversed: M1_D3D * M2_D3D = M2_GL * M1_GL
///
///  Diligent Engine shaders always use column-major matrices for the purposes of data storage. This means
///  that if you use D3D-style math in shaders (ClipPos = mul(WorldPos, WorldViewProj)), you need to
///  transpose the host-side matrix before writing it to GPU memory.
///
///  If you use GL-style math in shaders (ClipPos = mul(WorldViewProj, WorldPos)), you do not need to
///  transpose the host-side matrix and should write it to GPU memory as is. Since the matrix rows will
///  be written to the GPU matrix columns, this will have the effect of transposing the matrix.
///  Since mul(WorldViewProj, WorldPos) == mul(WorldPos, transpose(WorldViewProj)), the results will
///  be consistent with D3D case.

#include <cmath>
#include <algorithm>
#include <iostream>

#include "HashUtils.hpp"

#ifdef _MSC_VER
#    pragma warning(push)
#    pragma warning(disable : 4201) // nonstandard extension used: nameless struct/union
#endif

namespace Diligent
{

static constexpr double PI   = 3.14159265358979323846;
static constexpr float  PI_F = 3.1415927f;

// Template Vector & Matrix Classes
template <class T> struct Matrix2x2;
template <class T> struct Matrix3x3;
template <class T> struct Matrix4x4;
template <class T> struct Vector4;

template <class T> struct Vector2
{
    union
    {
        struct
        {
            T x;
            T y;
        };
        struct
        {
            T r;
            T g;
        };
        struct
        {
            T u;
            T v;
        };
    };


    Vector2 operator-(const Vector2<T>& right) const
    {
        return Vector2{x - right.x, y - right.y};
    }

    Vector2& operator-=(const Vector2<T>& right)
    {
        x -= right.x;
        y -= right.y;
        return *this;
    }

    Vector2 operator-() const
    {
        return Vector2{-x, -y};
    }

    Vector2 operator+(const Vector2<T>& right) const
    {
        return Vector2{x + right.x, y + right.y};
    }

    Vector2& operator+=(const Vector2<T>& right)
    {
        x += right.x;
        y += right.y;
        return *this;
    }

    Vector2 operator*(T s) const
    {
        return Vector2{x * s, y * s};
    }

    Vector2 operator*(const Vector2& right) const
    {
        return Vector2{x * right.x, y * right.y};
    }

    Vector2& operator*=(const Vector2& right)
    {
        x *= right.x;
        y *= right.y;
        return *this;
    }

    Vector2& operator*=(T s)
    {
        x *= s;
        y *= s;
        return *this;
    }

    Vector2 operator*(const Matrix2x2<T>& m) const
    {
        Vector2 out;
        out[0] = x * m[0][0] + y * m[1][0];
        out[1] = x * m[0][1] + y * m[1][1];
        return out;
    }

    Vector2 operator/(const Vector2& right) const
    {
        return Vector2{x / right.x, y / right.y};
    }

    Vector2& operator/=(const Vector2& right)
    {
        x /= right.x;
        y /= right.y;
        return *this;
    }

    Vector2 operator/(T s) const
    {
        return Vector2{x / s, y / s};
    }

    Vector2& operator/=(T s)
    {
        x /= s;
        y /= s;
        return *this;
    }

    bool operator==(const Vector2& right) const
    {
        return x == right.x && y == right.y;
    }

    bool operator!=(const Vector2& right) const
    {
        return !(*this == right);
    }

    Vector2 operator<(const Vector2& right) const
    {
        return Vector2{x < right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y < right.y ? static_cast<T>(1) : static_cast<T>(0)};
    }

    Vector2 operator>(const Vector2& right) const
    {
        return Vector2{x > right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y > right.y ? static_cast<T>(1) : static_cast<T>(0)};
    }

    Vector2 operator<=(const Vector2& right) const
    {
        return Vector2{x <= right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y <= right.y ? static_cast<T>(1) : static_cast<T>(0)};
    }

    Vector2 operator>=(const Vector2& right) const
    {
        return Vector2{x >= right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y >= right.y ? static_cast<T>(1) : static_cast<T>(0)};
    }

    T* Data() { return reinterpret_cast<T*>(this); }

    const T* Data() const { return reinterpret_cast<const T*>(this); }

    T& operator[](size_t index)
    {
        return Data()[index];
    }

    const T& operator[](size_t index) const
    {
        return Data()[index];
    }

    Vector2() :
        x{0}, y{0} {}
    Vector2(T _x, T _y) :
        x{_x}, y{_y} {}

    template <typename Y>
    static Vector2 MakeVector(const Y& vals)
    {
        return Vector2 //
            {
                static_cast<T>(vals[0]),
                static_cast<T>(vals[1]) //
            };
    }

    template <typename Y>
    Vector2<Y> Recast() const
    {
        return Vector2<Y>{static_cast<Y>(x),
                          static_cast<Y>(y)};
    }
};

template <class T>
Vector2<T> operator*(T s, const Vector2<T>& a)
{
    return a * s;
}


template <class T> struct Vector3
{
    union
    {
        struct
        {
            T x;
            T y;
            T z;
        };
        struct
        {
            T r;
            T g;
            T b;
        };
        struct
        {
            T u;
            T v;
            T w;
        };
    };


    Vector3 operator-(const Vector3& right) const
    {
        return Vector3{x - right.x, y - right.y, z - right.z};
    }

    Vector3 operator-() const
    {
        return Vector3{-x, -y, -z};
    }

    Vector3& operator-=(const Vector3<T>& right)
    {
        x -= right.x;
        y -= right.y;
        z -= right.z;
        return *this;
    }

    Vector3 operator+(const Vector3& right) const
    {
        return Vector3{x + right.x, y + right.y, z + right.z};
    }

    Vector3& operator+=(const Vector3<T>& right)
    {
        x += right.x;
        y += right.y;
        z += right.z;
        return *this;
    }

    Vector3 operator*(T s) const
    {
        return Vector3{x * s, y * s, z * s};
    }

    Vector3& operator*=(T s)
    {
        x *= s;
        y *= s;
        z *= s;
        return *this;
    }

    Vector3 operator*(const Vector3& right) const
    {
        return Vector3{x * right.x, y * right.y, z * right.z};
    }

    Vector3 operator*(const Matrix4x4<T>& m) const
    {
        Vector4<T> out4 = Vector4<T>(x, y, z, 1) * m;
        return Vector3{out4.x / out4.w, out4.y / out4.w, out4.z / out4.w};
    }

    Vector3& operator*=(const Vector3& right)
    {
        x *= right.x;
        y *= right.y;
        z *= right.z;
        return *this;
    }

    Vector3 operator*(const Matrix3x3<T>& m) const
    {
        Vector3 out;
        out[0] = x * m[0][0] + y * m[1][0] + z * m[2][0];
        out[1] = x * m[0][1] + y * m[1][1] + z * m[2][1];
        out[2] = x * m[0][2] + y * m[1][2] + z * m[2][2];
        return out;
    }

    Vector3 operator/(T s) const
    {
        return Vector3{x / s, y / s, z / s};
    }

    Vector3& operator/=(T s)
    {
        x /= s;
        y /= s;
        z /= s;
        return *this;
    }

    Vector3 operator/(const Vector3& right) const
    {
        return Vector3{x / right.x, y / right.y, z / right.z};
    }

    Vector3& operator/=(const Vector3& right)
    {
        x /= right.x;
        y /= right.y;
        z /= right.z;
        return *this;
    }

    bool operator==(const Vector3& right) const
    {
        return x == right.x && y == right.y && z == right.z;
    }

    bool operator!=(const Vector3& right) const
    {
        return !(*this == right);
    }

    Vector3 operator<(const Vector3& right) const
    {
        return Vector3{x < right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y < right.y ? static_cast<T>(1) : static_cast<T>(0),
                       z < right.z ? static_cast<T>(1) : static_cast<T>(0)};
    }

    Vector3 operator>(const Vector3& right) const
    {
        return Vector3{x > right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y > right.y ? static_cast<T>(1) : static_cast<T>(0),
                       z > right.z ? static_cast<T>(1) : static_cast<T>(0)};
    }

    Vector3 operator<=(const Vector3& right) const
    {
        return Vector3{x <= right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y <= right.y ? static_cast<T>(1) : static_cast<T>(0),
                       z <= right.z ? static_cast<T>(1) : static_cast<T>(0)};
    }

    Vector3 operator>=(const Vector3& right) const
    {
        return Vector3{x >= right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y >= right.y ? static_cast<T>(1) : static_cast<T>(0),
                       z >= right.z ? static_cast<T>(1) : static_cast<T>(0)};
    }

    T* Data() { return reinterpret_cast<T*>(this); }

    const T* Data() const { return reinterpret_cast<const T*>(this); }

    T& operator[](size_t index)
    {
        return Data()[index];
    }

    const T& operator[](size_t index) const
    {
        return Data()[index];
    }

    Vector3() :
        x{0}, y{0}, z{0} {}
    Vector3(T _x, T _y, T _z) :
        x{_x}, y{_y}, z{_z} {}

    template <typename Y>
    static Vector3 MakeVector(const Y& vals)
    {
        return Vector3 //
            {
                static_cast<T>(vals[0]),
                static_cast<T>(vals[1]),
                static_cast<T>(vals[2]) //
            };
    }

    template <typename Y>
    Vector3<Y> Recast() const
    {
        return Vector3<Y>{static_cast<Y>(x),
                          static_cast<Y>(y),
                          static_cast<Y>(z)};
    }

    operator Vector2<T>() const { return Vector2<T>(x, y); }
};

template <class T>
Vector3<T> operator*(T s, const Vector3<T>& a)
{
    return a * s;
}


template <class T> struct Vector4
{
    union
    {
        struct
        {
            T x;
            T y;
            T z;
            T w;
        };
        struct
        {
            T r;
            T g;
            T b;
            T a;
        };
    };

    Vector4 operator-(const Vector4& right) const
    {
        return Vector4{x - right.x, y - right.y, z - right.z, w - right.w};
    }

    Vector4 operator-() const
    {
        return Vector4{-x, -y, -z, -w};
    }

    Vector4& operator-=(const Vector4<T>& right)
    {
        x -= right.x;
        y -= right.y;
        z -= right.z;
        w -= right.w;
        return *this;
    }

    Vector4 operator+(const Vector4& right) const
    {
        return Vector4{x + right.x, y + right.y, z + right.z, w + right.w};
    }

    Vector4& operator+=(const Vector4<T>& right)
    {
        x += right.x;
        y += right.y;
        z += right.z;
        w += right.w;
        return *this;
    }

    Vector4 operator*(T s) const
    {
        return Vector4{x * s, y * s, z * s, w * s};
    }

    Vector4& operator*=(T s)
    {
        x *= s;
        y *= s;
        z *= s;
        w *= s;
        return *this;
    }

    Vector4 operator*(const Vector4& right) const
    {
        return Vector4{x * right.x, y * right.y, z * right.z, w * right.w};
    }

    Vector4& operator*=(const Vector4& right)
    {
        x *= right.x;
        y *= right.y;
        z *= right.z;
        w *= right.w;
        return *this;
    }

    Vector4 operator/(T s) const
    {
        return Vector4{x / s, y / s, z / s, w / s};
    }

    Vector4& operator/=(T s)
    {
        x /= s;
        y /= s;
        z /= s;
        w /= s;
        return *this;
    }

    Vector4 operator/(const Vector4& right) const
    {
        return Vector4{x / right.x, y / right.y, z / right.z, w / right.w};
    }

    Vector4& operator/=(const Vector4& right)
    {
        x /= right.x;
        y /= right.y;
        z /= right.z;
        w /= right.w;
        return *this;
    }

    bool operator==(const Vector4& right) const
    {
        return x == right.x && y == right.y && z == right.z && w == right.w;
    }

    bool operator!=(const Vector4& right) const
    {
        return !(*this == right);
    }

    Vector4 operator*(const Matrix4x4<T>& m) const
    {
        Vector4 out;
        out[0] = x * m[0][0] + y * m[1][0] + z * m[2][0] + w * m[3][0];
        out[1] = x * m[0][1] + y * m[1][1] + z * m[2][1] + w * m[3][1];
        out[2] = x * m[0][2] + y * m[1][2] + z * m[2][2] + w * m[3][2];
        out[3] = x * m[0][3] + y * m[1][3] + z * m[2][3] + w * m[3][3];
        return out;
    }

    Vector4& operator=(const Vector3<T>& v3)
    {
        x = v3.x;
        y = v3.y;
        z = v3.z;
        w = 1;
        return *this;
    }
    Vector4& operator=(const Vector4&) = default;

    Vector4 operator<(const Vector4& right) const
    {
        return Vector4{x < right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y < right.y ? static_cast<T>(1) : static_cast<T>(0),
                       z < right.z ? static_cast<T>(1) : static_cast<T>(0),
                       w < right.w ? static_cast<T>(1) : static_cast<T>(0)};
    }

    Vector4 operator>(const Vector4& right) const
    {
        return Vector4{x > right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y > right.y ? static_cast<T>(1) : static_cast<T>(0),
                       z > right.z ? static_cast<T>(1) : static_cast<T>(0),
                       w > right.w ? static_cast<T>(1) : static_cast<T>(0)};
    }

    Vector4 operator<=(const Vector4& right) const
    {
        return Vector4{x <= right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y <= right.y ? static_cast<T>(1) : static_cast<T>(0),
                       z <= right.z ? static_cast<T>(1) : static_cast<T>(0),
                       w <= right.w ? static_cast<T>(1) : static_cast<T>(0)};
    }

    Vector4 operator>=(const Vector4& right) const
    {
        return Vector4{x >= right.x ? static_cast<T>(1) : static_cast<T>(0),
                       y >= right.y ? static_cast<T>(1) : static_cast<T>(0),
                       z >= right.z ? static_cast<T>(1) : static_cast<T>(0),
                       w >= right.w ? static_cast<T>(1) : static_cast<T>(0)};
    }

    T* Data() { return reinterpret_cast<T*>(this); }

    const T* Data() const { return reinterpret_cast<const T*>(this); }

    T& operator[](size_t index)
    {
        return Data()[index];
    }

    const T& operator[](size_t index) const
    {
        return Data()[index];
    }

    Vector4() :
        x{0}, y{0}, z{0}, w{0} {}
    Vector4(T _x, T _y, T _z, T _w) :
        x{_x}, y{_y}, z{_z}, w{_w} {}
    Vector4(const Vector3<T>& v3, T _w) :
        x{v3.x}, y{v3.y}, z{v3.z}, w{_w} {}

    template <typename Y>
    static Vector4 MakeVector(const Y& vals)
    {
        return Vector4 //
            {
                static_cast<T>(vals[0]),
                static_cast<T>(vals[1]),
                static_cast<T>(vals[2]),
                static_cast<T>(vals[3]) //
            };
    }

    template <typename Y>
    Vector4<Y> Recast() const
    {
        return Vector4<Y>{static_cast<Y>(x),
                          static_cast<Y>(y),
                          static_cast<Y>(z),
                          static_cast<Y>(w)};
    }

    operator Vector3<T>() const
    {
        return Vector3<T>(x, y, z);
    }
};


template <class T>
Vector4<T> operator*(T s, const Vector4<T>& a)
{
    return a * s;
}


template <class T> struct Matrix2x2
{
    union
    {
        struct
        {
            T _11;
            T _12;
            T _21;
            T _22;
        };
        struct
        {
            T m00;
            T m01;
            T m10;
            T m11;
        };
        T m[2][2];
    };

    explicit Matrix2x2(T value) :
        // clang-format off
        _11{value}, _12{value},
        _21{value}, _22{value}
    // clang-format on
    {
    }

    Matrix2x2() :
        Matrix2x2{0} {}

    // clang-format off
    Matrix2x2(T i11, T i12,
              T i21, T i22) :
        _11{i11}, _12{i12},
        _21{i21}, _22{i22}
    // clang-format on
    {
    }

    template <typename Y>
    static Matrix2x2 MakeMatrix(const Y& vals)
    {
        return Matrix2x2 //
            {
                static_cast<T>(vals[0]), static_cast<T>(vals[1]),
                static_cast<T>(vals[2]), static_cast<T>(vals[3]) //
            };
    }

    bool operator==(const Matrix2x2& r) const
    {
        for (int i = 0; i < 2; ++i)
            for (int j = 0; j < 2; ++j)
                if ((*this)[i][j] != r[i][j])
                    return false;

        return true;
    }

    bool operator!=(const Matrix2x2& r) const
    {
        return !(*this == r);
    }

    T* operator[](size_t row)
    {
        return m[row];
    }

    const T* operator[](size_t row) const
    {
        return m[row];
    }

    T* Data() { return (*this)[0]; }

    const T* Data() const { return (*this)[0]; }


    Matrix2x2& operator*=(T s)
    {
        for (int i = 0; i < 4; ++i)
            (reinterpret_cast<T*>(this))[i] *= s;

        return *this;
    }

    Matrix2x2& operator*=(const Matrix2x2& right)
    {
        *this = Mul(*this, right);
        return *this;
    }

    Matrix2x2 Transpose() const
    {
        return Matrix2x2{
            _11, _21,
            _12, _22};
    }

    static Matrix2x2 Identity()
    {
        return Matrix2x2{
            1, 0,
            0, 1};
    }

    static Matrix2x2 Mul(const Matrix2x2& m1, const Matrix2x2& m2)
    {
        Matrix2x2 mOut;
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                for (int k = 0; k < 2; k++)
                {
                    mOut.m[i][j] += m1.m[i][k] * m2.m[k][j];
                }
            }
        }
        return mOut;
    }

    static Matrix2x2 Rotation(T angleInRadians)
    {
        auto s = std::sin(angleInRadians);
        auto c = std::cos(angleInRadians);

        return Matrix2x2 //
            {
                c, s,
                -s, c //
            };
    }

    T Determinant() const
    {
        return _11 * _22 - _12 * _21;
    }

    Matrix2x2 Inverse() const
    {
        Matrix2x2 Inv //
            {
                +_22, -_12,
                -_21, +_11 //
            };

        Inv *= static_cast<T>(1) / Determinant();
        return Inv;
    }
};


template <class T> struct Matrix3x3
{
    union
    {
        struct
        {
            T _11;
            T _12;
            T _13;
            T _21;
            T _22;
            T _23;
            T _31;
            T _32;
            T _33;
        };
        struct
        {
            T m00;
            T m01;
            T m02;
            T m10;
            T m11;
            T m12;
            T m20;
            T m21;
            T m22;
        };
        T m[3][3];
    };

    explicit Matrix3x3(T value) :
        // clang-format off
        _11{value}, _12{value}, _13{value},
        _21{value}, _22{value}, _23{value},
        _31{value}, _32{value}, _33{value}
    // clang-format on
    {
    }

    Matrix3x3() :
        Matrix3x3{0} {}

    // clang-format off
    Matrix3x3(T i11, T i12, T i13,
              T i21, T i22, T i23,
              T i31, T i32, T i33) :
        _11{i11}, _12{i12}, _13{i13}, 
        _21{i21}, _22{i22}, _23{i23}, 
        _31{i31}, _32{i32}, _33{i33}
    // clang-format on
    {
    }

    template <typename Y>
    static Matrix3x3 MakeMatrix(const Y& vals)
    {
        return Matrix3x3 //
            {
                static_cast<T>(vals[0]), static_cast<T>(vals[1]), static_cast<T>(vals[2]),
                static_cast<T>(vals[3]), static_cast<T>(vals[4]), static_cast<T>(vals[5]),
                static_cast<T>(vals[6]), static_cast<T>(vals[7]), static_cast<T>(vals[8]) //
            };
    }

    bool operator==(const Matrix3x3& r) const
    {
        for (int i = 0; i < 3; ++i)
            for (int j = 0; j < 3; ++j)
                if ((*this)[i][j] != r[i][j])
                    return false;

        return true;
    }

    bool operator!=(const Matrix3x3& r) const
    {
        return !(*this == r);
    }

    T* operator[](size_t row)
    {
        return m[row];
    }

    const T* operator[](size_t row) const
    {
        return m[row];
    }

    T* Data() { return (*this)[0]; }

    const T* Data() const { return (*this)[0]; }

    Matrix3x3& operator*=(T s)
    {
        for (int i = 0; i < 9; ++i)
            (reinterpret_cast<T*>(this))[i] *= s;

        return *this;
    }

    Matrix3x3& operator*=(const Matrix3x3& right)
    {
        *this = Mul(*this, right);
        return *this;
    }

    Matrix3x3 Transpose() const
    {
        return Matrix3x3 //
            {
                _11, _21, _31,
                _12, _22, _32,
                _13, _23, _33 //
            };
    }

    static Matrix3x3 Identity()
    {
        return Matrix3x3 //
            {
                1, 0, 0,
                0, 1, 0,
                0, 0, 1 //
            };
    }

    static Matrix3x3 Scale(T x, T y, T z)
    {
        return Matrix3x3 //
            {
                x, 0, 0,
                0, y, 0,
                0, 0, z //
            };
    }

    // D3D-style left-handed matrix that rotates a point around the x axis. Angle (in radians)
    // is measured clockwise when looking along the rotation axis toward the origin:
    // (x' y' z') = (x y z) * RotationX
    static Matrix3x3 RotationX(T angleInRadians)
    {
        auto s = std::sin(angleInRadians);
        auto c = std::cos(angleInRadians);

        return Matrix3x3 // clang-format off
            {
                1,  0,  0,
                0,  c,  s,
                0, -s,  c // clang-format on
            };
    }

    // D3D-style left-handed matrix that rotates a point around the y axis. Angle (in radians)
    // is measured clockwise when looking along the rotation axis toward the origin:
    // (x' y' z' 1) = (x y z 1) * RotationY
    static Matrix3x3 RotationY(T angleInRadians)
    {
        auto s = std::sin(angleInRadians);
        auto c = std::cos(angleInRadians);

        return Matrix3x3 // clang-format off
            {
                c,  0, -s,
                0,  1,  0,
                s,  0,  c // clang-format on
            };
    }

    // D3D-style left-handed matrix that rotates a point around the z axis. Angle (in radians)
    // is measured clockwise when looking along the rotation axis toward the origin:
    // (x' y' z' 1) = (x y z 1) * RotationZ
    static Matrix3x3 RotationZ(T angleInRadians)
    {
        auto s = std::sin(angleInRadians);
        auto c = std::cos(angleInRadians);

        return Matrix3x3 // clang-format off
            {
                 c,  s,  0,
                -s,  c,  0,
                 0,  0,  1 // clang-format on
            };
    }

    static Matrix3x3 Mul(const Matrix3x3& m1, const Matrix3x3& m2)
    {
        Matrix3x3 mOut;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    mOut.m[i][j] += m1.m[i][k] * m2.m[k][j];
                }
            }
        }

        return mOut;
    }

    T Determinant() const
    {
        T det = 0;
        det += _11 * (_22 * _33 - _32 * _23);
        det -= _12 * (_21 * _33 - _31 * _23);
        det += _13 * (_21 * _32 - _31 * _22);
        return det;
    }
};

template <class T> struct Matrix4x4
{
    union
    {
        struct
        {
            T _11;
            T _12;
            T _13;
            T _14;
            T _21;
            T _22;
            T _23;
            T _24;
            T _31;
            T _32;
            T _33;
            T _34;
            T _41;
            T _42;
            T _43;
            T _44;
        };
        struct
        {
            T m00;
            T m01;
            T m02;
            T m03;
            T m10;
            T m11;
            T m12;
            T m13;
            T m20;
            T m21;
            T m22;
            T m23;
            T m30;
            T m31;
            T m32;
            T m33;
        };
        T m[4][4];
    };

    explicit Matrix4x4(T value) :
        // clang-format off
        _11{value}, _12{value}, _13{value}, _14{value},
        _21{value}, _22{value}, _23{value}, _24{value},
        _31{value}, _32{value}, _33{value}, _34{value},
        _41{value}, _42{value}, _43{value}, _44{value}
    // clang-format on
    {
    }

    Matrix4x4() :
        Matrix4x4{0} {}

    // clang-format off
    Matrix4x4(T i11, T i12, T i13, T i14,
              T i21, T i22, T i23, T i24,
              T i31, T i32, T i33, T i34,
              T i41, T i42, T i43, T i44) :
        _11{i11}, _12{i12}, _13{i13}, _14{i14}, 
        _21{i21}, _22{i22}, _23{i23}, _24{i24}, 
        _31{i31}, _32{i32}, _33{i33}, _34{i34}, 
        _41{i41}, _42{i42}, _43{i43}, _44{i44}
    {
    }
    // clang-format on

    // clang-format off
    Matrix4x4(const Vector4<T>& Row0,
              const Vector4<T>& Row1,
              const Vector4<T>& Row2,
              const Vector4<T>& Row3) :
        _11{Row0.x}, _12{Row0.y}, _13{Row0.z}, _14{Row0.w}, 
        _21{Row1.x}, _22{Row1.y}, _23{Row1.z}, _24{Row1.w}, 
        _31{Row2.x}, _32{Row2.y}, _33{Row2.z}, _34{Row2.w}, 
        _41{Row3.x}, _42{Row3.y}, _43{Row3.z}, _44{Row3.w}
    {
    }
    // clang-format on

    template <typename Y>
    static Matrix4x4 MakeMatrix(const Y& vals)
    {
        // clang-format off
        return Matrix4x4
            {
                static_cast<T>(vals[ 0]), static_cast<T>(vals[ 1]), static_cast<T>(vals[ 2]), static_cast<T>(vals[ 3]),
                static_cast<T>(vals[ 4]), static_cast<T>(vals[ 5]), static_cast<T>(vals[ 6]), static_cast<T>(vals[ 7]),
                static_cast<T>(vals[ 8]), static_cast<T>(vals[ 9]), static_cast<T>(vals[10]), static_cast<T>(vals[11]),
                static_cast<T>(vals[12]), static_cast<T>(vals[13]), static_cast<T>(vals[14]), static_cast<T>(vals[15])
            };
        // clang-format on
    }

    bool operator==(const Matrix4x4& r) const
    {
        for (int i = 0; i < 4; ++i)
            for (int j = 0; j < 4; ++j)
                if ((*this)[i][j] != r[i][j])
                    return false;

        return true;
    }

    bool operator!=(const Matrix4x4& r) const
    {
        return !(*this == r);
    }

    T* operator[](size_t row)
    {
        return m[row];
    }

    const T* operator[](size_t row) const
    {
        return m[row];
    }

    T* Data() { return (*this)[0]; }

    const T* Data() const { return (*this)[0]; }

    Matrix4x4& operator*=(T s)
    {
        for (int i = 0; i < 16; ++i)
            (reinterpret_cast<T*>(this))[i] *= s;

        return *this;
    }

    Matrix4x4& operator*=(const Matrix4x4& right)
    {
        *this = Mul(*this, right);
        return *this;
    }

    Matrix4x4 Transpose() const
    {
        return Matrix4x4 //
            {
                _11, _21, _31, _41,
                _12, _22, _32, _42,
                _13, _23, _33, _43,
                _14, _24, _34, _44 //
            };
    }

    static Matrix4x4 Identity()
    {
        return Matrix4x4 //
            {
                1, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                0, 0, 0, 1 //
            };
    }

    static Matrix4x4 Translation(T x, T y, T z)
    {
        return Matrix4x4 //
            {
                1, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                x, y, z, 1 //
            };
    }

    static Matrix4x4 Translation(const Vector3<T>& v)
    {
        return Translation(v.x, v.y, v.z);
    }

    static Matrix4x4 Scale(T x, T y, T z)
    {
        return Matrix4x4 //
            {
                x, 0, 0, 0,
                0, y, 0, 0,
                0, 0, z, 0,
                0, 0, 0, 1 //
            };
    }

    static Matrix4x4 Scale(const Vector3<T>& v)
    {
        return Scale(v.x, v.y, v.z);
    }

    static Matrix4x4 Scale(T s)
    {
        return Scale(s, s, s);
    }


    // D3D-style left-handed matrix that rotates a point around the x axis. Angle (in radians)
    // is measured clockwise when looking along the rotation axis toward the origin:
    // (x' y' z' 1) = (x y z 1) * RotationX
    static Matrix4x4 RotationX(T angleInRadians)
    {
        auto s = std::sin(angleInRadians);
        auto c = std::cos(angleInRadians);

        return Matrix4x4 // clang-format off
            {
                1,  0,  0,  0,
                0,  c,  s,  0,
                0, -s,  c,  0,
                0,  0,  0,  1 // clang-format on
            };
    }

    // D3D-style left-handed matrix that rotates a point around the y axis. Angle (in radians)
    // is measured clockwise when looking along the rotation axis toward the origin:
    // (x' y' z' 1) = (x y z 1) * RotationY
    static Matrix4x4 RotationY(T angleInRadians)
    {
        auto s = std::sin(angleInRadians);
        auto c = std::cos(angleInRadians);

        return Matrix4x4 // clang-format off
            {
                c,  0, -s,  0,
                0,  1,  0,  0,
                s,  0,  c,  0,
                0,  0,  0,  1 // clang-format on
            };
    }

    // D3D-style left-handed matrix that rotates a point around the z axis. Angle (in radians)
    // is measured clockwise when looking along the rotation axis toward the origin:
    // (x' y' z' 1) = (x y z 1) * RotationZ
    static Matrix4x4 RotationZ(T angleInRadians)
    {
        auto s = std::sin(angleInRadians);
        auto c = std::cos(angleInRadians);

        return Matrix4x4 // clang-format off
            {
                 c,  s,  0,  0,
                -s,  c,  0,  0,
                 0,  0,  1,  0,
                 0,  0,  0,  1 // clang-format on
            };
    }

    // 3D Rotation matrix for an arbitrary axis specified by x, y and z
    static Matrix4x4 RotationArbitrary(Vector3<T> axis, T angleInRadians)
    {
        axis = normalize(axis);

        auto sinAngle         = std::sin(angleInRadians);
        auto cosAngle         = std::cos(angleInRadians);
        auto oneMinusCosAngle = 1 - cosAngle;

        Matrix4x4 mOut;

        mOut._11 = 1 + oneMinusCosAngle * (axis.x * axis.x - 1);
        mOut._12 = axis.z * sinAngle + oneMinusCosAngle * axis.x * axis.y;
        mOut._13 = -axis.y * sinAngle + oneMinusCosAngle * axis.x * axis.z;
        mOut._41 = 0;

        mOut._21 = -axis.z * sinAngle + oneMinusCosAngle * axis.y * axis.x;
        mOut._22 = 1 + oneMinusCosAngle * (axis.y * axis.y - 1);
        mOut._23 = axis.x * sinAngle + oneMinusCosAngle * axis.y * axis.z;
        mOut._24 = 0;

        mOut._31 = axis.y * sinAngle + oneMinusCosAngle * axis.z * axis.x;
        mOut._32 = -axis.x * sinAngle + oneMinusCosAngle * axis.z * axis.y;
        mOut._33 = 1 + oneMinusCosAngle * (axis.z * axis.z - 1);
        mOut._34 = 0;

        mOut._41 = 0;
        mOut._42 = 0;
        mOut._43 = 0;
        mOut._44 = 1;

        return mOut;
    }

    static Matrix4x4 ViewFromBasis(const Vector3<T>& f3X, const Vector3<T>& f3Y, const Vector3<T>& f3Z)
    {
        return Matrix4x4 // clang-format off
            {
                f3X.x, f3Y.x, f3Z.x,   0,
                f3X.y, f3Y.y, f3Z.y,   0,
                f3X.z, f3Y.z, f3Z.z,   0,
                    0,     0,     0,   1 // clang-format on
            };
    }


    void SetNearFarClipPlanes(T zNear, T zFar, T bIsGL)
    {
        if (bIsGL)
        {
            // https://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml
            // http://www.terathon.com/gdc07_lengyel.pdf
            // Note that OpenGL uses right-handed coordinate system, where
            // camera is looking in negative z direction:
            //   OO
            //  |__|<--------------------
            //         -z             +z
            // Consequently, OpenGL projection matrix given by these two
            // references inverts z axis.

            // We do not need to do this, because we use DX coordinate
            // system for the camera space. Thus we need to invert the
            // sign of the values in the third column in the matrix
            // from the references:

            _33 = -(-(zFar + zNear) / (zFar - zNear));
            _43 = -2 * zNear * zFar / (zFar - zNear);
            _34 = -(-1);
        }
        else
        {
            _33 = zFar / (zFar - zNear);
            _43 = -zNear * zFar / (zFar - zNear);
            _34 = 1;
        }
    }

    void GetNearFarClipPlanes(T& zNear, T& zFar, bool bIsGL) const
    {
        if (bIsGL)
        {
            zNear = _43 / (-1 - _33);
            zFar  = _43 / (+1 - _33);
        }
        else
        {
            zNear = -_43 / _33;
            zFar  = _33 / (_33 - 1) * zNear;
        }
    }

    static Matrix4x4 Projection(T fov, T aspectRatio, T zNear, T zFar, bool bIsGL) // Left-handed projection
    {
        Matrix4x4 mOut;
        auto      yScale = static_cast<T>(1) / std::tan(fov / static_cast<T>(2));
        auto      xScale = yScale / aspectRatio;
        mOut._11         = xScale;
        mOut._22         = yScale;

        mOut.SetNearFarClipPlanes(zNear, zFar, bIsGL);

        return mOut;
    }

    static Matrix4x4 OrthoOffCenter(T left, T right, T bottom, T top, T zNear, T zFar, bool bIsGL) // Left-handed ortho projection
    {
        auto _22 = (bIsGL ? 2 : 1) / (zFar - zNear);
        auto _32 = (bIsGL ? zNear + zFar : zNear) / (zNear - zFar);
        // clang-format off
        return Matrix4x4
            {
                         2   / (right - left),                                 0,     0,    0,
                                            0,                2 / (top - bottom),     0,    0,
                                            0,                                 0,   _22,    0,                
                (left + right)/(left - right),   (top + bottom) / (bottom - top),   _32,    1
            };
        // clang-format on
    }

    static Matrix4x4 Ortho(T width, T height, T zNear, T zFar, bool bIsGL) // Left-handed ortho projection
    {
        return OrthoOffCenter(
            -width * static_cast<T>(0.5),
            +width * static_cast<T>(0.5),
            -height * static_cast<T>(0.5),
            +height * static_cast<T>(0.5),
            zNear, zFar, bIsGL);
    }

    static Matrix4x4 Mul(const Matrix4x4& m1, const Matrix4x4& m2)
    {
        Matrix4x4 mOut;
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    mOut.m[i][j] += m1.m[i][k] * m2.m[k][j];
                }
            }
        }
        return mOut;
    }


    T Determinant() const
    {
        T det = 0.f;

        det += _11 *
            Matrix3x3<T>(_22, _23, _24,
                         _32, _33, _34,
                         _42, _43, _44)
                .Determinant();

        det -= _12 *
            Matrix3x3<T>(_21, _23, _24,
                         _31, _33, _34,
                         _41, _43, _44)
                .Determinant();

        det += _13 *
            Matrix3x3<T>(_21, _22, _24,
                         _31, _32, _34,
                         _41, _42, _44)
                .Determinant();

        det -= _14 *
            Matrix3x3<T>(_21, _22, _23,
                         _31, _32, _33,
                         _41, _42, _43)
                .Determinant();

        return det;
    }

    Matrix4x4 Inverse() const
    {
        Matrix4x4 inv;

        // row 1
        inv._11 =
            Matrix3x3<T>(_22, _23, _24,
                         _32, _33, _34,
                         _42, _43, _44)
                .Determinant();

        inv._12 =
            -Matrix3x3<T>(_21, _23, _24,
                          _31, _33, _34,
                          _41, _43, _44)
                 .Determinant();

        inv._13 =
            Matrix3x3<T>(_21, _22, _24,
                         _31, _32, _34,
                         _41, _42, _44)
                .Determinant();

        inv._14 =
            -Matrix3x3<T>(_21, _22, _23,
                          _31, _32, _33,
                          _41, _42, _43)
                 .Determinant();


        // row 2
        inv._21 =
            -Matrix3x3<T>(_12, _13, _14,
                          _32, _33, _34,
                          _42, _43, _44)
                 .Determinant();

        inv._22 =
            Matrix3x3<T>(_11, _13, _14,
                         _31, _33, _34,
                         _41, _43, _44)
                .Determinant();

        inv._23 =
            -Matrix3x3<T>(_11, _12, _14,
                          _31, _32, _34,
                          _41, _42, _44)
                 .Determinant();

        inv._24 =
            Matrix3x3<T>(_11, _12, _13,
                         _31, _32, _33,
                         _41, _42, _43)
                .Determinant();


        // row 3
        inv._31 =
            Matrix3x3<T>(_12, _13, _14,
                         _22, _23, _24,
                         _42, _43, _44)
                .Determinant();

        inv._32 =
            -Matrix3x3<T>(_11, _13, _14,
                          _21, _23, _24,
                          _41, _43, _44)
                 .Determinant();

        inv._33 =
            Matrix3x3<T>(_11, _12, _14,
                         _21, _22, _24,
                         _41, _42, _44)
                .Determinant();

        inv._34 =
            -Matrix3x3<T>(_11, _12, _13,
                          _21, _22, _23,
                          _41, _42, _43)
                 .Determinant();


        // row 4
        inv._41 =
            -Matrix3x3<T>(_12, _13, _14,
                          _22, _23, _24,
                          _32, _33, _34)
                 .Determinant();

        inv._42 =
            Matrix3x3<T>(_11, _13, _14,
                         _21, _23, _24,
                         _31, _33, _34)
                .Determinant();

        inv._43 =
            -Matrix3x3<T>(_11, _12, _14,
                          _21, _22, _24,
                          _31, _32, _34)
                 .Determinant();

        inv._44 =
            Matrix3x3<T>(_11, _12, _13,
                         _21, _22, _23,
                         _31, _32, _33)
                .Determinant();

        auto det = _11 * inv._11 + _12 * inv._12 + _13 * inv._13 + _14 * inv._14;
        inv      = inv.Transpose();
        inv *= static_cast<T>(1) / det;

        return inv;
    }

    Matrix4x4 RemoveTranslation() const
    {
        return Matrix4x4 // clang-format off
            {
                _11, _12, _13, _14,
                _21, _22, _23, _24,
                _31, _32, _33, _34,
                  0,   0,   0, _44 // clang-format on
            };
    }
};

// Template Vector Operations


template <class T>
T dot(const Vector2<T>& a, const Vector2<T>& b)
{
    return a.x * b.x + a.y * b.y;
}

template <class T>
T dot(const Vector3<T>& a, const Vector3<T>& b)
{
    return a.x * b.x + a.y * b.y + a.z * b.z;
}

template <class T>
T dot(const Vector4<T>& a, const Vector4<T>& b)
{
    return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}

template <class VectorType>
auto length(const VectorType& a) -> decltype(dot(a, a))
{
    return sqrt(dot(a, a));
}


template <class T>
Vector3<T> min(const Vector3<T>& a, const Vector3<T>& b)
{
    return Vector3<T>(std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z));
}

template <class T>
Vector4<T> min(const Vector4<T>& a, const Vector4<T>& b)
{
    return Vector4<T>(std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z), std::min(a.w, b.w));
}

template <class T>
Vector3<T> max(const Vector3<T>& a, const Vector3<T>& b)
{
    return Vector3<T>(std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z));
}

template <class T>
Vector4<T> max(const Vector4<T>& a, const Vector4<T>& b)
{
    return Vector4<T>(std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z), std::max(a.w, b.w));
}

template <class T>
Vector2<T> abs(const Vector2<T>& a)
{
    // WARNING: abs() on gcc is for integers only!
    return Vector2<T>(a.x < 0 ? -a.x : a.x,
                      a.y < 0 ? -a.y : a.y);
}

template <class T>
Vector3<T> abs(const Vector3<T>& a)
{
    // WARNING: abs() on gcc is for integers only!
    return Vector3<T>(a.x < 0 ? -a.x : a.x,
                      a.y < 0 ? -a.y : a.y,
                      a.z < 0 ? -a.z : a.z);
}

template <class T>
Vector4<T> abs(const Vector4<T>& a)
{
    // WARNING: abs() on gcc is for integers only!
    return Vector4<T>(a.x < 0 ? -a.x : a.x,
                      a.y < 0 ? -a.y : a.y,
                      a.z < 0 ? -a.z : a.z,
                      a.w < 0 ? -a.w : a.w);
}


template <typename T>
T clamp(T val, T _min, T _max)
{
    return val < _min ? _min : (val > _max ? _max : val);
}

template <class T>
Vector2<T> clamp(const Vector2<T>& a, const Vector2<T>& _min, const Vector2<T>& _max)
{
    return Vector2<T>(clamp(a.x, _min.x, _max.x),
                      clamp(a.y, _min.y, _max.y));
}

template <class T>
Vector3<T> clamp(const Vector3<T>& a, const Vector3<T>& _min, const Vector3<T>& _max)
{
    return Vector3<T>(clamp(a.x, _min.x, _max.x),
                      clamp(a.y, _min.y, _max.y),
                      clamp(a.z, _min.z, _max.z));
}

template <class T>
Vector4<T> clamp(const Vector4<T>& a, const Vector4<T>& _min, const Vector4<T>& _max)
{
    return Vector4<T>(clamp(a.x, _min.x, _max.x),
                      clamp(a.y, _min.y, _max.y),
                      clamp(a.z, _min.z, _max.z),
                      clamp(a.w, _min.w, _max.w));
}


template <class T>
Vector3<T> cross(const Vector3<T>& a, const Vector3<T>& b)
{
    // |   i    j    k   |
    // |  a.x  a.y  a.z  |
    // |  b.x  b.y  b.z  |
    return Vector3<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));
}

template <class T, class Y>
Vector3<T> cross(const Vector3<T>& a, const Vector3<T>& b)
{
    // |   i    j    k   |
    // |  a.x  a.y  a.z  |
    // |  b.x  b.y  b.z  |
    return Vector3<T> //
        {
            static_cast<T>(Y{a.y} * Y{b.z} - Y{a.z} * Y{b.y}),
            static_cast<T>(Y{a.z} * Y{b.x} - Y{a.x} * Y{b.z}),
            static_cast<T>(Y{a.x} * Y{b.y} - Y{a.y} * Y{b.x}) //
        };
}

inline Vector3<float> high_precision_cross(const Vector3<float>& a, const Vector3<float>& b)
{
    return cross<float, double>(a, b);
}

inline Vector3<int32_t> high_precision_cross(const Vector3<int32_t>& a, const Vector3<int32_t>& b)
{
    return cross<int32_t, int64_t>(a, b);
}

template <class VectorType>
VectorType normalize(const VectorType& a)
{
    auto len = length(a);
    return a / len;
}


// Template Matrix-Matrix multiplications

template <class T>
Matrix4x4<T> operator*(const Matrix4x4<T>& m1, const Matrix4x4<T>& m2)
{
    return Matrix4x4<T>::Mul(m1, m2);
}

template <class T>
Matrix3x3<T> operator*(const Matrix3x3<T>& m1, const Matrix3x3<T>& m2)
{
    return Matrix3x3<T>::Mul(m1, m2);
}

template <class T>
Matrix2x2<T> operator*(const Matrix2x2<T>& m1, const Matrix2x2<T>& m2)
{
    return Matrix2x2<T>::Mul(m1, m2);
}


// Template Matrix-Vector multiplications

template <class T>
Vector4<T> operator*(const Matrix4x4<T>& m, const Vector4<T>& v)
{
    Vector4<T> out;
    out[0] = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w;
    out[1] = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w;
    out[2] = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w;
    out[3] = m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w;
    return out;
}

template <class T>
Vector3<T> operator*(const Matrix3x3<T>& m, Vector3<T>& v)
{
    Vector3<T> out;
    out[0] = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z;
    out[1] = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z;
    out[2] = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z;
    return out;
}

template <class T>
Vector2<T> operator*(const Matrix2x2<T>& m, const Vector2<T>& v)
{
    Vector2<T> out;
    out[0] = m[0][0] * v.x + m[0][1] * v.y;
    out[1] = m[1][0] * v.x + m[1][1] * v.y;
    return out;
}

// Common HLSL-compatible vector typedefs

using uint  = uint32_t;
using uint2 = Vector2<uint>;
using uint3 = Vector3<uint>;
using uint4 = Vector4<uint>;

using int2 = Vector2<int32_t>;
using int3 = Vector3<int32_t>;
using int4 = Vector4<int32_t>;

using float2 = Vector2<float>;
using float3 = Vector3<float>;
using float4 = Vector4<float>;

using double2 = Vector2<double>;
using double3 = Vector3<double>;
using double4 = Vector4<double>;

using float4x4 = Matrix4x4<float>;
using float3x3 = Matrix3x3<float>;
using float2x2 = Matrix2x2<float>;

using double4x4 = Matrix4x4<double>;
using double3x3 = Matrix3x3<double>;
using double2x2 = Matrix2x2<double>;


struct Quaternion
{
    float4 q;

    Quaternion(const float4& _q) noexcept :
        q{_q}
    {}
    Quaternion(float x, float y, float z, float w) noexcept :
        q{x, y, z, w}
    {
    }
    Quaternion() noexcept {}

    bool operator==(const Quaternion& right) const
    {
        return q == right.q;
    }

    template <typename Y>
    static Quaternion MakeQuaternion(const Y& vals)
    {
        return Quaternion{float4::MakeVector(vals)};
    }

    static Quaternion RotationFromAxisAngle(const float3& axis, float angle)
    {
        Quaternion out{0, 0, 0, 1};
        float      norm = length(axis);
        if (norm != 0)
        {
            float sina2 = sin(0.5f * angle);
            out.q[0]    = sina2 * axis[0] / norm;
            out.q[1]    = sina2 * axis[1] / norm;
            out.q[2]    = sina2 * axis[2] / norm;
            out.q[3]    = cos(0.5f * angle);
        }
        return out;
    }

    void GetAxisAngle(float3& outAxis, float& outAngle) const
    {
        float sina2 = sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2]);
        outAngle    = 2.0f * atan2(sina2, q[3]);
        float r     = (sina2 > 0) ? (1.0f / sina2) : 0;
        outAxis[0]  = r * q[0];
        outAxis[1]  = r * q[1];
        outAxis[2]  = r * q[2];
    }

    float4x4 ToMatrix() const
    {
        float4x4 out;
        float    yy2 = 2.0f * q[1] * q[1];
        float    xy2 = 2.0f * q[0] * q[1];
        float    xz2 = 2.0f * q[0] * q[2];
        float    yz2 = 2.0f * q[1] * q[2];
        float    zz2 = 2.0f * q[2] * q[2];
        float    wz2 = 2.0f * q[3] * q[2];
        float    wy2 = 2.0f * q[3] * q[1];
        float    wx2 = 2.0f * q[3] * q[0];
        float    xx2 = 2.0f * q[0] * q[0];
        out[0][0]    = -yy2 - zz2 + 1.0f;
        out[0][1]    = xy2 + wz2;
        out[0][2]    = xz2 - wy2;
        out[0][3]    = 0;
        out[1][0]    = xy2 - wz2;
        out[1][1]    = -xx2 - zz2 + 1.0f;
        out[1][2]    = yz2 + wx2;
        out[1][3]    = 0;
        out[2][0]    = xz2 + wy2;
        out[2][1]    = yz2 - wx2;
        out[2][2]    = -xx2 - yy2 + 1.0f;
        out[2][3]    = 0;
        out[3][0] = out[3][1] = out[3][2] = 0;
        out[3][3]                         = 1;
        return out;
    }

    static Quaternion Mul(const Quaternion& q1, const Quaternion& q2)
    {
        Quaternion q1_q2;
        q1_q2.q.x = +q1.q.x * q2.q.w + q1.q.y * q2.q.z - q1.q.z * q2.q.y + q1.q.w * q2.q.x;
        q1_q2.q.y = -q1.q.x * q2.q.z + q1.q.y * q2.q.w + q1.q.z * q2.q.x + q1.q.w * q2.q.y;
        q1_q2.q.z = +q1.q.x * q2.q.y - q1.q.y * q2.q.x + q1.q.z * q2.q.w + q1.q.w * q2.q.z;
        q1_q2.q.w = -q1.q.x * q2.q.x - q1.q.y * q2.q.y - q1.q.z * q2.q.z + q1.q.w * q2.q.w;
        return q1_q2;
    }

    Quaternion& operator=(const Quaternion& rhs)
    {
        q = rhs.q;
        return *this;
    }

    Quaternion& operator*=(const Quaternion& rhs)
    {
        *this = Mul(*this, rhs);
        return *this;
    }

    float3 RotateVector(const float3& v) const
    {
        const float3 axis(q.x, q.y, q.z);
        return v + 2.f * cross(axis, cross(axis, v) + q.w * v);
    }
};

inline Quaternion operator*(const Quaternion& q1, const Quaternion& q2)
{
    return Quaternion::Mul(q1, q2);
}

inline Quaternion normalize(const Quaternion& q)
{
    return Quaternion{normalize(q.q)};
}

// https://en.wikipedia.org/wiki/Slerp
inline Quaternion slerp(Quaternion v0, Quaternion v1, float t, bool DoNotNormalize = false)
{
    // Only unit quaternions are valid rotations.
    // Normalize to avoid undefined behavior.
    if (!DoNotNormalize)
    {
        v0 = normalize(v0);
        v1 = normalize(v1);
    }

    // Compute the cosine of the angle between the two vectors.
    auto dp = dot(v0.q, v1.q);

    // If the dot product is negative, slerp won't take
    // the shorter path. Note that v1 and -v1 are equivalent when
    // the negation is applied to all four components. Fix by
    // reversing one quaternion.
    if (dp < 0)
    {
        v1.q = -v1.q;
        dp   = -dp;
    }

    const double DOT_THRESHOLD = 0.9995;
    if (dp > DOT_THRESHOLD)
    {
        // If the inputs are too close for comfort, linearly interpolate
        // and normalize the result.

        Quaternion result = Quaternion{v0.q + t * (v1.q - v0.q)};
        result.q          = normalize(result.q);
        return result;
    }

    // Since dot is in range [0, DOT_THRESHOLD], acos is safe
    auto theta_0     = std::acos(dp);     // theta_0 = angle between input vectors
    auto theta       = theta_0 * t;       // theta = angle between v0 and result
    auto sin_theta   = std::sin(theta);   // compute this value only once
    auto sin_theta_0 = std::sin(theta_0); // compute this value only once

    auto s0 = cos(theta) - dp * sin_theta / sin_theta_0; // == sin(theta_0 - theta) / sin(theta_0)
    auto s1 = sin_theta / sin_theta_0;

    auto v = Quaternion{v0.q * s0 + v1.q * s1};
    if (!DoNotNormalize)
    {
        v = normalize(v);
    }
    return v;
}


template <typename T>
T lerp(const T& Left, const T& Right, float w)
{
    return Left * (1.f - w) + Right * w;
}

template <typename T>
T SmoothStep(T Left, T Right, T w)
{
    auto t = clamp((w - Left) / (Right - Left), static_cast<T>(0), static_cast<T>(1));
    return t * t * (static_cast<T>(3) - static_cast<T>(2) * t);
}

template <typename T>
T max3(const T& x, const T& y, const T& z)
{
    return std::max(std::max(x, y), z);
}

template <typename T>
T min3(const T& x, const T& y, const T& z)
{
    return std::min(std::min(x, y), z);
}

inline float4 RGBA8Unorm_To_F4Color(Uint32 RGBA8)
{
    // clang-format off
    return float4
    {
            static_cast<float>((RGBA8 >>  0u) & 0xFF) / 255.f,
            static_cast<float>((RGBA8 >>  8u) & 0xFF) / 255.f,
            static_cast<float>((RGBA8 >> 16u) & 0xFF) / 255.f,
            static_cast<float>((RGBA8 >> 24u) & 0xFF) / 255.f
    };
    // clang-format on
}

inline Uint32 F4Color_To_RGBA8Unorm(const float4& f4Color)
{
    Uint32 RGBA8U = 0;
    RGBA8U |= static_cast<Uint32>(clamp(f4Color.r, 0.f, 1.f) * 255.f) << 0u;
    RGBA8U |= static_cast<Uint32>(clamp(f4Color.g, 0.f, 1.f) * 255.f) << 8u;
    RGBA8U |= static_cast<Uint32>(clamp(f4Color.b, 0.f, 1.f) * 255.f) << 16u;
    RGBA8U |= static_cast<Uint32>(clamp(f4Color.a, 0.f, 1.f) * 255.f) << 24u;
    return RGBA8U;
}

template <typename T>
struct _FastFloatIntermediateType
{
};

template <>
struct _FastFloatIntermediateType<float>
{
    // All floats that have fractional part are representable as 32-bit int
    using Type = Int32;
};

template <>
struct _FastFloatIntermediateType<double>
{
    // All doubles that have fractional part are representable as 64-bit int
    using Type = Int64;
};

// At least on MSVC std::floor is an actual function call into ucrtbase.dll.
// All floats/doubles that have fractional parts also fit into integer
// representable range, so we can do much better.
template <typename T>
T FastFloor(T x)
{
    auto i   = static_cast<typename _FastFloatIntermediateType<T>::Type>(x);
    auto flr = static_cast<T>(i);
    //   x         flr    floor(x)  flr <= x
    // +1.0   ->   1.0      1.0       true
    // +0.5   ->   0.0      0.0       true
    //  0.0   ->   0.0      0.0       true
    // -0.5   ->   0.0     -1.0      false
    // -1.0   ->  -1.0     -1.0       true

    return flr <= x ? flr : flr - 1;
}

template <typename T>
T FastCeil(T x)
{
    return -FastFloor(-x);
}


template <typename T>
Diligent::Vector2<T> FastFloor(const Diligent::Vector2<T>& vec)
{
    return Diligent::Vector2<T>{
        FastFloor(vec.x),
        FastFloor(vec.y)};
}

template <typename T>
Diligent::Vector3<T> FastFloor(const Diligent::Vector3<T>& vec)
{
    return Diligent::Vector3<T>{
        FastFloor(vec.x),
        FastFloor(vec.y),
        FastFloor(vec.z)};
}

template <typename T>
Diligent::Vector4<T> FastFloor(const Diligent::Vector4<T>& vec)
{
    return Diligent::Vector4<T>{
        FastFloor(vec.x),
        FastFloor(vec.y),
        FastFloor(vec.z),
        FastFloor(vec.w)};
}


template <typename T>
Diligent::Vector2<T> FastCeil(const Diligent::Vector2<T>& vec)
{
    return Diligent::Vector2<T>{
        FastCeil(vec.x),
        FastCeil(vec.y)};
}

template <typename T>
Diligent::Vector3<T> FastCeil(const Diligent::Vector3<T>& vec)
{
    return Diligent::Vector3<T>{
        FastCeil(vec.x),
        FastCeil(vec.y),
        FastCeil(vec.z)};
}

template <typename T>
Diligent::Vector4<T> FastCeil(const Diligent::Vector4<T>& vec)
{
    return Diligent::Vector4<T>{
        FastCeil(vec.x),
        FastCeil(vec.y),
        FastCeil(vec.z),
        FastCeil(vec.w)};
}

inline Uint32 BitInterleave16(Uint16 _x, Uint16 _y)
{
    // https://graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN

    // Interleave lower 16 bits of x and y, so the bits of x
    // are in the even positions and bits from y in the odd;
    // x | (y << 1) gets the resulting 32-bit Morton Number.
    // x and y must initially be less than 65536.
    Uint32 x = _x;
    Uint32 y = _y;

    x = (x | (x << 8u)) & 0x00FF00FFu;
    x = (x | (x << 4u)) & 0x0F0F0F0Fu;
    x = (x | (x << 2u)) & 0x33333333u;
    x = (x | (x << 1u)) & 0x55555555u;

    y = (y | (y << 8u)) & 0x00FF00FFu;
    y = (y | (y << 4u)) & 0x0F0F0F0Fu;
    y = (y | (y << 2u)) & 0x33333333u;
    y = (y | (y << 1u)) & 0x55555555u;

    return x | (y << 1u);
}

/// Returns the least-signficant bit and clears it in the input argument
template <typename T>
typename std::enable_if<std::is_integral<T>::value, T>::type ExtractLSB(T& bits)
{
    if (bits == T{0})
        return 0;

    const T bit = bits & ~(bits - T{1});
    bits &= ~bit;

    return bit;
}

/// Returns the enum value representing the least-signficant bit and clears it in the input argument
template <typename T>
typename std::enable_if<std::is_enum<T>::value, T>::type ExtractLSB(T& bits)
{
    return static_cast<T>(ExtractLSB(reinterpret_cast<typename std::underlying_type<T>::type&>(bits)));
}


inline std::ostream& operator<<(std::ostream& os, const float4& vec)
{
    return os << "float4(" << vec.x << ", " << vec.y << ", " << vec.z << ", " << vec.w << ')';
}
inline std::ostream& operator<<(std::ostream& os, const float3& vec)
{
    return os << "float3(" << vec.x << ", " << vec.y << ", " << vec.z << ')';
}
inline std::ostream& operator<<(std::ostream& os, const float2& vec)
{
    return os << "float2(" << vec.x << ", " << vec.y << ')';
}

inline std::ostream& operator<<(std::ostream& os, const int4& vec)
{
    return os << "int4(" << vec.x << ", " << vec.y << ", " << vec.z << ", " << vec.w << ')';
}
inline std::ostream& operator<<(std::ostream& os, const int3& vec)
{
    return os << "int3(" << vec.x << ", " << vec.y << ", " << vec.z << ')';
}
inline std::ostream& operator<<(std::ostream& os, const int2& vec)
{
    return os << "int2(" << vec.x << ", " << vec.y << ')';
}


inline std::ostream& operator<<(std::ostream& os, const uint4& vec)
{
    return os << "uint4(" << vec.x << ", " << vec.y << ", " << vec.z << ", " << vec.w << ')';
}
inline std::ostream& operator<<(std::ostream& os, const uint3& vec)
{
    return os << "uint3(" << vec.x << ", " << vec.y << ", " << vec.z << ')';
}
inline std::ostream& operator<<(std::ostream& os, const uint2& vec)
{
    return os << "uint2(" << vec.x << ", " << vec.y << ')';
}

} // namespace Diligent



namespace std
{
template <typename T>
Diligent::Vector2<T> max(const Diligent::Vector2<T>& Left, const Diligent::Vector2<T>& Right)
{
    return Diligent::Vector2<T>(
        std::max(Left.x, Right.x),
        std::max(Left.y, Right.y));
}

template <typename T>
Diligent::Vector3<T> max(const Diligent::Vector3<T>& Left, const Diligent::Vector3<T>& Right)
{
    return Diligent::Vector3<T>(
        std::max(Left.x, Right.x),
        std::max(Left.y, Right.y),
        std::max(Left.z, Right.z));
}

template <typename T>
Diligent::Vector4<T> max(const Diligent::Vector4<T>& Left, const Diligent::Vector4<T>& Right)
{
    return Diligent::Vector4<T>(
        std::max(Left.x, Right.x),
        std::max(Left.y, Right.y),
        std::max(Left.z, Right.z),
        std::max(Left.w, Right.w));
}


template <typename T>
Diligent::Vector2<T> min(const Diligent::Vector2<T>& Left, const Diligent::Vector2<T>& Right)
{
    return Diligent::Vector2<T>(
        std::min(Left.x, Right.x),
        std::min(Left.y, Right.y));
}

template <typename T>
Diligent::Vector3<T> min(const Diligent::Vector3<T>& Left, const Diligent::Vector3<T>& Right)
{
    return Diligent::Vector3<T>(
        std::min(Left.x, Right.x),
        std::min(Left.y, Right.y),
        std::min(Left.z, Right.z));
}

template <typename T>
Diligent::Vector4<T> min(const Diligent::Vector4<T>& Left, const Diligent::Vector4<T>& Right)
{
    return Diligent::Vector4<T>(
        std::min(Left.x, Right.x),
        std::min(Left.y, Right.y),
        std::min(Left.z, Right.z),
        std::min(Left.w, Right.w));
}

template <typename T>
Diligent::Vector2<T> floor(const Diligent::Vector2<T>& vec)
{
    return Diligent::Vector2<T>(
        std::floor(vec.x),
        std::floor(vec.y));
}

template <typename T>
Diligent::Vector3<T> floor(const Diligent::Vector3<T>& vec)
{
    return Diligent::Vector3<T>(
        std::floor(vec.x),
        std::floor(vec.y),
        std::floor(vec.z));
}

template <typename T>
Diligent::Vector4<T> floor(const Diligent::Vector4<T>& vec)
{
    return Diligent::Vector4<T>(
        std::floor(vec.x),
        std::floor(vec.y),
        std::floor(vec.z),
        std::floor(vec.w));
}


template <typename T>
Diligent::Vector2<T> ceil(const Diligent::Vector2<T>& vec)
{
    return Diligent::Vector2<T>(
        std::ceil(vec.x),
        std::ceil(vec.y));
}

template <typename T>
Diligent::Vector3<T> ceil(const Diligent::Vector3<T>& vec)
{
    return Diligent::Vector3<T>(
        std::ceil(vec.x),
        std::ceil(vec.y),
        std::ceil(vec.z));
}

template <typename T>
Diligent::Vector4<T> ceil(const Diligent::Vector4<T>& vec)
{
    return Diligent::Vector4<T>(
        std::ceil(vec.x),
        std::ceil(vec.y),
        std::ceil(vec.z),
        std::ceil(vec.w));
}


template <typename T>
struct hash<Diligent::Vector2<T>>
{
    size_t operator()(const Diligent::Vector2<T>& v2) const
    {
        return Diligent::ComputeHash(v2.x, v2.y);
    }
};

template <typename T>
struct hash<Diligent::Vector3<T>>
{
    size_t operator()(const Diligent::Vector3<T>& v3) const
    {
        return Diligent::ComputeHash(v3.x, v3.y, v3.z);
    }
};

template <typename T>
struct hash<Diligent::Vector4<T>>
{
    size_t operator()(const Diligent::Vector4<T>& v4) const
    {
        return Diligent::ComputeHash(v4.x, v4.y, v4.z, v4.w);
    }
};

template <typename T>
struct hash<Diligent::Matrix2x2<T>>
{
    size_t operator()(const Diligent::Matrix2x2<T>& m) const
    {
        return Diligent::ComputeHash(
            m.m00, m.m01,
            m.m10, m.m11);
    }
};

template <typename T>
struct hash<Diligent::Matrix3x3<T>>
{
    size_t operator()(const Diligent::Matrix3x3<T>& m) const
    {
        return Diligent::ComputeHash(
            m.m00, m.m01, m.m02,
            m.m10, m.m11, m.m12,
            m.m20, m.m21, m.m22);
    }
};

template <typename T>
struct hash<Diligent::Matrix4x4<T>>
{
    size_t operator()(const Diligent::Matrix4x4<T>& m) const
    {
        return Diligent::ComputeHash(
            m.m00, m.m01, m.m02, m.m03,
            m.m10, m.m11, m.m12, m.m13,
            m.m20, m.m21, m.m22, m.m23,
            m.m30, m.m31, m.m32, m.m33);
    }
};
} // namespace std

#ifdef _MSC_VER
#    pragma warning(pop)
#endif