aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Windows64/Iggy/gdraw/gdraw_gl_shared.inl
blob: f134b7407c8bfeb7e9c0c5516aa1cca5a2885bd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
// gdraw_gl_shared.inl - copyright 2012 RAD Game Tools
//
// This file implements the part of the Iggy graphics driver layer shared between
// GL and GL ES 2 (which is most of it). It heavily depends on a bunch of typedefs,
// #defines and some utility functions that need to be set up correctly for the GL
// version being targeted. It also targets a kind of pseudo-GL 2.0; the platform
// implementation has to set up some #defines and perform extra initialization
// work if we go through extensions instead. This is all a bit ugly, but much
// easier to maintain than the original solution, where we just kept two almost
// identical versions of this code.

/////////////////////////////////////////////////////////////
//
// common code shared by all GDraw implemetations
//

// The native handle type holds resource handles and a coarse description.
typedef union {
   // handle that is a texture
   struct {
      GLuint gl;
      GLuint gl_renderbuf;
      U32 w:24;
      U32 nonpow2:8;
      U32 h:24;
      U32 reserved:8;
   } tex;

   // handle that is a vertex buffer
   struct {
      GLuint base;
      GLuint indices;
   } vbuf;
} GDrawNativeHandle;

#include "gdraw_shared.inl"

// max rendertarget stack depth. this depends on the extent to which you
// use filters and non-standard blend modes, and how nested they are.
#define MAX_RENDER_STACK_DEPTH             8         // Iggy is hardcoded to a limit of 16... probably 1-3 is realistic
#define AATEX_SAMPLER                      3         // sampler that aa_tex gets set in
#define QUAD_IB_COUNT                      4096      // quad index buffer has indices for this many quads

#define ASSERT_COUNT(a,b)                  ((a) == (b) ? (b) : -1)

///////////////////////////////////////////////////////////////////////////////
//
//  debugging/validation
//

static RADINLINE void break_on_err(GLint e)
{
#ifdef _DEBUG
   if (e) {
      RR_BREAK();
   }
#endif
}

static void report_err(GLint e)
{
   break_on_err(e);
   IggyGDrawSendWarning(nullptr, "OpenGL glGetError error");
}

static void compilation_err(const char *msg)
{
   error_msg_platform_specific(msg);
   report_err(GL_INVALID_VALUE);
}

static void eat_gl_err(void)
{
   while (glGetError() != GL_NO_ERROR);
}

static void opengl_check(void)
{
#ifdef _DEBUG
   GLint e = glGetError();
   if (e != GL_NO_ERROR) {
      report_err(e);
      eat_gl_err();
   }
#endif
}

static U32 is_pow2(S32 n)
{
   return ((U32) n & (U32) (n-1)) == 0;
}

///////////////////////////////////////////////////////////////////////////////
//
//  GDraw
//
//  This data structure stores all the data for the GDraw, just to keep
//  it a bit cleaner instead of storing in globals, even though GDraw is
//  a singleton.

// fragment and vertex program

// The mac doesn't use extensions for the functions dealing with programs, and the non-extension versions
// take GLuint instead of GLhandle. The mac defines GDrawGLProgram to GLuint before including gdraw_gl_shared.inl
// to account for this. 
#ifndef GDrawGLProgram
#define GDrawGLProgram GLhandle
#endif

typedef struct ProgramWithCachedVariableLocations
{
   GDrawGLProgram program;
   GLint    vars[2][MAX_VARS];
} ProgramWithCachedVariableLocations;

// render-stack state
typedef struct
{
   GDrawHandle *color_buffer;
   GDrawHandle *stencil_depth;
   S32 base_x, base_y, width, height;
   rrbool cached;
} GDrawFramebufferState;

// texture format description
typedef struct {
   U8 iggyfmt;       // IFT_FORMAT_*
   U8 blkx, blky;    // compressed block size in pixels (for compressed formats)
   U8 blkbytes;      // block bytes
   GLenum intfmt;    // GL internal format
   GLenum fmt;       // GL_TEXTURE_COMPRESSED for compressed formats!
   GLenum type;
} TextureFormatDesc;

static GDrawFunctions gdraw_funcs;

///////////////////////////////////////////////////////////////////////////////
//
//     GDraw data structure
//
//
// This is the primary rendering abstraction, which hides all
// the platform-specific rendering behavior from /G/. It is
// full of platform-specific graphics state, and also general
// graphics state so that it doesn't have to callback into /G/
// to get at that graphics state.

static struct
{
   S32 multisampling;  // number of samples if multisampling (always 0 if no GDRAW_MULTISAMPLING)

   S32 vx,vy;   // viewport width/height in pixels
   S32 fw,fh;   // full width/height of bound rendertarget
   S32 tw,th;   // actual width/height of current tile
   S32 tpw,tph; // width/height of padded version of tile

   // tile origin location (without and with padding)
   rrbool tile_enabled;
   S32 tx0,ty0;
   S32 tx0p,ty0p;

   // if we're in the middle of rendering a blur, certain viewport-related
   // functions have to behave differently, so they check this flag
   rrbool in_blur;

   F32 projection[4]; // scalex, scaley, transx, transy

   // conversion from worldspace to viewspace <0,0>..<w,h> -- no translation or rotation
   F32 world_to_pixel[2];

   // 3d transformation
   F32 xform_3d[3][4];
   rrbool use_3d;

   // render-state stack for 'temporary' rendering
   GDrawFramebufferState frame[MAX_RENDER_STACK_DEPTH];
   GDrawFramebufferState *cur;

   // texture and vertex buffer pools
   GDrawHandleCache *texturecache;
   GDrawHandleCache *vbufcache;

   // GL_EXT_separate_shader_objects isn't sufficiently standard,
   // so we have to bind every vertex shader to every fragment shader

   // raw vertex shaders
   GLuint vert[GDRAW_vformat__count];

   // fragment shaders with vertex shaders
   ProgramWithCachedVariableLocations fprog[GDRAW_TEXTURE__count][3][3]; // [tex0mode][additive][vformat]
   ProgramWithCachedVariableLocations ihud[2];

   // fragment shaders with fixed-function
   ProgramWithCachedVariableLocations exceptional_blend[GDRAW_BLENDSPECIAL__count];
   ProgramWithCachedVariableLocations filter_prog[2][16];
   ProgramWithCachedVariableLocations blur_prog[MAX_TAPS+1];
   ProgramWithCachedVariableLocations colormatrix;
   ProgramWithCachedVariableLocations manual_clear;

   // render targets

   // these two lines must be adjacent because of how rendertargets works
   GDrawHandleCache      rendertargets;
   GDrawHandle           rendertarget_handles[MAX_RENDER_STACK_DEPTH]; // not -1, because we use +1 to initialize

   gswf_recti            rt_valid[MAX_RENDER_STACK_DEPTH + 1];
   GDrawHandle           stencil_depth;

   // size of our render targets
   S32 frametex_width, frametex_height;

   // framebuffer object used for render-to-texture
   GLuint framebuffer_stack_object;

   // framebuffer object used to copy from MSAA renderbuffer to texture
   GLuint framebuffer_copy_to_texture;

   // framebuffer object used for main screen (set to non-0 to do render to texture)
   GLuint main_framebuffer;

   // antialias texture
   GLuint aa_tex;

   // canned quad indices
   GLuint quad_ib;

   // texture formats
   const TextureFormatDesc *tex_formats;

   // caps
   U32 has_conditional_non_power_of_two : 1; // non-power-of-2 supported, but only CLAMP_TO_EDGE and can't have mipmaps
   U32 has_packed_depth_stencil : 1;
   U32 has_depth24 : 1;
   U32 has_mapbuffer : 1;
   U32 has_texture_max_level : 1;

   // fake fence tracking for thrashing detection
   U64 frame_counter;
} *gdraw;

////////////////////////////////////////////////////////////////////////
//
//   General resource management for both textures and vertex buffers
//

// make a texture with reasonable default state
static void make_texture(GLuint tex)
{
   glBindTexture(GL_TEXTURE_2D, tex);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}

static void make_rendertarget(GDrawHandle *t, GLuint tex, GLenum int_type, GLenum ext_type, GLenum data_type, S32 w, S32 h, S32 size)
{
   glBindTexture(GL_TEXTURE_2D, tex);
   glTexImage2D(GL_TEXTURE_2D, 0, int_type, w, h, 0, ext_type, data_type, nullptr);
   make_texture(tex);
   glBindTexture(GL_TEXTURE_2D, 0);
}

static void api_free_resource(GDrawHandle *r)
{
   if (r->state == GDRAW_HANDLE_STATE_user_owned)
      return;

   if (!r->cache->is_vertex) {
      glDeleteTextures(1, &r->handle.tex.gl);
      if (r->handle.tex.gl_renderbuf && r->handle.tex.gl_renderbuf != r->handle.tex.gl)
         glDeleteRenderbuffers(1, &r->handle.tex.gl_renderbuf);
   } else {
      glDeleteBuffers(1, &r->handle.vbuf.base);
      glDeleteBuffers(1, &r->handle.vbuf.indices);
   }
   opengl_check();
}

static void RADLINK gdraw_UnlockHandles(GDrawStats *gstats)
{
   // since we're not using fences for this implementation, move all textures off the active list
   // if you're using fences, this is when the fence needs to actually occur
   gdraw_HandleCacheUnlockAll(gdraw->texturecache);
   gdraw_HandleCacheUnlockAll(gdraw->vbufcache);
}

////////////////////////////////////////////////////////////////////////
//
//   Texture creation/updating
//

extern GDrawTexture *gdraw_GLx_(WrappedTextureCreate)(S32 gl_texture_handle, S32 width, S32 height, rrbool has_mipmaps)
{
   GDrawStats stats={0};
   GDrawHandle *p = gdraw_res_alloc_begin(gdraw->texturecache, 0, &stats); // it may need to free one item to give us a handle
   GLint old;

   glGetIntegerv(GL_TEXTURE_BINDING_2D, &old);
   glBindTexture(GL_TEXTURE_2D, gl_texture_handle);
   if (has_mipmaps)
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
   else
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glBindTexture(GL_TEXTURE_2D, old);

   p->bytes = 0;
   p->handle.tex.gl = gl_texture_handle;
   p->handle.tex.w = width;
   p->handle.tex.h = height;
   p->handle.tex.nonpow2 = !(is_pow2(width) && is_pow2(height));
   gdraw_HandleCacheAllocateEnd(p, 0, nullptr, GDRAW_HANDLE_STATE_user_owned);
   return (GDrawTexture *) p;
}

extern void gdraw_GLx_(WrappedTextureChange)(GDrawTexture *tex, S32 new_gl_texture_handle, S32 new_width, S32 new_height, rrbool has_mipmaps)
{
   GDrawHandle *p = (GDrawHandle *) tex;
   GLint old;

   glGetIntegerv(GL_TEXTURE_BINDING_2D, &old);
   glBindTexture(GL_TEXTURE_2D, new_gl_texture_handle);
   if (has_mipmaps)
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
   else
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glBindTexture(GL_TEXTURE_2D, old);

   p->handle.tex.gl = new_gl_texture_handle;
   p->handle.tex.w = new_width;
   p->handle.tex.h = new_height;
   p->handle.tex.nonpow2 = !(is_pow2(new_width) && is_pow2(new_height));
}

extern void gdraw_GLx_(WrappedTextureDestroy)(GDrawTexture *tex)
{
   GDrawStats stats={0};
   gdraw_res_free((GDrawHandle *) tex, &stats);
}

static void RADLINK gdraw_SetTextureUniqueID(GDrawTexture *tex, void *old_id, void *new_id)
{
   GDrawHandle *p = (GDrawHandle *) tex;
   // if this is still the handle it's thought to be, change the owner;
   // if the owner *doesn't* match, then they're changing a stale handle, so ignore
   if (p->owner == old_id)
      p->owner = new_id;
}

