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
use crate::sys::{
    self,
    display::DisplayPixelFormat,
    ge::{GeBreakParam, GeCommand, GeContext, GeListArgs, GeListState},
    kernel::SceUid,
    types::{ScePspFMatrix4, ScePspFVector3, ScePspIMatrix4, ScePspIVector4},
};
use core::{ffi::c_void, mem, ptr::addr_of_mut, ptr::null_mut};
use num_enum::TryFromPrimitive;

#[allow(clippy::approx_constant)]
pub const GU_PI: f32 = 3.141593;

/// Primitive types
#[repr(u32)]
#[derive(Copy, Clone, Debug)]
pub enum GuPrimitive {
    /// Single pixel points (1 vertex per primitive)
    Points = 0,
    /// Single pixel lines (2 vertices per primitive)
    Lines = 1,
    /// Single pixel line-strip (2 vertices for the first primitive, 1 for every following)
    LineStrip = 2,
    /// Filled triangles (3 vertices per primitive)
    Triangles = 3,
    /// Filled triangles-strip (3 vertices for the first primitive, 1 for every following)
    TriangleStrip = 4,
    /// Filled triangle-fan (3 vertices for the first primitive, 1 for every following)
    TriangleFan = 5,
    /// Filled blocks (2 vertices per primitive)
    Sprites = 6,
}

/// Patch primitive types
#[repr(u32)]
#[derive(Copy, Clone, Debug)]
pub enum PatchPrimitive {
    /// Single pixel points (1 vertex per primitive)
    Points = 0,
    /// Single pixel line-strip (2 vertices for the first primitive, 1 for every following)
    LineStrip = 2,
    /// Filled triangles-strip (3 vertices for the first primitive, 1 for every following)
    TriangleStrip = 4,
}

/// States
#[derive(Debug, Clone, Copy, Eq, PartialEq, TryFromPrimitive)]
#[repr(u32)]
pub enum GuState {
    AlphaTest = 0,
    DepthTest = 1,
    ScissorTest = 2,
    StencilTest = 3,
    Blend = 4,
    CullFace = 5,
    Dither = 6,
    Fog = 7,
    ClipPlanes = 8,
    Texture2D = 9,
    Lighting = 10,
    Light0 = 11,
    Light1 = 12,
    Light2 = 13,
    Light3 = 14,
    LineSmooth = 15,
    PatchCullFace = 16,
    ColorTest = 17,
    ColorLogicOp = 18,
    FaceNormalReverse = 19,
    PatchFace = 20,
    Fragment2X = 21,
}

/// Matrix modes
#[repr(u32)]
#[derive(Copy, Clone, Debug)]
pub enum MatrixMode {
    Projection = 0,
    View = 1,
    Model = 2,
    Texture = 3,
}

bitflags::bitflags! {
    /// The vertex type decides how the vertices align and what kind of
    /// information they contain.
    #[repr(transparent)]
    pub struct VertexType: i32 {
        /// 8-bit texture coordinates
        const TEXTURE_8BIT = 1;
        /// 16-bit texture coordinates
        const TEXTURE_16BIT = 2;
        /// 32-bit texture coordinates (float)
        const TEXTURE_32BITF = 3;

        /// 16-bit color (R5G6B5A0)
        const COLOR_5650 = 4 << 2;
        /// 16-bit color (R5G5B5A1)
        const COLOR_5551 = 5 << 2;
        /// 16-bit color (R4G4B4A4)
        const COLOR_4444 = 6 << 2;
        /// 32-bit color (R8G8B8A8)
        const COLOR_8888 = 7 << 2;

        /// 8-bit normals
        const NORMAL_8BIT = 1 << 5;
        /// 16-bit normals
        const NORMAL_16BIT = 2 << 5;
        /// 32-bit normals (float)
        const NORMAL_32BITF = 3 << 5;

        /// 8-bit vertex position
        const VERTEX_8BIT = 1 << 7;
        /// 16-bit vertex position
        const VERTEX_16BIT = 2 << 7;
        /// 32-bit vertex position (float)
        const VERTEX_32BITF = 3 << 7;

        /// 8-bit weights
        const WEIGHT_8BIT = 1 << 9;
        /// 16-bit weights
        const WEIGHT_16BIT = 2 << 9;
        /// 32-bit weights (float)
        const WEIGHT_32BITF = 3 << 9;

        /// 8-bit vertex index
        const INDEX_8BIT = 1 << 11;
        /// 16-bit vertex index
        const INDEX_16BIT = 2 << 11;

        // FIXME: Need to document this.
        // Number of weights (1-8)
        const WEIGHTS1 = Self::num_weights(1);
        const WEIGHTS2 = Self::num_weights(2);
        const WEIGHTS3 = Self::num_weights(3);
        const WEIGHTS4 = Self::num_weights(4);
        const WEIGHTS5 = Self::num_weights(5);
        const WEIGHTS6 = Self::num_weights(6);
        const WEIGHTS7 = Self::num_weights(7);
        const WEIGHTS8 = Self::num_weights(8);

        // Number of vertices (1-8)
        const VERTICES1 = Self::num_vertices(1);
        const VERTICES2 = Self::num_vertices(2);
        const VERTICES3 = Self::num_vertices(3);
        const VERTICES4 = Self::num_vertices(4);
        const VERTICES5 = Self::num_vertices(5);
        const VERTICES6 = Self::num_vertices(6);
        const VERTICES7 = Self::num_vertices(7);
        const VERTICES8 = Self::num_vertices(8);

        /// Coordinate is passed directly to the rasterizer
        const TRANSFORM_2D = 1 << 23;
        /// Coordinate is transformed before being passed to rasterizer
        const TRANSFORM_3D = 0;
    }
}

impl VertexType {
    const fn num_weights(n: u32) -> i32 {
        (((n - 1) & 7) << 14) as i32
    }

    const fn num_vertices(n: u32) -> i32 {
        (((n - 1) & 7) << 18) as i32
    }
}

/// Texture pixel formats
// TODO: Better documentation
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum TexturePixelFormat {
    /// Hicolor, 16-bit.
    Psm5650 = 0,
    /// Hicolor, 16-bit
    Psm5551 = 1,
    /// Hicolor, 16-bit
    Psm4444 = 2,
    /// Truecolor, 32-bit
    Psm8888 = 3,
    /// Indexed, 4-bit (2 pixels per byte)
    PsmT4 = 4,
    /// Indexed, 8-bit
    PsmT8 = 5,
    /// Indexed, 16-bit
    PsmT16 = 6,
    /// Indexed, 32-bit
    PsmT32 = 7,
    PsmDxt1 = 8,
    PsmDxt3 = 9,
    PsmDxt5 = 10,
}

/// Spline Mode
#[repr(u32)]
pub enum SplineMode {
    FillFill = 0,
    OpenFill = 1,
    FillOpen = 2,
    OpenOpen = 3,
}

/// Shading Model
#[repr(u32)]
// TODO: Should this be `ShadeMode` (no L)?
pub enum ShadingModel {
    Flat = 0,
    Smooth = 1,
}

/// Logical operation
#[repr(u32)]
pub enum LogicalOperation {
    Clear = 0,
    And = 1,
    AndReverse = 2,
    Copy = 3,
    AndInverted = 4,
    Noop = 5,
    Xor = 6,
    Or = 7,
    Nor = 8,
    Equiv = 9,
    Inverted = 10,
    OrReverse = 11,
    CopyInverted = 12,
    OrInverted = 13,
    Nand = 14,
    Set = 15,
}

/// Texture Filter
#[repr(u32)]
pub enum TextureFilter {
    Nearest = 0,
    Linear = 1,
    NearestMipmapNearest = 4,
    LinearMipmapNearest = 5,
    NearestMipmapLinear = 6,
    LinearMipmapLinear = 7,
}

/// Texture Map Mode
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum TextureMapMode {
    TextureCoords = 0,
    TextureMatrix = 1,
    EnvironmentMap = 2,
}

/// Texture Level Mode
#[repr(u32)]
pub enum TextureLevelMode {
    Auto = 0,
    Const = 1,
    Slope = 2,
}

/// Texture Projection Map Mode
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum TextureProjectionMapMode {
    Position = 0,
    Uv = 1,
    NormalizedNormal = 2,
    Normal = 3,
}

/// Wrap Mode
#[repr(u32)]
pub enum GuTexWrapMode {
    /// The texture repeats after crossing the border
    Repeat = 0,

    /// Texture clamps at the border
    Clamp = 1,
}

/// Front Face Direction
#[repr(u32)]
pub enum FrontFaceDirection {
    Clockwise = 0,
    CounterClockwise = 1,
}

/// Test function for alpha test
#[derive(Copy, Clone, Debug)]
#[repr(u32)]
pub enum AlphaFunc {
    Never = 0,
    Always,
    Equal,
    NotEqual,
    Less,
    LessOrEqual,
    Greater,
    GreaterOrEqual,
}

/// Test function for stencil test
#[derive(Copy, Clone, Debug)]
#[repr(u32)]
pub enum StencilFunc {
    Never = 0,
    Always,
    Equal,
    NotEqual,
    Less,
    LessOrEqual,
    Greater,
    GreaterOrEqual,
}

/// Test function for color test
#[derive(Copy, Clone, Debug)]
#[repr(u32)]
pub enum ColorFunc {
    Never = 0,
    Always,
    Equal,
    NotEqual,
}

/// Test function for depth test
#[derive(Copy, Clone, Debug)]
#[repr(u32)]
pub enum DepthFunc {
    /// No pixels pass the depth-test
    Never = 0,
    /// All pixels pass the depth-test
    Always,
    /// Pixels that match the depth-test pass
    Equal,
    /// Pixels that doesn't match the depth-test pass
    NotEqual,
    /// Pixels that are less in depth passes
    Less,
    /// Pixels that are less or equal in depth passes
    LessOrEqual,
    /// Pixels that are greater in depth passes
    Greater,
    /// Pixels that are greater or equal passes
    GreaterOrEqual,
}

bitflags::bitflags! {
    /// Clear Buffer Mask
    #[repr(transparent)]
    pub struct ClearBuffer: u32 {
        /// Clears the color buffer.
        const COLOR_BUFFER_BIT = 1;
        /// Clears the stencil buffer.
        const STENCIL_BUFFER_BIT = 2;
        /// Clears the depth buffer.
        const DEPTH_BUFFER_BIT = 4;
        /// Enables fast clearing. This divides the screen into 16 parts
        /// and clears them in parallel.
        const FAST_CLEAR_BIT = 16;
    }
}

/// Texture effect apply-modes.
///
/// Key for the apply-modes:
/// - `Cv`: Color value result
/// - `Ct`: Texture color
/// - `Cf`: Fragment color
/// - `Cc`: Constant color (specified by `sceGuTexEnvColor`)
///
/// The fields TCC_RGB and TCC_RGBA specify components that differ between
/// the two different component modes.
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum TextureEffect {
    // TODO: Better documentation
    /// The texture is multiplied with the current diffuse fragment.
    ///
    /// `Cv=Ct*Cf TCC_RGB: Av=Af TCC_RGBA: Av=At*Af`
    Modulate = 0,
    /// `TCC_RGB: Cv=Ct,Av=Af TCC_RGBA: Cv=Cf*(1-At)+Ct*At Av=Af`
    Decal = 1,
    /// `Cv=(Cf*(1-Ct))+(Cc*Ct) TCC_RGB: Av=Af TCC_RGBA: Av=At*Af`
    Blend = 2,
    /// The texture replaces the fragment
    ///
    /// `Cv=Ct TCC_RGB: Av=Af TCC_RGBA: Av=At`
    Replace = 3,
    /// The texture is added on-top of the diffuse fragment
    ///
    /// `Cv=Cf+Ct TCC_RGB: Av=Af TCC_RGBA: Av=At*Af`
    Add = 4,
}

/// Texture color component-modes.
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum TextureColorComponent {
    /// The texture alpha does not have any effect.
    Rgb = 0,
    /// The texture alpha is taken into account.
    Rgba = 1,
}

/// Mipmap Level
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum MipmapLevel {
    None = 0,
    Level1,
    Level2,
    Level3,
    Level4,
    Level5,
    Level6,
    Level7,
}

/// Blending Operation
///
/// Keys for the blending operations:
///
/// - `Cs`: Source color
/// - `Cd`: Destination color
/// - `Bs`: Blend function for source fragment
/// - `Bd`: Blend function for destination fragment
#[repr(u32)]
pub enum BlendOp {
    /// `(Cs*Bs) + (Cd*Bd)`
    Add = 0,
    /// `(Cs*Bs) - (Cd*Bd)`
    Subtract = 1,
    /// `(Cd*Bd) - (Cs*Bs)`
    ReverseSubtract = 2,
    /// `Cs < Cd ? Cs : Cd`
    Min = 3,
    /// `Cs < Cd ? Cd : Cs`
    Max = 4,
    /// `|Cs-Cd|`
    Abs = 5,
}

/// Blending factor
#[repr(u32)]
pub enum BlendFactor {
    Color = 0,
    OneMinusColor = 1,
    SrcAlpha = 2,
    OneMinusSrcAlpha = 3,
    DstAlpha = 4,
    OneMinusDstAlpha = 5,
    // TODO: There are likely 4 missing values here.
    // What are 6, 7, 8, 9? This can probably be determined with some experimentation.
    // They may also be reserved values.
    /// Use the fixed values provided in `sceGuBlendFunc`.
    Fix = 10,
}

/// Stencil Operations
#[repr(u32)]
pub enum StencilOperation {
    /// Keeps the current value
    Keep = 0,
    /// Sets the stencil buffer value to zero
    Zero = 1,
    /// Sets the stencil buffer value to ref, as specified by `sceGuStencilFunc`
    Replace = 2,
    /// Increments the current stencil buffer value
    Invert = 3,
    /// Decrease the current stencil buffer value
    Incr = 4,
    /// Bitwise invert the current stencil buffer value
    Decr = 5,
}

bitflags::bitflags!(
    /// Light Components
    #[repr(transparent)]
    pub struct LightComponent: i32 {
        const AMBIENT = 1;
        const DIFFUSE = 2;
        const SPECULAR = 4;

        // TODO: What is this?
        const UNKNOWN_LIGHT_COMPONENT = 8;
    }
);

/// Light modes
#[repr(u32)]
pub enum LightMode {
    SingleColor = 0,

