Hash :
a1be7e61
Author :
Date :
2024-11-26T14:39:18
Implement EGL_EXT_surface_compression This patch adds implementation of EGL_EXT_surface_compression to ANGLE, including new API eglQuerySupportedCompressionRatesEXT and adding EGL_SURFACE_COMPRESSION_EXT in EGLQuerySurface and EGLCreateWindowSurface/EGLCreatePlatformWindowSurface. Angle end2end test is added to verify the extension. Bug: angleproject:375496226 Change-Id: I06926930d94485a378fc831d552cf55fe7938a57 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6073355 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Charlie Lao <cclao@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716
//
// Copyright 2016 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.
//
// TextureVk.cpp:
// Implements the class methods for TextureVk.
//
#include "libANGLE/renderer/vulkan/TextureVk.h"
#include <vulkan/vulkan.h>
#include "common/debug.h"
#include "image_util/generatemip.inc"
#include "libANGLE/Config.h"
#include "libANGLE/Context.h"
#include "libANGLE/Image.h"
#include "libANGLE/MemoryObject.h"
#include "libANGLE/Surface.h"
#include "libANGLE/renderer/renderer_utils.h"
#include "libANGLE/renderer/vulkan/ContextVk.h"
#include "libANGLE/renderer/vulkan/FramebufferVk.h"
#include "libANGLE/renderer/vulkan/ImageVk.h"
#include "libANGLE/renderer/vulkan/MemoryObjectVk.h"
#include "libANGLE/renderer/vulkan/RenderbufferVk.h"
#include "libANGLE/renderer/vulkan/SurfaceVk.h"
#include "libANGLE/renderer/vulkan/UtilsVk.h"
#include "libANGLE/renderer/vulkan/vk_format_utils.h"
#include "libANGLE/renderer/vulkan/vk_helpers.h"
#include "libANGLE/renderer/vulkan/vk_renderer.h"
#include "libANGLE/renderer/vulkan/vk_utils.h"
namespace rx
{
namespace
{
constexpr VkImageUsageFlags kTransferImageFlags =
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
constexpr VkImageUsageFlags kColorAttachmentImageFlags =
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
constexpr VkImageUsageFlags kDrawStagingImageFlags =
kTransferImageFlags | kColorAttachmentImageFlags;
constexpr VkFormatFeatureFlags kBlitFeatureFlags =
VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
constexpr VkImageAspectFlags kDepthStencilAspects =
VK_IMAGE_ASPECT_STENCIL_BIT | VK_IMAGE_ASPECT_DEPTH_BIT;
constexpr angle::SubjectIndex kTextureImageSubjectIndex = 0;
// Test whether a texture level is within the range of levels for which the current image is
// allocated. This is used to ensure out-of-range updates are staged in the image, and not
// attempted to be directly applied.
bool IsTextureLevelInAllocatedImage(const vk::ImageHelper &image,
gl::LevelIndex textureLevelIndexGL)
{
gl::LevelIndex imageFirstAllocateLevel = image.getFirstAllocatedLevel();
if (textureLevelIndexGL < imageFirstAllocateLevel)
{
return false;
}
vk::LevelIndex imageLevelIndexVk = image.toVkLevel(textureLevelIndexGL);
return imageLevelIndexVk < vk::LevelIndex(image.getLevelCount());
}
// Test whether a redefined texture level is compatible with the currently allocated image. Returns
// true if the given size and format match the corresponding mip in the allocated image (taking
// base level into account). This could return false when:
//
// - Defining a texture level that is outside the range of the image levels. In this case, changes
// to this level should remain staged until the texture is redefined to include this level.
// - Redefining a texture level that is within the range of the image levels, but has a different
// size or format. In this case too, changes to this level should remain staged as the texture
// is no longer complete as is.
bool IsTextureLevelDefinitionCompatibleWithImage(const vk::ImageHelper &image,
gl::LevelIndex textureLevelIndexGL,
const gl::Extents &size,
angle::FormatID intendedFormatID,
angle::FormatID actualFormatID)
{
if (!IsTextureLevelInAllocatedImage(image, textureLevelIndexGL))
{
return false;
}
vk::LevelIndex imageLevelIndexVk = image.toVkLevel(textureLevelIndexGL);
return size == image.getLevelExtents(imageLevelIndexVk) &&
intendedFormatID == image.getIntendedFormatID() &&
actualFormatID == image.getActualFormatID();
}
bool CanCopyWithTransferForTexImage(vk::Renderer *renderer,
angle::FormatID srcIntendedFormatID,
angle::FormatID srcActualFormatID,
VkImageTiling srcTilingMode,
angle::FormatID dstIntendedFormatID,
angle::FormatID dstActualFormatID,
VkImageTiling dstTilingMode,
bool isViewportFlipY)
{
// For glTex[Sub]Image, only accept same-format transfers.
// There are cases that two images' actual format is the same, but intended formats are
// different due to one is using the fallback format (for example, RGB fallback to RGBA). In
// these situations CanCopyWithTransfer will say yes. But if we use transfer to do copy, the
// alpha channel will be also be copied with source data which is wrong.
bool isFormatCompatible =
srcIntendedFormatID == dstIntendedFormatID && srcActualFormatID == dstActualFormatID;
return !isViewportFlipY && isFormatCompatible &&
vk::CanCopyWithTransfer(renderer, srcActualFormatID, srcTilingMode, dstActualFormatID,
dstTilingMode);
}
bool CanCopyWithTransferForCopyTexture(vk::Renderer *renderer,
const vk::ImageHelper &srcImage,
VkImageTiling srcTilingMode,
angle::FormatID destIntendedFormatID,
angle::FormatID destActualFormatID,
VkImageTiling destTilingMode,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha)
{
if (unpackFlipY || unpackPremultiplyAlpha || unpackUnmultiplyAlpha)
{
return false;
}
if (!vk::CanCopyWithTransfer(renderer, srcImage.getActualFormatID(), srcTilingMode,
destActualFormatID, destTilingMode))
{
return false;
}
// If the formats are identical, we can always transfer between them.
if (srcImage.getIntendedFormatID() == destIntendedFormatID &&
srcImage.getActualFormatID() == destActualFormatID)
{
return true;
}
// If either format is emulated, cannot transfer.
if (srcImage.hasEmulatedImageFormat() ||
vk::HasEmulatedImageFormat(destIntendedFormatID, destActualFormatID))
{
return false;
}
// Otherwise, allow transfer between compatible formats. This is derived from the specification
// of CHROMIUM_copy_texture.
const angle::Format &srcAngleFormat = srcImage.getActualFormat();
const angle::Format &destAngleFormat = angle::Format::Get(destActualFormatID);
const bool srcIsBGRA = srcAngleFormat.isBGRA();
const bool srcHasR8 = srcAngleFormat.redBits == 8;
const bool srcHasG8 = srcAngleFormat.greenBits == 8;
const bool srcHasB8 = srcAngleFormat.blueBits == 8;
const bool srcHasA8 = srcAngleFormat.alphaBits == 8;
const bool srcIsSigned = srcAngleFormat.isSnorm() || srcAngleFormat.isSint();
const bool destIsBGRA = destAngleFormat.isBGRA();
const bool destHasR8 = destAngleFormat.redBits == 8;
const bool destHasG8 = destAngleFormat.greenBits == 8;
const bool destHasB8 = destAngleFormat.blueBits == 8;
const bool destHasA8 = destAngleFormat.alphaBits == 8;
const bool destIsSigned = destAngleFormat.isSnorm() || destAngleFormat.isSint();
// Copy is allowed as long as they have the same number, ordering and sign of (8-bit) channels.
// CHROMIUM_copy_texture expects verbatim copy between these format, so this copy is done
// regardless of sRGB, normalized, etc.
return srcIsBGRA == destIsBGRA && srcHasR8 == destHasR8 && srcHasG8 == destHasG8 &&
srcHasB8 == destHasB8 && srcHasA8 == destHasA8 && srcIsSigned == destIsSigned;
}
bool CanCopyWithDraw(vk::Renderer *renderer,
const angle::FormatID srcFormatID,
VkImageTiling srcTilingMode,
const angle::FormatID dstFormatID,
VkImageTiling destTilingMode)
{
// Checks that the formats in copy by drawing have the appropriate feature bits
bool srcFormatHasNecessaryFeature = vk::FormatHasNecessaryFeature(
renderer, srcFormatID, srcTilingMode, VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT);
bool dstFormatHasNecessaryFeature = vk::FormatHasNecessaryFeature(
renderer, dstFormatID, destTilingMode, VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT);
return srcFormatHasNecessaryFeature && dstFormatHasNecessaryFeature;
}
bool CanGenerateMipmapWithCompute(vk::Renderer *renderer,
VkImageType imageType,
angle::FormatID formatID,
GLint samples,
bool canBeRespecified)
{
// Feature needs to be enabled
if (!renderer->getFeatures().allowGenerateMipmapWithCompute.enabled)
{
return false;
}
// We need to be able to respecify the backing image
if (!canBeRespecified)
{
return false;
}
const angle::Format &angleFormat = angle::Format::Get(formatID);
// Format must have STORAGE support.
const bool hasStorageSupport =
renderer->hasImageFormatFeatureBits(formatID, VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT);
// No support for sRGB formats yet.
const bool isSRGB = angleFormat.isSRGB;
// No support for integer formats yet.
const bool isInt = angleFormat.isInt();
// Only 2D images are supported.
const bool is2D = imageType == VK_IMAGE_TYPE_2D;
// No support for multisampled images yet.
const bool isMultisampled = samples > 1;
// Only color formats are supported.
const bool isColorFormat = !angleFormat.hasDepthOrStencilBits();
return hasStorageSupport && !isSRGB && !isInt && is2D && !isMultisampled && isColorFormat;
}
void GetRenderTargetLayerCountAndIndex(vk::ImageHelper *image,
const gl::ImageIndex &index,
GLuint *layerIndex,
GLuint *layerCount,
GLuint *imageLayerCount)
{
*layerIndex = index.hasLayer() ? index.getLayerIndex() : 0;
*layerCount = index.getLayerCount();
switch (index.getType())
{
case gl::TextureType::_2D:
case gl::TextureType::_2DMultisample:
case gl::TextureType::External:
ASSERT(*layerIndex == 0 &&
(*layerCount == 1 ||
*layerCount == static_cast<GLuint>(gl::ImageIndex::kEntireLevel)));
*imageLayerCount = 1;
break;
case gl::TextureType::CubeMap:
ASSERT(!index.hasLayer() ||
*layerIndex == static_cast<GLuint>(index.cubeMapFaceIndex()));
*imageLayerCount = gl::kCubeFaceCount;
break;
case gl::TextureType::_3D:
{
gl::LevelIndex levelGL(index.getLevelIndex());
*imageLayerCount = image->getLevelExtents(image->toVkLevel(levelGL)).depth;
break;
}
case gl::TextureType::_2DArray:
case gl::TextureType::_2DMultisampleArray:
case gl::TextureType::CubeMapArray:
*imageLayerCount = image->getLayerCount();
break;
default:
UNREACHABLE();
}
if (*layerCount == static_cast<GLuint>(gl::ImageIndex::kEntireLevel))
{
ASSERT(*layerIndex == 0);
*layerCount = *imageLayerCount;
}
}
void Set3DBaseArrayLayerAndLayerCount(VkImageSubresourceLayers *Subresource)
{
// If the srcImage/dstImage parameters are of VkImageType VK_IMAGE_TYPE_3D, the baseArrayLayer
// and layerCount members of the corresponding subresource must be 0 and 1, respectively.
Subresource->baseArrayLayer = 0;
Subresource->layerCount = 1;
}
const vk::Format *AdjustStorageViewFormatPerWorkarounds(vk::Renderer *renderer,
const vk::Format *intended,
vk::ImageAccess access)
{
// r32f images are emulated with r32ui.
if (renderer->getFeatures().emulateR32fImageAtomicExchange.enabled &&
intended->getActualImageFormatID(access) == angle::FormatID::R32_FLOAT)
{
return &renderer->getFormat(angle::FormatID::R32_UINT);
}
return intended;
}
const vk::Format *AdjustViewFormatForSampler(vk::Renderer *renderer,
const vk::Format *intended,
gl::SamplerFormat samplerFormat)
{
switch (samplerFormat)
{
case gl::SamplerFormat::Float:
switch (intended->getIntendedFormatID())
{
case angle::FormatID::R8_UNORM:
case angle::FormatID::R16_FLOAT:
case angle::FormatID::R32_FLOAT:
case angle::FormatID::R8G8_UNORM:
case angle::FormatID::R16G16_FLOAT:
case angle::FormatID::R32G32_FLOAT:
case angle::FormatID::R32G32B32_FLOAT:
case angle::FormatID::R8G8B8A8_UNORM:
case angle::FormatID::R16G16B16A16_FLOAT:
case angle::FormatID::R32G32B32A32_FLOAT:
return intended;
case angle::FormatID::R8_SINT:
case angle::FormatID::R8_UINT:
return &renderer->getFormat(angle::FormatID::R8_UNORM);
case angle::FormatID::R16_SINT:
case angle::FormatID::R16_UINT:
return &renderer->getFormat(angle::FormatID::R16_FLOAT);
case angle::FormatID::R32_SINT:
case angle::FormatID::R32_UINT:
return &renderer->getFormat(angle::FormatID::R32_FLOAT);
case angle::FormatID::R8G8_SINT:
case angle::FormatID::R8G8_UINT:
return &renderer->getFormat(angle::FormatID::R8G8_UNORM);
case angle::FormatID::R16G16_SINT:
case angle::FormatID::R16G16_UINT:
return &renderer->getFormat(angle::FormatID::R16G16_FLOAT);
case angle::FormatID::R32G32_SINT:
case angle::FormatID::R32G32_UINT:
return &renderer->getFormat(angle::FormatID::R32G32_FLOAT);
case angle::FormatID::R32G32B32_SINT:
case angle::FormatID::R32G32B32_UINT:
return &renderer->getFormat(angle::FormatID::R32G32B32_FLOAT);
case angle::FormatID::R8G8B8A8_SINT:
case angle::FormatID::R8G8B8A8_UINT:
return &renderer->getFormat(angle::FormatID::R8G8B8A8_UNORM);
case angle::FormatID::R16G16B16A16_SINT:
case angle::FormatID::R16G16B16A16_UINT:
return &renderer->getFormat(angle::FormatID::R16G16B16A16_FLOAT);
case angle::FormatID::R32G32B32A32_SINT:
case angle::FormatID::R32G32B32A32_UINT:
return &renderer->getFormat(angle::FormatID::R32G32B32A32_FLOAT);
default:
UNREACHABLE();
return intended;
}
case gl::SamplerFormat::Unsigned:
switch (intended->getIntendedFormatID())
{
case angle::FormatID::R8_UINT:
case angle::FormatID::R16_UINT:
case angle::FormatID::R32_UINT:
case angle::FormatID::R8G8_UINT:
case angle::FormatID::R16G16_UINT:
case angle::FormatID::R32G32_UINT:
case angle::FormatID::R32G32B32_UINT:
case angle::FormatID::R8G8B8A8_UINT:
case angle::FormatID::R16G16B16A16_UINT:
case angle::FormatID::R32G32B32A32_UINT:
return intended;
case angle::FormatID::R8_UNORM:
case angle::FormatID::R8_SINT:
return &renderer->getFormat(angle::FormatID::R8_UINT);
case angle::FormatID::R16_FLOAT:
case angle::FormatID::R16_SINT:
return &renderer->getFormat(angle::FormatID::R16_UINT);
case angle::FormatID::R32_FLOAT:
case angle::FormatID::R32_SINT:
return &renderer->getFormat(angle::FormatID::R32_UINT);
case angle::FormatID::R8G8_UNORM:
case angle::FormatID::R8G8_SINT:
return &renderer->getFormat(angle::FormatID::R8G8_UINT);
case angle::FormatID::R16G16_FLOAT:
case angle::FormatID::R16G16_SINT:
return &renderer->getFormat(angle::FormatID::R16G16_UINT);
case angle::FormatID::R32G32_FLOAT:
case angle::FormatID::R32G32_SINT:
return &renderer->getFormat(angle::FormatID::R32G32_UINT);
case angle::FormatID::R32G32B32_FLOAT:
case angle::FormatID::R32G32B32_SINT:
return &renderer->getFormat(angle::FormatID::R32G32B32_UINT);
case angle::FormatID::R8G8B8A8_UNORM:
case angle::FormatID::R8G8B8A8_SINT:
return &renderer->getFormat(angle::FormatID::R8G8B8A8_UINT);
case angle::FormatID::R16G16B16A16_FLOAT:
case angle::FormatID::R16G16B16A16_SINT:
return &renderer->getFormat(angle::FormatID::R16G16B16A16_UINT);
case angle::FormatID::R32G32B32A32_FLOAT:
case angle::FormatID::R32G32B32A32_SINT:
return &renderer->getFormat(angle::FormatID::R32G32B32A32_UINT);
default:
UNREACHABLE();
return intended;
}
case gl::SamplerFormat::Signed:
switch (intended->getIntendedFormatID())
{
case angle::FormatID::R8_SINT:
case angle::FormatID::R16_SINT:
case angle::FormatID::R32_SINT:
case angle::FormatID::R8G8_SINT:
case angle::FormatID::R16G16_SINT:
case angle::FormatID::R32G32_SINT:
case angle::FormatID::R32G32B32_SINT:
case angle::FormatID::R8G8B8A8_SINT:
case angle::FormatID::R16G16B16A16_SINT:
case angle::FormatID::R32G32B32A32_SINT:
return intended;
case angle::FormatID::R8_UNORM:
case angle::FormatID::R8_UINT:
return &renderer->getFormat(angle::FormatID::R8_SINT);
case angle::FormatID::R16_FLOAT:
case angle::FormatID::R16_UINT:
return &renderer->getFormat(angle::FormatID::R16_SINT);
case angle::FormatID::R32_FLOAT:
case angle::FormatID::R32_UINT:
return &renderer->getFormat(angle::FormatID::R32_SINT);
case angle::FormatID::R8G8_UNORM:
case angle::FormatID::R8G8_UINT:
return &renderer->getFormat(angle::FormatID::R8G8_SINT);
case angle::FormatID::R16G16_FLOAT:
case angle::FormatID::R16G16_UINT:
return &renderer->getFormat(angle::FormatID::R16G16_SINT);
case angle::FormatID::R32G32_FLOAT:
case angle::FormatID::R32G32_UINT:
return &renderer->getFormat(angle::FormatID::R32G32_SINT);
case angle::FormatID::R32G32B32_FLOAT:
case angle::FormatID::R32G32B32_UINT:
return &renderer->getFormat(angle::FormatID::R32G32B32_SINT);
case angle::FormatID::R8G8B8A8_UNORM:
case angle::FormatID::R8G8B8A8_UINT:
return &renderer->getFormat(angle::FormatID::R8G8B8A8_SINT);
case angle::FormatID::R16G16B16A16_FLOAT:
case angle::FormatID::R16G16B16A16_UINT:
return &renderer->getFormat(angle::FormatID::R16G16B16A16_SINT);
case angle::FormatID::R32G32B32A32_FLOAT:
case angle::FormatID::R32G32B32A32_UINT:
return &renderer->getFormat(angle::FormatID::R32G32B32A32_SINT);
default:
UNREACHABLE();
return intended;
}
default:
UNREACHABLE();
return intended;
}
}
angle::FormatID GetRGBAEmulationDstFormat(angle::FormatID srcFormatID)
{
switch (srcFormatID)
{
case angle::FormatID::R32G32B32_UINT:
return angle::FormatID::R32G32B32A32_UINT;
case angle::FormatID::R32G32B32_SINT:
return angle::FormatID::R32G32B32A32_SINT;
case angle::FormatID::R32G32B32_FLOAT:
return angle::FormatID::R32G32B32A32_FLOAT;
default:
return angle::FormatID::NONE;
}
}
bool NeedsRGBAEmulation(vk::Renderer *renderer, angle::FormatID formatID)
{
if (renderer->hasBufferFormatFeatureBits(formatID, VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT))
{
return false;
}
// Vulkan driver support is required for all formats except the ones we emulate.
ASSERT(GetRGBAEmulationDstFormat(formatID) != angle::FormatID::NONE);
return true;
}
} // anonymous namespace
// TextureVk implementation.
TextureVk::TextureVk(const gl::TextureState &state, vk::Renderer *renderer)
: TextureImpl(state),
mOwnsImage(false),
mRequiresMutableStorage(false),
mRequiredImageAccess(vk::ImageAccess::SampleOnly),
mImmutableSamplerDirty(false),
mEGLImageNativeType(gl::TextureType::InvalidEnum),
mEGLImageLayerOffset(0),
mEGLImageLevelOffset(0),
mImage(nullptr),
mImageUsageFlags(0),
mImageCreateFlags(0),
mImageObserverBinding(this, kTextureImageSubjectIndex),
mCurrentBaseLevel(state.getBaseLevel()),
mCurrentMaxLevel(state.getMaxLevel()),
mCachedImageViewSubresourceSerialSRGBDecode{},
mCachedImageViewSubresourceSerialSkipDecode{}
{}
TextureVk::~TextureVk() = default;
void TextureVk::onDestroy(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
releaseAndDeleteImageAndViews(contextVk);
resetSampler();
}
angle::Result TextureVk::setImage(const gl::Context *context,
const gl::ImageIndex &index,
GLenum internalFormat,
const gl::Extents &size,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
gl::Buffer *unpackBuffer,
const uint8_t *pixels)
{
const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat, type);
return setImageImpl(context, index, formatInfo, size, type, unpack, unpackBuffer, pixels);
}
angle::Result TextureVk::setSubImage(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Box &area,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
gl::Buffer *unpackBuffer,
const uint8_t *pixels)
{
const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, type);
ContextVk *contextVk = vk::GetImpl(context);
const gl::ImageDesc &levelDesc = mState.getImageDesc(index);
const vk::Format &vkFormat =
contextVk->getRenderer()->getFormat(levelDesc.format.info->sizedInternalFormat);
return setSubImageImpl(context, index, area, formatInfo, type, unpack, unpackBuffer, pixels,
vkFormat);
}
bool TextureVk::isCompressedFormatEmulated(const gl::Context *context,
gl::TextureTarget target,
GLint level)
{
const gl::ImageDesc &levelDesc = mState.getImageDesc(target, level);
if (!levelDesc.format.info->compressed)
{
// If it isn't compressed, the remaining logic won't work
return false;
}
// Check against the list of formats used to emulate compressed textures
return gl::IsEmulatedCompressedFormat(levelDesc.format.info->sizedInternalFormat);
}
angle::Result TextureVk::setCompressedImage(const gl::Context *context,
const gl::ImageIndex &index,
GLenum internalFormat,
const gl::Extents &size,
const gl::PixelUnpackState &unpack,
size_t imageSize,
const uint8_t *pixels)
{
const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
const gl::State &glState = context->getState();
gl::Buffer *unpackBuffer = glState.getTargetBuffer(gl::BufferBinding::PixelUnpack);
if (unpackBuffer &&
this->isCompressedFormatEmulated(context, index.getTarget(), index.getLevelIndex()))
{
// TODO (anglebug.com/42265933): Can't populate from a buffer using emulated format
UNIMPLEMENTED();
return angle::Result::Stop;
}
return setImageImpl(context, index, formatInfo, size, GL_UNSIGNED_BYTE, unpack, unpackBuffer,
pixels);
}
angle::Result TextureVk::setCompressedSubImage(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Box &area,
GLenum format,
const gl::PixelUnpackState &unpack,
size_t imageSize,
const uint8_t *pixels)
{
const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, GL_UNSIGNED_BYTE);
ContextVk *contextVk = vk::GetImpl(context);
const gl::ImageDesc &levelDesc = mState.getImageDesc(index);
const vk::Format &vkFormat =
contextVk->getRenderer()->getFormat(levelDesc.format.info->sizedInternalFormat);
const gl::State &glState = contextVk->getState();
gl::Buffer *unpackBuffer = glState.getTargetBuffer(gl::BufferBinding::PixelUnpack);
if (unpackBuffer &&
this->isCompressedFormatEmulated(context, index.getTarget(), index.getLevelIndex()))
{
// TODO (anglebug.com/42265933): Can't populate from a buffer using emulated format
UNIMPLEMENTED();
return angle::Result::Stop;
}
return setSubImageImpl(context, index, area, formatInfo, GL_UNSIGNED_BYTE, unpack, unpackBuffer,
pixels, vkFormat);
}
angle::Result TextureVk::setImageImpl(const gl::Context *context,
const gl::ImageIndex &index,
const gl::InternalFormat &formatInfo,
const gl::Extents &size,
GLenum type,
const gl::PixelUnpackState &unpack,
gl::Buffer *unpackBuffer,
const uint8_t *pixels)
{
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
const vk::Format &vkFormat = renderer->getFormat(formatInfo.sizedInternalFormat);
ANGLE_TRY(redefineLevel(context, index, vkFormat, size));
// Early-out on empty textures, don't create a zero-sized storage.
if (size.empty())
{
return angle::Result::Continue;
}
return setSubImageImpl(context, index, gl::Box(gl::kOffsetZero, size), formatInfo, type, unpack,
unpackBuffer, pixels, vkFormat);
}
bool TextureVk::isFastUnpackPossible(const vk::Format &vkFormat,
size_t offset,
const vk::Format &bufferVkFormat) const
{
// Conditions to determine if fast unpacking is possible
// 1. Image must be well defined to unpack directly to it
// TODO(http://anglebug.com/42262852) Create and stage a temp image instead
// 2. Can't perform a fast copy for depth/stencil, except from non-emulated depth or stencil
// to emulated depth/stencil. GL requires depth and stencil data to be packed, while Vulkan
// requires them to be separate.
// 3. Can't perform a fast copy for emulated formats, except from non-emulated depth or stencil
// to emulated depth/stencil.
// 4. vkCmdCopyBufferToImage requires byte offset to be a multiple of 4.
// 5. Actual texture format and intended buffer format must match for color formats
const angle::Format &bufferFormat = vkFormat.getActualBufferFormat(false);
const bool isCombinedDepthStencil = bufferFormat.hasDepthAndStencilBits();
const bool isDepthXorStencil = bufferFormat.hasDepthOrStencilBits() && !isCombinedDepthStencil;
const bool isCompatibleDepth = vkFormat.getIntendedFormat().depthBits == bufferFormat.depthBits;
const VkDeviceSize imageCopyAlignment =
vk::GetImageCopyBufferAlignment(mImage->getActualFormatID());
const bool formatsMatch = bufferFormat.hasDepthOrStencilBits() ||
(vkFormat.getActualImageFormatID(getRequiredImageAccess()) ==
bufferVkFormat.getIntendedFormatID());
return mImage->valid() && !isCombinedDepthStencil &&
(vkFormat.getIntendedFormatID() ==
vkFormat.getActualImageFormatID(getRequiredImageAccess()) ||
(isDepthXorStencil && isCompatibleDepth)) &&
(offset % imageCopyAlignment) == 0 && formatsMatch;
}
bool TextureVk::isMipImageDescDefined(gl::TextureTarget textureTarget, size_t level)
{
// A defined image should have defined width, height, and format.
gl::ImageDesc imageDesc = mState.getImageDesc(textureTarget, level);
return imageDesc.size.height != 0 && imageDesc.size.width != 0 &&
imageDesc.format.info->format != GL_NONE;
}
bool TextureVk::isMutableTextureConsistentlySpecifiedForFlush()
{
// Disable optimization if the base level is not 0.
if (mState.getBaseLevel() != 0)
{
return false;
}
// If the texture is a cubemap, we will have to wait until it is complete.
if (mState.getType() == gl::TextureType::CubeMap && !mState.isCubeComplete())
{
return false;
}
// Before we initialize the mips, we make sure that the base mip level is properly defined.
gl::TextureTarget textureTarget = (mState.getType() == gl::TextureType::CubeMap)
? gl::kCubeMapTextureTargetMin
: gl::TextureTypeToTarget(mState.getType(), 0);
if (!isMipImageDescDefined(textureTarget, 0))
{
return false;
}
// We do not flush if the texture has been bound as an attachment.
if (mState.hasBeenBoundAsAttachment())
{
return false;
}
// For performance, flushing is skipped if the number of staged updates in a mip level is not
// one. For a cubemap, this applies to each face of the cube instead.
size_t maxUpdatesPerMipLevel = (mState.getType() == gl::TextureType::CubeMap) ? 6 : 1;
if (mImage->getLevelUpdateCount(gl::LevelIndex(0)) != maxUpdatesPerMipLevel)
{
return false;
}
// The mip levels that are already defined should have attributes compatible with those of the
// base mip level. For each defined mip level, its size, format, number of samples, and depth
// are checked before flushing the texture updates. For complete cubemaps, there are 6 images
// per mip level. Therefore, mState would have 6 times as many images.
gl::ImageDesc baseImageDesc = mState.getImageDesc(textureTarget, 0);
size_t maxImageMipLevels = (mState.getType() == gl::TextureType::CubeMap)
? (mState.getImageDescs().size() / 6)
: mState.getImageDescs().size();
for (size_t image = 1; image < maxImageMipLevels; image++)
{
gl::ImageDesc mipImageDesc = mState.getImageDesc(textureTarget, image);
if (!isMipImageDescDefined(textureTarget, image))
{
continue;
}
// If the texture is 2DArray or 3D, the depths should also be checked according to the mip
// levels. If the texture type is a cube map array, the depth represents the number of
// layer-faces and does not change for mipmaps. Otherwise, we skip the depth comparison.
gl::Extents baseImageDescMipSize;
baseImageDescMipSize.width = std::max(baseImageDesc.size.width >> image, 1);
baseImageDescMipSize.height = std::max(baseImageDesc.size.height >> image, 1);
baseImageDescMipSize.depth = std::max(baseImageDesc.size.depth >> image, 1);
bool isDepthCompatible = (mState.getType() == gl::TextureType::_3D ||
mState.getType() == gl::TextureType::_2DArray)
? (baseImageDescMipSize.depth == mipImageDesc.size.depth)
: (mState.getType() != gl::TextureType::CubeMapArray ||
baseImageDesc.size.depth == mipImageDesc.size.depth);
bool isSizeCompatible = (baseImageDescMipSize.width == mipImageDesc.size.width) &&
(baseImageDescMipSize.height == mipImageDesc.size.height) &&
isDepthCompatible;
bool isFormatCompatible = (baseImageDesc.format.info->sizedInternalFormat ==
mipImageDesc.format.info->sizedInternalFormat);
bool isNumberOfSamplesCompatible = (baseImageDesc.samples == mipImageDesc.samples);
bool isUpdateCompatible =
(mImage->getLevelUpdateCount(gl::LevelIndex(static_cast<GLint>(image))) ==
maxUpdatesPerMipLevel);
if (!isSizeCompatible || !isFormatCompatible || !isNumberOfSamplesCompatible ||
!isUpdateCompatible)
{
return false;
}
}
return true;
}
bool TextureVk::updateMustBeFlushed(gl::LevelIndex textureLevelIndexGL,
angle::FormatID dstImageFormatID) const
{
ASSERT(mImage);
// For EGLImages we should never stage the update since staged update is subject to thread
// racing bugs when two textures in different share groups are accessed at same time.
if (!mOwnsImage)
{
// EGLImage is always initialized upon creation and format should always renderable so that
// there is no format upgrade.
ASSERT(mImage->valid());
ASSERT(IsTextureLevelInAllocatedImage(*mImage, textureLevelIndexGL));
ASSERT(!IsTextureLevelRedefined(mRedefinedLevels, mState.getType(), textureLevelIndexGL));
return true;
}
return false;
}
bool TextureVk::updateMustBeStaged(gl::LevelIndex textureLevelIndexGL,
angle::FormatID dstImageFormatID) const
{
ASSERT(mImage);
// If we do not have storage yet, there is impossible to immediately do the copy, so just
// stage it. Note that immutable texture will have a valid storage.
if (!mImage->valid())
{
return true;
}
// If update is outside the range of image levels, it must be staged.
if (!IsTextureLevelInAllocatedImage(*mImage, textureLevelIndexGL))
{
return true;
}
// During the process of format change, mImage's format may become stale. In that case, we
// must always stage the update and let caller properly release mImage and initExternal and
// flush the update.
if (mImage->getActualFormatID() != dstImageFormatID)
{
return true;
}
// Otherwise, it can only be directly applied to the image if the level is not previously
// incompatibly redefined.
return IsTextureLevelRedefined(mRedefinedLevels, mState.getType(), textureLevelIndexGL);
}
angle::Result TextureVk::clearImage(const gl::Context *context,
GLint level,
GLenum format,
GLenum type,
const uint8_t *data)
{
// All defined cubemap faces are expected to have equal width and height.
bool isCubeMap = mState.getType() == gl::TextureType::CubeMap;
gl::TextureTarget textureTarget =
isCubeMap ? gl::kCubeMapTextureTargetMin : gl::TextureTypeToTarget(mState.getType(), 0);
gl::Extents extents = mState.getImageDesc(textureTarget, level).size;
gl::Box area = gl::Box(gl::kOffsetZero, extents);
if (isCubeMap)
{
// For a cubemap, the depth offset moves between cube faces.
ASSERT(area.depth == 1);
area.depth = 6;
}
return clearSubImageImpl(context, level, area, vk::ClearTextureMode::FullClear, format, type,
data);
}
angle::Result TextureVk::clearSubImage(const gl::Context *context,
GLint level,
const gl::Box &area,
GLenum format,
GLenum type,
const uint8_t *data)
{
bool isCubeMap = mState.getType() == gl::TextureType::CubeMap;
gl::TextureTarget textureTarget =
isCubeMap ? gl::kCubeMapTextureTargetMin : gl::TextureTypeToTarget(mState.getType(), 0);
gl::Extents extents = mState.getImageDesc(textureTarget, level).size;
int depthForFullClear = isCubeMap ? 6 : extents.depth;
vk::ClearTextureMode clearMode = vk::ClearTextureMode::PartialClear;
if (extents.width == area.width && extents.height == area.height &&
depthForFullClear == area.depth)
{
clearMode = vk::ClearTextureMode::FullClear;
}
return clearSubImageImpl(context, level, area, clearMode, format, type, data);
}
angle::Result TextureVk::clearSubImageImpl(const gl::Context *context,
GLint level,
const gl::Box &clearArea,
vk::ClearTextureMode clearMode,
GLenum format,
GLenum type,
const uint8_t *data)
{
// There should be no zero extents in the clear area, since such calls should return before
// entering the backend with no changes to the texture. For 2D textures, depth should be 1.
//
// From the spec: For texture types that do not have certain dimensions, this command treats
// those dimensions as having a size of 1. For example, to clear a portion of a two-dimensional
// texture, the application would use <zoffset> equal to zero and <depth> equal to one.
ASSERT(clearArea.width != 0 && clearArea.height != 0 && clearArea.depth != 0);
gl::TextureType textureType = mState.getType();
bool useLayerAsDepth = textureType == gl::TextureType::CubeMap ||
textureType == gl::TextureType::CubeMapArray ||
textureType == gl::TextureType::_2DArray ||
textureType == gl::TextureType::_2DMultisampleArray;
// If the texture is renderable (including multisampled), the partial clear can be applied to
// the image simply by opening/closing a render pass with LOAD_OP_CLEAR. Otherwise, a buffer can
// be filled with the given pixel data on the host and staged to the image as a buffer update.
ContextVk *contextVk = vk::GetImpl(context);
const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, type);
const vk::Format &vkFormat =
contextVk->getRenderer()->getFormat(formatInfo.sizedInternalFormat);
const angle::FormatID &actualFormatID =
vkFormat.getActualImageFormatID(getRequiredImageAccess());
bool usesBufferForClear = false;
VkFormatFeatureFlags renderableCheckFlag =
clearMode == vk::ClearTextureMode::FullClear ? VK_FORMAT_FEATURE_TRANSFER_DST_BIT
: formatInfo.isDepthOrStencil() ? VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
: VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;
if (vk::FormatHasNecessaryFeature(contextVk->getRenderer(), actualFormatID, getTilingMode(),
renderableCheckFlag))
{
uint32_t baseLayer = useLayerAsDepth ? clearArea.z : 0;
uint32_t layerCount = useLayerAsDepth ? clearArea.depth : 1;
ANGLE_TRY(mImage->stagePartialClear(contextVk, clearArea, clearMode, mState.getType(),
level, baseLayer, layerCount, type, formatInfo,
vkFormat, getRequiredImageAccess(), data));
}
else
{
ASSERT(mImage->getSamples() <= 1);
bool updateAppliedImmediately = false;
usesBufferForClear = true;
auto pixelSize = static_cast<size_t>(formatInfo.pixelBytes);
std::vector<uint8_t> pixelValue(pixelSize, 0);
if (data != nullptr)
{
memcpy(pixelValue.data(), data, pixelSize);
}
// For a cubemap, each face will be updated separately.
bool isCubeMap = textureType == gl::TextureType::CubeMap;
size_t clearBufferSize =
isCubeMap ? clearArea.width * clearArea.height * pixelSize
: clearArea.width * clearArea.height * clearArea.depth * pixelSize;
std::vector<uint8_t> clearBuffer(clearBufferSize, 0);
ASSERT(clearBufferSize % pixelSize == 0);
if (data != nullptr)
{
for (GLuint i = 0; i < clearBufferSize; i += pixelSize)
{
memcpy(&clearBuffer[i], pixelValue.data(), pixelSize);
}
}
if (isCubeMap)
{
size_t cubeFaceStart = clearArea.z;
auto cubeFaceEnd = static_cast<size_t>(clearArea.z + clearArea.depth);
for (size_t cubeFace = cubeFaceStart; cubeFace < cubeFaceEnd; cubeFace++)
{
const gl::ImageIndex index = gl::ImageIndex::MakeFromTarget(
gl::CubeFaceIndexToTextureTarget(cubeFace), level, 0);
ANGLE_TRY(mImage->stageSubresourceUpdate(
contextVk, getNativeImageIndex(index),
gl::Extents(clearArea.width, clearArea.height, 1),
gl::Offset(clearArea.x, clearArea.y, 0), formatInfo,
contextVk->getState().getUnpackState(), type, clearBuffer.data(), vkFormat,
getRequiredImageAccess(), vk::ApplyImageUpdate::Defer,
&updateAppliedImmediately));
ASSERT(!updateAppliedImmediately);
}
}
else
{
gl::TextureTarget textureTarget = gl::TextureTypeToTarget(textureType, 0);
uint32_t layerCount = useLayerAsDepth ? clearArea.depth : 0;
const gl::ImageIndex index =
gl::ImageIndex::MakeFromTarget(textureTarget, level, layerCount);
ANGLE_TRY(mImage->stageSubresourceUpdate(
contextVk, getNativeImageIndex(index),
gl::Extents(clearArea.width, clearArea.height, clearArea.depth),
gl::Offset(clearArea.x, clearArea.y, clearArea.z), formatInfo,
contextVk->getState().getUnpackState(), type, clearBuffer.data(), vkFormat,
getRequiredImageAccess(), vk::ApplyImageUpdate::Defer, &updateAppliedImmediately));
ASSERT(!updateAppliedImmediately);
}
}
// Flush the staged updates if needed.
ANGLE_TRY(ensureImageInitializedIfUpdatesNeedStageOrFlush(contextVk, gl::LevelIndex(level),
vkFormat, vk::ApplyImageUpdate::Defer,
usesBufferForClear));
return angle::Result::Continue;
}
angle::Result TextureVk::ensureImageInitializedIfUpdatesNeedStageOrFlush(
ContextVk *contextVk,
gl::LevelIndex level,
const vk::Format &vkFormat,
vk::ApplyImageUpdate applyUpdate,
bool usesBufferForUpdate)
{
bool mustFlush =
updateMustBeFlushed(level, vkFormat.getActualImageFormatID(getRequiredImageAccess()));
bool mustStage = applyUpdate == vk::ApplyImageUpdate::Defer;
// If texture has all levels being specified, then do the flush immediately. This tries to avoid
// issue flush as each level is being provided which may end up flushing out the staged clear
// that otherwise might able to be removed. It also helps tracking all updates with just one
// VkEvent instead of one for each level.
if (mustFlush ||
(!mustStage && mImage->valid() && mImage->hasBufferSourcedStagedUpdatesInAllLevels()))
{
ANGLE_TRY(ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
// If forceSubmitImmutableTextureUpdates is enabled, submit the staged updates as well.
if (contextVk->getFeatures().forceSubmitImmutableTextureUpdates.enabled)
{
ANGLE_TRY(contextVk->submitStagedTextureUpdates());
}
}
else if (usesBufferForUpdate && contextVk->isEligibleForMutableTextureFlush() &&
!mState.getImmutableFormat())
{
// Check if we should flush any mutable textures from before.
ANGLE_TRY(contextVk->getShareGroup()->onMutableTextureUpload(contextVk, this));
}
return angle::Result::Continue;
}
angle::Result TextureVk::setSubImageImpl(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Box &area,
const gl::InternalFormat &formatInfo,
GLenum type,
const gl::PixelUnpackState &unpack,
gl::Buffer *unpackBuffer,
const uint8_t *pixels,
const vk::Format &vkFormat)
{
ContextVk *contextVk = vk::GetImpl(context);
bool mustStage = updateMustBeStaged(gl::LevelIndex(index.getLevelIndex()),
vkFormat.getActualImageFormatID(getRequiredImageAccess()));
vk::ApplyImageUpdate applyUpdate;
if (mustStage)
{
applyUpdate = vk::ApplyImageUpdate::Defer;
}
else
{
// Cannot defer to unlocked tail call if:
//
// - The generate mipmap hint is set: This is because on return the Texture class would
// attempt to generate mipmaps, which may reallocate the image, or fall back to software
// mipmap generation.
// - The texture is incomplete: This is because unlocked tail call is disabled on draw
// calls, but that is when incomplete textures are created and initialized.
const bool canDeferToUnlockedTailCall =
mState.getGenerateMipmapHint() != GL_TRUE && !mState.isInternalIncompleteTexture();
// When possible flush out updates immediately.
applyUpdate = canDeferToUnlockedTailCall
? vk::ApplyImageUpdate::ImmediatelyInUnlockedTailCall
: vk::ApplyImageUpdate::Immediately;
}
bool updateAppliedImmediately = false;
if (unpackBuffer)
{
BufferVk *unpackBufferVk = vk::GetImpl(unpackBuffer);
vk::BufferHelper &bufferHelper = unpackBufferVk->getBuffer();
VkDeviceSize bufferOffset = bufferHelper.getOffset();
uintptr_t offset = reinterpret_cast<uintptr_t>(pixels);
GLuint inputRowPitch = 0;
GLuint inputDepthPitch = 0;
GLuint inputSkipBytes = 0;
ANGLE_TRY(mImage->CalculateBufferInfo(
contextVk, gl::Extents(area.width, area.height, area.depth), formatInfo, unpack, type,
index.usesTex3D(), &inputRowPitch, &inputDepthPitch, &inputSkipBytes));
size_t offsetBytes = static_cast<size_t>(bufferOffset + offset + inputSkipBytes);
// Note: cannot directly copy from a depth/stencil PBO. GL requires depth and stencil data
// to be packed, while Vulkan requires them to be separate.
const VkImageAspectFlags aspectFlags =
vk::GetFormatAspectFlags(vkFormat.getIntendedFormat());
const vk::Format &bufferVkFormat =
contextVk->getRenderer()->getFormat(formatInfo.sizedInternalFormat);
if (shouldUpdateBeFlushed(gl::LevelIndex(index.getLevelIndex()),
vkFormat.getActualImageFormatID(getRequiredImageAccess())) &&
isFastUnpackPossible(vkFormat, offsetBytes, bufferVkFormat))
{
GLuint pixelSize = formatInfo.pixelBytes;
GLuint blockWidth = formatInfo.compressedBlockWidth;
GLuint blockHeight = formatInfo.compressedBlockHeight;
if (!formatInfo.compressed)
{
pixelSize = formatInfo.computePixelBytes(type);
blockWidth = 1;
blockHeight = 1;
}
ASSERT(pixelSize != 0 && inputRowPitch != 0 && blockWidth != 0 && blockHeight != 0);
GLuint rowLengthPixels = inputRowPitch / pixelSize * blockWidth;
GLuint imageHeightPixels = inputDepthPitch / inputRowPitch * blockHeight;
ANGLE_TRY(copyBufferDataToImage(contextVk, &bufferHelper, index, rowLengthPixels,
imageHeightPixels, area, offsetBytes, aspectFlags));
}
else
{
ANGLE_VK_PERF_WARNING(
contextVk, GL_DEBUG_SEVERITY_HIGH,
"TexSubImage with unpack buffer copied on CPU due to store, format "
"or offset restrictions");
void *mapPtr = nullptr;
ANGLE_TRY(unpackBufferVk->mapImpl(contextVk, GL_MAP_READ_BIT, &mapPtr));
const uint8_t *source =
static_cast<const uint8_t *>(mapPtr) + reinterpret_cast<ptrdiff_t>(pixels);
ANGLE_TRY(mImage->stageSubresourceUpdateImpl(
contextVk, getNativeImageIndex(index),
gl::Extents(area.width, area.height, area.depth),
gl::Offset(area.x, area.y, area.z), formatInfo, unpack, type, source, vkFormat,
getRequiredImageAccess(), inputRowPitch, inputDepthPitch, inputSkipBytes,
applyUpdate, &updateAppliedImmediately));
ANGLE_TRY(unpackBufferVk->unmapImpl(contextVk));
}
}
else if (pixels)
{
ANGLE_TRY(mImage->stageSubresourceUpdate(
contextVk, getNativeImageIndex(index), gl::Extents(area.width, area.height, area.depth),
gl::Offset(area.x, area.y, area.z), formatInfo, unpack, type, pixels, vkFormat,
getRequiredImageAccess(), applyUpdate, &updateAppliedImmediately));
}
if (updateAppliedImmediately)
{
// Return if stageSubresourceUpdate already applied the update
return angle::Result::Continue;
}
// Flush the staged updates if needed.
ANGLE_TRY(ensureImageInitializedIfUpdatesNeedStageOrFlush(
contextVk, gl::LevelIndex(index.getLevelIndex()), vkFormat, applyUpdate, true));
return angle::Result::Continue;
}
angle::Result TextureVk::copyImage(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Rectangle &sourceArea,
GLenum internalFormat,
gl::Framebuffer *source)
{
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
gl::Extents newImageSize(sourceArea.width, sourceArea.height, 1);
const gl::InternalFormat &internalFormatInfo =
gl::GetInternalFormatInfo(internalFormat, GL_UNSIGNED_BYTE);
const vk::Format &vkFormat = renderer->getFormat(internalFormatInfo.sizedInternalFormat);
// Fall back to renderable format if copy cannot be done in transfer. Must be done before
// the dst format is accessed anywhere (in |redefineLevel| and |copySubImageImpl|).
ANGLE_TRY(ensureRenderableIfCopyTexImageCannotTransfer(contextVk, internalFormatInfo, source));
// The texture level being redefined might be the same as the one bound to the framebuffer.
// This _could_ be supported by using a temp image before redefining the level (and potentially
// discarding the image). However, this is currently unimplemented.
FramebufferVk *framebufferVk = vk::GetImpl(source);
RenderTargetVk *colorReadRT = framebufferVk->getColorReadRenderTarget();
vk::ImageHelper *srcImage = &colorReadRT->getImageForCopy();
const bool isCubeMap = index.getType() == gl::TextureType::CubeMap;
gl::LevelIndex levelIndex(getNativeImageIndex(index).getLevelIndex());
const uint32_t layerIndex = index.hasLayer() ? index.getLayerIndex() : 0;
const uint32_t redefinedFace = isCubeMap ? layerIndex : 0;
const uint32_t sourceFace = isCubeMap ? colorReadRT->getLayerIndex() : 0;
const bool isSelfCopy = mImage == srcImage && levelIndex == colorReadRT->getLevelIndex() &&
redefinedFace == sourceFace;
ANGLE_TRY(redefineLevel(context, index, vkFormat, newImageSize));
if (isSelfCopy)
{
UNIMPLEMENTED();
return angle::Result::Continue;
}
return copySubImageImpl(context, index, gl::Offset(0, 0, 0), sourceArea, internalFormatInfo,
source);
}
angle::Result TextureVk::copySubImage(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
gl::Framebuffer *source)
{
const gl::InternalFormat ¤tFormat = *mState.getImageDesc(index).format.info;
// Fall back to renderable format if copy cannot be done in transfer. Must be done before
// the dst format is accessed anywhere (in |redefineLevel| and |copySubImageImpl|).
ANGLE_TRY(
ensureRenderableIfCopyTexImageCannotTransfer(vk::GetImpl(context), currentFormat, source));
return copySubImageImpl(context, index, destOffset, sourceArea, currentFormat, source);
}
angle::Result TextureVk::copyTexture(const gl::Context *context,
const gl::ImageIndex &index,
GLenum internalFormat,
GLenum type,
GLint sourceLevelGL,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
const gl::Texture *source)
{
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
TextureVk *sourceVk = vk::GetImpl(source);
const gl::ImageDesc &srcImageDesc =
sourceVk->mState.getImageDesc(NonCubeTextureTypeToTarget(source->getType()), sourceLevelGL);
gl::Box sourceBox(gl::kOffsetZero, srcImageDesc.size);
const gl::InternalFormat &dstFormatInfo = gl::GetInternalFormatInfo(internalFormat, type);
const vk::Format &dstVkFormat = renderer->getFormat(dstFormatInfo.sizedInternalFormat);
ANGLE_TRY(sourceVk->ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
// Fall back to renderable format if copy cannot be done in transfer. Must be done before
// the dst format is accessed anywhere (in |redefineLevel| and |copySubTextureImpl|).
ANGLE_TRY(ensureRenderableIfCopyTextureCannotTransfer(contextVk, dstFormatInfo, unpackFlipY,
unpackPremultiplyAlpha,
unpackUnmultiplyAlpha, sourceVk));
ANGLE_TRY(redefineLevel(context, index, dstVkFormat, srcImageDesc.size));
return copySubTextureImpl(contextVk, index, gl::kOffsetZero, dstFormatInfo,
gl::LevelIndex(sourceLevelGL), sourceBox, unpackFlipY,
unpackPremultiplyAlpha, unpackUnmultiplyAlpha, sourceVk);
}
angle::Result TextureVk::copySubTexture(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Offset &dstOffset,
GLint srcLevelGL,
const gl::Box &sourceBox,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
const gl::Texture *source)
{
ContextVk *contextVk = vk::GetImpl(context);
gl::TextureTarget target = index.getTarget();
gl::LevelIndex dstLevelGL(index.getLevelIndex());
const gl::InternalFormat &dstFormatInfo =
*mState.getImageDesc(target, dstLevelGL.get()).format.info;
ANGLE_TRY(
vk::GetImpl(source)->ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
// Fall back to renderable format if copy cannot be done in transfer. Must be done before
// the dst format is accessed anywhere (in |copySubTextureImpl|).
ANGLE_TRY(ensureRenderableIfCopyTextureCannotTransfer(
contextVk, dstFormatInfo, unpackFlipY, unpackPremultiplyAlpha, unpackUnmultiplyAlpha,
vk::GetImpl(source)));
return copySubTextureImpl(contextVk, index, dstOffset, dstFormatInfo,
gl::LevelIndex(srcLevelGL), sourceBox, unpackFlipY,
unpackPremultiplyAlpha, unpackUnmultiplyAlpha, vk::GetImpl(source));
}
angle::Result TextureVk::copyRenderbufferSubData(const gl::Context *context,
const gl::Renderbuffer *srcBuffer,
GLint srcLevel,
GLint srcX,
GLint srcY,
GLint srcZ,
GLint dstLevel,
GLint dstX,
GLint dstY,
GLint dstZ,
GLsizei srcWidth,
GLsizei srcHeight,
GLsizei srcDepth)
{
ContextVk *contextVk = vk::GetImpl(context);
RenderbufferVk *sourceVk = vk::GetImpl(srcBuffer);
// Make sure the source/destination targets are initialized and all staged updates are flushed.
ANGLE_TRY(sourceVk->ensureImageInitialized(context));
ANGLE_TRY(ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
return vk::ImageHelper::CopyImageSubData(context, sourceVk->getImage(), srcLevel, srcX, srcY,
srcZ, mImage, dstLevel, dstX, dstY, dstZ, srcWidth,
srcHeight, srcDepth);
}
angle::Result TextureVk::copyTextureSubData(const gl::Context *context,
const gl::Texture *srcTexture,
GLint srcLevel,
GLint srcX,
GLint srcY,
GLint srcZ,
GLint dstLevel,
GLint dstX,
GLint dstY,
GLint dstZ,
GLsizei srcWidth,
GLsizei srcHeight,
GLsizei srcDepth)
{
ContextVk *contextVk = vk::GetImpl(context);
TextureVk *sourceVk = vk::GetImpl(srcTexture);
// Make sure the source/destination targets are initialized and all staged updates are flushed.
ANGLE_TRY(sourceVk->ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
ANGLE_TRY(ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
return vk::ImageHelper::CopyImageSubData(context, &sourceVk->getImage(), srcLevel, srcX, srcY,
srcZ, mImage, dstLevel, dstX, dstY, dstZ, srcWidth,
srcHeight, srcDepth);
}
angle::Result TextureVk::copyCompressedTexture(const gl::Context *context,
const gl::Texture *source)
{
ContextVk *contextVk = vk::GetImpl(context);
TextureVk *sourceVk = vk::GetImpl(source);
gl::TextureTarget sourceTarget = NonCubeTextureTypeToTarget(source->getType());
constexpr GLint sourceLevelGL = 0;
constexpr GLint destLevelGL = 0;
const gl::InternalFormat &internalFormat = *source->getFormat(sourceTarget, sourceLevelGL).info;
const vk::Format &vkFormat =
contextVk->getRenderer()->getFormat(internalFormat.sizedInternalFormat);
const gl::Extents size(static_cast<int>(source->getWidth(sourceTarget, sourceLevelGL)),
static_cast<int>(source->getHeight(sourceTarget, sourceLevelGL)),
static_cast<int>(source->getDepth(sourceTarget, sourceLevelGL)));
const gl::ImageIndex destIndex = gl::ImageIndex::MakeFromTarget(sourceTarget, destLevelGL, 1);
ANGLE_TRY(redefineLevel(context, destIndex, vkFormat, size));
ANGLE_TRY(sourceVk->ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
return copySubImageImplWithTransfer(contextVk, destIndex, gl::kOffsetZero, vkFormat,
gl::LevelIndex(sourceLevelGL), 0,
gl::Box(gl::kOffsetZero, size), &sourceVk->getImage());
}
angle::Result TextureVk::copySubImageImpl(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
const gl::InternalFormat &internalFormat,
gl::Framebuffer *source)
{
gl::Extents fbSize = source->getReadColorAttachment()->getSize();
gl::Rectangle clippedSourceArea;
if (!ClipRectangle(sourceArea, gl::Rectangle(0, 0, fbSize.width, fbSize.height),
&clippedSourceArea))
{
return angle::Result::Continue;
}
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
FramebufferVk *framebufferVk = vk::GetImpl(source);
const gl::ImageIndex offsetImageIndex = getNativeImageIndex(index);
// If negative offsets are given, clippedSourceArea ensures we don't read from those offsets.
// However, that changes the sourceOffset->destOffset mapping. Here, destOffset is shifted by
// the same amount as clipped to correct the error.
VkImageType imageType = gl_vk::GetImageType(mState.getType());
int zOffset = (imageType == VK_IMAGE_TYPE_3D) ? destOffset.z : 0;
const gl::Offset modifiedDestOffset(destOffset.x + clippedSourceArea.x - sourceArea.x,
destOffset.y + clippedSourceArea.y - sourceArea.y, zOffset);
RenderTargetVk *colorReadRT = framebufferVk->getColorReadRenderTarget();
angle::FormatID srcIntendedFormatID = colorReadRT->getImageIntendedFormatID();
angle::FormatID srcActualFormatID = colorReadRT->getImageActualFormatID();
VkImageTiling srcTilingMode = colorReadRT->getImageForCopy().getTilingMode();
const vk::Format &dstFormat = renderer->getFormat(internalFormat.sizedInternalFormat);
angle::FormatID dstIntendedFormatID = dstFormat.getIntendedFormatID();
angle::FormatID dstActualFormatID = dstFormat.getActualImageFormatID(getRequiredImageAccess());
VkImageTiling destTilingMode = getTilingMode();
bool isViewportFlipY = contextVk->isViewportFlipEnabledForReadFBO();
gl::Box clippedSourceBox(clippedSourceArea.x, clippedSourceArea.y, colorReadRT->getLayerIndex(),
clippedSourceArea.width, clippedSourceArea.height, 1);
// If it's possible to perform the copy with a transfer, that's the best option.
if (CanCopyWithTransferForTexImage(renderer, srcIntendedFormatID, srcActualFormatID,
srcTilingMode, dstIntendedFormatID, dstActualFormatID,
destTilingMode, isViewportFlipY))
{
return copySubImageImplWithTransfer(contextVk, offsetImageIndex, modifiedDestOffset,
dstFormat, colorReadRT->getLevelIndex(),
colorReadRT->getLayerIndex(), clippedSourceBox,
&colorReadRT->getImageForCopy());
}
// If it's possible to perform the copy with a draw call, do that.
if (CanCopyWithDraw(renderer, srcActualFormatID, srcTilingMode, dstActualFormatID,
destTilingMode))
{
// Layer count can only be 1 as the source is a framebuffer.
ASSERT(offsetImageIndex.getLayerCount() == 1);
// Flush the render pass, which may incur a vkQueueSubmit, before taking any views.
// Otherwise the view serials would not reflect the render pass they are really used in.
// http://crbug.com/1272266#c22
ANGLE_TRY(
contextVk->flushCommandsAndEndRenderPass(RenderPassClosureReason::PrepareForImageCopy));
const vk::ImageView *copyImageView = nullptr;
ANGLE_TRY(colorReadRT->getCopyImageView(contextVk, ©ImageView));
return copySubImageImplWithDraw(contextVk, offsetImageIndex, modifiedDestOffset, dstFormat,
colorReadRT->getLevelIndex(), clippedSourceBox,
isViewportFlipY, false, false, false,
&colorReadRT->getImageForCopy(), copyImageView,
contextVk->getRotationReadFramebuffer());
}
ANGLE_VK_PERF_WARNING(contextVk, GL_DEBUG_SEVERITY_HIGH,
"Texture copied on CPU due to format restrictions");
// Do a CPU readback that does the conversion, and then stage the change to the pixel buffer.
ANGLE_TRY(mImage->stageSubresourceUpdateFromFramebuffer(
context, offsetImageIndex, clippedSourceArea, modifiedDestOffset,
gl::Extents(clippedSourceArea.width, clippedSourceArea.height, 1), internalFormat,
getRequiredImageAccess(), framebufferVk));
// Flush out staged update if possible
if (shouldUpdateBeFlushed(gl::LevelIndex(index.getLevelIndex()), dstActualFormatID))
{
ANGLE_TRY(flushImageStagedUpdates(contextVk));
}
return angle::Result::Continue;
}
angle::Result TextureVk::copySubTextureImpl(ContextVk *contextVk,
const gl::ImageIndex &index,
const gl::Offset &dstOffset,
const gl::InternalFormat &dstFormat,
gl::LevelIndex sourceLevelGL,
const gl::Box &sourceBox,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
TextureVk *source)
{
vk::Renderer *renderer = contextVk->getRenderer();
const angle::Format &srcIntendedFormat = source->getImage().getIntendedFormat();
angle::FormatID srcFormatID = source->getImage().getActualFormatID();
VkImageTiling srcTilingMode = source->getImage().getTilingMode();
const vk::Format &dstVkFormat = renderer->getFormat(dstFormat.sizedInternalFormat);
angle::FormatID dstFormatID = dstVkFormat.getActualImageFormatID(getRequiredImageAccess());
VkImageTiling dstTilingMode = getTilingMode();
const gl::ImageIndex offsetImageIndex = getNativeImageIndex(index);
// If it's possible to perform the copy with a transfer, that's the best option.
if (CanCopyWithTransferForCopyTexture(
renderer, source->getImage(), srcTilingMode, dstVkFormat.getIntendedFormatID(),
dstFormatID, dstTilingMode, unpackFlipY, unpackPremultiplyAlpha, unpackUnmultiplyAlpha))
{
return copySubImageImplWithTransfer(contextVk, offsetImageIndex, dstOffset, dstVkFormat,
sourceLevelGL, sourceBox.z, sourceBox,
&source->getImage());
}
// If it's possible to perform the copy with a draw call, do that.
if (CanCopyWithDraw(renderer, srcFormatID, srcTilingMode, dstFormatID, dstTilingMode))
{
// Flush the render pass, which may incur a vkQueueSubmit, before taking any views.
// Otherwise the view serials would not reflect the render pass they are really used in.
// http://crbug.com/1272266#c22
ANGLE_TRY(
contextVk->flushCommandsAndEndRenderPass(RenderPassClosureReason::PrepareForImageCopy));
return copySubImageImplWithDraw(
contextVk, offsetImageIndex, dstOffset, dstVkFormat, sourceLevelGL, sourceBox, false,
unpackFlipY, unpackPremultiplyAlpha, unpackUnmultiplyAlpha, &source->getImage(),
&source->getCopyImageView(), SurfaceRotation::Identity);
}
ANGLE_VK_PERF_WARNING(contextVk, GL_DEBUG_SEVERITY_HIGH,
"Texture copied on CPU due to format restrictions");
// Read back the requested region of the source texture
vk::RendererScoped<vk::BufferHelper> bufferHelper(renderer);
uint8_t *sourceData = nullptr;
ANGLE_TRY(source->copyImageDataToBufferAndGetData(
contextVk, sourceLevelGL, sourceBox.depth, sourceBox,
RenderPassClosureReason::CopyTextureOnCPU, &bufferHelper.get(), &sourceData));
const angle::Format &srcTextureFormat = source->getImage().getActualFormat();
const angle::Format &dstTextureFormat =
dstVkFormat.getActualImageFormat(getRequiredImageAccess());
size_t destinationAllocationSize =
sourceBox.width * sourceBox.height * sourceBox.depth * dstTextureFormat.pixelBytes;
// Allocate memory in the destination texture for the copy/conversion
uint32_t stagingBaseLayer =
offsetImageIndex.hasLayer() ? offsetImageIndex.getLayerIndex() : dstOffset.z;
uint32_t stagingLayerCount = sourceBox.depth;
gl::Offset stagingOffset = dstOffset;
gl::Extents stagingExtents(sourceBox.width, sourceBox.height, sourceBox.depth);
bool is3D = gl_vk::GetImageType(mState.getType()) == VK_IMAGE_TYPE_3D;
if (is3D)
{
stagingBaseLayer = 0;
stagingLayerCount = 1;
}
else
{
stagingOffset.z = 0;
stagingExtents.depth = 1;
}
const gl::ImageIndex stagingIndex = gl::ImageIndex::Make2DArrayRange(
offsetImageIndex.getLevelIndex(), stagingBaseLayer, stagingLayerCount);
uint8_t *destData = nullptr;
ANGLE_TRY(mImage->stageSubresourceUpdateAndGetData(contextVk, destinationAllocationSize,
stagingIndex, stagingExtents, stagingOffset,
&destData, dstFormatID));
// Source and dst data is tightly packed
GLuint srcDataRowPitch = sourceBox.width * srcTextureFormat.pixelBytes;
GLuint dstDataRowPitch = sourceBox.width * dstTextureFormat.pixelBytes;
GLuint srcDataDepthPitch = srcDataRowPitch * sourceBox.height;
GLuint dstDataDepthPitch = dstDataRowPitch * sourceBox.height;
rx::PixelReadFunction pixelReadFunction = srcTextureFormat.pixelReadFunction;
rx::PixelWriteFunction pixelWriteFunction = dstTextureFormat.pixelWriteFunction;
// Fix up the read/write functions for the sake of luminance/alpha that are emulated with
// formats whose channels don't correspond to the original format (alpha is emulated with red,
// and luminance/alpha is emulated with red/green).
if (srcIntendedFormat.isLUMA())
{
pixelReadFunction = srcIntendedFormat.pixelReadFunction;
}
if (dstVkFormat.getIntendedFormat().isLUMA())
{
pixelWriteFunction = dstVkFormat.getIntendedFormat().pixelWriteFunction;
}
CopyImageCHROMIUM(sourceData, srcDataRowPitch, srcTextureFormat.pixelBytes, srcDataDepthPitch,
pixelReadFunction, destData, dstDataRowPitch, dstTextureFormat.pixelBytes,
dstDataDepthPitch, pixelWriteFunction, dstFormat.format,
dstFormat.componentType, sourceBox.width, sourceBox.height, sourceBox.depth,
unpackFlipY, unpackPremultiplyAlpha, unpackUnmultiplyAlpha);
if (shouldUpdateBeFlushed(gl::LevelIndex(index.getLevelIndex()), dstFormatID))
{
ANGLE_TRY(flushImageStagedUpdates(contextVk));
}
return angle::Result::Continue;
}
angle::Result TextureVk::copySubImageImplWithTransfer(ContextVk *contextVk,
const gl::ImageIndex &index,
const gl::Offset &dstOffset,
const vk::Format &dstFormat,
gl::LevelIndex sourceLevelGL,
size_t sourceLayer,
const gl::Box &sourceBox,
vk::ImageHelper *srcImage)
{
vk::Renderer *renderer = contextVk->getRenderer();
gl::LevelIndex level(index.getLevelIndex());
uint32_t baseLayer = index.hasLayer() ? index.getLayerIndex() : dstOffset.z;
uint32_t layerCount = sourceBox.depth;
gl::Offset srcOffset = {sourceBox.x, sourceBox.y, sourceBox.z};
gl::Extents extents = {sourceBox.width, sourceBox.height, sourceBox.depth};
// Change source layout if necessary
vk::CommandBufferAccess access;
access.onImageTransferRead(VK_IMAGE_ASPECT_COLOR_BIT, srcImage);
VkImageSubresourceLayers srcSubresource = {};
srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
srcSubresource.mipLevel = srcImage->toVkLevel(sourceLevelGL).get();
srcSubresource.baseArrayLayer = static_cast<uint32_t>(sourceLayer);
srcSubresource.layerCount = layerCount;
bool isSrc3D = srcImage->getExtents().depth > 1;
bool isDest3D = gl_vk::GetImageType(mState.getType()) == VK_IMAGE_TYPE_3D;
if (isSrc3D)
{
Set3DBaseArrayLayerAndLayerCount(&srcSubresource);
}
else
{
ASSERT(srcSubresource.baseArrayLayer == static_cast<uint32_t>(srcOffset.z));
srcOffset.z = 0;
}
gl::Offset dstOffsetModified = dstOffset;
if (!isDest3D)
{
// If destination is not 3D, destination offset must be 0.
dstOffsetModified.z = 0;
}
// Perform self-copies through a staging buffer.
// TODO: optimize to copy directly if possible. http://anglebug.com/42263319
bool isSelfCopy = mImage == srcImage;
// If destination is valid, copy the source directly into it.
if (shouldUpdateBeFlushed(level, dstFormat.getActualImageFormatID(getRequiredImageAccess())) &&
!isSelfCopy)
{
// Make sure any updates to the image are already flushed.
ANGLE_TRY(ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
access.onImageTransferWrite(level, 1, baseLayer, layerCount, VK_IMAGE_ASPECT_COLOR_BIT,
mImage);
vk::OutsideRenderPassCommandBuffer *commandBuffer;
ANGLE_TRY(contextVk->getOutsideRenderPassCommandBuffer(access, &commandBuffer));
VkImageSubresourceLayers destSubresource = srcSubresource;
destSubresource.mipLevel = mImage->toVkLevel(level).get();
destSubresource.baseArrayLayer = baseLayer;
destSubresource.layerCount = layerCount;
if (isDest3D)
{
Set3DBaseArrayLayerAndLayerCount(&destSubresource);
}
else if (!isSrc3D)
{
// extents.depth should be set to layer count if any of the source or destination is a
// 2D Array. If both are 2D Array, it should be set to 1.
extents.depth = 1;
}
vk::ImageHelper::Copy(renderer, srcImage, mImage, srcOffset, dstOffsetModified, extents,
srcSubresource, destSubresource, commandBuffer);
}
else
{
// Create a temporary image to stage the copy
std::unique_ptr<vk::RefCounted<vk::ImageHelper>> stagingImage;
stagingImage = std::make_unique<vk::RefCounted<vk::ImageHelper>>();
ANGLE_TRY(stagingImage->get().init2DStaging(
contextVk, mState.hasProtectedContent(), renderer->getMemoryProperties(),
gl::Extents(sourceBox.width, sourceBox.height, 1), dstFormat.getIntendedFormatID(),
dstFormat.getActualImageFormatID(getRequiredImageAccess()), kTransferImageFlags,
layerCount));
access.onImageTransferWrite(gl::LevelIndex(0), 1, 0, layerCount, VK_IMAGE_ASPECT_COLOR_BIT,
&stagingImage->get());
vk::OutsideRenderPassCommandBuffer *commandBuffer;
ANGLE_TRY(contextVk->getOutsideRenderPassCommandBuffer(access, &commandBuffer));
VkImageSubresourceLayers destSubresource = srcSubresource;
destSubresource.mipLevel = 0;
destSubresource.baseArrayLayer = 0;
destSubresource.layerCount = layerCount;
if (!isSrc3D)
{
// extents.depth should be set to layer count if any of the source or destination is a
// 2D Array. If both are 2D Array, it should be set to 1.
extents.depth = 1;
}
vk::ImageHelper::Copy(renderer, srcImage, &stagingImage->get(), srcOffset, gl::kOffsetZero,
extents, srcSubresource, destSubresource, commandBuffer);
// Stage the copy for when the image storage is actually created.
VkImageType imageType = gl_vk::GetImageType(mState.getType());
const gl::ImageIndex stagingIndex =
gl::ImageIndex::Make2DArrayRange(level.get(), baseLayer, layerCount);
mImage->stageSubresourceUpdateFromImage(stagingImage.release(), stagingIndex,
vk::LevelIndex(0), dstOffsetModified, extents,
imageType);
}
return angle::Result::Continue;
}
angle::Result TextureVk::copySubImageImplWithDraw(ContextVk *contextVk,
const gl::ImageIndex &index,
const gl::Offset &dstOffset,
const vk::Format &dstFormat,
gl::LevelIndex sourceLevelGL,
const gl::Box &sourceBox,
bool isSrcFlipY,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
vk::ImageHelper *srcImage,
const vk::ImageView *srcView,
SurfaceRotation srcFramebufferRotation)
{
vk::Renderer *renderer = contextVk->getRenderer();
UtilsVk &utilsVk = contextVk->getUtils();
// Potentially make adjustments for pre-rotation.
gl::Box rotatedSourceBox = sourceBox;
gl::Extents srcExtents = srcImage->getLevelExtents2D(vk::LevelIndex(0));
switch (srcFramebufferRotation)
{
case SurfaceRotation::Identity:
// No adjustments needed
break;
case SurfaceRotation::Rotated90Degrees:
// Turn off y-flip for 90 degrees, as we don't want it affecting the
// shaderParams.srcOffset calculation done in UtilsVk::copyImage().
ASSERT(isSrcFlipY);
isSrcFlipY = false;
std::swap(rotatedSourceBox.x, rotatedSourceBox.y);
std::swap(rotatedSourceBox.width, rotatedSourceBox.height);
std::swap(srcExtents.width, srcExtents.height);
break;
case SurfaceRotation::Rotated180Degrees:
ASSERT(isSrcFlipY);
rotatedSourceBox.x = srcExtents.width - sourceBox.x - sourceBox.width - 1;
rotatedSourceBox.y = srcExtents.height - sourceBox.y - sourceBox.height - 1;
break;
case SurfaceRotation::Rotated270Degrees:
// Turn off y-flip for 270 degrees, as we don't want it affecting the
// shaderParams.srcOffset calculation done in UtilsVk::copyImage(). It is needed
// within the shader (when it will affect how the shader looks-up the source pixel),
// and so shaderParams.flipY is turned on at the right time within
// UtilsVk::copyImage().
ASSERT(isSrcFlipY);
isSrcFlipY = false;
rotatedSourceBox.x = srcExtents.height - sourceBox.y - sourceBox.height - 1;
rotatedSourceBox.y = srcExtents.width - sourceBox.x - sourceBox.width - 1;
std::swap(rotatedSourceBox.width, rotatedSourceBox.height);
std::swap(srcExtents.width, srcExtents.height);
break;
default:
UNREACHABLE();
break;
}
gl::LevelIndex level(index.getLevelIndex());
UtilsVk::CopyImageParameters params;
params.srcOffset[0] = rotatedSourceBox.x;
params.srcOffset[1] = rotatedSourceBox.y;
params.srcExtents[0] = rotatedSourceBox.width;
params.srcExtents[1] = rotatedSourceBox.height;
params.dstOffset[0] = dstOffset.x;
params.dstOffset[1] = dstOffset.y;
params.srcMip = srcImage->toVkLevel(sourceLevelGL).get();
params.srcSampleCount = srcImage->getSamples();
params.srcHeight = srcExtents.height;
params.dstMip = level;
params.srcPremultiplyAlpha = unpackPremultiplyAlpha && !unpackUnmultiplyAlpha;
params.srcUnmultiplyAlpha = unpackUnmultiplyAlpha && !unpackPremultiplyAlpha;
params.srcFlipY = isSrcFlipY;
params.dstFlipY = unpackFlipY;
params.srcRotation = srcFramebufferRotation;
uint32_t baseLayer = index.hasLayer() ? index.getLayerIndex() : dstOffset.z;
uint32_t layerCount = sourceBox.depth;
gl::Extents extents = {sourceBox.width, sourceBox.height, sourceBox.depth};
bool isSrc3D = srcImage->getExtents().depth > 1;
bool isDest3D = gl_vk::GetImageType(mState.getType()) == VK_IMAGE_TYPE_3D;
// Perform self-copies through a staging buffer.
// TODO: optimize to copy directly if possible. http://anglebug.com/42263319
bool isSelfCopy = mImage == srcImage;
params.srcColorEncoding =
gl::GetSizedInternalFormatInfo(srcImage->getIntendedFormat().glInternalFormat)
.colorEncoding;
params.dstColorEncoding =
gl::GetSizedInternalFormatInfo(dstFormat.getIntendedFormat().glInternalFormat)
.colorEncoding;
// If destination is valid, copy the source directly into it.
if (shouldUpdateBeFlushed(level, dstFormat.getActualImageFormatID(getRequiredImageAccess())) &&
!isSelfCopy)
{
// Make sure any updates to the image are already flushed.
ANGLE_TRY(flushImageStagedUpdates(contextVk));
for (uint32_t layerIndex = 0; layerIndex < layerCount; ++layerIndex)
{
params.srcLayer = layerIndex + sourceBox.z;
params.dstLayer = baseLayer + layerIndex;
const vk::ImageView *destView;
ANGLE_TRY(getLevelLayerImageView(contextVk, level, baseLayer + layerIndex, &destView));
ANGLE_TRY(utilsVk.copyImage(contextVk, mImage, destView, srcImage, srcView, params));
}
}
else
{
GLint samples = srcImage->getSamples();
gl::TextureType stagingTextureType = vk::Get2DTextureType(layerCount, samples);
// Create a temporary image to stage the copy
std::unique_ptr<vk::RefCounted<vk::ImageHelper>> stagingImage;
stagingImage = std::make_unique<vk::RefCounted<vk::ImageHelper>>();
ANGLE_TRY(stagingImage->get().init2DStaging(
contextVk, mState.hasProtectedContent(), renderer->getMemoryProperties(),
gl::Extents(sourceBox.width, sourceBox.height, 1), dstFormat.getIntendedFormatID(),
dstFormat.getActualImageFormatID(getRequiredImageAccess()), kDrawStagingImageFlags,
layerCount));
params.dstOffset[0] = 0;
params.dstOffset[1] = 0;
for (uint32_t layerIndex = 0; layerIndex < layerCount; ++layerIndex)
{
params.srcLayer = layerIndex + sourceBox.z;
params.dstLayer = layerIndex;
// Create a temporary view for this layer.
vk::ImageView stagingView;
ANGLE_TRY(stagingImage->get().initLayerImageView(
contextVk, stagingTextureType, VK_IMAGE_ASPECT_COLOR_BIT, gl::SwizzleState(),
&stagingView, vk::LevelIndex(0), 1, layerIndex, 1));
ANGLE_TRY(utilsVk.copyImage(contextVk, &stagingImage->get(), &stagingView, srcImage,
srcView, params));
// Queue the resource for cleanup as soon as the copy above is finished. There's no
// need to keep it around.
contextVk->addGarbage(&stagingView);
}
if (!isSrc3D)
{
// extents.depth should be set to layer count if any of the source or destination is a
// 2D Array. If both are 2D Array, it should be set to 1.
extents.depth = 1;
}
gl::Offset dstOffsetModified = dstOffset;
if (!isDest3D)
{
// If destination is not 3D, destination offset must be 0.
dstOffsetModified.z = 0;
}
// Stage the copy for when the image storage is actually created.
VkImageType imageType = gl_vk::GetImageType(mState.getType());
const gl::ImageIndex stagingIndex =
gl::ImageIndex::Make2DArrayRange(level.get(), baseLayer, layerCount);
mImage->stageSubresourceUpdateFromImage(stagingImage.release(), stagingIndex,
vk::LevelIndex(0), dstOffsetModified, extents,
imageType);
}
return angle::Result::Continue;
}
angle::Result TextureVk::setStorageImpl(ContextVk *contextVk,
gl::TextureType type,
const vk::Format &format)
{
if (!mOwnsImage)
{
releaseAndDeleteImageAndViews(contextVk);
}
else if (mImage)
{
if (!contextVk->hasDisplayTextureShareGroup())
{
contextVk->getShareGroup()->onTextureRelease(this);
}
mImage->releaseStagedUpdates(contextVk->getRenderer());
}
// Assume all multisample texture types must be renderable.
if (type == gl::TextureType::_2DMultisample || type == gl::TextureType::_2DMultisampleArray)
{
ANGLE_TRY(ensureRenderableWithFormat(contextVk, format, nullptr));
}
// Fixed rate compression
if (mState.getSurfaceCompressionFixedRate() != GL_SURFACE_COMPRESSION_FIXED_RATE_NONE_EXT)
{
ANGLE_TRY(ensureRenderableWithFormat(contextVk, format, nullptr));
}
ANGLE_TRY(ensureImageAllocated(contextVk, format));
if (mImage->valid())
{
releaseImage(contextVk);
}
ASSERT(mState.getImmutableFormat());
ASSERT(!TextureHasAnyRedefinedLevels(mRedefinedLevels));
ANGLE_TRY(initImage(contextVk, format.getIntendedFormatID(),
format.getActualImageFormatID(getRequiredImageAccess()),
ImageMipLevels::FullMipChainForGenerateMipmap));
return angle::Result::Continue;
}
angle::Result TextureVk::setStorage(const gl::Context *context,
gl::TextureType type,
size_t levels,
GLenum internalFormat,
const gl::Extents &size)
{
return setStorageMultisample(context, type, 1, internalFormat, size, true);
}
angle::Result TextureVk::setStorageMultisample(const gl::Context *context,
gl::TextureType type,
GLsizei samples,
GLint internalformat,
const gl::Extents &size,
bool fixedSampleLocations)
{
ContextVk *contextVk = GetAs<ContextVk>(context->getImplementation());
vk::Renderer *renderer = contextVk->getRenderer();
const vk::Format &format = renderer->getFormat(internalformat);
ANGLE_TRY(setStorageImpl(contextVk, type, format));
return angle::Result::Continue;
}
angle::Result TextureVk::setStorageExternalMemory(const gl::Context *context,
gl::TextureType type,
size_t levels,
GLenum internalFormat,
const gl::Extents &size,
gl::MemoryObject *memoryObject,
GLuint64 offset,
GLbitfield createFlags,
GLbitfield usageFlags,
const void *imageCreateInfoPNext)
{
ContextVk *contextVk = vk::GetImpl(context);
MemoryObjectVk *memoryObjectVk = vk::GetImpl(memoryObject);
vk::Renderer *renderer = contextVk->getRenderer();
const vk::Format &vkFormat = renderer->getFormat(internalFormat);
angle::FormatID actualFormatID = vkFormat.getActualRenderableImageFormatID();
releaseAndDeleteImageAndViews(contextVk);
setImageHelper(contextVk, new vk::ImageHelper(), gl::TextureType::InvalidEnum, 0, 0, true, {});
mImage->setTilingMode(gl_vk::GetTilingMode(mState.getTilingMode()));
// EXT_external_objects issue 13 says that all supported usage flags must be specified.
// However, ANGLE_external_objects_flags allows these flags to be masked. Note that the GL enum
// values constituting the bits of |usageFlags| are identical to their corresponding Vulkan
// value.
usageFlags &= vk::GetMaximalImageUsageFlags(renderer, actualFormatID);
// Similarly, createFlags is restricted to what is valid.
createFlags &= vk::GetMinimalImageCreateFlags(renderer, type, usageFlags) |
VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
ANGLE_TRY(memoryObjectVk->createImage(contextVk, type, levels, internalFormat, size, offset,
mImage, createFlags, usageFlags, imageCreateInfoPNext));
mImageUsageFlags = usageFlags;
mImageCreateFlags = createFlags;
constexpr VkImageUsageFlags kRenderableUsageFlags =
kColorAttachmentImageFlags | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
if ((usageFlags & kRenderableUsageFlags) != 0)
{
mRequiredImageAccess = vk::ImageAccess::Renderable;
}
ANGLE_TRY(initImageViews(contextVk, getImageViewLevelCount()));
return angle::Result::Continue;
}
angle::Result TextureVk::setStorageAttribs(const gl::Context *context,
gl::TextureType type,
size_t levels,
GLint internalformat,
const gl::Extents &size,
const GLint *attribList)
{
ContextVk *contextVk = GetAs<ContextVk>(context->getImplementation());
vk::Renderer *renderer = contextVk->getRenderer();
const vk::Format &format = renderer->getFormat(internalformat);
ANGLE_TRY(setStorageImpl(contextVk, type, format));
return angle::Result::Continue;
}
GLint TextureVk::getImageCompressionRate(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
ASSERT(mImage != nullptr && mImage->valid());
ASSERT(renderer->getFeatures().supportsImageCompressionControl.enabled);
if (!mOwnsImage)
{
return 0;
}
VkImageSubresource2EXT imageSubresource2 = {};
imageSubresource2.sType = VK_STRUCTURE_TYPE_IMAGE_SUBRESOURCE_2_EXT;
imageSubresource2.imageSubresource.aspectMask = mImage->getAspectFlags();
VkImageCompressionPropertiesEXT compressionProperties = {};
compressionProperties.sType = VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT;
VkSubresourceLayout2EXT subresourceLayout = {};
subresourceLayout.sType = VK_STRUCTURE_TYPE_SUBRESOURCE_LAYOUT_2_EXT;
subresourceLayout.pNext = &compressionProperties;
vkGetImageSubresourceLayout2EXT(renderer->getDevice(), mImage->getImage().getHandle(),
&imageSubresource2, &subresourceLayout);
GLint compressionRate;
// For an existing image, should only report one compression rate
vk_gl::ConvertCompressionFlagsToGLFixedRates(
compressionProperties.imageCompressionFixedRateFlags, 1, &compressionRate);
return compressionRate;
}
GLint TextureVk::getFormatSupportedCompressionRatesImpl(vk::Renderer *renderer,
const vk::Format &format,
GLsizei bufSize,
GLint *rates)
{
if (renderer->getFeatures().supportsImageCompressionControl.enabled)
{
VkImageCompressionControlEXT compressionInfo = {};
compressionInfo.sType = VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT;
// Use default compression control flag for query
compressionInfo.flags = VK_IMAGE_COMPRESSION_FIXED_RATE_DEFAULT_EXT;
VkImageCompressionPropertiesEXT compressionProp = {};
compressionProp.sType = VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT;
if (vk::ImageHelper::FormatSupportsUsage(
renderer,
vk::GetVkFormatFromFormatID(renderer, format.getActualRenderableImageFormatID()),
VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
0, &compressionInfo, &compressionProp,
vk::ImageHelper::FormatSupportCheck::OnlyQuerySuccess))
{
if ((compressionProp.imageCompressionFlags &
VK_IMAGE_COMPRESSION_FIXED_RATE_EXPLICIT_EXT) != 0)
{
return vk_gl::ConvertCompressionFlagsToGLFixedRates(
compressionProp.imageCompressionFixedRateFlags, bufSize, rates);
}
}
}
return 0;
}
GLint TextureVk::getFormatSupportedCompressionRates(const gl::Context *context,
GLenum internalformat,
GLsizei bufSize,
GLint *rates)
{
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
const vk::Format &format = renderer->getFormat(internalformat);
return getFormatSupportedCompressionRatesImpl(renderer, format, bufSize, rates);
}
void TextureVk::handleImmutableSamplerTransition(const vk::ImageHelper *previousImage,
const vk::ImageHelper *nextImage)
{
// Did the previous image have an immutable sampler
bool previousImageHadImmutableSampler =
previousImage && previousImage->valid() && previousImage->hasImmutableSampler();
// Does the next image require an immutable sampler?
bool nextImageRequiresImmutableSampler =
nextImage && nextImage->valid() && nextImage->hasImmutableSampler();
// Has the external format changed?
bool externalFormatChanged = false;
if (previousImageHadImmutableSampler && nextImageRequiresImmutableSampler)
{
externalFormatChanged =
previousImage->getExternalFormat() != nextImage->getExternalFormat();
}
// Handle transition of immutable sampler state
if ((previousImageHadImmutableSampler != nextImageRequiresImmutableSampler) ||
externalFormatChanged)
{
// The immutable sampler state is dirty.
resetSampler();
mImmutableSamplerDirty = true;
}
}
angle::Result TextureVk::setEGLImageTarget(const gl::Context *context,
gl::TextureType type,
egl::Image *image)
{
ContextVk *contextVk = vk::GetImpl(context);
ImageVk *imageVk = vk::GetImpl(image);
// Early out if we are creating TextureVk with the exact same eglImage and target/face/level to
// avoid unnecessarily dirty the state and allocating new ImageViews etc.
if (mImage == imageVk->getImage() && mEGLImageNativeType == imageVk->getImageTextureType() &&
static_cast<GLint>(mEGLImageLevelOffset) == imageVk->getImageLevel().get() &&
mEGLImageLayerOffset == imageVk->getImageLayer())
{
return angle::Result::Continue;
}
ANGLE_TRY(contextVk->getShareGroup()->lockDefaultContextsPriority(contextVk));
// TODO: Textures other than EGLImage targets can have immutable samplers.
// http://anglebug.com/42264309
handleImmutableSamplerTransition(mImage, imageVk ? imageVk->getImage() : nullptr);
releaseAndDeleteImageAndViews(contextVk);
UniqueSerial siblingSerial = imageVk->generateSiblingSerial();
setImageHelper(contextVk, imageVk->getImage(), imageVk->getImageTextureType(),
imageVk->getImageLevel().get(), imageVk->getImageLayer(), false, siblingSerial);
// Update ImageViewHelper's colorspace related state
EGLenum imageColorspaceAttribute = image->getColorspaceAttribute();
if (imageColorspaceAttribute != EGL_GL_COLORSPACE_DEFAULT_EXT)
{
egl::ImageColorspace imageColorspace =
(imageColorspaceAttribute == EGL_GL_COLORSPACE_SRGB_KHR) ? egl::ImageColorspace::SRGB
: egl::ImageColorspace::Linear;
ASSERT(mImage != nullptr);
mImageView.updateEglImageColorspace(*mImage, imageColorspace);
}
ANGLE_TRY(initImageViews(contextVk, getImageViewLevelCount()));
return angle::Result::Continue;
}
angle::Result TextureVk::setImageExternal(const gl::Context *context,
gl::TextureType type,
egl::Stream *stream,
const egl::Stream::GLTextureDescription &desc)
{
ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
return angle::Result::Stop;
}
angle::Result TextureVk::setBuffer(const gl::Context *context, GLenum internalFormat)
{
// No longer an image
releaseAndDeleteImageAndViews(vk::GetImpl(context));
resetSampler();
// There's nothing else to do here.
return angle::Result::Continue;
}
gl::ImageIndex TextureVk::getNativeImageIndex(const gl::ImageIndex &inputImageIndex) const
{
if (mEGLImageNativeType == gl::TextureType::InvalidEnum)
{
return inputImageIndex;
}
// inputImageIndex can point to a specific layer, but only for non-2D textures.
// mEGLImageNativeType can be a valid type, but only for 2D textures.
// As such, both of these cannot be true at the same time.
ASSERT(!inputImageIndex.hasLayer() && inputImageIndex.getLevelIndex() == 0);
return gl::ImageIndex::MakeFromType(mEGLImageNativeType, mEGLImageLevelOffset,
mEGLImageLayerOffset);
}
gl::LevelIndex TextureVk::getNativeImageLevel(gl::LevelIndex frontendLevel) const
{
ASSERT(frontendLevel.get() == 0 || mEGLImageLevelOffset == 0);
return frontendLevel + mEGLImageLevelOffset;
}
uint32_t TextureVk::getNativeImageLayer(uint32_t frontendLayer) const
{
ASSERT(frontendLayer == 0 || mEGLImageLayerOffset == 0);
return frontendLayer + mEGLImageLayerOffset;
}
void TextureVk::releaseAndDeleteImageAndViews(ContextVk *contextVk)
{
if (mImage)
{
if (mOwnsImage)
{
releaseStagedUpdates(contextVk);
}
releaseImage(contextVk);
mImageObserverBinding.bind(nullptr);
mRequiresMutableStorage = false;
mRequiredImageAccess = vk::ImageAccess::SampleOnly;
mImageCreateFlags = 0;
SafeDelete(mImage);
}
if (!contextVk->hasDisplayTextureShareGroup())
{
contextVk->getShareGroup()->onTextureRelease(this);
}
if (getBuffer().get() != nullptr)
{
mBufferContentsObservers->disableForBuffer(getBuffer().get());
}
if (mBufferViews.isInitialized())
{
mBufferViews.release(contextVk);
onStateChange(angle::SubjectMessage::SubjectChanged);
}
mRedefinedLevels = {};
mDescriptorSetCacheManager.releaseKeys(contextVk->getRenderer());
}
void TextureVk::initImageUsageFlags(ContextVk *contextVk, angle::FormatID actualFormatID)
{
ASSERT(actualFormatID != angle::FormatID::NONE);
mImageUsageFlags = kTransferImageFlags | VK_IMAGE_USAGE_SAMPLED_BIT;
// If the image has depth/stencil support, add those as possible usage.
vk::Renderer *renderer = contextVk->getRenderer();
if (angle::Format::Get(actualFormatID).hasDepthOrStencilBits())
{
// Work around a bug in the Mock ICD:
// https://github.com/KhronosGroup/Vulkan-Tools/issues/445
if (renderer->hasImageFormatFeatureBits(actualFormatID,
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))
{
mImageUsageFlags |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
if (renderer->getFeatures().supportsShaderFramebufferFetchDepthStencil.enabled)
{
mImageUsageFlags |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
}
}
}
else if (renderer->hasImageFormatFeatureBits(actualFormatID,
VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))
{
mImageUsageFlags |= kColorAttachmentImageFlags;
}
}
angle::Result TextureVk::ensureImageAllocated(ContextVk *contextVk, const vk::Format &format)
{
if (mImage == nullptr)
{
setImageHelper(contextVk, new vk::ImageHelper(), gl::TextureType::InvalidEnum, 0, 0, true,
{});
}
initImageUsageFlags(contextVk, format.getActualImageFormatID(getRequiredImageAccess()));
return angle::Result::Continue;
}
void TextureVk::setImageHelper(ContextVk *contextVk,
vk::ImageHelper *imageHelper,
gl::TextureType eglImageNativeType,
uint32_t imageLevelOffset,
uint32_t imageLayerOffset,
bool selfOwned,
UniqueSerial siblingSerial)
{
ASSERT(mImage == nullptr);
mImageObserverBinding.bind(imageHelper);
ASSERT(selfOwned == !siblingSerial.valid());
mOwnsImage = selfOwned;
mImageSiblingSerial = siblingSerial;
// If image is shared between other container objects, force it to renderable format since we
// don't know if other container object will render or not.
if (!mOwnsImage && !imageHelper->isBackedByExternalMemory())
{
mRequiredImageAccess = vk::ImageAccess::Renderable;
}
mEGLImageNativeType = eglImageNativeType;
mEGLImageLevelOffset = imageLevelOffset;
mEGLImageLayerOffset = imageLayerOffset;
mImage = imageHelper;
// All render targets must be already destroyed prior to this call.
for (auto &renderTargets : mSingleLayerRenderTargets)
{
ASSERT(renderTargets.empty());
}
ASSERT(mMultiLayerRenderTargets.empty());
if (!selfOwned)
{
// (!selfOwned) implies that the texture is a target sibling.
// Inherit a few VkImage's create attributes from ImageHelper.
mImageCreateFlags = mImage->getCreateFlags();
mImageUsageFlags = mImage->getUsage();
mRequiresMutableStorage = (mImageCreateFlags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) != 0;
}
vk::Renderer *renderer = contextVk->getRenderer();
getImageViews().init(renderer);
}
angle::Result TextureVk::redefineLevel(const gl::Context *context,
const gl::ImageIndex &index,
const vk::Format &format,
const gl::Extents &size)
{
ContextVk *contextVk = vk::GetImpl(context);
if (!mOwnsImage)
{
releaseAndDeleteImageAndViews(contextVk);
}
if (mImage != nullptr)
{
// If there are any staged changes for this index, we can remove them since we're going to
// override them with this call.
gl::LevelIndex levelIndexGL(index.getLevelIndex());
const uint32_t layerIndex = index.hasLayer() ? index.getLayerIndex() : 0;
if (gl::IsArrayTextureType(index.getType()))
{
// A multi-layer texture is being redefined, remove all updates to this level; the
// number of layers may have changed.
mImage->removeStagedUpdates(contextVk, levelIndexGL, levelIndexGL);
}
else
{
// Otherwise remove only updates to this layer. For example, cube map updates can be
// done through glTexImage2D, one per cube face (i.e. layer) and so should not remove
// updates to the other layers.
ASSERT(index.getLayerCount() == 1);
mImage->removeSingleSubresourceStagedUpdates(contextVk, levelIndexGL, layerIndex,
index.getLayerCount());
}
if (mImage->valid())
{
TextureLevelAllocation levelAllocation =
IsTextureLevelInAllocatedImage(*mImage, levelIndexGL)
? TextureLevelAllocation::WithinAllocatedImage
: TextureLevelAllocation::OutsideAllocatedImage;
TextureLevelDefinition levelDefinition =
IsTextureLevelDefinitionCompatibleWithImage(
*mImage, levelIndexGL, size, format.getIntendedFormatID(),
format.getActualImageFormatID(getRequiredImageAccess()))
? TextureLevelDefinition::Compatible
: TextureLevelDefinition::Incompatible;
if (TextureRedefineLevel(levelAllocation, levelDefinition, mState.getImmutableFormat(),
mImage->getLevelCount(), layerIndex, index,
mImage->getFirstAllocatedLevel(), &mRedefinedLevels))
{
releaseImage(contextVk);
}
}
}
// If image is not released due to an out-of-range or incompatible level definition, the image
// is still valid and we shouldn't redefine it to use the new format. In that case,
// ensureImageAllocated will only use the format to update the staging buffer's alignment to
// support both the previous and the new formats.
ANGLE_TRY(ensureImageAllocated(contextVk, format));
return angle::Result::Continue;
}
angle::Result TextureVk::copyImageDataToBufferAndGetData(ContextVk *contextVk,
gl::LevelIndex sourceLevelGL,
uint32_t layerCount,
const gl::Box &sourceArea,
RenderPassClosureReason reason,
vk::BufferHelper *copyBuffer,
uint8_t **outDataPtr)
{
ANGLE_TRACE_EVENT0("gpu.angle", "TextureVk::copyImageDataToBufferAndGetData");
// Make sure the source is initialized and it's images are flushed.
ANGLE_TRY(ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
gl::Box modifiedSourceArea = sourceArea;
bool is3D = mImage->getExtents().depth > 1;
if (is3D)
{
layerCount = 1;
}
else
{
modifiedSourceArea.depth = 1;
}
ANGLE_TRY(mImage->copyImageDataToBuffer(contextVk, sourceLevelGL, layerCount, 0,
modifiedSourceArea, copyBuffer, outDataPtr));
// Explicitly finish. If new use cases arise where we don't want to block we can change this.
ANGLE_TRY(contextVk->finishImpl(reason));
// invalidate must be called after wait for finish.
ANGLE_TRY(copyBuffer->invalidate(contextVk->getRenderer()));
return angle::Result::Continue;
}
angle::Result TextureVk::copyBufferDataToImage(ContextVk *contextVk,
vk::BufferHelper *srcBuffer,
const gl::ImageIndex index,
uint32_t rowLength,
uint32_t imageHeight,
const gl::Box &sourceArea,
size_t offset,
VkImageAspectFlags aspectFlags)
{
ANGLE_TRACE_EVENT0("gpu.angle", "TextureVk::copyBufferDataToImage");
// Vulkan Spec requires the bufferOffset to be a multiple of pixel size for
// vkCmdCopyBufferToImage.
ASSERT((offset % vk::GetImageCopyBufferAlignment(mImage->getActualFormatID())) == 0);
gl::LevelIndex level = gl::LevelIndex(index.getLevelIndex());
GLuint layerCount = index.getLayerCount();
GLuint layerIndex = 0;
ASSERT((aspectFlags & kDepthStencilAspects) != kDepthStencilAspects);
VkBufferImageCopy region = {};
region.bufferOffset = offset;
region.bufferRowLength = rowLength;
region.bufferImageHeight = imageHeight;
region.imageExtent.width = sourceArea.width;
region.imageExtent.height = sourceArea.height;
region.imageExtent.depth = sourceArea.depth;
region.imageOffset.x = sourceArea.x;
region.imageOffset.y = sourceArea.y;
region.imageOffset.z = sourceArea.z;
region.imageSubresource.aspectMask = aspectFlags;
region.imageSubresource.layerCount = layerCount;
region.imageSubresource.mipLevel = mImage->toVkLevel(level).get();
if (gl::IsArrayTextureType(index.getType()))
{
layerIndex = sourceArea.z;
region.imageOffset.z = 0;
region.imageExtent.depth = 1;
}
else if (index.getType() == gl::TextureType::CubeMap)
{
// Copy to the correct cube map face.
layerIndex = index.getLayerIndex();
}
region.imageSubresource.baseArrayLayer = layerIndex;
// Make sure the source is initialized and its images are flushed.
ANGLE_TRY(ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
vk::CommandBufferAccess access;
access.onBufferTransferRead(srcBuffer);
access.onImageTransferWrite(level, 1, layerIndex, layerCount, mImage->getAspectFlags(), mImage);
vk::OutsideRenderPassCommandBuffer *commandBuffer;
ANGLE_TRY(contextVk->getOutsideRenderPassCommandBuffer(access, &commandBuffer));
commandBuffer->copyBufferToImage(srcBuffer->getBuffer().getHandle(), mImage->getImage(),
mImage->getCurrentLayout(contextVk->getRenderer()), 1,
®ion);
return angle::Result::Continue;
}
angle::Result TextureVk::generateMipmapsWithCompute(ContextVk *contextVk)
{
vk::Renderer *renderer = contextVk->getRenderer();
// Requires that the image:
//
// - is not sRGB
// - is not integer
// - is 2D or 2D array
// - is single sample
// - is color image
//
// Support for the first two can be added easily. Supporting 3D textures, MSAA and
// depth/stencil would be more involved.
ASSERT(!mImage->getActualFormat().isSRGB);
ASSERT(!mImage->getActualFormat().isInt());
ASSERT(mImage->getType() == VK_IMAGE_TYPE_2D);
ASSERT(mImage->getSamples() == 1);
ASSERT(mImage->getAspectFlags() == VK_IMAGE_ASPECT_COLOR_BIT);
// Create the appropriate sampler.
GLenum filter = CalculateGenerateMipmapFilter(contextVk, mImage->getActualFormatID());
gl::SamplerState samplerState;
samplerState.setMinFilter(filter);
samplerState.setMagFilter(filter);
samplerState.setWrapS(GL_CLAMP_TO_EDGE);
samplerState.setWrapT(GL_CLAMP_TO_EDGE);
samplerState.setWrapR(GL_CLAMP_TO_EDGE);
vk::SharedSamplerPtr sampler;
vk::SamplerDesc samplerDesc(contextVk, samplerState, false, nullptr,
static_cast<angle::FormatID>(0));
ANGLE_TRY(renderer->getSamplerCache().getSampler(contextVk, samplerDesc, &sampler));
// If the image has more levels than supported, generate as many mips as possible at a time.
const vk::LevelIndex maxGenerateLevels(UtilsVk::GetGenerateMipmapMaxLevels(contextVk));
vk::LevelIndex dstMaxLevelVk = mImage->toVkLevel(gl::LevelIndex(mState.getMipmapMaxLevel()));
for (vk::LevelIndex dstBaseLevelVk =
mImage->toVkLevel(gl::LevelIndex(mState.getEffectiveBaseLevel() + 1));
dstBaseLevelVk <= dstMaxLevelVk; dstBaseLevelVk = dstBaseLevelVk + maxGenerateLevels.get())
{
vk::CommandBufferAccess access;
// For mipmap generation, we should make sure that there is no pending write for the source
// mip level. If there is, a barrier should be inserted before the source mip being used.
const vk::LevelIndex srcLevelVk = dstBaseLevelVk - 1;
uint32_t writeLevelCount =
std::min(maxGenerateLevels.get(), dstMaxLevelVk.get() + 1 - dstBaseLevelVk.get());
access.onImageComputeMipmapGenerationRead(mImage->toGLLevel(srcLevelVk), 1, 0,
mImage->getLayerCount(),
VK_IMAGE_ASPECT_COLOR_BIT, mImage);
access.onImageComputeShaderWrite(mImage->toGLLevel(dstBaseLevelVk), writeLevelCount, 0,
mImage->getLayerCount(), VK_IMAGE_ASPECT_COLOR_BIT,
mImage);
vk::OutsideRenderPassCommandBuffer *commandBuffer;
ANGLE_TRY(contextVk->getOutsideRenderPassCommandBuffer(access, &commandBuffer));
// Generate mipmaps for every layer separately.
for (uint32_t layer = 0; layer < mImage->getLayerCount(); ++layer)
{
// Create the necessary views.
const vk::ImageView *srcView = nullptr;
UtilsVk::GenerateMipmapDestLevelViews destLevelViews = {};
ANGLE_TRY(getImageViews().getLevelLayerDrawImageView(contextVk, *mImage, srcLevelVk,
layer, &srcView));
vk::LevelIndex dstLevelCount = maxGenerateLevels;
for (vk::LevelIndex levelVk(0); levelVk < maxGenerateLevels; ++levelVk)
{
vk::LevelIndex dstLevelVk = dstBaseLevelVk + levelVk.get();
// If fewer levels left than maxGenerateLevels, cut the loop short.
if (dstLevelVk > dstMaxLevelVk)
{
dstLevelCount = levelVk;
break;
}
ANGLE_TRY(getImageViews().getLevelLayerDrawImageView(
contextVk, *mImage, dstLevelVk, layer, &destLevelViews[levelVk.get()]));
}
// If the image has fewer than maximum levels, fill the last views with a unused view.
ASSERT(dstLevelCount > vk::LevelIndex(0));
for (vk::LevelIndex levelVk = dstLevelCount;
levelVk < vk::LevelIndex(UtilsVk::kGenerateMipmapMaxLevels); ++levelVk)
{
destLevelViews[levelVk.get()] = destLevelViews[levelVk.get() - 1];
}
// Generate mipmaps.
UtilsVk::GenerateMipmapParameters params = {};
params.srcLevel = srcLevelVk.get();
params.dstLevelCount = dstLevelCount.get();
ANGLE_TRY(contextVk->getUtils().generateMipmap(contextVk, mImage, srcView, mImage,
destLevelViews, sampler->get(), params));
}
}
contextVk->trackImageWithOutsideRenderPassEvent(mImage);
return angle::Result::Continue;
}
angle::Result TextureVk::generateMipmapsWithCPU(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
gl::LevelIndex baseLevelGL(mState.getEffectiveBaseLevel());
vk::LevelIndex baseLevelVk = mImage->toVkLevel(baseLevelGL);
const gl::Extents baseLevelExtents = mImage->getLevelExtents(baseLevelVk);
uint32_t imageLayerCount = mImage->getLayerCount();
uint8_t *imageData = nullptr;
gl::Box imageArea(0, 0, 0, baseLevelExtents.width, baseLevelExtents.height,
baseLevelExtents.depth);
vk::RendererScoped<vk::BufferHelper> bufferHelper(contextVk->getRenderer());
ANGLE_TRY(copyImageDataToBufferAndGetData(contextVk, baseLevelGL, imageLayerCount, imageArea,
RenderPassClosureReason::GenerateMipmapOnCPU,
&bufferHelper.get(), &imageData));
const angle::Format &angleFormat = mImage->getActualFormat();
GLuint sourceRowPitch = baseLevelExtents.width * angleFormat.pixelBytes;
GLuint sourceDepthPitch = sourceRowPitch * baseLevelExtents.height;
size_t baseLevelAllocationSize = sourceDepthPitch * baseLevelExtents.depth;
// We now have the base level available to be manipulated in the imageData pointer. Generate all
// the missing mipmaps with the slow path. For each layer, use the copied data to generate all
// the mips.
for (GLuint layer = 0; layer < imageLayerCount; layer++)
{
size_t bufferOffset = layer * baseLevelAllocationSize;
ANGLE_TRY(generateMipmapLevelsWithCPU(contextVk, angleFormat, layer, baseLevelGL + 1,
gl::LevelIndex(mState.getMipmapMaxLevel()),
baseLevelExtents.width, baseLevelExtents.height,
baseLevelExtents.depth, sourceRowPitch,
sourceDepthPitch, imageData + bufferOffset));
}
ASSERT(!TextureHasAnyRedefinedLevels(mRedefinedLevels));
return flushImageStagedUpdates(contextVk);
}
angle::Result TextureVk::generateMipmap(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
// The image should already be allocated by a prior syncState.
ASSERT(mImage->valid());
// If base level has changed, the front-end should have called syncState already.
ASSERT(mState.getImmutableFormat() ||
mImage->getFirstAllocatedLevel() == gl::LevelIndex(mState.getEffectiveBaseLevel()));
// Only staged update here is the robust resource init if any.
ANGLE_TRY(ensureImageInitialized(contextVk, ImageMipLevels::FullMipChainForGenerateMipmap));
vk::LevelIndex baseLevel = mImage->toVkLevel(gl::LevelIndex(mState.getEffectiveBaseLevel()));
vk::LevelIndex maxLevel = mImage->toVkLevel(gl::LevelIndex(mState.getMipmapMaxLevel()));
ASSERT(maxLevel != vk::LevelIndex(0));
if (getImageViews().hasColorspaceOverrideForWrite(*mImage))
{
angle::FormatID actualFormatID =
getImageViews().getColorspaceOverrideFormatForWrite(mImage->getActualFormatID());
return contextVk->getUtils().generateMipmapWithDraw(
contextVk, mImage, actualFormatID,
gl::IsMipmapFiltered(mState.getSamplerState().getMinFilter()));
}
// If it's possible to generate mipmap in compute, that would give the best possible
// performance on some hardware.
if (CanGenerateMipmapWithCompute(renderer, mImage->getType(), mImage->getActualFormatID(),
mImage->getSamples(), mOwnsImage))
{
ASSERT((mImageUsageFlags & VK_IMAGE_USAGE_STORAGE_BIT) != 0);
return generateMipmapsWithCompute(contextVk);
}
else if (renderer->hasImageFormatFeatureBits(mImage->getActualFormatID(), kBlitFeatureFlags))
{
// Otherwise, use blit if possible.
return mImage->generateMipmapsWithBlit(contextVk, baseLevel, maxLevel);
}
ANGLE_VK_PERF_WARNING(contextVk, GL_DEBUG_SEVERITY_HIGH,
"Mipmap generated on CPU due to format restrictions");
// If not possible to generate mipmaps on the GPU, do it on the CPU for conformance.
return generateMipmapsWithCPU(context);
}
angle::Result TextureVk::setBaseLevel(const gl::Context *context, GLuint baseLevel)
{
return angle::Result::Continue;
}
angle::Result TextureVk::maybeUpdateBaseMaxLevels(ContextVk *contextVk,
TextureUpdateResult *updateResultOut)
{
if (!mImage)
{
return angle::Result::Continue;
}
bool baseLevelChanged = mCurrentBaseLevel.get() != static_cast<GLint>(mState.getBaseLevel());
bool maxLevelChanged = mCurrentMaxLevel.get() != static_cast<GLint>(mState.getMaxLevel());
if (!maxLevelChanged && !baseLevelChanged)
{
return angle::Result::Continue;
}
gl::LevelIndex newBaseLevel = gl::LevelIndex(mState.getEffectiveBaseLevel());
gl::LevelIndex newMaxLevel = gl::LevelIndex(mState.getEffectiveMaxLevel());
ASSERT(newBaseLevel <= newMaxLevel);
if (!mImage->valid())
{
// No further work to do, let staged updates handle the new levels
return angle::Result::Continue;
}
if (mState.getImmutableFormat())
{
// For immutable texture, baseLevel/maxLevel should be a subset of the texture's actual
// number of mip levels. We don't need to respecify an image.
ASSERT(!baseLevelChanged || newBaseLevel >= mImage->getFirstAllocatedLevel());
ASSERT(!maxLevelChanged || newMaxLevel < gl::LevelIndex(mImage->getLevelCount()));
}
else if (!baseLevelChanged && (newMaxLevel <= mImage->getLastAllocatedLevel()))
{
// With a valid image, check if only changing the maxLevel to a subset of the texture's
// actual number of mip levels
ASSERT(maxLevelChanged);
}
else
{
*updateResultOut = TextureUpdateResult::ImageRespecified;
return respecifyImageStorage(contextVk);
}
// Don't need to respecify the texture; but do need to update which vkImageView's are served up
// by ImageViewHelper
// Update the current max level in ImageViewHelper
ANGLE_TRY(initImageViews(contextVk, newMaxLevel - newBaseLevel + 1));
mCurrentBaseLevel = newBaseLevel;
mCurrentMaxLevel = newMaxLevel;
return angle::Result::Continue;
}
angle::Result TextureVk::copyAndStageImageData(ContextVk *contextVk,
gl::LevelIndex previousFirstAllocateLevel,
vk::ImageHelper *srcImage,
vk::ImageHelper *dstImage)
{
// Preserve the data in the Vulkan image. GL texture's staged updates that correspond to
// levels outside the range of the Vulkan image will remain intact.
vk::Renderer *renderer = contextVk->getRenderer();
// This path is only called when switching from !owned to owned, in which case if any level was
// redefined it's already released and deleted by TextureVk::redefineLevel().
ASSERT(!TextureHasAnyRedefinedLevels(mRedefinedLevels));
// Create a temp copy of srcImage for staging.
std::unique_ptr<vk::RefCounted<vk::ImageHelper>> stagingImage;
stagingImage = std::make_unique<vk::RefCounted<vk::ImageHelper>>();
const uint32_t levelCount = srcImage->getLevelCount();
const uint32_t layerCount = srcImage->getLayerCount();
ANGLE_TRY(stagingImage->get().initStaging(
contextVk, mState.hasProtectedContent(), renderer->getMemoryProperties(),
srcImage->getType(), srcImage->getExtents(), srcImage->getIntendedFormatID(),
srcImage->getActualFormatID(), srcImage->getSamples(), kTransferImageFlags, levelCount,
layerCount));
// Copy the src image wholly into the staging image
const VkImageAspectFlags aspectFlags = srcImage->getAspectFlags();
vk::CommandBufferAccess access;
access.onImageTransferWrite(gl::LevelIndex(0), levelCount, 0, layerCount, aspectFlags,
&stagingImage->get());
access.onImageTransferRead(aspectFlags, srcImage);
vk::OutsideRenderPassCommandBuffer *commandBuffer;
ANGLE_TRY(contextVk->getOutsideRenderPassCommandBuffer(access, &commandBuffer));
VkImageCopy copyRegion = {};
copyRegion.srcSubresource.aspectMask = aspectFlags;
copyRegion.srcSubresource.layerCount = layerCount;
copyRegion.dstSubresource = copyRegion.srcSubresource;
for (vk::LevelIndex levelVk(0); levelVk < vk::LevelIndex(levelCount); ++levelVk)
{
gl::Extents levelExtents = srcImage->getLevelExtents(levelVk);
copyRegion.srcSubresource.mipLevel = levelVk.get();
copyRegion.dstSubresource.mipLevel = levelVk.get();
gl_vk::GetExtent(levelExtents, ©Region.extent);
commandBuffer->copyImage(srcImage->getImage(), srcImage->getCurrentLayout(renderer),
stagingImage->get().getImage(),
stagingImage->get().getCurrentLayout(renderer), 1, ©Region);
}
// Stage the staging image in the destination
dstImage->stageSubresourceUpdatesFromAllImageLevels(stagingImage.release(),
previousFirstAllocateLevel);
return angle::Result::Continue;
}
angle::Result TextureVk::reinitImageAsRenderable(ContextVk *contextVk, const vk::Format &format)
{
ASSERT(mImage->valid());
vk::Renderer *renderer = contextVk->getRenderer();
const uint32_t levelCount = mImage->getLevelCount();
const uint32_t layerCount = mImage->getLayerCount();
// Make sure the source is initialized and its staged updates are flushed.
ANGLE_TRY(flushImageStagedUpdates(contextVk));
const angle::Format &srcFormat = mImage->getActualFormat();
const angle::Format &dstFormat = format.getActualImageFormat(getRequiredImageAccess());
// If layerCount or levelCount is bigger than 1, we go for the slow path for now. The problem
// with draw path is that in the multiple level/layer case, we have to do copy in a loop.
// Currently copySubImageImplWithDraw() calls ensureImageInitalized which forces flush out
// staged updates that we just staged inside the loop which is wrong.
if (levelCount == 1 && layerCount == 1 &&
!IsTextureLevelRedefined(mRedefinedLevels, mState.getType(),
mImage->getFirstAllocatedLevel()))
{
ANGLE_VK_PERF_WARNING(contextVk, GL_DEBUG_SEVERITY_LOW,
"Copying image data due to texture format fallback");
ASSERT(CanCopyWithDraw(renderer, mImage->getActualFormatID(), mImage->getTilingMode(),
format.getActualImageFormatID(getRequiredImageAccess()),
getTilingMode()));
vk::LevelIndex levelVk(0);
gl::LevelIndex sourceLevelGL = mImage->toGLLevel(levelVk);
gl::Box sourceBox(gl::kOffsetZero, mImage->getLevelExtents(levelVk));
const gl::ImageIndex index =
gl::ImageIndex::MakeFromType(mState.getType(), sourceLevelGL.get());
// Flush the render pass, which may incur a vkQueueSubmit, before taking any views.
// Otherwise the view serials would not reflect the render pass they are really used in.
// http://crbug.com/1272266#c22
ANGLE_TRY(
contextVk->flushCommandsAndEndRenderPass(RenderPassClosureReason::PrepareForImageCopy));
return copySubImageImplWithDraw(contextVk, index, gl::kOffsetZero, format, sourceLevelGL,
sourceBox, false, false, false, false, mImage,
&getCopyImageView(), SurfaceRotation::Identity);
}
for (vk::LevelIndex levelVk(0); levelVk < vk::LevelIndex(levelCount); ++levelVk)
{
gl::LevelIndex levelGL = mImage->toGLLevel(levelVk);
if (IsTextureLevelRedefined(mRedefinedLevels, mState.getType(), levelGL))
{
continue;
}
ANGLE_VK_PERF_WARNING(contextVk, GL_DEBUG_SEVERITY_HIGH,
"GPU stall due to texture format fallback");
gl::Box sourceBox(gl::kOffsetZero, mImage->getLevelExtents(levelVk));
// copy and stage entire layer
const gl::ImageIndex index =
gl::ImageIndex::MakeFromType(mState.getType(), levelGL.get(), 0, layerCount);
// Read back the requested region of the source texture
vk::RendererScoped<vk::BufferHelper> bufferHelper(renderer);
vk::BufferHelper *srcBuffer = &bufferHelper.get();
uint8_t *srcData = nullptr;
ANGLE_TRY(mImage->copyImageDataToBuffer(contextVk, levelGL, layerCount, 0, sourceBox,
srcBuffer, &srcData));
// Explicitly finish. If new use cases arise where we don't want to block we can change
// this.
ANGLE_TRY(contextVk->finishImpl(RenderPassClosureReason::TextureReformatToRenderable));
// invalidate must be called after wait for finish.
ANGLE_TRY(srcBuffer->invalidate(renderer));
size_t dstBufferSize = sourceBox.width * sourceBox.height * sourceBox.depth *
dstFormat.pixelBytes * layerCount;
// Allocate memory in the destination texture for the copy/conversion.
uint8_t *dstData = nullptr;
ANGLE_TRY(mImage->stageSubresourceUpdateAndGetData(
contextVk, dstBufferSize, index, mImage->getLevelExtents(levelVk), gl::kOffsetZero,
&dstData, dstFormat.id));
// Source and destination data is tightly packed
GLuint srcDataRowPitch = sourceBox.width * srcFormat.pixelBytes;
GLuint dstDataRowPitch = sourceBox.width * dstFormat.pixelBytes;
GLuint srcDataDepthPitch = srcDataRowPitch * sourceBox.height;
GLuint dstDataDepthPitch = dstDataRowPitch * sourceBox.height;
GLuint srcDataLayerPitch = srcDataDepthPitch * sourceBox.depth;
GLuint dstDataLayerPitch = dstDataDepthPitch * sourceBox.depth;
rx::PixelReadFunction pixelReadFunction = srcFormat.pixelReadFunction;
rx::PixelWriteFunction pixelWriteFunction = dstFormat.pixelWriteFunction;
const gl::InternalFormat &dstFormatInfo = *mState.getImageDesc(index).format.info;
for (uint32_t layer = 0; layer < layerCount; layer++)
{
CopyImageCHROMIUM(srcData + layer * srcDataLayerPitch, srcDataRowPitch,
srcFormat.pixelBytes, srcDataDepthPitch, pixelReadFunction,
dstData + layer * dstDataLayerPitch, dstDataRowPitch,
dstFormat.pixelBytes, dstDataDepthPitch, pixelWriteFunction,
dstFormatInfo.format, dstFormatInfo.componentType, sourceBox.width,
sourceBox.height, sourceBox.depth, false, false, false);
}
}
return angle::Result::Continue;
}
angle::Result TextureVk::respecifyImageStorage(ContextVk *contextVk)
{
if (!mImage->valid())
{
ASSERT(!TextureHasAnyRedefinedLevels(mRedefinedLevels));
return angle::Result::Continue;
}
// Recreate the image to reflect new base or max levels.
// First, flush any pending updates so we have good data in the current mImage
if (mImage->hasStagedUpdatesInAllocatedLevels())
{
ANGLE_TRY(flushImageStagedUpdates(contextVk));
}
if (!mOwnsImage)
{
// Cache values needed for copy and stage operations
vk::ImageHelper *srcImage = mImage;
const vk::Format &format = getBaseLevelFormat(contextVk->getRenderer());
// If any level was redefined but the image was not owned by the Texture, it's already
// released and deleted by TextureVk::redefineLevel().
ASSERT(!TextureHasAnyRedefinedLevels(mRedefinedLevels));
// Save previousFirstAllocateLevel before mImage becomes invalid
gl::LevelIndex previousFirstAllocateLevel = mImage->getFirstAllocatedLevel();
// If we didn't own the image, release the current and create a new one
releaseImage(contextVk);
// Create the image helper
ANGLE_TRY(ensureImageAllocated(contextVk, format));
ANGLE_TRY(initImage(contextVk, format.getIntendedFormatID(),
format.getActualImageFormatID(getRequiredImageAccess()),
mState.getImmutableFormat()
? ImageMipLevels::FullMipChainForGenerateMipmap
: ImageMipLevels::EnabledLevels));
// Make a copy of the old image (that's being released) and stage that as an update to the
// new image.
ANGLE_TRY(copyAndStageImageData(contextVk, previousFirstAllocateLevel, srcImage, mImage));
}
else
{
const vk::Format &format = getBaseLevelFormat(contextVk->getRenderer());
if (mImage->getActualFormatID() != format.getActualImageFormatID(getRequiredImageAccess()))
{
ANGLE_TRY(reinitImageAsRenderable(contextVk, format));
}
else
{
stageSelfAsSubresourceUpdates(contextVk);
}
// Release the current image so that it will be recreated with the correct number of mip
// levels, base level, and max level.
releaseImage(contextVk);
}
return angle::Result::Continue;
}
angle::Result TextureVk::bindTexImage(const gl::Context *context, egl::Surface *surface)
{
ContextVk *contextVk = vk::GetImpl(context);
releaseAndDeleteImageAndViews(contextVk);
// eglBindTexImage can only be called with pbuffer (offscreen) surfaces
OffscreenSurfaceVk *offscreenSurface = GetImplAs<OffscreenSurfaceVk>(surface);
// Surface can only have single target. Just generate valid serial with throw-away generator.
UniqueSerial siblingSerial = UniqueSerialFactory().generate();
setImageHelper(contextVk, offscreenSurface->getColorAttachmentImage(),
gl::TextureType::InvalidEnum, 0, 0, false, siblingSerial);
ASSERT(mImage->getLayerCount() == 1);
return initImageViews(contextVk, getImageViewLevelCount());
}
angle::Result TextureVk::releaseTexImage(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
releaseImage(contextVk);
return angle::Result::Continue;
}
angle::Result TextureVk::getAttachmentRenderTarget(const gl::Context *context,
GLenum binding,
const gl::ImageIndex &imageIndex,
GLsizei samples,
FramebufferAttachmentRenderTarget **rtOut)
{
GLint requestedLevel = imageIndex.getLevelIndex();
ASSERT(requestedLevel >= 0);
ContextVk *contextVk = vk::GetImpl(context);
// Sync the texture's image. See comment on this function in the header.
ANGLE_TRY(respecifyImageStorageIfNecessary(contextVk, gl::Command::Draw));
// Don't flush staged updates here. We'll handle that in FramebufferVk so we can defer clears.
if (!mImage->valid())
{
const vk::Format &format = getBaseLevelFormat(contextVk->getRenderer());
ANGLE_TRY(initImage(contextVk, format.getIntendedFormatID(),
format.getActualImageFormatID(getRequiredImageAccess()),
ImageMipLevels::EnabledLevels));
}
ANGLE_TRY(performImageQueueTransferIfNecessary(contextVk));
const bool hasRenderToTextureEXT =
contextVk->getFeatures().supportsMultisampledRenderToSingleSampled.enabled;
// If samples > 1 here, we have a singlesampled texture that's being multisampled rendered to.
// In this case, create a multisampled image that is otherwise identical to the single sampled
// image. That multisampled image is used as color or depth/stencil attachment, while the
// original image is used as the resolve attachment.
const gl::RenderToTextureImageIndex renderToTextureIndex =
hasRenderToTextureEXT
? gl::RenderToTextureImageIndex::Default
: static_cast<gl::RenderToTextureImageIndex>(PackSampleCount(samples));
if (samples > 1 && !hasRenderToTextureEXT)
{
// Initialize mMultisampledImages and mMultisampledImageViews if necessary
if (mMultisampledImages == nullptr)
{
mMultisampledImages = std::make_unique<MultiSampleImages>();
mMultisampledImageViews = std::make_unique<MultiSampleImageViews>();
}
ASSERT(mState.getBaseLevelDesc().samples <= 1);
vk::ImageHelper &multisampledImage =
mMultisampledImages->at(renderToTextureIndex)[requestedLevel];
if (!multisampledImage.valid())
{
// Ensure the view serial is valid.
vk::Renderer *renderer = contextVk->getRenderer();
mMultisampledImageViews->at(renderToTextureIndex)[requestedLevel].init(renderer);
// The MSAA image always comes from the single sampled one, so disable robust init.
bool useRobustInit = false;
// Calculate extents for multisample image
VkExtent3D extents = {};
gl_vk::GetExtent(
mImage->getLevelExtents(mImage->toVkLevel(gl::LevelIndex(requestedLevel))),
&extents);
// Create the implicit multisampled image.
ANGLE_TRY(multisampledImage.initImplicitMultisampledRenderToTexture(
contextVk, mState.hasProtectedContent(), renderer->getMemoryProperties(),
mState.getType(), samples, *mImage, extents, useRobustInit));
}
}
GLuint layerIndex = 0, layerCount = 0, imageLayerCount = 0;
GetRenderTargetLayerCountAndIndex(mImage, imageIndex, &layerIndex, &layerCount,
&imageLayerCount);
if (layerCount == 1)
{
initSingleLayerRenderTargets(contextVk, imageLayerCount, gl::LevelIndex(requestedLevel),
renderToTextureIndex);
std::vector<RenderTargetVector> &levelRenderTargets =
mSingleLayerRenderTargets[renderToTextureIndex];
ASSERT(requestedLevel < static_cast<int32_t>(levelRenderTargets.size()));
RenderTargetVector &layerRenderTargets = levelRenderTargets[requestedLevel];
ASSERT(imageIndex.getLayerIndex() < static_cast<int32_t>(layerRenderTargets.size()));
*rtOut = &layerRenderTargets[layerIndex];
}
else
{
ASSERT(layerCount > 0);
*rtOut = getMultiLayerRenderTarget(contextVk, gl::LevelIndex(imageIndex.getLevelIndex()),
layerIndex, layerCount);
}
return angle::Result::Continue;
}
angle::Result TextureVk::ensureImageInitialized(ContextVk *contextVk, ImageMipLevels mipLevels)
{
if (mImage->valid() && !mImage->hasStagedUpdatesInAllocatedLevels())
{
return angle::Result::Continue;
}
if (!mImage->valid())
{
ASSERT(!TextureHasAnyRedefinedLevels(mRedefinedLevels));
const vk::Format &format = getBaseLevelFormat(contextVk->getRenderer());
ANGLE_TRY(initImage(contextVk, format.getIntendedFormatID(),
format.getActualImageFormatID(getRequiredImageAccess()), mipLevels));
if (mipLevels == ImageMipLevels::FullMipChainForGenerateMipmap)
{
// Remove staged updates to non-base mips when generating mipmaps. These can only be
// emulated format init clears that are staged in initImage.
mImage->removeStagedUpdates(contextVk,
gl::LevelIndex(mState.getEffectiveBaseLevel() + 1),
gl::LevelIndex(mState.getMipmapMaxLevel()));
}
}
return flushImageStagedUpdates(contextVk);
}
angle::Result TextureVk::flushImageStagedUpdates(ContextVk *contextVk)
{
ASSERT(mImage->valid());
gl::LevelIndex firstLevelGL = getNativeImageLevel(mImage->getFirstAllocatedLevel());
uint32_t firstLayer = getNativeImageLayer(0);
return mImage->flushStagedUpdates(contextVk, firstLevelGL,
firstLevelGL + getImageViewLevelCount(), firstLayer,
firstLayer + getImageViewLayerCount(), mRedefinedLevels);
}
angle::Result TextureVk::performImageQueueTransferIfNecessary(ContextVk *contextVk)
{
if (mImage->valid() && mImage->isQueueFamilyChangeNeccesary(contextVk->getDeviceQueueIndex()))
{
vk::ImageLayout newLayout = vk::ImageLayout::AllGraphicsShadersWrite;
if (mImage->getUsage() & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
{
newLayout = vk::ImageLayout::ColorWrite;
}
else if (mImage->getUsage() & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)
{
newLayout = vk::ImageLayout::DepthWriteStencilWrite;
}
else if (mImage->getUsage() &
(VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT))
{
newLayout = vk::ImageLayout::AllGraphicsShadersReadOnly;
}
vk::OutsideRenderPassCommandBuffer *commandBuffer;
vk::CommandBufferAccess access;
access.onExternalAcquireRelease(mImage);
ANGLE_TRY(contextVk->getOutsideRenderPassCommandBuffer(access, &commandBuffer));
mImage->changeLayoutAndQueue(contextVk, mImage->getAspectFlags(), newLayout,
contextVk->getDeviceQueueIndex(), commandBuffer);
ANGLE_TRY(contextVk->onEGLImageQueueChange());
}
return angle::Result::Continue;
}
void TextureVk::initSingleLayerRenderTargets(ContextVk *contextVk,
GLuint layerCount,
gl::LevelIndex levelIndex,
gl::RenderToTextureImageIndex renderToTextureIndex)
{
GLint requestedLevel = levelIndex.get();
std::vector<RenderTargetVector> &allLevelsRenderTargets =
mSingleLayerRenderTargets[renderToTextureIndex];
if (allLevelsRenderTargets.size() <= static_cast<uint32_t>(requestedLevel))
{
allLevelsRenderTargets.resize(requestedLevel + 1);
}
RenderTargetVector &renderTargets = allLevelsRenderTargets[requestedLevel];
// Lazy init. Check if already initialized.
if (!renderTargets.empty())
{
return;
}
// There are |layerCount| render targets, one for each layer
renderTargets.resize(layerCount);
const bool isMultisampledRenderToTexture =
renderToTextureIndex != gl::RenderToTextureImageIndex::Default;
vk::ImageHelper *drawImage = mImage;
vk::ImageViewHelper *drawImageViews = &getImageViews();
vk::ImageHelper *resolveImage = nullptr;
vk::ImageViewHelper *resolveImageViews = nullptr;
RenderTargetTransience transience = RenderTargetTransience::Default;
// If multisampled render to texture, use the multisampled image as draw image instead, and
// resolve into the texture's image automatically.
if (isMultisampledRenderToTexture)
{
ASSERT(mMultisampledImages->at(renderToTextureIndex)[requestedLevel].valid());
ASSERT(!mImage->isYuvResolve());
resolveImage = drawImage;
resolveImageViews = drawImageViews;
drawImage = &mMultisampledImages->at(renderToTextureIndex)[requestedLevel];
drawImageViews = &mMultisampledImageViews->at(renderToTextureIndex)[requestedLevel];
// If the texture is depth/stencil, GL_EXT_multisampled_render_to_texture2 explicitly
// indicates that there is no need for the image to be resolved. In that case, mark the
// render target as entirely transient.
if (mImage->getAspectFlags() != VK_IMAGE_ASPECT_COLOR_BIT)
{
transience = RenderTargetTransience::EntirelyTransient;
}
else
{
transience = RenderTargetTransience::MultisampledTransient;
}
}
else if (mImage->isYuvResolve())
{
// If rendering to YUV, similar to multisampled render to texture
resolveImage = drawImage;
resolveImageViews = drawImageViews;
if (contextVk->getRenderer()->nullColorAttachmentWithExternalFormatResolve())
{
// If null color attachment, we still keep drawImage as is (the same as
// resolveImage) to avoid special treatment in many places where they assume there must
// be a color attachment if there is a resolve attachment. But when renderPass is
// created, color attachment will be ignored.
}
else
{
transience = RenderTargetTransience::YuvResolveTransient;
// Need to populate drawImage here; either abuse mMultisampledImages etc
// or build something parallel to it. we don't have a vulkan implementation which
// wants this path yet, though.
UNREACHABLE();
}
}
for (uint32_t layerIndex = 0; layerIndex < layerCount; ++layerIndex)
{
renderTargets[layerIndex].init(drawImage, drawImageViews, resolveImage, resolveImageViews,
mImageSiblingSerial, getNativeImageLevel(levelIndex),
getNativeImageLayer(layerIndex), 1, transience);
}
}
RenderTargetVk *TextureVk::getMultiLayerRenderTarget(ContextVk *contextVk,
gl::LevelIndex level,
GLuint layerIndex,
GLuint layerCount)
{
vk::ImageViewHelper *imageViews = &getImageViews();
vk::ImageSubresourceRange range = imageViews->getSubresourceDrawRange(
level, layerIndex, vk::GetLayerMode(*mImage, layerCount));
auto iter = mMultiLayerRenderTargets.find(range);
if (iter != mMultiLayerRenderTargets.end())
{
return iter->second.get();
}
// Create the layered render target. Note that multisampled render to texture is not
// allowed with layered render targets; nor is YUV rendering.
std::unique_ptr<RenderTargetVk> &rt = mMultiLayerRenderTargets[range];
if (!rt)
{
rt = std::make_unique<RenderTargetVk>();
}
rt->init(mImage, imageViews, nullptr, nullptr, mImageSiblingSerial, getNativeImageLevel(level),
getNativeImageLayer(layerIndex), layerCount, RenderTargetTransience::Default);
return rt.get();
}
void TextureVk::prepareForGenerateMipmap(ContextVk *contextVk)
{
gl::LevelIndex baseLevel(mState.getEffectiveBaseLevel());
gl::LevelIndex maxLevel(mState.getMipmapMaxLevel());
// Remove staged updates to the range that's being respecified (which is all the mips except
// baseLevel).
gl::LevelIndex firstGeneratedLevel = baseLevel + 1;
mImage->removeStagedUpdates(contextVk, firstGeneratedLevel, maxLevel);
TextureRedefineGenerateMipmapLevels(baseLevel, maxLevel, firstGeneratedLevel,
&mRedefinedLevels);
// If generating mipmap and base level is incompatibly redefined, the image is going to be
// recreated. Don't try to preserve the other mips.
if (IsTextureLevelRedefined(mRedefinedLevels, mState.getType(), baseLevel))
{
ASSERT(!mState.getImmutableFormat());
releaseImage(contextVk);
}
const gl::ImageDesc &baseLevelDesc = mState.getBaseLevelDesc();
VkImageType imageType = gl_vk::GetImageType(mState.getType());
const vk::Format &format = getBaseLevelFormat(contextVk->getRenderer());
const GLint samples = baseLevelDesc.samples ? baseLevelDesc.samples : 1;
// If the compute path is to be used to generate mipmaps, add the STORAGE usage.
if (CanGenerateMipmapWithCompute(contextVk->getRenderer(), imageType,
format.getActualImageFormatID(getRequiredImageAccess()),
samples, mOwnsImage))
{
mImageUsageFlags |= VK_IMAGE_USAGE_STORAGE_BIT;
}
}
angle::Result TextureVk::respecifyImageStorageIfNecessary(ContextVk *contextVk, gl::Command source)
{
ASSERT(mState.getBuffer().get() == nullptr);
VkImageUsageFlags oldUsageFlags = mImageUsageFlags;
VkImageCreateFlags oldCreateFlags = mImageCreateFlags;
// Create a new image if the storage state is enabled for the first time.
if (mState.hasBeenBoundAsImage())
{
mImageUsageFlags |= VK_IMAGE_USAGE_STORAGE_BIT;
mRequiresMutableStorage = true;
}
// If we're handling dirty srgb decode/override state, we may have to reallocate the image with
// VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT. Vulkan requires this bit to be set in order to use
// imageviews with a format that does not match the texture's internal format.
if (isSRGBOverrideEnabled())
{
mRequiresMutableStorage = true;
}
if (mRequiresMutableStorage)
{
mImageCreateFlags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
}
// Create a new image if used as attachment for the first time. This must be called before
// prepareForGenerateMipmap since this changes the format which prepareForGenerateMipmap relies
// on.
if (mState.hasBeenBoundAsAttachment())
{
TextureUpdateResult updateResult = TextureUpdateResult::ImageUnaffected;
ANGLE_TRY(ensureRenderable(contextVk, &updateResult));
if (updateResult == TextureUpdateResult::ImageRespecified)
{
oldUsageFlags = mImageUsageFlags;
oldCreateFlags = mImageCreateFlags;
}
}
// Before redefining the image for any reason, check to see if it's about to go through mipmap
// generation. In that case, drop every staged change for the subsequent mips after base, and
// make sure the image is created with the complete mip chain.
const bool isGenerateMipmap = source == gl::Command::GenerateMipmap;
if (isGenerateMipmap)
{
prepareForGenerateMipmap(contextVk);
}
// If texture was not originally created using the MSRTSS flag, it should be recreated when it
// is bound to an MSRTT framebuffer.
if (contextVk->getFeatures().supportsMultisampledRenderToSingleSampled.enabled &&
!contextVk->getFeatures().preferMSRTSSFlagByDefault.enabled &&
mState.hasBeenBoundToMSRTTFramebuffer() &&
((mImageCreateFlags & VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT) == 0))
{
ANGLE_TRY(respecifyImageStorage(contextVk));
oldUsageFlags = mImageUsageFlags;
oldCreateFlags = mImageCreateFlags;
}
// For immutable texture, base level does not affect allocation. Only usage flags are. If usage
// flag changed, we respecify image storage early on. This makes the code more reliable and also
// better performance wise. Otherwise, we will try to preserve base level by calling
// stageSelfAsSubresourceUpdates and then later on find out the mImageUsageFlags changed and the
// whole thing has to be respecified.
if (mState.getImmutableFormat() &&
(oldUsageFlags != mImageUsageFlags || oldCreateFlags != mImageCreateFlags))
{
ANGLE_TRY(respecifyImageStorage(contextVk));
oldUsageFlags = mImageUsageFlags;
oldCreateFlags = mImageCreateFlags;
}
// Set base and max level before initializing the image
TextureUpdateResult updateResult = TextureUpdateResult::ImageUnaffected;
ANGLE_TRY(maybeUpdateBaseMaxLevels(contextVk, &updateResult));
// Updating levels could have respecified the storage, recapture mImageCreateFlags
if (updateResult == TextureUpdateResult::ImageRespecified)
{
oldUsageFlags = mImageUsageFlags;
oldCreateFlags = mImageCreateFlags;
}
// It is possible for the image to have a single level (because it doesn't use mipmapping),
// then have more levels defined in it and mipmapping enabled. In that case, the image needs
// to be recreated.
bool isMipmapEnabledByMinFilter = false;
if (!isGenerateMipmap && mImage && mImage->valid())
{
isMipmapEnabledByMinFilter =
mImage->getLevelCount() < getMipLevelCount(ImageMipLevels::EnabledLevels);
}
// If generating mipmaps and the image needs to be recreated (not full-mip already, or changed
// usage flags), make sure it's recreated.
if (isGenerateMipmap && mImage && mImage->valid() &&
(oldUsageFlags != mImageUsageFlags ||
(!mState.getImmutableFormat() &&
mImage->getLevelCount() !=
getMipLevelCount(ImageMipLevels::FullMipChainForGenerateMipmap))))
{
ASSERT(mOwnsImage);
// Immutable texture is not expected to reach here. The usage flag change should have
// been handled earlier and level count change should not need to reallocate
ASSERT(!mState.getImmutableFormat());
// Flush staged updates to the base level of the image. Note that updates to the rest of
// the levels have already been discarded through the |removeStagedUpdates| call above.
ANGLE_TRY(flushImageStagedUpdates(contextVk));
stageSelfAsSubresourceUpdates(contextVk);
// Release the mImage without collecting garbage from image views.
releaseImage(contextVk);
}
// Respecify the image if it's changed in usage, or if any of its levels are redefined and no
// update to base/max levels were done (otherwise the above call would have already taken care
// of this). Note that if both base/max and image usage are changed, the image is recreated
// twice, which incurs unnecessary copies. This is not expected to be happening in real
// applications.
if (oldUsageFlags != mImageUsageFlags || oldCreateFlags != mImageCreateFlags ||
TextureHasAnyRedefinedLevels(mRedefinedLevels) || isMipmapEnabledByMinFilter)
{
ANGLE_TRY(respecifyImageStorage(contextVk));
}
return angle::Result::Continue;
}
angle::Result TextureVk::onLabelUpdate(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
return updateTextureLabel(contextVk);
}
angle::Result TextureVk::updateTextureLabel(ContextVk *contextVk)
{
vk::Renderer *renderer = contextVk->getRenderer();
std::string label = mState.getLabel();
if (!label.empty() && renderer->enableDebugUtils() && imageValid())
{
return vk::SetDebugUtilsObjectName(contextVk, VK_OBJECT_TYPE_IMAGE,
(uint64_t)(getImage().getImage().getHandle()),
mState.getLabel());
}
return angle::Result::Continue;
}
vk::BufferHelper *TextureVk::getRGBAConversionBufferHelper(vk::Renderer *renderer,
angle::FormatID formatID) const
{
BufferVk *bufferVk = vk::GetImpl(getBuffer().get());
const gl::OffsetBindingPointer<gl::Buffer> &bufferBinding = mState.getBuffer();
const VertexConversionBuffer::CacheKey cacheKey{
formatID, 16, static_cast<size_t>(bufferBinding.getOffset()), false, true};
ConversionBuffer *conversion = bufferVk->getVertexConversionBuffer(renderer, cacheKey);
return conversion->getBuffer();
}
angle::Result TextureVk::convertBufferToRGBA(ContextVk *contextVk, size_t &conversionBufferSize)
{
vk::Renderer *renderer = contextVk->getRenderer();
const gl::ImageDesc &baseLevelDesc = mState.getBaseLevelDesc();
const vk::Format *imageUniformFormat =
&renderer->getFormat(baseLevelDesc.format.info->sizedInternalFormat);
const gl::OffsetBindingPointer<gl::Buffer> &bufferBinding = mState.getBuffer();
BufferVk *bufferVk = vk::GetImpl(getBuffer().get());
const size_t bindingOffset = bufferBinding.getOffset();
const VkDeviceSize bufferSize = bufferVk->getSize();
const VkDeviceSize bufferSizeFromOffset = bufferSize - bindingOffset;
conversionBufferSize = roundUpPow2<size_t>(static_cast<size_t>((bufferSizeFromOffset / 3) * 4),
4 * sizeof(uint32_t));
const VertexConversionBuffer::CacheKey cacheKey{imageUniformFormat->getIntendedFormatID(), 16,
bindingOffset, false, true};
ConversionBuffer *conversion = bufferVk->getVertexConversionBuffer(renderer, cacheKey);
mBufferContentsObservers->enableForBuffer(getBuffer().get());
if (!conversion->valid())
{
ANGLE_TRY(contextVk->initBufferForVertexConversion(conversion, conversionBufferSize,
vk::MemoryHostVisibility::NonVisible));
}
vk::BufferHelper *conversionBufferHelper = conversion->getBuffer();
if (conversion->dirty())
{
vk::BufferHelper &bufferHelper = bufferVk->getBuffer();
UtilsVk &utilsVk = contextVk->getUtils();
const VkDeviceSize pixelSize = 3 * sizeof(uint32_t);
const VkDeviceSize pixelCount = bufferSizeFromOffset / pixelSize;
ANGLE_TRY(utilsVk.copyRgbToRgba(contextVk, imageUniformFormat->getIntendedFormat(),
&bufferHelper, static_cast<uint32_t>(bindingOffset),
static_cast<uint32_t>(pixelCount), conversionBufferHelper));
conversion->clearDirty();
}
return angle::Result::Continue;
}
angle::Result TextureVk::syncState(const gl::Context *context,
const gl::Texture::DirtyBits &dirtyBits,
gl::Command source)
{
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
// If this is a texture buffer, release buffer views. There's nothing else to sync. The
// image must already be deleted, and the sampler reset.
if (mState.getBuffer().get() != nullptr)
{
ASSERT(mImage == nullptr);
const gl::OffsetBindingPointer<gl::Buffer> &bufferBinding = mState.getBuffer();
VkDeviceSize offset = bufferBinding.getOffset();
VkDeviceSize size = gl::GetBoundBufferAvailableSize(bufferBinding);
if (NeedsRGBAEmulation(renderer, getBaseLevelFormat(renderer).getIntendedFormatID()))
{
size_t conversionBufferSize;
ANGLE_TRY(convertBufferToRGBA(contextVk, conversionBufferSize));
offset = 0;
size = conversionBufferSize;
}
mBufferViews.release(contextVk);
mBufferViews.init(renderer, offset, size);
mDescriptorSetCacheManager.releaseKeys(renderer);
return angle::Result::Continue;
}
ANGLE_TRY(respecifyImageStorageIfNecessary(contextVk, source));
ANGLE_TRY(performImageQueueTransferIfNecessary(contextVk));
// Initialize the image storage and flush the pixel buffer.
const bool isGenerateMipmap = source == gl::Command::GenerateMipmap;
ANGLE_TRY(ensureImageInitialized(contextVk, isGenerateMipmap
? ImageMipLevels::FullMipChainForGenerateMipmap
: ImageMipLevels::EnabledLevels));
// Mask out the IMPLEMENTATION dirty bit to avoid unnecessary syncs.
// Keep it set when the border color is used and needs to be resynced.
gl::Texture::DirtyBits localBits = dirtyBits;
if (!mState.getSamplerState().usesBorderColor())
{
localBits.reset(gl::Texture::DIRTY_BIT_IMPLEMENTATION);
}
localBits.reset(gl::Texture::DIRTY_BIT_BASE_LEVEL);
localBits.reset(gl::Texture::DIRTY_BIT_MAX_LEVEL);
// For AHBs, the ImageViews are created with VkSamplerYcbcrConversionInfo's chromaFilter
// matching min/magFilters as part of the eglEGLImageTargetTexture2DOES() call. However, the
// min/mag filters can change later, requiring the ImageViews to be refreshed.
if (mImage->valid() && mImage->hasImmutableSampler() &&
(dirtyBits.test(gl::Texture::DIRTY_BIT_MIN_FILTER) ||
dirtyBits.test(gl::Texture::DIRTY_BIT_MAG_FILTER)))
{
const gl::SamplerState &samplerState = mState.getSamplerState();
VkFilter chromaFilter = samplerState.getMinFilter() == samplerState.getMagFilter()
? gl_vk::GetFilter(samplerState.getMinFilter())
: vk::kDefaultYCbCrChromaFilter;
if (mImage->updateChromaFilter(renderer, chromaFilter))
{
resetSampler();
ANGLE_TRY(refreshImageViews(contextVk));
}
}
if (localBits.none() && mSampler)
{
return angle::Result::Continue;
}
if (mSampler)
{
resetSampler();
}
if (localBits.test(gl::Texture::DIRTY_BIT_SWIZZLE_RED) ||
localBits.test(gl::Texture::DIRTY_BIT_SWIZZLE_GREEN) ||
localBits.test(gl::Texture::DIRTY_BIT_SWIZZLE_BLUE) ||
localBits.test(gl::Texture::DIRTY_BIT_SWIZZLE_ALPHA))
{
ANGLE_TRY(refreshImageViews(contextVk));
}
if (localBits.test(gl::Texture::DIRTY_BIT_SRGB_OVERRIDE) ||
localBits.test(gl::Texture::DIRTY_BIT_SRGB_DECODE))
{
ASSERT(mImage != nullptr);
gl::SrgbDecode srgbDecode = (mState.getSamplerState().getSRGBDecode() == GL_SKIP_DECODE_EXT)
? gl::SrgbDecode::Skip
: gl::SrgbDecode::Default;
mImageView.updateSrgbDecode(*mImage, srgbDecode);
mImageView.updateSrgbOverride(*mImage, mState.getSRGBOverride());
if (!renderer->getFeatures().supportsImageFormatList.enabled)
{
ANGLE_TRY(refreshImageViews(contextVk));
}
}
vk::SamplerDesc samplerDesc(contextVk, mState.getSamplerState(), mState.isStencilMode(),
&mImage->getYcbcrConversionDesc(), mImage->getIntendedFormatID());
auto y2yConversionDesc = mImage->getY2YConversionDesc();
vk::SamplerDesc samplerDescSamplerExternal2DY2YEXT(contextVk, mState.getSamplerState(),
mState.isStencilMode(), &y2yConversionDesc,
mImage->getIntendedFormatID());
ANGLE_TRY(renderer->getSamplerCache().getSampler(contextVk, samplerDesc, &mSampler));
ANGLE_TRY(renderer->getSamplerCache().getSampler(contextVk, samplerDescSamplerExternal2DY2YEXT,
&mY2YSampler));
updateCachedImageViewSerials();
return angle::Result::Continue;
}
angle::Result TextureVk::initializeContents(const gl::Context *context,
GLenum binding,
const gl::ImageIndex &imageIndex)
{
ContextVk *contextVk = vk::GetImpl(context);
const gl::ImageDesc &desc = mState.getImageDesc(imageIndex);
const vk::Format &format =
contextVk->getRenderer()->getFormat(desc.format.info->sizedInternalFormat);
ASSERT(mImage);
// Note that we cannot ensure the image is initialized because we might be calling subImage
// on a non-complete cube map.
return mImage->stageRobustResourceClearWithFormat(
contextVk, imageIndex, desc.size, format.getIntendedFormat(),
format.getActualImageFormat(getRequiredImageAccess()));
}
angle::Result TextureVk::initializeContentsWithBlack(const gl::Context *context,
GLenum binding,
const gl::ImageIndex &imageIndex)
{
ContextVk *contextVk = vk::GetImpl(context);
const gl::ImageDesc &desc = mState.getImageDesc(imageIndex);
const vk::Format &format =
contextVk->getRenderer()->getFormat(desc.format.info->sizedInternalFormat);
VkClearValue clearValue = {};
clearValue.color = {{0, 0, 0, 1.0f}};
ASSERT(mImage);
// Note that we cannot ensure the image is initialized because we might be calling subImage
// on a non-complete cube map.
return mImage->stageResourceClearWithFormat(
contextVk, imageIndex, desc.size, format.getIntendedFormat(),
format.getActualImageFormat(getRequiredImageAccess()), clearValue);
}
void TextureVk::releaseOwnershipOfImage(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
ASSERT(!mImageSiblingSerial.valid());
mOwnsImage = false;
releaseAndDeleteImageAndViews(contextVk);
}
const vk::ImageView &TextureVk::getReadImageView(GLenum srgbDecode,
bool texelFetchStaticUse,
bool samplerExternal2DY2YEXT) const
{
ASSERT(mImage->valid());
const vk::ImageViewHelper &imageViews = getImageViews();
if (mState.isStencilMode() && imageViews.hasStencilReadImageView())
{
return imageViews.getStencilReadImageView();
}
if (samplerExternal2DY2YEXT)
{
ASSERT(imageViews.getSamplerExternal2DY2YEXTImageView().valid());
return imageViews.getSamplerExternal2DY2YEXTImageView();
}
ASSERT(mImage != nullptr && mImage->valid());
gl::SrgbDecode decode =
(srgbDecode == GL_DECODE_EXT) ? gl::SrgbDecode::Default : gl::SrgbDecode::Skip;
imageViews.updateSrgbDecode(*mImage, decode);
imageViews.updateStaticTexelFetch(*mImage, texelFetchStaticUse);
ASSERT(imageViews.getReadImageView().valid());
return imageViews.getReadImageView();
}
const vk::ImageView &TextureVk::getCopyImageView() const
{
ASSERT(mImage->valid());
const vk::ImageViewHelper &imageViews = getImageViews();
const angle::Format &angleFormat = mImage->getActualFormat();
ASSERT(angleFormat.isSRGB ==
(ConvertToLinear(mImage->getActualFormatID()) != angle::FormatID::NONE));
if (angleFormat.isSRGB)
{
return imageViews.getSRGBCopyImageView();
}
return imageViews.getLinearCopyImageView();
}
angle::Result TextureVk::getLevelLayerImageView(vk::Context *context,
gl::LevelIndex level,
size_t layer,
const vk::ImageView **imageViewOut)
{
ASSERT(mImage && mImage->valid());
gl::LevelIndex levelGL = getNativeImageLevel(level);
vk::LevelIndex levelVk = mImage->toVkLevel(levelGL);
uint32_t nativeLayer = getNativeImageLayer(static_cast<uint32_t>(layer));
return getImageViews().getLevelLayerDrawImageView(context, *mImage, levelVk, nativeLayer,
imageViewOut);
}
angle::Result TextureVk::getStorageImageView(vk::Context *context,
const gl::ImageUnit &binding,
const vk::ImageView **imageViewOut)
{
vk::Renderer *renderer = context->getRenderer();
angle::FormatID formatID = angle::Format::InternalFormatToID(binding.format);
const vk::Format *format = &renderer->getFormat(formatID);
format = AdjustStorageViewFormatPerWorkarounds(renderer, format, getRequiredImageAccess());
gl::LevelIndex nativeLevelGL =
getNativeImageLevel(gl::LevelIndex(static_cast<uint32_t>(binding.level)));
vk::LevelIndex nativeLevelVk = mImage->toVkLevel(nativeLevelGL);
if (binding.layered != GL_TRUE)
{
uint32_t nativeLayer = getNativeImageLayer(static_cast<uint32_t>(binding.layer));
return getImageViews().getLevelLayerStorageImageView(
context, *mImage, nativeLevelVk, nativeLayer,
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT,
format->getActualImageFormatID(getRequiredImageAccess()), imageViewOut);
}
uint32_t nativeLayer = getNativeImageLayer(0);
return getImageViews().getLevelStorageImageView(
context, mState.getType(), *mImage, nativeLevelVk, nativeLayer,
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT,
format->getActualImageFormatID(getRequiredImageAccess()), imageViewOut);
}
vk::BufferHelper *TextureVk::getPossiblyEmulatedTextureBuffer(vk::Context *context) const
{
vk::Renderer *renderer = context->getRenderer();
angle::FormatID format = getBaseLevelFormat(renderer).getIntendedFormatID();
if (NeedsRGBAEmulation(renderer, format))
{
return getRGBAConversionBufferHelper(renderer, format);
}
BufferVk *bufferVk = vk::GetImpl(getBuffer().get());
return &bufferVk->getBuffer();
}
angle::Result TextureVk::getBufferView(vk::Context *context,
const vk::Format *imageUniformFormat,
const gl::SamplerBinding *samplerBinding,
bool isImage,
const vk::BufferView **viewOut)
{
vk::Renderer *renderer = context->getRenderer();
ASSERT(mState.getBuffer().get() != nullptr);
// Use the format specified by glTexBuffer if no format specified by the shader.
if (imageUniformFormat == nullptr)
{
imageUniformFormat = &getBaseLevelFormat(renderer);
}
if (isImage)
{
imageUniformFormat = AdjustStorageViewFormatPerWorkarounds(renderer, imageUniformFormat,
getRequiredImageAccess());
}
const vk::BufferHelper *buffer = &vk::GetImpl(mState.getBuffer().get())->getBuffer();
if (NeedsRGBAEmulation(renderer, imageUniformFormat->getIntendedFormatID()))
{
buffer = getRGBAConversionBufferHelper(renderer, imageUniformFormat->getIntendedFormatID());
imageUniformFormat = &renderer->getFormat(
GetRGBAEmulationDstFormat(imageUniformFormat->getIntendedFormatID()));
}
if (samplerBinding)
{
imageUniformFormat =
AdjustViewFormatForSampler(renderer, imageUniformFormat, samplerBinding->format);
}
// Create a view for the required format.
return mBufferViews.getView(context, *buffer, buffer->getOffset(), *imageUniformFormat,
viewOut);
}
angle::Result TextureVk::initImage(ContextVk *contextVk,
angle::FormatID intendedImageFormatID,
angle::FormatID actualImageFormatID,
ImageMipLevels mipLevels)
{
vk::Renderer *renderer = contextVk->getRenderer();
// Create the image. For immutable texture, we always allocate the full immutable levels
// specified by texStorage call. Otherwise we only try to allocate from base to max levels.
const gl::ImageDesc *firstLevelDesc;
uint32_t firstLevel, levelCount;
if (mState.getImmutableFormat())
{
firstLevelDesc = &mState.getLevelZeroDesc();
firstLevel = 0;
levelCount = mState.getImmutableLevels();
}
else
{
firstLevelDesc = &mState.getBaseLevelDesc();
firstLevel = mState.getEffectiveBaseLevel();
levelCount = getMipLevelCount(mipLevels);
}
const gl::Extents &firstLevelExtents = firstLevelDesc->size;
VkExtent3D vkExtent;
uint32_t layerCount;
gl_vk::GetExtentsAndLayerCount(mState.getType(), firstLevelExtents, &vkExtent, &layerCount);
GLint samples = mState.getBaseLevelDesc().samples ? mState.getBaseLevelDesc().samples : 1;
if (contextVk->getFeatures().limitSampleCountTo2.enabled)
{
samples = std::min(samples, 2);
}
if (mState.hasProtectedContent())
{
mImageCreateFlags |= VK_IMAGE_CREATE_PROTECTED_BIT;
}
if (renderer->getFeatures().supportsComputeTranscodeEtcToBc.enabled &&
IsETCFormat(intendedImageFormatID) && IsBCFormat(actualImageFormatID))
{
mImageCreateFlags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT |
VK_IMAGE_CREATE_EXTENDED_USAGE_BIT |
VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT;
mImageUsageFlags |= VK_IMAGE_USAGE_STORAGE_BIT;
}
mImageCreateFlags |=
vk::GetMinimalImageCreateFlags(renderer, mState.getType(), mImageUsageFlags);
const VkFormat actualImageFormat =
rx::vk::GetVkFormatFromFormatID(renderer, actualImageFormatID);
const VkImageType imageType = gl_vk::GetImageType(mState.getType());
const VkImageTiling imageTiling = mImage->getTilingMode();
// The MSRTSS bit is included in the create flag for all textures if the feature flag
// corresponding to its preference is enabled. Otherwise, it is enabled for a texture if it is
// bound to an MSRTT framebuffer.
const bool shouldIncludeMSRTSSBit =
contextVk->getFeatures().supportsMultisampledRenderToSingleSampled.enabled &&
(contextVk->getFeatures().preferMSRTSSFlagByDefault.enabled ||
mState.hasBeenBoundToMSRTTFramebuffer());
if ((mImageUsageFlags & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) != 0 &&
mOwnsImage && samples == 1 && shouldIncludeMSRTSSBit)
{
VkImageCreateFlags createFlagsMultisampled =
mImageCreateFlags | VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT;
bool isActualFormatSRGB = angle::Format::Get(actualImageFormatID).isSRGB;
const VkFormat additionalViewFormat = rx::vk::GetVkFormatFromFormatID(
renderer, isActualFormatSRGB ? ConvertToLinear(actualImageFormatID)
: ConvertToSRGB(actualImageFormatID));
const bool isAdditionalFormatValid = additionalViewFormat != VK_FORMAT_UNDEFINED;
// If the texture has already been bound to an MSRTT framebuffer, lack of support should
// result in failure.
bool supportsMSRTTUsageActualFormat = vk::ImageHelper::FormatSupportsUsage(
renderer, actualImageFormat, imageType, imageTiling, mImageUsageFlags,
createFlagsMultisampled, nullptr, nullptr,
vk::ImageHelper::FormatSupportCheck::RequireMultisampling);
bool supportsMSRTTUsageAdditionalFormat =
!isAdditionalFormatValid ||
vk::ImageHelper::FormatSupportsUsage(
renderer, additionalViewFormat, imageType, imageTiling, mImageUsageFlags,
createFlagsMultisampled, nullptr, nullptr,
vk::ImageHelper::FormatSupportCheck::RequireMultisampling);
bool supportsMSRTTUsage =
supportsMSRTTUsageActualFormat && supportsMSRTTUsageAdditionalFormat;
if (ANGLE_UNLIKELY(mState.hasBeenBoundToMSRTTFramebuffer() && !supportsMSRTTUsage))
{
ERR() << "Texture bound to EXT_multisampled_render_to_texture framebuffer, "
<< "but this device does not support this format.";
ANGLE_VK_TRY(contextVk, VK_ERROR_FORMAT_NOT_SUPPORTED);
}
// Note: If we ever fail the following check, we should use the emulation path for this
// texture instead of ignoring MSRTT.
if (supportsMSRTTUsage)
{
// If supported by format add the MSRTSS flag because any texture might end up as an
// MSRTT attachment.
mImageCreateFlags |= VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT;
}
}
// Any format with VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT support is required to also support
// VK_FORMAT_FEATURE_2_HOST_IMAGE_TRANSFER_BIT_EXT. So no format feature query is needed.
// However, it's still necessary to use vkGetPhysicalDeviceImageFormatProperties2 to ensure host
// image copy is supported for the specific usage and flags.
//
// All TextureVk images are expected to have VK_IMAGE_USAGE_SAMPLED_BIT, so that is not checked
// either.
ASSERT((mImageUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT) != 0);
if (mOwnsImage && samples == 1 && renderer->getFeatures().supportsHostImageCopy.enabled)
{
VkHostImageCopyDevicePerformanceQueryEXT perfQuery = {};
perfQuery.sType = VK_STRUCTURE_TYPE_HOST_IMAGE_COPY_DEVICE_PERFORMANCE_QUERY_EXT;
// If host image copy is supported at all ...
if (vk::ImageHelper::FormatSupportsUsage(
renderer, actualImageFormat, imageType, imageTiling,
mImageUsageFlags | VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT, mImageCreateFlags, nullptr,
&perfQuery, vk::ImageHelper::FormatSupportCheck::OnlyQuerySuccess))
{
// Only enable it if it has no performance impact whatsoever (or impact is tiny, given
// feature).
if (perfQuery.identicalMemoryLayout ||
(perfQuery.optimalDeviceAccess &&
renderer->getFeatures().allowHostImageCopyDespiteNonIdenticalLayout.enabled))
{
mImageUsageFlags |= VK_IMAGE_USAGE_HOST_TRANSFER_BIT_EXT;
}
}
}
// Fixed rate compression
VkImageCompressionControlEXT *compressionInfo = nullptr;
VkImageCompressionControlEXT compressionInfoVar = {};
compressionInfoVar.sType = VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_CONTROL_EXT;
VkImageCompressionFixedRateFlagsEXT compressionRates = VK_IMAGE_COMPRESSION_FIXED_RATE_NONE_EXT;
if (mOwnsImage && renderer->getFeatures().supportsImageCompressionControl.enabled)
{
// Use default compression control flag for query
compressionInfoVar.flags = VK_IMAGE_COMPRESSION_FIXED_RATE_DEFAULT_EXT;
VkImageCompressionPropertiesEXT compressionProp = {};
compressionProp.sType = VK_STRUCTURE_TYPE_IMAGE_COMPRESSION_PROPERTIES_EXT;
// If fixed rate compression is supported by this type, not support YUV now.
const vk::Format &format = renderer->getFormat(intendedImageFormatID);
if (!mImage->isYuvResolve() &&
(getFormatSupportedCompressionRatesImpl(renderer, format, 0, nullptr) != 0))
{
mImage->getCompressionFixedRate(&compressionInfoVar, &compressionRates,
mState.getSurfaceCompressionFixedRate());
compressionInfo = &compressionInfoVar;
}
}
ANGLE_TRY(mImage->initExternal(
contextVk, mState.getType(), vkExtent, intendedImageFormatID, actualImageFormatID, samples,
mImageUsageFlags, mImageCreateFlags, vk::ImageLayout::Undefined, nullptr,
gl::LevelIndex(firstLevel), levelCount, layerCount,
contextVk->isRobustResourceInitEnabled(), mState.hasProtectedContent(),
vk::ImageHelper::deriveConversionDesc(contextVk, actualImageFormatID,
intendedImageFormatID),
compressionInfo));
ANGLE_TRY(updateTextureLabel(contextVk));
// Update create flags with mImage's create flags
mImageCreateFlags |= mImage->getCreateFlags();
mRequiresMutableStorage = (mImageCreateFlags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) != 0;
VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
if (mState.hasProtectedContent())
{
flags |= VK_MEMORY_PROPERTY_PROTECTED_BIT;
}
ANGLE_TRY(contextVk->initImageAllocation(mImage, mState.hasProtectedContent(),
renderer->getMemoryProperties(), flags,
vk::MemoryAllocationType::TextureImage));
const uint32_t viewLevelCount =
mState.getImmutableFormat() ? getMipLevelCount(ImageMipLevels::EnabledLevels) : levelCount;
ANGLE_TRY(initImageViews(contextVk, viewLevelCount));
mCurrentBaseLevel = gl::LevelIndex(mState.getBaseLevel());
mCurrentMaxLevel = gl::LevelIndex(mState.getMaxLevel());
return angle::Result::Continue;
}
angle::Result TextureVk::initImageViews(ContextVk *contextVk, uint32_t levelCount)
{
ASSERT(mImage != nullptr && mImage->valid());
gl::LevelIndex baseLevelGL =
getNativeImageLevel(gl::LevelIndex(mState.getEffectiveBaseLevel()));
vk::LevelIndex baseLevelVk = mImage->toVkLevel(baseLevelGL);
uint32_t baseLayer = getNativeImageLayer(0);
const gl::ImageDesc &baseLevelDesc = mState.getBaseLevelDesc();
const bool sized = baseLevelDesc.format.info->sized;
const angle::Format &intendedFormat = mImage->getIntendedFormat();
gl::SwizzleState formatSwizzle = GetFormatSwizzle(intendedFormat, sized);
gl::SwizzleState readSwizzle = ApplySwizzle(formatSwizzle, mState.getSwizzleState());
// Use this as a proxy for the SRGB override & skip decode settings.
bool createExtraSRGBViews = mRequiresMutableStorage;
const VkImageUsageFlags kDisallowedSwizzledUsage =
VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT |
VK_IMAGE_USAGE_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR;
ANGLE_TRY(getImageViews().initReadViews(contextVk, mState.getType(), *mImage, formatSwizzle,
readSwizzle, baseLevelVk, levelCount, baseLayer,
getImageViewLayerCount(), createExtraSRGBViews,
getImage().getUsage() & ~kDisallowedSwizzledUsage));
updateCachedImageViewSerials();
return angle::Result::Continue;
}
void TextureVk::releaseImage(ContextVk *contextVk)
{
vk::Renderer *renderer = contextVk->getRenderer();
releaseImageViews(contextVk);
if (mImage)
{
if (mOwnsImage)
{
mImage->releaseImageFromShareContexts(renderer, contextVk, mImageSiblingSerial);
}
else
{
mImage->finalizeImageLayoutInShareContexts(renderer, contextVk, mImageSiblingSerial);
mImageObserverBinding.bind(nullptr);
mImage = nullptr;
}
}
if (mMultisampledImages)
{
for (gl::TexLevelArray<vk::ImageHelper> &images : *mMultisampledImages)
{
for (vk::ImageHelper &image : images)
{
if (image.valid())
{
image.releaseImageFromShareContexts(renderer, contextVk, mImageSiblingSerial);
}
}
}
mMultisampledImages.reset();
}
onStateChange(angle::SubjectMessage::SubjectChanged);
mRedefinedLevels = {};
}
void TextureVk::releaseImageViews(ContextVk *contextVk)
{
vk::Renderer *renderer = contextVk->getRenderer();
mDescriptorSetCacheManager.releaseKeys(renderer);
if (mImage == nullptr)
{
if (mMultisampledImageViews)
{
for (gl::TexLevelArray<vk::ImageViewHelper> &imageViewHelpers :
*mMultisampledImageViews)
{
for (vk::ImageViewHelper &imageViewHelper : imageViewHelpers)
{
ASSERT(imageViewHelper.isImageViewGarbageEmpty());
}
}
mMultisampledImageViews.reset();
}
for (auto &renderTargets : mSingleLayerRenderTargets)
{
ASSERT(renderTargets.empty());
}
ASSERT(mMultiLayerRenderTargets.empty());
return;
}
mImageView.release(renderer, mImage->getResourceUse());
if (mMultisampledImageViews)
{
for (gl::TexLevelArray<vk::ImageViewHelper> &imageViewHelpers : *mMultisampledImageViews)
{
for (vk::ImageViewHelper &imageViewHelper : imageViewHelpers)
{
imageViewHelper.release(renderer, mImage->getResourceUse());
}
}
mMultisampledImageViews.reset();
}
for (auto &renderTargets : mSingleLayerRenderTargets)
{
for (RenderTargetVector &renderTargetLevels : renderTargets)
{
for (RenderTargetVk &renderTargetVk : renderTargetLevels)
{
renderTargetVk.releaseFramebuffers(contextVk);
}
// Clear the layers tracked for each level
renderTargetLevels.clear();
}
// Then clear the levels
renderTargets.clear();
}
for (auto &renderTargetPair : mMultiLayerRenderTargets)
{
renderTargetPair.second->releaseFramebuffers(contextVk);
}
mMultiLayerRenderTargets.clear();
}
void TextureVk::releaseStagedUpdates(ContextVk *contextVk)
{
if (mImage)
{
mImage->releaseStagedUpdates(contextVk->getRenderer());
}
}
uint32_t TextureVk::getMipLevelCount(ImageMipLevels mipLevels) const
{
switch (mipLevels)
{
// Returns level count from base to max that has been specified, i.e, enabled.
case ImageMipLevels::EnabledLevels:
return mState.getEnabledLevelCount();
// Returns all mipmap levels from base to max regardless if an image has been specified or
// not.
case ImageMipLevels::FullMipChainForGenerateMipmap:
return getMaxLevelCount() - mState.getEffectiveBaseLevel();
default:
UNREACHABLE();
return 0;
}
}
uint32_t TextureVk::getMaxLevelCount() const
{
// getMipmapMaxLevel will be 0 here if mipmaps are not used, so the levelCount is always +1.
return mState.getMipmapMaxLevel() + 1;
}
angle::Result TextureVk::generateMipmapLevelsWithCPU(ContextVk *contextVk,
const angle::Format &sourceFormat,
GLuint layer,
gl::LevelIndex firstMipLevel,
gl::LevelIndex maxMipLevel,
const size_t sourceWidth,
const size_t sourceHeight,
const size_t sourceDepth,
const size_t sourceRowPitch,
const size_t sourceDepthPitch,
uint8_t *sourceData)
{
size_t previousLevelWidth = sourceWidth;
size_t previousLevelHeight = sourceHeight;
size_t previousLevelDepth = sourceDepth;
uint8_t *previousLevelData = sourceData;
size_t previousLevelRowPitch = sourceRowPitch;
size_t previousLevelDepthPitch = sourceDepthPitch;
for (gl::LevelIndex currentMipLevel = firstMipLevel; currentMipLevel <= maxMipLevel;
++currentMipLevel)
{
// Compute next level width and height.
size_t mipWidth = std::max<size_t>(1, previousLevelWidth >> 1);
size_t mipHeight = std::max<size_t>(1, previousLevelHeight >> 1);
size_t mipDepth = std::max<size_t>(1, previousLevelDepth >> 1);
// With the width and height of the next mip, we can allocate the next buffer we need.
uint8_t *destData = nullptr;
size_t destRowPitch = mipWidth * sourceFormat.pixelBytes;
size_t destDepthPitch = destRowPitch * mipHeight;
size_t mipAllocationSize = destDepthPitch * mipDepth;
gl::Extents mipLevelExtents(static_cast<int>(mipWidth), static_cast<int>(mipHeight),
static_cast<int>(mipDepth));
ANGLE_TRY(mImage->stageSubresourceUpdateAndGetData(
contextVk, mipAllocationSize,
gl::ImageIndex::MakeFromType(mState.getType(), currentMipLevel.get(), layer),
mipLevelExtents, gl::Offset(), &destData, sourceFormat.id));
// Generate the mipmap into that new buffer
sourceFormat.mipGenerationFunction(
previousLevelWidth, previousLevelHeight, previousLevelDepth, previousLevelData,
previousLevelRowPitch, previousLevelDepthPitch, destData, destRowPitch, destDepthPitch);
// Swap for the next iteration
previousLevelWidth = mipWidth;
previousLevelHeight = mipHeight;
previousLevelDepth = mipDepth;
previousLevelData = destData;
previousLevelRowPitch = destRowPitch;
previousLevelDepthPitch = destDepthPitch;
}
return angle::Result::Continue;
}
const gl::InternalFormat &TextureVk::getImplementationSizedFormat(const gl::Context *context) const
{
GLenum sizedFormat = GL_NONE;
if (mImage && mImage->valid())
{
sizedFormat = mImage->getActualFormat().glInternalFormat;
}
else
{
ContextVk *contextVk = vk::GetImpl(context);
const vk::Format &format = getBaseLevelFormat(contextVk->getRenderer());
sizedFormat = format.getActualImageFormat(getRequiredImageAccess()).glInternalFormat;
}
return gl::GetSizedInternalFormatInfo(sizedFormat);
}
GLenum TextureVk::getColorReadFormat(const gl::Context *context)
{
const gl::InternalFormat &sizedFormat = getImplementationSizedFormat(context);
return sizedFormat.format;
}
GLenum TextureVk::getColorReadType(const gl::Context *context)
{
const gl::InternalFormat &sizedFormat = getImplementationSizedFormat(context);
return sizedFormat.type;
}
angle::Result TextureVk::getTexImage(const gl::Context *context,
const gl::PixelPackState &packState,
gl::Buffer *packBuffer,
gl::TextureTarget target,
GLint level,
GLenum format,
GLenum type,
void *pixels)
{
if (packBuffer && this->isCompressedFormatEmulated(context, target, level))
{
// TODO (anglebug.com/42265933): Can't populate from a buffer using emulated format
UNIMPLEMENTED();
return angle::Result::Stop;
}
ContextVk *contextVk = vk::GetImpl(context);
ANGLE_TRY(ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
GLint baseLevel = static_cast<int>(mState.getBaseLevel());
if (level < baseLevel || level >= baseLevel + static_cast<int>(mState.getEnabledLevelCount()))
{
// TODO(http://anglebug.com/42264855): Handle inconsistent textures.
WARN() << "GetTexImage for inconsistent texture levels is not implemented.";
UNIMPLEMENTED();
return angle::Result::Continue;
}
gl::MaybeOverrideLuminance(format, type, getColorReadFormat(context),
getColorReadType(context));
uint32_t layer = 0;
uint32_t layerCount = 1;
switch (target)
{
case gl::TextureTarget::CubeMapArray:
case gl::TextureTarget::_2DArray:
layerCount = mImage->getLayerCount();
break;
default:
if (gl::IsCubeMapFaceTarget(target))
{
layer = static_cast<uint32_t>(gl::CubeMapTextureTargetToFaceIndex(target));
}
break;
}
return mImage->readPixelsForGetImage(contextVk, packState, packBuffer, gl::LevelIndex(level),
layer, layerCount, format, type, pixels);
}
angle::Result TextureVk::getCompressedTexImage(const gl::Context *context,
const gl::PixelPackState &packState,
gl::Buffer *packBuffer,
gl::TextureTarget target,
GLint level,
void *pixels)
{
ContextVk *contextVk = vk::GetImpl(context);
ANGLE_TRY(ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
GLint baseLevel = static_cast<int>(mState.getBaseLevel());
if (level < baseLevel || level >= baseLevel + static_cast<int>(mState.getEnabledLevelCount()))
{
// TODO(http://anglebug.com/42264855): Handle inconsistent textures.
WARN() << "GetCompressedTexImage for inconsistent texture levels is not implemented.";
UNIMPLEMENTED();
return angle::Result::Continue;
}
uint32_t layer = 0;
uint32_t layerCount = 1;
switch (target)
{
case gl::TextureTarget::CubeMapArray:
case gl::TextureTarget::_2DArray:
layerCount = mImage->getLayerCount();
break;
default:
if (gl::IsCubeMapFaceTarget(target))
{
layer = static_cast<uint32_t>(gl::CubeMapTextureTargetToFaceIndex(target));
}
break;
}
return mImage->readPixelsForCompressedGetImage(
contextVk, packState, packBuffer, gl::LevelIndex(level), layer, layerCount, pixels);
}
const vk::Format &TextureVk::getBaseLevelFormat(vk::Renderer *renderer) const
{
const gl::ImageDesc &baseLevelDesc = mState.getBaseLevelDesc();
return renderer->getFormat(baseLevelDesc.format.info->sizedInternalFormat);
}
void TextureVk::onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message)
{
ASSERT(index == kTextureImageSubjectIndex &&
(message == angle::SubjectMessage::SubjectChanged ||
message == angle::SubjectMessage::InitializationComplete));
// Forward the notification to the parent that the staging buffer changed.
onStateChange(message);
}
vk::ImageOrBufferViewSubresourceSerial TextureVk::getImageViewSubresourceSerialImpl(
vk::ImageViewColorspace colorspace) const
{
gl::LevelIndex baseLevel(mState.getEffectiveBaseLevel());
// getMipmapMaxLevel will clamp to the max level if it is smaller than the number of mips.
uint32_t levelCount = gl::LevelIndex(mState.getMipmapMaxLevel()) - baseLevel + 1;
return getImageViews().getSubresourceSerialForColorspace(baseLevel, levelCount, 0,
vk::LayerMode::All, colorspace);
}
vk::ImageOrBufferViewSubresourceSerial TextureVk::getBufferViewSerial() const
{
return mBufferViews.getSerial();
}
vk::ImageOrBufferViewSubresourceSerial TextureVk::getStorageImageViewSerial(
const gl::ImageUnit &binding) const
{
vk::LayerMode layerMode = binding.layered == GL_TRUE ? vk::LayerMode::All : vk::LayerMode::_1;
uint32_t frontendLayer = binding.layered == GL_TRUE ? 0 : static_cast<uint32_t>(binding.layer);
uint32_t nativeLayer = getNativeImageLayer(frontendLayer);
gl::LevelIndex baseLevel(
getNativeImageLevel(gl::LevelIndex(static_cast<uint32_t>(binding.level))));
return getImageViews().getSubresourceSerial(baseLevel, 1, nativeLayer, layerMode);
}
uint32_t TextureVk::getImageViewLayerCount() const
{
// We use a special layer count here to handle EGLImages. They might only be
// looking at one layer of a cube or 2D array texture.
return mEGLImageNativeType == gl::TextureType::InvalidEnum ? mImage->getLayerCount() : 1;
}
uint32_t TextureVk::getImageViewLevelCount() const
{
// We use a special level count here to handle EGLImages. They might only be
// looking at one level of the texture's mipmap chain.
return mEGLImageNativeType == gl::TextureType::InvalidEnum ? mImage->getLevelCount() : 1;
}
angle::Result TextureVk::refreshImageViews(ContextVk *contextVk)
{
vk::ImageViewHelper &imageView = getImageViews();
if (mImage == nullptr)
{
ASSERT(imageView.isImageViewGarbageEmpty());
}
else
{
vk::Renderer *renderer = contextVk->getRenderer();
imageView.release(renderer, mImage->getResourceUse());
// Since view has changed, some descriptorSet cache maybe obsolete. SO proactively release
// cache.
mDescriptorSetCacheManager.releaseKeys(renderer);
for (auto &renderTargets : mSingleLayerRenderTargets)
{
for (RenderTargetVector &renderTargetLevels : renderTargets)
{
for (RenderTargetVk &renderTargetVk : renderTargetLevels)
{
renderTargetVk.releaseFramebuffers(contextVk);
}
}
}
for (auto &renderTargetPair : mMultiLayerRenderTargets)
{
renderTargetPair.second->releaseFramebuffers(contextVk);
}
}
ANGLE_TRY(initImageViews(contextVk, getImageViewLevelCount()));
// Let any Framebuffers know we need to refresh the RenderTarget cache.
onStateChange(angle::SubjectMessage::SubjectChanged);
return angle::Result::Continue;
}
angle::Result TextureVk::ensureMutable(ContextVk *contextVk)
{
if (mRequiresMutableStorage)
{
return angle::Result::Continue;
}
mRequiresMutableStorage = true;
mImageCreateFlags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
ANGLE_TRY(respecifyImageStorage(contextVk));
ANGLE_TRY(ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
return refreshImageViews(contextVk);
}
angle::Result TextureVk::ensureRenderable(ContextVk *contextVk,
TextureUpdateResult *updateResultOut)
{
return ensureRenderableWithFormat(contextVk, getBaseLevelFormat(contextVk->getRenderer()),
updateResultOut);
}
angle::Result TextureVk::ensureRenderableWithFormat(ContextVk *contextVk,
const vk::Format &format,
TextureUpdateResult *updateResultOut)
{
if (mRequiredImageAccess == vk::ImageAccess::Renderable)
{
return angle::Result::Continue;
}
mRequiredImageAccess = vk::ImageAccess::Renderable;
if (!mImage)
{
// Later on when ensureImageAllocated() is called, it will ensure a renderable format is
// used.
return angle::Result::Continue;
}
if (!format.hasRenderableImageFallbackFormat())
{
// If there is no fallback format for renderable, then nothing to do.
return angle::Result::Continue;
}
// luminance/alpha format never fallback for rendering and if we ever do fallback, the
// following code may not handle it properly.
ASSERT(!format.getIntendedFormat().isLUMA());
angle::FormatID previousActualFormatID =
format.getActualImageFormatID(vk::ImageAccess::SampleOnly);
angle::FormatID actualFormatID = format.getActualImageFormatID(vk::ImageAccess::Renderable);
if (!mImage->valid())
{
// Immutable texture must already have a valid image
ASSERT(!mState.getImmutableFormat());
// If we have staged updates and they were encoded with different format, we need to flush
// out these staged updates. The respecifyImageStorage should handle reading back the
// flushed data and re-stage it with the new format.
angle::FormatID intendedFormatID = format.getIntendedFormatID();
gl::LevelIndex levelGLStart, levelGLEnd;
ImageMipLevels mipLevels;
if (mState.getImmutableFormat())
{
levelGLStart = gl::LevelIndex(0);
levelGLEnd = gl::LevelIndex(mState.getImmutableLevels());
mipLevels = ImageMipLevels::FullMipChainForGenerateMipmap;
}
else
{
levelGLStart = gl::LevelIndex(mState.getEffectiveBaseLevel());
levelGLEnd =
gl::LevelIndex(levelGLStart + getMipLevelCount(ImageMipLevels::EnabledLevels));
mipLevels = ImageMipLevels::EnabledLevels;
}
if (mImage->hasStagedImageUpdatesWithMismatchedFormat(levelGLStart, levelGLEnd,
actualFormatID))
{
angle::FormatID sampleOnlyFormatID =
format.getActualImageFormatID(vk::ImageAccess::SampleOnly);
ANGLE_TRY(initImage(contextVk, intendedFormatID, sampleOnlyFormatID, mipLevels));
}
else
{
// First try to convert any staged buffer updates from old format to new format using
// CPU.
ANGLE_TRY(mImage->reformatStagedBufferUpdates(contextVk, previousActualFormatID,
actualFormatID));
}
}
// Make sure we update mImageUsage bits
const bool imageWasInitialized = mImage->valid();
ANGLE_TRY(ensureImageAllocated(contextVk, format));
ANGLE_TRY(respecifyImageStorage(contextVk));
if (imageWasInitialized)
{
ANGLE_TRY(ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
ANGLE_TRY(refreshImageViews(contextVk));
}
if (updateResultOut != nullptr)
{
*updateResultOut = TextureUpdateResult::ImageRespecified;
}
return angle::Result::Continue;
}
angle::Result TextureVk::ensureRenderableIfCopyTextureCannotTransfer(
ContextVk *contextVk,
const gl::InternalFormat &dstFormat,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
TextureVk *source)
{
vk::Renderer *renderer = contextVk->getRenderer();
VkImageTiling srcTilingMode = source->getImage().getTilingMode();
const vk::Format &dstVkFormat = renderer->getFormat(dstFormat.sizedInternalFormat);
angle::FormatID dstFormatID = dstVkFormat.getActualImageFormatID(getRequiredImageAccess());
VkImageTiling dstTilingMode = getTilingMode();
if (!CanCopyWithTransferForCopyTexture(
renderer, source->getImage(), srcTilingMode, dstVkFormat.getIntendedFormatID(),
dstFormatID, dstTilingMode, unpackFlipY, unpackPremultiplyAlpha, unpackUnmultiplyAlpha))
{
ANGLE_TRY(ensureRenderableWithFormat(contextVk, dstVkFormat, nullptr));
}
return angle::Result::Continue;
}
angle::Result TextureVk::ensureRenderableIfCopyTexImageCannotTransfer(
ContextVk *contextVk,
const gl::InternalFormat &dstFormat,
gl::Framebuffer *source)
{
vk::Renderer *renderer = contextVk->getRenderer();
FramebufferVk *framebufferVk = vk::GetImpl(source);
RenderTargetVk *colorReadRT = framebufferVk->getColorReadRenderTarget();
angle::FormatID srcIntendedFormatID = colorReadRT->getImageIntendedFormatID();
angle::FormatID srcActualFormatID = colorReadRT->getImageActualFormatID();
VkImageTiling srcTilingMode = colorReadRT->getImageForCopy().getTilingMode();
const vk::Format &dstVkFormat = renderer->getFormat(dstFormat.sizedInternalFormat);
angle::FormatID dstIntendedFormatID = dstVkFormat.getIntendedFormatID();
angle::FormatID dstActualFormatID =
dstVkFormat.getActualImageFormatID(getRequiredImageAccess());
VkImageTiling destTilingMode = getTilingMode();
bool isViewportFlipY = contextVk->isViewportFlipEnabledForReadFBO();
if (!CanCopyWithTransferForTexImage(renderer, srcIntendedFormatID, srcActualFormatID,
srcTilingMode, dstIntendedFormatID, dstActualFormatID,
destTilingMode, isViewportFlipY))
{
ANGLE_TRY(ensureRenderableWithFormat(contextVk, dstVkFormat, nullptr));
}
return angle::Result::Continue;
}
void TextureVk::stageSelfAsSubresourceUpdates(ContextVk *contextVk)
{
// If we are calling stageSelfAsSubresourceUpdates(), the current image will be swapped
// to prevImage in stageSelfAsSubresourceUpdates(), therefore we need to release the
// imageViews first as we want to use current image.mUse to keep track of imageViews' resource
// lifetime.
releaseImageViews(contextVk);
// Make the image stage itself as updates to its levels.
ASSERT(!mImageSiblingSerial.valid());
mImage->stageSelfAsSubresourceUpdates(contextVk, mImage->getLevelCount(), mState.getType(),
mRedefinedLevels);
}
void TextureVk::updateCachedImageViewSerials()
{
mCachedImageViewSubresourceSerialSRGBDecode =
getImageViewSubresourceSerialImpl(vk::ImageViewColorspace::SRGB);
mCachedImageViewSubresourceSerialSkipDecode =
getImageViewSubresourceSerialImpl(vk::ImageViewColorspace::Linear);
}
} // namespace rx