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

#include "qopengltexture.h"
#include "qopengltexture_p.h"
#include "qopengltexturehelper_p.h"
#include "qopenglfunctions.h"
#include <QtGui/qcolor.h>
#include <QtGui/qopenglcontext.h>
#include <QtCore/qdebug.h>
#include <private/qobject_p.h>
#include <private/qopenglcontext_p.h>

QT_BEGIN_NAMESPACE

//this is to work around GL_TEXTURE_WRAP_R_OES which also has 0x8072 as value
#if !defined(GL_TEXTURE_WRAP_R)
    #define GL_TEXTURE_WRAP_R 0x8072
#endif

QOpenGLTexturePrivate::QOpenGLTexturePrivate(QOpenGLTexture::Target textureTarget,
                                             QOpenGLTexture *qq)
    : q_ptr(qq),
      context(0),
      target(textureTarget),
      textureId(0),
      format(QOpenGLTexture::NoFormat),
      formatClass(QOpenGLTexture::NoFormatClass),
      requestedMipLevels(1),
      mipLevels(-1),
      layers(1),
      faces(1),
      samples(0),
      fixedSamplePositions(true),
      baseLevel(0),
      maxLevel(1000),
      depthStencilMode(QOpenGLTexture::DepthMode),
      comparisonFunction(QOpenGLTexture::CompareLessEqual),
      comparisonMode(QOpenGLTexture::CompareNone),
      minFilter(QOpenGLTexture::Nearest),
      magFilter(QOpenGLTexture::Nearest),
      maxAnisotropy(1.0f),
      minLevelOfDetail(-1000.0f),
      maxLevelOfDetail(1000.0f),
      levelOfDetailBias(0.0f),
      textureView(false),
      autoGenerateMipMaps(true),
      storageAllocated(false),
      texFuncs(0),
      functions(0)
{
    dimensions[0] = dimensions[1] = dimensions[2] = 1;

    switch (target) {
    case QOpenGLTexture::Target1D:
        bindingTarget = QOpenGLTexture::BindingTarget1D;
        break;
    case QOpenGLTexture::Target1DArray:
        bindingTarget = QOpenGLTexture::BindingTarget1DArray;
        break;
    case QOpenGLTexture::Target2D:
        bindingTarget = QOpenGLTexture::BindingTarget2D;
        break;
    case QOpenGLTexture::Target2DArray:
        bindingTarget = QOpenGLTexture::BindingTarget2DArray;
        break;
    case QOpenGLTexture::Target3D:
        bindingTarget = QOpenGLTexture::BindingTarget3D;
        break;
    case QOpenGLTexture::TargetCubeMap:
        bindingTarget = QOpenGLTexture::BindingTargetCubeMap;
        faces = 6;
        break;
    case QOpenGLTexture::TargetCubeMapArray:
        bindingTarget = QOpenGLTexture::BindingTargetCubeMapArray;
        faces = 6;
        break;
    case QOpenGLTexture::Target2DMultisample:
        bindingTarget = QOpenGLTexture::BindingTarget2DMultisample;
        break;
    case QOpenGLTexture::Target2DMultisampleArray:
        bindingTarget = QOpenGLTexture::BindingTarget2DMultisampleArray;
        break;
    case QOpenGLTexture::TargetRectangle:
        bindingTarget = QOpenGLTexture::BindingTargetRectangle;
        break;
    case QOpenGLTexture::TargetBuffer:
        bindingTarget = QOpenGLTexture::BindingTargetBuffer;
        break;
    }

    swizzleMask[0] = QOpenGLTexture::RedValue;
    swizzleMask[1] = QOpenGLTexture::GreenValue;
    swizzleMask[2] = QOpenGLTexture::BlueValue;
    swizzleMask[3] = QOpenGLTexture::AlphaValue;

    wrapModes[0] = wrapModes[1] = wrapModes[2] = target == QOpenGLTexture::TargetRectangle
        ? QOpenGLTexture::ClampToEdge : QOpenGLTexture::Repeat;
}

QOpenGLTexturePrivate::~QOpenGLTexturePrivate()
{
    destroy();
}

void QOpenGLTexturePrivate::initializeOpenGLFunctions()
{
    // If we already have a functions object, there is nothing to do
    if (texFuncs)
        return;

    // See if the context already has a suitable resource we can use.
    // If not create a functions object and add it to the context in case
    // others wish to use it too
    texFuncs = context->textureFunctions();
    if (!texFuncs) {
        texFuncs = new QOpenGLTextureHelper(context);
        context->setTextureFunctions(texFuncs);
    }
}

bool QOpenGLTexturePrivate::create()
{
    if (textureId != 0)
        return true;

    QOpenGLContext *ctx = QOpenGLContext::currentContext();
    if (!ctx) {
        qWarning("Requires a valid current OpenGL context.\n"
                 "Texture has not been created");
        return false;
    }
    context = ctx;
    functions = ctx->functions();

    // Resolve any functions we will need based upon context version and create the texture
    initializeOpenGLFunctions();

    // What features do we have?
    QOpenGLTexture::Feature feature = QOpenGLTexture::ImmutableStorage;
    while (feature != QOpenGLTexture::MaxFeatureFlag) {
        if (QOpenGLTexture::hasFeature(feature))
            features |= feature;
        feature = static_cast<QOpenGLTexture::Feature>(feature << 1);
    }

    functions->glGenTextures(1, &textureId);
    return textureId != 0;
}

void QOpenGLTexturePrivate::destroy()
{
    if (!textureId) {
        // not created or already destroyed
        return;
    }
    QOpenGLContext *currentContext = QOpenGLContext::currentContext();
    if (!currentContext) {
        qWarning("QOpenGLTexturePrivate::destroy() called without a current context.\n"
                 "Texture has not been destroyed");
        return;
    }
    if (!QOpenGLContext::areSharing(currentContext, context)) {

        qWarning("QOpenGLTexturePrivate::destroy() called but texture context %p"
                 " is not shared with current context %p.\n"
                 "Texture has not been destroyed",
                 static_cast<const void *>(context),
                 static_cast<const void *>(currentContext));
        return;
    }

    functions->glDeleteTextures(1, &textureId);

    context = 0;
    functions = 0;
    textureId = 0;
    format = QOpenGLTexture::NoFormat;
    formatClass = QOpenGLTexture::NoFormatClass;
    requestedMipLevels = 1;
    mipLevels = -1;
    layers = 1;
    faces = 1;
    samples = 0;
    fixedSamplePositions = true,
    baseLevel = 0;
    maxLevel = 1000;
    depthStencilMode = QOpenGLTexture::DepthMode;
    minFilter = QOpenGLTexture::Nearest;
    magFilter = QOpenGLTexture::Nearest;
    maxAnisotropy = 1.0f;
    minLevelOfDetail = -1000.0f;
    maxLevelOfDetail = 1000.0f;
    levelOfDetailBias = 0.0f;
    textureView = false;
    autoGenerateMipMaps = true;
    storageAllocated = false;
    texFuncs = 0;

    swizzleMask[0] = QOpenGLTexture::RedValue;
    swizzleMask[1] = QOpenGLTexture::GreenValue;
    swizzleMask[2] = QOpenGLTexture::BlueValue;
    swizzleMask[3] = QOpenGLTexture::AlphaValue;

    wrapModes[0] = wrapModes[1] = wrapModes[2] = target == QOpenGLTexture::TargetRectangle
        ? QOpenGLTexture::ClampToEdge : QOpenGLTexture::Repeat;
}

void QOpenGLTexturePrivate::bind()
{
    functions->glBindTexture(target, textureId);
}

void QOpenGLTexturePrivate::bind(uint unit, QOpenGLTexture::TextureUnitReset reset)
{
    GLint oldTextureUnit = 0;
    if (reset == QOpenGLTexture::ResetTextureUnit)
        functions->glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);

    texFuncs->glActiveTexture(GL_TEXTURE0 + unit);
    functions->glBindTexture(target, textureId);

    if (reset == QOpenGLTexture::ResetTextureUnit)
        texFuncs->glActiveTexture(GL_TEXTURE0 + oldTextureUnit);
}

void QOpenGLTexturePrivate::release()
{
    functions->glBindTexture(target, 0);
}

void QOpenGLTexturePrivate::release(uint unit, QOpenGLTexture::TextureUnitReset reset)
{
    GLint oldTextureUnit = 0;
    if (reset == QOpenGLTexture::ResetTextureUnit)
        functions->glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);

    texFuncs->glActiveTexture(GL_TEXTURE0 + unit);
    functions->glBindTexture(target, 0);

    if (reset == QOpenGLTexture::ResetTextureUnit)
        texFuncs->glActiveTexture(GL_TEXTURE0 + oldTextureUnit);
}

bool QOpenGLTexturePrivate::isBound() const
{
    GLint boundTextureId = 0;
    functions->glGetIntegerv(bindingTarget, &boundTextureId);
    return (static_cast<GLuint>(boundTextureId) == textureId);
}

bool QOpenGLTexturePrivate::isBound(uint unit) const
{
    GLint oldTextureUnit = 0;
    functions->glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);

    GLint boundTextureId = 0;
    texFuncs->glActiveTexture(GL_TEXTURE0 + unit);
    functions->glGetIntegerv(bindingTarget, &boundTextureId);
    bool result = (static_cast<GLuint>(boundTextureId) == textureId);

    texFuncs->glActiveTexture(GL_TEXTURE0 + oldTextureUnit);
    return result;
}

int QOpenGLTexturePrivate::evaluateMipLevels() const
{
    switch (target) {
    case QOpenGLTexture::Target1D:
    case QOpenGLTexture::Target1DArray:
    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::Target2DArray:
    case QOpenGLTexture::Target3D:
    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetCubeMapArray:
        return qMin(maximumMipLevelCount(), qMax(1, requestedMipLevels));

    case QOpenGLTexture::TargetRectangle:
    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
    case QOpenGLTexture::TargetBuffer:
    default:
        return 1;
    }
}

static bool isSizedTextureFormat(QOpenGLTexture::TextureFormat internalFormat)
{
    switch (internalFormat) {
    case QOpenGLTexture::NoFormat:
        return false;

    case QOpenGLTexture::R8_UNorm:
    case QOpenGLTexture::RG8_UNorm:
    case QOpenGLTexture::RGB8_UNorm:
    case QOpenGLTexture::RGBA8_UNorm:
    case QOpenGLTexture::R16_UNorm:
    case QOpenGLTexture::RG16_UNorm:
    case QOpenGLTexture::RGB16_UNorm:
    case QOpenGLTexture::RGBA16_UNorm:
    case QOpenGLTexture::R8_SNorm:
    case QOpenGLTexture::RG8_SNorm:
    case QOpenGLTexture::RGB8_SNorm:
    case QOpenGLTexture::RGBA8_SNorm:
    case QOpenGLTexture::R16_SNorm:
    case QOpenGLTexture::RG16_SNorm:
    case QOpenGLTexture::RGB16_SNorm:
    case QOpenGLTexture::RGBA16_SNorm:
    case QOpenGLTexture::R8U:
    case QOpenGLTexture::RG8U:
    case QOpenGLTexture::RGB8U:
    case QOpenGLTexture::RGBA8U:
    case QOpenGLTexture::R16U:
    case QOpenGLTexture::RG16U:
    case QOpenGLTexture::RGB16U:
    case QOpenGLTexture::RGBA16U:
    case QOpenGLTexture::R32U:
    case QOpenGLTexture::RG32U:
    case QOpenGLTexture::RGB32U:
    case QOpenGLTexture::RGBA32U:
    case QOpenGLTexture::R8I:
    case QOpenGLTexture::RG8I:
    case QOpenGLTexture::RGB8I:
    case QOpenGLTexture::RGBA8I:
    case QOpenGLTexture::R16I:
    case QOpenGLTexture::RG16I:
    case QOpenGLTexture::RGB16I:
    case QOpenGLTexture::RGBA16I:
    case QOpenGLTexture::R32I:
    case QOpenGLTexture::RG32I:
    case QOpenGLTexture::RGB32I:
    case QOpenGLTexture::RGBA32I:
    case QOpenGLTexture::R16F:
    case QOpenGLTexture::RG16F:
    case QOpenGLTexture::RGB16F:
    case QOpenGLTexture::RGBA16F:
    case QOpenGLTexture::R32F:
    case QOpenGLTexture::RG32F:
    case QOpenGLTexture::RGB32F:
    case QOpenGLTexture::RGBA32F:
    case QOpenGLTexture::RGB9E5:
    case QOpenGLTexture::RG11B10F:
    case QOpenGLTexture::RG3B2:
    case QOpenGLTexture::R5G6B5:
    case QOpenGLTexture::RGB5A1:
    case QOpenGLTexture::RGBA4:
    case QOpenGLTexture::RGB10A2:

    case QOpenGLTexture::D16:
    case QOpenGLTexture::D24:
    case QOpenGLTexture::D32:
    case QOpenGLTexture::D32F:

    case QOpenGLTexture::D24S8:
    case QOpenGLTexture::D32FS8X24:

    case QOpenGLTexture::S8:

    case QOpenGLTexture::RGB_DXT1:
    case QOpenGLTexture::RGBA_DXT1:
    case QOpenGLTexture::RGBA_DXT3:
    case QOpenGLTexture::RGBA_DXT5:
    case QOpenGLTexture::R_ATI1N_UNorm:
    case QOpenGLTexture::R_ATI1N_SNorm:
    case QOpenGLTexture::RG_ATI2N_UNorm:
    case QOpenGLTexture::RG_ATI2N_SNorm:
    case QOpenGLTexture::RGB_BP_UNSIGNED_FLOAT:
    case QOpenGLTexture::RGB_BP_SIGNED_FLOAT:
    case QOpenGLTexture::RGB_BP_UNorm:
    case QOpenGLTexture::SRGB8:
    case QOpenGLTexture::SRGB8_Alpha8:
    case QOpenGLTexture::SRGB_DXT1:
    case QOpenGLTexture::SRGB_Alpha_DXT1:
    case QOpenGLTexture::SRGB_Alpha_DXT3:
    case QOpenGLTexture::SRGB_Alpha_DXT5:
    case QOpenGLTexture::SRGB_BP_UNorm:
    case QOpenGLTexture::R11_EAC_UNorm:
    case QOpenGLTexture::R11_EAC_SNorm:
    case QOpenGLTexture::RG11_EAC_UNorm:
    case QOpenGLTexture::RG11_EAC_SNorm:
    case QOpenGLTexture::RGB8_ETC2:
    case QOpenGLTexture::SRGB8_ETC2:
    case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2:
    case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2:
    case QOpenGLTexture::RGBA8_ETC2_EAC:
    case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC:
    case QOpenGLTexture::RGBA_ASTC_4x4:
    case QOpenGLTexture::RGBA_ASTC_5x4:
    case QOpenGLTexture::RGBA_ASTC_5x5:
    case QOpenGLTexture::RGBA_ASTC_6x5:
    case QOpenGLTexture::RGBA_ASTC_6x6:
    case QOpenGLTexture::RGBA_ASTC_8x5:
    case QOpenGLTexture::RGBA_ASTC_8x6:
    case QOpenGLTexture::RGBA_ASTC_8x8:
    case QOpenGLTexture::RGBA_ASTC_10x5:
    case QOpenGLTexture::RGBA_ASTC_10x6:
    case QOpenGLTexture::RGBA_ASTC_10x8:
    case QOpenGLTexture::RGBA_ASTC_10x10:
    case QOpenGLTexture::RGBA_ASTC_12x10:
    case QOpenGLTexture::RGBA_ASTC_12x12:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_4x4:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x4:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x8:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x8:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x10:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x10:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x12:
        return true;

    case QOpenGLTexture::RGB8_ETC1:
        return false;

    case QOpenGLTexture::DepthFormat:
    case QOpenGLTexture::AlphaFormat:

    case QOpenGLTexture::RGBFormat:
    case QOpenGLTexture::RGBAFormat:

    case QOpenGLTexture::LuminanceFormat:

    case QOpenGLTexture::LuminanceAlphaFormat:
        return false;
    }

    Q_UNREACHABLE();
    return false;
}

static bool isTextureTargetMultisample(QOpenGLTexture::Target target)
{
    switch (target) {
    case QOpenGLTexture::Target1D:
    case QOpenGLTexture::Target1DArray:
    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::Target2DArray:
    case QOpenGLTexture::Target3D:
    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetCubeMapArray:
        return false;

    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
        return true;

    case QOpenGLTexture::TargetRectangle:
    case QOpenGLTexture::TargetBuffer:
        return false;
    }

    Q_UNREACHABLE();
    return false;
}

bool QOpenGLTexturePrivate::isUsingImmutableStorage() const
{
    // Use immutable storage whenever possible, falling back to mutable
    // Note that if multisample textures are not supported at all, we'll still fail into
    // the mutable storage allocation
    return isSizedTextureFormat(format)
            && (isTextureTargetMultisample(target)
                ? features.testFlag(QOpenGLTexture::ImmutableMultisampleStorage)
                : features.testFlag(QOpenGLTexture::ImmutableStorage));
}

void QOpenGLTexturePrivate::allocateStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType)
{
    // Resolve the actual number of mipmap levels we can use
    mipLevels = evaluateMipLevels();

    if (isUsingImmutableStorage())
        allocateImmutableStorage();
    else
        allocateMutableStorage(pixelFormat, pixelType);
}