static rrbool RADLINK gdraw_MakeTextureBegin(void *owner, S32 width, S32 height, gdraw_texture_format format, U32 flags, GDraw_MakeTexture_ProcessingInfo *p, GDrawStats *gstats)
{
   S32 size=0, asize, stride;
   GDrawHandle *t = nullptr;
   opengl_check();

   stride = width;
   if (format == GDRAW_TEXTURE_FORMAT_rgba32) stride *= 4;
   size = stride*height;

   asize = size;
   if (flags & GDRAW_MAKETEXTURE_FLAGS_mipmap) asize = asize*4/3;

   t = gdraw_res_alloc_begin(gdraw->texturecache, asize, gstats);
   if (!t)
      return IGGY_RESULT_Error_GDraw;

   glGenTextures(1, &t->handle.tex.gl);

   p->texture_data = IggyGDrawMalloc(size);
   if (!p->texture_data) {
      gdraw_HandleCacheAllocateFail(t);
      IggyGDrawSendWarning(nullptr, "GDraw malloc for texture data failed");
      return false;
   }
   
   t->handle.tex.w = width;
   t->handle.tex.h = height;
   t->handle.tex.nonpow2 = !(is_pow2(width) && is_pow2(height));

   p->num_rows = height;
   p->p0 = t;
   p->p1 = owner;
   p->stride_in_bytes = stride;
   p->texture_type = GDRAW_TEXTURE_TYPE_rgba;
   p->i0 = format;
   p->i1 = flags;
   p->i2 = width;
   p->i3 = height;
   p->i4 = asize;
   opengl_check();
   return true;
}

static GDrawTexture * RADLINK gdraw_MakeTextureEnd(GDraw_MakeTexture_ProcessingInfo *p, GDrawStats *stats)
{
   gdraw_texture_format format = (gdraw_texture_format) p->i0;
   S32 flags = p->i1;
   rrbool mipmap = (flags & GDRAW_MAKETEXTURE_FLAGS_mipmap) != 0;
   S32 width = p->i2, height = p->i3;
   GLuint z,e; 
   GDrawHandle *t = (GDrawHandle *) p->p0;

   z = t->handle.tex.gl;
   assert(z != 0);

   make_texture(z);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

   if (format == GDRAW_TEXTURE_FORMAT_font)
      glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, p->texture_data);
   else
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, p->texture_data);
   e = glGetError();
   break_on_err(e);

   if (mipmap)
      glGenerateMipmap(GL_TEXTURE_2D);
   if (!e) e = glGetError();

   if (e != 0) {
      gdraw_HandleCacheAllocateFail(t);
      IggyGDrawSendWarning(nullptr, "GDraw OpenGL error creating texture");
      eat_gl_err();
      return nullptr;
   } else {
      gdraw_HandleCacheAllocateEnd(t, p->i4, p->p1, (flags & GDRAW_MAKETEXTURE_FLAGS_never_flush) ? GDRAW_HANDLE_STATE_pinned : GDRAW_HANDLE_STATE_locked);
      stats->nonzero_flags |= GDRAW_STATS_alloc_tex;
      stats->alloc_tex += 1;
      stats->alloc_tex_bytes += p->i4;
   }

   // default wrap mode is clamp to edge
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

   if (mipmap)
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
   else
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

   IggyGDrawFree(p->texture_data);

   opengl_check();
   return (GDrawTexture *) t;
}

static rrbool RADLINK gdraw_UpdateTextureBegin(GDrawTexture *tex, void *unique_id, GDrawStats *stats)
{
   RR_UNUSED_VARIABLE(stats);
   return gdraw_HandleCacheLock((GDrawHandle *) tex, unique_id);
}

static void   RADLINK gdraw_UpdateTextureRect(GDrawTexture *tex, void *unique_id, S32 x, S32 y, S32 stride, S32 w, S32 h, U8 *data, gdraw_texture_format format)
{
   glBindTexture(GL_TEXTURE_2D, ((GDrawHandle *) tex)->handle.tex.gl);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   // @TODO: use 'stride'
   glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, (format == GDRAW_TEXTURE_FORMAT_font) ? GL_ALPHA : GL_RGBA, GL_UNSIGNED_BYTE, data);
   opengl_check();
}

static void RADLINK gdraw_UpdateTextureEnd(GDrawTexture *tex, void *unique_id, GDrawStats *stats)
{
   gdraw_HandleCacheUnlock((GDrawHandle *) tex);
}

static void RADLINK gdraw_FreeTexture(GDrawTexture *tt, void *unique_id, GDrawStats *gstats)
{
   GDrawHandle *t = (GDrawHandle *) tt;
   assert(t != nullptr);
   if (t->owner == unique_id || unique_id == nullptr) {
      if (t->cache == &gdraw->rendertargets) {
         gdraw_HandleCacheUnlock(t);
         // cache it by simply not freeing it
         return;
      }

      gdraw_res_free(t, gstats);
   }
}

static rrbool RADLINK gdraw_TryToLockTexture(GDrawTexture *t, void *unique_id, GDrawStats *gstats)
{
   RR_UNUSED_VARIABLE(gstats);
   return gdraw_HandleCacheLock((GDrawHandle *) t, unique_id);
}

static void RADLINK gdraw_DescribeTexture(GDrawTexture *tex, GDraw_Texture_Description *desc)
{
   GDrawHandle *p = (GDrawHandle *) tex;
   desc->width = p->handle.tex.w;
   desc->height = p->handle.tex.h;
   desc->size_in_bytes = p->bytes;
}

static void RADLINK gdraw_SetAntialiasTexture(S32 width, U8 *rgba)
{
   if (!gdraw->aa_tex)
      glGenTextures(1, &gdraw->aa_tex);

   make_texture(gdraw->aa_tex);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba);
   opengl_check();
}

////////////////////////////////////////////////////////////////////////
//
//   Vertex buffer creation/deletion
//

static rrbool RADLINK gdraw_MakeVertexBufferBegin(void *unique_id, gdraw_vformat vformat, S32 vbuf_size, S32 ibuf_size, GDraw_MakeVertexBuffer_ProcessingInfo *p, GDrawStats *gstats)
{
   GLuint e;
   GDrawHandle *vb;
   opengl_check();
   vb = gdraw_res_alloc_begin(gdraw->vbufcache, vbuf_size + ibuf_size, gstats);
   if (!vb) {
      IggyGDrawSendWarning(nullptr, "GDraw out of vertex buffer memory");
      return false;
   }

   e = glGetError();
   vb->handle.vbuf.base = 0;
   vb->handle.vbuf.indices = 0;
   glGenBuffers(1, &vb->handle.vbuf.base);
   glGenBuffers(1, &vb->handle.vbuf.indices);
   glBindBuffer(GL_ARRAY_BUFFER, vb->handle.vbuf.base);
   glBufferData(GL_ARRAY_BUFFER, vbuf_size, nullptr, GL_STATIC_DRAW);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vb->handle.vbuf.indices);
   glBufferData(GL_ELEMENT_ARRAY_BUFFER, ibuf_size, nullptr, GL_STATIC_DRAW);
   if (!e) e = glGetError();
   if (e != GL_NO_ERROR) {
      glBindBuffer(GL_ARRAY_BUFFER, 0);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
      glDeleteBuffers(1, &vb->handle.vbuf.base);
      glDeleteBuffers(1, &vb->handle.vbuf.indices);
      gdraw_HandleCacheAllocateFail(vb);
      eat_gl_err();
      IggyGDrawSendWarning(nullptr, "GDraw OpenGL vertex buffer creation failed");
      return false;
   }

   p->i0 = vbuf_size;
   p->i1 = ibuf_size;
   p->p0 = vb;
   p->p1 = unique_id;
   
   if (!gdraw->has_mapbuffer) {
      p->vertex_data = IggyGDrawMalloc(vbuf_size);
      p->vertex_data_length = vbuf_size;
      p->index_data = IggyGDrawMalloc(ibuf_size);
      p->index_data_length = ibuf_size;

      // check for out of memory conditions
      if (!p->vertex_data || !p->index_data) {
         if (p->vertex_data)  IggyGDrawFree(p->vertex_data);
         if (p->index_data)   IggyGDrawFree(p->index_data);
         IggyGDrawSendWarning(nullptr, "GDraw malloc for vertex buffer temporary memory failed");
         return false;
      }
   } else {
      p->vertex_data = (U8 *)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
      p->vertex_data_length = vbuf_size;

      p->index_data = (U8 *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
      p->index_data_length = ibuf_size;
   }

   opengl_check();
   return true;
}

static rrbool RADLINK gdraw_MakeVertexBufferMore(GDraw_MakeVertexBuffer_ProcessingInfo *p)
{
   assert(0);
   return false;
}

static GDrawVertexBuffer * RADLINK gdraw_MakeVertexBufferEnd(GDraw_MakeVertexBuffer_ProcessingInfo *p, GDrawStats *stats)
{
   GDrawHandle *vb = (GDrawHandle *) p->p0;
   rrbool ok = true;
   GLuint e;

   if (!gdraw->has_mapbuffer) {
      glBufferData(GL_ARRAY_BUFFER, p->i0, p->vertex_data, GL_STATIC_DRAW);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, p->i1, p->index_data, GL_STATIC_DRAW);
      IggyGDrawFree(p->vertex_data);
      IggyGDrawFree(p->index_data);
   } else {
      if (!glUnmapBuffer(GL_ARRAY_BUFFER)) ok = false;
      if (!glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER)) ok = false;
   }

   glBindBuffer(GL_ARRAY_BUFFER, 0);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

   e = glGetError();
   if (!ok || e != GL_NO_ERROR) {
      glDeleteBuffers(1, &vb->handle.vbuf.base);
      glDeleteBuffers(1, &vb->handle.vbuf.indices);
      gdraw_HandleCacheAllocateFail(vb);
      eat_gl_err();
      return nullptr;
   } else
      gdraw_HandleCacheAllocateEnd(vb, p->i0 + p->i1, p->p1, GDRAW_HANDLE_STATE_locked);

   opengl_check();
   return (GDrawVertexBuffer *) vb;
}

static rrbool RADLINK gdraw_TryToLockVertexBuffer(GDrawVertexBuffer *vb, void *unique_id, GDrawStats *stats)
{
   RR_UNUSED_VARIABLE(stats);
   return gdraw_HandleCacheLock((GDrawHandle *) vb, unique_id);
}

static void RADLINK gdraw_FreeVertexBuffer(GDrawVertexBuffer *vb, void *unique_id, GDrawStats *stats)
{
   GDrawHandle *h = (GDrawHandle *) vb;
   assert(h != nullptr);
   if (h->owner == unique_id)
      gdraw_res_free(h, stats);
}

static void RADLINK gdraw_DescribeVertexBuffer(GDrawVertexBuffer *vbuf, GDraw_VertexBuffer_Description *desc)
{
   GDrawHandle *p = (GDrawHandle *) vbuf;
   desc->size_in_bytes = p->bytes;
}

////////////////////////////////////////////////////////////////////////
//
//   Create/free (or cache) render targets
//