    /// Separate specular colors are used to interpolate the specular component
    /// independently, so that it can be added to the fragment after the texture
    /// color.
    SeparateSpecularColor = 1,
}

/// Light Type
#[repr(u32)]
pub enum LightType {
    Directional = 0,
    Pointlight = 1,
    Spotlight = 2,
}

/// Contexts
#[repr(u32)]
#[derive(Copy, Clone, Debug)]
pub enum GuContextType {
    Direct = 0,
    Call = 1,
    Send = 2,
}

/// List Queue Mode
#[repr(u32)]
pub enum GuQueueMode {
    /// Place list last in the queue, so it executes in-order
    Tail = 0,
    /// Place list first in queue so that it executes as soon as possible
    Head = 1,
}

/// Sync mode
#[repr(u32)]
pub enum GuSyncMode {
    /// Wait until the last sceGuFinish command is reached.
    Finish = 0,
    /// Wait until the last (?) signal is executed.
    Signal = 1,
    /// Wait until all commands currently in list are executed.
    Done = 2,
    /// Wait for the currently executed display list (`GuContextType::Direct`).
    List = 3,
    /// Wait for the last send list.
    Send = 4,
}

/// Sync Behavior
#[repr(u32)]
pub enum GuSyncBehavior {
    /// Wait for the GE list to be completed.
    Wait = 0,
    /// Just peek at the current state.
    NoWait = 1,
}

/// GU Callback ID
#[repr(u32)]
pub enum GuCallbackId {
    /// Called when `sceGuSignal` is used.
    Signal = 1,

    /// Called when display list is finished.
    Finish = 4,
}

/// Signal behavior
#[repr(u32)]
pub enum SignalBehavior {
    /// Stops display list execution until callback function finished.
    Suspend = 1,
    /// Do not stop display list execution during callback.
    Continue = 2,
}

/// Map 8-bit color channels into one 32-bit value.
#[inline]
pub const fn abgr(a: u8, b: u8, g: u8, r: u8) -> u32 {
    (r as u32) | ((g as u32) << 8) | ((b as u32) << 16) | ((a as u32) << 24)
}

/// Map 8-bit color channels into one 32-bit value.
#[inline]
pub const fn argb(a: u8, r: u8, g: u8, b: u8) -> u32 {
    abgr(a, b, g, r)
}

/// Map 8-bit color channels into one 32-bit value.
#[inline]
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> u32 {
    argb(a, r, g, b)
}

#[inline]
/// Map floating point channels (0..1) into one 32-bit value
pub fn color(r: f32, g: f32, b: f32, a: f32) -> u32 {
    rgba(
        (r * 255.0) as u8,
        (g * 255.0) as u8,
        (b * 255.0) as u8,
        (a * 255.0) as u8,
    )
}

pub type GuCallback = Option<extern "C" fn(id: i32, arg: *mut c_void)>;
pub type GuSwapBuffersCallback =
    Option<extern "C" fn(display: *mut *mut c_void, render: *mut *mut c_void)>;

struct Settings {
    sig: GuCallback,
    fin: GuCallback,
    signal_history: [i16; 16],
    signal_offset: u32,
    kernel_event_flag: SceUid,
    ge_callback_id: i32,
    swap_buffers_callback: GuSwapBuffersCallback,
    swap_buffers_behaviour: crate::sys::DisplaySetBufSync,
}

struct GuDisplayList {
    start: *mut u32,
    current: *mut u32,
    parent_context: GuContextType,
}

struct GuContext {
    list: GuDisplayList,
    scissor_enable: i32,
    scissor_start: [i32; 2],
    scissor_end: [i32; 2],
    near_plane: i32,
    far_plane: i32,
    depth_offset: i32,
    fragment_2x: i32,
    texture_function: i32,
    texture_proj_map_mode: TextureProjectionMapMode,
    texture_map_mode: TextureMapMode,
    sprite_mode: [i32; 4],
    clear_color: u32,
    clear_stencil: u32,
    clear_depth: u32,
    texture_mode: TexturePixelFormat,
}

struct GuDrawBuffer {
    pixel_size: DisplayPixelFormat,
    frame_width: i32,
    frame_buffer: *mut c_void,
    disp_buffer: *mut c_void,
    depth_buffer: *mut c_void,
    depth_width: i32,
    width: i32,
    height: i32,
}

struct GuLightSettings {
    /// Light type
    type_: GeCommand,
    /// X position
    xpos: GeCommand,
    /// Y position
    ypos: GeCommand,
    /// Z position
    zpos: GeCommand,
    /// X direction
    xdir: GeCommand,
    /// Y direction
    ydir: GeCommand,
    /// Z direction
    zdir: GeCommand,

    /// Ambient color
    ambient: GeCommand,
    /// Diffuse color
    diffuse: GeCommand,
    /// Specular color
    specular: GeCommand,
    /// Constant attenuation
    constant: GeCommand,
    /// Linear attenuation
    linear: GeCommand,
    /// Quadratic attenuation
    quadratic: GeCommand,
    /// Light exponent
    exponent: GeCommand,
    /// Light cutoff
    cutoff: GeCommand,
}

static mut CURRENT_FRAME: u32 = 0;
static mut CONTEXTS: [GuContext; 3] = [
    GuContext {
        list: GuDisplayList {
            start: null_mut(),
            current: null_mut(),
            parent_context: GuContextType::Direct,
        },
        scissor_enable: 0,
        scissor_start: [0, 0],
        scissor_end: [0, 0],
        near_plane: 0,
        far_plane: 0,
        depth_offset: 0,
        fragment_2x: 0,
        texture_function: 0,
        texture_proj_map_mode: TextureProjectionMapMode::Position,
        texture_map_mode: TextureMapMode::TextureCoords,
        sprite_mode: [0, 0, 0, 0],
        clear_color: 0,
        clear_stencil: 0,
        clear_depth: 0,
        texture_mode: TexturePixelFormat::Psm5650,
    },
    GuContext {
        list: GuDisplayList {
            start: null_mut(),
            current: null_mut(),
            parent_context: GuContextType::Direct,
        },
        scissor_enable: 0,
        scissor_start: [0, 0],
        scissor_end: [0, 0],
        near_plane: 0,
        far_plane: 0,
        depth_offset: 0,
        fragment_2x: 0,
        texture_function: 0,
        texture_proj_map_mode: TextureProjectionMapMode::Position,
        texture_map_mode: TextureMapMode::TextureCoords,
        sprite_mode: [0, 0, 0, 0],
        clear_color: 0,
        clear_stencil: 0,
        clear_depth: 0,
        texture_mode: TexturePixelFormat::Psm5650,
    },
    GuContext {
        list: GuDisplayList {
            start: null_mut(),
            current: null_mut(),
            parent_context: GuContextType::Direct,
        },
        scissor_enable: 0,
        scissor_start: [0, 0],
        scissor_end: [0, 0],
        near_plane: 0,
        far_plane: 0,
        depth_offset: 0,
        fragment_2x: 0,
        texture_function: 0,
        texture_proj_map_mode: TextureProjectionMapMode::Position,
        texture_map_mode: TextureMapMode::TextureCoords,
        sprite_mode: [0, 0, 0, 0],
        clear_color: 0,
        clear_stencil: 0,
        clear_depth: 0,
        texture_mode: TexturePixelFormat::Psm5650,
    },
];

static mut GE_LIST_EXECUTED: [i32; 2] = [0, 0];
static mut GE_EDRAM_ADDRESS: *mut c_void = null_mut();

static mut SETTINGS: Settings = Settings {
    sig: None,
    fin: None,
    signal_history: [0; 16],
    signal_offset: 0,

    // Invalid UID until initialized.
    kernel_event_flag: SceUid(-1),

    ge_callback_id: 0,
    swap_buffers_behaviour: crate::sys::DisplaySetBufSync::Immediate,
    swap_buffers_callback: None,
};

static mut LIST: *mut GuDisplayList = null_mut();
static mut CURR_CONTEXT: GuContextType = GuContextType::Direct;
static mut INIT: i32 = 0;
static mut DISPLAY_ON: bool = false;
static mut CALL_MODE: i32 = 0;
static mut STATES: u32 = 0;

static mut DRAW_BUFFER: GuDrawBuffer = GuDrawBuffer {
    depth_buffer: null_mut(),
    frame_buffer: null_mut(),
    disp_buffer: null_mut(),
    width: 0,
    height: 0,
    depth_width: 0,
    frame_width: 0,
    pixel_size: DisplayPixelFormat::Psm5650,
};

static mut OBJECT_STACK: *mut *mut u32 = null_mut();
static mut OBJECT_STACK_DEPTH: i32 = 0;

const LIGHT_COMMANDS: [GuLightSettings; 4] = [
    GuLightSettings {
        type_: GeCommand::LightType0,
        xpos: GeCommand::Light0X,
        ypos: GeCommand::Light0Y,
        zpos: GeCommand::Light0Z,
        xdir: GeCommand::Light0DirectionX,
        ydir: GeCommand::Light0DirectionY,
        zdir: GeCommand::Light0DirectionZ,
        ambient: GeCommand::Light0Ambient,
        diffuse: GeCommand::Light0Diffuse,
        specular: GeCommand::Light0Specular,
        constant: GeCommand::Light0ConstantAtten,
        linear: GeCommand::Light0LinearAtten,
        quadratic: GeCommand::Light0QuadtraticAtten,
        exponent: GeCommand::Light0ExponentAtten,
        cutoff: GeCommand::Light0CutoffAtten,
    },
    GuLightSettings {
        type_: GeCommand::LightType1,
        xpos: GeCommand::Light1X,
        ypos: GeCommand::Light1Y,
        zpos: GeCommand::Light1Z,
        xdir: GeCommand::Light1DirectionX,
        ydir: GeCommand::Light1DirectionY,
        zdir: GeCommand::Light1DirectionZ,
        ambient: GeCommand::Light1Ambient,
        diffuse: GeCommand::Light1Diffuse,
        specular: GeCommand::Light1Specular,
        constant: GeCommand::Light1ConstantAtten,
        linear: GeCommand::Light1LinearAtten,
        quadratic: GeCommand::Light1QuadtraticAtten,
        exponent: GeCommand::Light1ExponentAtten,
        cutoff: GeCommand::Light1CutoffAtten,
    },
    GuLightSettings {
        type_: GeCommand::LightType2,
        xpos: GeCommand::Light2X,
        ypos: GeCommand::Light2Y,
        zpos: GeCommand::Light2Z,
        xdir: GeCommand::Light2DirectionX,
        ydir: GeCommand::Light2DirectionY,
        zdir: GeCommand::Light2DirectionZ,
        ambient: GeCommand::Light2Ambient,
        diffuse: GeCommand::Light2Diffuse,
        specular: GeCommand::Light2Specular,
        constant: GeCommand::Light2ConstantAtten,
        linear: GeCommand::Light2LinearAtten,
        quadratic: GeCommand::Light2QuadtraticAtten,
        exponent: GeCommand::Light2ExponentAtten,
        cutoff: GeCommand::Light2CutoffAtten,
    },
    GuLightSettings {
        type_: GeCommand::LightType3,
        xpos: GeCommand::Light3X,
        ypos: GeCommand::Light3Y,
        zpos: GeCommand::Light3Z,
        xdir: GeCommand::Light3DirectionX,
        ydir: GeCommand::Light3DirectionY,
        zdir: GeCommand::Light3DirectionZ,
        ambient: GeCommand::Light3Ambient,
        diffuse: GeCommand::Light3Diffuse,
        specular: GeCommand::Light3Specular,
        constant: GeCommand::Light3ConstantAtten,
        linear: GeCommand::Light3LinearAtten,
        quadratic: GeCommand::Light3QuadtraticAtten,
        exponent: GeCommand::Light3ExponentAtten,
        cutoff: GeCommand::Light3CutoffAtten,
    },
];

#[inline]
unsafe fn send_command_i(cmd: GeCommand, argument: i32) {
    (*(*LIST).current) = ((cmd as u32) << 24) | (argument as u32 & 0xffffff);
    (*LIST).current = (*LIST).current.add(1);
}

#[inline]
unsafe fn send_command_f(cmd: GeCommand, argument: f32) {
    send_command_i(cmd, (argument.to_bits() >> 8) as i32);
}

#[inline]
unsafe fn send_command_i_stall(cmd: GeCommand, argument: i32) {
    send_command_i(cmd, argument);
    if let (GuContextType::Direct, 0) = (CURR_CONTEXT, OBJECT_STACK_DEPTH) {
        crate::sys::sceGeListUpdateStallAddr(GE_LIST_EXECUTED[0], (*LIST).current as *mut c_void);
    }
}

unsafe fn draw_region(x: i32, y: i32, width: i32, height: i32) {
    send_command_i(GeCommand::Region1, (y << 10) | x);
    send_command_i(
        GeCommand::Region2,
        (((y + height) - 1) << 10) | ((x + width) - 1),
    );
}

unsafe fn reset_values() {
    INIT = 0;
    STATES = 0;
    CURRENT_FRAME = 0;
    OBJECT_STACK_DEPTH = 0;
    DISPLAY_ON = false;
    CALL_MODE = 0;
    DRAW_BUFFER.pixel_size = DisplayPixelFormat::Psm5551;
    DRAW_BUFFER.frame_width = 0;
    DRAW_BUFFER.frame_buffer = null_mut();
    DRAW_BUFFER.disp_buffer = null_mut();
    DRAW_BUFFER.depth_buffer = null_mut();
    DRAW_BUFFER.depth_width = 0;
    DRAW_BUFFER.width = 480;
    DRAW_BUFFER.height = 272;

    for i in 0..CONTEXTS.len() {
        let context = addr_of_mut!(CONTEXTS[i]);
        (*context).scissor_enable = 0;
        (*context).scissor_start = [0, 0];
        (*context).scissor_end = [0, 0];

        (*context).near_plane = 0;
        (*context).far_plane = 1;

        (*context).depth_offset = 0;
        (*context).fragment_2x = 0;
        (*context).texture_function = 0;
        (*context).texture_proj_map_mode = TextureProjectionMapMode::Position;
        (*context).texture_map_mode = TextureMapMode::TextureCoords;
        (*context).sprite_mode[0] = 0;
        (*context).sprite_mode[1] = 0;
        (*context).sprite_mode[2] = 0;
        (*context).sprite_mode[3] = 0;
        (*context).clear_color = 0;
        (*context).clear_stencil = 0;
        (*context).clear_depth = 0xffff;
        (*context).texture_mode = TexturePixelFormat::Psm5650;
    }

    SETTINGS.sig = None;
    SETTINGS.fin = None;
}