static QOpenGLTexture::PixelFormat pixelFormatCompatibleWithInternalFormat(QOpenGLTexture::TextureFormat internalFormat)
{
    switch (internalFormat) {
    case QOpenGLTexture::NoFormat:
        return QOpenGLTexture::NoSourceFormat;

    case QOpenGLTexture::R8_UNorm:
        return QOpenGLTexture::Red;

    case QOpenGLTexture::RG8_UNorm:
        return QOpenGLTexture::RG;

    case QOpenGLTexture::RGB8_UNorm:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::RGBA8_UNorm:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::R16_UNorm:
        return QOpenGLTexture::Red;

    case QOpenGLTexture::RG16_UNorm:
        return QOpenGLTexture::RG;

    case QOpenGLTexture::RGB16_UNorm:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::RGBA16_UNorm:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::R8_SNorm:
        return QOpenGLTexture::Red;

    case QOpenGLTexture::RG8_SNorm:
        return QOpenGLTexture::RG;

    case QOpenGLTexture::RGB8_SNorm:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::RGBA8_SNorm:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::R16_SNorm:
        return QOpenGLTexture::Red;

    case QOpenGLTexture::RG16_SNorm:
        return QOpenGLTexture::RG;

    case QOpenGLTexture::RGB16_SNorm:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::RGBA16_SNorm:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::R8U:
        return QOpenGLTexture::Red_Integer;

    case QOpenGLTexture::RG8U:
        return QOpenGLTexture::RG_Integer;

    case QOpenGLTexture::RGB8U:
        return QOpenGLTexture::RGB_Integer;

    case QOpenGLTexture::RGBA8U:
        return QOpenGLTexture::RGBA_Integer;

    case QOpenGLTexture::R16U:
        return QOpenGLTexture::Red_Integer;

    case QOpenGLTexture::RG16U:
        return QOpenGLTexture::RG_Integer;

    case QOpenGLTexture::RGB16U:
        return QOpenGLTexture::RGB_Integer;

    case QOpenGLTexture::RGBA16U:
        return QOpenGLTexture::RGBA_Integer;

    case QOpenGLTexture::R32U:
        return QOpenGLTexture::Red_Integer;

    case QOpenGLTexture::RG32U:
        return QOpenGLTexture::RG_Integer;

    case QOpenGLTexture::RGB32U:
        return QOpenGLTexture::RGB_Integer;

    case QOpenGLTexture::RGBA32U:
        return QOpenGLTexture::RGBA_Integer;

    case QOpenGLTexture::R8I:
        return QOpenGLTexture::Red_Integer;

    case QOpenGLTexture::RG8I:
        return QOpenGLTexture::RG_Integer;

    case QOpenGLTexture::RGB8I:
        return QOpenGLTexture::RGB_Integer;

    case QOpenGLTexture::RGBA8I:
        return QOpenGLTexture::RGBA_Integer;

    case QOpenGLTexture::R16I:
        return QOpenGLTexture::Red_Integer;

    case QOpenGLTexture::RG16I:
        return QOpenGLTexture::RG_Integer;

    case QOpenGLTexture::RGB16I:
        return QOpenGLTexture::RGB_Integer;

    case QOpenGLTexture::RGBA16I:
        return QOpenGLTexture::RGBA_Integer;

    case QOpenGLTexture::R32I:
        return QOpenGLTexture::Red_Integer;

    case QOpenGLTexture::RG32I:
        return QOpenGLTexture::RG_Integer;

    case QOpenGLTexture::RGB32I:
        return QOpenGLTexture::RGB_Integer;

    case QOpenGLTexture::RGBA32I:
        return QOpenGLTexture::RGBA_Integer;

    case QOpenGLTexture::R16F:
        return QOpenGLTexture::Red;

    case QOpenGLTexture::RG16F:
        return QOpenGLTexture::RG;

    case QOpenGLTexture::RGB16F:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::RGBA16F:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::R32F:
        return QOpenGLTexture::Red;

    case QOpenGLTexture::RG32F:
        return QOpenGLTexture::RG;

    case QOpenGLTexture::RGB32F:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::RGBA32F:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::RGB9E5:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::RG11B10F:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::RG3B2:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::R5G6B5:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::RGB5A1:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::RGBA4:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::RGB10A2:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::D16:
    case QOpenGLTexture::D24:
    case QOpenGLTexture::D32:
    case QOpenGLTexture::D32F:
        return QOpenGLTexture::Depth;

    case QOpenGLTexture::D24S8:
    case QOpenGLTexture::D32FS8X24:
        return QOpenGLTexture::DepthStencil;

    case QOpenGLTexture::S8:
        return QOpenGLTexture::Stencil;

    case QOpenGLTexture::RGB_DXT1:
    case QOpenGLTexture::RGBA_DXT1:
    case QOpenGLTexture::RGBA_DXT3:
    case QOpenGLTexture::RGBA_DXT5:
    case QOpenGLTexture::R_ATI1N_UNorm:
    case QOpenGLTexture::R_ATI1N_SNorm:
    case QOpenGLTexture::RG_ATI2N_UNorm:
    case QOpenGLTexture::RG_ATI2N_SNorm:
    case QOpenGLTexture::RGB_BP_UNSIGNED_FLOAT:
    case QOpenGLTexture::RGB_BP_SIGNED_FLOAT:
    case QOpenGLTexture::RGB_BP_UNorm:
    case QOpenGLTexture::SRGB8:
    case QOpenGLTexture::SRGB8_Alpha8:
    case QOpenGLTexture::SRGB_DXT1:
    case QOpenGLTexture::SRGB_Alpha_DXT1:
    case QOpenGLTexture::SRGB_Alpha_DXT3:
    case QOpenGLTexture::SRGB_Alpha_DXT5:
    case QOpenGLTexture::SRGB_BP_UNorm:
    case QOpenGLTexture::RGB8_ETC1:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::R11_EAC_UNorm:
    case QOpenGLTexture::R11_EAC_SNorm:
        return QOpenGLTexture::Red;

    case QOpenGLTexture::RG11_EAC_UNorm:
    case QOpenGLTexture::RG11_EAC_SNorm:
        return QOpenGLTexture::RG;

    case QOpenGLTexture::RGB8_ETC2:
    case QOpenGLTexture::SRGB8_ETC2:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2:
    case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::RGBA8_ETC2_EAC:
    case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::RGBA_ASTC_4x4:
    case QOpenGLTexture::RGBA_ASTC_5x4:
    case QOpenGLTexture::RGBA_ASTC_5x5:
    case QOpenGLTexture::RGBA_ASTC_6x5:
    case QOpenGLTexture::RGBA_ASTC_6x6:
    case QOpenGLTexture::RGBA_ASTC_8x5:
    case QOpenGLTexture::RGBA_ASTC_8x6:
    case QOpenGLTexture::RGBA_ASTC_8x8:
    case QOpenGLTexture::RGBA_ASTC_10x5:
    case QOpenGLTexture::RGBA_ASTC_10x6:
    case QOpenGLTexture::RGBA_ASTC_10x8:
    case QOpenGLTexture::RGBA_ASTC_10x10:
    case QOpenGLTexture::RGBA_ASTC_12x10:
    case QOpenGLTexture::RGBA_ASTC_12x12:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_4x4:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x4:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x8:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x8:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x10:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x10:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x12:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::DepthFormat:
        return QOpenGLTexture::Depth;

    case QOpenGLTexture::AlphaFormat:
        return QOpenGLTexture::Alpha;

    case QOpenGLTexture::RGBFormat:
        return QOpenGLTexture::RGB;

    case QOpenGLTexture::RGBAFormat:
        return QOpenGLTexture::RGBA;

    case QOpenGLTexture::LuminanceFormat:
        return QOpenGLTexture::Luminance;

    case QOpenGLTexture::LuminanceAlphaFormat:
        return QOpenGLTexture::LuminanceAlpha;
    }

    Q_UNREACHABLE();
    return QOpenGLTexture::NoSourceFormat;
}

static QOpenGLTexture::PixelType pixelTypeCompatibleWithInternalFormat(QOpenGLTexture::TextureFormat internalFormat)
{
    switch (internalFormat) {
    case QOpenGLTexture::NoFormat:
        return QOpenGLTexture::NoPixelType;

    case QOpenGLTexture::R8_UNorm:
    case QOpenGLTexture::RG8_UNorm:
    case QOpenGLTexture::RGB8_UNorm:
    case QOpenGLTexture::RGBA8_UNorm:
    case QOpenGLTexture::R16_UNorm:
    case QOpenGLTexture::RG16_UNorm:
    case QOpenGLTexture::RGB16_UNorm:
    case QOpenGLTexture::RGBA16_UNorm:
        return QOpenGLTexture::UInt8;

    case QOpenGLTexture::R8_SNorm:
    case QOpenGLTexture::RG8_SNorm:
    case QOpenGLTexture::RGB8_SNorm:
    case QOpenGLTexture::RGBA8_SNorm:
    case QOpenGLTexture::R16_SNorm:
    case QOpenGLTexture::RG16_SNorm:
    case QOpenGLTexture::RGB16_SNorm:
    case QOpenGLTexture::RGBA16_SNorm:
        return QOpenGLTexture::Int8;

    case QOpenGLTexture::R8U:
    case QOpenGLTexture::RG8U:
    case QOpenGLTexture::RGB8U:
    case QOpenGLTexture::RGBA8U:
    case QOpenGLTexture::R16U:
    case QOpenGLTexture::RG16U:
    case QOpenGLTexture::RGB16U:
    case QOpenGLTexture::RGBA16U:
    case QOpenGLTexture::R32U:
    case QOpenGLTexture::RG32U:
    case QOpenGLTexture::RGB32U:
    case QOpenGLTexture::RGBA32U:
        return QOpenGLTexture::UInt8;

    case QOpenGLTexture::R8I:
    case QOpenGLTexture::RG8I:
    case QOpenGLTexture::RGB8I:
    case QOpenGLTexture::RGBA8I:
    case QOpenGLTexture::R16I:
    case QOpenGLTexture::RG16I:
    case QOpenGLTexture::RGB16I:
    case QOpenGLTexture::RGBA16I:
    case QOpenGLTexture::R32I:
    case QOpenGLTexture::RG32I:
    case QOpenGLTexture::RGB32I:
    case QOpenGLTexture::RGBA32I:
        return QOpenGLTexture::Int8;

    case QOpenGLTexture::R16F:
    case QOpenGLTexture::RG16F:
    case QOpenGLTexture::RGB16F:
    case QOpenGLTexture::RGBA16F:
        return QOpenGLTexture::Float16;

    case QOpenGLTexture::R32F:
    case QOpenGLTexture::RG32F:
    case QOpenGLTexture::RGB32F:
    case QOpenGLTexture::RGBA32F:
        return QOpenGLTexture::Float32;

    case QOpenGLTexture::RGB9E5:
        return QOpenGLTexture::UInt16_RGB5A1_Rev;

    case QOpenGLTexture::RG11B10F:
        return QOpenGLTexture::UInt32_RG11B10F;

    case QOpenGLTexture::RG3B2:
        return QOpenGLTexture::UInt8_RG3B2;

    case QOpenGLTexture::R5G6B5:
        return QOpenGLTexture::UInt16_R5G6B5;

    case QOpenGLTexture::RGB5A1:
        return QOpenGLTexture::UInt16_RGB5A1;

    case QOpenGLTexture::RGBA4:
        return QOpenGLTexture::UInt16_RGBA4;

    case QOpenGLTexture::RGB10A2:
        return QOpenGLTexture::UInt32_RGB10A2;

    case QOpenGLTexture::D16:
        return QOpenGLTexture::UInt16;

    case QOpenGLTexture::D24:
    case QOpenGLTexture::D32:
        return QOpenGLTexture::UInt32;

    case QOpenGLTexture::D32F:
        return QOpenGLTexture::Float32;

    case QOpenGLTexture::D24S8:
        return QOpenGLTexture::UInt32_D24S8;

    case QOpenGLTexture::D32FS8X24:
        return QOpenGLTexture::Float32_D32_UInt32_S8_X24;

    case QOpenGLTexture::S8:
        return QOpenGLTexture::UInt8;

    case QOpenGLTexture::RGB_DXT1:
    case QOpenGLTexture::RGBA_DXT1:
    case QOpenGLTexture::RGBA_DXT3:
    case QOpenGLTexture::RGBA_DXT5:
    case QOpenGLTexture::R_ATI1N_UNorm:
    case QOpenGLTexture::R_ATI1N_SNorm:
    case QOpenGLTexture::RG_ATI2N_UNorm:
    case QOpenGLTexture::RG_ATI2N_SNorm:
    case QOpenGLTexture::RGB_BP_UNSIGNED_FLOAT:
    case QOpenGLTexture::RGB_BP_SIGNED_FLOAT:
    case QOpenGLTexture::RGB_BP_UNorm:
    case QOpenGLTexture::SRGB8:
    case QOpenGLTexture::SRGB8_Alpha8:
    case QOpenGLTexture::SRGB_DXT1:
    case QOpenGLTexture::SRGB_Alpha_DXT1:
    case QOpenGLTexture::SRGB_Alpha_DXT3:
    case QOpenGLTexture::SRGB_Alpha_DXT5:
    case QOpenGLTexture::SRGB_BP_UNorm:
    case QOpenGLTexture::R11_EAC_UNorm:
    case QOpenGLTexture::R11_EAC_SNorm:
    case QOpenGLTexture::RG11_EAC_UNorm:
    case QOpenGLTexture::RG11_EAC_SNorm:
    case QOpenGLTexture::RGB8_ETC2:
    case QOpenGLTexture::SRGB8_ETC2:
    case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2:
    case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2:
    case QOpenGLTexture::RGBA8_ETC2_EAC:
    case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC:
    case QOpenGLTexture::RGB8_ETC1:
    case QOpenGLTexture::RGBA_ASTC_4x4:
    case QOpenGLTexture::RGBA_ASTC_5x4:
    case QOpenGLTexture::RGBA_ASTC_5x5:
    case QOpenGLTexture::RGBA_ASTC_6x5:
    case QOpenGLTexture::RGBA_ASTC_6x6:
    case QOpenGLTexture::RGBA_ASTC_8x5:
    case QOpenGLTexture::RGBA_ASTC_8x6:
    case QOpenGLTexture::RGBA_ASTC_8x8:
    case QOpenGLTexture::RGBA_ASTC_10x5:
    case QOpenGLTexture::RGBA_ASTC_10x6:
    case QOpenGLTexture::RGBA_ASTC_10x8:
    case QOpenGLTexture::RGBA_ASTC_10x10:
    case QOpenGLTexture::RGBA_ASTC_12x10:
    case QOpenGLTexture::RGBA_ASTC_12x12:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_4x4:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x4:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x8:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x8:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x10:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x10:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x12:
        return QOpenGLTexture::UInt8;

    case QOpenGLTexture::DepthFormat:
        return QOpenGLTexture::UInt32;

    case QOpenGLTexture::AlphaFormat:
    case QOpenGLTexture::RGBFormat:
    case QOpenGLTexture::RGBAFormat:
    case QOpenGLTexture::LuminanceFormat:
    case QOpenGLTexture::LuminanceAlphaFormat:
        return QOpenGLTexture::UInt8;
    }

    Q_UNREACHABLE();
    return QOpenGLTexture::NoPixelType;
}

static bool isCompressedFormat(QOpenGLTexture::TextureFormat internalFormat)
{
    switch (internalFormat) {
    case QOpenGLTexture::NoFormat:

    case QOpenGLTexture::R8_UNorm:
    case QOpenGLTexture::RG8_UNorm:
    case QOpenGLTexture::RGB8_UNorm:
    case QOpenGLTexture::RGBA8_UNorm:
    case QOpenGLTexture::R16_UNorm:
    case QOpenGLTexture::RG16_UNorm:
    case QOpenGLTexture::RGB16_UNorm:
    case QOpenGLTexture::RGBA16_UNorm:
    case QOpenGLTexture::R8_SNorm:
    case QOpenGLTexture::RG8_SNorm:
    case QOpenGLTexture::RGB8_SNorm:
    case QOpenGLTexture::RGBA8_SNorm:
    case QOpenGLTexture::R16_SNorm:
    case QOpenGLTexture::RG16_SNorm:
    case QOpenGLTexture::RGB16_SNorm:
    case QOpenGLTexture::RGBA16_SNorm:
    case QOpenGLTexture::R8U:
    case QOpenGLTexture::RG8U:
    case QOpenGLTexture::RGB8U:
    case QOpenGLTexture::RGBA8U:
    case QOpenGLTexture::R16U:
    case QOpenGLTexture::RG16U:
    case QOpenGLTexture::RGB16U:
    case QOpenGLTexture::RGBA16U:
    case QOpenGLTexture::R32U:
    case QOpenGLTexture::RG32U:
    case QOpenGLTexture::RGB32U:
    case QOpenGLTexture::RGBA32U:
    case QOpenGLTexture::R8I:
    case QOpenGLTexture::RG8I:
    case QOpenGLTexture::RGB8I:
    case QOpenGLTexture::RGBA8I:
    case QOpenGLTexture::R16I:
    case QOpenGLTexture::RG16I:
    case QOpenGLTexture::RGB16I:
    case QOpenGLTexture::RGBA16I:
    case QOpenGLTexture::R32I:
    case QOpenGLTexture::RG32I:
    case QOpenGLTexture::RGB32I:
    case QOpenGLTexture::RGBA32I:
    case QOpenGLTexture::R16F:
    case QOpenGLTexture::RG16F:
    case QOpenGLTexture::RGB16F:
    case QOpenGLTexture::RGBA16F:
    case QOpenGLTexture::R32F:
    case QOpenGLTexture::RG32F:
    case QOpenGLTexture::RGB32F:
    case QOpenGLTexture::RGBA32F:
    case QOpenGLTexture::RGB9E5:
    case QOpenGLTexture::RG11B10F:
    case QOpenGLTexture::RG3B2:
    case QOpenGLTexture::R5G6B5:
    case QOpenGLTexture::RGB5A1:
    case QOpenGLTexture::RGBA4:
    case QOpenGLTexture::RGB10A2:

    case QOpenGLTexture::D16:
    case QOpenGLTexture::D24:
    case QOpenGLTexture::D32:
    case QOpenGLTexture::D32F:

    case QOpenGLTexture::D24S8:
    case QOpenGLTexture::D32FS8X24:

    case QOpenGLTexture::S8:
        return false;

    case QOpenGLTexture::RGB_DXT1:
    case QOpenGLTexture::RGBA_DXT1:
    case QOpenGLTexture::RGBA_DXT3:
    case QOpenGLTexture::RGBA_DXT5:
    case QOpenGLTexture::R_ATI1N_UNorm:
    case QOpenGLTexture::R_ATI1N_SNorm:
    case QOpenGLTexture::RG_ATI2N_UNorm:
    case QOpenGLTexture::RG_ATI2N_SNorm:
    case QOpenGLTexture::RGB_BP_UNSIGNED_FLOAT:
    case QOpenGLTexture::RGB_BP_SIGNED_FLOAT:
    case QOpenGLTexture::RGB_BP_UNorm:
    case QOpenGLTexture::SRGB8:
    case QOpenGLTexture::SRGB8_Alpha8:
    case QOpenGLTexture::SRGB_DXT1:
    case QOpenGLTexture::SRGB_Alpha_DXT1:
    case QOpenGLTexture::SRGB_Alpha_DXT3:
    case QOpenGLTexture::SRGB_Alpha_DXT5:
    case QOpenGLTexture::SRGB_BP_UNorm:
    case QOpenGLTexture::R11_EAC_UNorm:
    case QOpenGLTexture::R11_EAC_SNorm:
    case QOpenGLTexture::RG11_EAC_UNorm:
    case QOpenGLTexture::RG11_EAC_SNorm:
    case QOpenGLTexture::RGB8_ETC2:
    case QOpenGLTexture::SRGB8_ETC2:
    case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2:
    case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2:
    case QOpenGLTexture::RGBA8_ETC2_EAC:
    case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC:
    case QOpenGLTexture::RGB8_ETC1:
    case QOpenGLTexture::RGBA_ASTC_4x4:
    case QOpenGLTexture::RGBA_ASTC_5x4:
    case QOpenGLTexture::RGBA_ASTC_5x5:
    case QOpenGLTexture::RGBA_ASTC_6x5:
    case QOpenGLTexture::RGBA_ASTC_6x6:
    case QOpenGLTexture::RGBA_ASTC_8x5:
    case QOpenGLTexture::RGBA_ASTC_8x6:
    case QOpenGLTexture::RGBA_ASTC_8x8:
    case QOpenGLTexture::RGBA_ASTC_10x5:
    case QOpenGLTexture::RGBA_ASTC_10x6:
    case QOpenGLTexture::RGBA_ASTC_10x8:
    case QOpenGLTexture::RGBA_ASTC_10x10:
    case QOpenGLTexture::RGBA_ASTC_12x10:
    case QOpenGLTexture::RGBA_ASTC_12x12:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_4x4:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x4:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x8:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x8:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x10:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x10:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x12:
        return true;

    case QOpenGLTexture::DepthFormat:
    case QOpenGLTexture::AlphaFormat:
    case QOpenGLTexture::RGBFormat:
    case QOpenGLTexture::RGBAFormat:
    case QOpenGLTexture::LuminanceFormat:
    case QOpenGLTexture::LuminanceAlphaFormat:
        return false;
    }

    Q_UNREACHABLE();
    return false;
}