static GDrawHandle *get_rendertarget_texture(int width, int height, void *owner, GDrawStats *gstats)
{
   S32 size;
   GDrawHandle *t;
   opengl_check();
   t = gdraw_HandleCacheGetLRU(&gdraw->rendertargets);
   if (t) {
      gdraw_HandleCacheLock(t, (void *) (UINTa) 1);
      return t;
   }

   size = gdraw->frametex_width * gdraw->frametex_height * 4;
   t = gdraw_res_alloc_begin(gdraw->texturecache, size, gstats);
   if (!t) return t;

   glGenTextures(1, &t->handle.tex.gl);
   make_rendertarget(t, t->handle.tex.gl, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, width, height, 4);
   t->handle.tex.w = gdraw->frametex_width;
   t->handle.tex.h = gdraw->frametex_height;
   t->handle.tex.nonpow2 = 1; // assume all rendertargets are non-pow2 for consistency
   gstats->nonzero_flags |= GDRAW_STATS_alloc_tex;
   gstats->alloc_tex += 1;
   gstats->alloc_tex_bytes += size;
   opengl_check();
   gdraw_HandleCacheAllocateEnd(t, size, owner, GDRAW_HANDLE_STATE_locked);

   return t;
}

static GDrawHandle *get_color_rendertarget(GDrawStats *gstats)
{
   S32 size;
   GDrawHandle *t;
   opengl_check();
   t = gdraw_HandleCacheGetLRU(&gdraw->rendertargets);
   if (t) {
      gdraw_HandleCacheLock(t, (void *) (UINTa) 1);
      return t;
   }

   // ran out of RTs, allocate a new one
   size = gdraw->frametex_width * gdraw->frametex_height * 4;
   if (gdraw->rendertargets.bytes_free < size) {
      IggyGDrawSendWarning(nullptr, "GDraw exceeded available rendertarget memory");
      return nullptr;
   }

   t = gdraw_HandleCacheAllocateBegin(&gdraw->rendertargets);
   if (!t) {
      IggyGDrawSendWarning(nullptr, "GDraw exceeded available rendertarget handles");
      return t;
   }

   glGenTextures(1, &t->handle.tex.gl);
   make_rendertarget(t, t->handle.tex.gl, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, gdraw->frametex_width,gdraw->frametex_height, 4);
   t->handle.tex.w = gdraw->frametex_width;
   t->handle.tex.h = gdraw->frametex_height;
   t->handle.tex.nonpow2 = 1; // assume all rendertargets are non-pow2 for consistency

#ifdef GDRAW_MULTISAMPLING
   if (gdraw->multisampling) {
      glGenRenderbuffers(1, &t->handle.tex.gl_renderbuf);
      glBindRenderbuffer(GL_RENDERBUFFER, t->handle.tex.gl_renderbuf);
      glRenderbufferStorageMultisample(GL_RENDERBUFFER, gdraw->multisampling, GL_RGBA, gdraw->frametex_width, gdraw->frametex_height);
      glBindRenderbuffer(GL_RENDERBUFFER, 0);
   }
#endif
   opengl_check();

   gdraw_HandleCacheAllocateEnd(t, size, (void *) (UINTa) 1, GDRAW_HANDLE_STATE_locked);
   gstats->nonzero_flags |= GDRAW_STATS_alloc_tex;
   gstats->alloc_tex += gdraw->multisampling ? 2 : 1;
   gstats->alloc_tex_bytes += (1 + gdraw->multisampling) * size;

   return t;
}

static GDrawHandle *get_depthstencil_renderbuffer(GDrawStats *gstats)
{
   if (!gdraw->stencil_depth.handle.tex.gl) {
      gstats->nonzero_flags |= GDRAW_STATS_alloc_tex;
      gstats->alloc_tex += 1;

#ifdef GDRAW_MULTISAMPLING
      if (gdraw->multisampling) {
         glGenRenderbuffers(1, &gdraw->stencil_depth.handle.tex.gl);
         glBindRenderbuffer(GL_RENDERBUFFER, gdraw->stencil_depth.handle.tex.gl);
         glRenderbufferStorageMultisample(GL_RENDERBUFFER, gdraw->multisampling, GL_DEPTH24_STENCIL8, gdraw->frametex_width, gdraw->frametex_height);

         gstats->alloc_tex_bytes += gdraw->multisampling * 4 * gdraw->frametex_width * gdraw->frametex_height;
      } else {
#endif
         if (gdraw->has_packed_depth_stencil) {
            glGenRenderbuffers(1, &gdraw->stencil_depth.handle.tex.gl);
            glBindRenderbuffer(GL_RENDERBUFFER, gdraw->stencil_depth.handle.tex.gl);
            glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, gdraw->frametex_width, gdraw->frametex_height);

            gdraw->stencil_depth.handle.tex.gl_renderbuf = gdraw->stencil_depth.handle.tex.gl;
         } else {
            // this path is mainly for the iOS simulator
            glGenRenderbuffers(1, &gdraw->stencil_depth.handle.tex.gl);
            glBindRenderbuffer(GL_RENDERBUFFER, gdraw->stencil_depth.handle.tex.gl);
            glRenderbufferStorage(GL_RENDERBUFFER, gdraw->has_depth24 ? GL_DEPTH_COMPONENT24 : GL_DEPTH_COMPONENT16, gdraw->frametex_width, gdraw->frametex_height);

            glGenRenderbuffers(1, &gdraw->stencil_depth.handle.tex.gl_renderbuf);
            glBindRenderbuffer(GL_RENDERBUFFER, gdraw->stencil_depth.handle.tex.gl_renderbuf);
            glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, gdraw->frametex_width, gdraw->frametex_height);
         }

         gstats->alloc_tex_bytes += 4 * gdraw->frametex_width * gdraw->frametex_height;
#ifdef GDRAW_MULTISAMPLING
      }
#endif

      glBindRenderbuffer(GL_RENDERBUFFER, 0);
      opengl_check();
   }
   return &gdraw->stencil_depth;
}

static void flush_rendertargets(GDrawStats *stats)
{
   gdraw_res_flush(&gdraw->rendertargets, stats);

   if (gdraw->stencil_depth.handle.tex.gl_renderbuf &&
       gdraw->stencil_depth.handle.tex.gl_renderbuf != gdraw->stencil_depth.handle.tex.gl) {
      glDeleteRenderbuffers(1, &gdraw->stencil_depth.handle.tex.gl_renderbuf);
      gdraw->stencil_depth.handle.tex.gl_renderbuf = 0;
   }

   if (gdraw->stencil_depth.handle.tex.gl) {
      glDeleteRenderbuffers(1, &gdraw->stencil_depth.handle.tex.gl);
      gdraw->stencil_depth.handle.tex.gl = 0;
   }
   opengl_check();
}

////////////////////////////////////////////////////////////////////////
//
//   Begin rendering for a frame
//

static void lazy_shader(ProgramWithCachedVariableLocations *ptr);

static RADINLINE void use_lazy_shader(ProgramWithCachedVariableLocations *prg)
{
   if (!prg->program)
      lazy_shader(prg); // already does a glUseProgram!
   else
      glUseProgram(prg->program);
}

static void set_viewport(void)
{
   if (gdraw->in_blur) {
      glViewport(0, 0, gdraw->tpw, gdraw->tph);
   } else if (gdraw->cur == gdraw->frame) {
      glViewport(gdraw->vx, gdraw->vy, gdraw->tw, gdraw->th);
   } else if (gdraw->cur->cached) {
      glViewport(0, 0, gdraw->cur->width, gdraw->cur->height);
   } else {
      glViewport(0, 0, gdraw->tpw, gdraw->tph);
      // we need to translate from naive pixel space to align a tile
   }
   opengl_check();
}

static void set_projection_raw(S32 x0, S32 x1, S32 y0, S32 y1)
{
   gdraw->projection[0] = 2.0f / (x1-x0);
   gdraw->projection[1] = 2.0f / (y1-y0);
   gdraw->projection[2] = (x1+x0)/(F32)(x0-x1);
   gdraw->projection[3] = (y1+y0)/(F32)(y0-y1);
}

static void set_projection(void)
{
   if (gdraw->in_blur)
      set_projection_raw(0, gdraw->tpw, gdraw->tph, 0);
   else if (gdraw->cur == gdraw->frame)
      set_projection_raw(gdraw->tx0, gdraw->tx0 + gdraw->tw, gdraw->ty0 + gdraw->th, gdraw->ty0);
   else if (gdraw->cur->cached)
      set_projection_raw(gdraw->cur->base_x, gdraw->cur->base_x + gdraw->cur->width, gdraw->cur->base_y, gdraw->cur->base_y + gdraw->cur->height);
   else
      set_projection_raw(gdraw->tx0p, gdraw->tx0p + gdraw->tpw, gdraw->ty0p + gdraw->tph, gdraw->ty0p);
}

static void clear_renderstate(void)
{
   clear_renderstate_platform_specific();

   // deactivate aa_tex
   glActiveTexture(GL_TEXTURE0 + AATEX_SAMPLER);
   glBindTexture(GL_TEXTURE_2D, 0);

   glColorMask(1,1,1,1);
   glDepthMask(GL_TRUE);

   glDisable(GL_CULL_FACE);
   glDisable(GL_BLEND);
   glDisable(GL_DEPTH_TEST);
   glDisable(GL_STENCIL_TEST);
   glDisable(GL_SCISSOR_TEST);
   glActiveTexture(GL_TEXTURE0);

   glUseProgram(0);
   opengl_check();
}

static void set_common_renderstate(void)
{
   clear_renderstate();

   // activate aa_tex
   glActiveTexture(GL_TEXTURE0 + AATEX_SAMPLER);
   glBindTexture(GL_TEXTURE_2D, gdraw->aa_tex);
   glActiveTexture(GL_TEXTURE0);
}

void gdraw_GLx_(SetTileOrigin)(S32 x, S32 y, U32 framebuffer)
{
   gdraw->vx = x;
   gdraw->vy = y;
   gdraw->main_framebuffer = framebuffer;
}

static void RADLINK gdraw_SetViewSizeAndWorldScale(S32 w, S32 h, F32 scalex, F32 scaley)
{
   memset(gdraw->frame, 0, sizeof(gdraw->frame));
   gdraw->cur = gdraw->frame;
   gdraw->fw = w;
   gdraw->fh = h;
   gdraw->world_to_pixel[0] = scalex;
   gdraw->world_to_pixel[1] = scaley;
}

static void RADLINK gdraw_Set3DTransform(F32 *mat)
{
   if (mat == nullptr)
      gdraw->use_3d = 0;
   else {
      gdraw->use_3d = 1;
      memcpy(gdraw->xform_3d, mat, sizeof(gdraw->xform_3d));
   }
}

// must include anything necessary for texture creation/update
static void RADLINK gdraw_RenderingBegin(void)
{
}
static void RADLINK gdraw_RenderingEnd(void)
{
}

static void RADLINK gdraw_RenderTileBegin(S32 x0, S32 y0, S32 x1, S32 y1, S32 pad, GDrawStats *gstats)
{
   opengl_check();

   if (x0 == 0 && y0 == 0 && x1 == gdraw->fw && y1 == gdraw->fh) {
      pad = 0;
      gdraw->tile_enabled = false;
   } else {
      gdraw->tile_enabled = true;
   }

   gdraw->tx0 = x0;
   gdraw->ty0 = y0;
   gdraw->tw = x1-x0;
   gdraw->th = y1-y0;
   gdraw->tpw = gdraw->tw + pad*2;
   gdraw->tph = gdraw->th + pad*2;
   // origin of padded region
   gdraw->tx0p = x0 - pad;
   gdraw->ty0p = y0 - pad;

   if (gdraw->tpw > gdraw->frametex_width || gdraw->tph > gdraw->frametex_height) {
      gdraw->frametex_width  = RR_MAX(gdraw->tpw, gdraw->frametex_width);
      gdraw->frametex_height = RR_MAX(gdraw->tph, gdraw->frametex_height);

      flush_rendertargets(gstats);
   }

   set_viewport();
   set_projection();
   set_common_renderstate();

   glBindFramebuffer(GL_FRAMEBUFFER, gdraw->main_framebuffer);
   opengl_check();
}

static void RADLINK gdraw_RenderTileEnd(GDrawStats *stats)
{
   clear_renderstate();
}