extern "C" fn callback_sig(id: i32, arg: *mut c_void) {
    let settings = arg as *mut Settings;

    unsafe {
        let idx = ((*settings).signal_offset & 15) as usize;
        (*settings).signal_history[idx] = (id & 0xffff) as i16;
        (*settings).signal_offset += 1;

        if (*settings).sig.is_some() {
            // Convert Option<fn(i32, *mut c_void)> -> fn(i32)
            // This is fine because we are transmuting a nullable function
            // pointer to another function pointer. The requirement here is that
            // it must not be null.
            let f = mem::transmute::<_, extern "C" fn(i32)>((*settings).sig);

            f(id & 0xffff);
        }

        crate::sys::sceKernelSetEventFlag((*settings).kernel_event_flag, 1);
    }
}

extern "C" fn callback_fin(id: i32, arg: *mut c_void) {
    unsafe {
        let settings = arg as *mut Settings;

        if let Some(fin) = (*settings).fin {
            // Convert Option<fn(i32, *mut c_void)> -> fn(i32)
            // This is fine because we are transmuting a nullable function
            // pointer to another function pointer. The requirement here is that
            // it must not be null.
            let f = core::mem::transmute::<_, extern "C" fn(i32)>(fin);

            f(id & 0xffff)
        }
    }
}

/// Set depth buffer parameters
///
/// # Parameters
///
/// - `zbp`: VRAM pointer where the depth buffer should start
/// - `zbw`: The width of the depth buffer (block-aligned)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDepthBuffer(zbp: *mut c_void, zbw: i32) {
    DRAW_BUFFER.depth_buffer = zbp;

    if DRAW_BUFFER.depth_width == 0 || DRAW_BUFFER.depth_width != zbw {
        DRAW_BUFFER.depth_width = zbw;
    }

    send_command_i(GeCommand::ZBufPtr, zbp as i32 & 0xffffff);
    send_command_i(
        GeCommand::ZBufWidth,
        (((zbp as u32 & 0xff000000) >> 8) | zbw as u32) as i32,
    );
}

/// Set display buffer parameters
///
/// # Parameters
///
/// - `width`: Width of the display buffer in pixels
/// - `height`: Width of the display buffer in pixels
/// - `dispbp`: VRAM pointer to where the display-buffer starts
/// - `dispbw`: Display buffer width (block aligned)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDispBuffer(
    width: i32,
    height: i32,
    dispbp: *mut c_void,
    dispbw: i32,
) {
    use crate::sys::DisplaySetBufSync;

    DRAW_BUFFER.width = width;
    DRAW_BUFFER.height = height;
    DRAW_BUFFER.disp_buffer = dispbp;

    if DRAW_BUFFER.frame_width == 0 || DRAW_BUFFER.frame_width != dispbw {
        DRAW_BUFFER.frame_width = dispbw;
    }

    draw_region(0, 0, DRAW_BUFFER.width, DRAW_BUFFER.height);

    crate::sys::sceDisplaySetMode(
        crate::sys::DisplayMode::Lcd,
        DRAW_BUFFER.width as usize,
        DRAW_BUFFER.height as usize,
    );

    if DISPLAY_ON {
        crate::sys::sceDisplaySetFrameBuf(
            (GE_EDRAM_ADDRESS as *mut u8).add(DRAW_BUFFER.disp_buffer as usize),
            dispbw as usize,
            DRAW_BUFFER.pixel_size,
            DisplaySetBufSync::NextFrame,
        );
    }
}

/// Set draw buffer parameters (and store in context for buffer-swap)
///
/// # Parameters
///
/// - `psm`: Pixel format to use for rendering (and display)
/// - `fbp`: VRAM pointer to where the draw buffer starts
/// - `fbw`: Frame buffer width (block aligned)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDrawBuffer(psm: DisplayPixelFormat, fbp: *mut c_void, fbw: i32) {
    DRAW_BUFFER.pixel_size = psm;
    DRAW_BUFFER.frame_width = fbw;
    DRAW_BUFFER.frame_buffer = fbp;

    if DRAW_BUFFER.depth_buffer.is_null() && DRAW_BUFFER.height != 0 {
        DRAW_BUFFER.depth_buffer =
            (fbp as u32 + (((DRAW_BUFFER.height * fbw) as u32) << 2u32)) as *mut c_void;
    }

    if DRAW_BUFFER.depth_width == 0 {
        DRAW_BUFFER.depth_width = fbw;
    }

    send_command_i(GeCommand::FramebufPixFormat, psm as i32);
    send_command_i(
        GeCommand::FrameBufPtr,
        DRAW_BUFFER.frame_buffer as i32 & 0xffffff,
    );
    send_command_i(
        GeCommand::FrameBufWidth,
        ((DRAW_BUFFER.frame_buffer as u32 & 0xff000000) >> 8) as i32 | DRAW_BUFFER.frame_width,
    );
    send_command_i(
        GeCommand::ZBufPtr,
        DRAW_BUFFER.depth_buffer as i32 & 0xffffff,
    );
    send_command_i(
        GeCommand::ZBufWidth,
        ((DRAW_BUFFER.depth_buffer as u32 & 0xff000000) >> 8) as i32 | DRAW_BUFFER.depth_width,
    );
}

/// Set draw buffer directly, not storing parameters in the context
///
/// # Parameters
///
/// - `psm`: Pixel format to use for rendering
/// - `fbp`: VRAM pointer to where the draw buffer starts
/// - `fbw`: Frame buffer width (block aligned)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDrawBufferList(psm: DisplayPixelFormat, fbp: *mut c_void, fbw: i32) {
    send_command_i(GeCommand::FramebufPixFormat, psm as i32);
    send_command_i(GeCommand::FrameBufPtr, fbp as i32 & 0xffffff);
    send_command_i(
        GeCommand::FrameBufWidth,
        ((fbp as u32 & 0xff000000) >> 8) as i32 | fbw,
    );
}

/// Turn display on or off
///
/// # Parameters
///
/// - `state`: Turn display on or off
///
/// # Return Value
///
/// State of the display prior to this call
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDisplay(state: bool) -> bool {
    use crate::sys::DisplaySetBufSync;

    if state {
        crate::sys::sceDisplaySetFrameBuf(
            (GE_EDRAM_ADDRESS as *mut u8).add(DRAW_BUFFER.disp_buffer as usize),
            DRAW_BUFFER.frame_width as usize,
            DRAW_BUFFER.pixel_size,
            DisplaySetBufSync::NextFrame,
        );
    } else {
        crate::sys::sceDisplaySetFrameBuf(
            null_mut(),
            0,
            DisplayPixelFormat::Psm5650,
            DisplaySetBufSync::NextFrame,
        );
    }

    DISPLAY_ON = state;
    state
}

/// Select which depth-test function to use
///
/// # Parameters
///
/// - `function`: Depth test function to use
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDepthFunc(function: DepthFunc) {
    send_command_i(GeCommand::ZTest, function as i32);
}

/// Mask depth buffer writes
///
/// # Parameters
///
/// - `mask`: `1` to disable Z writes, `0` to enable
// TODO: Use bool instead?
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDepthMask(mask: i32) {
    send_command_i(GeCommand::ZWriteDisable, mask);
}

#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDepthOffset(offset: i32) {
    let context = &mut CONTEXTS[CURR_CONTEXT as usize];
    context.depth_offset = offset;
    sceGuDepthRange(context.near_plane, context.far_plane);
}

/// Set which range to use for depth calculations.
///
/// # Note
///
/// The depth buffer is inversed, and takes values from 65535 to 0.
///
/// # Parameters
///
/// - `near`: Value to use for the near plane
/// - `far`: Value to use for the far plane
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDepthRange(mut near: i32, mut far: i32) {
    let context = &mut CONTEXTS[CURR_CONTEXT as usize];
    let max = near as u32 + far as u32;
    let val = ((max >> 31) + max) as i32;
    let z = (val >> 1) as f32;

    context.near_plane = near;
    context.far_plane = far;

    send_command_f(GeCommand::ViewportZScale, z - near as f32);
    send_command_f(GeCommand::ViewportZCenter, z + context.depth_offset as f32);

    if near > far {
        mem::swap(&mut near, &mut far);
    }

    send_command_i(GeCommand::MinZ, near);
    send_command_i(GeCommand::MaxZ, far);
}

#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuFog(near: f32, far: f32, color: u32) {
    let mut distance = far - near;

    if distance != 0.0 {
        distance = 1.0 / distance;
    }

    send_command_i(GeCommand::FogColor, (color & 0xffffff) as i32);
    send_command_f(GeCommand::Fog1, far);
    send_command_f(GeCommand::Fog2, distance);
}

/// Initalize the GU system
///
/// This function MUST be called as the first function, otherwise state is undetermined.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuInit() {
    const INIT_COMMANDS: [GeCommand; 223] = [
        GeCommand::Vaddr,
        GeCommand::Iaddr,
        GeCommand::Base,
        GeCommand::VertexType,
        GeCommand::OffsetAddr,
        GeCommand::Region1,
        GeCommand::Region2,
        GeCommand::LightingEnable,
        GeCommand::LightEnable0,
        GeCommand::LightEnable1,
        GeCommand::LightEnable2,
        GeCommand::LightEnable3,
        GeCommand::DepthClampEnable,
        GeCommand::CullFaceEnable,
        GeCommand::TextureMapEnable,
        GeCommand::FogEnable,
        GeCommand::DitherEnable,
        GeCommand::AlphaBlendEnable,
        GeCommand::AlphaTestEnable,
        GeCommand::ZTestEnable,
        GeCommand::StencilTestEnable,
        GeCommand::AntiAliasEnable,
        GeCommand::PatchCullEnable,
        GeCommand::ColorTestEnable,
        GeCommand::LogicOpEnable,
        GeCommand::BoneMatrixNumber,
        GeCommand::BoneMatrixData,
        GeCommand::MorphWeight0,
        GeCommand::MorphWeight1,
        GeCommand::MorphWeight2,
        GeCommand::MorphWeight3,
        GeCommand::MorphWeight4,
        GeCommand::MorphWeight5,
        GeCommand::MorphWeight6,
        GeCommand::MorphWeight7,
        GeCommand::PatchDivision,
        GeCommand::PatchPrimitive,
        GeCommand::PatchFacing,
        GeCommand::WorldMatrixNumber,
        GeCommand::WorldMatrixData,
        GeCommand::ViewMatrixNumber,
        GeCommand::ViewMatrixData,
        GeCommand::ProjMatrixNumber,
        GeCommand::ProjMatrixData,
        GeCommand::TGenMatrixNumber,
        GeCommand::TGenMatrixData,
        GeCommand::ViewportXScale,
        GeCommand::ViewportYScale,
        GeCommand::ViewportZScale,
        GeCommand::ViewportXCenter,
        GeCommand::ViewportYCenter,
        GeCommand::ViewportZCenter,
        GeCommand::TexScaleU,
        GeCommand::TexScaleV,
        GeCommand::TexOffsetU,
        GeCommand::TexOffsetV,
        GeCommand::OffsetX,
        GeCommand::OffsetY,
        GeCommand::ShadeMode,
        GeCommand::ReverseNormal,
        GeCommand::MaterialUpdate,
        GeCommand::MaterialEmissive,
        GeCommand::MaterialAmbient,
        GeCommand::MaterialDiffuse,
        GeCommand::MaterialSpecular,
        GeCommand::MaterialAlpha,
        GeCommand::MaterialSpecularCoef,
        GeCommand::AmbientColor,
        GeCommand::AmbientAlpha,
        GeCommand::LightMode,
        GeCommand::LightType0,
        GeCommand::LightType1,
        GeCommand::LightType2,
        GeCommand::LightType3,
        GeCommand::Light0X,
        GeCommand::Light0Y,
        GeCommand::Light0Z,
        GeCommand::Light1X,
        GeCommand::Light1Y,
        GeCommand::Light1Z,
        GeCommand::Light2X,
        GeCommand::Light2Y,
        GeCommand::Light2Z,
        GeCommand::Light3X,
        GeCommand::Light3Y,
        GeCommand::Light3Z,
        GeCommand::Light0DirectionX,
        GeCommand::Light0DirectionY,
        GeCommand::Light0DirectionZ,
        GeCommand::Light1DirectionX,
        GeCommand::Light1DirectionY,
        GeCommand::Light1DirectionZ,
        GeCommand::Light2DirectionX,
        GeCommand::Light2DirectionY,
        GeCommand::Light2DirectionZ,
        GeCommand::Light3DirectionX,
        GeCommand::Light3DirectionY,
        GeCommand::Light3DirectionZ,
        GeCommand::Light0ConstantAtten,
        GeCommand::Light0LinearAtten,
        GeCommand::Light0QuadtraticAtten,
        GeCommand::Light1ConstantAtten,
        GeCommand::Light1LinearAtten,
        GeCommand::Light1QuadtraticAtten,
        GeCommand::Light2ConstantAtten,
        GeCommand::Light2LinearAtten,
        GeCommand::Light2QuadtraticAtten,
        GeCommand::Light3ConstantAtten,
        GeCommand::Light3LinearAtten,
        GeCommand::Light3QuadtraticAtten,
        GeCommand::Light0ExponentAtten,
        GeCommand::Light1ExponentAtten,
        GeCommand::Light2ExponentAtten,
        GeCommand::Light3ExponentAtten,
        GeCommand::Light0CutoffAtten,
        GeCommand::Light1CutoffAtten,
        GeCommand::Light2CutoffAtten,
        GeCommand::Light3CutoffAtten,
        GeCommand::Light0Ambient,
        GeCommand::Light0Diffuse,
        GeCommand::Light0Specular,
        GeCommand::Light1Ambient,
        GeCommand::Light1Diffuse,
        GeCommand::Light1Specular,
        GeCommand::Light2Ambient,
        GeCommand::Light2Diffuse,
        GeCommand::Light2Specular,
        GeCommand::Light3Ambient,
        GeCommand::Light3Diffuse,
        GeCommand::Light3Specular,
        GeCommand::Cull,
        GeCommand::FrameBufPtr,
        GeCommand::FrameBufWidth,
        GeCommand::ZBufPtr,
        GeCommand::ZBufWidth,
        GeCommand::TexAddr0,
        GeCommand::TexAddr1,
        GeCommand::TexAddr2,
        GeCommand::TexAddr3,
        GeCommand::TexAddr4,
        GeCommand::TexAddr5,
        GeCommand::TexAddr6,
        GeCommand::TexAddr7,
        GeCommand::TexBufWidth0,
        GeCommand::TexBufWidth1,
        GeCommand::TexBufWidth2,
        GeCommand::TexBufWidth3,
        GeCommand::TexBufWidth4,
        GeCommand::TexBufWidth5,
        GeCommand::TexBufWidth6,
        GeCommand::TexBufWidth7,
        GeCommand::ClutAddr,
        GeCommand::ClutAddrUpper,
        GeCommand::TransferSrc,
        GeCommand::TransferSrcW,
        GeCommand::TransferDst,
        GeCommand::TransferDstW,
        GeCommand::TexSize0,
        GeCommand::TexSize1,
        GeCommand::TexSize2,
        GeCommand::TexSize3,
        GeCommand::TexSize4,
        GeCommand::TexSize5,
        GeCommand::TexSize6,
        GeCommand::TexSize7,
        GeCommand::TexMapMode,
        GeCommand::TexShadeLs,
        GeCommand::TexMode,
        GeCommand::TexFormat,
        GeCommand::LoadClut,
        GeCommand::ClutFormat,
        GeCommand::TexFilter,
        GeCommand::TexWrap,
        GeCommand::TexLevel,
        GeCommand::TexFunc,
        GeCommand::TexEnvColor,
        GeCommand::TexFlush,
        GeCommand::TexSync,
        GeCommand::Fog1,
        GeCommand::Fog2,
        GeCommand::FogColor,
        GeCommand::TexLodSlope,
        GeCommand::FramebufPixFormat,
        GeCommand::ClearMode,
        GeCommand::Scissor1,
        GeCommand::Scissor2,
        GeCommand::MinZ,
        GeCommand::MaxZ,
        GeCommand::ColorTest,
        GeCommand::ColorRef,
        GeCommand::ColorTestmask,
        GeCommand::AlphaTest,
        GeCommand::StencilTest,
        GeCommand::StencilOp,
        GeCommand::ZTest,
        GeCommand::BlendMode,
        GeCommand::BlendFixedA,
        GeCommand::BlendFixedB,
        GeCommand::Dith0,
        GeCommand::Dith1,
        GeCommand::Dith2,
        GeCommand::Dith3,
        GeCommand::LogicOp,
        GeCommand::ZWriteDisable,
        GeCommand::MaskRgb,
        GeCommand::MaskAlpha,
        GeCommand::TransferSrcPos,
        GeCommand::TransferDstPos,
        GeCommand::TransferSize,
        GeCommand::Vscx,
        GeCommand::Vscy,
        GeCommand::Vscz,
        GeCommand::Vtcs,
        GeCommand::Vtct,
        GeCommand::Vtcq,
        GeCommand::Vcv,
        GeCommand::Vap,
        GeCommand::Vfc,
        GeCommand::Vscv,
        GeCommand::Finish,
        GeCommand::End,
        GeCommand::Nop,
        GeCommand::Nop,
    ];

    static INIT_LIST: crate::Align16<[u32; 223]> = crate::Align16({
        let mut out = [0; 223];

        let mut i = 0;
        while i < 223 {
            out[i] = (INIT_COMMANDS[i] as u32) << 24;
            i += 1;
        }

        out
    });

    let mut callback = crate::sys::GeCallbackData {
        signal_func: Some(callback_sig),
        signal_arg: addr_of_mut!(SETTINGS).cast::<c_void>(),
        finish_func: Some(callback_fin),
        finish_arg: addr_of_mut!(SETTINGS).cast::<c_void>(),
    };

    SETTINGS.ge_callback_id = crate::sys::sceGeSetCallback(&mut callback);
    SETTINGS.swap_buffers_callback = None;
    SETTINGS.swap_buffers_behaviour = super::display::DisplaySetBufSync::Immediate;

    GE_EDRAM_ADDRESS = sys::sceGeEdramGetAddr().cast::<c_void>();

    GE_LIST_EXECUTED[0] = sys::sceGeListEnQueue(
        (&INIT_LIST as *const _ as u32 & 0x1fffffff) as *const _,
        core::ptr::null_mut(),
        SETTINGS.ge_callback_id,
        core::ptr::null_mut(),
    );

    reset_values();

    SETTINGS.kernel_event_flag = super::kernel::sceKernelCreateEventFlag(
        b"SceGuSignal\0" as *const u8,
        super::kernel::EventFlagAttributes::WAIT_MULTIPLE,
        3,
        null_mut(),
    );

    sys::sceGeListSync(GE_LIST_EXECUTED[0], 0);
}