void QOpenGLTexturePrivate::allocateMutableStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType)
{
    // There is no way to allocate mutable storage for compressed textures in in
    // versions older than OpenGL 3.1 and OpenGL ES 3.0, because the older specs
    // do not mandate accepting null data pointers for glCompressedTexImage*D,
    // unlike glTexImage*D (which in turn does not accept compressed formats).
    if (isCompressedFormat(format)) {
        storageAllocated = true;
        return;
    }

    switch (target) {
    case QOpenGLTexture::TargetBuffer:
        // Buffer textures get their storage from an external OpenGL buffer
        qWarning("Buffer textures do not allocate storage");
        return;

    case QOpenGLTexture::Target1D:
        if (features.testFlag(QOpenGLTexture::Texture1D)) {
            for (int level = 0; level < mipLevels; ++level)
                texFuncs->glTextureImage1D(textureId, target, bindingTarget, level, format,
                                           mipLevelSize(level, dimensions[0]),
                                           0,
                                           pixelFormat, pixelType, 0);
        } else {
            qWarning("1D textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::Target1DArray:
        if (features.testFlag(QOpenGLTexture::Texture1D)
                && features.testFlag(QOpenGLTexture::TextureArrays)) {
            for (int level = 0; level < mipLevels; ++level)
                texFuncs->glTextureImage2D(textureId, target, bindingTarget, level, format,
                                           mipLevelSize(level, dimensions[0]),
                                           layers,
                                           0,
                                           pixelFormat, pixelType, 0);
        } else {
            qWarning("1D array textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::TargetRectangle:
        for (int level = 0; level < mipLevels; ++level)
            texFuncs->glTextureImage2D(textureId, target, bindingTarget, level, format,
                                       mipLevelSize(level, dimensions[0]),
                                       mipLevelSize(level, dimensions[1]),
                                       0,
                                       pixelFormat, pixelType, 0);
        break;

    case QOpenGLTexture::TargetCubeMap: {
        // Cubemaps are the odd one out. We have to allocate storage for each
        // face and miplevel using the special cubemap face targets rather than
        // GL_TARGET_CUBEMAP.
        const QOpenGLTexture::CubeMapFace faceTargets[] = {
            QOpenGLTexture::CubeMapPositiveX, QOpenGLTexture::CubeMapNegativeX,
            QOpenGLTexture::CubeMapPositiveY, QOpenGLTexture::CubeMapNegativeY,
            QOpenGLTexture::CubeMapPositiveZ, QOpenGLTexture::CubeMapNegativeZ
        };

        for (int faceTarget = 0; faceTarget < 6; ++faceTarget) {
            for (int level = 0; level < mipLevels; ++level) {
                texFuncs->glTextureImage2D(textureId, faceTargets[faceTarget], bindingTarget,
                                           level, format,
                                           mipLevelSize(level, dimensions[0]),
                                           mipLevelSize(level, dimensions[1]),
                                           0,
                                           pixelFormat, pixelType, 0);
            }
        }
        break;
    }

    case QOpenGLTexture::Target2DArray:
        if (features.testFlag(QOpenGLTexture::TextureArrays)) {
            for (int level = 0; level < mipLevels; ++level)
                texFuncs->glTextureImage3D(textureId, target, bindingTarget, level, format,
                                           mipLevelSize(level, dimensions[0]),
                                           mipLevelSize(level, dimensions[1]),
                                           layers,
                                           0,
                                           pixelFormat, pixelType, 0);
        } else {
            qWarning("Array textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::TargetCubeMapArray:
        // Cubemap arrays must specify number of layer-faces (6 * layers) as depth parameter
        if (features.testFlag(QOpenGLTexture::TextureCubeMapArrays)) {
            for (int level = 0; level < mipLevels; ++level)
                texFuncs->glTextureImage3D(textureId, target, bindingTarget, level, format,
                                           mipLevelSize(level, dimensions[0]),
                                           mipLevelSize(level, dimensions[1]),
                                           6 * layers,
                                           0,
                                           pixelFormat, pixelType, 0);
        } else {
            qWarning("Cubemap Array textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::Target3D:
        if (features.testFlag(QOpenGLTexture::Texture3D)) {
            for (int level = 0; level < mipLevels; ++level)
                texFuncs->glTextureImage3D(textureId, target, bindingTarget, level, format,
                                           mipLevelSize(level, dimensions[0]),
                                           mipLevelSize(level, dimensions[1]),
                                           mipLevelSize(level, dimensions[2]),
                                           0,
                                           pixelFormat, pixelType, 0);
        } else {
            qWarning("3D textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::Target2DMultisample:
        if (features.testFlag(QOpenGLTexture::TextureMultisample)) {
            texFuncs->glTextureImage2DMultisample(textureId, target, bindingTarget, samples, format,
                                                  dimensions[0], dimensions[1],
                                                  fixedSamplePositions);
        } else {
            qWarning("Multisample textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::Target2DMultisampleArray:
        if (features.testFlag(QOpenGLTexture::TextureMultisample)
                && features.testFlag(QOpenGLTexture::TextureArrays)) {
            texFuncs->glTextureImage3DMultisample(textureId, target, bindingTarget, samples, format,
                                                  dimensions[0], dimensions[1], layers,
                                                  fixedSamplePositions);
        } else {
            qWarning("Multisample array textures are not supported");
            return;
        }
        break;
    }

    storageAllocated = true;
}

void QOpenGLTexturePrivate::allocateImmutableStorage()
{
    switch (target) {
    case QOpenGLTexture::TargetBuffer:
        // Buffer textures get their storage from an external OpenGL buffer
        qWarning("Buffer textures do not allocate storage");
        return;

    case QOpenGLTexture::Target1D:
        if (features.testFlag(QOpenGLTexture::Texture1D)) {
            texFuncs->glTextureStorage1D(textureId, target, bindingTarget, mipLevels, format,
                                         dimensions[0]);
        } else {
            qWarning("1D textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::Target1DArray:
        if (features.testFlag(QOpenGLTexture::Texture1D)
                && features.testFlag(QOpenGLTexture::TextureArrays)) {
            texFuncs->glTextureStorage2D(textureId, target, bindingTarget, mipLevels, format,
                                         dimensions[0], layers);
        } else {
            qWarning("1D array textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetRectangle:
        texFuncs->glTextureStorage2D(textureId, target, bindingTarget, mipLevels, format,
                                     dimensions[0], dimensions[1]);
        break;

    case QOpenGLTexture::Target2DArray:
        if (features.testFlag(QOpenGLTexture::TextureArrays)) {
            texFuncs->glTextureStorage3D(textureId, target, bindingTarget, mipLevels, format,
                                         dimensions[0], dimensions[1], layers);
        } else {
            qWarning("Array textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::TargetCubeMapArray:
        // Cubemap arrays must specify number of layer-faces (6 * layers) as depth parameter
        if (features.testFlag(QOpenGLTexture::TextureCubeMapArrays)) {
            texFuncs->glTextureStorage3D(textureId, target, bindingTarget, mipLevels, format,
                                         dimensions[0], dimensions[1], 6 * layers);
        } else {
            qWarning("Cubemap Array textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::Target3D:
        if (features.testFlag(QOpenGLTexture::Texture3D)) {
            texFuncs->glTextureStorage3D(textureId, target, bindingTarget, mipLevels, format,
                                         dimensions[0], dimensions[1], dimensions[2]);
        } else {
            qWarning("3D textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::Target2DMultisample:
        if (features.testFlag(QOpenGLTexture::ImmutableMultisampleStorage)) {
            texFuncs->glTextureStorage2DMultisample(textureId, target, bindingTarget, samples, format,
                                                    dimensions[0], dimensions[1],
                                                    fixedSamplePositions);
        } else {
            qWarning("Multisample textures are not supported");
            return;
        }
        break;

    case QOpenGLTexture::Target2DMultisampleArray:
        if (features.testFlag(QOpenGLTexture::ImmutableMultisampleStorage)
                && features.testFlag(QOpenGLTexture::TextureArrays)) {
            texFuncs->glTextureStorage3DMultisample(textureId, target, bindingTarget, samples, format,
                                                    dimensions[0], dimensions[1], layers,
                                                    fixedSamplePositions);
        } else {
            qWarning("Multisample array textures are not supported");
            return;
        }
        break;
    }

    storageAllocated = true;
}

void QOpenGLTexturePrivate::setData(int mipLevel, int layer, int layerCount, QOpenGLTexture::CubeMapFace cubeFace,
                                    QOpenGLTexture::PixelFormat sourceFormat, QOpenGLTexture::PixelType sourceType,
                                    const void *data, const QOpenGLPixelTransferOptions * const options)
{
    switch (target) {
    case QOpenGLTexture::Target1D:
        Q_UNUSED(layer);
        Q_UNUSED(cubeFace);
        Q_UNUSED(layerCount);
        texFuncs->glTextureSubImage1D(textureId, target, bindingTarget, mipLevel,
                                      0, mipLevelSize( mipLevel, dimensions[0] ),
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::Target1DArray:
        Q_UNUSED(cubeFace);
        texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, mipLevel,
                                      0, layer,
                                      mipLevelSize(mipLevel, dimensions[0]),
                                      layerCount,
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::Target2D:
        Q_UNUSED(layer);
        Q_UNUSED(cubeFace);
        Q_UNUSED(layerCount);
        texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, mipLevel,
                                      0, 0,
                                      mipLevelSize(mipLevel, dimensions[0]),
                                      mipLevelSize(mipLevel, dimensions[1]),
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::Target2DArray:
        Q_UNUSED(cubeFace);
        texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel,
                                      0, 0, layer,
                                      mipLevelSize(mipLevel, dimensions[0]),
                                      mipLevelSize(mipLevel, dimensions[1]),
                                      layerCount,
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::Target3D:
        Q_UNUSED(cubeFace);
        Q_UNUSED(layerCount);
        texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel,
                                      0, 0, layer,
                                      mipLevelSize(mipLevel, dimensions[0]),
                                      mipLevelSize(mipLevel, dimensions[1]),
                                      mipLevelSize(mipLevel, dimensions[2]),
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::TargetCubeMap:
        Q_UNUSED(layer);
        Q_UNUSED(layerCount);
        texFuncs->glTextureSubImage2D(textureId, cubeFace, bindingTarget, mipLevel,
                                      0, 0,
                                      mipLevelSize(mipLevel, dimensions[0]),
                                      mipLevelSize(mipLevel, dimensions[1]),
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::TargetCubeMapArray: {
        int faceIndex = cubeFace - QOpenGLTexture::CubeMapPositiveX;
        int layerFace = 6 * layer + faceIndex;
        texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel,
                                      0, 0, layerFace,
                                      mipLevelSize(mipLevel, dimensions[0]),
                                      mipLevelSize(mipLevel, dimensions[1]),
                                      layerCount,
                                      sourceFormat, sourceType, data, options);
        break;
    }

    case QOpenGLTexture::TargetRectangle:
        Q_UNUSED(mipLevel);
        Q_UNUSED(layer);
        Q_UNUSED(cubeFace);
        Q_UNUSED(layerCount);
        texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, 0,
                                      0, 0,
                                      dimensions[0],
                                      dimensions[1],
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
    case QOpenGLTexture::TargetBuffer:
        // We don't upload pixel data for these targets
        qWarning("QOpenGLTexture::setData(): Texture target does not support pixel data upload");
        break;
    }

    // If requested perform automatic mip map generation
    if (mipLevel == 0 && autoGenerateMipMaps && mipLevels > 1) {
        Q_Q(QOpenGLTexture);
        q->generateMipMaps();
    }
}

void QOpenGLTexturePrivate::setData(int xOffset, int yOffset, int zOffset, int width, int height, int depth,
                                    int mipLevel, int layer, int layerCount, QOpenGLTexture::CubeMapFace cubeFace,
                                    QOpenGLTexture::PixelFormat sourceFormat, QOpenGLTexture::PixelType sourceType,
                                    const void *data, const QOpenGLPixelTransferOptions * const options)
{
    switch (target) {
    case QOpenGLTexture::Target1D:
        Q_UNUSED(layer);
        Q_UNUSED(cubeFace);
        Q_UNUSED(layerCount);
        Q_UNUSED(yOffset);
        Q_UNUSED(zOffset);
        Q_UNUSED(height);
        Q_UNUSED(depth);
        texFuncs->glTextureSubImage1D(textureId, target, bindingTarget, mipLevel,
                                      xOffset, width,
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::Target1DArray:
        Q_UNUSED(cubeFace);
        Q_UNUSED(yOffset);
        Q_UNUSED(zOffset);
        Q_UNUSED(height);
        Q_UNUSED(depth);
        texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, mipLevel,
                                      xOffset, layer,
                                      width,
                                      layerCount,
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::Target2D:
        Q_UNUSED(layer);
        Q_UNUSED(cubeFace);
        Q_UNUSED(layerCount);
        Q_UNUSED(zOffset);
        Q_UNUSED(depth);
        texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, mipLevel,
                                      xOffset, yOffset,
                                      width, height,
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::Target2DArray:
        Q_UNUSED(cubeFace);
        Q_UNUSED(zOffset);
        Q_UNUSED(depth);
        texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel,
                                      xOffset, yOffset, layer,
                                      width, height, layerCount,
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::Target3D:
        Q_UNUSED(cubeFace);
        Q_UNUSED(layerCount);
        texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel,
                                      xOffset, yOffset, zOffset,
                                      width, height, depth,
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::TargetCubeMap:
        Q_UNUSED(layer);
        Q_UNUSED(layerCount);
        Q_UNUSED(zOffset);
        Q_UNUSED(depth);
        texFuncs->glTextureSubImage2D(textureId, cubeFace, bindingTarget, mipLevel,
                                      xOffset, yOffset,
                                      width, height,
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::TargetCubeMapArray: {
        Q_UNUSED(zOffset);
        Q_UNUSED(depth);
        int faceIndex = cubeFace - QOpenGLTexture::CubeMapPositiveX;
        int layerFace = 6 * layer + faceIndex;
        texFuncs->glTextureSubImage3D(textureId, target, bindingTarget, mipLevel,
                                      xOffset, yOffset, layerFace,
                                      width, height,
                                      layerCount,
                                      sourceFormat, sourceType, data, options);
        break;
    }

    case QOpenGLTexture::TargetRectangle:
        Q_UNUSED(mipLevel);
        Q_UNUSED(layer);
        Q_UNUSED(cubeFace);
        Q_UNUSED(layerCount);
        Q_UNUSED(zOffset);
        Q_UNUSED(depth);
        texFuncs->glTextureSubImage2D(textureId, target, bindingTarget, 0,
                                      xOffset, yOffset,
                                      width, height,
                                      sourceFormat, sourceType, data, options);
        break;

    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
    case QOpenGLTexture::TargetBuffer:
        // We don't upload pixel data for these targets
        qWarning("QOpenGLTexture::setData(): Texture target does not support pixel data upload");
        break;
    }

    // If requested perform automatic mip map generation
    if (mipLevel == 0 && autoGenerateMipMaps && mipLevels > 1) {
        Q_Q(QOpenGLTexture);
        q->generateMipMaps();
    }
}


void QOpenGLTexturePrivate::setCompressedData(int mipLevel, int layer, int layerCount,
                                              QOpenGLTexture::CubeMapFace cubeFace,
                                              int dataSize, const void *data,
                                              const QOpenGLPixelTransferOptions * const options)
{
    if (!isCompressedFormat(format)) {
        qWarning("Cannot set compressed data for non-compressed format 0x%x", format);
        return;
    }

    const bool needsFullSpec = !isUsingImmutableStorage(); // was allocateStorage() a no-op?

    switch (target) {
    case QOpenGLTexture::Target1D:
        Q_UNUSED(layer);
        Q_UNUSED(cubeFace);
        Q_UNUSED(layerCount);
        if (needsFullSpec) {
            texFuncs->glCompressedTextureImage1D(textureId, target, bindingTarget, mipLevel,
                                                 format,
                                                 mipLevelSize(mipLevel, dimensions[0]),
                                                 0, dataSize, data, options);
        } else {
            texFuncs->glCompressedTextureSubImage1D(textureId, target, bindingTarget, mipLevel,
                                                    0, mipLevelSize( mipLevel, dimensions[0] ),
                                                    format, dataSize, data, options);
        }
        break;

    case QOpenGLTexture::Target1DArray:
        Q_UNUSED(cubeFace);
        if (!needsFullSpec) {
            texFuncs->glCompressedTextureSubImage2D(textureId, target, bindingTarget, mipLevel,
                                                    0, layer,
                                                    mipLevelSize(mipLevel, dimensions[0]),
                                                    layerCount,
                                                    format, dataSize, data, options);
        }
        break;

    case QOpenGLTexture::Target2D:
        Q_UNUSED(layer);
        Q_UNUSED(cubeFace);
        Q_UNUSED(layerCount);
        if (needsFullSpec) {
            texFuncs->glCompressedTextureImage2D(textureId, target, bindingTarget, mipLevel,
                                                 format,
                                                 mipLevelSize(mipLevel, dimensions[0]),
                                                 mipLevelSize(mipLevel, dimensions[1]),
                                                 0, dataSize, data, options);
        } else {
            texFuncs->glCompressedTextureSubImage2D(textureId, target, bindingTarget, mipLevel,
                                                    0, 0,
                                                    mipLevelSize(mipLevel, dimensions[0]),
                                                    mipLevelSize(mipLevel, dimensions[1]),
                                                    format, dataSize, data, options);
        }
        break;

    case QOpenGLTexture::Target2DArray:
        Q_UNUSED(cubeFace);
        if (!needsFullSpec) {
            texFuncs->glCompressedTextureSubImage3D(textureId, target, bindingTarget, mipLevel,
                                                    0, 0, layer,
                                                    mipLevelSize(mipLevel, dimensions[0]),
                                                    mipLevelSize(mipLevel, dimensions[1]),
                                                    layerCount,
                                                    format, dataSize, data, options);
        }
        break;

    case QOpenGLTexture::Target3D:
        Q_UNUSED(cubeFace);
        Q_UNUSED(layerCount);
        if (needsFullSpec) {
            texFuncs->glCompressedTextureImage3D(textureId, target, bindingTarget, mipLevel,
                                                 format,
                                                 mipLevelSize(mipLevel, dimensions[0]),
                                                 mipLevelSize(mipLevel, dimensions[1]),
                                                 mipLevelSize(mipLevel, dimensions[2]),
                                                 0, dataSize, data, options);
        } else {
            texFuncs->glCompressedTextureSubImage3D(textureId, target, bindingTarget, mipLevel,
                                                    0, 0, layer,
                                                    mipLevelSize(mipLevel, dimensions[0]),
                                                    mipLevelSize(mipLevel, dimensions[1]),
                                                    mipLevelSize(mipLevel, dimensions[2]),
                                                    format, dataSize, data, options);
        }
        break;

    case QOpenGLTexture::TargetCubeMap:
        Q_UNUSED(layer);
        Q_UNUSED(layerCount);
        if (needsFullSpec) {
            texFuncs->glCompressedTextureImage2D(textureId, cubeFace, bindingTarget, mipLevel,
                                                 format,
                                                 mipLevelSize(mipLevel, dimensions[0]),
                                                 mipLevelSize(mipLevel, dimensions[1]),
                                                 0, dataSize, data, options);
        } else {
            texFuncs->glCompressedTextureSubImage2D(textureId, cubeFace, bindingTarget, mipLevel,
                                                    0, 0,
                                                    mipLevelSize(mipLevel, dimensions[0]),
                                                    mipLevelSize(mipLevel, dimensions[1]),
                                                    format, dataSize, data, options);
        }
        break;

    case QOpenGLTexture::TargetCubeMapArray: {
        int faceIndex = cubeFace - QOpenGLTexture::CubeMapPositiveX;
        int layerFace = 6 * layer + faceIndex;
        if (!needsFullSpec) {
            texFuncs->glCompressedTextureSubImage3D(textureId, target, bindingTarget, mipLevel,
                                                    0, 0, layerFace,
                                                    mipLevelSize(mipLevel, dimensions[0]),
                                                    mipLevelSize(mipLevel, dimensions[1]),
                                                    layerCount,
                                                    format, dataSize, data, options);
        }
        break;
    }

    case QOpenGLTexture::TargetRectangle:
    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
    case QOpenGLTexture::TargetBuffer:
        // We don't upload pixel data for these targets
        qWarning("QOpenGLTexture::setCompressedData(): Texture target does not support pixel data upload");
        break;
    }

    // If requested perform automatic mip map generation
    if (mipLevel == 0 && autoGenerateMipMaps && mipLevels > 1) {
        Q_Q(QOpenGLTexture);
        q->generateMipMaps();
    }
}

void QOpenGLTexturePrivate::setWrapMode(QOpenGLTexture::WrapMode mode)
{
    switch (target) {
    case QOpenGLTexture::Target1D:
    case QOpenGLTexture::Target1DArray:
    case QOpenGLTexture::TargetBuffer:
        wrapModes[0] = mode;
        texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_S, mode);
        break;

    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::Target2DArray:
    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetCubeMapArray:
    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
    case QOpenGLTexture::TargetRectangle:
        wrapModes[0] = wrapModes[1] = mode;
        texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_S, mode);
        texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_T, mode);
        break;

    case QOpenGLTexture::Target3D:
        wrapModes[0] = wrapModes[1] = wrapModes[2] = mode;
        texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_S, mode);
        texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_T, mode);
        texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_R, mode);
        break;
    }
}

void QOpenGLTexturePrivate::setWrapMode(QOpenGLTexture::CoordinateDirection direction, QOpenGLTexture::WrapMode mode)
{
    switch (target) {
    case QOpenGLTexture::Target1D:
    case QOpenGLTexture::Target1DArray:
    case QOpenGLTexture::TargetBuffer:
        switch (direction) {
        case QOpenGLTexture::DirectionS:
            wrapModes[0] = mode;
            texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_S, mode);
            break;

        case QOpenGLTexture::DirectionT:
        case QOpenGLTexture::DirectionR:
            qWarning("QOpenGLTexture::setWrapMode() direction not valid for this texture target");
            break;
        }
        break;

    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::Target2DArray:
    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetCubeMapArray:
    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
    case QOpenGLTexture::TargetRectangle:
        switch (direction) {
        case QOpenGLTexture::DirectionS:
            wrapModes[0] = mode;
            texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_S, mode);
            break;

        case QOpenGLTexture::DirectionT:
            wrapModes[1] = mode;
            texFuncs->glTextureParameteri(textureId, target, bindingTarget, GL_TEXTURE_WRAP_T, mode);
            break;

        case QOpenGLTexture::DirectionR:
            qWarning("QOpenGLTexture::setWrapMode() direction not valid for this texture target");
            break;
        }
        break;

    case QOpenGLTexture::Target3D:
        switch (direction) {
        case QOpenGLTexture::DirectionS:
            wrapModes[0] = mode;
            texFuncs->glTextureParameteri(textureId, target, bindingTarget, direction, mode);
            break;

        case QOpenGLTexture::DirectionT:
            wrapModes[1] = mode;
            texFuncs->glTextureParameteri(textureId, target, bindingTarget, direction, mode);
            break;

        case QOpenGLTexture::DirectionR:
            wrapModes[2] = mode;
            texFuncs->glTextureParameteri(textureId, target, bindingTarget, direction, mode);
            break;
        }
        break;
    }
}

QOpenGLTexture::WrapMode QOpenGLTexturePrivate::wrapMode(QOpenGLTexture::CoordinateDirection direction) const
{
    switch (target) {
    case QOpenGLTexture::Target1D:
    case QOpenGLTexture::Target1DArray:
    case QOpenGLTexture::TargetBuffer:
        switch (direction) {
        case QOpenGLTexture::DirectionS:
            return wrapModes[0];

        case QOpenGLTexture::DirectionT:
        case QOpenGLTexture::DirectionR:
            qWarning("QOpenGLTexture::wrapMode() direction not valid for this texture target");
            return QOpenGLTexture::Repeat;
        }
        break;

    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::Target2DArray:
    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetCubeMapArray:
    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
    case QOpenGLTexture::TargetRectangle:
        switch (direction) {
        case QOpenGLTexture::DirectionS:
            return wrapModes[0];

        case QOpenGLTexture::DirectionT:
            return wrapModes[1];

        case QOpenGLTexture::DirectionR:
            qWarning("QOpenGLTexture::wrapMode() direction not valid for this texture target");
            return QOpenGLTexture::Repeat;
        }
        break;

    case QOpenGLTexture::Target3D:
        switch (direction) {
        case QOpenGLTexture::DirectionS:
            return wrapModes[0];

        case QOpenGLTexture::DirectionT:
            return wrapModes[1];

        case QOpenGLTexture::DirectionR:
            return wrapModes[2];
        }
        break;
    }
    // Should never get here
    Q_ASSERT(false);
    return QOpenGLTexture::Repeat;
}

QOpenGLTexture *QOpenGLTexturePrivate::createTextureView(QOpenGLTexture::Target viewTarget,
                                                         QOpenGLTexture::TextureFormat viewFormat,
                                                         int minimumMipmapLevel, int maximumMipmapLevel,
                                                         int minimumLayer, int maximumLayer) const
{
    // Do sanity checks - see http://www.opengl.org/wiki/GLAPI/glTextureView

    // Check the targets are compatible
    bool viewTargetCompatible = false;
    switch (target) {
    case QOpenGLTexture::Target1D:
    case QOpenGLTexture::Target1DArray:
        viewTargetCompatible = (viewTarget == QOpenGLTexture::Target1D
                             || viewTarget == QOpenGLTexture::Target1DArray);
        break;


    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::Target2DArray:
        viewTargetCompatible = (viewTarget == QOpenGLTexture::Target2D
                             || viewTarget == QOpenGLTexture::Target2DArray);
        break;

    case QOpenGLTexture::Target3D:
        viewTargetCompatible = (viewTarget == QOpenGLTexture::Target3D);
        break;

    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetCubeMapArray:
        viewTargetCompatible = (viewTarget == QOpenGLTexture::TargetCubeMap
                             || viewTarget == QOpenGLTexture::Target2D
                             || viewTarget == QOpenGLTexture::Target2DArray
                             || viewTarget == QOpenGLTexture::TargetCubeMapArray);
        break;

    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
        viewTargetCompatible = (viewTarget == QOpenGLTexture::Target2DMultisample
                             || viewTarget == QOpenGLTexture::Target2DMultisampleArray);
        break;

    case QOpenGLTexture::TargetRectangle:
        viewTargetCompatible = (viewTarget == QOpenGLTexture::TargetRectangle);
        break;

    case QOpenGLTexture::TargetBuffer:
        // Cannot be used with texture views
        break;
    }

    if (!viewTargetCompatible) {
        qWarning("QOpenGLTexture::createTextureView(): Incompatible source and view targets");
        return 0;
    }

    // Check the formats are compatible
    bool viewFormatCompatible = false;
    switch (formatClass) {
    case QOpenGLTexture::NoFormatClass:
        break;

    case QOpenGLTexture::FormatClass_128Bit:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RGBA32F
                             || viewFormat == QOpenGLTexture::RGBA32U
                             || viewFormat == QOpenGLTexture::RGBA32I);
        break;

    case QOpenGLTexture::FormatClass_96Bit:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB32F
                             || viewFormat == QOpenGLTexture::RGB32U
                             || viewFormat == QOpenGLTexture::RGB32I);
        break;

    case QOpenGLTexture::FormatClass_64Bit:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RGBA16F
                             || viewFormat == QOpenGLTexture::RG32F
                             || viewFormat == QOpenGLTexture::RGBA16U
                             || viewFormat == QOpenGLTexture::RG32U
                             || viewFormat == QOpenGLTexture::RGBA16I
                             || viewFormat == QOpenGLTexture::RG32I
                             || viewFormat == QOpenGLTexture::RGBA16_UNorm
                             || viewFormat == QOpenGLTexture::RGBA16_SNorm);
        break;

    case QOpenGLTexture::FormatClass_48Bit:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB16_UNorm
                             || viewFormat == QOpenGLTexture::RGB16_SNorm
                             || viewFormat == QOpenGLTexture::RGB16F
                             || viewFormat == QOpenGLTexture::RGB16U
                             || viewFormat == QOpenGLTexture::RGB16I);
        break;

    case QOpenGLTexture::FormatClass_32Bit:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RG16F
                             || viewFormat == QOpenGLTexture::RG11B10F
                             || viewFormat == QOpenGLTexture::R32F
                             || viewFormat == QOpenGLTexture::RGB10A2
                             || viewFormat == QOpenGLTexture::RGBA8U
                             || viewFormat == QOpenGLTexture::RG16U
                             || viewFormat == QOpenGLTexture::R32U
                             || viewFormat == QOpenGLTexture::RGBA8I
                             || viewFormat == QOpenGLTexture::RG16I
                             || viewFormat == QOpenGLTexture::R32I
                             || viewFormat == QOpenGLTexture::RGBA8_UNorm
                             || viewFormat == QOpenGLTexture::RG16_UNorm
                             || viewFormat == QOpenGLTexture::RGBA8_SNorm
                             || viewFormat == QOpenGLTexture::RG16_SNorm
                             || viewFormat == QOpenGLTexture::SRGB8_Alpha8
                             || viewFormat == QOpenGLTexture::RGB9E5);
        break;

    case QOpenGLTexture::FormatClass_24Bit:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB8_UNorm
                             || viewFormat == QOpenGLTexture::RGB8_SNorm
                             || viewFormat == QOpenGLTexture::SRGB8
                             || viewFormat == QOpenGLTexture::RGB8U
                             || viewFormat == QOpenGLTexture::RGB8I);
        break;

    case QOpenGLTexture::FormatClass_16Bit:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::R16F
                             || viewFormat == QOpenGLTexture::RG8U
                             || viewFormat == QOpenGLTexture::R16U
                             || viewFormat == QOpenGLTexture::RG8I
                             || viewFormat == QOpenGLTexture::R16I
                             || viewFormat == QOpenGLTexture::RG8_UNorm
                             || viewFormat == QOpenGLTexture::R16_UNorm
                             || viewFormat == QOpenGLTexture::RG8_SNorm
                             || viewFormat == QOpenGLTexture::R16_SNorm);
        break;

    case QOpenGLTexture::FormatClass_8Bit:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::R8U
                             || viewFormat == QOpenGLTexture::R8I
                             || viewFormat == QOpenGLTexture::R8_UNorm
                             || viewFormat == QOpenGLTexture::R8_SNorm);
        break;

    case QOpenGLTexture::FormatClass_RGTC1_R:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::R_ATI1N_UNorm
                             || viewFormat == QOpenGLTexture::R_ATI1N_SNorm);
        break;

    case QOpenGLTexture::FormatClass_RGTC2_RG:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RG_ATI2N_UNorm
                             || viewFormat == QOpenGLTexture::RG_ATI2N_SNorm);
        break;

    case QOpenGLTexture::FormatClass_BPTC_Unorm:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB_BP_UNorm
                             || viewFormat == QOpenGLTexture::SRGB_BP_UNorm);
        break;

    case QOpenGLTexture::FormatClass_BPTC_Float:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB_BP_UNSIGNED_FLOAT
                             || viewFormat == QOpenGLTexture::RGB_BP_SIGNED_FLOAT);
        break;

    case QOpenGLTexture::FormatClass_S3TC_DXT1_RGB:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RGB_DXT1
                             || viewFormat == QOpenGLTexture::SRGB_DXT1);
        break;

    case QOpenGLTexture::FormatClass_S3TC_DXT1_RGBA:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RGBA_DXT1
                             || viewFormat == QOpenGLTexture::SRGB_Alpha_DXT1);
        break;

    case QOpenGLTexture::FormatClass_S3TC_DXT3_RGBA:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RGBA_DXT3
                             || viewFormat == QOpenGLTexture::SRGB_Alpha_DXT3);
        break;

    case QOpenGLTexture::FormatClass_S3TC_DXT5_RGBA:
        viewFormatCompatible = (viewFormat == QOpenGLTexture::RGBA_DXT5
                             || viewFormat == QOpenGLTexture::SRGB_Alpha_DXT5);
        break;

    case QOpenGLTexture::FormatClass_Unique:
        viewFormatCompatible = (viewFormat == format);
        break;
    }

    if (!viewFormatCompatible) {
        qWarning("QOpenGLTexture::createTextureView(): Incompatible source and view formats");
        return 0;
    }


    // Create a view
    QOpenGLTexture *view = new QOpenGLTexture(viewTarget);
    view->setFormat(viewFormat);
    view->create();
    view->d_ptr->textureView = true;
    texFuncs->glTextureView(view->textureId(), viewTarget, textureId, viewFormat,
                            minimumMipmapLevel, maximumMipmapLevel - minimumMipmapLevel + 1,
                            minimumLayer, maximumLayer - minimumLayer + 1);
    return view;
}


/*!
    \class QOpenGLTexture
    \inmodule QtGui
    \since 5.2
    \wrapper
    \brief The QOpenGLTexture class encapsulates an OpenGL texture object.

    QOpenGLTexture makes it easy to work with OpenGL textures and the myriad features
    and targets that they offer depending upon the capabilities of your OpenGL implementation.

    The typical usage pattern for QOpenGLTexture is
    \list
        \li Instantiate the object specifying the texture target type
        \li Set properties that affect the storage requirements e.g. storage format, dimensions
        \li Allocate the server-side storage
        \li Optionally upload pixel data
        \li Optionally set any additional properties e.g. filtering and border options
        \li Render with texture or render to texture
    \endlist

    In the common case of simply using a QImage as the source of texture pixel data
    most of the above steps are performed automatically.

    \code
    // Prepare texture
    QOpenGLTexture *texture = new QOpenGLTexture(QImage(fileName).mirrored());
    texture->setMinificationFilter(QOpenGLTexture::LinearMipMapLinear);
    texture->setMagnificationFilter(QOpenGLTexture::Linear);
    ...
    // Render with texture
    texture->bind();
    glDrawArrays(...);
    \endcode

    Note that the QImage is mirrored vertically to account for the fact that
    OpenGL and QImage use opposite directions for the y axis. Another option
    would be to transform your texture coordinates.
*/

/*!
    \enum QOpenGLTexture::Filter
    This enum defines the filtering parameters for a QOpenGLTexture object.
    \value Nearest Equivalent to GL_NEAREST
    \value Linear Equivalent to GL_LINEAR
    \value NearestMipMapNearest Equivalent to GL_NEAREST_MIPMAP_NEAREST
    \value NearestMipMapLinear Equivalent to GL_NEAREST_MIPMAP_LINEAR
    \value LinearMipMapNearest Equivalent to GL_LINEAR_MIPMAP_NEAREST
    \value LinearMipMapLinear Equivalent to GL_LINEAR_MIPMAP_LINEAR
*/

/*!
    \enum QOpenGLTexture::Target
    This enum defines the texture target of a QOpenGLTexture object.

    \value Target1D A 1-dimensional texture.
           Equivalent to GL_TEXTURE_1D.
    \value Target1DArray An array of 1-dimensional textures.
           Equivalent to GL_TEXTURE_1D_ARRAY
    \value Target2D A 2-dimensional texture.
           Equivalent to GL_TEXTURE_2D
    \value Target2DArray An array of 1-dimensional textures.
           Equivalent to GL_TEXTURE_2D_ARRAY
    \value Target3D A 3-dimensional texture.
           Equivalent to GL_TEXTURE_3D
    \value TargetCubeMap A cubemap texture.
           Equivalent to GL_TEXTURE_CUBE_MAP
    \value TargetCubeMapArray An array of cubemap textures.
           Equivalent to GL_TEXTURE_CUBE_MAP_ARRAY
    \value Target2DMultisample A 2-dimensional texture with multisample support.
           Equivalent to GL_TEXTURE_2D_MULTISAMPLE
    \value Target2DMultisampleArray An array of 2-dimensional textures with multisample support.
           Equivalent to GL_TEXTURE_2D_MULTISAMPLE_ARRAY
    \value TargetRectangle A rectangular 2-dimensional texture.
           Equivalent to GL_TEXTURE_RECTANGLE
    \value TargetBuffer A texture with data from an OpenGL buffer object.
           Equivalent to GL_TEXTURE_BUFFER
*/

/*!
    \enum QOpenGLTexture::BindingTarget
    This enum defines the possible binding targets of texture units.

    \value BindingTarget1D Equivalent to GL_TEXTURE_BINDING_1D
    \value BindingTarget1DArray Equivalent to GL_TEXTURE_BINDING_1D_ARRAY
    \value BindingTarget2D Equivalent to GL_TEXTURE_BINDING_2D
    \value BindingTarget2DArray Equivalent to GL_TEXTURE_BINDING_2D_ARRAY
    \value BindingTarget3D Equivalent to GL_TEXTURE_BINDING_3D
    \value BindingTargetCubeMap Equivalent to GL_TEXTURE_BINDING_CUBE_MAP
    \value BindingTargetCubeMapArray Equivalent to GL_TEXTURE_BINDING_CUBE_MAP_ARRAY
    \value BindingTarget2DMultisample Equivalent to GL_TEXTURE_BINDING_2D_MULTISAMPLE
    \value BindingTarget2DMultisampleArray Equivalent to GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY
    \value BindingTargetRectangle Equivalent to GL_TEXTURE_BINDING_RECTANGLE
    \value BindingTargetBuffer Equivalent to GL_TEXTURE_BINDING_BUFFER
*/

/*!
    \enum QOpenGLTexture::MipMapGeneration
    This enum defines the options to control mipmap generation.

    \value GenerateMipMaps Mipmaps should be generated
    \value DontGenerateMipMaps Mipmaps should not be generated
*/

/*!
    \enum QOpenGLTexture::TextureUnitReset
    This enum defines options ot control texture unit activation.

    \value ResetTextureUnit The previous active texture unit will be reset
    \value DontResetTextureUnit The previous active texture unit will not be rest
*/

/*!
    \enum QOpenGLTexture::TextureFormat
    This enum defines the possible texture formats. Depending upon your OpenGL
    implementation only a subset of these may be supported.

    \value NoFormat Equivalent to GL_NONE

    \value R8_UNorm Equivalent to GL_R8
    \value RG8_UNorm Equivalent to GL_RG8
    \value RGB8_UNorm Equivalent to GL_RGB8
    \value RGBA8_UNorm Equivalent to GL_RGBA8

    \value R16_UNorm Equivalent to GL_R16
    \value RG16_UNorm Equivalent to GL_RG16
    \value RGB16_UNorm Equivalent to GL_RGB16
    \value RGBA16_UNorm Equivalent to GL_RGBA16

    \value R8_SNorm Equivalent to GL_R8_SNORM
    \value RG8_SNorm Equivalent to GL_RG8_SNORM
    \value RGB8_SNorm Equivalent to GL_RGB8_SNORM
    \value RGBA8_SNorm Equivalent to GL_RGBA8_SNORM

    \value R16_SNorm Equivalent to GL_R16_SNORM
    \value RG16_SNorm Equivalent to GL_RG16_SNORM
    \value RGB16_SNorm Equivalent to GL_RGB16_SNORM
    \value RGBA16_SNorm Equivalent to GL_RGBA16_SNORM

    \value R8U Equivalent to GL_R8UI
    \value RG8U Equivalent to GL_RG8UI
    \value RGB8U Equivalent to GL_RGB8UI
    \value RGBA8U Equivalent to GL_RGBA8UI

    \value R16U Equivalent to GL_R16UI
    \value RG16U Equivalent to GL_RG16UI
    \value RGB16U Equivalent to GL_RGB16UI
    \value RGBA16U Equivalent to GL_RGBA16UI

    \value R32U Equivalent to GL_R32UI
    \value RG32U Equivalent to GL_RG32UI
    \value RGB32U Equivalent to GL_RGB32UI
    \value RGBA32U Equivalent to GL_RGBA32UI

    \value R8I Equivalent to GL_R8I
    \value RG8I Equivalent to GL_RG8I
    \value RGB8I Equivalent to GL_RGB8I
    \value RGBA8I Equivalent to GL_RGBA8I

    \value R16I Equivalent to GL_R16I
    \value RG16I Equivalent to GL_RG16I
    \value RGB16I Equivalent to GL_RGB16I
    \value RGBA16I Equivalent to GL_RGBA16I

    \value R32I Equivalent to GL_R32I
    \value RG32I Equivalent to GL_RG32I
    \value RGB32I Equivalent to GL_RGB32I
    \value RGBA32I Equivalent to GL_RGBA32I

    \value R16F Equivalent to GL_R16F
    \value RG16F Equivalent to GL_RG16F
    \value RGB16F Equivalent to GL_RGB16F
    \value RGBA16F Equivalent to GL_RGBA16F

    \value R32F Equivalent to GL_R32F
    \value RG32F Equivalent to GL_RG32F
    \value RGB32F Equivalent to GL_RGB32F
    \value RGBA32F Equivalent to GL_RGBA32F

    \value RGB9E5 Equivalent to GL_RGB9_E5
    \value RG11B10F Equivalent to GL_R11F_G11F_B10F
    \value RG3B2 Equivalent to GL_R3_G3_B2
    \value R5G6B5 Equivalent to GL_RGB565
    \value RGB5A1 Equivalent to GL_RGB5_A1
    \value RGBA4 Equivalent to GL_RGBA4
    \value RGB10A2 Equivalent to GL_RGB10_A2UI

    \value D16 Equivalent to GL_DEPTH_COMPONENT16
    \value D24 Equivalent to GL_DEPTH_COMPONENT24
    \value D24S8 Equivalent to GL_DEPTH24_STENCIL8
    \value D32 Equivalent to GL_DEPTH_COMPONENT32
    \value D32F Equivalent to GL_DEPTH_COMPONENT32F
    \value D32FS8X24 Equivalent to GL_DEPTH32F_STENCIL8
    \value S8 Equivalent to GL_STENCIL_INDEX8. Introduced in Qt 5.4

    \value RGB_DXT1 Equivalent to GL_COMPRESSED_RGB_S3TC_DXT1_EXT
    \value RGBA_DXT1 Equivalent to GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
    \value RGBA_DXT3 Equivalent to GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
    \value RGBA_DXT5 Equivalent to GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
    \value R_ATI1N_UNorm Equivalent to GL_COMPRESSED_RED_RGTC1
    \value R_ATI1N_SNorm Equivalent to GL_COMPRESSED_SIGNED_RED_RGTC1
    \value RG_ATI2N_UNorm Equivalent to GL_COMPRESSED_RG_RGTC2
    \value RG_ATI2N_SNorm Equivalent to GL_COMPRESSED_SIGNED_RG_RGTC2
    \value RGB_BP_UNSIGNED_FLOAT Equivalent to GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB
    \value RGB_BP_SIGNED_FLOAT Equivalent to GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB
    \value RGB_BP_UNorm Equivalent to GL_COMPRESSED_RGBA_BPTC_UNORM_ARB
    \value R11_EAC_UNorm Equivalent to GL_COMPRESSED_R11_EAC
    \value R11_EAC_SNorm Equivalent to GL_COMPRESSED_SIGNED_R11_EAC
    \value RG11_EAC_UNorm Equivalent to GL_COMPRESSED_RG11_EAC
    \value RG11_EAC_SNorm Equivalent to GL_COMPRESSED_SIGNED_RG11_EAC
    \value RGB8_ETC2 Equivalent to GL_COMPRESSED_RGB8_ETC2
    \value SRGB8_ETC2 Equivalent to GL_COMPRESSED_SRGB8_ETC2
    \value RGB8_PunchThrough_Alpha1_ETC2 Equivalent to GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2
    \value SRGB8_PunchThrough_Alpha1_ETC2 Equivalent to GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2
    \value RGBA8_ETC2_EAC Equivalent to GL_COMPRESSED_RGBA8_ETC2_EAC
    \value SRGB8_Alpha8_ETC2_EAC Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
    \value RGB8_ETC1 Equivalent to GL_ETC1_RGB8_OES
    \value RGBA_ASTC_4x4 Equivalent to GL_COMPRESSED_RGBA_ASTC_4x4_KHR
    \value RGBA_ASTC_5x4 Equivalent to GL_COMPRESSED_RGBA_ASTC_5x4_KHR
    \value RGBA_ASTC_5x5 Equivalent to GL_COMPRESSED_RGBA_ASTC_5x5_KHR
    \value RGBA_ASTC_6x5 Equivalent to GL_COMPRESSED_RGBA_ASTC_6x5_KHR
    \value RGBA_ASTC_6x6 Equivalent to GL_COMPRESSED_RGBA_ASTC_6x6_KHR
    \value RGBA_ASTC_8x5 Equivalent to GL_COMPRESSED_RGBA_ASTC_8x5_KHR
    \value RGBA_ASTC_8x6 Equivalent to GL_COMPRESSED_RGBA_ASTC_8x6_KHR
    \value RGBA_ASTC_8x8 Equivalent to GL_COMPRESSED_RGBA_ASTC_8x8_KHR
    \value RGBA_ASTC_10x5 Equivalent to GL_COMPRESSED_RGBA_ASTC_10x5_KHR
    \value RGBA_ASTC_10x6 Equivalent to GL_COMPRESSED_RGBA_ASTC_10x6_KHR
    \value RGBA_ASTC_10x8 Equivalent to GL_COMPRESSED_RGBA_ASTC_10x8_KHR
    \value RGBA_ASTC_10x10 Equivalent to GL_COMPRESSED_RGBA_ASTC_10x10_KHR
    \value RGBA_ASTC_12x10 Equivalent to GL_COMPRESSED_RGBA_ASTC_12x10_KHR
    \value RGBA_ASTC_12x12 Equivalent to GL_COMPRESSED_RGBA_ASTC_12x12_KHR
    \value SRGB8_Alpha8_ASTC_4x4 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR
    \value SRGB8_Alpha8_ASTC_5x4 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR
    \value SRGB8_Alpha8_ASTC_5x5 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR
    \value SRGB8_Alpha8_ASTC_6x5 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR
    \value SRGB8_Alpha8_ASTC_6x6 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR
    \value SRGB8_Alpha8_ASTC_8x5 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR
    \value SRGB8_Alpha8_ASTC_8x6 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR
    \value SRGB8_Alpha8_ASTC_8x8 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR
    \value SRGB8_Alpha8_ASTC_10x5 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR
    \value SRGB8_Alpha8_ASTC_10x6 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR
    \value SRGB8_Alpha8_ASTC_10x8 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR
    \value SRGB8_Alpha8_ASTC_10x10 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR
    \value SRGB8_Alpha8_ASTC_12x10 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR
    \value SRGB8_Alpha8_ASTC_12x12 Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR

    \value SRGB8 Equivalent to GL_SRGB8
    \value SRGB8_Alpha8 Equivalent to GL_SRGB8_ALPHA8
    \value SRGB_DXT1 Equivalent to GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
    \value SRGB_Alpha_DXT1 Equivalent to GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
    \value SRGB_Alpha_DXT3 Equivalent to GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
    \value SRGB_Alpha_DXT5 Equivalent to GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
    \value SRGB_BP_UNorm Equivalent to GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB

    \value DepthFormat Equivalent to GL_DEPTH_COMPONENT (only OpenGL ES 3 or ES 2 with OES_depth_texture)
    \value AlphaFormat Equivalent to GL_ALPHA (OpenGL ES 2 only)
    \value RGBFormat Equivalent to GL_RGB (OpenGL ES 2 only)
    \value RGBAFormat Equivalent to GL_RGBA (OpenGL ES 2 only)
    \value LuminanceFormat Equivalent to GL_LUMINANCE (OpenGL ES 2 only)
    \value LuminanceAlphaFormat Equivalent to GL_LUMINANCE_ALPHA (OpenGL ES 2 only)
*/

/*!
    \enum QOpenGLTexture::CubeMapFace
    This enum defines the possible CubeMap faces.

    \value CubeMapPositiveX Equivalent to GL_TEXTURE_CUBE_MAP_POSITIVE_X
    \value CubeMapNegativeX Equivalent to GL_TEXTURE_CUBE_MAP_NEGATIVE_X
    \value CubeMapPositiveY Equivalent to GL_TEXTURE_CUBE_MAP_POSITIVE_Y
    \value CubeMapNegativeY Equivalent to GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
    \value CubeMapPositiveZ Equivalent to GL_TEXTURE_CUBE_MAP_POSITIVE_Z
    \value CubeMapNegativeZ Equivalent to GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
*/

/*!
    \enum QOpenGLTexture::PixelFormat
    This enum defines the possible client-side pixel formats for a pixel
    transfer operation.

    \value NoSourceFormat Equivalent to GL_NONE
    \value Red Equivalent to GL_RED
    \value RG Equivalent to GL_RG
    \value RGB Equivalent to GL_RGB
    \value BGR Equivalent to GL_BGR
    \value RGBA Equivalent to GL_RGBA
    \value BGRA Equivalent to GL_BGRA
    \value Red_Integer Equivalent to GL_RED_INTEGER
    \value RG_Integer Equivalent to GL_RG_INTEGER
    \value RGB_Integer Equivalent to GL_RGB_INTEGER
    \value BGR_Integer Equivalent to GL_BGR_INTEGER
    \value RGBA_Integer Equivalent to GL_RGBA_INTEGER
    \value BGRA_Integer Equivalent to GL_BGRA_INTEGER
    \value Stencil Equivalent to GL_STENCIL_INDEX. Introduced in Qt 5.4
    \value Depth Equivalent to GL_DEPTH_COMPONENT
    \value DepthStencil Equivalent to GL_DEPTH_STENCIL
    \value Alpha Equivalent to GL_ALPHA (OpenGL ES 2 only)
    \value Luminance Equivalent to GL_LUMINANCE (OpenGL ES 2 only)
    \value LuminanceAlpha Equivalent to GL_LUMINANCE_ALPHA (OpenGL ES 2 only)

*/

/*!
    \enum QOpenGLTexture::PixelType
    This enum defines the possible pixel data types for a pixel transfer operation

    \value NoPixelType Equivalent to GL_NONE
    \value Int8 Equivalent to GL_BYTE
    \value UInt8 Equivalent to GL_UNSIGNED_BYTE
    \value Int16 Equivalent to GL_SHORT
    \value UInt16 Equivalent to GL_UNSIGNED_SHORT
    \value Int32 Equivalent to GL_INT
    \value UInt32 Equivalent to GL_UNSIGNED_INT
    \value Float16 Equivalent to GL_HALF_FLOAT
    \value Float16OES Equivalent to GL_HALF_FLOAT_OES
    \value Float32 Equivalent to GL_FLOAT
    \value UInt32_RGB9_E5 Equivalent to GL_UNSIGNED_INT_5_9_9_9_REV
    \value UInt32_RG11B10F Equivalent to GL_UNSIGNED_INT_10F_11F_11F_REV
    \value UInt8_RG3B2 Equivalent to GL_UNSIGNED_BYTE_3_3_2
    \value UInt8_RG3B2_Rev Equivalent to GL_UNSIGNED_BYTE_2_3_3_REV
    \value UInt16_RGB5A1 Equivalent to GL_UNSIGNED_SHORT_5_5_5_1
    \value UInt16_RGB5A1_Rev Equivalent to GL_UNSIGNED_SHORT_1_5_5_5_REV
    \value UInt16_R5G6B5 Equivalent to GL_UNSIGNED_SHORT_5_6_5
    \value UInt16_R5G6B5_Rev Equivalent to GL_UNSIGNED_SHORT_5_6_5_REV
    \value UInt16_RGBA4 Equivalent to GL_UNSIGNED_SHORT_4_4_4_4
    \value UInt16_RGBA4_Rev Equivalent to GL_UNSIGNED_SHORT_4_4_4_4_REV
    \value UInt32_RGBA8 Equivalent to GL_UNSIGNED_INT_8_8_8_8
    \value UInt32_RGBA8_Rev Equivalent to GL_UNSIGNED_INT_8_8_8_8_REV
    \value UInt32_RGB10A2 Equivalent to GL_UNSIGNED_INT_10_10_10_2
    \value UInt32_RGB10A2_Rev Equivalent to GL_UNSIGNED_INT_2_10_10_10_REV
    \value UInt32_D24S8 Equivalent to GL_UNSIGNED_INT_24_8. Introduced in Qt 5.4
    \value Float32_D32_UInt32_S8_X24 Equivalent to GL_FLOAT_32_UNSIGNED_INT_24_8_REV. Introduced in Qt 5.4
*/

/*!
    \enum QOpenGLTexture::Feature
    This enum defines the OpenGL texture-related features that can be tested for.

    \value ImmutableStorage Support for immutable texture storage
    \value ImmutableMultisampleStorage Support for immutable texture storage with
           multisample targets
    \value TextureRectangle Support for the GL_TEXTURE_RECTANGLE target
    \value TextureArrays Support for texture targets with array layers
    \value Texture3D Support for the 3 dimensional texture target
    \value TextureMultisample Support for texture targets that have multisample capabilities
    \value TextureBuffer Support for textures that use OpenGL buffer objects
           as their data source
    \value TextureCubeMapArrays Support for cubemap array texture target
    \value Swizzle Support for texture component swizzle masks
    \value StencilTexturing Support for stencil texturing (i.e. looking up depth or stencil
           components of a combined depth/stencil format texture in GLSL shaders).
    \value AnisotropicFiltering Support for anisotropic texture filtering
    \value NPOTTextures Basic support for non-power-of-two textures
    \value NPOTTextureRepeat Full support for non-power-of-two textures including texture
           repeat modes
    \value Texture1D Support for the 1 dimensional texture target
    \value TextureComparisonOperators Support for texture comparison operators
    \value TextureMipMapLevel Support for setting the base and maximum mipmap levels
*/

/*!
    \enum QOpenGLTexture::SwizzleComponent
    This enum defines the texture color components that can be assigned a swizzle mask.

    \value SwizzleRed The red component. Equivalent to GL_TEXTURE_SWIZZLE_R
    \value SwizzleGreen The green component. Equivalent to GL_TEXTURE_SWIZZLE_G
    \value SwizzleBlue The blue component. Equivalent to GL_TEXTURE_SWIZZLE_B
    \value SwizzleAlpha The alpha component. Equivalent to GL_TEXTURE_SWIZZLE_A
*/

/*!
    \enum QOpenGLTexture::SwizzleValue
    This enum defines the possible mask values for texture swizzling.

    \value RedValue Maps the component to the red channel. Equivalent to GL_RED
    \value GreenValue Maps the component to the green channel. Equivalent to GL_GREEN
    \value BlueValue Maps the component to the blue channel. Equivalent to GL_BLUE
    \value AlphaValue Maps the component to the alpha channel. Equivalent to GL_ALPHA
    \value ZeroValue Maps the component to a fixed value of 0. Equivalent to GL_ZERO
    \value OneValue Maps the component to a fixed value of 1. Equivalent to GL_ONE
*/

/*!
    \enum QOpenGLTexture::WrapMode
    This enum defines the possible texture coordinate wrapping modes.

    \value Repeat Texture coordinate is repeated. Equivalent to GL_REPEAT
    \value MirroredRepeat Texture coordinate is reflected about 0 and 1. Equivalent to GL_MIRRORED_REPEAT
    \value ClampToEdge Clamps the texture coordinates to [0,1]. Equivalent to GL_CLAMP_TO_EDGE
    \value ClampToBorder As for ClampToEdge but also blends samples at 0 and 1 with a
           fixed border color. Equivalent to GL_CLAMP_TO_BORDER
*/

/*!
    \enum QOpenGLTexture::CoordinateDirection
    This enum defines the possible texture coordinate directions

    \value DirectionS The horizontal direction. Equivalent to GL_TEXTURE_WRAP_S
    \value DirectionT The vertical direction. Equivalent to GL_TEXTURE_WRAP_T
    \value DirectionR The depth direction. Equivalent to GL_TEXTURE_WRAP_R
*/

/*!
    Creates a QOpenGLTexture object that can later be bound to \a target.

    This does not create the underlying OpenGL texture object. Therefore,
    construction using this constructor does not require a valid current
    OpenGL context.
*/
QOpenGLTexture::QOpenGLTexture(Target target)
    : d_ptr(new QOpenGLTexturePrivate(target, this))
{
}

/*!
    Creates a QOpenGLTexture object that can later be bound to the 2D texture
    target and contains the pixel data contained in \a image. If you wish
    to have a chain of mipmaps generated then set \a genMipMaps to \c true (this
    is the default).

    This does create the underlying OpenGL texture object. Therefore,
    construction using this constructor does require a valid current
    OpenGL context.
*/
QOpenGLTexture::QOpenGLTexture(const QImage& image, MipMapGeneration genMipMaps)
    : QOpenGLTexture(QOpenGLTexture::Target2D)
{
    setData(image, genMipMaps);
}

QOpenGLTexture::~QOpenGLTexture()
{
}

/*!
    Returns the binding target of this texture.

    \since 5.4
*/
QOpenGLTexture::Target QOpenGLTexture::target() const
{
    Q_D(const QOpenGLTexture);
    return d->target;
}

/*!
    Creates the underlying OpenGL texture object. This requires a current valid
    OpenGL context. If the texture object already exists, this function does
    nothing.

    Once the texture object is created you can obtain the object
    name from the textureId() function. This may be useful if you wish to make
    some raw OpenGL calls related to this texture.

    Normally it should not be necessary to call this function directly as all
    functions that set properties of the texture object implicitly call create()
    on your behalf.

    Returns \c true if the creation succeeded, otherwise returns \c false.

    \sa destroy(), isCreated(), textureId()
*/
bool QOpenGLTexture::create()
{
    Q_D(QOpenGLTexture);
    return d->create();
}

/*!
    Destroys the underlying OpenGL texture object. This requires a current valid
    OpenGL context.

    \sa create(), isCreated(), textureId()
*/
void QOpenGLTexture::destroy()
{
    Q_D(QOpenGLTexture);
    return d->destroy();
}

/*!
    Returns \c true if the underlying OpenGL texture object has been created.

    \sa create(), destroy(), textureId()
*/
bool QOpenGLTexture::isCreated() const
{
    Q_D(const QOpenGLTexture);
    return d->textureId != 0;
}

/*!
    Returns the name of the underlying OpenGL texture object or 0 if it has
    not yet been created.

    \sa create(), destroy(), isCreated()
*/
GLuint QOpenGLTexture::textureId() const
{
    Q_D(const QOpenGLTexture);
    return d->textureId;
}

/*!
    Binds this texture to the currently active texture unit ready for
    rendering. Note that you do not need to bind QOpenGLTexture objects
    in order to modify them as the implementation makes use of the
    EXT_direct_state_access extension where available and simulates it
    where it is not.

    \sa release()
*/
void QOpenGLTexture::bind()
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->bind();
}

/*!
    Binds this texture to texture unit \a unit ready for
    rendering. Note that you do not need to bind QOpenGLTexture objects
    in order to modify them as the implementation makes use of the
    EXT_direct_state_access extension where available and simulates it
    where it is not.

    If parameter \a reset is \c true then this function will restore
    the active unit to the texture unit that was active upon entry.

    \sa release()
*/
void QOpenGLTexture::bind(uint unit, TextureUnitReset reset)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->bind(unit, reset);
}

