File: PsychWindowSupport.c

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (8023 lines) | stat: -rw-r--r-- 495,263 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
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
/*
    PsychToolbox3/Source/Common/Screen/PsychWindowSupport.c

    PLATFORMS:

        All.

    AUTHORS:

        Allen Ingling       awi     Allen.Ingling@nyu.edu
        Mario Kleiner       mk      mario.kleiner.de@gmail.com

    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:

*/

#include "Screen.h"

// Define this for non-Waffle builds:
#ifndef WAFFLE_PLATFORM_WAYLAND
#define WAFFLE_PLATFORM_WAYLAND 0x0014
#endif

#if PSYCH_SYSTEM == PSYCH_LINUX
#include <errno.h>
// utsname for uname() so we can find out on which kernel we're running:
#include <sys/utsname.h>
#endif

// Support for nvstusb library requested to drive Nvidia NVision stereo shutter goggles?
#ifdef PTB_USE_NVSTUSB

#if PSYCH_SYSTEM != PSYCH_WINDOWS
// Include for dynamic loading of external nvstusb library plugin:
#include <dlfcn.h>
#endif

#include "nvstusb.h"
typedef struct nvstusb_context* (*NVSTUSB_INIT_PROC)(char const * fw);
typedef void (*NVSTUSB_DEINIT_PROC)(struct nvstusb_context *ctx);
typedef void (*NVSTUSB_SET_RATE_PROC)(struct nvstusb_context *ctx, float rate);
typedef void (*NVSTUSB_SWAP_PROC)(struct nvstusb_context *ctx, enum nvstusb_eye eye, void (*swapfunc)());
typedef void (*NVSTUSB_GET_KEYS_PROC)(struct nvstusb_context *ctx, struct nvstusb_keys *keys);
typedef void (*NVSTUSB_INVERT_EYES_PROC)(struct nvstusb_context *ctx);

NVSTUSB_INIT_PROC Nvstusb_init_proc = NULL;
NVSTUSB_DEINIT_PROC Nvstusb_deinit_proc = NULL;
NVSTUSB_SET_RATE_PROC Nvstusb_set_rate_proc = NULL;
NVSTUSB_SWAP_PROC Nvstusb_swap_proc = NULL;
NVSTUSB_GET_KEYS_PROC Nvstusb_get_keys_proc = NULL;
NVSTUSB_INVERT_EYES_PROC Nvstusb_invert_eyes_proc = NULL;

static void* nvstusb_plugin = NULL;
static struct nvstusb_context* nvstusb_goggles = NULL;
#endif
static double nvsttriggerdelay = 0;

#if PSYCH_SYSTEM != PSYCH_WINDOWS
#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;

static PsychWindowRecordType *splashTextureRecord = NULL;

/* 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;

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

// Count of onscreen windows which have our own threaded frameseq stereo implementation active:
static unsigned int frameSeqStereoActive = 0;

// Count of onscreen windows which have our own threaded VRR scheduler implementation active:
static unsigned int vrrSchedulersActive = 0;

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

// Return count of currently frameseq stereo threaded onscreen windows:
unsigned int PsychGetNrFrameSeqStereoWindowsActive(void)
{
    return(frameSeqStereoActive);
}

// Return count of currently VRR scheduler threaded onscreen windows:
unsigned int PsychGetNrVRRSchedulerWindowsActive(void)
{
    return(vrrSchedulersActive);
}

/*
 * Measure and compute VBLANK start and end timestamp for current refresh cycle.
 *
 * This uses the beamposition based timestamping to compute vblank start and end
 * time for the most recent (or still active) vblank.
 *
 * Returns -1 on failure / if the feature is unsupported.
 * Returns start of active scanout (= End time of vblank). If optional vblStartTime
 * is provided, then returns start time of vblank in the double pointed to by vblStartTime.
 */
double PsychGetVblankTimestamps(PsychWindowRecordType *windowRecord, double *vblankStartTime)
{
    int beamPosAtFlip;
    double vbl_lines_elapsed, onset_lines_togo;
    double time_at_vbl, vbl_time_elapsed, onset_time_togo;
    double currentrefreshestimate = windowRecord->VideoRefreshInterval;
    double vbl_startline = windowRecord->VBL_Startline;
    double vbl_endline = windowRecord->VBL_Endline;

    // Beamposition queries inoperative/unsupported/broken/disabled?
    if (vbl_endline == -1 || currentrefreshestimate <= 0.0)
        return(-1);

    beamPosAtFlip = PsychGetDisplayBeamPosition((CGDirectDisplayID) 0, windowRecord->screenNumber);
    PsychGetAdjustedPrecisionTimerSeconds(&time_at_vbl);

    // Failed / Unsupported?
    if (beamPosAtFlip == -1)
        return(-1);

    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 start of vblank -- Only works in non-VRR mode with fixed duration Vblank:
    if (vblankStartTime)
        *vblankStartTime = time_at_vbl - vbl_time_elapsed;

    // Compute of stimulus-onset, aka time when retrace is finished:
    time_at_vbl = time_at_vbl + onset_time_togo;

    // Return end of vblank aka start of active scanout:
    return(time_at_vbl);
}

static void PsychDrawSplash(PsychWindowRecordType* windowRecord, double jiggle)
{
    // Always clear framebuffer to defined background color:
    glClear(GL_COLOR_BUFFER_BIT);

    // Draw splash image texture at visual level 4+:
    if ((PsychPrefStateGet_VisualDebugLevel() >= 4) && splashTextureRecord && (PsychGetParentWindow(splashTextureRecord) == windowRecord)) {
        splashTextureRecord->clientrect[kPsychLeft] -= jiggle;
        splashTextureRecord->clientrect[kPsychRight] += jiggle;
        PsychBlitTextureToDisplay(splashTextureRecord, windowRecord, splashTextureRecord->rect, splashTextureRecord->clientrect, 0, 1, 1);
        splashTextureRecord->clientrect[kPsychLeft] += jiggle;
        splashTextureRecord->clientrect[kPsychRight] -= jiggle;
    }

    return;
}

// 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 = (PFNGLSHADERSOURCEPROC) 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 = 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, psych_int64 specialFlags, PsychVRRModeType vrrMode, PsychVRRStyleType vrrStyleHint, double vrrMinDuration, double vrrMaxDuration)
{
    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;
    int logo_x, logo_y;

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

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

    (void) dummychar;

    // 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: OS support status: %s\n", PsychSupportStatus());
            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");
            printf("PTB-INFO: For information about paid support, support memberships and other commercial services, please type\n");
            printf("PTB-INFO: 'PsychPaidSupportAndServices'.\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");
        }
    }

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

    // Add all passed-in VRR parameters to windowRecord:
    (*windowRecord)->vrrMode = vrrMode;
    (*windowRecord)->vrrStyleHint = vrrStyleHint;
    (*windowRecord)->vrrMinDuration = vrrMinDuration;
    (*windowRecord)->vrrMaxDuration = vrrMaxDuration;
    (*windowRecord)->vrrLatencyCompensation = 0.0;

    // 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 {
            // Try next window index:
            i++;

            // Skip this one if it is ourselves:
            if (i == (*windowRecord)->windowIndex) i++;

            // Abort search if no further windowRecords available (invalid_Windex return code):
            if (PsychError_invalidWindex == FindWindowRecord(i, &((*windowRecord)->slaveWindow))) break;

            // Repeat search if this ain't an onscreen window, or on Linux, if it is located on a
            // different X-Screen aka screenNumber, because windows can't share resources across X-Screens:
        } while (!PsychIsOnscreenWindow((*windowRecord)->slaveWindow) || ((PSYCH_SYSTEM == PSYCH_LINUX) && ((*windowRecord)->slaveWindow->screenNumber != screenSettings->screenNumber)));

        // Sanity check - Do conditions hold for valid sharing window?
        if (!((*windowRecord)->slaveWindow) || !PsychIsOnscreenWindow((*windowRecord)->slaveWindow) ||
            ((PSYCH_SYSTEM == PSYCH_LINUX) && ((*windowRecord)->slaveWindow->screenNumber != screenSettings->screenNumber))) {
            // Failed: Invalidate slaveWindow, so no context sharing takes place - It would fail anyway in lower-level layer:
            (*windowRecord)->slaveWindow = NULL;
            if (PsychPrefStateGet_Verbosity()>3) printf("PTB-INFO: This onscreen window could not find a peer window for sharing of OpenGL context ressources.\n");
        } else {
            // Ok, now we should have the first onscreen window assigned as slave window.
            if (PsychPrefStateGet_Verbosity()>3) printf("PTB-INFO: This onscreen window tries to share OpenGL context ressources with window %i.\n", i);
        }
    }

    // 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 gpu + display + gfx-driver combo may not support some requested feature.\n\n");
        FreeWindowRecordFromPntr(*windowRecord);
        return(FALSE);
    }

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

    // Do a dummy query for type of OpenGL api that is in use. This will initialize the
    // cached global copy of OpenGL api type to the setting of this windowRecord. The cached
    // copy is used whenever code can't access the per windowRecord copy, e.g., because it
    // does not have access to a windowRecord:
    PsychIsGLClassic(*windowRecord);

    // 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 (PsychPrefStateGet_Verbosity() > 1) {
        psych_bool softwareOpenGL = FALSE;

        if (PSYCH_SYSTEM == PSYCH_WINDOWS) {
            if (strstr((char*) glGetString(GL_RENDERER), "GDI")) {
                softwareOpenGL = TRUE;

                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");
            }
        }

        if (PSYCH_SYSTEM == PSYCH_LINUX) {
            if (strstr((char*) glGetString(GL_RENDERER), "Mesa X11") && strstr((char*) glGetString(GL_VENDOR), "Brian Paul")) {
                softwareOpenGL = TRUE;

                printf("\n\n\n\n");
                printf("PTB-WARNING: Seems that a Mesa 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 are running Psychtoolbox on a Matlab version 8.4 (R2014b) or later and\n");
                printf("PTB-WARNING: Matlab is causing this problem by overriding your operating systems OpenGL library with\n");
                printf("PTB-WARNING: its own outdated software library. Please run the setup script PsychLinuxConfiguration()\n");
                printf("PTB-WARNING: now from your Matlab command window and then quit and restart Matlab to fix this problem.\n");
                printf("\n\n");
            }
        }

        // On a software OpenGL implementation, only allow to continue if kPsychUseSoftwareRenderer flag is set:
        if (softwareOpenGL && ((PsychPrefStateGet_ConserveVRAM() & kPsychUseSoftwareRenderer) == 0)) {
            printf("PTB-WARNING: Actually, it is pointless to continue with the software renderer, as 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', 'ConserveVRAM', 64); 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);
        }
    }

    // Decide if 10 or 11 or 16 bpc framebuffer should be enabled by our own kernel driver trick, or
    // if the OS + graphics drivers has done proper work already:
    if (((*windowRecord)->depth == 30) || ((*windowRecord)->depth == 33) || ((*windowRecord)->depth == 48)) {
        // Ask the OS what it thinks it has set atm.:
        glGetIntegerv(GL_RED_BITS, &bpc);

        // Support for kernel driver available? Only on Linux:
#if PSYCH_SYSTEM == PSYCH_LINUX
        if (bpc >= (*windowRecord)->depth / 3) {
            // Linux, and the OS claims it runs at at least requested bpc. Good, take it at face value.
            printf("PTB-INFO: Linux native %i bit per color framebuffer requested, and the OS claims it is working fine. Good.\n", bpc);
        }
        else {
            // No native requested bpc support. Only support our homegrown method with PTB kernel driver on ATI/AMD hardware:
            printf("PTB-INFO: Native %i bit per color framebuffer requested, but the OS doesn't allow it. It only provides %i bpc.\n", (*windowRecord)->depth / 3, bpc);

            // We only support the 48 bit color depth / 16 bpc hack on Linux + X11, not on OSX et al.:
            if (((*windowRecord)->depth == 48) && !((*windowRecord)->specialflags & kPsychIsX11Window)) {
                printf("\nPTB-ERROR: Your script requested a %i bpp, %i bpc framebuffer, but i can't provide this for you, because\n", (*windowRecord)->depth, (*windowRecord)->depth / 3);
                printf("PTB-ERROR: my own 16 bpc setup code only works on Linux with a properly setup X11 display backend.\n");
                PsychOSCloseWindow(*windowRecord);
                FreeWindowRecordFromPntr(*windowRecord);
                return(FALSE);
            }

            printf("PTB-INFO: Will now try to use our own high bit depth setup code as an alternative approach to fullfill your needs.\n");
            gpuMaintype = kPsychUnknown;
            if (!PsychOSIsKernelDriverAvailable(screenSettings->screenNumber) ||
                !PsychGetGPUSpecs(screenSettings->screenNumber, &gpuMaintype, &gpuMinortype, NULL, NULL) ||
                (gpuMaintype != kPsychRadeon) || (gpuMinortype >= 120) || (PsychGetScreenDepthValue(screenSettings->screenNumber) != 24)) {
                printf("\nPTB-ERROR: Your script requested a %i bpp, %i bpc framebuffer, but i can't provide this for you, because\n", (*windowRecord)->depth, (*windowRecord)->depth / 3);
                if ((gpuMaintype != kPsychRadeon) || (gpuMinortype >= 120)) {
                    printf("PTB-ERROR: this functionality is not supported on your model of graphics card. Only AMD GPU's from the\n");
                    printf("PTB-ERROR: Radeon X1000 series up to and including the Radeon Polaris series, and corresponding FireGL/\n");
                    printf("PTB-ERROR: FirePro/Radeon Pro cards are supported. This covers basically all AMD gpu's introduced in the\n");
                    printf("PTB-ERROR: years from late 2005 to mid-2017, but not AMD Vega or AMD RX 5000 (RDNA) and later or AMD Ryzen integrated\n");
                    printf("PTB-ERROR: processor graphics chips and later since late 2017, e.g., AMD Raven Ridge, Renoir, Picasso etc.\n");
                    printf("PTB-ERROR: These recent AMD gpu's with the DCE-12 or new DCN display engine, as well as all NVidia graphics cards\n");
                    printf("PTB-ERROR: since the GeForce-8000 series and corresponding Quadro cards can be setup for 10 bpc framebuffer\n");
                    printf("PTB-ERROR: mode by specifying a 'DefaultDepth' setting of 30 bit in the xorg.conf file. The same is true for\n");
                    printf("PTB-ERROR: modern Intel graphics chips which may be able to achieve 10 bpc with 'DefaultDepth' 30 setting if your\n");
                    printf("PTB-ERROR: graphics driver is recent enough. Use XOrgConfCreator and XOrgConfSelector to guide you through the\n");
                    printf("PTB-ERROR: setup process for 10 bpc / 30 bit on such NVidia, Intel and modern AMD graphics cards.\n");
                }
                else if (!PsychOSIsKernelDriverAvailable(screenSettings->screenNumber)) {
                    printf("PTB-ERROR: Linux low-level MMIO access by Psychtoolbox is disabled or not permitted on your system in this session.\n");
                    printf("PTB-ERROR: On Linux you must configure your system by executing the script 'PsychLinuxConfiguration' once, and\n");
                    printf("PTB-ERROR: make sure that EFI Secure Boot is disabled or at least secure boot verification is disabled.\n\n");
                }
                else if (PsychGetScreenDepthValue(screenSettings->screenNumber) != 24) {
                    printf("PTB-ERROR: your display is not set to 24 bit 'DefaultDepth' color depth, but to %i bit color depth in xorg.conf.\n\n",
                           (int) PsychGetScreenDepthValue(screenSettings->screenNumber));
                }

                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 %i bpc framebuffer mode regardless if i think it is needed/sensible or not.\n", (*windowRecord)->depth / 3);
            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 if your GPU or setup is not one of the supported models and configurations...\n");
            (*windowRecord)->specialflags|= kPsychNative10bpcFBActive;
        }
#else
        // Make compiler happy:
        (void) gpuMaintype;
        (void) gpuMinortype;

        // Not supported by our own code and kernel driver (we don't have such a driver for Windows), but some recent 2008
        // series AMD FireGL and NVidia Quadro cards at least provide the option to enable this natively - although it didn't
        // work properly in our tests around the year 2009.
        if ((PSYCH_SYSTEM == PSYCH_OSX) && (bpc >= (*windowRecord)->depth / 3)) {
            // OSX and the OS claims it runs at at least requested bpc. Good, take
            // it at face value. This should work on OSX >= 10.11.2 at least for
            // some graphics cards, as far as the framebuffer is concerned.
            // The actual output bit depth is totally unverified, but supposed to
            // achieve up to 11 bpc on some Apple machines (MacPro 2013, iMac Retina 5k 2014 & 2015). See:
            // https://developer.apple.com/library/content/releasenotes/MacOSX/WhatsNewInOSX/Articles/MacOSX10_11_2.html#//apple_ref/doc/uid/TP40016630-SW1
            //
            // Note that this is a floating point framebuffer, so the effective linear bit depth for the
            // displayable color range is much lower. E.g., the 16 bpc half-float translate into actual
            // ~ 11 bpc linear precision!
            printf("PTB-INFO: OSX native floating point %i bit per color framebuffer requested, and the OS claims it is working fine. Good.\n", bpc);
            printf("PTB-INFO: Please note that the effective linear output precision will be *much* lower, e.g., only at most 11 bpc for 16 bpc float.\n");
            printf("PTB-INFO: Also, only some very limited subset of Apple hardware can actually output up to 11 bpc precision on a few displays.\n");
            printf("PTB-INFO: As of the year 2016, only the MacPro 2013, and iMac Retina 5k models from late 2014 and late 2015 are claimed\n");
            printf("PTB-INFO: by Apple to support about 10-11 bit output on some supported displays under some conditions!\n");
        }
        else if (bpc >= ((*windowRecord)->depth / 3)) {
            printf("PTB-INFO: Windows native %i bit per color framebuffer requested, and the OS claims it is working. Good.\n", bpc);
        }
        else {
            printf("\nPTB-ERROR: Your script requested a %i bpp, %i bpc framebuffer, but the OS rejected your request and only provides %i bpc.\n",
                   (*windowRecord)->depth, (*windowRecord)->depth / 3, bpc);
            if (PSYCH_SYSTEM == PSYCH_WINDOWS) {
                printf("PTB-ERROR: Some year 2008 and later AMD/ATI FireGL/FirePro and NVidia Quadro cards may support 10 bpc with some drivers,\n");
                printf("PTB-ERROR: but you must enable it manually in the Catalyst or Quadro Control center (somewhere under ''Workstation settings'').\n");
            }
            PsychOSCloseWindow(*windowRecord);
            FreeWindowRecordFromPntr(*windowRecord);
            return(FALSE);
        }
#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)->depth == 33) || ((*windowRecord)->depth == 48)) && !((*windowRecord)->specialflags & kPsychNative10bpcFBActive)) ||
        ((*windowRecord)->depth == 24) || ((*windowRecord)->depth == 32) || ((*windowRecord)->depth == 64) || ((*windowRecord)->depth == 128)) {

        // Floating point framebuffer active?
        isFloatBuffer = FALSE;
        glGetBooleanv(GL_RGBA_FLOAT_MODE_ARB, &isFloatBuffer);
        if (isFloatBuffer) {
            if (PsychPrefStateGet_Verbosity() > 2) printf("PTB-INFO: Floating point precision framebuffer enabled.\n");
        }
        else {
            if (PsychPrefStateGet_Verbosity() > 3) 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) || ((*windowRecord)->depth == 33) || ((*windowRecord)->depth == 48)) {
            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/11 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 (OS native, queried) color resolution of the GPU framebuffer is %i bits per RGB color component.\n", bpc);

        // Make sure user usercode got what it wanted: Warn if it didn't get what it wanted. 32 bpp is an exception, as
        // it is used by old crufty ptb scripts, but not to request a > 8 bpc fb, so omit 32 bpp.
        if (!isFloatBuffer && (bpc * 3 < (*windowRecord)->depth) && ((*windowRecord)->depth != 32) && (PsychPrefStateGet_Verbosity() > 1)) {
            printf("PTB-WARNING: Real color resolution of the GPU framebuffer of %i bits per RGB component is *lower* than the desired %i bits, at a requested pixelsize of %i bpp!\n",
                   bpc, (*windowRecord)->depth / 3, (*windowRecord)->depth);
        }

        // Assign effective color depth as windows 'Pixelsize':
        (*windowRecord)->depth = bpc * 3;
    }
    else {
        // Special 10/11/16 bpc framebuffer activated by our own method:
        bpc = (*windowRecord)->depth / 3;
        if (PsychPrefStateGet_Verbosity() > 2) printf("PTB-INFO: Assuming kernel driver provided color resolution of the GPU framebuffer will be %i bits per RGB color component.\n", bpc);
    }

    // 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);

    // First opened onscreen window in this session? If so, prepare the splash image:
    if (PsychIsLastOnscreenWindow(*windowRecord)) {
        // 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 = (int) 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;
            }
        }
        // End of splash image one-time setup per session.
    }

    // 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 or this is not the first onscreen window open in this session:
            if (!getenv("PTB_SKIPSPLASH") && PsychIsLastOnscreenWindow(*windowRecord))
                splashMinDurationSecs = 10.0;
        }

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

    // 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;

    // 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;

    if (PsychPrefStateGet_Verbosity() > 3) {
        printf("\n\nOpenGL-Vendor / renderer / version are: %s - %s - %s\n", (char*) glGetString(GL_VENDOR), (char*) glGetString(GL_RENDERER), (char*) glGetString(GL_VERSION));
        printf("\n\nOpenGL-Extensions are: %s\n\n", (char*) glGetString(GL_EXTENSIONS));
    }

    // 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);

    // Activate syncing to onset of vertical retrace (VBL) for double-buffered windows:
    if (numBuffers > 1) PsychOSSetVBLSyncLevel(*windowRecord, 1);

    // 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();

    // For external display backends like Vulkan, we rely on that backend to deal with timing
    // and trouble, so only run time-reduced synctests, and skip various warnings if they should
    // fail, as this is strictly non of our business:
    if ((*windowRecord)->specialflags & kPsychExternalDisplayMethod) {
        if (PsychPrefStateGet_Verbosity() > 2)
            printf("PTB-INFO: External display method is in use for this window. Running short and lenient timing tests only.\n");

        if (skip_synctests < 1)
            skip_synctests = 1;
    }

    // If this is a windowed onscreen window, be lenient with synctests. Make sure they never fail,
    // because miserable timing is expected in windowed mode. However, if we are running under a
    // Wayland server with properly working presentation_feedback extension, then presentation timing
    // and timestamping will likely be just as good in windowed mode as in fullscreen mode, at least
    // for the most common Wayland display backends, so we don't need special lenience in that case:
    if (!((*windowRecord)->specialflags & kPsychIsFullscreenWindow) && (skip_synctests < 1) &&
        (((*windowRecord)->winsysType != WAFFLE_PLATFORM_WAYLAND) || ((*windowRecord)->specialflags & kPsychOpenMLDefective))) {
        skip_synctests = 1;
    }

