DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (cdf352f02ac4)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507
use std::borrow::Borrow;
use std::collections::{BTreeMap, VecDeque};
use std::ops::Range;
use std::{ffi, mem, ptr, slice};

use spirv_cross::{hlsl, spirv, ErrorCode as SpirvErrorCode};
use smallvec::SmallVec;

use winapi::shared::minwindef::{FALSE, TRUE, UINT};
use winapi::shared::{dxgi, dxgi1_2, dxgi1_4, dxgiformat, dxgitype, windef, winerror};
use winapi::um::{d3d12, d3dcompiler, synchapi, winbase, winnt};
use winapi::Interface;

use auxil::spirv_cross_specialize_ast;
use hal::format::Aspects;
use hal::memory::Requirements;
use hal::pool::CommandPoolCreateFlags;
use hal::pso::VertexInputRate;
use hal::queue::{CommandQueue as _, QueueFamilyId};
use hal::range::RangeArg;
use hal::{
    self,
    buffer,
    device as d,
    format,
    image,
    memory,
    pass,
    pso,
    query,
    window as w,
};

use pool::{CommandPool, CommandPoolAllocator};
use range_alloc::RangeAllocator;
use root_constants::RootConstant;
use {
    conv,
    command as cmd,
    descriptors_cpu,
    resource as r,
    root_constants,
    window::{Surface, Swapchain},
    Backend as B,
    Device,
    MemoryGroup,
    MAX_VERTEX_BUFFERS,
    NUM_HEAP_PROPERTIES,
    QUEUE_FAMILIES,
};
use native::{
    PipelineStateSubobject,
    Subobject,
};

// Register space used for root constants.
const ROOT_CONSTANT_SPACE: u32 = 0;

const MEM_TYPE_MASK: u64 = 0x7;
const MEM_TYPE_SHIFT: u64 = 3;

const MEM_TYPE_UNIVERSAL_SHIFT: u64 = MEM_TYPE_SHIFT * MemoryGroup::Universal as u64;
const MEM_TYPE_BUFFER_SHIFT: u64 = MEM_TYPE_SHIFT * MemoryGroup::BufferOnly as u64;
const MEM_TYPE_IMAGE_SHIFT: u64 = MEM_TYPE_SHIFT * MemoryGroup::ImageOnly as u64;
const MEM_TYPE_TARGET_SHIFT: u64 = MEM_TYPE_SHIFT * MemoryGroup::TargetOnly as u64;

pub const IDENTITY_MAPPING: UINT = 0x1688; // D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING

/// Emit error during shader module creation. Used if we don't expect an error
/// but might panic due to an exception in SPIRV-Cross.
fn gen_unexpected_error(err: SpirvErrorCode) -> d::ShaderError {
    let msg = match err {
        SpirvErrorCode::CompilationError(msg) => msg,
        SpirvErrorCode::Unhandled => "Unexpected error".into(),
    };
    d::ShaderError::CompilationFailed(msg)
}

/// Emit error during shader module creation. Used if we execute an query command.
fn gen_query_error(err: SpirvErrorCode) -> d::ShaderError {
    let msg = match err {
        SpirvErrorCode::CompilationError(msg) => msg,
        SpirvErrorCode::Unhandled => "Unknown query error".into(),
    };
    d::ShaderError::CompilationFailed(msg)
}

#[derive(Clone, Debug)]
pub(crate) struct ViewInfo {
    pub(crate) resource: native::Resource,
    pub(crate) kind: image::Kind,
    pub(crate) caps: image::ViewCapabilities,
    pub(crate) view_kind: image::ViewKind,
    pub(crate) format: dxgiformat::DXGI_FORMAT,
    pub(crate) component_mapping: UINT,
    pub(crate) range: image::SubresourceRange,
}

pub(crate) enum CommandSignature {
    Draw,
    DrawIndexed,
    Dispatch,
}

/// Compile a single shader entry point from a HLSL text shader
pub(crate) fn compile_shader(
    stage: pso::Stage,
    shader_model: hlsl::ShaderModel,
    entry: &str,
    code: &[u8],
) -> Result<native::Blob, d::ShaderError> {
    let stage_to_str = |stage, shader_model| {
        let stage = match stage {
            pso::Stage::Vertex => "vs",
            pso::Stage::Fragment => "ps",
            pso::Stage::Compute => "cs",
            _ => unimplemented!(),
        };

        let model = match shader_model {
            hlsl::ShaderModel::V5_0 => "5_0",
            hlsl::ShaderModel::V5_1 => "5_1",
            hlsl::ShaderModel::V6_0 => "6_0",
            _ => unimplemented!(),
        };

        format!("{}_{}\0", stage, model)
    };

    let mut shader_data = native::Blob::null();
    let mut error = native::Blob::null();
    let entry = ffi::CString::new(entry).unwrap();
    let hr = unsafe {
        d3dcompiler::D3DCompile(
            code.as_ptr() as *const _,
            code.len(),
            ptr::null(),
            ptr::null(),
            ptr::null_mut(),
            entry.as_ptr() as *const _,
            stage_to_str(stage, shader_model).as_ptr() as *const i8,
            1,
            0,
            shader_data.mut_void() as *mut *mut _,
            error.mut_void() as *mut *mut _,
        )
    };
    if !winerror::SUCCEEDED(hr) {
        error!("D3DCompile error {:x}", hr);
        let message = unsafe {
            let pointer = error.GetBufferPointer();
            let size = error.GetBufferSize();
            let slice = slice::from_raw_parts(pointer as *const u8, size as usize);
            String::from_utf8_lossy(slice).into_owned()
        };
        unsafe {
            error.destroy();
        }
        Err(d::ShaderError::CompilationFailed(message))
    } else {
        Ok(shader_data)
    }
}

#[repr(C)]
struct GraphicsPipelineStateSubobjectStream {
    root_signature: PipelineStateSubobject<*mut d3d12::ID3D12RootSignature>,
    vs: PipelineStateSubobject<d3d12::D3D12_SHADER_BYTECODE>,
    ps: PipelineStateSubobject<d3d12::D3D12_SHADER_BYTECODE>,
    ds: PipelineStateSubobject<d3d12::D3D12_SHADER_BYTECODE>,
    hs: PipelineStateSubobject<d3d12::D3D12_SHADER_BYTECODE>,
    gs: PipelineStateSubobject<d3d12::D3D12_SHADER_BYTECODE>,
    stream_output: PipelineStateSubobject<d3d12::D3D12_STREAM_OUTPUT_DESC>,
    blend: PipelineStateSubobject<d3d12::D3D12_BLEND_DESC>,
    sample_mask: PipelineStateSubobject<UINT>,
    rasterizer: PipelineStateSubobject<d3d12::D3D12_RASTERIZER_DESC>,
    depth_stencil: PipelineStateSubobject<d3d12::D3D12_DEPTH_STENCIL_DESC1>,
    input_layout: PipelineStateSubobject<d3d12::D3D12_INPUT_LAYOUT_DESC>,
    ib_strip_cut_value: PipelineStateSubobject<d3d12::D3D12_INDEX_BUFFER_STRIP_CUT_VALUE>,
    primitive_topology: PipelineStateSubobject<d3d12::D3D12_PRIMITIVE_TOPOLOGY_TYPE>,
    render_target_formats: PipelineStateSubobject<d3d12::D3D12_RT_FORMAT_ARRAY>,
    depth_stencil_format: PipelineStateSubobject<dxgiformat::DXGI_FORMAT>,
    sample_desc: PipelineStateSubobject<dxgitype::DXGI_SAMPLE_DESC>,
    node_mask: PipelineStateSubobject<UINT>,
    cached_pso: PipelineStateSubobject<d3d12::D3D12_CACHED_PIPELINE_STATE>,
    flags: PipelineStateSubobject<d3d12::D3D12_PIPELINE_STATE_FLAGS>,
}

impl GraphicsPipelineStateSubobjectStream {
    fn new(
        pso_desc: &d3d12::D3D12_GRAPHICS_PIPELINE_STATE_DESC,
        depth_bounds_test_enable: bool,
    ) -> Self {
        GraphicsPipelineStateSubobjectStream {
            root_signature: PipelineStateSubobject::new(
                Subobject::RootSignature,
                pso_desc.pRootSignature,
            ),
            vs: PipelineStateSubobject::new(Subobject::VS, pso_desc.VS),
            ps: PipelineStateSubobject::new(Subobject::PS, pso_desc.PS),
            ds: PipelineStateSubobject::new(Subobject::DS, pso_desc.DS),
            hs: PipelineStateSubobject::new(Subobject::HS, pso_desc.HS),
            gs: PipelineStateSubobject::new(Subobject::GS, pso_desc.GS),
            stream_output: PipelineStateSubobject::new(
                Subobject::StreamOutput,
                pso_desc.StreamOutput,
            ),
            blend: PipelineStateSubobject::new(Subobject::Blend, pso_desc.BlendState),
            sample_mask: PipelineStateSubobject::new(Subobject::SampleMask, pso_desc.SampleMask),
            rasterizer: PipelineStateSubobject::new(
                Subobject::Rasterizer,
                pso_desc.RasterizerState,
            ),
            depth_stencil: PipelineStateSubobject::new(
                Subobject::DepthStencil1,
                d3d12::D3D12_DEPTH_STENCIL_DESC1 {
                    DepthEnable: pso_desc.DepthStencilState.DepthEnable,
                    DepthWriteMask: pso_desc.DepthStencilState.DepthWriteMask,
                    DepthFunc: pso_desc.DepthStencilState.DepthFunc,
                    StencilEnable: pso_desc.DepthStencilState.StencilEnable,
                    StencilReadMask: pso_desc.DepthStencilState.StencilReadMask,
                    StencilWriteMask: pso_desc.DepthStencilState.StencilWriteMask,
                    FrontFace: pso_desc.DepthStencilState.FrontFace,
                    BackFace: pso_desc.DepthStencilState.BackFace,
                    DepthBoundsTestEnable: depth_bounds_test_enable as _,
                },
            ),
            input_layout: PipelineStateSubobject::new(Subobject::InputLayout, pso_desc.InputLayout),
            ib_strip_cut_value: PipelineStateSubobject::new(
                Subobject::IBStripCut,
                pso_desc.IBStripCutValue,
            ),
            primitive_topology: PipelineStateSubobject::new(
                Subobject::PrimitiveTopology,
                pso_desc.PrimitiveTopologyType,
            ),
            render_target_formats: PipelineStateSubobject::new(
                Subobject::RTFormats,
                d3d12::D3D12_RT_FORMAT_ARRAY {
                    RTFormats: pso_desc.RTVFormats,
                    NumRenderTargets: pso_desc.NumRenderTargets,
                },
            ),
            depth_stencil_format: PipelineStateSubobject::new(
                Subobject::DSFormat,
                pso_desc.DSVFormat,
            ),
            sample_desc: PipelineStateSubobject::new(Subobject::SampleDesc, pso_desc.SampleDesc),
            node_mask: PipelineStateSubobject::new(Subobject::NodeMask, pso_desc.NodeMask),
            cached_pso: PipelineStateSubobject::new(Subobject::CachedPSO, pso_desc.CachedPSO),
            flags: PipelineStateSubobject::new(Subobject::Flags, pso_desc.Flags),
        }
    }
}

impl Device {
    fn parse_spirv(raw_data: &[u32]) -> Result<spirv::Ast<hlsl::Target>, d::ShaderError> {
        let module = spirv::Module::from_words(raw_data);

        spirv::Ast::parse(&module).map_err(|err| {
            let msg = match err {
                SpirvErrorCode::CompilationError(msg) => msg,
                SpirvErrorCode::Unhandled => "Unknown parsing error".into(),
            };
            d::ShaderError::CompilationFailed(msg)
        })
    }

    fn patch_spirv_resources(
        ast: &mut spirv::Ast<hlsl::Target>,
        layout: Option<&r::PipelineLayout>,
    ) -> Result<(), d::ShaderError> {
        // Move the descriptor sets away to yield for the root constants at "space0".
        let space_offset = match layout {
            Some(layout) if !layout.constants.is_empty() => 1,
            _ => return Ok(()),
        };

        let shader_resources = ast.get_shader_resources().map_err(gen_query_error)?;
        for image in &shader_resources.separate_images {
            let set = ast
                .get_decoration(image.id, spirv::Decoration::DescriptorSet)
                .map_err(gen_query_error)?;
            ast.set_decoration(
                image.id,
                spirv::Decoration::DescriptorSet,
                space_offset + set,
            )
            .map_err(gen_unexpected_error)?;
        }

        for uniform_buffer in &shader_resources.uniform_buffers {
            let set = ast
                .get_decoration(uniform_buffer.id, spirv::Decoration::DescriptorSet)
                .map_err(gen_query_error)?;
            ast.set_decoration(
                uniform_buffer.id,
                spirv::Decoration::DescriptorSet,
                space_offset + set,
            )
            .map_err(gen_unexpected_error)?;
        }

        for storage_buffer in &shader_resources.storage_buffers {
            let set = ast
                .get_decoration(storage_buffer.id, spirv::Decoration::DescriptorSet)
                .map_err(gen_query_error)?;
            ast.set_decoration(
                storage_buffer.id,
                spirv::Decoration::DescriptorSet,
                space_offset + set,
            )
            .map_err(gen_unexpected_error)?;
        }

        for image in &shader_resources.storage_images {
            let set = ast
                .get_decoration(image.id, spirv::Decoration::DescriptorSet)
                .map_err(gen_query_error)?;
            ast.set_decoration(
                image.id,
                spirv::Decoration::DescriptorSet,
                space_offset + set,
            )
            .map_err(gen_unexpected_error)?;
        }

        for sampler in &shader_resources.separate_samplers {
            let set = ast
                .get_decoration(sampler.id, spirv::Decoration::DescriptorSet)
                .map_err(gen_query_error)?;
            ast.set_decoration(
                sampler.id,
                spirv::Decoration::DescriptorSet,
                space_offset + set,
            )
            .map_err(gen_unexpected_error)?;
        }

        for image in &shader_resources.sampled_images {
            let set = ast
                .get_decoration(image.id, spirv::Decoration::DescriptorSet)
                .map_err(gen_query_error)?;
            ast.set_decoration(
                image.id,
                spirv::Decoration::DescriptorSet,
                space_offset + set,
            )
            .map_err(gen_unexpected_error)?;
        }

        for input in &shader_resources.subpass_inputs {
            let set = ast
                .get_decoration(input.id, spirv::Decoration::DescriptorSet)
                .map_err(gen_query_error)?;
            ast.set_decoration(
                input.id,
                spirv::Decoration::DescriptorSet,
                space_offset + set,
            )
            .map_err(gen_unexpected_error)?;
        }

        // TODO: other resources

        Ok(())
    }

    fn translate_spirv(
        ast: &mut spirv::Ast<hlsl::Target>,
        shader_model: hlsl::ShaderModel,
        layout: &r::PipelineLayout,
        stage: pso::Stage,
    ) -> Result<String, d::ShaderError> {
        let mut compile_options = hlsl::CompilerOptions::default();
        compile_options.shader_model = shader_model;
        compile_options.vertex.invert_y = true;

        let stage_flag = stage.into();
        let root_constant_layout = layout
            .constants
            .iter()
            .filter_map(|constant| {
                if constant.stages.contains(stage_flag) {
                    Some(hlsl::RootConstant {
                        start: constant.range.start * 4,
                        end: constant.range.end * 4,
                        binding: constant.range.start,
                        space: 0,
                    })
                } else {
                    None
                }
            })
            .collect();
        ast.set_compiler_options(&compile_options)
            .map_err(gen_unexpected_error)?;
        ast.set_root_constant_layout(root_constant_layout)
            .map_err(gen_unexpected_error)?;
        ast.compile().map_err(|err| {
            let msg = match err {
                SpirvErrorCode::CompilationError(msg) => msg,
                SpirvErrorCode::Unhandled => "Unknown compile error".into(),
            };
            d::ShaderError::CompilationFailed(msg)
        })
    }