/*!
    Unbinds this texture from the currently active texture unit.

    \sa bind()
*/
void QOpenGLTexture::release()
{
    Q_D(QOpenGLTexture);
    d->release();
}

/*!
    Unbinds this texture from texture unit \a unit.

    If parameter \a reset is \c true then this function
    will restore the active unit to the texture unit that was active
    upon entry.
*/
void QOpenGLTexture::release(uint unit, TextureUnitReset reset)
{
    Q_D(QOpenGLTexture);
    d->release(unit, reset);
}

/*!
    Returns \c true if this texture is bound to the corresponding target
    of the currently active texture unit.

    \sa bind(), release()
*/
bool QOpenGLTexture::isBound() const
{
    Q_D(const QOpenGLTexture);
    Q_ASSERT(d->textureId);
    return d->isBound();
}

/*!
    Returns \c true if this texture is bound to the corresponding target
    of texture unit \a unit.

    \sa bind(), release()
*/
bool QOpenGLTexture::isBound(uint unit)
{
    Q_D(const QOpenGLTexture);
    Q_ASSERT(d->textureId);
    return d->isBound(unit);
}

/*!
    Returns the textureId of the texture that is bound to the \a target
    of the currently active texture unit.
*/
GLuint QOpenGLTexture::boundTextureId(BindingTarget target)
{
    QOpenGLContext *ctx = QOpenGLContext::currentContext();
    if (!ctx) {
        qWarning("QOpenGLTexture::boundTextureId() requires a valid current context");
        return 0;
    }

    GLint textureId = 0;
    ctx->functions()->glGetIntegerv(target, &textureId);
    return static_cast<GLuint>(textureId);
}