#define MAX_DEPTH_VALUE   (1 << 13)

static void RADLINK gdraw_GetInfo(GDrawInfo *d)
{
   GLint maxtex;

   opengl_check();
   glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxtex);

   d->num_stencil_bits = 8;
   d->max_id = MAX_DEPTH_VALUE-2;
   // for floating point depth, just use mantissa, e.g. 16-20 bits
   d->max_texture_size = maxtex;
   d->buffer_format = GDRAW_BFORMAT_vbib;
   d->shared_depth_stencil = 0;
   d->always_mipmap = 0;
   d->conditional_nonpow2 = gdraw->has_conditional_non_power_of_two;
   opengl_check();
}


////////////////////////////////////////////////////////////////////////
//
//   Enable/disable rendertargets in stack fashion
//

static void clear_with_rect(gswf_recti *region, rrbool clear_depth, GDrawStats *stats);

static void set_render_target_state(void)
{
   GLint h;
#ifdef GDRAW_MULTISAMPLING
   if (gdraw->multisampling) {
      glGetIntegerv(GL_FRAMEBUFFER_BINDING, &h);
      h = gdraw->cur->color_buffer ? gdraw->cur->color_buffer->handle.tex.gl_renderbuf : 0;
      glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, h);
      h = gdraw->cur->stencil_depth ? gdraw->cur->stencil_depth->handle.tex.gl : 0;
      glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT  , GL_RENDERBUFFER, h);
      glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, h);
   } else {
#endif
      h = gdraw->cur->color_buffer ? gdraw->cur->color_buffer->handle.tex.gl : 0;
      glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_TEXTURE_2D, h, 0);
      if (gdraw->cur->stencil_depth) {
         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,   GL_RENDERBUFFER, gdraw->cur->stencil_depth->handle.tex.gl);
         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, gdraw->cur->stencil_depth->handle.tex.gl_renderbuf);
      } else {
         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,   GL_RENDERBUFFER, 0);
         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
      }
#ifdef GDRAW_MULTISAMPLING
   }
#endif
   opengl_check();
}

static rrbool RADLINK gdraw_TextureDrawBufferBegin(gswf_recti *region, gdraw_texture_format format, U32 flags, void *owner, GDrawStats *gstats)
{
   GDrawFramebufferState *n = gdraw->cur+1;
   GDrawHandle *t;
   int k;
   if (gdraw->tw == 0 || gdraw->th == 0) {
      IggyGDrawSendWarning(nullptr, "GDraw got a request for an empty rendertarget");
      return false;
   }

   if (n >= &gdraw->frame[MAX_RENDER_STACK_DEPTH]) {
      IggyGDrawSendWarning(nullptr, "GDraw rendertarget nesting exceeded MAX_RENDER_STACK_DEPTH");
      return false;
   }

   if (owner) {
      t = get_rendertarget_texture(region->x1 - region->x0, region->y1 - region->y0, owner, gstats);
      if (!t) {
         IggyGDrawSendWarning(nullptr, "GDraw ran out of rendertargets for cacheAsBItmap");
         return false;
      }
   } else {
      t = get_color_rendertarget(gstats);
      if (!t) {
         IggyGDrawSendWarning(nullptr, "GDraw ran out of rendertargets");
         return false;
      }
   }
   n->color_buffer = t;
   assert(n->color_buffer != nullptr);

   if (n == gdraw->frame+1)
      n->stencil_depth = get_depthstencil_renderbuffer(gstats);
   else
      n->stencil_depth = (n-1)->stencil_depth;
   ++gdraw->cur;

   gdraw->cur->cached = owner != nullptr;
   if (owner) {
      gdraw->cur->base_x = region->x0;
      gdraw->cur->base_y = region->y0;
      gdraw->cur->width  = region->x1 - region->x0;
      gdraw->cur->height = region->y1 - region->y0;
   }

   gstats->nonzero_flags |= GDRAW_STATS_rendtarg;

   glBindFramebuffer(GL_FRAMEBUFFER, gdraw->framebuffer_stack_object);
   set_render_target_state();

   assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);

   // viewport for clear (make sure scissor is inside viewport just in case)
   glViewport(0, 0, gdraw->frametex_width, gdraw->frametex_height);

   k = (int) (n->color_buffer - gdraw->rendertargets.handle);
   if (region) {
      S32 ox, oy;
      
      // in a perfect world, we'd only need 1 pixel of border on all sides for
      // bilinear filtering, which would mean pad = 1. however, texture interpolator
      // precision is not that high even on PC parts, and if we only use 1 pixel of
      // padding we will often get some un-filled pixels "creeping in" from the sides.
      // pad = 2 is fine on recent PC parts, but not old PC parts or even fairly new
      // mobile parts, so we play it safe and use 3 pixels which so far gives good
      // results everywhere.
      S32 pad = 3;

      // region.x0,y0 are the top left of the rectangle in display space
      // x,y are the *bottom* left of the rectangle in window space
      S32 h = gdraw->tph;
      S32 xt0, yt0, xt1, yt1;
      S32 x0, y0, x1, y1;

      if (gdraw->in_blur || !gdraw->tile_enabled)
         ox = oy = 0;
      else
         ox = gdraw->tx0, oy = gdraw->ty0;

      // clamp region to tile (in gdraw coords)
      xt0 = RR_MAX(region->x0 - ox, 0);
      yt0 = RR_MAX(region->y0 - oy, 0);
      xt1 = RR_MIN(region->x1 - ox, gdraw->tpw);
      yt1 = RR_MIN(region->y1 - oy, gdraw->tph);

      // but the padding gets clamped to framebuffer coords! also transfer to window space here.
      x0 = RR_MAX(    xt0 - pad, 0);
      y0 = RR_MAX(h - yt1 - pad, 0);
      x1 = RR_MIN(    xt1 + pad, gdraw->frametex_width);
      y1 = RR_MIN(h - yt0 + pad, gdraw->frametex_height);

      if (x1 <= x0 || y1 <= y0) { // region doesn't intersect with current tile
         --gdraw->cur;

         // remove color and stencil buffers
         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_TEXTURE_2D, 0, 0);
         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT  , GL_TEXTURE_2D, 0, 0);
         
         // switch render target back
         if (gdraw->cur == gdraw->frame)
            glBindFramebuffer(GL_FRAMEBUFFER, gdraw->main_framebuffer);
         else
            set_render_target_state();

         set_viewport();
         set_projection();
         opengl_check();

         // free our render target
         gdraw_FreeTexture((GDrawTexture *) n->color_buffer, 0, gstats);

         // note: don't send a warning since this will happen during regular tiled rendering
         return false;
      }

      glEnable(GL_SCISSOR_TEST);
      glScissor(x0, y0, x1 - x0, y1 - y0);
      gdraw->rt_valid[k].x0 = xt0;
      gdraw->rt_valid[k].y0 = yt0;
      gdraw->rt_valid[k].x1 = xt1;
      gdraw->rt_valid[k].y1 = yt1;
      gstats->cleared_pixels += (x1 - x0) * (y1 - y0);
   } else {
      glDisable(GL_SCISSOR_TEST);
      gdraw->rt_valid[k].x0 = 0;
      gdraw->rt_valid[k].y0 = 0;
      gdraw->rt_valid[k].x1 = gdraw->frametex_width;
      gdraw->rt_valid[k].y1 = gdraw->frametex_height;
      gstats->cleared_pixels += gdraw->frametex_width * gdraw->frametex_height;
   }

   gstats->nonzero_flags |= GDRAW_STATS_clears;
   gstats->num_clears += 1;

#ifdef GDRAW_FEWER_CLEARS
   if (region) {
      clear_with_rect(region, n==gdraw->frame+1, gstats);
   } else
#endif // GDRAW_FEWER_CLEARS
   {
      glClearColor(0,0,0,0); // must clear destination alpha
      glClearStencil(0);
      glClearDepth(1);
      glStencilMask(255);
      glDepthMask(GL_TRUE);
      glColorMask(1,1,1,1);
      glDisable(GL_STENCIL_TEST);
      if (n == gdraw->frame+1)
         glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      else
         glClear(GL_COLOR_BUFFER_BIT);
   }

   set_viewport();
   set_projection();

   opengl_check();

   return true;
}


static GDrawTexture *RADLINK gdraw_TextureDrawBufferEnd(GDrawStats *gstats)
{
   GDrawFramebufferState *n =   gdraw->cur;
   GDrawFramebufferState *m = --gdraw->cur;
   if (gdraw->fw == 0 || gdraw->fh == 0) return 0;

   if (n >= &gdraw->frame[MAX_RENDER_STACK_DEPTH])
      return 0; // already returned a warning in Start...()

   assert(m >= gdraw->frame);  // bug in Iggy -- unbalanced

   if (m != gdraw->frame)
      assert(m->color_buffer != nullptr);
   assert(n->color_buffer != nullptr);

   // remove color and stencil buffers
   glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, 0);
   glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
   glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT  , GL_RENDERBUFFER, 0);

#ifdef GDRAW_MULTISAMPLING
   if (gdraw->multisampling) {
      // blit from multisample to texture
      if (n->color_buffer->handle.tex.gl_renderbuf) {
         GLuint res;
         glBindFramebuffer(GL_READ_FRAMEBUFFER, gdraw->framebuffer_copy_to_texture);
         glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
         glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, 0);
         glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
         glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
         glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, n->color_buffer->handle.tex.gl_renderbuf);
         glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, n->color_buffer->handle.tex.gl, 0);
         res = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
         glBlitFramebuffer(0,0,gdraw->tpw,gdraw->tph,0,0,gdraw->tpw,gdraw->tph, GL_COLOR_BUFFER_BIT, GL_NEAREST);
         gstats->nonzero_flags |= GDRAW_STATS_blits;
         gstats->num_blits += 1;
         gstats->num_blit_pixels += (gdraw->tpw * gdraw->tph);

         glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, 0);
         glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, 0);
         glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
      }
   }
#endif

   gstats->nonzero_flags |= GDRAW_STATS_rendtarg;
   gstats->rendertarget_changes += 1;

   // set the new state
   if (m == gdraw->frame) // back to initial framebuffer
      glBindFramebuffer(GL_FRAMEBUFFER, gdraw->main_framebuffer);
   else
      set_render_target_state();

   // reset the viewport if we've reached the root scope
   set_viewport();
   set_projection();

   opengl_check();

   return (GDrawTexture *) n->color_buffer;
}


////////////////////////////////////////////////////////////////////////
//
//   Clear stencil/depth buffers
//
// Open question whether we'd be better off finding bounding boxes
// and only clearing those; it depends exactly how fast clearing works.
//

static void RADLINK gdraw_ClearStencilBits(U32 bits)
{
   glDisable(GL_SCISSOR_TEST);
   glStencilMask(bits);
   glClearStencil(0);
   glClear(GL_STENCIL_BUFFER_BIT);
   opengl_check();
}

// this only happens rarely (hopefully never) if we use the depth buffer,
// so we can just clear the whole thing
static void RADLINK gdraw_ClearID(void)
{
   glDisable(GL_SCISSOR_TEST);
   glClearDepth(1);
   glDepthMask(GL_TRUE);
   glClear(GL_DEPTH_BUFFER_BIT);
   opengl_check();
}


////////////////////////////////////////////////////////////////////////
//
//   Set all the render state from GDrawRenderState
//
// This also is responsible for getting the framebuffer into a texture
// if the read-modify-write blend operation can't be expressed with
// the native blend operators. (E.g. "screen")
//

enum
{
   VVAR_world0    = 0,
   VVAR_world1    = 1,
   VVAR_xoff      = 2,
   VVAR_texgen_s  = 3,
   VVAR_texgen_t  = 4,
   VVAR_viewproj  = 5,
   VVAR_x3d       = 5,
   VVAR_y3d       = 6,
   VVAR_z3d       = 7,
};

