File: mojoshader.h

package info (click to toggle)
mojoshader 0.0~hg1314%2Bdfsg-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 3,136 kB
  • sloc: ansic: 50,797; perl: 151; sh: 17; makefile: 11
file content (4110 lines) | stat: -rw-r--r-- 160,820 bytes parent folder | download
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
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
/**
 * MojoShader; generate shader programs from bytecode of compiled
 *  Direct3D shaders.
 *
 * Please see the file LICENSE.txt in the source's root directory.
 *
 *  This file written by Ryan C. Gordon.
 */

#ifndef _INCL_MOJOSHADER_H_
#define _INCL_MOJOSHADER_H_

#ifdef __cplusplus
extern "C" {
#endif

/* You can define this if you aren't generating mojoshader_version.h */
#ifndef MOJOSHADER_NO_VERSION_INCLUDE
#include "mojoshader_version.h"
#endif

#ifndef MOJOSHADER_VERSION
#define MOJOSHADER_VERSION -1
#endif

#ifndef MOJOSHADER_CHANGESET
#define MOJOSHADER_CHANGESET "???"
#endif

#ifndef DECLSPEC
#ifdef _WIN32
#define DECLSPEC __declspec(dllexport)
#else
#define DECLSPEC
#endif
#endif

#ifndef MOJOSHADERCALL
#ifdef _WIN32
#define MOJOSHADERCALL __cdecl
#else
#define MOJOSHADERCALL
#endif
#endif

/* -Wpedantic nameless union/struct silencing */
#ifndef MOJOSHADERNAMELESS
#ifdef __GNUC__
#define MOJOSHADERNAMELESS __extension__
#else
#define MOJOSHADERNAMELESS
#endif /* __GNUC__ */
#endif /* MOJOSHADERNAMELESS */

/*
 * For determining the version of MojoShader you are using:
 *    const int compiled_against = MOJOSHADER_VERSION;
 *    const int linked_against = MOJOSHADER_version();
 *
 * The version is a single integer that increments, not a major/minor value.
 */
DECLSPEC int MOJOSHADER_version(void);

/*
 * For determining the revision control changeset of MojoShader you are using:
 *    const char *compiled_against = MOJOSHADER_CHANGESET;
 *    const char *linked_against = MOJOSHADER_changeset();
 *
 * The version is an arbitrary, null-terminated ASCII string. It is probably
 *  a hash that represents a revision control changeset, and can't be
 *  compared to any other string to determine chronology.
 *
 * Do not attempt to free this string; it's statically allocated.
 */
DECLSPEC const char *MOJOSHADER_changeset(void);

/*
 * These allocators work just like the C runtime's malloc() and free()
 *  (in fact, they probably use malloc() and free() internally if you don't
 *  specify your own allocator, but don't rely on that behaviour).
 * (data) is the pointer you supplied when specifying these allocator
 *  callbacks, in case you need instance-specific data...it is passed through
 *  to your allocator unmolested, and can be NULL if you like.
 */
typedef void *(MOJOSHADERCALL *MOJOSHADER_malloc)(int bytes, void *data);
typedef void (MOJOSHADERCALL *MOJOSHADER_free)(void *ptr, void *data);


/*
 * These are enum values, but they also can be used in bitmasks, so we can
 *  test if an opcode is acceptable: if (op->shader_types & ourtype) {} ...
 */
typedef enum
{
    MOJOSHADER_TYPE_UNKNOWN  = 0,
    MOJOSHADER_TYPE_PIXEL    = (1 << 0),
    MOJOSHADER_TYPE_VERTEX   = (1 << 1),
    MOJOSHADER_TYPE_GEOMETRY = (1 << 2),  /* (not supported yet.) */
    MOJOSHADER_TYPE_ANY = 0x7FFFFFFF   /* used for bitmasks */
} MOJOSHADER_shaderType;

/*
 * Data types for vertex attribute streams.
 */
typedef enum
{
    MOJOSHADER_ATTRIBUTE_UNKNOWN = -1,  /* housekeeping; not returned. */
    MOJOSHADER_ATTRIBUTE_BYTE,
    MOJOSHADER_ATTRIBUTE_UBYTE,
    MOJOSHADER_ATTRIBUTE_SHORT,
    MOJOSHADER_ATTRIBUTE_USHORT,
    MOJOSHADER_ATTRIBUTE_INT,
    MOJOSHADER_ATTRIBUTE_UINT,
    MOJOSHADER_ATTRIBUTE_FLOAT,
    MOJOSHADER_ATTRIBUTE_DOUBLE,
    MOJOSHADER_ATTRIBUTE_HALF_FLOAT  /* MAYBE available in your OpenGL! */
} MOJOSHADER_attributeType;

/*
 * Data types for uniforms. See MOJOSHADER_uniform for more information.
 */
typedef enum
{
    MOJOSHADER_UNIFORM_UNKNOWN = -1, /* housekeeping value; never returned. */
    MOJOSHADER_UNIFORM_FLOAT,
    MOJOSHADER_UNIFORM_INT,
    MOJOSHADER_UNIFORM_BOOL
} MOJOSHADER_uniformType;

/*
 * These are the uniforms to be set for a shader. "Uniforms" are what Direct3D
 *  calls "Constants" ... IDirect3DDevice::SetVertexShaderConstantF() would
 *  need this data, for example. These integers are register indexes. So if
 *  index==6 and type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a
 *  4-float vector to be specified for what would be register "c6" in D3D
 *  assembly language, before drawing with the shader.
 * (array_count) means this is an array of uniforms...this happens in some
 *  profiles when we see a relative address ("c0[a0.x]", not the usual "c0").
 *  In those cases, the shader was built to set some range of constant
 *  registers as an array. You should set this array with (array_count)
 *  elements from the constant register file, starting at (index) instead of
 *  just a single uniform. To be extra difficult, you'll need to fill in the
 *  correct values from the MOJOSHADER_constant data into the appropriate
 *  parts of the array, overriding the constant register file. Fun!
 * (constant) says whether this is a constant array; these need to be loaded
 *  once at creation time, from the constant list and not ever updated from
 *  the constant register file. This is a workaround for limitations in some
 *  profiles.
 * (name) is a profile-specific variable name; it may be NULL if it isn't
 *  applicable to the requested profile.
 */
typedef struct MOJOSHADER_uniform
{
    MOJOSHADER_uniformType type;
    int index;
    int array_count;
    int constant;
    const char *name;
} MOJOSHADER_uniform;

/*
 * These are the constants defined in a shader. These are data values
 *  hardcoded in a shader (with the DEF, DEFI, DEFB instructions), which
 *  override your Uniforms. This data is largely for informational purposes,
 *  since they are compiled in and can't be changed, like Uniforms can be.
 * These integers are register indexes. So if index==6 and
 *  type==MOJOSHADER_UNIFORM_FLOAT, that means we'd expect a 4-float vector
 *  to be specified for what would be register "c6" in D3D assembly language,
 *  before drawing with the shader.
 * (value) is the value of the constant, unioned by type.
 */
typedef struct MOJOSHADER_constant
{
    MOJOSHADER_uniformType type;
    int index;
    union
    {
        float f[4];  /* if type==MOJOSHADER_UNIFORM_FLOAT */
        int i[4];    /* if type==MOJOSHADER_UNIFORM_INT */
        int b;       /* if type==MOJOSHADER_UNIFORM_BOOL */
    } value;
} MOJOSHADER_constant;

/*
 * Data types for samplers. See MOJOSHADER_sampler for more information.
 */
typedef enum
{
    MOJOSHADER_SAMPLER_UNKNOWN = -1, /* housekeeping value; never returned. */
    MOJOSHADER_SAMPLER_2D,
    MOJOSHADER_SAMPLER_CUBE,
    MOJOSHADER_SAMPLER_VOLUME
} MOJOSHADER_samplerType;

/*
 * These are the samplers to be set for a shader. ...
 *  IDirect3DDevice::SetTexture() would need this data, for example.
 * These integers are the sampler "stage". So if index==6 and
 *  type==MOJOSHADER_SAMPLER_2D, that means we'd expect a regular 2D texture
 *  to be specified for what would be register "s6" in D3D assembly language,
 *  before drawing with the shader.
 * (name) is a profile-specific variable name; it may be NULL if it isn't
 *  applicable to the requested profile.
 * (texbem) will be non-zero if a TEXBEM opcode references this sampler. This
 *  is only used in legacy shaders (ps_1_1 through ps_1_3), but it needs some
 *  special support to work, as we have to load a magic uniform behind the
 *  scenes to support it. Most code can ignore this field in general, and no
 *  one has to touch it unless they really know what they're doing.
 */
typedef struct MOJOSHADER_sampler
{
    MOJOSHADER_samplerType type;
    int index;
    const char *name;
    int texbem;
} MOJOSHADER_sampler;


/*
 * This struct is used if you have to force a sampler to a specific type.
 *  Generally, you can ignore this, but if you have, say, a ps_1_1
 *  shader, you might need to specify what the samplers are meant to be
 *  to get correct results, as Shader Model 1 samples textures according
 *  to what is bound to a sampler at the moment instead of what the shader
 *  is hardcoded to expect.
 */
typedef struct MOJOSHADER_samplerMap
{
    int index;
    MOJOSHADER_samplerType type;
} MOJOSHADER_samplerMap;

/*
 * Data types for attributes. See MOJOSHADER_attribute for more information.
 */
typedef enum
{
    MOJOSHADER_USAGE_UNKNOWN = -1,  /* housekeeping value; never returned. */
    MOJOSHADER_USAGE_POSITION,      /* 0-15 for Vertex, 1-15 for Pixel */
    MOJOSHADER_USAGE_BLENDWEIGHT,   /* 0-15 */
    MOJOSHADER_USAGE_BLENDINDICES,  /* 0-15 */
    MOJOSHADER_USAGE_NORMAL,        /* 0-15 */
    MOJOSHADER_USAGE_POINTSIZE,     /* 0-15 */
    MOJOSHADER_USAGE_TEXCOORD,      /* 0-15 */
    MOJOSHADER_USAGE_TANGENT,       /* 0-15 */
    MOJOSHADER_USAGE_BINORMAL,      /* 0-15 */
    MOJOSHADER_USAGE_TESSFACTOR,    /* 0 only */
    MOJOSHADER_USAGE_POSITIONT,     /* 0-15 for Vertex, 1-15 for Pixel */
    MOJOSHADER_USAGE_COLOR,         /* 0-15 but depends on MRT support */
    MOJOSHADER_USAGE_FOG,           /* 0-15 */
    MOJOSHADER_USAGE_DEPTH,         /* 0-15 */
    MOJOSHADER_USAGE_SAMPLE,
    MOJOSHADER_USAGE_TOTAL   /* housekeeping value; never returned. */
} MOJOSHADER_usage;

/*
 * These are the attributes to be set for a shader. "Attributes" are what
 *  Direct3D calls "Vertex Declarations Usages" ...
 *  IDirect3DDevice::CreateVertexDeclaration() would need this data, for
 *  example. Each attribute is associated with an array of data that uses one
 *  element per-vertex. So if usage==MOJOSHADER_USAGE_COLOR and index==1, that
 *  means we'd expect a secondary color array to be bound to this shader
 *  before drawing.
 * (name) is a profile-specific variable name; it may be NULL if it isn't
 *  applicable to the requested profile.
 */
typedef struct MOJOSHADER_attribute
{
    MOJOSHADER_usage usage;
    int index;
    const char *name;
} MOJOSHADER_attribute;

/*
 * Use this if you want to specify newly-parsed code to swizzle incoming
 *  data. This can be useful if you know, at parse time, that a shader
 *  will be processing data on COLOR0 that should be RGBA, but you'll
 *  be passing it a vertex array full of ARGB instead.
 */
typedef struct MOJOSHADER_swizzle
{
    MOJOSHADER_usage usage;
    unsigned int index;
    unsigned char swizzles[4];  /* {0,1,2,3} == .xyzw, {2,2,2,2} == .zzzz */
} MOJOSHADER_swizzle;


/*
 * MOJOSHADER_symbol data.
 *
 * These are used to expose high-level information in shader bytecode.
 *  They associate HLSL variables with registers. This data is used for both
 *  debugging and optimization.
 */

typedef enum
{
    MOJOSHADER_SYMREGSET_BOOL=0,
    MOJOSHADER_SYMREGSET_INT4,
    MOJOSHADER_SYMREGSET_FLOAT4,
    MOJOSHADER_SYMREGSET_SAMPLER,
    MOJOSHADER_SYMREGSET_TOTAL    /* housekeeping value; never returned. */
} MOJOSHADER_symbolRegisterSet;

typedef enum
{
    MOJOSHADER_SYMCLASS_SCALAR=0,
    MOJOSHADER_SYMCLASS_VECTOR,
    MOJOSHADER_SYMCLASS_MATRIX_ROWS,
    MOJOSHADER_SYMCLASS_MATRIX_COLUMNS,
    MOJOSHADER_SYMCLASS_OBJECT,
    MOJOSHADER_SYMCLASS_STRUCT,
    MOJOSHADER_SYMCLASS_TOTAL    /* housekeeping value; never returned. */
} MOJOSHADER_symbolClass;

typedef enum
{
    MOJOSHADER_SYMTYPE_VOID=0,
    MOJOSHADER_SYMTYPE_BOOL,
    MOJOSHADER_SYMTYPE_INT,
    MOJOSHADER_SYMTYPE_FLOAT,
    MOJOSHADER_SYMTYPE_STRING,
    MOJOSHADER_SYMTYPE_TEXTURE,
    MOJOSHADER_SYMTYPE_TEXTURE1D,
    MOJOSHADER_SYMTYPE_TEXTURE2D,
    MOJOSHADER_SYMTYPE_TEXTURE3D,
    MOJOSHADER_SYMTYPE_TEXTURECUBE,
    MOJOSHADER_SYMTYPE_SAMPLER,
    MOJOSHADER_SYMTYPE_SAMPLER1D,
    MOJOSHADER_SYMTYPE_SAMPLER2D,
    MOJOSHADER_SYMTYPE_SAMPLER3D,
    MOJOSHADER_SYMTYPE_SAMPLERCUBE,
    MOJOSHADER_SYMTYPE_PIXELSHADER,
    MOJOSHADER_SYMTYPE_VERTEXSHADER,
    MOJOSHADER_SYMTYPE_PIXELFRAGMENT,
    MOJOSHADER_SYMTYPE_VERTEXFRAGMENT,
    MOJOSHADER_SYMTYPE_UNSUPPORTED,
    MOJOSHADER_SYMTYPE_TOTAL    /* housekeeping value; never returned. */
} MOJOSHADER_symbolType;

typedef struct MOJOSHADER_symbolStructMember MOJOSHADER_symbolStructMember;

typedef struct MOJOSHADER_symbolTypeInfo
{
    MOJOSHADER_symbolClass parameter_class;
    MOJOSHADER_symbolType parameter_type;
    unsigned int rows;
    unsigned int columns;
    unsigned int elements;
    unsigned int member_count;
    MOJOSHADER_symbolStructMember *members;
} MOJOSHADER_symbolTypeInfo;

struct MOJOSHADER_symbolStructMember
{
    const char *name;
    MOJOSHADER_symbolTypeInfo info;
};

typedef struct MOJOSHADER_symbol
{
    const char *name;
    MOJOSHADER_symbolRegisterSet register_set;
    unsigned int register_index;
    unsigned int register_count;
    MOJOSHADER_symbolTypeInfo info;
} MOJOSHADER_symbol;


/*
 * These are used with MOJOSHADER_error as special case positions.
 */
#define MOJOSHADER_POSITION_NONE (-3)
#define MOJOSHADER_POSITION_BEFORE (-2)
#define MOJOSHADER_POSITION_AFTER (-1)

typedef struct MOJOSHADER_error
{
    /*
     * Human-readable error, if there is one. Will be NULL if there was no
     *  error. The string will be UTF-8 encoded, and English only. Most of
     *  these shouldn't be shown to the end-user anyhow.
     */
    const char *error;

    /*
     * Filename where error happened. This can be NULL if the information
     *  isn't available.
     */
    const char *filename;

    /*
     * Position of error, if there is one. Will be MOJOSHADER_POSITION_NONE if
     *  there was no error, MOJOSHADER_POSITION_BEFORE if there was an error
     *  before processing started, and MOJOSHADER_POSITION_AFTER if there was
     *  an error during final processing. If >= 0, MOJOSHADER_parse() sets
     *  this to the byte offset (starting at zero) into the bytecode you
     *  supplied, and MOJOSHADER_assemble(), MOJOSHADER_parseAst(), and
     *  MOJOSHADER_compile() sets this to a a line number in the source code
     *  you supplied (starting at one).
     */
    int error_position;
} MOJOSHADER_error;


/* !!! FIXME: document me. */
typedef enum MOJOSHADER_preshaderOpcode
{
    MOJOSHADER_PRESHADEROP_NOP,
    MOJOSHADER_PRESHADEROP_MOV,
    MOJOSHADER_PRESHADEROP_NEG,
    MOJOSHADER_PRESHADEROP_RCP,
    MOJOSHADER_PRESHADEROP_FRC,
    MOJOSHADER_PRESHADEROP_EXP,
    MOJOSHADER_PRESHADEROP_LOG,
    MOJOSHADER_PRESHADEROP_RSQ,
    MOJOSHADER_PRESHADEROP_SIN,
    MOJOSHADER_PRESHADEROP_COS,
    MOJOSHADER_PRESHADEROP_ASIN,
    MOJOSHADER_PRESHADEROP_ACOS,
    MOJOSHADER_PRESHADEROP_ATAN,
    MOJOSHADER_PRESHADEROP_MIN,
    MOJOSHADER_PRESHADEROP_MAX,
    MOJOSHADER_PRESHADEROP_LT,
    MOJOSHADER_PRESHADEROP_GE,
    MOJOSHADER_PRESHADEROP_ADD,
    MOJOSHADER_PRESHADEROP_MUL,
    MOJOSHADER_PRESHADEROP_ATAN2,
    MOJOSHADER_PRESHADEROP_DIV,
    MOJOSHADER_PRESHADEROP_CMP,
    MOJOSHADER_PRESHADEROP_MOVC,
    MOJOSHADER_PRESHADEROP_DOT,
    MOJOSHADER_PRESHADEROP_NOISE,
    MOJOSHADER_PRESHADEROP_SCALAR_OPS,
    MOJOSHADER_PRESHADEROP_MIN_SCALAR = MOJOSHADER_PRESHADEROP_SCALAR_OPS,
    MOJOSHADER_PRESHADEROP_MAX_SCALAR,
    MOJOSHADER_PRESHADEROP_LT_SCALAR,
    MOJOSHADER_PRESHADEROP_GE_SCALAR,
    MOJOSHADER_PRESHADEROP_ADD_SCALAR,
    MOJOSHADER_PRESHADEROP_MUL_SCALAR,
    MOJOSHADER_PRESHADEROP_ATAN2_SCALAR,
    MOJOSHADER_PRESHADEROP_DIV_SCALAR,
    MOJOSHADER_PRESHADEROP_DOT_SCALAR,
    MOJOSHADER_PRESHADEROP_NOISE_SCALAR
} MOJOSHADER_preshaderOpcode;

typedef enum MOJOSHADER_preshaderOperandType
{
    MOJOSHADER_PRESHADEROPERAND_INPUT,
    MOJOSHADER_PRESHADEROPERAND_OUTPUT,
    MOJOSHADER_PRESHADEROPERAND_LITERAL,
    MOJOSHADER_PRESHADEROPERAND_TEMP
} MOJOSHADER_preshaderOperandType;

typedef struct MOJOSHADER_preshaderOperand
{
    MOJOSHADER_preshaderOperandType type;
    unsigned int index;
    unsigned int array_register_count;
    unsigned int *array_registers;
} MOJOSHADER_preshaderOperand;

typedef struct MOJOSHADER_preshaderInstruction
{
    MOJOSHADER_preshaderOpcode opcode;
    unsigned int element_count;
    unsigned int operand_count;
    MOJOSHADER_preshaderOperand operands[4];
} MOJOSHADER_preshaderInstruction;

typedef struct MOJOSHADER_preshader
{
    unsigned int literal_count;
    double *literals;
    unsigned int temp_count;  /* scalar, not vector! */
    unsigned int symbol_count;
    MOJOSHADER_symbol *symbols;
    unsigned int instruction_count;
    MOJOSHADER_preshaderInstruction *instructions;
    unsigned int register_count;
    float *registers;
    MOJOSHADER_malloc malloc;
    MOJOSHADER_free free;
    void *malloc_data;
} MOJOSHADER_preshader;

/*
 * Structure used to return data from parsing of a shader...
 */
/* !!! FIXME: most of these ints should be unsigned. */
typedef struct MOJOSHADER_parseData
{
    /*
     * The number of elements pointed to by (errors).
     */
    int error_count;

    /*
     * (error_count) elements of data that specify errors that were generated
     *  by parsing this shader.
     * This can be NULL if there were no errors or if (error_count) is zero.
     */
    MOJOSHADER_error *errors;

    /*
     * The name of the profile used to parse the shader. Will be NULL on error.
     */
    const char *profile;

    /*
     * Bytes of output from parsing. Most profiles produce a string of source
     *  code, but profiles that do binary output may not be text at all.
     *  Will be NULL on error.
     */
    const char *output;

    /*
     * Byte count for output, not counting any null terminator. Most profiles
     *  produce an ASCII string of source code (which will be null-terminated
     *  even though that null char isn't included in output_len), but profiles
     *  that do binary output may not be text at all. Will be 0 on error.
     */
    int output_len;

    /*
     * Count of Direct3D instruction slots used. This is meaningless in terms
     *  of the actual output, as the profile will probably grow or reduce
     *  the count (or for high-level languages, not have that information at
     *  all). Also, as with Microsoft's own assembler, this value is just a
     *  rough estimate, as unpredicable real-world factors make the actual
     *  value vary at least a little from this count. Still, it can give you
     *  a rough idea of the size of your shader. Will be zero on error.
     */
    int instruction_count;

    /*
     * The type of shader we parsed. Will be MOJOSHADER_TYPE_UNKNOWN on error.
     */
    MOJOSHADER_shaderType shader_type;

    /*
     * The shader's major version. If this was a "vs_3_0", this would be 3.
     */
    int major_ver;

    /*
     * The shader's minor version. If this was a "ps_1_4", this would be 4.
     *  Two notes: for "vs_2_x", this is 1, and for "vs_3_sw", this is 255.
     */
    int minor_ver;

    /*
     * This is the main function name of the shader. This will be the
     *  caller-supplied string even if a given profile ignores it (GLSL,
     *  for example, always uses "main" in the shader output out of necessity,
     *  and Direct3D assembly has no concept of a "main function", etc).
     *  Otherwise, it'll be a default name chosen by the profile ("main") or
     *  whatnot.
     */
    const char *mainfn;

    /*
     * The number of elements pointed to by (uniforms).
     */
    int uniform_count;

    /*
     * (uniform_count) elements of data that specify Uniforms to be set for
     *  this shader. See discussion on MOJOSHADER_uniform for details.
     * This can be NULL on error or if (uniform_count) is zero.
     */
    MOJOSHADER_uniform *uniforms;

    /*
     * The number of elements pointed to by (constants).
     */
    int constant_count;

    /*
     * (constant_count) elements of data that specify constants used in
     *  this shader. See discussion on MOJOSHADER_constant for details.
     * This can be NULL on error or if (constant_count) is zero.
     *  This is largely informational: constants are hardcoded into a shader.
     *  The constants that you can set like parameters are in the "uniforms"
     *  list.
     */
    MOJOSHADER_constant *constants;

    /*
     * The number of elements pointed to by (samplers).
     */
    int sampler_count;

    /*
     * (sampler_count) elements of data that specify Samplers to be set for
     *  this shader. See discussion on MOJOSHADER_sampler for details.
     * This can be NULL on error or if (sampler_count) is zero.
     */
    MOJOSHADER_sampler *samplers;

    /* !!! FIXME: this should probably be "input" and not "attribute" */
    /*
     * The number of elements pointed to by (attributes).
     */
    int attribute_count;

    /* !!! FIXME: this should probably be "input" and not "attribute" */
    /*
     * (attribute_count) elements of data that specify Attributes to be set
     *  for this shader. See discussion on MOJOSHADER_attribute for details.
     * This can be NULL on error or if (attribute_count) is zero.
     */
    MOJOSHADER_attribute *attributes;

    /*
     * The number of elements pointed to by (outputs).
     */
    int output_count;

    /*
     * (output_count) elements of data that specify outputs this shader
     *  writes to. See discussion on MOJOSHADER_attribute for details.
     * This can be NULL on error or if (output_count) is zero.
     */
    MOJOSHADER_attribute *outputs;

    /*
     * The number of elements pointed to by (swizzles).
     */
    int swizzle_count;

    /* !!! FIXME: this should probably be "input" and not "attribute" */
    /*
     * (swizzle_count) elements of data that specify swizzles the shader will
     *  apply to incoming attributes. This is a copy of what was passed to
     *  MOJOSHADER_parseData().
     * This can be NULL on error or if (swizzle_count) is zero.
     */
    MOJOSHADER_swizzle *swizzles;

    /*
     * The number of elements pointed to by (symbols).
     */
    int symbol_count;

    /*
     * (symbol_count) elements of data that specify high-level symbol data
     *  for the shader. This will be parsed from the CTAB section
     *  in bytecode, and will be a copy of what you provide to
     *  MOJOSHADER_assemble(). This data is optional.
     * This can be NULL on error or if (symbol_count) is zero.
     */
    MOJOSHADER_symbol *symbols;

    /*
     * !!! FIXME: document me.
     * This can be NULL on error or if no preshader was available.
     */
    MOJOSHADER_preshader *preshader;

    /*
     * This is the malloc implementation you passed to MOJOSHADER_parse().
     */
    MOJOSHADER_malloc malloc;

    /*
     * This is the free implementation you passed to MOJOSHADER_parse().
     */
    MOJOSHADER_free free;

    /*
     * This is the pointer you passed as opaque data for your allocator.
     */
    void *malloc_data;
} MOJOSHADER_parseData;


/*
 * Profile string for Direct3D assembly language output.
 */
#define MOJOSHADER_PROFILE_D3D "d3d"

/*
 * Profile string for passthrough of the original bytecode, unchanged.
 */
#define MOJOSHADER_PROFILE_BYTECODE "bytecode"

/*
 * Profile string for HLSL Shader Model 4 output.
 */
#define MOJOSHADER_PROFILE_HLSL "hlsl"

/*
 * Profile string for GLSL: OpenGL high-level shader language output.
 */
#define MOJOSHADER_PROFILE_GLSL "glsl"

/*
 * Profile string for GLSL 1.20: minor improvements to base GLSL spec.
 */
#define MOJOSHADER_PROFILE_GLSL120 "glsl120"

/*
 * Profile string for GLSL ES: minor changes to GLSL output for ES compliance.
 */
#define MOJOSHADER_PROFILE_GLSLES "glsles"

/*
 * Profile string for OpenGL ARB 1.0 shaders: GL_ARB_(vertex|fragment)_program.
 */
#define MOJOSHADER_PROFILE_ARB1 "arb1"

/*
 * Profile string for OpenGL ARB 1.0 shaders with Nvidia 2.0 extensions:
 *  GL_NV_vertex_program2_option and GL_NV_fragment_program2
 */
#define MOJOSHADER_PROFILE_NV2 "nv2"

/*
 * Profile string for OpenGL ARB 1.0 shaders with Nvidia 3.0 extensions:
 *  GL_NV_vertex_program3 and GL_NV_fragment_program2
 */
#define MOJOSHADER_PROFILE_NV3 "nv3"

/*
 * Profile string for OpenGL ARB 1.0 shaders with Nvidia 4.0 extensions:
 *  GL_NV_gpu_program4
 */
#define MOJOSHADER_PROFILE_NV4 "nv4"

/*
 * Profile string for Metal: Apple's lowlevel API's high-level shader language.
 */
#define MOJOSHADER_PROFILE_METAL "metal"

/*
 * Profile string for SPIR-V binary output
 */
#define MOJOSHADER_PROFILE_SPIRV "spirv"

/*
 * Profile string for ARB_gl_spirv-friendly SPIR-V binary output
 */
#define MOJOSHADER_PROFILE_GLSPIRV "glspirv"

/*
 * Determine the highest supported Shader Model for a profile.
 */
DECLSPEC int MOJOSHADER_maxShaderModel(const char *profile);


/*
 * Parse a compiled Direct3D shader's bytecode.
 *
 * This is your primary entry point into MojoShader. You need to pass it
 *  a compiled D3D shader and tell it which "profile" you want to use to
 *  convert it into useful data.
 *
 * The available profiles are the set of MOJOSHADER_PROFILE_* defines.
 *  Note that MojoShader may be built without support for all listed
 *  profiles (in which case using one here will return with an error).
 *
 * As parsing requires some memory to be allocated, you may provide a custom
 *  allocator to this function, which will be used to allocate/free memory.
 *  They function just like malloc() and free(). We do not use realloc().
 *  If you don't care, pass NULL in for the allocator functions. If your
 *  allocator needs instance-specific data, you may supply it with the
 *  (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
 *
 * This function returns a MOJOSHADER_parseData.
 *
 * This function will never return NULL, even if the system is completely
 *  out of memory upon entry (in which case, this function returns a static
 *  MOJOSHADER_parseData object, which is still safe to pass to
 *  MOJOSHADER_freeParseData()).
 *
 * You can tell the generated program to swizzle certain inputs. If you know
 *  that COLOR0 should be RGBA but you're passing in ARGB, you can specify
 *  a swizzle of { MOJOSHADER_USAGE_COLOR, 0, {1,2,3,0} } to (swiz). If the
 *  input register in the code would produce reg.ywzx, that swizzle would
 *  change it to reg.wzxy ... (swiz) can be NULL.
 *
 * You can force the shader to expect samplers of certain types. Generally
 *  you don't need this, as Shader Model 2 and later always specify what they
 *  expect samplers to be (2D, cubemap, etc). Shader Model 1, however, just
 *  uses whatever is bound to a given sampler at draw time, but this doesn't
 *  work in OpenGL, etc. In these cases, MojoShader will default to
 *  2D texture sampling (or cubemap sampling, in cases where it makes sense,
 *  like the TEXM3X3TEX opcode), which works 75% of the time, but if you
 *  really needed something else, you'll need to specify it here. This can
 *  also be used, at your own risk, to override DCL opcodes in shaders: if
 *  the shader explicit says 2D, but you want Cubemap, for example, you can
 *  use this to override. If you aren't sure about any of this stuff, you can
 *  (and should) almost certainly ignore it: (smap) can be NULL.
 *
 * (bufsize) is the size in bytes of (tokenbuf). If (bufsize) is zero,
 *  MojoShader will attempt to figure out the size of the buffer, but you
 *  risk a buffer overflow if you have corrupt data, etc. Supply the value
 *  if you can.
 *
 * You should pass a name for your shader's main function in here, via the
 *  (mainfn) param. Some profiles need this name to be unique. Passing a NULL
 *  here will pick a reasonable default, and most profiles will ignore it
 *  anyhow. As the name of the shader's main function, etc, so make it a
 *  simple name that would match C's identifier rules. Keep it simple!
 *
 * This function is thread safe, so long as (m) and (f) are too, and that
 *  (tokenbuf) remains intact for the duration of the call. This allows you
 *  to parse several shaders on separate CPU cores at the same time.
 */
DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_parse(const char *profile,
                                                      const char *mainfn,
                                                      const unsigned char *tokenbuf,
                                                      const unsigned int bufsize,
                                                      const MOJOSHADER_swizzle *swiz,
                                                      const unsigned int swizcount,
                                                      const MOJOSHADER_samplerMap *smap,
                                                      const unsigned int smapcount,
                                                      MOJOSHADER_malloc m,
                                                      MOJOSHADER_free f,
                                                      void *d);


/*
 * Call this to dispose of parsing results when you are done with them.
 *  This will call the MOJOSHADER_free function you provided to
 *  MOJOSHADER_parse multiple times, if you provided one.
 *  Passing a NULL here is a safe no-op.
 *
 * This function is thread safe, so long as any allocator you passed into
 *  MOJOSHADER_parse() is, too.
 */
DECLSPEC void MOJOSHADER_freeParseData(const MOJOSHADER_parseData *data);


/*
 * You almost certainly don't need this function, unless you absolutely know
 *  why you need it without hesitation. This is useful if you're doing
 *  extremely low-level shader work or building specialized tools.
 *
 * Parse a preshader structure. This expects a buffer of bytes that represents
 *  the preshader data starting with its magic number token and ending at
 *  the end of the comment tokens that contain this preshader. Note that it
 *  does _not_ start at the beginning of the comment tokens.
 *
 * On success, this will return a MOJOSHADER_preshader. This can be
 *  deallocated later by calling MOJOSHADER_freePreshader(). On failure,
 *  this will return NULL. Unlike other MojoShader APIs, this assumes you
 *  either have a complete and valid buffer of preshader tokens or you have
 *  incomplete/corrupted data, so there is no explicit error reporting. Please
 *  note that if the system runs out of memory, this function will also return
 *  NULL without distinction.
 *
 * This function is thread safe, so long as any allocator you passed into
 *  MOJOSHADER_parsePreshader() is, too.
 */
DECLSPEC const MOJOSHADER_preshader *MOJOSHADER_parsePreshader(const unsigned char *buf,
                                                               const unsigned int len,
                                                               MOJOSHADER_malloc m,
                                                               MOJOSHADER_free f,
                                                               void *d);


/*
 * You almost certainly don't need this function, unless you absolutely know
 *  why you need it without hesitation. This is useful if you're doing
 *  extremely low-level shader work or building specialized tools.
 *
 * Call this to dispose of preshader parsing results when you are done with
 *  them. This will call the MOJOSHADER_free function you provided to
 *  MOJOSHADER_parsePreshader() multiple times, if you provided one.
 *  Passing a NULL here is a safe no-op.
 *
 * You only need to call this function for results from a call to
 *  MOJOSHADER_parsePreshader(). Other MojoShader structures with a preshader
 *  field, such as MOJOSHADER_parseData(), should not use this function, as
 *  the preshader will be deallocated with everything else in
 *  MOJOSHADER_freeParseData(), etc.
 *
 * This function is thread safe, so long as any allocator you passed into
 *  MOJOSHADER_parsePreshader() is, too.
 */
DECLSPEC void MOJOSHADER_freePreshader(const MOJOSHADER_preshader *preshader);


/* Preprocessor interface... */

/*
 * Structure used to pass predefined macros. Maps to D3DXMACRO.
 *  You can have macro arguments: set identifier to "a(b, c)" or whatever.
 */
typedef struct MOJOSHADER_preprocessorDefine
{
    const char *identifier;
    const char *definition;
} MOJOSHADER_preprocessorDefine;

/*
 * Used with the MOJOSHADER_includeOpen callback. Maps to D3DXINCLUDE_TYPE.
 */
typedef enum
{
    MOJOSHADER_INCLUDETYPE_LOCAL,   /* local header: #include "blah.h" */
    MOJOSHADER_INCLUDETYPE_SYSTEM   /* system header: #include <blah.h> */
} MOJOSHADER_includeType;


/*
 * Structure used to return data from preprocessing of a shader...
 */
/* !!! FIXME: most of these ints should be unsigned. */
typedef struct MOJOSHADER_preprocessData
{
    /*
     * The number of elements pointed to by (errors).
     */
    int error_count;

    /*
     * (error_count) elements of data that specify errors that were generated
     *  by parsing this shader.
     * This can be NULL if there were no errors or if (error_count) is zero.
     */
    MOJOSHADER_error *errors;

    /*
     * Bytes of output from preprocessing. This is a UTF-8 string. We
     *  guarantee it to be NULL-terminated. Will be NULL on error.
     */
    const char *output;

    /*
     * Byte count for output, not counting any null terminator.
     *  Will be 0 on error.
     */
    int output_len;

    /*
     * This is the malloc implementation you passed to MOJOSHADER_parse().
     */
    MOJOSHADER_malloc malloc;

    /*
     * This is the free implementation you passed to MOJOSHADER_parse().
     */
    MOJOSHADER_free free;

    /*
     * This is the pointer you passed as opaque data for your allocator.
     */
    void *malloc_data;
} MOJOSHADER_preprocessData;


/*
 * This callback allows an app to handle #include statements for the
 *  preprocessor. When the preprocessor sees an #include, it will call this
 *  function to obtain the contents of the requested file. This is optional;
 *  the preprocessor will open files directly if no callback is supplied, but
 *  this allows an app to retrieve data from something other than the
 *  traditional filesystem (for example, headers packed in a .zip file or
 *  headers generated on-the-fly).
 *
 * This function maps to ID3DXInclude::Open()
 *
 * (inctype) specifies the type of header we wish to include.
 * (fname) specifies the name of the file specified on the #include line.
 * (parent) is a string of the entire source file containing the include, in
 *  its original, not-yet-preprocessed state. Note that this is just the
 *  contents of the specific file, not all source code that the preprocessor
 *  has seen through other includes, etc.
 * (outdata) will be set by the callback to a pointer to the included file's
 *  contents. The callback is responsible for allocating this however they
 *  see fit (we provide allocator functions, but you may ignore them). This
 *  pointer must remain valid until the includeClose callback runs. This
 *  string does not need to be NULL-terminated.
 * (outbytes) will be set by the callback to the number of bytes pointed to
 *  by (outdata).
 * (m),(f), and (d) are the allocator details that the application passed to
 *  MojoShader. If these were NULL, MojoShader may have replaced them with its
 *  own internal allocators.
 *
 * The callback returns zero on error, non-zero on success.
 *
 * If you supply an includeOpen callback, you must supply includeClose, too.
 */
typedef int (MOJOSHADERCALL *MOJOSHADER_includeOpen)(MOJOSHADER_includeType inctype,
                            const char *fname, const char *parent,
                            const char **outdata, unsigned int *outbytes,
                            MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);

/*
 * This callback allows an app to clean up the results of a previous
 *  includeOpen callback.
 *
 * This function maps to ID3DXInclude::Close()
 *
 * (data) is the data that was returned from a previous call to includeOpen.
 *  It is now safe to deallocate this data.
 * (m),(f), and (d) are the same allocator details that were passed to your
 *  includeOpen callback.
 *
 * If you supply an includeClose callback, you must supply includeOpen, too.
 */
typedef void (MOJOSHADERCALL *MOJOSHADER_includeClose)(const char *data,
                            MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);


/*
 * This function is optional. Even if you are dealing with shader source
 *  code, you don't need to explicitly use the preprocessor, as the compiler
 *  and assembler will use it behind the scenes. In fact, you probably never
 *  need this function unless you are debugging a custom tool (or debugging
 *  MojoShader itself).
 *
 * Preprocessing roughly follows the syntax of an ANSI C preprocessor, as
 *  Microsoft's Direct3D assembler and HLSL compiler use this syntax. Please
 *  note that we try to match the output you'd get from Direct3D's
 *  preprocessor, which has some quirks if you're expecting output that matches
 *  a generic C preprocessor.
 *
 * This function maps to D3DXPreprocessShader().
 *
 * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
 *  actually access this file, as we obtain our data from (source). This
 *  string is copied when we need to report errors while processing (source),
 *  as opposed to errors in a file referenced via the #include directive in
 *  (source). If this is NULL, then errors will report the filename as NULL,
 *  too.
 *
 * (source) is an string of UTF-8 text to preprocess. It does not need to be
 *  NULL-terminated.
 *
 * (sourcelen) is the length of the string pointed to by (source), in bytes.
 *
 * (defines) points to (define_count) preprocessor definitions, and can be
 *  NULL. These are treated by the preprocessor as if the source code started
 *  with one #define for each entry you pass in here.
 *
 * (include_open) and (include_close) let the app control the preprocessor's
 *  behaviour for #include statements. Both are optional and can be NULL, but
 *  both must be specified if either is specified.
 *
 * This will return a MOJOSHADER_preprocessorData. You should pass this
 *  return value to MOJOSHADER_freePreprocessData() when you are done with
 *  it.
 *
 * This function will never return NULL, even if the system is completely
 *  out of memory upon entry (in which case, this function returns a static
 *  MOJOSHADER_preprocessData object, which is still safe to pass to
 *  MOJOSHADER_freePreprocessData()).
 *
 * As preprocessing requires some memory to be allocated, you may provide a
 *  custom allocator to this function, which will be used to allocate/free
 *  memory. They function just like malloc() and free(). We do not use
 *  realloc(). If you don't care, pass NULL in for the allocator functions.
 *  If your allocator needs instance-specific data, you may supply it with the
 *  (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
 *
 * This function is thread safe, so long as the various callback functions
 *  are, too, and that the parameters remains intact for the duration of the
 *  call. This allows you to preprocess several shaders on separate CPU cores
 *  at the same time.
 */
DECLSPEC const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *filename,
                             const char *source, unsigned int sourcelen,
                             const MOJOSHADER_preprocessorDefine *defines,
                             unsigned int define_count,
                             MOJOSHADER_includeOpen include_open,
                             MOJOSHADER_includeClose include_close,
                             MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);