/// Shutdown the GU system
///
/// Called when GU is no longer needed
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTerm() {
    sys::sceKernelDeleteEventFlag(SETTINGS.kernel_event_flag);
    sys::sceGeUnsetCallback(SETTINGS.ge_callback_id);
}

/// # Parameters
///
/// - `mode`: If set to 1, reset all the queues.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuBreak(mode: i32) {
    static mut UNUSED_BREAK: GeBreakParam = GeBreakParam { buf: [0; 4] };

    sys::sceGeBreak(mode, addr_of_mut!(UNUSED_BREAK));
}

#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuContinue() {
    // Return this?
    sys::sceGeContinue();
}

// FIXME: This documentation is confusing.
/// Setup signal handler
///
/// # Parameters
///
/// - `signal`: Signal index to install a handler for
/// - `callback`: Callback to call when signal index is triggered
///
/// # Return Value
///
/// The old callback handler
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSetCallback(
    signal: GuCallbackId,
    callback: GuCallback,
) -> GuCallback {
    let old_callback;

    match signal {
        GuCallbackId::Signal => {
            old_callback = SETTINGS.sig;
            SETTINGS.sig = callback;
        }

        GuCallbackId::Finish => {
            old_callback = SETTINGS.fin;
            SETTINGS.fin = callback;
        }
    }

    old_callback
}

// FIXME: This documentation is confusing.
/// Trigger signal to call code from the command stream
///
/// # Parameters
///
/// - `behavior`: Behavior type
/// - `signal`: Signal to trigger
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSignal(behavior: SignalBehavior, signal: i32) {
    send_command_i(
        GeCommand::Signal,
        ((signal & 0xff) << 16) | (behavior as i32 & 0xffff),
    );
    send_command_i(GeCommand::End, 0);

    if signal == 3 {
        send_command_i(GeCommand::Finish, 0);
        send_command_i(GeCommand::End, 0);
    }

    send_command_i_stall(GeCommand::Nop, 0);
}

/// Send raw float command to the GE
///
/// The argument is converted into a 24-bit float before transfer.
///
/// # Parameters
///
/// - `cmd`: Which command to send
/// - `argument`: Argument to pass along
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSendCommandf(cmd: GeCommand, argument: f32) {
    send_command_f(cmd, argument);
}

/// Send raw command to the GE
///
/// Only the 24 lower bits of the argument are passed along.
///
/// # Parameters
///
/// - `cmd`: Which command to send
/// - `argument`: Argument to pass along
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSendCommandi(cmd: GeCommand, argument: i32) {
    send_command_i(cmd, argument);
}

/// Allocate memory on the current display list for temporary storage
///
/// # Note
///
/// This function is NOT for permanent memory allocation, the memory will be
/// invalid as soon as you start filling the same display list again.
///
/// # Parameters
///
/// - `size`: How much memory to allocate
///
/// # Return Value
///
/// Memory-block ready for use
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuGetMemory(mut size: i32) -> *mut c_void {
    // Some kind of 4-byte alignment?
    size += 3;
    size += (((size >> 31) as u32) >> 30) as i32;
    size = (size >> 2) << 2;

    let orig_ptr = (*LIST).current;
    let new_ptr = (orig_ptr as usize + size as usize + 8) as *mut u32;

    let lo = (8 << 24) | (new_ptr as i32 & 0xffffff);
    let hi = ((16 << 24) | ((new_ptr as u32 >> 8) & 0xf0000)) as i32;

    *orig_ptr = hi as u32;
    *orig_ptr.offset(1) = lo as u32;

    (*LIST).current = new_ptr;

    if let GuContextType::Direct = CURR_CONTEXT {
        crate::sys::sceGeListUpdateStallAddr(GE_LIST_EXECUTED[0], new_ptr.cast::<c_void>());
    }

    orig_ptr.add(2).cast::<c_void>()
}

/// Start filling a new display-context
///
/// The previous context-type is stored so that it can be restored at `sceGuFinish`.
///
/// # Parameters
///
/// - `cid`: Context Type
/// - `list`: Pointer to display-list (16 byte aligned)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuStart(context_type: GuContextType, list: *mut c_void) {
    let context = &mut CONTEXTS[context_type as usize];
    let local_list = ((list as u32) | 0x4000_0000) as *mut u32;

    // setup display list
    context.list.start = local_list;
    context.list.current = local_list;
    context.list.parent_context = CURR_CONTEXT;
    LIST = &mut context.list;

    // store current context
    CURR_CONTEXT = context_type;

    if let GuContextType::Direct = context_type {
        GE_LIST_EXECUTED[0] = crate::sys::sceGeListEnQueue(
            local_list as *mut c_void,
            local_list as *mut c_void,
            SETTINGS.ge_callback_id,
            core::ptr::null_mut(),
        );

        SETTINGS.signal_offset = 0;
    }

    if INIT == 0 {
        static DITHER_MATRIX: ScePspIMatrix4 = ScePspIMatrix4 {
            x: ScePspIVector4 {
                x: -4,
                y: 0,
                z: -3,
                w: 1,
            },
            y: ScePspIVector4 {
                x: 2,
                y: -2,
                z: 3,
                w: -1,
            },
            z: ScePspIVector4 {
                x: -3,
                y: 1,
                z: -4,
                w: 0,
            },
            w: ScePspIVector4 {
                x: 3,
                y: -1,
                z: 2,
                w: -2,
            },
        };

        sceGuSetDither(&DITHER_MATRIX);
        sceGuPatchDivide(16, 16);
        sceGuColorMaterial(
            LightComponent::AMBIENT | LightComponent::DIFFUSE | LightComponent::SPECULAR,
        );

        sceGuSpecular(1.0);
        sceGuTexScale(1.0, 1.0);

        INIT = 1;
    }

    if let GuContextType::Direct = CURR_CONTEXT {
        if DRAW_BUFFER.frame_width != 0 {
            send_command_i(
                GeCommand::FrameBufPtr,
                DRAW_BUFFER.frame_buffer as i32 & 0xffffff,
            );
            send_command_i(
                GeCommand::FrameBufWidth,
                ((DRAW_BUFFER.frame_buffer as u32 & 0xff00_0000) >> 8) as i32
                    | DRAW_BUFFER.frame_width,
            );
        }
    }
}

/// Finish current display list and go back to the parent context
///
/// If the context is `Direct`, the stall-address is updated so that the entire
/// list will execute. Otherwise, only the terminating action is written to the
/// list, depending on the context type.
///
/// The finish-callback will get a zero as argument when using this function.
///
/// This also restores control back to whatever context that was active prior to
/// this call.
///
/// # Return Value
///
/// Size of finished display list
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuFinish() -> i32 {
    match CURR_CONTEXT {
        GuContextType::Direct | GuContextType::Send => {
            send_command_i(GeCommand::Finish, 0);
            send_command_i_stall(GeCommand::End, 0);
        }

        GuContextType::Call => {
            if CALL_MODE == 1 {
                send_command_i(GeCommand::Signal, 0x120000);
                send_command_i(GeCommand::End, 0);
                send_command_i_stall(GeCommand::Nop, 0);
            } else {
                send_command_i(GeCommand::Ret, 0);
            }
        }
    }

    let size = ((*LIST).current as usize) - ((*LIST).start as usize);

    // Go to parent list
    CURR_CONTEXT = (*LIST).parent_context;
    LIST = &mut CONTEXTS[CURR_CONTEXT as usize].list;
    size as i32
}

/// Finish current display list and go back to the parent context, sending
/// argument id for the finish callback.
///
/// If the context is `Direct`, the stall-address is updated so that the entire
/// list will execute. Otherwise, only the terminating action is written to the
/// list, depending on the context type.
///
/// # Parameters
///
/// - `id`: Finish callback id (16-bit)
///
/// # Return Value
///
/// Size of finished display list
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuFinishId(id: u32) -> i32 {
    match CURR_CONTEXT {
        GuContextType::Direct | GuContextType::Send => {
            send_command_i(GeCommand::Finish, (id & 0xffff) as i32);
            send_command_i_stall(GeCommand::End, 0);
        }

        GuContextType::Call => {
            if CALL_MODE == 1 {
                send_command_i(GeCommand::Signal, 0x120000);
                send_command_i(GeCommand::End, 0);
                send_command_i_stall(GeCommand::Nop, 0);
            } else {
                send_command_i(GeCommand::Ret, 0);
            }
        }
    }

    let size = ((*LIST).current as usize) - ((*LIST).start as usize);

    // Go to parent list
    CURR_CONTEXT = (*LIST).parent_context;
    LIST = &mut CONTEXTS[CURR_CONTEXT as usize].list;
    size as i32
}

/// Call previously generated display-list
///
/// # Parameters
///
/// - `list`: Display list to call
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuCallList(list: *const c_void) {
    let list_addr = list as u32;

    if CALL_MODE == 1 {
        send_command_i(GeCommand::Signal, (list_addr >> 16) as i32 | 0x110000);
        send_command_i(GeCommand::End, list_addr as i32 & 0xffff);
        send_command_i_stall(GeCommand::Nop, 0);
    } else {
        send_command_i(GeCommand::Base, (list_addr >> 8) as i32 & 0xf0000);
        send_command_i_stall(GeCommand::Call, list_addr as i32 & 0xffffff);
    }
}

/// Set whether to use stack-based calls or signals to handle execution of
/// called lists.
///
/// # Parameters
///
/// - `mode`: True (1) to enable signals, false (0) to disable signals and use
///   normal calls instead.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuCallMode(mode: i32) {
    CALL_MODE = mode;
}

