Hash :
c710023d
Author :
Date :
2023-03-31T00:00:00
D3D11: Implement ANGLE_polygon_mode Bug: angleproject:1791 Change-Id: I17a1ec9222199d4afb2dbcba3d0219f0a37f6a67 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4500079 Commit-Queue: Alexey Knyazev <lexa.knyazev@gmail.com> Reviewed-by: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033
//
// Copyright 2015 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.
//
// StateManager11.cpp: Defines a class for caching D3D11 state
#include "libANGLE/renderer/d3d/d3d11/StateManager11.h"
#include "common/angleutils.h"
#include "common/bitset_utils.h"
#include "common/mathutil.h"
#include "common/utilities.h"
#include "libANGLE/Context.h"
#include "libANGLE/Query.h"
#include "libANGLE/Surface.h"
#include "libANGLE/VertexArray.h"
#include "libANGLE/renderer/d3d/DisplayD3D.h"
#include "libANGLE/renderer/d3d/TextureD3D.h"
#include "libANGLE/renderer/d3d/d3d11/Buffer11.h"
#include "libANGLE/renderer/d3d/d3d11/Context11.h"
#include "libANGLE/renderer/d3d/d3d11/Framebuffer11.h"
#include "libANGLE/renderer/d3d/d3d11/IndexBuffer11.h"
#include "libANGLE/renderer/d3d/d3d11/RenderTarget11.h"
#include "libANGLE/renderer/d3d/d3d11/Renderer11.h"
#include "libANGLE/renderer/d3d/d3d11/ShaderExecutable11.h"
#include "libANGLE/renderer/d3d/d3d11/TextureStorage11.h"
#include "libANGLE/renderer/d3d/d3d11/TransformFeedback11.h"
#include "libANGLE/renderer/d3d/d3d11/VertexArray11.h"
#include "libANGLE/renderer/d3d/d3d11/VertexBuffer11.h"
namespace rx
{
namespace
{
bool ImageIndexConflictsWithSRV(const gl::ImageIndex &index, D3D11_SHADER_RESOURCE_VIEW_DESC desc)
{
unsigned mipLevel = index.getLevelIndex();
gl::TextureType textureType = index.getType();
switch (desc.ViewDimension)
{
case D3D11_SRV_DIMENSION_TEXTURE2D:
{
bool allLevels = (desc.Texture2D.MipLevels == std::numeric_limits<UINT>::max());
unsigned int maxSrvMip = desc.Texture2D.MipLevels + desc.Texture2D.MostDetailedMip;
maxSrvMip = allLevels ? INT_MAX : maxSrvMip;
unsigned mipMin = index.getLevelIndex();
unsigned mipMax = INT_MAX;
return textureType == gl::TextureType::_2D &&
gl::RangeUI(mipMin, mipMax)
.intersects(gl::RangeUI(desc.Texture2D.MostDetailedMip, maxSrvMip));
}
case D3D11_SRV_DIMENSION_TEXTURE2DARRAY:
{
GLint layerIndex = index.getLayerIndex();
bool allLevels = (desc.Texture2DArray.MipLevels == std::numeric_limits<UINT>::max());
unsigned int maxSrvMip =
desc.Texture2DArray.MipLevels + desc.Texture2DArray.MostDetailedMip;
maxSrvMip = allLevels ? INT_MAX : maxSrvMip;
unsigned maxSlice = desc.Texture2DArray.FirstArraySlice + desc.Texture2DArray.ArraySize;
// Cube maps can be mapped to Texture2DArray SRVs
return (textureType == gl::TextureType::_2DArray ||
textureType == gl::TextureType::CubeMap) &&
desc.Texture2DArray.MostDetailedMip <= mipLevel && mipLevel < maxSrvMip &&
desc.Texture2DArray.FirstArraySlice <= static_cast<UINT>(layerIndex) &&
static_cast<UINT>(layerIndex) < maxSlice;
}
case D3D11_SRV_DIMENSION_TEXTURECUBE:
{
bool allLevels = (desc.TextureCube.MipLevels == std::numeric_limits<UINT>::max());
unsigned int maxSrvMip = desc.TextureCube.MipLevels + desc.TextureCube.MostDetailedMip;
maxSrvMip = allLevels ? INT_MAX : maxSrvMip;
return textureType == gl::TextureType::CubeMap &&
desc.TextureCube.MostDetailedMip <= mipLevel && mipLevel < maxSrvMip;
}
case D3D11_SRV_DIMENSION_TEXTURE3D:
{
bool allLevels = (desc.Texture3D.MipLevels == std::numeric_limits<UINT>::max());
unsigned int maxSrvMip = desc.Texture3D.MipLevels + desc.Texture3D.MostDetailedMip;
maxSrvMip = allLevels ? INT_MAX : maxSrvMip;
return textureType == gl::TextureType::_3D &&
desc.Texture3D.MostDetailedMip <= mipLevel && mipLevel < maxSrvMip;
}
default:
// We only handle the cases corresponding to valid image indexes
UNIMPLEMENTED();
}
return false;
}
bool ImageIndexConflictsWithUAV(const gl::ImageIndex &index, D3D11_UNORDERED_ACCESS_VIEW_DESC desc)
{
unsigned mipLevel = index.getLevelIndex();
gl::TextureType textureType = index.getType();
switch (desc.ViewDimension)
{
case D3D11_UAV_DIMENSION_TEXTURE2D:
{
return textureType == gl::TextureType::_2D && mipLevel == desc.Texture2D.MipSlice;
}
case D3D11_UAV_DIMENSION_TEXTURE2DARRAY:
{
GLint layerIndex = index.getLayerIndex();
unsigned mipSlice = desc.Texture2DArray.MipSlice;
unsigned firstArraySlice = desc.Texture2DArray.FirstArraySlice;
unsigned lastArraySlice = firstArraySlice + desc.Texture2DArray.ArraySize;
return (textureType == gl::TextureType::_2DArray ||
textureType == gl::TextureType::CubeMap) &&
(mipLevel == mipSlice && gl::RangeUI(firstArraySlice, lastArraySlice)
.contains(static_cast<UINT>(layerIndex)));
}
case D3D11_UAV_DIMENSION_TEXTURE3D:
{
GLint layerIndex = index.getLayerIndex();
unsigned mipSlice = desc.Texture3D.MipSlice;
unsigned firstWSlice = desc.Texture3D.FirstWSlice;
unsigned lastWSlice = firstWSlice + desc.Texture3D.WSize;
return textureType == gl::TextureType::_3D &&
(mipLevel == mipSlice &&
gl::RangeUI(firstWSlice, lastWSlice).contains(static_cast<UINT>(layerIndex)));
}
default:
return false;
}
}
// Does *not* increment the resource ref count!!
ID3D11Resource *GetViewResource(ID3D11View *view)
{
ID3D11Resource *resource = nullptr;
ASSERT(view);
view->GetResource(&resource);
resource->Release();
return resource;
}
int GetWrapBits(GLenum wrap)
{
const int wrapBits = gl_d3d11::ConvertTextureWrap(wrap);
ASSERT(wrapBits >= 1 && wrapBits <= 5);
return wrapBits;
}
Optional<size_t> FindFirstNonInstanced(
const std::vector<const TranslatedAttribute *> ¤tAttributes)
{
for (size_t index = 0; index < currentAttributes.size(); ++index)
{
if (currentAttributes[index]->divisor == 0)
{
return Optional<size_t>(index);
}
}
return Optional<size_t>::Invalid();
}
void SortAttributesByLayout(const ProgramD3D &programD3D,
const std::vector<TranslatedAttribute> &vertexArrayAttribs,
const std::vector<TranslatedAttribute> ¤tValueAttribs,
AttribIndexArray *sortedD3DSemanticsOut,
std::vector<const TranslatedAttribute *> *sortedAttributesOut)
{
sortedAttributesOut->clear();
const AttribIndexArray &locationToSemantic = programD3D.getAttribLocationToD3DSemantics();
const gl::ProgramExecutable &executable = programD3D.getState().getExecutable();
for (auto locationIndex : executable.getActiveAttribLocationsMask())
{
int d3dSemantic = locationToSemantic[locationIndex];
if (sortedAttributesOut->size() <= static_cast<size_t>(d3dSemantic))
{
sortedAttributesOut->resize(d3dSemantic + 1);
}
(*sortedD3DSemanticsOut)[d3dSemantic] = d3dSemantic;
const auto *arrayAttrib = &vertexArrayAttribs[locationIndex];
if (arrayAttrib->attribute && arrayAttrib->attribute->enabled)
{
(*sortedAttributesOut)[d3dSemantic] = arrayAttrib;
}
else
{
ASSERT(currentValueAttribs[locationIndex].attribute);
(*sortedAttributesOut)[d3dSemantic] = ¤tValueAttribs[locationIndex];
}
}
}
void UpdateUniformBuffer(ID3D11DeviceContext *deviceContext,
UniformStorage11 *storage,
const d3d11::Buffer *buffer)
{
deviceContext->UpdateSubresource(buffer->get(), 0, nullptr, storage->getDataPointer(0, 0), 0,
0);
}
size_t GetReservedBufferCount(bool usesPointSpriteEmulation)
{
return usesPointSpriteEmulation ? 1 : 0;
}
bool CullsEverything(const gl::State &glState)
{
return (glState.getRasterizerState().cullFace &&
glState.getRasterizerState().cullMode == gl::CullFaceMode::FrontAndBack);
}
} // anonymous namespace
// StateManager11::ViewCache Implementation.
template <typename ViewType, typename DescType>
StateManager11::ViewCache<ViewType, DescType>::ViewCache() : mHighestUsedView(0)
{}
template <typename ViewType, typename DescType>
StateManager11::ViewCache<ViewType, DescType>::~ViewCache()
{}
template <typename ViewType, typename DescType>
void StateManager11::ViewCache<ViewType, DescType>::update(size_t resourceIndex, ViewType *view)
{
ASSERT(resourceIndex < mCurrentViews.size());
ViewRecord<DescType> *record = &mCurrentViews[resourceIndex];
record->view = reinterpret_cast<uintptr_t>(view);
if (view)
{
record->resource = reinterpret_cast<uintptr_t>(GetViewResource(view));
view->GetDesc(&record->desc);
mHighestUsedView = std::max(resourceIndex + 1, mHighestUsedView);
}
else
{
record->resource = 0;
if (resourceIndex + 1 == mHighestUsedView)
{
do
{
--mHighestUsedView;
} while (mHighestUsedView > 0 && mCurrentViews[mHighestUsedView].view == 0);
}
}
}
template <typename ViewType, typename DescType>
void StateManager11::ViewCache<ViewType, DescType>::clear()
{
if (mCurrentViews.empty())
{
return;
}
memset(&mCurrentViews[0], 0, sizeof(ViewRecord<DescType>) * mCurrentViews.size());
mHighestUsedView = 0;
}
StateManager11::SRVCache *StateManager11::getSRVCache(gl::ShaderType shaderType)
{
ASSERT(shaderType != gl::ShaderType::InvalidEnum);
return &mCurShaderSRVs[shaderType];
}
// ShaderConstants11 implementation
ShaderConstants11::ShaderConstants11() : mNumActiveShaderSamplers({})
{
mShaderConstantsDirty.set();
}
ShaderConstants11::~ShaderConstants11() {}
void ShaderConstants11::init(const gl::Caps &caps)
{
for (gl::ShaderType shaderType : gl::AllShaderTypes())
{
mShaderSamplerMetadata[shaderType].resize(caps.maxShaderTextureImageUnits[shaderType]);
mShaderReadonlyImageMetadata[shaderType].resize(caps.maxShaderImageUniforms[shaderType]);
mShaderImageMetadata[shaderType].resize(caps.maxShaderImageUniforms[shaderType]);
}
}
size_t ShaderConstants11::GetShaderConstantsStructSize(gl::ShaderType shaderType)
{
switch (shaderType)
{
case gl::ShaderType::Vertex:
return sizeof(Vertex);
case gl::ShaderType::Fragment:
return sizeof(Pixel);
case gl::ShaderType::Compute:
return sizeof(Compute);
// TODO(jiawei.shao@intel.com): return geometry shader constant struct size
case gl::ShaderType::Geometry:
return 0u;
default:
UNREACHABLE();
return 0u;
}
}
size_t ShaderConstants11::getRequiredBufferSize(gl::ShaderType shaderType) const
{
ASSERT(shaderType != gl::ShaderType::InvalidEnum);
return GetShaderConstantsStructSize(shaderType) +
mShaderSamplerMetadata[shaderType].size() * sizeof(SamplerMetadata) +
mShaderImageMetadata[shaderType].size() * sizeof(ImageMetadata) +
mShaderReadonlyImageMetadata[shaderType].size() * sizeof(ImageMetadata);
}
void ShaderConstants11::markDirty()
{
mShaderConstantsDirty.set();
mNumActiveShaderSamplers.fill(0);
}
bool ShaderConstants11::updateSamplerMetadata(SamplerMetadata *data,
const gl::Texture &texture,
const gl::SamplerState &samplerState)
{
bool dirty = false;
unsigned int baseLevel = texture.getTextureState().getEffectiveBaseLevel();
gl::TextureTarget target = (texture.getType() == gl::TextureType::CubeMap)
? gl::kCubeMapTextureTargetMin
: gl::NonCubeTextureTypeToTarget(texture.getType());
if (data->baseLevel != static_cast<int>(baseLevel))
{
data->baseLevel = static_cast<int>(baseLevel);
dirty = true;
}
// Some metadata is needed only for integer textures. We avoid updating the constant buffer
// unnecessarily by changing the data only in case the texture is an integer texture and
// the values have changed.
const gl::InternalFormat &info = *texture.getFormat(target, baseLevel).info;
if (!info.isInt() && !texture.getState().isStencilMode())
{
return dirty;
}
// Pack the wrap values into one integer so we can fit all the metadata in two 4-integer
// vectors.
const GLenum wrapS = samplerState.getWrapS();
const GLenum wrapT = samplerState.getWrapT();
const GLenum wrapR = samplerState.getWrapR();
const int wrapModes =
GetWrapBits(wrapS) | (GetWrapBits(wrapT) << 3) | (GetWrapBits(wrapR) << 6);
if (data->wrapModes != wrapModes)
{
data->wrapModes = wrapModes;
dirty = true;
}
// Skip checking and syncing integer border color if it is not used
if (wrapS != GL_CLAMP_TO_BORDER && wrapT != GL_CLAMP_TO_BORDER && wrapR != GL_CLAMP_TO_BORDER)
{
return dirty;
}
// Use the sampler state border color only if it is integer, initialize to zeros otherwise
angle::ColorGeneric borderColor;
ASSERT(borderColor.colorI.red == 0 && borderColor.colorI.green == 0 &&
borderColor.colorI.blue == 0 && borderColor.colorI.alpha == 0);
if (samplerState.getBorderColor().type != angle::ColorGeneric::Type::Float)
{
borderColor = samplerState.getBorderColor();
}
// Adjust the border color value to the texture format
borderColor = AdjustBorderColor<false>(
borderColor,
angle::Format::Get(angle::Format::InternalFormatToID(info.sizedInternalFormat)),
texture.getState().isStencilMode());
ASSERT(static_cast<const void *>(borderColor.colorI.data()) ==
static_cast<const void *>(borderColor.colorUI.data()));
if (memcmp(data->intBorderColor, borderColor.colorI.data(), sizeof(data->intBorderColor)) != 0)
{
memcpy(data->intBorderColor, borderColor.colorI.data(), sizeof(data->intBorderColor));
dirty = true;
}
return dirty;
}
bool ShaderConstants11::updateImageMetadata(ImageMetadata *data, const gl::ImageUnit &imageUnit)
{
bool dirty = false;
if (data->layer != static_cast<int>(imageUnit.layer))
{
data->layer = static_cast<int>(imageUnit.layer);
dirty = true;
}
if (data->level != static_cast<unsigned int>(imageUnit.level))
{
data->level = static_cast<unsigned int>(imageUnit.level);
dirty = true;
}
return dirty;
}
void ShaderConstants11::setComputeWorkGroups(GLuint numGroupsX,
GLuint numGroupsY,
GLuint numGroupsZ)
{
mCompute.numWorkGroups[0] = numGroupsX;
mCompute.numWorkGroups[1] = numGroupsY;
mCompute.numWorkGroups[2] = numGroupsZ;
mShaderConstantsDirty.set(gl::ShaderType::Compute);
}
void ShaderConstants11::setMultiviewWriteToViewportIndex(GLfloat index)
{
mVertex.multiviewWriteToViewportIndex = index;
mPixel.multiviewWriteToViewportIndex = index;
mShaderConstantsDirty.set(gl::ShaderType::Vertex);
mShaderConstantsDirty.set(gl::ShaderType::Fragment);
}
void ShaderConstants11::onViewportChange(const gl::Rectangle &glViewport,
const D3D11_VIEWPORT &dxViewport,
const gl::Offset &glFragCoordOffset,
bool is9_3,
bool presentPathFast)
{
mShaderConstantsDirty.set(gl::ShaderType::Vertex);
mShaderConstantsDirty.set(gl::ShaderType::Fragment);
// On Feature Level 9_*, we must emulate large and/or negative viewports in the shaders
// using viewAdjust (like the D3D9 renderer).
if (is9_3)
{
mVertex.viewAdjust[0] = static_cast<float>((glViewport.width - dxViewport.Width) +
2 * (glViewport.x - dxViewport.TopLeftX)) /
dxViewport.Width;
mVertex.viewAdjust[1] = static_cast<float>((glViewport.height - dxViewport.Height) +
2 * (glViewport.y - dxViewport.TopLeftY)) /
dxViewport.Height;
mVertex.viewAdjust[2] = static_cast<float>(glViewport.width) / dxViewport.Width;
mVertex.viewAdjust[3] = static_cast<float>(glViewport.height) / dxViewport.Height;
}
mPixel.viewCoords[0] = glViewport.width * 0.5f;
mPixel.viewCoords[1] = glViewport.height * 0.5f;
mPixel.viewCoords[2] = glViewport.x + (glViewport.width * 0.5f);
mPixel.viewCoords[3] = glViewport.y + (glViewport.height * 0.5f);
// Instanced pointsprite emulation requires ViewCoords to be defined in the
// the vertex shader.
mVertex.viewCoords[0] = mPixel.viewCoords[0];
mVertex.viewCoords[1] = mPixel.viewCoords[1];
mVertex.viewCoords[2] = mPixel.viewCoords[2];
mVertex.viewCoords[3] = mPixel.viewCoords[3];
const float zNear = dxViewport.MinDepth;
const float zFar = dxViewport.MaxDepth;
mPixel.depthFront[0] = (zFar - zNear) * 0.5f;
mPixel.depthFront[1] = (zNear + zFar) * 0.5f;
mVertex.depthRange[0] = zNear;
mVertex.depthRange[1] = zFar;
mVertex.depthRange[2] = zFar - zNear;
mPixel.depthRange[0] = zNear;
mPixel.depthRange[1] = zFar;
mPixel.depthRange[2] = zFar - zNear;
mPixel.viewScale[0] = 1.0f;
mPixel.viewScale[1] = presentPathFast ? 1.0f : -1.0f;
// Updates to the multiviewWriteToViewportIndex member are to be handled whenever the draw
// framebuffer's layout is changed.
mVertex.viewScale[0] = mPixel.viewScale[0];
mVertex.viewScale[1] = mPixel.viewScale[1];
mPixel.fragCoordOffset[0] = static_cast<float>(glFragCoordOffset.x);
mPixel.fragCoordOffset[1] = static_cast<float>(glFragCoordOffset.y);
}
// Update the ShaderConstants with a new first vertex and return whether the update dirties them.
ANGLE_INLINE bool ShaderConstants11::onFirstVertexChange(GLint firstVertex)
{
// firstVertex should already include baseVertex, if any.
uint32_t newFirstVertex = static_cast<uint32_t>(firstVertex);
bool firstVertexDirty = (mVertex.firstVertex != newFirstVertex);
if (firstVertexDirty)
{
mVertex.firstVertex = newFirstVertex;
mShaderConstantsDirty.set(gl::ShaderType::Vertex);
}
return firstVertexDirty;
}
void ShaderConstants11::onSamplerChange(gl::ShaderType shaderType,
unsigned int samplerIndex,
const gl::Texture &texture,
const gl::SamplerState &samplerState)
{
ASSERT(shaderType != gl::ShaderType::InvalidEnum);
if (updateSamplerMetadata(&mShaderSamplerMetadata[shaderType][samplerIndex], texture,
samplerState))
{
mNumActiveShaderSamplers[shaderType] = 0;
}
}
bool ShaderConstants11::onImageChange(gl::ShaderType shaderType,
unsigned int imageIndex,
const gl::ImageUnit &imageUnit)
{
ASSERT(shaderType != gl::ShaderType::InvalidEnum);
bool dirty = false;
if (imageUnit.access == GL_READ_ONLY)
{
if (updateImageMetadata(&mShaderReadonlyImageMetadata[shaderType][imageIndex], imageUnit))
{
mNumActiveShaderReadonlyImages[shaderType] = 0;
dirty = true;
}
}
else
{
if (updateImageMetadata(&mShaderImageMetadata[shaderType][imageIndex], imageUnit))
{
mNumActiveShaderImages[shaderType] = 0;
dirty = true;
}
}
return dirty;
}
void ShaderConstants11::onClipControlChange(bool lowerLeft, bool zeroToOne)
{
mVertex.clipControlOrigin = lowerLeft ? -1.0f : 1.0f;
mVertex.clipControlZeroToOne = zeroToOne ? 1.0f : 0.0f;
mShaderConstantsDirty.set(gl::ShaderType::Vertex);
}
bool ShaderConstants11::onClipDistancesEnabledChange(const uint32_t value)
{
ASSERT(value == (value & 0xFF));
const bool clipDistancesEnabledDirty = (mVertex.clipDistancesEnabled != value);
if (clipDistancesEnabledDirty)
{
mVertex.clipDistancesEnabled = value;
mShaderConstantsDirty.set(gl::ShaderType::Vertex);
}
return clipDistancesEnabledDirty;
}
angle::Result ShaderConstants11::updateBuffer(const gl::Context *context,
Renderer11 *renderer,
gl::ShaderType shaderType,
const ProgramD3D &programD3D,
const d3d11::Buffer &driverConstantBuffer)
{
// Re-upload the sampler meta-data if the current program uses more samplers
// than we previously uploaded.
const int numSamplers = programD3D.getUsedSamplerRange(shaderType).length();
const int numReadonlyImages = programD3D.getUsedImageRange(shaderType, true).length();
const int numImages = programD3D.getUsedImageRange(shaderType, false).length();
const bool dirty = mShaderConstantsDirty[shaderType] ||
(mNumActiveShaderSamplers[shaderType] < numSamplers) ||
(mNumActiveShaderReadonlyImages[shaderType] < numReadonlyImages) ||
(mNumActiveShaderImages[shaderType] < numImages);
const size_t dataSize = GetShaderConstantsStructSize(shaderType);
const uint8_t *samplerData =
reinterpret_cast<const uint8_t *>(mShaderSamplerMetadata[shaderType].data());
const size_t samplerDataSize = sizeof(SamplerMetadata) * numSamplers;
const uint8_t *readonlyImageData =
reinterpret_cast<const uint8_t *>(mShaderReadonlyImageMetadata[shaderType].data());
const size_t readonlyImageDataSize = sizeof(ImageMetadata) * numReadonlyImages;
const uint8_t *imageData =
reinterpret_cast<const uint8_t *>(mShaderImageMetadata[shaderType].data());
const size_t imageDataSize = sizeof(ImageMetadata) * numImages;
mNumActiveShaderSamplers[shaderType] = numSamplers;
mNumActiveShaderReadonlyImages[shaderType] = numReadonlyImages;
mNumActiveShaderImages[shaderType] = numImages;
mShaderConstantsDirty.set(shaderType, false);
const uint8_t *data = nullptr;
switch (shaderType)
{
case gl::ShaderType::Vertex:
data = reinterpret_cast<const uint8_t *>(&mVertex);
break;
case gl::ShaderType::Fragment:
data = reinterpret_cast<const uint8_t *>(&mPixel);
break;
case gl::ShaderType::Compute:
data = reinterpret_cast<const uint8_t *>(&mCompute);
break;
default:
UNREACHABLE();
break;
}
ASSERT(driverConstantBuffer.valid());
if (!dirty)
{
return angle::Result::Continue;
}
// Previous buffer contents are discarded, so we need to refresh the whole buffer.
D3D11_MAPPED_SUBRESOURCE mapping = {};
ANGLE_TRY(renderer->mapResource(context, driverConstantBuffer.get(), 0, D3D11_MAP_WRITE_DISCARD,
0, &mapping));
memcpy(mapping.pData, data, dataSize);
memcpy(static_cast<uint8_t *>(mapping.pData) + dataSize, samplerData,
sizeof(SamplerMetadata) * numSamplers);
memcpy(static_cast<uint8_t *>(mapping.pData) + dataSize + samplerDataSize, readonlyImageData,
readonlyImageDataSize);
memcpy(
static_cast<uint8_t *>(mapping.pData) + dataSize + samplerDataSize + readonlyImageDataSize,
imageData, imageDataSize);
renderer->getDeviceContext()->Unmap(driverConstantBuffer.get(), 0);
return angle::Result::Continue;
}
StateManager11::StateManager11(Renderer11 *renderer)
: mRenderer(renderer),
mInternalDirtyBits(),
mCurSampleAlphaToCoverage(false),
mCurBlendStateExt(),
mCurBlendColor(0, 0, 0, 0),
mCurSampleMask(0),
mCurStencilRef(0),
mCurStencilBackRef(0),
mCurStencilSize(0),
mCurScissorEnabled(false),
mCurScissorRect(),
mCurViewport(),
mCurNear(0.0f),
mCurFar(0.0f),
mViewportBounds(),
mRenderTargetIsDirty(true),
mCurPresentPathFastEnabled(false),
mCurPresentPathFastColorBufferHeight(0),
mDirtyCurrentValueAttribs(),
mCurrentValueAttribs(),
mCurrentInputLayout(),
mDirtyVertexBufferRange(gl::MAX_VERTEX_ATTRIBS, 0),
mCurrentPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_UNDEFINED),
mLastAppliedDrawMode(gl::PrimitiveMode::InvalidEnum),
mCullEverything(false),
mDirtySwizzles(false),
mAppliedIB(nullptr),
mAppliedIBFormat(DXGI_FORMAT_UNKNOWN),
mAppliedIBOffset(0),
mIndexBufferIsDirty(false),
mVertexDataManager(renderer),
mIndexDataManager(renderer),
mIsMultiviewEnabled(false),
mIndependentBlendStates(false),
mEmptySerial(mRenderer->generateSerial()),
mProgramD3D(nullptr),
mVertexArray11(nullptr),
mFramebuffer11(nullptr)
{
mCurDepthStencilState.depthTest = false;
mCurDepthStencilState.depthFunc = GL_LESS;
mCurDepthStencilState.depthMask = true;
mCurDepthStencilState.stencilTest = false;
mCurDepthStencilState.stencilMask = true;
mCurDepthStencilState.stencilFail = GL_KEEP;
mCurDepthStencilState.stencilPassDepthFail = GL_KEEP;
mCurDepthStencilState.stencilPassDepthPass = GL_KEEP;
mCurDepthStencilState.stencilWritemask = static_cast<GLuint>(-1);
mCurDepthStencilState.stencilBackFunc = GL_ALWAYS;
mCurDepthStencilState.stencilBackMask = static_cast<GLuint>(-1);
mCurDepthStencilState.stencilBackFail = GL_KEEP;
mCurDepthStencilState.stencilBackPassDepthFail = GL_KEEP;
mCurDepthStencilState.stencilBackPassDepthPass = GL_KEEP;
mCurDepthStencilState.stencilBackWritemask = static_cast<GLuint>(-1);
mCurRasterState.cullFace = false;
mCurRasterState.cullMode = gl::CullFaceMode::Back;
mCurRasterState.frontFace = GL_CCW;
mCurRasterState.polygonMode = gl::PolygonMode::Fill;
mCurRasterState.polygonOffsetPoint = false;
mCurRasterState.polygonOffsetLine = false;
mCurRasterState.polygonOffsetFill = false;
mCurRasterState.polygonOffsetFactor = 0.0f;
mCurRasterState.polygonOffsetUnits = 0.0f;
mCurRasterState.polygonOffsetClamp = 0.0f;
mCurRasterState.depthClamp = false;
mCurRasterState.pointDrawMode = false;
mCurRasterState.multiSample = false;
mCurRasterState.rasterizerDiscard = false;
mCurRasterState.dither = false;
// Start with all internal dirty bits set except the SRV and UAV bits.
mInternalDirtyBits.set();
mInternalDirtyBits.reset(DIRTY_BIT_GRAPHICS_SRV_STATE);
mInternalDirtyBits.reset(DIRTY_BIT_GRAPHICS_UAV_STATE);
mInternalDirtyBits.reset(DIRTY_BIT_COMPUTE_SRV_STATE);
mInternalDirtyBits.reset(DIRTY_BIT_COMPUTE_UAV_STATE);
mGraphicsDirtyBitsMask.set();
mGraphicsDirtyBitsMask.reset(DIRTY_BIT_COMPUTE_SRV_STATE);
mGraphicsDirtyBitsMask.reset(DIRTY_BIT_COMPUTE_UAV_STATE);
mComputeDirtyBitsMask.set(DIRTY_BIT_TEXTURE_AND_SAMPLER_STATE);
mComputeDirtyBitsMask.set(DIRTY_BIT_PROGRAM_UNIFORMS);
mComputeDirtyBitsMask.set(DIRTY_BIT_DRIVER_UNIFORMS);
mComputeDirtyBitsMask.set(DIRTY_BIT_PROGRAM_UNIFORM_BUFFERS);
mComputeDirtyBitsMask.set(DIRTY_BIT_SHADERS);
mComputeDirtyBitsMask.set(DIRTY_BIT_COMPUTE_SRV_STATE);
mComputeDirtyBitsMask.set(DIRTY_BIT_COMPUTE_UAV_STATE);
// Initially all current value attributes must be updated on first use.
mDirtyCurrentValueAttribs.set();
mCurrentVertexBuffers.fill(nullptr);
mCurrentVertexStrides.fill(std::numeric_limits<UINT>::max());
mCurrentVertexOffsets.fill(std::numeric_limits<UINT>::max());
}
StateManager11::~StateManager11() {}
template <typename SRVType>
void StateManager11::setShaderResourceInternal(gl::ShaderType shaderType,
UINT resourceSlot,
const SRVType *srv)
{
auto *currentSRVs = getSRVCache(shaderType);
ASSERT(static_cast<size_t>(resourceSlot) < currentSRVs->size());
const ViewRecord<D3D11_SHADER_RESOURCE_VIEW_DESC> &record = (*currentSRVs)[resourceSlot];
if (record.view != reinterpret_cast<uintptr_t>(srv))
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
ID3D11ShaderResourceView *srvPtr = srv ? srv->get() : nullptr;
if (srvPtr)
{
uintptr_t resource = reinterpret_cast<uintptr_t>(GetViewResource(srvPtr));
unsetConflictingUAVs(gl::PipelineType::GraphicsPipeline, gl::ShaderType::Compute,
resource, nullptr);
}
switch (shaderType)
{
case gl::ShaderType::Vertex:
deviceContext->VSSetShaderResources(resourceSlot, 1, &srvPtr);
break;
case gl::ShaderType::Fragment:
deviceContext->PSSetShaderResources(resourceSlot, 1, &srvPtr);
break;
case gl::ShaderType::Compute:
{
if (srvPtr)
{
uintptr_t resource = reinterpret_cast<uintptr_t>(GetViewResource(srvPtr));
unsetConflictingRTVs(resource);
}
deviceContext->CSSetShaderResources(resourceSlot, 1, &srvPtr);
break;
}
default:
UNREACHABLE();
}
currentSRVs->update(resourceSlot, srvPtr);
}
}
template <typename UAVType>
void StateManager11::setUnorderedAccessViewInternal(UINT resourceSlot,
const UAVType *uav,
UAVList *uavList)
{
ASSERT(static_cast<size_t>(resourceSlot) < mCurComputeUAVs.size());
const ViewRecord<D3D11_UNORDERED_ACCESS_VIEW_DESC> &record = mCurComputeUAVs[resourceSlot];
if (record.view != reinterpret_cast<uintptr_t>(uav))
{
ID3D11UnorderedAccessView *uavPtr = uav ? uav->get() : nullptr;
// We need to make sure that resource being set to UnorderedAccessView slot |resourceSlot|
// is not bound on SRV.
if (uavPtr)
{
uintptr_t resource = reinterpret_cast<uintptr_t>(GetViewResource(uavPtr));
unsetConflictingSRVs(gl::PipelineType::ComputePipeline, gl::ShaderType::Vertex,
resource, nullptr, false);
unsetConflictingSRVs(gl::PipelineType::ComputePipeline, gl::ShaderType::Fragment,
resource, nullptr, false);
unsetConflictingSRVs(gl::PipelineType::ComputePipeline, gl::ShaderType::Compute,
resource, nullptr, false);
}
uavList->data[resourceSlot] = uavPtr;
if (static_cast<int>(resourceSlot) > uavList->highestUsed)
{
uavList->highestUsed = resourceSlot;
}
mCurComputeUAVs.update(resourceSlot, uavPtr);
}
}
void StateManager11::updateStencilSizeIfChanged(bool depthStencilInitialized,
unsigned int stencilSize)
{
if (!depthStencilInitialized || stencilSize != mCurStencilSize)
{
mCurStencilSize = stencilSize;
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
}
}
void StateManager11::checkPresentPath(const gl::Context *context)
{
const auto *framebuffer = context->getState().getDrawFramebuffer();
const auto *firstColorAttachment = framebuffer->getFirstColorAttachment();
const bool clipSpaceOriginUpperLeft =
context->getState().getClipOrigin() == gl::ClipOrigin::UpperLeft;
const bool presentPathFastActive =
UsePresentPathFast(mRenderer, firstColorAttachment) || clipSpaceOriginUpperLeft;
const int colorBufferHeight = firstColorAttachment ? firstColorAttachment->getSize().height : 0;
if ((mCurPresentPathFastEnabled != presentPathFastActive) ||
(presentPathFastActive && (colorBufferHeight != mCurPresentPathFastColorBufferHeight)))
{
mCurPresentPathFastEnabled = presentPathFastActive;
mCurPresentPathFastColorBufferHeight = colorBufferHeight;
// Scissor rect may need to be vertically inverted
mInternalDirtyBits.set(DIRTY_BIT_SCISSOR_STATE);
// Cull Mode may need to be inverted
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
// Viewport may need to be vertically inverted
invalidateViewport(context);
}
}
angle::Result StateManager11::updateStateForCompute(const gl::Context *context,
GLuint numGroupsX,
GLuint numGroupsY,
GLuint numGroupsZ)
{
mShaderConstants.setComputeWorkGroups(numGroupsX, numGroupsY, numGroupsZ);
if (mProgramD3D->updateSamplerMapping() == ProgramD3D::SamplerMapping::WasDirty)
{
invalidateTexturesAndSamplers();
}
if (mDirtySwizzles)
{
ANGLE_TRY(generateSwizzlesForShader(context, gl::ShaderType::Compute));
mDirtySwizzles = false;
}
if (mProgramD3D->anyShaderUniformsDirty())
{
mInternalDirtyBits.set(DIRTY_BIT_PROGRAM_UNIFORMS);
}
auto dirtyBitsCopy = mInternalDirtyBits & mComputeDirtyBitsMask;
mInternalDirtyBits &= ~mComputeDirtyBitsMask;
for (auto iter = dirtyBitsCopy.begin(), end = dirtyBitsCopy.end(); iter != end; ++iter)
{
switch (*iter)
{
case DIRTY_BIT_COMPUTE_SRV_STATE:
// Avoid to call syncTexturesForCompute function two times.
iter.resetLaterBit(DIRTY_BIT_TEXTURE_AND_SAMPLER_STATE);
ANGLE_TRY(syncTexturesForCompute(context));
break;
case DIRTY_BIT_COMPUTE_UAV_STATE:
ANGLE_TRY(syncUAVsForCompute(context));
break;
case DIRTY_BIT_TEXTURE_AND_SAMPLER_STATE:
ANGLE_TRY(syncTexturesForCompute(context));
break;
case DIRTY_BIT_PROGRAM_UNIFORMS:
case DIRTY_BIT_DRIVER_UNIFORMS:
ANGLE_TRY(applyComputeUniforms(context, mProgramD3D));
break;
case DIRTY_BIT_PROGRAM_UNIFORM_BUFFERS:
ANGLE_TRY(syncUniformBuffers(context));
break;
case DIRTY_BIT_SHADERS:
ANGLE_TRY(syncProgramForCompute(context));
break;
default:
UNREACHABLE();
break;
}
}
return angle::Result::Continue;
}
void StateManager11::syncState(const gl::Context *context,
const gl::State::DirtyBits &dirtyBits,
const gl::State::ExtendedDirtyBits &extendedDirtyBits,
gl::Command command)
{
if (!dirtyBits.any())
{
return;
}
const gl::State &state = context->getState();
for (size_t dirtyBit : dirtyBits)
{
switch (dirtyBit)
{
case gl::State::DIRTY_BIT_BLEND_EQUATIONS:
{
const gl::BlendStateExt &blendStateExt = state.getBlendStateExt();
ASSERT(mCurBlendStateExt.getDrawBufferCount() ==
blendStateExt.getDrawBufferCount());
// Compare blend equations only for buffers with blending enabled because
// subsequent sync stages enforce default values for buffers with blending disabled.
if ((blendStateExt.getEnabledMask() &
mCurBlendStateExt.compareEquations(blendStateExt))
.any())
{
mInternalDirtyBits.set(DIRTY_BIT_BLEND_STATE);
}
break;
}
case gl::State::DIRTY_BIT_BLEND_FUNCS:
{
const gl::BlendStateExt &blendStateExt = state.getBlendStateExt();
ASSERT(mCurBlendStateExt.getDrawBufferCount() ==
blendStateExt.getDrawBufferCount());
// Compare blend factors only for buffers with blending enabled because
// subsequent sync stages enforce default values for buffers with blending disabled.
if ((blendStateExt.getEnabledMask() &
mCurBlendStateExt.compareFactors(blendStateExt))
.any())
{
mInternalDirtyBits.set(DIRTY_BIT_BLEND_STATE);
}
break;
}
case gl::State::DIRTY_BIT_BLEND_ENABLED:
{
if (state.getBlendStateExt().getEnabledMask() != mCurBlendStateExt.getEnabledMask())
{
mInternalDirtyBits.set(DIRTY_BIT_BLEND_STATE);
}
break;
}
case gl::State::DIRTY_BIT_SAMPLE_ALPHA_TO_COVERAGE_ENABLED:
if (state.isSampleAlphaToCoverageEnabled() != mCurSampleAlphaToCoverage)
{
mInternalDirtyBits.set(DIRTY_BIT_BLEND_STATE);
}
break;
case gl::State::DIRTY_BIT_DITHER_ENABLED:
if (state.getRasterizerState().dither != mCurRasterState.dither)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
}
break;
case gl::State::DIRTY_BIT_COLOR_MASK:
{
if (state.getBlendStateExt().getColorMaskBits() !=
mCurBlendStateExt.getColorMaskBits())
{
mInternalDirtyBits.set(DIRTY_BIT_BLEND_STATE);
}
break;
}
case gl::State::DIRTY_BIT_BLEND_COLOR:
if (state.getBlendColor() != mCurBlendColor)
{
mInternalDirtyBits.set(DIRTY_BIT_BLEND_STATE);
}
break;
// Depth and stencil redundant state changes are guarded in the
// frontend so for related cases here just set the dirty bit.
case gl::State::DIRTY_BIT_DEPTH_MASK:
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
break;
case gl::State::DIRTY_BIT_DEPTH_TEST_ENABLED:
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
break;
case gl::State::DIRTY_BIT_DEPTH_FUNC:
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
break;
case gl::State::DIRTY_BIT_STENCIL_TEST_ENABLED:
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
break;
case gl::State::DIRTY_BIT_STENCIL_FUNCS_FRONT:
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
break;
case gl::State::DIRTY_BIT_STENCIL_FUNCS_BACK:
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
break;
case gl::State::DIRTY_BIT_STENCIL_WRITEMASK_FRONT:
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
break;
case gl::State::DIRTY_BIT_STENCIL_WRITEMASK_BACK:
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
break;
case gl::State::DIRTY_BIT_STENCIL_OPS_FRONT:
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
break;
case gl::State::DIRTY_BIT_STENCIL_OPS_BACK:
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
break;
case gl::State::DIRTY_BIT_CULL_FACE_ENABLED:
if (state.getRasterizerState().cullFace != mCurRasterState.cullFace)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
mInternalDirtyBits.set(DIRTY_BIT_PRIMITIVE_TOPOLOGY);
}
break;
case gl::State::DIRTY_BIT_CULL_FACE:
if (state.getRasterizerState().cullMode != mCurRasterState.cullMode)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
mInternalDirtyBits.set(DIRTY_BIT_PRIMITIVE_TOPOLOGY);
}
break;
case gl::State::DIRTY_BIT_FRONT_FACE:
if (state.getRasterizerState().frontFace != mCurRasterState.frontFace)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
mInternalDirtyBits.set(DIRTY_BIT_PRIMITIVE_TOPOLOGY);
}
break;
case gl::State::DIRTY_BIT_POLYGON_OFFSET_FILL_ENABLED:
if (state.getRasterizerState().polygonOffsetFill !=
mCurRasterState.polygonOffsetFill)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
}
break;
case gl::State::DIRTY_BIT_POLYGON_OFFSET:
{
const gl::RasterizerState &rasterState = state.getRasterizerState();
if (rasterState.polygonOffsetFactor != mCurRasterState.polygonOffsetFactor ||
rasterState.polygonOffsetUnits != mCurRasterState.polygonOffsetUnits ||
rasterState.polygonOffsetClamp != mCurRasterState.polygonOffsetClamp)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
}
break;
}
case gl::State::DIRTY_BIT_RASTERIZER_DISCARD_ENABLED:
if (state.getRasterizerState().rasterizerDiscard !=
mCurRasterState.rasterizerDiscard)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
// Enabling/disabling rasterizer discard affects the pixel shader.
invalidateShaders();
}
break;
case gl::State::DIRTY_BIT_SCISSOR:
if (state.getScissor() != mCurScissorRect)
{
mInternalDirtyBits.set(DIRTY_BIT_SCISSOR_STATE);
}
break;
case gl::State::DIRTY_BIT_SCISSOR_TEST_ENABLED:
if (state.isScissorTestEnabled() != mCurScissorEnabled)
{
mInternalDirtyBits.set(DIRTY_BIT_SCISSOR_STATE);
// Rasterizer state update needs mCurScissorsEnabled and updates when it changes
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
}
break;
case gl::State::DIRTY_BIT_DEPTH_RANGE:
invalidateViewport(context);
break;
case gl::State::DIRTY_BIT_VIEWPORT:
if (state.getViewport() != mCurViewport)
{
invalidateViewport(context);
}
break;
case gl::State::DIRTY_BIT_DRAW_FRAMEBUFFER_BINDING:
invalidateRenderTarget();
if (mIsMultiviewEnabled)
{
handleMultiviewDrawFramebufferChange(context);
}
mFramebuffer11 = GetImplAs<Framebuffer11>(state.getDrawFramebuffer());
break;
case gl::State::DIRTY_BIT_VERTEX_ARRAY_BINDING:
invalidateVertexBuffer();
// Force invalidate the current value attributes, since the VertexArray11 keeps an
// internal cache of TranslatedAttributes, and they CurrentValue attributes are
// owned by the StateManager11/Context.
mDirtyCurrentValueAttribs.set();
// Invalidate the cached index buffer.
invalidateIndexBuffer();
mVertexArray11 = GetImplAs<VertexArray11>(state.getVertexArray());
break;
case gl::State::DIRTY_BIT_UNIFORM_BUFFER_BINDINGS:
invalidateProgramUniformBuffers();
break;
case gl::State::DIRTY_BIT_ATOMIC_COUNTER_BUFFER_BINDING:
invalidateProgramAtomicCounterBuffers();
break;
case gl::State::DIRTY_BIT_SHADER_STORAGE_BUFFER_BINDING:
invalidateProgramShaderStorageBuffers();
break;
case gl::State::DIRTY_BIT_TEXTURE_BINDINGS:
invalidateTexturesAndSamplers();
break;
case gl::State::DIRTY_BIT_SAMPLER_BINDINGS:
invalidateTexturesAndSamplers();
break;
case gl::State::DIRTY_BIT_IMAGE_BINDINGS:
invalidateImageBindings();
break;
case gl::State::DIRTY_BIT_TRANSFORM_FEEDBACK_BINDING:
invalidateTransformFeedback();
break;
case gl::State::DIRTY_BIT_PROGRAM_BINDING:
mProgramD3D = GetImplAs<ProgramD3D>(state.getProgram());
break;
case gl::State::DIRTY_BIT_PROGRAM_EXECUTABLE:
{
invalidateShaders();
invalidateTexturesAndSamplers();
invalidateProgramUniforms();
invalidateProgramUniformBuffers();
invalidateProgramAtomicCounterBuffers();
invalidateProgramShaderStorageBuffers();
invalidateDriverUniforms();
const gl::ProgramExecutable *executable = state.getProgramExecutable();
if (!executable || command != gl::Command::Dispatch)
{
mInternalDirtyBits.set(DIRTY_BIT_PRIMITIVE_TOPOLOGY);
invalidateVertexBuffer();
invalidateRenderTarget();
// If OVR_multiview2 is enabled, the attribute divisor has to be updated for
// each binding. When using compute, there could be no vertex array.
if (mIsMultiviewEnabled && mVertexArray11)
{
ASSERT(mProgramD3D);
ASSERT(mVertexArray11 == GetImplAs<VertexArray11>(state.getVertexArray()));
const gl::ProgramState &programState = mProgramD3D->getState();
int numViews =
programState.usesMultiview() ? programState.getNumViews() : 1;
mVertexArray11->markAllAttributeDivisorsForAdjustment(numViews);
}
}
break;
}
case gl::State::DIRTY_BIT_CURRENT_VALUES:
{
for (auto attribIndex : state.getAndResetDirtyCurrentValues())
{
invalidateCurrentValueAttrib(attribIndex);
}
break;
}
case gl::State::DIRTY_BIT_PROVOKING_VERTEX:
invalidateShaders();
break;
case gl::State::DIRTY_BIT_EXTENDED:
{
for (size_t extendedDirtyBit : extendedDirtyBits)
{
switch (extendedDirtyBit)
{
case gl::State::EXTENDED_DIRTY_BIT_CLIP_CONTROL:
checkPresentPath(context);
break;
case gl::State::EXTENDED_DIRTY_BIT_CLIP_DISTANCES:
if (mShaderConstants.onClipDistancesEnabledChange(
state.getEnabledClipDistances().bits()))
{
mInternalDirtyBits.set(DIRTY_BIT_DRIVER_UNIFORMS);
}
break;
case gl::State::EXTENDED_DIRTY_BIT_DEPTH_CLAMP_ENABLED:
if (state.getRasterizerState().depthClamp != mCurRasterState.depthClamp)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
}
break;
case gl::State::EXTENDED_DIRTY_BIT_POLYGON_MODE:
if (state.getRasterizerState().polygonMode !=
mCurRasterState.polygonMode)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
}
break;
case gl::State::EXTENDED_DIRTY_BIT_POLYGON_OFFSET_LINE_ENABLED:
if (state.getRasterizerState().polygonOffsetLine !=
mCurRasterState.polygonOffsetLine)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
}
break;
}
}
break;
}
default:
break;
}
}
// TODO(jmadill): Input layout and vertex buffer state.
}
void StateManager11::handleMultiviewDrawFramebufferChange(const gl::Context *context)
{
const auto &glState = context->getState();
const gl::Framebuffer *drawFramebuffer = glState.getDrawFramebuffer();
ASSERT(drawFramebuffer != nullptr);
if (drawFramebuffer->isMultiview())
{
// Because the base view index is applied as an offset to the 2D texture array when the
// RTV is created, we just have to pass a boolean to select which code path is to be
// used.
mShaderConstants.setMultiviewWriteToViewportIndex(0.0f);
}
}
angle::Result StateManager11::syncBlendState(const gl::Context *context,
const gl::BlendStateExt &blendStateExt,
const gl::ColorF &blendColor,
unsigned int sampleMask,
bool sampleAlphaToCoverage,
bool emulateConstantAlpha)
{
const d3d11::BlendState *dxBlendState = nullptr;
const d3d11::BlendStateKey &key = RenderStateCache::GetBlendStateKey(
context, mFramebuffer11, blendStateExt, sampleAlphaToCoverage);
ANGLE_TRY(mRenderer->getBlendState(context, key, &dxBlendState));
ASSERT(dxBlendState != nullptr);
// D3D11 does not support CONSTANT_ALPHA as source or destination color factor, so ANGLE sets
// the factor to CONSTANT_COLOR and swizzles the color value to aaaa. For this reason, it's
// impossible to simultaneously use CONSTANT_ALPHA and CONSTANT_COLOR as source or destination
// color factors in the same blend state. This is enforced in the validation layer.
float blendColors[4] = {0.0f};
blendColors[0] = emulateConstantAlpha ? blendColor.alpha : blendColor.red;
blendColors[1] = emulateConstantAlpha ? blendColor.alpha : blendColor.green;
blendColors[2] = emulateConstantAlpha ? blendColor.alpha : blendColor.blue;
blendColors[3] = blendColor.alpha;
mRenderer->getDeviceContext()->OMSetBlendState(dxBlendState->get(), blendColors, sampleMask);
mCurBlendStateExt = blendStateExt;
mCurBlendColor = blendColor;
mCurSampleMask = sampleMask;
mCurSampleAlphaToCoverage = sampleAlphaToCoverage;
return angle::Result::Continue;
}
angle::Result StateManager11::syncDepthStencilState(const gl::Context *context)
{
const gl::State &glState = context->getState();
mCurDepthStencilState = glState.getDepthStencilState();
mCurStencilRef = glState.getStencilRef();
mCurStencilBackRef = glState.getStencilBackRef();
// get the maximum size of the stencil ref
unsigned int maxStencil = 0;
if (mCurDepthStencilState.stencilTest && mCurStencilSize > 0)
{
maxStencil = (1 << mCurStencilSize) - 1;
}
ASSERT((mCurDepthStencilState.stencilWritemask & maxStencil) ==
(mCurDepthStencilState.stencilBackWritemask & maxStencil));
ASSERT(gl::clamp(mCurStencilRef, 0, static_cast<int>(maxStencil)) ==
gl::clamp(mCurStencilBackRef, 0, static_cast<int>(maxStencil)));
ASSERT((mCurDepthStencilState.stencilMask & maxStencil) ==
(mCurDepthStencilState.stencilBackMask & maxStencil));
gl::DepthStencilState modifiedGLState = glState.getDepthStencilState();
ASSERT(mCurDisableDepth.valid() && mCurDisableStencil.valid());
if (mCurDisableDepth.value())
{
modifiedGLState.depthTest = false;
modifiedGLState.depthMask = false;
}
if (mCurDisableStencil.value())
{
modifiedGLState.stencilTest = false;
}
if (!modifiedGLState.stencilTest)
{
modifiedGLState.stencilWritemask = 0;
modifiedGLState.stencilBackWritemask = 0;
}
// If STENCIL_TEST is disabled in glState, stencil testing and writing should be disabled.
// Verify that's true in the modifiedGLState so it is propagated to d3dState.
ASSERT(glState.getDepthStencilState().stencilTest ||
(!modifiedGLState.stencilTest && modifiedGLState.stencilWritemask == 0 &&
modifiedGLState.stencilBackWritemask == 0));
const d3d11::DepthStencilState *d3dState = nullptr;
ANGLE_TRY(mRenderer->getDepthStencilState(context, modifiedGLState, &d3dState));
ASSERT(d3dState);
// Max D3D11 stencil reference value is 0xFF,
// corresponding to the max 8 bits in a stencil buffer
// GL specifies we should clamp the ref value to the
// nearest bit depth when doing stencil ops
static_assert(D3D11_DEFAULT_STENCIL_READ_MASK == 0xFF,
"Unexpected value of D3D11_DEFAULT_STENCIL_READ_MASK");
static_assert(D3D11_DEFAULT_STENCIL_WRITE_MASK == 0xFF,
"Unexpected value of D3D11_DEFAULT_STENCIL_WRITE_MASK");
UINT dxStencilRef = static_cast<UINT>(gl::clamp(mCurStencilRef, 0, 0xFF));
mRenderer->getDeviceContext()->OMSetDepthStencilState(d3dState->get(), dxStencilRef);
return angle::Result::Continue;
}
angle::Result StateManager11::syncRasterizerState(const gl::Context *context,
gl::PrimitiveMode mode)
{
// TODO: Remove pointDrawMode and multiSample from gl::RasterizerState.
gl::RasterizerState rasterState = context->getState().getRasterizerState();
rasterState.pointDrawMode = (mode == gl::PrimitiveMode::Points);
rasterState.multiSample = mCurRasterState.multiSample;
ID3D11RasterizerState *dxRasterState = nullptr;
if (mCurPresentPathFastEnabled)
{
gl::RasterizerState modifiedRasterState = rasterState;
// If prseent path fast is active then we need invert the front face state.
// This ensures that both gl_FrontFacing is correct, and front/back culling
// is performed correctly.
if (modifiedRasterState.frontFace == GL_CCW)
{
modifiedRasterState.frontFace = GL_CW;
}
else
{
ASSERT(modifiedRasterState.frontFace == GL_CW);
modifiedRasterState.frontFace = GL_CCW;
}
ANGLE_TRY(mRenderer->getRasterizerState(context, modifiedRasterState, mCurScissorEnabled,
&dxRasterState));
}
else
{
ANGLE_TRY(mRenderer->getRasterizerState(context, rasterState, mCurScissorEnabled,
&dxRasterState));
}
mRenderer->getDeviceContext()->RSSetState(dxRasterState);
mCurRasterState = rasterState;
return angle::Result::Continue;
}
void StateManager11::syncScissorRectangle(const gl::Context *context)
{
const auto &glState = context->getState();
gl::Framebuffer *framebuffer = glState.getDrawFramebuffer();
const gl::Rectangle &scissor = glState.getScissor();
const bool enabled = glState.isScissorTestEnabled();
mCurScissorOffset = framebuffer->getSurfaceTextureOffset();
int scissorX = scissor.x + mCurScissorOffset.x;
int scissorY = scissor.y + mCurScissorOffset.y;
if (mCurPresentPathFastEnabled && glState.getClipOrigin() == gl::ClipOrigin::LowerLeft)
{
scissorY = mCurPresentPathFastColorBufferHeight - scissor.height - scissor.y;
}
if (enabled)
{
D3D11_RECT rect;
int x = scissorX;
int y = scissorY;
rect.left = std::max(0, x);
rect.top = std::max(0, y);
rect.right = x + std::max(0, scissor.width);
rect.bottom = y + std::max(0, scissor.height);
mRenderer->getDeviceContext()->RSSetScissorRects(1, &rect);
}
mCurScissorRect = scissor;
mCurScissorEnabled = enabled;
}
void StateManager11::syncViewport(const gl::Context *context)
{
const auto &glState = context->getState();
gl::Framebuffer *framebuffer = glState.getDrawFramebuffer();
float actualZNear = gl::clamp01(glState.getNearPlane());
float actualZFar = gl::clamp01(glState.getFarPlane());
const auto &caps = context->getCaps();
int dxMaxViewportBoundsX = caps.maxViewportWidth;
int dxMaxViewportBoundsY = caps.maxViewportHeight;
int dxMinViewportBoundsX = -dxMaxViewportBoundsX;
int dxMinViewportBoundsY = -dxMaxViewportBoundsY;
bool is9_3 = mRenderer->getRenderer11DeviceCaps().featureLevel <= D3D_FEATURE_LEVEL_9_3;
if (is9_3)
{
// Feature Level 9 viewports shouldn't exceed the dimensions of the rendertarget.
dxMaxViewportBoundsX = static_cast<int>(mViewportBounds.width);
dxMaxViewportBoundsY = static_cast<int>(mViewportBounds.height);
dxMinViewportBoundsX = 0;
dxMinViewportBoundsY = 0;
}
bool clipSpaceOriginLowerLeft = glState.getClipOrigin() == gl::ClipOrigin::LowerLeft;
mShaderConstants.onClipControlChange(clipSpaceOriginLowerLeft,
glState.isClipDepthModeZeroToOne());
const auto &viewport = glState.getViewport();
int dxViewportTopLeftX = 0;
int dxViewportTopLeftY = 0;
int dxViewportWidth = 0;
int dxViewportHeight = 0;
mCurViewportOffset = framebuffer->getSurfaceTextureOffset();
dxViewportTopLeftX =
gl::clamp(viewport.x + mCurViewportOffset.x, dxMinViewportBoundsX, dxMaxViewportBoundsX);
dxViewportTopLeftY =
gl::clamp(viewport.y + mCurViewportOffset.y, dxMinViewportBoundsY, dxMaxViewportBoundsY);
dxViewportWidth = gl::clamp(viewport.width, 0, dxMaxViewportBoundsX - dxViewportTopLeftX);
dxViewportHeight = gl::clamp(viewport.height, 0, dxMaxViewportBoundsY - dxViewportTopLeftY);
D3D11_VIEWPORT dxViewport;
dxViewport.TopLeftX = static_cast<float>(dxViewportTopLeftX);
if (mCurPresentPathFastEnabled && clipSpaceOriginLowerLeft)
{
// When present path fast is active and we're rendering to framebuffer 0, we must invert
// the viewport in Y-axis.
// NOTE: We delay the inversion until right before the call to RSSetViewports, and leave
// dxViewportTopLeftY unchanged. This allows us to calculate viewAdjust below using the
// unaltered dxViewportTopLeftY value.
dxViewport.TopLeftY = static_cast<float>(mCurPresentPathFastColorBufferHeight -
dxViewportTopLeftY - dxViewportHeight);
}
else
{
dxViewport.TopLeftY = static_cast<float>(dxViewportTopLeftY);
}
// The es 3.1 spec section 9.2 states that, "If there are no attachments, rendering
// will be limited to a rectangle having a lower left of (0, 0) and an upper right of
// (width, height), where width and height are the framebuffer object's default width
// and height." See http://anglebug.com/1594
// If the Framebuffer has no color attachment and the default width or height is smaller
// than the current viewport, use the smaller of the two sizes.
// If framebuffer default width or height is 0, the params should not set.
if (!framebuffer->getFirstNonNullAttachment() &&
(framebuffer->getDefaultWidth() || framebuffer->getDefaultHeight()))
{
dxViewport.Width =
static_cast<GLfloat>(std::min(viewport.width, framebuffer->getDefaultWidth()));
dxViewport.Height =
static_cast<GLfloat>(std::min(viewport.height, framebuffer->getDefaultHeight()));
}
else
{
dxViewport.Width = static_cast<float>(dxViewportWidth);
dxViewport.Height = static_cast<float>(dxViewportHeight);
}
dxViewport.MinDepth = actualZNear;
dxViewport.MaxDepth = actualZFar;
mRenderer->getDeviceContext()->RSSetViewports(1, &dxViewport);
mCurViewport = viewport;
mCurNear = actualZNear;
mCurFar = actualZFar;
const D3D11_VIEWPORT adjustViewport = {static_cast<FLOAT>(dxViewportTopLeftX),
static_cast<FLOAT>(dxViewportTopLeftY),
static_cast<FLOAT>(dxViewportWidth),
static_cast<FLOAT>(dxViewportHeight),
actualZNear,
actualZFar};
mShaderConstants.onViewportChange(viewport, adjustViewport, mCurViewportOffset, is9_3,
mCurPresentPathFastEnabled);
}
void StateManager11::invalidateRenderTarget()
{
mRenderTargetIsDirty = true;
}
void StateManager11::processFramebufferInvalidation(const gl::Context *context)
{
ASSERT(mRenderTargetIsDirty);
ASSERT(context);
mInternalDirtyBits.set(DIRTY_BIT_RENDER_TARGET);
// The pixel shader is dependent on the output layout.
invalidateShaders();
// The D3D11 blend state is heavily dependent on the current render target.
mInternalDirtyBits.set(DIRTY_BIT_BLEND_STATE);
gl::Framebuffer *fbo = context->getState().getDrawFramebuffer();
ASSERT(fbo);
// Dirty scissor and viewport because surface texture offset might have changed.
if (mCurViewportOffset != fbo->getSurfaceTextureOffset())
{
mInternalDirtyBits.set(DIRTY_BIT_VIEWPORT_STATE);
}
if (mCurScissorOffset != fbo->getSurfaceTextureOffset())
{
mInternalDirtyBits.set(DIRTY_BIT_SCISSOR_STATE);
}
// Disable the depth test/depth write if we are using a stencil-only attachment.
// This is because ANGLE emulates stencil-only with D24S8 on D3D11 - we should neither read
// nor write to the unused depth part of this emulated texture.
bool disableDepth = (!fbo->hasDepth() && fbo->hasStencil());
// Similarly we disable the stencil portion of the DS attachment if the app only binds depth.
bool disableStencil = (fbo->hasDepth() && !fbo->hasStencil());
if (!mCurDisableDepth.valid() || disableDepth != mCurDisableDepth.value() ||
!mCurDisableStencil.valid() || disableStencil != mCurDisableStencil.value())
{
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
mCurDisableDepth = disableDepth;
mCurDisableStencil = disableStencil;
}
bool multiSample = (fbo->getSamples(context) != 0);
if (multiSample != mCurRasterState.multiSample)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
mCurRasterState.multiSample = multiSample;
}
checkPresentPath(context);
if (mRenderer->getRenderer11DeviceCaps().featureLevel <= D3D_FEATURE_LEVEL_9_3)
{
const auto *firstAttachment = fbo->getFirstNonNullAttachment();
if (firstAttachment)
{
const auto &size = firstAttachment->getSize();
if (mViewportBounds.width != size.width || mViewportBounds.height != size.height)
{
mViewportBounds = gl::Extents(size.width, size.height, 1);
invalidateViewport(context);
}
}
}
}
void StateManager11::invalidateBoundViews()
{
for (SRVCache &curShaderSRV : mCurShaderSRVs)
{
curShaderSRV.clear();
}
invalidateRenderTarget();
}
void StateManager11::invalidateVertexBuffer()
{
unsigned int limit = std::min<unsigned int>(mRenderer->getNativeCaps().maxVertexAttributes,
gl::MAX_VERTEX_ATTRIBS);
mDirtyVertexBufferRange = gl::RangeUI(0, limit);
invalidateInputLayout();
invalidateShaders();
mInternalDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_ATTRIBS);
}
void StateManager11::invalidateViewport(const gl::Context *context)
{
mInternalDirtyBits.set(DIRTY_BIT_VIEWPORT_STATE);
// Viewport affects the driver constants.
invalidateDriverUniforms();
}
void StateManager11::invalidateTexturesAndSamplers()
{
mInternalDirtyBits.set(DIRTY_BIT_TEXTURE_AND_SAMPLER_STATE);
invalidateSwizzles();
// Texture state affects the driver uniforms (base level, etc).
invalidateDriverUniforms();
}
void StateManager11::invalidateSwizzles()
{
mDirtySwizzles = true;
}
void StateManager11::invalidateProgramUniforms()
{
mInternalDirtyBits.set(DIRTY_BIT_PROGRAM_UNIFORMS);
}
void StateManager11::invalidateDriverUniforms()
{
mInternalDirtyBits.set(DIRTY_BIT_DRIVER_UNIFORMS);
}
void StateManager11::invalidateProgramUniformBuffers()
{
mInternalDirtyBits.set(DIRTY_BIT_PROGRAM_UNIFORM_BUFFERS);
}
void StateManager11::invalidateProgramAtomicCounterBuffers()
{
mInternalDirtyBits.set(DIRTY_BIT_GRAPHICS_UAV_STATE);
mInternalDirtyBits.set(DIRTY_BIT_COMPUTE_UAV_STATE);
}
void StateManager11::invalidateProgramShaderStorageBuffers()
{
mInternalDirtyBits.set(DIRTY_BIT_GRAPHICS_UAV_STATE);
mInternalDirtyBits.set(DIRTY_BIT_COMPUTE_UAV_STATE);
}
void StateManager11::invalidateImageBindings()
{
mInternalDirtyBits.set(DIRTY_BIT_TEXTURE_AND_SAMPLER_STATE);
mInternalDirtyBits.set(DIRTY_BIT_GRAPHICS_SRV_STATE);
mInternalDirtyBits.set(DIRTY_BIT_GRAPHICS_UAV_STATE);
mInternalDirtyBits.set(DIRTY_BIT_COMPUTE_SRV_STATE);
mInternalDirtyBits.set(DIRTY_BIT_COMPUTE_UAV_STATE);
}
void StateManager11::invalidateConstantBuffer(unsigned int slot)
{
if (slot == d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DRIVER)
{
invalidateDriverUniforms();
}
else if (slot == d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DEFAULT_UNIFORM_BLOCK)
{
invalidateProgramUniforms();
}
else
{
invalidateProgramUniformBuffers();
}
}
void StateManager11::invalidateShaders()
{
mInternalDirtyBits.set(DIRTY_BIT_SHADERS);
}
void StateManager11::invalidateTransformFeedback()
{
// Transform feedback affects the stream-out geometry shader.
invalidateShaders();
mInternalDirtyBits.set(DIRTY_BIT_TRANSFORM_FEEDBACK);
// syncPrimitiveTopology checks the transform feedback state.
mInternalDirtyBits.set(DIRTY_BIT_PRIMITIVE_TOPOLOGY);
}
void StateManager11::invalidateInputLayout()
{
mInternalDirtyBits.set(DIRTY_BIT_VERTEX_BUFFERS_AND_INPUT_LAYOUT);
}
void StateManager11::invalidateIndexBuffer()
{
mIndexBufferIsDirty = true;
}
void StateManager11::setRenderTarget(ID3D11RenderTargetView *rtv, ID3D11DepthStencilView *dsv)
{
if (rtv)
{
unsetConflictingView(gl::PipelineType::GraphicsPipeline, rtv, true);
}
if (dsv)
{
unsetConflictingView(gl::PipelineType::GraphicsPipeline, dsv, true);
}
mRenderer->getDeviceContext()->OMSetRenderTargets(1, &rtv, dsv);
mCurRTVs.clear();
mCurRTVs.update(0, rtv);
mInternalDirtyBits.set(DIRTY_BIT_RENDER_TARGET);
}
void StateManager11::setRenderTargets(ID3D11RenderTargetView **rtvs,
UINT numRTVs,
ID3D11DepthStencilView *dsv)
{
for (UINT rtvIndex = 0; rtvIndex < numRTVs; ++rtvIndex)
{
unsetConflictingView(gl::PipelineType::GraphicsPipeline, rtvs[rtvIndex], true);
}
if (dsv)
{
unsetConflictingView(gl::PipelineType::GraphicsPipeline, dsv, true);
}
mRenderer->getDeviceContext()->OMSetRenderTargets(numRTVs, (numRTVs > 0) ? rtvs : nullptr, dsv);
mCurRTVs.clear();
for (UINT i = 0; i < numRTVs; i++)
{
mCurRTVs.update(i, rtvs[i]);
}
mInternalDirtyBits.set(DIRTY_BIT_RENDER_TARGET);
}
void StateManager11::onBeginQuery(Query11 *query)
{
mCurrentQueries.insert(query);
}
void StateManager11::onDeleteQueryObject(Query11 *query)
{
mCurrentQueries.erase(query);
}
angle::Result StateManager11::onMakeCurrent(const gl::Context *context)
{
ANGLE_TRY(ensureInitialized(context));
const gl::State &state = context->getState();
Context11 *context11 = GetImplAs<Context11>(context);
for (Query11 *query : mCurrentQueries)
{
ANGLE_TRY(query->pause(context11));
}
mCurrentQueries.clear();
for (gl::QueryType type : angle::AllEnums<gl::QueryType>())
{
gl::Query *query = state.getActiveQuery(type);
if (query != nullptr)
{
Query11 *query11 = GetImplAs<Query11>(query);
ANGLE_TRY(query11->resume(context11));
mCurrentQueries.insert(query11);
}
}
// Reset the cache objects.
mProgramD3D = nullptr;
mVertexArray11 = nullptr;
mFramebuffer11 = nullptr;
return angle::Result::Continue;
}
void StateManager11::unsetConflictingView(gl::PipelineType pipeline,
ID3D11View *view,
bool isRenderTarget)
{
uintptr_t resource = reinterpret_cast<uintptr_t>(GetViewResource(view));
unsetConflictingSRVs(pipeline, gl::ShaderType::Vertex, resource, nullptr, isRenderTarget);
unsetConflictingSRVs(pipeline, gl::ShaderType::Fragment, resource, nullptr, isRenderTarget);
unsetConflictingSRVs(pipeline, gl::ShaderType::Compute, resource, nullptr, isRenderTarget);
unsetConflictingUAVs(pipeline, gl::ShaderType::Compute, resource, nullptr);
}
void StateManager11::unsetConflictingSRVs(gl::PipelineType pipeline,
gl::ShaderType shaderType,
uintptr_t resource,
const gl::ImageIndex *index,
bool isRenderTarget)
{
auto *currentSRVs = getSRVCache(shaderType);
gl::PipelineType conflictPipeline = gl::GetPipelineType(shaderType);
bool foundOne = false;
size_t count = std::min(currentSRVs->size(), currentSRVs->highestUsed());
for (size_t resourceIndex = 0; resourceIndex < count; ++resourceIndex)
{
auto &record = (*currentSRVs)[resourceIndex];
if (record.view && record.resource == resource &&
(!index || ImageIndexConflictsWithSRV(*index, record.desc)))
{
setShaderResourceInternal<d3d11::ShaderResourceView>(
shaderType, static_cast<UINT>(resourceIndex), nullptr);
foundOne = true;
}
}
if (foundOne && (pipeline != conflictPipeline || isRenderTarget))
{
switch (conflictPipeline)
{
case gl::PipelineType::GraphicsPipeline:
mInternalDirtyBits.set(DIRTY_BIT_GRAPHICS_SRV_STATE);
break;
case gl::PipelineType::ComputePipeline:
mInternalDirtyBits.set(DIRTY_BIT_COMPUTE_SRV_STATE);
break;
default:
UNREACHABLE();
}
}
}
void StateManager11::unsetConflictingUAVs(gl::PipelineType pipeline,
gl::ShaderType shaderType,
uintptr_t resource,
const gl::ImageIndex *index)
{
ASSERT(shaderType == gl::ShaderType::Compute);
bool foundOne = false;
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
size_t count = std::min(mCurComputeUAVs.size(), mCurComputeUAVs.highestUsed());
for (size_t resourceIndex = 0; resourceIndex < count; ++resourceIndex)
{
auto &record = mCurComputeUAVs[resourceIndex];
if (record.view && record.resource == resource &&
(!index || ImageIndexConflictsWithUAV(*index, record.desc)))
{
deviceContext->CSSetUnorderedAccessViews(static_cast<UINT>(resourceIndex), 1,
&mNullUAVs[0], nullptr);
mCurComputeUAVs.update(resourceIndex, nullptr);
foundOne = true;
}
}
if (foundOne && pipeline == gl::PipelineType::GraphicsPipeline)
{
mInternalDirtyBits.set(DIRTY_BIT_COMPUTE_UAV_STATE);
}
}
void StateManager11::unsetConflictingRTVs(uintptr_t resource)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
size_t count = std::min(mCurRTVs.size(), mCurRTVs.highestUsed());
for (size_t resourceIndex = 0; resourceIndex < count; ++resourceIndex)
{
auto &record = mCurRTVs[resourceIndex];
if (record.view && record.resource == resource)
{
deviceContext->OMSetRenderTargets(0, nullptr, nullptr);
mCurRTVs.clear();
mInternalDirtyBits.set(DIRTY_BIT_RENDER_TARGET);
return;
}
}
}
void StateManager11::unsetConflictingAttachmentResources(
const gl::FramebufferAttachment &attachment,
ID3D11Resource *resource)
{
// Unbind render target SRVs from the shader here to prevent D3D11 warnings.
if (attachment.type() == GL_TEXTURE)
{
uintptr_t resourcePtr = reinterpret_cast<uintptr_t>(resource);
const gl::ImageIndex &index = attachment.getTextureImageIndex();
// The index doesn't need to be corrected for the small compressed texture workaround
// because a rendertarget is never compressed.
unsetConflictingSRVs(gl::PipelineType::GraphicsPipeline, gl::ShaderType::Vertex,
resourcePtr, &index, false);
unsetConflictingSRVs(gl::PipelineType::GraphicsPipeline, gl::ShaderType::Fragment,
resourcePtr, &index, false);
unsetConflictingSRVs(gl::PipelineType::GraphicsPipeline, gl::ShaderType::Compute,
resourcePtr, &index, false);
unsetConflictingUAVs(gl::PipelineType::GraphicsPipeline, gl::ShaderType::Compute,
resourcePtr, &index);
}
else if (attachment.type() == GL_FRAMEBUFFER_DEFAULT)
{
uintptr_t resourcePtr = reinterpret_cast<uintptr_t>(resource);
unsetConflictingSRVs(gl::PipelineType::GraphicsPipeline, gl::ShaderType::Vertex,
resourcePtr, nullptr, false);
unsetConflictingSRVs(gl::PipelineType::GraphicsPipeline, gl::ShaderType::Fragment,
resourcePtr, nullptr, false);
unsetConflictingSRVs(gl::PipelineType::GraphicsPipeline, gl::ShaderType::Compute,
resourcePtr, nullptr, false);
unsetConflictingUAVs(gl::PipelineType::GraphicsPipeline, gl::ShaderType::Compute,
resourcePtr, nullptr);
}
}
angle::Result StateManager11::ensureInitialized(const gl::Context *context)
{
Renderer11 *renderer = GetImplAs<Context11>(context)->getRenderer();
const gl::Caps &caps = renderer->getNativeCaps();
const gl::Extensions &extensions = renderer->getNativeExtensions();
for (gl::ShaderType shaderType : gl::AllShaderTypes())
{
const GLuint maxShaderTextureImageUnits =
static_cast<GLuint>(caps.maxShaderTextureImageUnits[shaderType]);
mCurShaderSRVs[shaderType].initialize(maxShaderTextureImageUnits);
mForceSetShaderSamplerStates[shaderType].resize(maxShaderTextureImageUnits, true);
mCurShaderSamplerStates[shaderType].resize(maxShaderTextureImageUnits);
}
mCurRTVs.initialize(caps.maxColorAttachments);
mCurComputeUAVs.initialize(caps.maxImageUnits);
// Initialize cached NULL SRV block
mNullSRVs.resize(caps.maxShaderTextureImageUnits[gl::ShaderType::Fragment], nullptr);
mNullUAVs.resize(caps.maxImageUnits, nullptr);
mCurrentValueAttribs.resize(caps.maxVertexAttributes);
mShaderConstants.init(caps);
mIsMultiviewEnabled = extensions.multiviewOVR || extensions.multiview2OVR;
mIndependentBlendStates = extensions.drawBuffersIndexedAny(); // requires FL10_1
// FL9_3 is limited to 4; ES3.1 context on FL11_0 is limited to 7
mCurBlendStateExt =
gl::BlendStateExt(GetImplAs<Context11>(context)->getNativeCaps().maxDrawBuffers);
ANGLE_TRY(mVertexDataManager.initialize(context));
mCurrentAttributes.reserve(gl::MAX_VERTEX_ATTRIBS);
return angle::Result::Continue;
}
void StateManager11::deinitialize()
{
mCurrentValueAttribs.clear();
mInputLayoutCache.clear();
mVertexDataManager.deinitialize();
mIndexDataManager.deinitialize();
for (d3d11::Buffer &ShaderDriverConstantBuffer : mShaderDriverConstantBuffers)
{
ShaderDriverConstantBuffer.reset();
}
mPointSpriteVertexBuffer.reset();
mPointSpriteIndexBuffer.reset();
}
// Applies the render target surface, depth stencil surface, viewport rectangle and
// scissor rectangle to the renderer
angle::Result StateManager11::syncFramebuffer(const gl::Context *context)
{
// Check for zero-sized default framebuffer, which is a special case.
// in this case we do not wish to modify any state and just silently return false.
// this will not report any gl error but will cause the calling method to return.
if (mFramebuffer11->getState().isDefault())
{
RenderTarget11 *firstRT = mFramebuffer11->getFirstRenderTarget();
const gl::Extents &size = firstRT->getExtents();
if (size.empty())
{
return angle::Result::Continue;
}
}
RTVArray framebufferRTVs = {{}};
const auto &colorRTs = mFramebuffer11->getCachedColorRenderTargets();
size_t appliedRTIndex = 0;
bool skipInactiveRTs = mRenderer->getFeatures().mrtPerfWorkaround.enabled;
const auto &drawStates = mFramebuffer11->getState().getDrawBufferStates();
gl::DrawBufferMask activeProgramOutputs =
mProgramD3D->getState().getExecutable().getActiveOutputVariablesMask();
UINT maxExistingRT = 0;
const auto &colorAttachments = mFramebuffer11->getState().getColorAttachments();
for (size_t rtIndex = 0; rtIndex < colorRTs.size(); ++rtIndex)
{
const RenderTarget11 *renderTarget = colorRTs[rtIndex];
// Skip inactive rendertargets if the workaround is enabled.
if (skipInactiveRTs &&
(!renderTarget || drawStates[rtIndex] == GL_NONE || !activeProgramOutputs[rtIndex]))
{
continue;
}
if (renderTarget)
{
framebufferRTVs[appliedRTIndex] = renderTarget->getRenderTargetView().get();
ASSERT(framebufferRTVs[appliedRTIndex]);
maxExistingRT = static_cast<UINT>(appliedRTIndex) + 1;
// Unset conflicting texture SRVs
const gl::FramebufferAttachment &attachment = colorAttachments[rtIndex];
ASSERT(attachment.isAttached());
unsetConflictingAttachmentResources(attachment, renderTarget->getTexture().get());
}
appliedRTIndex++;
}
// Get the depth stencil buffers
ID3D11DepthStencilView *framebufferDSV = nullptr;
const auto *depthStencilRenderTarget = mFramebuffer11->getCachedDepthStencilRenderTarget();
if (depthStencilRenderTarget)
{
framebufferDSV = depthStencilRenderTarget->getDepthStencilView().get();
ASSERT(framebufferDSV);
// Unset conflicting texture SRVs
const gl::FramebufferAttachment *attachment =
mFramebuffer11->getState().getDepthOrStencilAttachment();
ASSERT(attachment);
unsetConflictingAttachmentResources(*attachment,
depthStencilRenderTarget->getTexture().get());
}
ASSERT(maxExistingRT <= static_cast<UINT>(context->getCaps().maxDrawBuffers));
// Apply the render target and depth stencil
mRenderer->getDeviceContext()->OMSetRenderTargets(maxExistingRT, framebufferRTVs.data(),
framebufferDSV);
mCurRTVs.clear();
for (UINT i = 0; i < maxExistingRT; i++)
{
mCurRTVs.update(i, framebufferRTVs[i]);
}
return angle::Result::Continue;
}
void StateManager11::invalidateCurrentValueAttrib(size_t attribIndex)
{
mDirtyCurrentValueAttribs.set(attribIndex);
mInternalDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_ATTRIBS);
invalidateInputLayout();
invalidateShaders();
}
angle::Result StateManager11::syncCurrentValueAttribs(
const gl::Context *context,
const std::vector<gl::VertexAttribCurrentValueData> ¤tValues)
{
const gl::ProgramExecutable &executable = mProgramD3D->getState().getExecutable();
const auto &activeAttribsMask = executable.getActiveAttribLocationsMask();
const auto &dirtyActiveAttribs = (activeAttribsMask & mDirtyCurrentValueAttribs);
if (!dirtyActiveAttribs.any())
{
return angle::Result::Continue;
}
const auto &vertexAttributes = mVertexArray11->getState().getVertexAttributes();
const auto &vertexBindings = mVertexArray11->getState().getVertexBindings();
mDirtyCurrentValueAttribs = (mDirtyCurrentValueAttribs & ~dirtyActiveAttribs);
for (auto attribIndex : dirtyActiveAttribs)
{
if (vertexAttributes[attribIndex].enabled)
continue;
const auto *attrib = &vertexAttributes[attribIndex];
const auto ¤tValue = currentValues[attribIndex];
TranslatedAttribute *currentValueAttrib = &mCurrentValueAttribs[attribIndex];
currentValueAttrib->currentValueType = currentValue.Type;
currentValueAttrib->attribute = attrib;
currentValueAttrib->binding = &vertexBindings[attrib->bindingIndex];
mDirtyVertexBufferRange.extend(static_cast<unsigned int>(attribIndex));
ANGLE_TRY(mVertexDataManager.storeCurrentValue(context, currentValue, currentValueAttrib,
static_cast<size_t>(attribIndex)));
}
return angle::Result::Continue;
}
void StateManager11::setInputLayout(const d3d11::InputLayout *inputLayout)
{
if (setInputLayoutInternal(inputLayout))
{
invalidateInputLayout();
}
}
bool StateManager11::setInputLayoutInternal(const d3d11::InputLayout *inputLayout)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
if (inputLayout == nullptr)
{
if (!mCurrentInputLayout.empty())
{
deviceContext->IASetInputLayout(nullptr);
mCurrentInputLayout.clear();
return true;
}
}
else if (inputLayout->getSerial() != mCurrentInputLayout)
{
deviceContext->IASetInputLayout(inputLayout->get());
mCurrentInputLayout = inputLayout->getSerial();
return true;
}
return false;
}
bool StateManager11::queueVertexBufferChange(size_t bufferIndex,
ID3D11Buffer *buffer,
UINT stride,
UINT offset)
{
if (buffer != mCurrentVertexBuffers[bufferIndex] ||
stride != mCurrentVertexStrides[bufferIndex] ||
offset != mCurrentVertexOffsets[bufferIndex])
{
mDirtyVertexBufferRange.extend(static_cast<unsigned int>(bufferIndex));
mCurrentVertexBuffers[bufferIndex] = buffer;
mCurrentVertexStrides[bufferIndex] = stride;
mCurrentVertexOffsets[bufferIndex] = offset;
return true;
}
return false;
}
void StateManager11::applyVertexBufferChanges()
{
if (mDirtyVertexBufferRange.empty())
{
return;
}
ASSERT(mDirtyVertexBufferRange.high() <= gl::MAX_VERTEX_ATTRIBS);
UINT start = static_cast<UINT>(mDirtyVertexBufferRange.low());
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
deviceContext->IASetVertexBuffers(start, static_cast<UINT>(mDirtyVertexBufferRange.length()),
&mCurrentVertexBuffers[start], &mCurrentVertexStrides[start],
&mCurrentVertexOffsets[start]);
mDirtyVertexBufferRange = gl::RangeUI(gl::MAX_VERTEX_ATTRIBS, 0);
}
void StateManager11::setSingleVertexBuffer(const d3d11::Buffer *buffer, UINT stride, UINT offset)
{
ID3D11Buffer *native = buffer ? buffer->get() : nullptr;
if (queueVertexBufferChange(0, native, stride, offset))
{
invalidateInputLayout();
applyVertexBufferChanges();
}
}
angle::Result StateManager11::updateState(const gl::Context *context,
gl::PrimitiveMode mode,
GLint firstVertex,
GLsizei vertexOrIndexCount,
gl::DrawElementsType indexTypeOrInvalid,
const void *indices,
GLsizei instanceCount,
GLint baseVertex,
GLuint baseInstance,
bool promoteDynamic)
{
const gl::State &glState = context->getState();
// TODO(jmadill): Use dirty bits.
if (mRenderTargetIsDirty)
{
processFramebufferInvalidation(context);
mRenderTargetIsDirty = false;
}
// TODO(jmadill): Use dirty bits.
if (mProgramD3D->updateSamplerMapping() == ProgramD3D::SamplerMapping::WasDirty)
{
invalidateTexturesAndSamplers();
}
// TODO(jmadill): Use dirty bits.
if (mProgramD3D->anyShaderUniformsDirty())
{
mInternalDirtyBits.set(DIRTY_BIT_PROGRAM_UNIFORMS);
}
// Swizzling can cause internal state changes with blit shaders.
if (mDirtySwizzles)
{
ANGLE_TRY(generateSwizzles(context));
mDirtySwizzles = false;
}
ANGLE_TRY(mFramebuffer11->markAttachmentsDirty(context));
// TODO(jiawei.shao@intel.com): This can be recomputed only on framebuffer or multisample mask
// state changes.
RenderTarget11 *firstRT = mFramebuffer11->getFirstRenderTarget();
const int samples = (firstRT ? firstRT->getSamples() : 0);
// Single-sampled rendering requires ignoring sample coverage and sample mask states.
unsigned int sampleMask = (samples != 0) ? GetBlendSampleMask(glState, samples) : 0xFFFFFFFF;
if (sampleMask != mCurSampleMask)
{
mInternalDirtyBits.set(DIRTY_BIT_BLEND_STATE);
}
ANGLE_TRY(mVertexArray11->syncStateForDraw(context, firstVertex, vertexOrIndexCount,
indexTypeOrInvalid, indices, instanceCount,
baseVertex, baseInstance, promoteDynamic));
// Changes in the draw call can affect the vertex buffer translations.
if (!mLastFirstVertex.valid() || mLastFirstVertex.value() != firstVertex)
{
mLastFirstVertex = firstVertex;
invalidateInputLayout();
}
// The ShaderConstants only need to be updated when the program uses vertexID
if (mProgramD3D->usesVertexID())
{
GLint firstVertexOnChange = firstVertex + baseVertex;
ASSERT(mVertexArray11);
if (mVertexArray11->hasActiveDynamicAttrib(context) &&
indexTypeOrInvalid != gl::DrawElementsType::InvalidEnum)
{
// drawElements with Dynamic attribute
// the firstVertex is already including baseVertex when
// doing ComputeStartVertex
firstVertexOnChange = firstVertex;
}
if (mShaderConstants.onFirstVertexChange(firstVertexOnChange))
{
mInternalDirtyBits.set(DIRTY_BIT_DRIVER_UNIFORMS);
}
}
if (indexTypeOrInvalid != gl::DrawElementsType::InvalidEnum)
{
ANGLE_TRY(applyIndexBuffer(context, vertexOrIndexCount, indexTypeOrInvalid, indices));
}
if (mLastAppliedDrawMode != mode)
{
mLastAppliedDrawMode = mode;
mInternalDirtyBits.set(DIRTY_BIT_PRIMITIVE_TOPOLOGY);
bool pointDrawMode = (mode == gl::PrimitiveMode::Points);
if (pointDrawMode != mCurRasterState.pointDrawMode)
{
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
// Changing from points to not points (or vice-versa) affects the geometry shader.
invalidateShaders();
}
}
auto dirtyBitsCopy = mInternalDirtyBits & mGraphicsDirtyBitsMask;
for (auto iter = dirtyBitsCopy.begin(), end = dirtyBitsCopy.end(); iter != end; ++iter)
{
mInternalDirtyBits.reset(*iter);
switch (*iter)
{
case DIRTY_BIT_RENDER_TARGET:
ANGLE_TRY(syncFramebuffer(context));
break;
case DIRTY_BIT_VIEWPORT_STATE:
syncViewport(context);
break;
case DIRTY_BIT_SCISSOR_STATE:
syncScissorRectangle(context);
break;
case DIRTY_BIT_RASTERIZER_STATE:
ANGLE_TRY(syncRasterizerState(context, mode));
break;
case DIRTY_BIT_BLEND_STATE:
// Single-sampled rendering requires ignoring alpha-to-coverage state.
ANGLE_TRY(syncBlendState(context, glState.getBlendStateExt(),
glState.getBlendColor(), sampleMask,
glState.isSampleAlphaToCoverageEnabled() && (samples != 0),
glState.hasConstantAlphaBlendFunc()));
break;
case DIRTY_BIT_DEPTH_STENCIL_STATE:
ANGLE_TRY(syncDepthStencilState(context));
break;
case DIRTY_BIT_GRAPHICS_SRV_STATE:
ANGLE_TRY(syncTextures(context));
break;
case DIRTY_BIT_GRAPHICS_UAV_STATE:
ANGLE_TRY(syncUAVsForGraphics(context));
break;
case DIRTY_BIT_TEXTURE_AND_SAMPLER_STATE:
// TODO(jmadill): More fine-grained update.
ANGLE_TRY(syncTextures(context));
break;
case DIRTY_BIT_PROGRAM_UNIFORMS:
ANGLE_TRY(applyUniforms(context));
break;
case DIRTY_BIT_DRIVER_UNIFORMS:
// This must happen after viewport sync; the viewport affects builtin uniforms.
ANGLE_TRY(applyDriverUniforms(context));
break;
case DIRTY_BIT_PROGRAM_UNIFORM_BUFFERS:
ANGLE_TRY(syncUniformBuffers(context));
break;
case DIRTY_BIT_SHADERS:
ANGLE_TRY(syncProgram(context, mode));
break;
case DIRTY_BIT_CURRENT_VALUE_ATTRIBS:
ANGLE_TRY(syncCurrentValueAttribs(context, glState.getVertexAttribCurrentValues()));
break;
case DIRTY_BIT_TRANSFORM_FEEDBACK:
ANGLE_TRY(syncTransformFeedbackBuffers(context));
break;
case DIRTY_BIT_VERTEX_BUFFERS_AND_INPUT_LAYOUT:
ANGLE_TRY(syncVertexBuffersAndInputLayout(context, mode, firstVertex,
vertexOrIndexCount, indexTypeOrInvalid,
instanceCount));
break;
case DIRTY_BIT_PRIMITIVE_TOPOLOGY:
syncPrimitiveTopology(glState, mode);
break;
default:
UNREACHABLE();
break;
}
}
// Check that we haven't set any dirty bits in the flushing of the dirty bits loop, except
// DIRTY_BIT_COMPUTE_SRVUAV_STATE dirty bit.
ASSERT((mInternalDirtyBits & mGraphicsDirtyBitsMask).none());
return angle::Result::Continue;
}
void StateManager11::setShaderResourceShared(gl::ShaderType shaderType,
UINT resourceSlot,
const d3d11::SharedSRV *srv)
{
setShaderResourceInternal(shaderType, resourceSlot, srv);
// TODO(jmadill): Narrower dirty region.
mInternalDirtyBits.set(DIRTY_BIT_TEXTURE_AND_SAMPLER_STATE);
}
void StateManager11::setShaderResource(gl::ShaderType shaderType,
UINT resourceSlot,
const d3d11::ShaderResourceView *srv)
{
setShaderResourceInternal(shaderType, resourceSlot, srv);
// TODO(jmadill): Narrower dirty region.
mInternalDirtyBits.set(DIRTY_BIT_TEXTURE_AND_SAMPLER_STATE);
}
void StateManager11::setPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY primitiveTopology)
{
if (setPrimitiveTopologyInternal(primitiveTopology))
{
mInternalDirtyBits.set(DIRTY_BIT_PRIMITIVE_TOPOLOGY);
}
}
bool StateManager11::setPrimitiveTopologyInternal(D3D11_PRIMITIVE_TOPOLOGY primitiveTopology)
{
if (primitiveTopology != mCurrentPrimitiveTopology)
{
mRenderer->getDeviceContext()->IASetPrimitiveTopology(primitiveTopology);
mCurrentPrimitiveTopology = primitiveTopology;
return true;
}
else
{
return false;
}
}
void StateManager11::setDrawShaders(const d3d11::VertexShader *vertexShader,
const d3d11::GeometryShader *geometryShader,
const d3d11::PixelShader *pixelShader)
{
setVertexShader(vertexShader);
setGeometryShader(geometryShader);
setPixelShader(pixelShader);
}
void StateManager11::setVertexShader(const d3d11::VertexShader *shader)
{
ResourceSerial serial = shader ? shader->getSerial() : ResourceSerial(0);
if (serial != mAppliedShaders[gl::ShaderType::Vertex])
{
ID3D11VertexShader *appliedShader = shader ? shader->get() : nullptr;
mRenderer->getDeviceContext()->VSSetShader(appliedShader, nullptr, 0);
mAppliedShaders[gl::ShaderType::Vertex] = serial;
invalidateShaders();
}
}
void StateManager11::setGeometryShader(const d3d11::GeometryShader *shader)
{
ResourceSerial serial = shader ? shader->getSerial() : ResourceSerial(0);
if (serial != mAppliedShaders[gl::ShaderType::Geometry])
{
ID3D11GeometryShader *appliedShader = shader ? shader->get() : nullptr;
mRenderer->getDeviceContext()->GSSetShader(appliedShader, nullptr, 0);
mAppliedShaders[gl::ShaderType::Geometry] = serial;
invalidateShaders();
}
}
void StateManager11::setPixelShader(const d3d11::PixelShader *shader)
{
ResourceSerial serial = shader ? shader->getSerial() : ResourceSerial(0);
if (serial != mAppliedShaders[gl::ShaderType::Fragment])
{
ID3D11PixelShader *appliedShader = shader ? shader->get() : nullptr;
mRenderer->getDeviceContext()->PSSetShader(appliedShader, nullptr, 0);
mAppliedShaders[gl::ShaderType::Fragment] = serial;
invalidateShaders();
}
}
void StateManager11::setComputeShader(const d3d11::ComputeShader *shader)
{
ResourceSerial serial = shader ? shader->getSerial() : ResourceSerial(0);
if (serial != mAppliedShaders[gl::ShaderType::Compute])
{
ID3D11ComputeShader *appliedShader = shader ? shader->get() : nullptr;
mRenderer->getDeviceContext()->CSSetShader(appliedShader, nullptr, 0);
mAppliedShaders[gl::ShaderType::Compute] = serial;
invalidateShaders();
}
}
void StateManager11::setVertexConstantBuffer(unsigned int slot, const d3d11::Buffer *buffer)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
auto ¤tSerial = mCurrentConstantBufferVS[slot];
mCurrentConstantBufferVSOffset[slot] = 0;
mCurrentConstantBufferVSSize[slot] = 0;
if (buffer)
{
if (currentSerial != buffer->getSerial())
{
deviceContext->VSSetConstantBuffers(slot, 1, buffer->getPointer());
currentSerial = buffer->getSerial();
invalidateConstantBuffer(slot);
}
}
else
{
if (!currentSerial.empty())
{
ID3D11Buffer *nullBuffer = nullptr;
deviceContext->VSSetConstantBuffers(slot, 1, &nullBuffer);
currentSerial.clear();
invalidateConstantBuffer(slot);
}
}
}
void StateManager11::setPixelConstantBuffer(unsigned int slot, const d3d11::Buffer *buffer)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
auto ¤tSerial = mCurrentConstantBufferPS[slot];
mCurrentConstantBufferPSOffset[slot] = 0;
mCurrentConstantBufferPSSize[slot] = 0;
if (buffer)
{
if (currentSerial != buffer->getSerial())
{
deviceContext->PSSetConstantBuffers(slot, 1, buffer->getPointer());
currentSerial = buffer->getSerial();
invalidateConstantBuffer(slot);
}
}
else
{
if (!currentSerial.empty())
{
ID3D11Buffer *nullBuffer = nullptr;
deviceContext->PSSetConstantBuffers(slot, 1, &nullBuffer);
currentSerial.clear();
invalidateConstantBuffer(slot);
}
}
}
void StateManager11::setDepthStencilState(const d3d11::DepthStencilState *depthStencilState,
UINT stencilRef)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
if (depthStencilState)
{
deviceContext->OMSetDepthStencilState(depthStencilState->get(), stencilRef);
}
else
{
deviceContext->OMSetDepthStencilState(nullptr, stencilRef);
}
mInternalDirtyBits.set(DIRTY_BIT_DEPTH_STENCIL_STATE);
}
void StateManager11::setSimpleBlendState(const d3d11::BlendState *blendState)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
if (blendState)
{
deviceContext->OMSetBlendState(blendState->get(), nullptr, 0xFFFFFFFF);
}
else
{
deviceContext->OMSetBlendState(nullptr, nullptr, 0xFFFFFFFF);
}
mInternalDirtyBits.set(DIRTY_BIT_BLEND_STATE);
}
void StateManager11::setRasterizerState(const d3d11::RasterizerState *rasterizerState)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
if (rasterizerState)
{
deviceContext->RSSetState(rasterizerState->get());
}
else
{
deviceContext->RSSetState(nullptr);
}
mInternalDirtyBits.set(DIRTY_BIT_RASTERIZER_STATE);
}
void StateManager11::setSimpleViewport(const gl::Extents &extents)
{
setSimpleViewport(extents.width, extents.height);
}
void StateManager11::setSimpleViewport(int width, int height)
{
D3D11_VIEWPORT viewport;
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = static_cast<FLOAT>(width);
viewport.Height = static_cast<FLOAT>(height);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
mRenderer->getDeviceContext()->RSSetViewports(1, &viewport);
mInternalDirtyBits.set(DIRTY_BIT_VIEWPORT_STATE);
}
void StateManager11::setSimplePixelTextureAndSampler(const d3d11::SharedSRV &srv,
const d3d11::SamplerState &samplerState)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
setShaderResourceInternal(gl::ShaderType::Fragment, 0, &srv);
deviceContext->PSSetSamplers(0, 1, samplerState.getPointer());
mInternalDirtyBits.set(DIRTY_BIT_TEXTURE_AND_SAMPLER_STATE);
mForceSetShaderSamplerStates[gl::ShaderType::Fragment][0] = true;
}
void StateManager11::setSimpleScissorRect(const gl::Rectangle &glRect)
{
D3D11_RECT scissorRect;
scissorRect.left = glRect.x;
scissorRect.right = glRect.x + glRect.width;
scissorRect.top = glRect.y;
scissorRect.bottom = glRect.y + glRect.height;
setScissorRectD3D(scissorRect);
}
void StateManager11::setScissorRectD3D(const D3D11_RECT &d3dRect)
{
mRenderer->getDeviceContext()->RSSetScissorRects(1, &d3dRect);
mInternalDirtyBits.set(DIRTY_BIT_SCISSOR_STATE);
}
angle::Result StateManager11::syncTextures(const gl::Context *context)
{
ANGLE_TRY(applyTexturesForSRVs(context, gl::ShaderType::Vertex));
ANGLE_TRY(applyTexturesForSRVs(context, gl::ShaderType::Fragment));
if (mProgramD3D->hasShaderStage(gl::ShaderType::Geometry))
{
ANGLE_TRY(applyTexturesForSRVs(context, gl::ShaderType::Geometry));
}
return angle::Result::Continue;
}
angle::Result StateManager11::setSamplerState(const gl::Context *context,
gl::ShaderType type,
int index,
gl::Texture *texture,
const gl::SamplerState &samplerState)
{
#if !defined(NDEBUG)
// Storage should exist, texture should be complete. Only verified in Debug.
TextureD3D *textureD3D = GetImplAs<TextureD3D>(texture);
TextureStorage *storage = nullptr;
ANGLE_TRY(textureD3D->getNativeTexture(context, &storage));
ASSERT(storage);
#endif // !defined(NDEBUG)
auto *deviceContext = mRenderer->getDeviceContext();
ASSERT(index < mRenderer->getNativeCaps().maxShaderTextureImageUnits[type]);
// When border color is used, its value may need to be readjusted based on the texture format.
const bool usesBorderColor = samplerState.usesBorderColor();
if (mForceSetShaderSamplerStates[type][index] || usesBorderColor ||
memcmp(&samplerState, &mCurShaderSamplerStates[type][index], sizeof(gl::SamplerState)) != 0)
{
// When clamp-to-border mode is used and a floating-point border color is set, the color
// value must be adjusted based on the texture format. Reset it to zero in all other cases
// to reduce the number of cached sampler entries. Address modes for integer texture
// formats are emulated in shaders and do not rely on this state.
angle::ColorGeneric borderColor;
if (usesBorderColor)
{
if (samplerState.getBorderColor().type == angle::ColorGeneric::Type::Float)
{
borderColor = samplerState.getBorderColor();
}
const uint32_t baseLevel = texture->getTextureState().getEffectiveBaseLevel();
const gl::TextureTarget target = TextureTypeToTarget(texture->getType(), 0);
const angle::Format &format = angle::Format::Get(angle::Format::InternalFormatToID(
texture->getFormat(target, baseLevel).info->sizedInternalFormat));
borderColor = AdjustBorderColor<false>(borderColor, format, false);
}
gl::SamplerState adjustedSamplerState(samplerState);
adjustedSamplerState.setBorderColor(borderColor.colorF);
ID3D11SamplerState *dxSamplerState = nullptr;
ANGLE_TRY(mRenderer->getSamplerState(context, adjustedSamplerState, &dxSamplerState));
ASSERT(dxSamplerState != nullptr);
switch (type)
{
case gl::ShaderType::Vertex:
deviceContext->VSSetSamplers(index, 1, &dxSamplerState);
break;
case gl::ShaderType::Fragment:
deviceContext->PSSetSamplers(index, 1, &dxSamplerState);
break;
case gl::ShaderType::Compute:
deviceContext->CSSetSamplers(index, 1, &dxSamplerState);
break;
case gl::ShaderType::Geometry:
deviceContext->GSSetSamplers(index, 1, &dxSamplerState);
break;
default:
UNREACHABLE();
break;
}
mCurShaderSamplerStates[type][index] = samplerState;
}
mForceSetShaderSamplerStates[type][index] = false;
// Sampler metadata that's passed to shaders in uniforms is stored separately from rest of the
// sampler state since having it in contiguous memory makes it possible to memcpy to a constant
// buffer, and it doesn't affect the state set by
// PSSetSamplers/VSSetSamplers/CSSetSamplers/GSSetSamplers.
mShaderConstants.onSamplerChange(type, index, *texture, samplerState);
return angle::Result::Continue;
}
angle::Result StateManager11::setTextureForSampler(const gl::Context *context,
gl::ShaderType type,
int index,
gl::Texture *texture,
const gl::SamplerState &sampler)
{
const d3d11::SharedSRV *textureSRV = nullptr;
if (texture)
{
TextureD3D *textureImpl = GetImplAs<TextureD3D>(texture);
TextureStorage *texStorage = nullptr;
ANGLE_TRY(textureImpl->getNativeTexture(context, &texStorage));
// Texture should be complete and have a storage
ASSERT(texStorage);
TextureStorage11 *storage11 = GetAs<TextureStorage11>(texStorage);
ANGLE_TRY(
storage11->getSRVForSampler(context, texture->getTextureState(), sampler, &textureSRV));
// If we get an invalid SRV here, something went wrong in the texture class and we're
// unexpectedly missing the shader resource view.
ASSERT(textureSRV->valid());
textureImpl->resetDirty();
}
ASSERT(
(type == gl::ShaderType::Fragment &&
index < mRenderer->getNativeCaps().maxShaderTextureImageUnits[gl::ShaderType::Fragment]) ||
(type == gl::ShaderType::Vertex &&
index < mRenderer->getNativeCaps().maxShaderTextureImageUnits[gl::ShaderType::Vertex]) ||
(type == gl::ShaderType::Compute &&
index < mRenderer->getNativeCaps().maxShaderTextureImageUnits[gl::ShaderType::Compute]));
setShaderResourceInternal(type, index, textureSRV);
return angle::Result::Continue;
}
angle::Result StateManager11::setImageState(const gl::Context *context,
gl::ShaderType type,
int index,
const gl::ImageUnit &imageUnit)
{
ASSERT(index < mRenderer->getNativeCaps().maxShaderImageUniforms[type]);
if (mShaderConstants.onImageChange(type, index, imageUnit))
{
invalidateProgramUniforms();
}
return angle::Result::Continue;
}
// For each Direct3D sampler of either the pixel or vertex stage,
// looks up the corresponding OpenGL texture image unit and texture type,
// and sets the texture and its addressing/filtering state (or NULL when inactive).
// Sampler mapping needs to be up-to-date on the program object before this is called.
angle::Result StateManager11::applyTexturesForSRVs(const gl::Context *context,
gl::ShaderType shaderType)
{
const auto &glState = context->getState();
const auto &caps = context->getCaps();
ASSERT(!mProgramD3D->isSamplerMappingDirty());
// TODO(jmadill): Use the Program's sampler bindings.
const gl::ActiveTexturesCache &completeTextures = glState.getActiveTexturesCache();
const gl::RangeUI samplerRange = mProgramD3D->getUsedSamplerRange(shaderType);
for (unsigned int samplerIndex = samplerRange.low(); samplerIndex < samplerRange.high();
samplerIndex++)
{
GLint textureUnit = mProgramD3D->getSamplerMapping(shaderType, samplerIndex, caps);
ASSERT(textureUnit != -1);
gl::Texture *texture = completeTextures[textureUnit];
// A nullptr texture indicates incomplete.
if (texture)
{
gl::Sampler *samplerObject = glState.getSampler(textureUnit);
const gl::SamplerState &samplerState =
samplerObject ? samplerObject->getSamplerState() : texture->getSamplerState();
ANGLE_TRY(setSamplerState(context, shaderType, samplerIndex, texture, samplerState));
ANGLE_TRY(
setTextureForSampler(context, shaderType, samplerIndex, texture, samplerState));
}
else
{
gl::TextureType textureType =
mProgramD3D->getSamplerTextureType(shaderType, samplerIndex);
// Texture is not sampler complete or it is in use by the framebuffer. Bind the
// incomplete texture.
gl::Texture *incompleteTexture = nullptr;
ANGLE_TRY(mRenderer->getIncompleteTexture(context, textureType, &incompleteTexture));
ANGLE_TRY(setSamplerState(context, shaderType, samplerIndex, incompleteTexture,
incompleteTexture->getSamplerState()));
ANGLE_TRY(setTextureForSampler(context, shaderType, samplerIndex, incompleteTexture,
incompleteTexture->getSamplerState()));
}
}
const gl::RangeUI readonlyImageRange = mProgramD3D->getUsedImageRange(shaderType, true);
for (unsigned int readonlyImageIndex = readonlyImageRange.low();
readonlyImageIndex < readonlyImageRange.high(); readonlyImageIndex++)
{
GLint imageUnitIndex =
mProgramD3D->getImageMapping(shaderType, readonlyImageIndex, true, caps);
ASSERT(imageUnitIndex != -1);
const gl::ImageUnit &imageUnit = glState.getImageUnit(imageUnitIndex);
if (!imageUnit.layered)
{
ANGLE_TRY(setImageState(context, gl::ShaderType::Compute,
readonlyImageIndex - readonlyImageRange.low(), imageUnit));
}
ANGLE_TRY(setTextureForImage(context, shaderType, readonlyImageIndex, imageUnit));
}
return angle::Result::Continue;
}
angle::Result StateManager11::getUAVsForRWImages(const gl::Context *context,
gl::ShaderType shaderType,
UAVList *uavList)
{
const auto &glState = context->getState();
const auto &caps = context->getCaps();
const gl::RangeUI imageRange = mProgramD3D->getUsedImageRange(shaderType, false);
for (unsigned int imageIndex = imageRange.low(); imageIndex < imageRange.high(); imageIndex++)
{
GLint imageUnitIndex = mProgramD3D->getImageMapping(shaderType, imageIndex, false, caps);
ASSERT(imageUnitIndex != -1);
const gl::ImageUnit &imageUnit = glState.getImageUnit(imageUnitIndex);
if (!imageUnit.layered)
{
ANGLE_TRY(setImageState(context, shaderType, imageIndex - imageRange.low(), imageUnit));
}
ANGLE_TRY(getUAVForRWImage(context, shaderType, imageIndex, imageUnit, uavList));
}
return angle::Result::Continue;
}
angle::Result StateManager11::syncTexturesForCompute(const gl::Context *context)
{
ANGLE_TRY(applyTexturesForSRVs(context, gl::ShaderType::Compute));
return angle::Result::Continue;
}
angle::Result StateManager11::setTextureForImage(const gl::Context *context,
gl::ShaderType type,
int index,
const gl::ImageUnit &imageUnit)
{
TextureD3D *textureImpl = nullptr;
if (!imageUnit.texture.get())
{
setShaderResourceInternal<d3d11::ShaderResourceView>(type, static_cast<UINT>(index),
nullptr);
return angle::Result::Continue;
}
textureImpl = GetImplAs<TextureD3D>(imageUnit.texture.get());
// Ensure that texture has unordered access; convert it if not.
ANGLE_TRY(textureImpl->ensureUnorderedAccess(context));
TextureStorage *texStorage = nullptr;
ANGLE_TRY(textureImpl->getNativeTexture(context, &texStorage));
// Texture should be complete and have a storage
ASSERT(texStorage);
TextureStorage11 *storage11 = GetAs<TextureStorage11>(texStorage);
const d3d11::SharedSRV *textureSRV = nullptr;
ANGLE_TRY(storage11->getSRVForImage(context, imageUnit, &textureSRV));
// If we get an invalid SRV here, something went wrong in the texture class and we're
// unexpectedly missing the shader resource view.
ASSERT(textureSRV->valid());
ASSERT((index < mRenderer->getNativeCaps().maxImageUnits));
setShaderResourceInternal(type, index, textureSRV);
textureImpl->resetDirty();
return angle::Result::Continue;
}
angle::Result StateManager11::getUAVForRWImage(const gl::Context *context,
gl::ShaderType type,
int index,
const gl::ImageUnit &imageUnit,
UAVList *uavList)
{
TextureD3D *textureImpl = nullptr;
if (!imageUnit.texture.get())
{
setUnorderedAccessViewInternal<d3d11::UnorderedAccessView>(static_cast<UINT>(index),
nullptr, uavList);
return angle::Result::Continue;
}
textureImpl = GetImplAs<TextureD3D>(imageUnit.texture.get());
// Ensure that texture has unordered access; convert it if not.
ANGLE_TRY(textureImpl->ensureUnorderedAccess(context));
TextureStorage *texStorage = nullptr;
ANGLE_TRY(textureImpl->getNativeTexture(context, &texStorage));
// Texture should be complete and have a storage
ASSERT(texStorage);
TextureStorage11 *storage11 = GetAs<TextureStorage11>(texStorage);
const d3d11::SharedUAV *textureUAV = nullptr;
ANGLE_TRY(storage11->getUAVForImage(context, imageUnit, &textureUAV));
// If we get an invalid UAV here, something went wrong in the texture class and we're
// unexpectedly missing the unordered access view.
ASSERT(textureUAV->valid());
ASSERT((index < mRenderer->getNativeCaps().maxImageUnits));
setUnorderedAccessViewInternal(index, textureUAV, uavList);
textureImpl->resetDirty();
return angle::Result::Continue;
}
// Things that affect a program's dirtyness:
// 1. Directly changing the program executable -> triggered in StateManager11::syncState.
// 2. The vertex attribute layout -> triggered in VertexArray11::syncState/signal.
// 3. The fragment shader's rendertargets -> triggered in Framebuffer11::syncState/signal.
// 4. Enabling/disabling rasterizer discard. -> triggered in StateManager11::syncState.
// 5. Enabling/disabling transform feedback. -> checked in StateManager11::updateState.
// 6. An internal shader was used. -> triggered in StateManager11::set*Shader.
// 7. Drawing with/without point sprites. -> checked in StateManager11::updateState.
// TODO(jmadill): Use dirty bits for transform feedback.
angle::Result StateManager11::syncProgram(const gl::Context *context, gl::PrimitiveMode drawMode)
{
Context11 *context11 = GetImplAs<Context11>(context);
ANGLE_TRY(context11->triggerDrawCallProgramRecompilation(context, drawMode));
const auto &glState = context->getState();
mProgramD3D->updateCachedInputLayout(mVertexArray11->getCurrentStateSerial(), glState);
// Binaries must be compiled before the sync.
ASSERT(mProgramD3D->hasVertexExecutableForCachedInputLayout());
ASSERT(mProgramD3D->hasGeometryExecutableForPrimitiveType(glState, drawMode));
ASSERT(mProgramD3D->hasPixelExecutableForCachedOutputLayout());
ShaderExecutableD3D *vertexExe = nullptr;
ANGLE_TRY(mProgramD3D->getVertexExecutableForCachedInputLayout(context11, &vertexExe, nullptr));
ShaderExecutableD3D *pixelExe = nullptr;
ANGLE_TRY(mProgramD3D->getPixelExecutableForCachedOutputLayout(context11, &pixelExe, nullptr));
ShaderExecutableD3D *geometryExe = nullptr;
ANGLE_TRY(mProgramD3D->getGeometryExecutableForPrimitiveType(context11, glState, drawMode,
&geometryExe, nullptr));
const d3d11::VertexShader *vertexShader =
(vertexExe ? &GetAs<ShaderExecutable11>(vertexExe)->getVertexShader() : nullptr);
// Skip pixel shader if we're doing rasterizer discard.
const d3d11::PixelShader *pixelShader = nullptr;
if (!glState.getRasterizerState().rasterizerDiscard)
{
pixelShader = (pixelExe ? &GetAs<ShaderExecutable11>(pixelExe)->getPixelShader() : nullptr);
}
const d3d11::GeometryShader *geometryShader = nullptr;
if (glState.isTransformFeedbackActiveUnpaused())
{
geometryShader =
(vertexExe ? &GetAs<ShaderExecutable11>(vertexExe)->getStreamOutShader() : nullptr);
}
else
{
geometryShader =
(geometryExe ? &GetAs<ShaderExecutable11>(geometryExe)->getGeometryShader() : nullptr);
}
setDrawShaders(vertexShader, geometryShader, pixelShader);
// Explicitly clear the shaders dirty bit.
mInternalDirtyBits.reset(DIRTY_BIT_SHADERS);
return angle::Result::Continue;
}
angle::Result StateManager11::syncProgramForCompute(const gl::Context *context)
{
Context11 *context11 = GetImplAs<Context11>(context);
ANGLE_TRY(context11->triggerDispatchCallProgramRecompilation(context));
mProgramD3D->updateCachedComputeImage2DBindLayout(context);
// Binaries must be compiled before the sync.
ASSERT(mProgramD3D->hasComputeExecutableForCachedImage2DBindLayout());
ShaderExecutableD3D *computeExe = nullptr;
ANGLE_TRY(mProgramD3D->getComputeExecutableForImage2DBindLayout(context, context11, &computeExe,
nullptr));
const d3d11::ComputeShader *computeShader =
(computeExe ? &GetAs<ShaderExecutable11>(computeExe)->getComputeShader() : nullptr);
setComputeShader(computeShader);
// Explicitly clear the shaders dirty bit.
mInternalDirtyBits.reset(DIRTY_BIT_SHADERS);
return angle::Result::Continue;
}
angle::Result StateManager11::syncVertexBuffersAndInputLayout(
const gl::Context *context,
gl::PrimitiveMode mode,
GLint firstVertex,
GLsizei vertexOrIndexCount,
gl::DrawElementsType indexTypeOrInvalid,
GLsizei instanceCount)
{
const auto &vertexArrayAttribs = mVertexArray11->getTranslatedAttribs();
// Sort the attributes according to ensure we re-use similar input layouts.
AttribIndexArray sortedSemanticIndices;
SortAttributesByLayout(*mProgramD3D, vertexArrayAttribs, mCurrentValueAttribs,
&sortedSemanticIndices, &mCurrentAttributes);
D3D_FEATURE_LEVEL featureLevel = mRenderer->getRenderer11DeviceCaps().featureLevel;
// If we are using FL 9_3, make sure the first attribute is not instanced
if (featureLevel <= D3D_FEATURE_LEVEL_9_3 && !mCurrentAttributes.empty())
{
if (mCurrentAttributes[0]->divisor > 0)
{
Optional<size_t> firstNonInstancedIndex = FindFirstNonInstanced(mCurrentAttributes);
if (firstNonInstancedIndex.valid())
{
size_t index = firstNonInstancedIndex.value();
std::swap(mCurrentAttributes[0], mCurrentAttributes[index]);
std::swap(sortedSemanticIndices[0], sortedSemanticIndices[index]);
}
}
}
// Update the applied input layout by querying the cache.
const gl::State &state = context->getState();
const d3d11::InputLayout *inputLayout = nullptr;
ANGLE_TRY(mInputLayoutCache.getInputLayout(GetImplAs<Context11>(context), state,
mCurrentAttributes, sortedSemanticIndices, mode,
vertexOrIndexCount, instanceCount, &inputLayout));
setInputLayoutInternal(inputLayout);
// Update the applied vertex buffers.
ANGLE_TRY(applyVertexBuffers(context, mode, indexTypeOrInvalid, firstVertex));
return angle::Result::Continue;
}
angle::Result StateManager11::applyVertexBuffers(const gl::Context *context,
gl::PrimitiveMode mode,
gl::DrawElementsType indexTypeOrInvalid,
GLint firstVertex)
{
bool programUsesInstancedPointSprites =
mProgramD3D->usesPointSize() && mProgramD3D->usesInstancedPointSpriteEmulation();
bool instancedPointSpritesActive =
programUsesInstancedPointSprites && (mode == gl::PrimitiveMode::Points);
// Note that if we use instance emulation, we reserve the first buffer slot.
size_t reservedBuffers = GetReservedBufferCount(programUsesInstancedPointSprites);
for (size_t attribIndex = 0; attribIndex < (gl::MAX_VERTEX_ATTRIBS - reservedBuffers);
++attribIndex)
{
ID3D11Buffer *buffer = nullptr;
UINT vertexStride = 0;
UINT vertexOffset = 0;
if (attribIndex < mCurrentAttributes.size())
{
const TranslatedAttribute &attrib = *mCurrentAttributes[attribIndex];
Buffer11 *bufferStorage = attrib.storage ? GetAs<Buffer11>(attrib.storage) : nullptr;
// If indexed pointsprite emulation is active, then we need to take a less efficent code
// path. Emulated indexed pointsprite rendering requires that the vertex buffers match
// exactly to the indices passed by the caller. This could expand or shrink the vertex
// buffer depending on the number of points indicated by the index list or how many
// duplicates are found on the index list.
if (bufferStorage == nullptr)
{
ASSERT(attrib.vertexBuffer.get());
buffer = GetAs<VertexBuffer11>(attrib.vertexBuffer.get())->getBuffer().get();
}
else if (instancedPointSpritesActive &&
indexTypeOrInvalid != gl::DrawElementsType::InvalidEnum)
{
ASSERT(mVertexArray11->isCachedIndexInfoValid());
TranslatedIndexData indexInfo = mVertexArray11->getCachedIndexInfo();
if (indexInfo.srcIndexData.srcBuffer != nullptr)
{
const uint8_t *bufferData = nullptr;
ANGLE_TRY(indexInfo.srcIndexData.srcBuffer->getData(context, &bufferData));
ASSERT(bufferData != nullptr);
ptrdiff_t offset =
reinterpret_cast<ptrdiff_t>(indexInfo.srcIndexData.srcIndices);
indexInfo.srcIndexData.srcBuffer = nullptr;
indexInfo.srcIndexData.srcIndices = bufferData + offset;
}
ANGLE_TRY(bufferStorage->getEmulatedIndexedBuffer(context, &indexInfo.srcIndexData,
attrib, firstVertex, &buffer));
mVertexArray11->updateCachedIndexInfo(indexInfo);
}
else
{
ANGLE_TRY(bufferStorage->getBuffer(
context, BUFFER_USAGE_VERTEX_OR_TRANSFORM_FEEDBACK, &buffer));
}
vertexStride = attrib.stride;
ANGLE_TRY(attrib.computeOffset(context, firstVertex, &vertexOffset));
}
size_t bufferIndex = reservedBuffers + attribIndex;
queueVertexBufferChange(bufferIndex, buffer, vertexStride, vertexOffset);
}
Context11 *context11 = GetImplAs<Context11>(context);
// Instanced PointSprite emulation requires two additional ID3D11Buffers. A vertex buffer needs
// to be created and added to the list of current buffers, strides and offsets collections.
// This buffer contains the vertices for a single PointSprite quad.
// An index buffer also needs to be created and applied because rendering instanced data on
// D3D11 FL9_3 requires DrawIndexedInstanced() to be used. Shaders that contain gl_PointSize and
// used without the GL_POINTS rendering mode require a vertex buffer because some drivers cannot
// handle missing vertex data and will TDR the system.
if (programUsesInstancedPointSprites)
{
constexpr UINT kPointSpriteVertexStride = sizeof(float) * 5;
if (!mPointSpriteVertexBuffer.valid())
{
static constexpr float kPointSpriteVertices[] = {
// Position | TexCoord
-1.0f, -1.0f, 0.0f, 0.0f, 1.0f, /* v0 */
-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, /* v1 */
1.0f, 1.0f, 0.0f, 1.0f, 0.0f, /* v2 */
1.0f, -1.0f, 0.0f, 1.0f, 1.0f, /* v3 */
-1.0f, -1.0f, 0.0f, 0.0f, 1.0f, /* v4 */
1.0f, 1.0f, 0.0f, 1.0f, 0.0f, /* v5 */
};
D3D11_SUBRESOURCE_DATA vertexBufferData = {kPointSpriteVertices, 0, 0};
D3D11_BUFFER_DESC vertexBufferDesc;
vertexBufferDesc.ByteWidth = sizeof(kPointSpriteVertices);
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
ANGLE_TRY(mRenderer->allocateResource(context11, vertexBufferDesc, &vertexBufferData,
&mPointSpriteVertexBuffer));
}
// Set the stride to 0 if GL_POINTS mode is not being used to instruct the driver to avoid
// indexing into the vertex buffer.
UINT stride = instancedPointSpritesActive ? kPointSpriteVertexStride : 0;
queueVertexBufferChange(0, mPointSpriteVertexBuffer.get(), stride, 0);
if (!mPointSpriteIndexBuffer.valid())
{
// Create an index buffer and set it for pointsprite rendering
static constexpr unsigned short kPointSpriteIndices[] = {
0, 1, 2, 3, 4, 5,
};
D3D11_SUBRESOURCE_DATA indexBufferData = {kPointSpriteIndices, 0, 0};
D3D11_BUFFER_DESC indexBufferDesc;
indexBufferDesc.ByteWidth = sizeof(kPointSpriteIndices);
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
indexBufferDesc.StructureByteStride = 0;
ANGLE_TRY(mRenderer->allocateResource(context11, indexBufferDesc, &indexBufferData,
&mPointSpriteIndexBuffer));
}
if (instancedPointSpritesActive)
{
// The index buffer is applied here because Instanced PointSprite emulation uses the a
// non-indexed rendering path in ANGLE (DrawArrays). This means that applyIndexBuffer()
// on the renderer will not be called and setting this buffer here ensures that the
// rendering path will contain the correct index buffers.
syncIndexBuffer(mPointSpriteIndexBuffer.get(), DXGI_FORMAT_R16_UINT, 0);
}
}
applyVertexBufferChanges();
return angle::Result::Continue;
}
angle::Result StateManager11::applyIndexBuffer(const gl::Context *context,
GLsizei indexCount,
gl::DrawElementsType indexType,
const void *indices)
{
if (!mIndexBufferIsDirty)
{
// No streaming or index buffer application necessary.
return angle::Result::Continue;
}
gl::DrawElementsType destElementType = mVertexArray11->getCachedDestinationIndexType();
gl::Buffer *elementArrayBuffer = mVertexArray11->getState().getElementArrayBuffer();
TranslatedIndexData indexInfo;
ANGLE_TRY(mIndexDataManager.prepareIndexData(context, indexType, destElementType, indexCount,
elementArrayBuffer, indices, &indexInfo));
ID3D11Buffer *buffer = nullptr;
DXGI_FORMAT bufferFormat = (indexInfo.indexType == gl::DrawElementsType::UnsignedInt)
? DXGI_FORMAT_R32_UINT
: DXGI_FORMAT_R16_UINT;
if (indexInfo.storage)
{
Buffer11 *storage = GetAs<Buffer11>(indexInfo.storage);
ANGLE_TRY(storage->getBuffer(context, BUFFER_USAGE_INDEX, &buffer));
}
else
{
IndexBuffer11 *indexBuffer = GetAs<IndexBuffer11>(indexInfo.indexBuffer);
buffer = indexBuffer->getBuffer().get();
}
// Track dirty indices in the index range cache.
indexInfo.srcIndexData.srcIndicesChanged =
syncIndexBuffer(buffer, bufferFormat, indexInfo.startOffset);
mIndexBufferIsDirty = false;
mVertexArray11->updateCachedIndexInfo(indexInfo);
return angle::Result::Continue;
}
void StateManager11::setIndexBuffer(ID3D11Buffer *buffer,
DXGI_FORMAT indexFormat,
unsigned int offset)
{
if (syncIndexBuffer(buffer, indexFormat, offset))
{
invalidateIndexBuffer();
}
}
bool StateManager11::syncIndexBuffer(ID3D11Buffer *buffer,
DXGI_FORMAT indexFormat,
unsigned int offset)
{
if (buffer != mAppliedIB || indexFormat != mAppliedIBFormat || offset != mAppliedIBOffset)
{
mRenderer->getDeviceContext()->IASetIndexBuffer(buffer, indexFormat, offset);
mAppliedIB = buffer;
mAppliedIBFormat = indexFormat;
mAppliedIBOffset = offset;
return true;
}
return false;
}
// Vertex buffer is invalidated outside this function.
angle::Result StateManager11::updateVertexOffsetsForPointSpritesEmulation(
const gl::Context *context,
GLint startVertex,
GLsizei emulatedInstanceId)
{
size_t reservedBuffers = GetReservedBufferCount(true);
for (size_t attribIndex = 0; attribIndex < mCurrentAttributes.size(); ++attribIndex)
{
const auto &attrib = *mCurrentAttributes[attribIndex];
size_t bufferIndex = reservedBuffers + attribIndex;
if (attrib.divisor > 0)
{
unsigned int offset = 0;
ANGLE_TRY(attrib.computeOffset(context, startVertex, &offset));
offset += (attrib.stride * (emulatedInstanceId / attrib.divisor));
if (offset != mCurrentVertexOffsets[bufferIndex])
{
invalidateInputLayout();
mDirtyVertexBufferRange.extend(static_cast<unsigned int>(bufferIndex));
mCurrentVertexOffsets[bufferIndex] = offset;
}
}
}
applyVertexBufferChanges();
return angle::Result::Continue;
}
angle::Result StateManager11::generateSwizzle(const gl::Context *context, gl::Texture *texture)
{
if (!texture)
{
return angle::Result::Continue;
}
TextureD3D *textureD3D = GetImplAs<TextureD3D>(texture);
ASSERT(textureD3D);
TextureStorage *texStorage = nullptr;
ANGLE_TRY(textureD3D->getNativeTexture(context, &texStorage));
if (texStorage)
{
TextureStorage11 *storage11 = GetAs<TextureStorage11>(texStorage);
const gl::TextureState &textureState = texture->getTextureState();
ANGLE_TRY(storage11->generateSwizzles(context, textureState));
}
return angle::Result::Continue;
}
angle::Result StateManager11::generateSwizzlesForShader(const gl::Context *context,
gl::ShaderType type)
{
const gl::State &glState = context->getState();
const gl::RangeUI samplerRange = mProgramD3D->getUsedSamplerRange(type);
for (unsigned int i = samplerRange.low(); i < samplerRange.high(); i++)
{
gl::TextureType textureType = mProgramD3D->getSamplerTextureType(type, i);
GLint textureUnit = mProgramD3D->getSamplerMapping(type, i, context->getCaps());
if (textureUnit != -1)
{
gl::Texture *texture = glState.getSamplerTexture(textureUnit, textureType);
ASSERT(texture);
if (SwizzleRequired(texture->getTextureState()))
{
ANGLE_TRY(generateSwizzle(context, texture));
}
}
}
return angle::Result::Continue;
}
angle::Result StateManager11::generateSwizzles(const gl::Context *context)
{
ANGLE_TRY(generateSwizzlesForShader(context, gl::ShaderType::Vertex));
ANGLE_TRY(generateSwizzlesForShader(context, gl::ShaderType::Fragment));
return angle::Result::Continue;
}
angle::Result StateManager11::applyUniformsForShader(const gl::Context *context,
gl::ShaderType shaderType)
{
UniformStorage11 *shaderUniformStorage =
GetAs<UniformStorage11>(mProgramD3D->getShaderUniformStorage(shaderType));
ASSERT(shaderUniformStorage);
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
const d3d11::Buffer *shaderConstantBuffer = nullptr;
ANGLE_TRY(shaderUniformStorage->getConstantBuffer(context, mRenderer, &shaderConstantBuffer));
if (shaderUniformStorage->size() > 0 && mProgramD3D->areShaderUniformsDirty(shaderType))
{
UpdateUniformBuffer(deviceContext, shaderUniformStorage, shaderConstantBuffer);
}
unsigned int slot = d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DEFAULT_UNIFORM_BLOCK;
switch (shaderType)
{
case gl::ShaderType::Vertex:
if (mCurrentConstantBufferVS[slot] != shaderConstantBuffer->getSerial())
{
deviceContext->VSSetConstantBuffers(slot, 1, shaderConstantBuffer->getPointer());
mCurrentConstantBufferVS[slot] = shaderConstantBuffer->getSerial();
mCurrentConstantBufferVSOffset[slot] = 0;
mCurrentConstantBufferVSSize[slot] = 0;
}
break;
case gl::ShaderType::Fragment:
if (mCurrentConstantBufferPS[slot] != shaderConstantBuffer->getSerial())
{
deviceContext->PSSetConstantBuffers(slot, 1, shaderConstantBuffer->getPointer());
mCurrentConstantBufferPS[slot] = shaderConstantBuffer->getSerial();
mCurrentConstantBufferPSOffset[slot] = 0;
mCurrentConstantBufferPSSize[slot] = 0;
}
break;
// TODO(jiawei.shao@intel.com): apply geometry shader uniforms
case gl::ShaderType::Geometry:
UNIMPLEMENTED();
break;
default:
UNREACHABLE();
break;
}
return angle::Result::Continue;
}
angle::Result StateManager11::applyUniforms(const gl::Context *context)
{
ANGLE_TRY(applyUniformsForShader(context, gl::ShaderType::Vertex));
ANGLE_TRY(applyUniformsForShader(context, gl::ShaderType::Fragment));
if (mProgramD3D->hasShaderStage(gl::ShaderType::Geometry))
{
ANGLE_TRY(applyUniformsForShader(context, gl::ShaderType::Geometry));
}
mProgramD3D->markUniformsClean();
return angle::Result::Continue;
}
angle::Result StateManager11::applyDriverUniformsForShader(const gl::Context *context,
gl::ShaderType shaderType)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
d3d11::Buffer &shaderDriverConstantBuffer = mShaderDriverConstantBuffers[shaderType];
if (!shaderDriverConstantBuffer.valid())
{
size_t requiredSize = mShaderConstants.getRequiredBufferSize(shaderType);
D3D11_BUFFER_DESC constantBufferDescription = {};
d3d11::InitConstantBufferDesc(&constantBufferDescription, requiredSize);
ANGLE_TRY(mRenderer->allocateResource(
GetImplAs<Context11>(context), constantBufferDescription, &shaderDriverConstantBuffer));
ID3D11Buffer *driverConstants = shaderDriverConstantBuffer.get();
switch (shaderType)
{
case gl::ShaderType::Vertex:
deviceContext->VSSetConstantBuffers(d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DRIVER, 1,
&driverConstants);
break;
case gl::ShaderType::Fragment:
deviceContext->PSSetConstantBuffers(d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DRIVER, 1,
&driverConstants);
break;
case gl::ShaderType::Geometry:
deviceContext->GSSetConstantBuffers(d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DRIVER, 1,
&driverConstants);
break;
default:
UNREACHABLE();
return angle::Result::Continue;
}
}
// Sampler metadata and driver constants need to coexist in the same constant buffer to
// conserve constant buffer slots. We update both in the constant buffer if needed.
ANGLE_TRY(mShaderConstants.updateBuffer(context, mRenderer, shaderType, *mProgramD3D,
shaderDriverConstantBuffer));
return angle::Result::Continue;
}
angle::Result StateManager11::applyDriverUniforms(const gl::Context *context)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
ANGLE_TRY(applyDriverUniformsForShader(context, gl::ShaderType::Vertex));
ANGLE_TRY(applyDriverUniformsForShader(context, gl::ShaderType::Fragment));
if (mProgramD3D->hasShaderStage(gl::ShaderType::Geometry))
{
ANGLE_TRY(applyDriverUniformsForShader(context, gl::ShaderType::Geometry));
}
// needed for the point sprite geometry shader
// GSSetConstantBuffers triggers device removal on 9_3, so we should only call it for ES3.
if (mRenderer->isES3Capable())
{
d3d11::Buffer &driverConstantBufferPS =
mShaderDriverConstantBuffers[gl::ShaderType::Fragment];
if (mCurrentGeometryConstantBuffer != driverConstantBufferPS.getSerial())
{
ASSERT(driverConstantBufferPS.valid());
deviceContext->GSSetConstantBuffers(0, 1, driverConstantBufferPS.getPointer());
mCurrentGeometryConstantBuffer = driverConstantBufferPS.getSerial();
}
}
return angle::Result::Continue;
}
angle::Result StateManager11::applyComputeUniforms(const gl::Context *context,
ProgramD3D *programD3D)
{
UniformStorage11 *computeUniformStorage =
GetAs<UniformStorage11>(programD3D->getShaderUniformStorage(gl::ShaderType::Compute));
ASSERT(computeUniformStorage);
const d3d11::Buffer *constantBuffer = nullptr;
ANGLE_TRY(computeUniformStorage->getConstantBuffer(context, mRenderer, &constantBuffer));
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
if (computeUniformStorage->size() > 0 &&
programD3D->areShaderUniformsDirty(gl::ShaderType::Compute))
{
UpdateUniformBuffer(deviceContext, computeUniformStorage, constantBuffer);
programD3D->markUniformsClean();
}
if (mCurrentComputeConstantBuffer != constantBuffer->getSerial())
{
deviceContext->CSSetConstantBuffers(
d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DEFAULT_UNIFORM_BLOCK, 1,
constantBuffer->getPointer());
mCurrentComputeConstantBuffer = constantBuffer->getSerial();
}
if (!mShaderDriverConstantBuffers[gl::ShaderType::Compute].valid())
{
size_t requiredSize = mShaderConstants.getRequiredBufferSize(gl::ShaderType::Compute);
D3D11_BUFFER_DESC constantBufferDescription = {};
d3d11::InitConstantBufferDesc(&constantBufferDescription, requiredSize);
ANGLE_TRY(
mRenderer->allocateResource(GetImplAs<Context11>(context), constantBufferDescription,
&mShaderDriverConstantBuffers[gl::ShaderType::Compute]));
ID3D11Buffer *buffer = mShaderDriverConstantBuffers[gl::ShaderType::Compute].get();
deviceContext->CSSetConstantBuffers(d3d11::RESERVED_CONSTANT_BUFFER_SLOT_DRIVER, 1,
&buffer);
}
ANGLE_TRY(mShaderConstants.updateBuffer(context, mRenderer, gl::ShaderType::Compute,
*programD3D,
mShaderDriverConstantBuffers[gl::ShaderType::Compute]));
return angle::Result::Continue;
}
angle::Result StateManager11::syncUniformBuffersForShader(const gl::Context *context,
gl::ShaderType shaderType)
{
const auto &glState = context->getState();
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
ID3D11DeviceContext1 *deviceContext1 = mRenderer->getDeviceContext1IfSupported();
const auto &shaderUniformBuffers = mProgramD3D->getShaderUniformBufferCache(shaderType);
for (size_t bufferIndex = 0; bufferIndex < shaderUniformBuffers.size(); ++bufferIndex)
{
const D3DUBOCache cache = shaderUniformBuffers[bufferIndex];
if (cache.binding == -1)
{
continue;
}
const auto &uniformBuffer = glState.getIndexedUniformBuffer(cache.binding);
const GLintptr uniformBufferOffset = uniformBuffer.getOffset();
const GLsizeiptr uniformBufferSize = uniformBuffer.getSize();
if (uniformBuffer.get() == nullptr)
{
continue;
}
Buffer11 *bufferStorage = GetImplAs<Buffer11>(uniformBuffer.get());
const d3d11::Buffer *constantBuffer = nullptr;
UINT firstConstant = 0;
UINT numConstants = 0;
ANGLE_TRY(bufferStorage->getConstantBufferRange(context, uniformBufferOffset,
uniformBufferSize, &constantBuffer,
&firstConstant, &numConstants));
ASSERT(constantBuffer);
switch (shaderType)
{
case gl::ShaderType::Vertex:
{
if (mCurrentConstantBufferVS[cache.registerIndex] == constantBuffer->getSerial() &&
mCurrentConstantBufferVSOffset[cache.registerIndex] == uniformBufferOffset &&
mCurrentConstantBufferVSSize[cache.registerIndex] == uniformBufferSize)
{
continue;
}
if (firstConstant != 0 && uniformBufferSize != 0)
{
ASSERT(numConstants != 0);
deviceContext1->VSSetConstantBuffers1(cache.registerIndex, 1,
constantBuffer->getPointer(),
&firstConstant, &numConstants);
}
else
{
deviceContext->VSSetConstantBuffers(cache.registerIndex, 1,
constantBuffer->getPointer());
}
mCurrentConstantBufferVS[cache.registerIndex] = constantBuffer->getSerial();
mCurrentConstantBufferVSOffset[cache.registerIndex] = uniformBufferOffset;
mCurrentConstantBufferVSSize[cache.registerIndex] = uniformBufferSize;
break;
}
case gl::ShaderType::Fragment:
{
if (mCurrentConstantBufferPS[cache.registerIndex] == constantBuffer->getSerial() &&
mCurrentConstantBufferPSOffset[cache.registerIndex] == uniformBufferOffset &&
mCurrentConstantBufferPSSize[cache.registerIndex] == uniformBufferSize)
{
continue;
}
if (firstConstant != 0 && uniformBufferSize != 0)
{
deviceContext1->PSSetConstantBuffers1(cache.registerIndex, 1,
constantBuffer->getPointer(),
&firstConstant, &numConstants);
}
else
{
deviceContext->PSSetConstantBuffers(cache.registerIndex, 1,
constantBuffer->getPointer());
}
mCurrentConstantBufferPS[cache.registerIndex] = constantBuffer->getSerial();
mCurrentConstantBufferPSOffset[cache.registerIndex] = uniformBufferOffset;
mCurrentConstantBufferPSSize[cache.registerIndex] = uniformBufferSize;
break;
}
case gl::ShaderType::Compute:
{
if (mCurrentConstantBufferCS[bufferIndex] == constantBuffer->getSerial() &&
mCurrentConstantBufferCSOffset[bufferIndex] == uniformBufferOffset &&
mCurrentConstantBufferCSSize[bufferIndex] == uniformBufferSize)
{
continue;
}
if (firstConstant != 0 && uniformBufferSize != 0)
{
deviceContext1->CSSetConstantBuffers1(cache.registerIndex, 1,
constantBuffer->getPointer(),
&firstConstant, &numConstants);
}
else
{
deviceContext->CSSetConstantBuffers(cache.registerIndex, 1,
constantBuffer->getPointer());
}
mCurrentConstantBufferCS[cache.registerIndex] = constantBuffer->getSerial();
mCurrentConstantBufferCSOffset[cache.registerIndex] = uniformBufferOffset;
mCurrentConstantBufferCSSize[cache.registerIndex] = uniformBufferSize;
break;
}
// TODO(jiawei.shao@intel.com): update geometry shader uniform buffers.
case gl::ShaderType::Geometry:
UNIMPLEMENTED();
break;
default:
UNREACHABLE();
}
}
const auto &shaderUniformBuffersUseSB =
mProgramD3D->getShaderUniformBufferCacheUseSB(shaderType);
for (size_t bufferIndex = 0; bufferIndex < shaderUniformBuffersUseSB.size(); ++bufferIndex)
{
const D3DUBOCacheUseSB cache = shaderUniformBuffersUseSB[bufferIndex];
if (cache.binding == -1)
{
continue;
}
const auto &uniformBuffer = glState.getIndexedUniformBuffer(cache.binding);
if (uniformBuffer.get() == nullptr)
{
continue;
}
const GLintptr uniformBufferOffset = uniformBuffer.getOffset();
Buffer11 *bufferStorage = GetImplAs<Buffer11>(uniformBuffer.get());
const d3d11::ShaderResourceView *bufferSRV = nullptr;
ANGLE_TRY(bufferStorage->getStructuredBufferRangeSRV(
context, static_cast<unsigned int>(uniformBufferOffset), cache.byteWidth,
cache.structureByteStride, &bufferSRV));
ASSERT(bufferSRV->valid());
setShaderResourceInternal(shaderType, cache.registerIndex, bufferSRV);
}
return angle::Result::Continue;
}
angle::Result StateManager11::getUAVsForShaderStorageBuffers(const gl::Context *context,
gl::ShaderType shaderType,
UAVList *uavList)
{
const gl::State &glState = context->getState();
const gl::Program *program = glState.getProgram();
angle::FixedVector<Buffer11 *, gl::IMPLEMENTATION_MAX_SHADER_STORAGE_BUFFER_BINDINGS>
previouslyBound;
for (size_t blockIndex = 0; blockIndex < program->getActiveShaderStorageBlockCount();
blockIndex++)
{
GLuint binding = program->getShaderStorageBlockBinding(static_cast<GLuint>(blockIndex));
const unsigned int registerIndex = mProgramD3D->getShaderStorageBufferRegisterIndex(
static_cast<GLuint>(blockIndex), shaderType);
// It means this block is active but not statically used.
if (registerIndex == GL_INVALID_INDEX)
{
continue;
}
const auto &shaderStorageBuffer = glState.getIndexedShaderStorageBuffer(binding);
if (shaderStorageBuffer.get() == nullptr)
{
// We didn't see a driver error like atomic buffer did. But theoretically, the same
// thing should be done.
setUnorderedAccessViewInternal<d3d11::UnorderedAccessView>(registerIndex, nullptr,
uavList);
continue;
}
Buffer11 *bufferStorage = GetImplAs<Buffer11>(shaderStorageBuffer.get());
if (std::find(previouslyBound.begin(), previouslyBound.end(), bufferStorage) !=
previouslyBound.end())
{
// D3D11 doesn't support binding a buffer multiple times
// http://anglebug.com/3032
ERR() << "Writing to multiple blocks on the same buffer is not allowed.";
return angle::Result::Stop;
}
previouslyBound.push_back(bufferStorage);
d3d11::UnorderedAccessView *uavPtr = nullptr;
GLsizeiptr viewSize = 0;
// Bindings only have a valid size if bound using glBindBufferRange
if (shaderStorageBuffer.getSize() > 0)
{
viewSize = shaderStorageBuffer.getSize();
}
// We use the buffer size for glBindBufferBase
else
{
viewSize = bufferStorage->getSize();
}
ANGLE_TRY(bufferStorage->getRawUAVRange(context, shaderStorageBuffer.getOffset(), viewSize,
&uavPtr));
setUnorderedAccessViewInternal(registerIndex, uavPtr, uavList);
}
return angle::Result::Continue;
}
angle::Result StateManager11::syncUniformBuffers(const gl::Context *context)
{
mProgramD3D->updateUniformBufferCache(context->getCaps());
if (mProgramD3D->hasShaderStage(gl::ShaderType::Compute))
{
ANGLE_TRY(syncUniformBuffersForShader(context, gl::ShaderType::Compute));
}
else
{
ANGLE_TRY(syncUniformBuffersForShader(context, gl::ShaderType::Vertex));
ANGLE_TRY(syncUniformBuffersForShader(context, gl::ShaderType::Fragment));
if (mProgramD3D->hasShaderStage(gl::ShaderType::Geometry))
{
ANGLE_TRY(syncUniformBuffersForShader(context, gl::ShaderType::Geometry));
}
}
return angle::Result::Continue;
}
angle::Result StateManager11::getUAVsForAtomicCounterBuffers(const gl::Context *context,
gl::ShaderType shaderType,
UAVList *uavList)
{
const gl::State &glState = context->getState();
const gl::Program *program = glState.getProgram();
for (const auto &atomicCounterBuffer : program->getState().getAtomicCounterBuffers())
{
GLuint binding = atomicCounterBuffer.binding;
const auto &buffer = glState.getIndexedAtomicCounterBuffer(binding);
const unsigned int registerIndex =
mProgramD3D->getAtomicCounterBufferRegisterIndex(binding, shaderType);
ASSERT(registerIndex != GL_INVALID_INDEX);
if (buffer.get() == nullptr)
{
// The atomic counter is used in shader. However, there is no buffer binding to it. We
// should clear the corresponding UAV in case the previous view type is a texture not a
// buffer. Otherwise, below error will be reported. The Unordered Access View dimension
// declared in the shader code (BUFFER) does not match the view type bound to slot 0
// of the Compute Shader unit (TEXTURE2D).
setUnorderedAccessViewInternal<d3d11::UnorderedAccessView>(registerIndex, nullptr,
uavList);
continue;
}
Buffer11 *bufferStorage = GetImplAs<Buffer11>(buffer.get());
// TODO(enrico.galli@intel.com): Check to make sure that we aren't binding the same buffer
// multiple times, as this is unsupported by D3D11. http://anglebug.com/3141
// Bindings only have a valid size if bound using glBindBufferRange. Therefore, we use the
// buffer size for glBindBufferBase
GLsizeiptr viewSize = (buffer.getSize() > 0) ? buffer.getSize() : bufferStorage->getSize();
d3d11::UnorderedAccessView *uavPtr = nullptr;
ANGLE_TRY(bufferStorage->getRawUAVRange(context, buffer.getOffset(), viewSize, &uavPtr));
setUnorderedAccessViewInternal(registerIndex, uavPtr, uavList);
}
return angle::Result::Continue;
}
angle::Result StateManager11::getUAVsForShader(const gl::Context *context,
gl::ShaderType shaderType,
UAVList *uavList)
{
ANGLE_TRY(getUAVsForShaderStorageBuffers(context, shaderType, uavList));
ANGLE_TRY(getUAVsForRWImages(context, shaderType, uavList));
ANGLE_TRY(getUAVsForAtomicCounterBuffers(context, shaderType, uavList));
return angle::Result::Continue;
}
angle::Result StateManager11::syncUAVsForGraphics(const gl::Context *context)
{
UAVList uavList(mRenderer->getNativeCaps().maxImageUnits);
ANGLE_TRY(getUAVsForShader(context, gl::ShaderType::Fragment, &uavList));
ANGLE_TRY(getUAVsForShader(context, gl::ShaderType::Vertex, &uavList));
if (uavList.highestUsed >= 0)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
UINT baseUAVRegister = static_cast<UINT>(mProgramD3D->getPixelShaderKey().size());
deviceContext->OMSetRenderTargetsAndUnorderedAccessViews(
D3D11_KEEP_RENDER_TARGETS_AND_DEPTH_STENCIL, nullptr, nullptr, baseUAVRegister,
uavList.highestUsed + 1, uavList.data.data(), nullptr);
}
return angle::Result::Continue;
}
angle::Result StateManager11::syncUAVsForCompute(const gl::Context *context)
{
UAVList uavList(mRenderer->getNativeCaps().maxImageUnits);
ANGLE_TRY(getUAVsForShader(context, gl::ShaderType::Compute, &uavList));
if (uavList.highestUsed >= 0)
{
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
deviceContext->CSSetUnorderedAccessViews(0, uavList.highestUsed + 1, uavList.data.data(),
nullptr);
}
return angle::Result::Continue;
}
angle::Result StateManager11::syncTransformFeedbackBuffers(const gl::Context *context)
{
const auto &glState = context->getState();
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
// If transform feedback is not active, unbind all buffers
if (!glState.isTransformFeedbackActiveUnpaused())
{
if (mAppliedTFSerial != mEmptySerial)
{
deviceContext->SOSetTargets(0, nullptr, nullptr);
mAppliedTFSerial = mEmptySerial;
}
return angle::Result::Continue;
}
gl::TransformFeedback *transformFeedback = glState.getCurrentTransformFeedback();
TransformFeedback11 *tf11 = GetImplAs<TransformFeedback11>(transformFeedback);
if (mAppliedTFSerial == tf11->getSerial() && !tf11->isDirty())
{
return angle::Result::Continue;
}
const std::vector<ID3D11Buffer *> *soBuffers = nullptr;
ANGLE_TRY(tf11->getSOBuffers(context, &soBuffers));
const std::vector<UINT> &soOffsets = tf11->getSOBufferOffsets();
deviceContext->SOSetTargets(tf11->getNumSOBuffers(), soBuffers->data(), soOffsets.data());
mAppliedTFSerial = tf11->getSerial();
tf11->onApply();
return angle::Result::Continue;
}
void StateManager11::syncPrimitiveTopology(const gl::State &glState,
gl::PrimitiveMode currentDrawMode)
{
D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED;
// Don't cull everything by default, this also resets if we were previously culling
mCullEverything = false;
switch (currentDrawMode)
{
case gl::PrimitiveMode::Points:
{
bool usesPointSize = mProgramD3D->usesPointSize();
// ProgramBinary assumes non-point rendering if gl_PointSize isn't written,
// which affects varying interpolation. Since the value of gl_PointSize is
// undefined when not written, just skip drawing to avoid unexpected results.
if (!usesPointSize && !glState.isTransformFeedbackActiveUnpaused())
{
// Notify developers of risking undefined behavior.
WARN() << "Point rendering without writing to gl_PointSize.";
mCullEverything = true;
return;
}
// If instanced pointsprites are enabled and the shader uses gl_PointSize, the topology
// must be D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST.
if (usesPointSize && mRenderer->getFeatures().useInstancedPointSpriteEmulation.enabled)
{
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
}
else
{
primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST;
}
break;
}
case gl::PrimitiveMode::Lines:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST;
break;
case gl::PrimitiveMode::LineLoop:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP;
break;
case gl::PrimitiveMode::LineStrip:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP;
break;
case gl::PrimitiveMode::Triangles:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
mCullEverything = CullsEverything(glState);
break;
case gl::PrimitiveMode::TriangleStrip:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
mCullEverything = CullsEverything(glState);
break;
// emulate fans via rewriting index buffer
case gl::PrimitiveMode::TriangleFan:
primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
mCullEverything = CullsEverything(glState);
break;
default:
UNREACHABLE();
break;
}
setPrimitiveTopologyInternal(primitiveTopology);
}
} // namespace rx