File: PsychVulkan.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 (5053 lines) | stat: -rw-r--r-- 251,315 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
/*
 * PsychToolbox/Source/Common/PsychVulkanCore/PsychVulkan.c
 *
 * PROJECTS: PsychVulkanCore only.
 *
 * AUTHORS:
 *
 * mario.kleiner.de@gmail.com   mk
 *
 * PLATFORMS:   Linux, Windows, macOS.
 *
 * DESCRIPTION:
 *
 * A Psychtoolbox driver for interfacing with the Vulkan graphics rendering API
 * for special purpose display and compute tasks.
 *
 * Copyright (c) 2020 - 2024 Mario Kleiner. Licensed under the MIT license:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do so, subject to the
 * following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "PsychVulkan.h"
#include "PsychTimeGlue.h"

#if PSYCH_SYSTEM == PSYCH_LINUX
#define VK_USE_PLATFORM_XLIB_KHR
#define VK_USE_PLATFORM_DISPLAY_KHR
#define VK_USE_PLATFORM_XLIB_XRANDR_EXT
#endif

#if PSYCH_SYSTEM == PSYCH_WINDOWS
// Define for Vulkan WSI on Windows:
#define VK_USE_PLATFORM_WIN32_KHR

// Defines and includes for DXGI v1.6 low-level access and HDR output query:
#define COBJMACROS 1
#include <initguid.h>
#include <dxgi1_6.h>
#endif

#if PSYCH_SYSTEM == PSYCH_OSX
#include <MoltenVK/vk_mvk_moltenvk.h>
#endif

#include <vulkan/vulkan.h>

#ifndef VK_DRIVER_ID_NVIDIA_PROPRIETARY
#define VK_DRIVER_ID_NVIDIA_PROPRIETARY VK_DRIVER_ID_NVIDIA_PROPRIETARY_KHR
#endif

// Defines from Screen, replicated here for simplicity:
#define kPsychLeft      0
#define kPsychTop       1
#define kPsychRight     2
#define kPsychBottom    3
typedef double PsychRectType[4];

// At most 64 swapchain image buffers, but typically only 2 if possible:
#define MAX_BUFFERS 64

// Number of maximum simultaneously open Vulkan devices:
#define MAX_PSYCH_VULKAN_DEVS 100

// Number of maximum simultaneously open Vulkan output windows:
#define MAX_PSYCH_VULKAN_WINDOWS 100

// Short one-line help texts:
#define MAX_SYNOPSIS_STRINGS 50
static const char *synopsisSYNOPSIS[MAX_SYNOPSIS_STRINGS];

// Our Vulkan device record:
typedef struct PsychVulkanDevice {
    psych_bool                          isValid;
    int                                 deviceIndex;
    VkDevice                            device;
    VkQueue                             graphicsQueue;
    VkCommandPool                       commandPool;
    VkPhysicalDevice                    physicalDevice;
    VkPhysicalDeviceProperties          deviceProps;
    VkPhysicalDeviceIDProperties        physDeviceProps;
    VkPhysicalDeviceDriverPropertiesKHR driverProps;
    psych_bool                          hasHDR;
    psych_bool                          hasHDRLocalDimming;
    psych_bool                          hasTiming;
    psych_bool                          hasWait;
    uint32_t                            graphicsQueueFamilyIndex;
    VkPhysicalDeviceMemoryProperties    memoryProperties;
} PsychVulkanDevice;

#if defined(VK_USE_PLATFORM_WIN32_KHR)
typedef struct _ShareHandles {
    HANDLE memory;
    HANDLE glReady;
    HANDLE glComplete;
} ShareHandles;
#else
typedef struct _ShareHandles {
    int memory;
    int glReady;
    int glComplete;
} ShareHandles;
#endif

typedef struct PsychVulkanWindow {
    psych_bool                          isValid;
    int                                 index;
    PsychVulkanDevice*                  vulkan;
    VkDisplayKHR                        display;
    VkSurfaceKHR                        surface;
    VkSurfaceCapabilitiesKHR            surfaceCapabilities;
    psych_bool                          supports_vsync_FIFO;
    psych_bool                          supports_non_vsync;

    psych_bool                          local_dimming_supported;
    unsigned int                        localDimmmingEnable;
    unsigned int                        nativeDisplayHDRMetadataValidity;
    VkHdrMetadataEXT                    nativeDisplayHDRMetadata;
    psych_bool                          currentDisplayHDRMetadataNeedsCommit;
    VkHdrMetadataEXT                    currentDisplayHDRMetadata;

    uint32_t                            surfaceFormatCount;
    VkSurfaceFormat2KHR*                surfaceFormats;

    psych_bool                          supports_hdr10_rgba16f;
    psych_bool                          supports_hdr10_rgb10a2;
    psych_bool                          supports_hdr10_bgr10a2;

    psych_bool                          supports_scrgb_rgba16f;
    psych_bool                          supports_fs2_hdr;

    psych_bool                          supports_srgb_rgba16f;
    psych_bool                          supports_srgb_rgb10a2;
    psych_bool                          supports_srgb_bgr10a2;

    int                                 width;
    int                                 height;
    uint32_t                            numBuffers;
    psych_bool                          isStereo;
    psych_bool                          isFullscreen;
    int                                 createFlags;
    int                                 hdrMode;
    int                                 colorPrecision;
    VkColorSpaceKHR                     colorspace;
    VkFormat                            format;

    VkImage                             interopImage;
    VkFormat                            interopTextureVkFormat;
    psych_bool                          interopTextureTiled;
    VkDeviceMemory                      interopImageMemory;
    ShareHandles                        interopHandles;
    size_t                              interopMemorysize;

    VkSemaphore                         interopRenderDoneSemaphore;

    unsigned int                        frameIndex;
    VkFence                             flipDoneFence;
    double                              tPresentComplete;
    VkSwapchainKHR                      swapChain;
    VkImage*                            swapChainImages;
    uint32_t                            currentSwapChainBuffer;
    VkSemaphore                         imageAcquiredSemaphores[MAX_BUFFERS];
    VkSemaphore                         imageCopyDoneSemaphores[MAX_BUFFERS];
    VkCommandBuffer                     swapChainCommandBuffers[MAX_BUFFERS];

#if defined(VK_USE_PLATFORM_XLIB_KHR)
    Display*                            connection;
    Window                              x11PrivateWindow;
#endif

#if defined(VK_USE_PLATFORM_WIN32_KHR)
    HWND                                win32PrivateWindow;
#endif
} PsychVulkanWindow;

// Connection to our Windowing system:
#if defined(VK_USE_PLATFORM_WIN32_KHR)
static HINSTANCE connection;            // Application instance handle.
static psych_bool windowClassRegistered = FALSE;
#endif

// Main instance, from which everything else derives:
static VkInstance vulkanInstance = VK_NULL_HANDLE;
static VkDebugUtilsMessengerEXT debugMessenger = VK_NULL_HANDLE;

PsychVulkanDevice vulkanDevices[MAX_PSYCH_VULKAN_DEVS];
static uint32_t physicalGpuCount = 0;

PsychVulkanWindow vulkanWindows[MAX_PSYCH_VULKAN_WINDOWS];
static int windowCount = 0;

static int verbosity = 3;
static psych_bool initialized = FALSE;

// Do we require all enumerated gpu's to have a driver with HDR support?
static psych_bool needHDR = FALSE;

static unsigned int preferredDriver = 0; //VK_DRIVER_ID_MESA_RADV_KHR;
static psych_bool isSuitableDriver[4];

// Global count and requested instance extensions:
static uint32_t instanceExtensionsCount = 0;
static unsigned int instanceExtensionsEnabledCount = 0;
static char *instanceExtensionNames[64];

// Global count and requested device extensions for currently processed physical device:
static uint32_t deviceExtensionsCount = 0;
static unsigned int deviceExtensionsEnabledCount = 0;
static char *deviceExtensionNames[64];

static char *layerNames[1];

// Instance extensions:
PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR = NULL;
PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR = NULL; // TODO remove.
PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR = NULL;           // TODO remove.
PFN_vkGetPhysicalDeviceSurfaceFormats2KHR fpGetPhysicalDeviceSurfaceFormats2KHR = NULL;
PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR = NULL; // TODO remove.
PFN_vkGetPhysicalDeviceSurfaceCapabilities2KHR fpGetPhysicalDeviceSurfaceCapabilities2KHR = NULL;

// Device extensions: Could be bound by device instead of globally for the instance, to save a smidgen of
// dispatch overhead from the instance dispatch table to driver-specific entry points. Probably not worth
// it for our use case though:
PFN_vkReleaseDisplayEXT fpReleaseDisplayEXT = NULL;
PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR = NULL;
PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
PFN_vkQueuePresentKHR fpQueuePresentKHR;
PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE = NULL;
PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE = NULL;
#ifdef VK_KHR_present_id
PFN_vkWaitForPresentKHR fpWaitForPresentKHR = NULL;
#else
#if PSYCH_SYSTEM != PSYCH_OSX
    // Only warn on non-macOS, on macOS this is unsupported anyway by MoltenVK Vulkan ICD as of MoltenVK 1.2.8 in March 2024.
    #warning VK_KHR_PRESENT_ID extension not supported by ancient build system. Update the Vulkan SDK headers to at least SDK 1.2.189
#endif
#endif
PFN_vkSetHdrMetadataEXT fpSetHdrMetadataEXT = NULL;
PFN_vkSetLocalDimmingAMD fpSetLocalDimmingAMD = NULL;

#if PSYCH_SYSTEM == PSYCH_WINDOWS
PFN_vkAcquireFullScreenExclusiveModeEXT fpAcquireFullScreenExclusiveModeEXT;
PFN_vkGetMemoryWin32HandleKHR fpGetMemoryWin32HandleKHR;
PFN_vkGetSemaphoreWin32HandleKHR fpGetSemaphoreWin32HandleKHR;
#endif

#if PSYCH_SYSTEM == PSYCH_LINUX
PFN_vkGetMemoryFdKHR fpGetMemoryFdKHR;
PFN_vkGetSemaphoreFdKHR fpGetSemaphoreFdKHR;
PFN_vkGetRandROutputDisplayEXT fpGetRandROutputDisplayEXT = NULL;
PFN_vkAcquireXlibDisplayEXT fpAcquireXlibDisplayEXT = NULL;
#endif

#define GET_INSTANCE_PROC_ADDR(inst, entrypoint)                                                        \
{                                                                                                       \
    fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk" #entrypoint);                \
    if (fp##entrypoint == NULL) {                                                                       \
        printf("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
    }                                                                                                   \
}

static void PsychCloseAllVulkanWindows(void);
static psych_bool PsychSetHDRMetaData(PsychVulkanWindow* window);

void InitializeSynopsis(void)
{
    int i = 0;
    const char **synopsis = synopsisSYNOPSIS;

    synopsis[i++] = "PsychVulkanCore - A Psychtoolbox driver for interfacing with the Vulkan graphics rendering API\n";
    synopsis[i++] = "This driver allows to utilize the Vulkan graphics API for special purpose display and compute tasks.";
    synopsis[i++] = "Copyright (c) 2020 - 2024 Mario Kleiner. Licensed to you under the terms of the MIT license.";
    synopsis[i++] = "";
    synopsis[i++] = "This driver is used internally by Psychtoolbox. You should not call its functions";
    synopsis[i++] = "directly as a regular end-user from your scripts, as the API may change at any time";
    synopsis[i++] = "in a backwards incompatible way without any warning! The driver may even go away";
    synopsis[i++] = "completely in a future version of Psychtoolbox if its functionality gets more tightly";
    synopsis[i++] = "integrated into other mex files!";
    synopsis[i++] = "";
    synopsis[i++] = "Functions used internally by Psychtoolbox:";
    synopsis[i++] = "";
    synopsis[i++] = "oldVerbosity = PsychVulkanCore('Verbosity' [, verbosity]);";
    synopsis[i++] = "numDevices = PsychVulkanCore('GetCount');";
    synopsis[i++] = "devices = PsychVulkanCore('GetDevices');";
    synopsis[i++] = "PsychVulkanCore('Close');";
    synopsis[i++] = "vulkanWindow = PsychVulkanCore('OpenWindow', gpuIndex, targetUUID, isFullscreen, screenId, rect, outputHandle, hdrMode, colorPrecision, refreshHz, colorSpace, colorFormat, flags);";
    synopsis[i++] = "PsychVulkanCore('CloseWindow' [, vulkanWindow]);";
    synopsis[i++] = "hdr = PsychVulkanCore('GetHDRProperties', vulkanWindow);";
    synopsis[i++] = "oldlocalDimmmingEnable = PsychVulkanCore('HDRLocalDimming', vulkanWindow [, localDimmmingEnable]);";
    synopsis[i++] = "[interopObjectHandle, allocationSize, formatSpec, tilingMode, memoryOffset, width, height, interopSemaphoreHandle] = PsychVulkanCore('GetInteropHandle', vulkanWindowHandle [, wantSemaphore=0][, eye=0]);";
    synopsis[i++] = "oldHdrMetadata = PsychVulkanCore('HDRMetadata', vulkanWindow, metadataType [, maxFrameAverageLightLevel][, maxContentLightLevel][, minLuminance][, maxLuminance][, colorGamut]);";
    synopsis[i++] = "[tPredictedOnset, frameIndex] = PsychVulkanCore('Present', vulkanWindowHandle [, tWhen=0][, doTimestamp=2]);";
    synopsis[i++] = NULL; // Mark end of synopsis.

    if (i > MAX_SYNOPSIS_STRINGS) {
        PrintfExit("%s: Increase dimension of synopsis[] from %ld to at least %ld and recompile.", __FILE__, (long) MAX_SYNOPSIS_STRINGS, (long) i);
    }
}

PsychError PsychVulkanDisplaySynopsis(void)
{
    int i;

    for (i = 0; synopsisSYNOPSIS[i] != NULL; i++)
        printf("%s\n", synopsisSYNOPSIS[i]);

    return(PsychError_none);
}

PsychVulkanDevice* PsychGetVulkan(int handle, psych_bool dontfail)
{
    if (handle < 1 || handle > MAX_PSYCH_VULKAN_DEVS || handle > physicalGpuCount || !vulkanDevices[handle - 1].isValid) {
        if (!dontfail) {
            printf("PTB-ERROR: Invalid Vulkan device handle %i passed. No such device open.\n", handle);
            PsychErrorExitMsg(PsychError_user, "Invalid Vulkan handle.");
        }

        return(NULL);
    }

    return(&(vulkanDevices[handle - 1]));
}

PsychVulkanWindow* PsychGetVulkanWindow(int handle, psych_bool dontfail)
{
    if ((handle < 1) || (handle > MAX_PSYCH_VULKAN_WINDOWS) || !vulkanWindows[handle - 1].isValid) {
        if (!dontfail) {
            printf("PTB-ERROR: Invalid Vulkan window handle %i passed. No such window open.\n", handle);
            PsychErrorExitMsg(PsychError_user, "Invalid Vulkan window handle.");
        }

        return(NULL);
    }

    return(&(vulkanWindows[handle - 1]));
}

static VkBool32 PsychVulkanLogCB(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageTypes, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData)
{
    (void) messageTypes;
    (void) pUserData;

    if ((messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT && verbosity > 0) ||
        (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT && verbosity > 1) ||
        (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT && verbosity > 4) ||
        (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT && verbosity > 9)) {
        printf("PsychVulkanCore-Runtime:[%i] %s : %s.\n", pCallbackData->messageIdNumber, pCallbackData->pMessageIdName, pCallbackData->pMessage);
    }

    return(VK_FALSE);
}

static psych_bool addInstanceExtension(VkExtensionProperties* exts, unsigned int extCount, const char* wantedExt)
{
    unsigned int i;

    if (instanceExtensionsEnabledCount >= 64) {
        printf("Instance extension array max capacity of 64 slots reached! Skipping add of '%s'. Recompile!\n", wantedExt);
        return (FALSE);
    }

    for (i = 0; i < extCount; i++) {
        if (!strcmp(wantedExt, exts[i].extensionName)) {
            if (verbosity > 3)
                printf("PsychVulkanCore-INFO: Found and enabled %i. instance extension '%s'.\n", instanceExtensionsEnabledCount, wantedExt);

            instanceExtensionNames[instanceExtensionsEnabledCount++] = (char*) wantedExt;
            return (TRUE);
        }
    }

    if (verbosity > 3)
        printf("PsychVulkanCore-INFO: Extension '%s' is not supported.\n", wantedExt);

    return (FALSE);
}

static psych_bool addDeviceExtension(VkExtensionProperties* exts, unsigned int extCount, const char* wantedExt)
{
    unsigned int i;

    if (deviceExtensionsEnabledCount >= 64) {
        printf("PsychVulkanCore-CRITICAL: Device extension array max capacity of 64 slots reached! Skipping add of '%s'. Recompile!\n", wantedExt);
        return (FALSE);
    }

    for (i = 0; i < extCount; i++) {
        // printf("EXT %i: %s\n", i, exts[i].extensionName);
        if (!strcmp(wantedExt, exts[i].extensionName)) {
            if (verbosity > 3)
                printf("PsychVulkanCore-INFO: Found and enabled %i. device extension '%s'.\n", deviceExtensionsEnabledCount, wantedExt);

            deviceExtensionNames[deviceExtensionsEnabledCount++] = (char*) wantedExt;
            return (TRUE);
        }
    }

    if (verbosity > 3)
        printf("PsychVulkanCore-INFO: Device extension '%s' is not supported.\n", wantedExt);

    return (FALSE);
}

psych_bool checkAndRequestDeviceExtensions(VkPhysicalDevice* gpus, int gpuIndex, psych_bool* hasHDR, psych_bool* hasHDRLocalDimming, psych_bool* hasTiming, psych_bool* hasWait)
{
    int i;
    VkResult result;
    psych_bool rc = FALSE;

    // Reset count of requested device extensions:
    deviceExtensionsEnabledCount = 0;

    // Enumerate device extensions:
    result = vkEnumerateDeviceExtensionProperties(gpus[gpuIndex], NULL, &deviceExtensionsCount, NULL);
    if (result != VK_SUCCESS) {
        if (verbosity > 3)
            printf("PsychVulkanCore-INFO: GPU %i: Skipped, because could not enumerate device extensions (I): %i\n", gpuIndex, result);

        return(FALSE);
    }

    VkExtensionProperties *deviceExtensions = malloc(sizeof(VkExtensionProperties) * deviceExtensionsCount);

    result = vkEnumerateDeviceExtensionProperties(gpus[gpuIndex], NULL, &deviceExtensionsCount, deviceExtensions);
    if (result != VK_SUCCESS) {
        if (verbosity > 3)
            printf("PsychVulkanCore-INFO: GPU %i: Skipped, because could not enumerate device extensions (II): %i\n", gpuIndex, result);

        goto deviceExtensions_out;
    }

    if (verbosity > 4) {
        printf("PsychVulkanCore-INFO: GPU %i: Probing the following %i device extensions against required set.\n", gpuIndex, deviceExtensionsCount);
        for (i = 0; i < deviceExtensionsCount; i++)
            printf("PsychVulkanCore-INFO: %i. device extension is '%s'.\n", i, deviceExtensions[i].extensionName);
    }

    // First request all mandatory extensions we need for any Psychtoolbox use case:
    if (!addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_KHR_SWAPCHAIN_EXTENSION_NAME) ||
        !addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME) ||      // Already in core 1.1
        !addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_KHR_EXTERNAL_SEMAPHORE_EXTENSION_NAME) ||   // Already in core 1.1
        !addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_KHR_MAINTENANCE1_EXTENSION_NAME) ||         // Already in core 1.1

        // For Linux OpenGL <-> Vulkan interop:
        #if PSYCH_SYSTEM == PSYCH_LINUX
        !addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME) ||
        !addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME)
        #endif

        #if PSYCH_SYSTEM == PSYCH_WINDOWS
        // For Windows OpenGL <-> Vulkan interop:
        !addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME) ||
        !addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_KHR_EXTERNAL_SEMAPHORE_WIN32_EXTENSION_NAME) ||
        // For Windows fullscreen display with good performance and timing:
        !addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_EXT_FULL_SCREEN_EXCLUSIVE_EXTENSION_NAME)
        #endif

        #if PSYCH_SYSTEM == PSYCH_OSX
        // Do need the portability subset device extensions on macOS / MoltenVK:
        !addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME)
        #endif
    ) {
        if (verbosity > 3)
            printf("PsychVulkanCore-INFO: GPU %i: Skipped, because at least one mandatory device extension is missing.\n", gpuIndex);

        goto deviceExtensions_out;
    }

    // The HDR metadata extension is a must-have if HDR is requested:
    *hasHDR = addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_EXT_HDR_METADATA_EXTENSION_NAME);

    // The AMD specific extension for native display HDR and local backlight dimming control is optional and only makes
    // sense if we already have basic HDR support:
    if (*hasHDR)
        *hasHDRLocalDimming = addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_AMD_DISPLAY_NATIVE_HDR_EXTENSION_NAME);
    else
        *hasHDRLocalDimming = FALSE;

    if (hasTiming) {
        // The Google display timing extension is a must-have if precise timing is requested:
        *hasTiming = addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME);
    }

    if (hasWait) {
        #ifdef VK_KHR_present_id
        *hasWait = addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_KHR_PRESENT_WAIT_EXTENSION_NAME);
        *hasWait &= addDeviceExtension(deviceExtensions, deviceExtensionsCount, VK_KHR_PRESENT_ID_EXTENSION_NAME);
        #else
        *hasWait = FALSE;
        if (PSYCH_SYSTEM != PSYCH_OSX) {
            static unsigned int oneTimeInfoDone = 0;
            if ((verbosity > 2) && !(oneTimeInfoDone++))
                printf("PsychVulkanCore-INFO: Lacking support for efficient flip-wait. For maximum efficiency, try DownloadPsychtoolbox for upstream PTB.\n");
        }
        #endif
    }

    // Success: Fall through to cleanup with success return code:
    rc = TRUE;

deviceExtensions_out:
    deviceExtensionsCount = 0;
    free(deviceExtensions);

    return(rc);
}

void PsychVulkanCheckInit(psych_bool dontfail)
{
    VkResult result;
    PsychVulkanDevice* vulkan = NULL;

    uint32_t instanceVersion = 0;
    uint32_t probedCount = 0;
    uint32_t graphicsQueueFamilyIndex;
    uint32_t queueFamilyCount;
    int pass;
    int i, j;

    // Already initialized? No op then.
    if (initialized)
        return;

    memset(isSuitableDriver, 0, sizeof(isSuitableDriver));

    // Establish a connection to the host windowing system and get a connection handle:
    #if defined(VK_USE_PLATFORM_WIN32_KHR)
    connection = GetModuleHandle(NULL);
    if (connection == NULL) {
        printf("PsychVulkanCore-ERROR: Failed to get process module handle! Game over!\n");
        goto instance_init_out;
    }
    #endif

    // Enumerate and select all required instance extensions:
    memset(instanceExtensionNames, 0, sizeof(instanceExtensionNames));
    result = vkEnumerateInstanceExtensionProperties(NULL, &instanceExtensionsCount, NULL);
    if ((result != VK_SUCCESS) || (instanceExtensionsCount == 0)) {
        printf("PsychVulkanCore-ERROR: Failed to enumerate instance extensions I!\n");
        goto instance_init_out;
    }

    if (verbosity >= 4)
        printf("PsychVulkanCore-INFO: At startup there are %i Vulkan instance extensions available.\n", instanceExtensionsCount);

    VkExtensionProperties *instanceExtensions = malloc(sizeof(VkExtensionProperties) * instanceExtensionsCount);

    result = vkEnumerateInstanceExtensionProperties(NULL, &instanceExtensionsCount, instanceExtensions);
    if (result != VK_SUCCESS) {
        printf("PsychVulkanCore-ERROR: Failed to enumerate instance extensions II!\n");
        goto instance_init_out;
    }

    if (verbosity >= 5) {
        for (i = 0; i < instanceExtensionsCount; i++)
            printf("PsychVulkanCore-INFO: %i. instance extension is '%s'.\n", i, instanceExtensions[i].extensionName);
    }

    // Check if minimum set of required instance extensions exist and enable/request them, if so:
    if (!addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_KHR_SURFACE_EXTENSION_NAME) ||
        !addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME) ||
        !addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_KHR_EXTERNAL_MEMORY_CAPABILITIES_EXTENSION_NAME) ||       // Already in core 1.1
        !addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_KHR_EXTERNAL_SEMAPHORE_CAPABILITIES_EXTENSION_NAME) ||    // Already in core 1.1
        !addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) ||   // Already in core 1.1
        !addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) ||

        #if defined(VK_USE_PLATFORM_XLIB_KHR)
        !addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_KHR_XLIB_SURFACE_EXTENSION_NAME) ||
        #endif

        #if defined(VK_USE_PLATFORM_XLIB_XRANDR_EXT)
        !addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_EXT_ACQUIRE_XLIB_DISPLAY_EXTENSION_NAME) ||
        #endif

        #if defined(VK_USE_PLATFORM_DISPLAY_KHR)
        !addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_KHR_DISPLAY_EXTENSION_NAME) ||
        !addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_EXT_DIRECT_MODE_DISPLAY_EXTENSION_NAME)
        #endif

        #if defined(VK_USE_PLATFORM_WIN32_KHR)
        !addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_KHR_WIN32_SURFACE_EXTENSION_NAME)
        #endif

        #if PSYCH_SYSTEM == PSYCH_OSX
        !addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_EXT_METAL_SURFACE_EXTENSION_NAME)
        #endif
    ) {
        printf("PsychVulkanCore-ERROR: At least one required instance extension is missing!\n");

        free(instanceExtensions);
        goto instance_init_out;
    }

    // The swapchain color space extension is optional and has no associated entry points, just defines some additional
    // color spaces. Therefore we don't care if getting it succeeds or not:
    addInstanceExtension(instanceExtensions, instanceExtensionsCount, VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME);

    // Done enumerating instance extensions:
    free(instanceExtensions);

    // Create Vulkan instance which is the mother of all to come:
    VkApplicationInfo appInfo = { 0 };
    appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    appInfo.pApplicationName = "Psychtoolbox";
    appInfo.applicationVersion = VK_MAKE_VERSION(PsychGetMajorVersionNumber(), PsychGetMinorVersionNumber(), PsychGetPointVersionNumber());
    appInfo.pEngineName = "PsychVulkanCore";
    appInfo.engineVersion = appInfo.applicationVersion;
    appInfo.apiVersion = VK_API_VERSION_1_1;

    layerNames[0] = "VK_LAYER_KHRONOS_validation";

    VkInstanceCreateInfo createInfo = { 0 };
    createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    createInfo.pApplicationInfo = &appInfo;
    createInfo.enabledExtensionCount = instanceExtensionsEnabledCount;
    createInfo.ppEnabledExtensionNames = (const char *const *) instanceExtensionNames;
    createInfo.enabledLayerCount = (verbosity > 10) ? 1 : 0;
    createInfo.ppEnabledLayerNames = (const char *const *) layerNames;

    // Get Vulkan instance version:
    vkEnumerateInstanceVersion(&instanceVersion);
    if ((VK_VERSION_MAJOR(instanceVersion) == 1) && (VK_VERSION_MINOR(instanceVersion) == 0)) {
        // Only Vulkan 1.0 - Not good enough for us:
        printf("PsychVulkanCore-ERROR: Vulkan instance only supports Vulkan v1.0, not the minimum required v1.1. Game over!\n");
        goto instance_init_out;
    }

    if (vulkanInstance == VK_NULL_HANDLE) {
        result = vkCreateInstance(&createInfo, NULL, &vulkanInstance);
        if (result != VK_SUCCESS) {
            printf("PsychVulkanCore-ERROR: Vulkan instance creation failed: %i\n", result);
            goto instance_init_out;
        }

        if (verbosity >= 3)
            printf("PsychVulkanCore-INFO: Vulkan instance (version %i.%i.%i) created.\n", VK_VERSION_MAJOR(instanceVersion),
                    VK_VERSION_MINOR(instanceVersion), VK_VERSION_PATCH(instanceVersion));
    }

    if (verbosity > 4) {
        PFN_vkCreateDebugUtilsMessengerEXT pfnCreateDebugUtilsMessengerEXT = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(vulkanInstance, "vkCreateDebugUtilsMessengerEXT");

        VkDebugUtilsMessengerCreateInfoEXT messengerCreateInfo = {
            .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,  // sType
            .pNext = NULL,                                                     // pNext
            .flags = 0,                                                        // flags
            .messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | ((verbosity > 10) ? VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT : 0),
            .messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | ((verbosity > 10) ? VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT : 0),
            .pfnUserCallback = PsychVulkanLogCB,
            .pUserData = NULL,
        };

        result = pfnCreateDebugUtilsMessengerEXT(vulkanInstance, &messengerCreateInfo, NULL, &debugMessenger);
        if (result != VK_SUCCESS) {
            printf("PsychVulkanCore-ERROR: Could not enable debug messenger callback: %i\n", result);
            // goto instance_init_out;
        }
    }

    // Bind entry points for functions used from instance extensions:
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetPhysicalDeviceSurfaceSupportKHR);
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetPhysicalDeviceSurfaceCapabilitiesKHR);
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetPhysicalDeviceSurfaceCapabilities2KHR);
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetPhysicalDeviceSurfaceFormatsKHR);
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetPhysicalDeviceSurfaceFormats2KHR);
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetPhysicalDeviceSurfacePresentModesKHR);

    // Bind entry points for functions used from device extensions, but use the global instance
    // entry points and query, for simplicity atm.:

    // Swapchain extension:
    GET_INSTANCE_PROC_ADDR(vulkanInstance, CreateSwapchainKHR);
    GET_INSTANCE_PROC_ADDR(vulkanInstance, DestroySwapchainKHR);
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetSwapchainImagesKHR);
    GET_INSTANCE_PROC_ADDR(vulkanInstance, AcquireNextImageKHR);
    GET_INSTANCE_PROC_ADDR(vulkanInstance, QueuePresentKHR);

    #if PSYCH_SYSTEM == PSYCH_WINDOWS
    // External memory handle extension:
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetMemoryWin32HandleKHR);
    // External semaphore handle extension:
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetSemaphoreWin32HandleKHR);

    // Needed to switch a fullscreen window to fullscreen exclusive mode:
    GET_INSTANCE_PROC_ADDR(vulkanInstance, AcquireFullScreenExclusiveModeEXT);
    #endif

    #if PSYCH_SYSTEM == PSYCH_LINUX
    // External memory fd extension:
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetMemoryFdKHR);
    // External semaphore handle extension:
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetSemaphoreFdKHR);

    // Needed to switch to a fullscreen direct mode display mode:
    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetRandROutputDisplayEXT);
    GET_INSTANCE_PROC_ADDR(vulkanInstance, AcquireXlibDisplayEXT);
    GET_INSTANCE_PROC_ADDR(vulkanInstance, ReleaseDisplayEXT);
    #endif

    // Enumerate physical devices - actually combos of Vulkan driver + physical device:
    result = vkEnumeratePhysicalDevices(vulkanInstance, &probedCount, NULL);
    if (result != VK_SUCCESS) {
        if (verbosity > 0) {
            printf("PsychVulkanCore-ERROR: Could not enumerate physical Vulkan devices (I): %i\n", result);
            printf("PsychVulkanCore-ERROR: Most likely your system does not support Vulkan for your graphics cards.\n");
        }
        goto instance_init_out;
    }

    if (verbosity >= 4)
        printf("PsychVulkanCore-INFO: At startup there are %i Vulkan devices available.\n", probedCount);

    // Enumerate all available gpu+driver combos:
    if (probedCount > 0) {
        int oldVerbosity = verbosity;

        VkPhysicalDevice *physicalDevices = malloc(sizeof(VkPhysicalDevice) * probedCount);
        result = vkEnumeratePhysicalDevices(vulkanInstance, &probedCount, physicalDevices);
        if (result != VK_SUCCESS) {
            free(physicalDevices);
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: Could not enumerate physical Vulkan devices (II): %i\n", result);
            goto instance_init_out;
        }

        for (pass = 0; pass < 2; pass++) {
            verbosity = (pass == 0) ? 1 : oldVerbosity;

            for (i = 0; i < probedCount; i++) {
                psych_bool hasHDR, hasHDRLocalDimming, hasTiming, hasWait;

                // Get physical device properties:
                // Note: These daisy-chained structs might be useful in the future:
                // VkPhysicalDevicePCIBusInfoPropertiesEXT = PCI domain:bus:device:function of the gpu. [VK_EXT_pci_bus_info]

                // VkPhysicalDeviceIDProperties == Identification data (UUID's) for physical GPU [Vulkan 1.1 core]
                VkPhysicalDeviceIDProperties physDeviceProps = {
                    .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES,
                    .pNext = NULL
                };

                VkPhysicalDeviceDriverPropertiesKHR driverprops = {
                    .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR,
                    .pNext = &physDeviceProps,
                    .driverID = 0,
                };

                VkPhysicalDeviceProperties2 pdevprops2 = {
                    .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
                    .pNext = &driverprops,
                };

                vkGetPhysicalDeviceProperties2(physicalDevices[i], &pdevprops2);

                if (verbosity > 3) {
                    printf("PsychVulkanCore-INFO: GPU %i: '%s' : Vulkan %i.%i.%i : VendorId 0x%x : DeviceId 0x%x : ",
                        i, pdevprops2.properties.deviceName,
                        VK_VERSION_MAJOR(pdevprops2.properties.apiVersion), VK_VERSION_MINOR(pdevprops2.properties.apiVersion),
                        VK_VERSION_PATCH(pdevprops2.properties.apiVersion), pdevprops2.properties.vendorID, pdevprops2.properties.deviceID);

                    switch (pdevprops2.properties.deviceType) {
                        case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
                            printf("Integrated graphics chip.\n");
                            break;

                        case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
                            printf("Discrete graphics card.\n");
                            break;

                        case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
                            printf("Virtual graphics card.\n");
                            break;

                        case VK_PHYSICAL_DEVICE_TYPE_CPU:
                            printf("Software renderer.\n");
                            break;

                        case VK_PHYSICAL_DEVICE_TYPE_OTHER:
                        default:
                            printf("Unknown gpu type.\n");
                            break;
                    }

                    // Driver info available? Needs VK_KHR_driver_properties device extension or Vulkan 1.2.
                    if (driverprops.driverID > 0) {
                        printf("PsychVulkanCore-INFO: GPU %i: Driver '%s' [%s] : Driver version 0x%x\n", i, driverprops.driverName,
                               driverprops.driverInfo, pdevprops2.properties.driverVersion);
                    }
                }

                // Skip devices that are unsuitable for us:

                // Only Vulkan 1.0 supported? Reject.
                if ((VK_VERSION_MAJOR(pdevprops2.properties.apiVersion) == 1) && (VK_VERSION_MINOR(pdevprops2.properties.apiVersion) == 0)) {
                    if (verbosity > 3)
                        printf("PsychVulkanCore-INFO: GPU %i: Skipped, because only supports Vulkan v1.0, not the minimum required v1.1.\n", i);
                    continue;
                }

                // Not a integrated or discrete gpu, but a software renderer or virtualized gpu?
                // Not suitable for stimulus display at all (no display connected) or at least not for high performance display
                // with proper timing and precision -> Reject:
                if ((pdevprops2.properties.deviceType != VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU) &&
                    (pdevprops2.properties.deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)) {
                    if (verbosity > 3)
                        printf("PsychVulkanCore-INFO: GPU %i: Skipped, because not a suitable standard discrete or integrated gpu.\n", i);
                    continue;
                }

                // Check if minimum set of required device extensions is available on this gpu. This will also check if
                // HDR extensions and timing extensions are available, report back in hasHDR and hasTiming if they are
                // supported, and enable/request them if they are supported:
                if (!checkAndRequestDeviceExtensions(physicalDevices, i, &hasHDR, &hasHDRLocalDimming, &hasTiming, &hasWait))
                    continue;

                if (needHDR && !hasHDR) {
                    if (verbosity > 3)
                        printf("PsychVulkanCore-INFO: GPU %i: Skipped, because does not support HDR as required upon user-code request.\n", i);
                    continue;
                }

                // Query queue family properties for the GPU:
                vkGetPhysicalDeviceQueueFamilyProperties(physicalDevices[i], &queueFamilyCount, NULL);
                VkQueueFamilyProperties* queueProps = (VkQueueFamilyProperties*) malloc(queueFamilyCount * sizeof(VkQueueFamilyProperties));
                vkGetPhysicalDeviceQueueFamilyProperties(physicalDevices[i], &queueFamilyCount, queueProps);

                // Try to find a queue family with graphics queues:
                graphicsQueueFamilyIndex = 0xffffffff;
                for (j = 0; j < queueFamilyCount; j++) {
                    if (queueProps[j].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
                        graphicsQueueFamilyIndex = j;
                        break;
                    }
                }

                free(queueProps);

                if (graphicsQueueFamilyIndex == 0xffffffff) {
                    if (verbosity > 3)
                        printf("PsychVulkanCore-INFO: GPU %i: Skipped, because does not support graphics queue family.\n", i);
                    continue;
                }

                // Ok, this is a suitable driver + gpu combo. Any user preferences if multiple suitable drivers exist for the same gpu vendor?
                if (driverprops.driverID > 0) {
                    // We can identify the kind of driver. Let's see if it matches set preferences:
                    switch (driverprops.driverID) {
                        case VK_DRIVER_ID_AMD_PROPRIETARY_KHR:
                        case VK_DRIVER_ID_AMD_OPEN_SOURCE_KHR:
                        case VK_DRIVER_ID_MESA_RADV_KHR:
                            // Driver for an AMD gpu. User choice decides:
                            if (pass == 0) {
                                // Mark this driver as suitable for the task at hand:
                                isSuitableDriver[driverprops.driverID] = TRUE;
                                // Next enumerated gpu + driver combo...
                                continue;
                            } else {
                                // Pass 2, we know already which isSuitableDriver's are available.
                                if ((preferredDriver > 0) && isSuitableDriver[preferredDriver] && (driverprops.driverID != preferredDriver)) {
                                    // A preferredDriver is specified by user code and the preferredDriver is suitable and this one
                                    // is not it. Therefore skip this one.
                                    if (verbosity > 3)
                                        printf("PsychVulkanCore-INFO: GPU %i: Skipped, because '%s' is not the user-code preferred driver.\n", i, driverprops.driverName);

                                    continue;
                                }
                            }
                            break;

                        default:
                            // Non AMD gpu. There is only one driver for a gpu, so just skip forward to pass 1:
                            if (pass == 0)
                                continue;
                    }
                } else {
                    // Skip forward to pass 1 if we don't have driver preferences handling:
                    if (pass == 0)
                        continue;
                }

                if (pass == 0)
                    printf("IMPLEMENTATION BUG: Reached code in pass 0 which should only be reachable in pass 1 !!!\n");

                // Basic HDR support:
                if (hasHDR && !fpSetHdrMetadataEXT)
                    GET_INSTANCE_PROC_ADDR(vulkanInstance, SetHdrMetadataEXT);

                // AMD specific local backlight dimming control suppport on FreeSync2:
                if (hasHDRLocalDimming && !fpSetLocalDimmingAMD)
                    GET_INSTANCE_PROC_ADDR(vulkanInstance, SetLocalDimmingAMD);

                // Enhanced presentation timing support via VK_GOOGLE_DISPLAY_TIMING extension:
                if (hasTiming && (!fpGetRefreshCycleDurationGOOGLE || !fpGetPastPresentationTimingGOOGLE)) {
                    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetRefreshCycleDurationGOOGLE);
                    GET_INSTANCE_PROC_ADDR(vulkanInstance, GetPastPresentationTimingGOOGLE);
                }

                #ifdef VK_KHR_present_id
                // Ability to wait for present completion via VK_KHR_present_wait extension:
                if (hasWait && !fpWaitForPresentKHR)
                    GET_INSTANCE_PROC_ADDR(vulkanInstance, WaitForPresentKHR);
                #endif

                // Add a record about this GPU's basic properties:
                vulkan = &(vulkanDevices[physicalGpuCount]);
                vulkan->deviceIndex = physicalGpuCount + 1;
                vulkan->physicalDevice = physicalDevices[i];
                vulkan->deviceProps = pdevprops2.properties;
                vulkan->physDeviceProps = physDeviceProps;
                vulkan->driverProps = driverprops;
                vulkan->hasHDR = hasHDR;
                vulkan->hasHDRLocalDimming = hasHDRLocalDimming;
                vulkan->hasTiming = hasTiming;
                vulkan->hasWait = hasWait;
                vulkan->graphicsQueueFamilyIndex = graphicsQueueFamilyIndex;

                // Create logical vulkan device -- the "gpu" for our physical device:
                {
                    float priority = 1.0;
                    VkDeviceQueueCreateInfo queueCreateInfo = {
                        .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
                        .pNext = NULL,
                        .queueFamilyIndex = vulkan->graphicsQueueFamilyIndex,
                        .queueCount = 1,
                        .pQueuePriorities = &priority,
                        .flags = 0,
                    };

                    #ifdef VK_KHR_present_id
                    VkPhysicalDevicePresentIdFeaturesKHR presentIdFeatureEnable = {
                        .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_ID_FEATURES_KHR,
                        .pNext = NULL,
                        .presentId = VK_TRUE,
                    };

                    VkPhysicalDevicePresentWaitFeaturesKHR waitFeatureEnable = {
                        .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_WAIT_FEATURES_KHR,
                        .pNext = &presentIdFeatureEnable,
                        .presentWait = VK_TRUE,
                    };
                    #else
                    void *waitFeatureEnable = NULL;
                    #endif

                    VkDeviceCreateInfo deviceCreateInfo = {
                        .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
                        .pNext = (hasWait) ? &waitFeatureEnable : NULL,             // TODO: Add support for VK_EXT_global_priority ?
                        .queueCreateInfoCount = 1,
                        .pQueueCreateInfos = &queueCreateInfo,
                        .enabledLayerCount = (verbosity > 10) ? 1 : 0,              // Deprecated since v1.1, ignored.
                        .ppEnabledLayerNames = (const char *const *) layerNames,    // Deprecated since v1.1, ignored.
                        .enabledExtensionCount = deviceExtensionsEnabledCount,
                        .ppEnabledExtensionNames = (const char *const *) deviceExtensionNames,
                        .pEnabledFeatures = NULL,
                    };

                    result = vkCreateDevice(physicalDevices[i], &deviceCreateInfo, NULL, &vulkan->device);
                    if (result != VK_SUCCESS) {
                        if (verbosity > 3)
                            printf("PsychVulkanCore-INFO: GPU %i: Skipped, because vkCreateDevice() failed: %i\n", i, result);

                        continue;
                    }

                    // Bind device extensions:
                    // TODO: For the time being we bind the global instance entry points. I doubt this has any
                    // measureable performance impact for our use case, so using device specific binding may be
                    // overkill.

                    // Get the command queue we will use:
                    vkGetDeviceQueue(vulkan->device, vulkan->graphicsQueueFamilyIndex, 0, &vulkan->graphicsQueue);

                    // Create command buffer pool:
                    VkCommandPoolCreateInfo poolInfo = {
                        .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
                        .pNext = NULL,
                        .queueFamilyIndex = vulkan->graphicsQueueFamilyIndex,
                        .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
                    };

                    result = vkCreateCommandPool(vulkan->device, &poolInfo, NULL, &vulkan->commandPool);
                    if (result != VK_SUCCESS) {
                        if (verbosity > 3)
                            printf("PsychVulkanCore-INFO: GPU %i: Skipped, because vkCreateCommandPool() failed: %i\n", i, result);

                        vkDestroyDevice(vulkan->device, NULL);

                        continue;
                    }

                }

                // Get Memory information and properties
                vkGetPhysicalDeviceMemoryProperties(vulkan->physicalDevice, &vulkan->memoryProperties);

                // Make it valid:
                vulkan->isValid = TRUE;

                // Commit this new Vulkan device:
                physicalGpuCount++;

                // If we made it to here then this gpu i must be added to the list of selectable gpus:
                if (verbosity > 3)
                    printf("PsychVulkanCore-INFO: GPU %i: ACCEPTED AND ACTIVATED! Count now %i\n", i, physicalGpuCount);

                if (physicalGpuCount >= MAX_PSYCH_VULKAN_DEVS) {
                    printf("PsychVulkanCore-ERROR: Reached maximum supported count %i of simultaneously open Vulkan devices! Aborting enumeration.\n", MAX_PSYCH_VULKAN_DEVS);
                    break;
                }
            }
        }
    }

    initialized = TRUE;

    return;

instance_init_out:

    deviceExtensionsEnabledCount = 0;
    instanceExtensionsEnabledCount = 0;

    if (!dontfail) {
        PsychErrorExitMsg(PsychError_system, "PsychVulkanCore-ERROR: Initialization of Vulkan API failed. Driver disabled!");
    }
}

// "Does nothing, goes nowhere" so far...
void PsychVulkanClose(int handle)
{
    PsychVulkanDevice* vulkan;
    vulkan = PsychGetVulkan(handle, TRUE);
    if (NULL == vulkan)
        return;
}

void PsychVulkanInit(void) {
    int handle;

    memset(&vulkanDevices, 0, sizeof(vulkanDevices));
    physicalGpuCount = 0;

    memset(&vulkanWindows, 0, sizeof(vulkanWindows));
    windowCount = 0;

    vulkanInstance = VK_NULL_HANDLE;

    initialized = FALSE;
}

PsychError PsychVulkanShutDown(void) {
    int handle, i;

    if (initialized) {
        // Close all open Vulkan windows and release their associated resources:
        PsychCloseAllVulkanWindows();

        // TODO FIXME
        // for (handle = 0; handle < MAX_PSYCH_VULKAN_DEVS; handle++)
        //    PsychVulkanClose(handle);

        // Destroy all Vulkan devices:
        for (i = 0; i < physicalGpuCount; i++) {
            vkDeviceWaitIdle(vulkanDevices[i].device);
            vkDestroyCommandPool(vulkanDevices[i].device, vulkanDevices[i].commandPool, NULL);
            vkDestroyDevice(vulkanDevices[i].device, NULL);
        }

        physicalGpuCount = 0;
        windowCount = 0;
        instanceExtensionsEnabledCount = 0;

        // Disable error callback:
        PFN_vkDestroyDebugUtilsMessengerEXT pfnDestroyDebugUtilsMessengerEXT = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(vulkanInstance, "vkDestroyDebugUtilsMessengerEXT");
        if (debugMessenger != VK_NULL_HANDLE)
            pfnDestroyDebugUtilsMessengerEXT(vulkanInstance, debugMessenger, NULL);
        debugMessenger = VK_NULL_HANDLE;

        // Destroy our Vulkan instance:
        vkDestroyInstance(vulkanInstance, NULL);
        vulkanInstance = VK_NULL_HANDLE;

        if (verbosity >= 4)
            printf("PsychVulkanCore-INFO: Vulkan instance destruction and driver shutdown complete.\n");

        // Destroy our Windowing system connection:
        #if defined(VK_USE_PLATFORM_WIN32_KHR)
            if (windowClassRegistered) {
                UnregisterClass("PTB-Vulkan", connection);
                windowClassRegistered = FALSE;
            }

            connection = 0;
        #endif

        initialized = FALSE;
    }

    return(PsychError_none);
}

#if PSYCH_SYSTEM == PSYCH_WINDOWS
VkSurfaceFullScreenExclusiveWin32InfoEXT fullscreenExclusiveInfoWin32 = {
    .sType = VK_STRUCTURE_TYPE_SURFACE_FULL_SCREEN_EXCLUSIVE_WIN32_INFO_EXT,
    .pNext = NULL,
};

VkSurfaceFullScreenExclusiveInfoEXT fullscreenExclusiveInfo = {
    .sType = VK_STRUCTURE_TYPE_SURFACE_FULL_SCREEN_EXCLUSIVE_INFO_EXT,
    .pNext = &fullscreenExclusiveInfoWin32,
    .fullScreenExclusive = VK_FULL_SCREEN_EXCLUSIVE_APPLICATION_CONTROLLED_EXT,
};

void PsychInitFullScreenExlusiveStructs(PsychVulkanWindow* window)
{
    fullscreenExclusiveInfoWin32.hmonitor = MonitorFromWindow(window->win32PrivateWindow, MONITOR_DEFAULTTOPRIMARY);
    if (window->isFullscreen && !(window->createFlags & 0x2))
        fullscreenExclusiveInfo.fullScreenExclusive = VK_FULL_SCREEN_EXCLUSIVE_APPLICATION_CONTROLLED_EXT;
    else
        fullscreenExclusiveInfo.fullScreenExclusive = VK_FULL_SCREEN_EXCLUSIVE_DISALLOWED_EXT;
}

void PsychMSDXGIQueryOutputHDR(PsychVulkanWindow* window, PsychVulkanDevice* vulkan)
{
    // Could not query HDR display properties via Vulkan on Windows. Try to
    // use DXGI v1.6 on Windows-10 instead:
    IDXGIFactory4 *dxgiFactory = NULL;

    // Try to create DXGI factory:
    if (S_OK != CreateDXGIFactory2(0, &IID_IDXGIFactory4, (void**) &dxgiFactory)) {
        if (verbosity > 1)
            printf("PsychVulkanCore-WARNING: Failed to create a DXGI factory for HDR display properties fallback query!\n");
    }
    else {
        DXGI_ADAPTER_DESC1 desc1;
        IDXGIAdapter1 *dxgiAdapter = NULL;
        unsigned int index = 0;

        // Enumerate all DXGI adapters, check for matching one for our Vulkan/OpenGL gpu:
        while ((S_OK == IDXGIFactory4_EnumAdapters1(dxgiFactory, index, &dxgiAdapter)) && (S_OK == IDXGIAdapter1_GetDesc1(dxgiAdapter, &desc1))) {
            if (verbosity > 4)
                printf("PsychVulkanCore-DEBUG: Name of DXGI adapter %i is '%S'.\n", index, (wchar_t*) desc1.Description);

            // LUID valid and match between Vulkan and DXGI?
            if (vulkan->physDeviceProps.deviceLUIDValid && !memcmp(&desc1.AdapterLuid, &vulkan->physDeviceProps.deviceLUID[0], sizeof(desc1.AdapterLuid)))
                break;

            // Wrong DXGI gpu, not the same as Vulkan/OpenGL gpu:
            IDXGIAdapter1_Release(dxgiAdapter);
            dxgiAdapter = NULL;
            index++;
        }

        if (dxgiAdapter) {
            DXGI_OUTPUT_DESC outDesc;
            DXGI_OUTPUT_DESC1 outDesc1;
            IDXGIOutput *dxgiOutput = NULL;
            index = 0;

            while ((S_OK == IDXGIAdapter1_EnumOutputs(dxgiAdapter, index, &dxgiOutput)) && (S_OK == IDXGIOutput_GetDesc(dxgiOutput, &outDesc))) {
                if (outDesc.Monitor == fullscreenExclusiveInfoWin32.hmonitor) {
                    break;
                }

                IDXGIOutput_Release(dxgiOutput);
                dxgiOutput = NULL;
                index++;
            }

            // Found proper DXGI output? Able to query its HDR properties?
            if (dxgiOutput) {
                if (S_OK == IDXGIOutput6_GetDesc1((IDXGIOutput6*) dxgiOutput, &outDesc1)) {
                    if (verbosity > 3)
                        printf("PsychVulkanCore-DEBUG: Getting HDR properties via DXGI from output %i '%S':\n", index, outDesc1.DeviceName);

                    window->nativeDisplayHDRMetadata.displayPrimaryRed.x = outDesc1.RedPrimary[0];
                    window->nativeDisplayHDRMetadata.displayPrimaryRed.y = outDesc1.RedPrimary[1];

                    window->nativeDisplayHDRMetadata.displayPrimaryGreen.x = outDesc1.GreenPrimary[0];
                    window->nativeDisplayHDRMetadata.displayPrimaryGreen.y = outDesc1.GreenPrimary[1];

                    window->nativeDisplayHDRMetadata.displayPrimaryBlue.x = outDesc1.BluePrimary[0];
                    window->nativeDisplayHDRMetadata.displayPrimaryBlue.y = outDesc1.BluePrimary[1];

                    window->nativeDisplayHDRMetadata.whitePoint.x = outDesc1.WhitePoint[0];
                    window->nativeDisplayHDRMetadata.whitePoint.y = outDesc1.WhitePoint[1];

                    window->nativeDisplayHDRMetadata.minLuminance = outDesc1.MinLuminance;
                    window->nativeDisplayHDRMetadata.maxLuminance = outDesc1.MaxLuminance;
                    window->nativeDisplayHDRMetadata.maxFrameAverageLightLevel = outDesc1.MaxFullFrameLuminance;
                    window->nativeDisplayHDRMetadata.maxContentLightLevel = 0.0;

                    // Mark HDR data as valid:
                    window->nativeDisplayHDRMetadataValidity = 1;
                }

                IDXGIOutput_Release(dxgiOutput);
                dxgiOutput = NULL;
            }

            IDXGIAdapter1_Release(dxgiAdapter);
            dxgiAdapter = NULL;
        }

        IDXGIFactory4_Release(dxgiFactory);
        dxgiFactory = NULL;
    }
}
#else
void PsychInitFullScreenExlusiveStructs(PsychVulkanWindow* window) {}
#endif

psych_bool PsychIsColorSpaceFormatComboSupported(PsychVulkanWindow* window, VkColorSpaceKHR space, VkFormat format)
{
    int i;

    for (i = 0; i < window->surfaceFormatCount; i++) {
        if ((window->surfaceFormats[i].surfaceFormat.colorSpace == space) &&
            (window->surfaceFormats[i].surfaceFormat.format == format))
            return(TRUE);
    }

    return(FALSE);
}

psych_bool PsychProbeSurfaceProperties(PsychVulkanWindow* window, PsychVulkanDevice* vulkan)
{
    VkResult result;
    int i;

    // Clean up / Reset from previous call if needed:
    if (window->surfaceFormats) {
        free(window->surfaceFormats);
        window->surfaceFormats = NULL;
        window->surfaceFormatCount = 0;
    }

    window->supports_hdr10_rgba16f = FALSE;
    window->supports_hdr10_rgb10a2 = FALSE;
    window->supports_hdr10_bgr10a2 = FALSE;
    window->supports_scrgb_rgba16f = FALSE;
    window->supports_fs2_hdr = FALSE;
    window->supports_srgb_rgba16f = FALSE;
    window->supports_srgb_rgb10a2 = FALSE;
    window->supports_srgb_bgr10a2 = FALSE;

    memset(&window->nativeDisplayHDRMetadata, 0, sizeof(window->nativeDisplayHDRMetadata));
    window->nativeDisplayHDRMetadata.sType = VK_STRUCTURE_TYPE_HDR_METADATA_EXT;

    memset(&window->surfaceCapabilities, 0, sizeof(window->surfaceCapabilities));
    window->local_dimming_supported = FALSE;

    window->supports_vsync_FIFO = FALSE;
    window->supports_non_vsync = FALSE;

    const VkPhysicalDeviceSurfaceInfo2KHR surfaceInfo = {
        .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR,
        .surface = window->surface,
        #if PSYCH_SYSTEM == PSYCH_WINDOWS
        .pNext = (window->isFullscreen && !(window->createFlags & 0x2)) ? &fullscreenExclusiveInfo : NULL,
        #endif
    };

    VkDisplayNativeHdrSurfaceCapabilitiesAMD nativeHDRCapabilitiesAMD = {
        .sType = VK_STRUCTURE_TYPE_DISPLAY_NATIVE_HDR_SURFACE_CAPABILITIES_AMD,
        .localDimmingSupport = VK_FALSE,
        // amdvlk as of 2020-Q2-4 on Linux crashes in windowed mode if we set this non-NULL, because of a driver bug, so be careful to not provoke this:
        // Note: This has been fixed as of amdvlk 2020-Q2-6, so no crash anymore in windowed mode, but returns some hard-coded values instead of
        // real monitor data, so enabling it again is currently of little additional value.
        // Also skip for non-AMD on MS-Windows, otherwise we get validation warnings.
        .pNext = (((PSYCH_SYSTEM == PSYCH_WINDOWS) && (vulkan->driverProps.driverID == VK_DRIVER_ID_AMD_PROPRIETARY_KHR)) || (window->display != VK_NULL_HANDLE)) ? &window->nativeDisplayHDRMetadata : NULL,
    };

    VkSurfaceCapabilities2KHR surfaceCapabilities2 = {
        .sType = VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR,
        .pNext = &nativeHDRCapabilitiesAMD,
    };

    result = fpGetPhysicalDeviceSurfaceCapabilities2KHR(vulkan->physicalDevice, &surfaceInfo, &surfaceCapabilities2);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: Failed to get vkGetPhysicalDeviceSurfaceCapabilities2KHR: %i\n", result);

        return(FALSE);
    }

    window->local_dimming_supported = nativeHDRCapabilitiesAMD.localDimmingSupport;
    window->surfaceCapabilities = surfaceCapabilities2.surfaceCapabilities;

    // Check validity of queried display HDR properties:
    {
        window->nativeDisplayHDRMetadataValidity = 0;
        psych_uint8* bp = (psych_uint8*) &window->nativeDisplayHDRMetadata;

        for (i = 0; i < sizeof(window->nativeDisplayHDRMetadata); i++) {
            window->nativeDisplayHDRMetadataValidity += (unsigned int) *(bp++);
        }

        // Greater 355 sum over struct suggests at least some valid data was returned, so
        // mark it as valid (==1). Value 355 is what we would get with a content-free all
        // zero struct, where only the .sType is defined as VK_STRUCTURE_TYPE_HDR_METADATA_EXT:
        window->nativeDisplayHDRMetadataValidity = (window->nativeDisplayHDRMetadataValidity > 355) ? 1 : 0;

        // Missing HDR data, because Vulkan driver didn't provide any? Or buggy AMD Vulkan proprietary driver on MS-Windows?
        if (!window->nativeDisplayHDRMetadataValidity ||
            ((window->nativeDisplayHDRMetadata.maxFrameAverageLightLevel == 0) && (vulkan->driverProps.driverID == VK_DRIVER_ID_AMD_PROPRIETARY_KHR))) {
            // Try OS specific fallback to get (better - less buggy) display HDR properties:
            #if PSYCH_SYSTEM == PSYCH_WINDOWS
                PsychMSDXGIQueryOutputHDR(window, vulkan);
            #endif
        }

        // Invalid / Absurd white-point chromaticity coordinates way lower than around 0.3 for D65?
        if (window->nativeDisplayHDRMetadataValidity &&
            (window->nativeDisplayHDRMetadata.whitePoint.x < 0.15) && (window->nativeDisplayHDRMetadata.whitePoint.y < 0.15)) {
            // At least AMD written Linux drivers (ie. amdgpu-pro and amdvlk as of amdvlk 2020-Q2.6 (driverVersion 8388758 aka VK_MAKE_VERSION(2,0,150)))
            // have broken HDR color gamut reporting again. This time all reported chromaticity coordinates are too small by a factor of 5x.
            // Work around this by multiplying all coordinates by a factor of 5.0. This has been fixed in driverVersion 8388763, v2.0.155 aka 2020-Q3.4:
            if ((PSYCH_SYSTEM == PSYCH_LINUX) && (vulkan->deviceProps.driverVersion < 8388763) &&
                (vulkan->driverProps.driverID == VK_DRIVER_ID_AMD_OPEN_SOURCE_KHR || vulkan->driverProps.driverID == VK_DRIVER_ID_AMD_PROPRIETARY_KHR)) {
                window->nativeDisplayHDRMetadata.displayPrimaryRed.x *= 5.0;
                window->nativeDisplayHDRMetadata.displayPrimaryRed.y *= 5.0;

                window->nativeDisplayHDRMetadata.displayPrimaryGreen.x *= 5.0;
                window->nativeDisplayHDRMetadata.displayPrimaryGreen.y *= 5.0;

                window->nativeDisplayHDRMetadata.displayPrimaryBlue.x *= 5.0;
                window->nativeDisplayHDRMetadata.displayPrimaryBlue.y *= 5.0;

                window->nativeDisplayHDRMetadata.whitePoint.x *= 5.0;
                window->nativeDisplayHDRMetadata.whitePoint.y *= 5.0;

                if (verbosity > 1)
                    printf("PsychVulkanCore-WARNING: Buggy AMD Vulkan driver reports wrong (too small) color gamut. Hopefully fixing this by scaling up with a factor of 5.0x.\n");
            }
        }
    }

    // Initialize currentDisplayHDRMetadata, ie. what we would set when switching to HDR mode or
    // changing HDR info packets, to the displays nativeDisplayHDRMetadata, as a good startup
    // default, except for the content light levels which we set to zero, which signals to the
    // display that they are unknown. This because during dynamic stimulus rendering we really
    // do not know in advance what those maximum values over a whole session will be. Usercode
    // can always override for known content:
    window->currentDisplayHDRMetadata = window->nativeDisplayHDRMetadata;
    window->currentDisplayHDRMetadata.maxFrameAverageLightLevel = 0;
    window->currentDisplayHDRMetadata.maxContentLightLevel = 0;

    if (verbosity > 3) {
        printf("Surface minImageCount %i - maxImageCount %i\n", window->surfaceCapabilities.minImageCount, window->surfaceCapabilities.maxImageCount);
        printf("Surface currentExtent %i x %i pixels with up to %i view layers\n", window->surfaceCapabilities.currentExtent.width, window->surfaceCapabilities.currentExtent.height, window->surfaceCapabilities.maxImageArrayLayers);
        printf("Display native HDR properties as queried from monitor:\n");
        printf("Display Supports control of HDR local dimming: %s\n", nativeHDRCapabilitiesAMD.localDimmingSupport ? "Yes" : "No");
        printf("Display Gamut  R: [%f, %f]\n", window->nativeDisplayHDRMetadata.displayPrimaryRed.x, window->nativeDisplayHDRMetadata.displayPrimaryRed.y);
        printf("Display Gamut  G: [%f, %f]\n", window->nativeDisplayHDRMetadata.displayPrimaryGreen.x, window->nativeDisplayHDRMetadata.displayPrimaryGreen.y);
        printf("Display Gamut  B: [%f, %f]\n", window->nativeDisplayHDRMetadata.displayPrimaryBlue.x, window->nativeDisplayHDRMetadata.displayPrimaryBlue.y);
        printf("Display Gamut WP: [%f, %f]\n", window->nativeDisplayHDRMetadata.whitePoint.x, window->nativeDisplayHDRMetadata.whitePoint.y);
        printf("Display minLuminance: %f nits\n", window->nativeDisplayHDRMetadata.minLuminance);
        printf("Display maxLuminance: %f nits\n", window->nativeDisplayHDRMetadata.maxLuminance);
        printf("Content maxFrameAverageLightLevel: %f nits\n", window->nativeDisplayHDRMetadata.maxFrameAverageLightLevel);
        printf("Content maxContentLightLevel: %f nits\n", window->nativeDisplayHDRMetadata.maxContentLightLevel);
    }

    // Get the list of VkFormat's that are supported:
    result = fpGetPhysicalDeviceSurfaceFormats2KHR(vulkan->physicalDevice, &surfaceInfo, &window->surfaceFormatCount, NULL);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: Failed to vkGetPhysicalDeviceSurfaceFormats2KHR: %i\n", result);

        return(FALSE);
    }

    window->surfaceFormats = (VkSurfaceFormat2KHR*) malloc(window->surfaceFormatCount * sizeof(VkSurfaceFormat2KHR));
    for (i = 0; i < window->surfaceFormatCount; i++) {
        window->surfaceFormats[i].sType = VK_STRUCTURE_TYPE_SURFACE_FORMAT_2_KHR;
        window->surfaceFormats[i].pNext = NULL;
    }

    result = fpGetPhysicalDeviceSurfaceFormats2KHR(vulkan->physicalDevice, &surfaceInfo, &window->surfaceFormatCount, window->surfaceFormats);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: Failed to vkGetPhysicalDeviceSurfaceFormats2KHR II for %i formats: %i\n", window->surfaceFormatCount, result);

        free(window->surfaceFormats);
        window->surfaceFormats = NULL;
        window->surfaceFormatCount = 0;
        return(FALSE);
    }

    if (verbosity > 3)
        printf("Surface number of supported surface colorspace + pixelformat combinations: %i\n", window->surfaceFormatCount);

    // Use fallback format BGRA8 unorm if that is all that is supported:
    if (window->surfaceFormatCount == 1 && window->surfaceFormats[0].surfaceFormat.format == VK_FORMAT_UNDEFINED) {
        window->surfaceFormats[0].surfaceFormat.format = VK_FORMAT_B8G8R8A8_UNORM;
    }
    else {
        // More colorspace + format combos available. Enumerate:

        // Try to get RGBA16F float, then RGB10A2 formats as second choice, then
        // a fallback to default RGBA8:
        for (i = 0; i < window->surfaceFormatCount; i++) {
            if (verbosity > 3) {
                if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_DOLBYVISION_EXT)
                    printf("[%i] For colorspace VK_COLOR_SPACE_DOLBYVISION_EXT          - ", i);

                if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT)
                    printf("[%i] For colorspace VK_COLOR_SPACE_HDR10_ST2084_EXT         - ", i);

                if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_HDR10_HLG_EXT)
                    printf("[%i] For colorspace VK_COLOR_SPACE_HDR10_HLG_EXT            - ", i);

                if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_BT2020_LINEAR_EXT)
                    printf("[%i] For colorspace VK_COLOR_SPACE_BT2020_LINEAR_EXT        - ", i);

                if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_DISPLAY_NATIVE_AMD)
                    printf("[%i] For colorspace VK_COLOR_SPACE_DISPLAY_NATIVE_AMD       - ", i);

                if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_PASS_THROUGH_EXT)
                    printf("[%i] For colorspace VK_COLOR_SPACE_PASS_THROUGH_EXT         - ", i);

                if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT)
                    printf("[%i] For colorspace VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT - ", i);

                if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_BT709_NONLINEAR_EXT)
                    printf("[%i] For colorspace VK_COLOR_SPACE_BT709_NONLINEAR_EXT      - ", i);

                if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
                    printf("[%i] For colorspace VK_COLOR_SPACE_SRGB_NONLINEAR_KHR       - ", i);

                switch (window->surfaceFormats[i].surfaceFormat.format) {
                    case VK_FORMAT_R16G16B16A16_SFLOAT:
                        printf("[%i] Swapchain format VK_FORMAT_R16G16B16A16_SFLOAT\n", i);
                        break;

                    case VK_FORMAT_R16G16B16A16_UNORM:
                        printf("[%i] Swapchain format VK_FORMAT_R16G16B16A16_UNORM\n", i);
                        break;

                    case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
                        printf("[%i] Swapchain format VK_FORMAT_A2R10G10B10_UNORM_PACK32\n", i);
                        break;

                    case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
                        printf("[%i] Swapchain format VK_FORMAT_A2B10G10R10_UNORM_PACK32\n", i);
                        break;

                    case VK_FORMAT_B8G8R8A8_SRGB:
                        printf("[%i] Swapchain format VK_FORMAT_B8G8R8A8_SRGB\n", i);
                        break;

                    case VK_FORMAT_B8G8R8A8_UNORM:
                        printf("[%i] Swapchain format VK_FORMAT_B8G8R8A8_UNORM\n", i);
                        break;

                    case VK_FORMAT_R8G8B8A8_UNORM:
                        printf("[%i] Swapchain format VK_FORMAT_R8G8B8A8_UNORM\n", i);
                        break;

                    case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
                        printf("[%i] Swapchain format VK_FORMAT_A8B8G8R8_SRGB_PACK32\n", i);
                        break;

                    case VK_FORMAT_A8B8G8R8_UNORM_PACK32:
                        printf("[%i] Swapchain format VK_FORMAT_A8B8G8R8_UNORM_PACK32\n", i);
                        break;

                    default:
                        printf("[%i] Swapchain format unknown %d\n", i, window->surfaceFormats[i].surfaceFormat.format);
                }
            }

            // Standard HDR10 BT2020 colorspace with ST2084 PQ "Perceptual Quantizer" OETF supported?
            if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_HDR10_ST2084_EXT) {
                // Yes. Which pixel format?
                if (window->surfaceFormats[i].surfaceFormat.format == VK_FORMAT_R16G16B16A16_SFLOAT) {
                    window->supports_hdr10_rgba16f = TRUE;
                }

                if (window->surfaceFormats[i].surfaceFormat.format == VK_FORMAT_A2B10G10R10_UNORM_PACK32) {
                    window->supports_hdr10_rgb10a2 = TRUE;
                }

                if (window->surfaceFormats[i].surfaceFormat.format == VK_FORMAT_A2R10G10B10_UNORM_PACK32) {
                    window->supports_hdr10_bgr10a2 = TRUE;
                }
            }

            // Extended scRGB colorspace with linear (scaled) encoding supported?
            if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT) {
                // Yes. Which pixel format?
                if (window->surfaceFormats[i].surfaceFormat.format == VK_FORMAT_R16G16B16A16_SFLOAT) {
                    window->supports_scrgb_rgba16f = TRUE;
                }
            }

            // AMD FreeSync2 HDR colorspace supported?
            if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_DISPLAY_NATIVE_AMD) {
                // Yes. Which pixel format?
                if (window->surfaceFormats[i].surfaceFormat.format == VK_FORMAT_R16G16B16A16_SFLOAT ||
                    window->surfaceFormats[i].surfaceFormat.format == VK_FORMAT_A2R10G10B10_UNORM_PACK32) {
                    window->supports_fs2_hdr = TRUE;
                }
            }

            // Standard SRGB colorspace with non-linear (gamma) encoding supported?
            if (window->surfaceFormats[i].surfaceFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
                // Yes. Which pixel format?
                if (window->surfaceFormats[i].surfaceFormat.format == VK_FORMAT_R16G16B16A16_SFLOAT) {
                    window->supports_srgb_rgba16f = TRUE;
                }

                if (window->surfaceFormats[i].surfaceFormat.format == VK_FORMAT_A2B10G10R10_UNORM_PACK32) {
                    window->supports_srgb_rgb10a2 = TRUE;
                }

                if (window->surfaceFormats[i].surfaceFormat.format == VK_FORMAT_A2R10G10B10_UNORM_PACK32) {
                    window->supports_srgb_bgr10a2 = TRUE;
                }
            }
        }
    }

    // Probe supported present modes for surface:
    uint32_t presentModeCount;
    result = vkGetPhysicalDeviceSurfacePresentModesKHR(vulkan->physicalDevice, window->surface, &presentModeCount, NULL);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: Failed to vkGetPhysicalDeviceSurfacePresentModesKHR: %i\n", result);

        return(FALSE);
    }

    if (presentModeCount != 0) {
        VkPresentModeKHR presentModes[10];
        presentModeCount = (presentModeCount <= 10) ? presentModeCount : 10;
        result = vkGetPhysicalDeviceSurfacePresentModesKHR(vulkan->physicalDevice, window->surface, &presentModeCount, presentModes);
        if (result != VK_SUCCESS) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: Failed to vkGetPhysicalDeviceSurfacePresentModesKHR for %i modes: %i\n", presentModeCount, result);

            return(FALSE);
        }

        // At the moment we just check for vsynced and non-vsynced present. The
        // former should always be supported, as the spec requires it:
        for (i = 0; i < presentModeCount; i++) {
            if (presentModes[i] == VK_PRESENT_MODE_FIFO_KHR)
                window->supports_vsync_FIFO = TRUE;
            if (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)
                window->supports_non_vsync = TRUE;
        }
    }

    return(TRUE);
}

#if PSYCH_SYSTEM == PSYCH_WINDOWS
#if defined(VK_USE_PLATFORM_WIN32_KHR)

void PsychProcessWindowEvents(PsychVulkanWindow* window)
{
    (void) window;
    MSG msg;

    // Run our message dispatch loop until we've processed all events in the event queue for our Vulkan windows:
    // We need to do this periodically so Windows is convinced that our window/application is still alive and
    // responsive. Otherwise it might conclude trouble and replace our Vulkan window by a ghost window which
    // is effectively dead and useless for OpenGL rendering and Vulkan display:
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

psych_bool PsychCreateMSWindowsDisplaySurface(PsychVulkanWindow* window, PsychVulkanDevice* vulkan, psych_bool isFullscreen, int screenId, void* outputHandle, PsychRectType rect, double refreshHz)
{
    VkResult result;
    psych_bool rc = FALSE;

    window->surface = (VkSurfaceKHR) VK_NULL_HANDLE;
    window->display = (VkDisplayKHR) VK_NULL_HANDLE;
    window->win32PrivateWindow = NULL;

    if (!windowClassRegistered) {
        WNDCLASSEX windowClass;
        windowClass.cbSize = sizeof(WNDCLASSEX);
        windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
        windowClass.lpfnWndProc = DefWindowProc;
        windowClass.cbClsExtra = 0;
        windowClass.cbWndExtra = 0;
        windowClass.hInstance = connection;
        windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        windowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        windowClass.lpszMenuName = NULL;
        windowClass.lpszClassName = "PTB-Vulkan";
        windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);

        // Register our own window class for our Vulkan windows:
        if (!RegisterClassEx(&windowClass)) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: For gpu [%s] creating private vulkan window class failed in RegisterClassEx().\n", vulkan->deviceProps.deviceName);

            goto createsurface_out;
        }

        windowClassRegistered = TRUE;
    }

    // Windows Vulkan ICD's need their own window, they don't want to share with OpenGL and
    // therefore can not present into the standard Psychtoolbox onscreen window:
    window->win32PrivateWindow = CreateWindowEx(WS_EX_TOPMOST | WS_EX_APPWINDOW,
                                                "PTB-Vulkan",   // class name
                                                "PTB-Vulkan",   // app name
                                                WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, // window style
                                                (int) rect[0],  // x coord
                                                (int) rect[1],  // y coord
                                                (unsigned int) (rect[2] - rect[0]), // width
                                                (unsigned int) (rect[3] - rect[1]), // height
                                                NULL,           // handle to parent
                                                NULL,           // handle to menu
                                                connection,     // hInstance
                                                NULL);          // no extra parameters

    if (!window->win32PrivateWindow) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: For gpu [%s] creating private vulkan output window for NVidia Vulkan failed in .\n", vulkan->deviceProps.deviceName);

        goto createsurface_out;
    }

    ShowWindow(window->win32PrivateWindow, SW_SHOW);

    PsychProcessWindowEvents(window);

    // Setup fullScreenExclusive struct for use by PsychProbeSurfaceProperties()
    // and vkCreateSwapchainKHR():
    PsychInitFullScreenExlusiveStructs(window);

    VkWin32SurfaceCreateInfoKHR createInfoWin32 = {
        .sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR,
        .pNext = NULL,
        .flags = 0,
        .hinstance = connection,
        .hwnd = window->win32PrivateWindow
    };

    result = vkCreateWin32SurfaceKHR(vulkanInstance, &createInfoWin32, NULL, &window->surface);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: For gpu [%s] creating vulkan output window failed in vkCreateWin32SurfaceKHR: %i\n", vulkan->deviceProps.deviceName, result);

        goto createsurface_out;
    }

    // Got a windowed window for Vulkan stimulus display.
    if (verbosity > 3)
        printf("PsychVulkanCore-INFO: For gpu [%s] created a window display surface [%p] for display window %i\n", vulkan->deviceProps.deviceName, window->surface, window->index);

    // Mark success:
    rc = TRUE;

createsurface_out:

    if (!rc && (window->win32PrivateWindow)) {
        DestroyWindow(window->win32PrivateWindow);
        window->win32PrivateWindow = NULL;
    }

    return (rc);
}
#endif
#endif

#if PSYCH_SYSTEM == PSYCH_LINUX
void PsychProcessWindowEvents(PsychVulkanWindow* window)
{
    // No op so far:
    (void) window;
}

psych_bool PsychCreateLinuxDisplaySurface(PsychVulkanWindow* window, PsychVulkanDevice* vulkan, psych_bool isFullscreen, int screenId, void* outputHandle, PsychRectType rect, double refreshHz)
{
    VkResult result;
    psych_bool rc = FALSE;
    int saved_default_screen = -1;
    // We need a per-window X-Display connection, otherwise the NVidia proprietary blob will be unhappy in direct display mode.
    // More specifically it will work on 1st invocation, but then fail on repeated open window calls, unless the display connection
    // is closed each time / a new connection created for the next open window invocation. Ergo we need per-window instance display
    // connections to work around this driver bug:
    Display* connection = XOpenDisplay(NULL);

    window->surface = (VkSurfaceKHR) VK_NULL_HANDLE;
    window->display = (VkDisplayKHR) VK_NULL_HANDLE;
    window->x11PrivateWindow = None;

    if (isFullscreen) {
        unsigned int i;
        uint32_t modeCount, planeCount, planeIndex, planeStackIndex;
        VkDisplayModePropertiesKHR* modes;
        VkDisplayModePropertiesKHR targetMode;
        VkDisplayPlanePropertiesKHR* planes;

        // On Linux we use RandR output leasing for leasing the DRM/KMS output for exclusive control, then switch it to direct display mode:
        if (vulkan->driverProps.driverID == VK_DRIVER_ID_NVIDIA_PROPRIETARY) {
            // NVidia blob needs a special workaround. It fails output leasing if the DefaultScreen() X-Screen in the X-Display connection
            // handle is not the screen to which the NVidia driver is attached. More specifically, vkGetRandROutputDisplayEXT() works fine
            // and figures out the right X-Screen, but vkAcquireXlibDisplayEXT() doesn't. As a workaround we hack the proper screenId into
            // the display connection handle just for these two Vulkan calls, then restore to original.
            saved_default_screen = ((_XPrivDisplay) connection)->default_screen;
            ((_XPrivDisplay) connection)->default_screen = screenId;
        }

        result = fpGetRandROutputDisplayEXT(vulkan->physicalDevice, connection, (RROutput) outputHandle, &window->display);
        if (result != VK_SUCCESS || window->display == VK_NULL_HANDLE) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: For gpu [%s] Mapping vulkan output to target display failed in vkGetRandROutputDisplayEXT (display=%p): result = %i\n", vulkan->deviceProps.deviceName, window->display, result);

            goto createsurface_out;
        }

        if (verbosity > 3)
            printf("PsychVulkanCore-INFO: gpu [%s] has access to target display %p. Trying to switch to direct display mode...\n", vulkan->deviceProps.deviceName, window->display);

        result = fpAcquireXlibDisplayEXT(vulkan->physicalDevice, connection, window->display);
        if (result != VK_SUCCESS) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: For gpu [%s] acquiring display for direct display mode failed (display=%p): result = %i\n", vulkan->deviceProps.deviceName, window->display, result);

            goto createsurface_out;
        }

        if (verbosity > 3)
            printf("PsychVulkanCore-INFO: gpu [%s] display %p switched to direct display mode. Setting up for direct display surface.\n", vulkan->deviceProps.deviceName, window->display);

        // We have exclusive direct display mode access now. Probe all properties needed to create a fullscreen display surface.
        // Get the video modes supported by the display
        result = vkGetDisplayModePropertiesKHR(vulkan->physicalDevice, window->display, &modeCount, NULL);
        if (result != VK_SUCCESS || modeCount == 0) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: For gpu [%s] could not find any valid display modes! count = %i, result = %i\n", vulkan->deviceProps.deviceName, modeCount, result);

            goto createsurface_out;
        }

        modes = (VkDisplayModePropertiesKHR*) malloc(modeCount * sizeof(VkDisplayModePropertiesKHR));
        result = vkGetDisplayModePropertiesKHR(vulkan->physicalDevice, window->display, &modeCount, modes);
        if (result != VK_SUCCESS) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: For gpu [%s] could not find any valid display modes! count = %i, result = %i\n", vulkan->deviceProps.deviceName, modeCount, result);

            free(modes);
            goto createsurface_out;
        }

        // Match available modes against our requirements wrt. resolution and refresh rate:
        for (i = 0; i < modeCount; i++) {
            if (verbosity > 3)
                printf("PsychVulkanCore-INFO: Target %d x %d @%f Hz versus mode[%i]: %d x %d @%f Hz\n", (unsigned int) (rect[2] - rect[0]), (unsigned int) (rect[3] - rect[1]), refreshHz,
                       i, modes[i].parameters.visibleRegion.width, modes[i].parameters.visibleRegion.height, (float) modes[i].parameters.refreshRate / 1000.0f);

            if ((modes[i].parameters.visibleRegion.width == (unsigned int) (rect[2] - rect[0])) &&
                (modes[i].parameters.visibleRegion.height == (unsigned int) (rect[3] - rect[1])) &&
                (fabs(modes[i].parameters.refreshRate - refreshHz * 1000) < 500)) {
                // This one is suitable for us, select it as targetMode:
                targetMode = modes[i];
                if (verbosity > 3)
                    printf("PsychVulkanCore-INFO: Selecting video mode[%i]: %d x %d @%f Hz\n", i, modes[i].parameters.visibleRegion.width, modes[i].parameters.visibleRegion.height, (float) modes[i].parameters.refreshRate / 1000.0f);

                break;
            }
        }

        // No need for mode list anymore, we hopefully got our targetMode:
        free(modes);

        if (i == modeCount) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: For gpu [%s] could not find any suitable display mode for our requirements!\n", vulkan->deviceProps.deviceName);

            goto createsurface_out;
        }

        // Get the list of display planes:
        result = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(vulkan->physicalDevice, &planeCount, NULL);
        if (result != VK_SUCCESS || planeCount == 0) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: For gpu [%s] could not find any valid display planes! count = %i, result = %i\n", vulkan->deviceProps.deviceName, planeCount, result);

            goto createsurface_out;
        }

        planes = (VkDisplayPlanePropertiesKHR*) malloc(planeCount * sizeof(VkDisplayPlanePropertiesKHR));
        result = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(vulkan->physicalDevice, &planeCount, planes);
        if (result != VK_SUCCESS) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: For gpu [%s] could not find any valid display planes! count = %i, result = %i\n", vulkan->deviceProps.deviceName, planeCount, result);

            free(planes);
            goto createsurface_out;
        }

        // Find a compatible plane for the display:
        for (planeIndex = 0; planeIndex < planeCount; planeIndex++) {
            uint32_t displayCount;
            VkDisplayKHR *displays;

            // Reject planes already used on a different display:
            if ((planes[planeIndex].currentDisplay != VK_NULL_HANDLE) &&
                (planes[planeIndex].currentDisplay != window->display))
                continue;

            result = vkGetDisplayPlaneSupportedDisplaysKHR(vulkan->physicalDevice, planeIndex, &displayCount, NULL);
            if (result != VK_SUCCESS) {
                if (verbosity > 0)
                    printf("PsychVulkanCore-ERROR: For gpu [%s] vkGetDisplayPlaneSupportedDisplaysKHR() failed for planeIndex %i, result = %i\n", vulkan->deviceProps.deviceName, planeIndex, result);

                free(planes);
                goto createsurface_out;
            }

            // Skip to next plane if this one does not have supported displays:
            if (displayCount == 0)
                continue;

            // Get set of supported displays:
            displays = (VkDisplayKHR*) malloc(displayCount * sizeof(VkDisplayKHR));
            result = vkGetDisplayPlaneSupportedDisplaysKHR(vulkan->physicalDevice, planeIndex, &displayCount, displays);
            if (result != VK_SUCCESS) {
                if (verbosity > 0)
                    printf("PsychVulkanCore-ERROR: For gpu [%s] vkGetDisplayPlaneSupportedDisplaysKHR() II failed for planeIndex %i, result = %i\n", vulkan->deviceProps.deviceName, planeIndex, result);

                free(planes);
                free(displays);

                goto createsurface_out;
            }

            // Is our display part of the set of supported displays for this planeIndex?
            for (i = 0; i < displayCount; i++) {
                if (displays[i] == window->display)
                    break;
            }

            // Matching done, free displays:
            free(displays);

            // If our display is part of the set then break out of planeIndex finding loop:
            if (i < displayCount) {
                planeStackIndex = planes[planeIndex].currentStackIndex;
                break;
            }
        }

        // Done with probing planes for supported displays:
        free(planes);

        // Found a suitable planeIndex and planeStackIndex?
        if (planeIndex == planeCount) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: For gpu [%s] Could not find any plane which supports our display in a set of %i planes!?!\n", vulkan->deviceProps.deviceName, planeCount);

            goto createsurface_out;
        }

        VkDisplaySurfaceCreateInfoKHR createInfo = {
            .sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR,
            .pNext = NULL,
            .flags = 0,
            .displayMode = targetMode.displayMode,
            .planeIndex = planeIndex,
            .planeStackIndex = planeStackIndex,
            .transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
            .globalAlpha = 0,
            .alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
            .imageExtent = targetMode.parameters.visibleRegion
        };

        result = vkCreateDisplayPlaneSurfaceKHR(vulkanInstance, &createInfo, NULL, &window->surface);
        if (result != VK_SUCCESS) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: For gpu [%s] vkCreateDisplayPlaneSurfaceKHR() failed for planeIndex %i, result = %i\n", vulkan->deviceProps.deviceName, planeIndex, result);

            goto createsurface_out;
        }

        // We got a raw display surface for displaying to in direct display mode!
        if (verbosity > 3)
            printf("PsychVulkanCore-INFO: For gpu [%s] created a direct display surface [%p] for display window %i\n", vulkan->deviceProps.deviceName, window->surface, window->index);
    }
    else {
        // Open a X11 windowed output window on Linux:

        // outputHandle contains the X11 Window handle of the Psychtoolbox onscreen window.
        #if defined(VK_USE_PLATFORM_XLIB_KHR)
        // The NVidia blob needs its own X-Window, it doesn't want to present into the standard Psychtoolbox onscreen window.
        // But even for the FOSS drivers there are corner cases where things go sideways without a dedicated X-Window, e.g.,
        // when using mirror mode where primary stimulus is presented by Vulkan, but a slave window presents an experimenter
        // feedback image (mirroring/cloning) via OpenGL. So the safe thing to do is to always use a dedicated X-Window:
        {
            unsigned int windowMapEventCount = 0;
            XSetWindowAttributes attr = { 0 };

            attr.background_pixel = 0;  // Background color defaults to black.
            attr.border_pixel = 0;      // Border color as well.
            attr.event_mask = StructureNotifyMask; // We're only interested in StructureNotify to wait for Windows to be mapped.
            attr.override_redirect = 1;
            window->x11PrivateWindow = XCreateWindow(connection, RootWindow(connection, screenId), (int) rect[0], (int) rect[1],
                                                     (unsigned int) (rect[2] - rect[0]), (unsigned int) (rect[3] - rect[1]), 0,
                                                     CopyFromParent, CopyFromParent, CopyFromParent,
                                                     CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWEventMask,
                                                     &attr);

            if (window->x11PrivateWindow == None) {
                if (verbosity > 0)
                    printf("PsychVulkanCore-ERROR: For gpu [%s] creating private vulkan output window for NVidia Vulkan failed in XCreateSimpleWindow.\n", vulkan->deviceProps.deviceName);

                goto createsurface_out;
            }

            XMapRaised(connection, window->x11PrivateWindow);

            while (windowMapEventCount < 1) {
                XEvent ev;
                XNextEvent(connection, &ev);
                if (ev.type == MapNotify || ev.type == ConfigureNotify)
                    windowMapEventCount++;
            }
        }

        VkXlibSurfaceCreateInfoKHR createInfoX11 = {
            .sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
            .pNext = NULL,
            .flags = 0,
            .dpy = connection,
            .window = (window->x11PrivateWindow != None) ? window->x11PrivateWindow : (Window) outputHandle,
        };

        result = vkCreateXlibSurfaceKHR(vulkanInstance, &createInfoX11, NULL, &window->surface);
        if (result != VK_SUCCESS) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: For gpu [%s] creating vulkan output window failed in vkCreateXlibSurfaceKHR: %i\n", vulkan->deviceProps.deviceName, result);

            goto createsurface_out;
        }
        #endif

        // Got a windowed window for Vulkan stimulus display.
        if (verbosity > 3)
            printf("PsychVulkanCore-INFO: For gpu [%s] created a windowing system window display surface [%p] for display window %i\n", vulkan->deviceProps.deviceName, window->surface, window->index);
    }

    // Mark success:
    rc = TRUE;

createsurface_out:

    #if defined(VK_USE_PLATFORM_XLIB_KHR)
        if (!rc && (window->x11PrivateWindow != None)) {
            XUnmapWindow(connection, window->x11PrivateWindow);
            XDestroyWindow(connection, window->x11PrivateWindow);
            XFlush(connection);
            window->x11PrivateWindow = None;
        }
    #endif

    if (!rc && window->display) {
        fpReleaseDisplayEXT(vulkan->physicalDevice, window->display);
        window->display = VK_NULL_HANDLE;
    }

    if (saved_default_screen != -1)
        ((_XPrivDisplay) connection)->default_screen = saved_default_screen;

    if (!rc && connection)
        XCloseDisplay(connection);

    if (rc)
        window->connection = connection;

    return (rc);
}
#endif

#if PSYCH_SYSTEM == PSYCH_OSX
void PsychProcessWindowEvents(PsychVulkanWindow* window)
{
    // No op so far:
    (void) window;
}

psych_bool PsychCreateMoltenVKDisplaySurface(PsychVulkanWindow* window, PsychVulkanDevice* vulkan, psych_bool isFullscreen, int screenId, void* outputHandle, PsychRectType rect, double refreshHz)
{
    VkResult result;

    window->surface = (VkSurfaceKHR) VK_NULL_HANDLE;
    window->display = (VkDisplayKHR) VK_NULL_HANDLE;

    VkMetalSurfaceCreateInfoEXT createInfoMetal = {
        .sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT,
        .pNext = NULL,
        .flags = 0,
        .pLayer = (const CAMetalLayer*) outputHandle
    };

    result = vkCreateMetalSurfaceEXT(vulkanInstance, &createInfoMetal, NULL, &window->surface);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: For gpu [%s] creating vulkan output window failed in vkCreateMetalSurfaceEXT: %i\n", vulkan->deviceProps.deviceName, result);

        return(FALSE);
    }

    // Got a windowed window for Vulkan stimulus display.
    if (verbosity > 3)
        printf("PsychVulkanCore-INFO: For gpu [%s] created a window Metal display surface [%p] for display window %i\n", vulkan->deviceProps.deviceName, window->surface, window->index);

    return (TRUE);
}
#endif

psych_bool PsychCreateDisplaySurface(PsychVulkanWindow* window, PsychVulkanDevice* vulkan, psych_bool isFullscreen, int screenId, void* outputHandle, PsychRectType rect, double refreshHz)
{
    #if PSYCH_SYSTEM == PSYCH_LINUX
        return(PsychCreateLinuxDisplaySurface(window, vulkan, isFullscreen, screenId, outputHandle, rect, refreshHz));
    #endif

    #if PSYCH_SYSTEM == PSYCH_WINDOWS
        return(PsychCreateMSWindowsDisplaySurface(window, vulkan, isFullscreen, screenId, outputHandle, rect, refreshHz));
    #endif

    #if PSYCH_SYSTEM == PSYCH_OSX
        return(PsychCreateMoltenVKDisplaySurface(window, vulkan, isFullscreen, screenId, outputHandle, rect, refreshHz));
    #endif
}

psych_bool PsychIsVulkanGPUSuitable(PsychVulkanWindow* window, PsychVulkanDevice* vulkan, psych_uint8* targetdeviceUUID, psych_bool isFullscreen, int screenId, void* outputHandle, PsychRectType rect, double refreshHz, int hdrMode, psych_bool needsTiming, unsigned int colorPrecision, int colorSpace, int colorFormat, int flags)
{
    VkResult result;
    VkBool32 supportsPresent;
    psych_bool rc = FALSE;

    // First test if the required deviceUUID provided by our OpenGL rendering master matches the one of the Vulkan device.
    // For OpenGL -> Vulkan interop to work, both the OpenGL renderer and Vulkan renderer must use the same physical gpu,
    // so the UUID must match.

    // AMD's Vulkan drivers on Linux (amdgpu-pro and amdvlk) older than DriverVersionRaw 8388778 aka v2.0.170, aka amdvlk v-2020.Q4.6
    // use a different UUID from what Linux Mesa radeonsi OpenGL expects. The more recent releases do fix this bug:
    // Mesa OpenGL: 4-Byte fields describing PCI bus location in format: domain:bus:device:function.
    // AMD  Vulkan: 4-Byte fields describing PCI bus location in format: bus:device:function:0
    // Therefore we first treat matching against AMD written drivers on Linux to match up the right 4-byte subfields:
    if ((PSYCH_SYSTEM == PSYCH_LINUX) && (vulkan->driverProps.driverID == VK_DRIVER_ID_AMD_OPEN_SOURCE_KHR || vulkan->driverProps.driverID == VK_DRIVER_ID_AMD_PROPRIETARY_KHR)) {
        // Skip first 4 bytes of targetdeviceUUID (PCI bus domain) when matching against AMD Vulkan:
        rc = memcmp(&targetdeviceUUID[4], &vulkan->physDeviceProps.deviceUUID[0], 12) == 0;
    }

    // Other OS + driver + gpu combos, or AMD Vulkan specific match failed. Assume the UUID from OpenGL and Vulkan implementations match,
    // also in the case of the Linux combo AMD proprietary (amdgpu-pro) OpenGL + AMD written Vulkan drivers:
    if (!rc) {
        rc = memcmp(targetdeviceUUID, &(vulkan->physDeviceProps.deviceUUID[0]), 16) == 0;
    }

    if (!rc) {
        // Mismatch: This Vulkan physical device does not represent the same physical gpu that is used
        // on the OpenGL / external side for stimulus rendering and interop - Reject as unsuitable:
        if (verbosity > 4) {
            printf("PsychVulkanCore-INFO: Vulkan gpu '%s' does not represent same physical gpu as required by external client on window %i.\n", vulkan->deviceProps.deviceName, window->index);
            printf("PsychVulkanCore-INFO: Requested OpenGL targetdeviceUUID: ");
            for (int i = 0; i < 16; i++)
                printf("%02x ", targetdeviceUUID[i]);
            printf("\n");

            printf("PsychVulkanCore-INFO: Provided Vulkan deviceUUID:        ");
            for (int i = 0; i < 16; i++)
                printf("%02x ", vulkan->physDeviceProps.deviceUUID[i]);
            printf("\n\n");
        }

        return(FALSE);
    }

    // If we need HDR, then does the device support HDR at all?
    if (hdrMode && !vulkan->hasHDR) {
        if (verbosity > 4)
            printf("PsychVulkanCore-INFO: Vulkan gpu '%s' does not support HDR as required by window %i.\n", vulkan->deviceProps.deviceName, window->index);

        return(FALSE);
    }

    // If we need precise timing, and the device does support timing, we are good.
    // Otherwise we'll need to use our hacks which require strict double-buffering.
    if (needsTiming && !vulkan->hasTiming && !vulkan->hasWait) {
        // Our hacks are needed, and therefore strict double-buffering support by
        // the driver + gpu device combo.

        // Our assumption is that all devices support strict double-buffering in fullscreen mode,
        // at least as confirmed for AMD and NVidia on Linux and Windows so far, so that is fine:
        if (!isFullscreen) {
            // Windowed non-fullscreen mode, not all devices are suitable. Blacklist ones early here
            // which we already know won't work. Atm. this list is Mesa based drivers like radv for AMD
            // and anvil for Intel. However, starting with Mesa 19.3.0, the drivers support new driconf
            // options to force a minImageCount of 2, ie. support for strict double-buffering, instead of
            // the default triple-buffering (minImageCount of 3). Psychtoolbox mesa override options file
            // contains the following config section:
            //
            // <engine engine_name_match="PsychVulkanCore">
            // <option name="vk_x11_strict_image_count" value="true" />
            // <option name="vk_x11_override_min_image_count" value="2" />
            // </engine>
            //
            // to enforce use of double-buffering for us, so our timing/timestamping hack will work for
            // radv, anvil or any other Mesa supplied Vulkan driver, and our problem is solved.
            //
            // Therefore we only reject Mesa drivers if Mesa version is < 19.3.0.
            if ((PSYCH_SYSTEM == PSYCH_LINUX) &&
                (vulkan->driverProps.driverID == VK_DRIVER_ID_MESA_RADV_KHR || vulkan->driverProps.driverID == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA_KHR) &&
                (vulkan->deviceProps.driverVersion < VK_MAKE_VERSION(19, 3, 0))) {
                // This is a no-go:
                if (verbosity > 4) {
                    printf("PsychVulkanCore-INFO: Vulkan gpu '%s' does not support required visual onset timing precision in the windowed configuration required by window %i.\n", vulkan->deviceProps.deviceName, window->index);
                    printf("PsychVulkanCore-INFO: This limitation can be resolved by upgrading your Mesa installation to Mesa version 19.3.0 or any later versions.\n");
                }

                return(FALSE);
            }
        }
    }

    if (colorPrecision) {
        // As of June 2020, Mesa 20.0.4, none of the OSS or proprietary drivers from AMD, Intel or NVidia support > 8 bpc in windowed X11 mode.
        // As far as fullscreen mode, NVidia proprietary blob and AMD written amdvlk do support 10 bit on Linux. Ergo, if precision is needed,
        // we should reject Mesa radv early, so the probe gets more quickly to amdvlk or amdgpu-pro on AMD gpu's. Rejecting anything else early
        // is pointless, as for Intel and NVidia on Linux and all manufacturers on Windows there is only one driver and thereby VkPhysicalDevice
        // per physical gpu, iow. there is no choice to find via probing. Let the probe go fully through and fail later on in the "game-over" case:
        if (vulkan->driverProps.driverID == VK_DRIVER_ID_MESA_RADV_KHR) {
            if (verbosity > 4) {
                printf("PsychVulkanCore-INFO: Vulkan gpu '%s' does not support required visual stimulus color precision by window %i.\n", vulkan->deviceProps.deviceName, window->index);
            }

            return(FALSE);
        }
    }

    // This is as far as we can get without creating a display surface on the target display, so lets create one:
    if (!PsychCreateDisplaySurface(window, vulkan, isFullscreen, screenId, outputHandle, rect, refreshHz)) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: Creating a display surface on Vulkan gpu '%s' for further probing on window %i failed.\n", vulkan->deviceProps.deviceName, window->index);

        return(FALSE);
    }

    // Does the physical gpu support presenting to this surface?
    // Note: This is almost useless for probing if a given vulkan gpu is physically driving the display on which our windows surface is displaying .
    // Both the NVidia and AMD drivers ignore the relationship between windows location on a specific X-Screen and if the driver+gpu drive that X-Screen.
    // Code inspection for both Mesa open-source Vulkan drivers and the AMD amdvlk open-source driver show that window location is not taken into account.
    // The Mesa drivers just check (for windowed windows on X11) if DRI3/Present is supported, and the color format is supported, iow. 8 bpc 24 bit color depth.
    // The amdvlk driver just checks if the gpu has display engines at all.
    result = vkGetPhysicalDeviceSurfaceSupportKHR(vulkan->physicalDevice, vulkan->graphicsQueueFamilyIndex, window->surface, &supportsPresent);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: vkGetPhysicalDeviceSurfaceSupportKHR() failed: Surface %p presentation supported = %i (res=%i).\n", window->surface, supportsPresent, result);

        return(FALSE);
    }

    if (verbosity > 4)
        printf("PsychVulkanCore-INFO: Probing if gpu '%s' supports presenting to the target surface: %s\n", vulkan->deviceProps.deviceName, supportsPresent ? "Yes" : "No");

    if (!supportsPresent)
        return(FALSE);

    // Some time granted to GUI event dispatch:
    PsychProcessWindowEvents(window);

    // Presenting to the surface is supported. Check supported color spaces, formats, present modes against requirements:
    if (!PsychProbeSurfaceProperties(window, vulkan)) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: Failed to probe surface properties on gpu '%s' for window %i [%p]: %i\n", vulkan->deviceProps.deviceName, window->index, window, result);

        return(FALSE);
    }

    if (needsTiming) {
        // We either need the ability to have strictly double-buffered swapchains, or we need precise timing support by the
        // driver, ie. support for the VK_GOOGLE_DISPLAY_TIMING_EXTENSION, or at least wait-for-present-completion support:
        if (!vulkan->hasTiming && !vulkan->hasWait && (window->surfaceCapabilities.minImageCount > 2)) {
            // No dice.
            if (verbosity > 4) {
                printf("PsychVulkanCore-INFO: Vulkan gpu '%s' does not support required visual onset timing precision in the %s configuration required by window %i.\n", vulkan->deviceProps.deviceName, isFullscreen ? "fullscreen" : "windowed", window->index);
            }

            return(FALSE);
        }

        if (!window->supports_vsync_FIFO) {
            // Vsync'ed VK_PRESENT_MODE_FIFO_KHR not supported? This should never happen!
            if (verbosity > 4)
                printf("PsychVulkanCore-INFO: Vulkan gpu '%s' does not support VK_PRESENT_MODE_FIFO_KHR in the %s configuration required by window %i! Driver bug?\n", vulkan->deviceProps.deviceName, isFullscreen ? "fullscreen" : "windowed", window->index);

            return(FALSE);
        }
    }

    // Do we need HDR?
    if (hdrMode) {
        // Yes. Check if any HDR color space + pixel format combo is supported at a minimum:
        if (!(window->supports_hdr10_rgb10a2 || window->supports_hdr10_bgr10a2 || window->supports_hdr10_rgba16f || window->supports_scrgb_rgba16f || window->supports_fs2_hdr)) {
            // Nope:
            if (verbosity > 3) {
                printf("PsychVulkanCore-INFO: Vulkan gpu '%s' does not support HDR in the %s configuration required by window %i.\n", vulkan->deviceProps.deviceName, isFullscreen ? "fullscreen" : "windowed", window->index);
                if ((PSYCH_SYSTEM == PSYCH_WINDOWS) && !isFullscreen)
                    printf("PsychVulkanCore-INFO: You may be able to make this work on Windows-10 if you enable HDR support in the display settings.\n");
            }

            return(FALSE);
        }
    }

    // Do we need higher than 8 bpc color precision?
    if (colorPrecision > 0) {
        // Yes. Check if suitable pixel color formats are supported:
        psych_bool supportsPrecision;

        // TODO FIXME This is crude and only covers standard sRGB and HDR10 BT2020 color space. Needs better matching.
        switch (colorPrecision) {
            case 1: // 10 bpc fixed point.
            case 4: // 16 bpc fixed point -> 10 bpc fixed point.
            case 5: // 16 bpc fixed point -> 16 bpc float fp16 ->10 bpc fixed point.
            case 6: // 16 bpc float fp16  -> 10 bpc fixed point.
                // All these are satisfied by a 10 bpc fixed point fallback:
                supportsPrecision = window->supports_hdr10_rgb10a2 || window->supports_hdr10_bgr10a2 || window->supports_srgb_rgb10a2 || window->supports_srgb_bgr10a2 || window->supports_fs2_hdr;
                break;

            case 2: // 16 bpc floating point RGBA16F.
                supportsPrecision = window->supports_hdr10_rgba16f || window->supports_srgb_rgba16f || window->supports_scrgb_rgba16f || window->supports_fs2_hdr;
                break;

            case 3: // 16 bpc fixed point:
                supportsPrecision = PsychIsColorSpaceFormatComboSupported(window, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, VK_FORMAT_R16G16B16A16_UNORM) || PsychIsColorSpaceFormatComboSupported(window, VK_COLOR_SPACE_HDR10_ST2084_EXT, VK_FORMAT_R16G16B16A16_UNORM);
                break;

            default:
                supportsPrecision = FALSE;
        }

        if (!supportsPrecision) {
            // Nope:
            if (verbosity > 4) {
                printf("PsychVulkanCore-INFO: Vulkan gpu '%s' does not support required precision %i in the %s configuration required by window %i.\n", vulkan->deviceProps.deviceName, colorPrecision, isFullscreen ? "fullscreen" : "windowed", window->index);
            }

            return(FALSE);
        }
    }

    // TODO FIXME probe for specific colorSpace support.
    // TODO FIXME probe for specific colorFormat support.
    // TODO FIXME probe for specific refreshHz support.

    // Some time granted to GUI event dispatch:
    PsychProcessWindowEvents(window);

    return(TRUE);
}

static bool getMemoryTypeFromProperties(PsychVulkanDevice* vulkan, uint32_t memoryTypeBits, VkFlags requirementsMask, uint32_t *memoryTypeIndex)
{
    uint32_t i;

    // Search memory types to find first memory type index with suitable properties:
    for (i = 0; i < vulkan->memoryProperties.memoryTypeCount; i++) {
        if (verbosity > 5)
            printf("PsychVulkanCore-INFO: getMemoryTypeFromProperties(): Testing memoryType index %i (%x) against allowed %x - ", i, 1 << i, memoryTypeBits);

        if ((memoryTypeBits & (1 << i))) {
            if (verbosity > 5)
                printf(" Properties %x vs. required %x - ", vulkan->memoryProperties.memoryTypes[i].propertyFlags, requirementsMask);

            // Memory type i is consistent with needs of our VkImage, does it satisfy requested memory properties
            // wrt. cpu and gpu memory access, efficiency and memory coherence?
            if ((vulkan->memoryProperties.memoryTypes[i].propertyFlags & requirementsMask) == requirementsMask) {
                if (verbosity > 5)
                    printf("OK. Using memoryTypeIndex %i.\n", i);

                *memoryTypeIndex = i;
                return(TRUE);
            }
            else if (verbosity > 5)
                printf("Nope.\n");
        }
        else if (verbosity > 5)
            printf("Nope.\n");
    }

    // No go!
    return(FALSE);
}

psych_bool PsychBeginCommandBuffer(VkCommandBuffer buffer)
{
    VkResult result;

    const VkCommandBufferBeginInfo commandBufferBeginInfo = {
        .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
        .pNext = NULL,
        .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
        .pInheritanceInfo = NULL,
    };

    result = vkBeginCommandBuffer(buffer, &commandBufferBeginInfo);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychBeginCommandBuffer: Failed (res=%i).\n", result);

        return(FALSE);
    }

    return(TRUE);
}

psych_bool PsychTransitionImageLayout(VkCommandBuffer cmdBuffer, VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout)
{
    VkImageMemoryBarrier barrier = { 0 };
    barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
    barrier.oldLayout = oldLayout;
    barrier.newLayout = newLayout;
    barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
    barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
    barrier.image = image;
    barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
    barrier.subresourceRange.baseMipLevel = 0;
    barrier.subresourceRange.levelCount = 1;
    barrier.subresourceRange.baseArrayLayer = 0;
    barrier.subresourceRange.layerCount = 1;

    VkPipelineStageFlags sourceStage;
    VkPipelineStageFlags destinationStage;

    if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL) {
        // Switch uninitialized interop image to optimal source for copy/blit:
        barrier.srcAccessMask = 0;
        barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;

        sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
        destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
    }
    else if (oldLayout == VK_IMAGE_LAYOUT_PREINITIALIZED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL) {
        // Switch host initialized/written to interop image to optimal source for copy/blit:
        barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
        barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;

        sourceStage = VK_PIPELINE_STAGE_HOST_BIT;
        destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
    }
    else if (oldLayout == VK_IMAGE_LAYOUT_PREINITIALIZED && newLayout == VK_IMAGE_LAYOUT_GENERAL) {
        // Switch host initialized/written to interop image to optimal source for copy/blit:
        barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
        barrier.dstAccessMask = VK_ACCESS_MEMORY_WRITE_BIT | VK_ACCESS_MEMORY_READ_BIT ;
        printf("PsychTransitionImageLayout() VK_IMAGE_LAYOUT_PREINITIALIZED -> VK_IMAGE_LAYOUT_GENERAL\n");

        sourceStage = VK_PIPELINE_STAGE_HOST_BIT;
        destinationStage = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; // XXX: Could also use VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT.
    }
    else if (oldLayout == VK_IMAGE_LAYOUT_PREINITIALIZED && newLayout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
        // Switch host initialized/written to interop image to optimal source for copy/blit:
        barrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
        barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
        printf("PsychTransitionImageLayout() VK_IMAGE_LAYOUT_PREINITIALIZED -> VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL\n");

        sourceStage = VK_PIPELINE_STAGE_HOST_BIT;
        destinationStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
    }
    else if (oldLayout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL) {
        // Switch host initialized/written to interop image to optimal source for copy/blit:
        barrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
        barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
        printf("PsychTransitionImageLayout() VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL -> VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL\n");

        sourceStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT ;
        destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
    }
    else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
        // Switch host initialized/written to interop image to optimal source for copy/blit:
        barrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
        barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
        printf("PsychTransitionImageLayout() VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL -> VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL\n");

        sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
        destinationStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT ;
    }
    else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
        // Debug for interop image: Before vkCmdClearColorImage():
        barrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
        barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;

        sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
        destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
    }
    else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL) {
        // Debug for interop image: After vkCmdClearColorImage():
        barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
        barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;

        sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
        destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
    }
    else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
        // Switch swapchain image from copy/blit destination back to presentable image for display:
        barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
        barrier.dstAccessMask = 0;

        sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
        destinationStage = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
    }
    else if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
        // Switch swapchain image from "we don't care about old content" to optimal for copy/blit destination:
        barrier.srcAccessMask = 0;
        barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;

        sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
        destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
    }
    else {
        printf("PsychVulkanCore-CRITICAL:PsychTransitionImageLayout() Unsupported layout transition requested. No-Op!\n");
        return(FALSE);
    }

    vkCmdPipelineBarrier(cmdBuffer, sourceStage, destinationStage, 0, 0, NULL, 0, NULL, 1, &barrier);

    return(TRUE);
}

VkResult PsychGetNextSwapChainTargetBuffer(PsychVulkanWindow* window)
{
    VkResult result;

    // Get the index currentSwapChainBuffer of the next available swapchain image:
    // Both imageAcquiredSemaphores[frameIndex % window->numBuffers] and the
    // flipDoneFence will signal when the display engine is done with scanning
    // out the acquired image, ergo, when it was replaced as old scanout buffer
    // aka frontbuffer by a new frontbuffer, which was our old backbuffer. Iow.,
    // the semaphore and fence will signal when the previously scheduled swap /
    // present actually completed, hopefully due to gpu pageflip completion.
    //
    // So fence and semaphore signalling is our "flip completed" signal that we
    // can use to wait for flip and for visual stimulus onset timestamping.
    //
    // Note that this only works for double-buffered swap chains with exactly 2
    // buffers, not for triple-buffered or n-buffered chains with n != 2.
    result = vkResetFences(window->vulkan->device, 1, &window->flipDoneFence);
    if ((result != VK_SUCCESS) && (verbosity > 0))
        printf("PsychVulkanCore-ERROR: PsychGetNextSwapChainTargetBuffer(%i): Fence reset failed with error code %i.\n", window->index, result);

    result = vkAcquireNextImageKHR(window->vulkan->device, window->swapChain, UINT64_MAX, window->imageAcquiredSemaphores[window->frameIndex % window->numBuffers], window->flipDoneFence, &window->currentSwapChainBuffer);
    if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) {
        // Acquired currentSwapChainBuffer - the index of the next swapchain image
        // to copy the to-be-presented stimulus into, iow. our "backBuffer".
        if ((verbosity > 6) || (verbosity > 1 && result == VK_SUBOPTIMAL_KHR))
            printf("PsychVulkanCore-DEBUG: PsychGetNextSwapChainTargetBuffer(%i): frameIndex %i - Next swapChain backBuffer image with index %i acquired.\n", window->index,
                   window->frameIndex, window->currentSwapChainBuffer);

            if (verbosity > 1 && result == VK_SUBOPTIMAL_KHR)
                printf("PsychVulkanCore-WARNING: PsychGetNextSwapChainTargetBuffer(%i): Swapchain reports status VK_SUBOPTIMAL_KHR. Performance and timing precision may suffer!\n", window->index);

        result = VK_SUCCESS;

        return(result);
    }

    if (verbosity > 0) {
        if (result == VK_ERROR_OUT_OF_DATE_KHR) {
            printf("PsychVulkanCore-ERROR: PsychGetNextSwapChainTargetBuffer(%i): Swapchain reports status VK_ERROR_OUT_OF_DATE_KHR!\n", window->index);
        }
        else {
            printf("PsychVulkanCore-ERROR: PsychGetNextSwapChainTargetBuffer(%i): Swapchain reports error code %i.\n", window->index, result);
        }
    }

    // Error return:
    return(result);
}

psych_bool PsychWaitForPresentCompletion(PsychVulkanWindow* window)
{
    VkResult result;

    // Wait for flipDoneFence to signal, iow. for confirmed flip completion aka
    // imminent visual stimulus onset. Then reset the fence and timestamp the moment:
    result = vkWaitForFences(window->vulkan->device, 1, &window->flipDoneFence, VK_TRUE, 1e9);
    if (result == VK_SUCCESS)
        result = vkResetFences(window->vulkan->device, 1, &window->flipDoneFence);

    if ((result != VK_SUCCESS) && (verbosity > 0)) {
        if (result == VK_TIMEOUT) {
            printf("PsychVulkanCore-ERROR: PsychWaitForPresentCompletion(%i): Fence wait+reset failed - Timeout!\n", window->index);
        } else {
            printf("PsychVulkanCore-ERROR: PsychWaitForPresentCompletion(%i): Fence wait+reset failed with error code %i.\n", window->index, result);
        }
    }

    return (result == VK_SUCCESS ? TRUE : FALSE);
}

psych_bool PsychPresent(PsychVulkanWindow* window, double tWhen, unsigned int timestampMode)
{
    VkResult result;
    VkPresentTimeGOOGLE targetPresentTimeG;
    PsychVulkanDevice* vulkan = window->vulkan;
    uint64_t targetPresentId;

    // Mark presentation timestamp as so far "invalid"/"unknown":
    window->tPresentComplete = -1;

    // Some time granted to GUI event dispatch:
    PsychProcessWindowEvents(window);

    // Wait for the image acquired semaphore to be signaled to ensure that the
    // swapchain image won't be overwritten with new content from the interop
    // image until the presentation engine has fully released ownership to us,
    // and it is okay to overwrite the old image content:
    VkPipelineStageFlags pipeStageFlags[] = { VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT };

    // If window->interopRenderDoneSemaphore exists (non-zero) then we not only
    // wait until the target swapChainImage is ready, but also until the external
    // client has signalled render completion on the interop image via signalling
    // of interopRenderDoneSemaphore:
    VkSemaphore waitSemaphores[2];
    waitSemaphores[0] = window->imageAcquiredSemaphores[window->frameIndex % window->numBuffers];
    waitSemaphores[1] = window->interopRenderDoneSemaphore;

    VkSubmitInfo submitInfo = {
        .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
        .pNext = NULL,
        .waitSemaphoreCount = (window->interopRenderDoneSemaphore) ? 2 : 1,
        .pWaitSemaphores = waitSemaphores,
        .pWaitDstStageMask = &pipeStageFlags[0],
        .commandBufferCount = 1,
        .pCommandBuffers = &window->swapChainCommandBuffers[window->currentSwapChainBuffer],
        .signalSemaphoreCount = 1,
        .pSignalSemaphores = &window->imageCopyDoneSemaphores[window->frameIndex % window->numBuffers],
    };

    // Submit copy command. imageCopyDoneSemaphores will signal once the copy or blit
    // is completed and we are swap-ready:
    result = vkQueueSubmit(vulkan->graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychPresent(%i): Interop -> Swapchain copy failed with error code %i.\n", window->index, result);

        return(FALSE);
    }

    // Wait for imageCopyDoneSemaphores before queuing the present to the display driver:
    VkPresentInfoKHR present = {
        .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
        .pNext = NULL,
        .waitSemaphoreCount = 1,
        .pWaitSemaphores = &window->imageCopyDoneSemaphores[window->frameIndex % window->numBuffers],
        .swapchainCount = 1,
        .pSwapchains = &window->swapChain,
        .pImageIndices = &window->currentSwapChainBuffer,
        .pResults = NULL, // swapchainCount separate VkResult's if presenting to multiple chains.
    };

    // VK_KHR_PRESENT_ID supported?
    #ifdef VK_KHR_present_id
    if (vulkan->hasWait && (timestampMode > 0)) {
        // Assign present id - the current frameIndex:
        VkPresentIdKHR presentIdInfo = {
            .sType = VK_STRUCTURE_TYPE_PRESENT_ID_KHR,
            .pNext = present.pNext,
            .swapchainCount = 1,
            .pPresentIds = &targetPresentId,
        };

        // targetPresentId must start with 1 for 1st queued present, hence the frameIndex + 1:
        targetPresentId = window->frameIndex + 1;
        present.pNext = &presentIdInfo;

        if (verbosity > 7)
            printf("PsychVulkanCore-DEBUG: PsychPresent(%i): Assigning frame %i with VkPresentIdKHR presentID %lli.\n", window->index, window->frameIndex, targetPresentId);
    }
    #endif

    // VK_GOOGLE_DISPLAY_TIMING supported?
    if (vulkan->hasTiming && (timestampMode > 1)) {
        // Yes: Queue a target time for the present:
        VkPresentTimesInfoGOOGLE presentTimeInfoG = {
            .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
            .pNext = present.pNext,
            .swapchainCount = 1,
            .pTimes = &targetPresentTimeG,
        };

        present.pNext = &presentTimeInfoG;

        #if PSYCH_SYSTEM == PSYCH_LINUX || PSYCH_SYSTEM == PSYCH_OSX
        // Linux: Map tWhen GetSecs() CLOCK_REALTIME target time into CLOCK_MONOTONIC time, convert to Nanoseconds:
        // macOS: Map tWhen GetSecs() time into Mach host time (which is a no-op btw.), convert to Nanoseconds:
        targetPresentTimeG.desiredPresentTime = PsychOSRefTimeToMonotonicTime(tWhen) * 1e9;
        #else
        targetPresentTimeG.desiredPresentTime = 0; // TODO FIXME IMPLEMENT!
        #endif

        targetPresentTimeG.presentID = window->frameIndex;

        if (verbosity > 7)
            printf("PsychVulkanCore-DEBUG: PsychPresent(%i): Queuing frame %i with VkPresentTimesInfoGOOGLE for present at time >= tWhen %f secs.\n", window->index, window->frameIndex, tWhen);
    }
    else {
        // No: Simply wait for target present time:
        PsychWaitUntilSeconds(tWhen);
    }

    // Commit new HDR metadata to the swapChain / display if requested:
    if (window->hdrMode && window->currentDisplayHDRMetadataNeedsCommit) {
        window->currentDisplayHDRMetadataNeedsCommit = FALSE;

        PsychSetHDRMetaData(window);
    }

    // Present it asap:
    result = vkQueuePresentKHR(vulkan->graphicsQueue, &present);
    if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) {
        // Success! All perfectly good?
        if ((verbosity > 6) || (verbosity > 1 && result == VK_SUBOPTIMAL_KHR))
            printf("PsychVulkanCore-DEBUG: PsychPresent(%i): frameIndex %i - swapChain image with index %i queued for present at tWhen %f secs.\n", window->index,
                   window->frameIndex, window->currentSwapChainBuffer, tWhen);

        // Suboptimal present? This may tear or have reduced performance / increased latency, and also
        // potentially screwed up timing:
        if (verbosity > 1 && result == VK_SUBOPTIMAL_KHR)
            printf("PsychVulkanCore-WARNING: PsychPresent(%i): Swapchain reports status VK_SUBOPTIMAL_KHR. Performance and timing precision may suffer!\n", window->index);
    }
    else {
        // Failed!
        if (verbosity > 0) {
            if (result == VK_ERROR_OUT_OF_DATE_KHR) {
                printf("PsychVulkanCore-ERROR: PsychPresent(%i): Failed! Swapchain reports status VK_ERROR_OUT_OF_DATE_KHR!\n", window->index);
            }
            else {
                printf("PsychVulkanCore-ERROR: PsychPresent(%i): Failed! Swapchain reports error code %i.\n", window->index, result);
            }
        }

        return(FALSE);
    }

    // The present is queued, but possibly not finished yet, so we need to enforce a host wait
    // to  wait for present completion.
    window->frameIndex++;

    // Some time granted to GUI event dispatch:
    PsychProcessWindowEvents(window);

    // Try to acquire next target swapchain buffer. window->currentSwapChainBuffer will
    // contain the index of the proper swapChainImage. This will setup the flipDoneFence
    // to signal Present completion of the present we just sent out:
    if (PsychGetNextSwapChainTargetBuffer(window) != VK_SUCCESS)
        return(FALSE);

    // Always wait for flipDoneFence:
    // Makes fence handling more robust and we can't prevent being throttled anyway, given
    // that we can't change sync-to-vblank settings on the fly, only at swapChain creation time:
    if (!PsychWaitForPresentCompletion(window))
        return(FALSE);

    // Should we timestamp (imminent) stimulus onset?
    if (timestampMode > 0) {
        // Wait for present completion supported?
        #ifdef VK_KHR_present_id
        if (vulkan->hasWait) {
            // Blocking wait with timeout of 1 second for present completion of the just-queued present:
            result = fpWaitForPresentKHR(vulkan->device, window->swapChain, targetPresentId, 1e9);
            if ((result != VK_SUCCESS) && (verbosity > 0)) {
                if (result == VK_TIMEOUT) {
                    printf("PsychVulkanCore-ERROR: vkWaitForPresentKHR(%i): Failed due to timeout!\n", window->index);
                } else {
                    printf("PsychVulkanCore-ERROR: vkWaitForPresentKHR(%i): Failed with error code %i.\n", window->index, result);
                }
            }
            else if (verbosity > 8) {
                double tNow;
                PsychGetAdjustedPrecisionTimerSeconds(&tNow);
                printf("PsychVulkanCore-DEBUG: PsychPresent(%i): Frame %i with presentID %lli signalled by vkWaitForPresentKHR as complete at %f seconds.\n", window->index, window->frameIndex - 1, targetPresentId, tNow);
            }
        }
        #endif

        // VK_GOOGLE_DISPLAY_TIMING supported for timestamping?
        if (vulkan->hasTiming && (timestampMode > 1)) {
            // Yes. Fetch timestamp from Vulkan:
            const double tQueryTimeout = 0.5; // Time out after more than tQueryTimeout seconds of failure.
            VkPastPresentationTimingGOOGLE pastTiming[1000];
            double tNow, tStart;
            uint32_t count = 0;

            // Wait until target present time is reached - No point checking before.
            // Not needed if we used wait for present complete:
            if (!vulkan->hasWait)
                PsychWaitUntilSeconds(tWhen);

            // Poll for arrival of present completion timestamp:
            PsychGetAdjustedPrecisionTimerSeconds(&tNow);
            tStart = tNow;

            // Unused atm - no longer needed:
            // This label was only used on macOS + MoltenVK for buggy MVK versions which do not
            // actually dequeue timestamps fetched via vkGetPastPresentationTimingGOOGLE(), as they
            // should, but keep all timestamps in the queue until the queue reaches capacity. Iow.,
            // the queue can only grow (up to a defined maximum, e.g., 60 entries), but never shrink.
            // This is a spec violation and it can make the timestamp query fail for up to slightly
            // more than 1 video refresh cycle. As a workaround, we repeat the procedure in 1 msecs
            // intervals until either success, or our master timeout is hit, which suggests some other
            // unexpected macOS bug:
            macosmvkworkaroundrepeat:

            while ((count < 1) && (tNow < tStart + tQueryTimeout)) {
                result = fpGetPastPresentationTimingGOOGLE(vulkan->device, window->swapChain, &count, NULL);
                if (result != VK_SUCCESS) {
                    if (verbosity > 0)
                        printf("PsychVulkanCore-ERROR: PsychPresent(%i): Failed! vkGetPastPresentationTimingGOOGLE reports error code %i.\n", window->index, result);

                    return(FALSE);
                }

                PsychGetAdjustedPrecisionTimerSeconds(&tNow);
                if ((count < 1) && (tNow < tStart + tQueryTimeout)) {
                    PsychYieldIntervalSeconds(0.001);
                    if (verbosity > 9)
                        printf("PsychVulkanCore-DEBUG: PsychPresent(%i): Polling for vkGetPastPresentationTimingGOOGLE returning results. %f msecs elapsed, %f msecs since tWhen.\n", window->index, count, 1000 * (tNow - tStart), 1000 * (tNow - tWhen));
                }
            }

            if (verbosity > 8)
                printf("PsychVulkanCore-DEBUG: PsychPresent(%i): vkGetPastPresentationTimingGOOGLE returned %i timestamps after %f msecs wait. Fetching last one %f msecs since tWhen.\n", window->index, count, 1000 * (tNow - tStart), 1000 * (tNow - tWhen));

            // Got something?
            if (count < 1) {
                // Nope, timeout.

                // Take simple raw timestamp as fallback:
                PsychGetAdjustedPrecisionTimerSeconds(&window->tPresentComplete);

                // First frame in this windows session?
                if (window->frameIndex - 1 > 0) {
                    // No, bad:
                    if (verbosity > 0)
                        printf("PsychVulkanCore-ERROR: PsychPresent(%i): vkGetPastPresentationTimingGOOGLE failed to retrieve timestamp! Timed out.\n", window->index);

                    return(FALSE);
                }
                else {
                    // Yes, be lenient, as at least on Linux + Mesa this seems to be expected:
                    if (verbosity > 5)
                        printf("PsychVulkanCore-DEBUG: PsychPresent(%i): vkGetPastPresentationTimingGOOGLE failed to retrieve timestamp for frameIndex 0. Timed out. Carrying on with fallback.\n", window->index);
                }
            }
            else {
                int i;

                // Got at least one timestamp. We want the most recent one:
                do {
                    count = (count > 1000) ? 1000 : count;

                    result = fpGetPastPresentationTimingGOOGLE(vulkan->device, window->swapChain, &count, &pastTiming[0]);
                    if ((result != VK_SUCCESS) && (result != VK_INCOMPLETE)) {
                        if (verbosity > 0)
                            printf("PsychVulkanCore-ERROR: PsychPresent(%i): Failed to retrieve next timestamp! vkGetPastPresentationTimingGOOGLE reports error code %i.\n", window->index, result);

                        return(FALSE);
                    }
                    else if ((verbosity > 8) && (result == VK_INCOMPLETE))
                        printf("PsychVulkanCore-DEBUG: PsychPresent(%i): vkGetPastPresentationTimingGOOGLE (count %i) for presentID %i [current %i] returned old timestamp %f Fetching next one.\n",
                               window->index, count, pastTiming[0].presentID, targetPresentTimeG.presentID, (double) pastTiming[0].actualPresentTime / 1e9);

                    if (result == VK_INCOMPLETE) fpGetPastPresentationTimingGOOGLE(vulkan->device, window->swapChain, &count, NULL);
                } while (result == VK_INCOMPLETE);

                for (i = 0; i < count; i++) {
                    if (verbosity > 8)
                        printf("PsychVulkanCore-DEBUG: PsychPresent(%i):%i: presentID %i [need %i] returned timestamp %f.\n",
                               window->index, i, pastTiming[i].presentID, targetPresentTimeG.presentID, (double) pastTiming[i].actualPresentTime / 1e9);
                    if (pastTiming[i].presentID == targetPresentTimeG.presentID)
                        break;
                }

                // Found a matching record before timeout?
                if (i == count) {
                    // No. Could be timeout due to temporary malfunction, or an OS implementation bug.

                    // Yield to give the system some processing time:
                    PsychYieldIntervalSeconds(0.0001);
                    PsychGetAdjustedPrecisionTimerSeconds(&tNow);

                    // macOS and not yet timed out?
                    if ((PSYCH_SYSTEM == PSYCH_OSX) && (tNow < tStart + tQueryTimeout)) {
                        // As of macOS 10.15.7 and MoltenVK 1.0.10 May 2022, at least the very first presented frame
                        // does not result in the Windowserver calling the drawable presented callback, leaving the
                        // first wait-for-present-completion timing out after tQueryTimeout, and a stale handler left
                        // to invoke. Later cleanup actions triggered during vkAcquireNextImageKHR() -> [mtlDrawable release]
                        // will trigger handler invocation with 0 timestamp, so a presented event gets misqueued for frame 0
                        // and falsely picked up while waiting for frame 1 present completion events. This would lead to failure
                        // unless we repeat the whole query sequence here, discarding the frame 0 timestamp. This way, frame 1
                        // timestamp is picked up after true frame 1 present completion. Iow. this hack/workaround realigns
                        // expected with actual timestamps in case that a presented handler fails to get triggered by the
                        // Windowserver/CoreAnimation/Metal/Vulkan-MoltenVK at the proper time. Comparison with other purely
                        // Metal-based code suggests strongly a macOS operating system bug at work.
                        if (verbosity > 10)
                            printf("PsychVulkanCore-DEBUG: PsychPresent(%i): No match yet [%f usecs elapsed]! Retrying in a bit...\n", window->index, 1.0e6 * (tNow - tStart));

                        // Repeat the whole query timestamps + try to find proper presentation timestamp:
                        count = 0;
                        goto macosmvkworkaroundrepeat;
                    }

                    // Final timeout! This should not happen on this OS + display system! Fail.
                    if (verbosity > 0)
                        printf("PsychVulkanCore-ERROR: PsychPresent(%i): NO MATCH for target presentID %i! OS bug!?! Timed out, aborting.\n", window->index, targetPresentTimeG.presentID);

                    return(FALSE);
                }

                // On macOS MoltenVK, the driver maps an invalid Metal drawable present time of zero to targetPresentTimeG.desiredPresentTime,
                // so lets detect these Metal presentation and/or timestamping failures and report them to our caller as zero "invalid" timestamps:
                if ((PSYCH_SYSTEM == PSYCH_OSX) && (targetPresentTimeG.desiredPresentTime == pastTiming[i].actualPresentTime))
                    pastTiming[i].actualPresentTime = 0;

                // Got the final - and thereby most recent - timestamp.
                // Assign as present completion timestamp:
                window->tPresentComplete = PsychOSMonotonicToRefTime((double) pastTiming[i].actualPresentTime / 1e9);

                if (verbosity > 7)
                    printf("PsychVulkanCore-DEBUG: PsychPresent(%i): vkGetPastPresentationTimingGOOGLE for presentID %i returned flip completion timestamp %f secs vs. desired present time %f secs. Delta %f msecs.\n",
                        window->index, pastTiming[i].presentID, window->tPresentComplete, PsychOSMonotonicToRefTime((double) pastTiming[i].desiredPresentTime / 1e9), 1000 * (window->tPresentComplete - tWhen));
            }
        }
        else {
            // No. Take simple raw timestamp:
            PsychGetAdjustedPrecisionTimerSeconds(&window->tPresentComplete);
        }

        if (verbosity > 5)
            printf("PsychVulkanCore-DEBUG: PsychPresent(%i): Present for frameIndex %i completed: tComplete = %f secs.\n", window->index, window->frameIndex - 1, window->tPresentComplete);
    }

    // Some time granted to GUI event dispatch:
    PsychProcessWindowEvents(window);

    // Present successfully completed:
    return(TRUE);
}

psych_bool PsychCreateInteropTexture(PsychVulkanWindow* window)
{
    const psych_bool bringup = FALSE;
    psych_bool extmem = (PSYCH_SYSTEM != PSYCH_OSX) ? TRUE : FALSE;
    VkResult result;
    PsychVulkanDevice* vulkan = window->vulkan;
    VkImageTiling tiling = (window->interopTextureTiled && !bringup) ? VK_IMAGE_TILING_OPTIMAL : VK_IMAGE_TILING_LINEAR;
    VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | ((!bringup) ? VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT : 0);
    VkFlags requiredMemoryProps = bringup ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
    VkMemoryRequirements memoryRequirements;

    // Note wrt. memoryRequirements:
    // For bringup we need VK_IMAGE_TILING_LINEAR so we can map the memory for host writes.
    // We also need VK_IMAGE_TILING_LINEAR on NVidia to get host visible memory for host writes.
    // However, VK_IMAGE_TILING_LINEAR does *not* allow rendering into as VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT.

    // Image setup for external interop:
    VkExternalMemoryImageCreateInfo externalMemImageInfo = {
        .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
        .pNext = NULL,
        #if PSYCH_SYSTEM == PSYCH_WINDOWS
        .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT,
        #else
        .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
        #endif
    };

    // Create VkImage object:
    VkImageCreateInfo imageCreateInfo = {
        .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
        .pNext = (!bringup && extmem) ? &externalMemImageInfo : NULL,
        .imageType = VK_IMAGE_TYPE_2D,
        .format = window->interopTextureVkFormat,
        .extent = {window->width, window->height, 1},
        .mipLevels = 1,
        .arrayLayers = 1,
        .samples = VK_SAMPLE_COUNT_1_BIT,
        .tiling = tiling,
        .usage = usage,
        .flags = 0,
        .initialLayout = (!bringup && extmem) ? VK_IMAGE_LAYOUT_UNDEFINED : VK_IMAGE_LAYOUT_PREINITIALIZED,
    };

    result = vkCreateImage(vulkan->device, &imageCreateInfo, NULL, &window->interopImage);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture:vkCreateImage() failed for window %i (res=%i).\n", window->index, result);

        return(FALSE);
    }

    // Figure out memory and backing store requirements for image to be exportable to OpenGL:
    vkGetImageMemoryRequirements(vulkan->device, window->interopImage, &memoryRequirements);

    VkExportMemoryAllocateInfo exportAllocInfo = {
        .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO,
        .pNext = NULL,
        #if PSYCH_SYSTEM == PSYCH_WINDOWS
        .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT,
        #else
        .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
        #endif
    };

    VkMemoryAllocateInfo memoryAllocInfo = {
        memoryAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
        memoryAllocInfo.pNext = (!bringup && extmem) ? &exportAllocInfo : NULL,
        memoryAllocInfo.allocationSize = memoryRequirements.size,
        memoryAllocInfo.memoryTypeIndex = 0,
    };

    if(!getMemoryTypeFromProperties(vulkan, memoryRequirements.memoryTypeBits, requiredMemoryProps, &memoryAllocInfo.memoryTypeIndex)) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture:getMemoryTypeFromProperties() failed for window %i.\n", window->index);

        goto out_interop_image;
    }

    // Allocate the backing memory:
    result = vkAllocateMemory(vulkan->device, &memoryAllocInfo, NULL, &window->interopImageMemory);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture:vkAllocateMemory() failed for window %i (res=%i).\n", window->index, result);

        goto out_interop_image;
    }

    window->interopMemorysize = memoryRequirements.size;

    // Bind backing memory image:
    result = vkBindImageMemory(vulkan->device, window->interopImage, window->interopImageMemory, 0);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture:vkBindImageMemory() failed for window %i (res=%i).\n", window->index, result);

        goto out_interop_image;
    }

    // In normal operation mode, get the interop handles for exporting our interopImage to Psychtoolbox
    // for import as OpenGL textures/fbo for render-to-texture:
    if (!bringup) {
        #if PSYCH_SYSTEM == PSYCH_WINDOWS
            // Get handle for shared memory with OpenGL:
            VkMemoryGetWin32HandleInfoKHR memoryGetWinhandleInfo = {
                .sType = VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR,
                .pNext = NULL,
                .memory = window->interopImageMemory,
                .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT,
            };

            result = fpGetMemoryWin32HandleKHR(vulkan->device, &memoryGetWinhandleInfo, &window->interopHandles.memory);
            if (verbosity > 4)
                printf("PsychVulkanCore-INFO: PsychCreateInteropTexture: Got Win32 memory handle %p.\n", window->interopHandles.memory);
        #endif

        #if PSYCH_SYSTEM == PSYCH_LINUX
            // Get fd for shared memory with OpenGL:
            VkMemoryGetFdInfoKHR memoryGetFdInfo = {
                .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
                .pNext = NULL,
                .memory = window->interopImageMemory,
                .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
            };

            result = fpGetMemoryFdKHR(vulkan->device, &memoryGetFdInfo, &window->interopHandles.memory);
            if (verbosity > 4)
                printf("PsychVulkanCore-INFO: PsychCreateInteropTexture: Got POSIX memory fd %i.\n", window->interopHandles.memory);
        #endif

        #if PSYCH_SYSTEM == PSYCH_OSX
            // Tell MoltenVK to allocate a IOSurface as backing store for the interop image:
            result = vkUseIOSurfaceMVK(window->interopImage, NULL);
            if (result == VK_SUCCESS) {
                IOSurfaceRef ioSurface;

                // Retrieve backing IOSurface for interop image:
                vkGetIOSurfaceMVK(window->interopImage, &ioSurface);

                // Create a sibling OpenGL texture for it, of matching format:
                glGenTextures(1, (GLuint*) &window->interopHandles.memory);
                glBindTexture(GL_TEXTURE_RECTANGLE_ARB, (GLuint) window->interopHandles.memory);

                // Attach IOSurface as backing store for the texture, so rendering to the texture renders to the VkImage:
                // TODO GL_RGBA16 aka window->colorPrecision == 3, but not supported by MoltenVK/Metal on macOS for display anyway.
                if (CGLTexImageIOSurface2D(CGLGetCurrentContext(), GL_TEXTURE_RECTANGLE_ARB,
                                           (window->colorPrecision == 2) ? GL_RGBA16F : (window->colorPrecision == 1) ? GL_RGB10_A2 : GL_RGBA8,
                                           window->width, window->height,
                                           (window->colorPrecision >= 2) ? GL_RGBA : GL_BGRA,
                                           (window->colorPrecision == 2) ? GL_HALF_FLOAT : (window->colorPrecision == 1) ? GL_UNSIGNED_INT_2_10_10_10_REV : GL_UNSIGNED_INT_8_8_8_8_REV,
                                           ioSurface, 0)) {
                    // Failed!
                    if (verbosity > 0)
                        printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture: CGLTexImageIOSurface2D() failed! IOSurface = %p, texture = %i\n", ioSurface, window->interopHandles.memory);

                    glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
                    glDeleteTextures(1, (GLuint*) &window->interopHandles.memory);
                    window->interopHandles.memory = 0;
                    result = VK_ERROR_INITIALIZATION_FAILED;
                }
                else {
                    // Disable any filtering on texture - It interferes with rendering to it:
                    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                    glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

                    // Done with texture setup. Texture handle window->interopHandles.memory is ready for interop rendering:
                    glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);

                    if (verbosity > 4)
                        printf("PsychVulkanCore-INFO: PsychCreateInteropTexture: Got OpenGL interop texture handle %i [IOSurface %p].\n", window->interopHandles.memory, ioSurface);
                }
            }
            else if (verbosity > 0) {
                printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture: Failed to enable MoltenVK IOSurface backing!\n");
            }
        #endif

        if (result != VK_SUCCESS) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture: Retrieving OpenGL interop handles failed for window %i (res=%i).\n", window->index, result);

            goto out_interop_image;
        }
    }

    // For bringup, write some diagnostic pixel pattern into the interopImage:
    if (requiredMemoryProps & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
        VkImageSubresource subResource = {
            .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
            .mipLevel = 0,
            .arrayLayer = 0,
        };

        VkSubresourceLayout layout;
        uint8_t *data;

        vkGetImageSubresourceLayout(vulkan->device, window->interopImage, &subResource, &layout);

        if (VK_SUCCESS == vkMapMemory(vulkan->device, window->interopImageMemory, 0, memoryAllocInfo.allocationSize, 0, (void*) &data)) {
            size_t j = memoryAllocInfo.allocationSize;
            while (j--) {
                *(data++) = j;
            }

            vkUnmapMemory(vulkan->device, window->interopImageMemory);

            if (verbosity > 3)
                printf("PsychVulkanCore-INFO: PsychCreateInteropTexture: Created test image content for window %i.\n", window->index);
        }
        else {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture:vkMapMemory() failed for window %i (res=%i).\n", window->index, result);

            goto out_interop_image;
        }
    }

    // One-time transition the image layout to something useful for rendering into by the OpenGL client:
    if (!PsychBeginCommandBuffer(window->swapChainCommandBuffers[0])) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture:PsychBeginCommandBuffer() failed for window %i.\n", window->index);

        goto out_interop_image;
    }

    if (!PsychTransitionImageLayout(window->swapChainCommandBuffers[0], window->interopImage, imageCreateInfo.initialLayout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL)) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture:PsychTransitionImageLayout() failed for window %i.\n", window->index);

        goto out_interop_image;
    }

    result = vkEndCommandBuffer(window->swapChainCommandBuffers[0]);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture: vkEndCommandBuffer() failed for window %i (res=%i).\n", window->index, result);

        goto out_interop_image;
    }

    // Submit command for inital interopImage layout transition:
    VkSubmitInfo submitInfo = {
        .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
        .commandBufferCount = 1,
        .pCommandBuffers = window->swapChainCommandBuffers,
    };

    result = vkQueueSubmit(vulkan->graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture: vkQueueSubmit() failed for window %i (res=%i).\n", window->index, result);

        goto out_interop_image;
    }

    // Wait for everything to complete:
    result = vkQueueWaitIdle(vulkan->graphicsQueue);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychCreateInteropTexture: vkQueueWaitIdle() failed for window %i (res=%i).\n", window->index, result);

        goto out_interop_image;
    }

    // We can reset this command buffer, so it can be set up for actual operation for swapchain image 0:
    vkResetCommandBuffer(window->swapChainCommandBuffers[0], VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);

    if (verbosity > 3)
        printf("PsychVulkanCore-INFO: PsychCreateInteropTexture: Success for window %i.\n", window->index);

    return(TRUE);

out_interop_image:

    vkResetCommandBuffer(window->swapChainCommandBuffers[0], VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT);

    vkDestroyImage(vulkan->device, window->interopImage, NULL);
    window->interopImage = VK_NULL_HANDLE;

    vkFreeMemory(vulkan->device, window->interopImageMemory, NULL);
    window->interopImageMemory = VK_NULL_HANDLE;

    return(FALSE);
}

psych_bool PsychRecordCopyCommandBuffer(PsychVulkanWindow* window, int index, psych_bool noInterop)
{
    VkResult result;
    PsychVulkanDevice* vulkan = window->vulkan;
    VkCommandBuffer cmd = window->swapChainCommandBuffers[index];

    // Start the command buffer:
    if (!PsychBeginCommandBuffer(cmd)) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychRecordCopyCommandBuffer(): PsychBeginCommandBuffer() failed for %i'th buffer of window %i.\n", index, window->index);

        return(FALSE);
    }

    if (noInterop) {
        VkImageSubresourceRange range = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
        VkClearColorValue clearColor = { .float32 = { (index > 1) ? 1.0f : index, (index > 1) ? index - 2 : index, (index > 1) ? 0.0f : index, 1.0f } };
        PsychTransitionImageLayout(cmd, window->interopImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
        vkCmdClearColorImage(cmd, window->interopImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clearColor, 1, &range);
        PsychTransitionImageLayout(cmd, window->interopImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
    }

    // Transition target swapChainImage from undefined layout to optimal transfer destination. We do not have any need for the old
    // content of the image buffer, as we will overwrite it with a new image momentarily, so following recommendation in the Vulkan
    // spec, we transition from VK_IMAGE_LAYOUT_UNDEFINED to optimal transfer destination. Transitioning from undefined layout allows
    // the Vulkan driver to simply discard old content - or leave it undefined, and skip an actual conversion operation:
    if (!PsychTransitionImageLayout(cmd, window->swapChainImages[index], VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL)) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychRecordCopyCommandBuffer(): PsychTransitionImageLayout() to copy target failed for %i'th buffer of window %i.\n", index, window->index);

        return(FALSE);
    }

    // DISABLED:
    // Do pixel format of interop texture image and target swapchain image match?
    // if (window->format != window->interopTextureVkFormat) {
    //
    // Always use vkCmdBlitImage(), because defining dstOffsets the way we do
    // allows to flip the image vertically (ie. upside-down) during the blit.
    // This takes the different origin and y-axis direction of OpenGL vs. Vulkan
    // into account and flips the OpenGL source interop image into the upright
    // orientation for Vulkan:
    if (TRUE) {
        // No: Need pixel color format conversion -> blit image:
        VkImageBlit blitRegion = {
            .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
            .srcOffsets = {{ 0, 0, 0 }, { window->width, window->height, 1 }},
            .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
            .dstOffsets = {{ 0, window->height, 0 }, { window->width, 0, 1 }}
        };

        vkCmdBlitImage(cmd, window->interopImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, window->swapChainImages[index], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blitRegion, VK_FILTER_NEAREST);

        if (verbosity > 4)
            printf("PsychVulkanCore-INFO: PsychRecordCopyCommandBuffer(): Swapchainbuffer %i: Using vkCmdBlitImage() blit for interop -> swapchain transfer.\n", index);
    }
    else {
        // Yes: Can do a memcpy() style copy image in VRAM:
        VkImageCopy copyRegion = {
            .srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
            .srcOffset = { 0, 0, 0 },
            .dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
            .dstOffset = { 0, 0, 0 },
            .extent = { window->width, window->height, 1 },
        };

        vkCmdCopyImage(cmd, window->interopImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, window->swapChainImages[index], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copyRegion);

        if (verbosity > 4)
            printf("PsychVulkanCore-INFO: PsychRecordCopyCommandBuffer(): Swapchainbuffer %i: Using vkCmdCopyImage() copy for interop -> swapchain transfer.\n", index);
    }

    // Transition from optimal transfer destination layout to the one needed for onscreen presentation, iow. a scanout capable format:
    if (!PsychTransitionImageLayout(cmd, window->swapChainImages[index], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR)) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychRecordCopyCommandBuffer(): PsychTransitionImageLayout() to present source failed for %i'th buffer of window %i.\n", index, window->index);

        return(FALSE);
    }

    // Done recording the buffer:
    result = vkEndCommandBuffer(cmd);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: PsychRecordCopyCommandBuffer(): vkEndCommandBuffer() failed for %i'th buffer of window %i.\n", index, window->index);

        return(FALSE);
    }

    return(TRUE);
}

// Commit current window->currentDisplayHDRMetadata to display output:
psych_bool PsychSetHDRMetaData(PsychVulkanWindow* window)
{
    if (!window->vulkan->hasHDR) {
        if (verbosity > 1)
            printf("PsychVulkanCore-WARNING: Tried to call PsychSetHDRMetaData() on a window attached to a gpu without HDR support! Ignored.\n");

        return(FALSE);
    }

    if (!window->hdrMode) {
        if (verbosity > 1)
            printf("PsychVulkanCore-WARNING: Tried to call PsychSetHDRMetaData() on a window without HDR enabled! Ignored.\n");

        return(FALSE);
    }

    // Commit to swapChain. The update is supposed to happen atomically at next Present. This function
    // sadly doesn't return a result status code:
    fpSetHdrMetadataEXT(window->vulkan->device, 1, &window->swapChain, &window->currentDisplayHDRMetadata);

    if (verbosity > 4) {
        printf("\n");
        printf("PsychVulkanCore-INFO: Set HDR display properties for window %i, frameIndex %i:\n", window->index, window->frameIndex);
        printf("PsychVulkanCore-INFO: Display Gamut  R: [%f, %f]\n", window->currentDisplayHDRMetadata.displayPrimaryRed.x, window->currentDisplayHDRMetadata.displayPrimaryRed.y);
        printf("PsychVulkanCore-INFO: Display Gamut  G: [%f, %f]\n", window->currentDisplayHDRMetadata.displayPrimaryGreen.x, window->currentDisplayHDRMetadata.displayPrimaryGreen.y);
        printf("PsychVulkanCore-INFO: Display Gamut  B: [%f, %f]\n", window->currentDisplayHDRMetadata.displayPrimaryBlue.x, window->currentDisplayHDRMetadata.displayPrimaryBlue.y);
        printf("PsychVulkanCore-INFO: Display Gamut WP: [%f, %f]\n", window->currentDisplayHDRMetadata.whitePoint.x, window->currentDisplayHDRMetadata.whitePoint.y);
        printf("PsychVulkanCore-INFO: Display minLuminance: %f nits\n", window->currentDisplayHDRMetadata.minLuminance);
        printf("PsychVulkanCore-INFO: Display maxLuminance: %f nits\n", window->currentDisplayHDRMetadata.maxLuminance);
        printf("PsychVulkanCore-INFO: Content maxFrameAverageLightLevel: %f nits\n", window->currentDisplayHDRMetadata.maxFrameAverageLightLevel);
        printf("PsychVulkanCore-INFO: Content maxContentLightLevel: %f nits\n", window->currentDisplayHDRMetadata.maxContentLightLevel);
        printf("\n");
    }

    return(TRUE);
}

psych_bool PsychOpenVulkanWindow(PsychVulkanWindow* window, int gpuIndex, psych_uint8* targetdeviceUUID, psych_bool isFullscreen, int screenId, void* outputHandle, PsychRectType rect, int colorPrecision, int hdrMode, double refreshHz, int colorSpace, int colorFormat, int flags)
{
    VkResult result;
    int i;
    psych_bool needsTiming = TRUE;
    psych_bool supportsPresent;
    PsychVulkanDevice* vulkan;
    psych_bool rc = FALSE;
    psych_bool noDirectToDisplay = ((PSYCH_SYSTEM == PSYCH_OSX) && (flags & 0x2)) ? TRUE : FALSE;

    // Any HDR mode requested?
    if (hdrMode > 0) {
        // HDR requires at least 10 bpc framebuffer precision, so enforce at least
        // colorPrecision level 1:
        if (colorPrecision < 1)
            colorPrecision = 1;
    }

    // Automatic colorSpace selection, unless colorSpace already defined by caller:
    if (colorSpace == 0) {
        switch(hdrMode) {
            case 1:
                // HDR-10 BT2020 color space with ST-2084 PQ "Perceptual Quantizer" OETF:
                colorSpace = VK_COLOR_SPACE_HDR10_ST2084_EXT;

                // Unless this is MS-Windows, and fp16 float precision is requested,
                // or windowed non-fullscreen HDR is requested. In that case we need
                // to use the scRGB colorspace, because only scRGB supports fp16
                // consistently across multiple gpu vendors, and only scRGB is
                // supported for windowed HDR, ie. when the DWM compositor is responsible
                // for SDR + HDR compositing. We also use scRGB + fp16 on macOS:
                if (((PSYCH_SYSTEM == PSYCH_WINDOWS) && (!isFullscreen || (colorPrecision > 1))) || (PSYCH_SYSTEM == PSYCH_OSX)) {
                    colorSpace = VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT;

                    // The scRGB colorspace only works with exactly fp16 == precision 2:
                    colorPrecision = 2;
                }

                break;

            case 0:
                // Standard dynamic range SDR default Vulkan SRGB color space and standard
                // transfer function:
                colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
                break;

            default:
                colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
        }
    }

    window->isFullscreen = isFullscreen;
    window->createFlags = flags;

    // Specific gpuIndex requested?
    if (gpuIndex > 0) {
        psych_uint8 allzeros[16] = { 0 };

        // Yes. Choose this physical gpu for the window:
        vulkan = PsychGetVulkan(gpuIndex, FALSE);

        if (verbosity > 3)
            printf("PsychVulkanCore-INFO: gpuIndex %i [%s] manually selected as gpu for presenting to the target surface.\n", gpuIndex, vulkan->deviceProps.deviceName);

        // Map a all-zero targetdeviceUUID to a matching UUID, iow. make sure it gets ignored for compatibility matching:
        if (memcmp(targetdeviceUUID, allzeros, 16) == 0)
            memcpy(targetdeviceUUID, &vulkan->physDeviceProps.deviceUUID[0], 16);

        if (!PsychIsVulkanGPUSuitable(window, vulkan, targetdeviceUUID, isFullscreen, screenId, outputHandle, rect, refreshHz, hdrMode, needsTiming, colorPrecision, colorSpace, colorFormat, flags)) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: Creating vulkan output window failed. Selected gpu %i '%s' unsuitable for requested settings.\n", gpuIndex, vulkan->deviceProps.deviceName);

            goto openwindow_out2;
        }

        supportsPresent = TRUE;
    }
    else {
        // No. Try to auto-detect the proper gpuIndex / gpu by probing:
        for (gpuIndex = 1; gpuIndex <= physicalGpuCount; gpuIndex++) {
            vulkan = PsychGetVulkan(gpuIndex, FALSE);
            supportsPresent = PsychIsVulkanGPUSuitable(window, vulkan, targetdeviceUUID, isFullscreen, screenId, outputHandle, rect, refreshHz, hdrMode, needsTiming, colorPrecision, colorSpace, colorFormat, flags);

            if (verbosity > 3)
                printf("PsychVulkanCore-INFO: Does gpu %i [%s] meet our requirements for target surface: %s\n", gpuIndex, vulkan->deviceProps.deviceName, supportsPresent ? "Yes" : "No");

            if (supportsPresent)
                break;
        }

        if (!supportsPresent) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: Could not find any suitable gpu to present to given window for requested settings!\n");

            goto openwindow_out2;
        }

        if (verbosity > 3)
            printf("PsychVulkanCore-INFO: gpuIndex %i [%s] auto-selected as optimal gpu for presenting to the target surface.\n", gpuIndex, vulkan->deviceProps.deviceName);
    }

    if (!supportsPresent)
        goto openwindow_out2;

    // Assign vulkan device to this window:
    window->vulkan = vulkan;

    // Create a swapchain for the windows surface:

    // Select size of swapchain in pixels:
    VkExtent2D swapchainExtent;

    if (window->surfaceCapabilities.currentExtent.width == 0xFFFFFFFF) {
        // Swapchain pixel size undefined. Assign our window size:
        swapchainExtent.width = rect[kPsychRight] - rect[kPsychLeft];
        swapchainExtent.height = rect[kPsychBottom] - rect[kPsychTop];

        // Clamp extent to surface limits:
        if (swapchainExtent.width < window->surfaceCapabilities.minImageExtent.width)
            swapchainExtent.width = window->surfaceCapabilities.minImageExtent.width;

        if (swapchainExtent.width > window->surfaceCapabilities.maxImageExtent.width)
            swapchainExtent.width = window->surfaceCapabilities.maxImageExtent.width;

        if (swapchainExtent.height < window->surfaceCapabilities.minImageExtent.height)
            swapchainExtent.height = window->surfaceCapabilities.minImageExtent.height;

        if (swapchainExtent.height > window->surfaceCapabilities.maxImageExtent.height)
            swapchainExtent.height = window->surfaceCapabilities.maxImageExtent.height;
    }
    else {
        // Swapchain size defined. Take "as is":
        swapchainExtent = window->surfaceCapabilities.currentExtent;
    }

    // Assign size to window:
    window->width = swapchainExtent.width;
    window->height = swapchainExtent.height;

    // Select number of image buffers:
    uint32_t numBuffers = window->surfaceCapabilities.minImageCount;

    if (numBuffers != 2 && window->surfaceCapabilities.minImageCount <= 2)
        numBuffers = 2;

    if ((numBuffers > window->surfaceCapabilities.maxImageCount) && (window->surfaceCapabilities.maxImageCount != 0))
        numBuffers = window->surfaceCapabilities.maxImageCount;

    if (numBuffers > MAX_BUFFERS) {
        if (verbosity > 1)
            printf("PsychVulkanCore-WARNING: Window %i had more than max allowed buffers (numBuffers=%i vs. MAX_BUFFERS %i). Clamping to MAX_BUFFERS!\n", window->index, numBuffers, MAX_BUFFERS);
        numBuffers = MAX_BUFFERS;
    }

    if ((numBuffers != 2) && (verbosity > 1))
        printf("PsychVulkanCore-WARNING: Window %i does not support strict double-buffering (numBuffers=%i). Expect timing trouble!\n", window->index, numBuffers);

    window->numBuffers = 0;

    // Selection of proper swapchain format:
    window->format = VK_FORMAT_UNDEFINED;

    switch (colorPrecision) {
        case 0: // 8 bpc standard precision:
            // Prefer RGBA8 ordering if supported, as that matches OpenGL RGBA8 for most efficient interop:
            if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_R8G8B8A8_UNORM)) {
                window->format = VK_FORMAT_R8G8B8A8_UNORM;
                break;
            }
            else if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_B8G8R8A8_UNORM) && !noDirectToDisplay) {
                window->format = VK_FORMAT_B8G8R8A8_UNORM; // This is our slightly less efficient fallback, except on macOS Direct-To-Display.
                break;
            }

            // Note that in direct display mode on Linux, NVidia's blob supports VK_FORMAT_A8B8G8R8_UNORM_PACK32
            // as swapChain format, instead of the above formats. However, this format does not work for OpenGL
            // interop for unknown reasons, maybe a NVidia driver bug. Therefore we don't try to use it here.

            // None of the above 8 bpc formats supported! Or on macOS we want to avoid Direct-To-Display to
            // work around macOS operating system bugs.
            // Fallthrough to case 1 RGB10A2 iff none of the above 8 bpc format is supported, as 10 bpc
            // formats are a valid fallback in that case. Potentially less efficient, but compatible in
            // the way they represent color values.

        case 1: // RGB10A2 10 bpc precision:
            // Prefer RGBA ordering if supported, as that matches OpenGL RGB10A2 for most efficient interop,
            // except on macOS, where we need to choose BGR10A2 for optimal interop and if we want Direct-To-Display mode for optimum performance:
            if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_A2B10G10R10_UNORM_PACK32) && ((PSYCH_SYSTEM != PSYCH_OSX) || noDirectToDisplay))
                window->format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
            else if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_A2R10G10B10_UNORM_PACK32))
                window->format = VK_FORMAT_A2R10G10B10_UNORM_PACK32; // This is our slightly less efficient fallback.
            break;

        case 2: // RGBA16F fp 16 floating point precision. This is about 11 bpc for the unorm range 0.0 - 1.0:
            if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_R16G16B16A16_SFLOAT))
                window->format = VK_FORMAT_R16G16B16A16_SFLOAT;
            break;

        case 3: // RGBA16 16 bpc unsigned normalized unorm range 0.0 - 1.0. Up to 16 bit true precision:
            if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_R16G16B16A16_UNORM))
                window->format = VK_FORMAT_R16G16B16A16_UNORM;
            break;

        case 4: // Try all deep-color unorm precisions from highest to lowest: RGBA16 -> RGB10A2:
        case 5: // Try all deep-color precisions (unorm and fp16) from highest to lowest: RGBA16 -> RGBA16F -> RGB10A2:
            // First try RGBA16 16 bpc unsigned normalized unorm range 0.0 - 1.0. Up to 16 bit true precision:
            if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_R16G16B16A16_UNORM)) {
                window->format = VK_FORMAT_R16G16B16A16_UNORM;
                break;
            }

            // Case 5, also use fp16?
            if ((colorPrecision == 5) && (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_R16G16B16A16_SFLOAT))) {
                window->format = VK_FORMAT_R16G16B16A16_SFLOAT;
                break;
            }

            // Fall back to 10 bpc formats:

            // Prefer RGBA ordering if supported, as that matches OpenGL RGB10A2 for most efficient interop:
            if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_A2B10G10R10_UNORM_PACK32)) {
                window->format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
                break;
            }

            // Fall back to BGRA 10 bpc ordering:
            if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_A2R10G10B10_UNORM_PACK32)) {
                window->format = VK_FORMAT_A2R10G10B10_UNORM_PACK32; // This is our slightly less efficient fallback.
                break;
            }

            break;

        case 6: // Try fp16, then 10 bpc unorm: RGBA16F -> RGB10A2:
            if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_R16G16B16A16_SFLOAT)) {
                window->format = VK_FORMAT_R16G16B16A16_SFLOAT;
                break;
            }

            // Fall back to 10 bpc formats:

            // Prefer RGBA ordering if supported, as that matches OpenGL RGB10A2 for most efficient interop:
            if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_A2B10G10R10_UNORM_PACK32)) {
                window->format = VK_FORMAT_A2B10G10R10_UNORM_PACK32;
                break;
            }

            // Fall back to BGRA 10 bpc ordering:
            if (PsychIsColorSpaceFormatComboSupported(window, colorSpace, VK_FORMAT_A2R10G10B10_UNORM_PACK32)) {
                window->format = VK_FORMAT_A2R10G10B10_UNORM_PACK32; // This is our slightly less efficient fallback.
                break;
            }

            break;

        default:
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: Unsupported / Unknown color precision type %i requested!\n", colorPrecision);
            goto openwindow_out1;
    }

    // Got something usable?
    if (window->format == VK_FORMAT_UNDEFINED) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: Requested color precision type %i unsupported by the gpuIndex %i [%s]!\n", colorPrecision, gpuIndex, vulkan->deviceProps.deviceName);
        goto openwindow_out1;
    }

    // Assign colorspace to window:
    window->colorspace = colorSpace;

    VkSwapchainCreateInfoKHR swapChainCreateInfo = { 0 };
    swapChainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    swapChainCreateInfo.surface = window->surface;
    swapChainCreateInfo.minImageCount = numBuffers;
    swapChainCreateInfo.imageFormat = window->format;
    swapChainCreateInfo.imageColorSpace = colorSpace;
    swapChainCreateInfo.imageExtent = swapchainExtent;
    swapChainCreateInfo.imageArrayLayers = 1;
    swapChainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
    swapChainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
    swapChainCreateInfo.queueFamilyIndexCount = 0;
    swapChainCreateInfo.pQueueFamilyIndices = NULL;
    swapChainCreateInfo.preTransform = window->surfaceCapabilities.currentTransform;
    swapChainCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
    swapChainCreateInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR;
    swapChainCreateInfo.clipped = VK_TRUE;
    swapChainCreateInfo.oldSwapchain = VK_NULL_HANDLE;

    #if PSYCH_SYSTEM == PSYCH_WINDOWS
    swapChainCreateInfo.pNext = (window->isFullscreen) ? &fullscreenExclusiveInfo : NULL;
    #endif

    // Map swapchain format to a compatible OpenGL supported format for interop:
    switch(swapChainCreateInfo.imageFormat) {
        case VK_FORMAT_B8G8R8A8_UNORM:              // RGBA8 interop -> BGRA8 swapchain swizzled, except on macOS:
            window->interopTextureVkFormat = (PSYCH_SYSTEM == PSYCH_OSX) ? VK_FORMAT_B8G8R8A8_UNORM : VK_FORMAT_R8G8B8A8_UNORM;
            window->colorPrecision = 0;

            if (verbosity > 3)
                printf("PsychVulkanCore-INFO: Using 8 bpc unorm [0; 1] range RGBA8 framebuffer.\n");
            break;

        case VK_FORMAT_R8G8B8A8_UNORM:              // RGBA8 interop -> RGBA8 swapchain.
            window->interopTextureVkFormat = VK_FORMAT_R8G8B8A8_UNORM;
            window->colorPrecision = 0;

            if (verbosity > 3)
                printf("PsychVulkanCore-INFO: Using 8 bpc unorm [0; 1] range RGBA8 framebuffer.\n");
            break;

        case VK_FORMAT_A2R10G10B10_UNORM_PACK32: // RGB10A2 interop -> BGR10A2 swapchain swizzled, except on macOS:
            window->interopTextureVkFormat = (PSYCH_SYSTEM == PSYCH_OSX) ? VK_FORMAT_A2R10G10B10_UNORM_PACK32 : VK_FORMAT_A2B10G10R10_UNORM_PACK32;
            window->colorPrecision = 1;

            if (verbosity > 3)
                printf("PsychVulkanCore-INFO: Using 10 bpc unorm [0; 1] range RGB10A2 framebuffer.\n");
            break;

        case VK_FORMAT_A2B10G10R10_UNORM_PACK32: // RGB10A2 interop -> RGB10A2 swapchain, except on macOS where we need to swizzle:
            if ((PSYCH_SYSTEM == PSYCH_OSX) && (colorPrecision == 0) && noDirectToDisplay) {
                // Special case: macOS noDirectToDisplay hack, where we wanted a 8 bpc fb, but can't use it,
                // as it would trigger macOS Metal bugs. We allocate a 10 bpc swapchain buffer, but setup the
                // interop texture as 8 bpc, so Screen's imaging pipeline can potentially avoid an unneeded
                // drawBufferFBO->finalizedFBO unsharing, which would cause a performance hit:
                window->interopTextureVkFormat = VK_FORMAT_B8G8R8A8_UNORM;
                window->colorPrecision = 0;
            }
            else {
                // Standard 10 bpc use case:
                window->interopTextureVkFormat = (PSYCH_SYSTEM == PSYCH_OSX) ? VK_FORMAT_A2R10G10B10_UNORM_PACK32 : VK_FORMAT_A2B10G10R10_UNORM_PACK32;
                window->colorPrecision = 1;
            }

            if (verbosity > 3)
                printf("PsychVulkanCore-INFO: Using 10 bpc unorm [0; 1] range RGB10A2 framebuffer.\n");
            break;

        case VK_FORMAT_R16G16B16A16_SFLOAT: // RGBA16F interop -> RGBA16F swapchain.
            window->interopTextureVkFormat = VK_FORMAT_R16G16B16A16_SFLOAT;
            window->colorPrecision = 2;

            if (verbosity > 3)
                printf("PsychVulkanCore-INFO: Using 16 bit (~11 bpc unorm linear precision equivalent) fp16 half-float range RGBA16F framebuffer.\n");
            break;

        case VK_FORMAT_R16G16B16A16_UNORM: // RGBA16 interop -> RGBA16 swapchain.
            window->interopTextureVkFormat = VK_FORMAT_R16G16B16A16_UNORM;
            window->colorPrecision = 3;

            if (verbosity > 3)
                printf("PsychVulkanCore-INFO: Using 16 bpc unorm [0; 1] range RGBA16 framebuffer.\n");
            break;

        default:
            printf("PsychVulkanCore-CRITICAL: Unrecognized swapChain format 0x%x for window %i! Driver bug?!?\n", swapChainCreateInfo.imageFormat, window->index);
            goto openwindow_out1;
    }

    result = vkCreateSwapchainKHR(vulkan->device, &swapChainCreateInfo, NULL, &window->swapChain);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: vkCreateSwapchainKHR() failed for window %i: res=%i.\n", window->index, result);

        goto openwindow_out1;
    }

    // Some time granted to GUI event dispatch:
    PsychProcessWindowEvents(window);

    #if defined(VK_USE_PLATFORM_WIN32_KHR)
        if (isFullscreen && !(window->createFlags & 0x2)) {
            // Switch swapChain into fullScreenExclusive mode:
            int retries;

            // Retry to go fullscreen exclusive up to 10 times:
            result = VK_ERROR_INITIALIZATION_FAILED;
            for (retries = 0; (retries < 10) && (result == VK_ERROR_INITIALIZATION_FAILED); retries++) {
                result = fpAcquireFullScreenExclusiveModeEXT(vulkan->device, window->swapChain);
                if (result != VK_SUCCESS) {
                    if (verbosity > 0) {
                        if (result == VK_ERROR_INITIALIZATION_FAILED)
                            printf("PsychVulkanCore-ERROR: gpu [%s] Could not switch to fullscreen exclusive mode [Attempt nr. %i]!\n", vulkan->deviceProps.deviceName, retries + 1);
                        else
                            printf("PsychVulkanCore-ERROR: gpu [%s] Error during switch to fullscreen exclusive mode [Attempt nr. %i]: %i\n", vulkan->deviceProps.deviceName, result + 1);
                    }
                }
                else if (verbosity > 3)
                    printf("PsychVulkanCore-INFO: For gpu [%s] switched to fullscreen exclusive display mode for swapChain [%p] of display window %i\n", vulkan->deviceProps.deviceName, window->swapChain, window->index);

                PsychProcessWindowEvents(window);
                PsychYieldIntervalSeconds(0.050);
            }

            // Still no success?
            if (result != VK_SUCCESS) {
                // This is not a fatal error, but it may impair timing and reliability:
                if (verbosity > 0)
                    printf("PsychVulkanCore-ERROR: Multiple retries failed. Fallback to non-exclusive mode. Visual timing precision, robustness, and general reliability may be degraded!\n");

                // Destroy and recreate swapChain without fullscreen exclusive access:
                vkDestroySwapchainKHR(vulkan->device, window->swapChain, NULL);
                window->swapChain = (VkSwapchainKHR) VK_NULL_HANDLE;
                swapChainCreateInfo.pNext = NULL;

                result = vkCreateSwapchainKHR(vulkan->device, &swapChainCreateInfo, NULL, &window->swapChain);
                if (result != VK_SUCCESS) {
                    if (verbosity > 0)
                        printf("PsychVulkanCore-ERROR: vkCreateSwapchainKHR() failed for window %i in non-FS-except mode: res=%i.\n", window->index, result);

                    goto openwindow_out1;
                }
            }
        }
        else if (isFullscreen && verbosity > 1) {
            printf("PsychVulkanCore-WARNING: For gpu [%s] did *not* switch to fullscreen exclusive display mode for fullscreen display window %i,\n", vulkan->deviceProps.deviceName, window->index);
            printf("PsychVulkanCore-WARNING: because of user request, or due to automatic workaround for buggy Vulkan driver.\n");
            printf("PsychVulkanCore-WARNING: Visual timing precision and robustness, as well as general stimulus reliability, may be degraded!\n");
        }
    #endif

    result = vkGetSwapchainImagesKHR(vulkan->device, window->swapChain, &window->numBuffers, NULL);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: vkGetSwapchainImagesKHR() failed for window %i: res=%i.\n", window->index, result);

        goto openwindow_out1;
    }

    if ((window->numBuffers != numBuffers) && (verbosity > 1))
        printf("PsychVulkanCore-WARNING: Window %i has different number of swapchain images %i than requested %i. Expect timing trouble!\n",
               window->index, window->numBuffers, numBuffers);

    window->swapChainImages = (VkImage*) malloc(window->numBuffers * sizeof(VkImage));

    result = vkGetSwapchainImagesKHR(vulkan->device, window->swapChain, &window->numBuffers, window->swapChainImages);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: vkGetSwapchainImagesKHR() of %i images failed for window %i: res=%i.\n", window->numBuffers, window->index, result);

        goto openwindow_out1;
    }

    const VkSemaphoreCreateInfo semaphoreCreateInfo = {
        .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
    };

    // Create semaphores for each swapchain image:
    for (i = 0; i < window->numBuffers; i++) {
        result = vkCreateSemaphore(vulkan->device, &semaphoreCreateInfo, NULL, &window->imageAcquiredSemaphores[i]);
        if (result != VK_SUCCESS) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: vkCreateSemaphore() for %i'th swapchain image acquire of %i failed for window %i: res=%i.\n", i, window->numBuffers, window->index, result);

            goto openwindow_out1;
        }

        result = vkCreateSemaphore(vulkan->device, &semaphoreCreateInfo, NULL, &window->imageCopyDoneSemaphores[i]);
        if (result != VK_SUCCESS) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: vkCreateSemaphore() for %i'th swapchain image copy done of %i failed for window %i: res=%i.\n", i, window->numBuffers, window->index, result);

            goto openwindow_out1;
        }
    }

    const VkFenceCreateInfo fenceCreateInfo = {
        .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
    };

    // Create flipDoneFence:
    result = vkCreateFence(vulkan->device, &fenceCreateInfo, NULL, &window->flipDoneFence);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: vkCreateFence() for failed for window %i: res=%i.\n", window->index, result);

        goto openwindow_out1;
    }

    // Create command buffers, one for each swapchain image, to store the interop -> swapchain copy/blit commands
    // and layout transitions:
    VkCommandBufferAllocateInfo commandBufferInfo = {
        .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
        .pNext = NULL,
        .commandPool = vulkan->commandPool,
        .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
        .commandBufferCount = window->numBuffers,
    };

    result = vkAllocateCommandBuffers(vulkan->device, &commandBufferInfo, &window->swapChainCommandBuffers[0]);
    if (result != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: vkAllocateCommandBuffers() for command buffers for %i swapchain images failed for window %i: res=%i.\n", window->numBuffers, window->index, result);

        goto openwindow_out1;
    }

    // Create OpenGL -> Vulkan interop texture image:
    VkFormatProperties formatProps;
    VkFormatFeatureFlags requiredMask = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT;
    vkGetPhysicalDeviceFormatProperties(vulkan->physicalDevice, window->interopTextureVkFormat, &formatProps);

    switch (vulkan->deviceProps.vendorID) {
        case 0x8086: // Intel gpu: Verified to work with tiled rendering on Iris as of Mesa 21.0.0-devel + OpenGL interop patchset.
        case 0x10de: // NVidia gpu: At least proprietary drivers allow tiled rendering:
        case 0x14e4: // Broadcom gpu: Verified to work with tiled rendering on VideoCore-6 / RaspberryPi 4/400.
            if ((formatProps.optimalTilingFeatures & requiredMask) == requiredMask) {
                window->interopTextureTiled = TRUE;
                break;
            }
            // If tiling is not supported for some reason, fallthrough to linear mode:

        case 0x1002: // AMD gpu: AMD OpenGL drivers currently only allow rendering into linear non-tiled interop texture.
        default:
            if ((formatProps.linearTilingFeatures & requiredMask) == requiredMask) {
                window->interopTextureTiled = FALSE;
            }

            break;
    }

    // macOS MoltenVK can only do tiled rendering:
    if ((PSYCH_SYSTEM == PSYCH_OSX) && ((formatProps.optimalTilingFeatures & requiredMask) == requiredMask))
        window->interopTextureTiled = TRUE;

    if (!PsychCreateInteropTexture(window)) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: Creating OpenGL -> Vulkan interop texture failed for window %i.\n", window->index);

        goto openwindow_out1;
    }

    // Build command buffers for interop image -> swapchain copy/blit operation:
    for (i = 0; i < window->numBuffers; i++) {
        // flags bit 0 (value 0x1) set means to disable interop and just draw hard-coded test patterns:
        if (!PsychRecordCopyCommandBuffer(window, i, flags & 0x1)) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: Building command buffer for %i'th swapchain image copy/blit out of %i failed for window %i: res=%i.\n", i, window->numBuffers, window->index, result);

            goto openwindow_out1;
        }
    }

    // Some time granted to GUI event dispatch:
    PsychProcessWindowEvents(window);

    // Prep work for first true Present - One-time warmup after creating the swapChain.
    // Try to acquire next target swapchain buffer window->currentSwapChainBuffer will
    // contain the index of the proper swapChainImage:
    if (PsychGetNextSwapChainTargetBuffer(window) != VK_SUCCESS) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: Warmup stage 1 failed for window %i.\n", window->index);

        goto openwindow_out1;
    }

    if (!PsychWaitForPresentCompletion(window)) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: Warmup stage 2 failed for window %i.\n", window->index);

        goto openwindow_out1;
    }

    // Enable HDR metadata packet transmission to display output:
    window->hdrMode = hdrMode;
    if (hdrMode && !PsychSetHDRMetaData(window)) {
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: Failed to enable HDR mode %i for window %i.\n", hdrMode, window->index);

        goto openwindow_out1;
    }

    // Report nominal refresh rate of display if this is supported:
    if ((verbosity > 3) && vulkan->hasTiming && fpGetRefreshCycleDurationGOOGLE) {
        VkRefreshCycleDurationGOOGLE refreshDur;
        if (VK_SUCCESS == fpGetRefreshCycleDurationGOOGLE(vulkan->device, window->swapChain, &refreshDur)) {
            printf("PsychVulkanCore-INFO: Vulkan reports nominal refresh rate %f Hz for display associated with window %i.\n", 1.0e9 / (double) refreshDur.refreshDuration, window->index);
        } else {
            printf("PsychVulkanCore-INFO: vkGetRefreshCycleDurationGOOGLE() for window %i failed.\n", window->index);
        }
    }

    // Success: Mark it as such:
    rc = TRUE;

    window->isValid = TRUE;

    // Some time granted to GUI event dispatch:
    PsychProcessWindowEvents(window);

    return(TRUE);

openwindow_out1:

    // Free all command buffers:
    vkFreeCommandBuffers(vulkan->device, vulkan->commandPool, window->numBuffers, &window->swapChainCommandBuffers[0]);

    vkDestroyImage(vulkan->device, window->interopImage, NULL);
    window->interopImage = VK_NULL_HANDLE;

    vkFreeMemory(vulkan->device, window->interopImageMemory, NULL);
    window->interopImageMemory = VK_NULL_HANDLE;

    vkDestroyFence(vulkan->device, window->flipDoneFence, NULL);

    for (i = 0; i < MAX_BUFFERS; i++) {
        vkDestroySemaphore(vulkan->device, window->imageAcquiredSemaphores[i], NULL);
        window->imageAcquiredSemaphores[i] = VK_NULL_HANDLE;

        vkDestroySemaphore(vulkan->device, window->imageCopyDoneSemaphores[i], NULL);
        window->imageCopyDoneSemaphores[i] = VK_NULL_HANDLE;
    }

    if (window->swapChainImages) {
        free(window->swapChainImages);
        window->swapChainImages = NULL;
        window->numBuffers = 0;
    }

    vkDestroySwapchainKHR(vulkan->device, window->swapChain, NULL);
    window->swapChain = (VkSwapchainKHR) VK_NULL_HANDLE;

openwindow_out2:

    vkDestroySurfaceKHR(vulkanInstance, window->surface, NULL);
    window->surface = (VkSurfaceKHR) VK_NULL_HANDLE;

    if (window->display != VK_NULL_HANDLE) {
        fpReleaseDisplayEXT(vulkan->physicalDevice, window->display);
        window->display = VK_NULL_HANDLE;
    }

#if defined(VK_USE_PLATFORM_XLIB_KHR)
    if (window->connection) {
        if (window->x11PrivateWindow != None) {
            XUnmapWindow(window->connection, window->x11PrivateWindow);
            XDestroyWindow(window->connection, window->x11PrivateWindow);
            XFlush(window->connection);
            window->x11PrivateWindow = None;
        }

        XCloseDisplay(window->connection);
        window->connection = NULL;
    }
#endif

#if defined(VK_USE_PLATFORM_WIN32_KHR)
    if (window->win32PrivateWindow) {
        DestroyWindow(window->win32PrivateWindow);
        window->win32PrivateWindow = NULL;
    }
#endif

    return (rc);
}

psych_bool PsychCloseVulkanWindow(PsychVulkanWindow* window)
{
    int i;

    if (!window->isValid)
        return(TRUE);

    if (verbosity > 3) {
        printf("PsychVulkanCore-INFO: Closing Vulkan window %i.\n", window->index);
        fflush(NULL);
    }

    // Clean up / Reset from previous call if needed:
    if (window->surfaceFormats) {
        free(window->surfaceFormats);
        window->surfaceFormats = NULL;
        window->surfaceFormatCount = 0;
    }

    // Some time granted to GUI event dispatch:
    PsychProcessWindowEvents(window);

    vkDeviceWaitIdle(window->vulkan->device);

    // Free all command buffers:
    vkFreeCommandBuffers(window->vulkan->device, window->vulkan->commandPool, window->numBuffers, &window->swapChainCommandBuffers[0]);

    vkDestroyImage(window->vulkan->device, window->interopImage, NULL);
    window->interopImage = VK_NULL_HANDLE;

    // Do we still own the interop handle for memory?
    if (window->interopHandles.memory != 0) {
        // Need to release it:
        if (verbosity > 4) {
            printf("PsychVulkanCore-INFO: Vulkan window %i: Releasing image memory interop handle.\n", window->index);
        }

        #if PSYCH_SYSTEM == PSYCH_LINUX
            close(window->interopHandles.memory);
        #endif

        #if PSYCH_SYSTEM == PSYCH_WINDOWS
            CloseHandle(window->interopHandles.memory);
        #endif

        #if PSYCH_SYSTEM == PSYCH_OSX
            glDeleteTextures(1, (GLuint*) &window->interopHandles.memory);
        #endif

        window->interopHandles.memory = 0;
    }

    vkFreeMemory(window->vulkan->device, window->interopImageMemory, NULL);
    window->interopImageMemory = VK_NULL_HANDLE;

    vkDestroyFence(window->vulkan->device, window->flipDoneFence, NULL);

    vkDestroySemaphore(window->vulkan->device, window->interopRenderDoneSemaphore, NULL);
    window->interopRenderDoneSemaphore = VK_NULL_HANDLE;

    for (i = 0; i < MAX_BUFFERS; i++) {
        vkDestroySemaphore(window->vulkan->device, window->imageAcquiredSemaphores[i], NULL);
        window->imageAcquiredSemaphores[i] = VK_NULL_HANDLE;

        vkDestroySemaphore(window->vulkan->device, window->imageCopyDoneSemaphores[i], NULL);
        window->imageCopyDoneSemaphores[i] = VK_NULL_HANDLE;
    }

    if (window->swapChainImages) {
        free(window->swapChainImages);
        window->swapChainImages = NULL;
        window->numBuffers = 0;
    }

    vkDestroySwapchainKHR(window->vulkan->device, window->swapChain, NULL);
    window->swapChain = (VkSwapchainKHR) VK_NULL_HANDLE;

    vkDeviceWaitIdle(window->vulkan->device);

    if (verbosity > 4) {
        printf("PsychVulkanCore-INFO: Vulkan window %i: swapChain is gone. Releasing display.\n", window->index);
        fflush(NULL);
    }

    if (window->display != VK_NULL_HANDLE) {
        fpReleaseDisplayEXT(window->vulkan->physicalDevice, window->display);
        window->display = VK_NULL_HANDLE;
    }

    #if defined(VK_USE_PLATFORM_XLIB_KHR)
    if (window->connection) {
        if (window->x11PrivateWindow != None) {
            XUnmapWindow(window->connection, window->x11PrivateWindow);
            XDestroyWindow(window->connection, window->x11PrivateWindow);
            XFlush(window->connection);
            window->x11PrivateWindow = None;
        }

        XCloseDisplay(window->connection);
        window->connection = NULL;
    }
    #endif

    #if defined(VK_USE_PLATFORM_WIN32_KHR)
    if (window->win32PrivateWindow) {
        DestroyWindow(window->win32PrivateWindow);
        window->win32PrivateWindow = NULL;
    }
    #endif

    if (verbosity > 4) {
        printf("PsychVulkanCore-INFO: Vulkan window %i: Display or Window is gone. Releasing surface.\n", window->index);
        fflush(NULL);
    }

    vkDestroySurfaceKHR(vulkanInstance, window->surface, NULL);
    window->surface = (VkSurfaceKHR) VK_NULL_HANDLE;

    vkDeviceWaitIdle(window->vulkan->device);

    if (verbosity > 4) {
        printf("PsychVulkanCore-INFO: Vulkan window %i: Window successfully closed.\n", window->index);
        fflush(NULL);
    }

    // Invalidate window -- Mark it as dead in the window array:
    window->isValid = FALSE;

    // One less:
    windowCount--;

    return(TRUE);
}

void PsychCloseAllVulkanWindows(void)
{
    int handle;
    PsychVulkanWindow* window;

    for (handle = 0; handle < MAX_PSYCH_VULKAN_WINDOWS; handle++) {
        window = PsychGetVulkanWindow(handle, TRUE);
        if (window)
            PsychCloseVulkanWindow(window);
    }
}

PsychError PSYCHVULKANVerbosity(void)
{
    static char useString[] = "oldVerbosity = PsychVulkanCore('Verbosity' [, verbosity]);";
    static char synopsisString[] =
        "Returns and optionally sets level of 'verbosity' for driver debug output.\n"
        "'verbosity' = New level of verbosity: 0 = Silent, 1 = Errors only, 2 = Warnings, 3 = Info, 4 = Debug.\n";
    static char seeAlsoString[] = "";

    // All sub functions should have these two lines
    PsychPushHelp(useString, synopsisString,seeAlsoString);
    if (PsychIsGiveHelp()) { PsychGiveHelp(); return(PsychError_none); };

    // Check to see if the user supplied superfluous arguments
    PsychErrorExit(PsychCapNumOutputArgs(1));
    PsychErrorExit(PsychCapNumInputArgs(1));

    // Return optional old verbosity setting:
    PsychCopyOutDoubleArg(1, kPsychArgOptional, verbosity);

    // Get optional new verbosity setting:
    PsychCopyInIntegerArg(1, kPsychArgOptional, &verbosity);

    return(PsychError_none);
}

PsychError PSYCHVULKANGetCount(void)
{
    static char useString[] = "numDevices = PsychVulkanCore('GetCount');";
    static char synopsisString[] =
        "Returns count of currently available Vulkan driver + GPU combos.\n";
    static char seeAlsoString[] = "GetDevices";

    // All sub functions should have these two lines
    PsychPushHelp(useString, synopsisString, seeAlsoString);
    if (PsychIsGiveHelp()) { PsychGiveHelp(); return(PsychError_none); };

    // Check to see if the user supplied superfluous arguments
    PsychErrorExit(PsychCapNumOutputArgs(1));
    PsychErrorExit(PsychCapNumInputArgs(0));

    PsychVulkanCheckInit(TRUE);

    PsychCopyOutDoubleArg(1, kPsychArgOptional, physicalGpuCount);

    return(PsychError_none);
}

PsychError PSYCHVULKANGetDevices(void)
{
    static char useString[] = "devices = PsychVulkanCore('GetDevices');";
    static char synopsisString[] =
    "Returns an array of structs enumerating all currently available Vulkan driver + GPU combos.\n"
    "Each struct in the array describes one combination of Vulkan driver and device, with the "
    "following fields:\n\n"
    "'DeviceIndex' = Index of the device. Can be used in 'OpenWindow' as 'gpuIndex'.\n"
    "'GpuName' = Name of the Vulkan device / graphics card.\n"
    "'GpuDriver' = Name of the Vulkan driver associated with the device.\n"
    "'DriverInfo' = Additional info associated with the driver, vendor specific.\n"
    "'DriverVersion' = Major.minor.point version string with the driver version.\n"
    "'DriverVersionRaw' = Raw scalar Vulkan driver version.\n"
    "'DriverId' = Id of the type of Vulkan driver: 1 = AMD proprietary, 2 = AMD open\n"
    "             source amdvlk, 3 = AMD OSS Mesa radv, 4 = NVidia proprietary,\n"
    "             5 = Intel proprietary Windows, 6 = Intel OSS Mesa anvil,\n"
    "             13 = Mesa OSS llvmpipe.\n"
    "'VendorID' = Vulkan device vendor id. Typically the PCI vendor id.\n"
    "'DeviceID' = Vulkan device id. Typically the PCI device id.\n"
    "'VulkanVersion' = Major.minor.point version of supported Vulkan api.\n"
    "'GpuType' = Type of device: 0 = Unknown/Other, 1 = Integrated gpu,\n"
    "            2 = Discrete gpu, 3 = Virtual gpu, 4 = cpu, ie. software renderer.\n\n"
    "'SupportsHDR' = Does the gpu support driving HDR displays in principle?\n"
    "                0 = No, 1 = Basic HDR-10.\n"
    "'SupportsTiming' = Does the gpu support high precision/reliability timing\n"
    "                   extensions. 0 = No, 1 = Yes. If the driver does not support\n"
    "                   timing extensions, the driver will fall back to hacks.\n"
    "'SupportsWait' = Does the gpu support waiting for present completion: 0 = No, 1 = Yes.\n"
    "\n";
    static char seeAlsoString[] = "GetCount";

    int i;
    char tmp[64];
    PsychGenericScriptType *s;
    const char *fieldNames[] = { "DeviceIndex", "GpuName", "GpuDriver", "DriverInfo", "DriverVersion", "DriverVersionRaw", "DriverId", "VendorId", "DeviceId", "VulkanVersion", "GpuType",
                                 "SupportsHDR", "SupportsTiming", "SupportsWait" };
    const int fieldCount = 14;

    // All sub functions should have these two lines:
    PsychPushHelp(useString, synopsisString, seeAlsoString);
    if (PsychIsGiveHelp()) { PsychGiveHelp(); return(PsychError_none); };

    // Check to see if the user supplied superfluous arguments
    PsychErrorExit(PsychCapNumOutputArgs(1));
    PsychErrorExit(PsychCapNumInputArgs(0));

    PsychVulkanCheckInit(FALSE);

    // Create a structure and populate it.
    PsychAllocOutStructArray(1, kPsychArgOptional, physicalGpuCount, fieldCount, fieldNames, &s);

    for (i = 0; i < physicalGpuCount; i++) {
        PsychVulkanDevice* vulkan = PsychGetVulkan(i + 1, FALSE);
        PsychSetStructArrayDoubleElement("DeviceIndex", i, i + 1, s);
        PsychSetStructArrayStringElement("GpuName", i, vulkan->deviceProps.deviceName, s);
        PsychSetStructArrayStringElement("GpuDriver", i, vulkan->driverProps.driverName, s);
        PsychSetStructArrayStringElement("DriverInfo", i, vulkan->driverProps.driverInfo, s);
        sprintf(tmp, "%i.%i.%i", VK_VERSION_MAJOR(vulkan->deviceProps.driverVersion), VK_VERSION_MINOR(vulkan->deviceProps.driverVersion), VK_VERSION_PATCH(vulkan->deviceProps.driverVersion));
        PsychSetStructArrayStringElement("DriverVersion", i, tmp, s);
        PsychSetStructArrayDoubleElement("DriverVersionRaw", i, vulkan->deviceProps.driverVersion, s);
        PsychSetStructArrayDoubleElement("DriverId", i, vulkan->driverProps.driverID, s);
        PsychSetStructArrayDoubleElement("VendorId", i, vulkan->deviceProps.vendorID, s);
        PsychSetStructArrayDoubleElement("DeviceId", i, vulkan->deviceProps.deviceID, s);
        sprintf(tmp, "%i.%i.%i", VK_VERSION_MAJOR(vulkan->deviceProps.apiVersion), VK_VERSION_MINOR(vulkan->deviceProps.apiVersion), VK_VERSION_PATCH(vulkan->deviceProps.apiVersion));
        PsychSetStructArrayStringElement("VulkanVersion", i, tmp, s);
        PsychSetStructArrayDoubleElement("GpuType", i, vulkan->deviceProps.deviceType, s);
        PsychSetStructArrayDoubleElement("SupportsHDR", i, vulkan->hasHDR, s);
        PsychSetStructArrayDoubleElement("SupportsTiming", i, vulkan->hasTiming, s);
        PsychSetStructArrayDoubleElement("SupportsWait", i, vulkan->hasWait, s);
    }

    return(PsychError_none);
}

PsychError PSYCHVULKANCloseWindow(void)
{
    static char useString[] = "PsychVulkanCore('CloseWindow' [, vulkanWindow]);";
    static char synopsisString[] =
        "Close Vulkan presentation window 'vulkanWindow'.\n"
        "If 'vulkanWindow' is omitted then all Vulkan windows are closed.\n";
    static char seeAlsoString[] = "OpenWindow";

    int handle;

    // All sub functions should have these two lines:
    PsychPushHelp(useString, synopsisString, seeAlsoString);
    if (PsychIsGiveHelp()) { PsychGiveHelp(); return(PsychError_none); };

    // Check to see if the user supplied superfluous arguments:
    PsychErrorExit(PsychCapNumOutputArgs(0));
    PsychErrorExit(PsychCapNumInputArgs(1));

    // Make sure Vulkan api is initialized, fail if not:
    PsychVulkanCheckInit(FALSE);

    // Get window index, if any:
    if (PsychCopyInIntegerArg(1, kPsychArgOptional, &handle)) {
        // Get the window:
        PsychVulkanWindow* window = PsychGetVulkanWindow(handle, FALSE);

        // Close it:
        PsychCloseVulkanWindow(window);
    }
    else {
        // No handle given: Close all Vulkan windows:
        PsychCloseAllVulkanWindows();
    }

    return(PsychError_none);
}

PsychError PSYCHVULKANOpenWindow(void)
{
    static char useString[] = "vulkanWindow = PsychVulkanCore('OpenWindow', gpuIndex, targetUUID, isFullscreen, screenId, rect, outputHandle, hdrMode, colorPrecision, refreshHz, colorSpace, colorFormat, flags);";
    //                         1                                            1         2           3             4         5     6             7        8               9          10          11           12
    static char synopsisString[] =
        "Open a display window on a Vulkan device.\n\n"
        "'gpuIndex' is the index of the Vulkan gpu that should be used for displaying the window. Devices "
        "are numbered 1 to PsychVulkanCore('GetCount'). A value of zero means to auto-detect the proper "
        "gpu + driver combo, given all other passed in parameters.\n"
        "'targetUUID' A 16 byte uint8() vector with the unique device id UUID which identifies the gpu hardware. "
        "This is needed for OpenGL + Vulkan interoperation, so Screen() can pass images for presentation to this "
        "driver. The UUID of the OpenGL gpu used by Screen() for rendering and the Vulkan gpu used for presentation "
        "must match for this to work, hence Screen() needs to provide the proper 'targetUUID' to use here. "
        "The proper 'targetUUID' can be set as winfo.GLDeviceUUID with winfo queried as winfo = Screen('GetWindowInfo', window) "
        "for a given Screen onscreen 'window'. The special targetUUID = zeros(1, 16, 'uint8'); can be used to match any gpu.\n"
        "'isFullscreen' Open a fullscreen exclusive window if set to 1, a windowed window if set to 0. Proper presentation "
        "timing and timestamping, HDR display and similar is often only possible with fullscreen exclusive mode.\n"
        "'screenId' The 'screenId' of the presentation monitor or screen, as used in Screen('Openwindow', screenId) ...\n"
        "'rect' The [left, top, right, bottom] bounding rectangle of the presentation windows client area. Should match "
        "the rectangle covered of the desktop or screen of a display monitor for fullscreen exclusive mode. Defines the "
        "window location in windowed mode.\n"
        "'outputHandle' Handle defining the video output surface to use, in an operating system dependent manner: On MS-Windows "
        "the parameter is currently ignored. On Linux X11 in windowed mode, it must be the X11 window handle of the window to "
        "which we want to display (cfe. Screen('GetWindowInfo', ...);. On Linux in X11 fullscreen mode, it must be the RandR "
        "output XID for the video output to which we want to display in fullscreen mode.\n"
        "'hdrMode' Which HDR display mode, if any, to set up for: 0 = No HDR mode, SDR display. 1 = HDR-10. This will also "
        "determine presets for 'colorPrecision', 'colorSpace' and 'colorFormat' depending on selected mode, if those arguments "
        "are set to auto-select. E.g., hdrMode == 1 for HDR-10 enforces at least 'colorPrecision' 1 for at least 10 bpc output, "
        "and selects a HDR10 color space with BT2020 color gamut and SMPTE ST-2084 PQ eotf encoding.\n"
        "'colorPrecision' Required output framebuffer (swapchain) precision. For HDR display modes, will be forced to at least 1 "
        "for at least 10 bpc precision:\n"
        "0 = 8 bpc fixed point linear RGBA8, unorm\n"
        "1 = 10 bpc fixed point linear RGB10A2, unorm\n"
        "2 = fp16 RGBA16F half-float\n"
        "3 = 16 bpc fixed point linear RGBA16, unorm\n"
        "4 = Try all deep-color unorm precisions from highest to lowest: RGBA16 -> RGB10A2\n"
        "\n"
        "5 = Try all deep-color precisions (unorm and fp16) from highest to lowest: RGBA16 -> RGBA16F -> RGB10A2\n"
        "6 = Try fp16, then 10 bpc unorm: RGBA16F -> RGB10A2\n"
        "Values 0 - 3 request the exact precision, and fail window creation if the system does not support them.\n"
        "Values 4 - 6 request the highest precision, then fall back to lower precisions if the system does not support them, "
        "and fail if the lowest precision in the sequence is not supported either. E.g., A value of 6 would aim for fp16, for "
        "more than 10 bpc effective output precision, but fall back to 10 bpc if fp16 is not supported. It would abort though "
        "if 10 bpc would not be supported either. This is a good choice for HDR display modes with the highest possible "
        "precision that a given operating system + Vulkan driver + gpu + display cable + display combination supports.\n"
        "'refreshHz' The desired video refresh rate for fullscreen windows. May have no effect in windowed mode.\n"
        "'colorSpace' The output color space to assume / select as VkColorSpace id. If zero, then will be set automatically according to 'hdrMode', e.g., "
        "VK_COLOR_SPACE_SRGB_NONLINEAR_KHR in hdrMode 0, VK_COLOR_SPACE_HDR10_ST2084_EXT in hdrMode 1 (=HDR10).\n"
        "'colorFormat' NOT USED YET! The pixel output color format to use as VkFormat color format id. If empty, then will be set automatically according to 'colorPrecision' and/or 'hdrMode'.\n"
        "'flags' Special mode selection flags or'ed together: +1 = Diagnostic display only, no Screen() OpenGL interop, just show an alternating black-white test image. Useful "
        "for most basic Vulkan testing and driver bringup if the given gpu does not have graphics drivers with OpenGL+Vulkan interop capabilities yet.\n"
        "+2 = Do not switch to fullscreen-exclusive mode on MS-Windows, even for fullscreen windows. This is useful as workaround for some buggy Vulkan drivers.\n"
        "\n\n"
        "Returns: The 'vulkanWindow' handle of the Vulkan presentation window.\n";

    static char seeAlsoString[] = "CloseWindow";

    psych_uint8* targetdeviceUUID;
    int gpuIndex, screenId;
    int isFullscreen;
    int colorPrecision;
    int hdrMode;
    int m, n, p;
    double* mat;
    PsychRectType rect;
    void* outputHandle;
    double refreshHz;
    int colorSpace;
    int colorFormat;
    int flags;
    int handle;
    PsychVulkanWindow* window;

    // All sub functions should have these two lines:
    PsychPushHelp(useString, synopsisString,seeAlsoString);
    if (PsychIsGiveHelp()) { PsychGiveHelp(); return(PsychError_none); };

    // Check to see if the user supplied superfluous arguments:
    PsychErrorExit(PsychCapNumOutputArgs(1));
    PsychErrorExit(PsychCapNumInputArgs(12));
    PsychErrorExit(PsychRequireNumInputArgs(12));

    // Make sure Vulkan api is initialized, fail if not:
    PsychVulkanCheckInit(FALSE);

    // Get mandatory GPU index:
    PsychCopyInIntegerArg(1, kPsychArgRequired, &gpuIndex);

    // Get deviceUUID 16 Bytes long:
    PsychAllocInUnsignedByteMatArg(2, kPsychArgRequired, &m, &n, &p, &targetdeviceUUID);
    if (m != 1 || n != 16 || p != 1)
        PsychErrorExitMsg(PsychError_invalidRectArg, "Invalid targetUUID given. Must be a 16 Byte row vector.");

    // Get mandatory fullscreen flag:
    PsychCopyInIntegerArg(3, kPsychArgRequired, &isFullscreen);

    // Get mandatory Psychtoolbox style screenId:
    PsychCopyInIntegerArg(4, kPsychArgRequired, &screenId);

    // Get mandatory position and size rectangle:
    PsychAllocInDoubleMatArg(5, kPsychArgRequired, &m, &n, &p, &mat);
    if (m != 1 || n != 4 || p != 1)
        PsychErrorExitMsg(PsychError_invalidRectArg, "Invalid window rect given.");
    memcpy(rect, mat, sizeof(PsychRectType));

    // Get mandatory monitor output handle:
    PsychCopyInPointerArg(6, kPsychArgRequired, &outputHandle);

    // Get mandatory hdrMode:
    PsychCopyInIntegerArg(7, kPsychArgRequired, &hdrMode);

    // Get mandatory color precision flag:
    PsychCopyInIntegerArg(8, kPsychArgRequired, &colorPrecision);

    // Get mandatory refresh rate in Hz:
    PsychCopyInDoubleArg(9, kPsychArgRequired, &refreshHz);

    // Get mandatory color space selector:
    PsychCopyInIntegerArg(10, kPsychArgRequired, &colorSpace);

    // Get mandatory color format selector:
    PsychCopyInIntegerArg(11, kPsychArgRequired, &colorFormat);

    // Get mandatory flags:
    PsychCopyInIntegerArg(12, kPsychArgRequired, &flags);

    // Find a free window slot:
    for (handle = 1; (handle <= MAX_PSYCH_VULKAN_WINDOWS) && vulkanWindows[handle - 1].isValid; handle++);
    if (handle > MAX_PSYCH_VULKAN_WINDOWS)
        PsychErrorExitMsg(PsychError_toomanyWin, "Too many Vulkan output display windows open.");

    // Get vulkanWindow record for free slot:
    window = &vulkanWindows[handle - 1];

    // Zero-init:
    memset(window, 0, sizeof(*window));

    // Assign window index already for diagnostic purpose in called routines:
    window->index = handle;

    if (!PsychOpenVulkanWindow(window, gpuIndex, targetdeviceUUID, isFullscreen, screenId, outputHandle, rect, colorPrecision, hdrMode, refreshHz, colorSpace, colorFormat, flags))
        PsychErrorExitMsg(PsychError_user, "Failed to open vulkan output window.");

    windowCount++;

    PsychCopyOutDoubleArg(1, kPsychArgOptional, (double) handle);

    return(PsychError_none);
}

PsychError PSYCHVULKANGetHDRProperties(void)
{
    static char useString[] = "hdr = PsychVulkanCore('GetHDRProperties', vulkanWindow);";
    static char synopsisString[] =
    "Return HDR properties of Vulkan presentation window 'vulkanWindow'.\n"
    "'hdr' is a struct with information about the HDR properties and settings "
    "for the display and window represented by 'vulkanWindow'. The following "
    "fields are defined:\n"
    "'GPUIndex' = Index of the Vulkan device used for this window.\n"
    "'Valid' = Are the HDR display properties valid? 0 = No, as no data could be "
    "queried from the display, 1 = Yes, data has been queried from display and is "
    "supposed to represent actual display HDR capabilities and properties.\n"
    "'HDRMode' 0 = None (SDR), 1 = Basic HDR-10 enabled with BT-2020 color "
    "space, 10 bpc color precision, and ST-2084 PQ Perceptual Quantizer EOTF.\n"
    "'LocalDimmingControl' 0 = No, 1 = Local dimming control supported.\n"
    "'MinLuminance' Minimum supported luminance in nits.\n"
    "'MaxLuminance' Maximum supported peak / burst luminance in nits.\n"
    "'MaxFrameAverageLightLevel' Maximum sustainable supported luminance in nits.\n"
    "'MaxContentLightLevel' Maximum desired content light level in nits.\n"
    "'ColorGamut' A 2-by-4 matrix encoding the CIE-1931 2D chromaticity coordinates of the "
    "red, green, and blue color primaries in columns 1, 2 and 3, and the white-point "
    "in column 4.\n"
    "'ColorSpace' Vulkan colorspace used for display. See VkColorSpaceKHR spec for reference.\n"
    "'ColorFormat' Vulkan pixel color format used for display. See VkFormat spec for reference.\n"
    "'ColorPrecision' Effective color precision of the Vulkan interop image and Vulkan swapChain: "
    "0 = GL_RGBA8, 1 = GL_RGB10A2, 2 = GL_RGBA16F, 3 = GL_RGBA16.\n"
    "\n";
    static char seeAlsoString[] = "OpenWindow HDRMetadata";

    int handle;
    PsychVulkanWindow* window;
    PsychGenericScriptType *s;
    PsychGenericScriptType *outMat;
    double *v;
    const char *fieldNames[] = { "Valid", "HDRMode", "LocalDimmingControl", "MinLuminance", "MaxLuminance",
                                 "MaxFrameAverageLightLevel", "MaxContentLightLevel", "ColorGamut",
                                 "ColorSpace", "ColorFormat", "ColorPrecision", "GPUIndex" };
    const int fieldCount = 12;

    // All sub functions should have these two lines:
    PsychPushHelp(useString, synopsisString, seeAlsoString);
    if (PsychIsGiveHelp()) { PsychGiveHelp(); return(PsychError_none); };

    // Check to see if the user supplied superfluous arguments:
    PsychErrorExit(PsychCapNumOutputArgs(1));
    PsychErrorExit(PsychCapNumInputArgs(1));

    // Make sure Vulkan api is initialized, fail if not:
    PsychVulkanCheckInit(FALSE);

    // Get window index, if any:
    PsychCopyInIntegerArg(1, kPsychArgRequired, &handle);

    // Get the window:
    window = PsychGetVulkanWindow(handle, FALSE);

    // Create a structure and populate it.
    PsychAllocOutStructArray(1, kPsychArgOptional, -1, fieldCount, fieldNames, &s);

    PsychSetStructArrayDoubleElement("Valid", 0, window->nativeDisplayHDRMetadataValidity, s);
    PsychSetStructArrayDoubleElement("GPUIndex", 0, window->vulkan->deviceIndex, s);
    PsychSetStructArrayDoubleElement("HDRMode", 0, window->hdrMode, s);
    PsychSetStructArrayDoubleElement("LocalDimmingControl", 0, window->local_dimming_supported, s);
    PsychSetStructArrayDoubleElement("MinLuminance", 0, window->nativeDisplayHDRMetadata.minLuminance, s);
    PsychSetStructArrayDoubleElement("MaxLuminance", 0, window->nativeDisplayHDRMetadata.maxLuminance, s);
    PsychSetStructArrayDoubleElement("MaxFrameAverageLightLevel", 0, window->nativeDisplayHDRMetadata.maxFrameAverageLightLevel, s);
    PsychSetStructArrayDoubleElement("MaxContentLightLevel", 0, window->nativeDisplayHDRMetadata.maxContentLightLevel, s);
    PsychSetStructArrayDoubleElement("ColorSpace", 0, window->colorspace, s);
    PsychSetStructArrayDoubleElement("ColorFormat", 0, window->format, s);
    PsychSetStructArrayDoubleElement("ColorPrecision", 0, window->colorPrecision, s);

    // Create color gamut and white point matrix:
    PsychAllocateNativeDoubleMat(2, 4, 1, &v, &outMat);

    *(v++) = window->nativeDisplayHDRMetadata.displayPrimaryRed.x;
    *(v++) = window->nativeDisplayHDRMetadata.displayPrimaryRed.y;

    *(v++) = window->nativeDisplayHDRMetadata.displayPrimaryGreen.x;
    *(v++) = window->nativeDisplayHDRMetadata.displayPrimaryGreen.y;

    *(v++) = window->nativeDisplayHDRMetadata.displayPrimaryBlue.x;
    *(v++) = window->nativeDisplayHDRMetadata.displayPrimaryBlue.y;

    *(v++) = window->nativeDisplayHDRMetadata.whitePoint.x;
    *(v++) = window->nativeDisplayHDRMetadata.whitePoint.y;

    PsychSetStructArrayNativeElement("ColorGamut", 0, outMat, s);

    return(PsychError_none);
}

PsychError PSYCHVULKANHDRMetadata(void)
{
    static char useString[] = "oldHdrMetadata = PsychVulkanCore('HDRMetadata', vulkanWindow, metadataType [, maxFrameAverageLightLevel][, maxContentLightLevel][, minLuminance][, maxLuminance][, colorGamut]);";
    //                         1                                               1             2               3                            4                       5               6               7
    static char synopsisString[] =
    "Return and/or set HDR metadata for Vulkan presentation window 'vulkanWindow'.\n"
    "This function returns the currently defined HDR metadata that is sent to the "
    "HDR display monitor associated with Vulkan window 'vulkanWindow', according to the "
    "format specified by the mandatory 'metadataType' parameter. It optionally "
    "allows to define new HDR metadata to send to the monitor, starting with the next "
    "presented visual stimulus image, ie. at the completion of the next Screen('Flip') or "
    "PsychVulkanCore('Present').\n"
    "Note: Setting new HDR metadata can be an expensive - and potentially visually disruptive - "
    "operation with some graphics drivers on some operating system, e.g., on MS-Windows. Therefore "
    "avoid too frequent updates. Our driver will eliminate redundant updates as a simple performance "
    "optimization.\n"
    "Return argument 'oldHdrMetadata' is a struct with information about the current metadata. "
    "Optionally you can define new metadata to be sent to the display. If you specify any new "
    "settings, but omit any values or leave them [] empty, then those values will remain at their "
    "current / old values.\n"
    "The following fields in the struct and as new settings are defined:\n"
    "'MetadataType' Mandatory: Type of metadata to send or query. Currently only a value of 0 is supported, which "
    "defines \"Static HDR metadata type 1\", as specified by the standards SMPTE 2086 for the "
    "mastering display color properties (~ color volume) and CTA-861.3 / CTA-861-G standards "
    "for content light levels. This type of metadata allows for luminance levels between "
    "0 and 65535 nits, except for 'MinLuminance' which allows for a maximum of 6.5535 nits.\n"
    "The content light level properties 'MaxFrameAverageLightLevel' and 'MaxContentLightLevel' default to "
    "0 at startup, which signals to the display device that they are unknown, a reasonable assumption for "
    "dynamically rendered content with prior unknown maximum values over a whole session.\n"
    "'MaxFrameAverageLightLevel' Maximum frame average light level of the visual content in nits [0; 65535].\n"
    "'MaxContentLightLevel' Maximum light level of the visual content in nits [0; 65535].\n"
    "The following mastering display properties (~ color volume) default to the properties of the used HDR "
    "display monitor for presentation, if they could be queried from the connected monitor. "
    "It is advisable to override them with the real properties of the mastering display, e.g., for "
    "properly mastered movie content or image files where this information may be available.\n"
    "'MinLuminance' Minimum supported luminance of the mastering display in nits [0; 6.5535].\n"
    "'MaxLuminance' Maximum supported luminance of the mastering display in nits [0; 65535].\n"
    "'ColorGamut' A 2-by-4 matrix encoding the CIE-1931 2D chromaticity coordinates of the "
    "red, green, and blue color primaries in columns 1, 2, and 3, and the location of the white-point "
    "in column 4. This defines the color space and gamut in which the visual content was produced.\n"
    "\n";
    static char seeAlsoString[] = "OpenWindow GetHDRProperties Present";

    int handle;
    PsychVulkanWindow* window;
    VkHdrMetadataEXT oldDisplayHDRMetadata;
    PsychGenericScriptType *s;
    PsychGenericScriptType *outMat;
    double *v;
    int metadataType = 0;
    double maxFrameAverageLightLevel = DBL_MAX, maxContentLightLevel = DBL_MAX;
    double minLuminance = DBL_MAX, maxLuminance = DBL_MAX;
    double *colorGamut;
    int m, n, p;
    const char *fieldNames[] = { "MetadataType", "MinLuminance", "MaxLuminance", "MaxFrameAverageLightLevel", "MaxContentLightLevel", "ColorGamut" };
    const int fieldCount = 6;

    // All sub functions should have these two lines:
    PsychPushHelp(useString, synopsisString, seeAlsoString);
    if (PsychIsGiveHelp()) { PsychGiveHelp(); return(PsychError_none); };

    // Check to see if the user supplied superfluous or too little arguments:
    PsychErrorExit(PsychCapNumOutputArgs(1));
    PsychErrorExit(PsychCapNumInputArgs(7));
    PsychErrorExit(PsychRequireNumInputArgs(1));

    // Make sure Vulkan api is initialized, fail if not:
    PsychVulkanCheckInit(FALSE);

    // Get window index, if any:
    PsychCopyInIntegerArg(1, kPsychArgRequired, &handle);

    // Get the window:
    window = PsychGetVulkanWindow(handle, FALSE);

    // Get and validate required metadataType:
    PsychCopyInIntegerArg(2, (PsychGetNumInputArgs() > 2) ? kPsychArgRequired : kPsychArgOptional, &metadataType);
    if (metadataType != 0)
        PsychErrorExitMsg(PsychError_user, "Invalid HDR 'metadataType' specified: Valid value is 0 only at the moment.");

    // Userscript wants current / old settings?
    if (PsychIsArgPresent(PsychArgOut, 1)) {
        // Create a structure and populate it with the current settings / old settings:
        PsychAllocOutStructArray(1, kPsychArgOptional, -1, fieldCount, fieldNames, &s);

        // Currently we only support MetadataType 0, ie. "Static HDR Metadata Type 1" as known from HDR-10 standard,
        // and the only thing current graphics and display drivers generally support afaik as of September 2020:
        PsychSetStructArrayDoubleElement("MetadataType", 0, 0, s);

        // Mastering display min and max luminance:
        PsychSetStructArrayDoubleElement("MinLuminance", 0, window->currentDisplayHDRMetadata.minLuminance, s);
        PsychSetStructArrayDoubleElement("MaxLuminance", 0, window->currentDisplayHDRMetadata.maxLuminance, s);

        // Scene content average and maximum content light level:
        PsychSetStructArrayDoubleElement("MaxFrameAverageLightLevel", 0, window->currentDisplayHDRMetadata.maxFrameAverageLightLevel, s);
        PsychSetStructArrayDoubleElement("MaxContentLightLevel", 0, window->currentDisplayHDRMetadata.maxContentLightLevel, s);

        // Create color gamut and white point matrix defining the mastering display color gamut / color space:
        PsychAllocateNativeDoubleMat(2, 4, 1, &v, &outMat);

        *(v++) = window->currentDisplayHDRMetadata.displayPrimaryRed.x;
        *(v++) = window->currentDisplayHDRMetadata.displayPrimaryRed.y;

        *(v++) = window->currentDisplayHDRMetadata.displayPrimaryGreen.x;
        *(v++) = window->currentDisplayHDRMetadata.displayPrimaryGreen.y;

        *(v++) = window->currentDisplayHDRMetadata.displayPrimaryBlue.x;
        *(v++) = window->currentDisplayHDRMetadata.displayPrimaryBlue.y;

        *(v++) = window->currentDisplayHDRMetadata.whitePoint.x;
        *(v++) = window->currentDisplayHDRMetadata.whitePoint.y;

        PsychSetStructArrayNativeElement("ColorGamut", 0, outMat, s);
    }

    // Backup current settings, so we can avoid redundant updates:
    oldDisplayHDRMetadata = window->currentDisplayHDRMetadata;

    // Assign optional new settings from user script:

    // Content light levels:
    if (PsychCopyInDoubleArg(3, kPsychArgOptional, &maxFrameAverageLightLevel)) {
        if (maxFrameAverageLightLevel < 0 || maxFrameAverageLightLevel > 65535)
            PsychErrorExitMsg(PsychError_user, "Invalid HDR content maxFrameAverageLightLevel specified: Must be between 0 and 65535 nits.");

        window->currentDisplayHDRMetadata.maxFrameAverageLightLevel = (float) maxFrameAverageLightLevel;

        window->currentDisplayHDRMetadataNeedsCommit = TRUE;
    }

    if (PsychCopyInDoubleArg(4, kPsychArgOptional, &maxContentLightLevel)) {
        if (maxContentLightLevel < 0 || maxContentLightLevel > 65535)
            PsychErrorExitMsg(PsychError_user, "Invalid HDR content maxContentLightLevel specified: Must be between 0 and 65535 nits.");

        if (maxContentLightLevel < window->currentDisplayHDRMetadata.maxFrameAverageLightLevel)
            PsychErrorExitMsg(PsychError_user, "Invalid HDR content maxContentLightLevel specified: Must be equal or greater than maxFrameAverageLightLevel in nits.");

        window->currentDisplayHDRMetadata.maxContentLightLevel = (float) maxContentLightLevel;

        window->currentDisplayHDRMetadataNeedsCommit = TRUE;
    }

    // Mastering display supported light levels:
    if (PsychCopyInDoubleArg(5, kPsychArgOptional, &minLuminance)) {
        if (minLuminance < 0 || minLuminance > 6.5535)
            PsychErrorExitMsg(PsychError_user, "Invalid HDR mastering display minLuminance specified: Must be between 0 and 6.5535 nits.");

        window->currentDisplayHDRMetadata.minLuminance = (float) minLuminance;

        window->currentDisplayHDRMetadataNeedsCommit = TRUE;
    }

    if (PsychCopyInDoubleArg(6, kPsychArgOptional, &maxLuminance)) {
        if (maxLuminance < 0 || maxLuminance > 65535)
            PsychErrorExitMsg(PsychError_user, "Invalid HDR mastering display maxLuminance specified: Must be between 0 and 65535 nits.");

        if (maxLuminance <  window->currentDisplayHDRMetadata.minLuminance)
            PsychErrorExitMsg(PsychError_user, "Invalid HDR mastering display maxLuminance specified: Must be equal or greater than minLuminance in nits.");

        window->currentDisplayHDRMetadata.maxLuminance = (float) maxLuminance;

        window->currentDisplayHDRMetadataNeedsCommit = TRUE;
    }

    // Mastering display / content color space:
    if (PsychAllocInDoubleMatArg(7, kPsychArgOptional, &m, &n, &p, &colorGamut)) {
        if (m != 2 || n!= 4 || p > 1)
            PsychErrorExitMsg(PsychError_user, "Invalid HDR colorGamut matrix specified: Must be a 2-by-4 matrix with [red, green, blue, white-point] chromaticity coordinates.");

        // Note: We intentionally do not range-check these chromaticity coordinates against [0.0 ; 1.0], as
        // there are color spaces with gamut coordinates outside the "physical" range 0 - 1, so values
        // outside the conventional range are valid albeit "unconventional":
        window->currentDisplayHDRMetadata.displayPrimaryRed.x = (float) *(colorGamut++);
        window->currentDisplayHDRMetadata.displayPrimaryRed.y = (float) *(colorGamut++);

        window->currentDisplayHDRMetadata.displayPrimaryGreen.x = (float) *(colorGamut++);
        window->currentDisplayHDRMetadata.displayPrimaryGreen.y = (float) *(colorGamut++);

        window->currentDisplayHDRMetadata.displayPrimaryBlue.x = (float) *(colorGamut++);
        window->currentDisplayHDRMetadata.displayPrimaryBlue.y = (float) *(colorGamut++);

        window->currentDisplayHDRMetadata.whitePoint.x = (float) *(colorGamut++);
        window->currentDisplayHDRMetadata.whitePoint.y = (float) *(colorGamut++);

        window->currentDisplayHDRMetadataNeedsCommit = TRUE;
    }

    // We do not call the driver to update HDR metadata here, as the semantic says this should be
    // only happening at Present() time. We only marked the need for an update at proper time in
    // the assignments above.
    if (window->currentDisplayHDRMetadataNeedsCommit) {
        // Some more consistency checks:
        if ((window->currentDisplayHDRMetadata.maxLuminance <  window->currentDisplayHDRMetadata.minLuminance) && (minLuminance != DBL_MAX))
            PsychErrorExitMsg(PsychError_user, "Invalid HDR mastering display minLuminance specified: Must be equal or smaller than current maxLuminance in nits.");

        if ((window->currentDisplayHDRMetadata.maxContentLightLevel <  window->currentDisplayHDRMetadata.maxFrameAverageLightLevel) && (maxFrameAverageLightLevel != DBL_MAX))
            PsychErrorExitMsg(PsychError_user, "Invalid HDR content maxFrameAverageLightLevel specified: Must be equal or smaller than current maxContentLightLevel in nits.");

        // Check if the to-be-set metadata has really changed, ie. it isn't a redundant
        // update to the settings that were already current. Turns out this is important
        // on MS-Windows with both AMD and even more so NVidia drivers, or performance will
        // fall off a cliff! E.g., AMD takes over 100 msecs for an update, NVidia takes on the
        // order of 1 second and a modesetting sequence! Strong work :/
        if (memcmp(&oldDisplayHDRMetadata, &window->currentDisplayHDRMetadata, sizeof(oldDisplayHDRMetadata)) == 0) {
            // No effective change. This update is redundant, so skip it:
            window->currentDisplayHDRMetadataNeedsCommit = FALSE;

            if (verbosity > 5)
                printf("PsychVulkanCore-INFO: In 'HDRMetadata', redundant HDR metadata update for window %i skipped.\n", window->index);
        }
    }

    return(PsychError_none);
}

PsychError PSYCHVULKANHDRLocalDimming(void)
{
    static char useString[] = "oldlocalDimmmingEnable = PsychVulkanCore('HDRLocalDimming', vulkanWindow [, localDimmmingEnable]);";
    //                         1                                                           1               2
    static char synopsisString[] =
    "Return and/or set HDR local backlight dimming enable setting for Vulkan presentation window 'vulkanWindow'.\n"
    "This function returns the currently set HDR local backlight dimming setting for dynamic contrast control on "
    "the HDR display monitor associated with Vulkan window 'vulkanWindow'.\n"
    "Return argument 'oldlocalDimmmingEnable' is the current setting.\n"
    "The optional 'localDimmingEnable' is the new setting to apply. This will only "
    "work if the display and display driver supports the VK_AMD_display_native_hdr "
    "Vulkan extension. As of June 2020, only \"AMD FreeSync2 HDR compliant\" "
    "HDR monitors under Windows-10 with an AMD graphics card in fullscreen mode support this.\n"
    "The PsychVulkanCore('GetHDRProperties', vulkanWindow) function allows to query if the "
    "current setup supports this function. Please note that this function will always report "
    "the selected 'localDimmingEnable' setting made by your code on a nominally supported setup. "
    "There is no way for our driver to detect if the mode change on the display was accepted, as "
    "the operating system provides no feedback about this. At least one model of \"compatible\" monitor "
    "is already known to ignore this setting, unknown if this is an AMD driver bug or monitor firmware "
    "bug. Tread carefully! Manual control of this setting on the monitor itself may be the safer choice."
    "\n";
    static char seeAlsoString[] = "GetHDRProperties";

    int handle;
    PsychVulkanWindow* window;
    int localDimmmingEnable;

    // All sub functions should have these two lines:
    PsychPushHelp(useString, synopsisString, seeAlsoString);
    if (PsychIsGiveHelp()) { PsychGiveHelp(); return(PsychError_none); };

    // Check to see if the user supplied superfluous or too little arguments:
    PsychErrorExit(PsychCapNumOutputArgs(1));
    PsychErrorExit(PsychCapNumInputArgs(2));

    // Make sure Vulkan api is initialized, fail if not:
    PsychVulkanCheckInit(FALSE);

    // Get window index, if any:
    PsychCopyInIntegerArg(1, kPsychArgRequired, &handle);

    // Get the window:
    window = PsychGetVulkanWindow(handle, FALSE);

    // Return current / old setting:
    PsychCopyOutDoubleArg(1, kPsychArgOptional, window->localDimmmingEnable);

    // Assign optional new settings from user script:
    if (PsychCopyInIntegerArg(2, kPsychArgOptional, &localDimmmingEnable)) {
        if ((localDimmmingEnable != 0) && (localDimmmingEnable != 1))
            PsychErrorExitMsg(PsychError_user, "Invalid HDR localDimmmingEnable specified: Valid values are 0 (Disable) and 1 (Enable).");

        // Supported by display and driver?
        if (!window->local_dimming_supported)
            PsychErrorExitMsg(PsychError_user, "Tried to set HDR localDimmmingEnable mode for HDR display, but driver + display does not support this!");

        // vkSetLocalDimmingAMD does not provide a return code with success/failure,
        // so just assume it succeeded, the best we can do:
        window->localDimmmingEnable = localDimmmingEnable;
        fpSetLocalDimmingAMD(window->vulkan->device, window->swapChain, (VkBool32) localDimmmingEnable);
    }

    return(PsychError_none);
}

PsychError PSYCHVULKANClose(void)
{
    static char useString[] = "PsychVulkanCore('Close');";
    //
    static char synopsisString[] =
    //    MK: Hide the existence of the 'vulkanDevice' handle, as using it is non-sensical in the currrent
    //        implementation.
    //    "Close Vulkan device 'vulkanHandle'. Do nothing if no such device is open.\n"
    //    "If the optional 'vulkanHandle' is omitted, then close all open devices and shutdown the driver, "
    "Close all open Vulkan devices and shutdown the driver, "
    "ie. perform the same cleanup as if 'clear PsychVulkanCore' would be executed.\n";
    static char seeAlsoString[] = "Open";

    int handle = -1;

    // All sub functions should have these two lines
    PsychPushHelp(useString, synopsisString,seeAlsoString);
    if (PsychIsGiveHelp()) { PsychGiveHelp();return(PsychError_none); };

    // Check to see if the user supplied superfluous arguments:
    PsychErrorExit(PsychCapNumOutputArgs(0));
    PsychErrorExit(PsychCapNumInputArgs(1));

    // Make sure driver is initialized:
    PsychVulkanCheckInit(FALSE);

    // Get optional device handle:
    PsychCopyInIntegerArg(1, kPsychArgOptional, &handle);

    if (handle >= 1) {
        // Close device:
        PsychVulkanClose(handle);
    }
    else {
        // No handle provided: Close all devices, shutdown driver.
        PsychVulkanShutDown();
    }

    return(PsychError_none);
}

PsychError PSYCHVULKANGetInteropHandle(void)
{
    static char useString[] = "[interopObjectHandle, allocationSize, formatSpec, tilingMode, memoryOffset, width, height, interopSemaphoreHandle] = PsychVulkanCore('GetInteropHandle', vulkanWindowHandle [, wantSemaphore=0][, eye=0]);";
    //                          1                    2               3           4           5             6      7       8                                                             1                     2                  3
    static char synopsisString[] =
        "Retrieve all info needed to import the Vulkan interop image as OpenGL renderable texture.\n"
        "Returned values allow to construct an OpenGL GL_TEXTURE2D object which uses the driver "
        "internal VkImage's backing memory as backing memory for the OpenGL texture. Iow. OpenGL "
        "and Vulkan can share an image buffer into which OpenGL renders and which is later displayed "
        "by PsychVulkanCore() via Vulkan WSI.\n"
        "'vulkanWindowHandle' is a handle to the Vulkan window to use for stimulus display.\n"
        "'wantSemaphore' Optional: If a semaphore interop handle should also be returned as 8th "
        "return argument. Defaults to 0 = No. 1 = Yes will export the handle, and each 'Present' "
        "operation on the 'vulkanWindowHandle' will block until the returned semaphore is signalled.\n"
        "'eye' Optional Eye for which interop data should be returned in a stereo display setup:\n"
        "0 = Left eye view or monoscopic view, 1 = Right eye view. Defaults to 0.\n"
        "Return arguments:\n"
        "'interopObjectHandle' Operating system specific handle to the interop image backing memory. "
        "The caller is responsible for releasing the handle once it is no longer needed.\n"
        "'allocationSize' Number of bytes of backing memory for the 'interopObjectHandle' to import.\n"
        "'formatSpec' Type of texture to create: 0 = GL_RGBA8, 1 = GL_RGB10A2, 2 = GL_RGBA16F, 3 = GL_RGBA16.\n"
        "'tilingMode' Type of tiling to use/assume for rendering: 0 = Linear (non-tiled), 1 = Tiled.\n"
        "'memoryOffset' Memory offset in bytes into the imported memory object to use.\n"
        "'width' Width of texture in pixels.\n"
        "'height' Height of texture in pixels.\n"
        "'interopSemaphoreHandle' Operating system specific handle to the interop semaphore, if one was "
        "requested by setting 'wantSemaphore' to 1. "
        "The caller is responsible for releasing the handle once it is no longer needed.\n";

    static char seeAlsoString[] = "";

    int vulkanWindowHandle, eyeIndex, wantSemaphore;
    PsychVulkanWindow* window;

    // All sub functions should have these two lines
    PsychPushHelp(useString, synopsisString,seeAlsoString);
    if (PsychIsGiveHelp()) { PsychGiveHelp(); return(PsychError_none); };

    // Check to see if the user supplied superfluous arguments
    PsychErrorExit(PsychCapNumOutputArgs(8));
    PsychErrorExit(PsychCapNumInputArgs(3));
    PsychErrorExit(PsychRequireNumInputArgs(1));

    // Make sure driver is initialized:
    PsychVulkanCheckInit(FALSE);

    // Get vulkanWindowHandle and associated Vulkan window:
    PsychCopyInIntegerArg(1, kPsychArgRequired, &vulkanWindowHandle);
    window = PsychGetVulkanWindow(vulkanWindowHandle, FALSE);

    wantSemaphore = 0;
    PsychCopyInIntegerArg(2, kPsychArgOptional, &wantSemaphore);
    if (wantSemaphore < 0 || wantSemaphore > 1)
        PsychErrorExitMsg(PsychError_user, "Invalid 'wantSemaphore' specified. Must be 0 = no or 1 = yes.");

    // Get eye:
    eyeIndex = 0;
    PsychCopyInIntegerArg(3, kPsychArgOptional, &eyeIndex);
    if (eyeIndex < 0 || eyeIndex > 1)
        PsychErrorExitMsg(PsychError_user, "Invalid 'eye' specified. Must be 0 or 1 for left- or right eye.");

    if (eyeIndex > 0 && !(window->isStereo))
        PsychErrorExitMsg(PsychError_user, "Invalid 'eye' specified. Must be 0, as mono display mode is selected.");

    // Return interopObjectHandle for interop memory object which backs the interop VkImage. Make sure the caller
    // really wants this returned, as it matters for potential ownership transfer of handles, or the lack thereof:
    if ((PsychGetNumNamedOutputArgs() >= 1) && PsychCopyOutPointerArg(1, kPsychArgOptional, (void*) (size_t) window->interopHandles.memory)) {
        // interopHandles.memory handle returned to caller. This constitutes an ownership transfer,
        // so we are no longer owning this handle and are no longer responsible for destroying it
        // at the end of a session:
        window->interopHandles.memory = 0;
    }

    // Return backing memory size in bytes:
    PsychCopyOutDoubleArg(2, kPsychArgOptional, (double) window->interopMemorysize);

    // Return internal texture format precision specifier for OpenGL texture:
    PsychCopyOutDoubleArg(3, kPsychArgOptional, (double) window->colorPrecision);

    // Return tiling mode:
    PsychCopyOutDoubleArg(4, kPsychArgOptional, (double) window->interopTextureTiled);

    // Return memory offset into exported memory object - Hard-coded zero atm.:
    PsychCopyOutDoubleArg(5, kPsychArgOptional, (double) 0);

    // Return width:
    PsychCopyOutDoubleArg(6, kPsychArgOptional, (double) window->width);

    // Return height:
    PsychCopyOutDoubleArg(7, kPsychArgOptional, (double) window->height);

    // Export of a semaphore handle requested?
    if (wantSemaphore && (PsychGetNumNamedOutputArgs() >= 8)) {
        // Yes: Create the semaphore, retrieve and export an interop handle to it:
        VkResult result;

        // Exists already? Redundant calls are not allowed - Everybody only one cross...
        if (window->interopRenderDoneSemaphore)
            PsychErrorExitMsg(PsychError_user, "Tried to create and export 'interopSemaphoreHandle' twice. Must be done only once per window - Everybody only one cross!");

        // Create semaphore for export to / use by external renderer to signal render completion to us:
        const VkExportSemaphoreCreateInfo renderCompleteSemaphoreExportInfo = {
            .sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO,
            .pNext = NULL,
            .handleTypes = (PSYCH_SYSTEM == PSYCH_WINDOWS) ? VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT : VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
        };

        const VkSemaphoreCreateInfo renderCompleteSemaphoreCreateInfo = {
            .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
            .pNext = &renderCompleteSemaphoreExportInfo,
            .flags = 0,
        };

        result = vkCreateSemaphore(window->vulkan->device, &renderCompleteSemaphoreCreateInfo, NULL, &window->interopRenderDoneSemaphore);
        if (result != VK_SUCCESS) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: vkCreateSemaphore() for external interop render-complete semaphore failed for window %i: res=%i.\n", window->index, result);

            PsychErrorExitMsg(PsychError_system, "Creation of external interop render-complete semaphore failed.");
        }

        // Create export handle to semaphore:
        #if PSYCH_SYSTEM == PSYCH_WINDOWS
        VkSemaphoreGetWin32HandleInfoKHR interopRenderDoneSemaphoreHandleInfo = {
            .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_WIN32_HANDLE_INFO_KHR,
            .pNext = NULL,
            .semaphore = window->interopRenderDoneSemaphore,
            .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT,
        };

        result = fpGetSemaphoreWin32HandleKHR(window->vulkan->device, &interopRenderDoneSemaphoreHandleInfo, &window->interopHandles.glComplete);
        #endif

        #if PSYCH_SYSTEM == PSYCH_LINUX
        VkSemaphoreGetFdInfoKHR interopRenderDoneSemaphoreHandleInfo = {
            .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
            .pNext = NULL,
            .semaphore = window->interopRenderDoneSemaphore,
            .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
        };

        result = fpGetSemaphoreFdKHR(window->vulkan->device, &interopRenderDoneSemaphoreHandleInfo, &window->interopHandles.glComplete);
        #endif

        #if PSYCH_SYSTEM == PSYCH_OSX
        result = -1; // TODO OSX
        #endif

        if (result != VK_SUCCESS) {
            if (verbosity > 0)
                printf("PsychVulkanCore-ERROR: Getting handles for external interop render-complete semaphore failed for window %i: res=%i.\n", window->index, result);

            // Delete now obsolete interopRenderDoneSemaphore:
            vkDestroySemaphore(window->vulkan->device, window->interopRenderDoneSemaphore, NULL);
            window->interopRenderDoneSemaphore = VK_NULL_HANDLE;

            PsychErrorExitMsg(PsychError_system, "Getting handles for external interop render-complete semaphore failed.");
        }

        // Return semaphore handle to caller for external use/signalling:
        PsychCopyOutPointerArg(8, kPsychArgOptional, (void*) (size_t) window->interopHandles.glComplete);

        // Now that the ownership of the handle is transferred to the caller, remove our reference to it:
        window->interopHandles.glComplete = 0;
    }
    else {
        // Copy out dummy invalid 8th arg NULL pointer:
        PsychCopyOutPointerArg(8, kPsychArgOptional, NULL);
    }

    return(PsychError_none);
}

PsychError PSYCHVULKANPresent(void)
{
    static char useString[] = "[tPredictedOnset, frameIndex] = PsychVulkanCore('Present', vulkanWindowHandle [, tWhen=0][, doTimestamp=2]);";
    //                          1                2                                        1                     2          3
    static char synopsisString[] =
        "Present last rendered frame to Vulkan window 'vulkanWindowHandle'.\n\n"
        "This will commit the current set of 2D textures with new rendered content "
        "to the swapchains, for consumption by the presentation engine. "
        "You usually won't call this function yourself, but Screen('Flip') "
        "will call it automatically for you at the appropriate moment.\n\n"
        "'doTimestamp' If set to 1 or 2, performs timestamping of stimulus onset, or at least "
        "tries to estimate such onset time. If set to 0, do nothing timestamping-wise. "
        "A value of 1 always uses a home-made method of scheduling and timestamping. "
        "A value of 2 tries to use a Vulkan-provided high-precision method if available "
        "and possible, and falls back to method 1 otherwise. Value 2 is the default.\n\n"
        "'tWhen' If provided, defines the target presentation time, as provided by "
        "Screen('Flip', win, tWhen); a value of zero, or omission, means to present as "
        "soon as possible.\n\n"
        "'tPredictedOnset' returns predicted onset time for the just presented frame.\n\n"
        "'frameIndex' Running index of just presented (or queued for presentation) frame.\n"
        "\n\n";
    static char seeAlsoString[] = "";

    int vulkanWindowHandle;
    PsychVulkanWindow *window;
    double tWhen = 0;
    int doTimestamp = 2;

    // All sub functions should have these two lines:
    PsychPushHelp(useString, synopsisString, seeAlsoString);
    if (PsychIsGiveHelp()) { PsychGiveHelp(); return(PsychError_none); };

    // Check to see if the user supplied superfluous arguments
    PsychErrorExit(PsychCapNumOutputArgs(2));
    PsychErrorExit(PsychCapNumInputArgs(3));
    PsychErrorExit(PsychRequireNumInputArgs(1));

    // Make sure driver is initialized:
    PsychVulkanCheckInit(FALSE);

    // Get window handle:
    PsychCopyInIntegerArg(1, kPsychArgRequired, &vulkanWindowHandle);
    window = PsychGetVulkanWindow(vulkanWindowHandle, FALSE);
    if (window == NULL)
        PsychErrorExitMsg(PsychError_user, "Invalid 'vulkanWindowHandle' provided. No such window open.");

    // Get optional presentation target time:
    PsychCopyInDoubleArg(2, kPsychArgOptional, &tWhen);

    // Get optional timestamping flag:
    PsychCopyInIntegerArg(3, kPsychArgOptional, &doTimestamp);
    if (doTimestamp < 0 || doTimestamp > 2)
        PsychErrorExitMsg(PsychError_user, "Invalid 'doTimestamp' provided. Must be 0, 1 or 2.");

    // Present:
    if (!PsychPresent(window, tWhen, doTimestamp)) {
        // Make present failure non-fatal. We are often called from an imaging pipeline
        // hook callback, so erroring out with PsychErrorExitMsg() can cause complete
        // hangs of the host application - tedious to debug. Rather keep going with a
        // warning.
        if (verbosity > 0)
            printf("PsychVulkanCore-ERROR: 'Present' operation failed for some reason! Trying to keep going, brace for impact!\n");
    }

    // Return estimated onset time:
    PsychCopyOutDoubleArg(1, kPsychArgOptional, (doTimestamp > 0) ? window->tPresentComplete : -1.0);

    // Copy out frameIndex:
    PsychCopyOutDoubleArg(2, kPsychArgOptional, (double) window->frameIndex - 1);

    return(PsychError_none);
}