/*
 * Call this to dispose of preprocessing results when you are done with them.
 *  This will call the MOJOSHADER_free function you provided to
 *  MOJOSHADER_preprocess() multiple times, if you provided one.
 *  Passing a NULL here is a safe no-op.
 *
 * This function is thread safe, so long as any allocator you passed into
 *  MOJOSHADER_preprocess() is, too.
 */
DECLSPEC void MOJOSHADER_freePreprocessData(const MOJOSHADER_preprocessData *data);


/* Assembler interface... */

/*
 * This function is optional. Use this to convert Direct3D shader assembly
 *  language into bytecode, which can be handled by MOJOSHADER_parse().
 *
 * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
 *  actually access this file, as we obtain our data from (source). This
 *  string is copied when we need to report errors while processing (source),
 *  as opposed to errors in a file referenced via the #include directive in
 *  (source). If this is NULL, then errors will report the filename as NULL,
 *  too.
 *
 * (source) is an UTF-8 string of valid Direct3D shader assembly source code.
 *  It does not need to be NULL-terminated.
 *
 * (sourcelen) is the length of the string pointed to by (source), in bytes.
 *
 * (comments) points to (comment_count) NULL-terminated UTF-8 strings, and
 *  can be NULL. These strings are inserted as comments in the bytecode.
 *
 * (symbols) points to (symbol_count) symbol structs, and can be NULL. These
 *  become a CTAB field in the bytecode. This is optional, but
 *  MOJOSHADER_parse() needs CTAB data for all arrays used in a program, or
 *  relative addressing will not be permitted, so you'll want to at least
 *  provide symbol information for those. The symbol data is 100% trusted
 *  at this time; it will not be checked to see if it matches what was
 *  assembled in any way whatsoever.
 *
 * (defines) points to (define_count) preprocessor definitions, and can be
 *  NULL. These are treated by the preprocessor as if the source code started
 *  with one #define for each entry you pass in here.
 *
 * (include_open) and (include_close) let the app control the preprocessor's
 *  behaviour for #include statements. Both are optional and can be NULL, but
 *  both must be specified if either is specified.
 *
 * This will return a MOJOSHADER_parseData, like MOJOSHADER_parse() would,
 *  except the profile will be MOJOSHADER_PROFILE_BYTECODE and the output
 *  will be the assembled bytecode instead of some other language. This output
 *  can be pushed back through MOJOSHADER_parseData() with a different profile.
 *
 * This function will never return NULL, even if the system is completely
 *  out of memory upon entry (in which case, this function returns a static
 *  MOJOSHADER_parseData object, which is still safe to pass to
 *  MOJOSHADER_freeParseData()).
 *
 * As assembling requires some memory to be allocated, you may provide a
 *  custom allocator to this function, which will be used to allocate/free
 *  memory. They function just like malloc() and free(). We do not use
 *  realloc(). If you don't care, pass NULL in for the allocator functions.
 *  If your allocator needs instance-specific data, you may supply it with the
 *  (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
 *
 * This function is thread safe, so long as the various callback functions
 *  are, too, and that the parameters remains intact for the duration of the
 *  call. This allows you to assemble several shaders on separate CPU cores
 *  at the same time.
 */
DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_assemble(const char *filename,
                             const char *source, unsigned int sourcelen,
                             const char **comments, unsigned int comment_count,
                             const MOJOSHADER_symbol *symbols,
                             unsigned int symbol_count,
                             const MOJOSHADER_preprocessorDefine *defines,
                             unsigned int define_count,
                             MOJOSHADER_includeOpen include_open,
                             MOJOSHADER_includeClose include_close,
                             MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);


/* High level shading language support... */

/*
 * Source profile strings for HLSL: Direct3D High Level Shading Language.
 */
#define MOJOSHADER_SRC_PROFILE_HLSL_VS_1_1 "hlsl_vs_1_1"
#define MOJOSHADER_SRC_PROFILE_HLSL_VS_2_0 "hlsl_vs_2_0"
#define MOJOSHADER_SRC_PROFILE_HLSL_VS_3_0 "hlsl_vs_3_0"
#define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_1 "hlsl_ps_1_1"
#define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_2 "hlsl_ps_1_2"
#define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_3 "hlsl_ps_1_3"
#define MOJOSHADER_SRC_PROFILE_HLSL_PS_1_4 "hlsl_ps_1_4"
#define MOJOSHADER_SRC_PROFILE_HLSL_PS_2_0 "hlsl_ps_2_0"
#define MOJOSHADER_SRC_PROFILE_HLSL_PS_3_0 "hlsl_ps_3_0"


/* Abstract Syntax Tree interface... */

/*
 * ATTENTION: This adds a lot of stuff to the API, but almost everyone can
 *  ignore this section. Seriously, go ahead and skip over anything that has
 *  "AST" in it, unless you know why you'd want to use it.
 *
 * ALSO: This API is still evolving! We make no promises at this time to keep
 *  source or binary compatibility for the AST pieces.
 *
 * Important notes:
 *  - ASTs are the result of parsing the source code: a program that fails to
 *    compile will often parse successfully. Undeclared variables,
 *    type incompatibilities, etc, aren't detected at this point.
 *  - Vector swizzles (the ".xyzw" part of "MyVec4.xyzw") will look like
 *    structure dereferences. We don't realize these are actually swizzles
 *    until semantic analysis.
 *  - MOJOSHADER_astDataType info is not reliable when returned from
 *    MOJOSHADER_parseAst()! Most of the datatype info will be missing or have
 *    inaccurate data types. We sort these out during semantic analysis, which
 *    happens after the AST parsing is complete. A few are filled in, or can
 *    be deduced fairly trivially by processing several pieces into one.
 *    It's enough that you can reproduce the original source code, more or
 *    less, from the AST.
 */

/* High-level datatypes for AST nodes. */
typedef enum MOJOSHADER_astDataTypeType
{
    MOJOSHADER_AST_DATATYPE_NONE,
    MOJOSHADER_AST_DATATYPE_BOOL,
    MOJOSHADER_AST_DATATYPE_INT,
    MOJOSHADER_AST_DATATYPE_UINT,
    MOJOSHADER_AST_DATATYPE_FLOAT,
    MOJOSHADER_AST_DATATYPE_FLOAT_SNORM,
    MOJOSHADER_AST_DATATYPE_FLOAT_UNORM,
    MOJOSHADER_AST_DATATYPE_HALF,
    MOJOSHADER_AST_DATATYPE_DOUBLE,
    MOJOSHADER_AST_DATATYPE_STRING,
    MOJOSHADER_AST_DATATYPE_SAMPLER_1D,
    MOJOSHADER_AST_DATATYPE_SAMPLER_2D,
    MOJOSHADER_AST_DATATYPE_SAMPLER_3D,
    MOJOSHADER_AST_DATATYPE_SAMPLER_CUBE,
    MOJOSHADER_AST_DATATYPE_SAMPLER_STATE,
    MOJOSHADER_AST_DATATYPE_SAMPLER_COMPARISON_STATE,
    MOJOSHADER_AST_DATATYPE_STRUCT,
    MOJOSHADER_AST_DATATYPE_ARRAY,
    MOJOSHADER_AST_DATATYPE_VECTOR,
    MOJOSHADER_AST_DATATYPE_MATRIX,
    MOJOSHADER_AST_DATATYPE_BUFFER,
    MOJOSHADER_AST_DATATYPE_FUNCTION,
    MOJOSHADER_AST_DATATYPE_USER
} MOJOSHADER_astDataTypeType;
#define MOJOSHADER_AST_DATATYPE_CONST (1 << 31)

typedef union MOJOSHADER_astDataType MOJOSHADER_astDataType;

/* This is just part of DataTypeStruct, never appears outside of it. */
typedef struct MOJOSHADER_astDataTypeStructMember
{
    const MOJOSHADER_astDataType *datatype;
    const char *identifier;
} MOJOSHADER_astDataTypeStructMember;

typedef struct MOJOSHADER_astDataTypeStruct
{
    MOJOSHADER_astDataTypeType type;
    const MOJOSHADER_astDataTypeStructMember *members;
    int member_count;
} MOJOSHADER_astDataTypeStruct;

typedef struct MOJOSHADER_astDataTypeArray
{
    MOJOSHADER_astDataTypeType type;
    const MOJOSHADER_astDataType *base;
    int elements;
} MOJOSHADER_astDataTypeArray;

typedef MOJOSHADER_astDataTypeArray MOJOSHADER_astDataTypeVector;

typedef struct MOJOSHADER_astDataTypeMatrix
{
    MOJOSHADER_astDataTypeType type;
    const MOJOSHADER_astDataType *base;
    int rows;
    int columns;
} MOJOSHADER_astDataTypeMatrix;

typedef struct MOJOSHADER_astDataTypeBuffer
{
    MOJOSHADER_astDataTypeType type;
    const MOJOSHADER_astDataType *base;
} MOJOSHADER_astDataTypeBuffer;

typedef struct MOJOSHADER_astDataTypeFunction
{
    MOJOSHADER_astDataTypeType type;
    const MOJOSHADER_astDataType *retval;
    const MOJOSHADER_astDataType **params;
    int num_params;
    int intrinsic;  /* non-zero for built-in functions */
} MOJOSHADER_astDataTypeFunction;

typedef struct MOJOSHADER_astDataTypeUser
{
    MOJOSHADER_astDataTypeType type;
    const MOJOSHADER_astDataType *details;
    const char *name;
} MOJOSHADER_astDataTypeUser;

union MOJOSHADER_astDataType
{
    MOJOSHADER_astDataTypeType type;
    MOJOSHADER_astDataTypeArray array;
    MOJOSHADER_astDataTypeStruct structure;
    MOJOSHADER_astDataTypeVector vector;
    MOJOSHADER_astDataTypeMatrix matrix;
    MOJOSHADER_astDataTypeBuffer buffer;
    MOJOSHADER_astDataTypeUser user;
    MOJOSHADER_astDataTypeFunction function;
};

/* Structures that make up the parse tree... */

typedef enum MOJOSHADER_astNodeType
{
    MOJOSHADER_AST_OP_START_RANGE,         /* expression operators. */

    MOJOSHADER_AST_OP_START_RANGE_UNARY,   /* unary operators. */
    MOJOSHADER_AST_OP_PREINCREMENT,
    MOJOSHADER_AST_OP_PREDECREMENT,
    MOJOSHADER_AST_OP_NEGATE,
    MOJOSHADER_AST_OP_COMPLEMENT,
    MOJOSHADER_AST_OP_NOT,
    MOJOSHADER_AST_OP_POSTINCREMENT,
    MOJOSHADER_AST_OP_POSTDECREMENT,
    MOJOSHADER_AST_OP_CAST,
    MOJOSHADER_AST_OP_END_RANGE_UNARY,

    MOJOSHADER_AST_OP_START_RANGE_BINARY,  /* binary operators. */
    MOJOSHADER_AST_OP_COMMA,
    MOJOSHADER_AST_OP_MULTIPLY,
    MOJOSHADER_AST_OP_DIVIDE,
    MOJOSHADER_AST_OP_MODULO,
    MOJOSHADER_AST_OP_ADD,
    MOJOSHADER_AST_OP_SUBTRACT,
    MOJOSHADER_AST_OP_LSHIFT,
    MOJOSHADER_AST_OP_RSHIFT,
    MOJOSHADER_AST_OP_LESSTHAN,
    MOJOSHADER_AST_OP_GREATERTHAN,
    MOJOSHADER_AST_OP_LESSTHANOREQUAL,
    MOJOSHADER_AST_OP_GREATERTHANOREQUAL,
    MOJOSHADER_AST_OP_EQUAL,
    MOJOSHADER_AST_OP_NOTEQUAL,
    MOJOSHADER_AST_OP_BINARYAND,
    MOJOSHADER_AST_OP_BINARYXOR,
    MOJOSHADER_AST_OP_BINARYOR,
    MOJOSHADER_AST_OP_LOGICALAND,
    MOJOSHADER_AST_OP_LOGICALOR,
    MOJOSHADER_AST_OP_ASSIGN,
    MOJOSHADER_AST_OP_MULASSIGN,
    MOJOSHADER_AST_OP_DIVASSIGN,
    MOJOSHADER_AST_OP_MODASSIGN,
    MOJOSHADER_AST_OP_ADDASSIGN,
    MOJOSHADER_AST_OP_SUBASSIGN,
    MOJOSHADER_AST_OP_LSHIFTASSIGN,
    MOJOSHADER_AST_OP_RSHIFTASSIGN,
    MOJOSHADER_AST_OP_ANDASSIGN,
    MOJOSHADER_AST_OP_XORASSIGN,
    MOJOSHADER_AST_OP_ORASSIGN,
    MOJOSHADER_AST_OP_DEREF_ARRAY,
    MOJOSHADER_AST_OP_END_RANGE_BINARY,

    MOJOSHADER_AST_OP_START_RANGE_TERNARY,  /* ternary operators. */
    MOJOSHADER_AST_OP_CONDITIONAL,
    MOJOSHADER_AST_OP_END_RANGE_TERNARY,

    MOJOSHADER_AST_OP_START_RANGE_DATA,     /* expression operands. */
    MOJOSHADER_AST_OP_IDENTIFIER,
    MOJOSHADER_AST_OP_INT_LITERAL,
    MOJOSHADER_AST_OP_FLOAT_LITERAL,
    MOJOSHADER_AST_OP_STRING_LITERAL,
    MOJOSHADER_AST_OP_BOOLEAN_LITERAL,
    MOJOSHADER_AST_OP_END_RANGE_DATA,

    MOJOSHADER_AST_OP_START_RANGE_MISC,     /* other expression things. */
    MOJOSHADER_AST_OP_DEREF_STRUCT,
    MOJOSHADER_AST_OP_CALLFUNC,
    MOJOSHADER_AST_OP_CONSTRUCTOR,
    MOJOSHADER_AST_OP_END_RANGE_MISC,
    MOJOSHADER_AST_OP_END_RANGE,

    MOJOSHADER_AST_COMPUNIT_START_RANGE,    /* things in global scope. */
    MOJOSHADER_AST_COMPUNIT_FUNCTION,
    MOJOSHADER_AST_COMPUNIT_TYPEDEF,
    MOJOSHADER_AST_COMPUNIT_STRUCT,
    MOJOSHADER_AST_COMPUNIT_VARIABLE,
    MOJOSHADER_AST_COMPUNIT_END_RANGE,

    MOJOSHADER_AST_STATEMENT_START_RANGE,   /* statements in function scope. */
    MOJOSHADER_AST_STATEMENT_EMPTY,
    MOJOSHADER_AST_STATEMENT_BREAK,
    MOJOSHADER_AST_STATEMENT_CONTINUE,
    MOJOSHADER_AST_STATEMENT_DISCARD,
    MOJOSHADER_AST_STATEMENT_BLOCK,
    MOJOSHADER_AST_STATEMENT_EXPRESSION,
    MOJOSHADER_AST_STATEMENT_IF,
    MOJOSHADER_AST_STATEMENT_SWITCH,
    MOJOSHADER_AST_STATEMENT_FOR,
    MOJOSHADER_AST_STATEMENT_DO,
    MOJOSHADER_AST_STATEMENT_WHILE,
    MOJOSHADER_AST_STATEMENT_RETURN,
    MOJOSHADER_AST_STATEMENT_TYPEDEF,
    MOJOSHADER_AST_STATEMENT_STRUCT,
    MOJOSHADER_AST_STATEMENT_VARDECL,
    MOJOSHADER_AST_STATEMENT_END_RANGE,

    MOJOSHADER_AST_MISC_START_RANGE,        /* misc. syntactic glue. */
    MOJOSHADER_AST_FUNCTION_PARAMS,
    MOJOSHADER_AST_FUNCTION_SIGNATURE,
    MOJOSHADER_AST_SCALAR_OR_ARRAY,
    MOJOSHADER_AST_TYPEDEF,
    MOJOSHADER_AST_PACK_OFFSET,
    MOJOSHADER_AST_VARIABLE_LOWLEVEL,
    MOJOSHADER_AST_ANNOTATION,
    MOJOSHADER_AST_VARIABLE_DECLARATION,
    MOJOSHADER_AST_STRUCT_DECLARATION,
    MOJOSHADER_AST_STRUCT_MEMBER,
    MOJOSHADER_AST_SWITCH_CASE,
    MOJOSHADER_AST_ARGUMENTS,
    MOJOSHADER_AST_MISC_END_RANGE,

    MOJOSHADER_AST_END_RANGE
} MOJOSHADER_astNodeType;