    // Extract entry point from shader module on pipeline creation.
    // Returns compiled shader blob and bool to indicate if the shader should be
    // destroyed after pipeline creation
    fn extract_entry_point(
        stage: pso::Stage,
        source: &pso::EntryPoint<B>,
        layout: &r::PipelineLayout,
    ) -> Result<(native::Blob, bool), d::ShaderError> {
        match *source.module {
            r::ShaderModule::Compiled(ref shaders) => {
                // TODO: do we need to check for specialization constants?
                // Use precompiled shader, ignore specialization or layout.
                shaders
                    .get(source.entry)
                    .map(|src| (*src, false))
                    .ok_or(d::ShaderError::MissingEntryPoint(source.entry.into()))
            }
            r::ShaderModule::Spirv(ref raw_data) => {
                let mut ast = Self::parse_spirv(raw_data)?;
                spirv_cross_specialize_ast(&mut ast, &source.specialization)?;
                Self::patch_spirv_resources(&mut ast, Some(layout))?;

                let shader_model = hlsl::ShaderModel::V5_1;
                let shader_code = Self::translate_spirv(&mut ast, shader_model, layout, stage)?;
                debug!("SPIRV-Cross generated shader:\n{}", shader_code);

                let real_name = ast
                    .get_cleansed_entry_point_name(source.entry, conv::map_stage(stage))
                    .map_err(gen_query_error)?;
                // TODO: opt: don't query *all* entry points.
                let entry_points = ast.get_entry_points().map_err(gen_query_error)?;
                entry_points
                    .iter()
                    .find(|entry_point| entry_point.name == real_name)
                    .ok_or(d::ShaderError::MissingEntryPoint(source.entry.into()))
                    .and_then(|entry_point| {
                        let stage = conv::map_execution_model(entry_point.execution_model);
                        let shader = compile_shader(
                            stage,
                            shader_model,
                            &entry_point.name,
                            shader_code.as_bytes(),
                        )?;
                        Ok((shader, true))
                    })
            }
        }
    }

    /// Create a shader module from HLSL with a single entry point
    pub fn create_shader_module_from_source(
        &self,
        stage: pso::Stage,
        hlsl_entry: &str,
        entry_point: &str,
        code: &[u8],
    ) -> Result<r::ShaderModule, d::ShaderError> {
        let mut shader_map = BTreeMap::new();
        let blob = compile_shader(stage, hlsl::ShaderModel::V5_1, hlsl_entry, code)?;
        shader_map.insert(entry_point.into(), blob);
        Ok(r::ShaderModule::Compiled(shader_map))
    }

    pub(crate) fn create_command_signature(
        device: native::Device,
        ty: CommandSignature,
    ) -> native::CommandSignature {
        let (arg, stride) = match ty {
            CommandSignature::Draw => (native::IndirectArgument::draw(), 16),
            CommandSignature::DrawIndexed => (native::IndirectArgument::draw_indexed(), 20),
            CommandSignature::Dispatch => (native::IndirectArgument::dispatch(), 12),
        };

        let (signature, hr) =
            device.create_command_signature(native::RootSignature::null(), &[arg], stride, 0);

        if !winerror::SUCCEEDED(hr) {
            error!("error on command signature creation: {:x}", hr);
        }
        signature
    }

    pub(crate) fn create_descriptor_heap_impl(
        device: native::Device,
        heap_type: native::DescriptorHeapType,
        shader_visible: bool,
        capacity: usize,
    ) -> r::DescriptorHeap {
        assert_ne!(capacity, 0);

        let (heap, _hr) = device.create_descriptor_heap(
            capacity as _,
            heap_type,
            if shader_visible {
                native::DescriptorHeapFlags::SHADER_VISIBLE
            } else {
                native::DescriptorHeapFlags::empty()
            },
            0,
        );

        let descriptor_size = device.get_descriptor_increment_size(heap_type);
        let cpu_handle = heap.start_cpu_descriptor();
        let gpu_handle = heap.start_gpu_descriptor();

        let range_allocator = RangeAllocator::new(0 .. (capacity as u64));

        r::DescriptorHeap {
            raw: heap,
            handle_size: descriptor_size as _,
            total_handles: capacity as _,
            start: r::DualHandle {
                cpu: cpu_handle,
                gpu: gpu_handle,
                size: 0,
            },
            range_allocator,
        }
    }

    pub(crate) fn view_image_as_render_target_impl(
        device: native::Device,
        handle: d3d12::D3D12_CPU_DESCRIPTOR_HANDLE,
        info: ViewInfo,
    ) -> Result<(), image::ViewError> {
        #![allow(non_snake_case)]

        let mut desc = d3d12::D3D12_RENDER_TARGET_VIEW_DESC {
            Format: info.format,
            ViewDimension: 0,
            u: unsafe { mem::zeroed() },
        };

        let MipSlice = info.range.levels.start as _;
        let FirstArraySlice = info.range.layers.start as _;
        let ArraySize = (info.range.layers.end - info.range.layers.start) as _;
        let is_msaa = info.kind.num_samples() > 1;
        if info.range.levels.start + 1 != info.range.levels.end {
            return Err(image::ViewError::Level(info.range.levels.start));
        }
        if info.range.layers.end > info.kind.num_layers() {
            return Err(image::ViewError::Layer(image::LayerError::OutOfBounds(
                info.range.layers,
            )));
        }

        match info.view_kind {
            image::ViewKind::D1 => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_RTV_DIMENSION_TEXTURE1D;
                *unsafe { desc.u.Texture1D_mut() } = d3d12::D3D12_TEX1D_RTV { MipSlice }
            }
            image::ViewKind::D1Array => {
                desc.ViewDimension = d3d12::D3D12_RTV_DIMENSION_TEXTURE1DARRAY;
                *unsafe { desc.u.Texture1DArray_mut() } = d3d12::D3D12_TEX1D_ARRAY_RTV {
                    MipSlice,
                    FirstArraySlice,
                    ArraySize,
                }
            }
            image::ViewKind::D2 if is_msaa => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_RTV_DIMENSION_TEXTURE2DMS;
                *unsafe { desc.u.Texture2DMS_mut() } = d3d12::D3D12_TEX2DMS_RTV {
                    UnusedField_NothingToDefine: 0,
                }
            }
            image::ViewKind::D2 => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_RTV_DIMENSION_TEXTURE2D;
                *unsafe { desc.u.Texture2D_mut() } = d3d12::D3D12_TEX2D_RTV {
                    MipSlice,
                    PlaneSlice: 0, //TODO
                }
            }
            image::ViewKind::D2Array if is_msaa => {
                desc.ViewDimension = d3d12::D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY;
                *unsafe { desc.u.Texture2DMSArray_mut() } = d3d12::D3D12_TEX2DMS_ARRAY_RTV {
                    FirstArraySlice,
                    ArraySize,
                }
            }
            image::ViewKind::D2Array => {
                desc.ViewDimension = d3d12::D3D12_RTV_DIMENSION_TEXTURE2DARRAY;
                *unsafe { desc.u.Texture2DArray_mut() } = d3d12::D3D12_TEX2D_ARRAY_RTV {
                    MipSlice,
                    FirstArraySlice,
                    ArraySize,
                    PlaneSlice: 0, //TODO
                }
            }
            image::ViewKind::D3 => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_RTV_DIMENSION_TEXTURE3D;
                *unsafe { desc.u.Texture3D_mut() } = d3d12::D3D12_TEX3D_RTV {
                    MipSlice,
                    FirstWSlice: 0,
                    WSize: info.kind.extent().depth as _,
                }
            }
            image::ViewKind::Cube | image::ViewKind::CubeArray => {
                desc.ViewDimension = d3d12::D3D12_RTV_DIMENSION_TEXTURE2DARRAY;
                //TODO: double-check if any *6 are needed
                *unsafe { desc.u.Texture2DArray_mut() } = d3d12::D3D12_TEX2D_ARRAY_RTV {
                    MipSlice,
                    FirstArraySlice,
                    ArraySize,
                    PlaneSlice: 0, //TODO
                }
            }
        };

        unsafe {
            device.CreateRenderTargetView(info.resource.as_mut_ptr(), &desc, handle);
        }

        Ok(())
    }

    fn view_image_as_render_target(
        &self,
        info: ViewInfo,
    ) -> Result<d3d12::D3D12_CPU_DESCRIPTOR_HANDLE, image::ViewError> {
        let handle = self.rtv_pool.lock().unwrap().alloc_handle();
        Self::view_image_as_render_target_impl(self.raw, handle, info).map(|_| handle)
    }

    pub(crate) fn view_image_as_depth_stencil_impl(
        device: native::Device,
        handle: d3d12::D3D12_CPU_DESCRIPTOR_HANDLE,
        info: ViewInfo,
    ) -> Result<(), image::ViewError> {
        #![allow(non_snake_case)]

        let mut desc = d3d12::D3D12_DEPTH_STENCIL_VIEW_DESC {
            Format: info.format,
            ViewDimension: 0,
            Flags: 0,
            u: unsafe { mem::zeroed() },
        };

        let MipSlice = info.range.levels.start as _;
        let FirstArraySlice = info.range.layers.start as _;
        let ArraySize = (info.range.layers.end - info.range.layers.start) as _;
        let is_msaa = info.kind.num_samples() > 1;
        if info.range.levels.start + 1 != info.range.levels.end {
            return Err(image::ViewError::Level(info.range.levels.start));
        }
        if info.range.layers.end > info.kind.num_layers() {
            return Err(image::ViewError::Layer(image::LayerError::OutOfBounds(
                info.range.layers,
            )));
        }

        match info.view_kind {
            image::ViewKind::D1 => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_DSV_DIMENSION_TEXTURE1D;
                *unsafe { desc.u.Texture1D_mut() } = d3d12::D3D12_TEX1D_DSV { MipSlice }
            }
            image::ViewKind::D1Array => {
                desc.ViewDimension = d3d12::D3D12_DSV_DIMENSION_TEXTURE1DARRAY;
                *unsafe { desc.u.Texture1DArray_mut() } = d3d12::D3D12_TEX1D_ARRAY_DSV {
                    MipSlice,
                    FirstArraySlice,
                    ArraySize,
                }
            }
            image::ViewKind::D2 if is_msaa => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_DSV_DIMENSION_TEXTURE2DMS;
                *unsafe { desc.u.Texture2DMS_mut() } = d3d12::D3D12_TEX2DMS_DSV {
                    UnusedField_NothingToDefine: 0,
                }
            }
            image::ViewKind::D2 => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_DSV_DIMENSION_TEXTURE2D;
                *unsafe { desc.u.Texture2D_mut() } = d3d12::D3D12_TEX2D_DSV { MipSlice }
            }
            image::ViewKind::D2Array if is_msaa => {
                desc.ViewDimension = d3d12::D3D12_DSV_DIMENSION_TEXTURE2DMSARRAY;
                *unsafe { desc.u.Texture2DMSArray_mut() } = d3d12::D3D12_TEX2DMS_ARRAY_DSV {
                    FirstArraySlice,
                    ArraySize,
                }
            }
            image::ViewKind::D2Array => {
                desc.ViewDimension = d3d12::D3D12_DSV_DIMENSION_TEXTURE2DARRAY;
                *unsafe { desc.u.Texture2DArray_mut() } = d3d12::D3D12_TEX2D_ARRAY_DSV {
                    MipSlice,
                    FirstArraySlice,
                    ArraySize,
                }
            }
            image::ViewKind::D3 | image::ViewKind::Cube | image::ViewKind::CubeArray => unimplemented!(),
        };

        unsafe {
            device.CreateDepthStencilView(info.resource.as_mut_ptr(), &desc, handle);
        }

        Ok(())
    }

    fn view_image_as_depth_stencil(
        &self,
        info: ViewInfo,
    ) -> Result<d3d12::D3D12_CPU_DESCRIPTOR_HANDLE, image::ViewError> {
        let handle = self.dsv_pool.lock().unwrap().alloc_handle();
        Self::view_image_as_depth_stencil_impl(self.raw, handle, info).map(|_| handle)
    }

    pub(crate) fn build_image_as_shader_resource_desc(
        info: &ViewInfo,
    ) -> Result<d3d12::D3D12_SHADER_RESOURCE_VIEW_DESC, image::ViewError> {
        #![allow(non_snake_case)]

        let mut desc = d3d12::D3D12_SHADER_RESOURCE_VIEW_DESC {
            Format: info.format,
            ViewDimension: 0,
            Shader4ComponentMapping: info.component_mapping,
            u: unsafe { mem::zeroed() },
        };

        let MostDetailedMip = info.range.levels.start as _;
        let MipLevels = (info.range.levels.end - info.range.levels.start) as _;
        let FirstArraySlice = info.range.layers.start as _;
        let ArraySize = (info.range.layers.end - info.range.layers.start) as _;

        if info.range.layers.end > info.kind.num_layers() {
            return Err(image::ViewError::Layer(image::LayerError::OutOfBounds(
                info.range.layers.clone(),
            )));
        }
        let is_msaa = info.kind.num_samples() > 1;
        let is_cube = info.caps.contains(image::ViewCapabilities::KIND_CUBE);

        match info.view_kind {
            image::ViewKind::D1 => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_SRV_DIMENSION_TEXTURE1D;
                *unsafe { desc.u.Texture1D_mut() } = d3d12::D3D12_TEX1D_SRV {
                    MostDetailedMip,
                    MipLevels,
                    ResourceMinLODClamp: 0.0,
                }
            }
            image::ViewKind::D1Array => {
                desc.ViewDimension = d3d12::D3D12_SRV_DIMENSION_TEXTURE1DARRAY;
                *unsafe { desc.u.Texture1DArray_mut() } = d3d12::D3D12_TEX1D_ARRAY_SRV {
                    MostDetailedMip,
                    MipLevels,
                    FirstArraySlice,
                    ArraySize,
                    ResourceMinLODClamp: 0.0,
                }
            }
            image::ViewKind::D2 if is_msaa => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_SRV_DIMENSION_TEXTURE2DMS;
                *unsafe { desc.u.Texture2DMS_mut() } = d3d12::D3D12_TEX2DMS_SRV {
                    UnusedField_NothingToDefine: 0,
                }
            }
            image::ViewKind::D2 => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_SRV_DIMENSION_TEXTURE2D;
                *unsafe { desc.u.Texture2D_mut() } = d3d12::D3D12_TEX2D_SRV {
                    MostDetailedMip,
                    MipLevels,
                    PlaneSlice: 0, //TODO
                    ResourceMinLODClamp: 0.0,
                }
            }
            image::ViewKind::D2Array if is_msaa => {
                desc.ViewDimension = d3d12::D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY;
                *unsafe { desc.u.Texture2DMSArray_mut() } = d3d12::D3D12_TEX2DMS_ARRAY_SRV {
                    FirstArraySlice,
                    ArraySize,
                }
            }
            image::ViewKind::D2Array => {
                desc.ViewDimension = d3d12::D3D12_SRV_DIMENSION_TEXTURE2DARRAY;
                *unsafe { desc.u.Texture2DArray_mut() } = d3d12::D3D12_TEX2D_ARRAY_SRV {
                    MostDetailedMip,
                    MipLevels,
                    FirstArraySlice,
                    ArraySize,
                    PlaneSlice: 0, //TODO
                    ResourceMinLODClamp: 0.0,
                }
            }
            image::ViewKind::D3 => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_SRV_DIMENSION_TEXTURE3D;
                *unsafe { desc.u.Texture3D_mut() } = d3d12::D3D12_TEX3D_SRV {
                    MostDetailedMip,
                    MipLevels,
                    ResourceMinLODClamp: 0.0,
                }
            }
            image::ViewKind::Cube if is_cube => {
                desc.ViewDimension = d3d12::D3D12_SRV_DIMENSION_TEXTURECUBE;
                *unsafe { desc.u.TextureCube_mut() } = d3d12::D3D12_TEXCUBE_SRV {
                    MostDetailedMip,
                    MipLevels,
                    ResourceMinLODClamp: 0.0,
                }
            }
            image::ViewKind::CubeArray if is_cube => {
                assert_eq!(0, ArraySize % 6);
                desc.ViewDimension = d3d12::D3D12_SRV_DIMENSION_TEXTURECUBEARRAY;
                *unsafe { desc.u.TextureCubeArray_mut() } = d3d12::D3D12_TEXCUBE_ARRAY_SRV {
                    MostDetailedMip,
                    MipLevels,
                    First2DArrayFace: FirstArraySlice,
                    NumCubes: ArraySize / 6,
                    ResourceMinLODClamp: 0.0,
                }
            }
            image::ViewKind::Cube | image::ViewKind::CubeArray => {
                error!(
                    "Cube views are not supported for the image, kind: {:?}",
                    info.kind
                );
                return Err(image::ViewError::BadKind(info.view_kind));
            }
        }

        Ok(desc)
    }

    fn view_image_as_shader_resource(
        &self,
        mut info: ViewInfo,
    ) -> Result<d3d12::D3D12_CPU_DESCRIPTOR_HANDLE, image::ViewError> {
        #![allow(non_snake_case)]

        // Depth-stencil formats can't be used for SRVs.
        info.format = match info.format {
            dxgiformat::DXGI_FORMAT_D16_UNORM => dxgiformat::DXGI_FORMAT_R16_UNORM,
            dxgiformat::DXGI_FORMAT_D32_FLOAT => dxgiformat::DXGI_FORMAT_R32_FLOAT,
            format => format,
        };

        let desc = Self::build_image_as_shader_resource_desc(&info)?;
        let handle = self.srv_uav_pool.lock().unwrap().alloc_handle();
        unsafe {
            self.raw
                .CreateShaderResourceView(info.resource.as_mut_ptr(), &desc, handle);
        }

        Ok(handle)
    }

    fn view_image_as_storage(
        &self,
        info: ViewInfo,
    ) -> Result<d3d12::D3D12_CPU_DESCRIPTOR_HANDLE, image::ViewError> {
        #![allow(non_snake_case)]
        assert_eq!(info.range.levels.start + 1, info.range.levels.end);

        let mut desc = d3d12::D3D12_UNORDERED_ACCESS_VIEW_DESC {
            Format: info.format,
            ViewDimension: 0,
            u: unsafe { mem::zeroed() },
        };

        let MipSlice = info.range.levels.start as _;
        let FirstArraySlice = info.range.layers.start as _;
        let ArraySize = (info.range.layers.end - info.range.layers.start) as _;

        if info.range.layers.end > info.kind.num_layers() {
            return Err(image::ViewError::Layer(image::LayerError::OutOfBounds(
                info.range.layers,
            )));
        }
        if info.kind.num_samples() > 1 {
            error!("MSAA images can't be viewed as UAV");
            return Err(image::ViewError::Unsupported);
        }

        match info.view_kind {
            image::ViewKind::D1 => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_UAV_DIMENSION_TEXTURE1D;
                *unsafe { desc.u.Texture1D_mut() } = d3d12::D3D12_TEX1D_UAV { MipSlice }
            }
            image::ViewKind::D1Array => {
                desc.ViewDimension = d3d12::D3D12_UAV_DIMENSION_TEXTURE1DARRAY;
                *unsafe { desc.u.Texture1DArray_mut() } = d3d12::D3D12_TEX1D_ARRAY_UAV {
                    MipSlice,
                    FirstArraySlice,
                    ArraySize,
                }
            }
            image::ViewKind::D2 => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_UAV_DIMENSION_TEXTURE2D;
                *unsafe { desc.u.Texture2D_mut() } = d3d12::D3D12_TEX2D_UAV {
                    MipSlice,
                    PlaneSlice: 0, //TODO
                }
            }
            image::ViewKind::D2Array => {
                desc.ViewDimension = d3d12::D3D12_UAV_DIMENSION_TEXTURE2DARRAY;
                *unsafe { desc.u.Texture2DArray_mut() } = d3d12::D3D12_TEX2D_ARRAY_UAV {
                    MipSlice,
                    FirstArraySlice,
                    ArraySize,
                    PlaneSlice: 0, //TODO
                }
            }
            image::ViewKind::D3 => {
                assert_eq!(info.range.layers, 0 .. 1);
                desc.ViewDimension = d3d12::D3D12_UAV_DIMENSION_TEXTURE3D;
                *unsafe { desc.u.Texture3D_mut() } = d3d12::D3D12_TEX3D_UAV {
                    MipSlice,
                    FirstWSlice: 0,
                    WSize: info.kind.extent().depth as _,
                }
            }
            image::ViewKind::Cube | image::ViewKind::CubeArray => {
                error!("Cubic images can't be viewed as UAV");
                return Err(image::ViewError::Unsupported);
            }
        }

        let handle = self.srv_uav_pool.lock().unwrap().alloc_handle();
        unsafe {
            self.raw.CreateUnorderedAccessView(
                info.resource.as_mut_ptr(),
                ptr::null_mut(),
                &desc,
                handle,
            );
        }

        Ok(handle)
    }

    pub(crate) fn create_raw_fence(&self, signalled: bool) -> native::Fence {
        let mut handle = native::Fence::null();
        assert_eq!(winerror::S_OK, unsafe {
            self.raw.CreateFence(
                if signalled { 1 } else { 0 },
                d3d12::D3D12_FENCE_FLAG_NONE,
                &d3d12::ID3D12Fence::uuidof(),
                handle.mut_void(),
            )
        });
        handle
    }

    pub(crate) fn create_swapchain_impl(
        &self,
        config: &w::SwapchainConfig,
        window_handle: windef::HWND,
        factory: native::WeakPtr<dxgi1_4::IDXGIFactory4>,
    ) -> Result<
        (
            native::WeakPtr<dxgi1_4::IDXGISwapChain3>,
            dxgiformat::DXGI_FORMAT,
        ),
        w::CreationError,
    > {
        let mut swap_chain1 = native::WeakPtr::<dxgi1_2::IDXGISwapChain1>::null();

        //TODO: proper error type?
        let non_srgb_format = conv::map_format_nosrgb(config.format).unwrap();

        // TODO: double-check values
        let desc = dxgi1_2::DXGI_SWAP_CHAIN_DESC1 {
            AlphaMode: dxgi1_2::DXGI_ALPHA_MODE_IGNORE,
            BufferCount: config.image_count,
            Width: config.extent.width,
            Height: config.extent.height,
            Format: non_srgb_format,
            Flags: 0,
            BufferUsage: dxgitype::DXGI_USAGE_RENDER_TARGET_OUTPUT,
            SampleDesc: dxgitype::DXGI_SAMPLE_DESC {
                Count: 1,
                Quality: 0,
            },
            Scaling: dxgi1_2::DXGI_SCALING_STRETCH,
            Stereo: FALSE,
            SwapEffect: dxgi::DXGI_SWAP_EFFECT_FLIP_DISCARD,
        };

        unsafe {
            let hr = factory.CreateSwapChainForHwnd(
                self.present_queue.as_mut_ptr() as *mut _,
                window_handle,
                &desc,
                ptr::null(),
                ptr::null_mut(),
                swap_chain1.mut_void() as *mut *mut _,
            );

            if !winerror::SUCCEEDED(hr) {
                error!("error on swapchain creation 0x{:x}", hr);
            }

            let (swap_chain3, hr3) = swap_chain1.cast::<dxgi1_4::IDXGISwapChain3>();
            if !winerror::SUCCEEDED(hr3) {
                error!("error on swapchain cast 0x{:x}", hr3);
            }

            swap_chain1.destroy();
            Ok((swap_chain3, non_srgb_format))
        }
    }

    pub(crate) fn wrap_swapchain(
        &self,
        inner: native::WeakPtr<dxgi1_4::IDXGISwapChain3>,
        config: &w::SwapchainConfig,
    ) -> Swapchain {
        let rtv_desc = d3d12::D3D12_RENDER_TARGET_VIEW_DESC {
            Format: conv::map_format(config.format).unwrap(),
            ViewDimension: d3d12::D3D12_RTV_DIMENSION_TEXTURE2D,
            ..unsafe { mem::zeroed() }
        };
        let rtv_heap = Device::create_descriptor_heap_impl(
            self.raw,
            native::DescriptorHeapType::Rtv,
            false,
            config.image_count as _,
        );

        let mut resources = vec![native::Resource::null(); config.image_count as usize];
        for (i, res) in resources.iter_mut().enumerate() {
            let rtv_handle = rtv_heap.at(i as _, 0).cpu;
            unsafe {
                inner.GetBuffer(i as _, &d3d12::ID3D12Resource::uuidof(), res.mut_void());
                self.raw
                    .CreateRenderTargetView(res.as_mut_ptr(), &rtv_desc, rtv_handle);
            }
        }

        Swapchain {
            inner,
            next_frame: 0,
            frame_queue: VecDeque::new(),
            rtv_heap,
            resources,
        }
    }
}