/// Check how large the current display list is
///
/// # Return Value
///
/// The size of the current display list
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuCheckList() -> i32 {
    (*LIST).current.sub((*LIST).start as usize) as i32
}

/// Send a list to the GE directly
///
/// # Parameters
///
/// - `mode`: Whether to place the list first or last in queue
/// - `list`: List to send
/// - `context`: Temporary storage for the GE context
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSendList(
    mode: GuQueueMode,
    list: *const c_void,
    context: *mut GeContext,
) {
    SETTINGS.signal_offset = 0;

    let mut args = GeListArgs {
        size: 8,
        context,
        ..<_>::default()
    };

    let callback = SETTINGS.ge_callback_id;

    let list_id = match mode {
        GuQueueMode::Head => {
            crate::sys::sceGeListEnQueueHead(list, null_mut(), callback, &mut args)
        }

        GuQueueMode::Tail => crate::sys::sceGeListEnQueue(list, null_mut(), callback, &mut args),
    };

    GE_LIST_EXECUTED[1] = list_id;
}

/// Swap display and draw buffer
///
/// # Return Value
///
/// Pointer to the new drawbuffer
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSwapBuffers() -> *mut c_void {
    if let Some(cb) = SETTINGS.swap_buffers_callback {
        cb(
            addr_of_mut!(DRAW_BUFFER.disp_buffer),
            addr_of_mut!(DRAW_BUFFER.frame_buffer),
        );
    } else {
        mem::swap(&mut DRAW_BUFFER.disp_buffer, &mut DRAW_BUFFER.frame_buffer);
    }

    if DISPLAY_ON {
        crate::sys::sceDisplaySetFrameBuf(
            GE_EDRAM_ADDRESS.add(DRAW_BUFFER.disp_buffer as usize) as *const u8,
            DRAW_BUFFER.frame_width as usize,
            DRAW_BUFFER.pixel_size,
            SETTINGS.swap_buffers_behaviour,
        );
    }

    // Comment from the C PSPSDK: remove this? it serves no real purpose
    CURRENT_FRAME ^= 1;

    DRAW_BUFFER.frame_buffer
}

/// Wait until display list has finished executing
///
/// # Parameters
///
/// - `mode`: What to wait for, one of `GuSyncMode`
/// - `behavior`: How to sync, one of `GuSyncBehavior`
///
/// # Return Value
///
/// Unknown at this time. GeListState?
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSync(mode: GuSyncMode, behavior: GuSyncBehavior) -> GeListState {
    match mode {
        GuSyncMode::Finish => crate::sys::sceGeDrawSync(behavior as i32),
        GuSyncMode::List => crate::sys::sceGeListSync(GE_LIST_EXECUTED[0], behavior as i32),
        GuSyncMode::Send => crate::sys::sceGeListSync(GE_LIST_EXECUTED[1], behavior as i32),
        _ => GeListState::Done,
    }
}

// FIXME: Confusing documentation.
/// Draw array of vertices forming primitives
///
/// Vertex order:
///
/// - Weights (0-8)
/// - Texture UV
/// - Color
/// - Normal
/// - Position
///
/// # Note
///
/// Every vertex must align to 32 bits, which means that you HAVE to pad if it does not add up!
///
/// # Parameters
///
/// - `prim`: What kind of primitives to render
/// - `vtype`: Vertex type to process
/// - `count`: How many vertices to process
/// - `indices`: Optional pointer to an index-list
/// - `vertices`: Pointer to a vertex-list
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDrawArray(
    prim: GuPrimitive,
    vtype: VertexType,
    count: i32,
    indices: *const c_void,
    vertices: *const c_void,
) {
    if !vtype.is_empty() {
        send_command_i(GeCommand::VertexType, vtype.bits());
    }

    if !indices.is_null() {
        send_command_i(GeCommand::Base, (indices as u32 >> 8) as i32 & 0xf0000);
        send_command_i(GeCommand::Iaddr, indices as i32 & 0xffffff);
    }

    if !vertices.is_null() {
        send_command_i(GeCommand::Base, (vertices as u32 >> 8) as i32 & 0xf0000);
        send_command_i(GeCommand::Vaddr, vertices as i32 & 0xffffff);
    }

    send_command_i_stall(GeCommand::Prim, ((prim as i32) << 16) | count);
}

/// Begin conditional rendering of object
///
/// If no vertices passed into this function are inside the scissor region, it
/// will skip rendering the object. There can be up to 32 levels of conditional
/// testing, and all levels HAVE to be terminated by sceGuEndObject().
///
/// # Parameters
///
/// - `vtype`: Vertex type to process
/// - `count`: Number of vertices to test
/// - `indices`: Optional list to an index-list
/// - `vertices`: Pointer to a vertex-list
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuBeginObject(
    vtype: i32,
    count: i32,
    indices: *const c_void,
    vertices: *const c_void,
) {
    if vtype != 0 {
        send_command_i(GeCommand::VertexType, vtype);
    }

    if !indices.is_null() {
        send_command_i(GeCommand::Base, (indices as u32 >> 8) as i32 & 0xf0000);
        send_command_i(GeCommand::Iaddr, indices as i32 & 0xffffff);
    }

    if !vertices.is_null() {
        send_command_i(GeCommand::Base, (vertices as u32 >> 8) as i32 & 0xf0000);
        send_command_i(GeCommand::Vaddr, vertices as i32 & 0xffffff);
    }

    send_command_i(GeCommand::BoundingBox, count);

    // Store start to new object
    (*OBJECT_STACK.offset(OBJECT_STACK_DEPTH as isize)) = (*LIST).current;
    OBJECT_STACK_DEPTH += 1;

    // Dummy commands, overwritten in `sceGuEndObject`
    send_command_i(GeCommand::Base, 0);
    send_command_i(GeCommand::BJump, 0);
}

/// End conditional rendering of object
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuEndObject() {
    // Rewrite commands from `sceGuBeginObject`

    let current = (*LIST).current;
    (*LIST).current = *OBJECT_STACK.offset(OBJECT_STACK_DEPTH as isize - 1);

    send_command_i(GeCommand::Base, (current as u32 >> 8) as i32 & 0xf0000);
    send_command_i(GeCommand::BJump, current as i32 & 0xffffff);
    (*LIST).current = current;
    OBJECT_STACK_DEPTH -= 1;
}

/// Enable or disable GE state
///
/// Look at sceGuEnable() for a list of states
///
/// # Parameters
///
/// - `state`: Which state to change
/// - `status`: `1` to enable or `0` to disable the state
// TODO: bool for ABI?
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSetStatus(state: GuState, status: i32) {
    if status != 0 {
        sceGuEnable(state);
    } else {
        sceGuDisable(state);
    }
}

/// Get if state is currently enabled or disabled
///
/// # Parameters
///
/// - `state`: Which state to query about
///
/// # Return Value
///
/// Whether state is enabled or not
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuGetStatus(state: GuState) -> bool {
    let state = state as u32;

    if state < 22 {
        return (STATES >> state) & 1 != 0;
    }

    false
}

/// Set the status on all 22 available states
///
/// # Parameters
///
/// - `status`: Bitmask (0-21) containing the status of all 22 states
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSetAllStatus(status: i32) {
    for i in 0..22 {
        if (status >> i) & 1 != 0 {
            sceGuEnable(mem::transmute(i));
        } else {
            sceGuDisable(mem::transmute(i));
        }
    }
}

/// Query status on all 22 available states
///
/// # Return Value
///
/// Status of all 22 states as a bitmask (0-21)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuGetAllStatus() -> i32 {
    STATES as i32
}

/// Enable GE state
///
/// # Parameters
///
/// - `state`: Which state to enable, one of `GuState`
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuEnable(state: GuState) {
    match state {
        GuState::AlphaTest => send_command_i(GeCommand::AlphaTestEnable, 1),
        GuState::DepthTest => send_command_i(GeCommand::ZTestEnable, 1),
        GuState::ScissorTest => {
            let context = &mut CONTEXTS[CURR_CONTEXT as usize];
            context.scissor_enable = 1;
            send_command_i(
                GeCommand::Scissor1,
                (context.scissor_start[1] << 10) | context.scissor_start[0],
            );
            send_command_i(
                GeCommand::Scissor2,
                (context.scissor_end[1] << 10) | context.scissor_end[0],
            );
        }
        GuState::StencilTest => send_command_i(GeCommand::StencilTestEnable, 1),
        GuState::Blend => send_command_i(GeCommand::AlphaBlendEnable, 1),
        GuState::CullFace => send_command_i(GeCommand::CullFaceEnable, 1),
        GuState::Dither => send_command_i(GeCommand::DitherEnable, 1),
        GuState::Fog => send_command_i(GeCommand::FogEnable, 1),
        GuState::ClipPlanes => send_command_i(GeCommand::DepthClampEnable, 1),
        GuState::Texture2D => send_command_i(GeCommand::TextureMapEnable, 1),
        GuState::Lighting => send_command_i(GeCommand::LightingEnable, 1),
        GuState::Light0 => send_command_i(GeCommand::LightEnable0, 1),
        GuState::Light1 => send_command_i(GeCommand::LightEnable1, 1),
        GuState::Light2 => send_command_i(GeCommand::LightEnable2, 1),
        GuState::Light3 => send_command_i(GeCommand::LightEnable3, 1),
        GuState::LineSmooth => send_command_i(GeCommand::AntiAliasEnable, 1),
        GuState::PatchCullFace => send_command_i(GeCommand::PatchCullEnable, 1),
        GuState::ColorTest => send_command_i(GeCommand::ColorTestEnable, 1),
        GuState::ColorLogicOp => send_command_i(GeCommand::LogicOpEnable, 1),
        GuState::FaceNormalReverse => send_command_i(GeCommand::ReverseNormal, 1),
        GuState::PatchFace => send_command_i(GeCommand::PatchFacing, 1),
        GuState::Fragment2X => {
            let context = &mut CONTEXTS[CURR_CONTEXT as usize];
            context.fragment_2x = 0x10000;
            send_command_i(GeCommand::TexFunc, 0x10000 | context.texture_function);
        }
    }

    if (state as u32) < 22 {
        STATES |= 1 << state as u32
    }
}

/// Disable GE state
///
/// # Parameters
///
/// - `state`: Which state to disable, one of `GuState`
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDisable(state: GuState) {
    match state {
        GuState::AlphaTest => send_command_i(GeCommand::AlphaTestEnable, 0),
        GuState::DepthTest => send_command_i(GeCommand::ZTestEnable, 0),
        GuState::ScissorTest => {
            let context = &mut CONTEXTS[CURR_CONTEXT as usize];
            context.scissor_enable = 0;
            send_command_i(GeCommand::Scissor1, 0);
            send_command_i(
                GeCommand::Scissor2,
                ((DRAW_BUFFER.height - 1) << 10) | (DRAW_BUFFER.width - 1),
            );
        }
        GuState::StencilTest => send_command_i(GeCommand::StencilTestEnable, 0),
        GuState::Blend => send_command_i(GeCommand::AlphaBlendEnable, 0),
        GuState::CullFace => send_command_i(GeCommand::CullFaceEnable, 0),
        GuState::Dither => send_command_i(GeCommand::DitherEnable, 0),
        GuState::Fog => send_command_i(GeCommand::FogEnable, 0),
        GuState::ClipPlanes => send_command_i(GeCommand::DepthClampEnable, 0),
        GuState::Texture2D => send_command_i(GeCommand::TextureMapEnable, 0),
        GuState::Lighting => send_command_i(GeCommand::LightingEnable, 0),
        GuState::Light0 => send_command_i(GeCommand::LightEnable0, 0),
        GuState::Light1 => send_command_i(GeCommand::LightEnable1, 0),
        GuState::Light2 => send_command_i(GeCommand::LightEnable2, 0),
        GuState::Light3 => send_command_i(GeCommand::LightEnable3, 0),
        GuState::LineSmooth => send_command_i(GeCommand::AntiAliasEnable, 0),
        GuState::PatchCullFace => send_command_i(GeCommand::PatchCullEnable, 0),
        GuState::ColorTest => send_command_i(GeCommand::ColorTestEnable, 0),
        GuState::ColorLogicOp => send_command_i(GeCommand::LogicOpEnable, 0),
        GuState::FaceNormalReverse => send_command_i(GeCommand::ReverseNormal, 0),
        GuState::PatchFace => send_command_i(GeCommand::PatchFacing, 0),
        GuState::Fragment2X => {
            let context = &mut CONTEXTS[CURR_CONTEXT as usize];
            context.fragment_2x = 0;
            send_command_i(GeCommand::TexFunc, context.texture_function);
        }
    }

    if (state as u32) < 22 {
        STATES &= !(1 << state as u32)
    }
}

/// Set light parameters
///
/// # Parameters
///
/// - `light`: Light index
/// - `type`: Light type, one of `LightType`
/// - `components`: Light components, one or more of `LightComponent`
/// - `position`: Light position
// FIXME: light components seem to be a subset here.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuLight(
    light: i32,
    type_: LightType,
    components: LightComponent,
    position: &ScePspFVector3,
) {
    let settings = &LIGHT_COMMANDS[light as usize];

    send_command_f(settings.xpos, position.x);
    send_command_f(settings.ypos, position.y);
    send_command_f(settings.zpos, position.z);

    let mut kind = 2;
    if components.bits() != 8 {
        kind = if components.bits() ^ 6 < 1 { 1 } else { 0 };
    }

    send_command_i(settings.type_, ((type_ as i32 & 0x03) << 8) | kind);
}

/// Set light attenuation
///
/// # Parameters
///
/// - `light`: Light index
/// - `atten0`: Constant attenuation factor
/// - `atten1`: Linear attenuation factor
/// - `atten2`: Quadratic attenuation factor
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuLightAtt(light: i32, atten0: f32, atten1: f32, atten2: f32) {
    let settings = &LIGHT_COMMANDS[light as usize];
    send_command_f(settings.constant, atten0);
    send_command_f(settings.linear, atten1);
    send_command_f(settings.quadratic, atten2);
}