#if PSYCH_SYSTEM == PSYCH_LINUX
#ifndef PTB_USE_WAFFLE
    // Windowed window or transparent window under Linux/X11 with standard display backend?
    if (!((*windowRecord)->specialflags & kPsychExternalDisplayMethod) && ((*windowRecord)->specialflags & kPsychIsX11Window) &&
        (!((*windowRecord)->specialflags & kPsychIsFullscreenWindow) || (PsychPrefStateGet_WindowShieldingLevel() < 2000))) {
        // Yes. Try to enable extended client-compositor sync for improved timing and proper timestamping. Warn user if that
        // is not supported on the given X11 window manager and/or desktop compositor:
        if (!PsychOSEnableX11ClientCompositorSync(*windowRecord) && (PsychPrefStateGet_Verbosity() > 2)) {
            printf("PTB-INFO: Proper timing and timestamping of visual stimulus onset is not reliably supported at all on this desktop GUI\n");
            printf("PTB-INFO: when running in windowed mode (non-fullscreen), or for transparent windows. If PTB aborts with\n");
            printf("PTB-INFO: 'Synchronization failure' you can disable the sync test via a call to Screen('Preference', 'SkipSyncTests', 2).\n");
            printf("PTB-INFO: You won't get proper stimulus onset timestamps in any case though, so windowed mode is of limited use.\n");
            printf("PTB-INFO: Using a desktop GUI like GNOME-3 or Ubuntu desktop, which uses the Mutter compositor, may give better timing if\n");
            printf("PTB-INFO: you opt-in into our new experimental(!) support: Call setenv('PSYCH_EXPERIMENTAL_NETWMTS', '1') at start of your script.\n");
        }
        else {
            PsychOSEnablePresentEventReception(*windowRecord, 0, FALSE);
        }
    }
#endif
#endif

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

    displayMask=CGDisplayIDToOpenGLDisplayMask(cgDisplayID);

    GLint numRenderers;
    error= CGLQueryRendererInfo(displayMask, &rendererInfo, &numRenderers);
    if(numRenderers>1) numRenderers=1;
    for(i=0;i<numRenderers;i++) {
        CGLDescribeRenderer(rendererInfo, i, kCGLRPVideoMemoryMegabytes, &VRAMTotal);
        CGLDescribeRenderer(rendererInfo, i, kCGLRPTextureMemoryMegabytes, &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);

    if ((PsychPrefStateGet_Verbosity() > 3) && ((*windowRecord)->windowIndex == PSYCH_FIRST_WINDOW)) {
        psych_bool 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.\n\n");
        }
    }
