File: PsychWindowSupport.c

package info (click to toggle)
psychtoolbox-3 3.0.9%2Bsvn2579.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 63,408 kB
  • sloc: ansic: 73,310; cpp: 11,139; objc: 3,129; sh: 1,669; python: 382; php: 272; makefile: 172; java: 113
file content (5876 lines) | stat: -rwxr-xr-x 324,355 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
/*
	PsychToolbox3/Source/Common/Screen/PsychWindowSupport.c
	
	PLATFORMS:	
	
		All.  
				
	AUTHORS:
	
		Allen Ingling		awi	Allen.Ingling@nyu.edu
		Mario Kleiner		mk	mario.kleiner at tuebingen.mpg.de

	HISTORY:
	
		12/20/02		awi	Wrote it mostly by modifying SDL-specific refugees (from an experimental SDL-based Psychtoolbox).
		11/16/04		awi	Added description.
		4/22/05			mk      Added support for OpenGL stereo windows and enhanced Flip-behaviour:
						Flip'ing at specified deadline, retaining instead of clear'ing backbuffer during flip,
						return of stimulus onset related timestamps, properly syncing to VBL.
		4/29/05			mk      Additional safety checks for VBL sync in PsychOpenOnscreenWindow().
		5/14/05			mk      Additional safety checks for insufficient gfx-hardware and multi-display setups,
						failing beam-position queries. New refresh interval estimation code, reworked Flip.
		5/19/05			mk      Extra check for 'flipwhen' values over 1000 secs in future: Abort.
		5/30/05			mk      Check for Screen('Preference', 'SkipSyncTests', 1) -> Shortened tests, if set.
		6/09/05			mk      Experimental support for busy-waiting for VBL and for multi-flip of stereo displays.
		9/30/05			mk      Added PsychRealtimePriority for improving timing tests in PsychOpenWindow()
		9/30/05			mk      Added check for Screen('Preference', 'VisualDebugLevel', level) -> Amount of vis. feedback.
		10/10/05		mk      Important Bugfix for PsychRealtimePriority() - didn't switch back to non-RT priority!!
		10/19/05		awi	Cast NULL to CGLPixelFormatAttribute type to make the compiler happy.
		12/27/05		mk	PsychWindowSupport.h/c contains the shared parts of the windows implementation for all OS'es.
		3/07/06			awi	Print warnings conditionally according to PsychPrefStateGet_SuppressAllWarnings().
		11/14/06                mk      Replace blue screen by welcome text splash screen. Tighten threshold for frame skip detector for
		                                systems without beampos queries from 1.5 to 1.2, remove 0.5 offset and use unified deadline calculation
						code for the flipwhen>0 case and the flipwhen==0 case. All this should not matter on systems with beampos
						queries, but it makes the test more sensitive on systems without beampos queries, biasing it to more false
						positives on noisy systems, reducing the chance for false negatives.
                11/15/06                mk      Experimental support for low-level queries of vbl count and time from the driver: Used for verifying
                                                beampos query timestamping and as a fallback on systems that lack beampos query support.
 
	DESCRIPTION:
	
	NOTES:
	
        Documentation on the kernel-level shared memory access to the gfx-driver can be found here:
 
        http://developer.apple.com/documentation/Darwin/Reference/IOKit/IOFramebufferShared/index.html
	
        TO DO: 
	
*/

#include "Screen.h"

#if PSYCH_SYSTEM == PSYCH_LINUX
#include <errno.h>
#endif

#if PSYCH_SYSTEM != PSYCH_WINDOWS
//#ifndef MATLAB_R11
#include "ptbstartlogo.h"
#else
/* This is a placeholder for ptbstartlogo.h until the fu%&$ing M$-Compiler can handle it.
 * GIMP RGBA C-Source image dump (welcomeWait.c)
 */
static const struct {
  unsigned int 	 width;
  unsigned int 	 height;
  unsigned int 	 bytes_per_pixel; /* 3:RGB, 4:RGBA */ 
  unsigned char	 pixel_data[4 + 1];
} gimp_image = {
  1, 1, 4, "    ",};
#endif

static struct {
  unsigned int 	 width;
  unsigned int 	 height;
  unsigned int 	 bytes_per_pixel; /* 3:RGB, 4:RGBA */ 
  unsigned char* pixel_data;
} splash_image;

/* Flag which defines if userspace rendering is active: */
static psych_bool inGLUserspace = FALSE;

// We keep track of the current active rendertarget in order to
// avoid needless state changes:
static PsychWindowRecordType* currentRendertarget = NULL;

// The handle of the masterthread - The Matlab/Octave/PTB main interpreter thread: This
// is initialized when opening the first onscreen window. Its used in PsychSetDrawingTarget()
// to discriminate between the masterthread and the worker threads for async flip operations:
static psych_threadid	masterthread = (psych_threadid) NULL;

// Count of currently async-flipping onscreen windows:
static unsigned int	asyncFlipOpsActive = 0;

// Return count of currently async-flipping onscreen windows:
unsigned int PsychGetNrAsyncFlipsActive(void)
{
	return(asyncFlipOpsActive);
}

psych_bool PsychIsMasterThread(void)
{
	return(PsychIsCurrentThreadEqualToId(masterthread));
}

// Dynamic rebinding of ARB extensions to core routines:
// This is a trick to get GLSL working on current OS-X (10.4.4). MacOS-X supports the OpenGL
// shading language on all graphics cards as an ARB extension. But as OS-X only supports
// OpenGL versions < 2.0 as of now, the functionality is not available as core functions, but
// only as their ARB counterparts. e.g., glCreateProgram() is always a NULL-Ptr on OS-X, but
// glCreateProgramObjectARB() is supported with exactly the same syntax and behaviour. By
// binding glCreateProgram as glCreateProgramObjectARB, we allow users to write Matlab code
// that uses glCreateProgram -- which is cleaner code than using glCreateProgramObjectARB,
// and it saves us from parsing tons of additional redundant function definitions anc code
// generation...
// In this function, we try to detect such OS dependent quirks and try to work around them...
void PsychRebindARBExtensionsToCore(void)
{   
    // Remap unsupported OpenGL 2.0 core functions for GLSL to supported ARB extension counterparts:
    if (NULL == glCreateProgram) glCreateProgram = glCreateProgramObjectARB;
    if (NULL == glCreateShader) glCreateShader = glCreateShaderObjectARB;
    if (NULL == glShaderSource) glShaderSource = glShaderSourceARB;
    if (NULL == glCompileShader) glCompileShader = glCompileShaderARB;
    if (NULL == glAttachShader) glAttachShader = glAttachObjectARB;
    if (NULL == glLinkProgram) glLinkProgram = glLinkProgramARB;
    if (NULL == glUseProgram) glUseProgram = glUseProgramObjectARB;
    if (NULL == glGetAttribLocation) glGetAttribLocation = glGetAttribLocationARB;
    // if (NULL == glGetUniformLocation) glGetUniformLocation = (GLint (*)(GLint, const GLchar*)) glGetUniformLocationARB;
    if (NULL == glGetUniformLocation) glGetUniformLocation = glGetUniformLocationARB;
    if (NULL == glUniform1f) glUniform1f = glUniform1fARB;
    if (NULL == glUniform2f) glUniform2f = glUniform2fARB;
    if (NULL == glUniform3f) glUniform3f = glUniform3fARB;
    if (NULL == glUniform4f) glUniform4f = glUniform4fARB;
    if (NULL == glUniform1fv) glUniform1fv = glUniform1fvARB;
    if (NULL == glUniform2fv) glUniform2fv = glUniform2fvARB;
    if (NULL == glUniform3fv) glUniform3fv = glUniform3fvARB;
    if (NULL == glUniform4fv) glUniform4fv = glUniform4fvARB;
    if (NULL == glUniform1i) glUniform1i = glUniform1iARB;
    if (NULL == glUniform2i) glUniform2i = glUniform2iARB;
    if (NULL == glUniform3i) glUniform3i = glUniform3iARB;
    if (NULL == glUniform4i) glUniform4i = glUniform4iARB;
    if (NULL == glUniform1iv) glUniform1iv = glUniform1ivARB;
    if (NULL == glUniform2iv) glUniform2iv = glUniform2ivARB;
    if (NULL == glUniform3iv) glUniform3iv = glUniform3ivARB;
    if (NULL == glUniform4iv) glUniform4iv = glUniform4ivARB;
    if (NULL == glUniformMatrix2fv) glUniformMatrix2fv = glUniformMatrix2fvARB;
    if (NULL == glUniformMatrix3fv) glUniformMatrix3fv = glUniformMatrix3fvARB;
    if (NULL == glUniformMatrix4fv) glUniformMatrix4fv = glUniformMatrix4fvARB;
    if (NULL == glGetShaderiv) glGetShaderiv = glGetObjectParameterivARB;
    if (NULL == glGetProgramiv) glGetProgramiv = glGetObjectParameterivARB;
    if (NULL == glGetShaderInfoLog) glGetShaderInfoLog = glGetInfoLogARB;
    if (NULL == glGetProgramInfoLog) glGetProgramInfoLog = glGetInfoLogARB;
    if (NULL == glValidateProgram) glValidateProgram = glValidateProgramARB;
    if (NULL == glGenQueries) glGenQueries = glGenQueriesARB;
    if (NULL == glDeleteQueries) glDeleteQueries = glDeleteQueriesARB;
    if (NULL == glBeginQuery) glBeginQuery = glBeginQueryARB;
    if (NULL == glEndQuery) glEndQuery = glEndQueryARB;
    if (NULL == glGetQueryObjectuiv) glGetQueryObjectuiv = glGetQueryObjectuivARB;
	
    // Misc other stuff to remap...
    if (NULL == glDrawRangeElements) glDrawRangeElements = glDrawRangeElementsEXT;
    return;
}


/*
    PsychOpenOnscreenWindow()
    
    This routine first calls the operating system dependent setup routine in PsychWindowGlue to open
    an onscreen - window and create, setup and attach an OpenGL rendering context to it.

    Then it does all the OS independent setup stuff, like sanity and timing checks, determining the
    real monitor refresh and flip interval and start/endline of VBL via measurements and so on...

    -The pixel format and the context are stored in the target specific field of the window recored.  Close
    should clean up by destroying both the pixel format and the context.
    
    -We mantain the context because it must be be made the current context by drawing functions to draw into 
    the specified window.
    
    -We maintain the pixel format object because there seems to be no way to retrieve that from the context.
    
    -To tell the caller to clean up PsychOpenOnscreenWindow returns FALSE if we fail to open the window. It 
    would be better to just issue an PsychErrorExit() and have that clean up everything allocated outside of
    PsychOpenOnscreenWindow().
    
    MK: The new option 'stereomode' allows selection of stereo display instead of mono display:
    0 (default) == Old behaviour -> Monoscopic rendering context.
    >0          == Stereo display, where the number defines the type of stereo algorithm to use.
    =1          == Use OpenGL built-in stereo by creating a context/window with left- and right backbuffer.
    =2          == Use compressed frame stereo: Put both views into one framebuffer, one in top half, other in lower half.
 
    MK: Calibration/Measurement code was added that estimates the monitor refresh interval and the number of
        the last scanline (End of vertical blank interval). This increases time for opening an onscreen window
        by up to multiple seconds on slow (60 Hz) displays, but allows reliable syncing to VBL and kind of WaitBlanking
        functionality in Screen('Flip')... Also lots of tests for proper working of VBL-Sync and other stuff have been added.
        Contains experimental support for flipping multiple displays synchronously, e.g., for dual display stereo setups.
 
*/
psych_bool PsychOpenOnscreenWindow(PsychScreenSettingsType *screenSettings, PsychWindowRecordType **windowRecord, int numBuffers, int stereomode, double* rect, int multiSample, PsychWindowRecordType* sharedContextWindow, int specialFlags)
{
    PsychRectType dummyrect;
	double splashMinDurationSecs = 0;
    double ifi_nominal=0;
    double ifi_estimate = 0;
    int retry_count=0;    
    int numSamples=0;
    double stddev=0;
    double maxsecs;    
    int VBL_Endline = -1;
    long vbl_startline, dummy_width;
    int i, maxline, bp;
    double tsum=0;
    double tcount=0;
    double ifi_beamestimate = 0;

    CGDirectDisplayID				cgDisplayID;
    int attribcount=0;
    int ringTheBell=-1;
    long VRAMTotal=0;
    long TexmemTotal=0;
    psych_bool multidisplay = FALSE;
    psych_bool sync_trouble = false;
    psych_bool sync_disaster = false;
    int skip_synctests;
    int visual_debuglevel = PsychPrefStateGet_VisualDebugLevel();
    int conserveVRAM = PsychPrefStateGet_ConserveVRAM();
    int logo_x, logo_y;
    GLboolean	isFloatBuffer;
    GLint bpc;
    double maxStddev, maxDeviation, maxDuration;	// Sync thresholds and settings...
    int minSamples;
    int vblbias, vbltotal;

    // Splash screen support:
    char splashPath[FILENAME_MAX];
    char* dummychar;
    FILE* splashFd;

    // OS-9 emulation? If so, then we only work in double-buffer mode:
    if (PsychPrefStateGet_EmulateOldPTB()) numBuffers = 2;

    // Child protection: We need 2 AUX buffers for compressed stereo.
    if ((conserveVRAM & kPsychDisableAUXBuffers) && (stereomode==kPsychCompressedTLBRStereo || stereomode==kPsychCompressedTRBLStereo)) {
        printf("ERROR! You tried to disable AUX buffers via Screen('Preference', 'ConserveVRAM')\n while trying to use compressed stereo, which needs AUX-Buffers!\n");
        return(FALSE);
    }

	// Retrieve thresholds for a sync test and calibration that is considered to be successfull (i.e., !sync_disaster):
	PsychPrefStateGet_SynctestThresholds(&maxStddev, &minSamples, &maxDeviation, &maxDuration);

    //First allocate the window recored to store stuff into.  If we exit with an error PsychErrorExit() should
    //call PsychPurgeInvalidWindows which will clean up the window record. 
    PsychCreateWindowRecord(windowRecord);  		//this also fills the window index field.

	// Show our "splash-screen wannabe" startup message at opening of first onscreen window:
	// Also init the thread handle to our main thread here:
	if ((*windowRecord)->windowIndex == PSYCH_FIRST_WINDOW) {
		if(PsychPrefStateGet_Verbosity()>2) {
			printf("\n\nPTB-INFO: This is Psychtoolbox-3 for %s, under %s (Version %i.%i.%i - Build date: %s).\n", PSYCHTOOLBOX_OS_NAME, PSYCHTOOLBOX_SCRIPTING_LANGUAGE_NAME, PsychGetMajorVersionNumber(), PsychGetMinorVersionNumber(), PsychGetPointVersionNumber(), PsychGetBuildDate());
			printf("PTB-INFO: Type 'PsychtoolboxVersion' for more detailed version information.\n"); 
			printf("PTB-INFO: Most parts of the Psychtoolbox distribution are licensed to you under terms of the MIT License, with\n");
			printf("PTB-INFO: some restrictions. See file 'License.txt' in the Psychtoolbox root folder for the exact licensing conditions.\n\n");
		}

		if (PsychPrefStateGet_EmulateOldPTB() && PsychPrefStateGet_Verbosity()>1) {
			printf("PTB-INFO: Psychtoolbox is running in compatibility mode to old MacOS-9 PTB. This is an experimental feature with\n");
			printf("PTB-INFO: limited support and possibly significant bugs hidden in it! Use with great caution and avoid if you can!\n");
			printf("PTB-INFO: Currently implemented: Screen('OpenOffscreenWindow'), Screen('CopyWindow') and Screen('WaitBlanking')\n");
		}
		
		// Assign unique id of this thread (the Matlab/Octave main interpreter thread)
		// as masterthread. This masterthread is the only one allowed to execute complex
		// OpenGL code, therefore some routines check for that, e.g., PsychSetDrawingTarget().
		// This is part of the async flip mechanism:
		masterthread = PsychGetThreadId();
	}

	// Add all passed-in specialFlags to windows specialflags:
	(*windowRecord)->specialflags |= specialFlags;

    // Assign the passed windowrect 'rect' to the new window:
    PsychCopyRect((*windowRecord)->rect, rect);
    
    // Assign requested level of multisampling for hardware Anti-Aliasing: 0 means - No hw-AA,
    // n>0 means: use hw-AA and try to get multisample buffers for at least n samples per pixel.
    // Todays hardware (as of mid 2006) typically supports 2x and 4x AA, Radeons support 6x AA.
    // If a request for n samples/pixel can't be satisfied by the hardware/OS, then we fall back
    // to the highest possible value. Worst case: We fall back to non-multisampled mode.
    // We pass in the requested value, after opening the window, the windowRecord contains
    // the real value used.
    (*windowRecord)->multiSample = multiSample;

    // Assign requested color buffer depth:
    (*windowRecord)->depth = screenSettings->depth.depths[0];
    
	// Explicit OpenGL context ressource sharing requested?
	if (sharedContextWindow) {
		// A pointer to a previously created onscreen window was provided and the OpenGL context of
		// the new window shall share ressources with the context of the provided window:
		(*windowRecord)->slaveWindow = sharedContextWindow;
	}
	
	// Automatic OpenGL context ressource sharing? By default, if no explicit sharing with
	// a specific sharedContextWindow is requested and context sharing is not disabled via
	// some 'ConserveVRAM' flag, we will try to share ressources of all OpenGL contexts
	// to simplify multi-window operations.
	if ((sharedContextWindow == NULL) && ((conserveVRAM & kPsychDontShareContextRessources) == 0) && (PsychCountOpenWindows(kPsychDoubleBufferOnscreen) + PsychCountOpenWindows(kPsychSingleBufferOnscreen) > 0)) {
		// Try context ressource sharing: Assign first onscreen window as sharing window:
		i = PSYCH_FIRST_WINDOW - 1;
		do {
			i++;
			FindWindowRecord(i, &((*windowRecord)->slaveWindow));
		} while (((*windowRecord)->slaveWindow->windowType != kPsychDoubleBufferOnscreen) && ((*windowRecord)->slaveWindow->windowType != kPsychSingleBufferOnscreen));
		// Ok, now we should have the first onscreen window assigned as slave window.
		if(PsychPrefStateGet_Verbosity()>3) printf("PTB-INFO: This oncreen window tries to share OpenGL context ressources with window %i.\n", i);
	}
	
    //if (PSYCH_DEBUG == PSYCH_ON) printf("Entering PsychOSOpenOnscreenWindow\n");
    
    // Call the OS specific low-level Window & Context setup routine:
    if (!PsychOSOpenOnscreenWindow(screenSettings, (*windowRecord), numBuffers, stereomode, conserveVRAM)) {
        printf("\nPTB-ERROR[Low-Level setup of window failed]:The specified display may not support double buffering and/or stereo output. There could be insufficient video memory\n\n");
        FreeWindowRecordFromPntr(*windowRecord);
        return(FALSE);
    }

	// At this point, the new onscreen windows master OpenGL context is active and bound...

	// Check for properly working glGetString() -- Some drivers (Some NVidia GF8/9 drivers on WinXP)
	// have a bug in conjunction with context ressource sharing here. Non-working glGetString is
	// a showstopper bug, but we should tell the user about the problem and stop safely instead
	// of taking whole runtime down:
	if (NULL == glGetString(GL_EXTENSIONS)) {
		// Game over:
		printf("PTB CRITICAL ERROR: Your graphics driver seems to have a bug which causes the OpenGL command glGetString() to malfunction!\n");
		printf("PTB CRITICAL ERROR: Can't continue safely, will therefore abort execution here.\n");
		printf("PTB CRITICAL ERROR: In the past this bug has been observed with some NVidia Geforce 8000 drivers under WindowsXP when using\n");
		printf("PTB CRITICAL ERROR: OpenGL 3D graphics mode. The recommended fix is to update your graphics drivers. A workaround that may\n");
		printf("PTB CRITICAL ERROR: work (but has its own share of problems) is to disable OpenGL context isolation. Type 'help ConserveVRAMSettings'\n");
		printf("PTB CRICICAL ERROR: and read the paragraph about setting '8' for more info.\n\n");

		// We abort! Close the onscreen window:
		PsychOSCloseWindow(*windowRecord);
		// Free the windowRecord:
		FreeWindowRecordFromPntr(*windowRecord);
		// Done. Return failure:
		return(FALSE);
	}

	#if PSYCH_SYSTEM == PSYCH_WINDOWS
    if(PsychPrefStateGet_Verbosity()>1) {
		if (strstr((char*) glGetString(GL_RENDERER), "GDI")) {
			printf("\n\n\n\nPTB-WARNING: Seems that Microsofts OpenGL software renderer is active! This will likely cause miserable\n");
			printf("PTB-WARNING: performance, lack of functionality and severe timing and synchronization problems.\n");
			printf("PTB-WARNING: Most likely you do not have native OpenGL vendor supplied drivers (ICD's) for your graphics hardware\n");
			printf("PTB-WARNING: installed on your system.Many Windows machines (and especially Windows Vista) come without these preinstalled.\n");
			printf("PTB-WARNING: Go to the webpage of your computer vendor or directly to the webpage of NVidia/AMD/ATI/3DLabs/Intel\n");
			printf("PTB-WARNING: and make sure that you've download and install their latest driver for your graphics card.\n");
			printf("PTB-WARNING: Other causes, after you've ruled out the above:\n");
			printf("PTB-WARNING: Maybe you run at a too high display resolution, or the system is running out of ressources for some other reason.\n");
			printf("PTB-WARNING: Another reason could be that you disabled hardware acceleration in the display settings panel: Make sure that\n");
			printf("PTB-WARNING: in Display settings panel -> Settings -> Advanced -> Troubleshoot -> The hardware acceleration slider is\n");
			printf("PTB-WARNING: set to 'Full' (rightmost position).\n\n");
			printf("PTB-WARNING: Actually..., it is pointless to continue with the software renderer, that will cause more trouble than good.\n");
			printf("PTB-WARNING: I will abort now. Read the troubleshooting tips above to fix the problem. You can override this if you add the following\n");
			printf("PTB-WARNING: command: Screen('Preference', 'Verbosity', 1); to get a functional, but close to useless window up and running.\n\n\n");
			
			// We abort! Close the onscreen window:
			PsychOSCloseWindow(*windowRecord);

			// Free the windowRecord:
			FreeWindowRecordFromPntr(*windowRecord);

			// Done. Return failure:
			return(FALSE);			
		}
	}
	#endif

	// Decide if 10 bpc framebuffer should be enabled by our own kernel driver trick:
	if ((*windowRecord)->depth == 30) {
		// Support for kernel driver available?
#if PSYCH_SYSTEM == PSYCH_OSX || PSYCH_SYSTEM == PSYCH_LINUX
		if ((PSYCH_SYSTEM == PSYCH_LINUX) && (strstr((char*) glGetString(GL_VENDOR), "NVIDIA") || strstr((char*) glGetString(GL_VENDOR), "nouveau") ||
		    strstr((char*) glGetString(GL_VENDOR), "Intel") ||
		    ((strstr((char*) glGetString(GL_VENDOR), "ATI") || strstr((char*) glGetString(GL_VENDOR), "AMD")) && strstr((char*) glGetString(GL_RENDERER), "Fire")))) {
			// NVidia/Intel GPU or ATI Fire-Series GPU: Only native support by driver, if at all...
			printf("\nPTB-INFO: Your script requested a 30bpp, 10bpc framebuffer, but this is only supported on few special graphics cards and drivers on Linux.");
			printf("\nPTB-INFO: This may or may not work for you - Double check your results! Theoretically, the 2008 series ATI/AMD FireGL/FirePro and NVidia cards may support this with some drivers,");
			printf("\nPTB-INFO: but you must enable it manually in the Catalyst control center or NVidia control center (somewhere under ''Workstation settings'')\n");
		}
		else {
			// Only support our homegrown method with PTB kernel driver on ATI/AMD hardware:
			if (!PsychOSIsKernelDriverAvailable(screenSettings->screenNumber) || strstr((char*) glGetString(GL_VENDOR), "NVIDIA") || strstr((char*) glGetString(GL_VENDOR), "Intel")) {
				printf("\nPTB-ERROR: Your script requested a 30bpp, 10bpc framebuffer, but the Psychtoolbox kernel driver is not loaded and ready.\n");
				printf("PTB-ERROR: The driver currently only supports selected ATI Radeon GPU's (X1000/HD2000/HD3000/HD4000 series and corresponding FireGL/FirePro models).\n");
				printf("PTB-ERROR: On MacOS/X the driver must be loaded and functional for your graphics card for this to work.\n");
				printf("PTB-ERROR: Read 'help PsychtoolboxKernelDriver' for setup information.\n");
				printf("PTB-ERROR: On Linux you must either configure your system by executing the script PsychLinuxConfiguration once,\n");
				printf("PTB-ERROR: or start Octave or Matlab as root, ie. system administrator or via sudo command for this to work.\n\n");
				PsychOSCloseWindow(*windowRecord);
				FreeWindowRecordFromPntr(*windowRecord);
				return(FALSE);			
			}
			
			// Basic support seems to be there, set the request flag.
			(*windowRecord)->specialflags|= kPsychNative10bpcFBActive;
		}
        
		if (PsychPrefStateGet_ConserveVRAM() & kPsychEnforce10BitFramebufferHack) {
			printf("PTB-INFO: Override: Will try to enable 10 bpc framebuffer mode regardless if i think it is needed/sensible or not.\n");
			printf("PTB-INFO: Override: Doing so because you set the kPsychEnforce10BitFramebufferHack flag in Screen('Preference','ConserveVRAM').\n");
			printf("PTB-INFO: Override: Cross your fingers, this may end badly...\n");
			(*windowRecord)->specialflags|= kPsychNative10bpcFBActive;
		}
#else
		// Not supported by our own code and kernel driver (we don't have such a driver for Windows), but some recent 2008
		// series FireGL cards at least provide the option to enable this natively - although it didn't work properly in our tests.
		printf("\nPTB-INFO: Your script requested a 30bpp, 10bpc framebuffer, but this is only supported on few special graphics cards and drivers on MS-Windows.");
		printf("\nPTB-INFO: This may or may not work for you - Double check your results! Theoretically, the 2008 series ATI FireGL/FirePro and NVidia Quadro cards may support this with some drivers,");
		printf("\nPTB-INFO: but you must enable it manually in the Catalyst or Quadro Control center (somewhere under ''Workstation settings'')\n");
#endif
	}

	if ((sharedContextWindow == NULL) && ((*windowRecord)->slaveWindow)) {
		// Undo slave window assignment from context sharing:
		(*windowRecord)->slaveWindow = NULL;
	}
	
    // Now we have a valid, visible onscreen (fullscreen) window with valid
    // OpenGL context attached. We mark it immediately as Onscreen window,
    // so in case of an error, the Screen('CloseAll') routine can properly
    // close it and release the Window system and OpenGL ressources.
    if(numBuffers==1) {
      (*windowRecord)->windowType=kPsychSingleBufferOnscreen;
    } 
    else {
      (*windowRecord)->windowType=kPsychDoubleBufferOnscreen;
    }

    // Dynamically rebind core extensions: Ugly ugly...
    PsychRebindARBExtensionsToCore();
    
    if ((((*windowRecord)->depth == 30) && !((*windowRecord)->specialflags & kPsychNative10bpcFBActive)) || (*windowRecord)->depth == 64 || (*windowRecord)->depth == 128) {

        // Floating point framebuffer active? GL_RGBA_FLOAT_MODE_ARB would be a viable alternative?
		isFloatBuffer = FALSE;
        glGetBooleanv(GL_COLOR_FLOAT_APPLE, &isFloatBuffer);
        if (isFloatBuffer) {
            if (PsychPrefStateGet_Verbosity() > 2) printf("PTB-INFO: Floating point precision framebuffer enabled.\n");
        }
        else {
            if (PsychPrefStateGet_Verbosity() > 2) printf("PTB-INFO: Fixed point precision integer framebuffer enabled.\n");
        }
        
        // Query and show bpc for all channels:
        glGetIntegerv(GL_RED_BITS, &bpc);
        if (PsychPrefStateGet_Verbosity() > 3) printf("PTB-INFO: System Frame buffer provides %i bits for red channel.\n", bpc);
        glGetIntegerv(GL_GREEN_BITS, &bpc);
        if (PsychPrefStateGet_Verbosity() > 3) printf("PTB-INFO: System Frame buffer provides %i bits for green channel.\n", bpc);
        glGetIntegerv(GL_BLUE_BITS, &bpc);
        if (PsychPrefStateGet_Verbosity() > 3) printf("PTB-INFO: System Frame buffer provides %i bits for blue channel.\n", bpc);
        glGetIntegerv(GL_ALPHA_BITS, &bpc);
        if ((*windowRecord)->depth == 30) {
			if (PsychPrefStateGet_Verbosity() > 4) {
				printf("PTB-INFO: Hardware frame buffer provides %i bits for alpha channel. This is the effective alpha bit depths if the imaging pipeline is off.\n", bpc);
				printf("PTB-INFO: If the imaging pipeline is enabled, then the effective alpha bit depth depends on imaging pipeline configuration and is likely >= 8 bits.\n");
			}
		}
		else {
			if (PsychPrefStateGet_Verbosity() > 3) {
				printf("PTB-INFO: System frame buffer provides %i bits for alpha channel, but effective alpha bits depends on imaging pipeline setup, if any.\n", bpc);
			}
		}
    }
	
	// Query if this onscreen window has a backbuffer with alpha channel, i.e.
	// it has more than zero alpha bits: 
	glGetIntegerv(GL_ALPHA_BITS, &bpc);
	
	// Windows are either RGB or RGBA, so either 3 or 4 channels. Here we
	// assign the default depths for this window record. This value needs to get
	// overriden when imaging pipeline is active, because there we use framebuffer
	// objects as backing store which always have RGBA 4 channel format.
	(*windowRecord)->nrchannels = (bpc > 0) ? 4 : 3;

	// We need the real color depth (bits per color component) of the framebuffer attached
	// to this onscreen window. We need it to setup color range correctly:
	// Assign true bit depth bpc of framebuffer, unless we're using our own
	// special native 10 bit framebuffer mode on OS/X and Linux via kernel driver.
	// In that special case, the imaging pipeline will reassign a proper effective
	// depths of 16 bpc or 32 bpc for the float FBO drawbuffer later on.
	if (!((*windowRecord)->specialflags & kPsychNative10bpcFBActive)) {
		// Standard native framebuffer/backbuffer for this onscreen window bound.
		// Effective bpc is that of the OpenGL context. Query and assign:
		bpc = 8;
        glGetIntegerv(GL_RED_BITS, &bpc);		
		(*windowRecord)->bpc = bpc;
		if ((PsychPrefStateGet_Verbosity() > 2) && (bpc > 8)) printf("PTB-INFO: Real (native, queried) color resolution of the GPU framebuffer is %i bits per RGB color component.\n", bpc);
	}
	else {
		// Special 10 bpc framebuffer activated by our own method:
		bpc = 10;
		if (PsychPrefStateGet_Verbosity() > 2) printf("PTB-INFO: Real (kernel driver provided) color resolution of the GPU framebuffer is 10 bits per RGB color component.\n");
	}

	// Compute colorRange for bit depths:
	(*windowRecord)->colorRange = (double) ((1 << bpc) - 1);

    // Now we start to fill in the remaining windowRecord with settings:
    // -----------------------------------------------------------------

    // Normalize final windowRect: It is shifted so that its top-left corner is
    // always the origin (0,0). This way we lose the information about the absolute
    // position of the window on the screen, but this can be still queried from the
    // Screen('Rect') command for a screen index. Not normalizing creates breakage
    // in a lot of our own internal code, many demos and probably a lot of user code.
    PsychCopyRect(dummyrect, (*windowRecord)->rect);
    PsychNormalizeRect(dummyrect, (*windowRecord)->rect);

    // Setup a temporary clientrect for window which is a copy of rect. This is
    // just to bring us through "bringup" of the window. It will be replaced in
    // SCREENOpenWindow() by a properly computed clientrect:
    PsychCopyRect((*windowRecord)->clientrect, (*windowRecord)->rect);

	// Startup-Splashimage display code:
	// ---------------------------------
	
	// Default to "No external splash screen assigned":
	memset((void*) &splash_image, 0, sizeof(splash_image));

	// We load and display the splash image if the 'welcome' screen is enabled and we can
	// find it:
	if ((visual_debuglevel >= 4) && (strlen(PsychRuntimeGetPsychtoolboxRoot(FALSE)) > 0)) {
		// Yes! Assemble full path name to splash image:
		sprintf(splashPath, "%sPsychBasic/WelcomeSplash.ppm", PsychRuntimeGetPsychtoolboxRoot(FALSE));

		// Try to open splash image file:
		splashFd = fopen(splashPath, "rb");
		if (splashFd) {
			// Worked. Read header:

			// Check for valid "P6" magic of PPM file:
			dummychar = fgets(splashPath, sizeof(splashPath), splashFd);
			splash_image.bytes_per_pixel = (strstr(splashPath, "P6")) ? 1 : 0;
			if (PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG: PPM file magic is %s -> %s\n", splashPath, (splash_image.bytes_per_pixel) ? "Ok" : "Rejected");

			// Skip comment lines...
			while (fgets(splashPath, sizeof(splashPath), splashFd) && strstr(splashPath, "#")) if (PsychPrefStateGet_Verbosity() > 5) {
				printf("%s", splashPath);
			}

			// Check for valid header:
			if ((splash_image.bytes_per_pixel) && (2 == sscanf(splashPath, "%i %i", &splash_image.width, &splash_image.height)) &&
				(1 == fscanf(splashFd, "%i", &splash_image.bytes_per_pixel)) &&
				(splash_image.width > 0 && splash_image.width <= 1280) && (splash_image.height > 0 && splash_image.height <= 1024) &&
				(splash_image.bytes_per_pixel == 255)) {

				// Header for a PPM file read, detected and valid. Image dimensions within valid size range up to 1024 x 768, 8 bpc, 24 bpp.
				if (PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG: Recognized splash image of %i x %i pixels, maxlevel %i. Loading...\n", splash_image.width, splash_image.height, splash_image.bytes_per_pixel);

				// Allocate image buffer:
				splash_image.bytes_per_pixel = 0;
				if ((splash_image.pixel_data = (unsigned char*) malloc(splash_image.width * splash_image.height * 3)) != NULL) {
					// Allocated. Read content:

					// Skip one byte:
					i = fread(splash_image.pixel_data, 1, 1, splashFd);

					if (fread(splash_image.pixel_data, splash_image.width * splash_image.height * 3, 1, splashFd) == 1) {
						// Success! Mark loaded splash image as "valid" and set its format:
						splash_image.bytes_per_pixel = GL_RGB;
					}
					else {
						// Read failed. Revert to default splash:
						free(splash_image.pixel_data);
						splash_image.pixel_data = NULL;
					}
				}
			}
			else {
				if (PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG: Splash image rejected: %s, %i x %i maxlevel %i. Fallback...\n", splashPath, splash_image.width, splash_image.height, splash_image.bytes_per_pixel);
				splash_image.bytes_per_pixel = 0;
			}
			
			fclose(splashFd);
		}
		else {
			// Failed: We don't care why.
			if (PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG: Failed to read splash image from %s [%s].\n", splashPath, strerror(errno));
			errno = 0;
		}
	}

	// Need to use fallback hard-coded splash image?
	if (splash_image.bytes_per_pixel != GL_RGB) {
		// No splash image loaded. Use our old hard-coded "Welcome" splash:
		splash_image.width = gimp_image.width;
		splash_image.height = gimp_image.height;
		splash_image.pixel_data = (unsigned char*) &(gimp_image.pixel_data[0]);
		splash_image.bytes_per_pixel = GL_RGBA;
	}
	else if (strlen(PsychRuntimeGetPsychtoolboxRoot(TRUE)) > 0) {
		// Splash image ready. Check if the splash image should be shown for
		// longer because a Psychtoolbox update/installation was just performed
		// and this is the first invocation of Screen() since then.
		sprintf(splashPath, "%sscreen_buildnr_%i", PsychRuntimeGetPsychtoolboxRoot(TRUE), PsychGetBuildNumber());

		// Does the marker file for this Screen build already exist?
		splashFd = fopen(splashPath, "r");
		if (NULL == splashFd) {
			// No: This is the first invocation since this Screen() mex file
			// was build/installed. Create the empty marker file:
			splashFd = fopen(splashPath, "w");

			// Ok, first invocation after installation or update. Increase
			// presentation duration of our splash startup screen so users
			// are nudged to at least once notice what is displayed there:

			// Set splash image display duration to at least 10 seconds, unless our
			// special joker is used:
			if (NULL == getenv("PTB_SKIPSPLASH")) splashMinDurationSecs = 10.0;
		}

		// Close marker file in any case:
		if (splashFd) fclose(splashFd);
		errno = 0;		
	}

    // Compute logo_x and logo_y x,y offset for drawing the startup logo:
    logo_x = ((int) PsychGetWidthFromRect((*windowRecord)->rect) - (int) splash_image.width) / 2;
    logo_x = (logo_x > 0) ? logo_x : 0;
    logo_y = ((int) PsychGetHeightFromRect((*windowRecord)->rect) - (int) splash_image.height) / 2;
    logo_y = (logo_y > 0) ? logo_y : 0;

    //if (PSYCH_DEBUG == PSYCH_ON) printf("OSOpenOnscreenWindow done.\n");

    // Retrieve real number of samples/pixel for multisampling:
    (*windowRecord)->multiSample = 0;
    while(glGetError()!=GL_NO_ERROR);
    glGetIntegerv(GL_SAMPLES_ARB, (GLint*) &((*windowRecord)->multiSample));
    while(glGetError()!=GL_NO_ERROR);

    // Retrieve display handle for beamposition queries:
    PsychGetCGDisplayIDFromScreenNumber(&cgDisplayID, screenSettings->screenNumber);

    // VBL startline not yet assigned by PsychOSOpenOnscreenWindow()?
    if ((*windowRecord)->VBL_Startline == 0) {
        // Not yet assigned: Retrieve final vbl_startline, aka physical height of the display in pixels:
        PsychGetScreenSize(screenSettings->screenNumber, &dummy_width, &vbl_startline);
        (*windowRecord)->VBL_Startline = (int) vbl_startline;
    }
    else {
        // Already assigned: Get it for our consumption:
        vbl_startline = (*windowRecord)->VBL_Startline;
    }

    // Associated screens id and depth:
    (*windowRecord)->screenNumber=screenSettings->screenNumber;
    (*windowRecord)->depth=PsychGetScreenDepthValue(screenSettings->screenNumber);
    
    // MK: Assign stereomode: 0 == monoscopic (default) window. >0 Stereo output window, where
    // the number specifies the type of stereo-algorithm used. Currently value 1 is
    // supported, which means: Output via OpenGL built-in stereo facilities. This can drive
    // all Stereo display devices that are supported by MacOS-X, e.g., the "Crystal Space"
    // Liquid crystal eye shutter glasses.
    // We also support value 2 and 3, which means "compressed" stereo: Only one framebuffer is used,
    // the left-eyes image is placed in the top half of the framebuffer, the right-eyes image is
    // placed int the bottom half of the buffer. One looses half of the vertical image resolution,
    // but both views are encoded in one video frame and can be decoded by external stereo-hardware,
    // e.g., the one available from CrystalEyes, this allows for potentially faster refresh.
    // Mode 4/5 is implemented by simple manipulations to the glViewPort...
    (*windowRecord)->stereomode = stereomode;
    
    // Setup timestamps and pipeline state for 'Flip' and 'DrawingFinished' commands of Screen:
    (*windowRecord)->time_at_last_vbl = 0;
    (*windowRecord)->PipelineFlushDone = false;
    (*windowRecord)->backBufferBackupDone = false;
    (*windowRecord)->nr_missed_deadlines = 0;
	(*windowRecord)->flipCount = 0;
    (*windowRecord)->IFIRunningSum = 0;
    (*windowRecord)->nrIFISamples = 0;
    (*windowRecord)->VBL_Endline = -1;

    // Set the textureOrientation of onscreen windows to 2 aka "Normal, upright, non-transposed".
    // Textures of onscreen windows are created on demand as backup of the content of the onscreen
    // windows framebuffer. This happens in PsychSetDrawingTarget() if a switch from onscreen to
    // offscreen drawing target happens and the slow-path is used due to lack of Framebuffer-objects.
    // See code in PsychDrawingTarget()...
    (*windowRecord)->textureOrientation=2;

    // Perform a full safe reset of the framebuffer-object switching code:
    PsychSetDrawingTarget((PsychWindowRecordType*) 0x1);
    
    // Enable this windowRecords OpenGL context and framebuffer as current drawingtarget. This will also setup
    // the projection and modelview matrices, viewports and such to proper values:
    PsychSetDrawingTarget(*windowRecord);

	if(PsychPrefStateGet_Verbosity() > 3) {		
		  printf("\n\nOpenGL-Extensions are: %s\n\n", (char*) glGetString(GL_EXTENSIONS));
	}

	// Perform generic inquiry for interesting renderer capabilities and limitations/quirks
	// and setup the proper status bits for the windowRecord:
	PsychDetectAndAssignGfxCapabilities(*windowRecord);

	// Get final synctest setting after GPU caps detection:
	skip_synctests = PsychPrefStateGet_SkipSyncTests();

#if PSYCH_SYSTEM == PSYCH_OSX
    CGLRendererInfoObj				rendererInfo;
    CGOpenGLDisplayMask 			displayMask;
    CGLError					error;

    displayMask=CGDisplayIDToOpenGLDisplayMask(cgDisplayID);

    if (true) {
        long numRenderers, i;
        error= CGLQueryRendererInfo(displayMask, &rendererInfo, &numRenderers);
        if(numRenderers>1) numRenderers=1;
        for(i=0;i<numRenderers;i++) {
            CGLDescribeRenderer(rendererInfo, i, kCGLRPVideoMemory, &VRAMTotal);
            CGLDescribeRenderer(rendererInfo, i, kCGLRPTextureMemory, &TexmemTotal);
        }
        CGLDestroyRendererInfo(rendererInfo);
    }

    // Are we running a multi-display setup? Then some tests and words of wisdom for the user are important
    // to reduce the traffic on the Psychtoolbox-Forum ;-)

    // Query number of physically connected and switched on displays...
    CGDisplayCount totaldisplaycount=0;
    CGGetOnlineDisplayList(0, NULL, &totaldisplaycount);
    
	// More than one display online?
	if (totaldisplaycount > 1) {
		// Yes. Is this an ATI GPU?
		if (strstr((char*) glGetString(GL_VENDOR), "ATI")) {
			// Is this OS/X 10.5.7 or later?
			long osMinor, osBugfix, osArch;
			Gestalt(gestaltSystemVersionMinor, &osMinor);
			Gestalt(gestaltSystemVersionBugFix, &osBugfix);
			Gestalt(gestaltSysArchitecture, &osArch);
			
			if (osMinor == 5 && osBugfix >= 7 && osArch == gestaltIntel) {
				// OS/X 10.5.7 or later on IntelMac with an ATI GPU in dual-display or multi-display mode.
				// This specific configuration has serious bugs in CGDisplayBeamposition() beamposition
				// queries on multi-display setups. We mark the native beamposition mechanism as
				// unreliable, so our fallback kernel driver based solution is used instead - or
				// no beampos mechanism at all if driver not loaded:
				PsychPrefStateSet_ConserveVRAM(PsychPrefStateGet_ConserveVRAM() | kPsychDontUseNativeBeamposQuery);
				
				if (((*windowRecord)->windowIndex == PSYCH_FIRST_WINDOW) && (PsychPrefStateGet_Verbosity()>1)) {
					printf("\n\nPTB-INFO: This is Mac OS/X 10.5.%i on an Intel Mac with an ATI GPU in multi-display mode.\n", (int) osBugfix);
					printf("PTB-INFO: Beamposition queries are broken on this configuration! Will disable them.\n");
					printf("PTB-INFO: Our own beamposition mechanism will still work though if you have the PsychtoolboxKernelDriver loaded.\n");
					printf("PTB-INFO: Type 'help PsychtoolboxKernelDriver' at the command prompt for more info about this option.\n\n");
				}
			}
		}
	}
	
    if((PsychPrefStateGet_Verbosity() > 2) && ((*windowRecord)->windowIndex == PSYCH_FIRST_WINDOW)) {
		multidisplay = (totaldisplaycount>1) ? true : false;    
		if (multidisplay) {
			printf("\n\nPTB-INFO: You are using a multi-display setup (%i active displays):\n", totaldisplaycount);
			printf("PTB-INFO: Please read 'help MultiDisplaySetups' for specific information on the Do's, Dont's,\n");
			printf("PTB-INFO: and possible causes of trouble and how to diagnose and resolve them."); 
		}
		
		if (multidisplay && (!CGDisplayIsInMirrorSet(cgDisplayID) || PsychGetNumDisplays()>1)) {
			// This is a multi-display setup with separate (non-mirrored) displays: Bad for presentation timing :-(
			// Output some warning message to user, but continue. After all its the users
			// decision... ...and for some experiments were you need to show two different stims on two connected
			// monitors (haploscope, some stereo or binocular rivalry stuff) it is necessary. Let's hope they bought
			// a really fast gfx-card with plenty of VRAM :-)
			printf("\n\nPTB-INFO: According to the operating system, some of your connected displays do not seem to \n");
			printf("PTB-INFO: be switched into mirror mode. For a discussion of mirror mode vs. non-mirror mode,\n");
			printf("PTB-INFO: please read 'help MirrorMode'.\n");
		}
		
		if (CGDisplayIsInMirrorSet(cgDisplayID) && !CGDisplayIsInHWMirrorSet(cgDisplayID)) {
			// This is a multi-display setup with software-mirrored displays instead of hardware-mirrored ones: Not so good :-(
			// Output some warning message to user, but continue. After all its the users
			// decision...
			printf("\n\nPTB-WARNING: Seems that not all connected displays are switched into HARDWARE-mirror mode!\n");
			printf("PTB-WARNING: This could cause reduced drawing performance and inaccurate/wrong stimulus\n");
			printf("PTB-WARNING: presentation timing or skipped frames when showing moving/movie stimuli.\n");
			printf("PTB-WARNING: Seems that only SOFTWARE-mirroring is available for your current setup. You could\n");
			printf("PTB-WARNING: try to promote hardware-mirroring by trying different display settings...\n");
			printf("PTB-WARNING: If you still get this warning after putting your displays into mirror-mode, then\n");
			printf("PTB-WARNING: your system is unable to use hardware-mirroring and we recommend switching to a\n");
			printf("PTB-WARNING: single display setup if you encounter timing problems...\n\n");
			// Flash our visual warning bell:
			if (ringTheBell<1) ringTheBell=1;
		}
   } 
#endif

    // If we are in stereo mode 4 or 5 (free-fusion, cross-fusion, desktop-spanning stereo),
    // we need to enable Scissor tests to restrict drawing and buffer clear operations to
    // the currently set glScissor() rectangle (which is identical to the glViewport).
    if (stereomode == 4 || stereomode == 5) glEnable(GL_SCISSOR_TEST);


    if (numBuffers<2) {
		if(PsychPrefStateGet_Verbosity()>1){
			// Setup for single-buffer mode is finished!
			printf("\n\nPTB-WARNING: You are using a *single-buffered* window. This is *strongly discouraged* unless you\n");
			printf("PTB-WARNING: *really* know what you're doing! Stimulus presentation timing and all reported timestamps\n");
			printf("PTB-WARNING: will be inaccurate or wrong and synchronization to the vertical retrace will not work.\n");
			printf("PTB-WARNING: Please use *double-buffered* windows when doing anything else than debugging the PTB.\n\n");
			// Flash our visual warning bell:
			if (ringTheBell<2) ringTheBell=2;
			if (ringTheBell>=0) PsychVisualBell((*windowRecord), 4, ringTheBell);
			//mark the contents of the window record as valid.  Between the time it is created (always with PsychCreateWindowRecord) and when it is marked valid 
			//(with PsychSetWindowRecordValid) it is a potential victim of PsychPurgeInvalidWindows. 
		}
        PsychSetWindowRecordValid(*windowRecord);
        return(TRUE);
    }
    
    // Everything below this line is only for double-buffered contexts!

    // Activate syncing to onset of vertical retrace (VBL) for double-buffered windows:
    PsychOSSetVBLSyncLevel(*windowRecord, 1);
    
    // Setup of initial interframe-interval by multiple methods, for comparison:
    
    // First we query what the OS thinks is our monitor refresh interval:
    if (PsychGetNominalFramerate(screenSettings->screenNumber) > 0) {
        // Valid nominal framerate returned by OS: Calculate nominal IFI from it.
        ifi_nominal = 1.0 / ((double) PsychGetNominalFramerate(screenSettings->screenNumber));        
    }

    // This is pure eye-candy: We clear both framebuffers to a background color,
    // just to get rid of the junk that's in the framebuffers...
    // If visual debuglevel < 4 then we clear to black background...
    if (visual_debuglevel >= 4) {
		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
		
		// Splash image loaded?
		if (splash_image.bytes_per_pixel == GL_RGB) {
			// Yes: Adapt clear color to color of top-left splash pixel,
			// so colors match:
			if (PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG: glClear splash image top-left reference pixel: %i %i %i\n", splash_image.pixel_data[0],splash_image.pixel_data[1],splash_image.pixel_data[2]);
			glClearColor(((float) splash_image.pixel_data[0]) / 255.0, ((float) splash_image.pixel_data[1]) / 255.0, ((float) splash_image.pixel_data[2]) / 255.0, 0.0);
		}
		else {
			// No: Clear to white to prepare drawing of our default hard-coded logo:
			glClearColor(1,1,1,0);
		}
    }
    else {
      // Clear to black:
      glClearColor(0,0,0,0);
    }

    glDrawBuffer(GL_BACK_LEFT);
    glClear(GL_COLOR_BUFFER_BIT);

    glPixelZoom(1, -1);
    if (visual_debuglevel>=4) { glRasterPos2i(logo_x, logo_y); glDrawPixels(splash_image.width, splash_image.height, splash_image.bytes_per_pixel, GL_UNSIGNED_BYTE, (void*) &splash_image.pixel_data[0]); }
    PsychOSFlipWindowBuffers(*windowRecord);
    glClear(GL_COLOR_BUFFER_BIT);
    if (visual_debuglevel>=4) { glRasterPos2i(logo_x, logo_y); glDrawPixels(splash_image.width, splash_image.height, splash_image.bytes_per_pixel, GL_UNSIGNED_BYTE, (void*) &splash_image.pixel_data[0]); }
    PsychOSFlipWindowBuffers(*windowRecord);
    // We do it twice to clear possible stereo-contexts as well...
    if ((*windowRecord)->stereomode==kPsychOpenGLStereo) {
	glDrawBuffer(GL_BACK_RIGHT);
	glClear(GL_COLOR_BUFFER_BIT);
	if (visual_debuglevel>=4) { glRasterPos2i(logo_x, logo_y); glDrawPixels(splash_image.width, splash_image.height, splash_image.bytes_per_pixel, GL_UNSIGNED_BYTE, (void*) &splash_image.pixel_data[0]); }
	PsychOSFlipWindowBuffers(*windowRecord);
	glClear(GL_COLOR_BUFFER_BIT);
	if (visual_debuglevel>=4) { glRasterPos2i(logo_x, logo_y); glDrawPixels(splash_image.width, splash_image.height, splash_image.bytes_per_pixel, GL_UNSIGNED_BYTE, (void*) &splash_image.pixel_data[0]); }
	PsychOSFlipWindowBuffers(*windowRecord);
    }    
    glPixelZoom(1, 1);

    glDrawBuffer(GL_BACK);

	// Release dynamically allocated splash image buffer:
	if (splash_image.bytes_per_pixel == GL_RGB) {
		free(splash_image.pixel_data);
		splash_image.pixel_data = NULL;
		splash_image.bytes_per_pixel = 0;
	}

    // Make sure that the gfx-pipeline has settled to a stable state...
    glFinish();
    
	// Invalidate all corrective offsets for beamposition queries on the screen
	// associated with this window:
	PsychSetBeamposCorrection((*windowRecord)->screenNumber, 0, 0);
	
    // Complete skip of sync tests and all calibrations requested?
    // This should be only done if Psychtoolbox is not used as psychophysics
    // toolbox, but simply as a windowing/drawing toolkit for OpenGL in Matlab/Octave.
    if (skip_synctests<2) {
      // Normal calibration and at least some sync testing requested:
		if(PsychPrefStateGet_Verbosity() > 4) {
			printf("PTB-INFO: Threshold Settings for successfull video refresh calibration are: maxStdDev = %f msecs, maxDeviation = %f %%, minSamples = %i, maxDuration = %f secs.\n",
				   (float) maxStddev * 1000.0, (float) maxDeviation * 100.0, minSamples, (float) maxDuration);
		}
		
      // First we try if PsychGetDisplayBeamPosition works and try to estimate monitor refresh from it:
      
      // Check if a beamposition of 0 is returned at two points in time on OS-X:
      i = 0;
      if (((int) PsychGetDisplayBeamPosition(cgDisplayID, (*windowRecord)->screenNumber) == 0) && (PSYCH_SYSTEM == PSYCH_OSX)) {
        // Recheck after 2 ms on OS-X:
        PsychWaitIntervalSeconds(0.002);
        if ((int) PsychGetDisplayBeamPosition(cgDisplayID, (*windowRecord)->screenNumber) == 0) {
	  // A constant value of zero is reported on OS-X -> Beam position queries unsupported
	  // on this combo of gfx-driver and hardware :(
	  i=12345;
        }
      }
      
      // Check if a beamposition of -1 is returned: This would indicate that beamposition queries
      // are not available on this system: This always happens on Linux as that feature is unavailable.
      if ((-1 != ((int) PsychGetDisplayBeamPosition(cgDisplayID, (*windowRecord)->screenNumber))) && (i!=12345)) {
	// Switch to RT scheduling for timing tests:
	PsychRealtimePriority(true);
	
	// Code for estimating the final scanline of the vertical blank interval of display (needed by Screen('Flip')):
	
	// Check if PsychGetDisplayBeamPosition is working properly:
	// The first test checks, if it returns changing values at all or if it returns a constant
	// value at two measurements 2 ms apart...
	i=(int) PsychGetDisplayBeamPosition(cgDisplayID, (*windowRecord)->screenNumber);
	PsychWaitIntervalSeconds(0.002);
	if ((((int) PsychGetDisplayBeamPosition(cgDisplayID, (*windowRecord)->screenNumber)) == i) || (i < -1)) {
	  // PsychGetDisplayBeamPosition returns the same value at two different points in time?!?
	  // That's impossible on anything else than a high-precision 500 Hz display!
	  // --> PsychGetDisplayBeamPosition is not working correctly for some reason.
	  sync_trouble = true;
	  if(PsychPrefStateGet_Verbosity()>1) {
			if (i >=-1) {
				printf("\nWARNING: Querying rasterbeam-position doesn't work on your setup! (Returns a constant value %i)\n", i);
				printf("WARNING: This can happen if Psychtoolbox gets the mapping of connected displays to graphics card\n");
				printf("WARNING: outputs wrong. See 'help DisplayOutputMappings' for tips on how to resolve this problem.\n\n");
			}

			if (i < -1) printf("\nWARNING: Querying rasterbeam-position doesn't work on your setup! (Returns a negative value %i)\n", i);
	  }
	}
	else {
	  // PsychGetDisplayBeamPosition works: Use it to find VBL-Endline...
	  // Sample over 50 monitor refresh frames:
	  double told, tnew;
	  for (i=0; i<50; i++) {
	    // Take beam position samples from current monitor refresh interval:
	    maxline = -1;
	    // We spin-wait until retrace and record our highest measurement:
	    while ((bp=(int) PsychGetDisplayBeamPosition(cgDisplayID, (*windowRecord)->screenNumber)) >= maxline) maxline=bp;
	    // We also take timestamps for "yet another way" to measure monitor refresh interval...
	    PsychGetAdjustedPrecisionTimerSeconds(&tnew);
	    if (i>0) {
	      tsum+=(tnew - told);
	      tcount+=1;
	    }
	    told=tnew;
	    
		// Another (in)sanity check. Negative values immediately after retrace?
		if((int) PsychGetDisplayBeamPosition(cgDisplayID, (*windowRecord)->screenNumber) < 0) {
			// Driver bug! Abort this...
			VBL_Endline = -1;
			tnew = -1;
			
			if(PsychPrefStateGet_Verbosity()>1) printf("WARNING: Measured a negative beam position value after VBL onset!?! Broken display driver!!\n");

			// Break out of measurement loop:
			break;
		}
		
	    // Update global maximum with current sample:
	    if (maxline > VBL_Endline) VBL_Endline = maxline;
	  }
	  
	  // Setup reasonable timestamp for time of last vbl in emulation mode:
	  if (PsychPrefStateGet_EmulateOldPTB()) (*windowRecord)->time_at_last_vbl = tnew;
	}        
	
	// Switch to previous scheduling mode after timing tests:
	PsychRealtimePriority(false);
	
	// Override setting for VBL endline provided by usercode?
	if (PsychPrefStateGet_VBLEndlineOverride() >= 0) {
		// Yes. Assign it:
		if(PsychPrefStateGet_Verbosity()>1) {
			printf("PTB-WARNING: Usercode provided an override setting for the total height of the display in scanlines (aka VTOTAL)\n");
			printf("PTB-WARNING: via explicit use of the Screen('Preference', 'VBLEndlineOverride', ...); command.\n");
			printf("PTB-WARNING: Auto-detected old value was %i. New value from override which will be used for all timing: %i.\n", VBL_Endline, PsychPrefStateGet_VBLEndlineOverride());
			printf("PTB-WARNING: This is ok for working around graphics driver bugs, but make sure you don't apply this accidentally\n");
			printf("PTB-WARNING: without knowing what you're doing or why!\n\n");
		}
		
		VBL_Endline = PsychPrefStateGet_VBLEndlineOverride();
	}
	
	// Is the VBL endline >= VBL startline - 1, aka screen height?
	// Or is it outside a reasonable interval around vbl_startline or 2 * vbl_startline?
	if ((VBL_Endline < (int) vbl_startline - 1) || ((VBL_Endline > vbl_startline * 1.25) && ((VBL_Endline > vbl_startline * 2.25) || (VBL_Endline < vbl_startline * 2.0)))) {
		// Completely bogus VBL_Endline detected! Warn the user and mark VBL_Endline
		// as invalid so it doesn't get used anywhere:
		sync_trouble = true;
		ifi_beamestimate = 0;
		if(PsychPrefStateGet_Verbosity()>1) {
			printf("\nWARNING: Couldn't determine end-line of vertical blanking interval for your display! Trouble with beamposition queries?!?\n");
			printf("\nWARNING: Detected end-line is %i, which is either lower or more than 25%% higher than vbl startline %i --> Out of sane range!\n", VBL_Endline, vbl_startline);
		}
	}
	else {
		// Check if VBL_Endline is greater than 2 * vbl_startline. This would indicate the backend is running in
		// a double-scan videomode and we need to adapt our vbl_startline to be twice the framebuffer height:
		if ((VBL_Endline >= vbl_startline * 2) && (VBL_Endline < vbl_startline * 2.25)) vbl_startline = vbl_startline * 2;

		// Compute ifi from beampos:
		ifi_beamestimate = tsum / tcount;
		
		// Some GPU + driver combos need corrective offsets for beamposition reporting.
		// Following cases exist:
		// a) If the OS native beamposition query mechanism is used, we don't do correction.
		//    Although quite a few native implementations would need correction due to driver
		//    bugs, we don't (and can't) know the correct corrective values, so we can't do
		//    anything. Also we don't know which GPU + OS combos need correction and which not,
		//    so better play safe and don't correct.
		//    On OS/X the low-level code doesn't use the corrections, so nothing to do to handle
		//    case a) on OS/X. On Windows, the low-level code uses corrections if available,
		//    so we need to explicitely refrain from setting correction if we're on Windows.
		//    On Linux case a) doesn't exist.
		//
		// b) If our own mechanism is used (PsychtoolboxKernelDriver on OS/X and Linux), we
		//    do need this high-level correction for NVidia GPU's, but not for ATI/AMD GPU's,
		//    as the low-level driver code for ATI/AMD already applies proper corrections.
		
		// Only consider correction on non-Windows systems for now. We don't have any means to
		// find proper corrective values on Windows and we don't know if they are needed or if
		// drivers already do the right thing(tm) - Although testing suggests some are broken,
		// but no way for us to find out:
		if (PSYCH_SYSTEM != PSYCH_WINDOWS) {
			// Only setup correction for NVidia GPU's. Low level code will pickup these
			// corrections only if our own homegrown beampos query mechanism is used.
			// Additionally the PTB kernel driver must be available.
			// We don't setup for ATI/AMD as our low-level code already performs correct correction.
			// We also setup for Intel.
			if ((strstr((char*) glGetString(GL_VENDOR), "NVIDIA") || strstr((char*) glGetString(GL_VENDOR), "nouveau") ||
			     strstr((char*) glGetString(GL_RENDERER), "NVIDIA") || strstr((char*) glGetString(GL_RENDERER), "nouveau") ||
			     strstr((char*) glGetString(GL_VENDOR), "INTEL") || strstr((char*) glGetString(GL_VENDOR), "Intel") ||
			     strstr((char*) glGetString(GL_RENDERER), "INTEL") || strstr((char*) glGetString(GL_RENDERER), "Intel")) &&
				PsychOSIsKernelDriverAvailable((*windowRecord)->screenNumber)) {

				// Yep. Looks like we need to apply correction.
				
				// We ask the function to auto-detect proper values from GPU hardware and revert to safe (0,0) on failure:
				PsychSetBeamposCorrection((*windowRecord)->screenNumber, (int) 0xffffffff, (int) 0xffffffff);
			}
		}

		// Check if vbl startline equals measured vbl endline. That is an indication that
		// the busywait in vbl beamposition workaround is needed to keep beampos queries working
		// well. We don't do this on Windows, where it would be a tad bit too late here...
		if ((PSYCH_SYSTEM != PSYCH_WINDOWS) && (vbl_startline >= VBL_Endline)) {
			// Yup, problem. Enable the workaround:
			PsychPrefStateSet_ConserveVRAM(PsychPrefStateGet_ConserveVRAM() | kPsychUseBeampositionQueryWorkaround);

			// Tell user:
			if (PsychPrefStateGet_Verbosity() > 2) {
				printf("PTB-INFO: Implausible measured vblank endline %i indicates that the beamposition query workaround should be used for your GPU.\n", VBL_Endline);
				printf("PTB-INFO: Enabling the beamposition workaround, as explained in 'help ConserveVRAM', section 'kPsychUseBeampositionQueryWorkaround'.\n");
			}
		}
		
		// Query beampos offset correction for its opinion on vtotal. If it has a valid and
		// one, we set VBL_Endline to vtotal - 1, as this should be the case by definition.
		// We skip this override if both, measured and gpu detected endlines are the same.
		// This way we can also auto-fix issues where bugs in the properietary drivers cause
		// our VBL_Endline detection to falsely detect/report (vbl_startline >= VBL_Endline).
		// This way, at least on NVidia GPU's with the PTB kernel driver loaded, we can auto-correct
		// this proprietary driver bug without need to warn the user or require user intervention:
		PsychGetBeamposCorrection((*windowRecord)->screenNumber, &vblbias, &vbltotal);
		if ((vbltotal != 0) && (vbltotal - 1 > vbl_startline) && (vbltotal - 1 != VBL_Endline)) {
			// Plausible value for vbltotal:
			if (PsychPrefStateGet_Verbosity() > 2) {
				printf("PTB-INFO: Overriding unreliable measured vblank endline %i by low-level value %i read directly from GPU.\n", VBL_Endline, vbltotal - 1);
			}

			// Override VBL_Endline:			
			VBL_Endline = vbltotal - 1;
		}

		// Sensible result for VBL_Endline?
		if (vbl_startline >= VBL_Endline) {
			// No:
			if (PsychPrefStateGet_Verbosity() > 2) {
				printf("PTB-INFO: The detected endline of the vertical blank interval is equal or lower than the startline. This indicates\n");
				printf("PTB-INFO: that i couldn't detect the duration of the vertical blank interval and won't be able to correct timestamps\n");
				printf("PTB-INFO: for it. This will introduce a very small and constant offset (typically << 1 msec). Read 'help BeampositionQueries'\n");
				printf("PTB-INFO: for how to correct this, should you really require that last few microseconds of precision.\n");
				printf("PTB-INFO: Btw. this can also mean that your systems beamposition queries are slightly broken. It may help timing precision to\n");
				printf("PTB-INFO: enable the beamposition workaround, as explained in 'help ConserveVRAM', section 'kPsychUseBeampositionQueryWorkaround'.\n");
			}
		}
	}
	  }
      else {
		  // We don't have beamposition queries on this system:
		  ifi_beamestimate = 0;
		  // Setup fake-timestamp for time of last vbl in emulation mode:
		  if (PsychPrefStateGet_EmulateOldPTB()) PsychGetAdjustedPrecisionTimerSeconds(&((*windowRecord)->time_at_last_vbl));
      }
	  
	  // End of beamposition measurements and validation.
	  
	  // We now perform an initial calibration using VBL-Syncing of OpenGL:
	  // We use minSamples samples (minSamples monitor refresh intervals) and provide the ifi_nominal
	  // as a hint to the measurement routine to stabilize it:
      
      // We try 3 times a maxDuration seconds max., in case something goes wrong...
      while(ifi_estimate==0 && retry_count<3) {
		  numSamples = minSamples;      // Require at least minSamples *valid* samples...
		  // Require a std-deviation less than 200 microseconds on all systems except Microsoft Windows Vista / 7 and later
		  // with DWM enabled, where we accept up to maxStddev sec(!) noise. This is lame, but the only way to get 'em working at all :-(
		  stddev     = (PsychOSIsDWMEnabled()) ? maxStddev : 0.00020;
		  // If skipping of sync-test is requested, we limit the calibration to 1 sec.
		  maxsecs=(skip_synctests) ? 1 : maxDuration;
		  retry_count++;
		  ifi_estimate = PsychGetMonitorRefreshInterval(*windowRecord, &numSamples, &maxsecs, &stddev, ifi_nominal);
		  if((PsychPrefStateGet_Verbosity()>1) && (ifi_estimate==0 && retry_count<3)) {
			  printf("\nWARNING: VBL Calibration run No. %i failed. Retrying...\n", retry_count);
		  }
		  
		  // Is this the 2nd failed trial?
		  if ((ifi_estimate==0) && (retry_count == 2)) {
			  // Yes. Before we start the 3rd and final trial, we enable manual syncing of bufferswaps
			  // to retrace by setting the kPsychBusyWaitForVBLBeforeBufferSwapRequest flag for this window.
			  // Our PsychOSFlipWindowBuffers() routine will spin-wait/busy-wait manually for onset of VBL
			  // before emitting the double buffer swaprequest, in the hope that this will fix possible
			  // sync-to-retrace driver bugs/failures and fix our calibration issue. This should allow the
			  // 3rd calibration run to succeed if this is the problem:
			  (*windowRecord)->specialflags |= kPsychBusyWaitForVBLBeforeBufferSwapRequest;
			  if (PsychPrefStateGet_Verbosity() > 1) {
				  printf("WARNING: Will enable VBL busywait-workaround before trying final VBL Calibration run No. %i.\n", retry_count + 1);	
				  printf("WARNING: This will hopefully work-around graphics driver bugs of the GPU sync-to-retrace mechanism. Cross your fingers!\n");	
			  }
		  }
      }

      // Compare ifi_estimate from VBL-Sync against beam estimate. If we are in OpenGL native
      // flip-frame stereo mode, a ifi_estimate approx. 2 times the beamestimate would be valid
      // and we would correct it down to half ifi_estimate. If multiSampling is enabled, it is also
      // possible that the gfx-hw is not capable of downsampling fast enough to do it every refresh
      // interval, so we could get an ifi_estimate which is twice the real refresh, which would be valid.
      (*windowRecord)->VideoRefreshInterval = ifi_estimate;
      if ((*windowRecord)->stereomode == kPsychOpenGLStereo || (*windowRecord)->multiSample > 0) {
        // Flip frame stereo or multiSampling enabled. Check for ifi_estimate = 2 * ifi_beamestimate:
        if ((ifi_beamestimate>0 && ifi_estimate >= (1 - maxDeviation) * 2 * ifi_beamestimate && ifi_estimate <= (1 + maxDeviation) * 2 * ifi_beamestimate) ||
	    (ifi_beamestimate==0 && ifi_nominal>0 && ifi_estimate >= (1 - maxDeviation) * 2 * ifi_nominal && ifi_estimate <= (1 + maxDeviation) * 2 * ifi_nominal)
	    ){
	  // This seems to be a valid result: Flip-interval is roughly twice the monitor refresh interval.
	  // We "force" ifi_estimate = 0.5 * ifi_estimate, so ifi_estimate roughly equals to ifi_nominal and
	  // ifi_beamestimate, in order to simplify all timing checks below. We also store this value as
	  // video refresh interval...
	  ifi_estimate = ifi_estimate * 0.5f;
	  (*windowRecord)->VideoRefreshInterval = ifi_estimate;
	  if(PsychPrefStateGet_Verbosity()>2){
	    if ((*windowRecord)->stereomode == kPsychOpenGLStereo) {
	      printf("\nPTB-INFO: The timing granularity of stimulus onset/offset via Screen('Flip') is twice as long\n");
	      printf("PTB-INFO: as the refresh interval of your monitor when using OpenGL flip-frame stereo on your setup.\n");
	      printf("PTB-INFO: Please keep this in mind, otherwise you'll be confused about your timing.\n");
	    }
	    if ((*windowRecord)->multiSample > 0) {
	      printf("\nPTB-INFO: The timing granularity of stimulus onset/offset via Screen('Flip') is twice as long\n");
	      printf("PTB-INFO: as the refresh interval of your monitor when using Anti-Aliasing at multiSample=%i on your setup.\n",
		     (*windowRecord)->multiSample);
	      printf("PTB-INFO: Please keep this in mind, otherwise you'll be confused about your timing.\n");
	    }
	  }
        }
      }
    } // End of display calibration part I of synctests.
    else {
      // Complete skip of calibration and synctests: Mark all calibrations as invalid:
      ifi_beamestimate = 0;
    }

	// Windows Vista / Windows-7 / any later Windows system with Aero desktop compositor support enabled?
	// High precision timestamp enabled?
	if (PsychOSIsDWMEnabled() && (PsychPrefStateGet_VBLTimestampingMode() >= 0)) {
		// Yes, the DWM is active. Beamposition based timestamping is not reliable on a DWM composited desktop,
		// but DWM timestamps are (hopefully!). Switch to timestamping mode 3, i.e., use DWM timestamp queries.
		//		if(PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG: Timestamping mode was set to %i, but Windows DWM is active. Overriding timestamping mode to new setting 3, aka use DWM timestamping.\n", PsychPrefStateGet_VBLTimestampingMode());
		//
		//		// Switch to "DWM timestamping or nothing":
		//		PsychPrefStateSet_VBLTimestampingMode(3);
	}

	//  Nice idea, but doesn't work :-( --> Reported refresh period is not neccessarily == video refresh!
	//	// Noisy flipinterval estimate? If this is the case and we are running on MS-Vista or later with
	//	// the DWM enabled, we use the DWM's estimate of ifi_estimate instead. Should be accurate, as it
	//	// is supposedly measured in ISR interrupt via QPC calls:
	//	#if PSYCH_SYSTEM == PSYCH_WINDOWS
	//	if (PsychOSIsDWMEnabled() && PsychIsMSVista() && (ifi_estimate > 0) && (stddev > 0.00020)) {
	//		if (PsychOSGetPresentationTimingInfo((*windowRecord), FALSE, 1, NULL, NULL, NULL, &ifi_estimate, 0)) {
	//			// Reassign ifi_estimate to VideoRefreshInterval again:
	//			(*windowRecord)->VideoRefreshInterval = ifi_estimate;
	//			if(PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG: MS-Windows Vista DWM is active and measured ifi_estimate has high stddev of %f msecs. Overriding with DWM qpcRefreshPeriod of %f msecs.\n", stddev * 1000, 1000 * ifi_estimate);
	//		}
	//	}
	//	#endif

	if(PsychPrefStateGet_Verbosity()>2) printf("\n\nPTB-INFO: OpenGL-Renderer is %s :: %s :: %s\n", (char*) glGetString(GL_VENDOR), (char*) glGetString(GL_RENDERER), (char*) glGetString(GL_VERSION));

	// Running on nouveau? Then issue some words of caution about lack of timing precision:
	if ((PsychPrefStateGet_Verbosity() > 1) && (strstr((char*) glGetString(GL_VENDOR), "nouveau") || strstr((char*) glGetString(GL_RENDERER), "nouveau"))) {
		printf("\n\nPTB-WARNING: You are using the free nouveau graphics driver on your NVidia graphics card. As of %s,\n", PsychGetBuildDate());
		printf("PTB-WARNING: this driver does *not allow* robust and precise visual stimulus onset timestamping by any method at all!\n");
		printf("PTB-WARNING: If you need precise visual stimulus timing, either install the binary NVidia driver, or double-check\n");
		printf("PTB-WARNING: that a more recent version of nouveau is installed and in fact does provide proper timing.\n");
		printf("PTB-WARNING: You may find relevant info on the Psychtoolbox forum, Psychtoolbox Wiki, or by updating your Psychtoolbox.\n");
		printf("PTB-WARNING: If this warning goes away by a Psychtoolbox update then your nouveau driver is probably safe to use.\n\n");
	}

    if(PsychPrefStateGet_Verbosity()>2) {
      if (VRAMTotal>0) printf("PTB-INFO: Renderer has %li MB of VRAM and a maximum %li MB of texture memory.\n", VRAMTotal / 1024 / 1024, TexmemTotal / 1024 / 1024);
      printf("PTB-INFO: VBL startline = %i , VBL Endline = %i\n", (int) vbl_startline, VBL_Endline);
      if (ifi_beamestimate>0) {
          printf("PTB-INFO: Measured monitor refresh interval from beamposition = %f ms [%f Hz].\n", ifi_beamestimate * 1000, 1/ifi_beamestimate);

		  if ((PsychPrefStateGet_VBLTimestampingMode()==4) && !((*windowRecord)->specialflags & kPsychOpenMLDefective)) {
			  printf("PTB-INFO: Will try to use OS-Builtin %s for accurate Flip timestamping.\n", (PSYCH_SYSTEM == PSYCH_LINUX) ? "OpenML sync control support" : "method");
		  }
		  else if ((PsychPrefStateGet_VBLTimestampingMode()==3) && (PSYCH_SYSTEM == PSYCH_OSX || ((PSYCH_SYSTEM == PSYCH_LINUX) && !((*windowRecord)->specialflags & kPsychOpenMLDefective)))) {
              printf("PTB-INFO: Will try to use kernel-level interrupts for accurate Flip time stamping.\n");
          }
          else {
              if (PsychPrefStateGet_VBLTimestampingMode()>=0) printf("PTB-INFO: Will use beamposition query for accurate Flip time stamping.\n");
              if (PsychPrefStateGet_VBLTimestampingMode()< 0) printf("PTB-INFO: Beamposition queries are supported, but disabled. Using basic timestamping as fallback: Timestamps returned by Screen('Flip') will be less robust and accurate.\n");
          }
      }
      else {
		  if ((PsychPrefStateGet_VBLTimestampingMode()==4) && !((*windowRecord)->specialflags & kPsychOpenMLDefective)) {
			  printf("PTB-INFO: Will try to use OS-Builtin %s for accurate Flip timestamping.\n", (PSYCH_SYSTEM == PSYCH_LINUX) ? "OpenML sync control support" : "method");
		  }
		  else if ((PsychPrefStateGet_VBLTimestampingMode()==1 || PsychPrefStateGet_VBLTimestampingMode()==3) && (PSYCH_SYSTEM == PSYCH_OSX || ((PSYCH_SYSTEM == PSYCH_LINUX) && !((*windowRecord)->specialflags & kPsychOpenMLDefective)))) {
              if (PSYCH_SYSTEM == PSYCH_OSX || PSYCH_SYSTEM == PSYCH_LINUX) printf("PTB-INFO: Beamposition queries unsupported on this system. Will try to use kernel-level vbl interrupts as fallback.\n");
          }
          else {
              printf("PTB-INFO: Beamposition queries unsupported or defective on this system. Using basic timestamping as fallback: Timestamps returned by Screen('Flip') will be less robust and accurate.\n");
          }
      }	  
      printf("PTB-INFO: Measured monitor refresh interval from VBLsync = %f ms [%f Hz]. (%i valid samples taken, stddev=%f ms.)\n",
	     ifi_estimate * 1000, 1/ifi_estimate, numSamples, stddev*1000);
      if (ifi_nominal > 0) printf("PTB-INFO: Reported monitor refresh interval from operating system = %f ms [%f Hz].\n", ifi_nominal * 1000, 1/ifi_nominal);
      printf("PTB-INFO: Small deviations between reported values are normal and no reason to worry.\n");

      if ((*windowRecord)->stereomode==kPsychOpenGLStereo) printf("PTB-INFO: Stereo display via OpenGL built-in frame-sequential stereo requested.\n");
      if ((*windowRecord)->stereomode==kPsychCompressedTLBRStereo) printf("PTB-INFO: Stereo display via vertical image compression enabled (Top=LeftEye, Bot.=RightEye).\n");
      if ((*windowRecord)->stereomode==kPsychCompressedTRBLStereo) printf("PTB-INFO: Stereo display via vertical image compression enabled (Top=RightEye, Bot.=LeftEye).\n");
      if ((*windowRecord)->stereomode==kPsychFreeFusionStereo) printf("PTB-INFO: Stereo for free fusion or dual-display desktop spanning enabled (2-in-1 stereo).\n");
      if ((*windowRecord)->stereomode==kPsychFreeCrossFusionStereo) printf("PTB-INFO: Stereo via free cross-fusion enabled (2-in-1 stereo).\n");
      if ((*windowRecord)->stereomode==kPsychAnaglyphRGStereo) printf("PTB-INFO: Stereo display via Anaglyph Red-Green stereo enabled.\n");
      if ((*windowRecord)->stereomode==kPsychAnaglyphGRStereo) printf("PTB-INFO: Stereo display via Anaglyph Green-Red stereo enabled.\n");
      if ((*windowRecord)->stereomode==kPsychAnaglyphRBStereo) printf("PTB-INFO: Stereo display via Anaglyph Red-Blue stereo enabled.\n");
      if ((*windowRecord)->stereomode==kPsychAnaglyphBRStereo) printf("PTB-INFO: Stereo display via Anaglyph Blue-Red stereo enabled.\n");
      if ((*windowRecord)->stereomode==kPsychDualWindowStereo) printf("PTB-INFO: Stereo display via dual window output with imaging pipeline enabled.\n");
      if ((*windowRecord)->stereomode==kPsychFrameSequentialStereo) printf("PTB-INFO: Stereo display via non-native frame-sequential stereo method enabled.\n");
      if ((PsychPrefStateGet_ConserveVRAM() & kPsychDontCacheTextures) && (strstr((char*) glGetString(GL_EXTENSIONS), "GL_APPLE_client_storage")==NULL)) {
		// User wants us to use client storage, but client storage is unavailable :(
		printf("PTB-WARNING: You asked me for reducing VRAM consumption but for this, your graphics hardware would need\n");
		printf("PTB-WARNING: to support the GL_APPLE_client_storage extension, which it doesn't! Sorry... :(\n");
      }
      if (PsychPrefStateGet_3DGfx() > 0) printf("PTB-INFO: Support for OpenGL 3D graphics rendering enabled: 24 bit depth-buffer and 8 bit stencil buffer attached.\n");
      if (PsychPrefStateGet_3DGfx() & 2) printf("PTB-INFO: Additional accumulation buffer for OpenGL 3D graphics rendering attached.\n");
	  
      if (multiSample>0) {
	if ((*windowRecord)->multiSample >= multiSample) {
	  printf("PTB-INFO: Anti-Aliasing with %i samples per pixel enabled.\n", (*windowRecord)->multiSample);
	}
	if ((*windowRecord)->multiSample < multiSample && (*windowRecord)->multiSample>0) {
	  printf("PTB-WARNING: Anti-Aliasing with %i samples per pixel enabled. Requested value of %i not supported by hardware.\n",
		 (*windowRecord)->multiSample, multiSample);
	}
	if ((*windowRecord)->multiSample <= 0) {
	  printf("PTB-WARNING: Could not enable Anti-Aliasing as requested. Your hardware does not support this feature!\n");
	}
      }
      else {
	// Multisampling enabled by external code, e.g., operating system override on M$-Windows?
	if ((*windowRecord)->multiSample > 0) {
	  // Report this, so user is aware of possible issues reg. performance and stimulus properties:
	  printf("PTB-WARNING: Anti-Aliasing with %i samples per pixel enabled, contrary to Psychtoolboxs request\n", (*windowRecord)->multiSample);                        
	  printf("PTB-WARNING: for non Anti-Aliased drawing! This will reduce drawing performance and will affect\n");                        
	  printf("PTB-WARNING: low-level properties of your visual stimuli! Check your display settings for a way\n");                        
	  printf("PTB-WARNING: to disable this behaviour if you don't like it. I will try to forcefully disable it now,\n");                        
	  printf("PTB-WARNING: but have no way to check if disabling it worked.\n");                        
	}
      }
    }
    
    // Final master-setup for multisampling:
    if (multiSample>0) {
      // Try to enable multisampling in software:
      while(glGetError()!=GL_NO_ERROR);
      glEnable(0x809D); // 0x809D == GL_MULTISAMPLE_ARB
      while(glGetError()!=GL_NO_ERROR);
      // Set sampling algorithm to the most high-quality one, even if it is
      // computationally more expensive: This will only work if the NVidia
      // GL_NV_multisample_filter_hint extension is supported...
      glHint(0x8534, GL_NICEST); // Set MULTISAMPLE_FILTER_HINT_NV (0x8534) to NICEST.
      while(glGetError()!=GL_NO_ERROR);
    }
    else {
      // Try to disable multisampling in software. That is the best we can do here:
      while(glGetError()!=GL_NO_ERROR);
      glDisable(0x809D);
      while(glGetError()!=GL_NO_ERROR);
    }
    
	// Master override: If context isolation is disabled then we use the PTB internal context...
	if ((conserveVRAM & kPsychDisableContextIsolation) && (PsychPrefStateGet_Verbosity()>1)) {
		printf("PTB-WARNING: You disabled OpenGL context isolation. This will increase the probability of cross-talk between\n");
		printf("PTB-WARNING: Psychtoolbox and Matlab-OpenGL code. Only use this switch to work around broken graphics drivers,\n");
		printf("PTB-WARNING: try if a driver update would be a more sane option.\n");
	}

    // Autodetect and setup type of texture extension to use for high-perf texture mapping:
    PsychDetectTextureTarget(*windowRecord);

	if ((PsychPrefStateGet_Verbosity() > 1) && PsychIsMSVista() && PsychOSIsDWMEnabled()) {
		printf("PTB-WARNING: ============================================================================================================================\n");
		printf("PTB-WARNING: WINDOWS DWM AERO DESKTOP COMPOSITOR IS ACTIVE! ALL FLIP STIMULUS ONSET TIMESTAMPS WILL BE GROSSLY INACCURATE AND UNRELIABLE!\n");
		printf("PTB-WARNING: DO NOT USE THIS MODE FOR RUNNING EXPERIMENT SESSIONS WITH ANY REQUIREMENTS FOR ACCURATE TIMING!\n");
		printf("PTB-WARNING: ============================================================================================================================\n");
	}
	
    if (skip_synctests < 2) {
      // Reliable estimate? These are our minimum requirements...
      if (numSamples< minSamples || stddev> maxStddev) {
		  sync_disaster = true;
		  if(PsychPrefStateGet_Verbosity()>1)
			  printf("\nWARNING: Couldn't compute a reliable estimate of monitor refresh interval! Trouble with VBL syncing?!?\n");
      }
      
      // Check for mismatch between measured ifi from glFinish() VBLSync method and the value reported by the OS, if any:
      // This would indicate that we have massive trouble syncing to the VBL!
      if ((ifi_nominal > 0) && (ifi_estimate < (1 - maxDeviation) * ifi_nominal || ifi_estimate > (1 + maxDeviation) * ifi_nominal)) {
        if(PsychPrefStateGet_Verbosity()>1)
	  printf("\nWARNING: Mismatch between measured monitor refresh interval and interval reported by operating system.\nThis indicates massive problems with VBL sync.\n");    
        sync_disaster = true;
      }
    
      // Another check for proper VBL syncing: We only accept monitor refresh intervals between 20 Hz and 250 Hz.
      // Lower- / higher values probably indicate sync-trouble...
      if (ifi_estimate < 0.004 || ifi_estimate > 0.050) {
        if(PsychPrefStateGet_Verbosity()>1)
	  printf("\nWARNING: Measured monitor refresh interval indicates a display refresh of less than 20 Hz or more than 250 Hz?!?\nThis indicates massive problems with VBL sync.\n");    
        sync_disaster = true;        
      }
    } // End of synctests part II.
    
    // This is a "last resort" fallback: If user requests to *skip* all sync-tests and calibration routines
    // and we are unable to compute any ifi_estimate, we will fake one in order to be able to continue.
    // Either we use the nominal framerate provided by the operating system, or - if that's unavailable as well -
    // we assume a monitor refresh of 60 Hz, the typical value for flat-panels.
    if (ifi_estimate==0 && skip_synctests) {
      ifi_estimate = (ifi_nominal>0) ? ifi_nominal : (1.0/60.0);
      (*windowRecord)->nrIFISamples=1;
      (*windowRecord)->IFIRunningSum=ifi_estimate;
      (*windowRecord)->VideoRefreshInterval = ifi_estimate;
      if(PsychPrefStateGet_Verbosity()>1) {
	if (skip_synctests < 2) {
	  printf("\nPTB-WARNING: Unable to measure monitor refresh interval! Using a fake value of %f milliseconds.\n", ifi_estimate*1000);
	}
	else {
	  printf("PTB-INFO: All display tests and calibrations disabled. Assuming a refresh interval of %f Hz. Timing will be inaccurate!\n", 1.0/ifi_estimate);
	}
      }
    }
    
    if (sync_disaster) {
		// We fail! Continuing would be too dangerous without a working VBL sync. We don't
		// want to spoil somebodys study just because s(he) is relying on a non-working sync.
		if(PsychPrefStateGet_Verbosity()>0){		
			printf("\n\n");
			printf("----- ! PTB - ERROR: SYNCHRONIZATION FAILURE ! ----\n\n");
			printf("One or more internal checks (see Warnings above) indicate that synchronization\n");
			printf("of Psychtoolbox to the vertical retrace (VBL) is not working on your setup.\n\n");
			printf("This will seriously impair proper stimulus presentation and stimulus presentation timing!\n");
			printf("Please read 'help SyncTrouble' for information about how to solve or work-around the problem.\n");
			printf("You can force Psychtoolbox to continue, despite the severe problems, by adding the command\n");
			printf("Screen('Preference', 'SkipSyncTests', 1); at the top of your script, if you really know what you are doing.\n\n\n");
		}
		
		// Abort right here if sync tests are enabled:
		if (!skip_synctests) {
			// We abort! Close the onscreen window:
			PsychOSCloseWindow(*windowRecord);
			// Free the windowRecord:
			FreeWindowRecordFromPntr(*windowRecord);
			// Done. Return failure:
			return(FALSE);
		}
		
		// Flash our visual warning bell at alert-level for 1 second if skipping sync tests is requested:
		PsychVisualBell((*windowRecord), 1, 2);
    }
    
    // Ok, basic syncing to VBL via CGLFlushDrawable + glFinish seems to work and we have a valid
    // estimate of monitor refresh interval...
    
    // Check for mismatch between measured ifi from beamposition and from glFinish() VBLSync method.
    // This would indicate that the beam position is reported from a different display device
    // than the one we are VBL syncing to. -> Trouble!
    if ((ifi_beamestimate < 0.8 * ifi_estimate || ifi_beamestimate > 1.2 * ifi_estimate) && (ifi_beamestimate > 0)) {
        if(PsychPrefStateGet_Verbosity()>1)
	  printf("\nWARNING: Mismatch between measured monitor refresh intervals! This indicates problems with rasterbeam position queries.\n");    
        sync_trouble = true;
    }

    if (sync_trouble) {
        // Fail-Safe: Mark VBL-Endline as invalid, so a couple of mechanisms get disabled in Screen('Flip') aka PsychFlipWindowBuffers().
        VBL_Endline = -1;
		if(PsychPrefStateGet_Verbosity()>1){		
			printf("\n\n");
			printf("----- ! PTB - WARNING: SYNCHRONIZATION TROUBLE ! ----\n\n");
			printf("One or more internal checks (see Warnings above) indicate that\n");
			printf("queries of rasterbeam position are not properly working for your setup.\n\n");
			printf("Psychtoolbox will work around this by using a different timing algorithm, \n");
			printf("but it will cause Screen('Flip') to report less accurate/robust timestamps\n");
			printf("for stimulus timing.\n");
			printf("Read 'help BeampositionQueries' for more info and troubleshooting tips.\n");
			printf("\n\n");
			// Flash our visual warning bell:
			if (ringTheBell<2) ringTheBell=2;
		}
    }

    // Assign our best estimate of the scanline which marks end of vertical blanking interval:
    (*windowRecord)->VBL_Endline = VBL_Endline;
	// Store estimated video refresh cycle from beamposition method as well:
    (*windowRecord)->ifi_beamestimate = ifi_beamestimate;
    //mark the contents of the window record as valid.  Between the time it is created (always with PsychCreateWindowRecord) and when it is marked valid 
    //(with PsychSetWindowRecordValid) it is a potential victim of PsychPurgeInvalidWindows.  
    PsychSetWindowRecordValid(*windowRecord);

    // Ring the visual bell for one second if anything demands this:
    if (ringTheBell>=0 && !skip_synctests) PsychVisualBell((*windowRecord), 1, ringTheBell);

    if (PsychPrefStateGet_EmulateOldPTB()) {
        // Perform all drawing and reading in the front-buffer for OS-9 emulation mode:
        glReadBuffer(GL_FRONT);
        glDrawBuffer(GL_FRONT);
    }

	// Check if 10 bpc native framebuffer support is requested, or if 10 bit LUT bypass
    // is requested. In both cases we execute PsychEnableNative10BitFramebuffer(), which
    // will internally sort out if it needs to go through all the moves or only enable the
    // 10 bit LUT bypass (possibly on FireGL and FirePro with broken drivers):
	if ((((*windowRecord)->specialflags & kPsychNative10bpcFBActive) || (PsychPrefStateGet_ConserveVRAM() & kPsychBypassLUTFor10BitFramebuffer))
        && PsychOSIsKernelDriverAvailable((*windowRecord)->screenNumber)) {
		// Try to switch framebuffer to native 10 bpc mode:
		PsychEnableNative10BitFramebuffer((*windowRecord), TRUE);
	}

    // Allocate and zero-init the flipInfo struct for this window:
    (*windowRecord)->flipInfo = (PsychFlipInfoStruct*) malloc(sizeof(PsychFlipInfoStruct));
    if (NULL == (*windowRecord)->flipInfo) PsychErrorExitMsg(PsychError_outofMemory, "Out of memory when trying to malloc() flipInfo struct!");
    memset((*windowRecord)->flipInfo, 0, sizeof(PsychFlipInfoStruct));

	// Wait for splashMinDurationSecs, so that the "Welcome" splash screen is
	// displayed at least that long:
	PsychYieldIntervalSeconds(splashMinDurationSecs);

    // Done.
    return(TRUE);
}


/*
    PsychOpenOffscreenWindow()
    
    Accept specifications for the offscreen window in the platform-neutral structures, convert to native OpenGL structures,
    create the texture, allocate a window record and record the window specifications and memory location there.
    TO DO:  We need to walk down the screen number and fill in the correct value for the benefit of TexturizeOffscreenWindow
*/
psych_bool PsychOpenOffscreenWindow(double *rect, int depth, PsychWindowRecordType **windowRecord)
{
    // This is a complete no-op as everything is implemented in SCREENOpenOffscreenWindow at the moment.
    return(TRUE);
    
    //    return(PsychOSOpenOffscreenWindow(rect, depth, windowRecord));
}


void PsychCloseWindow(PsychWindowRecordType *windowRecord)
{
    PsychWindowRecordType	**windowRecordArray;
    int                         i, numWindows; 
    int                         queryState;
    
	// Extra child-protection to protect against half-initialized windowRecords...
	if (!windowRecord->isValid) {
		if (PsychPrefStateGet_Verbosity()>5) {
			printf("PTB-ERROR: Tried to destroy invalid windowRecord. Screw up in init sequence?!? Skipped.\n");
			fflush(NULL);
		}
		
		return;
	}
	
	// If our to-be-destroyed windowRecord is currently bound as drawing target,
	// e.g. as onscreen window or offscreen window, then we need to safe-reset
	// our drawing engine - Unbind its FBO (if any) and reset current target to
	// 'none'.
	if (PsychGetDrawingTarget() == windowRecord) {
		if (PsychIsOnscreenWindow(windowRecord)) {
			// Onscreen window? Do a simple soft-reset:
			PsychSetDrawingTarget((PsychWindowRecordType*) 0x1);
		}
		else {
			// Offscreen window/texture: Protect against some corner case. Reset
			// the drawing target to the associated top-level parent onscreen window:
			PsychSetDrawingTarget(PsychGetParentWindow(windowRecord));
		}
	}
	
	if(PsychIsOnscreenWindow(windowRecord)){
				// Call cleanup routine for the flipInfo record (and possible associated threads):
				// This must be first in order to not get caught in infinite loops if this is
				// a window close due to error-abort or other abort with async flips active:
				PsychReleaseFlipInfoStruct(windowRecord);

				// Check if 10 bpc native framebuffer support was supposed to be enabled:
				if (((windowRecord->specialflags & kPsychNative10bpcFBActive) || (PsychPrefStateGet_ConserveVRAM() & kPsychBypassLUTFor10BitFramebuffer))
				    && PsychOSIsKernelDriverAvailable(windowRecord->screenNumber)) {
					// Try to switch framebuffer back to standard 8 bpc mode. This will silently
					// do nothing if framebuffer wasn't in non-8bpc mode:
					PsychEnableNative10BitFramebuffer(windowRecord, FALSE);
				}

                // Free possible shadow textures:
                PsychFreeTextureForWindowRecord(windowRecord);        
                
                // Make sure that OpenGL pipeline is done & idle for this window:
                PsychSetGLContext(windowRecord);
				
				// Execute hook chain for OpenGL related shutdown:
				PsychPipelineExecuteHook(windowRecord, kPsychCloseWindowPreGLShutdown, NULL, NULL, FALSE, FALSE, NULL, NULL, NULL, NULL);

				// Sync and idle the pipeline:
                glFinish();
                
				// Shutdown only OpenGL related parts of imaging pipeline for this windowRecord, i.e.
				// do the shutdown work which still requires a fully functional OpenGL context and
				// hook-chains:
				PsychShutdownImagingPipeline(windowRecord, TRUE);
				
				// Call cleanup routine of text renderers to cleanup anything text related for this windowRecord:
				PsychCleanupTextRenderer(windowRecord);

		// Destroy a potentially orphaned GPU rendertime query:
		if (windowRecord->gpuRenderTimeQuery) {
			glGetQueryiv(GL_TIME_ELAPSED_EXT, GL_CURRENT_QUERY, &queryState);
			if (queryState > 0) glEndQuery(GL_TIME_ELAPSED_EXT);
			glDeleteQueries(1, &windowRecord->gpuRenderTimeQuery);
			windowRecord->gpuRenderTimeQuery = 0;
		}

				// Sync and idle the pipeline again:
                glFinish();

                // We need to NULL-out all references to the - now destroyed - OpenGL context:
                PsychCreateVolatileWindowRecordPointerList(&numWindows, &windowRecordArray);
                for(i=0;i<numWindows;i++) {
                    if (windowRecordArray[i]->targetSpecific.contextObject == windowRecord->targetSpecific.contextObject &&
                        (windowRecordArray[i]->windowType==kPsychTexture || windowRecordArray[i]->windowType==kPsychProxyWindow)) {
                        windowRecordArray[i]->targetSpecific.contextObject = NULL;
						windowRecordArray[i]->targetSpecific.glusercontextObject = NULL;
                    }
                }
                PsychDestroyVolatileWindowRecordPointerList(windowRecordArray);

                // Disable rendering context:
                PsychOSUnsetGLContext(windowRecord);

				// Call OS specific low-level window close routine:
				PsychOSCloseWindow(windowRecord);

                windowRecord->targetSpecific.contextObject=NULL;

				// Execute hook chain for final non-OpenGL related shutdown:
				PsychPipelineExecuteHook(windowRecord, kPsychCloseWindowPostGLShutdown, NULL, NULL, FALSE, FALSE, NULL, NULL, NULL, NULL);
				
				// If this was the last onscreen window then we reset the currentRendertarget etc. to pre-Screen load time:
				if (PsychIsLastOnscreenWindow(windowRecord)) {
					currentRendertarget = NULL;
					asyncFlipOpsActive = 0;
				}
    }
    else if(windowRecord->windowType==kPsychTexture) {
                // Texture or Offscreen window - which is also just a form of texture.
				PsychFreeTextureForWindowRecord(windowRecord);

				// Shutdown only OpenGL related parts of imaging pipeline for this windowRecord, i.e.
				// do the shutdown work which still requires a fully functional OpenGL context and
				// hook-chains:
				PsychShutdownImagingPipeline(windowRecord, TRUE);
    }
    else if(windowRecord->windowType==kPsychProxyWindow) {
				// Proxy window object without associated OpenGL state or content.
				// Run shutdown sequence for imaging pipeline in case the proxy has bounce-buffer or
				// lookup table textures or FBO's attached:
				PsychShutdownImagingPipeline(windowRecord, TRUE);
    }
    else if(windowRecord->windowType==kPsychNoWindow) {
				// Partially initialized windowRecord, not yet associated to a real Window system
				// window or OpenGL rendering context. We skip this one - there's nothing to do.
				// Well almost... ...we output some warning, as something must have screwed up seriously if
				// we reached this point in control-flow...
				printf("PTB-ERROR: Something is screwed up seriously! Please read all warnings and error messages\n");
				printf("PTB-ERROR: above these lines very carefully to assess and fix the problem...\n");
				fflush(NULL);
				return;
    }
    else {
                // If we reach this point then we've really screwed up, e.g., internal memory corruption.
				PsychErrorExitMsg(PsychError_internal, "FATAL ERROR: Unrecognized window type. Memory corruption?!?");
    }
    
	// Output count of missed deadlines. Don't bother for 1 missed deadline -- that's an expected artifact of the measurement...
    if (PsychIsOnscreenWindow(windowRecord) && (windowRecord->nr_missed_deadlines>1)) {
		if(PsychPrefStateGet_Verbosity() > 2) {
			printf("\n\nINFO: PTB's Screen('Flip', %i) command seems to have missed the requested stimulus presentation deadline\n", windowRecord->windowIndex);
			printf("INFO: a total of %i times out of a total of %i flips during this session.\n\n", windowRecord->nr_missed_deadlines, windowRecord->flipCount);
			printf("INFO: This number is fairly accurate (and indicative of real timing problems in your own code or your system)\n");
			printf("INFO: if you provided requested stimulus onset times with the 'when' argument of Screen('Flip', window [, when]);\n");
			printf("INFO: If you called Screen('Flip', window); without the 'when' argument, this count is more of a ''mild'' indicator\n");
			printf("INFO: of timing behaviour than a hard reliable measurement. Large numbers may indicate problems and should at least\n");
			printf("INFO: deserve your closer attention. Cfe. 'help SyncTrouble', the FAQ section at www.psychtoolbox.org and the\n");
			printf("INFO: examples in the PDF presentation in PsychDocumentation/Psychtoolbox3-Slides.pdf for more info and timing tips.\n\n");
		}
    }
    
    if (PsychIsOnscreenWindow(windowRecord) && PsychPrefStateGet_SkipSyncTests()) {
        if(PsychPrefStateGet_Verbosity()>1){
			printf("\n\nWARNING: This session of your experiment was run by you with the setting Screen('Preference', 'SkipSyncTests', %i).\n",
			       (int) PsychPrefStateGet_SkipSyncTests());
			printf("WARNING: This means that some internal self-tests and calibrations were skipped. Your stimulus presentation timing\n");
			printf("WARNING: may have been wrong. This is fine for development and debugging of your experiment, but for running the real\n");
			printf("WARNING: study, please make sure to set Screen('Preference', 'SkipSyncTests', 0) for maximum accuracy and reliability.\n");
		}
    }

	// Shutdown non-OpenGL related parts of imaging pipeline for this windowRecord:
	PsychShutdownImagingPipeline(windowRecord, FALSE);

    PsychErrorExit(FreeWindowRecordFromPntr(windowRecord));
}


/*
    PsychFlushGL()
    
    Enforce rendering of all pending OpenGL drawing commands and wait for render completion.
    This routine is called at the end of each Screen drawing subfunction. A call to it signals
    the end of a single Matlab drawing command.
 
    -If this is an onscreen window in OS-9 emulation mode we call glFinish();

    -In all other cases we don't do anything because CGLFlushDrawable which is called by PsychFlipWindowBuffers()
    implicitley calls glFlush() before flipping the buffers. Apple warns of lowered perfomance if glFlush() is called 
    immediately before CGLFlushDrawable().
 
    Note that a glFinish() after each drawing command can significantly impair overall drawing performance and
    execution speed of Matlab Psychtoolbox scripts, because the parallelism between CPU and GPU breaks down completely
    and we can run out of DMA command buffers, effectively stalling the CPU!
 
    We need to do this to provide backward compatibility for old PTB code from OS-9 or Win PTB, where
    synchronization to VBL is done via WaitBlanking: After a WaitBlanking, all drawing commands must execute as soon
    as possible and a GetSecs - call after *any* drawing command must return a useful timestamp for stimulus onset
    time. We can only achieve a compatible semantic by using a glFinish() after each drawing op.
*/
void PsychFlushGL(PsychWindowRecordType *windowRecord)
{
    if(PsychIsOnscreenWindow(windowRecord) && PsychPrefStateGet_EmulateOldPTB()) glFinish();            
}

#if PSYCH_SYSTEM == PSYCH_WINDOWS
#define strerror(x) "UNKNOWN"
#endif

/* PsychReleaseFlipInfoStruct() -- Cleanup flipInfo struct
 *
 * This routine cleans up the flipInfo struct field of onscreen window records at
 * onscreen window close time (called from PsychCloseWindow() for onscreen windows).
 * It also performs all neccessary thread shutdown and release actions if a async
 * thread is associated with the windowRecord.
 *
 */
void PsychReleaseFlipInfoStruct(PsychWindowRecordType *windowRecord)
{
	PsychFlipInfoStruct* flipRequest = windowRecord->flipInfo;
	int rc;
	static unsigned int recursionlevel = 0;
	
	// Nothing to do for NULL structs:
	if (NULL == flipRequest) return;
	
	// Any async flips in progress?
	if (flipRequest->asyncstate != 0) {
		// Hmm, what to do?
		printf("PTB-WARNING: Asynchronous flip operation for window %p in progress while Screen('Close') or Screen('CloseAll') was called or\n", windowRecord);
		printf("PTB-WARNING: exiting from a Screen error! Will try to finalize it gracefully. This may hang, crash or go into an infinite loop...\n");
		fflush(NULL);
		
		// A value of 2 would mean its basically done, so nothing to do here.
		if (flipRequest->asyncstate == 1) {
			// If no recursion and flipper thread not in error state it might be safe to try a normal shutdown:
			if (recursionlevel == 0 && flipRequest->flipperState < 4) {
				// Operation in progress: Try to stop it the normal way...
				flipRequest->opmode = 2;
				recursionlevel++;
				PsychFlipWindowBuffersIndirect(windowRecord);
				recursionlevel--;
			}
			else {
				// We seem to be in an infinite error loop. Try to force asyncstate to zero
				// in the hope that we'll break out of the loop that way and hope for the best...
				printf("PTB-WARNING: Infinite loop detected. Trying to break out in a cruel way. This may hang, crash or go into another infinite loop...\n");
				fflush(NULL);
				flipRequest->asyncstate = 0;

				// Decrement the asyncFlipOpsActive count:
				asyncFlipOpsActive--;
			}
		}
	}
	
	// Any threads attached?
	if (flipRequest->flipperThread) {
		// Yes. Cancel and destroy / release it, also release all mutex locks:

		// Disable realtime scheduling, e.g., Vista-MMCSS: Important to do this,
		// as at least Vista et al. does not reset MMCSS scheduling, even if the
		// thread dies later on, causing wreakage for all future calls of this
		// function in a running session! (WTF?!?)
		PsychSetThreadPriority(&(flipRequest->flipperThread), 0, 0);

		// Set opmode to "terminate please":
		flipRequest->opmode = -1;

		// Signal the thread in case it is waiting on the condition variable:
		if ((rc=PsychSignalCondition(&(flipRequest->flipperGoGoGo)))) {
			printf("PTB-ERROR: In PsychReleaseFlipInfoStruct(): pthread_cond_signal in thread shutdown operation failed  [%s].\n", strerror(rc));
			printf("PTB-ERROR: This must not ever happen! PTB design bug or severe operating system or runtime environment malfunction!! Memory corruption?!?");

			// Anyway, just hope it is not fatal for us...
			// Try to cancel the thread in a more cruel manner. That's the best we can do.
			PsychAbortThread(&(flipRequest->flipperThread));
		}

		// Do we hold the mutex? Given a flipperThread exist if we made it until here,
		// at least one async flip has happened in this session, which means that we do
		// hold the lock after an async flip has finalized (2) and/or Screen('AsyncFlipEnd/Check')
		// has gathered the data from the finalized flip and reset to synchronous mode (0). In
		// any of these cases, the flipper thread is sleeping on its condition variable, waiting
		// for new instructions and we have the lock. Check for asyncstate 2 or 0 to decide
		// if we need to release the lock:
		if ((flipRequest->asyncstate == 0) || (flipRequest->asyncstate == 2)) {
			// Unlock the lock, so the thread can't block on it:
			PsychUnlockMutex(&(flipRequest->performFlipLock));
		}
		
		// Thread should wake up on the signal/condition now, reaquire the lock, parse
		// our opmode = -1 abort command and therefore release the lock and terminate.

		// Wait for thread to stop and die:
		if (PsychPrefStateGet_Verbosity()>5) printf("PTB-DEBUG: Waiting (join()ing) for helper thread of window %p to finish up. If this doesn't happen quickly, you'll have to kill Matlab/Octave...\n", windowRecord);

		// If any error happened here, it wouldn't be a problem for us...
		// Wait for thread termination, cleanup and release the thread:
		PsychDeleteThread(&(flipRequest->flipperThread));
		
		// Ok, thread is dead. Mark it as such:
		flipRequest->flipperThread = (psych_thread) NULL;

		// Destroy the mutex:
		if ((rc=PsychDestroyMutex(&(flipRequest->performFlipLock)))) {
			printf("PTB-WARNING: In PsychReleaseFlipInfoStruct: Could not destroy performFlipLock mutex lock [%s].\n", strerror(rc));
			printf("PTB-WARNING: This will cause ressource leakage. Maybe you should better exit and restart Matlab/Octave?");
		}
		
		// Destroy condition variable:
		if ((rc=PsychDestroyCondition(&(flipRequest->flipperGoGoGo)))) {
			printf("PTB-WARNING: In PsychReleaseFlipInfoStruct: Could not destroy flipperGoGoGo condition variable [%s].\n", strerror(rc));
			printf("PTB-WARNING: This will cause ressource leakage. Maybe you should better exit and restart Matlab/Octave?");
		}
		
		// At this point, the thread and all other async flip resources have been terminated and released.
	}

	// Release struct:
	free(flipRequest);
	windowRecord->flipInfo = NULL;
	
	// Done.
	return;
}

/* PsychFlipperThreadMain() the "main()" routine of the asynchronous flip worker thread:
 *
 * This routine implements an infinite loop (well, infinite until cancellation at Screen('Close')
 * time etc.). The loop waits for a trigger signal from the PTB/Matlab/Octave main thread that
 * an async flip for the associated onscreen window is requested. Then it processes that request
 * by calling the underlying flip routine properly, returns all return values in the flipRequest
 * struct and goes to sleep again to wait for the next request.
 *
 * Each onscreen window has its own thread, but threads are created lazily at first invokation of
 * an async fip for a window, so most users will never ever have any of these beasts running.
 * The threads are destroyed at Screen('Close', window); Screen('CloseAl') or Screen errorhandling/
 * clear Screen / Matlab exit time.
 *
 * While an async flip is active for an onscreen window, the worker thread exclusively owns the
 * OpenGL context of that window and the main thread is prevented from executing any OpenGL related
 * commands. This is important because while many OpenGL contexts are allowed to be attached to
 * many threads in parallel, it's not allowed for one context to be attached to multiple threads!
 * We have lots of locking and protection in place to prevent such things.
 *
 * Its also important that no code from a worker thread is allowed to call back into Matlab, so
 * the imaging pipeline can not run from this thread: PsychPreflipOperations() is run fromt the main
 * thread in a fully synchronous manner, only after imaging pipe completion is control handed to the
 * worker thread. Error output or error handling from within code executed here may or may not be
 * safe in Matlab, so if one of these errors triggers, things may screw up in uncontrolled ways,
 * ending in a hang or crash of Matlab/Octave. We try to catch most common errors outside the
 * worker thread to minimize chance of this happening.
 *
 */
void* PsychFlipperThreadMain(void* windowRecordToCast)
{
	int rc;
	psych_bool needWork;
	double tnow, lastvbl;
	int dummy1;
	double dummy2, dummy3, dummy4;
    int viewid;
	psych_uint64 vblcount = 0;
	psych_uint64 vblqcount = 0;

	// Select async flip implementation: Old-Style -- One context for both master-thread and flipper-thread:
	psych_bool oldStyle = (PsychPrefStateGet_ConserveVRAM() & kPsychUseOldStyleAsyncFlips) ? TRUE : FALSE;

	// Get a handle to our info structs: These pointers must not be NULL!!!
	PsychWindowRecordType*	windowRecord = (PsychWindowRecordType*) windowRecordToCast;
	PsychFlipInfoStruct*	flipRequest	 = windowRecord->flipInfo;
	psych_bool useOpenML = (windowRecord->gfxcaps & kPsychGfxCapSupportsOpenML) ? TRUE : FALSE;
	
	// Try to lock, block until available if not available:
	if ((rc=PsychLockMutex(&(flipRequest->performFlipLock)))) {
		// This could potentially kill Matlab, as we're printing from outside the main interpreter thread.
		// Use fprintf() instead of the overloaded printf() (aka mexPrintf()) in the hope that we don't
		// wreak havoc -- maybe it goes to the system log, which should be safer...
		fprintf(stderr, "PTB-ERROR: In PsychFlipperThreadMain(): First mutex_lock in init failed  [%s].\n", strerror(rc));

		// Commit suicide with state "error, lock not held":
		flipRequest->flipperState = 5;
		return(NULL);
	}

	if (!oldStyle) {
		// We have our own dedicated OpenGL context for flip operations. Need to
		// attach to it. This attachment is permanent until the thread exits.
		// Add os-specific context binding here directly, as it is the only place
		// this is needed - not much of a point adding extra subfunctions for it
		// (yes, i know, this layering violation out of lazyness is lame!).

		#if PSYCH_SYSTEM == PSYCH_OSX
		CGLSetCurrentContext(windowRecord->targetSpecific.glswapcontextObject);
		#endif

		#if PSYCH_SYSTEM == PSYCH_LINUX
		glXMakeCurrent(windowRecord->targetSpecific.deviceContext, windowRecord->targetSpecific.windowHandle, windowRecord->targetSpecific.glswapcontextObject);
		#endif

		#if PSYCH_SYSTEM == PSYCH_WINDOWS
		wglMakeCurrent(windowRecord->targetSpecific.deviceContext, windowRecord->targetSpecific.glswapcontextObject);
		#endif

		// Enable vsync'ed bufferswaps on our private OpenGL glswapcontext:
		PsychOSSetVBLSyncLevel(windowRecord, 1);
	}
    
	// We have a special dispatch loop for our home-grown frame-sequential stereo implementation:
	if (windowRecord->stereomode != kPsychFrameSequentialStereo) {
		// Set our state as "initialized, ready & waiting":
		flipRequest->flipperState = 1;

		// Standard dispatch loop: Repeats infinitely, processing one flip request per loop iteration.
		// Well, not infinitely, but until we receive a shutdown request and terminate ourselves...
		while (TRUE) {
			// Unlock the lock and go to sleep, waiting on the condition variable for a start signal from
			// the master thread. This is an atomic operation, both unlock and sleep happen simultaneously.
			// After a wakeup due to signalling, the lock is automatically reacquired, so no need to mutex_lock
			// anymore. This is also a thread cancellation point...
			if ((rc=PsychWaitCondition(&(flipRequest->flipperGoGoGo), &(flipRequest->performFlipLock)))) {
				// Failed: Log it in a hopefully not too unsafe way:
				fprintf(stderr, "PTB-ERROR: In PsychFlipperThreadMain():  pthread_cond_wait() on flipperGoGoGo trigger failed  [%s].\n", strerror(rc));

				// Commit suicide with state "error, lock not held":
				flipRequest->flipperState = 5;

				// Make sure our thread detaches from its private OpenGL context before it dies:
				PsychOSUnsetGLContext(windowRecord);

				// Die!
				return(NULL);
			}

			// Got woken up, work to do! We have the lock from auto-reaquire in cond_wait:

			// Check if we are supposed to terminate:
			if (flipRequest->opmode == -1) {
				// We shall terminate: We are not waiting on the flipperGoGoGo variable.
				// We hold the mutex, so set us to state "terminating with lock held" and exit the loop:
				flipRequest->flipperState = 4;
				break;	
			}

			// Got the lock: Set our state to "executing - flip in progress":
			flipRequest->flipperState = 2;

			// fprintf(stdout, "WAITING UNTIL T = %f\n", flipRequest->flipwhen); fflush(NULL);

			// Setup context etc. manually, as PsychSetDrawingTarget() is a no-op when called from
			// this thread:

			// Old style method: Attach to context - It's detached in the main thread:
			if (oldStyle) PsychSetGLContext(windowRecord);

			// Setup view: We set the full backbuffer area of the window.
			PsychSetupView(windowRecord, TRUE);

			// Nothing more to do, the system backbuffer is bound, no FBO's are set at this point.

			// Unpack struct and execute synchronous flip: Synchronous in our thread, asynchronous from Matlabs/Octaves perspective!
			flipRequest->vbl_timestamp = PsychFlipWindowBuffers(windowRecord, flipRequest->multiflip, flipRequest->vbl_synclevel, flipRequest->dont_clear, flipRequest->flipwhen, &(flipRequest->beamPosAtFlip), &(flipRequest->miss_estimate), &(flipRequest->time_at_flipend), &(flipRequest->time_at_onset));

			// Flip finished and struct filled with return arguments.
			// Set our state to 3 aka "flip operation finished, ready for new commands":
			flipRequest->flipperState = 3;

			// Detach our GL context, so main interpreter thread can use it again. This will also unbind any bound FBO's.
			// As there wasn't any drawing target bound throughout our execution, and the drawingtarget was reset to
			// NULL in main thread before our invocation, there's none bound now. --> The first Screen command in
			// the main thread will rebind and setup the context and drawingtarget properly:
			if (oldStyle) {
				PsychOSUnsetGLContext(windowRecord);
			}
			else {
				// We must glFinish() here, to make sure all rendering commands submitted
				// by our OpenGL context are finished before we signal async-flip completion
				// to masterthread, ie., before we unblock a Screen('AsyncFlipEnd') etc.
				// If we omitted this, those usercode commands would no longer act as barriers
				// and we might race with userspace rendering that thinks it has exclusive access
				// to the drawing buffer:
				glFinish();

				// Need to unbind any FBO's in old context before switch, otherwise bad things can happen...
				if (glBindFramebufferEXT) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
			}

			//fprintf(stdout, "DETACHED, CONDWAIT\n"); fflush(NULL);

			// Repeat the dispatch loop. That will atomically unlock the lock and set us asleep until we
			// get triggered again with more work to do:
		}
		// Exit from standard dispatch loop.
	}
	else {
		// Frame-Sequential stereo dispatch loop: Repeats infinitely, processing one flip request or stereo buffer swap per loop iteration.
		// Well, not infinitely, but until we receive a shutdown request and terminate ourselves...

		// Set our state as "initialized and ready":
		flipRequest->flipperState = 6;

		PsychUnlockMutex(&(flipRequest->performFlipLock));
		needWork = TRUE;

		// Dispatch loop:
		while (TRUE) {
			// Do we need a new flip work item? If so, can we get the lock to check it?
			if (needWork && (PsychTryLockMutex(&(flipRequest->performFlipLock)) == 0)) {
				// Need new work and got lock.

				// Check if we are supposed to terminate:
				if (flipRequest->opmode == -1) {
					// We shall terminate: We are not waiting on the flipperGoGoGo variable.
					// We hold the mutex, so set us to state "terminating with lock held" and exit the loop:
					flipRequest->flipperState = 4;
					break;	
				}
				
				// New work available?
				if (flipRequest->flipperState == 1) {
					// Yes: We have work and we have the mutex until we are
					// done with the work.
					needWork = FALSE;
	
					// Set our state to "executing - flip in progress":
					flipRequest->flipperState = 2;
				}
				else {
					// No: Release the lock, so master has a chance to give us new work:
					PsychUnlockMutex(&(flipRequest->performFlipLock));
				}
			}

			// Are we past the deadline for pending flip work?
			PsychGetAdjustedPrecisionTimerSeconds(&tnow);

			// Update our vblank counter: On Linux and OS/X we can query the actual
			// gpu counter. On Windows, manual increments after each completed swap
			// must do as a less reliable replacement. Anyway we dynamically check this:
			PsychOSGetVBLTimeAndCount(windowRecord, &vblqcount);
			if (vblqcount > 0) vblcount = vblqcount;

			// For performing a "virtual bufferswap" we need a swaprequest to be pending, and the flipwhen deadline being
			// reached, and we need to be on the proper field -- so left-eye stims start always at even (or odd) fields,
			// at the users discretion:
			if (!needWork && (tnow >= flipRequest->flipwhen) &&
			    ((windowRecord->targetFlipFieldType == -1) || (((vblcount + 1) % 2) == (int) windowRecord->targetFlipFieldType))) {
				// Yes: Time to update the backbuffers with our finalizedFBOs and do
				// properly scheduled/timestamped bufferswaps:

				// viewid = Which eye to select for first stereo frame. 0 == Predicted stereo frame onset on
				// even vblank count -> Choose left-view buffer [0] first. 1 == Onset on odd vblank count -->
				// choose right-view buffer [1] first.
				viewid = (int) ((vblcount + 1) % 2);

				// Copy viewid-view fbo to backbuffer.
				// This sets up its viewports, texture and fbo bindings and restores them to pre-exec state:
				PsychPipelineExecuteHook(windowRecord, kPsychIdentityBlit, NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->finalizedFBO[viewid]]), NULL,
							 &(windowRecord->fboTable[0]), NULL);

				// Execute synchronous flip to make it the frontbuffer: This resets the framebuffer binding to 0 at exit:
				flipRequest->vbl_timestamp = PsychFlipWindowBuffers(windowRecord, 0, 0, 2, flipRequest->flipwhen, &(flipRequest->beamPosAtFlip),
										    &(flipRequest->miss_estimate), &(flipRequest->time_at_flipend), &(flipRequest->time_at_onset));

				// Maintain virtual vblank counter on platforms where we need it:
				vblcount++;

				// Copy non-viewid-view fbo to backbuffer.
				// This sets up its viewports, texture and fbo bindings and restores them to pre-exec state:
				PsychPipelineExecuteHook(windowRecord, kPsychIdentityBlit, NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->finalizedFBO[1-viewid]]), NULL,
							 &(windowRecord->fboTable[0]), NULL);

				// We glFinish() here, to make sure all rendering commands submitted
				// by our OpenGL context are finished. This means the finalizedFBOs are
				// "used up" for this redraw cycle and ready for refill by the masterthread:
				glFinish();
	
				// "Double-Flip" is now on its way to finish and struct is filled with return arguments.
				// The buffers now contain the new left/right view images and can be simply exchanged
				// periodically to provide a "static" frame-sequential stimulus to the observer.

				// Set our state to 3 aka "flip operation finished, ready for new commands":
				flipRequest->flipperState = 3;

				// Compute swap deadline for onset of 2nd view (right eye):
				tnow = flipRequest->flipwhen + windowRecord->VideoRefreshInterval;

				// We can release the lock already to unblock the client code on the masterthread,
				// as it already has access to all timestamps and status information and can start
				// rendering into the client framebuffers (drawBufferFBOs) already. It could even
				// already perform new preflip operations, as we're done with the finalizedFBOs:
				PsychUnlockMutex(&(flipRequest->performFlipLock));

				// Execute synchronous flip to make it the frontbuffer: This resets the framebuffer binding to 0 at exit:
				PsychFlipWindowBuffers(windowRecord, 0, 0, 2, tnow, &dummy1, &dummy2, &dummy3, &dummy4);

				// Maintain virtual vblank counter on platforms where we need it:
				vblcount++;

				// Ready to accept new work:
				needWork = TRUE;

				// Restart at beginning of dispatch while-loop:
				continue;
			}

			// Ok, either swap deadline for client triggered flip not yet close,
			// or nothing to do from client side. Idle swap handling...

			// Get estimate of last vblank time of swap completion:
			lastvbl = windowRecord->time_at_last_vbl;

			// Estimate next vblank deadline: Safety headroom is 0.5 refresh cycles.
			lastvbl += windowRecord->VideoRefreshInterval / 2;

			// Time for a swap request?
			if (tnow >= lastvbl) {
				// Trigger a doublebuffer swap in sync with vblank:
				PsychOSFlipWindowBuffers(windowRecord);

				if (PsychPrefStateGet_Verbosity() > 10) {
					printf("PTB-DEBUG: Idle-Swap tnow = %f >= deadline = %f  delta = %f  [lastvbl = %f]\n", tnow, lastvbl, tnow - lastvbl, windowRecord->time_at_last_vbl);
				}

				// Wait for swap completion, so we get an updated vblank time estimate:
				if (!(useOpenML && (PsychOSGetSwapCompletionTimestamp(windowRecord, 0, &(windowRecord->time_at_last_vbl)) > 0))) {
					// OpenML swap completion timestamping unsupported, disabled, or failed.
					// Use our standard trick instead.
					glBegin(GL_POINTS);
					glColor4f(0, 0, 0, 0);
					glVertex2i(10, 10);
					glEnd();
					glFinish();
					PsychGetAdjustedPrecisionTimerSeconds(&(windowRecord->time_at_last_vbl));
				}

				// Maintain virtual vblank counter on platforms where we need it:
				vblcount++;
			} else {
				// Nope. Need to sleep a bit here to kill some time:
				PsychYieldIntervalSeconds(0.001);
			}

			// Next dispatch loop iteration...
		}
		// Exit from frame-sequential stereo dispatch loop.
	}

	// Exit path from thread at thread termination...

	// Make sure our thread detaches from its private OpenGL context before it dies:
	PsychOSUnsetGLContext(windowRecord);

	// Need to unlock the mutex:
	if (flipRequest->flipperState == 4) {
		if ((rc=PsychUnlockMutex(&(flipRequest->performFlipLock)))) {
			// This could potentially kill Matlab, as we're printing from outside the main interpreter thread.
			// Use fprintf() instead of the overloaded printf() (aka mexPrintf()) in the hope that we don't
			// wreak havoc -- maybe it goes to the system log, which should be safer...
			fprintf(stderr, "PTB-ERROR: In PsychFlipperThreadMain(): Last mutex_unlock in termination failed  [%s].\n", strerror(rc));

			// Commit suicide with state "error, lock not held":
			flipRequest->flipperState = 5;
			return(NULL);
		}
	}

	// Ok, we're not blocked on condition variable and we've unlocked the lock (or at least, did our best to do so),
	// and set the termination state: Go and die peacefully...
	return(NULL);
}

/*	PsychFlipWindowBuffersIndirect()
 *
 *	This is a wrapper around PsychFlipWindowBuffers(); which gets all flip request parameters
 *	passed in a struct PsychFlipInfoStruct, decodes that struct, calls the PsychFlipWindowBuffers()
 *	accordingly, then encodes the returned flip results into the struct.
 *
 *	This method not only allows synchronous flips - in which case it has the same behaviour
 *	as PsychFlipWindowBuffers(), just with struct parameters - but also asynchronous flips:
 *	In that case, the flip request is just scheduled for later async, parallel execution by
 *	a background helper thread. Another invocation allows to retrieve the results of that
 *	flip synchronously.
 *
 *	This is the preferred method of calling flips from userspace, used in SCREENFlip.c for
 *	standard Screen('Flips'), but also for async Screen('FlipAsyncStart') and Screen('FlipAsyncEnd').
 *
 *	The passed windowRecord of the onscreen window to flip must contain a PsychFlipInfoStruct*
 *	flipRequest with all neccessary info for the flip parameters and the fields in which result
 *	shall be returned, as well as the datastructures for thread/mutex/cond locking etc...
 *
 *      flipRequest->opmode can be one of:
 *	0 = Execute Synchronous flip, 1 = Start async flip, 2 = Finish async flip, 3 = Poll for finish of async flip.
 *
 *      *	Synchronous flips are performed without changing the mutex lock flipRequest->performFlipLock. We check if
 *		there are not flip ops scheduled or executing for the window, then simply execute the flip and return its
 *		results, if none are active.
 *
 *	*	Asynchronous flips are always started with the flipInfo struct setup with all needed parameters and
 *		the performFlipLock locked on triggering the worker thread: Either because we create the thread at
 *		first invocation and acquire the lock just before initial trigger, or because we are initiating a flip
 *		after a previous successfull and finished async flip -- in which case we come from there with the lock
 *		held. Also the worker thread is waiting on the flipperGoGoGo condition variable.
 *
 *	*	Async flips are finalized or polled for finalization (and then finalized on poll success) by entering with
 *		the lock not held, so we need to lock->check->unlock (in not ready yet case) or lock->check->finalize in
 *		success case - in which case we leave with the worker thread waiting on the flipperGoGoGo for new work and
 *		our performFlipLock held -- Just as we want it for the next async flip invocation.
 *
 *  More important stuff:
 *
 *      *	Code executing in the PsychFlipperThreadMain() worker thread is not allowed to print anything to the
 *		Matlab/Octave console, alloate or deallocate memory or other stuff that might interact with the runtime
 *		environment Matlab or Octave. We don't know if they are thread-safe, but assume they are not!
 *
 *	*	Error handling as well as clear Screen and Screen('Close', window) or Screen('CloseAll') all trigger
 *		PsychCloseWindow() for the onscreen window, which in turn triggers cleanup in PsychReleaseFlipInfoStruct().
 *		That routine must not only release the struct, but also make absolutely sure that our thread gets cancelled
 *		or signalled to exit and joined, then destroyed and all mutexes unlocked and destroyed!!!
 *
 *      *	The master interpreter thread must detach from the PTB internal OpenGL context for the windowRecord and
 *		not reattach until an async flip is finished! PsychSetGLContext() contains appropriate checking code:
 *		Only one thread is allowed to attach to a specific context, so we must basically lock that ressource as
 *		long as our flipperThread needs it to perform preflip,bufferswap and timestamping, postflip operations...
 *
 *      *	The userspace OpenGL context is not so critical in theory, but we protect that one as well, as it is a
 *		separate context, so no problems from the OpenGL/OS expected (multiple threads can have multiple contexts
 *		attached, as long as each context only has one thread attached), but both contexts share the same drawable
 *		and therefore the same backbuffer. That could prevent bufferswaps at requested deadline/VSYNC because some
 *		usercode rasterizes into the backbuffer and subverts our preflip operations...
 *
 *	Returns success state: TRUE on success, FALSE on error.
 *
 */
psych_bool PsychFlipWindowBuffersIndirect(PsychWindowRecordType *windowRecord)
{
	int rc;
	PsychFlipInfoStruct* flipRequest;
	
	if (NULL == windowRecord) PsychErrorExitMsg(PsychError_internal, "NULL-Ptr for windowRecord passed in PsychFlipWindowsIndirect()!!");
	
	flipRequest = windowRecord->flipInfo;
	if (NULL == flipRequest) PsychErrorExitMsg(PsychError_internal, "NULL-Ptr for 'flipRequest' field of windowRecord passed in PsychFlipWindowsIndirect()!!");

	// Synchronous flip requested?
	if ((flipRequest->opmode == 0) && (windowRecord->stereomode != kPsychFrameSequentialStereo)) {
		// Yes. Any pending operation in progress?
		if (flipRequest->asyncstate != 0) PsychErrorExitMsg(PsychError_internal, "Tried to invoke synchronous flip while flip still in progress!");
		
		// Unpack struct and execute synchronous flip:
		flipRequest->vbl_timestamp = PsychFlipWindowBuffers(windowRecord, flipRequest->multiflip, flipRequest->vbl_synclevel, flipRequest->dont_clear, flipRequest->flipwhen, &(flipRequest->beamPosAtFlip), &(flipRequest->miss_estimate), &(flipRequest->time_at_flipend), &(flipRequest->time_at_onset));

		// Call hookchain with callbacks to be performed after successfull flip completion:
		PsychPipelineExecuteHook(windowRecord, kPsychScreenFlipImpliedOperations, NULL, NULL, FALSE, FALSE, NULL, NULL, NULL, NULL);

		// Done, and all return values filled in struct. We leave asyncstate at its zero setting, ie., idle and simply return:
		return(TRUE);
	}

	// Asynchronous flip mode, either request to trigger one or request to finalize one:
	if ((flipRequest->opmode == 1) || ((flipRequest->opmode == 0) && (windowRecord->stereomode == kPsychFrameSequentialStereo))) {
		// Async flip start request, or a sync flip turned into an async flip due to kPsychFrameSequentialStereo:
		if (flipRequest->asyncstate != 0) PsychErrorExitMsg(PsychError_internal, "Tried to invoke asynchronous flip while flip still in progress!");

		// Current multiflip > 0 implementation is not thread-safe, so we don't support this:
		if (flipRequest->multiflip != 0) PsychErrorExitMsg(PsychError_user, "Using a non-zero 'multiflip' flag while starting an asynchronous flip! This is forbidden! Aborted.\n");

		if ((flipRequest->opmode == 0) && (PsychPrefStateGet_ConserveVRAM() & kPsychUseOldStyleAsyncFlips)) {
			PsychErrorExitMsg(PsychError_user, "Tried to use frame-sequential stereo mode while Screen('Preference', 'ConserveVRAM') setting kPsychUseOldStyleAsyncFlips is set! Forbidden!");
		}

		// PsychPreflip operations are not thread-safe due to possible callbacks into Matlab interpreter thread
		// as part of hookchain processing when the imaging pipeline is enabled: We perform/trigger them here
		// before entering the async flip thread:
		PsychPreFlipOperations(windowRecord, flipRequest->dont_clear);

		// Tell Flip that pipeline - flushing has been done already to avoid redundant flush:
		windowRecord->PipelineFlushDone = TRUE;

		// ... and flush & finish the pipe:
		glFinish();

		// First time async request? Threads already set up?
		if (flipRequest->flipperThread == (psych_thread) NULL) {
			// First time init: Need to startup flipper thread:

			// printf("IN THREADCREATE\n"); fflush(NULL);

			// Create & Init the two mutexes:
			if ((rc=PsychInitMutex(&(flipRequest->performFlipLock)))) {
				printf("PTB-ERROR: In Screen('FlipAsyncBegin'): PsychFlipWindowBuffersIndirect(): Could not create performFlipLock mutex lock [%s].\n", strerror(rc));
				PsychErrorExitMsg(PsychError_system, "Insufficient system ressources for mutex creation as part of async flip setup!");
			}
			
			if ((rc=PsychInitCondition(&(flipRequest->flipperGoGoGo), NULL))) {
				printf("PTB-ERROR: In Screen('FlipAsyncBegin'): PsychFlipWindowBuffersIndirect(): Could not create flipperGoGoGo condition variable [%s].\n", strerror(rc));
				PsychErrorExitMsg(PsychError_system, "Insufficient system ressources for condition variable creation as part of async flip setup!");
			}
			
			// Set initial thread state to "inactive, not initialized at all":
			flipRequest->flipperState = 0;
			
			// Create and startup thread:
			if ((rc=PsychCreateThread(&(flipRequest->flipperThread), NULL, PsychFlipperThreadMain, (void*) windowRecord))) {
				printf("PTB-ERROR: In Screen('FlipAsyncBegin'): PsychFlipWindowBuffersIndirect(): Could not create flipper  [%s].\n", strerror(rc));
				PsychErrorExitMsg(PsychError_system, "Insufficient system ressources for mutex creation as part of async flip setup!");
			}

			// Additionally try to schedule flipperThread MMCSS: This will lift it roughly into the
			// same scheduling range as HIGH_PRIORITY_CLASS, even if we are non-admin users
			// on Vista and Windows-7 and later, however with a scheduler safety net applied.
			// For some braindead reasons, apparently only one thread can be scheduled in class 10,
			// so we need to make sure the masterthread is not MMCSS scheduled, otherwise our new
			// request will fail:
			if (PSYCH_SYSTEM == PSYCH_WINDOWS) {
				// On Windows, we have to set flipperThread to +2 RT priority levels while
				// throwing ourselves off RT priority scheduling. This is a brain-dead requirement
				// of Vista et al's MMCSS scheduler which only allows one of our threads being
				// scheduled like that :( -- Disable RT scheduling for ourselves (masterthread):
				PsychSetThreadPriority((psych_thread*) 0x1, 0, 0);
			}

			// Boost priority of flipperThread by 2 levels and switch it to RT scheduling,
			// unless it is already RT-Scheduled. As the thread inherited our scheduling
			// priority from PsychCreateThread(), we only need to +2 tweak it from there:
			// Note: On OS/X this means ultra-low latency non-preemptible operation (as we need), with up to
			// 3 msecs uninterrupted computation time out of 10 msecs if we really need it. Normally we can
			// get along with << 1 msec, but some pathetic cases of GPU driver bugs could drive it up to 3 msecs
			// in the async flipper thread:
			PsychSetThreadPriority(&(flipRequest->flipperThread), 10, 2);

			// The thread is started with flipperState == 0, ie., not "initialized and ready", the lock is unlocked.
			// First thing the thread will do is try to lock the lock, then set its flipperState to 1 == initialized and
			// ready, then init itself, then enter a wait on our flipperGoGoGo condition variable and atomically unlock
			// the lock.
			// We now need to try to acquire the lock, then - after we got it - check if we got it because we were faster
			// than the flipperThread and he didn't have a chance to get it (iff flipperState still == 0) - in which case
			// we need to release the lock, wait a bit and then retry a lock->check->sleep->unlock->... cycle. If we got it
			// because flipperState == 1 then this means the thread had the lock, initialized itself, set its state to ready
			// and went sleeping and releasing the lock (that's why we could lock it). In that case, the thread is ready to
			// do work for us and is just waiting for us. At that point we: a) Have the lock, b) can trigger the thread via
			// condition variable to do work for us. That's the condition we want and we can proceed as in the non-firsttimeinit
			// case...
			while (TRUE) {
				// Try to lock, block until available if not available:
			
				//printf("ENTERING THREADCREATEFINISHED MUTEX: MUTEX_LOCK\n"); fflush(NULL);

				if ((rc=PsychLockMutex(&(flipRequest->performFlipLock)))) {
					printf("PTB-ERROR: In Screen('FlipAsyncBegin'): PsychFlipWindowBuffersIndirect(): First mutex_lock in init failed  [%s].\n", strerror(rc));
					PsychErrorExitMsg(PsychError_system, "Internal error or deadlock avoided as part of async flip setup!");
				}
				
				//printf("ENTERING THREADCREATEFINISHED MUTEX: MUTEX_LOCKED!\n"); fflush(NULL);

				// Got it! Check condition:
				if (flipRequest->flipperState == 1 || flipRequest->flipperState == 6) {
					// Thread ready and we have the lock: Proceed...
					break;
				}

				//printf("ENTERING THREADCREATEFINISHED MUTEX: MUTEX_UNLOCK\n"); fflush(NULL);

				if ((rc=PsychUnlockMutex(&(flipRequest->performFlipLock)))) {
					printf("PTB-ERROR: In Screen('FlipAsyncBegin'): PsychFlipWindowBuffersIndirect(): First mutex_unlock in init failed  [%s].\n", strerror(rc));
					PsychErrorExitMsg(PsychError_system, "Internal error or deadlock avoided as part of async flip setup!");
				}

				//printf("ENTERING THREADCREATEFINISHED MUTEX: MUTEX_UNLOCKED\n"); fflush(NULL);

				// Thread not ready. Sleep a millisecond and repeat...
				PsychYieldIntervalSeconds(0.001);

				//printf("ENTERING THREADCREATEFINISHED MUTEX: RETRY\n"); fflush(NULL);
			}

			// End of first-time init for this windowRecord and its thread.

			// printf("FIRST TIME INIT DONE\n"); fflush(NULL);
		}
		
		// Our flipperThread is ready to do work for us (waiting on flipperGoGoGo condition variable) and
		// we have the lock on the flipRequest struct. The struct is already filled with all input parameters
		// for a flip request, so we can simply release our lock and signal the thread that it should do its
		// job:

		// printf("IN ASYNCSTART: DROP CONTEXT\n"); fflush(NULL);

		// Detach from our OpenGL context:
		// This is important even with the new-style model, because it enforces
		// rebinding of our rendering context in PsychSetGLContext() at the first
		// operation that wants to touch this windowRecord or its children.
		// Rebinding will imply a validation to make sure rebinding is allowed
		// under the current mode of operation.
		PsychSetDrawingTarget(NULL);
		PsychOSUnsetGLContext(windowRecord);
		
		// Increment the counter asyncFlipOpsActive:
		asyncFlipOpsActive++;

		// printf("IN ASYNCSTART: MUTEXUNLOCK\n"); fflush(NULL);

		// This is only needed for frame-sequential thread mode:
		flipRequest->flipperState = 1;

		// Trigger the thread:
		if ((rc=PsychSignalCondition(&(flipRequest->flipperGoGoGo)))) {
			printf("PTB-ERROR: In Screen('FlipAsyncBegin'): PsychFlipWindowBuffersIndirect(): pthread_cond_signal in trigger operation failed  [%s].\n", strerror(rc));
			PsychErrorExitMsg(PsychError_internal, "This must not ever happen! PTB design bug or severe operating system or runtime environment malfunction!! Memory corruption?!?");
		}

		// Release the lock:
		if ((rc=PsychUnlockMutex(&(flipRequest->performFlipLock)))) {
			printf("PTB-ERROR: In Screen('FlipAsyncBegin'): PsychFlipWindowBuffersIndirect(): mutex_unlock in trigger operation failed  [%s].\n", strerror(rc));
			PsychErrorExitMsg(PsychError_internal, "This must not ever happen! PTB design bug or severe operating system or runtime environment malfunction!! Memory corruption?!?");
		}

		// That's it, operation in progress: Mark it as such.
		flipRequest->asyncstate = 1;
		
		// Done, unless this wasn't a real async flip. If this was a pseudo-sync-flip,
		// we fall through to finalization stage:
		if (flipRequest->opmode == 1) {
			// Was an async-flip begin: We´re done:
			return(TRUE);
		}
		else {
			// Was a sync flip turned into an async-flip begin for our
			// frame-sequential stereo implementation. Turn this into
			// a blocking wait for async-flip end and fall-through, so
			// we get the effective semantics of a classic sync-flip,
			// just executed indirectly by the flipperthread:
			flipRequest->opmode = 2;
		}
	}
	
	// Request to wait or poll for finalization of an async flip operation:
	if ((flipRequest->opmode == 2) || (flipRequest->opmode == 3)) {
		// Child protection:
		if (flipRequest->asyncstate != 1) PsychErrorExitMsg(PsychError_internal, "Tried to invoke end of an asynchronous flip although none is in progress!");

		// We try to get the lock, then check if flip is finished. If not, we need to wait
		// a bit and retry:
		while (TRUE) {
			if (flipRequest->opmode == 2) {
				// Blocking wait:
				// Try to lock, block until available if not available:

				//printf("END: MUTEX_LOCK\n"); fflush(NULL);
				
				if ((rc=PsychLockMutex(&(flipRequest->performFlipLock)))) {
					printf("PTB-ERROR: In Screen('AsyncFlipEnd'): PsychFlipWindowBuffersIndirect(): mutex_lock in wait for finish failed  [%s].\n", strerror(rc));
					PsychErrorExitMsg(PsychError_system, "Internal error or deadlock avoided as part of async flip end!");
				}
			}
			else {
				// Polling mode:
				// Try to lock, return to caller if not available:
				if (PsychTryLockMutex(&(flipRequest->performFlipLock)) > 0) return(FALSE);
			}

			// printf("END: MUTEX_LOCKED\n"); fflush(NULL);
			
			// Got it! Check condition:
			if (flipRequest->flipperState == 3) {
				// Thread finished with flip request execution, ready for new work and waiting for a trigger and we have the lock: Proceed...
				break;
			}

			//printf("END: NOTREADY MUTEX_UNLOCK\n"); fflush(NULL);

			// Not finished. Unlock:
			if ((rc=PsychUnlockMutex(&(flipRequest->performFlipLock)))) {
				printf("PTB-ERROR: In Screen('FlipAsyncBegin'): PsychFlipWindowBuffersIndirect(): mutex_unlock in wait/poll for finish failed  [%s].\n", strerror(rc));
				PsychErrorExitMsg(PsychError_system, "Internal error or deadlock avoided as part of async flip end!");
			}

			//printf("END: NOTREADY MUTEX_UNLOCKED\n"); fflush(NULL);

			if (flipRequest->opmode == 3) {
				// Polling mode: We just exit our polling op, so user code can retry later:
				return(FALSE);
			}
			
			//printf("END: RETRY\n"); fflush(NULL);
			
			// Waiting mode, need to repeat:
			// Thread not finished. Sleep a millisecond and repeat...
			PsychYieldIntervalSeconds(0.001);
		}

		//printf("END: SUCCESS\n"); fflush(NULL);
		
		// Ok, the thread is finished and ready for new work and waiting.
		// We have the lock as well.
		
		// Reset thread state to "initialized, ready and waiting" just as if it just started at first invocation:
		flipRequest->flipperState = 1;

		// Set flip state to finished:
		flipRequest->asyncstate = 2;
		
		// Decrement the asyncFlipOpsActive count:
		asyncFlipOpsActive--;

		if (windowRecord->stereomode == kPsychFrameSequentialStereo) {
			flipRequest->asyncstate = 0;
			PsychPostFlipOperations(windowRecord, flipRequest->dont_clear);
			flipRequest->asyncstate = 2;
		}

		// Reset flags used for avoiding redundant Pipeline flushes and backbuffer-backups:
		// This flags are altered and checked by SCREENDrawingFinished() and PsychPreFlipOperations() as well:
		windowRecord->PipelineFlushDone = false;
		windowRecord->backBufferBackupDone = false;

		// Call hookchain with callbacks to be performed after successfull flip completion:
		PsychPipelineExecuteHook(windowRecord, kPsychScreenFlipImpliedOperations, NULL, NULL, FALSE, FALSE, NULL, NULL, NULL, NULL);

		// Now we are in the same condition as after first time init. The thread is waiting for new work,
		// we hold the lock so we can read out the flipRequest struct or fill it with a new request,
		// and all information from the finalized flip is available in the struct.
		// We can return to our parent function with our result:
		return(TRUE);
	}

	return(TRUE);
}

#if PSYCH_SYSTEM == PSYCH_WINDOWS
#undef strerror
#endif

/*
    PsychFlipWindowBuffers()
    
    Flip front and back buffers in sync with vertical retrace (VBL) and sync thread execution to VBL.
    Returns various timestamps related to sync to VBL, so experimenters can check proper
    syncing and presentation timing by themselves. Also contains an automatic "skipped frames"
    /"missed presentation deadline" detector, which is pretty reliable.
    Allows to not clear the framebuffer after flip, but keep it as before flip - allows
    incremental drawing of stims. Allows to flip not at next retrace, but at the retrace
    immediately after some deadline (spec'd in system time) has been reached.
    Optimizes rendering in collaboration with new SCREENDrawingFinished.c.
 
    Accepts:
    dont_clear flag: 0 = glClear after flip, 1 = restore backbuffer after flip, 2 = don't do anything.
    flipwhen: 0 = traditional flip (flip on next VBL), >0 flip at VBL immediately after time "flipwhen" has been reached.
             -1 = don't sync PTB's execution to VBL, aka sync stimulus onset to VBL but don't pause execution up to then
                  This also disables all timestamping and deadline checking code and makes synchronization of Matlabs
                  execution locked to the VBL impossible. -> Only useful for very special cases...
    
    Returns:
    double value VBL start time: Calculated time of VBL start (aka buffer swap) from timestamp, beamposition and IFI.
    beamPosAtFlip = Position of monitor scanning beam at time that timestamp was taken.
    miss_estimate = Estimate of how far we've missed our presentation deadline. >0 == No deadline-miss, < 0 == deadline-miss
    time_at_onset = Estimated time when stimulus onset, aka end of VBL, aka beamposition==0 occurs.
    time_at_flipend = Timestamp taken shortly before return of FlipWindowBuffers for benchmarking.
 
	Notes:
	
	The type of timestamping used depends on the 'VBLTimestampingMode' preference setting. Specific numbers
	select specific strategies. If a strategy is unsupported or found to be defective, a fallback strategy is
	tried and so on.
	
	Mode 4: Use OS-Builtin wait-for-swap-completion and timestamping if supported. On Linux this would be OpenML OML_sync_control.
	Mode 3: Use VBLANK IRQ timestamping - on Linux with OpenML or OS/X.
	Mode 2: Use mode 1 and 3 with consistency checking among both.
	Mode 1: Use beamposition timestamping.
	Mode 0: Use beamposition timestamping, but no VBLANK timestamping.
	Mode -1: Use raw timestamps.
	
	Fallback sequence, assuming mode 1, 2 or 4 is:

	4:   OS-Builtin (e.g., OpenML) --> Beamposition --> VBLANK --> Raw.
	3:   VBLANK --> Raw.
	2,1: Beamposition --> VBLANK --> Raw.
	0:   Beamposition --> Raw.
	-1:  Raw.

*/
double PsychFlipWindowBuffers(PsychWindowRecordType *windowRecord, int multiflip, int vbl_synclevel, int dont_clear, double flipwhen, int* beamPosAtFlip, double* miss_estimate, double* time_at_flipend, double* time_at_onset)
{
    int screenheight, screenwidth;
    GLint read_buffer, draw_buffer;
    unsigned char bufferstamp;
    const psych_bool vblsyncworkaround=false;  // Setting this to 'true' would enable some checking code. Leave it false by default.
    static unsigned char id=1;
    psych_bool sync_to_vbl;                    // Should we synchronize the CPU to vertical retrace? 
    double tremaining;                      // Remaining time to flipwhen - deadline
	double tprescheduleswap;				// Time before os specific call to swap scheduling.
    CGDirectDisplayID displayID;            // Handle for our display - needed for beampos-query.
    double time_at_vbl=0;                   // Time (in seconds) when last Flip in sync with start of VBL happened.
    double currentflipestimate;             // Estimated video flip interval in seconds at current monitor frame rate.
    double currentrefreshestimate;          // Estimated video refresh interval in seconds at current monitor frame rate.
    double tshouldflip;                     // Deadline for a successfull flip. If time_at_vbl > tshouldflip --> Deadline miss!
    double slackfactor;                     // Slack factor for deadline miss detection.
    double vbl_startline;
    double vbl_endline;
    double vbl_lines_elapsed, onset_lines_togo;
    double vbl_time_elapsed; 
    double onset_time_togo;
	psych_uint64 targetVBL;						// Target VBL count for scheduled flips with Windows Vista DWM.
    psych_uint64 preflip_vblcount = 0;          // VBL counters and timestamps acquired from low-level OS specific routines.
    psych_uint64 postflip_vblcount = 0;         // Currently only supported on OS-X, but Linux/X11 implementation will follow.
    double preflip_vbltimestamp = -1;
    double postflip_vbltimestamp = -1;
	unsigned int vbltimestampquery_retrycount = 0;
	double time_at_swaprequest=0;			// Timestamp taken immediately before requesting buffer swap. Used for consistency checks.
	double time_post_swaprequest=0;			// Timestamp taken immediately after requesting buffer swap. Used for consistency checks.
	double time_at_swapcompletion=0;		// Timestamp taken after swap completion -- initially identical to time_at_vbl.
	int line_pre_swaprequest = -1;			// Scanline of display immediately before swaprequest.
	int line_post_swaprequest = -1;			// Scanline of display immediately after swaprequest.
	int min_line_allowed = 50;				// The scanline up to which "out of VBL" swaps are accepted: A fudge factor for broken drivers...
	psych_uint64 dwmPreOnsetVBLCount, preFlipFrameId; 
	psych_uint64 dwmPostOnsetVBLCount, postFlipFrameId; 
	double dwmPreOnsetVBLTime, dwmOnsetVBLTime;
	double dwmCompositionRate = 0;
	int	   dwmrc;
	psych_bool flipcondition_satisfied;
	psych_bool osspecific_asyncflip_scheduled = FALSE;		// Set to TRUE if PsychOSScheduleFlipWindowBuffers() has been successfully called.
	psych_bool must_wait = FALSE;							// Set to TRUE if an active wait is absolutely needed, regardless of os specific flip scheduling.
	unsigned int targetSwapFlags;
	double targetWhen;			// Target time for OS-Builtin swap scheduling.
	double tSwapComplete;		// Swap completion timestamp for OS-Builtin timestamping.
	psych_int64 swap_msc;		// Swap completion vblank count for OS-Builtin timestamping.

    int vbltimestampmode = PsychPrefStateGet_VBLTimestampingMode();
    PsychWindowRecordType **windowRecordArray=NULL;
    int	i;
    int numWindows=0; 
	int verbosity;

    // Assign level of verbosity:
    verbosity = PsychPrefStateGet_Verbosity();
    
    // We force verbosity to -1 (no output at all except for critical timestamping errors) if we're executing
    // from a background thread instead of the masterthread and verbosity is smaller than 11, because printing
    // from suchthreads can potentially cause bigger problems than not seeing status output:
    verbosity = (PsychIsMasterThread() || (verbosity > 10)) ? verbosity : ((verbosity > 0) ? -1 : 0);

    // Child protection:
    if(windowRecord->windowType!=kPsychDoubleBufferOnscreen)
        PsychErrorExitMsg(PsychError_internal,"Attempt to swap a single window buffer");
    
    // Retrieve estimate of interframe flip-interval:
    if (windowRecord->nrIFISamples > 0) {
        currentflipestimate=windowRecord->IFIRunningSum / ((double) windowRecord->nrIFISamples);
    }
    else {
        // We don't have a valid estimate! This will screw up all timestamping, checking and waiting code!
        // It also indicates that syncing to VBL doesn't work!
        currentflipestimate=0;
        // We abort - This is too unsafe...
        PsychErrorExitMsg(PsychError_internal,"Flip called, while estimate of monitor flip interval is INVALID -> Syncing trouble -> Aborting!");
    }
    
    // Retrieve estimate of monitor refresh interval:
    if (windowRecord->VideoRefreshInterval > 0) {
        currentrefreshestimate = windowRecord->VideoRefreshInterval;
    }
    else {
        currentrefreshestimate=0;
        // We abort - This is too unsafe...
        PsychErrorExitMsg(PsychError_internal,"Flip called, while estimate of monitor refresh interval is INVALID -> Syncing trouble -> Aborting!");
    }
    
    // Setup reasonable slack-factor for deadline miss detector:
    if (windowRecord->VBL_Endline!=-1) {
        // If beam position queries work, we use a tight value:
        slackfactor = 1.05;
    }
    else {
        // If beam position queries don't work, we use a "slacky" value:
        slackfactor = 1.2;
    }
    
    // Retrieve display id and screen size spec that is needed later...
    PsychGetCGDisplayIDFromScreenNumber(&displayID, windowRecord->screenNumber);
    screenwidth=(int) PsychGetWidthFromRect(windowRecord->rect);
    screenheight=(int) PsychGetHeightFromRect(windowRecord->rect);
    vbl_startline = windowRecord->VBL_Startline;

    // Enable this windowRecords framebuffer as current drawingtarget: [No op on flipperthread!]
    PsychSetDrawingTarget(windowRecord);
    
    // Should we sync to the onset of vertical retrace?
    // Note: Flipping the front- and backbuffers is nearly always done in sync with VBL on
    // a double-buffered setup. sync_to_vbl specs, if the application should wait for
    // the VBL to start before continuing execution.
    sync_to_vbl = (vbl_synclevel == 0 || vbl_synclevel == 3) ? true : false;

    if (vbl_synclevel==2) {
        // We are requested to flip immediately, instead of syncing to VBL. Disable VBL-Sync.
		PsychOSSetVBLSyncLevel(windowRecord, 0);
		// Disable also for a slave window, if any. Unsupported for async flips.
		if (windowRecord->slaveWindow && PsychIsMasterThread()) PsychOSSetVBLSyncLevel(windowRecord->slaveWindow, 0);
    }
    else {
        // The horror of Apple OS/X: Do a redundant call to enable vsync on the async
        // glswapcontext for async flips despite vsync is already enabled on that context.
        // Otherwise vsync will fail -- Bug found on 10.4.11:
        if ((PSYCH_SYSTEM == PSYCH_OSX) && !PsychIsMasterThread()) PsychOSSetVBLSyncLevel(windowRecord, 1);
    }

    if (multiflip > 0) {
        // Experimental Multiflip requested. Build list of all onscreen windows...
		// CAUTION: This is not thread-safe in the Matlab/Octave environment, due to
		// callbacks into Matlabs/Octaves memory managment from a non-master thread!
		// --> multiflip > 0 is not allowed for async flips!!!
        PsychCreateVolatileWindowRecordPointerList(&numWindows, &windowRecordArray);
    }
    
    if (multiflip == 2) {
        // Disable VBL-Sync for all onscreen windows except our primary one:
        for(i=0;i<numWindows;i++) {
            if (PsychIsOnscreenWindow(windowRecordArray[i]) && (windowRecordArray[i]!=windowRecord)) {
				PsychOSSetVBLSyncLevel(windowRecordArray[i], 0);
            }
        }
    }

    // PsychOSSetVBLSyncLevel() above may have switched the OpenGL context. Make sure our context is bound:
    // No-Op on flipperthread, but safe, because PsychOSSetVBLSyncLevel() doesn't switch contexts there:
    PsychSetGLContext(windowRecord);
    
    // Backup current assignment of read- writebuffers:
    glGetIntegerv(GL_READ_BUFFER, &read_buffer);
    glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);

    // Perform preflip-operations: Backbuffer backups for the different dontclear-modes
    // and special compositing operations for specific stereo algorithms...
    // These are not thread-safe! For async flip, this gets called in async flip start
    // while still on the main thread, so this call here turns into a no-op:
    PsychPreFlipOperations(windowRecord, dont_clear);
    
    // Special imaging mode active? in that case a FBO may be bound instead of the system framebuffer.
    if (windowRecord->imagingMode > 0) {
	// Reset our drawing engine: This will unbind any FBO's (i.e. reset to system framebuffer)
	// and reset the current target window to 'none'. We need this to be sure that our flip
	// sync pixel token is written to the real system backbuffer...
	// No-Op on flipperthread, but ok, because it always has system-fb bound.
	PsychSetDrawingTarget(NULL);
    }

    // Reset color write mask to "all enabled"
    glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
	
    // Part 1 of workaround- /checkcode for syncing to vertical retrace:
    if (vblsyncworkaround) {
        glDrawBuffer(GL_BACK);
        glRasterPos2f(0, screenheight);
        glDrawPixels(1,1,GL_RED,GL_UNSIGNED_BYTE, &id);
    }
 
    // Update of hardware gamma table in sync with flip requested?
    if ((windowRecord->inRedTable) && (windowRecord->loadGammaTableOnNextFlip > 0)) {
      // Yes! Call the update routine now. It should schedule the actual update for
      // the same VSYNC to which our bufferswap will lock. "Should" means, we have no
      // way of checking programmatically if it really worked, only via our normal deadline
      // miss detector. If we are running under M$-Windows then loading the hw-gamma table
      // will block our execution until next retrace. Then it will upload the new gamma table.
      // Therefore we need to disable sync of bufferswaps to VBL, otherwise we would swap only
      // one VBL after the table update -> out of sync by one monitor refresh!
      if (PSYCH_SYSTEM==PSYCH_WINDOWS) PsychOSSetVBLSyncLevel(windowRecord, 0);
      
      // We need to wait for render-completion here, otherwise hw-gamma table update and
      // bufferswap could get out of sync due to unfinished rendering commands which would
      // defer the bufferswap, but not the clut update.
      glFinish();
	  
	  // Pipeline flush done by glFinish(), avoid redundant pipeline flushes:
	  windowRecord->PipelineFlushDone = TRUE;
	  
	  // Make sure the waiting code below is executed, regardless if any special os specific
	  // methods for swap scheduling are used. We need to emit the gamma table load command
	  // in the target refresh interval before the expected bufferswap time:
	  must_wait = TRUE;	  
    }
	else {
		// Need to sync the pipeline, if this special workaround is active to get good timing:
		if ((PsychPrefStateGet_ConserveVRAM() & kPsychBusyWaitForVBLBeforeBufferSwapRequest) || (windowRecord->specialflags & kPsychBusyWaitForVBLBeforeBufferSwapRequest)) {
			glFinish();

			// Pipeline flush done by glFinish(), avoid redundant pipeline flushes:
			windowRecord->PipelineFlushDone = TRUE;
		}
	}

	// Make sure the waiting code below is executed, regardless if any special os specific
	// methods for swap scheduling are used if the special PreSwapBuffersOperations hookchain
	// is enabled and contains commands. The semantic of this hookchain is to execute immediately
	// before the bufferswap, so we need to to wait until immediately before the expected swap:
	if (PsychIsHookChainOperational(windowRecord, kPsychPreSwapbuffersOperations)) must_wait = TRUE;

	// Setup and execution of OS specific swap scheduling mechanisms, e.g., OpenML OML_sync_control
	// extensions:
	
	// Don't use absolute vbl or (divisor,remainder) constraint by default:
	targetSwapFlags = 0;

	// Swap at a specific video field (even or odd) requested, e.g., to select the target field
	// in a frame-sequential stereo presentations setup and thereby the specific eye for stimulus
	// onset?
	if ((windowRecord->targetFlipFieldType != -1) && (windowRecord->stereomode != kPsychFrameSequentialStereo)) {
		// Yes. Map it to swapflags: 0/1 maps to 1/2 aka even/odd frame:
		targetSwapFlags |= (windowRecord->targetFlipFieldType == 0) ? 1 : 2;
	}
	
	// Get reference time:
	PsychGetAdjustedPrecisionTimerSeconds(&tremaining);
	tprescheduleswap = tremaining;
	
    // Pausing until a specific deadline requested?
    if (flipwhen > 0) {
		// Make sure the requested onset time is no more than 1000 seconds in the future,
		// otherwise we assume it is a coding error in usercode:
		if (flipwhen - tremaining > 1000) {
			PsychErrorExitMsg(PsychError_user, "\nYou specified a 'when' value to Flip that's over 1000 seconds in the future?!? Aborting, assuming that's an error.\n\n");
		}
		
		// Assign flipwhen as target swap time:
		targetWhen = flipwhen;
	}
	else {
		// Assign a time 1 second in the past as target swap time, so
		// an immediate (asap) swap is triggered:
		targetWhen = tremaining - 1.0;
	}

	// Invalidate target swapbuffer count values. Will get set to useful values
	// if PsychOSScheduleFlipWindowBuffers() succeeds:
	windowRecord->target_sbc = 0;
	if (windowRecord->slaveWindow) windowRecord->slaveWindow->target_sbc = 0;

	// Emit swap scheduling commands:
	// These are allowed to fail, due to some error condition or simply because this
	// function isn't supported on a given platform or configuration. Set our status
	// flag to TRUE, optimistically assuming it will work out. We reset to FALSE if
	// any of the involved commands fail:
	osspecific_asyncflip_scheduled = TRUE;

	// Schedule swap on main window:
	if ((swap_msc = PsychOSScheduleFlipWindowBuffers(windowRecord, targetWhen, 0, 0, 0, targetSwapFlags)) < 0) {
		// Scheduling failed or unsupported!
		if ((swap_msc < -1) && (verbosity > 1)) printf("PTB-WARNING: PsychOSScheduleFlipWindowBuffers() FAILED: errorcode = %i, targetWhen = %f, targetSwapFlags = %i.\n", (int) swap_msc, (float) targetWhen, (int) targetSwapFlags);
		
		osspecific_asyncflip_scheduled = FALSE;
	}
	
	// Also schedule the slave window, if any and if scheduling so far worked:
	if ((windowRecord->slaveWindow) && (osspecific_asyncflip_scheduled) && ((swap_msc = PsychOSScheduleFlipWindowBuffers(windowRecord->slaveWindow, targetWhen, 0, 0, 0, targetSwapFlags)) < 0)) {
		// Scheduling failed or unsupported!
		osspecific_asyncflip_scheduled = FALSE;
		
		// Big deal here: Because it worked on the master, but failed on the slave, we now
		// have an inconsistent situation and can't do anything about it, except warn user
		// of trouble ahead:
		if (verbosity > 1) {
			printf("PTB-WARNING: Scheduling a bufferswap on slave window of dual-window pair FAILED after successfull scheduling on masterwindow!\n");
			printf("PTB-WARNING: Expect complete loss of sync between windows and other severe visual- and timing-artifacts!\n");
			printf("PTB-WARNING: PsychOSScheduleFlipWindowBuffers() FAILED: errorcode = %i, targetWhen = %f, targetSwapFlags = %i.\n", (int) swap_msc, (float) targetWhen, (int) targetSwapFlags);
		}
	}

	// Multiflip with vbl-sync requested and scheduling worked so far?
	if ((multiflip == 1) && (osspecific_asyncflip_scheduled)) {
		for(i = 0; (i < numWindows) && (osspecific_asyncflip_scheduled); i++) {
			if (PsychIsOnscreenWindow(windowRecordArray[i]) && (windowRecordArray[i] != windowRecord)) {
				if (PsychOSScheduleFlipWindowBuffers(windowRecordArray[i], targetWhen, 0, 0, 0, targetSwapFlags) < 0) {
					// Scheduling failed or unsupported!
					osspecific_asyncflip_scheduled = FALSE;

					// Big deal here: Because it worked on the master, but failed on the slave, we now
					// have an inconsistent situation and can't do anything about it, except warn user
					// of trouble ahead:
					if (verbosity > 1) {
						printf("PTB-WARNING: Scheduling a bufferswap on some secondary window of a multi-window flip operation FAILED after successfull schedule on masterwindow!\n");
						printf("PTB-WARNING: Expect complete loss of sync between windows and other severe visual- and timing-artifacts!\n");
					}
				}
			}
		}
	}

    // Pausing until a specific deadline requested?
    if (flipwhen > 0) {
        // We shall not swap at next VSYNC, but at the VSYNC immediately following the
        // system time "flipwhen". This is the premium version of the old WaitBlanking... ;-)
        
        // Calculate deadline for a successfull flip: If time_at_vbl is later than that,
        // it means that we missed the proper video refresh cycle:
        tshouldflip = flipwhen;
        
        // Some time left until deadline 'flipwhen'?
        PsychGetAdjustedPrecisionTimerSeconds(&tremaining);
        if ((flipwhen - tremaining) > 0) {
            // Child protection against people specifying a flipwhen that's infinite, e.g.,
            // via wrong ifi calculation in Matlab: if a flipwhen more than 1000 secs.
            // in the future is specified, we just assume this is an error...
            if (flipwhen - tremaining > 1000) {
                PsychErrorExitMsg(PsychError_user, "\nYou specified a 'when' value to Flip that's over 1000 seconds in the future?!? Aborting, assuming that's an error.\n\n");
            }
            
			// We only wait here until 'flipwhen' deadline is reached if this isn't a
			// Windows Vista or later system with the DWM enabled. On Vista+DWM, we use
			// the DWM present API instead (see below) to schedule proper stimulus onset timing:
			// MK: Override - We do it the oldschool way, the DWM was utterly useless...
			// if (TRUE || !(PsychIsMSVista() && PsychOSIsDWMEnabled()) ) {
			if (!osspecific_asyncflip_scheduled || must_wait) {
				// We force the rendering pipeline to finish all pending OpenGL operations,
				// so we can be sure that swapping at VBL will be as fast as possible.
				// Btw: glFlush() is not recommended by Apple, but in this specific case
				// it makes sense (MK). We avoid this redundant flush, if a pipeline flush has
				// been already done by other routines, e.g, SCREENDrawingFinished.
				if (!windowRecord->PipelineFlushDone) glFlush();
				
				// We'll sleep - and hope that the OS will wake us up in time, if the remaining waiting
				// time is more than 0 milliseconds. This way, we don't burn up valuable CPU cycles by
				// busy waiting and don't get punished by the overload detection of the OS:
				PsychWaitUntilSeconds(flipwhen);
			}
        }
        // At this point, we are less than one video refresh interval away from the deadline - the next
        // VBL will be the one we want to flip at. Leave the rest of the job to CGLFlushDrawable...
    }
    else {
        // We should lock to next possible VSYNC:
        // Calculate deadline for a successfull flip on next VSYNC: If time_at_vbl is later than that,
        // it means that we missed the proper video refresh cycle:
        PsychGetAdjustedPrecisionTimerSeconds(&tshouldflip);        
    }

    // Do we know the exact system time when a VBL happened in the past?
    if ((windowRecord->time_at_last_vbl > 0) && (currentflipestimate > 0)) {
      // Yes! We use this as a base-line time to compute from the current time a virtual deadline,
      // which is at the beginning of the current monitor refresh interval.
      //
      // As long as we do synchronous Flips (sync_to_vbl == true - PTB blocks until VBL onset),
      // we should have a valid time_at_last_vbl, so this mechanism works.
      // Only on the *very first* invocation of Flip either after PTB-Startup or after a non-blocking
      // Flip, we can't do this because the time_at_last_vbl timestamp isn't available...
      tshouldflip = windowRecord->time_at_last_vbl + ((floor((tshouldflip - windowRecord->time_at_last_vbl) / currentflipestimate)) * currentflipestimate);
    }
    
    // Calculate final deadline for deadline-miss detection:
    tshouldflip = tshouldflip + slackfactor * currentflipestimate;        

	// Low level queries to the driver:
	// We query the timestamp and count of the last vertical retrace. This is needed for
	// correctness checking and timestamp computation on gfx-hardware without beamposition
	// queries (IntelMacs as of OS/X 10.4.10).
	// In frame-sequential stereo mode it also allows to lock bufferswaps either to even
	// or odd video refresh intervals (if windowRecord->targetFlipFieldType specifies this).
	// That way one can require stereo stimulus onset with either the left eye view or the
	// right eye view, depending on flip field selection. In other stereo modes or mono
	// mode one usually doesn't care about onset in even or odd fields.
	flipcondition_satisfied = FALSE;
	do {
		// Query driver:
		preflip_vbltimestamp = PsychOSGetVBLTimeAndCount(windowRecord, &preflip_vblcount);
		// Check if ready for flip, ie. if the proper even/odd video refresh cycle is approaching or
		// if we don't care about this, or if care has been taken already by osspecific_asyncflip_scheduled:
		flipcondition_satisfied = (windowRecord->stereomode == kPsychFrameSequentialStereo) || (windowRecord->targetFlipFieldType == -1) || (((preflip_vblcount + 1) % 2) == windowRecord->targetFlipFieldType) || (osspecific_asyncflip_scheduled && !must_wait);
		// If in wrong video cycle, we simply sleep a millisecond, then retry...
		if (!flipcondition_satisfied) PsychWaitIntervalSeconds(0.001);
	} while (!flipcondition_satisfied);

	// Reset to "undefined":
	preFlipFrameId = -1;
	dwmCompositionRate = 0;
	
	#if PSYCH_SYSTEM == PSYCH_WINDOWS
		// Vista or later? DWM supported and enabled?		
		if (PsychIsMSVista() && PsychOSGetPresentationTimingInfo(windowRecord, FALSE, 0, &dwmPreOnsetVBLCount, &dwmPreOnsetVBLTime, &preFlipFrameId, &dwmCompositionRate, 0)) {
			// Ok, now we've got the preflip state:
			if (verbosity > 5) printf("PTB-DEBUG: Preflip DWM Timing: VBLCount = %i  : PreOnsetTime = %15.6f : PreFlipFrameId = %i : CompositionRate = %f Hz.\n", (int) dwmPreOnsetVBLCount, dwmPreOnsetVBLTime, (int) preFlipFrameId, dwmCompositionRate);
		}
		else {
			preFlipFrameId = -1;
			dwmCompositionRate = 0;
		}
	#endif
    
	// Execute the hookchain for non-OpenGL operations that need to happen immediately before the bufferswap, e.g.,
	// sending out control signals or commands to external hardware to somehow sync it up to imminent bufferswaps:
	PsychPipelineExecuteHook(windowRecord, kPsychPreSwapbuffersOperations, NULL, NULL, FALSE, FALSE, NULL, NULL, NULL, NULL);
	
	// Take a measurement of the beamposition at time of swap request:
	line_pre_swaprequest = (int) PsychGetDisplayBeamPosition(displayID, windowRecord->screenNumber);

	// Take preswap timestamp:
	PsychGetAdjustedPrecisionTimerSeconds(&time_at_swaprequest);

	// Some check for buggy drivers: If VBL synched flipping is requested, we expect that at least 2 msecs
	// should pass between consecutive bufferswaps. 2 msecs is chosen because the VBL period of most displays
	// at most settings does not last longer than 2 msecs (usually way less than 1 msec), and this would still allow
	// for an update rate of 500 Hz -- more than any current display can do.
	// We also don't allow swap submission in the top area of the video scanout cycle between scanline 1 and
	// scanline min_line_allowed: Some broken drivers would still execute a swap within this forbidden top area
	// of the video display although video scanout has already started - resulting in tearing!
	//
	// Note that this isn't needed if OS specific swap scheduling is used, as that is supposed to take
	// care of such nuisances - and if it didn't, this wouldn't help anyway ;) :
	if ((windowRecord->time_at_last_vbl > 0) && (vbl_synclevel!=2) && (!osspecific_asyncflip_scheduled) &&
		((time_at_swaprequest - windowRecord->time_at_last_vbl < 0.002) || ((line_pre_swaprequest < min_line_allowed) && (line_pre_swaprequest > 0)))) {
		// Less than 2 msecs passed since last bufferswap, although swap in sync with retrace requested.
		// Some drivers seem to have a bug where a bufferswap happens anywhere in the VBL period, even
		// if already a swap happened in a VBL --> Multiple swaps per refresh cycle if this routine is
		// called fast enough, ie. multiple times during one single VBL period. Not good!
		// An example is the ATI Mobility Radeon X1600 in 2nd generation MacBookPro's under OS/X 10.4.10
		// and 10.4.11 -- probably most cards operated by the same driver have the same problem...
		if (verbosity > 9) printf("PTB-DEBUG: Swaprequest too close to last swap vbl (%f secs) or between forbidden scanline 1 and %i. Delaying...\n", time_at_swaprequest - windowRecord->time_at_last_vbl, min_line_allowed);

		// We try to enforce correct behaviour by waiting until at least 2 msecs have elapsed before the next
		// bufferswap:
		PsychWaitUntilSeconds(windowRecord->time_at_last_vbl + 0.002);

		// We also wait until beam leaves the forbidden area between scanline 1 and min_line_allowed, where
		// some broken drivers allow a swap to happen although the beam is already scanning out active
		// parts of the display:
		do {
			// Query beampos again:
			line_pre_swaprequest = (int) PsychGetDisplayBeamPosition(displayID, windowRecord->screenNumber);
		} while ((line_pre_swaprequest < min_line_allowed) && (line_pre_swaprequest > 0));

		// Take a measurement of the beamposition at time of swap request:
		line_pre_swaprequest = (int) PsychGetDisplayBeamPosition(displayID, windowRecord->screenNumber);

		// Take updated preswap timestamp:
		PsychGetAdjustedPrecisionTimerSeconds(&time_at_swaprequest);
	}
	
	#if PSYCH_SYSTEM == PSYCH_WINDOWS
	if (FALSE && PsychIsMSVista() && PsychOSIsDWMEnabled()) {
		// Use the DWM present parameters API to define requested stimulus onset time as
		// VSYNC count of target refresh interval:
		
		// If flipwhen is unspecified, just set it to "now":
		if (flipwhen <= 0) flipwhen = time_at_swaprequest;
		
		if ((flipwhen >= preflip_vbltimestamp) && (flipwhen >= time_at_swaprequest)) {
			// Compute target VBL count for stimulus onset, based on most recent known VBL time and counter value from
			// PsychOSGetVBLTimeAndCount() above. We translate flipwhen into a delta (in VBL intervals) relative to that
			// basetime/count, then compute the VBL count of the refresh interval in which flipwhen is contained, then add
			// 1 to aim for the successive refresh interval as targetVBL for stimulus onset:
			targetVBL = preflip_vblcount + ((psych_uint64) floor((flipwhen - preflip_vbltimestamp) / windowRecord->VideoRefreshInterval)) + 1;
		}
		else {
			// 'flipwhen' is in the past! No way we can make this deadline!
			// Choose the closest reasonable targetVBL we can use, the one of the last stimulus onset:
			targetVBL = (dwmPreOnsetVBLCount > preflip_vblcount) ? dwmPreOnsetVBLCount : preflip_vblcount;
			if (verbosity > 10) printf("PTB-DEBUG: In flip GPU-Scheduling: Running late!!! flipwhen %f secs is far in the past! Trying to swap asap.\n", flipwhen);
		}

		if (verbosity > 10) {
			printf("PTB-DEBUG: In flip GPU-Scheduling: preflip_vblcount = %i, targetVBL = %i, origFlipWhen = %f, preflip_vbltimestamp = %f\n", (int) preflip_vblcount,
					(int) targetVBL, (float) flipwhen, (float) preflip_vbltimestamp);
		}
		
		// Compute expected real stimulus onset time and update flipwhen accordingly - we'll need it later on:
		flipwhen  = (((psych_int64)(targetVBL - preflip_vblcount)) * windowRecord->VideoRefreshInterval) + preflip_vbltimestamp;

		// Tell DWM about the requested onset interval, and that that frame should be presented for 1 video refresh.
		// Buffercount is arbitrarily set to the maximum of 8 for now...
		if (!PsychOSSetPresentParameters(windowRecord, targetVBL, 8, -1)) {
			// Failed!
			if (verbosity > -1) printf("PTB-ERROR: PsychOSPresentParameters(%i, %i, %i) during bufferswap failed! Expect severe timing trouble!!\n", targetVBL, 8, -1);
		}
		
		// Same game for slaveWindow's if any:
		if (windowRecord->slaveWindow) {
			if (!PsychOSSetPresentParameters(windowRecord, targetVBL, 8, -1)) {
				// Failed!
				if (verbosity > -1) printf("PTB-ERROR: PsychOSPresentParameters(%i, %i, %i) during slaveWindow bufferswap failed! Expect severe timing trouble!!\n", targetVBL, 8, -1);
			}
		}
	}
	#endif
	
	// Update of hardware gamma table in sync with flip requested?
	if ((windowRecord->inRedTable) && (windowRecord->loadGammaTableOnNextFlip > 0)) {
		// Perform hw-table upload on M$-Windows in sync with retrace, wait until completion. On
		// OS-X just schedule update in sync with next retrace, but continue immediately.
		// See above for code that made sure we only reach this statement immediately prior
		// to the expected swap time, so this is as properly synced to target retrace as it gets:
		PsychLoadNormalizedGammaTable(windowRecord->screenNumber, -1, windowRecord->inTableSize, windowRecord->inRedTable, windowRecord->inGreenTable, windowRecord->inBlueTable);
	}

	// Only perform a bog-standard bufferswap request if no OS-specific method has been
	// executed successfully already:
	if (!osspecific_asyncflip_scheduled) {
		// Trigger the "Front <-> Back buffer swap (flip) on next vertical retrace" and
		PsychOSFlipWindowBuffers(windowRecord);
		
		// Also swap the slave window, if any:
		if (windowRecord->slaveWindow) PsychOSFlipWindowBuffers(windowRecord->slaveWindow);
		
		// Multiflip with vbl-sync requested?
		if (multiflip==1) {
			//  Trigger the "Front <-> Back buffer swap (flip) on next vertical retrace"
			//  for all onscreen windows except our primary one:
			for(i=0;i<numWindows;i++) {
				if (PsychIsOnscreenWindow(windowRecordArray[i]) && (windowRecordArray[i]!=windowRecord)) {
					PsychOSFlipWindowBuffers(windowRecordArray[i]);
				}
			}
		}
    }

	// Take post-swap request line:
	line_post_swaprequest = (int) PsychGetDisplayBeamPosition(displayID, windowRecord->screenNumber);

	// Take postswap request timestamp:
	PsychGetAdjustedPrecisionTimerSeconds(&time_post_swaprequest);

	// Store timestamp of swaprequest submission:
	windowRecord->time_at_swaprequest = time_at_swaprequest;
	windowRecord->time_post_swaprequest = time_post_swaprequest;
	
    // Pause execution of application until start of VBL, if requested:
    if (sync_to_vbl) {
		// Init tSwapComplete to undefined:
		tSwapComplete = 0;
		swap_msc = -1;

		// If OS-Builtin optimal timestamping is requested (aka vbltimestampmode 4), try to use it to
		// get a perfect timestamp. If this either fails or isn't supported on this system configuration,
		// fallback to regular PTB homegrown strategy:
		if ((vbltimestampmode == 4) && ((swap_msc = PsychOSGetSwapCompletionTimestamp(windowRecord, 0, &tSwapComplete)) >= 0)) {
			// Success! OS builtin optimal method provides swap_msc of swap completion and
			// tSwapComplete timestamp. Nothing to do here for now, but code at end of timestamping
			// will use this timestamp as override for anything else that got computed.
			// At return of the method, we know that the swap has completed.
			if (verbosity > 10) printf("PTB-DEBUG:PsychOSGetSwapCompletionTimestamp() success: swap_msc = %lld, tSwapComplete = %lf secs.\n", swap_msc, tSwapComplete);
		}
		else {
			// OS-Builtin timestamping failed, is unsupported, or it is disabled by usercode.
			if ((swap_msc < -1) && (verbosity > 1)) {
				printf("PTB-WARNING:PsychOSGetSwapCompletionTimestamp() FAILED: errorcode = %lld, tSwapComplete = %lf.\n", swap_msc, tSwapComplete);
				printf("PTB-WARNING: This likely means that timestamping will *not work at all* and has to be considered\n");
				printf("PTB-WARNING: not trustworthy! Check your system configuration, e.g., /etc/X11/xorg.conf and\n");
				printf("PTB-WARNING: /var/log/XOrg.0.log on Linux for hints on what could be misconfigured. This is \n");
				printf("PTB-WARNING: very likely not a bug, but a system misconfiguration by you or your distribution vendor.\n");
			}

			// Use one of our own home grown wait-for-swapcompletion and timestamping strategies:
			if ((vbl_synclevel==3) && (windowRecord->VBL_Endline != -1)) {
				// Wait for VBL onset via experimental busy-waiting spinloop instead of
				// blocking: We spin-wait until the rasterbeam of our master-display enters the
				// VBL-Area of the display:
				while (vbl_startline > (int) PsychGetDisplayBeamPosition(displayID, windowRecord->screenNumber));
			}
			else {
				// Standard blocking wait for flip/VBL onset requested:
				
				// Draw a single pixel in left-top area of back-buffer. This will wait/stall the rendering pipeline
				// until the buffer flip has happened, aka immediately after the VBL has started.
				// We need the pixel as "synchronization token", so the following glFinish() really
				// waits for VBL instead of just "falling through" due to the asynchronous nature of
				// OpenGL:                
				glDrawBuffer(GL_BACK_LEFT);
				// We draw our single pixel with an alpha-value of zero - so effectively it doesn't
				// change the color buffer - just the z-buffer if z-writes are enabled...
				glColor4f(0,0,0,0);
				glBegin(GL_POINTS);
				glVertex2i(10,10);
				glEnd();
				// This glFinish() will wait until point drawing is finished, ergo backbuffer was ready
				// for drawing, ergo buffer swap in sync with start of VBL has happened.
				glFinish();
			}			
		}

		// At this point, start of VBL on masterdisplay has happened, the bufferswap has completed and we can continue execution...
		
		// Multiflip without vbl-sync requested?
		if (multiflip==2) {
			// Immediately flip all onscreen windows except our primary one:
			for(i=0;i<numWindows;i++) {
				if (PsychIsOnscreenWindow(windowRecordArray[i]) && (windowRecordArray[i]!=windowRecord)) {
					PsychOSFlipWindowBuffers(windowRecordArray[i]);
				}
			}
		}
		
        // Query and return rasterbeam position immediately after Flip and before timestamp:
        *beamPosAtFlip=(int) PsychGetDisplayBeamPosition(displayID, windowRecord->screenNumber);

		// We take the corresponding timestamp here. This time_at_vbl will be later corrected in various
		// ways to get the real VBL timestamp of stimulus onset, either via the beamPosAtFlip above, or
		// via other means:
        PsychGetAdjustedPrecisionTimerSeconds(&time_at_vbl);
		
		// Store timestamp "as is" so we have the raw value for benchmarking and testing purpose as well:
		time_at_swapcompletion = time_at_vbl;

		// On Vista et al. with DWM, we need to "manually" wait for the expected flipwhen
		// time to elapse, because all the code before this point will execute mostly
		// non-blocking due to asynchronous nature of DWM composition.
		if (FALSE && PsychIsMSVista() && PsychOSIsDWMEnabled()) {
			if (verbosity > 10) printf("PTB-DEBUG: After swaprequest submission to GPU-Scheduling: Will wait for %f secs blocking until = %f (now = %f)\n", (float) (flipwhen - time_at_swapcompletion), (float) flipwhen, (float) time_at_swapcompletion);
			PsychWaitUntilSeconds(flipwhen);
		}
		
        // Run kernel-level timestamping always in modes 2 and 3 or on demand in mode 1 if beampos.
        // queries don't work properly or mode 4 if both beampos timestamping and OS-Builtin timestamping
		// doesn't work correctly:
        if ((vbltimestampmode == 2) || (vbltimestampmode == 3) || (vbltimestampmode == 1 && windowRecord->VBL_Endline == -1) || (vbltimestampmode == 4 && swap_msc < 0 && windowRecord->VBL_Endline == -1)) {
			
			// Preinit to no DWM timestamps:
			dwmrc = FALSE;
			
			// On MS-Windows Vista / Windows-7 systems with DWM support, we try to get a
			// onset timestamp from the DWM: TODO: Make this more general / OS independent.
			#if PSYCH_SYSTEM == PSYCH_WINDOWS
				// Vista or later? DWM supported and enabled?
				// MK: DISABLE THIS --> DWM presentation timing info is not a reliable source of stimulus onset timestamps at all :-(	
				if (FALSE && PsychIsMSVista() && (dwmCompositionRate > 0) && (preFlipFrameId != -1)) {
					// DWM available and enabled, preflip query worked. Perform repeated queries until we get
					// valid results (i.e., different from the preflip query), or an error happens:
					while (((dwmrc = PsychOSGetPresentationTimingInfo(windowRecord, TRUE, 0, &dwmPostOnsetVBLCount, &dwmOnsetVBLTime, &postFlipFrameId, &dwmCompositionRate, 0)) == TRUE) && (preFlipFrameId == postFlipFrameId)) {
						// Query successfull, but still returns same ol' values. Need to repeat query.
						// But first we yield a bit of cpu time to the system by taking a nap:
						if (verbosity > 10) printf("PTB-DEBUG: Not yet finished, napping... : PostFlip DWM Timing: VBLCount = %i  : PostOnsetTime = %15.6f : PostFlipFrameId = %i : CompositionRate = %f Hz.\n", (int) dwmPostOnsetVBLCount, dwmOnsetVBLTime, (int) postFlipFrameId, dwmCompositionRate);
						PsychYieldIntervalSeconds(0);
					}
				}
			#endif
			
			// No DWM timestamps?
			if (!dwmrc) {
				// Some systems (e.g., OS/X) only: Low level query to the driver: We need to yield the cpu for a couple of
				// microseconds, let's say 250 microsecs. for now, so the low-level vbl interrupt task
				// in IOKits workloop can do its job. But first let's try to do it without yielding...
				vbltimestampquery_retrycount = 0;
				PsychWaitIntervalSeconds(0.00025);

				// Testcode: On Linux we wait another msec before initial query
				// to avoid race-condition between return from glFinish() and VBL-Timestamping -- this to test nouveau's
				// KMS-Timestamping:
				// Disabled: Only uncomment for testing: if (PSYCH_SYSTEM == PSYCH_LINUX) PsychWaitIntervalSeconds(0.001);

				postflip_vbltimestamp = PsychOSGetVBLTimeAndCount(windowRecord, &postflip_vblcount);
				
				// If a valid preflip timestamp equals the postflip timestamp although the swaprequest likely didn't
				// happen inside a VBL interval (in which case this would be a legal condition), we retry the
				// query up to 8 times, each time sleeping for 0.25 msecs, for a total retry time of 2 msecs.
				// The sleeping is meant to release the processor to other system tasks which may be crucial for
				// correct timestamping, but preempted by our Matlab thread in realtime mode. If we don't succeed
				// in 2 msecs then something's pretty screwed and we should just give up.
				while ((preflip_vbltimestamp > 0) && (preflip_vbltimestamp == postflip_vbltimestamp) && (vbltimestampquery_retrycount < 8) && (time_at_swaprequest - preflip_vbltimestamp > 0.001)) {
					PsychWaitIntervalSeconds(0.00025);
					postflip_vbltimestamp = PsychOSGetVBLTimeAndCount(windowRecord, &postflip_vblcount);
					vbltimestampquery_retrycount++;
				}		
			}
			else {
				// Have DWM timestamps: Assign 'em:
				vbltimestampquery_retrycount = 0;
				postflip_vbltimestamp = dwmOnsetVBLTime;
				postflip_vblcount = dwmPostOnsetVBLCount;
				if (verbosity > 5) printf("PTB-DEBUG: Will use timestamps from DWM: VBLCount = %i  : PostOnsetTime = %15.6f : PostFlipFrameId = %i : CompositionRate = %f Hz.\n", (int) dwmPostOnsetVBLCount, dwmOnsetVBLTime, (int) postFlipFrameId, dwmCompositionRate);

				// Need to block here until final confirmed stimulus onset deadline to preserve Screen('Flip') semantic, ie., that it only returns
				// after the bufferswap has happened and stimulus onset is imminent:
				if (verbosity > 5) printf("PTB-DEBUG: Will wait blocking until expected bufferswap time = %f secs.\n", (float) postflip_vbltimestamp);
				PsychWaitUntilSeconds(postflip_vbltimestamp);
			}
        }
		
        // Calculate estimate of real time of VBL, based on our post glFinish() timestamp, post glFinish() beam-
        // position and the roughly known height of image and duration of IFI. The corrected time_at_vbl
        // contains time at start of VBL. This value is crucial for control stimulus presentation timing.
        // We also estimate the end of VBL, aka the stimulus onset time in time_at_onset.
        
        // First we calculate the number of scanlines that have passed since start of VBL area:
        vbl_endline = windowRecord->VBL_Endline;
        
        // VBL_Endline is determined in a calibration loop in PsychOpenOnscreenWindow above.
        // If this fails for some reason, we mark it as invalid by setting it to -1.
        if ((windowRecord->VBL_Endline != -1) && (vbltimestampmode>=0)) {

			// One more sanity check to account for the existence of the most
			// insane OS on earth: Check for impossible beamposition values although
			// we've already verified correct working of the queries during startup.
			if ((*beamPosAtFlip < 0) || (*beamPosAtFlip > vbl_endline)) {
				// Was the workaround for this bug already enabled?
				if (!(PsychPrefStateGet_ConserveVRAM() & kPsychUseBeampositionQueryWorkaround) && (PSYCH_SYSTEM == PSYCH_WINDOWS)) {
					// Workaround not yet enabled. Let's enable the workaround now, give a more
					// mild warning for this invocation of Flip and see if the workaround prevents
					// future malfunctions. If not, the if()->then() branch will catch it again in
					// next invocation and trigger an emergency shutdown of beampos timestamping.
					
					// Force workaround on: Hack hack hack - An ugly call to force it on on MS-Windows.
					// We hijack PsychCaptureScreen() for this purpose with special flag -1:
					PsychPrefStateSet_ConserveVRAM(PsychPrefStateGet_ConserveVRAM() | kPsychUseBeampositionQueryWorkaround);
					PsychCaptureScreen(-1);

					if (verbosity > -1) {
						printf("PTB-WARNING: Beamposition query after flip returned the *impossible* value %i (Valid would be between zero and %i)!!!\n", *beamPosAtFlip, (int) vbl_endline);
						printf("PTB-WARNING: This is a severe malfunction, indicating a bug in your graphics driver. Our startup test should have\n");
						printf("PTB-WARNING: caught this and a workaround should have been enabled. Apparently we missed this. Will enable workaround\n");
						printf("PTB-WARNING: now and see if it helps for future flips.\n");
						printf("PTB-WARNING: Read 'help Beampositionqueries' for further information on how to enable this workaround manually for\n");
						printf("PTB-WARNING: future sessions to avoid this warning.\n\n");
					}
				}
				else {
					// Workaround enabled and still this massive beampos failure?!? Or a non-Windows system?
					// Ok, this is completely foo-bared.
					if (verbosity > -1) {
						if (swap_msc < 0) {
							// No support for OS-Builtin alternative timestamping, or that mechanism failed.
							// This is serious:
							printf("PTB-ERROR: Beamposition query after flip returned the *impossible* value %i (Valid would be between zero and %i)!!!\n", *beamPosAtFlip, (int) vbl_endline);
							printf("PTB-ERROR: This is a severe malfunction, indicating a bug in your graphics driver. Will disable beamposition queries from now on.\n");
						}
						else {
							// Will use OS-Builtin timestamping anyway and it provided valid results,
							// so this is rather interesting than worrysome:
							printf("PTB-INFO: Beamposition query after flip returned the *impossible* value %i (Valid would be between zero and %i)!!!\n", *beamPosAtFlip, (int) vbl_endline);
							printf("PTB-INFO: This is a malfunction, indicating a bug in your graphics driver. Will disable beamposition queries from now on.\n");
							printf("PTB-INFO: Don't worry though, as we use a different mechanism for timestamping on your system anyway. This is just our backup that wouldn't work.\n");							
						}
						
						if ((swap_msc >= 0) && (vbltimestampmode == 4)) {
							printf("PTB-INFO: Will use OS-Builtin timestamping mechanism solely for further timestamping.\n");
						}
						else if ((PSYCH_SYSTEM == PSYCH_OSX) && (vbltimestampmode == 1)) { 
							printf("PTB-ERROR: As this is MacOS/X, i'll switch to a (potentially slightly less accurate) mechanism based on vertical blank interrupts...\n");
						}
						else {
							printf("PTB-ERROR: Timestamps returned by Flip will be correct, but less robust and accurate than they would be with working beamposition queries.\n");
						}

						if (swap_msc < 0) {
							printf("PTB-ERROR: It's strongly recommended to update your graphics driver and optionally file a bug report to your vendor if that doesn't help.\n");
							printf("PTB-ERROR: Read 'help Beampositionqueries' for further information.\n");
						}
					}
					
					// Mark vbl endline as invalid, so beampos is not used anymore for future flips.
					windowRecord->VBL_Endline = -1;
				}

				// Create fake beampos value for this invocation of Flip so we return an ok timestamp:
				*beamPosAtFlip = vbl_startline;
			}
			
            if (*beamPosAtFlip >= vbl_startline) {
                vbl_lines_elapsed = *beamPosAtFlip - vbl_startline;
                onset_lines_togo = vbl_endline - (*beamPosAtFlip) + 1;
            }
            else {
                vbl_lines_elapsed = vbl_endline - vbl_startline + 1 + *beamPosAtFlip;
                onset_lines_togo = -1.0 * (*beamPosAtFlip);
            }
            
            // From the elapsed number we calculate the elapsed time since VBL start:
            vbl_time_elapsed = vbl_lines_elapsed / vbl_endline * currentrefreshestimate; 
            onset_time_togo = onset_lines_togo / vbl_endline * currentrefreshestimate;
            // Compute of stimulus-onset, aka time when retrace is finished:
            *time_at_onset = time_at_vbl + onset_time_togo;
            // Now we correct our time_at_vbl by this correction value:
            time_at_vbl = time_at_vbl - vbl_time_elapsed;
        }
        else {
            // Beamposition queries unavailable!
            
            // Shall we fall-back to kernel-level query?
            if ((vbltimestampmode==1 || vbltimestampmode==2 || vbltimestampmode==4) && preflip_vbltimestamp > 0) {
                // Yes: Use fallback result:
                time_at_vbl = postflip_vbltimestamp;
            }
            
            // If we can't depend on timestamp correction, we just set time_at_onset == time_at_vbl.
            // This is not strictly correct, but at least the user doesn't have to change the whole
            // implementation of his code and we've warned him anyway at Window open time...
            *time_at_onset=time_at_vbl;
        }
                
        // OS level queries of timestamps supported and consistency check wanted?
        if (preflip_vbltimestamp > 0 && vbltimestampmode==2) {
            // Yes. Check both methods for consistency: We accept max. 1 ms deviation.
            if ((fabs(postflip_vbltimestamp - time_at_vbl) > 0.001) || (verbosity > 20)) {
                printf("VBL timestamp deviation: precount=%i , postcount=%i, delta = %i, postflip_vbltimestamp = %f  -  beampos_vbltimestamp = %f  == Delta is = %f \n",
                   (int) preflip_vblcount, (int) postflip_vblcount, (int) (postflip_vblcount - preflip_vblcount), postflip_vbltimestamp, time_at_vbl, postflip_vbltimestamp - time_at_vbl);
            }
        }
        
        // Shall kernel-level method override everything else?
        if (preflip_vbltimestamp > 0 && vbltimestampmode==3) {
            time_at_vbl = postflip_vbltimestamp;
            *time_at_onset=time_at_vbl;
        }
        
		// Another consistency check: Computed swap/VBL timestamp should never be earlier than
		// the system time when bufferswap request was initiated - Can't complete swap before
		// actually starting it! We test for this, but allow for a slack of 50 microseconds,
		// because a small "too early" offset could be just due to small errors in refresh rate
		// calibration or other sources of harmless timing errors.
		//
		// This is a test specific for beamposition based timestamping. We can't execute the
		// test (would not be diagnostic) if the swaprequest happened within the VBL interval,
		// because in that case, it is possible to get a VBL swap timestamp that is before the
		// swaprequest: The timestamp always denotes the onset of a VBL, but a swaprequest issued
		// at the very end of VBL would still get executed, therefore the VBL timestamp would be
		// valid although it technically precedes the time of the "late" swap request: This is
		// why we check the beampositions around time of swaprequest to make sure that the request
		// was issued while outside the VBL:
		if ((time_at_vbl < time_at_swaprequest - 0.00005) && ((line_pre_swaprequest > min_line_allowed) && (line_pre_swaprequest < vbl_startline)) && (windowRecord->VBL_Endline != -1) &&
			((line_post_swaprequest > min_line_allowed) && (line_post_swaprequest < vbl_startline)) && (line_pre_swaprequest <= line_post_swaprequest) &&
			(vbltimestampmode >= 0) && ((vbltimestampmode < 3) || (vbltimestampmode == 4 && swap_msc < 0 && !osspecific_asyncflip_scheduled))) {

			// Ohoh! Broken timing. Disable beamposition timestamping for future operations, warn user.			
			if (verbosity > -1) {
				printf("\n\nPTB-ERROR: Screen('Flip'); beamposition timestamping computed an *impossible stimulus onset value* of %f secs, which would indicate that\n", time_at_vbl);
				printf("PTB-ERROR: stimulus onset happened *before* it was actually requested! (Earliest theoretically possible %f secs).\n\n", time_at_swaprequest);
				printf("PTB-ERROR: Some more diagnostic values (only for experts): rawTimestamp = %f, scanline = %i\n", time_at_swapcompletion, *beamPosAtFlip);
				printf("PTB-ERROR: Some more diagnostic values (only for experts): line_pre_swaprequest = %i, line_post_swaprequest = %i, time_post_swaprequest = %f\n", line_pre_swaprequest, line_post_swaprequest, time_post_swaprequest);
				printf("PTB-ERROR: Some more diagnostic values (only for experts): preflip_vblcount = %i, preflip_vbltimestamp = %f\n", (int) preflip_vblcount, preflip_vbltimestamp);
				printf("PTB-ERROR: Some more diagnostic values (only for experts): postflip_vblcount = %i, postflip_vbltimestamp = %f, vbltimestampquery_retrycount = %i\n", (int) postflip_vblcount, postflip_vbltimestamp, (int) vbltimestampquery_retrycount);
				printf("\n");
			}
			
			// Is VBL IRQ timestamping allowed as a fallback and delivered a valid result?
			if (vbltimestampmode >= 1 && postflip_vbltimestamp > 0) {
				// Available. Meaningful result?
				if (verbosity > -1) {
					printf("PTB-ERROR: The most likely cause of this error (based on cross-check with kernel-level timestamping) is:\n");
					if (((postflip_vbltimestamp < time_at_swaprequest - 0.00005) && (postflip_vbltimestamp == preflip_vbltimestamp)) ||
						((preflip_vblcount + 1 == postflip_vblcount) && (vbltimestampquery_retrycount > 1))) {
						// Hmm. These results back up the hypothesis that sync of bufferswaps to VBL is broken, ie.
						// the buffers swap as soon as swappable and requested, instead of only within VBL:
						printf("PTB-ERROR: Synchronization of stimulus onset (buffer swap) to the vertical blank interval VBL is not working properly.\n");
						printf("PTB-ERROR: Please run the script PerceptualVBLSyncTest to check this. With non-working sync to VBL, all stimulus timing\n");
						printf("PTB-ERROR: becomes quite futile. Also read 'help SyncTrouble' !\n");
						printf("PTB-ERROR: For the remainder of this session, i've switched to kernel based timestamping as a backup method for the\n");
						printf("PTB-ERROR: less likely case that beamposition timestamping in your system is broken. However, this method seems to\n");
						printf("PTB-ERROR: confirm the hypothesis of broken sync of stimulus onset to VBL.\n\n");
					}
					else {
						// VBL IRQ timestamping doesn't support VBL sync failure, so it might be a problem with beamposition timestamping...
						printf("PTB-ERROR: Something may be broken in your systems beamposition timestamping. Read 'help SyncTrouble' !\n\n");
						printf("PTB-ERROR: For the remainder of this session, i've switched to kernel based timestamping as a backup method.\n");
						printf("PTB-ERROR: This method is slightly less accurate and higher overhead but should be similarly robust.\n");
						printf("PTB-ERROR: A less likely cause could be that Synchronization of stimulus onset (buffer swap) to the\n");
						printf("PTB-ERROR: vertical blank interval VBL is not working properly.\n");
						printf("PTB-ERROR: Please run the script PerceptualVBLSyncTest to check this. With non-working sync to VBL, all stimulus timing\n");
						printf("PTB-ERROR: becomes quite futile.\n");
					}
				}
				
				// Disable beamposition timestamping for further session:
				windowRecord->VBL_Endline = -1;
				
				// Set vbltimestampmode = 0 for rest of this subfunction, so the checking code for
				// stand-alone kernel level timestamping below this routine gets suppressed for this
				// iteration:
				vbltimestampmode = 0;
				
				// In any case: Override with VBL IRQ method results:
				time_at_vbl = postflip_vbltimestamp;
				*time_at_onset=time_at_vbl;
			}
			else {
				// VBL timestamping didn't deliver results? Because it is not enabled in parallel with beampos queries?
				if ((vbltimestampmode == 1) && (PSYCH_SYSTEM == PSYCH_OSX || PSYCH_SYSTEM == PSYCH_LINUX)) {
					// Auto fallback enabled, but not if beampos queries appear to be functional. They are
					// dysfunctional by now, but weren't at swap time, so we can't get any useful data from
					// kernel level timestamping. However in the next round we should get something. Therefore,
					// enable both methods in consistency cross check mode:
					PsychPrefStateSet_VBLTimestampingMode(2);

					// Set vbltimestampmode = 0 for rest of this subfunction, so the checking code for
					// stand-alone kernel level timestamping below this routine gets suppressed for this
					// iteration:
					vbltimestampmode = 0;

					if (verbosity > -1) {
						printf("PTB-ERROR: I have enabled additional cross checking between beamposition based and kernel-level based timestamping.\n");
						printf("PTB-ERROR: This should allow to get a better idea of what's going wrong if successive invocations of Screen('Flip');\n");
						printf("PTB-ERROR: fail to deliver proper timestamps as well. It may even fix the problem if the culprit would be a bug in \n");
						printf("PTB-ERROR: beamposition based high precision timestamping. We will see...\n\n");
						printf("PTB-ERROR: An equally likely cause would be that Synchronization of stimulus onset (buffer swap) to the\n");
						printf("PTB-ERROR: vertical blank interval VBL is not working properly.\n");
						printf("PTB-ERROR: Please run the script PerceptualVBLSyncTest to check this. With non-working sync to VBL, all stimulus timing\n");
						printf("PTB-ERROR: becomes quite futile. Also read 'help SyncTrouble' !\n");
					}
				}
				else {
					// Ok, we lost:
					// VBL kernel level timestamping not operational or intentionally disabled: No backup solutions left, and no way to
					// cross-check stuff: We disable high precision timestamping completely:

					// Disable beamposition timestamping for further session:
					PsychPrefStateSet_VBLTimestampingMode(-1);
					vbltimestampmode = -1;
					
					if (verbosity > -1) {
						printf("PTB-ERROR: This error can be due to either of the following causes (No way to discriminate):\n");
						printf("PTB-ERROR: Either something is broken in your systems beamposition timestamping. I've disabled high precision\n");
						printf("PTB-ERROR: timestamping for now. Returned timestamps will be less robust and accurate, but if that was the culprit it should be fixed.\n\n");
						printf("PTB-ERROR: An equally likely cause would be that Synchronization of stimulus onset (buffer swap) to the\n");
						printf("PTB-ERROR: vertical blank interval VBL is not working properly.\n");
						printf("PTB-ERROR: Please run the script PerceptualVBLSyncTest to check this. With non-working sync to VBL, all stimulus timing\n");
						printf("PTB-ERROR: becomes quite futile. Also read 'help SyncTrouble' !\n");
					}
				}
			}
		}
		
		// VBL IRQ based timestamping in charge? Either because selected by usercode, or as a fallback for failed/disabled beampos timestamping or OS-Builtin timestamping?
		if ((PSYCH_SYSTEM == PSYCH_OSX || PSYCH_SYSTEM == PSYCH_LINUX) && ((vbltimestampmode == 3) || (!osspecific_asyncflip_scheduled && vbltimestampmode == 4 && windowRecord->VBL_Endline == -1 && swap_msc < 0) || ((vbltimestampmode == 1 || vbltimestampmode == 2) && windowRecord->VBL_Endline == -1))) {
			// Yes. Try some consistency checks for that:

			// Some diagnostics at high debug-levels:
			if (vbltimestampquery_retrycount > 0 && verbosity > 10) printf("PTB-DEBUG: In PsychFlipWindowBuffers(), VBLTimestamping: RETRYCOUNT %i : Delta Swaprequest - preflip_vbl timestamp: %f secs.\n", (int) vbltimestampquery_retrycount, time_at_swaprequest - preflip_vbltimestamp);

			if ((vbltimestampquery_retrycount>=8) && (preflip_vbltimestamp == postflip_vbltimestamp) && (preflip_vbltimestamp > 0)) {
				// Postflip timestamp equals valid preflip timestamp after many retries:
				// This can be due to a swaprequest emitted and satisfied/completed within a single VBL
				// interval - then it would be perfectly fine. Or it happens despite a swaprequest in
				// the middle of a video refersh cycle. Then it would indicate trouble, either with the
				// timestamping mechanism or with sync of bufferswaps to VBL:
				// If we happened to do everything within a VBL, then the different timestamps should
				// be close together -- probably within 2 msecs - the max duration of a VBL and/or retry sequence:
				if (fabs(preflip_vbltimestamp - time_at_swaprequest) < 0.002) {
					// Swaprequest, Completion and whole timestamping happened likely within one VBL,
					// so no reason to worry...
					if (verbosity > 10) {
						printf("PTB-DEBUG: With kernel-level timestamping: ");
						printf("vbltimestampquery_retrycount = %i, preflip_vbltimestamp=postflip= %f, time_at_swaprequest= %f\n", (int) vbltimestampquery_retrycount, preflip_vbltimestamp, time_at_swaprequest);
					}
				}
				else {
					// Stupid values, but swaprequest not close to VBL, but likely within refresh cycle.
					// This could be either broken queries, or broken sync to VBL:
					if (verbosity > -1) {
						printf("\n\nPTB-ERROR: Screen('Flip'); kernel-level timestamping computed bogus values!!!\n");
						printf("PTB-ERROR: vbltimestampquery_retrycount = %i, preflip_vbltimestamp=postflip= %f, time_at_swaprequest= %f\n", (int) vbltimestampquery_retrycount, preflip_vbltimestamp, time_at_swaprequest);
						printf("PTB-ERROR: This error can be due to either of the following causes (No simple way to discriminate):\n");
						printf("PTB-ERROR: Either something is broken in your systems VBL-IRQ timestamping. I've disabled high precision\n");
						printf("PTB-ERROR: timestamping for now. Returned timestamps will be less robust and accurate, but at least ok, if that was the culprit.\n\n");
						printf("PTB-ERROR: An equally likely cause would be that Synchronization of stimulus onset (buffer swap) to the\n");
						printf("PTB-ERROR: vertical blank interval VBL is not working properly.\n");
						printf("PTB-ERROR: Please run the script PerceptualVBLSyncTest to check this. With non-working sync to VBL, all stimulus timing\n");
						printf("PTB-ERROR: becomes quite futile. Also read 'help SyncTrouble' !\n\n\n");
					}
					
					PsychPrefStateSet_VBLTimestampingMode(-1);
					time_at_vbl = time_at_swapcompletion;
					*time_at_onset=time_at_vbl;
				}
			}
									
			// We try to detect wrong order of events, but again allow for a bit of slack,
			// as some drivers (this time on PowerPC) have their own share of trouble. Specifically,
			// this might happen if a driver performs VBL timestamping at the end of a VBL interval,
			// instead of at the beginning. In that case, the bufferswap may happen at rising-edge
			// of VBL, get acknowledged and timestamped by us somewhere in the middle of VBL, but
			// the postflip timestamping via IRQ may carry a timestamp at end of VBL.
			// ==> Swap would have happened correctly within VBL and postflip timestamp would
			// be valid, just the order would be unexpected. We set a slack of 2.5 msecs, because
			// the duration of a VBL interval is usually no longer than that.
			if (postflip_vbltimestamp - time_at_swapcompletion > 0.0025) {
				// VBL irq queries broken! Disable them.
				if (verbosity > -1) {
					printf("PTB-ERROR: VBL kernel-level timestamp queries broken on your setup [Impossible order of events]!\n");
					printf("PTB-ERROR: Will disable them for now until the problem is resolved. You may want to restart Matlab and retry.\n");
					printf("PTB-ERROR: postflip - time_at_swapcompletion == %f secs.\n", postflip_vbltimestamp - time_at_swapcompletion);
					printf("PTB-ERROR: Btw. if you are running in windowed mode, this is not unusual -- timestamping doesn't work well in windowed mode...\n");
				}
				
				PsychPrefStateSet_VBLTimestampingMode(-1);
				time_at_vbl = time_at_swapcompletion;
				*time_at_onset=time_at_vbl;
			}
		}
		
		// Shall OS-Builtin optimal timestamping override all other results (vbltimestampmode 4)
		// and is it supported and worked successfully?
		if ((vbltimestampmode == 4) && (swap_msc >= 0)) {
			// Yes. Override final timestamps with results from OS-Builtin timestamping:

			// Consistency check: Swap can't complete before it was scheduled: Have a fudge
			// value of 1 msec to account for roundoff errors:
			if (osspecific_asyncflip_scheduled && (tSwapComplete < tprescheduleswap - 0.001)) {
				if (verbosity > -1) {
					printf("PTB-ERROR: OpenML timestamping reports that flip completed before it was scheduled [Scheduled no earlier than %f secs, completed at %f secs]!\n", tprescheduleswap, tSwapComplete);
					printf("PTB-ERROR: This could mean that sync of bufferswaps to vertical retrace is broken or some other driver bug! Switching to alternative timestamping method.\n");
					printf("PTB-ERROR: Check your system setup!\n");
				}

				// Fall back to beamposition timestamping or raw timestamping:
				PsychPrefStateSet_VBLTimestampingMode(1);

				// Use override raw timestamp as temporary fallback:
				time_at_vbl = time_at_swapcompletion;
				*time_at_onset=time_at_vbl;
			} else {
				// Looks good. Assign / Override:
				time_at_vbl = tSwapComplete;
				*time_at_onset = tSwapComplete;
			}
		}

		// Timestamping finished, final results available!

        // Check for missed / skipped frames: We exclude the very first "Flip" after
        // creation of the onscreen window from the check, as deadline-miss is expected
        // in that case. We also disable the skipped frame detection if our own home-grown
        // frame-sequential stereo mode is active, as the detector can't work sensibly with it:
        if ((time_at_vbl > tshouldflip) && (windowRecord->time_at_last_vbl!=0) && (windowRecord->stereomode != kPsychFrameSequentialStereo)) {
            // Deadline missed!
            windowRecord->nr_missed_deadlines = windowRecord->nr_missed_deadlines + 1;
        }
        
        // Return some estimate of how much we've missed our deadline (positive value) or
        // how much headroom was left (negative value):
        *miss_estimate = time_at_vbl - tshouldflip;
        
        // Update timestamp of last vbl:
        windowRecord->time_at_last_vbl = time_at_vbl;

		// Store raw-timestamp of swap completion, mostly for benchmark purposes:
		windowRecord->rawtime_at_swapcompletion = time_at_swapcompletion;
		
		// Store optional VBL-IRQ timestamp as well:
		windowRecord->postflip_vbltimestamp = postflip_vbltimestamp;
		
		// Store optional OS-Builtin swap timestamp as well:
		windowRecord->osbuiltin_swaptime = tSwapComplete;
    }
    else {
        // syncing to vbl is disabled, time_at_vbl becomes meaningless, so we set it to a
        // safe default of zero to indicate this.
        time_at_vbl = 0;
        *time_at_onset = 0;
        *beamPosAtFlip = -1;  // Ditto for beam position...
        
        // Invalidate timestamp of last vbl:
        windowRecord->time_at_last_vbl = 0;
        windowRecord->rawtime_at_swapcompletion = 0;
        windowRecord->postflip_vbltimestamp = -1;
        windowRecord->osbuiltin_swaptime = 0;
    }

	// Increment the "flips successfully completed" counter:
	windowRecord->flipCount++;
        
    // Part 2 of workaround- /checkcode for syncing to vertical retrace:
    if (vblsyncworkaround) {
        glReadBuffer(GL_FRONT);
        glPixelStorei(GL_PACK_ALIGNMENT,1);
        glReadPixels(0,0,1,1,GL_RED,GL_UNSIGNED_BYTE, &bufferstamp);
        if (bufferstamp!=id) {
            printf("%i -> %i  ", id, bufferstamp);
            glColor3b((GLint) bufferstamp, (GLint) 0,(GLint) 0);
            glBegin(GL_TRIANGLES);
            glVertex2d(20,20);
            glVertex2d(200,200);
            glVertex2d(20,200);
            glEnd();
        }

        id++;
    }

    // The remaining code will run asynchronously on the GPU again and prepares the back-buffer
    // for drawing of next stim.
    PsychPostFlipOperations(windowRecord, dont_clear);

    // Special imaging mode active? in that case we need to restore drawing engine state to preflip state.
    if (windowRecord->imagingMode > 0) {
        if (PsychIsMasterThread()) {
			// Can use PsychSetDrawingTarget for synchronous flip:
			PsychSetDrawingTarget(windowRecord);
		}
		else {
			// Async flip: need to manually reset framebuffer binding
			// to system framebuffer, as PsychSetDrawingTarget() is a no-op:
			glFinish();
			if (glBindFramebufferEXT) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
		}
    }

    // Restore assignments of read- and drawbuffers to pre-Flip state:
    glReadBuffer(read_buffer);
    glDrawBuffer(draw_buffer);

    // Managing these flags is the sole job of the master thread:
    if (PsychIsMasterThread()) {
	// Reset flags used for avoiding redundant Pipeline flushes and backbuffer-backups:
	// This flags are altered and checked by SCREENDrawingFinished() and PsychPreFlipOperations() as well:
	windowRecord->PipelineFlushDone = false;
	windowRecord->backBufferBackupDone = false;
    }

    // If we disabled (upon request) VBL syncing, we have to reenable it here:
    if (vbl_synclevel==2 || (windowRecord->inRedTable && (PSYCH_SYSTEM == PSYCH_WINDOWS))) {
		PsychOSSetVBLSyncLevel(windowRecord, 1);
		// Reenable also for a slave window, if any:
		if (windowRecord->slaveWindow && PsychIsMasterThread()) PsychOSSetVBLSyncLevel(windowRecord->slaveWindow, 1);
    }
    
    // Was this an experimental Multiflip with "hard" busy flipping?
    if (multiflip==2) {
        // Reenable VBL-Sync for all onscreen windows except our primary one:
        for(i=0;i<numWindows;i++) {
            if (PsychIsOnscreenWindow(windowRecordArray[i]) && (windowRecordArray[i]!=windowRecord)) {
				PsychOSSetVBLSyncLevel(windowRecordArray[i], 1);
            }
        }
    }

    // PsychOSSetVBLSyncLevel() above may have switched the OpenGL context. Make sure our context is bound:
    PsychSetGLContext(windowRecord);
    
    if (multiflip>0) {
        // Cleanup our multiflip windowlist: Not thread-safe!
        PsychDestroyVolatileWindowRecordPointerList(windowRecordArray);
    }
    
    // Cleanup temporary gamma tables if needed: Should be thread-safe due to standard libc call.
    if ((windowRecord->inRedTable) && (windowRecord->loadGammaTableOnNextFlip > 0)) {
	free(windowRecord->inRedTable); windowRecord->inRedTable = NULL;
	free(windowRecord->inGreenTable); windowRecord->inGreenTable = NULL;
	free(windowRecord->inBlueTable); windowRecord->inBlueTable = NULL;
	windowRecord->inTableSize = 0;
	windowRecord->loadGammaTableOnNextFlip = 0;
    }

    // We take a second timestamp here to mark the end of the Flip-routine and return it to "userspace"
    PsychGetAdjustedPrecisionTimerSeconds(time_at_flipend);
    
    // Done. Return high resolution system time in seconds when VBL happened.
    return(time_at_vbl);
}

/*
    PsychSetGLContext()
    
    Set the window to which GL drawing commands are sent.  
*/
void PsychSetGLContext(PsychWindowRecordType *windowRecord)
{
	PsychWindowRecordType *parentRecord;
	psych_bool oldStyle = (PsychPrefStateGet_ConserveVRAM() & kPsychUseOldStyleAsyncFlips) ? TRUE : FALSE;

	// Are we called from the main interpreter thread? If not, then we return
	// immediately (No-Op). Worker threads for async flip don't expect this
	// subroutine to execute, at least not with the new-style async flip method:
	if (!oldStyle && !PsychIsMasterThread()) return;

	// Check if any async flip on any onscreen window in progress: In that case only the async flip worker thread is allowed to call PsychSetGLContext()
	// on async-flipping onscreen windows, and none of the threads is allowed to attach to non-onscreen-window ressources.
	// asyncFlipOpsActive is a count of currently async-flipping onscreen windows...

	// Any async flips in progress? If not, then we can skip this whole checking...
	if (asyncFlipOpsActive > 0) {
		// At least one async flip in progress. Find the parent window of this windowRecord, ie.,
		// the onscreen window which "owns" the relevant OpenGL context. This can be the windowRecord
		// itself if it is an onscreen window:
		parentRecord = PsychGetParentWindow(windowRecord);
		
		// Behaviour depends on use of the new or old (legacy) async-flip implementation:
		//
		// New:
		// Binding to the associated onscreen windows master OpenGL context is no problem, just touching its
		// system framebuffer would wreak havoc. We allow to bind the OpenGL context and have proper safe-guards
		// implemented in PsychSetDrawingTarget() to make sure the imaging pipeline is set up in a way to prevent
		// framebuffer access by redirecting all drawing to FBO's. Additional safe-guards are to be found in
		// routines that touch the system fb like 'GetImage' and 'DrawingFinished'.
		//
		// Legacy / Old:
		// We allow the main thread to attach to the OpenGL contexts owned by "parentRecord" windows which are not (yet)
		// involved in an async flip operation. Only the worker thread of an async flipping "parentRecord" window is
		// allowed to attach that window while async flip in progress.
        
		// Check for illegal operations in current mode:
		if (oldStyle && (parentRecord->flipInfo) && (parentRecord->flipInfo->asyncstate == 1) &&
		    (!PsychIsCurrentThreadEqualToPsychThread(parentRecord->flipInfo->flipperThread))) {
			// Wrong thread - This one is not allowed to attach to any OpenGL context for this parentRecord at the moment.
			// Likely a programming error by the user:
			if (!PsychIsOnscreenWindow(windowRecord)) {
				// Ok, we don't error-abort as usual via PsychErrorExitMsg() because this leads to
				// infinite recursion and other funny problems. Instead we output a warning to the
				// user and no-op this PsychSetGLContext() operation:
				printf("PTB-ERROR: Your code tried to execute a Screen() graphics command or Matlab/Octave/C OpenGL command for an offscreen window,\ntexture or proxy while some asynchronous flip operation was in progress for the parent window!\nThis is not allowed for that command! Finalize the async flip(s) first via Screen('AsyncFlipCheckEnd') or Screen('AsyncFlipEnd')!\nAlternatively enable the imaging pipeline (help PsychImaging) to make this work.\n\n");
				return;
			}
			else {
				PsychErrorExitMsg(PsychError_user, "Your code tried to execute a Screen() graphics command or Matlab/Octave/C OpenGL command for an onscreen window while an\nasynchronous flip operation was in progress on that window! This is not allowed for that command!\nFinalize the flip first via Screen('AsyncFlipCheckEnd') or Screen('AsyncFlipEnd')!\nAlternatively enable the imaging pipeline (help PsychImaging) to make this work.");
			}
		}
		
		// Note: We allow drawing to non-async-flipping onscreen windows and offscreen windows/textures/proxies which don't have
		// the same OpenGL context as any of the async-flipping windows, or drawing anywhere with the imaging pipeline on, because
		// the parallel background flip thread uses its own dedicated OpenGL context for its operations and the imaging pipeline
		// redirects any drawing into offscreen FBO's and thereby protects the system framebuffer from interference with flip ops.
		// This should be sufficient to prevent crashes and/or other GL malfunctions, so formally this is safe.
		// However, once created all textures/FBO's are shared between all contexts because we (have to) enable context resource
		// sharing on all our contexts for optimal performance and various of our features. That means that operations performed
		// on them may impact the performance and latency of operations in unrelated contexts, e.g., the ones involved in async flip
		//  --> Potential to impair the stimulus onset timing of async-flipping windows. Another reason for impaired timing is that
		// some ressources on the GPU can't be used in parallel to async flips, so operations in parallely executing contexts get
		// serialized due to ressource contention. Examples would be the command processor CP on ATI Radeons, which only exists once,
		// so all command streams have to be serialized --> Bufferswap trigger command packet could get stuck/stalled behind a large
		// bunch of drawing commands for an unrelated GL context and command stream in the worst case! This is totally dependent
		// on the gfx-drivers implementation of register programming for swap -- e.g., done through CP or done via direct register writes?
		//
		// While recent NVidia gpu's have multiple command fifo's, the execution of the actual rendering work is afaik mostly serialized,
		// just the scheduling of command streams is offloaded to hardware. In summary, pretty much all shipping GPU's as of beginning 2012
		// will impose some serialization somewhere in the rendering pipeline and are potentially vulnerable to cross-talk between
		// parallely executing OpenGL command streams, potentially causing timing issues.
		//
		// All in all, the option to render to non-flipping ressources may give a performance boost when used very carefully, but
		// may also impair good timing if not used by advanced/experienced users. But in some sense that is true for the whole async
		// flip stuff -- it is pushing the system pretty hard so it will always be more fragile wrt. system load fluctuations etc.
		//
		// A notable exception is Linux + DRI2 graphics stack on some GPU's, e.g., ATI/AMD: Its advanced swap scheduling allows to
		// remove some of the timing problems mentioned above, so parallel rendering may not impair swapping as much as on other
		// system configs.
	}

	// Child protection: Calling this function is only allowed in non-userspace rendering mode:
	if (PsychIsMasterThread() && PsychIsUserspaceRendering()) PsychErrorExitMsg(PsychError_user, "You tried to call a Screen graphics command after Screen('BeginOpenGL'), but without calling Screen('EndOpenGL') beforehand! Read the help for 'Screen EndOpenGL?'.");
	
	// Call OS - specific context switching code:
	PsychOSSetGLContext(windowRecord);
}

/*
    PsychClearGLContext()
    
    Clear the drawing context.  
*/
void PsychUnsetGLContext(void)
{
  PsychErrorExitMsg(PsychError_internal, "Ouuuucchhhh!!! PsychUnsetGLContext(void) called!!!!\n");    
}

/*
    PsychGetMonitorRefreshInterval() -- Monitor refresh calibration.
    
    When called with numSamples>0, will enter a calibration loop that measures until
    either numSamples valid samples have been taken *and* measured standard deviation
    is below some spec'd threshold, or some maximum time has elapsed (timeout).
    The refresh interval - intervalHint (if >0) is taken as a hint for the real refresh
    interval to stabilize measurements by detection/elimination of outliers.

    The estimate is stored in the windowRecord of the associated onscreen-window for
    use by different PTB-Routines, e.g., PsychFlipWindowBuffers(), it is also returned
    in the double-return argument.
 
    When called with numSamples=0, will return the refresh interval estimated by a
    previous calibration run of the routine.
 
    This routine will be called in PsychOpenOnscreenWindow for initial calibration,
    taking at least 50 samples and can be triggered by Matlab by calling the
    SCREENGetFlipInterval routine, if an experimenter needs a more accurate estimate...
*/
double PsychGetMonitorRefreshInterval(PsychWindowRecordType *windowRecord, int* numSamples, double* maxsecs, double* stddev, double intervalHint)
{
    int i, j;
    double told, tnew, tdur, tstart;
    double tstddev=10000.0f;
    double tavg=0;
    double tavgsq=0;
    double n=0;
    double reqstddev=*stddev;   // stddev contains the requested standard deviation.
    int fallthroughcount=0;
    double* samples = NULL;
    int maxlogsamples = 0;
    psych_bool useOpenML = ((PsychPrefStateGet_VBLTimestampingMode() == 4) && (windowRecord->gfxcaps & kPsychGfxCapSupportsOpenML));

    // Child protection: We only work on double-buffered onscreen-windows...
    if (windowRecord->windowType != kPsychDoubleBufferOnscreen) {
        PsychErrorExitMsg(PsychError_InvalidWindowRecord, "Tried to query/measure monitor refresh interval on a window that's not double-buffered and on-screen.");
    }
    
    // Calibration run requested?
    if (*numSamples>0) {
        // Calibration run of 'numSamples' requested. Let's do it.
        
		if (PsychPrefStateGet_Verbosity()>4) {
			// Allocate a sample logbuffer for maxsecs duration at 1000 hz refresh:
			maxlogsamples =  (int) (ceil(*maxsecs) * 1000);
			samples = calloc(sizeof(double), maxlogsamples);
		}
		
        // Switch to RT scheduling for timing tests:
        PsychRealtimePriority(true);

        // Wipe out old measurements:
        windowRecord->IFIRunningSum = 0;
        windowRecord->nrIFISamples = 0;

        // Enable this windowRecords framebuffer as current drawingtarget: Important to do this, even
		// if it gets immediately disabled below, as this also sets the OpenGL context and takes care
		// of all state transitions between onscreen/offscreen windows etc.:
        PsychSetDrawingTarget(windowRecord);

		// Disable any shaders:
		PsychSetShader(windowRecord, 0);
		
		// ...and immediately disable it in imagingmode, because it won't be the system backbuffer,
		// but a FBO -- which would break sync of glFinish() with bufferswaps and vertical retrace.
		if ((windowRecord->imagingMode > 0) && (windowRecord->imagingMode != kPsychNeedFastOffscreenWindows)) PsychSetDrawingTarget(NULL);
		
        glDrawBuffer(GL_BACK_LEFT);
        
        PsychGetAdjustedPrecisionTimerSeconds(&tnew);
        tstart = tnew;
		told = -1;
		
		// Schedule a buffer-swap on next VBL:
		PsychOSFlipWindowBuffers(windowRecord);
		
        // Take samples during consecutive refresh intervals:
        // We measure until either:
        // - A maximum measurment time of maxsecs seconds has elapsed... (This is the emergency switch to prevent infinite loops).
        // - Or at least numSamples valid samples have been taken AND measured standard deviation is below the requested deviation stddev.
	for (i=0; (fallthroughcount<10) && ((tnew - tstart) < *maxsecs) && (n < *numSamples || ((n >= *numSamples) && (tstddev > reqstddev))); i++) {
		// Schedule a buffer-swap on next VBL:
		PsychOSFlipWindowBuffers(windowRecord);
            
		if (!(useOpenML && (PsychOSGetSwapCompletionTimestamp(windowRecord, 0, &tnew) > 0))) {
			// OpenML swap completion timestamping unsupported, disabled, or failed.
			// Use our standard trick instead.
			
			// Wait for it, aka VBL start: See PsychFlipWindowBuffers for explanation...
			glBegin(GL_POINTS);
			glColor4f(0,0,0,0);
			glVertex2i(10,10);
			glEnd();
			
			// This glFinish() will wait until point drawing is finished, ergo backbuffer was ready
			// for drawing, ergo buffer swap in sync with start of VBL has happened.
			glFinish();
			
			// At this point, start of VBL has happened and we can continue execution...
			// We take our timestamp here:
			PsychGetAdjustedPrecisionTimerSeconds(&tnew);
		}

            // We skip the first measurement, because we first need to establish an initial base-time 'told'
            if (told > 0) {
                // Compute duration of this refresh interval in tnew:
                tdur = tnew - told;
                
                // This is a catch for complete sync-failure:
                // tdur < 0.004 can happen occasionally due to operating system scheduling jitter,
                // in this case tdur will be >> 1 monitor refresh for next iteration. Both samples
                // will be rejected, so they don't skew the estimate.
                // But if tdur < 0.004 for multiple consecutive frames, this indicates that
                // synchronization fails completely and we are just "falling through" glFinish().
                // A fallthroughcount>=10 will therefore abort the measurment-loop and invalidate
                // the whole result - indicating VBL sync trouble...
                // We need this additional check, because without it, we could get 1 valid sample with
                // approx. 10 ms ifi accidentally because the whole loop can stall for 10 ms on overload.
                // The 10 ms value is a valid value corresponding to 100 Hz refresh and by coincidence its
                // the "standard-timeslicing quantum" of the MacOS-X scheduler... ...Wonderful world of
                // operating system design and unintended side-effects for poor psychologists... ;-)
                fallthroughcount = (tdur < 0.004) ? fallthroughcount+1 : 0;

                // We accept the measurement as valid if either no intervalHint is available as reference or
                // we are in an interval between +/-20% of the hint.
                // We also check if interval corresponds to a measured refresh between 20 Hz and 250 Hz. Other
                // values are considered impossible and are therefore rejected...
                // If we are in OpenGL native stereo display mode, aka temporally interleaved flip-frame stereo,
                // then we also accept samples that are in a +/-20% rnage around twice the intervalHint. This is,
                // because in OpenGL stereo mode, ATI hardware doubles the flip-interval: It only flips every 2nd
                // video refresh, so a doubled flip interval is a legal valid result.
                if ((tdur >= 0.004 && tdur <= 0.050) && ((intervalHint<=0) || (intervalHint>0 &&
                    ( ((tdur > 0.8 * intervalHint) && (tdur < 1.2 * intervalHint)) ||
                      (((windowRecord->stereomode==kPsychOpenGLStereo) || (windowRecord->multiSample > 0)) && (tdur > 0.8 * 2 * intervalHint) && (tdur < 1.2 * 2 * intervalHint))
                    )))) {
                    // Valid measurement - Update our estimate:
                    windowRecord->IFIRunningSum = windowRecord->IFIRunningSum + tdur;
                    windowRecord->nrIFISamples = windowRecord->nrIFISamples + 1;

                    // Update our sliding mean and standard-deviation:
                    tavg = tavg + tdur;
                    tavgsq = tavgsq + (tdur * tdur);
                    n=windowRecord->nrIFISamples;
                    tstddev = (n>1) ? sqrt( ( tavgsq - ( tavg * tavg / n ) ) / (n-1) ) : 10000.0f;

					// Update reference timestamp:
					told = tnew;
					
					// Pause for 2 msecs after a valid sample was taken. This to guarantee we're out
					// of the VBL period of the successfull swap.
					PsychWaitIntervalSeconds(0.002);
                }
				else {
					// Rejected sample: Better invalidate told as well:
					//told = -1;
					// MK: Ok, i have no clue why above told = -1 is wrong, but doing it makes OS/X 10.4.10 much
					// more prone to sync failures, whereas not doing it makes it more reliable. Doesn't make
					// sense, but we are better off reverting to the old strategy...
					// Update: I think i know why. Some (buggy!) drivers, e.g., the ATI Radeon X1600 driver on
					// OS/X 10.4.10, do not limit the number of bufferswaps to 1 per refresh cycle as mandated
					// by the spec, but they allow as many bufferswaps as you want, as long as all of them happen
					// inside the VBL period! Basically the swap-trigger seems to be level-triggered instead of
					// edge-triggered. This leads to a ratio of 2 invalid samples followed by 1 valid sample.
					// If we'd reset our told at each invalid sample, we would need over 3 times the amount of
					// samples for a useable calibration --> No go. Now we wait for 2 msecs after each successfull
					// sample (see above), so the VBL period will be over before we manage to try to swap again.
					
					// Reinitialize told to tnew, otherwise errors can accumulate:
					told = tnew;

					// Pause for 2 msecs after a valid sample was taken. This to guarantee we're out
					// of the VBL period of the successfull swap.
					PsychWaitIntervalSeconds(0.002);
				}
				
				// Store current sample in samplebuffer if requested:
				if (samples && i < maxlogsamples) samples[i] = tdur;
            }
			else {
				// (Re-)initialize reference timestamp:
				told = tnew;

				// Pause for 2 msecs after a first sample was taken. This to guarantee we're out
				// of the VBL period of the successfull swap.
				PsychWaitIntervalSeconds(0.002);
			}
			
        } // Next measurement loop iteration...
        
        // Switch back to old scheduling after timing tests:
        PsychRealtimePriority(false);
        
        // Ok, now we should have a pretty good estimate of IFI.
        if ( windowRecord->nrIFISamples <= 0 ) {
            printf("PTB-WARNING: Couldn't even collect one single valid flip interval sample! Sanity range checks failed!\n");
            printf("PTB-WARNING: Could be a system bug, or a temporary timing problem. Retrying the procedure might help if\n");
            printf("PTB-WARNING: the latter is the culprit.\n");
        }

        // Some additional check:
        if (fallthroughcount>=10) {
            // Complete sync failure! Invalidate all measurements:
            windowRecord->nrIFISamples = 0;
            n=0;
            tstddev=1000000.0;
            windowRecord->VideoRefreshInterval = 0;
            printf("PTB-WARNING: Couldn't collect valid flip interval samples! Fatal VBL sync failure!\n");
            printf("PTB-WARNING: Either synchronization of doublebuffer swapping to the vertical retrace signal of your display is broken,\n");
            printf("PTB-WARNING: or the mechanism for detection of swap completion is broken. In any case, this is an operating system or gfx-driver bug!\n");
        }
        
        *numSamples = n;
        *stddev = tstddev;
		
		// Verbose output requested? We dump our whole buffer of samples to the console:
		if (samples) {
			printf("\n\nPTB-DEBUG: Output of all acquired samples of calibration run follows:\n");
			for (j=0; j<i; j++) printf("PTB-DEBUG: Sample %i: %f\n", j, samples[j]);
			printf("PTB-DEBUG: End of calibration data for this run...\n\n");
			free(samples);
			samples = NULL;
		}
		
    } // End of IFI measurement code.
    else {
        // No measurements taken...
        *numSamples = 0;
        *stddev = 0;
    }
    
    // Return the current estimate of flip interval & monitor refresh interval, if any...
    if (windowRecord->nrIFISamples > 0) {
        return(windowRecord->IFIRunningSum / windowRecord->nrIFISamples);
    }
    else {
        // Invalidate refresh on error.
        windowRecord->VideoRefreshInterval = 0;
        return(0);
    }
}

/*
    PsychVisualBell()
 
    Visual bell: Flashes the screen multiple times by changing background-color.
    This meant to be used when PTB detects some condition important for the user.
    The idea is to output some debug/warning messages to the Matlab command window,
    but as the user can't see them while the fullscreen stimulus window is open, we
    have to tell him/her somehow that his attention is recquired.
    This is mostly used in Screen('OpenWindow') of some tests fail or similar things...
 
    "duration" Defines duration in seconds.
    "belltype" Defines kind of info (Info = 0, Warning = 1, Error/Urgent = 2, 3 = Visual flicker test-sheet)
 
*/
void PsychVisualBell(PsychWindowRecordType *windowRecord, double duration, int belltype)
{
    double tdeadline, tcurrent, v=0;
    GLdouble color[4];
    int f=0;
    int scanline;
    CGDirectDisplayID cgDisplayID;
    float w,h;
    int visual_debuglevel;
    PsychGetCGDisplayIDFromScreenNumber(&cgDisplayID, windowRecord->screenNumber);    
    
    // Query current visual feedback level and abort, if it doesn't
    // allow requested type of visual feedback:
    visual_debuglevel = PsychPrefStateGet_VisualDebugLevel();    
    if (belltype == 0 && visual_debuglevel < 3) return;
    if (belltype == 1 && visual_debuglevel < 2) return;
    if (belltype == 2 && visual_debuglevel < 1) return;
    if (belltype == 3 && visual_debuglevel < 5) return;
    
    glGetDoublev(GL_COLOR_CLEAR_VALUE, (GLdouble*) &color);

    PsychGetAdjustedPrecisionTimerSeconds(&tdeadline);
    tdeadline+=duration;
    
    // Enable this windowRecords framebuffer as current drawingtarget:
    PsychSetDrawingTarget(windowRecord);

    w=PsychGetWidthFromRect(windowRecord->rect);
    h=PsychGetHeightFromRect(windowRecord->rect);
    
    // Clear out both buffers so it doesn't lool like junk:
    glClearColor(0,0,0,0);
    glClear(GL_COLOR_BUFFER_BIT);
    PsychOSFlipWindowBuffers(windowRecord);
    glClear(GL_COLOR_BUFFER_BIT);
    PsychOSFlipWindowBuffers(windowRecord);
    
    if (belltype==3) {
        // Test-Sheet mode: Need smaller warning triangle...
        w=w/3;
        h=h/3;
    }
    
    do {
        // Take timestamp for abort-check and driving animation:
        PsychGetAdjustedPrecisionTimerSeconds(&tcurrent);

        // Calc our visual ;-)
        v=0.5 + 0.5 * sin(tcurrent*6.283);
        
        switch (belltype) {
            case 0: // Info - Make it blue
                glClearColor(0,0,v,0);
                break;
            case 1: // Warning - Make it yellow
                glClearColor(v,v,0,0);
                break;
            case 2: // Error - Make it red
                glClearColor(v,0,0,0);
            break;
            case 3: // Test-Sheet - Don't clear...
                // Draw some flickering area (alternating black-white flicker)
                f=1-f;
                glBegin(GL_QUADS);
                glColor3f(f,f,f);
                glVertex2f(0.00*w, 0.00*h);
                glVertex2f(2.00*w, 0.00*h);
                glVertex2f(2.00*w, 3.00*h);
                glVertex2f(0.00*w, 3.00*h);
                glColor3f(0,0,v);
                glVertex2f(0.00*w, 0.00*h);
                glVertex2f(1.00*w, 0.00*h);
                glVertex2f(1.00*w, 1.00*h);
                glVertex2f(0.00*w, 1.00*h);
                glEnd();
            break;
        }
        
        if (belltype!=3) glClear(GL_COLOR_BUFFER_BIT);
        
        // Draw a yellow triangle with black border:
        glColor3f(0,0,0);
        glBegin(GL_TRIANGLES);
        glVertex2f(0.1*w, 0.1*h);
        glVertex2f(0.9*w, 0.1*h);
        glVertex2f(0.5*w, 0.9*h);
        glColor3f(1,1,0);
        glVertex2f(0.2*w, 0.2*h);
        glVertex2f(0.8*w, 0.2*h);
        glVertex2f(0.5*w, 0.8*h);
        glEnd();
        // Draw a black exclamation mark into triangle:
        glBegin(GL_QUADS);
        glColor3f(0,0,0);
        glVertex2f(0.47*w, 0.23*h);
        glVertex2f(0.53*w, 0.23*h);
        glVertex2f(0.53*w, 0.55*h);
        glVertex2f(0.47*w, 0.55*h);
        glVertex2f(0.47*w, 0.60*h);
        glVertex2f(0.53*w, 0.60*h);
        glVertex2f(0.53*w, 0.70*h);
        glVertex2f(0.47*w, 0.70*h);
        glEnd();
        
        // Initiate back-front buffer flip:
	PsychOSFlipWindowBuffers(windowRecord);
        
        // Our old VBL-Sync trick again... We need sync to VBL to visually check if
        // beamposition is locked to VBL:
        // We draw our single pixel with an alpha-value of zero - so effectively it doesn't
        // change the color buffer - just the z-buffer if z-writes are enabled...
        glColor4f(0,0,0,0);
        glBegin(GL_POINTS);
        glVertex2i(10,10);
        glEnd();        
        // This glFinish() will wait until point drawing is finished, ergo backbuffer was ready
        // for drawing, ergo buffer swap in sync with start of VBL has happened.
        glFinish();

        // Query and visualize scanline immediately after VBL onset, aka return of glFinish();
        scanline=(int) PsychGetDisplayBeamPosition(cgDisplayID, windowRecord->screenNumber);    
        if (belltype==3) {
            glColor3f(1,1,0);
            glBegin(GL_LINES);
            glVertex2f(2*w, scanline);
            glVertex2f(3*w, scanline);
            glEnd();
        }
    } while (tcurrent < tdeadline);

    // Restore clear color:
    glClearColor(color[0], color[1], color[2], color[3]);

    return;
}

/*
 * PsychPreFlipOperations()  -- Prepare windows backbuffer for flip.
 *
 * This routine performs all preparatory work to bring the windows backbuffer in its
 * final state for bufferswap as soon as possible.
 *
 * If a special stereo display mode is active, it performs all necessary drawing/setup/
 * compositing operations to assemble the final stereo display from the content of diverse
 * stereo backbuffers/AUX buffers/stereo metadata and such.
 *
 * If clearmode = Don't clear after flip is selected, the necessary backup copy of the
 * backbuffers into AUX buffers is made, so backbuffer can be restored to previous state
 * after Flip.
 *
 * This routine is called automatically by PsychFlipWindowBuffers on Screen('Flip') time as
 * well as by Screen('DrawingFinished') for manually triggered preflip work.
 *
 * -> Unifies the code in Flip and DrawingFinished.
 *
 */
void PsychPreFlipOperations(PsychWindowRecordType *windowRecord, int clearmode)
{
    int screenwidth=(int) PsychGetWidthFromRect(windowRecord->rect);
    int screenheight=(int) PsychGetHeightFromRect(windowRecord->rect);
    int stereo_mode=windowRecord->stereomode;
    int imagingMode = windowRecord->imagingMode;
    int viewid, hookchainid;
    GLint read_buffer, draw_buffer, blending_on;
    GLint auxbuffers;
    int queryState;

    // Early reject: If this flag is set, then there's no need for any processing:
    // We only continue processing textures, aka offscreen windows...
    if (windowRecord->windowType!=kPsychTexture && windowRecord->backBufferBackupDone) return;

    // We also reject any request not coming from the master thread:
    if (!PsychIsMasterThread()) return;

    // Enable this windowRecords framebuffer as current drawingtarget:
    PsychSetDrawingTarget(windowRecord);
    
    // We stop processing here if window is a texture, aka offscreen window...
    if (windowRecord->windowType==kPsychTexture) return;

    #if PSYCH_SYSTEM == PSYCH_WINDOWS
	    // Enforce a one-shot GUI event queue dispatch via this dummy call to PsychGetMouseButtonState() to
	    // make MS-Windows GUI event processing happy. Not strictly related to preflip operations, but couldn't
	    // think of a better place to guarantee periodic execution of this function without screwing too much with
	    // timing:
	    PsychGetMouseButtonState(NULL);
    #endif

    // Make sure we don't execute on an onscreen window with pending async flip, as this would interfere
    // by touching the system backbuffer -> Corruption of the flip-pending stimulus image by the new stimulus!
    if (windowRecord->flipInfo->asyncstate > 0) {
        PsychErrorExitMsg(PsychError_internal, "PsychPreFlipOperations() called on onscreen window with pending async flip?!? Forbidden!");
    }
    
    // Disable any shaders:
    PsychSetShader(windowRecord, 0);
	
    // Reset viewport to full-screen default:
    glViewport(0, 0, screenwidth, screenheight);
    glScissor(0, 0, screenwidth, screenheight);
    
    // Reset color buffer writemask to "All enabled":
    glColorMask(TRUE, TRUE, TRUE, TRUE);

    // Query number of available AUX-buffers:
    glGetIntegerv(GL_AUX_BUFFERS, &auxbuffers);

    // Set transform matrix to well-defined state:
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    
	// The following code is for traditional non-imaging rendering. Its also executed for
	// the special case of FBO backed Offscreen windows only:
	if (imagingMode == 0 || imagingMode == kPsychNeedFastOffscreenWindows) {
		// Check for compressed stereo handling...
		if (stereo_mode==kPsychCompressedTLBRStereo || stereo_mode==kPsychCompressedTRBLStereo) {
			if (auxbuffers<2) {
				PsychErrorExitMsg(PsychError_user, "OpenGL AUX buffers unavailable! The requested stereo mode doesn't work without them.\n"
								  "Either unsupported by your graphics card, or you disabled them via call to Screen('Preference', 'ConserveVRAM')?\n"
								  "On a modern graphics card, try to enable the imaging pipeline (see 'help PsychImaging') to make it work anyway.");
			}
			
			// Compressed stereo mode active. Compositing already done?
			// Backup backbuffer -> AUX buffer: We trigger this via transition to "no buffer":
			PsychSwitchCompressedStereoDrawBuffer(windowRecord, 2);
			
			// Ok, now both AUX buffers contain the final stereo content: Compose them into
			// back-buffer:
			PsychComposeCompressedStereoBuffer(windowRecord);
		}
		// Non-compressed stereo case: Mono or other stereo alg. Normal treatment applies...
		// Check if we should do the backbuffer -> AUX buffer backup, because we use
		// clearmode 1 aka "Don't clear after flip, but retain backbuffer content"
		else if (clearmode==1 && windowRecord->windowType==kPsychDoubleBufferOnscreen) {
			// Backup current assignment of read- writebuffers:
			glGetIntegerv(GL_READ_BUFFER, &read_buffer);
			glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
			blending_on = (int) glIsEnabled(GL_BLEND);
			glDisable(GL_BLEND);
			
			// Is this window equipped with a native OpenGL stereo rendering context?
			// If so, then we need to backup both backbuffers (left-eye and right-eye),
			// instead of only the monoscopic one.
			if (stereo_mode==kPsychOpenGLStereo) {
				if (auxbuffers<2) {
					PsychErrorExitMsg(PsychError_user, "OpenGL AUX buffers unavailable! dontclear=1 in Screen-Flip doesn't work without them.\n"
									  "Either unsupported by your graphics card, or you disabled them via call to Screen('Preference', 'ConserveVRAM')?\n"
									  "On a modern graphics card, try to enable the imaging pipeline (see 'help PsychImaging') to make it work anyway.");
				}
				
				glDrawBuffer(GL_AUX0);
				glReadBuffer(GL_BACK_LEFT);
				glRasterPos2i(0, screenheight);
				glCopyPixels(0, 0, screenwidth, screenheight, GL_COLOR);            
				glDrawBuffer(GL_AUX1);
				glReadBuffer(GL_BACK_RIGHT);
				glRasterPos2i(0, screenheight);
				glCopyPixels(0, 0, screenwidth, screenheight, GL_COLOR);            
			}
			else {
				// Single backbuffer: Here we provide a fallback implementation in case AUX buffers
				// are unavailable:
				if (auxbuffers < 1) {
					// No aux buffers. We use OpenGL textures as replacement solution.
					// Backup current backbuffer of current onscreen window to a texture, create
					// the texture if neccessary:
					PsychBackupFramebufferToBackingTexture(windowRecord);
				}
				else {
					// At least one aux buffer: Use it for fast backup/restore:
					glDrawBuffer(GL_AUX0);
					glReadBuffer(GL_BACK);
					glRasterPos2i(0, screenheight);
					glCopyPixels(0, 0, screenwidth, screenheight, GL_COLOR);            
				}
			}
			
			if (blending_on) glEnable(GL_BLEND);
			
			// Restore assignment of read- writebuffers:
			glReadBuffer(read_buffer);
			glDrawBuffer(draw_buffer);        
		}

		// Check if the finalizer blit chain is operational. This is the only blit chain available for preflip operations in non-imaging mode,
		// useful for encoding special information into final framebuffer images, e.g., sync lines, time stamps, cluts...
		// All other blit chains are only available in imaging mode - they need support for shaders and framebuffer objects...
		if (PsychIsHookChainOperational(windowRecord, kPsychLeftFinalizerBlit) || PsychIsHookChainOperational(windowRecord, kPsychRightFinalizerBlit)) {
			glGetIntegerv(GL_READ_BUFFER, &read_buffer);
			glGetIntegerv(GL_DRAW_BUFFER, &draw_buffer);
			blending_on = (int) glIsEnabled(GL_BLEND);
			glDisable(GL_BLEND);
			
			// Process each of the (up to two) streams:
			for (viewid = 0; viewid < ((stereo_mode == kPsychOpenGLStereo) ? 2 : 1); viewid++) {
				
				// Select drawbuffer:
				if (stereo_mode == kPsychOpenGLStereo) {
					// Quad buffered stereo: Select proper backbuffer:
					glDrawBuffer((viewid==0) ? GL_BACK_LEFT : GL_BACK_RIGHT);
				} else {
					// Mono mode: Select backbuffer:
					glDrawBuffer(GL_BACK);
				}
				
				// This special purpose blit chains can be used to encode low-level information about frames into
				// the frames or do other limited per-frame processing. Their main use (as of now) is to draw
				// the blue-line sync signal into quad-buffered windows in quad-buffered stereo mode. One could
				// use them e.g., to encode a frame index, a timestamp or a trigger signal into frames as well.
				// Encoding CLUTs for devices like the Bits++ is conceivable as well - these would be automatically
				// synchronous to frame updates and could be injected from our own gamma-table functions.
				PsychPipelineExecuteHook(windowRecord, (viewid==0) ? kPsychLeftFinalizerBlit : kPsychRightFinalizerBlit, NULL, NULL, TRUE, FALSE, NULL, NULL, NULL, NULL);				
			}
			
			// Restore blending mode:
			if (blending_on) glEnable(GL_BLEND);
			
			// Restore assignment of read- writebuffers:
			glReadBuffer(read_buffer);
			glDrawBuffer(draw_buffer);
		}
		
		// Restore modelview matrix:
		glPopMatrix();

	}	// End of traditional preflip path.
	
	if (imagingMode && imagingMode!=kPsychNeedFastOffscreenWindows) {
		// Preflip operations for imaging mode:

		// Detach any active drawing targets:
		PsychSetDrawingTarget(NULL);

		// Reset modelview matrix to identity:
		glLoadIdentity();

		// Save all state:
		glPushAttrib(GL_ALL_ATTRIB_BITS);
		
		// Disable alpha-blending:
		glDisable(GL_BLEND);
		
		// Execute post processing sequence for this onscreen window:
		
		// Is there a need for special processing on the drawBufferFBO during copy to inputBufferFBO?
		// Or are both identical?
		for (viewid = 0; viewid < ((stereo_mode > 0) ? 2 : 1); viewid++) {
			if (windowRecord->inputBufferFBO[viewid] != windowRecord->drawBufferFBO[viewid]) {
				// Separate draw- and inputbuffers: We need to copy the drawBufferFBO to its
				// corresponding inputBufferFBO, applying a special conversion operation.
				// We use this for multisample-resolve of multisampled drawBufferFBO's.
				// A simple glBlitFramebufferEXT() call will do the copy & downsample operation:
				glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, windowRecord->fboTable[windowRecord->drawBufferFBO[viewid]]->fboid);
				glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, windowRecord->fboTable[windowRecord->inputBufferFBO[viewid]]->fboid);
				glBlitFramebufferEXT(0, 0, windowRecord->fboTable[windowRecord->inputBufferFBO[viewid]]->width, windowRecord->fboTable[windowRecord->inputBufferFBO[viewid]]->height,
									 0, 0, windowRecord->fboTable[windowRecord->inputBufferFBO[viewid]]->width, windowRecord->fboTable[windowRecord->inputBufferFBO[viewid]]->height,
									 GL_COLOR_BUFFER_BIT, GL_NEAREST);
			}
		}
		
		// Reset framebuffer binding to something safe - The system framebuffer:
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
		
		// Generic image processing on viewchannels enabled?
		if (imagingMode & kPsychNeedImageProcessing) {
			// Yes. Process each of the (up to two) streams:
			for (viewid = 0; viewid < ((stereo_mode > 0) ? 2 : 1); viewid++) {
				// Processing chain enabled and non-empty?
				hookchainid = (viewid==0) ? kPsychStereoLeftCompositingBlit : kPsychStereoRightCompositingBlit;
				if (PsychIsHookChainOperational(windowRecord, hookchainid)) {
					// Hook chain ready to do its job: Execute it.      userd,blitf
					// Don't supply user-specific data, blitfunction is default blitter, unless defined otherwise in blitchain,
					// srcfbos are read-only, swizzling forbidden, 2nd srcfbo doesn't exist (only needed for stereo merge op),
					// We provide a bounce-buffer... We could bind the 2nd channel in steromode if we wanted. Should we?
					// TODO: Define special userdata struct, e.g., for C-Callbacks or scripting callbacks?
					PsychPipelineExecuteHook(windowRecord, hookchainid, NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->inputBufferFBO[viewid]]), NULL, &(windowRecord->fboTable[windowRecord->processedDrawBufferFBO[viewid]]),  (windowRecord->processedDrawBufferFBO[2]>=0) ? &(windowRecord->fboTable[windowRecord->processedDrawBufferFBO[2]]) : NULL);
				}
				else {
					// Hook chain disabled by userspace or doesn't contain any instructions.
					// Execute our special identity blit chain to transfer the data from source buffer
					// to destination buffer:
					PsychPipelineExecuteHook(windowRecord, kPsychIdentityBlit, NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->inputBufferFBO[viewid]]), NULL, &(windowRecord->fboTable[windowRecord->processedDrawBufferFBO[viewid]]), NULL);
				}
			}
		}
		
		// At this point, processedDrawBufferFBO[0 and 1] contain the per-viewchannel result of
		// user defined (or stereo) image processing.
		
		// Stereo processing: This depends on selected stereomode...
		if (stereo_mode <= kPsychOpenGLStereo || stereo_mode == kPsychDualWindowStereo || stereo_mode == kPsychFrameSequentialStereo) {
			// No stereo or quad-buffered stereo or dual-window stereo or own frame-seq stereo - Nothing to do in merge stage.
		}
		else if (stereo_mode <= kPsychAnaglyphBRStereo) {
			// Merged stereo - All work is done by the anaglyph shader that was created for this purpose
			// in pipeline setup, no geometric transform or such are needed, so we can use the default blitter:
			if (PsychIsHookChainOperational(windowRecord, kPsychStereoCompositingBlit)) {
				// Don't supply user-specific data, blitfunction is default blitter, unless defined otherwise in blitchain,
				// srcfbos are read-only, swizzling forbidden, 2nd srcfbo is right-eye channel, whereas 1st srcfbo is left-eye channel.
				// We provide a bounce-buffer as well.
				// TODO: Define special userdata struct, e.g., for C-Callbacks or scripting callbacks?
				PsychPipelineExecuteHook(windowRecord, kPsychStereoCompositingBlit, NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->processedDrawBufferFBO[0]]), &(windowRecord->fboTable[windowRecord->processedDrawBufferFBO[1]]), &(windowRecord->fboTable[windowRecord->preConversionFBO[0]]), (windowRecord->preConversionFBO[2]>=0) ? &(windowRecord->fboTable[windowRecord->preConversionFBO[2]]) : NULL);
			}
			else {
				// Hook chain disabled by userspace or doesn't contain any instructions.
				// We vitally need the compositing chain, there's no simple fallback here!
				PsychErrorExitMsg(PsychError_internal, "Processing chain for stereo processing merge operations is needed, but empty or disabled - No visual output produced! Bug?!?\n");
			}			
		}
		else {
			// Invalid stereo mode?
			PsychErrorExitMsg(PsychError_internal, "Invalid stereo mode encountered!?!");
		}
		
		// At this point we have image data ready for final post-processing and special device output formatting...
		// In mono mode: Image in preConversionFBO[0].
		// In quad-buffered stereo mode: Left eye image in preConversionFBO[0], Right eye image in preConversionFBO[1].
		// In other stereo modes: Merged image in both preConversionFBO[0] and preConversionFBO[1], both reference the same image buffer.
		// If dual window output mode is requested, the merged - or single monoscopic - image is also in both
		// preConversionFBO[0] and preConversionFBO[1], as both reference the same image buffer.
		
		// Ready to create the final content, either for drawing into a snapshot buffer or into the real system framebuffer.
		// finalizedFBO[0] is set up to take the final image for anything but quad-buffered stereo.
		// In quad-buffered mode, finalizedFBO[0] shall receive the left-eye image, finalizedFBO[1] shall receive the right-eye image.
		// Each FBO is either a real FBO for framebuffer "screenshots" or the system framebuffer for final output into the backbuffer.

		// Process each of the (up to two) streams:
		for (viewid = 0; viewid < ((stereo_mode == kPsychOpenGLStereo || stereo_mode == kPsychFrameSequentialStereo || stereo_mode == kPsychDualWindowStereo ||
		     (imagingMode & kPsychNeedDualWindowOutput)) ? 2 : 1); viewid++) {

			// Select final drawbuffer if our target is the system framebuffer:
			if (windowRecord->fboTable[windowRecord->finalizedFBO[viewid]]->fboid == 0) {
				// Final target is system backbuffer:
				if (stereo_mode == kPsychOpenGLStereo) {
					// Quad buffered stereo: Select proper backbuffer:
					glDrawBuffer((viewid==0) ? GL_BACK_LEFT : GL_BACK_RIGHT);
				} else {
					// Mono mode: Select backbuffer:
					glDrawBuffer(GL_BACK);
				}
			}

			// Output conversion needed, processing chain(s) enabled and non-empty?
			if ((imagingMode & kPsychNeedOutputConversion) && (PsychIsHookChainOperational(windowRecord, kPsychFinalOutputFormattingBlit) ||
				(PsychIsHookChainOperational(windowRecord, kPsychFinalOutputFormattingBlit0) && PsychIsHookChainOperational(windowRecord, kPsychFinalOutputFormattingBlit1)))) {
				// Output conversion needed and unified chain or dual-channel chains operational.
				// Which ones to use?
				if (PsychIsHookChainOperational(windowRecord, kPsychFinalOutputFormattingBlit0)) {
					// Dual stream chains for separate formatting of both output views are active.
					// Unified chain active as well? That would be reason for a little warning about conflicts...
					if (PsychIsHookChainOperational(windowRecord, kPsychFinalOutputFormattingBlit) && (PsychPrefStateGet_Verbosity() > 1)) {
						printf("PTB-WARNING: Both, separate chains *and* unified chain for image output formatting active! Coding bug?!? Will use separate chains as override.\n");
					}

					// Use proper per view output formatting chain:
					PsychPipelineExecuteHook(windowRecord, ((viewid > 0) ? kPsychFinalOutputFormattingBlit1 : kPsychFinalOutputFormattingBlit0), NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->preConversionFBO[viewid]]), NULL, &(windowRecord->fboTable[windowRecord->finalizedFBO[viewid]]), (windowRecord->preConversionFBO[2]>=0) ? &(windowRecord->fboTable[windowRecord->preConversionFBO[2]]) : NULL);
				}
				else {
					// Single unified formatting chain to be used:
					PsychPipelineExecuteHook(windowRecord, kPsychFinalOutputFormattingBlit, NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->preConversionFBO[viewid]]), NULL, &(windowRecord->fboTable[windowRecord->finalizedFBO[viewid]]), (windowRecord->preConversionFBO[2]>=0) ? &(windowRecord->fboTable[windowRecord->preConversionFBO[2]]) : NULL);
				}
			}
			else {
				// No conversion needed or chain disabled: Do our identity blit, but only if really needed!
				// This gets skipped in mono-mode if no conversion needed and only single-pass image processing
				// applied. In that case, the image processing stage did the final blit already.
				if (windowRecord->preConversionFBO[viewid] != windowRecord->finalizedFBO[viewid]) {
					if ((imagingMode & kPsychNeedOutputConversion) && (PsychPrefStateGet_Verbosity()>3)) printf("PTB-INFO: Processing chain(s) for output conversion disabled -- Using identity copy as workaround.\n");
					PsychPipelineExecuteHook(windowRecord, kPsychIdentityBlit, NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->preConversionFBO[viewid]]), NULL, &(windowRecord->fboTable[windowRecord->finalizedFBO[viewid]]), NULL);				
				}
			}
			
			// This special purpose blit chains can be used to encode low-level information about frames into
			// the frames or do other limited per-frame processing. Their main use (as of now) is to draw
			// the blue-line sync signal into quad-buffered windows in quad-buffered stereo mode. One could
			// use them e.g., to encode a frame index, a timestamp or a trigger signal into frames as well.
			// Encoding CLUTs for devices like the Bits++ is conceivable as well - these would be automatically
			// synchronous to frame updates and could be injected from our own gamma-table functions.
			PsychPipelineExecuteHook(windowRecord, (viewid==0) ? kPsychLeftFinalizerBlit : kPsychRightFinalizerBlit, NULL, NULL, TRUE, FALSE, NULL, NULL, &(windowRecord->fboTable[windowRecord->finalizedFBO[viewid]]), NULL);				
		}
		
		// At this point we should have either a valid snapshot of the framebuffer in the finalizedFBOs, or
		// (the common case) the final image in the system backbuffers, ready for display after swap.
		
		// Disabled debug code:
		if (FALSE) {
			windowRecord->textureNumber = windowRecord->fboTable[windowRecord->drawBufferFBO[0]]->coltexid;
			
			// Now we need to blit the new rendertargets texture into the framebuffer. We need to make
			// sure that alpha-blending is disabled during this blit operation:
			// Alpha blending not enabled. Just blit it:
			PsychBlitTextureToDisplay(windowRecord, windowRecord, windowRecord->rect, windowRecord->rect, 0, 0, 1);
			
			windowRecord->textureNumber = 0;
		}

		// Restore all state, including blending and texturing state:
		glPopAttrib();
		
		// Restore modelview matrix:
		glPopMatrix();
		
		// In dual-window stereomode or dual-window output mode, we need to copy the finalizedFBO[1] into the backbuffer of
		// the slave-window:
		if (stereo_mode == kPsychDualWindowStereo || (imagingMode & kPsychNeedDualWindowOutput)) {
			if (windowRecord->slaveWindow == NULL) {
				if (PsychPrefStateGet_Verbosity()>3) printf("PTB-INFO: Skipping master->slave blit operation in dual-window stereo mode or output mode...\n");
			}
			else {
				// Perform blit operation: This looks weird. Due to the peculiar implementation of PsychPipelineExecuteHook() we must
				// pass slaveWindow as reference, so its GL context is activated. That means we will execute its default identity
				// blit chain (which was setup in SCREENOpenWindow.c). We blit from windowRecords finalizedFBO[1] - which is a color
				// texture with the final stimulus image for slaveWindow into finalizedFBO[0], which is just a pseudo-FBO representing
				// the system framebuffer - and therefore the backbuffer of slaveWindow.
				// -> This is a bit dirty and convoluted, but its the most efficient procedure for this special case.
				PsychPipelineExecuteHook(windowRecord->slaveWindow, kPsychIdentityBlit, NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->finalizedFBO[1]]), NULL, &(windowRecord->fboTable[windowRecord->finalizedFBO[0]]), NULL);				

				// Paranoia mode: A dual-window display configuration must swap both display windows in
				// close sync with each other and the vertical retraces of their respective display heads. Due
				// to the non-atomic submission of the swap-commands this config is especially prone to one display
				// missing the VBL deadline and flipping one video refresh too late. We try to reduce the chance of
				// this happening by forcing both rendering contexts of both displays to finish rendering now. That
				// way both backbuffers will be ready for swap and likelihood of a asymetric miss is much lower.
				// This may however cost a bit of performance on some setups...
				glFinish();
				
				// Restore current context and glFinish it as well:
				PsychSetGLContext(windowRecord);
				glFinish();
			}
		}
		
	}	// End of preflip operations for imaging mode:
    
    // Tell Flip that backbuffer backup has been done already to avoid redundant backups. This is a bit of a
    // unlucky name. It actually signals that all the preflip processing has been done, the old name is historical.
    windowRecord->backBufferBackupDone = true;

    // End time measurement for any previously submitted rendering commands if a
    // GPU rendertime query was requested (See Screen('GetWindowInfo', ..); for infoType 5.
    if (windowRecord->gpuRenderTimeQuery) {
	// Unfinished query? If so, finish it.
	glGetQueryiv(GL_TIME_ELAPSED_EXT, GL_CURRENT_QUERY, &queryState);
	if (queryState > 0) glEndQuery(GL_TIME_ELAPSED_EXT);
    }

    return;
}

/*
 * PsychPostFlipOperations()  -- Prepare windows backbuffer after flip.
 *
 * This routine performs all preparatory work to bring the windows backbuffer in its
 * proper state for drawing the next stimulus after bufferswap has completed.
 *
 * If a special stereo display mode is active, it performs all necessary setup/
 * operations to restore the content of diverse stereo backbuffers/AUX buffers/stereo
 * metadata and such.
 *
 * If clearmode = Don't clear after flip is selected, the backbuffer is restored to previous state
 * after Flip from the AUX buffer copies.
 *
 * This routine is called automatically by PsychFlipWindowBuffers on Screen('Flip') time after
 * the flip has happened.
 *
 * -> Unifies the code in Flip and DrawingFinished.
 *
 */
void PsychPostFlipOperations(PsychWindowRecordType *windowRecord, int clearmode)
{
	GLenum glerr;
    int screenwidth=(int) PsychGetWidthFromRect(windowRecord->rect);
    int screenheight=(int) PsychGetHeightFromRect(windowRecord->rect);
    int stereo_mode=windowRecord->stereomode;
	GLint blending_on, auxbuffers;

    // Switch to associated GL-Context of windowRecord:
    PsychSetGLContext(windowRecord);

	// Imaging pipeline off?
	if (windowRecord->imagingMode==0 || windowRecord->imagingMode == kPsychNeedFastOffscreenWindows) {
		// Imaging pipeline disabled: This is the old-style way of doing things:
		
		// Set transform matrix to well-defined state:
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		
		// Vertical compression stereo active? This needs special treatment...
		if (stereo_mode==kPsychCompressedTLBRStereo || stereo_mode==kPsychCompressedTRBLStereo) {
			// Yes. We reset the active stereo buffer to 2 == none selected.
			windowRecord->stereodrawbuffer=2;
			// In clearmode==1, aka retain we don't do anything. This way the AUX buffers
			// restore the preflip state automatically. clearmode=2 is undefined by definition ;-)
			if (clearmode==0) {
				// clearmode 0 active. Sterobuffers shall be cleared on flip. We just
				// reset the dirty-flags of the AUX buffers, so backbuffer gets cleared
				// on first use after selection of a new stereo draw buffer:
				windowRecord->auxbuffer_dirty[0]=FALSE;
				windowRecord->auxbuffer_dirty[1]=FALSE;
			}
		}
		// In other stereo modes and mono mode, we don't need to play backbuffer-AUX buffer games,
		// just treat'em as in mono case...
		else if (clearmode!=2) {
			// Reinitialization of back buffer for drawing of next stim requested:
			if (clearmode==1) {
				// We shall not clear the back buffer(s), but restore them to state before "Flip",
				// so previous stim can be incrementally updated where this makes sense.
				// Copy back our backup-copy from AUX buffers:
				blending_on = (int) glIsEnabled(GL_BLEND);
				if (blending_on) glDisable(GL_BLEND);
				
				// Need to do it on both backbuffers when OpenGL native stereo is enabled:
				if (stereo_mode==kPsychOpenGLStereo) {
					glDrawBuffer(GL_BACK_LEFT);
					glReadBuffer(GL_AUX0);
					glRasterPos2i(0, screenheight);
					glCopyPixels(0, 0, screenwidth, screenheight, GL_COLOR);
					glDrawBuffer(GL_BACK_RIGHT);
					glReadBuffer(GL_AUX1);
					glRasterPos2i(0, screenheight);
					glCopyPixels(0, 0, screenwidth, screenheight, GL_COLOR);
				}
				else {
					// At least one AUX buffer supported?
					glGetIntegerv(GL_AUX_BUFFERS, &auxbuffers);
					if (auxbuffers > 0) {
						// Restore backbuffer from aux buffer 0:
						glDrawBuffer(GL_BACK);
						glReadBuffer(GL_AUX0);
						glRasterPos2i(0, screenheight);
						glCopyPixels(0, 0, screenwidth, screenheight, GL_COLOR);
					}
					else {
						// Restore backbuffer from backing shadow texture:
						glDrawBuffer(GL_BACK);
						PsychBlitTextureToDisplay(windowRecord, windowRecord, windowRecord->rect, windowRecord->rect, 0, 0, 1);
					}
				}
				
				if (blending_on) glEnable(GL_BLEND);
			}
			else {
				// Clearing (both)  back buffer requested:
				if (stereo_mode==kPsychOpenGLStereo) {
					glDrawBuffer(GL_BACK_LEFT);
					PsychGLClear(windowRecord);
					glDrawBuffer(GL_BACK_RIGHT);
					PsychGLClear(windowRecord);
				}
				else {
					glDrawBuffer(GL_BACK);
					PsychGLClear(windowRecord);
				}
			}
		}
		
		// Restore modelview matrix:
		glPopMatrix();
	} // End of traditional postflip implementation for non-imaging mode:
	
	// Imaging pipeline enabled?
	if (windowRecord->imagingMode > 0 && windowRecord->imagingMode != kPsychNeedFastOffscreenWindows) {
		// Yes. This is rather simple. In dontclear=2 mode we do nothing, except reenable
		// the windowRecord as drawing target again. In dontclear=1 mode ditto, because
		// our backing store FBO's already retained a backup of the preflip-framebuffer.
		// Only in dontclear = 0 mode, we need to clear the backing FBO's:		
		if (clearmode==0) {
			// Select proper viewport and cliprectangles for clearing:
			PsychSetupView(windowRecord, FALSE);
			
			// Bind left view (or mono view) buffer:
			glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, windowRecord->fboTable[windowRecord->drawBufferFBO[0]]->fboid);
			// and clear it:
			PsychGLClear(windowRecord);
			
			if (windowRecord->stereomode > 0) {
				// Bind right view buffer for stereo mode:
				glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, windowRecord->fboTable[windowRecord->drawBufferFBO[1]]->fboid);
				// and clear it:
				PsychGLClear(windowRecord);
			}
		}
		
		// Select proper rendertarget for further drawing ops - restore preflip state:
		PsychSetDrawingTarget(windowRecord);
	}

	// Peform extensive checking for OpenGL errors, unless instructed not to do so:
	if (!(PsychPrefStateGet_ConserveVRAM() & kPsychAvoidCPUGPUSync)) {
		glerr = glGetError();
		if (glerr != GL_NO_ERROR) {
			if (glerr == GL_OUT_OF_MEMORY) {
				// Special case: Out of memory after Flip + Postflip operations.
				printf("PTB-Error: The OpenGL graphics hardware encountered an out of memory condition!\n");
				printf("PTB-Error: One cause of this could be that you are running your display at a too\n");
				printf("PTB-Error: high resolution and/or use Anti-Aliasing with a multiSample value that\n");
				printf("PTB-Error: your gfx-card can't handle at the current display resolution. If this is\n");
				printf("PTB-Error: the case, you may have to reduce multiSample level or display resolution.\n");
				printf("PTB-Error: It may help to quit and restart Matlab or Octave before continuing.\n");
			}
			else {
				printf("PTB-Error: The OpenGL graphics hardware encountered the following OpenGL error after flip: %s.\n", gluErrorString(glerr));
			}
		}
		
		PsychTestForGLErrors();
	}
	
	// Fixup possible low-level framebuffer layout changes caused by commands above this point. Needed from native 10bpc FB support to work reliably.
	PsychFixupNative10BitFramebufferEnableAfterEndOfSceneMarker(windowRecord);
	
    // Done.
    return;
}

PsychWindowRecordType* PsychGetDrawingTarget(void)
{
	return(currentRendertarget);
}

/* PsychSetDrawingTarget - Set the target window for following drawing ops.
 *
 * Set up 'windowRecord' as the target window for all drawing operations.
 *
 * This routine is usually called from the Screen drawing- and userspace OpenGL <-> Screen
 * state transition routines to setup a specific PTB window as drawing target.
 *
 * It is also called by Screen's internal special image processing routines (e.g,
 * 'TransformTexture' and preparation routines for OpenGL double-buffer swaps to
 * *disable* a window as drawing target, so the low-level internal code is free
 * to do whatever it wants with the system framebuffer or OpenGL FBO's without
 * the danger of interfering/damaging the integrity of onscreen windows and offscreen
 * windows/textures.
 *
 * Basic usage is one of three ways:
 *
 * * PsychSetDrawingTarget(windowRecord) to prepare drawing into the framebuffer of
 * windowRecord, including all behind-the-scenes management, activating the associated
 * OpenGL context for that window and setting up viewport, scissor and projection/modelview
 * matrices etc.
 *
 * * PsychSetDrawingTarget((PsychWindowRecordType*) 0x1) to safely reset the drawing target to "None". This will
 * perform all relevant tear-down actions (switching off FBOs, performing backbuffer backups etc.)
 * for the previously active drawing target, then setting the current drawing target to NULL.
 * This command is to be used by PTB internal routines if they need to be able to do
 * whatever they want with the system backbuffer or FBO's via low-level OpenGL calls,
 * without needing to worry about possible side-effects or image corruption in any
 * user defined onscreen/offscreen windows or textures. This is used in routines like
 * 'Flip', 'OpenWindow', 'OpenOffscreenWindow', 'TransformTexture' etc.
 * After this call, the current OpenGL context binding will be undefined! Or to be more
 * accurate: If no window was active then maybe no context will be bound -- Any OpenGL
 * command would cause a crash! If a window was active then that windows context will
 * be bound -- probably not what you want, unless you carefully verified it *is* what
 * you want! ==> Check your assumption wrt. bound context or use PsychSetGLContext()
 * to explicitely set the context you need!
 *
 * * PsychSetDrawingTarget(NULL) is a hard-reset, like the (0x1) case, but without
 * performing sensible tear-down actions. Wrong usage will leave Screen in an undefined
 * state! All current uses of this call have been carefully audited for correctness,
 * usually you don't need this!
 * 
 * The implementation contains two pathways of execution: One for use of imaging pipeline,
 * i.e., with FBO backed framebuffers -- this is the preferred way on modern hardware,
 * as it is more flexible, robust, simpler and faster. For old hardware and non-imaging
 * mode there is a slow path that tries to emulate FBO's with old OpenGL 1.1 mechanisms
 * like glCopyTexImage() et al. This one is relatively limited and inflexible, slow
 * and convoluted!
 *
 * FastPath:
 *
 * If windowRecord corresponds to an onscreen window, the standard framebuffer is
 * selected as drawing target when imagingMode == Use fast offscreen windows, otherwise
 * (full imaging pipe) the FBO of the windows virtual framebuffer is bound.
 * If 'windowRecord' corresponds to a Psychtoolbox texture (or Offscreen Window), we
 * bind the texture as OpenGL framebuffer object, so we have render-to-texture functionality.
 *
 * This requires support for EXT_Framebuffer_object extension, ie., OpenGL 1.5 or higher.
 * On OS/X it requires Tiger 10.4.3 or later.
 *
 * SlowPath:
 *
 * Textures and offscreen windows are implemented via standard OpenGL textures, but as
 * OpenGL FBO's are not available (or disabled), we use the backbuffer as both, backbuffer
 * of an onscreen window, and as a framebuffer for offscreen windows/textures when drawing
 * to them. The routine performs switching between windows (onscreen or offscreen) by
 * saving the backbuffer of the previously active rendertarget into an OpenGL texture via
 * glCopyTexImage() et al., then initializing the backbuffer with the content of the texture
 * of the new drawingtarget by blitting the texture into the framebuffer. Lots of care
 * has to be taken to always backup/restore from/to the proper backbuffer ie. the proper
 * OpenGL context (if multiple are used), to handle the case of transposed or inverted
 * textures (e.g, quicktime engine, videocapture engine, Screen('MakeTexture')), and
 * to handle the case of TEXTURE_2D textures on old hardware that doesn't support rectangle
 * textures! This is all pretty complex and convoluted.
 *
 *
 * This routine only performs state-transitions if necessary, in order to save expensive
 * state switches. It tries to be lazy and avoid work!
 *
 * A special case is calls of this routine from background worker threads not equal
 * to the Matlab/Octave/PTB main execution thread. These threads are part of the async
 * flip implementation on OS/X and Linux. They call code that sometimes calls into this
 * routine. The system is designed to behave properly if this routine just return()'s without
 * doing anything when called from such a workerthread. That's why we check and early-exit
 * in case of non-master thread invocation.
 * 
 */
void PsychSetDrawingTarget(PsychWindowRecordType *windowRecord)
{
    static unsigned int	recursionLevel = 0;
    PsychWindowRecordType *parentRecord;
    int	twidth, theight;
    psych_bool EmulateOldPTB = PsychPrefStateGet_EmulateOldPTB();
    psych_bool oldStyle = (PsychPrefStateGet_ConserveVRAM() & kPsychUseOldStyleAsyncFlips) ? TRUE : FALSE;

    // Are we called from the main interpreter thread? If not, then we return
    // immediately (No-Op). Worker threads for async flip don't expect this
    // subroutine to execute:
    if (!PsychIsMasterThread()) return;

    // Called from main thread --> Work to do.

    // Increase recursion level count:
    recursionLevel++;
    
    // Is this a recursive call?
    if (recursionLevel>1) {
        // Yep. Then we just do nothing:
        recursionLevel--;
        return;
    }
    
	if ((currentRendertarget == NULL) && (windowRecord == (PsychWindowRecordType *) 0x1)) {
		// Fast exit: No rendertarget set and save reset to "none" requested.
		// Nothing special to do, just revert to NULL case:
		windowRecord = NULL;
	}
	
	// Make sure currentRendertargets context is active if currentRendertarget is non-NULL:
	if (currentRendertarget) {
		PsychSetGLContext(currentRendertarget);
	}
	
	// windowRecord or NULL provided? NULL would mean a warm-start. A value of 0x1 means
	// to backup the current state of bound 'currentRendertarget', then reset to a NULL
	// target, ie. no target. This is like warmstart, but binding any rendertarget later
	// will do the right thing, instead of "forgetting" state info about currentRendertarget.
    if (windowRecord) {
        // State transition requested?
        if (currentRendertarget != windowRecord) {
			// Need to do a switch between drawing target windows:

			// Soft reset required?
			if (windowRecord == (PsychWindowRecordType *) 0x1) {
				// Special case: No new rendertarget, just request to backup the old
				// one and leave it in a tidy, consistent state, then reset to NULL
				// binding. We achieve this by turning windowRecord into a NULL request and
				// unbinding any possibly bound FBO's:
				windowRecord = NULL;

				// Bind system framebuffer if FBO's supported on this system:
				if (glBindFramebufferEXT) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
			}

			// Special safe-guards for setting a new drawingtarget during active async flip operations needed?
			if (windowRecord && (asyncFlipOpsActive > 0)) {
				// Yes. At least one async flip in progress and want to bind windowRecord.
				//
				// Assumption: We don't need to worry about the currentRendertarget, because
				// either currentRendertarget is NULL, ie. there isn't any, or if it is non-NULL,
				// that means PsychSetDrawingTarget() was executed successfully on currentRendertarget
				// before this invocation. We always start with currentRenderTarget == NULL and no
				// OpenGL context bound to the masterthread immediately after scheduling an async-flip,
				// (see AsyncFlipBegin-implementation in PsychFlipWindowBuffersIndirect()). The fact
				// that we managed to transition from NULL-target / NULL-context to a real
				// currentRenderTarget while an async flip was pending, means that currentRenderTarget
				// is FBO-backed with sufficiently enabled imaging pipeline.
				//
				// We just need to find out if it is safe to bind windowRecord during async-flip:
				// Find the parent window of this windowRecord, ie.,
				// the onscreen window which "owns" the relevant OpenGL context and system framebuffer.
				// This can be the windowRecord itself if it is an onscreen window:
				parentRecord = PsychGetParentWindow(windowRecord);

				// No problem if there aren't any pending async-flips on the parentRecord onscreen window:
				if ((parentRecord->flipInfo) && (parentRecord->flipInfo->asyncstate == 1)) {
					// Async flip pending on relevant target OpenGL context / framebuffer combo.

					// If this is the oldStyle async-flip implementation it is game-over, as we
					// can't use the master OpenGL context, it is owned exclusively by the flipperThread:
					if (oldStyle) {
						printf("PTB-ERROR: Tried to draw into a window or texture while associated parent window has an async-flip pending\n");
						printf("PTB-ERROR: and the legacy async-flip implementation is active! Disable the legacy implementation by clearing the\n");
						printf("PTB-ERROR: kPsychUseOldStyleAsyncFlips flag in your settings to make this work. See 'help ConserveVRAMSettings'.\n");
						printf("PTB-ERROR: Operation aborted, expect corrupted visual stimuli until you fix this.\n\n");

						recursionLevel--;
						return;
					}

					// New style async-flip implementation. We can use the master OpenGL context anytime,
					// for rendering into whatever, as the flipper thread has its own dedicated context.
					// We are not allowed to touch the system framebuffer of our parentRecord onscreen window,
					// so windowRecord must be a FBO backed offscreen surface with imaging pipeline sufficiently
					// enabled and configured.

					// Onscreen windows need full pipeline enabled for FBO based drawBufferFBO's:
					if (PsychIsOnscreenWindow(windowRecord) && !(windowRecord->imagingMode & kPsychNeedFastBackingStore)) {
						// Nope. Game over:
						printf("PTB-ERROR: Tried to draw into an onscreen window while it has an async-flip pending.\n");
						printf("PTB-ERROR: This is only allowed if you enable the Psychtoolbox imaging pipeline (see 'help PsychImaging').\n");
						printf("PTB-ERROR: The pipeline is currently not fully enabled by your script, so drawing won't work.\n");
						printf("PTB-ERROR: Operation aborted, expect corrupted visual stimuli until you fix this.\n\n");

						recursionLevel--;

						PsychErrorExitMsg(PsychError_user, "Tried to draw into onscreen window with async flip pending and imaging pipeline off. Forbidden!");
						return;
					}

					if (!PsychIsOnscreenWindow(windowRecord) &&
					    !(windowRecord->imagingMode & kPsychNeedFastBackingStore) &&
					    !(windowRecord->imagingMode & kPsychNeedFastOffscreenWindows)) {
						// Nope. Game over:
						printf("PTB-ERROR: Tried to draw into an offscreen window or texture while parent window has an async-flip pending.\n");
						printf("PTB-ERROR: This is only allowed if you enable the full Psychtoolbox imaging pipeline (see 'help PsychImaging'),\n");
						printf("PTB-ERROR: or at least support for fast offscreen windows. As neither of these is enabled at the moment,\n");
						printf("PTB-ERROR: drawing won't work. Operation aborted, expect corrupted visual stimuli until you fix this.\n\n");

						recursionLevel--;
						PsychErrorExitMsg(PsychError_user, "Tried to draw into offscreen window or texture with async flip pending and imaging pipeline off. Forbidden!");
						return;
					}
				}
			} // End of asyncflip safeguards. Everything ready to go if we get here.

			// Check if the imaging pipeline is enabled for this window. If so, we will use
			// the fast FBO based rendertarget implementation - unless windowRecord is a NULL target,
			// in which case we're done already:
			if (windowRecord && ((windowRecord->imagingMode & kPsychNeedFastBackingStore) || (windowRecord->imagingMode == kPsychNeedFastOffscreenWindows))) {
				// Imaging pipeline (at least partially) active for this window. Use OpenGL framebuffer objects: This is the fast-path!

				// Switch to new context if needed: This will unbind any pending FBO's in old context, if any:
				PsychSetGLContext(windowRecord);

				// Transition to offscreen rendertarget?
				if (windowRecord->windowType == kPsychTexture) {
					// Yes. Need to bind the texture as framebuffer object. This only works for rectangle textures.
					if (PsychGetTextureTarget(windowRecord)!=GL_TEXTURE_RECTANGLE_EXT) {
						PsychErrorExitMsg(PsychError_user, "You tried to draw into a special power-of-two offscreen window or texture. Sorry, this is not supported.");
					}
					
					// It also only works on RGB or RGBA textures, not Luminance or LA textures, and the texture needs to be upright.
					// PsychNormalizeTextureOrientation takes care of swapping it upright and converting it into a RGB or RGBA format,
					// if needed. Only if it were an upright non-RGB(A) texture, it would slip through this and trigger an error abort
					// in the following PsychCreateShadowFBO... call. This however can't happen with textures created by 'OpenOffscreenWindow',
					// textures from the Quicktime movie engine, the videocapture engine or other internal sources. Textures created via
					// MakeTexture will be auto-converted as well, unless some special flags to MakeTexture are given.
					// --> The user code needs to do something very unusual and special to trigger an error abort here, and if it triggers
					// one, it will abort with a helpful error message, telling how to fix the problem very simply.
					PsychSetShader(windowRecord, 0);
					PsychNormalizeTextureOrientation(windowRecord);
					
					// Do we already have a framebuffer object for this texture? All textures start off without one,
					// because most textures are just used for drawing them, not drawing *into* them. Therefore we
					// only create a full blown FBO on demand here.
					PsychCreateShadowFBOForTexture(windowRecord, TRUE, -1);

					// Switch to FBO for given texture or offscreen window:
					glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, windowRecord->fboTable[0]->fboid);

				} // Special setup for offscreen windows or textures finished.
				else {
					// Bind onscreen window as drawing target:
					if (windowRecord->imagingMode == kPsychNeedFastOffscreenWindows) {
						// Only fast offscreen windows active: Onscreen window is the system framebuffer.
						// Revert to it:
						glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
					}
					else {
						// Full pipeline active:
						
						// Pending async flip on the target onscreen window? Drawing to such a windows
						// drawbuffer FBO while async flip is pending is only safe if the flip executes
						// with a dontclear == 2 flag, ie., without executing PsychPostFlipOperations()
						// at end of swap. Other dontclear modes would involve a race between the masterthread
						// rendering a stimulus to the drawBufferFBO and the async flipper thread clearing
						// the drawBufferFBO, with rather hilarious results, depending on who wins the race.
						// We check if we have an async flip + dontclear != 2 and warn the user about possible
						// trouble in such a config:
						if ((windowRecord->flipInfo->dont_clear != 2) && (windowRecord->flipInfo->asyncstate > 0) &&
						    (PsychPrefStateGet_Verbosity() > 1)) {
							printf("PTB-WARNING: You are drawing to an onscreen window while an async flip is pending on it and the\n");
							printf("PTB-WARNING: async flip is executed with the 'dontclear' flag set to something else than 2.\n");
							printf("PTB-WARNING: This will likely lead to undefined stimuli - visual corruption. Please set the\n");
							printf("PTB-WARNING: 'dontclear' flag in Screen('AsyncFlipBegin', ...) to 2 to avoid this problem.\n");
						}
                        
						// We either bind the drawBufferFBO for the left channel or right channel, depending
						// on stereo mode and selected stereo buffer:
						if ((windowRecord->stereomode > 0) && (windowRecord->stereodrawbuffer == 1)) {
							// We are in stereo mode and want to draw into the right-eye channel. Bind FBO-1
							glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, windowRecord->fboTable[windowRecord->drawBufferFBO[1]]->fboid);
						}
						else {
							// We are either in stereo mode and want to draw into left-eye channel or we are
							// in mono mode. Bind FBO-0:
							glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, windowRecord->fboTable[windowRecord->drawBufferFBO[0]]->fboid);
						}
					}
				}

			// Fast path for rendertarget switch finished.
		} // End of fast-path: FBO based processing...
		else {
                // Use standard OpenGL without framebuffer objects for drawing target switch:
                // This code path is executed when the imaging pipeline is disabled. It only uses
		// OpenGL 1.1 functionality so it should work on any piece of gfx-hardware:
				
                // Whatever is bound at the moment needs to be backed-up into a texture...
                // If currentRendertarget is NULL then we've got nothing to back up.
		// If currentRendertarget is using the imaging pipeline in any way, then there's also no
		// need for any backups, as all textures/offscreen windows are backed by FBO's and the
		// system framebuffer is just used as backingstore for onscreen windows, ie., no need
		// to ever backup system framebuffer into any kind of texture based storage.
		// Therefore skip this if any imaging mode is active (i.e., imagingMode is non-zero):
                if (currentRendertarget && (currentRendertarget->imagingMode == 0)) {
                    // There is a bound render target in non-imaging mode: Any backups of its current backbuffer to some
					// texture backing store needed?
                    if (currentRendertarget->windowType == kPsychTexture || (windowRecord && (windowRecord->windowType == kPsychTexture))) {
                        // Ok we transition from- or to a texture. We need to backup the old content:
                        if (EmulateOldPTB) {
                            // OS-9 emulation: frontbuffer = framebuffer, backbuffer = offscreen scratchpad
                            if (PsychIsOnscreenWindow(currentRendertarget)) {
                                // Need to read the content of the frontbuffer to create the backup copy:
                                glReadBuffer(GL_FRONT);
                                glDrawBuffer(GL_FRONT);
                            }
                            else {
                                // Need to read the content of the backbuffer (scratch buffer for offscreen windows) to create the backup copy:
                                glReadBuffer(GL_BACK);
                                glDrawBuffer(GL_BACK);
                            }
                        }
                        
                        // In emulation mode for old PTB, we only need to back up offscreen windows, as they
                        // share the backbuffer as scratchpad. Each onscreen window has its own frontbuffer, so
                        // it will be unaffected by the switch --> No need to backup & restore.
                        if (!EmulateOldPTB || (EmulateOldPTB && !PsychIsOnscreenWindow(currentRendertarget))) {
							// Call helper routine defined below:
							PsychBackupFramebufferToBackingTexture(currentRendertarget);
                        } // Backbuffer -> Texture backup code.
                    } // Transition from- or to a texture.
                } // currentRenderTarget non-NULL.
				
				// At this point we're done with the context and stuff of the old currentRendertarget.
				// Everything backed up.
				
				// A real new rendertarget requested?
				if (windowRecord) {
					// Yes. Activate its OpenGL context:
					PsychSetGLContext(windowRecord);

					// We only blit when a texture was involved, either as previous rendertarget or as new rendertarget:
					if (windowRecord->windowType == kPsychTexture || (currentRendertarget && currentRendertarget->windowType == kPsychTexture)) {
						// OS-9 emulation: frontbuffer = framebuffer, backbuffer = offscreen scratchpad
						if (EmulateOldPTB) {
							// OS-9 emulation: frontbuffer = framebuffer, backbuffer = offscreen scratchpad
							if (PsychIsOnscreenWindow(windowRecord)) {
								// Need to write the content to the frontbuffer to restore from the backup copy:
								glReadBuffer(GL_FRONT);
								glDrawBuffer(GL_FRONT);
							}
							else {
								// Need to write the content to the backbuffer (scratch buffer for offscreen windows) to restore from the backup copy:
								glReadBuffer(GL_BACK);
								glDrawBuffer(GL_BACK);
							}
						}
						
						// In emulation mode for old PTB, we only need to restore offscreen windows, as they
						// share the backbuffer as scratchpad. Each onscreen window has its own frontbuffer, so
						// it will be unaffected by the switch --> No need to backup & restore.
						if (!EmulateOldPTB || (EmulateOldPTB && !PsychIsOnscreenWindow(windowRecord))) {
							// Setup viewport and projections to fit new dimensions of new rendertarget:
							PsychSetupView(windowRecord, TRUE);
							glPushMatrix();
							glLoadIdentity();

							// Disable any shaders:
							PsychSetShader(windowRecord, 0);
							
							// Now we need to blit the new rendertargets texture into the framebuffer. We need to make
							// sure that alpha-blending is disabled during this blit operation:
							if (glIsEnabled(GL_BLEND)) {
								// Alpha blending enabled. Disable it, blit texture, reenable it:
								glDisable(GL_BLEND);
								PsychBlitTextureToDisplay(windowRecord, windowRecord, windowRecord->rect, windowRecord->rect, 0, 0, 1);
								glEnable(GL_BLEND);
							}
							else {
								// Alpha blending not enabled. Just blit it:
								PsychBlitTextureToDisplay(windowRecord, windowRecord, windowRecord->rect, windowRecord->rect, 0, 0, 1);
							}
							
							glPopMatrix();
							
							// Ok, the framebuffer has been initialized with the content of our texture.
						}
					}	// End of from- to- texture/offscreen window transition...
				}	// End of setup of a real new rendertarget windowRecord...

                // At this point we should have the image of our drawing target in the framebuffer.
                // If this transition didn't involve a switch from- or to a texture aka offscreen window,
                // then the whole switching up to now was a no-op... This way, we optimize for the common
                // case: No drawing to Offscreen windows at all, but proper use of other drawing functions
                // or of MakeTexture.
            } // End of switching code for imaging vs. non-imaging.
            
			// Common code after fast- or slow-path switching:
			
            // Setup viewport, clip rectangle and projections to fit new dimensions of new drawingtarget:
            if (windowRecord) PsychSetupView(windowRecord, FALSE);
			
            // Update our bookkeeping, set windowRecord as current rendertarget:
            currentRendertarget = windowRecord;
			
			// Transition finished.
        } // End of transition code.
    } // End of if(windowRecord) - then branch...
    else {
        // windowRecord==NULL. Reset of currentRendertarget and framebufferobject requested:

	// Bind system framebuffer if FBO's supported on this system:
        if (glBindFramebufferEXT && currentRendertarget) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

	// Reset current rendertarget to 'none':
        currentRendertarget = NULL;
    }

    // Decrease recursion level tracker:
    recursionLevel--;
    
    return;
}

/* PsychSetupView()  -- Setup proper viewport, clip rectangle and projection
 * matrix for specified window.
 *
 * Usually call with useRawFramebufferSize = FALSE, so the clientrect of the
 * windows/textures effective user-visible drawing area is used.
 *
 * useRawFramebufferSize = TRUE: Use full backbuffer area of window, e.g., for
 * setup inside imaging pipeline or for other non-user controlled rendering.
 *
 */
void PsychSetupView(PsychWindowRecordType *windowRecord, psych_bool useRawFramebufferSize)
{
    PsychRectType rect;
    PsychCopyRect(rect, (useRawFramebufferSize) ? windowRecord->rect : windowRecord->clientrect);

    // Set viewport and scissor rectangle to windowsize:
    glViewport(0, 0, (int) PsychGetWidthFromRect(rect), (int) PsychGetHeightFromRect(rect));
    glScissor(0, 0, (int) PsychGetWidthFromRect(rect), (int) PsychGetHeightFromRect(rect));
    
    // Setup projection matrix for a proper orthonormal projection for this framebuffer or window:
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(rect[kPsychLeft], rect[kPsychRight], rect[kPsychBottom], rect[kPsychTop]);

    // Switch back to modelview matrix, but leave it unaltered:
    glMatrixMode(GL_MODELVIEW);
    return;
}

/* PsychSetupClientRect() -- Compute windows clientrect from raw backbuffer size rect.
 */
void PsychSetupClientRect(PsychWindowRecordType *windowRecord)
{
	// Define windows clientrect. It is a copy of windows rect, but stretched or compressed
    // to twice or half the width or height of the windows rect, depending on the special size
    // flags. clientrect is used as reference for all size query functions Screen('Rect'), Screen('WindowSize')
    // and for all Screen 2D drawing functions:
	PsychMakeRect(windowRecord->clientrect,
                  windowRecord->rect[kPsychLeft], windowRecord->rect[kPsychTop],
                  windowRecord->rect[kPsychLeft] + PsychGetWidthFromRect(windowRecord->rect) * ((windowRecord->specialflags & kPsychTwiceWidthWindow) ? 2 : 1) / ((windowRecord->specialflags & kPsychHalfWidthWindow) ? 2 : 1),
                  windowRecord->rect[kPsychTop] + PsychGetHeightFromRect(windowRecord->rect) / ((windowRecord->specialflags & kPsychHalfHeightWindow) ? 2 : 1));
    return;
}

/* PsychBackupFramebufferToBackingTexture(PsychWindowRecordType *backupRendertarget)
 * Copy current content of current backbuffer into a texture of matching size and RGBA8
 * format.
 *
 * This is used by PsychSetDrawingTarget() for target window switches when no imaging pipeline
 * aka OpenGL framebuffer objects are available. Mostly for offscreen <-> onscreen window switching
 * and offscreen <-> offscreen window switching.
 *
 * It is also used by PsychPreFlipOperations() in non-imaging mode when clearmode 1 requires a
 * backbuffer backup/restore but the system doesn't support AUX buffers which are the preferred
 * solution in such a case.
 *
 */
void PsychBackupFramebufferToBackingTexture(PsychWindowRecordType *backupRendertarget)
{
	int twidth, theight;
	
	// Already a shadow-texture available as backing store?
	if (backupRendertarget->textureNumber == 0) {
		// This one is an onscreen window that doesn't have a shadow-texture yet. Create a suitable one.
		glGenTextures(1, &(backupRendertarget->textureNumber));
		glBindTexture(PsychGetTextureTarget(backupRendertarget), backupRendertarget->textureNumber);

		// If this system only supports power-of-2 textures, then we'll need a little trick:
		if (PsychGetTextureTarget(backupRendertarget)==GL_TEXTURE_2D) {
			// Ok, we need to create an empty texture of suitable power-of-two size:
			// Now we can do subimage texturing...
			twidth=1; while(twidth < (int) PsychGetWidthFromRect(backupRendertarget->rect)) { twidth = twidth * 2; };
			theight=1; while(theight < (int) PsychGetHeightFromRect(backupRendertarget->rect)) { theight = theight * 2; };
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, twidth, theight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
			glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, (int) PsychGetWidthFromRect(backupRendertarget->rect), (int) PsychGetHeightFromRect(backupRendertarget->rect));
		}
		else {
			// Supports rectangle textures. Just create texture as copy of framebuffer:
			glCopyTexImage2D(PsychGetTextureTarget(backupRendertarget), 0, GL_RGBA8, 0, 0, (int) PsychGetWidthFromRect(backupRendertarget->rect), (int) PsychGetHeightFromRect(backupRendertarget->rect), 0); 
		}
	}
	else {
		// Texture for this one already exist: Bind and update it:
		twidth  = (int) PsychGetWidthFromRect(backupRendertarget->rect);
		theight = (int) PsychGetHeightFromRect(backupRendertarget->rect);
		
		// If this is a texture in non-normal orientation, we need to swap width and height, and reset orientation
		// to upright:
		if (!PsychIsOnscreenWindow(backupRendertarget)) {
			// Texture. Handle size correctly:
			if ((backupRendertarget->textureOrientation <= 1) && (PsychGetTextureTarget(backupRendertarget)==GL_TEXTURE_2D)) {
				// Transposed power of two texture. Need to realloc texture...
				twidth=1; while(twidth < (int) PsychGetWidthFromRect(backupRendertarget->rect)) { twidth = twidth * 2; };
				theight=1; while(theight < (int) PsychGetHeightFromRect(backupRendertarget->rect)) { theight = theight * 2; };
				glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, twidth, theight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
				
				// Reassign real size:
				twidth  = (int) PsychGetWidthFromRect(backupRendertarget->rect);
				theight = (int) PsychGetHeightFromRect(backupRendertarget->rect);
				
				backupRendertarget->surfaceSizeBytes = 4 * twidth * theight; 
			}
			
			// After this backup-op, the texture orientation will be a nice upright one:
			backupRendertarget->textureOrientation = 2;
		}
		
		glBindTexture(PsychGetTextureTarget(backupRendertarget), backupRendertarget->textureNumber);
		if (PsychGetTextureTarget(backupRendertarget)==GL_TEXTURE_2D) {
			// Special case for power-of-two textures:
			glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, twidth, theight);
		}
		else {
			// This would be appropriate but crashes for no good reason on OS-X 10.4.4: glCopyTexSubImage2D(PsychGetTextureTarget(backupRendertarget), 0, 0, 0, 0, 0, (int) PsychGetWidthFromRect(backupRendertarget->rect), (int) PsychGetHeightFromRect(backupRendertarget->rect));                         
			glCopyTexImage2D(PsychGetTextureTarget(backupRendertarget), 0, GL_RGBA8, 0, 0, twidth, theight, 0);
			backupRendertarget->surfaceSizeBytes = 4 * twidth * theight; 
		}
	}
	
	return;
}

/* Set Screen - global flag which tells PTB if userspace rendering is active or not. */
void PsychSetUserspaceGLFlag(psych_bool inuserspace)
{
	inGLUserspace = inuserspace;
}

/* Get Screen - global flag which tells if we are in userspace rendering mode: */
psych_bool PsychIsUserspaceRendering(void)
{
	return(inGLUserspace);
}

int PsychRessourceCheckAndReminder(psych_bool displayMessage) {
	int i,j = 0;

	#if PSYCH_SYSTEM != PSYCH_LINUX
	// Check for open movies:
	j = PsychGetMovieCount();
	if (j > 0) {
		if (displayMessage && PsychPrefStateGet_Verbosity()>2) {
			printf("\n\nPTB-INFO: There are still %i movies open. Screen('CloseAll') will auto-close them.\n", j);
			printf("PTB-INFO: This may be fine for studies where you only use a single movie, but a large number of open\n");
			printf("PTB-INFO: movies can be an indication that you forgot to dispose no longer needed movie objects\n");
			printf("PTB-INFO: via a proper call to Screen('CloseMovie', moviePtr); , e.g., at the end of each trial. These\n");
			printf("PTB-INFO: stale movies linger around and can consume significant memory and cpu ressources, causing\n");
			printf("PTB-INFO: degraded performance, timing trouble and ultimately out of memory or out of ressource\n");
			printf("PTB-INFO: conditions or even crashes of Matlab/Octave (in rare cases). Please check your code.\n\n");
		}
	}
	#endif

	// Check for open textures and proxies at close time. Might be indicative of missing
	// close calls for releasing texture -- ie. leaked memory:
	i = PsychCountOpenWindows(kPsychTexture) + PsychCountOpenWindows(kPsychProxyWindow);
	
	// Textures open. Give a friendly reminder if either at least 10 textures are remaining or
	// the user asked for verbosity level > 3, ie. very exhaustive info, and at least one texture is remaining.
	if (displayMessage && ((PsychPrefStateGet_Verbosity()>2 && i> 10) || (PsychPrefStateGet_Verbosity() > 3 && i > 0))) {
		printf("\n\nPTB-INFO: There are still %i textures, offscreen windows or proxy windows open. Screen('CloseAll') will auto-close them.\n", i);
		printf("PTB-INFO: This may be fine for studies where you only use a few textures or windows, but a large number of open\n");
		printf("PTB-INFO: textures or offscreen windows can be an indication that you forgot to dispose no longer needed items\n");
		printf("PTB-INFO: via a proper call to Screen('Close', [windowOrTextureIndex]); , e.g., at the end of each trial. These\n");
		printf("PTB-INFO: stale objects linger around and can consume significant memory ressources, causing degraded performance,\n");
		printf("PTB-INFO: timing trouble (if the system has to resort to disk paging) and ultimately out of memory conditions or\n");
		printf("PTB-INFO: crashes. Please check your code. (Screen('Close') is a quick way to release all textures and offscreen windows)\n\n");
	}
	
	// Return total sum of open ressource hogs ;-)
	return(i + j);
}

/* PsychGetCurrentShader() - Returns currently bound GLSL
 * program object, if any. Returns 0 if fixed-function pipeline
 * is active.
 *
 * This needs to distinguish between OpenGL 2.0 and earlier.
 */
int PsychGetCurrentShader(PsychWindowRecordType *windowRecord) {
	int curShader;
	
	if (GLEW_VERSION_2_0) {
		glGetIntegerv(GL_CURRENT_PROGRAM, &curShader);
	}
	else {
		curShader = (int) glGetHandleARB(GL_PROGRAM_OBJECT_ARB);
	}

	return(curShader);
}

/* PsychSetShader() -- Lazily choose a GLSL shader to use for further operations.
 *
 * The routine shall bind the shader 'shader' for the OpenGL context of window
 * 'windowRecord'. It assumes that the OpenGL context for that windowRecord is
 * already bound.
 *
 * This is a wrapper around glUseProgram(). It does nothing if GLSL isn't supported,
 * ie. if gluseProgram() is not available. Otherwise it checks the currently bound
 * shader and only rebinds the new shader if it isn't already bound - avoiding redundant
 * calls to glUseProgram() as such calls might be expensive on some systems.
 *
 * A 'shader' value of zero disables shading and enables fixed-function pipe, as usual.
 * A positive value sets the shader with that handle. Negative values have special
 * meaning in that the select special purpose shaders stored in the 'windowRecord'.
 *
 * Currently the value -1 is defined to choose the windowRecord->defaultDrawShader.
 * That shader can be anything special, zero for fixed function pipe, or e.g., a shader
 * to disable color clamping.
 */
int PsychSetShader(PsychWindowRecordType *windowRecord, int shader)
{
	int oldShader;

	// Have GLSL support?
	if (glUseProgram) {
		// Choose this windowRecords assigned default draw shader if shader == -1:
		if (shader == -1) shader = (int) windowRecord->defaultDrawShader;
		if (shader <  -1) { printf("PTB-BUG: Invalid shader id %i requested in PsychSetShader()! Switching to fixed function.\n", shader); shader = 0; }
		
		// Query currently bound shader:
		oldShader = PsychGetCurrentShader(windowRecord);

		// Switch required? Switch if so:
		if (shader != oldShader) glUseProgram((GLuint) shader);
	}
	else {
		shader = 0;
	}
	
	// Return new bound shader (or zero in case of fixed function only):
	return(shader);
}

/* PsychDetectAndAssignGfxCapabilities()
 *
 * This routine must be called with the OpenGL context of the given 'windowRecord' active,
 * usually once during onscreen window creation.
 *
 * It uses different methods, heuristics, white- and blacklists to determine which capabilities
 * are supported by a given gfx-renderer, or which restrictions apply. It then sets up the
 * gfxcaps bitfield of the windowRecord with proper status bits accordingly.
 *
 * The resulting statusbits can be used by different PTB routines to decide if some feature
 * can be used or if any specific work-arounds need to be enabled for a specific renderer.
 * Most stuff is related to floating point rendering/blending/filtering etc. as recent hw
 * differs in that area.
 */
void PsychDetectAndAssignGfxCapabilities(PsychWindowRecordType *windowRecord)
{
	psych_bool verbose = (PsychPrefStateGet_Verbosity() > 5) ? TRUE : FALSE;
	
	psych_bool nvidia = FALSE;
	psych_bool ati = FALSE;
	GLint maxtexsize=0, maxcolattachments=0, maxaluinst=0;
	GLboolean nativeStereo = FALSE;

	// Init Id string for GPU core to zero. This has at most 8 Bytes, including 0-terminator,
	// so use at most 7 letters!
	memset(&(windowRecord->gpuCoreId[0]), 0, 8);

	if (strstr((char*) glGetString(GL_VENDOR), "ATI") || strstr((char*) glGetString(GL_VENDOR), "AMD") || strstr((char*) glGetString(GL_RENDERER), "AMD")) { ati = TRUE; sprintf(windowRecord->gpuCoreId, "R100"); }
	if (strstr((char*) glGetString(GL_VENDOR), "NVIDIA") || strstr((char*) glGetString(GL_RENDERER), "nouveau") || strstr((char*) glGetString(GL_VENDOR), "nouveau")) { nvidia = TRUE; sprintf(windowRecord->gpuCoreId, "NV10"); }

	// Detection code for Linux DRI driver stack with ATI GPU:
	if (strstr((char*) glGetString(GL_VENDOR), "Advanced Micro Devices") || strstr((char*) glGetString(GL_RENDERER), "ATI")) { ati = TRUE; sprintf(windowRecord->gpuCoreId, "R100"); }
	
	while (glGetError());
	glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_EXT, &maxtexsize);
	glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &maxcolattachments);
	if ((glewIsSupported("GL_ARB_fragment_program") || glewIsSupported("GL_ARB_vertex_program")) && glGetProgramivARB!=NULL) glGetProgramivARB(GL_FRAGMENT_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, &maxaluinst);

	// Fallback query for max 2D texture size, in case rectangle texture size query should fail:
	if (maxtexsize == 0) glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxtexsize);

	// Cache maximum supported texture size for reuse by other routines:
	windowRecord->maxTextureSize = (int) maxtexsize;

	while (glGetError());
	
	if (verbose) {
		printf("PTB-DEBUG: Interrogating Low-level renderer capabilities for onscreen window with handle %i:\n", windowRecord->windowIndex);
		printf("Indicator variables: FBO's %i, ATI_texture_float %i, ARB_texture_float %i, Vendor %s, Renderer %s.\n",
				glewIsSupported("GL_EXT_framebuffer_object"),glewIsSupported("GL_ATI_texture_float"), glewIsSupported("GL_ARB_texture_float"), (char*) glGetString(GL_VENDOR), (char*) glGetString(GL_RENDERER));
		printf("Indicator variables: maxcolorattachments = %i, maxrectangletexturesize = %i, maxnativealuinstructions = %i.\n", maxcolattachments, maxtexsize, maxaluinst);
	}

	if (glewIsSupported("GL_MESA_ycbcr_texture") || glewIsSupported("GL_APPLE_ycbcr_422")) {
		windowRecord->gfxcaps |= kPsychGfxCapUYVYTexture;
		if (verbose) printf("PTB-DEBUG: GPU supports UYVY - YCrCb texture formats for optimized handling of video content.\n");
	}

	// Is this a GPU with known broken drivers that yield miserable texture creation performance
	// for RGBA8 textures when using the standard optimized settings?
	// As far as we know (June 2008), ATI hardware under MS-Windows and Linux has this driver bugs,
	// at least on X1600 mobile and X1300 desktop:
	if ((PSYCH_SYSTEM == PSYCH_WINDOWS || PSYCH_SYSTEM == PSYCH_LINUX) && ati) {
		// Supposedly: Set the special flag that will trigger alternative parameter selection
		// in PsychCreateTexture():
		windowRecord->gfxcaps |= kPsychGfxCapNeedsUnsignedByteRGBATextureUpload;
	}
	
	// Does usercode want us to override the automatic choice of optimal texture upload format for RGBA8 textures?
	if (PsychPrefStateGet_ConserveVRAM() & kPsychTextureUploadFormatOverride) {
		// Override! Invert current setting:
		if (windowRecord->gfxcaps & kPsychGfxCapNeedsUnsignedByteRGBATextureUpload) {
			// Clear this caps bit:
			windowRecord->gfxcaps &= (~kPsychGfxCapNeedsUnsignedByteRGBATextureUpload);
		}
		else {
			// Set this caps bit:
			windowRecord->gfxcaps |= kPsychGfxCapNeedsUnsignedByteRGBATextureUpload;
		}
	}

        if (glewIsSupported("GL_EXT_texture_snorm")) {
		if (verbose) printf("Hardware supports signed normalized textures of 16 bpc integer format.\n");
		windowRecord->gfxcaps |= kPsychGfxCapSNTex16;
	}

	// Support for basic FBO's? Needed for any operation of the imaging pipeline, e.g.,
	// full imaging pipe, fast offscreen windows, Screen('TransformTexture')...
	
	// Check if this system does support OpenGL framebuffer objects and rectangle textures:
	if (glewIsSupported("GL_EXT_framebuffer_object") && (glewIsSupported("GL_EXT_texture_rectangle") || glewIsSupported("GL_ARB_texture_rectangle") || glewIsSupported("GL_NV_texture_rectangle"))) {
		// Basic FBO's utilizing texture rectangle textures as rendertargets are supported.
		// We've got at least RGBA8 rendertargets, including full alpha blending:
		if (verbose) printf("Basic framebuffer objects with rectangle texture rendertargets supported --> RGBA8 rendertargets with blending.\n");
		windowRecord->gfxcaps |= kPsychGfxCapFBO;
		
		// Support for fast inter-framebuffer blits?
		if (glewIsSupported("GL_EXT_framebuffer_blit")) {
			if (verbose) printf("Framebuffer objects support fast blitting between each other.\n");
			windowRecord->gfxcaps |= kPsychGfxCapFBOBlit;			
		}
		
		// Support for multisampled FBO's?
		if (glewIsSupported("GL_EXT_framebuffer_multisample") && (windowRecord->gfxcaps & kPsychGfxCapFBOBlit)) {
			if (verbose) printf("Framebuffer objects support anti-aliasing via multisampling.\n");
			windowRecord->gfxcaps |= kPsychGfxCapFBOMultisample;			
		}
	}

	// ATI_texture_float is supported by R300 ATI cores and later, as well as NV30/40 NVidia cores and later.
	if (glewIsSupported("GL_ATI_texture_float") || glewIsSupported("GL_ARB_texture_float") || strstr((char*) glGetString(GL_EXTENSIONS), "GL_MESAX_texture_float")) {
		// Floating point textures are supported, both 16bpc and 32bpc:
		if (verbose) printf("Hardware supports floating point textures of 16bpc and 32bpc float format.\n");
		windowRecord->gfxcaps |= kPsychGfxCapFPTex16;
		windowRecord->gfxcaps |= kPsychGfxCapFPTex32;
		
		// ATI specific detection logic:
		if (ati && (windowRecord->gfxcaps & kPsychGfxCapFBO)) {
			// ATI hardware with float texture support is a R300 core or later: They support floating point FBO's as well:
			if (verbose) printf("Assuming ATI R300 core or later: Hardware supports basic floating point framebuffers of 16bpc and 32bpc float format.\n");
			sprintf(windowRecord->gpuCoreId, "R300");
			windowRecord->gfxcaps |= kPsychGfxCapFPFBO16;
			windowRecord->gfxcaps |= kPsychGfxCapFPFBO32;
			
			// ATI R500 core (X1000 series) can do blending on 16bpc float FBO's, but not R300/R400. They differ
			// in maximum supported texture size (R500 == 4096, R400 == 2560, R300 == 2048) so we use that as detector:
			if (maxtexsize > 4000) {
				// R500 core or later:
				if (verbose) printf("Assuming ATI R500 or later (maxtexsize=%i): Hardware supports floating point blending on 16bpc float format.\n", maxtexsize);
				sprintf(windowRecord->gpuCoreId, "R500");
				windowRecord->gfxcaps |= kPsychGfxCapFPBlend16;

				if (verbose) printf("Hardware supports full 32 bit floating point precision shading.\n");
				windowRecord->gfxcaps |= kPsychGfxCapFP32Shading;
				
				// The R600 and later can do full FP blending and texture filtering on 16bpc and 32 bpc float,
				// whereas none of the <= R5xx can do *any* float texture filtering. However, for OS/X, there
				// doesn't seem to be a clear differentiating gl extension or limit to allow distinguishing
				// R600 from earlier cores. The best we can do for now is name matching, which won't work
				// for the FireGL series however, so we also check for maxaluinst > 2000, because presumably,
				// the R600 has a limit of 2048 whereas previous cores only had 512.
				if ((strstr((char*) glGetString(GL_RENDERER), "R600")) || (strstr((char*) glGetString(GL_RENDERER), "Radeon") && strstr((char*) glGetString(GL_RENDERER), "HD"))) {
					// Ok, a Radeon HD 2xxx/3xxx or later -> R600 or later:
					if (verbose) printf("Assuming ATI R600 or later (Matching namestring): Hardware supports floating point blending and filtering on 16bpc and 32bpc float formats.\n");
					sprintf(windowRecord->gpuCoreId, "R600");
					windowRecord->gfxcaps |= kPsychGfxCapFPBlend32;
					windowRecord->gfxcaps |= kPsychGfxCapFPFilter16;
					windowRecord->gfxcaps |= kPsychGfxCapFPFilter32;
				}
				else if (maxaluinst > 2000) {
					// Name matching failed, but number ALU instructions is high, so maybe a FireGL with R600 core?
					if (verbose) printf("Assuming ATI R600 or later (Max native ALU inst. = %i): Hardware supports floating point blending and filtering on 16bpc and 32bpc float formats.\n", maxaluinst);
					sprintf(windowRecord->gpuCoreId, "R600");
					windowRecord->gfxcaps |= kPsychGfxCapFPBlend32;
					windowRecord->gfxcaps |= kPsychGfxCapFPFilter16;
					windowRecord->gfxcaps |= kPsychGfxCapFPFilter32;					
				}
			}
		}
		
		// NVIDIA specific detection logic:
		if (nvidia && (windowRecord->gfxcaps & kPsychGfxCapFBO)) {
			// NVIDIA hardware with float texture support is a NV30 core or later: They support floating point FBO's as well:
			if (verbose) printf("Assuming NV30 core or later...\n");
			sprintf(windowRecord->gpuCoreId, "NV30");
			
			// Use maximum number of color attachments as differentiator between GeforceFX and GF6xxx/7xxx/....
			if (maxcolattachments > 1) {
				// NV40 core of GF 6000 or later supports at least 16 bpc float texture filtering and framebuffer blending:
				if (verbose) printf("Assuming NV40 core or later (maxcolattachments=%i): Hardware supports floating point blending and filtering on 16bpc float format.\n", maxcolattachments);
				if (verbose) printf("Hardware also supports floating point framebuffers of 16bpc and 32bpc float format.\n");
				sprintf(windowRecord->gpuCoreId, "NV40");
				windowRecord->gfxcaps |= kPsychGfxCapFPFBO16;
				windowRecord->gfxcaps |= kPsychGfxCapFPFBO32;
				windowRecord->gfxcaps |= kPsychGfxCapFPFilter16;
				windowRecord->gfxcaps |= kPsychGfxCapFPBlend16;	
				
				// NV 40 supports full 32 bit float precision in shaders:
				if (verbose) printf("Hardware supports full 32 bit floating point precision shading.\n");
				windowRecord->gfxcaps |= kPsychGfxCapFP32Shading;
			}
			
			// The Geforce 8xxx/9xxx series and later (G80 cores and later) do support full 32 bpc float filtering and blending:
			// They also support a max texture size of > 4096 texels --> 8192 texels, so we use that as detector:
			if ((maxtexsize > 4100) || (strstr((char*) glGetString(GL_VENDOR), "nouveau") && (maxtexsize >= 4096) && (maxaluinst >= 16384))) {
				if (verbose) printf("Assuming G80 core or later (maxtexsize=%i): Hardware supports full floating point blending and filtering on 16bpc and 32bpc float format.\n", maxtexsize);
				sprintf(windowRecord->gpuCoreId, "G80");
				windowRecord->gfxcaps |= kPsychGfxCapFPBlend32;
				windowRecord->gfxcaps |= kPsychGfxCapFPFilter32;
				windowRecord->gfxcaps |= kPsychGfxCapFPFilter16;
				windowRecord->gfxcaps |= kPsychGfxCapFPBlend16;				
			}
		}		
	}

	// Native OpenGL quad-buffered stereo context?
	glGetBooleanv(GL_STEREO, &nativeStereo);
	if (nativeStereo) {
		if (verbose) printf("Hardware supports native OpenGL quad-buffered stereo, e.g., frame-sequential mode.\n");
		windowRecord->gfxcaps |= kPsychGfxCapNativeStereo;
	}

	// Running under Chromium OpenGL virtualization or Mesa Software Rasterizer?
	if ((strstr((char*) glGetString(GL_VENDOR), "Humper") && strstr((char*) glGetString(GL_RENDERER), "Chromium")) ||
        (strstr((char*) glGetString(GL_VENDOR), "Mesa") && strstr((char*) glGetString(GL_RENDERER), "Software Rasterizer"))) {
		// Yes: We're very likely running inside a Virtual Machine, e.g., VirtualBox.
		// This does not provide sufficiently accurate display timing for production use of Psychtoolbox.
		// Output a info message for user and disable all calibrations and sync tests -- they would fail anyway.
		if (PsychPrefStateGet_Verbosity() > 2) {
			printf("\n\n");
			printf("PTB-INFO: Seems like Psychtoolbox is running inside a Virtual Machine? This doesn't provide sufficient\n");
			printf("PTB-INFO: visual stimulus timing precision for research grade visual stimulation. I will disable most\n");
			printf("PTB-INFO: tests and calibrations so you can at least get your scripts running for demo purposes. Other\n");
			printf("PTB-INFO: presentation modalities and various Psychtoolbox functions will only work with limited functionality\n");
			printf("PTB-INFO: and precision. Only use this for demos and simple tests, not for real experiment sessions!\n\n");

			// Disable all sync tests and display timing calibrations, unless usercode already did something similar:
			if (PsychPrefStateGet_SkipSyncTests() < 1) PsychPrefStateSet_SkipSyncTests(2);
			// Disable strict OpenGL error checking, so we don't abort for minor OpenGL errors and
			// don't clutter the console with OpenGL error warnings. This keeps some scripts running in
			// at least a bearable way:
			PsychPrefStateSet_ConserveVRAM(PsychPrefStateGet_ConserveVRAM() | kPsychAvoidCPUGPUSync);
		}
	}
	
	#ifdef GLX_OML_sync_control
	
	// Running on a XServer prior to version 1.8.2 with broken OpenML implementation? Mark it, if so:
	if (PsychPrefStateGet_Verbosity() > 4) printf("PTB-Info: Running on '%s' XServer, Vendor release %i.\n", XServerVendor(windowRecord->targetSpecific.deviceContext), (int) XVendorRelease(windowRecord->targetSpecific.deviceContext));

	if (verbose) {
		printf("OML_sync_control indicators: glXGetSyncValuesOML=%p , glXWaitForMscOML=%p, glXWaitForSbcOML=%p, glXSwapBuffersMscOML=%p\n",
				glXGetSyncValuesOML, glXWaitForMscOML, glXWaitForSbcOML, glXSwapBuffersMscOML);
		printf("OML_sync_control indicators: glxewIsSupported() says %i.\n", (int) glxewIsSupported("GLX_OML_sync_control"));
	}

	// Check if OpenML extensions for precisely scheduled stimulus onset and onset timestamping are supported:
	if (glxewIsSupported("GLX_OML_sync_control") && (glXGetSyncValuesOML && glXWaitForMscOML && glXWaitForSbcOML && glXSwapBuffersMscOML)) {
		if (verbose) printf("System supports OpenML OML_sync_control extension for high-precision scheduled swaps and timestamping.\n");

		// If prior 1.8.2 and therefore defective, disable use of OpenML for anything, even timestamping:
		if (XVendorRelease(windowRecord->targetSpecific.deviceContext) < 10802000) {
			// OpenML timestamping in PsychOSGetSwapCompletionTimestamp() and PsychOSGetVBLTimeAndCount() disabled:
			windowRecord->specialflags |= kPsychOpenMLDefective;
			
			// OpenML swap scheduling in PsychFlipWindowBuffers() disabled:
			windowRecord->gfxcaps &= ~kPsychGfxCapSupportsOpenML;
			
			if (PsychPrefStateGet_Verbosity() > 1) {
				printf("PTB-WARNING: XServer version prior to 1.8.2 with defective OpenML OML_sync_control implementation detected! Disabling all OpenML support.\n");
			}
		}
		else {
			// OpenML is currently only supported on GNU/Linux, but should be pretty well working/useable
			// starting with Linux kernel 2.6.35 and XOrg X-Servers 1.8.2, 1.9.x and later, as shipping
			// in the Ubuntu 10.10 release in October 2010 and other future distributions.
			// PTB will use OpenML scheduling if supported and found to be correctly working, but the
			// kPsychDisableOpenMLScheduling conserveVRAM flag allows to force it off and fall back to
			// conventional PTB scheduling.
			if (!(PsychPrefStateGet_ConserveVRAM() & kPsychDisableOpenMLScheduling)) {
				// Enabled and supported: Use it.
				windowRecord->gfxcaps |= kPsychGfxCapSupportsOpenML;
				if (verbose) printf("OpenML OML_sync_control extension enabled for all scheduled swaps.\n");
				
				// Perform correctness check and enable all relevant workarounds. We know that OpenML
				// currently is only supported on Linux/X11 with some DRI2 drivers, and that the shipping
				// DRI implementation up to and including Linux 2.6.36 does have a limitation that we need to detect
				// and workaround. Therefore we restrict this test & setup routine to Linux:
				#if PSYCH_SYSTEM == PSYCH_LINUX
				// Perform baseline init and correctness check:
				PsychOSInitializeOpenML(windowRecord);
				#endif
			}
		}
	}
	else {
		// OpenML unsupported:
		if (verbose) printf("No support for OpenML OML_sync_control extension. Using standard implementation.\n");

		// OpenML timestamping in PsychOSGetSwapCompletionTimestamp() and PsychOSGetVBLTimeAndCount() disabled:
		windowRecord->specialflags |= kPsychOpenMLDefective;
		
		// OpenML swap scheduling in PsychFlipWindowBuffers() disabled:
		windowRecord->gfxcaps &= ~kPsychGfxCapSupportsOpenML;

	}
	#else

        // Make sure we don't compile without OML_sync_control support on Linux, as that would be a shame:
        #if PSYCH_SYSTEM == PSYCH_LINUX
		#error Build aborted. You *must* compile with the -std=gnu99  gcc compiler switch to enable the required OML_sync_control extension!
        #endif
        
		// OpenML unsupported:
		if (verbose) printf("No compiled in support for OpenML OML_sync_control extension. Using standard implementation.\n");

		// OpenML timestamping in PsychOSGetSwapCompletionTimestamp() and PsychOSGetVBLTimeAndCount() disabled:
		windowRecord->specialflags |= kPsychOpenMLDefective;
		
		// OpenML swap scheduling in PsychFlipWindowBuffers() disabled:
		windowRecord->gfxcaps &= ~kPsychGfxCapSupportsOpenML;
	#endif
	
	if (verbose) printf("PTB-DEBUG: Interrogation done.\n\n");
	
	return;
}

// Common (Operating system independent) code to be executed immediately
// before a OS specific double buffer swap request is performed: This
// is called from PsychOSFlipWindowBuffers() within the OS specific variants
// of PsychWindowGlue.c and shall implement special logging actions, workarounds
// etc.
//
// Currently it implements manual syncing of bufferswap requests to VBL onset,
// i.e., waits via beamposition query for VBL onset before returning. This to
// work around setups will totally broken VSYNC support.
void PsychExecuteBufferSwapPrefix(PsychWindowRecordType *windowRecord)
{
    CGDirectDisplayID	cgDisplayID;
    long				vbl_startline, scanline, lastline;

	// Workaround for broken sync-bufferswap-to-VBL support needed?
	if ((windowRecord->specialflags & kPsychBusyWaitForVBLBeforeBufferSwapRequest) || (PsychPrefStateGet_ConserveVRAM() & kPsychBusyWaitForVBLBeforeBufferSwapRequest)) {
		// Yes: Sync of bufferswaps to VBL requested?
		if (windowRecord->vSynced) {
			// Sync of bufferswaps to retrace requested:
			// We perform a busy-waiting spin-loop and query current beamposition until
			// beam leaves VBL area:
			
			// Retrieve display handle for beamposition queries:
			PsychGetCGDisplayIDFromScreenNumber(&cgDisplayID, windowRecord->screenNumber);
			
			// Retrieve final vbl_startline, aka physical height of the display in pixels:
            vbl_startline = windowRecord->VBL_Startline;
            
			// Busy-Wait: The special handling of <=0 values makes sure we don't hang here
			// if beamposition queries are broken as well:
			lastline = (long) PsychGetDisplayBeamPosition(cgDisplayID, windowRecord->screenNumber);
			if (lastline == 0) {
				// Zero scanout position. Could be sign of a failure, or just that we happened zero
				// by chance. Wait  250 usecs and retry. If it is still zero, we know this is a
				// permanent failure condition.
				PsychWaitIntervalSeconds(0.000250);
				lastline = (long) PsychGetDisplayBeamPosition(cgDisplayID, windowRecord->screenNumber);
			}
			
			if (lastline > 0) {
				// Within video frame. Wait for beamposition wraparound or start of VBL:
				if (PsychPrefStateGet_Verbosity()>9) printf("\nPTB-DEBUG: Lastline beampos = %i\n", (int) lastline);

				scanline = lastline;

				// Wait until entering VBL or wraparound (i.e., VBL skipped). The fudge
				// factor of -1 is to take yet another NVidia bug into account :-(
				while ((scanline < vbl_startline) && (scanline >= lastline - 1)) {
					lastline = (scanline > lastline) ? scanline : lastline;
					if (scanline < (vbl_startline - 100)) PsychYieldIntervalSeconds(0.0);
					scanline = (long) PsychGetDisplayBeamPosition(cgDisplayID, windowRecord->screenNumber);
				}
				if (PsychPrefStateGet_Verbosity()>9) printf("\nPTB-DEBUG: At exit of loop: Lastline beampos = %i, Scanline beampos = %i\n", (int) lastline, (int) scanline);
			}
		}
	}

	return;
}

/* PsychFindFreeSwapGroupId() - Return the id of the first unused
 * swapgroup between 1 and maxGroupId. Return zero if no such free
 * group id can be found.
 * This is a helper function for OS dependent setup routines for swaplock/framelock
 * extensions.
 */
int	PsychFindFreeSwapGroupId(int maxGroupId)
{
    PsychWindowRecordType **windowRecordArray = NULL;
    int	i, j, rc;
	psych_bool taken;
    int numWindows = 0;

	if (maxGroupId < 1) return(0);

	PsychCreateVolatileWindowRecordPointerList(&numWindows, &windowRecordArray);
    rc = 0;

	for (j = 1; j <= maxGroupId; j++) {
		// Search all swapgroups if id 'j' is already taken.
		taken = FALSE;
		for(i = 0; i < numWindows; i++) {
			if (PsychIsOnscreenWindow(windowRecordArray[i]) && ((int) windowRecordArray[i]->swapGroup == j)) {
				taken = TRUE;
				break;
			}
		}
		
		if (!taken) {
			// Bingo!
			rc = j;
			break;
		}
	}
	
	// rc is either zero if no swapgroup id free, or a free swapgroup handle.
	PsychDestroyVolatileWindowRecordPointerList(windowRecordArray);
	
	return(rc);
}