// convert an ID request to a value suitable for the depth buffer,
// in homogeneous clip space with w=1 (depth from -1..1)
static float depth_from_id(S32 id)
{
   return 1.0f - 2.0f*(id+1) / (F32) MAX_DEPTH_VALUE;
}

static void set_texture(U32 texunit, GDrawTexture *tex)
{
   glActiveTexture(GL_TEXTURE0 + texunit);
   if (tex == nullptr)
      glBindTexture(GL_TEXTURE_2D, 0);
   else
      glBindTexture(GL_TEXTURE_2D, ((GDrawHandle *) tex)->handle.tex.gl);
}

static void set_world_projection(const int *vvars, const F32 world[2*4])
{
   assert(vvars[VVAR_world0] >= 0 && vvars[VVAR_world1] >= 0 && vvars[VVAR_viewproj] >= 0);
   glUniform4fv(vvars[VVAR_world0], 1, world + 0);
   glUniform4fv(vvars[VVAR_world1], 1, world + 4);
   glUniform4fv(vvars[VVAR_viewproj], 1, gdraw->projection);
}

static void set_3d_projection(const int *vvars, const F32 world[2*4], const F32 xform[3][4])
{
   assert(vvars[VVAR_world0] >= 0 && vvars[VVAR_world1] >= 0);
   glUniform4fv(vvars[VVAR_world0], 1, world + 0);
   glUniform4fv(vvars[VVAR_world1], 1, world + 4);

   assert(vvars[VVAR_x3d] >= 0 && vvars[VVAR_y3d] >= 0 && vvars[VVAR_z3d] >= 0);
   glUniform4fv(vvars[VVAR_x3d], 1, xform[0]);
   glUniform4fv(vvars[VVAR_y3d], 1, xform[1]);
   glUniform4fv(vvars[VVAR_z3d], 1, xform[2]);
}


static int set_render_state(GDrawRenderState *r, S32 vformat, const int **ovvars, GDrawPrimitive *p, GDrawStats *gstats)
{
   static struct gdraw_gl_blendspec {
      GLboolean enable;
      GLenum src;
      GLenum dst;
   } blends[ASSERT_COUNT(GDRAW_BLEND__count, 6)] = {
      { GL_FALSE, GL_ONE,        GL_ZERO                },  // GDRAW_BLEND_none
      { GL_TRUE,  GL_ONE,        GL_ONE_MINUS_SRC_ALPHA },  // GDRAW_BLEND_alpha
      { GL_TRUE,  GL_DST_COLOR,  GL_ONE_MINUS_SRC_ALPHA },  // GDRAW_BLEND_multiply
      { GL_TRUE,  GL_ONE,        GL_ONE                 },  // GDRAW_BLEND_add

      { GL_FALSE, GL_ONE,        GL_ZERO                },  // GDRAW_BLEND_filter
      { GL_FALSE, GL_ONE,        GL_ZERO                },  // GDRAW_BLEND_special
   };

   F32 world[2*4];
   ProgramWithCachedVariableLocations *prg;
   int *fvars, *vvars;
   int blend_mode;

   opengl_check();
   assert((vformat >= 0 && vformat < GDRAW_vformat__basic_count) || vformat == GDRAW_vformat_ihud1);

   if (vformat == GDRAW_vformat_ihud1) {
      glEnable(GL_BLEND);
      glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); // premultiplied alpha blend mode
      prg = &gdraw->ihud[0];
   } else {
      // apply the major blend mode
      blend_mode = r->blend_mode;
      assert(blend_mode >= 0 && blend_mode < sizeof(blends)/sizeof(*blends));
      if (blends[blend_mode].enable) {
         glEnable(GL_BLEND);
         glBlendFunc(blends[blend_mode].src, blends[blend_mode].dst);
      } else
         glDisable(GL_BLEND);

      // set the fragment program if it wasn't set above
      if (r->blend_mode != GDRAW_BLEND_special) {
         // make sure data has been initialized
         int which = r->tex0_mode, additive = 0;

         if (r->cxf_add) {
            additive = 1;
            if (r->cxf_add[3]) additive = 2;
         }

         prg = &gdraw->fprog[which][additive][vformat];
      } else
         prg = &gdraw->exceptional_blend[r->special_blend];
   }

   use_lazy_shader(prg);
   opengl_check();
   fvars = prg->vars[0];
   vvars = prg->vars[1];

   if (vformat == GDRAW_vformat_ihud1) {
      F32 wv[2][4] = { 1.0f/960,0,0,-1.0, 0,-1.0f/540,0,+1.0 };
      glUniform4fv(vvars[VAR_ihudv_worldview], 2, wv[0]);
      opengl_check();
      glUniform4fv(vvars[VAR_ihudv_material], p->uniform_count, p->uniforms);
      opengl_check();
      glUniform1f(vvars[VAR_ihudv_textmode], p->drawprim_mode ? 0.0f : 1.0f);
      opengl_check();
   } else {

      // set vertex shader constants
      if (!r->use_world_space)
         gdraw_ObjectSpace(world, r->o2w, depth_from_id(r->id), 0.0f);
      else
         gdraw_WorldSpace(world, gdraw->world_to_pixel, depth_from_id(r->id), 0.0f);

   #ifdef FLASH_10
      set_3d_projection(vvars, world, gdraw->xform_3d);
   #else
      set_world_projection(vvars, world);
   #endif

      if (vvars[VVAR_xoff] >= 0)
         glUniform4fv(vvars[VVAR_xoff], 1, r->edge_matrix);

      if (r->texgen0_enabled) {
         assert(vvars[VVAR_texgen_s] >= 0 && vvars[VVAR_texgen_t] >= 0);
         glUniform4fv(vvars[VVAR_texgen_s], 1, r->s0_texgen);
         glUniform4fv(vvars[VVAR_texgen_t], 1, r->t0_texgen);
      }
   }

   // texture stuff
   set_texture(0, r->tex[0]);
   opengl_check();

   if (r->tex[0] && gdraw->has_conditional_non_power_of_two && ((GDrawHandle*) r->tex[0])->handle.tex.nonpow2) {
      // only wrap mode allowed in conditional nonpow2 is clamp; this should
      // have been set when the texture was created, but to be on the safe side...
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   } else switch (r->wrap0) {
      case GDRAW_WRAP_repeat:
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
         break;
      case GDRAW_WRAP_clamp:
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
         break;
      case GDRAW_WRAP_mirror:
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
         break;
   }

   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, r->nearest0 ? GL_NEAREST : GL_LINEAR);

   // fragment shader constants

   if (fvars[VAR_cmul] >= 0)
      glUniform4f(fvars[VAR_cmul], r->color[0], r->color[1], r->color[2], r->color[3]);
   if (fvars[VAR_cadd] >= 0)
      if (r->cxf_add)
         glUniform4f(fvars[VAR_cadd], r->cxf_add[0]/255.0f, r->cxf_add[1]/255.0f, r->cxf_add[2]/255.0f, r->cxf_add[3]/255.0f);
      else
         glUniform4f(fvars[VAR_cadd], 0,0,0,0);
   if (fvars[VAR_focal] >= 0)
      glUniform4fv(fvars[VAR_focal], 1, r->focal_point);

   glActiveTexture(GL_TEXTURE0);

   // Set pixel operation states

   if (r->scissor) {
      S32 x0,y0,x1,y1;
      // scissor.x0,y0 are the top left of the rectangle in display space
      // x,y are the *bottom* left of the rectangle in window space
      x0 = r->scissor_rect.x0;
      y0 = r->scissor_rect.y1; 
      x1 = r->scissor_rect.x1;
      y1 = r->scissor_rect.y0;
      // convert into tile-relative coordinates
      if (gdraw->tile_enabled) {
         x0 -= gdraw->tx0;
         y0 -= gdraw->ty0;
         x1 -= gdraw->tx0;
         y1 -= gdraw->ty0;
      }
      // convert bottom-most edge to bottom-relative
      y0 = (gdraw->th) - y0;
      y1 = (gdraw->th) - y1;
      if (gdraw->cur == gdraw->frame) {
         // move into viewport space
         x0 += gdraw->vx;
         y0 += gdraw->vy;
         x1 += gdraw->vx;
         y1 += gdraw->vy;
      }
      glScissor(x0,y0,x1-x0,y1-y0);
      glEnable(GL_SCISSOR_TEST);
   } else
      glDisable(GL_SCISSOR_TEST);

   glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
   glStencilMask(r->stencil_set);
   glStencilFunc(GL_EQUAL, 255, r->stencil_test);
   if (r->stencil_set | r->stencil_test)
      glEnable(GL_STENCIL_TEST);
   else
      glDisable(GL_STENCIL_TEST);

   if (r->stencil_set)
      glColorMask(0,0,0,0);
   else
      glColorMask(1,1,1,1);

   if (r->test_id) {
      glEnable(GL_DEPTH_TEST);
      glDepthFunc(GL_LESS);
   } else {
      glDisable(GL_DEPTH_TEST);
      glDepthFunc(GL_LESS);
   }

   if (r->set_id)
      glDepthMask(GL_TRUE);
   else
      glDepthMask(GL_FALSE);

   opengl_check();
   if (ovvars)
      *ovvars = vvars;

   return 1;
}

////////////////////////////////////////////////////////////////////////
//
//   Vertex formats
//

static void set_vertex_format(S32 format, F32 *vertices)
{
   switch (format) {
      case GDRAW_vformat_v2:
         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
         glEnableVertexAttribArray(0);
         break;

      case GDRAW_vformat_v2aa:
         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 16, vertices);
         glVertexAttribPointer(1, 4, GL_SHORT, GL_FALSE, 16, vertices+2);
         glEnableVertexAttribArray(0);
         glEnableVertexAttribArray(1);
         break;

      case GDRAW_vformat_v2tc2:
         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 16, vertices);
         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 16, vertices+2);
         glEnableVertexAttribArray(0);
         glEnableVertexAttribArray(1);
         break;

      case GDRAW_vformat_ihud1:
         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 20, vertices);
         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 20, vertices+2);
         glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, 20, vertices+4);
         glEnableVertexAttribArray(0);
         glEnableVertexAttribArray(1);
         glEnableVertexAttribArray(2);
         break;

      default:
         assert(0);
   }
}

static void reset_vertex_format(S32 format)
{
   // we don't use attrib #1 for all formats, but doesn't seem worthwhile to check
   format = format;
   glDisableVertexAttribArray(0);
   glDisableVertexAttribArray(1);
   glDisableVertexAttribArray(2);
}

////////////////////////////////////////////////////////////////////////
//
//   Draw triangles with a given renderstate
//

static void tag_resources(void *r1, void *r2, void *r3)
{
   U64 now = gdraw->frame_counter;
   if (r1) ((GDrawHandle *) r1)->fence.value = now;
   if (r2) ((GDrawHandle *) r2)->fence.value = now;
   if (r3) ((GDrawHandle *) r3)->fence.value = now;
}

static int vformat_stride[] =
{
   2,4,4,5
};