/*!
    Returns the textureId of the texture that is bound to the \a target
    of the texture unit \a unit.
*/
GLuint QOpenGLTexture::boundTextureId(uint unit, BindingTarget target)
{
    QOpenGLContext *ctx = QOpenGLContext::currentContext();
    if (!ctx) {
        qWarning("QOpenGLTexture::boundTextureId() requires a valid current context");
        return 0;
    }

    QOpenGLFunctions *funcs = ctx->functions();
    funcs->initializeOpenGLFunctions();

    GLint oldTextureUnit = 0;
    funcs->glGetIntegerv(GL_ACTIVE_TEXTURE, &oldTextureUnit);

    funcs->glActiveTexture(unit);
    GLint textureId = 0;
    funcs->glGetIntegerv(target, &textureId);
    funcs->glActiveTexture(oldTextureUnit);

    return static_cast<GLuint>(textureId);
}

/*!
    Sets the format of this texture object to \a format. This function
    must be called before texture storage is allocated.

    Note that all formats may not be supported. The exact set of supported
    formats is dependent upon your OpenGL implementation and version.

    \sa format(), allocateStorage()
*/
void QOpenGLTexture::setFormat(TextureFormat format)
{
    Q_D(QOpenGLTexture);
    d->create();
    if (isStorageAllocated()) {
        qWarning("QOpenGLTexture::setFormat(): Cannot change format once storage has been allocated");
        return;
    }

    d->format = format;

    switch (format) {
    case NoFormat:
        d->formatClass = NoFormatClass;
        break;

    case RGBA32F:
    case RGBA32U:
    case RGBA32I:
        d->formatClass = FormatClass_128Bit;
        break;

    case RGB32F:
    case RGB32U:
    case RGB32I:
        d->formatClass = FormatClass_96Bit;
        break;

    case RGBA16F:
    case RG32F:
    case RGBA16U:
    case RG32U:
    case RGBA16I:
    case RG32I:
    case RGBA16_UNorm:
    case RGBA16_SNorm:
        d->formatClass = FormatClass_64Bit;
        break;

    case RGB16_UNorm:
    case RGB16_SNorm:
    case RGB16F:
    case RGB16U:
    case RGB16I:
        d->formatClass = FormatClass_48Bit;
        break;

    case RG16F:
    case RG11B10F:
    case R32F:
    case RGB10A2:
    case RGBA8U:
    case RG16U:
    case R32U:
    case RGBA8I:
    case RG16I:
    case R32I:
    case RGBA8_UNorm:
    case RG16_UNorm:
    case RGBA8_SNorm:
    case RG16_SNorm:
    case SRGB8_Alpha8:
    case RGB9E5:
        d->formatClass = FormatClass_32Bit;
        break;

    case RGB8_UNorm:
    case RGB8_SNorm:
    case SRGB8:
    case RGB8U:
    case RGB8I:
        d->formatClass = FormatClass_24Bit;
        break;

    case R16F:
    case RG8U:
    case R16U:
    case RG8I:
    case R16I:
    case RG8_UNorm:
    case R16_UNorm:
    case RG8_SNorm:
    case R16_SNorm:
        d->formatClass = FormatClass_16Bit;
        break;

    case R8U:
    case R8I:
    case R8_UNorm:
    case R8_SNorm:
        d->formatClass = FormatClass_8Bit;
        break;

    case R_ATI1N_UNorm:
    case R_ATI1N_SNorm:
        d->formatClass = FormatClass_RGTC1_R;
        break;

    case RG_ATI2N_UNorm:
    case RG_ATI2N_SNorm:
        d->formatClass = FormatClass_RGTC2_RG;
        break;

    case RGB_BP_UNorm:
    case SRGB_BP_UNorm:
        d->formatClass = FormatClass_BPTC_Unorm;
        break;

    case RGB_BP_UNSIGNED_FLOAT:
    case RGB_BP_SIGNED_FLOAT:
        d->formatClass = FormatClass_BPTC_Float;
        break;

    case RGB_DXT1:
    case SRGB_DXT1:
        d->formatClass = FormatClass_S3TC_DXT1_RGB;
        break;

    case RGBA_DXT1:
    case SRGB_Alpha_DXT1:
        d->formatClass = FormatClass_S3TC_DXT1_RGBA;
        break;

    case RGBA_DXT3:
    case SRGB_Alpha_DXT3:
        d->formatClass = FormatClass_S3TC_DXT3_RGBA;
        break;

    case RGBA_DXT5:
    case SRGB_Alpha_DXT5:
        d->formatClass = FormatClass_S3TC_DXT5_RGBA;
        break;

    case QOpenGLTexture::R11_EAC_UNorm:
    case QOpenGLTexture::R11_EAC_SNorm:
    case QOpenGLTexture::RG11_EAC_UNorm:
    case QOpenGLTexture::RG11_EAC_SNorm:
    case QOpenGLTexture::RGB8_ETC2:
    case QOpenGLTexture::SRGB8_ETC2:
    case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2:
    case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2:
    case QOpenGLTexture::RGBA8_ETC2_EAC:
    case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC:
    case QOpenGLTexture::RGB8_ETC1:
    case RG3B2:
    case R5G6B5:
    case RGB5A1:
    case RGBA4:
    case D16:
    case D24:
    case D24S8:
    case D32:
    case D32F:
    case D32FS8X24:
    case S8:
    case DepthFormat:
    case AlphaFormat:
    case RGBFormat:
    case RGBAFormat:
    case LuminanceFormat:
    case LuminanceAlphaFormat:
    case QOpenGLTexture::RGBA_ASTC_4x4:
    case QOpenGLTexture::RGBA_ASTC_5x4:
    case QOpenGLTexture::RGBA_ASTC_5x5:
    case QOpenGLTexture::RGBA_ASTC_6x5:
    case QOpenGLTexture::RGBA_ASTC_6x6:
    case QOpenGLTexture::RGBA_ASTC_8x5:
    case QOpenGLTexture::RGBA_ASTC_8x6:
    case QOpenGLTexture::RGBA_ASTC_8x8:
    case QOpenGLTexture::RGBA_ASTC_10x5:
    case QOpenGLTexture::RGBA_ASTC_10x6:
    case QOpenGLTexture::RGBA_ASTC_10x8:
    case QOpenGLTexture::RGBA_ASTC_10x10:
    case QOpenGLTexture::RGBA_ASTC_12x10:
    case QOpenGLTexture::RGBA_ASTC_12x12:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_4x4:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x4:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_5x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_6x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_8x8:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x5:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x6:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x8:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_10x10:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x10:
    case QOpenGLTexture::SRGB8_Alpha8_ASTC_12x12:
        d->formatClass = FormatClass_Unique;
        break;
    }
}