typedef struct MOJOSHADER_astNodeInfo
{
    MOJOSHADER_astNodeType type;
    const char *filename;
    unsigned int line;
} MOJOSHADER_astNodeInfo;

typedef enum MOJOSHADER_astVariableAttributes
{
    MOJOSHADER_AST_VARATTR_EXTERN = (1 << 0),
    MOJOSHADER_AST_VARATTR_NOINTERPOLATION = (1 << 1),
    MOJOSHADER_AST_VARATTR_SHARED = (1 << 2),
    MOJOSHADER_AST_VARATTR_STATIC = (1 << 3),
    MOJOSHADER_AST_VARATTR_UNIFORM = (1 << 4),
    MOJOSHADER_AST_VARATTR_VOLATILE = (1 << 5),
    MOJOSHADER_AST_VARATTR_CONST = (1 << 6),
    MOJOSHADER_AST_VARATTR_ROWMAJOR = (1 << 7),
    MOJOSHADER_AST_VARATTR_COLUMNMAJOR = (1 << 8)
} MOJOSHADER_astVariableAttributes;

typedef enum MOJOSHADER_astIfAttributes
{
    MOJOSHADER_AST_IFATTR_NONE,
    MOJOSHADER_AST_IFATTR_BRANCH,
    MOJOSHADER_AST_IFATTR_FLATTEN,
    MOJOSHADER_AST_IFATTR_IFALL,
    MOJOSHADER_AST_IFATTR_IFANY,
    MOJOSHADER_AST_IFATTR_PREDICATE,
    MOJOSHADER_AST_IFATTR_PREDICATEBLOCK
} MOJOSHADER_astIfAttributes;

typedef enum MOJOSHADER_astSwitchAttributes
{
    MOJOSHADER_AST_SWITCHATTR_NONE,
    MOJOSHADER_AST_SWITCHATTR_FLATTEN,
    MOJOSHADER_AST_SWITCHATTR_BRANCH,
    MOJOSHADER_AST_SWITCHATTR_FORCECASE,
    MOJOSHADER_AST_SWITCHATTR_CALL
} MOJOSHADER_astSwitchAttributes;

/* You can cast any AST node pointer to this. */
typedef struct MOJOSHADER_astGeneric
{
    MOJOSHADER_astNodeInfo ast;
} MOJOSHADER_astGeneric;

typedef struct MOJOSHADER_astExpression
{
    MOJOSHADER_astNodeInfo ast;
    const MOJOSHADER_astDataType *datatype;
} MOJOSHADER_astExpression;

typedef struct MOJOSHADER_astArguments
{
    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_ARGUMENTS */
    MOJOSHADER_astExpression *argument;
    struct MOJOSHADER_astArguments *next;
} MOJOSHADER_astArguments;

typedef struct MOJOSHADER_astExpressionUnary
{
    MOJOSHADER_astNodeInfo ast;
    const MOJOSHADER_astDataType *datatype;
    MOJOSHADER_astExpression *operand;
} MOJOSHADER_astExpressionUnary;

typedef struct MOJOSHADER_astExpressionBinary
{
    MOJOSHADER_astNodeInfo ast;
    const MOJOSHADER_astDataType *datatype;
    MOJOSHADER_astExpression *left;
    MOJOSHADER_astExpression *right;
} MOJOSHADER_astExpressionBinary;

typedef struct MOJOSHADER_astExpressionTernary
{
    MOJOSHADER_astNodeInfo ast;
    const MOJOSHADER_astDataType *datatype;
    MOJOSHADER_astExpression *left;
    MOJOSHADER_astExpression *center;
    MOJOSHADER_astExpression *right;
} MOJOSHADER_astExpressionTernary;

/* Identifier indexes aren't available until semantic analysis phase completes.
 *  It provides a unique id for this identifier's variable.
 *  It will be negative for global scope, positive for function scope
 *  (global values are globally unique, function values are only
 *  unique within the scope of the given function). There's a different
 *  set of indices if this identifier is a function (positive for
 *  user-defined functions, negative for intrinsics).
 *  May be zero for various reasons (unknown identifier, etc).
 */
typedef struct MOJOSHADER_astExpressionIdentifier
{
    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_IDENTIFIER */
    const MOJOSHADER_astDataType *datatype;
    const char *identifier;
    int index;
} MOJOSHADER_astExpressionIdentifier;

typedef struct MOJOSHADER_astExpressionIntLiteral
{
    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_INT_LITERAL */
    const MOJOSHADER_astDataType *datatype;  /* always AST_DATATYPE_INT */
    int value;
} MOJOSHADER_astExpressionIntLiteral;

typedef struct MOJOSHADER_astExpressionFloatLiteral
{
    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_FLOAT_LITERAL */
    const MOJOSHADER_astDataType *datatype;  /* always AST_DATATYPE_FLOAT */
    double value;
} MOJOSHADER_astExpressionFloatLiteral;

typedef struct MOJOSHADER_astExpressionStringLiteral
{
    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_STRING_LITERAL */
    const MOJOSHADER_astDataType *datatype;  /* always AST_DATATYPE_STRING */
    const char *string;
} MOJOSHADER_astExpressionStringLiteral;

typedef struct MOJOSHADER_astExpressionBooleanLiteral
{
    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_BOOLEAN_LITERAL */
    const MOJOSHADER_astDataType *datatype;  /* always AST_DATATYPE_BOOL */
    int value;  /* Always 1 or 0. */
} MOJOSHADER_astExpressionBooleanLiteral;

typedef struct MOJOSHADER_astExpressionConstructor
{
    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_CONSTRUCTOR */
    const MOJOSHADER_astDataType *datatype;
    MOJOSHADER_astArguments *args;
} MOJOSHADER_astExpressionConstructor;

typedef struct MOJOSHADER_astExpressionDerefStruct
{
    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_DEREF_STRUCT */
    const MOJOSHADER_astDataType *datatype;
    /* !!! FIXME:
     *  "identifier" is misnamed; this might not be an identifier at all:
     *    x = FunctionThatReturnsAStruct().SomeMember;
     */
    MOJOSHADER_astExpression *identifier;
    const char *member;
    int isswizzle;  /* Always 1 or 0. Never set by parseAst()! */
    int member_index;  /* Never set by parseAst()! */
} MOJOSHADER_astExpressionDerefStruct;

typedef struct MOJOSHADER_astExpressionCallFunction
{
    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_CALLFUNC */
    const MOJOSHADER_astDataType *datatype;
    MOJOSHADER_astExpressionIdentifier *identifier;
    MOJOSHADER_astArguments *args;
} MOJOSHADER_astExpressionCallFunction;

typedef struct MOJOSHADER_astExpressionCast
{
    MOJOSHADER_astNodeInfo ast;  /* Always MOJOSHADER_AST_OP_CAST */
    const MOJOSHADER_astDataType *datatype;
    MOJOSHADER_astExpression *operand;
} MOJOSHADER_astExpressionCast;

typedef struct MOJOSHADER_astCompilationUnit
{
    MOJOSHADER_astNodeInfo ast;
    struct MOJOSHADER_astCompilationUnit *next;
} MOJOSHADER_astCompilationUnit;

typedef enum MOJOSHADER_astFunctionStorageClass
{
    MOJOSHADER_AST_FNSTORECLS_NONE,
    MOJOSHADER_AST_FNSTORECLS_INLINE
} MOJOSHADER_astFunctionStorageClass;

typedef enum MOJOSHADER_astInputModifier
{
    MOJOSHADER_AST_INPUTMOD_NONE,
    MOJOSHADER_AST_INPUTMOD_IN,
    MOJOSHADER_AST_INPUTMOD_OUT,
    MOJOSHADER_AST_INPUTMOD_INOUT,
    MOJOSHADER_AST_INPUTMOD_UNIFORM
} MOJOSHADER_astInputModifier;

typedef enum MOJOSHADER_astInterpolationModifier
{
    MOJOSHADER_AST_INTERPMOD_NONE,
    MOJOSHADER_AST_INTERPMOD_LINEAR,
    MOJOSHADER_AST_INTERPMOD_CENTROID,
    MOJOSHADER_AST_INTERPMOD_NOINTERPOLATION,
    MOJOSHADER_AST_INTERPMOD_NOPERSPECTIVE,
    MOJOSHADER_AST_INTERPMOD_SAMPLE
} MOJOSHADER_astInterpolationModifier;

typedef struct MOJOSHADER_astFunctionParameters
{
    MOJOSHADER_astNodeInfo ast;
    const MOJOSHADER_astDataType *datatype;
    MOJOSHADER_astInputModifier input_modifier;
    const char *identifier;
    const char *semantic;
    MOJOSHADER_astInterpolationModifier interpolation_modifier;
    MOJOSHADER_astExpression *initializer;
    struct MOJOSHADER_astFunctionParameters *next;
} MOJOSHADER_astFunctionParameters;

typedef struct MOJOSHADER_astFunctionSignature
{
    MOJOSHADER_astNodeInfo ast;
    const MOJOSHADER_astDataType *datatype;
    const char *identifier;
    MOJOSHADER_astFunctionParameters *params;
    MOJOSHADER_astFunctionStorageClass storage_class;
    const char *semantic;
} MOJOSHADER_astFunctionSignature;

typedef struct MOJOSHADER_astScalarOrArray
{
    MOJOSHADER_astNodeInfo ast;
    const char *identifier;
    int isarray;  /* boolean: 1 or 0 */
    MOJOSHADER_astExpression *dimension;
} MOJOSHADER_astScalarOrArray;

typedef struct MOJOSHADER_astAnnotations
{
    MOJOSHADER_astNodeInfo ast;
    const MOJOSHADER_astDataType *datatype;
    MOJOSHADER_astExpression *initializer;
    struct MOJOSHADER_astAnnotations *next;
} MOJOSHADER_astAnnotations;

typedef struct MOJOSHADER_astPackOffset
{
    MOJOSHADER_astNodeInfo ast;
    const char *ident1;   /* !!! FIXME: rename this. */
    const char *ident2;
} MOJOSHADER_astPackOffset;

typedef struct MOJOSHADER_astVariableLowLevel
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astPackOffset *packoffset;
    const char *register_name;
} MOJOSHADER_astVariableLowLevel;

typedef struct MOJOSHADER_astStructMembers
{
    MOJOSHADER_astNodeInfo ast;
    const MOJOSHADER_astDataType *datatype;
    const char *semantic;
    MOJOSHADER_astScalarOrArray *details;
    MOJOSHADER_astInterpolationModifier interpolation_mod;
    struct MOJOSHADER_astStructMembers *next;
} MOJOSHADER_astStructMembers;

typedef struct MOJOSHADER_astStructDeclaration
{
    MOJOSHADER_astNodeInfo ast;
    const MOJOSHADER_astDataType *datatype;
    const char *name;
    MOJOSHADER_astStructMembers *members;
} MOJOSHADER_astStructDeclaration;

typedef struct MOJOSHADER_astVariableDeclaration
{
    MOJOSHADER_astNodeInfo ast;
    int attributes;
    const MOJOSHADER_astDataType *datatype;
    MOJOSHADER_astStructDeclaration *anonymous_datatype;
    MOJOSHADER_astScalarOrArray *details;
    const char *semantic;
    MOJOSHADER_astAnnotations *annotations;
    MOJOSHADER_astExpression *initializer;
    MOJOSHADER_astVariableLowLevel *lowlevel;
    struct MOJOSHADER_astVariableDeclaration *next;
} MOJOSHADER_astVariableDeclaration;

typedef struct MOJOSHADER_astStatement
{
    MOJOSHADER_astNodeInfo ast;
    struct MOJOSHADER_astStatement *next;
} MOJOSHADER_astStatement;

typedef MOJOSHADER_astStatement MOJOSHADER_astEmptyStatement;
typedef MOJOSHADER_astStatement MOJOSHADER_astBreakStatement;
typedef MOJOSHADER_astStatement MOJOSHADER_astContinueStatement;
typedef MOJOSHADER_astStatement MOJOSHADER_astDiscardStatement;

/* something enclosed in "{}" braces. */
typedef struct MOJOSHADER_astBlockStatement
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astStatement *next;
    MOJOSHADER_astStatement *statements;  /* list of child statements. */
} MOJOSHADER_astBlockStatement;

typedef struct MOJOSHADER_astReturnStatement
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astStatement *next;
    MOJOSHADER_astExpression *expr;
} MOJOSHADER_astReturnStatement;

typedef struct MOJOSHADER_astExpressionStatement
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astStatement *next;
    MOJOSHADER_astExpression *expr;
} MOJOSHADER_astExpressionStatement;

typedef struct MOJOSHADER_astIfStatement
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astStatement *next;
    int attributes;
    MOJOSHADER_astExpression *expr;
    MOJOSHADER_astStatement *statement;
    MOJOSHADER_astStatement *else_statement;
} MOJOSHADER_astIfStatement;

typedef struct MOJOSHADER_astSwitchCases
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astExpression *expr;
    MOJOSHADER_astStatement *statement;
    struct MOJOSHADER_astSwitchCases *next;
} MOJOSHADER_astSwitchCases;

typedef struct MOJOSHADER_astSwitchStatement
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astStatement *next;
    int attributes;
    MOJOSHADER_astExpression *expr;
    MOJOSHADER_astSwitchCases *cases;
} MOJOSHADER_astSwitchStatement;

typedef struct MOJOSHADER_astWhileStatement
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astStatement *next;
    int unroll;  /* # times to unroll, 0 to loop, < 0 == compiler's choice. */
    MOJOSHADER_astExpression *expr;
    MOJOSHADER_astStatement *statement;
} MOJOSHADER_astWhileStatement;

typedef MOJOSHADER_astWhileStatement MOJOSHADER_astDoStatement;

typedef struct MOJOSHADER_astForStatement
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astStatement *next;
    int unroll;  /* # times to unroll, 0 to loop, < 0 == compiler's choice. */
    MOJOSHADER_astVariableDeclaration *var_decl;  /* either this ... */
    MOJOSHADER_astExpression *initializer;        /*  ... or this will used. */
    MOJOSHADER_astExpression *looptest;
    MOJOSHADER_astExpression *counter;
    MOJOSHADER_astStatement *statement;
} MOJOSHADER_astForStatement;

typedef struct MOJOSHADER_astTypedef
{
    MOJOSHADER_astNodeInfo ast;
    const MOJOSHADER_astDataType *datatype;
    int isconst;  /* boolean: 1 or 0 */
    MOJOSHADER_astScalarOrArray *details;
} MOJOSHADER_astTypedef;

typedef struct MOJOSHADER_astTypedefStatement
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astStatement *next;
    MOJOSHADER_astTypedef *type_info;
} MOJOSHADER_astTypedefStatement;

typedef struct MOJOSHADER_astVarDeclStatement
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astStatement *next;
    MOJOSHADER_astVariableDeclaration *declaration;
} MOJOSHADER_astVarDeclStatement;

typedef struct MOJOSHADER_astStructStatement
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astStatement *next;
    MOJOSHADER_astStructDeclaration *struct_info;
} MOJOSHADER_astStructStatement;

typedef struct MOJOSHADER_astCompilationUnitFunction
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astCompilationUnit *next;
    MOJOSHADER_astFunctionSignature *declaration;
    MOJOSHADER_astStatement *definition;
    int index;  /* unique id. Will be 0 until semantic analysis runs. */
} MOJOSHADER_astCompilationUnitFunction;

typedef struct MOJOSHADER_astCompilationUnitTypedef
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astCompilationUnit *next;
    MOJOSHADER_astTypedef *type_info;
} MOJOSHADER_astCompilationUnitTypedef;

typedef struct MOJOSHADER_astCompilationUnitStruct
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astCompilationUnit *next;
    MOJOSHADER_astStructDeclaration *struct_info;
} MOJOSHADER_astCompilationUnitStruct;

typedef struct MOJOSHADER_astCompilationUnitVariable
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astCompilationUnit *next;
    MOJOSHADER_astVariableDeclaration *declaration;
} MOJOSHADER_astCompilationUnitVariable;