static void RADLINK gdraw_DrawIndexedTriangles(GDrawRenderState *r, GDrawPrimitive *p, GDrawVertexBuffer *buf, GDrawStats *gstats)
{
   GDrawHandle *vb = (GDrawHandle *) buf;
   if (vb) {
      glBindBuffer(GL_ARRAY_BUFFER, vb->handle.vbuf.base);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vb->handle.vbuf.indices);
   } else {
      glBindBuffer(GL_ARRAY_BUFFER, 0);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
   }

   if (!set_render_state(r,p->vertex_format, nullptr, p, gstats)) return;
   gstats->nonzero_flags |= GDRAW_STATS_batches;
   gstats->num_batches += 1;
   gstats->drawn_indices += p->num_indices;
   gstats->drawn_vertices += p->num_vertices;

   if (vb || p->indices) { // regular path
      set_vertex_format(p->vertex_format, p->vertices);
      glDrawElements(GL_TRIANGLES, p->num_indices, GL_UNSIGNED_SHORT, p->indices);
   } else { // dynamic quads
      S32 pos = 0;
      U32 stride = vformat_stride[p->vertex_format]; // in units of sizeof(F32)
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gdraw->quad_ib);
      assert(p->num_vertices % 4 == 0);

      while (pos < p->num_vertices) {
         S32 vert_count = RR_MIN(p->num_vertices - pos, QUAD_IB_COUNT * 4);
         set_vertex_format(p->vertex_format, p->vertices + pos*stride);
         glDrawElements(GL_TRIANGLES, (vert_count >> 2) * 6, GL_UNSIGNED_SHORT, nullptr);
         pos += vert_count;
      }
      
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
   }
   reset_vertex_format(p->vertex_format);

   if (vb) {
      glBindBuffer(GL_ARRAY_BUFFER, 0);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
   }

   opengl_check();
   tag_resources(vb, r->tex[0], r->tex[1]);
}

///////////////////////////////////////////////////////////////////////
//
//   Flash 8 filter effects
//

// caller sets up texture coordinates
static void do_screen_quad(gswf_recti *s, F32 *tc, const int *vvars, GDrawStats *gstats, F32 depth)
{
   F32 px0 = (F32) s->x0, py0 = (F32) s->y0, px1 = (F32) s->x1, py1 = (F32) s->y1;
   F32 s0 = tc[0], t0 = tc[1], s1 = tc[2], t1 = tc[3];
   F32 vert[4][4];
   F32 world[2*4];

   opengl_check();

   vert[0][0] = px0;  vert[0][1] = py0; vert[0][2] = s0; vert[0][3] = t0;
   vert[1][0] = px1;  vert[1][1] = py0; vert[1][2] = s1; vert[1][3] = t0;
   vert[2][0] = px1;  vert[2][1] = py1; vert[2][2] = s1; vert[2][3] = t1;
   vert[3][0] = px0;  vert[3][1] = py1; vert[3][2] = s0; vert[3][3] = t1;

   opengl_check();
   gdraw_PixelSpace(world);
   world[2] = depth;
   set_world_projection(vvars, world);
   opengl_check();

   set_vertex_format(GDRAW_vformat_v2tc2, vert[0]);
   glBindBuffer(GL_ARRAY_BUFFER, 0);
   glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
   opengl_check();
   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
   reset_vertex_format(GDRAW_vformat_v2tc2);
   opengl_check();

   gstats->nonzero_flags |= GDRAW_STATS_batches;
   gstats->num_batches    += 1;
   gstats->drawn_vertices += 4;
   gstats->drawn_indices  += 6;

   opengl_check();
}

#ifdef GDRAW_FEWER_CLEARS
static void clear_with_rect(gswf_recti *region, rrbool clear_depth, GDrawStats *gstats)
{
   F32 tc[4] = { 0,0,0,0 };

   use_lazy_shader(&gdraw->manual_clear);
   glUniform4f(gdraw->manual_clear.vars[0][0], 0.0, 0,0,0);

   glDisable(GL_BLEND);

   if (clear_depth) {
      glEnable(GL_DEPTH_TEST);
      glDepthFunc(GL_ALWAYS);
      glDepthMask(GL_TRUE);

      glEnable(GL_STENCIL_TEST);
      glStencilMask(255);
      glStencilOp(GL_REPLACE,GL_REPLACE,GL_REPLACE);
      glStencilFunc(GL_ALWAYS,0,255);
   } else {
      glDisable(GL_DEPTH_TEST);
      glDisable(GL_STENCIL_TEST);
   }

   glColorMask(1,1,1,1);
   glColor4f(0,0,0,0);

   {
      // coordinate system doesn't match, so just draw whole screen, rely on scissor to clip it properly
      gswf_recti foo = { -10000,-10000,10000,10000 };
      do_screen_quad(&foo, tc, gdraw->manual_clear.vars[1], gstats, 1.0f);
   }
}
#endif

static void gdraw_DriverBlurPass(GDrawRenderState *r, int taps, F32 *data, gswf_recti *s, F32 *tc, F32 height_max, F32 *clamp, GDrawStats *gstats)
{
   ProgramWithCachedVariableLocations *prg = &gdraw->blur_prog[taps];
   F32 clampv[4];

   // fix OpenGL t values for rendertargets are from bottom, not top
   tc[1] = height_max - tc[1];
   tc[3] = height_max - tc[3];

   clampv[0] = clamp[0];
   clampv[1] = height_max - clamp[3];
   clampv[2] = clamp[2];
   clampv[3] = height_max - clamp[1];

   use_lazy_shader(prg);
   set_texture(0, r->tex[0]);

   glColorMask(1,1,1,1);
   glDisable(GL_BLEND);
   glDisable(GL_SCISSOR_TEST);

   assert(prg->vars[0][VAR_blur_tap] >= 0);
   glUniform4fv(prg->vars[0][VAR_blur_tap], taps, data);
   glUniform4fv(prg->vars[0][VAR_blur_clampv], 1, clampv);

   do_screen_quad(s, tc, prg->vars[1], gstats, 0);
   tag_resources(r->tex[0],0,0);
}

static void gdraw_Colormatrix(GDrawRenderState *r, gswf_recti *s, float *tc, GDrawStats *gstats)
{
   ProgramWithCachedVariableLocations *prg = &gdraw->colormatrix;
   if (!gdraw_TextureDrawBufferBegin(s, GDRAW_TEXTURE_FORMAT_rgba32, GDRAW_TEXTUREDRAWBUFFER_FLAGS_needs_color | GDRAW_TEXTUREDRAWBUFFER_FLAGS_needs_alpha, nullptr, gstats))
      return;
   use_lazy_shader(prg);
   set_texture(0, r->tex[0]);
   glUniform4fv(prg->vars[0][VAR_colormatrix_data], 5, r->shader_data);
   do_screen_quad(s, tc, gdraw->colormatrix.vars[1], gstats, 0);
   tag_resources(r->tex[0],0,0);
   r->tex[0] = gdraw_TextureDrawBufferEnd(gstats);
}

static gswf_recti *get_valid_rect(GDrawTexture *tex)
{
   GDrawHandle *h = (GDrawHandle *) tex;
   S32 n = (S32) (h - gdraw->rendertargets.handle);
   assert(n >= 0 && n <= MAX_RENDER_STACK_DEPTH+1);
   return &gdraw->rt_valid[n];
}

static void set_clamp_constant(GLint constant, GDrawTexture *tex)
{
   gswf_recti *s = get_valid_rect(tex);
   // when we make the valid data, we make sure there is an extra empty pixel at the border
   // we also have to convert from GDraw coords to GL coords here.
   glUniform4f(constant,
      (           s->x0-0.5f) / gdraw->frametex_width,
      (gdraw->tph-s->y1-0.5f) / gdraw->frametex_height,
      (           s->x1+0.5f) / gdraw->frametex_width,
      (gdraw->tph-s->y0+0.5f) / gdraw->frametex_height);
}

static void gdraw_Filter(GDrawRenderState *r, gswf_recti *s, float *tc, int isbevel, GDrawStats *gstats)
{
   ProgramWithCachedVariableLocations *prg = &gdraw->filter_prog[isbevel][r->filter_mode];
   if (!gdraw_TextureDrawBufferBegin(s, GDRAW_TEXTURE_FORMAT_rgba32, GDRAW_TEXTUREDRAWBUFFER_FLAGS_needs_color | GDRAW_TEXTUREDRAWBUFFER_FLAGS_needs_alpha, nullptr, gstats))
      return;
   use_lazy_shader(prg);
   set_texture(0, r->tex[0]);
   set_texture(1, r->tex[1]);
   set_texture(2, r->tex[2]);
   glUniform4fv(prg->vars[0][VAR_filter_color], 1, &r->shader_data[0]);
   glUniform4f(prg->vars[0][VAR_filter_tc_off], -r->shader_data[4] / (F32)gdraw->frametex_width, r->shader_data[5] / (F32)gdraw->frametex_height, r->shader_data[6], 0);
   if (prg->vars[0][VAR_filter_color2] >= 0)
      glUniform4fv(prg->vars[0][VAR_filter_color2], 1, &r->shader_data[8]);
   set_clamp_constant(prg->vars[0][VAR_filter_clamp0], r->tex[0]);
   set_clamp_constant(prg->vars[0][VAR_filter_clamp1], r->tex[1]);
   do_screen_quad(s, tc, prg->vars[1], gstats, 0);
   tag_resources(r->tex[0],0,0);
   r->tex[0] = gdraw_TextureDrawBufferEnd(gstats);
}

static void RADLINK gdraw_FilterQuad(GDrawRenderState *r, S32 x0, S32 y0, S32 x1, S32 y1, GDrawStats *gstats)
{
   F32 tc[4];
   gswf_recti s;

   // clip to tile boundaries
   s.x0 = RR_MAX(x0, gdraw->tx0p);
   s.y0 = RR_MAX(y0, gdraw->ty0p);
   s.x1 = RR_MIN(x1, gdraw->tx0p + gdraw->tpw);
   s.y1 = RR_MIN(y1, gdraw->ty0p + gdraw->tph);
   if (s.x1 <= s.x0 || s.y1 <= s.y0)
      return;

   // if it's a rendertarget, it's inverted from our design because OpenGL is bottom-left 0,0
   // and we have to compensate for scaling
   tc[0] =               (s.x0 - gdraw->tx0p)  / (F32) gdraw->frametex_width;
   tc[1] = (gdraw->tph - (s.y0 + gdraw->ty0p)) / (F32) gdraw->frametex_height;
   tc[2] =               (s.x1 - gdraw->tx0p)  / (F32) gdraw->frametex_width;
   tc[3] = (gdraw->tph - (s.y1 - gdraw->ty0p)) / (F32) gdraw->frametex_height;

   glUseProgram(0);
   set_texture(0, 0);
   set_texture(1, 0);
   set_texture(2, 0);

   glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
   glStencilMask(255);
   glDisable(GL_STENCIL_TEST);
   glColorMask(1,1,1,1);
   glDisable(GL_BLEND);
   glDisable(GL_DEPTH_TEST);
   opengl_check();

   if (r->blend_mode == GDRAW_BLEND_filter) {
      switch (r->filter) {
         case GDRAW_FILTER_blur: {
            GDrawBlurInfo b;
            gswf_recti bounds = *get_valid_rect(r->tex[0]);
            gdraw_ShiftRect(&s, &s, -gdraw->tx0p, -gdraw->ty0p); // blur uses physical rendertarget coordinates

            b.BlurPass = gdraw_DriverBlurPass;
            b.w = gdraw->tpw;
            b.h = gdraw->tph;
            b.frametex_width = gdraw->frametex_width;
            b.frametex_height = gdraw->frametex_height;

            // blur passes must override the viewport/ortho projection

            gdraw->in_blur = true; // prevent viewport/projection munging in start/end texture
            set_viewport();
            set_projection();
            gdraw_Blur(&gdraw_funcs, &b,r, &s, &bounds, gstats);

            gdraw->in_blur = false;

            set_viewport();
            set_projection();
            break;
         }

         case GDRAW_FILTER_colormatrix:
            gdraw_Colormatrix(r, &s, tc, gstats);
            break;

         case GDRAW_FILTER_dropshadow:
            gdraw_Filter(r, &s, tc, 0, gstats);
            break;

         case GDRAW_FILTER_bevel:
            gdraw_Filter(r, &s, tc, 1, gstats);
            break;

         default:
            assert(0);
      }
   } else {
      GDrawTexture *blend_tex = nullptr;
      const int *vvars;

      // for crazy blend modes, we need to read back from the framebuffer
      // and do the blending in the pixel shader. we do this with CopyTexSubImage,
      // rather than trying to render-to-texture-all-along, because that's a pain.
      // @TODO: propogate the rectangle down and only copy what we need, like in 360

      if (r->blend_mode == GDRAW_BLEND_special) {
         blend_tex = (GDrawTexture *) get_color_rendertarget(gstats);
         glBindTexture(GL_TEXTURE_2D, ((GDrawHandle *) blend_tex)->handle.tex.gl);
         if (gdraw->cur != gdraw->frame)
            glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, 0,0,gdraw->tpw,gdraw->tph);
         else
            glCopyTexSubImage2D(GL_TEXTURE_2D, 0, gdraw->tx0 - gdraw->tx0p, gdraw->ty0 - gdraw->ty0p, gdraw->vx,gdraw->vy,gdraw->tw,gdraw->th);

         set_texture(1, blend_tex);
      }

      if (!set_render_state(r, GDRAW_vformat_v2tc2, &vvars, nullptr, gstats))
         return;
      do_screen_quad(&s, tc, vvars, gstats, 0);
      tag_resources(r->tex[0],r->tex[1],0);
      if (blend_tex)
         gdraw_FreeTexture(blend_tex, 0, gstats);
   }
}

