Hash :
d2b659f9
Author :
Date :
2021-03-25T15:40:58
EGL: implement EGL_EXT_protected_content Context Add Validation check to Contexts and Surfaces Add Vulkan protected memory features and properties Add protected member to Surfaces and Contexts Implement hasProtectedContent in Vulkan Add QueueFamily helper, extent DeviceQueueMap Protected Swapchains always on for Android Add EGLProtectedContentTest Test: angle_end2end_test --gtest_filter=EGLProtectedContentTest Bug: angleproject:3965 Change-Id: I9352b1e594f71bb4e89cee7137a468940d186b1b Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2800413 Commit-Queue: Mohan Maiya <m.maiya@samsung.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Jamie Madill <jmadill@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
//
// Copyright 2018 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.
//
// vk_helpers:
// Helper utilitiy classes that manage Vulkan resources.
#ifndef LIBANGLE_RENDERER_VULKAN_VK_HELPERS_H_
#define LIBANGLE_RENDERER_VULKAN_VK_HELPERS_H_
#include "common/MemoryBuffer.h"
#include "libANGLE/renderer/vulkan/ResourceVk.h"
#include "libANGLE/renderer/vulkan/vk_cache_utils.h"
namespace gl
{
class ImageIndex;
} // namespace gl
namespace rx
{
namespace vk
{
constexpr VkBufferUsageFlags kVertexBufferUsageFlags =
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
constexpr VkBufferUsageFlags kIndexBufferUsageFlags =
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
constexpr VkBufferUsageFlags kIndirectBufferUsageFlags =
VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
constexpr size_t kVertexBufferAlignment = 4;
constexpr size_t kIndexBufferAlignment = 4;
constexpr size_t kIndirectBufferAlignment = 4;
constexpr VkBufferUsageFlags kStagingBufferFlags =
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
constexpr size_t kStagingBufferSize = 1024 * 16;
constexpr VkImageCreateFlags kVkImageCreateFlagsNone = 0;
using StagingBufferOffsetArray = std::array<VkDeviceSize, 2>;
struct TextureUnit final
{
TextureVk *texture;
const SamplerHelper *sampler;
GLenum srgbDecode;
};
// A dynamic buffer is conceptually an infinitely long buffer. Each time you write to the buffer,
// you will always write to a previously unused portion. After a series of writes, you must flush
// the buffer data to the device. Buffer lifetime currently assumes that each new allocation will
// last as long or longer than each prior allocation.
//
// Dynamic buffers are used to implement a variety of data streaming operations in Vulkan, such
// as for immediate vertex array and element array data, uniform updates, and other dynamic data.
//
// Internally dynamic buffers keep a collection of VkBuffers. When we write past the end of a
// currently active VkBuffer we keep it until it is no longer in use. We then mark it available
// for future allocations in a free list.
class BufferHelper;
using BufferHelperPointerVector = std::vector<std::unique_ptr<BufferHelper>>;
enum class DynamicBufferPolicy
{
// Used where future allocations from the dynamic buffer are unlikely, so it's best to free the
// memory when the allocated buffers are no longer in use.
OneShotUse,
// Used where multiple small allocations are made every frame, so it's worth keeping the free
// buffers around to avoid release/reallocation.
FrequentSmallAllocations,
// Used where bursts of allocation happen occasionally, but the steady state may make
// allocations every now and then. In that case, a limited number of buffers are retained.
SporadicTextureUpload,
};
class DynamicBuffer : angle::NonCopyable
{
public:
DynamicBuffer();
DynamicBuffer(DynamicBuffer &&other);
~DynamicBuffer();
// Init is called after the buffer creation so that the alignment can be specified later.
void init(RendererVk *renderer,
VkBufferUsageFlags usage,
size_t alignment,
size_t initialSize,
bool hostVisible,
DynamicBufferPolicy policy);
// Init that gives the ability to pass in specified memory property flags for the buffer.
void initWithFlags(RendererVk *renderer,
VkBufferUsageFlags usage,
size_t alignment,
size_t initialSize,
VkMemoryPropertyFlags memoryProperty,
DynamicBufferPolicy policy);
// This call will allocate a new region at the end of the current buffer. If it can't find
// enough space in the current buffer, it returns false. This gives caller a chance to deal with
// buffer switch that may occur with allocate call.
bool allocateFromCurrentBuffer(size_t sizeInBytes, uint8_t **ptrOut, VkDeviceSize *offsetOut);
// This call will allocate a new region at the end of the buffer. It internally may trigger
// a new buffer to be created (which is returned in the optional parameter
// `newBufferAllocatedOut`). The new region will be in the returned buffer at given offset. If
// a memory pointer is given, the buffer will be automatically map()ed.
angle::Result allocateWithAlignment(ContextVk *contextVk,
size_t sizeInBytes,
size_t alignment,
uint8_t **ptrOut,
VkBuffer *bufferOut,
VkDeviceSize *offsetOut,
bool *newBufferAllocatedOut);
// Allocate with default alignment
angle::Result allocate(ContextVk *contextVk,
size_t sizeInBytes,
uint8_t **ptrOut,
VkBuffer *bufferOut,
VkDeviceSize *offsetOut,
bool *newBufferAllocatedOut)
{
return allocateWithAlignment(contextVk, sizeInBytes, mAlignment, ptrOut, bufferOut,
offsetOut, newBufferAllocatedOut);
}
// After a sequence of writes, call flush to ensure the data is visible to the device.
angle::Result flush(ContextVk *contextVk);
// After a sequence of writes, call invalidate to ensure the data is visible to the host.
angle::Result invalidate(ContextVk *contextVk);
// This releases resources when they might currently be in use.
void release(RendererVk *renderer);
// This releases all the buffers that have been allocated since this was last called.
void releaseInFlightBuffers(ContextVk *contextVk);
// This adds inflight buffers to the context's mResourceUseList and then releases them
void releaseInFlightBuffersToResourceUseList(ContextVk *contextVk);
// This frees resources immediately.
void destroy(RendererVk *renderer);
BufferHelper *getCurrentBuffer() const { return mBuffer.get(); }
// **Accumulate** an alignment requirement. A dynamic buffer is used as the staging buffer for
// image uploads, which can contain updates to unrelated mips, possibly with different formats.
// The staging buffer should have an alignment that can satisfy all those formats, i.e. it's the
// lcm of all alignments set in its lifetime.
void requireAlignment(RendererVk *renderer, size_t alignment);
size_t getAlignment() const { return mAlignment; }
// For testing only!
void setMinimumSizeForTesting(size_t minSize);
bool isCoherent() const
{
return (mMemoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) != 0;
}
bool valid() const { return mSize != 0; }
private:
void reset();
angle::Result allocateNewBuffer(ContextVk *contextVk);
VkBufferUsageFlags mUsage;
bool mHostVisible;
DynamicBufferPolicy mPolicy;
size_t mInitialSize;
std::unique_ptr<BufferHelper> mBuffer;
uint32_t mNextAllocationOffset;
uint32_t mLastFlushOrInvalidateOffset;
size_t mSize;
size_t mAlignment;
VkMemoryPropertyFlags mMemoryPropertyFlags;
BufferHelperPointerVector mInFlightBuffers;
BufferHelperPointerVector mBufferFreeList;
};
// Based off of the DynamicBuffer class, DynamicShadowBuffer provides
// a similar conceptually infinitely long buffer that will only be written
// to and read by the CPU. This can be used to provide CPU cached copies of
// GPU-read only buffers. The value add here is that when an app requests
// CPU access to a buffer we can fullfil such a request in O(1) time since
// we don't need to wait for GPU to be done with in-flight commands.
//
// The hidden cost here is that any operation that updates a buffer, either
// through a buffer sub data update or a buffer-to-buffer copy will have an
// additional overhead of having to update its CPU only buffer
class DynamicShadowBuffer : public angle::NonCopyable
{
public:
DynamicShadowBuffer();
DynamicShadowBuffer(DynamicShadowBuffer &&other);
~DynamicShadowBuffer();
// Initialize the DynamicShadowBuffer.
void init(size_t initialSize);
// Returns whether this DynamicShadowBuffer is active
ANGLE_INLINE bool valid() { return (mSize != 0); }
// This call will actually allocate a new CPU only memory from the heap.
// The size can be different than the one specified during `init`.
angle::Result allocate(size_t sizeInBytes);
ANGLE_INLINE void updateData(const uint8_t *data, size_t size, size_t offset)
{
ASSERT(!mBuffer.empty());
// Memcopy data into the buffer
memcpy((mBuffer.data() + offset), data, size);
}
// Map the CPU only buffer and return the pointer. We map the entire buffer for now.
ANGLE_INLINE void map(size_t offset, void **mapPtr)
{
ASSERT(mapPtr);
ASSERT(!mBuffer.empty());
*mapPtr = mBuffer.data() + offset;
}
// Unmap the CPU only buffer, NOOP for now
ANGLE_INLINE void unmap() {}
// This releases resources when they might currently be in use.
void release();
// This frees resources immediately.
void destroy(VkDevice device);
ANGLE_INLINE uint8_t *getCurrentBuffer()
{
ASSERT(!mBuffer.empty());
return mBuffer.data();
}
ANGLE_INLINE const uint8_t *getCurrentBuffer() const
{
ASSERT(!mBuffer.empty());
return mBuffer.data();
}
private:
void reset();
size_t mInitialSize;
size_t mSize;
angle::MemoryBuffer mBuffer;
};
// Uses DescriptorPool to allocate descriptor sets as needed. If a descriptor pool becomes full, we
// allocate new pools internally as needed. RendererVk takes care of the lifetime of the discarded
// pools. Note that we used a fixed layout for descriptor pools in ANGLE.
// Shared handle to a descriptor pool. Each helper is allocated from the dynamic descriptor pool.
// Can be used to share descriptor pools between multiple ProgramVks and the ContextVk.
class DescriptorPoolHelper : public Resource
{
public:
DescriptorPoolHelper();
~DescriptorPoolHelper() override;
bool valid() { return mDescriptorPool.valid(); }
bool hasCapacity(uint32_t descriptorSetCount) const;
angle::Result init(ContextVk *contextVk,
const std::vector<VkDescriptorPoolSize> &poolSizesIn,
uint32_t maxSets);
void destroy(VkDevice device);
void release(ContextVk *contextVk);
angle::Result allocateSets(ContextVk *contextVk,
const VkDescriptorSetLayout *descriptorSetLayout,
uint32_t descriptorSetCount,
VkDescriptorSet *descriptorSetsOut);
private:
uint32_t mFreeDescriptorSets;
DescriptorPool mDescriptorPool;
};
using RefCountedDescriptorPoolHelper = RefCounted<DescriptorPoolHelper>;
using RefCountedDescriptorPoolBinding = BindingPointer<DescriptorPoolHelper>;
class DynamicDescriptorPool final : angle::NonCopyable
{
public:
DynamicDescriptorPool();
~DynamicDescriptorPool();
// The DynamicDescriptorPool only handles one pool size at this time.
// Note that setSizes[i].descriptorCount is expected to be the number of descriptors in
// an individual set. The pool size will be calculated accordingly.
angle::Result init(ContextVk *contextVk,
const VkDescriptorPoolSize *setSizes,
size_t setSizeCount,
VkDescriptorSetLayout descriptorSetLayout);
void destroy(VkDevice device);
void release(ContextVk *contextVk);
// We use the descriptor type to help count the number of free sets.
// By convention, sets are indexed according to the constants in vk_cache_utils.h.
ANGLE_INLINE angle::Result allocateSets(ContextVk *contextVk,
const VkDescriptorSetLayout *descriptorSetLayout,
uint32_t descriptorSetCount,
RefCountedDescriptorPoolBinding *bindingOut,
VkDescriptorSet *descriptorSetsOut)
{
bool ignoreNewPoolAllocated;
return allocateSetsAndGetInfo(contextVk, descriptorSetLayout, descriptorSetCount,
bindingOut, descriptorSetsOut, &ignoreNewPoolAllocated);
}
// We use the descriptor type to help count the number of free sets.
// By convention, sets are indexed according to the constants in vk_cache_utils.h.
angle::Result allocateSetsAndGetInfo(ContextVk *contextVk,
const VkDescriptorSetLayout *descriptorSetLayout,
uint32_t descriptorSetCount,
RefCountedDescriptorPoolBinding *bindingOut,
VkDescriptorSet *descriptorSetsOut,
bool *newPoolAllocatedOut);
// For testing only!
static uint32_t GetMaxSetsPerPoolForTesting();
static void SetMaxSetsPerPoolForTesting(uint32_t maxSetsPerPool);
static uint32_t GetMaxSetsPerPoolMultiplierForTesting();
static void SetMaxSetsPerPoolMultiplierForTesting(uint32_t maxSetsPerPool);
private:
angle::Result allocateNewPool(ContextVk *contextVk);
static constexpr uint32_t kMaxSetsPerPoolMax = 512;
static uint32_t mMaxSetsPerPool;
static uint32_t mMaxSetsPerPoolMultiplier;
size_t mCurrentPoolIndex;
std::vector<RefCountedDescriptorPoolHelper *> mDescriptorPools;
std::vector<VkDescriptorPoolSize> mPoolSizes;
// This cached handle is used for verifying the layout being used to allocate descriptor sets
// from the pool matches the layout that the pool was created for, to ensure that the free
// descriptor count is accurate and new pools are created appropriately.
VkDescriptorSetLayout mCachedDescriptorSetLayout;
};
template <typename Pool>
class DynamicallyGrowingPool : angle::NonCopyable
{
public:
DynamicallyGrowingPool();
virtual ~DynamicallyGrowingPool();
bool isValid() { return mPoolSize > 0; }
protected:
angle::Result initEntryPool(Context *contextVk, uint32_t poolSize);
void destroyEntryPool();
// Checks to see if any pool is already free, in which case it sets it as current pool and
// returns true.
bool findFreeEntryPool(ContextVk *contextVk);
// Allocates a new entry and initializes it with the given pool.
angle::Result allocateNewEntryPool(ContextVk *contextVk, Pool &&pool);
// Called by the implementation whenever an entry is freed.
void onEntryFreed(ContextVk *contextVk, size_t poolIndex);
// The pool size, to know when a pool is completely freed.
uint32_t mPoolSize;
std::vector<Pool> mPools;
struct PoolStats
{
// A count corresponding to each pool indicating how many of its allocated entries
// have been freed. Once that value reaches mPoolSize for each pool, that pool is considered
// free and reusable. While keeping a bitset would allow allocation of each index, the
// slight runtime overhead of finding free indices is not worth the slight memory overhead
// of creating new pools when unnecessary.
uint32_t freedCount;
// The serial of the renderer is stored on each object free to make sure no
// new allocations are made from the pool until it's not in use.
Serial serial;
};
std::vector<PoolStats> mPoolStats;
// Index into mPools indicating pool we are currently allocating from.
size_t mCurrentPool;
// Index inside mPools[mCurrentPool] indicating which index can be allocated next.
uint32_t mCurrentFreeEntry;
};
// DynamicQueryPool allocates indices out of QueryPool as needed. Once a QueryPool is exhausted,
// another is created. The query pools live permanently, but are recycled as indices get freed.
// These are arbitrary default sizes for query pools.
constexpr uint32_t kDefaultOcclusionQueryPoolSize = 64;
constexpr uint32_t kDefaultTimestampQueryPoolSize = 64;
constexpr uint32_t kDefaultTransformFeedbackQueryPoolSize = 128;
constexpr uint32_t kDefaultPrimitivesGeneratedQueryPoolSize = 128;
class QueryHelper;
class DynamicQueryPool final : public DynamicallyGrowingPool<QueryPool>
{
public:
DynamicQueryPool();
~DynamicQueryPool() override;
angle::Result init(ContextVk *contextVk, VkQueryType type, uint32_t poolSize);
void destroy(VkDevice device);
angle::Result allocateQuery(ContextVk *contextVk, QueryHelper *queryOut, uint32_t queryCount);
void freeQuery(ContextVk *contextVk, QueryHelper *query);
const QueryPool &getQueryPool(size_t index) const { return mPools[index]; }
private:
angle::Result allocateNewPool(ContextVk *contextVk);
// Information required to create new query pools
VkQueryType mQueryType;
};
// Stores the result of a Vulkan query call. XFB queries in particular store two result values.
class QueryResult final
{
public:
QueryResult(uint32_t intsPerResult) : mIntsPerResult(intsPerResult), mResults{} {}
void operator+=(const QueryResult &rhs)
{
mResults[0] += rhs.mResults[0];
mResults[1] += rhs.mResults[1];
}
size_t getDataSize() const { return mIntsPerResult * sizeof(uint64_t); }
void setResults(uint64_t *results, uint32_t queryCount);
uint64_t getResult(size_t index) const
{
ASSERT(index < mIntsPerResult);
return mResults[index];
}
static constexpr size_t kDefaultResultIndex = 0;
static constexpr size_t kTransformFeedbackPrimitivesWrittenIndex = 0;
static constexpr size_t kPrimitivesGeneratedIndex = 1;
private:
uint32_t mIntsPerResult;
std::array<uint64_t, 2> mResults;
};
// Queries in Vulkan are identified by the query pool and an index for a query within that pool.
// Unlike other pools, such as descriptor pools where an allocation returns an independent object
// from the pool, the query allocations are not done through a Vulkan function and are only an
// integer index.
//
// Furthermore, to support arbitrarily large number of queries, DynamicQueryPool creates query pools
// of a fixed size as needed and allocates indices within those pools.
//
// The QueryHelper class below keeps the pool and index pair together. For multiview, multiple
// consecutive query indices are implicitly written to by the driver, so the query count is
// additionally kept.
class QueryHelper final : public Resource
{
public:
QueryHelper();
~QueryHelper() override;
QueryHelper(QueryHelper &&rhs);
QueryHelper &operator=(QueryHelper &&rhs);
void init(const DynamicQueryPool *dynamicQueryPool,
const size_t queryPoolIndex,
uint32_t query,
uint32_t queryCount);
void deinit();
bool valid() const { return mDynamicQueryPool != nullptr; }
// Begin/end queries. These functions break the render pass.
angle::Result beginQuery(ContextVk *contextVk);
angle::Result endQuery(ContextVk *contextVk);
// Begin/end queries within a started render pass.
angle::Result beginRenderPassQuery(ContextVk *contextVk);
void endRenderPassQuery(ContextVk *contextVk);
angle::Result flushAndWriteTimestamp(ContextVk *contextVk);
// When syncing gpu/cpu time, main thread accesses primary directly
void writeTimestampToPrimary(ContextVk *contextVk, PrimaryCommandBuffer *primary);
// All other timestamp accesses should be made on outsideRenderPassCommandBuffer
void writeTimestamp(ContextVk *contextVk, CommandBuffer *outsideRenderPassCommandBuffer);
// Whether this query helper has generated and submitted any commands.
bool hasSubmittedCommands() const;
angle::Result getUint64ResultNonBlocking(ContextVk *contextVk,
QueryResult *resultOut,
bool *availableOut);
angle::Result getUint64Result(ContextVk *contextVk, QueryResult *resultOut);
private:
friend class DynamicQueryPool;
const QueryPool &getQueryPool() const
{
ASSERT(valid());
return mDynamicQueryPool->getQueryPool(mQueryPoolIndex);
}
// Reset needs to always be done outside a render pass, which may be different from the
// passed-in command buffer (which could be the render pass').
void beginQueryImpl(ContextVk *contextVk,
CommandBuffer *resetCommandBuffer,
CommandBuffer *commandBuffer);
void endQueryImpl(ContextVk *contextVk, CommandBuffer *commandBuffer);
VkResult getResultImpl(ContextVk *contextVk,
const VkQueryResultFlags flags,
QueryResult *resultOut);
const DynamicQueryPool *mDynamicQueryPool;
size_t mQueryPoolIndex;
uint32_t mQuery;
uint32_t mQueryCount;
};
// DynamicSemaphorePool allocates semaphores as needed. It uses a std::vector
// as a pool to allocate many semaphores at once. The pools live permanently,
// but are recycled as semaphores get freed.
// These are arbitrary default sizes for semaphore pools.
constexpr uint32_t kDefaultSemaphorePoolSize = 64;
class SemaphoreHelper;
class DynamicSemaphorePool final : public DynamicallyGrowingPool<std::vector<Semaphore>>
{
public:
DynamicSemaphorePool();
~DynamicSemaphorePool() override;
angle::Result init(ContextVk *contextVk, uint32_t poolSize);
void destroy(VkDevice device);
bool isValid() { return mPoolSize > 0; }
// autoFree can be used to allocate a semaphore that's expected to be freed at the end of the
// frame. This renders freeSemaphore unnecessary and saves an eventual search.
angle::Result allocateSemaphore(ContextVk *contextVk, SemaphoreHelper *semaphoreOut);
void freeSemaphore(ContextVk *contextVk, SemaphoreHelper *semaphore);
private:
angle::Result allocateNewPool(ContextVk *contextVk);
};
// Semaphores that are allocated from the semaphore pool are encapsulated in a helper object,
// keeping track of where in the pool they are allocated from.
class SemaphoreHelper final : angle::NonCopyable
{
public:
SemaphoreHelper();
~SemaphoreHelper();
SemaphoreHelper(SemaphoreHelper &&other);
SemaphoreHelper &operator=(SemaphoreHelper &&other);
void init(const size_t semaphorePoolIndex, const Semaphore *semaphore);
void deinit();
const Semaphore *getSemaphore() const { return mSemaphore; }
// Used only by DynamicSemaphorePool.
size_t getSemaphorePoolIndex() const { return mSemaphorePoolIndex; }
private:
size_t mSemaphorePoolIndex;
const Semaphore *mSemaphore;
};
// This class' responsibility is to create index buffers needed to support line loops in Vulkan.
// In the setup phase of drawing, the createIndexBuffer method should be called with the
// current draw call parameters. If an element array buffer is bound for an indexed draw, use
// createIndexBufferFromElementArrayBuffer.
//
// If the user wants to draw a loop between [v1, v2, v3], we will create an indexed buffer with
// these indexes: [0, 1, 2, 3, 0] to emulate the loop.
class LineLoopHelper final : angle::NonCopyable
{
public:
LineLoopHelper(RendererVk *renderer);
~LineLoopHelper();
angle::Result getIndexBufferForDrawArrays(ContextVk *contextVk,
uint32_t clampedVertexCount,
GLint firstVertex,
BufferHelper **bufferOut,
VkDeviceSize *offsetOut);
angle::Result getIndexBufferForElementArrayBuffer(ContextVk *contextVk,
BufferVk *elementArrayBufferVk,
gl::DrawElementsType glIndexType,
int indexCount,
intptr_t elementArrayOffset,
BufferHelper **bufferOut,
VkDeviceSize *bufferOffsetOut,
uint32_t *indexCountOut);
angle::Result streamIndices(ContextVk *contextVk,
gl::DrawElementsType glIndexType,
GLsizei indexCount,
const uint8_t *srcPtr,
BufferHelper **bufferOut,
VkDeviceSize *bufferOffsetOut,
uint32_t *indexCountOut);
angle::Result streamIndicesIndirect(ContextVk *contextVk,
gl::DrawElementsType glIndexType,
BufferHelper *indexBuffer,
VkDeviceSize indexBufferOffset,
BufferHelper *indirectBuffer,
VkDeviceSize indirectBufferOffset,
BufferHelper **indexBufferOut,
VkDeviceSize *indexBufferOffsetOut,
BufferHelper **indirectBufferOut,
VkDeviceSize *indirectBufferOffsetOut);
angle::Result streamArrayIndirect(ContextVk *contextVk,
size_t vertexCount,
BufferHelper *arrayIndirectBuffer,
VkDeviceSize arrayIndirectBufferOffset,
BufferHelper **indexBufferOut,
VkDeviceSize *indexBufferOffsetOut,
BufferHelper **indexIndirectBufferOut,
VkDeviceSize *indexIndirectBufferOffsetOut);
void release(ContextVk *contextVk);
void destroy(RendererVk *renderer);
static void Draw(uint32_t count, uint32_t baseVertex, CommandBuffer *commandBuffer);
private:
DynamicBuffer mDynamicIndexBuffer;
DynamicBuffer mDynamicIndirectBuffer;
};
// This defines enum for VkPipelineStageFlagBits so that we can use it to compare and index into
// array.
enum class PipelineStage : uint16_t
{
// Bellow are ordered based on Graphics Pipeline Stages
TopOfPipe = 0,
DrawIndirect = 1,
VertexInput = 2,
VertexShader = 3,
GeometryShader = 4,
TransformFeedback = 5,
EarlyFragmentTest = 6,
FragmentShader = 7,
LateFragmentTest = 8,
ColorAttachmentOutput = 9,
// Compute specific pipeline Stage
ComputeShader = 10,
// Transfer specific pipeline Stage
Transfer = 11,
BottomOfPipe = 12,
// Host specific pipeline stage
Host = 13,
InvalidEnum = 14,
EnumCount = InvalidEnum,
};
using PipelineStagesMask = angle::PackedEnumBitSet<PipelineStage, uint16_t>;
PipelineStage GetPipelineStage(gl::ShaderType stage);
// This wraps data and API for vkCmdPipelineBarrier call
class PipelineBarrier : angle::NonCopyable
{
public:
PipelineBarrier()
: mSrcStageMask(0),
mDstStageMask(0),
mMemoryBarrierSrcAccess(0),
mMemoryBarrierDstAccess(0),
mImageMemoryBarriers()
{}
~PipelineBarrier() = default;
bool isEmpty() const { return mImageMemoryBarriers.empty() && mMemoryBarrierDstAccess == 0; }
void execute(PrimaryCommandBuffer *primary)
{
if (isEmpty())
{
return;
}
// Issue vkCmdPipelineBarrier call
VkMemoryBarrier memoryBarrier = {};
uint32_t memoryBarrierCount = 0;
if (mMemoryBarrierDstAccess != 0)
{
memoryBarrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
memoryBarrier.srcAccessMask = mMemoryBarrierSrcAccess;
memoryBarrier.dstAccessMask = mMemoryBarrierDstAccess;
memoryBarrierCount++;
}
primary->pipelineBarrier(
mSrcStageMask, mDstStageMask, 0, memoryBarrierCount, &memoryBarrier, 0, nullptr,
static_cast<uint32_t>(mImageMemoryBarriers.size()), mImageMemoryBarriers.data());
reset();
}
void executeIndividually(PrimaryCommandBuffer *primary)
{
if (isEmpty())
{
return;
}
// Issue vkCmdPipelineBarrier call
VkMemoryBarrier memoryBarrier = {};
uint32_t memoryBarrierCount = 0;
if (mMemoryBarrierDstAccess != 0)
{
memoryBarrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
memoryBarrier.srcAccessMask = mMemoryBarrierSrcAccess;
memoryBarrier.dstAccessMask = mMemoryBarrierDstAccess;
memoryBarrierCount++;
}
for (const VkImageMemoryBarrier &imageBarrier : mImageMemoryBarriers)
{
primary->pipelineBarrier(mSrcStageMask, mDstStageMask, 0, memoryBarrierCount,
&memoryBarrier, 0, nullptr, 1, &imageBarrier);
}
reset();
}
// merge two barriers into one
void merge(PipelineBarrier *other)
{
mSrcStageMask |= other->mSrcStageMask;
mDstStageMask |= other->mDstStageMask;
mMemoryBarrierSrcAccess |= other->mMemoryBarrierSrcAccess;
mMemoryBarrierDstAccess |= other->mMemoryBarrierDstAccess;
mImageMemoryBarriers.insert(mImageMemoryBarriers.end(), other->mImageMemoryBarriers.begin(),
other->mImageMemoryBarriers.end());
other->reset();
}
void mergeMemoryBarrier(VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask,
VkFlags srcAccess,
VkFlags dstAccess)
{
mSrcStageMask |= srcStageMask;
mDstStageMask |= dstStageMask;
mMemoryBarrierSrcAccess |= srcAccess;
mMemoryBarrierDstAccess |= dstAccess;
}
void mergeImageBarrier(VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask,
const VkImageMemoryBarrier &imageMemoryBarrier)
{
ASSERT(imageMemoryBarrier.pNext == nullptr);
mSrcStageMask |= srcStageMask;
mDstStageMask |= dstStageMask;
mImageMemoryBarriers.push_back(imageMemoryBarrier);
}
void reset()
{
mSrcStageMask = 0;
mDstStageMask = 0;
mMemoryBarrierSrcAccess = 0;
mMemoryBarrierDstAccess = 0;
mImageMemoryBarriers.clear();
}
void addDiagnosticsString(std::ostringstream &out) const;
private:
VkPipelineStageFlags mSrcStageMask;
VkPipelineStageFlags mDstStageMask;
VkFlags mMemoryBarrierSrcAccess;
VkFlags mMemoryBarrierDstAccess;
std::vector<VkImageMemoryBarrier> mImageMemoryBarriers;
};
using PipelineBarrierArray = angle::PackedEnumMap<PipelineStage, PipelineBarrier>;
class FramebufferHelper;
class BufferMemory : angle::NonCopyable
{
public:
BufferMemory();
~BufferMemory();
angle::Result initExternal(GLeglClientBufferEXT clientBuffer);
angle::Result init();
void destroy(RendererVk *renderer);
angle::Result map(ContextVk *contextVk, VkDeviceSize size, uint8_t **ptrOut)
{
if (mMappedMemory == nullptr)
{
ANGLE_TRY(mapImpl(contextVk, size));
}
*ptrOut = mMappedMemory;
return angle::Result::Continue;
}
void unmap(RendererVk *renderer);
void flush(RendererVk *renderer,
VkMemoryMapFlags memoryPropertyFlags,
VkDeviceSize offset,
VkDeviceSize size);
void invalidate(RendererVk *renderer,
VkMemoryMapFlags memoryPropertyFlags,
VkDeviceSize offset,
VkDeviceSize size);
bool isExternalBuffer() const { return mClientBuffer != nullptr; }
uint8_t *getMappedMemory() const { return mMappedMemory; }
DeviceMemory *getExternalMemoryObject() { return &mExternalMemory; }
Allocation *getMemoryObject() { return &mAllocation; }
private:
angle::Result mapImpl(ContextVk *contextVk, VkDeviceSize size);
Allocation mAllocation; // use mAllocation if isExternalBuffer() is false
DeviceMemory mExternalMemory; // use mExternalMemory if isExternalBuffer() is true
GLeglClientBufferEXT mClientBuffer;
uint8_t *mMappedMemory;
};
class BufferHelper final : public Resource
{
public:
BufferHelper();
~BufferHelper() override;
angle::Result init(ContextVk *contextVk,
const VkBufferCreateInfo &createInfo,
VkMemoryPropertyFlags memoryPropertyFlags);
angle::Result initExternal(ContextVk *contextVk,
VkMemoryPropertyFlags memoryProperties,
const VkBufferCreateInfo &requestedCreateInfo,
GLeglClientBufferEXT clientBuffer);
void destroy(RendererVk *renderer);
void release(RendererVk *renderer);
BufferSerial getBufferSerial() const { return mSerial; }
bool valid() const { return mBuffer.valid(); }
const Buffer &getBuffer() const { return mBuffer; }
VkDeviceSize getSize() const { return mSize; }
uint8_t *getMappedMemory() const
{
ASSERT(isMapped());
return mMemory.getMappedMemory();
}
bool isHostVisible() const
{
return (mMemoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0;
}
bool isCoherent() const
{
return (mMemoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) != 0;
}
bool isMapped() const { return mMemory.getMappedMemory() != nullptr; }
bool isExternalBuffer() const { return mMemory.isExternalBuffer(); }
// Also implicitly sets up the correct barriers.
angle::Result copyFromBuffer(ContextVk *contextVk,
BufferHelper *srcBuffer,
uint32_t regionCount,
const VkBufferCopy *copyRegions);
angle::Result map(ContextVk *contextVk, uint8_t **ptrOut)
{
return mMemory.map(contextVk, mSize, ptrOut);
}
angle::Result mapWithOffset(ContextVk *contextVk, uint8_t **ptrOut, size_t offset)
{
uint8_t *mapBufPointer;
ANGLE_TRY(mMemory.map(contextVk, mSize, &mapBufPointer));
*ptrOut = mapBufPointer + offset;
return angle::Result::Continue;
}
void unmap(RendererVk *renderer);
// After a sequence of writes, call flush to ensure the data is visible to the device.
angle::Result flush(RendererVk *renderer, VkDeviceSize offset, VkDeviceSize size);
// After a sequence of writes, call invalidate to ensure the data is visible to the host.
angle::Result invalidate(RendererVk *renderer, VkDeviceSize offset, VkDeviceSize size);
void changeQueue(uint32_t newQueueFamilyIndex, CommandBuffer *commandBuffer);
// Performs an ownership transfer from an external instance or API.
void acquireFromExternal(ContextVk *contextVk,
uint32_t externalQueueFamilyIndex,
uint32_t rendererQueueFamilyIndex,
CommandBuffer *commandBuffer);
// Performs an ownership transfer to an external instance or API.
void releaseToExternal(ContextVk *contextVk,
uint32_t rendererQueueFamilyIndex,
uint32_t externalQueueFamilyIndex,
CommandBuffer *commandBuffer);
// Returns true if the image is owned by an external API or instance.
bool isReleasedToExternal() const;
bool recordReadBarrier(VkAccessFlags readAccessType,
VkPipelineStageFlags readStage,
PipelineBarrier *barrier);
bool recordWriteBarrier(VkAccessFlags writeAccessType,
VkPipelineStageFlags writeStage,
PipelineBarrier *barrier);
private:
angle::Result initializeNonZeroMemory(Context *context, VkDeviceSize size);
// Vulkan objects.
Buffer mBuffer;
BufferMemory mMemory;
// Cached properties.
VkMemoryPropertyFlags mMemoryPropertyFlags;
VkDeviceSize mSize;
uint32_t mCurrentQueueFamilyIndex;
// For memory barriers.
VkFlags mCurrentWriteAccess;
VkFlags mCurrentReadAccess;
VkPipelineStageFlags mCurrentWriteStages;
VkPipelineStageFlags mCurrentReadStages;
BufferSerial mSerial;
};
enum class BufferAccess
{
Read,
Write,
};
enum class AliasingMode
{
Allowed,
Disallowed,
};
// Stores clear value In packed attachment index
class PackedClearValuesArray final
{
public:
PackedClearValuesArray();
~PackedClearValuesArray();
PackedClearValuesArray(const PackedClearValuesArray &other);
PackedClearValuesArray &operator=(const PackedClearValuesArray &rhs);
void store(PackedAttachmentIndex index,
VkImageAspectFlags aspectFlags,
const VkClearValue &clearValue);
void storeNoDepthStencil(PackedAttachmentIndex index, const VkClearValue &clearValue);
const VkClearValue &operator[](PackedAttachmentIndex index) const
{
return mValues[index.get()];
}
const VkClearValue *data() const { return mValues.data(); }
private:
gl::AttachmentArray<VkClearValue> mValues;
};
// Stores ImageHelpers In packed attachment index
class PackedImageAttachmentArray final
{
public:
PackedImageAttachmentArray() : mImages{} {}
~PackedImageAttachmentArray() = default;
ImageHelper *&operator[](PackedAttachmentIndex index) { return mImages[index.get()]; }
void reset() { mImages.fill(nullptr); }
private:
gl::AttachmentArray<ImageHelper *> mImages;
};
// The following are used to help track the state of an invalidated attachment.
// This value indicates an "infinite" CmdSize that is not valid for comparing
constexpr uint32_t kInfiniteCmdSize = 0xFFFFFFFF;
// CommandBufferHelper (CBH) class wraps ANGLE's custom command buffer
// class, SecondaryCommandBuffer. This provides a way to temporarily
// store Vulkan commands that be can submitted in-line to a primary
// command buffer at a later time.
// The current plan is for the main ANGLE thread to record commands
// into the CBH and then pass the CBH off to a worker thread that will
// process the commands into a primary command buffer and then submit
// those commands to the queue.
class CommandBufferHelper : angle::NonCopyable
{
public:
CommandBufferHelper();
~CommandBufferHelper();
// General Functions (non-renderPass specific)
void initialize(bool isRenderPassCommandBuffer);
void bufferRead(ContextVk *contextVk,
VkAccessFlags readAccessType,
PipelineStage readStage,
BufferHelper *buffer);
void bufferWrite(ContextVk *contextVk,
VkAccessFlags writeAccessType,
PipelineStage writeStage,
AliasingMode aliasingMode,
BufferHelper *buffer);
void imageRead(ContextVk *contextVk,
VkImageAspectFlags aspectFlags,
ImageLayout imageLayout,
ImageHelper *image);
void imageWrite(ContextVk *contextVk,
gl::LevelIndex level,
uint32_t layerStart,
uint32_t layerCount,
VkImageAspectFlags aspectFlags,
ImageLayout imageLayout,
AliasingMode aliasingMode,
ImageHelper *image);
void colorImagesDraw(ResourceUseList *resourceUseList,
ImageHelper *image,
ImageHelper *resolveImage,
PackedAttachmentIndex packedAttachmentIndex);
void depthStencilImagesDraw(ResourceUseList *resourceUseList,
gl::LevelIndex level,
uint32_t layerStart,
uint32_t layerCount,
ImageHelper *image,
ImageHelper *resolveImage);
CommandBuffer &getCommandBuffer() { return mCommandBuffer; }
angle::Result flushToPrimary(const angle::FeaturesVk &features,
PrimaryCommandBuffer *primary,
const RenderPass *renderPass);
void executeBarriers(const angle::FeaturesVk &features, PrimaryCommandBuffer *primary);
void setHasRenderPass(bool hasRenderPass) { mIsRenderPassCommandBuffer = hasRenderPass; }
// The markOpen and markClosed functions are to aid in proper use of the CommandBufferHelper.
// saw invalid use due to threading issues that can be easily caught by marking when it's safe
// (open) to write to the commandbuffer.
#if defined(ANGLE_ENABLE_ASSERTS)
void markOpen() { mCommandBuffer.open(); }
void markClosed() { mCommandBuffer.close(); }
#else
void markOpen() {}
void markClosed() {}
#endif
void reset();
// Returns true if we have no work to execute. For renderpass command buffer, even if the
// underlying command buffer is empty, we may still have a renderpass with an empty command
// buffer just to do the clear.
bool empty() const
{
return mIsRenderPassCommandBuffer ? !mRenderPassStarted : mCommandBuffer.empty();
}
// RenderPass related functions. This is equivalent to !empty(), but only when you know this is
// a RenderPass command buffer
bool started() const
{
ASSERT(mIsRenderPassCommandBuffer);
return mRenderPassStarted;
}
// Finalize the layout if image has any deferred layout transition.
void finalizeImageLayout(Context *context, const ImageHelper *image);
void beginRenderPass(const Framebuffer &framebuffer,
const gl::Rectangle &renderArea,
const RenderPassDesc &renderPassDesc,
const AttachmentOpsArray &renderPassAttachmentOps,
const vk::PackedAttachmentCount colorAttachmentCount,
const PackedAttachmentIndex depthStencilAttachmentIndex,
const PackedClearValuesArray &clearValues,
CommandBuffer **commandBufferOut);
void endRenderPass(ContextVk *contextVk);
void updateStartedRenderPassWithDepthMode(bool readOnlyDepthStencilMode);
void beginTransformFeedback(size_t validBufferCount,
const VkBuffer *counterBuffers,
bool rebindBuffers);
void endTransformFeedback();
void invalidateRenderPassColorAttachment(PackedAttachmentIndex attachmentIndex);
void invalidateRenderPassDepthAttachment(const gl::DepthStencilState &dsState,
const gl::Rectangle &invalidateArea);
void invalidateRenderPassStencilAttachment(const gl::DepthStencilState &dsState,
const gl::Rectangle &invalidateArea);
bool hasWriteAfterInvalidate(uint32_t cmdCountInvalidated, uint32_t cmdCountDisabled)
{
ASSERT(mIsRenderPassCommandBuffer);
return (cmdCountInvalidated != kInfiniteCmdSize &&
std::min(cmdCountDisabled, mCommandBuffer.getCommandSize()) != cmdCountInvalidated);
}
bool isInvalidated(uint32_t cmdCountInvalidated, uint32_t cmdCountDisabled)
{
ASSERT(mIsRenderPassCommandBuffer);
return cmdCountInvalidated != kInfiniteCmdSize &&
std::min(cmdCountDisabled, mCommandBuffer.getCommandSize()) == cmdCountInvalidated;
}
void updateRenderPassColorClear(PackedAttachmentIndex colorIndex,
const VkClearValue &colorClearValue);
void updateRenderPassDepthStencilClear(VkImageAspectFlags aspectFlags,
const VkClearValue &clearValue);
const gl::Rectangle &getRenderArea() const
{
ASSERT(mIsRenderPassCommandBuffer);
return mRenderArea;
}
// If render pass is started with a small render area due to a small scissor, and if a new
// larger scissor is specified, grow the render area to accomodate it.
void growRenderArea(ContextVk *contextVk, const gl::Rectangle &newRenderArea);
void resumeTransformFeedback();
void pauseTransformFeedback();
bool isTransformFeedbackStarted() const { return mValidTransformFeedbackBufferCount > 0; }
bool isTransformFeedbackActiveUnpaused() const { return mIsTransformFeedbackActiveUnpaused; }
uint32_t getAndResetCounter()
{
ASSERT(mIsRenderPassCommandBuffer);
uint32_t count = mCounter;
mCounter = 0;
return count;
}
VkFramebuffer getFramebufferHandle() const
{
ASSERT(mIsRenderPassCommandBuffer);
return mFramebuffer.getHandle();
}
bool usesBuffer(const BufferHelper &buffer) const;
bool usesBufferForWrite(const BufferHelper &buffer) const;
bool usesImageInRenderPass(const ImageHelper &image) const;
size_t getUsedBuffersCount() const { return mUsedBuffers.size(); }
// Dumping the command stream is disabled by default.
static constexpr bool kEnableCommandStreamDiagnostics = false;
void onDepthAccess(ResourceAccess access);
void onStencilAccess(ResourceAccess access);
void updateRenderPassForResolve(ContextVk *contextVk,
Framebuffer *newFramebuffer,
const RenderPassDesc &renderPassDesc);
bool hasDepthStencilWriteOrClear() const
{
return mDepthAccess == ResourceAccess::Write || mStencilAccess == ResourceAccess::Write ||
mAttachmentOps[mDepthStencilAttachmentIndex].loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR ||
mAttachmentOps[mDepthStencilAttachmentIndex].stencilLoadOp ==
VK_ATTACHMENT_LOAD_OP_CLEAR;
}
void addCommandDiagnostics(ContextVk *contextVk);
const RenderPassDesc &getRenderPassDesc() const { return mRenderPassDesc; }
const AttachmentOpsArray &getAttachmentOps() const { return mAttachmentOps; }
bool hasRenderPass() const { return mIsRenderPassCommandBuffer; }
void setHasShaderStorageOutput() { mHasShaderStorageOutput = true; }
bool hasShaderStorageOutput() const { return mHasShaderStorageOutput; }
void setGLMemoryBarrierIssued()
{
if (!empty())
{
mHasGLMemoryBarrierIssued = true;
}
}
bool hasGLMemoryBarrierIssued() const { return mHasGLMemoryBarrierIssued; }
void setImageOptimizeForPresent(ImageHelper *image) { mImageOptimizeForPresent = image; }
private:
bool onDepthStencilAccess(ResourceAccess access,
uint32_t *cmdCountInvalidated,
uint32_t *cmdCountDisabled);
void restoreDepthContent();
void restoreStencilContent();
// We can't determine the image layout at the renderpass start time since their full usage
// aren't known until later time. We finalize the layout when either ImageHelper object is
// released or when renderpass ends.
void finalizeColorImageLayout(Context *context,
ImageHelper *image,
PackedAttachmentIndex packedAttachmentIndex,
bool isResolveImage);
void finalizeDepthStencilImageLayout(Context *context);
void finalizeDepthStencilResolveImageLayout(Context *context);
void finalizeDepthStencilLoadStore(Context *context);
void finalizeDepthStencilImageLayoutAndLoadStore(Context *context);
void updateImageLayoutAndBarrier(Context *context,
ImageHelper *image,
VkImageAspectFlags aspectFlags,
ImageLayout imageLayout);
// Allocator used by this class. Using a pool allocator per CBH to avoid threading issues
// that occur w/ shared allocator between multiple CBHs.
angle::PoolAllocator mAllocator;
// General state (non-renderPass related)
PipelineBarrierArray mPipelineBarriers;
PipelineStagesMask mPipelineBarrierMask;
CommandBuffer mCommandBuffer;
// RenderPass state
uint32_t mCounter;
RenderPassDesc mRenderPassDesc;
AttachmentOpsArray mAttachmentOps;
Framebuffer mFramebuffer;
gl::Rectangle mRenderArea;
PackedClearValuesArray mClearValues;
bool mRenderPassStarted;
// Transform feedback state
gl::TransformFeedbackBuffersArray<VkBuffer> mTransformFeedbackCounterBuffers;
uint32_t mValidTransformFeedbackBufferCount;
bool mRebindTransformFeedbackBuffers;
bool mIsTransformFeedbackActiveUnpaused;
bool mIsRenderPassCommandBuffer;
// Whether the command buffers contains any draw/dispatch calls that possibly output data
// through storage buffers and images. This is used to determine whether glMemoryBarrier*
// should flush the command buffer.
bool mHasShaderStorageOutput;
// Whether glMemoryBarrier has been called while commands are recorded in this command buffer.
// This is used to know when to check and potentially flush the command buffer if storage
// buffers and images are used in it.
bool mHasGLMemoryBarrierIssued;
// State tracking for the maximum (Write been the highest) depth access during the entire
// renderpass. Note that this does not include VK_ATTACHMENT_LOAD_OP_CLEAR which is tracked
// separately. This is done this way to allow clear op to being optimized out when we find out
// that the depth buffer is not being used during the entire renderpass and store op is
// VK_ATTACHMENT_STORE_OP_DONTCARE.
ResourceAccess mDepthAccess;
// Similar tracking to mDepthAccess but for the stencil aspect.
ResourceAccess mStencilAccess;
// State tracking for whether to optimize the storeOp to DONT_CARE
uint32_t mDepthCmdSizeInvalidated;
uint32_t mDepthCmdSizeDisabled;
uint32_t mStencilCmdSizeInvalidated;
uint32_t mStencilCmdSizeDisabled;
gl::Rectangle mDepthInvalidateArea;
gl::Rectangle mStencilInvalidateArea;
// Keep track of the depth/stencil attachment index
PackedAttachmentIndex mDepthStencilAttachmentIndex;
// Tracks resources used in the command buffer.
// For Buffers, we track the read/write access type so we can enable simultaneous reads.
// Images have unique layouts unlike buffers therefore we don't support multi-read.
angle::FastIntegerMap<BufferAccess> mUsedBuffers;
angle::FastIntegerSet mRenderPassUsedImages;
ImageHelper *mDepthStencilImage;
ImageHelper *mDepthStencilResolveImage;
gl::LevelIndex mDepthStencilLevelIndex;
uint32_t mDepthStencilLayerIndex;
uint32_t mDepthStencilLayerCount;
// Array size of mColorImages
PackedAttachmentCount mColorImagesCount;
// Attached render target images. Color and depth resolve images are always come last.
PackedImageAttachmentArray mColorImages;
PackedImageAttachmentArray mColorResolveImages;
// This is last renderpass before present and this is the image will be presented. We can use
// final layout of the renderpass to transit it to the presentable layout
ImageHelper *mImageOptimizeForPresent;
};
// Imagine an image going through a few layout transitions:
//
// srcStage 1 dstStage 2 srcStage 2 dstStage 3
// Layout 1 ------Transition 1-----> Layout 2 ------Transition 2------> Layout 3
// srcAccess 1 dstAccess 2 srcAccess 2 dstAccess 3
// \_________________ ___________________/
// \/
// A transition
//
// Every transition requires 6 pieces of information: from/to layouts, src/dst stage masks and
// src/dst access masks. At the moment we decide to transition the image to Layout 2 (i.e.
// Transition 1), we need to have Layout 1, srcStage 1 and srcAccess 1 stored as history of the
// image. To perform the transition, we need to know Layout 2, dstStage 2 and dstAccess 2.
// Additionally, we need to know srcStage 2 and srcAccess 2 to retain them for the next transition.
//
// That is, with the history kept, on every new transition we need 5 pieces of new information:
// layout/dstStage/dstAccess to transition into the layout, and srcStage/srcAccess for the future
// transition out from it. Given the small number of possible combinations of these values, an
// enum is used were each value encapsulates these 5 pieces of information:
//
// +--------------------------------+
// srcStage 1 | dstStage 2 srcStage 2 | dstStage 3
// Layout 1 ------Transition 1-----> Layout 2 ------Transition 2------> Layout 3
// srcAccess 1 |dstAccess 2 srcAccess 2| dstAccess 3
// +--------------- ---------------+
// \/
// One enum value
//
// Note that, while generally dstStage for the to-transition and srcStage for the from-transition
// are the same, they may occasionally be BOTTOM_OF_PIPE and TOP_OF_PIPE respectively.
enum class ImageLayout
{
Undefined = 0,
// Framebuffer attachment layouts are placed first, so they can fit in fewer bits in
// PackedAttachmentOpsDesc.
ColorAttachment,
ColorAttachmentAndFragmentShaderRead,
ColorAttachmentAndAllShadersRead,
DSAttachmentWriteAndFragmentShaderRead,
DSAttachmentWriteAndAllShadersRead,
DSAttachmentReadAndFragmentShaderRead,
DSAttachmentReadAndAllShadersRead,
DepthStencilAttachmentReadOnly,
DepthStencilAttachment,
DepthStencilResolveAttachment,
Present,
// The rest of the layouts.
ExternalPreInitialized,
ExternalShadersReadOnly,
ExternalShadersWrite,
TransferSrc,
TransferDst,
VertexShaderReadOnly,
VertexShaderWrite,
// PreFragment == Vertex, Tessellation and Geometry stages
PreFragmentShadersReadOnly,
PreFragmentShadersWrite,
FragmentShaderReadOnly,
FragmentShaderWrite,
ComputeShaderReadOnly,
ComputeShaderWrite,
AllGraphicsShadersReadOnly,
AllGraphicsShadersWrite,
InvalidEnum,
EnumCount = InvalidEnum,
};
VkImageLayout ConvertImageLayoutToVkImageLayout(ImageLayout imageLayout);
// How the ImageHelper object is being used by the renderpass
enum class RenderPassUsage
{
// Attached to the render taget of the current renderpass commands. It could be read/write or
// read only access.
RenderTargetAttachment,
// This is special case of RenderTargetAttachment where the render target access is read only.
// Right now it is only tracked for depth stencil attachment
ReadOnlyAttachment,
// Attached to the texture sampler of the current renderpass commands
TextureSampler,
InvalidEnum,
EnumCount = InvalidEnum,
};
using RenderPassUsageFlags = angle::PackedEnumBitSet<RenderPassUsage, uint16_t>;
bool FormatHasNecessaryFeature(RendererVk *renderer,
angle::FormatID formatID,
VkImageTiling tilingMode,
VkFormatFeatureFlags featureBits);
bool CanCopyWithTransfer(RendererVk *renderer,
const Format &srcFormat,
VkImageTiling srcTilingMode,
const Format &destFormat,
VkImageTiling destTilingMode);
class ImageHelper final : public Resource, public angle::Subject
{
public:
ImageHelper();
ImageHelper(ImageHelper &&other);
~ImageHelper() override;
void initStagingBuffer(RendererVk *renderer,
size_t imageCopyBufferAlignment,
VkBufferUsageFlags usageFlags,
size_t initialSize);
angle::Result init(Context *context,
gl::TextureType textureType,
const VkExtent3D &extents,
const Format &format,
GLint samples,
VkImageUsageFlags usage,
gl::LevelIndex firstLevel,
uint32_t mipLevels,
uint32_t layerCount,
bool isRobustResourceInitEnabled,
bool hasProtectedContent);
angle::Result initMSAASwapchain(Context *context,
gl::TextureType textureType,
const VkExtent3D &extents,
bool rotatedAspectRatio,
const Format &format,
GLint samples,
VkImageUsageFlags usage,
gl::LevelIndex firstLevel,
uint32_t mipLevels,
uint32_t layerCount,
bool isRobustResourceInitEnabled,
bool hasProtectedContent);
angle::Result initExternal(Context *context,
gl::TextureType textureType,
const VkExtent3D &extents,
const Format &format,
GLint samples,
VkImageUsageFlags usage,
VkImageCreateFlags additionalCreateFlags,
ImageLayout initialLayout,
const void *externalImageCreateInfo,
gl::LevelIndex firstLevel,
uint32_t mipLevels,
uint32_t layerCount,
bool isRobustResourceInitEnabled,
bool *imageFormatListEnabledOut,
bool hasProtectedContent);
angle::Result initMemory(Context *context,
bool hasProtectedContent,
const MemoryProperties &memoryProperties,
VkMemoryPropertyFlags flags);
angle::Result initExternalMemory(
Context *context,
const MemoryProperties &memoryProperties,
const VkMemoryRequirements &memoryRequirements,
const VkSamplerYcbcrConversionCreateInfo *samplerYcbcrConversionCreateInfo,
const void *extraAllocationInfo,
uint32_t currentQueueFamilyIndex,
VkMemoryPropertyFlags flags);
angle::Result initLayerImageView(Context *context,
gl::TextureType textureType,
VkImageAspectFlags aspectMask,
const gl::SwizzleState &swizzleMap,
ImageView *imageViewOut,
LevelIndex baseMipLevelVk,
uint32_t levelCount,
uint32_t baseArrayLayer,
uint32_t layerCount,
gl::SrgbWriteControlMode mode) const;
angle::Result initLayerImageViewWithFormat(Context *context,
gl::TextureType textureType,
const Format &format,
VkImageAspectFlags aspectMask,
const gl::SwizzleState &swizzleMap,
ImageView *imageViewOut,
LevelIndex baseMipLevelVk,
uint32_t levelCount,
uint32_t baseArrayLayer,
uint32_t layerCount) const;
angle::Result initReinterpretedLayerImageView(Context *context,
gl::TextureType textureType,
VkImageAspectFlags aspectMask,
const gl::SwizzleState &swizzleMap,
ImageView *imageViewOut,
LevelIndex baseMipLevelVk,
uint32_t levelCount,
uint32_t baseArrayLayer,
uint32_t layerCount,
VkImageUsageFlags imageUsageFlags,
angle::FormatID imageViewFormat) const;
angle::Result initImageView(Context *context,
gl::TextureType textureType,
VkImageAspectFlags aspectMask,
const gl::SwizzleState &swizzleMap,
ImageView *imageViewOut,
LevelIndex baseMipLevelVk,
uint32_t levelCount);
// Create a 2D[Array] for staging purposes. Used by:
//
// - TextureVk::copySubImageImplWithDraw
// - FramebufferVk::readPixelsImpl
//
angle::Result init2DStaging(Context *context,
bool hasProtectedContent,
const MemoryProperties &memoryProperties,
const gl::Extents &glExtents,
const Format &format,
VkImageUsageFlags usage,
uint32_t layerCount);
// Create an image for staging purposes. Used by:
//
// - TextureVk::copyAndStageImageData
//
angle::Result initStaging(Context *context,
bool hasProtectedContent,
const MemoryProperties &memoryProperties,
VkImageType imageType,
const VkExtent3D &extents,
const Format &format,
GLint samples,
VkImageUsageFlags usage,
uint32_t mipLevels,
uint32_t layerCount);
// Create a multisampled image for use as the implicit image in multisampled render to texture
// rendering. If LAZILY_ALLOCATED memory is available, it will prefer that.
angle::Result initImplicitMultisampledRenderToTexture(Context *context,
bool hasProtectedContent,
const MemoryProperties &memoryProperties,
gl::TextureType textureType,
GLint samples,
const ImageHelper &resolveImage,
bool isRobustResourceInitEnabled);
// Release the underlining VkImage object for garbage collection.
void releaseImage(RendererVk *renderer);
// Similar to releaseImage, but also notify all contexts in the same share group to stop
// accessing to it.
void releaseImageFromShareContexts(RendererVk *renderer, ContextVk *contextVk);
void releaseStagingBuffer(RendererVk *renderer);
bool valid() const { return mImage.valid(); }
VkImageAspectFlags getAspectFlags() const;
// True if image contains both depth & stencil aspects
bool isCombinedDepthStencilFormat() const;
void destroy(RendererVk *renderer);
void release(RendererVk *renderer) { destroy(renderer); }
void init2DWeakReference(Context *context,
VkImage handle,
const gl::Extents &glExtents,
bool rotatedAspectRatio,
const Format &format,
GLint samples,
bool isRobustResourceInitEnabled);
void resetImageWeakReference();
const Image &getImage() const { return mImage; }
const DeviceMemory &getDeviceMemory() const { return mDeviceMemory; }
void setTilingMode(VkImageTiling tilingMode) { mTilingMode = tilingMode; }
VkImageTiling getTilingMode() const { return mTilingMode; }
VkImageCreateFlags getCreateFlags() const { return mCreateFlags; }
VkImageUsageFlags getUsage() const { return mUsage; }
VkImageType getType() const { return mImageType; }
const VkExtent3D &getExtents() const { return mExtents; }
const VkExtent3D getRotatedExtents() const;
uint32_t getLayerCount() const { return mLayerCount; }
uint32_t getLevelCount() const { return mLevelCount; }
const Format &getFormat() const { return *mFormat; }
GLint getSamples() const { return mSamples; }
ImageSerial getImageSerial() const
{
ASSERT(valid() && mImageSerial.valid());
return mImageSerial;
}
void setCurrentImageLayout(ImageLayout newLayout) { mCurrentLayout = newLayout; }
ImageLayout getCurrentImageLayout() const { return mCurrentLayout; }
VkImageLayout getCurrentLayout() const;
gl::Extents getLevelExtents(LevelIndex levelVk) const;
// Helper function to calculate the extents of a render target created for a certain mip of the
// image.
gl::Extents getLevelExtents2D(LevelIndex levelVk) const;
gl::Extents getRotatedLevelExtents2D(LevelIndex levelVk) const;
bool isDepthOrStencil() const;
void setRenderPassUsageFlag(RenderPassUsage flag);
void clearRenderPassUsageFlag(RenderPassUsage flag);
void resetRenderPassUsageFlags();
bool hasRenderPassUsageFlag(RenderPassUsage flag) const;
bool usedByCurrentRenderPassAsAttachmentAndSampler() const;
// Clear either color or depth/stencil based on image format.
void clear(VkImageAspectFlags aspectFlags,
const VkClearValue &value,
LevelIndex mipLevel,
uint32_t baseArrayLayer,
uint32_t layerCount,
CommandBuffer *commandBuffer);
static void Copy(ImageHelper *srcImage,
ImageHelper *dstImage,
const gl::Offset &srcOffset,
const gl::Offset &dstOffset,
const gl::Extents ©Size,
const VkImageSubresourceLayers &srcSubresources,
const VkImageSubresourceLayers &dstSubresources,
CommandBuffer *commandBuffer);
static angle::Result CopyImageSubData(const gl::Context *context,
ImageHelper *srcImage,
GLint srcLevel,
GLint srcX,
GLint srcY,
GLint srcZ,
ImageHelper *dstImage,
GLint dstLevel,
GLint dstX,
GLint dstY,
GLint dstZ,
GLsizei srcWidth,
GLsizei srcHeight,
GLsizei srcDepth);
// Generate mipmap from level 0 into the rest of the levels with blit.
angle::Result generateMipmapsWithBlit(ContextVk *contextVk,
LevelIndex baseLevel,
LevelIndex maxLevel);
// Resolve this image into a destination image. This image should be in the TransferSrc layout.
// The destination image is automatically transitioned into TransferDst.
void resolve(ImageHelper *dest, const VkImageResolve ®ion, CommandBuffer *commandBuffer);
// Data staging
void removeSingleSubresourceStagedUpdates(ContextVk *contextVk,
gl::LevelIndex levelIndexGL,
uint32_t layerIndex,
uint32_t layerCount);
void removeStagedUpdates(Context *context,
gl::LevelIndex levelGLStart,
gl::LevelIndex levelGLEnd);
angle::Result stageSubresourceUpdateImpl(ContextVk *contextVk,
const gl::ImageIndex &index,
const gl::Extents &glExtents,
const gl::Offset &offset,
const gl::InternalFormat &formatInfo,
const gl::PixelUnpackState &unpack,
DynamicBuffer *stagingBufferOverride,
GLenum type,
const uint8_t *pixels,
const Format &vkFormat,
const GLuint inputRowPitch,
const GLuint inputDepthPitch,
const GLuint inputSkipBytes);
angle::Result stageSubresourceUpdate(ContextVk *contextVk,
const gl::ImageIndex &index,
const gl::Extents &glExtents,
const gl::Offset &offset,
const gl::InternalFormat &formatInfo,
const gl::PixelUnpackState &unpack,
DynamicBuffer *stagingBufferOverride,
GLenum type,
const uint8_t *pixels,
const Format &vkFormat);
angle::Result stageSubresourceUpdateAndGetData(ContextVk *contextVk,
size_t allocationSize,
const gl::ImageIndex &imageIndex,
const gl::Extents &glExtents,
const gl::Offset &offset,
uint8_t **destData,
DynamicBuffer *stagingBufferOverride);
angle::Result stageSubresourceUpdateFromFramebuffer(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Rectangle &sourceArea,
const gl::Offset &dstOffset,
const gl::Extents &dstExtent,
const gl::InternalFormat &formatInfo,
FramebufferVk *framebufferVk,
DynamicBuffer *stagingBufferOverride);
void stageSubresourceUpdateFromImage(RefCounted<ImageHelper> *image,
const gl::ImageIndex &index,
LevelIndex srcMipLevel,
const gl::Offset &destOffset,
const gl::Extents &glExtents,
const VkImageType imageType);
// Takes an image and stages a subresource update for each level of it, including its full
// extent and all its layers, at the specified GL level.
void stageSubresourceUpdatesFromAllImageLevels(RefCounted<ImageHelper> *image,
gl::LevelIndex baseLevel);
// Stage a clear to an arbitrary value.
void stageClear(const gl::ImageIndex &index,
VkImageAspectFlags aspectFlags,
const VkClearValue &clearValue);
// Stage a clear based on robust resource init.
angle::Result stageRobustResourceClearWithFormat(ContextVk *contextVk,
const gl::ImageIndex &index,
const gl::Extents &glExtents,
const Format &format);
void stageRobustResourceClear(const gl::ImageIndex &index);
// Stage the currently allocated image as updates to base level and on, making this !valid().
// This is used for:
//
// - Mipmap generation, where levelCount is 1 so only the base level is retained
// - Image respecification, where every level (other than those explicitly skipped) is staged
void stageSelfAsSubresourceUpdates(ContextVk *contextVk,
uint32_t levelCount,
gl::TexLevelMask skipLevelsMask);
// Flush staged updates for a single subresource. Can optionally take a parameter to defer
// clears to a subsequent RenderPass load op.
angle::Result flushSingleSubresourceStagedUpdates(ContextVk *contextVk,
gl::LevelIndex levelGL,
uint32_t layer,
uint32_t layerCount,
ClearValuesArray *deferredClears,
uint32_t deferredClearIndex);
// Flushes staged updates to a range of levels and layers from start to (but not including) end.
// Due to the nature of updates (done wholly to a VkImageSubresourceLayers), some unsolicited
// layers may also be updated.
angle::Result flushStagedUpdates(ContextVk *contextVk,
gl::LevelIndex levelGLStart,
gl::LevelIndex levelGLEnd,
uint32_t layerStart,
uint32_t layerEnd,
gl::TexLevelMask skipLevelsMask);
// Creates a command buffer and flushes all staged updates. This is used for one-time
// initialization of resources that we don't expect to accumulate further staged updates, such
// as with renderbuffers or surface images.
angle::Result flushAllStagedUpdates(ContextVk *contextVk);
bool hasStagedUpdatesForSubresource(gl::LevelIndex levelGL,
uint32_t layer,
uint32_t layerCount) const;
bool hasStagedUpdatesInAllocatedLevels() const;
void recordWriteBarrier(Context *context,
VkImageAspectFlags aspectMask,
ImageLayout newLayout,
CommandBuffer *commandBuffer)
{
barrierImpl(context, aspectMask, newLayout, mCurrentQueueFamilyIndex, commandBuffer);
}
// This function can be used to prevent issuing redundant layout transition commands.
bool isReadBarrierNecessary(ImageLayout newLayout) const;
void recordReadBarrier(Context *context,
VkImageAspectFlags aspectMask,
ImageLayout newLayout,
CommandBuffer *commandBuffer)
{
if (!isReadBarrierNecessary(newLayout))
{
return;
}
barrierImpl(context, aspectMask, newLayout, mCurrentQueueFamilyIndex, commandBuffer);
}
bool isQueueChangeNeccesary(uint32_t newQueueFamilyIndex) const
{
return mCurrentQueueFamilyIndex != newQueueFamilyIndex;
}
void changeLayoutAndQueue(Context *context,
VkImageAspectFlags aspectMask,
ImageLayout newLayout,
uint32_t newQueueFamilyIndex,
CommandBuffer *commandBuffer);
// Returns true if barrier has been generated
bool updateLayoutAndBarrier(Context *context,
VkImageAspectFlags aspectMask,
ImageLayout newLayout,
PipelineBarrier *barrier);
// Performs an ownership transfer from an external instance or API.
void acquireFromExternal(ContextVk *contextVk,
uint32_t externalQueueFamilyIndex,
uint32_t rendererQueueFamilyIndex,
ImageLayout currentLayout,
CommandBuffer *commandBuffer);
// Performs an ownership transfer to an external instance or API.
void releaseToExternal(ContextVk *contextVk,
uint32_t rendererQueueFamilyIndex,
uint32_t externalQueueFamilyIndex,
ImageLayout desiredLayout,
CommandBuffer *commandBuffer);
// Returns true if the image is owned by an external API or instance.
bool isReleasedToExternal() const;
gl::LevelIndex getFirstAllocatedLevel() const { return mFirstAllocatedLevel; }
void setFirstAllocatedLevel(gl::LevelIndex firstLevel);
gl::LevelIndex getLastAllocatedLevel() const;
LevelIndex toVkLevel(gl::LevelIndex levelIndexGL) const;
gl::LevelIndex toGLLevel(LevelIndex levelIndexVk) const;
angle::Result copyImageDataToBuffer(ContextVk *contextVk,
gl::LevelIndex sourceLevelGL,
uint32_t layerCount,
uint32_t baseLayer,
const gl::Box &sourceArea,
BufferHelper **bufferOut,
size_t *bufferSize,
StagingBufferOffsetArray *bufferOffsetsOut,
uint8_t **outDataPtr);
static angle::Result GetReadPixelsParams(ContextVk *contextVk,
const gl::PixelPackState &packState,
gl::Buffer *packBuffer,
GLenum format,
GLenum type,
const gl::Rectangle &area,
const gl::Rectangle &clippedArea,
PackPixelsParams *paramsOut,
GLuint *skipBytesOut);
angle::Result readPixelsForGetImage(ContextVk *contextVk,
const gl::PixelPackState &packState,
gl::Buffer *packBuffer,
gl::LevelIndex levelGL,
uint32_t layer,
GLenum format,
GLenum type,
void *pixels);
angle::Result readPixels(ContextVk *contextVk,
const gl::Rectangle &area,
const PackPixelsParams &packPixelsParams,
VkImageAspectFlagBits copyAspectFlags,
gl::LevelIndex levelGL,
uint32_t layer,
void *pixels,
DynamicBuffer *stagingBuffer);
angle::Result CalculateBufferInfo(ContextVk *contextVk,
const gl::Extents &glExtents,
const gl::InternalFormat &formatInfo,
const gl::PixelUnpackState &unpack,
GLenum type,
bool is3D,
GLuint *inputRowPitch,
GLuint *inputDepthPitch,
GLuint *inputSkipBytes);
// Mark a given subresource as written to. The subresource is identified by [levelStart,
// levelStart + levelCount) and [layerStart, layerStart + layerCount).
void onWrite(gl::LevelIndex levelStart,
uint32_t levelCount,
uint32_t layerStart,
uint32_t layerCount,
VkImageAspectFlags aspectFlags);
bool hasImmutableSampler() const;
uint64_t getExternalFormat() const { return mExternalFormat; }
// Used by framebuffer and render pass functions to decide loadOps and invalidate/un-invalidate
// render target contents.
bool hasSubresourceDefinedContent(gl::LevelIndex level,
uint32_t layerIndex,
uint32_t layerCount) const;
bool hasSubresourceDefinedStencilContent(gl::LevelIndex level,
uint32_t layerIndex,
uint32_t layerCount) const;
void invalidateSubresourceContent(ContextVk *contextVk,
gl::LevelIndex level,
uint32_t layerIndex,
uint32_t layerCount);
void invalidateSubresourceStencilContent(ContextVk *contextVk,
gl::LevelIndex level,
uint32_t layerIndex,
uint32_t layerCount);
void restoreSubresourceContent(gl::LevelIndex level, uint32_t layerIndex, uint32_t layerCount);
void restoreSubresourceStencilContent(gl::LevelIndex level,
uint32_t layerIndex,
uint32_t layerCount);
private:
enum class UpdateSource
{
Clear,
Buffer,
Image,
};
ANGLE_ENABLE_STRUCT_PADDING_WARNINGS
struct ClearUpdate
{
bool operator==(const ClearUpdate &rhs)
{
return memcmp(this, &rhs, sizeof(ClearUpdate)) == 0;
}
VkImageAspectFlags aspectFlags;
VkClearValue value;
uint32_t levelIndex;
uint32_t layerIndex;
uint32_t layerCount;
};
ANGLE_DISABLE_STRUCT_PADDING_WARNINGS
struct BufferUpdate
{
BufferHelper *bufferHelper;
VkBufferImageCopy copyRegion;
};
struct ImageUpdate
{
VkImageCopy copyRegion;
};
struct SubresourceUpdate : angle::NonCopyable
{
SubresourceUpdate();
~SubresourceUpdate();
SubresourceUpdate(BufferHelper *bufferHelperIn, const VkBufferImageCopy ©Region);
SubresourceUpdate(RefCounted<ImageHelper> *imageIn, const VkImageCopy ©Region);
SubresourceUpdate(VkImageAspectFlags aspectFlags,
const VkClearValue &clearValue,
const gl::ImageIndex &imageIndex);
SubresourceUpdate(SubresourceUpdate &&other);
SubresourceUpdate &operator=(SubresourceUpdate &&other);
void release(RendererVk *renderer);
bool isUpdateToLayers(uint32_t layerIndex, uint32_t layerCount) const;
void getDestSubresource(uint32_t imageLayerCount,
uint32_t *baseLayerOut,
uint32_t *layerCountOut) const;
VkImageAspectFlags getDestAspectFlags() const;
UpdateSource updateSource;
union
{
ClearUpdate clear;
BufferUpdate buffer;
ImageUpdate image;
} data;
RefCounted<ImageHelper> *image;
};
// Called from flushStagedUpdates, removes updates that are later superseded by another. This
// cannot be done at the time the updates were staged, as the image is not created (and thus the
// extents are not known).
void removeSupersededUpdates(ContextVk *contextVk, gl::TexLevelMask skipLevelsMask);
void initImageMemoryBarrierStruct(VkImageAspectFlags aspectMask,
ImageLayout newLayout,
uint32_t newQueueFamilyIndex,
VkImageMemoryBarrier *imageMemoryBarrier) const;
// Generalized to accept both "primary" and "secondary" command buffers.
template <typename CommandBufferT>
void barrierImpl(Context *context,
VkImageAspectFlags aspectMask,
ImageLayout newLayout,
uint32_t newQueueFamilyIndex,
CommandBufferT *commandBuffer);
// If the image has emulated channels, we clear them once so as not to leave garbage on those
// channels.
void stageClearIfEmulatedFormat(bool isRobustResourceInitEnabled);
void clearColor(const VkClearColorValue &color,
LevelIndex baseMipLevelVk,
uint32_t levelCount,
uint32_t baseArrayLayer,
uint32_t layerCount,
CommandBuffer *commandBuffer);
void clearDepthStencil(VkImageAspectFlags clearAspectFlags,
const VkClearDepthStencilValue &depthStencil,
LevelIndex baseMipLevelVk,
uint32_t levelCount,
uint32_t baseArrayLayer,
uint32_t layerCount,
CommandBuffer *commandBuffer);
angle::Result initializeNonZeroMemory(Context *context,
bool hasProtectedContent,
VkDeviceSize size);
std::vector<SubresourceUpdate> *getLevelUpdates(gl::LevelIndex level);
const std::vector<SubresourceUpdate> *getLevelUpdates(gl::LevelIndex level) const;
void appendSubresourceUpdate(gl::LevelIndex level, SubresourceUpdate &&update);
void prependSubresourceUpdate(gl::LevelIndex level, SubresourceUpdate &&update);
// Whether there are any updates in [start, end).
bool hasStagedUpdatesInLevels(gl::LevelIndex levelStart, gl::LevelIndex levelEnd) const;
// Used only for assertions, these functions verify that SubresourceUpdate::image references
// have the correct ref count. This is to prevent accidental leaks.
bool validateSubresourceUpdateImageRefConsistent(RefCounted<ImageHelper> *image) const;
bool validateSubresourceUpdateImageRefsConsistent() const;
void resetCachedProperties();
void setEntireContentDefined();
void setEntireContentUndefined();
void setContentDefined(LevelIndex levelStart,
uint32_t levelCount,
uint32_t layerStart,
uint32_t layerCount,
VkImageAspectFlags aspectFlags);
// Up to 8 layers are tracked per level for whether contents are defined, above which the
// contents are considered unconditionally defined. This handles the more likely scenarios of:
//
// - Single layer framebuffer attachments,
// - Cube map framebuffer attachments,
// - Multi-view rendering.
//
// If there arises a need to optimize an application that invalidates layer >= 8, an additional
// hash map can be used to track such subresources.
static constexpr uint32_t kMaxContentDefinedLayerCount = 8;
using LevelContentDefinedMask = angle::BitSet8<kMaxContentDefinedLayerCount>;
// Use the following functions to access m*ContentDefined to make sure the correct level index
// is used (i.e. vk::LevelIndex and not gl::LevelIndex).
LevelContentDefinedMask &getLevelContentDefined(LevelIndex level);
LevelContentDefinedMask &getLevelStencilContentDefined(LevelIndex level);
const LevelContentDefinedMask &getLevelContentDefined(LevelIndex level) const;
const LevelContentDefinedMask &getLevelStencilContentDefined(LevelIndex level) const;
angle::Result initLayerImageViewImpl(
Context *context,
gl::TextureType textureType,
VkImageAspectFlags aspectMask,
const gl::SwizzleState &swizzleMap,
ImageView *imageViewOut,
LevelIndex baseMipLevelVk,
uint32_t levelCount,
uint32_t baseArrayLayer,
uint32_t layerCount,
VkFormat imageFormat,
const VkImageViewUsageCreateInfo *imageViewUsageCreateInfo) const;
// Vulkan objects.
Image mImage;
DeviceMemory mDeviceMemory;
// Image properties.
VkImageType mImageType;
VkImageTiling mTilingMode;
VkImageCreateFlags mCreateFlags;
VkImageUsageFlags mUsage;
// For Android swapchain images, the Vulkan VkImage must be "rotated". However, most of ANGLE
// uses non-rotated extents (i.e. the way the application views the extents--see "Introduction
// to Android rotation and pre-rotation" in "SurfaceVk.cpp"). Thus, mExtents are non-rotated.
// The rotated extents are also stored along with a bool that indicates if the aspect ratio is
// different between the rotated and non-rotated extents.
VkExtent3D mExtents;
bool mRotatedAspectRatio;
const Format *mFormat;
GLint mSamples;
ImageSerial mImageSerial;
// Current state.
ImageLayout mCurrentLayout;
uint32_t mCurrentQueueFamilyIndex;
// For optimizing transition between different shader readonly layouts
ImageLayout mLastNonShaderReadOnlyLayout;
VkPipelineStageFlags mCurrentShaderReadStageMask;
// Track how it is being used by current open renderpass.
RenderPassUsageFlags mRenderPassUsageFlags;
// For imported images
BindingPointer<SamplerYcbcrConversion> mYuvConversionSampler;
uint64_t mExternalFormat;
// The first level that has been allocated. For mutable textures, this should be same as
// mBaseLevel since we always reallocate VkImage based on mBaseLevel change. But for immutable
// textures, we always allocate from level 0 regardless of mBaseLevel change.
gl::LevelIndex mFirstAllocatedLevel;
// Cached properties.
uint32_t mLayerCount;
uint32_t mLevelCount;
// Staging buffer
DynamicBuffer mStagingBuffer;
std::vector<std::vector<SubresourceUpdate>> mSubresourceUpdates;
// Optimization for repeated clear with the same value. If this pointer is not null, the entire
// image it has been cleared to the specified clear value. If another clear call is made with
// the exact same clear value, we will detect and skip the clear call.
Optional<ClearUpdate> mCurrentSingleClearValue;
// Track whether each subresource has defined contents. Up to 8 layers are tracked per level,
// above which the contents are considered unconditionally defined.
gl::TexLevelArray<LevelContentDefinedMask> mContentDefined;
gl::TexLevelArray<LevelContentDefinedMask> mStencilContentDefined;
};
// A vector of image views, such as one per level or one per layer.
using ImageViewVector = std::vector<ImageView>;
// A vector of vector of image views. Primary index is layer, secondary index is level.
using LayerLevelImageViewVector = std::vector<ImageViewVector>;
// Address mode for layers: only possible to access either all layers, or up to
// IMPLEMENTATION_ANGLE_MULTIVIEW_MAX_VIEWS layers. This enum uses 0 for all layers and the rest of
// the values conveniently alias the number of layers.
enum LayerMode
{
All,
_1,
_2,
_3,
_4,
};
static_assert(gl::IMPLEMENTATION_ANGLE_MULTIVIEW_MAX_VIEWS == 4, "Update LayerMode");
LayerMode GetLayerMode(const vk::ImageHelper &image, uint32_t layerCount);
// Sampler decode mode indicating if an attachment needs to be decoded in linear colorspace or sRGB
enum class SrgbDecodeMode
{
SkipDecode,
SrgbDecode
};
class ImageViewHelper final : public Resource
{
public:
ImageViewHelper();
ImageViewHelper(ImageViewHelper &&other);
~ImageViewHelper() override;
void init(RendererVk *renderer);
void release(RendererVk *renderer);
void destroy(VkDevice device);
const ImageView &getLinearReadImageView() const
{
return getValidReadViewImpl(mPerLevelLinearReadImageViews);
}
const ImageView &getSRGBReadImageView() const
{
return getValidReadViewImpl(mPerLevelSRGBReadImageViews);
}
const ImageView &getLinearFetchImageView() const
{
return getValidReadViewImpl(mPerLevelLinearFetchImageViews);
}
const ImageView &getSRGBFetchImageView() const
{
return getValidReadViewImpl(mPerLevelSRGBFetchImageViews);
}
const ImageView &getLinearCopyImageView() const
{
return getValidReadViewImpl(mPerLevelLinearCopyImageViews);
}
const ImageView &getSRGBCopyImageView() const
{
return getValidReadViewImpl(mPerLevelSRGBCopyImageViews);
}
const ImageView &getStencilReadImageView() const
{
return getValidReadViewImpl(mPerLevelStencilReadImageViews);
}
const ImageView &getReadImageView() const
{
return mLinearColorspace ? getReadViewImpl(mPerLevelLinearReadImageViews)
: getReadViewImpl(mPerLevelSRGBReadImageViews);
}
const ImageView &getFetchImageView() const
{
return mLinearColorspace ? getReadViewImpl(mPerLevelLinearFetchImageViews)
: getReadViewImpl(mPerLevelSRGBFetchImageViews);
}
const ImageView &getCopyImageView() const
{
return mLinearColorspace ? getReadViewImpl(mPerLevelLinearCopyImageViews)
: getReadViewImpl(mPerLevelSRGBCopyImageViews);
}
// Used when initialized RenderTargets.
bool hasStencilReadImageView() const
{
return mCurrentMaxLevel.get() < mPerLevelStencilReadImageViews.size()
? mPerLevelStencilReadImageViews[mCurrentMaxLevel.get()].valid()
: false;
}
bool hasFetchImageView() const
{
if ((mLinearColorspace && mCurrentMaxLevel.get() < mPerLevelLinearFetchImageViews.size()) ||
(!mLinearColorspace && mCurrentMaxLevel.get() < mPerLevelSRGBFetchImageViews.size()))
{
return getFetchImageView().valid();
}
else
{
return false;
}
}
bool hasCopyImageView() const
{
if ((mLinearColorspace && mCurrentMaxLevel.get() < mPerLevelLinearCopyImageViews.size()) ||
(!mLinearColorspace && mCurrentMaxLevel.get() < mPerLevelSRGBCopyImageViews.size()))
{
return getCopyImageView().valid();
}
else
{
return false;
}
}
// For applications that frequently switch a texture's max level, and make no other changes to
// the texture, change the currently-used max level, and potentially create new "read views"
// for the new max-level
angle::Result initReadViews(ContextVk *contextVk,
gl::TextureType viewType,
const ImageHelper &image,
const Format &format,
const gl::SwizzleState &formatSwizzle,
const gl::SwizzleState &readSwizzle,
LevelIndex baseLevel,
uint32_t levelCount,
uint32_t baseLayer,
uint32_t layerCount,
bool requiresSRGBViews,
VkImageUsageFlags imageUsageFlags);
// Creates a storage view with all layers of the level.
angle::Result getLevelStorageImageView(ContextVk *contextVk,
gl::TextureType viewType,
const ImageHelper &image,
LevelIndex levelVk,
uint32_t layer,
VkImageUsageFlags imageUsageFlags,
angle::FormatID formatID,
const ImageView **imageViewOut);
// Creates a storage view with a single layer of the level.
angle::Result getLevelLayerStorageImageView(ContextVk *contextVk,
const ImageHelper &image,
LevelIndex levelVk,
uint32_t layer,
VkImageUsageFlags imageUsageFlags,
angle::FormatID formatID,
const ImageView **imageViewOut);
// Creates a draw view with a range of layers of the level.
angle::Result getLevelDrawImageView(ContextVk *contextVk,
const ImageHelper &image,
LevelIndex levelVk,
uint32_t layer,
uint32_t layerCount,
gl::SrgbWriteControlMode mode,
const ImageView **imageViewOut);
// Creates a draw view with a single layer of the level.
angle::Result getLevelLayerDrawImageView(ContextVk *contextVk,
const ImageHelper &image,
LevelIndex levelVk,
uint32_t layer,
gl::SrgbWriteControlMode mode,
const ImageView **imageViewOut);
// Return unique Serial for an imageView.
ImageOrBufferViewSubresourceSerial getSubresourceSerial(
gl::LevelIndex levelGL,
uint32_t levelCount,
uint32_t layer,
LayerMode layerMode,
SrgbDecodeMode srgbDecodeMode,
gl::SrgbOverride srgbOverrideMode) const;
private:
ImageView &getReadImageView()
{
return mLinearColorspace ? getReadViewImpl(mPerLevelLinearReadImageViews)
: getReadViewImpl(mPerLevelSRGBReadImageViews);
}
ImageView &getFetchImageView()
{
return mLinearColorspace ? getReadViewImpl(mPerLevelLinearFetchImageViews)
: getReadViewImpl(mPerLevelSRGBFetchImageViews);
}
ImageView &getCopyImageView()
{
return mLinearColorspace ? getReadViewImpl(mPerLevelLinearCopyImageViews)
: getReadViewImpl(mPerLevelSRGBCopyImageViews);
}
// Used by public get*ImageView() methods to do proper assert based on vector size and validity
inline const ImageView &getValidReadViewImpl(const ImageViewVector &imageViewVector) const
{
ASSERT(mCurrentMaxLevel.get() < imageViewVector.size() &&
imageViewVector[mCurrentMaxLevel.get()].valid());
return imageViewVector[mCurrentMaxLevel.get()];
}
// Used by public get*ImageView() methods to do proper assert based on vector size
inline const ImageView &getReadViewImpl(const ImageViewVector &imageViewVector) const
{
ASSERT(mCurrentMaxLevel.get() < imageViewVector.size());
return imageViewVector[mCurrentMaxLevel.get()];
}
// Used by private get*ImageView() methods to do proper assert based on vector size
inline ImageView &getReadViewImpl(ImageViewVector &imageViewVector)
{
ASSERT(mCurrentMaxLevel.get() < imageViewVector.size());
return imageViewVector[mCurrentMaxLevel.get()];
}
// Creates views with multiple layers and levels.
angle::Result initReadViewsImpl(ContextVk *contextVk,
gl::TextureType viewType,
const ImageHelper &image,
const Format &format,
const gl::SwizzleState &formatSwizzle,
const gl::SwizzleState &readSwizzle,
LevelIndex baseLevel,
uint32_t levelCount,
uint32_t baseLayer,
uint32_t layerCount);
// Create SRGB-reinterpreted read views
angle::Result initSRGBReadViewsImpl(ContextVk *contextVk,
gl::TextureType viewType,
const ImageHelper &image,
const Format &format,
const gl::SwizzleState &formatSwizzle,
const gl::SwizzleState &readSwizzle,
LevelIndex baseLevel,
uint32_t levelCount,
uint32_t baseLayer,
uint32_t layerCount,
VkImageUsageFlags imageUsageFlags);
// For applications that frequently switch a texture's max level, and make no other changes to
// the texture, keep track of the currently-used max level, and keep one "read view" per
// max-level
LevelIndex mCurrentMaxLevel;
// Read views (one per max-level)
ImageViewVector mPerLevelLinearReadImageViews;
ImageViewVector mPerLevelSRGBReadImageViews;
ImageViewVector mPerLevelLinearFetchImageViews;
ImageViewVector mPerLevelSRGBFetchImageViews;
ImageViewVector mPerLevelLinearCopyImageViews;
ImageViewVector mPerLevelSRGBCopyImageViews;
ImageViewVector mPerLevelStencilReadImageViews;
bool mLinearColorspace;
// Draw views
LayerLevelImageViewVector mLayerLevelDrawImageViews;
LayerLevelImageViewVector mLayerLevelDrawImageViewsLinear;
angle::HashMap<ImageSubresourceRange, std::unique_ptr<ImageView>> mSubresourceDrawImageViews;
// Storage views
ImageViewVector mLevelStorageImageViews;
LayerLevelImageViewVector mLayerLevelStorageImageViews;
// Serial for the image view set. getSubresourceSerial combines it with subresource info.
ImageOrBufferViewSerial mImageViewSerial;
};
ImageSubresourceRange MakeImageSubresourceReadRange(gl::LevelIndex level,
uint32_t levelCount,
uint32_t layer,
LayerMode layerMode,
SrgbDecodeMode srgbDecodeMode,
gl::SrgbOverride srgbOverrideMode);
ImageSubresourceRange MakeImageSubresourceDrawRange(gl::LevelIndex level,
uint32_t layer,
LayerMode layerMode,
gl::SrgbWriteControlMode srgbWriteControlMode);
class BufferViewHelper final : public Resource
{
public:
BufferViewHelper();
BufferViewHelper(BufferViewHelper &&other);
~BufferViewHelper() override;
void init(RendererVk *renderer, VkDeviceSize offset, VkDeviceSize size);
void release(RendererVk *renderer);
void destroy(VkDevice device);
angle::Result getView(ContextVk *contextVk,
const BufferHelper &buffer,
VkDeviceSize bufferOffset,
const Format &format,
const BufferView **viewOut);
// Return unique Serial for a bufferView.
ImageOrBufferViewSubresourceSerial getSerial() const;
private:
// To support format reinterpretation, additional views for formats other than the one specified
// to glTexBuffer may need to be created. On draw/dispatch, the format layout qualifier of the
// imageBuffer is used (if provided) to create a potentially different view of the buffer.
angle::HashMap<VkFormat, BufferView> mViews;
// View properties:
//
// Offset and size specified to glTexBufferRange
VkDeviceSize mOffset;
VkDeviceSize mSize;
// Serial for the buffer view. An ImageOrBufferViewSerial is used for texture buffers so that
// they fit together with the other texture types.
ImageOrBufferViewSerial mViewSerial;
};
class FramebufferHelper : public Resource
{
public:
FramebufferHelper();
~FramebufferHelper() override;
FramebufferHelper(FramebufferHelper &&other);
FramebufferHelper &operator=(FramebufferHelper &&other);
angle::Result init(ContextVk *contextVk, const VkFramebufferCreateInfo &createInfo);
void release(ContextVk *contextVk);
bool valid() { return mFramebuffer.valid(); }
const Framebuffer &getFramebuffer() const
{
ASSERT(mFramebuffer.valid());
return mFramebuffer;
}
Framebuffer &getFramebuffer()
{
ASSERT(mFramebuffer.valid());
return mFramebuffer;
}
private:
// Vulkan object.
Framebuffer mFramebuffer;
};
class ShaderProgramHelper : angle::NonCopyable
{
public:
ShaderProgramHelper();
~ShaderProgramHelper();
bool valid(const gl::ShaderType shaderType) const;
void destroy(RendererVk *rendererVk);
void release(ContextVk *contextVk);
ShaderAndSerial &getShader(gl::ShaderType shaderType) { return mShaders[shaderType].get(); }
void setShader(gl::ShaderType shaderType, RefCounted<ShaderAndSerial> *shader);
void setSpecializationConstant(sh::vk::SpecializationConstantId id, uint32_t value);
// For getting a Pipeline and from the pipeline cache.
ANGLE_INLINE angle::Result getGraphicsPipeline(
ContextVk *contextVk,
RenderPassCache *renderPassCache,
const PipelineCache &pipelineCache,
const PipelineLayout &pipelineLayout,
const GraphicsPipelineDesc &pipelineDesc,
const gl::AttributesMask &activeAttribLocationsMask,
const gl::ComponentTypeMask &programAttribsTypeMask,
const GraphicsPipelineDesc **descPtrOut,
PipelineHelper **pipelineOut)
{
// Pull in a compatible RenderPass.
RenderPass *compatibleRenderPass = nullptr;
ANGLE_TRY(renderPassCache->getCompatibleRenderPass(
contextVk, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
ShaderModule *vertexShader = &mShaders[gl::ShaderType::Vertex].get().get();
ShaderModule *fragmentShader = mShaders[gl::ShaderType::Fragment].valid()
? &mShaders[gl::ShaderType::Fragment].get().get()
: nullptr;
ShaderModule *geometryShader = mShaders[gl::ShaderType::Geometry].valid()
? &mShaders[gl::ShaderType::Geometry].get().get()
: nullptr;
ShaderModule *tessControlShader = mShaders[gl::ShaderType::TessControl].valid()
? &mShaders[gl::ShaderType::TessControl].get().get()
: nullptr;
ShaderModule *tessEvaluationShader =
mShaders[gl::ShaderType::TessEvaluation].valid()
? &mShaders[gl::ShaderType::TessEvaluation].get().get()
: nullptr;
return mGraphicsPipelines.getPipeline(
contextVk, pipelineCache, *compatibleRenderPass, pipelineLayout,
activeAttribLocationsMask, programAttribsTypeMask, vertexShader, fragmentShader,
geometryShader, tessControlShader, tessEvaluationShader, mSpecializationConstants,
pipelineDesc, descPtrOut, pipelineOut);
}
angle::Result getComputePipeline(Context *context,
const PipelineLayout &pipelineLayout,
PipelineAndSerial **pipelineOut);
private:
gl::ShaderMap<BindingPointer<ShaderAndSerial>> mShaders;
GraphicsPipelineCache mGraphicsPipelines;
// We should probably use PipelineHelper here so we can remove PipelineAndSerial.
PipelineAndSerial mComputePipeline;
// Specialization constants, currently only used by the graphics queue.
SpecializationConstants mSpecializationConstants;
};
// Tracks current handle allocation counts in the back-end. Useful for debugging and profiling.
// Note: not all handle types are currently implemented.
class ActiveHandleCounter final : angle::NonCopyable
{
public:
ActiveHandleCounter();
~ActiveHandleCounter();
void onAllocate(HandleType handleType)
{
mActiveCounts[handleType]++;
mAllocatedCounts[handleType]++;
}
void onDeallocate(HandleType handleType) { mActiveCounts[handleType]--; }
uint32_t getActive(HandleType handleType) const { return mActiveCounts[handleType]; }
uint32_t getAllocated(HandleType handleType) const { return mAllocatedCounts[handleType]; }
private:
angle::PackedEnumMap<HandleType, uint32_t> mActiveCounts;
angle::PackedEnumMap<HandleType, uint32_t> mAllocatedCounts;
};
ANGLE_INLINE bool CommandBufferHelper::usesImageInRenderPass(const ImageHelper &image) const
{
ASSERT(mIsRenderPassCommandBuffer);
return mRenderPassUsedImages.contains(image.getImageSerial().getValue());
}
// Sometimes ANGLE issues a command internally, such as copies, draws and dispatches that do not
// directly correspond to the application draw/dispatch call. Before the command is recorded in the
// command buffer, the render pass may need to be broken and/or appropriate barriers may need to be
// inserted. The following struct aggregates all resources that such internal commands need.
struct CommandBufferBufferAccess
{
BufferHelper *buffer;
VkAccessFlags accessType;
PipelineStage stage;
};
struct CommandBufferImageAccess
{
ImageHelper *image;
VkImageAspectFlags aspectFlags;
ImageLayout imageLayout;
};
struct CommandBufferImageWrite
{
CommandBufferImageAccess access;
gl::LevelIndex levelStart;
uint32_t levelCount;
uint32_t layerStart;
uint32_t layerCount;
};
class CommandBufferAccess : angle::NonCopyable
{
public:
CommandBufferAccess();
~CommandBufferAccess();
void onBufferTransferRead(BufferHelper *buffer)
{
onBufferRead(VK_ACCESS_TRANSFER_READ_BIT, PipelineStage::Transfer, buffer);
}
void onBufferTransferWrite(BufferHelper *buffer)
{
onBufferWrite(VK_ACCESS_TRANSFER_WRITE_BIT, PipelineStage::Transfer, buffer);
}
void onBufferSelfCopy(BufferHelper *buffer)
{
onBufferWrite(VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT,
PipelineStage::Transfer, buffer);
}
void onBufferComputeShaderRead(BufferHelper *buffer)
{
onBufferRead(VK_ACCESS_SHADER_READ_BIT, PipelineStage::ComputeShader, buffer);
}
void onBufferComputeShaderWrite(BufferHelper *buffer)
{
onBufferWrite(VK_ACCESS_SHADER_WRITE_BIT, PipelineStage::ComputeShader, buffer);
}
void onImageTransferRead(VkImageAspectFlags aspectFlags, ImageHelper *image)
{
onImageRead(aspectFlags, ImageLayout::TransferSrc, image);
}
void onImageTransferWrite(gl::LevelIndex levelStart,
uint32_t levelCount,
uint32_t layerStart,
uint32_t layerCount,
VkImageAspectFlags aspectFlags,
ImageHelper *image)
{
onImageWrite(levelStart, levelCount, layerStart, layerCount, aspectFlags,
ImageLayout::TransferDst, image);
}
void onImageComputeShaderRead(VkImageAspectFlags aspectFlags, ImageHelper *image)
{
onImageRead(aspectFlags, ImageLayout::ComputeShaderReadOnly, image);
}
void onImageComputeShaderWrite(gl::LevelIndex levelStart,
uint32_t levelCount,
uint32_t layerStart,
uint32_t layerCount,
VkImageAspectFlags aspectFlags,
ImageHelper *image)
{
onImageWrite(levelStart, levelCount, layerStart, layerCount, aspectFlags,
ImageLayout::ComputeShaderWrite, image);
}
// The limits reflect the current maximum concurrent usage of each resource type. ASSERTs will
// fire if this limit is exceeded in the future.
using ReadBuffers = angle::FixedVector<CommandBufferBufferAccess, 2>;
using WriteBuffers = angle::FixedVector<CommandBufferBufferAccess, 2>;
using ReadImages = angle::FixedVector<CommandBufferImageAccess, 2>;
using WriteImages = angle::FixedVector<CommandBufferImageWrite, 1>;
const ReadBuffers &getReadBuffers() const { return mReadBuffers; }
const WriteBuffers &getWriteBuffers() const { return mWriteBuffers; }
const ReadImages &getReadImages() const { return mReadImages; }
const WriteImages &getWriteImages() const { return mWriteImages; }
private:
void onBufferRead(VkAccessFlags readAccessType, PipelineStage readStage, BufferHelper *buffer);
void onBufferWrite(VkAccessFlags writeAccessType,
PipelineStage writeStage,
BufferHelper *buffer);
void onImageRead(VkImageAspectFlags aspectFlags, ImageLayout imageLayout, ImageHelper *image);
void onImageWrite(gl::LevelIndex levelStart,
uint32_t levelCount,
uint32_t layerStart,
uint32_t layerCount,
VkImageAspectFlags aspectFlags,
ImageLayout imageLayout,
ImageHelper *image);
ReadBuffers mReadBuffers;
WriteBuffers mWriteBuffers;
ReadImages mReadImages;
WriteImages mWriteImages;
};
} // namespace vk
} // namespace rx
#endif // LIBANGLE_RENDERER_VULKAN_VK_HELPERS_H_