/* this is way cleaner than all the nasty typecasting. */
typedef union MOJOSHADER_astNode
{
    MOJOSHADER_astNodeInfo ast;
    MOJOSHADER_astGeneric generic;
    MOJOSHADER_astExpression expression;
    MOJOSHADER_astArguments arguments;
    MOJOSHADER_astExpressionUnary unary;
    MOJOSHADER_astExpressionBinary binary;
    MOJOSHADER_astExpressionTernary ternary;
    MOJOSHADER_astExpressionIdentifier identifier;
    MOJOSHADER_astExpressionIntLiteral intliteral;
    MOJOSHADER_astExpressionFloatLiteral floatliteral;
    MOJOSHADER_astExpressionStringLiteral stringliteral;
    MOJOSHADER_astExpressionBooleanLiteral boolliteral;
    MOJOSHADER_astExpressionConstructor constructor;
    MOJOSHADER_astExpressionDerefStruct derefstruct;
    MOJOSHADER_astExpressionCallFunction callfunc;
    MOJOSHADER_astExpressionCast cast;
    MOJOSHADER_astCompilationUnit compunit;
    MOJOSHADER_astFunctionParameters params;
    MOJOSHADER_astFunctionSignature funcsig;
    MOJOSHADER_astScalarOrArray soa;
    MOJOSHADER_astAnnotations annotations;
    MOJOSHADER_astPackOffset packoffset;
    MOJOSHADER_astVariableLowLevel varlowlevel;
    MOJOSHADER_astStructMembers structmembers;
    MOJOSHADER_astStructDeclaration structdecl;
    MOJOSHADER_astVariableDeclaration vardecl;
    MOJOSHADER_astStatement stmt;
    MOJOSHADER_astEmptyStatement emptystmt;
    MOJOSHADER_astBreakStatement breakstmt;
    MOJOSHADER_astContinueStatement contstmt;
    MOJOSHADER_astDiscardStatement discardstmt;
    MOJOSHADER_astBlockStatement blockstmt;
    MOJOSHADER_astReturnStatement returnstmt;
    MOJOSHADER_astExpressionStatement exprstmt;
    MOJOSHADER_astIfStatement ifstmt;
    MOJOSHADER_astSwitchCases cases;
    MOJOSHADER_astSwitchStatement switchstmt;
    MOJOSHADER_astWhileStatement whilestmt;
    MOJOSHADER_astDoStatement dostmt;
    MOJOSHADER_astForStatement forstmt;
    MOJOSHADER_astTypedef typdef;
    MOJOSHADER_astTypedefStatement typedefstmt;
    MOJOSHADER_astVarDeclStatement vardeclstmt;
    MOJOSHADER_astStructStatement structstmt;
    MOJOSHADER_astCompilationUnitFunction funcunit;
    MOJOSHADER_astCompilationUnitTypedef typedefunit;
    MOJOSHADER_astCompilationUnitStruct structunit;
    MOJOSHADER_astCompilationUnitVariable varunit;
} MOJOSHADER_astNode;


/*
 * Structure used to return data from parsing of a shader into an AST...
 */
/* !!! FIXME: most of these ints should be unsigned. */
typedef struct MOJOSHADER_astData
{
    /*
     * The number of elements pointed to by (errors).
     */
    int error_count;

    /*
     * (error_count) elements of data that specify errors that were generated
     *  by parsing this shader.
     * This can be NULL if there were no errors or if (error_count) is zero.
     *  Note that this will only produce errors for syntax problems. Most of
     *  the things we expect a compiler to produce errors for--incompatible
     *  types, unknown identifiers, etc--are not checked at all during
     *  initial generation of the syntax tree...bogus programs that would
     *  fail to compile will pass here without error, if they are syntactically
     *  correct!
     */
    MOJOSHADER_error *errors;

    /*
     * The name of the source profile used to parse the shader. Will be NULL
     *  on error.
     */
    const char *source_profile;

    /*
     * The actual syntax tree. You are responsible for walking it yourself.
     *  CompilationUnits are always the top of the tree (functions, typedefs,
     *  global variables, etc). Will be NULL on error.
     */
    const MOJOSHADER_astNode *ast;

    /*
     * This is the malloc implementation you passed to MOJOSHADER_parse().
     */
    MOJOSHADER_malloc malloc;

    /*
     * This is the free implementation you passed to MOJOSHADER_parse().
     */
    MOJOSHADER_free free;

    /*
     * This is the pointer you passed as opaque data for your allocator.
     */
    void *malloc_data;

    /*
     * This is internal data, and not for the application to touch.
     */
    void *opaque;
} MOJOSHADER_astData;


/*
 * You almost certainly don't need this function, unless you absolutely know
 *  why you need it without hesitation. This is almost certainly only good for
 *  building code analysis tools on top of.
 *
 * This is intended to parse HLSL source code, turning it into an abstract
 *  syntax tree.
 *
 * (srcprofile) specifies the source language of the shader. You can specify
 *  a shader model with this, too. See MOJOSHADER_SRC_PROFILE_* constants.
 *
 * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
 *  actually access this file, as we obtain our data from (source). This
 *  string is copied when we need to report errors while processing (source),
 *  as opposed to errors in a file referenced via the #include directive in
 *  (source). If this is NULL, then errors will report the filename as NULL,
 *  too.
 *
 * (source) is an UTF-8 string of valid high-level shader source code.
 *  It does not need to be NULL-terminated.
 *
 * (sourcelen) is the length of the string pointed to by (source), in bytes.
 *
 * (defines) points to (define_count) preprocessor definitions, and can be
 *  NULL. These are treated by the preprocessor as if the source code started
 *  with one #define for each entry you pass in here.
 *
 * (include_open) and (include_close) let the app control the preprocessor's
 *  behaviour for #include statements. Both are optional and can be NULL, but
 *  both must be specified if either is specified.
 *
 * This will return a MOJOSHADER_astData. The data supplied here gives the
 *  application a tree-like structure they can walk to see the layout of
 *  a given program. When you are done with this data, pass it to
 *  MOJOSHADER_freeCompileData() to deallocate resources.
 *
 * This function will never return NULL, even if the system is completely
 *  out of memory upon entry (in which case, this function returns a static
 *  MOJOSHADER_astData object, which is still safe to pass to
 *  MOJOSHADER_freeAstData()).
 *
 * As parsing requires some memory to be allocated, you may provide a
 *  custom allocator to this function, which will be used to allocate/free
 *  memory. They function just like malloc() and free(). We do not use
 *  realloc(). If you don't care, pass NULL in for the allocator functions.
 *  If your allocator needs instance-specific data, you may supply it with the
 *  (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
 *
 * This function is thread safe, so long as the various callback functions
 *  are, too, and that the parameters remains intact for the duration of the
 *  call. This allows you to parse several shaders on separate CPU cores
 *  at the same time.
 */
DECLSPEC const MOJOSHADER_astData *MOJOSHADER_parseAst(const char *srcprofile,
                                    const char *filename, const char *source,
                                    unsigned int sourcelen,
                                    const MOJOSHADER_preprocessorDefine *defs,
                                    unsigned int define_count,
                                    MOJOSHADER_includeOpen include_open,
                                    MOJOSHADER_includeClose include_close,
                                    MOJOSHADER_malloc m, MOJOSHADER_free f,
                                    void *d);


/* !!! FIXME: expose semantic analysis to the public API? */


/*
 * Call this to dispose of AST parsing results when you are done with them.
 *  This will call the MOJOSHADER_free function you provided to
 *  MOJOSHADER_parseAst() multiple times, if you provided one.
 *  Passing a NULL here is a safe no-op.
 *
 * This function is thread safe, so long as any allocator you passed into
 *  MOJOSHADER_parseAst() is, too.
 */
DECLSPEC void MOJOSHADER_freeAstData(const MOJOSHADER_astData *data);


/* Intermediate Representation interface... */
/* !!! FIXME: there is currently no way to access the IR via the public API. */
typedef enum MOJOSHADER_irNodeType
{
    MOJOSHADER_IR_START_RANGE_EXPR,
    MOJOSHADER_IR_CONSTANT,
    MOJOSHADER_IR_TEMP,
    MOJOSHADER_IR_BINOP,
    MOJOSHADER_IR_MEMORY,
    MOJOSHADER_IR_CALL,
    MOJOSHADER_IR_ESEQ,
    MOJOSHADER_IR_ARRAY,
    MOJOSHADER_IR_CONVERT,
    MOJOSHADER_IR_SWIZZLE,
    MOJOSHADER_IR_CONSTRUCT,
    MOJOSHADER_IR_END_RANGE_EXPR,

    MOJOSHADER_IR_START_RANGE_STMT,
    MOJOSHADER_IR_MOVE,
    MOJOSHADER_IR_EXPR_STMT,
    MOJOSHADER_IR_JUMP,
    MOJOSHADER_IR_CJUMP,
    MOJOSHADER_IR_SEQ,
    MOJOSHADER_IR_LABEL,
    MOJOSHADER_IR_DISCARD,
    MOJOSHADER_IR_END_RANGE_STMT,

    MOJOSHADER_IR_START_RANGE_MISC,
    MOJOSHADER_IR_EXPRLIST,
    MOJOSHADER_IR_END_RANGE_MISC,

    MOJOSHADER_IR_END_RANGE
} MOJOSHADER_irNodeType;

typedef struct MOJOSHADER_irNodeInfo
{
    MOJOSHADER_irNodeType type;
    const char *filename;
    unsigned int line;
} MOJOSHADER_irNodeInfo;

typedef struct MOJOSHADER_irExprList MOJOSHADER_irExprList;

/*
 * IR nodes are categorized into Expressions, Statements, and Everything Else.
 *  You can cast any of them to MOJOSHADER_irGeneric, but this split is
 *  useful for slightly better type-checking (you can't cleanly assign
 *  something that doesn't return a value to something that wants one, etc).
 * These broader categories are just unions of the simpler types, so the
 *  real definitions are below all the things they contain (but these
 *  predeclarations are because the simpler types refer to the broader
 *  categories).
 */
typedef union MOJOSHADER_irExpression MOJOSHADER_irExpression;  /* returns a value. */
typedef union MOJOSHADER_irStatement MOJOSHADER_irStatement;   /* no returned value. */
typedef union MOJOSHADER_irMisc MOJOSHADER_irMisc;        /* Everything Else. */
typedef union MOJOSHADER_irNode MOJOSHADER_irNode;        /* Generic uber-wrapper. */

/* You can cast any IR node pointer to this. */
typedef struct MOJOSHADER_irGeneric
{
    MOJOSHADER_irNodeInfo ir;
} MOJOSHADER_irGeneric;


/* These are used for MOJOSHADER_irBinOp */
typedef enum MOJOSHADER_irBinOpType
{
    MOJOSHADER_IR_BINOP_ADD,
    MOJOSHADER_IR_BINOP_SUBTRACT,
    MOJOSHADER_IR_BINOP_MULTIPLY,
    MOJOSHADER_IR_BINOP_DIVIDE,
    MOJOSHADER_IR_BINOP_MODULO,
    MOJOSHADER_IR_BINOP_AND,
    MOJOSHADER_IR_BINOP_OR,
    MOJOSHADER_IR_BINOP_XOR,
    MOJOSHADER_IR_BINOP_LSHIFT,
    MOJOSHADER_IR_BINOP_RSHIFT,
    MOJOSHADER_IR_BINOP_UNKNOWN
} MOJOSHADER_irBinOpType;

typedef enum MOJOSHADER_irConditionType
{
    MOJOSHADER_IR_COND_EQL,
    MOJOSHADER_IR_COND_NEQ,
    MOJOSHADER_IR_COND_LT,
    MOJOSHADER_IR_COND_GT,
    MOJOSHADER_IR_COND_LEQ,
    MOJOSHADER_IR_COND_GEQ,
    MOJOSHADER_IR_COND_UNKNOWN
} MOJOSHADER_irConditionType;


/* MOJOSHADER_irExpression types... */

typedef struct MOJOSHADER_irExprInfo
{
    MOJOSHADER_irNodeInfo ir;
    MOJOSHADER_astDataTypeType type;
    int elements;
} MOJOSHADER_irExprInfo;

typedef struct MOJOSHADER_irConstant    /* Constant value */
{
    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_CONSTANT */
    union
    {
        int ival[16];
        float fval[16];
    } value;
} MOJOSHADER_irConstant;

typedef struct MOJOSHADER_irTemp /* temp value (not necessarily a register). */
{
    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_TEMP */
    int index;
} MOJOSHADER_irTemp;

typedef struct MOJOSHADER_irBinOp  /* binary operator (+, -, etc) */
{
    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_BINOP */
    MOJOSHADER_irBinOpType op;
    MOJOSHADER_irExpression *left;
    MOJOSHADER_irExpression *right;
} MOJOSHADER_irBinOp;

typedef struct MOJOSHADER_irMemory
{
    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_MEMORY */
    int index;  /* not final addresses, just a unique identifier. */
} MOJOSHADER_irMemory;

typedef struct MOJOSHADER_irCall
{
    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_CALL */
    int index;
    MOJOSHADER_irExprList *args;
} MOJOSHADER_irCall;

typedef struct MOJOSHADER_irESeq  /* statement with result */
{
    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_ESEQ */
    MOJOSHADER_irStatement *stmt;  /* execute this for side-effects, then... */
    MOJOSHADER_irExpression *expr; /* ...use this for the result. */
} MOJOSHADER_irESeq;

typedef struct MOJOSHADER_irArray  /* Array dereference. */
{
    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_ARRAY */
    MOJOSHADER_irExpression *array;
    MOJOSHADER_irExpression *element;
} MOJOSHADER_irArray;

typedef struct MOJOSHADER_irConvert  /* casting between datatypes */
{
    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_CONVERT */
    MOJOSHADER_irExpression *expr;
} MOJOSHADER_irConvert;

typedef struct MOJOSHADER_irSwizzle  /* vector swizzle */
{
    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_SWIZZLE */
    MOJOSHADER_irExpression *expr;
    char channels[4];
} MOJOSHADER_irSwizzle;

typedef struct MOJOSHADER_irConstruct  /* vector construct from discrete items */
{
    MOJOSHADER_irExprInfo info;  /* Always MOJOSHADER_IR_CONTSTRUCT */
    MOJOSHADER_irExprList *args;
} MOJOSHADER_irConstruct;

/* Wrap the whole category in a union for type "safety." */
union MOJOSHADER_irExpression
{
    MOJOSHADER_irNodeInfo ir;
    MOJOSHADER_irExprInfo info;
    MOJOSHADER_irConstant constant;
    MOJOSHADER_irTemp temp;
    MOJOSHADER_irBinOp binop;
    MOJOSHADER_irMemory memory;
    MOJOSHADER_irCall call;
    MOJOSHADER_irESeq eseq;
    MOJOSHADER_irArray array;
    MOJOSHADER_irConvert convert;
    MOJOSHADER_irSwizzle swizzle;
    MOJOSHADER_irConstruct construct;
};

/* MOJOSHADER_irStatement types. */

typedef struct MOJOSHADER_irMove  /* load/store. */
{
    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_MOVE */
    MOJOSHADER_irExpression *dst; /* must result in a temp or mem! */
    MOJOSHADER_irExpression *src;
    int writemask;  /* for write-masking vector channels. */
} MOJOSHADER_irMove;

typedef struct MOJOSHADER_irExprStmt  /* evaluate expression, throw it away. */
{
    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_EXPR_STMT */
    MOJOSHADER_irExpression *expr;
} MOJOSHADER_irExprStmt;

typedef struct MOJOSHADER_irJump  /* unconditional jump */
{
    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_JUMP */
    int label;
    /* !!! FIXME: possible label list, for further optimization passes. */
} MOJOSHADER_irJump;

typedef struct MOJOSHADER_irCJump  /* conditional jump */
{
    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_CJUMP */
    MOJOSHADER_irConditionType cond;
    MOJOSHADER_irExpression *left;  /* if (left cond right) */
    MOJOSHADER_irExpression *right;
    int iftrue;  /* label id for true case. */
    int iffalse; /* label id for false case. */
} MOJOSHADER_irCJump;

typedef struct MOJOSHADER_irSeq  /* statement without side effects */
{
    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_SEQ */
    MOJOSHADER_irStatement *first;
    MOJOSHADER_irStatement *next;
} MOJOSHADER_irSeq;

typedef struct MOJOSHADER_irLabel  /* like a label in assembly language. */
{
    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_LABEL */
    int index;
} MOJOSHADER_irLabel;

typedef MOJOSHADER_irGeneric MOJOSHADER_irDiscard;  /* discard statement. */


/* Wrap the whole category in a union for type "safety." */
union MOJOSHADER_irStatement
{
    MOJOSHADER_irNodeInfo ir;
    MOJOSHADER_irGeneric generic;
    MOJOSHADER_irMove move;
    MOJOSHADER_irExprStmt expr;
    MOJOSHADER_irJump jump;
    MOJOSHADER_irCJump cjump;
    MOJOSHADER_irSeq seq;
    MOJOSHADER_irLabel label;
    MOJOSHADER_irDiscard discard;
};

/* MOJOSHADER_irMisc types. */

struct MOJOSHADER_irExprList
{
    MOJOSHADER_irNodeInfo ir;  /* Always MOJOSHADER_IR_EXPRLIST */
    MOJOSHADER_irExpression *expr;
    MOJOSHADER_irExprList *next;
};

/* Wrap the whole category in a union for type "safety." */
union MOJOSHADER_irMisc
{
    MOJOSHADER_irNodeInfo ir;
    MOJOSHADER_irGeneric generic;
    MOJOSHADER_irExprList exprlist;
};

/* This is a catchall for all your needs. :) */
union MOJOSHADER_irNode
{
    MOJOSHADER_irNodeInfo ir;
    MOJOSHADER_irGeneric generic;
    MOJOSHADER_irExpression expr;
    MOJOSHADER_irStatement stmt;
    MOJOSHADER_irMisc misc;
};


/* Compiler interface... */

/*
 * Structure used to return data from parsing of a shader...
 */
/* !!! FIXME: most of these ints should be unsigned. */
typedef struct MOJOSHADER_compileData
{
    /*
     * The number of elements pointed to by (errors).
     */
    int error_count;

    /*
     * (error_count) elements of data that specify errors that were generated
     *  by compiling this shader.
     * This can be NULL if there were no errors or if (error_count) is zero.
     */
    MOJOSHADER_error *errors;

    /*
     * The number of elements pointed to by (warnings).
     */
    int warning_count;

    /*
     * (warning_count) elements of data that specify errors that were
     *  generated by compiling this shader.
     * This can be NULL if there were no errors or if (warning_count) is zero.
     */
    MOJOSHADER_error *warnings;

    /*
     * The name of the source profile used to compile the shader. Will be NULL
     *  on error.
     */
    const char *source_profile;

    /*
     * Bytes of output from compiling. This will be a null-terminated ASCII
     *  string of D3D assembly source code.
     */
    const char *output;

    /*
     * Byte count for output, not counting any null terminator.
     *  Will be 0 on error.
     */
    int output_len;

    /*
     * The number of elements pointed to by (symbols).
     */
    int symbol_count;

    /*
     * (symbol_count) elements of data that specify high-level symbol data
     *  for the shader. This can be used by MOJOSHADER_assemble() to
     *  generate a CTAB section in bytecode, which is needed by
     *  MOJOSHADER_parseData() to handle some shaders. This can be NULL on
     *  error or if (symbol_count) is zero.
     */
    MOJOSHADER_symbol *symbols;

    /*
     * This is the malloc implementation you passed to MOJOSHADER_parse().
     */
    MOJOSHADER_malloc malloc;

    /*
     * This is the free implementation you passed to MOJOSHADER_parse().
     */
    MOJOSHADER_free free;

    /*
     * This is the pointer you passed as opaque data for your allocator.
     */
    void *malloc_data;
} MOJOSHADER_compileData;


