1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
|
/*
* Mesa 3-D graphics library
* Version: 7.6
*
* Copyright (C) 2009 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
* BRIAN PAUL 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.
*/
/**
* Meta operations. Some GL operations can be expressed in terms of
* other GL operations. For example, glBlitFramebuffer() can be done
* with texture mapping and glClear() can be done with polygon rendering.
*
* \author Brian Paul
*/
#include "main/glheader.h"
#include "main/mtypes.h"
#include "main/imports.h"
#include "main/arbprogram.h"
#include "main/arrayobj.h"
#include "main/blend.h"
#include "main/bufferobj.h"
#include "main/buffers.h"
#include "main/colortab.h"
#include "main/condrender.h"
#include "main/depth.h"
#include "main/enable.h"
#include "main/fbobject.h"
#include "main/feedback.h"
#include "main/formats.h"
#include "main/glformats.h"
#include "main/image.h"
#include "main/macros.h"
#include "main/matrix.h"
#include "main/mipmap.h"
#include "main/pixel.h"
#include "main/pbo.h"
#include "main/polygon.h"
#include "main/queryobj.h"
#include "main/readpix.h"
#include "main/scissor.h"
#include "main/shaderapi.h"
#include "main/shaderobj.h"
#include "main/state.h"
#include "main/stencil.h"
#include "main/texobj.h"
#include "main/texenv.h"
#include "main/texgetimage.h"
#include "main/teximage.h"
#include "main/texparam.h"
#include "main/texstate.h"
#include "main/transformfeedback.h"
#include "main/uniforms.h"
#include "main/varray.h"
#include "main/viewport.h"
#include "main/samplerobj.h"
#include "program/program.h"
#include "swrast/swrast.h"
#include "drivers/common/meta.h"
#include "main/enums.h"
#include "main/glformats.h"
#include "../glsl/ralloc.h"
/** Return offset in bytes of the field within a vertex struct */
#define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
/**
* State which we may save/restore across meta ops.
* XXX this may be incomplete...
*/
struct save_state
{
GLbitfield SavedState; /**< bitmask of MESA_META_* flags */
/** MESA_META_CLEAR (and others?) */
struct gl_query_object *CurrentOcclusionObject;
/** MESA_META_ALPHA_TEST */
GLboolean AlphaEnabled;
GLenum AlphaFunc;
GLclampf AlphaRef;
/** MESA_META_BLEND */
GLbitfield BlendEnabled;
GLboolean ColorLogicOpEnabled;
/** MESA_META_COLOR_MASK */
GLubyte ColorMask[MAX_DRAW_BUFFERS][4];
/** MESA_META_DEPTH_TEST */
struct gl_depthbuffer_attrib Depth;
/** MESA_META_FOG */
GLboolean Fog;
/** MESA_META_PIXEL_STORE */
struct gl_pixelstore_attrib Pack, Unpack;
/** MESA_META_PIXEL_TRANSFER */
GLfloat RedBias, RedScale;
GLfloat GreenBias, GreenScale;
GLfloat BlueBias, BlueScale;
GLfloat AlphaBias, AlphaScale;
GLfloat DepthBias, DepthScale;
GLboolean MapColorFlag;
/** MESA_META_RASTERIZATION */
GLenum FrontPolygonMode, BackPolygonMode;
GLboolean PolygonOffset;
GLboolean PolygonSmooth;
GLboolean PolygonStipple;
GLboolean PolygonCull;
/** MESA_META_SCISSOR */
struct gl_scissor_attrib Scissor;
/** MESA_META_SHADER */
GLboolean VertexProgramEnabled;
struct gl_vertex_program *VertexProgram;
GLboolean FragmentProgramEnabled;
struct gl_fragment_program *FragmentProgram;
GLboolean ATIFragmentShaderEnabled;
struct gl_shader_program *VertexShader;
struct gl_shader_program *GeometryShader;
struct gl_shader_program *FragmentShader;
struct gl_shader_program *ActiveShader;
/** MESA_META_STENCIL_TEST */
struct gl_stencil_attrib Stencil;
/** MESA_META_TRANSFORM */
GLenum MatrixMode;
GLfloat ModelviewMatrix[16];
GLfloat ProjectionMatrix[16];
GLfloat TextureMatrix[16];
/** MESA_META_CLIP */
GLbitfield ClipPlanesEnabled;
/** MESA_META_TEXTURE */
GLuint ActiveUnit;
GLuint ClientActiveUnit;
/** for unit[0] only */
struct gl_texture_object *CurrentTexture[NUM_TEXTURE_TARGETS];
/** mask of TEXTURE_2D_BIT, etc */
GLbitfield TexEnabled[MAX_TEXTURE_UNITS];
GLbitfield TexGenEnabled[MAX_TEXTURE_UNITS];
GLuint EnvMode; /* unit[0] only */
/** MESA_META_VERTEX */
struct gl_array_object *ArrayObj;
struct gl_buffer_object *ArrayBufferObj;
/** MESA_META_VIEWPORT */
GLint ViewportX, ViewportY, ViewportW, ViewportH;
GLclampd DepthNear, DepthFar;
/** MESA_META_CLAMP_FRAGMENT_COLOR */
GLenum ClampFragmentColor;
/** MESA_META_CLAMP_VERTEX_COLOR */
GLenum ClampVertexColor;
/** MESA_META_CONDITIONAL_RENDER */
struct gl_query_object *CondRenderQuery;
GLenum CondRenderMode;
/** MESA_META_SELECT_FEEDBACK */
GLenum RenderMode;
struct gl_selection Select;
struct gl_feedback Feedback;
/** MESA_META_MULTISAMPLE */
GLboolean MultisampleEnabled;
/** MESA_META_FRAMEBUFFER_SRGB */
GLboolean sRGBEnabled;
/** Miscellaneous (always disabled) */
GLboolean Lighting;
GLboolean RasterDiscard;
GLboolean TransformFeedbackNeedsResume;
};
/**
* Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
* This is currently shared by all the meta ops. But we could create a
* separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
*/
struct temp_texture
{
GLuint TexObj;
GLenum Target; /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
GLsizei MinSize; /**< Min texture size to allocate */
GLsizei MaxSize; /**< Max possible texture size */
GLboolean NPOT; /**< Non-power of two size OK? */
GLsizei Width, Height; /**< Current texture size */
GLenum IntFormat;
GLfloat Sright, Ttop; /**< right, top texcoords */
};
/**
* State for glBlitFramebufer()
*/
struct blit_state
{
GLuint ArrayObj;
GLuint VBO;
GLuint DepthFP;
GLuint ShaderProg;
GLuint RectShaderProg;
struct temp_texture depthTex;
};
/**
* State for glClear()
*/
struct clear_state
{
GLuint ArrayObj;
GLuint VBO;
GLuint ShaderProg;
GLint ColorLocation;
GLuint IntegerShaderProg;
GLint IntegerColorLocation;
};
/**
* State for glCopyPixels()
*/
struct copypix_state
{
GLuint ArrayObj;
GLuint VBO;
};
/**
* State for glDrawPixels()
*/
struct drawpix_state
{
GLuint ArrayObj;
GLuint StencilFP; /**< Fragment program for drawing stencil images */
GLuint DepthFP; /**< Fragment program for drawing depth images */
};
/**
* State for glBitmap()
*/
struct bitmap_state
{
GLuint ArrayObj;
GLuint VBO;
struct temp_texture Tex; /**< separate texture from other meta ops */
};
/**
* State for GLSL texture sampler which is used to generate fragment
* shader in _mesa_meta_generate_mipmap().
*/
struct glsl_sampler {
const char *type;
const char *func;
const char *texcoords;
GLuint shader_prog;
};
/**
* State for _mesa_meta_generate_mipmap()
*/
struct gen_mipmap_state
{
GLuint ArrayObj;
GLuint VBO;
GLuint FBO;
GLuint Sampler;
GLuint ShaderProg;
struct glsl_sampler sampler_1d;
struct glsl_sampler sampler_2d;
struct glsl_sampler sampler_3d;
struct glsl_sampler sampler_cubemap;
struct glsl_sampler sampler_1d_array;
struct glsl_sampler sampler_2d_array;
};
/**
* State for texture decompression
*/
struct decompress_state
{
GLuint ArrayObj;
GLuint VBO, FBO, RBO, Sampler;
GLint Width, Height;
};
/**
* State for glDrawTex()
*/
struct drawtex_state
{
GLuint ArrayObj;
GLuint VBO;
};
#define MAX_META_OPS_DEPTH 8
/**
* All per-context meta state.
*/
struct gl_meta_state
{
/** Stack of state saved during meta-ops */
struct save_state Save[MAX_META_OPS_DEPTH];
/** Save stack depth */
GLuint SaveStackDepth;
struct temp_texture TempTex;
struct blit_state Blit; /**< For _mesa_meta_BlitFramebuffer() */
struct clear_state Clear; /**< For _mesa_meta_Clear() */
struct copypix_state CopyPix; /**< For _mesa_meta_CopyPixels() */
struct drawpix_state DrawPix; /**< For _mesa_meta_DrawPixels() */
struct bitmap_state Bitmap; /**< For _mesa_meta_Bitmap() */
struct gen_mipmap_state Mipmap; /**< For _mesa_meta_GenerateMipmap() */
struct decompress_state Decompress; /**< For texture decompression */
struct drawtex_state DrawTex; /**< For _mesa_meta_DrawTex() */
};
static void meta_glsl_blit_cleanup(struct gl_context *ctx, struct blit_state *blit);
static void cleanup_temp_texture(struct gl_context *ctx, struct temp_texture *tex);
static void meta_glsl_clear_cleanup(struct gl_context *ctx, struct clear_state *clear);
static void meta_glsl_generate_mipmap_cleanup(struct gl_context *ctx,
struct gen_mipmap_state *mipmap);
static GLuint
compile_shader_with_debug(struct gl_context *ctx, GLenum target, const GLcharARB *source)
{
GLuint shader;
GLint ok, size;
GLchar *info;
shader = _mesa_CreateShaderObjectARB(target);
_mesa_ShaderSource(shader, 1, &source, NULL);
_mesa_CompileShader(shader);
_mesa_GetShaderiv(shader, GL_COMPILE_STATUS, &ok);
if (ok)
return shader;
_mesa_GetShaderiv(shader, GL_INFO_LOG_LENGTH, &size);
if (size == 0) {
_mesa_DeleteObjectARB(shader);
return 0;
}
info = malloc(size);
if (!info) {
_mesa_DeleteObjectARB(shader);
return 0;
}
_mesa_GetProgramInfoLog(shader, size, NULL, info);
_mesa_problem(ctx,
"meta program compile failed:\n%s\n"
"source:\n%s\n",
info, source);
free(info);
_mesa_DeleteObjectARB(shader);
return 0;
}
static GLuint
link_program_with_debug(struct gl_context *ctx, GLuint program)
{
GLint ok, size;
GLchar *info;
_mesa_LinkProgram(program);
_mesa_GetProgramiv(program, GL_LINK_STATUS, &ok);
if (ok)
return program;
_mesa_GetProgramiv(program, GL_INFO_LOG_LENGTH, &size);
if (size == 0)
return 0;
info = malloc(size);
if (!info)
return 0;
_mesa_GetProgramInfoLog(program, size, NULL, info);
_mesa_problem(ctx, "meta program link failed:\n%s", info);
free(info);
return 0;
}
/**
* Initialize meta-ops for a context.
* To be called once during context creation.
*/
void
_mesa_meta_init(struct gl_context *ctx)
{
ASSERT(!ctx->Meta);
ctx->Meta = CALLOC_STRUCT(gl_meta_state);
}
/**
* Free context meta-op state.
* To be called once during context destruction.
*/
void
_mesa_meta_free(struct gl_context *ctx)
{
GET_CURRENT_CONTEXT(old_context);
_mesa_make_current(ctx, NULL, NULL);
meta_glsl_blit_cleanup(ctx, &ctx->Meta->Blit);
meta_glsl_clear_cleanup(ctx, &ctx->Meta->Clear);
meta_glsl_generate_mipmap_cleanup(ctx, &ctx->Meta->Mipmap);
cleanup_temp_texture(ctx, &ctx->Meta->TempTex);
if (old_context)
_mesa_make_current(old_context, old_context->WinSysDrawBuffer, old_context->WinSysReadBuffer);
else
_mesa_make_current(NULL, NULL, NULL);
free(ctx->Meta);
ctx->Meta = NULL;
}
/**
* Enter meta state. This is like a light-weight version of glPushAttrib
* but it also resets most GL state back to default values.
*
* \param state bitmask of MESA_META_* flags indicating which attribute groups
* to save and reset to their defaults
*/
void
_mesa_meta_begin(struct gl_context *ctx, GLbitfield state)
{
struct save_state *save;
/* hope MAX_META_OPS_DEPTH is large enough */
assert(ctx->Meta->SaveStackDepth < MAX_META_OPS_DEPTH);
save = &ctx->Meta->Save[ctx->Meta->SaveStackDepth++];
memset(save, 0, sizeof(*save));
save->SavedState = state;
/* Pausing transform feedback needs to be done early, or else we won't be
* able to change other state.
*/
save->TransformFeedbackNeedsResume =
_mesa_is_xfb_active_and_unpaused(ctx);
if (save->TransformFeedbackNeedsResume)
_mesa_PauseTransformFeedback();
/* After saving the current occlusion object, call EndQuery so that no
* occlusion querying will be active during the meta-operation.
*/
if (state & MESA_META_OCCLUSION_QUERY) {
save->CurrentOcclusionObject = ctx->Query.CurrentOcclusionObject;
if (save->CurrentOcclusionObject)
_mesa_EndQuery(save->CurrentOcclusionObject->Target);
}
if (state & MESA_META_ALPHA_TEST) {
save->AlphaEnabled = ctx->Color.AlphaEnabled;
save->AlphaFunc = ctx->Color.AlphaFunc;
save->AlphaRef = ctx->Color.AlphaRef;
if (ctx->Color.AlphaEnabled)
_mesa_set_enable(ctx, GL_ALPHA_TEST, GL_FALSE);
}
if (state & MESA_META_BLEND) {
save->BlendEnabled = ctx->Color.BlendEnabled;
if (ctx->Color.BlendEnabled) {
if (ctx->Extensions.EXT_draw_buffers2) {
GLuint i;
for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
_mesa_set_enablei(ctx, GL_BLEND, i, GL_FALSE);
}
}
else {
_mesa_set_enable(ctx, GL_BLEND, GL_FALSE);
}
}
save->ColorLogicOpEnabled = ctx->Color.ColorLogicOpEnabled;
if (ctx->Color.ColorLogicOpEnabled)
_mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, GL_FALSE);
}
if (state & MESA_META_COLOR_MASK) {
memcpy(save->ColorMask, ctx->Color.ColorMask,
sizeof(ctx->Color.ColorMask));
if (!ctx->Color.ColorMask[0][0] ||
!ctx->Color.ColorMask[0][1] ||
!ctx->Color.ColorMask[0][2] ||
!ctx->Color.ColorMask[0][3])
_mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
}
if (state & MESA_META_DEPTH_TEST) {
save->Depth = ctx->Depth; /* struct copy */
if (ctx->Depth.Test)
_mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
}
if ((state & MESA_META_FOG)
&& ctx->API != API_OPENGL_CORE
&& ctx->API != API_OPENGLES2) {
save->Fog = ctx->Fog.Enabled;
if (ctx->Fog.Enabled)
_mesa_set_enable(ctx, GL_FOG, GL_FALSE);
}
if (state & MESA_META_PIXEL_STORE) {
save->Pack = ctx->Pack;
save->Unpack = ctx->Unpack;
ctx->Pack = ctx->DefaultPacking;
ctx->Unpack = ctx->DefaultPacking;
}
if (state & MESA_META_PIXEL_TRANSFER) {
save->RedScale = ctx->Pixel.RedScale;
save->RedBias = ctx->Pixel.RedBias;
save->GreenScale = ctx->Pixel.GreenScale;
save->GreenBias = ctx->Pixel.GreenBias;
save->BlueScale = ctx->Pixel.BlueScale;
save->BlueBias = ctx->Pixel.BlueBias;
save->AlphaScale = ctx->Pixel.AlphaScale;
save->AlphaBias = ctx->Pixel.AlphaBias;
save->MapColorFlag = ctx->Pixel.MapColorFlag;
ctx->Pixel.RedScale = 1.0F;
ctx->Pixel.RedBias = 0.0F;
ctx->Pixel.GreenScale = 1.0F;
ctx->Pixel.GreenBias = 0.0F;
ctx->Pixel.BlueScale = 1.0F;
ctx->Pixel.BlueBias = 0.0F;
ctx->Pixel.AlphaScale = 1.0F;
ctx->Pixel.AlphaBias = 0.0F;
ctx->Pixel.MapColorFlag = GL_FALSE;
/* XXX more state */
ctx->NewState |=_NEW_PIXEL;
}
if (state & MESA_META_RASTERIZATION) {
save->FrontPolygonMode = ctx->Polygon.FrontMode;
save->BackPolygonMode = ctx->Polygon.BackMode;
save->PolygonOffset = ctx->Polygon.OffsetFill;
save->PolygonSmooth = ctx->Polygon.SmoothFlag;
save->PolygonStipple = ctx->Polygon.StippleFlag;
save->PolygonCull = ctx->Polygon.CullFlag;
_mesa_PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
_mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, GL_FALSE);
if (ctx->API == API_OPENGL_COMPAT) {
_mesa_set_enable(ctx, GL_POLYGON_SMOOTH, GL_FALSE);
_mesa_set_enable(ctx, GL_POLYGON_STIPPLE, GL_FALSE);
}
_mesa_set_enable(ctx, GL_CULL_FACE, GL_FALSE);
}
if (state & MESA_META_SCISSOR) {
save->Scissor = ctx->Scissor; /* struct copy */
_mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_FALSE);
}
if (state & MESA_META_SHADER) {
if (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_vertex_program) {
save->VertexProgramEnabled = ctx->VertexProgram.Enabled;
_mesa_reference_vertprog(ctx, &save->VertexProgram,
ctx->VertexProgram.Current);
_mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB, GL_FALSE);
}
if (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_fragment_program) {
save->FragmentProgramEnabled = ctx->FragmentProgram.Enabled;
_mesa_reference_fragprog(ctx, &save->FragmentProgram,
ctx->FragmentProgram.Current);
_mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_FALSE);
}
if (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ATI_fragment_shader) {
save->ATIFragmentShaderEnabled = ctx->ATIFragmentShader.Enabled;
_mesa_set_enable(ctx, GL_FRAGMENT_SHADER_ATI, GL_FALSE);
}
if (ctx->Extensions.ARB_shader_objects) {
_mesa_reference_shader_program(ctx, &save->VertexShader,
ctx->Shader.CurrentVertexProgram);
_mesa_reference_shader_program(ctx, &save->GeometryShader,
ctx->Shader.CurrentGeometryProgram);
_mesa_reference_shader_program(ctx, &save->FragmentShader,
ctx->Shader.CurrentFragmentProgram);
_mesa_reference_shader_program(ctx, &save->ActiveShader,
ctx->Shader.ActiveProgram);
_mesa_UseProgram(0);
}
}
if (state & MESA_META_STENCIL_TEST) {
save->Stencil = ctx->Stencil; /* struct copy */
if (ctx->Stencil.Enabled)
_mesa_set_enable(ctx, GL_STENCIL_TEST, GL_FALSE);
/* NOTE: other stencil state not reset */
}
if (state & MESA_META_TEXTURE) {
GLuint u, tgt;
save->ActiveUnit = ctx->Texture.CurrentUnit;
save->ClientActiveUnit = ctx->Array.ActiveTexture;
save->EnvMode = ctx->Texture.Unit[0].EnvMode;
/* Disable all texture units */
if (ctx->API == API_OPENGL_COMPAT || ctx->API == API_OPENGLES) {
for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
save->TexEnabled[u] = ctx->Texture.Unit[u].Enabled;
save->TexGenEnabled[u] = ctx->Texture.Unit[u].TexGenEnabled;
if (ctx->Texture.Unit[u].Enabled ||
ctx->Texture.Unit[u].TexGenEnabled) {
_mesa_ActiveTexture(GL_TEXTURE0 + u);
_mesa_set_enable(ctx, GL_TEXTURE_2D, GL_FALSE);
if (ctx->Extensions.ARB_texture_cube_map)
_mesa_set_enable(ctx, GL_TEXTURE_CUBE_MAP, GL_FALSE);
if (ctx->Extensions.OES_EGL_image_external)
_mesa_set_enable(ctx, GL_TEXTURE_EXTERNAL_OES, GL_FALSE);
if (ctx->API == API_OPENGL_COMPAT) {
_mesa_set_enable(ctx, GL_TEXTURE_1D, GL_FALSE);
_mesa_set_enable(ctx, GL_TEXTURE_3D, GL_FALSE);
if (ctx->Extensions.NV_texture_rectangle)
_mesa_set_enable(ctx, GL_TEXTURE_RECTANGLE, GL_FALSE);
_mesa_set_enable(ctx, GL_TEXTURE_GEN_S, GL_FALSE);
_mesa_set_enable(ctx, GL_TEXTURE_GEN_T, GL_FALSE);
_mesa_set_enable(ctx, GL_TEXTURE_GEN_R, GL_FALSE);
_mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
} else {
_mesa_set_enable(ctx, GL_TEXTURE_GEN_STR_OES, GL_FALSE);
}
}
}
}
/* save current texture objects for unit[0] only */
for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
_mesa_reference_texobj(&save->CurrentTexture[tgt],
ctx->Texture.Unit[0].CurrentTex[tgt]);
}
/* set defaults for unit[0] */
_mesa_ActiveTexture(GL_TEXTURE0);
_mesa_ClientActiveTexture(GL_TEXTURE0);
if (ctx->API == API_OPENGL_COMPAT || ctx->API == API_OPENGLES) {
_mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}
}
if (state & MESA_META_TRANSFORM) {
GLuint activeTexture = ctx->Texture.CurrentUnit;
memcpy(save->ModelviewMatrix, ctx->ModelviewMatrixStack.Top->m,
16 * sizeof(GLfloat));
memcpy(save->ProjectionMatrix, ctx->ProjectionMatrixStack.Top->m,
16 * sizeof(GLfloat));
memcpy(save->TextureMatrix, ctx->TextureMatrixStack[0].Top->m,
16 * sizeof(GLfloat));
save->MatrixMode = ctx->Transform.MatrixMode;
/* set 1:1 vertex:pixel coordinate transform */
_mesa_ActiveTexture(GL_TEXTURE0);
_mesa_MatrixMode(GL_TEXTURE);
_mesa_LoadIdentity();
_mesa_ActiveTexture(GL_TEXTURE0 + activeTexture);
_mesa_MatrixMode(GL_MODELVIEW);
_mesa_LoadIdentity();
_mesa_MatrixMode(GL_PROJECTION);
_mesa_LoadIdentity();
_mesa_Ortho(0.0, ctx->DrawBuffer->Width,
0.0, ctx->DrawBuffer->Height,
-1.0, 1.0);
}
if (state & MESA_META_CLIP) {
save->ClipPlanesEnabled = ctx->Transform.ClipPlanesEnabled;
if (ctx->Transform.ClipPlanesEnabled) {
GLuint i;
for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
_mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_FALSE);
}
}
}
if (state & MESA_META_VERTEX) {
/* save vertex array object state */
_mesa_reference_array_object(ctx, &save->ArrayObj,
ctx->Array.ArrayObj);
_mesa_reference_buffer_object(ctx, &save->ArrayBufferObj,
ctx->Array.ArrayBufferObj);
/* set some default state? */
}
if (state & MESA_META_VIEWPORT) {
/* save viewport state */
save->ViewportX = ctx->Viewport.X;
save->ViewportY = ctx->Viewport.Y;
save->ViewportW = ctx->Viewport.Width;
save->ViewportH = ctx->Viewport.Height;
/* set viewport to match window size */
if (ctx->Viewport.X != 0 ||
ctx->Viewport.Y != 0 ||
ctx->Viewport.Width != ctx->DrawBuffer->Width ||
ctx->Viewport.Height != ctx->DrawBuffer->Height) {
_mesa_set_viewport(ctx, 0, 0,
ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
}
/* save depth range state */
save->DepthNear = ctx->Viewport.Near;
save->DepthFar = ctx->Viewport.Far;
/* set depth range to default */
_mesa_DepthRange(0.0, 1.0);
}
if (state & MESA_META_CLAMP_FRAGMENT_COLOR) {
save->ClampFragmentColor = ctx->Color.ClampFragmentColor;
/* Generally in here we want to do clamping according to whether
* it's for the pixel path (ClampFragmentColor is GL_TRUE),
* regardless of the internal implementation of the metaops.
*/
if (ctx->Color.ClampFragmentColor != GL_TRUE &&
ctx->Extensions.ARB_color_buffer_float)
_mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
}
if (state & MESA_META_CLAMP_VERTEX_COLOR) {
save->ClampVertexColor = ctx->Light.ClampVertexColor;
/* Generally in here we never want vertex color clamping --
* result clamping is only dependent on fragment clamping.
*/
if (ctx->Extensions.ARB_color_buffer_float)
_mesa_ClampColor(GL_CLAMP_VERTEX_COLOR, GL_FALSE);
}
if (state & MESA_META_CONDITIONAL_RENDER) {
save->CondRenderQuery = ctx->Query.CondRenderQuery;
save->CondRenderMode = ctx->Query.CondRenderMode;
if (ctx->Query.CondRenderQuery)
_mesa_EndConditionalRender();
}
if (state & MESA_META_SELECT_FEEDBACK) {
save->RenderMode = ctx->RenderMode;
if (ctx->RenderMode == GL_SELECT) {
save->Select = ctx->Select; /* struct copy */
_mesa_RenderMode(GL_RENDER);
} else if (ctx->RenderMode == GL_FEEDBACK) {
save->Feedback = ctx->Feedback; /* struct copy */
_mesa_RenderMode(GL_RENDER);
}
}
if (state & MESA_META_MULTISAMPLE) {
save->MultisampleEnabled = ctx->Multisample.Enabled;
if (ctx->Multisample.Enabled)
_mesa_set_multisample(ctx, GL_FALSE);
}
if (state & MESA_META_FRAMEBUFFER_SRGB) {
save->sRGBEnabled = ctx->Color.sRGBEnabled;
if (ctx->Color.sRGBEnabled)
_mesa_set_framebuffer_srgb(ctx, GL_FALSE);
}
/* misc */
{
save->Lighting = ctx->Light.Enabled;
if (ctx->Light.Enabled)
_mesa_set_enable(ctx, GL_LIGHTING, GL_FALSE);
save->RasterDiscard = ctx->RasterDiscard;
if (ctx->RasterDiscard)
_mesa_set_enable(ctx, GL_RASTERIZER_DISCARD, GL_FALSE);
}
}
/**
* Leave meta state. This is like a light-weight version of glPopAttrib().
*/
void
_mesa_meta_end(struct gl_context *ctx)
{
struct save_state *save = &ctx->Meta->Save[ctx->Meta->SaveStackDepth - 1];
const GLbitfield state = save->SavedState;
/* After starting a new occlusion query, initialize the results to the
* values saved previously. The driver will then continue to increment
* these values.
*/
if (state & MESA_META_OCCLUSION_QUERY) {
if (save->CurrentOcclusionObject) {
_mesa_BeginQuery(save->CurrentOcclusionObject->Target,
save->CurrentOcclusionObject->Id);
ctx->Query.CurrentOcclusionObject->Result = save->CurrentOcclusionObject->Result;
}
}
if (state & MESA_META_ALPHA_TEST) {
if (ctx->Color.AlphaEnabled != save->AlphaEnabled)
_mesa_set_enable(ctx, GL_ALPHA_TEST, save->AlphaEnabled);
_mesa_AlphaFunc(save->AlphaFunc, save->AlphaRef);
}
if (state & MESA_META_BLEND) {
if (ctx->Color.BlendEnabled != save->BlendEnabled) {
if (ctx->Extensions.EXT_draw_buffers2) {
GLuint i;
for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
_mesa_set_enablei(ctx, GL_BLEND, i, (save->BlendEnabled >> i) & 1);
}
}
else {
_mesa_set_enable(ctx, GL_BLEND, (save->BlendEnabled & 1));
}
}
if (ctx->Color.ColorLogicOpEnabled != save->ColorLogicOpEnabled)
_mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, save->ColorLogicOpEnabled);
}
if (state & MESA_META_COLOR_MASK) {
GLuint i;
for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
if (!TEST_EQ_4V(ctx->Color.ColorMask[i], save->ColorMask[i])) {
if (i == 0) {
_mesa_ColorMask(save->ColorMask[i][0], save->ColorMask[i][1],
save->ColorMask[i][2], save->ColorMask[i][3]);
}
else {
_mesa_ColorMaski(i,
save->ColorMask[i][0],
save->ColorMask[i][1],
save->ColorMask[i][2],
save->ColorMask[i][3]);
}
}
}
}
if (state & MESA_META_DEPTH_TEST) {
if (ctx->Depth.Test != save->Depth.Test)
_mesa_set_enable(ctx, GL_DEPTH_TEST, save->Depth.Test);
_mesa_DepthFunc(save->Depth.Func);
_mesa_DepthMask(save->Depth.Mask);
}
if ((state & MESA_META_FOG)
&& ctx->API != API_OPENGL_CORE
&& ctx->API != API_OPENGLES2) {
_mesa_set_enable(ctx, GL_FOG, save->Fog);
}
if (state & MESA_META_PIXEL_STORE) {
ctx->Pack = save->Pack;
ctx->Unpack = save->Unpack;
}
if (state & MESA_META_PIXEL_TRANSFER) {
ctx->Pixel.RedScale = save->RedScale;
ctx->Pixel.RedBias = save->RedBias;
ctx->Pixel.GreenScale = save->GreenScale;
ctx->Pixel.GreenBias = save->GreenBias;
ctx->Pixel.BlueScale = save->BlueScale;
ctx->Pixel.BlueBias = save->BlueBias;
ctx->Pixel.AlphaScale = save->AlphaScale;
ctx->Pixel.AlphaBias = save->AlphaBias;
ctx->Pixel.MapColorFlag = save->MapColorFlag;
/* XXX more state */
ctx->NewState |=_NEW_PIXEL;
}
if (state & MESA_META_RASTERIZATION) {
/* Core context requires that front and back mode be the same.
*/
if (ctx->API == API_OPENGL_CORE) {
_mesa_PolygonMode(GL_FRONT_AND_BACK, save->FrontPolygonMode);
} else {
_mesa_PolygonMode(GL_FRONT, save->FrontPolygonMode);
_mesa_PolygonMode(GL_BACK, save->BackPolygonMode);
}
if (ctx->API == API_OPENGL_COMPAT) {
_mesa_set_enable(ctx, GL_POLYGON_STIPPLE, save->PolygonStipple);
_mesa_set_enable(ctx, GL_POLYGON_SMOOTH, save->PolygonSmooth);
}
_mesa_set_enable(ctx, GL_POLYGON_OFFSET_FILL, save->PolygonOffset);
_mesa_set_enable(ctx, GL_CULL_FACE, save->PolygonCull);
}
if (state & MESA_META_SCISSOR) {
_mesa_set_enable(ctx, GL_SCISSOR_TEST, save->Scissor.Enabled);
_mesa_Scissor(save->Scissor.X, save->Scissor.Y,
save->Scissor.Width, save->Scissor.Height);
}
if (state & MESA_META_SHADER) {
if (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_vertex_program) {
_mesa_set_enable(ctx, GL_VERTEX_PROGRAM_ARB,
save->VertexProgramEnabled);
_mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
save->VertexProgram);
_mesa_reference_vertprog(ctx, &save->VertexProgram, NULL);
}
if (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_fragment_program) {
_mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB,
save->FragmentProgramEnabled);
_mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
save->FragmentProgram);
_mesa_reference_fragprog(ctx, &save->FragmentProgram, NULL);
}
if (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ATI_fragment_shader) {
_mesa_set_enable(ctx, GL_FRAGMENT_SHADER_ATI,
save->ATIFragmentShaderEnabled);
}
if (ctx->Extensions.ARB_vertex_shader)
_mesa_use_shader_program(ctx, GL_VERTEX_SHADER, save->VertexShader);
if (ctx->Extensions.ARB_geometry_shader4)
_mesa_use_shader_program(ctx, GL_GEOMETRY_SHADER_ARB,
save->GeometryShader);
if (ctx->Extensions.ARB_fragment_shader)
_mesa_use_shader_program(ctx, GL_FRAGMENT_SHADER,
save->FragmentShader);
_mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram,
save->ActiveShader);
_mesa_reference_shader_program(ctx, &save->VertexShader, NULL);
_mesa_reference_shader_program(ctx, &save->GeometryShader, NULL);
_mesa_reference_shader_program(ctx, &save->FragmentShader, NULL);
_mesa_reference_shader_program(ctx, &save->ActiveShader, NULL);
}
if (state & MESA_META_STENCIL_TEST) {
const struct gl_stencil_attrib *stencil = &save->Stencil;
_mesa_set_enable(ctx, GL_STENCIL_TEST, stencil->Enabled);
_mesa_ClearStencil(stencil->Clear);
if (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.EXT_stencil_two_side) {
_mesa_set_enable(ctx, GL_STENCIL_TEST_TWO_SIDE_EXT,
stencil->TestTwoSide);
_mesa_ActiveStencilFaceEXT(stencil->ActiveFace
? GL_BACK : GL_FRONT);
}
/* front state */
_mesa_StencilFuncSeparate(GL_FRONT,
stencil->Function[0],
stencil->Ref[0],
stencil->ValueMask[0]);
_mesa_StencilMaskSeparate(GL_FRONT, stencil->WriteMask[0]);
_mesa_StencilOpSeparate(GL_FRONT, stencil->FailFunc[0],
stencil->ZFailFunc[0],
stencil->ZPassFunc[0]);
/* back state */
_mesa_StencilFuncSeparate(GL_BACK,
stencil->Function[1],
stencil->Ref[1],
stencil->ValueMask[1]);
_mesa_StencilMaskSeparate(GL_BACK, stencil->WriteMask[1]);
_mesa_StencilOpSeparate(GL_BACK, stencil->FailFunc[1],
stencil->ZFailFunc[1],
stencil->ZPassFunc[1]);
}
if (state & MESA_META_TEXTURE) {
GLuint u, tgt;
ASSERT(ctx->Texture.CurrentUnit == 0);
/* restore texenv for unit[0] */
if (ctx->API == API_OPENGL_COMPAT || ctx->API == API_OPENGLES) {
_mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, save->EnvMode);
}
/* restore texture objects for unit[0] only */
for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
if (ctx->Texture.Unit[0].CurrentTex[tgt] != save->CurrentTexture[tgt]) {
FLUSH_VERTICES(ctx, _NEW_TEXTURE);
_mesa_reference_texobj(&ctx->Texture.Unit[0].CurrentTex[tgt],
save->CurrentTexture[tgt]);
}
_mesa_reference_texobj(&save->CurrentTexture[tgt], NULL);
}
/* Restore fixed function texture enables, texgen */
if (ctx->API == API_OPENGL_COMPAT || ctx->API == API_OPENGLES) {
for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
if (ctx->Texture.Unit[u].Enabled != save->TexEnabled[u]) {
FLUSH_VERTICES(ctx, _NEW_TEXTURE);
ctx->Texture.Unit[u].Enabled = save->TexEnabled[u];
}
if (ctx->Texture.Unit[u].TexGenEnabled != save->TexGenEnabled[u]) {
FLUSH_VERTICES(ctx, _NEW_TEXTURE);
ctx->Texture.Unit[u].TexGenEnabled = save->TexGenEnabled[u];
}
}
}
/* restore current unit state */
_mesa_ActiveTexture(GL_TEXTURE0 + save->ActiveUnit);
_mesa_ClientActiveTexture(GL_TEXTURE0 + save->ClientActiveUnit);
}
if (state & MESA_META_TRANSFORM) {
GLuint activeTexture = ctx->Texture.CurrentUnit;
_mesa_ActiveTexture(GL_TEXTURE0);
_mesa_MatrixMode(GL_TEXTURE);
_mesa_LoadMatrixf(save->TextureMatrix);
_mesa_ActiveTexture(GL_TEXTURE0 + activeTexture);
_mesa_MatrixMode(GL_MODELVIEW);
_mesa_LoadMatrixf(save->ModelviewMatrix);
_mesa_MatrixMode(GL_PROJECTION);
_mesa_LoadMatrixf(save->ProjectionMatrix);
_mesa_MatrixMode(save->MatrixMode);
}
if (state & MESA_META_CLIP) {
if (save->ClipPlanesEnabled) {
GLuint i;
for (i = 0; i < ctx->Const.MaxClipPlanes; i++) {
if (save->ClipPlanesEnabled & (1 << i)) {
_mesa_set_enable(ctx, GL_CLIP_PLANE0 + i, GL_TRUE);
}
}
}
}
if (state & MESA_META_VERTEX) {
/* restore vertex buffer object */
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, save->ArrayBufferObj->Name);
_mesa_reference_buffer_object(ctx, &save->ArrayBufferObj, NULL);
/* restore vertex array object */
_mesa_BindVertexArray(save->ArrayObj->Name);
_mesa_reference_array_object(ctx, &save->ArrayObj, NULL);
}
if (state & MESA_META_VIEWPORT) {
if (save->ViewportX != ctx->Viewport.X ||
save->ViewportY != ctx->Viewport.Y ||
save->ViewportW != ctx->Viewport.Width ||
save->ViewportH != ctx->Viewport.Height) {
_mesa_set_viewport(ctx, save->ViewportX, save->ViewportY,
save->ViewportW, save->ViewportH);
}
_mesa_DepthRange(save->DepthNear, save->DepthFar);
}
if (state & MESA_META_CLAMP_FRAGMENT_COLOR &&
ctx->Extensions.ARB_color_buffer_float) {
_mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, save->ClampFragmentColor);
}
if (state & MESA_META_CLAMP_VERTEX_COLOR &&
ctx->Extensions.ARB_color_buffer_float) {
_mesa_ClampColor(GL_CLAMP_VERTEX_COLOR, save->ClampVertexColor);
}
if (state & MESA_META_CONDITIONAL_RENDER) {
if (save->CondRenderQuery)
_mesa_BeginConditionalRender(save->CondRenderQuery->Id,
save->CondRenderMode);
}
if (state & MESA_META_SELECT_FEEDBACK) {
if (save->RenderMode == GL_SELECT) {
_mesa_RenderMode(GL_SELECT);
ctx->Select = save->Select;
} else if (save->RenderMode == GL_FEEDBACK) {
_mesa_RenderMode(GL_FEEDBACK);
ctx->Feedback = save->Feedback;
}
}
if (state & MESA_META_MULTISAMPLE) {
if (ctx->Multisample.Enabled != save->MultisampleEnabled)
_mesa_set_multisample(ctx, save->MultisampleEnabled);
}
if (state & MESA_META_FRAMEBUFFER_SRGB) {
if (ctx->Color.sRGBEnabled != save->sRGBEnabled)
_mesa_set_framebuffer_srgb(ctx, save->sRGBEnabled);
}
/* misc */
if (save->Lighting) {
_mesa_set_enable(ctx, GL_LIGHTING, GL_TRUE);
}
if (save->RasterDiscard) {
_mesa_set_enable(ctx, GL_RASTERIZER_DISCARD, GL_TRUE);
}
if (save->TransformFeedbackNeedsResume)
_mesa_ResumeTransformFeedback();
ctx->Meta->SaveStackDepth--;
}
/**
* Determine whether Mesa is currently in a meta state.
*/
GLboolean
_mesa_meta_in_progress(struct gl_context *ctx)
{
return ctx->Meta->SaveStackDepth != 0;
}
/**
* Convert Z from a normalized value in the range [0, 1] to an object-space
* Z coordinate in [-1, +1] so that drawing at the new Z position with the
* default/identity ortho projection results in the original Z value.
* Used by the meta-Clear, Draw/CopyPixels and Bitmap functions where the Z
* value comes from the clear value or raster position.
*/
static INLINE GLfloat
invert_z(GLfloat normZ)
{
GLfloat objZ = 1.0f - 2.0f * normZ;
return objZ;
}
/**
* One-time init for a temp_texture object.
* Choose tex target, compute max tex size, etc.
*/
static void
init_temp_texture(struct gl_context *ctx, struct temp_texture *tex)
{
/* prefer texture rectangle */
if (_mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_texture_rectangle) {
tex->Target = GL_TEXTURE_RECTANGLE;
tex->MaxSize = ctx->Const.MaxTextureRectSize;
tex->NPOT = GL_TRUE;
}
else {
/* use 2D texture, NPOT if possible */
tex->Target = GL_TEXTURE_2D;
tex->MaxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
tex->NPOT = ctx->Extensions.ARB_texture_non_power_of_two;
}
tex->MinSize = 16; /* 16 x 16 at least */
assert(tex->MaxSize > 0);
_mesa_GenTextures(1, &tex->TexObj);
}
static void
cleanup_temp_texture(struct gl_context *ctx, struct temp_texture *tex)
{
if (!tex->TexObj)
return;
_mesa_DeleteTextures(1, &tex->TexObj);
tex->TexObj = 0;
}
/**
* Return pointer to temp_texture info for non-bitmap ops.
* This does some one-time init if needed.
*/
static struct temp_texture *
get_temp_texture(struct gl_context *ctx)
{
struct temp_texture *tex = &ctx->Meta->TempTex;
if (!tex->TexObj) {
init_temp_texture(ctx, tex);
}
return tex;
}
/**
* Return pointer to temp_texture info for _mesa_meta_bitmap().
* We use a separate texture for bitmaps to reduce texture
* allocation/deallocation.
*/
static struct temp_texture *
get_bitmap_temp_texture(struct gl_context *ctx)
{
struct temp_texture *tex = &ctx->Meta->Bitmap.Tex;
if (!tex->TexObj) {
init_temp_texture(ctx, tex);
}
return tex;
}
/**
* Return pointer to depth temp_texture.
* This does some one-time init if needed.
*/
static struct temp_texture *
get_temp_depth_texture(struct gl_context *ctx)
{
struct temp_texture *tex = &ctx->Meta->Blit.depthTex;
if (!tex->TexObj) {
init_temp_texture(ctx, tex);
}
return tex;
}
/**
* Compute the width/height of texture needed to draw an image of the
* given size. Return a flag indicating whether the current texture
* can be re-used (glTexSubImage2D) or if a new texture needs to be
* allocated (glTexImage2D).
* Also, compute s/t texcoords for drawing.
*
* \return GL_TRUE if new texture is needed, GL_FALSE otherwise
*/
static GLboolean
alloc_texture(struct temp_texture *tex,
GLsizei width, GLsizei height, GLenum intFormat)
{
GLboolean newTex = GL_FALSE;
ASSERT(width <= tex->MaxSize);
ASSERT(height <= tex->MaxSize);
if (width > tex->Width ||
height > tex->Height ||
intFormat != tex->IntFormat) {
/* alloc new texture (larger or different format) */
if (tex->NPOT) {
/* use non-power of two size */
tex->Width = MAX2(tex->MinSize, width);
tex->Height = MAX2(tex->MinSize, height);
}
else {
/* find power of two size */
GLsizei w, h;
w = h = tex->MinSize;
while (w < width)
w *= 2;
while (h < height)
h *= 2;
tex->Width = w;
tex->Height = h;
}
tex->IntFormat = intFormat;
newTex = GL_TRUE;
}
/* compute texcoords */
if (tex->Target == GL_TEXTURE_RECTANGLE) {
tex->Sright = (GLfloat) width;
tex->Ttop = (GLfloat) height;
}
else {
tex->Sright = (GLfloat) width / tex->Width;
tex->Ttop = (GLfloat) height / tex->Height;
}
return newTex;
}
/**
* Setup/load texture for glCopyPixels or glBlitFramebuffer.
*/
static void
setup_copypix_texture(struct gl_context *ctx,
struct temp_texture *tex,
GLboolean newTex,
GLint srcX, GLint srcY,
GLsizei width, GLsizei height, GLenum intFormat,
GLenum filter)
{
_mesa_BindTexture(tex->Target, tex->TexObj);
_mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, filter);
_mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, filter);
if (ctx->API == API_OPENGL_COMPAT || ctx->API == API_OPENGLES)
_mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
/* copy framebuffer image to texture */
if (newTex) {
/* create new tex image */
if (tex->Width == width && tex->Height == height) {
/* create new tex with framebuffer data */
_mesa_CopyTexImage2D(tex->Target, 0, tex->IntFormat,
srcX, srcY, width, height, 0);
}
else {
/* create empty texture */
_mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
tex->Width, tex->Height, 0,
intFormat, GL_UNSIGNED_BYTE, NULL);
/* load image */
_mesa_CopyTexSubImage2D(tex->Target, 0,
0, 0, srcX, srcY, width, height);
}
}
else {
/* replace existing tex image */
_mesa_CopyTexSubImage2D(tex->Target, 0,
0, 0, srcX, srcY, width, height);
}
}
/**
* Setup/load texture for glDrawPixels.
*/
static void
setup_drawpix_texture(struct gl_context *ctx,
struct temp_texture *tex,
GLboolean newTex,
GLenum texIntFormat,
GLsizei width, GLsizei height,
GLenum format, GLenum type,
const GLvoid *pixels)
{
_mesa_BindTexture(tex->Target, tex->TexObj);
_mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
_mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
if (ctx->API == API_OPENGL_COMPAT || ctx->API == API_OPENGLES)
_mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
/* copy pixel data to texture */
if (newTex) {
/* create new tex image */
if (tex->Width == width && tex->Height == height) {
/* create new tex and load image data */
_mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
tex->Width, tex->Height, 0, format, type, pixels);
}
else {
struct gl_buffer_object *save_unpack_obj = NULL;
_mesa_reference_buffer_object(ctx, &save_unpack_obj,
ctx->Unpack.BufferObj);
_mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
/* create empty texture */
_mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
tex->Width, tex->Height, 0, format, type, NULL);
if (save_unpack_obj != NULL)
_mesa_BindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB,
save_unpack_obj->Name);
/* load image */
_mesa_TexSubImage2D(tex->Target, 0,
0, 0, width, height, format, type, pixels);
}
}
else {
/* replace existing tex image */
_mesa_TexSubImage2D(tex->Target, 0,
0, 0, width, height, format, type, pixels);
}
}
/**
* One-time init for drawing depth pixels.
*/
static void
init_blit_depth_pixels(struct gl_context *ctx)
{
static const char *program =
"!!ARBfp1.0\n"
"TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
"END \n";
char program2[200];
struct blit_state *blit = &ctx->Meta->Blit;
struct temp_texture *tex = get_temp_texture(ctx);
const char *texTarget;
assert(blit->DepthFP == 0);
/* replace %s with "RECT" or "2D" */
assert(strlen(program) + 4 < sizeof(program2));
if (tex->Target == GL_TEXTURE_RECTANGLE)
texTarget = "RECT";
else
texTarget = "2D";
_mesa_snprintf(program2, sizeof(program2), program, texTarget);
_mesa_GenProgramsARB(1, &blit->DepthFP);
_mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
_mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
strlen(program2), (const GLubyte *) program2);
}
static void
setup_ff_blit_framebuffer(struct gl_context *ctx,
struct blit_state *blit)
{
struct vertex {
GLfloat x, y, s, t;
};
struct vertex verts[4];
if (blit->ArrayObj == 0) {
/* one-time setup */
/* create vertex array object */
_mesa_GenVertexArrays(1, &blit->ArrayObj);
_mesa_BindVertexArray(blit->ArrayObj);
/* create vertex array buffer */
_mesa_GenBuffers(1, &blit->VBO);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, blit->VBO);
_mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
NULL, GL_DYNAMIC_DRAW_ARB);
/* setup vertex arrays */
_mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
_mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
_mesa_EnableClientState(GL_VERTEX_ARRAY);
_mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
}
/* setup projection matrix */
_mesa_MatrixMode(GL_PROJECTION);
_mesa_LoadIdentity();
_mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
static void
setup_glsl_blit_framebuffer(struct gl_context *ctx,
struct blit_state *blit,
GLenum target)
{
struct vertex {
GLfloat x, y, s, t;
};
struct vertex verts[4];
const char *vs_source;
char *fs_source;
GLuint vs, fs;
void *mem_ctx;
GLuint ShaderProg;
GLboolean texture_2d = (target == GL_TEXTURE_2D);
/* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
assert(_mesa_is_desktop_gl(ctx) || texture_2d);
/* Check if already initialized */
if (blit->ArrayObj == 0) {
/* create vertex array object */
_mesa_GenVertexArrays(1, &blit->ArrayObj);
_mesa_BindVertexArray(blit->ArrayObj);
/* create vertex array buffer */
_mesa_GenBuffers(1, &blit->VBO);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, blit->VBO);
_mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
NULL, GL_DYNAMIC_DRAW_ARB);
/* setup vertex arrays */
_mesa_VertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,
sizeof(struct vertex), OFFSET(x));
_mesa_VertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
sizeof(struct vertex), OFFSET(s));
}
/* Generate a relevant fragment shader program for the texture target */
if ((target == GL_TEXTURE_2D && blit->ShaderProg != 0) ||
(target == GL_TEXTURE_RECTANGLE && blit->RectShaderProg != 0)) {
return;
}
mem_ctx = ralloc_context(NULL);
if (ctx->Const.GLSLVersion < 130) {
vs_source =
"attribute vec2 position;\n"
"attribute vec2 textureCoords;\n"
"varying vec2 texCoords;\n"
"void main()\n"
"{\n"
" texCoords = textureCoords;\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n";
fs_source = ralloc_asprintf(mem_ctx,
"uniform %s texSampler;\n"
"varying vec2 texCoords;\n"
"void main()\n"
"{\n"
" gl_FragColor = %s(texSampler, texCoords);\n"
" gl_FragDepth = gl_FragColor.r;\n"
"}\n",
texture_2d ? "sampler2D" : "sampler2DRect",
texture_2d ? "texture2D" : "texture2DRect");
}
else {
vs_source = ralloc_asprintf(mem_ctx,
"#version %s\n"
"in vec2 position;\n"
"in vec2 textureCoords;\n"
"out vec2 texCoords;\n"
"void main()\n"
"{\n"
" texCoords = textureCoords;\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n",
_mesa_is_desktop_gl(ctx) ? "130" : "300 es");
fs_source = ralloc_asprintf(mem_ctx,
"#version %s\n"
"uniform %s texSampler;\n"
"in vec2 texCoords;\n"
"out vec4 out_color;\n"
"\n"
"void main()\n"
"{\n"
" out_color = %s(texSampler, texCoords);\n"
" gl_FragDepth = out_color.r;\n"
"}\n",
_mesa_is_desktop_gl(ctx) ? "130" : "300 es",
texture_2d ? "sampler2D" : "sampler2DRect",
texture_2d ? "texture" : "texture2DRect");
}
vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
ShaderProg = _mesa_CreateProgramObjectARB();
_mesa_AttachShader(ShaderProg, fs);
_mesa_DeleteObjectARB(fs);
_mesa_AttachShader(ShaderProg, vs);
_mesa_DeleteObjectARB(vs);
_mesa_BindAttribLocation(ShaderProg, 0, "position");
_mesa_BindAttribLocation(ShaderProg, 1, "texcoords");
_mesa_EnableVertexAttribArray(0);
_mesa_EnableVertexAttribArray(1);
link_program_with_debug(ctx, ShaderProg);
ralloc_free(mem_ctx);
if (texture_2d)
blit->ShaderProg = ShaderProg;
else
blit->RectShaderProg = ShaderProg;
}
/**
* Try to do a glBlitFramebuffer using no-copy texturing.
* We can do this when the src renderbuffer is actually a texture.
* But if the src buffer == dst buffer we cannot do this.
*
* \return new buffer mask indicating the buffers left to blit using the
* normal path.
*/
static GLbitfield
blitframebuffer_texture(struct gl_context *ctx,
GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
GLbitfield mask, GLenum filter, GLint flipX,
GLint flipY, GLboolean glsl_version)
{
if (mask & GL_COLOR_BUFFER_BIT) {
const struct gl_framebuffer *drawFb = ctx->DrawBuffer;
const struct gl_framebuffer *readFb = ctx->ReadBuffer;
const struct gl_renderbuffer_attachment *drawAtt;
const struct gl_renderbuffer_attachment *readAtt =
&readFb->Attachment[readFb->_ColorReadBufferIndex];
if (readAtt && readAtt->Texture) {
struct blit_state *blit = &ctx->Meta->Blit;
const GLint dstX = MIN2(dstX0, dstX1);
const GLint dstY = MIN2(dstY0, dstY1);
const GLint dstW = abs(dstX1 - dstX0);
const GLint dstH = abs(dstY1 - dstY0);
const struct gl_texture_object *texObj = readAtt->Texture;
const GLuint srcLevel = readAtt->TextureLevel;
const GLint baseLevelSave = texObj->BaseLevel;
const GLint maxLevelSave = texObj->MaxLevel;
const GLenum target = texObj->Target;
GLuint sampler, samplerSave =
ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
int i;
/* Iterate through all draw buffers */
for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
int idx = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
if (idx == -1)
continue;
drawAtt = &drawFb->Attachment[idx];
if (drawAtt->Texture == readAtt->Texture) {
/* Can't use same texture as both the source and dest. We need
* to handle overlapping blits and besides, some hw may not
* support this.
*/
return mask;
}
}
if (target != GL_TEXTURE_2D && target != GL_TEXTURE_RECTANGLE_ARB) {
/* Can't handle other texture types at this time */
return mask;
}
/* Choose between glsl version and fixed function version of
* BlitFramebuffer function.
*/
if (glsl_version) {
setup_glsl_blit_framebuffer(ctx, blit, target);
if (target == GL_TEXTURE_2D)
_mesa_UseProgram(blit->ShaderProg);
else
_mesa_UseProgram(blit->RectShaderProg);
}
else {
setup_ff_blit_framebuffer(ctx, &ctx->Meta->Blit);
}
_mesa_BindVertexArray(blit->ArrayObj);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, blit->VBO);
_mesa_GenSamplers(1, &sampler);
_mesa_BindSampler(ctx->Texture.CurrentUnit, sampler);
/*
printf("Blit from texture!\n");
printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
*/
/* Prepare src texture state */
_mesa_BindTexture(target, texObj->Name);
_mesa_SamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, filter);
_mesa_SamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, filter);
if (target != GL_TEXTURE_RECTANGLE_ARB) {
_mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel);
_mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
}
_mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
_mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
/* Always do our blits with no sRGB decode or encode. Note that
* GL_FRAMEBUFFER_SRGB has already been disabled by
* _mesa_meta_begin().
*/
if (ctx->Extensions.EXT_texture_sRGB_decode) {
_mesa_SamplerParameteri(sampler, GL_TEXTURE_SRGB_DECODE_EXT,
GL_SKIP_DECODE_EXT);
}
if (ctx->API == API_OPENGL_COMPAT || ctx->API == API_OPENGLES) {
_mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
_mesa_set_enable(ctx, target, GL_TRUE);
}
/* Prepare vertex data (the VBO was previously created and bound) */
{
struct vertex {
GLfloat x, y, s, t;
};
struct vertex verts[4];
GLfloat s0, t0, s1, t1;
if (target == GL_TEXTURE_2D) {
const struct gl_texture_image *texImage
= _mesa_select_tex_image(ctx, texObj, target, srcLevel);
s0 = srcX0 / (float) texImage->Width;
s1 = srcX1 / (float) texImage->Width;
t0 = srcY0 / (float) texImage->Height;
t1 = srcY1 / (float) texImage->Height;
}
else {
assert(target == GL_TEXTURE_RECTANGLE_ARB);
s0 = srcX0;
s1 = srcX1;
t0 = srcY0;
t1 = srcY1;
}
/* setup vertex positions */
verts[0].x = -1.0F * flipX;
verts[0].y = -1.0F * flipY;
verts[1].x = 1.0F * flipX;
verts[1].y = -1.0F * flipY;
verts[2].x = 1.0F * flipX;
verts[2].y = 1.0F * flipY;
verts[3].x = -1.0F * flipX;
verts[3].y = 1.0F * flipY;
verts[0].s = s0;
verts[0].t = t0;
verts[1].s = s1;
verts[1].t = t0;
verts[2].s = s1;
verts[2].t = t1;
verts[3].s = s0;
verts[3].t = t1;
_mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
}
/* setup viewport */
_mesa_set_viewport(ctx, dstX, dstY, dstW, dstH);
_mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
_mesa_DepthMask(GL_FALSE);
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
/* Restore texture object state, the texture binding will
* be restored by _mesa_meta_end().
*/
if (target != GL_TEXTURE_RECTANGLE_ARB) {
_mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, baseLevelSave);
_mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
}
_mesa_BindSampler(ctx->Texture.CurrentUnit, samplerSave);
_mesa_DeleteSamplers(1, &sampler);
/* Done with color buffer */
mask &= ~GL_COLOR_BUFFER_BIT;
}
}
return mask;
}
/**
* Meta implementation of ctx->Driver.BlitFramebuffer() in terms
* of texture mapping and polygon rendering.
*/
void
_mesa_meta_BlitFramebuffer(struct gl_context *ctx,
GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
GLbitfield mask, GLenum filter)
{
struct blit_state *blit = &ctx->Meta->Blit;
struct temp_texture *tex = get_temp_texture(ctx);
struct temp_texture *depthTex = get_temp_depth_texture(ctx);
const GLsizei maxTexSize = tex->MaxSize;
const GLint srcX = MIN2(srcX0, srcX1);
const GLint srcY = MIN2(srcY0, srcY1);
const GLint srcW = abs(srcX1 - srcX0);
const GLint srcH = abs(srcY1 - srcY0);
const GLint dstX = MIN2(dstX0, dstX1);
const GLint dstY = MIN2(dstY0, dstY1);
const GLint dstW = abs(dstX1 - dstX0);
const GLint dstH = abs(dstY1 - dstY0);
const GLint srcFlipX = (srcX1 - srcX0) / srcW;
const GLint srcFlipY = (srcY1 - srcY0) / srcH;
const GLint dstFlipX = (dstX1 - dstX0) / dstW;
const GLint dstFlipY = (dstY1 - dstY0) / dstH;
const GLint flipX = srcFlipX * dstFlipX;
const GLint flipY = srcFlipY * dstFlipY;
struct vertex {
GLfloat x, y, s, t;
};
struct vertex verts[4];
GLboolean newTex;
const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
ctx->Extensions.ARB_fragment_shader &&
(ctx->API != API_OPENGLES);
/* In addition to falling back if the blit size is larger than the maximum
* texture size, fallback if the source is multisampled. This fallback can
* be removed once Mesa gets support ARB_texture_multisample.
*/
if (srcW > maxTexSize || srcH > maxTexSize
|| ctx->ReadBuffer->Visual.samples > 0) {
/* XXX avoid this fallback */
_swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
dstX0, dstY0, dstX1, dstY1, mask, filter);
return;
}
/* only scissor effects blit so save/clear all other relevant state */
_mesa_meta_begin(ctx, ~MESA_META_SCISSOR);
/* Try faster, direct texture approach first */
mask = blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1,
dstX0, dstY0, dstX1, dstY1, mask, filter,
dstFlipX, dstFlipY, use_glsl_version);
if (mask == 0x0) {
_mesa_meta_end(ctx);
return;
}
/* Choose between glsl version and fixed function version of
* BlitFramebuffer function.
*/
if (use_glsl_version) {
setup_glsl_blit_framebuffer(ctx, blit, tex->Target);
if (tex->Target == GL_TEXTURE_2D)
_mesa_UseProgram(blit->ShaderProg);
else
_mesa_UseProgram(blit->RectShaderProg);
}
else {
setup_ff_blit_framebuffer(ctx, blit);
}
_mesa_BindVertexArray(blit->ArrayObj);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, blit->VBO);
/* Continue with "normal" approach which involves copying the src rect
* into a temporary texture and is "blitted" by drawing a textured quad.
*/
{
/* setup vertex positions */
verts[0].x = -1.0F * flipX;
verts[0].y = -1.0F * flipY;
verts[1].x = 1.0F * flipX;
verts[1].y = -1.0F * flipY;
verts[2].x = 1.0F * flipX;
verts[2].y = 1.0F * flipY;
verts[3].x = -1.0F * flipX;
verts[3].y = 1.0F * flipY;
}
/* glEnable() in gles2 and gles3 doesn't allow GL_TEXTURE_{1D, 2D, etc.}
* tokens.
*/
if (_mesa_is_desktop_gl(ctx) || ctx->API == API_OPENGLES)
_mesa_set_enable(ctx, tex->Target, GL_TRUE);
if (mask & GL_COLOR_BUFFER_BIT) {
const struct gl_framebuffer *readFb = ctx->ReadBuffer;
const struct gl_renderbuffer *colorReadRb = readFb->_ColorReadBuffer;
const GLenum rb_base_format =
_mesa_base_tex_format(ctx, colorReadRb->InternalFormat);
newTex = alloc_texture(tex, srcW, srcH, rb_base_format);
setup_copypix_texture(ctx, tex, newTex, srcX, srcY, srcW, srcH,
rb_base_format, filter);
/* texcoords (after texture allocation!) */
{
verts[0].s = 0.0F;
verts[0].t = 0.0F;
verts[1].s = tex->Sright;
verts[1].t = 0.0F;
verts[2].s = tex->Sright;
verts[2].t = tex->Ttop;
verts[3].s = 0.0F;
verts[3].t = tex->Ttop;
/* upload new vertex data */
_mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
}
_mesa_set_viewport(ctx, dstX, dstY, dstW, dstH);
_mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
_mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
_mesa_DepthMask(GL_FALSE);
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
mask &= ~GL_COLOR_BUFFER_BIT;
}
if ((mask & GL_DEPTH_BUFFER_BIT) &&
_mesa_is_desktop_gl(ctx) &&
ctx->Extensions.ARB_depth_texture &&
ctx->Extensions.ARB_fragment_program) {
GLuint *tmp = malloc(srcW * srcH * sizeof(GLuint));
if (tmp) {
newTex = alloc_texture(depthTex, srcW, srcH, GL_DEPTH_COMPONENT);
_mesa_ReadPixels(srcX, srcY, srcW, srcH, GL_DEPTH_COMPONENT,
GL_UNSIGNED_INT, tmp);
setup_drawpix_texture(ctx, depthTex, newTex, GL_DEPTH_COMPONENT,
srcW, srcH, GL_DEPTH_COMPONENT,
GL_UNSIGNED_INT, tmp);
/* texcoords (after texture allocation!) */
{
verts[0].s = 0.0F;
verts[0].t = 0.0F;
verts[1].s = depthTex->Sright;
verts[1].t = 0.0F;
verts[2].s = depthTex->Sright;
verts[2].t = depthTex->Ttop;
verts[3].s = 0.0F;
verts[3].t = depthTex->Ttop;
/* upload new vertex data */
_mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
}
if (!blit->DepthFP)
init_blit_depth_pixels(ctx);
_mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
_mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
_mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
_mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
_mesa_DepthFunc(GL_ALWAYS);
_mesa_DepthMask(GL_TRUE);
_mesa_set_viewport(ctx, dstX, dstY, dstW, dstH);
_mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
mask &= ~GL_DEPTH_BUFFER_BIT;
free(tmp);
}
}
if (mask & GL_STENCIL_BUFFER_BIT) {
/* XXX can't easily do stencil */
}
if (_mesa_is_desktop_gl(ctx) || ctx->API == API_OPENGLES)
_mesa_set_enable(ctx, tex->Target, GL_FALSE);
_mesa_meta_end(ctx);
if (mask) {
_swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
dstX0, dstY0, dstX1, dstY1, mask, filter);
}
}
static void
meta_glsl_blit_cleanup(struct gl_context *ctx, struct blit_state *blit)
{
if (blit->ArrayObj) {
_mesa_DeleteVertexArrays(1, &blit->ArrayObj);
blit->ArrayObj = 0;
_mesa_DeleteBuffers(1, &blit->VBO);
blit->VBO = 0;
}
if (blit->DepthFP) {
_mesa_DeleteProgramsARB(1, &blit->DepthFP);
blit->DepthFP = 0;
}
_mesa_DeleteObjectARB(blit->ShaderProg);
blit->ShaderProg = 0;
_mesa_DeleteObjectARB(blit->RectShaderProg);
blit->RectShaderProg = 0;
_mesa_DeleteTextures(1, &blit->depthTex.TexObj);
blit->depthTex.TexObj = 0;
}
/**
* Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
*/
void
_mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers)
{
struct clear_state *clear = &ctx->Meta->Clear;
struct vertex {
GLfloat x, y, z, r, g, b, a;
};
struct vertex verts[4];
/* save all state but scissor, pixel pack/unpack */
GLbitfield metaSave = (MESA_META_ALL -
MESA_META_SCISSOR -
MESA_META_PIXEL_STORE -
MESA_META_CONDITIONAL_RENDER -
MESA_META_FRAMEBUFFER_SRGB);
const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
if (buffers & BUFFER_BITS_COLOR) {
/* if clearing color buffers, don't save/restore colormask */
metaSave -= MESA_META_COLOR_MASK;
}
_mesa_meta_begin(ctx, metaSave);
if (clear->ArrayObj == 0) {
/* one-time setup */
/* create vertex array object */
_mesa_GenVertexArrays(1, &clear->ArrayObj);
_mesa_BindVertexArray(clear->ArrayObj);
/* create vertex array buffer */
_mesa_GenBuffers(1, &clear->VBO);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, clear->VBO);
/* setup vertex arrays */
_mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
_mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
_mesa_EnableClientState(GL_VERTEX_ARRAY);
_mesa_EnableClientState(GL_COLOR_ARRAY);
}
else {
_mesa_BindVertexArray(clear->ArrayObj);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, clear->VBO);
}
/* GL_COLOR_BUFFER_BIT */
if (buffers & BUFFER_BITS_COLOR) {
/* leave colormask, glDrawBuffer state as-is */
/* Clears never have the color clamped. */
if (ctx->Extensions.ARB_color_buffer_float)
_mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
}
else {
ASSERT(metaSave & MESA_META_COLOR_MASK);
_mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
}
/* GL_DEPTH_BUFFER_BIT */
if (buffers & BUFFER_BIT_DEPTH) {
_mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
_mesa_DepthFunc(GL_ALWAYS);
_mesa_DepthMask(GL_TRUE);
}
else {
assert(!ctx->Depth.Test);
}
/* GL_STENCIL_BUFFER_BIT */
if (buffers & BUFFER_BIT_STENCIL) {
_mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
_mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
GL_REPLACE, GL_REPLACE, GL_REPLACE);
_mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
ctx->Stencil.Clear & stencilMax,
ctx->Stencil.WriteMask[0]);
}
else {
assert(!ctx->Stencil.Enabled);
}
/* vertex positions/colors */
{
const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
const GLfloat z = invert_z(ctx->Depth.Clear);
GLuint i;
verts[0].x = x0;
verts[0].y = y0;
verts[0].z = z;
verts[1].x = x1;
verts[1].y = y0;
verts[1].z = z;
verts[2].x = x1;
verts[2].y = y1;
verts[2].z = z;
verts[3].x = x0;
verts[3].y = y1;
verts[3].z = z;
/* vertex colors */
for (i = 0; i < 4; i++) {
verts[i].r = ctx->Color.ClearColor.f[0];
verts[i].g = ctx->Color.ClearColor.f[1];
verts[i].b = ctx->Color.ClearColor.f[2];
verts[i].a = ctx->Color.ClearColor.f[3];
}
/* upload new vertex data */
_mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
GL_DYNAMIC_DRAW_ARB);
}
/* draw quad */
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
_mesa_meta_end(ctx);
}
static void
meta_glsl_clear_init(struct gl_context *ctx, struct clear_state *clear)
{
const char *vs_source =
"attribute vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n";
const char *fs_source =
"uniform vec4 color;\n"
"void main()\n"
"{\n"
" gl_FragColor = color;\n"
"}\n";
GLuint vs, fs;
bool has_integer_textures;
if (clear->ArrayObj != 0)
return;
/* create vertex array object */
_mesa_GenVertexArrays(1, &clear->ArrayObj);
_mesa_BindVertexArray(clear->ArrayObj);
/* create vertex array buffer */
_mesa_GenBuffers(1, &clear->VBO);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, clear->VBO);
/* setup vertex arrays */
_mesa_VertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
_mesa_EnableVertexAttribArray(0);
vs = _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER);
_mesa_ShaderSource(vs, 1, &vs_source, NULL);
_mesa_CompileShader(vs);
fs = _mesa_CreateShaderObjectARB(GL_FRAGMENT_SHADER);
_mesa_ShaderSource(fs, 1, &fs_source, NULL);
_mesa_CompileShader(fs);
clear->ShaderProg = _mesa_CreateProgramObjectARB();
_mesa_AttachShader(clear->ShaderProg, fs);
_mesa_DeleteObjectARB(fs);
_mesa_AttachShader(clear->ShaderProg, vs);
_mesa_DeleteObjectARB(vs);
_mesa_BindAttribLocation(clear->ShaderProg, 0, "position");
_mesa_LinkProgram(clear->ShaderProg);
clear->ColorLocation = _mesa_GetUniformLocation(clear->ShaderProg,
"color");
has_integer_textures = _mesa_is_gles3(ctx) ||
(_mesa_is_desktop_gl(ctx) && ctx->Const.GLSLVersion >= 130);
if (has_integer_textures) {
void *shader_source_mem_ctx = ralloc_context(NULL);
const char *vs_int_source =
ralloc_asprintf(shader_source_mem_ctx,
"#version %s\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}\n",
_mesa_is_desktop_gl(ctx) ? "130" : "300 es");
const char *fs_int_source =
ralloc_asprintf(shader_source_mem_ctx,
"#version %s\n"
"uniform ivec4 color;\n"
"out ivec4 out_color;\n"
"\n"
"void main()\n"
"{\n"
" out_color = color;\n"
"}\n",
_mesa_is_desktop_gl(ctx) ? "130" : "300 es");
vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_int_source);
fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_int_source);
ralloc_free(shader_source_mem_ctx);
clear->IntegerShaderProg = _mesa_CreateProgramObjectARB();
_mesa_AttachShader(clear->IntegerShaderProg, fs);
_mesa_DeleteObjectARB(fs);
_mesa_AttachShader(clear->IntegerShaderProg, vs);
_mesa_DeleteObjectARB(vs);
_mesa_BindAttribLocation(clear->IntegerShaderProg, 0, "position");
/* Note that user-defined out attributes get automatically assigned
* locations starting from 0, so we don't need to explicitly
* BindFragDataLocation to 0.
*/
link_program_with_debug(ctx, clear->IntegerShaderProg);
clear->IntegerColorLocation =
_mesa_GetUniformLocation(clear->IntegerShaderProg, "color");
}
}
static void
meta_glsl_clear_cleanup(struct gl_context *ctx, struct clear_state *clear)
{
if (clear->ArrayObj == 0)
return;
_mesa_DeleteVertexArrays(1, &clear->ArrayObj);
clear->ArrayObj = 0;
_mesa_DeleteBuffers(1, &clear->VBO);
clear->VBO = 0;
_mesa_DeleteObjectARB(clear->ShaderProg);
clear->ShaderProg = 0;
if (clear->IntegerShaderProg) {
_mesa_DeleteObjectARB(clear->IntegerShaderProg);
clear->IntegerShaderProg = 0;
}
}
/**
* Meta implementation of ctx->Driver.Clear() in terms of polygon rendering.
*/
void
_mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers)
{
struct clear_state *clear = &ctx->Meta->Clear;
GLbitfield metaSave;
const GLuint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
struct gl_framebuffer *fb = ctx->DrawBuffer;
const float x0 = ((float)fb->_Xmin / fb->Width) * 2.0f - 1.0f;
const float y0 = ((float)fb->_Ymin / fb->Height) * 2.0f - 1.0f;
const float x1 = ((float)fb->_Xmax / fb->Width) * 2.0f - 1.0f;
const float y1 = ((float)fb->_Ymax / fb->Height) * 2.0f - 1.0f;
const float z = -invert_z(ctx->Depth.Clear);
struct vertex {
GLfloat x, y, z;
} verts[4];
metaSave = (MESA_META_ALPHA_TEST |
MESA_META_BLEND |
MESA_META_DEPTH_TEST |
MESA_META_RASTERIZATION |
MESA_META_SHADER |
MESA_META_STENCIL_TEST |
MESA_META_VERTEX |
MESA_META_VIEWPORT |
MESA_META_CLIP |
MESA_META_CLAMP_FRAGMENT_COLOR |
MESA_META_MULTISAMPLE |
MESA_META_OCCLUSION_QUERY);
if (!(buffers & BUFFER_BITS_COLOR)) {
/* We'll use colormask to disable color writes. Otherwise,
* respect color mask
*/
metaSave |= MESA_META_COLOR_MASK;
}
_mesa_meta_begin(ctx, metaSave);
meta_glsl_clear_init(ctx, clear);
if (fb->_IntegerColor) {
_mesa_UseProgram(clear->IntegerShaderProg);
_mesa_Uniform4iv(clear->IntegerColorLocation, 1,
ctx->Color.ClearColor.i);
} else {
_mesa_UseProgram(clear->ShaderProg);
_mesa_Uniform4fv(clear->ColorLocation, 1,
ctx->Color.ClearColor.f);
}
_mesa_BindVertexArray(clear->ArrayObj);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, clear->VBO);
/* GL_COLOR_BUFFER_BIT */
if (buffers & BUFFER_BITS_COLOR) {
/* leave colormask, glDrawBuffer state as-is */
/* Clears never have the color clamped. */
if (ctx->Extensions.ARB_color_buffer_float)
_mesa_ClampColor(GL_CLAMP_FRAGMENT_COLOR, GL_FALSE);
}
else {
ASSERT(metaSave & MESA_META_COLOR_MASK);
_mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
}
/* GL_DEPTH_BUFFER_BIT */
if (buffers & BUFFER_BIT_DEPTH) {
_mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
_mesa_DepthFunc(GL_ALWAYS);
_mesa_DepthMask(GL_TRUE);
}
else {
assert(!ctx->Depth.Test);
}
/* GL_STENCIL_BUFFER_BIT */
if (buffers & BUFFER_BIT_STENCIL) {
_mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
_mesa_StencilOpSeparate(GL_FRONT_AND_BACK,
GL_REPLACE, GL_REPLACE, GL_REPLACE);
_mesa_StencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS,
ctx->Stencil.Clear & stencilMax,
ctx->Stencil.WriteMask[0]);
}
else {
assert(!ctx->Stencil.Enabled);
}
/* vertex positions */
verts[0].x = x0;
verts[0].y = y0;
verts[0].z = z;
verts[1].x = x1;
verts[1].y = y0;
verts[1].z = z;
verts[2].x = x1;
verts[2].y = y1;
verts[2].z = z;
verts[3].x = x0;
verts[3].y = y1;
verts[3].z = z;
/* upload new vertex data */
_mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts,
GL_DYNAMIC_DRAW_ARB);
/* draw quad */
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
_mesa_meta_end(ctx);
}
/**
* Meta implementation of ctx->Driver.CopyPixels() in terms
* of texture mapping and polygon rendering and GLSL shaders.
*/
void
_mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcX, GLint srcY,
GLsizei width, GLsizei height,
GLint dstX, GLint dstY, GLenum type)
{
struct copypix_state *copypix = &ctx->Meta->CopyPix;
struct temp_texture *tex = get_temp_texture(ctx);
struct vertex {
GLfloat x, y, z, s, t;
};
struct vertex verts[4];
GLboolean newTex;
GLenum intFormat = GL_RGBA;
if (type != GL_COLOR ||
ctx->_ImageTransferState ||
ctx->Fog.Enabled ||
width > tex->MaxSize ||
height > tex->MaxSize) {
/* XXX avoid this fallback */
_swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
return;
}
/* Most GL state applies to glCopyPixels, but a there's a few things
* we need to override:
*/
_mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
MESA_META_SHADER |
MESA_META_TEXTURE |
MESA_META_TRANSFORM |
MESA_META_CLIP |
MESA_META_VERTEX |
MESA_META_VIEWPORT));
if (copypix->ArrayObj == 0) {
/* one-time setup */
/* create vertex array object */
_mesa_GenVertexArrays(1, ©pix->ArrayObj);
_mesa_BindVertexArray(copypix->ArrayObj);
/* create vertex array buffer */
_mesa_GenBuffers(1, ©pix->VBO);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, copypix->VBO);
_mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
NULL, GL_DYNAMIC_DRAW_ARB);
/* setup vertex arrays */
_mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
_mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
_mesa_EnableClientState(GL_VERTEX_ARRAY);
_mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
}
else {
_mesa_BindVertexArray(copypix->ArrayObj);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, copypix->VBO);
}
newTex = alloc_texture(tex, width, height, intFormat);
/* vertex positions, texcoords (after texture allocation!) */
{
const GLfloat dstX0 = (GLfloat) dstX;
const GLfloat dstY0 = (GLfloat) dstY;
const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX;
const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY;
const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
verts[0].x = dstX0;
verts[0].y = dstY0;
verts[0].z = z;
verts[0].s = 0.0F;
verts[0].t = 0.0F;
verts[1].x = dstX1;
verts[1].y = dstY0;
verts[1].z = z;
verts[1].s = tex->Sright;
verts[1].t = 0.0F;
verts[2].x = dstX1;
verts[2].y = dstY1;
verts[2].z = z;
verts[2].s = tex->Sright;
verts[2].t = tex->Ttop;
verts[3].x = dstX0;
verts[3].y = dstY1;
verts[3].z = z;
verts[3].s = 0.0F;
verts[3].t = tex->Ttop;
/* upload new vertex data */
_mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
}
/* Alloc/setup texture */
setup_copypix_texture(ctx, tex, newTex, srcX, srcY, width, height,
GL_RGBA, GL_NEAREST);
_mesa_set_enable(ctx, tex->Target, GL_TRUE);
/* draw textured quad */
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
_mesa_set_enable(ctx, tex->Target, GL_FALSE);
_mesa_meta_end(ctx);
}
/**
* When the glDrawPixels() image size is greater than the max rectangle
* texture size we use this function to break the glDrawPixels() image
* into tiles which fit into the max texture size.
*/
static void
tiled_draw_pixels(struct gl_context *ctx,
GLint tileSize,
GLint x, GLint y, GLsizei width, GLsizei height,
GLenum format, GLenum type,
const struct gl_pixelstore_attrib *unpack,
const GLvoid *pixels)
{
struct gl_pixelstore_attrib tileUnpack = *unpack;
GLint i, j;
if (tileUnpack.RowLength == 0)
tileUnpack.RowLength = width;
for (i = 0; i < width; i += tileSize) {
const GLint tileWidth = MIN2(tileSize, width - i);
const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX);
tileUnpack.SkipPixels = unpack->SkipPixels + i;
for (j = 0; j < height; j += tileSize) {
const GLint tileHeight = MIN2(tileSize, height - j);
const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY);
tileUnpack.SkipRows = unpack->SkipRows + j;
_mesa_meta_DrawPixels(ctx, tileX, tileY, tileWidth, tileHeight,
format, type, &tileUnpack, pixels);
}
}
}
/**
* One-time init for drawing stencil pixels.
*/
static void
init_draw_stencil_pixels(struct gl_context *ctx)
{
/* This program is run eight times, once for each stencil bit.
* The stencil values to draw are found in an 8-bit alpha texture.
* We read the texture/stencil value and test if bit 'b' is set.
* If the bit is not set, use KIL to kill the fragment.
* Finally, we use the stencil test to update the stencil buffer.
*
* The basic algorithm for checking if a bit is set is:
* if (is_odd(value / (1 << bit)))
* result is one (or non-zero).
* else
* result is zero.
* The program parameter contains three values:
* parm.x = 255 / (1 << bit)
* parm.y = 0.5
* parm.z = 0.0
*/
static const char *program =
"!!ARBfp1.0\n"
"PARAM parm = program.local[0]; \n"
"TEMP t; \n"
"TEX t, fragment.texcoord[0], texture[0], %s; \n" /* NOTE %s here! */
"# t = t * 255 / bit \n"
"MUL t.x, t.a, parm.x; \n"
"# t = (int) t \n"
"FRC t.y, t.x; \n"
"SUB t.x, t.x, t.y; \n"
"# t = t * 0.5 \n"
"MUL t.x, t.x, parm.y; \n"
"# t = fract(t.x) \n"
"FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
"# t.x = (t.x == 0 ? 1 : 0) \n"
"SGE t.x, -t.x, parm.z; \n"
"KIL -t.x; \n"
"# for debug only \n"
"#MOV result.color, t.x; \n"
"END \n";
char program2[1000];
struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
struct temp_texture *tex = get_temp_texture(ctx);
const char *texTarget;
assert(drawpix->StencilFP == 0);
/* replace %s with "RECT" or "2D" */
assert(strlen(program) + 4 < sizeof(program2));
if (tex->Target == GL_TEXTURE_RECTANGLE)
texTarget = "RECT";
else
texTarget = "2D";
_mesa_snprintf(program2, sizeof(program2), program, texTarget);
_mesa_GenProgramsARB(1, &drawpix->StencilFP);
_mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
_mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
strlen(program2), (const GLubyte *) program2);
}
/**
* One-time init for drawing depth pixels.
*/
static void
init_draw_depth_pixels(struct gl_context *ctx)
{
static const char *program =
"!!ARBfp1.0\n"
"PARAM color = program.local[0]; \n"
"TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
"MOV result.color, color; \n"
"END \n";
char program2[200];
struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
struct temp_texture *tex = get_temp_texture(ctx);
const char *texTarget;
assert(drawpix->DepthFP == 0);
/* replace %s with "RECT" or "2D" */
assert(strlen(program) + 4 < sizeof(program2));
if (tex->Target == GL_TEXTURE_RECTANGLE)
texTarget = "RECT";
else
texTarget = "2D";
_mesa_snprintf(program2, sizeof(program2), program, texTarget);
_mesa_GenProgramsARB(1, &drawpix->DepthFP);
_mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
_mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
strlen(program2), (const GLubyte *) program2);
}
/**
* Meta implementation of ctx->Driver.DrawPixels() in terms
* of texture mapping and polygon rendering.
*/
void
_mesa_meta_DrawPixels(struct gl_context *ctx,
GLint x, GLint y, GLsizei width, GLsizei height,
GLenum format, GLenum type,
const struct gl_pixelstore_attrib *unpack,
const GLvoid *pixels)
{
struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
struct temp_texture *tex = get_temp_texture(ctx);
const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
struct vertex {
GLfloat x, y, z, s, t;
};
struct vertex verts[4];
GLenum texIntFormat;
GLboolean fallback, newTex;
GLbitfield metaExtraSave = 0x0;
GLuint vbo;
/*
* Determine if we can do the glDrawPixels with texture mapping.
*/
fallback = GL_FALSE;
if (ctx->Fog.Enabled) {
fallback = GL_TRUE;
}
if (_mesa_is_color_format(format)) {
/* use more compact format when possible */
/* XXX disable special case for GL_LUMINANCE for now to work around
* apparent i965 driver bug (see bug #23670).
*/
if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
texIntFormat = format;
else
texIntFormat = GL_RGBA;
/* If we're not supposed to clamp the resulting color, then just
* promote our texture to fully float. We could do better by
* just going for the matching set of channels, in floating
* point.
*/
if (ctx->Color.ClampFragmentColor != GL_TRUE &&
ctx->Extensions.ARB_texture_float)
texIntFormat = GL_RGBA32F;
}
else if (_mesa_is_stencil_format(format)) {
if (ctx->Extensions.ARB_fragment_program &&
ctx->Pixel.IndexShift == 0 &&
ctx->Pixel.IndexOffset == 0 &&
type == GL_UNSIGNED_BYTE) {
/* We'll store stencil as alpha. This only works for GLubyte
* image data because of how incoming values are mapped to alpha
* in [0,1].
*/
texIntFormat = GL_ALPHA;
metaExtraSave = (MESA_META_COLOR_MASK |
MESA_META_DEPTH_TEST |
MESA_META_PIXEL_TRANSFER |
MESA_META_SHADER |
MESA_META_STENCIL_TEST);
}
else {
fallback = GL_TRUE;
}
}
else if (_mesa_is_depth_format(format)) {
if (ctx->Extensions.ARB_depth_texture &&
ctx->Extensions.ARB_fragment_program) {
texIntFormat = GL_DEPTH_COMPONENT;
metaExtraSave = (MESA_META_SHADER);
}
else {
fallback = GL_TRUE;
}
}
else {
fallback = GL_TRUE;
}
if (fallback) {
_swrast_DrawPixels(ctx, x, y, width, height,
format, type, unpack, pixels);
return;
}
/*
* Check image size against max texture size, draw as tiles if needed.
*/
if (width > tex->MaxSize || height > tex->MaxSize) {
tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
format, type, unpack, pixels);
return;
}
/* Most GL state applies to glDrawPixels (like blending, stencil, etc),
* but a there's a few things we need to override:
*/
_mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
MESA_META_SHADER |
MESA_META_TEXTURE |
MESA_META_TRANSFORM |
MESA_META_CLIP |
MESA_META_VERTEX |
MESA_META_VIEWPORT |
metaExtraSave));
newTex = alloc_texture(tex, width, height, texIntFormat);
/* vertex positions, texcoords (after texture allocation!) */
{
const GLfloat x0 = (GLfloat) x;
const GLfloat y0 = (GLfloat) y;
const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
verts[0].x = x0;
verts[0].y = y0;
verts[0].z = z;
verts[0].s = 0.0F;
verts[0].t = 0.0F;
verts[1].x = x1;
verts[1].y = y0;
verts[1].z = z;
verts[1].s = tex->Sright;
verts[1].t = 0.0F;
verts[2].x = x1;
verts[2].y = y1;
verts[2].z = z;
verts[2].s = tex->Sright;
verts[2].t = tex->Ttop;
verts[3].x = x0;
verts[3].y = y1;
verts[3].z = z;
verts[3].s = 0.0F;
verts[3].t = tex->Ttop;
}
if (drawpix->ArrayObj == 0) {
/* one-time setup: create vertex array object */
_mesa_GenVertexArrays(1, &drawpix->ArrayObj);
}
_mesa_BindVertexArray(drawpix->ArrayObj);
/* create vertex array buffer */
_mesa_GenBuffers(1, &vbo);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, vbo);
_mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
verts, GL_DYNAMIC_DRAW_ARB);
/* setup vertex arrays */
_mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
_mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
_mesa_EnableClientState(GL_VERTEX_ARRAY);
_mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
/* set given unpack params */
ctx->Unpack = *unpack;
_mesa_set_enable(ctx, tex->Target, GL_TRUE);
if (_mesa_is_stencil_format(format)) {
/* Drawing stencil */
GLint bit;
if (!drawpix->StencilFP)
init_draw_stencil_pixels(ctx);
setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
GL_ALPHA, type, pixels);
_mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
_mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
/* set all stencil bits to 0 */
_mesa_StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
_mesa_StencilFunc(GL_ALWAYS, 0, 255);
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
/* set stencil bits to 1 where needed */
_mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
_mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
_mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
for (bit = 0; bit < ctx->DrawBuffer->Visual.stencilBits; bit++) {
const GLuint mask = 1 << bit;
if (mask & origStencilMask) {
_mesa_StencilFunc(GL_ALWAYS, mask, mask);
_mesa_StencilMask(mask);
_mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
255.0 / mask, 0.5, 0.0, 0.0);
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
}
}
else if (_mesa_is_depth_format(format)) {
/* Drawing depth */
if (!drawpix->DepthFP)
init_draw_depth_pixels(ctx);
_mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
_mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
/* polygon color = current raster color */
_mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
ctx->Current.RasterColor);
setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
format, type, pixels);
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
else {
/* Drawing RGBA */
setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
format, type, pixels);
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
_mesa_set_enable(ctx, tex->Target, GL_FALSE);
_mesa_DeleteBuffers(1, &vbo);
/* restore unpack params */
ctx->Unpack = unpackSave;
_mesa_meta_end(ctx);
}
static GLboolean
alpha_test_raster_color(struct gl_context *ctx)
{
GLfloat alpha = ctx->Current.RasterColor[ACOMP];
GLfloat ref = ctx->Color.AlphaRef;
switch (ctx->Color.AlphaFunc) {
case GL_NEVER:
return GL_FALSE;
case GL_LESS:
return alpha < ref;
case GL_EQUAL:
return alpha == ref;
case GL_LEQUAL:
return alpha <= ref;
case GL_GREATER:
return alpha > ref;
case GL_NOTEQUAL:
return alpha != ref;
case GL_GEQUAL:
return alpha >= ref;
case GL_ALWAYS:
return GL_TRUE;
default:
assert(0);
return GL_FALSE;
}
}
/**
* Do glBitmap with a alpha texture quad. Use the alpha test to cull
* the 'off' bits. A bitmap cache as in the gallium/mesa state
* tracker would improve performance a lot.
*/
void
_mesa_meta_Bitmap(struct gl_context *ctx,
GLint x, GLint y, GLsizei width, GLsizei height,
const struct gl_pixelstore_attrib *unpack,
const GLubyte *bitmap1)
{
struct bitmap_state *bitmap = &ctx->Meta->Bitmap;
struct temp_texture *tex = get_bitmap_temp_texture(ctx);
const GLenum texIntFormat = GL_ALPHA;
const struct gl_pixelstore_attrib unpackSave = *unpack;
GLubyte fg, bg;
struct vertex {
GLfloat x, y, z, s, t, r, g, b, a;
};
struct vertex verts[4];
GLboolean newTex;
GLubyte *bitmap8;
/*
* Check if swrast fallback is needed.
*/
if (ctx->_ImageTransferState ||
ctx->FragmentProgram._Enabled ||
ctx->Fog.Enabled ||
ctx->Texture._EnabledUnits ||
width > tex->MaxSize ||
height > tex->MaxSize) {
_swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
return;
}
if (ctx->Color.AlphaEnabled && !alpha_test_raster_color(ctx))
return;
/* Most GL state applies to glBitmap (like blending, stencil, etc),
* but a there's a few things we need to override:
*/
_mesa_meta_begin(ctx, (MESA_META_ALPHA_TEST |
MESA_META_PIXEL_STORE |
MESA_META_RASTERIZATION |
MESA_META_SHADER |
MESA_META_TEXTURE |
MESA_META_TRANSFORM |
MESA_META_CLIP |
MESA_META_VERTEX |
MESA_META_VIEWPORT));
if (bitmap->ArrayObj == 0) {
/* one-time setup */
/* create vertex array object */
_mesa_GenVertexArraysAPPLE(1, &bitmap->ArrayObj);
_mesa_BindVertexArrayAPPLE(bitmap->ArrayObj);
/* create vertex array buffer */
_mesa_GenBuffers(1, &bitmap->VBO);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
_mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
NULL, GL_DYNAMIC_DRAW_ARB);
/* setup vertex arrays */
_mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
_mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
_mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
_mesa_EnableClientState(GL_VERTEX_ARRAY);
_mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
_mesa_EnableClientState(GL_COLOR_ARRAY);
}
else {
_mesa_BindVertexArray(bitmap->ArrayObj);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
}
newTex = alloc_texture(tex, width, height, texIntFormat);
/* vertex positions, texcoords, colors (after texture allocation!) */
{
const GLfloat x0 = (GLfloat) x;
const GLfloat y0 = (GLfloat) y;
const GLfloat x1 = (GLfloat) (x + width);
const GLfloat y1 = (GLfloat) (y + height);
const GLfloat z = invert_z(ctx->Current.RasterPos[2]);
GLuint i;
verts[0].x = x0;
verts[0].y = y0;
verts[0].z = z;
verts[0].s = 0.0F;
verts[0].t = 0.0F;
verts[1].x = x1;
verts[1].y = y0;
verts[1].z = z;
verts[1].s = tex->Sright;
verts[1].t = 0.0F;
verts[2].x = x1;
verts[2].y = y1;
verts[2].z = z;
verts[2].s = tex->Sright;
verts[2].t = tex->Ttop;
verts[3].x = x0;
verts[3].y = y1;
verts[3].z = z;
verts[3].s = 0.0F;
verts[3].t = tex->Ttop;
for (i = 0; i < 4; i++) {
verts[i].r = ctx->Current.RasterColor[0];
verts[i].g = ctx->Current.RasterColor[1];
verts[i].b = ctx->Current.RasterColor[2];
verts[i].a = ctx->Current.RasterColor[3];
}
/* upload new vertex data */
_mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
}
/* choose different foreground/background alpha values */
CLAMPED_FLOAT_TO_UBYTE(fg, ctx->Current.RasterColor[ACOMP]);
bg = (fg > 127 ? 0 : 255);
bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
if (!bitmap1) {
_mesa_meta_end(ctx);
return;
}
bitmap8 = malloc(width * height);
if (bitmap8) {
memset(bitmap8, bg, width * height);
_mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
bitmap8, width, fg);
_mesa_set_enable(ctx, tex->Target, GL_TRUE);
_mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
_mesa_AlphaFunc(GL_NOTEQUAL, UBYTE_TO_FLOAT(bg));
setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
_mesa_set_enable(ctx, tex->Target, GL_FALSE);
free(bitmap8);
}
_mesa_unmap_pbo_source(ctx, &unpackSave);
_mesa_meta_end(ctx);
}
/**
* Check if the call to _mesa_meta_GenerateMipmap() will require a
* software fallback. The fallback path will require that the texture
* images are mapped.
* \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
*/
GLboolean
_mesa_meta_check_generate_mipmap_fallback(struct gl_context *ctx, GLenum target,
struct gl_texture_object *texObj)
{
const GLuint fboSave = ctx->DrawBuffer->Name;
struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
struct gl_texture_image *baseImage;
GLuint srcLevel;
GLenum status;
/* check for fallbacks */
if (!ctx->Extensions.EXT_framebuffer_object ||
target == GL_TEXTURE_3D ||
target == GL_TEXTURE_1D_ARRAY ||
target == GL_TEXTURE_2D_ARRAY) {
return GL_TRUE;
}
srcLevel = texObj->BaseLevel;
baseImage = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
if (!baseImage || _mesa_is_format_compressed(baseImage->TexFormat)) {
return GL_TRUE;
}
if (_mesa_get_format_color_encoding(baseImage->TexFormat) == GL_SRGB &&
!ctx->Extensions.EXT_texture_sRGB_decode) {
/* The texture format is sRGB but we can't turn off sRGB->linear
* texture sample conversion. So we won't be able to generate the
* right colors when rendering. Need to use a fallback.
*/
return GL_TRUE;
}
/*
* Test that we can actually render in the texture's format.
*/
if (!mipmap->FBO)
_mesa_GenFramebuffers(1, &mipmap->FBO);
_mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, mipmap->FBO);
if (target == GL_TEXTURE_1D) {
_mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
target, texObj->Name, srcLevel);
}
#if 0
/* other work is needed to enable 3D mipmap generation */
else if (target == GL_TEXTURE_3D) {
GLint zoffset = 0;
_mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
target, texObj->Name, srcLevel, zoffset);
}
#endif
else {
/* 2D / cube */
_mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
target, texObj->Name, srcLevel);
}
status = _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
_mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, fboSave);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
return GL_TRUE;
}
return GL_FALSE;
}
/**
* Compute the texture coordinates for the four vertices of a quad for
* drawing a 2D texture image or slice of a cube/3D texture.
* \param faceTarget GL_TEXTURE_1D/2D/3D or cube face name
* \param slice slice of a 1D/2D array texture or 3D texture
* \param width width of the texture image
* \param height height of the texture image
* \param coords0/1/2/3 returns the computed texcoords
*/
static void
setup_texture_coords(GLenum faceTarget,
GLint slice,
GLint width,
GLint height,
GLint depth,
GLfloat coords0[3],
GLfloat coords1[3],
GLfloat coords2[3],
GLfloat coords3[3])
{
static const GLfloat st[4][2] = {
{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
};
GLuint i;
GLfloat r;
switch (faceTarget) {
case GL_TEXTURE_1D:
case GL_TEXTURE_2D:
case GL_TEXTURE_3D:
case GL_TEXTURE_2D_ARRAY:
if (faceTarget == GL_TEXTURE_3D) {
assert(slice < depth);
assert(depth >= 1);
r = (slice + 0.5f) / depth;
}
else if (faceTarget == GL_TEXTURE_2D_ARRAY)
r = slice;
else
r = 0.0F;
coords0[0] = 0.0F; /* s */
coords0[1] = 0.0F; /* t */
coords0[2] = r; /* r */
coords1[0] = 1.0F;
coords1[1] = 0.0F;
coords1[2] = r;
coords2[0] = 1.0F;
coords2[1] = 1.0F;
coords2[2] = r;
coords3[0] = 0.0F;
coords3[1] = 1.0F;
coords3[2] = r;
break;
case GL_TEXTURE_RECTANGLE_ARB:
coords0[0] = 0.0F; /* s */
coords0[1] = 0.0F; /* t */
coords0[2] = 0.0F; /* r */
coords1[0] = width;
coords1[1] = 0.0F;
coords1[2] = 0.0F;
coords2[0] = width;
coords2[1] = height;
coords2[2] = 0.0F;
coords3[0] = 0.0F;
coords3[1] = height;
coords3[2] = 0.0F;
break;
case GL_TEXTURE_1D_ARRAY:
coords0[0] = 0.0F; /* s */
coords0[1] = slice; /* t */
coords0[2] = 0.0F; /* r */
coords1[0] = 1.0f;
coords1[1] = slice;
coords1[2] = 0.0F;
coords2[0] = 1.0F;
coords2[1] = slice;
coords2[2] = 0.0F;
coords3[0] = 0.0F;
coords3[1] = slice;
coords3[2] = 0.0F;
break;
case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
/* loop over quad verts */
for (i = 0; i < 4; i++) {
/* Compute sc = +/-scale and tc = +/-scale.
* Not +/-1 to avoid cube face selection ambiguity near the edges,
* though that can still sometimes happen with this scale factor...
*/
const GLfloat scale = 0.9999f;
const GLfloat sc = (2.0f * st[i][0] - 1.0f) * scale;
const GLfloat tc = (2.0f * st[i][1] - 1.0f) * scale;
GLfloat *coord;
switch (i) {
case 0:
coord = coords0;
break;
case 1:
coord = coords1;
break;
case 2:
coord = coords2;
break;
case 3:
coord = coords3;
break;
default:
assert(0);
}
switch (faceTarget) {
case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
coord[0] = 1.0f;
coord[1] = -tc;
coord[2] = -sc;
break;
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
coord[0] = -1.0f;
coord[1] = -tc;
coord[2] = sc;
break;
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
coord[0] = sc;
coord[1] = 1.0f;
coord[2] = tc;
break;
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
coord[0] = sc;
coord[1] = -1.0f;
coord[2] = -tc;
break;
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
coord[0] = sc;
coord[1] = -tc;
coord[2] = 1.0f;
break;
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
coord[0] = -sc;
coord[1] = -tc;
coord[2] = -1.0f;
break;
default:
assert(0);
}
}
break;
default:
assert(0 && "unexpected target in meta setup_texture_coords()");
}
}
static void
setup_ff_generate_mipmap(struct gl_context *ctx,
struct gen_mipmap_state *mipmap)
{
struct vertex {
GLfloat x, y, tex[3];
};
if (mipmap->ArrayObj == 0) {
/* one-time setup */
/* create vertex array object */
_mesa_GenVertexArraysAPPLE(1, &mipmap->ArrayObj);
_mesa_BindVertexArrayAPPLE(mipmap->ArrayObj);
/* create vertex array buffer */
_mesa_GenBuffers(1, &mipmap->VBO);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
/* setup vertex arrays */
_mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
_mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(tex));
_mesa_EnableClientState(GL_VERTEX_ARRAY);
_mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
}
/* setup projection matrix */
_mesa_MatrixMode(GL_PROJECTION);
_mesa_LoadIdentity();
_mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}
static struct glsl_sampler *
setup_texture_sampler(GLenum target, struct gen_mipmap_state *mipmap)
{
switch(target) {
case GL_TEXTURE_1D:
mipmap->sampler_1d.type = "sampler1D";
mipmap->sampler_1d.func = "texture1D";
mipmap->sampler_1d.texcoords = "texCoords.x";
return &mipmap->sampler_1d;
case GL_TEXTURE_2D:
mipmap->sampler_2d.type = "sampler2D";
mipmap->sampler_2d.func = "texture2D";
mipmap->sampler_2d.texcoords = "texCoords.xy";
return &mipmap->sampler_2d;
case GL_TEXTURE_3D:
/* Code for mipmap generation with 3D textures is not used yet.
* It's a sw fallback.
*/
mipmap->sampler_3d.type = "sampler3D";
mipmap->sampler_3d.func = "texture3D";
mipmap->sampler_3d.texcoords = "texCoords";
return &mipmap->sampler_3d;
case GL_TEXTURE_CUBE_MAP:
mipmap->sampler_cubemap.type = "samplerCube";
mipmap->sampler_cubemap.func = "textureCube";
mipmap->sampler_cubemap.texcoords = "texCoords";
return &mipmap->sampler_cubemap;
case GL_TEXTURE_1D_ARRAY:
mipmap->sampler_1d_array.type = "sampler1DArray";
mipmap->sampler_1d_array.func = "texture1DArray";
mipmap->sampler_1d_array.texcoords = "texCoords.xy";
return &mipmap->sampler_1d_array;
case GL_TEXTURE_2D_ARRAY:
mipmap->sampler_2d_array.type = "sampler2DArray";
mipmap->sampler_2d_array.func = "texture2DArray";
mipmap->sampler_2d_array.texcoords = "texCoords";
return &mipmap->sampler_2d_array;
default:
_mesa_problem(NULL, "Unexpected texture target 0x%x in"
" setup_texture_sampler()\n", target);
return NULL;
}
}
static void
setup_glsl_generate_mipmap(struct gl_context *ctx,
struct gen_mipmap_state *mipmap,
GLenum target)
{
struct vertex {
GLfloat x, y, tex[3];
};
struct glsl_sampler *sampler;
const char *vs_source;
char *fs_source;
GLuint vs, fs;
void *mem_ctx;
/* Check if already initialized */
if (mipmap->ArrayObj == 0) {
/* create vertex array object */
_mesa_GenVertexArrays(1, &mipmap->ArrayObj);
_mesa_BindVertexArray(mipmap->ArrayObj);
/* create vertex array buffer */
_mesa_GenBuffers(1, &mipmap->VBO);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
/* setup vertex arrays */
_mesa_VertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,
sizeof(struct vertex), OFFSET(x));
_mesa_VertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
sizeof(struct vertex), OFFSET(tex));
}
/* Generate a fragment shader program appropriate for the texture target */
sampler = setup_texture_sampler(target, mipmap);
assert(sampler != NULL);
if (sampler->shader_prog != 0) {
mipmap->ShaderProg = sampler->shader_prog;
return;
}
mem_ctx = ralloc_context(NULL);
if (ctx->API == API_OPENGLES2 || ctx->Const.GLSLVersion < 130) {
vs_source =
"attribute vec2 position;\n"
"attribute vec3 textureCoords;\n"
"varying vec3 texCoords;\n"
"void main()\n"
"{\n"
" texCoords = textureCoords;\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n";
fs_source = ralloc_asprintf(mem_ctx,
"#extension GL_EXT_texture_array : enable\n"
"uniform %s texSampler;\n"
"varying vec3 texCoords;\n"
"void main()\n"
"{\n"
" gl_FragColor = %s(texSampler, %s);\n"
"}\n",
sampler->type,
sampler->func, sampler->texcoords);
}
else {
vs_source = ralloc_asprintf(mem_ctx,
"#version %s\n"
"in vec2 position;\n"
"in vec3 textureCoords;\n"
"out vec3 texCoords;\n"
"void main()\n"
"{\n"
" texCoords = textureCoords;\n"
" gl_Position = vec4(position, 0.0, 1.0);\n"
"}\n",
_mesa_is_desktop_gl(ctx) ? "130" : "300 es");
fs_source = ralloc_asprintf(mem_ctx,
"#version %s\n"
"uniform %s texSampler;\n"
"in vec3 texCoords;\n"
"out vec4 out_color;\n"
"\n"
"void main()\n"
"{\n"
" out_color = texture(texSampler, %s);\n"
"}\n",
_mesa_is_desktop_gl(ctx) ? "130" : "300 es",
sampler->type,
sampler->texcoords);
}
vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
mipmap->ShaderProg = _mesa_CreateProgramObjectARB();
_mesa_AttachShader(mipmap->ShaderProg, fs);
_mesa_DeleteObjectARB(fs);
_mesa_AttachShader(mipmap->ShaderProg, vs);
_mesa_DeleteObjectARB(vs);
_mesa_BindAttribLocation(mipmap->ShaderProg, 0, "position");
_mesa_BindAttribLocation(mipmap->ShaderProg, 1, "texcoords");
_mesa_EnableVertexAttribArray(0);
_mesa_EnableVertexAttribArray(1);
link_program_with_debug(ctx, mipmap->ShaderProg);
sampler->shader_prog = mipmap->ShaderProg;
ralloc_free(mem_ctx);
}
static void
meta_glsl_generate_mipmap_cleanup(struct gl_context *ctx,
struct gen_mipmap_state *mipmap)
{
if (mipmap->ArrayObj == 0)
return;
_mesa_DeleteVertexArrays(1, &mipmap->ArrayObj);
mipmap->ArrayObj = 0;
_mesa_DeleteBuffers(1, &mipmap->VBO);
mipmap->VBO = 0;
_mesa_DeleteObjectARB(mipmap->sampler_1d.shader_prog);
_mesa_DeleteObjectARB(mipmap->sampler_2d.shader_prog);
_mesa_DeleteObjectARB(mipmap->sampler_3d.shader_prog);
_mesa_DeleteObjectARB(mipmap->sampler_cubemap.shader_prog);
_mesa_DeleteObjectARB(mipmap->sampler_1d_array.shader_prog);
_mesa_DeleteObjectARB(mipmap->sampler_2d_array.shader_prog);
mipmap->sampler_1d.shader_prog = 0;
mipmap->sampler_2d.shader_prog = 0;
mipmap->sampler_3d.shader_prog = 0;
mipmap->sampler_cubemap.shader_prog = 0;
mipmap->sampler_1d_array.shader_prog = 0;
mipmap->sampler_2d_array.shader_prog = 0;
}
/**
* Called via ctx->Driver.GenerateMipmap()
* Note: We don't yet support 3D textures, 1D/2D array textures or texture
* borders.
*/
void
_mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
struct gl_texture_object *texObj)
{
struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
struct vertex {
GLfloat x, y, tex[3];
};
struct vertex verts[4];
const GLuint baseLevel = texObj->BaseLevel;
const GLuint maxLevel = texObj->MaxLevel;
const GLint maxLevelSave = texObj->MaxLevel;
const GLboolean genMipmapSave = texObj->GenerateMipmap;
const GLuint fboSave = ctx->DrawBuffer->Name;
const GLuint currentTexUnitSave = ctx->Texture.CurrentUnit;
const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
ctx->Extensions.ARB_fragment_shader &&
(ctx->API != API_OPENGLES);
GLenum faceTarget;
GLuint dstLevel;
const GLint slice = 0;
GLuint samplerSave;
if (_mesa_meta_check_generate_mipmap_fallback(ctx, target, texObj)) {
_mesa_generate_mipmap(ctx, target, texObj);
return;
}
if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
faceTarget = target;
target = GL_TEXTURE_CUBE_MAP;
}
else {
faceTarget = target;
}
_mesa_meta_begin(ctx, MESA_META_ALL);
/* Choose between glsl version and fixed function version of
* GenerateMipmap function.
*/
if (use_glsl_version) {
setup_glsl_generate_mipmap(ctx, mipmap, target);
_mesa_UseProgram(mipmap->ShaderProg);
}
else {
setup_ff_generate_mipmap(ctx, mipmap);
_mesa_set_enable(ctx, target, GL_TRUE);
}
_mesa_BindVertexArray(mipmap->ArrayObj);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
samplerSave = ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
if (currentTexUnitSave != 0)
_mesa_BindTexture(target, texObj->Name);
if (!mipmap->FBO) {
_mesa_GenFramebuffers(1, &mipmap->FBO);
}
if (!mipmap->Sampler) {
_mesa_GenSamplers(1, &mipmap->Sampler);
_mesa_BindSampler(ctx->Texture.CurrentUnit, mipmap->Sampler);
_mesa_SamplerParameteri(mipmap->Sampler,
GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
_mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
_mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
_mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
_mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
/* We don't want to encode or decode sRGB values; treat them as linear.
* This is not technically correct for GLES3 but we don't get any API
* error at the moment.
*/
if (ctx->Extensions.EXT_texture_sRGB_decode) {
_mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_SRGB_DECODE_EXT,
GL_SKIP_DECODE_EXT);
}
} else {
_mesa_BindSampler(ctx->Texture.CurrentUnit, mipmap->Sampler);
}
_mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, mipmap->FBO);
if (ctx->API == API_OPENGL_COMPAT || ctx->API == API_OPENGLES)
_mesa_TexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
else
assert(!genMipmapSave);
/* Setup texture coordinates */
setup_texture_coords(faceTarget,
slice,
0, 0, 1, /* width, height never used here */
verts[0].tex,
verts[1].tex,
verts[2].tex,
verts[3].tex);
/* setup vertex positions */
verts[0].x = -1.0F;
verts[0].y = -1.0F;
verts[1].x = 1.0F;
verts[1].y = -1.0F;
verts[2].x = 1.0F;
verts[2].y = 1.0F;
verts[3].x = -1.0F;
verts[3].y = 1.0F;
/* upload vertex data */
_mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
verts, GL_DYNAMIC_DRAW_ARB);
/* texture is already locked, unlock now */
_mesa_unlock_texture(ctx, texObj);
for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) {
const struct gl_texture_image *srcImage;
const GLuint srcLevel = dstLevel - 1;
GLsizei srcWidth, srcHeight, srcDepth;
GLsizei dstWidth, dstHeight, dstDepth;
GLenum status;
srcImage = _mesa_select_tex_image(ctx, texObj, faceTarget, srcLevel);
assert(srcImage->Border == 0);
/* src size */
srcWidth = srcImage->Width;
srcHeight = srcImage->Height;
srcDepth = srcImage->Depth;
/* new dst size */
dstWidth = MAX2(1, srcWidth / 2);
dstHeight = MAX2(1, srcHeight / 2);
dstDepth = MAX2(1, srcDepth / 2);
if (dstWidth == srcImage->Width &&
dstHeight == srcImage->Height &&
dstDepth == srcImage->Depth) {
/* all done */
break;
}
/* Allocate storage for the destination mipmap image(s) */
/* Set MaxLevel large enough to hold the new level when we allocate it */
_mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, dstLevel);
if (!_mesa_prepare_mipmap_level(ctx, texObj, dstLevel,
dstWidth, dstHeight, dstDepth,
srcImage->Border,
srcImage->InternalFormat,
srcImage->TexFormat)) {
/* All done. We either ran out of memory or we would go beyond the
* last valid level of an immutable texture if we continued.
*/
break;
}
/* limit minification to src level */
_mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
/* Set to draw into the current dstLevel */
if (target == GL_TEXTURE_1D) {
_mesa_FramebufferTexture1D(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
target,
texObj->Name,
dstLevel);
}
else if (target == GL_TEXTURE_3D) {
GLint zoffset = 0; /* XXX unfinished */
_mesa_FramebufferTexture3D(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
target,
texObj->Name,
dstLevel, zoffset);
}
else {
/* 2D / cube */
_mesa_FramebufferTexture2D(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
faceTarget,
texObj->Name,
dstLevel);
}
_mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
/* sanity check */
status = _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
_mesa_problem(ctx, "Unexpected incomplete framebuffer in "
"_mesa_meta_GenerateMipmap()");
break;
}
assert(dstWidth == ctx->DrawBuffer->Width);
assert(dstHeight == ctx->DrawBuffer->Height);
/* setup viewport */
_mesa_set_viewport(ctx, 0, 0, dstWidth, dstHeight);
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
}
_mesa_lock_texture(ctx, texObj); /* relock */
_mesa_BindSampler(ctx->Texture.CurrentUnit, samplerSave);
_mesa_meta_end(ctx);
_mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
if (genMipmapSave)
_mesa_TexParameteri(target, GL_GENERATE_MIPMAP, genMipmapSave);
_mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, fboSave);
}
/**
* Determine the GL data type to use for the temporary image read with
* ReadPixels() and passed to Tex[Sub]Image().
*/
static GLenum
get_temp_image_type(struct gl_context *ctx, gl_format format)
{
GLenum baseFormat;
baseFormat = _mesa_get_format_base_format(format);
switch (baseFormat) {
case GL_RGBA:
case GL_RGB:
case GL_RG:
case GL_RED:
case GL_ALPHA:
case GL_LUMINANCE:
case GL_LUMINANCE_ALPHA:
case GL_INTENSITY:
if (ctx->DrawBuffer->Visual.redBits <= 8) {
return GL_UNSIGNED_BYTE;
} else if (ctx->DrawBuffer->Visual.redBits <= 16) {
return GL_UNSIGNED_SHORT;
} else {
GLenum datatype = _mesa_get_format_datatype(format);
if (datatype == GL_INT || datatype == GL_UNSIGNED_INT)
return datatype;
return GL_FLOAT;
}
case GL_DEPTH_COMPONENT:
return GL_UNSIGNED_INT;
case GL_DEPTH_STENCIL:
return GL_UNSIGNED_INT_24_8;
default:
_mesa_problem(ctx, "Unexpected format %d in get_temp_image_type()",
baseFormat);
return 0;
}
}
/**
* Helper for _mesa_meta_CopyTexSubImage1/2/3D() functions.
* Have to be careful with locking and meta state for pixel transfer.
*/
void
_mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
struct gl_texture_image *texImage,
GLint xoffset, GLint yoffset, GLint zoffset,
struct gl_renderbuffer *rb,
GLint x, GLint y,
GLsizei width, GLsizei height)
{
struct gl_texture_object *texObj = texImage->TexObject;
GLenum format, type;
GLint bpp;
void *buf;
/* Choose format/type for temporary image buffer */
format = _mesa_get_format_base_format(texImage->TexFormat);
if (format == GL_LUMINANCE ||
format == GL_LUMINANCE_ALPHA ||
format == GL_INTENSITY) {
/* We don't want to use GL_LUMINANCE, GL_INTENSITY, etc. for the
* temp image buffer because glReadPixels will do L=R+G+B which is
* not what we want (should be L=R).
*/
format = GL_RGBA;
}
type = get_temp_image_type(ctx, texImage->TexFormat);
if (_mesa_is_format_integer_color(texImage->TexFormat)) {
format = _mesa_base_format_to_integer_format(format);
}
bpp = _mesa_bytes_per_pixel(format, type);
if (bpp <= 0) {
_mesa_problem(ctx, "Bad bpp in _mesa_meta_CopyTexSubImage()");
return;
}
/*
* Alloc image buffer (XXX could use a PBO)
*/
buf = malloc(width * height * bpp);
if (!buf) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage%uD", dims);
return;
}
_mesa_unlock_texture(ctx, texObj); /* need to unlock first */
/*
* Read image from framebuffer (disable pixel transfer ops)
*/
_mesa_meta_begin(ctx, MESA_META_PIXEL_STORE | MESA_META_PIXEL_TRANSFER);
ctx->Driver.ReadPixels(ctx, x, y, width, height,
format, type, &ctx->Pack, buf);
_mesa_meta_end(ctx);
_mesa_update_state(ctx); /* to update pixel transfer state */
/*
* Store texture data (with pixel transfer ops)
*/
_mesa_meta_begin(ctx, MESA_META_PIXEL_STORE);
ctx->Driver.TexSubImage(ctx, dims, texImage,
xoffset, yoffset, zoffset, width, height, 1,
format, type, buf, &ctx->Unpack);
_mesa_meta_end(ctx);
_mesa_lock_texture(ctx, texObj); /* re-lock */
free(buf);
}
/**
* Decompress a texture image by drawing a quad with the compressed
* texture and reading the pixels out of the color buffer.
* \param slice which slice of a 3D texture or layer of a 1D/2D texture
* \param destFormat format, ala glReadPixels
* \param destType type, ala glReadPixels
* \param dest destination buffer
* \param destRowLength dest image rowLength (ala GL_PACK_ROW_LENGTH)
*/
static void
decompress_texture_image(struct gl_context *ctx,
struct gl_texture_image *texImage,
GLuint slice,
GLenum destFormat, GLenum destType,
GLvoid *dest)
{
struct decompress_state *decompress = &ctx->Meta->Decompress;
struct gl_texture_object *texObj = texImage->TexObject;
const GLint width = texImage->Width;
const GLint height = texImage->Height;
const GLint depth = texImage->Height;
const GLenum target = texObj->Target;
GLenum faceTarget;
struct vertex {
GLfloat x, y, tex[3];
};
struct vertex verts[4];
GLuint fboDrawSave, fboReadSave;
GLuint rbSave;
GLuint samplerSave;
if (slice > 0) {
assert(target == GL_TEXTURE_3D ||
target == GL_TEXTURE_2D_ARRAY);
}
if (target == GL_TEXTURE_CUBE_MAP) {
faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + texImage->Face;
}
else {
faceTarget = target;
}
/* save fbo bindings (not saved by _mesa_meta_begin()) */
fboDrawSave = ctx->DrawBuffer->Name;
fboReadSave = ctx->ReadBuffer->Name;
rbSave = ctx->CurrentRenderbuffer ? ctx->CurrentRenderbuffer->Name : 0;
_mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_PIXEL_STORE);
samplerSave = ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
/* Create/bind FBO/renderbuffer */
if (decompress->FBO == 0) {
_mesa_GenFramebuffers(1, &decompress->FBO);
_mesa_GenRenderbuffers(1, &decompress->RBO);
_mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, decompress->FBO);
_mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, decompress->RBO);
_mesa_FramebufferRenderbuffer(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
GL_RENDERBUFFER_EXT,
decompress->RBO);
}
else {
_mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, decompress->FBO);
}
/* alloc dest surface */
if (width > decompress->Width || height > decompress->Height) {
_mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, decompress->RBO);
_mesa_RenderbufferStorage(GL_RENDERBUFFER_EXT, GL_RGBA,
width, height);
decompress->Width = width;
decompress->Height = height;
}
/* setup VBO data */
if (decompress->ArrayObj == 0) {
/* create vertex array object */
_mesa_GenVertexArrays(1, &decompress->ArrayObj);
_mesa_BindVertexArray(decompress->ArrayObj);
/* create vertex array buffer */
_mesa_GenBuffers(1, &decompress->VBO);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, decompress->VBO);
_mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
NULL, GL_DYNAMIC_DRAW_ARB);
/* setup vertex arrays */
_mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
_mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(tex));
_mesa_EnableClientState(GL_VERTEX_ARRAY);
_mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
}
else {
_mesa_BindVertexArray(decompress->ArrayObj);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, decompress->VBO);
}
if (!decompress->Sampler) {
_mesa_GenSamplers(1, &decompress->Sampler);
_mesa_BindSampler(ctx->Texture.CurrentUnit, decompress->Sampler);
/* nearest filtering */
_mesa_SamplerParameteri(decompress->Sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
_mesa_SamplerParameteri(decompress->Sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
/* No sRGB decode or encode.*/
if (ctx->Extensions.EXT_texture_sRGB_decode) {
_mesa_SamplerParameteri(decompress->Sampler, GL_TEXTURE_SRGB_DECODE_EXT,
GL_SKIP_DECODE_EXT);
}
} else {
_mesa_BindSampler(ctx->Texture.CurrentUnit, decompress->Sampler);
}
setup_texture_coords(faceTarget, slice, width, height, depth,
verts[0].tex,
verts[1].tex,
verts[2].tex,
verts[3].tex);
/* setup vertex positions */
verts[0].x = 0.0F;
verts[0].y = 0.0F;
verts[1].x = width;
verts[1].y = 0.0F;
verts[2].x = width;
verts[2].y = height;
verts[3].x = 0.0F;
verts[3].y = height;
/* upload new vertex data */
_mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
/* setup texture state */
_mesa_BindTexture(target, texObj->Name);
_mesa_set_enable(ctx, target, GL_TRUE);
{
/* save texture object state */
const GLint baseLevelSave = texObj->BaseLevel;
const GLint maxLevelSave = texObj->MaxLevel;
/* restrict sampling to the texture level of interest */
if (target != GL_TEXTURE_RECTANGLE_ARB) {
_mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, texImage->Level);
_mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, texImage->Level);
}
/* render quad w/ texture into renderbuffer */
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
/* Restore texture object state, the texture binding will
* be restored by _mesa_meta_end().
*/
if (target != GL_TEXTURE_RECTANGLE_ARB) {
_mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, baseLevelSave);
_mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
}
}
/* read pixels from renderbuffer */
{
GLenum baseTexFormat = texImage->_BaseFormat;
GLenum destBaseFormat = _mesa_base_tex_format(ctx, destFormat);
/* The pixel transfer state will be set to default values at this point
* (see MESA_META_PIXEL_TRANSFER) so pixel transfer ops are effectively
* turned off (as required by glGetTexImage) but we need to handle some
* special cases. In particular, single-channel texture values are
* returned as red and two-channel texture values are returned as
* red/alpha.
*/
if ((baseTexFormat == GL_LUMINANCE ||
baseTexFormat == GL_LUMINANCE_ALPHA ||
baseTexFormat == GL_INTENSITY) ||
/* If we're reading back an RGB(A) texture (using glGetTexImage) as
* luminance then we need to return L=tex(R).
*/
((baseTexFormat == GL_RGBA ||
baseTexFormat == GL_RGB ||
baseTexFormat == GL_RG) &&
(destBaseFormat == GL_LUMINANCE ||
destBaseFormat == GL_LUMINANCE_ALPHA ||
destBaseFormat == GL_LUMINANCE_INTEGER_EXT ||
destBaseFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT))) {
/* Green and blue must be zero */
_mesa_PixelTransferf(GL_GREEN_SCALE, 0.0f);
_mesa_PixelTransferf(GL_BLUE_SCALE, 0.0f);
}
_mesa_ReadPixels(0, 0, width, height, destFormat, destType, dest);
}
/* disable texture unit */
_mesa_set_enable(ctx, target, GL_FALSE);
_mesa_BindSampler(ctx->Texture.CurrentUnit, samplerSave);
_mesa_meta_end(ctx);
/* restore fbo bindings */
if (fboDrawSave == fboReadSave) {
_mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, fboDrawSave);
}
else {
_mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, fboDrawSave);
_mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT, fboReadSave);
}
_mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, rbSave);
}
/**
* This is just a wrapper around _mesa_get_tex_image() and
* decompress_texture_image(). Meta functions should not be directly called
* from core Mesa.
*/
void
_mesa_meta_GetTexImage(struct gl_context *ctx,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage)
{
/* We can only use the decompress-with-blit method here if the texels are
* unsigned, normalized values. We could handle signed and unnormalized
* with floating point renderbuffers...
*/
if (_mesa_is_format_compressed(texImage->TexFormat) &&
_mesa_get_format_datatype(texImage->TexFormat)
== GL_UNSIGNED_NORMALIZED) {
struct gl_texture_object *texObj = texImage->TexObject;
const GLuint slice = 0; /* only 2D compressed textures for now */
/* Need to unlock the texture here to prevent deadlock... */
_mesa_unlock_texture(ctx, texObj);
decompress_texture_image(ctx, texImage, slice, format, type, pixels);
/* ... and relock it */
_mesa_lock_texture(ctx, texObj);
}
else {
_mesa_get_teximage(ctx, format, type, pixels, texImage);
}
}
/**
* Meta implementation of ctx->Driver.DrawTex() in terms
* of polygon rendering.
*/
void
_mesa_meta_DrawTex(struct gl_context *ctx, GLfloat x, GLfloat y, GLfloat z,
GLfloat width, GLfloat height)
{
struct drawtex_state *drawtex = &ctx->Meta->DrawTex;
struct vertex {
GLfloat x, y, z, st[MAX_TEXTURE_UNITS][2];
};
struct vertex verts[4];
GLuint i;
_mesa_meta_begin(ctx, (MESA_META_RASTERIZATION |
MESA_META_SHADER |
MESA_META_TRANSFORM |
MESA_META_VERTEX |
MESA_META_VIEWPORT));
if (drawtex->ArrayObj == 0) {
/* one-time setup */
GLint active_texture;
/* create vertex array object */
_mesa_GenVertexArrays(1, &drawtex->ArrayObj);
_mesa_BindVertexArray(drawtex->ArrayObj);
/* create vertex array buffer */
_mesa_GenBuffers(1, &drawtex->VBO);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, drawtex->VBO);
_mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
NULL, GL_DYNAMIC_DRAW_ARB);
/* client active texture is not part of the array object */
active_texture = ctx->Array.ActiveTexture;
/* setup vertex arrays */
_mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
_mesa_EnableClientState(GL_VERTEX_ARRAY);
for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
_mesa_ClientActiveTexture(GL_TEXTURE0 + i);
_mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(st[i]));
_mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
}
/* restore client active texture */
_mesa_ClientActiveTexture(GL_TEXTURE0 + active_texture);
}
else {
_mesa_BindVertexArray(drawtex->ArrayObj);
_mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, drawtex->VBO);
}
/* vertex positions, texcoords */
{
const GLfloat x1 = x + width;
const GLfloat y1 = y + height;
z = CLAMP(z, 0.0f, 1.0f);
z = invert_z(z);
verts[0].x = x;
verts[0].y = y;
verts[0].z = z;
verts[1].x = x1;
verts[1].y = y;
verts[1].z = z;
verts[2].x = x1;
verts[2].y = y1;
verts[2].z = z;
verts[3].x = x;
verts[3].y = y1;
verts[3].z = z;
for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
const struct gl_texture_object *texObj;
const struct gl_texture_image *texImage;
GLfloat s, t, s1, t1;
GLuint tw, th;
if (!ctx->Texture.Unit[i]._ReallyEnabled) {
GLuint j;
for (j = 0; j < 4; j++) {
verts[j].st[i][0] = 0.0f;
verts[j].st[i][1] = 0.0f;
}
continue;
}
texObj = ctx->Texture.Unit[i]._Current;
texImage = texObj->Image[0][texObj->BaseLevel];
tw = texImage->Width2;
th = texImage->Height2;
s = (GLfloat) texObj->CropRect[0] / tw;
t = (GLfloat) texObj->CropRect[1] / th;
s1 = (GLfloat) (texObj->CropRect[0] + texObj->CropRect[2]) / tw;
t1 = (GLfloat) (texObj->CropRect[1] + texObj->CropRect[3]) / th;
verts[0].st[i][0] = s;
verts[0].st[i][1] = t;
verts[1].st[i][0] = s1;
verts[1].st[i][1] = t;
verts[2].st[i][0] = s1;
verts[2].st[i][1] = t1;
verts[3].st[i][0] = s;
verts[3].st[i][1] = t1;
}
_mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
}
_mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
_mesa_meta_end(ctx);
}
|