Hash :
ccd9a43b
Author :
Date :
2024-05-22T11:15:39
Split EGL_ANGLE_device_d3d into D3D9 and D3D11 versions. Add EGL_ANGLE_device_d3d9 and EGL_ANGLE_device_d3d11 which make it possible to know what type of device can be queried ahead of time without generating EGL errors. Refactor the DeviceD3D class into Device9 and Device11. Remove the getType method now that it's not needed for internal validation. Keep EGL_ANGLE_device_d3d for backwards compatibility. Bug: angleproject:342096132 Change-Id: Ib950abad58e46a5be269891ea7afd0cb8534cbe8 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5559163 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: 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
//
// Copyright 2012 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.
//
// Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer.
#include "libANGLE/renderer/d3d/d3d9/Renderer9.h"
#include <EGL/eglext.h>
#include <sstream>
#include "common/utilities.h"
#include "libANGLE/Buffer.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/Framebuffer.h"
#include "libANGLE/FramebufferAttachment.h"
#include "libANGLE/Program.h"
#include "libANGLE/Renderbuffer.h"
#include "libANGLE/State.h"
#include "libANGLE/Surface.h"
#include "libANGLE/Texture.h"
#include "libANGLE/angletypes.h"
#include "libANGLE/features.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/renderer/d3d/CompilerD3D.h"
#include "libANGLE/renderer/d3d/DisplayD3D.h"
#include "libANGLE/renderer/d3d/FramebufferD3D.h"
#include "libANGLE/renderer/d3d/IndexDataManager.h"
#include "libANGLE/renderer/d3d/ProgramD3D.h"
#include "libANGLE/renderer/d3d/ProgramExecutableD3D.h"
#include "libANGLE/renderer/d3d/RenderbufferD3D.h"
#include "libANGLE/renderer/d3d/ShaderD3D.h"
#include "libANGLE/renderer/d3d/SurfaceD3D.h"
#include "libANGLE/renderer/d3d/TextureD3D.h"
#include "libANGLE/renderer/d3d/d3d9/Blit9.h"
#include "libANGLE/renderer/d3d/d3d9/Buffer9.h"
#include "libANGLE/renderer/d3d/d3d9/Context9.h"
#include "libANGLE/renderer/d3d/d3d9/Device9.h"
#include "libANGLE/renderer/d3d/d3d9/Fence9.h"
#include "libANGLE/renderer/d3d/d3d9/Framebuffer9.h"
#include "libANGLE/renderer/d3d/d3d9/Image9.h"
#include "libANGLE/renderer/d3d/d3d9/IndexBuffer9.h"
#include "libANGLE/renderer/d3d/d3d9/NativeWindow9.h"
#include "libANGLE/renderer/d3d/d3d9/Query9.h"
#include "libANGLE/renderer/d3d/d3d9/RenderTarget9.h"
#include "libANGLE/renderer/d3d/d3d9/ShaderExecutable9.h"
#include "libANGLE/renderer/d3d/d3d9/SwapChain9.h"
#include "libANGLE/renderer/d3d/d3d9/TextureStorage9.h"
#include "libANGLE/renderer/d3d/d3d9/VertexArray9.h"
#include "libANGLE/renderer/d3d/d3d9/VertexBuffer9.h"
#include "libANGLE/renderer/d3d/d3d9/formatutils9.h"
#include "libANGLE/renderer/d3d/d3d9/renderer9_utils.h"
#include "libANGLE/renderer/d3d/driver_utils_d3d.h"
#include "libANGLE/renderer/driver_utils.h"
#include "libANGLE/trace.h"
#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
# define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL3
#endif
// Enable ANGLE_SUPPORT_SHADER_MODEL_2 if you wish devices with only shader model 2.
// Such a device would not be conformant.
#ifndef ANGLE_SUPPORT_SHADER_MODEL_2
# define ANGLE_SUPPORT_SHADER_MODEL_2 0
#endif
namespace rx
{
namespace
{
enum
{
MAX_VERTEX_CONSTANT_VECTORS_D3D9 = 256,
MAX_PIXEL_CONSTANT_VECTORS_SM2 = 32,
MAX_PIXEL_CONSTANT_VECTORS_SM3 = 224,
MAX_VARYING_VECTORS_SM2 = 8,
MAX_VARYING_VECTORS_SM3 = 10,
MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4
};
template <typename T>
static void DrawPoints(IDirect3DDevice9 *device, GLsizei count, const void *indices, int minIndex)
{
for (int i = 0; i < count; i++)
{
unsigned int indexValue =
static_cast<unsigned int>(static_cast<const T *>(indices)[i]) - minIndex;
device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1);
}
}
// A hard limit on buffer size. This works around a problem in the NVIDIA drivers where buffer sizes
// close to MAX_UINT would give undefined results. The limit of MAX_UINT/2 should be generous enough
// for almost any demanding application.
constexpr UINT kMaximumBufferSizeHardLimit = std::numeric_limits<UINT>::max() >> 1;
} // anonymous namespace
Renderer9::Renderer9(egl::Display *display) : RendererD3D(display), mStateManager(this)
{
mD3d9Module = nullptr;
mD3d9 = nullptr;
mD3d9Ex = nullptr;
mDevice = nullptr;
mDeviceEx = nullptr;
mDeviceWindow = nullptr;
mBlit = nullptr;
mAdapter = D3DADAPTER_DEFAULT;
const egl::AttributeMap &attributes = display->getAttributeMap();
EGLint requestedDeviceType = static_cast<EGLint>(attributes.get(
EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE));
switch (requestedDeviceType)
{
case EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE:
mDeviceType = D3DDEVTYPE_HAL;
break;
case EGL_PLATFORM_ANGLE_DEVICE_TYPE_D3D_REFERENCE_ANGLE:
mDeviceType = D3DDEVTYPE_REF;
break;
case EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE:
mDeviceType = D3DDEVTYPE_NULLREF;
break;
default:
UNREACHABLE();
}
mMaskedClearSavedState = nullptr;
mVertexDataManager = nullptr;
mIndexDataManager = nullptr;
mLineLoopIB = nullptr;
mCountingIB = nullptr;
mMaxNullColorbufferLRU = 0;
for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
{
mNullRenderTargetCache[i].lruCount = 0;
mNullRenderTargetCache[i].width = 0;
mNullRenderTargetCache[i].height = 0;
mNullRenderTargetCache[i].renderTarget = nullptr;
}
mAppliedVertexShader = nullptr;
mAppliedPixelShader = nullptr;
mAppliedProgramSerial = 0;
gl::InitializeDebugAnnotations(&mAnnotator);
}
void Renderer9::setGlobalDebugAnnotator()
{
gl::InitializeDebugAnnotations(&mAnnotator);
}
Renderer9::~Renderer9()
{
if (mDevice)
{
// If the device is lost, reset it first to prevent leaving the driver in an unstable state
if (testDeviceLost())
{
resetDevice();
}
}
release();
}
void Renderer9::release()
{
gl::UninitializeDebugAnnotations();
mTranslatedAttribCache.clear();
releaseDeviceResources();
SafeRelease(mDevice);
SafeRelease(mDeviceEx);
SafeRelease(mD3d9);
SafeRelease(mD3d9Ex);
mCompiler.release();
if (mDeviceWindow)
{
DestroyWindow(mDeviceWindow);
mDeviceWindow = nullptr;
}
mD3d9Module = nullptr;
}
egl::Error Renderer9::initialize()
{
ANGLE_TRACE_EVENT0("gpu.angle", "GetModuleHandle_d3d9");
mD3d9Module = ::LoadLibrary(TEXT("d3d9.dll"));
if (mD3d9Module == nullptr)
{
return egl::EglNotInitialized(D3D9_INIT_MISSING_DEP) << "No D3D9 module found.";
}
typedef HRESULT(WINAPI * Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex **);
Direct3DCreate9ExFunc Direct3DCreate9ExPtr =
reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
// Use Direct3D9Ex if available. Among other things, this version is less
// inclined to report a lost context, for example when the user switches
// desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are
// available.
if (static_cast<bool>(ANGLE_D3D9EX) && Direct3DCreate9ExPtr &&
SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
{
ANGLE_TRACE_EVENT0("gpu.angle", "D3d9Ex_QueryInterface");
ASSERT(mD3d9Ex);
mD3d9Ex->QueryInterface(__uuidof(IDirect3D9), reinterpret_cast<void **>(&mD3d9));
ASSERT(mD3d9);
}
else
{
ANGLE_TRACE_EVENT0("gpu.angle", "Direct3DCreate9");
mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
}
if (!mD3d9)
{
return egl::EglNotInitialized(D3D9_INIT_MISSING_DEP) << "Could not create D3D9 device.";
}
if (mDisplay->getNativeDisplayId() != nullptr)
{
// UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context
// corresponds to
}
HRESULT result;
// Give up on getting device caps after about one second.
{
ANGLE_TRACE_EVENT0("gpu.angle", "GetDeviceCaps");
for (int i = 0; i < 10; ++i)
{
result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
if (SUCCEEDED(result))
{
break;
}
else if (result == D3DERR_NOTAVAILABLE)
{
Sleep(100); // Give the driver some time to initialize/recover
}
else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY,
// D3DERR_INVALIDDEVICE, or another error we can't recover
// from
{
return egl::EglNotInitialized(D3D9_INIT_OTHER_ERROR)
<< "Failed to get device caps, " << gl::FmtHR(result);
}
}
}
#if ANGLE_SUPPORT_SHADER_MODEL_2
size_t minShaderModel = 2;
#else
size_t minShaderModel = 3;
#endif
if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(minShaderModel, 0))
{
return egl::EglNotInitialized(D3D9_INIT_UNSUPPORTED_VERSION)
<< "Renderer does not support PS " << minShaderModel << ".0, aborting!";
}
// When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture
// to a render target texture is not supported. This is required by
// Texture2D::ensureRenderTarget.
if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
{
return egl::EglNotInitialized(D3D9_INIT_UNSUPPORTED_STRETCHRECT)
<< "Renderer does not support StretctRect from textures.";
}
{
ANGLE_TRACE_EVENT0("gpu.angle", "GetAdapterIdentifier");
mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
}
static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
static const TCHAR className[] = TEXT("STATIC");
{
ANGLE_TRACE_EVENT0("gpu.angle", "CreateWindowEx");
mDeviceWindow =
CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1,
1, HWND_MESSAGE, nullptr, GetModuleHandle(nullptr), nullptr);
}
D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
DWORD behaviorFlags =
D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES | D3DCREATE_MULTITHREADED;
{
ANGLE_TRACE_EVENT0("gpu.angle", "D3d9_CreateDevice");
result = mD3d9->CreateDevice(
mAdapter, mDeviceType, mDeviceWindow,
behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE,
&presentParameters, &mDevice);
if (FAILED(result))
{
ERR() << "CreateDevice1 failed: (" << gl::FmtHR(result) << ")";
}
}
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
{
return egl::EglBadAlloc(D3D9_INIT_OUT_OF_MEMORY)
<< "CreateDevice failed: device lost or out of memory (" << gl::FmtHR(result) << ")";
}
if (FAILED(result))
{
ANGLE_TRACE_EVENT0("gpu.angle", "D3d9_CreateDevice2");
result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow,
behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&presentParameters, &mDevice);
if (FAILED(result))
{
ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY ||
result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
return egl::EglBadAlloc(D3D9_INIT_OUT_OF_MEMORY)
<< "CreateDevice2 failed: device lost, not available, or of out of memory ("
<< gl::FmtHR(result) << ")";
}
}
if (mD3d9Ex)
{
ANGLE_TRACE_EVENT0("gpu.angle", "mDevice_QueryInterface");
result = mDevice->QueryInterface(__uuidof(IDirect3DDevice9Ex), (void **)&mDeviceEx);
ASSERT(SUCCEEDED(result));
}
{
ANGLE_TRACE_EVENT0("gpu.angle", "ShaderCache initialize");
mVertexShaderCache.initialize(mDevice);
mPixelShaderCache.initialize(mDevice);
}
D3DDISPLAYMODE currentDisplayMode;
mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode);
// Check vertex texture support
// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
// We test this using D3D9 by checking support for the R16F format.
mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) &&
SUCCEEDED(mD3d9->CheckDeviceFormat(
mAdapter, mDeviceType, currentDisplayMode.Format,
D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F));
ANGLE_TRY(initializeDevice());
return egl::NoError();
}
// do any one-time device initialization
// NOTE: this is also needed after a device lost/reset
// to reset the scene status and ensure the default states are reset.
egl::Error Renderer9::initializeDevice()
{
// Permanent non-default states
mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE);
if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0))
{
mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD &)mDeviceCaps.MaxPointSize);
}
else
{
mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f
}
const gl::Caps &rendererCaps = getNativeCaps();
mCurVertexSamplerStates.resize(rendererCaps.maxShaderTextureImageUnits[gl::ShaderType::Vertex]);
mCurPixelSamplerStates.resize(
rendererCaps.maxShaderTextureImageUnits[gl::ShaderType::Fragment]);
mCurVertexTextures.resize(rendererCaps.maxShaderTextureImageUnits[gl::ShaderType::Vertex]);
mCurPixelTextures.resize(rendererCaps.maxShaderTextureImageUnits[gl::ShaderType::Fragment]);
markAllStateDirty();
mSceneStarted = false;
ASSERT(!mBlit);
mBlit = new Blit9(this);
ASSERT(!mVertexDataManager && !mIndexDataManager);
mIndexDataManager = new IndexDataManager(this);
mTranslatedAttribCache.resize(getNativeCaps().maxVertexAttributes);
mStateManager.initialize();
return egl::NoError();
}
D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters()
{
D3DPRESENT_PARAMETERS presentParameters = {};
// The default swap chain is never actually used. Surface will create a new swap chain with the
// proper parameters.
presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
presentParameters.BackBufferCount = 1;
presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
presentParameters.BackBufferWidth = 1;
presentParameters.BackBufferHeight = 1;
presentParameters.EnableAutoDepthStencil = FALSE;
presentParameters.Flags = 0;
presentParameters.hDeviceWindow = mDeviceWindow;
presentParameters.MultiSampleQuality = 0;
presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
presentParameters.Windowed = TRUE;
return presentParameters;
}
egl::ConfigSet Renderer9::generateConfigs()
{
static const GLenum colorBufferFormats[] = {
GL_BGR5_A1_ANGLEX,
GL_BGRA8_EXT,
GL_RGB565,
};
static const GLenum depthStencilBufferFormats[] = {
GL_NONE,
GL_DEPTH_COMPONENT32_OES,
GL_DEPTH24_STENCIL8_OES,
GL_DEPTH_COMPONENT24_OES,
GL_DEPTH_COMPONENT16,
};
const gl::Caps &rendererCaps = getNativeCaps();
const gl::TextureCapsMap &rendererTextureCaps = getNativeTextureCaps();
D3DDISPLAYMODE currentDisplayMode;
mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode);
// Determine the min and max swap intervals
int minSwapInterval = 4;
int maxSwapInterval = 0;
if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
{
minSwapInterval = std::min(minSwapInterval, 0);
maxSwapInterval = std::max(maxSwapInterval, 0);
}
if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
{
minSwapInterval = std::min(minSwapInterval, 1);
maxSwapInterval = std::max(maxSwapInterval, 1);
}
if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
{
minSwapInterval = std::min(minSwapInterval, 2);
maxSwapInterval = std::max(maxSwapInterval, 2);
}
if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
{
minSwapInterval = std::min(minSwapInterval, 3);
maxSwapInterval = std::max(maxSwapInterval, 3);
}
if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
{
minSwapInterval = std::min(minSwapInterval, 4);
maxSwapInterval = std::max(maxSwapInterval, 4);
}
egl::ConfigSet configs;
for (size_t formatIndex = 0; formatIndex < ArraySize(colorBufferFormats); formatIndex++)
{
GLenum colorBufferInternalFormat = colorBufferFormats[formatIndex];
const gl::TextureCaps &colorBufferFormatCaps =
rendererTextureCaps.get(colorBufferInternalFormat);
if (colorBufferFormatCaps.renderbuffer)
{
ASSERT(colorBufferFormatCaps.textureAttachment);
for (size_t depthStencilIndex = 0;
depthStencilIndex < ArraySize(depthStencilBufferFormats); depthStencilIndex++)
{
GLenum depthStencilBufferInternalFormat =
depthStencilBufferFormats[depthStencilIndex];
const gl::TextureCaps &depthStencilBufferFormatCaps =
rendererTextureCaps.get(depthStencilBufferInternalFormat);
if (depthStencilBufferFormatCaps.renderbuffer ||
depthStencilBufferInternalFormat == GL_NONE)
{
ASSERT(depthStencilBufferFormatCaps.textureAttachment ||
depthStencilBufferInternalFormat == GL_NONE);
const gl::InternalFormat &colorBufferFormatInfo =
gl::GetSizedInternalFormatInfo(colorBufferInternalFormat);
const gl::InternalFormat &depthStencilBufferFormatInfo =
gl::GetSizedInternalFormatInfo(depthStencilBufferInternalFormat);
const d3d9::TextureFormat &d3d9ColorBufferFormatInfo =
d3d9::GetTextureFormatInfo(colorBufferInternalFormat);
egl::Config config;
config.renderTargetFormat = colorBufferInternalFormat;
config.depthStencilFormat = depthStencilBufferInternalFormat;
config.bufferSize = colorBufferFormatInfo.getEGLConfigBufferSize();
config.redSize = colorBufferFormatInfo.redBits;
config.greenSize = colorBufferFormatInfo.greenBits;
config.blueSize = colorBufferFormatInfo.blueBits;
config.luminanceSize = colorBufferFormatInfo.luminanceBits;
config.alphaSize = colorBufferFormatInfo.alphaBits;
config.alphaMaskSize = 0;
config.bindToTextureRGB = (colorBufferFormatInfo.format == GL_RGB);
config.bindToTextureRGBA = (colorBufferFormatInfo.format == GL_RGBA ||
colorBufferFormatInfo.format == GL_BGRA_EXT);
config.colorBufferType = EGL_RGB_BUFFER;
// Mark as slow if blits to the back-buffer won't be straight forward
config.configCaveat =
(currentDisplayMode.Format == d3d9ColorBufferFormatInfo.renderFormat)
? EGL_NONE
: EGL_SLOW_CONFIG;
config.configID = static_cast<EGLint>(configs.size() + 1);
config.conformant = EGL_OPENGL_ES2_BIT;
config.depthSize = depthStencilBufferFormatInfo.depthBits;
config.level = 0;
config.matchNativePixmap = EGL_NONE;
config.maxPBufferWidth = rendererCaps.max2DTextureSize;
config.maxPBufferHeight = rendererCaps.max2DTextureSize;
config.maxPBufferPixels =
rendererCaps.max2DTextureSize * rendererCaps.max2DTextureSize;
config.maxSwapInterval = maxSwapInterval;
config.minSwapInterval = minSwapInterval;
config.nativeRenderable = EGL_FALSE;
config.nativeVisualID = 0;
config.nativeVisualType = EGL_NONE;
config.renderableType = EGL_OPENGL_ES2_BIT;
config.sampleBuffers = 0; // FIXME: enumerate multi-sampling
config.samples = 0;
config.stencilSize = depthStencilBufferFormatInfo.stencilBits;
config.surfaceType =
EGL_PBUFFER_BIT | EGL_WINDOW_BIT | EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
config.transparentType = EGL_NONE;
config.transparentRedValue = 0;
config.transparentGreenValue = 0;
config.transparentBlueValue = 0;
config.colorComponentType = gl_egl::GLComponentTypeToEGLColorComponentType(
colorBufferFormatInfo.componentType);
configs.add(config);
}
}
}
}
ASSERT(configs.size() > 0);
return configs;
}
void Renderer9::generateDisplayExtensions(egl::DisplayExtensions *outExtensions) const
{
outExtensions->createContextRobustness = true;
if (getShareHandleSupport())
{
outExtensions->d3dShareHandleClientBuffer = true;
outExtensions->surfaceD3DTexture2DShareHandle = true;
}
outExtensions->d3dTextureClientBuffer = true;
outExtensions->querySurfacePointer = true;
outExtensions->windowFixedSize = true;
outExtensions->postSubBuffer = true;
outExtensions->image = true;
outExtensions->imageBase = true;
outExtensions->glTexture2DImage = true;
outExtensions->glRenderbufferImage = true;
outExtensions->noConfigContext = true;
// Contexts are virtualized so textures and semaphores can be shared globally
outExtensions->displayTextureShareGroup = true;
outExtensions->displaySemaphoreShareGroup = true;
// D3D9 can be used without an output surface
outExtensions->surfacelessContext = true;
outExtensions->robustResourceInitializationANGLE = true;
}
void Renderer9::startScene()
{
if (!mSceneStarted)
{
long result = mDevice->BeginScene();
if (SUCCEEDED(result))
{
// This is defensive checking against the device being
// lost at unexpected times.
mSceneStarted = true;
}
}
}
void Renderer9::endScene()
{
if (mSceneStarted)
{
// EndScene can fail if the device was lost, for example due
// to a TDR during a draw call.
mDevice->EndScene();
mSceneStarted = false;
}
}
angle::Result Renderer9::flush(const gl::Context *context)
{
IDirect3DQuery9 *query = nullptr;
ANGLE_TRY(allocateEventQuery(context, &query));
Context9 *context9 = GetImplAs<Context9>(context);
HRESULT result = query->Issue(D3DISSUE_END);
ANGLE_TRY_HR(context9, result, "Failed to issue event query");
// Grab the query data once
result = query->GetData(nullptr, 0, D3DGETDATA_FLUSH);
freeEventQuery(query);
ANGLE_TRY_HR(context9, result, "Failed to get event query data");
return angle::Result::Continue;
}
angle::Result Renderer9::finish(const gl::Context *context)
{
IDirect3DQuery9 *query = nullptr;
ANGLE_TRY(allocateEventQuery(context, &query));
Context9 *context9 = GetImplAs<Context9>(context);
HRESULT result = query->Issue(D3DISSUE_END);
ANGLE_TRY_HR(context9, result, "Failed to issue event query");
// Grab the query data once
result = query->GetData(nullptr, 0, D3DGETDATA_FLUSH);
if (FAILED(result))
{
freeEventQuery(query);
}
ANGLE_TRY_HR(context9, result, "Failed to get event query data");
// Loop until the query completes
unsigned int attempt = 0;
while (result == S_FALSE)
{
// Keep polling, but allow other threads to do something useful first
std::this_thread::yield();
result = query->GetData(nullptr, 0, D3DGETDATA_FLUSH);
attempt++;
if (result == S_FALSE)
{
// explicitly check for device loss
// some drivers seem to return S_FALSE even if the device is lost
// instead of D3DERR_DEVICELOST like they should
bool checkDeviceLost = (attempt % kPollingD3DDeviceLostCheckFrequency) == 0;
if (checkDeviceLost && testDeviceLost())
{
result = D3DERR_DEVICELOST;
}
}
if (FAILED(result))
{
freeEventQuery(query);
}
ANGLE_TRY_HR(context9, result, "Failed to get event query data");
}
freeEventQuery(query);
return angle::Result::Continue;
}
bool Renderer9::isValidNativeWindow(EGLNativeWindowType window) const
{
return NativeWindow9::IsValidNativeWindow(window);
}
NativeWindowD3D *Renderer9::createNativeWindow(EGLNativeWindowType window,
const egl::Config *,
const egl::AttributeMap &) const
{
return new NativeWindow9(window);
}
SwapChainD3D *Renderer9::createSwapChain(NativeWindowD3D *nativeWindow,
HANDLE shareHandle,
IUnknown *d3dTexture,
GLenum backBufferFormat,
GLenum depthBufferFormat,
EGLint orientation,
EGLint samples)
{
return new SwapChain9(this, GetAs<NativeWindow9>(nativeWindow), shareHandle, d3dTexture,
backBufferFormat, depthBufferFormat, orientation);
}
egl::Error Renderer9::getD3DTextureInfo(const egl::Config *configuration,
IUnknown *d3dTexture,
const egl::AttributeMap &attribs,
EGLint *width,
EGLint *height,
GLsizei *samples,
gl::Format *glFormat,
const angle::Format **angleFormat,
UINT *arraySlice) const
{
IDirect3DTexture9 *texture = nullptr;
if (FAILED(d3dTexture->QueryInterface(&texture)))
{
return egl::EglBadParameter() << "Client buffer is not a IDirect3DTexture9";
}
IDirect3DDevice9 *textureDevice = nullptr;
texture->GetDevice(&textureDevice);
if (textureDevice != mDevice)
{
SafeRelease(texture);
return egl::EglBadParameter() << "Texture's device does not match.";
}
SafeRelease(textureDevice);
D3DSURFACE_DESC desc;
texture->GetLevelDesc(0, &desc);
SafeRelease(texture);
if (width)
{
*width = static_cast<EGLint>(desc.Width);
}
if (height)
{
*height = static_cast<EGLint>(desc.Height);
}
// GetSamplesCount() returns 0 when multisampling isn't used.
GLsizei sampleCount = d3d9_gl::GetSamplesCount(desc.MultiSampleType);
if ((configuration && configuration->samples > 1) || sampleCount != 0)
{
return egl::EglBadParameter() << "Multisampling not supported for client buffer texture";
}
if (samples)
{
*samples = static_cast<EGLint>(sampleCount);
}
// From table egl.restrictions in EGL_ANGLE_d3d_texture_client_buffer.
switch (desc.Format)
{
case D3DFMT_R8G8B8:
case D3DFMT_A8R8G8B8:
case D3DFMT_A16B16G16R16F:
case D3DFMT_A32B32G32R32F:
break;
default:
return egl::EglBadParameter()
<< "Unknown client buffer texture format: " << desc.Format;
}
const auto &d3dFormatInfo = d3d9::GetD3DFormatInfo(desc.Format);
ASSERT(d3dFormatInfo.info().id != angle::FormatID::NONE);
if (glFormat)
{
*glFormat = gl::Format(d3dFormatInfo.info().glInternalFormat);
}
if (angleFormat)
{
*angleFormat = &d3dFormatInfo.info();
}
if (arraySlice)
{
*arraySlice = 0;
}
return egl::NoError();
}
egl::Error Renderer9::validateShareHandle(const egl::Config *config,
HANDLE shareHandle,
const egl::AttributeMap &attribs) const
{
if (shareHandle == nullptr)
{
return egl::EglBadParameter() << "NULL share handle.";
}
EGLint width = attribs.getAsInt(EGL_WIDTH, 0);
EGLint height = attribs.getAsInt(EGL_HEIGHT, 0);
ASSERT(width != 0 && height != 0);
const d3d9::TextureFormat &backBufferd3dFormatInfo =
d3d9::GetTextureFormatInfo(config->renderTargetFormat);
IDirect3DTexture9 *texture = nullptr;
HRESULT result = mDevice->CreateTexture(width, height, 1, D3DUSAGE_RENDERTARGET,
backBufferd3dFormatInfo.texFormat, D3DPOOL_DEFAULT,
&texture, &shareHandle);
if (FAILED(result))
{
return egl::EglBadParameter() << "Failed to open share handle, " << gl::FmtHR(result);
}
DWORD levelCount = texture->GetLevelCount();
D3DSURFACE_DESC desc;
texture->GetLevelDesc(0, &desc);
SafeRelease(texture);
if (levelCount != 1 || desc.Width != static_cast<UINT>(width) ||
desc.Height != static_cast<UINT>(height) ||
desc.Format != backBufferd3dFormatInfo.texFormat)
{
return egl::EglBadParameter() << "Invalid texture parameters in share handle texture.";
}
return egl::NoError();
}
ContextImpl *Renderer9::createContext(const gl::State &state, gl::ErrorSet *errorSet)
{
return new Context9(state, errorSet, this);
}
void *Renderer9::getD3DDevice()
{
return mDevice;
}
angle::Result Renderer9::allocateEventQuery(const gl::Context *context, IDirect3DQuery9 **outQuery)
{
if (mEventQueryPool.empty())
{
HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, outQuery);
ANGLE_TRY_HR(GetImplAs<Context9>(context), result, "Failed to allocate event query");
}
else
{
*outQuery = mEventQueryPool.back();
mEventQueryPool.pop_back();
}
return angle::Result::Continue;
}
void Renderer9::freeEventQuery(IDirect3DQuery9 *query)
{
if (mEventQueryPool.size() > 1000)
{
SafeRelease(query);
}
else
{
mEventQueryPool.push_back(query);
}
}
angle::Result Renderer9::createVertexShader(d3d::Context *context,
const DWORD *function,
size_t length,
IDirect3DVertexShader9 **outShader)
{
return mVertexShaderCache.create(context, function, length, outShader);
}
angle::Result Renderer9::createPixelShader(d3d::Context *context,
const DWORD *function,
size_t length,
IDirect3DPixelShader9 **outShader)
{
return mPixelShaderCache.create(context, function, length, outShader);
}
HRESULT Renderer9::createVertexBuffer(UINT Length,
DWORD Usage,
IDirect3DVertexBuffer9 **ppVertexBuffer)
{
// Force buffers to be limited to a fixed max size.
if (Length > kMaximumBufferSizeHardLimit)
{
return E_OUTOFMEMORY;
}
D3DPOOL Pool = getBufferPool(Usage);
return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, nullptr);
}
VertexBuffer *Renderer9::createVertexBuffer()
{
return new VertexBuffer9(this);
}
HRESULT Renderer9::createIndexBuffer(UINT Length,
DWORD Usage,
D3DFORMAT Format,
IDirect3DIndexBuffer9 **ppIndexBuffer)
{
// Force buffers to be limited to a fixed max size.
if (Length > kMaximumBufferSizeHardLimit)
{
return E_OUTOFMEMORY;
}
D3DPOOL Pool = getBufferPool(Usage);
return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, nullptr);
}
IndexBuffer *Renderer9::createIndexBuffer()
{
return new IndexBuffer9(this);
}
StreamProducerImpl *Renderer9::createStreamProducerD3DTexture(
egl::Stream::ConsumerType consumerType,
const egl::AttributeMap &attribs)
{
// Streams are not supported under D3D9
UNREACHABLE();
return nullptr;
}
bool Renderer9::supportsFastCopyBufferToTexture(GLenum internalFormat) const
{
// Pixel buffer objects are not supported in D3D9, since D3D9 is ES2-only and PBOs are ES3.
return false;
}
angle::Result Renderer9::fastCopyBufferToTexture(const gl::Context *context,
const gl::PixelUnpackState &unpack,
gl::Buffer *unpackBuffer,
unsigned int offset,
RenderTargetD3D *destRenderTarget,
GLenum destinationFormat,
GLenum sourcePixelsType,
const gl::Box &destArea)
{
// Pixel buffer objects are not supported in D3D9, since D3D9 is ES2-only and PBOs are ES3.
ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
return angle::Result::Stop;
}
angle::Result Renderer9::setSamplerState(const gl::Context *context,
gl::ShaderType type,
int index,
gl::Texture *texture,
const gl::SamplerState &samplerState)
{
CurSamplerState &appliedSampler = (type == gl::ShaderType::Fragment)
? mCurPixelSamplerStates[index]
: mCurVertexSamplerStates[index];
// Make sure to add the level offset for our tiny compressed texture workaround
TextureD3D *textureD3D = GetImplAs<TextureD3D>(texture);
TextureStorage *storage = nullptr;
ANGLE_TRY(textureD3D->getNativeTexture(context, &storage));
// Storage should exist, texture should be complete
ASSERT(storage);
DWORD baseLevel = texture->getBaseLevel() + storage->getTopLevel();
if (appliedSampler.forceSet || appliedSampler.baseLevel != baseLevel ||
memcmp(&samplerState, &appliedSampler, sizeof(gl::SamplerState)) != 0)
{
int d3dSamplerOffset = (type == gl::ShaderType::Fragment) ? 0 : D3DVERTEXTEXTURESAMPLER0;
int d3dSampler = index + d3dSamplerOffset;
mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU,
gl_d3d9::ConvertTextureWrap(samplerState.getWrapS()));
mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV,
gl_d3d9::ConvertTextureWrap(samplerState.getWrapT()));
mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER,
gl_d3d9::ConvertMagFilter(samplerState.getMagFilter(),
samplerState.getMaxAnisotropy()));
D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
float lodBias;
gl_d3d9::ConvertMinFilter(samplerState.getMinFilter(), &d3dMinFilter, &d3dMipFilter,
&lodBias, samplerState.getMaxAnisotropy(), baseLevel);
mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, baseLevel);
mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPMAPLODBIAS, static_cast<DWORD>(lodBias));
if (getNativeExtensions().textureFilterAnisotropicEXT)
{
DWORD maxAnisotropy = std::min(mDeviceCaps.MaxAnisotropy,
static_cast<DWORD>(samplerState.getMaxAnisotropy()));
mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, maxAnisotropy);
}
const gl::InternalFormat &info =
gl::GetSizedInternalFormatInfo(textureD3D->getBaseLevelInternalFormat());
mDevice->SetSamplerState(d3dSampler, D3DSAMP_SRGBTEXTURE, info.colorEncoding == GL_SRGB);
if (samplerState.usesBorderColor())
{
angle::ColorGeneric borderColor = texture->getBorderColor();
ASSERT(borderColor.type == angle::ColorGeneric::Type::Float);
// Enforce opaque alpha for opaque formats, excluding DXT1 RGBA as it has no bits info.
if (info.alphaBits == 0 && info.componentCount < 4)
{
borderColor.colorF.alpha = 1.0f;
}
if (info.isLUMA())
{
if (info.luminanceBits == 0)
{
borderColor.colorF.red = 0.0f;
}
// Older Intel drivers use RGBA border color when sampling from D3DFMT_A8L8.
// However, some recent Intel drivers sample alpha from green border channel
// when using this format. Assume the old behavior because newer GPUs should
// use D3D11 anyway.
borderColor.colorF.green = borderColor.colorF.red;
borderColor.colorF.blue = borderColor.colorF.red;
}
D3DCOLOR d3dBorderColor;
if (info.colorEncoding == GL_SRGB && getFeatures().borderColorSrgb.enabled)
{
d3dBorderColor =
D3DCOLOR_RGBA(gl::linearToSRGB(gl::clamp01(borderColor.colorF.red)),
gl::linearToSRGB(gl::clamp01(borderColor.colorF.green)),
gl::linearToSRGB(gl::clamp01(borderColor.colorF.blue)),
gl::unorm<8>(borderColor.colorF.alpha));
}
else
{
d3dBorderColor = gl_d3d9::ConvertColor(borderColor.colorF);
}
mDevice->SetSamplerState(d3dSampler, D3DSAMP_BORDERCOLOR, d3dBorderColor);
}
}
appliedSampler.forceSet = false;
appliedSampler.samplerState = samplerState;
appliedSampler.baseLevel = baseLevel;
return angle::Result::Continue;
}
angle::Result Renderer9::setTexture(const gl::Context *context,
gl::ShaderType type,
int index,
gl::Texture *texture)
{
int d3dSamplerOffset = (type == gl::ShaderType::Fragment) ? 0 : D3DVERTEXTEXTURESAMPLER0;
int d3dSampler = index + d3dSamplerOffset;
IDirect3DBaseTexture9 *d3dTexture = nullptr;
bool forceSetTexture = false;
std::vector<uintptr_t> &appliedTextures =
(type == gl::ShaderType::Fragment) ? mCurPixelTextures : mCurVertexTextures;
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);
TextureStorage9 *storage9 = GetAs<TextureStorage9>(texStorage);
ANGLE_TRY(storage9->getBaseTexture(context, &d3dTexture));
// If we get NULL back from getBaseTexture here, something went wrong
// in the texture class and we're unexpectedly missing the d3d texture
ASSERT(d3dTexture != nullptr);
forceSetTexture = textureImpl->hasDirtyImages();
textureImpl->resetDirty();
}
if (forceSetTexture || appliedTextures[index] != reinterpret_cast<uintptr_t>(d3dTexture))
{
mDevice->SetTexture(d3dSampler, d3dTexture);
}
appliedTextures[index] = reinterpret_cast<uintptr_t>(d3dTexture);
return angle::Result::Continue;
}
angle::Result Renderer9::updateState(const gl::Context *context, gl::PrimitiveMode drawMode)
{
const auto &glState = context->getState();
// Applies the render target surface, depth stencil surface, viewport rectangle and
// scissor rectangle to the renderer
gl::Framebuffer *framebuffer = glState.getDrawFramebuffer();
ASSERT(framebuffer && !framebuffer->hasAnyDirtyBit());
Framebuffer9 *framebuffer9 = GetImplAs<Framebuffer9>(framebuffer);
ANGLE_TRY(applyRenderTarget(context, framebuffer9->getCachedColorRenderTargets()[0],
framebuffer9->getCachedDepthStencilRenderTarget()));
// Setting viewport state
setViewport(glState.getViewport(), glState.getNearPlane(), glState.getFarPlane(), drawMode,
glState.getRasterizerState().frontFace, false);
// Setting scissors state
setScissorRectangle(glState.getScissor(), glState.isScissorTestEnabled());
// Setting blend, depth stencil, and rasterizer states
// Since framebuffer->getSamples will return the original samples which may be different with
// the sample counts that we set in render target view, here we use renderTarget->getSamples to
// get the actual samples.
GLsizei samples = 0;
const gl::FramebufferAttachment *firstColorAttachment = framebuffer->getFirstColorAttachment();
if (firstColorAttachment)
{
ASSERT(firstColorAttachment->isAttached());
RenderTarget9 *renderTarget = nullptr;
ANGLE_TRY(firstColorAttachment->getRenderTarget(context, firstColorAttachment->getSamples(),
&renderTarget));
samples = renderTarget->getSamples();
mDevice->SetRenderState(D3DRS_SRGBWRITEENABLE,
renderTarget->getInternalFormat() == GL_SRGB8_ALPHA8);
}
gl::RasterizerState rasterizer = glState.getRasterizerState();
rasterizer.pointDrawMode = (drawMode == gl::PrimitiveMode::Points);
rasterizer.multiSample = (samples != 0);
ANGLE_TRY(setBlendDepthRasterStates(context, drawMode));
mStateManager.resetDirtyBits();
return angle::Result::Continue;
}
void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled)
{
mStateManager.setScissorState(scissor, enabled);
}
angle::Result Renderer9::setBlendDepthRasterStates(const gl::Context *context,
gl::PrimitiveMode drawMode)
{
const auto &glState = context->getState();
gl::Framebuffer *drawFramebuffer = glState.getDrawFramebuffer();
ASSERT(!drawFramebuffer->hasAnyDirtyBit());
// Since framebuffer->getSamples will return the original samples which may be different with
// the sample counts that we set in render target view, here we use renderTarget->getSamples to
// get the actual samples.
GLsizei samples = 0;
const gl::FramebufferAttachment *firstColorAttachment =
drawFramebuffer->getFirstColorAttachment();
if (firstColorAttachment)
{
ASSERT(firstColorAttachment->isAttached());
RenderTarget9 *renderTarget = nullptr;
ANGLE_TRY(firstColorAttachment->getRenderTarget(context, firstColorAttachment->getSamples(),
&renderTarget));
samples = renderTarget->getSamples();
}
gl::RasterizerState rasterizer = glState.getRasterizerState();
rasterizer.pointDrawMode = (drawMode == gl::PrimitiveMode::Points);
rasterizer.multiSample = (samples != 0);
unsigned int mask = GetBlendSampleMask(glState, samples);
mStateManager.setBlendDepthRasterStates(glState, mask);
return angle::Result::Continue;
}
void Renderer9::setViewport(const gl::Rectangle &viewport,
float zNear,
float zFar,
gl::PrimitiveMode drawMode,
GLenum frontFace,
bool ignoreViewport)
{
mStateManager.setViewportState(viewport, zNear, zFar, drawMode, frontFace, ignoreViewport);
}
bool Renderer9::applyPrimitiveType(gl::PrimitiveMode mode, GLsizei count, bool usesPointSize)
{
switch (mode)
{
case gl::PrimitiveMode::Points:
mPrimitiveType = D3DPT_POINTLIST;
mPrimitiveCount = count;
break;
case gl::PrimitiveMode::Lines:
mPrimitiveType = D3DPT_LINELIST;
mPrimitiveCount = count / 2;
break;
case gl::PrimitiveMode::LineLoop:
mPrimitiveType = D3DPT_LINESTRIP;
mPrimitiveCount =
count - 1; // D3D doesn't support line loops, so we draw the last line separately
break;
case gl::PrimitiveMode::LineStrip:
mPrimitiveType = D3DPT_LINESTRIP;
mPrimitiveCount = count - 1;
break;
case gl::PrimitiveMode::Triangles:
mPrimitiveType = D3DPT_TRIANGLELIST;
mPrimitiveCount = count / 3;
break;
case gl::PrimitiveMode::TriangleStrip:
mPrimitiveType = D3DPT_TRIANGLESTRIP;
mPrimitiveCount = count - 2;
break;
case gl::PrimitiveMode::TriangleFan:
mPrimitiveType = D3DPT_TRIANGLEFAN;
mPrimitiveCount = count - 2;
break;
default:
UNREACHABLE();
return false;
}
return mPrimitiveCount > 0;
}
angle::Result Renderer9::getNullColorRenderTarget(const gl::Context *context,
const RenderTarget9 *depthRenderTarget,
const RenderTarget9 **outColorRenderTarget)
{
ASSERT(depthRenderTarget);
const gl::Extents &size = depthRenderTarget->getExtents();
// search cached nullcolorbuffers
for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
{
if (mNullRenderTargetCache[i].renderTarget != nullptr &&
mNullRenderTargetCache[i].width == size.width &&
mNullRenderTargetCache[i].height == size.height)
{
mNullRenderTargetCache[i].lruCount = ++mMaxNullColorbufferLRU;
*outColorRenderTarget = mNullRenderTargetCache[i].renderTarget;
return angle::Result::Continue;
}
}
RenderTargetD3D *nullRenderTarget = nullptr;
ANGLE_TRY(createRenderTarget(context, size.width, size.height, GL_NONE, 0, &nullRenderTarget));
// add nullbuffer to the cache
NullRenderTargetCacheEntry *oldest = &mNullRenderTargetCache[0];
for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
{
if (mNullRenderTargetCache[i].lruCount < oldest->lruCount)
{
oldest = &mNullRenderTargetCache[i];
}
}
SafeDelete(oldest->renderTarget);
oldest->renderTarget = GetAs<RenderTarget9>(nullRenderTarget);
oldest->lruCount = ++mMaxNullColorbufferLRU;
oldest->width = size.width;
oldest->height = size.height;
*outColorRenderTarget = oldest->renderTarget;
return angle::Result::Continue;
}
angle::Result Renderer9::applyRenderTarget(const gl::Context *context,
const RenderTarget9 *colorRenderTargetIn,
const RenderTarget9 *depthStencilRenderTarget)
{
// if there is no color attachment we must synthesize a NULL colorattachment
// to keep the D3D runtime happy. This should only be possible if depth texturing.
const RenderTarget9 *colorRenderTarget = colorRenderTargetIn;
if (colorRenderTarget == nullptr)
{
ANGLE_TRY(getNullColorRenderTarget(context, depthStencilRenderTarget, &colorRenderTarget));
}
ASSERT(colorRenderTarget != nullptr);
size_t renderTargetWidth = 0;
size_t renderTargetHeight = 0;
bool renderTargetChanged = false;
unsigned int renderTargetSerial = colorRenderTarget->getSerial();
if (renderTargetSerial != mAppliedRenderTargetSerial)
{
// Apply the render target on the device
IDirect3DSurface9 *renderTargetSurface = colorRenderTarget->getSurface();
ASSERT(renderTargetSurface);
mDevice->SetRenderTarget(0, renderTargetSurface);
SafeRelease(renderTargetSurface);
renderTargetWidth = colorRenderTarget->getWidth();
renderTargetHeight = colorRenderTarget->getHeight();
mAppliedRenderTargetSerial = renderTargetSerial;
renderTargetChanged = true;
}
unsigned int depthStencilSerial = 0;
if (depthStencilRenderTarget != nullptr)
{
depthStencilSerial = depthStencilRenderTarget->getSerial();
}
if (depthStencilSerial != mAppliedDepthStencilSerial || !mDepthStencilInitialized)
{
unsigned int depthSize = 0;
unsigned int stencilSize = 0;
// Apply the depth stencil on the device
if (depthStencilRenderTarget)
{
IDirect3DSurface9 *depthStencilSurface = depthStencilRenderTarget->getSurface();
ASSERT(depthStencilSurface);
mDevice->SetDepthStencilSurface(depthStencilSurface);
SafeRelease(depthStencilSurface);
const gl::InternalFormat &format =
gl::GetSizedInternalFormatInfo(depthStencilRenderTarget->getInternalFormat());
depthSize = format.depthBits;
stencilSize = format.stencilBits;
}
else
{
mDevice->SetDepthStencilSurface(nullptr);
}
mStateManager.updateDepthSizeIfChanged(mDepthStencilInitialized, depthSize);
mStateManager.updateStencilSizeIfChanged(mDepthStencilInitialized, stencilSize);
mAppliedDepthStencilSerial = depthStencilSerial;
mDepthStencilInitialized = true;
}
if (renderTargetChanged || !mRenderTargetDescInitialized)
{
mStateManager.forceSetBlendState();
mStateManager.forceSetScissorState();
mStateManager.setRenderTargetBounds(renderTargetWidth, renderTargetHeight);
mRenderTargetDescInitialized = true;
}
return angle::Result::Continue;
}
angle::Result Renderer9::applyVertexBuffer(const gl::Context *context,
gl::PrimitiveMode mode,
GLint first,
GLsizei count,
GLsizei instances,
TranslatedIndexData * /*indexInfo*/)
{
const gl::State &state = context->getState();
ANGLE_TRY(mVertexDataManager->prepareVertexData(context, first, count, &mTranslatedAttribCache,
instances));
return mVertexDeclarationCache.applyDeclaration(context, mDevice, mTranslatedAttribCache,
state.getProgramExecutable(), first, instances,
&mRepeatDraw);
}
// Applies the indices and element array bindings to the Direct3D 9 device
angle::Result Renderer9::applyIndexBuffer(const gl::Context *context,
const void *indices,
GLsizei count,
gl::PrimitiveMode mode,
gl::DrawElementsType type,
TranslatedIndexData *indexInfo)
{
gl::VertexArray *vao = context->getState().getVertexArray();
gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer();
gl::DrawElementsType dstType = gl::DrawElementsType::InvalidEnum;
ANGLE_TRY(GetIndexTranslationDestType(context, count, type, indices, false, &dstType));
ANGLE_TRY(mIndexDataManager->prepareIndexData(context, type, dstType, count, elementArrayBuffer,
indices, indexInfo));
// Directly binding the storage buffer is not supported for d3d9
ASSERT(indexInfo->storage == nullptr);
if (indexInfo->serial != mAppliedIBSerial)
{
IndexBuffer9 *indexBuffer = GetAs<IndexBuffer9>(indexInfo->indexBuffer);
mDevice->SetIndices(indexBuffer->getBuffer());
mAppliedIBSerial = indexInfo->serial;
}
return angle::Result::Continue;
}
angle::Result Renderer9::drawArraysImpl(const gl::Context *context,
gl::PrimitiveMode mode,
GLint startVertex,
GLsizei count,
GLsizei instances)
{
ASSERT(!context->getState().isTransformFeedbackActiveUnpaused());
startScene();
if (mode == gl::PrimitiveMode::LineLoop)
{
return drawLineLoop(context, count, gl::DrawElementsType::InvalidEnum, nullptr, 0, nullptr);
}
if (instances > 0)
{
StaticIndexBufferInterface *countingIB = nullptr;
ANGLE_TRY(getCountingIB(context, count, &countingIB));
if (mAppliedIBSerial != countingIB->getSerial())
{
IndexBuffer9 *indexBuffer = GetAs<IndexBuffer9>(countingIB->getIndexBuffer());
mDevice->SetIndices(indexBuffer->getBuffer());
mAppliedIBSerial = countingIB->getSerial();
}
for (int i = 0; i < mRepeatDraw; i++)
{
mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount);
}
return angle::Result::Continue;
}
// Regular case
mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount);
return angle::Result::Continue;
}
angle::Result Renderer9::drawElementsImpl(const gl::Context *context,
gl::PrimitiveMode mode,
GLsizei count,
gl::DrawElementsType type,
const void *indices,
GLsizei instances)
{
TranslatedIndexData indexInfo;
ANGLE_TRY(applyIndexBuffer(context, indices, count, mode, type, &indexInfo));
gl::IndexRange indexRange;
ANGLE_TRY(context->getState().getVertexArray()->getIndexRange(context, type, count, indices,
&indexRange));
size_t vertexCount = indexRange.vertexCount();
ANGLE_TRY(applyVertexBuffer(context, mode, static_cast<GLsizei>(indexRange.start),
static_cast<GLsizei>(vertexCount), instances, &indexInfo));
startScene();
int minIndex = static_cast<int>(indexRange.start);
gl::VertexArray *vao = context->getState().getVertexArray();
gl::Buffer *elementArrayBuffer = vao->getElementArrayBuffer();
if (mode == gl::PrimitiveMode::Points)
{
return drawIndexedPoints(context, count, type, indices, minIndex, elementArrayBuffer);
}
if (mode == gl::PrimitiveMode::LineLoop)
{
return drawLineLoop(context, count, type, indices, minIndex, elementArrayBuffer);
}
for (int i = 0; i < mRepeatDraw; i++)
{
mDevice->DrawIndexedPrimitive(mPrimitiveType, -minIndex, minIndex,
static_cast<UINT>(vertexCount), indexInfo.startIndex,
mPrimitiveCount);
}
return angle::Result::Continue;
}
angle::Result Renderer9::drawLineLoop(const gl::Context *context,
GLsizei count,
gl::DrawElementsType type,
const void *indices,
int minIndex,
gl::Buffer *elementArrayBuffer)
{
// Get the raw indices for an indexed draw
if (type != gl::DrawElementsType::InvalidEnum && elementArrayBuffer)
{
BufferD3D *storage = GetImplAs<BufferD3D>(elementArrayBuffer);
intptr_t offset = reinterpret_cast<intptr_t>(indices);
const uint8_t *bufferData = nullptr;
ANGLE_TRY(storage->getData(context, &bufferData));
indices = bufferData + offset;
}
unsigned int startIndex = 0;
Context9 *context9 = GetImplAs<Context9>(context);
if (getNativeExtensions().elementIndexUintOES)
{
if (!mLineLoopIB)
{
mLineLoopIB = new StreamingIndexBufferInterface(this);
ANGLE_TRY(mLineLoopIB->reserveBufferSpace(context, INITIAL_INDEX_BUFFER_SIZE,
gl::DrawElementsType::UnsignedInt));
}
// Checked by Renderer9::applyPrimitiveType
ASSERT(count >= 0);
ANGLE_CHECK(context9,
static_cast<unsigned int>(count) + 1 <=
(std::numeric_limits<unsigned int>::max() / sizeof(unsigned int)),
"Failed to create a 32-bit looping index buffer for "
"GL_LINE_LOOP, too many indices required.",
GL_OUT_OF_MEMORY);
const unsigned int spaceNeeded =
(static_cast<unsigned int>(count) + 1) * sizeof(unsigned int);
ANGLE_TRY(mLineLoopIB->reserveBufferSpace(context, spaceNeeded,
gl::DrawElementsType::UnsignedInt));
void *mappedMemory = nullptr;
unsigned int offset = 0;
ANGLE_TRY(mLineLoopIB->mapBuffer(context, spaceNeeded, &mappedMemory, &offset));
startIndex = static_cast<unsigned int>(offset) / 4;
unsigned int *data = static_cast<unsigned int *>(mappedMemory);
switch (type)
{
case gl::DrawElementsType::InvalidEnum: // Non-indexed draw
for (int i = 0; i < count; i++)
{
data[i] = i;
}
data[count] = 0;
break;
case gl::DrawElementsType::UnsignedByte:
for (int i = 0; i < count; i++)
{
data[i] = static_cast<const GLubyte *>(indices)[i];
}
data[count] = static_cast<const GLubyte *>(indices)[0];
break;
case gl::DrawElementsType::UnsignedShort:
for (int i = 0; i < count; i++)
{
data[i] = static_cast<const GLushort *>(indices)[i];
}
data[count] = static_cast<const GLushort *>(indices)[0];
break;
case gl::DrawElementsType::UnsignedInt:
for (int i = 0; i < count; i++)
{
data[i] = static_cast<const GLuint *>(indices)[i];
}
data[count] = static_cast<const GLuint *>(indices)[0];
break;
default:
UNREACHABLE();
}
ANGLE_TRY(mLineLoopIB->unmapBuffer(context));
}
else
{
if (!mLineLoopIB)
{
mLineLoopIB = new StreamingIndexBufferInterface(this);
ANGLE_TRY(mLineLoopIB->reserveBufferSpace(context, INITIAL_INDEX_BUFFER_SIZE,
gl::DrawElementsType::UnsignedShort));
}
// Checked by Renderer9::applyPrimitiveType
ASSERT(count >= 0);
ANGLE_CHECK(context9,
static_cast<unsigned int>(count) + 1 <=
(std::numeric_limits<unsigned short>::max() / sizeof(unsigned short)),
"Failed to create a 16-bit looping index buffer for "
"GL_LINE_LOOP, too many indices required.",
GL_OUT_OF_MEMORY);
const unsigned int spaceNeeded =
(static_cast<unsigned int>(count) + 1) * sizeof(unsigned short);
ANGLE_TRY(mLineLoopIB->reserveBufferSpace(context, spaceNeeded,
gl::DrawElementsType::UnsignedShort));
void *mappedMemory = nullptr;
unsigned int offset;
ANGLE_TRY(mLineLoopIB->mapBuffer(context, spaceNeeded, &mappedMemory, &offset));
startIndex = static_cast<unsigned int>(offset) / 2;
unsigned short *data = static_cast<unsigned short *>(mappedMemory);
switch (type)
{
case gl::DrawElementsType::InvalidEnum: // Non-indexed draw
for (int i = 0; i < count; i++)
{
data[i] = static_cast<unsigned short>(i);
}
data[count] = 0;
break;
case gl::DrawElementsType::UnsignedByte:
for (int i = 0; i < count; i++)
{
data[i] = static_cast<const GLubyte *>(indices)[i];
}
data[count] = static_cast<const GLubyte *>(indices)[0];
break;
case gl::DrawElementsType::UnsignedShort:
for (int i = 0; i < count; i++)
{
data[i] = static_cast<const GLushort *>(indices)[i];
}
data[count] = static_cast<const GLushort *>(indices)[0];
break;
case gl::DrawElementsType::UnsignedInt:
for (int i = 0; i < count; i++)
{
data[i] = static_cast<unsigned short>(static_cast<const GLuint *>(indices)[i]);
}
data[count] = static_cast<unsigned short>(static_cast<const GLuint *>(indices)[0]);
break;
default:
UNREACHABLE();
}
ANGLE_TRY(mLineLoopIB->unmapBuffer(context));
}
if (mAppliedIBSerial != mLineLoopIB->getSerial())
{
IndexBuffer9 *indexBuffer = GetAs<IndexBuffer9>(mLineLoopIB->getIndexBuffer());
mDevice->SetIndices(indexBuffer->getBuffer());
mAppliedIBSerial = mLineLoopIB->getSerial();
}
mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
return angle::Result::Continue;
}
angle::Result Renderer9::drawIndexedPoints(const gl::Context *context,
GLsizei count,
gl::DrawElementsType type,
const void *indices,
int minIndex,
gl::Buffer *elementArrayBuffer)
{
// Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call
// for each individual point. This call is not expected to happen often.
if (elementArrayBuffer)
{
BufferD3D *storage = GetImplAs<BufferD3D>(elementArrayBuffer);
intptr_t offset = reinterpret_cast<intptr_t>(indices);
const uint8_t *bufferData = nullptr;
ANGLE_TRY(storage->getData(context, &bufferData));
indices = bufferData + offset;
}
switch (type)
{
case gl::DrawElementsType::UnsignedByte:
DrawPoints<GLubyte>(mDevice, count, indices, minIndex);
return angle::Result::Continue;
case gl::DrawElementsType::UnsignedShort:
DrawPoints<GLushort>(mDevice, count, indices, minIndex);
return angle::Result::Continue;
case gl::DrawElementsType::UnsignedInt:
DrawPoints<GLuint>(mDevice, count, indices, minIndex);
return angle::Result::Continue;
default:
ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
}
}
angle::Result Renderer9::getCountingIB(const gl::Context *context,
size_t count,
StaticIndexBufferInterface **outIB)
{
// Update the counting index buffer if it is not large enough or has not been created yet.
if (count <= 65536) // 16-bit indices
{
const unsigned int spaceNeeded = static_cast<unsigned int>(count) * sizeof(unsigned short);
if (!mCountingIB || mCountingIB->getBufferSize() < spaceNeeded)
{
SafeDelete(mCountingIB);
mCountingIB = new StaticIndexBufferInterface(this);
ANGLE_TRY(mCountingIB->reserveBufferSpace(context, spaceNeeded,
gl::DrawElementsType::UnsignedShort));
void *mappedMemory = nullptr;
ANGLE_TRY(mCountingIB->mapBuffer(context, spaceNeeded, &mappedMemory, nullptr));
unsigned short *data = static_cast<unsigned short *>(mappedMemory);
for (size_t i = 0; i < count; i++)
{
data[i] = static_cast<unsigned short>(i);
}
ANGLE_TRY(mCountingIB->unmapBuffer(context));
}
}
else if (getNativeExtensions().elementIndexUintOES)
{
const unsigned int spaceNeeded = static_cast<unsigned int>(count) * sizeof(unsigned int);
if (!mCountingIB || mCountingIB->getBufferSize() < spaceNeeded)
{
SafeDelete(mCountingIB);
mCountingIB = new StaticIndexBufferInterface(this);
ANGLE_TRY(mCountingIB->reserveBufferSpace(context, spaceNeeded,
gl::DrawElementsType::UnsignedInt));
void *mappedMemory = nullptr;
ANGLE_TRY(mCountingIB->mapBuffer(context, spaceNeeded, &mappedMemory, nullptr));
unsigned int *data = static_cast<unsigned int *>(mappedMemory);
for (unsigned int i = 0; i < count; i++)
{
data[i] = i;
}
ANGLE_TRY(mCountingIB->unmapBuffer(context));
}
}
else
{
ANGLE_TRY_HR(GetImplAs<Context9>(context), E_OUTOFMEMORY,
"Could not create a counting index buffer for glDrawArraysInstanced.");
}
*outIB = mCountingIB;
return angle::Result::Continue;
}
angle::Result Renderer9::applyShaders(const gl::Context *context, gl::PrimitiveMode drawMode)
{
const gl::State &state = context->getState();
Context9 *context9 = GetImplAs<Context9>(context);
RendererD3D *renderer = context9->getRenderer();
// This method is called single-threaded.
ANGLE_TRY(ensureHLSLCompilerInitialized(context9));
ProgramExecutableD3D *executableD3D =
GetImplAs<ProgramExecutableD3D>(state.getProgramExecutable());
VertexArray9 *vao = GetImplAs<VertexArray9>(state.getVertexArray());
executableD3D->updateCachedInputLayout(renderer, vao->getCurrentStateSerial(), state);
ShaderExecutableD3D *vertexExe = nullptr;
ANGLE_TRY(executableD3D->getVertexExecutableForCachedInputLayout(context9, renderer, &vertexExe,
nullptr));
const gl::Framebuffer *drawFramebuffer = state.getDrawFramebuffer();
executableD3D->updateCachedOutputLayout(context, drawFramebuffer);
ShaderExecutableD3D *pixelExe = nullptr;
ANGLE_TRY(executableD3D->getPixelExecutableForCachedOutputLayout(context9, renderer, &pixelExe,
nullptr));
IDirect3DVertexShader9 *vertexShader =
(vertexExe ? GetAs<ShaderExecutable9>(vertexExe)->getVertexShader() : nullptr);
IDirect3DPixelShader9 *pixelShader =
(pixelExe ? GetAs<ShaderExecutable9>(pixelExe)->getPixelShader() : nullptr);
if (vertexShader != mAppliedVertexShader)
{
mDevice->SetVertexShader(vertexShader);
mAppliedVertexShader = vertexShader;
}
if (pixelShader != mAppliedPixelShader)
{
mDevice->SetPixelShader(pixelShader);
mAppliedPixelShader = pixelShader;
}
// D3D9 has a quirk where creating multiple shaders with the same content
// can return the same shader pointer. Because GL programs store different data
// per-program, checking the program serial guarantees we upload fresh
// uniform data even if our shader pointers are the same.
// https://code.google.com/p/angleproject/issues/detail?id=661
unsigned int programSerial = executableD3D->getSerial();
if (programSerial != mAppliedProgramSerial)
{
executableD3D->dirtyAllUniforms();
mStateManager.forceSetDXUniformsState();
mAppliedProgramSerial = programSerial;
}
applyUniforms(executableD3D);
// Driver uniforms
mStateManager.setShaderConstants();
return angle::Result::Continue;
}
void Renderer9::applyUniforms(ProgramExecutableD3D *executableD3D)
{
// Skip updates if we're not dirty. Note that D3D9 cannot have compute or geometry.
if (!executableD3D->anyShaderUniformsDirty())
{
return;
}
const auto &uniformArray = executableD3D->getD3DUniforms();
for (const D3DUniform *targetUniform : uniformArray)
{
// Built-in uniforms must be skipped.
if (!targetUniform->isReferencedByShader(gl::ShaderType::Vertex) &&
!targetUniform->isReferencedByShader(gl::ShaderType::Fragment))
continue;
const GLfloat *f = reinterpret_cast<const GLfloat *>(targetUniform->firstNonNullData());
const GLint *i = reinterpret_cast<const GLint *>(targetUniform->firstNonNullData());
switch (targetUniform->typeInfo.type)
{
case GL_SAMPLER_2D:
case GL_SAMPLER_CUBE:
case GL_SAMPLER_EXTERNAL_OES:
case GL_SAMPLER_VIDEO_IMAGE_WEBGL:
break;
case GL_BOOL:
case GL_BOOL_VEC2:
case GL_BOOL_VEC3:
case GL_BOOL_VEC4:
applyUniformnbv(targetUniform, i);
break;
case GL_FLOAT:
case GL_FLOAT_VEC2:
case GL_FLOAT_VEC3:
case GL_FLOAT_VEC4:
case GL_FLOAT_MAT2:
case GL_FLOAT_MAT3:
case GL_FLOAT_MAT4:
applyUniformnfv(targetUniform, f);
break;
case GL_INT:
case GL_INT_VEC2:
case GL_INT_VEC3:
case GL_INT_VEC4:
applyUniformniv(targetUniform, i);
break;
default:
UNREACHABLE();
}
}
executableD3D->markUniformsClean();
}
void Renderer9::applyUniformnfv(const D3DUniform *targetUniform, const GLfloat *v)
{
if (targetUniform->isReferencedByShader(gl::ShaderType::Fragment))
{
mDevice->SetPixelShaderConstantF(
targetUniform->mShaderRegisterIndexes[gl::ShaderType::Fragment], v,
targetUniform->registerCount);
}
if (targetUniform->isReferencedByShader(gl::ShaderType::Vertex))
{
mDevice->SetVertexShaderConstantF(
targetUniform->mShaderRegisterIndexes[gl::ShaderType::Vertex], v,
targetUniform->registerCount);
}
}
void Renderer9::applyUniformniv(const D3DUniform *targetUniform, const GLint *v)
{
ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
for (unsigned int i = 0; i < targetUniform->registerCount; i++)
{
vector[i][0] = (GLfloat)v[4 * i + 0];
vector[i][1] = (GLfloat)v[4 * i + 1];
vector[i][2] = (GLfloat)v[4 * i + 2];
vector[i][3] = (GLfloat)v[4 * i + 3];
}
applyUniformnfv(targetUniform, (GLfloat *)vector);
}
void Renderer9::applyUniformnbv(const D3DUniform *targetUniform, const GLint *v)
{
ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9);
GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4];
for (unsigned int i = 0; i < targetUniform->registerCount; i++)
{
vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f;
vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f;
vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f;
vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f;
}
applyUniformnfv(targetUniform, (GLfloat *)vector);
}
void Renderer9::clear(const ClearParameters &clearParams,
const RenderTarget9 *colorRenderTarget,
const RenderTarget9 *depthStencilRenderTarget)
{
// Clearing buffers with non-float values is not supported by Renderer9 and ES 2.0
ASSERT(clearParams.colorType == GL_FLOAT);
// Clearing individual buffers other than buffer zero is not supported by Renderer9 and ES 2.0
bool clearColor = clearParams.clearColor[0];
for (unsigned int i = 0; i < clearParams.clearColor.size(); i++)
{
ASSERT(clearParams.clearColor[i] == clearColor);
}
float depth = gl::clamp01(clearParams.depthValue);
DWORD stencil = clearParams.stencilValue & 0x000000FF;
unsigned int stencilUnmasked = 0x0;
if (clearParams.clearStencil && depthStencilRenderTarget)
{
const gl::InternalFormat &depthStencilFormat =
gl::GetSizedInternalFormatInfo(depthStencilRenderTarget->getInternalFormat());
if (depthStencilFormat.stencilBits > 0)
{
const d3d9::D3DFormat &d3dFormatInfo =
d3d9::GetD3DFormatInfo(depthStencilRenderTarget->getD3DFormat());
stencilUnmasked = (0x1 << d3dFormatInfo.stencilBits) - 1;
}
}
const bool needMaskedStencilClear =
clearParams.clearStencil &&
(clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked;
bool needMaskedColorClear = false;
D3DCOLOR color = D3DCOLOR_ARGB(255, 0, 0, 0);
if (clearColor)
{
ASSERT(colorRenderTarget != nullptr);
const gl::InternalFormat &formatInfo =
gl::GetSizedInternalFormatInfo(colorRenderTarget->getInternalFormat());
const d3d9::D3DFormat &d3dFormatInfo =
d3d9::GetD3DFormatInfo(colorRenderTarget->getD3DFormat());
color =
D3DCOLOR_ARGB(gl::unorm<8>((formatInfo.alphaBits == 0 && d3dFormatInfo.alphaBits > 0)
? 1.0f
: clearParams.colorF.alpha),
gl::unorm<8>((formatInfo.redBits == 0 && d3dFormatInfo.redBits > 0)
? 0.0f
: clearParams.colorF.red),
gl::unorm<8>((formatInfo.greenBits == 0 && d3dFormatInfo.greenBits > 0)
? 0.0f
: clearParams.colorF.green),
gl::unorm<8>((formatInfo.blueBits == 0 && d3dFormatInfo.blueBits > 0)
? 0.0f
: clearParams.colorF.blue));
const uint8_t colorMask =
gl::BlendStateExt::ColorMaskStorage::GetValueIndexed(0, clearParams.colorMask);
bool r, g, b, a;
gl::BlendStateExt::UnpackColorMask(colorMask, &r, &g, &b, &a);
if ((formatInfo.redBits > 0 && !r) || (formatInfo.greenBits > 0 && !g) ||
(formatInfo.blueBits > 0 && !b) || (formatInfo.alphaBits > 0 && !a))
{
needMaskedColorClear = true;
}
}
if (needMaskedColorClear || needMaskedStencilClear)
{
// State which is altered in all paths from this point to the clear call is saved.
// State which is altered in only some paths will be flagged dirty in the case that
// that path is taken.
HRESULT hr;
if (mMaskedClearSavedState == nullptr)
{
hr = mDevice->BeginStateBlock();
ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
mDevice->SetPixelShader(nullptr);
mDevice->SetVertexShader(nullptr);
mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
mDevice->SetStreamSource(0, nullptr, 0, 0);
mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
{
mDevice->SetStreamSourceFreq(i, 1);
}
hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
}
ASSERT(mMaskedClearSavedState != nullptr);
if (mMaskedClearSavedState != nullptr)
{
hr = mMaskedClearSavedState->Capture();
ASSERT(SUCCEEDED(hr));
}
mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
if (clearColor)
{
// clearParams.colorMask follows the same packing scheme as
// D3DCOLORWRITEENABLE_RED/GREEN/BLUE/ALPHA
mDevice->SetRenderState(
D3DRS_COLORWRITEENABLE,
gl::BlendStateExt::ColorMaskStorage::GetValueIndexed(0, clearParams.colorMask));
}
else
{
mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
}
if (stencilUnmasked != 0x0 && clearParams.clearStencil)
{
mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask);
mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
}
else
{
mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
}
mDevice->SetPixelShader(nullptr);
mDevice->SetVertexShader(nullptr);
mDevice->SetFVF(D3DFVF_XYZRHW);
mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
{
mDevice->SetStreamSourceFreq(i, 1);
}
int renderTargetWidth = mStateManager.getRenderTargetWidth();
int renderTargetHeight = mStateManager.getRenderTargetHeight();
float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
quad[0][0] = -0.5f;
quad[0][1] = renderTargetHeight - 0.5f;
quad[0][2] = 0.0f;
quad[0][3] = 1.0f;
quad[1][0] = renderTargetWidth - 0.5f;
quad[1][1] = renderTargetHeight - 0.5f;
quad[1][2] = 0.0f;
quad[1][3] = 1.0f;
quad[2][0] = -0.5f;
quad[2][1] = -0.5f;
quad[2][2] = 0.0f;
quad[2][3] = 1.0f;
quad[3][0] = renderTargetWidth - 0.5f;
quad[3][1] = -0.5f;
quad[3][2] = 0.0f;
quad[3][3] = 1.0f;
startScene();
mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
if (clearParams.clearDepth)
{
mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
mDevice->Clear(0, nullptr, D3DCLEAR_ZBUFFER, color, depth, stencil);
}
if (mMaskedClearSavedState != nullptr)
{
mMaskedClearSavedState->Apply();
}
}
else if (clearColor || clearParams.clearDepth || clearParams.clearStencil)
{
DWORD dxClearFlags = 0;
if (clearColor)
{
dxClearFlags |= D3DCLEAR_TARGET;
}
if (clearParams.clearDepth)
{
dxClearFlags |= D3DCLEAR_ZBUFFER;
}
if (clearParams.clearStencil)
{
dxClearFlags |= D3DCLEAR_STENCIL;
}
mDevice->Clear(0, nullptr, dxClearFlags, color, depth, stencil);
}
}
void Renderer9::markAllStateDirty()
{
mAppliedRenderTargetSerial = 0;
mAppliedDepthStencilSerial = 0;
mDepthStencilInitialized = false;
mRenderTargetDescInitialized = false;
mStateManager.forceSetRasterState();
mStateManager.forceSetDepthStencilState();
mStateManager.forceSetBlendState();
mStateManager.forceSetScissorState();
mStateManager.forceSetViewportState();
ASSERT(mCurVertexSamplerStates.size() == mCurVertexTextures.size());
for (unsigned int i = 0; i < mCurVertexTextures.size(); i++)
{
mCurVertexSamplerStates[i].forceSet = true;
mCurVertexTextures[i] = angle::DirtyPointer;
}
ASSERT(mCurPixelSamplerStates.size() == mCurPixelTextures.size());
for (unsigned int i = 0; i < mCurPixelSamplerStates.size(); i++)
{
mCurPixelSamplerStates[i].forceSet = true;
mCurPixelTextures[i] = angle::DirtyPointer;
}
mAppliedIBSerial = 0;
mAppliedVertexShader = nullptr;
mAppliedPixelShader = nullptr;
mAppliedProgramSerial = 0;
mStateManager.forceSetDXUniformsState();
mVertexDeclarationCache.markStateDirty();
}
void Renderer9::releaseDeviceResources()
{
for (size_t i = 0; i < mEventQueryPool.size(); i++)
{
SafeRelease(mEventQueryPool[i]);
}
mEventQueryPool.clear();
SafeRelease(mMaskedClearSavedState);
mVertexShaderCache.clear();
mPixelShaderCache.clear();
SafeDelete(mBlit);
SafeDelete(mVertexDataManager);
SafeDelete(mIndexDataManager);
SafeDelete(mLineLoopIB);
SafeDelete(mCountingIB);
for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++)
{
SafeDelete(mNullRenderTargetCache[i].renderTarget);
}
}
// set notify to true to broadcast a message to all contexts of the device loss
bool Renderer9::testDeviceLost()
{
HRESULT status = getDeviceStatusCode();
return FAILED(status);
}
HRESULT Renderer9::getDeviceStatusCode()
{
HRESULT status = D3D_OK;
if (mDeviceEx)
{
status = mDeviceEx->CheckDeviceState(nullptr);
}
else if (mDevice)
{
status = mDevice->TestCooperativeLevel();
}
return status;
}
bool Renderer9::testDeviceResettable()
{
// On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted
// DEVICEREMOVED indicates the device has been stopped and must be recreated
switch (getDeviceStatusCode())
{
case D3DERR_DEVICENOTRESET:
case D3DERR_DEVICEHUNG:
return true;
case D3DERR_DEVICELOST:
return (mDeviceEx != nullptr);
case D3DERR_DEVICEREMOVED:
ASSERT(mDeviceEx != nullptr);
return isRemovedDeviceResettable();
default:
return false;
}
}
bool Renderer9::resetDevice()
{
releaseDeviceResources();
D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
HRESULT result = D3D_OK;
bool lost = testDeviceLost();
bool removedDevice = (getDeviceStatusCode() == D3DERR_DEVICEREMOVED);
// Device Removed is a feature which is only present with D3D9Ex
ASSERT(mDeviceEx != nullptr || !removedDevice);
for (int attempts = 3; lost && attempts > 0; attempts--)
{
if (removedDevice)
{
// Device removed, which may trigger on driver reinstallation,
// may cause a longer wait other reset attempts before the
// system is ready to handle creating a new device.
Sleep(800);
lost = !resetRemovedDevice();
}
else if (mDeviceEx)
{
Sleep(500); // Give the graphics driver some CPU time
result = mDeviceEx->ResetEx(&presentParameters, nullptr);
lost = testDeviceLost();
}
else
{
result = mDevice->TestCooperativeLevel();
while (result == D3DERR_DEVICELOST)
{
Sleep(100); // Give the graphics driver some CPU time
result = mDevice->TestCooperativeLevel();
}
if (result == D3DERR_DEVICENOTRESET)
{
result = mDevice->Reset(&presentParameters);
}
lost = testDeviceLost();
}
}
if (FAILED(result))
{
ERR() << "Reset/ResetEx failed multiple times, " << gl::FmtHR(result);
return false;
}
if (removedDevice && lost)
{
ERR() << "Device lost reset failed multiple times";
return false;
}
// If the device was removed, we already finished re-initialization in resetRemovedDevice
if (!removedDevice)
{
// reset device defaults
if (initializeDevice().isError())
{
return false;
}
}
return true;
}
bool Renderer9::isRemovedDeviceResettable() const
{
bool success = false;
#if ANGLE_D3D9EX
IDirect3D9Ex *d3d9Ex = nullptr;
typedef HRESULT(WINAPI * Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex **);
Direct3DCreate9ExFunc Direct3DCreate9ExPtr =
reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
if (Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &d3d9Ex)))
{
D3DCAPS9 deviceCaps;
HRESULT result = d3d9Ex->GetDeviceCaps(mAdapter, mDeviceType, &deviceCaps);
success = SUCCEEDED(result);
}
SafeRelease(d3d9Ex);
#else
UNREACHABLE();
#endif
return success;
}
bool Renderer9::resetRemovedDevice()
{
// From http://msdn.microsoft.com/en-us/library/windows/desktop/bb172554(v=vs.85).aspx:
// The hardware adapter has been removed. Application must destroy the device, do enumeration of
// adapters and create another Direct3D device. If application continues rendering without
// calling Reset, the rendering calls will succeed. Applies to Direct3D 9Ex only.
release();
return !initialize().isError();
}
VendorID Renderer9::getVendorId() const
{
return static_cast<VendorID>(mAdapterIdentifier.VendorId);
}
std::string Renderer9::getRendererDescription() const
{
std::ostringstream rendererString;
rendererString << mAdapterIdentifier.Description;
if (getShareHandleSupport())
{
rendererString << " Direct3D9Ex";
}
else
{
rendererString << " Direct3D9";
}
rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_"
<< D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion);
rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_"
<< D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
return rendererString.str();
}
DeviceIdentifier Renderer9::getAdapterIdentifier() const
{
DeviceIdentifier deviceIdentifier = {};
deviceIdentifier.VendorId = static_cast<UINT>(mAdapterIdentifier.VendorId);
deviceIdentifier.DeviceId = static_cast<UINT>(mAdapterIdentifier.DeviceId);
deviceIdentifier.SubSysId = static_cast<UINT>(mAdapterIdentifier.SubSysId);
deviceIdentifier.Revision = static_cast<UINT>(mAdapterIdentifier.Revision);
deviceIdentifier.FeatureLevel = 0;
return deviceIdentifier;
}
unsigned int Renderer9::getReservedVertexUniformVectors() const
{
return d3d9_gl::GetReservedVertexUniformVectors();
}
unsigned int Renderer9::getReservedFragmentUniformVectors() const
{
return d3d9_gl::GetReservedFragmentUniformVectors();
}
bool Renderer9::getShareHandleSupport() const
{
// PIX doesn't seem to support using share handles, so disable them.
return (mD3d9Ex != nullptr) && !gl::DebugAnnotationsActive(/*context=*/nullptr);
}
int Renderer9::getMajorShaderModel() const
{
return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion);
}
int Renderer9::getMinorShaderModel() const
{
return D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion);
}
std::string Renderer9::getShaderModelSuffix() const
{
return "";
}
DWORD Renderer9::getCapsDeclTypes() const
{
return mDeviceCaps.DeclTypes;
}
D3DPOOL Renderer9::getBufferPool(DWORD usage) const
{
if (mD3d9Ex != nullptr)
{
return D3DPOOL_DEFAULT;
}
else
{
if (!(usage & D3DUSAGE_DYNAMIC))
{
return D3DPOOL_MANAGED;
}
}
return D3DPOOL_DEFAULT;
}
angle::Result Renderer9::copyImage2D(const gl::Context *context,
const gl::Framebuffer *framebuffer,
const gl::Rectangle &sourceRect,
GLenum destFormat,
const gl::Offset &destOffset,
TextureStorage *storage,
GLint level)
{
RECT rect;
rect.left = sourceRect.x;
rect.top = sourceRect.y;
rect.right = sourceRect.x + sourceRect.width;
rect.bottom = sourceRect.y + sourceRect.height;
return mBlit->copy2D(context, framebuffer, rect, destFormat, destOffset, storage, level);
}
angle::Result Renderer9::copyImageCube(const gl::Context *context,
const gl::Framebuffer *framebuffer,
const gl::Rectangle &sourceRect,
GLenum destFormat,
const gl::Offset &destOffset,
TextureStorage *storage,
gl::TextureTarget target,
GLint level)
{
RECT rect;
rect.left = sourceRect.x;
rect.top = sourceRect.y;
rect.right = sourceRect.x + sourceRect.width;
rect.bottom = sourceRect.y + sourceRect.height;
return mBlit->copyCube(context, framebuffer, rect, destFormat, destOffset, storage, target,
level);
}
angle::Result Renderer9::copyImage3D(const gl::Context *context,
const gl::Framebuffer *framebuffer,
const gl::Rectangle &sourceRect,
GLenum destFormat,
const gl::Offset &destOffset,
TextureStorage *storage,
GLint level)
{
// 3D textures are not available in the D3D9 backend.
ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
return angle::Result::Stop;
}
angle::Result Renderer9::copyImage2DArray(const gl::Context *context,
const gl::Framebuffer *framebuffer,
const gl::Rectangle &sourceRect,
GLenum destFormat,
const gl::Offset &destOffset,
TextureStorage *storage,
GLint level)
{
// 2D array textures are not available in the D3D9 backend.
ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
return angle::Result::Stop;
}
angle::Result Renderer9::copyTexture(const gl::Context *context,
const gl::Texture *source,
GLint sourceLevel,
gl::TextureTarget srcTarget,
const gl::Box &sourceBox,
GLenum destFormat,
GLenum destType,
const gl::Offset &destOffset,
TextureStorage *storage,
gl::TextureTarget destTarget,
GLint destLevel,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha)
{
RECT rect;
rect.left = sourceBox.x;
rect.top = sourceBox.y;
rect.right = sourceBox.x + sourceBox.width;
rect.bottom = sourceBox.y + sourceBox.height;
return mBlit->copyTexture(context, source, sourceLevel, rect, destFormat, destOffset, storage,
destTarget, destLevel, unpackFlipY, unpackPremultiplyAlpha,
unpackUnmultiplyAlpha);
}
angle::Result Renderer9::copyCompressedTexture(const gl::Context *context,
const gl::Texture *source,
GLint sourceLevel,
TextureStorage *storage,
GLint destLevel)
{
ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
return angle::Result::Stop;
}
angle::Result Renderer9::createRenderTarget(const gl::Context *context,
int width,
int height,
GLenum format,
GLsizei samples,
RenderTargetD3D **outRT)
{
const d3d9::TextureFormat &d3d9FormatInfo = d3d9::GetTextureFormatInfo(format);
const gl::TextureCaps &textureCaps = getNativeTextureCaps().get(format);
GLuint supportedSamples = textureCaps.getNearestSamples(samples);
IDirect3DTexture9 *texture = nullptr;
IDirect3DSurface9 *renderTarget = nullptr;
if (width > 0 && height > 0)
{
bool requiresInitialization = false;
HRESULT result = D3DERR_INVALIDCALL;
const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(format);
if (formatInfo.depthBits > 0 || formatInfo.stencilBits > 0)
{
result = mDevice->CreateDepthStencilSurface(
width, height, d3d9FormatInfo.renderFormat,
gl_d3d9::GetMultisampleType(supportedSamples), 0, FALSE, &renderTarget, nullptr);
}
else
{
requiresInitialization = (d3d9FormatInfo.dataInitializerFunction != nullptr);
if (supportedSamples > 0)
{
result = mDevice->CreateRenderTarget(width, height, d3d9FormatInfo.renderFormat,
gl_d3d9::GetMultisampleType(supportedSamples),
0, FALSE, &renderTarget, nullptr);
}
else
{
result = mDevice->CreateTexture(
width, height, 1, D3DUSAGE_RENDERTARGET, d3d9FormatInfo.texFormat,
getTexturePool(D3DUSAGE_RENDERTARGET), &texture, nullptr);
if (!FAILED(result))
{
result = texture->GetSurfaceLevel(0, &renderTarget);
}
}
}
ANGLE_TRY_HR(GetImplAs<Context9>(context), result, "Failed to create render target");
if (requiresInitialization)
{
// This format requires that the data be initialized before the render target can be
// used Unfortunately this requires a Get call on the d3d device but it is far better
// than having to mark the render target as lockable and copy data to the gpu.
IDirect3DSurface9 *prevRenderTarget = nullptr;
mDevice->GetRenderTarget(0, &prevRenderTarget);
mDevice->SetRenderTarget(0, renderTarget);
mDevice->Clear(0, nullptr, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 255), 0.0f, 0);
mDevice->SetRenderTarget(0, prevRenderTarget);
}
}
*outRT = new TextureRenderTarget9(texture, 0, renderTarget, format, width, height, 1,
supportedSamples);
return angle::Result::Continue;
}
angle::Result Renderer9::createRenderTargetCopy(const gl::Context *context,
RenderTargetD3D *source,
RenderTargetD3D **outRT)
{
ASSERT(source != nullptr);
RenderTargetD3D *newRT = nullptr;
ANGLE_TRY(createRenderTarget(context, source->getWidth(), source->getHeight(),
source->getInternalFormat(), source->getSamples(), &newRT));
RenderTarget9 *source9 = GetAs<RenderTarget9>(source);
RenderTarget9 *dest9 = GetAs<RenderTarget9>(newRT);
HRESULT result = mDevice->StretchRect(source9->getSurface(), nullptr, dest9->getSurface(),
nullptr, D3DTEXF_NONE);
ANGLE_TRY_HR(GetImplAs<Context9>(context), result, "Failed to copy render target");
*outRT = newRT;
return angle::Result::Continue;
}
angle::Result Renderer9::loadExecutable(d3d::Context *context,
const uint8_t *function,
size_t length,
gl::ShaderType type,
const std::vector<D3DVarying> &streamOutVaryings,
bool separatedOutputBuffers,
ShaderExecutableD3D **outExecutable)
{
// Transform feedback is not supported in ES2 or D3D9
ASSERT(streamOutVaryings.empty());
switch (type)
{
case gl::ShaderType::Vertex:
{
IDirect3DVertexShader9 *vshader = nullptr;
ANGLE_TRY(createVertexShader(context, (DWORD *)function, length, &vshader));
*outExecutable = new ShaderExecutable9(function, length, vshader);
}
break;
case gl::ShaderType::Fragment:
{
IDirect3DPixelShader9 *pshader = nullptr;
ANGLE_TRY(createPixelShader(context, (DWORD *)function, length, &pshader));
*outExecutable = new ShaderExecutable9(function, length, pshader);
}
break;
default:
ANGLE_HR_UNREACHABLE(context);
}
return angle::Result::Continue;
}
angle::Result Renderer9::compileToExecutable(d3d::Context *context,
gl::InfoLog &infoLog,
const std::string &shaderHLSL,
gl::ShaderType type,
const std::vector<D3DVarying> &streamOutVaryings,
bool separatedOutputBuffers,
const CompilerWorkaroundsD3D &workarounds,
ShaderExecutableD3D **outExectuable)
{
// Transform feedback is not supported in ES2 or D3D9
ASSERT(streamOutVaryings.empty());
std::stringstream profileStream;
switch (type)
{
case gl::ShaderType::Vertex:
profileStream << "vs";
break;
case gl::ShaderType::Fragment:
profileStream << "ps";
break;
default:
ANGLE_HR_UNREACHABLE(context);
}
profileStream << "_" << ((getMajorShaderModel() >= 3) ? 3 : 2);
profileStream << "_" << "0";
std::string profile = profileStream.str();
UINT flags = ANGLE_COMPILE_OPTIMIZATION_LEVEL;
if (workarounds.skipOptimization)
{
flags = D3DCOMPILE_SKIP_OPTIMIZATION;
}
else if (workarounds.useMaxOptimization)
{
flags = D3DCOMPILE_OPTIMIZATION_LEVEL3;
}
if (gl::DebugAnnotationsActive(/*context=*/nullptr))
{
#ifndef NDEBUG
flags = D3DCOMPILE_SKIP_OPTIMIZATION;
#endif
flags |= D3DCOMPILE_DEBUG;
}
// Sometimes D3DCompile will fail with the default compilation flags for complicated shaders
// when it would otherwise pass with alternative options. Try the default flags first and if
// compilation fails, try some alternatives.
std::vector<CompileConfig> configs;
configs.push_back(CompileConfig(flags, "default"));
configs.push_back(CompileConfig(flags | D3DCOMPILE_AVOID_FLOW_CONTROL, "avoid flow control"));
configs.push_back(CompileConfig(flags | D3DCOMPILE_PREFER_FLOW_CONTROL, "prefer flow control"));
ID3DBlob *binary = nullptr;
std::string debugInfo;
angle::Result error = mCompiler.compileToBinary(context, infoLog, shaderHLSL, profile, configs,
nullptr, &binary, &debugInfo);
ANGLE_TRY(error);
// It's possible that binary is NULL if the compiler failed in all configurations. Set the
// executable to NULL and return GL_NO_ERROR to signify that there was a link error but the
// internal state is still OK.
if (!binary)
{
*outExectuable = nullptr;
return angle::Result::Continue;
}
error = loadExecutable(context, reinterpret_cast<const uint8_t *>(binary->GetBufferPointer()),
binary->GetBufferSize(), type, streamOutVaryings, separatedOutputBuffers,
outExectuable);
SafeRelease(binary);
ANGLE_TRY(error);
if (!debugInfo.empty())
{
(*outExectuable)->appendDebugInfo(debugInfo);
}
return angle::Result::Continue;
}
angle::Result Renderer9::ensureHLSLCompilerInitialized(d3d::Context *context)
{
return mCompiler.ensureInitialized(context);
}
UniformStorageD3D *Renderer9::createUniformStorage(size_t storageSize)
{
return new UniformStorageD3D(storageSize);
}
angle::Result Renderer9::boxFilter(Context9 *context9,
IDirect3DSurface9 *source,
IDirect3DSurface9 *dest)
{
return mBlit->boxFilter(context9, source, dest);
}
D3DPOOL Renderer9::getTexturePool(DWORD usage) const
{
if (mD3d9Ex != nullptr)
{
return D3DPOOL_DEFAULT;
}
else
{
if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET)))
{
return D3DPOOL_MANAGED;
}
}
return D3DPOOL_DEFAULT;
}
angle::Result Renderer9::copyToRenderTarget(const gl::Context *context,
IDirect3DSurface9 *dest,
IDirect3DSurface9 *source,
bool fromManaged)
{
ASSERT(source && dest);
Context9 *context9 = GetImplAs<Context9>(context);
HRESULT result = D3DERR_OUTOFVIDEOMEMORY;
if (fromManaged)
{
D3DSURFACE_DESC desc;
source->GetDesc(&desc);
IDirect3DSurface9 *surf = 0;
result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
D3DPOOL_SYSTEMMEM, &surf, nullptr);
if (SUCCEEDED(result))
{
ANGLE_TRY(Image9::CopyLockableSurfaces(context9, surf, source));
result = mDevice->UpdateSurface(surf, nullptr, dest, nullptr);
SafeRelease(surf);
}
}
else
{
endScene();
result = mDevice->StretchRect(source, nullptr, dest, nullptr, D3DTEXF_NONE);
}
ANGLE_TRY_HR(context9, result, "Failed to blit internal texture");
return angle::Result::Continue;
}
RendererClass Renderer9::getRendererClass() const
{
return RENDERER_D3D9;
}
ImageD3D *Renderer9::createImage()
{
return new Image9(this);
}
ExternalImageSiblingImpl *Renderer9::createExternalImageSibling(const gl::Context *context,
EGLenum target,
EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
{
UNREACHABLE();
return nullptr;
}
angle::Result Renderer9::generateMipmap(const gl::Context *context, ImageD3D *dest, ImageD3D *src)
{
Image9 *src9 = GetAs<Image9>(src);
Image9 *dst9 = GetAs<Image9>(dest);
return Image9::GenerateMipmap(GetImplAs<Context9>(context), dst9, src9);
}
angle::Result Renderer9::generateMipmapUsingD3D(const gl::Context *context,
TextureStorage *storage,
const gl::TextureState &textureState)
{
ANGLE_HR_UNREACHABLE(GetImplAs<Context9>(context));
return angle::Result::Stop;
}
angle::Result Renderer9::copyImage(const gl::Context *context,
ImageD3D *dest,
ImageD3D *source,
const gl::Box &sourceBox,
const gl::Offset &destOffset,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha)
{
Image9 *dest9 = GetAs<Image9>(dest);
Image9 *src9 = GetAs<Image9>(source);
return Image9::CopyImage(context, dest9, src9, sourceBox.toRect(), destOffset, unpackFlipY,
unpackPremultiplyAlpha, unpackUnmultiplyAlpha);
}
TextureStorage *Renderer9::createTextureStorage2D(SwapChainD3D *swapChain, const std::string &label)
{
SwapChain9 *swapChain9 = GetAs<SwapChain9>(swapChain);
return new TextureStorage9_2D(this, swapChain9, label);
}
TextureStorage *Renderer9::createTextureStorageEGLImage(EGLImageD3D *eglImage,
RenderTargetD3D *renderTargetD3D,
const std::string &label)
{
return new TextureStorage9_EGLImage(this, eglImage, GetAs<RenderTarget9>(renderTargetD3D),
label);
}
TextureStorage *Renderer9::createTextureStorageBuffer(
const gl::OffsetBindingPointer<gl::Buffer> &buffer,
GLenum internalFormat,
const std::string &label)
{
UNREACHABLE();
return nullptr;
}
TextureStorage *Renderer9::createTextureStorageExternal(
egl::Stream *stream,
const egl::Stream::GLTextureDescription &desc,
const std::string &label)
{
UNIMPLEMENTED();
return nullptr;
}
TextureStorage *Renderer9::createTextureStorage2D(GLenum internalformat,
BindFlags bindFlags,
GLsizei width,
GLsizei height,
int levels,
const std::string &label,
bool hintLevelZeroOnly)
{
return new TextureStorage9_2D(this, internalformat, bindFlags.renderTarget, width, height,
levels, label);
}
TextureStorage *Renderer9::createTextureStorageCube(GLenum internalformat,
BindFlags bindFlags,
int size,
int levels,
bool hintLevelZeroOnly,
const std::string &label)
{
return new TextureStorage9_Cube(this, internalformat, bindFlags.renderTarget, size, levels,
hintLevelZeroOnly, label);
}
TextureStorage *Renderer9::createTextureStorage3D(GLenum internalformat,
BindFlags bindFlags,
GLsizei width,
GLsizei height,
GLsizei depth,
int levels,
const std::string &label)
{
// 3D textures are not supported by the D3D9 backend.
UNREACHABLE();
return nullptr;
}
TextureStorage *Renderer9::createTextureStorage2DArray(GLenum internalformat,
BindFlags bindFlags,
GLsizei width,
GLsizei height,
GLsizei depth,
int levels,
const std::string &label)
{
// 2D array textures are not supported by the D3D9 backend.
UNREACHABLE();
return nullptr;
}
TextureStorage *Renderer9::createTextureStorage2DMultisample(GLenum internalformat,
GLsizei width,
GLsizei height,
int levels,
int samples,
bool fixedSampleLocations,
const std::string &label)
{
// 2D multisampled textures are not supported by the D3D9 backend.
UNREACHABLE();
return nullptr;
}
TextureStorage *Renderer9::createTextureStorage2DMultisampleArray(GLenum internalformat,
GLsizei width,
GLsizei height,
GLsizei depth,
int levels,
int samples,
bool fixedSampleLocations,
const std::string &label)
{
// 2D multisampled textures are not supported by the D3D9 backend.
UNREACHABLE();
return nullptr;
}
bool Renderer9::getLUID(LUID *adapterLuid) const
{
adapterLuid->HighPart = 0;
adapterLuid->LowPart = 0;
if (mD3d9Ex)
{
mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid);
return true;
}
return false;
}
VertexConversionType Renderer9::getVertexConversionType(angle::FormatID vertexFormatID) const
{
return d3d9::GetVertexFormatInfo(getCapsDeclTypes(), vertexFormatID).conversionType;
}
GLenum Renderer9::getVertexComponentType(angle::FormatID vertexFormatID) const
{
return d3d9::GetVertexFormatInfo(getCapsDeclTypes(), vertexFormatID).componentType;
}
angle::Result Renderer9::getVertexSpaceRequired(const gl::Context *context,
const gl::VertexAttribute &attrib,
const gl::VertexBinding &binding,
size_t count,
GLsizei instances,
GLuint baseInstance,
unsigned int *bytesRequiredOut) const
{
if (!attrib.enabled)
{
*bytesRequiredOut = 16u;
return angle::Result::Continue;
}
angle::FormatID vertexFormatID = gl::GetVertexFormatID(attrib, gl::VertexAttribType::Float);
const d3d9::VertexFormat &d3d9VertexInfo =
d3d9::GetVertexFormatInfo(getCapsDeclTypes(), vertexFormatID);
unsigned int elementCount = 0;
const unsigned int divisor = binding.getDivisor();
if (instances == 0 || divisor == 0)
{
elementCount = static_cast<unsigned int>(count);
}
else
{
// Round up to divisor, if possible
elementCount = UnsignedCeilDivide(static_cast<unsigned int>(instances), divisor);
}
bool check = (d3d9VertexInfo.outputElementSize >
std::numeric_limits<unsigned int>::max() / elementCount);
ANGLE_CHECK(GetImplAs<Context9>(context), !check,
"New vertex buffer size would result in an overflow.", GL_OUT_OF_MEMORY);
*bytesRequiredOut = static_cast<unsigned int>(d3d9VertexInfo.outputElementSize) * elementCount;
return angle::Result::Continue;
}
void Renderer9::generateCaps(gl::Caps *outCaps,
gl::TextureCapsMap *outTextureCaps,
gl::Extensions *outExtensions,
gl::Limitations *outLimitations,
ShPixelLocalStorageOptions *outPLSOptions) const
{
d3d9_gl::GenerateCaps(mD3d9, mDevice, mDeviceType, mAdapter, outCaps, outTextureCaps,
outExtensions, outLimitations);
}
void Renderer9::initializeFeatures(angle::FeaturesD3D *features) const
{
ApplyFeatureOverrides(features, mDisplay->getState().featureOverrides);
if (!mDisplay->getState().featureOverrides.allDisabled)
{
d3d9::InitializeFeatures(features, mAdapterIdentifier.VendorId);
}
}
void Renderer9::initializeFrontendFeatures(angle::FrontendFeatures *features) const
{
ApplyFeatureOverrides(features, mDisplay->getState().featureOverrides);
if (!mDisplay->getState().featureOverrides.allDisabled)
{
d3d9::InitializeFrontendFeatures(features, mAdapterIdentifier.VendorId);
}
}
DeviceImpl *Renderer9::createEGLDevice()
{
return new Device9(mDevice);
}
Renderer9::CurSamplerState::CurSamplerState()
: forceSet(true), baseLevel(std::numeric_limits<size_t>::max()), samplerState()
{}
angle::Result Renderer9::genericDrawElements(const gl::Context *context,
gl::PrimitiveMode mode,
GLsizei count,
gl::DrawElementsType type,
const void *indices,
GLsizei instances)
{
const gl::State &state = context->getState();
ProgramExecutableD3D *executableD3D =
GetImplAs<ProgramExecutableD3D>(state.getProgramExecutable());
ASSERT(executableD3D != nullptr);
bool usesPointSize = executableD3D->usesPointSize();
if (executableD3D->isSamplerMappingDirty())
{
executableD3D->updateSamplerMapping();
}
if (!applyPrimitiveType(mode, count, usesPointSize))
{
return angle::Result::Continue;
}
ANGLE_TRY(updateState(context, mode));
ANGLE_TRY(applyTextures(context));
ANGLE_TRY(applyShaders(context, mode));
if (!skipDraw(state, mode))
{
ANGLE_TRY(drawElementsImpl(context, mode, count, type, indices, instances));
}
return angle::Result::Continue;
}
angle::Result Renderer9::genericDrawArrays(const gl::Context *context,
gl::PrimitiveMode mode,
GLint first,
GLsizei count,
GLsizei instances)
{
const gl::State &state = context->getState();
ProgramExecutableD3D *executableD3D =
GetImplAs<ProgramExecutableD3D>(state.getProgramExecutable());
ASSERT(executableD3D != nullptr);
bool usesPointSize = executableD3D->usesPointSize();
if (executableD3D->isSamplerMappingDirty())
{
executableD3D->updateSamplerMapping();
}
if (!applyPrimitiveType(mode, count, usesPointSize))
{
return angle::Result::Continue;
}
ANGLE_TRY(updateState(context, mode));
ANGLE_TRY(applyVertexBuffer(context, mode, first, count, instances, nullptr));
ANGLE_TRY(applyTextures(context));
ANGLE_TRY(applyShaders(context, mode));
if (!skipDraw(context->getState(), mode))
{
ANGLE_TRY(drawArraysImpl(context, mode, first, count, instances));
}
return angle::Result::Continue;
}
FramebufferImpl *Renderer9::createDefaultFramebuffer(const gl::FramebufferState &state)
{
return new Framebuffer9(state, this);
}
gl::Version Renderer9::getMaxSupportedESVersion() const
{
return gl::Version(2, 0);
}
gl::Version Renderer9::getMaxConformantESVersion() const
{
return gl::Version(2, 0);
}
angle::Result Renderer9::clearRenderTarget(const gl::Context *context,
RenderTargetD3D *renderTarget,
const gl::ColorF &clearColorValue,
const float clearDepthValue,
const unsigned int clearStencilValue)
{
D3DCOLOR color =
D3DCOLOR_ARGB(gl::unorm<8>(clearColorValue.alpha), gl::unorm<8>(clearColorValue.red),
gl::unorm<8>(clearColorValue.green), gl::unorm<8>(clearColorValue.blue));
float depth = clearDepthValue;
DWORD stencil = clearStencilValue & 0x000000FF;
unsigned int renderTargetSerial = renderTarget->getSerial();
RenderTarget9 *renderTarget9 = GetAs<RenderTarget9>(renderTarget);
IDirect3DSurface9 *renderTargetSurface = renderTarget9->getSurface();
ASSERT(renderTargetSurface);
DWORD dxClearFlags = 0;
const gl::InternalFormat &internalFormatInfo =
gl::GetSizedInternalFormatInfo(renderTarget->getInternalFormat());
if (internalFormatInfo.depthBits > 0 || internalFormatInfo.stencilBits > 0)
{
dxClearFlags = D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL;
if (mAppliedDepthStencilSerial != renderTargetSerial)
{
mDevice->SetDepthStencilSurface(renderTargetSurface);
}
}
else
{
dxClearFlags = D3DCLEAR_TARGET;
if (mAppliedRenderTargetSerial != renderTargetSerial)
{
mDevice->SetRenderTarget(0, renderTargetSurface);
}
}
SafeRelease(renderTargetSurface);
D3DVIEWPORT9 viewport;
viewport.X = 0;
viewport.Y = 0;
viewport.Width = renderTarget->getWidth();
viewport.Height = renderTarget->getHeight();
mDevice->SetViewport(&viewport);
mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
mDevice->Clear(0, nullptr, dxClearFlags, color, depth, stencil);
markAllStateDirty();
return angle::Result::Continue;
}
bool Renderer9::canSelectViewInVertexShader() const
{
return false;
}
// 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 Renderer9::applyTextures(const gl::Context *context, gl::ShaderType shaderType)
{
const auto &glState = context->getState();
const auto &caps = context->getCaps();
ProgramExecutableD3D *executableD3D =
GetImplAs<ProgramExecutableD3D>(glState.getProgramExecutable());
ASSERT(!executableD3D->isSamplerMappingDirty());
// TODO(jmadill): Use the Program's sampler bindings.
const gl::ActiveTexturesCache &activeTextures = glState.getActiveTexturesCache();
const gl::RangeUI samplerRange = executableD3D->getUsedSamplerRange(shaderType);
for (unsigned int samplerIndex = samplerRange.low(); samplerIndex < samplerRange.high();
samplerIndex++)
{
GLint textureUnit = executableD3D->getSamplerMapping(shaderType, samplerIndex, caps);
ASSERT(textureUnit != -1);
gl::Texture *texture = activeTextures[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(setTexture(context, shaderType, samplerIndex, texture));
}
else
{
gl::TextureType textureType =
executableD3D->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(getIncompleteTexture(context, textureType, &incompleteTexture));
ANGLE_TRY(setSamplerState(context, shaderType, samplerIndex, incompleteTexture,
incompleteTexture->getSamplerState()));
ANGLE_TRY(setTexture(context, shaderType, samplerIndex, incompleteTexture));
}
}
// Set all the remaining textures to NULL
int samplerCount = (shaderType == gl::ShaderType::Fragment)
? caps.maxShaderTextureImageUnits[gl::ShaderType::Fragment]
: caps.maxShaderTextureImageUnits[gl::ShaderType::Vertex];
// TODO(jmadill): faster way?
for (int samplerIndex = samplerRange.high(); samplerIndex < samplerCount; samplerIndex++)
{
ANGLE_TRY(setTexture(context, shaderType, samplerIndex, nullptr));
}
return angle::Result::Continue;
}
angle::Result Renderer9::applyTextures(const gl::Context *context)
{
ANGLE_TRY(applyTextures(context, gl::ShaderType::Vertex));
ANGLE_TRY(applyTextures(context, gl::ShaderType::Fragment));
return angle::Result::Continue;
}
angle::Result Renderer9::getIncompleteTexture(const gl::Context *context,
gl::TextureType type,
gl::Texture **textureOut)
{
return GetImplAs<Context9>(context)->getIncompleteTexture(context, type, textureOut);
}
angle::Result Renderer9::ensureVertexDataManagerInitialized(const gl::Context *context)
{
if (!mVertexDataManager)
{
mVertexDataManager = new VertexDataManager(this);
ANGLE_TRY(mVertexDataManager->initialize(context));
}
return angle::Result::Continue;
}
std::string Renderer9::getVendorString() const
{
return GetVendorString(getVendorId());
}
std::string Renderer9::getVersionString(bool includeFullVersion) const
{
std::ostringstream versionString;
std::string driverName(mAdapterIdentifier.Driver);
if (!driverName.empty())
{
versionString << mAdapterIdentifier.Driver;
}
else
{
versionString << "D3D9";
}
if (includeFullVersion)
{
versionString << " -";
versionString << GetDriverVersionString(mAdapterIdentifier.DriverVersion);
}
return versionString.str();
}
RendererD3D *CreateRenderer9(egl::Display *display)
{
return new Renderer9(display);
}
} // namespace rx