/// Set light color
///
/// # Parameters
///
/// - `light`: Light index
/// - `component`: Which component(s) to set
/// - `color`: Which color to use
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuLightColor(light: i32, component: LightComponent, color: u32) {
    let settings = &LIGHT_COMMANDS[light as usize];

    // PSPSDK implements this as a jump table, probably for speed. Should we do
    // this too?
    //
    // TODO: Or maybe only certain combinations are valid?

    if component.intersects(LightComponent::AMBIENT) {
        send_command_i(settings.ambient, (color & 0xffffff) as i32);
    }

    if component.intersects(LightComponent::DIFFUSE) {
        send_command_i(settings.diffuse, (color & 0xffffff) as i32);
    }

    if component.intersects(LightComponent::SPECULAR) {
        send_command_i(settings.specular, (color & 0xffffff) as i32);
    }
}

/// Set light mode
///
/// # Parameters
///
/// - `mode`: Light mode to use
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuLightMode(mode: LightMode) {
    send_command_i(GeCommand::LightMode, mode as i32);
}

/// Set spotlight parameters
///
/// # Parameters
///
/// - `light`: Light index
/// - `direction`: Spotlight direction
/// - `exponent`: Spotlight exponent
/// - `cutoff`: Spotlight cutoff angle (in radians)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuLightSpot(
    light: i32,
    direction: &ScePspFVector3,
    exponent: f32,
    cutoff: f32,
) {
    let settings = &LIGHT_COMMANDS[light as usize];

    send_command_f(settings.exponent, exponent);
    send_command_f(settings.cutoff, cutoff);

    send_command_f(settings.xdir, direction.x);
    send_command_f(settings.ydir, direction.y);
    send_command_f(settings.zdir, direction.z);
}

/// Clear current drawbuffer
///
/// # Parameters
///
/// - `flags`: Which part of the buffer to clear
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuClear(flags: ClearBuffer) {
    let context = &mut CONTEXTS[CURR_CONTEXT as usize];

    struct Vertex {
        color: u32,
        x: u16,
        y: u16,
        z: u16,
        _pad: u16,
    }

    let filter: u32 = match DRAW_BUFFER.pixel_size {
        DisplayPixelFormat::Psm5650 => context.clear_color & 0xffffff,
        DisplayPixelFormat::Psm5551 => {
            (context.clear_color & 0xffffff) | (context.clear_stencil << 31)
        }
        DisplayPixelFormat::Psm4444 => {
            (context.clear_color & 0xffffff) | (context.clear_stencil << 28)
        }
        DisplayPixelFormat::Psm8888 => {
            (context.clear_color & 0xffffff) | (context.clear_stencil << 24)
        }
    };

    let vertices;
    let count;

    if !flags.intersects(ClearBuffer::FAST_CLEAR_BIT) {
        vertices = sceGuGetMemory(2 * mem::size_of::<Vertex>() as i32) as *mut Vertex;
        count = 2;

        (*vertices.offset(0)).color = 0;
        (*vertices.offset(0)).x = 0;
        (*vertices.offset(0)).y = 0;
        (*vertices.offset(0)).z = context.clear_depth as u16;

        (*vertices.offset(1)).color = filter;
        (*vertices.offset(1)).x = DRAW_BUFFER.width as u16;
        (*vertices.offset(1)).y = DRAW_BUFFER.height as u16;
        (*vertices.offset(1)).z = context.clear_depth as u16;
    } else {
        count = ((DRAW_BUFFER.width + 63) / 64) * 2;
        vertices = sceGuGetMemory(count * core::mem::size_of::<Vertex>() as i32) as *mut Vertex;

        let mut curr = vertices;

        for i in 0..count {
            let j = i >> 1;
            let k = i & 1;

            (*curr).color = filter;
            (*curr).x = (j + k) as u16 * 64;
            (*curr).y = (k * DRAW_BUFFER.height) as u16;
            (*curr).z = context.clear_depth as u16;

            curr = curr.add(1);
        }
    }

    {
        let relevant_flags = flags
            & (ClearBuffer::COLOR_BUFFER_BIT
                | ClearBuffer::STENCIL_BUFFER_BIT
                | ClearBuffer::DEPTH_BUFFER_BIT);

        send_command_i(
            GeCommand::ClearMode,
            (relevant_flags.bits() << 8) as i32 | 0x01,
        );
    }

    sceGuDrawArray(
        GuPrimitive::Sprites,
        VertexType::COLOR_8888 | VertexType::VERTEX_16BIT | VertexType::TRANSFORM_2D,
        count,
        null_mut(),
        vertices as *mut c_void,
    );

    send_command_i(GeCommand::ClearMode, 0);
}

/// Set the current clear-color
///
/// # Parameters
///
/// - `color`: Color to clear with
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuClearColor(color: u32) {
    CONTEXTS[CURR_CONTEXT as usize].clear_color = color;
}

/// Set the current clear-depth
///
/// # Parameters
///
/// - `depth`: Set which depth to clear with (0x0000-0xffff)
// TODO: Can `depth` be u16 or does this cause issues with FFI ABI compatibility?
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuClearDepth(depth: u32) {
    CONTEXTS[CURR_CONTEXT as usize].clear_depth = depth;
}

/// Set the current stencil clear value
///
/// # Parameters
///
/// - `stencil`: Set which stencil value to clear with (0-255)
// TODO: Can `stencil` be u8 or does this cause issues with FFI ABI compatibility?
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuClearStencil(stencil: u32) {
    CONTEXTS[CURR_CONTEXT as usize].clear_stencil = stencil;
}

/// Set mask for which bits of the pixels to write
///
/// # Parameters
///
/// - `mask`: Which bits to filter against writes
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuPixelMask(mask: u32) {
    send_command_i(GeCommand::MaskRgb, mask as i32 & 0xffffff);
    send_command_i(GeCommand::MaskAlpha, (mask >> 24) as i32);
}

/// Set current primitive color
///
/// # Parameters
///
/// - `color`: Which color to use (overridden by vertex colors)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuColor(color: u32) {
    sceGuMaterial(
        LightComponent::AMBIENT | LightComponent::DIFFUSE | LightComponent::SPECULAR,
        color,
    );
}

/// Set the color test function
///
/// The color test is only performed while `GuState::ColorTest` is enabled, e.g.
/// via `sceGuEnable`.
///
/// # Parameters
///
/// - `func`: Color test function
/// - `color`: Color to test against
/// - `mask`: Mask ANDed against both source and destination when testing
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuColorFunc(func: ColorFunc, color: u32, mask: u32) {
    send_command_i(GeCommand::ColorTest, func as i32 & 0x03);
    send_command_i(GeCommand::ColorRef, color as i32 & 0xffffff);
    send_command_i(GeCommand::ColorTestmask, mask as i32);
}

/// Set which color components the material will receive
///
/// # Parameters
///
/// - `components`: Which component(s) to receive
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuColorMaterial(components: LightComponent) {
    send_command_i(GeCommand::MaterialUpdate, components.bits());
}

/// Set the alpha test parameters
///
/// # Parameters
///
/// - `func`: Specifies the alpha comparison function.
/// - `value`: Specifies the reference value that incoming alpha values are compared to.
/// - `mask`: Specifies the mask that both values are ANDed with before comparison.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuAlphaFunc(func: AlphaFunc, value: i32, mask: i32) {
    let arg = func as i32 | ((value & 0xff) << 8) | ((mask & 0xff) << 16);
    send_command_i(GeCommand::AlphaTest, arg);
}

#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuAmbient(color: u32) {
    send_command_i(GeCommand::AmbientColor, color as i32 & 0xffffff);
    send_command_i(GeCommand::AmbientAlpha, (color >> 24) as i32);
}

#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuAmbientColor(color: u32) {
    send_command_i(GeCommand::MaterialAmbient, color as i32 & 0xffffff);
    send_command_i(GeCommand::MaterialAlpha, (color >> 24) as i32);
}

/// Set the blending mode
///
/// This is similar to `glBlendEquation` combined with `glBlendFunc` in OpenGL.
///
/// # Parameters
///
/// - `op`: Blending Operation
/// - `src`: Blending function for source operand
/// - `dest`: Blending function for dest operand
/// - `srcfix`: Fixed value for `BlendFactor::Fix` (source operand)
/// - `destfix`: Fixed value for `BlendFactor::Fix` (dest operand)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuBlendFunc(
    op: BlendOp,
    src: BlendFactor,
    dest: BlendFactor,
    src_fix: u32,
    dest_fix: u32,
) {
    send_command_i(
        GeCommand::BlendMode,
        src as i32 | ((dest as i32) << 4) | ((op as i32) << 8),
    );
    send_command_i(GeCommand::BlendFixedA, src_fix as i32 & 0xffffff);
    send_command_i(GeCommand::BlendFixedB, dest_fix as i32 & 0xffffff);
}

/// Set current primitive color, for specific light components.
///
/// # Parameters
///
/// - `components`: Which component(s) to set
/// - `color`: Color to set (*likely* overridden by vertex colors)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuMaterial(components: LightComponent, color: u32) {
    if components.intersects(LightComponent::AMBIENT) {
        send_command_i(GeCommand::MaterialAmbient, color as i32 & 0xffffff);
        send_command_i(GeCommand::MaterialAlpha, (color >> 24) as i32);
    }

    if components.intersects(LightComponent::DIFFUSE) {
        send_command_i(GeCommand::MaterialDiffuse, color as i32 & 0xffffff);
    }

    if components.intersects(LightComponent::SPECULAR) {
        send_command_i(GeCommand::MaterialSpecular, color as i32 & 0xffffff);
    }
}

// TODO: Needs documentation.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuModelColor(emissive: u32, ambient: u32, diffuse: u32, specular: u32) {
    send_command_i(GeCommand::MaterialEmissive, emissive as i32 & 0xffffff);
    send_command_i(GeCommand::MaterialAmbient, ambient as i32 & 0xffffff);
    send_command_i(GeCommand::MaterialDiffuse, diffuse as i32 & 0xffffff);
    send_command_i(GeCommand::MaterialSpecular, specular as i32 & 0xffffff);
}

/// Set stencil function and reference value for stencil testing
///
/// # Parameters
///
/// - `func`: Test function
/// - `ref_`: The reference value for the stencil test
/// - `mask`: Mask that is ANDed with both the reference value and stored
///   stencil value when the test is done
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuStencilFunc(func: StencilFunc, ref_: i32, mask: i32) {
    send_command_i(
        GeCommand::StencilTest,
        func as i32 | ((ref_ & 0xff) << 8) | ((mask & 0xff) << 16),
    );
}

/// Set the stencil test actions
///
/// As stencil buffer shares memory with framebuffer alpha, resolution of the buffer
/// is directly in relation.
///
/// # Parameters
///
/// - `fail`: The action to take when the stencil test fails
/// - `zfail`: The action to take when the stencil test passes, but the depth test fails
/// - `zpass`: The action to take when both the stencil test and depth test pass
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuStencilOp(
    fail: StencilOperation,
    zfail: StencilOperation,
    zpass: StencilOperation,
) {
    send_command_i(
        GeCommand::StencilOp,
        fail as i32 | ((zfail as i32) << 8) | ((zpass as i32) << 16),
    );
}

/// Set the specular power for the material
///
/// # Parameters
///
/// - `power`: Specular power
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSpecular(power: f32) {
    send_command_f(GeCommand::MaterialSpecularCoef, power);
}

/// Set the current face-order (for culling)
///
/// This only has effect when culling (`GuState::CullFace`) is enabled, e.g. via
/// `sceGuEnable`.
///
/// # Parameters
///
/// - `order`: Which order to use, one of `FrontFaceDirection`
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuFrontFace(order: FrontFaceDirection) {
    match order {
        FrontFaceDirection::CounterClockwise => send_command_i(GeCommand::Cull, 0),
        FrontFaceDirection::Clockwise => send_command_i(GeCommand::Cull, 1),
    }
}

/// Set color logical operation
///
/// This operation only has effect if `GuState::ColorLogicOp` is enabled, e.g. via
/// `sceGuEnable`.
///
/// # Parameters
///
/// - `op`: Operation to execute
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuLogicalOp(op: LogicalOperation) {
    send_command_i(GeCommand::LogicOp, op as i32 & 0x0f);
}

/// Set ordered pixel dither matrix
///
/// This dither matrix is only applied if `GuState::Dither` is enabled, e.g. via
/// `sceGuEnable`.
///
/// # Parameters
///
/// - `matrix`: Dither matrix
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSetDither(matrix: &ScePspIMatrix4) {
    send_command_i(
        GeCommand::Dith0,
        (matrix.x.x & 0x0f)
            | ((matrix.x.y & 0x0f) << 4)
            | ((matrix.x.z & 0x0f) << 8)
            | ((matrix.x.w & 0x0f) << 12),
    );

    send_command_i(
        GeCommand::Dith1,
        (matrix.y.x & 0x0f)
            | ((matrix.y.y & 0x0f) << 4)
            | ((matrix.y.z & 0x0f) << 8)
            | ((matrix.y.w & 0x0f) << 12),
    );

    send_command_i(
        GeCommand::Dith2,
        (matrix.z.x & 0x0f)
            | ((matrix.z.y & 0x0f) << 4)
            | ((matrix.z.z & 0x0f) << 8)
            | ((matrix.z.w & 0x0f) << 12),
    );

    send_command_i(
        GeCommand::Dith3,
        (matrix.w.x & 0x0f)
            | ((matrix.w.y & 0x0f) << 4)
            | ((matrix.w.z & 0x0f) << 8)
            | ((matrix.w.w & 0x0f) << 12),
    );
}

/// Set how primitives are shaded
///
/// # Parameters
///
/// - `mode`: Which mode to use, one of `ShadingModel`.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuShadeModel(mode: ShadingModel) {
    match mode {
        ShadingModel::Smooth => send_command_i(GeCommand::ShadeMode, 1),
        ShadingModel::Flat => send_command_i(GeCommand::ShadeMode, 0),
    }
}