impl d::Device<B> for Device {
    unsafe fn allocate_memory(
        &self,
        mem_type: hal::MemoryTypeId,
        size: u64,
    ) -> Result<r::Memory, d::AllocationError> {
        let mem_type = mem_type.0;
        let mem_base_id = mem_type % NUM_HEAP_PROPERTIES;
        let heap_property = &self.heap_properties[mem_base_id];

        let properties = d3d12::D3D12_HEAP_PROPERTIES {
            Type: d3d12::D3D12_HEAP_TYPE_CUSTOM,
            CPUPageProperty: heap_property.page_property,
            MemoryPoolPreference: heap_property.memory_pool,
            CreationNodeMask: 0,
            VisibleNodeMask: 0,
        };

        // Exposed memory types are grouped according to their capabilities.
        // See `MemoryGroup` for more details.
        let mem_group = mem_type / NUM_HEAP_PROPERTIES;

        let desc = d3d12::D3D12_HEAP_DESC {
            SizeInBytes: size,
            Properties: properties,
            Alignment: d3d12::D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT as _, // TODO: not always..?
            Flags: match mem_group {
                0 => d3d12::D3D12_HEAP_FLAG_ALLOW_ALL_BUFFERS_AND_TEXTURES,
                1 => d3d12::D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS,
                2 => d3d12::D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES,
                3 => d3d12::D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES,
                _ => unreachable!(),
            },
        };

        let mut heap = native::Heap::null();
        let hr = self
            .raw
            .clone()
            .CreateHeap(&desc, &d3d12::ID3D12Heap::uuidof(), heap.mut_void());
        if hr == winerror::E_OUTOFMEMORY {
            return Err(d::OutOfMemory::Device.into());
        }
        assert_eq!(winerror::S_OK, hr);

        // The first memory heap of each group corresponds to the default heap, which is can never
        // be mapped.
        // Devices supporting heap tier 1 can only created buffers on mem group 1 (ALLOW_ONLY_BUFFERS).
        // Devices supporting heap tier 2 always expose only mem group 0 and don't have any further restrictions.
        let is_mapable = mem_base_id != 0
            && (mem_group == MemoryGroup::Universal as _
                || mem_group == MemoryGroup::BufferOnly as _);

        // Create a buffer resource covering the whole memory slice to be able to map the whole memory.
        let resource = if is_mapable {
            let mut resource = native::Resource::null();
            let desc = d3d12::D3D12_RESOURCE_DESC {
                Dimension: d3d12::D3D12_RESOURCE_DIMENSION_BUFFER,
                Alignment: 0,
                Width: size,
                Height: 1,
                DepthOrArraySize: 1,
                MipLevels: 1,
                Format: dxgiformat::DXGI_FORMAT_UNKNOWN,
                SampleDesc: dxgitype::DXGI_SAMPLE_DESC {
                    Count: 1,
                    Quality: 0,
                },
                Layout: d3d12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
                Flags: d3d12::D3D12_RESOURCE_FLAG_NONE,
            };

            assert_eq!(
                winerror::S_OK,
                self.raw.clone().CreatePlacedResource(
                    heap.as_mut_ptr(),
                    0,
                    &desc,
                    d3d12::D3D12_RESOURCE_STATE_COMMON,
                    ptr::null(),
                    &d3d12::ID3D12Resource::uuidof(),
                    resource.mut_void(),
                )
            );

            Some(resource)
        } else {
            None
        };

        Ok(r::Memory {
            heap,
            type_id: mem_type,
            size,
            resource,
        })
    }

    unsafe fn create_command_pool(
        &self,
        family: QueueFamilyId,
        create_flags: CommandPoolCreateFlags,
    ) -> Result<CommandPool, d::OutOfMemory> {
        let list_type = QUEUE_FAMILIES[family.0].native_type();

        let allocator = if create_flags.contains(CommandPoolCreateFlags::RESET_INDIVIDUAL) {
            // Allocators are created per individual ID3D12GraphicsCommandList
            CommandPoolAllocator::Individual(Vec::new())
        } else {
            let (command_allocator, hr) = self.raw.create_command_allocator(list_type);

            // TODO: error handling
            if !winerror::SUCCEEDED(hr) {
                error!("error on command allocator creation: {:x}", hr);
            }

            CommandPoolAllocator::Shared(command_allocator)
        };

        Ok(CommandPool {
            allocator,
            device: self.raw,
            list_type,
            shared: self.shared.clone(),
            create_flags,
        })
    }

    unsafe fn destroy_command_pool(&self, pool: CommandPool) {
        pool.destroy();
    }