/*!
    Returns the format of this texture object.

    \sa setFormat()
*/
QOpenGLTexture::TextureFormat QOpenGLTexture::format() const
{
    Q_D(const QOpenGLTexture);
    return d->format;
}

static bool isNpot(int width, int height = 1, int depth = 1)
{
    return width & (width-1) || height & (height-1) || depth & (depth-1);
}

/*!
    Sets the dimensions of this texture object to \a width,
    \a height, and \a depth. The default for each dimension is 1.
    The maximum allowable texture size is dependent upon your OpenGL
    implementation. Allocating storage for a texture less than the
    maximum size can still fail if your system is low on resources.

    If a non-power-of-two \a width, \a height or \a depth is provided and your
    OpenGL implementation doesn't have support for repeating non-power-of-two
    textures, then the wrap mode is automatically set to ClampToEdge.

    \sa width(), height(), depth()
*/
void QOpenGLTexture::setSize(int width, int height, int depth)
{
    Q_D(QOpenGLTexture);
    d->create();
    if (isStorageAllocated()) {
        qWarning("Cannot resize a texture that already has storage allocated.\n"
                 "To do so, destroy() the texture and then create() and setSize()");
        return;
    }

    if (isNpot(width, height, depth) && !hasFeature(Feature::NPOTTextureRepeat) && d->target != Target::TargetRectangle)
        d->setWrapMode(WrapMode::ClampToEdge);

    switch (d->target) {
    case QOpenGLTexture::Target1D:
    case QOpenGLTexture::Target1DArray:
    case QOpenGLTexture::TargetBuffer:
        d->dimensions[0] = width;
        Q_UNUSED(height);
        Q_UNUSED(depth);
        break;

    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::Target2DArray:
    case QOpenGLTexture::TargetRectangle:
    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
        d->dimensions[0] = width;
        d->dimensions[1] = height;
        Q_UNUSED(depth);
        break;

    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetCubeMapArray:
        if (width != height)
            qWarning("QAbstractOpenGLTexture::setSize(): Cube map textures must be square");
        d->dimensions[0] = d->dimensions[1] = width;
        Q_UNUSED(depth);
        break;

    case QOpenGLTexture::Target3D:
        d->dimensions[0] = width;
        d->dimensions[1] = height;
        d->dimensions[2] = depth;
        break;
    }
}

/*!
    Returns the width of a 1D, 2D or 3D texture.

    \sa height(), depth(), setSize()
*/
int QOpenGLTexture::width() const
{
    Q_D(const QOpenGLTexture);
    return d->dimensions[0];
}

/*!
    Returns the height of a 2D or 3D texture.

    \sa width(), depth(), setSize()
*/
int QOpenGLTexture::height() const
{
    Q_D(const QOpenGLTexture);
    return d->dimensions[1];
}

/*!
    Returns the depth of a 3D texture.

    \sa width(), height(), setSize()
*/
int QOpenGLTexture::depth() const
{
    Q_D(const QOpenGLTexture);
    return d->dimensions[2];
}

/*!
    For texture targets that support mipmaps, this function
    sets the requested number of mipmap \a levels to allocate storage
    for. This function should be called before storage is allocated
    for the texture.

    If the texture target does not support mipmaps this function
    has no effect.

    \sa mipLevels(), maximumMipLevels(), isStorageAllocated()
*/
void QOpenGLTexture::setMipLevels(int levels)
{
    Q_D(QOpenGLTexture);
    d->create();
    if (isStorageAllocated()) {
        qWarning("Cannot set mip levels on a texture that already has storage allocated.\n"
                 "To do so, destroy() the texture and then create() and setMipLevels()");
        return;
    }

    switch (d->target) {
    case QOpenGLTexture::Target1D:
    case QOpenGLTexture::Target1DArray:
    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::Target2DArray:
    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetCubeMapArray:
    case QOpenGLTexture::Target3D:
        d->requestedMipLevels = levels;
        break;

    case QOpenGLTexture::TargetBuffer:
    case QOpenGLTexture::TargetRectangle:
    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
        qWarning("QAbstractOpenGLTexture::setMipLevels(): This texture target does not support mipmaps");
        break;
    }
}

/*!
    Returns the number of mipmap levels for this texture. If storage
    has not yet been allocated for this texture it returns the
    requested number of mipmap levels.

    \sa setMipLevels(), maximumMipLevels(), isStorageAllocated()
*/
int QOpenGLTexture::mipLevels() const
{
    Q_D(const QOpenGLTexture);
    return isStorageAllocated() ? d->mipLevels : d->requestedMipLevels;
}

/*!
    Returns the maximum number of mipmap levels that this texture
    can have given the current dimensions.

    \sa setMipLevels(), mipLevels(), setSize()
*/
int QOpenGLTexture::maximumMipLevels() const
{
    Q_D(const QOpenGLTexture);
    return d->maximumMipLevelCount();
}

/*!
    Sets the number of array \a layers to allocate storage for. This
    function should be called before storage is allocated for the texture.

    For targets that do not support array layers this function has
    no effect.

    \sa layers(), isStorageAllocated()
*/
void QOpenGLTexture::setLayers(int layers)
{
    Q_D(QOpenGLTexture);
    d->create();
    if (isStorageAllocated()) {
        qWarning("Cannot set layers on a texture that already has storage allocated.\n"
                 "To do so, destroy() the texture and then create() and setLayers()");
        return;
    }

    switch (d->target) {
    case QOpenGLTexture::Target1DArray:
    case QOpenGLTexture::Target2DArray:
    case QOpenGLTexture::TargetCubeMapArray:
    case QOpenGLTexture::Target2DMultisampleArray:
        d->layers = layers;
        break;

    case QOpenGLTexture::Target1D:
    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::Target3D:
    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetBuffer:
    case QOpenGLTexture::TargetRectangle:
    case QOpenGLTexture::Target2DMultisample:
        qWarning("Texture target does not support array layers");
        break;
    }
}

/*!
    Returns the number of array layers for this texture. If
    storage has not yet been allocated for this texture then
    this function returns the requested number of array layers.

    For texture targets that do not support array layers this
    will return 1.

    \sa setLayers(), isStorageAllocated()
*/
int QOpenGLTexture::layers() const
{
    Q_D(const QOpenGLTexture);
    return d->layers;
}

/*!
    Returns the number of faces for this texture. For cubemap
    and cubemap array type targets this will be 6.

    For non-cubemap type targets this will return 1.
*/
int QOpenGLTexture::faces() const
{
    Q_D(const QOpenGLTexture);
    return d->faces;
}

/*!
    Sets the number of \a samples to allocate storage for when rendering to
    a multisample capable texture target. This function should
    be called before storage is allocated for the texture.

    For targets that do not support multisampling this function has
    no effect.

    \sa samples(), isStorageAllocated()
*/
void QOpenGLTexture::setSamples(int samples)
{
    Q_D(QOpenGLTexture);
    d->create();
    if (isStorageAllocated()) {
        qWarning("Cannot set sample count on a texture that already has storage allocated.\n"
                 "To do so, destroy() the texture and then create() and setSamples()");
        return;
    }

    switch (d->target) {
    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
        d->samples = samples;
        break;

    case QOpenGLTexture::Target1D:
    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::Target3D:
    case QOpenGLTexture::Target1DArray:
    case QOpenGLTexture::Target2DArray:
    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetCubeMapArray:
    case QOpenGLTexture::TargetBuffer:
    case QOpenGLTexture::TargetRectangle:

        qWarning("Texture target does not support multisampling");
        break;
    }
}

/*!
    Returns the number of multisample sample points for this texture.
    If storage has not yet been allocated for this texture then
    this function returns the requested number of samples.

    For texture targets that do not support multisampling this
    will return 0.

    \sa setSamples(), isStorageAllocated()
*/
int QOpenGLTexture::samples() const
{
    Q_D(const QOpenGLTexture);
    return d->samples;
}

/*!
    Sets whether the sample positions and number of samples used with
    a multisample capable texture target to \a fixed. If set to \c true
    the sample positions and number of samples used are the same for
    all texels in the image and will not depend upon the image size or
    internal format. This function should be called before storage is allocated
    for the texture.

    For targets that do not support multisampling this function has
    no effect.

    The default value is \c true.

    \sa isFixedSamplePositions(), isStorageAllocated()
*/
void QOpenGLTexture::setFixedSamplePositions(bool fixed)
{
    Q_D(QOpenGLTexture);
    d->create();
    if (isStorageAllocated()) {
        qWarning("Cannot set sample positions on a texture that already has storage allocated.\n"
                 "To do so, destroy() the texture and then create() and setFixedSamplePositions()");
        return;
    }

    switch (d->target) {
    case QOpenGLTexture::Target2DMultisample:
    case QOpenGLTexture::Target2DMultisampleArray:
        d->fixedSamplePositions = fixed;
        break;

    case QOpenGLTexture::Target1D:
    case QOpenGLTexture::Target2D:
    case QOpenGLTexture::Target3D:
    case QOpenGLTexture::Target1DArray:
    case QOpenGLTexture::Target2DArray:
    case QOpenGLTexture::TargetCubeMap:
    case QOpenGLTexture::TargetCubeMapArray:
    case QOpenGLTexture::TargetBuffer:
    case QOpenGLTexture::TargetRectangle:

        qWarning("Texture target does not support multisampling");
        break;
    }
}

/*!
    Returns whether this texture uses a fixed pattern of multisample
    samples. If storage has not yet been allocated for this texture then
    this function returns the requested fixed sample position setting.

    For texture targets that do not support multisampling this
    will return \c true.

    \sa setFixedSamplePositions(), isStorageAllocated()
*/
bool QOpenGLTexture::isFixedSamplePositions() const
{
    Q_D(const QOpenGLTexture);
    return d->fixedSamplePositions;
}

/*!
    Allocates server-side storage for this texture object taking
    into account, the format, dimensions, mipmap levels, array
    layers and cubemap faces.

    Once storage has been allocated it is no longer possible to change
    these properties.

    If supported QOpenGLTexture makes use of immutable texture
    storage.

    Once storage has been allocated for the texture then pixel data
    can be uploaded via one of the setData() overloads.

    \note If immutable texture storage is not available,
    then a default pixel format and pixel type will be used to
    create the mutable storage. You can use the other
    allocateStorage() overload to specify exactly the pixel format
    and the pixel type to use when allocating mutable storage;
    this is particulary useful under certain OpenGL ES implementations
    (notably, OpenGL ES 2), where the pixel format and the pixel type
    used at allocation time must perfectly match the format
    and the type passed to any subsequent setData() call.

    \sa isStorageAllocated(), setData()
*/
void QOpenGLTexture::allocateStorage()
{
    Q_D(QOpenGLTexture);
    if (d->create()) {
        const QOpenGLTexture::PixelFormat pixelFormat = pixelFormatCompatibleWithInternalFormat(d->format);
        const QOpenGLTexture::PixelType pixelType = pixelTypeCompatibleWithInternalFormat(d->format);
        d->allocateStorage(pixelFormat, pixelType);
    }
}

/*!
    \since 5.5

    Allocates server-side storage for this texture object taking
    into account, the format, dimensions, mipmap levels, array
    layers and cubemap faces.

    Once storage has been allocated it is no longer possible to change
    these properties.

    If supported QOpenGLTexture makes use of immutable texture
    storage. However, if immutable texture storage is not available,
    then the specified \a pixelFormat and \a pixelType will be used
    to allocate mutable storage; note that in certain OpenGL implementations
    (notably, OpenGL ES 2) they must perfectly match the format
    and the type passed to any subsequent setData() call.

    Once storage has been allocated for the texture then pixel data
    can be uploaded via one of the setData() overloads.

    \sa isStorageAllocated(), setData()
*/
void QOpenGLTexture::allocateStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType)
{
    Q_D(QOpenGLTexture);
    if (d->create())
        d->allocateStorage(pixelFormat, pixelType);
}

/*!
    Returns \c true if server-side storage for this texture as been
    allocated.

    The texture format, dimensions, mipmap levels and array layers
    cannot be altered once storage ihas been allocated.

    \sa allocateStorage(), setSize(), setMipLevels(), setLayers(), setFormat()
*/
bool QOpenGLTexture::isStorageAllocated() const
{
    Q_D(const QOpenGLTexture);
    return d->storageAllocated;
}

/*!
    Attempts to create a texture view onto this texture. A texture
    view is somewhat analogous to a view in SQL in that it presents
    a restricted or reinterpreted view of the original data. Texture
    views do not allocate any more server-side storage, insted relying
    on the storage buffer of the source texture.

    Texture views are only available when using immutable storage. For
    more information on texture views see
    http://www.opengl.org/wiki/Texture_Storage#Texture_views.

    The \a target argument specifies the target to use for the view.
    Only some targets can be used depending upon the target of the original
    target. For e.g. a view onto a Target1DArray texture can specify
    either Target1DArray or Target1D but for the latter the number of
    array layers specified with \a minimumLayer and \a maximumLayer must
    be exactly 1.

    Simpliar constraints apply for the \a viewFormat. See the above link
    and the specification for more details.

    The \a minimumMipmapLevel, \a maximumMipmapLevel, \a minimumLayer,
    and \a maximumLayer arguments serve to restrict the parts of the
    texture accessible by the texture view.

    If creation of the texture view fails this function will return
    0. If the function succeeds it will return a pointer to a new
    QOpenGLTexture object that will return \c true from its isTextureView()
    function.

    \sa isTextureView()
*/
QOpenGLTexture *QOpenGLTexture::createTextureView(Target target,
                                                  TextureFormat viewFormat,
                                                  int minimumMipmapLevel, int maximumMipmapLevel,
                                                  int minimumLayer, int maximumLayer) const
{
    Q_D(const QOpenGLTexture);
    if (!isStorageAllocated()) {
        qWarning("Cannot set create a texture view of a texture that does not have storage allocated.");
        return 0;
    }
    Q_ASSERT(maximumMipmapLevel >= minimumMipmapLevel);
    Q_ASSERT(maximumLayer >= minimumLayer);
    return d->createTextureView(target, viewFormat,
                                minimumMipmapLevel, maximumMipmapLevel,
                                minimumLayer, maximumLayer);
}

/*!
    Returns \c true if this texture object is actually a view onto another
    texture object.

    \sa createTextureView()
*/
bool QOpenGLTexture::isTextureView() const
{
    Q_D(const QOpenGLTexture);
    Q_ASSERT(d->textureId);
    return d->textureView;
}

/*!
    Uploads pixel \a data for this texture object \a mipLevel, array \a layer, and \a cubeFace.
    Storage must have been allocated before uploading pixel data. Some overloads of setData()
    will set appropriate dimensions, mipmap levels, and array layers and then allocate storage
    for you if they have enough information to do so. This will be noted in the function
    documentation.

    The structure of the pixel data pointed to by \a data is specified by \a sourceFormat
    and \a sourceType. The pixel data upload can optionally be controlled by \a options.

    If using a compressed format() then you should use setCompressedData() instead of this
    function.

    \since 5.3
    \sa setCompressedData()
*/
void QOpenGLTexture::setData(int mipLevel, int layer, CubeMapFace cubeFace,
                             PixelFormat sourceFormat, PixelType sourceType,
                             const void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    if (!isStorageAllocated()) {
        qWarning("Cannot set data on a texture that does not have storage allocated.\n"
                 "To do so call allocateStorage() before this function");
        return;
    }
    d->setData(mipLevel, layer, 1, cubeFace, sourceFormat, sourceType, data, options);
}

/*!
    \since 5.9
    \overload

    Parameter \a layerCount is the number of layers in a texture array
    that are being uploaded/populated by this call.
*/
void QOpenGLTexture::setData(int mipLevel, int layer, int layerCount, QOpenGLTexture::CubeMapFace cubeFace, QOpenGLTexture::PixelFormat sourceFormat, QOpenGLTexture::PixelType sourceType, const void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    if (!isStorageAllocated()) {
        qWarning("Cannot set data on a texture that does not have storage allocated.\n"
                 "To do so call allocateStorage() before this function");
        return;
    }
    d->setData(mipLevel, layer, layerCount, cubeFace, sourceFormat, sourceType, data, options);
}

/*!
    \since 5.3
    \overload
*/
void QOpenGLTexture::setData(int mipLevel, int layer,
                             PixelFormat sourceFormat, PixelType sourceType,
                             const void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setData(mipLevel, layer, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options);
}

/*!
    \since 5.3
    \overload
*/
void QOpenGLTexture::setData(int mipLevel,
                             PixelFormat sourceFormat, PixelType sourceType,
                             const void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setData(mipLevel, 0, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options);
}

/*!
    \since 5.3
    \overload
*/
void QOpenGLTexture::setData(PixelFormat sourceFormat, PixelType sourceType,
                             const void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setData(0, 0, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options);
}

/*!
    \since 5.14
    \overload

    This overload is to be used to update a part of the texture. Parameters \a
    xOffset, \a yOffset, \a zOffset specify the texel offsets within the
    texture. Parameters \a width, \a height and \a depth specify the dimensions
    of the sub image.

    The structure of the pixel data pointed to by \a data is specified by \a
    sourceFormat and \a sourceType. The pixel data upload can optionally be
    controlled by \a options.
*/
void QOpenGLTexture::setData(int xOffset, int yOffset, int zOffset,
                             int width, int height, int depth,
                             PixelFormat sourceFormat, PixelType sourceType,
                             const void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setData(xOffset, yOffset, zOffset,
               width, height, depth,
               0, 0, 1,
               QOpenGLTexture::CubeMapPositiveX, sourceFormat,
               sourceType, data, options);
}

/*!
    \since 5.14
    \overload

    This overload is to be used to update a part of the texture. Parameters \a
    xOffset, \a yOffset, \a zOffset specify the texel offsets within the
    texture. Parameters \a width, \a height and \a depth specify the dimensions
    of the sub image. The mip map level the sub image we want to
    update is specified with \a mipLevel.

    The structure of the pixel data pointed to by \a data is specified by \a
    sourceFormat and \a sourceType. The pixel data upload can optionally be
    controlled by \a options.
*/
void QOpenGLTexture::setData(int xOffset, int yOffset, int zOffset,
                             int width, int height, int depth,
                             int mipLevel,
                             PixelFormat sourceFormat, PixelType sourceType,
                             const void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setData(xOffset, yOffset, zOffset,
               width, height, depth,
               mipLevel, 0, 1,
               QOpenGLTexture::CubeMapPositiveX, sourceFormat,
               sourceType, data, options);
}

/*!
    \since 5.14
    \overload

    This overload is to be used to update a part of the texture. Parameters \a
    xOffset, \a yOffset, \a zOffset specify the texel offsets within the
    texture. Parameters \a width, \a height and \a depth specify the dimensions
    of the sub image. The mip map level and layerof the sub image we want to
    update are specified with \a mipLevel and \a layer.

    The structure of the pixel data pointed to by \a data is specified by \a
    sourceFormat and \a sourceType. The pixel data upload can optionally be
    controlled by \a options.
*/
void QOpenGLTexture::setData(int xOffset, int yOffset, int zOffset,
                             int width, int height, int depth,
                             int mipLevel, int layer,
                             PixelFormat sourceFormat, PixelType sourceType,
                             const void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setData(xOffset, yOffset, zOffset,
               width, height, depth,
               mipLevel, layer, 1,
               QOpenGLTexture::CubeMapPositiveX, sourceFormat,
               sourceType, data, options);
}

/*!
    \since 5.14
    \overload

    This overload is to be used to update a part of the texture. Parameters \a
    xOffset, \a yOffset, \a zOffset specify the texel offsets within the
    texture. Parameters \a width, \a height and \a depth specify the dimensions
    of the sub image.The mip map level, layer and cube map face of the sub
    image we want to update are specified with \a mipLevel, \a layer and \a
    face.

    The structure of the pixel data pointed to by \a data is specified by \a
    sourceFormat and \a sourceType. The pixel data upload can optionally be
    controlled by \a options.
*/
void QOpenGLTexture::setData(int xOffset, int yOffset, int zOffset,
                             int width, int height, int depth,
                             int mipLevel, int layer,
                             CubeMapFace face,
                             PixelFormat sourceFormat, PixelType sourceType,
                             const void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setData(xOffset, yOffset, zOffset,
               width, height, depth,
               mipLevel, layer, 1,
               face, sourceFormat,
               sourceType, data, options);
}

/*!
    \since 5.14
    \overload

    This overload is to be used to update a part of the texture. Parameters \a
    xOffset, \a yOffset, \a zOffset specify the texel offsets within the
    texture. Parameters \a width, \a height and \a depth specify the dimensions
    of the sub image.The mip map level, starting layer, cube map face and
    number of layers of the sub image we want to update are specified with \a
    mipLevel, \a layer, \a face and \a layerCount.

    The structure of the pixel data pointed to by \a data is specified by \a
    sourceFormat and \a sourceType. The pixel data upload can optionally be
    controlled by \a options.
*/
void QOpenGLTexture::setData(int xOffset, int yOffset, int zOffset,
                             int width, int height, int depth,
                             int mipLevel, int layer,
                             CubeMapFace face, int layerCount,
                             PixelFormat sourceFormat, PixelType sourceType,
                             const void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setData(xOffset, yOffset, zOffset,
               width, height, depth,
               mipLevel, layer, layerCount,
               face, sourceFormat,
               sourceType, data, options);
}