// TODO: Maybe add examples in documentation?
/// Image transfer using the GE
///
/// # Note
///
/// Data must be aligned to 1 quad word (16 bytes)
///
/// # Parameters
///
/// - `psm`: Pixel format for buffer
/// - `sx`: Source X
/// - `sy`: Source Y
/// - `width`: Image width
/// - `height`: Image height
/// - `srcw`: Source buffer width (block aligned)
/// - `src`: Source pointer
/// - `dx`: Destination X
/// - `dy`: Destination Y
/// - `destw`: Destination buffer width (block aligned)
/// - `dest`: Destination pointer
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuCopyImage(
    psm: DisplayPixelFormat,
    sx: i32,
    sy: i32,
    width: i32,
    height: i32,
    srcw: i32,
    src: *mut c_void,
    dx: i32,
    dy: i32,
    destw: i32,
    dest: *mut c_void,
) {
    send_command_i(GeCommand::TransferSrc, (src as i32) & 0xffffff);
    send_command_i(
        GeCommand::TransferSrcW,
        (((src as u32) & 0xff000000) >> 8) as i32 | srcw,
    );
    send_command_i(GeCommand::TransferSrcPos, (sy << 10) | sx);
    send_command_i(GeCommand::TransferDst, (dest as i32) & 0xffffff);
    send_command_i(
        GeCommand::TransferDstW,
        (((dest as u32) & 0xff000000) >> 8) as i32 | destw,
    );
    send_command_i(GeCommand::TransferDstPos, (dy << 10) | dx);
    send_command_i(GeCommand::TransferSize, ((height - 1) << 10) | (width - 1));

    let is_32_bit_texel = if let DisplayPixelFormat::Psm8888 = psm {
        1
    } else {
        0
    };

    send_command_i(GeCommand::TransferStart, is_32_bit_texel);
}

/// Specify the texture environment color
///
/// This is used in the texture function when a constant color is needed.
///
/// See `sceGuTexFunc` for more information.
///
/// # Parameters
///
/// - `color`: Constant color (0x00BBGGRR)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexEnvColor(color: u32) {
    send_command_i(GeCommand::TexEnvColor, color as i32 & 0xffffff);
}

/// Set how the texture is filtered
///
/// # Parameters
///
/// - `min`: Minimizing filter
/// - `mag`: Magnifying filter
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexFilter(min: TextureFilter, mag: TextureFilter) {
    send_command_i(GeCommand::TexFilter, ((mag as i32) << 8) | (min as i32));
}

/// Flush texture page-cache
///
/// Do this if you have copied/rendered into an area currently in the texture
/// cache.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexFlush() {
    send_command_f(GeCommand::TexFlush, 0.0);
}

/// Set how textures are applied
///
/// # Parameters
///
/// - `tfx`: Which apply-mode to use
/// - `tcc`: Which component-mode to use
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexFunc(tfx: TextureEffect, tcc: TextureColorComponent) {
    let context = &mut CONTEXTS[CURR_CONTEXT as usize];
    context.texture_function = (((tcc as u32) << 8) | (tfx as u32)) as i32;
    send_command_i(
        GeCommand::TexFunc,
        (((tcc as u32) << 8) | (tfx as u32) | context.fragment_2x as u32) as i32,
    );
}

/// Set current texturemap
///
/// Textures may reside in main RAM, but it has a huge speed-penalty. Swizzle textures
/// to get maximum speed.
///
/// # Note
///
/// Data must be aligned to 1 quad word (16 bytes)
///
/// # Parameters
///
/// - `mipmap`: Mipmap level
/// - `width`: Width of texture map (must be a power of 2)
/// - `height`: Height of texture map (must be a power of 2)
/// - `tbw`: Texture Buffer Width (block-aligned). For `PixelFormat::8888`, this
///    seems to have to be a multiple of 4. This value indicates the actual
///    width of the image, not the width of the texture map.
/// - `tbp`: Texture buffer pointer (16 byte aligned)
///
/// # `width` vs `tbw`
///
/// The parameters `height` and `width` indicate the size of the texture map. It
/// is possible to pass in oddly sized textures, however, using the `tbw`
/// parameter, as long as the true width is divisible by (... block size? The
/// block size seems to be 4 for `PixelFormat::8888`).
///
/// As an example, say you have a 340x340 image. This image can be passed in
/// like so:
///
/// ```ignore
/// let image_data = ...; // Some 16-byte aligned source.
/// sceGuTexImage(MipmapLevel::None, 512, 512, 340, data);
/// ```
///
/// This will generate a 512x512 pixel texture map, with the remaining horizontal
/// space being filled with the original texture repeating. The remaining
/// vertical space will overflow into the data past the input buffer, which may
/// appear as garbage data. This is not a problem as the UV coordinates on the
/// triangles can be crafted to stay within the image bounds, both vertically and
/// horizontally.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexImage(
    mipmap: MipmapLevel,
    width: i32,
    height: i32,
    tbw: i32,
    tbp: *const c_void,
) {
    use core::intrinsics::ctlz;

    const TBP_CMD_TBL: [GeCommand; 8] = [
        GeCommand::TexAddr0,
        GeCommand::TexAddr1,
        GeCommand::TexAddr2,
        GeCommand::TexAddr3,
        GeCommand::TexAddr4,
        GeCommand::TexAddr5,
        GeCommand::TexAddr6,
        GeCommand::TexAddr7,
    ];

    const TBW_CMD_TBL: [GeCommand; 8] = [
        GeCommand::TexBufWidth0,
        GeCommand::TexBufWidth1,
        GeCommand::TexBufWidth2,
        GeCommand::TexBufWidth3,
        GeCommand::TexBufWidth4,
        GeCommand::TexBufWidth5,
        GeCommand::TexBufWidth6,
        GeCommand::TexBufWidth7,
    ];

    const TSIZE_CMD_TBL: [GeCommand; 8] = [
        GeCommand::TexSize0,
        GeCommand::TexSize1,
        GeCommand::TexSize2,
        GeCommand::TexSize3,
        GeCommand::TexSize4,
        GeCommand::TexSize5,
        GeCommand::TexSize6,
        GeCommand::TexSize7,
    ];

    send_command_i(TBP_CMD_TBL[mipmap as usize], (tbp as i32) & 0xffffff);
    send_command_i(
        TBW_CMD_TBL[mipmap as usize],
        ((tbp as u32 >> 8) as i32 & 0x0f0000) | tbw,
    );
    send_command_i(
        TSIZE_CMD_TBL[mipmap as usize],
        (((31 - ctlz(height & 0x3ff)) << 8) | (31 - ctlz(width & 0x3ff))) as i32,
    );
    sceGuTexFlush();
}

/// Set texture-level mode (mipmapping)
///
/// # Parameters
///
/// - `mode`: Which mode to use, one of TextureLevelMode
/// - `bias`: Which mipmap bias to use
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexLevelMode(mode: TextureLevelMode, bias: f32) {
    // Linker error if this is not here.
    #[no_mangle]
    #[cfg(target_os = "psp")]
    #[allow(deprecated)]
    unsafe extern "C" fn truncf(mut x: f32) -> f32 {
        core::arch::asm!("cvt.w.s {0}, {0}", inout(freg) x);
        x
    }

    let mut offset = core::intrinsics::truncf32(bias * 16.0) as i32;

    // PSPSDK: mip map bias?
    if offset >= 128 {
        offset = 128
    } else if offset < -128 {
        offset = -128;
    }

    send_command_i(GeCommand::TexLevel, ((offset as i32) << 16) | mode as i32);
}

/// Set the texture-mapping mode
///
/// # Parameters
///
/// - `mode`: Which mode to use
/// - `a1`: Unknown
/// - `a2`: Unknown
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexMapMode(mode: TextureMapMode, a1: u32, a2: u32) {
    let context = &mut CONTEXTS[CURR_CONTEXT as usize];
    context.texture_map_mode = mode;
    send_command_i(
        GeCommand::TexMapMode,
        ((context.texture_proj_map_mode as i32) << 8) | mode as i32,
    );
    send_command_i(GeCommand::TexShadeLs, ((a2 << 8) | (a1 & 0x03)) as i32);
}

/// Set texture-mode parameters
///
/// # Parameters
///
/// - `tpsm`: Which texture format to use
/// - `maxmips`: Number of mipmaps to use (0-7)
/// - `a2`: Unknown, set to 0
/// - `swizzle`: `1` to swizzle texture-reads.
// TODO: Are boolean parameters ABI compatibile with FFI i32 parameters?
// TODO: Better documentation for `maxmips`. What does it do? Maybe it should be
//       of type `MipmapLevel`?
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexMode(
    tpsm: TexturePixelFormat,
    maxmips: i32,
    a2: i32,
    swizzle: i32,
) {
    CONTEXTS[CURR_CONTEXT as usize].texture_mode = tpsm;

    send_command_i(GeCommand::TexMode, (maxmips << 16) | (a2 << 8) | swizzle);

    send_command_i(GeCommand::TexFormat, tpsm as i32);
    sceGuTexFlush();
}

/// Set texture offset
///
/// # Note
///
/// Only used by the 3D T&L pipe, renders done with `VertexType::TRANSFORM_2D`
/// are not affected by this.
///
/// # Parameters
///
/// - `u`: Offset to add to the U coordinate
/// - `v`: Offset to add to the V coordinate
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexOffset(u: f32, v: f32) {
    send_command_f(GeCommand::TexOffsetU, u);
    send_command_f(GeCommand::TexOffsetV, v);
}

/// Set texture projection-map mode
///
/// # Parameters
///
/// - `mode`: Which mode to use
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexProjMapMode(mode: TextureProjectionMapMode) {
    let context = &mut CONTEXTS[CURR_CONTEXT as usize];
    context.texture_proj_map_mode = mode;
    send_command_i(
        GeCommand::TexMapMode,
        ((mode as i32) << 8) | context.texture_map_mode as i32,
    );
}

/// Set texture scale
///
/// # Note
///
/// Only used by the 3D T&L pipe, renders ton with `VertexType::TRANSFORM_2D`
/// are not affected by this.
///
/// # Parameters
///
/// - `u`: Scalar to multiply U coordinate with
/// - `v`: Scalar to multiply V coordinate with
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexScale(u: f32, v: f32) {
    send_command_f(GeCommand::TexScaleU, u);
    send_command_f(GeCommand::TexScaleV, v);
}

#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexSlope(slope: f32) {
    send_command_f(GeCommand::TexLodSlope, slope);
}

/// Synchronize rendering pipeline with image upload.
///
/// This will stall the rendering pipeline until the current image upload initiated by
/// `sceGuCopyImage` has completed.
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexSync() {
    send_command_i(GeCommand::TexSync, 0);
}

/// Set if the texture should repeat or clamp
///
/// Available modes are:
///
/// # Parameters
///
/// - `u`: Wrap-mode for the U direction
/// - `v`: Wrap-mode for the V direction
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuTexWrap(u: GuTexWrapMode, v: GuTexWrapMode) {
    send_command_i(GeCommand::TexWrap, ((v as i32) << 8) | u as i32);
}

/// Upload CLUT (Color Lookup Table)
///
/// # Note
///
/// Data must be aligned to 1 quad word (16 bytes)
///
/// # Parameters
///
/// - `num_blocks`: How many blocks of 8 entries to upload (32*8 is 256 colors)
/// - `cbp`: Pointer to palette (16 byte aligned)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuClutLoad(num_blocks: i32, cbp: *const c_void) {
    send_command_i(GeCommand::ClutAddr, (cbp as i32) & 0xffffff);
    send_command_i(
        GeCommand::ClutAddrUpper,
        ((cbp as u32) >> 8) as i32 & 0xf0000,
    );
    send_command_i(GeCommand::LoadClut, num_blocks);
}

/// CLUT palette pixel formats.
///
/// This is the pixel format for the input palette when setting up a CLUT.
#[repr(u32)]
pub enum ClutPixelFormat {
    /// Hicolor, 16-bit, RGB 5:6:5
    Psm5650 = 0,
    /// Hicolor, 16-bit, RGBA 5:5:5:1
    Psm5551 = 1,
    /// Hicolor, 16-bit, RGBA 4:4:4:4
    Psm4444 = 2,
    /// Truecolor, 32-bit, RGBA 8:8:8:8
    Psm8888 = 3,
}

/// Set current CLUT mode
///
/// # Parameters
///
/// - `cpsm`: Which pixel format to use for the palette
/// - `shift`: Shifts color index by that many bits to the right
/// - `mask`: Masks the color index with this bitmask after the shift (0-0xFF)
/// - `a3`: Unknown, set to 0
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuClutMode(cpsm: ClutPixelFormat, shift: u32, mask: u32, a3: u32) {
    let arg = ((cpsm as u32) | (shift << 2) | (mask << 8) | (a3 << 16)) as i32;
    send_command_i(GeCommand::ClutFormat, arg);
}

/// Set virtual coordinate offset
///
/// The PSP has a virtual coordinate-space of 4096x4096, this controls where
/// rendering is performed.
///
/// # Example
///
/// Center the virtual coordinate range:
///
/// ```no_run
/// # use psp::sys::sceGuOffset;
/// sceGuOffset(2048 - (480 / 2), 2048 - (272 / 2));
/// ```
///
/// # Parameters
///
/// - `x`: Offset (0-4095)
/// - `y`: Offset (0-4095)
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuOffset(x: u32, y: u32) {
    send_command_i(GeCommand::OffsetX, (x << 4) as i32);
    send_command_i(GeCommand::OffsetY, (y << 4) as i32);
}

/// Set what to scissor within the current viewport
///
/// Note that scissoring is only performed if the custom scissoring
/// (`GuState::ScissorTest`) is enabled, e.g. via `sceGuEnable`.
///
/// # Parameters
///
/// - `x`: Left of scissor region
/// - `y`: Top of scissor region
/// - `w`: Width of scissor region
/// - `h`: Height of scissor region
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuScissor(x: i32, y: i32, w: i32, h: i32) {
    let context = &mut CONTEXTS[CURR_CONTEXT as usize];

    context.scissor_start = [x, y];
    context.scissor_end = [w - 1, h - 1];

    if context.scissor_enable != 0 {
        send_command_i(
            GeCommand::Scissor1,
            (context.scissor_start[1] << 10) | context.scissor_start[0],
        );
        send_command_i(
            GeCommand::Scissor2,
            (context.scissor_end[1] << 10) | context.scissor_end[0],
        );
    }
}

/// Set current viewport
///
/// # Example
///
/// Setup a viewport of size (480,272) with origin at (2048,2048)
///
/// ```no_run
/// # use psp::sys::sceGuViewport;
/// sceGuViewport(2048, 2048, 480, 272);
/// ```
///
/// # Parameters
///
/// - `cx`: Center for horizontal viewport
/// - `cy`: Center for vertical viewport
/// - `width`: Width of viewport
/// - `height`: Height of viewport
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuViewport(cx: i32, cy: i32, width: i32, height: i32) {
    send_command_f(GeCommand::ViewportXScale, (width >> 1) as f32);
    send_command_f(GeCommand::ViewportYScale, ((-height) >> 1) as f32);
    send_command_f(GeCommand::ViewportXCenter, cx as f32);
    send_command_f(GeCommand::ViewportYCenter, cy as f32);
}