#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!

    // 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));
    }

    // Make sure the lockedflush workaround is applied before we first touch
    // the framebuffer of this brand new onscreen window for real via the
    // glClear() call sequence below. The assumption is that the first access
    // to the drawable will also trigger a X11 roundtrip for fb validation:
    PsychLockedTouchFramebufferIfNeeded(*windowRecord);

    // 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((GLclampf) (((float) splash_image.pixel_data[0]) / 255.0), (GLclampf) (((float) splash_image.pixel_data[1]) / 255.0), (GLclampf) (((float) splash_image.pixel_data[2]) / 255.0), 1.0);
        }
        else {
            // No: Clear to white to prepare drawing of our default hard-coded logo:
            glClearColor(1,1,1,1);
        }
    }
    else {
        // Clear to black:
        glClearColor(0,0,0,1);
    }

    // splashTextureRecord already exists? Otherwise create and assign it:
    if (!splashTextureRecord) {
        PsychCreateWindowRecord(&splashTextureRecord);
        splashTextureRecord->windowType = kPsychTexture;
        splashTextureRecord->screenNumber = (*windowRecord)->screenNumber;

        // Assign parent window and copy its inheritable properties:
        PsychAssignParentWindow(splashTextureRecord, *windowRecord);

        // Mark it valid and return handle to userspace:
        PsychSetWindowRecordValid(splashTextureRecord);

        // Ok, setup texture record for RGB8 texture:
        splashTextureRecord->depth = 3 * 8;
        splashTextureRecord->nrchannels = 3;
        PsychMakeRect(splashTextureRecord->rect, 0, 0, splash_image.width, splash_image.height);

        // Orientation is 3 - like an upside down Offscreen window texture.
        splashTextureRecord->textureOrientation = 3;

        // Setting memsize to zero prevents unwanted free() operation in PsychDeleteTexture...
        splashTextureRecord->textureMemorySizeBytes = 0;

        // Assign pointer to pixeldata:
        splashTextureRecord->textureMemory = (GLuint*) &splash_image.pixel_data[0];

        // Let PsychCreateTexture() do the rest of the job of creating, setting up and
        // filling an OpenGL texture with memory buffers image content:
        PsychCreateTexture(splashTextureRecord);

        // 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;

        // Client rect of a texture is always == rect of it, but here we abuse it as
        // target rect:
        PsychCopyRect(splashTextureRecord->clientrect, splashTextureRecord->rect);
        splashTextureRecord->clientrect[kPsychLeft]   += logo_x;
        splashTextureRecord->clientrect[kPsychRight]  += logo_x;
        splashTextureRecord->clientrect[kPsychTop]    += logo_y;
        splashTextureRecord->clientrect[kPsychBottom] += logo_y;

        // Clamp size of logo to size of onscreen window during drawing, so it scales down properly on
        // small displays:
        if (splashTextureRecord->clientrect[kPsychRight] > PsychGetWidthFromRect((*windowRecord)->rect))
            splashTextureRecord->clientrect[kPsychRight] = PsychGetWidthFromRect((*windowRecord)->rect);

        if (splashTextureRecord->clientrect[kPsychBottom] > PsychGetHeightFromRect((*windowRecord)->rect))
            splashTextureRecord->clientrect[kPsychBottom] = PsychGetHeightFromRect((*windowRecord)->rect);
    }

    // Predraw welcome splash image - or neutral background if no splash image is wanted:
    {
        double tDummy;

        // Classic OpenGL-1/2 splash image drawing code:
        glDrawBuffer(GL_BACK_LEFT);

        // Draw and swapbuffers the startup screen 3 times, so everything works with single-/double-/triple-buffered framebuffer setups:
        PsychDrawSplash(*windowRecord, 20);
        PsychOSFlipWindowBuffers(*windowRecord);

        // PsychOSGetSwapCompletionTimestamp can help the Intel ddx under DRI2+SNA to
        // misbehave less if triple-buffering is enabled. It becomes almost useful
        // if we accomodate its current quirks:
        PsychOSGetSwapCompletionTimestamp(*windowRecord, 0, &tDummy);

        // Protect against multi-threading trouble if needed:
        PsychLockedTouchFramebufferIfNeeded(*windowRecord);

        PsychDrawSplash(*windowRecord, 0);
        PsychOSFlipWindowBuffers(*windowRecord);
        PsychOSGetSwapCompletionTimestamp(*windowRecord, 0, &tDummy);
        PsychLockedTouchFramebufferIfNeeded(*windowRecord);

        PsychDrawSplash(*windowRecord, 0);
        PsychOSFlipWindowBuffers(*windowRecord);
        PsychOSGetSwapCompletionTimestamp(*windowRecord, 0, &tDummy);
        PsychLockedTouchFramebufferIfNeeded(*windowRecord);

        // We do it again for right backbuffer to clear possible stereo-contexts as well...
        if (((*windowRecord)->stereomode == kPsychOpenGLStereo) && ((*windowRecord)->gfxcaps & kPsychGfxCapNativeStereo)) {
            glDrawBuffer(GL_BACK_RIGHT);

            PsychDrawSplash(*windowRecord, 20);
            PsychOSFlipWindowBuffers(*windowRecord);
            PsychOSGetSwapCompletionTimestamp(*windowRecord, 0, &tDummy);
            PsychLockedTouchFramebufferIfNeeded(*windowRecord);

            PsychDrawSplash(*windowRecord, 0);
            PsychOSFlipWindowBuffers(*windowRecord);
            PsychOSGetSwapCompletionTimestamp(*windowRecord, 0, &tDummy);
            PsychLockedTouchFramebufferIfNeeded(*windowRecord);

            PsychDrawSplash(*windowRecord, 0);
            PsychOSFlipWindowBuffers(*windowRecord);
            PsychOSGetSwapCompletionTimestamp(*windowRecord, 0, &tDummy);
            PsychLockedTouchFramebufferIfNeeded(*windowRecord);
        }

        glDrawBuffer(GL_BACK);
    }

    // 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:
        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)) &&
                !PsychVRRActive(*windowRecord)) {
                // 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("\nPTB-WARNING: Querying rasterbeam-position doesn't work on your setup! (Returns a constant value %i)\n", i);
                        printf("PTB-WARNING: This can happen if Psychtoolbox gets the mapping of connected displays to graphics card\n");
                        printf("PTB-WARNING: outputs wrong. See 'help DisplayOutputMappings' for tips on how to resolve this problem.\n");
                    }

                    if (i < -1) printf("\nPTB-WARNING: Querying rasterbeam-position doesn't work on your setup! (Returns a negative value %i)\n", i);

                    if ((PsychPrefStateGet_VBLTimestampingMode() == 4) && !((*windowRecord)->specialflags & kPsychOpenMLDefective)) {
                        printf("PTB-WARNING: However, this probably doesn't really matter on your setup for most purposes, as i can use OpenML\n");
                        printf("PTB-WARNING: timestamping instead, which is even more precise. Only few applications need beampos queries in this case.\n");
                    }
                    printf("\n");
                }
            }
            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:

                    // In a VRR/FreeSync/G-Sync/DP-Adaptive sync setup, beampositions will behave
                    // extremely weird if no flip is pending during measurement, so make sure one is pending:
                    if (PsychVRRActive(*windowRecord))
                        PsychOSFlipWindowBuffers(*windowRecord);

                    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 * PsychPrefStateGet_VBLEndlineMaxFactor()) && ((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:
                if (!sync_trouble && PsychPrefStateGet_Verbosity()>1) {
                    printf("\nPTB-WARNING: Couldn't determine end-line of vertical blanking interval for your display! Trouble with beamposition queries?!?\n");
                    printf("PTB-WARNING: Detected end-line is %i, which is either lower or more than %f times higher than vbl startline %i --> Out of sane range!\n", VBL_Endline, PsychPrefStateGet_VBLEndlineMaxFactor(), vbl_startline);
                }
                sync_trouble = true;
                ifi_beamestimate = 0;
            }
            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:
                // Note: This should hopefully also take care of Apple's brain-dead pixel-doubling mode on MBP's with Retina-Display,
                // where the backend is running with 1800 lines vertical resolution, but the framebuffer frontend only with
                // 900 lines effective resolution ("Best for Retina" LOFL...).
                if ((VBL_Endline >= vbl_startline * 2) && (VBL_Endline < vbl_startline * 2.25)) {
                    vbl_startline = vbl_startline * 2;
                    (*windowRecord)->VBL_Startline = vbl_startline;

                    // OSX doesn't support double-scan modes by default, but a Retina display in scaled mode ("Best for Retina")
                    // would also lead to this point. Let's assume its non-native res Retina. We don't know if our timestamps make
                    // sense on such a display: Actual presentation timing might be decoupled from what we think by implicit triple-
                    // buffering caused by some gpu-based panel scaling, unless the used gpu's panel-fitter has some special circuitry
                    // to do it on the fly - which i doubt. No way to find out without photo-diode measurements etc., so better tell user
                    // about potential trouble:
                    if ((PSYCH_SYSTEM == PSYCH_OSX) && (PsychPrefStateGet_Verbosity() > 1)) {
                        printf("PTB-WARNING: Seems this window is displayed on a Retina-Display in scaled mode at a non-native resolution for the display.\n");
                        printf("PTB-WARNING: Reliabiliy of visual stimulus onset timing in such a scaled mode is so far unknown, but may be severely degraded.\n");
                        printf("PTB-WARNING: Stimulus onset timing and returned timestamps may be wrong, with no way for me to automatically detect this.\n");
                    }

                    // Tell about double-scan video mode:
                    if ((PSYCH_SYSTEM != PSYCH_OSX) && (PsychPrefStateGet_Verbosity() > 2)) printf("PTB-INFO: Double-Scan video mode detected as active on display for this window.\n");
                }

                // 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 ConserveVRAMSettings', 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) {
                    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 ConserveVRAMSettings', 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. If ifi_nominal is unavailable, we use
        // ifi_beamestimate as alternative ifi hint for stabilization. Having an alternative is especially
        // important on OSX which reports ifi_nominal == 0 on builtin flat panels and has often noisy timing,
        // especially since OSX 10.9 Mavericks.
        //
        // We skip this flip-based procedure if the special Linux MMIO hack is used to achieve 16 bpc / 64 bpp
        // framebuffers for 48 bit color depth display on modern AMD GCN gpu's with DCE8+ display engine. The
        // hack causes such a massive rendering performance hit even on fast gpu's that this test will definitely
        // fail due to a fps way lower than the video refresh rate of the display, iow. the test would show a false
        // positive, e.g., a 60 Hz display may only be able to flip at 20 fps at most, possibly even slower. On Linux
        // we have various other means to check for proper timing, so this test can be skipped with no harm.
        if (!((*windowRecord)->specialflags & kPsychNative10bpcFBActive) || ((*windowRecord)->depth != 48)) {
            // Performance degrading hack should not be used?
            if (((getenv("R600_DEBUG") && strstr(getenv("R600_DEBUG"), "notiling")) || (getenv("AMD_DEBUG") && strstr(getenv("AMD_DEBUG"), "notiling"))) &&
                (PsychPrefStateGet_Verbosity() > 1)) {
                // Uh, notiling / linear render-/scanoutbuffers requested, although not needed! This will kill perfomance.
                // Probably a leftover from using 16 bpc hack. Advise user:
                printf("\nPTB-WARNING: The environment variables R600_DEBUG or AMD_DEBUG are set to include the keyword 'notiling'. This\n");
                printf("PTB-WARNING: will cause massive graphics performance degradation and possibly failure of the sync tests. Unless\n");
                printf("PTB-WARNING: you are doing this for some low-level debugging purpose, it should be avoided. Either remove the keyword\n");
                printf("PTB-WARNING: from both environment variables and then 'clear all', or if that does not help - or as a simple solution -\n");
                printf("PTB-WARNING: simply quit and restart Octave or Matlab. Will continue, but may fail the timing tests due to this confound...\n\n");
            }

            // 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 of maxStddev (default is less than 200 microseconds) on all systems.
                // If a non-Linux system likely has desktop composition enabled, we are more lenient and allow
                // up to 5x the maxStddev which would end up with 1 msec at default settings:
                stddev = (PsychOSIsDWMEnabled(screenSettings->screenNumber) && (PSYCH_SYSTEM != PSYCH_LINUX)) ? (5 * maxStddev) : maxStddev;
                // If skipping of sync-test is requested, we limit the calibration to 1 sec.
                maxsecs = (skip_synctests) ? 1 : maxDuration;

                // Ok, in case of external display backend, give it 3 seconds for a bit better results:
                if ((*windowRecord)->specialflags & kPsychExternalDisplayMethod)
                    maxsecs = 3;

                retry_count++;
                ifi_estimate = PsychGetMonitorRefreshInterval(*windowRecord, &numSamples, &maxsecs, &stddev, ((ifi_nominal > 0) ? ifi_nominal : ifi_beamestimate), &did_pageflip);

                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");
                    }
                }
            }
        }
        else {
            // 48 bit color depth, 64 bpp, 16 bpc high precision MMIO hack active. Skipped this calibration. Make up some
            // reasonable values to keep going:
            ifi_estimate = (ifi_nominal > 0) ? ifi_nominal : ifi_beamestimate;
            did_pageflip = TRUE;
            numSamples = minSamples;
            stddev = 0.0;
            maxsecs = 0.0;
            (*windowRecord)->nrIFISamples = numSamples;
            (*windowRecord)->IFIRunningSum = ifi_estimate * minSamples;
        }

        // 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 ||
            #if (PSYCH_SYSTEM == PSYCH_LINUX) && !defined(PTB_USE_WAFFLE)
            ((PsychPrefStateGet_VBLTimestampingMode()==4) && PsychOSX11ClientCompositorSyncEnabled(*windowRecord)) ||
            #endif
            ((*windowRecord)->hybridGraphics == 1) || ((*windowRecord)->hybridGraphics == 3) || ((*windowRecord)->hybridGraphics == 4) || ((*windowRecord)->hybridGraphics == 5)) {
            // Flip frame stereo or multiSampling enabled, or some hybrid graphics laptop? 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)->IFIRunningSum /= 2.0;
                (*windowRecord)->VideoRefreshInterval = ifi_estimate;
                if (PsychPrefStateGet_Verbosity()>2) {
                    if ((*windowRecord)->hybridGraphics > 0) {
                        printf("\nPTB-INFO: The timing granularity of stimulus onset/offset via Screen('Flip') is twice as long as\n");
                        printf("PTB-INFO: the refresh interval of your monitor when using dual-gpu hybrid graphics on your setup.\n");
                        printf("PTB-INFO: Please keep this in mind, otherwise you'll be confused about your timing.\n");
                    }

                    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;
    }

    // Does a splashTextureRecord exist and is associated with this about-to-die window?
    if (splashTextureRecord) {
        // Yes. Delete splash-image texture:
        PsychFreeTextureForWindowRecord(splashTextureRecord);
        FreeWindowRecordFromPntr(splashTextureRecord);
        splashTextureRecord = NULL;
    }

    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));
        if (VRAMTotal > 0) printf("PTB-INFO: Renderer has %li MB of VRAM and a maximum %li MB of texture memory.\n", VRAMTotal, TexmemTotal);
        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 (PSYCH_SYSTEM == PSYCH_LINUX) && !defined(PTB_USE_WAFFLE)
            if ((PsychPrefStateGet_VBLTimestampingMode()==4) && PsychOSX11ClientCompositorSyncEnabled(*windowRecord))
                printf("PTB-INFO: Will try to use OS-Builtin experimental NetWM compositor timing for accurate Flip timestamping.\n");
            else
            #endif
            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) && ((*windowRecord)->winsysType != WAFFLE_PLATFORM_WAYLAND)) ? "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");
                }
                else if ((*windowRecord)->specialflags & kPsychExternalDisplayMethod) {
                    printf("PTB-INFO: Beamposition queries are supported, but disabled. Screen('Flip') timestamping will\n");
                    printf("PTB-INFO: fully rely on mechanisms in the external display backend, with unknown precision.\n");
                }
                else {
                    printf("PTB-INFO: Beamposition queries are supported, but disabled. Using basic timestamping as fallback:\n");
                    printf("PTB-INFO: Timestamps returned by Screen('Flip') will be therefore less robust and accurate.\n");
                }
            }
        }
        else {
            #if (PSYCH_SYSTEM == PSYCH_LINUX) && !defined(PTB_USE_WAFFLE)
            if ((PsychPrefStateGet_VBLTimestampingMode()==4) && PsychOSX11ClientCompositorSyncEnabled(*windowRecord))
                printf("PTB-INFO: Will try to use OS-Builtin experimental NetWM compositor timing for accurate Flip timestamping.\n");
            else
            #endif
            if ((PsychPrefStateGet_VBLTimestampingMode()==4) &&
                (!((*windowRecord)->specialflags & kPsychOpenMLDefective) || ((*windowRecord)->hybridGraphics == 5))) {
                if ((*windowRecord)->hybridGraphics == 3 || (*windowRecord)->hybridGraphics == 4) {
                    printf("PTB-INFO: Will try to use my PRIME custom modesetting-ddx protocol for accurate Flip timestamping.\n");
                } else if ((*windowRecord)->hybridGraphics == 5) {
                    printf("PTB-INFO: Will try to use my custom X11/Present feedback method for accurate Flip timestamping.\n");
                } else {
                    printf("PTB-INFO: Will try to use OS-Builtin %s for accurate Flip timestamping.\n",
                           ((PSYCH_SYSTEM == PSYCH_LINUX) && ((*windowRecord)->winsysType != WAFFLE_PLATFORM_WAYLAND)) ? "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 if ((*windowRecord)->specialflags & kPsychExternalDisplayMethod) {
                printf("PTB-INFO: Beamposition queries unsupported or defective on this system. Screen('Flip') timestamping will\n");
                printf("PTB-INFO: fully rely on mechanisms in the external display backend, with unknown precision and reliability.\n");
            }
            else {
                printf("PTB-INFO: Beamposition queries unsupported or defective on this system. Using basic timestamping as fallback.\n");
                printf("PTB-INFO: Timestamps returned by Screen('Flip') will be therefore 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 (PsychVRRActive(*windowRecord)) {
            printf("PTB-INFO: Enabling Variable Refresh Rate VRR mode, using method %i and timing style %i.\n", (*windowRecord)->vrrMode, (*windowRecord)->vrrStyleHint);
            printf("PTB-INFO: Assuming minimum VRR refresh duration %f msecs, maximum duration %f msecs.\n", 1000 * (*windowRecord)->vrrMinDuration, 1000 * (*windowRecord)->vrrMaxDuration);
            printf("PTB-INFO: Using initial VRR latency compensation of %f msecs.\n", 1000 * (*windowRecord)->vrrLatencyCompensation);
        }

        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 ((*windowRecord)->stereomode==kPsychDualStreamStereo) printf("PTB-INFO: Stereo display via dual-stream stereo for special consumers with imaging pipeline enabled.\n");

        if (PsychPrefStateGet_3DGfx() > 0) printf("PTB-INFO: Support for OpenGL 3D graphics rendering enabled: depth-buffer and 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?
            // We require at least 2 samples for concluding it is erroneously enabled, instead of at least
            // 1 sample. Why? Because most Intel graphics drivers on MS-Windows have a bug where they report
            // number of samples == 1 even if multisampling is disabled, therefore causing false alarms/clutter.
            // Let's declutter this a bit by being more lenient:
            if ((*windowRecord)->multiSample > 1) {
                // 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(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...
        if (glewIsSupported("GL_NV_multisample_filter_hint"))
            glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_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(GL_MULTISAMPLE_ARB);
        while(glGetError()!=GL_NO_ERROR);

        // Set it to zero, so remaining code can rely on zero == off:
        (*windowRecord)->multiSample = 0;
    }

    // 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 (!((*windowRecord)->specialflags & kPsychExternalDisplayMethod)) {
        // Check for desktop compositor activity on MS-Windows Vista and later:
        if ((PsychPrefStateGet_Verbosity() > 1) && PsychIsMSVista() && PsychOSIsDWMEnabled(0)) {
            #if PSYCH_SYSTEM == PSYCH_WINDOWS
            // Regular fullscreen onscreen window on Windows-10 or later OS'es?
            if (PsychOSIsMSWin10() && ((*windowRecord)->specialflags & kPsychIsFullscreenWindow) && (PsychPrefStateGet_WindowShieldingLevel() >= 2000)) {
                // Yes. Initial testing suggests we might be mostly fine timing-wise despite the DWM being active, both
                // on single-display and multi-display setups. Therefore tone down the DWM warnings and just tell the user
                // to tread carefully:
                printf("PTB-INFO: ==============================================================================================================================\n");
                printf("PTB-INFO: WINDOWS DWM DESKTOP COMPOSITOR IS ACTIVE. On this Windows-10 or later system, Psychtoolbox can no longer reliably detect if\n");
                printf("PTB-INFO: this will cause trouble for timing and integrity of visual stimuli or not. You might be just fine, or you could be in trouble.\n");
                printf("PTB-INFO: Use external measurement equipment and independent procedures to verify reliability of timing if you care about proper timing.\n");
                printf("PTB-INFO: ==============================================================================================================================\n");
            }
            else {
                // DWM is active on at least one display. On a single-display setup, this means
                // it will definitely affect/interfere with our onscreen windows timing and we should
                // warn the user about likely performance and timing degradation. The same is true if
                // our onscreen window is not a fullscreen window, in which case it will always interfere
                // with our window:
                if ((PsychGetNumDisplays() == 1) || !((*windowRecord)->specialflags & kPsychIsFullscreenWindow)) {
                    // Ok, DWM will definitely mess with our stimuli: Warn the user about the likely hazard.
                    printf("PTB-WARNING: ==============================================================================================================================\n");
                    printf("PTB-WARNING: WINDOWS DWM DESKTOP COMPOSITOR IS ACTIVE! ALL FLIP STIMULUS ONSET TIMESTAMPS WILL BE VERY LIKELY UNRELIABLE AND LESS ACCURATE!\n");
                    printf("PTB-WARNING: STIMULUS ONSET TIMING WILL BE UNRELIABLE AS WELL, AND GRAPHICS PERFORMANCE MAY BE SEVERELY REDUCED! STIMULUS IMAGES MAY NOT\n");
                    printf("PTB-WARNING: SHOW UP AT ALL! DO NOT USE THIS MODE FOR RUNNING REAL EXPERIMENT SESSIONS WITH ANY REQUIREMENTS FOR ACCURATE TIMING!\n");
                    printf("PTB-WARNING: ==============================================================================================================================\n");
                }
                else {
                    // This is a multi-display setup with the DWM active on at least some display(s) and our
                    // stimulus onscreen window is a fullscreen window that covers at least one whole display.
                    // We can't know if our stimulus display is affected, or only other irrelevant GUI desktop
                    // displays. At least on one tested recent versions of Windows-7 and presumably Windows-8,
                    // DWM was interfering massively with fullscreen stimulus displays, leading to completely
                    // wrong stimulus onset timestamps:
                    printf("PTB-WARNING: ============================================================================================================================\n");
                    printf("PTB-WARNING: WINDOWS DWM DESKTOP COMPOSITOR IS ACTIVE ON AT LEAST ONE DISPLAY! ALL FLIP STIMULUS ONSET TIMESTAMPS WILL BE LIKELY WRONG!\n");
                    printf("PTB-WARNING: STIMULUS ONSET TIMING WILL BE UNRELIABLE AS WELL, AND GRAPHICS PERFORMANCE MAY BE SEVERELY REDUCED! STIMULUS IMAGES MAY NOT\n");
                    printf("PTB-WARNING: SHOW UP AT ALL! DO NOT USE THIS MODE FOR RUNNING REAL EXPERIMENT SESSIONS WITH ANY REQUIREMENTS FOR ACCURATE TIMING!\n");
                    printf("PTB-WARNING: ============================================================================================================================\n");
                }
            }
            #endif
        }
        else if ((PsychPrefStateGet_Verbosity() > 1) && PsychIsMSVista() && (PsychGetNumDisplays() > 1)) {
            // MS-Vista or later with DWM effectively disabled/inactive. On a single display setup,
            // this seems to be fine. On a dual-display setup, timing was wrong as well.
            printf("PTB-WARNING: ==============================================================================================================\n");
            printf("PTB-WARNING: WINDOWS SYSTEM IS RUNNING IN MULTI-DISPLAY MODE! ALL FLIP STIMULUS ONSET TIMESTAMPS WILL BE LIKELY UNRELIABLE!\n");
            printf("PTB-WARNING: STIMULUS ONSET TIMING WILL BE UNRELIABLE AS WELL, AND GRAPHICS PERFORMANCE MAY BE SEVERELY REDUCED!\n");
            printf("PTB-WARNING: DO NOT USE MULTI-DISPLAY MODE FOR RUNNING REAL EXPERIMENT SESSIONS WITH ANY REQUIREMENTS FOR ACCURATE TIMING!\n");
            printf("PTB-WARNING: ==============================================================================================================\n");
        }

        // Check for desktop compositor activity on OSX: Check not (yet) applicable on Linux, as it is only reliable with KDE/KWin...
        if ((PSYCH_SYSTEM == PSYCH_OSX) && PsychOSIsDWMEnabled(screenSettings->screenNumber) && (PsychPrefStateGet_Verbosity() > 1)) {
            // Desktop compositor active for our onscreen window. Explain consequences to user:
            printf("PTB-WARNING: ==================================================================================================================\n");
            printf("PTB-WARNING: DESKTOP COMPOSITOR IS ACTIVE! ALL FLIP STIMULUS ONSET TIMESTAMPS WILL BE VERY LIKELY UNRELIABLE AND LESS ACCURATE!\n");
            printf("PTB-WARNING: STIMULUS ONSET TIMING WILL BE UNRELIABLE AS WELL, AND GRAPHICS PERFORMANCE MAY BE REDUCED!\n");
            printf("PTB-WARNING: DO NOT USE THIS MODE FOR RUNNING REAL EXPERIMENT SESSIONS WITH ANY REQUIREMENTS FOR ACCURATE TIMING!\n");
            printf("PTB-WARNING: ==================================================================================================================\n");
        }
    }

    if ((skip_synctests < 2) && !((*windowRecord)->specialflags & kPsychExternalDisplayMethod)) {
        // Reliable estimate? These are our minimum requirements:
        // At least minSamples valid samples collected.
        // stddev (== noise) not greater than maxStddev, unless we are sure pageflipping under our full control was used during
        // refresh calibration and sync tests, in which case we tolerate up to 3 times maxStddev. Why? Because pageflipping under
        // our control rules out interference of a desktop compositor or other sources of triple buffering, so if timing noise is
        // the only unusual symptom it is likely to be just noise, not a symptom of a compositor at work.
        if ((numSamples < minSamples) || (!did_pageflip && (stddev > maxStddev)) || (did_pageflip && (stddev > 3 * 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;
        }
    } // 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) || !skip_synctests) {
            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");
        }

        // Apparently the busy-wait for VBL before bufferswap request workaround did not help
        // in passing the 3rd run of the sync tests. Let's disable it again, as it can actually
        // make things worse if one uses skip sync test setting 1, by throttling to vblank twice,
        // once in hardware, once by our workaround. If user wants us to limp along, let us at
        // least as fast as we can:
        (*windowRecord)->specialflags &= ~kPsychBusyWaitForVBLBeforeBufferSwapRequest;

        // 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 seems to work and we have a valid estimate of monitor refresh interval...

    // VRR requested, but not working?
    if (((*windowRecord)->vrrMode) && !PsychVRRActive(*windowRecord)) {
        // We abort, unless in skip_synctests mode. If the usercode requires VRR for this study then it
        // would screw up the results if we'd run fixed refresh rate instead:
        if (PsychPrefStateGet_Verbosity() > 0) {
            printf("\n\n");
            printf("----- ! PTB - ERROR: VRR FAILURE ! -----\n\n");
            printf("One or more internal checks (see Warnings above) indicate that Variable Refresh Rate mode (VRR)\n");
            printf("for fine-grained stimulus onset timing is not working for this onscreen window on your setup.\n\n");
            printf("This will seriously impair proper stimulus presentation timing!\n");
            printf("Please read 'help VRRSupport' for information about how to solve the problem.\n");
            printf("You can force Psychtoolbox to continue, despite the severe problems, by adding the command\n");
            printf("Screen('Preference', 'SkipSyncTests', 2); 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 < 2) {
            // We abort! Close the onscreen window:
            PsychOSCloseWindow(*windowRecord);

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

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

        // User wants us to continue despite VRR failure. Mark VRR as disabled:
        (*windowRecord)->vrrMode = kPsychVRROff;

        // Flash our visual warning bell at alert-level for 1 second if skipping sync tests is requested:
        PsychVisualBell((*windowRecord), 1, 2);
    }

    // Check for mismatch between measured ifi from beamposition and from 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!
    // This condition may not hold on a G-Sync/FreeSync setup, so skip fail in that case.
    if ((ifi_beamestimate < 0.8 * ifi_estimate || ifi_beamestimate > 1.2 * ifi_estimate) && (ifi_beamestimate > 0) &&
        !PsychVRRActive(*windowRecord)) {
        // Mismatch! This is bad and often the case on Windows-10 multi-display setups, due to
        // DWM compositor interference, especially if different displays run at different nominal
        // refresh rates.
        // Normal setup with OpenGL display backend?
        if (!((*windowRecord)->specialflags & kPsychExternalDisplayMethod)) {
            // Game over for rasterbeam position queries: Inaccurate, but better safe than sorry...
            if (!sync_trouble && PsychPrefStateGet_Verbosity()>1)
                printf("\nPTB-WARNING: Mismatch between measured monitor refresh intervals! This indicates problems with rasterbeam position queries.\n");

            sync_trouble = true;
        }
        else {
            // External display method used for display and display timing/timestamping.
            // At least on MS-Windows on a dual-display setup, measured ifi_estimate can
            // be as wrong as it is irrelevant to timing/timstamping, but we may still
            // need our beamposition query timestamping to augment the external display
            // method, e.g., Vulkan without native Vulkan timestamping support.
            //
            // Therefore we use ifi_nominal as a reference to check plausibility of
            // ifi_beamestimate, as that is more likely to be right, even on MS-Windows
            // dual-display setups, where the DWM compositor may interfere with ifi_estimate.
            // Testing against hw equipment showed errors of more than 5 msecs at 60 Hz if
            // we can not utilize beampos timestamping on Windows-10, so we really want this.
            if ((ifi_beamestimate < 0.8 * ifi_nominal || ifi_beamestimate > 1.2 * ifi_nominal) && (ifi_nominal > 0)) {
                // Also mismatch with ifi_nominal -> Most likely beamposition queries are defective, so give up:
                if (!sync_trouble && PsychPrefStateGet_Verbosity() > 1)
                    printf("\nPTB-WARNING: Mismatch between beamposition-measured and OS reported monitor refresh interval! This indicates problems with rasterbeam position queries.\n");

                sync_trouble = true;
            }
            else {
                // Matching values -> Assume the ifi_estimate is wrong due to compositor interference.
                // Keep beamposition method going, and make it new reference for beampos timestamping:
                (*windowRecord)->VideoRefreshInterval = (!sync_trouble) ? ifi_beamestimate : ifi_nominal;

                if (PsychPrefStateGet_Verbosity() > 1)
                    printf("\nPTB-WARNING: Mismatch between measured monitor refresh intervals! Assuming VBL sync measurement is wrong, overriding reference refresh interval to %f msecs.\n",
                           1000 * (*windowRecord)->VideoRefreshInterval);
            }
        }
    }

    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;

        // Only warn user and flash the warning triangle if we can't use OpenML timestamping because it is disabled or broken.
        // If OpenML timestamping is available then beamposition queries are not needed anyway, so no reason to make a big fuss...
        if ((PsychPrefStateGet_Verbosity() > 1) && ((PsychPrefStateGet_VBLTimestampingMode() != 4) ||
            (((*windowRecord)->specialflags & kPsychOpenMLDefective) && ((*windowRecord)->hybridGraphics != 5)))) {
            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));
    (*windowRecord)->flipInfo->flipwhen = -DBL_MAX;

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

    // Done.
    return(TRUE);
}

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();

        // Make sure our main OpenGL context is active for final cleanup:
        PsychSetGLContext(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);

        // 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 - about to be 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);

        // Reduce count of onscreen windows with our own threaded framesequential stereo mode active:
        if (windowRecord->stereomode == kPsychFrameSequentialStereo) {
            frameSeqStereoActive--;

            // Perform shutdown of shutter goggle driver if needed:
            PsychSetupShutterGoggles(windowRecord, FALSE);
        }

        // Reduce count of onscreen windows with our own threaded VRR scheduler active:
        if (windowRecord->vrrMode == kPsychVRROwnScheduled)
            vrrSchedulersActive--;

        // 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;
            frameSeqStereoActive = 0;
            vrrSchedulersActive = 0;
        }

        // Release dynamically allocated splash image buffer, if any, if this is the last onscreen window to be closed:
        if (PsychIsLastOnscreenWindow(windowRecord) && (splash_image.bytes_per_pixel == GL_RGB)) {
            free(splash_image.pixel_data);
            splash_image.pixel_data = NULL;
            splash_image.bytes_per_pixel = 0;
        }
    }
    else if(windowRecord->windowType==kPsychTexture) {
        // Texture or Offscreen window - which is also just a form of texture.
        PsychFreeTextureForWindowRecord(windowRecord);

        // Execute hook chain for OpenGL related shutdown:
        PsychPipelineExecuteHook(windowRecord, kPsychCloseWindowPreGLShutdown, NULL, NULL, FALSE, FALSE, NULL, NULL, NULL, NULL);

        // 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);

        // Execute hook chain for final non-OpenGL related shutdown:
        PsychPipelineExecuteHook(windowRecord, kPsychCloseWindowPostGLShutdown, NULL, NULL, FALSE, FALSE, NULL, NULL, NULL, NULL);
    }
    else if(windowRecord->windowType==kPsychProxyWindow) {
        // Proxy window object without associated OpenGL state or content.

        // Execute hook chain for OpenGL related shutdown:
        PsychPipelineExecuteHook(windowRecord, kPsychCloseWindowPreGLShutdown, NULL, NULL, FALSE, FALSE, NULL, NULL, NULL, NULL);

        // Run shutdown sequence for imaging pipeline in case the proxy has bounce-buffer or
        // lookup table textures or FBO's attached:
        PsychShutdownImagingPipeline(windowRecord, TRUE);

        // Execute hook chain for final non-OpenGL related shutdown:
        PsychPipelineExecuteHook(windowRecord, kPsychCloseWindowPostGLShutdown, NULL, NULL, FALSE, FALSE, NULL, NULL, NULL, NULL);
    }
    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() && !(windowRecord->specialflags & kPsychExternalDisplayMethod)) {
        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();
}

/* PsychSetupShutterGoggles()
 *
 * Setup/Enable or Disable support for NVidia's NVision stereo emitter kit, which
 * allows to drive stereo shutter goggles from NVidia in frame-sequential stereo mode.
 *
 * We use the LGPL licensed libnvstusb to do this. It is dynamically loaded as a shared
 * library, as is the firmware that needs to be loaded into the NVision controller.
 * libnvstusb emits the neccessary USB trigger packets as usb bulk transfers, using
 * the reverse-engineered protocol of the NVision USB controller.
 *
 * If no libnvstusb.so library is found, no firmware file is found, or no suitable
 * controller is connected then this function does nothing and we end up with the
 * usual blue line sync stereo. The function is currently also only for Linux. Porting
 * to OSX or Windows would be possible with small adjustments here and more significant
 * but doable modifications to libnvstusb.
 *
 */
void PsychSetupShutterGoggles(PsychWindowRecordType *windowRecord, psych_bool doInit)
{
    #ifdef PTB_USE_NVSTUSB
        char pluginPath[FILENAME_MAX];
        char firmwareFile[FILENAME_MAX];
        char pluginName[100];
        char backupenv[100];
        FILE *fw;

        // Init or shutdown?
        if (doInit) {
            // Init.

            // Try to find required firmware file. First in the Psychtoolbox configuration directory, then in the users $HOME directory:
            sprintf(firmwareFile, "%snvstusb.fw", PsychRuntimeGetPsychtoolboxRoot(TRUE));
            errno = 0;
            if ((fw = fopen(firmwareFile, "rb")) != NULL) {
                fclose(fw);
            }
            else {
                if (PsychPrefStateGet_Verbosity() > 4)
                    printf("PTB-DEBUG: PsychSetupShutterGoggles: Could not find NVision firmware file at '%s' [%s].\n", firmwareFile, strerror(errno));

                sprintf(firmwareFile, "%s/nvstusb.fw", getenv("HOME"));
                errno = 0;
                if ((fw = fopen(firmwareFile, "rb")) != NULL) {
                    fclose(fw);
                }
                else {
                    if (PsychPrefStateGet_Verbosity() > 4)
                        printf("PTB-DEBUG: PsychSetupShutterGoggles: Could not find NVision firmware file at '%s' [%s].\n", firmwareFile, strerror(errno));
                    if (PsychPrefStateGet_Verbosity() > 2)
                        printf("PTB-INFO: Could not find any firmware file for NVidia NVision stereo goggles, therefore not driving such goggles.\n");
                    return;
                }
            }

            // Firmware file found - Probably the user wants us to drive such Goggles.

            // Plugin already loaded and linked?
            if (NULL == nvstusb_plugin) {
                // No. Try to load and bind the library:
                sprintf(pluginName, "libnvstusb.so");

                // Try to get it from the PsychPlugins folder:
                if (strlen(PsychRuntimeGetPsychtoolboxRoot(FALSE)) > 0) {
                    // Yes! Assemble full path name to plugin:
                    sprintf(pluginPath, "%sPsychBasic/PsychPlugins/%s", PsychRuntimeGetPsychtoolboxRoot(FALSE), pluginName);
                    if (PsychPrefStateGet_Verbosity() > 5) printf("PTB-DEBUG: PsychSetupShutterGoggles: Trying to load external driver plugin from following file: [ %s ]\n", pluginPath);
                }
                else {
                    // Failed! Assign only plugin name and hope the user installed the plugin into
                    // a folder on the system library search path:
                    sprintf(pluginPath, "%s", pluginName);
                    if (PsychPrefStateGet_Verbosity() > 2) printf("PTB-INFO: PsychSetupShutterGoggles: Failed to find installation directory for external driver plugin [ %s ].\nHoping it is somewhere in the library search path...\n", pluginPath);
                }

                if ((dlopen("libusb-1.0.so.0", RTLD_NOW | RTLD_GLOBAL | RTLD_NOLOAD) == NULL) &&
                    (PsychPrefStateGet_Verbosity() > 0)) {
                    printf("PTB-DEBUG: PsychSetupShutterGoggles: Failed to reopen libusb-1.0.so.0 in no-reload-mode [%s]. nvstusb plugin load will likely fail...\n", (const char*) dlerror());
                }

                nvstusb_plugin = dlopen(pluginPath, RTLD_NOW | RTLD_GLOBAL);
                if (NULL == nvstusb_plugin) {
                    // First try failed:
                    if (PsychPrefStateGet_Verbosity() > 3) {
                        printf("PTB-DEBUG: PsychSetupShutterGoggles: Failed to load external driver plugin [%s]. Retrying under generic name [%s].\n", (const char*) dlerror(), pluginName);
                    }

                    sprintf(pluginPath, "%s", pluginName);
                    nvstusb_plugin = dlopen(pluginPath, RTLD_NOW | RTLD_GLOBAL);
                }

                if (NULL == nvstusb_plugin) {
                    // Game over.
                    if (PsychPrefStateGet_Verbosity() > 2)
                        printf("PTB-INFO: PsychSetupShutterGoggles: Could not load stereo goggle driver plugin [%s]. Goggle support disabled.\n", (const char*) dlerror());
                    return;
                }

                // Plugin loaded. Bind entry points:
                Nvstusb_init_proc = dlsym(nvstusb_plugin, "nvstusb_init");
                Nvstusb_deinit_proc = dlsym(nvstusb_plugin, "nvstusb_deinit");
                Nvstusb_set_rate_proc = dlsym(nvstusb_plugin, "nvstusb_set_rate");
                Nvstusb_swap_proc = dlsym(nvstusb_plugin, "nvstusb_swap");
                Nvstusb_get_keys_proc = dlsym(nvstusb_plugin, "nvstusb_get_keys");
                Nvstusb_invert_eyes_proc = dlsym(nvstusb_plugin, "nvstusb_invert_eyes");

                nvsttriggerdelay = 0;
                if (getenv("PTB_NVISION3D_DELAY"))
                    nvsttriggerdelay = atof(getenv("PTB_NVISION3D_DELAY"));

                // Successfully linked?
                if (!Nvstusb_init_proc || !Nvstusb_deinit_proc || !Nvstusb_set_rate_proc || !Nvstusb_swap_proc || !Nvstusb_get_keys_proc || !Nvstusb_invert_eyes_proc) {
                    if (PsychPrefStateGet_Verbosity() > 2)
                        printf("PTB-INFO: PsychSetupShutterGoggles: Could not load link goggle driver plugin [%s]. Goggle support disabled.\n", (const char*) dlerror());

                    // Request shutdown of stuff again:
                    doInit = FALSE;
                }
            }

            // Still green to go? Otherwise we fall through to the shutdown path:
            if (doInit) {
                // Ok, the plugin is ready. Let's see if we have actual goggles to drive.

                // First we set the environment variable __GL_SYNC_TO_VBLANK before
                // calling Nvstusb_init_proc(). This will cause the init routine to
                // select a drive strategy for the goggles that is optimal for our
                // frame sequential stereo implementation. Specifically it will simply
                // immediately send the USB goggle trigger packet to the goggles when
                // we ask it to do so. We make sure to call the function after swap
                // completion is confirmed by the OS.

                // Step 1: Backup the current setting of __GL_SYNC_TO_VBLANK:
                if (getenv("__GL_SYNC_TO_VBLANK")) {
                    strcpy(backupenv, getenv("__GL_SYNC_TO_VBLANK"));
                }
                else {
                    backupenv[0] = 0;
                }

                // Step 2: Force it to our wanted value:
                setenv("__GL_SYNC_TO_VBLANK", "1", 1);

                // Step 3: Init library:
                nvstusb_goggles = Nvstusb_init_proc(firmwareFile);

                // Step 4: Restore old setting of __GL_SYNC_TO_VBLANK, if any:
                if (backupenv[0] != 0) {
                    setenv("__GL_SYNC_TO_VBLANK", backupenv, 1);
                }
                else {
                    unsetenv("__GL_SYNC_TO_VBLANK");
                }

                // Step 5: Check if init actually worked:
                if (NULL == nvstusb_goggles) {
                    // Nope. We are done here.
                    if (PsychPrefStateGet_Verbosity() > 2)
                        printf("PTB-INFO: Could not connect to NVidia NVision stereo goggles, therefore not enabling such goggles.\n");
                    doInit = FALSE;
                }
                else {
                    // Yes. Set expected update rate as video refresh rate:
                    Nvstusb_set_rate_proc(nvstusb_goggles, (float) (1.0 / windowRecord->VideoRefreshInterval));
                    if (PsychPrefStateGet_Verbosity() > 2)
                        printf("PTB-INFO: Activated NVidia NVision stereo goggles.\n");
                    return;
                }
            }
        }

        // Shutdown requested?
        if (!doInit) {
            // Perform cleanup and shutdown:
            if (nvstusb_goggles) {
                // Goggles online. Deinit them and the library:
                if (PsychPrefStateGet_Verbosity() > 2)
                    printf("PTB-INFO: Shutting down NVidia NVision stereo goggles.\n");

                Nvstusb_deinit_proc(nvstusb_goggles);
                nvstusb_goggles = NULL;
            }

            if (nvstusb_plugin) {
                if (PsychPrefStateGet_Verbosity() > 4)
                    printf("PTB-DEBUG: Unloading nvstusb library.\n");

                Nvstusb_init_proc = NULL;
                Nvstusb_deinit_proc = NULL;
                Nvstusb_set_rate_proc = NULL;
                Nvstusb_swap_proc = NULL;
                Nvstusb_get_keys_proc = NULL;
                Nvstusb_invert_eyes_proc = NULL;
                dlclose(nvstusb_plugin);
                nvstusb_plugin = NULL;
            }

            // Done.
            return;
        }

    #endif
}

/* PsychTriggerShutterGoggles()
 *
 * Use libnvstusb to emit USB stereo trigger packets to a NVidia NVision stereo controller.
 * Do nothing if no such setup was enabled via PsychSetupShutterGoggles().
 *
 */
void PsychTriggerShutterGoggles(PsychWindowRecordType *windowRecord, int viewid)
{
    static int oldviewid = -1;
    double dT;

    // Delay trigger emission if requested:
    if (nvsttriggerdelay > 0) {
        PsychWaitUntilSeconds(windowRecord->time_at_last_vbl + nvsttriggerdelay);
        // Delayed triggering only makes sense if we want to trigger for the other
        // eye - the one that will present at next refresh, not the one that is
        // currently presenting. Therefore switch left-right eye trigger:
        viewid = 1 - viewid;
    }

    PsychGetAdjustedPrecisionTimerSeconds(&dT);
    dT = 1000 * (dT - windowRecord->time_at_last_vbl);

    #ifdef PTB_USE_NVSTUSB
        // Emit shutter trigger if frameSeqStereoActive and NVideo NVision driver active:
        if (nvstusb_plugin && nvstusb_goggles && (windowRecord->stereomode == kPsychFrameSequentialStereo)) {
            Nvstusb_swap_proc(nvstusb_goggles, ((viewid == 0) ? nvstusb_left : nvstusb_right), NULL);
            if ((PsychPrefStateGet_Verbosity() > 9) || (nvsttriggerdelay < 0))
                printf("PTB-DEBUG: PsychTriggerShutterGoggles: Triggering for viewid %i, dT since vblank in msecs: %f\n", viewid, dT);
        }
        else {
            if ((PsychPrefStateGet_Verbosity() > 9) || (nvsttriggerdelay < 0))
                printf("PTB-DEBUG: PsychTriggerShutterGoggles: No-Op call for viewid %i, dT since vblank in msecs: %f\n", viewid, dT);
        }
    #endif

    // Check if stereo views alternate nicely at each invocation:
    if ((oldviewid != -1) && (oldviewid != 1 - viewid)) {
        if (PsychPrefStateGet_Verbosity() > 9)
            printf("PTB-WARNING: Frame sequential stereo sequencing order mismatch. Double take on viewid %i. Visual glitch expected in stereo presentation.\n", viewid);
    }
    oldviewid = viewid;
}

#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);

                // Protect against multi-threading trouble if needed:
                PsychLockedTouchFramebufferIfNeeded(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 = 0;
    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 = ((PsychPrefStateGet_VBLTimestampingMode() == 4) && (!(windowRecord->specialflags & kPsychOpenMLDefective) || (windowRecord->hybridGraphics == 5)));

    // Assign a name to ourselves, for debugging:
    PsychSetThreadName("ScreenFlipper");

    // 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
            PsychLockDisplay();
            #ifndef PTB_USE_WAFFLE
                glXMakeCurrent(windowRecord->targetSpecific.deviceContext, windowRecord->targetSpecific.windowHandle, windowRecord->targetSpecific.glswapcontextObject);
            #else
                if (!waffle_make_current(windowRecord->targetSpecific.deviceContext, windowRecord->targetSpecific.windowHandle, windowRecord->targetSpecific.glswapcontextObject) &&
                    (PsychPrefStateGet_Verbosity() > 0)) {
                        printf("\nPTB-ERROR: Failed to bind OpenGL context for async flip thread [%s]! This will end badly...\n", waffle_error_to_string(waffle_error_get_code()));
                }
            #endif
            PsychUnlockDisplay();
        #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);
    }

    // Are we supposed to use sRGB rendering and blending on capable framebuffers? Enable, if so:
    if (windowRecord->imagingMode & kPsychEnableSRGBRendering)
        glEnable(GL_FRAMEBUFFER_SRGB);

    // Is this a true async flip and not something else like stereo or vrr?
    if ((windowRecord->stereomode != kPsychFrameSequentialStereo) && (windowRecord->vrrMode != kPsychVRROwnScheduled)) {
        // 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) {
            // EGL-backed windows need special treatment:
            if (windowRecord->specialflags & kPsychIsEGLWindow) {
                // We must unbind our context, so the masterthread can bind its context(s) to our EGL backing surface
                // in order to perform rendering, even without imaging pipeline active, ie. to regular backbuffer:
                PsychOSUnsetGLContext(windowRecord);
            }

            // 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:
            if (oldStyle) {
                // Old style method: Attach to context - It's detached in the main thread:
                PsychSetGLContext(windowRecord);
            }
            else if (windowRecord->specialflags & kPsychIsEGLWindow) {
                // New style method: For EGL backend on Linux + Waffle, we need to rebind
                // our swap context to the windows EGL backing surface, knowing the master
                // thread doesn't have the surface bound and won't bind it until we're fully
                // done with the swap:
                #if PSYCH_SYSTEM == PSYCH_LINUX
                    PsychLockDisplay();
                    #ifdef PTB_USE_WAFFLE
                        if (!waffle_make_current(windowRecord->targetSpecific.deviceContext, windowRecord->targetSpecific.windowHandle, windowRecord->targetSpecific.glswapcontextObject) &&
                            (PsychPrefStateGet_Verbosity() > 0)) {
                            printf("\nPTB-ERROR: Failed to rebind OpenGL context for async flip thread [%s]! This will end badly...\n", waffle_error_to_string(waffle_error_get_code()));
                        }
                    #endif
                    PsychUnlockDisplay();
                #endif
            }

            // 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.
    } // End of standard Async flip code.

    if (windowRecord->stereomode == kPsychFrameSequentialStereo) {
        // 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...

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

        // 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) == (psych_uint64) 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));

                // Trigger an update of the shutters of potentially connected stereo goggles:
                PsychTriggerShutterGoggles(windowRecord, viewid);

                // 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);

                // Trigger an update of the shutters of potentially connected stereo goggles:
                viewid = 1 - viewid;
                PsychTriggerShutterGoggles(windowRecord, viewid);

                // 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);

                // Protect against multi-threading trouble if needed:
                PsychLockedTouchFramebufferIfNeeded(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.
                    PsychWaitPixelSyncToken(windowRecord, FALSE);
                    PsychGetAdjustedPrecisionTimerSeconds(&(windowRecord->time_at_last_vbl));
                }

                // Trigger an update of the shutters of potentially connected stereo goggles:
                viewid = 1 - viewid;
                PsychTriggerShutterGoggles(windowRecord, viewid);

                // 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.
    } // End of frame-sequential stereo code.

    if (windowRecord->vrrMode == kPsychVRROwnScheduled) {
        // Homegrown VRR scheduler dispatch loop: Repeats infinitely, processing one flip request or "prevent lfc kick-in" per loop iteration.
        // Well, not infinitely, but until we receive a shutdown request and terminate ourselves...

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

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

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

        // Dispatch loop:
        while (TRUE) {
            // Do we need a new vrr 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));
                }
            }

            // Update our last vblank end timestamp: On Linux we can query the actual
            // timestamp at any time iff the open-source graphics drivers are used.
            // On Linux with NVidia proprietary blob or on Windows and macOS, no such
            // luck, and especially not in a VRR configuration! On such setups we fall
            // back to approximation trickery which may or may not work reliable:
            lastvbl = PsychOSGetVBLTimeAndCount(windowRecord, &vblcount);
            if (lastvbl < 0)
                lastvbl = windowRecord->time_at_last_vbl;

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

            // For performing a "virtual bufferswap" we need a swaprequest to be pending,
            // and the flipwhen deadline being reached.
            if (!needWork && (flipRequest->flipwhen - lastvbl < windowRecord->vrrMaxDuration - windowRecord->vrrLatencyCompensation)) {
                // Yes: Time to update the backbuffers with our finalizedFBOs and do
                // properly scheduled/timestamped bufferswaps:

                // Copy our virtual backbuffer finalizedFBO[0] fbo to true backbuffer:
                PsychPipelineExecuteHook(windowRecord, kPsychIdentityBlit, NULL, NULL, TRUE, FALSE,
                                         &(windowRecord->fboTable[windowRecord->finalizedFBO[0]]), 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 - windowRecord->vrrLatencyCompensation,
                                                                    &(flipRequest->beamPosAtFlip), &(flipRequest->miss_estimate),
                                                                    &(flipRequest->time_at_flipend), &(flipRequest->time_at_onset));

                // 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();

                // 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 finalizedFBO:
                PsychUnlockMutex(&(flipRequest->performFlipLock));

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

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

            // Ok, either swap deadline for flip not within reach of current VRR
            // refresh cycle, or nothing to do from client side. Idle swap handling...
            if (!needWork) {
                // Have something to swap, but not within reach of current refresh cycle.
                // Perform immediate idle flip to move us closer to target time flipwhen,
                // while keeping lfc or vblank timeout handling from triggering:
                lastvbl = tnow;
            }
            else {
                // We are idle, no new bufferswap pending. Set a reasonable idle-flip
                // to keep lfc from triggering:

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

            // Time for a swap request?
            if (tnow >= lastvbl) {
                // Copy current frontbuffer to backbuffer for an idle "no-op" swap:
                glReadBuffer(GL_FRONT);
                glRasterPos2i(0, (int) PsychGetHeightFromRect(windowRecord->rect));
                glCopyPixels(0, 0, (int) PsychGetWidthFromRect(windowRecord->rect), (int) PsychGetHeightFromRect(windowRecord->rect), GL_COLOR);

                // Trigger a doublebuffer swap in sync with vblank:
                PsychOSFlipWindowBuffers(windowRecord);

                // Protect against multi-threading trouble if needed:
                PsychLockedTouchFramebufferIfNeeded(windowRecord);

                if (PsychPrefStateGet_Verbosity() > 10) {
                    printf("PTB-DEBUG: VRR 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.
                    PsychWaitPixelSyncToken(windowRecord, FALSE);

                    // Try to get good predicted scanout start timestamp:
                    windowRecord->time_at_last_vbl = PsychGetVblankTimestamps(windowRecord, NULL);

                    // Fallback to the basics if that fails:
                    if (windowRecord->time_at_last_vbl < 0)
                        PsychGetAdjustedPrecisionTimerSeconds(&(windowRecord->time_at_last_vbl));
                }
            } else {
                // Nope. Need to sleep a bit here to kill some time:
                PsychYieldIntervalSeconds(0.001);
            }

            // Next dispatch loop iteration...
        }
        // Exit from VRR scheduling dispatch loop.
    } // End of VRR scheduler code.

    // 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) && (windowRecord->vrrMode != kPsychVRROwnScheduled)) {
        // 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) || (windowRecord->vrrMode == kPsychVRROwnScheduled)))) {
        // Async flip start request, or a sync flip turned into an async flip due to kPsychFrameSequentialStereo or VRR with our own scheduler:
        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)) {
            if (windowRecord->vrrMode == kPsychVRROwnScheduled)
                PsychErrorExitMsg(PsychError_user, "Tried to use VRR mode while Screen('Preference', 'ConserveVRAM') setting kPsychUseOldStyleAsyncFlips is set! Forbidden!");
            else
                PsychErrorExitMsg(PsychError_user, "Tried to use frame-sequential stereo mode while Screen('Preference', 'ConserveVRAM') setting kPsychUseOldStyleAsyncFlips is set! Forbidden!");
        }

        if (windowRecord->specialflags & kPsychDontUseFlipperThread)
            PsychErrorExitMsg(PsychError_user, "Tried to use some functionality that requires use of the background flipper thread, but this is forbidden for this window due to specialFlags setting kPsychDontUseFlipperThread!");

        // PsychPreflip operations are not thread-safe due to possible callbacks into runtime 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:
        if ((windowRecord->stereomode == kPsychFrameSequentialStereo) || (windowRecord->vrrMode == kPsychVRROwnScheduled)) {
            // In frame sequential / VRR scheduling mode we need to glFinish() to make sure our
            // finalizedFBO's are really ready for immediate consumption without
            // blocking the stereo/vrr flipperThread, as that would glitch the left-right
            // eye alternating and break stereo or glitch the VRR submission within time constraints:
            glFinish();
        }
        else {
            // For a regular async flip, a glFlush is good enough - it doesn't matter
            // if we block here, or if the flipperThread will eventually block on pending
            // rendering. However not blocking here might help some high-perf rendering to
            // get some more parallelism between gpu rendering and cpu processing in the
            // interpreter thread:
            glFlush();
        }

        // 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.
        //
        // It is also needed with EGL display backend, so we don't keep the
        // binding to the windows EGL surface during startup of flipperThread or
        // any execution of async flip. The thread must bind the surface exclusively
        // for it to work, at least while executing the async flip. frame-sequential
        // stereo mode handles it differently, see SCREENOpenWindow.c for explanation.
        //
        // Note: PsychPreflipOperations() has already made sure the drawing target is
        // backed up and warm-reset properly, so we can do a NULL cold reset here safely:
        PsychSetDrawingTarget(NULL);
        PsychOSUnsetGLContext(windowRecord);

        // 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 PsychFlipWindowBuffersIndirect(): Could not create performFlipLock mutex lock [%s].\n", strerror(rc));
                PsychErrorExitMsg(PsychError_system, "Insufficient system ressources for mutex creation as part of flip threading setup!");
            }

            if ((rc=PsychInitCondition(&(flipRequest->flipperGoGoGo), NULL))) {
                printf("PTB-ERROR: In PsychFlipWindowBuffersIndirect(): Could not create flipperGoGoGo condition variable [%s].\n", strerror(rc));
                PsychErrorExitMsg(PsychError_system, "Insufficient system ressources for condition variable creation as part of flip threading setup!");
            }

            // Set initial thread state to "inactive, not initialized at all":
            flipRequest->flipperState = 0;

            // Setup for our own framesequential stereo implementation:
            if (windowRecord->stereomode == kPsychFrameSequentialStereo) {
                // Increment count of onscreen windows with our own threaded framesequential stereo mode active:
                frameSeqStereoActive++;

                // Perform setup of shutter goggle driver if needed:
                PsychSetupShutterGoggles(windowRecord, TRUE);
            }

            // Setup for our own VRR scheduler implementation:
            if (windowRecord->vrrMode == kPsychVRROwnScheduled) {
                vrrSchedulersActive++;

                if (PsychPrefStateGet_Verbosity() > 2)
                    printf("PTB-INFO: Switching stimulus onset scheduling to use of our own VRR scheduler.\n");
            }

            // Create and startup thread:
            if ((rc=PsychCreateThread(&(flipRequest->flipperThread), NULL, PsychFlipperThreadMain, (void*) windowRecord))) {
                printf("PTB-ERROR: In PsychFlipWindowBuffersIndirect(): Could not create flipper  [%s].\n", strerror(rc));
                PsychErrorExitMsg(PsychError_system, "Insufficient system ressources for thread creation as part of flip threading 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 PsychFlipWindowBuffersIndirect(): First mutex_lock in init failed  [%s].\n", strerror(rc));
                    PsychErrorExitMsg(PsychError_system, "Internal error or deadlock avoided as part of flip threading 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 PsychFlipWindowBuffersIndirect(): First mutex_unlock in init failed  [%s].\n", strerror(rc));
                    PsychErrorExitMsg(PsychError_system, "Internal error or deadlock avoided as part of flip threading 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);
            }

            // On our VRR scheduler, give the VRR machinery some time to stabilize:
            if (windowRecord->vrrMode == kPsychVRROwnScheduled)
                PsychYieldIntervalSeconds(2);

            // 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:

        // 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 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 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?!?");
        }

        // Scheduling regular async-flip as opposed to frame-seq stereo / vrr flip,
        // and EGL windowing backend active? If so we must prevent binding of
        // our masterthread contexts to the EGL backing surface of the window
        // while our async flipper thread has the surface bound:
        if ((windowRecord->stereomode != kPsychFrameSequentialStereo) && (windowRecord->vrrMode != kPsychVRROwnScheduled) && (windowRecord->specialflags & kPsychIsEGLWindow)) {
            // Yes: Veto all EGL surface binds for this windowRecords regular contexts:
            windowRecord->specialflags |= kPsychSurfacelessContexts;
        }

        // 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 or VRR scheduler 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 thread:
            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 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 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) || (windowRecord->vrrMode == kPsychVRROwnScheduled)) {
            // Finalize frame-seq stereo flip or VRR flip: run post-flip ops:
            flipRequest->asyncstate = 0;
            PsychPostFlipOperations(windowRecord, flipRequest->dont_clear);
            flipRequest->asyncstate = 2;
        }
        else {
            // Finalize regular async-flip.

            // EGL-backed windows need special treatment:
            if (windowRecord->specialflags & kPsychIsEGLWindow) {
                // The async thread has unbound its context. We unbind our context
                // to force a rebind. Why? Because if one of our contexts was bound
                // while the async flip was pending, it will have been bound without
                // attachment to the EGL framebuffer surface - this is needed for
                // multi-threaded flips to work on EGL. Now before we reenter regular
                // usercode driven rendering, we must rebind the context with attachment
                // to the EGL backing surface. Unbinding will automatically trigger this:
                PsychSetDrawingTarget((PsychWindowRecordType*) 0x1);
                PsychOSUnsetGLContext(windowRecord);

                // Remove our veto to all EGL surface binds for this windowRecords regular contexts:
                windowRecord->specialflags &= ~kPsychSurfacelessContexts;
            }
        }

        // 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;
    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 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_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) && (vbltimestampmode>=0) && (vbltimestampmode<=2)) ||
        ((vbltimestampmode == 4) && (!(windowRecord->specialflags & kPsychOpenMLDefective) || (windowRecord->VBL_Endline!=-1)))) {
        // If beamposition queries work, or OpenML timestamping is supported and working, we use a tight value:
        slackfactor = 1.05;
    }
    else {
        // If beam position queries don't work, or are disabled, we use a "slacky" value:
        slackfactor = 1.2;
    }

    // Retrieve display id and screen size spec that is needed later...
    PsychGetCGDisplayIDFromScreenNumber(&displayID, windowRecord->screenNumber);
    screenheight=(int) PsychGetHeightFromRect(windowRecord->rect);
    vbl_startline = windowRecord->VBL_Startline;

    // Enable this windowRecords framebuffer as current drawingtarget: [No op on flipperthread!]
    PsychSetDrawingTarget(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);
    }

    // Skip actual stimulus presentation to onscreen window this time?
    if (windowRecord->specialflags & kPsychSkipSwapForFlipOnce) {
        // Yes. So not wait-for-swapcompletion, timestamping, messing with vsync either:
        sync_to_vbl = false;    // No wait-for-swapcompletion, timestamping, checks.
        vbl_synclevel = 1;      // No touching of vsync settings - leave vsync "on".
        multiflip = 0;          // No multiflip.
    }
    else {
        // Presentation to onscreen window enabled. Enforce non-vsynced swap?
        if (windowRecord->specialflags & kPsychSkipVsyncForFlipOnce)
            vbl_synclevel = 2;

        // 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;

        // One-time disable of wait for swapcompletion + timestamping + timing checks requested?
        if (windowRecord->specialflags & kPsychSkipTimestampingForFlipOnce)
            sync_to_vbl = false;

        if ((vbl_synclevel == 2) || (windowRecord->specialflags & kPsychSkipSecondaryVsyncForFlip)) {
            // We are requested to flip immediately, instead of syncing to VBL. Disable VBL-Sync.
            if (vbl_synclevel == 2)
                PsychOSSetVBLSyncLevel(windowRecord, 0);

            // Disable also for a slave window, if any. Unsupported for async flips.
            if (windowRecord->slaveWindow && (windowRecord->slaveWindow->vSynced) && PsychIsMasterThread())
                PsychOSSetVBLSyncLevel(windowRecord->slaveWindow, 0);
        }

        if (vbl_synclevel != 2) {
            // 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);

    // Reset color write mask to "all enabled"
    glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);

    // Reset viewport, projection etc. to full window backbuffer:
    PsychSetupView(windowRecord, TRUE);

    // Part 1 of workaround- /checkcode for syncing to vertical retrace:
    if (vblsyncworkaround) {
        glDrawBuffer(GL_BACK);
        glRasterPos2f(0, (GLfloat) 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) && !(windowRecord->specialflags & kPsychSkipSwapForFlipOnce)) {
        // 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) && !(windowRecord->specialflags & kPsychSkipWaitForFlipOnce))
        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;

    // Request flip at exactly tWhen if fine-grained onset scheduling is requested for this window,
    // subject to the required hardware + OS support:
    if (PsychVRRActive(windowRecord))
        targetSwapFlags |= 4;

    // 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;
    }

    // Must not invalidate if our custom Prime timestamping is used:
    if (windowRecord->hybridGraphics != 5) {
        // 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;

    // Clever swap scheduling is incompatible with the users desire to a) not swap at all, and b) to not perform swap scheduling
    // inside Screen() at all, aka kPsychSkipWaitForFlipOnce.
    if (!(windowRecord->specialflags & kPsychSkipSwapForFlipOnce) && !(windowRecord->specialflags & kPsychSkipWaitForFlipOnce)) {
        // 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");
                        }
                    }
                }
            }
        }
    }
    else {
        osspecific_asyncflip_scheduled = FALSE;
    }

    // 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;

        if (!(windowRecord->specialflags & kPsychSkipSwapForFlipOnce)) {
            // Adjust target time for potential OS-specific compositor delay:
            flipwhen = PsychOSAdjustForCompositorDelay(windowRecord, flipwhen, FALSE);
        }

        // 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
            // system with OS specific swapbuffers scheduling support, or if OS specific
            // scheduling failed, or a special condition requires us to wait anyway:
            if ((!osspecific_asyncflip_scheduled || must_wait) && !(windowRecord->specialflags & kPsychSkipWaitForFlipOnce)) {
                // 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);

        // Make sure we don't go into the past with tshouldflip if time_at_last_vbl is
        // in the future, e.g., because it is == stimulus onset, not start of vblank:
        if (tshouldflip < windowRecord->time_at_last_vbl)
            tshouldflip = windowRecord->time_at_last_vbl;
    }

    // Calculate final deadline for deadline-miss detection:
    tshouldflip = tshouldflip + slackfactor * currentflipestimate;

    // In VRR mode, if a specific target time flipwhen is given and > 1 minimum
    // frame duration away, then set that as tshouldflip + 1 msec slack:
    if (PsychVRRActive(windowRecord) && (flipwhen > 0) && (flipwhen > tshouldflip))
        tshouldflip = flipwhen + 0.001;

    if (!(windowRecord->specialflags & kPsychSkipSwapForFlipOnce)) {
        // 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 == 0) || (((preflip_vblcount + 1) % 2) == (psych_uint64) windowRecord->targetFlipFieldType) ||
                                        (osspecific_asyncflip_scheduled && !must_wait) || (windowRecord->specialflags & kPsychSkipWaitForFlipOnce) ||
                                        (windowRecord->vrrMode == kPsychVRROwnScheduled);
            // If in wrong video cycle, we simply sleep a millisecond, then retry...
            if (!flipcondition_satisfied) PsychWaitIntervalSeconds(0.001);
        } while (!flipcondition_satisfied);

        // 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);

    // 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);

    // Only execute swap sequence + synchronized gamma table updates if no skip requested:
    if (!(windowRecord->specialflags & kPsychSkipSwapForFlipOnce)) {
        // 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. This wait must not be used
        // for Prime-Synced outputSrc -> outputSink setups, as that would add 1 frame extra lag. by preventing
        // us from subitting a swaprequest 1 frame ahead to compensate for the 1 frame lag of Prime sync.
        if ((windowRecord->time_at_last_vbl > 0) && (vbl_synclevel!=2) && (!osspecific_asyncflip_scheduled) && (windowRecord->hybridGraphics < 2) &&
            !(windowRecord->specialflags & kPsychSkipWaitForFlipOnce) && !(PsychPrefStateGet_ConserveVRAM() & kPsychSkipOutOfVblankWait) &&
            ((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);
        }

        // 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) {
                // Some drivers need the context of the to-be-swapped window, e.g., NVidia binary blob on Linux:
                PsychSetGLContext(windowRecord->slaveWindow);
                PsychOSFlipWindowBuffers(windowRecord->slaveWindow);

                // Protect against multi-threading trouble if needed:
                PsychLockedTouchFramebufferIfNeeded(windowRecord->slaveWindow);

                PsychSetGLContext(windowRecord);
            }

            // 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)) {
                        // Some drivers need the context of the to-be-swapped window, e.g., NVidia binary blob on Linux:
                        PsychSetGLContext(windowRecordArray[i]);
                        PsychOSFlipWindowBuffers(windowRecordArray[i]);

                        // Protect against multi-threading trouble if needed:
                        PsychLockedTouchFramebufferIfNeeded(windowRecordArray[i]);
                    }
                }
                PsychSetGLContext(windowRecord);
            }
        }

        // Take post-swap request line:
        line_post_swaprequest = (int) PsychGetDisplayBeamPosition(displayID, windowRecord->screenNumber);
    }   // Following else-if reached if an external display backend is in use on the primary onscreen window.
    else if (windowRecord->slaveWindow && (windowRecord->imagingMode & kPsychNeedDualWindowOutput)) {
        // This onscreen window uses an external display backend, e.g., Vulkan, for stimulus presentation,
        // but also has a secondary slave window for visual feedback, e.g., to the experimenter, attached
        // and enabled for visual stimulus mirroring/cloning to the OpenGL slave window. At this point, the
        // slave windows OpenGL backbuffer already contains the to be shown mirror/clone image, and the
        // main stimulus display will have started stimulus presentation, so all that's left to do is to
        // perform the OpenGL bufferswap to also show the same stimulus on the feedback slave window.
        // Do the minimum amount of operations to trigger the OpenGL bufferswap + workarounds sometimes needed:
        PsychSetGLContext(windowRecord->slaveWindow);

        // Disable vsync for swaps on slaveWindow if requested by specialflags, unless vsync already disabled:
        if ((windowRecord->specialflags & kPsychSkipSecondaryVsyncForFlip) && (windowRecord->slaveWindow->vSynced) && PsychIsMasterThread())
            PsychOSSetVBLSyncLevel(windowRecord->slaveWindow, 0);

        PsychOSFlipWindowBuffers(windowRecord->slaveWindow);
        PsychLockedTouchFramebufferIfNeeded(windowRecord->slaveWindow);
        PsychSetGLContext(windowRecord);
    }

    // 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;

    // Protect against multi-threading trouble if needed:
    if (!(windowRecord->specialflags & kPsychSkipSwapForFlipOnce))
        PsychLockedTouchFramebufferIfNeeded(windowRecord);

    // 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: If this message shows up frequently during sessions, instead of only very sporadically, then\n");
                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");
                printf("PTB-WARNING: Read the Linux specific section of 'help SyncTrouble' for some common causes and fixes for this problem.\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...
                PsychWaitPixelSyncToken(windowRecord, FALSE);
            }

            #if PSYCH_SYSTEM == PSYCH_LINUX
                #ifndef GLX_BACK_BUFFER_AGE_EXT
                #define GLX_BACK_BUFFER_AGE_EXT 0x20F4
                #endif

                #ifndef PTB_USE_WAFFLE
                    // Extra-paranoia for fullscreen windows on Linux, just because we can:
                    if ((windowRecord->specialflags & kPsychIsFullscreenWindow) && (PsychPrefStateGet_SkipSyncTests() < 2)) {
                        // Linux: GLX_EXT_buffer_age supported? If so, then we can query the age in frames of our current post-swap backbuffer.
                        // A value of 2 means double-buffering is used by the gfx-driver, a value of 3 is triple-buffering, zero is single-buffering, n is n-nbuffering, ...
                        // Our currently chosen classic timestamping path absolutely requires double-buffering, so any value other than 2 means big trouble for timing:
                        // However, we also accept a value of zero, as this can legally happen if the driver employs some special optimizations -- zero is non-diagnostic,
                        // neither a sign of success, nor a sign of failure, so we just ignore it to avoid meaningless warning clutter in the rare cases where it turns up.
                        // One example of such an optimization are first time use of AUX buffers on a NVidia gpu with NVidia proprietary driver. Some funny lazy allocation going on...
                        // The value of 1 is observed when a desktop compositor (3d OpenGL, or 2d X-RENDER based) is active and redirecting our window to a composition surface
                        // by a framebuffer copy from backbuffer -> compositor buffer -- copy leads to constant buffer_age of 1.
                        //
                        // There also seem to be some false positive reports by some versions of the NVidia proprietary graphics driver on some gpus and setups.
                        // To avoid flooding the screen with meaningless warnings we turn this into a one-time warning, so the amount of chatter is tolerable on
                        // setups with a misreporting driver. We also tone down the text of the warning message a bit, as this is more an indicator that something
                        // could be wrong than a clear proof that something is wrong.
                        //
                        unsigned int buffer_age = 2; // Init to 2 to give benefit of doubt in case query below fails.
                        if (windowRecord->gfxcaps & kPsychGfxCapSupportsBufferAge) {
                            PsychLockDisplay();
                            glXQueryDrawable(windowRecord->targetSpecific.deviceContext, windowRecord->targetSpecific.windowHandle, GLX_BACK_BUFFER_AGE_EXT, &buffer_age);
                            PsychUnlockDisplay();

                            if ((buffer_age > 0) && (buffer_age != 2) && (verbosity > 1) && !(windowRecord->specialflags & kPsychBufferAgeWarningDone)) {
                                // One time warning only:
                                windowRecord->specialflags |= kPsychBufferAgeWarningDone;

                                printf("PTB-WARNING: OpenGL driver seems to use %i-buffering instead of the required double-buffering for Screen('Flip').\n", buffer_age);
                                printf("PTB-WARNING: This could be a false positive in which case there is no reason to worry, but it could also indicate some problem\n");
                                printf("PTB-WARNING: with visual stimulus onset timing on your setup, which would impair visual timing and timestamps of Screen('Flip').\n");
                                printf("PTB-WARNING: If timing matters, I'd recommend performing further diagnosis and potential troubleshooting (read 'help SyncTrouble').\n");
                                if (buffer_age == 1) printf("PTB-WARNING: One potential cause for this is that some kind of desktop compositor is active and interfering.\n");
                                if (buffer_age == 3) printf("PTB-WARNING: One potential cause for this is that TripleBuffering is enabled somewhere in the driver or xorg.conf settings.\n");
                                if (buffer_age > 3) printf("PTB-WARNING: One potential cause for this is that %i-Buffering is enabled somewhere in the driver or xorg.conf settings.\n", buffer_age);
                                if ((windowRecord->hybridGraphics == 2) && strstr((char*) glGetString(GL_VENDOR), "NVIDIA")) {
                                    printf("PTB-WARNING: The most likely cause on this NVidia Optimus setup is the use of the proprietary driver, which will massively impair proper timing!\n");
                                    printf("PTB-WARNING: Use the open-source nouveau graphics driver, or install the enhanced modesetting ddx, or do not use the NVidia gpu at all, if you\n");
                                    printf("PTB-WARNING: care about research grade timing. See 'help HybridGraphics' for more info.\n");
                                }
                                printf("PTB-WARNING: This is a one-time warning which will not repeat, even if the problem persists during this session.\n\n");
                            }

                            if (verbosity > 9) printf("PTB-DEBUG: GLX_BACK_BUFFER_AGE_EXT == %i after swap completion.\n", buffer_age);
                        }
                    }
                #endif

                // Additional paranoia check at high debug levels where performance doesn't matter anymore.
                // Check if compositor is active. This just to test functionality, we won't enable this check
                // for normal operation yet, as i suspect it involves a potentially expensive time-consuming
                // roundtrip to the x-server, which may not be acceptable for high-framerate stimulus presentation.
                //
                // In its current form this is only useful for development and testing, as the method implemented
                // only detects true compositor activity reliably with KDE/KWin. On other compositors it always
                // reports composition on, even if the compositor is just hanging around idly in standby mode. We
                // can't afford that many false alerts.
                if ((verbosity > 10) && PsychOSIsDWMEnabled(windowRecord->screenNumber)) printf("PTB-DEBUG:Linux:Screen('Flip'): After swapcomplete compositor reported active.\n");
            #endif

            // Check if we can get a second opinion about use of pageflipping from the GPU itself, assuming standard
            // swap scheduling was used and beampos timestamping works - ergo other GPU low level features like this,
            // as otherwise the results would be misleading:
            if (!osspecific_asyncflip_scheduled && (PSYCH_SYSTEM == PSYCH_LINUX || PSYCH_SYSTEM == PSYCH_OSX) &&
                (windowRecord->VBL_Endline != -1)) {
                int pflip_status = PsychIsGPUPageflipUsed(windowRecord);
                if (verbosity > 10) printf("PTB-DEBUG: PsychFlipWindowBuffers(): After swapcomplete PsychIsGPUPageflipUsed() = %i.\n", pflip_status);

                // Only really relevant for fullscreen windows if pageflipping is used and under our control:
                if ((windowRecord->specialflags & kPsychIsFullscreenWindow) && (PsychPrefStateGet_SkipSyncTests() < 2)) {
                    // -1 = Don't know / non-diagnostic. 2 = Pageflip completed - Good. Everything else
                    // means trouble - either copyswap or desktop compositing:
                    if ((pflip_status != -1) && (pflip_status != 2) && (verbosity > 1)) {
                        printf("PTB-WARNING: GPU reports that pageflipping isn't used - or under our control - for Screen('Flip')! [pflip_status = %i]\n", pflip_status);
                        printf("PTB-WARNING: Returned Screen('Flip') timestamps might be wrong! Please fix this now (read 'help SyncTrouble').\n");
                        if (pflip_status == 1) printf("PTB-WARNING: The most likely cause for this is that some kind of desktop compositor is active and interfering.\n");
                    }
                }
            }
        }

        // 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)) {
                    // Some drivers need the context of the to-be-swapped window, e.g., NVidia binary blob on Linux:
                    PsychSetGLContext(windowRecordArray[i]);
                    PsychOSFlipWindowBuffers(windowRecordArray[i]);
                    PsychLockedTouchFramebufferIfNeeded(windowRecordArray[i]);
                }
            }
            // Restore to our context:
            PsychSetGLContext(windowRecord);
        }

        // 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;

        // 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)) {

            // Some systems 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)) {
                // Shoddy OSX with its deficient Core video display link implementation in use?
                // If so we wait another extra bit of time to give it a chance to catch up to reality:
                // CoreVideo display link callbacks can be tremendeously delayed wrt. actual VBlank time, so
                // querying vblank time and count too close to a vblank can easily provide us with stale
                // results. We take longer breaks between query retries to increase the chance of a callback
                // delivering updated results to us. Best we can do, after all other approaches turned out to
                // be flawed or fragile as well and Apple seems to be utterly disinterested in fixing their mess.
                if (PSYCH_SYSTEM == PSYCH_OSX) PsychWaitIntervalSeconds(0.00025);
                PsychWaitIntervalSeconds(0.00025);
                postflip_vbltimestamp = PsychOSGetVBLTimeAndCount(windowRecord, &postflip_vblcount);
                vbltimestampquery_retrycount++;
            }
        }

        // 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 > 0) {
                        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 > 0) {
                        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, i'll switch to a likely equally broken mechanism based on CoreVideo timestamps. But hope dies last...\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 = (int) 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;

            // time_at_vbl is not computable on a VRR setup, so override it with the next best surrogate, the time_at_onset:
            if (PsychVRRActive(windowRecord)) {
                time_at_vbl = *time_at_onset;
            }
        }
        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 && !(windowRecord->specialflags & kPsychSkipWaitForFlipOnce)) {
            // 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)) &&
            !(windowRecord->specialflags & kPsychSkipWaitForFlipOnce)) {

            // Ohoh! Broken timing. Disable beamposition timestamping for future operations, warn user.
            if (verbosity > 0) {
                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 > 0) {
                    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. Equally likely and serious is lack of sync between swap completion and PTB. 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, or of swap completion signalling to swap completion.\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: On Linux, this method is slightly less accurate and higher overhead but should be similarly robust,\n");
                        printf("PTB-ERROR: On macOS, this method is usually also badly broken and unreliable, but hope dies last...\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, or of PTB to swap completion, is not working properly.\n");
                        printf("PTB-ERROR: Please run the script PerceptualVBLSyncTest to check this. With any of these problems,\n");
                        printf("PTB-ERROR: all stimulus timing is futile. Also run OSXCompositorIdiocyTest on macOS. \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 > 0) {
                        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 unlikely culprit would be\n");
                        printf("PTB-ERROR: a bug in 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, or of swap completion to completion signalling 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: is futile. Also run OSXCompositorIdiocyTest on macOS. 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 > 0) {
                        printf("PTB-ERROR: This error can be due to either of the following causes:\n");
                        printf("PTB-ERROR: Very unlikely: 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.\n\n");
                        printf("PTB-ERROR: The most 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, or swap completion signalling to PTB is broken.\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: is futile. Also run OSXCompositorIdiocyTest on macOS. 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) && !(windowRecord->specialflags & kPsychSkipWaitForFlipOnce) &&
            ((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 > 0) {
                        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:\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: The by far most likely cause would be that Synchronization of stimulus onset (buffer swap) to the\n");
                        printf("PTB-ERROR: vertical blank interval VBL or of swap completion to completion signalling 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: is futile. Also run OSXCompositorIdiocyTest on macOS. 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 > 0) {
                    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;
            }
        }

        // Special case NVidia Optimus laptop with proprietary graphics driver. Only DRI PRIME slave output operation
        // available, not the precise and reliable gpu renderoffload. Timestamping is highly fragile, unreliable and
        // inaccurate, but one thing we do know, at least in the initial XOrg 1.19 implementation, is that the timestamp
        // will be at least one video refresh too early. Therefore, if we have such a type 2 setup, we add 1 refresh duration
        // to the so far computed timestamps.
        // Note that these assignments get overriden directly below in VBLTimestampingMode 4 if we are running under the
        // custom modified XOrg 1.19 modesetting ddx as a fullscreen window and therefore received a high precision timestamp
        // in tSwapComplete from PsychOSGetSwapCompletionTimestamp(). This snippet only gets used in the fallback on unmodified
        // XOrg 1.19 to keep us limping along with bad/inaccurate/unreliable timestamps that are at least somehow self-consistent
        // in some sense:
        if ((PSYCH_SYSTEM == PSYCH_LINUX) && (windowRecord->specialflags & kPsychIsX11Window) && ((windowRecord->hybridGraphics == 2) || (windowRecord->hybridGraphics == 3))) {
            time_at_vbl += windowRecord->VideoRefreshInterval;
            *time_at_onset += windowRecord->VideoRefreshInterval;
        }

        // 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 ((PsychPrefStateGet_SkipSyncTests() < 2) && !(windowRecord->specialflags & kPsychSkipWaitForFlipOnce) &&
                ((osspecific_asyncflip_scheduled && (tSwapComplete < tprescheduleswap - 0.001)) ||
                (!osspecific_asyncflip_scheduled && (tSwapComplete < time_at_swaprequest - 0.001)))) {
                if (verbosity > 0) {
                    printf("PTB-ERROR: OpenML timestamping reports that flip completed before it was scheduled [Scheduled no earlier than %f secs, completed at %f secs]!\n",
                           (osspecific_asyncflip_scheduled) ? tprescheduleswap : time_at_swaprequest, tSwapComplete);
                    printf("PTB-ERROR: This could mean that sync of bufferswaps to vertical retrace is broken or some other driver bug! Check your system setup!\n");
                }

                // Fall back to beamposition timestamping or raw timestamping, unless
                // we are running under DRI3/Present, in which case none of the fallback methods
                // will be useable and we just have to hope this was a transient failure:
                if (!(windowRecord->specialflags & kPsychIsDRI3Window)) {
                    if (verbosity > 0) printf("PTB-ERROR: Switching to alternative timestamping method.\n");
                    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;

                // Also check for flips that completed before their target time, which
                // would indicate a failure in swap scheduling. Usual roundoff fudge applies:
                if ((PsychPrefStateGet_SkipSyncTests() < 2) && !(windowRecord->specialflags & kPsychSkipWaitForFlipOnce) && (targetWhen > 0) && (tSwapComplete < targetWhen - 0.001)) {
                    if (verbosity > 0) {
                        printf("PTB-ERROR: OpenML timestamping reports that flip completed before its requested target time [Target no earlier than %f secs, completed at %f secs]!\n",
                               targetWhen, tSwapComplete);
                        printf("PTB-ERROR: Something is wrong with swap scheduling, a misconfiguration or potential graphics driver bug! Check your system setup!\n");
                        if (windowRecord->gfxcaps & kPsychGfxCapSupportsOpenML) printf("PTB-ERROR: Switching to alternative fallback scheduling method.\n");
                    }

                    // Disable OS native swap scheduling. We will use the classic wait + glXSwapBuffers path,
                    // but still keep OS native timestamping functional:
                    windowRecord->gfxcaps &= ~kPsychGfxCapSupportsOpenML;
                }
            }
        }

        // 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 OS-Builtin swap timestamp as well, or stimulus onset time from beampos
        // timestamping, if OS-Builtin timestamp not available:
        windowRecord->osbuiltin_swaptime = (tSwapComplete > 0) ? tSwapComplete : *time_at_onset;

        // Store beamposition at flip completion as well:
        windowRecord->beamposition_at_flip = *beamPosAtFlip;
    }
    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;
        *miss_estimate = 0;
        *beamPosAtFlip = -1;  // Ditto for beam position...

        // Was internal timestamping suppressed?
        if (windowRecord->specialflags & kPsychSkipTimestampingForFlipOnce) {
            // Latch potential values injected via Screen Hookfunction 'SetOneshotFlipResults':
            time_at_vbl = windowRecord->time_at_last_vbl;
            *time_at_onset = windowRecord->osbuiltin_swaptime;
            *miss_estimate = windowRecord->postflip_vbltimestamp;
            *beamPosAtFlip = windowRecord->beamposition_at_flip;
        }
        else {
            // Invalidate timestamps of last vbl:
            windowRecord->time_at_last_vbl = 0;
            windowRecord->osbuiltin_swaptime = 0;
            windowRecord->beamposition_at_flip = -1;
        }

        // These are always reset, as not latchable by usercode:
        windowRecord->postflip_vbltimestamp = -1;
        windowRecord->rawtime_at_swapcompletion = 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;
    }

    // Clear the one-shot flipFlags, so they won't automatically apply to the next flip again, unless explicitely
    // asked to not to clear the one-shot flags: XXX Better do it in SCREENFlip than here?
    if (!(windowRecord->specialflags & kPsychDontAutoResetOneshotFlags))
        windowRecord->specialflags &= ~(kPsychSkipVsyncForFlipOnce | kPsychSkipTimestampingForFlipOnce | kPsychSkipSwapForFlipOnce | kPsychSkipWaitForFlipOnce);

    // 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, psych_bool* did_pageflip)
{
    int i = 0, 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->specialflags & kPsychOpenMLDefective) || (windowRecord->hybridGraphics == 5)));
    int pflip_count = 0;

    if (did_pageflip) *did_pageflip = FALSE;

    // 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);
        told = -1;

        // Need to redraw our splash image, as at least under Linux with the FOSS stack
        // in DRI3/Present mode, OpenGL is n-buffered with n dynamic but n > 2, ie.,
        // our old double-buffering assumption no longer holds:
        if (PSYCH_SYSTEM == PSYCH_LINUX)
            PsychDrawSplash(windowRecord, 0);

        // Schedule a buffer-swap on next VBL:
        PsychOSFlipWindowBuffers(windowRecord);
        PsychOSGetSwapCompletionTimestamp(windowRecord, 0, &tnew);

        // Protect against multi-threading trouble if needed:
        PsychLockedTouchFramebufferIfNeeded(windowRecord);

        if (PSYCH_SYSTEM == PSYCH_OSX) {
            // Give gfx-system a second to settle: This stupid hack to
            // counteract a new type of stupid bug introduced in OSX 10.11
            // El Capitan: Sync failure at each first run after application
            // startup. Thanks Apple!
            PsychYieldIntervalSeconds(1);
        }

        PsychGetAdjustedPrecisionTimerSeconds(&tstart);

        // Take samples during consecutive refresh intervals:
        // We measure until either:
        // - A maximum measurement 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++) {
            // Draw splash image again, to handle Linux DRI3/Present and similar setups:
            if (PSYCH_SYSTEM == PSYCH_LINUX)
                PsychDrawSplash(windowRecord, 0);

            // Schedule a buffer-swap on next VBL:
            PsychOSFlipWindowBuffers(windowRecord);

            // Protect against multi-threading trouble if needed:
            PsychLockedTouchFramebufferIfNeeded(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...
                PsychWaitPixelSyncToken(windowRecord, FALSE);

                // At this point, start of VBL has happened and we can continue execution...
                // We take our timestamp here:
                PsychGetAdjustedPrecisionTimerSeconds(&tnew);

                // Ask GPU if a Pageflip was used and under our control:
                if (2 == PsychIsGPUPageflipUsed(windowRecord)) pflip_count++;
            }

            // 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(),
                // unless we are running on a ultra-high video refresh rate display with > 250 Hz refresh,
                // then all bets are off and we skip this check at the moment.
                // 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) && (intervalHint > 0.004 || intervalHint <= 0)) ? fallthroughcount+1 : 0;

                // Shorten our measured time sample by the delay introduced by a potentially running
                // and interfering compositor, so we discount that confound. Needed, e.g., for Windows DWM
                // and Wayland + Weston, at least before Weston 1.8.:
                tdur = PsychOSAdjustForCompositorDelay(windowRecord, tdur, TRUE);

                // 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.
                // 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, some hardware doubles the flip-interval: It only flips every 2nd
                // video refresh, so a doubled flip interval is a legal valid result.
                if (((intervalHint<=0) || (intervalHint>0 &&
                    (((tdur > 0.8 * intervalHint) && (tdur < 1.2 * intervalHint)) ||
                    (((windowRecord->stereomode==kPsychOpenGLStereo) || (windowRecord->multiSample > 0) || windowRecord->hybridGraphics) &&
                     (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;
                }

                // Store current sample in samplebuffer if requested:
                if (samples && i < maxlogsamples) samples[i] = tdur;
            }

            // Update reference timestamp:
            told = tnew;

            // 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 1 msecs after each sample,
            // so the VBL period will be over before we manage to try to swap again.
            //
            // Pause for 1 msec after a sample was taken. This to guarantee we're out
            // of the VBL period of the successfull swap. Skip for ultra-high refresh rate displays:
            if (intervalHint > 0.004 || intervalHint <= 0)
                PsychWaitIntervalSeconds(0.001);
        } // 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) && (PsychPrefStateGet_Verbosity() > 1)) {
            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;
            if (PsychPrefStateGet_Verbosity() > 1) {
                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 = (int) 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]);
            free(samples);
            samples = NULL;

            printf("PTB-DEBUG: End of calibration data for this run...\n\n");
        }

        // Summary of pageflip only makes sense if !useOpenML, so actual accounting was done:
        if (!useOpenML && (PsychIsGPUPageflipUsed(windowRecord) >= 0)) {
            if (PsychPrefStateGet_Verbosity() > 4)
                printf("PTB-DEBUG: %i out of %i samples confirm use of GPU pageflipping for the swap during refresh calibration.\n", pflip_count, i);

            // Good result?
            if (pflip_count >= i - 1) {
                if (PsychPrefStateGet_Verbosity() > 4)
                    printf("PTB-DEBUG: --> Good, one neccessary condition for defined visual timing is satisfied.\n");
            }
            else if ((PsychPrefStateGet_Verbosity() > 1) && (windowRecord->specialflags & kPsychIsFullscreenWindow) && !(windowRecord->specialflags & kPsychExternalDisplayMethod)) {
                // No reliable pageflipping or pageflipping at all on a fullscreen window without use of an external display method.
                // This is pretty much game over for reliable visual timing or timestamping:
                printf("PTB-WARNING: Pageflipping wasn't used %s during refresh calibration [%i of %i].\n",
                       (pflip_count > 0) ? "consistently" : "at all", pflip_count, i);
                printf("PTB-WARNING: Visual presentation timing is broken on your system and all followup tests and workarounds will likely fail.\n");
                if (PSYCH_SYSTEM == PSYCH_OSX) {
                    printf("PTB-WARNING: On this Apple macOS system you probably don't need to even bother asking anybody for help.\n");
                    printf("PTB-WARNING: Just upgrade to Linux if you care about trustworthy visual timing and stimulation.\n\n");
                }
            }
        }
    } // End of IFI measurement code.
    else {
        // No measurements taken...
        *numSamples = 0;
        *stddev = 0;
    }

    if ((i > 0) && (pflip_count > 0) && (pflip_count >= i - 1)) {
        // Pageflip was used consistently during measurement - a good sign:
        if (did_pageflip) *did_pageflip = TRUE;
    }

    // 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;
    GLclampf v=0;
    GLclampf color[4];
    float f = 0;
    float 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;

    glGetFloatv(GL_COLOR_CLEAR_VALUE, (GLfloat*) &color);

    PsychGetAdjustedPrecisionTimerSeconds(&tdeadline);
    tdeadline+=duration;

    // Enable this windowRecords framebuffer as current drawingtarget:
    PsychSetDrawingTarget(windowRecord);

    w = (float) PsychGetWidthFromRect(windowRecord->rect);
    h = (float) PsychGetHeightFromRect(windowRecord->rect);

    // Clear out all potentially 3 buffers so it doesn't look like junk,
    // even if on a triple-buffered graphics system:
    glClearColor(0,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);
    PsychOSFlipWindowBuffers(windowRecord);

    // Protect against multi-threading trouble if needed:
    PsychLockedTouchFramebufferIfNeeded(windowRecord);
    glClear(GL_COLOR_BUFFER_BIT);
    PsychOSFlipWindowBuffers(windowRecord);

    // Protect against multi-threading trouble if needed:
    PsychLockedTouchFramebufferIfNeeded(windowRecord);
    glClear(GL_COLOR_BUFFER_BIT);
    PsychOSFlipWindowBuffers(windowRecord);

    // Protect against multi-threading trouble if needed:
    PsychLockedTouchFramebufferIfNeeded(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 = (float) (0.5 + 0.5 * sin(tcurrent*6.283));

        switch (belltype) {
            case 0: // Info - Make it blue
                glClearColor(0,0,v,1);
                break;
            case 1: // Warning - Make it yellow
                glClearColor(v,v,0,1);
                break;
            case 2: // Error - Make it red
                glClearColor(v,0,0,1);
                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.00f*w, 0.00f*h);
                glVertex2f(2.00f*w, 0.00f*h);
                glVertex2f(2.00f*w, 3.00f*h);
                glVertex2f(0.00f*w, 3.00f*h);
                glColor3f(0,0,v);
                glVertex2f(0.00f*w, 0.00f*h);
                glVertex2f(1.00f*w, 0.00f*h);
                glVertex2f(1.00f*w, 1.00f*h);
                glVertex2f(0.00f*w, 1.00f*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.1f*w, 0.1f*h);
        glVertex2f(0.9f*w, 0.1f*h);
        glVertex2f(0.5f*w, 0.9f*h);
        glColor3f(1,1,0);
        glVertex2f(0.2f*w, 0.2f*h);
        glVertex2f(0.8f*w, 0.2f*h);
        glVertex2f(0.5f*w, 0.8f*h);
        glEnd();

        // Draw a black exclamation mark into triangle:
        glBegin(GL_QUADS);
        glColor3f(0,0,0);
        glVertex2f(0.47f*w, 0.23f*h);
        glVertex2f(0.53f*w, 0.23f*h);
        glVertex2f(0.53f*w, 0.55f*h);
        glVertex2f(0.47f*w, 0.55f*h);
        glVertex2f(0.47f*w, 0.60f*h);
        glVertex2f(0.53f*w, 0.60f*h);
        glVertex2f(0.53f*w, 0.70f*h);
        glVertex2f(0.47f*w, 0.70f*h);
        glEnd();

        // Initiate back-front buffer flip:
        PsychOSFlipWindowBuffers(windowRecord);

        // Protect against multi-threading trouble if needed:
        PsychLockedTouchFramebufferIfNeeded(windowRecord);

        // Our old VBL-Sync trick again... We need sync to VBL to visually check if
        // beamposition is locked to VBL:
        PsychWaitPixelSyncToken(windowRecord, FALSE);

        // Query and visualize scanline immediately after VBL onset, aka return of glFinish();
        scanline = (float) 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;
    GLenum blitscalemode;
    char overridepString1[100];

    // 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;

    // Peform extensive checking for OpenGL errors, unless instructed not to do so:
    if (!(PsychPrefStateGet_ConserveVRAM() & kPsychAvoidCPUGPUSync)) {
        GLenum glerr;

        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 (Preflip-I)!\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 before preflip (Preflip-I): %s.\n", gluErrorString(glerr));
            }
        }

        PsychTestForGLErrors();
    }

    // Enable this windowRecords framebuffer as current drawingtarget:
    PsychSetDrawingTarget(windowRecord);

    // Execute hook chain for ops post-user space drawing (e.g., drawing an overlay over user content):
    PsychPipelineExecuteHook(windowRecord, kPsychUserspaceBufferDrawingFinished, NULL, NULL, FALSE, FALSE, NULL, NULL, NULL, NULL);

    // 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:
    if ((PsychPrefStateGet_ConserveVRAM() & kPsychDisableAUXBuffers) || !PsychIsGLClassic(windowRecord)) {
        auxbuffers = 0;
    }
    else 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.

                // Set proper binding of source and destination FBO for blit, unless we use the texture
                // blitter fallback below, in which case these separte low-level bindings are not needed:
                if (windowRecord->gfxcaps & kPsychGfxCapFBOBlit) {
                    // Supported:
                    glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, windowRecord->fboTable[windowRecord->drawBufferFBO[viewid]]->fboid);
                    glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, windowRecord->fboTable[windowRecord->inputBufferFBO[viewid]]->fboid);
                }
                else {
                    // Only fallback possible. This rules out any multisample resolve blits, and thereby means failure on
                    // multisampled configs, unless multisample textures as colorbuffer attachment are supported and setup:
                    if (((windowRecord->multiSample > 0) && (windowRecord->fboTable[windowRecord->drawBufferFBO[viewid]]->textarget != GL_TEXTURE_2D_MULTISAMPLE)) ||
                        ((windowRecord->multiSample == 0) && !(windowRecord->imagingMode & kPsychNeedGPUPanelFitter))) {
                        PsychErrorExitMsg(PsychError_internal, "Tried to do multisample resolve, or a non-panelfitter op in drawbuffer->inputbuffer stage, but this is unsupported on your gpu! Bug?!?");
                    }
                }

                // Panelfitter requested?
                if (windowRecord->imagingMode & kPsychNeedGPUPanelFitter) {
                    // Need to rescale and/or reposition during src->dest blit to implement panel scaling.

                    // Simultaneous Multisample-resolve during blit requested and supported? If so, use special blitmode to do both in one go.
                    // Otherwise just use bilinear filtering for nice scaling:
                    blitscalemode = ((windowRecord->multiSample > 0) && (windowRecord->gfxcaps & kPsychGfxCapFBOScaledResolveBlit)) ? GL_SCALED_RESOLVE_NICEST_EXT : GL_LINEAR;

                    // Do src- and dst- rectangles match in size? Then we can optimize:
                    if (((windowRecord->panelFitterParams[2] - windowRecord->panelFitterParams[0]) == (windowRecord->panelFitterParams[6] - windowRecord->panelFitterParams[4])) &&
                        ((windowRecord->panelFitterParams[3] - windowRecord->panelFitterParams[1]) == (windowRecord->panelFitterParams[7] - windowRecord->panelFitterParams[5]))) {
                        // Sizes of source and destination rectangles for blit are identical, therefore no scaling required, therefore we
                        // don't need a filter for the blit, just simple nearest-neighbour sampling, ie., a one-to-one blit from one location
                        // to the other, possibly with different (x,y) start and end offsets:
                        blitscalemode = GL_NEAREST;
                    }

                    if (PsychPrefStateGet_Verbosity() > 4) {
                        printf("PTB-DEBUG: Panel-Fitter %s %sblit: [%i %i %i %i] -> [%i %i %i %i], Rotation=%i, RotCenter=[%i, %i]\n",
                                (blitscalemode == GL_NEAREST) ? "unscaled" : "scaled",
                                (windowRecord->multiSample > 0) ? "MultisampleResolveScale" : "Scale",
                                windowRecord->panelFitterParams[0], windowRecord->panelFitterParams[1], windowRecord->panelFitterParams[2], windowRecord->panelFitterParams[3],
                                windowRecord->panelFitterParams[4], windowRecord->panelFitterParams[5], windowRecord->panelFitterParams[6], windowRecord->panelFitterParams[7],
                                windowRecord->panelFitterParams[8], windowRecord->panelFitterParams[9], windowRecord->panelFitterParams[10]);
                    }

                    // This is a scaled blit, but all blit parameters are defined in the panelFitterParams array, which
                    // has to be set up by external code via Screen('PanelFitterProperties'). We can't use framebuffer blit
                    // if the target inputBufferFBO is itself multisampled, and the src and dst regions are of different size,
                    // as that would be a MSAA source --> MSAA target blit, for which rescaling is not supported:
                    if ((windowRecord->gfxcaps & kPsychGfxCapFBOBlit) && (windowRecord->panelFitterParams[8] == 0) &&
                        ((windowRecord->fboTable[windowRecord->inputBufferFBO[viewid]]->multisample == 0) || (blitscalemode == GL_NEAREST))) {
                        // Framebuffer blitting supported, good!
                        glBlitFramebufferEXT(windowRecord->panelFitterParams[0], windowRecord->panelFitterParams[1], windowRecord->panelFitterParams[2],
                                            windowRecord->panelFitterParams[3],
                                            windowRecord->panelFitterParams[4], windowRecord->panelFitterParams[5], windowRecord->panelFitterParams[6],
                                            windowRecord->panelFitterParams[7],
                                            GL_COLOR_BUFFER_BIT, blitscalemode);
                    }
                    else {
                        // Framebuffer blit unsupported or rotation requested. Use our normal texture blitting code as a fallback.
                        // This has two downsides: First, it doesn't allow multisampling resolve, unless multisample textures are supported
                        // and in use for drawBufferFBO's.
                        if ((windowRecord->multiSample > 0) && (windowRecord->fboTable[windowRecord->drawBufferFBO[viewid]]->textarget != GL_TEXTURE_2D_MULTISAMPLE)) {
                            // Ohoh, need multisampling resolve, but no multisample texture bound. Game over!
                            printf("PTB-ERROR: The requested panelfitting operation (most likely display rotation?) is not supported on your system if\n");
                            printf("PTB-ERROR: multisample anti-aliasing is active at the same time. Disable either multisampling, or the panelfitting task.\n");
                            PsychErrorExitMsg(PsychError_user, "Tried to use panelfitter fallback with multisampling enabled, but multisampling unsupported on your gpu!");
                        }

                        // Second, it only allows to blit the original drawBufferFBO at its full size into a potentially scaled and
                        // offset inputBufferFBO destination region, ie., the source region is ignored aka
                        // panelFitterParams[0-3] are ignored. Should still work ok with many panelfitter
                        // modes, e.g., whenever a lower resolution virtual framebuffer is centered in, or
                        // upscaled to a higher resolution real framebuffer:
                        if (blitscalemode == GL_NEAREST) {
                            // Unscaled blit, possibly with offset in destination FBO:
                            sprintf(overridepString1, "Offset:%i:%i:Rotation:%f:RotCenter:%f:%f", windowRecord->panelFitterParams[4], windowRecord->panelFitterParams[5],
                                    (double) windowRecord->panelFitterParams[8],
                                    (double) windowRecord->panelFitterParams[9], (double) windowRecord->panelFitterParams[10]);
                        }
                        else {
                            // Scaled blit with bilinear filtering:
                            sprintf(overridepString1, "Bilinear:Offset:%i:%i:OvrSize:%i:%i:Rotation:%f:RotCenter:%f:%f",
                                    windowRecord->panelFitterParams[4], windowRecord->panelFitterParams[5],
                                    (windowRecord->panelFitterParams[6] - windowRecord->panelFitterParams[4]),
                                    (windowRecord->panelFitterParams[7] - windowRecord->panelFitterParams[5]),
                                    (double) windowRecord->panelFitterParams[8],
                                    (double) windowRecord->panelFitterParams[9], (double) windowRecord->panelFitterParams[10]);
                        }
                        PsychPipelineExecuteHook(windowRecord, kPsychIdentityBlit, overridepString1, NULL, TRUE, FALSE,
                                                &(windowRecord->fboTable[windowRecord->drawBufferFBO[viewid]]), NULL,
                                                &(windowRecord->fboTable[windowRecord->inputBufferFBO[viewid]]), NULL);
                    }
                }
                else {
                    // No rescaling by panel-fitter required:
                    if (windowRecord->gfxcaps & kPsychGfxCapFBOBlit) {
                        // We use this for multisample-resolve of multisampled drawBufferFBO's.
                        // A simple glBlitFramebufferEXT() call will do the copy & downsample operation:
                        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);
                    }
                    else {
                        // No blitting possible. Fallback to imaging pipeline, which has multisample texture bound,
                        // so that should work as well, albeit less efficient:
                        PsychPipelineExecuteHook(windowRecord, kPsychIdentityBlit, NULL, NULL, TRUE, FALSE,
                                                &(windowRecord->fboTable[windowRecord->drawBufferFBO[viewid]]), NULL,
                                                &(windowRecord->fboTable[windowRecord->inputBufferFBO[viewid]]), NULL);
                    }
                }
            }
        }

        // 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]]),
                                            (windowRecord->imagingMode & kPsychNeedOtherStreamInput) ? &(windowRecord->fboTable[windowRecord->inputBufferFBO[1 - 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 || stereo_mode == kPsychDualStreamStereo) {
            // No stereo or quad-buffered stereo or dual-window stereo or own frame-seq stereo, or explicitely requested dual stereo streams - 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/dual-window/frameseq/dual-stream stereo modes: 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 or other dual-stream/separate stream modes, 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 || stereo_mode == kPsychDualStreamStereo ||
                                    ((imagingMode & kPsychNeedDualWindowOutput) && !(windowRecord->imagingMode & kPsychNeedFinalizedFBOSinks))) ? 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]]),
                                            ((windowRecord->imagingMode & kPsychNeedOtherStreamInput) && (windowRecord->preConversionFBO[1-viewid]>=0)) ? &(windowRecord->fboTable[windowRecord->preConversionFBO[1 - 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]]),
                                            ((windowRecord->imagingMode & kPsychNeedOtherStreamInput) && (windowRecord->preConversionFBO[1-viewid]>=0)) ? &(windowRecord->fboTable[windowRecord->preConversionFBO[1 - 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() > 4)) 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);

            // If the finalizedFBO for this viewid has a renderCompleteSemaphore attached, then signal the semaphore:
            if (glIsSemaphoreEXT && glIsSemaphoreEXT(windowRecord->fboTable[windowRecord->finalizedFBO[viewid]]->renderCompleteSemaphore)) {
                GLenum dstLayout = GL_LAYOUT_TRANSFER_SRC_EXT; // GL_LAYOUT_SHADER_READ_ONLY_EXT;

                glSignalSemaphoreEXT(windowRecord->fboTable[windowRecord->finalizedFBO[viewid]]->renderCompleteSemaphore, 0, NULL,
                                     1, &windowRecord->fboTable[windowRecord->finalizedFBO[viewid]]->coltexid, &dstLayout);

                if (PsychPrefStateGet_Verbosity() > 5)
                    printf("PTB-INFO: renderCompleteSemaphore for finalizedFBO[%i] of window %i signalled.\n", viewid, windowRecord->windowIndex);
            }
        }

        // 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.

        // Flush the pipeline. This is very important in case renderCompleteSemaphore's are used, otherwise
        // the external consumer might hang for a long time!
        glFlush();

        // 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] (Stereo) or
        // finalizedFBO[0] (dual-window output) into the backbuffer of the slave-window:
        if (stereo_mode == kPsychDualWindowStereo || (imagingMode & kPsychNeedDualWindowOutput)) {
            if (windowRecord->slaveWindow == NULL) {
                if (PsychPrefStateGet_Verbosity() > 4) 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.
                // Note that for kPsychNeedDualWindowOutput instead of kPsychDualWindowStereo, we need to invert the source vs. target,
                // ie. blit into finalizedFBO[1] instead of [0].
                //
                // -> This is a bit dirty and convoluted, but it is the most efficient procedure for this special case.
                if (stereo_mode == kPsychDualWindowStereo) {
                    PsychPipelineExecuteHook(windowRecord->slaveWindow, kPsychIdentityBlit, NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->finalizedFBO[1]]), NULL, &(windowRecord->fboTable[windowRecord->finalizedFBO[0]]), NULL);
                } else {
                    // We must make sure to blit to the true system framebuffer / OpenGL backbuffer, regardless what other
                    // mappings are set up in finalizedFBO[1], so enforce a OpenGL FBO id of zero == System framebuffer during
                    // the blit, and then restore to whatever it was before after the blit:
                    GLuint oldfbo = windowRecord->fboTable[windowRecord->finalizedFBO[1]]->fboid;
                    windowRecord->fboTable[windowRecord->finalizedFBO[1]]->fboid = 0;
                    if (PsychIsHookChainOperational(windowRecord->slaveWindow, kPsychMirrorWindowBlit))
                        PsychPipelineExecuteHook(windowRecord->slaveWindow, kPsychMirrorWindowBlit, NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->finalizedFBO[0]]), NULL, &(windowRecord->fboTable[windowRecord->finalizedFBO[1]]), NULL);
                    else
                        PsychPipelineExecuteHook(windowRecord->slaveWindow, kPsychIdentityBlit, NULL, NULL, TRUE, FALSE, &(windowRecord->fboTable[windowRecord->finalizedFBO[0]]), NULL, &(windowRecord->fboTable[windowRecord->finalizedFBO[1]]), NULL);

                    windowRecord->fboTable[windowRecord->finalizedFBO[1]]->fboid = oldfbo;
                }

                // 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();

                // Fixup possible low-level framebuffer layout changes caused by commands above this point. Needed from native 10bpc FB support to work reliably.
                // First fixup framebuffer of slave window:
                PsychFixupNative10BitFramebufferEnableAfterEndOfSceneMarker(windowRecord->slaveWindow);

                // Restore current context and glFinish it as well:
                PsychSetGLContext(windowRecord);
                glFinish();

                // Then fixup framebuffer of master window:
                PsychFixupNative10BitFramebufferEnableAfterEndOfSceneMarker(windowRecord);
            }
        }
    }    // 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);
    }

    // Peform extensive checking for OpenGL errors, unless instructed not to do so:
    if (!(PsychPrefStateGet_ConserveVRAM() & kPsychAvoidCPUGPUSync)) {
        GLenum glerr;

        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 (Preflip-II)!\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 preflip (Preflip-II): %s.\n", gluErrorString(glerr));
            }
        }

        PsychTestForGLErrors();
    }

    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);

    // 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 (I)!\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 (I): %s.\n", gluErrorString(glerr));
            }
        }

        PsychTestForGLErrors();
    }

    // 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?
                    if ((PsychPrefStateGet_ConserveVRAM() & kPsychDisableAUXBuffers) || !PsychIsGLClassic(windowRecord)) {
                        auxbuffers = 0;
                    }
                    else {
                        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 (II)!\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 (II): %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);
}