    unsafe fn create_render_pass<'a, IA, IS, ID>(
        &self,
        attachments: IA,
        subpasses: IS,
        dependencies: ID,
    ) -> Result<r::RenderPass, d::OutOfMemory>
    where
        IA: IntoIterator,
        IA::Item: Borrow<pass::Attachment>,
        IS: IntoIterator,
        IS::Item: Borrow<pass::SubpassDesc<'a>>,
        ID: IntoIterator,
        ID::Item: Borrow<pass::SubpassDependency>,
    {
        #[derive(Copy, Clone, Debug, PartialEq)]
        enum SubState {
            New(d3d12::D3D12_RESOURCE_STATES),
            // Color attachment which will be resolved at the end of the subpass
            Resolve(d3d12::D3D12_RESOURCE_STATES),
            Preserve,
            Undefined,
        }
        /// Temporary information about every sub-pass
        struct SubInfo<'a> {
            desc: pass::SubpassDesc<'a>,
            /// States before the render-pass (in self.start)
            /// and after the render-pass (in self.end).
            external_dependencies: Range<image::Access>,
            /// Counts the number of dependencies that need to be resolved
            /// before starting this subpass.
            unresolved_dependencies: u16,
        }
        struct AttachmentInfo {
            sub_states: Vec<SubState>,
            last_state: d3d12::D3D12_RESOURCE_STATES,
            barrier_start_index: usize,
        }

        let attachments = attachments
            .into_iter()
            .map(|attachment| attachment.borrow().clone())
            .collect::<SmallVec<[_; 5]>>();
        let mut sub_infos = subpasses
            .into_iter()
            .map(|desc| {
                SubInfo {
                    desc: desc.borrow().clone(),
                    external_dependencies: image::Access::empty() .. image::Access::empty(),
                    unresolved_dependencies: 0,
                }
            })
            .collect::<SmallVec<[_; 1]>>();
        let dependencies = dependencies.into_iter().collect::<SmallVec<[_; 2]>>();

        let mut att_infos = (0 .. attachments.len())
            .map(|_| AttachmentInfo {
                sub_states: vec![SubState::Undefined; sub_infos.len()],
                last_state: d3d12::D3D12_RESOURCE_STATE_COMMON, // is to be overwritten
                barrier_start_index: 0,
            })
            .collect::<SmallVec<[_; 5]>>();

        for dep in &dependencies {
            use hal::pass::SubpassRef as Sr;
            let dep = dep.borrow();
            match dep.passes {
                Range { start: Sr::External, end: Sr::External } => {
                    error!("Unexpected external-external dependency!");
                }
                Range { start: Sr::External, end: Sr::Pass(sid) } => {
                    sub_infos[sid].external_dependencies.start |= dep.accesses.start;
                }
                Range { start: Sr::Pass(sid), end: Sr::External } => {
                    sub_infos[sid].external_dependencies.end |= dep.accesses.end;
                }
                Range { start: Sr::Pass(from_sid), end: Sr::Pass(sid) } => {
                    //Note: self-dependencies are ignored
                    if from_sid != sid {
                        sub_infos[sid].unresolved_dependencies += 1;
                    }
                }
            }
        }

        // Fill out subpass known layouts
        for (sid, sub_info) in sub_infos.iter().enumerate() {
            let sub = &sub_info.desc;
            for (i, &(id, _layout)) in sub.colors.iter().enumerate() {
                let target_state = d3d12::D3D12_RESOURCE_STATE_RENDER_TARGET;
                let state = match sub.resolves.get(i) {
                    Some(_) => SubState::Resolve(target_state),
                    None => SubState::New(target_state),
                };
                let old = mem::replace(&mut att_infos[id].sub_states[sid], state);
                debug_assert_eq!(SubState::Undefined, old);
            }
            for &(id, layout) in sub.depth_stencil {
                let state = SubState::New(match layout {
                    image::Layout::DepthStencilAttachmentOptimal => d3d12::D3D12_RESOURCE_STATE_DEPTH_WRITE,
                    image::Layout::DepthStencilReadOnlyOptimal => d3d12::D3D12_RESOURCE_STATE_DEPTH_READ,
                    image::Layout::General => d3d12::D3D12_RESOURCE_STATE_DEPTH_WRITE,
                    _ => {
                        error!("Unexpected depth/stencil layout: {:?}", layout);
                        d3d12::D3D12_RESOURCE_STATE_COMMON
                    }
                });
                let old = mem::replace(&mut att_infos[id].sub_states[sid], state);
                debug_assert_eq!(SubState::Undefined, old);
            }
            for &(id, _layout) in sub.inputs {
                let state = SubState::New(d3d12::D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
                let old = mem::replace(&mut att_infos[id].sub_states[sid], state);
                debug_assert_eq!(SubState::Undefined, old);
            }
            for &(id, _layout) in sub.resolves {
                let state = SubState::New(d3d12::D3D12_RESOURCE_STATE_RESOLVE_DEST);
                let old = mem::replace(&mut att_infos[id].sub_states[sid], state);
                debug_assert_eq!(SubState::Undefined, old);
            }
            for &id in sub.preserves {
                let old = mem::replace(&mut att_infos[id].sub_states[sid], SubState::Preserve);
                debug_assert_eq!(SubState::Undefined, old);
            }
        }

        let mut rp = r::RenderPass {
            attachments: attachments.iter().cloned().collect(),
            subpasses: Vec::new(),
            post_barriers: Vec::new(),
        };

        while let Some(sid) = sub_infos.iter().position(|si| si.unresolved_dependencies == 0) {
            for dep in &dependencies {
                let dep = dep.borrow();
                if dep.passes.start != dep.passes.end
                    && dep.passes.start == pass::SubpassRef::Pass(sid)
                {
                    if let pass::SubpassRef::Pass(other) = dep.passes.end {
                        sub_infos[other].unresolved_dependencies -= 1;
                    }
                }
            }

            let si = &mut sub_infos[sid];
            si.unresolved_dependencies = !0; // mark as done

            // Subpass barriers
            let mut pre_barriers = Vec::new();
            let mut post_barriers = Vec::new();
            for (att_id, (ai, att)) in att_infos.iter_mut().zip(attachments.iter()).enumerate() {
                // Attachment wasn't used before, figure out the initial state
                if ai.barrier_start_index == 0 {
                    //Note: the external dependencies are provided for all attachments that are
                    // first used in this sub-pass, so they may contain more states than we expect
                    // for this particular attachment.
                    ai.last_state = conv::map_image_resource_state(
                        si.external_dependencies.start,
                        att.layouts.start,
                    );
                }
                // Barrier from previous subpass to current or following subpasses.
                match ai.sub_states[sid] {
                    SubState::Preserve => {
                        ai.barrier_start_index = rp.subpasses.len() + 1;
                    }
                    SubState::New(state) if state != ai.last_state => {
                        let barrier = r::BarrierDesc::new(att_id, ai.last_state .. state);
                        match rp.subpasses.get_mut(ai.barrier_start_index) {
                            Some(past_subpass) => {
                                let split = barrier.split();
                                past_subpass.pre_barriers.push(split.start);
                                pre_barriers.push(split.end);
                            }
                            None => pre_barriers.push(barrier),
                        }
                        ai.last_state = state;
                        ai.barrier_start_index = rp.subpasses.len() + 1;
                    }
                    SubState::Resolve(state) => {
                        // 1. Standard pre barrier to update state from previous pass into desired substate.
                        if state != ai.last_state {
                            let barrier = r::BarrierDesc::new(att_id, ai.last_state .. state);
                            match rp.subpasses.get_mut(ai.barrier_start_index) {
                                Some(past_subpass) => {
                                    let split = barrier.split();
                                    past_subpass.pre_barriers.push(split.start);
                                    pre_barriers.push(split.end);
                                }
                                None => pre_barriers.push(barrier),
                            }
                        }

                        // 2. Post Barrier at the end of the subpass into RESOLVE_SOURCE.
                        let resolve_state = d3d12::D3D12_RESOURCE_STATE_RESOLVE_SOURCE;
                        let barrier = r::BarrierDesc::new(att_id, state .. resolve_state);
                        post_barriers.push(barrier);

                        ai.last_state = resolve_state;
                        ai.barrier_start_index = rp.subpasses.len() + 1;
                    }
                    SubState::Undefined |
                    SubState::New(_) => {}
                };
            }

            rp.subpasses.push(r::SubpassDesc {
                color_attachments: si.desc.colors.iter().cloned().collect(),
                depth_stencil_attachment: si.desc.depth_stencil.cloned(),
                input_attachments: si.desc.inputs.iter().cloned().collect(),
                resolve_attachments: si.desc.resolves.iter().cloned().collect(),
                pre_barriers,
                post_barriers,
            });
        }
        // if this fails, our graph has cycles
        assert_eq!(rp.subpasses.len(), sub_infos.len());
        assert!(sub_infos.iter().all(|si| si.unresolved_dependencies == !0));

        // take care of the post-pass transitions at the end of the renderpass.
        for (att_id, (ai, att)) in att_infos.iter().zip(attachments.iter()).enumerate() {
            let state_dst = if ai.barrier_start_index == 0 {
                // attachment wasn't used in any sub-pass?
                continue
            } else {
                let si = &sub_infos[ai.barrier_start_index - 1];
                conv::map_image_resource_state(si.external_dependencies.end, att.layouts.end)
            };
            if state_dst == ai.last_state {
                continue;
            }
            let barrier = r::BarrierDesc::new(att_id, ai.last_state .. state_dst);
            match rp.subpasses.get_mut(ai.barrier_start_index) {
                Some(past_subpass) => {
                    let split = barrier.split();
                    past_subpass.pre_barriers.push(split.start);
                    rp.post_barriers.push(split.end);
                }
                None => rp.post_barriers.push(barrier),
            }
        }

        Ok(rp)
    }

    unsafe fn create_pipeline_layout<IS, IR>(
        &self,
        sets: IS,
        push_constant_ranges: IR,
    ) -> Result<r::PipelineLayout, d::OutOfMemory>
    where
        IS: IntoIterator,
        IS::Item: Borrow<r::DescriptorSetLayout>,
        IR: IntoIterator,
        IR::Item: Borrow<(pso::ShaderStageFlags, Range<u32>)>,
    {
        // Pipeline layouts are implemented as RootSignature for D3D12.
        //
        // Push Constants are implemented as root constants.
        //
        // Each descriptor set layout will be one table entry of the root signature.
        // We have the additional restriction that SRV/CBV/UAV and samplers need to be
        // separated, so each set layout will actually occupy up to 2 entries!
        //
        // Dynamic uniform buffers are implemented as root descriptors.
        // This allows to handle the dynamic offsets properly, which would not be feasible
        // with a combination of root constant and descriptor table.
        //
        // Root signature layout:
        //     Root Constants: Register: Offest/4, Space: 0
        //     ...
        //     DescriptorTable0: Space: 1 (+1) (SrvCbvUav)
        //     Root Descriptors
        //     DescriptorTable0: Space: 2 (+1) (Sampler)
        //     DescriptorTable1: Space: 3 (+1) (SrvCbvUav)
        //     ...

        let sets = sets.into_iter().collect::<Vec<_>>();

        let mut root_offset = 0;
        let root_constants = root_constants::split(push_constant_ranges)
            .iter()
            .map(|constant| {
                assert!(constant.range.start <= constant.range.end);
                root_offset += (constant.range.end - constant.range.start) as usize;

                RootConstant {
                    stages: constant.stages,
                    range: constant.range.start .. constant.range.end,
                }
            })
            .collect::<Vec<_>>();

        info!(
            "Creating a pipeline layout with {} sets and {} root constants",
            sets.len(),
            root_constants.len()
        );

        // Number of elements in the root signature.
        // Guarantees that no re-allocation is done, and our pointers are valid
        let mut parameters = Vec::with_capacity(root_constants.len() + sets.len() * 2);

        // Convert root signature descriptions into root signature parameters.
        for root_constant in root_constants.iter() {
            debug!(
                "\tRoot constant set={} range {:?}",
                ROOT_CONSTANT_SPACE, root_constant.range
            );
            parameters.push(native::RootParameter::constants(
                conv::map_shader_visibility(root_constant.stages),
                native::Binding {
                    register: root_constant.range.start as _,
                    space: ROOT_CONSTANT_SPACE,
                },
                (root_constant.range.end - root_constant.range.start) as _,
            ));
        }

        // Offest of `spaceN` for descriptor tables. Root constants will be in
        // `space0`.
        // This has to match `patch_spirv_resources` logic.
        let root_space_offset = if !root_constants.is_empty() { 1 } else { 0 };

        // Collect the whole number of bindings we will create upfront.
        // It allows us to preallocate enough storage to avoid reallocation,
        // which could cause invalid pointers.
        let total = sets
            .iter()
            .map(|desc_set| {
                let mut sum = 0;
                for binding in desc_set.borrow().bindings.iter() {
                    let content = r::DescriptorContent::from(binding.ty);
                    if !content.is_dynamic() {
                        sum += content.bits().count_ones() as usize;
                    }
                }
                sum
            })
            .sum();
        let mut ranges = Vec::with_capacity(total);

        let elements = sets.iter().enumerate().map(|(i, set)| {
            let set = set.borrow();
            let space = (root_space_offset + i) as u32;
            let mut table_type = r::SetTableTypes::empty();
            let root_table_offset = root_offset;

            //TODO: split between sampler and non-sampler tables
            let visibility = conv::map_shader_visibility(
                set.bindings
                    .iter()
                    .fold(pso::ShaderStageFlags::empty(), |u, bind| {
                        u | bind.stage_flags
                    }),
            );

            for bind in set.bindings.iter() {
                debug!("\tRange {:?} at space={}", bind, space);
            }

            let describe = |bind: &pso::DescriptorSetLayoutBinding, ty| {
                native::DescriptorRange::new(
                    ty,
                    bind.count as _,
                    native::Binding {
                        register: bind.binding as _,
                        space,
                    },
                    d3d12::D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,
                )
            };

            let mut descriptors = Vec::new();
            let mut range_base = ranges.len();
            for bind in set.bindings.iter() {
                let content = r::DescriptorContent::from(bind.ty);

                if content.is_dynamic() {
                    // Root Descriptor
                    let binding = native::Binding {
                        register: bind.binding as _,
                        space,
                    };

                    if content.contains(r::DescriptorContent::CBV) {
                        descriptors.push(r::RootDescriptor {
                            offset: root_offset,
                        });
                        parameters.push(native::RootParameter::cbv_descriptor(visibility, binding));
                        root_offset += 2;
                    } else {
                        // SRV and UAV not implemented so far
                        unimplemented!()
                    }
                } else {
                    // Descriptor table ranges
                    if content.contains(r::DescriptorContent::CBV) {
                        ranges.push(describe(bind, native::DescriptorRangeType::CBV));
                    }
                    if content.contains(r::DescriptorContent::SRV) {
                        ranges.push(describe(bind, native::DescriptorRangeType::SRV));
                    }
                    if content.contains(r::DescriptorContent::UAV) {
                        ranges.push(describe(bind, native::DescriptorRangeType::UAV));
                    }
                }
            }
            if ranges.len() > range_base {
                parameters.push(native::RootParameter::descriptor_table(
                    visibility,
                    &ranges[range_base ..],
                ));
                table_type |= r::SRV_CBV_UAV;
                root_offset += 1;
            }

            range_base = ranges.len();
            for bind in set.bindings.iter() {
                let content = r::DescriptorContent::from(bind.ty);
                if content.contains(r::DescriptorContent::SAMPLER) {
                    ranges.push(describe(bind, native::DescriptorRangeType::Sampler));
                }
            }
            if ranges.len() > range_base {
                parameters.push(native::RootParameter::descriptor_table(
                    visibility,
                    &ranges[range_base ..],
                ));
                table_type |= r::SAMPLERS;
                root_offset += 1;
            }

            r::RootElement {
                table: r::RootTable {
                    ty: table_type,
                    offset: root_table_offset as _,
                },
                descriptors,
            }
        }).collect();

        // Ensure that we didn't reallocate!
        debug_assert_eq!(ranges.len(), total);

        // TODO: error handling
        let (signature_raw, error) = match self.library.serialize_root_signature(
            native::RootSignatureVersion::V1_0,
            &parameters,
            &[],
            native::RootSignatureFlags::ALLOW_IA_INPUT_LAYOUT,
        ) {
            Ok((pair, hr)) if winerror::SUCCEEDED(hr) => pair,
            Ok((_, hr)) => panic!("Can't serialize root signature: {:?}", hr),
            Err(e) => panic!("Can't find serialization function: {:?}", e),
        };

        if !error.is_null() {
            error!(
                "Root signature serialization error: {:?}",
                error.as_c_str().to_str().unwrap()
            );
            error.destroy();
        }

        // TODO: error handling
        let (signature, _hr) = self.raw.create_root_signature(signature_raw, 0);
        signature_raw.destroy();

        Ok(r::PipelineLayout {
            raw: signature,
            constants: root_constants,
            elements,
            num_parameter_slots: parameters.len(),
        })
    }

    unsafe fn create_pipeline_cache(&self, _data: Option<&[u8]>) -> Result<(), d::OutOfMemory> {
        Ok(())
    }

    unsafe fn get_pipeline_cache_data(&self, _cache: &()) -> Result<Vec<u8>, d::OutOfMemory> {
        //empty
        Ok(Vec::new())
    }

    unsafe fn destroy_pipeline_cache(&self, _: ()) {
        //empty
    }

    unsafe fn merge_pipeline_caches<I>(&self, _: &(), _: I) -> Result<(), d::OutOfMemory>
    where
        I: IntoIterator,
        I::Item: Borrow<()>,
    {
        //empty
        Ok(())
    }

    unsafe fn create_graphics_pipeline<'a>(
        &self,
        desc: &pso::GraphicsPipelineDesc<'a, B>,
        _cache: Option<&()>,
    ) -> Result<r::GraphicsPipeline, pso::CreationError> {
        enum ShaderBc {
            Owned(native::Blob),
            Borrowed(native::Blob),
            None,
        }
        impl ShaderBc {
            pub fn shader(&self) -> native::Shader {
                match *self {
                    ShaderBc::Owned(ref bc) | ShaderBc::Borrowed(ref bc) => {
                        native::Shader::from_blob(*bc)
                    }
                    ShaderBc::None => native::Shader::null(),
                }
            }
        }

        let build_shader = |stage: pso::Stage, source: Option<&pso::EntryPoint<'a, B>>| {
            let source = match source {
                Some(src) => src,
                None => return Ok(ShaderBc::None),
            };

            match Self::extract_entry_point(stage, source, desc.layout) {
                Ok((shader, true)) => Ok(ShaderBc::Owned(shader)),
                Ok((shader, false)) => Ok(ShaderBc::Borrowed(shader)),
                Err(err) => Err(pso::CreationError::Shader(err)),
            }
        };

        let vs = build_shader(pso::Stage::Vertex, Some(&desc.shaders.vertex))?;
        let ps = build_shader(pso::Stage::Fragment, desc.shaders.fragment.as_ref())?;
        let gs = build_shader(pso::Stage::Geometry, desc.shaders.geometry.as_ref())?;
        let ds = build_shader(pso::Stage::Domain, desc.shaders.domain.as_ref())?;
        let hs = build_shader(pso::Stage::Hull, desc.shaders.hull.as_ref())?;

        // Rebind vertex buffers, see native.rs for more details.
        let mut vertex_bindings = [None; MAX_VERTEX_BUFFERS];
        let mut vertex_strides = [0; MAX_VERTEX_BUFFERS];

        for buffer in &desc.vertex_buffers {
            vertex_strides[buffer.binding as usize] = buffer.stride;
        }
        // Fill in identity mapping where we don't need to adjust anything.
        for attrib in &desc.attributes {
            let binding = attrib.binding as usize;
            let stride = vertex_strides[attrib.binding as usize];
            if attrib.element.offset < stride {
                vertex_bindings[binding] = Some(r::VertexBinding {
                    stride: vertex_strides[attrib.binding as usize],
                    offset: 0,
                    mapped_binding: binding,
                });
            }
        }

        // Define input element descriptions
        let input_element_descs = desc
            .attributes
            .iter()
            .filter_map(|attrib| {
                let buffer_desc = match desc
                    .vertex_buffers
                    .iter()
                    .find(|buffer_desc| buffer_desc.binding == attrib.binding)
                {
                    Some(buffer_desc) => buffer_desc,
                    None => {
                        error!(
                            "Couldn't find associated vertex buffer description {:?}",
                            attrib.binding
                        );
                        return Some(Err(pso::CreationError::Other));
                    }
                };

                let (slot_class, step_rate) = match buffer_desc.rate {
                    VertexInputRate::Vertex => {
                        (d3d12::D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0)
                    }
                    VertexInputRate::Instance(divisor) => {
                        (d3d12::D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA, divisor)
                    }
                };
                let format = attrib.element.format;

                // Check if we need to add a new remapping in-case the offset is
                // higher than the vertex stride.
                // In this case we rebase the attribute to zero offset.
                let binding = attrib.binding as usize;
                let stride = vertex_strides[binding];
                let offset = attrib.element.offset;
                let (input_slot, offset) = if stride <= offset {
                    // Number of input attributes may not exceed bindings, see limits.
                    // We will always find at least one free binding.
                    let mapping = vertex_bindings.iter().position(Option::is_none).unwrap();
                    vertex_bindings[mapping] = Some(r::VertexBinding {
                        stride: vertex_strides[binding],
                        offset: offset,
                        mapped_binding: binding,
                    });

                    (mapping, 0)
                } else {
                    (binding, offset)
                };

                Some(Ok(d3d12::D3D12_INPUT_ELEMENT_DESC {
                    SemanticName: "TEXCOORD\0".as_ptr() as *const _, // Semantic name used by SPIRV-Cross
                    SemanticIndex: attrib.location,
                    Format: match conv::map_format(format) {
                        Some(fm) => fm,
                        None => {
                            error!("Unable to find DXGI format for {:?}", format);
                            return Some(Err(pso::CreationError::Other));
                        }
                    },
                    InputSlot: input_slot as _,
                    AlignedByteOffset: offset,
                    InputSlotClass: slot_class,
                    InstanceDataStepRate: step_rate as _,
                }))
            })
            .collect::<Result<Vec<_>, _>>()?;

        // TODO: check maximum number of rtvs
        // Get associated subpass information
        let pass = {
            let subpass = &desc.subpass;
            match subpass.main_pass.subpasses.get(subpass.index) {
                Some(subpass) => subpass,
                None => return Err(pso::CreationError::InvalidSubpass(subpass.index)),
            }
        };

        // Get color attachment formats from subpass
        let (rtvs, num_rtvs) = {
            let mut rtvs = [dxgiformat::DXGI_FORMAT_UNKNOWN; 8];
            let mut num_rtvs = 0;
            for (rtv, target) in rtvs.iter_mut().zip(pass.color_attachments.iter()) {
                let format = desc.subpass.main_pass.attachments[target.0].format;
                *rtv = format
                    .and_then(conv::map_format)
                    .unwrap_or(dxgiformat::DXGI_FORMAT_UNKNOWN);
                num_rtvs += 1;
            }
            (rtvs, num_rtvs)
        };

        let sample_desc = dxgitype::DXGI_SAMPLE_DESC {
            Count: match desc.multisampling {
                Some(ref ms) => ms.rasterization_samples as _,
                None => 1,
            },
            Quality: 0,
        };

        // Setup pipeline description
        let pso_desc = d3d12::D3D12_GRAPHICS_PIPELINE_STATE_DESC {
            pRootSignature: desc.layout.raw.as_mut_ptr(),
            VS: *vs.shader(),
            PS: *ps.shader(),
            GS: *gs.shader(),
            DS: *ds.shader(),
            HS: *hs.shader(),
            StreamOutput: d3d12::D3D12_STREAM_OUTPUT_DESC {
                pSODeclaration: ptr::null(),
                NumEntries: 0,
                pBufferStrides: ptr::null(),
                NumStrides: 0,
                RasterizedStream: 0,
            },
            BlendState: d3d12::D3D12_BLEND_DESC {
                AlphaToCoverageEnable: desc.multisampling.as_ref().map_or(FALSE, |ms| {
                    if ms.alpha_coverage {
                        TRUE
                    } else {
                        FALSE
                    }
                }),
                IndependentBlendEnable: TRUE,
                RenderTarget: conv::map_render_targets(&desc.blender.targets),
            },
            SampleMask: UINT::max_value(),
            RasterizerState: conv::map_rasterizer(&desc.rasterizer),
            DepthStencilState: conv::map_depth_stencil(&desc.depth_stencil),
            InputLayout: d3d12::D3D12_INPUT_LAYOUT_DESC {
                pInputElementDescs: if input_element_descs.is_empty() {
                    ptr::null()
                } else {
                    input_element_descs.as_ptr()
                },
                NumElements: input_element_descs.len() as u32,
            },
            IBStripCutValue: d3d12::D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED, // TODO
            PrimitiveTopologyType: conv::map_topology_type(desc.input_assembler.primitive),
            NumRenderTargets: num_rtvs,
            RTVFormats: rtvs,
            DSVFormat: pass
                .depth_stencil_attachment
                .and_then(|att_ref| {
                    desc.subpass.main_pass.attachments[att_ref.0]
                        .format
                        .and_then(|f| conv::map_format_dsv(f.base_format().0))
                })
                .unwrap_or(dxgiformat::DXGI_FORMAT_UNKNOWN),
            SampleDesc: sample_desc,
            NodeMask: 0,
            CachedPSO: d3d12::D3D12_CACHED_PIPELINE_STATE {
                pCachedBlob: ptr::null(),
                CachedBlobSizeInBytes: 0,
            },
            Flags: d3d12::D3D12_PIPELINE_STATE_FLAG_NONE,
        };

        let topology = conv::map_topology(&desc.input_assembler);

        // Create PSO
        let mut pipeline = native::PipelineState::null();
        let hr = if desc.depth_stencil.depth_bounds {
            // The DepthBoundsTestEnable option isn't available in the original D3D12_GRAPHICS_PIPELINE_STATE_DESC struct.
            // Instead, we must use the newer subobject stream method.
            let (device2, hr) = self.raw.cast::<d3d12::ID3D12Device2>();
            if winerror::SUCCEEDED(hr) {
                let mut pss_stream = GraphicsPipelineStateSubobjectStream::new(&pso_desc, true);
                let pss_desc = d3d12::D3D12_PIPELINE_STATE_STREAM_DESC {
                    SizeInBytes: mem::size_of_val(&pss_stream),
                    pPipelineStateSubobjectStream: &mut pss_stream as *mut _ as _,
                };
                device2.CreatePipelineState(
                    &pss_desc,
                    &d3d12::ID3D12PipelineState::uuidof(),
                    pipeline.mut_void(),
                )
            } else {
                hr
            }
        } else {
            self.raw.clone().CreateGraphicsPipelineState(
                &pso_desc,
                &d3d12::ID3D12PipelineState::uuidof(),
                pipeline.mut_void(),
            )
        };

        let destroy_shader = |shader: ShaderBc| {
            if let ShaderBc::Owned(bc) = shader {
                bc.destroy();
            }
        };

        destroy_shader(vs);
        destroy_shader(ps);
        destroy_shader(gs);
        destroy_shader(hs);
        destroy_shader(ds);

        if winerror::SUCCEEDED(hr) {
            let mut baked_states = desc.baked_states.clone();
            if !desc.depth_stencil.depth_bounds {
                baked_states.depth_bounds = None;
            }

            Ok(r::GraphicsPipeline {
                raw: pipeline,
                signature: desc.layout.raw,
                num_parameter_slots: desc.layout.num_parameter_slots,
                topology,
                constants: desc.layout.constants.clone(),
                vertex_bindings,
                baked_states,
            })
        } else {
            Err(pso::CreationError::Other)
        }
    }

    unsafe fn create_compute_pipeline<'a>(
        &self,
        desc: &pso::ComputePipelineDesc<'a, B>,
        _cache: Option<&()>,
    ) -> Result<r::ComputePipeline, pso::CreationError> {
        let (cs, cs_destroy) =
            Self::extract_entry_point(pso::Stage::Compute, &desc.shader, desc.layout)
                .map_err(|err| pso::CreationError::Shader(err))?;

        let (pipeline, hr) = self.raw.create_compute_pipeline_state(
            desc.layout.raw,
            native::Shader::from_blob(cs),
            0,
            native::CachedPSO::null(),
            native::PipelineStateFlags::empty(),
        );

        if cs_destroy {
            cs.destroy();
        }

        if winerror::SUCCEEDED(hr) {
            Ok(r::ComputePipeline {
                raw: pipeline,
                signature: desc.layout.raw,
                num_parameter_slots: desc.layout.num_parameter_slots,
                constants: desc.layout.constants.clone(),
            })
        } else {
            Err(pso::CreationError::Other)
        }
    }

    unsafe fn create_framebuffer<I>(
        &self,
        _renderpass: &r::RenderPass,
        attachments: I,
        extent: image::Extent,
    ) -> Result<r::Framebuffer, d::OutOfMemory>
    where
        I: IntoIterator,
        I::Item: Borrow<r::ImageView>,
    {
        Ok(r::Framebuffer {
            attachments: attachments.into_iter().map(|att| *att.borrow()).collect(),
            layers: extent.depth as _,
        })
    }

    unsafe fn create_shader_module(
        &self,
        raw_data: &[u32],
    ) -> Result<r::ShaderModule, d::ShaderError> {
        Ok(r::ShaderModule::Spirv(raw_data.into()))
    }

    unsafe fn create_buffer(
        &self,
        mut size: u64,
        usage: buffer::Usage,
    ) -> Result<r::Buffer, buffer::CreationError> {
        if usage.contains(buffer::Usage::UNIFORM) {
            // Constant buffer view sizes need to be aligned.
            // Coupled with the offset alignment we can enforce an aligned CBV size
            // on descriptor updates.
            size = (size + 255) & !255;
        }
        if usage.contains(buffer::Usage::TRANSFER_DST) {
            // minimum of 1 word for the clear UAV
            size = size.max(4);
        }

        let type_mask_shift = if self.private_caps.heterogeneous_resource_heaps {
            MEM_TYPE_UNIVERSAL_SHIFT
        } else {
            MEM_TYPE_BUFFER_SHIFT
        };

        let requirements = memory::Requirements {
            size,
            alignment: d3d12::D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT as u64,
            type_mask: MEM_TYPE_MASK << type_mask_shift,
        };

        Ok(r::Buffer::Unbound(r::BufferUnbound {
            requirements,
            usage,
        }))
    }

    unsafe fn get_buffer_requirements(&self, buffer: &r::Buffer) -> Requirements {
        match buffer {
            r::Buffer::Unbound(b) => b.requirements,
            r::Buffer::Bound(b) => b.requirements,
        }
    }

    unsafe fn bind_buffer_memory(
        &self,
        memory: &r::Memory,
        offset: u64,
        buffer: &mut r::Buffer,
    ) -> Result<(), d::BindError> {
        let buffer_unbound = *buffer.expect_unbound();
        if buffer_unbound.requirements.type_mask & (1 << memory.type_id) == 0 {
            error!(
                "Bind memory failure: supported mask 0x{:x}, given id {}",
                buffer_unbound.requirements.type_mask, memory.type_id
            );
            return Err(d::BindError::WrongMemory);
        }
        if offset + buffer_unbound.requirements.size > memory.size {
            return Err(d::BindError::OutOfBounds);
        }

        let mut resource = native::Resource::null();
        let desc = d3d12::D3D12_RESOURCE_DESC {
            Dimension: d3d12::D3D12_RESOURCE_DIMENSION_BUFFER,
            Alignment: 0,
            Width: buffer_unbound.requirements.size,
            Height: 1,
            DepthOrArraySize: 1,
            MipLevels: 1,
            Format: dxgiformat::DXGI_FORMAT_UNKNOWN,
            SampleDesc: dxgitype::DXGI_SAMPLE_DESC {
                Count: 1,
                Quality: 0,
            },
            Layout: d3d12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
            Flags: conv::map_buffer_flags(buffer_unbound.usage),
        };

        assert_eq!(
            winerror::S_OK,
            self.raw.clone().CreatePlacedResource(
                memory.heap.as_mut_ptr(),
                offset,
                &desc,
                d3d12::D3D12_RESOURCE_STATE_COMMON,
                ptr::null(),
                &d3d12::ID3D12Resource::uuidof(),
                resource.mut_void(),
            )
        );

        let clear_uav = if buffer_unbound.usage.contains(buffer::Usage::TRANSFER_DST) {
            let handle = self.srv_uav_pool.lock().unwrap().alloc_handle();
            let mut view_desc = d3d12::D3D12_UNORDERED_ACCESS_VIEW_DESC {
                Format: dxgiformat::DXGI_FORMAT_R32_TYPELESS,
                ViewDimension: d3d12::D3D12_UAV_DIMENSION_BUFFER,
                u: mem::zeroed(),
            };

            *view_desc.u.Buffer_mut() = d3d12::D3D12_BUFFER_UAV {
                FirstElement: 0,
                NumElements: (buffer_unbound.requirements.size / 4) as _,
                StructureByteStride: 0,
                CounterOffsetInBytes: 0,
                Flags: d3d12::D3D12_BUFFER_UAV_FLAG_RAW,
            };

            self.raw.CreateUnorderedAccessView(
                resource.as_mut_ptr(),
                ptr::null_mut(),
                &view_desc,
                handle,
            );
            Some(handle)
        } else {
            None
        };

        *buffer = r::Buffer::Bound(r::BufferBound {
            resource,
            requirements: buffer_unbound.requirements,
            clear_uav,
        });

        Ok(())
    }

    unsafe fn create_buffer_view<R: RangeArg<u64>>(
        &self,
        buffer: &r::Buffer,
        format: Option<format::Format>,
        range: R,
    ) -> Result<r::BufferView, buffer::ViewCreationError> {
        let buffer = buffer.expect_bound();
        let buffer_features = {
            let idx = format.map(|fmt| fmt as usize).unwrap_or(0);
            self.format_properties.get(idx).properties.buffer_features
        };
        let (format, format_desc) = match format.and_then(conv::map_format) {
            Some(fmt) => (fmt, format.unwrap().surface_desc()),
            None => return Err(buffer::ViewCreationError::UnsupportedFormat { format }),
        };

        let start = *range.start().unwrap_or(&0);
        let end = *range.end().unwrap_or(&(buffer.requirements.size as _));

        let bytes_per_texel = (format_desc.bits / 8) as u64;
        // Check if it adheres to the texel buffer offset limit
        assert_eq!(start % bytes_per_texel, 0);
        let first_element = start / bytes_per_texel;
        let num_elements = (end - start) / bytes_per_texel; // rounds down to next smaller size

        let handle_srv = if buffer_features.contains(format::BufferFeature::UNIFORM_TEXEL) {
            let mut desc = d3d12::D3D12_SHADER_RESOURCE_VIEW_DESC {
                Format: format,
                ViewDimension: d3d12::D3D12_SRV_DIMENSION_BUFFER,
                Shader4ComponentMapping: IDENTITY_MAPPING,
                u: mem::zeroed(),
            };

            *desc.u.Buffer_mut() = d3d12::D3D12_BUFFER_SRV {
                FirstElement: first_element,
                NumElements: num_elements as _,
                StructureByteStride: bytes_per_texel as _,
                Flags: d3d12::D3D12_BUFFER_SRV_FLAG_NONE,
            };

            let handle = self.srv_uav_pool.lock().unwrap().alloc_handle();
            self.raw
                .clone()
                .CreateShaderResourceView(buffer.resource.as_mut_ptr(), &desc, handle);
            handle
        } else {
            d3d12::D3D12_CPU_DESCRIPTOR_HANDLE { ptr: 0 }
        };

        let handle_uav = if buffer_features.intersects(
            format::BufferFeature::STORAGE_TEXEL | format::BufferFeature::STORAGE_TEXEL_ATOMIC,
        ) {
            let mut desc = d3d12::D3D12_UNORDERED_ACCESS_VIEW_DESC {
                Format: format,
                ViewDimension: d3d12::D3D12_UAV_DIMENSION_BUFFER,
                u: mem::zeroed(),
            };

            *desc.u.Buffer_mut() = d3d12::D3D12_BUFFER_UAV {
                FirstElement: first_element,
                NumElements: num_elements as _,
                StructureByteStride: bytes_per_texel as _,
                Flags: d3d12::D3D12_BUFFER_UAV_FLAG_NONE,
                CounterOffsetInBytes: 0,
            };

            let handle = self.srv_uav_pool.lock().unwrap().alloc_handle();
            self.raw.clone().CreateUnorderedAccessView(
                buffer.resource.as_mut_ptr(),
                ptr::null_mut(),
                &desc,
                handle,
            );
            handle
        } else {
            d3d12::D3D12_CPU_DESCRIPTOR_HANDLE { ptr: 0 }
        };

        return Ok(r::BufferView {
            handle_srv,
            handle_uav,
        });
    }

    unsafe fn create_image(
        &self,
        kind: image::Kind,
        mip_levels: image::Level,
        format: format::Format,
        tiling: image::Tiling,
        usage: image::Usage,
        view_caps: image::ViewCapabilities,
    ) -> Result<r::Image, image::CreationError> {
        assert!(mip_levels <= kind.num_levels());

        let base_format = format.base_format();
        let format_desc = base_format.0.desc();
        let bytes_per_block = (format_desc.bits / 8) as _;
        let block_dim = format_desc.dim;
        let extent = kind.extent();

        let format_info = self.format_properties.get(format as usize);
        let (layout, features) = match tiling {
            image::Tiling::Optimal => (
                d3d12::D3D12_TEXTURE_LAYOUT_UNKNOWN,
                format_info.properties.optimal_tiling,
            ),
            image::Tiling::Linear => (
                d3d12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
                format_info.properties.linear_tiling,
            ),
        };
        if format_info.sample_count_mask & kind.num_samples() == 0 {
            return Err(image::CreationError::Samples(kind.num_samples()));
        }

        let desc = d3d12::D3D12_RESOURCE_DESC {
            Dimension: match kind {
                image::Kind::D1(..) => d3d12::D3D12_RESOURCE_DIMENSION_TEXTURE1D,
                image::Kind::D2(..) => d3d12::D3D12_RESOURCE_DIMENSION_TEXTURE2D,
                image::Kind::D3(..) => d3d12::D3D12_RESOURCE_DIMENSION_TEXTURE3D,
            },
            Alignment: 0,
            Width: extent.width as _,
            Height: extent.height as _,
            DepthOrArraySize: if extent.depth > 1 {
                extent.depth as _
            } else {
                kind.num_layers() as _
            },
            MipLevels: mip_levels as _,
            Format: match conv::map_surface_type(base_format.0) {
                Some(format) => format,
                None => return Err(image::CreationError::Format(format)),
            },
            SampleDesc: dxgitype::DXGI_SAMPLE_DESC {
                Count: kind.num_samples() as _,
                Quality: 0,
            },
            Layout: layout,
            Flags: conv::map_image_flags(usage, features),
        };

        let alloc_info = self.raw.clone().GetResourceAllocationInfo(0, 1, &desc);

        // Image usages which require RT/DS heap due to internal implementation.
        let target_usage = image::Usage::COLOR_ATTACHMENT
            | image::Usage::DEPTH_STENCIL_ATTACHMENT
            | image::Usage::TRANSFER_DST;

        let type_mask_shift = if self.private_caps.heterogeneous_resource_heaps {
            MEM_TYPE_UNIVERSAL_SHIFT
        } else if usage.intersects(target_usage) {
            MEM_TYPE_TARGET_SHIFT
        } else {
            MEM_TYPE_IMAGE_SHIFT
        };

        Ok(r::Image::Unbound(r::ImageUnbound {
            view_format: conv::map_format(format),
            dsv_format: conv::map_format_dsv(base_format.0),
            desc,
            requirements: memory::Requirements {
                size: alloc_info.SizeInBytes,
                alignment: alloc_info.Alignment,
                type_mask: MEM_TYPE_MASK << type_mask_shift,
            },
            format,
            kind,
            usage,
            tiling,
            view_caps,
            bytes_per_block,
            block_dim,
        }))
    }

    unsafe fn get_image_requirements(&self, image: &r::Image) -> Requirements {
        match image {
            r::Image::Bound(i) => i.requirements,
            r::Image::Unbound(i) => i.requirements,
        }
    }

    unsafe fn get_image_subresource_footprint(
        &self,
        image: &r::Image,
        sub: image::Subresource,
    ) -> image::SubresourceFootprint {
        let mut num_rows = 0;
        let mut total_bytes = 0;
        let _desc = match image {
            r::Image::Bound(i) => i.descriptor,
            r::Image::Unbound(i) => i.desc,
        };
        let footprint = {
            let mut footprint = mem::zeroed();
            self.raw.GetCopyableFootprints(
                image.get_desc(),
                image.calc_subresource(sub.level as _, sub.layer as _, 0),
                1,
                0,
                &mut footprint,
                &mut num_rows,
                ptr::null_mut(), // row size in bytes
                &mut total_bytes,
            );
            footprint
        };

        let depth_pitch = (footprint.Footprint.RowPitch * num_rows) as buffer::Offset;
        let array_pitch = footprint.Footprint.Depth as buffer::Offset * depth_pitch;
        image::SubresourceFootprint {
            slice: footprint.Offset .. footprint.Offset + total_bytes,
            row_pitch: footprint.Footprint.RowPitch as _,
            depth_pitch,
            array_pitch,
        }
    }

    unsafe fn bind_image_memory(
        &self,
        memory: &r::Memory,
        offset: u64,
        image: &mut r::Image,
    ) -> Result<(), d::BindError> {
        use self::image::Usage;

        let image_unbound = *image.expect_unbound();
        if image_unbound.requirements.type_mask & (1 << memory.type_id) == 0 {
            error!(
                "Bind memory failure: supported mask 0x{:x}, given id {}",
                image_unbound.requirements.type_mask, memory.type_id
            );
            return Err(d::BindError::WrongMemory);
        }
        if offset + image_unbound.requirements.size > memory.size {
            return Err(d::BindError::OutOfBounds);
        }

        let mut resource = native::Resource::null();
        let num_layers = image_unbound.kind.num_layers();

        assert_eq!(
            winerror::S_OK,
            self.raw.clone().CreatePlacedResource(
                memory.heap.as_mut_ptr(),
                offset,
                &image_unbound.desc,
                d3d12::D3D12_RESOURCE_STATE_COMMON,
                ptr::null(),
                &d3d12::ID3D12Resource::uuidof(),
                resource.mut_void(),
            )
        );

        let info = ViewInfo {
            resource,
            kind: image_unbound.kind,
            caps: image::ViewCapabilities::empty(),
            view_kind: match image_unbound.kind {
                image::Kind::D1(..) => image::ViewKind::D1Array,
                image::Kind::D2(..) => image::ViewKind::D2Array,
                image::Kind::D3(..) => image::ViewKind::D3,
            },
            format: image_unbound.desc.Format,
            component_mapping: IDENTITY_MAPPING,
            range: image::SubresourceRange {
                aspects: Aspects::empty(),
                levels: 0 .. 0,
                layers: 0 .. 0,
            },
        };

        //TODO: the clear_Xv is incomplete. We should support clearing images created without XXX_ATTACHMENT usage.
        // for this, we need to check the format and force the `RENDER_TARGET` flag behind the user's back
        // if the format supports being rendered into, allowing us to create clear_Xv
        let format_properties = self
            .format_properties
            .get(image_unbound.format as usize)
            .properties;
        let props = match image_unbound.tiling {
            image::Tiling::Optimal => format_properties.optimal_tiling,
            image::Tiling::Linear => format_properties.linear_tiling,
        };
        let can_clear_color = image_unbound
            .usage
            .intersects(Usage::TRANSFER_DST | Usage::COLOR_ATTACHMENT)
            && props.contains(format::ImageFeature::COLOR_ATTACHMENT);
        let can_clear_depth = image_unbound
            .usage
            .intersects(Usage::TRANSFER_DST | Usage::DEPTH_STENCIL_ATTACHMENT)
            && props.contains(format::ImageFeature::DEPTH_STENCIL_ATTACHMENT);
        let aspects = image_unbound.format.surface_desc().aspects;

        *image = r::Image::Bound(r::ImageBound {
            resource: resource,
            place: r::Place::Heap {
                raw: memory.heap.clone(),
                offset,
            },
            surface_type: image_unbound.format.base_format().0,
            kind: image_unbound.kind,
            usage: image_unbound.usage,
            default_view_format: image_unbound.view_format,
            view_caps: image_unbound.view_caps,
            descriptor: image_unbound.desc,
            bytes_per_block: image_unbound.bytes_per_block,
            block_dim: image_unbound.block_dim,
            clear_cv: if aspects.contains(Aspects::COLOR) && can_clear_color {
                let format = image_unbound.view_format.unwrap();
                (0 .. num_layers)
                    .map(|layer| {
                        self.view_image_as_render_target(ViewInfo {
                            format,
                            range: image::SubresourceRange {
                                aspects: Aspects::COLOR,
                                levels: 0 .. 1, //TODO?
                                layers: layer .. layer + 1,
                            },
                            ..info.clone()
                        })
                        .unwrap()
                    })
                    .collect()
            } else {
                Vec::new()
            },
            clear_dv: if aspects.contains(Aspects::DEPTH) && can_clear_depth {
                let format = image_unbound.dsv_format.unwrap();
                (0 .. num_layers)
                    .map(|layer| {
                        self.view_image_as_depth_stencil(ViewInfo {
                            format,
                            range: image::SubresourceRange {
                                aspects: Aspects::DEPTH,
                                levels: 0 .. 1, //TODO?
                                layers: layer .. layer + 1,
                            },
                            ..info.clone()
                        })
                        .unwrap()
                    })
                    .collect()
            } else {
                Vec::new()
            },
            clear_sv: if aspects.contains(Aspects::STENCIL) && can_clear_depth {
                let format = image_unbound.dsv_format.unwrap();
                (0 .. num_layers)
                    .map(|layer| {
                        self.view_image_as_depth_stencil(ViewInfo {
                            format,
                            range: image::SubresourceRange {
                                aspects: Aspects::STENCIL,
                                levels: 0 .. 1, //TODO?
                                layers: layer .. layer + 1,
                            },
                            ..info.clone()
                        })
                        .unwrap()
                    })
                    .collect()
            } else {
                Vec::new()
            },
            requirements: image_unbound.requirements,
        });

        Ok(())
    }

    unsafe fn create_image_view(
        &self,
        image: &r::Image,
        view_kind: image::ViewKind,
        format: format::Format,
        swizzle: format::Swizzle,
        range: image::SubresourceRange,
    ) -> Result<r::ImageView, image::ViewError> {
        let image = image.expect_bound();
        let is_array = image.kind.num_layers() > 1;
        let mip_levels = (range.levels.start, range.levels.end);
        let layers = (range.layers.start, range.layers.end);

        let info = ViewInfo {
            resource: image.resource,
            kind: image.kind,
            caps: image.view_caps,
            // D3D12 doesn't allow looking at a single slice of an array as a non-array
            view_kind: if is_array && view_kind == image::ViewKind::D2 {
                image::ViewKind::D2Array
            } else if is_array && view_kind == image::ViewKind::D1 {
                image::ViewKind::D1Array
            } else {
                view_kind
            },
            format: conv::map_format(format).ok_or(image::ViewError::BadFormat(format))?,
            component_mapping: conv::map_swizzle(swizzle),
            range,
        };

        //Note: we allow RTV/DSV/SRV/UAV views to fail to be created here,
        // because we don't know if the user will even need to use them.

        Ok(r::ImageView {
            resource: image.resource,
            handle_srv: if image
                .usage
                .intersects(image::Usage::SAMPLED | image::Usage::INPUT_ATTACHMENT)
            {
                self.view_image_as_shader_resource(info.clone()).ok()
            } else {
                None
            },
            handle_rtv: if image.usage.contains(image::Usage::COLOR_ATTACHMENT) {
                self.view_image_as_render_target(info.clone()).ok()
            } else {
                None
            },
            handle_uav: if image.usage.contains(image::Usage::STORAGE) {
                self.view_image_as_storage(info.clone()).ok()
            } else {
                None
            },
            handle_dsv: if image.usage.contains(image::Usage::DEPTH_STENCIL_ATTACHMENT) {
                match conv::map_format_dsv(format.base_format().0) {
                    Some(dsv_format) => self
                        .view_image_as_depth_stencil(ViewInfo {
                            format: dsv_format,
                            ..info
                        })
                        .ok(),
                    None => None,
                }
            } else {
                None
            },
            dxgi_format: image.default_view_format.unwrap(),
            num_levels: image.descriptor.MipLevels as image::Level,
            mip_levels,
            layers,
            kind: info.kind,
        })
    }

    unsafe fn create_sampler(
        &self,
        info: &image::SamplerDesc,
    ) -> Result<r::Sampler, d::AllocationError> {
        assert!(info.normalized);
        let handle = self.sampler_pool.lock().unwrap().alloc_handle();

        let op = match info.comparison {
            Some(_) => d3d12::D3D12_FILTER_REDUCTION_TYPE_COMPARISON,
            None => d3d12::D3D12_FILTER_REDUCTION_TYPE_STANDARD,
        };
        self.raw.create_sampler(
            handle,
            conv::map_filter(
                info.mag_filter,
                info.min_filter,
                info.mip_filter,
                op,
                info.anisotropic,
            ),
            [
                conv::map_wrap(info.wrap_mode.0),
                conv::map_wrap(info.wrap_mode.1),
                conv::map_wrap(info.wrap_mode.2),
            ],
            info.lod_bias.0,
            match info.anisotropic {
                image::Anisotropic::On(max) => max as _, // TODO: check support here?
                image::Anisotropic::Off => 0,
            },
            conv::map_comparison(info.comparison.unwrap_or(pso::Comparison::Always)),
            info.border.into(),
            info.lod_range.start.0 .. info.lod_range.end.0,
        );

        Ok(r::Sampler { handle })
    }

    unsafe fn create_descriptor_pool<I>(
        &self,
        max_sets: usize,
        descriptor_pools: I,
        _flags: pso::DescriptorPoolCreateFlags,
    ) -> Result<r::DescriptorPool, d::OutOfMemory>
    where
        I: IntoIterator,
        I::Item: Borrow<pso::DescriptorRangeDesc>,
    {
        // Descriptor pools are implemented as slices of the global descriptor heaps.
        // A descriptor pool will occupy a contiguous space in each heap (CBV/SRV/UAV and Sampler) depending
        // on the total requested amount of descriptors.

        let mut num_srv_cbv_uav = 0;
        let mut num_samplers = 0;

        let descriptor_pools = descriptor_pools
            .into_iter()
            .map(|desc| *desc.borrow())
            .collect::<Vec<_>>();

        info!("create_descriptor_pool with {} max sets", max_sets);
        for desc in &descriptor_pools {
            let content = r::DescriptorContent::from(desc.ty);
            debug!("\tcontent {:?}", content);
            if content.contains(r::DescriptorContent::CBV) {
                num_srv_cbv_uav += desc.count;
            }
            if content.contains(r::DescriptorContent::SRV) {
                num_srv_cbv_uav += desc.count;
            }
            if content.contains(r::DescriptorContent::UAV) {
                num_srv_cbv_uav += desc.count;
            }
            if content.contains(r::DescriptorContent::SAMPLER) {
                num_samplers += desc.count;
            }
        }

        info!(
            "total {} views and {} samplers",
            num_srv_cbv_uav, num_samplers
        );

        // Allocate slices of the global GPU descriptor heaps.
        let heap_srv_cbv_uav = {
            let mut heap_srv_cbv_uav = self.heap_srv_cbv_uav.lock().unwrap();

            let range = match num_srv_cbv_uav {
                0 => 0 .. 0,
                _ => heap_srv_cbv_uav
                    .range_allocator
                    .allocate_range(num_srv_cbv_uav as _)
                    .unwrap(), // TODO: error/resize
            };

            r::DescriptorHeapSlice {
                heap: heap_srv_cbv_uav.raw.clone(),
                handle_size: heap_srv_cbv_uav.handle_size as _,
                range_allocator: RangeAllocator::new(range),
                start: heap_srv_cbv_uav.start,
            }
        };

        let heap_sampler = {
            let mut heap_sampler = self.heap_sampler.lock().unwrap();

            let range = match num_samplers {
                0 => 0 .. 0,
                _ => heap_sampler
                    .range_allocator
                    .allocate_range(num_samplers as _)
                    .unwrap(), // TODO: error/resize
            };

            r::DescriptorHeapSlice {
                heap: heap_sampler.raw.clone(),
                handle_size: heap_sampler.handle_size as _,
                range_allocator: RangeAllocator::new(range),
                start: heap_sampler.start,
            }
        };

        Ok(r::DescriptorPool {
            heap_srv_cbv_uav,
            heap_sampler,
            pools: descriptor_pools,
            max_size: max_sets as _,
        })
    }

    unsafe fn create_descriptor_set_layout<I, J>(
        &self,
        bindings: I,
        _immutable_samplers: J,
    ) -> Result<r::DescriptorSetLayout, d::OutOfMemory>
    where
        I: IntoIterator,
        I::Item: Borrow<pso::DescriptorSetLayoutBinding>,
        J: IntoIterator,
        J::Item: Borrow<r::Sampler>,
    {
        Ok(r::DescriptorSetLayout {
            bindings: bindings.into_iter().map(|b| b.borrow().clone()).collect(),
        })
    }

    unsafe fn write_descriptor_sets<'a, I, J>(&self, write_iter: I)
    where
        I: IntoIterator<Item = pso::DescriptorSetWrite<'a, B, J>>,
        J: IntoIterator,
        J::Item: Borrow<pso::Descriptor<'a, B>>,
    {
        let mut descriptor_update_pools = self.descriptor_update_pools.lock().unwrap();
        let mut update_pool_index = 0;

        //TODO: combine destination ranges
        let mut dst_samplers = Vec::new();
        let mut dst_views = Vec::new();
        let mut src_samplers = Vec::new();
        let mut src_views = Vec::new();
        let mut num_samplers = Vec::new();
        let mut num_views = Vec::new();
        debug!("write_descriptor_sets");

        for write in write_iter {
            let mut offset = write.array_offset as u64;
            let mut target_binding = write.binding as usize;
            let mut bind_info = &write.set.binding_infos[target_binding];
            debug!(
                "\t{:?} binding {} array offset {}",
                bind_info, target_binding, offset
            );
            for descriptor in write.descriptors {
                // spill over the writes onto the next binding
                while offset >= bind_info.count {
                    assert_eq!(offset, bind_info.count);
                    target_binding += 1;
                    bind_info = &write.set.binding_infos[target_binding];
                    offset = 0;
                }
                let mut src_cbv = None;
                let mut src_srv = None;
                let mut src_uav = None;
                let mut src_sampler = None;

                match *descriptor.borrow() {
                    pso::Descriptor::Buffer(buffer, ref range) => {
                        let buffer = buffer.expect_bound();

                        if bind_info.content.is_dynamic() {
                            // Root Descriptor
                            let buffer_offset = range.start.unwrap_or(0);
                            let buffer_address = (*buffer.resource).GetGPUVirtualAddress();

                            // Descriptor sets need to be externally synchronized according to specification
                            let dynamic_descriptors = &mut *bind_info.dynamic_descriptors.get();
                            dynamic_descriptors[offset as usize].gpu_buffer_location = buffer_address + buffer_offset;
                        } else {
                            // Descriptor table
                            if update_pool_index == descriptor_update_pools.len() {
                                let max_size = 1u64 << 12; //arbitrary
                                descriptor_update_pools.push(descriptors_cpu::HeapLinear::new(
                                    self.raw,
                                    native::DescriptorHeapType::CbvSrvUav,
                                    max_size as _,
                                ));
                            }
                            let mut heap = descriptor_update_pools.last_mut().unwrap();
                            let start = range.start.unwrap_or(0);
                            let end = range.end.unwrap_or(buffer.requirements.size as _);

                            if bind_info.content.contains(r::DescriptorContent::CBV) {
                                // Making the size field of buffer requirements for uniform
                                // buffers a multiple of 256 and setting the required offset
                                // alignment to 256 allows us to patch the size here.
                                // We can always enforce the size to be aligned to 256 for
                                // CBVs without going out-of-bounds.
                                let size = ((end - start) + 255) & !255;
                                let desc = d3d12::D3D12_CONSTANT_BUFFER_VIEW_DESC {
                                    BufferLocation: (*buffer.resource).GetGPUVirtualAddress() + start,
                                    SizeInBytes: size as _,
                                };
                                let handle = heap.alloc_handle();
                                self.raw.CreateConstantBufferView(&desc, handle);
                                src_cbv = Some(handle);
                            }
                            if bind_info.content.contains(r::DescriptorContent::SRV) {
                                assert_eq!((end - start) % 4, 0);
                                let mut desc = d3d12::D3D12_SHADER_RESOURCE_VIEW_DESC {
                                    Format: dxgiformat::DXGI_FORMAT_R32_TYPELESS,
                                    Shader4ComponentMapping: IDENTITY_MAPPING,
                                    ViewDimension: d3d12::D3D12_SRV_DIMENSION_BUFFER,
                                    u: mem::zeroed(),
                                };
                                *desc.u.Buffer_mut() = d3d12::D3D12_BUFFER_SRV {
                                    FirstElement: start as _,
                                    NumElements: ((end - start) / 4) as _,
                                    StructureByteStride: 0,
                                    Flags: d3d12::D3D12_BUFFER_SRV_FLAG_RAW,
                                };
                                let handle = heap.alloc_handle();
                                self.raw.CreateShaderResourceView(
                                    buffer.resource.as_mut_ptr(),
                                    &desc,
                                    handle,
                                );
                                src_srv = Some(handle);
                            }
                            if bind_info.content.contains(r::DescriptorContent::UAV) {
                                assert_eq!((end - start) % 4, 0);
                                let mut desc = d3d12::D3D12_UNORDERED_ACCESS_VIEW_DESC {
                                    Format: dxgiformat::DXGI_FORMAT_R32_TYPELESS,
                                    ViewDimension: d3d12::D3D12_UAV_DIMENSION_BUFFER,
                                    u: mem::zeroed(),
                                };
                                *desc.u.Buffer_mut() = d3d12::D3D12_BUFFER_UAV {
                                    FirstElement: start as _,
                                    NumElements: ((end - start) / 4) as _,
                                    StructureByteStride: 0,
                                    CounterOffsetInBytes: 0,
                                    Flags: d3d12::D3D12_BUFFER_UAV_FLAG_RAW,
                                };
                                if heap.is_full() {
                                    // pool is full, move to the next one
                                    update_pool_index += 1;
                                    let max_size = 1u64 << 12; //arbitrary
                                    descriptor_update_pools.push(descriptors_cpu::HeapLinear::new(
                                        self.raw,
                                        native::DescriptorHeapType::CbvSrvUav,
                                        max_size as _,
                                    ));
                                    heap = descriptor_update_pools.last_mut().unwrap();
                                }
                                let handle = heap.alloc_handle();
                                self.raw.CreateUnorderedAccessView(
                                    buffer.resource.as_mut_ptr(),
                                    ptr::null_mut(),
                                    &desc,
                                    handle,
                                );
                                src_uav = Some(handle);
                            }

                            // always leave this block of code prepared
                            if heap.is_full() {
                                // pool is full, move to the next one
                                update_pool_index += 1;
                            }
                        }
                    }
                    pso::Descriptor::Image(image, _layout) => {
                        if bind_info.content.contains(r::DescriptorContent::SRV) {
                            src_srv = image.handle_srv;
                        }
                        if bind_info.content.contains(r::DescriptorContent::UAV) {
                            src_uav = image.handle_uav;
                        }
                    }
                    pso::Descriptor::CombinedImageSampler(image, _layout, sampler) => {
                        src_srv = image.handle_srv;
                        src_sampler = Some(sampler.handle);
                    }
                    pso::Descriptor::Sampler(sampler) => {
                        src_sampler = Some(sampler.handle);
                    }
                    pso::Descriptor::UniformTexelBuffer(buffer_view) => {
                        let handle = buffer_view.handle_srv;
                        src_srv = Some(handle);
                        if handle.ptr == 0 {
                            error!("SRV handle of the uniform texel buffer is zero (not supported by specified format).");
                        }
                    }
                    pso::Descriptor::StorageTexelBuffer(buffer_view) => {
                        if bind_info.content.contains(r::DescriptorContent::SRV) {
                            let handle = buffer_view.handle_srv;
                            src_srv = Some(handle);
                            if handle.ptr == 0 {
                                error!("SRV handle of the storage texel buffer is zero (not supported by specified format).");
                            }
                        }
                        if bind_info.content.contains(r::DescriptorContent::UAV) {
                            let handle = buffer_view.handle_uav;
                            src_uav = Some(handle);
                            if handle.ptr == 0 {
                                error!("UAV handle of the storage texel buffer is zero (not supported by specified format).");
                            }
                        }
                    }
                }

                if let Some(handle) = src_cbv {
                    trace!("\tcbv offset {}", offset);
                    src_views.push(handle);
                    dst_views.push(bind_info.view_range.as_ref().unwrap().at(offset));
                    num_views.push(1);
                }
                if let Some(handle) = src_srv {
                    trace!("\tsrv offset {}", offset);
                    src_views.push(handle);
                    dst_views.push(bind_info.view_range.as_ref().unwrap().at(offset));
                    num_views.push(1);
                }
                if let Some(handle) = src_uav {
                    let uav_offset = if bind_info.content.contains(r::DescriptorContent::SRV) {
                        bind_info.count + offset
                    } else {
                        offset
                    };
                    trace!("\tuav offset {}", uav_offset);
                    src_views.push(handle);
                    dst_views.push(bind_info.view_range.as_ref().unwrap().at(uav_offset));
                    num_views.push(1);
                }
                if let Some(handle) = src_sampler {
                    trace!("\tsampler offset {}", offset);
                    src_samplers.push(handle);
                    dst_samplers.push(bind_info.sampler_range.as_ref().unwrap().at(offset));
                    num_samplers.push(1);
                }

                offset += 1;
            }
        }

        if !num_views.is_empty() {
            self.raw.clone().CopyDescriptors(
                dst_views.len() as u32,
                dst_views.as_ptr(),
                num_views.as_ptr(),
                src_views.len() as u32,
                src_views.as_ptr(),
                num_views.as_ptr(),
                d3d12::D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
            );
        }
        if !num_samplers.is_empty() {
            self.raw.clone().CopyDescriptors(
                dst_samplers.len() as u32,
                dst_samplers.as_ptr(),
                num_samplers.as_ptr(),
                src_samplers.len() as u32,
                src_samplers.as_ptr(),
                num_samplers.as_ptr(),
                d3d12::D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
            );
        }

        // reset the temporary CPU-size descriptor pools
        for buffer_desc_pool in descriptor_update_pools.iter_mut() {
            buffer_desc_pool.clear();
        }
    }

    unsafe fn copy_descriptor_sets<'a, I>(&self, copy_iter: I)
    where
        I: IntoIterator,
        I::Item: Borrow<pso::DescriptorSetCopy<'a, B>>,
    {
        let mut dst_samplers = Vec::new();
        let mut dst_views = Vec::new();
        let mut src_samplers = Vec::new();
        let mut src_views = Vec::new();
        let mut num_samplers = Vec::new();
        let mut num_views = Vec::new();

        for copy_wrap in copy_iter {
            let copy = copy_wrap.borrow();
            let src_info = &copy.src_set.binding_infos[copy.src_binding as usize];
            let dst_info = &copy.dst_set.binding_infos[copy.dst_binding as usize];
            if let (Some(src_range), Some(dst_range)) =
                (src_info.view_range.as_ref(), dst_info.view_range.as_ref())
            {
                assert!(copy.src_array_offset + copy.count <= src_range.count as usize);
                assert!(copy.dst_array_offset + copy.count <= dst_range.count as usize);
                src_views.push(src_range.at(copy.src_array_offset as _));
                dst_views.push(dst_range.at(copy.dst_array_offset as _));
                num_views.push(copy.count as u32);

                if (src_info.content & dst_info.content)
                    .contains(r::DescriptorContent::SRV | r::DescriptorContent::UAV)
                {
                    assert!(
                        src_info.count as usize + copy.src_array_offset + copy.count
                            <= src_range.count as usize
                    );
                    assert!(
                        dst_info.count as usize + copy.dst_array_offset + copy.count
                            <= dst_range.count as usize
                    );
                    src_views.push(src_range.at(src_info.count + copy.src_array_offset as u64));
                    dst_views.push(dst_range.at(dst_info.count + copy.dst_array_offset as u64));
                    num_views.push(copy.count as u32);
                }
            }
            if let (Some(src_range), Some(dst_range)) = (
                src_info.sampler_range.as_ref(),
                dst_info.sampler_range.as_ref(),
            ) {
                assert!(copy.src_array_offset + copy.count <= src_range.count as usize);
                assert!(copy.dst_array_offset + copy.count <= dst_range.count as usize);
                src_samplers.push(src_range.at(copy.src_array_offset as _));
                dst_samplers.push(dst_range.at(copy.dst_array_offset as _));
                num_samplers.push(copy.count as u32);
            }
        }

        if !num_views.is_empty() {
            self.raw.clone().CopyDescriptors(
                dst_views.len() as u32,
                dst_views.as_ptr(),
                num_views.as_ptr(),
                src_views.len() as u32,
                src_views.as_ptr(),
                num_views.as_ptr(),
                d3d12::D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
            );
        }
        if !num_samplers.is_empty() {
            self.raw.clone().CopyDescriptors(
                dst_samplers.len() as u32,
                dst_samplers.as_ptr(),
                num_samplers.as_ptr(),
                src_samplers.len() as u32,
                src_samplers.as_ptr(),
                num_samplers.as_ptr(),
                d3d12::D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
            );
        }
    }

    unsafe fn map_memory<R>(&self, memory: &r::Memory, range: R) -> Result<*mut u8, d::MapError>
    where
        R: RangeArg<u64>,
    {
        if let Some(mem) = memory.resource {
            let start = range.start().unwrap_or(&0);
            let end = range.end().unwrap_or(&memory.size);
            assert!(start <= end);

            let mut ptr = ptr::null_mut();
            assert_eq!(
                winerror::S_OK,
                (*mem).Map(0, &d3d12::D3D12_RANGE { Begin: 0, End: 0 }, &mut ptr)
            );
            ptr = ptr.offset(*start as _);
            Ok(ptr as *mut _)
        } else {
            panic!("Memory not created with a memory type exposing `CPU_VISIBLE`.")
        }
    }

    unsafe fn unmap_memory(&self, memory: &r::Memory) {
        if let Some(mem) = memory.resource {
            (*mem).Unmap(0, &d3d12::D3D12_RANGE { Begin: 0, End: 0 });
        }
    }

    unsafe fn flush_mapped_memory_ranges<'a, I, R>(&self, ranges: I) -> Result<(), d::OutOfMemory>
    where
        I: IntoIterator,
        I::Item: Borrow<(&'a r::Memory, R)>,
        R: RangeArg<u64>,
    {
        for range in ranges {
            let &(ref memory, ref range) = range.borrow();
            if let Some(mem) = memory.resource {
                // map and immediately unmap, hoping that dx12 drivers internally cache
                // currently mapped buffers.
                assert_eq!(
                    winerror::S_OK,
                    (*mem).Map(0, &d3d12::D3D12_RANGE { Begin: 0, End: 0 }, ptr::null_mut())
                );

                let start = *range.start().unwrap_or(&0);
                let end = *range.end().unwrap_or(&memory.size); // TODO: only need to be end of current mapping

                (*mem).Unmap(
                    0,
                    &d3d12::D3D12_RANGE {
                        Begin: start as _,
                        End: end as _,
                    },
                );
            }
        }

        Ok(())
    }

    unsafe fn invalidate_mapped_memory_ranges<'a, I, R>(
        &self,
        ranges: I,
    ) -> Result<(), d::OutOfMemory>
    where
        I: IntoIterator,
        I::Item: Borrow<(&'a r::Memory, R)>,
        R: RangeArg<u64>,
    {
        for range in ranges {
            let &(ref memory, ref range) = range.borrow();
            if let Some(mem) = memory.resource {
                let start = *range.start().unwrap_or(&0);
                let end = *range.end().unwrap_or(&memory.size); // TODO: only need to be end of current mapping

                // map and immediately unmap, hoping that dx12 drivers internally cache
                // currently mapped buffers.
                assert_eq!(
                    winerror::S_OK,
                    (*mem).Map(
                        0,
                        &d3d12::D3D12_RANGE {
                            Begin: start as _,
                            End: end as _,
                        },
                        ptr::null_mut(),
                    )
                );

                (*mem).Unmap(0, &d3d12::D3D12_RANGE { Begin: 0, End: 0 });
            }
        }

        Ok(())
    }

    fn create_semaphore(&self) -> Result<r::Semaphore, d::OutOfMemory> {
        let fence = self.create_fence(false)?;
        Ok(r::Semaphore { raw: fence.raw })
    }

    fn create_fence(&self, signalled: bool) -> Result<r::Fence, d::OutOfMemory> {
        Ok(r::Fence {
            raw: self.create_raw_fence(signalled),
        })
    }

    unsafe fn reset_fence(&self, fence: &r::Fence) -> Result<(), d::OutOfMemory> {
        assert_eq!(winerror::S_OK, fence.raw.signal(0));
        Ok(())
    }

    unsafe fn wait_for_fences<I>(
        &self,
        fences: I,
        wait: d::WaitFor,
        timeout_ns: u64,
    ) -> Result<bool, d::OomOrDeviceLost>
    where
        I: IntoIterator,
        I::Item: Borrow<r::Fence>,
    {
        let fences = fences.into_iter().collect::<Vec<_>>();
        let mut events = self.events.lock().unwrap();
        for _ in events.len() .. fences.len() {
            events.push(native::Event::create(false, false));
        }

        for (&event, fence) in events.iter().zip(fences.iter()) {
            synchapi::ResetEvent(event.0);
            assert_eq!(
                winerror::S_OK,
                fence.borrow().raw.set_event_on_completion(event, 1)
            );
        }

        let all = match wait {
            d::WaitFor::Any => FALSE,
            d::WaitFor::All => TRUE,
        };

        let hr = {
            // This block handles overflow when converting to u32 and always rounds up
            // The Vulkan specification allows to wait more than specified
            let timeout_ms = {
                if timeout_ns > (<u32>::max_value() as u64) * 1_000_000 {
                    <u32>::max_value()
                } else {
                    ((timeout_ns + 999_999) / 1_000_000) as u32
                }
            };

            synchapi::WaitForMultipleObjects(
                fences.len() as u32,
                events.as_ptr() as *const _,
                all,
                timeout_ms,
            )
        };

        const WAIT_OBJECT_LAST: u32 = winbase::WAIT_OBJECT_0 + winnt::MAXIMUM_WAIT_OBJECTS;
        const WAIT_ABANDONED_LAST: u32 = winbase::WAIT_ABANDONED_0 + winnt::MAXIMUM_WAIT_OBJECTS;
        match hr {
            winbase::WAIT_OBJECT_0 ..= WAIT_OBJECT_LAST => Ok(true),
            winbase::WAIT_ABANDONED_0 ..= WAIT_ABANDONED_LAST => Ok(true), //TODO?
            winerror::WAIT_TIMEOUT => Ok(false),
            _ => panic!("Unexpected wait status 0x{:X}", hr),
        }
    }

    unsafe fn get_fence_status(&self, fence: &r::Fence) -> Result<bool, d::DeviceLost> {
        match fence.raw.GetCompletedValue() {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(d::DeviceLost),
        }
    }

    fn create_event(&self) -> Result<(), d::OutOfMemory> {
        unimplemented!()
    }

    unsafe fn get_event_status(&self, _event: &()) -> Result<bool, d::OomOrDeviceLost> {
        unimplemented!()
    }

    unsafe fn set_event(&self, _event: &()) -> Result<(), d::OutOfMemory> {
        unimplemented!()
    }

    unsafe fn reset_event(&self, _event: &()) -> Result<(), d::OutOfMemory> {
        unimplemented!()
    }

    unsafe fn free_memory(&self, memory: r::Memory) {
        memory.heap.destroy();
        if let Some(buffer) = memory.resource {
            buffer.destroy();
        }
    }

    unsafe fn create_query_pool(
        &self,
        query_ty: query::Type,
        count: query::Id,
    ) -> Result<r::QueryPool, query::CreationError> {
        let heap_ty = match query_ty {
            query::Type::Occlusion => native::QueryHeapType::Occlusion,
            query::Type::PipelineStatistics(_) => native::QueryHeapType::PipelineStatistics,
            query::Type::Timestamp => native::QueryHeapType::Timestamp,
        };

        let (query_heap, hr) = self.raw.create_query_heap(heap_ty, count, 0);
        assert_eq!(winerror::S_OK, hr);

        Ok(r::QueryPool {
            raw: query_heap,
            ty: heap_ty,
        })
    }

    unsafe fn destroy_query_pool(&self, pool: r::QueryPool) {
        pool.raw.destroy();
    }

    unsafe fn get_query_pool_results(
        &self,
        _pool: &r::QueryPool,
        _queries: Range<query::Id>,
        _data: &mut [u8],
        _stride: buffer::Offset,
        _flags: query::ResultFlags,
    ) -> Result<bool, d::OomOrDeviceLost> {
        unimplemented!()
    }

    unsafe fn destroy_shader_module(&self, shader_lib: r::ShaderModule) {
        if let r::ShaderModule::Compiled(shaders) = shader_lib {
            for (_, blob) in shaders {
                blob.destroy();
            }
        }
    }

    unsafe fn destroy_render_pass(&self, _rp: r::RenderPass) {
        // Just drop
    }

    unsafe fn destroy_pipeline_layout(&self, layout: r::PipelineLayout) {
        layout.raw.destroy();
    }

    unsafe fn destroy_graphics_pipeline(&self, pipeline: r::GraphicsPipeline) {
        pipeline.raw.destroy();
    }

    unsafe fn destroy_compute_pipeline(&self, pipeline: r::ComputePipeline) {
        pipeline.raw.destroy();
    }

    unsafe fn destroy_framebuffer(&self, _fb: r::Framebuffer) {
        // Just drop
    }

    unsafe fn destroy_buffer(&self, buffer: r::Buffer) {
        match buffer {
            r::Buffer::Bound(buffer) => {
                buffer.resource.destroy();
            }
            r::Buffer::Unbound(_) => {}
        }
    }

    unsafe fn destroy_buffer_view(&self, _view: r::BufferView) {
        // empty
    }

    unsafe fn destroy_image(&self, image: r::Image) {
        match image {
            r::Image::Bound(image) => {
                image.resource.destroy();
            }
            r::Image::Unbound(_) => {}
        }
    }

    unsafe fn destroy_image_view(&self, _view: r::ImageView) {
        // Just drop
    }

    unsafe fn destroy_sampler(&self, _sampler: r::Sampler) {
        // Just drop
    }

    unsafe fn destroy_descriptor_pool(&self, _pool: r::DescriptorPool) {
        // Just drop
        // Allocated descriptor sets don't need to be freed beforehand.
    }

    unsafe fn destroy_descriptor_set_layout(&self, _layout: r::DescriptorSetLayout) {
        // Just drop
    }

    unsafe fn destroy_fence(&self, fence: r::Fence) {
        fence.raw.destroy();
    }

    unsafe fn destroy_semaphore(&self, semaphore: r::Semaphore) {
        semaphore.raw.destroy();
    }

    unsafe fn destroy_event(&self, _event: ()) {
        unimplemented!()
    }

    unsafe fn create_swapchain(
        &self,
        surface: &mut Surface,
        config: w::SwapchainConfig,
        old_swapchain: Option<Swapchain>,
    ) -> Result<(Swapchain, Vec<r::Image>), w::CreationError> {
        if let Some(old_swapchain) = old_swapchain {
            self.destroy_swapchain(old_swapchain);
        }

        let (swap_chain3, non_srgb_format) =
            self.create_swapchain_impl(&config, surface.wnd_handle, surface.factory)?;

        let swapchain = self.wrap_swapchain(swap_chain3, &config);

        let mut images = Vec::with_capacity(config.image_count as usize);
        for (i, &resource) in swapchain.resources.iter().enumerate() {
            let rtv_handle = swapchain.rtv_heap.at(i as _, 0).cpu;
            let surface_type = config.format.base_format().0;
            let format_desc = surface_type.desc();

            let bytes_per_block = (format_desc.bits / 8) as _;
            let block_dim = format_desc.dim;
            let kind = image::Kind::D2(config.extent.width, config.extent.height, 1, 1);

            images.push(r::Image::Bound(r::ImageBound {
                resource,
                place: r::Place::SwapChain,
                surface_type,
                kind,
                usage: config.image_usage,
                default_view_format: Some(non_srgb_format),
                view_caps: image::ViewCapabilities::empty(),
                descriptor: d3d12::D3D12_RESOURCE_DESC {
                    Dimension: d3d12::D3D12_RESOURCE_DIMENSION_TEXTURE2D,
                    Alignment: 0,
                    Width: config.extent.width as _,
                    Height: config.extent.height as _,
                    DepthOrArraySize: 1,
                    MipLevels: 1,
                    Format: non_srgb_format,
                    SampleDesc: dxgitype::DXGI_SAMPLE_DESC {
                        Count: 1,
                        Quality: 0,
                    },
                    Layout: d3d12::D3D12_TEXTURE_LAYOUT_UNKNOWN,
                    Flags: 0,
                },
                bytes_per_block,
                block_dim,
                clear_cv: vec![rtv_handle],
                clear_dv: Vec::new(),
                clear_sv: Vec::new(),
                // Dummy values, image is already bound
                requirements: memory::Requirements {
                    alignment: 1,
                    size: 1,
                    type_mask: MEM_TYPE_MASK,
                },
            }));
        }

        Ok((swapchain, images))
    }

    unsafe fn destroy_swapchain(&self, swapchain: Swapchain) {
        let inner = swapchain.release_resources();
        inner.destroy();
    }

    fn wait_idle(&self) -> Result<(), d::OutOfMemory> {
        for queue in &self.queues {
            queue.wait_idle()?;
        }
        Ok(())
    }

    unsafe fn set_image_name(&self, _image: &mut r::Image, _name: &str) {
        // TODO
    }

    unsafe fn set_buffer_name(&self, _buffer: &mut r::Buffer, _name: &str) {
        // TODO
    }

    unsafe fn set_command_buffer_name(
        &self,
        _command_buffer: &mut cmd::CommandBuffer,
        _name: &str
    ) {
        // TODO
    }

    unsafe fn set_semaphore_name(&self, _semaphore: &mut r::Semaphore, _name: &str) {
        // TODO
    }

    unsafe fn set_fence_name(&self, _fence: &mut r::Fence, _name: &str) {
        // TODO
    }

    unsafe fn set_framebuffer_name(&self, _framebuffer: &mut r::Framebuffer, _name: &str) {
        // TODO
    }

    unsafe fn set_render_pass_name(&self, _render_pass: &mut r::RenderPass, _name: &str) {
        // TODO
    }

    unsafe fn set_descriptor_set_name(&self, _descriptor_set: &mut r::DescriptorSet, _name: &str) {
        // TODO
    }

    unsafe fn set_descriptor_set_layout_name(
        &self,
        _descriptor_set_layout: &mut r::DescriptorSetLayout,
        _name: &str,
    ) {
        // TODO
    }
}

#[test]
fn test_identity_mapping() {
    assert_eq!(conv::map_swizzle(format::Swizzle::NO), IDENTITY_MAPPING);
}