#if QT_DEPRECATED_SINCE(5, 3)
/*!
    \obsolete
    \overload

    \sa setCompressedData()
*/
void QOpenGLTexture::setData(int mipLevel, int layer, CubeMapFace cubeFace,
                             PixelFormat sourceFormat, PixelType sourceType,
                             void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    if (!isStorageAllocated()) {
        qWarning("Cannot set data on a texture that does not have storage allocated.\n"
                 "To do so call allocateStorage() before this function");
        return;
    }
    d->setData(mipLevel, layer, 1, cubeFace, sourceFormat, sourceType, data, options);
}

/*!
    \obsolete
    \overload
*/
void QOpenGLTexture::setData(int mipLevel, int layer,
                             PixelFormat sourceFormat, PixelType sourceType,
                             void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setData(mipLevel, layer, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options);
}

/*!
    \obsolete
    \overload
*/
void QOpenGLTexture::setData(int mipLevel,
                             PixelFormat sourceFormat, PixelType sourceType,
                             void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setData(mipLevel, 0, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options);
}

/*!
    \obsolete
    \overload
*/
void QOpenGLTexture::setData(PixelFormat sourceFormat, PixelType sourceType,
                             void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setData(0, 0, 1, QOpenGLTexture::CubeMapPositiveX, sourceFormat, sourceType, data, options);
}
#endif

/*!
    This overload of setData() will allocate storage for you.
    The pixel data is contained in \a image. Mipmaps are generated by default.
    Set \a genMipMaps to \l DontGenerateMipMaps to turn off mipmap generation.

    \overload
*/
void QOpenGLTexture::setData(const QImage& image, MipMapGeneration genMipMaps)
{
    QOpenGLContext *context = QOpenGLContext::currentContext();
    if (!context) {
        qWarning("QOpenGLTexture::setData() requires a valid current context");
        return;
    }

    if (image.isNull()) {
        qWarning("QOpenGLTexture::setData() tried to set a null image");
        return;
    }

    if (context->isOpenGLES() && context->format().majorVersion() < 3)
        setFormat(QOpenGLTexture::RGBAFormat);
    else
        setFormat(QOpenGLTexture::RGBA8_UNorm);

    setSize(image.width(), image.height());
    setMipLevels(genMipMaps == GenerateMipMaps ? maximumMipLevels() : 1);
    allocateStorage(QOpenGLTexture::RGBA, QOpenGLTexture::UInt8);

    // Upload pixel data and generate mipmaps
    QImage glImage = image.convertToFormat(QImage::Format_RGBA8888);
    QOpenGLPixelTransferOptions uploadOptions;
    uploadOptions.setAlignment(1);
    setData(0, QOpenGLTexture::RGBA, QOpenGLTexture::UInt8, glImage.constBits(), &uploadOptions);
}

/*!
    Uploads compressed pixel \a data to \a mipLevel, array \a layer, and \a cubeFace.
    The pixel transfer can optionally be controlled with \a options. The \a dataSize
    argument should specify the size of the data pointed to by \a data.

    If not using a compressed format() then you should use setData() instead of this
    function.

    \since 5.3
*/
void QOpenGLTexture::setCompressedData(int mipLevel, int layer, CubeMapFace cubeFace,
                                       int dataSize, const void *data,
                                       const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    if (!isStorageAllocated()) {
        qWarning("Cannot set data on a texture that does not have storage allocated.\n"
                 "To do so call allocateStorage() before this function");
        return;
    }
    d->setCompressedData(mipLevel, layer, 1, cubeFace, dataSize, data, options);
}

/*!
    \since 5.9
    \overload

    Parameter \a layerCount is the number of layers in a texture array
    that are being uploaded/populated by this call.
*/
void QOpenGLTexture::setCompressedData(int mipLevel, int layer, int layerCount, QOpenGLTexture::CubeMapFace cubeFace, int dataSize, const void *data, const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    if (!isStorageAllocated()) {
        qWarning("Cannot set data on a texture that does not have storage allocated.\n"
                 "To do so call allocateStorage() before this function");
        return;
    }
    d->setCompressedData(mipLevel, layer, layerCount, cubeFace, dataSize, data, options);
}

/*!
    \overload
*/
void QOpenGLTexture::setCompressedData(int mipLevel, int layer, int dataSize, const void *data,
                                       const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setCompressedData(mipLevel, layer, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options);
}

/*!
    \overload
*/
void QOpenGLTexture::setCompressedData(int mipLevel, int dataSize, const void *data,
                                       const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setCompressedData(mipLevel, 0, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options);
}

/*!
    \overload
*/
void QOpenGLTexture::setCompressedData(int dataSize, const void *data,
                                       const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setCompressedData(0, 0, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options);
}

#if QT_DEPRECATED_SINCE(5, 3)
/*!
    \obsolete
    \overload
*/
void QOpenGLTexture::setCompressedData(int mipLevel, int layer, CubeMapFace cubeFace,
                                       int dataSize, void *data,
                                       const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    if (!isStorageAllocated()) {
        qWarning("Cannot set data on a texture that does not have storage allocated.\n"
                 "To do so call allocateStorage() before this function");
        return;
    }
    d->setCompressedData(mipLevel, layer, 1, cubeFace, dataSize, data, options);
}

/*!
    \obsolete
    \overload
*/
void QOpenGLTexture::setCompressedData(int mipLevel, int layer, int dataSize, void *data,
                                       const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setCompressedData(mipLevel, layer, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options);
}

/*!
    \obsolete
    \overload
*/
void QOpenGLTexture::setCompressedData(int mipLevel, int dataSize, void *data,
                                       const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setCompressedData(mipLevel, 0, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options);
}

/*!
    \obsolete
    \overload
*/
void QOpenGLTexture::setCompressedData(int dataSize, void *data,
                                       const QOpenGLPixelTransferOptions * const options)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->textureId);
    d->setCompressedData(0, 0, 1, QOpenGLTexture::CubeMapPositiveX, dataSize, data, options);
}
#endif

/*!
    Returns \c true if your OpenGL implementation and version supports the texture
    feature \a feature.
*/
bool QOpenGLTexture::hasFeature(Feature feature)
{
    QOpenGLContext *ctx = QOpenGLContext::currentContext();
    if (!ctx) {
        qWarning("QOpenGLTexture::hasFeature() requires a valid current context");
        return false;
    }

    QSurfaceFormat f = ctx->format();

    bool supported = false;

#if !defined(QT_OPENGL_ES_2)
    if (!ctx->isOpenGLES()) {
        switch (feature) {
        case ImmutableMultisampleStorage:
            supported = f.version() >= qMakePair(4, 3)
                    || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_storage_multisample"));
            break;

        case TextureBuffer:
            supported = f.version() >= qMakePair(3, 0)
                    || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_buffer_object"));
            break;

        case StencilTexturing:
            supported = f.version() >= qMakePair(4, 3)
                    || ctx->hasExtension(QByteArrayLiteral("GL_ARB_stencil_texturing"));
            break;

        case ImmutableStorage:
            supported = f.version() >= qMakePair(4, 2)
                    || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_storage"))
                    || ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_storage"));
            break;

        case TextureCubeMapArrays:
            supported = f.version() >= qMakePair(4, 0)
                    || ctx->hasExtension(QByteArrayLiteral("ARB_texture_cube_map_array"));
            break;

        case Swizzle:
            supported = f.version() >= qMakePair(3, 3)
                    || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_swizzle"));
            break;

        case TextureMultisample:
            supported = f.version() >= qMakePair(3, 2)
                    || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_multisample"));
            break;

        case TextureArrays:
            supported = f.version() >= qMakePair(3, 0)
                    || ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_array"));
            break;

        case TextureRectangle:
            supported = f.version() >= qMakePair(2, 1)
                    || ctx->hasExtension(QByteArrayLiteral("ARB_texture_rectangle"));
            break;

        case Texture3D:
            supported = f.version() >= qMakePair(1, 3);
            break;

        case AnisotropicFiltering:
            supported = ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_filter_anisotropic"));
            break;

        case NPOTTextures:
        case NPOTTextureRepeat:
            supported = ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_non_power_of_two"));
            break;

        case Texture1D:
            supported = f.version() >= qMakePair(1, 1);
            break;

        case TextureComparisonOperators:
            // GL 1.4 and GL_ARB_shadow alone support only LEQUAL and GEQUAL;
            // since we're talking about history anyhow avoid to be extra pedantic
            // in the feature set, and simply claim supported if we have the full set of operators
            // (which has been added into 1.5 / GL_EXT_shadow_funcs).
            supported = f.version() >= qMakePair(1, 5)
                    || (ctx->hasExtension(QByteArrayLiteral("GL_ARB_shadow"))
                        && ctx->hasExtension(QByteArrayLiteral("GL_EXT_shadow_funcs")));
            break;

        case TextureMipMapLevel:
            supported = f.version() >= qMakePair(1, 2);
            break;

        case MaxFeatureFlag:
            break;
        }
    }

    if (ctx->isOpenGLES())
#endif
    {
        const char *renderer = reinterpret_cast<const char *>(ctx->functions()->glGetString(GL_RENDERER));
        switch (feature) {
        case ImmutableStorage:
            supported = (f.version() >= qMakePair(3, 0) || ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_storage")))
                && !(renderer && strstr(renderer, "Mali")); // do not use on Mali: QTBUG-45106
            break;

        case ImmutableMultisampleStorage:
            supported = f.version() >= qMakePair(3, 1);
            break;

        case TextureRectangle:
            break;

        case TextureArrays:
            supported = f.version() >= qMakePair(3, 0);
            break;

        case Texture3D:
            supported = f.version() >= qMakePair(3, 0)
                    || ctx->hasExtension(QByteArrayLiteral("GL_OES_texture_3D"));
            break;

        case TextureMultisample:
            supported = f.version() >= qMakePair(3, 1);
            break;

        case TextureBuffer:
            break;

        case TextureCubeMapArrays:
            break;

        case Swizzle:
            supported = f.version() >= qMakePair(3, 0);
            break;

        case StencilTexturing:
            break;

        case AnisotropicFiltering:
            supported = ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_filter_anisotropic"));
            break;

        case NPOTTextures:
        case NPOTTextureRepeat:
            supported = f.version() >= qMakePair(3,0)
                    || ctx->hasExtension(QByteArrayLiteral("GL_OES_texture_npot"))
                    || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_non_power_of_two"));
            break;

        case Texture1D:
            break;

        case TextureComparisonOperators:
            supported = f.version() >= qMakePair(3, 0)
                    || ctx->hasExtension(QByteArrayLiteral("GL_EXT_shadow_samplers"));
            break;

        case TextureMipMapLevel:
            supported = f.version() >= qMakePair(3, 0);
            break;

        case MaxFeatureFlag:
            break;
        }
    }

    return supported;
}

/*!
    Sets the base mipmap level used for all texture lookups with this texture to \a baseLevel.

    \note This function has no effect on Qt built for OpenGL ES 2.
    \sa mipBaseLevel(), setMipMaxLevel(), setMipLevelRange()
*/
void QOpenGLTexture::setMipBaseLevel(int baseLevel)
{
    Q_D(QOpenGLTexture);
    d->create();
    if (!d->features.testFlag(TextureMipMapLevel)) {
        qWarning("QOpenGLTexture::setMipBaseLevel: requires OpenGL >= 1.2 or OpenGL ES >= 3.0");
        return;
    }
    Q_ASSERT(d->textureId);
    Q_ASSERT(d->texFuncs);
    Q_ASSERT(baseLevel <= d->maxLevel);
    d->baseLevel = baseLevel;
    d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_BASE_LEVEL, baseLevel);
}

/*!
    Returns the mipmap base level used for all texture lookups with this texture.
    The default is 0.

    \sa setMipBaseLevel(), mipMaxLevel(), mipLevelRange()
*/
int QOpenGLTexture::mipBaseLevel() const
{
    Q_D(const QOpenGLTexture);
    return d->baseLevel;
}

/*!
    Sets the maximum mipmap level used for all texture lookups with this texture to \a maxLevel.

    \note This function has no effect on Qt built for OpenGL ES 2.
    \sa mipMaxLevel(), setMipBaseLevel(), setMipLevelRange()
*/
void QOpenGLTexture::setMipMaxLevel(int maxLevel)
{
    Q_D(QOpenGLTexture);
    d->create();
    if (!d->features.testFlag(TextureMipMapLevel)) {
        qWarning("QOpenGLTexture::setMipMaxLevel: requires OpenGL >= 1.2 or OpenGL ES >= 3.0");
        return;
    }
    Q_ASSERT(d->textureId);
    Q_ASSERT(d->texFuncs);
    Q_ASSERT(d->baseLevel <= maxLevel);
    d->maxLevel = maxLevel;
    d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAX_LEVEL, maxLevel);
}

/*!
    Returns the mipmap maximum level used for all texture lookups with this texture.

    \sa setMipMaxLevel(), mipBaseLevel(), mipLevelRange()
*/
int QOpenGLTexture::mipMaxLevel() const
{
    Q_D(const QOpenGLTexture);
    return d->maxLevel;
}

/*!
    Sets the range of mipmap levels that can be used for texture lookups with this texture
    to range from \a baseLevel to \a maxLevel.

    \note This function has no effect on Qt built for OpenGL ES 2.
    \sa setMipBaseLevel(), setMipMaxLevel(), mipLevelRange()
*/
void QOpenGLTexture::setMipLevelRange(int baseLevel, int maxLevel)
{
    Q_D(QOpenGLTexture);
    d->create();
    if (!d->features.testFlag(TextureMipMapLevel)) {
        qWarning("QOpenGLTexture::setMipLevelRange: requires OpenGL >= 1.2 or OpenGL ES >= 3.0");
        return;
    }
    Q_ASSERT(d->textureId);
    Q_ASSERT(d->texFuncs);
    Q_ASSERT(baseLevel <= maxLevel);
    d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_BASE_LEVEL, baseLevel);
    d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAX_LEVEL, maxLevel);
}

/*!
    Returns the range of mipmap levels that can be used for texture lookups with this texture.

    \sa mipBaseLevel(), mipMaxLevel()
*/
QPair<int, int> QOpenGLTexture::mipLevelRange() const
{
    Q_D(const QOpenGLTexture);
    return qMakePair(d->baseLevel, d->maxLevel);
}

/*!
    If \a enabled is \c true, enables automatic mipmap generation for this texture object
    to occur whenever the level 0 mipmap data is set via setData().

    The automatic mipmap generation is enabled by default.

    \note Mipmap generation is not supported for compressed textures with OpenGL ES 2.0.

    \sa isAutoMipMapGenerationEnabled(), generateMipMaps()
*/
void QOpenGLTexture::setAutoMipMapGenerationEnabled(bool enabled)
{
    Q_D(QOpenGLTexture);
    d->autoGenerateMipMaps = enabled;
}

/*!
    Returns whether auto mipmap generation is enabled for this texture object.

    \sa setAutoMipMapGenerationEnabled(), generateMipMaps()
*/
bool QOpenGLTexture::isAutoMipMapGenerationEnabled() const
{
    Q_D(const QOpenGLTexture);
    return d->autoGenerateMipMaps;
}

/*!
    Generates mipmaps for this texture object from mipmap level 0. If you are
    using a texture target and filtering option that requires mipmaps and you
    have disabled automatic mipmap generation then you need to call this function
    or the overload to create the mipmap chain.

    \note Mipmap generation is not supported for compressed textures with OpenGL ES.

    \sa setAutoMipMapGenerationEnabled(), setMipLevels(), mipLevels()
*/
void QOpenGLTexture::generateMipMaps()
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->texFuncs);
    Q_ASSERT(d->textureId);
    if (isCompressedFormat(d->format)) {
        if (QOpenGLContext *ctx = QOpenGLContext::currentContext())
            if (ctx->isOpenGLES())
                return;
    }
    d->texFuncs->glGenerateTextureMipmap(d->textureId, d->target, d->bindingTarget);
}

/*!
    Generates mipmaps for this texture object from mipmap level \a baseLevel. If you are
    using a texture target and filtering option that requires mipmaps and you
    have disabled automatic mipmap generation then you need to call this function
    or the overload to create the mipmap chain.

    The generation of mipmaps to above \a baseLevel is achieved by setting the mipmap
    base level to \a baseLevel and then generating the mipmap chain. If \a resetBaseLevel
    is \c true, then the baseLevel of the texture will be reset to its previous value.

    \sa setAutoMipMapGenerationEnabled(), setMipLevels(), mipLevels()
*/
void QOpenGLTexture::generateMipMaps(int baseLevel, bool resetBaseLevel)
{
    Q_D(QOpenGLTexture);
    Q_ASSERT(d->texFuncs);
    Q_ASSERT(d->textureId);
    if (isCompressedFormat(d->format)) {
        if (QOpenGLContext *ctx = QOpenGLContext::currentContext())
            if (ctx->isOpenGLES())
                return;
    }
    int oldBaseLevel;
    if (resetBaseLevel)
        oldBaseLevel = mipBaseLevel();
    setMipBaseLevel(baseLevel);
    d->texFuncs->glGenerateTextureMipmap(d->textureId, d->target, d->bindingTarget);
    if (resetBaseLevel)
        setMipBaseLevel(oldBaseLevel);
}

/*!
    GLSL shaders are able to reorder the components of the vec4 returned by texture
    functions. It is also desirable to be able to control this reordering from CPU
    side code. This is made possible by swizzle masks since OpenGL 3.3.

    Each component of the texture can be mapped to one of the SwizzleValue options.

    This function maps \a component to the output \a value.

    \note This function has no effect on Mac and Qt built for OpenGL ES 2.
    \sa swizzleMask()
*/
void QOpenGLTexture::setSwizzleMask(SwizzleComponent component, SwizzleValue value)
{
#if !defined(Q_OS_MAC) && !defined(QT_OPENGL_ES_2)
    if (!QOpenGLContext::currentContext()->isOpenGLES()) {
        Q_D(QOpenGLTexture);
        d->create();
        Q_ASSERT(d->texFuncs);
        Q_ASSERT(d->textureId);
        if (!d->features.testFlag(Swizzle)) {
            qWarning("QOpenGLTexture::setSwizzleMask() requires OpenGL >= 3.3");
            return;
        }
        d->swizzleMask[component - SwizzleRed] = value;
        d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, component, value);
        return;
    }
#else
    Q_UNUSED(component);
    Q_UNUSED(value);
#endif
    qWarning("QOpenGLTexture: Texture swizzling is not supported");
}

/*!
    Parameters \a {r}, \a {g}, \a {b}, and \a {a}  are values used for setting
    the colors red, green, blue, and the alpha value.
    \overload
*/
void QOpenGLTexture::setSwizzleMask(SwizzleValue r, SwizzleValue g,
                                    SwizzleValue b, SwizzleValue a)
{
#if !defined(Q_OS_MAC) && !defined(QT_OPENGL_ES_2)
    if (!QOpenGLContext::currentContext()->isOpenGLES()) {
        Q_D(QOpenGLTexture);
        d->create();
        Q_ASSERT(d->texFuncs);
        Q_ASSERT(d->textureId);
        if (!d->features.testFlag(Swizzle)) {
            qWarning("QOpenGLTexture::setSwizzleMask() requires OpenGL >= 3.3");
            return;
        }
        GLint swizzleMask[] = {GLint(r), GLint(g), GLint(b), GLint(a)};
        d->swizzleMask[0] = r;
        d->swizzleMask[1] = g;
        d->swizzleMask[2] = b;
        d->swizzleMask[3] = a;
        d->texFuncs->glTextureParameteriv(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
        return;
    }
#else
    Q_UNUSED(r);
    Q_UNUSED(g);
    Q_UNUSED(b);
    Q_UNUSED(a);
#endif
    qWarning("QOpenGLTexture: Texture swizzling is not supported");
}

/*!
    Returns the swizzle mask for texture \a component.
*/
QOpenGLTexture::SwizzleValue QOpenGLTexture::swizzleMask(SwizzleComponent component) const
{
    Q_D(const QOpenGLTexture);
    return d->swizzleMask[component - SwizzleRed];
}

/*!
    \enum QOpenGLTexture::DepthStencilMode
    \since 5.4
    This enum specifies which component of a depth/stencil texture is
    accessed when the texture is sampled.

    \value DepthMode Equivalent to GL_DEPTH_COMPONENT.
    \value StencilMode Equivalent to GL_STENCIL_INDEX.
*/

/*!
    If using a texture that has a combined depth/stencil format this function sets
    which component of the texture is accessed to \a mode.

    When the parameter is set to DepthMode, then accessing it from the
    shader will access the depth component as a single float, as normal. But when
    the parameter is set to StencilMode, the shader will access the stencil component.

    \note This function has no effect on Mac and Qt built for OpenGL ES 2.
    \since 5.4
    \sa depthStencilMode()
*/
void QOpenGLTexture::setDepthStencilMode(QOpenGLTexture::DepthStencilMode mode)
{
#if !defined(Q_OS_MAC) && !defined(QT_OPENGL_ES_2)
    if (!QOpenGLContext::currentContext()->isOpenGLES()) {
        Q_D(QOpenGLTexture);
        d->create();
        Q_ASSERT(d->texFuncs);
        Q_ASSERT(d->textureId);
        if (!d->features.testFlag(StencilTexturing)) {
            qWarning("QOpenGLTexture::setDepthStencilMode() requires OpenGL >= 4.3 or GL_ARB_stencil_texturing");
            return;
        }
        d->depthStencilMode = mode;
        d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_DEPTH_STENCIL_TEXTURE_MODE, mode);
        return;
    }
#else
    Q_UNUSED(mode);
#endif
    qWarning("QOpenGLTexture: DepthStencil Mode is not supported");
}