/* Hard-Reset the current rendertarget. Only call this from the ScreenCloseAllWindows() function! */
void PsychColdResetDrawingTarget(void)
{
    currentRendertarget = NULL;
}

/* 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, movie 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;
    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);
            }

            // Safeguard us against being called with windowRecord unsuitable as drawing target, e.g., kPsychProxyWindow type:
            if (windowRecord && !(PsychIsOnscreenWindow(windowRecord) || PsychIsOffscreenWindow(windowRecord))) {
                if (PsychPrefStateGet_Verbosity() > 1)
                    printf("PTB-WARNING: PsychSetDrawingTarget() called with non-drawingtarget windowRecord[%i:%p], type %i! No-Op.\n",
                           windowRecord->windowIndex, windowRecord, windowRecord->windowType);

                recursionLevel--;
                return;
            }

            // 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.

                    // 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 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);

                    // Set "dirty" flag on texture: (Ab)used to trigger regeneration of mip-maps during texture drawing of mip-mapped textures.
                    windowRecord->needsViewportSetup = TRUE;

                    // 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);
                        }
                    }
                }
            } // 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);
    if ((windowRecord->proj == NULL) || useRawFramebufferSize) {
        // Default case: No override projection matrix assigned, or use of it not wanted.
        // Setup standard ortho transform:
        glLoadIdentity();
        if (!PsychIsGLES(windowRecord)) {
            gluOrtho2D(rect[kPsychLeft], rect[kPsychRight], rect[kPsychBottom], rect[kPsychTop]);
        }
        else {
            glOrthof((float) rect[kPsychLeft], (float) rect[kPsychRight], (float) rect[kPsychBottom], (float) rect[kPsychTop], (float) -1, (float) 1);
        }
    }
    else {
        // Override projection matrix/matrices assigned. Select proper one for given
        // mono/stereo view:
        if (windowRecord->stereomode > 0 && windowRecord->stereodrawbuffer == 1)
            glLoadMatrixd(&(windowRecord->proj[16]));
        else
            glLoadMatrixd(&(windowRecord->proj[0]));
    }

    // 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)
{
    // Do nothing if panel fitter is active or the clientrect has been set to a fixed
    // size at openwindow time for the lifetime of this window:
    if (windowRecord->imagingMode & (kPsychNeedGPUPanelFitter | kPsychNeedClientRectNoFitter)) return;

    // 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 & kPsychTripleWidthWindow) ? 3 : 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...
            if (!(backupRendertarget->gfxcaps & kPsychGfxCapNPOTTex)) {
                twidth=1; while(twidth < (int) PsychGetWidthFromRect(backupRendertarget->rect)) { twidth = twidth * 2; };
                theight=1; while(theight < (int) PsychGetHeightFromRect(backupRendertarget->rect)) { theight = theight * 2; };
            } else {
                // GPU has NPOT support, take it "as is":
                twidth  = (int) PsychGetWidthFromRect(backupRendertarget->rect);
                theight = (int) PsychGetHeightFromRect(backupRendertarget->rect);
            }

            if (PsychIsGLES(backupRendertarget)) {
                // OES extension for faster format supported?
                if (strstr((const char*) glGetString(GL_EXTENSIONS), "GL_EXT_texture_format_BGRA8888")) {
                    // Faster path:
                    glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, twidth, theight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
                }
                else {
                    // Slower fallback:
                    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, twidth, theight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
                }
            }
            else {
                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 exists: 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...
                if (!(backupRendertarget->gfxcaps & kPsychGfxCapNPOTTex)) {
                    // No non-power-of-two support: Need to find closest matching POT texture size:
                    twidth=1; while(twidth < (int) PsychGetWidthFromRect(backupRendertarget->rect)) { twidth = twidth * 2; };
                    theight=1; while(theight < (int) PsychGetHeightFromRect(backupRendertarget->rect)) { theight = theight * 2; };
                }

                if (PsychIsGLES(backupRendertarget)) {
                    // OES extension for faster format supported?
                    if (strstr((const char*) glGetString(GL_EXTENSIONS), "GL_EXT_texture_format_BGRA8888")) {
                        // Faster path:
                        glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, twidth, theight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
                    }
                    else {
                        // Slower fallback:
                        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, twidth, theight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
                    }
                }
                else {
                    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;

    // 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");
        }
    }

    // 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;

    (void) windowRecord;

    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)
{
    int gpuMaintype, gpuMinortype, gpuModel;
    unsigned int rendergpuVendor = 0, rendergpuModel = 0;
    psych_bool verbose = (PsychPrefStateGet_Verbosity() > 5) ? TRUE : FALSE;
    psych_bool nvidia = FALSE;
    psych_bool ati = FALSE;
    psych_bool intel = FALSE;
    psych_bool llvmpipe = FALSE;
    psych_bool vc4 = FALSE;
    GLint maxtexsize=0, maxcolattachments=0, maxaluinst=0;
    GLboolean nativeStereo = FALSE;
    int mesaversion[3];
    const char* mesaver = NULL;

    gpuMaintype = kPsychUnknown;
    gpuModel = 0xFFFFFFFF;
    gpuMinortype = 0;
    PsychGetGPUSpecs(windowRecord->screenNumber, &gpuMaintype, &gpuMinortype, &gpuModel, NULL);

    // Try to detect if we are running on top of Mesa OpenGL, and which version:
    mesaver = strstr((const char*) glGetString(GL_VERSION), "Mesa");
    if (mesaver && (3 == sscanf(mesaver, "Mesa %i.%i.%i", &mesaversion[0], &mesaversion[1], &mesaversion[2]))) {
        if (verbose)
            printf("PTB-DEBUG: Running on Mesa version %i.%i.%i\n", mesaversion[0], mesaversion[1], mesaversion[2]);
    }
    else {
        mesaversion[0] = mesaversion[1] = mesaversion[2] = 0;
        if (verbose)
            printf("PTB-DEBUG: Not running on Mesa graphics library.\n");
    }

    #if PSYCH_SYSTEM == PSYCH_LINUX
    // Try to query renderer gpu vendor from Mesa if supported:
    if (glxewIsSupported("GLX_MESA_query_renderer") && (glXQueryCurrentRendererIntegerMESA != NULL) &&
        glXQueryCurrentRendererIntegerMESA(GLX_RENDERER_VENDOR_ID_MESA, &rendergpuVendor) && (rendergpuVendor != 0xFFFFFFFF)) {
        if (verbose)
            printf("PTB-DEBUG: Vendor Id of render gpu - rendergpuVendor 0x%x.\n", rendergpuVendor);
    }
    else {
        rendergpuVendor = 0;
    }
    #endif

    // 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") ||
        (rendergpuVendor == PCI_VENDOR_ID_NVIDIA)) {
        nvidia = TRUE; sprintf(windowRecord->gpuCoreId, "NV10");
    }

    if (strstr((char*) glGetString(GL_VENDOR), "INTEL") || strstr((char*) glGetString(GL_VENDOR), "Intel") || strstr((char*) glGetString(GL_RENDERER), "Intel") ||
        (rendergpuVendor == PCI_VENDOR_ID_INTEL)) {
        intel = TRUE; sprintf(windowRecord->gpuCoreId, "Intel");
    }

    if (strstr((char*) glGetString(GL_VENDOR), "VMware") || strstr((char*) glGetString(GL_RENDERER), "llvmpipe")) {
        llvmpipe = TRUE; sprintf(windowRecord->gpuCoreId, "gllvm");
    }

    // 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") || strstr((char*) glGetString(GL_RENDERER), "Radeon") ||
        (rendergpuVendor == PCI_VENDOR_ID_AMD) || (rendergpuVendor == PCI_VENDOR_ID_ATI)) {
        ati = TRUE; sprintf(windowRecord->gpuCoreId, "R100");
    }

    if (strstr((char*) glGetString(GL_VENDOR), "Broadcom") || strstr((char*) glGetString(GL_RENDERER), "VC4")) {
        vc4 = TRUE; sprintf(windowRecord->gpuCoreId, "VC4");
    }

    // Is this a hybrid graphics dual-gpu laptop which uses DRI PRIME for muxless render offload?
    #if PSYCH_SYSTEM == PSYCH_LINUX
    if ((getenv("DRI_PRIME") && ((const char*) getenv("DRI_PRIME"))[0] != '0') ||
        (rendergpuVendor && (rendergpuVendor != 0xFFFFFFFF) && (gpuMaintype != kPsychUnknown) &&
        glXQueryCurrentRendererIntegerMESA(GLX_RENDERER_DEVICE_ID_MESA, &rendergpuModel) && (rendergpuModel != 0xFFFFFFFF) && (gpuModel != (int) 0xFFFFFFFF) &&
        (((int) rendergpuModel != gpuModel) || (gpuMaintype == kPsychIntelIGP && rendergpuVendor != PCI_VENDOR_ID_INTEL) ||
        (gpuMaintype == kPsychGeForce && rendergpuVendor != PCI_VENDOR_ID_NVIDIA) ||
        (gpuMaintype == kPsychRadeon && rendergpuVendor != PCI_VENDOR_ID_AMD && rendergpuVendor != PCI_VENDOR_ID_ATI)))) {
        windowRecord->hybridGraphics = 1;
        if (verbose) printf("PTB-DEBUG: Prime indicators: rendergpuVendor 0x%x, rendergpuModel 0x%x, displaygpuModel 0x%x displaygpuType 0x%x.\n", rendergpuVendor, rendergpuModel, gpuModel, gpuMaintype);
        if (PsychPrefStateGet_Verbosity() >= 3) printf("PTB-INFO: Hybrid graphics setup with DRI PRIME muxless render offload detected.\n");

        // Prime renderoffload only works with proper timing and quality if DRI3/Present
        // is used for our onscreen window:
        if ((windowRecord->specialflags & kPsychIsX11Window) && !(windowRecord->specialflags & kPsychIsDRI3Window) && (PsychPrefStateGet_Verbosity() > 1)) {
            printf("PTB-WARNING: Hybrid graphics DRI PRIME muxless render offload requires the use of DRI3/Present for rendering,\n");
            printf("PTB-WARNING: but this is not enabled on your setup. Use XOrgConfCreator to setup your system accordingly.\n");
            printf("PTB-WARNING: Without this, your visual stimulation will suffer severe timing and display artifacts.\n\n");
        }
    }

    #ifndef PTB_USE_WAFFLE
    // Is this a hybrid graphics dual-gpu laptop which uses muxless Optimus / PRIME render offload to a NVIDIA gpu, while using the proprietary driver?
    if ((windowRecord->specialflags & kPsychIsX11Window) && (windowRecord->specialflags & kPsychIsDRI3Window) && getenv("__NV_PRIME_RENDER_OFFLOAD")) {
        // Yes. Try to setup our custom timestamping method which plays tricks with the X11 Present extension:
        if (PsychPrefStateGet_Verbosity() >= 3)
            printf("PTB-INFO: Hybrid graphics with NVidia proprietary Prime render offload implementation detected. Enabling special handling.\n");

        // Enable our own Present-Event handling:
        PsychOSEnablePresentEventReception(windowRecord, 0, TRUE);

        // Enable our own Present-Event handling:
        if (PsychOSEnablePresentEventReception(windowRecord, 0, TRUE))
            windowRecord->hybridGraphics = 5;
    }
    #endif

    // Find out if the primary output of the screen on which this window is
    // displaying has a "PRIME Synchronization" output property. If so then
    // we are likely running under a NVidia Optimus PRIME setup with output slave
    // instead of a DRI3/Present renderoffload setup and need to take note
    // of this for some special case handling:
    if ((windowRecord->specialflags & kPsychIsX11Window) && (gpuMaintype == kPsychIntelIGP) && strstr((char*) glGetString(GL_VENDOR), "NVIDIA")) {
        static Atom nvidiaprimesync;
        CGDirectDisplayID dpy;
        int screen;
        RROutput output = (RROutput) 0;

        // Get XID / RROutput id of primary output for this screen:
        PsychOSGetOutputProps(windowRecord->screenNumber, 0, FALSE, NULL, NULL, (unsigned long *) &output);

        // Map screenNumber to dpy, screen, rootwindow and RandR output:
        PsychGetCGDisplayIDFromScreenNumber(&dpy, windowRecord->screenNumber);
        screen = PsychGetXScreenIdForScreen(windowRecord->screenNumber);

        PsychLockDisplay();

        nvidiaprimesync = XInternAtom(dpy, "PRIME Synchronization", True);
        if (nvidiaprimesync != None) {
            Window root = RootWindow(dpy, screen);
            XRRScreenResources *resources = XRRGetScreenResources(dpy, root);
            if (resources) {
                // Prime sync property exposed?
                unsigned long nitems;
                unsigned long bytes_after;
                unsigned char *prop;
                Atom actual_type;
                int actual_format;
                // Get Prime sync property for primary output:
                if ((XRRGetOutputProperty(dpy, output, nvidiaprimesync, 0, 4, False, False, None, &actual_type, &actual_format, &nitems, &bytes_after, &prop) == Success) && (prop != NULL)) {
                    char prime_sync_on = *((char *) prop);
                    XFree(prop);
                    windowRecord->hybridGraphics = 2;

                    // Are we running under the hacked/enhanced modesetting ddx as outputSink
                    // driver, which supports our custom UDP flip timestamping protocol?
                    if (XInternAtom(dpy, "PrimeTimingHack1", True) != None) {
                        windowRecord->hybridGraphics = 3;
                    }

                    // Is this a modesetting ddx with additional 1 frame lag avoidance?
                    if (XInternAtom(dpy, "PrimeTimingHack2", True) != None) {
                        windowRecord->hybridGraphics = 4;
                    }

                    if (PsychPrefStateGet_Verbosity() >= 3) {
                        printf("PTB-INFO: Hybrid graphics setup with support for \"NVidia Optimus\" DRI PRIME-Sync output slaving detected - or so i think?\n");
                        printf("PTB-INFO: If you are displaying on a video output directly connected to the NVidia gpu, then my thinking here\n");
                        printf("PTB-INFO: might be wrong. In that case use PsychTweak('UseGPUIndex') to fix me! Otherwise timing might be wrong.\n");
                        printf("PTB-INFO: If i'm right, then the following assessments hold for your Optimus setup with NVidia's proprietary driver:\n");

                        if (!prime_sync_on) {
                            printf("PTB-WARNING: Optimus GPU synchronization seems to be disabled. Maybe the nvidia-modesetting driver is not enabled?\n");
                            printf("PTB-WARNING: Both visual timing and timestamping will likely be highly unreliable. Please read 'help HybridGraphics'\n");
                            printf("PTB-WARNING: on how to enable nvidia-modesetting, and then reboot once to improve quality of visual stimulation.\n");
                        }
                        else if (windowRecord->hybridGraphics < 3) {
                            printf("PTB-INFO: The custom modesetting ddx of Psychtoolbox for Optimus is not installed, so visual timestamping will be wrong!\n");
                            printf("PTB-INFO: Therefore visual timing and timestamping will likely be highly unreliable. Please read 'help HybridGraphics'\n");
                            printf("PTB-INFO: on how to install a custom modesetting ddx in order to fix visual onset timestamping and improve timing.\n");
                        }
                        else if (windowRecord->hybridGraphics >= 3) {
                            printf("PTB-INFO: Custom modesetting ddx detected. Visual onset timestamps should be mostly reliable at least on display setups\n");
                            printf("PTB-INFO: with at most one display per X-Screen and one topmost, non-transparent fullscreen window on that X-Screen.\n");
                            if (windowRecord->hybridGraphics < 4) {
                                printf("PTB-INFO: Accurate onset timing requires strict adherence to recommended practices for Screen('Flip', window, tWhen) 'tWhen' times.\n");
                                printf("PTB-INFO: An extra stimulus onset delay of 1 video refresh cycle can't be avoided for immediate flips with this driver though.\n");
                                printf("PTB-INFO: If you need low-latency stimulus onset, install the NoLag variant of the modesetting ddx instead of the highlag variant.\n");
                            }
                        }
                    }
                }
                XRRFreeScreenResources(resources);
            }
        }
        else {
            // Intel iGPU + NVidia dGPU + NVidia proprietary driver, but no PRIME-SYNC capable ddx.
            // This is likely hopeless, warn user:
            if (PsychPrefStateGet_Verbosity() >= 3) {
                printf("PTB-INFO: This seems to be an Optimus hybrid graphics laptop with proprietary NVidia driver, which will cause timing problems.\n");
                printf("PTB-INFO: Please read 'help HybridGraphics' on how to install a custom modesetting ddx on X-Server 1.19 or later in order to\n");
                printf("PTB-INFO: fix visual onset timestamping and improve timing. If you are displaying on a video output directly connected to the\n");
                printf("PTB-INFO: NVidia gpu though, then my mapping of GPU's might be wrong (PsychTweak('UseGPUIndex') to fix it) and you can ignore\n");
                printf("PTB-INFO: this message.\n");
            }
        }
        PsychUnlockDisplay();
    }

    // On a hybridGraphics setup always make sure our Linux kernel is recent enough:
    if ((windowRecord->hybridGraphics) && (PsychPrefStateGet_Verbosity() > 1)) {
        struct utsname unameresult;
        int major = 0, minor = 0, patchlevel = 0;
        uname(&unameresult);
        sscanf(unameresult.release, "%i.%i.%i", &major, &minor, &patchlevel);

        // If the display iGPU is an Intel chip then we need kernel 4.6 or later for proper dmabuf-fence sync:
        if (gpuMaintype == kPsychIntelIGP) {
            // Need at least Linux 4.6 for Intel iGPU + some dGPU. In principle 4.5 would do,
            // but NVIDIA found some performance issues when running on their Optimus drivers
            // wrt. concurrent hardware cursor updates on the Intel side. Linux 4.6 seems to
            // fix that, according to them.
            if (major < 4 || (major == 4 && minor < 6)) {
                printf("PTB-WARNING: This hybrid graphics setup uses a Intel gpu for display, but your Linux kernel %i.%i.%i is too old.\n", major, minor, patchlevel);
                printf("PTB-WARNING: Please upgrade to Linux version 4.6 or later for hybrid graphics support without visual stimulation artifacts.\n");
                printf("PTB-WARNING: If your machine has a modern AMD discrete gpu, upgrading to Linux 4.8.11 or later is required.\n");
            }
            else if ((windowRecord->hybridGraphics == 1) && (ati || rendergpuVendor == PCI_VENDOR_ID_AMD || rendergpuVendor == PCI_VENDOR_ID_ATI)) {
                // Intel iGPU + AMD dGPU renderoffload on Linux 4.6 or later. All is fine on older AMD
                // parts driven by radeon-kms. "Volcanic Islands" GCN 1.2+ gpus driven by amdgpu-kms
                // need at least Linux 4.8.11 in order to provide proper fence sync
                // under high load. Specifically the following patch is needed for amdgpu-kms:
                // http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=8e94a46c1770884166b31adc99eba7da65a446a7
                // The patch has been back-ported already to the Linux 4.8.11 kernel, and also by
                // Canonical's kernel team to their upcoming Ubuntu-4.8.0-29.31 kernel for Ubuntu 16.10
                // and the future Ubuntu 16.04.2-LTS HWE-1 stack:
                // http://kernel.ubuntu.com/git/ubuntu/ubuntu-yakkety.git/commit/?h=master-next&id=d8ae64bcc02ff9e1c171539bdea55640ef53a1eb

                // Probe if amdgpu-kms driver modules is loaded:
                FILE *amdgpu = fopen("/sys/module/amdgpu", "r");
                if (amdgpu) {
                    // Yes, dGPU is driven by amdgpu-kms.
                    fclose(amdgpu);

                    if (verbose) printf("PTB-DEBUG: Render gpu driven by amdgpu-kms. Checking required kernel version...\n");

                    if (major == 4 && minor == 8 && patchlevel == 0) {
                        char extraversionsignature[512];
                        // Kernel release 4.8.0 could mean a Ubuntu kernel, ie., an Ubuntu kernel
                        // originally based on 4.8.0, but then maintained independent of mainline.
                        // If so, then the /proc/version_signature file should exist and contain
                        // the release of the mainline kernel on which the Ubuntu 4.8.0-XXX kernel
                        // is based. Ubuntu 16.10 "Yaketty Yak", and Ubuntu 16.04.2-LTS HWE kernel
                        // are based on 4.8.0, so lets handle this Ubuntu specific special case.
                        // Is this a Ubuntu distro kernel?
                        FILE* fd = fopen("/proc/version_signature", "rt");
                        if (fd && fgets(extraversionsignature, sizeof(extraversionsignature), fd)) {
                            // Seems so. Find signature of the stable kernel on which this one is based:
                            if (strstr(extraversionsignature, "Ubuntu")) {
                                // Ubuntu 4.8.0-X Yaketty kernel, based on 4.8.x upstream. Lets find out
                                // on which upstream kernel it is based and use that for checking Prime
                                // renderoffload capabilities:
                                if (sscanf(extraversionsignature, "%*s %*s %i.%i.%i", &major, &minor, &patchlevel) == 3) {
                                    if (verbose) printf("PTB-DEBUG: Ubuntu 4.8.0 Yaketty/16.04.2-LTS HWE-I kernel detected. Based on mainline kernel %i.%i.%i. Using this for detection.\n",
                                                        major, minor, patchlevel);
                                }
                                else {
                                    printf("PTB-WARNING: Could not find mainline kernel version of this Ubuntu 4.8.0 based kernel. May misdetect AMD hybrid graphics capabilities!\n");
                                }
                            }
                        }
                        if (fd) fclose(fd);
                    }

                    // As of November 2016 we need kernel 4.8.11 or later, otherwise warn about potential display artifacts:
                    if (major == 4 && minor < 9 && !(minor == 8 && patchlevel >= 11)) {
                        printf("PTB-WARNING: This hybrid graphics setup uses a Intel iGPU for display and AMD dGPU for rendering, but your Linux kernel %i.%i.%i\n", major, minor, patchlevel);
                        printf("PTB-WARNING: is too old to guarantee reliable visual stimulation. Upgrade to Linux version 4.8.11 or later for hybrid graphics support\n");
                        printf("PTB-WARNING: without visual stimulation artifacts. On a Ubuntu flavor, upgrade to at least Ubuntu Linux kernel 'Ubuntu-4.8.0-29.31'.\n");
                    }
                }
            }
        }
    }
    #endif

    // Check if this is an open-source (Mesa/Gallium) graphics driver on Linux with X11
    // backend in use. If so, we must emit a single pixel write into the backbuffer, followed
    // by a pipeline glFlush after each scheduled double-buffer swap, all protected by the
    // display lock. Why? Because each scheduled/pending bufferswap invalidates the drawable
    // of the associated onscreen window, so the first write or read of the system framebuffer
    // after a scheduled swap will require a buffer revalidation, which will require a roundtrip
    // to the X-Server via our shared X11 x-display connection. Any operation on this connection
    // must be lock protected for thread-safety. We normally wouldn't know when the first access
    // to the framebuffer happens after swap and we can't lock-protect everything, so we intentionally
    // do a dummy-write immediately after each swap, under lock protection, so we know this revalidation
    // roundtrip will happen under proper lock protection. Without this, we'd get crashes on the
    // FOSS drivers. This hack is not needed with the NVidia proprietary drivers, as they do their
    // buffer revalidation without involvement of the X11 protocol. AMD Catalyst likely and AMD amdgpu-pro
    // definitely do need this workaround as well.
    //
    // So the rules are: If this onscreen window is using a X11 display connection for its operation
    // and the graphics driver is not in a white-list of known multithread-safe drivers (ie., it is
    // not the NVidia binary blob), we assume locking is required after each scheduled swap:
    if (windowRecord->specialflags & kPsychIsX11Window) {
        // X11 display backend in use. Lock-protect unless it is the white-listed NVidia blob:
        if (!strstr((char*) glGetString(GL_VENDOR), "NVIDIA")) {
            // Driver requires locked framebuffer dummy-write + flush:
            windowRecord->specialflags |= kPsychNeedPostSwapLockedFlush;
            if (verbose) printf("PTB-DEBUG: Linux X11 backend with non-NVidia blob drivers - Enabling locked pixeltoken-write + flush workaround for XLib thread-safety.\n");
        }
    }

    // Check if this is a NVidia GPU with (currently required) proprietary driver and G-Sync support (ie. non-Optimus configuration, Kepler or later):
    if ((gpuMaintype == kPsychGeForce) && (gpuMinortype >= 0x0E0) && nvidia && !(windowRecord->hybridGraphics) && strstr((char*) glGetString(GL_VENDOR), "NVIDIA")) {
        windowRecord->gfxcaps |= kPsychGfxCapGSync;
        if (verbose)
            printf("PTB-DEBUG: NVidia GPU with G-Sync support detected. VRR requested by usercode? %s\n", (windowRecord->vrrMode) ? "Yes" : "No");
    }

    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("GPU supports UYVY - YCrCb texture formats for optimized handling of video content.\n");
    }

    if (glewIsSupported("GL_ARB_texture_non_power_of_two") || strstr((const char*) glGetString(GL_EXTENSIONS), "GL_OES_texture_npot")) {
        windowRecord->gfxcaps |= kPsychGfxCapNPOTTex;
        if (verbose) printf("GPU supports non-power-of-two textures.\n");
    }

    // OpenGL-ES setup?
    if (PsychIsGLES(windowRecord)) {
        // OES framebuffer objects supported?
        if (strstr((const char*) glGetString(GL_EXTENSIONS), "GL_OES_framebuffer_object") || glewIsSupported("GL_EXT_framebuffer_object") || glewIsSupported("GL_ARB_framebuffer_object")) {
            if (verbose) printf("Basic OES framebuffer objects supported --> RGBA rendertargets with blending.\n");
            windowRecord->gfxcaps |= kPsychGfxCapFBO;

            if (strstr((const char*) glGetString(GL_EXTENSIONS), "_framebuffer_blit")) {
                if (verbose) printf("OES Framebuffer objects support fast blitting between each other.\n");
                windowRecord->gfxcaps |= kPsychGfxCapFBOBlit;
            }
        }
    }

    // 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_ARB_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;
        }

        // Support for framebuffer blits which do a scaling operation and a multisample resolve at once?
        if ((windowRecord->gfxcaps & kPsychGfxCapFBOMultisample) &&
            (glewIsSupported("GL_EXT_framebuffer_multisample_blit_scaled") || strstr((const char*) glGetString(GL_EXTENSIONS), "GL_EXT_framebuffer_multisample_blit_scaled"))) {
            if (verbose) printf("Framebuffer objects support single-pass multisample resolve blits and image rescaling.\n");
            windowRecord->gfxcaps |= kPsychGfxCapFBOScaledResolveBlit;
        }
    }

    // 32-bpc floating point textures on OpenGL-ES hardware supported?
    if (strstr((char*) glGetString(GL_EXTENSIONS), "GL_OES_texture_float")) {
        // Yes: This means we (only) have 32 bpc float textures and possibly framebuffers,
        // not 16 bpc. It also means we only have nearest neighbour textures sampling/filtering,
        // and probably no alpha blending. But better than nothing:
        //
        // Note: Seems that 16/32 bpc float textures behave the same as on desktop GL. We still
        // restrict ourselves to 32 bpc formats instead of additionally 16 bpc to simplify initial
        // porting to embedded gl - no need to complicate things.
        windowRecord->gfxcaps |= kPsychGfxCapFPTex32;
        if (verbose) printf("Hardware supports floating point textures of 32bpc float format.\n");

        if (strstr((char*) glGetString(GL_EXTENSIONS), "GL_OES_texture_float_linear")) {
            windowRecord->gfxcaps |= kPsychGfxCapFPFilter32;
            if (verbose) printf("Hardware supports filtering of 32 bpc floating point textures.\n");
        }

        // 32-bpc float FBO's supported? We assume that if FBO's are supported and float textures
        // are supported, that then also float FBO's with float blending are available. The spec
        // does not say much about this, but at least on the NVidia binary desktop drivers this seems
        // to be the case:
        if ((windowRecord->gfxcaps & kPsychGfxCapFBO)) { //  && strstr((char*) glGetString(GL_EXTENSIONS), "GL_EXT_color_buffer_float")) {
            windowRecord->gfxcaps |= kPsychGfxCapFPFBO32;
            windowRecord->gfxcaps |= kPsychGfxCapFPBlend32;
            if (verbose) printf("Hardware likely supports floating point framebuffers of 32bpc float format with blending.\n");
        }
    }

    // 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;
                }

                // Running on Linux + Mesa OpenGL driving an AMD R600 gpu or later, and thereby possibly a
                // AMD GCN gpu controlled by the Mesa Gallium radeonsi gfx-driver? If so then we need a workaround
                // for a radeonsi bug present in Mesa versions 18.1, 18.2, and early versions of Mesa 18.3.x.
                // The bug is fixed upstream for Mesa 19+ and for Mesa 18.3.2+, so the workaround is used for
                // Mesa 18.1.0 - Mesa 18.3.1.
                //
                // The bug is that 1 component 1D or 2 component 2D vertex attributes submitted via VBO's or VAO's,
                // e.g., via glTexCoordPointer() or similar will not get transmitted to GLSL vertex shaders properly
                // iff user data type GL_DOUBLE is used. E.g., glTexCoordPointer(1, GL_DOUBLE, ...) will result in
                // reception of corrupted gl_MultiTexCoordX.x values. This can be avoided by using GL_FLOAT as input
                // data type, e.g., glTexCoordPointer(1, GL_FLOAT, ...) or using the experimental NIR shader backend
                // (setenv R600_DEBUG=nir). Set the specialflags flag kPsychNeedVBODouble12Workaround to signal this
                // bug. Atm. only Screen('DrawDots') needs to work around the bug if smooth/round point rendering or
                // shader based rendering is requested (dot_type > 0) and different point sizes are used for individual
                // dots.
                if ((PSYCH_SYSTEM == PSYCH_LINUX) && strstr(windowRecord->gpuCoreId, "R600") &&
                    (mesaversion[0] == 18) && (mesaversion[1] > 0) && ((mesaversion[1] < 3) || (mesaversion[2] < 2))) {
                    if (verbose)
                        printf("Potential AMD GCN gpu on Linux with potentially buggy Mesa Gallium radeonsi driver. Enabling 'DrawDots' workaround.\n");
                    windowRecord->specialflags |= kPsychNeedVBODouble12Workaround;
                }
            }
        }

        // 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;
            }
        }

        // FBO + float textures support on Intel gfx, Broadcom VideoCore 5/6, software renderers?
        if ((vc4 || intel || llvmpipe) && (windowRecord->gfxcaps & kPsychGfxCapFBO) && glewIsSupported("GL_ARB_texture_float")) {
            // Intel GPUs with FBO and ARB_texture_float support: These are usually of the HD graphics series and
            // recent enough to support floating point textures and rendertargets with 16 bpc and 32 bpc float, including
            // texture filtering and frame buffer blending, and as a bonus FP32 shading. Iow. they support the whole
            // shebang, as they are at least OpenGL 3.0 / Direct3D-10 compliant:
            if (verbose && intel) printf("Assuming HD graphics core or later: Hardware supports full 16/32 bit floating point textures, frame buffers, filtering and blending, as well as some 32 bit float shading.\n");

            // The software renderers also support the full functionality at full precision:
            if (verbose && llvmpipe) printf("Assuming Gallium LLVM-Pipe rasterizer: Renderer supports full 16/32 bit floating point textures, frame buffers, filtering and blending, as well as some 32 bit float shading.\n");

            // The older Broadcom VideoCore-4 (e.g., RaspberryPi 0/1/2/3) does not support floating point textures
            // or framebuffers at all, hence never reaches this code path. It is limited to 8 bpc fixed point.
            //
            // The Broadcom VideoCore-5/6 (e.g., as found in the RaspberryPi 4), with Mesa v3d driver, does support
            // 16 and 32 bpc floating point textures and framebuffers. However the hardware does not support texture
            // linear filtering or framebuffer blending on 32 bpc floating point textures and framebuffers, only on
            // 16 bpc half-float variants. Therefore we must make sure we use a GLSL filter shader for 32 bpc textures,
            // and avoid 32 bpc framebuffers for blending, or avoid blending on 32 bpc framebuffers:
            if (verbose && vc4) printf("Assuming VideoCore-6: Renderer supports full 16/32 bit floating point textures and frame buffers, but only 16 bit float filtering and blending, as well as some 32 bit float shading.\n");

            windowRecord->gfxcaps |= kPsychGfxCapFP32Shading;
            windowRecord->gfxcaps |= kPsychGfxCapFPFBO16;
            windowRecord->gfxcaps |= kPsychGfxCapFPFBO32;
            windowRecord->gfxcaps |= kPsychGfxCapFPFilter16;
            windowRecord->gfxcaps |= kPsychGfxCapFPBlend16;

            if (intel || llvmpipe) {
                // 32 bpc float filtering and blending only on Intel and software, not on v3d / VideoCore-6:
                windowRecord->gfxcaps |= kPsychGfxCapFPFilter32;
                windowRecord->gfxcaps |= kPsychGfxCapFPBlend32;
            }
        }
    }

    // Is GL_POINT_SMOOTH actually producing round, anti-aliased points? As of October 2015,
    // we know NVidia gpus support this on Linux, both with binary blob and nouveau, but the current
    // Mesa drivers for AMD and Intel don't. Windows and OSX graphics drivers do support point
    // smoothing. Modern AMD hardware supports point smoothing with the proprietary amdgpu-pro driver.
    // Check for OpenGL version 4 as a sign of a modern enough AMD gpu, as we don't want shader-based
    // in the driver:
    if ((PSYCH_SYSTEM != PSYCH_LINUX) || nvidia ||
        (strstr((char*) glGetString(GL_VENDOR), "ATI") && strstr((char*) glGetString(GL_VERSION), "Compatibility Profile") && !strncmp((char*) glGetString(GL_VERSION), "4.", 2))) {
        if (verbose) printf("Assuming hardware supports native OpenGL primitive smoothing (points, lines).\n");
        windowRecord->gfxcaps |= kPsychGfxCapSmoothPrimitives;
    }

    {
        // Check the rounding behavior if floating point color values are written to a
        // fixed point integer framebuffer - truncate or round to nearest integer?
        GLubyte testpixel;

        glClearColor(0.5, 0.5, 0.5, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);
        glReadPixels(0, 0, 1, 1, GL_RED, GL_UNSIGNED_BYTE, (GLvoid*) &testpixel);
        if (testpixel == 128)
            windowRecord->gfxcaps |= kPsychGfxCapFloatToIntRound;

        if (verbose)
            printf("Float color value 0.5 -> fixed point reads back as %i ==> %s.\n",
                   (int) testpixel, (windowRecord->gfxcaps & kPsychGfxCapFloatToIntRound) ? "Rounds" : "Truncates");
    }

    // Allow usercode to override our pessimistic view of vertex color precision:
    if (PsychPrefStateGet_ConserveVRAM() & kPsychAssumeGfxCapVCGood) {
        if (verbose) printf("Assuming hardware can process vertex colors at full 32bpc float precision, as requested by usercode via ConserveVRAMSetting kPsychAssumeGfxCapVCGood.\n");
        windowRecord->gfxcaps |= kPsychGfxCapVCGood;
    }

    // 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 some other known software renderer / rasterizer?
    if ((strstr((char*) glGetString(GL_VENDOR), "Humper") && strstr((char*) glGetString(GL_RENDERER), "Chromium")) ||
        (strstr((char*) glGetString(GL_VENDOR), "VMware")) ||
        (strstr((char*) glGetString(GL_VENDOR), "Mesa") && strstr((char*) glGetString(GL_RENDERER), "Software Rasterizer")) ||
        (strstr((char*) glGetString(GL_VENDOR), "Mesa") && strstr((char*) glGetString(GL_RENDERER), "llvmpipe"))) {
        // 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");
            #if (PSYCH_SYSTEM == PSYCH_WINDOWS) && defined(PTBOCTAVE3MEX)
            printf("PTB-INFO: Another reason for lack of hardware OpenGL could be that GNU/Octave's own opengl32.dll\n");
            printf("PTB-INFO: library has not been deleted or renamed by selecting 'System OpenGL' during installation of Octave,\n");
            printf("PTB-INFO: so it is enforcing software rendering.\n");
            printf("PTB-INFO: You have to delete or rename that file and then restart Octave for this to work.\n");
            printf("PTB-INFO: E.g., on Octave-7.3 at its standard installation location, the file to delete or rename would be likely this:\n");
            printf("PTB-INFO: C:\\Program Files\\GNU Octave\\Octave-7.3.0\\mingw64\\bin\\opengl32.dll\n");
            //printf("PTB-INFO: C:\\Program Files\\GNU Octave\\Octave-8.1.0\\mingw64\\bin\\opengl_switch.exe can be used to do this for you.\n\n");
            #endif
            // 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
    #ifndef PTB_USE_WAFFLE
    // Running on a XServer prior to version 1.8.2 with broken OpenML implementation? Mark it, if so:
    if (PsychPrefStateGet_Verbosity() > 4) {
        PsychLockDisplay();
        printf("PTB-Info: Running on '%s' XServer, Vendor release %i.\n", XServerVendor(windowRecord->targetSpecific.deviceContext), (int) XVendorRelease(windowRecord->targetSpecific.deviceContext));
        PsychUnlockDisplay();
    }

    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"));
        printf("OML_sync_control indicators: glXQueryExtensionsString() says %i.\n",
               !!((long) strstr(glXQueryExtensionsString(windowRecord->targetSpecific.deviceContext, PsychGetXScreenIdForScreen(windowRecord->screenNumber)), "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) &&
        strstr(glXQueryExtensionsString(windowRecord->targetSpecific.deviceContext, PsychGetXScreenIdForScreen(windowRecord->screenNumber)), "GLX_OML_sync_control")) {
    #else
    // Disable this whole code-path if PTB_USE_WAFFLE:
    if (FALSE) {
    #endif
        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:
        PsychLockDisplay();
        if (XVendorRelease(windowRecord->targetSpecific.privDpy) < 10802000) {
            PsychUnlockDisplay();
            // 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 {
            PsychUnlockDisplay();
            // 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.\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.\n");

        // OpenML timestamping in PsychOSGetSwapCompletionTimestamp() and PsychOSGetVBLTimeAndCount() disabled:
        windowRecord->specialflags |= kPsychOpenMLDefective;

        // OpenML swap scheduling in PsychFlipWindowBuffers() disabled:
        windowRecord->gfxcaps &= ~kPsychGfxCapSupportsOpenML;
    #endif

    #if (PSYCH_SYSTEM == PSYCH_LINUX) && !defined(PTB_USE_WAFFLE)
    PsychLockDisplay();
    if (strstr(glXQueryExtensionsString(windowRecord->targetSpecific.deviceContext, PsychGetXScreenIdForScreen(windowRecord->screenNumber)), "GLX_EXT_buffer_age")) {
        // Age queries for current backbuffer supported:
        if (verbose) printf("System supports backbuffer age queries.\n");
        windowRecord->gfxcaps |= kPsychGfxCapSupportsBufferAge;

        // Is this a pre NV-50 gpu (GeForce 7xxx or earlier)? If so we don't use buffer age queries,
        // because quite a few of the NVidia proprietary graphics drivers for these old gpus do have
        // bugs in their query mechanism that could cause us to spew a lot of false positive about
        // broken visual stimulation timing. NV-50 and later have well working drivers available:
        if ((gpuMaintype == kPsychGeForce) && (gpuMinortype > 0x0) && (gpuMinortype < 0x50)) {
            windowRecord->gfxcaps &= ~kPsychGfxCapSupportsBufferAge;
            if (verbose) printf("Not using backbuffer age queries due to pre NV-50 NVidia gpu with potentially problematic driver wrt. this feature.\n");
        }
    }
    PsychUnlockDisplay();
    #endif

    // If we are on Linux + Waffle backend, we call PsychOSInitializeOpenML()
    // anyway. It may use any "OpenML equivalent" of a given backend to do
    // the job of OpenML. On most backends it will no-op, on Wayland it will
    // try to use its new presentation extension for swap scheduling and
    // completion timestamping:
    #if (PSYCH_SYSTEM == PSYCH_LINUX) && defined(PTB_USE_WAFFLE)
        PsychOSInitializeOpenML(windowRecord);
    #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;

    // Store current preflip GPU graphics surface addresses, if supported:
    PsychStoreGPUSurfaceAddresses(windowRecord);

    // 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);
}

/* Make sure the lockedflush workaround is applied before we first touch
 * the framebuffer of this brand new onscreen window for real via the
 * glClear() call sequence below. The assumption is that the first access
 * to the drawable will also trigger a X11 roundtrip for fb validation:
 *
 */