void gdraw_GLx_(NoMoreGDrawThisFrame)(void)
{
   clear_renderstate();
   ++gdraw->frame_counter;
}

void gdraw_GLx_(BeginCustomDraw)(IggyCustomDrawCallbackRegion *region, F32 *matrix)
{
   clear_renderstate();
   gdraw_GetObjectSpaceMatrix(matrix, region->o2w, gdraw->projection, depth_from_id(0), 1);
}

void gdraw_GLx_(EndCustomDraw)(IggyCustomDrawCallbackRegion *region)
{
   set_common_renderstate();
}


///////////////////////////////////////////////////////////////////////
//
//   Vertex and Fragment program initialization
//

#include GDRAW_SHADERS

static void make_vars(GDrawGLProgram prog, S32 vars[2][8], char **varn)
{
   if (prog) {
      char **varn2 = (varn == pshader_general2_vars ? vshader_vsglihud_vars : vshader_vsgl_vars);
      S32 k;
      for (k=0; varn[k]; ++k)
         if (varn[k][0])
            vars[0][k] = glGetUniformLocation(prog, varn[k]);
         else
            vars[0][k] = -1;

      for (k=0; varn2[k]; ++k)
         if (varn2[k][0])
            vars[1][k] = glGetUniformLocation(prog, varn2[k]);
         else
            vars[1][k] = -1;

      if (vars[0][0] >= 0)
         assert(vars[0][0] != vars[0][1]);
   }
}

static void make_fragment_program(ProgramWithCachedVariableLocations *p, int num_strings, char **strings, char **varn)
{
   S32 i;
   GLint res;
   GDrawGLProgram shad;
   opengl_check();
   for (i=0; i < MAX_VARS; ++i) {
      p->vars[0][i] = -1;
      p->vars[1][i] = -1;
   }

   shad = glCreateShader(GL_FRAGMENT_SHADER);
   glShaderSource(shad, num_strings, (const GLchar **)strings, nullptr);
   glCompileShader(shad);
   glGetShaderiv(shad, GL_COMPILE_STATUS, &res);
   if (!res) {
      char errors[512];
      glGetShaderInfoLog(shad, sizeof(errors)-2, &res, errors);
      compilation_err(errors);
      p->program = 0;
   } else {
      S32 vert = GDRAW_vformat_v2tc2;
      if (p >= &gdraw->fprog[0][0][0] && p < &gdraw->fprog[GDRAW_TEXTURE__count][0][0]) {
         // for basic rendering shaders, we have three versions corresponding to the
         // three vertex formats we support.
         S32 n = (S32) (p - gdraw->fprog[0][0]);
         vert = n % 3;
      }

      if (p == &gdraw->ihud[0])
         vert = GDRAW_vformat_ihud1;

      opengl_check();
      p->program = glCreateProgram();
      glAttachShader(p->program, shad);
      glAttachShader(p->program, gdraw->vert[vert]);
      opengl_check();

      if (vert == GDRAW_vformat_ihud1) {
         glBindAttribLocation(p->program, 0, "position");
         glBindAttribLocation(p->program, 1, "texcoord");
         glBindAttribLocation(p->program, 2, "material_index");
      } else {
         glBindAttribLocation(p->program, 0, "position");
         glBindAttribLocation(p->program, 1, "in_attr");
      }

      glLinkProgram(p->program);
      glGetProgramiv(p->program, GL_LINK_STATUS, &res);
      if (!res) {
         char errors[512];
         glGetProgramiv(p->program, GL_INFO_LOG_LENGTH, &res);
         glGetProgramInfoLog(p->program, sizeof(errors)-2, &res, errors);
         compilation_err(errors);
         glDeleteShader(shad);
         glDeleteProgram(p->program);
         p->program = 0;
      } else
         make_vars(p->program, p->vars, varn);
   }
   opengl_check();
   glUseProgram(p->program); // now activate the program
   opengl_check();
}

static void make_vertex_program(GLuint *vprog, int num_strings, char **strings)
{
   GLint res;
   GDrawGLProgram shad;
   opengl_check();

   if(strings[0])
   {
      shad = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(shad, num_strings, (const GLchar **)strings, nullptr);
      glCompileShader(shad);
      glGetShaderiv(shad, GL_COMPILE_STATUS, &res);
      if (!res) {
         char errors[512];
         glGetShaderInfoLog(shad, sizeof(errors)-2, &res, errors);
         compilation_err(errors);
         glDeleteShader(shad);
         shad = 0;
      }
      opengl_check();
      *vprog = shad;
   }
   else
   {
      *vprog = 0;
   }
}

static void bind_sampler(ProgramWithCachedVariableLocations *prog, int varidx, int sampleridx)
{
   int var = prog->vars[0][varidx];
   if (var >= 0)
      glUniform1i(var, sampleridx);
}

static void make_vertex_programs(void)
{
   int type;
   for (type=0; type < GDRAW_vformat__basic_count; type++)
      make_vertex_program(&gdraw->vert[type], NUMFRAGMENTS_vshader_vsgl, vshader_vsgl(type));
   type = GDRAW_vformat_ihud1;
   make_vertex_program(&gdraw->vert[type], NUMFRAGMENTS_vshader_vsglihud, vshader_vsglihud());
}

static void lazy_shader(ProgramWithCachedVariableLocations *ptr)
{
   if (ptr >= &gdraw->fprog[0][0][0] && ptr < &gdraw->fprog[GDRAW_TEXTURE__count][0][0]) {
      S32 n = (S32) (ptr - gdraw->fprog[0][0]);
      n /= 3;

      make_fragment_program(ptr, NUMFRAGMENTS_pshader_basic, pshader_basic_arr[n], pshader_basic_vars);
      bind_sampler(ptr, VAR_tex0, 0);
      bind_sampler(ptr, VAR_tex1, AATEX_SAMPLER);
      return;
   }

   if (ptr >= &gdraw->exceptional_blend[0] && ptr < &gdraw->exceptional_blend[GDRAW_BLENDSPECIAL__count]) {
      S32 n = (S32) (ptr - gdraw->exceptional_blend);
      make_fragment_program(ptr, NUMFRAGMENTS_pshader_exceptional_blend, pshader_exceptional_blend_arr[n], pshader_exceptional_blend_vars);
      bind_sampler(ptr, VAR_tex0, 0);
      bind_sampler(ptr, VAR_tex1, 1);
      return;
   }

   if (ptr >= &gdraw->filter_prog[0][0] && ptr <= &gdraw->filter_prog[1][15]) {
      S32 n = (S32) (ptr - gdraw->filter_prog[0]);
      make_fragment_program(ptr, NUMFRAGMENTS_pshader_filter, pshader_filter_arr[n], pshader_filter_vars);
      bind_sampler(ptr, VAR_filter_tex0, 0);
      bind_sampler(ptr, VAR_filter_tex1, 1);
      bind_sampler(ptr, VAR_filter_tex2, 2);
      return;
   }

   if (ptr >= &gdraw->blur_prog[0] && ptr <= &gdraw->blur_prog[MAX_TAPS]) {
      S32 n = (S32) (ptr - gdraw->blur_prog);
      make_fragment_program(ptr, NUMFRAGMENTS_pshader_blur, pshader_blur_arr[n], pshader_blur_vars);
      bind_sampler(ptr, VAR_blur_tex0, 0);
      return;
   }

   if (ptr == &gdraw->colormatrix) {
      make_fragment_program(ptr, NUMFRAGMENTS_pshader_color_matrix, pshader_color_matrix_arr[0], pshader_color_matrix_vars);
      bind_sampler(ptr, VAR_colormatrix_tex0, 0);
      return;
   }

   if (ptr == &gdraw->manual_clear) {
      make_fragment_program(ptr, NUMFRAGMENTS_pshader_manual_clear, pshader_manual_clear_arr[0], pshader_manual_clear_vars);
      return;
   }

   if (ptr == &gdraw->ihud[0]) {
      make_fragment_program(ptr, NUMFRAGMENTS_pshader_general2, pshader_general2_arr[0], pshader_general2_vars);
      bind_sampler(ptr, VAR_tex0, 0);
      return;
   }

   RR_BREAK();
}

static rrbool make_quad_indices(void)
{
   int size = QUAD_IB_COUNT * 6 * sizeof(GLushort);
   GLushort *inds = IggyGDrawMalloc(size);
   int i, e;

   if (!inds)
      return 0;

   // make quad inds
   for (i=0; i < QUAD_IB_COUNT; i++) {
      inds[i*6 + 0] = (GLushort) (i*4 + 0);
      inds[i*6 + 1] = (GLushort) (i*4 + 1);
      inds[i*6 + 2] = (GLushort) (i*4 + 2);
      inds[i*6 + 3] = (GLushort) (i*4 + 0);
      inds[i*6 + 4] = (GLushort) (i*4 + 2);
      inds[i*6 + 5] = (GLushort) (i*4 + 3);
   }

   glGenBuffers(1, &gdraw->quad_ib);
   glBindBuffer(GL_ARRAY_BUFFER, gdraw->quad_ib);
   glBufferData(GL_ARRAY_BUFFER, size, inds, GL_STATIC_DRAW);
   IggyGDrawFree(inds);
   e = glGetError();
   if (e != GL_NO_ERROR) {
      eat_gl_err();
      return 0;
   }

   return 1;
}

////////////////////////////////////////////////////////////////////////
//
//   Create and tear-down the state
//

typedef struct
{
   S32 num_handles;
   S32 num_bytes;
} GDrawResourceLimit;

// These are the defaults limits used by GDraw unless the user specifies something else.
static GDrawResourceLimit gdraw_limits[GDRAW_GLx_(RESOURCE__count)] = {
   MAX_RENDER_STACK_DEPTH + 1, 16*1024*1024,  // GDRAW_GLx_RESOURCE_rendertarget
    500,                       20*1024*1024,  // GDRAW_GLx_RESOURCE_texture
   1000,                        2*1024*1024,  // GDRAW_GLx_RESOURCE_vertexbuffer
};