/// Draw bezier surface
///
/// # Parameters
///
/// - `vtype`: Vertex type
/// - `ucount`: Number of vertices used in the U direction
/// - `vcount`: Number of vertices used in the V direction
/// - `indices`: Pointer to index buffer
/// - `vertices`: Pointer to vertex buffer
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDrawBezier(
    v_type: VertexType,
    u_count: i32,
    v_count: i32,
    indices: *const c_void,
    vertices: *const c_void,
) {
    if !v_type.is_empty() {
        send_command_i(GeCommand::VertexType, v_type.bits());
    }

    if !indices.is_null() {
        send_command_i(GeCommand::Base, ((indices as u32) >> 8) as i32 & 0xf0000);
        send_command_i(GeCommand::Iaddr, (indices as i32) & 0xffffff);
    }

    if !vertices.is_null() {
        send_command_i(GeCommand::Base, ((vertices as u32) >> 8) as i32 & 0xf0000);
        send_command_i(GeCommand::Vaddr, (vertices as i32) & 0xffffff);
    }

    send_command_i(GeCommand::Bezier, (v_count << 8) | u_count);
}

/// Set dividing for patches (beziers and splines)
///
/// # Parameters
///
/// - `ulevel`: Number of division on u direction
/// - `vlevel`: Number of division on v direction
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuPatchDivide(ulevel: u32, vlevel: u32) {
    send_command_i(GeCommand::PatchDivision, ((vlevel << 8) | ulevel) as i32);
}

#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuPatchFrontFace(a0: u32) {
    send_command_i(GeCommand::PatchFacing, a0 as i32);
}

/// Set primitive for patches (beziers and splines)
///
/// # Parameters
///
/// - `prim`: Desired primitive type
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuPatchPrim(prim: PatchPrimitive) {
    match prim {
        PatchPrimitive::Points => send_command_i(GeCommand::PatchPrimitive, 2),
        PatchPrimitive::LineStrip => send_command_i(GeCommand::PatchPrimitive, 1),
        PatchPrimitive::TriangleStrip => send_command_i(GeCommand::PatchPrimitive, 0),
    }
}

#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDrawSpline(
    v_type: VertexType,
    u_count: i32,
    v_count: i32,
    u_edge: i32,
    v_edge: i32,
    indices: *const c_void,
    vertices: *const c_void,
) {
    if !v_type.is_empty() {
        send_command_i(GeCommand::VertexType, v_type.bits());
    }

    if !indices.is_null() {
        send_command_i(GeCommand::Base, ((indices as u32) >> 8) as i32 & 0xf0000);
        send_command_i(GeCommand::Iaddr, (indices as i32) & 0xffffff);
    }

    if !vertices.is_null() {
        send_command_i(GeCommand::Base, ((vertices as u32) >> 8) as i32 & 0xf0000);
        send_command_i(GeCommand::Vaddr, (vertices as i32) & 0xffffff);
    }

    send_command_i(
        GeCommand::Spline,
        (v_edge << 18) | (u_edge << 16) | (v_count << 8) | u_count,
    );
}

/// Set transform matrices
///
/// # Parameters
///
/// - `type`: Which matrix-type to set
/// - `matrix`: Matrix to load
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuSetMatrix(type_: MatrixMode, matrix: &ScePspFMatrix4) {
    let fmatrix = matrix as *const _ as *const f32;

    match type_ {
        MatrixMode::Projection => {
            send_command_f(GeCommand::ProjMatrixNumber, 0.0);
            for i in 0..16 {
                send_command_f(GeCommand::ProjMatrixData, *fmatrix.offset(i));
            }
        }

        MatrixMode::View => {
            send_command_f(GeCommand::ViewMatrixNumber, 0.0);
            for i in 0..4 {
                for j in 0..3 {
                    send_command_f(GeCommand::ViewMatrixData, *fmatrix.offset(j + i * 4));
                }
            }
        }

        MatrixMode::Model => {
            send_command_f(GeCommand::WorldMatrixNumber, 0.0);
            for i in 0..4 {
                for j in 0..3 {
                    send_command_f(GeCommand::WorldMatrixData, *fmatrix.offset(j + i * 4));
                }
            }
        }

        MatrixMode::Texture => {
            send_command_f(GeCommand::TGenMatrixNumber, 0.0);
            for i in 0..4 {
                for j in 0..3 {
                    send_command_f(GeCommand::TGenMatrixData, *fmatrix.offset(j + i * 4));
                }
            }
        }
    }
}

/// Specify skinning matrix entry
///
/// To enable vertex skinning, use `VertexType::WEIGHTSn`, where n is between
/// 1-8, and pass an applicable `VertexType::WEIGHT_?BIT` declaration. This will
/// change the amount of weights passed in the vertex array, and by setting the
/// skinning matrices, you will multiply each vertex every weight and vertex
/// passed.
///
/// Please see `VertexType` for vertex format information.
///
/// # Parameters
///
/// - `index`: Skinning matrix index (0-7)
/// - `matrix`: Matrix to set
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuBoneMatrix(index: u32, matrix: &ScePspFMatrix4) {
    send_command_i(GeCommand::BoneMatrixNumber, index as i32 * 12); // 3 * 4 matrix

    send_command_f(GeCommand::BoneMatrixData, matrix.x.x);
    send_command_f(GeCommand::BoneMatrixData, matrix.x.y);
    send_command_f(GeCommand::BoneMatrixData, matrix.x.z);

    send_command_f(GeCommand::BoneMatrixData, matrix.y.x);
    send_command_f(GeCommand::BoneMatrixData, matrix.y.y);
    send_command_f(GeCommand::BoneMatrixData, matrix.y.z);

    send_command_f(GeCommand::BoneMatrixData, matrix.z.x);
    send_command_f(GeCommand::BoneMatrixData, matrix.z.y);
    send_command_f(GeCommand::BoneMatrixData, matrix.z.z);

    send_command_f(GeCommand::BoneMatrixData, matrix.w.x);
    send_command_f(GeCommand::BoneMatrixData, matrix.w.y);
    send_command_f(GeCommand::BoneMatrixData, matrix.w.z);
}

/// Specify morph weight entry
///
/// To enable vertex morphing, use `VertexType::VERTICESn`, where n is between
/// 1-8. This will change the amount of vertices passed in the vertex array,
/// and by setting the morph weights for every vertex entry in the array,
/// you can blend between them.
///
/// Please see `VertexType` for vertex format information.
///
/// # Parameters
///
/// - `index`: Morph weight index (0-7)
/// - `weight`: Weight to set
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuMorphWeight(index: i32, weight: f32) {
    let cmd = match index {
        0 => GeCommand::MorphWeight0,
        1 => GeCommand::MorphWeight1,
        2 => GeCommand::MorphWeight2,
        3 => GeCommand::MorphWeight3,
        4 => GeCommand::MorphWeight4,
        5 => GeCommand::MorphWeight5,
        6 => GeCommand::MorphWeight6,
        7 => GeCommand::MorphWeight7,
        _ => core::intrinsics::unreachable(),
    };

    send_command_f(cmd, weight);
}

#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDrawArrayN(
    primitive_type: GuPrimitive,
    v_type: VertexType,
    count: i32,
    a3: i32,
    indices: *const c_void,
    vertices: *const c_void,
) {
    if !v_type.is_empty() {
        send_command_i(GeCommand::VertexType, v_type.bits());
    }

    if !indices.is_null() {
        send_command_i(GeCommand::Base, ((indices as u32) >> 8) as i32 & 0xf0000);
        send_command_i(GeCommand::Iaddr, indices as i32 & 0xffffff);
    }

    if !vertices.is_null() {
        send_command_i(GeCommand::Base, ((vertices as u32) >> 8) as i32 & 0xf0000);
        send_command_i(GeCommand::Vaddr, vertices as i32 & 0xffffff);
    }

    if a3 > 0 {
        // PSPSDK: TODO: not sure about this loop, might be off. review
        for _ in 1..a3 {
            send_command_i(GeCommand::Prim, ((primitive_type as i32) << 16) | count);
        }

        send_command_i_stall(GeCommand::Prim, ((primitive_type as i32) << 16) | count);
    }
}

static mut CHAR_BUFFER_USED: u32 = 0;
static mut CHAR_BUFFER: [DebugCharStruct; 2048] = [DebugCharStruct {
    x: 0,
    y: 0,
    color: 0,
    character: b'\0',
    unused: [0, 0, 0],
}; 2048];
static FONT: [u8; 768] = *include_bytes!("./debugfont.bin");

#[repr(C, packed)]
#[derive(Copy, Clone)]
struct DebugCharStruct {
    x: i32,
    y: i32,
    color: u32,
    character: u8,
    unused: [u8; 3],
}

/// Add characters to an internal buffer for later printing with sceGuDebugFlush
///
/// # Parameters
///
/// - `x`: Horizontal start position
/// - `y`: Vertical start position
/// - `color`: Text color, ABGR
/// - `msg`: C-style string
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDebugPrint(x: i32, mut y: i32, mut color: u32, mut msg: *const u8) {
    let mut cur_char: u8;
    let mut uVar1: u32;
    let iVar2: i32;
    let mut cur_x: i32;
    let mut char_struct_ptr: *mut DebugCharStruct =
        addr_of_mut!(CHAR_BUFFER).cast::<DebugCharStruct>();

    let mut i = CHAR_BUFFER_USED;
    if i >= 0x3ff {
        return;
    }
    uVar1 = color >> 8 & 0xff;
    let uVar3 = color >> 16 & 0xff;
    let iVar4 = (uVar3 >> 3) as i32;
    cur_x = x;
    match DRAW_BUFFER.pixel_size {
        DisplayPixelFormat::Psm5650 => {
            iVar2 = (uVar1 as i32) >> 2;
            uVar1 = (iVar4 as u32) << 0xb;
            uVar1 = (uVar1 | iVar2 as u32) << 5;
            color = (color & 0xff) >> 3;
            color |= uVar1;
        }
        DisplayPixelFormat::Psm5551 => {
            iVar2 = (uVar1 >> 3) as i32;
            uVar1 = ((color >> 24) >> 7) << 0xf | (iVar4 as u32) << 10;
            uVar1 = (uVar1 | iVar2 as u32) << 5;
            color = (color & 0xff) >> 3;
            color |= uVar1;
        }
        DisplayPixelFormat::Psm8888 => {}
        DisplayPixelFormat::Psm4444 => {
            uVar1 = ((color >> 0x18) >> 4) << 0xc | (uVar3 >> 4) << 8 | (uVar1 >> 4) << 4;
            color &= 0xff >> 4;
            color |= uVar1;
        }
    }
    cur_char = *msg;
    while cur_char != b'\0' {
        if cur_char == b'\n' {
            y += 8;
            cur_x = x;
        } else {
            (*char_struct_ptr).x = cur_x;
            i += 1;
            (*char_struct_ptr).character = cur_char - 0x20;
            (*char_struct_ptr).y = y;
            (*char_struct_ptr).color = color;
            char_struct_ptr = (char_struct_ptr as u32 + 16) as *mut DebugCharStruct;
            cur_x += 8;
        }
        msg = msg.add(1);
        cur_char = *msg;
    }
    CHAR_BUFFER_USED = i;
}

/// Flush character buffer created by sceGuDebugPrint to the draw buffer
#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn sceGuDebugFlush() {
    let edram_address = GE_EDRAM_ADDRESS;
    let mut pixel_size: DisplayPixelFormat;
    let mut frame_width: i32;
    let mut frame_buffer: *mut c_void;
    let draw_buffer_height = DRAW_BUFFER.height;
    let mut char_index: i32;
    let mut pos: i32;
    let mut x_pixel_counter: i32;
    let mut glyph_pos: u32 = 0;
    let mut color: u32;
    let mut font_glyph: u32 = 0;
    let mut y_pixel_counter: i32;
    let mut x: i32;
    let mut char_buffer_used = CHAR_BUFFER_USED;
    let mut y: i32;
    let mut char_struct_ptr: *mut DebugCharStruct =
        addr_of_mut!(CHAR_BUFFER).cast::<DebugCharStruct>();

    if char_buffer_used != 0 {
        loop {
            frame_buffer = DRAW_BUFFER.frame_buffer;
            frame_width = DRAW_BUFFER.frame_width;
            pixel_size = DRAW_BUFFER.pixel_size;
            y = (*char_struct_ptr).y;
            x = (*char_struct_ptr).x;
            if (y + 7 < draw_buffer_height)
                && (((x + 7 < DRAW_BUFFER.width) as i32 & !y >> 0x1f) != 0)
                && -1 < x
            {
                color = (*char_struct_ptr).color;
                char_index = ((*char_struct_ptr).character) as i32 * 8;
                y_pixel_counter = 0;
                loop {
                    if y_pixel_counter == 0 {
                        font_glyph =
                            *(((&FONT as *const _ as u32) + char_index as u32) as *const u32);
                        glyph_pos = 1;
                    } else if y_pixel_counter == 4 {
                        font_glyph =
                            *(((&FONT as *const _ as u32) + 4 + char_index as u32) as *const u32);
                        glyph_pos = 1
                    }
                    x_pixel_counter = 7;
                    pos = x + (y + y_pixel_counter) * frame_width;
                    pos = pos * 4 + edram_address as i32 + frame_buffer as i32;
                    loop {
                        match pixel_size {
                            DisplayPixelFormat::Psm8888 => {
                                if font_glyph & glyph_pos != 0 {
                                    *((pos as u32 + 0x4000_0000) as *mut u32) = color;
                                }
                            }
                            _ => {
                                *((pos as u32 + 0x4000_0002) as *mut u16) = color as u16;
                            }
                        }
                        x_pixel_counter -= 1;
                        glyph_pos <<= 1;
                        pos += 4;
                        if x_pixel_counter <= -1 {
                            break;
                        }
                    }
                    y_pixel_counter += 1;
                    if 8 <= y_pixel_counter {
                        break;
                    }
                }
            }
            char_buffer_used -= 1;
            char_struct_ptr = ((char_struct_ptr as u32) + 16) as *mut DebugCharStruct;
            if char_buffer_used == 0 {
                break;
            }
        }
        CHAR_BUFFER_USED = 0;
    }
}