void PsychLockedTouchFramebufferIfNeeded(PsychWindowRecordType *windowRecord)
{
    // Is this workaround needed at all to avoid multi-threading corruption on the
    // shared x-display connection?
    // If so, is it needed now? It is needed if there is any chance a parallel background flipper
    // thread is active and executing at this moment, ie. if any of this is true:
    // a) We are executing on a flipper thread, ie., not the master thread.
    // b) Any async flips ops are active on any window.
    // c) Any framesequential stereo flipping threads are active on any window.
    // d) Any custom VRR scheduling threads are active on any window.
    if ((windowRecord->specialflags & kPsychNeedPostSwapLockedFlush) &&
        (!PsychIsMasterThread() || (PsychGetNrAsyncFlipsActive() > 0) || (PsychGetNrFrameSeqStereoWindowsActive() > 0) || (PsychGetNrVRRSchedulerWindowsActive() > 0))
        ) {
        // Workaround needed.

        // Try to wait for double-buffer swap completion in a non-blocking way, if this is supported,
        // e.g., via OpenML OML_sync_control extension. Calling with a (0, NULL) pair will just wait
        // for swap completion in a poll-waiting way without blocking the x-connection much. The
        // function will fall-through and noop if OpenML is unsupported or broken.
        PsychOSGetSwapCompletionTimestamp(windowRecord, 0, NULL);

        if (PsychPrefStateGet_Verbosity() > 15) {
            printf("PTB-DEBUG: PsychLockedTouchFramebufferIfNeeded()! isMaster = %i   AsyncFlips = %i   StereoWindows = %i   VRRSchedulersActive = %i\n",
                PsychIsMasterThread(), PsychGetNrAsyncFlipsActive(), PsychGetNrFrameSeqStereoWindowsActive(), PsychGetNrVRRSchedulerWindowsActive());
            fflush(NULL);
        }

        // Touch the framebuffer for framebuffer revalidation roundtrip to X-Server,
        // with the display lock held, to make future access to this onscreen windows
        // framebuffer thread-safe on XLib:
        #if PSYCH_SYSTEM == PSYCH_LINUX
            PsychLockDisplay();
            PsychWaitPixelSyncToken(windowRecord, TRUE);
            PsychUnlockDisplay();
        #endif
    }
}