/*
 * This function is optional. Use this to compile high-level shader programs.
 *
 * This is intended to turn HLSL source code into D3D assembly code, which
 *  can then be passed to MOJOSHADER_assemble() to convert it to D3D bytecode
 *  (which can then be used with MOJOSHADER_parseData() to support other
 *  shading targets).
 *
 * (srcprofile) specifies the source language of the shader. You can specify
 *  a shader model with this, too. See MOJOSHADER_SRC_PROFILE_* constants.
 *
 * (filename) is a NULL-terminated UTF-8 filename. It can be NULL. We do not
 *  actually access this file, as we obtain our data from (source). This
 *  string is copied when we need to report errors while processing (source),
 *  as opposed to errors in a file referenced via the #include directive in
 *  (source). If this is NULL, then errors will report the filename as NULL,
 *  too.
 *
 * (source) is an UTF-8 string of valid high-level shader source code.
 *  It does not need to be NULL-terminated.
 *
 * (sourcelen) is the length of the string pointed to by (source), in bytes.
 *
 * (defines) points to (define_count) preprocessor definitions, and can be
 *  NULL. These are treated by the preprocessor as if the source code started
 *  with one #define for each entry you pass in here.
 *
 * (include_open) and (include_close) let the app control the preprocessor's
 *  behaviour for #include statements. Both are optional and can be NULL, but
 *  both must be specified if either is specified.
 *
 * This will return a MOJOSHADER_compileData. The data supplied here is
 *  sufficient to supply to MOJOSHADER_assemble() for further processing.
 *  When you are done with this data, pass it to MOJOSHADER_freeCompileData()
 *  to deallocate resources.
 *
 * This function will never return NULL, even if the system is completely
 *  out of memory upon entry (in which case, this function returns a static
 *  MOJOSHADER_compileData object, which is still safe to pass to
 *  MOJOSHADER_freeCompileData()).
 *
 * As compiling requires some memory to be allocated, you may provide a
 *  custom allocator to this function, which will be used to allocate/free
 *  memory. They function just like malloc() and free(). We do not use
 *  realloc(). If you don't care, pass NULL in for the allocator functions.
 *  If your allocator needs instance-specific data, you may supply it with the
 *  (d) parameter. This pointer is passed as-is to your (m) and (f) functions.
 *
 * This function is thread safe, so long as the various callback functions
 *  are, too, and that the parameters remains intact for the duration of the
 *  call. This allows you to compile several shaders on separate CPU cores
 *  at the same time.
 */
DECLSPEC const MOJOSHADER_compileData *MOJOSHADER_compile(const char *srcprofile,
                                    const char *filename, const char *source,
                                    unsigned int sourcelen,
                                    const MOJOSHADER_preprocessorDefine *defs,
                                    unsigned int define_count,
                                    MOJOSHADER_includeOpen include_open,
                                    MOJOSHADER_includeClose include_close,
                                    MOJOSHADER_malloc m, MOJOSHADER_free f,
                                    void *d);


/*
 * Call this to dispose of compile results when you are done with them.
 *  This will call the MOJOSHADER_free function you provided to
 *  MOJOSHADER_compile() multiple times, if you provided one.
 *  Passing a NULL here is a safe no-op.
 *
 * This function is thread safe, so long as any allocator you passed into
 *  MOJOSHADER_compile() is, too.
 */
DECLSPEC void MOJOSHADER_freeCompileData(const MOJOSHADER_compileData *data);


/* OpenGL interface... */

/*
 * Signature for function lookup callbacks. MojoShader will call a function
 *  you provide to get OpenGL entry points (both standard functions and
 *  extensions). Through this, MojoShader never links directly to OpenGL,
 *  but relies on you to provide the implementation. This means you can
 *  swap in different drivers, or hook functions (log every GL call MojoShader
 *  makes, etc).
 *
 * (fnname) is the function name we want the address for ("glBegin" or
 *  whatever. (data) is a void pointer you provide, if this callback needs
 *  extra information. If you don't need it, you may specify NULL.
 *
 * Return the entry point on success, NULL if it couldn't be found.
 *  Note that this could ask for standard entry points like glEnable(), or
 *  extensions like glProgramLocalParameterI4ivNV(), so you might need
 *  to check two places to find the desired entry point, depending on your
 *  platform (Windows might need to look in OpenGL32.dll and use WGL, etc).
 */
typedef void *(MOJOSHADERCALL *MOJOSHADER_glGetProcAddress)(const char *fnname, void *data);


/*
 * "Contexts" map to OpenGL contexts...you need one per window, or whatever,
 *  and need to inform MojoShader when you make a new one current.
 *
 * "Shaders" refer to individual vertex or pixel programs, and are created
 *  by "compiling" Direct3D shader bytecode. A vertex and pixel shader are
 *  "linked" into a "Program" before you can use them to render.
 *
 * To the calling application, these are all opaque handles.
 */
typedef struct MOJOSHADER_glContext MOJOSHADER_glContext;
typedef struct MOJOSHADER_glShader MOJOSHADER_glShader;
typedef struct MOJOSHADER_glProgram MOJOSHADER_glProgram;


/*
 * Get a list of available profiles. This will fill in the array (profs)
 *  with up to (size) pointers of profiles that the current system can handle;
 *  that is, the profiles are built into MojoShader and the OpenGL extensions
 *  required for them exist at runtime. This function returns the number of
 *  available profiles, which may be more, less, or equal to (size).
 *
 * If there are more than (size) profiles, the (profs) buffer will not
 *  overflow. You can check the return value for the total number of
 *  available profiles, allocate more space, and try again if necessary.
 *  Calling this function with (size) == 0 is legal.
 *
 * You can only call this AFTER you have successfully built your GL context
 *  and made it current. This function will lookup the GL functions it needs
 *  through the callback you supply, via (lookup) and (lookup_d). The lookup
 *  function is neither stored nor used by MojoShader after this function
 *  returns, nor are the functions it might look up.
 *
 * As MojoShader requires some memory to be allocated, you may provide a
 *  custom allocator to this function, which will be used to allocate/free
 *  memory. They function just like malloc() and free(). We do not use
 *  realloc(). If you don't care, pass NULL in for the allocator functions.
 *  If your allocator needs instance-specific data, you may supply it with the
 *  (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
 *  functions.
 *
 * You should not free any strings returned from this function; they are
 *  pointers to internal, probably static, memory.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 */
DECLSPEC int MOJOSHADER_glAvailableProfiles(MOJOSHADER_glGetProcAddress lookup,
                                            void *lookup_d,
                                            const char **profs, const int size,
                                            MOJOSHADER_malloc m, MOJOSHADER_free f,
                                            void *malloc_d);


/*
 * Determine the best profile to use for the current system.
 *
 * You can only call this AFTER you have successfully built your GL context
 *  and made it current. This function will lookup the GL functions it needs
 *  through the callback you supply via (lookup) and (lookup_d). The lookup
 *  function is neither stored nor used by MojoShader after this function
 *  returns, nor are the functions it might look up.
 *
 * Returns the name of the "best" profile on success, NULL if none of the
 *  available profiles will work on this system. "Best" is a relative term,
 *  but it generally means the best trade off between feature set and
 *  performance. The selection algorithm may be arbitrary and complex.
 *
 * As MojoShader requires some memory to be allocated, you may provide a
 *  custom allocator to this function, which will be used to allocate/free
 *  memory. They function just like malloc() and free(). We do not use
 *  realloc(). If you don't care, pass NULL in for the allocator functions.
 *  If your allocator needs instance-specific data, you may supply it with the
 *  (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
 *  functions.
 *
 * The returned value is an internal static string, and should not be free()'d
 *  by the caller. If you get a NULL, calling MOJOSHADER_glGetError() might
 *  shed some light on why.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 */
DECLSPEC const char *MOJOSHADER_glBestProfile(MOJOSHADER_glGetProcAddress lookup,
                                   void *lookup_d,
                                   MOJOSHADER_malloc m, MOJOSHADER_free f,
                                   void *malloc_d);

/*
 * Prepare MojoShader to manage OpenGL shaders.
 *
 * You do not need to call this if all you want is MOJOSHADER_parse().
 *
 * You must call this once AFTER you have successfully built your GL context
 *  and made it current. This function will lookup the GL functions it needs
 *  through the callback you supply via (lookup) and (lookup_d), after which
 *  it may call them at any time up until you call
 *  MOJOSHADER_glDestroyContext(). The lookup function is neither stored nor
 *  used by MojoShader after this function returns.
 *
 * (profile) is an OpenGL-specific MojoShader profile, which decides how
 *  Direct3D bytecode shaders get turned into OpenGL programs, and how they
 *  are fed to the GL.
 *
 * (lookup) is a callback that is used to load GL entry points. This callback
 *  has to look up base GL functions and extension entry points. The pointer
 *  you supply in (lookup_d) is passed as-is to the callback.
 *
 * As MojoShader requires some memory to be allocated, you may provide a
 *  custom allocator to this function, which will be used to allocate/free
 *  memory. They function just like malloc() and free(). We do not use
 *  realloc(). If you don't care, pass NULL in for the allocator functions.
 *  If your allocator needs instance-specific data, you may supply it with the
 *  (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
 *  functions.
 *
 * Returns a new context on success, NULL on error. If you get a new context,
 *  you need to make it current before using it with
 *  MOJOSHADER_glMakeContextCurrent().
 *
 * This call is NOT thread safe! It must return success before you may call
 *  any other MOJOSHADER_gl* function. Also, as most OpenGL implementations
 *  are not thread safe, you should probably only call this from the same
 *  thread that created the GL context.
 */
DECLSPEC MOJOSHADER_glContext *MOJOSHADER_glCreateContext(const char *profile,
                                        MOJOSHADER_glGetProcAddress lookup,
                                        void *lookup_d,
                                        MOJOSHADER_malloc m, MOJOSHADER_free f,
                                        void *malloc_d);

/*
 * You must call this before using the context that you got from
 *  MOJOSHADER_glCreateContext(), and must use it when you switch to a new GL
 *  context.
 *
 * You can only have one MOJOSHADER_glContext per actual GL context, or
 *  undefined behaviour will result.
 *
 * It is legal to call this with a NULL pointer to make no context current,
 *  but you need a valid context to be current to use most of MojoShader.
 */
DECLSPEC void MOJOSHADER_glMakeContextCurrent(MOJOSHADER_glContext *ctx);

/*
 * Get any error state we might have picked up. MojoShader will NOT call
 *  glGetError() internally, but there are other errors we can pick up,
 *  such as failed shader compilation, etc.
 *
 * Returns a human-readable string. This string is for debugging purposes, and
 *  not guaranteed to be localized, coherent, or user-friendly in any way.
 *  It's for programmers!
 *
 * The latest error may remain between calls. New errors replace any existing
 *  error. Don't check this string for a sign that an error happened, check
 *  return codes instead and use this for explanation when debugging.
 *
 * Do not free the returned string: it's a pointer to a static internal
 *  buffer. Do not keep the pointer around, either, as it's likely to become
 *  invalid as soon as you call into MojoShader again.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call does NOT require a valid MOJOSHADER_glContext to have been made
 *  current. The error buffer is shared between contexts, so you can get
 *  error results from a failed MOJOSHADER_glCreateContext().
 */
DECLSPEC const char *MOJOSHADER_glGetError(void);

/*
 * Get the maximum uniforms a shader can support for the current GL context,
 *  MojoShader profile, and shader type. You can use this to make decisions
 *  about what shaders you want to use (for example, a less complicated
 *  shader may be swapped in for lower-end systems).
 *
 * Returns the number, or -1 on error.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC int MOJOSHADER_glMaxUniforms(MOJOSHADER_shaderType shader_type);

/*
 * Compile a buffer of Direct3D shader bytecode into an OpenGL shader.
 *  You still need to link the shader before you may render with it.
 *
 *   (tokenbuf) is a buffer of Direct3D shader bytecode.
 *   (bufsize) is the size, in bytes, of the bytecode buffer.
 *   (swiz), (swizcount), (smap), and (smapcount) are passed to
 *   MOJOSHADER_parse() unmolested.
 *
 * Returns NULL on error, or a shader handle on success.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Compiled shaders from this function may not be shared between contexts.
 */
DECLSPEC MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
                                                         const unsigned int bufsize,
                                                         const MOJOSHADER_swizzle *swiz,
                                                         const unsigned int swizcount,
                                                         const MOJOSHADER_samplerMap *smap,
                                                         const unsigned int smapcount);

/*
 * Increments a shader's internal refcount. To decrement the refcount, call
 *  MOJOSHADER_glDeleteShader().
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_glShaderAddRef(MOJOSHADER_glShader *shader);

/*
 * Get the MOJOSHADER_parseData structure that was produced from the
 *  call to MOJOSHADER_glCompileShader().
 *
 * This data is read-only, and you should NOT attempt to free it. This
 *  pointer remains valid until the shader is deleted.
 */
DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_glGetShaderParseData(
                                                MOJOSHADER_glShader *shader);
/*
 * Link a vertex and pixel shader into an OpenGL program.
 *  (vshader) or (pshader) can be NULL, to specify that the GL should use the
 *  fixed-function pipeline instead of the programmable pipeline for that
 *  portion of the work. You can reuse shaders in various combinations across
 *  multiple programs, by relinking different pairs.
 *
 * It is illegal to give a vertex shader for (pshader) or a pixel shader
 *  for (vshader).
 *
 * Once you have successfully linked a program, you may render with it.
 *
 * Returns NULL on error, or a program handle on success.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Linked programs from this function may not be shared between contexts.
 */
DECLSPEC MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
                                                        MOJOSHADER_glShader *pshader);

/*
 * This binds the program (using, for example, glUseProgramObjectARB()), and
 *  disables all the client-side arrays so we can reset them with new values
 *  if appropriate.
 *
 * Call with NULL to disable the programmable pipeline and all enabled
 *  client-side arrays.
 *
 * After binding a program, you should update any uniforms you care about
 *  with MOJOSHADER_glSetVertexShaderUniformF() (etc), set any vertex arrays
 *  you want to use with MOJOSHADER_glSetVertexAttribute(), and finally call
 *  MOJOSHADER_glProgramReady() to commit everything to the GL. Then you may
 *  begin drawing through standard GL entry points.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program);

/*
 * This binds individual shaders as if you had linked them with
 *  MOJOSHADER_glLinkProgram(), and used MOJOSHADER_glBindProgram() on the
 *  linked result.
 *
 * MojoShader will handle linking behind the scenes, and keep a cache of
 *  programs linked here. Programs are removed from this cache when one of the
 *  invidual shaders in it is deleted, otherwise they remain cached so future
 *  calls to this function don't need to relink a previously-used shader
 *  grouping.
 *
 * This function is for convenience, as the API is closer to how Direct3D
 *  works, and retrofitting linking into your app can be difficult;
 *  frequently, you just end up building your own cache, anyhow.
 *
 * Calling with all shaders set to NULL is equivalent to calling
 *  MOJOSHADER_glBindProgram(NULL).
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_glBindShaders(MOJOSHADER_glShader *vshader,
                                       MOJOSHADER_glShader *pshader);

/*
 * This queries for the shaders currently bound to the active context.
 *
 * This function is only for convenience, specifically for compatibility with
 * the effects API.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_glGetBoundShaders(MOJOSHADER_glShader **vshader,
                                           MOJOSHADER_glShader **pshader);

/*
 * Set a floating-point uniform value (what Direct3D calls a "constant").
 *
 * There is a single array of 4-float "registers" shared by all vertex shaders.
 *  This is the "c" register file in Direct3D (c0, c1, c2, etc...)
 *  MojoShader will take care of synchronizing this internal array with the
 *  appropriate variables in the GL shaders.
 *
 * (idx) is the index into the internal array: 0 is the first four floats,
 *  1 is the next four, etc.
 * (data) is a pointer to (vec4count*4) floats.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
                                                   unsigned int vec4count);

/*
 * Retrieve a floating-point uniform value (what Direct3D calls a "constant").
 *
 * There is a single array of 4-float "registers" shared by all vertex shaders.
 *  This is the "c" register file in Direct3D (c0, c1, c2, etc...)
 *  MojoShader will take care of synchronizing this internal array with the
 *  appropriate variables in the GL shaders.
 *
 * (idx) is the index into the internal array: 0 is the first four floats,
 *  1 is the next four, etc.
 * (data) is a pointer to space for (vec4count*4) floats.
 *  (data) will be filled will current values in the register file. Results
 *  are undefined if you request data past the end of the register file or
 *  previously uninitialized registers.
 *
 * This is a "fast" call; we're just reading from internal memory. We do not
 *  query the GPU or the GL for this information.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glGetVertexShaderUniformF(unsigned int idx, float *data,
                                                   unsigned int vec4count);


/*
 * Set an integer uniform value (what Direct3D calls a "constant").
 *
 * There is a single array of 4-int "registers" shared by all vertex shaders.
 *  This is the "i" register file in Direct3D (i0, i1, i2, etc...)
 *  MojoShader will take care of synchronizing this internal array with the
 *  appropriate variables in the GL shaders.
 *
 * (idx) is the index into the internal array: 0 is the first four ints,
 *  1 is the next four, etc.
 * (data) is a pointer to (ivec4count*4) ints.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
                                                   unsigned int ivec4count);

/*
 * Retrieve an integer uniform value (what Direct3D calls a "constant").
 *
 * There is a single array of 4-int "registers" shared by all vertex shaders.
 *  This is the "i" register file in Direct3D (i0, i1, i2, etc...)
 *  MojoShader will take care of synchronizing this internal array with the
 *  appropriate variables in the GL shaders.
 *
 * (idx) is the index into the internal array: 0 is the first four ints,
 *  1 is the next four, etc.
 * (data) is a pointer to space for (ivec4count*4) ints.
 *  (data) will be filled will current values in the register file. Results
 *  are undefined if you request data past the end of the register file or
 *  previously uninitialized registers.
 *
 * This is a "fast" call; we're just reading from internal memory. We do not
 *  query the GPU or the GL for this information.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glGetVertexShaderUniformI(unsigned int idx, int *data,
                                                   unsigned int ivec4count);

/*
 * Set a boolean uniform value (what Direct3D calls a "constant").
 *
 * There is a single array of "registers" shared by all vertex shaders.
 *  This is the "b" register file in Direct3D (b0, b1, b2, etc...)
 *  MojoShader will take care of synchronizing this internal array with the
 *  appropriate variables in the GL shaders.
 *
 * Unlike the float and int counterparts, booleans are single values, not
 *  four-element vectors...so idx==1 is the second boolean in the internal
 *  array, not the fifth.
 *
 * Non-zero values are considered "true" and zero is considered "false".
 *
 * (idx) is the index into the internal array.
 * (data) is a pointer to (bcount) ints.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
                                                   unsigned int bcount);

/*
 * Retrieve a boolean uniform value (what Direct3D calls a "constant").
 *
 * There is a single array of "registers" shared by all vertex shaders.
 *  This is the "b" register file in Direct3D (b0, b1, b2, etc...)
 *  MojoShader will take care of synchronizing this internal array with the
 *  appropriate variables in the GL shaders.
 *
 * Unlike the float and int counterparts, booleans are single values, not
 *  four-element vectors...so idx==1 is the second boolean in the internal
 *  array, not the fifth.
 *
 * Non-zero values are considered "true" and zero is considered "false".
 *  This function will always return true values as 1, regardless of what
 *  non-zero integer you originally used to set the registers.
 *
 * (idx) is the index into the internal array.
 * (data) is a pointer to space for (bcount) ints.
 *  (data) will be filled will current values in the register file. Results
 *  are undefined if you request data past the end of the register file or
 *  previously uninitialized registers.
 *
 * This is a "fast" call; we're just reading from internal memory. We do not
 *  query the GPU or the GL for this information.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glGetVertexShaderUniformB(unsigned int idx, int *data,
                                                   unsigned int bcount);

/*
 * The equivalent of MOJOSHADER_glSetVertexShaderUniformF() for pixel
 *  shaders. Other than using a different internal array that is specific
 *  to pixel shaders, this functions just like its vertex array equivalent.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
                                                  unsigned int vec4count);


/*
 * The equivalent of MOJOSHADER_glGetVertexShaderUniformF() for pixel
 *  shaders. Other than using a different internal array that is specific
 *  to pixel shaders, this functions just like its vertex array equivalent.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glGetPixelShaderUniformF(unsigned int idx, float *data,
                                                  unsigned int vec4count);


/*
 * The equivalent of MOJOSHADER_glSetVertexShaderUniformI() for pixel
 *  shaders. Other than using a different internal array that is specific
 *  to pixel shaders, this functions just like its vertex array equivalent.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
                                                  unsigned int ivec4count);


/*
 * The equivalent of MOJOSHADER_glGetVertexShaderUniformI() for pixel
 *  shaders. Other than using a different internal array that is specific
 *  to pixel shaders, this functions just like its vertex array equivalent.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glGetPixelShaderUniformI(unsigned int idx, int *data,
                                                  unsigned int ivec4count);

/*
 * The equivalent of MOJOSHADER_glSetVertexShaderUniformB() for pixel
 *  shaders. Other than using a different internal array that is specific
 *  to pixel shaders, this functions just like its vertex array equivalent.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
                                                  unsigned int bcount);

/*
 * The equivalent of MOJOSHADER_glGetVertexShaderUniformB() for pixel
 *  shaders. Other than using a different internal array that is specific
 *  to pixel shaders, this functions just like its vertex array equivalent.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Uniforms are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glGetPixelShaderUniformB(unsigned int idx, int *data,
                                                  unsigned int bcount);

/*
 * Fills register pointers with pointers that are directly used to push uniform
 *  data to the GL shader context.
 *
 * This function is really just for the effects API, you should NOT be using
 *  this unless you know every single line of MojoShader from memory.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_glMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
                                                  float **psf, int **psi, unsigned char **psb);

/*
 * Tells the context that you are done with the memory mapped by
 *  MOJOSHADER_glMapUniformBufferMemory().
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_glUnmapUniformBufferMemory();

/*
 * Set up the vector for the TEXBEM opcode. Most apps can ignore this API.
 *
 * Shader Model 1.1 through 1.3 had an instruction for "fake bump mapping"
 *  called TEXBEM. To use it, you had to set some sampler states,
 *  D3DTSS_BUMPENVMATxx, which would be referenced by the opcode.
 *
 * This functionality was removed from Shader Model 1.4 and later, because
 *  it was special-purpose and limited. The functionality could be built on
 *  more general opcodes, and the sampler state could be supplied in a more
 *  general uniform.
 *
 * However, to support this opcode, we supply a way to specify that sampler
 *  state, and the OpenGL glue code does the right thing to pass that
 *  information to the shader.
 *
 * This call maps to IDirect3DDevice::SetTextureStageState() with the
 *  D3DTSS_BUMPENVMAT00, D3DTSS_BUMPENVMAT01, D3DTSS_BUMPENVMAT10,
 *  D3DTSS_BUMPENVMAT11, D3DTSS_BUMPENVLSCALE, and D3DTSS_BUMPENVLOFFSET
 *  targets. This is only useful for Shader Model < 1.4 pixel shaders, if
 *  they use the TEXBEM or TEXBEML opcode. If you aren't sure, you don't need
 *  this function.
 *
 * Like the rest of your uniforms, you must call MOJOSHADER_glProgramReady()
 *  between setting new values and drawing with them.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * These values are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glSetLegacyBumpMapEnv(unsigned int sampler, float mat00,
                                               float mat01, float mat10, float mat11,
                                               float lscale, float loffset);

/*
 * Return the location of a vertex attribute for the currently-bound program.
 *
 * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
 *  be MOJOSHADER_USAGE_COLOR and 1.
 *
 * The return value is the index of the attribute to be sent to
 *  glVertexAttribPointer, or -1 if the stream is not used.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC int MOJOSHADER_glGetVertexAttribLocation(MOJOSHADER_usage usage, int index);

/*
 * Connect a client-side array to the currently-bound program.
 *
 * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
 *  be MOJOSHADER_USAGE_COLOR and 1.
 *
 * The caller should bind VBOs before this call and treat (ptr) as an offset,
 *  if appropriate.
 *
 * MojoShader will figure out where to plug this stream into the
 *  currently-bound program, and enable the appropriate client-side array.
 *
 * (size), (type), (normalized), (stride), and (ptr) correspond to
 *  glVertexAttribPointer()'s parameters (in most cases, these get passed
 *  unmolested to that very entry point during this function).
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Vertex attributes are not shared between contexts.
 */
 /* !!! FIXME: this should probably be "input" and not "attribute" */
 /* !!! FIXME: or maybe "vertex array" or something. */
