1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
|
// The Vulkan-backed ID3D12Device behind the Xbox-ABI device proxy, end to end, on a GPU. Everything goes through
// the proxy the way the game will call it: device methods (forwarded slots), descriptor heaps from slot 14 with
// start handles read in the Xbox shape (RAX), a texture from slot 76 with the Xbox layout word, committed
// resources, views, command lists, a queue, a fence and query heaps. Results are read back through D3D12 itself
// (CopyTextureRegion into a readback heap, then Map). GetCopyableFootprints, GetResourceAllocationInfo,
// GetResourceTiling, copies (d3d12_copy_compare.h), CheckFeatureSupport's error paths, fence semantics and the
// HRESULTs of invalid arguments to resource, heap, queue and fence creation and Map are compared with the host PC
// runtime's
// (LEONIDA_D3D49_VERBOSE=1 prints every argument case). WriteToSubresource/ReadFromSubresource run on
// devices of their own, once per copy path. No shaders or game data. Set LEONIDA_VK_VALIDATION=1 to run
// under VK_LAYER_KHRONOS_validation; any validation error fails the test.
#include "vk_test_device.h"
#include "../shims/d3d12_com.h"
#include "../shims/d3d12_feature_compat.h"
#include "../shims/d3d12_xs_device.h"
#include "d3d12_copy_compare.h" // D3D-46: copies compared with the host runtime
#include "d3d12_depth24_compare.h" // D24 clears compared with the host runtime
#include "../src/vulkan_memory.h" // D3D-96: resizable_bar
#include <d3d12video.h>
#include <dxgi1_4.h>
#include <dxgi1_4.h>
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#define EXPECT(condition) do { \
if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
} while (0)
namespace {
// The Xbox call shapes measured in the prior art (docs/d3d12_vulkan.md section 7).
using XboxHeapHandleFn = std::uint64_t(WINAPI*)(void* heap); // heap slots 9/10: 8-byte handle in RAX
using XboxCreateResourceFn = HRESULT(WINAPI*)(void* device, const D3D12_HEAP_PROPERTIES*, D3D12_HEAP_FLAGS,
const D3D12_RESOURCE_DESC*, D3D12_RESOURCE_STATES,
const D3D12_CLEAR_VALUE*, const IID*, void**); // device slot 76
template<class T>
struct Ref {
T* p = nullptr;
~Ref() { reset(); }
void reset() {
if (p) p->Release();
p = nullptr;
}
T* operator->() const { return p; }
};
void* slot(void* object, unsigned index) { return (*reinterpret_cast<void***>(object))[index]; }
D3D12_RESOURCE_DESC texture_desc(UINT width, UINT height, DXGI_FORMAT format, D3D12_RESOURCE_FLAGS flags,
UINT16 mips = 1, UINT16 layers = 1) {
D3D12_RESOURCE_DESC desc{};
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
desc.Width = width;
desc.Height = height;
desc.DepthOrArraySize = layers;
desc.MipLevels = mips;
desc.Format = format;
desc.SampleDesc.Count = 1;
desc.Flags = flags;
return desc;
}
D3D12_RESOURCE_DESC buffer_desc(UINT64 bytes) {
D3D12_RESOURCE_DESC desc{};
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
desc.Width = bytes;
desc.Height = 1;
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.SampleDesc.Count = 1;
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
return desc;
}
D3D12_RESOURCE_DESC volume_desc(UINT16 mips) {
auto desc = texture_desc(8, 6, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, mips, 16);
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
return desc;
}
D3D12_RESOURCE_DESC line_desc(UINT width, UINT16 mips, UINT16 layers, DXGI_FORMAT format) {
auto desc = texture_desc(width, 1, format, D3D12_RESOURCE_FLAG_NONE, mips, layers);
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE1D;
return desc;
}
D3D12_TEXTURE_COPY_LOCATION subresource(ID3D12Resource* resource, UINT index) {
D3D12_TEXTURE_COPY_LOCATION location{};
location.pResource = resource;
location.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
location.SubresourceIndex = index;
return location;
}
D3D12_TEXTURE_COPY_LOCATION footprint(ID3D12Resource* buffer, const D3D12_PLACED_SUBRESOURCE_FOOTPRINT& placed) {
D3D12_TEXTURE_COPY_LOCATION location{};
location.pResource = buffer;
location.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
location.PlacedFootprint = placed;
return location;
}
// Places one subresource of `resource` in a buffer at the next aligned offset after `cursor`.
D3D12_PLACED_SUBRESOURCE_FOOTPRINT place(ID3D12Device* device, ID3D12Resource* resource, UINT index,
UINT64& cursor) {
const D3D12_RESOURCE_DESC desc = resource->GetDesc();
D3D12_PLACED_SUBRESOURCE_FOOTPRINT layout{};
UINT64 total = 0;
const UINT64 base = (cursor + D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT - 1) / D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT *
D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT;
device->GetCopyableFootprints(&desc, index, 1, base, &layout, nullptr, nullptr, &total);
cursor = base + total;
return layout;
}
std::array<std::uint8_t, 4> pattern(UINT x, UINT y) {
return {std::uint8_t(x * 16 + y), std::uint8_t(255 - y * 16), std::uint8_t(x * y), 255};
}
D3D12_RESOURCE_BARRIER transition(ID3D12Resource* resource, D3D12_RESOURCE_STATES before,
D3D12_RESOURCE_STATES after) {
D3D12_RESOURCE_BARRIER barrier{};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Transition.pResource = resource;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
barrier.Transition.StateBefore = before;
barrier.Transition.StateAfter = after;
return barrier;
}
std::uint8_t texel_byte(UINT x, UINT y, UINT z, UINT i) { return std::uint8_t(x * 31 + y * 7 + z * 61 + i * 3 + 1); }
// WriteToSubresource/ReadFromSubresource on `device`. Textures in a CPU-visible custom heap copy on the host when the
// device has VK_EXT_host_image_copy, through a staging submit when it doesn't or when the driver gave the
// host-transfer image no memory type; main runs this once per path, three paths. Covers
// zero-filled first reads, boxes with pitches unlike the texture's, a mip, a volume, BC1 blocks, a default-heap
// texture (always staged), the rejected calls, and both directions between the CPU copies and GPU copies.
int cpu_copies(ID3D12Device* device, bool bc, const char* path) {
const D3D12_HEAP_PROPERTIES cpu_heap{D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_WRITE_BACK, D3D12_MEMORY_POOL_L0};
const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT};
const D3D12_HEAP_PROPERTIES upload_heap{D3D12_HEAP_TYPE_UPLOAD};
const D3D12_HEAP_PROPERTIES readback_heap{D3D12_HEAP_TYPE_READBACK};
const auto create = [&](const D3D12_HEAP_PROPERTIES& heap, const D3D12_RESOURCE_DESC& desc,
D3D12_RESOURCE_STATES state, Ref<ID3D12Resource>& out) {
return device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, state, nullptr, IID_PPV_ARGS(&out.p));
};
unsigned bad = 0;
const auto expect_bytes = [&](const char* what, UINT x, UINT y, UINT z, const std::uint8_t* got,
const std::array<std::uint8_t, 4>& want) {
if (std::memcmp(got, want.data(), 4) == 0) return;
if (bad++ < 8)
std::fprintf(stderr, "%s, %s (%u,%u,%u): got %02x %02x %02x %02x, want %02x %02x %02x %02x\n", path, what,
x, y, z, got[0], got[1], got[2], got[3], want[0], want[1], want[2], want[3]);
};
const auto texel = [](UINT x, UINT y, UINT z) {
return std::array<std::uint8_t, 4>{texel_byte(x, y, z, 0), texel_byte(x, y, z, 1), texel_byte(x, y, z, 2),
texel_byte(x, y, z, 3)};
};
constexpr std::array<std::uint8_t, 4> zero{};
Ref<ID3D12Resource> tex, vol, bc1, gpu_only, buffer, upload, readback;
EXPECT(SUCCEEDED(create(cpu_heap, texture_desc(8, 8, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 2),
D3D12_RESOURCE_STATE_COMMON, tex)));
D3D12_RESOURCE_DESC volume = texture_desc(4, 4, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 1, 4);
volume.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
EXPECT(SUCCEEDED(create(cpu_heap, volume, D3D12_RESOURCE_STATE_COMMON, vol)));
if (bc)
EXPECT(SUCCEEDED(create(cpu_heap, texture_desc(8, 8, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE),
D3D12_RESOURCE_STATE_COMMON, bc1)));
EXPECT(SUCCEEDED(create(default_heap, texture_desc(4, 4, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE),
D3D12_RESOURCE_STATE_COPY_SOURCE, gpu_only)));
EXPECT(SUCCEEDED(create(cpu_heap, buffer_desc(4096), D3D12_RESOURCE_STATE_COMMON, buffer)));
EXPECT(SUCCEEDED(create(upload_heap, buffer_desc(4096), D3D12_RESOURCE_STATE_GENERIC_READ, upload)));
EXPECT(SUCCEEDED(create(readback_heap, buffer_desc(4096), D3D12_RESOURCE_STATE_COPY_DEST, readback)));
// A texture in a CPU-visible heap maps only without a pointer; a default-heap texture not at all.
void* pointer = nullptr;
EXPECT(tex->Map(0, nullptr, nullptr) == S_OK);
EXPECT(tex->Map(1, nullptr, nullptr) == S_OK);
EXPECT(tex->Map(0, nullptr, &pointer) == E_INVALIDARG);
EXPECT(tex->Map(2, nullptr, nullptr) == E_INVALIDARG);
EXPECT(gpu_only->Map(0, nullptr, nullptr) == E_INVALIDARG);
tex->Unmap(1, nullptr);
// A new texture reads back zero-filled.
std::vector<std::uint8_t> got(1024, 0xcd);
EXPECT(SUCCEEDED(tex->ReadFromSubresource(got.data(), 32, 0, 0, nullptr)));
for (UINT y = 0; y < 8; ++y)
for (UINT x = 0; x < 8; ++x) expect_bytes("new texture", x, y, 0, &got[y * 32 + x * 4], zero);
// A 5x4 box from rows 100 bytes apart, read back whole at a 40-byte pitch.
std::vector<std::uint8_t> src(1024, 0);
const D3D12_BOX box{2, 1, 0, 7, 5, 1};
for (UINT y = 0; y < 4; ++y)
for (UINT x = 0; x < 5; ++x) std::memcpy(&src[y * 100 + x * 4], texel(x + 2, y + 1, 0).data(), 4);
EXPECT(SUCCEEDED(tex->WriteToSubresource(0, &box, src.data(), 100, 0)));
std::fill(got.begin(), got.end(), std::uint8_t(0xcd));
EXPECT(SUCCEEDED(tex->ReadFromSubresource(got.data(), 40, 0, 0, nullptr)));
for (UINT y = 0; y < 8; ++y)
for (UINT x = 0; x < 8; ++x) {
const bool inside = x >= 2 && x < 7 && y >= 1 && y < 5;
expect_bytes("boxed write", x, y, 0, &got[y * 40 + x * 4], inside ? texel(x, y, 0) : zero);
}
// Mip 1 (4x4) written whole, read back as a box.
for (UINT y = 0; y < 4; ++y)
for (UINT x = 0; x < 4; ++x) std::memcpy(&src[y * 16 + x * 4], texel(x, y, 1).data(), 4);
EXPECT(SUCCEEDED(tex->WriteToSubresource(1, nullptr, src.data(), 16, 0)));
const D3D12_BOX mip_box{1, 1, 0, 3, 4, 1};
std::fill(got.begin(), got.end(), std::uint8_t(0xcd));
EXPECT(SUCCEEDED(tex->ReadFromSubresource(got.data(), 8, 0, 1, &mip_box)));
for (UINT y = 0; y < 3; ++y)
for (UINT x = 0; x < 2; ++x) expect_bytes("mip 1", x + 1, y + 1, 1, &got[y * 8 + x * 4], texel(x + 1, y + 1, 1));
// A volume: three slices of a 3x4 box, rows 12 and slices 64 bytes apart; read back whole at other pitches.
std::fill(src.begin(), src.end(), std::uint8_t(0));
const D3D12_BOX volume_box{1, 0, 1, 4, 4, 4};
for (UINT z = 0; z < 3; ++z)
for (UINT y = 0; y < 4; ++y)
for (UINT x = 0; x < 3; ++x) std::memcpy(&src[z * 64 + y * 12 + x * 4], texel(x + 1, y, z + 1).data(), 4);
EXPECT(SUCCEEDED(vol->WriteToSubresource(0, &volume_box, src.data(), 12, 64)));
std::fill(got.begin(), got.end(), std::uint8_t(0xcd));
EXPECT(SUCCEEDED(vol->ReadFromSubresource(got.data(), 20, 96, 0, nullptr)));
for (UINT z = 0; z < 4; ++z)
for (UINT y = 0; y < 4; ++y)
for (UINT x = 0; x < 4; ++x)
expect_bytes("volume", x, y, z, &got[z * 96 + y * 20 + x * 4], x >= 1 && z >= 1 ? texel(x, y, z) : zero);
// BC1: 2x2 blocks of 8 bytes written whole, then one block replaced through a box.
if (bc1.p) {
std::uint8_t blocks[32];
for (UINT i = 0; i < 32; ++i) blocks[i] = std::uint8_t(0x40 + i);
EXPECT(SUCCEEDED(bc1->WriteToSubresource(0, nullptr, blocks, 16, 0)));
const D3D12_BOX corner{4, 4, 0, 8, 8, 1};
const std::uint8_t replaced[8] = {0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7};
EXPECT(SUCCEEDED(bc1->WriteToSubresource(0, &corner, replaced, 8, 0)));
std::memcpy(blocks + 24, replaced, 8);
std::uint8_t back[64] = {};
EXPECT(SUCCEEDED(bc1->ReadFromSubresource(back, 32, 0, 0, nullptr)));
for (UINT row = 0; row < 2; ++row)
if (std::memcmp(back + row * 32, blocks + row * 16, 16) != 0 && bad++ < 8)
std::fprintf(stderr, "%s, BC1 block row %u differs\n", path, row);
const D3D12_BOX cut{2, 0, 0, 8, 4, 1}; // starts inside a block
EXPECT(bc1->WriteToSubresource(0, &cut, blocks, 16, 0) == E_INVALIDARG);
}
// Calls D3D12 rejects, and two it doesn't: an empty box copies nothing, and pitches aren't checked (the host's
// answers, d3d12_subresource_copy_test).
const D3D12_BOX outside{0, 0, 0, 9, 1, 1}, empty{2, 2, 0, 2, 3, 1};
EXPECT(tex->WriteToSubresource(0, &outside, src.data(), 100, 0) == E_INVALIDARG);
EXPECT(tex->WriteToSubresource(0, &empty, src.data(), 100, 0) == S_OK);
EXPECT(tex->WriteToSubresource(2, nullptr, src.data(), 100, 0) == E_INVALIDARG); // 2 mips, 1 layer
EXPECT(tex->WriteToSubresource(0, nullptr, nullptr, 32, 0) == E_INVALIDARG);
EXPECT(tex->ReadFromSubresource(nullptr, 32, 0, 0, nullptr) == E_INVALIDARG);
EXPECT(vol->WriteToSubresource(0, nullptr, src.data(), 16, 32) == S_OK); // 64-byte slices 32 apart
EXPECT(buffer->WriteToSubresource(0, nullptr, src.data(), 16, 0) == E_INVALIDARG);
// A default-heap texture: D3D12 wants a CPU-visible heap, but the copy still works (and is logged).
for (UINT y = 0; y < 4; ++y)
for (UINT x = 0; x < 4; ++x) std::memcpy(&src[y * 16 + x * 4], texel(x, y, 7).data(), 4);
EXPECT(SUCCEEDED(gpu_only->WriteToSubresource(0, nullptr, src.data(), 16, 0)));
// GPU copies see the CPU's writes (mip 1, the default-heap texture), and the CPU sees a GPU copy's (a 4x4
// upload into the bottom left of mip 0, which is in COMMON and promoted).
D3D12_COMMAND_QUEUE_DESC queue_desc{};
Ref<ID3D12CommandQueue> queue;
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
Ref<ID3D12Fence> fence;
EXPECT(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))));
EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))));
EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
IID_PPV_ARGS(&list.p))));
EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))));
std::uint8_t* up = nullptr;
std::uint8_t* down = nullptr;
EXPECT(SUCCEEDED(upload->Map(0, nullptr, reinterpret_cast<void**>(&up))));
EXPECT(SUCCEEDED(readback->Map(0, nullptr, reinterpret_cast<void**>(&down))));
D3D12_PLACED_SUBRESOURCE_FOOTPRINT square{0, {DXGI_FORMAT_R8G8B8A8_UNORM, 4, 4, 1, 256}};
for (UINT y = 0; y < 4; ++y)
for (UINT x = 0; x < 4; ++x) std::memcpy(up + y * 256 + x * 4, texel(x, y + 4, 0).data(), 4);
std::memset(down, 0xcd, 4096);
D3D12_PLACED_SUBRESOURCE_FOOTPRINT second = square;
second.Offset = 2048;
{
const auto dst = subresource(tex.p, 0);
const auto in = footprint(upload.p, square);
list->CopyTextureRegion(&dst, 0, 4, 0, &in, nullptr);
const auto out = footprint(readback.p, square);
const auto mip1 = subresource(tex.p, 1);
list->CopyTextureRegion(&out, 0, 0, 0, &mip1, nullptr);
const auto out2 = footprint(readback.p, second);
const auto staged = subresource(gpu_only.p, 0);
list->CopyTextureRegion(&out2, 0, 0, 0, &staged, nullptr);
}
EXPECT(list->Close() == S_OK);
ID3D12CommandList* lists[] = {list.p};
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(SUCCEEDED(queue->Signal(fence.p, 1)) && SUCCEEDED(fence->SetEventOnCompletion(1, nullptr)));
for (UINT y = 0; y < 4; ++y)
for (UINT x = 0; x < 4; ++x) {
expect_bytes("GPU copy of mip 1", x, y, 1, down + y * 256 + x * 4, texel(x, y, 1));
expect_bytes("GPU copy of the default-heap texture", x, y, 7, down + 2048 + y * 256 + x * 4, texel(x, y, 7));
}
const D3D12_BOX bottom_left{0, 4, 0, 4, 8, 1};
std::fill(got.begin(), got.end(), std::uint8_t(0xcd));
EXPECT(SUCCEEDED(tex->ReadFromSubresource(got.data(), 16, 0, 0, &bottom_left)));
for (UINT y = 0; y < 4; ++y)
for (UINT x = 0; x < 4; ++x) expect_bytes("CPU read of a GPU upload", x, y + 4, 0, &got[y * 16 + x * 4], texel(x, y + 4, 0));
upload->Unmap(0, nullptr);
readback->Unmap(0, nullptr);
EXPECT(!bad);
std::printf("WriteToSubresource/ReadFromSubresource (%s): boxes, pitches, a mip, a volume%s, rejected calls and "
"GPU copies both ways matched\n", path, bc1.p ? ", BC1" : "");
return 0;
}
// The host PC runtime's D3D12 device to compare answers with: the layer's own adapter (set in main), else the default
// one, or the one LEONIDA_HOST_ADAPTER names ("warp", or part of an adapter's name); false if there is none.
// Under Wine there is none (copycmp::create_host).
bool create_host_device(Ref<ID3D12Device>& host, std::string* adapter_name = nullptr) {
copycmp::Ptr<ID3D12Device> device;
std::string name;
if (!copycmp::create_host(device, name)) return false;
if (adapter_name) *adapter_name = name;
host.p = device.p;
device.p = nullptr;
return true;
}
bool block_compressed(DXGI_FORMAT format) {
switch (format) {
case DXGI_FORMAT_BC1_UNORM:
case DXGI_FORMAT_BC1_UNORM_SRGB:
case DXGI_FORMAT_BC2_UNORM:
case DXGI_FORMAT_BC2_UNORM_SRGB:
case DXGI_FORMAT_BC3_UNORM:
case DXGI_FORMAT_BC3_UNORM_SRGB:
case DXGI_FORMAT_BC4_UNORM:
case DXGI_FORMAT_BC4_SNORM:
case DXGI_FORMAT_BC5_UNORM:
case DXGI_FORMAT_BC5_SNORM:
case DXGI_FORMAT_BC6H_UF16:
case DXGI_FORMAT_BC6H_SF16:
case DXGI_FORMAT_BC7_UNORM:
case DXGI_FORMAT_BC7_UNORM_SRGB: return true;
default: return false;
}
}
// A block-compressed texture whose top mip is not a whole number of 4x4 blocks. The PC runtime only answers
// GetCopyableFootprints for these when D3D12_OPTIONS8.UnalignedBlockTexturesSupported is TRUE (Windows 10 does
// not); it returns -1 for every field otherwise.
bool uses_unaligned_blocks(const D3D12_RESOURCE_DESC& desc) {
return block_compressed(desc.Format) && (desc.Width % 4 != 0 || desc.Height % 4 != 0);
}
bool host_supports_unaligned_block_textures(ID3D12Device* host) {
D3D12_FEATURE_DATA_D3D12_OPTIONS8 options{};
return SUCCEEDED(host->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS8, &options, sizeof(options))) &&
options.UnalignedBlockTexturesSupported;
}
// ResolveSubresourceRegion is on ID3D12GraphicsCommandList7, which the Windows 10 runtime does not have.
bool host_has_graphics_command_list7(ID3D12Device* host) {
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
Ref<ID3D12GraphicsCommandList7> list7;
if (FAILED(host->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))))
return false;
if (FAILED(host->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
IID_PPV_ARGS(&list.p))))
return false;
if (FAILED(list->Close())) return false;
return SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list7.p)));
}
int compare_format_info(ID3D12Device* ours, const vktest::Device& vk) {
VkPhysicalDeviceIDProperties id{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES};
VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &id};
vk.instance_fn<PFN_vkGetPhysicalDeviceProperties2>("vkGetPhysicalDeviceProperties2")(vk.physical, &properties);
LUID luid{};
static_assert(sizeof(luid) == sizeof(id.deviceLUID));
std::memcpy(&luid, id.deviceLUID, sizeof(luid));
copycmp::Ptr<ID3D12Device> host;
std::string adapter_name;
if (!id.deviceLUIDValid || !copycmp::try_create_host(host, adapter_name, &luid)) {
std::printf("FORMAT_INFO: no native D3D12 device on the Vulkan adapter, skipped comparison\n");
return 0;
}
unsigned compared = 0, host_unknown = 0;
int mismatches = 0;
const auto compare = [&](DXGI_FORMAT format, UINT size) {
D3D12_FEATURE_DATA_FORMAT_INFO expected{format, 0xff}, actual = expected;
HRESULT expected_hr = host->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, &expected, size);
// Some Windows runtimes predate these single-plane formats. Still require the layer to recognize them.
if (expected_hr == E_INVALIDARG && size == sizeof(expected) &&
(format == DXGI_FORMAT_SAMPLER_FEEDBACK_MIN_MIP_OPAQUE ||
format == DXGI_FORMAT_SAMPLER_FEEDBACK_MIP_REGION_USED_OPAQUE || format == DXGI_FORMAT_A4B4G4R4_UNORM)) {
expected_hr = S_OK;
expected.PlaneCount = 1;
++host_unknown;
std::printf("FORMAT_INFO format %u: host returned E_INVALIDARG, checking for one plane\n", UINT(format));
}
const HRESULT our_hr = ours->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, &actual, size);
++compared;
if (our_hr != expected_hr || actual.PlaneCount != expected.PlaneCount || actual.Format != format) {
std::fprintf(stderr, "FORMAT_INFO format %u size %u: HRESULT/planes %08lx/%u, expected %08lx/%u\n",
UINT(format), size, static_cast<unsigned long>(our_hr), UINT(actual.PlaneCount),
static_cast<unsigned long>(expected_hr), UINT(expected.PlaneCount));
++mismatches;
}
};
constexpr UINT size = sizeof(D3D12_FEATURE_DATA_FORMAT_INFO);
// Include UNKNOWN, holes between format families, and the value after the last named format.
for (UINT format = 0; format <= UINT(DXGI_FORMAT_A4B4G4R4_UNORM) + 1; ++format)
compare(static_cast<DXGI_FORMAT>(format), size);
compare(DXGI_FORMAT_FORCE_UINT, size);
compare(DXGI_FORMAT_D24_UNORM_S8_UINT, size - 1);
std::printf("FORMAT_INFO on %s: %u queries, %u newer formats unknown to host, %d mismatches\n",
adapter_name.c_str(), compared, host_unknown, mismatches);
return mismatches;
}
// D3D-122: descriptions GetCopyableFootprints must refuse (false) or answer (true), as the PC runtime does. All were
// measured on WARP. Also on an NVIDIA GPU: texture Width/Height 0, Count 0, Count 4 without a render-target or depth
// flag, Count 3; buffer Width 0, Height 2, Format, MipLevels 0, Layout, Count 0; and the buffer render-target, extra
// mip, ROW_MAJOR and Quality 1 controls. compare_footprints checks them against the host, the main test on their own.
std::vector<std::pair<D3D12_RESOURCE_DESC, bool>> footprint_validity_cases() {
const auto rgba = DXGI_FORMAT_R8G8B8A8_UNORM;
const auto rt = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
std::vector<std::pair<D3D12_RESOURCE_DESC, bool>> cases;
const auto add = [&](D3D12_RESOURCE_DESC d, bool valid, auto&& change) {
change(d);
cases.push_back({d, valid});
};
const auto buffer = buffer_desc(256);
add(buffer, false, [](auto& d) { d.Width = 0; });
add(buffer, false, [](auto& d) { d.Height = 2; });
add(buffer, false, [](auto& d) { d.DepthOrArraySize = 2; });
add(buffer, false, [](auto& d) { d.MipLevels = 0; });
add(buffer, false, [](auto& d) { d.MipLevels = 2; });
add(buffer, false, [&](auto& d) { d.Format = rgba; });
add(buffer, false, [](auto& d) { d.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; });
add(buffer, false, [](auto& d) { d.SampleDesc.Count = 0; });
add(buffer, false, [](auto& d) { d.SampleDesc.Count = 2; });
add(buffer, false, [](auto& d) { d.SampleDesc.Quality = 1; });
add(buffer, false, [](auto& d) { d.Alignment = 4096; });
add(buffer, true, [&](auto& d) { d.Flags = rt; });
add(buffer, true, [](auto& d) { d.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT; });
const auto texture = texture_desc(16, 16, rgba, D3D12_RESOURCE_FLAG_NONE);
add(texture, false, [](auto& d) { d.Width = 0; });
add(texture, false, [](auto& d) { d.Height = 0; });
add(texture, false, [](auto& d) { d.DepthOrArraySize = 0; });
add(texture, false, [](auto& d) { d.SampleDesc.Count = 0; });
add(texture, false, [](auto& d) { d.SampleDesc.Count = 4; }); // no render-target or depth flag
add(texture, false, [](auto& d) { d.SampleDesc.Count = 4, d.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; });
add(texture, false, [&](auto& d) { d.SampleDesc.Count = 3, d.Flags = rt; });
add(texture, false, [&](auto& d) { d.SampleDesc.Count = 4, d.Flags = rt, d.MipLevels = 2; });
add(texture, false, [&](auto& d) { d.SampleDesc.Count = 4, d.Flags = rt, d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; });
add(texture, true, [&](auto& d) { d.SampleDesc.Count = 4, d.Flags = rt; });
add(texture, true, [](auto& d) { d.MipLevels = 9; }); // past the full chain
add(texture, true, [](auto& d) { d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; });
add(texture, true, [](auto& d) { d.SampleDesc.Quality = 1; });
add(texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL), true,
[](auto& d) { d.SampleDesc.Count = 4; });
add(line_desc(16, 1, 1, rgba), false, [](auto& d) { d.Width = 0; });
add(line_desc(16, 1, 1, rgba), false, [&](auto& d) { d.SampleDesc.Count = 2, d.Flags = rt; });
add(volume_desc(1), false, [](auto& d) { d.DepthOrArraySize = 0; });
add(volume_desc(1), false, [](auto& d) { d.Height = 0; });
add(volume_desc(1), false, [&](auto& d) { d.SampleDesc.Count = 2, d.Flags = rt; });
return cases;
}
// GetCopyableFootprints must match the PC runtime's exactly, since applications size and fill upload buffers from
// it. Unaligned block-compressed cases are skipped when the host runtime cannot answer them, so the rest still
// run. Returns the number of mismatches, or -1 when the host has no D3D12 device to compare with.
int compare_footprints(ID3D12Device* ours) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
const bool unaligned_blocks = host_supports_unaligned_block_textures(host.p);
unsigned skipped = 0;
struct Case {
D3D12_RESOURCE_DESC desc;
UINT subresources;
};
std::vector<Case> cases = {
{volume_desc(5), 5},
{line_desc(13, 4, 1, DXGI_FORMAT_R8G8B8A8_UNORM), 4},
{line_desc(5, 3, 2, DXGI_FORMAT_R8G8B8A8_UNORM), 6},
{buffer_desc(300), 1},
{texture_desc(8, 8, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 4, 2), 8},
{texture_desc(13, 7, DXGI_FORMAT_R9G9B9E5_SHAREDEXP, D3D12_RESOURCE_FLAG_NONE, 4, 2), 8},
{texture_desc(13, 7, DXGI_FORMAT_B5G5R5A1_UNORM, D3D12_RESOURCE_FLAG_NONE, 4, 2), 8},
{texture_desc(13, 7, DXGI_FORMAT_A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 4, 2), 8},
{texture_desc(13, 7, DXGI_FORMAT_R16G16B16A16_FLOAT, D3D12_RESOURCE_FLAG_NONE, 4), 4},
{texture_desc(13, 7, DXGI_FORMAT_B8G8R8X8_UNORM, D3D12_RESOURCE_FLAG_NONE, 4, 2), 8},
{texture_desc(13, 7, DXGI_FORMAT_B8G8R8X8_UNORM_SRGB, D3D12_RESOURCE_FLAG_NONE, 4), 4},
{texture_desc(16, 16, DXGI_FORMAT_B8G8R8X8_TYPELESS, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(10, 6, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE, 4), 4},
{texture_desc(4, 4, DXGI_FORMAT_BC7_UNORM_SRGB, D3D12_RESOURCE_FLAG_NONE, 1, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_BC5_UNORM, D3D12_RESOURCE_FLAG_NONE, 5), 5},
{texture_desc(8, 8, DXGI_FORMAT_D32_FLOAT_S8X24_UINT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, 2), 4},
{texture_desc(8, 8, DXGI_FORMAT_D24_UNORM_S8_UINT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, 2, 2), 8},
{texture_desc(300, 5, DXGI_FORMAT_D32_FLOAT_S8X24_UINT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL), 2},
{texture_desc(8, 8, DXGI_FORMAT_D32_FLOAT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL), 1},
{texture_desc(8, 8, DXGI_FORMAT_D16_UNORM, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, 4), 4},
{texture_desc(6, 6, DXGI_FORMAT_R32_TYPELESS, D3D12_RESOURCE_FLAG_NONE), 1},
{texture_desc(16, 16, DXGI_FORMAT_R32G32_UINT, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_R32G32_SINT, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_R32G32_TYPELESS, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_R16G16B16A16_UINT, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_R16G16B16A16_SINT, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_R10G10B10A2_UINT, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM, D3D12_RESOURCE_FLAG_NONE, 1), 1},
{texture_desc(16, 16, DXGI_FORMAT_R16G16_TYPELESS, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_R16G16_UINT, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_R16G16_SINT, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_R8G8_TYPELESS, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_R8G8_UINT, D3D12_RESOURCE_FLAG_NONE, 3), 3},
{texture_desc(16, 16, DXGI_FORMAT_R8G8_SINT, D3D12_RESOURCE_FLAG_NONE, 3), 3},
// Mip counts past 32 (and past 64 for the width): D3D12 accepts any count, every mip past the chain is 1x1.
{texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 40), 40},
{line_desc(16, 70, 1, DXGI_FORMAT_R8G8B8A8_UNORM), 70},
{volume_desc(40), 40},
};
for (const auto& c : footprint_validity_cases()) cases.push_back({c.first, 1});
int mismatches = 0;
for (const Case& c : cases) {
if (!unaligned_blocks && uses_unaligned_blocks(c.desc)) {
++skipped;
continue;
}
// 513 and 2^32 + 1 are not multiples of D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT: they show whether the layout
// is aligned from BaseOffset or from 0.
for (const UINT64 base : {0ull, 513ull, 1024ull, (1ull << 32) + 1}) {
std::vector<D3D12_PLACED_SUBRESOURCE_FOOTPRINT> a(c.subresources), b(c.subresources);
std::vector<UINT> rows_a(c.subresources), rows_b(c.subresources);
std::vector<UINT64> size_a(c.subresources), size_b(c.subresources);
UINT64 total_a = 0, total_b = 0;
ours->GetCopyableFootprints(&c.desc, 0, c.subresources, base, a.data(), rows_a.data(), size_a.data(),
&total_a);
host->GetCopyableFootprints(&c.desc, 0, c.subresources, base, b.data(), rows_b.data(), size_b.data(),
&total_b);
for (UINT i = 0; i < c.subresources; ++i) {
const auto &x = a[i], &y = b[i];
if (x.Offset == y.Offset && x.Footprint.Format == y.Footprint.Format &&
x.Footprint.Width == y.Footprint.Width && x.Footprint.Height == y.Footprint.Height &&
x.Footprint.Depth == y.Footprint.Depth && x.Footprint.RowPitch == y.Footprint.RowPitch &&
rows_a[i] == rows_b[i] && size_a[i] == size_b[i])
continue;
if (mismatches++ < 24)
std::fprintf(stderr,
"footprint format %d base %llu subresource %u: ours offset %llu fmt %d %ux%ux%u pitch %u "
"rows %u row %llu; host offset %llu fmt %d %ux%ux%u pitch %u rows %u row %llu\n",
int(c.desc.Format), (unsigned long long)base, i, (unsigned long long)x.Offset,
int(x.Footprint.Format), x.Footprint.Width, x.Footprint.Height, x.Footprint.Depth,
x.Footprint.RowPitch, rows_a[i], (unsigned long long)size_a[i],
(unsigned long long)y.Offset, int(y.Footprint.Format), y.Footprint.Width,
y.Footprint.Height, y.Footprint.Depth, y.Footprint.RowPitch, rows_b[i],
(unsigned long long)size_b[i]);
}
if (total_a != total_b && mismatches++ < 8)
std::fprintf(stderr, "footprint format %d base %llu: total ours %llu host %llu\n", int(c.desc.Format),
(unsigned long long)base, (unsigned long long)total_a, (unsigned long long)total_b);
}
}
if (skipped)
std::printf("GetCopyableFootprints: %u unaligned block-compressed cases skipped (the host runtime has no "
"D3D12_OPTIONS8.UnalignedBlockTexturesSupported)\n", skipped);
return mismatches;
}
// GetResourceAllocationInfo against the PC runtime. Buffer sizes, every alignment and which descriptions are invalid
// must match it exactly. A texture's size is the driver's choice on both sides, so ours is only checked to be a
// whole number of alignments and at least the texels of its top mip; both sizes are printed. Also checks the
// Device4/Device8 variants' per-resource layout. Returns the number of mismatches, or -1 without a host device.
int compare_allocation_info(ID3D12Device* ours) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
const auto with = [](D3D12_RESOURCE_DESC desc, UINT64 alignment, UINT samples = 1) {
desc.Alignment = alignment;
desc.SampleDesc.Count = samples;
return desc;
};
constexpr UINT64 k4 = D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT, k64 = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
constexpr D3D12_RESOURCE_FLAGS none = D3D12_RESOURCE_FLAG_NONE, rt = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
constexpr DXGI_FORMAT rgba8 = DXGI_FORMAT_R8G8B8A8_UNORM;
struct Case {
const char* name;
D3D12_RESOURCE_DESC desc;
UINT64 top_mip_bytes; // 0: the size must match the host's exactly (buffers, invalid descriptions)
};
D3D12_RESOURCE_DESC bad_buffer = buffer_desc(256);
bad_buffer.Height = 2;
D3D12_RESOURCE_DESC typed_buffer = buffer_desc(256);
typed_buffer.Format = DXGI_FORMAT_R8_UNORM;
D3D12_RESOURCE_DESC big_volume = volume_desc(1);
big_volume.Width = big_volume.Height = 32;
big_volume.DepthOrArraySize = 8;
D3D12_RESOURCE_DESC bad_line = line_desc(8, 1, 1, rgba8);
bad_line.Height = 2;
D3D12_RESOURCE_DESC line_rt = line_desc(8, 1, 1, rgba8);
line_rt.Flags = rt;
const auto small_volume = [&](UINT width, UINT height, UINT16 depth, DXGI_FORMAT format) {
D3D12_RESOURCE_DESC desc = with(texture_desc(width, height, format, none, 1, depth), k4);
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
return desc;
};
const Case cases[] = {
{"buffer 300", buffer_desc(300), 0},
{"buffer 64 KiB", buffer_desc(65536), 0},
{"buffer 64 KiB + 1", buffer_desc(65537), 0},
{"buffer aligned 64 KiB", with(buffer_desc(1000), k64), 0},
{"buffer width 0", buffer_desc(0), 0},
{"buffer height 2", bad_buffer, 0},
{"buffer with a format", typed_buffer, 0},
// Resource flag handling (D3D-467).
{"buffer render target", [&] {
auto d = buffer_desc(256);
d.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
return d;
}(), 0},
{"buffer render target UAV", [&] {
auto d = buffer_desc(256);
d.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
return d;
}(), 0},
{"buffer cross adapter", [&] {
auto d = buffer_desc(256);
d.Flags = D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER;
return d;
}(), 0},
{"buffer depth stencil (invalid)", [&] {
auto d = buffer_desc(256);
d.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
return d;
}(), 0},
{"buffer simultaneous access (invalid)", [&] {
auto d = buffer_desc(256);
d.Flags = D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS;
return d;
}(), 0},
{"buffer video decode reference only (invalid)", [&] {
auto d = buffer_desc(256);
d.Flags = D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY;
return d;
}(), 0},
{"buffer flag 0x1000 (invalid)", [&] {
auto d = buffer_desc(256);
d.Flags = D3D12_RESOURCE_FLAGS(0x1000);
return d;
}(), 0},
{"tex2d deny shader resource alone (invalid)", [&] {
auto d = texture_desc(16, 16, rgba8, D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
return d;
}(), 0},
{"tex2d single sample quality 1", [&] {
auto d = texture_desc(16, 16, rgba8, none);
d.SampleDesc.Quality = 1;
return d;
}(), 16 * 16 * 4},
{"tex2d single sample quality center pattern", [&] {
auto d = texture_desc(16, 16, rgba8, none);
d.SampleDesc.Quality = 0xffffffff;
return d;
}(), 16 * 16 * 4},
{"256x256 full chain", texture_desc(256, 256, rgba8, none, 0), 256 * 256 * 4},
{"16x16 small", with(texture_desc(16, 16, rgba8, none), k4), 16 * 16 * 4},
{"16x16 small render target", with(texture_desc(16, 16, rgba8, rt), k4), 16 * 16 * 4},
{"1024x1024 small (too big)", with(texture_desc(1024, 1024, rgba8, none), k4), 1024 * 1024 * 4},
{"64x64 BC1 small", with(texture_desc(64, 64, DXGI_FORMAT_BC1_UNORM, none, 3), k4), 16 * 16 * 8},
// 4 KiB alignment counts the 4 KiB tiles of one slice's top mip (at most 16), not the whole resource.
{"128x128 full chain small", with(texture_desc(128, 128, rgba8, none, 0), k4), 128 * 128 * 4},
{"64x64 array of 8 small", with(texture_desc(64, 64, rgba8, none, 1, 8), k4), 64 * 64 * 4 * 8},
{"16x16 array of 256 small", with(texture_desc(16, 16, rgba8, none, 1, 256), k4), 16 * 16 * 4 * 256},
{"256x64 full chain small", with(texture_desc(256, 64, rgba8, none, 0), k4), 256 * 64 * 4},
{"1D 16384 small", with(line_desc(16384, 1, 1, rgba8), k4), 16384 * 4},
{"R8 32x32x64 volume small", small_volume(32, 32, 64, DXGI_FORMAT_R8_UNORM), 32 * 32 * 64},
{"R16 16x16x128 volume small", small_volume(16, 16, 128, DXGI_FORMAT_R16_FLOAT), 16 * 16 * 128 * 2},
{"33x480 small (30 tiles)", with(texture_desc(33, 480, rgba8, none), k4), 33 * 480 * 4},
{"128x129 small (20 tiles)", with(texture_desc(128, 129, rgba8, none), k4), 128 * 129 * 4},
{"R8 65x1000 small (32 tiles)", with(texture_desc(65, 1000, DXGI_FORMAT_R8_UNORM, none), k4), 65 * 1000},
{"100x100 volume small (49 tiles)", small_volume(100, 100, 1, rgba8), 100 * 100 * 4},
{"R16 16x8x256 volume small (32 tiles)", small_volume(16, 8, 256, DXGI_FORMAT_R16_FLOAT), 16 * 8 * 256 * 2},
// One shape per element size, each exactly 16 tiles along one axis, then one texel more.
{"R8 16x256x16 volume small", small_volume(16, 256, 16, DXGI_FORMAT_R8_UNORM), 16 * 256 * 16},
{"R8 16x257x16 volume small (17 tiles)", small_volume(16, 257, 16, DXGI_FORMAT_R8_UNORM), 16 * 257 * 16},
{"R16 256x16x8 volume small", small_volume(256, 16, 8, DXGI_FORMAT_R16_FLOAT), 256 * 16 * 8 * 2},
{"R16 257x16x8 volume small (17 tiles)", small_volume(257, 16, 8, DXGI_FORMAT_R16_FLOAT), 257 * 16 * 8 * 2},
{"rgba8 256x8x8 volume small", small_volume(256, 8, 8, rgba8), 256 * 8 * 8 * 4},
{"rgba8 257x8x8 volume small (17 tiles)", small_volume(257, 8, 8, rgba8), 257 * 8 * 8 * 4},
{"rgba16 8x128x8 volume small", small_volume(8, 128, 8, DXGI_FORMAT_R16G16B16A16_FLOAT), 8 * 128 * 8 * 8},
{"rgba16 8x129x8 volume small (17 tiles)", small_volume(8, 129, 8, DXGI_FORMAT_R16G16B16A16_FLOAT),
8 * 129 * 8 * 8},
{"rgba32 128x8x4 volume small", small_volume(128, 8, 4, DXGI_FORMAT_R32G32B32A32_FLOAT), 128 * 8 * 4 * 16},
{"rgba32 129x8x4 volume small (17 tiles)", small_volume(129, 8, 4, DXGI_FORMAT_R32G32B32A32_FLOAT),
129 * 8 * 4 * 16},
{"BC1 32x512x8 volume small", small_volume(32, 512, 8, DXGI_FORMAT_BC1_UNORM), 8 * 128 * 8 * 8},
{"BC1 32x516x8 volume small (17 tiles)", small_volume(32, 516, 8, DXGI_FORMAT_BC1_UNORM), 8 * 129 * 8 * 8},
// Simultaneous access is for single-sampled color only; a volume cannot be a depth buffer.
{"depth with simultaneous access",
texture_desc(64, 64, DXGI_FORMAT_D32_FLOAT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS), 0},
{"4x MSAA with simultaneous access",
with(texture_desc(64, 64, rgba8, rt | D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS), 0, 4), 0},
{"depth-stencil volume", [&] {
D3D12_RESOURCE_DESC desc = small_volume(8, 8, 4, DXGI_FORMAT_D32_FLOAT);
desc.Alignment = 0;
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
return desc;
}(), 0},
{"64x64 depth", texture_desc(64, 64, DXGI_FORMAT_D32_FLOAT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
64 * 64 * 4},
{"8x8 array of 6", texture_desc(8, 8, rgba8, none, 4, 6), 8 * 8 * 4 * 6},
{"32x32x8 volume", big_volume, 32 * 32 * 8 * 4},
{"1D 13 full chain", line_desc(13, 4, 1, rgba8), 13 * 4},
{"1D array 5x3", line_desc(5, 3, 2, rgba8), 5 * 4 * 2},
{"1D height 2", bad_line, 0},
{"1D render target", line_rt, 8 * 4},
{"64x64 4x MSAA", with(texture_desc(64, 64, rgba8, rt), 0, 4), 64 * 64 * 4 * 4},
{"64x64 32x MSAA", with(texture_desc(64, 64, rgba8, rt), 0, 32), 64 * 64 * 4 * 32},
{"64x64 64x MSAA (invalid)", with(texture_desc(64, 64, rgba8, rt), 0, 64), 0},
{"64x64 zero sample count (invalid)", with(texture_desc(64, 64, rgba8, rt), 0, 0), 0},
{"64x64 4x MSAA small", with(texture_desc(64, 64, rgba8, rt), k64, 4), 64 * 64 * 4 * 4},
{"1024x1024 4x MSAA small", with(texture_desc(1024, 1024, rgba8, rt), k64, 4), 1024 * 1024 * 4 * 4},
{"zero width", texture_desc(0, 16, rgba8, none), 0},
{"alignment 12345", with(texture_desc(16, 16, rgba8, none), 12345), 0},
{"4x MSAA with 2 mips", with(texture_desc(64, 64, rgba8, rt, 2), 0, 4), 0},
{"16x16 R32G32_UINT", texture_desc(16, 16, DXGI_FORMAT_R32G32_UINT, none, 1), 16 * 16 * 8},
{"16x16 R32G32_SINT", texture_desc(16, 16, DXGI_FORMAT_R32G32_SINT, none, 1), 16 * 16 * 8},
{"16x16 R32G32_TYPELESS", texture_desc(16, 16, DXGI_FORMAT_R32G32_TYPELESS, none, 1), 16 * 16 * 8},
{"16x16 R16G16B16A16_UINT", texture_desc(16, 16, DXGI_FORMAT_R16G16B16A16_UINT, none, 1), 16 * 16 * 8},
{"16x16 R16G16B16A16_SINT", texture_desc(16, 16, DXGI_FORMAT_R16G16B16A16_SINT, none, 1), 16 * 16 * 8},
{"16x16 R10G10B10A2_UINT", texture_desc(16, 16, DXGI_FORMAT_R10G10B10A2_UINT, none, 1), 16 * 16 * 4},
{"16x16 R10G10B10_XR_BIAS_A2_UNORM",
texture_desc(16, 16, DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM, none, 1), 16 * 16 * 4},
{"16x16 R16G16_TYPELESS", texture_desc(16, 16, DXGI_FORMAT_R16G16_TYPELESS, none, 1), 16 * 16 * 4},
{"16x16 R16G16_UINT", texture_desc(16, 16, DXGI_FORMAT_R16G16_UINT, none, 1), 16 * 16 * 4},
{"16x16 R16G16_SINT", texture_desc(16, 16, DXGI_FORMAT_R16G16_SINT, none, 1), 16 * 16 * 4},
{"16x16 R8G8_TYPELESS", texture_desc(16, 16, DXGI_FORMAT_R8G8_TYPELESS, none, 1), 16 * 16 * 2},
{"16x16 R8G8_UINT", texture_desc(16, 16, DXGI_FORMAT_R8G8_UINT, none, 1), 16 * 16 * 2},
{"16x16 R8G8_SINT", texture_desc(16, 16, DXGI_FORMAT_R8G8_SINT, none, 1), 16 * 16 * 2},
// Tiled (64KB_UNDEFINED_SWIZZLE) textures: 64 KiB alignment and tile-multiple size; 1D, buffers, 4 KiB
// alignment, untileable 96-bit and two-plane depth-stencil formats are invalid.
{"tiled 2D 256x256", [&] {
D3D12_RESOURCE_DESC d = texture_desc(256, 256, rgba8, none, 1);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 256 * 256 * 4},
{"tiled 2D D32F 256x256", [&] {
D3D12_RESOURCE_DESC d = texture_desc(256, 256, DXGI_FORMAT_D32_FLOAT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, 1);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 256 * 256 * 4},
{"tiled 3D 64x64x64", [&] {
D3D12_RESOURCE_DESC d = small_volume(64, 64, 64, rgba8);
d.Alignment = 0;
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 64 * 64 * 64 * 4},
{"tiled buffer (invalid)", [&] {
D3D12_RESOURCE_DESC d = buffer_desc(4096);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 0},
{"tiled 1D (invalid)", [&] {
D3D12_RESOURCE_DESC d = line_desc(256, 1, 1, rgba8);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 0},
{"tiled 2D 4K alignment (invalid)", [&] {
D3D12_RESOURCE_DESC d = with(texture_desc(16, 16, rgba8, none, 1), k4);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 0},
{"tiled 2D RGB32F (invalid)", [&] {
D3D12_RESOURCE_DESC d = texture_desc(64, 64, DXGI_FORMAT_R32G32B32_FLOAT, none, 1);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 0},
{"tiled 2D D24S8 (invalid)", [&] {
D3D12_RESOURCE_DESC d = texture_desc(64, 64, DXGI_FORMAT_D24_UNORM_S8_UINT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, 1);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 0},
{"tiled 2D 4x MSAA RGBA8", [&] {
D3D12_RESOURCE_DESC d = with(texture_desc(256, 256, rgba8, rt, 1), 0, 4);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 256 * 256 * 4 * 4},
{"tiled 2D 4x MSAA RGBA32F", [&] {
D3D12_RESOURCE_DESC d = with(texture_desc(256, 256, DXGI_FORMAT_R32G32B32A32_FLOAT, rt, 1), 0, 4);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 256 * 256 * 16 * 4},
{"tiled 2D 4x MSAA D32F", [&] {
D3D12_RESOURCE_DESC d = with(texture_desc(256, 256, DXGI_FORMAT_D32_FLOAT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, 1), 0, 4);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 256 * 256 * 4 * 4},
{"tiled 2D 2x MSAA (invalid)", [&] {
D3D12_RESOURCE_DESC d = with(texture_desc(256, 256, rgba8, rt, 1), 0, 2);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 0},
{"tiled 2D 8x MSAA (invalid)", [&] {
D3D12_RESOURCE_DESC d = with(texture_desc(256, 256, rgba8, rt, 1), 0, 8);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 0},
{"tiled 2D 4x MSAA NONE (invalid)", [&] {
D3D12_RESOURCE_DESC d = with(texture_desc(256, 256, rgba8, none, 1), 0, 4);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 0},
{"tiled 2D 4x MSAA RT+DS (invalid)", [&] {
D3D12_RESOURCE_DESC d = with(texture_desc(256, 256, rgba8,
rt | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, 1), 0, 4);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 0},
{"tiled 2D 4x MSAA 4M alignment (invalid)", [&] {
D3D12_RESOURCE_DESC d = with(texture_desc(256, 256, rgba8, rt, 1),
D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT, 4);
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
}(), 0},
{"UNKNOWN 4x MSAA NONE (invalid)", with(texture_desc(256, 256, rgba8, none, 1), 0, 4), 0},
{"UNKNOWN 4x MSAA RT+DS (invalid)",
with(texture_desc(256, 256, rgba8, rt | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, 1), 0, 4), 0},
};
int mismatches = 0;
const auto mismatch = [&](const char* name, const D3D12_RESOURCE_ALLOCATION_INFO& a,
const D3D12_RESOURCE_ALLOCATION_INFO& b) {
if (mismatches++ < 24)
std::fprintf(stderr, "allocation info %s: ours size %llu alignment %llu, host size %llu alignment %llu\n",
name, (unsigned long long)a.SizeInBytes, (unsigned long long)a.Alignment,
(unsigned long long)b.SizeInBytes, (unsigned long long)b.Alignment);
};
for (const Case& c : cases) {
const D3D12_RESOURCE_ALLOCATION_INFO a = ours->GetResourceAllocationInfo(0, 1, &c.desc);
const D3D12_RESOURCE_ALLOCATION_INFO b = host->GetResourceAllocationInfo(0, 1, &c.desc);
if (c.top_mip_bytes && b.SizeInBytes == UINT64_MAX && c.desc.SampleDesc.Count > 1 &&
c.desc.Layout == D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE) {
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS ms{
c.desc.Format, c.desc.SampleDesc.Count, D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_TILED_RESOURCE, 0};
if (SUCCEEDED(host->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &ms, sizeof(ms))) &&
ms.NumQualityLevels == 0) {
if (a.SizeInBytes == UINT64_MAX || a.Alignment != D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT)
mismatch(c.name, a, b);
std::printf("allocation info %s: host unsupported (NumQualityLevels=0), skipped\n", c.name);
continue;
}
}
if (b.SizeInBytes == UINT64_MAX || !c.top_mip_bytes) {
if (a.SizeInBytes != b.SizeInBytes || (b.SizeInBytes != UINT64_MAX && a.Alignment != b.Alignment))
mismatch(c.name, a, b);
continue;
}
if (a.Alignment != b.Alignment || a.SizeInBytes == UINT64_MAX || a.SizeInBytes % a.Alignment ||
a.SizeInBytes < c.top_mip_bytes)
mismatch(c.name, a, b);
std::printf("allocation info %s: alignment %llu, size ours %llu host %llu\n", c.name,
(unsigned long long)a.Alignment, (unsigned long long)a.SizeInBytes,
(unsigned long long)b.SizeInBytes);
}
// Several resources at once: each at its own alignment after the one before, the largest alignment overall.
// Buffers only for the host comparison (their sizes are fixed); the texture checks our own layout.
const D3D12_RESOURCE_DESC buffers[] = {buffer_desc(300), buffer_desc(70000), buffer_desc(1)};
const D3D12_RESOURCE_ALLOCATION_INFO a = ours->GetResourceAllocationInfo(0, 3, buffers);
const D3D12_RESOURCE_ALLOCATION_INFO b = host->GetResourceAllocationInfo(0, 3, buffers);
if (a.SizeInBytes != b.SizeInBytes || a.Alignment != b.Alignment) mismatch("three buffers", a, b);
// The total is rounded up to the largest alignment, saturates rather than wraps, and is aligned 1 when empty.
// The textures here are smaller than their alignment, so their sizes are fixed too.
const D3D12_RESOURCE_DESC to_total[][2] = {
{buffer_desc(300), with(texture_desc(16, 16, rgba8, none), k4)},
{with(texture_desc(64, 64, rgba8, rt), 0, 4), buffer_desc(300)},
{buffer_desc(1ull << 63), buffer_desc(1ull << 63)},
{buffer_desc(UINT64_MAX - k64), buffer_desc(1)},
};
const char* total_names[] = {"buffer + 4 KiB texture", "4x MSAA + buffer", "two 2^63 buffers",
"largest buffer + buffer"};
// The last two overflow 2^64. Hosts disagree there: WARP and the RTX 5070/4070 SUPER hosts of #1204 saturate at
// 0xFFFFFFFFFFFF0000, while the Intel Raptor Lake-S (101.7092) and RTX 5060 Laptop (621.24) hosts on Windows
// 26300.9974 (D3D12Core 10.0.26100.9818) wrap, to 0 and 65536, and WARP on that same PC saturates. Ours is pinned
// to the saturated answer, which can't make a caller allocate less than one of its resources; a host whose total
// is below its largest resource has wrapped, and is reported rather than compared.
constexpr UINT64 saturated = UINT64_MAX / k64 * k64;
for (int i = 0; i < 4; ++i) {
const D3D12_RESOURCE_ALLOCATION_INFO x = ours->GetResourceAllocationInfo(0, 2, to_total[i]);
const D3D12_RESOURCE_ALLOCATION_INFO y = host->GetResourceAllocationInfo(0, 2, to_total[i]);
if (i >= 2) {
if (x.SizeInBytes != saturated || x.Alignment != k64) mismatch(total_names[i], x, y);
else if (y.SizeInBytes < to_total[i][0].Width && y.Alignment == k64)
std::printf("allocation info %s: host wraps to %llu, ours saturates\n", total_names[i],
(unsigned long long)y.SizeInBytes);
else if (y.SizeInBytes != x.SizeInBytes || y.Alignment != x.Alignment)
mismatch(total_names[i], x, y);
continue;
}
if (x.SizeInBytes != y.SizeInBytes || x.Alignment != y.Alignment) mismatch(total_names[i], x, y);
}
const D3D12_RESOURCE_ALLOCATION_INFO empty_a = ours->GetResourceAllocationInfo(0, 0, nullptr);
const D3D12_RESOURCE_ALLOCATION_INFO empty_b = host->GetResourceAllocationInfo(0, 0, nullptr);
if (empty_a.SizeInBytes != empty_b.SizeInBytes || empty_a.Alignment != empty_b.Alignment)
mismatch("no resources", empty_a, empty_b);
// A visible mask above 1 names a node a single-node adapter does not have: the host answers the invalid
// allocation (size UINT64_MAX, 64 KiB alignment), for one resource and for none, through GetResourceAllocationInfo
// and its 1 and 2 variants, and leaves the per-resource array as it was.
{
Ref<ID3D12Device4> ours4, host4;
Ref<ID3D12Device8> ours8_mask, host8_mask;
ours->QueryInterface(IID_PPV_ARGS(&ours4.p));
host->QueryInterface(IID_PPV_ARGS(&host4.p));
ours->QueryInterface(IID_PPV_ARGS(&ours8_mask.p));
host->QueryInterface(IID_PPV_ARGS(&host8_mask.p));
const D3D12_RESOURCE_DESC buffer = buffer_desc(256);
D3D12_RESOURCE_DESC1 buffer1{};
buffer1.Dimension = buffer.Dimension;
buffer1.Width = buffer.Width;
buffer1.Height = buffer1.DepthOrArraySize = buffer1.MipLevels = 1;
buffer1.SampleDesc.Count = 1;
buffer1.Layout = buffer.Layout;
const auto same_info = [](const D3D12_RESOURCE_ALLOCATION_INFO& x, const D3D12_RESOURCE_ALLOCATION_INFO& y) {
return x.SizeInBytes == y.SizeInBytes && x.Alignment == y.Alignment;
};
for (const UINT mask : {0u, 1u, 2u, 3u, 0x80000000u}) {
char name[64];
std::snprintf(name, sizeof(name), "visible mask 0x%x", mask);
for (const UINT count : {0u, 1u}) {
const D3D12_RESOURCE_DESC* descs = count ? &buffer : nullptr;
const auto ia = ours->GetResourceAllocationInfo(mask, count, descs);
const auto ib = host->GetResourceAllocationInfo(mask, count, descs);
if (!same_info(ia, ib)) mismatch(name, ia, ib);
}
if (ours4.p && host4.p) {
const D3D12_RESOURCE_ALLOCATION_INFO1 sentinel{0xdead, 0xbeef, 0xcafe};
D3D12_RESOURCE_ALLOCATION_INFO1 each_a = sentinel, each_b = sentinel;
const auto ia = ours4->GetResourceAllocationInfo1(mask, 1, &buffer, &each_a);
const auto ib = host4->GetResourceAllocationInfo1(mask, 1, &buffer, &each_b);
if (!same_info(ia, ib) || each_a.Offset != each_b.Offset || each_a.SizeInBytes != each_b.SizeInBytes ||
each_a.Alignment != each_b.Alignment)
mismatch("visible mask, GetResourceAllocationInfo1", ia, ib);
}
if (ours8_mask.p && host8_mask.p) {
const D3D12_RESOURCE_ALLOCATION_INFO1 sentinel{0xdead, 0xbeef, 0xcafe};
D3D12_RESOURCE_ALLOCATION_INFO1 each_a = sentinel, each_b = sentinel;
const auto ia = ours8_mask->GetResourceAllocationInfo2(mask, 1, &buffer1, &each_a);
const auto ib = host8_mask->GetResourceAllocationInfo2(mask, 1, &buffer1, &each_b);
if (!same_info(ia, ib) || each_a.Offset != each_b.Offset || each_a.SizeInBytes != each_b.SizeInBytes ||
each_a.Alignment != each_b.Alignment)
mismatch("visible mask, GetResourceAllocationInfo2", ia, ib);
}
}
}
Ref<ID3D12Device8> ours8;
// MULTISAMPLE_QUALITY_LEVELS with TILED_RESOURCE: the same answers as the host, except 128-bit formats at 4x,
// which the AMD drivers refuse and WARP and NVIDIA report (and GetResourceAllocationInfo allocates here).
for (const DXGI_FORMAT format : {rgba8, DXGI_FORMAT_R16G16B16A16_FLOAT, DXGI_FORMAT_R32G32_FLOAT,
DXGI_FORMAT_R32G32B32A32_FLOAT, DXGI_FORMAT_D32_FLOAT,
DXGI_FORMAT_R32G32B32_FLOAT}) {
for (const UINT samples : {1u, 2u, 4u, 8u, 16u}) {
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS x{format, samples,
D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_TILED_RESOURCE};
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS y = x;
const HRESULT hx = ours->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &x, sizeof(x));
const HRESULT hy = host->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &y, sizeof(y));
const bool wide = format == DXGI_FORMAT_R32G32B32A32_FLOAT;
if (hx == hy && (x.NumQualityLevels == y.NumQualityLevels ||
(wide && samples == 4 && x.NumQualityLevels == 1 && y.NumQualityLevels == 0)))
continue;
if (mismatches++ < 24)
std::fprintf(stderr, "tiled quality levels format %d %ux: ours 0x%08lx %u, host 0x%08lx %u\n",
int(format), samples, static_cast<unsigned long>(hx), x.NumQualityLevels,
static_cast<unsigned long>(hy), y.NumQualityLevels);
}
}
if (FAILED(ours->QueryInterface(IID_PPV_ARGS(&ours8.p)))) return mismatches + 1;
const D3D12_RESOURCE_DESC mixed[] = {with(texture_desc(16, 16, rgba8, none), k4), buffer_desc(300),
with(texture_desc(64, 64, rgba8, rt), 0, 4)};
D3D12_RESOURCE_DESC1 mixed1[3]{};
for (int i = 0; i < 3; ++i) std::memcpy(&mixed1[i], &mixed[i], sizeof(mixed[i]));
D3D12_RESOURCE_ALLOCATION_INFO1 each[3]{}, each2[3]{};
const D3D12_RESOURCE_ALLOCATION_INFO total = ours8->GetResourceAllocationInfo1(0, 3, mixed, each);
const D3D12_RESOURCE_ALLOCATION_INFO total2 = ours8->GetResourceAllocationInfo2(0, 3, mixed1, each2);
const auto aligned = [](UINT64 v, UINT64 to) { return (v + to - 1) / to * to; };
bool layout_ok = total.SizeInBytes == total2.SizeInBytes && total.Alignment == total2.Alignment &&
each[0].Offset == 0 && each[0].Alignment == k4 && each[1].Alignment == k64 &&
each[2].Alignment == D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT &&
total.Alignment == D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT &&
total.SizeInBytes == each[2].Offset + each[2].SizeInBytes;
for (int i = 0; i < 3; ++i) {
const D3D12_RESOURCE_ALLOCATION_INFO single = ours->GetResourceAllocationInfo(0, 1, &mixed[i]);
layout_ok = layout_ok && each[i].SizeInBytes == single.SizeInBytes &&
each[i].Alignment == single.Alignment && each[i].Offset == each2[i].Offset &&
each[i].SizeInBytes == each2[i].SizeInBytes && each[i].Alignment == each2[i].Alignment &&
(i == 0 || each[i].Offset == aligned(each[i - 1].Offset + each[i - 1].SizeInBytes,
each[i].Alignment));
}
// An invalid description in the list makes the whole answer invalid.
const D3D12_RESOURCE_DESC with_bad[] = {buffer_desc(300), buffer_desc(0)};
layout_ok = layout_ok && ours8->GetResourceAllocationInfo1(0, 2, with_bad, each).SizeInBytes == UINT64_MAX &&
host->GetResourceAllocationInfo(0, 2, with_bad).SizeInBytes == UINT64_MAX;
if (!layout_ok) {
++mismatches;
std::fprintf(stderr, "allocation info: Device4/Device8 per-resource layout wrong (total %llu/%llu)\n",
(unsigned long long)total.SizeInBytes, (unsigned long long)total.Alignment);
}
return mismatches;
}
struct Rule {
const char* name;
HRESULT result;
};
constexpr const char* kOpenListExecuted = "device after executing an open list";
// Command-list and allocator misuse (D3D-51), in one fixed order on one device; each entry is what that call
// returned. Run on the PC runtime and on ours, the two lists must agree. The queue waits on a fence the CPU signals
// later, so "in flight" really is: the GPU cannot have finished the list yet.
std::vector<Rule> command_list_rules(ID3D12Device* device) {
std::vector<Rule> out;
const auto add = [&](const char* name, HRESULT hr) { out.push_back({name, hr}); };
constexpr D3D12_COMMAND_LIST_TYPE direct = D3D12_COMMAND_LIST_TYPE_DIRECT;
Ref<ID3D12CommandAllocator> alloc, other, compute;
Ref<ID3D12CommandQueue> queue;
Ref<ID3D12Fence> gate, done;
D3D12_COMMAND_QUEUE_DESC queue_desc{};
queue_desc.Type = direct;
if (FAILED(device->CreateCommandAllocator(direct, IID_PPV_ARGS(&alloc.p))) ||
FAILED(device->CreateCommandAllocator(direct, IID_PPV_ARGS(&other.p))) ||
FAILED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE, IID_PPV_ARGS(&compute.p))) ||
FAILED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))) ||
FAILED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&gate.p))) ||
FAILED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&done.p)))) {
add("setup", E_FAIL);
return out;
}
Ref<ID3D12GraphicsCommandList> list, second, third;
add("CreateCommandList", device->CreateCommandList(0, direct, alloc.p, nullptr, IID_PPV_ARGS(&list.p)));
if (!list.p) return out;
add("Reset while recording", list->Reset(alloc.p, nullptr));
add("allocator Reset while a list records into it", alloc->Reset());
add("CreateCommandList on an allocator a list records into",
device->CreateCommandList(0, direct, alloc.p, nullptr, IID_PPV_ARGS(&second.p)));
add("Close", list->Close());
add("Close twice", list->Close());
// Not compared: Reset with no allocator. The PC runtime dereferences the null pointer (an access violation);
// ours returns E_INVALIDARG.
add("Reset with a compute allocator", list->Reset(compute.p, nullptr));
add("Reset", list->Reset(alloc.p, nullptr));
add("CreateCommandList on another allocator",
device->CreateCommandList(0, direct, other.p, nullptr, IID_PPV_ARGS(&third.p)));
if (third.p) {
add("Close the other list", third->Close());
add("Reset onto an allocator another list records into", third->Reset(alloc.p, nullptr));
}
add("Close", list->Close());
add("allocator Reset with its lists closed", alloc->Reset());
// In flight: the queue first waits for `gate`, which only the CPU signals, after the checks below.
add("Reset", list->Reset(alloc.p, nullptr));
add("Close", list->Close());
add("queue Wait", queue->Wait(gate.p, 1));
ID3D12CommandList* lists[] = {list.p};
queue->ExecuteCommandLists(1, lists);
add("allocator Reset while its list is in flight", alloc->Reset());
add("Reset the in-flight list onto another allocator", list->Reset(other.p, nullptr));
add("Close", list->Close());
add("fence Signal from the CPU", gate->Signal(1));
add("queue Signal", queue->Signal(done.p, 1));
add("SetEventOnCompletion (blocking)", done->SetEventOnCompletion(1, nullptr));
add("allocator Reset after the GPU finished", alloc->Reset());
// Last, since the PC runtime removes the device for it: executing a list that is still open.
add("Reset", list->Reset(alloc.p, nullptr));
queue->ExecuteCommandLists(1, lists);
add(kOpenListExecuted, device->GetDeviceRemovedReason());
return out;
}
// Returns the number of rules where ours differs from the PC runtime, or -1 without a host device. One difference
// is known and allowed: the PC runtime removes the device (DXGI_ERROR_INVALID_CALL) for executing an open list,
// while this layer submits nothing and keeps the device (docs/d3d12_vulkan.md).
int compare_command_list_rules(ID3D12Device* ours) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
const std::vector<Rule> a = command_list_rules(ours), b = command_list_rules(host.p);
int mismatches = a.size() == b.size() ? 0 : 1;
for (size_t i = 0; i < a.size() || i < b.size(); ++i) {
const Rule* x = i < a.size() ? &a[i] : nullptr;
const Rule* y = i < b.size() ? &b[i] : nullptr;
const bool same = x && y && x->result == y->result;
const bool known = x && y && std::strcmp(x->name, kOpenListExecuted) == 0 && x->result == S_OK &&
y->result == DXGI_ERROR_INVALID_CALL;
if (!same && !known) ++mismatches;
std::printf("command-list rule %-55s ours 0x%08lx host 0x%08lx%s\n", (x ? x : y)->name,
x ? static_cast<unsigned long>(x->result) : 0ul, y ? static_cast<unsigned long>(y->result) : 0ul,
same ? "" : known ? " (known: an invalid call does not remove our device)" : " <-- differs");
}
return mismatches;
}
D3D12_RESOURCE_DESC reserved_desc(D3D12_RESOURCE_DIMENSION dimension, UINT64 width, UINT height, UINT16 depth,
UINT16 mips, DXGI_FORMAT format) {
D3D12_RESOURCE_DESC desc = dimension == D3D12_RESOURCE_DIMENSION_BUFFER
? buffer_desc(width)
: texture_desc(UINT(width), height, format, D3D12_RESOURCE_FLAG_NONE, mips, depth);
desc.Dimension = dimension;
if (dimension != D3D12_RESOURCE_DIMENSION_BUFFER) desc.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return desc;
}
// GetResourceTiling against the PC runtime, for the same reserved resources. What D3D12 defines must match: the
// tile shape, every standard mip's tile counts and its start within its array slice, and that no mip filling a
// whole tile is packed. Which smaller mips are packed (D3D11.3 spec 5.9.2.8.5: "may or may not be packed,
// depending on the IHV") and how many tiles they take is the driver's choice (src/resource_tiling.h), and with it
// the slice size and the total: compared only against a host that splits at the same mip. A layout the host
// refuses is skipped (drivers differ in what they accept); one the host accepts and ours refuses is a mismatch.
// Returns the number of mismatches, or -1 without a host D3D12 device with tiled resources or with every layout
// skipped.
int compare_tiling(ID3D12Device* ours) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
D3D12_FEATURE_DATA_D3D12_OPTIONS options{};
if (FAILED(host->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options))) ||
options.TiledResourcesTier == D3D12_TILED_RESOURCES_TIER_NOT_SUPPORTED)
return -1;
const bool volumes = options.TiledResourcesTier >= D3D12_TILED_RESOURCES_TIER_3;
const auto B = D3D12_RESOURCE_DIMENSION_BUFFER, T2 = D3D12_RESOURCE_DIMENSION_TEXTURE2D,
T3 = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
const D3D12_RESOURCE_DESC cases[] = {
reserved_desc(B, 200000, 1, 1, 1, DXGI_FORMAT_UNKNOWN),
reserved_desc(T2, 1024, 1024, 1, 0, DXGI_FORMAT_R8G8B8A8_UNORM),
reserved_desc(T2, 1000, 300, 3, 0, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB),
reserved_desc(T2, 512, 512, 1, 0, DXGI_FORMAT_R8_UNORM),
reserved_desc(T2, 512, 512, 1, 0, DXGI_FORMAT_R16_FLOAT),
reserved_desc(T2, 512, 512, 1, 0, DXGI_FORMAT_R16G16B16A16_FLOAT),
reserved_desc(T2, 512, 512, 2, 0, DXGI_FORMAT_R32G32B32A32_FLOAT),
reserved_desc(T2, 2048, 2048, 1, 0, DXGI_FORMAT_BC1_UNORM),
reserved_desc(T2, 2048, 1024, 2, 0, DXGI_FORMAT_BC7_UNORM),
reserved_desc(T2, 1000, 600, 1, 0, DXGI_FORMAT_BC3_UNORM),
reserved_desc(T2, 128, 128, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM),
reserved_desc(T2, 100, 100, 1, 0, DXGI_FORMAT_R8G8B8A8_UNORM),
reserved_desc(T2, 4096, 64, 1, 0, DXGI_FORMAT_B8G8R8A8_UNORM),
reserved_desc(T2, 1024, 1024, 4, 3, DXGI_FORMAT_R32_FLOAT),
reserved_desc(T3, 256, 256, 64, 0, DXGI_FORMAT_R8G8B8A8_UNORM),
reserved_desc(T3, 128, 128, 128, 0, DXGI_FORMAT_R8_UNORM),
reserved_desc(T3, 64, 64, 64, 0, DXGI_FORMAT_R16G16B16A16_FLOAT),
reserved_desc(T3, 64, 64, 64, 0, DXGI_FORMAT_R32G32B32A32_FLOAT),
};
struct Tiling {
UINT total = 0, count = 64;
D3D12_PACKED_MIP_INFO packed{};
D3D12_TILE_SHAPE shape{};
D3D12_SUBRESOURCE_TILING subresources[64]{};
};
int mismatches = 0, compared = 0;
const auto report = [&](int index, const char* what, UINT a, UINT b) {
if (mismatches++ < 24) std::fprintf(stderr, "tiling case %d %s: ours %u host %u\n", index, what, a, b);
};
for (int index = 0; index < int(std::size(cases)); ++index) {
const D3D12_RESOURCE_DESC& desc = cases[index];
if (desc.Dimension == T3 && !volumes) continue;
Ref<ID3D12Resource> a, b;
const HRESULT ours_hr =
ours->CreateReservedResource(&desc, D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&a.p));
const HRESULT host_hr =
host->CreateReservedResource(&desc, D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&b.p));
if (FAILED(host_hr)) {
// Which reserved resources a PC driver accepts varies: an RTX 3090 on driver 616.56 refuses texture
// arrays whose mips reach the packed tail (cases 2, 6 and 8, at tier 3), an RTX 5070 on 621.01 accepts
// them. The console is the reference for what the layer accepts, not the host, so there is nothing to
// compare here.
std::printf("GetResourceTiling case %d: the host runtime refuses it (0x%08lx), skipped\n", index,
static_cast<unsigned long>(host_hr));
continue;
}
if (FAILED(ours_hr)) {
report(index, "creation refused by ours, accepted by the host (HRESULT low word)", ours_hr & 0xffff, 0);
continue;
}
++compared;
Tiling x, y;
ours->GetResourceTiling(a.p, &x.total, &x.packed, &x.shape, &x.count, 0, x.subresources);
host->GetResourceTiling(b.p, &y.total, &y.packed, &y.shape, &y.count, 0, y.subresources);
if (x.shape.WidthInTexels != y.shape.WidthInTexels) report(index, "tile width", x.shape.WidthInTexels, y.shape.WidthInTexels);
if (x.shape.HeightInTexels != y.shape.HeightInTexels) report(index, "tile height", x.shape.HeightInTexels, y.shape.HeightInTexels);
if (x.shape.DepthInTexels != y.shape.DepthInTexels) report(index, "tile depth", x.shape.DepthInTexels, y.shape.DepthInTexels);
if (x.count != y.count) {
report(index, "subresource count", x.count, y.count);
continue;
}
// Which small mips are packed is the driver's choice (D3D11.3 functional spec 5.9.2.8.5, D3D12_PACKED_MIP_INFO):
// only a mip that fills at least one standard tile in every dimension must stay standard; a coarser mip with
// any dimension below the tile "may or may not be packed, depending on the IHV", and once a mip is packed all
// coarser ones are. Hosts differ: an RTX 5070 packs from the first mip below the tile (like ours), a Radeon
// 880M keeps one or two more standard, WARP packs nothing. So the layer must keep every mip that fills a
// tile standard and be self-consistent; the host's boundary is informational, and tile counts are compared
// only for mips both treat as standard (and the packed totals only when both split at the same mip).
const UINT layers = desc.Dimension == T2 ? desc.DepthOrArraySize : 1;
const UINT mips = x.count / layers;
const UINT slice_x = x.total / layers, slice_y = y.total / layers;
UINT shared = 0;
if (desc.Dimension != B) {
UINT required = 0; // leading mips that fill a whole tile in every dimension
for (UINT m = 0; m < mips; ++m) {
const UINT64 w = std::max<UINT64>(1, desc.Width >> m);
const UINT h = std::max<UINT>(1, desc.Height >> m);
const UINT d = desc.Dimension == T3 ? std::max<UINT>(1, desc.DepthOrArraySize >> m) : 1;
if (w < x.shape.WidthInTexels || h < x.shape.HeightInTexels || d < x.shape.DepthInTexels) break;
++required;
}
if (x.packed.NumStandardMips < required || x.packed.NumStandardMips + x.packed.NumPackedMips != int(mips))
report(index, "standard mips (a mip filling a tile must not be packed)", x.packed.NumStandardMips, required);
if (y.packed.NumStandardMips < required)
report(index, "host standard mips below the D3D12 minimum", y.packed.NumStandardMips, required);
if (bool(x.packed.NumPackedMips) != bool(x.packed.NumTilesForPackedMips))
report(index, "packed tiles", x.packed.NumPackedMips, x.packed.NumTilesForPackedMips);
if (x.packed.NumStandardMips != y.packed.NumStandardMips)
std::printf("GetResourceTiling case %d: standard mips ours %u, host %u (driver's choice, not compared)\n",
index, x.packed.NumStandardMips, y.packed.NumStandardMips);
shared = std::min(x.packed.NumStandardMips, y.packed.NumStandardMips);
if (x.packed.NumStandardMips == y.packed.NumStandardMips) {
if (x.packed.NumPackedMips != y.packed.NumPackedMips) report(index, "packed mips", x.packed.NumPackedMips, y.packed.NumPackedMips);
if (x.packed.StartTileIndexInOverallResource != y.packed.StartTileIndexInOverallResource)
report(index, "packed start", x.packed.StartTileIndexInOverallResource, y.packed.StartTileIndexInOverallResource);
if (slice_x - x.packed.NumTilesForPackedMips != slice_y - y.packed.NumTilesForPackedMips)
report(index, "standard tiles per slice", slice_x - x.packed.NumTilesForPackedMips,
slice_y - y.packed.NumTilesForPackedMips);
}
} else if (x.total != y.total) {
report(index, "buffer tiles", x.total, y.total);
}
for (UINT i = 0; i < x.count; ++i) {
const auto &s = x.subresources[i], &t = y.subresources[i];
const UINT layer = i / mips;
if (desc.Dimension != B && i % mips >= x.packed.NumStandardMips) {
// Packed: no tiles of its own. (On slices after the first the PC runtime writes a tile index here,
// not the D3D12_PACKED_TILE its documentation gives; ours follows the documentation.)
if (s.WidthInTiles || s.HeightInTiles || s.DepthInTiles || s.StartTileIndexInOverallResource != D3D12_PACKED_TILE)
report(index, "packed subresource", i, 0);
continue;
}
if (desc.Dimension != B && i % mips >= shared) continue; // standard in ours only: the host packs it
if (s.WidthInTiles != t.WidthInTiles || s.HeightInTiles != t.HeightInTiles || s.DepthInTiles != t.DepthInTiles ||
s.StartTileIndexInOverallResource - layer * slice_x != t.StartTileIndexInOverallResource - layer * slice_y)
report(index, "standard subresource", i, 0);
}
}
return compared ? mismatches : -1;
}
// whole_rectangle is a rectangle over the whole source: by the documentation the same as a null rectangle.
enum class ResolveScenario { full, rectangle, null_rectangle, whole_rectangle };
const char* resolve_scenario_name(ResolveScenario scenario) {
switch (scenario) {
case ResolveScenario::full: return "ResolveSubresource";
case ResolveScenario::rectangle: return "ResolveSubresourceRegion(rect)";
case ResolveScenario::null_rectangle: return "ResolveSubresourceRegion(null rect)";
case ResolveScenario::whole_rectangle: return "ResolveSubresourceRegion(whole-source rect)";
}
return "unknown";
}
UINT resolve_pixel_size(DXGI_FORMAT format) {
switch (format) {
case DXGI_FORMAT_R8G8B8A8_UNORM:
case DXGI_FORMAT_B8G8R8A8_UNORM:
case DXGI_FORMAT_R9G9B9E5_SHAREDEXP:
case DXGI_FORMAT_R10G10B10A2_UNORM: return 4;
case DXGI_FORMAT_R16G16B16A16_FLOAT: return 8;
case DXGI_FORMAT_R32G32B32A32_FLOAT: return 16;
default: return 0;
}
}
// Runs one deterministic resolve on either the host D3D12 device or the Vulkan-backed proxy and returns only
// packed texel bytes (row-pitch padding is discarded). The source/destination are cleared to solid colours so
// every format and sample count exercises the resolve path without requiring a shader or game data.
bool capture_resolve(ID3D12Device* device, DXGI_FORMAT format, UINT samples, ResolveScenario scenario,
std::vector<std::uint8_t>& pixels) {
#define RESOLVE_CHECK(condition) do { \
if (!(condition)) { \
std::fprintf(stderr, "resolve capture failed at line %d: %s (format %d, samples %u, %s)\n", \
__LINE__, #condition, int(format), samples, resolve_scenario_name(scenario)); \
return false; \
} \
} while (false)
const bool proxy = rt::d3d12_xs::is_device_proxy(device);
const UINT src_width = 8, src_height = 8;
const UINT dst_width = scenario == ResolveScenario::full ? src_width : 12;
const UINT dst_height = scenario == ResolveScenario::full ? src_height : 10;
const UINT pixel_size = resolve_pixel_size(format);
RESOLVE_CHECK(pixel_size != 0);
const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT};
const D3D12_HEAP_PROPERTIES readback_heap{D3D12_HEAP_TYPE_READBACK};
D3D12_RESOURCE_DESC src_desc =
texture_desc(src_width, src_height, format, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
src_desc.SampleDesc.Count = samples;
D3D12_RESOURCE_DESC dst_desc =
texture_desc(dst_width, dst_height, format, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
Ref<ID3D12Resource> src, dst, readback;
RESOLVE_CHECK(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &src_desc,
D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr,
IID_PPV_ARGS(&src.p))));
RESOLVE_CHECK(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &dst_desc,
D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr,
IID_PPV_ARGS(&dst.p))));
D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 2, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
Ref<ID3D12DescriptorHeap> rtv_heap;
RESOLVE_CHECK(SUCCEEDED(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&rtv_heap.p))));
D3D12_CPU_DESCRIPTOR_HANDLE src_rtv{}, dst_rtv{};
if (proxy) {
const std::uint64_t start =
reinterpret_cast<XboxHeapHandleFn>(slot(rtv_heap.p, 9))(rtv_heap.p);
const UINT increment = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
RESOLVE_CHECK(start != 0 && increment >= sizeof(leonida::TargetView));
src_rtv = {SIZE_T(start)};
dst_rtv = {SIZE_T(start + increment)};
} else {
src_rtv = rtv_heap->GetCPUDescriptorHandleForHeapStart();
dst_rtv = {src_rtv.ptr + device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV)};
}
D3D12_RENDER_TARGET_VIEW_DESC msaa_view{};
msaa_view.Format = format;
msaa_view.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DMS;
device->CreateRenderTargetView(src.p, &msaa_view, src_rtv);
device->CreateRenderTargetView(dst.p, nullptr, dst_rtv);
D3D12_COMMAND_QUEUE_DESC queue_desc{};
queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
Ref<ID3D12CommandQueue> queue;
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
Ref<ID3D12GraphicsCommandList7> list7;
Ref<ID3D12Fence> fence;
RESOLVE_CHECK(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))));
RESOLVE_CHECK(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))));
RESOLVE_CHECK(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
IID_PPV_ARGS(&list.p))));
// ResolveSubresourceRegion is on ID3D12GraphicsCommandList7; the full ResolveSubresource path does not need
// it, so a host without list7 (Windows 10) can still be compared on the full form.
if (scenario != ResolveScenario::full)
RESOLVE_CHECK(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list7.p))));
RESOLVE_CHECK(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))));
UINT64 cursor = 0;
const auto readback_layout = place(device, dst.p, 0, cursor);
RESOLVE_CHECK(cursor != 0 && readback_layout.Footprint.RowPitch >= dst_width * pixel_size);
const D3D12_RESOURCE_DESC readback_desc = buffer_desc(cursor);
RESOLVE_CHECK(SUCCEEDED(device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE,
&readback_desc, D3D12_RESOURCE_STATE_COPY_DEST,
nullptr, IID_PPV_ARGS(&readback.p))));
const FLOAT green[4] = {0, 1, 0, 1};
const FLOAT red[4] = {1, 0, 0, 1};
// Use a rectangle even for the full source: multisampled images are cleared as rendering attachments,
// not with vkCmdClearColorImage (which is for single-sample transfer clears).
const D3D12_RECT full_source{0, 0, LONG(src_width), LONG(src_height)};
list->ClearRenderTargetView(src_rtv, green, 1, &full_source);
list->ClearRenderTargetView(dst_rtv, red, 0, nullptr);
const D3D12_RESOURCE_BARRIER before_resolve[] = {
transition(src.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_RESOLVE_SOURCE),
transition(dst.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_RESOLVE_DEST)};
list->ResourceBarrier(2, before_resolve);
if (scenario == ResolveScenario::full) {
list->ResolveSubresource(dst.p, 0, src.p, 0, format);
} else {
D3D12_RECT rectangle{2, 1, 6, 5};
if (scenario == ResolveScenario::whole_rectangle) rectangle = full_source;
list7->ResolveSubresourceRegion(dst.p, 0, 3, 2, src.p, 0,
scenario == ResolveScenario::null_rectangle ? nullptr : &rectangle,
format, D3D12_RESOLVE_MODE_AVERAGE);
}
const auto to_copy = transition(dst.p, D3D12_RESOURCE_STATE_RESOLVE_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &to_copy);
const auto readback_location = footprint(readback.p, readback_layout);
const auto resolved_location = subresource(dst.p, 0);
list->CopyTextureRegion(&readback_location, 0, 0, 0, &resolved_location, nullptr);
RESOLVE_CHECK(list->Close() == S_OK);
ID3D12CommandList* lists[] = {list.p};
queue->ExecuteCommandLists(1, lists);
if (proxy)
RESOLVE_CHECK(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
RESOLVE_CHECK(SUCCEEDED(queue->Signal(fence.p, 1)) && SUCCEEDED(fence->SetEventOnCompletion(1, nullptr)));
D3D12_RANGE read_range{0, SIZE_T(cursor)};
std::uint8_t* mapped = nullptr;
RESOLVE_CHECK(SUCCEEDED(readback->Map(0, &read_range, reinterpret_cast<void**>(&mapped))) && mapped);
pixels.resize(size_t(dst_width) * dst_height * pixel_size);
for (UINT row = 0; row < dst_height; ++row)
std::memcpy(pixels.data() + size_t(row) * dst_width * pixel_size,
mapped + readback_layout.Offset + size_t(row) * readback_layout.Footprint.RowPitch,
size_t(dst_width) * pixel_size);
const D3D12_RANGE written{0, 0};
readback->Unmap(0, &written);
#undef RESOLVE_CHECK
return true;
}
// Compare RGB9E5 capability queries with the sample counts of the actual Vulkan attachment, then
// resolve green over a red single-sample destination and check its packed shared-exponent texels.
bool check_rgb9e5_resolves(ID3D12Device* device, const vktest::Device& vk) {
constexpr auto format = DXGI_FORMAT_R9G9B9E5_SHAREDEXP;
constexpr VkImageUsageFlags usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
VkImageFormatProperties properties{};
const auto query = vk.instance_fn<PFN_vkGetPhysicalDeviceImageFormatProperties>("vkGetPhysicalDeviceImageFormatProperties");
const auto result = query(vk.physical, VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, VK_IMAGE_TYPE_2D,
VK_IMAGE_TILING_OPTIMAL, usage, 0, &properties);
if (result != VK_SUCCESS && result != VK_ERROR_FORMAT_NOT_SUPPORTED) return false;
const VkSampleCountFlags counts = result == VK_SUCCESS ? properties.sampleCounts : 0;
D3D12_FEATURE_DATA_FORMAT_SUPPORT support{format};
if (FAILED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support)))) return false;
constexpr UINT bits = D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RENDERTARGET | D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RESOLVE;
const bool multisampled = (counts & (2u | 4u | 8u | 16u | 32u)) != 0;
if ((UINT(support.Support1) & bits) != (multisampled ? bits : 0u)) {
std::fprintf(stderr, "RGB9E5 Support1=0x%x, Vulkan sample counts=0x%x\n", UINT(support.Support1), counts);
return false;
}
for (UINT samples : {2u, 4u, 8u, 16u, 32u}) {
const bool supported = (counts & samples) != 0;
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS quality{format, samples};
if (FAILED(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &quality, sizeof(quality))) ||
quality.NumQualityLevels != UINT(supported)) return false;
if (!supported) continue;
std::vector<std::uint8_t> pixels;
if (!capture_resolve(device, format, samples, ResolveScenario::full, pixels) || pixels.size() != 8 * 8 * 4)
return false;
for (size_t i = 0; i < pixels.size(); i += 4) {
std::uint32_t packed;
std::memcpy(&packed, pixels.data() + i, sizeof(packed));
if (packed != 0x80020000u) { // green mantissa 256, shared exponent 16
std::fprintf(stderr, "RGB9E5 %ux resolve: pixel %zu got 0x%08x, expected 0x80020000\n",
samples, i / 4, packed);
return false;
}
}
}
return true;
}
// Compare D3D12 multisample quality queries for depth/stencil formats with the host runtime. The Vulkan
// backend has one standard sample pattern, so every supported count reports one quality level (index 0).
// A count the Vulkan GPU cannot do for depth and stencil attachments (depth_sample_counts, from its limits) is not
// something the layer can offer: a software host such as WARP advertises 16x, an RTX or Radeon does not.
int compare_depth_multisample_quality_levels(ID3D12Device* ours, VkSampleCountFlags depth_sample_counts) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
constexpr DXGI_FORMAT formats[] = {
DXGI_FORMAT_D32_FLOAT,
DXGI_FORMAT_D16_UNORM,
DXGI_FORMAT_D24_UNORM_S8_UINT,
DXGI_FORMAT_D32_FLOAT_S8X24_UINT,
DXGI_FORMAT_R24G8_TYPELESS,
DXGI_FORMAT_R32G8X24_TYPELESS,
};
constexpr UINT sample_counts[] = {2, 4, 8, 16, 32};
unsigned compared = 0, skipped = 0;
int mismatches = 0;
for (DXGI_FORMAT format : formats) {
for (UINT samples : sample_counts) {
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS host_levels{};
host_levels.Format = format;
host_levels.SampleCount = samples;
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS our_levels = host_levels;
const HRESULT host_hr = host->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS,
&host_levels, sizeof(host_levels));
if (FAILED(host_hr) || host_levels.NumQualityLevels == 0 || !(depth_sample_counts & samples)) {
++skipped;
continue;
}
++compared;
const HRESULT our_hr = ours->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS,
&our_levels, sizeof(our_levels));
if (FAILED(our_hr) || our_levels.NumQualityLevels != 1) {
std::fprintf(stderr,
"depth MSAA quality mismatch: format %d samples %u (host %u quality levels, Vulkan "
"HRESULT 0x%08lx / %u levels)\n",
int(format), samples, host_levels.NumQualityLevels,
static_cast<unsigned long>(our_hr), our_levels.NumQualityLevels);
++mismatches;
}
}
}
std::printf("Depth/stencil MSAA quality queries: %u compared, %u host-unsupported skipped, %d mismatches\n",
compared, skipped, mismatches);
return mismatches;
}
bool check_plain_resolve_formats(ID3D12Device* device) {
struct Case { DXGI_FORMAT format; bool valid; bool depth = false; DXGI_FORMAT requested = DXGI_FORMAT_UNKNOWN; };
constexpr Case cases[] = {
{DXGI_FORMAT_R8G8B8A8_UINT, false},
{DXGI_FORMAT_R8G8B8A8_SINT, false},
{DXGI_FORMAT_R32_UINT, false},
{DXGI_FORMAT_R32_SINT, false},
{DXGI_FORMAT_R32G32B32A32_SINT, false},
{DXGI_FORMAT_R8G8B8A8_UINT, false, false, DXGI_FORMAT_R8G8B8A8_UNORM},
{DXGI_FORMAT_R8G8B8A8_UNORM, true},
{DXGI_FORMAT_R16G16B16A16_FLOAT, true},
{DXGI_FORMAT_D32_FLOAT, true, true},
};
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
if (FAILED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))) ||
FAILED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
IID_PPV_ARGS(&list.p)))) return false;
const D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_DEFAULT};
bool ok = true;
unsigned tested = 0, skipped = 0;
for (const auto& c : cases) {
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS quality{c.format, 4};
if (FAILED(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &quality, sizeof(quality))))
return false;
if (!quality.NumQualityLevels) {
std::printf("plain resolve format %d: 4x MSAA unsupported, skipped\n", int(c.format));
++skipped;
continue;
}
auto desc = texture_desc(8, 8, c.format, c.depth ? D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL
: D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.SampleDesc.Count = 4;
Ref<ID3D12Resource> src, dst;
if (FAILED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc,
D3D12_RESOURCE_STATE_RESOLVE_SOURCE, nullptr,
IID_PPV_ARGS(&src.p)))) return false;
desc.SampleDesc.Count = 1;
if (FAILED(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc,
D3D12_RESOURCE_STATE_RESOLVE_DEST, nullptr,
IID_PPV_ARGS(&dst.p)))) return false;
const DXGI_FORMAT requested = c.requested == DXGI_FORMAT_UNKNOWN ? c.format : c.requested;
list->ResolveSubresource(dst.p, 0, src.p, 0, requested);
const HRESULT result = list->Close();
if (result != (c.valid ? S_OK : E_INVALIDARG)) {
std::fprintf(stderr, "plain resolve format %d: Close returned 0x%08lx, expected 0x%08lx\n",
int(c.format), static_cast<unsigned long>(result),
static_cast<unsigned long>(c.valid ? S_OK : E_INVALIDARG));
ok = false;
}
++tested;
if (FAILED(list->Reset(allocator.p, nullptr))) return false;
}
if (FAILED(list->Close())) return false;
std::printf("plain resolve format validation: %u tested, %u unsupported cases skipped\n", tested, skipped);
return ok;
}
// Compare the Vulkan-backed resolve output byte-for-byte with the host runtime for every common supported format,
// sample count and region form. A host-unsupported combination is a deliberate skip; a mismatch in advertised
// support or in the resulting pixels is a failure. The region forms need ID3D12GraphicsCommandList7 on the host;
// without it (Windows 10) only the full ResolveSubresource form is compared, so the later device checks still run.
int compare_resolves(ID3D12Device* ours, VkSampleCountFlags color_sample_counts) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
const bool host_regions = host_has_graphics_command_list7(host.p);
if (!host_regions)
std::printf("Resolve comparison: the host runtime has no ID3D12GraphicsCommandList7; "
"ResolveSubresourceRegion cases are skipped\n");
constexpr DXGI_FORMAT formats[] = {
DXGI_FORMAT_R8G8B8A8_UNORM,
DXGI_FORMAT_B8G8R8A8_UNORM,
DXGI_FORMAT_R10G10B10A2_UNORM,
DXGI_FORMAT_R16G16B16A16_FLOAT,
DXGI_FORMAT_R32G32B32A32_FLOAT,
};
constexpr UINT sample_counts[] = {2, 4, 8, 16, 32};
constexpr UINT required =
UINT(D3D12_FORMAT_SUPPORT1_RENDER_TARGET) | UINT(D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RENDERTARGET) |
UINT(D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RESOLVE);
constexpr ResolveScenario scenarios[] = {
ResolveScenario::full, ResolveScenario::rectangle, ResolveScenario::null_rectangle,
ResolveScenario::whole_rectangle};
int mismatches = 0;
unsigned compared = 0, matched = 0, skipped = 0;
for (DXGI_FORMAT format : formats) {
D3D12_FEATURE_DATA_FORMAT_SUPPORT host_format{format}, our_format{format};
const HRESULT host_format_hr =
host->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &host_format, sizeof(host_format));
const HRESULT our_format_hr =
ours->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &our_format, sizeof(our_format));
if (FAILED(host_format_hr) || (UINT(host_format.Support1) & required) != required) {
std::printf("resolve format %d: host does not advertise multisample colour resolve, skipped\n", int(format));
++skipped;
continue;
}
if (FAILED(our_format_hr) || (UINT(our_format.Support1) & required) != required) {
std::fprintf(stderr, "resolve format %d: Vulkan device does not match host multisample format support\n",
int(format));
++mismatches;
continue;
}
for (UINT samples : sample_counts) {
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS host_levels{};
host_levels.Format = format;
host_levels.SampleCount = samples;
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS our_levels = host_levels;
const HRESULT host_hr = host->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS,
&host_levels, sizeof(host_levels));
const HRESULT our_hr = ours->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS,
&our_levels, sizeof(our_levels));
// Not a count the Vulkan GPU can do for colour attachments (a software host like WARP offers 16x).
if (FAILED(host_hr) || host_levels.NumQualityLevels == 0 || !(color_sample_counts & samples)) {
++skipped;
continue;
}
if (FAILED(our_hr) || our_levels.NumQualityLevels == 0) {
std::fprintf(stderr, "resolve format %d samples %u: Vulkan reports unsupported, host supports it\n",
int(format), samples);
++mismatches;
continue;
}
for (ResolveScenario scenario : scenarios) {
if (!host_regions && scenario != ResolveScenario::full) {
++skipped;
continue;
}
std::vector<std::uint8_t> host_pixels, our_pixels;
if (!capture_resolve(host.p, format, samples, scenario, host_pixels) ||
!capture_resolve(ours, format, samples, scenario, our_pixels)) {
++mismatches;
continue;
}
++compared;
if (host_pixels != our_pixels && scenario == ResolveScenario::null_rectangle) {
// A null rectangle is the whole source (ID3D12GraphicsCommandList1::ResolveSubresourceRegion).
// Some hosts do not honour that (the Radeon 880M on driver 26.11.2 and WARP resolve to the
// destination's origin and leave the rest unwritten; an RTX 5070 follows the documentation), so
// where the host disagrees with its own whole-source rectangle, the layer is held to the
// documented result: that rectangle's.
std::vector<std::uint8_t> host_whole;
if (capture_resolve(host.p, format, samples, ResolveScenario::whole_rectangle, host_whole) &&
host_whole != host_pixels && host_whole == our_pixels) {
std::printf("resolve format %d samples %u: the host's null-rectangle resolve differs from "
"its whole-source rectangle (host quirk); ours follows the documentation\n",
int(format), samples);
++matched;
continue;
}
}
if (host_pixels != our_pixels) {
std::fprintf(stderr, "resolve mismatch: format %d samples %u %s (host %zu bytes, Vulkan %zu)\n",
int(format), samples, resolve_scenario_name(scenario),
host_pixels.size(), our_pixels.size());
++mismatches;
} else {
++matched;
}
}
}
}
std::printf("Resolve comparison: %u compared, %u matched, %u unsupported or host-unavailable cases skipped, "
"%d mismatches\n", compared, matched, skipped, mismatches);
if (compared == 0) {
std::fprintf(stderr, "Resolve comparison: no common supported format/sample combination was exercised\n");
++mismatches;
}
return mismatches;
}
// D3D12_FEATURE_SHADER_MODEL and D3D12_FEATURE_ROOT_SIGNATURE must give the host PC runtime's HRESULT, with the
// highest supported value clamped to what the layer can honour (DXIL 1.0, root signature 1.1). Returns the number
// of mismatches, or -1 when the host has no D3D12 device.
int compare_feature_support(ID3D12Device* ours) {
if (copycmp::host_is_wine()) return -1;
const HMODULE d3d12 = LoadLibraryExA("d3d12.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
const auto create = d3d12 ? reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(
reinterpret_cast<void*>(GetProcAddress(d3d12, "D3D12CreateDevice")))
: nullptr;
Ref<ID3D12Device> host;
if (!create || FAILED(create(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) return -1;
int mismatches = 0;
const auto report = [&](const char* what, UINT request, HRESULT got_hr, UINT got, HRESULT want_hr, UINT want) {
if (got_hr == want_hr && (FAILED(got_hr) || got == want)) return;
if (mismatches++ < 8)
std::fprintf(stderr, "%s request 0x%02x: ours hr=0x%08lX value 0x%02x; host hr=0x%08lX value 0x%02x\n",
what, request, static_cast<unsigned long>(got_hr), got,
static_cast<unsigned long>(want_hr), want);
};
// A host runtime older than a request refuses it with E_INVALIDARG where a newer one clamps it: Windows 10's
// d3d12.dll answers shader models up to 6.6 and root signatures up to 1.1. Such a request can't be compared with
// the host, so the layer's answer is checked against its own clamp instead. The requests ascend, so `answered`
// is the highest one the host has accepted so far.
unsigned predated = 0;
const auto host_predates = [&](const char* what, UINT request, UINT answered, HRESULT host_hr, HRESULT got_hr,
UINT got, UINT clamp) {
if (host_hr != E_INVALIDARG || !answered || request <= answered) return false;
++predated;
if ((got_hr != S_OK || got != clamp) && mismatches++ < 8)
std::fprintf(stderr, "%s request 0x%02x: ours hr=0x%08lX value 0x%02x; the host runtime predates it, "
"the layer's clamp is S_OK 0x%02x\n",
what, request, static_cast<unsigned long>(got_hr), got, clamp);
return true;
};
// 5.1 upwards: D3D12's own baseline, which every SDK names, then 6.0-6.9 by number (only 6.0 is guaranteed to be
// named by every SDK that has the query at all). And only values the runtime defines go to the host: it crashed
// the process on an undefined one (0x5f, and once on 0), so the layer's refusal of an undefined value is checked
// in main.
const D3D_SHADER_MODEL models[] = {
D3D_SHADER_MODEL_5_1, D3D_SHADER_MODEL_6_0, static_cast<D3D_SHADER_MODEL>(0x61),
static_cast<D3D_SHADER_MODEL>(0x62),
static_cast<D3D_SHADER_MODEL>(0x63), static_cast<D3D_SHADER_MODEL>(0x64),
static_cast<D3D_SHADER_MODEL>(0x65), static_cast<D3D_SHADER_MODEL>(0x66),
static_cast<D3D_SHADER_MODEL>(0x67), static_cast<D3D_SHADER_MODEL>(0x68),
static_cast<D3D_SHADER_MODEL>(0x69)};
UINT host_model = 0;
for (const D3D_SHADER_MODEL model : models) {
D3D12_FEATURE_DATA_SHADER_MODEL ours_data{model}, host_data{model};
const HRESULT ours_hr =
ours->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &ours_data, sizeof(ours_data));
const HRESULT host_hr =
host->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &host_data, sizeof(host_data));
if (SUCCEEDED(host_hr)) host_model = static_cast<UINT>(model);
if (host_predates("shader model", static_cast<UINT>(model), host_model, host_hr, ours_hr,
static_cast<UINT>(ours_data.HighestShaderModel),
std::min<UINT>(static_cast<UINT>(model), static_cast<UINT>(D3D_SHADER_MODEL_6_0))))
continue;
const UINT want = std::min<UINT>(static_cast<UINT>(host_data.HighestShaderModel),
static_cast<UINT>(D3D_SHADER_MODEL_6_0));
report("shader model", static_cast<UINT>(model), ours_hr,
static_cast<UINT>(ours_data.HighestShaderModel), host_hr, want);
}
// 0x3 is 1.2 by number: SDKs without the 1.2 headers do not name it (runtime/CMakeLists.txt probes for that).
const D3D_ROOT_SIGNATURE_VERSION versions[] = {D3D_ROOT_SIGNATURE_VERSION_1_0, D3D_ROOT_SIGNATURE_VERSION_1_1,
static_cast<D3D_ROOT_SIGNATURE_VERSION>(0x3)};
UINT host_version = 0;
for (const D3D_ROOT_SIGNATURE_VERSION version : versions) {
D3D12_FEATURE_DATA_ROOT_SIGNATURE ours_data{version}, host_data{version};
const HRESULT ours_hr =
ours->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &ours_data, sizeof(ours_data));
const HRESULT host_hr =
host->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &host_data, sizeof(host_data));
if (SUCCEEDED(host_hr)) host_version = static_cast<UINT>(version);
if (host_predates("root signature", static_cast<UINT>(version), host_version, host_hr, ours_hr,
static_cast<UINT>(ours_data.HighestVersion),
std::min<UINT>(static_cast<UINT>(version),
static_cast<UINT>(D3D_ROOT_SIGNATURE_VERSION_1_1))))
continue;
const UINT want = std::min<UINT>(static_cast<UINT>(host_data.HighestVersion),
static_cast<UINT>(D3D_ROOT_SIGNATURE_VERSION_1_1));
report("root signature", static_cast<UINT>(version), ours_hr,
static_cast<UINT>(ours_data.HighestVersion), host_hr, want);
}
if (predated)
std::printf("CheckFeatureSupport: %u requests the host runtime predates (e.g. Windows 10) checked against the "
"layer's clamp instead of the host\n", predated);
// A wrong DataSize is refused by both, in both queries.
{
D3D12_FEATURE_DATA_SHADER_MODEL ours_data{D3D_SHADER_MODEL_6_0};
D3D12_FEATURE_DATA_ROOT_SIGNATURE ours_root{D3D_ROOT_SIGNATURE_VERSION_1_1};
if (SUCCEEDED(ours->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &ours_data, sizeof(ours_data) - 1)))
++mismatches;
if (SUCCEEDED(ours->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &ours_root, sizeof(ours_root) - 1)))
++mismatches;
if (SUCCEEDED(ours->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, nullptr, sizeof(ours_data))))
++mismatches;
if (SUCCEEDED(ours->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, nullptr, sizeof(ours_root))))
++mismatches;
}
return mismatches;
}
// Same-adapter host D3D12 device matched by VkPhysicalDeviceIDProperties (LUID first, then PCI IDs), falling back
// to the default adapter. False if the host has no D3D12 device.
// D3D11.3 spec 3.2.3.8's ideal FLOAT -> SRGB encoding of c in [0, 1], before scaling to the format's maximum.
double ideal_srgb(double c) { return c <= 0.0031308 ? c * 12.92 : 1.055 * std::pow(c, 1.0 / 2.4) - 0.055; }
// Both bytes within 0.6 ULP (on the integer side) of the ideal value: the tolerance spec 3.2.3.6 (FLOAT -> UNORM)
// and 3.2.3.8 (FLOAT -> SRGB) permit, so two drivers may give the codes either side of a near-tie.
bool within_06_ulp(std::uint8_t a, std::uint8_t b, double ideal) {
return std::abs(double(a) - ideal) <= 0.6 && std::abs(double(b) - ideal) <= 0.6;
}
// The code nearest to `ideal`, a tie going to the even one: what the layer writes for a normalised clear (#521's
// quantize_unorm), whichever way the GPU's own driver rounds the tie.
double nearest_even_code(double ideal) {
const double r = std::floor(ideal + 0.5);
return r - ideal == 0.5 && std::fmod(r, 2.0) != 0.0 ? r - 1.0 : r;
}
bool create_host_device_for_physical(VkInstance instance, VkPhysicalDevice physical,
PFN_vkGetInstanceProcAddr instance_proc, Ref<ID3D12Device>& host,
std::string& adapter_name) {
if (copycmp::host_is_wine()) return false;
const HMODULE d3d12 = LoadLibraryExA("d3d12.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
const auto create = d3d12 ? reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(
reinterpret_cast<void*>(GetProcAddress(d3d12, "D3D12CreateDevice")))
: nullptr;
if (!create) return false;
VkPhysicalDeviceIDProperties id_props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES};
VkPhysicalDeviceProperties2 props2{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &id_props};
if (instance && physical && instance_proc) {
if (const auto get_props2 = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties2>(
instance_proc(instance, "vkGetPhysicalDeviceProperties2"))) {
get_props2(physical, &props2);
}
}
const HMODULE dxgi = LoadLibraryExA("dxgi.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
using CreateFactoryFn = HRESULT(WINAPI*)(REFIID, void**);
const auto create_factory = dxgi ? reinterpret_cast<CreateFactoryFn>(
reinterpret_cast<void*>(GetProcAddress(dxgi, "CreateDXGIFactory1")))
: nullptr;
Ref<IDXGIAdapter1> luid_match, pci_match;
std::string luid_name, pci_name;
if (create_factory) {
Ref<IDXGIFactory4> factory;
if (SUCCEEDED(create_factory(IID_PPV_ARGS(&factory.p)))) {
Ref<IDXGIAdapter1> candidate;
for (UINT i = 0; factory->EnumAdapters1(i, &candidate.p) != DXGI_ERROR_NOT_FOUND; ++i) {
DXGI_ADAPTER_DESC1 desc{};
if (SUCCEEDED(candidate->GetDesc1(&desc))) {
char utf8[256]{};
WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, utf8, sizeof(utf8), nullptr, nullptr);
if (!luid_match.p && id_props.deviceLUIDValid &&
std::memcmp(&desc.AdapterLuid, id_props.deviceLUID, sizeof(desc.AdapterLuid)) == 0) {
luid_match.p = candidate.p;
candidate.p = nullptr;
luid_name = utf8;
break;
}
if (!pci_match.p && props2.properties.vendorID != 0 && desc.VendorId == props2.properties.vendorID &&
desc.DeviceId == props2.properties.deviceID) {
pci_match.p = candidate.p;
candidate.p = nullptr;
pci_name = utf8;
}
}
candidate.reset();
}
}
}
if (luid_match.p && SUCCEEDED(create(luid_match.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) {
adapter_name = luid_name;
return true;
}
if (pci_match.p && SUCCEEDED(create(pci_match.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) {
adapter_name = pci_name;
return true;
}
if (SUCCEEDED(create(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) {
adapter_name = "default adapter";
return true;
}
return false;
}
bool integer_format(DXGI_FORMAT format) {
switch (format) {
case DXGI_FORMAT_R32G32B32A32_UINT: case DXGI_FORMAT_R32G32B32A32_SINT: case DXGI_FORMAT_R32G32B32_UINT:
case DXGI_FORMAT_R32G32B32_SINT: case DXGI_FORMAT_R16G16B16A16_UINT: case DXGI_FORMAT_R16G16B16A16_SINT:
case DXGI_FORMAT_R32G32_UINT: case DXGI_FORMAT_R32G32_SINT: case DXGI_FORMAT_R10G10B10A2_UINT:
case DXGI_FORMAT_R8G8B8A8_UINT: case DXGI_FORMAT_R8G8B8A8_SINT: case DXGI_FORMAT_R16G16_UINT:
case DXGI_FORMAT_R16G16_SINT: case DXGI_FORMAT_R32_UINT: case DXGI_FORMAT_R32_SINT: case DXGI_FORMAT_R8G8_UINT:
case DXGI_FORMAT_R8G8_SINT: case DXGI_FORMAT_R16_UINT: case DXGI_FORMAT_R16_SINT: case DXGI_FORMAT_R8_UINT:
case DXGI_FORMAT_R8_SINT: return true;
default: return false;
}
}
// FORMAT_SUPPORT bits that D3D12 defines per format rather than per GPU, compared with the host PC runtime on the
// same adapter for every format both support. Two kinds of check:
// - against the host: no 1D, 3D, depth-stencil, MIP or atomics the host lacks; storage-only (TYPELESS) formats
// stay storage-only; depth formats are neither shader resources nor UAVs; MIP wherever the host has it;
// - rules and consistency within the layer: no sample, gather or resolve of integer formats; atomics only on
// R32_UINT/R32_SINT, and R32_UINT typed UAVs carry them; gather with sample; typed load with typed store;
// MULTISAMPLE_RENDERTARGET only with quality levels, and on depth formats whenever there are quality levels.
// Hardware-dependent bits (render targets, filtering, typed UAV formats, which float formats resolve) are left to
// the Vulkan driver. Formats the layer reports nothing for (unsupported formats, D24 without Vulkan D24) are listed,
// not checked.
int compare_format_support(ID3D12Device* ours, const rt::d3d12::VulkanDeviceInfo& info) {
Ref<ID3D12Device> host;
std::string adapter;
if (!create_host_device_for_physical(info.instance, info.physical, info.get_instance_proc, host, adapter)) {
std::printf("FORMAT_SUPPORT comparison: no host D3D12 device, skipped\n");
return 0;
}
if (adapter == "default adapter") { // neither LUID nor PCI IDs matched: another GPU's answers
std::printf("FORMAT_SUPPORT comparison: no host device on the same adapter, skipped\n");
return 0;
}
constexpr UINT dims = D3D12_FORMAT_SUPPORT1_TEXTURE1D | D3D12_FORMAT_SUPPORT1_TEXTURE2D |
D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_TEXTURECUBE;
constexpr UINT storage_only = dims | D3D12_FORMAT_SUPPORT1_MIP | D3D12_FORMAT_SUPPORT1_CAST_WITHIN_BIT_LAYOUT;
constexpr UINT format_rules = D3D12_FORMAT_SUPPORT1_TEXTURE1D | D3D12_FORMAT_SUPPORT1_TEXTURE3D |
D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL | D3D12_FORMAT_SUPPORT1_MIP;
constexpr UINT shader_reads = D3D12_FORMAT_SUPPORT1_SHADER_LOAD | D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE |
D3D12_FORMAT_SUPPORT1_SHADER_GATHER;
constexpr UINT filtered = D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE | D3D12_FORMAT_SUPPORT1_SHADER_GATHER |
D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RESOLVE;
constexpr UINT atomics = D3D12_FORMAT_SUPPORT2_UAV_ATOMIC_ADD | D3D12_FORMAT_SUPPORT2_UAV_ATOMIC_BITWISE_OPS |
D3D12_FORMAT_SUPPORT2_UAV_ATOMIC_COMPARE_STORE_OR_COMPARE_EXCHANGE |
D3D12_FORMAT_SUPPORT2_UAV_ATOMIC_SIGNED_MIN_OR_MAX |
D3D12_FORMAT_SUPPORT2_UAV_ATOMIC_UNSIGNED_MIN_OR_MAX;
constexpr UINT any_atomic = atomics | D3D12_FORMAT_SUPPORT2_UAV_ATOMIC_EXCHANGE;
int mismatches = 0;
unsigned compared = 0;
std::string unreported;
const auto fail = [&](int format, const char* what, UINT o1, UINT o2, UINT h1, UINT h2) {
if (mismatches++ < 32)
std::fprintf(stderr, "FORMAT_SUPPORT format %d: %s (layer %08x/%08x, host %08x/%08x)\n", format, what,
o1, o2, h1, h2);
};
const auto multisampled = [&](DXGI_FORMAT format) {
for (UINT samples : {2u, 4u, 8u, 16u}) {
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS levels{format, samples};
if (SUCCEEDED(ours->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &levels,
sizeof(levels))) &&
levels.NumQualityLevels)
return true;
}
return false;
};
for (int f = 1; f <= DXGI_FORMAT_A4B4G4R4_UNORM; ++f) {
if (f > DXGI_FORMAT_V408 && f < DXGI_FORMAT_SAMPLER_FEEDBACK_MIN_MIP_OPAQUE) continue;
D3D12_FEATURE_DATA_FORMAT_SUPPORT o{DXGI_FORMAT(f)}, h{DXGI_FORMAT(f)};
if (FAILED(ours->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &o, sizeof(o))) ||
FAILED(host->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &h, sizeof(h))))
continue;
const UINT o1 = UINT(o.Support1), o2 = UINT(o.Support2), h1 = UINT(h.Support1), h2 = UINT(h.Support2);
if (!h1) continue;
if (!o1) { // the layer answers S_OK with no uses: nothing to compare, but say which
unreported += (unreported.empty() ? "" : ", ") + std::to_string(f);
continue;
}
++compared;
if ((f == DXGI_FORMAT_R8G8B8A8_UNORM || f == DXGI_FORMAT_B8G8R8A8_UNORM ||
f == DXGI_FORMAT_B8G8R8X8_UNORM || f == DXGI_FORMAT_R8G8B8A8_UNORM_SRGB ||
f == DXGI_FORMAT_B8G8R8A8_UNORM_SRGB || f == DXGI_FORMAT_B8G8R8X8_UNORM_SRGB) &&
((o1 ^ h1) & D3D12_FORMAT_SUPPORT1_BUFFER))
fail(f, "buffer support differs from host", o1, o2, h1, h2);
if (o1 & format_rules & ~h1) fail(f, "1D, 3D, depth-stencil or MIP the host doesn't have", o1, o2, h1, h2);
if (o2 & any_atomic & ~h2) fail(f, "UAV atomics the host doesn't have", o1, o2, h1, h2);
if (integer_format(DXGI_FORMAT(f)) && (o1 & filtered))
fail(f, "sample, gather or resolve of an integer format", o1, o2, h1, h2);
if ((o2 & any_atomic) && f != DXGI_FORMAT_R32_UINT && f != DXGI_FORMAT_R32_SINT)
fail(f, "UAV atomics on a format other than R32_UINT/R32_SINT", o1, o2, h1, h2);
// A host answer of nothing but dimensions, mips and casts is a TYPELESS (storage-only) format.
if ((h1 & dims) && !(h1 & ~storage_only) && !(h2 & ~UINT(D3D12_FORMAT_SUPPORT2_TILED)) &&
((o1 & ~storage_only) || o2))
fail(f, "a storage-only (TYPELESS) format reported with uses of its own", o1, o2, h1, h2);
if ((h1 & D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL) &&
((o1 & (shader_reads | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) & ~h1) || (o2 & ~h2)))
fail(f, "a depth format reported as a shader resource or UAV", o1, o2, h1, h2);
if ((h1 & D3D12_FORMAT_SUPPORT1_MIP) && (o1 & (dims | D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL)) &&
!(o1 & D3D12_FORMAT_SUPPORT1_MIP))
fail(f, "no MIP where the host has mips", o1, o2, h1, h2);
if (bool(o1 & D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE) != bool(o1 & D3D12_FORMAT_SUPPORT1_SHADER_GATHER))
fail(f, "gather and sample disagree", o1, o2, h1, h2);
if (bool(o2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE) != bool(o2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD))
fail(f, "typed UAV load and store disagree", o1, o2, h1, h2);
if (f == DXGI_FORMAT_R32_UINT && (o1 & D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW) &&
(o2 & atomics) != atomics)
fail(f, "R32_UINT typed UAV without its atomics", o1, o2, h1, h2);
const bool ms_target = (o1 & D3D12_FORMAT_SUPPORT1_MULTISAMPLE_RENDERTARGET) != 0;
if (ms_target || (o1 & D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL)) {
const bool levels = multisampled(DXGI_FORMAT(f));
if (ms_target && !levels) fail(f, "MULTISAMPLE_RENDERTARGET without quality levels", o1, o2, h1, h2);
if ((o1 & D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL) && levels && !ms_target)
fail(f, "a multisampled depth format without MULTISAMPLE_RENDERTARGET", o1, o2, h1, h2);
}
}
if (!unreported.empty())
std::printf("FORMAT_SUPPORT: the layer reports no uses for host formats %s (not compared)\n",
unreported.c_str());
// A layer that answers nothing passes every check above; a working one shares far more than this with any host.
constexpr unsigned minimum_compared = 40;
if (compared < minimum_compared) {
std::fprintf(stderr, "FORMAT_SUPPORT: only %u formats compared (expected at least %u)\n", compared,
minimum_compared);
++mismatches;
}
std::printf("FORMAT_SUPPORT against the host on %s: %u formats compared, %d mismatches\n", adapter.c_str(),
compared, mismatches);
return mismatches;
}
// Clears a 1x1 render target of `format` on `device` (the layer's PC-ABI device or the host's) to `colour` and reads
// its texel back into `texel` (8 bytes, zero beyond the format's size). False if any step fails.
bool clear_and_read_texel(ID3D12Device* device, DXGI_FORMAT format, const float colour[4],
std::array<std::uint8_t, 8>& texel) {
const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}, readback_heap{D3D12_HEAP_TYPE_READBACK};
const D3D12_RESOURCE_DESC desc = texture_desc(1, 1, format, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
const D3D12_RESOURCE_DESC buffer = buffer_desc(256);
const D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1};
D3D12_COMMAND_QUEUE_DESC queue_desc{};
Ref<ID3D12Resource> target, readback;
Ref<ID3D12DescriptorHeap> heap;
Ref<ID3D12CommandQueue> queue;
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
Ref<ID3D12Fence> fence;
if (FAILED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc,
D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, IID_PPV_ARGS(&target.p))) ||
FAILED(device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &buffer,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&readback.p))) ||
FAILED(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&heap.p))) ||
FAILED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))) ||
FAILED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))) ||
FAILED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
IID_PPV_ARGS(&list.p))) ||
FAILED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))))
return false;
const D3D12_CPU_DESCRIPTOR_HANDLE rtv = heap->GetCPUDescriptorHandleForHeapStart();
device->CreateRenderTargetView(target.p, nullptr, rtv);
list->ClearRenderTargetView(rtv, colour, 0, nullptr);
const D3D12_RESOURCE_BARRIER to_copy =
transition(target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &to_copy);
const D3D12_PLACED_SUBRESOURCE_FOOTPRINT placed{0, {format, 1, 1, 1, 256}};
const auto src = subresource(target.p, 0);
const auto out = footprint(readback.p, placed);
list->CopyTextureRegion(&out, 0, 0, 0, &src, nullptr);
if (list->Close() != S_OK) return false;
ID3D12CommandList* lists[] = {list.p};
queue->ExecuteCommandLists(1, lists);
if (FAILED(queue->Signal(fence.p, 1)) || FAILED(fence->SetEventOnCompletion(1, nullptr))) return false;
void* mapped = nullptr;
if (FAILED(readback->Map(0, nullptr, &mapped)) || !mapped) return false;
std::memcpy(texel.data(), mapped, texel.size());
readback->Unmap(0, nullptr);
return true;
}
// Item 1 of the FORMAT_SUPPORT finding in #1380: formats the host creates that vulkan_format() didn't map. Each one
// is created as a texture (as on the host, when there is one), reported as a 2D texture, and a 4x4 upload copied in
// and back out reads back bit for bit. Every one both the layer and the host render to is cleared to the same colour
// on both, and the bytes must match: a mapping with the right size but the wrong numeric type (SNORM as UNORM, SINT as
// UINT) clears differently. A4B4G4R4_UNORM is also cleared to pure R, G, B and A where the layer reports a render
// target: the host keeps red in bits 12-15 and alpha in bits 0-3 (measured on AMD and WARP), Vulkan's R4G4B4A4_PACK16.
int new_format_round_trips(ID3D12Device* device, const rt::d3d12::VulkanDeviceInfo& info, bool r4g4b4a4) {
struct NewFormat {
DXGI_FORMAT format;
UINT bytes;
};
constexpr NewFormat formats[] = {
{DXGI_FORMAT_R16_UINT, 2}, {DXGI_FORMAT_R16_SINT, 2}, {DXGI_FORMAT_R16_SNORM, 2},
{DXGI_FORMAT_R8_UINT, 1}, {DXGI_FORMAT_R8_SINT, 1}, {DXGI_FORMAT_R8_SNORM, 1},
{DXGI_FORMAT_R8_TYPELESS, 1}, {DXGI_FORMAT_R16G16_UNORM, 4}, {DXGI_FORMAT_R16G16_SNORM, 4},
{DXGI_FORMAT_R16G16B16A16_SNORM, 8}, {DXGI_FORMAT_A4B4G4R4_UNORM, 2},
};
constexpr UINT size = 4, pitch = 256, slot = 1024;
int failures = 0;
unsigned host_compared = 0, host_lacking = 0;
const auto fail = [&](DXGI_FORMAT format, const char* what) {
if (failures++ < 16) std::fprintf(stderr, "new format %d: %s\n", int(format), what);
};
// The host on the Vulkan device's own adapter: on a two-GPU PC the default adapter is another GPU's driver.
Ref<ID3D12Device> host;
std::string adapter;
if (create_host_device_for_physical(info.instance, info.physical, info.get_instance_proc, host, adapter) &&
adapter == "default adapter")
host.reset();
if (!host.p) std::printf("new formats: no host device on the same adapter, host comparisons skipped\n");
// Vulkan doesn't require R4G4B4A4_UNORM_PACK16 (A4B4G4R4): where the driver can't sample it, it is skipped.
// The skip follows the Vulkan driver, not the layer's answers, so a dropped mapping still fails.
const auto skipped = [&](DXGI_FORMAT format) { return format == DXGI_FORMAT_A4B4G4R4_UNORM && !r4g4b4a4; };
if (!r4g4b4a4) std::printf("new formats: the Vulkan driver can't sample R4G4B4A4_UNORM_PACK16, A4B4G4R4 skipped\n");
const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}, upload_heap{D3D12_HEAP_TYPE_UPLOAD},
readback_heap{D3D12_HEAP_TYPE_READBACK};
const UINT64 buffer_bytes = UINT64(slot) * std::size(formats);
const D3D12_RESOURCE_DESC buffer = buffer_desc(buffer_bytes);
Ref<ID3D12Resource> upload, readback;
EXPECT(SUCCEEDED(device->CreateCommittedResource(&upload_heap, D3D12_HEAP_FLAG_NONE, &buffer,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
IID_PPV_ARGS(&upload.p))));
EXPECT(SUCCEEDED(device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &buffer,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&readback.p))));
if (!upload.p || !readback.p) return 1;
std::uint8_t *up = nullptr, *down = nullptr;
EXPECT(SUCCEEDED(upload->Map(0, nullptr, reinterpret_cast<void**>(&up))));
EXPECT(SUCCEEDED(readback->Map(0, nullptr, reinterpret_cast<void**>(&down))));
if (!up || !down) return 1;
std::memset(down, 0xcd, size_t(buffer_bytes));
for (UINT64 i = 0; i < buffer_bytes; ++i) up[i] = std::uint8_t(i * 37 + (i >> 8) * 11 + 5);
D3D12_COMMAND_QUEUE_DESC queue_desc{};
Ref<ID3D12CommandQueue> queue;
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
Ref<ID3D12Fence> fence;
EXPECT(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))));
EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))));
EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
IID_PPV_ARGS(&list.p))));
EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))));
if (!queue.p || !list.p || !fence.p) return 1;
UINT64 fence_value = 0;
const auto submit = [&] {
const bool closed = list->Close() == S_OK;
ID3D12CommandList* lists[] = {list.p};
queue->ExecuteCommandLists(1, lists);
const bool replayed = rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success;
++fence_value;
const bool waited = SUCCEEDED(queue->Signal(fence.p, fence_value)) &&
SUCCEEDED(fence->SetEventOnCompletion(fence_value, nullptr));
if (!closed || !replayed || !waited) fail(DXGI_FORMAT_UNKNOWN, "a command list didn't close, replay or finish");
};
std::array<Ref<ID3D12Resource>, std::size(formats)> textures;
for (size_t i = 0; i < std::size(formats); ++i) {
const DXGI_FORMAT format = formats[i].format;
if (skipped(format)) continue;
D3D12_FEATURE_DATA_FORMAT_SUPPORT support{format};
if (FAILED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support))) ||
!(support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D))
fail(format, "FORMAT_SUPPORT reports no 2D texture");
const D3D12_RESOURCE_DESC desc = texture_desc(size, size, format, D3D12_RESOURCE_FLAG_NONE);
if (FAILED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&textures[i].p)))) {
fail(format, "CreateCommittedResource failed");
continue;
}
if (host.p) {
// A host runtime older than the format has nothing to match: A4B4G4R4_UNORM is not in Windows 10's D3D12,
// which answers the query with an error (E_FAIL on Windows 10 build 19045, E_INVALIDARG on another 19041
// host) or reports no 2D texture, and refuses the texture. Compare only where the host itself says it has
// the format, so a host that reports it and still refuses the texture fails as before. The answer is
// printed, so an unexpected code stays visible.
D3D12_FEATURE_DATA_FORMAT_SUPPORT host_support{format};
const HRESULT query = host->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &host_support,
sizeof(host_support));
const bool host_has_format = SUCCEEDED(query) && (host_support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D);
if (!host_has_format) {
++host_lacking;
std::printf("new format %d: the host runtime has no such 2D texture (FORMAT_SUPPORT answered %08lx, "
"e.g. Windows 10), host comparison skipped\n",
int(format), static_cast<unsigned long>(query));
} else {
++host_compared;
Ref<ID3D12Resource> native;
if (FAILED(host->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&native.p))))
fail(format, "the host refuses a texture the layer creates");
}
}
D3D12_PLACED_SUBRESOURCE_FOOTPRINT placed{};
UINT rows = 0;
UINT64 row_bytes = 0;
device->GetCopyableFootprints(&desc, 0, 1, UINT64(slot) * i, &placed, &rows, &row_bytes, nullptr);
if (placed.Footprint.Format != format || placed.Footprint.RowPitch != pitch || rows != size ||
row_bytes != UINT64(size) * formats[i].bytes) {
fail(format, "unexpected copyable footprint");
textures[i].reset();
continue;
}
const auto dst = subresource(textures[i].p, 0);
const auto in = footprint(upload.p, placed);
list->CopyTextureRegion(&dst, 0, 0, 0, &in, nullptr);
const D3D12_RESOURCE_BARRIER readable =
transition(textures[i].p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &readable);
const auto out = footprint(readback.p, placed);
list->CopyTextureRegion(&out, 0, 0, 0, &dst, nullptr);
}
submit();
unsigned round_trips = 0;
for (size_t i = 0; i < std::size(formats); ++i) {
if (!textures[i].p) continue;
const size_t base = size_t(slot) * i;
bool same = true;
for (UINT y = 0; y < size && same; ++y)
same = std::memcmp(down + base + y * pitch, up + base + y * pitch, size_t(size) * formats[i].bytes) == 0;
if (same)
++round_trips;
else
fail(formats[i].format, "a texel read back differs from the one uploaded");
}
// The numeric type, against the host. Red is -3 because single-channel formats see only red: SNORM clamps it to
// -1, SINT keeps -3, UNORM and UINT give 0. -0.4, 0.25 and 7 cover negatives, fractions and clamping in G, B, A;
// none is an exact half step at 4, 8 or 16 bits (-0.5 is one for 16-bit SNORM, where drivers round differently).
unsigned clears_matched = 0;
if (host.p) {
constexpr float colour[4] = {-3.0f, -0.4f, 0.25f, 7.0f};
for (const NewFormat& f : formats) {
if (skipped(f.format)) continue;
D3D12_FEATURE_DATA_FORMAT_SUPPORT ours_support{f.format}, host_support{f.format};
const bool both_render =
SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &ours_support,
sizeof(ours_support))) &&
(ours_support.Support1 & D3D12_FORMAT_SUPPORT1_RENDER_TARGET) &&
SUCCEEDED(host->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &host_support,
sizeof(host_support))) &&
(host_support.Support1 & D3D12_FORMAT_SUPPORT1_RENDER_TARGET);
if (!both_render) continue;
std::array<std::uint8_t, 8> ours{}, theirs{};
if (!clear_and_read_texel(device, f.format, colour, ours) ||
!clear_and_read_texel(host.p, f.format, colour, theirs)) {
fail(f.format, "a render-target clear failed");
continue;
}
if (std::memcmp(ours.data(), theirs.data(), f.bytes) == 0) {
++clears_matched;
} else if (failures++ < 16) {
std::fprintf(stderr, "new format %d: clear to (-3, -0.4, 0.25, 7) differs from the host:", int(f.format));
for (UINT b = 0; b < f.bytes; ++b) std::fprintf(stderr, " %02x/%02x", ours[b], theirs[b]);
std::fprintf(stderr, " (layer/host)\n");
}
}
}
// A4B4G4R4 cleared to pure red, green, blue and alpha in turn.
D3D12_FEATURE_DATA_FORMAT_SUPPORT a4_support{DXGI_FORMAT_A4B4G4R4_UNORM};
const bool a4_target = !skipped(DXGI_FORMAT_A4B4G4R4_UNORM) &&
SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &a4_support, sizeof(a4_support))) &&
(a4_support.Support1 & D3D12_FORMAT_SUPPORT1_RENDER_TARGET);
unsigned a4_cleared = 0;
if (a4_target) {
const D3D12_RESOURCE_DESC desc =
texture_desc(1, 1, DXGI_FORMAT_A4B4G4R4_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
Ref<ID3D12Resource> target;
Ref<ID3D12DescriptorHeap> heap;
const D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1};
EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc,
D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr,
IID_PPV_ARGS(&target.p))));
EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&heap.p))));
if (target.p && heap.p) {
const D3D12_CPU_DESCRIPTOR_HANDLE rtv = heap->GetCPUDescriptorHandleForHeapStart();
device->CreateRenderTargetView(target.p, nullptr, rtv);
constexpr float colours[4][4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
constexpr std::uint16_t words[4] = {0xf000, 0x0f00, 0x00f0, 0x000f};
const D3D12_PLACED_SUBRESOURCE_FOOTPRINT texel{0, {DXGI_FORMAT_A4B4G4R4_UNORM, 1, 1, 1, pitch}};
for (int c = 0; c < 4; ++c) {
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
list->ClearRenderTargetView(rtv, colours[c], 0, nullptr);
const D3D12_RESOURCE_BARRIER to_copy =
transition(target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &to_copy);
const auto src = subresource(target.p, 0);
const auto out = footprint(readback.p, texel);
list->CopyTextureRegion(&out, 0, 0, 0, &src, nullptr);
const D3D12_RESOURCE_BARRIER back =
transition(target.p, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET);
list->ResourceBarrier(1, &back);
submit();
std::uint16_t got = 0;
std::memcpy(&got, down, 2);
if (got == words[c]) {
++a4_cleared;
} else if (failures++ < 16) {
std::fprintf(stderr, "A4B4G4R4 clear to channel %d: got %04x, want %04x\n", c, got, words[c]);
}
}
}
}
upload->Unmap(0, nullptr);
readback->Unmap(0, nullptr);
char compared[64] = "";
if (host.p) std::snprintf(compared, sizeof(compared), " (host compared on %u, host lacks %u)", host_compared,
host_lacking);
std::printf("new formats: %u of %zu created and copied both ways bit for bit%s, %u render-target clears "
"matched the host, A4B4G4R4 render target %s, %d failures\n",
round_trips, std::size(formats), compared, clears_matched,
a4_target ? (a4_cleared == 4 ? "cleared to R, G, B and A" : "cleared wrong") : "not reported, skipped",
failures);
return failures;
}
// D3D-86: validate CheckFeatureSupport(D3D12_OPTIONS2..21 except 6/8/10/12/17), check size/null refusals and local
// invariants, and verify conservative compatibility against the host PC D3D12 runtime on the same adapter.
int compare_feature_options(ID3D12Device* ours, const rt::d3d12::VulkanDeviceInfo& info) {
int mismatches = 0;
const auto fail = [&](const char* what) {
if (mismatches++ < 32) std::fprintf(stderr, "CheckFeatureSupport options: %s\n", what);
};
const auto query_ours_raw = [&](const char* name, D3D12_FEATURE feature, const void* init, void* out,
size_t sz) -> bool {
if (ours->CheckFeatureSupport(feature, nullptr, UINT(sz)) != E_INVALIDARG) {
std::fprintf(stderr, "%s: expected E_INVALIDARG for nullptr\n", name);
++mismatches;
return false;
}
std::vector<std::uint8_t> scratch(sz + 1, 0xAA);
if (init) std::memcpy(scratch.data(), init, sz);
for (UINT bad_size : {0u, UINT(sz - 1), UINT(sz + 1)}) {
if (ours->CheckFeatureSupport(feature, scratch.data(), bad_size) != E_INVALIDARG) {
std::fprintf(stderr, "%s: expected E_INVALIDARG for size %u\n", name, bad_size);
++mismatches;
return false;
}
}
if (init)
std::memcpy(out, init, sz);
else
std::memset(out, 0xAA, sz);
if (FAILED(ours->CheckFeatureSupport(feature, out, UINT(sz)))) {
std::fprintf(stderr, "%s: CheckFeatureSupport failed on Vulkan device\n", name);
++mismatches;
return false;
}
return true;
};
const auto query_ours = [&](auto tag_feature, auto& out) -> bool {
using T = std::remove_reference_t<decltype(out)>;
return query_ours_raw(tag_feature.first, tag_feature.second, nullptr, &out, sizeof(T));
};
const auto query_ours_input = [&](auto tag_feature, const auto& init, auto& out) -> bool {
using T = std::remove_reference_t<decltype(out)>;
return query_ours_raw(tag_feature.first, tag_feature.second, &init, &out, sizeof(T));
};
D3D12_FEATURE_DATA_D3D12_OPTIONS2 o2{};
D3D12_FEATURE_DATA_D3D12_OPTIONS3 o3{};
D3D12_FEATURE_DATA_D3D12_OPTIONS4 o4{};
D3D12_FEATURE_DATA_D3D12_OPTIONS5 o5{};
D3D12_FEATURE_DATA_D3D12_OPTIONS7 o7{};
D3D12_FEATURE_DATA_D3D12_OPTIONS9 o9{};
D3D12_FEATURE_DATA_D3D12_OPTIONS11 o11{};
D3D12_FEATURE_DATA_D3D12_OPTIONS13 o13{};
D3D12_FEATURE_DATA_D3D12_OPTIONS14 o14{};
D3D12_FEATURE_DATA_D3D12_OPTIONS15 o15{};
D3D12_FEATURE_DATA_D3D12_OPTIONS16 o16{};
D3D12_FEATURE_DATA_D3D12_OPTIONS18 o18{};
D3D12_FEATURE_DATA_D3D12_OPTIONS19 o19{};
rt::d3d12::compat::FeatureDataOptions20 o20{};
rt::d3d12::compat::FeatureDataOptions21 o21{};
query_ours(std::pair{"OPTIONS2", D3D12_FEATURE_D3D12_OPTIONS2}, o2);
query_ours(std::pair{"OPTIONS3", D3D12_FEATURE_D3D12_OPTIONS3}, o3);
query_ours(std::pair{"OPTIONS4", D3D12_FEATURE_D3D12_OPTIONS4}, o4);
query_ours(std::pair{"OPTIONS5", D3D12_FEATURE_D3D12_OPTIONS5}, o5);
query_ours(std::pair{"OPTIONS7", D3D12_FEATURE_D3D12_OPTIONS7}, o7);
query_ours(std::pair{"OPTIONS9", D3D12_FEATURE_D3D12_OPTIONS9}, o9);
query_ours(std::pair{"OPTIONS11", D3D12_FEATURE_D3D12_OPTIONS11}, o11);
query_ours(std::pair{"OPTIONS13", D3D12_FEATURE_D3D12_OPTIONS13}, o13);
query_ours(std::pair{"OPTIONS14", D3D12_FEATURE_D3D12_OPTIONS14}, o14);
query_ours(std::pair{"OPTIONS15", D3D12_FEATURE_D3D12_OPTIONS15}, o15);
query_ours(std::pair{"OPTIONS16", D3D12_FEATURE_D3D12_OPTIONS16}, o16);
query_ours(std::pair{"OPTIONS18", D3D12_FEATURE_D3D12_OPTIONS18}, o18);
query_ours(std::pair{"OPTIONS19", D3D12_FEATURE_D3D12_OPTIONS19}, o19);
query_ours(std::pair{"OPTIONS20", rt::d3d12::compat::kFeatureOptions20}, o20);
query_ours(std::pair{"OPTIONS21", rt::d3d12::compat::kFeatureOptions21}, o21);
D3D12_FEATURE_DATA_SHADER_CACHE shader_cache{};
D3D12_FEATURE_DATA_EXISTING_HEAPS existing_heaps{};
D3D12_FEATURE_DATA_CROSS_NODE cross_node{};
D3D12_FEATURE_DATA_DISPLAYABLE displayable{};
rt::d3d12::compat::FeatureSupportFlag predication{};
rt::d3d12::compat::FeatureSupportFlag driver_state{};
rt::d3d12::compat::FeatureSupportFlag bypass_hash{};
D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_SUPPORT prot_support{};
D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_TYPE_COUNT prot_count{};
D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_TYPES prot_types{};
D3D12_FEATURE_DATA_SERIALIZATION serialization{};
D3D12_FEATURE_DATA_COMMAND_QUEUE_PRIORITY queue_priority{};
query_ours(std::pair{"SHADER_CACHE", D3D12_FEATURE_SHADER_CACHE}, shader_cache);
query_ours(std::pair{"EXISTING_HEAPS", D3D12_FEATURE_EXISTING_HEAPS}, existing_heaps);
query_ours(std::pair{"CROSS_NODE", D3D12_FEATURE_CROSS_NODE}, cross_node);
query_ours(std::pair{"DISPLAYABLE", D3D12_FEATURE_DISPLAYABLE}, displayable);
query_ours(std::pair{"PREDICATION", rt::d3d12::compat::kFeaturePredication}, predication);
query_ours(std::pair{"APPLICATION_SPECIFIC_DRIVER_STATE",
rt::d3d12::compat::kFeatureApplicationSpecificDriverState},
driver_state);
query_ours(std::pair{"BYTECODE_BYPASS_HASH_SUPPORTED",
rt::d3d12::compat::kFeatureBytecodeBypassHashSupported},
bypass_hash);
query_ours_input(std::pair{"PROTECTED_RESOURCE_SESSION_SUPPORT", D3D12_FEATURE_PROTECTED_RESOURCE_SESSION_SUPPORT},
D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_SUPPORT{0, {}}, prot_support);
query_ours_input(
std::pair{"PROTECTED_RESOURCE_SESSION_TYPE_COUNT", D3D12_FEATURE_PROTECTED_RESOURCE_SESSION_TYPE_COUNT},
D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_TYPE_COUNT{0, 0}, prot_count);
query_ours_input(std::pair{"PROTECTED_RESOURCE_SESSION_TYPES", D3D12_FEATURE_PROTECTED_RESOURCE_SESSION_TYPES},
D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_TYPES{0, 0, nullptr}, prot_types);
query_ours_input(std::pair{"SERIALIZATION", D3D12_FEATURE_SERIALIZATION}, D3D12_FEATURE_DATA_SERIALIZATION{0, {}},
serialization);
query_ours_input(std::pair{"COMMAND_QUEUE_PRIORITY", D3D12_FEATURE_COMMAND_QUEUE_PRIORITY},
D3D12_FEATURE_DATA_COMMAND_QUEUE_PRIORITY{D3D12_COMMAND_LIST_TYPE_DIRECT,
D3D12_COMMAND_QUEUE_PRIORITY_NORMAL, FALSE},
queue_priority);
std::uint32_t unknown_feature = 0;
if (ours->CheckFeatureSupport(static_cast<D3D12_FEATURE>(9999), &unknown_feature, sizeof(unknown_feature)) !=
E_INVALIDARG)
fail("unknown feature unexpectedly answered");
// Local invariants for what this layer actually implements.
// DepthBoundsTestSupported follows the device's depthBounds feature.
if (o2.ProgrammableSamplePositionsTier != D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER_NOT_SUPPORTED)
fail("OPTIONS2 unexpected support");
if (o3.CastingFullyTypedFormatSupported ||
o3.WriteBufferImmediateSupportFlags != D3D12_COMMAND_LIST_SUPPORT_FLAG_NONE ||
o3.ViewInstancingTier != D3D12_VIEW_INSTANCING_TIER_NOT_SUPPORTED || o3.BarycentricsSupported)
fail("OPTIONS3 unexpected support");
if (!o4.MSAA64KBAlignedTextureSupported ||
o4.SharedResourceCompatibilityTier != D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER_0 ||
o4.Native16BitShaderOpsSupported)
fail("OPTIONS4 mismatch");
if (o5.SRVOnlyTiledResourceTier3 || o5.RenderPassesTier != D3D12_RENDER_PASS_TIER_0 ||
o5.RaytracingTier != D3D12_RAYTRACING_TIER_NOT_SUPPORTED)
fail("OPTIONS5 unexpected support");
if (o7.MeshShaderTier != D3D12_MESH_SHADER_TIER_NOT_SUPPORTED ||
o7.SamplerFeedbackTier != D3D12_SAMPLER_FEEDBACK_TIER_NOT_SUPPORTED)
fail("OPTIONS7 unexpected support");
if (o9.MeshShaderPipelineStatsSupported || o9.MeshShaderSupportsFullRangeRenderTargetArrayIndex ||
o9.AtomicInt64OnTypedResourceSupported || o9.AtomicInt64OnGroupSharedSupported ||
o9.DerivativesInMeshAndAmplificationShadersSupported ||
o9.WaveMMATier != D3D12_WAVE_MMA_TIER_NOT_SUPPORTED)
fail("OPTIONS9 unexpected support");
if (o11.AtomicInt64OnDescriptorHeapResourceSupported) fail("OPTIONS11 unexpected support");
if (o13.UnrestrictedBufferTextureCopyPitchSupported || o13.UnrestrictedVertexElementAlignmentSupported ||
o13.InvertedViewportHeightFlipsYSupported || o13.InvertedViewportDepthFlipsZSupported ||
o13.TextureCopyBetweenDimensionsSupported || o13.AlphaBlendFactorSupported)
fail("OPTIONS13 unexpected support");
if (o14.AdvancedTextureOpsSupported || o14.WriteableMSAATexturesSupported)
fail("OPTIONS14 unexpected support");
// DEPTH_STENCIL2's per-face masks and OMSetFrontAndBackStencilRef (d3d12_state_sweep_test draws them).
if (!o14.IndependentFrontAndBackStencilRefMaskSupported)
fail("OPTIONS14.IndependentFrontAndBackStencilRefMaskSupported should be TRUE");
if (o15.TriangleFanSupported || o15.DynamicIndexBufferStripCutSupported) fail("OPTIONS15 unexpected support");
if (o16.DynamicDepthBiasSupported || o16.GPUUploadHeapSupported) fail("OPTIONS16 unexpected support");
if (o18.RenderPassesValid) fail("OPTIONS18 unexpected support");
if (o19.MismatchingOutputDimensionsSupported || (o19.SupportedSampleCountsWithNoOutputs & ~0x3Fu) != 0 ||
(o19.SupportedSampleCountsWithNoOutputs & 1u) == 0 || o19.PointSamplingAddressesNeverRoundUp ||
o19.RasterizerDesc2Supported || o19.NarrowQuadrilateralLinesSupported ||
o19.AnisoFilterWithPointMipSupported != (info.sampler_anisotropy ? TRUE : FALSE) ||
o19.MaxSamplerDescriptorHeapSize != D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE ||
o19.MaxSamplerDescriptorHeapSizeWithStaticSamplers != D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE ||
o19.MaxViewDescriptorHeapSize != D3D12_MAX_SHADER_VISIBLE_DESCRIPTOR_HEAP_SIZE_TIER_2 ||
o19.ComputeOnlyCustomHeapSupported)
fail("OPTIONS19 mismatch");
if (o20.ComputeOnlyWriteWatchSupported || o20.RecreateAtTier != rt::d3d12::compat::kRecreateAtTierNotSupported)
fail("OPTIONS20 unexpected support");
if (o21.WorkGraphsTier != rt::d3d12::compat::kWorkGraphsTierNotSupported ||
o21.ExecuteIndirectTier != rt::d3d12::compat::kExecuteIndirectTier1_0 ||
o21.SampleCmpGradientAndBiasSupported || o21.ExtendedCommandInfoSupported)
fail("OPTIONS21 unexpected support");
// Verify OPTIONS19.AnisoFilterWithPointMipSupported follows VulkanDeviceInfo::sampler_anisotropy.
{
rt::d3d12::VulkanDeviceInfo toggled_info = info;
toggled_info.sampler_anisotropy = !info.sampler_anisotropy;
Ref<ID3D12Device> toggled;
if (FAILED(rt::d3d12::create_vulkan_device(toggled_info, IID_PPV_ARGS(&toggled.p)))) {
fail("failed to create toggled sampler_anisotropy device");
} else {
D3D12_FEATURE_DATA_D3D12_OPTIONS19 toggled19{};
if (FAILED(toggled->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS19, &toggled19, sizeof(toggled19))) ||
toggled19.AnisoFilterWithPointMipSupported != (toggled_info.sampler_anisotropy ? TRUE : FALSE))
fail("OPTIONS19.AnisoFilterWithPointMipSupported did not follow sampler_anisotropy");
}
}
if (shader_cache.SupportFlags != D3D12_SHADER_CACHE_SUPPORT_NONE) fail("SHADER_CACHE unexpected support");
if (existing_heaps.Supported) fail("EXISTING_HEAPS unexpected support");
if (cross_node.SharingTier != D3D12_CROSS_NODE_SHARING_TIER_NOT_SUPPORTED || cross_node.AtomicShaderInstructions)
fail("CROSS_NODE unexpected support");
if (displayable.DisplayableTexture ||
displayable.SharedResourceCompatibilityTier != D3D12_SHARED_RESOURCE_COMPATIBILITY_TIER_0)
fail("DISPLAYABLE unexpected support");
if (predication.Supported != (info.conditional_rendering ? TRUE : FALSE))
fail("PREDICATION did not match conditional_rendering");
if (driver_state.Supported) fail("APPLICATION_SPECIFIC_DRIVER_STATE unexpected support");
if (bypass_hash.Supported) fail("BYTECODE_BYPASS_HASH_SUPPORTED unexpected support");
if (prot_support.Support != D3D12_PROTECTED_RESOURCE_SESSION_SUPPORT_FLAG_NONE || prot_count.Count != 0)
fail("PROTECTED_RESOURCE_SESSION unexpected support");
if (serialization.HeapSerializationTier != D3D12_HEAP_SERIALIZATION_TIER_0)
fail("SERIALIZATION unexpected tier");
// NodeIndex > 0, non-zero protected type count, and zeroed meta-command queries must return E_INVALIDARG.
{
D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_SUPPORT ps1{
1, D3D12_PROTECTED_RESOURCE_SESSION_SUPPORT_FLAG_SUPPORTED};
D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_TYPE_COUNT ptc1{1, 99};
GUID dummy_guid{};
D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_TYPES pt_n1{1, 0, nullptr};
D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_TYPES pt_c1{0, 1, &dummy_guid};
D3D12_FEATURE_DATA_SERIALIZATION ser1{1, D3D12_HEAP_SERIALIZATION_TIER_0};
D3D12_FEATURE_DATA_QUERY_META_COMMAND meta{};
if (ours->CheckFeatureSupport(D3D12_FEATURE_PROTECTED_RESOURCE_SESSION_SUPPORT, &ps1, sizeof(ps1)) !=
E_INVALIDARG ||
ps1.Support != D3D12_PROTECTED_RESOURCE_SESSION_SUPPORT_FLAG_NONE)
fail("PROTECTED_RESOURCE_SESSION_SUPPORT NodeIndex=1 not rejected");
if (ours->CheckFeatureSupport(D3D12_FEATURE_PROTECTED_RESOURCE_SESSION_TYPE_COUNT, &ptc1, sizeof(ptc1)) !=
E_INVALIDARG)
fail("PROTECTED_RESOURCE_SESSION_TYPE_COUNT NodeIndex=1 not rejected");
if (ours->CheckFeatureSupport(D3D12_FEATURE_PROTECTED_RESOURCE_SESSION_TYPES, &pt_n1, sizeof(pt_n1)) !=
E_INVALIDARG ||
ours->CheckFeatureSupport(D3D12_FEATURE_PROTECTED_RESOURCE_SESSION_TYPES, &pt_c1, sizeof(pt_c1)) !=
E_INVALIDARG)
fail("PROTECTED_RESOURCE_SESSION_TYPES invalid node/count not rejected");
if (ours->CheckFeatureSupport(D3D12_FEATURE_SERIALIZATION, &ser1, sizeof(ser1)) != E_INVALIDARG)
fail("SERIALIZATION NodeIndex=1 not rejected");
if (ours->CheckFeatureSupport(D3D12_FEATURE_QUERY_META_COMMAND, &meta, sizeof(meta)) != E_INVALIDARG ||
ours->CheckFeatureSupport(D3D12_FEATURE_QUERY_META_COMMAND, nullptr, sizeof(meta)) != E_INVALIDARG ||
ours->CheckFeatureSupport(D3D12_FEATURE_QUERY_META_COMMAND, &meta, sizeof(meta) - 1) != E_INVALIDARG)
fail("QUERY_META_COMMAND not rejected");
}
// COMMAND_QUEUE_PRIORITY matrix + invalid type/priority validation.
for (D3D12_COMMAND_LIST_TYPE type :
{D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_LIST_TYPE_BUNDLE, D3D12_COMMAND_LIST_TYPE_COMPUTE,
D3D12_COMMAND_LIST_TYPE_COPY, D3D12_COMMAND_LIST_TYPE_VIDEO_DECODE, D3D12_COMMAND_LIST_TYPE_VIDEO_PROCESS,
D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE}) {
for (UINT prio :
{UINT(D3D12_COMMAND_QUEUE_PRIORITY_NORMAL), UINT(D3D12_COMMAND_QUEUE_PRIORITY_HIGH),
UINT(D3D12_COMMAND_QUEUE_PRIORITY_GLOBAL_REALTIME)}) {
D3D12_FEATURE_DATA_COMMAND_QUEUE_PRIORITY q{type, prio, FALSE};
const bool expect_ok = (type == D3D12_COMMAND_LIST_TYPE_DIRECT ||
type == D3D12_COMMAND_LIST_TYPE_COMPUTE ||
type == D3D12_COMMAND_LIST_TYPE_COPY) &&
prio == D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
if (ours->CheckFeatureSupport(D3D12_FEATURE_COMMAND_QUEUE_PRIORITY, &q, sizeof(q)) != S_OK ||
q.PriorityForTypeIsSupported != (expect_ok ? TRUE : FALSE))
fail("COMMAND_QUEUE_PRIORITY answer mismatch");
}
}
{
D3D12_FEATURE_DATA_COMMAND_QUEUE_PRIORITY bad_type{static_cast<D3D12_COMMAND_LIST_TYPE>(99),
D3D12_COMMAND_QUEUE_PRIORITY_NORMAL, TRUE};
D3D12_FEATURE_DATA_COMMAND_QUEUE_PRIORITY bad_prio{D3D12_COMMAND_LIST_TYPE_DIRECT, 50u, TRUE};
if (ours->CheckFeatureSupport(D3D12_FEATURE_COMMAND_QUEUE_PRIORITY, &bad_type, sizeof(bad_type)) !=
E_INVALIDARG ||
bad_type.PriorityForTypeIsSupported != FALSE ||
ours->CheckFeatureSupport(D3D12_FEATURE_COMMAND_QUEUE_PRIORITY, &bad_prio, sizeof(bad_prio)) !=
E_INVALIDARG ||
bad_prio.PriorityForTypeIsSupported != FALSE)
fail("COMMAND_QUEUE_PRIORITY invalid type/priority not rejected");
}
// Verify D3D12_FEATURE_PREDICATION follows conditional_rendering when disabled on a device without SetPredication.
if (info.conditional_rendering) {
rt::d3d12::VulkanDeviceInfo no_pred_info = info;
no_pred_info.conditional_rendering = false;
Ref<ID3D12Device> no_pred;
if (FAILED(rt::d3d12::create_vulkan_device(no_pred_info, IID_PPV_ARGS(&no_pred.p)))) {
fail("failed to create conditional_rendering=false device");
} else {
rt::d3d12::compat::FeatureSupportFlag pred_off{TRUE};
if (FAILED(no_pred->CheckFeatureSupport(rt::d3d12::compat::kFeaturePredication, &pred_off,
sizeof(pred_off))) ||
pred_off.Supported != FALSE)
fail("PREDICATION did not become FALSE when conditional_rendering is off");
}
}
Ref<ID3D12Device> host;
std::string adapter_name;
if (!create_host_device_for_physical(info.instance, info.physical, info.get_instance_proc, host, adapter_name)) {
std::printf("CheckFeatureSupport options: no host D3D12 device to compare with, skipped\n");
return mismatches;
}
UINT host_compared = 0, host_skipped = 0;
const auto check_bool = [&](const char* feature, const char* field, BOOL a, BOOL b) {
if (!a || b) return;
if (mismatches++ < 32)
std::fprintf(stderr, "%s.%s: ours TRUE exceeds host FALSE\n", feature, field);
};
const auto check_tier = [&](const char* feature, const char* field, int a, int b) {
if (a <= b) return;
if (mismatches++ < 32)
std::fprintf(stderr, "%s.%s: ours tier %d exceeds host %d\n", feature, field, a, b);
};
const auto compare_host_input = [&](const char* name, D3D12_FEATURE feature, const auto& ours_val,
const auto& host_init, auto&& verify) {
using T = std::remove_cv_t<std::remove_reference_t<decltype(ours_val)>>;
T host_val = host_init;
if (FAILED(host->CheckFeatureSupport(feature, &host_val, sizeof(host_val)))) {
++host_skipped;
return;
}
++host_compared;
verify(name, ours_val, host_val);
};
const auto compare_host = [&](const char* name, D3D12_FEATURE feature, const auto& ours_val, auto&& verify) {
using T = std::remove_cv_t<std::remove_reference_t<decltype(ours_val)>>;
compare_host_input(name, feature, ours_val, T{}, verify);
};
compare_host("OPTIONS2", D3D12_FEATURE_D3D12_OPTIONS2, o2,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "DepthBoundsTestSupported", a.DepthBoundsTestSupported, b.DepthBoundsTestSupported);
check_tier(n, "ProgrammableSamplePositionsTier", int(a.ProgrammableSamplePositionsTier),
int(b.ProgrammableSamplePositionsTier));
});
compare_host("OPTIONS3", D3D12_FEATURE_D3D12_OPTIONS3, o3,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "CopyQueueTimestampQueriesSupported", a.CopyQueueTimestampQueriesSupported,
b.CopyQueueTimestampQueriesSupported);
check_bool(n, "CastingFullyTypedFormatSupported", a.CastingFullyTypedFormatSupported,
b.CastingFullyTypedFormatSupported);
if ((UINT(a.WriteBufferImmediateSupportFlags) & ~UINT(b.WriteBufferImmediateSupportFlags)) != 0 &&
mismatches++ < 32)
std::fprintf(stderr, "%s.WriteBufferImmediateSupportFlags: ours 0x%x not subset of host 0x%x\n",
n, UINT(a.WriteBufferImmediateSupportFlags),
UINT(b.WriteBufferImmediateSupportFlags));
check_tier(n, "ViewInstancingTier", int(a.ViewInstancingTier), int(b.ViewInstancingTier));
check_bool(n, "BarycentricsSupported", a.BarycentricsSupported, b.BarycentricsSupported);
});
compare_host("OPTIONS4", D3D12_FEATURE_D3D12_OPTIONS4, o4,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "MSAA64KBAlignedTextureSupported", a.MSAA64KBAlignedTextureSupported,
b.MSAA64KBAlignedTextureSupported);
check_tier(n, "SharedResourceCompatibilityTier", int(a.SharedResourceCompatibilityTier),
int(b.SharedResourceCompatibilityTier));
check_bool(n, "Native16BitShaderOpsSupported", a.Native16BitShaderOpsSupported,
b.Native16BitShaderOpsSupported);
});
compare_host("OPTIONS5", D3D12_FEATURE_D3D12_OPTIONS5, o5,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "SRVOnlyTiledResourceTier3", a.SRVOnlyTiledResourceTier3, b.SRVOnlyTiledResourceTier3);
check_tier(n, "RenderPassesTier", int(a.RenderPassesTier), int(b.RenderPassesTier));
check_tier(n, "RaytracingTier", int(a.RaytracingTier), int(b.RaytracingTier));
});
compare_host("OPTIONS7", D3D12_FEATURE_D3D12_OPTIONS7, o7,
[&](const char* n, const auto& a, const auto& b) {
check_tier(n, "MeshShaderTier", int(a.MeshShaderTier), int(b.MeshShaderTier));
check_tier(n, "SamplerFeedbackTier", int(a.SamplerFeedbackTier), int(b.SamplerFeedbackTier));
});
compare_host("OPTIONS9", D3D12_FEATURE_D3D12_OPTIONS9, o9,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "MeshShaderPipelineStatsSupported", a.MeshShaderPipelineStatsSupported,
b.MeshShaderPipelineStatsSupported);
check_bool(n, "MeshShaderSupportsFullRangeRenderTargetArrayIndex",
a.MeshShaderSupportsFullRangeRenderTargetArrayIndex,
b.MeshShaderSupportsFullRangeRenderTargetArrayIndex);
check_bool(n, "AtomicInt64OnTypedResourceSupported", a.AtomicInt64OnTypedResourceSupported,
b.AtomicInt64OnTypedResourceSupported);
check_bool(n, "AtomicInt64OnGroupSharedSupported", a.AtomicInt64OnGroupSharedSupported,
b.AtomicInt64OnGroupSharedSupported);
check_bool(n, "DerivativesInMeshAndAmplificationShadersSupported",
a.DerivativesInMeshAndAmplificationShadersSupported,
b.DerivativesInMeshAndAmplificationShadersSupported);
check_tier(n, "WaveMMATier", int(a.WaveMMATier), int(b.WaveMMATier));
});
compare_host("OPTIONS11", D3D12_FEATURE_D3D12_OPTIONS11, o11,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "AtomicInt64OnDescriptorHeapResourceSupported",
a.AtomicInt64OnDescriptorHeapResourceSupported,
b.AtomicInt64OnDescriptorHeapResourceSupported);
});
compare_host("OPTIONS13", D3D12_FEATURE_D3D12_OPTIONS13, o13,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "UnrestrictedBufferTextureCopyPitchSupported",
a.UnrestrictedBufferTextureCopyPitchSupported,
b.UnrestrictedBufferTextureCopyPitchSupported);
check_bool(n, "UnrestrictedVertexElementAlignmentSupported",
a.UnrestrictedVertexElementAlignmentSupported,
b.UnrestrictedVertexElementAlignmentSupported);
check_bool(n, "InvertedViewportHeightFlipsYSupported", a.InvertedViewportHeightFlipsYSupported,
b.InvertedViewportHeightFlipsYSupported);
check_bool(n, "InvertedViewportDepthFlipsZSupported", a.InvertedViewportDepthFlipsZSupported,
b.InvertedViewportDepthFlipsZSupported);
check_bool(n, "TextureCopyBetweenDimensionsSupported", a.TextureCopyBetweenDimensionsSupported,
b.TextureCopyBetweenDimensionsSupported);
check_bool(n, "AlphaBlendFactorSupported", a.AlphaBlendFactorSupported,
b.AlphaBlendFactorSupported);
});
compare_host("OPTIONS14", D3D12_FEATURE_D3D12_OPTIONS14, o14,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "AdvancedTextureOpsSupported", a.AdvancedTextureOpsSupported,
b.AdvancedTextureOpsSupported);
check_bool(n, "WriteableMSAATexturesSupported", a.WriteableMSAATexturesSupported,
b.WriteableMSAATexturesSupported);
check_bool(n, "IndependentFrontAndBackStencilRefMaskSupported",
a.IndependentFrontAndBackStencilRefMaskSupported,
b.IndependentFrontAndBackStencilRefMaskSupported);
});
compare_host("OPTIONS15", D3D12_FEATURE_D3D12_OPTIONS15, o15,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "TriangleFanSupported", a.TriangleFanSupported, b.TriangleFanSupported);
check_bool(n, "DynamicIndexBufferStripCutSupported", a.DynamicIndexBufferStripCutSupported,
b.DynamicIndexBufferStripCutSupported);
});
compare_host("OPTIONS16", D3D12_FEATURE_D3D12_OPTIONS16, o16,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "DynamicDepthBiasSupported", a.DynamicDepthBiasSupported,
b.DynamicDepthBiasSupported);
check_bool(n, "GPUUploadHeapSupported", a.GPUUploadHeapSupported, b.GPUUploadHeapSupported);
});
compare_host("OPTIONS18", D3D12_FEATURE_D3D12_OPTIONS18, o18,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "RenderPassesValid", a.RenderPassesValid, b.RenderPassesValid);
});
compare_host("OPTIONS19", D3D12_FEATURE_D3D12_OPTIONS19, o19,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "MismatchingOutputDimensionsSupported", a.MismatchingOutputDimensionsSupported,
b.MismatchingOutputDimensionsSupported);
if (b.SupportedSampleCountsWithNoOutputs != 0 &&
(a.SupportedSampleCountsWithNoOutputs & ~b.SupportedSampleCountsWithNoOutputs) != 0 &&
mismatches++ < 32)
std::fprintf(stderr, "%s.SupportedSampleCountsWithNoOutputs: ours 0x%x not subset of host 0x%x\n",
n, a.SupportedSampleCountsWithNoOutputs, b.SupportedSampleCountsWithNoOutputs);
check_bool(n, "PointSamplingAddressesNeverRoundUp", a.PointSamplingAddressesNeverRoundUp,
b.PointSamplingAddressesNeverRoundUp);
check_bool(n, "RasterizerDesc2Supported", a.RasterizerDesc2Supported, b.RasterizerDesc2Supported);
check_bool(n, "NarrowQuadrilateralLinesSupported", a.NarrowQuadrilateralLinesSupported,
b.NarrowQuadrilateralLinesSupported);
check_bool(n, "AnisoFilterWithPointMipSupported", a.AnisoFilterWithPointMipSupported,
b.AnisoFilterWithPointMipSupported);
if (b.MaxSamplerDescriptorHeapSize > 0 &&
a.MaxSamplerDescriptorHeapSize > b.MaxSamplerDescriptorHeapSize && mismatches++ < 32)
std::fprintf(stderr, "%s.MaxSamplerDescriptorHeapSize: ours %u > host %u\n", n,
a.MaxSamplerDescriptorHeapSize, b.MaxSamplerDescriptorHeapSize);
if (b.MaxSamplerDescriptorHeapSizeWithStaticSamplers > 0 &&
a.MaxSamplerDescriptorHeapSizeWithStaticSamplers >
b.MaxSamplerDescriptorHeapSizeWithStaticSamplers &&
mismatches++ < 32)
std::fprintf(stderr,
"%s.MaxSamplerDescriptorHeapSizeWithStaticSamplers: ours %u > host %u\n", n,
a.MaxSamplerDescriptorHeapSizeWithStaticSamplers,
b.MaxSamplerDescriptorHeapSizeWithStaticSamplers);
if (b.MaxViewDescriptorHeapSize > 0 &&
a.MaxViewDescriptorHeapSize > b.MaxViewDescriptorHeapSize && mismatches++ < 32)
std::fprintf(stderr, "%s.MaxViewDescriptorHeapSize: ours %u > host %u\n", n,
a.MaxViewDescriptorHeapSize, b.MaxViewDescriptorHeapSize);
check_bool(n, "ComputeOnlyCustomHeapSupported", a.ComputeOnlyCustomHeapSupported,
b.ComputeOnlyCustomHeapSupported);
});
compare_host("OPTIONS20", rt::d3d12::compat::kFeatureOptions20, o20,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "ComputeOnlyWriteWatchSupported", a.ComputeOnlyWriteWatchSupported,
b.ComputeOnlyWriteWatchSupported);
check_tier(n, "RecreateAtTier", int(a.RecreateAtTier), int(b.RecreateAtTier));
});
compare_host("OPTIONS21", rt::d3d12::compat::kFeatureOptions21, o21,
[&](const char* n, const auto& a, const auto& b) {
check_tier(n, "WorkGraphsTier", int(a.WorkGraphsTier), int(b.WorkGraphsTier));
check_tier(n, "ExecuteIndirectTier", int(a.ExecuteIndirectTier), int(b.ExecuteIndirectTier));
check_bool(n, "SampleCmpGradientAndBiasSupported", a.SampleCmpGradientAndBiasSupported,
b.SampleCmpGradientAndBiasSupported);
check_bool(n, "ExtendedCommandInfoSupported", a.ExtendedCommandInfoSupported,
b.ExtendedCommandInfoSupported);
});
compare_host("SHADER_CACHE", D3D12_FEATURE_SHADER_CACHE, shader_cache,
[&](const char* n, const auto& a, const auto& b) {
if ((UINT(a.SupportFlags) & ~UINT(b.SupportFlags)) != 0 && mismatches++ < 32)
std::fprintf(stderr, "%s.SupportFlags: ours 0x%x not subset of host 0x%x\n", n,
UINT(a.SupportFlags), UINT(b.SupportFlags));
});
compare_host("EXISTING_HEAPS", D3D12_FEATURE_EXISTING_HEAPS, existing_heaps,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "Supported", a.Supported, b.Supported);
});
compare_host("CROSS_NODE", D3D12_FEATURE_CROSS_NODE, cross_node,
[&](const char* n, const auto& a, const auto& b) {
check_tier(n, "SharingTier", int(a.SharingTier), int(b.SharingTier));
check_bool(n, "AtomicShaderInstructions", a.AtomicShaderInstructions, b.AtomicShaderInstructions);
});
compare_host("DISPLAYABLE", D3D12_FEATURE_DISPLAYABLE, displayable,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "DisplayableTexture", a.DisplayableTexture, b.DisplayableTexture);
check_tier(n, "SharedResourceCompatibilityTier", int(a.SharedResourceCompatibilityTier),
int(b.SharedResourceCompatibilityTier));
});
compare_host("PREDICATION", rt::d3d12::compat::kFeaturePredication, predication,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "Supported", a.Supported, b.Supported);
});
compare_host("APPLICATION_SPECIFIC_DRIVER_STATE", rt::d3d12::compat::kFeatureApplicationSpecificDriverState,
driver_state,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "Supported", a.Supported, b.Supported);
});
compare_host("BYTECODE_BYPASS_HASH_SUPPORTED", rt::d3d12::compat::kFeatureBytecodeBypassHashSupported,
bypass_hash,
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "Supported", a.Supported, b.Supported);
});
compare_host_input("PROTECTED_RESOURCE_SESSION_SUPPORT", D3D12_FEATURE_PROTECTED_RESOURCE_SESSION_SUPPORT,
prot_support, D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_SUPPORT{0, {}},
[&](const char* n, const auto& a, const auto& b) {
if ((UINT(a.Support) & ~UINT(b.Support)) != 0 && mismatches++ < 32)
std::fprintf(stderr, "%s.Support: ours 0x%x not subset of host 0x%x\n", n, UINT(a.Support),
UINT(b.Support));
});
compare_host_input("PROTECTED_RESOURCE_SESSION_TYPE_COUNT", D3D12_FEATURE_PROTECTED_RESOURCE_SESSION_TYPE_COUNT,
prot_count, D3D12_FEATURE_DATA_PROTECTED_RESOURCE_SESSION_TYPE_COUNT{0, 0},
[&](const char* n, const auto& a, const auto& b) {
if (a.Count > b.Count && mismatches++ < 32)
std::fprintf(stderr, "%s.Count: ours %u > host %u\n", n, a.Count, b.Count);
});
compare_host_input("SERIALIZATION", D3D12_FEATURE_SERIALIZATION, serialization, D3D12_FEATURE_DATA_SERIALIZATION{0, {}},
[&](const char* n, const auto& a, const auto& b) {
check_tier(n, "HeapSerializationTier", int(a.HeapSerializationTier),
int(b.HeapSerializationTier));
});
for (D3D12_COMMAND_LIST_TYPE type :
{D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_LIST_TYPE_BUNDLE, D3D12_COMMAND_LIST_TYPE_COMPUTE,
D3D12_COMMAND_LIST_TYPE_COPY, D3D12_COMMAND_LIST_TYPE_VIDEO_DECODE, D3D12_COMMAND_LIST_TYPE_VIDEO_PROCESS,
D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE}) {
for (UINT prio :
{UINT(D3D12_COMMAND_QUEUE_PRIORITY_NORMAL), UINT(D3D12_COMMAND_QUEUE_PRIORITY_HIGH),
UINT(D3D12_COMMAND_QUEUE_PRIORITY_GLOBAL_REALTIME)}) {
D3D12_FEATURE_DATA_COMMAND_QUEUE_PRIORITY ours_q{type, prio, FALSE};
if (SUCCEEDED(ours->CheckFeatureSupport(D3D12_FEATURE_COMMAND_QUEUE_PRIORITY, &ours_q, sizeof(ours_q)))) {
compare_host_input("COMMAND_QUEUE_PRIORITY", D3D12_FEATURE_COMMAND_QUEUE_PRIORITY, ours_q,
D3D12_FEATURE_DATA_COMMAND_QUEUE_PRIORITY{type, prio, FALSE},
[&](const char* n, const auto& a, const auto& b) {
check_bool(n, "PriorityForTypeIsSupported", a.PriorityForTypeIsSupported,
b.PriorityForTypeIsSupported);
});
}
}
}
std::printf("CheckFeatureSupport options (%s): %u feature queries compared (%u host-unsupported skipped), "
"MSAA64KB=%d, NoOutputsSamples=0x%x, MaxViewHeap=%u, %d mismatches\n",
adapter_name.empty() ? "host D3D12" : adapter_name.c_str(), host_compared, host_skipped,
int(o4.MSAA64KBAlignedTextureSupported), o19.SupportedSampleCountsWithNoOutputs,
o19.MaxViewDescriptorHeapSize, mismatches);
return mismatches;
}
// A software Vulkan device (llvmpipe on CI) and the host's WARP device answer about different machines, so the
// OPTIONS1 comparison is skipped there; on a GPU both runtimes see the same hardware.
bool software_vulkan_device(const vktest::Device& vk) {
const auto get_properties = reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
vk.instance_proc(vk.instance, "vkGetPhysicalDeviceProperties"));
if (!get_properties) return true;
VkPhysicalDeviceProperties properties{};
get_properties(vk.physical, &properties);
return properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;
}
// D3D12_OPTIONS1 (D3D-86): wave ops and the lane range come from the Vulkan device's subgroup properties, so the
// host PC runtime on the same GPU is the oracle. Returns the number of mismatches, -2 for a software Vulkan device,
// or -1 when the host has no D3D12 device.
int compare_options1(ID3D12Device* ours, const vktest::Device& vk) {
if (software_vulkan_device(vk)) return -2;
// The host device must be the same GPU (matched by LUID, then PCI ids), not the default adapter: on a PC with
// an AMD iGPU and an NVIDIA card the default is the NVIDIA, whose 32..32 lanes don't match the AMD's 32..64.
Ref<ID3D12Device> host;
std::string adapter_name;
if (!create_host_device_for_physical(vk.instance, vk.physical, vk.instance_proc, host, adapter_name)) return -1;
D3D12_FEATURE_DATA_D3D12_OPTIONS1 theirs{};
if (FAILED(host->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &theirs, sizeof(theirs)))) return -1;
D3D12_FEATURE_DATA_D3D12_OPTIONS1 mine{};
if (FAILED(ours->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &mine, sizeof(mine)))) return 1;
int mismatches = 0;
const auto same = [&](const char* field, UINT got, UINT want) {
if (got == want) return;
if (mismatches++ < 4)
std::fprintf(stderr, "D3D12_OPTIONS1 %s: ours %u, host %u\n", field, got, want);
};
same("WaveOps", mine.WaveOps, theirs.WaveOps);
same("WaveLaneCountMin", mine.WaveLaneCountMin, theirs.WaveLaneCountMin);
same("WaveLaneCountMax", mine.WaveLaneCountMax, theirs.WaveLaneCountMax);
same("ExpandedComputeResourceStates", mine.ExpandedComputeResourceStates,
theirs.ExpandedComputeResourceStates);
// TotalLaneCount is deliberately not compared: the host counts every lane of the device (6144 on the RTX 5070)
// and the layer answers one full wave (docs section 4.1).
std::printf("D3D12_OPTIONS1: lanes %u..%u, total %u (host total %u)\n", mine.WaveLaneCountMin,
mine.WaveLaneCountMax, mine.TotalLaneCount, theirs.TotalLaneCount);
return mismatches;
}
// D3D-49: the HRESULTs of invalid (and a few valid edge-case) calls, compared with the host PC runtime. Games and
// middleware branch on these, so a wrong success hides a bug and a wrong failure takes a path the console never
// takes. Each case is a stateless call on one device that releases whatever it created.
// Releases what a creation call wrote to `object`. Takes the address, not the pointer: the call must run first.
HRESULT released(HRESULT hr, void* const* object) {
if (*object) static_cast<IUnknown*>(*object)->Release();
return hr;
}
HRESULT committed(ID3D12Device* device, D3D12_HEAP_PROPERTIES heap, D3D12_RESOURCE_DESC desc,
D3D12_RESOURCE_STATES state = D3D12_RESOURCE_STATE_COMMON,
D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE, const D3D12_CLEAR_VALUE* clear = nullptr) {
void* out = nullptr;
const HRESULT hr =
device->CreateCommittedResource(&heap, flags, &desc, state, clear, __uuidof(ID3D12Resource), &out);
return released(hr, &out);
}
D3D12_RESOURCE_DESC rgba8(D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE) {
return texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, flags);
}
HRESULT with_heap_flags(ID3D12Device* device, const D3D12_RESOURCE_DESC& desc, UINT heap_flags) {
return committed(device, {D3D12_HEAP_TYPE_DEFAULT}, desc, D3D12_RESOURCE_STATE_COMMON,
D3D12_HEAP_FLAGS(heap_flags));
}
HRESULT descriptor_heap(ID3D12Device* device, D3D12_DESCRIPTOR_HEAP_DESC desc) {
void* out = nullptr;
return released(device->CreateDescriptorHeap(&desc, __uuidof(ID3D12DescriptorHeap), &out), &out);
}
HRESULT command_queue(ID3D12Device* device, D3D12_COMMAND_QUEUE_DESC desc) {
void* out = nullptr;
return released(device->CreateCommandQueue(&desc, __uuidof(ID3D12CommandQueue), &out), &out);
}
HRESULT query_heap(ID3D12Device* device, D3D12_QUERY_HEAP_TYPE type, UINT count, UINT node_mask = 0,
bool output = true) {
const D3D12_QUERY_HEAP_DESC desc{type, count, node_mask};
void* out = nullptr;
return released(device->CreateQueryHeap(&desc, __uuidof(ID3D12QueryHeap), output ? &out : nullptr), &out);
}
// A root signature blob from the host's own serializer (version 1.0): empty, or one root CBV at b0, or one table of
// unbounded SRVs from t0. Empty when d3d12.dll has no serializer, and both devices then get the same refusal.
std::vector<std::uint8_t> root_signature_blob(int parameters) {
using Serialize = HRESULT(WINAPI*)(const D3D12_ROOT_SIGNATURE_DESC*, D3D_ROOT_SIGNATURE_VERSION, ID3DBlob**,
ID3DBlob**);
static const auto serialize = reinterpret_cast<Serialize>(reinterpret_cast<void*>(GetProcAddress(
LoadLibraryExA("d3d12.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32), "D3D12SerializeRootSignature")));
D3D12_ROOT_PARAMETER parameter{};
parameter.ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
const D3D12_DESCRIPTOR_RANGE range{D3D12_DESCRIPTOR_RANGE_TYPE_SRV, UINT_MAX, 0, 0,
D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND};
if (parameters == 1) {
parameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
} else if (parameters == 2) {
parameter.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
parameter.DescriptorTable = {1, &range};
}
const D3D12_ROOT_SIGNATURE_DESC desc{parameters ? 1u : 0u, parameters ? ¶meter : nullptr, 0, nullptr,
D3D12_ROOT_SIGNATURE_FLAG_NONE};
ID3DBlob* blob = nullptr;
ID3DBlob* error = nullptr;
std::vector<std::uint8_t> bytes;
if (serialize && SUCCEEDED(serialize(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, &error)) && blob) {
const auto* first = static_cast<const std::uint8_t*>(blob->GetBufferPointer());
bytes.assign(first, first + blob->GetBufferSize());
}
if (blob) blob->Release();
if (error) error->Release();
return bytes;
}
// CreateRootSignature with `blob`, passed as `size` bytes (the blob's own size when 0; the vector is padded with
// zeros when `size` is larger).
HRESULT root_signature(ID3D12Device* device, std::vector<std::uint8_t> blob, SIZE_T size = 0, UINT node_mask = 0,
bool output = true, REFIID riid = __uuidof(ID3D12RootSignature)) {
if (!size) size = blob.size();
if (blob.size() < size) blob.resize(size);
void* out = nullptr;
return released(device->CreateRootSignature(node_mask, blob.empty() ? nullptr : blob.data(), size, riid,
output ? &out : nullptr),
&out);
}
// CreateCommandSignature without a root signature. Each argument is only its type; a CONSTANT one sets one value.
HRESULT command_signature(ID3D12Device* device, UINT stride, std::initializer_list<D3D12_INDIRECT_ARGUMENT_TYPE> types,
UINT node_mask = 0) {
std::vector<D3D12_INDIRECT_ARGUMENT_DESC> args;
for (const D3D12_INDIRECT_ARGUMENT_TYPE type : types) {
D3D12_INDIRECT_ARGUMENT_DESC arg{};
arg.Type = type;
if (type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT) arg.Constant.Num32BitValuesToSet = 1;
args.push_back(arg);
}
const D3D12_COMMAND_SIGNATURE_DESC desc{stride, UINT(args.size()), args.data(), node_mask};
void* out = nullptr;
return released(device->CreateCommandSignature(&desc, nullptr, __uuidof(ID3D12CommandSignature), &out), &out);
}
// CheckFeatureSupport(FORMAT_SUPPORT) for `value`: the HRESULT, or Support1 when it succeeds (Support2 holds what the
// GPU can do, such as TILED, which the layer doesn't offer, so it isn't compared).
HRESULT format_support1(ID3D12Device* device, UINT value) {
D3D12_FEATURE_DATA_FORMAT_SUPPORT support{static_cast<DXGI_FORMAT>(value)};
const HRESULT hr = device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support));
return SUCCEEDED(hr) ? static_cast<HRESULT>(support.Support1) : hr;
}
HRESULT fence(ID3D12Device* device, D3D12_FENCE_FLAGS flags, REFIID riid = __uuidof(ID3D12Fence)) {
void* out = nullptr;
return released(device->CreateFence(0, flags, riid, &out), &out);
}
HRESULT command_allocator(ID3D12Device* device, D3D12_COMMAND_LIST_TYPE type, bool output = true,
REFIID riid = __uuidof(ID3D12CommandAllocator)) {
void* out = nullptr;
return released(device->CreateCommandAllocator(type, riid, output ? &out : nullptr), &out);
}
// CreateCommandList with a fresh allocator of `allocator_type` (or none when it is -2).
HRESULT command_list(ID3D12Device* device, UINT node_mask, D3D12_COMMAND_LIST_TYPE type,
int allocator_type, bool output = true) {
void* allocator = nullptr;
if (allocator_type != -2)
device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE(allocator_type), __uuidof(ID3D12CommandAllocator),
&allocator);
void* out = nullptr;
const HRESULT hr = device->CreateCommandList(node_mask, type, static_cast<ID3D12CommandAllocator*>(allocator),
nullptr, __uuidof(ID3D12GraphicsCommandList), output ? &out : nullptr);
released(S_OK, &allocator);
return released(hr, &out);
}
HRESULT command_list1(ID3D12Device* device, UINT node_mask, D3D12_COMMAND_LIST_TYPE type, bool output = true,
D3D12_COMMAND_LIST_FLAGS flags = D3D12_COMMAND_LIST_FLAG_NONE) {
Ref<ID3D12Device4> device4;
if (FAILED(device->QueryInterface(IID_PPV_ARGS(&device4.p)))) return E_NOINTERFACE;
void* out = nullptr;
return released(device4->CreateCommandList1(node_mask, type, flags,
__uuidof(ID3D12GraphicsCommandList), output ? &out : nullptr),
&out);
}
// A 64 KiB heap description for the CreateHeap cases (D3D-238).
D3D12_HEAP_DESC heap_desc_of(UINT64 alignment, D3D12_HEAP_PROPERTIES properties = {D3D12_HEAP_TYPE_DEFAULT},
D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE, UINT64 size = 64 * 1024) {
D3D12_HEAP_DESC desc{};
desc.SizeInBytes = size;
desc.Properties = properties;
desc.Alignment = alignment;
desc.Flags = flags;
return desc;
}
HRESULT heap_create(ID3D12Device* device, D3D12_HEAP_DESC desc) {
void* out = nullptr;
return released(device->CreateHeap(&desc, __uuidof(ID3D12Heap), &out), &out);
}
// CreatePlacedResource in a fresh heap of `heap` made on the same device. A heap the device refuses makes the case
// itself wrong, so it answers E_UNEXPECTED on either side.
struct PlacedArgs {
D3D12_HEAP_DESC heap = heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT}, D3D12_HEAP_FLAG_NONE, 1024 * 1024);
UINT64 offset = 0;
D3D12_RESOURCE_STATES state = D3D12_RESOURCE_STATE_COMMON;
const D3D12_CLEAR_VALUE* clear = nullptr;
bool output = true;
const IID* riid = &__uuidof(ID3D12Resource);
};
HRESULT placed(ID3D12Device* device, const D3D12_RESOURCE_DESC* desc, const PlacedArgs& args = {}) {
ID3D12Heap* heap = nullptr;
if (FAILED(device->CreateHeap(&args.heap, IID_PPV_ARGS(&heap)))) return E_UNEXPECTED;
void* out = nullptr;
const HRESULT hr = device->CreatePlacedResource(heap, args.offset, desc, args.state, args.clear, *args.riid,
args.output ? &out : nullptr);
released(hr, &out);
heap->Release();
return hr;
}
HRESULT placed(ID3D12Device* device, D3D12_RESOURCE_DESC desc, const PlacedArgs& args = {}) {
return placed(device, &desc, args);
}
HRESULT reserved(ID3D12Device* device, const D3D12_RESOURCE_DESC* desc,
D3D12_RESOURCE_STATES state = D3D12_RESOURCE_STATE_COMMON, const D3D12_CLEAR_VALUE* clear = nullptr,
bool output = true, REFIID riid = __uuidof(ID3D12Resource)) {
void* out = nullptr;
return released(device->CreateReservedResource(desc, state, clear, riid, output ? &out : nullptr), &out);
}
HRESULT reserved(ID3D12Device* device, D3D12_RESOURCE_DESC desc,
D3D12_RESOURCE_STATES state = D3D12_RESOURCE_STATE_COMMON, const D3D12_CLEAR_VALUE* clear = nullptr) {
return reserved(device, &desc, state, clear);
}
D3D12_RESOURCE_DESC tiled(D3D12_RESOURCE_DESC desc) {
desc.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return desc;
}
// Map on a fresh committed resource; `range` is the read range, or null.
HRESULT map(ID3D12Device* device, D3D12_HEAP_PROPERTIES heap, D3D12_RESOURCE_DESC desc, D3D12_RESOURCE_STATES state,
UINT subresource, const D3D12_RANGE* range, bool want_pointer = true) {
ID3D12Resource* resource = nullptr;
HRESULT hr = device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, state, nullptr,
IID_PPV_ARGS(&resource));
if (FAILED(hr)) return E_UNEXPECTED; // the case itself is wrong: the resource must be creatable on both
void* data = nullptr;
hr = resource->Map(subresource, range, want_pointer ? &data : nullptr);
if (SUCCEEDED(hr)) resource->Unmap(subresource, nullptr);
return released(hr, reinterpret_cast<void* const*>(&resource));
}
using InvalidCase = HRESULT (*)(ID3D12Device*);
// A call that faults is reported as this, not a crash of the whole test (the host may not validate everything).
constexpr HRESULT kFaulted = HRESULT(0xC0000005);
HRESULT call_guarded(InvalidCase call, ID3D12Device* device) {
__try {
return call(device);
} __except (EXCEPTION_EXECUTE_HANDLER) {
return kFaulted;
}
}
// The host accepts a UAV buffer in RAYTRACING_ACCELERATION_STRUCTURE state only where OPTIONS5 reports ray tracing
// (an RTX does, an integrated Radeon without DXR refuses it: @aguedou-34 on #2462). The layer accepts it where it
// has acceleration structures: the standalone device (XDXR or KHR), not the device main builds without them.
bool host_ray_tracing(ID3D12Device* host) {
D3D12_FEATURE_DATA_D3D12_OPTIONS5 o5{};
return SUCCEEDED(host->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS5, &o5, sizeof(o5))) &&
o5.RaytracingTier != D3D12_RAYTRACING_TIER_NOT_SUPPORTED;
}
// Windows 10's runtime (no ID3D12Device10) still checks a buffer's initial state and refuses a HARDWARE_PROTECTED
// heap, where newer runtimes and the layer accept them (measured on Windows 10 22H2 build 19045.5737). Windows 11
// 23H2's runtime (D3D12Core 10.0.22621.5415: ID3D12Device10 but no ID3D12Device11) still refuses the
// HARDWARE_PROTECTED heap. For a case marked windows10_refuses only that pair is excused: the layer's S_OK against
// the host's E_INVALIDARG, on a host without ID3D12Device11. Any other difference, and any difference on a newer
// host, is still a mismatch.
bool windows10_refusal(const char* name, ID3D12Device* host, HRESULT ours, HRESULT theirs) {
if (ours != S_OK || theirs != E_INVALIDARG) return false;
Ref<ID3D12Device11> newer;
if (SUCCEEDED(host->QueryInterface(IID_PPV_ARGS(&newer.p)))) return false;
std::printf("invalid arguments %s: an older host runtime (Windows 10, Windows 11 23H2) refuses it (ours 0x%08lx, "
"host 0x%08lx)\n", name, ours, theirs);
return true;
}
int compare_invalid_arguments(ID3D12Device* ours) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
using H = D3D12_HEAP_PROPERTIES;
struct Case {
const char* name;
InvalidCase call;
bool (*comparable)(ID3D12Device*) = nullptr;
bool windows10_refuses = false;
};
const auto device10 = [](ID3D12Device* d) {
Ref<ID3D12Device10> newer;
return SUCCEEDED(d->QueryInterface(IID_PPV_ARGS(&newer.p)));
};
// Windows 11 23H2's in-box runtime (D3D12Core 10.0.22621.5415) has ID3D12Device10 but not ID3D12Device11, and
// still answers five of the device10 cases as Windows 10 does (HARDWARE_PROTECTED and flag 0x400 refused, 2049
// shader-visible samplers refused, a type -1 list created). Those five are compared only on a host with
// ID3D12Device11, which newer runtimes have (d3d12_sampler2_test compares against one).
const auto device11 = [](ID3D12Device* d) {
Ref<ID3D12Device11> newer;
return SUCCEEDED(d->QueryInterface(IID_PPV_ARGS(&newer.p)));
};
static const Case cases[] = {
// CreateCommittedResource: arguments.
// A null D3D12_HEAP_PROPERTIES faults in the host runtime, so it is not compared.
{"committed: null description", [](ID3D12Device* d) {
const H heap{D3D12_HEAP_TYPE_DEFAULT};
void* out = nullptr;
return released(d->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, nullptr,
D3D12_RESOURCE_STATE_COMMON, nullptr,
__uuidof(ID3D12Resource), &out), &out);
}},
{"committed: null output (capability check)", [](ID3D12Device* d) {
const H heap{D3D12_HEAP_TYPE_DEFAULT};
const D3D12_RESOURCE_DESC desc = buffer_desc(256);
return d->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON,
nullptr, __uuidof(ID3D12Resource), nullptr);
}},
{"committed: wrong interface", [](ID3D12Device* d) {
const H heap{D3D12_HEAP_TYPE_DEFAULT};
const D3D12_RESOURCE_DESC desc = buffer_desc(256);
void* out = nullptr;
return released(d->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc,
D3D12_RESOURCE_STATE_COMMON, nullptr,
__uuidof(ID3D12Fence), &out), &out);
}},
// CreateCommittedResource: the initial state. A buffer takes any bits but the acceleration-structure state
// without UAV access; a texture takes GENERIC_READ, one write state alone (RENDER_TARGET, UNORDERED_ACCESS,
// DEPTH_WRITE with their flag), or a mix of read states (DEPTH_READ with ALLOW_DEPTH_STENCIL, the shader
// resource states without DENY_SHADER_RESOURCE), and never a buffer-only or undefined bit.
{"committed: plain texture in RENDER_TARGET state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(), D3D12_RESOURCE_STATE_RENDER_TARGET);
}},
{"committed: render target in RENDER_TARGET state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
D3D12_RESOURCE_STATE_RENDER_TARGET);
}},
{"committed: plain texture in UNORDERED_ACCESS state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
}},
{"committed: UAV texture in UNORDERED_ACCESS state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS),
D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
}},
{"committed: plain texture in DEPTH_WRITE state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(), D3D12_RESOURCE_STATE_DEPTH_WRITE);
}},
{"committed: plain texture in DEPTH_READ state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(), D3D12_RESOURCE_STATE_DEPTH_READ);
}},
{"committed: depth texture in DEPTH_READ | PIXEL_SHADER_RESOURCE state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
D3D12_RESOURCE_STATE_DEPTH_READ | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
}},
{"committed: depth texture in DEPTH_WRITE | DEPTH_READ state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
D3D12_RESOURCE_STATE_DEPTH_WRITE | D3D12_RESOURCE_STATE_DEPTH_READ);
}},
{"committed: depth texture without shader resource in PIXEL_SHADER_RESOURCE state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL |
D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE),
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
}},
{"committed: depth texture without shader resource in GENERIC_READ state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL |
D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE),
D3D12_RESOURCE_STATE_GENERIC_READ);
}},
{"committed: texture in GENERIC_READ state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(), D3D12_RESOURCE_STATE_GENERIC_READ);
}},
{"committed: texture in GENERIC_READ | COPY_DEST state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(),
D3D12_RESOURCE_STATE_GENERIC_READ | D3D12_RESOURCE_STATE_COPY_DEST);
}},
{"committed: texture in COPY_DEST | PIXEL_SHADER_RESOURCE state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(),
D3D12_RESOURCE_STATE_COPY_DEST | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
}},
{"committed: texture in the three shader-read and copy-source states", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(),
D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_COPY_SOURCE);
}},
{"committed: render target in RENDER_TARGET | PIXEL_SHADER_RESOURCE state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
D3D12_RESOURCE_STATE_RENDER_TARGET | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
}},
{"committed: texture in VERTEX_AND_CONSTANT_BUFFER state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(), D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
}},
{"committed: texture in INDEX_BUFFER | PIXEL_SHADER_RESOURCE state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(),
D3D12_RESOURCE_STATE_INDEX_BUFFER | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
}},
{"committed: texture in state 0x80000000", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(), D3D12_RESOURCE_STATES(0x80000000u));
}},
{"committed: texture in RESOLVE_DEST state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(), D3D12_RESOURCE_STATE_RESOLVE_DEST);
}},
// A buffer's initial state: Windows 10's runtime refuses these two, newer ones accept them, as the layer does.
{"committed: buffer in state 0x80000000", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, buffer_desc(256), D3D12_RESOURCE_STATES(0x80000000u));
}, nullptr, true},
{"committed: buffer in RENDER_TARGET | UNORDERED_ACCESS state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, buffer_desc(256),
D3D12_RESOURCE_STATE_RENDER_TARGET | D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
}, nullptr, true},
{"committed: buffer in RAYTRACING_ACCELERATION_STRUCTURE state", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, buffer_desc(256),
D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE);
}},
// This device has no acceleration structures and refuses it, as a host without ray tracing does.
{"committed: UAV buffer in RAYTRACING_ACCELERATION_STRUCTURE state", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc,
D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE);
}, [](ID3D12Device* host) { return !host_ray_tracing(host); }},
// CreateCommittedResource: buffer descriptions.
{"committed: buffer width 0", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, buffer_desc(0));
}},
{"committed: buffer height 2", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Height = 2;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: buffer with 2 mips", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.MipLevels = 2;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: buffer with a format", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Format = DXGI_FORMAT_R8_UNORM;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: buffer not row-major", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: buffer 2 samples", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.SampleDesc.Count = 2;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: buffer alignment 12345", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Alignment = 12345;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: buffer as render target", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: buffer as depth stencil", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: buffer with a clear value", [](ID3D12Device* d) {
D3D12_CLEAR_VALUE clear{};
clear.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, buffer_desc(256), D3D12_RESOURCE_STATE_COMMON,
D3D12_HEAP_FLAG_NONE, &clear);
}},
{"committed: upload buffer in COMMON", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_UPLOAD}, buffer_desc(256), D3D12_RESOURCE_STATE_COMMON);
}, +device10},
{"committed: upload buffer in COPY_DEST", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_UPLOAD}, buffer_desc(256), D3D12_RESOURCE_STATE_COPY_DEST);
}},
{"committed: readback buffer in GENERIC_READ", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_READBACK}, buffer_desc(256), D3D12_RESOURCE_STATE_GENERIC_READ);
}},
{"committed: upload buffer with UAV", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
return committed(d, {D3D12_HEAP_TYPE_UPLOAD}, desc, D3D12_RESOURCE_STATE_GENERIC_READ);
}},
// CreateCommittedResource: heap properties and flags.
{"committed: heap type 99", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE(99)}, buffer_desc(256));
}},
{"committed: default heap with a CPU page property", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_WRITE_BACK}, buffer_desc(256));
}},
{"committed: default heap with a memory pool", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_UNKNOWN, D3D12_MEMORY_POOL_L0},
buffer_desc(256));
}},
{"committed: custom heap, unknown page property", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_UNKNOWN, D3D12_MEMORY_POOL_L0},
buffer_desc(256));
}},
{"committed: custom heap, unknown pool", [](ID3D12Device* d) {
return committed(d,
{D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_WRITE_BACK, D3D12_MEMORY_POOL_UNKNOWN},
buffer_desc(256));
}},
{"committed: node mask 2", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_UNKNOWN,
D3D12_MEMORY_POOL_UNKNOWN, 2, 2}, buffer_desc(256));
}},
{"committed: heap flag DENY_BUFFERS on a buffer", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, buffer_desc(256), D3D12_RESOURCE_STATE_COMMON,
D3D12_HEAP_FLAG_DENY_BUFFERS);
}},
{"committed: heap flag 0x80000000", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, buffer_desc(256), D3D12_RESOURCE_STATE_COMMON,
D3D12_HEAP_FLAGS(0x80000000));
}},
// CreateCommittedResource: texture descriptions.
{"committed: dimension UNKNOWN", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.Dimension = D3D12_RESOURCE_DIMENSION_UNKNOWN;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: dimension 9", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.Dimension = D3D12_RESOURCE_DIMENSION(9);
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: texture width 0", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(0, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE));
}},
{"committed: texture height 0", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 0, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE));
}},
{"committed: texture array size 0", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 1, 0));
}},
{"committed: texture array size 2049", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(4, 4, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 1, 2049));
}},
{"committed: volume depth 2049", [](ID3D12Device* d) {
auto desc = volume_desc(1);
desc.DepthOrArraySize = 2049;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: texture format UNKNOWN", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE));
}},
{"committed: texture format 250", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT(250), D3D12_RESOURCE_FLAG_NONE));
}},
{"committed: texture 0 samples", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.SampleDesc.Count = 0;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: texture 3 samples", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.SampleDesc.Count = 3;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: 4x MSAA with 2 mips", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, 2);
desc.SampleDesc.Count = 4;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: 4x MSAA quality 999", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.SampleDesc.Count = 4;
desc.SampleDesc.Quality = 999;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: 1x quality 1", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.SampleDesc.Quality = 1;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: 4x MSAA without RT or DS", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.SampleDesc.Count = 4;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: colour format as depth stencil", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
rgba8(D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL));
}},
{"committed: BC1 as render target", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET));
}},
{"committed: BC1 6x6 top mip", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(6, 6, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE));
}, host_supports_unaligned_block_textures},
{"committed: render target and depth stencil", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET |
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL));
}},
{"committed: depth stencil with UAV", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL |
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS));
}},
{"committed: DENY_SHADER_RESOURCE without depth stencil", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
rgba8(D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE));
}},
{"committed: resource flag 0x8000", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAGS(0x8000)));
}},
{"committed: texture on an upload heap", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_UPLOAD},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE),
D3D12_RESOURCE_STATE_GENERIC_READ);
}},
{"committed: texture on a readback heap", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_READBACK},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE),
D3D12_RESOURCE_STATE_COPY_DEST);
}},
{"committed: row-major 2D texture", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: texture alignment 12345", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.Alignment = 12345;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: render target clear value format UNKNOWN", [](ID3D12Device* d) {
D3D12_CLEAR_VALUE clear{};
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
D3D12_RESOURCE_STATE_COMMON, D3D12_HEAP_FLAG_NONE, &clear);
}},
{"committed: clear value on a plain texture", [](ID3D12Device* d) {
D3D12_CLEAR_VALUE clear{};
clear.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE),
D3D12_RESOURCE_STATE_COMMON, D3D12_HEAP_FLAG_NONE, &clear);
}},
// CreateCommittedResource: valid neighbours of the cases above, so a check does not reject too much.
{"committed: buffer aligned 64 KiB", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: buffer aligned 4 KiB", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Alignment = D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: buffer with UAV", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: readback buffer in COMMON", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_READBACK}, buffer_desc(256), D3D12_RESOURCE_STATE_COMMON);
}, +device10},
{"committed: readback buffer with UAV", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
return committed(d, {D3D12_HEAP_TYPE_READBACK}, desc, D3D12_RESOURCE_STATE_COPY_DEST);
}},
{"committed: node masks 1", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_UNKNOWN,
D3D12_MEMORY_POOL_UNKNOWN, 1, 1}, buffer_desc(256));
}},
{"committed: custom write-combine L0 buffer", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE, D3D12_MEMORY_POOL_L0},
buffer_desc(256));
}},
{"committed: heap flag CREATE_NOT_ZEROED", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, buffer_desc(256), D3D12_RESOURCE_STATE_COMMON,
D3D12_HEAP_FLAG_CREATE_NOT_ZEROED);
}},
{"committed: heap flag DENY_BUFFERS on a texture", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE),
D3D12_RESOURCE_STATE_COMMON, D3D12_HEAP_FLAG_DENY_BUFFERS);
}},
{"committed: heap flag DENY_RT_DS_TEXTURES on a render target", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
D3D12_RESOURCE_STATE_COMMON, D3D12_HEAP_FLAG_DENY_RT_DS_TEXTURES);
}},
{"committed: heap flag DENY_NON_RT_DS_TEXTURES on a plain texture", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE),
D3D12_RESOURCE_STATE_COMMON, D3D12_HEAP_FLAG_DENY_NON_RT_DS_TEXTURES);
}},
{"committed: 16x16 with 5 mips", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 5));
}},
{"committed: texture width 16384", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16384, 4, DXGI_FORMAT_R8_UNORM, D3D12_RESOURCE_FLAG_NONE));
}},
{"committed: texture array size 2048", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(4, 4, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 1, 2048));
}},
{"committed: volume 2049 wide", [](ID3D12Device* d) {
auto desc = volume_desc(1);
desc.Width = 2049;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: small texture aligned 4 KiB", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.Alignment = D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: large texture aligned 4 KiB", [](ID3D12Device* d) {
auto desc = texture_desc(1024, 1024, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.Alignment = D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: render target aligned 4 KiB", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.Alignment = D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: texture aligned 4 MiB", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.Alignment = D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: 4x MSAA aligned 4 MiB", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.SampleDesc.Count = 4;
desc.Alignment = D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: 4x MSAA aligned 64 KiB", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.SampleDesc.Count = 4;
desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: 4x MSAA depth", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
desc.SampleDesc.Count = 4;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc, D3D12_RESOURCE_STATE_DEPTH_WRITE);
}},
{"committed: render target with UAV", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET |
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS));
}},
{"committed: depth stencil, DENY_SHADER_RESOURCE", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL |
D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE),
D3D12_RESOURCE_STATE_DEPTH_WRITE);
}},
{"committed: typeless depth as depth stencil", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R32_TYPELESS, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
D3D12_RESOURCE_STATE_DEPTH_WRITE);
}},
{"committed: BC1 render target, typeless", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_BC1_TYPELESS, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET));
}},
{"committed: render target clear value", [](ID3D12Device* d) {
D3D12_CLEAR_VALUE clear{};
clear.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
D3D12_RESOURCE_STATE_COMMON, D3D12_HEAP_FLAG_NONE, &clear);
}},
{"committed: render target clear value of another format", [](ID3D12Device* d) {
D3D12_CLEAR_VALUE clear{};
clear.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
D3D12_RESOURCE_STATE_COMMON, D3D12_HEAP_FLAG_NONE, &clear);
}},
{"committed: typeless render target clear value", [](ID3D12Device* d) {
D3D12_CLEAR_VALUE clear{};
clear.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_TYPELESS,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
D3D12_RESOURCE_STATE_COMMON, D3D12_HEAP_FLAG_NONE, &clear);
}},
{"committed: depth clear value", [](ID3D12Device* d) {
D3D12_CLEAR_VALUE clear{};
clear.Format = DXGI_FORMAT_D32_FLOAT;
clear.DepthStencil.Depth = 1.0f;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
D3D12_RESOURCE_STATE_DEPTH_WRITE, D3D12_HEAP_FLAG_NONE, &clear);
}},
// Heap and resource flags one by one (measured the same on an RX 9070 XT, an integrated Radeon and WARP).
{"committed: heap flag SHARED on a buffer", [](ID3D12Device* d) {
return with_heap_flags(d, buffer_desc(256), D3D12_HEAP_FLAG_SHARED);
}},
{"committed: heap flag 0x2 on a buffer", [](ID3D12Device* d) {
return with_heap_flags(d, buffer_desc(256), 0x2);
}},
{"committed: heap flag ALLOW_DISPLAY on a buffer", [](ID3D12Device* d) {
return with_heap_flags(d, buffer_desc(256), D3D12_HEAP_FLAG_ALLOW_DISPLAY);
}},
{"committed: heap flag ALLOW_DISPLAY on a texture", [](ID3D12Device* d) {
return with_heap_flags(d, rgba8(), D3D12_HEAP_FLAG_ALLOW_DISPLAY);
}},
{"committed: heap flag 0x10 on a texture", [](ID3D12Device* d) {
return with_heap_flags(d, rgba8(), 0x10);
}},
{"committed: heap flag SHARED_CROSS_ADAPTER on a buffer", [](ID3D12Device* d) {
return with_heap_flags(d, buffer_desc(256), D3D12_HEAP_FLAG_SHARED_CROSS_ADAPTER);
}},
{"committed: heap flag HARDWARE_PROTECTED on a texture", [](ID3D12Device* d) {
return with_heap_flags(d, rgba8(), D3D12_HEAP_FLAG_HARDWARE_PROTECTED);
}, +device11},
{"committed: heap flag ALLOW_WRITE_WATCH on a buffer", [](ID3D12Device* d) {
return with_heap_flags(d, buffer_desc(256), D3D12_HEAP_FLAG_ALLOW_WRITE_WATCH);
}},
{"committed: heap flag ALLOW_SHADER_ATOMICS on a buffer", [](ID3D12Device* d) {
return with_heap_flags(d, buffer_desc(256), D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS);
}},
{"committed: heap flag ALLOW_SHADER_ATOMICS on a UAV buffer", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
return with_heap_flags(d, desc, D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS);
}},
{"committed: heap flag ALLOW_SHADER_ATOMICS on a UAV texture", [](ID3D12Device* d) {
return with_heap_flags(d, texture_desc(16, 16, DXGI_FORMAT_R32_FLOAT,
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS),
D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS);
}},
{"committed: heap flag ALLOW_SHADER_ATOMICS on a render-target UAV", [](ID3D12Device* d) {
return with_heap_flags(d, rgba8(D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET |
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS),
D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS);
}},
{"committed: heap flag ALLOW_SHADER_ATOMICS on a texture without UAV", [](ID3D12Device* d) {
return with_heap_flags(d, rgba8(), D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS);
}},
{"committed: heap flag ALLOW_SHADER_ATOMICS on a render target without UAV", [](ID3D12Device* d) {
return with_heap_flags(d, rgba8(D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS);
}},
{"committed: heap flag ALLOW_SHADER_ATOMICS on an upload buffer", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_UPLOAD}, buffer_desc(256), D3D12_RESOURCE_STATE_GENERIC_READ,
D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS);
}},
{"committed: heap flag CREATE_NOT_RESIDENT on a texture", [](ID3D12Device* d) {
return with_heap_flags(d, rgba8(), D3D12_HEAP_FLAG_CREATE_NOT_RESIDENT);
}},
{"committed: heap flag TOOLS_USE_MANUAL_WRITE_TRACKING on a buffer", [](ID3D12Device* d) {
return with_heap_flags(d, buffer_desc(256), D3D12_HEAP_FLAG_TOOLS_USE_MANUAL_WRITE_TRACKING);
}},
{"committed: heap flag DENY_RT_DS_TEXTURES on a texture", [](ID3D12Device* d) {
return with_heap_flags(d, rgba8(), D3D12_HEAP_FLAG_DENY_RT_DS_TEXTURES);
}},
{"committed: heap flag DENY_NON_RT_DS_TEXTURES on a buffer", [](ID3D12Device* d) {
return with_heap_flags(d, buffer_desc(256), D3D12_HEAP_FLAG_DENY_NON_RT_DS_TEXTURES);
}},
{"committed: resource flag DENY_SHADER_RESOURCE on a buffer", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAGS(D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE);
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: resource flag ALLOW_CROSS_ADAPTER on a buffer", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAGS(D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER);
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: resource flag ALLOW_CROSS_ADAPTER on a texture", [](ID3D12Device* d) {
auto desc = rgba8(D3D12_RESOURCE_FLAGS(D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER));
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: resource flag ALLOW_SIMULTANEOUS_ACCESS on a buffer", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAGS(D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS);
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: resource flag ALLOW_SIMULTANEOUS_ACCESS on a texture", [](ID3D12Device* d) {
auto desc = rgba8(D3D12_RESOURCE_FLAGS(D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS));
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: resource flag VIDEO_DECODE_REFERENCE_ONLY on a texture", [](ID3D12Device* d) {
auto desc = rgba8(D3D12_RESOURCE_FLAGS(D3D12_RESOURCE_FLAG_VIDEO_DECODE_REFERENCE_ONLY));
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: resource flag VIDEO_ENCODE_REFERENCE_ONLY on a buffer", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAGS(D3D12_RESOURCE_FLAG_VIDEO_ENCODE_REFERENCE_ONLY);
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: resource flag RAYTRACING_ACCELERATION_STRUCTURE on a buffer", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAGS(D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE);
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}, +device10},
{"committed: resource flag 0x200 on a buffer", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAGS(0x200);
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: resource flag 0x800 on a texture", [](ID3D12Device* d) {
auto desc = rgba8(D3D12_RESOURCE_FLAGS(0x800));
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: 4x MSAA quality 1", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.SampleDesc = {4, 1};
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: 4x MSAA quality standard pattern", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.SampleDesc = {4, DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN}; // older SDKs lack the D3D12_ name
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: 4x MSAA quality center pattern", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.SampleDesc = {4, DXGI_CENTER_MULTISAMPLE_QUALITY_PATTERN};
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}},
{"committed: resource flag RAYTRACING_ACCELERATION_STRUCTURE on a texture", [](ID3D12Device* d) {
auto desc = rgba8(D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE);
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}, +device10},
{"committed: resource flag 0x400 on a buffer", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAGS(0x400);
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc);
}, +device11},
{"committed: resource flag 0x400 on a texture", [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, rgba8(D3D12_RESOURCE_FLAGS(0x400)));
}, +device11},
// A null output is a capability check; with invalid arguments too, the host's answer says which comes first.
{"committed: heap type 99, null output", [](ID3D12Device* d) {
const H heap{D3D12_HEAP_TYPE(99)};
const D3D12_RESOURCE_DESC desc = buffer_desc(256);
return d->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON,
nullptr, __uuidof(ID3D12Resource), nullptr);
}},
{"committed: buffer height 2, null output", [](ID3D12Device* d) {
const H heap{D3D12_HEAP_TYPE_DEFAULT};
D3D12_RESOURCE_DESC desc = buffer_desc(256);
desc.Height = 2;
return d->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON,
nullptr, __uuidof(ID3D12Resource), nullptr);
}},
// CreateHeap (D3D-238): the four descriptions the host refused on an RX 5700 and WARP, and the heap-property
// rules CreateCommittedResource shares. A refusal needs no arena; the valid neighbours do, so
// compare_heap_neighbours compares them on the standalone device.
{"heap: alignment 4096", [](ID3D12Device* d) { return heap_create(d, heap_desc_of(4096)); }},
{"heap: custom heap, unknown page property", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_UNKNOWN,
D3D12_MEMORY_POOL_L0}));
}},
{"heap: all three DENY flags", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT},
D3D12_HEAP_FLAG_DENY_BUFFERS | D3D12_HEAP_FLAG_DENY_RT_DS_TEXTURES |
D3D12_HEAP_FLAG_DENY_NON_RT_DS_TEXTURES));
}},
{"heap: type 0", [](ID3D12Device* d) { return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE(0)})); }},
// ALLOW_DISPLAY is for committed resources on a default heap only (D3D12_HEAP_FLAGS documentation); the host
// refused it on an RTX 4070 SUPER and a UHD 770 (@CowBoYdu76).
{"heap: ALLOW_DISPLAY", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT}, D3D12_HEAP_FLAG_ALLOW_DISPLAY));
}},
{"heap: custom heap, unknown pool", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE,
D3D12_MEMORY_POOL_UNKNOWN}));
}},
{"heap: default heap with a CPU page property", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_WRITE_BACK}));
}},
{"heap: default heap with a memory pool", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_UNKNOWN,
D3D12_MEMORY_POOL_L0}));
}},
// CreateDescriptorHeap. A null description faults in the host runtime, so it is not compared.
{"descriptor heap: null output", [](ID3D12Device* d) {
const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 4};
return d->CreateDescriptorHeap(&desc, __uuidof(ID3D12DescriptorHeap), nullptr);
}},
// The host answers a null output before the flag, type and node rules (measured by @quackottu and here).
{"descriptor heap: shader-visible RTV, null output", [](ID3D12Device* d) {
const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 4,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE};
return d->CreateDescriptorHeap(&desc, __uuidof(ID3D12DescriptorHeap), nullptr);
}},
{"descriptor heap: flag 0x80, null output", [](ID3D12Device* d) {
const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 4,
D3D12_DESCRIPTOR_HEAP_FLAGS(0x80)};
return d->CreateDescriptorHeap(&desc, __uuidof(ID3D12DescriptorHeap), nullptr);
}},
{"descriptor heap: node mask 2, null output", [](ID3D12Device* d) {
const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 4,
D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 2};
return d->CreateDescriptorHeap(&desc, __uuidof(ID3D12DescriptorHeap), nullptr);
}},
{"descriptor heap: wrong interface", [](ID3D12Device* d) {
const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 4};
void* out = nullptr;
return released(d->CreateDescriptorHeap(&desc, __uuidof(ID3D12Fence), &out), &out);
}},
{"descriptor heap: 0 descriptors", [](ID3D12Device* d) {
return descriptor_heap(d, {D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 0});
}},
{"descriptor heap: shader-visible RTV", [](ID3D12Device* d) {
return descriptor_heap(d, {D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 4, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE});
}},
{"descriptor heap: shader-visible DSV", [](ID3D12Device* d) {
return descriptor_heap(d, {D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 4, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE});
}},
{"descriptor heap: shader-visible 2049 samplers", [](ID3D12Device* d) {
return descriptor_heap(d, {D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, 2049,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE});
}, +device11},
{"descriptor heap: shader-visible 2048 samplers", [](ID3D12Device* d) {
return descriptor_heap(d, {D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, 2048,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE});
}},
{"descriptor heap: flag 0x80", [](ID3D12Device* d) {
return descriptor_heap(d, {D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 4, D3D12_DESCRIPTOR_HEAP_FLAGS(0x80)});
}},
{"descriptor heap: node mask 2", [](ID3D12Device* d) {
return descriptor_heap(d, {D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 4, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 2});
}},
{"descriptor heap: node mask 1", [](ID3D12Device* d) {
return descriptor_heap(d, {D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 4, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 1});
}},
{"descriptor heap: shader-visible CBV/SRV/UAV", [](ID3D12Device* d) {
return descriptor_heap(d, {D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 4,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE});
}},
// CreateCommandQueue.
{"command queue: null description", [](ID3D12Device* d) {
void* out = nullptr;
return released(d->CreateCommandQueue(nullptr, __uuidof(ID3D12CommandQueue), &out), &out);
}},
{"command queue: null output", [](ID3D12Device* d) {
const D3D12_COMMAND_QUEUE_DESC desc{D3D12_COMMAND_LIST_TYPE_DIRECT};
return d->CreateCommandQueue(&desc, __uuidof(ID3D12CommandQueue), nullptr);
}},
{"command queue: wrong interface", [](ID3D12Device* d) {
const D3D12_COMMAND_QUEUE_DESC desc{D3D12_COMMAND_LIST_TYPE_DIRECT};
void* out = nullptr;
return released(d->CreateCommandQueue(&desc, __uuidof(ID3D12Fence), &out), &out);
}},
{"command queue: bundle", [](ID3D12Device* d) { return command_queue(d, {D3D12_COMMAND_LIST_TYPE_BUNDLE}); }},
{"command queue: type 99", [](ID3D12Device* d) { return command_queue(d, {D3D12_COMMAND_LIST_TYPE(99)}); }},
{"command queue: priority 5", [](ID3D12Device* d) {
return command_queue(d, {D3D12_COMMAND_LIST_TYPE_DIRECT, 5});
}},
{"command queue: flag 0x80", [](ID3D12Device* d) {
return command_queue(d, {D3D12_COMMAND_LIST_TYPE_DIRECT, 0, D3D12_COMMAND_QUEUE_FLAGS(0x80)});
}},
{"command queue: node mask 2", [](ID3D12Device* d) {
return command_queue(d, {D3D12_COMMAND_LIST_TYPE_DIRECT, 0, D3D12_COMMAND_QUEUE_FLAG_NONE, 2});
}},
{"command queue: global realtime priority", [](ID3D12Device* d) {
return command_queue(d, {D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_QUEUE_PRIORITY_GLOBAL_REALTIME});
}},
{"command queue: compute", [](ID3D12Device* d) { return command_queue(d, {D3D12_COMMAND_LIST_TYPE_COMPUTE}); }},
{"command queue: copy", [](ID3D12Device* d) { return command_queue(d, {D3D12_COMMAND_LIST_TYPE_COPY}); }},
// CreateQueryHeap: the description is checked first (types past PIPELINE_STATISTICS1 are refused), then a
// null output is E_UNEXPECTED. Known types this layer has no path for (stream output, video decode, mesh
// statistics) are E_NOTIMPL here and not compared.
{"query heap: occlusion", [](ID3D12Device* d) { return query_heap(d, D3D12_QUERY_HEAP_TYPE_OCCLUSION, 4); }},
{"query heap: timestamps", [](ID3D12Device* d) { return query_heap(d, D3D12_QUERY_HEAP_TYPE_TIMESTAMP, 4); }},
{"query heap: no queries", [](ID3D12Device* d) { return query_heap(d, D3D12_QUERY_HEAP_TYPE_OCCLUSION, 0); }},
{"query heap: type 8", [](ID3D12Device* d) { return query_heap(d, D3D12_QUERY_HEAP_TYPE(8), 1); }},
{"query heap: type 99", [](ID3D12Device* d) { return query_heap(d, D3D12_QUERY_HEAP_TYPE(99), 1); }},
{"query heap: type -1", [](ID3D12Device* d) { return query_heap(d, D3D12_QUERY_HEAP_TYPE(-1), 1); }},
{"query heap: node mask 1", [](ID3D12Device* d) {
return query_heap(d, D3D12_QUERY_HEAP_TYPE_OCCLUSION, 1, 1);
}},
{"query heap: node mask 2", [](ID3D12Device* d) {
return query_heap(d, D3D12_QUERY_HEAP_TYPE_OCCLUSION, 1, 2);
}},
{"query heap: null output", [](ID3D12Device* d) {
return query_heap(d, D3D12_QUERY_HEAP_TYPE_OCCLUSION, 4, 0, false);
}},
{"query heap: null output, no queries", [](ID3D12Device* d) {
return query_heap(d, D3D12_QUERY_HEAP_TYPE_OCCLUSION, 0, 0, false);
}},
{"query heap: null output, type 99", [](ID3D12Device* d) {
return query_heap(d, D3D12_QUERY_HEAP_TYPE(99), 1, 0, false);
}},
{"query heap: null output, node mask 2", [](ID3D12Device* d) {
return query_heap(d, D3D12_QUERY_HEAP_TYPE_OCCLUSION, 1, 2, false);
}},
{"query heap: wrong interface", [](ID3D12Device* d) {
const D3D12_QUERY_HEAP_DESC desc{D3D12_QUERY_HEAP_TYPE_OCCLUSION, 1, 0};
void* out = nullptr;
return released(d->CreateQueryHeap(&desc, __uuidof(ID3D12Fence), &out), &out);
}},
// CreateRootSignature: a null output is S_FALSE whatever the blob; otherwise node 0 only, and the blob's
// DXBC size must be the size passed.
{"root signature: empty", [](ID3D12Device* d) { return root_signature(d, root_signature_blob(0)); }},
{"root signature: root CBV", [](ID3D12Device* d) { return root_signature(d, root_signature_blob(1)); }},
{"root signature: unbounded SRV table", [](ID3D12Device* d) {
return root_signature(d, root_signature_blob(2));
}},
{"root signature: size 0", [](ID3D12Device* d) {
void* out = nullptr;
const auto blob = root_signature_blob(0);
return released(d->CreateRootSignature(0, blob.data(), 0, __uuidof(ID3D12RootSignature), &out), &out);
}},
{"root signature: one byte short", [](ID3D12Device* d) {
const auto blob = root_signature_blob(0);
return root_signature(d, blob, blob.size() - 1);
}},
{"root signature: 4 bytes over", [](ID3D12Device* d) {
const auto blob = root_signature_blob(1);
return root_signature(d, blob, blob.size() + 4);
}},
{"root signature: 16 bytes over", [](ID3D12Device* d) {
const auto blob = root_signature_blob(0);
return root_signature(d, blob, blob.size() + 16);
}},
{"root signature: 64 zero bytes", [](ID3D12Device* d) {
return root_signature(d, std::vector<std::uint8_t>(64, 0));
}},
{"root signature: null blob", [](ID3D12Device* d) {
void* out = nullptr;
return released(d->CreateRootSignature(0, nullptr, 64, __uuidof(ID3D12RootSignature), &out), &out);
}},
{"root signature: node mask 1", [](ID3D12Device* d) {
return root_signature(d, root_signature_blob(0), 0, 1);
}},
{"root signature: node mask 2", [](ID3D12Device* d) {
return root_signature(d, root_signature_blob(0), 0, 2);
}},
{"root signature: node mask 0x80000000", [](ID3D12Device* d) {
return root_signature(d, root_signature_blob(0), 0, 0x80000000u);
}},
{"root signature: wrong interface", [](ID3D12Device* d) {
return root_signature(d, root_signature_blob(0), 0, 0, true, __uuidof(ID3D12Fence));
}},
{"root signature: null output", [](ID3D12Device* d) {
return root_signature(d, root_signature_blob(1), 0, 0, false);
}},
{"root signature: null output, 64 zero bytes", [](ID3D12Device* d) {
return root_signature(d, std::vector<std::uint8_t>(64, 0), 0, 0, false);
}},
{"root signature: null output, node mask 2", [](ID3D12Device* d) {
return root_signature(d, root_signature_blob(0), 0, 2, false);
}},
{"root signature: null output, 16 bytes over", [](ID3D12Device* d) {
const auto blob = root_signature_blob(0);
return root_signature(d, blob, blob.size() + 16, 0, false);
}},
{"command queue: high priority", [](ID3D12Device* d) {
return command_queue(d, {D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_QUEUE_PRIORITY_HIGH});
}},
// Command allocators and lists (video list types are only created here, not compared: the host hands out
// a video list, which is not an ID3D12GraphicsCommandList). Types past VIDEO_ENCODE and nodes other than 0
// are refused first; a null output is then E_UNEXPECTED.
{"command allocator: type direct", [](ID3D12Device* d) {
return command_allocator(d, D3D12_COMMAND_LIST_TYPE(0));
}},
{"command allocator: type bundle", [](ID3D12Device* d) {
return command_allocator(d, D3D12_COMMAND_LIST_TYPE(1));
}},
{"command allocator: type compute", [](ID3D12Device* d) {
return command_allocator(d, D3D12_COMMAND_LIST_TYPE(2));
}},
{"command allocator: type copy", [](ID3D12Device* d) {
return command_allocator(d, D3D12_COMMAND_LIST_TYPE(3));
}},
{"command allocator: type video encode", [](ID3D12Device* d) {
return command_allocator(d, D3D12_COMMAND_LIST_TYPE_VIDEO_ENCODE);
}, [](ID3D12Device* d) {
// A host without the D3D12 video interface, such as WARP on Windows 10, rejects video allocators.
Ref<ID3D12VideoDevice> video;
return d->QueryInterface(IID_PPV_ARGS(&video.p)) != E_NOINTERFACE;
}},
{"command allocator: type 7", [](ID3D12Device* d) { return command_allocator(d, D3D12_COMMAND_LIST_TYPE(7)); }},
{"command allocator: type 99", [](ID3D12Device* d) {
return command_allocator(d, D3D12_COMMAND_LIST_TYPE(99));
}},
{"command allocator: type -1", [](ID3D12Device* d) {
return command_allocator(d, D3D12_COMMAND_LIST_TYPE(-1));
}},
{"command allocator: null output", [](ID3D12Device* d) {
return command_allocator(d, D3D12_COMMAND_LIST_TYPE_DIRECT, false);
}},
{"command allocator: null output, type 99", [](ID3D12Device* d) {
return command_allocator(d, D3D12_COMMAND_LIST_TYPE(99), false);
}},
{"command allocator: null output, wrong interface", [](ID3D12Device* d) {
return command_allocator(d, D3D12_COMMAND_LIST_TYPE_DIRECT, false, __uuidof(ID3D12Fence));
}},
{"command allocator: wrong interface", [](ID3D12Device* d) {
return command_allocator(d, D3D12_COMMAND_LIST_TYPE_DIRECT, true, __uuidof(ID3D12Fence));
}},
{"command list: direct", [](ID3D12Device* d) { return command_list(d, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, 0); }},
{"command list: bundle", [](ID3D12Device* d) { return command_list(d, 0, D3D12_COMMAND_LIST_TYPE_BUNDLE, 1); }},
{"command list: node mask 1", [](ID3D12Device* d) {
return command_list(d, 1, D3D12_COMMAND_LIST_TYPE_DIRECT, 0);
}},
{"command list: node mask 2", [](ID3D12Device* d) {
return command_list(d, 2, D3D12_COMMAND_LIST_TYPE_DIRECT, 0);
}},
{"command list: node mask 3", [](ID3D12Device* d) {
return command_list(d, 3, D3D12_COMMAND_LIST_TYPE_DIRECT, 0);
}},
{"command list: compute allocator", [](ID3D12Device* d) {
return command_list(d, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, 2);
}},
{"command list: no allocator", [](ID3D12Device* d) {
return command_list(d, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, -2);
}},
{"command list: type 99", [](ID3D12Device* d) { return command_list(d, 0, D3D12_COMMAND_LIST_TYPE(99), 0); }},
{"command list: null output", [](ID3D12Device* d) {
return command_list(d, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, 0, false);
}},
{"command list: null output, node mask 2", [](ID3D12Device* d) {
return command_list(d, 2, D3D12_COMMAND_LIST_TYPE_DIRECT, 0, false);
}},
{"command list: null output, no allocator", [](ID3D12Device* d) {
return command_list(d, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, -2, false);
}},
{"command list1: type direct", [](ID3D12Device* d) { return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE(0)); }},
{"command list1: type bundle", [](ID3D12Device* d) { return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE(1)); }},
{"command list1: type compute", [](ID3D12Device* d) {
return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE(2));
}},
{"command list1: type copy", [](ID3D12Device* d) { return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE(3)); }},
{"command list1: type 7", [](ID3D12Device* d) { return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE(7)); }},
{"command list1: type 99", [](ID3D12Device* d) { return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE(99)); }},
// NONE (-1): Windows 10's runtime (19045, no ID3D12Device10) creates the list and newer Windows 11 runtimes
// refuse it (measured on #2297); Windows 11 23H2 (22621.5415, ID3D12Device10 but no ID3D12Device11) still
// creates it, so it is compared only on a host with ID3D12Device11.
{"command list1: type -1", [](ID3D12Device* d) {
return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE(-1));
}, +device11},
{"command list1: node mask 1", [](ID3D12Device* d) {
return command_list1(d, 1, D3D12_COMMAND_LIST_TYPE_DIRECT);
}},
{"command list1: node mask 2", [](ID3D12Device* d) {
return command_list1(d, 2, D3D12_COMMAND_LIST_TYPE_DIRECT);
}},
{"command list1: null output", [](ID3D12Device* d) {
return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, false);
}},
{"command list1: null output, node mask 2", [](ID3D12Device* d) {
return command_list1(d, 2, D3D12_COMMAND_LIST_TYPE_DIRECT, false);
}},
{"command list1: null output, type 99", [](ID3D12Device* d) {
return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE(99), false);
}},
{"command list1: flags 1", [](ID3D12Device* d) {
return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, true, D3D12_COMMAND_LIST_FLAGS(1));
}},
{"command list1: flags 0xffffffff", [](ID3D12Device* d) {
return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, true, D3D12_COMMAND_LIST_FLAGS(0xffffffffu));
}},
{"command list1: null output, flags 1", [](ID3D12Device* d) {
return command_list1(d, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, false, D3D12_COMMAND_LIST_FLAGS(1));
}},
// CreateCommandSignature (no root signature). A null description faults in the host runtime, so it is not
// compared.
{"command signature: null output", [](ID3D12Device* d) {
const D3D12_INDIRECT_ARGUMENT_DESC arg{D3D12_INDIRECT_ARGUMENT_TYPE_DRAW};
const D3D12_COMMAND_SIGNATURE_DESC desc{16, 1, &arg, 0};
return d->CreateCommandSignature(&desc, nullptr, __uuidof(ID3D12CommandSignature), nullptr);
}},
{"command signature: wrong interface", [](ID3D12Device* d) {
const D3D12_INDIRECT_ARGUMENT_DESC arg{D3D12_INDIRECT_ARGUMENT_TYPE_DRAW};
const D3D12_COMMAND_SIGNATURE_DESC desc{16, 1, &arg, 0};
void* out = nullptr;
return released(d->CreateCommandSignature(&desc, nullptr, __uuidof(ID3D12Fence), &out), &out);
}},
{"command signature: draw", [](ID3D12Device* d) {
return command_signature(d, 16, {D3D12_INDIRECT_ARGUMENT_TYPE_DRAW});
}},
{"command signature: draw indexed", [](ID3D12Device* d) {
return command_signature(d, 20, {D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED});
}},
{"command signature: dispatch", [](ID3D12Device* d) {
return command_signature(d, 12, {D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH});
}},
{"command signature: dispatch, stride 64", [](ID3D12Device* d) {
return command_signature(d, 64, {D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH});
}},
{"command signature: no arguments", [](ID3D12Device* d) { return command_signature(d, 16, {}); }},
{"command signature: stride 0", [](ID3D12Device* d) {
return command_signature(d, 0, {D3D12_INDIRECT_ARGUMENT_TYPE_DRAW});
}},
{"command signature: stride 18", [](ID3D12Device* d) {
return command_signature(d, 18, {D3D12_INDIRECT_ARGUMENT_TYPE_DRAW});
}},
{"command signature: draw stride 12", [](ID3D12Device* d) {
return command_signature(d, 12, {D3D12_INDIRECT_ARGUMENT_TYPE_DRAW});
}},
{"command signature: draw indexed stride 16", [](ID3D12Device* d) {
return command_signature(d, 16, {D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED});
}},
{"command signature: two draws", [](ID3D12Device* d) {
return command_signature(d, 32, {D3D12_INDIRECT_ARGUMENT_TYPE_DRAW, D3D12_INDIRECT_ARGUMENT_TYPE_DRAW});
}},
{"command signature: draw then dispatch", [](ID3D12Device* d) {
return command_signature(d, 28,
{D3D12_INDIRECT_ARGUMENT_TYPE_DRAW, D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH});
}},
{"command signature: vertex buffer view only", [](ID3D12Device* d) {
return command_signature(d, 16, {D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW});
}},
{"command signature: draw before a vertex buffer view", [](ID3D12Device* d) {
return command_signature(d, 32, {D3D12_INDIRECT_ARGUMENT_TYPE_DRAW,
D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW});
}},
{"command signature: constant without a root signature", [](ID3D12Device* d) {
return command_signature(d, 20,
{D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT, D3D12_INDIRECT_ARGUMENT_TYPE_DRAW});
}},
{"command signature: UAV without a root signature", [](ID3D12Device* d) {
return command_signature(d, 20, {D3D12_INDIRECT_ARGUMENT_TYPE_UNORDERED_ACCESS_VIEW,
D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH});
}},
{"command signature: type 99", [](ID3D12Device* d) {
return command_signature(d, 16, {D3D12_INDIRECT_ARGUMENT_TYPE(99)});
}},
{"command signature: type 99 before a draw", [](ID3D12Device* d) {
return command_signature(d, 32, {D3D12_INDIRECT_ARGUMENT_TYPE(99), D3D12_INDIRECT_ARGUMENT_TYPE_DRAW});
}},
{"command signature: node mask 1", [](ID3D12Device* d) {
return command_signature(d, 16, {D3D12_INDIRECT_ARGUMENT_TYPE_DRAW}, 1);
}},
{"command signature: node mask 2", [](ID3D12Device* d) {
return command_signature(d, 16, {D3D12_INDIRECT_ARGUMENT_TYPE_DRAW}, 2);
}},
{"command queue: disable GPU timeout", [](ID3D12Device* d) {
return command_queue(d, {D3D12_COMMAND_LIST_TYPE_DIRECT, 0, D3D12_COMMAND_QUEUE_FLAG_DISABLE_GPU_TIMEOUT});
}},
{"command queue: node mask 1", [](ID3D12Device* d) {
return command_queue(d, {D3D12_COMMAND_LIST_TYPE_DIRECT, 0, D3D12_COMMAND_QUEUE_FLAG_NONE, 1});
}},
// CreateFence.
// FORMAT_SUPPORT for values the runtime's format table has no entry for (E_FAIL), and for UNKNOWN (a buffer
// format: Support1 is compared, not Support2).
{"format support: UNKNOWN", [](ID3D12Device* d) { return format_support1(d, DXGI_FORMAT_UNKNOWN); }},
{"format support: 116, the first unused value", [](ID3D12Device* d) { return format_support1(d, 0x74u); }},
{"format support: 129", [](ID3D12Device* d) { return format_support1(d, 0x81u); }},
{"format support: 130, P208", [](ID3D12Device* d) { return format_support1(d, 0x82u); }},
{"format support: 131, V208", [](ID3D12Device* d) { return format_support1(d, 0x83u); }},
{"format support: 132, V408", [](ID3D12Device* d) { return format_support1(d, 0x84u); }},
{"format support: 133", [](ID3D12Device* d) { return format_support1(d, 0x85u); }},
{"format support: 188", [](ID3D12Device* d) { return format_support1(d, 0xbcu); }},
{"format support: 192, past A4B4G4R4", [](ID3D12Device* d) { return format_support1(d, 0xc0u); }},
{"format support: 1000", [](ID3D12Device* d) { return format_support1(d, 0x3e8u); }},
{"format support: -1", [](ID3D12Device* d) { return format_support1(d, 0xffffffffu); }},
{"fence: null output", [](ID3D12Device* d) {
return d->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), nullptr);
}},
{"fence: flag 0x80, null output", [](ID3D12Device* d) {
return d->CreateFence(0, D3D12_FENCE_FLAGS(0x80), __uuidof(ID3D12Fence), nullptr);
}},
{"fence: non-monitored, null output", [](ID3D12Device* d) {
return d->CreateFence(0, D3D12_FENCE_FLAG_NON_MONITORED, __uuidof(ID3D12Fence), nullptr);
}},
{"fence: wrong interface", [](ID3D12Device* d) {
return fence(d, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Resource));
}},
{"fence: flag 0x80", [](ID3D12Device* d) { return fence(d, D3D12_FENCE_FLAGS(0x80)); }},
{"fence: cross-adapter without shared", [](ID3D12Device* d) {
return fence(d, D3D12_FENCE_FLAG_SHARED_CROSS_ADAPTER);
}},
{"fence: non-monitored", [](ID3D12Device* d) { return fence(d, D3D12_FENCE_FLAG_NON_MONITORED); }},
{"fence: shared", [](ID3D12Device* d) { return fence(d, D3D12_FENCE_FLAG_SHARED); }},
{"fence: shared cross-adapter", [](ID3D12Device* d) {
return fence(d, D3D12_FENCE_FLAG_SHARED | D3D12_FENCE_FLAG_SHARED_CROSS_ADAPTER);
}},
{"fence: shared non-monitored", [](ID3D12Device* d) {
return fence(d, D3D12_FENCE_FLAG_SHARED | D3D12_FENCE_FLAG_NON_MONITORED);
}},
// Map.
{"map: upload buffer", [](ID3D12Device* d) {
return map(d, {D3D12_HEAP_TYPE_UPLOAD}, buffer_desc(256), D3D12_RESOURCE_STATE_GENERIC_READ, 0, nullptr);
}},
{"map: upload buffer, null pointer", [](ID3D12Device* d) {
return map(d, {D3D12_HEAP_TYPE_UPLOAD}, buffer_desc(256), D3D12_RESOURCE_STATE_GENERIC_READ, 0, nullptr,
false);
}},
{"map: upload buffer, subresource 1", [](ID3D12Device* d) {
return map(d, {D3D12_HEAP_TYPE_UPLOAD}, buffer_desc(256), D3D12_RESOURCE_STATE_GENERIC_READ, 1, nullptr);
}},
{"map: readback buffer, read range past the end", [](ID3D12Device* d) {
const D3D12_RANGE range{0, 4096};
return map(d, {D3D12_HEAP_TYPE_READBACK}, buffer_desc(256), D3D12_RESOURCE_STATE_COPY_DEST, 0, &range);
}},
{"map: readback buffer, reversed read range", [](ID3D12Device* d) {
const D3D12_RANGE range{128, 64};
return map(d, {D3D12_HEAP_TYPE_READBACK}, buffer_desc(256), D3D12_RESOURCE_STATE_COPY_DEST, 0, &range);
}},
{"map: readback buffer, empty read range", [](ID3D12Device* d) {
const D3D12_RANGE range{0, 0};
return map(d, {D3D12_HEAP_TYPE_READBACK}, buffer_desc(256), D3D12_RESOURCE_STATE_COPY_DEST, 0, &range);
}},
{"map: default buffer", [](ID3D12Device* d) {
return map(d, {D3D12_HEAP_TYPE_DEFAULT}, buffer_desc(256), D3D12_RESOURCE_STATE_COMMON, 0, nullptr);
}},
{"map: default buffer, null pointer", [](ID3D12Device* d) {
return map(d, {D3D12_HEAP_TYPE_DEFAULT}, buffer_desc(256), D3D12_RESOURCE_STATE_COMMON, 0, nullptr, false);
}},
{"map: default texture", [](ID3D12Device* d) {
return map(d, {D3D12_HEAP_TYPE_DEFAULT},
texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE),
D3D12_RESOURCE_STATE_COMMON, 0, nullptr);
}},
{"map: custom write-back buffer", [](ID3D12Device* d) {
return map(d, {D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_WRITE_BACK, D3D12_MEMORY_POOL_L0},
buffer_desc(256), D3D12_RESOURCE_STATE_COMMON, 0, nullptr);
}},
{"map: custom NOT_AVAILABLE buffer", [](ID3D12Device* d) {
return map(d, {D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE, D3D12_MEMORY_POOL_L0},
buffer_desc(256), D3D12_RESOURCE_STATE_COMMON, 0, nullptr);
}},
};
int mismatches = 0;
const bool verbose = GetEnvironmentVariableA("LEONIDA_D3D49_VERBOSE", nullptr, 0) > 0;
for (const Case& c : cases) {
if (verbose) { // flushed first: a host runtime that dies on a case leaves its name behind
std::printf("invalid arguments %s:", c.name);
std::fflush(stdout);
}
const HRESULT a = call_guarded(c.call, ours), b = call_guarded(c.call, host.p);
if (verbose) std::printf(" ours 0x%08lx host 0x%08lx\n", a, b);
if (a == b) continue;
if (c.windows10_refuses && windows10_refusal(c.name, host.p, a, b)) continue;
if (c.comparable && !c.comparable(host.p)) {
std::printf("invalid arguments %s: not comparable on this host (ours 0x%08lx, host 0x%08lx)\n", c.name,
a, b);
continue;
}
++mismatches;
std::fprintf(stderr, "invalid arguments %s: ours 0x%08lx, host 0x%08lx\n", c.name, a, b);
}
// A buffer on a GPU upload heap. The layer reports OPTIONS16.GPUUploadHeapSupported FALSE and refuses the heap
// type; a host runtime refuses it too when it has no such heap (Resizable BAR off), and accepts it otherwise, so
// the answers are compared only when the host also reports no support.
{
D3D12_FEATURE_DATA_D3D12_OPTIONS16 host16{};
const bool host_gpu_upload =
SUCCEEDED(host->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS16, &host16, sizeof(host16))) &&
host16.GPUUploadHeapSupported;
const auto gpu_upload = [](ID3D12Device* d) {
return committed(d, {D3D12_HEAP_TYPE_GPU_UPLOAD}, buffer_desc(256));
};
const HRESULT a = call_guarded(gpu_upload, ours), b = call_guarded(gpu_upload, host.p);
if (host_gpu_upload) {
if (verbose) std::printf("invalid arguments GPU upload heap buffer: the host has GPU upload heaps\n");
} else if (a != b) {
++mismatches;
std::fprintf(stderr, "invalid arguments GPU upload heap buffer: ours 0x%08lx, host 0x%08lx\n", a, b);
} else if (verbose) {
std::printf("invalid arguments GPU upload heap buffer: ours 0x%08lx host 0x%08lx\n", a, b);
}
}
// ALLOW_RENDER_TARGET and ALLOW_DEPTH_STENCIL on every DXGI format both devices create as a plain texture. The
// host's answers follow the format family (the same on the three adapters measured), except R32G32B32, which is
// the driver's. Whether the layer can make an attachment of a format is its Vulkan driver's answer, so only the
// host refusing what the layer accepts is a mismatch; the other way round (D32_FLOAT and D16_UNORM as render
// targets, the R32/R16 colour formats as depth buffers on the drivers measured) is printed.
for (int f = 1; f <= 99; ++f) {
const DXGI_FORMAT format = DXGI_FORMAT(f);
const D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_DEFAULT};
const auto create = [&](ID3D12Device* device, D3D12_RESOURCE_FLAGS flags) {
return committed(device, heap, texture_desc(16, 16, format, flags));
};
if (FAILED(create(ours, D3D12_RESOURCE_FLAG_NONE)) || FAILED(create(host.p, D3D12_RESOURCE_FLAG_NONE)))
continue;
for (const D3D12_RESOURCE_FLAGS flags : {D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL}) {
const char* what = flags == D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET ? "RT" : "DS";
const HRESULT a = create(ours, flags), b = create(host.p, flags);
if (verbose)
std::printf("invalid arguments format %d %s: ours 0x%08lx host 0x%08lx\n", f, what, a, b);
if (a == b) continue;
if (FAILED(a) && SUCCEEDED(b)) {
std::printf("invalid arguments format %d %s: the host accepts it, this Vulkan driver can't\n", f, what);
continue;
}
// The other way round is a mismatch, except where the layer takes a console format stand-in the host
// cannot attach: an R9G9B9E5 render target is made as B10G11R11 (console format stand-ins, fa83b2825),
// which the title asserts on. Only present on a GPU without a native E5B9G9R9 attachment.
if (SUCCEEDED(a) && FAILED(b) && format == DXGI_FORMAT_R9G9B9E5_SHAREDEXP &&
flags == D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) {
std::printf("invalid arguments format %d RT: a console stand-in the host itself refuses\n", f);
continue;
}
++mismatches;
std::fprintf(stderr, "invalid arguments format %d %s: ours 0x%08lx, host 0x%08lx\n", f, what, a, b);
}
}
return mismatches;
}
// D3D-238: the valid neighbours of the CreateHeap refusals in compare_invalid_arguments, compared
// with the host on a device that has the GPU address arena (without it the layer answers every heap E_NOTIMPL).
int compare_heap_neighbours(ID3D12Device* ours) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
struct Case {
const char* name;
InvalidCase call;
bool windows10_refuses = false;
};
static const Case cases[] = {
{"heap: alignment 0", [](ID3D12Device* d) { return heap_create(d, heap_desc_of(0)); }},
{"heap: alignment 64 KiB", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT));
}},
{"heap: alignment 4 MiB", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT,
{D3D12_HEAP_TYPE_DEFAULT}, D3D12_HEAP_FLAG_NONE,
D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT));
}},
{"heap: size 1000", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT}, D3D12_HEAP_FLAG_NONE, 1000));
}},
{"heap: upload", [](ID3D12Device* d) { return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_UPLOAD})); }},
{"heap: readback", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_READBACK}));
}},
{"heap: custom write-combine L0", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE,
D3D12_MEMORY_POOL_L0}));
}},
{"heap: ALLOW_ONLY_BUFFERS (two DENY flags)", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT}, D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS));
}},
// The D3D12_HEAP_FLAGS documentation calls it unsupported, but the host accepts it (RTX 4070 SUPER and
// UHD 770, @CowBoYdu76). Windows 10's and Windows 11 23H2's runtimes refuse it, as they refuse the committed
// case above.
{"heap: HARDWARE_PROTECTED", [](ID3D12Device* d) {
return heap_create(d, heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT}, D3D12_HEAP_FLAG_HARDWARE_PROTECTED));
}, true},
};
int mismatches = 0;
for (const Case& c : cases) {
const HRESULT a = call_guarded(c.call, ours), b = call_guarded(c.call, host.p);
if (a == b) continue;
if (c.windows10_refuses && windows10_refusal(c.name, host.p, a, b)) continue;
++mismatches;
std::fprintf(stderr, "invalid arguments %s: ours 0x%08lx, host 0x%08lx\n", c.name, a, b);
}
return mismatches;
}
// ID3D12Heap::GetDesc reads back as the host's for heaps of each type with each node-mask pair, on a device with the
// GPU address arena: a node mask of 0 reads back as the one node (1), as the layer's resources, descriptor heaps and
// queues already answer (d3d12_node_mask_test). Returns the number of mismatches, or -1 without a host.
int compare_heap_descs(ID3D12Device* ours) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
struct HeapCase {
D3D12_HEAP_PROPERTIES properties;
UINT64 alignment; // 0: the default
};
const HeapCase heaps[] = {
{{D3D12_HEAP_TYPE_DEFAULT}, 0},
{{D3D12_HEAP_TYPE_DEFAULT}, D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT},
{{D3D12_HEAP_TYPE_UPLOAD}, 0},
{{D3D12_HEAP_TYPE_READBACK}, 0},
{{D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE, D3D12_MEMORY_POOL_L0}, 0},
};
const UINT masks[][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
int mismatches = 0;
for (const HeapCase& heap : heaps) {
for (const auto& mask : masks) {
D3D12_HEAP_PROPERTIES props = heap.properties;
props.CreationNodeMask = mask[0];
props.VisibleNodeMask = mask[1];
const D3D12_HEAP_DESC desc = heap_desc_of(heap.alignment, props, D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS,
heap.alignment ? heap.alignment : 64 * 1024);
Ref<ID3D12Heap> a, e;
const HRESULT ha = ours->CreateHeap(&desc, IID_PPV_ARGS(&a.p));
const HRESULT he = host->CreateHeap(&desc, IID_PPV_ARGS(&e.p));
if (FAILED(he) || !e.p) continue; // nothing to compare
if (FAILED(ha) || !a.p) {
++mismatches;
std::fprintf(stderr, "heap GetDesc type %d align %llu masks %u/%u: the layer refused it (0x%08lx)\n",
int(props.Type), heap.alignment, mask[0], mask[1], ha);
continue;
}
const D3D12_HEAP_DESC x = a->GetDesc(), y = e->GetDesc();
if (x.SizeInBytes == y.SizeInBytes && x.Alignment == y.Alignment && x.Flags == y.Flags &&
x.Properties.Type == y.Properties.Type &&
x.Properties.CPUPageProperty == y.Properties.CPUPageProperty &&
x.Properties.MemoryPoolPreference == y.Properties.MemoryPoolPreference &&
x.Properties.CreationNodeMask == y.Properties.CreationNodeMask &&
x.Properties.VisibleNodeMask == y.Properties.VisibleNodeMask)
continue;
++mismatches;
std::fprintf(stderr,
"heap GetDesc type %d align %llu masks %u/%u: ours size %llu align %llu flags 0x%x cpu %d "
"pool %d masks %u/%u, host size %llu align %llu flags 0x%x cpu %d pool %d masks %u/%u\n",
int(props.Type), heap.alignment, mask[0], mask[1], x.SizeInBytes, x.Alignment,
unsigned(x.Flags), int(x.Properties.CPUPageProperty),
int(x.Properties.MemoryPoolPreference), x.Properties.CreationNodeMask,
x.Properties.VisibleNodeMask, y.SizeInBytes, y.Alignment, unsigned(y.Flags),
int(y.Properties.CPUPageProperty), int(y.Properties.MemoryPoolPreference),
y.Properties.CreationNodeMask, y.Properties.VisibleNodeMask);
}
}
std::printf("ID3D12Heap::GetDesc against the host: %zu heaps, %d differ\n", std::size(heaps) * std::size(masks),
mismatches);
return mismatches;
}
// CreatePlacedResource and CreateReservedResource answer their arguments as the host runtime does: the placement
// (offset, alignment, room in the heap), the heap's type and DENY flags against the resource, the description and
// initial state rules CreateCommittedResource shares, and the output and interface. Run on a device with the GPU
// address arena (without it every heap is E_NOTIMPL). Returns the number of mismatches, or -1 without a host.
int compare_placed_reserved(ID3D12Device* ours) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
struct Case {
const char* name;
InvalidCase call;
bool (*comparable)(ID3D12Device*) = nullptr;
bool windows10_refuses = false;
};
static const Case cases[] = {
// The standalone device has acceleration structures (XDXR, or KHR with LEONIDA_RT_AS=khr) and accepts a UAV
// buffer in their state, as a host with ray tracing does.
{"committed (standalone): UAV buffer in RAYTRACING_ACCELERATION_STRUCTURE state", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
return committed(d, {D3D12_HEAP_TYPE_DEFAULT}, desc,
D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE);
}, host_ray_tracing},
{"placed: UAV buffer in RAYTRACING_ACCELERATION_STRUCTURE state", [](ID3D12Device* d) {
PlacedArgs a;
a.state = D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE;
auto desc = buffer_desc(64 * 1024);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
return placed(d, desc, a);
}, host_ray_tracing},
// CreatePlacedResource: arguments.
{"placed: buffer at 0", [](ID3D12Device* d) { return placed(d, buffer_desc(64 * 1024)); }},
{"placed: null description", [](ID3D12Device* d) { return placed(d, nullptr); }},
{"placed: null heap", [](ID3D12Device* d) {
const D3D12_RESOURCE_DESC desc = buffer_desc(64 * 1024);
void* out = nullptr;
return released(d->CreatePlacedResource(nullptr, 0, &desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
__uuidof(ID3D12Resource), &out), &out);
}},
{"placed: null output (capability check)", [](ID3D12Device* d) {
PlacedArgs a;
a.output = false;
return placed(d, buffer_desc(64 * 1024), a);
}},
{"placed: null output, offset 4096", [](ID3D12Device* d) {
PlacedArgs a;
a.output = false;
a.offset = 4096;
return placed(d, buffer_desc(64 * 1024), a);
}},
{"placed: null output, buffer height 2", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Height = 2;
PlacedArgs a;
a.output = false;
return placed(d, desc, a);
}},
{"placed: null output, texture past the heap's end", [](ID3D12Device* d) {
PlacedArgs a;
a.output = false;
a.offset = 1024 * 1024 - 64 * 1024;
return placed(d, texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE), a);
}},
{"placed: wrong interface", [](ID3D12Device* d) {
PlacedArgs a;
a.riid = &__uuidof(ID3D12Fence);
return placed(d, buffer_desc(64 * 1024), a);
}},
// CreatePlacedResource: where the resource goes.
{"placed: buffer at 64 KiB", [](ID3D12Device* d) {
PlacedArgs a;
a.offset = 64 * 1024;
return placed(d, buffer_desc(64 * 1024), a);
}},
{"placed: buffer at 4096", [](ID3D12Device* d) {
PlacedArgs a;
a.offset = 4096;
return placed(d, buffer_desc(256), a);
}},
{"placed: buffer filling the heap", [](ID3D12Device* d) { return placed(d, buffer_desc(1024 * 1024)); }},
{"placed: buffer one byte larger than the heap", [](ID3D12Device* d) {
return placed(d, buffer_desc(1024 * 1024 + 1));
}},
{"placed: buffer past the heap's end", [](ID3D12Device* d) {
PlacedArgs a;
a.offset = 1024 * 1024 - 64 * 1024;
return placed(d, buffer_desc(128 * 1024), a);
}},
{"placed: buffer at the heap's end", [](ID3D12Device* d) {
PlacedArgs a;
a.offset = 1024 * 1024;
return placed(d, buffer_desc(256), a);
}},
{"placed: small texture at 4096 (4 KiB alignment)", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.Alignment = D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT;
PlacedArgs a;
a.offset = 4096;
return placed(d, desc, a);
}},
{"placed: texture at 4096 (default alignment)", [](ID3D12Device* d) {
PlacedArgs a;
a.offset = 4096;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE), a);
}},
{"placed: texture at 64 KiB", [](ID3D12Device* d) {
PlacedArgs a;
a.offset = 64 * 1024;
return placed(d, texture_desc(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE), a);
}},
{"placed: texture larger than the heap", [](ID3D12Device* d) {
return placed(d, texture_desc(1024, 1024, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE));
}},
{"placed: texture past the heap's end", [](ID3D12Device* d) {
PlacedArgs a;
a.offset = 1024 * 1024 - 64 * 1024;
return placed(d, texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE), a);
}},
{"placed: MSAA texture at 64 KiB (4 MiB alignment)", [](ID3D12Device* d) {
auto desc = texture_desc(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.SampleDesc.Count = 4;
PlacedArgs a;
a.heap = heap_desc_of(D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT, {D3D12_HEAP_TYPE_DEFAULT},
D3D12_HEAP_FLAG_NONE, 8 * 1024 * 1024);
a.offset = 64 * 1024;
return placed(d, desc, a);
}},
{"placed: MSAA texture in a 64 KiB-aligned heap", [](ID3D12Device* d) {
auto desc = texture_desc(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
desc.SampleDesc.Count = 4;
return placed(d, desc);
}},
// CreatePlacedResource: the heap's type and DENY flags against the resource.
{"placed: buffer in an ALLOW_ONLY_NON_RT_DS_TEXTURES heap", [](ID3D12Device* d) {
PlacedArgs a;
a.heap.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES;
return placed(d, buffer_desc(256), a);
}},
{"placed: texture in an ALLOW_ONLY_BUFFERS heap", [](ID3D12Device* d) {
PlacedArgs a;
a.heap.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE), a);
}},
{"placed: render target in an ALLOW_ONLY_NON_RT_DS_TEXTURES heap", [](ID3D12Device* d) {
PlacedArgs a;
a.heap.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET), a);
}},
{"placed: plain texture in an ALLOW_ONLY_RT_DS_TEXTURES heap", [](ID3D12Device* d) {
PlacedArgs a;
a.heap.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE), a);
}},
{"placed: render target in an ALLOW_ONLY_RT_DS_TEXTURES heap", [](ID3D12Device* d) {
PlacedArgs a;
a.heap.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET), a);
}},
{"placed: texture in an upload heap", [](ID3D12Device* d) {
PlacedArgs a;
a.heap.Properties.Type = D3D12_HEAP_TYPE_UPLOAD;
a.state = D3D12_RESOURCE_STATE_GENERIC_READ;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE), a);
}},
// CreatePlacedResource: the description rules CreateCommittedResource has.
{"placed: buffer width 0", [](ID3D12Device* d) { return placed(d, buffer_desc(0)); }},
{"placed: buffer height 2", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Height = 2;
return placed(d, desc);
}},
{"placed: buffer with 2 mips", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.MipLevels = 2;
return placed(d, desc);
}},
{"placed: buffer with a format", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
return placed(d, desc);
}},
{"placed: buffer with layout UNKNOWN", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
return placed(d, desc);
}},
{"placed: buffer with 4 KiB alignment", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Alignment = D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT;
return placed(d, desc);
}},
{"placed: buffer with ALLOW_DEPTH_STENCIL", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
return placed(d, desc);
}},
{"placed: texture width 0", [](ID3D12Device* d) {
return placed(d, texture_desc(0, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE));
}},
{"placed: texture with format UNKNOWN", [](ID3D12Device* d) {
return placed(d, texture_desc(16, 16, DXGI_FORMAT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE));
}},
{"placed: texture with layout ROW_MAJOR", [](ID3D12Device* d) {
auto desc = texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
return placed(d, desc);
}},
{"placed: depth texture with ALLOW_UNORDERED_ACCESS", [](ID3D12Device* d) {
return placed(d, texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL |
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS));
}},
// CreatePlacedResource: the initial state and the clear value.
{"placed: upload buffer in GENERIC_READ", [](ID3D12Device* d) {
PlacedArgs a;
a.heap.Properties.Type = D3D12_HEAP_TYPE_UPLOAD;
a.state = D3D12_RESOURCE_STATE_GENERIC_READ;
return placed(d, buffer_desc(256), a);
}},
{"placed: upload buffer in COPY_DEST", [](ID3D12Device* d) {
PlacedArgs a;
a.heap.Properties.Type = D3D12_HEAP_TYPE_UPLOAD;
a.state = D3D12_RESOURCE_STATE_COPY_DEST;
return placed(d, buffer_desc(256), a);
}, nullptr, true},
{"placed: upload buffer with ALLOW_UNORDERED_ACCESS", [](ID3D12Device* d) {
auto desc = buffer_desc(256);
desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
PlacedArgs a;
a.heap.Properties.Type = D3D12_HEAP_TYPE_UPLOAD;
a.state = D3D12_RESOURCE_STATE_GENERIC_READ;
return placed(d, desc, a);
}},
{"placed: readback buffer in COPY_DEST", [](ID3D12Device* d) {
PlacedArgs a;
a.heap.Properties.Type = D3D12_HEAP_TYPE_READBACK;
a.state = D3D12_RESOURCE_STATE_COPY_DEST;
return placed(d, buffer_desc(256), a);
}},
{"placed: readback buffer in GENERIC_READ", [](ID3D12Device* d) {
PlacedArgs a;
a.heap.Properties.Type = D3D12_HEAP_TYPE_READBACK;
a.state = D3D12_RESOURCE_STATE_GENERIC_READ;
return placed(d, buffer_desc(256), a);
}, nullptr, true},
{"placed: default buffer in state 0x80000000", [](ID3D12Device* d) {
PlacedArgs a;
a.state = D3D12_RESOURCE_STATES(0x80000000u);
return placed(d, buffer_desc(256), a);
}, nullptr, true},
{"placed: depth texture in DEPTH_WRITE", [](ID3D12Device* d) {
PlacedArgs a;
a.state = D3D12_RESOURCE_STATE_DEPTH_WRITE;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_D32_FLOAT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
a);
}},
{"placed: plain texture in UNORDERED_ACCESS", [](ID3D12Device* d) {
PlacedArgs a;
a.state = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE), a);
}},
{"placed: render target in RENDER_TARGET", [](ID3D12Device* d) {
PlacedArgs a;
a.state = D3D12_RESOURCE_STATE_RENDER_TARGET;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET), a);
}},
{"placed: plain texture in RENDER_TARGET", [](ID3D12Device* d) {
PlacedArgs a;
a.state = D3D12_RESOURCE_STATE_RENDER_TARGET;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE), a);
}},
{"placed: render target with a clear value", [](ID3D12Device* d) {
static const D3D12_CLEAR_VALUE clear{DXGI_FORMAT_R8G8B8A8_UNORM, {0.f, 0.f, 0.f, 1.f}};
PlacedArgs a;
a.clear = &clear;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET), a);
}},
{"placed: plain texture with a clear value", [](ID3D12Device* d) {
static const D3D12_CLEAR_VALUE clear{DXGI_FORMAT_R8G8B8A8_UNORM, {0.f, 0.f, 0.f, 1.f}};
PlacedArgs a;
a.clear = &clear;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE), a);
}},
{"placed: buffer with a clear value", [](ID3D12Device* d) {
static const D3D12_CLEAR_VALUE clear{DXGI_FORMAT_R8G8B8A8_UNORM, {0.f, 0.f, 0.f, 1.f}};
PlacedArgs a;
a.clear = &clear;
return placed(d, buffer_desc(256), a);
}},
{"placed: render target, clear value of another format", [](ID3D12Device* d) {
static const D3D12_CLEAR_VALUE clear{DXGI_FORMAT_R16G16B16A16_FLOAT, {0.f, 0.f, 0.f, 1.f}};
PlacedArgs a;
a.clear = &clear;
return placed(d, texture_desc(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET), a);
}},
// CreateReservedResource.
{"reserved: buffer", [](ID3D12Device* d) { return reserved(d, buffer_desc(64 * 1024)); }},
{"reserved: null description", [](ID3D12Device* d) { return reserved(d, nullptr); }},
{"reserved: null output (capability check)", [](ID3D12Device* d) {
const auto desc = buffer_desc(64 * 1024);
return reserved(d, &desc, D3D12_RESOURCE_STATE_COMMON, nullptr, false);
}},
{"reserved: null output, buffer height 2", [](ID3D12Device* d) {
auto desc = buffer_desc(64 * 1024);
desc.Height = 2;
return reserved(d, &desc, D3D12_RESOURCE_STATE_COMMON, nullptr, false);
}},
{"reserved: null output, plain texture in RENDER_TARGET", [](ID3D12Device* d) {
const auto desc =
tiled(texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE));
return reserved(d, &desc, D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, false);
}},
{"reserved: wrong interface", [](ID3D12Device* d) {
const auto desc = buffer_desc(64 * 1024);
return reserved(d, &desc, D3D12_RESOURCE_STATE_COMMON, nullptr, true, __uuidof(ID3D12Fence));
}},
{"reserved: buffer with layout UNKNOWN", [](ID3D12Device* d) {
auto desc = buffer_desc(64 * 1024);
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
return reserved(d, desc);
}},
{"reserved: buffer width 0", [](ID3D12Device* d) { return reserved(d, buffer_desc(0)); }},
{"reserved: buffer height 2", [](ID3D12Device* d) {
auto desc = buffer_desc(64 * 1024);
desc.Height = 2;
return reserved(d, desc);
}},
{"reserved: buffer with a format", [](ID3D12Device* d) {
auto desc = buffer_desc(64 * 1024);
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
return reserved(d, desc);
}},
{"reserved: buffer with 4 KiB alignment", [](ID3D12Device* d) {
auto desc = buffer_desc(64 * 1024);
desc.Alignment = D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT;
return reserved(d, desc);
}},
{"reserved: buffer in COPY_DEST", [](ID3D12Device* d) {
return reserved(d, buffer_desc(64 * 1024), D3D12_RESOURCE_STATE_COPY_DEST);
}},
{"reserved: buffer in state 0x80000000", [](ID3D12Device* d) {
return reserved(d, buffer_desc(64 * 1024), D3D12_RESOURCE_STATES(0x80000000u));
}, nullptr, true},
{"reserved: texture", [](ID3D12Device* d) {
return reserved(d, tiled(texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE)));
}},
{"reserved: texture with layout UNKNOWN", [](ID3D12Device* d) {
return reserved(d, texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE));
}},
{"reserved: texture with layout ROW_MAJOR", [](ID3D12Device* d) {
auto desc = texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
return reserved(d, desc);
}},
{"reserved: texture with 64KB_STANDARD_SWIZZLE", [](ID3D12Device* d) {
auto desc = texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
desc.Layout = D3D12_TEXTURE_LAYOUT_64KB_STANDARD_SWIZZLE;
return reserved(d, desc);
}},
{"reserved: texture width 0", [](ID3D12Device* d) {
return reserved(d, tiled(texture_desc(0, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE)));
}},
{"reserved: full mip chain", [](ID3D12Device* d) {
return reserved(d, tiled(texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE,
0)));
}},
{"reserved: BC1 texture", [](ID3D12Device* d) {
return reserved(d, tiled(texture_desc(256, 256, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE)));
}},
{"reserved: 1D texture", [](ID3D12Device* d) {
auto desc = tiled(texture_desc(256, 1, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE));
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE1D;
return reserved(d, desc);
}},
{"reserved: D24S8 depth texture", [](ID3D12Device* d) {
return reserved(d, tiled(texture_desc(256, 256, DXGI_FORMAT_D24_UNORM_S8_UINT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)));
}},
{"reserved: D32 depth texture", [](ID3D12Device* d) {
return reserved(d, tiled(texture_desc(256, 256, DXGI_FORMAT_D32_FLOAT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)));
}},
{"reserved: R32G32B32 texture", [](ID3D12Device* d) {
return reserved(d, tiled(texture_desc(256, 256, DXGI_FORMAT_R32G32B32_FLOAT, D3D12_RESOURCE_FLAG_NONE)));
}},
{"reserved: texture with ALLOW_SIMULTANEOUS_ACCESS", [](ID3D12Device* d) {
return reserved(d, tiled(texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS)));
}},
{"reserved: render target with a clear value", [](ID3D12Device* d) {
static const D3D12_CLEAR_VALUE clear{DXGI_FORMAT_R8G8B8A8_UNORM, {0.f, 0.f, 0.f, 1.f}};
return reserved(d, tiled(texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)),
D3D12_RESOURCE_STATE_COMMON, &clear);
}},
{"reserved: plain texture with a clear value", [](ID3D12Device* d) {
static const D3D12_CLEAR_VALUE clear{DXGI_FORMAT_R8G8B8A8_UNORM, {0.f, 0.f, 0.f, 1.f}};
return reserved(d, tiled(texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE)),
D3D12_RESOURCE_STATE_COMMON, &clear);
}},
{"reserved: plain texture in RENDER_TARGET", [](ID3D12Device* d) {
return reserved(d, tiled(texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE)),
D3D12_RESOURCE_STATE_RENDER_TARGET);
}},
{"reserved: 3D texture", [](ID3D12Device* d) {
auto desc = tiled(texture_desc(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 1, 16));
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
return reserved(d, desc);
}},
{"reserved: 4x MSAA render target", [](ID3D12Device* d) {
auto desc = tiled(texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET));
desc.SampleDesc.Count = 4;
return reserved(d, desc);
}},
};
const bool verbose = GetEnvironmentVariableA("LEONIDA_D3D49_VERBOSE", nullptr, 0) > 0;
int mismatches = 0;
for (const Case& c : cases) {
if (verbose) {
std::printf("invalid arguments %s:", c.name);
std::fflush(stdout);
}
const HRESULT a = call_guarded(c.call, ours), b = call_guarded(c.call, host.p);
if (verbose) std::printf(" ours 0x%08lx host 0x%08lx\n", a, b);
if (a == b) continue;
if (c.windows10_refuses && windows10_refusal(c.name, host.p, a, b)) continue;
if (c.comparable && !c.comparable(host.p)) {
std::printf("invalid arguments %s: not comparable on this host (ours 0x%08lx, host 0x%08lx)\n", c.name,
a, b);
continue;
}
++mismatches;
std::fprintf(stderr, "invalid arguments %s: ours 0x%08lx, host 0x%08lx\n", c.name, a, b);
}
return mismatches;
}
// CheckFeatureSupport's error paths must match the host runtime's exactly: games and middleware branch on these
// HRESULTs at startup, and a wrong success or a wrong error sends them down the wrong path. A null data pointer, a
// structure smaller than the feature requires, or a feature value no runtime defines must all return the same HRESULT
// on both sides. Valid queries are not compared here: their answers are the GPU's choice. Returns the number of
// mismatches, or -1 when the host has no D3D12 device to compare with.
int compare_check_feature_support(ID3D12Device* ours) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
// Features the shim answers; their error paths must match the host's.
const D3D12_FEATURE answered[] = {
D3D12_FEATURE_FEATURE_LEVELS, D3D12_FEATURE_ARCHITECTURE, D3D12_FEATURE_ARCHITECTURE1,
D3D12_FEATURE_D3D12_OPTIONS, D3D12_FEATURE_GPU_VIRTUAL_ADDRESS_SUPPORT, D3D12_FEATURE_FORMAT_SUPPORT,
};
// Feature values no runtime defines (well beyond D3D12_FEATURE's range); both sides must reject them the same way.
const D3D12_FEATURE undefined[] = {D3D12_FEATURE(0x4000), D3D12_FEATURE(0x7fffffff)};
int mismatches = 0;
const auto check = [&](const char* what, D3D12_FEATURE feature, void* data, UINT size) {
const HRESULT a = ours->CheckFeatureSupport(feature, data, size);
const HRESULT b = host->CheckFeatureSupport(feature, data, size);
if (a == b) return;
if (mismatches++ < 24)
std::fprintf(stderr, "CheckFeatureSupport %s feature %d size %u: ours 0x%08x host 0x%08x\n", what,
int(feature), size, static_cast<unsigned>(a), static_cast<unsigned>(b));
};
for (const D3D12_FEATURE f : answered) {
std::uint8_t buf[256] = {};
check("null data", f, nullptr, sizeof(buf));
check("zero size", f, buf, 0);
check("one byte", f, buf, 1);
}
// A structure one byte short, for every answered feature's data type.
{
D3D12_FEATURE_DATA_FEATURE_LEVELS fl{};
check("feature levels short", D3D12_FEATURE_FEATURE_LEVELS, &fl, sizeof(fl) - 1);
D3D12_FEATURE_DATA_ARCHITECTURE ar{};
check("architecture short", D3D12_FEATURE_ARCHITECTURE, &ar, sizeof(ar) - 1);
D3D12_FEATURE_DATA_ARCHITECTURE1 a1{};
check("architecture1 short", D3D12_FEATURE_ARCHITECTURE1, &a1, sizeof(a1) - 1);
D3D12_FEATURE_DATA_D3D12_OPTIONS op{};
check("options short", D3D12_FEATURE_D3D12_OPTIONS, &op, sizeof(op) - 1);
D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT va{};
check("gpu va short", D3D12_FEATURE_GPU_VIRTUAL_ADDRESS_SUPPORT, &va, sizeof(va) - 1);
D3D12_FEATURE_DATA_FORMAT_SUPPORT fs{};
check("format short", D3D12_FEATURE_FORMAT_SUPPORT, &fs, sizeof(fs) - 1);
}
for (const D3D12_FEATURE f : undefined) {
std::uint8_t buf[256] = {};
check("undefined feature", f, buf, sizeof(buf));
}
return mismatches;
}
// Fences and queues must follow the host runtime's semantics: Signal raises the completed value and a null event set
// for a reached value blocks until then. Runs the same sequence on ours and the host and compares the completed value
// and the HRESULT at each step. A signal of an already-passed (lower) value is a documented deviation: the shim drops
// it and stays monotonic because a Vulkan timeline cannot go back, so that case is checked against the documented
// behaviour rather than the host. Returns the number of mismatches, or -1 when the host has no D3D12 device.
int compare_fences(ID3D12Device* ours) {
Ref<ID3D12Device> host;
if (!create_host_device(host)) return -1;
D3D12_COMMAND_QUEUE_DESC queue_desc{};
queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
Ref<ID3D12CommandQueue> oq, hq;
Ref<ID3D12Fence> of, hf;
if (FAILED(ours->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&oq.p))) ||
FAILED(host->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&hq.p))) ||
FAILED(ours->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&of.p))) ||
FAILED(host->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&hf.p))))
return -1;
int mismatches = 0;
const auto check = [&](const char* what, UINT64 a, UINT64 b) {
if (a != b && mismatches++ < 24)
std::fprintf(stderr, "fence %s: ours %llu host %llu\n", what, static_cast<unsigned long long>(a),
static_cast<unsigned long long>(b));
};
const auto check_hr = [&](const char* what, HRESULT a, HRESULT b) {
if (a != b && mismatches++ < 24)
std::fprintf(stderr, "fence %s: ours 0x%08x host 0x%08x\n", what, static_cast<unsigned>(a),
static_cast<unsigned>(b));
};
// A signal raises the completed value; a null event blocks until it is reached.
check_hr("signal 5", oq->Signal(of.p, 5), hq->Signal(hf.p, 5));
check_hr("block to 5", of->SetEventOnCompletion(5, nullptr), hf->SetEventOnCompletion(5, nullptr));
check("completed at 5", of->GetCompletedValue(), hf->GetCompletedValue());
// A higher signal raises it again.
check_hr("signal 9", oq->Signal(of.p, 9), hq->Signal(hf.p, 9));
check_hr("block to 9", of->SetEventOnCompletion(9, nullptr), hf->SetEventOnCompletion(9, nullptr));
check("completed at 9", of->GetCompletedValue(), hf->GetCompletedValue());
// An event for an already-reached value returns at once.
check_hr("event at reached 9", of->SetEventOnCompletion(9, nullptr), hf->SetEventOnCompletion(9, nullptr));
// A signal of an already-passed (lower) value: the host is free to lower the fence on some drivers, but a Vulkan
// timeline only increases. The shim deliberately drops it and keeps the higher value (see Fence::claim in
// d3d12_com.cpp), so this is checked against the documented behaviour, not the host.
check_hr("signal 3 (lower)", oq->Signal(of.p, 3), S_OK);
if (of->GetCompletedValue() != 9 && mismatches++ < 24)
std::fprintf(stderr, "fence lower signal: ours %llu, want 9\n",
static_cast<unsigned long long>(of->GetCompletedValue()));
return mismatches;
}
} // namespace
int main() {
vktest::Device d;
const auto status = d.initialize();
if (status == vktest::DeviceStatus::unavailable) return 77;
if (status == vktest::DeviceStatus::failed) return 1;
{
// The host comparisons (create_host_device, the copies) run on the layer's own adapter, not D3D12's default
// one, which on a PC with two GPUs can be the other GPU (copycmp::layer_adapter).
VkPhysicalDeviceIDProperties id{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES};
VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &id};
d.instance_fn<PFN_vkGetPhysicalDeviceProperties2>("vkGetPhysicalDeviceProperties2")(d.physical, &properties);
if (id.deviceLUIDValid) {
LUID luid{};
std::memcpy(&luid, id.deviceLUID, sizeof(luid));
copycmp::layer_adapter() = luid;
}
}
EXPECT(d.depth_format_usable(VK_FORMAT_D32_SFLOAT_S8_UINT));
VkSampleCountFlags depth_sample_counts = 0, color_sample_counts = 0;
{
VkPhysicalDeviceProperties properties{};
d.instance_fn<PFN_vkGetPhysicalDeviceProperties>("vkGetPhysicalDeviceProperties")(d.physical, &properties);
depth_sample_counts =
properties.limits.framebufferDepthSampleCounts & properties.limits.framebufferStencilSampleCounts;
color_sample_counts = properties.limits.framebufferColorSampleCounts;
}
constexpr UINT size = 8, small_size = 4;
unsigned mismatches = 0;
const auto report = [&](const char* what, UINT index, const std::uint8_t* got, const std::uint8_t* want, size_t n) {
if (std::memcmp(got, want, n) == 0) return;
if (mismatches++ < 8) {
std::fprintf(stderr, "%s %u: got", what, index);
for (size_t i = 0; i < n; ++i) std::fprintf(stderr, " %02x", got[i]);
std::fprintf(stderr, ", want");
for (size_t i = 0; i < n; ++i) std::fprintf(stderr, " %02x", want[i]);
std::fprintf(stderr, "\n");
}
};
{
rt::d3d12::VulkanDeviceInfo info{};
info.get_instance_proc = d.instance_proc;
info.instance = d.instance;
info.physical = d.physical;
info.device = d.device;
info.queue = d.queue;
info.queue_family = d.family;
info.conditional_rendering = d.conditional_rendering; // SetPredication (the D24 tests check copies under it)
info.texture_compression_bc = d.bc;
info.occlusion_query_precise = d.occlusion_query_precise;
info.pipeline_statistics_query = d.pipeline_statistics_query;
info.conditional_rendering = d.conditional_rendering;
ID3D12Device* inner = nullptr;
EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&inner))));
Ref<ID3D12Device> device;
EXPECT(SUCCEEDED(rt::d3d12_xs::create_device_proxy(inner, __uuidof(ID3D12Device),
reinterpret_cast<void**>(&device.p), /*console_lists=*/false)));
rt::d3d12::set_device_outer(inner, device.p); // the proxy now owns `inner`
EXPECT(rt::d3d12_xs::is_device_proxy(device.p));
EXPECT(rt::d3d12_xs::device_proxy_slot_count(device.p) == 80); // ID3D12Device11
EXPECT(check_plain_resolve_formats(device.p));
{
Ref<ID3D12Device11> device11;
Ref<IUnknown> device12;
EXPECT(SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&device11.p))) && device11.p == device.p);
EXPECT(device->QueryInterface(__uuidof(ID3D12Device12), reinterpret_cast<void**>(&device12.p)) ==
E_NOINTERFACE);
}
EXPECT(device->GetNodeCount() == 1);
// _COM_Outptr_: a failed slot 14 leaves NULL in the caller's output, as the inner device does.
{
// An unknown flag (zero descriptors are valid, as on the host runtime).
D3D12_DESCRIPTOR_HEAP_DESC bad{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 4,
D3D12_DESCRIPTOR_HEAP_FLAGS(0x80)};
void* inner_out = reinterpret_cast<void*>(0x1111);
EXPECT(inner->CreateDescriptorHeap(&bad, __uuidof(ID3D12DescriptorHeap), &inner_out) == E_INVALIDARG &&
inner_out == nullptr);
void* proxy_out = reinterpret_cast<void*>(0x1111);
EXPECT(device->CreateDescriptorHeap(&bad, __uuidof(ID3D12DescriptorHeap), &proxy_out) ==
E_INVALIDARG);
EXPECT(proxy_out == nullptr);
}
// BC1 can be sampled, but it cannot be an attachment or a storage image.
if (d.bc) {
const D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_DEFAULT};
Ref<ID3D12Device> host;
create_host_device(host);
for (const auto flags : {D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET,
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS}) {
const auto desc = texture_desc(8, 8, DXGI_FORMAT_BC1_UNORM, flags);
Ref<ID3D12Resource> image;
const auto create = [&](ID3D12Device* dev, Ref<ID3D12Resource>& out) {
return dev->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON,
nullptr, IID_PPV_ARGS(&out.p));
};
const HRESULT hr = create(device.p, image);
const bool supported = flags == D3D12_RESOURCE_FLAG_NONE;
EXPECT(hr == (supported ? S_OK : E_INVALIDARG));
EXPECT((image.p != nullptr) == supported);
if (host.p) {
Ref<ID3D12Resource> native;
EXPECT(create(host.p, native) == hr);
EXPECT((native.p != nullptr) == supported);
}
const auto allocation = device->GetResourceAllocationInfo(0, 1, &desc);
EXPECT((allocation.SizeInBytes != UINT64_MAX) == supported);
}
}
const D3D_FEATURE_LEVEL levels[] = {D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_12_1, D3D_FEATURE_LEVEL_12_2};
D3D12_FEATURE_DATA_FEATURE_LEVELS feature_levels{3, levels, D3D_FEATURE_LEVEL(0)};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS, &feature_levels,
sizeof(feature_levels))));
EXPECT(feature_levels.MaxSupportedFeatureLevel == D3D_FEATURE_LEVEL_12_1);
// An empty or missing list is E_INVALIDARG on the host (NVIDIA and WARP), not DXGI_ERROR_UNSUPPORTED or a
// read through null; a list the device can't meet answers DXGI_ERROR_UNSUPPORTED and writes level 0.
D3D12_FEATURE_DATA_FEATURE_LEVELS no_levels{0, levels, D3D_FEATURE_LEVEL_11_0};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS, &no_levels, sizeof(no_levels)) == E_INVALIDARG);
D3D12_FEATURE_DATA_FEATURE_LEVELS null_levels{1, nullptr, D3D_FEATURE_LEVEL_11_0};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS, &null_levels, sizeof(null_levels)) ==
E_INVALIDARG);
D3D12_FEATURE_DATA_FEATURE_LEVELS too_high{1, &levels[2], D3D_FEATURE_LEVEL_11_0};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS, &too_high, sizeof(too_high)) ==
DXGI_ERROR_UNSUPPORTED);
EXPECT(too_high.MaxSupportedFeatureLevel == D3D_FEATURE_LEVEL(0));
D3D12_FEATURE_DATA_ARCHITECTURE architecture{};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_ARCHITECTURE, &architecture, sizeof(architecture))));
D3D12_FEATURE_DATA_D3D12_OPTIONS options{};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options))));
EXPECT(options.ResourceBindingTier == D3D12_RESOURCE_BINDING_TIER_3);
EXPECT(options.MaxGPUVirtualAddressBitsPerResource == 40);
// Console 0x40-byte (16-dword) D3D12_OPTIONS query (docs/d3d12_xs_surface.md §7.1): 0x3C PC prefix + zero tail.
UINT console_opts[16]{};
console_opts[15] = 0xdeadbeefu;
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, console_opts, sizeof(console_opts))));
EXPECT(std::memcmp(console_opts, &options, sizeof(options)) == 0);
EXPECT(console_opts[15] == 0);
D3D12_FEATURE_DATA_GPU_VIRTUAL_ADDRESS_SUPPORT va_support{};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_GPU_VIRTUAL_ADDRESS_SUPPORT, &va_support, sizeof(va_support))));
EXPECT(va_support.MaxGPUVirtualAddressBitsPerResource == 40);
EXPECT(va_support.MaxGPUVirtualAddressBitsPerProcess == 48);
EXPECT(compare_format_info(device.p, d) == 0);
// Use a real oversized buffer and check that rejecting it leaves the output untouched.
D3D12_FEATURE_DATA_FORMAT_INFO format_info[2]{{DXGI_FORMAT_D24_UNORM_S8_UINT, 0xff}};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, nullptr, sizeof(format_info[0])) == E_INVALIDARG);
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, format_info, sizeof(format_info)) == E_INVALIDARG);
EXPECT(format_info[0].PlaneCount == 0xff);
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, format_info, sizeof(format_info[0])) == S_OK);
EXPECT(format_info[0].PlaneCount == 2);
D3D12_FEATURE_DATA_FORMAT_SUPPORT format_support{DXGI_FORMAT_R8G8B8A8_UNORM};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &format_support, sizeof(format_support))));
EXPECT(format_support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D);
EXPECT(format_support.Support1 & D3D12_FORMAT_SUPPORT1_RENDER_TARGET);
EXPECT(format_support.Support1 & D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE);
// RTX 5070 / 621.14 host answers, without requiring a host device for this regression.
for (const UINT samples : {0u, 33u, 64u, UINT_MAX}) {
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS quality{DXGI_FORMAT_R8G8B8A8_UNORM, samples,
D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE, 99};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &quality,
sizeof(quality)) == E_FAIL);
EXPECT(quality.NumQualityLevels == 0);
}
for (UINT format = 0; format <= 192; ++format) {
const bool known = format != UINT(DXGI_FORMAT_R1_UNORM) &&
(format <= 115 || (format >= 189 && format <= 191));
for (const UINT samples : {1u, 3u, 31u}) {
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS quality{DXGI_FORMAT(format), samples,
D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE, 99};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &quality,
sizeof(quality)) == (known ? S_OK : E_FAIL));
EXPECT(quality.NumQualityLevels == (known ? UINT(samples == 1) : 99u));
}
}
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS invalid_format_levels{DXGI_FORMAT_FORCE_UINT, 1,
D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE, 99};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &invalid_format_levels,
sizeof(invalid_format_levels)) == E_FAIL);
EXPECT(invalid_format_levels.NumQualityLevels == 99);
// Native (RTX 5070 / 621.24): an unknown format keeps the output even with an invalid count.
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS unknown_zero{DXGI_FORMAT(192), 0,
D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE, 99};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &unknown_zero,
sizeof(unknown_zero)) == E_FAIL);
EXPECT(unknown_zero.NumQualityLevels == 99);
for (const auto format : {DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_BC1_UNORM, DXGI_FORMAT_R32G32B32_FLOAT}) {
for (const UINT samples : {1u, 3u, 4u, 32u}) {
for (const UINT flags : {0u, 1u}) {
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS plain{format, samples,
D3D12_MULTISAMPLE_QUALITY_LEVEL_FLAGS(flags)};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &plain,
sizeof(plain)) == S_OK);
if (flags && format == DXGI_FORMAT_R32G32B32_FLOAT) EXPECT(plain.NumQualityLevels == 0);
for (const UINT extra : {2u, 0xfeu}) {
auto undefined_flags = plain;
undefined_flags.Flags = D3D12_MULTISAMPLE_QUALITY_LEVEL_FLAGS(flags | extra);
undefined_flags.NumQualityLevels = 99;
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &undefined_flags,
sizeof(undefined_flags)) == S_OK);
EXPECT(undefined_flags.NumQualityLevels == plain.NumQualityLevels);
}
}
}
}
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS wrong_size[2]{{DXGI_FORMAT_R8G8B8A8_UNORM, 1,
D3D12_MULTISAMPLE_QUALITY_LEVELS_FLAG_NONE, 99}};
for (const UINT bytes : {UINT(sizeof(wrong_size[0]) - 1), UINT(sizeof(wrong_size))}) {
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, wrong_size,
bytes) == E_INVALIDARG);
EXPECT(wrong_size[0].NumQualityLevels == 99);
}
D3D12_RESOURCE_DESC unsupported_quality =
texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
unsupported_quality.SampleDesc.Count = 4;
unsupported_quality.SampleDesc.Quality = 1;
EXPECT(device->GetResourceAllocationInfo(0, 1, &unsupported_quality).SizeInBytes == UINT64_MAX);
// UNKNOWN is a buffer format to the host runtime (S_OK, Support1 BUFFER; "format support: UNKNOWN" compares
// it), and a value past its format table is E_FAIL.
D3D12_FEATURE_DATA_FORMAT_SUPPORT unknown_format{DXGI_FORMAT_UNKNOWN};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &unknown_format, sizeof(unknown_format)) ==
S_OK &&
unknown_format.Support1 == D3D12_FORMAT_SUPPORT1_BUFFER);
D3D12_FEATURE_DATA_FORMAT_SUPPORT invalid_format{static_cast<DXGI_FORMAT>(116)};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &invalid_format, sizeof(invalid_format)) ==
E_FAIL);
// The table's last entries before and after the gap are formats, whatever this GPU supports of them.
for (const UINT edge : {115u, 189u, 191u}) { // B4G4R4A4_UNORM, SAMPLER_FEEDBACK_MIN_MIP_OPAQUE, A4B4G4R4_UNORM
D3D12_FEATURE_DATA_FORMAT_SUPPORT known{static_cast<DXGI_FORMAT>(edge)};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &known, sizeof(known)) != E_FAIL);
}
// D3D-61: XR_BIAS is a display-only format with the R10G10B10A2 bit layout. The layer can create and
// copy it, and its views can cast to the ordinary 10:10:10:2 format, but it does not claim display or
// video-processor support.
{
const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT};
D3D12_FEATURE_DATA_FORMAT_SUPPORT xr{DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &xr, sizeof(xr))));
EXPECT(xr.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D);
EXPECT(xr.Support1 & D3D12_FORMAT_SUPPORT1_CAST_WITHIN_BIT_LAYOUT);
EXPECT(!(xr.Support1 & (D3D12_FORMAT_SUPPORT1_TEXTURE3D | D3D12_FORMAT_SUPPORT1_MIP |
D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE | D3D12_FORMAT_SUPPORT1_RENDER_TARGET |
D3D12_FORMAT_SUPPORT1_BLENDABLE)));
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS multsample{DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM, 4};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &multsample,
sizeof(multsample))));
EXPECT(multsample.NumQualityLevels == 0);
Ref<ID3D12Resource> tex;
const D3D12_RESOURCE_DESC desc = texture_desc(
16, 16, DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM, D3D12_RESOURCE_FLAG_NONE, 1);
EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &desc,
D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&tex.p))));
EXPECT(tex.p != nullptr);
// The host refuses extra mips, multisampling and 3D; the layer must refuse the same descriptions.
D3D12_RESOURCE_DESC invalid = desc;
invalid.MipLevels = 2;
EXPECT(FAILED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &invalid,
D3D12_RESOURCE_STATE_COMMON, nullptr,
__uuidof(ID3D12Resource), nullptr)));
EXPECT(device->GetResourceAllocationInfo(0, 1, &invalid).SizeInBytes == UINT64_MAX);
D3D12_PLACED_SUBRESOURCE_FOOTPRINT layout{};
UINT rows = 0;
UINT64 total = 0;
device->GetCopyableFootprints(&invalid, 0, 1, 0, &layout, &rows, nullptr, &total);
EXPECT(total == UINT64_MAX);
invalid = desc;
invalid.SampleDesc.Count = 4;
invalid.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
EXPECT(FAILED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &invalid,
D3D12_RESOURCE_STATE_COMMON, nullptr,
__uuidof(ID3D12Resource), nullptr)));
EXPECT(device->GetResourceAllocationInfo(0, 1, &invalid).SizeInBytes == UINT64_MAX);
invalid = desc;
invalid.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
invalid.DepthOrArraySize = 4;
EXPECT(FAILED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &invalid,
D3D12_RESOURCE_STATE_COMMON, nullptr,
__uuidof(ID3D12Resource), nullptr)));
EXPECT(device->GetResourceAllocationInfo(0, 1, &invalid).SizeInBytes == UINT64_MAX);
}
// D3D-60: CheckFeatureSupport and committed resource creation for integer and typeless formats
{
const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT};
for (DXGI_FORMAT f : {
DXGI_FORMAT_R32G32_TYPELESS, DXGI_FORMAT_R32G32_UINT, DXGI_FORMAT_R32G32_SINT,
DXGI_FORMAT_R16G16B16A16_UINT, DXGI_FORMAT_R16G16B16A16_SINT,
DXGI_FORMAT_R10G10B10A2_UINT, DXGI_FORMAT_R16G16_TYPELESS, DXGI_FORMAT_R16G16_UINT,
DXGI_FORMAT_R16G16_SINT, DXGI_FORMAT_R8G8_TYPELESS, DXGI_FORMAT_R8G8_UINT,
DXGI_FORMAT_R8G8_SINT, DXGI_FORMAT_B8G8R8X8_TYPELESS, DXGI_FORMAT_B8G8R8X8_UNORM,
DXGI_FORMAT_B8G8R8X8_UNORM_SRGB}) {
D3D12_FEATURE_DATA_FORMAT_SUPPORT fs{f};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &fs, sizeof(fs))));
EXPECT(fs.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D);
Ref<ID3D12Resource> tex;
const D3D12_RESOURCE_DESC desc = texture_desc(16, 16, f, D3D12_RESOURCE_FLAG_NONE, 1);
EXPECT(SUCCEEDED(device->CreateCommittedResource(
&default_heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&tex.p))));
EXPECT(tex.p != nullptr);
}
}
// D3D-87, B8G8R8X8: no typed UAV and no typed texture that allows unordered access (the typeless family may),
// as the host runtime answers; render targets as for B8G8R8A8.
{
Ref<ID3D12Device> host;
if (create_host_device(host)) {
const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT};
for (DXGI_FORMAT f : {DXGI_FORMAT_B8G8R8X8_UNORM, DXGI_FORMAT_B8G8R8X8_UNORM_SRGB,
DXGI_FORMAT_B8G8R8X8_TYPELESS}) {
D3D12_FEATURE_DATA_FORMAT_SUPPORT a{f}, b{f};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &a, sizeof(a))));
EXPECT(SUCCEEDED(host->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &b, sizeof(b))));
constexpr UINT typed_uav = D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW;
constexpr UINT typed_uav2 = D3D12_FORMAT_SUPPORT2_UAV_TYPED_LOAD | D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE;
EXPECT((UINT(a.Support1) & typed_uav) == (UINT(b.Support1) & typed_uav));
EXPECT((UINT(a.Support2) & typed_uav2) == (UINT(b.Support2) & typed_uav2));
for (auto flag : {D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET}) {
const D3D12_RESOURCE_DESC desc = texture_desc(16, 16, f, flag, 1);
Ref<ID3D12Resource> ours_resource, host_resource;
const HRESULT ours_hr = device->CreateCommittedResource(
&default_heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&ours_resource.p));
const HRESULT host_hr = host->CreateCommittedResource(
&default_heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&host_resource.p));
if (SUCCEEDED(ours_hr) != SUCCEEDED(host_hr))
std::fprintf(stderr, "B8G8R8X8 format %d flags %d: ours %08lx, host %08lx\n", int(f),
int(flag), static_cast<unsigned long>(ours_hr),
static_cast<unsigned long>(host_hr));
EXPECT(SUCCEEDED(ours_hr) == SUCCEEDED(host_hr));
}
}
std::printf("B8G8R8X8: typed UAV support and UAV/RTV resource creation match the host runtime\n");
} else {
std::printf("B8G8R8X8: no host D3D12 device to compare with, skipped\n");
}
}
const int options_mismatches = compare_feature_options(device.p, info);
EXPECT(options_mismatches == 0);
// Uncompressed 8-bit sRGB formats support texture reads but cannot type a D3D12 buffer view.
for (DXGI_FORMAT format : {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, DXGI_FORMAT_B8G8R8A8_UNORM_SRGB,
DXGI_FORMAT_B8G8R8X8_UNORM_SRGB}) {
D3D12_FEATURE_DATA_FORMAT_SUPPORT support{format};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support))));
EXPECT(!(support.Support1 & D3D12_FORMAT_SUPPORT1_BUFFER));
EXPECT(support.Support1 & D3D12_FORMAT_SUPPORT1_SHADER_LOAD);
EXPECT(support.Support1 & D3D12_FORMAT_SUPPORT1_TEXTURE2D);
}
const int format_support_mismatches = compare_format_support(device.p, info);
EXPECT(format_support_mismatches == 0);
{
VkFormatProperties r4_props{};
d.instance_fn<PFN_vkGetPhysicalDeviceFormatProperties>("vkGetPhysicalDeviceFormatProperties")(
d.physical, VK_FORMAT_R4G4B4A4_UNORM_PACK16, &r4_props);
const bool r4g4b4a4 = (r4_props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) != 0;
// The PC-ABI layer device: the proxy returns heap starts by value.
EXPECT(new_format_round_trips(inner, info, r4g4b4a4) == 0);
}
// Push constants limit check (D3D-14): Vulkan spec guarantees >= 128 bytes.
EXPECT(rt::d3d12::device_max_push_constants_size(device.p) >= 128);
EXPECT(rt::d3d12::device_max_push_constants_size(inner) == rt::d3d12::device_max_push_constants_size(device.p));
// The test GPU may have either answer (Intel Arc and a Radeon 680M on driver 22.40.84.06 report 128 bytes);
// the device must flag the fallback exactly when the 144-byte block does not fit.
EXPECT(rt::d3d12::device_push_constants_need_fallback(device.p) ==
(rt::d3d12::device_max_push_constants_size(device.p) < 144));
// Simulated fallback path when maxPushConstantsSize < 144 bytes:
{
rt::d3d12::VulkanDeviceInfo fallback_info = info;
fallback_info.texture_compression_bc = false;
fallback_info.max_push_constants_size = 128;
fallback_info.push_constants_need_fallback = true;
ID3D12Device* fallback_inner = nullptr;
EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(fallback_info, IID_PPV_ARGS(&fallback_inner))));
Ref<ID3D12Device> fallback_device;
EXPECT(SUCCEEDED(rt::d3d12_xs::create_device_proxy(fallback_inner, __uuidof(ID3D12Device),
reinterpret_cast<void**>(&fallback_device.p), /*console_lists=*/false)));
rt::d3d12::set_device_outer(fallback_inner, fallback_device.p);
D3D12_FEATURE_DATA_D3D12_OPTIONS8 options8{TRUE};
EXPECT(fallback_device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS8, &options8,
sizeof(options8)) == S_OK);
EXPECT(options8.UnalignedBlockTexturesSupported == FALSE);
EXPECT(rt::d3d12::device_max_push_constants_size(fallback_device.p) == 128);
EXPECT(rt::d3d12::device_push_constants_need_fallback(fallback_device.p));
}
// The Windows 10 footprint skip depends on this classification; check it on every host.
EXPECT(uses_unaligned_blocks(texture_desc(10, 6, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE)));
EXPECT(!uses_unaligned_blocks(texture_desc(8, 8, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE)));
EXPECT(!uses_unaligned_blocks(texture_desc(10, 6, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE)));
// D3D12_OPTIONS1 (D3D-86): the lane range is the Vulkan device's own subgroup range and the device-wide lane
// count is deliberately one wave, so those are pinned here rather than only compared. Wave ops can only be
// promised by a GPU, and a software Vulkan device has no host GPU to compare with either.
{
D3D12_FEATURE_DATA_D3D12_OPTIONS1 wave{};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &wave, sizeof(wave))));
EXPECT(wave.WaveLaneCountMin >= 1);
EXPECT(wave.WaveLaneCountMax >= wave.WaveLaneCountMin);
EXPECT(wave.TotalLaneCount == wave.WaveLaneCountMax);
EXPECT(wave.ExpandedComputeResourceStates);
const int options1 = compare_options1(device.p, d);
if (options1 == -2)
std::printf("D3D12_OPTIONS1: software Vulkan device, host comparison skipped\n");
else if (options1 < 0)
std::printf("D3D12_OPTIONS1: no host D3D12 device to compare with, skipped\n");
EXPECT(options1 <= 0);
if (options1 != -2) EXPECT(wave.WaveOps);
D3D12_FEATURE_DATA_D3D12_OPTIONS1 shrunk{};
EXPECT(FAILED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &shrunk, sizeof(shrunk) - 4)));
EXPECT(FAILED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, nullptr, sizeof(shrunk))));
}
const int footprint_mismatches = compare_footprints(device.p);
if (footprint_mismatches < 0)
std::printf("GetCopyableFootprints: no host D3D12 device to compare with, skipped\n");
EXPECT(footprint_mismatches <= 0);
{
// D3D-122: the same refusals without a host device (CI's llvmpipe), so they can't regress unseen.
const auto cases = footprint_validity_cases();
for (size_t i = 0; i < cases.size(); ++i) {
UINT64 total = 0;
device->GetCopyableFootprints(&cases[i].first, 0, 1, 0, nullptr, nullptr, nullptr, &total);
if ((total != UINT64_MAX) != cases[i].second)
std::fprintf(stderr, "footprint validity case %zu: total %llu, expected %s\n", i,
(unsigned long long)total, cases[i].second ? "a footprint" : "the failure answer");
EXPECT((total != UINT64_MAX) == cases[i].second);
}
}
// Shader model and root signature (D3D-86): the answers are only useful if the game can rely on them, so
// pin both the ceiling and the refusal cases rather than just comparing with the host.
{
D3D12_FEATURE_DATA_SHADER_MODEL model{D3D_SHADER_MODEL_6_0};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &model, sizeof(model))));
EXPECT(model.HighestShaderModel == D3D_SHADER_MODEL_6_0);
// 5.1 is D3D12's baseline and the host answers it, so a caller asking for the minimum must get it back.
D3D12_FEATURE_DATA_SHADER_MODEL baseline{D3D_SHADER_MODEL_5_1};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &baseline, sizeof(baseline))));
EXPECT(baseline.HighestShaderModel == D3D_SHADER_MODEL_5_1);
D3D12_FEATURE_DATA_SHADER_MODEL above{static_cast<D3D_SHADER_MODEL>(0x69)};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &above, sizeof(above))));
EXPECT(above.HighestShaderModel == D3D_SHADER_MODEL_6_0); // clamped to what the layer accepts
D3D12_FEATURE_DATA_SHADER_MODEL below{static_cast<D3D_SHADER_MODEL>(0)};
EXPECT(FAILED(device->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &below, sizeof(below))));
// Between 5.1 and 6.0 no runtime names a model: refused (the host's E_INVALIDARG), not echoed back.
for (const UINT undefined : {0x52u, 0x5au, 0x5fu}) {
D3D12_FEATURE_DATA_SHADER_MODEL gap{static_cast<D3D_SHADER_MODEL>(undefined)};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &gap, sizeof(gap)) == E_INVALIDARG);
}
D3D12_FEATURE_DATA_ROOT_SIGNATURE signature{D3D_ROOT_SIGNATURE_VERSION_1_1};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &signature, sizeof(signature))));
EXPECT(signature.HighestVersion == D3D_ROOT_SIGNATURE_VERSION_1_1);
D3D12_FEATURE_DATA_ROOT_SIGNATURE newer{static_cast<D3D_ROOT_SIGNATURE_VERSION>(0x3)};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &newer, sizeof(newer))));
EXPECT(newer.HighestVersion == D3D_ROOT_SIGNATURE_VERSION_1_1);
D3D12_FEATURE_DATA_ROOT_SIGNATURE undefined{static_cast<D3D_ROOT_SIGNATURE_VERSION>(0)};
EXPECT(FAILED(device->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &undefined, sizeof(undefined))));
// Anything above 1.2 is undefined: E_INVALIDARG with the value left alone, measured on an RTX 4070 Laptop
// (621.24) and WARP. Checked here rather than against the host, which has crashed on undefined values of
// the shader model query next to this one.
for (const UINT undefined_version : {0x4u, 0x5u, 0x10u, 0xffu, 0x7fffffffu, 0x80000000u, 0xffffffffu}) {
D3D12_FEATURE_DATA_ROOT_SIGNATURE refused{static_cast<D3D_ROOT_SIGNATURE_VERSION>(undefined_version)};
EXPECT(device->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &refused, sizeof(refused)) ==
E_INVALIDARG);
EXPECT(static_cast<UINT>(refused.HighestVersion) == undefined_version);
}
// A wrong DataSize and a null buffer are refused for both queries.
D3D12_FEATURE_DATA_SHADER_MODEL short_model{D3D_SHADER_MODEL_6_0};
D3D12_FEATURE_DATA_ROOT_SIGNATURE short_signature{D3D_ROOT_SIGNATURE_VERSION_1_1};
EXPECT(FAILED(device->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &short_model,
sizeof(short_model) - 1)));
EXPECT(FAILED(device->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, &short_signature,
sizeof(short_signature) - 1)));
EXPECT(FAILED(device->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, nullptr, sizeof(short_model))));
EXPECT(FAILED(device->CheckFeatureSupport(D3D12_FEATURE_ROOT_SIGNATURE, nullptr, sizeof(short_signature))));
}
const int feature_mismatches = compare_feature_support(device.p);
if (feature_mismatches < 0)
std::printf("CheckFeatureSupport: no host D3D12 device to compare with, skipped\n");
EXPECT(feature_mismatches <= 0);
// GetResourceAllocationInfo is compared on the layer's own PC device, not on the proxy: the proxy answers the
// console's placement of a title description (whole console tiles at the console's own alignment,
// d3d12_xs_alloc_size.h), which xs_proxy_mock's slot 25 test pins. The layer itself must match the PC runtime.
ID3D12Device* const layer_device = rt::d3d12_xs::device_proxy_real_device(device.p);
const int allocation_mismatches = compare_allocation_info(layer_device);
if (allocation_mismatches < 0)
std::printf("GetResourceAllocationInfo: no host D3D12 device to compare with, skipped\n");
EXPECT(allocation_mismatches <= 0);
const int rule_mismatches = compare_command_list_rules(device.p);
if (rule_mismatches < 0) std::printf("command-list rules: no host D3D12 device to compare with, skipped\n");
EXPECT(rule_mismatches <= 0);
// D3D-46: copies against the host runtime (d3d12_copy_compare.h).
const int copy_mismatches = copycmp::compare_copies(device.p, d.bc);
if (copy_mismatches < 0) std::printf("Copies: no host D3D12 device to compare with, skipped\n");
EXPECT(copy_mismatches <= 0);
const int depth24_mismatches = copycmp::compare_depth24(device.p, d.depth_format_usable(VK_FORMAT_D24_UNORM_S8_UINT));
if (depth24_mismatches < 0) std::printf("D24 clears: no host D3D12 device to compare with, skipped\n");
EXPECT(depth24_mismatches <= 0);
const int tiling_mismatches = compare_tiling(device.p);
if (tiling_mismatches < 0)
std::printf("GetResourceTiling: no host D3D12 device with tiled resources, or no layout it accepts; "
"skipped\n");
EXPECT(tiling_mismatches <= 0);
const int argument_mismatches = compare_invalid_arguments(device.p);
if (argument_mismatches < 0)
std::printf("invalid arguments: no host D3D12 device to compare with, skipped\n");
EXPECT(argument_mismatches <= 0);
const int feature_error_mismatches = compare_check_feature_support(device.p);
if (feature_error_mismatches < 0)
std::printf("CheckFeatureSupport error paths: no host D3D12 device to compare with, skipped\n");
EXPECT(feature_error_mismatches <= 0);
const int fence_mismatches = compare_fences(device.p);
if (fence_mismatches < 0)
std::printf("Fences: no host D3D12 device to compare with, skipped\n");
EXPECT(fence_mismatches <= 0);
// D3D-236: CreateCommandQueue with D3D12_COMMAND_QUEUE_PRIORITY_GLOBAL_REALTIME requires
// SeIncreaseBasePriorityPrivilege enabled in the token, matching the Windows D3D12 runtime.
{
const D3D12_COMMAND_QUEUE_DESC rt_desc{
D3D12_COMMAND_LIST_TYPE_DIRECT, D3D12_COMMAND_QUEUE_PRIORITY_GLOBAL_REALTIME,
D3D12_COMMAND_QUEUE_FLAG_NONE, 0};
struct RestoreOverride {
~RestoreOverride() { rt::d3d12::set_global_realtime_privilege_override_for_test(-1); }
} restore_override;
rt::d3d12::set_global_realtime_privilege_override_for_test(0);
EXPECT(!rt::d3d12::has_global_realtime_privilege());
Ref<ID3D12CommandQueue> denied_q;
EXPECT(device->CreateCommandQueue(&rt_desc, IID_PPV_ARGS(&denied_q.p)) == DXGI_ERROR_ACCESS_DENIED);
EXPECT(!denied_q.p);
EXPECT(device->CreateCommandQueue(&rt_desc, __uuidof(ID3D12CommandQueue), nullptr) ==
DXGI_ERROR_ACCESS_DENIED);
rt::d3d12::set_global_realtime_privilege_override_for_test(1);
EXPECT(rt::d3d12::has_global_realtime_privilege());
EXPECT(device->CreateCommandQueue(&rt_desc, __uuidof(ID3D12CommandQueue), nullptr) == E_UNEXPECTED);
Ref<ID3D12CommandQueue> granted_q;
EXPECT(SUCCEEDED(device->CreateCommandQueue(&rt_desc, IID_PPV_ARGS(&granted_q.p))) && granted_q.p);
if (granted_q.p) {
// PC GetDesc shares console slot 18 with the newer layout's GetTimestampFrequency; the proxy
// serves the frequency getter while the layout is undetected, so the desc is read on the real
// queue instead.
if (auto* real_q = rt::d3d12_xs::queue_proxy_real_queue(granted_q.p)) {
EXPECT(real_q->GetDesc().Priority == D3D12_COMMAND_QUEUE_PRIORITY_GLOBAL_REALTIME);
} else {
EXPECT(granted_q->GetDesc().Priority == D3D12_COMMAND_QUEUE_PRIORITY_GLOBAL_REALTIME);
}
}
granted_q.reset();
rt::d3d12::set_global_realtime_privilege_override_for_test(-1);
Ref<ID3D12Device> host;
create_host_device(host);
if (host.p) EXPECT(command_queue(device.p, rt_desc) == command_queue(host.p, rt_desc));
// SecurityAnonymous thread impersonation (OpenThreadToken fails with ERROR_CANT_OPEN_ANONYMOUS, not
// ERROR_NO_TOKEN) must fail closed instead of falling back to the process token, matching the host runtime.
const auto check_security_anonymous_impersonation = [&]() -> int {
HANDLE proc_token = nullptr;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE, &proc_token)) return 0;
HANDLE anon_imp = nullptr;
if (DuplicateTokenEx(proc_token, MAXIMUM_ALLOWED, nullptr, SecurityAnonymous, TokenImpersonation,
&anon_imp)) {
if (SetThreadToken(nullptr, anon_imp)) {
struct RevertImpersonation {
~RevertImpersonation() { RevertToSelf(); }
} revert_impersonation;
HANDLE opened = nullptr;
EXPECT(!OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &opened));
EXPECT(GetLastError() == ERROR_CANT_OPEN_ANONYMOUS);
if (opened) CloseHandle(opened);
EXPECT(!rt::d3d12::has_global_realtime_privilege());
EXPECT(command_queue(device.p, rt_desc) == DXGI_ERROR_ACCESS_DENIED);
if (host.p) EXPECT(command_queue(host.p, rt_desc) == DXGI_ERROR_ACCESS_DENIED);
}
CloseHandle(anon_imp);
}
CloseHandle(proc_token);
return 0;
};
if (check_security_anonymous_impersonation() != 0) return 1;
// When the process token holds SeIncreaseBasePriorityPrivilege (e.g. an elevated session), toggle the
// real token privilege and verify both states (plus SecurityAnonymous impersonation while the process
// token has the privilege enabled) against the host D3D12 runtime.
HANDLE token = nullptr;
LUID luid{};
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token)) {
if (LookupPrivilegeValueW(nullptr, L"SeIncreaseBasePriorityPrivilege", &luid)) {
TOKEN_PRIVILEGES tp{}, prev{};
DWORD prev_len = 0;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (AdjustTokenPrivileges(token, FALSE, &tp, sizeof(prev), &prev, &prev_len) &&
GetLastError() == ERROR_SUCCESS) {
EXPECT(rt::d3d12::has_global_realtime_privilege());
EXPECT(command_queue(device.p, rt_desc) == S_OK);
if (host.p) EXPECT(command_queue(host.p, rt_desc) == S_OK);
if (check_security_anonymous_impersonation() != 0) {
AdjustTokenPrivileges(token, FALSE, &prev, 0, nullptr, nullptr);
CloseHandle(token);
return 1;
}
tp.Privileges[0].Attributes = 0;
if (AdjustTokenPrivileges(token, FALSE, &tp, 0, nullptr, nullptr) &&
GetLastError() == ERROR_SUCCESS) {
EXPECT(!rt::d3d12::has_global_realtime_privilege());
EXPECT(command_queue(device.p, rt_desc) == DXGI_ERROR_ACCESS_DENIED);
if (host.p) EXPECT(command_queue(host.p, rt_desc) == DXGI_ERROR_ACCESS_DENIED);
}
AdjustTokenPrivileges(token, FALSE, &prev, 0, nullptr, nullptr);
}
}
CloseHandle(token);
}
}
const int depth_quality_mismatches = compare_depth_multisample_quality_levels(device.p, depth_sample_counts);
if (depth_quality_mismatches < 0)
std::printf("Depth/stencil MSAA quality queries: no host D3D12 device to compare with, skipped\n");
EXPECT(depth_quality_mismatches <= 0);
EXPECT(check_rgb9e5_resolves(device.p, d));
const int resolve_mismatches = compare_resolves(device.p, color_sample_counts);
if (resolve_mismatches < 0)
std::printf("Resolve comparison: no host D3D12 device to compare with, skipped\n");
EXPECT(resolve_mismatches <= 0);
// Command objects; their GetDevice is the proxy, never the inner device.
D3D12_COMMAND_QUEUE_DESC queue_desc{};
queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
Ref<ID3D12CommandQueue> queue;
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
Ref<ID3D12Fence> fence;
EXPECT(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))));
EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))));
EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
IID_PPV_ARGS(&list.p))));
EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))));
// Every wait on `fence` goes through this: a hand-picked value that is reused or lower than an earlier one
// makes SetEventOnCompletion return at once, and the block then reads results the GPU hasn't written.
UINT64 fence_last = 0;
const auto submit_and_wait = [&] {
const UINT64 v = ++fence_last;
return SUCCEEDED(queue->Signal(fence.p, v)) && SUCCEEDED(fence->SetEventOnCompletion(v, nullptr)) &&
fence->GetCompletedValue() >= v;
};
{
Ref<ID3D12Device> parent;
EXPECT(SUCCEEDED(queue->GetDevice(IID_PPV_ARGS(&parent.p))) && parent.p == device.p);
}
// Residency and power (D3D-33): no-ops on Vulkan that still check their arguments, through the proxy's
// forwarded slots. EnqueueMakeResident must signal its fence, or a queue waiting on it would hang.
{
Ref<ID3D12Fence> residency;
EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&residency.p))));
ID3D12Pageable* const pageables[] = {fence.p, allocator.p};
EXPECT(device->MakeResident(2, pageables) == S_OK);
EXPECT(device->MakeResident(0, nullptr) == S_OK);
EXPECT(device->MakeResident(1, nullptr) == E_INVALIDARG);
EXPECT(device->Evict(2, pageables) == S_OK);
EXPECT(device->Evict(0, nullptr) == S_OK);
EXPECT(device->Evict(1, nullptr) == E_INVALIDARG);
EXPECT(device->SetStablePowerState(TRUE) == S_OK);
EXPECT(device->SetStablePowerState(FALSE) == S_OK);
Ref<ID3D12Device1> device1;
EXPECT(SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&device1.p))));
const D3D12_RESIDENCY_PRIORITY priorities[] = {D3D12_RESIDENCY_PRIORITY_MAXIMUM,
D3D12_RESIDENCY_PRIORITY(D3D12_RESIDENCY_PRIORITY_LOW + 5)};
// Only committed resources and heaps take a priority, as on the host runtime (d3d12_residency_test): a
// fence and an allocator don't.
EXPECT(device1->SetResidencyPriority(2, pageables, priorities) == E_INVALIDARG);
EXPECT(device1->SetResidencyPriority(0, nullptr, nullptr) == S_OK);
EXPECT(device1->SetResidencyPriority(1, pageables, nullptr) == E_INVALIDARG);
EXPECT(device1->SetResidencyPriority(1, nullptr, priorities) == E_INVALIDARG);
Ref<ID3D12Device3> device3;
EXPECT(SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&device3.p))));
EXPECT(device3->EnqueueMakeResident(D3D12_RESIDENCY_FLAG_NONE, 2, pageables, residency.p, 7) == S_OK);
EXPECT(residency->GetCompletedValue() == 7);
EXPECT(device3->EnqueueMakeResident(D3D12_RESIDENCY_FLAG_DENY_OVERBUDGET, 0, nullptr, residency.p, 9) ==
S_OK);
EXPECT(residency->GetCompletedValue() == 9);
// A null fence is accepted (the host runtime's answer) and signals nothing; refused calls leave the fence
// alone.
EXPECT(device3->EnqueueMakeResident(D3D12_RESIDENCY_FLAG_NONE, 1, pageables, nullptr, 11) == S_OK);
EXPECT(device3->EnqueueMakeResident(D3D12_RESIDENCY_FLAG_NONE, 1, nullptr, residency.p, 11) ==
E_INVALIDARG);
EXPECT(device3->EnqueueMakeResident(D3D12_RESIDENCY_FLAGS(0x2), 1, pageables, residency.p, 11) ==
E_INVALIDARG);
EXPECT(residency->GetCompletedValue() == 9);
// The GPU sees the signal: a queue waits on it, then signals past it.
EXPECT(SUCCEEDED(queue->Wait(residency.p, 9)));
EXPECT(SUCCEEDED(queue->Signal(residency.p, 10)));
EXPECT(SUCCEEDED(residency->SetEventOnCompletion(10, nullptr)) && residency->GetCompletedValue() == 10);
Ref<ID3D12Device6> device6;
EXPECT(SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&device6.p))));
const HANDLE done = CreateEventW(nullptr, TRUE, FALSE, nullptr);
EXPECT(done);
BOOL further = TRUE;
const HRESULT background = device6->SetBackgroundProcessingMode(
D3D12_BACKGROUND_PROCESSING_MODE_DISABLE_BACKGROUND_WORK, D3D12_MEASUREMENTS_ACTION_COMMIT_RESULTS,
done, &further);
const DWORD signalled = WaitForSingleObject(done, 0);
CloseHandle(done);
EXPECT(background == S_OK && signalled == WAIT_OBJECT_0 && further == FALSE);
EXPECT(device6->SetBackgroundProcessingMode(D3D12_BACKGROUND_PROCESSING_MODE_ALLOWED,
D3D12_MEASUREMENTS_ACTION_KEEP_ALL, nullptr, nullptr) == S_OK);
EXPECT(device6->SetBackgroundProcessingMode(D3D12_BACKGROUND_PROCESSING_MODE(4),
D3D12_MEASUREMENTS_ACTION_KEEP_ALL, nullptr, nullptr) ==
E_INVALIDARG);
EXPECT(device6->SetBackgroundProcessingMode(D3D12_BACKGROUND_PROCESSING_MODE_ALLOWED,
D3D12_MEASUREMENTS_ACTION(4), nullptr, nullptr) ==
E_INVALIDARG);
}
{
// On the Xbox device proxy, slot 61 is the title's CreateSamplerX (e22994464), where ID3D12Device5 has
// CreateMetaCommand, so these PC methods are checked on the Vulkan device behind the proxy.
ID3D12Device* const pc5 = rt::d3d12_xs::is_device_proxy(device.p) ? rt::d3d12_xs::device_proxy_real_device(device.p)
: device.p;
Ref<ID3D12Device5> device5;
EXPECT(SUCCEEDED(pc5->QueryInterface(IID_PPV_ARGS(&device5.p))));
UINT meta_count = 7;
EXPECT(device5->EnumerateMetaCommands(&meta_count, nullptr) == S_OK && meta_count == 0);
EXPECT(device5->EnumerateMetaCommands(nullptr, nullptr) == E_INVALIDARG);
const GUID unknown_command{0x12345678, 0x1234, 0x1234, {1, 2, 3, 4, 5, 6, 7, 8}};
UINT total_size = 0, param_count = 0;
EXPECT(device5->EnumerateMetaCommandParameters(unknown_command, D3D12_META_COMMAND_PARAMETER_STAGE_CREATION,
&total_size, ¶m_count, nullptr) == E_INVALIDARG);
void* meta = reinterpret_cast<void*>(1);
EXPECT(device5->CreateMetaCommand(unknown_command, 0, nullptr, 0, IID_ID3D12MetaCommand, &meta) ==
E_INVALIDARG &&
meta == nullptr);
D3D12_SERIALIZED_DATA_DRIVER_MATCHING_IDENTIFIER identifier{};
EXPECT(device5->CheckDriverMatchingIdentifier(D3D12_SERIALIZED_DATA_RAYTRACING_ACCELERATION_STRUCTURE,
&identifier) == D3D12_DRIVER_MATCHING_IDENTIFIER_UNRECOGNIZED);
EXPECT(device5->CheckDriverMatchingIdentifier(D3D12_SERIALIZED_DATA_TYPE(1), &identifier) ==
D3D12_DRIVER_MATCHING_IDENTIFIER_UNSUPPORTED_TYPE);
}
{
Ref<ID3D12Device4> device4;
Ref<ID3D12GraphicsCommandList> closed;
EXPECT(SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&device4.p))));
EXPECT(SUCCEEDED(device4->CreateCommandList1(0, D3D12_COMMAND_LIST_TYPE_DIRECT,
D3D12_COMMAND_LIST_FLAG_NONE, IID_PPV_ARGS(&closed.p))));
EXPECT(closed->Close() == E_FAIL); // CreateCommandList1 returns a closed list
}
// Descriptor heaps from slot 14, start handles in the Xbox shape.
D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 3, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
Ref<ID3D12DescriptorHeap> rtv_heap, dsv_heap;
EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&rtv_heap.p))));
heap_desc = {D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&dsv_heap.p))));
EXPECT(rt::d3d12_xs::is_heap_proxy(rtv_heap.p) && rt::d3d12_xs::is_heap_proxy(dsv_heap.p));
const std::uint64_t rtv_start = reinterpret_cast<XboxHeapHandleFn>(slot(rtv_heap.p, 9))(rtv_heap.p);
const std::uint64_t dsv_start = reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p);
const UINT increment = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
EXPECT(rtv_start && dsv_start && increment >= sizeof(leonida::TargetView));
const D3D12_CPU_DESCRIPTOR_HANDLE rtv0{SIZE_T(rtv_start)}, rtv1{SIZE_T(rtv_start + increment)},
rtv565{SIZE_T(rtv_start + 2 * increment)}, dsv0{SIZE_T(dsv_start)};
// Resources: the render target through slot 76 with the Xbox tiled layout word, the others through the
// PC CreateCommittedResource.
const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT};
const D3D12_HEAP_PROPERTIES upload_heap{D3D12_HEAP_TYPE_UPLOAD};
const D3D12_HEAP_PROPERTIES readback_heap{D3D12_HEAP_TYPE_READBACK};
const auto create = [&](const D3D12_HEAP_PROPERTIES& heap, const D3D12_RESOURCE_DESC& desc,
D3D12_RESOURCE_STATES state, Ref<ID3D12Resource>& out) {
return device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, state, nullptr,
IID_PPV_ARGS(&out.p));
};
Ref<ID3D12Resource> refused_buffer;
D3D12_RESOURCE_DESC invalid_buffer_samples = buffer_desc(256);
invalid_buffer_samples.SampleDesc.Count = 0;
EXPECT(create(default_heap, invalid_buffer_samples, D3D12_RESOURCE_STATE_COPY_DEST, refused_buffer) ==
E_INVALIDARG && !refused_buffer.p);
invalid_buffer_samples.SampleDesc.Count = 1;
invalid_buffer_samples.SampleDesc.Quality = 1;
EXPECT(create(default_heap, invalid_buffer_samples, D3D12_RESOURCE_STATE_COPY_DEST, refused_buffer) ==
E_INVALIDARG && !refused_buffer.p);
Ref<ID3D12Resource> rt_resource, packed565, ds, untouched, upload, readback, volume, line, line_array,
line_refused, line_target;
D3D12_RESOURCE_DESC xbox_rt =
texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
xbox_rt.Layout = static_cast<D3D12_TEXTURE_LAYOUT>(0x101);
const IID resource_iid = __uuidof(ID3D12Resource);
EXPECT(SUCCEEDED(reinterpret_cast<XboxCreateResourceFn>(slot(device.p, 76))(
device.p, &default_heap, D3D12_HEAP_FLAG_NONE, &xbox_rt, D3D12_RESOURCE_STATE_COMMON, nullptr,
&resource_iid, reinterpret_cast<void**>(&rt_resource.p))));
EXPECT(SUCCEEDED(create(default_heap,
texture_desc(size, size, DXGI_FORMAT_B5G6R5_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
D3D12_RESOURCE_STATE_RENDER_TARGET, packed565)));
EXPECT(SUCCEEDED(create(default_heap,
texture_desc(size, size, DXGI_FORMAT_D32_FLOAT_S8X24_UINT,
D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
D3D12_RESOURCE_STATE_DEPTH_WRITE, ds)));
EXPECT(SUCCEEDED(create(default_heap,
texture_desc(small_size, small_size, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE),
D3D12_RESOURCE_STATE_COPY_SOURCE, untouched)));
EXPECT(SUCCEEDED(create(upload_heap, buffer_desc(65536), D3D12_RESOURCE_STATE_GENERIC_READ, upload)));
EXPECT(SUCCEEDED(create(readback_heap, buffer_desc(65536), D3D12_RESOURCE_STATE_COPY_DEST, readback)));
EXPECT(SUCCEEDED(create(default_heap, volume_desc(0), D3D12_RESOURCE_STATE_COPY_DEST, volume)));
EXPECT(volume->GetDesc().MipLevels == 5);
const auto line_shape = line_desc(size, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM);
const auto line_array_shape = line_desc(small_size, 1, 3, DXGI_FORMAT_R8G8B8A8_UNORM);
EXPECT(SUCCEEDED(create(default_heap, line_shape, D3D12_RESOURCE_STATE_COPY_DEST, line)));
EXPECT(SUCCEEDED(create(default_heap, line_array_shape, D3D12_RESOURCE_STATE_COPY_DEST, line_array)));
EXPECT(line->GetDesc().Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D && line->GetDesc().Height == 1);
EXPECT(line_array->GetDesc().DepthOrArraySize == 3);
EXPECT(rt::d3d12::vulkan_image(line.p) && rt::d3d12::vulkan_image(line_array.p));
auto refused_line = line_shape;
refused_line.Height = 2;
EXPECT(create(default_heap, refused_line, D3D12_RESOURCE_STATE_COMMON, line_refused) == E_INVALIDARG &&
!line_refused.p);
// A 1D render target is created, as the PC runtime does (d3d12_texture1d_target_test compares them).
auto line_target_shape = line_shape;
line_target_shape.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
EXPECT(SUCCEEDED(create(default_heap, line_target_shape, D3D12_RESOURCE_STATE_RENDER_TARGET, line_target)) &&
rt::d3d12::vulkan_image(line_target.p));
EXPECT(rt_resource->GetDesc().Width == size && rt_resource->GetDesc().Layout == D3D12_TEXTURE_LAYOUT_UNKNOWN);
EXPECT(rt::d3d12::vulkan_image(rt_resource.p) && !rt::d3d12::vulkan_image(upload.p));
// Upload buffers are mapped and zeroed.
std::uint8_t* up = nullptr;
std::uint8_t* down = nullptr;
EXPECT(SUCCEEDED(upload->Map(0, nullptr, reinterpret_cast<void**>(&up))) && up);
EXPECT(SUCCEEDED(readback->Map(0, nullptr, reinterpret_cast<void**>(&down))) && down);
bool zeroed = true;
for (int i = 0; i < 65536; ++i) zeroed = zeroed && up[i] == 0;
EXPECT(zeroed);
D3D12_HEAP_PROPERTIES heap_properties{};
EXPECT(SUCCEEDED(upload->GetHeapProperties(&heap_properties, nullptr)) &&
heap_properties.Type == D3D12_HEAP_TYPE_UPLOAD);
{
// D3D-469: the flag stays on the heap, next to the DENY flags a committed buffer gets (0x4c0 on the host).
const D3D12_HEAP_PROPERTIES atomics_heap{D3D12_HEAP_TYPE_DEFAULT};
auto atomics_desc = buffer_desc(256);
atomics_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
Ref<ID3D12Resource> atomics;
EXPECT(SUCCEEDED(device->CreateCommittedResource(&atomics_heap, D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS,
&atomics_desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&atomics.p))));
D3D12_HEAP_PROPERTIES atomics_got{};
D3D12_HEAP_FLAGS atomics_flags = D3D12_HEAP_FLAG_NONE;
EXPECT(SUCCEEDED(atomics->GetHeapProperties(&atomics_got, &atomics_flags)));
EXPECT(UINT(atomics_flags) == (UINT(D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS) |
UINT(D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS)));
}
// D3D-96: upload buffers are in VRAM exactly when the GPU has Resizable BAR (and LEONIDA_UPLOAD_SYSMEM is not
// set), host-visible either way. The copies below read this buffer on the GPU, so both placements are checked.
{
VkPhysicalDeviceMemoryProperties memory{};
d.instance_fn<PFN_vkGetPhysicalDeviceMemoryProperties>("vkGetPhysicalDeviceMemoryProperties")(d.physical,
&memory);
char text[4] = {};
size_t length = 0;
const bool sysmem = getenv_s(&length, text, "LEONIDA_UPLOAD_SYSMEM") == 0 && length > 1 && text[0] != '0';
const bool in_vram = rt::d3d12::device_upload_in_vram(device.p);
EXPECT(in_vram == (leonida::resizable_bar(memory) && !sysmem));
const VkMemoryPropertyFlags upload_flags = rt::d3d12::vulkan_memory_flags(upload.p);
EXPECT((upload_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) &&
(upload_flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT));
if (in_vram) EXPECT(upload_flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
EXPECT(rt::d3d12::vulkan_memory_flags(readback.p) & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
std::printf("upload buffer memory: %s\n", in_vram ? "VRAM (Resizable BAR)" : "system memory");
}
// Views into the heaps.
device->CreateRenderTargetView(rt_resource.p, nullptr, rtv0);
D3D12_RENDER_TARGET_VIEW_DESC rtv_desc{};
rtv_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
device->CreateRenderTargetView(rt_resource.p, &rtv_desc, rtv1);
device->CreateDepthStencilView(ds.p, nullptr, dsv0);
device->CreateRenderTargetView(packed565.p, nullptr, rtv565);
// List 1: clears and barriers on never-written resources (their first use zero-fills them), then copies of
// the colour, depth plane, stencil plane and the untouched texture into the readback buffer.
UINT64 cursor = 0;
const auto rt_place = place(device.p, rt_resource.p, 0, cursor);
const auto packed_place = place(device.p, packed565.p, 0, cursor);
const auto depth_place = place(device.p, ds.p, 0, cursor);
const auto stencil_place = place(device.p, ds.p, 1, cursor);
const auto untouched_place = place(device.p, untouched.p, 0, cursor);
EXPECT(rt_place.Footprint.RowPitch == 256 && depth_place.Footprint.Format == DXGI_FORMAT_R32_TYPELESS &&
stencil_place.Footprint.Format == DXGI_FORMAT_R8_TYPELESS);
std::memset(down, 0xcd, 65536);
D3D12_RESOURCE_BARRIER barrier =
transition(rt_resource.p, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_RENDER_TARGET);
list->ResourceBarrier(1, &barrier);
const FLOAT red[4] = {1, 0, 0, 1}, blue[4] = {0, 0, 1, 1};
const D3D12_RECT square{2, 2, 6, 6}, top_half{0, 0, 8, 4};
list->ClearRenderTargetView(rtv0, red, 0, nullptr);
list->ClearRenderTargetView(rtv1, blue, 1, &square);
const FLOAT packed_full[4] = {1, 0.25f, 0, 1}, packed_rect[4] = {0, 0.75f, 1, 0};
list->ClearRenderTargetView(rtv565, packed_full, 0, nullptr);
list->ClearRenderTargetView(rtv565, packed_rect, 1, &square);
barrier = transition(packed565.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
list->ClearDepthStencilView(dsv0, D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, 0.25f, 0x42, 1, &top_half);
barrier = transition(rt_resource.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
barrier = transition(ds.p, D3D12_RESOURCE_STATE_DEPTH_WRITE, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
Ref<ID3D12GraphicsCommandList7> list7;
EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list7.p))));
D3D12_TEXTURE_BARRIER readable{}; // an enhanced barrier that only makes the untouched texture readable
readable.SyncBefore = D3D12_BARRIER_SYNC_NONE;
readable.SyncAfter = D3D12_BARRIER_SYNC_COPY;
readable.AccessBefore = D3D12_BARRIER_ACCESS_NO_ACCESS;
readable.AccessAfter = D3D12_BARRIER_ACCESS_COPY_SOURCE;
readable.LayoutBefore = D3D12_BARRIER_LAYOUT_COPY_SOURCE;
readable.LayoutAfter = D3D12_BARRIER_LAYOUT_COPY_SOURCE;
readable.pResource = untouched.p;
readable.Subresources = {0xffffffff, 0, 0, 0, 0, 0};
D3D12_BARRIER_GROUP group{};
group.Type = D3D12_BARRIER_TYPE_TEXTURE;
group.NumBarriers = 1;
group.pTextureBarriers = &readable;
list7->Barrier(1, &group);
for (const auto& [resource, index, placed] :
{std::tuple{rt_resource.p, 0u, rt_place}, std::tuple{packed565.p, 0u, packed_place},
std::tuple{ds.p, 0u, depth_place},
std::tuple{ds.p, 1u, stencil_place}, std::tuple{untouched.p, 0u, untouched_place}}) {
const auto dst = footprint(readback.p, placed);
const auto src = subresource(resource, index);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
EXPECT(list->Close() == S_OK);
ID3D12CommandList* lists[] = {list.p};
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait()); // null event: blocks until done
for (UINT row = 0; row < size; ++row) {
for (UINT col = 0; col < size; ++col) {
const bool inside = col >= 2 && col < 6 && row >= 2 && row < 6;
const std::uint8_t want[4] = {std::uint8_t(inside ? 0 : 255), 0, std::uint8_t(inside ? 255 : 0), 255};
report("colour texel", row * size + col,
down + rt_place.Offset + row * rt_place.Footprint.RowPitch + col * 4, want, 4);
// B is in bits 0..4, G in 5..10 and R in 11..15. The green levels round to 16 and 47.
const std::uint16_t packed_word = inside ? 0x05ff : 0xfa00;
report("B5G6R5 texel", row * size + col,
down + packed_place.Offset + row * packed_place.Footprint.RowPitch + col * 2,
reinterpret_cast<const std::uint8_t*>(&packed_word), sizeof(packed_word));
const float depth = row < 4 ? 0.25f : 0.0f; // the bottom half was zero-filled, never cleared
report("depth texel", row * size + col,
down + depth_place.Offset + row * depth_place.Footprint.RowPitch + col * 4,
reinterpret_cast<const std::uint8_t*>(&depth), 4);
const std::uint8_t stencil = row < 4 ? 0x42 : 0;
report("stencil texel", row * size + col,
down + stencil_place.Offset + row * stencil_place.Footprint.RowPitch + col, &stencil, 1);
}
}
const std::uint8_t zero[4] = {};
for (UINT row = 0; row < small_size; ++row)
for (UINT col = 0; col < small_size; ++col)
report("untouched texel", row * small_size + col,
down + untouched_place.Offset + row * untouched_place.Footprint.RowPitch + col * 4, zero, 4);
// List 2: uploads. A pattern goes from the upload buffer into a texture, which is copied whole into a
// texture created in COMMON (implicit promotion to COPY_DEST), and in part (a boxed region) into a smaller
// one; a BC1 texture makes a round trip; a buffer region is copied.
Ref<ID3D12Resource> source, whole, part, bc;
const D3D12_RESOURCE_DESC rgba = texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
EXPECT(SUCCEEDED(create(default_heap, rgba, D3D12_RESOURCE_STATE_COPY_DEST, source)));
EXPECT(SUCCEEDED(create(default_heap, rgba, D3D12_RESOURCE_STATE_COMMON, whole)));
EXPECT(SUCCEEDED(create(default_heap,
texture_desc(small_size, small_size, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE),
D3D12_RESOURCE_STATE_COPY_DEST, part)));
if (d.bc)
EXPECT(SUCCEEDED(create(default_heap,
texture_desc(size, size, DXGI_FORMAT_BC1_UNORM, D3D12_RESOURCE_FLAG_NONE),
D3D12_RESOURCE_STATE_COPY_DEST, bc)));
UINT64 up_cursor = 0;
const auto pattern_place = place(device.p, source.p, 0, up_cursor);
for (UINT row = 0; row < size; ++row)
for (UINT col = 0; col < size; ++col)
std::memcpy(up + pattern_place.Offset + row * pattern_place.Footprint.RowPitch + col * 4,
pattern(col, row).data(), 4);
D3D12_PLACED_SUBRESOURCE_FOOTPRINT bc_up{}, bc_down{};
if (bc.p) {
bc_up = place(device.p, bc.p, 0, up_cursor);
for (UINT block_row = 0; block_row < 2; ++block_row)
for (UINT i = 0; i < 16; ++i) // two 8-byte blocks per row
up[bc_up.Offset + block_row * bc_up.Footprint.RowPitch + i] = std::uint8_t(0x30 + block_row * 16 + i);
}
const UINT64 bytes_at = (up_cursor + 255) / 256 * 256;
for (UINT i = 0; i < 64; ++i) up[bytes_at + i] = std::uint8_t(0xa0 + i);
cursor = 0;
const auto whole_place = place(device.p, whole.p, 0, cursor);
const auto part_place = place(device.p, part.p, 0, cursor);
if (bc.p) bc_down = place(device.p, bc.p, 0, cursor);
const UINT64 bytes_down = (cursor + 255) / 256 * 256;
std::memset(down, 0xcd, 65536);
EXPECT(SUCCEEDED(allocator->Reset()));
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
{
const auto dst = subresource(source.p, 0);
const auto src = footprint(upload.p, pattern_place);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
barrier = transition(source.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
list->CopyResource(whole.p, source.p);
{
const auto dst = subresource(part.p, 0);
const auto src = subresource(source.p, 0);
const D3D12_BOX box{2, 2, 0, 6, 6, 1};
list->CopyTextureRegion(&dst, 0, 0, 0, &src, &box);
}
barrier = transition(whole.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE); // promoted
list->ResourceBarrier(1, &barrier);
barrier = transition(part.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
if (bc.p) {
const auto dst = subresource(bc.p, 0);
const auto src = footprint(upload.p, bc_up);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
barrier = transition(bc.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
}
for (const auto& [resource, placed] : {std::pair{whole.p, whole_place}, std::pair{part.p, part_place},
std::pair{bc.p, bc_down}}) {
if (!resource) continue;
const auto dst = footprint(readback.p, placed);
const auto src = subresource(resource, 0);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
list->CopyBufferRegion(readback.p, bytes_down, upload.p, bytes_at, 64);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
for (UINT row = 0; row < size; ++row)
for (UINT col = 0; col < size; ++col)
report("whole-copy texel", row * size + col,
down + whole_place.Offset + row * whole_place.Footprint.RowPitch + col * 4,
pattern(col, row).data(), 4);
for (UINT row = 0; row < small_size; ++row)
for (UINT col = 0; col < small_size; ++col)
report("boxed-copy texel", row * small_size + col,
down + part_place.Offset + row * part_place.Footprint.RowPitch + col * 4,
pattern(col + 2, row + 2).data(), 4);
if (bc.p)
for (UINT block_row = 0; block_row < 2; ++block_row)
report("BC1 block row", block_row, down + bc_down.Offset + block_row * bc_down.Footprint.RowPitch,
up + bc_up.Offset + block_row * bc_up.Footprint.RowPitch, 16);
else
std::printf("BC1 round trip: no textureCompressionBC, skipped\n");
report("buffer copy", 0, down + bytes_down, up + bytes_at, 64);
// Mip 1 is 4x3x8. Move a two-row box across slices through each of the three copy paths.
// The footprint has a third row, so using the box height as the slice stride corrupts every later slice.
{
Ref<ID3D12Resource> clone, cropped;
EXPECT(SUCCEEDED(create(default_heap, volume_desc(0), D3D12_RESOURCE_STATE_COPY_DEST, clone)));
EXPECT(SUCCEEDED(create(default_heap, volume_desc(0), D3D12_RESOURCE_STATE_COPY_DEST, cropped)));
cursor = 0;
const auto layout = place(device.p, volume.p, 1, cursor);
EXPECT(layout.Footprint.Depth == 8 && layout.Footprint.Height == 3 && layout.Footprint.Width == 4);
const auto offset = [&](UINT x, UINT y, UINT z) {
return layout.Offset + (z * 3 + y) * layout.Footprint.RowPitch + x * 4;
};
for (UINT z = 0; z < 8; ++z)
for (UINT y = 0; y < 3; ++y)
for (UINT x = 0; x < 4; ++x)
std::memcpy(up + offset(x, y, z), pattern(x, z + 8 * y).data(), 4);
std::memset(down, 0xcd, 65536);
const auto in = footprint(upload.p, layout), out = footprint(readback.p, layout);
const auto v = subresource(volume.p, 1), c = subresource(clone.p, 1), target = subresource(cropped.p, 1);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
const D3D12_BOX upload_box{1, 1, 1, 4, 3, 5};
list->CopyTextureRegion(&v, 0, 0, 2, &in, &upload_box);
auto readable_volume = transition(volume.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &readable_volume);
list->CopyResource(clone.p, volume.p);
readable_volume = transition(clone.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &readable_volume);
const D3D12_BOX clone_box{0, 0, 2, 3, 2, 6};
list->CopyTextureRegion(&target, 1, 1, 3, &c, &clone_box);
readable_volume = transition(cropped.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &readable_volume);
const D3D12_BOX output_box{1, 1, 3, 4, 3, 7};
list->CopyTextureRegion(&out, 0, 0, 1, &target, &output_box);
EXPECT(SUCCEEDED(list->Close()));
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
const std::array<std::uint8_t, 4> untouched_bytes{0xcd, 0xcd, 0xcd, 0xcd};
for (UINT z = 0; z < 8; ++z)
for (UINT y = 0; y < 3; ++y)
for (UINT x = 0; x < 4; ++x) {
const auto want = (x < 3 && y < 2 && z >= 1 && z < 5)
? pattern(x + 1, z + 8 * (y + 1)) : untouched_bytes;
report("volume texel", (z * 3 + y) * 4 + x, down + offset(x, y, z), want.data(), 4);
}
}
volume.reset();
// A resolve: a 4x multisampled target is cleared and resolved into a single-sampled texture, which is read
// back. First the shapes D3D12 refuses: a multisampled texture that is not a single-mip render target or
// depth-stencil, a sample count that is not a power of two, and a multisampled volume.
{
Ref<ID3D12Resource> msaa, resolved, refused;
D3D12_RESOURCE_DESC shape =
texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, 2);
shape.SampleDesc.Count = 4;
EXPECT(create(default_heap, shape, D3D12_RESOURCE_STATE_COMMON, refused) == E_INVALIDARG && !refused.p);
shape.MipLevels = 1;
shape.Flags = D3D12_RESOURCE_FLAG_NONE;
EXPECT(create(default_heap, shape, D3D12_RESOURCE_STATE_COMMON, refused) == E_INVALIDARG && !refused.p);
shape.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
shape.SampleDesc.Count = 3;
EXPECT(create(default_heap, shape, D3D12_RESOURCE_STATE_COMMON, refused) == E_INVALIDARG && !refused.p);
shape.SampleDesc.Count = D3D12_MAX_MULTISAMPLE_SAMPLE_COUNT * 2;
EXPECT(create(default_heap, shape, D3D12_RESOURCE_STATE_COMMON, refused) == E_INVALIDARG && !refused.p);
shape.SampleDesc.Count = 0;
EXPECT(create(default_heap, shape, D3D12_RESOURCE_STATE_COMMON, refused) == E_INVALIDARG && !refused.p);
shape.SampleDesc.Count = 4;
shape.SampleDesc.Quality = 1; // The Vulkan backend exposes only quality level 0.
EXPECT(create(default_heap, shape, D3D12_RESOURCE_STATE_COMMON, refused) == E_INVALIDARG && !refused.p);
shape.SampleDesc.Quality = 0;
shape = volume_desc(1);
shape.SampleDesc.Count = 4;
EXPECT(create(default_heap, shape, D3D12_RESOURCE_STATE_COMMON, refused) == E_INVALIDARG && !refused.p);
D3D12_RESOURCE_DESC samples4 =
texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
samples4.SampleDesc.Count = 4;
EXPECT(SUCCEEDED(create(default_heap, samples4, D3D12_RESOURCE_STATE_RENDER_TARGET, msaa)));
EXPECT(SUCCEEDED(create(default_heap, rgba, D3D12_RESOURCE_STATE_RESOLVE_DEST, resolved)));
EXPECT(msaa->GetDesc().SampleDesc.Count == 4);
D3D12_RENDER_TARGET_VIEW_DESC msaa_view{};
msaa_view.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
msaa_view.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DMS;
device->CreateRenderTargetView(msaa.p, &msaa_view, rtv1); // the lists that used this slot have run
cursor = 0;
const auto resolved_place = place(device.p, resolved.p, 0, cursor);
std::memset(down, 0xcd, 65536);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
const FLOAT green[4] = {0, 1, 0, 1};
list->ClearRenderTargetView(rtv1, green, 0, nullptr);
barrier = transition(msaa.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
list->ResourceBarrier(1, &barrier);
list->ResolveSubresource(resolved.p, 0, msaa.p, 0, DXGI_FORMAT_R8G8B8A8_UNORM);
barrier = transition(resolved.p, D3D12_RESOURCE_STATE_RESOLVE_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
{
const auto dst = footprint(readback.p, resolved_place);
const auto src = subresource(resolved.p, 0);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
const std::uint8_t resolved_green[4] = {0, 255, 0, 255};
for (UINT row = 0; row < size; ++row)
for (UINT col = 0; col < size; ++col)
report("resolved texel", row * size + col,
down + resolved_place.Offset + row * resolved_place.Footprint.RowPitch + col * 4,
resolved_green, 4);
// Move a cropped two-colour region into a larger red texture.
constexpr UINT region_width = 10, region_height = 9;
constexpr auto region_format = DXGI_FORMAT_R8G8B8A8_UNORM;
const auto region_desc =
texture_desc(region_width, region_height, region_format, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
Ref<ID3D12Resource> region_target;
EXPECT(SUCCEEDED(create(default_heap, region_desc, D3D12_RESOURCE_STATE_RENDER_TARGET, region_target)));
device->CreateRenderTargetView(region_target.p, nullptr, rtv0);
cursor = 0;
const auto region_place = place(device.p, region_target.p, 0, cursor);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
barrier = transition(msaa.p, D3D12_RESOURCE_STATE_RESOLVE_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET);
list->ResourceBarrier(1, &barrier);
const D3D12_RECT blue_patch{2, 3, 4, 5};
list->ClearRenderTargetView(rtv1, blue, 1, &blue_patch);
list->ClearRenderTargetView(rtv0, red, 0, nullptr);
const D3D12_RESOURCE_BARRIER resolve_barriers[] = {
transition(msaa.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_RESOLVE_SOURCE),
transition(region_target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_RESOLVE_DEST)};
list->ResourceBarrier(2, resolve_barriers);
D3D12_RECT region{1, 2, 5, 5};
const auto region_copy = subresource(region_target.p, 0), region_out = footprint(readback.p, region_place);
const auto resolve_and_readback = [&](UINT x, D3D12_RECT* rect) {
list7->ResolveSubresourceRegion(region_target.p, 0, x, 1, msaa.p, 0, rect, region_format,
D3D12_RESOLVE_MODE_AVERAGE);
const auto to_copy = transition(region_target.p, D3D12_RESOURCE_STATE_RESOLVE_DEST,
D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &to_copy);
list->CopyTextureRegion(®ion_out, 0, 0, 0, ®ion_copy, nullptr);
if (list->Close() != S_OK) return false;
queue->ExecuteCommandLists(1, lists);
return rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success &&
submit_and_wait();
};
EXPECT(resolve_and_readback(4, ®ion));
const std::uint8_t resolved_red[4] = {255, 0, 0, 255}, resolved_blue[4] = {0, 0, 255, 255};
for (UINT row = 0; row < region_height; ++row)
for (UINT col = 0; col < region_width; ++col) {
const bool in_region = row >= 1 && row < 4 && col >= 4 && col < 8;
const bool in_blue = row >= 2 && row < 4 && col >= 5 && col < 7;
report("partial resolve texel", row * region_width + col,
down + region_place.Offset + row * region_place.Footprint.RowPitch + col * 4,
in_blue ? resolved_blue : in_region ? resolved_green : resolved_red, 4);
}
// A null rectangle resolves the entire source at the destination offset.
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
const auto to_resolve = transition(region_target.p, D3D12_RESOURCE_STATE_COPY_SOURCE,
D3D12_RESOURCE_STATE_RESOLVE_DEST);
list->ResourceBarrier(1, &to_resolve);
EXPECT(resolve_and_readback(1, nullptr));
for (UINT row = 0; row < region_height; ++row)
for (UINT col = 0; col < region_width; ++col) {
const bool in_source = row >= 1 && col >= 1 && col < 9;
const bool in_blue = row >= 4 && row < 6 && col >= 3 && col < 5;
report("whole source at offset", row * region_width + col,
down + region_place.Offset + row * region_place.Footprint.RowPitch + col * 4,
in_blue ? resolved_blue : in_source ? resolved_green : resolved_red, 4);
}
D3D12_RECT out_of_source{0, 0, LONG(size + 1), 1};
for (auto [x, rect] : {std::pair{0u, &out_of_source}, std::pair{7u, ®ion}}) {
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
list->ResourceBarrier(1, &to_resolve);
list7->ResolveSubresourceRegion(region_target.p, 0, x, 1, msaa.p, 0, rect, region_format,
D3D12_RESOLVE_MODE_AVERAGE);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::invalid_copy);
}
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
list7->ResolveSubresourceRegion(region_target.p, 0, 4, 1, msaa.p, 0, ®ion, region_format,
D3D12_RESOLVE_MODE_MAX);
EXPECT(list->Close() == E_INVALIDARG);
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
list7->ResolveSubresourceRegion(region_target.p, 0, 4, 1, msaa.p, 0, ®ion,
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, D3D12_RESOLVE_MODE_AVERAGE);
EXPECT(list->Close() == E_INVALIDARG);
// A resolve of a resource into itself is refused when it is recorded, one from a single-sampled
// texture when it is executed; nothing is submitted.
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
list->ResolveSubresource(resolved.p, 0, resolved.p, 0, DXGI_FORMAT_R8G8B8A8_UNORM);
EXPECT(list->Close() == E_INVALIDARG);
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
list->ResolveSubresource(resolved.p, 0, source.p, 0, DXGI_FORMAT_R8G8B8A8_UNORM);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::invalid_copy);
}
// D3D-26 through COM: AtomicCopyBufferUINT/UINT64 from the upload buffer to the readback buffer, discards of a
// texture and a buffer, and a WriteBufferImmediate whose address no resource holds, which fails at execution.
{
Ref<ID3D12GraphicsCommandList1> list1;
Ref<ID3D12GraphicsCommandList2> list2;
EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p))));
EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list2.p))));
const std::uint32_t value32 = 0x600df00d;
const std::uint64_t value64 = 0x0123456789abcdefull;
std::memcpy(up + 4096, &value32, 4);
std::memcpy(up + 4104, &value64, 8);
std::memset(down, 0xcd, 65536);
ID3D12Resource* const dependents[] = {upload.p};
const D3D12_SUBRESOURCE_RANGE_UINT64 ranges[] = {{0, {4096, 4112}}};
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
list1->AtomicCopyBufferUINT(readback.p, 256, upload.p, 4096, 1, dependents, ranges);
list1->AtomicCopyBufferUINT64(readback.p, 264, upload.p, 4104, 1, dependents, ranges);
list->DiscardResource(rt_resource.p, nullptr);
const D3D12_DISCARD_REGION first_mip{0, nullptr, 0, 1};
list->DiscardResource(rt_resource.p, &first_mip);
list->DiscardResource(upload.p, nullptr);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
report("atomic copy UINT", 0, down + 256, reinterpret_cast<const std::uint8_t*>(&value32), 4);
report("atomic copy UINT64", 0, down + 264, reinterpret_cast<const std::uint8_t*>(&value64), 8);
const std::uint8_t cd[4] = {0xcd, 0xcd, 0xcd, 0xcd};
report("next to the atomic copies", 0, down + 252, cd, 4);
report("next to the atomic copies", 1, down + 272, cd, 4);
// Misaligned calls are refused when recorded; count == 0 is accepted (matches Windows 11 WARP & RTX 5090).
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
list1->AtomicCopyBufferUINT64(readback.p, 260, upload.p, 4104, 1, dependents, ranges);
EXPECT(list->Close() == E_INVALIDARG);
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
list1->AtomicCopyBufferUINT(readback.p, 256, upload.p, 4096, 0, nullptr, nullptr);
EXPECT(list->Close() == S_OK);
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
const D3D12_WRITEBUFFERIMMEDIATE_PARAMETER misaligned{0x10002, 1};
list2->WriteBufferImmediate(1, &misaligned, nullptr);
EXPECT(list->Close() == E_INVALIDARG);
// A region with NumSubresources = UINT_MAX is an explicit range, not a null region.
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
const D3D12_DISCARD_REGION all_from_one{0, nullptr, 1, UINT_MAX};
list->DiscardResource(rt_resource.p, &all_from_one);
EXPECT(list->Close() == E_INVALIDARG);
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
const D3D12_DISCARD_REGION all_from_zero{0, nullptr, 0, UINT_MAX};
list->DiscardResource(rt_resource.p, &all_from_zero);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::invalid_subresource);
// Nothing on main gives a buffer a GPU virtual address yet (GetGPUVirtualAddress, #157/#291), so an
// immediate write has no target: the list is refused and nothing is submitted.
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
const D3D12_WRITEBUFFERIMMEDIATE_PARAMETER write{0x10000, 1};
const D3D12_WRITEBUFFERIMMEDIATE_MODE mode = D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT;
list2->WriteBufferImmediate(1, &write, &mode);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::unresolved_resource);
}
// A depth-stencil view of a slice range of a 4x multisampled depth array (D3D12_DSV_DIMENSION_TEXTURE2DMSARRAY):
// the view captures the range, a clear through it replays, and a range past the last slice is refused when
// the list runs. D3D12 can't copy a multisampled texture to a buffer, so the clear itself isn't read back.
{
Ref<ID3D12Resource> ms_depth;
Ref<ID3D12Fence> ms_fence;
D3D12_RESOURCE_DESC ms_desc =
texture_desc(size, size, DXGI_FORMAT_D32_FLOAT, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, 1, 4);
ms_desc.SampleDesc.Count = 4;
EXPECT(SUCCEEDED(create(default_heap, ms_desc, D3D12_RESOURCE_STATE_DEPTH_WRITE, ms_depth)));
EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&ms_fence.p))));
D3D12_DEPTH_STENCIL_VIEW_DESC ms_view{};
ms_view.Format = DXGI_FORMAT_D32_FLOAT;
ms_view.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DMSARRAY;
ms_view.Texture2DMSArray.FirstArraySlice = 1;
ms_view.Texture2DMSArray.ArraySize = 2;
device->CreateDepthStencilView(ms_depth.p, &ms_view, dsv0); // the lists that used this slot have run
leonida::TargetView captured{};
std::memcpy(&captured, reinterpret_cast<const void*>(dsv0.ptr), sizeof(captured));
EXPECT(captured.resource && captured.mip == 0 && captured.first_layer == 1 && captured.layer_count == 2);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
list->ClearDepthStencilView(dsv0, D3D12_CLEAR_FLAG_DEPTH, 0.5f, 0, 0, nullptr);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(SUCCEEDED(queue->Signal(ms_fence.p, 1)) && SUCCEEDED(ms_fence->SetEventOnCompletion(1, nullptr)));
ms_view.Texture2DMSArray.FirstArraySlice = 3; // slices 3 and 4 of a 4-slice texture
device->CreateDepthStencilView(ms_depth.p, &ms_view, dsv0);
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
list->ClearDepthStencilView(dsv0, D3D12_CLEAR_FLAG_DEPTH, 0.5f, 0, 0, nullptr);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::invalid_subresource);
EXPECT(SUCCEEDED(queue->Signal(ms_fence.p, 2)) && SUCCEEDED(ms_fence->SetEventOnCompletion(2, nullptr)));
}
// A render-target view of a slice range of a 4x multisampled colour array (D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY):
// all four slices are cleared red through a whole-array view, then slices 1-2 green through a two-slice view.
// Every slice is resolved and read back, so only slices 1 and 2 may be green. A range past the last slice is
// refused when the list runs.
{
Ref<ID3D12Resource> ms_colour, ms_resolved;
Ref<ID3D12Fence> ms_fence;
constexpr UINT16 slices = 4;
D3D12_RESOURCE_DESC ms_desc = texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, 1, slices);
ms_desc.SampleDesc.Count = 4;
EXPECT(SUCCEEDED(create(default_heap, ms_desc, D3D12_RESOURCE_STATE_RENDER_TARGET, ms_colour)));
EXPECT(SUCCEEDED(create(default_heap,
texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 1,
slices),
D3D12_RESOURCE_STATE_RESOLVE_DEST, ms_resolved)));
EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&ms_fence.p))));
D3D12_RENDER_TARGET_VIEW_DESC ms_view{};
ms_view.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
ms_view.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY;
ms_view.Texture2DMSArray.FirstArraySlice = 0;
ms_view.Texture2DMSArray.ArraySize = slices;
device->CreateRenderTargetView(ms_colour.p, &ms_view, rtv0); // the lists that used these slots have run
ms_view.Texture2DMSArray.FirstArraySlice = 1;
ms_view.Texture2DMSArray.ArraySize = 2;
device->CreateRenderTargetView(ms_colour.p, &ms_view, rtv1);
leonida::TargetView captured{};
std::memcpy(&captured, reinterpret_cast<const void*>(rtv1.ptr), sizeof(captured));
EXPECT(captured.resource && captured.mip == 0 && captured.first_layer == 1 && captured.layer_count == 2);
cursor = 0;
D3D12_PLACED_SUBRESOURCE_FOOTPRINT slice_place[slices];
for (UINT s = 0; s < slices; ++s) slice_place[s] = place(device.p, ms_resolved.p, s, cursor);
std::memset(down, 0xcd, 65536);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
const FLOAT slice_red[4] = {1, 0, 0, 1}, slice_green[4] = {0, 1, 0, 1};
list->ClearRenderTargetView(rtv0, slice_red, 0, nullptr);
list->ClearRenderTargetView(rtv1, slice_green, 0, nullptr);
barrier = transition(ms_colour.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
list->ResourceBarrier(1, &barrier);
for (UINT s = 0; s < slices; ++s)
list->ResolveSubresource(ms_resolved.p, s, ms_colour.p, s, DXGI_FORMAT_R8G8B8A8_UNORM);
barrier = transition(ms_resolved.p, D3D12_RESOURCE_STATE_RESOLVE_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
for (UINT s = 0; s < slices; ++s) {
const auto dst = footprint(readback.p, slice_place[s]);
const auto src = subresource(ms_resolved.p, s);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(SUCCEEDED(queue->Signal(ms_fence.p, 1)) && SUCCEEDED(ms_fence->SetEventOnCompletion(1, nullptr)));
const std::uint8_t red_texel[4] = {255, 0, 0, 255}, green_texel[4] = {0, 255, 0, 255};
for (UINT s = 0; s < slices; ++s)
for (UINT row = 0; row < size; ++row)
for (UINT col = 0; col < size; ++col)
report("multisample array slice texel", (s * size + row) * size + col,
down + slice_place[s].Offset + row * slice_place[s].Footprint.RowPitch + col * 4,
s == 1 || s == 2 ? green_texel : red_texel, 4);
ms_view.Texture2DMSArray.FirstArraySlice = 3; // slices 3 and 4 of a 4-slice texture
device->CreateRenderTargetView(ms_colour.p, &ms_view, rtv1);
EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr)));
barrier = transition(ms_colour.p, D3D12_RESOURCE_STATE_RESOLVE_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET);
list->ResourceBarrier(1, &barrier);
list->ClearRenderTargetView(rtv1, slice_green, 0, nullptr);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::invalid_subresource);
EXPECT(SUCCEEDED(queue->Signal(ms_fence.p, 2)) && SUCCEEDED(ms_fence->SetEventOnCompletion(2, nullptr)));
}
// 1D textures and 1D arrays use the same footprint path; one array layer is enough to prove the image
// type and the array stride. This runs after the resolve fences so it can use a higher fence value.
{
cursor = 0;
const auto line_up = place(device.p, line.p, 0, cursor);
const auto line_array_up = place(device.p, line_array.p, 1, cursor);
EXPECT(line_up.Footprint.Width == size && line_up.Footprint.Height == 1 &&
line_up.Footprint.Depth == 1 && line_up.Footprint.RowPitch == 256);
EXPECT(line_array_up.Footprint.Height == 1 && line_array_up.Footprint.Depth == 1);
for (UINT col = 0; col < size; ++col)
std::memcpy(up + line_up.Offset + col * 4, pattern(col, 0).data(), 4);
for (UINT col = 0; col < small_size; ++col)
std::memcpy(up + line_array_up.Offset + col * 4, pattern(col, 1).data(), 4);
cursor = 0;
const auto line_down = place(device.p, line.p, 0, cursor);
const auto line_array_down = place(device.p, line_array.p, 1, cursor);
std::memset(down, 0xcd, 65536);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
{
const auto dst = subresource(line.p, 0);
const auto src = footprint(upload.p, line_up);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
{
const auto dst = subresource(line_array.p, 1);
const auto src = footprint(upload.p, line_array_up);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
barrier = transition(line.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
barrier = transition(line_array.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
{
const auto dst = footprint(readback.p, line_down);
const auto src = subresource(line.p, 0);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
{
const auto dst = footprint(readback.p, line_array_down);
const auto src = subresource(line_array.p, 1);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
for (UINT col = 0; col < size; ++col)
report("1D texel", col, down + line_down.Offset + col * 4, pattern(col, 0).data(), 4);
for (UINT col = 0; col < small_size; ++col)
report("1D array texel", col, down + line_array_down.Offset + col * 4,
pattern(col, 1).data(), 4);
}
// Reserved resources (docs/d3d12_vulkan.md section 4.2): fully backed at creation, so they copy like
// committed ones and their first use reads zero; D3D12's description rules hold; tile-mapping calls are
// accepted and change nothing.
{
const auto B = D3D12_RESOURCE_DIMENSION_BUFFER, T2 = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
Ref<ID3D12Resource> tiled, tiled_buffer, xbox_tiled, refused;
const D3D12_RESOURCE_DESC tiled_desc = reserved_desc(T2, size, size, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM);
EXPECT(SUCCEEDED(device->CreateReservedResource(&tiled_desc, D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&tiled.p))));
EXPECT(tiled->GetDesc().Layout == D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE);
D3D12_HEAP_PROPERTIES reserved_heap{};
EXPECT(tiled->GetHeapProperties(&reserved_heap, nullptr) == E_INVALIDARG); // no heap of its own
// A null output is E_UNEXPECTED, as on the host runtime (compare_placed_reserved, "reserved:").
EXPECT(device->CreateReservedResource(&tiled_desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
__uuidof(ID3D12Resource), nullptr) == E_UNEXPECTED);
const auto refuses = [&](D3D12_RESOURCE_DESC desc) {
const HRESULT hr = device->CreateReservedResource(&desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&refused.p));
return hr == E_INVALIDARG && !refused.p;
};
auto bad = tiled_desc;
bad.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // textures need a 64 KiB swizzle layout
EXPECT(refuses(bad));
bad.Layout = D3D12_TEXTURE_LAYOUT_64KB_STANDARD_SWIZZLE; // StandardSwizzle64KBSupported is FALSE
EXPECT(refuses(bad));
EXPECT(refuses(reserved_desc(T2, 64, 64, 1, 1, DXGI_FORMAT_R32G32B32_FLOAT))); // 12-byte texels: no tiles
bad = reserved_desc(B, 65536, 1, 1, 1, DXGI_FORMAT_UNKNOWN);
bad.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; // buffers are ROW_MAJOR
EXPECT(refuses(bad));
const D3D12_RESOURCE_DESC tiled_buffer_desc = reserved_desc(B, 2 * 65536, 1, 1, 1, DXGI_FORMAT_UNKNOWN);
EXPECT(SUCCEEDED(device->CreateReservedResource(&tiled_buffer_desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&tiled_buffer.p))));
auto xbox_desc = tiled_desc;
xbox_desc.Layout = static_cast<D3D12_TEXTURE_LAYOUT>(0x101); // the Xbox tiled layout word
EXPECT(SUCCEEDED(device->CreateReservedResource(&xbox_desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&xbox_tiled.p))));
{
Ref<ID3D12Device4> device4;
Ref<ID3D12Device10> device10;
Ref<ID3D12Resource> one, two;
EXPECT(SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&device4.p))));
// On the Xbox device proxy, slot 78 is the title's SetFrameIntervalX (#6935) and slot 55 is the Xbox
// CreateDerivedPipelineState, so the PC methods are checked on the Vulkan device behind the proxy.
ID3D12Device* pc = rt::d3d12_xs::is_device_proxy(device.p) ? rt::d3d12_xs::device_proxy_real_device(device.p)
: device.p;
EXPECT(SUCCEEDED(pc->QueryInterface(IID_PPV_ARGS(&device10.p))));
Ref<ID3D12Device4> real4;
EXPECT(SUCCEEDED(pc->QueryInterface(IID_PPV_ARGS(&real4.p))));
EXPECT(SUCCEEDED(real4->CreateReservedResource1(&tiled_desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
nullptr, IID_PPV_ARGS(&one.p))));
EXPECT(SUCCEEDED(device10->CreateReservedResource2(&tiled_desc, D3D12_BARRIER_LAYOUT_COMMON, nullptr,
nullptr, 0, nullptr, IID_PPV_ARGS(&two.p))));
EXPECT(two.p);
EXPECT(one.p && two.p);
UINT tiles = 0;
device->GetResourceTiling(two.p, &tiles, nullptr, nullptr, nullptr, 0, nullptr);
EXPECT(tiles == 1); // 8x8 RGBA8: one packed tile
}
// Tiling: a resource that is not reserved has none, as on the PC runtime; a first index past the end
// returns no subresources.
UINT total = 7, count = 4;
D3D12_PACKED_MIP_INFO packed{1, 1, 1, 1};
D3D12_TILE_SHAPE shape{1, 1, 1};
D3D12_SUBRESOURCE_TILING tilings[4]{};
device->GetResourceTiling(upload.p, &total, &packed, &shape, &count, 0, tilings);
EXPECT(total == 0 && count == 0 && shape.WidthInTexels == 0 && packed.NumStandardMips == 0 &&
packed.NumTilesForPackedMips == 0);
count = 4;
device->GetResourceTiling(tiled_buffer.p, &total, &packed, &shape, &count, 0, tilings);
EXPECT(total == 2 && count == 1 && shape.WidthInTexels == 65536 && tilings[0].WidthInTiles == 2);
count = 4;
device->GetResourceTiling(tiled_buffer.p, nullptr, nullptr, nullptr, &count, 1, tilings);
EXPECT(count == 0);
// Mapping calls: accepted, logged once, and the resources keep their memory.
D3D12_TILED_RESOURCE_COORDINATE origin{};
D3D12_TILE_REGION_SIZE one_tile{1, FALSE, 0, 0, 0};
const D3D12_TILE_RANGE_FLAGS unmap = D3D12_TILE_RANGE_FLAG_NULL;
queue->UpdateTileMappings(tiled.p, 1, &origin, &one_tile, nullptr, 1, &unmap, nullptr, nullptr,
D3D12_TILE_MAPPING_FLAG_NONE);
queue->CopyTileMappings(tiled_buffer.p, &origin, tiled_buffer.p, &origin, &one_tile,
D3D12_TILE_MAPPING_FLAG_NONE);
// Upload a pattern into the reserved texture and bytes into the reserved buffer, read both back, and
// read the never-written Xbox-layout texture, which is zero.
UINT64 tiled_up_cursor = 0;
const auto tiled_up = place(device.p, tiled.p, 0, tiled_up_cursor);
for (UINT row = 0; row < size; ++row)
for (UINT col = 0; col < size; ++col)
std::memcpy(up + tiled_up.Offset + row * tiled_up.Footprint.RowPitch + col * 4,
pattern(col, row).data(), 4);
const UINT64 tiled_bytes_up = (tiled_up_cursor + 255) / 256 * 256;
for (UINT i = 0; i < 256; ++i) up[tiled_bytes_up + i] = std::uint8_t(0x5a ^ i);
UINT64 tiled_down_cursor = 0;
const auto tiled_down = place(device.p, tiled.p, 0, tiled_down_cursor);
const auto xbox_down = place(device.p, xbox_tiled.p, 0, tiled_down_cursor);
const UINT64 tiled_bytes_down = (tiled_down_cursor + 255) / 256 * 256;
std::memset(down, 0xcd, 65536);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
{
const auto dst = subresource(tiled.p, 0);
const auto src = footprint(upload.p, tiled_up);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
barrier = transition(tiled.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
barrier = transition(xbox_tiled.p, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
for (const auto& [resource, placed] : {std::pair{tiled.p, tiled_down}, std::pair{xbox_tiled.p, xbox_down}}) {
const auto dst = footprint(readback.p, placed);
const auto src = subresource(resource, 0);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
list->CopyBufferRegion(tiled_buffer.p, 65536 + 4096, upload.p, tiled_bytes_up, 256); // into the second tile
barrier = transition(tiled_buffer.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
list->CopyBufferRegion(readback.p, tiled_bytes_down, tiled_buffer.p, 65536 + 4096, 256);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
for (UINT row = 0; row < size; ++row)
for (UINT col = 0; col < size; ++col) {
report("reserved texel", row * size + col,
down + tiled_down.Offset + row * tiled_down.Footprint.RowPitch + col * 4,
pattern(col, row).data(), 4);
report("unwritten reserved texel", row * size + col,
down + xbox_down.Offset + row * xbox_down.Footprint.RowPitch + col * 4, zero, 4);
}
report("reserved buffer", 0, down + tiled_bytes_down, up + tiled_bytes_up, 256);
}
// Several lists in one ExecuteCommandLists, then fewer. The queue keeps its prepared storage between
// executions, so the second must replay only the list it is given. B clears blue and A red: {B, A} leaves
// red, then {B} alone must leave blue (a stale A replayed after it would leave red).
{
Ref<ID3D12CommandAllocator> allocator_a, allocator_b;
Ref<ID3D12GraphicsCommandList> list_a, list_b;
Ref<ID3D12Resource> target;
EXPECT(SUCCEEDED(create(default_heap,
texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
D3D12_RESOURCE_STATE_RENDER_TARGET, target)));
device->CreateRenderTargetView(target.p, nullptr, rtv1); // the lists that used this slot have run
for (auto [a, l, colour] : {std::tuple{&allocator_a, &list_a, red}, std::tuple{&allocator_b, &list_b, blue}}) {
EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&a->p))));
EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, a->p, nullptr,
IID_PPV_ARGS(&l->p))));
(*l)->ClearRenderTargetView(rtv1, colour, 0, nullptr);
EXPECT((*l)->Close() == S_OK);
}
ID3D12CommandList* b_then_a[] = {list_b.p, list_a.p};
queue->ExecuteCommandLists(2, b_then_a);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
ID3D12CommandList* b_only[] = {list_b.p};
queue->ExecuteCommandLists(1, b_only);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
UINT64 target_cursor = 0;
const auto target_place = place(device.p, target.p, 0, target_cursor);
std::memset(down, 0xcd, 65536);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
barrier = transition(target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
{
const auto dst = footprint(readback.p, target_place);
const auto src = subresource(target.p, 0);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
const std::uint8_t blue_texel[4] = {0, 0, 255, 255};
for (UINT row = 0; row < size; ++row)
for (UINT col = 0; col < size; ++col)
report("second ExecuteCommandLists, fewer lists", row * size + col,
down + target_place.Offset + row * target_place.Footprint.RowPitch + col * 4, blue_texel, 4);
}
// Queries (docs/d3d12_vulkan.md section 4.6). The layer has no draws yet, so occlusion and pipeline
// statistics count nothing and read zero; what is checked is that every query is reset and run, that
// results land as UINT64s at the offsets asked for (over a 0xcd fill), that timestamps advance at the
// reported frequency, that a query keeps its result across executions, that one never ended reads zero
// instead of stalling the GPU, and that the D3D12 rules for the calls are kept.
{
UINT64 frequency = 0;
EXPECT(SUCCEEDED(queue->GetTimestampFrequency(&frequency)) && frequency >= 1'000'000);
EXPECT(queue->GetTimestampFrequency(nullptr) == E_INVALIDARG);
Ref<ID3D12QueryHeap> occlusion, timestamps, statistics, so, refused;
D3D12_QUERY_HEAP_DESC query_desc{D3D12_QUERY_HEAP_TYPE_OCCLUSION, 4, 0};
EXPECT(SUCCEEDED(device->CreateQueryHeap(&query_desc, IID_PPV_ARGS(&occlusion.p))));
// A null output is E_UNEXPECTED, as on the host runtime (compare_invalid_arguments, "query heap:").
EXPECT(device->CreateQueryHeap(&query_desc, __uuidof(ID3D12QueryHeap), nullptr) == E_UNEXPECTED);
{
Ref<ID3D12Device> parent;
EXPECT(SUCCEEDED(occlusion->GetDevice(IID_PPV_ARGS(&parent.p))) && parent.p == device.p);
}
query_desc = {D3D12_QUERY_HEAP_TYPE_TIMESTAMP, 3, 0};
EXPECT(SUCCEEDED(device->CreateQueryHeap(&query_desc, IID_PPV_ARGS(×tamps.p))));
query_desc = {D3D12_QUERY_HEAP_TYPE_OCCLUSION, 0, 0};
EXPECT(device->CreateQueryHeap(&query_desc, IID_PPV_ARGS(&refused.p)) == E_INVALIDARG && !refused.p);
// D3D-302: an SO_STATISTICS heap is a real transform-feedback pool on a device with
// VK_EXT_transform_feedback whose transformFeedbackQueries property is set (indexed on the stream); without
// them the heap is made without a Vulkan pool and every query reads zero. A stream past
// maxTransformFeedbackStreams reads zero too. StreamOutput PSOs are still refused, so no stream output runs
// and the resolved counts are 0 in either case.
query_desc = {D3D12_QUERY_HEAP_TYPE_SO_STATISTICS, 3, 0};
EXPECT(SUCCEEDED(device->CreateQueryHeap(&query_desc, IID_PPV_ARGS(&so.p))));
query_desc = {D3D12_QUERY_HEAP_TYPE_PIPELINE_STATISTICS, 2, 0};
if (d.pipeline_statistics_query)
EXPECT(SUCCEEDED(device->CreateQueryHeap(&query_desc, IID_PPV_ARGS(&statistics.p))));
else
std::printf("pipeline statistics: no pipelineStatisticsQuery, skipped\n");
// Readback layout: timestamps at 0, occlusion at 64, statistics at 256, stream output at 512, the second
// run's at 1024.
constexpr UINT64 ts_at = 0, occlusion_at = 64, statistics_at = 256, so_at = 512, again_at = 1024;
static_assert(sizeof(D3D12_QUERY_DATA_SO_STATISTICS) == 16);
const auto u64 = [&](UINT64 offset) {
UINT64 value;
std::memcpy(&value, down + offset, sizeof(value));
return value;
};
std::memset(down, 0xcd, 65536);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
list->EndQuery(timestamps.p, D3D12_QUERY_TYPE_TIMESTAMP, 0);
if (statistics.p) list->BeginQuery(statistics.p, D3D12_QUERY_TYPE_PIPELINE_STATISTICS, 0);
// Two streams at once, inside the statistics query and crossing the occlusion one: none of them is a
// Vulkan query, so none overlaps.
list->BeginQuery(so.p, D3D12_QUERY_TYPE_SO_STATISTICS_STREAM0, 0);
list->BeginQuery(so.p, D3D12_QUERY_TYPE_SO_STATISTICS_STREAM3, 1);
// A rectangle clear runs in a render pass inside a query; clears count no samples.
device->CreateRenderTargetView(rt_resource.p, &rtv_desc, rtv1); // the resolve test reused it
list->BeginQuery(occlusion.p, D3D12_QUERY_TYPE_OCCLUSION, 0);
barrier = transition(rt_resource.p, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET);
list->ResourceBarrier(1, &barrier);
list->ClearRenderTargetView(rtv1, red, 1, &square);
list->EndQuery(so.p, D3D12_QUERY_TYPE_SO_STATISTICS_STREAM0, 0);
list->EndQuery(occlusion.p, D3D12_QUERY_TYPE_OCCLUSION, 0);
list->BeginQuery(occlusion.p, D3D12_QUERY_TYPE_BINARY_OCCLUSION, 1);
list->CopyBufferRegion(readback.p, 8192, upload.p, 0, 32768);
list->EndQuery(so.p, D3D12_QUERY_TYPE_SO_STATISTICS_STREAM3, 1);
list->EndQuery(occlusion.p, D3D12_QUERY_TYPE_BINARY_OCCLUSION, 1);
if (statistics.p) list->EndQuery(statistics.p, D3D12_QUERY_TYPE_PIPELINE_STATISTICS, 0);
list->EndQuery(timestamps.p, D3D12_QUERY_TYPE_TIMESTAMP, 1);
list->ResolveQueryData(timestamps.p, D3D12_QUERY_TYPE_TIMESTAMP, 0, 3, readback.p, ts_at); // 2: never
list->ResolveQueryData(occlusion.p, D3D12_QUERY_TYPE_OCCLUSION, 0, 1, readback.p, occlusion_at);
list->ResolveQueryData(occlusion.p, D3D12_QUERY_TYPE_BINARY_OCCLUSION, 1, 1, readback.p, occlusion_at + 8);
list->ResolveQueryData(occlusion.p, D3D12_QUERY_TYPE_OCCLUSION, 2, 2, readback.p, occlusion_at + 16);
if (statistics.p)
list->ResolveQueryData(statistics.p, D3D12_QUERY_TYPE_PIPELINE_STATISTICS, 0, 2, readback.p,
statistics_at);
list->ResolveQueryData(so.p, D3D12_QUERY_TYPE_SO_STATISTICS_STREAM0, 0, 1, readback.p, so_at);
list->ResolveQueryData(so.p, D3D12_QUERY_TYPE_SO_STATISTICS_STREAM3, 1, 2, readback.p, so_at + 16); // 2: never
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
const UINT64 t0 = u64(ts_at), t1 = u64(ts_at + 8);
EXPECT(t0 != 0 && t0 != 0xcdcdcdcdcdcdcdcdull && t1 >= t0);
EXPECT(double(t1 - t0) / double(frequency) < 10.0); // the copy took less than ten seconds
EXPECT(u64(ts_at + 16) == 0); // never written
for (UINT i = 0; i < 4; ++i) EXPECT(u64(occlusion_at + 8 * i) == 0);
for (UINT i = 0; statistics.p && i < 22; ++i) EXPECT(u64(statistics_at + 8 * i) == 0);
EXPECT(u64(statistics.p ? statistics_at + 176 : statistics_at) == 0xcdcdcdcdcdcdcdcdull); // not past it
for (UINT i = 0; i < 6; ++i) EXPECT(u64(so_at + 8 * i) == 0); // written and needed, three queries
EXPECT(u64(so_at + 48) == 0xcdcdcdcdcdcdcdcdull); // not past them
// A second execution reuses timestamp 0 (reset before it is written again) and resolves timestamp 1,
// which keeps the result of the first execution.
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
list->EndQuery(timestamps.p, D3D12_QUERY_TYPE_TIMESTAMP, 0);
list->ResolveQueryData(timestamps.p, D3D12_QUERY_TYPE_TIMESTAMP, 0, 2, readback.p, again_at);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
EXPECT(u64(again_at) >= t1 && u64(again_at + 8) == t1);
// A timestamp written by one execution and resolved by the next, with nothing waited for in between, as
// a game resolves last frame's timestamps. The resolve reads the new value (on MoltenVK only because the
// queue orders the two; docs/d3d12_vulkan.md section 4.6).
{
constexpr UINT64 later_at = 2048;
Ref<ID3D12CommandAllocator> writer_allocator;
Ref<ID3D12GraphicsCommandList> writer;
EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
IID_PPV_ARGS(&writer_allocator.p))));
EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, writer_allocator.p,
nullptr, IID_PPV_ARGS(&writer.p))));
writer->CopyBufferRegion(readback.p, 8192, upload.p, 0, 32768);
writer->EndQuery(timestamps.p, D3D12_QUERY_TYPE_TIMESTAMP, 2);
EXPECT(writer->Close() == S_OK);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
list->ResolveQueryData(timestamps.p, D3D12_QUERY_TYPE_TIMESTAMP, 0, 3, readback.p, later_at);
EXPECT(list->Close() == S_OK);
ID3D12CommandList* const writers[] = {writer.p};
queue->ExecuteCommandLists(1, writers);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
EXPECT(u64(later_at) == u64(again_at) && u64(later_at + 8) == t1);
EXPECT(u64(later_at + 16) >= u64(again_at) && u64(later_at + 16) != 0xcdcdcdcdcdcdcdcdull);
}
// Lists that break the rules. Close reports what it can see; the rest is refused at execution, and
// nothing is submitted.
const auto refuse = [&](HRESULT close, leonida::ReplayResult result, auto&& record) {
if (FAILED(allocator->Reset()) || FAILED(list->Reset(allocator.p, nullptr))) return false;
record();
if (list->Close() != close) return false;
queue->ExecuteCommandLists(1, lists);
return rt::d3d12::last_execute_result(queue.p) == result;
};
using R = leonida::ReplayResult;
EXPECT(refuse(E_INVALIDARG, R::unsupported, // begun and never ended; invalid list is refused at execution
[&] { list->BeginQuery(occlusion.p, D3D12_QUERY_TYPE_OCCLUSION, 0); }));
EXPECT(refuse(E_INVALIDARG, R::unsupported, // timestamps have no Begin; invalid list is refused at execution
[&] { list->BeginQuery(timestamps.p, D3D12_QUERY_TYPE_TIMESTAMP, 0); }));
EXPECT(refuse(E_INVALIDARG, R::unsupported, // offset not a multiple of 8; invalid list is refused at execution
[&] { list->ResolveQueryData(timestamps.p, D3D12_QUERY_TYPE_TIMESTAMP, 0, 1, readback.p, 4); }));
EXPECT(refuse(S_OK, R::invalid_query, // past the end of the heap
[&] { list->ResolveQueryData(occlusion.p, D3D12_QUERY_TYPE_OCCLUSION, 3, 2, readback.p, 0); }));
EXPECT(refuse(S_OK, R::invalid_query, // a type the heap does not hold
[&] { list->EndQuery(occlusion.p, D3D12_QUERY_TYPE_TIMESTAMP, 0); }));
EXPECT(refuse(S_OK, R::invalid_query, [&] { // a stream-output query in an occlusion heap
list->BeginQuery(occlusion.p, D3D12_QUERY_TYPE_SO_STATISTICS_STREAM1, 0);
list->EndQuery(occlusion.p, D3D12_QUERY_TYPE_SO_STATISTICS_STREAM1, 0);
}));
EXPECT(refuse(E_INVALIDARG, R::unsupported, // a stream-output query begun and never ended; invalid list is refused
[&] { list->BeginQuery(so.p, D3D12_QUERY_TYPE_SO_STATISTICS_STREAM2, 0); }));
EXPECT(refuse(S_OK, R::invalid_query, // into a texture
[&] { list->ResolveQueryData(timestamps.p, D3D12_QUERY_TYPE_TIMESTAMP, 0, 1, rt_resource.p, 0); }));
EXPECT(refuse(S_OK, R::invalid_query, // past the end of the buffer
[&] { list->ResolveQueryData(timestamps.p, D3D12_QUERY_TYPE_TIMESTAMP, 0, 2, readback.p, 65528); }));
EXPECT(refuse(S_OK, R::success, [&] { // overlapping occlusion kinds share physical segments (D3D-252)
list->BeginQuery(occlusion.p, D3D12_QUERY_TYPE_OCCLUSION, 0);
list->BeginQuery(occlusion.p, D3D12_QUERY_TYPE_BINARY_OCCLUSION, 1);
list->EndQuery(occlusion.p, D3D12_QUERY_TYPE_BINARY_OCCLUSION, 1);
list->EndQuery(occlusion.p, D3D12_QUERY_TYPE_OCCLUSION, 0);
list->ResolveQueryData(occlusion.p, D3D12_QUERY_TYPE_OCCLUSION, 0, 1, readback.p, 0);
list->ResolveQueryData(occlusion.p, D3D12_QUERY_TYPE_BINARY_OCCLUSION, 1, 1, readback.p, 8);
}));
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
EXPECT(SUCCEEDED(list->Close()));
}
// An occlusion query resolved into a predicate, the way a renderer skips occluded work (docs/d3d12_vulkan.md
// sections 4.6 and 4.7). There are no draws yet, so the query counts 0 samples: EQUAL_ZERO skips the left
// clear and NOT_EQUAL_ZERO runs the right one. The predicate goes COPY_DEST -> PREDICATION -> COPY_DEST and is
// resolved into again, so validation sees both sides of the hand-off between the copy and conditional
// rendering's read.
if (!d.conditional_rendering) {
std::printf("predication: no VK_EXT_conditional_rendering, skipped\n");
} else {
Ref<ID3D12QueryHeap> occlusion;
Ref<ID3D12Resource> predicate, target;
const D3D12_QUERY_HEAP_DESC query_heap_desc{D3D12_QUERY_HEAP_TYPE_OCCLUSION, 1, 0};
EXPECT(SUCCEEDED(device->CreateQueryHeap(&query_heap_desc, IID_PPV_ARGS(&occlusion.p))));
EXPECT(SUCCEEDED(create(default_heap, buffer_desc(256), D3D12_RESOURCE_STATE_COPY_DEST, predicate)));
EXPECT(SUCCEEDED(create(default_heap,
texture_desc(size, size, DXGI_FORMAT_R8G8B8A8_UNORM,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
D3D12_RESOURCE_STATE_RENDER_TARGET, target)));
device->CreateRenderTargetView(target.p, nullptr, rtv1); // the lists that used this slot have run
UINT64 target_cursor = 0;
const auto target_place = place(device.p, target.p, 0, target_cursor);
const FLOAT green[4] = {0, 1, 0, 1};
const D3D12_RECT left{0, 0, LONG(size / 2), LONG(size)}, right{LONG(size / 2), 0, LONG(size), LONG(size)};
const auto resolve_predicate = [&] {
list->BeginQuery(occlusion.p, D3D12_QUERY_TYPE_BINARY_OCCLUSION, 0);
list->EndQuery(occlusion.p, D3D12_QUERY_TYPE_BINARY_OCCLUSION, 0);
list->ResolveQueryData(occlusion.p, D3D12_QUERY_TYPE_BINARY_OCCLUSION, 0, 1, predicate.p, 0);
};
// The predicate's hand-offs, through legacy barriers and through enhanced buffer barriers.
const auto hand_off = [&](bool enhanced, bool to_predicate) {
if (!enhanced) {
const auto copy_dest = D3D12_RESOURCE_STATE_COPY_DEST, predication = D3D12_RESOURCE_STATE_PREDICATION;
barrier = transition(predicate.p, to_predicate ? copy_dest : predication,
to_predicate ? predication : copy_dest);
list->ResourceBarrier(1, &barrier);
return;
}
D3D12_BUFFER_BARRIER buffer{};
buffer.SyncBefore = to_predicate ? D3D12_BARRIER_SYNC_COPY : D3D12_BARRIER_SYNC_PREDICATION;
buffer.SyncAfter = to_predicate ? D3D12_BARRIER_SYNC_PREDICATION : D3D12_BARRIER_SYNC_COPY;
buffer.AccessBefore = to_predicate ? D3D12_BARRIER_ACCESS_COPY_DEST : D3D12_BARRIER_ACCESS_PREDICATION;
buffer.AccessAfter = to_predicate ? D3D12_BARRIER_ACCESS_PREDICATION : D3D12_BARRIER_ACCESS_COPY_DEST;
buffer.pResource = predicate.p;
buffer.Offset = 0;
buffer.Size = UINT64_MAX;
D3D12_BARRIER_GROUP buffer_group{};
buffer_group.Type = D3D12_BARRIER_TYPE_BUFFER;
buffer_group.NumBarriers = 1;
buffer_group.pBufferBarriers = &buffer;
list7->Barrier(1, &buffer_group);
};
for (const bool enhanced : {false, true}) {
std::memset(down, 0xcd, 65536);
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
if (enhanced) {
barrier = transition(target.p, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET);
list->ResourceBarrier(1, &barrier);
}
list->ClearRenderTargetView(rtv1, red, 0, nullptr);
resolve_predicate();
hand_off(enhanced, true);
list->SetPredication(predicate.p, 0, D3D12_PREDICATION_OP_EQUAL_ZERO); // the value is 0: skipped
list->ClearRenderTargetView(rtv1, green, 1, &left);
list->SetPredication(predicate.p, 0, D3D12_PREDICATION_OP_NOT_EQUAL_ZERO); // runs
list->ClearRenderTargetView(rtv1, blue, 1, &right);
list->SetPredication(nullptr, 0, D3D12_PREDICATION_OP_EQUAL_ZERO);
hand_off(enhanced, false);
resolve_predicate(); // written again after conditional rendering has read it
barrier = transition(target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
{
const auto dst = footprint(readback.p, target_place);
const auto src = subresource(target.p, 0);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
const std::uint8_t red_texel[4] = {255, 0, 0, 255}, blue_texel[4] = {0, 0, 255, 255};
for (UINT row = 0; row < size; ++row)
for (UINT col = 0; col < size; ++col)
report(enhanced ? "predicated clear texel (enhanced barriers)" : "predicated clear texel",
row * size + col,
down + target_place.Offset + row * target_place.Footprint.RowPitch + col * 4,
col < size / 2 ? red_texel : blue_texel, 4);
}
}
// D3D-71: a default-heap buffer outside the arena (this device has none) starts zeroed, as D3D12 committed
// resources do. Its memory is first dirtied through an earlier buffer, which is then released so the next
// allocation is likely to reuse it. CREATE_NOT_ZEROED skips the fill; a copy into the same list still lands.
{
constexpr UINT64 bytes = 4096;
const D3D12_HEAP_PROPERTIES default_buffer_heap{D3D12_HEAP_TYPE_DEFAULT};
std::memset(up, 0xab, std::size_t(bytes));
{
Ref<ID3D12Resource> dirty;
EXPECT(SUCCEEDED(create(default_buffer_heap, buffer_desc(bytes), D3D12_RESOURCE_STATE_COMMON, dirty)));
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
list->CopyBufferRegion(dirty.p, 0, upload.p, 0, bytes);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(submit_and_wait());
}
Ref<ID3D12Resource> fresh, not_zeroed;
EXPECT(SUCCEEDED(create(default_buffer_heap, buffer_desc(bytes), D3D12_RESOURCE_STATE_COMMON, fresh)));
const D3D12_RESOURCE_DESC zero_desc = buffer_desc(bytes);
EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_buffer_heap, D3D12_HEAP_FLAG_CREATE_NOT_ZEROED,
&zero_desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(¬_zeroed.p))));
std::memset(down, 0xcd, std::size_t(2 * bytes));
EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, nullptr)));
list->CopyBufferRegion(readback.p, 0, fresh.p, 0, bytes);
list->CopyBufferRegion(not_zeroed.p, 0, upload.p, 0, bytes); // written before it is read
auto readable = transition(not_zeroed.p, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &readable);
list->CopyBufferRegion(readback.p, bytes, not_zeroed.p, 0, bytes);
EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(submit_and_wait());
const std::uint8_t zero[4] = {}, written[4] = {0xab, 0xab, 0xab, 0xab};
for (UINT64 i = 0; i < bytes; i += 4) report("zero-filled buffer byte", UINT(i), down + i, zero, 4);
for (UINT64 i = 0; i < bytes; i += 4)
report("NOT_ZEROED buffer byte after a copy", UINT(i), down + bytes + i, written, 4);
}
// Every child holds the proxy; once they are gone the proxy holds the only reference.
upload->Unmap(0, nullptr);
readback->Unmap(0, nullptr);
for (auto* r : {&rt_resource, &packed565, &ds, &untouched, &upload, &readback, &source, &whole, &part, &bc,
&line, &line_array, &line_target}) r->reset();
list7.reset();
list.reset();
allocator.reset();
queue.reset();
fence.reset();
rtv_heap.reset();
dsv_heap.reset();
EXPECT(rt::d3d12_xs::device_proxy_ref_count(device.p) == 1);
} // the device is released here, before the Vulkan device it runs on
EXPECT(!mismatches);
// CPU texture copies, once per path. A second D3D12 device on the same Vulkan device, told the extension is
// off, takes the staging path even on a GPU that has host image copy. The third path has the extension but
// treats every host-transfer image as one the driver gave no memory type (a GTX 1050 Ti, NVIDIA 582.78): the
// image is made again without host-transfer usage and copies on the staging path.
struct CopyPath {
const char* name;
bool host;
bool force_retry;
};
for (const CopyPath path : {CopyPath{"host image copy", true, false}, CopyPath{"staging copy", false, false},
CopyPath{"host image copy refused by the driver", true, true}}) {
const bool host = path.host;
if (host && !d.host_image_copy) {
std::printf("WriteToSubresource/ReadFromSubresource (%s): no VK_EXT_host_image_copy, skipped\n",
path.name);
continue;
}
rt::d3d12::VulkanDeviceInfo info{};
info.get_instance_proc = d.instance_proc;
info.instance = d.instance;
info.physical = d.physical;
info.device = d.device;
info.queue = d.queue;
info.queue_family = d.family;
info.host_image_copy = host;
info.force_host_transfer_retry = path.force_retry;
Ref<ID3D12Device> device;
EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&device.p))));
EXPECT(cpu_copies(device.p, d.bc, path.name) == 0);
}
{
Ref<ID3D12Device> removable;
EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&removable.p))));
Ref<ID3D12Device5> removable5;
EXPECT(removable.p && SUCCEEDED(removable->QueryInterface(IID_PPV_ARGS(&removable5.p))));
if (removable5.p) {
EXPECT(removable5->GetDeviceRemovedReason() == S_OK);
removable5->RemoveDevice();
EXPECT(removable5->GetDeviceRemovedReason() == DXGI_ERROR_DEVICE_REMOVED);
}
}
// The path D3D12XboxCreateDevice takes with LEONIDA_D3D12=vulkan: a device on its own Vulkan instance.
{
Ref<ID3D12Device> standalone;
EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&standalone.p))));
// What translated shaders can declare beyond Int64/Float64 (the index's `features`) is enabled wherever the GPU
// has it: image stores and loads without a format, gather offsets, LOD clamps, a vertex shader's layer and
// viewport. Creating a module that declares one without it breaks VUID-VkShaderModuleCreateInfo-pCode-08740.
const rt::d3d12::VulkanDeviceInfo* made = rt::d3d12::vulkan_device_info(standalone.p);
EXPECT(made != nullptr);
if (made) {
const auto features2 = reinterpret_cast<PFN_vkGetPhysicalDeviceFeatures2>(
made->get_instance_proc(made->instance, "vkGetPhysicalDeviceFeatures2"));
EXPECT(features2 != nullptr);
VkPhysicalDeviceVulkan12Features have12{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES};
VkPhysicalDeviceFeatures2 have{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &have12};
if (features2) features2(made->physical, &have);
const VkPhysicalDeviceFeatures& on = made->features;
EXPECT(on.shaderStorageImageWriteWithoutFormat == have.features.shaderStorageImageWriteWithoutFormat);
EXPECT(on.shaderStorageImageReadWithoutFormat == have.features.shaderStorageImageReadWithoutFormat);
EXPECT(on.shaderImageGatherExtended == have.features.shaderImageGatherExtended);
EXPECT(on.shaderResourceMinLod == have.features.shaderResourceMinLod);
EXPECT(made->shader_output_layer == (have12.shaderOutputLayer == VK_TRUE));
EXPECT(made->shader_output_viewport_index == (have12.shaderOutputViewportIndex == VK_TRUE));
// The pixel-shader extensions (barycentrics, the POPS interlock, maintenance8's texel offsets): each is
// enabled exactly when the GPU offers the extension and its feature. A structure is chained into the
// query only when its extension is offered.
const auto enumerate = reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
made->get_instance_proc(made->instance, "vkEnumerateDeviceExtensionProperties"));
EXPECT(enumerate != nullptr);
std::vector<VkExtensionProperties> offered;
std::uint32_t offered_count = 0;
if (enumerate && enumerate(made->physical, nullptr, &offered_count, nullptr) == VK_SUCCESS) {
offered.resize(offered_count);
enumerate(made->physical, nullptr, &offered_count, offered.data());
offered.resize(offered_count);
}
const auto has = [&](const char* name) {
return std::any_of(offered.begin(), offered.end(),
[&](const VkExtensionProperties& e) { return std::strcmp(e.extensionName, name) == 0; });
};
VkPhysicalDeviceFragmentShaderBarycentricFeaturesKHR bary{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADER_BARYCENTRIC_FEATURES_KHR};
VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT lock{
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADER_INTERLOCK_FEATURES_EXT};
VkPhysicalDeviceFeatures2 ext_query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};
if (has(VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME)) {
bary.pNext = ext_query.pNext;
ext_query.pNext = &bary;
}
if (has(VK_EXT_FRAGMENT_SHADER_INTERLOCK_EXTENSION_NAME)) {
lock.pNext = ext_query.pNext;
ext_query.pNext = &lock;
}
#ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME
VkPhysicalDeviceMaintenance8FeaturesKHR m8{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_8_FEATURES_KHR};
if (has(VK_KHR_MAINTENANCE_8_EXTENSION_NAME)) {
m8.pNext = ext_query.pNext;
ext_query.pNext = &m8;
}
#endif
VkPhysicalDeviceShaderClockFeaturesKHR clock{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_CLOCK_FEATURES_KHR};
if (has(VK_KHR_SHADER_CLOCK_EXTENSION_NAME)) {
clock.pNext = ext_query.pNext;
ext_query.pNext = &clock;
}
if (features2) features2(made->physical, &ext_query);
EXPECT(made->shader_subgroup_clock == (clock.shaderSubgroupClock == VK_TRUE));
EXPECT(made->shader_device_clock == (clock.shaderDeviceClock == VK_TRUE));
EXPECT(made->fragment_shader_barycentric == (bary.fragmentShaderBarycentric == VK_TRUE));
EXPECT(made->fragment_shader_interlock == (lock.fragmentShaderPixelInterlock == VK_TRUE));
#ifdef VK_KHR_MAINTENANCE_8_EXTENSION_NAME
EXPECT(made->maintenance8 == (m8.maintenance8 == VK_TRUE));
#endif
// The OPTIONS booleans answer what the layer will honour, not what the GPU offers (docs 4.1: a false
// "supported" is worse at boot; the PSO paths refuse these same missing features). Stencil ref: the
// layer enables no stencil export and the translator emits no stencil-ref write, so always FALSE.
D3D12_FEATURE_DATA_D3D12_OPTIONS answers{};
EXPECT(SUCCEEDED(standalone->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &answers,
sizeof(answers))));
EXPECT(answers.DoublePrecisionFloatShaderOps == BOOL(made->features.shaderFloat64));
EXPECT(answers.OutputMergerLogicOp == BOOL(made->features.logicOp));
EXPECT(answers.PSSpecifiedStencilRefSupported == FALSE);
EXPECT(answers.ROVsSupported == BOOL(made->fragment_shader_interlock));
EXPECT(answers.VPAndRTArrayIndexFromAnyShaderFeedingRasterizerSupportedWithoutGSEmulation ==
BOOL(made->shader_output_layer && made->shader_output_viewport_index));
}
D3D12_COMMAND_QUEUE_DESC queue_desc{};
Ref<ID3D12CommandQueue> queue;
Ref<ID3D12Fence> fence;
EXPECT(SUCCEEDED(standalone->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))));
EXPECT(SUCCEEDED(standalone->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))));
EXPECT(SUCCEEDED(queue->Signal(fence.p, 5)));
EXPECT(SUCCEEDED(fence->SetEventOnCompletion(5, nullptr)) && fence->GetCompletedValue() == 5);
UINT64 frequency = 0;
Ref<ID3D12QueryHeap> timestamps;
const D3D12_QUERY_HEAP_DESC query_desc{D3D12_QUERY_HEAP_TYPE_TIMESTAMP, 1, 0};
EXPECT(SUCCEEDED(queue->GetTimestampFrequency(&frequency)) && frequency);
EXPECT(SUCCEEDED(standalone->CreateQueryHeap(&query_desc, IID_PPV_ARGS(×tamps.p))));
// Heaps and placed resources: no proxy sits on these slots, so this is the game's call path. A heap reserves
// arena memory, and a placed buffer's console VA is where it sits in it.
D3D12_HEAP_PROPERTIES placed_props{};
placed_props.Type = D3D12_HEAP_TYPE_DEFAULT;
D3D12_HEAP_DESC placed_heap_desc{};
placed_heap_desc.Properties = placed_props;
placed_heap_desc.SizeInBytes = 256 * 1024;
placed_heap_desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
Ref<ID3D12Heap> placed_heap, again;
EXPECT(SUCCEEDED(standalone->CreateHeap(&placed_heap_desc, IID_PPV_ARGS(&placed_heap.p))));
EXPECT(placed_heap->GetDesc().SizeInBytes == placed_heap_desc.SizeInBytes &&
placed_heap->GetDesc().Properties.Type == D3D12_HEAP_TYPE_DEFAULT);
Ref<ID3D12Resource> placed_a, placed_b, placed_image, refused;
const D3D12_RESOURCE_DESC placed_buffer = buffer_desc(4096);
const D3D12_RESOURCE_DESC placed_texture =
texture_desc(4, 4, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE);
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(placed_heap.p, 0, &placed_buffer,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
IID_PPV_ARGS(&placed_a.p))));
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(placed_heap.p, 65536, &placed_buffer,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
IID_PPV_ARGS(&placed_b.p))));
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(placed_heap.p, 131072, &placed_texture,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&placed_image.p))));
const D3D12_GPU_VIRTUAL_ADDRESS placed_va = placed_a->GetGPUVirtualAddress();
EXPECT(placed_va && placed_va % D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT == 0);
EXPECT(placed_b->GetGPUVirtualAddress() == placed_va + 65536);
EXPECT(placed_image->GetGPUVirtualAddress() == 0); // only buffers are addressable, as in D3D12
// Off the 64 KiB grid, past the heap end, and non-ROW_MAJOR buffers: E_INVALIDARG, not placed somewhere wrong.
EXPECT(standalone->CreatePlacedResource(placed_heap.p, 512, &placed_buffer, D3D12_RESOURCE_STATE_COMMON,
nullptr, IID_PPV_ARGS(&refused.p)) == E_INVALIDARG);
EXPECT(standalone->CreatePlacedResource(placed_heap.p, 256 * 1024, &placed_buffer,
D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&refused.p)) == E_INVALIDARG);
D3D12_RESOURCE_DESC tiled_buf = buffer_desc(4096);
tiled_buf.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
EXPECT(standalone->CreatePlacedResource(placed_heap.p, 0, &tiled_buf, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&refused.p)) == E_INVALIDARG);
EXPECT(!refused.p);
// A placed buffer reads as zeroed through the copy path, at its console VA.
D3D12_HEAP_PROPERTIES readback_props{};
readback_props.Type = D3D12_HEAP_TYPE_UPLOAD;
const D3D12_RESOURCE_DESC readback_desc = buffer_desc(64);
Ref<ID3D12Resource> zeroes;
EXPECT(SUCCEEDED(standalone->CreateCommittedResource(&readback_props, D3D12_HEAP_FLAG_NONE, &readback_desc,
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
IID_PPV_ARGS(&zeroes.p))));
Ref<ID3D12CommandAllocator> placed_allocator;
Ref<ID3D12GraphicsCommandList> placed_list;
EXPECT(SUCCEEDED(standalone->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
IID_PPV_ARGS(&placed_allocator.p))));
EXPECT(SUCCEEDED(standalone->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, placed_allocator.p, nullptr,
IID_PPV_ARGS(&placed_list.p))));
ID3D12CommandList* placed_lists[] = {placed_list.p};
placed_list->CopyBufferRegion(zeroes.p, 0, placed_a.p, 0, 64);
EXPECT(SUCCEEDED(placed_list->Close()));
queue->ExecuteCommandLists(1, placed_lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(SUCCEEDED(queue->Signal(fence.p, 6)) && SUCCEEDED(fence->SetEventOnCompletion(6, nullptr)));
void* zero_bytes = nullptr;
EXPECT(SUCCEEDED(zeroes->Map(0, nullptr, &zero_bytes)));
report("placed buffer", 0, static_cast<std::uint8_t*>(zero_bytes), std::array<std::uint8_t, 64>{}.data(), 64);
zeroes->Unmap(0, nullptr);
// Placed buffers carry their console VA in VulkanContext (so VA-resolved commands like WriteBufferImmediate
// find them) and placed readback buffers mark host_readable (so the submission flushes GPU writes to host).
{
D3D12_HEAP_DESC rb_heap_desc{};
rb_heap_desc.Properties.Type = D3D12_HEAP_TYPE_READBACK;
rb_heap_desc.SizeInBytes = 65536;
rb_heap_desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
Ref<ID3D12Heap> rb_heap;
Ref<ID3D12Resource> placed_rb;
EXPECT(SUCCEEDED(standalone->CreateHeap(&rb_heap_desc, IID_PPV_ARGS(&rb_heap.p))));
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(rb_heap.p, 0, &placed_buffer,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&placed_rb.p))));
Ref<ID3D12GraphicsCommandList2> placed_list2;
EXPECT(SUCCEEDED(placed_list->QueryInterface(IID_PPV_ARGS(&placed_list2.p))));
EXPECT(SUCCEEDED(placed_allocator->Reset()) && SUCCEEDED(placed_list->Reset(placed_allocator.p, nullptr)));
const D3D12_WRITEBUFFERIMMEDIATE_PARAMETER wp{placed_va + 16, 0xCAFEBABEu};
const D3D12_WRITEBUFFERIMMEDIATE_MODE wm = D3D12_WRITEBUFFERIMMEDIATE_MODE_DEFAULT;
placed_list2->WriteBufferImmediate(1, &wp, &wm);
D3D12_RESOURCE_BARRIER b{};
b.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
b.Transition = {placed_a.p, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE};
placed_list->ResourceBarrier(1, &b);
placed_list->CopyBufferRegion(placed_rb.p, 0, placed_a.p, 0, 64);
EXPECT(SUCCEEDED(placed_list->Close()));
queue->ExecuteCommandLists(1, placed_lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(SUCCEEDED(queue->Signal(fence.p, 7)) && SUCCEEDED(fence->SetEventOnCompletion(7, nullptr)));
void* rb_bytes = nullptr;
EXPECT(SUCCEEDED(placed_rb->Map(0, nullptr, &rb_bytes)));
std::uint32_t word = 0;
std::memcpy(&word, static_cast<const std::uint8_t*>(rb_bytes) + 16, sizeof(word));
EXPECT(word == 0xCAFEBABEu);
placed_rb->Unmap(0, nullptr);
}
// The heap owns the range: releasing it puts the next heap of that size back at the same place.
placed_image.reset();
placed_b.reset();
placed_a.reset();
placed_heap.reset();
EXPECT(SUCCEEDED(standalone->CreateHeap(&placed_heap_desc, IID_PPV_ARGS(&again.p))));
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(again.p, 0, &placed_buffer, D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr, IID_PPV_ARGS(&placed_a.p))));
EXPECT(placed_a->GetGPUVirtualAddress() == placed_va);
// D3D-191: tiled placed resources (D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE and Xbox 0x101).
D3D12_HEAP_DESC tiled_heap_desc{};
tiled_heap_desc.Properties.Type = D3D12_HEAP_TYPE_DEFAULT;
tiled_heap_desc.SizeInBytes = 2 * 1024 * 1024;
tiled_heap_desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
Ref<ID3D12Heap> tiled_heap, upload_placed_heap;
EXPECT(SUCCEEDED(standalone->CreateHeap(&tiled_heap_desc, IID_PPV_ARGS(&tiled_heap.p))));
D3D12_HEAP_DESC upload_heap_desc = tiled_heap_desc;
upload_heap_desc.Properties.Type = D3D12_HEAP_TYPE_UPLOAD;
EXPECT(SUCCEEDED(standalone->CreateHeap(&upload_heap_desc, IID_PPV_ARGS(&upload_placed_heap.p))));
// D3D-238: heap descriptions the host refuses (RX 5700 and WARP; ALLOW_DISPLAY on an RTX 4070 SUPER and a
// UHD 770) and their valid neighbours, HARDWARE_PROTECTED among them. compare_invalid_arguments and
// compare_heap_neighbours check them against a host.
{
const D3D12_HEAP_FLAGS deny_all = D3D12_HEAP_FLAG_DENY_BUFFERS | D3D12_HEAP_FLAG_DENY_RT_DS_TEXTURES |
D3D12_HEAP_FLAG_DENY_NON_RT_DS_TEXTURES;
const D3D12_HEAP_DESC refused_heaps[] = {
heap_desc_of(4096),
heap_desc_of(0, {D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_UNKNOWN, D3D12_MEMORY_POOL_L0}),
heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT}, deny_all),
heap_desc_of(0, {D3D12_HEAP_TYPE(0)}),
heap_desc_of(0, {D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE,
D3D12_MEMORY_POOL_UNKNOWN}),
heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_WRITE_BACK}),
heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT}, D3D12_HEAP_FLAG_NONE, 0),
heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT}, D3D12_HEAP_FLAG_ALLOW_DISPLAY),
};
for (const D3D12_HEAP_DESC& hd : refused_heaps) EXPECT(heap_create(standalone.p, hd) == E_INVALIDARG);
const D3D12_HEAP_DESC accepted_heaps[] = {
heap_desc_of(0),
heap_desc_of(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT),
heap_desc_of(D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT, {D3D12_HEAP_TYPE_DEFAULT},
D3D12_HEAP_FLAG_NONE, D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT),
heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT}, D3D12_HEAP_FLAG_NONE, 1000),
heap_desc_of(0, {D3D12_HEAP_TYPE_UPLOAD}),
heap_desc_of(0, {D3D12_HEAP_TYPE_READBACK}),
heap_desc_of(0, {D3D12_HEAP_TYPE_CUSTOM, D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE, D3D12_MEMORY_POOL_L0}),
heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT}, D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS),
heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT},
D3D12_HEAP_FLAG_DENY_BUFFERS | D3D12_HEAP_FLAG_DENY_NON_RT_DS_TEXTURES),
heap_desc_of(0, {D3D12_HEAP_TYPE_DEFAULT}, D3D12_HEAP_FLAG_HARDWARE_PROTECTED),
};
for (const D3D12_HEAP_DESC& hd : accepted_heaps) EXPECT(heap_create(standalone.p, hd) == S_OK);
const int heap_mismatches = compare_heap_neighbours(standalone.p);
if (heap_mismatches < 0)
std::printf("CreateHeap valid neighbours: no host D3D12 device to compare with, skipped\n");
EXPECT(heap_mismatches <= 0);
const int heap_desc_mismatches = compare_heap_descs(standalone.p);
if (heap_desc_mismatches < 0)
std::printf("ID3D12Heap::GetDesc: no host D3D12 device to compare with, skipped\n");
EXPECT(heap_desc_mismatches <= 0);
const int placed_mismatches = compare_placed_reserved(standalone.p);
if (placed_mismatches < 0)
std::printf("CreatePlacedResource/CreateReservedResource arguments: no host D3D12 device to compare "
"with, skipped\n");
EXPECT(placed_mismatches <= 0);
}
auto tiled_2d = [&](UINT w, UINT h, DXGI_FORMAT fmt, UINT16 mips, D3D12_RESOURCE_FLAGS flags,
UINT64 align = 0) {
D3D12_RESOURCE_DESC d = texture_desc(w, h, fmt, flags, mips);
d.Alignment = align;
d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
return d;
};
const D3D12_RESOURCE_DESC tiled_rt_desc =
tiled_2d(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
const D3D12_RESOURCE_DESC tiled_bc_desc =
tiled_2d(256, 256, DXGI_FORMAT_BC1_UNORM, 0, D3D12_RESOURCE_FLAG_NONE);
const D3D12_RESOURCE_DESC tiled_ds_desc =
tiled_2d(256, 256, DXGI_FORMAT_D32_FLOAT, 1, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
D3D12_RESOURCE_DESC tiled_3d_desc =
tiled_2d(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D12_RESOURCE_FLAG_NONE);
tiled_3d_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
tiled_3d_desc.DepthOrArraySize = 32;
D3D12_RESOURCE_DESC xbox_tiled_desc =
tiled_2d(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, 1, D3D12_RESOURCE_FLAG_NONE);
xbox_tiled_desc.Layout = static_cast<D3D12_TEXTURE_LAYOUT>(0x101);
Ref<ID3D12Resource> tiled_rt, tiled_bc, tiled_ds, tiled_3d, xbox_tiled;
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(tiled_heap.p, 0, &tiled_rt_desc, D3D12_RESOURCE_STATE_COMMON,
nullptr, IID_PPV_ARGS(&tiled_rt.p))));
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(tiled_heap.p, 512 * 1024, &tiled_bc_desc,
D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&tiled_bc.p))));
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(tiled_heap.p, 640 * 1024, &tiled_ds_desc,
D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&tiled_ds.p))));
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(tiled_heap.p, 1024 * 1024, &tiled_3d_desc,
D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&tiled_3d.p))));
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(tiled_heap.p, 0, &xbox_tiled_desc, D3D12_RESOURCE_STATE_COMMON,
nullptr, IID_PPV_ARGS(&xbox_tiled.p))));
for (ID3D12Resource* r : {tiled_rt.p, tiled_bc.p, tiled_ds.p, tiled_3d.p}) {
const D3D12_RESOURCE_DESC got = r->GetDesc();
EXPECT(r->GetGPUVirtualAddress() == 0);
EXPECT(got.Layout == D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE);
EXPECT(got.Alignment == D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT);
EXPECT(got.MipLevels > 0);
D3D12_HEAP_PROPERTIES got_hp{};
D3D12_HEAP_FLAGS got_hf = D3D12_HEAP_FLAG_NONE;
EXPECT(SUCCEEDED(r->GetHeapProperties(&got_hp, &got_hf)));
EXPECT(got_hp.Type == D3D12_HEAP_TYPE_DEFAULT);
const D3D12_RESOURCE_ALLOCATION_INFO ai = standalone->GetResourceAllocationInfo(0, 1, &got);
UINT tiles = 0;
D3D12_TILE_SHAPE shape{};
standalone->GetResourceTiling(r, &tiles, nullptr, &shape, nullptr, 0, nullptr);
EXPECT(tiles > 0 && shape.WidthInTexels > 0 && ai.Alignment == D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT);
EXPECT(ai.SizeInBytes == UINT64(tiles) * D3D12_TILED_RESOURCE_TILE_SIZE_IN_BYTES);
}
EXPECT(static_cast<UINT>(xbox_tiled->GetDesc().Layout) == 0x101);
UINT xbox_tiles = 0;
standalone->GetResourceTiling(xbox_tiled.p, &xbox_tiles, nullptr, nullptr, nullptr, 0, nullptr);
EXPECT(xbox_tiles == 4);
// Refusals matching host D3D12: 1D, 4 KiB alignment, 96-bit, two-plane DS, invalid MSAA, upload heap, past-end.
D3D12_RESOURCE_DESC bad_1d = line_desc(256, 1, 1, DXGI_FORMAT_R8G8B8A8_UNORM);
bad_1d.Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
auto tiled_2d_msaa = [&](UINT samples, D3D12_RESOURCE_FLAGS flags, UINT64 align = 0) {
D3D12_RESOURCE_DESC d = tiled_2d(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, 1, flags, align);
d.SampleDesc.Count = samples;
return d;
};
const D3D12_RESOURCE_DESC bad_cases[] = {
bad_1d,
tiled_2d(16, 16, DXGI_FORMAT_R8G8B8A8_UNORM, 1, D3D12_RESOURCE_FLAG_NONE,
D3D12_SMALL_RESOURCE_PLACEMENT_ALIGNMENT),
tiled_2d(64, 64, DXGI_FORMAT_R32G32B32_FLOAT, 1, D3D12_RESOURCE_FLAG_NONE),
tiled_2d(64, 64, DXGI_FORMAT_D24_UNORM_S8_UINT, 1, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
tiled_2d(64, 64, DXGI_FORMAT_D32_FLOAT_S8X24_UINT, 1, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
tiled_2d_msaa(2, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET),
tiled_2d_msaa(4, D3D12_RESOURCE_FLAG_NONE),
tiled_2d_msaa(4, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT),
};
for (const D3D12_RESOURCE_DESC& bad : bad_cases) {
Ref<ID3D12Resource> fail;
EXPECT(standalone->CreatePlacedResource(tiled_heap.p, 0, &bad, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&fail.p)) == E_INVALIDARG);
EXPECT(!fail.p);
}
{
Ref<ID3D12Resource> fail;
EXPECT(standalone->CreatePlacedResource(upload_placed_heap.p, 0, &tiled_rt_desc,
D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&fail.p)) == E_INVALIDARG);
const D3D12_RESOURCE_ALLOCATION_INFO rt_alloc = standalone->GetResourceAllocationInfo(0, 1, &tiled_rt_desc);
EXPECT(standalone->CreatePlacedResource(tiled_heap.p,
tiled_heap_desc.SizeInBytes - rt_alloc.SizeInBytes + 65536,
&tiled_rt_desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&fail.p)) == E_INVALIDARG);
EXPECT(!fail.p);
}
// Clear and read back a placed tiled RTV to verify GPU execution.
D3D12_DESCRIPTOR_HEAP_DESC tiled_rtv_desc{};
tiled_rtv_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
tiled_rtv_desc.NumDescriptors = 1;
Ref<ID3D12DescriptorHeap> tiled_rtv_heap;
EXPECT(SUCCEEDED(standalone->CreateDescriptorHeap(&tiled_rtv_desc, IID_PPV_ARGS(&tiled_rtv_heap.p))));
standalone->CreateRenderTargetView(tiled_rt.p, nullptr, tiled_rtv_heap->GetCPUDescriptorHandleForHeapStart());
D3D12_HEAP_PROPERTIES rb_props{D3D12_HEAP_TYPE_READBACK};
const D3D12_RESOURCE_DESC rb_desc = buffer_desc(256);
Ref<ID3D12Resource> tiled_rb;
EXPECT(SUCCEEDED(standalone->CreateCommittedResource(&rb_props, D3D12_HEAP_FLAG_NONE, &rb_desc,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&tiled_rb.p))));
Ref<ID3D12CommandAllocator> tiled_alloc;
Ref<ID3D12GraphicsCommandList> tiled_list;
EXPECT(SUCCEEDED(standalone->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
IID_PPV_ARGS(&tiled_alloc.p))));
EXPECT(SUCCEEDED(standalone->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, tiled_alloc.p, nullptr,
IID_PPV_ARGS(&tiled_list.p))));
const FLOAT tiled_color[4] = {64.0f / 255.0f, 128.0f / 255.0f, 191.0f / 255.0f, 1.0f};
D3D12_RESOURCE_BARRIER b = transition(tiled_rt.p, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_RENDER_TARGET);
tiled_list->ResourceBarrier(1, &b);
tiled_list->ClearRenderTargetView(tiled_rtv_heap->GetCPUDescriptorHandleForHeapStart(), tiled_color, 0, nullptr);
b = transition(tiled_rt.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
tiled_list->ResourceBarrier(1, &b);
D3D12_PLACED_SUBRESOURCE_FOOTPRINT fp{};
fp.Footprint = {DXGI_FORMAT_R8G8B8A8_UNORM, 1, 1, 1, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT};
const D3D12_TEXTURE_COPY_LOCATION copy_dst = footprint(tiled_rb.p, fp);
const D3D12_TEXTURE_COPY_LOCATION copy_src = subresource(tiled_rt.p, 0);
D3D12_BOX box{0, 0, 0, 1, 1, 1};
tiled_list->CopyTextureRegion(©_dst, 0, 0, 0, ©_src, &box);
EXPECT(SUCCEEDED(tiled_list->Close()));
ID3D12CommandList* tiled_lists[] = {tiled_list.p};
queue->ExecuteCommandLists(1, tiled_lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(SUCCEEDED(queue->Signal(fence.p, 8)) && SUCCEEDED(fence->SetEventOnCompletion(8, nullptr)));
void* tiled_bytes = nullptr;
EXPECT(SUCCEEDED(tiled_rb->Map(0, nullptr, &tiled_bytes)));
const std::uint8_t want_rgba[4] = {64, 128, 191, 255};
report("tiled placed RTV", 0, static_cast<std::uint8_t*>(tiled_bytes), want_rgba, 4);
EXPECT(std::memcmp(tiled_bytes, want_rgba, 4) == 0);
tiled_rb->Unmap(0, nullptr);
// Compare placed 64KB_UNDEFINED_SWIZZLE acceptance, layout, alignment, heap props and tile shape with host.
Ref<ID3D12Device> host_dev;
if (create_host_device(host_dev)) {
D3D12_FEATURE_DATA_D3D12_OPTIONS host_opts{};
if (SUCCEEDED(host_dev->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &host_opts, sizeof(host_opts))) &&
host_opts.TiledResourcesTier != D3D12_TILED_RESOURCES_TIER_NOT_SUPPORTED) {
Ref<ID3D12Heap> host_heap;
if (SUCCEEDED(host_dev->CreateHeap(&tiled_heap_desc, IID_PPV_ARGS(&host_heap.p)))) {
const D3D12_RESOURCE_DESC host_cases[] = {
tiled_2d(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, 1, D3D12_RESOURCE_FLAG_NONE),
tiled_2d(256, 256, DXGI_FORMAT_D32_FLOAT, 1, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
tiled_3d_desc,
};
for (int idx = 0; idx < int(std::size(host_cases)); ++idx) {
const D3D12_RESOURCE_DESC& host_desc = host_cases[idx];
if (host_desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D &&
host_opts.TiledResourcesTier < D3D12_TILED_RESOURCES_TIER_3)
continue;
Ref<ID3D12Resource> our_r, host_r;
const HRESULT our_hr = standalone->CreatePlacedResource(
tiled_heap.p, 0, &host_desc, D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&our_r.p));
const HRESULT host_hr = host_dev->CreatePlacedResource(
host_heap.p, 0, &host_desc, D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&host_r.p));
if (FAILED(host_hr)) continue;
EXPECT(SUCCEEDED(our_hr));
EXPECT(our_r->GetDesc().Layout == host_r->GetDesc().Layout);
EXPECT(our_r->GetDesc().Alignment == host_r->GetDesc().Alignment);
D3D12_HEAP_PROPERTIES ohp{}, hhp{};
EXPECT(SUCCEEDED(our_r->GetHeapProperties(&ohp, nullptr)) &&
SUCCEEDED(host_r->GetHeapProperties(&hhp, nullptr)));
EXPECT(ohp.Type == hhp.Type);
D3D12_TILE_SHAPE os{}, hs{};
standalone->GetResourceTiling(our_r.p, nullptr, nullptr, &os, nullptr, 0, nullptr);
host_dev->GetResourceTiling(host_r.p, nullptr, nullptr, &hs, nullptr, 0, nullptr);
EXPECT(os.WidthInTexels == hs.WidthInTexels && os.HeightInTexels == hs.HeightInTexels &&
os.DepthInTexels == hs.DepthInTexels);
}
}
}
}
// D3D-222: multisampled placed textures (UNKNOWN and 4x tiled).
D3D12_HEAP_DESC msaa_heap_desc{};
msaa_heap_desc.Properties.Type = D3D12_HEAP_TYPE_DEFAULT;
msaa_heap_desc.SizeInBytes = 16 * 1024 * 1024;
msaa_heap_desc.Alignment = D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT;
Ref<ID3D12Heap> msaa_heap;
EXPECT(SUCCEEDED(standalone->CreateHeap(&msaa_heap_desc, IID_PPV_ARGS(&msaa_heap.p))));
D3D12_HEAP_DESC deny_rt_desc = tiled_heap_desc;
deny_rt_desc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES;
Ref<ID3D12Heap> deny_rt_heap;
EXPECT(SUCCEEDED(standalone->CreateHeap(&deny_rt_desc, IID_PPV_ARGS(&deny_rt_heap.p))));
auto msaa_desc = [&](UINT w, UINT h, UINT samples, UINT64 align, D3D12_TEXTURE_LAYOUT layout,
D3D12_RESOURCE_FLAGS flags, DXGI_FORMAT fmt = DXGI_FORMAT_R8G8B8A8_UNORM,
UINT16 mips = 1) {
D3D12_RESOURCE_DESC d = texture_desc(w, h, fmt, flags, mips);
d.Alignment = align;
d.SampleDesc.Count = samples;
d.Layout = layout;
return d;
};
const D3D12_RESOURCE_DESC msaa_unk_rt_desc =
msaa_desc(256, 256, 4, 0, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
const D3D12_RESOURCE_DESC msaa_unk_small_desc = msaa_desc(
256, 256, 4, D3D12_SMALL_MSAA_RESOURCE_PLACEMENT_ALIGNMENT, D3D12_TEXTURE_LAYOUT_UNKNOWN,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
const D3D12_RESOURCE_DESC msaa_unk_ds_desc =
msaa_desc(256, 256, 4, 0, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL,
DXGI_FORMAT_D32_FLOAT);
const D3D12_RESOURCE_DESC msaa_tiled_rt_desc = tiled_2d_msaa(4, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
D3D12_RESOURCE_DESC msaa_xbox_rt_desc = msaa_tiled_rt_desc;
msaa_xbox_rt_desc.Layout = static_cast<D3D12_TEXTURE_LAYOUT>(0x101);
Ref<ID3D12Resource> msaa_unk_rt, msaa_unk_small, msaa_unk_ds, msaa_tiled_rt, msaa_xbox_rt, msaa_cast_rt;
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(msaa_heap.p, D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT,
&msaa_unk_rt_desc, D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&msaa_unk_rt.p))));
EXPECT(msaa_unk_rt->GetGPUVirtualAddress() == 0);
EXPECT(msaa_unk_rt->GetDesc().Alignment == D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT);
EXPECT(msaa_unk_rt->GetDesc().SampleDesc.Count == 4);
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(tiled_heap.p, 65536, &msaa_unk_small_desc,
D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&msaa_unk_small.p))));
EXPECT(msaa_unk_small->GetDesc().Alignment == D3D12_SMALL_MSAA_RESOURCE_PLACEMENT_ALIGNMENT);
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(msaa_heap.p, 8 * 1024 * 1024, &msaa_unk_ds_desc,
D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&msaa_unk_ds.p))));
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(tiled_heap.p, 65536, &msaa_tiled_rt_desc,
D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&msaa_tiled_rt.p))));
EXPECT(msaa_tiled_rt->GetDesc().Alignment == D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT);
UINT msaa_tiles = 0;
D3D12_TILE_SHAPE msaa_shape{};
standalone->GetResourceTiling(msaa_tiled_rt.p, &msaa_tiles, nullptr, &msaa_shape, nullptr, 0, nullptr);
EXPECT(msaa_tiles == 16 && msaa_shape.WidthInTexels == 64 && msaa_shape.HeightInTexels == 64);
EXPECT(SUCCEEDED(standalone->CreatePlacedResource(tiled_heap.p, 65536, &msaa_xbox_rt_desc,
D3D12_RESOURCE_STATE_COMMON, nullptr,
IID_PPV_ARGS(&msaa_xbox_rt.p))));
EXPECT(msaa_xbox_rt->GetDesc().Alignment == D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT);
EXPECT(static_cast<UINT>(msaa_xbox_rt->GetDesc().Layout) == 0x101);
{
Ref<ID3D12Device10> standalone10;
EXPECT(SUCCEEDED(standalone->QueryInterface(IID_PPV_ARGS(&standalone10.p))));
D3D12_RESOURCE_DESC1 cast_msaa{};
cast_msaa.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
cast_msaa.Width = 64;
cast_msaa.Height = 64;
cast_msaa.DepthOrArraySize = 1;
cast_msaa.MipLevels = 1;
cast_msaa.Format = DXGI_FORMAT_R8G8B8A8_TYPELESS;
cast_msaa.SampleDesc.Count = 4;
cast_msaa.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
const DXGI_FORMAT cast_list[] = {DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB};
EXPECT(SUCCEEDED(standalone10->CreatePlacedResource2(
msaa_heap.p, 0, &cast_msaa, D3D12_BARRIER_LAYOUT_COMMON, nullptr, 2, cast_list,
IID_PPV_ARGS(&msaa_cast_rt.p))));
}
// Refusals matching host D3D12 for MSAA placed resources.
{
struct BadMsaaCase {
ID3D12Heap* heap;
UINT64 offset;
D3D12_RESOURCE_DESC desc;
};
D3D12_RESOURCE_DESC msaa_3d =
msaa_desc(16, 16, 4, 0, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
msaa_3d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
msaa_3d.DepthOrArraySize = 8;
const BadMsaaCase bad_msaa[] = {
// 4 MiB-aligned UNKNOWN MSAA fails on a 64 KiB heap (even at offset 0) and at misaligned offsets.
{tiled_heap.p, 0, msaa_unk_rt_desc},
{msaa_heap.p, 65536, msaa_unk_rt_desc},
{msaa_heap.p, msaa_heap_desc.SizeInBytes - 65536, msaa_unk_small_desc},
{upload_placed_heap.p, 0, msaa_unk_small_desc},
{deny_rt_heap.p, 0, msaa_unk_small_desc},
{deny_rt_heap.p, 0, tiled_rt_desc},
{msaa_heap.p, 0,
msaa_desc(256, 256, 4, 0, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE)},
{msaa_heap.p, 0,
msaa_desc(256, 256, 4, 0, D3D12_TEXTURE_LAYOUT_UNKNOWN,
D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)},
{msaa_heap.p, 0,
msaa_desc(64, 64, 4, 0, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET,
DXGI_FORMAT_R8G8B8A8_UNORM, 0)},
{msaa_heap.p, 0, msaa_3d},
};
for (const BadMsaaCase& bad : bad_msaa) {
Ref<ID3D12Resource> fail;
EXPECT(standalone->CreatePlacedResource(bad.heap, bad.offset, &bad.desc, D3D12_RESOURCE_STATE_COMMON,
nullptr, IID_PPV_ARGS(&fail.p)) == E_INVALIDARG);
EXPECT(!fail.p);
}
D3D12_HEAP_DESC only_rt_desc = tiled_heap_desc;
only_rt_desc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES;
Ref<ID3D12Heap> only_rt_heap;
EXPECT(SUCCEEDED(standalone->CreateHeap(&only_rt_desc, IID_PPV_ARGS(&only_rt_heap.p))));
for (const D3D12_RESOURCE_DESC& bad_only_rt :
{texture_desc(64, 64, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 1),
buffer_desc(4096)}) {
Ref<ID3D12Resource> fail;
EXPECT(standalone->CreatePlacedResource(only_rt_heap.p, 0, &bad_only_rt, D3D12_RESOURCE_STATE_COMMON,
nullptr, IID_PPV_ARGS(&fail.p)) == E_INVALIDARG);
EXPECT(!fail.p);
}
}
// Clear placed 4x MSAA RTVs (UNKNOWN + tiled) and DSV, resolve, and read back to verify GPU execution.
{
const D3D12_RESOURCE_DESC resolved_desc =
texture_desc(256, 256, DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RESOURCE_FLAG_NONE, 1);
Ref<ID3D12Resource> msaa_resolved_unk, msaa_resolved_tiled;
D3D12_HEAP_PROPERTIES def_hp{D3D12_HEAP_TYPE_DEFAULT};
EXPECT(SUCCEEDED(standalone->CreateCommittedResource(&def_hp, D3D12_HEAP_FLAG_NONE, &resolved_desc,
D3D12_RESOURCE_STATE_RESOLVE_DEST, nullptr,
IID_PPV_ARGS(&msaa_resolved_unk.p))));
EXPECT(SUCCEEDED(standalone->CreateCommittedResource(&def_hp, D3D12_HEAP_FLAG_NONE, &resolved_desc,
D3D12_RESOURCE_STATE_RESOLVE_DEST, nullptr,
IID_PPV_ARGS(&msaa_resolved_tiled.p))));
D3D12_DESCRIPTOR_HEAP_DESC msaa_rtv_hd{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 2, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
D3D12_DESCRIPTOR_HEAP_DESC msaa_dsv_hd{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
Ref<ID3D12DescriptorHeap> msaa_rtv_heap, msaa_dsv_heap;
EXPECT(SUCCEEDED(standalone->CreateDescriptorHeap(&msaa_rtv_hd, IID_PPV_ARGS(&msaa_rtv_heap.p))));
EXPECT(SUCCEEDED(standalone->CreateDescriptorHeap(&msaa_dsv_hd, IID_PPV_ARGS(&msaa_dsv_heap.p))));
const UINT rtv_step = standalone->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
const D3D12_CPU_DESCRIPTOR_HANDLE rtv0 = msaa_rtv_heap->GetCPUDescriptorHandleForHeapStart();
const D3D12_CPU_DESCRIPTOR_HANDLE rtv1{rtv0.ptr + rtv_step};
standalone->CreateRenderTargetView(msaa_unk_rt.p, nullptr, rtv0);
standalone->CreateRenderTargetView(msaa_tiled_rt.p, nullptr, rtv1);
standalone->CreateDepthStencilView(msaa_unk_ds.p, nullptr, msaa_dsv_heap->GetCPUDescriptorHandleForHeapStart());
EXPECT(SUCCEEDED(tiled_alloc->Reset()) && SUCCEEDED(tiled_list->Reset(tiled_alloc.p, nullptr)));
D3D12_RESOURCE_BARRIER barriers[] = {
transition(msaa_unk_rt.p, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_RENDER_TARGET),
transition(msaa_tiled_rt.p, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_RENDER_TARGET),
transition(msaa_unk_ds.p, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_DEPTH_WRITE),
};
tiled_list->ResourceBarrier(3, barriers);
tiled_list->ClearRenderTargetView(rtv0, tiled_color, 0, nullptr);
tiled_list->ClearRenderTargetView(rtv1, tiled_color, 0, nullptr);
tiled_list->ClearDepthStencilView(msaa_dsv_heap->GetCPUDescriptorHandleForHeapStart(),
D3D12_CLEAR_FLAG_DEPTH, 0.5f, 0, 0, nullptr);
D3D12_RESOURCE_BARRIER to_resolve[] = {
transition(msaa_unk_rt.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_RESOLVE_SOURCE),
transition(msaa_tiled_rt.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_RESOLVE_SOURCE),
};
tiled_list->ResourceBarrier(2, to_resolve);
tiled_list->ResolveSubresource(msaa_resolved_unk.p, 0, msaa_unk_rt.p, 0, DXGI_FORMAT_R8G8B8A8_UNORM);
tiled_list->ResolveSubresource(msaa_resolved_tiled.p, 0, msaa_tiled_rt.p, 0, DXGI_FORMAT_R8G8B8A8_UNORM);
D3D12_RESOURCE_BARRIER to_copy[] = {
transition(msaa_resolved_unk.p, D3D12_RESOURCE_STATE_RESOLVE_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE),
transition(msaa_resolved_tiled.p, D3D12_RESOURCE_STATE_RESOLVE_DEST, D3D12_RESOURCE_STATE_COPY_SOURCE),
};
tiled_list->ResourceBarrier(2, to_copy);
Ref<ID3D12Resource> msaa_rb;
const D3D12_RESOURCE_DESC msaa_rb_desc = buffer_desc(1024);
EXPECT(SUCCEEDED(standalone->CreateCommittedResource(&rb_props, D3D12_HEAP_FLAG_NONE, &msaa_rb_desc,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&msaa_rb.p))));
D3D12_PLACED_SUBRESOURCE_FOOTPRINT fp_unk{}, fp_tiled{};
fp_unk.Offset = 0;
fp_unk.Footprint = {DXGI_FORMAT_R8G8B8A8_UNORM, 1, 1, 1, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT};
fp_tiled = fp_unk;
fp_tiled.Offset = D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT;
const D3D12_TEXTURE_COPY_LOCATION dst_unk = footprint(msaa_rb.p, fp_unk);
const D3D12_TEXTURE_COPY_LOCATION src_unk = subresource(msaa_resolved_unk.p, 0);
const D3D12_TEXTURE_COPY_LOCATION dst_tiled = footprint(msaa_rb.p, fp_tiled);
const D3D12_TEXTURE_COPY_LOCATION src_tiled = subresource(msaa_resolved_tiled.p, 0);
tiled_list->CopyTextureRegion(&dst_unk, 0, 0, 0, &src_unk, &box);
tiled_list->CopyTextureRegion(&dst_tiled, 0, 0, 0, &src_tiled, &box);
EXPECT(SUCCEEDED(tiled_list->Close()));
queue->ExecuteCommandLists(1, tiled_lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(SUCCEEDED(queue->Signal(fence.p, 9)) && SUCCEEDED(fence->SetEventOnCompletion(9, nullptr)));
void* msaa_bytes = nullptr;
EXPECT(SUCCEEDED(msaa_rb->Map(0, nullptr, &msaa_bytes)));
const auto* out_bytes = static_cast<const std::uint8_t*>(msaa_bytes);
report("placed MSAA UNKNOWN RTV resolve", 0, out_bytes, want_rgba, 4);
report("placed MSAA tiled RTV resolve", 0, out_bytes + D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT, want_rgba, 4);
EXPECT(std::memcmp(out_bytes, want_rgba, 4) == 0);
EXPECT(std::memcmp(out_bytes + D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT, want_rgba, 4) == 0);
msaa_rb->Unmap(0, nullptr);
}
// Host parity for MSAA placed acceptance/refusals, layout, alignment, heap flags and tiled 4x shape.
if (host_dev.p) {
Ref<ID3D12Heap> host_heap64, host_heap4m;
if (SUCCEEDED(host_dev->CreateHeap(&tiled_heap_desc, IID_PPV_ARGS(&host_heap64.p))) &&
SUCCEEDED(host_dev->CreateHeap(&msaa_heap_desc, IID_PPV_ARGS(&host_heap4m.p)))) {
struct MsaaParityCase {
ID3D12Heap* our_heap;
ID3D12Heap* host_heap;
UINT64 offset;
D3D12_RESOURCE_DESC desc;
};
const MsaaParityCase parity[] = {
{tiled_heap.p, host_heap64.p, 0, msaa_unk_rt_desc},
{msaa_heap.p, host_heap4m.p, 65536, msaa_unk_rt_desc},
{msaa_heap.p, host_heap4m.p, 0, msaa_unk_rt_desc},
{tiled_heap.p, host_heap64.p, 65536, msaa_unk_small_desc},
{tiled_heap.p, host_heap64.p, 65536, msaa_tiled_rt_desc},
};
for (const MsaaParityCase& mc : parity) {
Ref<ID3D12Resource> our_r, host_r;
const HRESULT our_hr = standalone->CreatePlacedResource(
mc.our_heap, mc.offset, &mc.desc, D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&our_r.p));
const HRESULT host_hr = host_dev->CreatePlacedResource(
mc.host_heap, mc.offset, &mc.desc, D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&host_r.p));
EXPECT(SUCCEEDED(our_hr) == SUCCEEDED(host_hr));
if (FAILED(host_hr)) {
EXPECT(our_hr == host_hr);
continue;
}
EXPECT(our_r->GetDesc().Layout == host_r->GetDesc().Layout);
EXPECT(our_r->GetDesc().Alignment == host_r->GetDesc().Alignment);
EXPECT(our_r->GetDesc().SampleDesc.Count == host_r->GetDesc().SampleDesc.Count);
D3D12_HEAP_PROPERTIES ohp{}, hhp{};
EXPECT(SUCCEEDED(our_r->GetHeapProperties(&ohp, nullptr)) &&
SUCCEEDED(host_r->GetHeapProperties(&hhp, nullptr)));
EXPECT(ohp.Type == hhp.Type);
if (mc.desc.Layout == D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE) {
D3D12_TILE_SHAPE os{}, hs{};
standalone->GetResourceTiling(our_r.p, nullptr, nullptr, &os, nullptr, 0, nullptr);
host_dev->GetResourceTiling(host_r.p, nullptr, nullptr, &hs, nullptr, 0, nullptr);
EXPECT(os.WidthInTexels == hs.WidthInTexels && os.HeightInTexels == hs.HeightInTexels &&
os.DepthInTexels == hs.DepthInTexels);
}
}
}
}
}
// D3D-90, the placed half of castable formats: CreatePlacedResource2 takes NumCastableFormats exactly as
// CreateCommittedResource3 does (#693). A castable list on a placed texture makes a mutable image whose views
// may cast: an SRV descriptor is written only for listed formats, an RTV in a listed family format clears
// through the cast, and the cleared bytes are compared with the host PC runtime's (WARP when the default host
// adapter cannot cast). Refused lists give the host's HRESULT, and a placement without a list still works.
#define CAST_CHECK(condition) \
do { \
if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return false; } \
} while (0)
const auto placed_cast_scenario = [](ID3D12Device10* device10, bool layer, std::uint8_t* out) -> bool {
D3D12_HEAP_DESC heap_desc{};
heap_desc.Properties.Type = D3D12_HEAP_TYPE_DEFAULT;
heap_desc.SizeInBytes = 256 * 1024;
heap_desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
Ref<ID3D12Heap> heap;
CAST_CHECK(SUCCEEDED(device10->CreateHeap(&heap_desc, IID_PPV_ARGS(&heap.p))));
D3D12_RESOURCE_DESC1 texture{};
texture.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
texture.Width = 4;
texture.Height = 4;
texture.MipLevels = 1;
texture.DepthOrArraySize = 1;
texture.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texture.SampleDesc.Count = 1;
texture.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
const DXGI_FORMAT srgb_list[] = {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB};
Ref<ID3D12Resource> srgb, plain;
CAST_CHECK(SUCCEEDED(device10->CreatePlacedResource2(heap.p, 0, &texture, D3D12_BARRIER_LAYOUT_COMMON,
nullptr, 1, srgb_list, IID_PPV_ARGS(&srgb.p))));
// The control: a count with a null list is no list, so this placement works as CreatePlacedResource's.
CAST_CHECK(SUCCEEDED(device10->CreatePlacedResource2(heap.p, 65536, &texture, D3D12_BARRIER_LAYOUT_COMMON,
nullptr, 2, nullptr, IID_PPV_ARGS(&plain.p))));
D3D12_DESCRIPTOR_HEAP_DESC rtv_heap_desc{};
rtv_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
rtv_heap_desc.NumDescriptors = 2;
Ref<ID3D12DescriptorHeap> rtv_heap;
CAST_CHECK(SUCCEEDED(device10->CreateDescriptorHeap(&rtv_heap_desc, IID_PPV_ARGS(&rtv_heap.p))));
const UINT rtv_stride = device10->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
D3D12_CPU_DESCRIPTOR_HANDLE rtv = rtv_heap->GetCPUDescriptorHandleForHeapStart();
D3D12_RENDER_TARGET_VIEW_DESC srgb_view{}, plain_view{};
srgb_view.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
plain_view.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
srgb_view.ViewDimension = plain_view.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
device10->CreateRenderTargetView(srgb.p, &srgb_view, rtv);
device10->CreateRenderTargetView(plain.p, &plain_view, {rtv.ptr + rtv_stride});
D3D12_COMMAND_QUEUE_DESC queue_desc{};
Ref<ID3D12CommandQueue> queue;
Ref<ID3D12Fence> fence;
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
CAST_CHECK(SUCCEEDED(device10->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p))));
CAST_CHECK(SUCCEEDED(device10->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p))));
CAST_CHECK(SUCCEEDED(device10->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
IID_PPV_ARGS(&allocator.p))));
CAST_CHECK(SUCCEEDED(device10->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
IID_PPV_ARGS(&list.p))));
const FLOAT quarter[4] = {0.25f, 0.5f, 0.75f, 1.0f};
D3D12_RESOURCE_BARRIER barrier = transition(srgb.p, D3D12_RESOURCE_STATE_COMMON,
D3D12_RESOURCE_STATE_RENDER_TARGET);
list->ResourceBarrier(1, &barrier);
list->ClearRenderTargetView(rtv, quarter, 0, nullptr);
barrier = transition(srgb.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
barrier = transition(plain.p, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_RENDER_TARGET);
list->ResourceBarrier(1, &barrier);
list->ClearRenderTargetView({rtv.ptr + rtv_stride}, quarter, 0, nullptr);
barrier = transition(plain.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
list->ResourceBarrier(1, &barrier);
D3D12_HEAP_PROPERTIES readback_heap{D3D12_HEAP_TYPE_READBACK};
const D3D12_RESOURCE_DESC readback_desc = buffer_desc(2048);
Ref<ID3D12Resource> readback;
CAST_CHECK(SUCCEEDED(device10->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE,
&readback_desc, D3D12_RESOURCE_STATE_COPY_DEST,
nullptr, IID_PPV_ARGS(&readback.p))));
for (const auto& [resource, offset] : {std::tuple{srgb.p, 0u}, std::tuple{plain.p, 1024u}}) {
D3D12_PLACED_SUBRESOURCE_FOOTPRINT placed{};
placed.Offset = offset;
placed.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
placed.Footprint.Width = 4;
placed.Footprint.Height = 4;
placed.Footprint.Depth = 1;
placed.Footprint.RowPitch = 256;
const D3D12_TEXTURE_COPY_LOCATION dst = footprint(readback.p, placed);
const D3D12_TEXTURE_COPY_LOCATION src = subresource(resource, 0);
list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
}
CAST_CHECK(SUCCEEDED(list->Close()));
ID3D12CommandList* lists[] = {list.p};
queue->ExecuteCommandLists(1, lists);
if (layer) CAST_CHECK(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
CAST_CHECK(SUCCEEDED(queue->Signal(fence.p, 1)));
CAST_CHECK(SUCCEEDED(fence->SetEventOnCompletion(1, nullptr)));
void* bytes = nullptr;
CAST_CHECK(SUCCEEDED(readback->Map(0, nullptr, &bytes)));
std::memcpy(out, bytes, 2048);
readback->Unmap(0, nullptr);
return true;
};
// The refusal answers, also run on the host: a castable list on a buffer, an unmapped format, and a cast
// whose texel size differs from the resource's.
const auto placed_cast_refusals = [](ID3D12Device10* device10, std::array<HRESULT, 3>& out) -> bool {
D3D12_HEAP_DESC heap_desc{};
heap_desc.Properties.Type = D3D12_HEAP_TYPE_DEFAULT;
heap_desc.SizeInBytes = 256 * 1024;
heap_desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
Ref<ID3D12Heap> heap;
if (FAILED(device10->CreateHeap(&heap_desc, IID_PPV_ARGS(&heap.p)))) return false;
D3D12_RESOURCE_DESC1 texture{};
texture.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
texture.Width = 4;
texture.Height = 4;
texture.MipLevels = 1;
texture.DepthOrArraySize = 1;
texture.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texture.SampleDesc.Count = 1;
texture.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
D3D12_RESOURCE_DESC1 buffer{};
buffer.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
buffer.Width = 4096;
buffer.Height = 1;
buffer.DepthOrArraySize = 1;
buffer.MipLevels = 1;
buffer.SampleDesc.Count = 1;
buffer.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
const DXGI_FORMAT srgb_list[] = {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB};
const DXGI_FORMAT unknown_list[] = {DXGI_FORMAT_UNKNOWN};
const DXGI_FORMAT wide_list[] = {DXGI_FORMAT_R16G16B16A16_FLOAT};
Ref<ID3D12Resource> refused;
out[0] = device10->CreatePlacedResource2(heap.p, 0, &buffer, D3D12_BARRIER_LAYOUT_UNDEFINED, nullptr, 1,
srgb_list, IID_PPV_ARGS(&refused.p));
out[1] = device10->CreatePlacedResource2(heap.p, 0, &texture, D3D12_BARRIER_LAYOUT_COMMON, nullptr, 1,
unknown_list, IID_PPV_ARGS(&refused.p));
out[2] = device10->CreatePlacedResource2(heap.p, 0, &texture, D3D12_BARRIER_LAYOUT_COMMON, nullptr, 1,
wide_list, IID_PPV_ARGS(&refused.p));
return !refused.p;
};
{
Ref<ID3D12Device> cast;
EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&cast.p))));
Ref<ID3D12Device10> cast10;
EXPECT(SUCCEEDED(cast.p->QueryInterface(IID_PPV_ARGS(&cast10.p))));
std::array<std::uint8_t, 2048> ours{}, host{};
std::array<HRESULT, 3> our_refusals{};
EXPECT(placed_cast_scenario(cast10.p, true, ours.data()));
EXPECT(placed_cast_refusals(cast10.p, our_refusals));
EXPECT(our_refusals[0] == E_INVALIDARG && our_refusals[1] == E_INVALIDARG);
// CPU-side: the SRV allow-list on placed textures, and the RTV's, as on the committed path.
{
D3D12_HEAP_DESC heap_desc{};
heap_desc.Properties.Type = D3D12_HEAP_TYPE_DEFAULT;
heap_desc.SizeInBytes = 256 * 1024;
heap_desc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
Ref<ID3D12Heap> heap;
EXPECT(SUCCEEDED(cast10->CreateHeap(&heap_desc, IID_PPV_ARGS(&heap.p))));
D3D12_RESOURCE_DESC1 texture{};
texture.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
texture.Width = 4;
texture.Height = 4;
texture.MipLevels = 1;
texture.DepthOrArraySize = 1;
texture.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
texture.SampleDesc.Count = 1;
texture.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
const DXGI_FORMAT srgb_list[] = {DXGI_FORMAT_R8G8B8A8_UNORM_SRGB};
Ref<ID3D12Resource> listed;
EXPECT(SUCCEEDED(cast10->CreatePlacedResource2(heap.p, 0, &texture, D3D12_BARRIER_LAYOUT_COMMON,
nullptr, 1, srgb_list, IID_PPV_ARGS(&listed.p))));
EXPECT(listed->GetGPUVirtualAddress() == 0); // a placed texture keeps a null VA, castable or not
D3D12_RESOURCE_DESC1 uint_texture = texture;
uint_texture.Format = DXGI_FORMAT_R8G8B8A8_UINT;
const DXGI_FORMAT uint_list[] = {DXGI_FORMAT_R32_UINT};
Ref<ID3D12Resource> cross_family;
EXPECT(SUCCEEDED(cast10->CreatePlacedResource2(heap.p, 65536, &uint_texture,
D3D12_BARRIER_LAYOUT_COMMON, nullptr, 1, uint_list,
IID_PPV_ARGS(&cross_family.p))));
D3D12_DESCRIPTOR_HEAP_DESC srv_heap_desc{};
srv_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
srv_heap_desc.NumDescriptors = 3;
srv_heap_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
Ref<ID3D12DescriptorHeap> srv_heap;
EXPECT(SUCCEEDED(cast10->CreateDescriptorHeap(&srv_heap_desc, IID_PPV_ARGS(&srv_heap.p))));
const UINT stride = cast10->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
D3D12_CPU_DESCRIPTOR_HANDLE slot = srv_heap->GetCPUDescriptorHandleForHeapStart();
D3D12_SHADER_RESOURCE_VIEW_DESC srv{};
srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srv.Texture2D.MipLevels = 1;
const auto slot_nonzero = [&]() {
std::uint32_t words[8]{};
std::memcpy(words, reinterpret_cast<void*>(slot.ptr), sizeof(words));
for (std::uint32_t word : words)
if (word) return true;
return false;
};
srv.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; // listed: written
cast.p->CreateShaderResourceView(listed.p, &srv, slot);
EXPECT(slot_nonzero());
srv.Format = DXGI_FORMAT_R8G8B8A8_UINT; // unlisted: a null descriptor
slot.ptr += stride;
cast.p->CreateShaderResourceView(listed.p, &srv, slot);
std::uint32_t words[8]{};
std::memcpy(words, reinterpret_cast<void*>(slot.ptr), sizeof(words));
EXPECT(words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 && words[4] == 0 &&
words[5] == 0 && words[6] == 0 && words[7] == 0);
srv.Format = DXGI_FORMAT_R32_UINT; // a listed same-size cross-family cast: written
slot.ptr += stride;
cast.p->CreateShaderResourceView(cross_family.p, &srv, slot);
EXPECT(slot_nonzero());
// The RTV allow-list: a listed family format is a view, an unlisted one is not.
D3D12_DESCRIPTOR_HEAP_DESC rtv_heap_desc{};
rtv_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
rtv_heap_desc.NumDescriptors = 2;
Ref<ID3D12DescriptorHeap> rtv_heap;
EXPECT(SUCCEEDED(cast10->CreateDescriptorHeap(&rtv_heap_desc, IID_PPV_ARGS(&rtv_heap.p))));
const UINT rtv_stride = cast10->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
D3D12_CPU_DESCRIPTOR_HANDLE rtv = rtv_heap->GetCPUDescriptorHandleForHeapStart();
D3D12_RENDER_TARGET_VIEW_DESC rtv_desc{};
rtv_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
cast.p->CreateRenderTargetView(listed.p, &rtv_desc, rtv);
std::memcpy(words, reinterpret_cast<void*>(rtv.ptr), sizeof(words));
EXPECT(words[0] != 0 || words[1] != 0);
rtv_desc.Format = DXGI_FORMAT_R8G8B8A8_UINT; // unlisted
cast.p->CreateRenderTargetView(listed.p, &rtv_desc, {rtv.ptr + rtv_stride});
std::memcpy(words, reinterpret_cast<void*>(rtv.ptr + rtv_stride), sizeof(words));
EXPECT(words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0);
}
// The tolerance host_side applies, pinned here so every GPU checks it: its accept side only runs where the
// host rounds differently (Intel's SRGB clear), so a change to it would otherwise stay green elsewhere.
// One code either side of a near-tie is accepted; two codes, or a code past 0.6 ULP, are not.
EXPECT(within_06_ulp(187, 188, ideal_srgb(0.5) * 255.0) && within_06_ulp(127, 128, 127.5));
EXPECT(!within_06_ulp(186, 188, ideal_srgb(0.5) * 255.0) && !within_06_ulp(126, 128, 127.5));
EXPECT(!within_06_ulp(224, 225, ideal_srgb(0.75) * 255.0) &&
!within_06_ulp(136, 137, ideal_srgb(0.25) * 255.0));
EXPECT(!within_06_ulp(254, 255, 255.0));
// The host PC runtime's bytes and refusals: the default adapter when it can cast, WARP otherwise.
bool compared = false;
const char* source = "no host";
Ref<ID3D12Device> host_device;
const auto host_side = [&](ID3D12Device10* device10, bool same_adapter) {
std::array<HRESULT, 3> host_refusals{};
if (!placed_cast_scenario(device10, false, host.data())) return false;
if (!placed_cast_refusals(device10, host_refusals)) return false;
unsigned differences = 0;
// D3D12 does not pin float -> UNORM or float -> SRGB to one code: the D3D11.3 functional spec
// (3.2.3.6 FLOAT -> UNORM, 3.2.3.8 FLOAT -> SRGB) permits a tolerance of 0.6 ULP on the integer side, so
// a value near halfway between two codes may map to either. The plain clear's 0.5 * 255 = 127.5 is a
// tie: WARP and AMD give the upper code, NVIDIA's D3D12 driver and its Vulkan driver the lower (RTX 3080
// 616.64, RTX 3090 610.88 and RTX 5070 Laptop 617.42). The SRGB-cast clear's 0.5 encodes to 187.516:
// WARP and NVIDIA's Vulkan driver give 188, Intel's Vulkan driver 187 (UHD 770 101.7092), both within
// 0.6 ULP. On the layer's own adapter (same LUID) the comparison stays exact; against another adapter
// or WARP a byte may differ when both sides are within 0.6 ULP of the ideal value.
// The plain clear's normalised values are rounded by the layer itself (nearest even, WARP's and AMD's
// code), so on its own NVIDIA adapter the tie byte may be the step above the driver's: accepted only
// when it is exactly that nearest-even code and within 0.6 ULP.
const FLOAT clear_value[4] = {0.25f, 0.5f, 0.75f, 1.0f}; // placed_cast_scenario's clear colour
const auto within_tolerance = [&](std::uint8_t ours_byte, std::uint8_t host_byte, double ideal) {
return !same_adapter && within_06_ulp(ours_byte, host_byte, ideal);
};
unsigned plain_within = 0, srgb_within = 0;
for (UINT row = 0; row < 4; ++row)
for (UINT i = 0; i < 16; ++i) {
const UINT a = row * 256 + i, b = 1024 + row * 256 + i;
const double c = clear_value[i % 4];
const double plain_ideal = c * 255.0;
const double srgb_ideal = (i % 4 == 3 ? c : ideal_srgb(c)) * 255.0; // alpha stays linear
if (ours[b] != host[b]) {
const bool layer_tie = double(ours[b]) == nearest_even_code(plain_ideal) &&
within_06_ulp(ours[b], host[b], plain_ideal);
if (within_tolerance(ours[b], host[b], plain_ideal) || layer_tie)
++plain_within;
else if (differences++ < 8)
std::fprintf(stderr, "placed plain clear row %u byte %u: ours %02x, host %02x\n", row, i,
ours[b], host[b]);
}
if (ours[a] != host[a]) {
if (within_tolerance(ours[a], host[a], srgb_ideal))
++srgb_within;
else if (differences++ < 8)
std::fprintf(stderr, "placed SRGB-cast clear row %u byte %u: ours %02x, host %02x\n",
row, i, ours[a], host[a]);
}
}
if (plain_within || srgb_within)
std::printf("placed clears: %u plain and %u SRGB-cast bytes differ from the host by one code, both "
"within the 0.6 ULP tolerance of D3D11.3 spec 3.2.3.6 and 3.2.3.8\n",
plain_within, srgb_within);
for (UINT i = 0; i < 3; ++i)
if (our_refusals[i] != host_refusals[i]) {
++differences;
std::fprintf(stderr, "placed castable refusal %u: ours %08lx, host %08lx\n", i,
static_cast<unsigned long>(our_refusals[i]),
static_cast<unsigned long>(host_refusals[i]));
}
return differences == 0;
};
const auto can_cast = [&](ID3D12Device* device, Ref<ID3D12Device10>& out10) {
D3D12_FEATURE_DATA_D3D12_OPTIONS12 options12{};
return SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&out10.p))) &&
SUCCEEDED(out10->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS12, &options12,
sizeof(options12))) &&
options12.RelaxedFormatCastingSupported;
};
// The host is the layer's own adapter (matched by LUID, then PCI ids) unless LEONIDA_HOST_ADAPTER names
// one: drivers round float -> UNORM ties differently, so the same GPU is the fair reference.
std::string host_adapter, host_source;
char host_override[8] = {};
const bool named = GetEnvironmentVariableA("LEONIDA_HOST_ADAPTER", host_override, sizeof(host_override)) > 0;
const bool have_host =
named ? create_host_device(host_device, &host_adapter)
: create_host_device_for_physical(d.instance, d.physical, d.instance_proc, host_device, host_adapter);
if (have_host) {
VkPhysicalDeviceIDProperties id{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES};
VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &id};
d.instance_fn<PFN_vkGetPhysicalDeviceProperties2>("vkGetPhysicalDeviceProperties2")(d.physical, &properties);
const LUID host_luid = host_device->GetAdapterLuid();
const bool same_adapter =
id.deviceLUIDValid && std::memcmp(&host_luid, id.deviceLUID, sizeof(host_luid)) == 0;
Ref<ID3D12Device10> host10;
if (can_cast(host_device.p, host10)) {
EXPECT(host_side(host10.p, same_adapter));
compared = true;
host_source = "the host runtime on " + host_adapter +
(same_adapter ? " (the layer's own adapter)" : " (a different adapter from the layer's)");
source = host_source.c_str();
}
}
std::string warp_name;
if (!compared) {
// WARP, the host path that casts when the default adapter cannot. LEONIDA_HOST_ADAPTER is saved
// and restored: it belongs to whoever runs the test.
char previous[128] = {};
const BOOL had = GetEnvironmentVariableA("LEONIDA_HOST_ADAPTER", previous, sizeof(previous));
SetEnvironmentVariableA("LEONIDA_HOST_ADAPTER", "warp");
copycmp::Ptr<ID3D12Device> warp;
Ref<ID3D12Device10> warp10;
if (copycmp::try_create_host(warp, warp_name) && can_cast(warp.p, warp10)) {
EXPECT(host_side(warp10.p, false));
compared = true;
source = warp_name.c_str();
}
SetEnvironmentVariableA("LEONIDA_HOST_ADAPTER", had && previous[0] ? previous : nullptr);
}
std::printf("placed castable formats (D3D-90): SRGB-cast and plain clears read back and compared with %s; "
"refusals %s\n",
source, compared ? "matched the host's" : "checked on the layer alone");
}
#undef CAST_CHECK
if (d.validation_errors()) {
std::fprintf(stderr, "%u validation errors\n", d.validation_errors());
return 1;
}
std::printf("Vulkan device behind the Xbox-ABI proxy: clears, zero-filled first use, uploads, texture, buffer and "
"BC copies, a resolve, reserved resources, queries, GetCopyableFootprints, GetResourceTiling and "
"CheckFeatureSupport error paths and fence semantics all matched\n");
return 0;
}
|