static GDrawHandleCache *make_handle_cache(gdraw_resourcetype type)
{
   S32 num_handles = gdraw_limits[type].num_handles;
   S32 num_bytes = gdraw_limits[type].num_bytes;
   GDrawHandleCache *cache = (GDrawHandleCache *) IggyGDrawMalloc(sizeof(GDrawHandleCache) + (num_handles - 1) * sizeof(GDrawHandle));
   if (cache) {
      gdraw_HandleCacheInit(cache, num_handles, num_bytes);
      cache->is_vertex = (type == GDRAW_GLx_(RESOURCE_vertexbuffer));
   }

   return cache;
}

static void free_gdraw()
{
   if (!gdraw) return;
   if (gdraw->texturecache) IggyGDrawFree(gdraw->texturecache);
   if (gdraw->vbufcache) IggyGDrawFree(gdraw->vbufcache);
   IggyGDrawFree(gdraw);
   gdraw = nullptr;
}

int gdraw_GLx_(SetResourceLimits)(gdraw_resourcetype type, S32 num_handles, S32 num_bytes)
{
   GDrawStats stats={0};

   if (type == GDRAW_GLx_(RESOURCE_rendertarget)) // RT count is small and space is preallocated
      num_handles = MAX_RENDER_STACK_DEPTH + 1;

   assert(type >= GDRAW_GLx_(RESOURCE_rendertarget) && type < GDRAW_GLx_(RESOURCE__count));
   assert(num_handles >= 0);
   assert(num_bytes >= 0);

   // nothing to do if the values are unchanged
   if (gdraw_limits[type].num_handles == num_handles &&
      gdraw_limits[type].num_bytes == num_bytes)
      return 1;

   gdraw_limits[type].num_handles = num_handles;
   gdraw_limits[type].num_bytes = num_bytes;

   // if no gdraw context created, there's nothing to worry about
   if (!gdraw)
      return 1;

   // resize the appropriate pool
   switch (type) {
      case GDRAW_GLx_(RESOURCE_rendertarget):
         flush_rendertargets(&stats);
         gdraw_HandleCacheInit(&gdraw->rendertargets, num_handles, num_bytes);
         return 1;
         
      case GDRAW_GLx_(RESOURCE_texture):
         if (gdraw->texturecache) {
            gdraw_res_flush(gdraw->texturecache, &stats);
            IggyGDrawFree(gdraw->texturecache);
         }
         gdraw->texturecache = make_handle_cache(GDRAW_GLx_(RESOURCE_texture));
         return gdraw->texturecache != nullptr;

      case GDRAW_GLx_(RESOURCE_vertexbuffer):
         if (gdraw->vbufcache) {
            gdraw_res_flush(gdraw->vbufcache, &stats);
            IggyGDrawFree(gdraw->vbufcache);
         }
         gdraw->vbufcache = make_handle_cache(GDRAW_GLx_(RESOURCE_vertexbuffer));
         return gdraw->vbufcache != nullptr;

      default:
         return 0;
   }
}

GDrawTexture * RADLINK gdraw_GLx_(MakeTextureFromResource)(U8 *resource_file, S32 len, IggyFileTextureRaw *texture)
{
   int i, offset, mips;
   const TextureFormatDesc *fmt;
   GDrawTexture *tex;
   GLuint gl_texture_handle;

   // look up the texture format
   fmt = gdraw->tex_formats;
   while (fmt->iggyfmt != texture->format && fmt->blkbytes)
      fmt++;
   if (!fmt->blkbytes) // end of list - i.e. format not supported
      return nullptr;

   // prepare texture
   glGenTextures(1, &gl_texture_handle);
   if (gl_texture_handle == 0)
      return nullptr;

   opengl_check();
   make_texture(gl_texture_handle);
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

   offset = texture->file_offset;
   mips = RR_MAX(texture->mipmaps, 1);

   // disable mipmaps if non-pow-2 is unsupported
   if (gdraw->has_conditional_non_power_of_two)
      if (!is_pow2(texture->w) || !is_pow2(texture->h))
         mips = 1;

   // disable mipmaps if chain is incomplete and GL_TEXTURE_MAX_LEVEL is unsupported
   if (!gdraw->has_texture_max_level && mips > 1) {
      int lastmip = mips-1;
      if ((texture->w >> lastmip) > 1 || (texture->h >> lastmip) > 1)
         mips = 1;
   }

   for (i=0; i < mips; i++) {
      U8 *data = resource_file + offset;
      int w = RR_MAX(texture->w >> i, 1);
      int h = RR_MAX(texture->h >> i, 1);
      int j;

      if (texture->format == IFT_FORMAT_rgba_4444_LE) {
         for (j=0; j < w * h; ++j) {
            unsigned short x = * (unsigned short *) (data + j*2);
            x = ((x>>12) & 0xf) | ((x<<4) & 0xfff0);
            * (unsigned short *) (data + j*2) = x;
         }
      }
      if (texture->format == IFT_FORMAT_rgba_5551_LE) {
         for (j=0; j < w * h; ++j) {
            unsigned short x = * (unsigned short *) (data + j*2);
            x = (x >> 15) | (x << 1);
            * (unsigned short *) (data + j*2) = x;
         }
      }

      if (fmt->fmt != 0) {
         glTexImage2D(GL_TEXTURE_2D, i, fmt->intfmt, w, h, 0, fmt->fmt, fmt->type, data);
         offset += w * h * fmt->blkbytes;
      } else {
         int size = ((w + fmt->blkx-1) / fmt->blkx) * ((h + fmt->blky-1) / fmt->blky) * fmt->blkbytes;
         glCompressedTexImage2D(GL_TEXTURE_2D, i, fmt->intfmt, w, h, 0, size, data);
         offset += size;
      }

      opengl_check();
   }

   if (gdraw->has_texture_max_level)
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mips-1);

   tex = gdraw_GLx_(WrappedTextureCreate)(gl_texture_handle, texture->w, texture->h, mips > 1);
   if (tex == nullptr)
      glDeleteTextures(1, &gl_texture_handle);
   opengl_check();
   return tex;
}

void RADLINK gdraw_GLx_(DestroyTextureFromResource)(GDrawTexture *tex)
{
   if (tex)
      gdraw_GLx_(WrappedTextureDestroy)(tex);
}

static rrbool hasext(const char *exts, const char *which)
{
   const char *where;
   size_t len;

#ifdef GDRAW_USE_glGetStringi
   if (exts == nullptr) {
      GLint i, num_exts;
      glGetIntegerv(GL_NUM_EXTENSIONS, &num_exts);
      for (i=0; i < num_exts; ++i)
         if (0==strcmp(which, (char const *) glGetStringi(GL_EXTENSIONS, i)))
            return 1;
      return 0;
   }
#endif

   where = exts;
   len = strlen(which);

   for(;;) {
      where = strstr(where, which);
      if (where == nullptr)
         return false;

      if (   (where == exts || *(where - 1) == ' ')   // starts with terminator
          && (where[len] == ' ' || where[len] == 0))  // ends with terminator
         return true;
      where += len;
   }
}

static GDrawFunctions *create_context(S32 w, S32 h)
{
   gdraw = IggyGDrawMalloc(sizeof(*gdraw));
   if (!gdraw) return nullptr;

   memset(gdraw, 0, sizeof(*gdraw));

   gdraw->texturecache = make_handle_cache(GDRAW_GLx_(RESOURCE_texture));
   gdraw->vbufcache = make_handle_cache(GDRAW_GLx_(RESOURCE_vertexbuffer));
   gdraw_HandleCacheInit(&gdraw->rendertargets, gdraw_limits[GDRAW_GLx_(RESOURCE_rendertarget)].num_handles, gdraw_limits[GDRAW_GLx_(RESOURCE_rendertarget)].num_bytes);

   if (!gdraw->texturecache || !gdraw->vbufcache || !make_quad_indices()) {
      free_gdraw();
      return nullptr;
   }

   opengl_check();

   gdraw->frametex_width = w;
   gdraw->frametex_height = h;
   gdraw->frame->cached = false;

   // if the globals have already been initialized, this has no effect;
   // otherwise it initializes them with no global texture storage and the
   // default global rendertarget storage

   glGenFramebuffers(1, &gdraw->framebuffer_stack_object);
   glGenFramebuffers(1, &gdraw->framebuffer_copy_to_texture);
   opengl_check();

   make_vertex_programs();
   // fragment shaders are created lazily

   gdraw_funcs.SetViewSizeAndWorldScale = gdraw_SetViewSizeAndWorldScale;
   gdraw_funcs.RenderingBegin  = gdraw_RenderingBegin;
   gdraw_funcs.RenderingEnd    = gdraw_RenderingEnd;
   gdraw_funcs.RenderTileBegin = gdraw_RenderTileBegin;
   gdraw_funcs.RenderTileEnd   = gdraw_RenderTileEnd;
   gdraw_funcs.GetInfo         = gdraw_GetInfo;
   gdraw_funcs.DescribeTexture = gdraw_DescribeTexture;
   gdraw_funcs.DescribeVertexBuffer = gdraw_DescribeVertexBuffer;

   gdraw_funcs.TextureDrawBufferBegin = gdraw_TextureDrawBufferBegin;
   gdraw_funcs.TextureDrawBufferEnd = gdraw_TextureDrawBufferEnd;

   gdraw_funcs.DrawIndexedTriangles = gdraw_DrawIndexedTriangles;
   gdraw_funcs.FilterQuad = gdraw_FilterQuad;

   gdraw_funcs.SetAntialiasTexture = gdraw_SetAntialiasTexture;

   gdraw_funcs.ClearStencilBits = gdraw_ClearStencilBits;
   gdraw_funcs.ClearID = gdraw_ClearID;

   gdraw_funcs.MakeTextureBegin = gdraw_MakeTextureBegin;
   gdraw_funcs.MakeTextureMore = nullptr;
   gdraw_funcs.MakeTextureEnd = gdraw_MakeTextureEnd;

   gdraw_funcs.UpdateTextureRect = gdraw_UpdateTextureRect;
   gdraw_funcs.UpdateTextureBegin = gdraw_UpdateTextureBegin;
   gdraw_funcs.UpdateTextureEnd = gdraw_UpdateTextureEnd;
   gdraw_funcs.FreeTexture = gdraw_FreeTexture;
   gdraw_funcs.TryToLockTexture = gdraw_TryToLockTexture;

   gdraw_funcs.MakeVertexBufferBegin = gdraw_MakeVertexBufferBegin;
   gdraw_funcs.MakeVertexBufferMore  = gdraw_MakeVertexBufferMore;
   gdraw_funcs.MakeVertexBufferEnd   = gdraw_MakeVertexBufferEnd;
   gdraw_funcs.TryToLockVertexBuffer = gdraw_TryToLockVertexBuffer;
   gdraw_funcs.FreeVertexBuffer = gdraw_FreeVertexBuffer;

   gdraw_funcs.UnlockHandles = gdraw_UnlockHandles;
   gdraw_funcs.SetTextureUniqueID = gdraw_SetTextureUniqueID;

   gdraw_funcs.MakeTextureFromResource = (gdraw_make_texture_from_resource *) gdraw_GLx_(MakeTextureFromResource);
   gdraw_funcs.FreeTextureFromResource = gdraw_GLx_(DestroyTextureFromResource);

   gdraw_funcs.Set3DTransform = gdraw_Set3DTransform;

   return &gdraw_funcs;
}

void gdraw_GLx_(DestroyContext)(void)
{
   if (gdraw)
   {
      GDrawStats stats={0};
      if (gdraw->texturecache)   gdraw_res_flush(gdraw->texturecache, &stats);
      if (gdraw->vbufcache)      gdraw_res_flush(gdraw->vbufcache, &stats);
      flush_rendertargets(&stats);

      if (gdraw->aa_tex)
         glDeleteTextures(1, &gdraw->aa_tex);

      if (gdraw->quad_ib)
         glDeleteBuffers(1, &gdraw->quad_ib);
   }

   opengl_check();
   free_gdraw();
}