DECLSPEC void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
                                              int index, unsigned int size,
                                              MOJOSHADER_attributeType type,
                                              int normalized, unsigned int stride,
                                              const void *ptr);

/*
 * Modify the rate at which this vertex attribute advances during instanced
 *  rendering.
 *
 * This should be called alongside glSetVertexAttribute, as this does not flag
 *  the vertex array as being in use. This just calls glVertexAttribDivisorARB.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 *
 * Vertex attributes are not shared between contexts.
 */
DECLSPEC void MOJOSHADER_glSetVertexAttribDivisor(MOJOSHADER_usage usage,
                                                  int index, unsigned int divisor);

/*
 * Inform MojoShader that it should commit any pending state to the GL. This
 *  must be called after you bind a program and update any inputs, right
 *  before you start drawing, so any outstanding changes made to the shared
 *  constants array (etc) can propagate to the shader during this call.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_glProgramReady(void);

/*
 * Provide information about the current viewport to the prepared shader
 *  program.
 *
 * There are numerous components of OpenGL and Direct3D where the coordinate
 *  systems do not match, and so the vertex/pixel shaders have to be modified to
 *  compensate for these mismatches (for example, gl_FragCoord requires some
 *  additional math on the Y coordinate to match vPos when rendering to the
 *  backbuffer). Call this after MOJOSHADER_glProgramReady to apply all of the
 *  relevant coordinate fixups at once.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_glProgramViewportInfo(int viewportW, int viewportH,
                                               int backbufferW, int backbufferH,
                                               int renderTargetBound);

/*
 * Free the resources of a linked program. This will delete the GL object
 *  and free memory.
 *
 * If the program is currently bound by MOJOSHADER_glBindProgram(), it will
 *  be deleted as soon as it becomes unbound.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_glDeleteProgram(MOJOSHADER_glProgram *program);

/*
 * Free the resources of a compiled shader. This will delete the GL object
 *  and free memory.
 *
 * If the shader is currently referenced by a linked program (or is currently
 *  bound with MOJOSHADER_glBindShaders()), it will be deleted as soon as all
 *  referencing programs are deleted and it is no longer bound, too.
 *
 * This call is NOT thread safe! As most OpenGL implementations are not thread
 *  safe, you should probably only call this from the same thread that created
 *  the GL context.
 *
 * This call requires a valid MOJOSHADER_glContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_glMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader);

/*
 * Deinitialize MojoShader's OpenGL shader management.
 *
 * You must call this once, while your GL context (not MojoShader context) is
 *  still current, if you previously had a successful call to
 *  MOJOSHADER_glCreateContext(). This should be the last MOJOSHADER_gl*
 *  function you call until you've prepared a context again.
 *
 * This will clean up resources previously allocated, and may call into the GL.
 *
 * This will not clean up shaders and programs you created! Please call
 *  MOJOSHADER_glDeleteShader() and MOJOSHADER_glDeleteProgram() to clean
 *  those up before calling this function!
 *
 * This function destroys the MOJOSHADER_glContext you pass it. If it's the
 *  current context, then no context will be current upon return.
 *
 * This call is NOT thread safe! There must not be any other MOJOSHADER_gl*
 *  functions running when this is called. Also, as most OpenGL implementations
 *  are not thread safe, you should probably only call this from the same
 *  thread that created the GL context.
 */
DECLSPEC void MOJOSHADER_glDestroyContext(MOJOSHADER_glContext *ctx);


/* Metal interface... */

typedef struct MOJOSHADER_mtlContext MOJOSHADER_mtlContext;
typedef struct MOJOSHADER_mtlShader MOJOSHADER_mtlShader;

/*
 * Prepare MojoShader to manage Metal shaders.
 *
 * This is really just for the effects framework. Don't call this unless
 *  you know for sure that you need it.
 *
 * You must call this only once, AFTER you have created your MTLDevice.
 *  This function will lookup the Objective-C selectors it needs, after which
 *  it may call them at any time until you call MOJOSHADER_mtlDestroyContext().
 *
 * (device) refers to the active MTLDevice, cast from id<MTLDevice> to void*.
 *
 * As MojoShader requires some memory to be allocated, you may provide a
 *  custom allocator to this function, which will be used to allocate/free
 *  memory. They function just like malloc() and free(). We do not use
 *  realloc(). If you don't care, pass NULL in for the allocator functions.
 *  If your allocator needs instance-specific data, you may supply it with the
 *  (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
 *  functions.
 *
 * Returns a new context on success, NULL on error. If you get a new context,
 *  you need to make it current before using it with
 *  MOJOSHADER_mtlMakeContextCurrent().
 */
DECLSPEC MOJOSHADER_mtlContext *MOJOSHADER_mtlCreateContext(void *mtlDevice,
                                        MOJOSHADER_malloc m, MOJOSHADER_free f,
                                        void *malloc_d);

/*
 * You must call this before using the context that you got from
 *  MOJOSHADER_mtlCreateContext(), and must use it when you switch to a new
 *  context.
 *
 * It is legal to call this with a NULL pointer to make no context current,
 *  but you need a valid context to be current to use most of MojoShader.
 */
DECLSPEC void MOJOSHADER_mtlMakeContextCurrent(MOJOSHADER_mtlContext *ctx);

/*
 * Transform a buffer of Direct3D shader bytecode into a struct containing
 *  Metal shader metadata.
 *
 * This function is only for convenience, specifically for compatibility with
 *  the effects API.
 *
 * Despite the name, this does NOT compile a shader! The actual compilation
 *  happens inside MOJOSHADER_mtlCompileLibrary, which batch-compiles an effect
 *  into a single MTLLibrary.
 *
 *   (mainfn) is the name of the shader's main function.
 *   (tokenbuf) is a buffer of Direct3D shader bytecode.
 *   (bufsize) is the size, in bytes, of the bytecode buffer.
 *   (swiz), (swizcount), (smap), and (smapcount) are passed to
 *   MOJOSHADER_parse() unmolested.
 *
 * Returns NULL on error, or a shader handle on success.
 *
 * This call requires a valid MOJOSHADER_mtlContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_mtlMakeContextCurrent().
 */
DECLSPEC MOJOSHADER_mtlShader *MOJOSHADER_mtlCompileShader(const char *mainfn,
                                                const unsigned char *tokenbuf,
                                                const unsigned int bufsize,
                                                const MOJOSHADER_swizzle *swiz,
                                                const unsigned int swizcount,
                                                const MOJOSHADER_samplerMap *smap,
                                                const unsigned int smapcount);

/*
 * Increments a shader's internal refcount. To decrement the refcount, call
 *  MOJOSHADER_mtlDeleteShader().
 */
DECLSPEC void MOJOSHADER_mtlShaderAddRef(MOJOSHADER_mtlShader *shader);

/*
 * Get the MOJOSHADER_parseData structure that was produced from the
 *  call to MOJOSHADER_mtlCompileShader().
 *
 * This data is read-only, and you should NOT attempt to free it. This
 *  pointer remains valid until the shader is deleted.
 */
DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_mtlGetShaderParseData(
                                                MOJOSHADER_mtlShader *shader);

/*
 * This "binds" individual shaders, which effectively means the context
 *  will store these shaders for later retrieval. No actual binding or
 *  pipeline creation is performed.
 *
 * This function is only for convenience, specifically for compatibility with
 *  the effects API.
 *
 * This call requires a valid MOJOSHADER_mtlContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_mtlMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_mtlBindShaders(MOJOSHADER_mtlShader *vshader,
                                        MOJOSHADER_mtlShader *pshader);

/*
 * This queries for the shaders currently bound to the active context.
 *
 * This function is only for convenience, specifically for compatibility with
 *  the effects API.
 *
 * This call requires a valid MOJOSHADER_mtlContext to have been created,
 *  or it will crash your program. See MOJOSHADER_mtlMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_mtlGetBoundShaders(MOJOSHADER_mtlShader **vshader,
                                            MOJOSHADER_mtlShader **pshader);

/*
 * Fills register pointers with pointers that are directly used to push uniform
 *  data to the Metal shader context.
 *
 * This function is really just for the effects API, you should NOT be using
 *  this unless you know every single line of MojoShader from memory.
 *
 * This call requires a valid MOJOSHADER_mtlContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_mtlMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_mtlMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
                                                   float **psf, int **psi, unsigned char **psb);

/*
 * Tells the context that you are done with the memory mapped by
 *  MOJOSHADER_mtlMapUniformBufferMemory().
 *
 * This call requires a valid MOJOSHADER_mtlContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_mtlMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_mtlUnmapUniformBufferMemory();

/*
 * This queries for the uniform buffer and byte offsets for each of the
 *  currently bound shaders.
 *
 * This function is only for convenience, specifically for compatibility with
 *  the effects API.
 *
 * This call requires a valid MOJOSHADER_mtlContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_mtlMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_mtlGetUniformData(void **buf, int *voff, int *poff);

/*
 * Get the MTLFunction* from the given MOJOSHADER_mtlShader.
 */
DECLSPEC void *MOJOSHADER_mtlGetFunctionHandle(MOJOSHADER_mtlShader *shader);

/*
 * Resets buffer offsets to prepare for the next frame.
 *
 * Always call this after submitting the final command buffer for a frame!
 */
DECLSPEC void MOJOSHADER_mtlEndFrame(void);

/*
 * Return the location of a vertex attribute for the given shader.
 *
 * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
 *  be MOJOSHADER_USAGE_COLOR and 1.
 *
 * The return value is the index of the attribute to be used to create
 *  a MTLVertexAttributeDescriptor, or -1 if the stream is not used.
 */
DECLSPEC int MOJOSHADER_mtlGetVertexAttribLocation(MOJOSHADER_mtlShader *vert,
                                          MOJOSHADER_usage usage, int index);

/*
 * Get any error state we might have picked up, such as failed shader
 *  compilation.
 *
 * Returns a human-readable string. This string is for debugging purposes, and
 *  not guaranteed to be localized, coherent, or user-friendly in any way.
 *  It's for programmers!
 *
 * The latest error may remain between calls. New errors replace any existing
 *  error. Don't check this string for a sign that an error happened, check
 *  return codes instead and use this for explanation when debugging.
 *
 * This call does NOT require a valid MOJOSHADER_mtlContext to have been made
 *  current. The error buffer is shared between contexts, so you can get
 *  error results from a failed MOJOSHADER_mtlCreateContext().
 *
 * Do not free the returned string: it's a pointer to a static internal
 *  buffer. Do not keep the pointer around, either, as it's likely to become
 *  invalid as soon as you call into MojoShader again.
 */
DECLSPEC const char *MOJOSHADER_mtlGetError(void);

/*
 * Release the given MTLLibrary and all MTLFunctions it contains.
 *
 * This does NOT free MOJOSHADER_mtlShaders! Call MOJOSHADER_mtlDeleteShader()
 *  on all the shaders of the library before you call this.
 *
 * This call requires a valid MOJOSHADER_mtlContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_mtlMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_mtlDeleteLibrary(void *library);

/*
 * Free the resources of a compiled shader. This will delete the MojoShader
 *  shader struct and free memory.
 *
 * This does NOT release the actual shader! The shader data belongs to an
 *  MTLLibrary that must be deleted with MOJOSHADER_mtlDeleteLibrary().
 *
 * This call requires a valid MOJOSHADER_mtlContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_mtlMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_mtlDeleteShader(MOJOSHADER_mtlShader *shader);

/*
 * Deinitialize MojoShader's Metal shader management.
 *
 * This should be the last MOJOSHADER_mtl* function you call until you've
 *  prepared a context again.
 *
 * This will clean up resources previously allocated, and may call into Metal.
 *
 * This will not clean up shaders and libraries you created! Please call
 *  MOJOSHADER_mtlDeleteShader() and MOJOSHADER_mtlDeleteLibrary() to clean
 *  those up before calling this function!
 *
 * This function destroys the MOJOSHADER_mtlContext you pass it. If it's the
 *  current context, then no context will be current upon return.
 */
DECLSPEC void MOJOSHADER_mtlDestroyContext(MOJOSHADER_mtlContext *_ctx);

/* Vulkan interface */

/* Avoid including vulkan.h, don't define handles if it's already included */
#ifdef VULKAN_H_
#define NO_MOJOSHADER_VULKAN_TYPEDEFS
#endif
#ifndef NO_MOJOSHADER_VULKAN_TYPEDEFS
#ifdef _WIN32
#define VKAPI_CALL __stdcall
#define VKAPI_PTR VKAPI_CALL
#else
#define VKAPI_CALL
#define VKAPI_PTR
#endif
#define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;

#if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
#define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object;
#else
#define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef unsigned long long object;
#endif

VK_DEFINE_HANDLE(VkInstance)
VK_DEFINE_HANDLE(VkDevice)
VK_DEFINE_HANDLE(VkPhysicalDevice)
VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkBuffer)
VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkShaderModule)

#endif /* !NO_MOJOSHADER_VULKAN_TYPEDEFS */

typedef void (VKAPI_PTR *PFN_MOJOSHADER_vkVoidFunction)(void);
typedef PFN_MOJOSHADER_vkVoidFunction (VKAPI_PTR *PFN_MOJOSHADER_vkGetDeviceProcAddr)(
	VkDevice device,
	const char* pName
);
typedef PFN_MOJOSHADER_vkVoidFunction (VKAPI_PTR *PFN_MOJOSHADER_vkGetInstanceProcAddr)(
	VkInstance instance,
	const char* pName
);

