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
|
//
// Copyright 2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// TextureD3D.cpp: Implementations of the Texture interfaces shared betweeen the D3D backends.
#include "libANGLE/renderer/d3d/TextureD3D.h"
#include "common/mathutil.h"
#include "common/utilities.h"
#include "libANGLE/Buffer.h"
#include "libANGLE/Config.h"
#include "libANGLE/Framebuffer.h"
#include "libANGLE/Image.h"
#include "libANGLE/Surface.h"
#include "libANGLE/Texture.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/renderer/BufferImpl.h"
#include "libANGLE/renderer/d3d/BufferD3D.h"
#include "libANGLE/renderer/d3d/EGLImageD3D.h"
#include "libANGLE/renderer/d3d/ImageD3D.h"
#include "libANGLE/renderer/d3d/RendererD3D.h"
#include "libANGLE/renderer/d3d/RenderTargetD3D.h"
#include "libANGLE/renderer/d3d/SurfaceD3D.h"
#include "libANGLE/renderer/d3d/TextureStorage.h"
namespace rx
{
namespace
{
gl::Error GetUnpackPointer(const gl::PixelUnpackState &unpack, const uint8_t *pixels,
ptrdiff_t layerOffset, const uint8_t **pointerOut)
{
if (unpack.pixelBuffer.id() != 0)
{
// Do a CPU readback here, if we have an unpack buffer bound and the fast GPU path is not supported
gl::Buffer *pixelBuffer = unpack.pixelBuffer.get();
ptrdiff_t offset = reinterpret_cast<ptrdiff_t>(pixels);
// TODO: this is the only place outside of renderer that asks for a buffers raw data.
// This functionality should be moved into renderer and the getData method of BufferImpl removed.
BufferD3D *bufferD3D = GetImplAs<BufferD3D>(pixelBuffer);
ASSERT(bufferD3D);
const uint8_t *bufferData = NULL;
ANGLE_TRY(bufferD3D->getData(&bufferData));
*pointerOut = bufferData + offset;
}
else
{
*pointerOut = pixels;
}
// Offset the pointer for 2D array layer (if it's valid)
if (*pointerOut != nullptr)
{
*pointerOut += layerOffset;
}
return gl::Error(GL_NO_ERROR);
}
bool IsRenderTargetUsage(GLenum usage)
{
return (usage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE);
}
}
TextureD3D::TextureD3D(const gl::TextureState &state, RendererD3D *renderer)
: TextureImpl(state),
mRenderer(renderer),
mDirtyImages(true),
mImmutable(false),
mTexStorage(nullptr),
mBaseLevel(0)
{
}
TextureD3D::~TextureD3D()
{
}
gl::Error TextureD3D::getNativeTexture(TextureStorage **outStorage)
{
// ensure the underlying texture is created
ANGLE_TRY(initializeStorage(false));
if (mTexStorage)
{
ANGLE_TRY(updateStorage());
}
ASSERT(outStorage);
*outStorage = mTexStorage;
return gl::NoError();
}
GLint TextureD3D::getLevelZeroWidth() const
{
ASSERT(gl::CountLeadingZeros(static_cast<uint32_t>(getBaseLevelWidth())) > getBaseLevel());
return getBaseLevelWidth() << mBaseLevel;
}
GLint TextureD3D::getLevelZeroHeight() const
{
ASSERT(gl::CountLeadingZeros(static_cast<uint32_t>(getBaseLevelHeight())) > getBaseLevel());
return getBaseLevelHeight() << mBaseLevel;
}
GLint TextureD3D::getLevelZeroDepth() const
{
return getBaseLevelDepth();
}
GLint TextureD3D::getBaseLevelWidth() const
{
const ImageD3D *baseImage = getBaseLevelImage();
return (baseImage ? baseImage->getWidth() : 0);
}
GLint TextureD3D::getBaseLevelHeight() const
{
const ImageD3D *baseImage = getBaseLevelImage();
return (baseImage ? baseImage->getHeight() : 0);
}
GLint TextureD3D::getBaseLevelDepth() const
{
const ImageD3D *baseImage = getBaseLevelImage();
return (baseImage ? baseImage->getDepth() : 0);
}
// Note: "base level image" is loosely defined to be any image from the base level,
// where in the base of 2D array textures and cube maps there are several. Don't use
// the base level image for anything except querying texture format and size.
GLenum TextureD3D::getBaseLevelInternalFormat() const
{
const ImageD3D *baseImage = getBaseLevelImage();
return (baseImage ? baseImage->getInternalFormat() : GL_NONE);
}
bool TextureD3D::shouldUseSetData(const ImageD3D *image) const
{
if (!mRenderer->getWorkarounds().setDataFasterThanImageUpload)
{
return false;
}
gl::InternalFormat internalFormat = gl::GetInternalFormatInfo(image->getInternalFormat());
// We can only handle full updates for depth-stencil textures, so to avoid complications
// disable them entirely.
if (internalFormat.depthBits > 0 || internalFormat.stencilBits > 0)
{
return false;
}
// TODO(jmadill): Handle compressed internal formats
return (mTexStorage && !internalFormat.compressed);
}
gl::Error TextureD3D::setImageImpl(const gl::ImageIndex &index,
GLenum type,
const gl::PixelUnpackState &unpack,
const uint8_t *pixels,
ptrdiff_t layerOffset)
{
ImageD3D *image = getImage(index);
ASSERT(image);
// No-op
if (image->getWidth() == 0 || image->getHeight() == 0 || image->getDepth() == 0)
{
return gl::Error(GL_NO_ERROR);
}
// We no longer need the "GLenum format" parameter to TexImage to determine what data format "pixels" contains.
// From our image internal format we know how many channels to expect, and "type" gives the format of pixel's components.
const uint8_t *pixelData = NULL;
ANGLE_TRY(GetUnpackPointer(unpack, pixels, layerOffset, &pixelData));
if (pixelData != nullptr)
{
if (shouldUseSetData(image))
{
ANGLE_TRY(mTexStorage->setData(index, image, NULL, type, unpack, pixelData));
}
else
{
gl::Box fullImageArea(0, 0, 0, image->getWidth(), image->getHeight(), image->getDepth());
ANGLE_TRY(image->loadData(fullImageArea, unpack, type, pixelData, index.is3D()));
}
mDirtyImages = true;
}
return gl::NoError();
}
gl::Error TextureD3D::subImage(const gl::ImageIndex &index, const gl::Box &area, GLenum format, GLenum type,
const gl::PixelUnpackState &unpack, const uint8_t *pixels, ptrdiff_t layerOffset)
{
// CPU readback & copy where direct GPU copy is not supported
const uint8_t *pixelData = NULL;
ANGLE_TRY(GetUnpackPointer(unpack, pixels, layerOffset, &pixelData));
if (pixelData != NULL)
{
ImageD3D *image = getImage(index);
ASSERT(image);
if (shouldUseSetData(image))
{
return mTexStorage->setData(index, image, &area, type, unpack, pixelData);
}
ANGLE_TRY(image->loadData(area, unpack, type, pixelData, index.is3D()));
ANGLE_TRY(commitRegion(index, area));
mDirtyImages = true;
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D::setCompressedImageImpl(const gl::ImageIndex &index,
const gl::PixelUnpackState &unpack,
const uint8_t *pixels,
ptrdiff_t layerOffset)
{
ImageD3D *image = getImage(index);
ASSERT(image);
if (image->getWidth() == 0 || image->getHeight() == 0 || image->getDepth() == 0)
{
return gl::NoError();
}
// We no longer need the "GLenum format" parameter to TexImage to determine what data format "pixels" contains.
// From our image internal format we know how many channels to expect, and "type" gives the format of pixel's components.
const uint8_t *pixelData = NULL;
ANGLE_TRY(GetUnpackPointer(unpack, pixels, layerOffset, &pixelData));
if (pixelData != NULL)
{
gl::Box fullImageArea(0, 0, 0, image->getWidth(), image->getHeight(), image->getDepth());
ANGLE_TRY(image->loadCompressedData(fullImageArea, pixelData));
mDirtyImages = true;
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D::subImageCompressed(const gl::ImageIndex &index, const gl::Box &area, GLenum format,
const gl::PixelUnpackState &unpack, const uint8_t *pixels,
ptrdiff_t layerOffset)
{
const uint8_t *pixelData = NULL;
ANGLE_TRY(GetUnpackPointer(unpack, pixels, layerOffset, &pixelData));
if (pixelData != NULL)
{
ImageD3D *image = getImage(index);
ASSERT(image);
ANGLE_TRY(image->loadCompressedData(area, pixelData));
mDirtyImages = true;
}
return gl::Error(GL_NO_ERROR);
}
bool TextureD3D::isFastUnpackable(const gl::PixelUnpackState &unpack, GLenum sizedInternalFormat)
{
return unpack.pixelBuffer.id() != 0 && mRenderer->supportsFastCopyBufferToTexture(sizedInternalFormat);
}
gl::Error TextureD3D::fastUnpackPixels(const gl::PixelUnpackState &unpack, const uint8_t *pixels, const gl::Box &destArea,
GLenum sizedInternalFormat, GLenum type, RenderTargetD3D *destRenderTarget)
{
if (unpack.skipRows != 0 || unpack.skipPixels != 0 || unpack.imageHeight != 0 ||
unpack.skipImages != 0)
{
// TODO(jmadill): additional unpack parameters
UNIMPLEMENTED();
return gl::Error(GL_INVALID_OPERATION,
"Unimplemented pixel store parameters in fastUnpackPixels");
}
// No-op
if (destArea.width <= 0 && destArea.height <= 0 && destArea.depth <= 0)
{
return gl::Error(GL_NO_ERROR);
}
// In order to perform the fast copy through the shader, we must have the right format, and be able
// to create a render target.
ASSERT(mRenderer->supportsFastCopyBufferToTexture(sizedInternalFormat));
uintptr_t offset = reinterpret_cast<uintptr_t>(pixels);
ANGLE_TRY(mRenderer->fastCopyBufferToTexture(unpack, static_cast<unsigned int>(offset),
destRenderTarget, sizedInternalFormat, type,
destArea));
return gl::NoError();
}
GLint TextureD3D::creationLevels(GLsizei width, GLsizei height, GLsizei depth) const
{
if ((gl::isPow2(width) && gl::isPow2(height) && gl::isPow2(depth)) ||
mRenderer->getNativeExtensions().textureNPOT)
{
// Maximum number of levels
return gl::log2(std::max(std::max(width, height), depth)) + 1;
}
else
{
// OpenGL ES 2.0 without GL_OES_texture_npot does not permit NPOT mipmaps.
return 1;
}
}
TextureStorage *TextureD3D::getStorage()
{
ASSERT(mTexStorage);
return mTexStorage;
}
ImageD3D *TextureD3D::getBaseLevelImage() const
{
if (mBaseLevel >= gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
{
return nullptr;
}
return getImage(getImageIndex(mBaseLevel, 0));
}
gl::Error TextureD3D::setImageExternal(GLenum target,
egl::Stream *stream,
const egl::Stream::GLTextureDescription &desc)
{
// Only external images can accept external textures
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureD3D::generateMipmap()
{
const GLuint baseLevel = mState.getEffectiveBaseLevel();
const GLuint maxLevel = mState.getMipmapMaxLevel();
ASSERT(maxLevel > baseLevel); // Should be checked before calling this.
if (mTexStorage && mRenderer->getWorkarounds().zeroMaxLodWorkaround)
{
// Switch to using the mipmapped texture.
TextureStorage *textureStorage = NULL;
ANGLE_TRY(getNativeTexture(&textureStorage));
ANGLE_TRY(textureStorage->useLevelZeroWorkaroundTexture(false));
}
// Set up proper mipmap chain in our Image array.
initMipmapImages();
if (mTexStorage && mTexStorage->supportsNativeMipmapFunction())
{
ANGLE_TRY(updateStorage());
// Generate the mipmap chain using the ad-hoc DirectX function.
ANGLE_TRY(mRenderer->generateMipmapUsingD3D(mTexStorage, mState));
}
else
{
// Generate the mipmap chain, one level at a time.
ANGLE_TRY(generateMipmapUsingImages(maxLevel));
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D::generateMipmapUsingImages(const GLuint maxLevel)
{
// We know that all layers have the same dimension, for the texture to be complete
GLint layerCount = static_cast<GLint>(getLayerCount(mBaseLevel));
// When making mipmaps with the setData workaround enabled, the texture storage has
// the image data already. For non-render-target storage, we have to pull it out into
// an image layer.
if (mRenderer->getWorkarounds().setDataFasterThanImageUpload && mTexStorage)
{
if (!mTexStorage->isRenderTarget())
{
// Copy from the storage mip 0 to Image mip 0
for (GLint layer = 0; layer < layerCount; ++layer)
{
gl::ImageIndex srcIndex = getImageIndex(mBaseLevel, layer);
ImageD3D *image = getImage(srcIndex);
ANGLE_TRY(image->copyFromTexStorage(srcIndex, mTexStorage));
}
}
else
{
ANGLE_TRY(updateStorage());
}
}
// TODO: Decouple this from zeroMaxLodWorkaround. This is a 9_3 restriction, unrelated to zeroMaxLodWorkaround.
// The restriction is because Feature Level 9_3 can't create SRVs on individual levels of the texture.
// As a result, even if the storage is a rendertarget, we can't use the GPU to generate the mipmaps without further work.
// The D3D9 renderer works around this by copying each level of the texture into its own single-layer GPU texture (in Blit9::boxFilter).
// Feature Level 9_3 could do something similar, or it could continue to use CPU-side mipmap generation, or something else.
bool renderableStorage = (mTexStorage && mTexStorage->isRenderTarget() && !(mRenderer->getWorkarounds().zeroMaxLodWorkaround));
for (GLint layer = 0; layer < layerCount; ++layer)
{
for (GLuint mip = mBaseLevel + 1; mip <= maxLevel; ++mip)
{
ASSERT(getLayerCount(mip) == layerCount);
gl::ImageIndex sourceIndex = getImageIndex(mip - 1, layer);
gl::ImageIndex destIndex = getImageIndex(mip, layer);
if (renderableStorage)
{
// GPU-side mipmapping
ANGLE_TRY(mTexStorage->generateMipmap(sourceIndex, destIndex));
}
else
{
// CPU-side mipmapping
ANGLE_TRY(mRenderer->generateMipmap(getImage(destIndex), getImage(sourceIndex)));
}
}
}
if (mTexStorage)
{
updateStorage();
}
return gl::NoError();
}
bool TextureD3D::isBaseImageZeroSize() const
{
ImageD3D *baseImage = getBaseLevelImage();
if (!baseImage || baseImage->getWidth() <= 0)
{
return true;
}
if (!gl::IsCubeMapTextureTarget(baseImage->getTarget()) && baseImage->getHeight() <= 0)
{
return true;
}
if (baseImage->getTarget() == GL_TEXTURE_3D && baseImage->getDepth() <= 0)
{
return true;
}
if (baseImage->getTarget() == GL_TEXTURE_2D_ARRAY && getLayerCount(getBaseLevel()) <= 0)
{
return true;
}
return false;
}
gl::Error TextureD3D::ensureRenderTarget()
{
ANGLE_TRY(initializeStorage(true));
if (!isBaseImageZeroSize())
{
ASSERT(mTexStorage);
if (!mTexStorage->isRenderTarget())
{
TextureStorage *newRenderTargetStorage = NULL;
ANGLE_TRY(createCompleteStorage(true, &newRenderTargetStorage));
gl::Error error = mTexStorage->copyToStorage(newRenderTargetStorage);
if (error.isError())
{
SafeDelete(newRenderTargetStorage);
return error;
}
error = setCompleteTexStorage(newRenderTargetStorage);
if (error.isError())
{
SafeDelete(newRenderTargetStorage);
return error;
}
}
}
return gl::NoError();
}
bool TextureD3D::canCreateRenderTargetForImage(const gl::ImageIndex &index) const
{
ImageD3D *image = getImage(index);
bool levelsComplete = (isImageComplete(index) && isImageComplete(getImageIndex(0, 0)));
return (image->isRenderableFormat() && levelsComplete);
}
gl::Error TextureD3D::commitRegion(const gl::ImageIndex &index, const gl::Box ®ion)
{
if (mTexStorage)
{
ASSERT(isValidIndex(index));
ImageD3D *image = getImage(index);
ANGLE_TRY(image->copyToStorage(mTexStorage, index, region));
image->markClean();
}
return gl::NoError();
}
gl::Error TextureD3D::getAttachmentRenderTarget(const gl::FramebufferAttachment::Target &target,
FramebufferAttachmentRenderTarget **rtOut)
{
RenderTargetD3D *rtD3D = nullptr;
gl::Error error = getRenderTarget(target.textureIndex(), &rtD3D);
*rtOut = static_cast<FramebufferAttachmentRenderTarget *>(rtD3D);
return error;
}
void TextureD3D::setBaseLevel(GLuint baseLevel)
{
const int oldStorageWidth = std::max(1, getLevelZeroWidth());
const int oldStorageHeight = std::max(1, getLevelZeroHeight());
const int oldStorageDepth = std::max(1, getLevelZeroDepth());
const int oldStorageFormat = getBaseLevelInternalFormat();
mBaseLevel = baseLevel;
// When the base level changes, the texture storage might not be valid anymore, since it could
// have been created based on the dimensions of the previous specified level range.
const int newStorageWidth = std::max(1, getLevelZeroWidth());
const int newStorageHeight = std::max(1, getLevelZeroHeight());
const int newStorageDepth = std::max(1, getLevelZeroDepth());
const int newStorageFormat = getBaseLevelInternalFormat();
if (mTexStorage &&
(newStorageWidth != oldStorageWidth || newStorageHeight != oldStorageHeight ||
newStorageDepth != oldStorageDepth || newStorageFormat != oldStorageFormat))
{
markAllImagesDirty();
SafeDelete(mTexStorage);
}
}
void TextureD3D::syncState(const gl::Texture::DirtyBits &dirtyBits)
{
// TODO(geofflang): Use dirty bits
}
TextureD3D_2D::TextureD3D_2D(const gl::TextureState &state, RendererD3D *renderer)
: TextureD3D(state, renderer)
{
mEGLImageTarget = false;
for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++i)
{
mImageArray[i] = renderer->createImage();
}
}
TextureD3D_2D::~TextureD3D_2D()
{
// Delete the Images before the TextureStorage.
// Images might be relying on the TextureStorage for some of their data.
// If TextureStorage is deleted before the Images, then their data will be wastefully copied back from the GPU before we delete the Images.
for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++i)
{
delete mImageArray[i];
}
SafeDelete(mTexStorage);
}
ImageD3D *TextureD3D_2D::getImage(int level, int layer) const
{
ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
ASSERT(layer == 0);
return mImageArray[level];
}
ImageD3D *TextureD3D_2D::getImage(const gl::ImageIndex &index) const
{
ASSERT(index.mipIndex < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
ASSERT(!index.hasLayer());
ASSERT(index.type == GL_TEXTURE_2D);
return mImageArray[index.mipIndex];
}
GLsizei TextureD3D_2D::getLayerCount(int level) const
{
ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
return 1;
}
GLsizei TextureD3D_2D::getWidth(GLint level) const
{
if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
return mImageArray[level]->getWidth();
else
return 0;
}
GLsizei TextureD3D_2D::getHeight(GLint level) const
{
if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
return mImageArray[level]->getHeight();
else
return 0;
}
GLenum TextureD3D_2D::getInternalFormat(GLint level) const
{
if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
return mImageArray[level]->getInternalFormat();
else
return GL_NONE;
}
bool TextureD3D_2D::isDepth(GLint level) const
{
return gl::GetInternalFormatInfo(getInternalFormat(level)).depthBits > 0;
}
gl::Error TextureD3D_2D::setImage(GLenum target,
size_t imageLevel,
GLenum internalFormat,
const gl::Extents &size,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_2D && size.depth == 1);
GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, type);
bool fastUnpacked = false;
GLint level = static_cast<GLint>(imageLevel);
redefineImage(level, sizedInternalFormat, size, false);
gl::ImageIndex index = gl::ImageIndex::Make2D(level);
// Attempt a fast gpu copy of the pixel data to the surface
if (isFastUnpackable(unpack, sizedInternalFormat) && isLevelComplete(level))
{
// Will try to create RT storage if it does not exist
RenderTargetD3D *destRenderTarget = NULL;
ANGLE_TRY(getRenderTarget(index, &destRenderTarget));
gl::Box destArea(0, 0, 0, getWidth(level), getHeight(level), 1);
ANGLE_TRY(fastUnpackPixels(unpack, pixels, destArea, sizedInternalFormat, type,
destRenderTarget));
// Ensure we don't overwrite our newly initialized data
mImageArray[level]->markClean();
fastUnpacked = true;
}
if (!fastUnpacked)
{
ANGLE_TRY(setImageImpl(index, type, unpack, pixels, 0));
}
return gl::NoError();
}
gl::Error TextureD3D_2D::setSubImage(GLenum target,
size_t imageLevel,
const gl::Box &area,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_2D && area.depth == 1 && area.z == 0);
GLint level = static_cast<GLint>(imageLevel);
gl::ImageIndex index = gl::ImageIndex::Make2D(level);
if (isFastUnpackable(unpack, getInternalFormat(level)) && isLevelComplete(level))
{
RenderTargetD3D *renderTarget = NULL;
ANGLE_TRY(getRenderTarget(index, &renderTarget));
ASSERT(!mImageArray[level]->isDirty());
return fastUnpackPixels(unpack, pixels, area, getInternalFormat(level), type, renderTarget);
}
else
{
return TextureD3D::subImage(index, area, format, type, unpack, pixels, 0);
}
}
gl::Error TextureD3D_2D::setCompressedImage(GLenum target,
size_t imageLevel,
GLenum internalFormat,
const gl::Extents &size,
const gl::PixelUnpackState &unpack,
size_t imageSize,
const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_2D && size.depth == 1);
GLint level = static_cast<GLint>(imageLevel);
// compressed formats don't have separate sized internal formats-- we can just use the compressed format directly
redefineImage(level, internalFormat, size, false);
return setCompressedImageImpl(gl::ImageIndex::Make2D(level), unpack, pixels, 0);
}
gl::Error TextureD3D_2D::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format,
const gl::PixelUnpackState &unpack, size_t imageSize, const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_2D && area.depth == 1 && area.z == 0);
gl::ImageIndex index = gl::ImageIndex::Make2D(static_cast<GLint>(level));
ANGLE_TRY(TextureD3D::subImageCompressed(index, area, format, unpack, pixels, 0));
return commitRegion(index, area);
}
gl::Error TextureD3D_2D::copyImage(GLenum target,
size_t imageLevel,
const gl::Rectangle &sourceArea,
GLenum internalFormat,
const gl::Framebuffer *source)
{
ASSERT(target == GL_TEXTURE_2D);
GLint level = static_cast<GLint>(imageLevel);
GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, GL_UNSIGNED_BYTE);
redefineImage(level, sizedInternalFormat, gl::Extents(sourceArea.width, sourceArea.height, 1),
false);
gl::ImageIndex index = gl::ImageIndex::Make2D(level);
gl::Offset destOffset(0, 0, 0);
// If the zero max LOD workaround is active, then we can't sample from individual layers of the framebuffer in shaders,
// so we should use the non-rendering copy path.
if (!canCreateRenderTargetForImage(index) || mRenderer->getWorkarounds().zeroMaxLodWorkaround)
{
ANGLE_TRY(mImageArray[level]->copyFromFramebuffer(destOffset, sourceArea, source));
mDirtyImages = true;
}
else
{
ANGLE_TRY(ensureRenderTarget());
mImageArray[level]->markClean();
if (sourceArea.width != 0 && sourceArea.height != 0 && isValidLevel(level))
{
ANGLE_TRY(mRenderer->copyImage2D(source, sourceArea, internalFormat, destOffset,
mTexStorage, level));
}
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_2D::copySubImage(GLenum target,
size_t imageLevel,
const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
const gl::Framebuffer *source)
{
ASSERT(target == GL_TEXTURE_2D && destOffset.z == 0);
// can only make our texture storage to a render target if level 0 is defined (with a width & height) and
// the current level we're copying to is defined (with appropriate format, width & height)
GLint level = static_cast<GLint>(imageLevel);
gl::ImageIndex index = gl::ImageIndex::Make2D(level);
// If the zero max LOD workaround is active, then we can't sample from individual layers of the framebuffer in shaders,
// so we should use the non-rendering copy path.
if (!canCreateRenderTargetForImage(index) || mRenderer->getWorkarounds().zeroMaxLodWorkaround)
{
ANGLE_TRY(mImageArray[level]->copyFromFramebuffer(destOffset, sourceArea, source));
mDirtyImages = true;
}
else
{
ANGLE_TRY(ensureRenderTarget());
if (isValidLevel(level))
{
ANGLE_TRY(updateStorageLevel(level));
ANGLE_TRY(mRenderer->copyImage2D(
source, sourceArea, gl::GetInternalFormatInfo(getBaseLevelInternalFormat()).format,
destOffset, mTexStorage, level));
}
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_2D::copyTexture(GLenum internalFormat,
GLenum type,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
const gl::Texture *source)
{
GLenum sourceTarget = source->getTarget();
GLint sourceLevel = 0;
GLint destLevel = 0;
GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, type);
gl::Extents size(static_cast<int>(source->getWidth(sourceTarget, sourceLevel)),
static_cast<int>(source->getHeight(sourceTarget, sourceLevel)), 1);
redefineImage(destLevel, sizedInternalFormat, size, false);
ASSERT(canCreateRenderTargetForImage(gl::ImageIndex::Make2D(destLevel)));
ANGLE_TRY(ensureRenderTarget());
ASSERT(isValidLevel(destLevel));
ANGLE_TRY(updateStorageLevel(destLevel));
gl::Rectangle sourceRect(0, 0, size.width, size.height);
gl::Offset destOffset(0, 0, 0);
ANGLE_TRY(mRenderer->copyTexture(source, sourceLevel, sourceRect,
gl::GetInternalFormatInfo(sizedInternalFormat).format,
destOffset, mTexStorage, destLevel, unpackFlipY,
unpackPremultiplyAlpha, unpackUnmultiplyAlpha));
return gl::NoError();
}
gl::Error TextureD3D_2D::copySubTexture(const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
const gl::Texture *source)
{
GLint sourceLevel = 0;
GLint destLevel = 0;
ASSERT(canCreateRenderTargetForImage(gl::ImageIndex::Make2D(destLevel)));
ANGLE_TRY(ensureRenderTarget());
ASSERT(isValidLevel(destLevel));
ANGLE_TRY(updateStorageLevel(destLevel));
ANGLE_TRY(mRenderer->copyTexture(source, sourceLevel, sourceArea,
gl::GetInternalFormatInfo(getBaseLevelInternalFormat()).format,
destOffset, mTexStorage, destLevel, unpackFlipY,
unpackPremultiplyAlpha, unpackUnmultiplyAlpha));
return gl::NoError();
}
gl::Error TextureD3D_2D::copyCompressedTexture(const gl::Texture *source)
{
GLenum sourceTarget = source->getTarget();
GLint sourceLevel = 0;
GLint destLevel = 0;
GLenum sizedInternalFormat = source->getFormat(sourceTarget, sourceLevel).asSized();
gl::Extents size(static_cast<int>(source->getWidth(sourceTarget, sourceLevel)),
static_cast<int>(source->getHeight(sourceTarget, sourceLevel)), 1);
redefineImage(destLevel, sizedInternalFormat, size, false);
ANGLE_TRY(initializeStorage(false));
ASSERT(mTexStorage);
ANGLE_TRY(mRenderer->copyCompressedTexture(source, sourceLevel, mTexStorage, destLevel));
return gl::NoError();
}
gl::Error TextureD3D_2D::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size)
{
ASSERT(GL_TEXTURE_2D && size.depth == 1);
for (size_t level = 0; level < levels; level++)
{
gl::Extents levelSize(std::max(1, size.width >> level),
std::max(1, size.height >> level),
1);
redefineImage(level, internalFormat, levelSize, true);
}
for (size_t level = levels; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++)
{
redefineImage(level, GL_NONE, gl::Extents(0, 0, 1), true);
}
// TODO(geofflang): Verify storage creation had no errors
bool renderTarget = IsRenderTargetUsage(mState.getUsage());
TextureStorage *storage = mRenderer->createTextureStorage2D(
internalFormat, renderTarget, size.width, size.height, static_cast<int>(levels), false);
gl::Error error = setCompleteTexStorage(storage);
if (error.isError())
{
SafeDelete(storage);
return error;
}
ANGLE_TRY(updateStorage());
mImmutable = true;
return gl::NoError();
}
void TextureD3D_2D::bindTexImage(egl::Surface *surface)
{
GLenum internalformat = surface->getConfig()->renderTargetFormat;
gl::Extents size(surface->getWidth(), surface->getHeight(), 1);
redefineImage(0, internalformat, size, true);
if (mTexStorage)
{
SafeDelete(mTexStorage);
}
SurfaceD3D *surfaceD3D = GetImplAs<SurfaceD3D>(surface);
ASSERT(surfaceD3D);
mTexStorage = mRenderer->createTextureStorage2D(surfaceD3D->getSwapChain());
mEGLImageTarget = false;
mDirtyImages = true;
}
void TextureD3D_2D::releaseTexImage()
{
if (mTexStorage)
{
SafeDelete(mTexStorage);
}
for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
{
redefineImage(i, GL_NONE, gl::Extents(0, 0, 1), true);
}
}
gl::Error TextureD3D_2D::setEGLImageTarget(GLenum target, egl::Image *image)
{
EGLImageD3D *eglImaged3d = GetImplAs<EGLImageD3D>(image);
// Set the properties of the base mip level from the EGL image
const auto &format = image->getFormat();
gl::Extents size(static_cast<int>(image->getWidth()), static_cast<int>(image->getHeight()), 1);
redefineImage(0, format.asSized(), size, true);
// Clear all other images.
for (size_t level = 1; level < ArraySize(mImageArray); level++)
{
redefineImage(level, GL_NONE, gl::Extents(0, 0, 1), true);
}
SafeDelete(mTexStorage);
mImageArray[0]->markClean();
// Pass in the RenderTargetD3D here: createTextureStorage can't generate an error.
RenderTargetD3D *renderTargetD3D = nullptr;
ANGLE_TRY(eglImaged3d->getRenderTarget(&renderTargetD3D));
mTexStorage = mRenderer->createTextureStorageEGLImage(eglImaged3d, renderTargetD3D);
mEGLImageTarget = true;
return gl::Error(GL_NO_ERROR);
}
void TextureD3D_2D::initMipmapImages()
{
const GLuint baseLevel = mState.getEffectiveBaseLevel();
const GLuint maxLevel = mState.getMipmapMaxLevel();
// Purge array levels baseLevel + 1 through q and reset them to represent the generated mipmap
// levels.
for (GLuint level = baseLevel + 1; level <= maxLevel; level++)
{
gl::Extents levelSize(std::max(getLevelZeroWidth() >> level, 1),
std::max(getLevelZeroHeight() >> level, 1), 1);
redefineImage(level, getBaseLevelInternalFormat(), levelSize, false);
}
}
gl::Error TextureD3D_2D::getRenderTarget(const gl::ImageIndex &index, RenderTargetD3D **outRT)
{
ASSERT(!index.hasLayer());
// ensure the underlying texture is created
ANGLE_TRY(ensureRenderTarget());
ANGLE_TRY(updateStorageLevel(index.mipIndex));
return mTexStorage->getRenderTarget(index, outRT);
}
bool TextureD3D_2D::isValidLevel(int level) const
{
return (mTexStorage ? (level >= 0 && level < mTexStorage->getLevelCount()) : false);
}
bool TextureD3D_2D::isLevelComplete(int level) const
{
if (isImmutable())
{
return true;
}
GLsizei width = getLevelZeroWidth();
GLsizei height = getLevelZeroHeight();
if (width <= 0 || height <= 0)
{
return false;
}
// The base image level is complete if the width and height are positive
if (level == static_cast<int>(getBaseLevel()))
{
return true;
}
ASSERT(level >= 0 && level <= (int)ArraySize(mImageArray) && mImageArray[level] != nullptr);
ImageD3D *image = mImageArray[level];
if (image->getInternalFormat() != getBaseLevelInternalFormat())
{
return false;
}
if (image->getWidth() != std::max(1, width >> level))
{
return false;
}
if (image->getHeight() != std::max(1, height >> level))
{
return false;
}
return true;
}
bool TextureD3D_2D::isImageComplete(const gl::ImageIndex &index) const
{
return isLevelComplete(index.mipIndex);
}
// Constructs a native texture resource from the texture images
gl::Error TextureD3D_2D::initializeStorage(bool renderTarget)
{
// Only initialize the first time this texture is used as a render target or shader resource
if (mTexStorage)
{
return gl::Error(GL_NO_ERROR);
}
// do not attempt to create storage for nonexistant data
if (!isLevelComplete(getBaseLevel()))
{
return gl::Error(GL_NO_ERROR);
}
bool createRenderTarget = (renderTarget || IsRenderTargetUsage(mState.getUsage()));
TextureStorage *storage = NULL;
ANGLE_TRY(createCompleteStorage(createRenderTarget, &storage));
gl::Error error = setCompleteTexStorage(storage);
if (error.isError())
{
SafeDelete(storage);
return error;
}
ASSERT(mTexStorage);
// flush image data to the storage
ANGLE_TRY(updateStorage());
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_2D::createCompleteStorage(bool renderTarget, TextureStorage **outTexStorage) const
{
GLsizei width = getLevelZeroWidth();
GLsizei height = getLevelZeroHeight();
GLenum internalFormat = getBaseLevelInternalFormat();
ASSERT(width > 0 && height > 0);
// use existing storage level count, when previously specified by TexStorage*D
GLint levels = (mTexStorage ? mTexStorage->getLevelCount() : creationLevels(width, height, 1));
bool hintLevelZeroOnly = false;
if (mRenderer->getWorkarounds().zeroMaxLodWorkaround)
{
// If any of the CPU images (levels >= 1) are dirty, then the textureStorage2D should use the mipped texture to begin with.
// Otherwise, it should use the level-zero-only texture.
hintLevelZeroOnly = true;
for (int level = 1; level < levels && hintLevelZeroOnly; level++)
{
hintLevelZeroOnly = !(mImageArray[level]->isDirty() && isLevelComplete(level));
}
}
// TODO(geofflang): Determine if the texture creation succeeded
*outTexStorage = mRenderer->createTextureStorage2D(internalFormat, renderTarget, width, height, levels, hintLevelZeroOnly);
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_2D::setCompleteTexStorage(TextureStorage *newCompleteTexStorage)
{
if (newCompleteTexStorage && newCompleteTexStorage->isManaged())
{
for (int level = 0; level < newCompleteTexStorage->getLevelCount(); level++)
{
ANGLE_TRY(mImageArray[level]->setManagedSurface2D(newCompleteTexStorage, level));
}
}
SafeDelete(mTexStorage);
mTexStorage = newCompleteTexStorage;
mDirtyImages = true;
return gl::NoError();
}
gl::Error TextureD3D_2D::updateStorage()
{
ASSERT(mTexStorage != NULL);
GLint storageLevels = mTexStorage->getLevelCount();
for (int level = 0; level < storageLevels; level++)
{
if (mImageArray[level]->isDirty() && isLevelComplete(level))
{
ANGLE_TRY(updateStorageLevel(level));
}
}
return gl::NoError();
}
gl::Error TextureD3D_2D::updateStorageLevel(int level)
{
ASSERT(level <= (int)ArraySize(mImageArray) && mImageArray[level] != NULL);
ASSERT(isLevelComplete(level));
if (mImageArray[level]->isDirty())
{
gl::ImageIndex index = gl::ImageIndex::Make2D(level);
gl::Box region(0, 0, 0, getWidth(level), getHeight(level), 1);
ANGLE_TRY(commitRegion(index, region));
}
return gl::NoError();
}
void TextureD3D_2D::redefineImage(size_t level,
GLenum internalformat,
const gl::Extents &size,
bool forceRelease)
{
ASSERT(size.depth == 1);
// If there currently is a corresponding storage texture image, it has these parameters
const int storageWidth = std::max(1, getLevelZeroWidth() >> level);
const int storageHeight = std::max(1, getLevelZeroHeight() >> level);
const GLenum storageFormat = getBaseLevelInternalFormat();
mImageArray[level]->redefine(GL_TEXTURE_2D, internalformat, size, forceRelease);
if (mTexStorage)
{
const size_t storageLevels = mTexStorage->getLevelCount();
// If the storage was from an EGL image, copy it back into local images to preserve it
// while orphaning
if (level != 0 && mEGLImageTarget)
{
// TODO(jmadill): Don't discard error.
mImageArray[0]->copyFromTexStorage(gl::ImageIndex::Make2D(0), mTexStorage);
}
if ((level >= storageLevels && storageLevels != 0) ||
size.width != storageWidth ||
size.height != storageHeight ||
internalformat != storageFormat) // Discard mismatched storage
{
markAllImagesDirty();
SafeDelete(mTexStorage);
}
}
// Can't be an EGL image target after being redefined
mEGLImageTarget = false;
}
gl::ImageIndexIterator TextureD3D_2D::imageIterator() const
{
return gl::ImageIndexIterator::Make2D(0, mTexStorage->getLevelCount());
}
gl::ImageIndex TextureD3D_2D::getImageIndex(GLint mip, GLint /*layer*/) const
{
// "layer" does not apply to 2D Textures.
return gl::ImageIndex::Make2D(mip);
}
bool TextureD3D_2D::isValidIndex(const gl::ImageIndex &index) const
{
return (mTexStorage && index.type == GL_TEXTURE_2D &&
index.mipIndex >= 0 && index.mipIndex < mTexStorage->getLevelCount());
}
void TextureD3D_2D::markAllImagesDirty()
{
for (size_t i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
{
mImageArray[i]->markDirty();
}
mDirtyImages = true;
}
TextureD3D_Cube::TextureD3D_Cube(const gl::TextureState &state, RendererD3D *renderer)
: TextureD3D(state, renderer)
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++j)
{
mImageArray[i][j] = renderer->createImage();
}
}
}
TextureD3D_Cube::~TextureD3D_Cube()
{
// Delete the Images before the TextureStorage.
// Images might be relying on the TextureStorage for some of their data.
// If TextureStorage is deleted before the Images, then their data will be wastefully copied back from the GPU before we delete the Images.
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++j)
{
SafeDelete(mImageArray[i][j]);
}
}
SafeDelete(mTexStorage);
}
ImageD3D *TextureD3D_Cube::getImage(int level, int layer) const
{
ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
ASSERT(layer >= 0 && layer < 6);
return mImageArray[layer][level];
}
ImageD3D *TextureD3D_Cube::getImage(const gl::ImageIndex &index) const
{
ASSERT(index.mipIndex < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
ASSERT(index.layerIndex >= 0 && index.layerIndex < 6);
return mImageArray[index.layerIndex][index.mipIndex];
}
GLsizei TextureD3D_Cube::getLayerCount(int level) const
{
ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
return 6;
}
GLenum TextureD3D_Cube::getInternalFormat(GLint level, GLint layer) const
{
if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
return mImageArray[layer][level]->getInternalFormat();
else
return GL_NONE;
}
bool TextureD3D_Cube::isDepth(GLint level, GLint layer) const
{
return gl::GetInternalFormatInfo(getInternalFormat(level, layer)).depthBits > 0;
}
gl::Error TextureD3D_Cube::setEGLImageTarget(GLenum target, egl::Image *image)
{
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureD3D_Cube::setImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, GLenum format, GLenum type,
const gl::PixelUnpackState &unpack, const uint8_t *pixels)
{
ASSERT(size.depth == 1);
GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, type);
gl::ImageIndex index = gl::ImageIndex::MakeCube(target, static_cast<GLint>(level));
redefineImage(index.layerIndex, static_cast<GLint>(level), sizedInternalFormat, size);
return setImageImpl(index, type, unpack, pixels, 0);
}
gl::Error TextureD3D_Cube::setSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, GLenum type,
const gl::PixelUnpackState &unpack, const uint8_t *pixels)
{
ASSERT(area.depth == 1 && area.z == 0);
gl::ImageIndex index = gl::ImageIndex::MakeCube(target, static_cast<GLint>(level));
return TextureD3D::subImage(index, area, format, type, unpack, pixels, 0);
}
gl::Error TextureD3D_Cube::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size,
const gl::PixelUnpackState &unpack, size_t imageSize, const uint8_t *pixels)
{
ASSERT(size.depth == 1);
// compressed formats don't have separate sized internal formats-- we can just use the compressed format directly
size_t faceIndex = gl::CubeMapTextureTargetToLayerIndex(target);
redefineImage(static_cast<int>(faceIndex), static_cast<GLint>(level), internalFormat, size);
gl::ImageIndex index = gl::ImageIndex::MakeCube(target, static_cast<GLint>(level));
return setCompressedImageImpl(index, unpack, pixels, 0);
}
gl::Error TextureD3D_Cube::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format,
const gl::PixelUnpackState &unpack, size_t imageSize, const uint8_t *pixels)
{
ASSERT(area.depth == 1 && area.z == 0);
gl::ImageIndex index = gl::ImageIndex::MakeCube(target, static_cast<GLint>(level));
ANGLE_TRY(TextureD3D::subImageCompressed(index, area, format, unpack, pixels, 0));
return commitRegion(index, area);
}
gl::Error TextureD3D_Cube::copyImage(GLenum target,
size_t imageLevel,
const gl::Rectangle &sourceArea,
GLenum internalFormat,
const gl::Framebuffer *source)
{
int faceIndex = static_cast<int>(gl::CubeMapTextureTargetToLayerIndex(target));
GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, GL_UNSIGNED_BYTE);
GLint level = static_cast<GLint>(imageLevel);
gl::Extents size(sourceArea.width, sourceArea.height, 1);
redefineImage(static_cast<int>(faceIndex), level, sizedInternalFormat, size);
gl::ImageIndex index = gl::ImageIndex::MakeCube(target, level);
gl::Offset destOffset(0, 0, 0);
// If the zero max LOD workaround is active, then we can't sample from individual layers of the framebuffer in shaders,
// so we should use the non-rendering copy path.
if (!canCreateRenderTargetForImage(index) || mRenderer->getWorkarounds().zeroMaxLodWorkaround)
{
ANGLE_TRY(
mImageArray[faceIndex][level]->copyFromFramebuffer(destOffset, sourceArea, source));
mDirtyImages = true;
}
else
{
ANGLE_TRY(ensureRenderTarget());
mImageArray[faceIndex][level]->markClean();
ASSERT(size.width == size.height);
if (size.width > 0 && isValidFaceLevel(faceIndex, level))
{
ANGLE_TRY(mRenderer->copyImageCube(source, sourceArea, internalFormat, destOffset,
mTexStorage, target, level));
}
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_Cube::copySubImage(GLenum target,
size_t imageLevel,
const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
const gl::Framebuffer *source)
{
int faceIndex = static_cast<int>(gl::CubeMapTextureTargetToLayerIndex(target));
GLint level = static_cast<GLint>(imageLevel);
gl::ImageIndex index = gl::ImageIndex::MakeCube(target, level);
// If the zero max LOD workaround is active, then we can't sample from individual layers of the framebuffer in shaders,
// so we should use the non-rendering copy path.
if (!canCreateRenderTargetForImage(index) || mRenderer->getWorkarounds().zeroMaxLodWorkaround)
{
gl::Error error =
mImageArray[faceIndex][level]->copyFromFramebuffer(destOffset, sourceArea, source);
if (error.isError())
{
return error;
}
mDirtyImages = true;
}
else
{
ANGLE_TRY(ensureRenderTarget());
if (isValidFaceLevel(faceIndex, level))
{
ANGLE_TRY(updateStorageFaceLevel(faceIndex, level));
ANGLE_TRY(mRenderer->copyImageCube(
source, sourceArea, gl::GetInternalFormatInfo(getBaseLevelInternalFormat()).format,
destOffset, mTexStorage, target, level));
}
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_Cube::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size)
{
ASSERT(size.width == size.height);
ASSERT(size.depth == 1);
for (size_t level = 0; level < levels; level++)
{
GLsizei mipSize = std::max(1, size.width >> level);
for (int faceIndex = 0; faceIndex < 6; faceIndex++)
{
mImageArray[faceIndex][level]->redefine(GL_TEXTURE_CUBE_MAP, internalFormat, gl::Extents(mipSize, mipSize, 1), true);
}
}
for (size_t level = levels; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++)
{
for (int faceIndex = 0; faceIndex < 6; faceIndex++)
{
mImageArray[faceIndex][level]->redefine(GL_TEXTURE_CUBE_MAP, GL_NONE, gl::Extents(0, 0, 0), true);
}
}
// TODO(geofflang): Verify storage creation had no errors
bool renderTarget = IsRenderTargetUsage(mState.getUsage());
TextureStorage *storage = mRenderer->createTextureStorageCube(
internalFormat, renderTarget, size.width, static_cast<int>(levels), false);
gl::Error error = setCompleteTexStorage(storage);
if (error.isError())
{
SafeDelete(storage);
return error;
}
ANGLE_TRY(updateStorage());
mImmutable = true;
return gl::NoError();
}
// Tests for cube texture completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81.
bool TextureD3D_Cube::isCubeComplete() const
{
int baseWidth = getBaseLevelWidth();
int baseHeight = getBaseLevelHeight();
GLenum baseFormat = getBaseLevelInternalFormat();
if (baseWidth <= 0 || baseWidth != baseHeight)
{
return false;
}
for (int faceIndex = 1; faceIndex < 6; faceIndex++)
{
const ImageD3D &faceBaseImage = *mImageArray[faceIndex][getBaseLevel()];
if (faceBaseImage.getWidth() != baseWidth ||
faceBaseImage.getHeight() != baseHeight ||
faceBaseImage.getInternalFormat() != baseFormat )
{
return false;
}
}
return true;
}
void TextureD3D_Cube::bindTexImage(egl::Surface *surface)
{
UNREACHABLE();
}
void TextureD3D_Cube::releaseTexImage()
{
UNREACHABLE();
}
void TextureD3D_Cube::initMipmapImages()
{
const GLuint baseLevel = mState.getEffectiveBaseLevel();
const GLuint maxLevel = mState.getMipmapMaxLevel();
// Purge array levels baseLevel + 1 through q and reset them to represent the generated mipmap
// levels.
for (int faceIndex = 0; faceIndex < 6; faceIndex++)
{
for (GLuint level = baseLevel + 1; level <= maxLevel; level++)
{
int faceLevelSize =
(std::max(mImageArray[faceIndex][baseLevel]->getWidth() >> (level - baseLevel), 1));
redefineImage(faceIndex, level, mImageArray[faceIndex][baseLevel]->getInternalFormat(),
gl::Extents(faceLevelSize, faceLevelSize, 1));
}
}
}
gl::Error TextureD3D_Cube::getRenderTarget(const gl::ImageIndex &index, RenderTargetD3D **outRT)
{
ASSERT(gl::IsCubeMapTextureTarget(index.type));
// ensure the underlying texture is created
ANGLE_TRY(ensureRenderTarget());
ANGLE_TRY(updateStorageFaceLevel(index.layerIndex, index.mipIndex));
return mTexStorage->getRenderTarget(index, outRT);
}
gl::Error TextureD3D_Cube::initializeStorage(bool renderTarget)
{
// Only initialize the first time this texture is used as a render target or shader resource
if (mTexStorage)
{
return gl::NoError();
}
// do not attempt to create storage for nonexistant data
if (!isFaceLevelComplete(0, getBaseLevel()))
{
return gl::NoError();
}
bool createRenderTarget = (renderTarget || IsRenderTargetUsage(mState.getUsage()));
TextureStorage *storage = NULL;
ANGLE_TRY(createCompleteStorage(createRenderTarget, &storage));
gl::Error error = setCompleteTexStorage(storage);
if (error.isError())
{
SafeDelete(storage);
return error;
}
ASSERT(mTexStorage);
// flush image data to the storage
ANGLE_TRY(updateStorage());
return gl::NoError();
}
gl::Error TextureD3D_Cube::createCompleteStorage(bool renderTarget, TextureStorage **outTexStorage) const
{
GLsizei size = getLevelZeroWidth();
ASSERT(size > 0);
// use existing storage level count, when previously specified by TexStorage*D
GLint levels = (mTexStorage ? mTexStorage->getLevelCount() : creationLevels(size, size, 1));
bool hintLevelZeroOnly = false;
if (mRenderer->getWorkarounds().zeroMaxLodWorkaround)
{
// If any of the CPU images (levels >= 1) are dirty, then the textureStorage should use the mipped texture to begin with.
// Otherwise, it should use the level-zero-only texture.
hintLevelZeroOnly = true;
for (int faceIndex = 0; faceIndex < 6 && hintLevelZeroOnly; faceIndex++)
{
for (int level = 1; level < levels && hintLevelZeroOnly; level++)
{
hintLevelZeroOnly = !(mImageArray[faceIndex][level]->isDirty() && isFaceLevelComplete(faceIndex, level));
}
}
}
// TODO (geofflang): detect if storage creation succeeded
*outTexStorage = mRenderer->createTextureStorageCube(getBaseLevelInternalFormat(), renderTarget, size, levels, hintLevelZeroOnly);
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_Cube::setCompleteTexStorage(TextureStorage *newCompleteTexStorage)
{
if (newCompleteTexStorage && newCompleteTexStorage->isManaged())
{
for (int faceIndex = 0; faceIndex < 6; faceIndex++)
{
for (int level = 0; level < newCompleteTexStorage->getLevelCount(); level++)
{
ANGLE_TRY(mImageArray[faceIndex][level]->setManagedSurfaceCube(
newCompleteTexStorage, faceIndex, level));
}
}
}
SafeDelete(mTexStorage);
mTexStorage = newCompleteTexStorage;
mDirtyImages = true;
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_Cube::updateStorage()
{
ASSERT(mTexStorage != NULL);
GLint storageLevels = mTexStorage->getLevelCount();
for (int face = 0; face < 6; face++)
{
for (int level = 0; level < storageLevels; level++)
{
if (mImageArray[face][level]->isDirty() && isFaceLevelComplete(face, level))
{
ANGLE_TRY(updateStorageFaceLevel(face, level));
}
}
}
return gl::NoError();
}
bool TextureD3D_Cube::isValidFaceLevel(int faceIndex, int level) const
{
return (mTexStorage ? (level >= 0 && level < mTexStorage->getLevelCount()) : 0);
}
bool TextureD3D_Cube::isFaceLevelComplete(int faceIndex, int level) const
{
if (getBaseLevel() >= gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
{
return false;
}
ASSERT(level >= 0 && faceIndex < 6 && level < (int)ArraySize(mImageArray[faceIndex]) &&
mImageArray[faceIndex][level] != nullptr);
if (isImmutable())
{
return true;
}
int levelZeroSize = getLevelZeroWidth();
if (levelZeroSize <= 0)
{
return false;
}
// "isCubeComplete" checks for base level completeness and we must call that
// to determine if any face at level 0 is complete. We omit that check here
// to avoid re-checking cube-completeness for every face at level 0.
if (level == 0)
{
return true;
}
// Check that non-zero levels are consistent with the base level.
const ImageD3D *faceLevelImage = mImageArray[faceIndex][level];
if (faceLevelImage->getInternalFormat() != getBaseLevelInternalFormat())
{
return false;
}
if (faceLevelImage->getWidth() != std::max(1, levelZeroSize >> level))
{
return false;
}
return true;
}
bool TextureD3D_Cube::isImageComplete(const gl::ImageIndex &index) const
{
return isFaceLevelComplete(index.layerIndex, index.mipIndex);
}
gl::Error TextureD3D_Cube::updateStorageFaceLevel(int faceIndex, int level)
{
ASSERT(level >= 0 && faceIndex < 6 && level < (int)ArraySize(mImageArray[faceIndex]) && mImageArray[faceIndex][level] != NULL);
ImageD3D *image = mImageArray[faceIndex][level];
if (image->isDirty())
{
GLenum faceTarget = gl::LayerIndexToCubeMapTextureTarget(faceIndex);
gl::ImageIndex index = gl::ImageIndex::MakeCube(faceTarget, level);
gl::Box region(0, 0, 0, image->getWidth(), image->getHeight(), 1);
ANGLE_TRY(commitRegion(index, region));
}
return gl::NoError();
}
void TextureD3D_Cube::redefineImage(int faceIndex, GLint level, GLenum internalformat, const gl::Extents &size)
{
// If there currently is a corresponding storage texture image, it has these parameters
const int storageWidth = std::max(1, getLevelZeroWidth() >> level);
const int storageHeight = std::max(1, getLevelZeroHeight() >> level);
const GLenum storageFormat = getBaseLevelInternalFormat();
mImageArray[faceIndex][level]->redefine(GL_TEXTURE_CUBE_MAP, internalformat, size, false);
if (mTexStorage)
{
const int storageLevels = mTexStorage->getLevelCount();
if ((level >= storageLevels && storageLevels != 0) ||
size.width != storageWidth ||
size.height != storageHeight ||
internalformat != storageFormat) // Discard mismatched storage
{
markAllImagesDirty();
SafeDelete(mTexStorage);
}
}
}
gl::ImageIndexIterator TextureD3D_Cube::imageIterator() const
{
return gl::ImageIndexIterator::MakeCube(0, mTexStorage->getLevelCount());
}
gl::ImageIndex TextureD3D_Cube::getImageIndex(GLint mip, GLint layer) const
{
// The "layer" of the image index corresponds to the cube face
return gl::ImageIndex::MakeCube(gl::LayerIndexToCubeMapTextureTarget(layer), mip);
}
bool TextureD3D_Cube::isValidIndex(const gl::ImageIndex &index) const
{
return (mTexStorage && gl::IsCubeMapTextureTarget(index.type) &&
index.mipIndex >= 0 && index.mipIndex < mTexStorage->getLevelCount());
}
void TextureD3D_Cube::markAllImagesDirty()
{
for (int dirtyLevel = 0; dirtyLevel < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; dirtyLevel++)
{
for (int dirtyFace = 0; dirtyFace < 6; dirtyFace++)
{
mImageArray[dirtyFace][dirtyLevel]->markDirty();
}
}
mDirtyImages = true;
}
TextureD3D_3D::TextureD3D_3D(const gl::TextureState &state, RendererD3D *renderer)
: TextureD3D(state, renderer)
{
for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++i)
{
mImageArray[i] = renderer->createImage();
}
}
TextureD3D_3D::~TextureD3D_3D()
{
// Delete the Images before the TextureStorage.
// Images might be relying on the TextureStorage for some of their data.
// If TextureStorage is deleted before the Images, then their data will be wastefully copied back from the GPU before we delete the Images.
for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++i)
{
delete mImageArray[i];
}
SafeDelete(mTexStorage);
}
ImageD3D *TextureD3D_3D::getImage(int level, int layer) const
{
ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
ASSERT(layer == 0);
return mImageArray[level];
}
ImageD3D *TextureD3D_3D::getImage(const gl::ImageIndex &index) const
{
ASSERT(index.mipIndex < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
ASSERT(!index.hasLayer());
ASSERT(index.type == GL_TEXTURE_3D);
return mImageArray[index.mipIndex];
}
GLsizei TextureD3D_3D::getLayerCount(int level) const
{
ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
return 1;
}
GLsizei TextureD3D_3D::getWidth(GLint level) const
{
if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
return mImageArray[level]->getWidth();
else
return 0;
}
GLsizei TextureD3D_3D::getHeight(GLint level) const
{
if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
return mImageArray[level]->getHeight();
else
return 0;
}
GLsizei TextureD3D_3D::getDepth(GLint level) const
{
if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
return mImageArray[level]->getDepth();
else
return 0;
}
GLenum TextureD3D_3D::getInternalFormat(GLint level) const
{
if (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
return mImageArray[level]->getInternalFormat();
else
return GL_NONE;
}
bool TextureD3D_3D::isDepth(GLint level) const
{
return gl::GetInternalFormatInfo(getInternalFormat(level)).depthBits > 0;
}
gl::Error TextureD3D_3D::setEGLImageTarget(GLenum target, egl::Image *image)
{
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureD3D_3D::setImage(GLenum target,
size_t imageLevel,
GLenum internalFormat,
const gl::Extents &size,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_3D);
GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, type);
GLint level = static_cast<GLint>(imageLevel);
redefineImage(level, sizedInternalFormat, size);
bool fastUnpacked = false;
gl::ImageIndex index = gl::ImageIndex::Make3D(level);
// Attempt a fast gpu copy of the pixel data to the surface if the app bound an unpack buffer
if (isFastUnpackable(unpack, sizedInternalFormat) && !size.empty() && isLevelComplete(level))
{
// Will try to create RT storage if it does not exist
RenderTargetD3D *destRenderTarget = NULL;
ANGLE_TRY(getRenderTarget(index, &destRenderTarget));
gl::Box destArea(0, 0, 0, getWidth(level), getHeight(level), getDepth(level));
ANGLE_TRY(fastUnpackPixels(unpack, pixels, destArea, sizedInternalFormat, type,
destRenderTarget));
// Ensure we don't overwrite our newly initialized data
mImageArray[level]->markClean();
fastUnpacked = true;
}
if (!fastUnpacked)
{
ANGLE_TRY(setImageImpl(index, type, unpack, pixels, 0));
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_3D::setSubImage(GLenum target,
size_t imageLevel,
const gl::Box &area,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_3D);
GLint level = static_cast<GLint>(imageLevel);
gl::ImageIndex index = gl::ImageIndex::Make3D(level);
// Attempt a fast gpu copy of the pixel data to the surface if the app bound an unpack buffer
if (isFastUnpackable(unpack, getInternalFormat(level)) && isLevelComplete(level))
{
RenderTargetD3D *destRenderTarget = NULL;
ANGLE_TRY(getRenderTarget(index, &destRenderTarget));
ASSERT(!mImageArray[level]->isDirty());
return fastUnpackPixels(unpack, pixels, area, getInternalFormat(level), type, destRenderTarget);
}
else
{
return TextureD3D::subImage(index, area, format, type, unpack, pixels, 0);
}
}
gl::Error TextureD3D_3D::setCompressedImage(GLenum target,
size_t imageLevel,
GLenum internalFormat,
const gl::Extents &size,
const gl::PixelUnpackState &unpack,
size_t imageSize,
const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_3D);
GLint level = static_cast<GLint>(imageLevel);
// compressed formats don't have separate sized internal formats-- we can just use the compressed format directly
redefineImage(level, internalFormat, size);
gl::ImageIndex index = gl::ImageIndex::Make3D(level);
return setCompressedImageImpl(index, unpack, pixels, 0);
}
gl::Error TextureD3D_3D::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format,
const gl::PixelUnpackState &unpack, size_t imageSize, const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_3D);
gl::ImageIndex index = gl::ImageIndex::Make3D(static_cast<GLint>(level));
ANGLE_TRY(TextureD3D::subImageCompressed(index, area, format, unpack, pixels, 0));
return commitRegion(index, area);
}
gl::Error TextureD3D_3D::copyImage(GLenum target, size_t level, const gl::Rectangle &sourceArea, GLenum internalFormat,
const gl::Framebuffer *source)
{
UNIMPLEMENTED();
return gl::Error(GL_INVALID_OPERATION, "Copying 3D textures is unimplemented.");
}
gl::Error TextureD3D_3D::copySubImage(GLenum target,
size_t imageLevel,
const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
const gl::Framebuffer *source)
{
ASSERT(target == GL_TEXTURE_3D);
GLint level = static_cast<GLint>(imageLevel);
gl::ImageIndex index = gl::ImageIndex::Make3D(level);
if (canCreateRenderTargetForImage(index))
{
ANGLE_TRY(mImageArray[level]->copyFromFramebuffer(destOffset, sourceArea, source));
mDirtyImages = true;
}
else
{
ANGLE_TRY(ensureRenderTarget());
if (isValidLevel(level))
{
ANGLE_TRY(updateStorageLevel(level));
ANGLE_TRY(mRenderer->copyImage3D(
source, sourceArea, gl::GetInternalFormatInfo(getBaseLevelInternalFormat()).format,
destOffset, mTexStorage, level));
}
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_3D::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size)
{
ASSERT(target == GL_TEXTURE_3D);
for (size_t level = 0; level < levels; level++)
{
gl::Extents levelSize(std::max(1, size.width >> level),
std::max(1, size.height >> level),
std::max(1, size.depth >> level));
mImageArray[level]->redefine(GL_TEXTURE_3D, internalFormat, levelSize, true);
}
for (size_t level = levels; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++)
{
mImageArray[level]->redefine(GL_TEXTURE_3D, GL_NONE, gl::Extents(0, 0, 0), true);
}
// TODO(geofflang): Verify storage creation had no errors
bool renderTarget = IsRenderTargetUsage(mState.getUsage());
TextureStorage *storage =
mRenderer->createTextureStorage3D(internalFormat, renderTarget, size.width, size.height,
size.depth, static_cast<int>(levels));
gl::Error error = setCompleteTexStorage(storage);
if (error.isError())
{
SafeDelete(storage);
return error;
}
ANGLE_TRY(updateStorage());
mImmutable = true;
return gl::NoError();
}
void TextureD3D_3D::bindTexImage(egl::Surface *surface)
{
UNREACHABLE();
}
void TextureD3D_3D::releaseTexImage()
{
UNREACHABLE();
}
void TextureD3D_3D::initMipmapImages()
{
const GLuint baseLevel = mState.getEffectiveBaseLevel();
const GLuint maxLevel = mState.getMipmapMaxLevel();
// Purge array levels baseLevel + 1 through q and reset them to represent the generated mipmap
// levels.
for (GLuint level = baseLevel + 1; level <= maxLevel; level++)
{
gl::Extents levelSize(std::max(getLevelZeroWidth() >> level, 1),
std::max(getLevelZeroHeight() >> level, 1),
std::max(getLevelZeroDepth() >> level, 1));
redefineImage(level, getBaseLevelInternalFormat(), levelSize);
}
}
gl::Error TextureD3D_3D::getRenderTarget(const gl::ImageIndex &index, RenderTargetD3D **outRT)
{
// ensure the underlying texture is created
ANGLE_TRY(ensureRenderTarget());
if (index.hasLayer())
{
ANGLE_TRY(updateStorage());
}
else
{
ANGLE_TRY(updateStorageLevel(index.mipIndex));
}
return mTexStorage->getRenderTarget(index, outRT);
}
gl::Error TextureD3D_3D::initializeStorage(bool renderTarget)
{
// Only initialize the first time this texture is used as a render target or shader resource
if (mTexStorage)
{
return gl::Error(GL_NO_ERROR);
}
// do not attempt to create storage for nonexistant data
if (!isLevelComplete(getBaseLevel()))
{
return gl::Error(GL_NO_ERROR);
}
bool createRenderTarget = (renderTarget || IsRenderTargetUsage(mState.getUsage()));
TextureStorage *storage = NULL;
ANGLE_TRY(createCompleteStorage(createRenderTarget, &storage));
gl::Error error = setCompleteTexStorage(storage);
if (error.isError())
{
SafeDelete(storage);
return error;
}
ASSERT(mTexStorage);
// flush image data to the storage
ANGLE_TRY(updateStorage());
return gl::NoError();
}
gl::Error TextureD3D_3D::createCompleteStorage(bool renderTarget, TextureStorage **outStorage) const
{
GLsizei width = getLevelZeroWidth();
GLsizei height = getLevelZeroHeight();
GLsizei depth = getLevelZeroDepth();
GLenum internalFormat = getBaseLevelInternalFormat();
ASSERT(width > 0 && height > 0 && depth > 0);
// use existing storage level count, when previously specified by TexStorage*D
GLint levels = (mTexStorage ? mTexStorage->getLevelCount() : creationLevels(width, height, depth));
// TODO: Verify creation of the storage succeeded
*outStorage = mRenderer->createTextureStorage3D(internalFormat, renderTarget, width, height, depth, levels);
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_3D::setCompleteTexStorage(TextureStorage *newCompleteTexStorage)
{
SafeDelete(mTexStorage);
mTexStorage = newCompleteTexStorage;
mDirtyImages = true;
// We do not support managed 3D storage, as that is D3D9/ES2-only
ASSERT(!mTexStorage->isManaged());
return gl::NoError();
}
gl::Error TextureD3D_3D::updateStorage()
{
ASSERT(mTexStorage != NULL);
GLint storageLevels = mTexStorage->getLevelCount();
for (int level = 0; level < storageLevels; level++)
{
if (mImageArray[level]->isDirty() && isLevelComplete(level))
{
ANGLE_TRY(updateStorageLevel(level));
}
}
return gl::NoError();
}
bool TextureD3D_3D::isValidLevel(int level) const
{
return (mTexStorage ? (level >= 0 && level < mTexStorage->getLevelCount()) : 0);
}
bool TextureD3D_3D::isLevelComplete(int level) const
{
ASSERT(level >= 0 && level < (int)ArraySize(mImageArray) && mImageArray[level] != NULL);
if (isImmutable())
{
return true;
}
GLsizei width = getLevelZeroWidth();
GLsizei height = getLevelZeroHeight();
GLsizei depth = getLevelZeroDepth();
if (width <= 0 || height <= 0 || depth <= 0)
{
return false;
}
if (level == static_cast<int>(getBaseLevel()))
{
return true;
}
ImageD3D *levelImage = mImageArray[level];
if (levelImage->getInternalFormat() != getBaseLevelInternalFormat())
{
return false;
}
if (levelImage->getWidth() != std::max(1, width >> level))
{
return false;
}
if (levelImage->getHeight() != std::max(1, height >> level))
{
return false;
}
if (levelImage->getDepth() != std::max(1, depth >> level))
{
return false;
}
return true;
}
bool TextureD3D_3D::isImageComplete(const gl::ImageIndex &index) const
{
return isLevelComplete(index.mipIndex);
}
gl::Error TextureD3D_3D::updateStorageLevel(int level)
{
ASSERT(level >= 0 && level < (int)ArraySize(mImageArray) && mImageArray[level] != NULL);
ASSERT(isLevelComplete(level));
if (mImageArray[level]->isDirty())
{
gl::ImageIndex index = gl::ImageIndex::Make3D(level);
gl::Box region(0, 0, 0, getWidth(level), getHeight(level), getDepth(level));
ANGLE_TRY(commitRegion(index, region));
}
return gl::NoError();
}
void TextureD3D_3D::redefineImage(GLint level, GLenum internalformat, const gl::Extents &size)
{
// If there currently is a corresponding storage texture image, it has these parameters
const int storageWidth = std::max(1, getLevelZeroWidth() >> level);
const int storageHeight = std::max(1, getLevelZeroHeight() >> level);
const int storageDepth = std::max(1, getLevelZeroDepth() >> level);
const GLenum storageFormat = getBaseLevelInternalFormat();
mImageArray[level]->redefine(GL_TEXTURE_3D, internalformat, size, false);
if (mTexStorage)
{
const int storageLevels = mTexStorage->getLevelCount();
if ((level >= storageLevels && storageLevels != 0) ||
size.width != storageWidth ||
size.height != storageHeight ||
size.depth != storageDepth ||
internalformat != storageFormat) // Discard mismatched storage
{
markAllImagesDirty();
SafeDelete(mTexStorage);
}
}
}
gl::ImageIndexIterator TextureD3D_3D::imageIterator() const
{
return gl::ImageIndexIterator::Make3D(0, mTexStorage->getLevelCount(),
gl::ImageIndex::ENTIRE_LEVEL, gl::ImageIndex::ENTIRE_LEVEL);
}
gl::ImageIndex TextureD3D_3D::getImageIndex(GLint mip, GLint /*layer*/) const
{
// The "layer" here does not apply to 3D images. We use one Image per mip.
return gl::ImageIndex::Make3D(mip);
}
bool TextureD3D_3D::isValidIndex(const gl::ImageIndex &index) const
{
return (mTexStorage && index.type == GL_TEXTURE_3D &&
index.mipIndex >= 0 && index.mipIndex < mTexStorage->getLevelCount());
}
void TextureD3D_3D::markAllImagesDirty()
{
for (int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++)
{
mImageArray[i]->markDirty();
}
mDirtyImages = true;
}
GLint TextureD3D_3D::getLevelZeroDepth() const
{
ASSERT(gl::CountLeadingZeros(static_cast<uint32_t>(getBaseLevelDepth())) > getBaseLevel());
return getBaseLevelDepth() << getBaseLevel();
}
TextureD3D_2DArray::TextureD3D_2DArray(const gl::TextureState &state, RendererD3D *renderer)
: TextureD3D(state, renderer)
{
for (int level = 0; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++level)
{
mLayerCounts[level] = 0;
mImageArray[level] = NULL;
}
}
TextureD3D_2DArray::~TextureD3D_2DArray()
{
// Delete the Images before the TextureStorage.
// Images might be relying on the TextureStorage for some of their data.
// If TextureStorage is deleted before the Images, then their data will be wastefully copied back from the GPU before we delete the Images.
deleteImages();
SafeDelete(mTexStorage);
}
ImageD3D *TextureD3D_2DArray::getImage(int level, int layer) const
{
ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
ASSERT((layer == 0 && mLayerCounts[level] == 0) ||
layer < mLayerCounts[level]);
return (mImageArray[level] ? mImageArray[level][layer] : NULL);
}
ImageD3D *TextureD3D_2DArray::getImage(const gl::ImageIndex &index) const
{
ASSERT(index.mipIndex < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
ASSERT((index.layerIndex == 0 && mLayerCounts[index.mipIndex] == 0) ||
index.layerIndex < mLayerCounts[index.mipIndex]);
ASSERT(index.type == GL_TEXTURE_2D_ARRAY);
return (mImageArray[index.mipIndex] ? mImageArray[index.mipIndex][index.layerIndex] : NULL);
}
GLsizei TextureD3D_2DArray::getLayerCount(int level) const
{
ASSERT(level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS);
return mLayerCounts[level];
}
GLsizei TextureD3D_2DArray::getWidth(GLint level) const
{
return (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS && mLayerCounts[level] > 0) ? mImageArray[level][0]->getWidth() : 0;
}
GLsizei TextureD3D_2DArray::getHeight(GLint level) const
{
return (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS && mLayerCounts[level] > 0) ? mImageArray[level][0]->getHeight() : 0;
}
GLenum TextureD3D_2DArray::getInternalFormat(GLint level) const
{
return (level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS && mLayerCounts[level] > 0) ? mImageArray[level][0]->getInternalFormat() : GL_NONE;
}
bool TextureD3D_2DArray::isDepth(GLint level) const
{
return gl::GetInternalFormatInfo(getInternalFormat(level)).depthBits > 0;
}
gl::Error TextureD3D_2DArray::setEGLImageTarget(GLenum target, egl::Image *image)
{
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureD3D_2DArray::setImage(GLenum target,
size_t imageLevel,
GLenum internalFormat,
const gl::Extents &size,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_2D_ARRAY);
GLint level = static_cast<GLint>(imageLevel);
GLenum sizedInternalFormat = gl::GetSizedInternalFormat(internalFormat, type);
redefineImage(level, sizedInternalFormat, size);
const auto sizedInputFormat = gl::GetSizedInternalFormat(format, type);
const gl::InternalFormat &inputFormat = gl::GetInternalFormatInfo(sizedInputFormat);
GLsizei inputDepthPitch = 0;
ANGLE_TRY_RESULT(inputFormat.computeDepthPitch(size.width, size.height, unpack.alignment,
unpack.rowLength, unpack.imageHeight),
inputDepthPitch);
for (int i = 0; i < size.depth; i++)
{
const ptrdiff_t layerOffset = (inputDepthPitch * i);
gl::ImageIndex index = gl::ImageIndex::Make2DArray(level, i);
ANGLE_TRY(setImageImpl(index, type, unpack, pixels, layerOffset));
}
return gl::NoError();
}
gl::Error TextureD3D_2DArray::setSubImage(GLenum target,
size_t imageLevel,
const gl::Box &area,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_2D_ARRAY);
GLint level = static_cast<GLint>(imageLevel);
const auto sizedInputFormat = gl::GetSizedInternalFormat(format, type);
const gl::InternalFormat &inputFormat = gl::GetInternalFormatInfo(sizedInputFormat);
GLsizei inputDepthPitch = 0;
ANGLE_TRY_RESULT(inputFormat.computeDepthPitch(area.width, area.height, unpack.alignment,
unpack.rowLength, unpack.imageHeight),
inputDepthPitch);
for (int i = 0; i < area.depth; i++)
{
int layer = area.z + i;
const ptrdiff_t layerOffset = (inputDepthPitch * i);
gl::Box layerArea(area.x, area.y, 0, area.width, area.height, 1);
gl::ImageIndex index = gl::ImageIndex::Make2DArray(level, layer);
ANGLE_TRY(
TextureD3D::subImage(index, layerArea, format, type, unpack, pixels, layerOffset));
}
return gl::NoError();
}
gl::Error TextureD3D_2DArray::setCompressedImage(GLenum target,
size_t imageLevel,
GLenum internalFormat,
const gl::Extents &size,
const gl::PixelUnpackState &unpack,
size_t imageSize,
const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_2D_ARRAY);
GLint level = static_cast<GLint>(imageLevel);
// compressed formats don't have separate sized internal formats-- we can just use the compressed format directly
redefineImage(level, internalFormat, size);
const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
GLsizei inputDepthPitch = 0;
ANGLE_TRY_RESULT(
formatInfo.computeDepthPitch(size.width, size.height, 1, 0, 0),
inputDepthPitch);
for (int i = 0; i < size.depth; i++)
{
const ptrdiff_t layerOffset = (inputDepthPitch * i);
gl::ImageIndex index = gl::ImageIndex::Make2DArray(level, i);
ANGLE_TRY(setCompressedImageImpl(index, unpack, pixels, layerOffset));
}
return gl::NoError();
}
gl::Error TextureD3D_2DArray::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format,
const gl::PixelUnpackState &unpack, size_t imageSize, const uint8_t *pixels)
{
ASSERT(target == GL_TEXTURE_2D_ARRAY);
const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format);
GLsizei inputDepthPitch = 0;
ANGLE_TRY_RESULT(
formatInfo.computeDepthPitch(area.width, area.height, 1, 0, 0),
inputDepthPitch);
for (int i = 0; i < area.depth; i++)
{
int layer = area.z + i;
const ptrdiff_t layerOffset = (inputDepthPitch * i);
gl::Box layerArea(area.x, area.y, 0, area.width, area.height, 1);
gl::ImageIndex index = gl::ImageIndex::Make2DArray(static_cast<GLint>(level), layer);
ANGLE_TRY(
TextureD3D::subImageCompressed(index, layerArea, format, unpack, pixels, layerOffset));
ANGLE_TRY(commitRegion(index, layerArea));
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_2DArray::copyImage(GLenum target, size_t level, const gl::Rectangle &sourceArea, GLenum internalFormat,
const gl::Framebuffer *source)
{
UNIMPLEMENTED();
return gl::Error(GL_INVALID_OPERATION, "Copying 2D array textures is unimplemented.");
}
gl::Error TextureD3D_2DArray::copySubImage(GLenum target,
size_t imageLevel,
const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
const gl::Framebuffer *source)
{
ASSERT(target == GL_TEXTURE_2D_ARRAY);
GLint level = static_cast<GLint>(imageLevel);
gl::ImageIndex index = gl::ImageIndex::Make2DArray(level, destOffset.z);
if (canCreateRenderTargetForImage(index))
{
gl::Offset destLayerOffset(destOffset.x, destOffset.y, 0);
ANGLE_TRY(mImageArray[level][destOffset.z]->copyFromFramebuffer(destLayerOffset, sourceArea,
source));
mDirtyImages = true;
}
else
{
ANGLE_TRY(ensureRenderTarget());
if (isValidLevel(level))
{
ANGLE_TRY(updateStorageLevel(level));
ANGLE_TRY(mRenderer->copyImage2DArray(
source, sourceArea,
gl::GetInternalFormatInfo(getInternalFormat(getBaseLevel())).format, destOffset,
mTexStorage, level));
}
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_2DArray::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size)
{
ASSERT(target == GL_TEXTURE_2D_ARRAY);
deleteImages();
for (size_t level = 0; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++)
{
gl::Extents levelLayerSize(std::max(1, size.width >> level),
std::max(1, size.height >> level),
1);
mLayerCounts[level] = (level < levels ? size.depth : 0);
if (mLayerCounts[level] > 0)
{
// Create new images for this level
mImageArray[level] = new ImageD3D*[mLayerCounts[level]];
for (int layer = 0; layer < mLayerCounts[level]; layer++)
{
mImageArray[level][layer] = mRenderer->createImage();
mImageArray[level][layer]->redefine(GL_TEXTURE_2D_ARRAY, internalFormat, levelLayerSize, true);
}
}
}
// TODO(geofflang): Verify storage creation had no errors
bool renderTarget = IsRenderTargetUsage(mState.getUsage());
TextureStorage *storage =
mRenderer->createTextureStorage2DArray(internalFormat, renderTarget, size.width,
size.height, size.depth, static_cast<int>(levels));
gl::Error error = setCompleteTexStorage(storage);
if (error.isError())
{
SafeDelete(storage);
return error;
}
ANGLE_TRY(updateStorage());
mImmutable = true;
return gl::NoError();
}
void TextureD3D_2DArray::bindTexImage(egl::Surface *surface)
{
UNREACHABLE();
}
void TextureD3D_2DArray::releaseTexImage()
{
UNREACHABLE();
}
void TextureD3D_2DArray::initMipmapImages()
{
const GLuint baseLevel = mState.getEffectiveBaseLevel();
const GLuint maxLevel = mState.getMipmapMaxLevel();
int baseWidth = getLevelZeroWidth();
int baseHeight = getLevelZeroHeight();
int baseDepth = getLayerCount(getBaseLevel());
GLenum baseFormat = getBaseLevelInternalFormat();
// Purge array levels baseLevel + 1 through q and reset them to represent the generated mipmap
// levels.
for (GLuint level = baseLevel + 1u; level <= maxLevel; level++)
{
ASSERT((baseWidth >> level) > 0 || (baseHeight >> level) > 0);
gl::Extents levelLayerSize(std::max(baseWidth >> level, 1),
std::max(baseHeight >> level, 1),
baseDepth);
redefineImage(level, baseFormat, levelLayerSize);
}
}
gl::Error TextureD3D_2DArray::getRenderTarget(const gl::ImageIndex &index, RenderTargetD3D **outRT)
{
// ensure the underlying texture is created
ANGLE_TRY(ensureRenderTarget());
ANGLE_TRY(updateStorageLevel(index.mipIndex));
return mTexStorage->getRenderTarget(index, outRT);
}
gl::Error TextureD3D_2DArray::initializeStorage(bool renderTarget)
{
// Only initialize the first time this texture is used as a render target or shader resource
if (mTexStorage)
{
return gl::NoError();
}
// do not attempt to create storage for nonexistant data
if (!isLevelComplete(getBaseLevel()))
{
return gl::NoError();
}
bool createRenderTarget = (renderTarget || IsRenderTargetUsage(mState.getUsage()));
TextureStorage *storage = nullptr;
ANGLE_TRY(createCompleteStorage(createRenderTarget, &storage));
gl::Error error = setCompleteTexStorage(storage);
if (error.isError())
{
SafeDelete(storage);
return error;
}
ASSERT(mTexStorage);
// flush image data to the storage
ANGLE_TRY(updateStorage());
return gl::NoError();
}
gl::Error TextureD3D_2DArray::createCompleteStorage(bool renderTarget, TextureStorage **outStorage) const
{
GLsizei width = getLevelZeroWidth();
GLsizei height = getLevelZeroHeight();
GLsizei depth = getLayerCount(getBaseLevel());
GLenum internalFormat = getBaseLevelInternalFormat();
ASSERT(width > 0 && height > 0 && depth > 0);
// use existing storage level count, when previously specified by TexStorage*D
GLint levels = (mTexStorage ? mTexStorage->getLevelCount() : creationLevels(width, height, 1));
// TODO(geofflang): Verify storage creation succeeds
*outStorage = mRenderer->createTextureStorage2DArray(internalFormat, renderTarget, width, height, depth, levels);
return gl::NoError();
}
gl::Error TextureD3D_2DArray::setCompleteTexStorage(TextureStorage *newCompleteTexStorage)
{
SafeDelete(mTexStorage);
mTexStorage = newCompleteTexStorage;
mDirtyImages = true;
// We do not support managed 2D array storage, as managed storage is ES2/D3D9 only
ASSERT(!mTexStorage->isManaged());
return gl::NoError();
}
gl::Error TextureD3D_2DArray::updateStorage()
{
ASSERT(mTexStorage != NULL);
GLint storageLevels = mTexStorage->getLevelCount();
for (int level = 0; level < storageLevels; level++)
{
if (isLevelComplete(level))
{
ANGLE_TRY(updateStorageLevel(level));
}
}
return gl::NoError();
}
bool TextureD3D_2DArray::isValidLevel(int level) const
{
return (mTexStorage ? (level >= 0 && level < mTexStorage->getLevelCount()) : 0);
}
bool TextureD3D_2DArray::isLevelComplete(int level) const
{
ASSERT(level >= 0 && level < (int)ArraySize(mImageArray));
if (isImmutable())
{
return true;
}
GLsizei width = getLevelZeroWidth();
GLsizei height = getLevelZeroHeight();
if (width <= 0 || height <= 0)
{
return false;
}
// Layers check needs to happen after the above checks, otherwise out-of-range base level may be
// queried.
GLsizei layers = getLayerCount(getBaseLevel());
if (layers <= 0)
{
return false;
}
if (level == static_cast<int>(getBaseLevel()))
{
return true;
}
if (getInternalFormat(level) != getInternalFormat(getBaseLevel()))
{
return false;
}
if (getWidth(level) != std::max(1, width >> level))
{
return false;
}
if (getHeight(level) != std::max(1, height >> level))
{
return false;
}
if (getLayerCount(level) != layers)
{
return false;
}
return true;
}
bool TextureD3D_2DArray::isImageComplete(const gl::ImageIndex &index) const
{
return isLevelComplete(index.mipIndex);
}
gl::Error TextureD3D_2DArray::updateStorageLevel(int level)
{
ASSERT(level >= 0 && level < (int)ArraySize(mLayerCounts));
ASSERT(isLevelComplete(level));
for (int layer = 0; layer < mLayerCounts[level]; layer++)
{
ASSERT(mImageArray[level] != NULL && mImageArray[level][layer] != NULL);
if (mImageArray[level][layer]->isDirty())
{
gl::ImageIndex index = gl::ImageIndex::Make2DArray(level, layer);
gl::Box region(0, 0, 0, getWidth(level), getHeight(level), 1);
ANGLE_TRY(commitRegion(index, region));
}
}
return gl::Error(GL_NO_ERROR);
}
void TextureD3D_2DArray::deleteImages()
{
for (int level = 0; level < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++level)
{
for (int layer = 0; layer < mLayerCounts[level]; ++layer)
{
delete mImageArray[level][layer];
}
delete[] mImageArray[level];
mImageArray[level] = NULL;
mLayerCounts[level] = 0;
}
}
void TextureD3D_2DArray::redefineImage(GLint level, GLenum internalformat, const gl::Extents &size)
{
// If there currently is a corresponding storage texture image, it has these parameters
const int storageWidth = std::max(1, getLevelZeroWidth() >> level);
const int storageHeight = std::max(1, getLevelZeroHeight() >> level);
const GLuint baseLevel = getBaseLevel();
int storageDepth = 0;
if (baseLevel < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS)
{
storageDepth = getLayerCount(baseLevel);
}
// Only reallocate the layers if the size doesn't match
if (size.depth != mLayerCounts[level])
{
for (int layer = 0; layer < mLayerCounts[level]; layer++)
{
SafeDelete(mImageArray[level][layer]);
}
SafeDeleteArray(mImageArray[level]);
mLayerCounts[level] = size.depth;
if (size.depth > 0)
{
mImageArray[level] = new ImageD3D*[size.depth];
for (int layer = 0; layer < mLayerCounts[level]; layer++)
{
mImageArray[level][layer] = mRenderer->createImage();
}
}
}
if (size.depth > 0)
{
for (int layer = 0; layer < mLayerCounts[level]; layer++)
{
mImageArray[level][layer]->redefine(GL_TEXTURE_2D_ARRAY, internalformat,
gl::Extents(size.width, size.height, 1), false);
}
}
if (mTexStorage)
{
const GLenum storageFormat = getBaseLevelInternalFormat();
const int storageLevels = mTexStorage->getLevelCount();
if ((level >= storageLevels && storageLevels != 0) ||
size.width != storageWidth ||
size.height != storageHeight ||
size.depth != storageDepth ||
internalformat != storageFormat) // Discard mismatched storage
{
markAllImagesDirty();
SafeDelete(mTexStorage);
}
}
}
gl::ImageIndexIterator TextureD3D_2DArray::imageIterator() const
{
return gl::ImageIndexIterator::Make2DArray(0, mTexStorage->getLevelCount(), mLayerCounts);
}
gl::ImageIndex TextureD3D_2DArray::getImageIndex(GLint mip, GLint layer) const
{
return gl::ImageIndex::Make2DArray(mip, layer);
}
bool TextureD3D_2DArray::isValidIndex(const gl::ImageIndex &index) const
{
// Check for having a storage and the right type of index
if (!mTexStorage || index.type != GL_TEXTURE_2D_ARRAY)
{
return false;
}
// Check the mip index
if (index.mipIndex < 0 || index.mipIndex >= mTexStorage->getLevelCount())
{
return false;
}
// Check the layer index
return (!index.hasLayer() || (index.layerIndex >= 0 && index.layerIndex < mLayerCounts[index.mipIndex]));
}
void TextureD3D_2DArray::markAllImagesDirty()
{
for (int dirtyLevel = 0; dirtyLevel < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; dirtyLevel++)
{
for (int dirtyLayer = 0; dirtyLayer < mLayerCounts[dirtyLevel]; dirtyLayer++)
{
mImageArray[dirtyLevel][dirtyLayer]->markDirty();
}
}
mDirtyImages = true;
}
TextureD3D_External::TextureD3D_External(const gl::TextureState &state, RendererD3D *renderer)
: TextureD3D(state, renderer)
{
}
TextureD3D_External::~TextureD3D_External()
{
SafeDelete(mTexStorage);
}
ImageD3D *TextureD3D_External::getImage(const gl::ImageIndex &index) const
{
UNREACHABLE();
return nullptr;
}
GLsizei TextureD3D_External::getLayerCount(int level) const
{
return 1;
}
gl::Error TextureD3D_External::setImage(GLenum target,
size_t imageLevel,
GLenum internalFormat,
const gl::Extents &size,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
const uint8_t *pixels)
{
// Image setting is not supported for external images
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureD3D_External::setSubImage(GLenum target,
size_t imageLevel,
const gl::Box &area,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
const uint8_t *pixels)
{
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureD3D_External::setCompressedImage(GLenum target,
size_t imageLevel,
GLenum internalFormat,
const gl::Extents &size,
const gl::PixelUnpackState &unpack,
size_t imageSize,
const uint8_t *pixels)
{
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureD3D_External::setCompressedSubImage(GLenum target,
size_t level,
const gl::Box &area,
GLenum format,
const gl::PixelUnpackState &unpack,
size_t imageSize,
const uint8_t *pixels)
{
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureD3D_External::copyImage(GLenum target,
size_t imageLevel,
const gl::Rectangle &sourceArea,
GLenum internalFormat,
const gl::Framebuffer *source)
{
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureD3D_External::copySubImage(GLenum target,
size_t imageLevel,
const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
const gl::Framebuffer *source)
{
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureD3D_External::setStorage(GLenum target,
size_t levels,
GLenum internalFormat,
const gl::Extents &size)
{
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
gl::Error TextureD3D_External::setImageExternal(GLenum target,
egl::Stream *stream,
const egl::Stream::GLTextureDescription &desc)
{
ASSERT(target == GL_TEXTURE_EXTERNAL_OES);
SafeDelete(mTexStorage);
// If the stream is null, the external image is unbound and we release the storage
if (stream != nullptr)
{
mTexStorage = mRenderer->createTextureStorageExternal(stream, desc);
}
return gl::Error(GL_NO_ERROR);
}
void TextureD3D_External::bindTexImage(egl::Surface *surface)
{
UNREACHABLE();
}
void TextureD3D_External::releaseTexImage()
{
UNREACHABLE();
}
gl::Error TextureD3D_External::setEGLImageTarget(GLenum target, egl::Image *image)
{
EGLImageD3D *eglImaged3d = GetImplAs<EGLImageD3D>(image);
// Pass in the RenderTargetD3D here: createTextureStorage can't generate an error.
RenderTargetD3D *renderTargetD3D = nullptr;
ANGLE_TRY(eglImaged3d->getRenderTarget(&renderTargetD3D));
SafeDelete(mTexStorage);
mTexStorage = mRenderer->createTextureStorageEGLImage(eglImaged3d, renderTargetD3D);
return gl::NoError();
}
void TextureD3D_External::initMipmapImages()
{
UNREACHABLE();
}
gl::Error TextureD3D_External::getRenderTarget(const gl::ImageIndex &index, RenderTargetD3D **outRT)
{
UNREACHABLE();
return gl::Error(GL_INVALID_OPERATION);
}
bool TextureD3D_External::isImageComplete(const gl::ImageIndex &index) const
{
return (index.mipIndex == 0) ? (mTexStorage != nullptr) : false;
}
gl::Error TextureD3D_External::initializeStorage(bool renderTarget)
{
// Texture storage is created when an external image is bound
ASSERT(mTexStorage);
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_External::createCompleteStorage(bool renderTarget,
TextureStorage **outTexStorage) const
{
UNREACHABLE();
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_External::setCompleteTexStorage(TextureStorage *newCompleteTexStorage)
{
UNREACHABLE();
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureD3D_External::updateStorage()
{
// Texture storage does not need to be updated since it is already loaded with the latest
// external image
ASSERT(mTexStorage);
return gl::Error(GL_NO_ERROR);
}
gl::ImageIndexIterator TextureD3D_External::imageIterator() const
{
return gl::ImageIndexIterator::Make2D(0, mTexStorage->getLevelCount());
}
gl::ImageIndex TextureD3D_External::getImageIndex(GLint mip, GLint /*layer*/) const
{
// "layer" does not apply to 2D Textures.
return gl::ImageIndex::Make2D(mip);
}
bool TextureD3D_External::isValidIndex(const gl::ImageIndex &index) const
{
return (mTexStorage && index.type == GL_TEXTURE_EXTERNAL_OES && index.mipIndex == 0);
}
void TextureD3D_External::markAllImagesDirty()
{
UNREACHABLE();
}
} // namespace rx
|