aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/hw/xwin/glx/indirect.c
blob: 05e951e0a76fbe024edfcf5ce135baca9a3d7f20 (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
/*
 * File: indirect.c
 * Purpose: A GLX implementation that uses Windows OpenGL library
 *
 * Authors: Alexander Gottwald
 *          Jon TURNEY
 *
 * Copyright (c) Jon TURNEY 2009
 * Copyright (c) Alexander Gottwald 2004
 *
 * Portions of this file are copied from GL/apple/indirect.c,
 * which contains the following copyright:
 *
 * Copyright (c) 2007, 2008, 2009 Apple Inc.
 * Copyright (c) 2004 Torrey T. Lyons. All Rights Reserved.
 * Copyright (c) 2002 Greg Parker. All Rights Reserved.
 *
 * Portions of this file are copied from Mesa's xf86glx.c,
 * which contains the following copyright:
 *
 * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/*
  TODO:
  - hook up remaining unimplemented extensions
  - research what guarantees glXWaitX, glXWaitGL are supposed to offer, and implement then
    using GdiFlush and/or glFinish
  - pbuffer clobbering: we don't get async notification, but can we arrange to emit the
    event when we notice it's been clobbered? at the very least, check if it's been clobbered
    before using it?
  - are the __GLXConfig * we get handed back ones we are made (so we can extend the structure
    with privates?) Or are they created inside the GLX core as well?
*/

/*
  MSDN clarifications:

  It says SetPixelFormat()'s PIXELFORMATDESCRIPTOR pointer argument has no effect
  except on metafiles, this seems to mean that as it's ok to supply NULL if the DC
  is not for a metafile

  wglMakeCurrent ignores the hdc if hglrc is NULL, so wglMakeCurrent(NULL, NULL)
  is used to make no context current

*/

#ifdef HAVE_XWIN_CONFIG_H
#include <xwin-config.h>
#endif

#include "glwindows.h"
#include <glx/glxserver.h>
#include <glx/glxutil.h>
#include <glx/extension_string.h>
#include <GL/internal/glcore.h>
#include <GL/glxtokens.h>

#include <winpriv.h>
#include <wgl_ext_api.h>

extern Bool g_fNativeGl;

#define NUM_ELEMENTS(x) (sizeof(x)/ sizeof(x[1]))

/* ---------------------------------------------------------------------- */
/*
 *   structure definitions
 */

typedef struct __GLXWinContext  __GLXWinContext;
typedef struct __GLXWinDrawable __GLXWinDrawable;
typedef struct __GLXWinScreen glxWinScreen;
typedef struct __GLXWinConfig GLXWinConfig;

struct __GLXWinContext {
  __GLXcontext base;
  HGLRC ctx;                         /* Windows GL Context */
  __GLXWinContext *shareContext;     /* Context with which we will share display lists and textures */
  HWND hwnd;                         /* For detecting when HWND has changed */
};

struct __GLXWinDrawable
{
  __GLXdrawable base;
  __GLXWinContext *drawContext;
  __GLXWinContext *readContext;

  /* If this drawable is GLX_DRAWABLE_PBUFFER */
  HPBUFFERARB hPbuffer;

  /* If this drawable is GLX_DRAWABLE_PIXMAP */
  HDC dibDC;
  HBITMAP hDIB;
  HBITMAP hOldDIB; /* original DIB for DC */
  void *pOldBits; /* original pBits for this drawable's pixmap */
};

struct __GLXWinScreen
{
  __GLXscreen base;

  /* Supported GLX extensions */
  unsigned char glx_enable_bits[__GLX_EXT_BYTES];

  Bool has_WGL_ARB_multisample;
  Bool has_WGL_ARB_pixel_format;
  Bool has_WGL_ARB_pbuffer;
  Bool has_WGL_ARB_render_texture;

  /* wrapped screen functions */
  RealizeWindowProcPtr RealizeWindow;
  UnrealizeWindowProcPtr UnrealizeWindow;
  CopyWindowProcPtr CopyWindow;
};

struct __GLXWinConfig
{
  __GLXconfig base;
  int pixelFormatIndex;
};

/* ---------------------------------------------------------------------- */
/*
 * Various debug helpers
 */

#define GLWIN_DEBUG_HWND(hwnd)  \
    if (glxWinDebugSettings.dumpHWND) { \
        char buffer[1024]; \
        if (GetWindowText(hwnd, buffer, sizeof(buffer))==0) *buffer=0; \
        GLWIN_DEBUG_MSG("Got HWND %p for window '%s'", hwnd, buffer); \
    }

glxWinDebugSettingsRec glxWinDebugSettings = { 0, 0, 0, 0, 0, 0};

static void glxWinInitDebugSettings(void)
{
    char *envptr;

    envptr = getenv("GLWIN_ENABLE_DEBUG");
    if (envptr != NULL)
        glxWinDebugSettings.enableDebug = (atoi(envptr) == 1);

    envptr = getenv("GLWIN_ENABLE_TRACE");
    if (envptr != NULL)
        glxWinDebugSettings.enableTrace = (atoi(envptr) == 1);

    envptr = getenv("GLWIN_DUMP_PFD");
    if (envptr != NULL)
        glxWinDebugSettings.dumpPFD = (atoi(envptr) == 1);

    envptr = getenv("GLWIN_DUMP_HWND");
    if (envptr != NULL)
        glxWinDebugSettings.dumpHWND = (atoi(envptr) == 1);

    envptr = getenv("GLWIN_DUMP_DC");
    if (envptr != NULL)
        glxWinDebugSettings.dumpDC = (atoi(envptr) == 1);

    envptr = getenv("GLWIN_ENABLE_GLCALL_TRACE");
    if (envptr != NULL)
        glxWinDebugSettings.enableGLcallTrace = (atoi(envptr) == 1);

    envptr = getenv("GLWIN_ENABLE_WGLCALL_TRACE");
    if (envptr != NULL)
        glxWinDebugSettings.enableWGLcallTrace = (atoi(envptr) == 1);

    envptr = getenv("GLWIN_DEBUG_ALL");
    if (envptr != NULL)
      {
        glxWinDebugSettings.enableDebug = 1;
        glxWinDebugSettings.enableTrace = 1;
        glxWinDebugSettings.dumpPFD = 1;
        glxWinDebugSettings.dumpHWND = 1;
        glxWinDebugSettings.dumpDC = 1;
        glxWinDebugSettings.enableGLcallTrace = 1;
        glxWinDebugSettings.enableWGLcallTrace = 1;
      }
}

static
const char *glxWinErrorMessage(void)
{
  static char errorbuffer[1024];

  if (!FormatMessage(
                     FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                     NULL,
                     GetLastError(),
                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                     (LPTSTR) &errorbuffer,
                     sizeof(errorbuffer),
                     NULL ))
    {
      snprintf(errorbuffer, sizeof(errorbuffer), "Unknown error in FormatMessage: %08x!", (unsigned)GetLastError());
    }

  if (errorbuffer[strlen(errorbuffer)-1] == '\n')
    errorbuffer[strlen(errorbuffer)-1] = 0;

  return errorbuffer;
}

static void pfdOut(const PIXELFORMATDESCRIPTOR *pfd);

#define DUMP_PFD_FLAG(flag) \
    if (pfd->dwFlags & flag) { \
        ErrorF("%s%s", pipesym, #flag); \
        pipesym = " | "; \
    }

static void pfdOut(const PIXELFORMATDESCRIPTOR *pfd)
{
    const char *pipesym = ""; /* will be set after first flag dump */
    ErrorF("PIXELFORMATDESCRIPTOR:\n");
    ErrorF("nSize = %u\n", pfd->nSize);
    ErrorF("nVersion = %u\n", pfd->nVersion);
    ErrorF("dwFlags = %lu = {", pfd->dwFlags);
        DUMP_PFD_FLAG(PFD_DOUBLEBUFFER);
        DUMP_PFD_FLAG(PFD_STEREO);
        DUMP_PFD_FLAG(PFD_DRAW_TO_WINDOW);
        DUMP_PFD_FLAG(PFD_DRAW_TO_BITMAP);
        DUMP_PFD_FLAG(PFD_SUPPORT_GDI);
        DUMP_PFD_FLAG(PFD_SUPPORT_OPENGL);
        DUMP_PFD_FLAG(PFD_GENERIC_FORMAT);
        DUMP_PFD_FLAG(PFD_NEED_PALETTE);
        DUMP_PFD_FLAG(PFD_NEED_SYSTEM_PALETTE);
        DUMP_PFD_FLAG(PFD_SWAP_EXCHANGE);
        DUMP_PFD_FLAG(PFD_SWAP_COPY);
        DUMP_PFD_FLAG(PFD_SWAP_LAYER_BUFFERS);
        DUMP_PFD_FLAG(PFD_GENERIC_ACCELERATED);
        DUMP_PFD_FLAG(PFD_DEPTH_DONTCARE);
        DUMP_PFD_FLAG(PFD_DOUBLEBUFFER_DONTCARE);
        DUMP_PFD_FLAG(PFD_STEREO_DONTCARE);
    ErrorF("}\n");

    ErrorF("iPixelType = %hu = %s\n", pfd->iPixelType,
            (pfd->iPixelType == PFD_TYPE_RGBA ? "PFD_TYPE_RGBA" : "PFD_TYPE_COLORINDEX"));
    ErrorF("cColorBits = %hhu\n", pfd->cColorBits);
    ErrorF("cRedBits = %hhu\n", pfd->cRedBits);
    ErrorF("cRedShift = %hhu\n", pfd->cRedShift);
    ErrorF("cGreenBits = %hhu\n", pfd->cGreenBits);
    ErrorF("cGreenShift = %hhu\n", pfd->cGreenShift);
    ErrorF("cBlueBits = %hhu\n", pfd->cBlueBits);
    ErrorF("cBlueShift = %hhu\n", pfd->cBlueShift);
    ErrorF("cAlphaBits = %hhu\n", pfd->cAlphaBits);
    ErrorF("cAlphaShift = %hhu\n", pfd->cAlphaShift);
    ErrorF("cAccumBits = %hhu\n", pfd->cAccumBits);
    ErrorF("cAccumRedBits = %hhu\n", pfd->cAccumRedBits);
    ErrorF("cAccumGreenBits = %hhu\n", pfd->cAccumGreenBits);
    ErrorF("cAccumBlueBits = %hhu\n", pfd->cAccumBlueBits);
    ErrorF("cAccumAlphaBits = %hhu\n", pfd->cAccumAlphaBits);
    ErrorF("cDepthBits = %hhu\n", pfd->cDepthBits);
    ErrorF("cStencilBits = %hhu\n", pfd->cStencilBits);
    ErrorF("cAuxBuffers = %hhu\n", pfd->cAuxBuffers);
    ErrorF("iLayerType = %hhu\n", pfd->iLayerType);
    ErrorF("bReserved = %hhu\n", pfd->bReserved);
    ErrorF("dwLayerMask = %lu\n", pfd->dwLayerMask);
    ErrorF("dwVisibleMask = %lu\n", pfd->dwVisibleMask);
    ErrorF("dwDamageMask = %lu\n", pfd->dwDamageMask);
    ErrorF("\n");
}

static const char *
visual_class_name(int cls)
{
  switch (cls) {
  case GLX_STATIC_COLOR:
    return "StaticColor";
  case GLX_PSEUDO_COLOR:
    return "PseudoColor";
  case GLX_STATIC_GRAY:
    return "StaticGray";
  case GLX_GRAY_SCALE:
    return "GrayScale";
  case GLX_TRUE_COLOR:
     return "TrueColor";
  case GLX_DIRECT_COLOR:
     return "DirectColor";
  default:
    return "-none-";
  }
}

static const char *
swap_method_name(int mthd)
{
  switch (mthd)
    {
    case GLX_SWAP_EXCHANGE_OML:
      return "xchg";
    case GLX_SWAP_COPY_OML:
      return "copy";
    case GLX_SWAP_UNDEFINED_OML:
      return "    ";
    default:
      return "????";
    }
}

static void
fbConfigsDump(unsigned int n, __GLXconfig *c)
{
  ErrorF("%d fbConfigs\n", n);
  ErrorF("pxf vis  fb                      render         Ste                     aux    accum        MS    drawable             Group/\n");
  ErrorF("idx  ID  ID VisualType Depth Lvl RGB CI DB Swap reo  R  G  B  A   Z  S  buf AR AG AB AA  bufs num  W P Pb  Float Trans Caveat\n");
  ErrorF("-----------------------------------------------------------------------------------------------------------------------------\n");

  while (c != NULL)
    {
      unsigned int i = ((GLXWinConfig *)c)->pixelFormatIndex;

      ErrorF("%3d  %2x  %2x "
             "%-11s"
             " %3d %3d   %s   %s  %s %s  %s  "
             "%2d %2d %2d %2d  "
             "%2d %2d  "
             "%2d  "
             "%2d %2d %2d %2d"
             "   %2d   %2d"
             "  %s %s %s "
             "    %s   "
             "  %s   "
             "  %d %s"
             "\n",
             i, c->visualID, c->fbconfigID,
             visual_class_name(c->visualType),
             c->rgbBits ? c->rgbBits : c->indexBits,
             c->level,
	     (c->renderType & GLX_RGBA_BIT) ? "y" : ".",
	     (c->renderType & GLX_COLOR_INDEX_BIT) ? "y" : ".",
	     c->doubleBufferMode ? "y" : ".",
             swap_method_name(c->swapMethod),
	     c->stereoMode ? "y" : ".",
             c->redBits, c->greenBits, c->blueBits, c->alphaBits,
             c->depthBits, c->stencilBits,
             c->numAuxBuffers,
             c->accumRedBits, c->accumGreenBits, c->accumBlueBits, c->accumAlphaBits,
             c->sampleBuffers, c->samples,
             (c->drawableType & GLX_WINDOW_BIT) ? "y" : ".",
             (c->drawableType & GLX_PIXMAP_BIT) ? "y" : ".",
             (c->drawableType & GLX_PBUFFER_BIT) ? "y" : ".",
             ".",
             (c->transparentPixel != GLX_NONE_EXT) ? "y" : ".",
             c->visualSelectGroup, (c->visualRating == GLX_SLOW_VISUAL_EXT) ? "*" : " ");

      c = c->next;
    }
}

/* ---------------------------------------------------------------------- */
/*
 * Forward declarations
 */

static __GLXscreen *glxWinScreenProbe(ScreenPtr pScreen);
static __GLXcontext *glxWinCreateContext(__GLXscreen *screen,
                                        __GLXconfig *modes,
                                        __GLXcontext *baseShareContext);
static __GLXdrawable *glxWinCreateDrawable(__GLXscreen *screen,
                                          DrawablePtr pDraw,
                                          int type,
                                          XID drawId,
                                          __GLXconfig *conf);

static Bool glxWinRealizeWindow(WindowPtr pWin);
static Bool glxWinUnrealizeWindow(WindowPtr pWin);
static void glxWinCopyWindow(WindowPtr pWindow, DDXPointRec ptOldOrg, RegionPtr prgnSrc);

static HDC glxWinMakeDC(__GLXWinContext *gc, __GLXWinDrawable *draw, HDC *hdc, HWND *hwnd);
static void glxWinReleaseDC(HWND hwnd, HDC hdc, __GLXWinDrawable *draw);

static void glxWinCreateConfigs(HDC dc, glxWinScreen *screen);
static void glxWinCreateConfigsExt(HDC hdc, glxWinScreen *screen);
static int fbConfigToPixelFormat(__GLXconfig *mode, PIXELFORMATDESCRIPTOR *pfdret, int drawableTypeOverride);
static int fbConfigToPixelFormatIndex(HDC hdc, __GLXconfig *mode, int drawableTypeOverride, glxWinScreen *winScreen);

/* ---------------------------------------------------------------------- */
/*
 * The GLX provider
 */

__GLXprovider __glXWGLProvider = {
    glxWinScreenProbe,
    "Win32 native WGL",
    NULL
};

void
glxWinPushNativeProvider(void)
{
  if (g_fNativeGl)
    GlxPushProvider(&__glXWGLProvider);
}

/* ---------------------------------------------------------------------- */
/*
 * Screen functions
 */

static void
glxWinScreenDestroy(__GLXscreen *screen)
{
    GLWIN_DEBUG_MSG("glxWinScreenDestroy(%p)", screen);
    __glXScreenDestroy(screen);
    xfree(screen);
}

static int
glxWinScreenSwapInterval(__GLXdrawable *drawable, int interval)
{
  BOOL ret = wglSwapIntervalEXTWrapper(interval);
  if (!ret)
    {
      ErrorF("wglSwapIntervalEXT interval %d failed:%s\n", interval, glxWinErrorMessage());
    }
  return ret;
}

/* This is called by GlxExtensionInit() asking the GLX provider if it can handle the screen... */
static __GLXscreen *
glxWinScreenProbe(ScreenPtr pScreen)
{
    glxWinScreen *screen;
    const char *gl_extensions;
    const char *wgl_extensions;
    HWND hwnd;
    HDC hdc;
    HGLRC hglrc;

    GLWIN_DEBUG_MSG("glxWinScreenProbe");

    glxWinInitDebugSettings();

    if (pScreen == NULL)
	return NULL;

    if (!winCheckScreenAiglxIsSupported(pScreen))
      {
        LogMessage(X_ERROR,"AIGLX: No native OpenGL in modes with a root window\n");
        return NULL;
      }

    screen = xcalloc(1, sizeof(glxWinScreen));

    if (NULL == screen)
	return NULL;

    /* Wrap RealizeWindow, UnrealizeWindow and CopyWindow on this screen */
    screen->RealizeWindow = pScreen->RealizeWindow;
    pScreen->RealizeWindow = glxWinRealizeWindow;
    screen->UnrealizeWindow = pScreen->UnrealizeWindow;
    pScreen->UnrealizeWindow = glxWinUnrealizeWindow;
    screen->CopyWindow = pScreen->CopyWindow;
    pScreen->CopyWindow = glxWinCopyWindow;

    /* Dump out some useful information about the native renderer */

    // create window class
#define WIN_GL_TEST_WINDOW_CLASS "XWinGLTest"
    {
      static wATOM glTestWndClass = 0;
      if (glTestWndClass == 0)
        {
          WNDCLASSEX wc;
          wc.cbSize = sizeof(WNDCLASSEX);
          wc.style = CS_HREDRAW | CS_VREDRAW;
          wc.lpfnWndProc = DefWindowProc;
          wc.cbClsExtra = 0;
          wc.cbWndExtra = 0;
          wc.hInstance = GetModuleHandle(NULL);
          wc.hIcon = 0;
          wc.hCursor = 0;
          wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
          wc.lpszMenuName = NULL;
          wc.lpszClassName = WIN_GL_TEST_WINDOW_CLASS;
          wc.hIconSm = 0;
          RegisterClassEx (&wc);
      }
    }

    // create an invisible window for a scratch DC
    hwnd = CreateWindowExA(0,
                           WIN_GL_TEST_WINDOW_CLASS,
                           "XWin GL Renderer Capabilities Test Window",
                           0, 0, 0, 0, 0, NULL, NULL, GetModuleHandle(NULL), NULL);
    if (hwnd == NULL)
      LogMessage(X_ERROR,"AIGLX: Couldn't create a window for render capabilities testing\n");

    hdc = GetDC(hwnd);

    // we must set a pixel format before we can create a context, just use the first one...
    SetPixelFormat(hdc, 1, NULL);
    hglrc = wglCreateContext(hdc);
    wglMakeCurrent(hdc, hglrc);

    // initialize wgl extension proc pointers (don't call them before here...)
    // (but we need to have a current context for them to be resolvable)
    wglResolveExtensionProcs();

    ErrorF("GL_VERSION:    %s\n", glGetStringWrapperNonstatic(GL_VERSION));
    ErrorF("GL_VENDOR:     %s\n", glGetStringWrapperNonstatic(GL_VENDOR));
    ErrorF("GL_RENDERER:   %s\n", glGetStringWrapperNonstatic(GL_RENDERER));
    gl_extensions = (const char *)glGetStringWrapperNonstatic(GL_EXTENSIONS);
    ErrorF("GL_EXTENSIONS: %s\n", gl_extensions);
    wgl_extensions = wglGetExtensionsStringARBWrapper(hdc);
    ErrorF("WGL_EXTENSIONS:%s\n", wgl_extensions);

    // Can you see the problem here?  The extensions string is DC specific
    // Different DCs for windows on a multimonitor system driven by multiple cards
    // might have completely different capabilities.  Of course, good luck getting
    // those screens to be accelerated in XP and earlier...

    {
      // testing facility to not use any WGL extensions
      char *envptr = getenv("GLWIN_NO_WGL_EXTENSIONS");
      if ((envptr != NULL) && (atoi(envptr) != 0))
        {
          ErrorF("GLWIN_NO_WGL_EXTENSIONS is set, ignoring WGL_EXTENSIONS\n");
          wgl_extensions = "";
        }
    }

    {
      Bool glx_sgi_make_current_read = FALSE;

      //
      // Based on the WGL extensions available, enable various GLX extensions
      // XXX: make this table-driven ?
      //
      memset(screen->glx_enable_bits, 0, __GLX_EXT_BYTES);

      __glXEnableExtension(screen->glx_enable_bits, "GLX_EXT_visual_info");
      __glXEnableExtension(screen->glx_enable_bits, "GLX_EXT_visual_rating");
      __glXEnableExtension(screen->glx_enable_bits, "GLX_EXT_import_context");
      __glXEnableExtension(screen->glx_enable_bits, "GLX_OML_swap_method");
      __glXEnableExtension(screen->glx_enable_bits, "GLX_SGIX_fbconfig");

      if (strstr(wgl_extensions, "WGL_ARB_make_current_read"))
        {
          __glXEnableExtension(screen->glx_enable_bits, "GLX_SGI_make_current_read");
          LogMessage(X_INFO, "AIGLX: enabled GLX_SGI_make_current_read\n");
          glx_sgi_make_current_read = TRUE;
        }

      if (strstr(gl_extensions, "GL_WIN_swap_hint"))
        {
          __glXEnableExtension(screen->glx_enable_bits, "GLX_MESA_copy_sub_buffer");
          LogMessage(X_INFO, "AIGLX: enabled GLX_MESA_copy_sub_buffer\n");
        }

      if (strstr(wgl_extensions, "WGL_EXT_swap_control"))
        {
          __glXEnableExtension(screen->glx_enable_bits, "GLX_SGI_swap_control");
          __glXEnableExtension(screen->glx_enable_bits, "GLX_MESA_swap_control");
          LogMessage(X_INFO, "AIGLX: enabled GLX_SGI_swap_control and GLX_MESA_swap_control\n");
        }

/*       // Hmm?  screen->texOffset */
/*       if (strstr(wgl_extensions, "WGL_ARB_render_texture")) */
/*         { */
/*           __glXEnableExtension(screen->glx_enable_bits, "GLX_EXT_texture_from_pixmap"); */
/*           LogMessage(X_INFO, "AIGLX: GLX_EXT_texture_from_pixmap backed by buffer objects\n"); */
/*           screen->has_WGL_ARB_render_texture = TRUE; */
/*         } */

      if (strstr(wgl_extensions, "WGL_ARB_pbuffer"))
        {
          __glXEnableExtension(screen->glx_enable_bits, "GLX_SGIX_pbuffer");
          LogMessage(X_INFO, "AIGLX: enabled GLX_SGIX_pbuffer\n");
          screen->has_WGL_ARB_pbuffer = TRUE;
        }

      if (strstr(wgl_extensions, "WGL_ARB_multisample"))
        {
          __glXEnableExtension(screen->glx_enable_bits, "GLX_ARB_multisample");
          __glXEnableExtension(screen->glx_enable_bits, "GLX_SGIS_multisample");
          LogMessage(X_INFO, "AIGLX: enabled GLX_ARB_multisample and GLX_SGIS_multisample\n");
          screen->has_WGL_ARB_multisample = TRUE;
        }

      screen->base.destroy = glxWinScreenDestroy;
      screen->base.createContext = glxWinCreateContext;
      screen->base.createDrawable = glxWinCreateDrawable;
      screen->base.swapInterval = glxWinScreenSwapInterval;
      screen->base.hyperpipeFuncs = NULL;
      screen->base.swapBarrierFuncs = NULL;
      screen->base.pScreen = pScreen;

      if (strstr(wgl_extensions, "WGL_ARB_pixel_format"))
        {
          glxWinCreateConfigsExt(hdc, screen);
          screen->has_WGL_ARB_pixel_format = TRUE;
        }
      else
        {
          glxWinCreateConfigs(hdc, screen);
          screen->has_WGL_ARB_pixel_format = FALSE;
        }
      // Initializes screen->base.fbconfigs and screen->base.numFBConfigs

      /* These will be set by __glXScreenInit */
      screen->base.visuals = NULL;
      screen->base.numVisuals = 0;

      __glXScreenInit(&screen->base, pScreen);

      // dump out fbConfigs now fbConfigIds and visualIDs have been assigned
      fbConfigsDump(screen->base.numFBConfigs, screen->base.fbconfigs);

      // Override the GL extensions string set by __glXScreenInit()
      screen->base.GLextensions = xstrdup(gl_extensions);

      // Generate the GLX extensions string (overrides that set by __glXScreenInit())
      {
        unsigned int buffer_size = __glXGetExtensionString(screen->glx_enable_bits, NULL);
        if (buffer_size > 0)
          {
            if (screen->base.GLXextensions != NULL)
              {
                xfree(screen->base.GLXextensions);
              }

            screen->base.GLXextensions = xnfalloc(buffer_size);
            __glXGetExtensionString(screen->glx_enable_bits, screen->base.GLXextensions);
          }
      }

      //
      // Override the GLX version (__glXScreenInit() sets it to "1.2")
      // if we have all the needed extensionsto operate as a higher version
      //
      // SGIX_fbconfig && SGIX_pbuffer && SGI_make_current_read -> 1.3
      // ARB_multisample -> 1.4
      //
      if (screen->has_WGL_ARB_pbuffer && glx_sgi_make_current_read)
        {
          xfree(screen->base.GLXversion);

          if (screen->has_WGL_ARB_multisample)
            {
              screen->base.GLXversion = xstrdup("1.4");
              screen->base.GLXminor=4;
              /*
                XXX: this just controls the version string returned to glXQueryServerString()
                there is currently no way to control the version number the server returns to
                glXQueryVersion()...
              */
            }
          else
            {
              screen->base.GLXversion = xstrdup("1.3");
              screen->base.GLXminor=3;
            }
          LogMessage(X_INFO, "AIGLX: Set GLX version to %s\n", screen->base.GLXversion);
        }
    }

    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hglrc);
    ReleaseDC(hwnd, hdc);
    DestroyWindow(hwnd);

    return &screen->base;
}

/* ---------------------------------------------------------------------- */
/*
 * Window functions
 */

static Bool
glxWinRealizeWindow(WindowPtr pWin)
{
    Bool result;
    ScreenPtr pScreen = pWin->drawable.pScreen;
    glxWinScreen *screenPriv = (glxWinScreen *) glxGetScreen(pScreen);

    GLWIN_DEBUG_MSG("glxWinRealizeWindow");

    /* Allow the window to be created (RootlessRealizeWindow is inside our wrap) */
    pScreen->RealizeWindow = screenPriv->RealizeWindow;
    result = pScreen->RealizeWindow(pWin);
    pScreen->RealizeWindow = glxWinRealizeWindow;

    return result;
}


static void
glxWinCopyWindow(WindowPtr pWindow, DDXPointRec ptOldOrg, RegionPtr prgnSrc)
{
    __GLXWinDrawable *pGlxDraw;
    ScreenPtr pScreen = pWindow->drawable.pScreen;
    glxWinScreen *screenPriv = (glxWinScreen *) glxGetScreen(pScreen);

    GLWIN_TRACE_MSG("glxWinCopyWindow pWindow %p", pWindow);

    dixLookupResourceByType((pointer) &pGlxDraw, pWindow->drawable.id, __glXDrawableRes,
				NullClient, DixUnknownAccess);


    /*
       Discard any CopyWindow requests if a GL drawing context is pointing at the window

       For regions which are being drawn by GL, the shadow framebuffer doesn't have the
       correct bits, so we wish to avoid shadow framebuffer damage occuring, which will
       cause those incorrect bits to be transferred to the display....
    */
    if (pGlxDraw && pGlxDraw->drawContext)
      {
        GLWIN_DEBUG_MSG("glxWinCopyWindow: discarding");
        return;
      }

    GLWIN_DEBUG_MSG("glxWinCopyWindow - passing to hw layer");

    pScreen->CopyWindow = screenPriv->CopyWindow;
    pScreen->CopyWindow(pWindow, ptOldOrg, prgnSrc);
    pScreen->CopyWindow = glxWinCopyWindow;
}

static Bool
glxWinUnrealizeWindow(WindowPtr pWin)
{
    Bool result;
    ScreenPtr pScreen = pWin->drawable.pScreen;
    glxWinScreen *screenPriv = (glxWinScreen *)glxGetScreen(pScreen);

    GLWIN_DEBUG_MSG("glxWinUnrealizeWindow");

    pScreen->UnrealizeWindow = screenPriv->UnrealizeWindow;
    result = pScreen->UnrealizeWindow(pWin);
    pScreen->UnrealizeWindow = glxWinUnrealizeWindow;

    return result;
}

/* ---------------------------------------------------------------------- */
/*
 * Drawable functions
 */

static GLboolean
glxWinDrawableSwapBuffers(__GLXdrawable *base)
{
    HDC dc;
    HWND hwnd;
    BOOL ret;
    __GLXWinDrawable *draw = (__GLXWinDrawable *)base;

    /* Swap buffers on the last active context for drawing on the drawable */
    if (draw->drawContext == NULL)
      {
        GLWIN_TRACE_MSG("glxWinSwapBuffers - no context for drawable");
        return GL_FALSE;
      }

    GLWIN_TRACE_MSG("glxWinSwapBuffers on drawable %p, last context %p (native ctx %p)", base, draw->drawContext, draw->drawContext->ctx);

    /*
       draw->drawContext->base.drawPriv will not be set if the context is not current anymore,
       but if it is, it should point to this drawable....
    */
    assert((draw->drawContext->base.drawPriv == NULL) || (draw->drawContext->base.drawPriv == base));

    dc = glxWinMakeDC(draw->drawContext, draw, &dc, &hwnd);
    if (dc == NULL)
      return GL_FALSE;

    ret = wglSwapLayerBuffers(dc, WGL_SWAP_MAIN_PLANE);

    glxWinReleaseDC(hwnd, dc, draw);

    if (!ret)
      {
        ErrorF("wglSwapBuffers failed: %s\n", glxWinErrorMessage());
        return GL_FALSE;
      }

    return GL_TRUE;
}

static void
glxWinDrawableCopySubBuffer(__GLXdrawable *drawable,
                            int x, int y, int w, int h)
{
  glAddSwapHintRectWINWrapperNonstatic(x, y, w, h);
  glxWinDrawableSwapBuffers(drawable);
}

static void
glxWinDrawableDestroy(__GLXdrawable *base)
{
  __GLXWinDrawable *glxPriv = (__GLXWinDrawable *)base;

  if (glxPriv->drawContext && (__glXLastContext == &((glxPriv->drawContext)->base)))
    {
      // if this context is current and has unflushed commands, say we have flushed them
      // (don't actually flush them, the window is going away anyhow, and an implict flush occurs
      // on the next context change)
      // (GLX core considers it an error when we try to select a new current context if the old one
      // has unflushed commands, but the window has disappeared..)
      __GLX_NOTE_FLUSHED_CMDS(__glXLastContext);
      __glXLastContext = NULL;
    }

  if (glxPriv->hPbuffer)
    if (!wglDestroyPbufferARBWrapper(glxPriv->hPbuffer))
      {
        ErrorF("wglDestroyPbufferARB failed: %s\n", glxWinErrorMessage());
      }

  if (glxPriv->dibDC)
    {
      // restore the default DIB
      SelectObject(glxPriv->dibDC, glxPriv->hOldDIB);

      if (!DeleteDC(glxPriv->dibDC))
        {
          ErrorF("DeleteDC failed: %s\n", glxWinErrorMessage());
        }
    }

  if (glxPriv->hDIB)
    {
      if (!DeleteObject(glxPriv->hDIB))
        {
          ErrorF("DeleteObject failed: %s\n", glxWinErrorMessage());
        }

      ((PixmapPtr)glxPriv->base.pDraw)->devPrivate.ptr = glxPriv->pOldBits;
    }

  GLWIN_DEBUG_MSG("glxWinDestroyDrawable");
  xfree(glxPriv);
}

static __GLXdrawable *
glxWinCreateDrawable(__GLXscreen *screen,
                    DrawablePtr pDraw,
                    int type,
                    XID drawId,
                    __GLXconfig *conf)
{
  __GLXWinDrawable *glxPriv;

  glxPriv = xalloc(sizeof *glxPriv);

  if (glxPriv == NULL)
      return NULL;

  memset(glxPriv, 0, sizeof *glxPriv);

  if(!__glXDrawableInit(&glxPriv->base, screen, pDraw, type, drawId, conf)) {
    xfree(glxPriv);
    return NULL;
  }

  glxPriv->base.destroy       = glxWinDrawableDestroy;
  glxPriv->base.swapBuffers   = glxWinDrawableSwapBuffers;
  glxPriv->base.copySubBuffer = glxWinDrawableCopySubBuffer;
  // glxPriv->base.waitX  what are these for?
  // glxPriv->base.waitGL

  GLWIN_DEBUG_MSG("glxWinCreateDrawable %p", glxPriv);

  return &glxPriv->base;
}

/* ---------------------------------------------------------------------- */
/*
 * Texture functions
 */

static
int glxWinBindTexImage(__GLXcontext  *baseContext,
                      int            buffer,
                      __GLXdrawable *pixmap)
{
  ErrorF("glxWinBindTexImage: not implemented\n");
  return FALSE;
}

static
int glxWinReleaseTexImage(__GLXcontext  *baseContext,
                         int            buffer,
                         __GLXdrawable *pixmap)
{
  ErrorF(" glxWinReleaseTexImage: not implemented\n");
  return FALSE;
}

/* ---------------------------------------------------------------------- */
/*
 * Lazy update context implementation
 *
 * WGL contexts are created for a specific HDC, so we cannot create the WGL
 * context in glxWinCreateContext(), we must defer creation until the context
 * is actually used on a specifc drawable which is connected to a native window,
 * pbuffer or DIB
 *
 * The WGL context may be used on other, compatible HDCs, so we don't need to
 * recreate it for every new native window
 *
 * XXX: I wonder why we can't create the WGL context on the screen HDC ?
 * Basically we assume all HDCs are compatible at the moment: if they are not
 * we are in a muddle, there was some code in the old implementation to attempt
 * to transparently migrate a context to a new DC by copying state and sharing
 * lists with the old one...
 */

static void
glxWinSetPixelFormat(__GLXWinContext *gc, HDC hdc, int bppOverride, int drawableTypeOverride)
{
  __GLXscreen *screen = gc->base.pGlxScreen;
  glxWinScreen *winScreen = (glxWinScreen *)screen;

  __GLXconfig *config = gc->base.config;
  GLXWinConfig *winConfig = (GLXWinConfig *)config;
  
  GLWIN_DEBUG_MSG("glxWinSetPixelFormat: pixelFormatIndex %d", winConfig->pixelFormatIndex);

  /*
    Normally, we can just use the the pixelFormatIndex corresponding
    to the fbconfig which has been specified by the client
  */

  if (!((bppOverride && (bppOverride != (config->redBits + config->greenBits + config->blueBits) ))
        || ((config->drawableType & drawableTypeOverride) == 0)))
    {
      if (!SetPixelFormat(hdc, winConfig->pixelFormatIndex, NULL))
        {
          ErrorF("SetPixelFormat error: %s\n", glxWinErrorMessage());
          return;
        }

      return;
    }

  /*
    However, in certain special cases this pixel format will be incompatible with the
    use we are going to put it to, so we need to re-evaluate the pixel format to use:

    1) When PFD_DRAW_TO_BITMAP is set, ChoosePixelFormat() always returns a format with
       the cColorBits we asked for, so we need to ensure it matches the bpp of the bitmap

    2) Applications may assume that visuals selected with glXChooseVisual() work with
       pixmap drawables (there is no attribute to explicitly query for pixmap drawable
       support as there is for glXChooseFBConfig())
       (it's arguable this is an error in the application, but we try to make it work)

       pixmap rendering is always slow for us, so we don't want to choose those visuals
       by default, but if the actual drawable type we're trying to select the context
       on (drawableTypeOverride) isn't supported by the selected fbConfig, reconsider
       and see if we can find a suitable one...
   */
  ErrorF("glxWinSetPixelFormat: having second thoughts: cColorbits %d, bppOveride %d; config->drawableType %d, drawableTypeOverride %d\n",
         (config->redBits + config->greenBits + config->blueBits), bppOverride, config->drawableType, drawableTypeOverride);

  if (!winScreen->has_WGL_ARB_pixel_format)
    {
      PIXELFORMATDESCRIPTOR pfd;
      int pixelFormat;
      
      /* convert fbConfig to PFD */
      if (fbConfigToPixelFormat(gc->base.config, &pfd, drawableTypeOverride))
        {
          ErrorF("glxWinSetPixelFormat: fbConfigToPixelFormat failed\n");
          return;
        }

      if (glxWinDebugSettings.dumpPFD)
        pfdOut(&pfd);

      if (bppOverride)
        {
          GLWIN_DEBUG_MSG("glxWinSetPixelFormat: Forcing bpp from %d to %d\n", pfd.cColorBits, bppOverride);
          pfd.cColorBits = bppOverride;
        }

      pixelFormat = ChoosePixelFormat(hdc, &pfd);
      if (pixelFormat == 0)
        {
          ErrorF("ChoosePixelFormat error: %s\n", glxWinErrorMessage());
          return;
        }

      GLWIN_DEBUG_MSG("ChoosePixelFormat: chose pixelFormatIndex %d", pixelFormat);
      ErrorF("ChoosePixelFormat: chose pixelFormatIndex %d (rather than %d as originally planned)\n", pixelFormat, winConfig->pixelFormatIndex);

      if (!SetPixelFormat(hdc, pixelFormat, &pfd))
        {
          ErrorF("SetPixelFormat error: %s\n", glxWinErrorMessage());
          return;
        }
    }
  else
    {
      int pixelFormat = fbConfigToPixelFormatIndex(hdc, gc->base.config, drawableTypeOverride, winScreen);
      if (pixelFormat == 0)
        {
          ErrorF("wglChoosePixelFormat error: %s\n", glxWinErrorMessage());
          return;
        }

      GLWIN_DEBUG_MSG("wglChoosePixelFormat: chose pixelFormatIndex %d", pixelFormat);
      ErrorF("wglChoosePixelFormat: chose pixelFormatIndex %d (rather than %d as originally planned)\n", pixelFormat, winConfig->pixelFormatIndex);

      if (!SetPixelFormat(hdc, pixelFormat, NULL))
        {
          ErrorF("SetPixelFormat error: %s\n", glxWinErrorMessage());
          return;
        }
    }
}

static HDC
glxWinMakeDC(__GLXWinContext *gc, __GLXWinDrawable *draw, HDC *hdc, HWND *hwnd)
{
  *hdc = NULL;
  *hwnd = NULL;

  if (draw == NULL)
    {
      GLWIN_TRACE_MSG("No drawable for context %p (native ctx %p)", gc, gc->ctx);
      return NULL;
    }

  switch (draw->base.type)
  {
    case GLX_DRAWABLE_WINDOW:
    {
      winWindowInfoRec winInfo;
      WindowPtr pWin;

      pWin = (WindowPtr) draw->base.pDraw;
      if (pWin == NULL)
        {
          GLWIN_TRACE_MSG("for drawable %p, no WindowPtr", pWin);
          return NULL;
        }

      winGetWindowInfo(pWin, &winInfo);
      *hwnd = winInfo.hwnd;

      if (winInfo.hwnd == NULL)
        {
          ErrorF("No HWND error: %s\n", glxWinErrorMessage());
          return NULL;
        }

      *hdc = GetDC(winInfo.hwnd);

      if (*hdc == NULL)
        ErrorF("GetDC error: %s\n", glxWinErrorMessage());

      /* Check if the hwnd has changed... */
      if (winInfo.hwnd != gc->hwnd)
        {
          if (glxWinDebugSettings.enableTrace)
            GLWIN_DEBUG_HWND(winInfo.hwnd);

          GLWIN_TRACE_MSG("for context %p (native ctx %p), hWnd changed from %p to %p", gc, gc->ctx, gc->hwnd, winInfo.hwnd);
          gc->hwnd = winInfo.hwnd;

          /* We must select a pixelformat, but SetPixelFormat can only be called once for a window... */
          glxWinSetPixelFormat(gc, *hdc, 0, GLX_WINDOW_BIT);
        }
    }
    break;

  case GLX_DRAWABLE_PBUFFER:
    {
      *hdc = wglGetPbufferDCARBWrapper(draw->hPbuffer);

      if (*hdc == NULL)
        ErrorF("GetDC (pbuffer) error: %s\n", glxWinErrorMessage());
    }
    break;

  case GLX_DRAWABLE_PIXMAP:
    {
      *hdc = draw->dibDC;
    }
    break;

  default:
    {
      ErrorF("glxWinMakeDC: tried to makeDC for unhandled drawable type %d\n", draw->base.type);
    }
  }

  if (glxWinDebugSettings.dumpDC)
    GLWIN_DEBUG_MSG("Got HDC %p", *hdc);

  return *hdc;
}

static void
glxWinReleaseDC(HWND hwnd, HDC hdc,__GLXWinDrawable *draw)
{
  switch (draw->base.type)
  {
    case GLX_DRAWABLE_WINDOW:
    {
      ReleaseDC(hwnd, hdc);
    }
    break;

  case GLX_DRAWABLE_PBUFFER:
    {
      if (!wglReleasePbufferDCARBWrapper(draw->hPbuffer, hdc))
        {
          ErrorF("wglReleasePbufferDCARB error: %s\n", glxWinErrorMessage());
        }
    }
    break;

  case GLX_DRAWABLE_PIXMAP:
    {
      // don't release DC, the memory DC lives as long as the bitmap

      // We must ensure that all GDI drawing into the bitmap has completed
      // in case we subsequently access the bits from it
      GdiFlush();
    }
    break;

  default:
    {
      ErrorF("glxWinReleaseDC: tried to releaseDC for unhandled drawable type %d\n", draw->base.type);
    }
  }
}

static void
glxWinDeferredCreateContext(__GLXWinContext *gc, __GLXWinDrawable *draw)
{
  HDC dc;
  HWND hwnd;
  GLWIN_DEBUG_MSG("glxWinDeferredCreateContext: attach context %p to drawable %p", gc, draw);

  switch (draw->base.type)
  {
    case GLX_DRAWABLE_WINDOW:
    {
      winWindowInfoRec winInfo;
      WindowPtr pWin = (WindowPtr) draw->base.pDraw;

      if (!(gc->base.config->drawableType & GLX_WINDOW_BIT))
        {
          ErrorF("glxWinDeferredCreateContext: tried to attach a context whose fbConfig doesn't have drawableType GLX_WINDOW_BIT to a GLX_DRAWABLE_WINDOW drawable\n");
        }

      if (pWin == NULL)
        {
          GLWIN_DEBUG_MSG("Deferring until X window is created");
          return;
        }

      GLWIN_DEBUG_MSG("glxWinDeferredCreateContext: pWin %p", pWin);

      winGetWindowInfo(pWin, &winInfo);
      if (winInfo.hwnd == NULL)
        {
          GLWIN_DEBUG_MSG("Deferring until native window is created");
          return;
        }
    }
    break;

    case GLX_DRAWABLE_PBUFFER:
    {
      if (draw->hPbuffer == NULL)
        {
          __GLXscreen *screen;
          glxWinScreen *winScreen;
          int pixelFormat;
          // XXX: which DC are supposed to use???
          HDC screenDC = GetDC(NULL);

          if (!(gc->base.config->drawableType & GLX_PBUFFER_BIT))
            {
              ErrorF("glxWinDeferredCreateContext: tried to attach a context whose fbConfig doesn't have drawableType GLX_PBUFFER_BIT to a GLX_DRAWABLE_PBUFFER drawable\n");
            }

          screen = gc->base.pGlxScreen;
          winScreen = (glxWinScreen *)screen;

          pixelFormat = fbConfigToPixelFormatIndex(screenDC, gc->base.config, GLX_DRAWABLE_PBUFFER, winScreen);
          if (pixelFormat == 0)
            {
              ErrorF("wglChoosePixelFormat error: %s\n", glxWinErrorMessage());
              return;
            }

          draw->hPbuffer = wglCreatePbufferARBWrapper(screenDC, pixelFormat, draw->base.pDraw->width, draw->base.pDraw->height, NULL);
          ReleaseDC(NULL, screenDC);

          if (draw->hPbuffer == NULL)
            {
              ErrorF("wglCreatePbufferARBWrapper error: %s\n", glxWinErrorMessage());
              return;
            }

          GLWIN_DEBUG_MSG("glxWinDeferredCreateContext: pBuffer %p created for drawable %p", draw->hPbuffer, draw);
        }
    }
    break;

    case GLX_DRAWABLE_PIXMAP:
    {
      if (draw->dibDC == NULL)
        {
          BITMAPINFOHEADER bmpHeader = { sizeof(bmpHeader),
                                         draw->base.pDraw->width, draw->base.pDraw->height,
                                         1, draw->base.pDraw->bitsPerPixel,
                                         BI_RGB};
          void *pBits;

          if (!(gc->base.config->drawableType & GLX_PIXMAP_BIT))
            {
              ErrorF("glxWinDeferredCreateContext: tried to attach a context whose fbConfig doesn't have drawableType GLX_PIXMAP_BIT to a GLX_DRAWABLE_PIXMAP drawable\n");
            }

          draw->dibDC = CreateCompatibleDC(NULL);
          if (draw->dibDC == NULL)
            {
              ErrorF("CreateCompatibleDC error: %s\n", glxWinErrorMessage());
              return;
            }

          draw->hDIB = CreateDIBSection(draw->dibDC, (BITMAPINFO *)&bmpHeader, DIB_RGB_COLORS, &pBits, 0, 0);
          if (draw->dibDC == NULL)
            {
              ErrorF("CreateDIBSection error: %s\n", glxWinErrorMessage());
              return;
            }

          // XXX: CreateDIBSection insists on allocating the bitmap memory for us, so we're going to
          // need some jiggery pokery to point the underlying X Drawable's bitmap at the same set of bits
          // so that they can be read with XGetImage as well as glReadPixels, assuming the formats are
          // even compatible ...
          draw->pOldBits = ((PixmapPtr)draw->base.pDraw)->devPrivate.ptr;
          ((PixmapPtr)draw->base.pDraw)->devPrivate.ptr = pBits;

          // Select the DIB into the DC
          draw->hOldDIB = SelectObject(draw->dibDC, draw->hDIB);
          if (!draw->hOldDIB)
            {
              ErrorF("SelectObject error: %s\n", glxWinErrorMessage());
            }

          // Set the pixel format of the bitmap
          glxWinSetPixelFormat(gc, draw->dibDC, draw->base.pDraw->bitsPerPixel, GLX_PIXMAP_BIT);

          GLWIN_DEBUG_MSG("glxWinDeferredCreateContext: DIB bitmap %p created for drawable %p", draw->hDIB, draw);
        }
    }
    break;

    default:
    {
      ErrorF("glxWinDeferredCreateContext: tried to attach unhandled drawable type %d\n", draw->base.type);
      return;
    }
  }

  dc = glxWinMakeDC(gc, draw, &dc, &hwnd);
  gc->ctx = wglCreateContext(dc);
  glxWinReleaseDC(hwnd, dc, draw);

  if (gc->ctx == NULL)
    {
      ErrorF("wglCreateContext error: %s\n", glxWinErrorMessage());
      return;
    }

  GLWIN_DEBUG_MSG("glxWinDeferredCreateContext: attached context %p to native context %p drawable %p", gc, gc->ctx, draw);

  // if the native context was created successfully, shareLists if needed
  if (gc->ctx && gc->shareContext)
    {
      GLWIN_DEBUG_MSG("glxWinCreateContextReal shareLists with context %p (native ctx %p)", gc->shareContext, gc->shareContext->ctx);

      if (!wglShareLists(gc->shareContext->ctx, gc->ctx))
        {
          ErrorF("wglShareLists error: %s\n", glxWinErrorMessage());
        }
    }
}

/* ---------------------------------------------------------------------- */
/*
 * Context functions
 */


/* Context manipulation routines should return TRUE on success, FALSE on failure */
static int
glxWinContextMakeCurrent(__GLXcontext *base)
{
  __GLXWinContext *gc = (__GLXWinContext *)base;
  BOOL ret;
  HDC drawDC;
  HDC readDC = NULL;
  __GLXdrawable *drawPriv;
  __GLXdrawable *readPriv;
  HWND hwnd;

  GLWIN_TRACE_MSG("glxWinContextMakeCurrent context %p (native ctx %p)", gc, gc->ctx);
  glWinCallDelta();

  /* Keep a note of the last active context in the drawable */
  drawPriv = gc->base.drawPriv;
  ((__GLXWinDrawable *)drawPriv)->drawContext = gc;

  if (gc->ctx == NULL)
    {
      glxWinDeferredCreateContext(gc, (__GLXWinDrawable *)drawPriv);
    }

  if (gc->ctx == NULL)
    {
      ErrorF("glxWinContextMakeCurrent: Native context is NULL\n");
      return FALSE;
    }

  drawDC = glxWinMakeDC(gc, (__GLXWinDrawable *)drawPriv, &drawDC, &hwnd);
  if (drawDC == NULL)
    {
      ErrorF("glxWinMakeDC failed for drawDC\n");
      return FALSE;
    }

  if ((gc->base.readPriv != NULL) && (gc->base.readPriv != gc->base.drawPriv))
    {
      // XXX: should only occur with WGL_ARB_make_current_read
      /*
        If there is a separate read drawable, create a separate read DC, and
        use the wglMakeContextCurrent extension to make the context current drawing
        to one DC and reading from the other
      */
      readPriv = gc->base.readPriv;
      readDC = glxWinMakeDC(gc, (__GLXWinDrawable *)readPriv, &readDC, &hwnd);
      if (readDC == NULL)
        {
          ErrorF("glxWinMakeDC failed for readDC\n");
          glxWinReleaseDC(hwnd, drawDC, (__GLXWinDrawable *)readPriv);
          return FALSE;
        }

      ret = wglMakeContextCurrentARBWrapper(drawDC, readDC, gc->ctx);
      if (!ret)
        {
          ErrorF("wglMakeContextCurrent error: %s\n", glxWinErrorMessage());
        }
    }
  else
    {
      /* Otherwise, just use wglMakeCurrent */
      ret = wglMakeCurrent(drawDC, gc->ctx);
      if (!ret)
        {
          ErrorF("wglMakeCurrent error: %s\n", glxWinErrorMessage());
        }
    }

  // apparently make current could fail if the context is current in a different thread,
  // but that shouldn't be able to happen in the current server...

  glxWinReleaseDC(hwnd, drawDC, (__GLXWinDrawable *)drawPriv);
  if (readDC)
    glxWinReleaseDC(hwnd, drawDC, (__GLXWinDrawable *)readPriv);

  return ret;
}

static int
glxWinContextLoseCurrent(__GLXcontext *base)
{
  BOOL ret;
  __GLXWinContext *gc = (__GLXWinContext *)base;

  GLWIN_TRACE_MSG("glxWinContextLoseCurrent context %p (native ctx %p)", gc, gc->ctx);
  glWinCallDelta();

  ret = wglMakeCurrent(NULL, NULL); /* We don't need a DC when setting no current context */
  if (!ret)
    ErrorF("glxWinContextLoseCurrent error: %s\n", glxWinErrorMessage());

  __glXLastContext = NULL; /* Mesa does this; why? */
  // __glXFlushContextCache()

  return TRUE;
}

static int
glxWinContextCopy(__GLXcontext *dst_base, __GLXcontext *src_base, unsigned long mask)
{
  __GLXWinContext *dst = (__GLXWinContext *)dst_base;
  __GLXWinContext *src = (__GLXWinContext *)src_base;
  BOOL ret;

  GLWIN_DEBUG_MSG("glxWinContextCopy");

  ret = wglCopyContext(src->ctx, dst->ctx, mask);
  if (!ret)
    {
      ErrorF("wglCopyContext error: %s\n", glxWinErrorMessage());
    }

  return ret;
}

static int
glxWinContextForceCurrent(__GLXcontext *base)
{
  /* wglMakeCurrent always flushes the previous context, so this is equivalent to glxWinContextMakeCurrent */
  return glxWinContextMakeCurrent(base);
}

static void
glxWinContextDestroy(__GLXcontext *base)
{
  __GLXWinContext *gc = (__GLXWinContext *)base;

  if (gc != NULL)
    {
      GLWIN_DEBUG_MSG("GLXcontext %p destroyed (native ctx %p)", base, gc->ctx);

      if (gc->ctx)
        {
          BOOL ret;
          /* It's bad style to delete the context while it's still current */
          if (wglGetCurrentContext() == gc->ctx)
            {
              wglMakeCurrent(NULL, NULL);
            }

          ret = wglDeleteContext(gc->ctx);
          if (!ret)
            ErrorF("wglDeleteContext error: %s\n", glxWinErrorMessage());
          gc->ctx = NULL;
        }

      xfree(gc);
    }
}

static __GLXcontext *
glxWinCreateContext(__GLXscreen *screen,
                   __GLXconfig *modes,
                   __GLXcontext *baseShareContext)
{
    __GLXWinContext *context;
    __GLXWinContext *shareContext = (__GLXWinContext *)baseShareContext;

    static __GLXtextureFromPixmap glxWinTextureFromPixmap =
      {
        glxWinBindTexImage,
        glxWinReleaseTexImage
      };

    context = (__GLXWinContext *)xcalloc(1, sizeof(__GLXWinContext));

    if (!context)
        return NULL;

    memset(context, 0, sizeof *context);
    context->base.destroy        = glxWinContextDestroy;
    context->base.makeCurrent    = glxWinContextMakeCurrent;
    context->base.loseCurrent    = glxWinContextLoseCurrent;
    context->base.copy           = glxWinContextCopy;
    context->base.forceCurrent   = glxWinContextForceCurrent;
    context->base.textureFromPixmap = &glxWinTextureFromPixmap;
    context->base.config = modes;
    context->base.pGlxScreen = screen;

    // actual native GL context creation is deferred until attach()
    context->ctx = NULL;
    context->shareContext = shareContext;

    glWinSetupDispatchTable();

    GLWIN_DEBUG_MSG("GLXcontext %p created", context);

    return &(context->base);
}

/* ---------------------------------------------------------------------- */
/*
 * Utility functions
 */

static int
fbConfigToPixelFormat(__GLXconfig *mode, PIXELFORMATDESCRIPTOR *pfdret, int drawableTypeOverride)
{
    PIXELFORMATDESCRIPTOR pfd = {
      sizeof(PIXELFORMATDESCRIPTOR),   /* size of this pfd */
      1,                     /* version number */
      PFD_SUPPORT_OPENGL,    /* support OpenGL */
      PFD_TYPE_RGBA,         /* RGBA type */
      24,                    /* 24-bit color depth */
      0, 0, 0, 0, 0, 0,      /* color bits ignored */
      0,                     /* no alpha buffer */
      0,                     /* shift bit ignored */
      0,                     /* no accumulation buffer */
      0, 0, 0, 0,            /* accum bits ignored */
      32,                    /* 32-bit z-buffer */
      0,                     /* no stencil buffer */
      0,                     /* no auxiliary buffer */
      PFD_MAIN_PLANE,        /* main layer */
      0,                     /* reserved */
      0, 0, 0                /* layer masks ignored */
    };

    if ((mode->drawableType | drawableTypeOverride) & GLX_WINDOW_BIT)
      pfd.dwFlags |= PFD_DRAW_TO_WINDOW; /* support window */

    if ((mode->drawableType | drawableTypeOverride) & GLX_PIXMAP_BIT)
      pfd.dwFlags |= (PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI); /* supports software rendering to bitmap */

    if (mode->stereoMode) {
        pfd.dwFlags |= PFD_STEREO;
    }
    if (mode->doubleBufferMode) {
        pfd.dwFlags |= PFD_DOUBLEBUFFER;
    }

    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = mode->redBits + mode->greenBits + mode->blueBits;
    pfd.cRedBits = mode->redBits;
    pfd.cRedShift = 0; /* FIXME */
    pfd.cGreenBits = mode->greenBits;
    pfd.cGreenShift = 0; /* FIXME  */
    pfd.cBlueBits = mode->blueBits;
    pfd.cBlueShift = 0; /* FIXME */
    pfd.cAlphaBits = mode->alphaBits;
    pfd.cAlphaShift = 0; /* FIXME */

    pfd.cAccumBits = mode->accumRedBits + mode->accumGreenBits + mode->accumBlueBits + mode->accumAlphaBits;
    pfd.cAccumRedBits = mode->accumRedBits;
    pfd.cAccumGreenBits = mode->accumGreenBits;
    pfd.cAccumBlueBits = mode->accumBlueBits;
    pfd.cAccumAlphaBits = mode->accumAlphaBits;

    pfd.cDepthBits = mode->depthBits;
    pfd.cStencilBits = mode->stencilBits;
    pfd.cAuxBuffers = mode->numAuxBuffers;

    /* mode->level ? */
    /* mode->pixmapMode ? */

    *pfdret = pfd;

    return 0;
}

#define SET_ATTR_VALUE(attr, value) { attribList[i++] = attr; attribList[i++] = value; assert(i < NUM_ELEMENTS(attribList)); }

static int
fbConfigToPixelFormatIndex(HDC hdc, __GLXconfig *mode, int drawableTypeOverride, glxWinScreen *winScreen)
{
  UINT numFormats;
  unsigned int i = 0;

  /* convert fbConfig to attr-value list  */
  int attribList[60];

  SET_ATTR_VALUE(WGL_SUPPORT_OPENGL_ARB, TRUE);
  SET_ATTR_VALUE(WGL_PIXEL_TYPE_ARB, (mode->visualType == GLX_TRUE_COLOR) ? WGL_TYPE_RGBA_ARB : WGL_TYPE_COLORINDEX_ARB);
  SET_ATTR_VALUE(WGL_COLOR_BITS_ARB, (mode->visualType == GLX_TRUE_COLOR) ? mode->rgbBits : mode->indexBits);
  SET_ATTR_VALUE(WGL_RED_BITS_ARB, mode->redBits);
  SET_ATTR_VALUE(WGL_GREEN_BITS_ARB, mode->greenBits);
  SET_ATTR_VALUE(WGL_BLUE_BITS_ARB, mode->blueBits);
  SET_ATTR_VALUE(WGL_ALPHA_BITS_ARB, mode->alphaBits);
  SET_ATTR_VALUE(WGL_ACCUM_RED_BITS_ARB, mode->accumRedBits);
  SET_ATTR_VALUE(WGL_ACCUM_GREEN_BITS_ARB, mode->accumGreenBits);
  SET_ATTR_VALUE(WGL_ACCUM_BLUE_BITS_ARB, mode->accumBlueBits);
  SET_ATTR_VALUE(WGL_ACCUM_ALPHA_BITS_ARB, mode->accumAlphaBits);
  SET_ATTR_VALUE(WGL_DEPTH_BITS_ARB, mode->depthBits);
  SET_ATTR_VALUE(WGL_STENCIL_BITS_ARB, mode->stencilBits);
  SET_ATTR_VALUE(WGL_AUX_BUFFERS_ARB, mode->numAuxBuffers);

  if (mode->doubleBufferMode)
    SET_ATTR_VALUE(WGL_DOUBLE_BUFFER_ARB, TRUE);

  if (mode->stereoMode)
    SET_ATTR_VALUE(WGL_STEREO_ARB, TRUE);

  // Some attributes are only added to the list if the value requested is not 'don't care', as exactly matching that is daft..
  if (mode->swapMethod == GLX_SWAP_EXCHANGE_OML)
    SET_ATTR_VALUE(WGL_SWAP_METHOD_ARB, WGL_SWAP_EXCHANGE_ARB);

  if (mode->swapMethod == GLX_SWAP_COPY_OML)
    SET_ATTR_VALUE(WGL_SWAP_COPY_ARB, TRUE);

  // XXX: this should probably be the other way around, but that messes up drawableTypeOverride
  if (mode->visualRating == GLX_SLOW_VISUAL_EXT)
    SET_ATTR_VALUE(WGL_ACCELERATION_ARB, WGL_NO_ACCELERATION_ARB);

  // must support all the drawable types the mode supports
  if ((mode->drawableType | drawableTypeOverride) & GLX_WINDOW_BIT)
    SET_ATTR_VALUE(WGL_DRAW_TO_WINDOW_ARB,TRUE);

  // XXX: this is a horrible hacky heuristic, in fact this whole drawableTypeOverride thing is a bad idea
  // try to avoid asking for formats which don't exist (by not asking for all when adjusting the config to include the drawableTypeOverride)
  if (drawableTypeOverride == GLX_WINDOW_BIT)
    {
      if (mode->drawableType & GLX_PIXMAP_BIT)
        SET_ATTR_VALUE(WGL_DRAW_TO_BITMAP_ARB, TRUE);

      if (mode->drawableType & GLX_PBUFFER_BIT)
        if (winScreen->has_WGL_ARB_pbuffer)
          SET_ATTR_VALUE(WGL_DRAW_TO_PBUFFER_ARB, TRUE);
    }
  else
    {
      if (drawableTypeOverride & GLX_PIXMAP_BIT)
        SET_ATTR_VALUE(WGL_DRAW_TO_BITMAP_ARB, TRUE);

      if (drawableTypeOverride & GLX_PBUFFER_BIT)
        if (winScreen->has_WGL_ARB_pbuffer)
          SET_ATTR_VALUE(WGL_DRAW_TO_PBUFFER_ARB, TRUE);
    }

  SET_ATTR_VALUE(0, 0); // terminator

  /* choose the first match */
  {
    int pixelFormatIndex;

    if (!wglChoosePixelFormatARBWrapper(hdc, attribList, NULL, 1, &pixelFormatIndex, &numFormats))
      {
        ErrorF("wglChoosePixelFormat error: %s\n", glxWinErrorMessage());
      }
    else
      {
        if (numFormats > 0)
          {
            GLWIN_DEBUG_MSG("wglChoosePixelFormat: chose pixelFormatIndex %d)", pixelFormatIndex);
            return pixelFormatIndex;
          }
        else
          ErrorF("wglChoosePixelFormat couldn't decide\n");
      }
  }

  return 0;
}

/* ---------------------------------------------------------------------- */

#define BITS_AND_SHIFT_TO_MASK(bits,mask) (((1<<(bits))-1) << (mask))

//
// Create the GLXconfigs using DescribePixelFormat()
//
static void
glxWinCreateConfigs(HDC hdc, glxWinScreen *screen)
{
  GLXWinConfig *c, *result, *prev = NULL;
  int numConfigs = 0;
  int i = 0;
  int n = 0;
  PIXELFORMATDESCRIPTOR pfd;

  GLWIN_DEBUG_MSG("glxWinCreateConfigs");

  screen->base.numFBConfigs = 0;
  screen->base.fbconfigs = NULL;

  // get the number of pixelformats
  numConfigs = DescribePixelFormat(hdc, 1, sizeof(PIXELFORMATDESCRIPTOR), NULL);
  GLWIN_DEBUG_MSG("DescribePixelFormat says %d possible pixel formats", numConfigs);

  /* alloc */
  result = xalloc(sizeof(GLXWinConfig) * numConfigs);

  if (NULL == result)
    {
      return;
    }

  memset(result, 0, sizeof(GLXWinConfig) * numConfigs);
  n = 0;

  /* fill in configs */
  for (i = 0;  i < numConfigs; i++)
    {
      int rc;

      c = &(result[i]);
      c->base.next = NULL;
      c->pixelFormatIndex = i+1;

      rc = DescribePixelFormat(hdc, i+1, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

      if (!rc)
        {
          ErrorF("DescribePixelFormat failed for index %d, error %s\n", i+1, glxWinErrorMessage());
          break;
        }

      if (glxWinDebugSettings.dumpPFD)
        pfdOut(&pfd);

      if (!(pfd.dwFlags & (PFD_DRAW_TO_WINDOW | PFD_DRAW_TO_BITMAP)) || !(pfd.dwFlags & PFD_SUPPORT_OPENGL))
        {
          GLWIN_DEBUG_MSG("pixelFormat %d has unsuitable flags 0x%08lx, skipping", i+1, pfd.dwFlags);
          continue;
        }

      c->base.doubleBufferMode = (pfd.dwFlags & PFD_DOUBLEBUFFER) ? GL_TRUE : GL_FALSE;
      c->base.stereoMode = (pfd.dwFlags & PFD_STEREO) ? GL_TRUE : GL_FALSE;

      c->base.redBits = pfd.cRedBits;
      c->base.greenBits = pfd.cGreenBits;
      c->base.blueBits = pfd.cBlueBits;
      c->base.alphaBits = pfd.cAlphaBits;

      c->base.redMask = BITS_AND_SHIFT_TO_MASK(pfd.cRedBits, pfd.cRedShift);
      c->base.greenMask =  BITS_AND_SHIFT_TO_MASK(pfd.cGreenBits, pfd.cGreenShift);
      c->base.blueMask = BITS_AND_SHIFT_TO_MASK(pfd.cBlueBits, pfd.cBlueShift);
      c->base.alphaMask = BITS_AND_SHIFT_TO_MASK(pfd.cAlphaBits, pfd.cAlphaShift);

      c->base.rgbBits = pfd.cColorBits;

      if (pfd.iPixelType == PFD_TYPE_COLORINDEX)
        {
          c->base.indexBits = pfd.cColorBits;
        }
      else
        {
          c->base.indexBits = 0;
        }

      c->base.accumRedBits = pfd.cAccumRedBits;
      c->base.accumGreenBits = pfd.cAccumGreenBits;
      c->base.accumBlueBits = pfd.cAccumBlueBits;
      c->base.accumAlphaBits = pfd.cAccumAlphaBits;
      //  pfd.cAccumBits;

      c->base.depthBits = pfd.cDepthBits;
      c->base.stencilBits = pfd.cStencilBits;
      c->base.numAuxBuffers = pfd.cAuxBuffers;

      // pfd.iLayerType; // ignored
      c->base.level = 0;
      // pfd.dwLayerMask; // ignored
      // pfd.dwDamageMask;  // ignored

      c->base.pixmapMode = 0;
      c->base.visualID = -1;  // will be set by __glXScreenInit()

      /* EXT_visual_rating / GLX 1.2 */
      if (pfd.dwFlags & PFD_GENERIC_FORMAT)
        {
          c->base.visualRating = GLX_SLOW_VISUAL_EXT;
        }
      else
        {
          // PFD_GENERIC_ACCELERATED is not considered, so this may be MCD or ICD acclerated...
          c->base.visualRating = GLX_NONE_EXT;
        }

      /* EXT_visual_info / GLX 1.2 */
      if (pfd.iPixelType == PFD_TYPE_COLORINDEX)
        {
          c->base.visualType = GLX_STATIC_COLOR;
        }
      else
        {
          c->base.visualType = GLX_TRUE_COLOR;
        }

      // pfd.dwVisibleMask; ???
      c->base.transparentPixel = GLX_NONE;
      c->base.transparentRed = GLX_NONE;
      c->base.transparentGreen = GLX_NONE;
      c->base.transparentBlue = GLX_NONE;
      c->base.transparentAlpha = GLX_NONE;
      c->base.transparentIndex = GLX_NONE;

      /* ARB_multisample / SGIS_multisample */
      c->base.sampleBuffers = 0;
      c->base.samples = 0;

      /* SGIX_fbconfig / GLX 1.3 */
      c->base.drawableType = (((pfd.dwFlags & PFD_DRAW_TO_WINDOW) ? GLX_WINDOW_BIT : 0)
                         | ((pfd.dwFlags & PFD_DRAW_TO_BITMAP) ? GLX_PIXMAP_BIT : 0));
      c->base.renderType = GLX_RGBA_BIT;
      c->base.xRenderable = GL_TRUE;
      c->base.fbconfigID = -1; // will be set by __glXScreenInit()

      /* SGIX_pbuffer / GLX 1.3 */
      // XXX: How can we find these values out ???
      c->base.maxPbufferWidth = -1;
      c->base.maxPbufferHeight = -1;
      c->base.maxPbufferPixels = -1;
      c->base.optimalPbufferWidth = 0; // there is no optimal value
      c->base.optimalPbufferHeight = 0;

      /* SGIX_visual_select_group */
      // arrange for visuals with the best acceleration to be preferred in selection
      switch (pfd.dwFlags & (PFD_GENERIC_FORMAT | PFD_GENERIC_ACCELERATED))
        {
        case 0:
          c->base.visualSelectGroup = 2;
          break;

        case PFD_GENERIC_ACCELERATED:
          c->base.visualSelectGroup = 1;
          break;

        case PFD_GENERIC_FORMAT:
          c->base.visualSelectGroup = 0;
          break;

        default:
          ;
          // "can't happen"
        }

      /* OML_swap_method */
      if (pfd.dwFlags & PFD_SWAP_EXCHANGE)
        c->base.swapMethod = GLX_SWAP_EXCHANGE_OML;
      else if (pfd.dwFlags & PFD_SWAP_COPY)
        c->base.swapMethod = GLX_SWAP_COPY_OML;
      else
        c->base.swapMethod = GLX_SWAP_UNDEFINED_OML;

      /* EXT_import_context */
      c->base.screen = screen->base.pScreen->myNum;

      /* EXT_texture_from_pixmap */
      c->base.bindToTextureRgb = -1;
      c->base.bindToTextureRgba = -1;
      c->base.bindToMipmapTexture = -1;
      c->base.bindToTextureTargets = -1;
      c->base.yInverted = -1;

      n++;

      // update previous config to point to this config
      if (prev)
        prev->base.next = &(c->base);

      prev = c;
    }

  GLWIN_DEBUG_MSG("found %d pixelFormats suitable for conversion to fbConfigs", n);

  screen->base.numFBConfigs = n;
  screen->base.fbconfigs = &(result->base);
}

// helper function to access an attribute value from an attribute value array by attribute
static
int getAttrValue(const int attrs[], int values[], unsigned int num, int attr, int fallback)
{
  unsigned int i;
  for (i = 0; i < num; i++)
    {
      if (attrs[i] == attr)
        {
          GLWIN_TRACE_MSG("getAttrValue attr 0x%x, value %d", attr, values[i]);
          return values[i];
        }
    }

  ErrorF("getAttrValue failed to find attr 0x%x, using default value %d\n", attr, fallback);
  return fallback;
}

//
// Create the GLXconfigs using wglGetPixelFormatAttribfvARB() extension
//
static void
glxWinCreateConfigsExt(HDC hdc, glxWinScreen *screen)
{
  GLXWinConfig *c, *result, *prev = NULL;
  int i = 0;
  int n = 0;

  const int attr = WGL_NUMBER_PIXEL_FORMATS_ARB;
  int numConfigs;

  int attrs[50];
  unsigned int num_attrs = 0;

  GLWIN_DEBUG_MSG("glxWinCreateConfigsExt");

  screen->base.numFBConfigs = 0;
  screen->base.fbconfigs = NULL;

  if (!wglGetPixelFormatAttribivARBWrapper(hdc, 0, 0, 1, &attr, &numConfigs))
    {
      ErrorF("wglGetPixelFormatAttribivARB failed for WGL_NUMBER_PIXEL_FORMATS_ARB: %s\n", glxWinErrorMessage());
      return;
    }

  GLWIN_DEBUG_MSG("wglGetPixelFormatAttribivARB says %d possible pixel formats", numConfigs);

  /* alloc */
  result = xalloc(sizeof(GLXWinConfig) * numConfigs);

  if (NULL == result)
    {
      return;
    }

  memset(result, 0, sizeof(GLXWinConfig) * numConfigs);
  n = 0;

#define ADD_ATTR(a) { attrs[num_attrs++] = a; assert(num_attrs < NUM_ELEMENTS(attrs)); }

  ADD_ATTR(WGL_DRAW_TO_WINDOW_ARB);
  ADD_ATTR(WGL_DRAW_TO_BITMAP_ARB);
  ADD_ATTR(WGL_ACCELERATION_ARB);
  ADD_ATTR(WGL_SWAP_LAYER_BUFFERS_ARB);
  ADD_ATTR(WGL_NUMBER_OVERLAYS_ARB);
  ADD_ATTR(WGL_NUMBER_UNDERLAYS_ARB);
  ADD_ATTR(WGL_TRANSPARENT_ARB);
  ADD_ATTR(WGL_TRANSPARENT_RED_VALUE_ARB);
  ADD_ATTR(WGL_TRANSPARENT_GREEN_VALUE_ARB);
  ADD_ATTR(WGL_TRANSPARENT_GREEN_VALUE_ARB);
  ADD_ATTR(WGL_TRANSPARENT_ALPHA_VALUE_ARB);
  ADD_ATTR(WGL_SUPPORT_OPENGL_ARB);
  ADD_ATTR(WGL_DOUBLE_BUFFER_ARB);
  ADD_ATTR(WGL_STEREO_ARB);
  ADD_ATTR(WGL_PIXEL_TYPE_ARB);
  ADD_ATTR(WGL_COLOR_BITS_ARB);
  ADD_ATTR(WGL_RED_BITS_ARB);
  ADD_ATTR(WGL_RED_SHIFT_ARB);
  ADD_ATTR(WGL_GREEN_BITS_ARB);
  ADD_ATTR(WGL_GREEN_SHIFT_ARB);
  ADD_ATTR(WGL_BLUE_BITS_ARB);
  ADD_ATTR(WGL_BLUE_SHIFT_ARB);
  ADD_ATTR(WGL_ALPHA_BITS_ARB);
  ADD_ATTR(WGL_ALPHA_SHIFT_ARB);
  ADD_ATTR(WGL_ACCUM_RED_BITS_ARB);
  ADD_ATTR(WGL_ACCUM_GREEN_BITS_ARB);
  ADD_ATTR(WGL_ACCUM_BLUE_BITS_ARB);
  ADD_ATTR(WGL_ACCUM_ALPHA_BITS_ARB);
  ADD_ATTR(WGL_DEPTH_BITS_ARB);
  ADD_ATTR(WGL_STENCIL_BITS_ARB);
  ADD_ATTR(WGL_AUX_BUFFERS_ARB);
  ADD_ATTR(WGL_SWAP_METHOD_ARB);

  if (screen->has_WGL_ARB_multisample)
    {
      // we may not query these attrs if WGL_ARB_multisample is not offered
      ADD_ATTR(WGL_SAMPLE_BUFFERS_ARB);
      ADD_ATTR(WGL_SAMPLES_ARB);
    }

  if (screen->has_WGL_ARB_render_texture)
    {
      // we may not query these attrs if WGL_ARB_render_texture is not offered
      ADD_ATTR(WGL_BIND_TO_TEXTURE_RGB_ARB);
      ADD_ATTR(WGL_BIND_TO_TEXTURE_RGBA_ARB);
    }

  if (screen->has_WGL_ARB_pbuffer)
    {
      // we may not query these attrs if WGL_ARB_pbuffer is not offered
      ADD_ATTR(WGL_DRAW_TO_PBUFFER_ARB);
      ADD_ATTR(WGL_MAX_PBUFFER_PIXELS_ARB);
      ADD_ATTR(WGL_MAX_PBUFFER_WIDTH_ARB);
      ADD_ATTR(WGL_MAX_PBUFFER_HEIGHT_ARB);
    }

  /* fill in configs */
  for (i = 0;  i < numConfigs; i++)
    {
      int sizevalues=num_attrs*sizeof(int);
      int *values=(int*)_alloca(sizevalues);

      memset(values,0,sizevalues);

      c = &(result[i]);
      c->base.next = NULL;
      c->pixelFormatIndex = i+1;

      if (!wglGetPixelFormatAttribivARBWrapper(hdc, i+1, 0, num_attrs, attrs, values))
        {
          ErrorF("wglGetPixelFormatAttribivARB failed for index %d, error %s\n", i+1, glxWinErrorMessage());
          break;
        }

#define ATTR_VALUE(a, d) getAttrValue(attrs, values, num_attrs, (a), (d))

      if (!ATTR_VALUE(WGL_SUPPORT_OPENGL_ARB, 0))
        {
          GLWIN_DEBUG_MSG("pixelFormat %d isn't WGL_SUPPORT_OPENGL_ARB, skipping", i+1);
          continue;
        }

      c->base.doubleBufferMode = ATTR_VALUE(WGL_DOUBLE_BUFFER_ARB, 0) ? GL_TRUE : GL_FALSE;
      c->base.stereoMode = ATTR_VALUE(WGL_STEREO_ARB, 0) ? GL_TRUE : GL_FALSE;

      c->base.redBits = ATTR_VALUE(WGL_RED_BITS_ARB, 0);
      c->base.greenBits = ATTR_VALUE(WGL_GREEN_BITS_ARB, 0);
      c->base.blueBits = ATTR_VALUE(WGL_BLUE_BITS_ARB, 0);
      c->base.alphaBits = ATTR_VALUE(WGL_ALPHA_BITS_ARB, 0);

      c->base.redMask = BITS_AND_SHIFT_TO_MASK(c->base.redBits, ATTR_VALUE(WGL_RED_SHIFT_ARB, 0));
      c->base.greenMask = BITS_AND_SHIFT_TO_MASK(c->base.greenBits, ATTR_VALUE(WGL_GREEN_SHIFT_ARB, 0));
      c->base.blueMask = BITS_AND_SHIFT_TO_MASK(c->base.blueBits, ATTR_VALUE(WGL_BLUE_SHIFT_ARB, 0));
      c->base.alphaMask = BITS_AND_SHIFT_TO_MASK(c->base.alphaBits, ATTR_VALUE(WGL_ALPHA_SHIFT_ARB, 0));

      switch (ATTR_VALUE(WGL_PIXEL_TYPE_ARB, 0))
        {
        case WGL_TYPE_COLORINDEX_ARB:
          c->base.indexBits = ATTR_VALUE(WGL_COLOR_BITS_ARB, 0);
          c->base.rgbBits = 0;
          c->base.visualType = GLX_STATIC_COLOR;
          break;

        case WGL_TYPE_RGBA_FLOAT_ARB:
          GLWIN_DEBUG_MSG("pixelFormat %d is WGL_TYPE_RGBA_FLOAT_ARB, skipping", i+1);
          continue;
          
        case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:
          GLWIN_DEBUG_MSG("pixelFormat %d is WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT, skipping", i+1);
          continue;

        case WGL_TYPE_RGBA_ARB:
          c->base.indexBits = 0;
          c->base.rgbBits = ATTR_VALUE(WGL_COLOR_BITS_ARB, 0);
          c->base.visualType = GLX_TRUE_COLOR;
          break;

        default:
          ErrorF("wglGetPixelFormatAttribivARB returned unknown value 0x%x for WGL_PIXEL_TYPE_ARB\n", ATTR_VALUE(WGL_PIXEL_TYPE_ARB, 0));
          continue;
        }

      c->base.accumRedBits = ATTR_VALUE(WGL_ACCUM_RED_BITS_ARB, 0);
      c->base.accumGreenBits = ATTR_VALUE(WGL_ACCUM_GREEN_BITS_ARB, 0);
      c->base.accumBlueBits = ATTR_VALUE(WGL_ACCUM_BLUE_BITS_ARB, 0);
      c->base.accumAlphaBits = ATTR_VALUE(WGL_ACCUM_ALPHA_BITS_ARB, 0);

      c->base.depthBits = ATTR_VALUE(WGL_DEPTH_BITS_ARB, 0);
      c->base.stencilBits = ATTR_VALUE(WGL_STENCIL_BITS_ARB, 0);
      c->base.numAuxBuffers = ATTR_VALUE(WGL_AUX_BUFFERS_ARB, 0);

      {
        int layers = ATTR_VALUE(WGL_NUMBER_OVERLAYS_ARB,0) + ATTR_VALUE(WGL_NUMBER_UNDERLAYS_ARB, 0);

        if (layers > 0)
          {
            ErrorF("pixelFormat %d: has %d overlay, %d underlays which aren't currently handled", i, ATTR_VALUE(WGL_NUMBER_OVERLAYS_ARB,0), ATTR_VALUE(WGL_NUMBER_UNDERLAYS_ARB, 0));
            // XXX: need to iterate over layers?
          }
      }
      c->base.level = 0;

      c->base.pixmapMode = 0; // ???
      c->base.visualID = -1;  // will be set by __glXScreenInit()

      /* EXT_visual_rating / GLX 1.2 */
      switch (ATTR_VALUE(WGL_ACCELERATION_ARB, 0))
        {
        default:
          ErrorF("wglGetPixelFormatAttribivARB returned unknown value 0x%x for WGL_ACCELERATION_ARB\n", ATTR_VALUE(WGL_ACCELERATION_ARB, 0));

        case WGL_NO_ACCELERATION_ARB:
          c->base.visualRating = GLX_SLOW_VISUAL_EXT;
          break;

        case WGL_GENERIC_ACCELERATION_ARB:
        case WGL_FULL_ACCELERATION_ARB:
          c->base.visualRating = GLX_NONE_EXT;
          break;
        }

      /* EXT_visual_info / GLX 1.2 */
      // c->base.visualType is set above
      if (ATTR_VALUE(WGL_TRANSPARENT_ARB, 0))
        {
          c->base.transparentPixel = (c->base.visualType == GLX_TRUE_COLOR) ? GLX_TRANSPARENT_RGB_EXT : GLX_TRANSPARENT_INDEX_EXT;
          c->base.transparentRed = ATTR_VALUE(WGL_TRANSPARENT_RED_VALUE_ARB, 0);
          c->base.transparentGreen = ATTR_VALUE(WGL_TRANSPARENT_GREEN_VALUE_ARB, 0);
          c->base.transparentBlue = ATTR_VALUE(WGL_TRANSPARENT_BLUE_VALUE_ARB, 0);
          c->base.transparentAlpha = ATTR_VALUE(WGL_TRANSPARENT_ALPHA_VALUE_ARB, 0);
          c->base.transparentIndex = ATTR_VALUE(WGL_TRANSPARENT_INDEX_VALUE_ARB, 0);
        }
      else
        {
          c->base.transparentPixel = GLX_NONE_EXT;
          c->base.transparentRed = GLX_NONE;
          c->base.transparentGreen = GLX_NONE;
          c->base.transparentBlue = GLX_NONE;
          c->base.transparentAlpha = GLX_NONE;
          c->base.transparentIndex = GLX_NONE;
        }

      /* ARB_multisample / SGIS_multisample */
      if (screen->has_WGL_ARB_multisample)
        {
          c->base.sampleBuffers = ATTR_VALUE(WGL_SAMPLE_BUFFERS_ARB, 0);
          c->base.samples = ATTR_VALUE(WGL_SAMPLES_ARB, 0);
        }
      else
        {
          c->base.sampleBuffers = 0;
          c->base.samples = 0;
        }

      /* SGIX_fbconfig / GLX 1.3 */
      c->base.drawableType = ((ATTR_VALUE(WGL_DRAW_TO_WINDOW_ARB, 0) ? GLX_WINDOW_BIT : 0)
                         | (ATTR_VALUE(WGL_DRAW_TO_BITMAP_ARB, 0) ? GLX_PIXMAP_BIT : 0)
                         | (ATTR_VALUE(WGL_DRAW_TO_PBUFFER_ARB, 0) ? GLX_PBUFFER_BIT : 0));
      c->base.renderType = GLX_RGBA_BIT | GLX_COLOR_INDEX_BIT; // Hmmm ???
      c->base.xRenderable = GL_TRUE;
      c->base.fbconfigID = -1; // will be set by __glXScreenInit()

      /* SGIX_pbuffer / GLX 1.3 */
      if (screen->has_WGL_ARB_pbuffer)
        {
          c->base.maxPbufferWidth = ATTR_VALUE(WGL_MAX_PBUFFER_WIDTH_ARB, -1);
          c->base.maxPbufferHeight = ATTR_VALUE(WGL_MAX_PBUFFER_HEIGHT_ARB, -1);
          c->base.maxPbufferPixels =  ATTR_VALUE(WGL_MAX_PBUFFER_PIXELS_ARB, -1);
        }
      else
        {
          c->base.maxPbufferWidth = -1;
          c->base.maxPbufferHeight = -1;
          c->base.maxPbufferPixels =  -1;
        }
      c->base.optimalPbufferWidth = 0; // there is no optimal value
      c->base.optimalPbufferHeight = 0;

      /* SGIX_visual_select_group */
      // arrange for visuals with the best acceleration to be preferred in selection
      switch (ATTR_VALUE(WGL_ACCELERATION_ARB, 0))
        {
        case WGL_FULL_ACCELERATION_ARB:
          c->base.visualSelectGroup = 2;
          break;

        case WGL_GENERIC_ACCELERATION_ARB:
          c->base.visualSelectGroup = 1;
          break;

        default:
        case WGL_NO_ACCELERATION_ARB:
          c->base.visualSelectGroup = 0;
          break;
        }

      /* OML_swap_method */
      switch (ATTR_VALUE(WGL_SWAP_METHOD_ARB, 0))
        {
        case WGL_SWAP_EXCHANGE_ARB:
          c->base.swapMethod = GLX_SWAP_EXCHANGE_OML;
          break;

        case WGL_SWAP_COPY_ARB:
          c->base.swapMethod = GLX_SWAP_COPY_OML;
          break;

        default:
          ErrorF("wglGetPixelFormatAttribivARB returned unknown value 0x%x for WGL_SWAP_METHOD_ARB\n", ATTR_VALUE(WGL_SWAP_METHOD_ARB, 0));

        case WGL_SWAP_UNDEFINED_ARB:
          c->base.swapMethod = GLX_SWAP_UNDEFINED_OML;
        }

      /* EXT_import_context */
      c->base.screen = screen->base.pScreen->myNum;

      /* EXT_texture_from_pixmap */
      /*
         Mesa's DRI configs always have bindToTextureRgb/Rgba TRUE (see driCreateConfigs(), so setting
         bindToTextureRgb/bindToTextureRgba to FALSE means that swrast can't find any fbConfigs to use,
         so setting these to 0, even if we know bindToTexture isn't available, isn't a good idea...
       */
      if (screen->has_WGL_ARB_render_texture)
        {
          c->base.bindToTextureRgb = ATTR_VALUE(WGL_BIND_TO_TEXTURE_RGB_ARB, -1);
          c->base.bindToTextureRgba = ATTR_VALUE(WGL_BIND_TO_TEXTURE_RGBA_ARB, -1);
        }
      else
        {
          c->base.bindToTextureRgb = -1;
          c->base.bindToTextureRgba = -1;
        }
      c->base.bindToMipmapTexture = -1;
      c->base.bindToTextureTargets = GLX_TEXTURE_1D_BIT_EXT | GLX_TEXTURE_2D_BIT_EXT | GLX_TEXTURE_RECTANGLE_BIT_EXT;
      c->base.yInverted = -1;

      n++;

      // update previous config to point to this config
      if (prev)
        prev->base.next = &(c->base);

      prev = c;
    }

  screen->base.numFBConfigs = n;
  screen->base.fbconfigs = &(result->base);
}