typedef struct MOJOSHADER_vkContext MOJOSHADER_vkContext;
typedef struct MOJOSHADER_vkShader MOJOSHADER_vkShader;
typedef struct MOJOSHADER_vkProgram MOJOSHADER_vkProgram;

/*
 * Prepares a context to manage Vulkan shaders.
 *
 * Don't call this unless you know for sure that you need it.
 *
 * You must call this after creating VkDevice and VkInstance.
 *
 * (instance) refers to VkInstance, cast to void*.
 *
 * (device) refers to VkDevice, cast to void*.
 *
 * (frames_in_flight) refers to the maximum number of frames that can be
 * processed simultaneously.
 *
 * (lookup) refers to PFN_vkGetDeviceProcAddr, a function pointer that
 * is used to dynamically link required Vulkan functions.
 *
 * You must pass in the graphics queue family index and the memory type index
 * you will be using with your Vulkan instance.
 *
 * As MojoShader requires some memory to be allocated, you may provide a
 *  custom allocator to this function, which will be used to allocate/free
 *  memory. They function just like malloc() and free(). We do not use
 *  realloc(). If you don't care, pass NULL in for the allocator functions.
 *  If your allocator needs instance-specific data, you may supply it with the
 *  (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
 *  functions.
 *
 * The context created by this function will automatically become the current
 *  context. No further action is needed by the caller.
 *
 * Returns 0 on success or -1 on failure.
 */

DECLSPEC MOJOSHADER_vkContext *MOJOSHADER_vkCreateContext(VkInstance *instance,
                                                          VkPhysicalDevice *physical_device,
                                                          VkDevice *logical_device,
                                                          PFN_MOJOSHADER_vkGetInstanceProcAddr instance_lookup,
                                                          PFN_MOJOSHADER_vkGetDeviceProcAddr lookup,
                                                          unsigned int graphics_queue_family_index,
                                                          unsigned int max_uniform_buffer_range,
                                                          unsigned int min_uniform_buffer_offset_alignment,
                                                          MOJOSHADER_malloc m, MOJOSHADER_free f,
                                                          void *malloc_d);

/*
 * You must call this before using the context that you got from
 *  MOJOSHADER_vkCreateContext(), and must use it when you switch to a new GL
 *  context.
 *
 * You can only have one MOJOSHADER_vkContext per actual Vulkan context, or
 *  undefined behaviour will result.
 *
 * It is legal to call this with a NULL pointer to make no context current,
 *  but you need a valid context to be current to use most of MojoShader.
 */
DECLSPEC void MOJOSHADER_vkMakeContextCurrent(MOJOSHADER_vkContext *_ctx);

/*
 * Get any error state we might have picked up.
 *
 * Returns a human-readable string. This string is for debugging purposes, and
 *  not guaranteed to be localized, coherent, or user-friendly in any way.
 *  It's for programmers!
 *
 * The latest error may remain between calls. New errors replace any existing
 *  error. Don't check this string for a sign that an error happened, check
 *  return codes instead and use this for explanation when debugging.
 *
 * Do not free the returned string: it's a pointer to a static internal
 *  buffer. Do not keep the pointer around, either, as it's likely to become
 *  invalid as soon as you call into MojoShader again.
 *
 * This call does NOT require a valid MOJOSHADER_vkContext to have been made
 *  current. The error buffer is shared between contexts, so you can get
 *  error results from a failed MOJOSHADER_vkCreateContext().
 */
DECLSPEC const char *MOJOSHADER_vkGetError();

/*
 * Deinitialize MojoShader's Vulkan shader management.
 *
 * You must call this once, while your Vulkan context (not MojoShader context) is
 *  still current, if you previously had a successful call to
 *  MOJOSHADER_vkCreateContext(). This should be the last MOJOSHADER_vk*
 *  function you call until you've prepared a context again.
 *
 * This will clean up resources previously allocated, and may call into Vulkan.
 *
 * This will not clean up shaders and programs you created! Please call
 *  MOJOSHADER_vkDeleteShader() and MOJOSHADER_vkDeleteProgram() to clean
 *  those up before calling this function!
 *
 * This function destroys the MOJOSHADER_vkContext you pass it. If it's the
 *  current context, then no context will be current upon return.
 */
DECLSPEC void MOJOSHADER_vkDestroyContext(MOJOSHADER_vkContext *ctx);

/*
 * Compile a buffer of Direct3D shader bytecode into a Vulkan shader module.
 *
 *   (tokenbuf) is a buffer of Direct3D shader bytecode.
 *   (bufsize) is the size, in bytes, of the bytecode buffer.
 *   (swiz), (swizcount), (smap), and (smapcount) are passed to
 *   MOJOSHADER_parse() unmolested.
 *
 * Returns NULL on error, or a shader handle on success.
 *
 * This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 *
 * Compiled shaders from this function may not be shared between contexts.
 */
DECLSPEC MOJOSHADER_vkShader *MOJOSHADER_vkCompileShader(const char *mainfn,
                                                         const unsigned char *tokenbuf,
                                                         const unsigned int bufsize,
                                                         const MOJOSHADER_swizzle *swiz,
                                                         const unsigned int swizcount,
                                                         const MOJOSHADER_samplerMap *smap,
                                                         const unsigned int smapcount);

/*
 * Increments a shader's internal refcount.
 *
 * To decrement the refcount, call
 *  MOJOSHADER_vkDeleteShader().
 *
 * This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_vkShaderAddRef(MOJOSHADER_vkShader *shader);

/*
 * Increments a shader's internal refcount.
 *
 * To decrement the refcount, call MOJOSHADER_vkDeleteShader().
 *
 * This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_vkDeleteShader(MOJOSHADER_vkShader *shader);

/*
 * Get the MOJOSHADER_parseData structure that was produced from the
 *  call to MOJOSHADER_vkCompileShader().
 *
 * This data is read-only, and you should NOT attempt to free it. This
 *  pointer remains valid until the shader is deleted.
 */
DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_vkGetShaderParseData(
                                                MOJOSHADER_vkShader *shader);

/*
 * Link a vertex and pixel shader into a working Vulkan shader program.
 *  (vshader) or (pshader) can NOT be NULL, unlike OpenGL.
 *
 * You can reuse shaders in various combinations across
 *  multiple programs, by relinking different pairs.
 *
 * It is illegal to give a vertex shader for (pshader) or a pixel shader
 *  for (vshader).
 *
 * Once you have successfully linked a program, you may render with it.
 *
 * Returns NULL on error, or a program handle on success.
 *
 * This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 */
DECLSPEC MOJOSHADER_vkProgram *MOJOSHADER_vkLinkProgram(MOJOSHADER_vkShader *vshader,
                                                        MOJOSHADER_vkShader *pshader);

/*
 * This binds the program to the active context, and does nothing particularly
 * special until you start working with uniform buffers or shader modules.
 *
 * After binding a program, you should update any uniforms you care about
 *  with MOJOSHADER_vkMapUniformBufferMemory() (etc), set any vertex arrays
 *  using MOJOSHADER_vkGetVertexAttribLocation(), and finally call
 *  MOJOSHADER_vkGetShaderModules() to get the final modules. Then you may
 *  begin building your pipeline state objects.
 *
 * This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_vkBindProgram(MOJOSHADER_vkProgram *program);

/*
 * Free the resources of a linked program. This will delete the shader modules
 *  and free memory.
 *
 * If the program is currently bound by MOJOSHADER_vkBindProgram(), it will
 *  be deleted as soon as it becomes unbound.
 *
 * This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_vkDeleteProgram(MOJOSHADER_vkProgram *program);

/*
 * This "binds" individual shaders, which effectively means the context
 *  will store these shaders for later retrieval. No actual binding or
 *  pipeline creation is performed.
 *
 * This function is only for convenience, specifically for compatibility
 *  with the effects API.
 *
 * This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_vkBindShaders(MOJOSHADER_vkShader *vshader,
                                       MOJOSHADER_vkShader *pshader);

/*
 * This queries for the shaders currently bound to the active context.
 *
 * This function is only for convenience, specifically for compatibility
 *  with the effects API.
 *
 * This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_vkGetBoundShaders(MOJOSHADER_vkShader **vshader,
                                           MOJOSHADER_vkShader **pshader);

/*
 * Fills register pointers with pointers that are directly used to push uniform
 *  data to the Vulkan shader context.
 *
 * This function is really just for the effects API, you should NOT be using
 *  this unless you know every single line of MojoShader from memory.
 *
 * This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_vkMapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
                                                  float **psf, int **psi, unsigned char **psb);

/*
 * Tells the context that you are done with the memory mapped by
 *  MOJOSHADER_vkMapUniformBufferMemory().
 *
 * This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_vkUnmapUniformBufferMemory();

/*
 * This queries for the uniform buffer, byte offset and byte size for each of the
 *  currently bound shaders.
 *
 * This function is only for convenience, specifically for compatibility with
 *  the effects API.
 *
 * This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 */
DECLSPEC void MOJOSHADER_vkGetUniformBuffers(VkBuffer *vbuf,
                                             unsigned long long *voff,
                                             unsigned long long *vsize,
                                             VkBuffer *pbuf,
                                             unsigned long long *poff,
                                             unsigned long long *psize);

/*
 * Prepares uniform buffers for reuse.
 *
 * Always call this after submitting the final command buffer for a frame!
 */
DECLSPEC void MOJOSHADER_vkEndFrame();

/*
 * Return the location of a vertex attribute for the given shader.
 *
 * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
 *  be MOJOSHADER_USAGE_COLOR and 1.
 *
 * The return value is the index of the attribute to be used to create
 *  a VkVertexInputAttributeDescription, or -1 if the stream is not used.
 *
 *  This call requires a valid MOJOSHADER_vkContext to have been made current,
 *  or it will crash your program. See MOJOSHADER_vkMakeContextCurrent().
 */
DECLSPEC int MOJOSHADER_vkGetVertexAttribLocation(MOJOSHADER_vkShader *vert,
                                                  MOJOSHADER_usage usage,
                                                  int index);

/*
 * Get the VkShaderModules from the currently bound shader program.
 */
DECLSPEC void MOJOSHADER_vkGetShaderModules(VkShaderModule *vmodule,
                                            VkShaderModule *pmodule);

/* D3D11 interface... */

typedef struct MOJOSHADER_d3d11Shader MOJOSHADER_d3d11Shader;

/*
 * Prepare MojoShader to manage Direct3D 11 shaders.
 *
 * You do not need to call this if all you want is MOJOSHADER_parse().
 *
 * You must call this once AFTER you have successfully built your D3D11 context.
 *
 * As MojoShader requires some memory to be allocated, you may provide a
 *  custom allocator to this function, which will be used to allocate/free
 *  memory. They function just like malloc() and free(). We do not use
 *  realloc(). If you don't care, pass NULL in for the allocator functions.
 *  If your allocator needs instance-specific data, you may supply it with the
 *  (malloc_d) parameter. This pointer is passed as-is to your (m) and (f)
 *  functions.
 *
 * This call is only as thread safe as your D3D11 context! If you call your
 *  context from multiple threads, you must protect this call with whatever
 *  thread synchronization technique you have for your other D3D calls.
 */
DECLSPEC int MOJOSHADER_d3d11CreateContext(void *device, void *deviceContext,
                                           MOJOSHADER_malloc m, MOJOSHADER_free f,
                                           void *malloc_d);

/*
 * Get any error state we might have picked up, such as failed shader
 *  compilation.
 *
 * Returns a human-readable string. This string is for debugging purposes, and
 *  not guaranteed to be localized, coherent, or user-friendly in any way.
 *  It's for programmers!
 *
 * The latest error may remain between calls. New errors replace any existing
 *  error. Don't check this string for a sign that an error happened, check
 *  return codes instead and use this for explanation when debugging.
 *
 * Do not free the returned string: it's a pointer to a static internal
 *  buffer. Do not keep the pointer around, either, as it's likely to become
 *  invalid as soon as you call into MojoShader again.
 */
DECLSPEC const char *MOJOSHADER_d3d11GetError(void);

/*
 * Compile a buffer of Direct3D 9 shader bytecode into a Direct3D 11 shader.
 *  You still need to link the shader before you may render with it.
 *
 *   (mainfn) is the name of the shader's main function.
 *   (tokenbuf) is a buffer of Direct3D shader bytecode.
 *   (bufsize) is the size, in bytes, of the bytecode buffer.
 *   (swiz), (swizcount), (smap), and (smapcount) are passed to
 *   MOJOSHADER_parse() unmolested.
 *
 * Returns NULL on error, or a shader handle on success.
 *
 * This call is only as thread safe as your D3D11 context! If you call your
 *  context from multiple threads, you must protect this call with whatever
 *  thread synchronization technique you have for your other D3D calls.
 */
DECLSPEC MOJOSHADER_d3d11Shader *MOJOSHADER_d3d11CompileShader(const char *mainfn,
                                                               const unsigned char *tokenbuf,
                                                               const unsigned int bufsize,
                                                               const MOJOSHADER_swizzle *swiz,
                                                               const unsigned int swizcount,
                                                               const MOJOSHADER_samplerMap *smap,
                                                               const unsigned int smapcount);

/*
 * Increments a shader's internal refcount. To decrement the refcount, call
 *  MOJOSHADER_glDeleteShader().
 *
 * This call is only as thread safe as your D3D11 context! If you call your
 *  context from multiple threads, you must protect this call with whatever
 *  thread synchronization technique you have for your other D3D calls.
 */
DECLSPEC void MOJOSHADER_d3d11ShaderAddRef(MOJOSHADER_d3d11Shader *shader);

/*
 * Get the MOJOSHADER_parseData structure that was produced from the
 *  call to MOJOSHADER_d3d11CompileShader().
 *
 * This data is read-only, and you should NOT attempt to free it. This
 *  pointer remains valid until the shader is deleted.
 */
DECLSPEC const MOJOSHADER_parseData *MOJOSHADER_d3d11GetShaderParseData(
                                                MOJOSHADER_d3d11Shader *shader);

/*
 * This binds individual shaders together, to be linked into a single working
 *  program once MOJOSHADER_d3d11ProgramReady is called.
 *
 * This call is only as thread safe as your D3D11 context! If you call your
 *  context from multiple threads, you must protect this call with whatever
 *  thread synchronization technique you have for your other D3D calls.
 */
DECLSPEC void MOJOSHADER_d3d11BindShaders(MOJOSHADER_d3d11Shader *vshader,
                                          MOJOSHADER_d3d11Shader *pshader);

/*
 * This queries for the shaders currently bound to the active context.
 *
 * This function is only for convenience, specifically for compatibility with
 * the effects API.
 *
 * This call is only as thread safe as your D3D11 context! If you call your
 *  context from multiple threads, you must protect this call with whatever
 *  thread synchronization technique you have for your other D3D calls.
 */
DECLSPEC void MOJOSHADER_d3d11GetBoundShaders(MOJOSHADER_d3d11Shader **vshader,
                                              MOJOSHADER_d3d11Shader **pshader);

/*
 * Fills register pointers with pointers that are directly used to push uniform
 *  data to the D3D11 shader context.
 *
 * This function is really just for the effects API, you should NOT be using
 *  this unless you know every single line of MojoShader from memory.
 *
 * This call is only as thread safe as your D3D11 context! If you call your
 *  context from multiple threads, you must protect this call with whatever
 *  thread synchronization technique you have for your other D3D calls.
 */
DECLSPEC void MOJOSHADER_d3d11MapUniformBufferMemory(float **vsf, int **vsi, unsigned char **vsb,
                                                     float **psf, int **psi, unsigned char **psb);

/*
 * Tells the context that you are done with the memory mapped by
 *  MOJOSHADER_d3d11MapUniformBufferMemory().
 *
 * This call is only as thread safe as your D3D11 context! If you call your
 *  context from multiple threads, you must protect this call with whatever
 *  thread synchronization technique you have for your other D3D calls.
 */
DECLSPEC void MOJOSHADER_d3d11UnmapUniformBufferMemory();

/*
 * Return the location of a vertex attribute for the given vertex shader.
 *
 * (usage) and (index) map to Direct3D vertex declaration values: COLOR1 would
 *  be MOJOSHADER_USAGE_COLOR and 1.
 *
 * The return value is the index of the attribute to be used when building the
 *  input layout object.
 */
DECLSPEC int MOJOSHADER_d3d11GetVertexAttribLocation(MOJOSHADER_d3d11Shader *vert,
                                                     MOJOSHADER_usage usage,
                                                     int index);

/*
 * Using the given input layout, compiles the vertex shader with input
 *  parameters that will be compatible with the incoming vertex data.
 *
 * (inputLayoutHash) is an application-defined value to differentiate unique
 *  vertex declarations that will be passed to the vertex shader.
 *  (elements) is an array of D3D11_INPUT_ELEMENT_DESCs, with (elementCount)
 *  entries. (bytecode) and (bytecodeLength) will be filled with the final
 *  compiled D3D11 vertex shader.
 *
 * This call is only as thread safe as your D3D11 context! If you call your
 *  context from multiple threads, you must protect this call with whatever
 *  thread synchronization technique you have for your other D3D calls.
 */
DECLSPEC void MOJOSHADER_d3d11CompileVertexShader(unsigned long long inputLayoutHash,
                                                  void *elements, int elementCount,
                                                  void **bytecode, int *bytecodeLength);

/*
 * Inform MojoShader that it should commit any pending state and prepare the
 *  final shader program object, linking the input/output parameter data to
 *  be compatible with the more-strict Shader Model 4 rule set. This must be
 *  called after you bind shaders and update any inputs, right before you start
 *  drawing, so any outstanding changes made to the shared constants array (etc)
 *  can propagate to the shader during this call.
 *
 * This call is only as thread safe as your D3D11 context! If you call your
 *  context from multiple threads, you must protect this call with whatever
 *  thread synchronization technique you have for your other D3D calls.
 */
DECLSPEC void MOJOSHADER_d3d11ProgramReady(unsigned long long inputLayoutHash);

/*
 * Free the resources of a compiled shader. This will delete the shader object
 *  and free memory.
 *
 * This call is only as thread safe as your D3D11 context! If you call your
 *  context from multiple threads, you must protect this call with whatever
 *  thread synchronization technique you have for your other D3D calls.
 */
DECLSPEC void MOJOSHADER_d3d11DeleteShader(MOJOSHADER_d3d11Shader *shader);

/*
 * Deinitialize MojoShader's D3D11 shader management.
 *
 * This will clean up resources previously allocated for the active context.
 *
 * This will NOT clean up shaders you created! Please destroy all shaders
 *  before calling this function.
 */
DECLSPEC void MOJOSHADER_d3d11DestroyContext(void);


/* Effects interface... */
#include "mojoshader_effects.h"


#ifdef __cplusplus
}
#endif

#endif  /* include-once blocker. */

/* end of mojoshader.h ... */