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
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the QtOpenGL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qglshaderprogram.h"
#include "qglextensions_p.h"
#include "qgl_p.h"
#include <QtCore/qdebug.h>
#include <QtCore/qfile.h>
#include <QtCore/qvarlengtharray.h>
#include <QtCore/qvector.h>
QT_BEGIN_NAMESPACE
#if !defined(QT_OPENGL_ES_1_CL)
/*!
\class QGLShaderProgram
\brief The QGLShaderProgram class allows OpenGL shader programs to be linked and used.
\since 4.6
\section1 Introduction
This class supports shader programs written in the OpenGL Shading
Language (GLSL) and in the OpenGL/ES Shading Language (GLSL/ES).
QGLShader and QGLShaderProgram shelter the programmer from the details of
compiling and linking vertex and fragment shaders.
The following example creates a vertex shader program using the
supplied source \c{code}. Once compiled and linked, the shader
program is activated in the current QGLContext by calling
QGLShaderProgram::enable():
\code
QGLShader shader(QGLShader::VertexShader);
shader.setSourceCode(code);
QGLShaderProgram program(context);
program.addShader(shader);
program.link();
program.enable();
\endcode
\section1 Writing portable shaders
Shader programs can be difficult to reuse across OpenGL implementations
because of varying levels of support for standard vertex attributes and
uniform variables. In particular, GLSL/ES lacks all of the
standard variables that are present on desktop OpenGL systems:
\c{gl_Vertex}, \c{gl_Normal}, \c{gl_Color}, and so on. Desktop OpenGL
lacks the variable qualifiers \c{highp}, \c{mediump}, and \c{lowp}.
The QGLShaderProgram class makes the process of writing portable shaders
easier by prefixing all shader programs with the following lines on
desktop OpenGL:
\code
#define highp
#define mediump
#define lowp
\endcode
This makes it possible to run most GLSL/ES shader programs
on desktop systems. The programmer should restrict themselves
to just features that are present in GLSL/ES, and avoid
standard variable names that only work on the desktop.
\section1 Simple shader example
\code
program.addShader(QGLShader::VertexShader,
"attribute highp vec4 vertex;\n"
"attribute mediump mat4 matrix;\n"
"void main(void)\n"
"{\n"
" gl_Position = matrix * vertex;\n"
"}");
program.addShader(QGLShader::FragmentShader,
"uniform mediump vec4 color;\n"
"void main(void)\n"
"{\n"
" gl_FragColor = color;\n"
"}");
program.link();
program.enable();
int vertexLocation = program.attributeLocation("vertex");
int matrixLocation = program.attributeLocation("matrix");
int colorLocation = program.uniformLocation("color");
\endcode
With the above shader program active, we can draw a green triangle
as follows:
\code
static GLfloat const triangleVertices[] = {
60.0f, 10.0f, 0.0f,
110.0f, 110.0f, 0.0f,
10.0f, 110.0f, 0.0f
};
QColor color(0, 255, 0, 255);
QMatrix4x4 pmvMatrix;
pmvMatrix.ortho(rect());
program.setAttributeArray(vertexLocation, triangleVertices, 3);
program.setUniformValue(matrixLocation, pmvMatrix);
program.setUniformValue(colorLocation, color);
glDrawArrays(GL_TRIANGLES, 0, 3);
\endcode
\section1 Partial shaders
Desktop GLSL can attach an arbitrary number of vertex and fragment
shaders to a shader program. Embedded GLSL/ES on the other hand
supports only a single shader of each type on a shader program.
Multiple shaders of the same type can be useful when large libraries
of shaders are needed. Common functions can be factored out into
library shaders that can be reused in multiple shader programs.
To support this use of shaders, the application programmer can
create shaders with the QGLShader::PartialVertexShader and
QGLShader::PartialFragmentShader types. These types direct
QGLShader and QGLShaderProgram to delay shader compilation until
link time.
When link() is called, the sources for the partial shaders are
concatenated, and a single vertex or fragment shader is compiled
and linked into the shader program.
It is more efficient to use the QGLShader::VertexShader and
QGLShader::FragmentShader when there is only one shader of that
type in the program.
\sa QGLShader
*/
/*!
\class QGLShader
\brief The QGLShader class allows OpenGL shaders to be compiled.
\since 4.6
This class supports shaders written in the OpenGL Shading Language (GLSL)
and in the OpenGL/ES Shading Language (GLSL/ES).
QGLShader and QGLShaderProgram shelter the programmer from the details of
compiling and linking vertex and fragment shaders.
\sa QGLShaderProgram
*/
/*!
\enum QGLShader::ShaderType
This enum specifies the type of QGLShader that is being created.
\value VertexShader Vertex shader written in the OpenGL Shading Language (GLSL).
\value FragmentShader Fragment shader written in the OpenGL Shading Language (GLSL).
\value PartialVertexShader Partial vertex shader that will be concatenated with all other partial vertex shaders at link time.
\value PartialFragmentShader Partial fragment shader that will be concatenated with all other partial fragment shaders at link time.
*/
#ifndef GL_FRAGMENT_SHADER
#define GL_FRAGMENT_SHADER 0x8B30
#endif
#ifndef GL_VERTEX_SHADER
#define GL_VERTEX_SHADER 0x8B31
#endif
#ifndef GL_COMPILE_STATUS
#define GL_COMPILE_STATUS 0x8B81
#endif
#ifndef GL_LINK_STATUS
#define GL_LINK_STATUS 0x8B82
#endif
#ifndef GL_INFO_LOG_LENGTH
#define GL_INFO_LOG_LENGTH 0x8B84
#endif
#ifndef GL_ACTIVE_UNIFORMS
#define GL_ACTIVE_UNIFORMS 0x8B86
#endif
#ifndef GL_ACTIVE_UNIFORM_MAX_LENGTH
#define GL_ACTIVE_UNIFORM_MAX_LENGTH 0x8B87
#endif
#ifndef GL_ACTIVE_ATTRIBUTES
#define GL_ACTIVE_ATTRIBUTES 0x8B89
#endif
#ifndef GL_ACTIVE_ATTRIBUTE_MAX_LENGTH
#define GL_ACTIVE_ATTRIBUTE_MAX_LENGTH 0x8B8A
#endif
#ifndef GL_CURRENT_VERTEX_ATTRIB
#define GL_CURRENT_VERTEX_ATTRIB 0x8626
#endif
#ifndef GL_SHADER_SOURCE_LENGTH
#define GL_SHADER_SOURCE_LENGTH 0x8B88
#endif
#ifndef GL_SHADER_BINARY_FORMATS
#define GL_SHADER_BINARY_FORMATS 0x8DF8
#endif
#ifndef GL_NUM_SHADER_BINARY_FORMATS
#define GL_NUM_SHADER_BINARY_FORMATS 0x8DF9
#endif
class QGLShaderPrivate
{
public:
QGLShaderPrivate(QGLShader::ShaderType type, const QGLContext *ctx)
{
context = ctx;
shader = 0;
shaderType = type;
compiled = false;
isPartial = (type == QGLShader::PartialVertexShader ||
type == QGLShader::PartialFragmentShader);
hasPartialSource = false;
}
const QGLContext *context;
GLuint shader;
QGLShader::ShaderType shaderType;
bool compiled;
bool isPartial;
bool hasPartialSource;
QString errors;
QByteArray partialSource;
bool create();
bool compile();
};
#define ctx context
bool QGLShaderPrivate::create()
{
if (isPartial)
return true;
if (!context)
context = QGLContext::currentContext();
if (!context)
return false;
if (qt_resolve_glsl_extensions(const_cast<QGLContext *>(context))) {
if (shaderType == QGLShader::VertexShader)
shader = glCreateShader(GL_VERTEX_SHADER);
else
shader = glCreateShader(GL_FRAGMENT_SHADER);
if (!shader) {
qWarning() << "QGLShader: could not create shader";
return false;
}
return true;
} else {
return false;
}
}
bool QGLShaderPrivate::compile()
{
// Partial shaders are compiled during QGLShaderProgram::link().
if (isPartial && hasPartialSource) {
compiled = true;
return true;
}
if (!shader)
return false;
glCompileShader(shader);
GLint value = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &value);
compiled = (value != 0);
value = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &value);
if (!compiled && value > 1) {
char *log = new char [value];
GLint len;
glGetShaderInfoLog(shader, value, &len, log);
errors = QString::fromLatin1(log);
qWarning() << "QGLShader::compile:" << errors;
delete [] log;
}
return compiled;
}
#undef ctx
#define ctx d->context
/*!
Constructs a new QGLShader object of the specified \a type
and attaches it to \a parent. If shader programs are not supported,
then isValid() will return false.
This constructor is normally followed by a call to setSourceCode()
or setSourceCodeFile().
The shader will be associated with the current QGLContext.
\sa setSourceCode(), setSourceCodeFile(), isValid()
*/
QGLShader::QGLShader(QGLShader::ShaderType type, QObject *parent)
: QObject(parent)
{
d = new QGLShaderPrivate(type, QGLContext::currentContext());
d->create();
}
/*!
Constructs a new QGLShader object from the source code in \a fileName
and attaches it to \a parent. If the filename ends in \c{.fsh},
it is assumed to be a fragment shader, otherwise it is assumed to
be a vertex shader (normally the extension is \c{.vsh} for vertex shaders).
If the shader could not be loaded, then isValid() will return false.
The shader will be associated with the current QGLContext.
\sa isValid()
*/
QGLShader::QGLShader(const QString& fileName, QObject *parent)
: QObject(parent)
{
if (fileName.endsWith(QLatin1String(".fsh"), Qt::CaseInsensitive))
d = new QGLShaderPrivate(QGLShader::FragmentShader, QGLContext::currentContext());
else
d = new QGLShaderPrivate(QGLShader::VertexShader, QGLContext::currentContext());
if (d->create() && !setSourceCodeFile(fileName)) {
if (d->shader)
glDeleteShader(d->shader);
d->shader = 0;
}
}
/*!
Constructs a new QGLShader object of the specified \a type from the
source code in \a fileName and attaches it to \a parent.
If the shader could not be loaded, then isValid() will return false.
The shader will be associated with the current QGLContext.
\sa isValid()
*/
QGLShader::QGLShader
(const QString& fileName, QGLShader::ShaderType type, QObject *parent)
: QObject(parent)
{
d = new QGLShaderPrivate(type, QGLContext::currentContext());
if (d->create() && !setSourceCodeFile(fileName)) {
if (d->shader)
glDeleteShader(d->shader);
d->shader = 0;
}
}
/*!
Constructs a new QGLShader object of the specified \a type
and attaches it to \a parent. If shader programs are not supported,
then isValid() will return false.
This constructor is normally followed by a call to setSourceCode()
or setSourceCodeFile().
The shader will be associated with \a context.
\sa setSourceCode(), setSourceCodeFile(), isValid()
*/
QGLShader::QGLShader(QGLShader::ShaderType type, const QGLContext *context, QObject *parent)
: QObject(parent)
{
d = new QGLShaderPrivate(type, context);
d->create();
}
/*!
Constructs a new QGLShader object from the source code in \a fileName
and attaches it to \a parent. If the filename ends in \c{.fsh},
it is assumed to be a fragment shader, otherwise it is assumed to
be a vertex shader (normally the extension is \c{.vsh} for vertex shaders).
If the shader could not be loaded, then isValid() will return false.
The shader will be associated with \a context.
\sa isValid()
*/
QGLShader::QGLShader(const QString& fileName, const QGLContext *context, QObject *parent)
: QObject(parent)
{
if (fileName.endsWith(QLatin1String(".fsh"), Qt::CaseInsensitive))
d = new QGLShaderPrivate(QGLShader::FragmentShader, context);
else
d = new QGLShaderPrivate(QGLShader::VertexShader, context);
if (d->create() && !setSourceCodeFile(fileName)) {
if (d->shader)
glDeleteShader(d->shader);
d->shader = 0;
}
}
/*!
Constructs a new QGLShader object of the specified \a type from the
source code in \a fileName and attaches it to \a parent.
If the shader could not be loaded, then isValid() will return false.
The shader will be associated with \a context.
\sa isValid()
*/
QGLShader::QGLShader
(const QString& fileName, QGLShader::ShaderType type, const QGLContext *context, QObject *parent)
: QObject(parent)
{
d = new QGLShaderPrivate(type, context);
if (d->create() && !setSourceCodeFile(fileName)) {
if (d->shader)
glDeleteShader(d->shader);
d->shader = 0;
}
}
/*!
Deletes this shader. If the shader has been attached to a
QGLShaderProgram object, then the actual shader will stay around
until the QGLShaderProgram is destroyed.
*/
QGLShader::~QGLShader()
{
if (d->shader)
glDeleteShader(d->shader);
delete d;
}
/*!
Returns true if this shader is valid. Shaders become invalid
when they are destroyed and no longer attached to a QGLShaderProgram.
*/
bool QGLShader::isValid() const
{
if (d->isPartial && d->hasPartialSource)
return true;
if (!d->shader)
return false;
#if defined(QT_OPENGL_ES_2)
return glIsShader(d->shader);
#else
// glIsShader() may not exist on some systems.
return (!glIsShader || glIsShader(d->shader));
#endif
}
/*!
Returns the type of this shader.
*/
QGLShader::ShaderType QGLShader::shaderType() const
{
return d->shaderType;
}
// The precision qualifiers are useful on OpenGL/ES systems,
// but usually not present on desktop systems. Define the
// keywords to empty strings on desktop systems.
#ifndef QT_OPENGL_ES
#define QGL_DEFINE_QUALIFIERS 1
static const char qualifierDefines[] =
"#define lowp\n"
"#define mediump\n"
"#define highp\n";
#endif
/*!
Sets the \a source code for this shader and compiles it.
Returns true if the source was successfully compiled, false otherwise.
If shaderType() is PartialVertexShader or PartialFragmentShader,
then this function will always return true, even if the source code
is invalid. Partial shaders are compiled when QGLShaderProgram::link()
is called.
*/
bool QGLShader::setSourceCode(const char *source)
{
if (d->isPartial) {
d->partialSource = QByteArray(source);
d->hasPartialSource = true;
return d->compile();
} else if (d->shader) {
QVarLengthArray<const char *> src;
#ifdef QGL_DEFINE_QUALIFIERS
src.append(qualifierDefines);
#endif
src.append(source);
glShaderSource(d->shader, src.size(), src.data(), 0);
return d->compile();
} else {
return false;
}
}
/*!
\overload
Sets the \a source code for this shader and compiles it.
Returns true if the source was successfully compiled, false otherwise.
If shaderType() is PartialVertexShader or PartialFragmentShader,
then this function will always return true, even if the source code
is invalid. Partial shaders are compiled when QGLShaderProgram::link()
is called.
*/
bool QGLShader::setSourceCode(const QByteArray& source)
{
return setSourceCode(source.constData());
}
/*!
\overload
Sets the \a source code for this shader and compiles it.
Returns true if the source was successfully compiled, false otherwise.
If shaderType() is PartialVertexShader or PartialFragmentShader,
then this function will always return true, even if the source code
is invalid. Partial shaders are compiled when QGLShaderProgram::link()
is called.
*/
bool QGLShader::setSourceCode(const QString& source)
{
return setSourceCode(source.toLatin1().constData());
}
/*!
Sets the source code for this shader to the contents of \a fileName
and compiles it. Returns true if the file could be opened and the
source compiled, false otherwise.
If shaderType() is PartialVertexShader or PartialFragmentShader,
then this function will always return true, even if the source code
is invalid. Partial shaders are compiled when QGLShaderProgram::link()
is called.
*/
bool QGLShader::setSourceCodeFile(const QString& fileName)
{
if (!d->shader)
return false;
QFile file(fileName);
if (!file.open(QFile::ReadOnly)) {
qWarning() << "QGLShader: Unable to open file" << fileName;
return false;
}
QByteArray contents = file.readAll();
return setSourceCode(contents.constData());
}
/*!
Sets the binary code for this shader to the \a length bytes from
the array \a binary. The \a format specifies how the binary data
should be interpreted by the OpenGL engine. Returns true if the
binary was set on the shader; false otherwise.
This function cannot be used with PartialVertexShader or
PartialFragmentShader.
\sa shaderBinaryFormats()
*/
bool QGLShader::setBinaryCode(GLenum format, const void *binary, int length)
{
#if !defined(QT_OPENGL_ES_2)
if (!glShaderBinary)
return false;
#endif
if (d->isPartial || !d->shader)
return false;
glGetError(); // Clear error state.
glShaderBinary(1, &(d->shader), format, binary, length);
return (glGetError() == GL_NO_ERROR);
}
/*!
Sets the binary code for this shader to the \a length bytes from
the array \a binary. The \a format specifies how the binary data
should be interpreted by the OpenGL engine. Returns true if the
binary was set on the shader; false otherwise.
The \a otherShader will also have binary code set on it. This is
for the case where \a binary contains both vertex and fragment
shader code.
This function cannot be used with PartialVertexShader or
PartialFragmentShader.
\sa shaderBinaryFormats()
*/
bool QGLShader::setBinaryCode
(QGLShader& otherShader, GLenum format, const void *binary, int length)
{
#if !defined(QT_OPENGL_ES_2)
if (!glShaderBinary)
return false;
#endif
if (d->isPartial || !d->shader)
return false;
if (otherShader.d->isPartial || !otherShader.d->shader)
return false;
glGetError(); // Clear error state.
GLuint shaders[2];
shaders[0] = d->shader;
shaders[1] = otherShader.d->shader;
glShaderBinary(2, shaders, format, binary, length);
return (glGetError() == GL_NO_ERROR);
}
/*!
Returns a list of all binary formats that are supported by
setBinaryCode() on this system.
\sa setBinaryCode()
*/
QList<GLenum> QGLShader::shaderBinaryFormats()
{
GLint num;
QList<GLenum> list;
glGetError(); // Clear error state.
glGetIntegerv(GL_NUM_SHADER_BINARY_FORMATS, &num);
if (glGetError() != GL_NO_ERROR || num <= 0)
return list;
QVarLengthArray<GLint> formats(num);
glGetIntegerv(GL_SHADER_BINARY_FORMATS, formats.data());
for (GLint i = 0; i < num; ++i)
list += (GLenum)(formats[i]);
return list;
}
/*!
Returns the source code for this shader.
\sa setSourceCode()
*/
QByteArray QGLShader::sourceCode() const
{
if (d->isPartial)
return d->partialSource;
if (!d->shader)
return QByteArray();
GLint size = 0;
glGetShaderiv(d->shader, GL_SHADER_SOURCE_LENGTH, &size);
if (size <= 0)
return QByteArray();
GLint len = 0;
char *source = new char [size];
glGetShaderSource(d->shader, size, &len, source);
QByteArray src(source);
delete [] source;
return src;
}
/*!
Returns true if this shader has been compiled; false otherwise.
\sa setSourceCode()
*/
bool QGLShader::isCompiled() const
{
return d->compiled;
}
/*!
Returns the errors that occurred during the last compile.
\sa setSourceCode()
*/
QString QGLShader::errors() const
{
return d->errors;
}
/*!
Returns the OpenGL identifier associated with this shader.
If shaderType() is PartialVertexShader or PartialFragmentShader,
this function will always return zero. Partial shaders are
created and compiled when QGLShaderProgram::link() is called.
\sa QGLShaderProgram::programId()
*/
GLuint QGLShader::shaderId() const
{
return d->shader;
}
#undef ctx
#define ctx context
class QGLShaderProgramPrivate
{
public:
QGLShaderProgramPrivate(const QGLContext *ctx)
{
context = ctx;
program = 0;
linked = false;
inited = false;
hasPartialShaders = false;
vertexShader = 0;
fragmentShader = 0;
}
~QGLShaderProgramPrivate()
{
if (program)
glDeleteProgram(program);
}
const QGLContext *context;
GLuint program;
bool linked;
bool inited;
bool hasPartialShaders;
QString errors;
QList<QGLShader *> shaders;
QList<QGLShader *> anonShaders;
QGLShader *vertexShader;
QGLShader *fragmentShader;
};
#undef ctx
#define ctx d->context
/*!
Constructs a new shader program and attaches it to \a parent.
The program will be invalid until addShader() is called.
The shader program will be associated with the current QGLContext.
\sa isValid(), addShader()
*/
QGLShaderProgram::QGLShaderProgram(QObject *parent)
: QObject(parent)
{
d = new QGLShaderProgramPrivate(QGLContext::currentContext());
}
/*!
Constructs a new shader program and attaches it to \a parent.
The program will be invalid until addShader() is called.
The shader program will be associated with \a context.
\sa isValid(), addShader()
*/
QGLShaderProgram::QGLShaderProgram(const QGLContext *context, QObject *parent)
: QObject(parent)
{
d = new QGLShaderProgramPrivate(context);
}
/*!
Deletes this shader program.
*/
QGLShaderProgram::~QGLShaderProgram()
{
delete d;
}
bool QGLShaderProgram::init()
{
if (d->program || d->inited)
return true;
d->inited = true;
if (!d->context)
d->context = QGLContext::currentContext();
if (!d->context)
return false;
if (qt_resolve_glsl_extensions(const_cast<QGLContext *>(d->context))) {
d->program = glCreateProgram();
if (!(d->program)) {
qWarning() << "QGLShaderProgram: could not create shader program";
return false;
}
return true;
} else {
qWarning() << "QGLShaderProgram: shader programs are not supported";
return false;
}
}
/*!
Returns true if this shader program object is valid, false otherwise.
*/
bool QGLShaderProgram::isValid() const
{
if (!d->program)
return false;
#if defined(QT_OPENGL_ES_2)
return glIsProgram(d->program);
#else
// glIsProgram() may not exist on some systems.
return (!glIsProgram || glIsProgram(d->program));
#endif
}
/*!
Adds a compiled \a shader to this shader program. Returns true
if the shader could be added, or false otherwise.
Ownership of the \a shader object remains with the caller.
It will not be deleted when this QGLShaderProgram instance
is deleted. This allows the caller to add the same shader
to multiple shader programs.
\sa removeShader(), link(), removeAllShaders()
*/
bool QGLShaderProgram::addShader(QGLShader *shader)
{
if (!init())
return false;
if (d->shaders.contains(shader))
return true; // Already added to this shader program.
if (d->program && shader) {
if (!shader->d->compiled)
return false;
if (!shader->d->isPartial) {
if (!shader->d->shader)
return false;
glAttachShader(d->program, shader->d->shader);
} else {
d->hasPartialShaders = true;
}
d->linked = false; // Program needs to be relinked.
d->shaders.append(shader);
return true;
} else {
return false;
}
}
/*!
Compiles \a source as a shader of the specified \a type and
adds it to this shader program. Returns true if compilation
was successful, false otherwise. The compilation errors
will be made available via errors().
This function is intended to be a short-cut for quickly
adding vertex and fragment shaders to a shader program without
creating an instance of QGLShader first.
\sa removeShader(), link(), errors(), removeAllShaders()
*/
bool QGLShaderProgram::addShader(QGLShader::ShaderType type, const char *source)
{
if (!init())
return false;
QGLShader *shader = new QGLShader(type, this);
if (!shader->setSourceCode(source)) {
d->errors = shader->errors();
delete shader;
return false;
}
d->anonShaders.append(shader);
return addShader(shader);
}
/*!
\overload
Compiles \a source as a shader of the specified \a type and
adds it to this shader program. Returns true if compilation
was successful, false otherwise. The compilation errors
will be made available via errors().
This function is intended to be a short-cut for quickly
adding vertex and fragment shaders to a shader program without
creating an instance of QGLShader first.
\sa removeShader(), link(), errors(), removeAllShaders()
*/
bool QGLShaderProgram::addShader(QGLShader::ShaderType type, const QByteArray& source)
{
return addShader(type, source.constData());
}
/*!
\overload
Compiles \a source as a shader of the specified \a type and
adds it to this shader program. Returns true if compilation
was successful, false otherwise. The compilation errors
will be made available via errors().
This function is intended to be a short-cut for quickly
adding vertex and fragment shaders to a shader program without
creating an instance of QGLShader first.
\sa removeShader(), link(), errors(), removeAllShaders()
*/
bool QGLShaderProgram::addShader(QGLShader::ShaderType type, const QString& source)
{
return addShader(type, source.toLatin1().constData());
}
/*!
Removes \a shader from this shader program. The object is not deleted.
\sa addShader(), link(), removeAllShaders()
*/
void QGLShaderProgram::removeShader(QGLShader *shader)
{
if (d->program && shader && shader->d->shader) {
glDetachShader(d->program, shader->d->shader);
d->linked = false; // Program needs to be relinked.
}
d->shaders.removeAll(shader);
d->anonShaders.removeAll(shader);
}
/*!
Returns a list of all shaders that have been added to this shader
program using addShader().
\sa addShader(), removeShader()
*/
QList<QGLShader *> QGLShaderProgram::shaders() const
{
return d->shaders;
}
/*!
Removes all of the shaders that were added to this program previously.
The QGLShader objects for the shaders will not be deleted if they
were constructed externally. QGLShader objects that are constructed
internally by QGLShaderProgram will be deleted.
\sa addShader(), removeShader()
*/
void QGLShaderProgram::removeAllShaders()
{
foreach (QGLShader *shader, d->shaders) {
if (d->program && shader && shader->d->shader)
glDetachShader(d->program, shader->d->shader);
}
foreach (QGLShader *shader, d->anonShaders) {
// Delete shader objects that were created anonymously.
delete shader;
}
d->shaders.clear();
d->anonShaders.clear();
d->linked = false; // Program needs to be relinked.
}
#if defined(QT_OPENGL_ES_2)
#ifndef GL_PROGRAM_BINARY_LENGTH_OES
#define GL_PROGRAM_BINARY_LENGTH_OES 0x8741
#endif
#ifndef GL_NUM_PROGRAM_BINARY_FORMATS_OES
#define GL_NUM_PROGRAM_BINARY_FORMATS_OES 0x87FE
#endif
#ifndef GL_PROGRAM_BINARY_FORMATS_OES
#define GL_PROGRAM_BINARY_FORMATS_OES 0x87FF
#endif
#endif
/*!
Returns the program binary associated with this shader program.
The numeric identifier of the program binary format is returned
in \a format. The \c OES_get_program_binary extension will need
to be supported by the system for binary retrieval to succeed.
Returns an empty QByteArray if the program binary cannot be
retrieved on this system, or the shader program has not yet
been linked.
The returned binary can be supplied to setProgramBinary() on the
same machine at some future point to reload the program. It contains
the compiled code of all of the shaders that were attached to the
program at the time programBinary() is called.
\sa setProgramBinary(), programBinaryFormats()
*/
QByteArray QGLShaderProgram::programBinary(int *format) const
{
#if defined(QT_OPENGL_ES_2)
if (!isLinked())
return QByteArray();
// Get the length of the binary data, bailing out if there is none.
GLint length = 0;
glGetProgramiv(d->program, GL_PROGRAM_BINARY_LENGTH_OES, &length);
if (length <= 0)
return QByteArray();
// Retrieve the binary data.
QByteArray binary(length, 0);
GLenum binaryFormat;
glGetProgramBinaryOES(d->program, length, 0, &binaryFormat, binary.data());
if (format)
*format = (int)binaryFormat;
return binary;
#else
Q_UNUSED(format);
return QByteArray();
#endif
}
/*!
Sets the \a binary for this shader program according to \a format.
Returns true if the binary was set, or false if the binary format
is not supported or this system does not support program binaries.
The program will be linked if the load succeeds.
\sa programBinary(), programBinaryFormats(), isLinked()
*/
bool QGLShaderProgram::setProgramBinary(int format, const QByteArray& binary)
{
#if defined(QT_OPENGL_ES_2)
// Load the binary and check that it was linked correctly.
glProgramBinaryOES(d->program, (GLenum)format,
binary.constData(), binary.size());
GLint value = 0;
glGetProgramiv(d->program, GL_LINK_STATUS, &value);
d->linked = (value != 0);
value = 0;
glGetProgramiv(d->program, GL_INFO_LOG_LENGTH, &value);
d->errors = QString();
if (value > 1) {
char *log = new char [value];
GLint len;
glGetProgramInfoLog(d->program, value, &len, log);
d->errors = QString::fromLatin1(log);
qWarning() << "QGLShaderProgram::setProgramBinary:" << d->errors;
delete [] log;
}
return d->linked;
#else
Q_UNUSED(format);
Q_UNUSED(binary);
return false;
#endif
}
/*!
Returns the list of program binary formats that are accepted by
this system for use with setProgramBinary().
\sa programBinary, setProgramBinary()
*/
QList<int> QGLShaderProgram::programBinaryFormats()
{
#if defined(QT_OPENGL_ES_2)
GLint count = 0;
glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS_OES, &count);
if (count <= 0)
return QList<int>();
QVector<int> list;
list.resize(count);
glGetIntegerv(GL_PROGRAM_BINARY_FORMATS_OES, list.data());
return list.toList();
#else
return QList<int>();
#endif
}
/*!
Links together the shaders that were added to this program with
addShader(). Returns true if the link was successful or
false otherwise. If the link failed, the error messages can
be retrieved with errors().
Subclasses can override this function to initialize attributes
and uniform variables for use in specific shader programs.
If the shader program was already linked, calling this
function again will force it to be re-linked.
\sa addShader(), errors()
*/
bool QGLShaderProgram::link()
{
if (!d->program)
return false;
if (d->hasPartialShaders) {
// Compile the partial vertex and fragment shaders.
QByteArray vertexSource;
QByteArray fragmentSource;
foreach (QGLShader *shader, d->shaders) {
if (shader->shaderType() == QGLShader::PartialVertexShader)
vertexSource += shader->sourceCode();
else if (shader->shaderType() == QGLShader::PartialFragmentShader)
fragmentSource += shader->sourceCode();
}
if (vertexSource.isEmpty()) {
if (d->vertexShader) {
glDetachShader(d->program, d->vertexShader->d->shader);
delete d->vertexShader;
d->vertexShader = 0;
}
} else {
if (!d->vertexShader) {
d->vertexShader =
new QGLShader(QGLShader::VertexShader, this);
}
if (!d->vertexShader->setSourceCode(vertexSource)) {
d->errors = d->vertexShader->errors();
return false;
}
glAttachShader(d->program, d->vertexShader->d->shader);
}
if (fragmentSource.isEmpty()) {
if (d->fragmentShader) {
glDetachShader(d->program, d->fragmentShader->d->shader);
delete d->fragmentShader;
d->fragmentShader = 0;
}
} else {
if (!d->fragmentShader) {
d->fragmentShader =
new QGLShader(QGLShader::FragmentShader, this);
}
if (!d->fragmentShader->setSourceCode(fragmentSource)) {
d->errors = d->fragmentShader->errors();
return false;
}
glAttachShader(d->program, d->fragmentShader->d->shader);
}
}
glLinkProgram(d->program);
GLint value = 0;
glGetProgramiv(d->program, GL_LINK_STATUS, &value);
d->linked = (value != 0);
value = 0;
glGetProgramiv(d->program, GL_INFO_LOG_LENGTH, &value);
d->errors = QString();
if (value > 1) {
char *log = new char [value];
GLint len;
glGetProgramInfoLog(d->program, value, &len, log);
d->errors = QString::fromLatin1(log);
qWarning() << "QGLShaderProgram::link:" << d->errors;
delete [] log;
}
return d->linked;
}
/*!
Returns true if this shader program has been linked; false otherwise.
\sa link()
*/
bool QGLShaderProgram::isLinked() const
{
return d->linked;
}
/*!
Returns the errors that occurred during the last link()
or addShader() with explicitly specified source code.
\sa link()
*/
QString QGLShaderProgram::errors() const
{
return d->errors;
}
/*!
Enable use of this shader program in the currently active QGLContext.
Returns true if the program was successfully enabled; false
otherwise. If the shader program has not yet been linked,
or it needs to be re-linked, this function will call link().
\sa link(), disable()
*/
bool QGLShaderProgram::enable()
{
if (!d->program)
return false;
if (!d->linked && !link())
return false;
glUseProgram(d->program);
return true;
}
/*!
Disables this shader program in the currently active QGLContext.
This is equivalent to calling \c{glUseProgram(0)}.
\sa enable()
*/
void QGLShaderProgram::disable()
{
#if defined(QT_OPENGL_ES_2)
glUseProgram(0);
#else
if (glUseProgram)
glUseProgram(0);
#endif
}
/*!
Returns the OpenGL identifier associated with this shader program.
\sa QGLShader::shaderId()
*/
GLuint QGLShaderProgram::programId() const
{
return d->program;
}
/*!
Binds the attribute \a name to the specified \a location. This
function can be called before or after the program has been linked.
Any attributes that have not been explicitly bound when the program
is linked will be assigned locations automatically.
\sa attributeLocation()
*/
void QGLShaderProgram::bindAttributeLocation(const char *name, int location)
{
glBindAttribLocation(d->program, location, name);
}
/*!
\overload
Binds the attribute \a name to the specified \a location. This
function can be called before or after the program has been linked.
Any attributes that have not been explicitly bound when the program
is linked will be assigned locations automatically.
\sa attributeLocation()
*/
void QGLShaderProgram::bindAttributeLocation(const QByteArray& name, int location)
{
glBindAttribLocation(d->program, location, name.constData());
}
/*!
\overload
Binds the attribute \a name to the specified \a location. This
function can be called before or after the program has been linked.
Any attributes that have not been explicitly bound when the program
is linked will be assigned locations automatically.
\sa attributeLocation()
*/
void QGLShaderProgram::bindAttributeLocation(const QString& name, int location)
{
glBindAttribLocation(d->program, location, name.toLatin1().constData());
}
/*!
Returns the location of the attribute \a name within this shader
program's parameter list. Returns -1 if \a name is not a valid
attribute for this shader program.
\sa uniformLocation(), bindAttributeLocation()
*/
int QGLShaderProgram::attributeLocation(const char *name) const
{
if (d->linked) {
return glGetAttribLocation(d->program, name);
} else {
qWarning() << "QGLShaderProgram::attributeLocation(" << name
<< "): shader program is not linked";
return -1;
}
}
/*!
\overload
Returns the location of the attribute \a name within this shader
program's parameter list. Returns -1 if \a name is not a valid
attribute for this shader program.
\sa uniformLocation(), bindAttributeLocation()
*/
int QGLShaderProgram::attributeLocation(const QByteArray& name) const
{
return attributeLocation(name.constData());
}
/*!
\overload
Returns the location of the attribute \a name within this shader
program's parameter list. Returns -1 if \a name is not a valid
attribute for this shader program.
\sa uniformLocation(), bindAttributeLocation()
*/
int QGLShaderProgram::attributeLocation(const QString& name) const
{
return attributeLocation(name.toLatin1().constData());
}
/*!
Sets the attribute at \a location in the current context to \a value.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(int location, GLfloat value)
{
if (location != -1)
glVertexAttrib1fv(location, &value);
}
/*!
\overload
Sets the attribute called \a name in the current context to \a value.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(const char *name, GLfloat value)
{
setAttributeValue(attributeLocation(name), value);
}
/*!
Sets the attribute at \a location in the current context to
the 2D vector (\a x, \a y).
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(int location, GLfloat x, GLfloat y)
{
if (location != -1) {
GLfloat values[2] = {x, y};
glVertexAttrib2fv(location, values);
}
}
/*!
\overload
Sets the attribute called \a name in the current context to
the 2D vector (\a x, \a y).
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(const char *name, GLfloat x, GLfloat y)
{
setAttributeValue(attributeLocation(name), x, y);
}
/*!
Sets the attribute at \a location in the current context to
the 3D vector (\a x, \a y, \a z).
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue
(int location, GLfloat x, GLfloat y, GLfloat z)
{
if (location != -1) {
GLfloat values[3] = {x, y, z};
glVertexAttrib3fv(location, values);
}
}
/*!
\overload
Sets the attribute called \a name in the current context to
the 3D vector (\a x, \a y, \a z).
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue
(const char *name, GLfloat x, GLfloat y, GLfloat z)
{
setAttributeValue(attributeLocation(name), x, y, z);
}
/*!
Sets the attribute at \a location in the current context to
the 4D vector (\a x, \a y, \a z, \a w).
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue
(int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
{
if (location != -1) {
GLfloat values[4] = {x, y, z, w};
glVertexAttrib4fv(location, values);
}
}
/*!
\overload
Sets the attribute called \a name in the current context to
the 4D vector (\a x, \a y, \a z, \a w).
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue
(const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
{
setAttributeValue(attributeLocation(name), x, y, z, w);
}
/*!
Sets the attribute at \a location in the current context to \a value.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(int location, const QVector2D& value)
{
if (location != -1)
glVertexAttrib2fv(location, reinterpret_cast<const GLfloat *>(&value));
}
/*!
\overload
Sets the attribute called \a name in the current context to \a value.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(const char *name, const QVector2D& value)
{
setAttributeValue(attributeLocation(name), value);
}
/*!
Sets the attribute at \a location in the current context to \a value.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(int location, const QVector3D& value)
{
if (location != -1)
glVertexAttrib3fv(location, reinterpret_cast<const GLfloat *>(&value));
}
/*!
\overload
Sets the attribute called \a name in the current context to \a value.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(const char *name, const QVector3D& value)
{
setAttributeValue(attributeLocation(name), value);
}
/*!
Sets the attribute at \a location in the current context to \a value.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(int location, const QVector4D& value)
{
if (location != -1)
glVertexAttrib4fv(location, reinterpret_cast<const GLfloat *>(&value));
}
/*!
\overload
Sets the attribute called \a name in the current context to \a value.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(const char *name, const QVector4D& value)
{
setAttributeValue(attributeLocation(name), value);
}
/*!
Sets the attribute at \a location in the current context to \a value.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(int location, const QColor& value)
{
if (location != -1) {
GLfloat values[4] = {value.redF(), value.greenF(), value.blueF(), value.alphaF()};
glVertexAttrib4fv(location, values);
}
}
/*!
\overload
Sets the attribute called \a name in the current context to \a value.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue(const char *name, const QColor& value)
{
setAttributeValue(attributeLocation(name), value);
}
/*!
Sets the attribute at \a location in the current context to the
contents of \a values, which contains \a columns elements, each
consisting of \a rows elements. The \a rows value should be
1, 2, 3, or 4. This function is typically used to set matrix
values and column vectors.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue
(int location, const GLfloat *values, int columns, int rows)
{
if (rows < 1 || rows > 4) {
qWarning() << "QGLShaderProgram::setAttributeValue: rows" << rows << "not supported";
return;
}
if (location != -1) {
while (columns-- > 0) {
if (rows == 1)
glVertexAttrib1fv(location, values);
else if (rows == 2)
glVertexAttrib2fv(location, values);
else if (rows == 3)
glVertexAttrib3fv(location, values);
else
glVertexAttrib4fv(location, values);
values += rows;
++location;
}
}
}
/*!
\overload
Sets the attribute called \a name in the current context to the
contents of \a values, which contains \a columns elements, each
consisting of \a rows elements. The \a rows value should be
1, 2, 3, or 4. This function is typically used to set matrix
values and column vectors.
\sa setUniformValue()
*/
void QGLShaderProgram::setAttributeValue
(const char *name, const GLfloat *values, int columns, int rows)
{
setAttributeValue(attributeLocation(name), values, columns, rows);
}
/*!
Sets an array of vertex \a values on the attribute at \a location
in this shader program. The \a size indicates the number of
components per vertex (1, 2, 3, or 4), and the \a stride indicates
the number of bytes between vertices. A default \a stride value
of zero indicates that the vertices are densely packed in \a values.
\sa setAttributeValue(), setUniformValue(), disableAttributeArray()
*/
void QGLShaderProgram::setAttributeArray
(int location, const GLfloat *values, int size, int stride)
{
if (location != -1) {
glVertexAttribPointer(location, size, GL_FLOAT, GL_FALSE,
stride, values);
glEnableVertexAttribArray(location);
}
}
/*!
Sets an array of 2D vertex \a values on the attribute at \a location
in this shader program. The \a stride indicates the number of bytes
between vertices. A default \a stride value of zero indicates that
the vertices are densely packed in \a values.
\sa setAttributeValue(), setUniformValue(), disableAttributeArray()
*/
void QGLShaderProgram::setAttributeArray
(int location, const QVector2D *values, int stride)
{
if (location != -1) {
glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE,
stride, values);
glEnableVertexAttribArray(location);
}
}
/*!
Sets an array of 3D vertex \a values on the attribute at \a location
in this shader program. The \a stride indicates the number of bytes
between vertices. A default \a stride value of zero indicates that
the vertices are densely packed in \a values.
\sa setAttributeValue(), setUniformValue(), disableAttributeArray()
*/
void QGLShaderProgram::setAttributeArray
(int location, const QVector3D *values, int stride)
{
if (location != -1) {
glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE,
stride, values);
glEnableVertexAttribArray(location);
}
}
/*!
Sets an array of 4D vertex \a values on the attribute at \a location
in this shader program. The \a stride indicates the number of bytes
between vertices. A default \a stride value of zero indicates that
the vertices are densely packed in \a values.
\sa setAttributeValue(), setUniformValue(), disableAttributeArray()
*/
void QGLShaderProgram::setAttributeArray
(int location, const QVector4D *values, int stride)
{
if (location != -1) {
glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE,
stride, values);
glEnableVertexAttribArray(location);
}
}
/*!
\overload
Sets an array of vertex \a values on the attribute called \a name
in this shader program. The \a size indicates the number of
components per vertex (1, 2, 3, or 4), and the \a stride indicates
the number of bytes between vertices. A default \a stride value
of zero indicates that the vertices are densely packed in \a values.
\sa setAttributeValue(), setUniformValue(), disableAttributeArray()
*/
void QGLShaderProgram::setAttributeArray
(const char *name, const GLfloat *values, int size, int stride)
{
setAttributeArray(attributeLocation(name), values, size, stride);
}
/*!
\overload
Sets an array of 2D vertex \a values on the attribute called \a name
in this shader program. The \a stride indicates the number of bytes
between vertices. A default \a stride value of zero indicates that
the vertices are densely packed in \a values.
\sa setAttributeValue(), setUniformValue(), disableAttributeArray()
*/
void QGLShaderProgram::setAttributeArray
(const char *name, const QVector2D *values, int stride)
{
setAttributeArray(attributeLocation(name), values, stride);
}
/*!
\overload
Sets an array of 3D vertex \a values on the attribute called \a name
in this shader program. The \a stride indicates the number of bytes
between vertices. A default \a stride value of zero indicates that
the vertices are densely packed in \a values.
\sa setAttributeValue(), setUniformValue(), disableAttributeArray()
*/
void QGLShaderProgram::setAttributeArray
(const char *name, const QVector3D *values, int stride)
{
setAttributeArray(attributeLocation(name), values, stride);
}
/*!
\overload
Sets an array of 4D vertex \a values on the attribute called \a name
in this shader program. The \a stride indicates the number of bytes
between vertices. A default \a stride value of zero indicates that
the vertices are densely packed in \a values.
\sa setAttributeValue(), setUniformValue(), disableAttributeArray()
*/
void QGLShaderProgram::setAttributeArray
(const char *name, const QVector4D *values, int stride)
{
setAttributeArray(attributeLocation(name), values, stride);
}
/*!
Disables the vertex array at \a location in this shader program
that was enabled by a previous call to setAttributeArray().
\sa setAttributeArray(), setAttributeValue(), setUniformValue()
*/
void QGLShaderProgram::disableAttributeArray(int location)
{
if (location != -1)
glDisableVertexAttribArray(location);
}
/*!
\overload
Disables the vertex array called \a name in this shader program
that was enabled by a previous call to setAttributeArray().
\sa setAttributeArray(), setAttributeValue(), setUniformValue()
*/
void QGLShaderProgram::disableAttributeArray(const char *name)
{
disableAttributeArray(attributeLocation(name));
}
/*!
Returns the location of the uniform variable \a name within this shader
program's parameter list. Returns -1 if \a name is not a valid
uniform variable for this shader program.
\sa attributeLocation()
*/
int QGLShaderProgram::uniformLocation(const char *name) const
{
if (d->linked) {
return glGetUniformLocation(d->program, name);
} else {
qWarning() << "QGLShaderProgram::uniformLocation(" << name
<< "): shader program is not linked";
return -1;
}
}
/*!
\overload
Returns the location of the uniform variable \a name within this shader
program's parameter list. Returns -1 if \a name is not a valid
uniform variable for this shader program.
\sa attributeLocation()
*/
int QGLShaderProgram::uniformLocation(const QByteArray& name) const
{
return uniformLocation(name.constData());
}
/*!
\overload
Returns the location of the uniform variable \a name within this shader
program's parameter list. Returns -1 if \a name is not a valid
uniform variable for this shader program.
\sa attributeLocation()
*/
int QGLShaderProgram::uniformLocation(const QString& name) const
{
return uniformLocation(name.toLatin1().constData());
}
/*!
Sets the uniform variable at \a location in the current context to \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, GLfloat value)
{
if (location != -1)
glUniform1fv(location, 1, &value);
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, GLfloat value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context to \a value.
This function must be used when setting sampler values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, GLint value)
{
if (location != -1)
glUniform1i(location, value);
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to \a value. This function must be used when setting sampler values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, GLint value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context to
the 2D vector (\a x, \a y).
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, GLfloat x, GLfloat y)
{
if (location != -1) {
GLfloat values[2] = {x, y};
glUniform2fv(location, 1, values);
}
}
/*!
\overload
Sets the uniform variable called \a name in the current context to
the 2D vector (\a x, \a y).
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, GLfloat x, GLfloat y)
{
setUniformValue(uniformLocation(name), x, y);
}
/*!
Sets the uniform variable at \a location in the current context to
the 3D vector (\a x, \a y, \a z).
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue
(int location, GLfloat x, GLfloat y, GLfloat z)
{
if (location != -1) {
GLfloat values[3] = {x, y, z};
glUniform3fv(location, 1, values);
}
}
/*!
\overload
Sets the uniform variable called \a name in the current context to
the 3D vector (\a x, \a y, \a z).
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue
(const char *name, GLfloat x, GLfloat y, GLfloat z)
{
setUniformValue(uniformLocation(name), x, y, z);
}
/*!
Sets the uniform variable at \a location in the current context to
the 4D vector (\a x, \a y, \a z, \a w).
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue
(int location, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
{
if (location != -1) {
GLfloat values[4] = {x, y, z, w};
glUniform4fv(location, 1, values);
}
}
/*!
\overload
Sets the uniform variable called \a name in the current context to
the 4D vector (\a x, \a y, \a z, \a w).
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue
(const char *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w)
{
setUniformValue(uniformLocation(name), x, y, z, w);
}
/*!
Sets the uniform variable at \a location in the current context to \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QVector2D& value)
{
if (location != -1)
glUniform2fv(location, 1, reinterpret_cast<const GLfloat *>(&value));
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QVector2D& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context to \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QVector3D& value)
{
if (location != -1)
glUniform3fv(location, 1, reinterpret_cast<const GLfloat *>(&value));
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QVector3D& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context to \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QVector4D& value)
{
if (location != -1)
glUniform4fv(location, 1, reinterpret_cast<const GLfloat *>(&value));
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QVector4D& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context to
the red, green, blue, and alpha components of \a color.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QColor& color)
{
if (location != -1) {
GLfloat values[4] = {color.redF(), color.greenF(), color.blueF(), color.alphaF()};
glUniform4fv(location, 1, values);
}
}
/*!
\overload
Sets the uniform variable called \a name in the current context to
the red, green, blue, and alpha components of \a color.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QColor& color)
{
setUniformValue(uniformLocation(name), color);
}
/*!
Sets the uniform variable at \a location in the current context
to a 2x2 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QMatrix2x2& value)
{
if (location != -1)
glUniformMatrix2fv(location, 1, GL_FALSE, value.data());
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to a 2x2 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QMatrix2x2& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context
to a 2x3 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QMatrix2x3& value)
{
#if !defined(QT_OPENGL_ES_2)
if (location != -1) {
if (glUniformMatrix2x3fv) {
// OpenGL 2.1+: pass the matrix directly.
glUniformMatrix2x3fv(location, 1, GL_FALSE, value.data());
} else {
// OpenGL 2.0: pass the matrix columns as a vector.
glUniform3fv(location, 2, value.data());
}
}
#else
if (location != -1)
glUniform3fv(location, 2, value.data());
#endif
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to a 2x3 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QMatrix2x3& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context
to a 2x4 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QMatrix2x4& value)
{
#if !defined(QT_OPENGL_ES_2)
if (location != -1) {
if (glUniformMatrix2x4fv) {
// OpenGL 2.1+: pass the matrix directly.
glUniformMatrix2x4fv(location, 1, GL_FALSE, value.data());
} else {
// OpenGL 2.0: pass the matrix columns as a vector.
glUniform4fv(location, 2, value.data());
}
}
#else
if (location != -1)
glUniform4fv(location, 2, value.data());
#endif
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to a 2x4 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QMatrix2x4& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context
to a 3x2 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QMatrix3x2& value)
{
#if !defined(QT_OPENGL_ES_2)
if (location != -1) {
if (glUniformMatrix3x2fv) {
// OpenGL 2.1+: pass the matrix directly.
glUniformMatrix3x2fv(location, 1, GL_FALSE, value.data());
} else {
// OpenGL 2.0: pass the matrix columns as a vector.
glUniform2fv(location, 3, value.data());
}
}
#else
if (location != -1)
glUniform2fv(location, 3, value.data());
#endif
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to a 3x2 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QMatrix3x2& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context
to a 3x3 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QMatrix3x3& value)
{
if (location != -1)
glUniformMatrix3fv(location, 1, GL_FALSE, value.data());
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to a 3x3 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QMatrix3x3& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context
to a 3x4 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QMatrix3x4& value)
{
#if !defined(QT_OPENGL_ES_2)
if (location != -1) {
if (glUniformMatrix3x4fv) {
// OpenGL 2.1+: pass the matrix directly.
glUniformMatrix3x4fv(location, 1, GL_FALSE, value.data());
} else {
// OpenGL 2.0: pass the matrix columns as a vector.
glUniform4fv(location, 3, value.data());
}
}
#else
if (location != -1)
glUniform4fv(location, 3, value.data());
#endif
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to a 3x4 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QMatrix3x4& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context
to a 4x2 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QMatrix4x2& value)
{
#if !defined(QT_OPENGL_ES_2)
if (location != -1) {
if (glUniformMatrix4x2fv) {
// OpenGL 2.1+: pass the matrix directly.
glUniformMatrix4x2fv(location, 1, GL_FALSE, value.data());
} else {
// OpenGL 2.0: pass the matrix columns as a vector.
glUniform2fv(location, 4, value.data());
}
}
#else
if (location != -1)
glUniform2fv(location, 4, value.data());
#endif
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to a 4x2 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QMatrix4x2& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context
to a 4x3 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QMatrix4x3& value)
{
#if !defined(QT_OPENGL_ES_2)
if (location != -1) {
if (glUniformMatrix4x3fv) {
// OpenGL 2.1+: pass the matrix directly.
glUniformMatrix4x3fv(location, 1, GL_FALSE, value.data());
} else {
// OpenGL 2.0: pass the matrix columns as a vector.
glUniform3fv(location, 4, value.data());
}
}
#else
if (location != -1)
glUniform3fv(location, 4, value.data());
#endif
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to a 4x3 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QMatrix4x3& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context
to a 4x4 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const QMatrix4x4& value)
{
if (location != -1)
glUniformMatrix4fv(location, 1, GL_FALSE, value.data());
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to a 4x4 matrix \a value.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const QMatrix4x4& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
\overload
Sets the uniform variable at \a location in the current context
to a 4x4 matrix \a value. The matrix elements must be specified
in column-major order.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(int location, const GLfloat value[4][4])
{
if (location != -1)
glUniformMatrix4fv(location, 1, GL_FALSE, value[0]);
}
/*!
\overload
Sets the uniform variable called \a name in the current context
to a 4x4 matrix \a value. The matrix elements must be specified
in column-major order.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValue(const char *name, const GLfloat value[4][4])
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable at \a location in the current context to a
3x3 transformation matrix \a value that is specified as a QTransform value.
To set a QTransform value as a 4x4 matrix in a shader, use
\c{setUniformValue(location, QMatrix4x4(value))}.
*/
void QGLShaderProgram::setUniformValue(int location, const QTransform& value)
{
if (location != -1) {
GLfloat mat[3][3] = {
{value.m11(), value.m12(), value.m13()},
{value.m21(), value.m22(), value.m23()},
{value.m31(), value.m32(), value.m33()}
};
glUniformMatrix3fv(location, 1, GL_FALSE, mat[0]);
}
}
/*!
\overload
Sets the uniform variable called \a name in the current context to a
3x3 transformation matrix \a value that is specified as a QTransform value.
To set a QTransform value as a 4x4 matrix in a shader, use
\c{setUniformValue(name, QMatrix4x4(value))}.
*/
void QGLShaderProgram::setUniformValue
(const char *name, const QTransform& value)
{
setUniformValue(uniformLocation(name), value);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count elements of \a values. This overload
must be used when setting an array of sampler values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const GLint *values, int count)
{
if (location != -1)
glUniform1iv(location, count, values);
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count elements of \a values. This overload
must be used when setting an array of sampler values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray
(const char *name, const GLint *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count elements of \a values. Each element
has \a size components. The \a size must be 1, 2, 3, or 4.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const GLfloat *values, int count, int size)
{
if (location != -1) {
if (size == 1)
glUniform1fv(location, count, values);
else if (size == 2)
glUniform2fv(location, count, values);
else if (size == 3)
glUniform3fv(location, count, values);
else if (size == 4)
glUniform4fv(location, count, values);
else
qWarning() << "QGLShaderProgram::setUniformValue: size" << size << "not supported";
}
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count elements of \a values. Each element
has \a size components. The \a size must be 1, 2, 3, or 4.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray
(const char *name, const GLfloat *values, int count, int size)
{
setUniformValueArray(uniformLocation(name), values, count, size);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 2D vector elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QVector2D *values, int count)
{
if (location != -1)
glUniform2fv(location, count, reinterpret_cast<const GLfloat *>(values));
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 2D vector elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QVector2D *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 3D vector elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QVector3D *values, int count)
{
if (location != -1)
glUniform3fv(location, count, reinterpret_cast<const GLfloat *>(values));
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 3D vector elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QVector3D *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 4D vector elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QVector4D *values, int count)
{
if (location != -1)
glUniform4fv(location, count, reinterpret_cast<const GLfloat *>(values));
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 4D vector elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QVector4D *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
// We may have to repack matrix arrays if the matrix types
// contain additional flag bits. Especially QMatrix4x4.
#define setUniformMatrixArray(func,location,values,count,type,cols,rows) \
if (location == -1 || count <= 0) \
return; \
if (count == 1 || sizeof(type) == cols * rows * sizeof(GLfloat)) { \
func(location, count, GL_FALSE, values->constData()); \
} else { \
QVarLengthArray<GLfloat> temp(cols * rows * count); \
for (int index = 0; index < count; ++index) { \
qMemCopy(temp.data() + cols * rows * index, \
values[index].constData(), cols * rows * sizeof(GLfloat)); \
} \
func(location, count, GL_FALSE, temp.constData()); \
}
#if !defined(QT_OPENGL_ES_2)
#define setUniformGenericMatrixArray(func,colfunc,location,values,count,type,cols,rows) \
if (location == -1 || count <= 0) \
return; \
if (count == 1 || sizeof(type) == cols * rows * sizeof(GLfloat)) { \
if (func) \
func(location, count, GL_FALSE, values->constData()); \
else \
colfunc(location, cols * count, values->constData()); \
} else { \
QVarLengthArray<GLfloat> temp(cols * rows * count); \
for (int index = 0; index < count; ++index) { \
qMemCopy(temp.data() + cols * rows * index, \
values[index].constData(), cols * rows * sizeof(GLfloat)); \
} \
if (func) \
func(location, count, GL_FALSE, temp.constData()); \
else \
colfunc(location, count * cols, temp.constData()); \
}
#else
#define setUniformGenericMatrixArray(func,colfunc,location,values,count,type,cols,rows) \
if (location == -1 || count <= 0) \
return; \
if (count == 1 || sizeof(type) == cols * rows * sizeof(GLfloat)) { \
colfunc(location, cols * count, values->constData()); \
} else { \
QVarLengthArray<GLfloat> temp(cols * rows * count); \
for (int index = 0; index < count; ++index) { \
qMemCopy(temp.data() + cols * rows * index, \
values[index].constData(), cols * rows * sizeof(GLfloat)); \
} \
colfunc(location, count * cols, temp.constData()); \
}
#endif
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 2x2 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QMatrix2x2 *values, int count)
{
setUniformMatrixArray
(glUniformMatrix2fv, location, values, count, QMatrix2x2, 2, 2);
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 2x2 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix2x2 *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 2x3 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QMatrix2x3 *values, int count)
{
setUniformGenericMatrixArray
(glUniformMatrix2x3fv, glUniform3fv, location, values, count,
QMatrix2x3, 2, 3);
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 2x3 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix2x3 *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 2x4 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QMatrix2x4 *values, int count)
{
setUniformGenericMatrixArray
(glUniformMatrix2x4fv, glUniform4fv, location, values, count,
QMatrix2x4, 2, 4);
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 2x4 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix2x4 *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 3x2 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QMatrix3x2 *values, int count)
{
setUniformGenericMatrixArray
(glUniformMatrix3x2fv, glUniform2fv, location, values, count,
QMatrix3x2, 3, 2);
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 3x2 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix3x2 *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 3x3 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QMatrix3x3 *values, int count)
{
setUniformMatrixArray
(glUniformMatrix3fv, location, values, count, QMatrix3x3, 3, 3);
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 3x3 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix3x3 *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 3x4 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QMatrix3x4 *values, int count)
{
setUniformGenericMatrixArray
(glUniformMatrix3x4fv, glUniform4fv, location, values, count,
QMatrix3x4, 3, 4);
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 3x4 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix3x4 *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 4x2 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QMatrix4x2 *values, int count)
{
setUniformGenericMatrixArray
(glUniformMatrix4x2fv, glUniform2fv, location, values, count,
QMatrix4x2, 4, 2);
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 4x2 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix4x2 *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 4x3 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QMatrix4x3 *values, int count)
{
setUniformGenericMatrixArray
(glUniformMatrix4x3fv, glUniform3fv, location, values, count,
QMatrix4x3, 4, 3);
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 4x3 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix4x3 *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Sets the uniform variable array at \a location in the current
context to the \a count 4x4 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(int location, const QMatrix4x4 *values, int count)
{
setUniformMatrixArray
(glUniformMatrix4fv, location, values, count, QMatrix4x4, 4, 4);
}
/*!
\overload
Sets the uniform variable array called \a name in the current
context to the \a count 4x4 matrix elements of \a values.
\sa setAttributeValue()
*/
void QGLShaderProgram::setUniformValueArray(const char *name, const QMatrix4x4 *values, int count)
{
setUniformValueArray(uniformLocation(name), values, count);
}
/*!
Returns true if shader programs written in the OpenGL Shading
Language (GLSL) are supported on this system; false otherwise.
The \a context is used to resolve the GLSL extensions.
If \a context is null, then QGLContext::currentContext() is used.
*/
bool QGLShaderProgram::hasShaderPrograms(const QGLContext *context)
{
#if !defined(QT_OPENGL_ES_2)
if (!context)
context = QGLContext::currentContext();
if (!context)
return false;
return qt_resolve_glsl_extensions(const_cast<QGLContext *>(context));
#else
Q_UNUSED(context);
return true;
#endif
}
#endif
QT_END_NAMESPACE
|