/*!
    Returns the depth stencil mode for textures using a combined depth/stencil format.

    \since 5.4
    \sa setDepthStencilMode()
*/
QOpenGLTexture::DepthStencilMode QOpenGLTexture::depthStencilMode() const
{
    Q_D(const QOpenGLTexture);
    return d->depthStencilMode;
}

/*!
    \enum QOpenGLTexture::ComparisonFunction
    \since 5.5
    This enum specifies which comparison operator is used when texture comparison
    is enabled on this texture.

    \value CompareLessEqual Equivalent to GL_LEQUAL.
    \value CompareGreaterEqual Equivalent to GL_GEQUAL.
    \value CompareLess Equivalent to GL_LESS.
    \value CompareGreater Equivalent to GL_GREATER.
    \value CompareEqual Equivalent to GL_EQUAL.
    \value CommpareNotEqual Equivalent to GL_NOTEQUAL.
    \value CompareAlways Equivalent to GL_ALWAYS.
    \value CompareNever Equivalent to GL_NEVER.

*/

/*!
    \since 5.5

    Sets the texture comparison function on this texture to \a function. The texture
    comparison function is used by shadow samplers when sampling a depth texture.

    \sa comparisonFunction()
*/
void QOpenGLTexture::setComparisonFunction(QOpenGLTexture::ComparisonFunction function)
{
    Q_D(QOpenGLTexture);
    d->create();
    if (!d->features.testFlag(TextureComparisonOperators)) {
        qWarning("QOpenGLTexture::setComparisonFunction: requires OpenGL >= 1.5 or OpenGL ES >= 3.0");
        return;
    }
    d->comparisonFunction = function;
    d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_COMPARE_FUNC, function);
}

/*!
    \since 5.5

    Returns the texture comparison operator set on this texture. By default, a
    texture has a CompareLessEqual comparison function.

    \sa setComparisonFunction()
*/
QOpenGLTexture::ComparisonFunction QOpenGLTexture::comparisonFunction() const
{
    Q_D(const QOpenGLTexture);
    return d->comparisonFunction;
}

/*!
    \enum QOpenGLTexture::ComparisonMode
    \since 5.5
    This enum specifies which comparison mode is used when sampling this texture.

    \value CompareRefToTexture Equivalent to GL_COMPARE_REF_TO_TEXTURE.
    \value CompareNone Equivalent to GL_NONE.
*/

/*!
    \since 5.5

    Sets the texture comparison mode on this texture to \a mode. The texture
    comparison mode is used by shadow samplers when sampling a depth texture.

    \sa comparisonMode()
*/
void QOpenGLTexture::setComparisonMode(QOpenGLTexture::ComparisonMode mode)
{
    Q_D(QOpenGLTexture);
    d->create();
    if (!d->features.testFlag(TextureComparisonOperators)) {
        qWarning("QOpenGLTexture::setComparisonMode: requires OpenGL >= 1.5 or OpenGL ES >= 3.0");
        return;
    }
    d->comparisonMode = mode;
    d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_COMPARE_MODE, mode);
}

/*!
    \since 5.5

    Returns the texture comparison mode set on this texture. By default, a
    texture has a CompareNone comparison mode (i.e. comparisons are disabled).

    \sa setComparisonMode()
*/
QOpenGLTexture::ComparisonMode QOpenGLTexture::comparisonMode() const
{
    Q_D(const QOpenGLTexture);
    return d->comparisonMode;
}

/*!
    Sets the filter used for minification to \a filter.

    \sa minificationFilter(), setMagnificationFilter(), setMinMagFilters()
*/
void QOpenGLTexture::setMinificationFilter(QOpenGLTexture::Filter filter)
{
    Q_D(QOpenGLTexture);
    d->create();
    Q_ASSERT(d->texFuncs);
    Q_ASSERT(d->textureId);
    d->minFilter = filter;
    d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MIN_FILTER, filter);
}

/*!
    Returns the minification filter.

    \sa setMinificationFilter()
*/
QOpenGLTexture::Filter QOpenGLTexture::minificationFilter() const
{
    Q_D(const QOpenGLTexture);
    return d->minFilter;
}

/*!
    Sets the magnification filter to \a filter.

    \sa magnificationFilter(), setMinificationFilter(), setMinMagFilters()
*/
void QOpenGLTexture::setMagnificationFilter(QOpenGLTexture::Filter filter)
{
    Q_D(QOpenGLTexture);
    d->create();
    Q_ASSERT(d->texFuncs);
    Q_ASSERT(d->textureId);
    d->magFilter = filter;
    d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAG_FILTER, filter);
}

/*!
    Returns the magnification filter.

    \sa setMagnificationFilter()
*/
QOpenGLTexture::Filter QOpenGLTexture::magnificationFilter() const
{
    Q_D(const QOpenGLTexture);
    return d->magFilter;
}

/*!
    Sets the minification filter to \a minificationFilter and the magnification filter
    to \a magnificationFilter.

    \sa minMagFilters(), setMinificationFilter(), setMagnificationFilter()
*/
void QOpenGLTexture::setMinMagFilters(QOpenGLTexture::Filter minificationFilter,
                                      QOpenGLTexture::Filter magnificationFilter)
{
    Q_D(QOpenGLTexture);
    d->create();
    Q_ASSERT(d->texFuncs);
    Q_ASSERT(d->textureId);
    d->minFilter = minificationFilter;
    d->magFilter = magnificationFilter;
    d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MIN_FILTER, minificationFilter);
    d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAG_FILTER, magnificationFilter);
}

/*!
    Returns the current minification and magnification filters.

    \sa setMinMagFilters()
*/
QPair<QOpenGLTexture::Filter, QOpenGLTexture::Filter> QOpenGLTexture::minMagFilters() const
{
    Q_D(const QOpenGLTexture);
    return QPair<QOpenGLTexture::Filter, QOpenGLTexture::Filter>(d->minFilter, d->magFilter);
}

/*!
    If your OpenGL implementation supports the GL_EXT_texture_filter_anisotropic extension
    this function sets the maximum anisotropy level to \a anisotropy.

    \sa maximumAnisotropy()
*/
void QOpenGLTexture::setMaximumAnisotropy(float anisotropy)
{
    Q_D(QOpenGLTexture);
    d->create();
    Q_ASSERT(d->texFuncs);
    Q_ASSERT(d->textureId);
    if (!d->features.testFlag(AnisotropicFiltering)) {
        qWarning("QOpenGLTexture::setMaximumAnisotropy() requires GL_EXT_texture_filter_anisotropic");
        return;
    }
    d->maxAnisotropy = anisotropy;
    d->texFuncs->glTextureParameteri(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
}

/*!
    Returns the maximum level of anisotropy to be accounted for when performing texture lookups.
    This requires the GL_EXT_texture_filter_anisotropic extension.

    \sa setMaximumAnisotropy()
*/
float QOpenGLTexture::maximumAnisotropy() const
{
    Q_D(const QOpenGLTexture);
    return d->maxAnisotropy;
}

/*!
    Sets the wrap (or repeat mode) for all texture dimentions to \a mode.

    \sa wrapMode()
*/
void QOpenGLTexture::setWrapMode(QOpenGLTexture::WrapMode mode)
{
    Q_D(QOpenGLTexture);
    d->create();
    Q_ASSERT(d->texFuncs);
    Q_ASSERT(d->textureId);
    d->setWrapMode(mode);
}

/*!
    Holds the texture dimension \a direction.
    \overload
*/
void QOpenGLTexture::setWrapMode(QOpenGLTexture::CoordinateDirection direction, QOpenGLTexture::WrapMode mode)
{
    Q_D(QOpenGLTexture);
    d->create();
    Q_ASSERT(d->texFuncs);
    Q_ASSERT(d->textureId);
    d->setWrapMode(direction, mode);
}

/*!
    Returns the wrap mode for the texture dimension \a direction.

    \sa setWrapMode()
*/
QOpenGLTexture::WrapMode QOpenGLTexture::wrapMode(QOpenGLTexture::CoordinateDirection direction) const
{
    Q_D(const QOpenGLTexture);
    return d->wrapMode(direction);
}

/*!
    Sets the border color of the texture to \a color.

    \note This function has no effect on Mac and Qt built for OpenGL ES 2.
    \sa borderColor()
*/
void QOpenGLTexture::setBorderColor(QColor color)
{
    setBorderColor(static_cast<float>(color.redF()), static_cast<float>(color.greenF()),
                   static_cast<float>(color.blueF()), static_cast<float>(color.alphaF()));
}

/*!
    Sets the color red to \a {r}, green to \a {g}, blue to \a {b}, and \a {a} to the
    alpha value.
    \overload
*/
void QOpenGLTexture::setBorderColor(float r, float g, float b, float a)
{
#if !defined(QT_OPENGL_ES_2)
    if (!QOpenGLContext::currentContext()->isOpenGLES()) {
        Q_D(QOpenGLTexture);
        d->create();
        Q_ASSERT(d->texFuncs);
        Q_ASSERT(d->textureId);
        float values[4];
        values[0] = r;
        values[1] = g;
        values[2] = b;
        values[3] = a;
        d->borderColor.clear();
        for (int i = 0; i < 4; ++i)
            d->borderColor.append(QVariant(values[i]));
        d->texFuncs->glTextureParameterfv(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_BORDER_COLOR, values);
        return;
    }
#else
    Q_UNUSED(r);
    Q_UNUSED(g);
    Q_UNUSED(b);
    Q_UNUSED(a);
#endif
    qWarning("QOpenGLTexture: Border color is not supported");
}

/*!
    Sets the color red to \a {r}, green to \a {g}, blue to \a {b}, and the alpha
    value to \a {a}.
    \overload
*/
void QOpenGLTexture::setBorderColor(int r, int g, int b, int a)
{
#if !defined(QT_OPENGL_ES_2)
    if (!QOpenGLContext::currentContext()->isOpenGLES()) {
        Q_D(QOpenGLTexture);
        d->create();
        Q_ASSERT(d->texFuncs);
        Q_ASSERT(d->textureId);
        int values[4];
        values[0] = r;
        values[1] = g;
        values[2] = b;
        values[3] = a;
        d->borderColor.clear();
        for (int i = 0; i < 4; ++i)
            d->borderColor.append(QVariant(values[i]));
        d->texFuncs->glTextureParameteriv(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_BORDER_COLOR, values);
        return;
    }
#else
    Q_UNUSED(r);
    Q_UNUSED(g);
    Q_UNUSED(b);
    Q_UNUSED(a);
#endif
    qWarning("QOpenGLTexture: Border color is not supported");

    // TODO Handle case of using glTextureParameterIiv() based on format
}

/*!
    Sets the color red to \a {r}, green to \a {g}, blue to \a {b}, and the alpha
    value to \a {a}.
    \overload
*/
void QOpenGLTexture::setBorderColor(uint r, uint g, uint b, uint a)
{
#if !defined(QT_OPENGL_ES_2)
    if (!QOpenGLContext::currentContext()->isOpenGLES()) {
        Q_D(QOpenGLTexture);
        d->create();
        Q_ASSERT(d->texFuncs);
        Q_ASSERT(d->textureId);
        int values[4];
        values[0] = int(r);
        values[1] = int(g);
        values[2] = int(b);
        values[3] = int(a);
        d->borderColor.clear();
        for (int i = 0; i < 4; ++i)
            d->borderColor.append(QVariant(values[i]));
        d->texFuncs->glTextureParameteriv(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_BORDER_COLOR, values);
        return;
    }
#else
    Q_UNUSED(r);
    Q_UNUSED(g);
    Q_UNUSED(b);
    Q_UNUSED(a);
#endif
    qWarning("QOpenGLTexture: Border color is not supported");

    // TODO Handle case of using glTextureParameterIuiv() based on format
}

/*!
    Returns the borderColor of this texture.

    \sa setBorderColor()
*/
QColor QOpenGLTexture::borderColor() const
{
    Q_D(const QOpenGLTexture);
    QColor c(0.0f, 0.0f, 0.0f, 0.0f);
    if (!d->borderColor.isEmpty()) {
        c.setRedF(d->borderColor.at(0).toFloat());
        c.setGreenF(d->borderColor.at(1).toFloat());
        c.setBlueF(d->borderColor.at(2).toFloat());
        c.setAlphaF(d->borderColor.at(3).toFloat());
    }
    return c;
}

/*!
    Writes the texture border color into the first four elements
    of the array pointed to by \a border.

    \sa setBorderColor()
*/
void QOpenGLTexture::borderColor(float *border) const
{
    Q_D(const QOpenGLTexture);
    Q_ASSERT(border);
    if (d->borderColor.isEmpty()) {
        for (int i = 0; i < 4; ++i)
            border[i] = 0.0f;
    } else {
        for (int i = 0; i < 4; ++i)
            border[i] = d->borderColor.at(i).toFloat();
    }
}

/*!
    Writes the texture border color into the first four elements
    of the array pointed to by \a border.

    \overload
*/
void QOpenGLTexture::borderColor(int *border) const
{
    Q_D(const QOpenGLTexture);
    Q_ASSERT(border);
    if (d->borderColor.isEmpty()) {
        for (int i = 0; i < 4; ++i)
            border[i] = 0;
    } else {
        for (int i = 0; i < 4; ++i)
            border[i] = d->borderColor.at(i).toInt();
    }
}

/*!
    Writes the texture border color into the first four elements
    of the array pointed to by \a border.

    \overload
*/
void QOpenGLTexture::borderColor(unsigned int *border) const
{
    Q_D(const QOpenGLTexture);
    Q_ASSERT(border);
    if (d->borderColor.isEmpty()) {
        for (int i = 0; i < 4; ++i)
            border[i] = 0;
    } else {
        for (int i = 0; i < 4; ++i)
            border[i] = d->borderColor.at(i).toUInt();
    }
}

/*!
    Sets the minimum level of detail to \a value. This limits the selection of highest
    resolution mipmap (lowest mipmap level). The default value is -1000.

    \note This function has no effect on Qt built for OpenGL ES 2.
    \sa minimumLevelOfDetail(), setMaximumLevelOfDetail(), setLevelOfDetailRange()
*/
void QOpenGLTexture::setMinimumLevelOfDetail(float value)
{
#if !defined(QT_OPENGL_ES_2)
    if (!QOpenGLContext::currentContext()->isOpenGLES()) {
        Q_D(QOpenGLTexture);
        d->create();
        Q_ASSERT(d->texFuncs);
        Q_ASSERT(d->textureId);
        Q_ASSERT(value < d->maxLevelOfDetail);
        d->minLevelOfDetail = value;
        d->texFuncs->glTextureParameterf(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MIN_LOD, value);
        return;
    }
#else
    Q_UNUSED(value);
#endif
    qWarning("QOpenGLTexture: Detail level is not supported");
}

/*!
    Returns the minimum level of detail parameter.

    \sa setMinimumLevelOfDetail(), maximumLevelOfDetail(), levelOfDetailRange()
*/
float QOpenGLTexture::minimumLevelOfDetail() const
{
    Q_D(const QOpenGLTexture);
    return d->minLevelOfDetail;
}

/*!
    Sets the maximum level of detail to \a value. This limits the selection of lowest
    resolution mipmap (highest mipmap level). The default value is 1000.

    \note This function has no effect on Qt built for OpenGL ES 2.
    \sa maximumLevelOfDetail(), setMinimumLevelOfDetail(), setLevelOfDetailRange()
*/
void QOpenGLTexture::setMaximumLevelOfDetail(float value)
{
#if !defined(QT_OPENGL_ES_2)
    if (!QOpenGLContext::currentContext()->isOpenGLES()) {
        Q_D(QOpenGLTexture);
        d->create();
        Q_ASSERT(d->texFuncs);
        Q_ASSERT(d->textureId);
        Q_ASSERT(value > d->minLevelOfDetail);
        d->maxLevelOfDetail = value;
        d->texFuncs->glTextureParameterf(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAX_LOD, value);
        return;
    }
#else
    Q_UNUSED(value);
#endif
    qWarning("QOpenGLTexture: Detail level is not supported");
}

/*!
    Returns the maximum level of detail parameter.

    \sa setMaximumLevelOfDetail(), minimumLevelOfDetail(), levelOfDetailRange()
*/
float QOpenGLTexture::maximumLevelOfDetail() const
{
    Q_D(const QOpenGLTexture);
    return d->maxLevelOfDetail;
}

/*!
    Sets the minimum level of detail parameters to \a min and the maximum level
    to \a max.
    \note This function has no effect on Qt built for OpenGL ES 2.
    \sa levelOfDetailRange(), setMinimumLevelOfDetail(), setMaximumLevelOfDetail()
*/
void QOpenGLTexture::setLevelOfDetailRange(float min, float max)
{
#if !defined(QT_OPENGL_ES_2)
    if (!QOpenGLContext::currentContext()->isOpenGLES()) {
        Q_D(QOpenGLTexture);
        d->create();
        Q_ASSERT(d->texFuncs);
        Q_ASSERT(d->textureId);
        Q_ASSERT(min < max);
        d->minLevelOfDetail = min;
        d->maxLevelOfDetail = max;
        d->texFuncs->glTextureParameterf(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MIN_LOD, min);
        d->texFuncs->glTextureParameterf(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_MAX_LOD, max);
        return;
    }
#else
    Q_UNUSED(min);
    Q_UNUSED(max);
#endif
    qWarning("QOpenGLTexture: Detail level is not supported");
}

/*!
    Returns the minimum and maximum level of detail parameters.

    \sa setLevelOfDetailRange(), minimumLevelOfDetail(), maximumLevelOfDetail()
*/
QPair<float, float> QOpenGLTexture::levelOfDetailRange() const
{
    Q_D(const QOpenGLTexture);
    return qMakePair(d->minLevelOfDetail, d->maxLevelOfDetail);
}

/*!
    Sets the level of detail bias to \a bias.
    Level of detail bias affects the point at which mipmapping levels change.
    Increasing values for level of detail bias makes the overall images blurrier
    or smoother. Decreasing values make the overall images sharper.

    \note This function has no effect on Qt built for OpenGL ES 2.
    \sa levelofDetailBias()
*/
void QOpenGLTexture::setLevelofDetailBias(float bias)
{
#if !defined(QT_OPENGL_ES_2)
    if (!QOpenGLContext::currentContext()->isOpenGLES()) {
        Q_D(QOpenGLTexture);
        d->create();
        Q_ASSERT(d->texFuncs);
        Q_ASSERT(d->textureId);
        d->levelOfDetailBias = bias;
        d->texFuncs->glTextureParameterf(d->textureId, d->target, d->bindingTarget, GL_TEXTURE_LOD_BIAS, bias);
        return;
    }
#else
    Q_UNUSED(bias);
#endif
    qWarning("QOpenGLTexture: Detail level is not supported");
}

/*!
    Returns the level of detail bias parameter.

    \sa setLevelofDetailBias()
*/
float QOpenGLTexture::levelofDetailBias() const
{
    Q_D(const QOpenGLTexture);
    return d->levelOfDetailBias;
}

#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug debug, const QOpenGLTexture *t)
{
    QDebugStateSaver saver(debug);
    debug.nospace();
    debug << "QOpenGLTexture(";
    if (t) {
        const QOpenGLTexturePrivate *d = t->d_ptr.data();
        debug << d->target << ", bindingTarget=" << d->bindingTarget
            << ", size=[" << d->dimensions[0]
            << ", " << d->dimensions[1];
        if (d->target == QOpenGLTexture::Target3D)
            debug << ", " << d->dimensions[2];
        debug << "], format=" << d->format << ", formatClass=" << d->formatClass;
        if (t->isCreated())
            debug << ", textureId=" << d->textureId;
        if (t->isBound())
            debug << ", [bound]";
        if (t->isTextureView())
            debug << ", [view]";
        if (d->fixedSamplePositions)
            debug << ", [fixedSamplePositions]";
        debug << ", mipLevels=" << d->requestedMipLevels << ", layers=" << d->layers
            << ", faces=" << d->faces << ", samples=" << d->samples
            << ", depthStencilMode=" << d->depthStencilMode << ", comparisonFunction="
            << d->comparisonFunction << ", comparisonMode=" << d->comparisonMode
            << ", features=" << d->features << ", minificationFilter=" << d->minFilter
            << ", magnificationFilter=" << d->magFilter << ", wrapMode=" << d->wrapModes[0];
    } else {
        debug << '0';
    }
    debug << ')';
    return debug;
}
#endif // QT_NO_DEBUG_STREAM

QT_END_NAMESPACE