This file is indexed.

/usr/include/bullet/Bullet3OpenCL/NarrowphaseCollision/kernels/satKernels.h is in libbullet-dev 2.87+dfsg-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
static const char* satKernelsCL= \
"//keep this enum in sync with the CPU version (in btCollidable.h)\n"
"//written by Erwin Coumans\n"
"#define SHAPE_CONVEX_HULL 3\n"
"#define SHAPE_CONCAVE_TRIMESH 5\n"
"#define TRIANGLE_NUM_CONVEX_FACES 5\n"
"#define SHAPE_COMPOUND_OF_CONVEX_HULLS 6\n"
"#define B3_MAX_STACK_DEPTH 256\n"
"typedef unsigned int u32;\n"
"///keep this in sync with btCollidable.h\n"
"typedef struct\n"
"{\n"
"	union {\n"
"		int m_numChildShapes;\n"
"		int m_bvhIndex;\n"
"	};\n"
"	union\n"
"	{\n"
"		float m_radius;\n"
"		int	m_compoundBvhIndex;\n"
"	};\n"
"	\n"
"	int m_shapeType;\n"
"	int m_shapeIndex;\n"
"	\n"
"} btCollidableGpu;\n"
"#define MAX_NUM_PARTS_IN_BITS 10\n"
"///b3QuantizedBvhNode is a compressed aabb node, 16 bytes.\n"
"///Node can be used for leafnode or internal node. Leafnodes can point to 32-bit triangle index (non-negative range).\n"
"typedef struct\n"
"{\n"
"	//12 bytes\n"
"	unsigned short int	m_quantizedAabbMin[3];\n"
"	unsigned short int	m_quantizedAabbMax[3];\n"
"	//4 bytes\n"
"	int	m_escapeIndexOrTriangleIndex;\n"
"} b3QuantizedBvhNode;\n"
"typedef struct\n"
"{\n"
"	float4		m_aabbMin;\n"
"	float4		m_aabbMax;\n"
"	float4		m_quantization;\n"
"	int			m_numNodes;\n"
"	int			m_numSubTrees;\n"
"	int			m_nodeOffset;\n"
"	int			m_subTreeOffset;\n"
"} b3BvhInfo;\n"
"int	getTriangleIndex(const b3QuantizedBvhNode* rootNode)\n"
"{\n"
"	unsigned int x=0;\n"
"	unsigned int y = (~(x&0))<<(31-MAX_NUM_PARTS_IN_BITS);\n"
"	// Get only the lower bits where the triangle index is stored\n"
"	return (rootNode->m_escapeIndexOrTriangleIndex&~(y));\n"
"}\n"
"int	getTriangleIndexGlobal(__global const b3QuantizedBvhNode* rootNode)\n"
"{\n"
"	unsigned int x=0;\n"
"	unsigned int y = (~(x&0))<<(31-MAX_NUM_PARTS_IN_BITS);\n"
"	// Get only the lower bits where the triangle index is stored\n"
"	return (rootNode->m_escapeIndexOrTriangleIndex&~(y));\n"
"}\n"
"int isLeafNode(const b3QuantizedBvhNode* rootNode)\n"
"{\n"
"	//skipindex is negative (internal node), triangleindex >=0 (leafnode)\n"
"	return (rootNode->m_escapeIndexOrTriangleIndex >= 0)? 1 : 0;\n"
"}\n"
"int isLeafNodeGlobal(__global const b3QuantizedBvhNode* rootNode)\n"
"{\n"
"	//skipindex is negative (internal node), triangleindex >=0 (leafnode)\n"
"	return (rootNode->m_escapeIndexOrTriangleIndex >= 0)? 1 : 0;\n"
"}\n"
"	\n"
"int getEscapeIndex(const b3QuantizedBvhNode* rootNode)\n"
"{\n"
"	return -rootNode->m_escapeIndexOrTriangleIndex;\n"
"}\n"
"int getEscapeIndexGlobal(__global const b3QuantizedBvhNode* rootNode)\n"
"{\n"
"	return -rootNode->m_escapeIndexOrTriangleIndex;\n"
"}\n"
"typedef struct\n"
"{\n"
"	//12 bytes\n"
"	unsigned short int	m_quantizedAabbMin[3];\n"
"	unsigned short int	m_quantizedAabbMax[3];\n"
"	//4 bytes, points to the root of the subtree\n"
"	int			m_rootNodeIndex;\n"
"	//4 bytes\n"
"	int			m_subtreeSize;\n"
"	int			m_padding[3];\n"
"} b3BvhSubtreeInfo;\n"
"typedef struct\n"
"{\n"
"	float4	m_childPosition;\n"
"	float4	m_childOrientation;\n"
"	int m_shapeIndex;\n"
"	int m_unused0;\n"
"	int m_unused1;\n"
"	int m_unused2;\n"
"} btGpuChildShape;\n"
"typedef struct\n"
"{\n"
"	float4 m_pos;\n"
"	float4 m_quat;\n"
"	float4 m_linVel;\n"
"	float4 m_angVel;\n"
"	u32 m_collidableIdx;\n"
"	float m_invMass;\n"
"	float m_restituitionCoeff;\n"
"	float m_frictionCoeff;\n"
"} BodyData;\n"
"typedef struct  \n"
"{\n"
"	float4		m_localCenter;\n"
"	float4		m_extents;\n"
"	float4		mC;\n"
"	float4		mE;\n"
"	\n"
"	float			m_radius;\n"
"	int	m_faceOffset;\n"
"	int m_numFaces;\n"
"	int	m_numVertices;\n"
"	int m_vertexOffset;\n"
"	int	m_uniqueEdgesOffset;\n"
"	int	m_numUniqueEdges;\n"
"	int m_unused;\n"
"} ConvexPolyhedronCL;\n"
"typedef struct \n"
"{\n"
"	union\n"
"	{\n"
"		float4	m_min;\n"
"		float   m_minElems[4];\n"
"		int			m_minIndices[4];\n"
"	};\n"
"	union\n"
"	{\n"
"		float4	m_max;\n"
"		float   m_maxElems[4];\n"
"		int			m_maxIndices[4];\n"
"	};\n"
"} btAabbCL;\n"
"#ifndef B3_AABB_H\n"
"#define B3_AABB_H\n"
"#ifndef B3_FLOAT4_H\n"
"#define B3_FLOAT4_H\n"
"#ifndef B3_PLATFORM_DEFINITIONS_H\n"
"#define B3_PLATFORM_DEFINITIONS_H\n"
"struct MyTest\n"
"{\n"
"	int bla;\n"
"};\n"
"#ifdef __cplusplus\n"
"#else\n"
"//keep B3_LARGE_FLOAT*B3_LARGE_FLOAT < FLT_MAX\n"
"#define B3_LARGE_FLOAT 1e18f\n"
"#define B3_INFINITY 1e18f\n"
"#define b3Assert(a)\n"
"#define b3ConstArray(a) __global const a*\n"
"#define b3AtomicInc atomic_inc\n"
"#define b3AtomicAdd atomic_add\n"
"#define b3Fabs fabs\n"
"#define b3Sqrt native_sqrt\n"
"#define b3Sin native_sin\n"
"#define b3Cos native_cos\n"
"#define B3_STATIC\n"
"#endif\n"
"#endif\n"
"#ifdef __cplusplus\n"
"#else\n"
"	typedef float4	b3Float4;\n"
"	#define b3Float4ConstArg const b3Float4\n"
"	#define b3MakeFloat4 (float4)\n"
"	float b3Dot3F4(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
"	{\n"
"		float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
"		float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
"		return dot(a1, b1);\n"
"	}\n"
"	b3Float4 b3Cross3(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
"	{\n"
"		float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
"		float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
"		return cross(a1, b1);\n"
"	}\n"
"	#define b3MinFloat4 min\n"
"	#define b3MaxFloat4 max\n"
"	#define b3Normalized(a) normalize(a)\n"
"#endif \n"
"		\n"
"inline bool b3IsAlmostZero(b3Float4ConstArg v)\n"
"{\n"
"	if(b3Fabs(v.x)>1e-6 || b3Fabs(v.y)>1e-6 || b3Fabs(v.z)>1e-6)	\n"
"		return false;\n"
"	return true;\n"
"}\n"
"inline int    b3MaxDot( b3Float4ConstArg vec, __global const b3Float4* vecArray, int vecLen, float* dotOut )\n"
"{\n"
"    float maxDot = -B3_INFINITY;\n"
"    int i = 0;\n"
"    int ptIndex = -1;\n"
"    for( i = 0; i < vecLen; i++ )\n"
"    {\n"
"        float dot = b3Dot3F4(vecArray[i],vec);\n"
"            \n"
"        if( dot > maxDot )\n"
"        {\n"
"            maxDot = dot;\n"
"            ptIndex = i;\n"
"        }\n"
"    }\n"
"	b3Assert(ptIndex>=0);\n"
"    if (ptIndex<0)\n"
"	{\n"
"		ptIndex = 0;\n"
"	}\n"
"    *dotOut = maxDot;\n"
"    return ptIndex;\n"
"}\n"
"#endif //B3_FLOAT4_H\n"
"#ifndef B3_MAT3x3_H\n"
"#define B3_MAT3x3_H\n"
"#ifndef B3_QUAT_H\n"
"#define B3_QUAT_H\n"
"#ifndef B3_PLATFORM_DEFINITIONS_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif\n"
"#endif\n"
"#ifndef B3_FLOAT4_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#endif \n"
"#endif //B3_FLOAT4_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"	typedef float4	b3Quat;\n"
"	#define b3QuatConstArg const b3Quat\n"
"	\n"
"	\n"
"inline float4 b3FastNormalize4(float4 v)\n"
"{\n"
"	v = (float4)(v.xyz,0.f);\n"
"	return fast_normalize(v);\n"
"}\n"
"	\n"
"inline b3Quat b3QuatMul(b3Quat a, b3Quat b);\n"
"inline b3Quat b3QuatNormalized(b3QuatConstArg in);\n"
"inline b3Quat b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec);\n"
"inline b3Quat b3QuatInvert(b3QuatConstArg q);\n"
"inline b3Quat b3QuatInverse(b3QuatConstArg q);\n"
"inline b3Quat b3QuatMul(b3QuatConstArg a, b3QuatConstArg b)\n"
"{\n"
"	b3Quat ans;\n"
"	ans = b3Cross3( a, b );\n"
"	ans += a.w*b+b.w*a;\n"
"//	ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
"	ans.w = a.w*b.w - b3Dot3F4(a, b);\n"
"	return ans;\n"
"}\n"
"inline b3Quat b3QuatNormalized(b3QuatConstArg in)\n"
"{\n"
"	b3Quat q;\n"
"	q=in;\n"
"	//return b3FastNormalize4(in);\n"
"	float len = native_sqrt(dot(q, q));\n"
"	if(len > 0.f)\n"
"	{\n"
"		q *= 1.f / len;\n"
"	}\n"
"	else\n"
"	{\n"
"		q.x = q.y = q.z = 0.f;\n"
"		q.w = 1.f;\n"
"	}\n"
"	return q;\n"
"}\n"
"inline float4 b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
"{\n"
"	b3Quat qInv = b3QuatInvert( q );\n"
"	float4 vcpy = vec;\n"
"	vcpy.w = 0.f;\n"
"	float4 out = b3QuatMul(b3QuatMul(q,vcpy),qInv);\n"
"	return out;\n"
"}\n"
"inline b3Quat b3QuatInverse(b3QuatConstArg q)\n"
"{\n"
"	return (b3Quat)(-q.xyz, q.w);\n"
"}\n"
"inline b3Quat b3QuatInvert(b3QuatConstArg q)\n"
"{\n"
"	return (b3Quat)(-q.xyz, q.w);\n"
"}\n"
"inline float4 b3QuatInvRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
"{\n"
"	return b3QuatRotate( b3QuatInvert( q ), vec );\n"
"}\n"
"inline b3Float4 b3TransformPoint(b3Float4ConstArg point, b3Float4ConstArg translation, b3QuatConstArg  orientation)\n"
"{\n"
"	return b3QuatRotate( orientation, point ) + (translation);\n"
"}\n"
"	\n"
"#endif \n"
"#endif //B3_QUAT_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"typedef struct\n"
"{\n"
"	b3Float4 m_row[3];\n"
"}b3Mat3x3;\n"
"#define b3Mat3x3ConstArg const b3Mat3x3\n"
"#define b3GetRow(m,row) (m.m_row[row])\n"
"inline b3Mat3x3 b3QuatGetRotationMatrix(b3Quat quat)\n"
"{\n"
"	b3Float4 quat2 = (b3Float4)(quat.x*quat.x, quat.y*quat.y, quat.z*quat.z, 0.f);\n"
"	b3Mat3x3 out;\n"
"	out.m_row[0].x=1-2*quat2.y-2*quat2.z;\n"
"	out.m_row[0].y=2*quat.x*quat.y-2*quat.w*quat.z;\n"
"	out.m_row[0].z=2*quat.x*quat.z+2*quat.w*quat.y;\n"
"	out.m_row[0].w = 0.f;\n"
"	out.m_row[1].x=2*quat.x*quat.y+2*quat.w*quat.z;\n"
"	out.m_row[1].y=1-2*quat2.x-2*quat2.z;\n"
"	out.m_row[1].z=2*quat.y*quat.z-2*quat.w*quat.x;\n"
"	out.m_row[1].w = 0.f;\n"
"	out.m_row[2].x=2*quat.x*quat.z-2*quat.w*quat.y;\n"
"	out.m_row[2].y=2*quat.y*quat.z+2*quat.w*quat.x;\n"
"	out.m_row[2].z=1-2*quat2.x-2*quat2.y;\n"
"	out.m_row[2].w = 0.f;\n"
"	return out;\n"
"}\n"
"inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg matIn)\n"
"{\n"
"	b3Mat3x3 out;\n"
"	out.m_row[0] = fabs(matIn.m_row[0]);\n"
"	out.m_row[1] = fabs(matIn.m_row[1]);\n"
"	out.m_row[2] = fabs(matIn.m_row[2]);\n"
"	return out;\n"
"}\n"
"__inline\n"
"b3Mat3x3 mtZero();\n"
"__inline\n"
"b3Mat3x3 mtIdentity();\n"
"__inline\n"
"b3Mat3x3 mtTranspose(b3Mat3x3 m);\n"
"__inline\n"
"b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b);\n"
"__inline\n"
"b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b);\n"
"__inline\n"
"b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b);\n"
"__inline\n"
"b3Mat3x3 mtZero()\n"
"{\n"
"	b3Mat3x3 m;\n"
"	m.m_row[0] = (b3Float4)(0.f);\n"
"	m.m_row[1] = (b3Float4)(0.f);\n"
"	m.m_row[2] = (b3Float4)(0.f);\n"
"	return m;\n"
"}\n"
"__inline\n"
"b3Mat3x3 mtIdentity()\n"
"{\n"
"	b3Mat3x3 m;\n"
"	m.m_row[0] = (b3Float4)(1,0,0,0);\n"
"	m.m_row[1] = (b3Float4)(0,1,0,0);\n"
"	m.m_row[2] = (b3Float4)(0,0,1,0);\n"
"	return m;\n"
"}\n"
"__inline\n"
"b3Mat3x3 mtTranspose(b3Mat3x3 m)\n"
"{\n"
"	b3Mat3x3 out;\n"
"	out.m_row[0] = (b3Float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);\n"
"	out.m_row[1] = (b3Float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);\n"
"	out.m_row[2] = (b3Float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);\n"
"	return out;\n"
"}\n"
"__inline\n"
"b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b)\n"
"{\n"
"	b3Mat3x3 transB;\n"
"	transB = mtTranspose( b );\n"
"	b3Mat3x3 ans;\n"
"	//	why this doesn't run when 0ing in the for{}\n"
"	a.m_row[0].w = 0.f;\n"
"	a.m_row[1].w = 0.f;\n"
"	a.m_row[2].w = 0.f;\n"
"	for(int i=0; i<3; i++)\n"
"	{\n"
"//	a.m_row[i].w = 0.f;\n"
"		ans.m_row[i].x = b3Dot3F4(a.m_row[i],transB.m_row[0]);\n"
"		ans.m_row[i].y = b3Dot3F4(a.m_row[i],transB.m_row[1]);\n"
"		ans.m_row[i].z = b3Dot3F4(a.m_row[i],transB.m_row[2]);\n"
"		ans.m_row[i].w = 0.f;\n"
"	}\n"
"	return ans;\n"
"}\n"
"__inline\n"
"b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b)\n"
"{\n"
"	b3Float4 ans;\n"
"	ans.x = b3Dot3F4( a.m_row[0], b );\n"
"	ans.y = b3Dot3F4( a.m_row[1], b );\n"
"	ans.z = b3Dot3F4( a.m_row[2], b );\n"
"	ans.w = 0.f;\n"
"	return ans;\n"
"}\n"
"__inline\n"
"b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b)\n"
"{\n"
"	b3Float4 colx = b3MakeFloat4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);\n"
"	b3Float4 coly = b3MakeFloat4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);\n"
"	b3Float4 colz = b3MakeFloat4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);\n"
"	b3Float4 ans;\n"
"	ans.x = b3Dot3F4( a, colx );\n"
"	ans.y = b3Dot3F4( a, coly );\n"
"	ans.z = b3Dot3F4( a, colz );\n"
"	return ans;\n"
"}\n"
"#endif\n"
"#endif //B3_MAT3x3_H\n"
"typedef struct b3Aabb b3Aabb_t;\n"
"struct b3Aabb\n"
"{\n"
"	union\n"
"	{\n"
"		float m_min[4];\n"
"		b3Float4 m_minVec;\n"
"		int m_minIndices[4];\n"
"	};\n"
"	union\n"
"	{\n"
"		float	m_max[4];\n"
"		b3Float4 m_maxVec;\n"
"		int m_signedMaxIndices[4];\n"
"	};\n"
"};\n"
"inline void b3TransformAabb2(b3Float4ConstArg localAabbMin,b3Float4ConstArg localAabbMax, float margin,\n"
"						b3Float4ConstArg pos,\n"
"						b3QuatConstArg orn,\n"
"						b3Float4* aabbMinOut,b3Float4* aabbMaxOut)\n"
"{\n"
"		b3Float4 localHalfExtents = 0.5f*(localAabbMax-localAabbMin);\n"
"		localHalfExtents+=b3MakeFloat4(margin,margin,margin,0.f);\n"
"		b3Float4 localCenter = 0.5f*(localAabbMax+localAabbMin);\n"
"		b3Mat3x3 m;\n"
"		m = b3QuatGetRotationMatrix(orn);\n"
"		b3Mat3x3 abs_b = b3AbsoluteMat3x3(m);\n"
"		b3Float4 center = b3TransformPoint(localCenter,pos,orn);\n"
"		\n"
"		b3Float4 extent = b3MakeFloat4(b3Dot3F4(localHalfExtents,b3GetRow(abs_b,0)),\n"
"										 b3Dot3F4(localHalfExtents,b3GetRow(abs_b,1)),\n"
"										 b3Dot3F4(localHalfExtents,b3GetRow(abs_b,2)),\n"
"										 0.f);\n"
"		*aabbMinOut = center-extent;\n"
"		*aabbMaxOut = center+extent;\n"
"}\n"
"/// conservative test for overlap between two aabbs\n"
"inline bool b3TestAabbAgainstAabb(b3Float4ConstArg aabbMin1,b3Float4ConstArg aabbMax1,\n"
"								b3Float4ConstArg aabbMin2, b3Float4ConstArg aabbMax2)\n"
"{\n"
"	bool overlap = true;\n"
"	overlap = (aabbMin1.x > aabbMax2.x || aabbMax1.x < aabbMin2.x) ? false : overlap;\n"
"	overlap = (aabbMin1.z > aabbMax2.z || aabbMax1.z < aabbMin2.z) ? false : overlap;\n"
"	overlap = (aabbMin1.y > aabbMax2.y || aabbMax1.y < aabbMin2.y) ? false : overlap;\n"
"	return overlap;\n"
"}\n"
"#endif //B3_AABB_H\n"
"/*\n"
"Bullet Continuous Collision Detection and Physics Library\n"
"Copyright (c) 2003-2013 Erwin Coumans  http://bulletphysics.org\n"
"This software is provided 'as-is', without any express or implied warranty.\n"
"In no event will the authors be held liable for any damages arising from the use of this software.\n"
"Permission is granted to anyone to use this software for any purpose,\n"
"including commercial applications, and to alter it and redistribute it freely,\n"
"subject to the following restrictions:\n"
"1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.\n"
"2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n"
"3. This notice may not be removed or altered from any source distribution.\n"
"*/\n"
"#ifndef B3_INT2_H\n"
"#define B3_INT2_H\n"
"#ifdef __cplusplus\n"
"#else\n"
"#define b3UnsignedInt2 uint2\n"
"#define b3Int2 int2\n"
"#define b3MakeInt2 (int2)\n"
"#endif //__cplusplus\n"
"#endif\n"
"typedef struct\n"
"{\n"
"	float4 m_plane;\n"
"	int m_indexOffset;\n"
"	int m_numIndices;\n"
"} btGpuFace;\n"
"#define make_float4 (float4)\n"
"__inline\n"
"float4 cross3(float4 a, float4 b)\n"
"{\n"
"	return cross(a,b);\n"
"	\n"
"//	float4 a1 = make_float4(a.xyz,0.f);\n"
"//	float4 b1 = make_float4(b.xyz,0.f);\n"
"//	return cross(a1,b1);\n"
"//float4 c = make_float4(a.y*b.z - a.z*b.y,a.z*b.x - a.x*b.z,a.x*b.y - a.y*b.x,0.f);\n"
"	\n"
"	//	float4 c = make_float4(a.y*b.z - a.z*b.y,1.f,a.x*b.y - a.y*b.x,0.f);\n"
"	\n"
"	//return c;\n"
"}\n"
"__inline\n"
"float dot3F4(float4 a, float4 b)\n"
"{\n"
"	float4 a1 = make_float4(a.xyz,0.f);\n"
"	float4 b1 = make_float4(b.xyz,0.f);\n"
"	return dot(a1, b1);\n"
"}\n"
"__inline\n"
"float4 fastNormalize4(float4 v)\n"
"{\n"
"	v = make_float4(v.xyz,0.f);\n"
"	return fast_normalize(v);\n"
"}\n"
"///////////////////////////////////////\n"
"//	Quaternion\n"
"///////////////////////////////////////\n"
"typedef float4 Quaternion;\n"
"__inline\n"
"Quaternion qtMul(Quaternion a, Quaternion b);\n"
"__inline\n"
"Quaternion qtNormalize(Quaternion in);\n"
"__inline\n"
"float4 qtRotate(Quaternion q, float4 vec);\n"
"__inline\n"
"Quaternion qtInvert(Quaternion q);\n"
"__inline\n"
"Quaternion qtMul(Quaternion a, Quaternion b)\n"
"{\n"
"	Quaternion ans;\n"
"	ans = cross3( a, b );\n"
"	ans += a.w*b+b.w*a;\n"
"//	ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
"	ans.w = a.w*b.w - dot3F4(a, b);\n"
"	return ans;\n"
"}\n"
"__inline\n"
"Quaternion qtNormalize(Quaternion in)\n"
"{\n"
"	return fastNormalize4(in);\n"
"//	in /= length( in );\n"
"//	return in;\n"
"}\n"
"__inline\n"
"float4 qtRotate(Quaternion q, float4 vec)\n"
"{\n"
"	Quaternion qInv = qtInvert( q );\n"
"	float4 vcpy = vec;\n"
"	vcpy.w = 0.f;\n"
"	float4 out = qtMul(qtMul(q,vcpy),qInv);\n"
"	return out;\n"
"}\n"
"__inline\n"
"Quaternion qtInvert(Quaternion q)\n"
"{\n"
"	return (Quaternion)(-q.xyz, q.w);\n"
"}\n"
"__inline\n"
"float4 qtInvRotate(const Quaternion q, float4 vec)\n"
"{\n"
"	return qtRotate( qtInvert( q ), vec );\n"
"}\n"
"__inline\n"
"float4 transform(const float4* p, const float4* translation, const Quaternion* orientation)\n"
"{\n"
"	return qtRotate( *orientation, *p ) + (*translation);\n"
"}\n"
"__inline\n"
"float4 normalize3(const float4 a)\n"
"{\n"
"	float4 n = make_float4(a.x, a.y, a.z, 0.f);\n"
"	return fastNormalize4( n );\n"
"}\n"
"inline void projectLocal(const ConvexPolyhedronCL* hull,  const float4 pos, const float4 orn, \n"
"const float4* dir, const float4* vertices, float* min, float* max)\n"
"{\n"
"	min[0] = FLT_MAX;\n"
"	max[0] = -FLT_MAX;\n"
"	int numVerts = hull->m_numVertices;\n"
"	const float4 localDir = qtInvRotate(orn,*dir);\n"
"	float offset = dot(pos,*dir);\n"
"	for(int i=0;i<numVerts;i++)\n"
"	{\n"
"		float dp = dot(vertices[hull->m_vertexOffset+i],localDir);\n"
"		if(dp < min[0])	\n"
"			min[0] = dp;\n"
"		if(dp > max[0])	\n"
"			max[0] = dp;\n"
"	}\n"
"	if(min[0]>max[0])\n"
"	{\n"
"		float tmp = min[0];\n"
"		min[0] = max[0];\n"
"		max[0] = tmp;\n"
"	}\n"
"	min[0] += offset;\n"
"	max[0] += offset;\n"
"}\n"
"inline void project(__global const ConvexPolyhedronCL* hull,  const float4 pos, const float4 orn, \n"
"const float4* dir, __global const float4* vertices, float* min, float* max)\n"
"{\n"
"	min[0] = FLT_MAX;\n"
"	max[0] = -FLT_MAX;\n"
"	int numVerts = hull->m_numVertices;\n"
"	const float4 localDir = qtInvRotate(orn,*dir);\n"
"	float offset = dot(pos,*dir);\n"
"	for(int i=0;i<numVerts;i++)\n"
"	{\n"
"		float dp = dot(vertices[hull->m_vertexOffset+i],localDir);\n"
"		if(dp < min[0])	\n"
"			min[0] = dp;\n"
"		if(dp > max[0])	\n"
"			max[0] = dp;\n"
"	}\n"
"	if(min[0]>max[0])\n"
"	{\n"
"		float tmp = min[0];\n"
"		min[0] = max[0];\n"
"		max[0] = tmp;\n"
"	}\n"
"	min[0] += offset;\n"
"	max[0] += offset;\n"
"}\n"
"inline bool TestSepAxisLocalA(const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
"	const float4 posA,const float4 ornA,\n"
"	const float4 posB,const float4 ornB,\n"
"	float4* sep_axis, const float4* verticesA, __global const float4* verticesB,float* depth)\n"
"{\n"
"	float Min0,Max0;\n"
"	float Min1,Max1;\n"
"	projectLocal(hullA,posA,ornA,sep_axis,verticesA, &Min0, &Max0);\n"
"	project(hullB,posB,ornB, sep_axis,verticesB, &Min1, &Max1);\n"
"	if(Max0<Min1 || Max1<Min0)\n"
"		return false;\n"
"	float d0 = Max0 - Min1;\n"
"	float d1 = Max1 - Min0;\n"
"	*depth = d0<d1 ? d0:d1;\n"
"	return true;\n"
"}\n"
"inline bool IsAlmostZero(const float4 v)\n"
"{\n"
"	if(fabs(v.x)>1e-6f || fabs(v.y)>1e-6f || fabs(v.z)>1e-6f)\n"
"		return false;\n"
"	return true;\n"
"}\n"
"bool findSeparatingAxisLocalA(	const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
"	const float4 posA1,\n"
"	const float4 ornA,\n"
"	const float4 posB1,\n"
"	const float4 ornB,\n"
"	const float4 DeltaC2,\n"
"	\n"
"	const float4* verticesA, \n"
"	const float4* uniqueEdgesA, \n"
"	const btGpuFace* facesA,\n"
"	const int*  indicesA,\n"
"	__global const float4* verticesB, \n"
"	__global const float4* uniqueEdgesB, \n"
"	__global const btGpuFace* facesB,\n"
"	__global const int*  indicesB,\n"
"	float4* sep,\n"
"	float* dmin)\n"
"{\n"
"	\n"
"	float4 posA = posA1;\n"
"	posA.w = 0.f;\n"
"	float4 posB = posB1;\n"
"	posB.w = 0.f;\n"
"	int curPlaneTests=0;\n"
"	{\n"
"		int numFacesA = hullA->m_numFaces;\n"
"		// Test normals from hullA\n"
"		for(int i=0;i<numFacesA;i++)\n"
"		{\n"
"			const float4 normal = facesA[hullA->m_faceOffset+i].m_plane;\n"
"			float4 faceANormalWS = qtRotate(ornA,normal);\n"
"			if (dot3F4(DeltaC2,faceANormalWS)<0)\n"
"				faceANormalWS*=-1.f;\n"
"			curPlaneTests++;\n"
"			float d;\n"
"			if(!TestSepAxisLocalA( hullA, hullB, posA,ornA,posB,ornB,&faceANormalWS, verticesA, verticesB,&d))\n"
"				return false;\n"
"			if(d<*dmin)\n"
"			{\n"
"				*dmin = d;\n"
"				*sep = faceANormalWS;\n"
"			}\n"
"		}\n"
"	}\n"
"	if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
"	{\n"
"		*sep = -(*sep);\n"
"	}\n"
"	return true;\n"
"}\n"
"bool findSeparatingAxisLocalB(	__global const ConvexPolyhedronCL* hullA,  const ConvexPolyhedronCL* hullB, \n"
"	const float4 posA1,\n"
"	const float4 ornA,\n"
"	const float4 posB1,\n"
"	const float4 ornB,\n"
"	const float4 DeltaC2,\n"
"	__global const float4* verticesA, \n"
"	__global const float4* uniqueEdgesA, \n"
"	__global const btGpuFace* facesA,\n"
"	__global const int*  indicesA,\n"
"	const float4* verticesB,\n"
"	const float4* uniqueEdgesB, \n"
"	const btGpuFace* facesB,\n"
"	const int*  indicesB,\n"
"	float4* sep,\n"
"	float* dmin)\n"
"{\n"
"	float4 posA = posA1;\n"
"	posA.w = 0.f;\n"
"	float4 posB = posB1;\n"
"	posB.w = 0.f;\n"
"	int curPlaneTests=0;\n"
"	{\n"
"		int numFacesA = hullA->m_numFaces;\n"
"		// Test normals from hullA\n"
"		for(int i=0;i<numFacesA;i++)\n"
"		{\n"
"			const float4 normal = facesA[hullA->m_faceOffset+i].m_plane;\n"
"			float4 faceANormalWS = qtRotate(ornA,normal);\n"
"			if (dot3F4(DeltaC2,faceANormalWS)<0)\n"
"				faceANormalWS *= -1.f;\n"
"			curPlaneTests++;\n"
"			float d;\n"
"			if(!TestSepAxisLocalA( hullB, hullA, posB,ornB,posA,ornA, &faceANormalWS, verticesB,verticesA, &d))\n"
"				return false;\n"
"			if(d<*dmin)\n"
"			{\n"
"				*dmin = d;\n"
"				*sep = faceANormalWS;\n"
"			}\n"
"		}\n"
"	}\n"
"	if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
"	{\n"
"		*sep = -(*sep);\n"
"	}\n"
"	return true;\n"
"}\n"
"bool findSeparatingAxisEdgeEdgeLocalA(	const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
"	const float4 posA1,\n"
"	const float4 ornA,\n"
"	const float4 posB1,\n"
"	const float4 ornB,\n"
"	const float4 DeltaC2,\n"
"	const float4* verticesA, \n"
"	const float4* uniqueEdgesA, \n"
"	const btGpuFace* facesA,\n"
"	const int*  indicesA,\n"
"	__global const float4* verticesB, \n"
"	__global const float4* uniqueEdgesB, \n"
"	__global const btGpuFace* facesB,\n"
"	__global const int*  indicesB,\n"
"		float4* sep,\n"
"	float* dmin)\n"
"{\n"
"	float4 posA = posA1;\n"
"	posA.w = 0.f;\n"
"	float4 posB = posB1;\n"
"	posB.w = 0.f;\n"
"	int curPlaneTests=0;\n"
"	int curEdgeEdge = 0;\n"
"	// Test edges\n"
"	for(int e0=0;e0<hullA->m_numUniqueEdges;e0++)\n"
"	{\n"
"		const float4 edge0 = uniqueEdgesA[hullA->m_uniqueEdgesOffset+e0];\n"
"		float4 edge0World = qtRotate(ornA,edge0);\n"
"		for(int e1=0;e1<hullB->m_numUniqueEdges;e1++)\n"
"		{\n"
"			const float4 edge1 = uniqueEdgesB[hullB->m_uniqueEdgesOffset+e1];\n"
"			float4 edge1World = qtRotate(ornB,edge1);\n"
"			float4 crossje = cross3(edge0World,edge1World);\n"
"			curEdgeEdge++;\n"
"			if(!IsAlmostZero(crossje))\n"
"			{\n"
"				crossje = normalize3(crossje);\n"
"				if (dot3F4(DeltaC2,crossje)<0)\n"
"					crossje *= -1.f;\n"
"				float dist;\n"
"				bool result = true;\n"
"				{\n"
"					float Min0,Max0;\n"
"					float Min1,Max1;\n"
"					projectLocal(hullA,posA,ornA,&crossje,verticesA, &Min0, &Max0);\n"
"					project(hullB,posB,ornB,&crossje,verticesB, &Min1, &Max1);\n"
"				\n"
"					if(Max0<Min1 || Max1<Min0)\n"
"						result = false;\n"
"				\n"
"					float d0 = Max0 - Min1;\n"
"					float d1 = Max1 - Min0;\n"
"					dist = d0<d1 ? d0:d1;\n"
"					result = true;\n"
"				}\n"
"				\n"
"				if(dist<*dmin)\n"
"				{\n"
"					*dmin = dist;\n"
"					*sep = crossje;\n"
"				}\n"
"			}\n"
"		}\n"
"	}\n"
"	\n"
"	if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
"	{\n"
"		*sep = -(*sep);\n"
"	}\n"
"	return true;\n"
"}\n"
"inline bool TestSepAxis(__global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
"	const float4 posA,const float4 ornA,\n"
"	const float4 posB,const float4 ornB,\n"
"	float4* sep_axis, __global const float4* vertices,float* depth)\n"
"{\n"
"	float Min0,Max0;\n"
"	float Min1,Max1;\n"
"	project(hullA,posA,ornA,sep_axis,vertices, &Min0, &Max0);\n"
"	project(hullB,posB,ornB, sep_axis,vertices, &Min1, &Max1);\n"
"	if(Max0<Min1 || Max1<Min0)\n"
"		return false;\n"
"	float d0 = Max0 - Min1;\n"
"	float d1 = Max1 - Min0;\n"
"	*depth = d0<d1 ? d0:d1;\n"
"	return true;\n"
"}\n"
"bool findSeparatingAxis(	__global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
"	const float4 posA1,\n"
"	const float4 ornA,\n"
"	const float4 posB1,\n"
"	const float4 ornB,\n"
"	const float4 DeltaC2,\n"
"	__global const float4* vertices, \n"
"	__global const float4* uniqueEdges, \n"
"	__global const btGpuFace* faces,\n"
"	__global const int*  indices,\n"
"	float4* sep,\n"
"	float* dmin)\n"
"{\n"
"	\n"
"	float4 posA = posA1;\n"
"	posA.w = 0.f;\n"
"	float4 posB = posB1;\n"
"	posB.w = 0.f;\n"
"	\n"
"	int curPlaneTests=0;\n"
"	{\n"
"		int numFacesA = hullA->m_numFaces;\n"
"		// Test normals from hullA\n"
"		for(int i=0;i<numFacesA;i++)\n"
"		{\n"
"			const float4 normal = faces[hullA->m_faceOffset+i].m_plane;\n"
"			float4 faceANormalWS = qtRotate(ornA,normal);\n"
"	\n"
"			if (dot3F4(DeltaC2,faceANormalWS)<0)\n"
"				faceANormalWS*=-1.f;\n"
"				\n"
"			curPlaneTests++;\n"
"	\n"
"			float d;\n"
"			if(!TestSepAxis( hullA, hullB, posA,ornA,posB,ornB,&faceANormalWS, vertices,&d))\n"
"				return false;\n"
"	\n"
"			if(d<*dmin)\n"
"			{\n"
"				*dmin = d;\n"
"				*sep = faceANormalWS;\n"
"			}\n"
"		}\n"
"	}\n"
"		if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
"		{\n"
"			*sep = -(*sep);\n"
"		}\n"
"	\n"
"	return true;\n"
"}\n"
"bool findSeparatingAxisUnitSphere(	__global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
"	const float4 posA1,\n"
"	const float4 ornA,\n"
"	const float4 posB1,\n"
"	const float4 ornB,\n"
"	const float4 DeltaC2,\n"
"	__global const float4* vertices,\n"
"	__global const float4* unitSphereDirections,\n"
"	int numUnitSphereDirections,\n"
"	float4* sep,\n"
"	float* dmin)\n"
"{\n"
"	\n"
"	float4 posA = posA1;\n"
"	posA.w = 0.f;\n"
"	float4 posB = posB1;\n"
"	posB.w = 0.f;\n"
"	int curPlaneTests=0;\n"
"	int curEdgeEdge = 0;\n"
"	// Test unit sphere directions\n"
"	for (int i=0;i<numUnitSphereDirections;i++)\n"
"	{\n"
"		float4 crossje;\n"
"		crossje = unitSphereDirections[i];	\n"
"		if (dot3F4(DeltaC2,crossje)>0)\n"
"			crossje *= -1.f;\n"
"		{\n"
"			float dist;\n"
"			bool result = true;\n"
"			float Min0,Max0;\n"
"			float Min1,Max1;\n"
"			project(hullA,posA,ornA,&crossje,vertices, &Min0, &Max0);\n"
"			project(hullB,posB,ornB,&crossje,vertices, &Min1, &Max1);\n"
"		\n"
"			if(Max0<Min1 || Max1<Min0)\n"
"				return false;\n"
"		\n"
"			float d0 = Max0 - Min1;\n"
"			float d1 = Max1 - Min0;\n"
"			dist = d0<d1 ? d0:d1;\n"
"			result = true;\n"
"	\n"
"			if(dist<*dmin)\n"
"			{\n"
"				*dmin = dist;\n"
"				*sep = crossje;\n"
"			}\n"
"		}\n"
"	}\n"
"	\n"
"	if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
"	{\n"
"		*sep = -(*sep);\n"
"	}\n"
"	return true;\n"
"}\n"
"bool findSeparatingAxisEdgeEdge(	__global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
"	const float4 posA1,\n"
"	const float4 ornA,\n"
"	const float4 posB1,\n"
"	const float4 ornB,\n"
"	const float4 DeltaC2,\n"
"	__global const float4* vertices, \n"
"	__global const float4* uniqueEdges, \n"
"	__global const btGpuFace* faces,\n"
"	__global const int*  indices,\n"
"	float4* sep,\n"
"	float* dmin)\n"
"{\n"
"	\n"
"	float4 posA = posA1;\n"
"	posA.w = 0.f;\n"
"	float4 posB = posB1;\n"
"	posB.w = 0.f;\n"
"	int curPlaneTests=0;\n"
"	int curEdgeEdge = 0;\n"
"	// Test edges\n"
"	for(int e0=0;e0<hullA->m_numUniqueEdges;e0++)\n"
"	{\n"
"		const float4 edge0 = uniqueEdges[hullA->m_uniqueEdgesOffset+e0];\n"
"		float4 edge0World = qtRotate(ornA,edge0);\n"
"		for(int e1=0;e1<hullB->m_numUniqueEdges;e1++)\n"
"		{\n"
"			const float4 edge1 = uniqueEdges[hullB->m_uniqueEdgesOffset+e1];\n"
"			float4 edge1World = qtRotate(ornB,edge1);\n"
"			float4 crossje = cross3(edge0World,edge1World);\n"
"			curEdgeEdge++;\n"
"			if(!IsAlmostZero(crossje))\n"
"			{\n"
"				crossje = normalize3(crossje);\n"
"				if (dot3F4(DeltaC2,crossje)<0)\n"
"					crossje*=-1.f;\n"
"					\n"
"				float dist;\n"
"				bool result = true;\n"
"				{\n"
"					float Min0,Max0;\n"
"					float Min1,Max1;\n"
"					project(hullA,posA,ornA,&crossje,vertices, &Min0, &Max0);\n"
"					project(hullB,posB,ornB,&crossje,vertices, &Min1, &Max1);\n"
"				\n"
"					if(Max0<Min1 || Max1<Min0)\n"
"						return false;\n"
"				\n"
"					float d0 = Max0 - Min1;\n"
"					float d1 = Max1 - Min0;\n"
"					dist = d0<d1 ? d0:d1;\n"
"					result = true;\n"
"				}\n"
"				\n"
"				if(dist<*dmin)\n"
"				{\n"
"					*dmin = dist;\n"
"					*sep = crossje;\n"
"				}\n"
"			}\n"
"		}\n"
"	}\n"
"	\n"
"	if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
"	{\n"
"		*sep = -(*sep);\n"
"	}\n"
"	return true;\n"
"}\n"
"// work-in-progress\n"
"__kernel void   processCompoundPairsKernel( __global const int4* gpuCompoundPairs,\n"
"																					__global const BodyData* rigidBodies, \n"
"																					__global const btCollidableGpu* collidables,\n"
"																					__global const ConvexPolyhedronCL* convexShapes, \n"
"																					__global const float4* vertices,\n"
"																					__global const float4* uniqueEdges,\n"
"																					__global const btGpuFace* faces,\n"
"																					__global const int* indices,\n"
"																					__global btAabbCL* aabbs,\n"
"																					__global const btGpuChildShape* gpuChildShapes,\n"
"																					__global volatile float4* gpuCompoundSepNormalsOut,\n"
"																					__global volatile int* gpuHasCompoundSepNormalsOut,\n"
"																					int numCompoundPairs\n"
"																					)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	if (i<numCompoundPairs)\n"
"	{\n"
"		int bodyIndexA = gpuCompoundPairs[i].x;\n"
"		int bodyIndexB = gpuCompoundPairs[i].y;\n"
"		int childShapeIndexA = gpuCompoundPairs[i].z;\n"
"		int childShapeIndexB = gpuCompoundPairs[i].w;\n"
"		\n"
"		int collidableIndexA = -1;\n"
"		int collidableIndexB = -1;\n"
"		\n"
"		float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
"		float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
"		\n"
"		float4 ornB = rigidBodies[bodyIndexB].m_quat;\n"
"		float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
"							\n"
"		if (childShapeIndexA >= 0)\n"
"		{\n"
"			collidableIndexA = gpuChildShapes[childShapeIndexA].m_shapeIndex;\n"
"			float4 childPosA = gpuChildShapes[childShapeIndexA].m_childPosition;\n"
"			float4 childOrnA = gpuChildShapes[childShapeIndexA].m_childOrientation;\n"
"			float4 newPosA = qtRotate(ornA,childPosA)+posA;\n"
"			float4 newOrnA = qtMul(ornA,childOrnA);\n"
"			posA = newPosA;\n"
"			ornA = newOrnA;\n"
"		} else\n"
"		{\n"
"			collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
"		}\n"
"		\n"
"		if (childShapeIndexB>=0)\n"
"		{\n"
"			collidableIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;\n"
"			float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;\n"
"			float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;\n"
"			float4 newPosB = transform(&childPosB,&posB,&ornB);\n"
"			float4 newOrnB = qtMul(ornB,childOrnB);\n"
"			posB = newPosB;\n"
"			ornB = newOrnB;\n"
"		} else\n"
"		{\n"
"			collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;	\n"
"		}\n"
"	\n"
"		gpuHasCompoundSepNormalsOut[i] = 0;\n"
"	\n"
"		int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
"		int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
"	\n"
"		int shapeTypeA = collidables[collidableIndexA].m_shapeType;\n"
"		int shapeTypeB = collidables[collidableIndexB].m_shapeType;\n"
"	\n"
"		if ((shapeTypeA != SHAPE_CONVEX_HULL) || (shapeTypeB != SHAPE_CONVEX_HULL))\n"
"		{\n"
"			return;\n"
"		}\n"
"		int hasSeparatingAxis = 5;\n"
"							\n"
"		int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
"		float dmin = FLT_MAX;\n"
"		posA.w = 0.f;\n"
"		posB.w = 0.f;\n"
"		float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
"		float4 c0 = transform(&c0local, &posA, &ornA);\n"
"		float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
"		float4 c1 = transform(&c1local,&posB,&ornB);\n"
"		const float4 DeltaC2 = c0 - c1;\n"
"		float4 sepNormal = make_float4(1,0,0,0);\n"
"		bool sepA = findSeparatingAxis(	&convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,posB,ornB,DeltaC2,vertices,uniqueEdges,faces,indices,&sepNormal,&dmin);\n"
"		hasSeparatingAxis = 4;\n"
"		if (!sepA)\n"
"		{\n"
"			hasSeparatingAxis = 0;\n"
"		} else\n"
"		{\n"
"			bool sepB = findSeparatingAxis(	&convexShapes[shapeIndexB],&convexShapes[shapeIndexA],posB,ornB,posA,ornA,DeltaC2,vertices,uniqueEdges,faces,indices,&sepNormal,&dmin);\n"
"			if (!sepB)\n"
"			{\n"
"				hasSeparatingAxis = 0;\n"
"			} else//(!sepB)\n"
"			{\n"
"				bool sepEE = findSeparatingAxisEdgeEdge(	&convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,posB,ornB,DeltaC2,vertices,uniqueEdges,faces,indices,&sepNormal,&dmin);\n"
"				if (sepEE)\n"
"				{\n"
"						gpuCompoundSepNormalsOut[i] = sepNormal;//fastNormalize4(sepNormal);\n"
"						gpuHasCompoundSepNormalsOut[i] = 1;\n"
"				}//sepEE\n"
"			}//(!sepB)\n"
"		}//(!sepA)\n"
"		\n"
"		\n"
"	}\n"
"		\n"
"}\n"
"inline b3Float4 MyUnQuantize(const unsigned short* vecIn, b3Float4 quantization, b3Float4 bvhAabbMin)\n"
"{\n"
"		b3Float4 vecOut;\n"
"		vecOut = b3MakeFloat4(\n"
"			(float)(vecIn[0]) / (quantization.x),\n"
"			(float)(vecIn[1]) / (quantization.y),\n"
"			(float)(vecIn[2]) / (quantization.z),\n"
"			0.f);\n"
"		vecOut += bvhAabbMin;\n"
"		return vecOut;\n"
"}\n"
"inline b3Float4 MyUnQuantizeGlobal(__global const unsigned short* vecIn, b3Float4 quantization, b3Float4 bvhAabbMin)\n"
"{\n"
"		b3Float4 vecOut;\n"
"		vecOut = b3MakeFloat4(\n"
"			(float)(vecIn[0]) / (quantization.x),\n"
"			(float)(vecIn[1]) / (quantization.y),\n"
"			(float)(vecIn[2]) / (quantization.z),\n"
"			0.f);\n"
"		vecOut += bvhAabbMin;\n"
"		return vecOut;\n"
"}\n"
"// work-in-progress\n"
"__kernel void   findCompoundPairsKernel( __global const int4* pairs, \n"
"	__global const BodyData* rigidBodies, \n"
"	__global const btCollidableGpu* collidables,\n"
"	__global const ConvexPolyhedronCL* convexShapes, \n"
"	__global const float4* vertices,\n"
"	__global const float4* uniqueEdges,\n"
"	__global const btGpuFace* faces,\n"
"	__global const int* indices,\n"
"	__global b3Aabb_t* aabbLocalSpace,\n"
"	__global const btGpuChildShape* gpuChildShapes,\n"
"	__global volatile int4* gpuCompoundPairsOut,\n"
"	__global volatile int* numCompoundPairsOut,\n"
"	__global const b3BvhSubtreeInfo* subtrees,\n"
"	__global const b3QuantizedBvhNode* quantizedNodes,\n"
"	__global const b3BvhInfo* bvhInfos,\n"
"	int numPairs,\n"
"	int maxNumCompoundPairsCapacity\n"
"	)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	if (i<numPairs)\n"
"	{\n"
"		int bodyIndexA = pairs[i].x;\n"
"		int bodyIndexB = pairs[i].y;\n"
"		int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
"		int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
"		int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
"		int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
"		//once the broadphase avoids static-static pairs, we can remove this test\n"
"		if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))\n"
"		{\n"
"			return;\n"
"		}\n"
"		if ((collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS) &&(collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))\n"
"		{\n"
"			int bvhA = collidables[collidableIndexA].m_compoundBvhIndex;\n"
"			int bvhB = collidables[collidableIndexB].m_compoundBvhIndex;\n"
"			int numSubTreesA = bvhInfos[bvhA].m_numSubTrees;\n"
"			int subTreesOffsetA = bvhInfos[bvhA].m_subTreeOffset;\n"
"			int subTreesOffsetB = bvhInfos[bvhB].m_subTreeOffset;\n"
"			int numSubTreesB = bvhInfos[bvhB].m_numSubTrees;\n"
"			\n"
"			float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
"			b3Quat ornA = rigidBodies[bodyIndexA].m_quat;\n"
"			b3Quat ornB = rigidBodies[bodyIndexB].m_quat;\n"
"			float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
"			\n"
"			for (int p=0;p<numSubTreesA;p++)\n"
"			{\n"
"				b3BvhSubtreeInfo subtreeA = subtrees[subTreesOffsetA+p];\n"
"				//bvhInfos[bvhA].m_quantization\n"
"				b3Float4 treeAminLocal = MyUnQuantize(subtreeA.m_quantizedAabbMin,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);\n"
"				b3Float4 treeAmaxLocal = MyUnQuantize(subtreeA.m_quantizedAabbMax,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);\n"
"				b3Float4 aabbAMinOut,aabbAMaxOut;\n"
"				float margin=0.f;\n"
"				b3TransformAabb2(treeAminLocal,treeAmaxLocal, margin,posA,ornA,&aabbAMinOut,&aabbAMaxOut);\n"
"				\n"
"				for (int q=0;q<numSubTreesB;q++)\n"
"				{\n"
"					b3BvhSubtreeInfo subtreeB = subtrees[subTreesOffsetB+q];\n"
"					b3Float4 treeBminLocal = MyUnQuantize(subtreeB.m_quantizedAabbMin,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);\n"
"					b3Float4 treeBmaxLocal = MyUnQuantize(subtreeB.m_quantizedAabbMax,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);\n"
"					b3Float4 aabbBMinOut,aabbBMaxOut;\n"
"					float margin=0.f;\n"
"					b3TransformAabb2(treeBminLocal,treeBmaxLocal, margin,posB,ornB,&aabbBMinOut,&aabbBMaxOut);\n"
"					\n"
"					\n"
"					bool aabbOverlap = b3TestAabbAgainstAabb(aabbAMinOut,aabbAMaxOut,aabbBMinOut,aabbBMaxOut);\n"
"					if (aabbOverlap)\n"
"					{\n"
"						\n"
"						int startNodeIndexA = subtreeA.m_rootNodeIndex+bvhInfos[bvhA].m_nodeOffset;\n"
"						int endNodeIndexA = startNodeIndexA+subtreeA.m_subtreeSize;\n"
"						int startNodeIndexB = subtreeB.m_rootNodeIndex+bvhInfos[bvhB].m_nodeOffset;\n"
"						int endNodeIndexB = startNodeIndexB+subtreeB.m_subtreeSize;\n"
"						b3Int2 nodeStack[B3_MAX_STACK_DEPTH];\n"
"						b3Int2 node0;\n"
"						node0.x = startNodeIndexA;\n"
"						node0.y = startNodeIndexB;\n"
"						int maxStackDepth = B3_MAX_STACK_DEPTH;\n"
"						int depth=0;\n"
"						nodeStack[depth++]=node0;\n"
"						do\n"
"						{\n"
"							b3Int2 node = nodeStack[--depth];\n"
"							b3Float4 aMinLocal = MyUnQuantizeGlobal(quantizedNodes[node.x].m_quantizedAabbMin,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);\n"
"							b3Float4 aMaxLocal = MyUnQuantizeGlobal(quantizedNodes[node.x].m_quantizedAabbMax,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);\n"
"							b3Float4 bMinLocal = MyUnQuantizeGlobal(quantizedNodes[node.y].m_quantizedAabbMin,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);\n"
"							b3Float4 bMaxLocal = MyUnQuantizeGlobal(quantizedNodes[node.y].m_quantizedAabbMax,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);\n"
"							float margin=0.f;\n"
"							b3Float4 aabbAMinOut,aabbAMaxOut;\n"
"							b3TransformAabb2(aMinLocal,aMaxLocal, margin,posA,ornA,&aabbAMinOut,&aabbAMaxOut);\n"
"							b3Float4 aabbBMinOut,aabbBMaxOut;\n"
"							b3TransformAabb2(bMinLocal,bMaxLocal, margin,posB,ornB,&aabbBMinOut,&aabbBMaxOut);\n"
"							\n"
"							bool nodeOverlap = b3TestAabbAgainstAabb(aabbAMinOut,aabbAMaxOut,aabbBMinOut,aabbBMaxOut);\n"
"							if (nodeOverlap)\n"
"							{\n"
"								bool isLeafA = isLeafNodeGlobal(&quantizedNodes[node.x]);\n"
"								bool isLeafB = isLeafNodeGlobal(&quantizedNodes[node.y]);\n"
"								bool isInternalA = !isLeafA;\n"
"								bool isInternalB = !isLeafB;\n"
"								//fail, even though it might hit two leaf nodes\n"
"								if (depth+4>maxStackDepth && !(isLeafA && isLeafB))\n"
"								{\n"
"									//printf(\"Error: traversal exceeded maxStackDepth\");\n"
"									continue;\n"
"								}\n"
"								if(isInternalA)\n"
"								{\n"
"									int nodeAleftChild = node.x+1;\n"
"									bool isNodeALeftChildLeaf = isLeafNodeGlobal(&quantizedNodes[node.x+1]);\n"
"									int nodeArightChild = isNodeALeftChildLeaf? node.x+2 : node.x+1 + getEscapeIndexGlobal(&quantizedNodes[node.x+1]);\n"
"									if(isInternalB)\n"
"									{					\n"
"										int nodeBleftChild = node.y+1;\n"
"										bool isNodeBLeftChildLeaf = isLeafNodeGlobal(&quantizedNodes[node.y+1]);\n"
"										int nodeBrightChild = isNodeBLeftChildLeaf? node.y+2 : node.y+1 + getEscapeIndexGlobal(&quantizedNodes[node.y+1]);\n"
"										nodeStack[depth++] = b3MakeInt2(nodeAleftChild, nodeBleftChild);\n"
"										nodeStack[depth++] = b3MakeInt2(nodeArightChild, nodeBleftChild);\n"
"										nodeStack[depth++] = b3MakeInt2(nodeAleftChild, nodeBrightChild);\n"
"										nodeStack[depth++] = b3MakeInt2(nodeArightChild, nodeBrightChild);\n"
"									}\n"
"									else\n"
"									{\n"
"										nodeStack[depth++] = b3MakeInt2(nodeAleftChild,node.y);\n"
"										nodeStack[depth++] = b3MakeInt2(nodeArightChild,node.y);\n"
"									}\n"
"								}\n"
"								else\n"
"								{\n"
"									if(isInternalB)\n"
"									{\n"
"										int nodeBleftChild = node.y+1;\n"
"										bool isNodeBLeftChildLeaf = isLeafNodeGlobal(&quantizedNodes[node.y+1]);\n"
"										int nodeBrightChild = isNodeBLeftChildLeaf? node.y+2 : node.y+1 + getEscapeIndexGlobal(&quantizedNodes[node.y+1]);\n"
"										nodeStack[depth++] = b3MakeInt2(node.x,nodeBleftChild);\n"
"										nodeStack[depth++] = b3MakeInt2(node.x,nodeBrightChild);\n"
"									}\n"
"									else\n"
"									{\n"
"										int compoundPairIdx = atomic_inc(numCompoundPairsOut);\n"
"										if (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
"										{\n"
"											int childShapeIndexA = getTriangleIndexGlobal(&quantizedNodes[node.x]);\n"
"											int childShapeIndexB = getTriangleIndexGlobal(&quantizedNodes[node.y]);\n"
"											gpuCompoundPairsOut[compoundPairIdx]  = (int4)(bodyIndexA,bodyIndexB,childShapeIndexA,childShapeIndexB);\n"
"										}\n"
"									}\n"
"								}\n"
"							}\n"
"						} while (depth);\n"
"					}\n"
"				}\n"
"			}\n"
"			\n"
"			return;\n"
"		}\n"
"		if ((collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS) ||(collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))\n"
"		{\n"
"			if (collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS) \n"
"			{\n"
"				int numChildrenA = collidables[collidableIndexA].m_numChildShapes;\n"
"				for (int c=0;c<numChildrenA;c++)\n"
"				{\n"
"					int childShapeIndexA = collidables[collidableIndexA].m_shapeIndex+c;\n"
"					int childColIndexA = gpuChildShapes[childShapeIndexA].m_shapeIndex;\n"
"					float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
"					float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
"					float4 childPosA = gpuChildShapes[childShapeIndexA].m_childPosition;\n"
"					float4 childOrnA = gpuChildShapes[childShapeIndexA].m_childOrientation;\n"
"					float4 newPosA = qtRotate(ornA,childPosA)+posA;\n"
"					float4 newOrnA = qtMul(ornA,childOrnA);\n"
"					int shapeIndexA = collidables[childColIndexA].m_shapeIndex;\n"
"					b3Aabb_t aabbAlocal = aabbLocalSpace[shapeIndexA];\n"
"					float margin = 0.f;\n"
"					\n"
"					b3Float4 aabbAMinWS;\n"
"					b3Float4 aabbAMaxWS;\n"
"					\n"
"					b3TransformAabb2(aabbAlocal.m_minVec,aabbAlocal.m_maxVec,margin,\n"
"						newPosA,\n"
"						newOrnA,\n"
"						&aabbAMinWS,&aabbAMaxWS);\n"
"						\n"
"					\n"
"					if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
"					{\n"
"						int numChildrenB = collidables[collidableIndexB].m_numChildShapes;\n"
"						for (int b=0;b<numChildrenB;b++)\n"
"						{\n"
"							int childShapeIndexB = collidables[collidableIndexB].m_shapeIndex+b;\n"
"							int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;\n"
"							float4 ornB = rigidBodies[bodyIndexB].m_quat;\n"
"							float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
"							float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;\n"
"							float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;\n"
"							float4 newPosB = transform(&childPosB,&posB,&ornB);\n"
"							float4 newOrnB = qtMul(ornB,childOrnB);\n"
"							int shapeIndexB = collidables[childColIndexB].m_shapeIndex;\n"
"							b3Aabb_t aabbBlocal = aabbLocalSpace[shapeIndexB];\n"
"							\n"
"							b3Float4 aabbBMinWS;\n"
"							b3Float4 aabbBMaxWS;\n"
"							\n"
"							b3TransformAabb2(aabbBlocal.m_minVec,aabbBlocal.m_maxVec,margin,\n"
"								newPosB,\n"
"								newOrnB,\n"
"								&aabbBMinWS,&aabbBMaxWS);\n"
"								\n"
"								\n"
"							\n"
"							bool aabbOverlap = b3TestAabbAgainstAabb(aabbAMinWS,aabbAMaxWS,aabbBMinWS,aabbBMaxWS);\n"
"							if (aabbOverlap)\n"
"							{\n"
"								int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
"								float dmin = FLT_MAX;\n"
"								float4 posA = newPosA;\n"
"								posA.w = 0.f;\n"
"								float4 posB = newPosB;\n"
"								posB.w = 0.f;\n"
"								float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
"								float4 ornA = newOrnA;\n"
"								float4 c0 = transform(&c0local, &posA, &ornA);\n"
"								float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
"								float4 ornB =newOrnB;\n"
"								float4 c1 = transform(&c1local,&posB,&ornB);\n"
"								const float4 DeltaC2 = c0 - c1;\n"
"								{//\n"
"									int compoundPairIdx = atomic_inc(numCompoundPairsOut);\n"
"									if (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
"									{\n"
"										gpuCompoundPairsOut[compoundPairIdx]  = (int4)(bodyIndexA,bodyIndexB,childShapeIndexA,childShapeIndexB);\n"
"									}\n"
"								}//\n"
"							}//fi(1)\n"
"						} //for (int b=0\n"
"					}//if (collidables[collidableIndexB].\n"
"					else//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
"					{\n"
"						if (1)\n"
"						{\n"
"							int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
"							float dmin = FLT_MAX;\n"
"							float4 posA = newPosA;\n"
"							posA.w = 0.f;\n"
"							float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
"							posB.w = 0.f;\n"
"							float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
"							float4 ornA = newOrnA;\n"
"							float4 c0 = transform(&c0local, &posA, &ornA);\n"
"							float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
"							float4 ornB = rigidBodies[bodyIndexB].m_quat;\n"
"							float4 c1 = transform(&c1local,&posB,&ornB);\n"
"							const float4 DeltaC2 = c0 - c1;\n"
"							{\n"
"								int compoundPairIdx = atomic_inc(numCompoundPairsOut);\n"
"								if (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
"								{\n"
"									gpuCompoundPairsOut[compoundPairIdx] = (int4)(bodyIndexA,bodyIndexB,childShapeIndexA,-1);\n"
"								}//if (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
"							}//\n"
"						}//fi (1)\n"
"					}//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
"				}//for (int b=0;b<numChildrenB;b++)	\n"
"				return;\n"
"			}//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
"			if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONCAVE_TRIMESH) \n"
"				&& (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))\n"
"			{\n"
"				int numChildrenB = collidables[collidableIndexB].m_numChildShapes;\n"
"				for (int b=0;b<numChildrenB;b++)\n"
"				{\n"
"					int childShapeIndexB = collidables[collidableIndexB].m_shapeIndex+b;\n"
"					int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;\n"
"					float4 ornB = rigidBodies[bodyIndexB].m_quat;\n"
"					float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
"					float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;\n"
"					float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;\n"
"					float4 newPosB = qtRotate(ornB,childPosB)+posB;\n"
"					float4 newOrnB = qtMul(ornB,childOrnB);\n"
"					int shapeIndexB = collidables[childColIndexB].m_shapeIndex;\n"
"					//////////////////////////////////////\n"
"					if (1)\n"
"					{\n"
"						int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
"						float dmin = FLT_MAX;\n"
"						float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
"						posA.w = 0.f;\n"
"						float4 posB = newPosB;\n"
"						posB.w = 0.f;\n"
"						float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
"						float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
"						float4 c0 = transform(&c0local, &posA, &ornA);\n"
"						float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
"						float4 ornB =newOrnB;\n"
"						float4 c1 = transform(&c1local,&posB,&ornB);\n"
"						const float4 DeltaC2 = c0 - c1;\n"
"						{//\n"
"							int compoundPairIdx = atomic_inc(numCompoundPairsOut);\n"
"							if (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
"							{\n"
"								gpuCompoundPairsOut[compoundPairIdx] = (int4)(bodyIndexA,bodyIndexB,-1,childShapeIndexB);\n"
"							}//fi (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
"						}//\n"
"					}//fi (1)	\n"
"				}//for (int b=0;b<numChildrenB;b++)\n"
"				return;\n"
"			}//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
"			return;\n"
"		}//fi ((collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS) ||(collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))\n"
"	}//i<numPairs\n"
"}\n"
"// work-in-progress\n"
"__kernel void   findSeparatingAxisKernel( __global const int4* pairs, \n"
"																					__global const BodyData* rigidBodies, \n"
"																					__global const btCollidableGpu* collidables,\n"
"																					__global const ConvexPolyhedronCL* convexShapes, \n"
"																					__global const float4* vertices,\n"
"																					__global const float4* uniqueEdges,\n"
"																					__global const btGpuFace* faces,\n"
"																					__global const int* indices,\n"
"																					__global btAabbCL* aabbs,\n"
"																					__global volatile float4* separatingNormals,\n"
"																					__global volatile int* hasSeparatingAxis,\n"
"																					int numPairs\n"
"																					)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	\n"
"	if (i<numPairs)\n"
"	{\n"
"	\n"
"		int bodyIndexA = pairs[i].x;\n"
"		int bodyIndexB = pairs[i].y;\n"
"		int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
"		int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
"	\n"
"		int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
"		int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
"		\n"
"		\n"
"		//once the broadphase avoids static-static pairs, we can remove this test\n"
"		if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))\n"
"		{\n"
"			hasSeparatingAxis[i] = 0;\n"
"			return;\n"
"		}\n"
"		\n"
"		if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONVEX_HULL) ||(collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL))\n"
"		{\n"
"			hasSeparatingAxis[i] = 0;\n"
"			return;\n"
"		}\n"
"			\n"
"		if ((collidables[collidableIndexA].m_shapeType==SHAPE_CONCAVE_TRIMESH))\n"
"		{\n"
"			hasSeparatingAxis[i] = 0;\n"
"			return;\n"
"		}\n"
"		int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
"		float dmin = FLT_MAX;\n"
"		float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
"		posA.w = 0.f;\n"
"		float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
"		posB.w = 0.f;\n"
"		float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
"		float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
"		float4 c0 = transform(&c0local, &posA, &ornA);\n"
"		float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
"		float4 ornB =rigidBodies[bodyIndexB].m_quat;\n"
"		float4 c1 = transform(&c1local,&posB,&ornB);\n"
"		const float4 DeltaC2 = c0 - c1;\n"
"		float4 sepNormal;\n"
"		\n"
"		bool sepA = findSeparatingAxis(	&convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
"																								posB,ornB,\n"
"																								DeltaC2,\n"
"																								vertices,uniqueEdges,faces,\n"
"																								indices,&sepNormal,&dmin);\n"
"		hasSeparatingAxis[i] = 4;\n"
"		if (!sepA)\n"
"		{\n"
"			hasSeparatingAxis[i] = 0;\n"
"		} else\n"
"		{\n"
"			bool sepB = findSeparatingAxis(	&convexShapes[shapeIndexB],&convexShapes[shapeIndexA],posB,ornB,\n"
"																									posA,ornA,\n"
"																									DeltaC2,\n"
"																									vertices,uniqueEdges,faces,\n"
"																									indices,&sepNormal,&dmin);\n"
"			if (!sepB)\n"
"			{\n"
"				hasSeparatingAxis[i] = 0;\n"
"			} else\n"
"			{\n"
"				bool sepEE = findSeparatingAxisEdgeEdge(	&convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
"																									posB,ornB,\n"
"																									DeltaC2,\n"
"																									vertices,uniqueEdges,faces,\n"
"																									indices,&sepNormal,&dmin);\n"
"				if (!sepEE)\n"
"				{\n"
"					hasSeparatingAxis[i] = 0;\n"
"				} else\n"
"				{\n"
"					hasSeparatingAxis[i] = 1;\n"
"					separatingNormals[i] = sepNormal;\n"
"				}\n"
"			}\n"
"		}\n"
"		\n"
"	}\n"
"}\n"
"__kernel void   findSeparatingAxisVertexFaceKernel( __global const int4* pairs, \n"
"																					__global const BodyData* rigidBodies, \n"
"																					__global const btCollidableGpu* collidables,\n"
"																					__global const ConvexPolyhedronCL* convexShapes, \n"
"																					__global const float4* vertices,\n"
"																					__global const float4* uniqueEdges,\n"
"																					__global const btGpuFace* faces,\n"
"																					__global const int* indices,\n"
"																					__global btAabbCL* aabbs,\n"
"																					__global volatile float4* separatingNormals,\n"
"																					__global volatile int* hasSeparatingAxis,\n"
"																					__global  float* dmins,\n"
"																					int numPairs\n"
"																					)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	\n"
"	if (i<numPairs)\n"
"	{\n"
"	\n"
"		int bodyIndexA = pairs[i].x;\n"
"		int bodyIndexB = pairs[i].y;\n"
"		int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
"		int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
"	\n"
"		int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
"		int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
"	\n"
"		hasSeparatingAxis[i] = 0;	\n"
"		\n"
"		//once the broadphase avoids static-static pairs, we can remove this test\n"
"		if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))\n"
"		{\n"
"			return;\n"
"		}\n"
"		\n"
"		if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONVEX_HULL) ||(collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL))\n"
"		{\n"
"			return;\n"
"		}\n"
"			\n"
"		int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
"		float dmin = FLT_MAX;\n"
"		dmins[i] = dmin;\n"
"		\n"
"		float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
"		posA.w = 0.f;\n"
"		float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
"		posB.w = 0.f;\n"
"		float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
"		float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
"		float4 c0 = transform(&c0local, &posA, &ornA);\n"
"		float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
"		float4 ornB =rigidBodies[bodyIndexB].m_quat;\n"
"		float4 c1 = transform(&c1local,&posB,&ornB);\n"
"		const float4 DeltaC2 = c0 - c1;\n"
"		float4 sepNormal;\n"
"		\n"
"		bool sepA = findSeparatingAxis(	&convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
"																								posB,ornB,\n"
"																								DeltaC2,\n"
"																								vertices,uniqueEdges,faces,\n"
"																								indices,&sepNormal,&dmin);\n"
"		hasSeparatingAxis[i] = 4;\n"
"		if (!sepA)\n"
"		{\n"
"			hasSeparatingAxis[i] = 0;\n"
"		} else\n"
"		{\n"
"			bool sepB = findSeparatingAxis(	&convexShapes[shapeIndexB],&convexShapes[shapeIndexA],posB,ornB,\n"
"																									posA,ornA,\n"
"																									DeltaC2,\n"
"																									vertices,uniqueEdges,faces,\n"
"																									indices,&sepNormal,&dmin);\n"
"			if (sepB)\n"
"			{\n"
"				dmins[i] = dmin;\n"
"				hasSeparatingAxis[i] = 1;\n"
"				separatingNormals[i] = sepNormal;\n"
"			}\n"
"		}\n"
"		\n"
"	}\n"
"}\n"
"__kernel void   findSeparatingAxisEdgeEdgeKernel( __global const int4* pairs, \n"
"																					__global const BodyData* rigidBodies, \n"
"																					__global const btCollidableGpu* collidables,\n"
"																					__global const ConvexPolyhedronCL* convexShapes, \n"
"																					__global const float4* vertices,\n"
"																					__global const float4* uniqueEdges,\n"
"																					__global const btGpuFace* faces,\n"
"																					__global const int* indices,\n"
"																					__global btAabbCL* aabbs,\n"
"																					__global  float4* separatingNormals,\n"
"																					__global  int* hasSeparatingAxis,\n"
"																					__global  float* dmins,\n"
"																					__global const float4* unitSphereDirections,\n"
"																					int numUnitSphereDirections,\n"
"																					int numPairs\n"
"																					)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	\n"
"	if (i<numPairs)\n"
"	{\n"
"		if (hasSeparatingAxis[i])\n"
"		{\n"
"	\n"
"			int bodyIndexA = pairs[i].x;\n"
"			int bodyIndexB = pairs[i].y;\n"
"	\n"
"			int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
"			int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
"		\n"
"			int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
"			int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
"			\n"
"			\n"
"			int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
"	\n"
"			float dmin = dmins[i];\n"
"	\n"
"			float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
"			posA.w = 0.f;\n"
"			float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
"			posB.w = 0.f;\n"
"			float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
"			float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
"			float4 c0 = transform(&c0local, &posA, &ornA);\n"
"			float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
"			float4 ornB =rigidBodies[bodyIndexB].m_quat;\n"
"			float4 c1 = transform(&c1local,&posB,&ornB);\n"
"			const float4 DeltaC2 = c0 - c1;\n"
"			float4 sepNormal = separatingNormals[i];\n"
"			\n"
"			\n"
"			\n"
"			bool sepEE = false;\n"
"			int numEdgeEdgeDirections = convexShapes[shapeIndexA].m_numUniqueEdges*convexShapes[shapeIndexB].m_numUniqueEdges;\n"
"			if (numEdgeEdgeDirections<=numUnitSphereDirections)\n"
"			{\n"
"				sepEE = findSeparatingAxisEdgeEdge(	&convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
"																									posB,ornB,\n"
"																									DeltaC2,\n"
"																									vertices,uniqueEdges,faces,\n"
"																									indices,&sepNormal,&dmin);\n"
"																									\n"
"					if (!sepEE)\n"
"					{\n"
"						hasSeparatingAxis[i] = 0;\n"
"					} else\n"
"					{\n"
"						hasSeparatingAxis[i] = 1;\n"
"						separatingNormals[i] = sepNormal;\n"
"					}\n"
"			}\n"
"			/*\n"
"			///else case is a separate kernel, to make Mac OSX OpenCL compiler happy\n"
"			else\n"
"			{\n"
"				sepEE = findSeparatingAxisUnitSphere(&convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
"																									posB,ornB,\n"
"																									DeltaC2,\n"
"																									vertices,unitSphereDirections,numUnitSphereDirections,\n"
"																									&sepNormal,&dmin);\n"
"					if (!sepEE)\n"
"					{\n"
"						hasSeparatingAxis[i] = 0;\n"
"					} else\n"
"					{\n"
"						hasSeparatingAxis[i] = 1;\n"
"						separatingNormals[i] = sepNormal;\n"
"					}\n"
"			}\n"
"			*/\n"
"		}		//if (hasSeparatingAxis[i])\n"
"	}//(i<numPairs)\n"
"}\n"
"inline int	findClippingFaces(const float4 separatingNormal,\n"
"                      const ConvexPolyhedronCL* hullA, \n"
"					  __global const ConvexPolyhedronCL* hullB,\n"
"                      const float4 posA, const Quaternion ornA,const float4 posB, const Quaternion ornB,\n"
"                       __global float4* worldVertsA1,\n"
"                      __global float4* worldNormalsA1,\n"
"                      __global float4* worldVertsB1,\n"
"                      int capacityWorldVerts,\n"
"                      const float minDist, float maxDist,\n"
"					  const float4* verticesA,\n"
"                      const btGpuFace* facesA,\n"
"                      const int* indicesA,\n"
"					  __global const float4* verticesB,\n"
"                      __global const btGpuFace* facesB,\n"
"                      __global const int* indicesB,\n"
"                      __global int4* clippingFaces, int pairIndex)\n"
"{\n"
"	int numContactsOut = 0;\n"
"	int numWorldVertsB1= 0;\n"
"    \n"
"    \n"
"	int closestFaceB=0;\n"
"	float dmax = -FLT_MAX;\n"
"    \n"
"	{\n"
"		for(int face=0;face<hullB->m_numFaces;face++)\n"
"		{\n"
"			const float4 Normal = make_float4(facesB[hullB->m_faceOffset+face].m_plane.x,\n"
"                                              facesB[hullB->m_faceOffset+face].m_plane.y, facesB[hullB->m_faceOffset+face].m_plane.z,0.f);\n"
"			const float4 WorldNormal = qtRotate(ornB, Normal);\n"
"			float d = dot3F4(WorldNormal,separatingNormal);\n"
"			if (d > dmax)\n"
"			{\n"
"				dmax = d;\n"
"				closestFaceB = face;\n"
"			}\n"
"		}\n"
"	}\n"
"    \n"
"	{\n"
"		const btGpuFace polyB = facesB[hullB->m_faceOffset+closestFaceB];\n"
"		int numVertices = polyB.m_numIndices;\n"
"        if (numVertices>capacityWorldVerts)\n"
"            numVertices = capacityWorldVerts;\n"
"        \n"
"		for(int e0=0;e0<numVertices;e0++)\n"
"		{\n"
"            if (e0<capacityWorldVerts)\n"
"            {\n"
"                const float4 b = verticesB[hullB->m_vertexOffset+indicesB[polyB.m_indexOffset+e0]];\n"
"                worldVertsB1[pairIndex*capacityWorldVerts+numWorldVertsB1++] = transform(&b,&posB,&ornB);\n"
"            }\n"
"		}\n"
"	}\n"
"    \n"
"    int closestFaceA=0;\n"
"	{\n"
"		float dmin = FLT_MAX;\n"
"		for(int face=0;face<hullA->m_numFaces;face++)\n"
"		{\n"
"			const float4 Normal = make_float4(\n"
"                                              facesA[hullA->m_faceOffset+face].m_plane.x,\n"
"                                              facesA[hullA->m_faceOffset+face].m_plane.y,\n"
"                                              facesA[hullA->m_faceOffset+face].m_plane.z,\n"
"                                              0.f);\n"
"			const float4 faceANormalWS = qtRotate(ornA,Normal);\n"
"            \n"
"			float d = dot3F4(faceANormalWS,separatingNormal);\n"
"			if (d < dmin)\n"
"			{\n"
"				dmin = d;\n"
"				closestFaceA = face;\n"
"                worldNormalsA1[pairIndex] = faceANormalWS;\n"
"			}\n"
"		}\n"
"	}\n"
"    \n"
"    int numVerticesA = facesA[hullA->m_faceOffset+closestFaceA].m_numIndices;\n"
"    if (numVerticesA>capacityWorldVerts)\n"
"       numVerticesA = capacityWorldVerts;\n"
"    \n"
"	for(int e0=0;e0<numVerticesA;e0++)\n"
"	{\n"
"        if (e0<capacityWorldVerts)\n"
"        {\n"
"            const float4 a = verticesA[hullA->m_vertexOffset+indicesA[facesA[hullA->m_faceOffset+closestFaceA].m_indexOffset+e0]];\n"
"            worldVertsA1[pairIndex*capacityWorldVerts+e0] = transform(&a, &posA,&ornA);\n"
"        }\n"
"    }\n"
"    \n"
"    clippingFaces[pairIndex].x = closestFaceA;\n"
"    clippingFaces[pairIndex].y = closestFaceB;\n"
"    clippingFaces[pairIndex].z = numVerticesA;\n"
"    clippingFaces[pairIndex].w = numWorldVertsB1;\n"
"    \n"
"    \n"
"	return numContactsOut;\n"
"}\n"
"// work-in-progress\n"
"__kernel void   findConcaveSeparatingAxisKernel( __global int4* concavePairs,\n"
"																					__global const BodyData* rigidBodies,\n"
"																					__global const btCollidableGpu* collidables,\n"
"																					__global const ConvexPolyhedronCL* convexShapes, \n"
"																					__global const float4* vertices,\n"
"																					__global const float4* uniqueEdges,\n"
"																					__global const btGpuFace* faces,\n"
"																					__global const int* indices,\n"
"																					__global const btGpuChildShape* gpuChildShapes,\n"
"																					__global btAabbCL* aabbs,\n"
"																					__global float4* concaveSeparatingNormalsOut,\n"
"																					__global int* concaveHasSeparatingNormals,\n"
"																					__global int4* clippingFacesOut,\n"
"																					__global float4* worldVertsA1GPU,\n"
"																					__global float4*  worldNormalsAGPU,\n"
"																					__global float4* worldVertsB1GPU,\n"
"																					int vertexFaceCapacity,\n"
"																					int numConcavePairs\n"
"																					)\n"
"{\n"
"	int i = get_global_id(0);\n"
"	if (i>=numConcavePairs)\n"
"		return;\n"
"	concaveHasSeparatingNormals[i] = 0;\n"
"	int pairIdx = i;\n"
"	int bodyIndexA = concavePairs[i].x;\n"
"	int bodyIndexB = concavePairs[i].y;\n"
"	int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
"	int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
"	int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
"	int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
"	if (collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL&&\n"
"		collidables[collidableIndexB].m_shapeType!=SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
"	{\n"
"		concavePairs[pairIdx].w = -1;\n"
"		return;\n"
"	}\n"
"	int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
"	int numActualConcaveConvexTests = 0;\n"
"	\n"
"	int f = concavePairs[i].z;\n"
"	\n"
"	bool overlap = false;\n"
"	\n"
"	ConvexPolyhedronCL convexPolyhedronA;\n"
"	//add 3 vertices of the triangle\n"
"	convexPolyhedronA.m_numVertices = 3;\n"
"	convexPolyhedronA.m_vertexOffset = 0;\n"
"	float4	localCenter = make_float4(0.f,0.f,0.f,0.f);\n"
"	btGpuFace face = faces[convexShapes[shapeIndexA].m_faceOffset+f];\n"
"	float4 triMinAabb, triMaxAabb;\n"
"	btAabbCL triAabb;\n"
"	triAabb.m_min = make_float4(1e30f,1e30f,1e30f,0.f);\n"
"	triAabb.m_max = make_float4(-1e30f,-1e30f,-1e30f,0.f);\n"
"	\n"
"	float4 verticesA[3];\n"
"	for (int i=0;i<3;i++)\n"
"	{\n"
"		int index = indices[face.m_indexOffset+i];\n"
"		float4 vert = vertices[convexShapes[shapeIndexA].m_vertexOffset+index];\n"
"		verticesA[i] = vert;\n"
"		localCenter += vert;\n"
"			\n"
"		triAabb.m_min = min(triAabb.m_min,vert);		\n"
"		triAabb.m_max = max(triAabb.m_max,vert);		\n"
"	}\n"
"	overlap = true;\n"
"	overlap = (triAabb.m_min.x > aabbs[bodyIndexB].m_max.x || triAabb.m_max.x < aabbs[bodyIndexB].m_min.x) ? false : overlap;\n"
"	overlap = (triAabb.m_min.z > aabbs[bodyIndexB].m_max.z || triAabb.m_max.z < aabbs[bodyIndexB].m_min.z) ? false : overlap;\n"
"	overlap = (triAabb.m_min.y > aabbs[bodyIndexB].m_max.y || triAabb.m_max.y < aabbs[bodyIndexB].m_min.y) ? false : overlap;\n"
"		\n"
"	if (overlap)\n"
"	{\n"
"		float dmin = FLT_MAX;\n"
"		int hasSeparatingAxis=5;\n"
"		float4 sepAxis=make_float4(1,2,3,4);\n"
"		int localCC=0;\n"
"		numActualConcaveConvexTests++;\n"
"		//a triangle has 3 unique edges\n"
"		convexPolyhedronA.m_numUniqueEdges = 3;\n"
"		convexPolyhedronA.m_uniqueEdgesOffset = 0;\n"
"		float4 uniqueEdgesA[3];\n"
"		\n"
"		uniqueEdgesA[0] = (verticesA[1]-verticesA[0]);\n"
"		uniqueEdgesA[1] = (verticesA[2]-verticesA[1]);\n"
"		uniqueEdgesA[2] = (verticesA[0]-verticesA[2]);\n"
"		convexPolyhedronA.m_faceOffset = 0;\n"
"                                  \n"
"		float4 normal = make_float4(face.m_plane.x,face.m_plane.y,face.m_plane.z,0.f);\n"
"                             \n"
"		btGpuFace facesA[TRIANGLE_NUM_CONVEX_FACES];\n"
"		int indicesA[3+3+2+2+2];\n"
"		int curUsedIndices=0;\n"
"		int fidx=0;\n"
"		//front size of triangle\n"
"		{\n"
"			facesA[fidx].m_indexOffset=curUsedIndices;\n"
"			indicesA[0] = 0;\n"
"			indicesA[1] = 1;\n"
"			indicesA[2] = 2;\n"
"			curUsedIndices+=3;\n"
"			float c = face.m_plane.w;\n"
"			facesA[fidx].m_plane.x = normal.x;\n"
"			facesA[fidx].m_plane.y = normal.y;\n"
"			facesA[fidx].m_plane.z = normal.z;\n"
"			facesA[fidx].m_plane.w = c;\n"
"			facesA[fidx].m_numIndices=3;\n"
"		}\n"
"		fidx++;\n"
"		//back size of triangle\n"
"		{\n"
"			facesA[fidx].m_indexOffset=curUsedIndices;\n"
"			indicesA[3]=2;\n"
"			indicesA[4]=1;\n"
"			indicesA[5]=0;\n"
"			curUsedIndices+=3;\n"
"			float c = dot(normal,verticesA[0]);\n"
"			float c1 = -face.m_plane.w;\n"
"			facesA[fidx].m_plane.x = -normal.x;\n"
"			facesA[fidx].m_plane.y = -normal.y;\n"
"			facesA[fidx].m_plane.z = -normal.z;\n"
"			facesA[fidx].m_plane.w = c;\n"
"			facesA[fidx].m_numIndices=3;\n"
"		}\n"
"		fidx++;\n"
"		bool addEdgePlanes = true;\n"
"		if (addEdgePlanes)\n"
"		{\n"
"			int numVertices=3;\n"
"			int prevVertex = numVertices-1;\n"
"			for (int i=0;i<numVertices;i++)\n"
"			{\n"
"				float4 v0 = verticesA[i];\n"
"				float4 v1 = verticesA[prevVertex];\n"
"                                            \n"
"				float4 edgeNormal = normalize(cross(normal,v1-v0));\n"
"				float c = -dot(edgeNormal,v0);\n"
"				facesA[fidx].m_numIndices = 2;\n"
"				facesA[fidx].m_indexOffset=curUsedIndices;\n"
"				indicesA[curUsedIndices++]=i;\n"
"				indicesA[curUsedIndices++]=prevVertex;\n"
"                                            \n"
"				facesA[fidx].m_plane.x = edgeNormal.x;\n"
"				facesA[fidx].m_plane.y = edgeNormal.y;\n"
"				facesA[fidx].m_plane.z = edgeNormal.z;\n"
"				facesA[fidx].m_plane.w = c;\n"
"				fidx++;\n"
"				prevVertex = i;\n"
"			}\n"
"		}\n"
"		convexPolyhedronA.m_numFaces = TRIANGLE_NUM_CONVEX_FACES;\n"
"		convexPolyhedronA.m_localCenter = localCenter*(1.f/3.f);\n"
"		float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
"		posA.w = 0.f;\n"
"		float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
"		posB.w = 0.f;\n"
"		float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
"		float4 ornB =rigidBodies[bodyIndexB].m_quat;\n"
"		\n"
"		///////////////////\n"
"		///compound shape support\n"
"		if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
"		{\n"
"			int compoundChild = concavePairs[pairIdx].w;\n"
"			int childShapeIndexB = compoundChild;//collidables[collidableIndexB].m_shapeIndex+compoundChild;\n"
"			int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;\n"
"			float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;\n"
"			float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;\n"
"			float4 newPosB = transform(&childPosB,&posB,&ornB);\n"
"			float4 newOrnB = qtMul(ornB,childOrnB);\n"
"			posB = newPosB;\n"
"			ornB = newOrnB;\n"
"			shapeIndexB = collidables[childColIndexB].m_shapeIndex;\n"
"		}\n"
"		//////////////////\n"
"		float4 c0local = convexPolyhedronA.m_localCenter;\n"
"		float4 c0 = transform(&c0local, &posA, &ornA);\n"
"		float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
"		float4 c1 = transform(&c1local,&posB,&ornB);\n"
"		const float4 DeltaC2 = c0 - c1;\n"
"		bool sepA = findSeparatingAxisLocalA(	&convexPolyhedronA, &convexShapes[shapeIndexB],\n"
"												posA,ornA,\n"
"												posB,ornB,\n"
"												DeltaC2,\n"
"												verticesA,uniqueEdgesA,facesA,indicesA,\n"
"												vertices,uniqueEdges,faces,indices,\n"
"												&sepAxis,&dmin);\n"
"		hasSeparatingAxis = 4;\n"
"		if (!sepA)\n"
"		{\n"
"			hasSeparatingAxis = 0;\n"
"		} else\n"
"		{\n"
"			bool sepB = findSeparatingAxisLocalB(	&convexShapes[shapeIndexB],&convexPolyhedronA,\n"
"												posB,ornB,\n"
"												posA,ornA,\n"
"												DeltaC2,\n"
"												vertices,uniqueEdges,faces,indices,\n"
"												verticesA,uniqueEdgesA,facesA,indicesA,\n"
"												&sepAxis,&dmin);\n"
"			if (!sepB)\n"
"			{\n"
"				hasSeparatingAxis = 0;\n"
"			} else\n"
"			{\n"
"				bool sepEE = findSeparatingAxisEdgeEdgeLocalA(	&convexPolyhedronA, &convexShapes[shapeIndexB],\n"
"															posA,ornA,\n"
"															posB,ornB,\n"
"															DeltaC2,\n"
"															verticesA,uniqueEdgesA,facesA,indicesA,\n"
"															vertices,uniqueEdges,faces,indices,\n"
"															&sepAxis,&dmin);\n"
"	\n"
"				if (!sepEE)\n"
"				{\n"
"					hasSeparatingAxis = 0;\n"
"				} else\n"
"				{\n"
"					hasSeparatingAxis = 1;\n"
"				}\n"
"			}\n"
"		}	\n"
"		\n"
"		if (hasSeparatingAxis)\n"
"		{\n"
"			sepAxis.w = dmin;\n"
"			concaveSeparatingNormalsOut[pairIdx]=sepAxis;\n"
"			concaveHasSeparatingNormals[i]=1;\n"
"			float minDist = -1e30f;\n"
"			float maxDist = 0.02f;\n"
"		\n"
"			findClippingFaces(sepAxis,\n"
"                     &convexPolyhedronA,\n"
"					 &convexShapes[shapeIndexB],\n"
"					 posA,ornA,\n"
"					 posB,ornB,\n"
"                      worldVertsA1GPU,\n"
"                      worldNormalsAGPU,\n"
"                      worldVertsB1GPU,\n"
"					  vertexFaceCapacity,\n"
"                      minDist, maxDist,\n"
"                      verticesA,\n"
"                      facesA,\n"
"                      indicesA,\n"
" 					  vertices,\n"
"                      faces,\n"
"                      indices,\n"
"                      clippingFacesOut, pairIdx);\n"
"		} else\n"
"		{	\n"
"			//mark this pair as in-active\n"
"			concavePairs[pairIdx].w = -1;\n"
"		}\n"
"	}\n"
"	else\n"
"	{	\n"
"		//mark this pair as in-active\n"
"		concavePairs[pairIdx].w = -1;\n"
"	}\n"
"	\n"
"	concavePairs[pairIdx].z = -1;//now z is used for existing/persistent contacts\n"
"}\n"
;