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
|
/*
* Mesa 3-D graphics library
*
* Copyright (C) 2004-2008 Brian Paul All Rights Reserved.
* Copyright (C) 2009-2010 VMware, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* \file shaderapi.c
* \author Brian Paul
*
* Implementation of GLSL-related API functions.
* The glUniform* functions are in uniforms.c
*
*
* XXX things to do:
* 1. Check that the right error code is generated for all _mesa_error() calls.
* 2. Insert FLUSH_VERTICES calls in various places
*/
#include <stdbool.h>
#include "main/glheader.h"
#include "main/context.h"
#include "main/dispatch.h"
#include "main/enums.h"
#include "main/hash.h"
#include "main/mtypes.h"
#include "main/pipelineobj.h"
#include "main/shaderapi.h"
#include "main/shaderobj.h"
#include "main/transformfeedback.h"
#include "main/uniforms.h"
#include "compiler/glsl/glsl_parser_extras.h"
#include "compiler/glsl/ir.h"
#include "compiler/glsl/ir_uniform.h"
#include "compiler/glsl/program.h"
#include "program/program.h"
#include "program/prog_print.h"
#include "program/prog_parameter.h"
#include "util/ralloc.h"
#include "util/hash_table.h"
#include "util/mesa-sha1.h"
#ifdef _MSC_VER
#include <stdlib.h>
#define PATH_MAX _MAX_PATH
#endif
/**
* Return mask of GLSL_x flags by examining the MESA_GLSL env var.
*/
GLbitfield
_mesa_get_shader_flags(void)
{
GLbitfield flags = 0x0;
const char *env = getenv("MESA_GLSL");
if (env) {
if (strstr(env, "dump_on_error"))
flags |= GLSL_DUMP_ON_ERROR;
else if (strstr(env, "dump"))
flags |= GLSL_DUMP;
if (strstr(env, "log"))
flags |= GLSL_LOG;
if (strstr(env, "nopvert"))
flags |= GLSL_NOP_VERT;
if (strstr(env, "nopfrag"))
flags |= GLSL_NOP_FRAG;
if (strstr(env, "nopt"))
flags |= GLSL_NO_OPT;
else if (strstr(env, "opt"))
flags |= GLSL_OPT;
if (strstr(env, "uniform"))
flags |= GLSL_UNIFORMS;
if (strstr(env, "useprog"))
flags |= GLSL_USE_PROG;
if (strstr(env, "errors"))
flags |= GLSL_REPORT_ERRORS;
}
return flags;
}
/**
* Memoized version of getenv("MESA_SHADER_CAPTURE_PATH").
*/
const char *
_mesa_get_shader_capture_path(void)
{
static bool read_env_var = false;
static const char *path = NULL;
if (!read_env_var) {
path = getenv("MESA_SHADER_CAPTURE_PATH");
read_env_var = true;
if (path &&
strlen(path) > PATH_MAX - strlen("/fp-4294967295.shader_test")) {
GET_CURRENT_CONTEXT(ctx);
_mesa_warning(ctx, "MESA_SHADER_CAPTURE_PATH too long; ignoring "
"request to capture shaders");
path = NULL;
}
}
return path;
}
/**
* Initialize context's shader state.
*/
void
_mesa_init_shader_state(struct gl_context *ctx)
{
/* Device drivers may override these to control what kind of instructions
* are generated by the GLSL compiler.
*/
struct gl_shader_compiler_options options;
gl_shader_stage sh;
int i;
memset(&options, 0, sizeof(options));
options.MaxUnrollIterations = 32;
options.MaxIfDepth = UINT_MAX;
for (sh = 0; sh < MESA_SHADER_STAGES; ++sh)
memcpy(&ctx->Const.ShaderCompilerOptions[sh], &options, sizeof(options));
ctx->Shader.Flags = _mesa_get_shader_flags();
if (ctx->Shader.Flags != 0)
ctx->Const.GenerateTemporaryNames = true;
/* Extended for ARB_separate_shader_objects */
ctx->Shader.RefCount = 1;
mtx_init(&ctx->Shader.Mutex, mtx_plain);
ctx->TessCtrlProgram.patch_vertices = 3;
for (i = 0; i < 4; ++i)
ctx->TessCtrlProgram.patch_default_outer_level[i] = 1.0;
for (i = 0; i < 2; ++i)
ctx->TessCtrlProgram.patch_default_inner_level[i] = 1.0;
}
/**
* Free the per-context shader-related state.
*/
void
_mesa_free_shader_state(struct gl_context *ctx)
{
int i;
for (i = 0; i < MESA_SHADER_STAGES; i++) {
_mesa_reference_shader_program(ctx, &ctx->Shader.CurrentProgram[i],
NULL);
}
_mesa_reference_shader_program(ctx, &ctx->Shader._CurrentFragmentProgram,
NULL);
_mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, NULL);
/* Extended for ARB_separate_shader_objects */
_mesa_reference_pipeline_object(ctx, &ctx->_Shader, NULL);
assert(ctx->Shader.RefCount == 1);
mtx_destroy(&ctx->Shader.Mutex);
}
/**
* Copy string from <src> to <dst>, up to maxLength characters, returning
* length of <dst> in <length>.
* \param src the strings source
* \param maxLength max chars to copy
* \param length returns number of chars copied
* \param dst the string destination
*/
void
_mesa_copy_string(GLchar *dst, GLsizei maxLength,
GLsizei *length, const GLchar *src)
{
GLsizei len;
for (len = 0; len < maxLength - 1 && src && src[len]; len++)
dst[len] = src[len];
if (maxLength > 0)
dst[len] = 0;
if (length)
*length = len;
}
/**
* Confirm that the a shader type is valid and supported by the implementation
*
* \param ctx Current GL context
* \param type Shader target
*
*/
bool
_mesa_validate_shader_target(const struct gl_context *ctx, GLenum type)
{
/* Note: when building built-in GLSL functions, this function may be
* invoked with ctx == NULL. In that case, we can only validate that it's
* a shader target we recognize, not that it's supported in the current
* context. But that's fine--we don't need any further validation than
* that when building built-in GLSL functions.
*/
switch (type) {
case GL_FRAGMENT_SHADER:
return ctx == NULL || ctx->Extensions.ARB_fragment_shader;
case GL_VERTEX_SHADER:
return ctx == NULL || ctx->Extensions.ARB_vertex_shader;
case GL_GEOMETRY_SHADER_ARB:
return ctx == NULL || _mesa_has_geometry_shaders(ctx);
case GL_TESS_CONTROL_SHADER:
case GL_TESS_EVALUATION_SHADER:
return ctx == NULL || _mesa_has_tessellation(ctx);
case GL_COMPUTE_SHADER:
return ctx == NULL || _mesa_has_compute_shaders(ctx);
default:
return false;
}
}
static GLboolean
is_program(struct gl_context *ctx, GLuint name)
{
struct gl_shader_program *shProg = _mesa_lookup_shader_program(ctx, name);
return shProg ? GL_TRUE : GL_FALSE;
}
static GLboolean
is_shader(struct gl_context *ctx, GLuint name)
{
struct gl_shader *shader = _mesa_lookup_shader(ctx, name);
return shader ? GL_TRUE : GL_FALSE;
}
/**
* Attach shader to a shader program.
*/
static void
attach_shader(struct gl_context *ctx, GLuint program, GLuint shader)
{
struct gl_shader_program *shProg;
struct gl_shader *sh;
GLuint i, n;
const bool same_type_disallowed = _mesa_is_gles(ctx);
shProg = _mesa_lookup_shader_program_err(ctx, program, "glAttachShader");
if (!shProg)
return;
sh = _mesa_lookup_shader_err(ctx, shader, "glAttachShader");
if (!sh) {
return;
}
n = shProg->NumShaders;
for (i = 0; i < n; i++) {
if (shProg->Shaders[i] == sh) {
/* The shader is already attched to this program. The
* GL_ARB_shader_objects spec says:
*
* "The error INVALID_OPERATION is generated by AttachObjectARB
* if <obj> is already attached to <containerObj>."
*/
_mesa_error(ctx, GL_INVALID_OPERATION, "glAttachShader");
return;
} else if (same_type_disallowed &&
shProg->Shaders[i]->Stage == sh->Stage) {
/* Shader with the same type is already attached to this program,
* OpenGL ES 2.0 and 3.0 specs say:
*
* "Multiple shader objects of the same type may not be attached
* to a single program object. [...] The error INVALID_OPERATION
* is generated if [...] another shader object of the same type
* as shader is already attached to program."
*/
_mesa_error(ctx, GL_INVALID_OPERATION, "glAttachShader");
return;
}
}
/* grow list */
shProg->Shaders = realloc(shProg->Shaders,
(n + 1) * sizeof(struct gl_shader *));
if (!shProg->Shaders) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glAttachShader");
return;
}
/* append */
shProg->Shaders[n] = NULL; /* since realloc() didn't zero the new space */
_mesa_reference_shader(ctx, &shProg->Shaders[n], sh);
shProg->NumShaders++;
}
static GLuint
create_shader(struct gl_context *ctx, GLenum type)
{
struct gl_shader *sh;
GLuint name;
if (!_mesa_validate_shader_target(ctx, type)) {
_mesa_error(ctx, GL_INVALID_ENUM, "CreateShader(%s)",
_mesa_enum_to_string(type));
return 0;
}
_mesa_HashLockMutex(ctx->Shared->ShaderObjects);
name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
sh = _mesa_new_shader(name, _mesa_shader_enum_to_shader_stage(type));
sh->Type = type;
_mesa_HashInsertLocked(ctx->Shared->ShaderObjects, name, sh);
_mesa_HashUnlockMutex(ctx->Shared->ShaderObjects);
return name;
}
static GLuint
create_shader_program(struct gl_context *ctx)
{
GLuint name;
struct gl_shader_program *shProg;
_mesa_HashLockMutex(ctx->Shared->ShaderObjects);
name = _mesa_HashFindFreeKeyBlock(ctx->Shared->ShaderObjects, 1);
shProg = _mesa_new_shader_program(name);
_mesa_HashInsertLocked(ctx->Shared->ShaderObjects, name, shProg);
assert(shProg->RefCount == 1);
_mesa_HashUnlockMutex(ctx->Shared->ShaderObjects);
return name;
}
/**
* Delete a shader program. Actually, just decrement the program's
* reference count and mark it as DeletePending.
* Used to implement glDeleteProgram() and glDeleteObjectARB().
*/
static void
delete_shader_program(struct gl_context *ctx, GLuint name)
{
/*
* NOTE: deleting shaders/programs works a bit differently than
* texture objects (and buffer objects, etc). Shader/program
* handles/IDs exist in the hash table until the object is really
* deleted (refcount==0). With texture objects, the handle/ID is
* removed from the hash table in glDeleteTextures() while the tex
* object itself might linger until its refcount goes to zero.
*/
struct gl_shader_program *shProg;
shProg = _mesa_lookup_shader_program_err(ctx, name, "glDeleteProgram");
if (!shProg)
return;
if (!shProg->DeletePending) {
shProg->DeletePending = GL_TRUE;
/* effectively, decr shProg's refcount */
_mesa_reference_shader_program(ctx, &shProg, NULL);
}
}
static void
delete_shader(struct gl_context *ctx, GLuint shader)
{
struct gl_shader *sh;
sh = _mesa_lookup_shader_err(ctx, shader, "glDeleteShader");
if (!sh)
return;
if (!sh->DeletePending) {
sh->DeletePending = GL_TRUE;
/* effectively, decr sh's refcount */
_mesa_reference_shader(ctx, &sh, NULL);
}
}
static void
detach_shader(struct gl_context *ctx, GLuint program, GLuint shader)
{
struct gl_shader_program *shProg;
GLuint n;
GLuint i, j;
shProg = _mesa_lookup_shader_program_err(ctx, program, "glDetachShader");
if (!shProg)
return;
n = shProg->NumShaders;
for (i = 0; i < n; i++) {
if (shProg->Shaders[i]->Name == shader) {
/* found it */
struct gl_shader **newList;
/* release */
_mesa_reference_shader(ctx, &shProg->Shaders[i], NULL);
/* alloc new, smaller array */
newList = malloc((n - 1) * sizeof(struct gl_shader *));
if (!newList) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glDetachShader");
return;
}
/* Copy old list entries to new list, skipping removed entry at [i] */
for (j = 0; j < i; j++) {
newList[j] = shProg->Shaders[j];
}
while (++i < n) {
newList[j++] = shProg->Shaders[i];
}
/* Free old list and install new one */
free(shProg->Shaders);
shProg->Shaders = newList;
shProg->NumShaders = n - 1;
#ifdef DEBUG
/* sanity check - make sure the new list's entries are sensible */
for (j = 0; j < shProg->NumShaders; j++) {
assert(shProg->Shaders[j]->Stage == MESA_SHADER_VERTEX ||
shProg->Shaders[j]->Stage == MESA_SHADER_TESS_CTRL ||
shProg->Shaders[j]->Stage == MESA_SHADER_TESS_EVAL ||
shProg->Shaders[j]->Stage == MESA_SHADER_GEOMETRY ||
shProg->Shaders[j]->Stage == MESA_SHADER_FRAGMENT);
assert(shProg->Shaders[j]->RefCount > 0);
}
#endif
return;
}
}
/* not found */
{
GLenum err;
if (is_shader(ctx, shader) || is_program(ctx, shader))
err = GL_INVALID_OPERATION;
else
err = GL_INVALID_VALUE;
_mesa_error(ctx, err, "glDetachShader(shader)");
return;
}
}
/**
* Return list of shaders attached to shader program.
*/
static void
get_attached_shaders(struct gl_context *ctx, GLuint program, GLsizei maxCount,
GLsizei *count, GLuint *obj)
{
struct gl_shader_program *shProg;
if (maxCount < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glGetAttachedShaders(maxCount < 0)");
return;
}
shProg =
_mesa_lookup_shader_program_err(ctx, program, "glGetAttachedShaders");
if (shProg) {
GLuint i;
for (i = 0; i < (GLuint) maxCount && i < shProg->NumShaders; i++) {
obj[i] = shProg->Shaders[i]->Name;
}
if (count)
*count = i;
}
}
/**
* glGetHandleARB() - return ID/name of currently bound shader program.
*/
static GLuint
get_handle(struct gl_context *ctx, GLenum pname)
{
if (pname == GL_PROGRAM_OBJECT_ARB) {
if (ctx->_Shader->ActiveProgram)
return ctx->_Shader->ActiveProgram->Name;
else
return 0;
}
else {
_mesa_error(ctx, GL_INVALID_ENUM, "glGetHandleARB");
return 0;
}
}
/**
* Check if a geometry shader query is valid at this time. If not, report an
* error and return false.
*
* From GL 3.2 section 6.1.16 (Shader and Program Queries):
*
* "If GEOMETRY_VERTICES_OUT, GEOMETRY_INPUT_TYPE, or GEOMETRY_OUTPUT_TYPE
* are queried for a program which has not been linked successfully, or
* which does not contain objects to form a geometry shader, then an
* INVALID_OPERATION error is generated."
*/
static bool
check_gs_query(struct gl_context *ctx, const struct gl_shader_program *shProg)
{
if (shProg->LinkStatus &&
shProg->_LinkedShaders[MESA_SHADER_GEOMETRY] != NULL) {
return true;
}
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetProgramv(linked geometry shader required)");
return false;
}
/**
* Check if a tessellation control shader query is valid at this time.
* If not, report an error and return false.
*
* From GL 4.0 section 6.1.12 (Shader and Program Queries):
*
* "If TESS_CONTROL_OUTPUT_VERTICES is queried for a program which has
* not been linked successfully, or which does not contain objects to
* form a tessellation control shader, then an INVALID_OPERATION error is
* generated."
*/
static bool
check_tcs_query(struct gl_context *ctx, const struct gl_shader_program *shProg)
{
if (shProg->LinkStatus &&
shProg->_LinkedShaders[MESA_SHADER_TESS_CTRL] != NULL) {
return true;
}
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetProgramv(linked tessellation control shader required)");
return false;
}
/**
* Check if a tessellation evaluation shader query is valid at this time.
* If not, report an error and return false.
*
* From GL 4.0 section 6.1.12 (Shader and Program Queries):
*
* "If any of the pname values in this paragraph are queried for a program
* which has not been linked successfully, or which does not contain
* objects to form a tessellation evaluation shader, then an
* INVALID_OPERATION error is generated."
*
*/
static bool
check_tes_query(struct gl_context *ctx, const struct gl_shader_program *shProg)
{
if (shProg->LinkStatus &&
shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL] != NULL) {
return true;
}
_mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramv(linked tessellation "
"evaluation shader required)");
return false;
}
/**
* glGetProgramiv() - get shader program state.
* Note that this is for GLSL shader programs, not ARB vertex/fragment
* programs (see glGetProgramivARB).
*/
static void
get_programiv(struct gl_context *ctx, GLuint program, GLenum pname,
GLint *params)
{
struct gl_shader_program *shProg
= _mesa_lookup_shader_program_err(ctx, program, "glGetProgramiv(program)");
/* Is transform feedback available in this context?
*/
const bool has_xfb =
(ctx->API == API_OPENGL_COMPAT && ctx->Extensions.EXT_transform_feedback)
|| ctx->API == API_OPENGL_CORE
|| _mesa_is_gles3(ctx);
/* True if geometry shaders (of the form that was adopted into GLSL 1.50
* and GL 3.2) are available in this context
*/
const bool has_core_gs = _mesa_has_geometry_shaders(ctx);
const bool has_tess = _mesa_has_tessellation(ctx);
/* Are uniform buffer objects available in this context?
*/
const bool has_ubo =
(ctx->API == API_OPENGL_COMPAT &&
ctx->Extensions.ARB_uniform_buffer_object)
|| ctx->API == API_OPENGL_CORE
|| _mesa_is_gles3(ctx);
if (!shProg) {
return;
}
switch (pname) {
case GL_DELETE_STATUS:
*params = shProg->DeletePending;
return;
case GL_LINK_STATUS:
*params = shProg->LinkStatus;
return;
case GL_VALIDATE_STATUS:
*params = shProg->Validated;
return;
case GL_INFO_LOG_LENGTH:
*params = shProg->InfoLog ? strlen(shProg->InfoLog) + 1 : 0;
return;
case GL_ATTACHED_SHADERS:
*params = shProg->NumShaders;
return;
case GL_ACTIVE_ATTRIBUTES:
*params = _mesa_count_active_attribs(shProg);
return;
case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
*params = _mesa_longest_attribute_name_length(shProg);
return;
case GL_ACTIVE_UNIFORMS: {
unsigned i;
const unsigned num_uniforms =
shProg->NumUniformStorage - shProg->NumHiddenUniforms;
for (*params = 0, i = 0; i < num_uniforms; i++) {
if (!shProg->UniformStorage[i].is_shader_storage)
(*params)++;
}
return;
}
case GL_ACTIVE_UNIFORM_MAX_LENGTH: {
unsigned i;
GLint max_len = 0;
const unsigned num_uniforms =
shProg->NumUniformStorage - shProg->NumHiddenUniforms;
for (i = 0; i < num_uniforms; i++) {
if (shProg->UniformStorage[i].is_shader_storage)
continue;
/* Add one for the terminating NUL character for a non-array, and
* 4 for the "[0]" and the NUL for an array.
*/
const GLint len = strlen(shProg->UniformStorage[i].name) + 1 +
((shProg->UniformStorage[i].array_elements != 0) ? 3 : 0);
if (len > max_len)
max_len = len;
}
*params = max_len;
return;
}
case GL_TRANSFORM_FEEDBACK_VARYINGS:
if (!has_xfb)
break;
*params = shProg->TransformFeedback.NumVarying;
return;
case GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH: {
unsigned i;
GLint max_len = 0;
if (!has_xfb)
break;
for (i = 0; i < shProg->TransformFeedback.NumVarying; i++) {
/* Add one for the terminating NUL character.
*/
const GLint len =
strlen(shProg->TransformFeedback.VaryingNames[i]) + 1;
if (len > max_len)
max_len = len;
}
*params = max_len;
return;
}
case GL_TRANSFORM_FEEDBACK_BUFFER_MODE:
if (!has_xfb)
break;
*params = shProg->TransformFeedback.BufferMode;
return;
case GL_GEOMETRY_VERTICES_OUT:
if (!has_core_gs)
break;
if (check_gs_query(ctx, shProg)) {
*params = shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->
info.Geom.VerticesOut;
}
return;
case GL_GEOMETRY_SHADER_INVOCATIONS:
if (!has_core_gs || !ctx->Extensions.ARB_gpu_shader5)
break;
if (check_gs_query(ctx, shProg)) {
*params = shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->
info.Geom.Invocations;
}
return;
case GL_GEOMETRY_INPUT_TYPE:
if (!has_core_gs)
break;
if (check_gs_query(ctx, shProg)) {
*params = shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->
info.Geom.InputType;
}
return;
case GL_GEOMETRY_OUTPUT_TYPE:
if (!has_core_gs)
break;
if (check_gs_query(ctx, shProg)) {
*params = shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->
info.Geom.OutputType;
}
return;
case GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH: {
unsigned i;
GLint max_len = 0;
if (!has_ubo)
break;
for (i = 0; i < shProg->NumUniformBlocks; i++) {
/* Add one for the terminating NUL character.
*/
const GLint len = strlen(shProg->UniformBlocks[i].Name) + 1;
if (len > max_len)
max_len = len;
}
*params = max_len;
return;
}
case GL_ACTIVE_UNIFORM_BLOCKS:
if (!has_ubo)
break;
*params = shProg->NumUniformBlocks;
return;
case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
/* This enum isn't part of the OES extension for OpenGL ES 2.0. It is
* only available with desktop OpenGL 3.0+ with the
* GL_ARB_get_program_binary extension or OpenGL ES 3.0.
*
* On desktop, we ignore the 3.0+ requirement because it is silly.
*/
if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
break;
*params = shProg->BinaryRetreivableHint;
return;
case GL_PROGRAM_BINARY_LENGTH:
*params = 0;
return;
case GL_ACTIVE_ATOMIC_COUNTER_BUFFERS:
if (!ctx->Extensions.ARB_shader_atomic_counters)
break;
*params = shProg->NumAtomicBuffers;
return;
case GL_COMPUTE_WORK_GROUP_SIZE: {
int i;
if (!_mesa_has_compute_shaders(ctx))
break;
if (!shProg->LinkStatus) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramiv(program not "
"linked)");
return;
}
if (shProg->_LinkedShaders[MESA_SHADER_COMPUTE] == NULL) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glGetProgramiv(no compute "
"shaders)");
return;
}
for (i = 0; i < 3; i++)
params[i] = shProg->Comp.LocalSize[i];
return;
}
case GL_PROGRAM_SEPARABLE:
/* If the program has not been linked, return initial value 0. */
*params = (shProg->LinkStatus == GL_FALSE) ? 0 : shProg->SeparateShader;
return;
/* ARB_tessellation_shader */
case GL_TESS_CONTROL_OUTPUT_VERTICES:
if (!has_tess)
break;
if (check_tcs_query(ctx, shProg)) {
*params = shProg->_LinkedShaders[MESA_SHADER_TESS_CTRL]->
info.TessCtrl.VerticesOut;
}
return;
case GL_TESS_GEN_MODE:
if (!has_tess)
break;
if (check_tes_query(ctx, shProg)) {
*params = shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL]->
info.TessEval.PrimitiveMode;
}
return;
case GL_TESS_GEN_SPACING:
if (!has_tess)
break;
if (check_tes_query(ctx, shProg)) {
*params = shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL]->
info.TessEval.Spacing;
}
return;
case GL_TESS_GEN_VERTEX_ORDER:
if (!has_tess)
break;
if (check_tes_query(ctx, shProg)) {
*params = shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL]->
info.TessEval.VertexOrder;
}
return;
case GL_TESS_GEN_POINT_MODE:
if (!has_tess)
break;
if (check_tes_query(ctx, shProg)) {
*params = shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL]->
info.TessEval.PointMode;
}
return;
default:
break;
}
_mesa_error(ctx, GL_INVALID_ENUM, "glGetProgramiv(pname=%s)",
_mesa_enum_to_string(pname));
}
/**
* glGetShaderiv() - get GLSL shader state
*/
static void
get_shaderiv(struct gl_context *ctx, GLuint name, GLenum pname, GLint *params)
{
struct gl_shader *shader =
_mesa_lookup_shader_err(ctx, name, "glGetShaderiv");
if (!shader) {
return;
}
switch (pname) {
case GL_SHADER_TYPE:
*params = shader->Type;
break;
case GL_DELETE_STATUS:
*params = shader->DeletePending;
break;
case GL_COMPILE_STATUS:
*params = shader->CompileStatus;
break;
case GL_INFO_LOG_LENGTH:
*params = shader->InfoLog ? strlen(shader->InfoLog) + 1 : 0;
break;
case GL_SHADER_SOURCE_LENGTH:
*params = shader->Source ? strlen((char *) shader->Source) + 1 : 0;
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM, "glGetShaderiv(pname)");
return;
}
}
static void
get_program_info_log(struct gl_context *ctx, GLuint program, GLsizei bufSize,
GLsizei *length, GLchar *infoLog)
{
struct gl_shader_program *shProg;
/* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
* section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
*
* "If a negative number is provided where an argument of type sizei or
* sizeiptr is specified, an INVALID_VALUE error is generated."
*/
if (bufSize < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramInfoLog(bufSize < 0)");
return;
}
shProg = _mesa_lookup_shader_program_err(ctx, program,
"glGetProgramInfoLog(program)");
if (!shProg) {
return;
}
_mesa_copy_string(infoLog, bufSize, length, shProg->InfoLog);
}
static void
get_shader_info_log(struct gl_context *ctx, GLuint shader, GLsizei bufSize,
GLsizei *length, GLchar *infoLog)
{
struct gl_shader *sh;
/* Section 2.5 GL Errors (page 18) of the OpenGL ES 3.0.4 spec and
* section 2.3.1 (Errors) of the OpenGL 4.5 spec say:
*
* "If a negative number is provided where an argument of type sizei or
* sizeiptr is specified, an INVALID_VALUE error is generated."
*/
if (bufSize < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderInfoLog(bufSize < 0)");
return;
}
sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderInfoLog(shader)");
if (!sh) {
return;
}
_mesa_copy_string(infoLog, bufSize, length, sh->InfoLog);
}
/**
* Return shader source code.
*/
static void
get_shader_source(struct gl_context *ctx, GLuint shader, GLsizei maxLength,
GLsizei *length, GLchar *sourceOut)
{
struct gl_shader *sh;
if (maxLength < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glGetShaderSource(bufSize < 0)");
return;
}
sh = _mesa_lookup_shader_err(ctx, shader, "glGetShaderSource");
if (!sh) {
return;
}
_mesa_copy_string(sourceOut, maxLength, length, sh->Source);
}
/**
* Set/replace shader source code. A helper function used by
* glShaderSource[ARB].
*/
static void
shader_source(struct gl_shader *sh, const GLchar *source)
{
assert(sh);
/* free old shader source string and install new one */
free((void *)sh->Source);
sh->Source = source;
#ifdef DEBUG
sh->SourceChecksum = _mesa_str_checksum(sh->Source);
#endif
}
/**
* Compile a shader.
*/
void
_mesa_compile_shader(struct gl_context *ctx, struct gl_shader *sh)
{
if (!sh)
return;
if (!sh->Source) {
/* If the user called glCompileShader without first calling
* glShaderSource, we should fail to compile, but not raise a GL_ERROR.
*/
sh->CompileStatus = GL_FALSE;
} else {
if (ctx->_Shader->Flags & GLSL_DUMP) {
_mesa_log("GLSL source for %s shader %d:\n",
_mesa_shader_stage_to_string(sh->Stage), sh->Name);
_mesa_log("%s\n", sh->Source);
}
/* this call will set the shader->CompileStatus field to indicate if
* compilation was successful.
*/
_mesa_glsl_compile_shader(ctx, sh, false, false);
if (ctx->_Shader->Flags & GLSL_LOG) {
_mesa_write_shader_to_file(sh);
}
if (ctx->_Shader->Flags & GLSL_DUMP) {
if (sh->CompileStatus) {
if (sh->ir) {
_mesa_log("GLSL IR for shader %d:\n", sh->Name);
_mesa_print_ir(_mesa_get_log_file(), sh->ir, NULL);
} else {
_mesa_log("No GLSL IR for shader %d (shader may be from "
"cache)\n", sh->Name);
}
_mesa_log("\n\n");
} else {
_mesa_log("GLSL shader %d failed to compile.\n", sh->Name);
}
if (sh->InfoLog && sh->InfoLog[0] != 0) {
_mesa_log("GLSL shader %d info log:\n", sh->Name);
_mesa_log("%s\n", sh->InfoLog);
}
}
}
if (!sh->CompileStatus) {
if (ctx->_Shader->Flags & GLSL_DUMP_ON_ERROR) {
_mesa_log("GLSL source for %s shader %d:\n",
_mesa_shader_stage_to_string(sh->Stage), sh->Name);
_mesa_log("%s\n", sh->Source);
_mesa_log("Info Log:\n%s\n", sh->InfoLog);
}
if (ctx->_Shader->Flags & GLSL_REPORT_ERRORS) {
_mesa_debug(ctx, "Error compiling shader %u:\n%s\n",
sh->Name, sh->InfoLog);
}
}
}
/**
* Link a program's shaders.
*/
void
_mesa_link_program(struct gl_context *ctx, struct gl_shader_program *shProg)
{
if (!shProg)
return;
/* From the ARB_transform_feedback2 specification:
* "The error INVALID_OPERATION is generated by LinkProgram if <program> is
* the name of a program being used by one or more transform feedback
* objects, even if the objects are not currently bound or are paused."
*/
if (_mesa_transform_feedback_is_using_program(ctx, shProg)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glLinkProgram(transform feedback is using the program)");
return;
}
FLUSH_VERTICES(ctx, _NEW_PROGRAM);
_mesa_glsl_link_shader(ctx, shProg);
/* Capture .shader_test files. */
const char *capture_path = _mesa_get_shader_capture_path();
if (shProg->Name != 0 && shProg->Name != ~0 && capture_path != NULL) {
FILE *file;
char filename[PATH_MAX];
_mesa_snprintf(filename, sizeof(filename), "%s/%u.shader_test",
capture_path, shProg->Name);
file = fopen(filename, "w");
if (file) {
fprintf(file, "[require]\nGLSL%s >= %u.%02u\n",
shProg->IsES ? " ES" : "",
shProg->Version / 100, shProg->Version % 100);
if (shProg->SeparateShader)
fprintf(file, "GL_ARB_separate_shader_objects\nSSO ENABLED\n");
fprintf(file, "\n");
for (unsigned i = 0; i < shProg->NumShaders; i++) {
fprintf(file, "[%s shader]\n%s\n",
_mesa_shader_stage_to_string(shProg->Shaders[i]->Stage),
shProg->Shaders[i]->Source);
}
fclose(file);
} else {
_mesa_warning(ctx, "Failed to open %s", filename);
}
}
if (shProg->LinkStatus == GL_FALSE &&
(ctx->_Shader->Flags & GLSL_REPORT_ERRORS)) {
_mesa_debug(ctx, "Error linking program %u:\n%s\n",
shProg->Name, shProg->InfoLog);
}
/* debug code */
if (0) {
GLuint i;
printf("Link %u shaders in program %u: %s\n",
shProg->NumShaders, shProg->Name,
shProg->LinkStatus ? "Success" : "Failed");
for (i = 0; i < shProg->NumShaders; i++) {
printf(" shader %u, stage %u\n",
shProg->Shaders[i]->Name,
shProg->Shaders[i]->Stage);
}
}
}
/**
* Print basic shader info (for debug).
*/
static void
print_shader_info(const struct gl_shader_program *shProg)
{
GLuint i;
printf("Mesa: glUseProgram(%u)\n", shProg->Name);
for (i = 0; i < shProg->NumShaders; i++) {
printf(" %s shader %u, checksum %u\n",
_mesa_shader_stage_to_string(shProg->Shaders[i]->Stage),
shProg->Shaders[i]->Name,
shProg->Shaders[i]->SourceChecksum);
}
if (shProg->_LinkedShaders[MESA_SHADER_VERTEX])
printf(" vert prog %u\n",
shProg->_LinkedShaders[MESA_SHADER_VERTEX]->Program->Id);
if (shProg->_LinkedShaders[MESA_SHADER_FRAGMENT])
printf(" frag prog %u\n",
shProg->_LinkedShaders[MESA_SHADER_FRAGMENT]->Program->Id);
if (shProg->_LinkedShaders[MESA_SHADER_GEOMETRY])
printf(" geom prog %u\n",
shProg->_LinkedShaders[MESA_SHADER_GEOMETRY]->Program->Id);
if (shProg->_LinkedShaders[MESA_SHADER_TESS_CTRL])
printf(" tesc prog %u\n",
shProg->_LinkedShaders[MESA_SHADER_TESS_CTRL]->Program->Id);
if (shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL])
printf(" tese prog %u\n",
shProg->_LinkedShaders[MESA_SHADER_TESS_EVAL]->Program->Id);
}
/**
* Use the named shader program for subsequent glUniform calls
*/
void
_mesa_active_program(struct gl_context *ctx, struct gl_shader_program *shProg,
const char *caller)
{
if ((shProg != NULL) && !shProg->LinkStatus) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(program %u not linked)", caller, shProg->Name);
return;
}
if (ctx->Shader.ActiveProgram != shProg) {
_mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, shProg);
}
}
static void
use_shader_program(struct gl_context *ctx, gl_shader_stage stage,
struct gl_shader_program *shProg,
struct gl_pipeline_object *shTarget)
{
struct gl_shader_program **target;
target = &shTarget->CurrentProgram[stage];
if ((shProg != NULL) && (shProg->_LinkedShaders[stage] == NULL))
shProg = NULL;
if (shProg)
_mesa_shader_program_init_subroutine_defaults(ctx, shProg);
if (*target != shProg) {
/* Program is current, flush it */
if (shTarget == ctx->_Shader) {
FLUSH_VERTICES(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
}
/* If the shader is also bound as the current rendering shader, unbind
* it from that binding point as well. This ensures that the correct
* semantics of glDeleteProgram are maintained.
*/
switch (stage) {
case MESA_SHADER_VERTEX:
case MESA_SHADER_TESS_CTRL:
case MESA_SHADER_TESS_EVAL:
case MESA_SHADER_GEOMETRY:
case MESA_SHADER_COMPUTE:
/* Empty for now. */
break;
case MESA_SHADER_FRAGMENT:
if (*target == ctx->_Shader->_CurrentFragmentProgram) {
_mesa_reference_shader_program(ctx,
&ctx->_Shader->_CurrentFragmentProgram,
NULL);
}
break;
}
_mesa_reference_shader_program(ctx, target, shProg);
return;
}
}
/**
* Use the named shader program for subsequent rendering.
*/
void
_mesa_use_program(struct gl_context *ctx, struct gl_shader_program *shProg)
{
int i;
for (i = 0; i < MESA_SHADER_STAGES; i++)
use_shader_program(ctx, i, shProg, &ctx->Shader);
_mesa_active_program(ctx, shProg, "glUseProgram");
}
/**
* Do validation of the given shader program.
* \param errMsg returns error message if validation fails.
* \return GL_TRUE if valid, GL_FALSE if invalid (and set errMsg)
*/
static GLboolean
validate_shader_program(const struct gl_shader_program *shProg,
char *errMsg)
{
if (!shProg->LinkStatus) {
return GL_FALSE;
}
/* From the GL spec, a program is invalid if any of these are true:
any two active samplers in the current program object are of
different types, but refer to the same texture image unit,
any active sampler in the current program object refers to a texture
image unit where fixed-function fragment processing accesses a
texture target that does not match the sampler type, or
the sum of the number of active samplers in the program and the
number of texture image units enabled for fixed-function fragment
processing exceeds the combined limit on the total number of texture
image units allowed.
*/
/*
* Check: any two active samplers in the current program object are of
* different types, but refer to the same texture image unit,
*/
if (!_mesa_sampler_uniforms_are_valid(shProg, errMsg, 100))
return GL_FALSE;
return GL_TRUE;
}
/**
* Called via glValidateProgram()
*/
static void
validate_program(struct gl_context *ctx, GLuint program)
{
struct gl_shader_program *shProg;
char errMsg[100] = "";
shProg = _mesa_lookup_shader_program_err(ctx, program, "glValidateProgram");
if (!shProg) {
return;
}
shProg->Validated = validate_shader_program(shProg, errMsg);
if (!shProg->Validated) {
/* update info log */
if (shProg->InfoLog) {
ralloc_free(shProg->InfoLog);
}
shProg->InfoLog = ralloc_strdup(shProg, errMsg);
}
}
void GLAPIENTRY
_mesa_AttachObjectARB(GLhandleARB program, GLhandleARB shader)
{
GET_CURRENT_CONTEXT(ctx);
attach_shader(ctx, program, shader);
}
void GLAPIENTRY
_mesa_AttachShader(GLuint program, GLuint shader)
{
GET_CURRENT_CONTEXT(ctx);
attach_shader(ctx, program, shader);
}
void GLAPIENTRY
_mesa_CompileShader(GLuint shaderObj)
{
GET_CURRENT_CONTEXT(ctx);
if (MESA_VERBOSE & VERBOSE_API)
_mesa_debug(ctx, "glCompileShader %u\n", shaderObj);
_mesa_compile_shader(ctx, _mesa_lookup_shader_err(ctx, shaderObj,
"glCompileShader"));
}
GLuint GLAPIENTRY
_mesa_CreateShader(GLenum type)
{
GET_CURRENT_CONTEXT(ctx);
if (MESA_VERBOSE & VERBOSE_API)
_mesa_debug(ctx, "glCreateShader %s\n", _mesa_enum_to_string(type));
return create_shader(ctx, type);
}
GLhandleARB GLAPIENTRY
_mesa_CreateShaderObjectARB(GLenum type)
{
GET_CURRENT_CONTEXT(ctx);
return create_shader(ctx, type);
}
GLuint GLAPIENTRY
_mesa_CreateProgram(void)
{
GET_CURRENT_CONTEXT(ctx);
if (MESA_VERBOSE & VERBOSE_API)
_mesa_debug(ctx, "glCreateProgram\n");
return create_shader_program(ctx);
}
GLhandleARB GLAPIENTRY
_mesa_CreateProgramObjectARB(void)
{
GET_CURRENT_CONTEXT(ctx);
return create_shader_program(ctx);
}
void GLAPIENTRY
_mesa_DeleteObjectARB(GLhandleARB obj)
{
if (MESA_VERBOSE & VERBOSE_API) {
GET_CURRENT_CONTEXT(ctx);
_mesa_debug(ctx, "glDeleteObjectARB(%lu)\n", (unsigned long)obj);
}
if (obj) {
GET_CURRENT_CONTEXT(ctx);
FLUSH_VERTICES(ctx, 0);
if (is_program(ctx, obj)) {
delete_shader_program(ctx, obj);
}
else if (is_shader(ctx, obj)) {
delete_shader(ctx, obj);
}
else {
/* error? */
}
}
}
void GLAPIENTRY
_mesa_DeleteProgram(GLuint name)
{
if (name) {
GET_CURRENT_CONTEXT(ctx);
FLUSH_VERTICES(ctx, 0);
delete_shader_program(ctx, name);
}
}
void GLAPIENTRY
_mesa_DeleteShader(GLuint name)
{
if (name) {
GET_CURRENT_CONTEXT(ctx);
FLUSH_VERTICES(ctx, 0);
delete_shader(ctx, name);
}
}
void GLAPIENTRY
_mesa_DetachObjectARB(GLhandleARB program, GLhandleARB shader)
{
GET_CURRENT_CONTEXT(ctx);
detach_shader(ctx, program, shader);
}
void GLAPIENTRY
_mesa_DetachShader(GLuint program, GLuint shader)
{
GET_CURRENT_CONTEXT(ctx);
detach_shader(ctx, program, shader);
}
void GLAPIENTRY
_mesa_GetAttachedObjectsARB(GLhandleARB container, GLsizei maxCount,
GLsizei * count, GLhandleARB * obj)
{
GET_CURRENT_CONTEXT(ctx);
get_attached_shaders(ctx, container, maxCount, count, obj);
}
void GLAPIENTRY
_mesa_GetAttachedShaders(GLuint program, GLsizei maxCount,
GLsizei *count, GLuint *obj)
{
GET_CURRENT_CONTEXT(ctx);
get_attached_shaders(ctx, program, maxCount, count, obj);
}
void GLAPIENTRY
_mesa_GetInfoLogARB(GLhandleARB object, GLsizei maxLength, GLsizei * length,
GLcharARB * infoLog)
{
GET_CURRENT_CONTEXT(ctx);
if (is_program(ctx, object)) {
get_program_info_log(ctx, object, maxLength, length, infoLog);
}
else if (is_shader(ctx, object)) {
get_shader_info_log(ctx, object, maxLength, length, infoLog);
}
else {
_mesa_error(ctx, GL_INVALID_OPERATION, "glGetInfoLogARB");
}
}
void GLAPIENTRY
_mesa_GetObjectParameterivARB(GLhandleARB object, GLenum pname, GLint *params)
{
GET_CURRENT_CONTEXT(ctx);
/* Implement in terms of GetProgramiv, GetShaderiv */
if (is_program(ctx, object)) {
if (pname == GL_OBJECT_TYPE_ARB) {
*params = GL_PROGRAM_OBJECT_ARB;
}
else {
get_programiv(ctx, object, pname, params);
}
}
else if (is_shader(ctx, object)) {
if (pname == GL_OBJECT_TYPE_ARB) {
*params = GL_SHADER_OBJECT_ARB;
}
else {
get_shaderiv(ctx, object, pname, params);
}
}
else {
_mesa_error(ctx, GL_INVALID_VALUE, "glGetObjectParameterivARB");
}
}
void GLAPIENTRY
_mesa_GetObjectParameterfvARB(GLhandleARB object, GLenum pname,
GLfloat *params)
{
GLint iparams[1] = {0}; /* XXX is one element enough? */
_mesa_GetObjectParameterivARB(object, pname, iparams);
params[0] = (GLfloat) iparams[0];
}
void GLAPIENTRY
_mesa_GetProgramiv(GLuint program, GLenum pname, GLint *params)
{
GET_CURRENT_CONTEXT(ctx);
get_programiv(ctx, program, pname, params);
}
void GLAPIENTRY
_mesa_GetShaderiv(GLuint shader, GLenum pname, GLint *params)
{
GET_CURRENT_CONTEXT(ctx);
get_shaderiv(ctx, shader, pname, params);
}
void GLAPIENTRY
_mesa_GetProgramInfoLog(GLuint program, GLsizei bufSize,
GLsizei *length, GLchar *infoLog)
{
GET_CURRENT_CONTEXT(ctx);
get_program_info_log(ctx, program, bufSize, length, infoLog);
}
void GLAPIENTRY
_mesa_GetShaderInfoLog(GLuint shader, GLsizei bufSize,
GLsizei *length, GLchar *infoLog)
{
GET_CURRENT_CONTEXT(ctx);
get_shader_info_log(ctx, shader, bufSize, length, infoLog);
}
void GLAPIENTRY
_mesa_GetShaderSource(GLuint shader, GLsizei maxLength,
GLsizei *length, GLchar *sourceOut)
{
GET_CURRENT_CONTEXT(ctx);
get_shader_source(ctx, shader, maxLength, length, sourceOut);
}
GLhandleARB GLAPIENTRY
_mesa_GetHandleARB(GLenum pname)
{
GET_CURRENT_CONTEXT(ctx);
return get_handle(ctx, pname);
}
GLboolean GLAPIENTRY
_mesa_IsProgram(GLuint name)
{
GET_CURRENT_CONTEXT(ctx);
return is_program(ctx, name);
}
GLboolean GLAPIENTRY
_mesa_IsShader(GLuint name)
{
GET_CURRENT_CONTEXT(ctx);
return is_shader(ctx, name);
}
void GLAPIENTRY
_mesa_LinkProgram(GLuint programObj)
{
GET_CURRENT_CONTEXT(ctx);
if (MESA_VERBOSE & VERBOSE_API)
_mesa_debug(ctx, "glLinkProgram %u\n", programObj);
_mesa_link_program(ctx, _mesa_lookup_shader_program_err(ctx, programObj,
"glLinkProgram"));
}
#if defined(HAVE_SHA1)
/**
* Generate a SHA-1 hash value string for given source string.
*/
static void
generate_sha1(const char *source, char sha_str[64])
{
unsigned char sha[20];
_mesa_sha1_compute(source, strlen(source), sha);
_mesa_sha1_format(sha_str, sha);
}
/**
* Construct a full path for shader replacement functionality using
* following format:
*
* <path>/<stage prefix>_<CHECKSUM>.glsl
*/
static void
construct_name(const gl_shader_stage stage, const char *source,
const char *path, char *name, unsigned length)
{
char sha[64];
static const char *types[] = {
"VS", "TC", "TE", "GS", "FS", "CS",
};
generate_sha1(source, sha);
_mesa_snprintf(name, length, "%s/%s_%s.glsl", path, types[stage],
sha);
}
/**
* Write given shader source to a file in MESA_SHADER_DUMP_PATH.
*/
static void
dump_shader(const gl_shader_stage stage, const char *source)
{
char name[PATH_MAX];
static bool path_exists = true;
char *dump_path;
FILE *f;
if (!path_exists)
return;
dump_path = getenv("MESA_SHADER_DUMP_PATH");
if (!dump_path) {
path_exists = false;
return;
}
construct_name(stage, source, dump_path, name, PATH_MAX);
f = fopen(name, "w");
if (f) {
fputs(source, f);
fclose(f);
} else {
GET_CURRENT_CONTEXT(ctx);
_mesa_warning(ctx, "could not open %s for dumping shader (%s)", name,
strerror(errno));
}
}
/**
* Read shader source code from a file.
* Useful for debugging to override an app's shader.
*/
static GLcharARB *
read_shader(const gl_shader_stage stage, const char *source)
{
char name[PATH_MAX];
char *read_path;
static bool path_exists = true;
int len, shader_size = 0;
GLcharARB *buffer;
FILE *f;
if (!path_exists)
return NULL;
read_path = getenv("MESA_SHADER_READ_PATH");
if (!read_path) {
path_exists = false;
return NULL;
}
construct_name(stage, source, read_path, name, PATH_MAX);
f = fopen(name, "r");
if (!f)
return NULL;
/* allocate enough room for the entire shader */
fseek(f, 0, SEEK_END);
shader_size = ftell(f);
rewind(f);
assert(shader_size);
/* add one for terminating zero */
shader_size++;
buffer = malloc(shader_size);
assert(buffer);
len = fread(buffer, 1, shader_size, f);
buffer[len] = 0;
fclose(f);
return buffer;
}
#endif /* HAVE_SHA1 */
/**
* Called via glShaderSource() and glShaderSourceARB() API functions.
* Basically, concatenate the source code strings into one long string
* and pass it to _mesa_shader_source().
*/
void GLAPIENTRY
_mesa_ShaderSource(GLuint shaderObj, GLsizei count,
const GLchar * const * string, const GLint * length)
{
GET_CURRENT_CONTEXT(ctx);
GLint *offsets;
GLsizei i, totalLength;
GLcharARB *source;
struct gl_shader *sh;
#if defined(HAVE_SHA1)
GLcharARB *replacement;
#endif /* HAVE_SHA1 */
sh = _mesa_lookup_shader_err(ctx, shaderObj, "glShaderSourceARB");
if (!sh)
return;
if (string == NULL) {
_mesa_error(ctx, GL_INVALID_VALUE, "glShaderSourceARB");
return;
}
/*
* This array holds offsets of where the appropriate string ends, thus the
* last element will be set to the total length of the source code.
*/
offsets = malloc(count * sizeof(GLint));
if (offsets == NULL) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
return;
}
for (i = 0; i < count; i++) {
if (string[i] == NULL) {
free((GLvoid *) offsets);
_mesa_error(ctx, GL_INVALID_OPERATION,
"glShaderSourceARB(null string)");
return;
}
if (length == NULL || length[i] < 0)
offsets[i] = strlen(string[i]);
else
offsets[i] = length[i];
/* accumulate string lengths */
if (i > 0)
offsets[i] += offsets[i - 1];
}
/* Total length of source string is sum off all strings plus two.
* One extra byte for terminating zero, another extra byte to silence
* valgrind warnings in the parser/grammer code.
*/
totalLength = offsets[count - 1] + 2;
source = malloc(totalLength * sizeof(GLcharARB));
if (source == NULL) {
free((GLvoid *) offsets);
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glShaderSourceARB");
return;
}
for (i = 0; i < count; i++) {
GLint start = (i > 0) ? offsets[i - 1] : 0;
memcpy(source + start, string[i],
(offsets[i] - start) * sizeof(GLcharARB));
}
source[totalLength - 1] = '\0';
source[totalLength - 2] = '\0';
#if defined(HAVE_SHA1)
/* Dump original shader source to MESA_SHADER_DUMP_PATH and replace
* if corresponding entry found from MESA_SHADER_READ_PATH.
*/
dump_shader(sh->Stage, source);
replacement = read_shader(sh->Stage, source);
if (replacement) {
free(source);
source = replacement;
}
#endif /* HAVE_SHA1 */
shader_source(sh, source);
free(offsets);
}
void GLAPIENTRY
_mesa_UseProgram(GLuint program)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_shader_program *shProg;
if (MESA_VERBOSE & VERBOSE_API)
_mesa_debug(ctx, "glUseProgram %u\n", program);
if (_mesa_is_xfb_active_and_unpaused(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glUseProgram(transform feedback active)");
return;
}
if (program) {
shProg = _mesa_lookup_shader_program_err(ctx, program, "glUseProgram");
if (!shProg) {
return;
}
if (!shProg->LinkStatus) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glUseProgram(program %u not linked)", program);
return;
}
/* debug code */
if (ctx->_Shader->Flags & GLSL_USE_PROG) {
print_shader_info(shProg);
}
}
else {
shProg = NULL;
}
/* The ARB_separate_shader_object spec says:
*
* "The executable code for an individual shader stage is taken from
* the current program for that stage. If there is a current program
* object established by UseProgram, that program is considered current
* for all stages. Otherwise, if there is a bound program pipeline
* object (section 2.14.PPO), the program bound to the appropriate
* stage of the pipeline object is considered current."
*/
if (program) {
/* Attach shader state to the binding point */
_mesa_reference_pipeline_object(ctx, &ctx->_Shader, &ctx->Shader);
/* Update the program */
_mesa_use_program(ctx, shProg);
} else {
/* Must be done first: detach the progam */
_mesa_use_program(ctx, shProg);
/* Unattach shader_state binding point */
_mesa_reference_pipeline_object(ctx, &ctx->_Shader, ctx->Pipeline.Default);
/* If a pipeline was bound, rebind it */
if (ctx->Pipeline.Current) {
_mesa_BindProgramPipeline(ctx->Pipeline.Current->Name);
}
}
}
void GLAPIENTRY
_mesa_ValidateProgram(GLuint program)
{
GET_CURRENT_CONTEXT(ctx);
validate_program(ctx, program);
}
/**
* For OpenGL ES 2.0, GL_ARB_ES2_compatibility
*/
void GLAPIENTRY
_mesa_GetShaderPrecisionFormat(GLenum shadertype, GLenum precisiontype,
GLint* range, GLint* precision)
{
const struct gl_program_constants *limits;
const struct gl_precision *p;
GET_CURRENT_CONTEXT(ctx);
switch (shadertype) {
case GL_VERTEX_SHADER:
limits = &ctx->Const.Program[MESA_SHADER_VERTEX];
break;
case GL_FRAGMENT_SHADER:
limits = &ctx->Const.Program[MESA_SHADER_FRAGMENT];
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM,
"glGetShaderPrecisionFormat(shadertype)");
return;
}
switch (precisiontype) {
case GL_LOW_FLOAT:
p = &limits->LowFloat;
break;
case GL_MEDIUM_FLOAT:
p = &limits->MediumFloat;
break;
case GL_HIGH_FLOAT:
p = &limits->HighFloat;
break;
case GL_LOW_INT:
p = &limits->LowInt;
break;
case GL_MEDIUM_INT:
p = &limits->MediumInt;
break;
case GL_HIGH_INT:
p = &limits->HighInt;
break;
default:
_mesa_error(ctx, GL_INVALID_ENUM,
"glGetShaderPrecisionFormat(precisiontype)");
return;
}
range[0] = p->RangeMin;
range[1] = p->RangeMax;
precision[0] = p->Precision;
}
/**
* For OpenGL ES 2.0, GL_ARB_ES2_compatibility
*/
void GLAPIENTRY
_mesa_ReleaseShaderCompiler(void)
{
_mesa_destroy_shader_compiler_caches();
}
/**
* For OpenGL ES 2.0, GL_ARB_ES2_compatibility
*/
void GLAPIENTRY
_mesa_ShaderBinary(GLint n, const GLuint* shaders, GLenum binaryformat,
const void* binary, GLint length)
{
GET_CURRENT_CONTEXT(ctx);
(void) shaders;
(void) binaryformat;
(void) binary;
/* Page 68, section 7.2 'Shader Binaries" of the of the OpenGL ES 3.1, and
* page 88 of the OpenGL 4.5 specs state:
*
* "An INVALID_VALUE error is generated if count or length is negative.
* An INVALID_ENUM error is generated if binaryformat is not a supported
* format returned in SHADER_BINARY_FORMATS."
*/
if (n < 0 || length < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glShaderBinary(count or length < 0)");
return;
}
_mesa_error(ctx, GL_INVALID_ENUM, "glShaderBinary(format)");
}
void GLAPIENTRY
_mesa_GetProgramBinary(GLuint program, GLsizei bufSize, GLsizei *length,
GLenum *binaryFormat, GLvoid *binary)
{
struct gl_shader_program *shProg;
GLsizei length_dummy;
GET_CURRENT_CONTEXT(ctx);
if (bufSize < 0){
_mesa_error(ctx, GL_INVALID_VALUE, "glGetProgramBinary(bufSize < 0)");
return;
}
shProg = _mesa_lookup_shader_program_err(ctx, program, "glGetProgramBinary");
if (!shProg)
return;
/* The ARB_get_program_binary spec says:
*
* "If <length> is NULL, then no length is returned."
*
* Ensure that length always points to valid storage to avoid multiple NULL
* pointer checks below.
*/
if (length == NULL)
length = &length_dummy;
/* The ARB_get_program_binary spec says:
*
* "When a program object's LINK_STATUS is FALSE, its program binary
* length is zero, and a call to GetProgramBinary will generate an
* INVALID_OPERATION error.
*/
if (!shProg->LinkStatus) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetProgramBinary(program %u not linked)",
shProg->Name);
*length = 0;
return;
}
*length = 0;
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetProgramBinary(driver supports zero binary formats)");
(void) binaryFormat;
(void) binary;
}
void GLAPIENTRY
_mesa_ProgramBinary(GLuint program, GLenum binaryFormat,
const GLvoid *binary, GLsizei length)
{
struct gl_shader_program *shProg;
GET_CURRENT_CONTEXT(ctx);
shProg = _mesa_lookup_shader_program_err(ctx, program, "glProgramBinary");
if (!shProg)
return;
(void) binaryFormat;
(void) binary;
/* Section 2.3.1 (Errors) of the OpenGL 4.5 spec says:
*
* "If a negative number is provided where an argument of type sizei or
* sizeiptr is specified, an INVALID_VALUE error is generated."
*/
if (length < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glProgramBinary(length < 0)");
return;
}
/* The ARB_get_program_binary spec says:
*
* "<binaryFormat> and <binary> must be those returned by a previous
* call to GetProgramBinary, and <length> must be the length of the
* program binary as returned by GetProgramBinary or GetProgramiv with
* <pname> PROGRAM_BINARY_LENGTH. Loading the program binary will fail,
* setting the LINK_STATUS of <program> to FALSE, if these conditions
* are not met."
*
* Since any value of binaryFormat passed "is not one of those specified as
* allowable for [this] command, an INVALID_ENUM error is generated."
*/
shProg->LinkStatus = GL_FALSE;
_mesa_error(ctx, GL_INVALID_ENUM, "glProgramBinary");
}
void GLAPIENTRY
_mesa_ProgramParameteri(GLuint program, GLenum pname, GLint value)
{
struct gl_shader_program *shProg;
GET_CURRENT_CONTEXT(ctx);
shProg = _mesa_lookup_shader_program_err(ctx, program,
"glProgramParameteri");
if (!shProg)
return;
switch (pname) {
case GL_PROGRAM_BINARY_RETRIEVABLE_HINT:
/* This enum isn't part of the OES extension for OpenGL ES 2.0, but it
* is part of OpenGL ES 3.0. For the ES2 case, this function shouldn't
* even be in the dispatch table, so we shouldn't need to expclicitly
* check here.
*
* On desktop, we ignore the 3.0+ requirement because it is silly.
*/
/* The ARB_get_program_binary extension spec says:
*
* "An INVALID_VALUE error is generated if the <value> argument to
* ProgramParameteri is not TRUE or FALSE."
*/
if (value != GL_TRUE && value != GL_FALSE) {
goto invalid_value;
}
/* No need to notify the driver. Any changes will actually take effect
* the next time the shader is linked.
*
* The ARB_get_program_binary extension spec says:
*
* "To indicate that a program binary is likely to be retrieved,
* ProgramParameteri should be called with <pname>
* PROGRAM_BINARY_RETRIEVABLE_HINT and <value> TRUE. This setting
* will not be in effect until the next time LinkProgram or
* ProgramBinary has been called successfully."
*
* The resloution of issue 9 in the extension spec also says:
*
* "The application may use the PROGRAM_BINARY_RETRIEVABLE_HINT hint
* to indicate to the GL implementation that this program will
* likely be saved with GetProgramBinary at some point. This will
* give the GL implementation the opportunity to track any state
* changes made to the program before being saved such that when it
* is loaded again a recompile can be avoided."
*/
shProg->BinaryRetreivableHint = value;
return;
case GL_PROGRAM_SEPARABLE:
/* Spec imply that the behavior is the same as ARB_get_program_binary
* Chapter 7.3 Program Objects
*/
if (value != GL_TRUE && value != GL_FALSE) {
goto invalid_value;
}
shProg->SeparateShader = value;
return;
default:
_mesa_error(ctx, GL_INVALID_ENUM, "glProgramParameteri(pname=%s)",
_mesa_enum_to_string(pname));
return;
}
invalid_value:
_mesa_error(ctx, GL_INVALID_VALUE,
"glProgramParameteri(pname=%s, value=%d): "
"value must be 0 or 1.",
_mesa_enum_to_string(pname),
value);
}
void
_mesa_use_shader_program(struct gl_context *ctx, GLenum type,
struct gl_shader_program *shProg,
struct gl_pipeline_object *shTarget)
{
gl_shader_stage stage = _mesa_shader_enum_to_shader_stage(type);
use_shader_program(ctx, stage, shProg, shTarget);
}
/**
* Copy program-specific data generated by linking from the gl_shader_program
* object to the gl_program object referred to by the gl_linked_shader.
*
* This function expects _mesa_reference_program() to have been previously
* called setting the gl_linked_shaders program reference.
*/
void
_mesa_copy_linked_program_data(const struct gl_shader_program *src,
struct gl_linked_shader *dst_sh)
{
assert(dst_sh->Program);
struct gl_program *dst = dst_sh->Program;
switch (dst_sh->Stage) {
case MESA_SHADER_VERTEX:
dst->ClipDistanceArraySize = src->Vert.ClipDistanceArraySize;
dst->CullDistanceArraySize = src->Vert.CullDistanceArraySize;
break;
case MESA_SHADER_TESS_CTRL: {
struct gl_tess_ctrl_program *dst_tcp =
(struct gl_tess_ctrl_program *) dst;
dst_tcp->VerticesOut = dst_sh->info.TessCtrl.VerticesOut;
break;
}
case MESA_SHADER_TESS_EVAL: {
struct gl_tess_eval_program *dst_tep =
(struct gl_tess_eval_program *) dst;
dst_tep->PrimitiveMode = dst_sh->info.TessEval.PrimitiveMode;
dst_tep->Spacing = dst_sh->info.TessEval.Spacing;
dst_tep->VertexOrder = dst_sh->info.TessEval.VertexOrder;
dst_tep->PointMode = dst_sh->info.TessEval.PointMode;
dst->ClipDistanceArraySize = src->TessEval.ClipDistanceArraySize;
dst->CullDistanceArraySize = src->TessEval.CullDistanceArraySize;
break;
}
case MESA_SHADER_GEOMETRY: {
struct gl_geometry_program *dst_gp = (struct gl_geometry_program *) dst;
dst_gp->VerticesIn = src->Geom.VerticesIn;
dst_gp->VerticesOut = dst_sh->info.Geom.VerticesOut;
dst_gp->Invocations = dst_sh->info.Geom.Invocations;
dst_gp->InputType = dst_sh->info.Geom.InputType;
dst_gp->OutputType = dst_sh->info.Geom.OutputType;
dst->ClipDistanceArraySize = src->Geom.ClipDistanceArraySize;
dst->CullDistanceArraySize = src->Geom.CullDistanceArraySize;
dst_gp->UsesEndPrimitive = src->Geom.UsesEndPrimitive;
dst_gp->UsesStreams = src->Geom.UsesStreams;
break;
}
case MESA_SHADER_FRAGMENT: {
struct gl_fragment_program *dst_fp = (struct gl_fragment_program *) dst;
dst_fp->FragDepthLayout = src->FragDepthLayout;
break;
}
case MESA_SHADER_COMPUTE: {
struct gl_compute_program *dst_cp = (struct gl_compute_program *) dst;
int i;
for (i = 0; i < 3; i++)
dst_cp->LocalSize[i] = src->Comp.LocalSize[i];
dst_cp->SharedSize = src->Comp.SharedSize;
break;
}
default:
break;
}
copy_shader_info(src, dst_sh);
}
/**
* ARB_separate_shader_objects: Compile & Link Program
*/
GLuint GLAPIENTRY
_mesa_CreateShaderProgramv(GLenum type, GLsizei count,
const GLchar* const *strings)
{
GET_CURRENT_CONTEXT(ctx);
const GLuint shader = create_shader(ctx, type);
GLuint program = 0;
/*
* According to OpenGL 4.5 and OpenGL ES 3.1 standards, section 7.3:
* GL_INVALID_VALUE should be generated if count < 0
*/
if (count < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "glCreateShaderProgram (count < 0)");
return program;
}
if (shader) {
struct gl_shader *sh = _mesa_lookup_shader(ctx, shader);
_mesa_ShaderSource(shader, count, strings, NULL);
_mesa_compile_shader(ctx, sh);
program = create_shader_program(ctx);
if (program) {
struct gl_shader_program *shProg;
GLint compiled = GL_FALSE;
shProg = _mesa_lookup_shader_program(ctx, program);
shProg->SeparateShader = GL_TRUE;
get_shaderiv(ctx, shader, GL_COMPILE_STATUS, &compiled);
if (compiled) {
attach_shader(ctx, program, shader);
_mesa_link_program(ctx, shProg);
detach_shader(ctx, program, shader);
#if 0
/* Possibly... */
if (active-user-defined-varyings-in-linked-program) {
append-error-to-info-log;
shProg->LinkStatus = GL_FALSE;
}
#endif
}
if (sh->InfoLog)
ralloc_strcat(&shProg->InfoLog, sh->InfoLog);
}
delete_shader(ctx, shader);
}
return program;
}
/**
* For GL_ARB_tessellation_shader
*/
extern void GLAPIENTRY
_mesa_PatchParameteri(GLenum pname, GLint value)
{
GET_CURRENT_CONTEXT(ctx);
if (!_mesa_has_tessellation(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glPatchParameteri");
return;
}
if (pname != GL_PATCH_VERTICES) {
_mesa_error(ctx, GL_INVALID_ENUM, "glPatchParameteri");
return;
}
if (value <= 0 || value > ctx->Const.MaxPatchVertices) {
_mesa_error(ctx, GL_INVALID_VALUE, "glPatchParameteri");
return;
}
ctx->TessCtrlProgram.patch_vertices = value;
}
extern void GLAPIENTRY
_mesa_PatchParameterfv(GLenum pname, const GLfloat *values)
{
GET_CURRENT_CONTEXT(ctx);
if (!_mesa_has_tessellation(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glPatchParameterfv");
return;
}
switch(pname) {
case GL_PATCH_DEFAULT_OUTER_LEVEL:
FLUSH_VERTICES(ctx, 0);
memcpy(ctx->TessCtrlProgram.patch_default_outer_level, values,
4 * sizeof(GLfloat));
ctx->NewDriverState |= ctx->DriverFlags.NewDefaultTessLevels;
return;
case GL_PATCH_DEFAULT_INNER_LEVEL:
FLUSH_VERTICES(ctx, 0);
memcpy(ctx->TessCtrlProgram.patch_default_inner_level, values,
2 * sizeof(GLfloat));
ctx->NewDriverState |= ctx->DriverFlags.NewDefaultTessLevels;
return;
default:
_mesa_error(ctx, GL_INVALID_ENUM, "glPatchParameterfv");
return;
}
}
/**
* ARB_shader_subroutine
*/
GLint GLAPIENTRY
_mesa_GetSubroutineUniformLocation(GLuint program, GLenum shadertype,
const GLchar *name)
{
GET_CURRENT_CONTEXT(ctx);
const char *api_name = "glGetSubroutineUniformLocation";
struct gl_shader_program *shProg;
GLenum resource_type;
gl_shader_stage stage;
if (!_mesa_has_ARB_shader_subroutine(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return -1;
}
if (!_mesa_validate_shader_target(ctx, shadertype)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return -1;
}
shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
if (!shProg)
return -1;
stage = _mesa_shader_enum_to_shader_stage(shadertype);
if (!shProg->_LinkedShaders[stage]) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return -1;
}
resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
return _mesa_program_resource_location(shProg, resource_type, name);
}
GLuint GLAPIENTRY
_mesa_GetSubroutineIndex(GLuint program, GLenum shadertype,
const GLchar *name)
{
GET_CURRENT_CONTEXT(ctx);
const char *api_name = "glGetSubroutineIndex";
struct gl_shader_program *shProg;
struct gl_program_resource *res;
GLenum resource_type;
gl_shader_stage stage;
if (!_mesa_has_ARB_shader_subroutine(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return -1;
}
if (!_mesa_validate_shader_target(ctx, shadertype)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return -1;
}
shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
if (!shProg)
return -1;
stage = _mesa_shader_enum_to_shader_stage(shadertype);
if (!shProg->_LinkedShaders[stage]) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return -1;
}
resource_type = _mesa_shader_stage_to_subroutine(stage);
res = _mesa_program_resource_find_name(shProg, resource_type, name, NULL);
if (!res) {
return -1;
}
return _mesa_program_resource_index(shProg, res);
}
GLvoid GLAPIENTRY
_mesa_GetActiveSubroutineUniformiv(GLuint program, GLenum shadertype,
GLuint index, GLenum pname, GLint *values)
{
GET_CURRENT_CONTEXT(ctx);
const char *api_name = "glGetActiveSubroutineUniformiv";
struct gl_shader_program *shProg;
struct gl_linked_shader *sh;
gl_shader_stage stage;
struct gl_program_resource *res;
const struct gl_uniform_storage *uni;
GLenum resource_type;
int count, i, j;
if (!_mesa_has_ARB_shader_subroutine(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
if (!_mesa_validate_shader_target(ctx, shadertype)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
if (!shProg)
return;
stage = _mesa_shader_enum_to_shader_stage(shadertype);
resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
sh = shProg->_LinkedShaders[stage];
if (!sh) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
if (index >= sh->NumSubroutineUniforms) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s: invalid index greater than GL_ACTIVE_SUBROUTINE_UNIFORMS", api_name);
return;
}
switch (pname) {
case GL_NUM_COMPATIBLE_SUBROUTINES: {
res = _mesa_program_resource_find_index(shProg, resource_type, index);
if (res) {
uni = res->Data;
values[0] = uni->num_compatible_subroutines;
}
break;
}
case GL_COMPATIBLE_SUBROUTINES: {
res = _mesa_program_resource_find_index(shProg, resource_type, index);
if (res) {
uni = res->Data;
count = 0;
for (i = 0; i < sh->NumSubroutineFunctions; i++) {
struct gl_subroutine_function *fn = &sh->SubroutineFunctions[i];
for (j = 0; j < fn->num_compat_types; j++) {
if (fn->types[j] == uni->type) {
values[count++] = i;
break;
}
}
}
}
break;
}
case GL_UNIFORM_SIZE:
res = _mesa_program_resource_find_index(shProg, resource_type, index);
if (res) {
uni = res->Data;
values[0] = uni->array_elements ? uni->array_elements : 1;
}
break;
case GL_UNIFORM_NAME_LENGTH:
res = _mesa_program_resource_find_index(shProg, resource_type, index);
if (res) {
values[0] = strlen(_mesa_program_resource_name(res)) + 1
+ ((_mesa_program_resource_array_size(res) != 0) ? 3 : 0);
}
break;
default:
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
}
GLvoid GLAPIENTRY
_mesa_GetActiveSubroutineUniformName(GLuint program, GLenum shadertype,
GLuint index, GLsizei bufsize,
GLsizei *length, GLchar *name)
{
GET_CURRENT_CONTEXT(ctx);
const char *api_name = "glGetActiveSubroutineUniformName";
struct gl_shader_program *shProg;
GLenum resource_type;
gl_shader_stage stage;
if (!_mesa_has_ARB_shader_subroutine(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
if (!_mesa_validate_shader_target(ctx, shadertype)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
if (!shProg)
return;
stage = _mesa_shader_enum_to_shader_stage(shadertype);
if (!shProg->_LinkedShaders[stage]) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
/* get program resource name */
_mesa_get_program_resource_name(shProg, resource_type,
index, bufsize,
length, name, api_name);
}
GLvoid GLAPIENTRY
_mesa_GetActiveSubroutineName(GLuint program, GLenum shadertype,
GLuint index, GLsizei bufsize,
GLsizei *length, GLchar *name)
{
GET_CURRENT_CONTEXT(ctx);
const char *api_name = "glGetActiveSubroutineName";
struct gl_shader_program *shProg;
GLenum resource_type;
gl_shader_stage stage;
if (!_mesa_has_ARB_shader_subroutine(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
if (!_mesa_validate_shader_target(ctx, shadertype)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
if (!shProg)
return;
stage = _mesa_shader_enum_to_shader_stage(shadertype);
if (!shProg->_LinkedShaders[stage]) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
resource_type = _mesa_shader_stage_to_subroutine(stage);
_mesa_get_program_resource_name(shProg, resource_type,
index, bufsize,
length, name, api_name);
}
GLvoid GLAPIENTRY
_mesa_UniformSubroutinesuiv(GLenum shadertype, GLsizei count,
const GLuint *indices)
{
GET_CURRENT_CONTEXT(ctx);
const char *api_name = "glUniformSubroutinesuiv";
struct gl_shader_program *shProg;
struct gl_linked_shader *sh;
gl_shader_stage stage;
int i;
if (!_mesa_has_ARB_shader_subroutine(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
if (!_mesa_validate_shader_target(ctx, shadertype)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
stage = _mesa_shader_enum_to_shader_stage(shadertype);
shProg = ctx->_Shader->CurrentProgram[stage];
if (!shProg) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
sh = shProg->_LinkedShaders[stage];
if (!sh) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
if (count != sh->NumSubroutineUniformRemapTable) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s", api_name);
return;
}
i = 0;
do {
struct gl_uniform_storage *uni = sh->SubroutineUniformRemapTable[i];
if (uni == NULL) {
i++;
continue;
}
int uni_count = uni->array_elements ? uni->array_elements : 1;
int j, k, f;
for (j = i; j < i + uni_count; j++) {
struct gl_subroutine_function *subfn = NULL;
if (indices[j] > sh->MaxSubroutineFunctionIndex) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s", api_name);
return;
}
for (f = 0; f < sh->NumSubroutineFunctions; f++) {
if (sh->SubroutineFunctions[f].index == indices[j])
subfn = &sh->SubroutineFunctions[f];
}
if (!subfn) {
continue;
}
for (k = 0; k < subfn->num_compat_types; k++) {
if (subfn->types[k] == uni->type)
break;
}
if (k == subfn->num_compat_types) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
ctx->SubroutineIndex[sh->Stage].IndexPtr[j] = indices[j];
}
i += uni_count;
} while(i < count);
FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
}
GLvoid GLAPIENTRY
_mesa_GetUniformSubroutineuiv(GLenum shadertype, GLint location,
GLuint *params)
{
GET_CURRENT_CONTEXT(ctx);
const char *api_name = "glGetUniformSubroutineuiv";
struct gl_shader_program *shProg;
struct gl_linked_shader *sh;
gl_shader_stage stage;
if (!_mesa_has_ARB_shader_subroutine(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
if (!_mesa_validate_shader_target(ctx, shadertype)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
stage = _mesa_shader_enum_to_shader_stage(shadertype);
shProg = ctx->_Shader->CurrentProgram[stage];
if (!shProg) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
sh = shProg->_LinkedShaders[stage];
if (!sh) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
if (location >= sh->NumSubroutineUniformRemapTable) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s", api_name);
return;
}
*params = ctx->SubroutineIndex[sh->Stage].IndexPtr[location];
}
GLvoid GLAPIENTRY
_mesa_GetProgramStageiv(GLuint program, GLenum shadertype,
GLenum pname, GLint *values)
{
GET_CURRENT_CONTEXT(ctx);
const char *api_name = "glGetProgramStageiv";
struct gl_shader_program *shProg;
struct gl_linked_shader *sh;
gl_shader_stage stage;
if (!_mesa_has_ARB_shader_subroutine(ctx)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
if (!_mesa_validate_shader_target(ctx, shadertype)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
return;
}
shProg = _mesa_lookup_shader_program_err(ctx, program, api_name);
if (!shProg)
return;
stage = _mesa_shader_enum_to_shader_stage(shadertype);
sh = shProg->_LinkedShaders[stage];
/* ARB_shader_subroutine doesn't ask the program to be linked, or list any
* INVALID_OPERATION in the case of not be linked.
*
* And for some pnames, like GL_ACTIVE_SUBROUTINE_UNIFORMS, you can ask the
* same info using other specs (ARB_program_interface_query), without the
* need of the program to be linked, being the value for that case 0.
*
* But at the same time, some other methods require the program to be
* linked for pname related to locations, so it would be inconsistent to
* not do the same here. So we are:
* * Return GL_INVALID_OPERATION if not linked only for locations.
* * Setting a default value of 0, to be returned if not linked.
*/
if (!sh) {
values[0] = 0;
if (pname == GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", api_name);
}
return;
}
switch (pname) {
case GL_ACTIVE_SUBROUTINES:
values[0] = sh->NumSubroutineFunctions;
break;
case GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS:
values[0] = sh->NumSubroutineUniformRemapTable;
break;
case GL_ACTIVE_SUBROUTINE_UNIFORMS:
values[0] = sh->NumSubroutineUniforms;
break;
case GL_ACTIVE_SUBROUTINE_MAX_LENGTH:
{
unsigned i;
GLint max_len = 0;
GLenum resource_type;
struct gl_program_resource *res;
resource_type = _mesa_shader_stage_to_subroutine(stage);
for (i = 0; i < sh->NumSubroutineFunctions; i++) {
res = _mesa_program_resource_find_index(shProg, resource_type, i);
if (res) {
const GLint len = strlen(_mesa_program_resource_name(res)) + 1;
if (len > max_len)
max_len = len;
}
}
values[0] = max_len;
break;
}
case GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH:
{
unsigned i;
GLint max_len = 0;
GLenum resource_type;
struct gl_program_resource *res;
resource_type = _mesa_shader_stage_to_subroutine_uniform(stage);
for (i = 0; i < sh->NumSubroutineUniformRemapTable; i++) {
res = _mesa_program_resource_find_index(shProg, resource_type, i);
if (res) {
const GLint len = strlen(_mesa_program_resource_name(res)) + 1
+ ((_mesa_program_resource_array_size(res) != 0) ? 3 : 0);
if (len > max_len)
max_len = len;
}
}
values[0] = max_len;
break;
}
default:
_mesa_error(ctx, GL_INVALID_ENUM, "%s", api_name);
values[0] = -1;
break;
}
}
static int
find_compat_subroutine(struct gl_linked_shader *sh,
const struct glsl_type *type)
{
int i, j;
for (i = 0; i < sh->NumSubroutineFunctions; i++) {
struct gl_subroutine_function *fn = &sh->SubroutineFunctions[i];
for (j = 0; j < fn->num_compat_types; j++) {
if (fn->types[j] == type)
return i;
}
}
return 0;
}
static void
_mesa_shader_write_subroutine_index(struct gl_context *ctx,
struct gl_linked_shader *sh)
{
int i, j;
if (sh->NumSubroutineUniformRemapTable == 0)
return;
i = 0;
do {
struct gl_uniform_storage *uni = sh->SubroutineUniformRemapTable[i];
int uni_count;
int val;
if (!uni) {
i++;
continue;
}
uni_count = uni->array_elements ? uni->array_elements : 1;
for (j = 0; j < uni_count; j++) {
val = ctx->SubroutineIndex[sh->Stage].IndexPtr[i + j];
memcpy(&uni->storage[j], &val, sizeof(int));
}
_mesa_propagate_uniforms_to_driver_storage(uni, 0, uni_count);
i += uni_count;
} while(i < sh->NumSubroutineUniformRemapTable);
}
void
_mesa_shader_write_subroutine_indices(struct gl_context *ctx,
gl_shader_stage stage)
{
if (ctx->_Shader->CurrentProgram[stage] &&
ctx->_Shader->CurrentProgram[stage]->_LinkedShaders[stage])
_mesa_shader_write_subroutine_index(ctx,
ctx->_Shader->CurrentProgram[stage]->_LinkedShaders[stage]);
}
static void
_mesa_shader_init_subroutine_defaults(struct gl_context *ctx,
struct gl_linked_shader *sh)
{
int i;
struct gl_subroutine_index_binding *binding = &ctx->SubroutineIndex[sh->Stage];
if (binding->NumIndex != sh->NumSubroutineUniformRemapTable) {
binding->IndexPtr = realloc(binding->IndexPtr,
sh->NumSubroutineUniformRemapTable * (sizeof(GLuint)));
binding->NumIndex = sh->NumSubroutineUniformRemapTable;
}
for (i = 0; i < sh->NumSubroutineUniformRemapTable; i++) {
struct gl_uniform_storage *uni = sh->SubroutineUniformRemapTable[i];
if (!uni)
continue;
binding->IndexPtr[i] = find_compat_subroutine(sh, uni->type);
}
}
void
_mesa_shader_program_init_subroutine_defaults(struct gl_context *ctx,
struct gl_shader_program *shProg)
{
int i;
if (!shProg)
return;
for (i = 0; i < MESA_SHADER_STAGES; i++) {
if (!shProg->_LinkedShaders[i])
continue;
_mesa_shader_init_subroutine_defaults(ctx, shProg->_LinkedShaders[i]);
}
}
|