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 | /**************************************************************************/
/* rendering_device_driver_d3d12.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "rendering_device_driver_d3d12.h"
#include "core/config/project_settings.h"
#include "core/io/marshalls.h"
#include "servers/rendering/rendering_device.h"
#include "thirdparty/zlib/zlib.h"
#include "d3d12_godot_nir_bridge.h"
#include "dxil_hash.h"
#include "rendering_context_driver_d3d12.h"
// No point in fighting warnings in Mesa.
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable : 4200) // "nonstandard extension used: zero-sized array in struct/union".
#pragma warning(disable : 4806) // "'&': unsafe operation: no value of type 'bool' promoted to type 'uint32_t' can equal the given constant".
#endif
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
#pragma GCC diagnostic ignored "-Wshadow"
#pragma GCC diagnostic ignored "-Wswitch"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#elif defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
#pragma clang diagnostic ignored "-Wstring-plus-int"
#pragma clang diagnostic ignored "-Wswitch"
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
#endif
#include "nir_spirv.h"
#include "nir_to_dxil.h"
#include "spirv_to_dxil.h"
extern "C" {
#include "dxil_spirv_nir.h"
}
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#elif defined(__clang__)
#pragma clang diagnostic pop
#endif
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#if !defined(_MSC_VER)
#include <guiddef.h>
#include <dxguids.h>
#endif
// Mesa may define this.
#ifdef UNUSED
#undef UNUSED
#endif
#ifdef PIX_ENABLED
#if defined(__GNUC__)
#define _MSC_VER 1800
#endif
#define USE_PIX
#include "WinPixEventRuntime/pix3.h"
#if defined(__GNUC__)
#undef _MSC_VER
#endif
#endif
static const D3D12_RANGE VOID_RANGE = {};
static const uint32_t ROOT_CONSTANT_REGISTER = GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER * (RDD::MAX_UNIFORM_SETS + 1);
static const uint32_t RUNTIME_DATA_REGISTER = GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER * (RDD::MAX_UNIFORM_SETS + 2);
/*****************/
/**** GENERIC ****/
/*****************/
// NOTE: RD's packed format names are reversed in relation to DXGI's; e.g.:.
// - DATA_FORMAT_A8B8G8R8_UNORM_PACK32 -> DXGI_FORMAT_R8G8B8A8_UNORM (packed; note ABGR vs. RGBA).
// - DATA_FORMAT_B8G8R8A8_UNORM -> DXGI_FORMAT_B8G8R8A8_UNORM (not packed; note BGRA order matches).
// TODO: Add YUV formats properly, which would require better support for planes in the RD API.
const RenderingDeviceDriverD3D12::D3D12Format RenderingDeviceDriverD3D12::RD_TO_D3D12_FORMAT[RDD::DATA_FORMAT_MAX] = {
/* DATA_FORMAT_R4G4_UNORM_PACK8 */ {},
/* DATA_FORMAT_R4G4B4A4_UNORM_PACK16 */ { DXGI_FORMAT_B4G4R4A4_UNORM, DXGI_FORMAT_B4G4R4A4_UNORM, D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(1, 2, 3, 0) },
/* DATA_FORMAT_B4G4R4A4_UNORM_PACK16 */ { DXGI_FORMAT_B4G4R4A4_UNORM, DXGI_FORMAT_B4G4R4A4_UNORM, D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(3, 2, 1, 0) },
/* DATA_FORMAT_R5G6B5_UNORM_PACK16 */ { DXGI_FORMAT_B5G6R5_UNORM, DXGI_FORMAT_B5G6R5_UNORM },
/* DATA_FORMAT_B5G6R5_UNORM_PACK16 */ { DXGI_FORMAT_B5G6R5_UNORM, DXGI_FORMAT_B5G6R5_UNORM, D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(2, 1, 0, 3) },
/* DATA_FORMAT_R5G5B5A1_UNORM_PACK16 */ { DXGI_FORMAT_B5G6R5_UNORM, DXGI_FORMAT_B5G5R5A1_UNORM, D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(1, 2, 3, 0) },
/* DATA_FORMAT_B5G5R5A1_UNORM_PACK16 */ { DXGI_FORMAT_B5G6R5_UNORM, DXGI_FORMAT_B5G5R5A1_UNORM, D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(3, 2, 1, 0) },
/* DATA_FORMAT_A1R5G5B5_UNORM_PACK16 */ { DXGI_FORMAT_B5G6R5_UNORM, DXGI_FORMAT_B5G5R5A1_UNORM },
/* DATA_FORMAT_R8_UNORM */ { DXGI_FORMAT_R8_TYPELESS, DXGI_FORMAT_R8_UNORM },
/* DATA_FORMAT_R8_SNORM */ { DXGI_FORMAT_R8_TYPELESS, DXGI_FORMAT_R8_SNORM },
/* DATA_FORMAT_R8_USCALED */ { DXGI_FORMAT_R8_TYPELESS, DXGI_FORMAT_R8_UINT },
/* DATA_FORMAT_R8_SSCALED */ { DXGI_FORMAT_R8_TYPELESS, DXGI_FORMAT_R8_SINT },
/* DATA_FORMAT_R8_UINT */ { DXGI_FORMAT_R8_TYPELESS, DXGI_FORMAT_R8_UINT },
/* DATA_FORMAT_R8_SINT */ { DXGI_FORMAT_R8_TYPELESS, DXGI_FORMAT_R8_SINT },
/* DATA_FORMAT_R8_SRGB */ {},
/* DATA_FORMAT_R8G8_UNORM */ { DXGI_FORMAT_R8G8_TYPELESS, DXGI_FORMAT_R8G8_UNORM },
/* DATA_FORMAT_R8G8_SNORM */ { DXGI_FORMAT_R8G8_TYPELESS, DXGI_FORMAT_R8G8_SNORM },
/* DATA_FORMAT_R8G8_USCALED */ { DXGI_FORMAT_R8G8_TYPELESS, DXGI_FORMAT_R8G8_UINT },
/* DATA_FORMAT_R8G8_SSCALED */ { DXGI_FORMAT_R8G8_TYPELESS, DXGI_FORMAT_R8G8_SINT },
/* DATA_FORMAT_R8G8_UINT */ { DXGI_FORMAT_R8G8_TYPELESS, DXGI_FORMAT_R8G8_UINT },
/* DATA_FORMAT_R8G8_SINT */ { DXGI_FORMAT_R8G8_TYPELESS, DXGI_FORMAT_R8G8_SINT },
/* DATA_FORMAT_R8G8_SRGB */ {},
/* DATA_FORMAT_R8G8B8_UNORM */ {},
/* DATA_FORMAT_R8G8B8_SNORM */ {},
/* DATA_FORMAT_R8G8B8_USCALED */ {},
/* DATA_FORMAT_R8G8B8_SSCALED */ {},
/* DATA_FORMAT_R8G8B8_UINT */ {},
/* DATA_FORMAT_R8G8B8_SINT */ {},
/* DATA_FORMAT_R8G8B8_SRGB */ {},
/* DATA_FORMAT_B8G8R8_UNORM */ {},
/* DATA_FORMAT_B8G8R8_SNORM */ {},
/* DATA_FORMAT_B8G8R8_USCALED */ {},
/* DATA_FORMAT_B8G8R8_SSCALED */ {},
/* DATA_FORMAT_B8G8R8_UINT */ {},
/* DATA_FORMAT_B8G8R8_SINT */ {},
/* DATA_FORMAT_B8G8R8_SRGB */ {},
/* DATA_FORMAT_R8G8B8A8_UNORM */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_UNORM },
/* DATA_FORMAT_R8G8B8A8_SNORM */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_SNORM },
/* DATA_FORMAT_R8G8B8A8_USCALED */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_UINT },
/* DATA_FORMAT_R8G8B8A8_SSCALED */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_SINT },
/* DATA_FORMAT_R8G8B8A8_UINT */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_UINT },
/* DATA_FORMAT_R8G8B8A8_SINT */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_SINT },
/* DATA_FORMAT_R8G8B8A8_SRGB */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB },
/* DATA_FORMAT_B8G8R8A8_UNORM */ { DXGI_FORMAT_B8G8R8A8_TYPELESS, DXGI_FORMAT_B8G8R8A8_UNORM },
/* DATA_FORMAT_B8G8R8A8_SNORM */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_SNORM },
/* DATA_FORMAT_B8G8R8A8_USCALED */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_UINT },
/* DATA_FORMAT_B8G8R8A8_SSCALED */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_SINT },
/* DATA_FORMAT_B8G8R8A8_UINT */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_UINT },
/* DATA_FORMAT_B8G8R8A8_SINT */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_SINT },
/* DATA_FORMAT_B8G8R8A8_SRGB */ { DXGI_FORMAT_B8G8R8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB },
/* DATA_FORMAT_A8B8G8R8_UNORM_PACK32 */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_UNORM },
/* DATA_FORMAT_A8B8G8R8_SNORM_PACK32 */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_SNORM },
/* DATA_FORMAT_A8B8G8R8_USCALED_PACK32 */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_UINT },
/* DATA_FORMAT_A8B8G8R8_SSCALED_PACK32 */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_SINT },
/* DATA_FORMAT_A8B8G8R8_UINT_PACK32 */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_UINT },
/* DATA_FORMAT_A8B8G8R8_SINT_PACK32 */ { DXGI_FORMAT_R8G8B8A8_TYPELESS, DXGI_FORMAT_R8G8B8A8_SINT },
/* DATA_FORMAT_A8B8G8R8_SRGB_PACK32 */ { DXGI_FORMAT_B8G8R8A8_TYPELESS, DXGI_FORMAT_B8G8R8A8_UNORM_SRGB },
/* DATA_FORMAT_A2R10G10B10_UNORM_PACK32 */ { DXGI_FORMAT_R10G10B10A2_TYPELESS, DXGI_FORMAT_R10G10B10A2_UNORM, D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(2, 1, 0, 3) },
/* DATA_FORMAT_A2R10G10B10_SNORM_PACK32 */ {},
/* DATA_FORMAT_A2R10G10B10_USCALED_PACK32 */ { DXGI_FORMAT_R10G10B10A2_TYPELESS, DXGI_FORMAT_R10G10B10A2_UINT, D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(2, 1, 0, 3) },
/* DATA_FORMAT_A2R10G10B10_SSCALED_PACK32 */ {},
/* DATA_FORMAT_A2R10G10B10_UINT_PACK32 */ { DXGI_FORMAT_R10G10B10A2_TYPELESS, DXGI_FORMAT_R10G10B10A2_UINT, D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(2, 1, 0, 3) },
/* DATA_FORMAT_A2R10G10B10_SINT_PACK32 */ {},
/* DATA_FORMAT_A2B10G10R10_UNORM_PACK32 */ { DXGI_FORMAT_R10G10B10A2_TYPELESS, DXGI_FORMAT_R10G10B10A2_UNORM },
/* DATA_FORMAT_A2B10G10R10_SNORM_PACK32 */ {},
/* DATA_FORMAT_A2B10G10R10_USCALED_PACK32 */ { DXGI_FORMAT_R10G10B10A2_TYPELESS, DXGI_FORMAT_R10G10B10A2_UINT },
/* DATA_FORMAT_A2B10G10R10_SSCALED_PACK32 */ {},
/* DATA_FORMAT_A2B10G10R10_UINT_PACK32 */ { DXGI_FORMAT_R10G10B10A2_TYPELESS, DXGI_FORMAT_R10G10B10A2_UINT },
/* DATA_FORMAT_A2B10G10R10_SINT_PACK32 */ {},
/* DATA_FORMAT_R16_UNORM */ { DXGI_FORMAT_R16_TYPELESS, DXGI_FORMAT_R16_UNORM },
/* DATA_FORMAT_R16_SNORM */ { DXGI_FORMAT_R16_TYPELESS, DXGI_FORMAT_R16_SNORM },
/* DATA_FORMAT_R16_USCALED */ { DXGI_FORMAT_R16_TYPELESS, DXGI_FORMAT_R16_UINT },
/* DATA_FORMAT_R16_SSCALED */ { DXGI_FORMAT_R16_TYPELESS, DXGI_FORMAT_R16_SINT },
/* DATA_FORMAT_R16_UINT */ { DXGI_FORMAT_R16_TYPELESS, DXGI_FORMAT_R16_UINT },
/* DATA_FORMAT_R16_SINT */ { DXGI_FORMAT_R16_TYPELESS, DXGI_FORMAT_R16_SINT },
/* DATA_FORMAT_R16_SFLOAT */ { DXGI_FORMAT_R16_TYPELESS, DXGI_FORMAT_R16_FLOAT },
/* DATA_FORMAT_R16G16_UNORM */ { DXGI_FORMAT_R16G16_TYPELESS, DXGI_FORMAT_R16G16_UNORM },
/* DATA_FORMAT_R16G16_SNORM */ { DXGI_FORMAT_R16G16_TYPELESS, DXGI_FORMAT_R16G16_SNORM },
/* DATA_FORMAT_R16G16_USCALED */ { DXGI_FORMAT_R16G16_TYPELESS, DXGI_FORMAT_R16G16_UINT },
/* DATA_FORMAT_R16G16_SSCALED */ { DXGI_FORMAT_R16G16_TYPELESS, DXGI_FORMAT_R16G16_SINT },
/* DATA_FORMAT_R16G16_UINT */ { DXGI_FORMAT_R16G16_TYPELESS, DXGI_FORMAT_R16G16_UINT },
/* DATA_FORMAT_R16G16_SINT */ { DXGI_FORMAT_R16G16_TYPELESS, DXGI_FORMAT_R16G16_SINT },
/* DATA_FORMAT_R16G16_SFLOAT */ { DXGI_FORMAT_R16G16_TYPELESS, DXGI_FORMAT_R16G16_FLOAT },
/* DATA_FORMAT_R16G16B16_UNORM */ {},
/* DATA_FORMAT_R16G16B16_SNORM */ {},
/* DATA_FORMAT_R16G16B16_USCALED */ {},
/* DATA_FORMAT_R16G16B16_SSCALED */ {},
/* DATA_FORMAT_R16G16B16_UINT */ {},
/* DATA_FORMAT_R16G16B16_SINT */ {},
/* DATA_FORMAT_R16G16B16_SFLOAT */ {},
/* DATA_FORMAT_R16G16B16A16_UNORM */ { DXGI_FORMAT_R16G16B16A16_TYPELESS, DXGI_FORMAT_R16G16B16A16_UNORM },
/* DATA_FORMAT_R16G16B16A16_SNORM */ { DXGI_FORMAT_R16G16B16A16_TYPELESS, DXGI_FORMAT_R16G16B16A16_SNORM },
/* DATA_FORMAT_R16G16B16A16_USCALED */ { DXGI_FORMAT_R16G16B16A16_TYPELESS, DXGI_FORMAT_R16G16B16A16_UINT },
/* DATA_FORMAT_R16G16B16A16_SSCALED */ { DXGI_FORMAT_R16G16B16A16_TYPELESS, DXGI_FORMAT_R16G16B16A16_SINT },
/* DATA_FORMAT_R16G16B16A16_UINT */ { DXGI_FORMAT_R16G16B16A16_TYPELESS, DXGI_FORMAT_R16G16B16A16_UINT },
/* DATA_FORMAT_R16G16B16A16_SINT */ { DXGI_FORMAT_R16G16B16A16_TYPELESS, DXGI_FORMAT_R16G16B16A16_SINT },
/* DATA_FORMAT_R16G16B16A16_SFLOAT */ { DXGI_FORMAT_R16G16B16A16_TYPELESS, DXGI_FORMAT_R16G16B16A16_FLOAT },
/* DATA_FORMAT_R32_UINT */ { DXGI_FORMAT_R32_TYPELESS, DXGI_FORMAT_R32_UINT },
/* DATA_FORMAT_R32_SINT */ { DXGI_FORMAT_R32_TYPELESS, DXGI_FORMAT_R32_SINT },
/* DATA_FORMAT_R32_SFLOAT */ { DXGI_FORMAT_R32_TYPELESS, DXGI_FORMAT_R32_FLOAT },
/* DATA_FORMAT_R32G32_UINT */ { DXGI_FORMAT_R32G32_TYPELESS, DXGI_FORMAT_R32G32_UINT },
/* DATA_FORMAT_R32G32_SINT */ { DXGI_FORMAT_R32G32_TYPELESS, DXGI_FORMAT_R32G32_SINT },
/* DATA_FORMAT_R32G32_SFLOAT */ { DXGI_FORMAT_R32G32_TYPELESS, DXGI_FORMAT_R32G32_FLOAT },
/* DATA_FORMAT_R32G32B32_UINT */ { DXGI_FORMAT_R32G32B32_TYPELESS, DXGI_FORMAT_R32G32B32_UINT },
/* DATA_FORMAT_R32G32B32_SINT */ { DXGI_FORMAT_R32G32B32_TYPELESS, DXGI_FORMAT_R32G32B32_SINT },
/* DATA_FORMAT_R32G32B32_SFLOAT */ { DXGI_FORMAT_R32G32B32_TYPELESS, DXGI_FORMAT_R32G32B32_FLOAT },
/* DATA_FORMAT_R32G32B32A32_UINT */ { DXGI_FORMAT_R32G32B32A32_TYPELESS, DXGI_FORMAT_R32G32B32A32_UINT },
/* DATA_FORMAT_R32G32B32A32_SINT */ { DXGI_FORMAT_R32G32B32A32_TYPELESS, DXGI_FORMAT_R32G32B32A32_SINT },
/* DATA_FORMAT_R32G32B32A32_SFLOAT */ { DXGI_FORMAT_R32G32B32A32_TYPELESS, DXGI_FORMAT_R32G32B32A32_FLOAT },
/* DATA_FORMAT_R64_UINT */ {},
/* DATA_FORMAT_R64_SINT */ {},
/* DATA_FORMAT_R64_SFLOAT */ {},
/* DATA_FORMAT_R64G64_UINT */ {},
/* DATA_FORMAT_R64G64_SINT */ {},
/* DATA_FORMAT_R64G64_SFLOAT */ {},
/* DATA_FORMAT_R64G64B64_UINT */ {},
/* DATA_FORMAT_R64G64B64_SINT */ {},
/* DATA_FORMAT_R64G64B64_SFLOAT */ {},
/* DATA_FORMAT_R64G64B64A64_UINT */ {},
/* DATA_FORMAT_R64G64B64A64_SINT */ {},
/* DATA_FORMAT_R64G64B64A64_SFLOAT */ {},
/* DATA_FORMAT_B10G11R11_UFLOAT_PACK32 */ { DXGI_FORMAT_R11G11B10_FLOAT, DXGI_FORMAT_R11G11B10_FLOAT },
/* DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32 */ { DXGI_FORMAT_R9G9B9E5_SHAREDEXP, DXGI_FORMAT_R9G9B9E5_SHAREDEXP },
/* DATA_FORMAT_D16_UNORM */ { DXGI_FORMAT_R16_TYPELESS, DXGI_FORMAT_R16_UNORM, 0, DXGI_FORMAT_D16_UNORM },
/* DATA_FORMAT_X8_D24_UNORM_PACK32 */ { DXGI_FORMAT_R24G8_TYPELESS, DXGI_FORMAT_UNKNOWN, 0, DXGI_FORMAT_D24_UNORM_S8_UINT },
/* DATA_FORMAT_D32_SFLOAT */ { DXGI_FORMAT_R32_TYPELESS, DXGI_FORMAT_R32_FLOAT, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, DXGI_FORMAT_D32_FLOAT },
/* DATA_FORMAT_S8_UINT */ {},
/* DATA_FORMAT_D16_UNORM_S8_UINT */ {},
/* DATA_FORMAT_D24_UNORM_S8_UINT */ { DXGI_FORMAT_R24G8_TYPELESS, DXGI_FORMAT_UNKNOWN, 0, DXGI_FORMAT_D24_UNORM_S8_UINT },
/* DATA_FORMAT_D32_SFLOAT_S8_UINT */ { DXGI_FORMAT_R32G8X24_TYPELESS, DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, DXGI_FORMAT_D32_FLOAT_S8X24_UINT },
/* DATA_FORMAT_BC1_RGB_UNORM_BLOCK */ { DXGI_FORMAT_BC1_TYPELESS, DXGI_FORMAT_BC1_UNORM, D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(0, 1, 2, D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_1) },
/* DATA_FORMAT_BC1_RGB_SRGB_BLOCK */ { DXGI_FORMAT_BC1_TYPELESS, DXGI_FORMAT_BC1_UNORM_SRGB, D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(0, 1, 2, D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_1) },
/* DATA_FORMAT_BC1_RGBA_UNORM_BLOCK */ { DXGI_FORMAT_BC1_TYPELESS, DXGI_FORMAT_BC1_UNORM },
/* DATA_FORMAT_BC1_RGBA_SRGB_BLOCK */ { DXGI_FORMAT_BC1_TYPELESS, DXGI_FORMAT_BC1_UNORM_SRGB },
/* DATA_FORMAT_BC2_UNORM_BLOCK */ { DXGI_FORMAT_BC2_TYPELESS, DXGI_FORMAT_BC2_UNORM },
/* DATA_FORMAT_BC2_SRGB_BLOCK */ { DXGI_FORMAT_BC2_TYPELESS, DXGI_FORMAT_BC2_UNORM_SRGB },
/* DATA_FORMAT_BC3_UNORM_BLOCK */ { DXGI_FORMAT_BC3_TYPELESS, DXGI_FORMAT_BC3_UNORM },
/* DATA_FORMAT_BC3_SRGB_BLOCK */ { DXGI_FORMAT_BC3_TYPELESS, DXGI_FORMAT_BC3_UNORM_SRGB },
/* DATA_FORMAT_BC4_UNORM_BLOCK */ { DXGI_FORMAT_BC4_TYPELESS, DXGI_FORMAT_BC4_UNORM },
/* DATA_FORMAT_BC4_SNORM_BLOCK */ { DXGI_FORMAT_BC4_TYPELESS, DXGI_FORMAT_BC4_SNORM },
/* DATA_FORMAT_BC5_UNORM_BLOCK */ { DXGI_FORMAT_BC5_TYPELESS, DXGI_FORMAT_BC5_UNORM },
/* DATA_FORMAT_BC5_SNORM_BLOCK */ { DXGI_FORMAT_BC5_TYPELESS, DXGI_FORMAT_BC5_SNORM },
/* DATA_FORMAT_BC6H_UFLOAT_BLOCK */ { DXGI_FORMAT_BC6H_TYPELESS, DXGI_FORMAT_BC6H_UF16 },
/* DATA_FORMAT_BC6H_SFLOAT_BLOCK */ { DXGI_FORMAT_BC6H_TYPELESS, DXGI_FORMAT_BC6H_SF16 },
/* DATA_FORMAT_BC7_UNORM_BLOCK */ { DXGI_FORMAT_BC7_TYPELESS, DXGI_FORMAT_BC7_UNORM },
/* DATA_FORMAT_BC7_SRGB_BLOCK */ { DXGI_FORMAT_BC7_TYPELESS, DXGI_FORMAT_BC7_UNORM_SRGB },
/* DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK */ {},
/* DATA_FORMAT_ETC2_R8G8B8_SRGB_BLOCK */ {},
/* DATA_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK */ {},
/* DATA_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK */ {},
/* DATA_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK */ {},
/* DATA_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK */ {},
/* DATA_FORMAT_EAC_R11_UNORM_BLOCK */ {},
/* DATA_FORMAT_EAC_R11_SNORM_BLOCK */ {},
/* DATA_FORMAT_EAC_R11G11_UNORM_BLOCK */ {},
/* DATA_FORMAT_EAC_R11G11_SNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_4x4_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_4x4_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_5x4_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_5x4_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_5x5_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_5x5_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_6x5_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_6x5_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_6x6_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_6x6_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_8x5_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_8x5_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_8x6_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_8x6_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_8x8_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_8x8_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_10x5_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_10x5_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_10x6_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_10x6_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_10x8_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_10x8_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_10x10_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_10x10_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_12x10_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_12x10_SRGB_BLOCK */ {},
/* DATA_FORMAT_ASTC_12x12_UNORM_BLOCK */ {},
/* DATA_FORMAT_ASTC_12x12_SRGB_BLOCK */ {},
/* DATA_FORMAT_G8B8G8R8_422_UNORM */ {},
/* DATA_FORMAT_B8G8R8G8_422_UNORM */ {},
/* DATA_FORMAT_G8_B8_R8_3PLANE_420_UNORM */ {},
/* DATA_FORMAT_G8_B8R8_2PLANE_420_UNORM */ {},
/* DATA_FORMAT_G8_B8_R8_3PLANE_422_UNORM */ {},
/* DATA_FORMAT_G8_B8R8_2PLANE_422_UNORM */ {},
/* DATA_FORMAT_G8_B8_R8_3PLANE_444_UNORM */ {},
/* DATA_FORMAT_R10X6_UNORM_PACK16 */ {},
/* DATA_FORMAT_R10X6G10X6_UNORM_2PACK16 */ {},
/* DATA_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16 */ {},
/* DATA_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16 */ {},
/* DATA_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16 */ {},
/* DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16 */ {},
/* DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16 */ {},
/* DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16 */ {},
/* DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16 */ {},
/* DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16 */ {},
/* DATA_FORMAT_R12X4_UNORM_PACK16 */ {},
/* DATA_FORMAT_R12X4G12X4_UNORM_2PACK16 */ {},
/* DATA_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16 */ {},
/* DATA_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16 */ {},
/* DATA_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16 */ {},
/* DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16 */ {},
/* DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16 */ {},
/* DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16 */ {},
/* DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16 */ {},
/* DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16 */ {},
/* DATA_FORMAT_G16B16G16R16_422_UNORM */ {},
/* DATA_FORMAT_B16G16R16G16_422_UNORM */ {},
/* DATA_FORMAT_G16_B16_R16_3PLANE_420_UNORM */ {},
/* DATA_FORMAT_G16_B16R16_2PLANE_420_UNORM */ {},
/* DATA_FORMAT_G16_B16_R16_3PLANE_422_UNORM */ {},
/* DATA_FORMAT_G16_B16R16_2PLANE_422_UNORM */ {},
/* DATA_FORMAT_G16_B16_R16_3PLANE_444_UNORM */ {},
};
Error RenderingDeviceDriverD3D12::DescriptorsHeap::allocate(ID3D12Device *p_device, D3D12_DESCRIPTOR_HEAP_TYPE p_type, uint32_t p_descriptor_count, bool p_for_gpu) {
ERR_FAIL_COND_V(heap, ERR_ALREADY_EXISTS);
ERR_FAIL_COND_V(p_descriptor_count == 0, ERR_INVALID_PARAMETER);
handle_size = p_device->GetDescriptorHandleIncrementSize(p_type);
desc.Type = p_type;
desc.NumDescriptors = p_descriptor_count;
desc.Flags = p_for_gpu ? D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE : D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
HRESULT res = p_device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(heap.GetAddressOf()));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), ERR_CANT_CREATE, "CreateDescriptorHeap failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
return OK;
}
RenderingDeviceDriverD3D12::DescriptorsHeap::Walker RenderingDeviceDriverD3D12::DescriptorsHeap::make_walker() const {
Walker walker;
walker.handle_size = handle_size;
walker.handle_count = desc.NumDescriptors;
if (heap) {
#if defined(_MSC_VER) || !defined(_WIN32)
walker.first_cpu_handle = heap->GetCPUDescriptorHandleForHeapStart();
if ((desc.Flags & D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE)) {
walker.first_gpu_handle = heap->GetGPUDescriptorHandleForHeapStart();
}
#else
heap->GetCPUDescriptorHandleForHeapStart(&walker.first_cpu_handle);
if ((desc.Flags & D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE)) {
heap->GetGPUDescriptorHandleForHeapStart(&walker.first_gpu_handle);
}
#endif
}
return walker;
}
void RenderingDeviceDriverD3D12::DescriptorsHeap::Walker::advance(uint32_t p_count) {
ERR_FAIL_COND_MSG(handle_index + p_count > handle_count, "Would advance past EOF.");
handle_index += p_count;
}
D3D12_CPU_DESCRIPTOR_HANDLE RenderingDeviceDriverD3D12::DescriptorsHeap::Walker::get_curr_cpu_handle() {
ERR_FAIL_COND_V_MSG(is_at_eof(), D3D12_CPU_DESCRIPTOR_HANDLE(), "Heap walker is at EOF.");
return D3D12_CPU_DESCRIPTOR_HANDLE{ first_cpu_handle.ptr + handle_index * handle_size };
}
D3D12_GPU_DESCRIPTOR_HANDLE RenderingDeviceDriverD3D12::DescriptorsHeap::Walker::get_curr_gpu_handle() {
ERR_FAIL_COND_V_MSG(!first_gpu_handle.ptr, D3D12_GPU_DESCRIPTOR_HANDLE(), "Can't provide a GPU handle from a non-GPU descriptors heap.");
ERR_FAIL_COND_V_MSG(is_at_eof(), D3D12_GPU_DESCRIPTOR_HANDLE(), "Heap walker is at EOF.");
return D3D12_GPU_DESCRIPTOR_HANDLE{ first_gpu_handle.ptr + handle_index * handle_size };
}
static const D3D12_COMPARISON_FUNC RD_TO_D3D12_COMPARE_OP[RD::COMPARE_OP_MAX] = {
D3D12_COMPARISON_FUNC_NEVER,
D3D12_COMPARISON_FUNC_LESS,
D3D12_COMPARISON_FUNC_EQUAL,
D3D12_COMPARISON_FUNC_LESS_EQUAL,
D3D12_COMPARISON_FUNC_GREATER,
D3D12_COMPARISON_FUNC_NOT_EQUAL,
D3D12_COMPARISON_FUNC_GREATER_EQUAL,
D3D12_COMPARISON_FUNC_ALWAYS,
};
uint32_t RenderingDeviceDriverD3D12::SubgroupCapabilities::supported_stages_flags_rd() const {
// If there's a way to check exactly which are supported, I have yet to find it.
return (
RenderingDevice::ShaderStage::SHADER_STAGE_FRAGMENT_BIT |
RenderingDevice::ShaderStage::SHADER_STAGE_COMPUTE_BIT);
}
uint32_t RenderingDeviceDriverD3D12::SubgroupCapabilities::supported_operations_flags_rd() const {
if (!wave_ops_supported) {
return 0;
} else {
return (
RenderingDevice::SubgroupOperations::SUBGROUP_BASIC_BIT |
RenderingDevice::SubgroupOperations::SUBGROUP_BALLOT_BIT |
RenderingDevice::SubgroupOperations::SUBGROUP_VOTE_BIT |
RenderingDevice::SubgroupOperations::SUBGROUP_SHUFFLE_BIT |
RenderingDevice::SubgroupOperations::SUBGROUP_SHUFFLE_RELATIVE_BIT |
RenderingDevice::SubgroupOperations::SUBGROUP_QUAD_BIT |
RenderingDevice::SubgroupOperations::SUBGROUP_ARITHMETIC_BIT |
RenderingDevice::SubgroupOperations::SUBGROUP_CLUSTERED_BIT);
}
}
void RenderingDeviceDriverD3D12::_debug_message_func(D3D12_MESSAGE_CATEGORY p_category, D3D12_MESSAGE_SEVERITY p_severity, D3D12_MESSAGE_ID p_id, LPCSTR p_description, void *p_context) {
String type_string;
switch (p_category) {
case D3D12_MESSAGE_CATEGORY_APPLICATION_DEFINED:
type_string = "APPLICATION_DEFINED";
break;
case D3D12_MESSAGE_CATEGORY_MISCELLANEOUS:
type_string = "MISCELLANEOUS";
break;
case D3D12_MESSAGE_CATEGORY_INITIALIZATION:
type_string = "INITIALIZATION";
break;
case D3D12_MESSAGE_CATEGORY_CLEANUP:
type_string = "CLEANUP";
break;
case D3D12_MESSAGE_CATEGORY_COMPILATION:
type_string = "COMPILATION";
break;
case D3D12_MESSAGE_CATEGORY_STATE_CREATION:
type_string = "STATE_CREATION";
break;
case D3D12_MESSAGE_CATEGORY_STATE_SETTING:
type_string = "STATE_SETTING";
break;
case D3D12_MESSAGE_CATEGORY_STATE_GETTING:
type_string = "STATE_GETTING";
break;
case D3D12_MESSAGE_CATEGORY_RESOURCE_MANIPULATION:
type_string = "RESOURCE_MANIPULATION";
break;
case D3D12_MESSAGE_CATEGORY_EXECUTION:
type_string = "EXECUTION";
break;
case D3D12_MESSAGE_CATEGORY_SHADER:
type_string = "SHADER";
break;
}
String error_message(type_string +
" - Message Id Number: " + String::num_int64(p_id) +
"\n\t" + p_description);
// Convert D3D12 severity to our own log macros.
switch (p_severity) {
case D3D12_MESSAGE_SEVERITY_MESSAGE:
print_verbose(error_message);
break;
case D3D12_MESSAGE_SEVERITY_INFO:
print_line(error_message);
break;
case D3D12_MESSAGE_SEVERITY_WARNING:
WARN_PRINT(error_message);
break;
case D3D12_MESSAGE_SEVERITY_ERROR:
case D3D12_MESSAGE_SEVERITY_CORRUPTION:
ERR_PRINT(error_message);
CRASH_COND_MSG(Engine::get_singleton()->is_abort_on_gpu_errors_enabled(),
"Crashing, because abort on GPU errors is enabled.");
break;
}
}
/******************/
/**** RESOURCE ****/
/******************/
static const D3D12_RESOURCE_DIMENSION RD_TEXTURE_TYPE_TO_D3D12_RESOURCE_DIMENSION[RD::TEXTURE_TYPE_MAX] = {
D3D12_RESOURCE_DIMENSION_TEXTURE1D,
D3D12_RESOURCE_DIMENSION_TEXTURE2D,
D3D12_RESOURCE_DIMENSION_TEXTURE3D,
D3D12_RESOURCE_DIMENSION_TEXTURE2D,
D3D12_RESOURCE_DIMENSION_TEXTURE1D,
D3D12_RESOURCE_DIMENSION_TEXTURE2D,
D3D12_RESOURCE_DIMENSION_TEXTURE2D,
};
void RenderingDeviceDriverD3D12::_resource_transition_batch(CommandBufferInfo *p_command_buffer, ResourceInfo *p_resource, uint32_t p_subresource, uint32_t p_num_planes, D3D12_RESOURCE_STATES p_new_state) {
DEV_ASSERT(p_subresource != UINT32_MAX); // We don't support an "all-resources" command here.
ResourceInfo::States *res_states = p_resource->states_ptr;
D3D12_RESOURCE_STATES *curr_state = &res_states->subresource_states[p_subresource];
// Transitions can be considered redundant if the current state has all the bits of the new state.
// This check does not apply to the common state however, which must resort to checking if the state is the same (0).
bool any_state_is_common = *curr_state == D3D12_RESOURCE_STATE_COMMON || p_new_state == D3D12_RESOURCE_STATE_COMMON;
bool redundant_transition = any_state_is_common ? *curr_state == p_new_state : ((*curr_state) & p_new_state) == p_new_state;
if (redundant_transition) {
bool just_written = *curr_state == D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
bool needs_uav_barrier = just_written && res_states->last_batch_with_uav_barrier != p_command_buffer->res_barriers_batch;
if (needs_uav_barrier) {
if (p_command_buffer->res_barriers.size() < p_command_buffer->res_barriers_count + 1) {
p_command_buffer->res_barriers.resize(p_command_buffer->res_barriers_count + 1);
}
p_command_buffer->res_barriers[p_command_buffer->res_barriers_count] = CD3DX12_RESOURCE_BARRIER::UAV(p_resource->resource);
p_command_buffer->res_barriers_count++;
res_states->last_batch_with_uav_barrier = p_command_buffer->res_barriers_batch;
}
} else {
uint64_t subres_mask_piece = ((uint64_t)1 << (p_subresource & 0b111111));
uint8_t subres_qword = p_subresource >> 6;
if (p_command_buffer->res_barriers_requests.has(res_states)) {
BarrierRequest &br = p_command_buffer->res_barriers_requests.get(res_states);
DEV_ASSERT(br.dx_resource == p_resource->resource);
DEV_ASSERT(br.subres_mask_qwords == STEPIFY(res_states->subresource_states.size(), 64) / 64);
DEV_ASSERT(br.planes == p_num_planes);
// First, find if the subresource already has a barrier scheduled.
uint8_t curr_group_idx = 0;
bool same_transition_scheduled = false;
for (curr_group_idx = 0; curr_group_idx < br.groups_count; curr_group_idx++) {
if (unlikely(br.groups[curr_group_idx].states == BarrierRequest::DELETED_GROUP)) {
continue;
}
if ((br.groups[curr_group_idx].subres_mask[subres_qword] & subres_mask_piece)) {
uint32_t state_mask = br.groups[curr_group_idx].states;
same_transition_scheduled = (state_mask & (uint32_t)p_new_state) == (uint32_t)p_new_state;
break;
}
}
if (!same_transition_scheduled) {
bool subres_already_there = curr_group_idx != br.groups_count;
D3D12_RESOURCE_STATES final_states = {};<--- final_states is initialized
if (subres_already_there) {
final_states = br.groups[curr_group_idx].states;<--- final_states is overwritten
final_states |= p_new_state;
bool subres_alone = true;
for (uint8_t i = 0; i < br.subres_mask_qwords; i++) {
if (i == subres_qword) {
if (br.groups[curr_group_idx].subres_mask[i] != subres_mask_piece) {
subres_alone = false;
break;
}
} else {
if (br.groups[curr_group_idx].subres_mask[i] != 0) {
subres_alone = false;
break;
}
}
}
bool relocated = false;<--- The scope of the variable 'relocated' can be reduced. [+]The scope of the variable 'relocated' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
if (subres_alone) {
// Subresource is there by itself.
for (uint8_t i = 0; i < br.groups_count; i++) {
if (unlikely(i == curr_group_idx)) {
continue;
}
if (unlikely(br.groups[i].states == BarrierRequest::DELETED_GROUP)) {
continue;
}
// There's another group with the final states; relocate to it.
if (br.groups[i].states == final_states) {
br.groups[curr_group_idx].subres_mask[subres_qword] &= ~subres_mask_piece;
relocated = true;
break;
}
}
if (relocated) {
// Let's delete the group where it used to be by itself.
if (curr_group_idx == br.groups_count - 1) {
br.groups_count--;
} else {
br.groups[curr_group_idx].states = BarrierRequest::DELETED_GROUP;
}
} else {
// Its current group, where it's alone, can extend its states.
br.groups[curr_group_idx].states = final_states;
}
} else {
// Already there, but not by itself and the state mask is different, so it now belongs to a different group.
br.groups[curr_group_idx].subres_mask[subres_qword] &= ~subres_mask_piece;
subres_already_there = false;
}
} else {
final_states = p_new_state;
}
if (!subres_already_there) {
// See if it fits exactly the states of some of the groups to fit it there.
for (uint8_t i = 0; i < br.groups_count; i++) {
if (unlikely(i == curr_group_idx)) {
continue;
}
if (unlikely(br.groups[i].states == BarrierRequest::DELETED_GROUP)) {
continue;
}
if (br.groups[i].states == final_states) {
br.groups[i].subres_mask[subres_qword] |= subres_mask_piece;
subres_already_there = true;
break;
}
}
if (!subres_already_there) {
// Add a new group to accommodate this subresource.
uint8_t group_to_fill = 0;
if (br.groups_count < BarrierRequest::MAX_GROUPS) {
// There are still free groups.
group_to_fill = br.groups_count;
br.groups_count++;
} else {
// Let's try to take over a deleted one.
for (; group_to_fill < br.groups_count; group_to_fill++) {
if (unlikely(br.groups[group_to_fill].states == BarrierRequest::DELETED_GROUP)) {
break;
}
}
CRASH_COND(group_to_fill == br.groups_count);
}
br.groups[group_to_fill].states = final_states;
for (uint8_t i = 0; i < br.subres_mask_qwords; i++) {
if (unlikely(i == subres_qword)) {
br.groups[group_to_fill].subres_mask[i] = subres_mask_piece;
} else {
br.groups[group_to_fill].subres_mask[i] = 0;
}
}
}
}
}
} else {
BarrierRequest &br = p_command_buffer->res_barriers_requests[res_states];
br.dx_resource = p_resource->resource;
br.subres_mask_qwords = STEPIFY(p_resource->states_ptr->subresource_states.size(), 64) / 64;
CRASH_COND(p_resource->states_ptr->subresource_states.size() > BarrierRequest::MAX_SUBRESOURCES);
br.planes = p_num_planes;
br.groups[0].states = p_new_state;
for (uint8_t i = 0; i < br.subres_mask_qwords; i++) {
if (unlikely(i == subres_qword)) {
br.groups[0].subres_mask[i] = subres_mask_piece;
} else {
br.groups[0].subres_mask[i] = 0;
}
}
br.groups_count = 1;
}
}
}
void RenderingDeviceDriverD3D12::_resource_transitions_flush(CommandBufferInfo *p_command_buffer) {
for (const KeyValue<ResourceInfo::States *, BarrierRequest> &E : p_command_buffer->res_barriers_requests) {
ResourceInfo::States *res_states = E.key;
const BarrierRequest &br = E.value;
uint32_t num_subresources = res_states->subresource_states.size();
// When there's not a lot of subresources, the empirical finding is that it's better
// to avoid attempting the single-barrier optimization.
static const uint32_t SINGLE_BARRIER_ATTEMPT_MAX_NUM_SUBRESOURCES = 48;
bool may_do_single_barrier = br.groups_count == 1 && num_subresources * br.planes >= SINGLE_BARRIER_ATTEMPT_MAX_NUM_SUBRESOURCES;
if (may_do_single_barrier) {
// A single group means we may be able to do a single all-subresources barrier.
{
// First requisite is that all subresources are involved.
uint8_t subres_mask_full_qwords = num_subresources / 64;
for (uint32_t i = 0; i < subres_mask_full_qwords; i++) {
if (br.groups[0].subres_mask[i] != UINT64_MAX) {
may_do_single_barrier = false;
break;
}
}
if (may_do_single_barrier) {
if (num_subresources % 64) {
DEV_ASSERT(br.subres_mask_qwords == subres_mask_full_qwords + 1);
uint64_t mask_tail_qword = 0;
for (uint8_t i = 0; i < num_subresources % 64; i++) {
mask_tail_qword |= ((uint64_t)1 << i);
}
if ((br.groups[0].subres_mask[subres_mask_full_qwords] & mask_tail_qword) != mask_tail_qword) {
may_do_single_barrier = false;
}
}
}
}
if (may_do_single_barrier) {
// Second requisite is that the source state is the same for all.
for (uint32_t i = 1; i < num_subresources; i++) {
if (res_states->subresource_states[i] != res_states->subresource_states[0]) {
may_do_single_barrier = false;
break;
}
}
if (may_do_single_barrier) {
// Hurray!, we can do a single barrier (plus maybe a UAV one, too).
bool just_written = res_states->subresource_states[0] == D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
bool needs_uav_barrier = just_written && res_states->last_batch_with_uav_barrier != p_command_buffer->res_barriers_batch;
uint32_t needed_barriers = (needs_uav_barrier ? 1 : 0) + 1;
if (p_command_buffer->res_barriers.size() < p_command_buffer->res_barriers_count + needed_barriers) {
p_command_buffer->res_barriers.resize(p_command_buffer->res_barriers_count + needed_barriers);
}
if (needs_uav_barrier) {
p_command_buffer->res_barriers[p_command_buffer->res_barriers_count] = CD3DX12_RESOURCE_BARRIER::UAV(br.dx_resource);
p_command_buffer->res_barriers_count++;
res_states->last_batch_with_uav_barrier = p_command_buffer->res_barriers_batch;
}
if (res_states->subresource_states[0] != br.groups[0].states) {
p_command_buffer->res_barriers[p_command_buffer->res_barriers_count] = CD3DX12_RESOURCE_BARRIER::Transition(br.dx_resource, res_states->subresource_states[0], br.groups[0].states, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES);
p_command_buffer->res_barriers_count++;
}
for (uint32_t i = 0; i < num_subresources; i++) {
res_states->subresource_states[i] = br.groups[0].states;
}
}
}
}
if (!may_do_single_barrier) {
for (uint8_t i = 0; i < br.groups_count; i++) {
const BarrierRequest::Group &g = E.value.groups[i];
if (unlikely(g.states == BarrierRequest::DELETED_GROUP)) {
continue;
}
uint32_t subresource = 0;
do {
uint64_t subres_mask_piece = ((uint64_t)1 << (subresource % 64));
uint8_t subres_qword = subresource / 64;
if (likely(g.subres_mask[subres_qword] == 0)) {
subresource += 64;
continue;
}
if (likely(!(g.subres_mask[subres_qword] & subres_mask_piece))) {
subresource++;
continue;
}
D3D12_RESOURCE_STATES *curr_state = &res_states->subresource_states[subresource];
bool just_written = *curr_state == D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
bool needs_uav_barrier = just_written && res_states->last_batch_with_uav_barrier != p_command_buffer->res_barriers_batch;
uint32_t needed_barriers = (needs_uav_barrier ? 1 : 0) + br.planes;
if (p_command_buffer->res_barriers.size() < p_command_buffer->res_barriers_count + needed_barriers) {
p_command_buffer->res_barriers.resize(p_command_buffer->res_barriers_count + needed_barriers);
}
if (needs_uav_barrier) {
p_command_buffer->res_barriers[p_command_buffer->res_barriers_count] = CD3DX12_RESOURCE_BARRIER::UAV(br.dx_resource);
p_command_buffer->res_barriers_count++;
res_states->last_batch_with_uav_barrier = p_command_buffer->res_barriers_batch;
}
if (*curr_state != g.states) {
for (uint8_t k = 0; k < br.planes; k++) {
p_command_buffer->res_barriers[p_command_buffer->res_barriers_count] = CD3DX12_RESOURCE_BARRIER::Transition(br.dx_resource, *curr_state, g.states, subresource + k * num_subresources);
p_command_buffer->res_barriers_count++;
}
}
*curr_state = g.states;
subresource++;
} while (subresource < num_subresources);
}
}
}
if (p_command_buffer->res_barriers_count) {
p_command_buffer->cmd_list->ResourceBarrier(p_command_buffer->res_barriers_count, p_command_buffer->res_barriers.ptr());
p_command_buffer->res_barriers_requests.clear();
}
p_command_buffer->res_barriers_count = 0;
p_command_buffer->res_barriers_batch++;
}
/*****************/
/**** BUFFERS ****/
/*****************/
RDD::BufferID RenderingDeviceDriverD3D12::buffer_create(uint64_t p_size, BitField<BufferUsageBits> p_usage, MemoryAllocationType p_allocation_type) {
// D3D12 debug layers complain at CBV creation time if the size is not multiple of the value per the spec
// but also if you give a rounded size at that point because it will extend beyond the
// memory of the resource. Therefore, it seems the only way is to create it with a
// rounded size.
CD3DX12_RESOURCE_DESC1 resource_desc = CD3DX12_RESOURCE_DESC1::Buffer(STEPIFY(p_size, D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT));
if (p_usage.has_flag(RDD::BUFFER_USAGE_STORAGE_BIT)) {
resource_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
} else {
resource_desc.Flags |= D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE;
}
D3D12MA::ALLOCATION_DESC allocation_desc = {};
allocation_desc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
D3D12_RESOURCE_STATES initial_state = D3D12_RESOURCE_STATE_COMMON;
switch (p_allocation_type) {
case MEMORY_ALLOCATION_TYPE_CPU: {
bool is_src = p_usage.has_flag(BUFFER_USAGE_TRANSFER_FROM_BIT);
bool is_dst = p_usage.has_flag(BUFFER_USAGE_TRANSFER_TO_BIT);
if (is_src && !is_dst) {
// Looks like a staging buffer: CPU maps, writes sequentially, then GPU copies to VRAM.
allocation_desc.HeapType = D3D12_HEAP_TYPE_UPLOAD;
initial_state = D3D12_RESOURCE_STATE_GENERIC_READ;
}
if (is_dst && !is_src) {
// Looks like a readback buffer: GPU copies from VRAM, then CPU maps and reads.
allocation_desc.HeapType = D3D12_HEAP_TYPE_READBACK;
initial_state = D3D12_RESOURCE_STATE_COPY_DEST;
}
} break;
case MEMORY_ALLOCATION_TYPE_GPU: {
// Use default parameters.
} break;
}
ComPtr<ID3D12Resource> buffer;
ComPtr<D3D12MA::Allocation> allocation;
HRESULT res;
if (barrier_capabilities.enhanced_barriers_supported) {
res = allocator->CreateResource3(
&allocation_desc,
&resource_desc,
D3D12_BARRIER_LAYOUT_UNDEFINED,
nullptr,
0,
nullptr,
allocation.GetAddressOf(),
IID_PPV_ARGS(buffer.GetAddressOf()));
} else {
res = allocator->CreateResource(
&allocation_desc,
reinterpret_cast<const D3D12_RESOURCE_DESC *>(&resource_desc),
initial_state,
nullptr,
allocation.GetAddressOf(),
IID_PPV_ARGS(buffer.GetAddressOf()));
}
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), BufferID(), "Can't create buffer of size: " + itos(p_size) + ", error " + vformat("0x%08ux", (uint64_t)res) + ".");
// Bookkeep.
BufferInfo *buf_info = VersatileResource::allocate<BufferInfo>(resources_allocator);
buf_info->resource = buffer.Get();
buf_info->owner_info.resource = buffer;
buf_info->owner_info.allocation = allocation;
buf_info->owner_info.states.subresource_states.push_back(initial_state);
buf_info->states_ptr = &buf_info->owner_info.states;
buf_info->size = p_size;
buf_info->flags.usable_as_uav = (resource_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
return BufferID(buf_info);
}
bool RenderingDeviceDriverD3D12::buffer_set_texel_format(BufferID p_buffer, DataFormat p_format) {
BufferInfo *buf_info = (BufferInfo *)p_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
buf_info->texel_format = p_format;
return true;
}
void RenderingDeviceDriverD3D12::buffer_free(BufferID p_buffer) {
BufferInfo *buf_info = (BufferInfo *)p_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
VersatileResource::free(resources_allocator, buf_info);
}
uint64_t RenderingDeviceDriverD3D12::buffer_get_allocation_size(BufferID p_buffer) {
const BufferInfo *buf_info = (const BufferInfo *)p_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
return buf_info->owner_info.allocation ? buf_info->owner_info.allocation->GetSize() : 0;
}
uint8_t *RenderingDeviceDriverD3D12::buffer_map(BufferID p_buffer) {
const BufferInfo *buf_info = (const BufferInfo *)p_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
void *data_ptr = nullptr;
HRESULT res = buf_info->resource->Map(0, &VOID_RANGE, &data_ptr);
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), nullptr, "Map failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
return (uint8_t *)data_ptr;
}
void RenderingDeviceDriverD3D12::buffer_unmap(BufferID p_buffer) {
const BufferInfo *buf_info = (const BufferInfo *)p_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
buf_info->resource->Unmap(0, &VOID_RANGE);
}
/*****************/
/**** TEXTURE ****/
/*****************/
static const D3D12_SRV_DIMENSION RD_TEXTURE_TYPE_TO_D3D12_VIEW_DIMENSION_FOR_SRV[RD::TEXTURE_TYPE_MAX] = {
D3D12_SRV_DIMENSION_TEXTURE1D,
D3D12_SRV_DIMENSION_TEXTURE2D,
D3D12_SRV_DIMENSION_TEXTURE3D,
D3D12_SRV_DIMENSION_TEXTURECUBE,
D3D12_SRV_DIMENSION_TEXTURE1DARRAY,
D3D12_SRV_DIMENSION_TEXTURE2DARRAY,
D3D12_SRV_DIMENSION_TEXTURECUBEARRAY,
};
static const D3D12_SRV_DIMENSION RD_TEXTURE_TYPE_TO_D3D12_VIEW_DIMENSION_FOR_SRV_MS[RD::TEXTURE_TYPE_MAX] = {
D3D12_SRV_DIMENSION_UNKNOWN,
D3D12_SRV_DIMENSION_TEXTURE2DMS,
D3D12_SRV_DIMENSION_UNKNOWN,
D3D12_SRV_DIMENSION_UNKNOWN,
D3D12_SRV_DIMENSION_UNKNOWN,
D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY,
D3D12_SRV_DIMENSION_UNKNOWN,
};
static const D3D12_UAV_DIMENSION RD_TEXTURE_TYPE_TO_D3D12_VIEW_DIMENSION_FOR_UAV[RD::TEXTURE_TYPE_MAX] = {
D3D12_UAV_DIMENSION_TEXTURE1D,
D3D12_UAV_DIMENSION_TEXTURE2D,
D3D12_UAV_DIMENSION_TEXTURE3D,
D3D12_UAV_DIMENSION_TEXTURE2DARRAY,
D3D12_UAV_DIMENSION_TEXTURE1DARRAY,
D3D12_UAV_DIMENSION_TEXTURE2DARRAY,
D3D12_UAV_DIMENSION_TEXTURE2DARRAY,
};
uint32_t RenderingDeviceDriverD3D12::_find_max_common_supported_sample_count(VectorView<DXGI_FORMAT> p_formats) {
uint32_t common = UINT32_MAX;
MutexLock lock(format_sample_counts_mask_cache_mutex);
for (uint32_t i = 0; i < p_formats.size(); i++) {
if (format_sample_counts_mask_cache.has(p_formats[i])) {
common &= format_sample_counts_mask_cache[p_formats[i]];
} else {
D3D12_FEATURE_DATA_MULTISAMPLE_QUALITY_LEVELS msql = {};
msql.Format = p_formats[i];
uint32_t mask = 0;
for (int samples = 1 << (TEXTURE_SAMPLES_MAX - 1); samples >= 1; samples /= 2) {
msql.SampleCount = (UINT)samples;
HRESULT res = device->CheckFeatureSupport(D3D12_FEATURE_MULTISAMPLE_QUALITY_LEVELS, &msql, sizeof(msql));
if (SUCCEEDED(res) && msql.NumQualityLevels) {
int bit = get_shift_from_power_of_2(samples);
ERR_FAIL_COND_V(bit == -1, 1);
mask |= (uint32_t)(1 << bit);
}
}
format_sample_counts_mask_cache.insert(p_formats[i], mask);
common &= mask;
}
}
if (common == UINT32_MAX) {
return 1;
} else {
return ((uint32_t)1 << nearest_shift(common));
}
}
UINT RenderingDeviceDriverD3D12::_compute_component_mapping(const RDD::TextureView &p_view) {
UINT base_swizzle = RD_TO_D3D12_FORMAT[p_view.format].swizzle;
D3D12_SHADER_COMPONENT_MAPPING component_swizzles[TEXTURE_SWIZZLE_MAX] = {
D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0, // Unused.
D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_0,
D3D12_SHADER_COMPONENT_MAPPING_FORCE_VALUE_1,
// These will be D3D12_SHADER_COMPONENT_MAPPING_FROM_MEMORY_COMPONENT_*.
D3D12_DECODE_SHADER_4_COMPONENT_MAPPING(0, base_swizzle),
D3D12_DECODE_SHADER_4_COMPONENT_MAPPING(1, base_swizzle),
D3D12_DECODE_SHADER_4_COMPONENT_MAPPING(2, base_swizzle),
D3D12_DECODE_SHADER_4_COMPONENT_MAPPING(3, base_swizzle),
};
return D3D12_ENCODE_SHADER_4_COMPONENT_MAPPING(
p_view.swizzle_r == TEXTURE_SWIZZLE_IDENTITY ? component_swizzles[TEXTURE_SWIZZLE_R] : component_swizzles[p_view.swizzle_r],
p_view.swizzle_g == TEXTURE_SWIZZLE_IDENTITY ? component_swizzles[TEXTURE_SWIZZLE_G] : component_swizzles[p_view.swizzle_g],
p_view.swizzle_b == TEXTURE_SWIZZLE_IDENTITY ? component_swizzles[TEXTURE_SWIZZLE_B] : component_swizzles[p_view.swizzle_b],
p_view.swizzle_a == TEXTURE_SWIZZLE_IDENTITY ? component_swizzles[TEXTURE_SWIZZLE_A] : component_swizzles[p_view.swizzle_a]);
}
UINT RenderingDeviceDriverD3D12::_compute_plane_slice(DataFormat p_format, BitField<TextureAspectBits> p_aspect_bits) {
TextureAspect aspect = TEXTURE_ASPECT_MAX;
if (p_aspect_bits.has_flag(TEXTURE_ASPECT_COLOR_BIT)) {
DEV_ASSERT(aspect == TEXTURE_ASPECT_MAX);
aspect = TEXTURE_ASPECT_COLOR;
}
if (p_aspect_bits.has_flag(TEXTURE_ASPECT_DEPTH_BIT)) {
DEV_ASSERT(aspect == TEXTURE_ASPECT_MAX);
aspect = TEXTURE_ASPECT_DEPTH;
} else if (p_aspect_bits.has_flag(TEXTURE_ASPECT_STENCIL_BIT)) {
DEV_ASSERT(aspect == TEXTURE_ASPECT_MAX);
aspect = TEXTURE_ASPECT_STENCIL;
}
DEV_ASSERT(aspect != TEXTURE_ASPECT_MAX);
return _compute_plane_slice(p_format, aspect);
}
UINT RenderingDeviceDriverD3D12::_compute_plane_slice(DataFormat p_format, TextureAspect p_aspect) {
switch (p_aspect) {
case TEXTURE_ASPECT_COLOR:
// The plane must be 0 for the color aspect (assuming the format is a regular color one, which must be the case).
return 0;
case TEXTURE_ASPECT_DEPTH:
// The plane must be 0 for the color or depth aspect
return 0;
case TEXTURE_ASPECT_STENCIL:
// The plane may be 0 for the stencil aspect (if the format is stencil-only), or 1 (if the format is depth-stencil; other cases are ill).
return format_get_plane_count(p_format) == 2 ? 1 : 0;
default:
DEV_ASSERT(false);
return 0;
}
}
UINT RenderingDeviceDriverD3D12::_compute_subresource_from_layers(TextureInfo *p_texture, const TextureSubresourceLayers &p_layers, uint32_t p_layer_offset) {
return D3D12CalcSubresource(p_layers.mipmap, p_layers.base_layer + p_layer_offset, _compute_plane_slice(p_texture->format, p_layers.aspect), p_texture->desc.MipLevels, p_texture->desc.ArraySize());
}
void RenderingDeviceDriverD3D12::_discard_texture_subresources(const TextureInfo *p_tex_info, const CommandBufferInfo *p_cmd_buf_info) {
uint32_t planes = 1;
if ((p_tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) {
planes = format_get_plane_count(p_tex_info->format);
}
D3D12_DISCARD_REGION dr = {};
dr.NumRects = p_cmd_buf_info->render_pass_state.region_is_all ? 0 : 1;
dr.pRects = p_cmd_buf_info->render_pass_state.region_is_all ? nullptr : &p_cmd_buf_info->render_pass_state.region_rect;
dr.FirstSubresource = UINT_MAX;
dr.NumSubresources = 0;
for (uint32_t u = 0; u < planes; u++) {
for (uint32_t v = 0; v < p_tex_info->layers; v++) {
for (uint32_t w = 0; w < p_tex_info->mipmaps; w++) {
UINT subresource = D3D12CalcSubresource(
p_tex_info->base_mip + w,
p_tex_info->base_layer + v,
u,
p_tex_info->desc.MipLevels,
p_tex_info->desc.ArraySize());
if (dr.NumSubresources == 0) {
dr.FirstSubresource = subresource;
dr.NumSubresources = 1;
} else if (dr.FirstSubresource + dr.NumSubresources == subresource) {
dr.NumSubresources++;
} else {
p_cmd_buf_info->cmd_list->DiscardResource(p_tex_info->resource, &dr);
dr.FirstSubresource = subresource;
dr.NumSubresources = 1;
}
}
}
}
if (dr.NumSubresources) {
p_cmd_buf_info->cmd_list->DiscardResource(p_tex_info->resource, &dr);
}
}
bool RenderingDeviceDriverD3D12::_unordered_access_supported_by_format(DataFormat p_format) {
switch (p_format) {
case DATA_FORMAT_R4G4_UNORM_PACK8:
case DATA_FORMAT_R4G4B4A4_UNORM_PACK16:
case DATA_FORMAT_B4G4R4A4_UNORM_PACK16:
case DATA_FORMAT_R5G6B5_UNORM_PACK16:
case DATA_FORMAT_B5G6R5_UNORM_PACK16:
case DATA_FORMAT_R5G5B5A1_UNORM_PACK16:
case DATA_FORMAT_B5G5R5A1_UNORM_PACK16:
case DATA_FORMAT_A1R5G5B5_UNORM_PACK16:
case DATA_FORMAT_A8B8G8R8_UNORM_PACK32:
case DATA_FORMAT_A8B8G8R8_SNORM_PACK32:
case DATA_FORMAT_A8B8G8R8_USCALED_PACK32:
case DATA_FORMAT_A8B8G8R8_SSCALED_PACK32:
case DATA_FORMAT_A8B8G8R8_UINT_PACK32:
case DATA_FORMAT_A8B8G8R8_SINT_PACK32:
case DATA_FORMAT_A8B8G8R8_SRGB_PACK32:
case DATA_FORMAT_A2R10G10B10_UNORM_PACK32:
case DATA_FORMAT_A2R10G10B10_SNORM_PACK32:
case DATA_FORMAT_A2R10G10B10_USCALED_PACK32:
case DATA_FORMAT_A2R10G10B10_SSCALED_PACK32:
case DATA_FORMAT_A2R10G10B10_UINT_PACK32:
case DATA_FORMAT_A2R10G10B10_SINT_PACK32:
case DATA_FORMAT_A2B10G10R10_UNORM_PACK32:
case DATA_FORMAT_A2B10G10R10_SNORM_PACK32:
case DATA_FORMAT_A2B10G10R10_USCALED_PACK32:
case DATA_FORMAT_A2B10G10R10_SSCALED_PACK32:
case DATA_FORMAT_A2B10G10R10_UINT_PACK32:
case DATA_FORMAT_A2B10G10R10_SINT_PACK32:
case DATA_FORMAT_B10G11R11_UFLOAT_PACK32:
case DATA_FORMAT_E5B9G9R9_UFLOAT_PACK32:
case DATA_FORMAT_X8_D24_UNORM_PACK32:
case DATA_FORMAT_R10X6_UNORM_PACK16:
case DATA_FORMAT_R10X6G10X6_UNORM_2PACK16:
case DATA_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16:
case DATA_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16:
case DATA_FORMAT_B10X6G10X6R10X6G10X6_422_UNORM_4PACK16:
case DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_420_UNORM_3PACK16:
case DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
case DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_422_UNORM_3PACK16:
case DATA_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16:
case DATA_FORMAT_G10X6_B10X6_R10X6_3PLANE_444_UNORM_3PACK16:
case DATA_FORMAT_R12X4_UNORM_PACK16:
case DATA_FORMAT_R12X4G12X4_UNORM_2PACK16:
case DATA_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16:
case DATA_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16:
case DATA_FORMAT_B12X4G12X4R12X4G12X4_422_UNORM_4PACK16:
case DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_420_UNORM_3PACK16:
case DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16:
case DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_422_UNORM_3PACK16:
case DATA_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16:
case DATA_FORMAT_G12X4_B12X4_R12X4_3PLANE_444_UNORM_3PACK16:
return false;
default:
return true;
}
}
RDD::TextureID RenderingDeviceDriverD3D12::texture_create(const TextureFormat &p_format, const TextureView &p_view) {
// Using D3D12_RESOURCE_DESC1. Thanks to the layout, it's sliceable down to D3D12_RESOURCE_DESC if needed.
CD3DX12_RESOURCE_DESC1 resource_desc = {};
resource_desc.Dimension = RD_TEXTURE_TYPE_TO_D3D12_RESOURCE_DIMENSION[p_format.texture_type];
resource_desc.Alignment = 0; // D3D12MA will override this to use a smaller alignment than the default if possible.
resource_desc.Width = p_format.width;
resource_desc.Height = p_format.height;
resource_desc.DepthOrArraySize = p_format.depth * p_format.array_layers;
resource_desc.MipLevels = p_format.mipmaps;
// Format.
bool cross_family_sharing = false;
bool relaxed_casting_available = false;
DXGI_FORMAT *relaxed_casting_formats = nullptr;
uint32_t relaxed_casting_format_count = 0;
{
resource_desc.Format = RD_TO_D3D12_FORMAT[p_format.format].family;
// If views of different families are wanted, special setup is needed for proper sharing among them.
// If the driver reports relaxed casting is, leverage its new extended resource creation API (via D3D12MA).
if (p_format.shareable_formats.size() && format_capabilities.relaxed_casting_supported) {
relaxed_casting_available = true;
relaxed_casting_formats = ALLOCA_ARRAY(DXGI_FORMAT, p_format.shareable_formats.size() + 1);
relaxed_casting_formats[0] = RD_TO_D3D12_FORMAT[p_format.format].general_format;
relaxed_casting_format_count++;
}
HashMap<DataFormat, D3D12_RESOURCE_FLAGS> aliases_forbidden_flags;
for (int i = 0; i < p_format.shareable_formats.size(); i++) {
DataFormat curr_format = p_format.shareable_formats[i];
String format_text = "'" + String(FORMAT_NAMES[p_format.format]) + "'";
ERR_FAIL_COND_V_MSG(RD_TO_D3D12_FORMAT[curr_format].family == DXGI_FORMAT_UNKNOWN, TextureID(), "Format " + format_text + " is not supported.");
if (RD_TO_D3D12_FORMAT[curr_format].family != RD_TO_D3D12_FORMAT[p_format.format].family) {
cross_family_sharing = true;
}
if (relaxed_casting_available) {
relaxed_casting_formats[relaxed_casting_format_count] = RD_TO_D3D12_FORMAT[curr_format].general_format;
relaxed_casting_format_count++;
}
}
if (cross_family_sharing && !relaxed_casting_available) {
// Per https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_texture_layout.
if (p_format.texture_type == TEXTURE_TYPE_1D) {
ERR_FAIL_V_MSG(TextureID(), "This texture's views require aliasing, but that's not supported for a 1D texture.");
}
if (p_format.samples != TEXTURE_SAMPLES_1) {
ERR_FAIL_V_MSG(TextureID(), "This texture's views require aliasing, but that's not supported for a multi-sample texture.");
}
if ((p_format.usage_bits & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
ERR_FAIL_V_MSG(TextureID(), "This texture's views require aliasing, but that's not supported for a depth-stencil texture.");
}
if (RD_TO_D3D12_FORMAT[p_format.format].family == DXGI_FORMAT_R32G32B32_TYPELESS) {
ERR_FAIL_V_MSG(TextureID(), "This texture's views require aliasing, but that's not supported for an R32G32B32 texture.");
}
}
}
// Usage.
if ((p_format.usage_bits & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT)) {
resource_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
} else {
if ((p_format.usage_bits & TEXTURE_USAGE_CAN_COPY_TO_BIT) && _unordered_access_supported_by_format(p_format.format)) {
resource_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; // For clearing via UAV.
}
}
if ((p_format.usage_bits & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
resource_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
}
if ((p_format.usage_bits & TEXTURE_USAGE_STORAGE_BIT)) {
resource_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
}
if ((p_format.usage_bits & TEXTURE_USAGE_VRS_ATTACHMENT_BIT)) {
// For VRS images we can't use the typeless format.
resource_desc.Format = DXGI_FORMAT_R8_UINT;
}
resource_desc.SampleDesc = {};
DXGI_FORMAT format_to_test = (resource_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) ? RD_TO_D3D12_FORMAT[p_format.format].dsv_format : RD_TO_D3D12_FORMAT[p_format.format].general_format;
if (!(resource_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) {
resource_desc.SampleDesc.Count = MIN(
_find_max_common_supported_sample_count(format_to_test),
TEXTURE_SAMPLES_COUNT[p_format.samples]);
} else {
// No MSAA in D3D12 if storage. May have become possible recently where supported, though.
resource_desc.SampleDesc.Count = 1;
}
resource_desc.SampleDesc.Quality = resource_desc.SampleDesc.Count == 1 ? 0 : DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
// Create.
D3D12MA::ALLOCATION_DESC allocation_desc = {};
allocation_desc.HeapType = (p_format.usage_bits & TEXTURE_USAGE_CPU_READ_BIT) ? D3D12_HEAP_TYPE_READBACK : D3D12_HEAP_TYPE_DEFAULT;
if ((resource_desc.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL))) {
allocation_desc.ExtraHeapFlags = D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES;
} else {
allocation_desc.ExtraHeapFlags = D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES;
}
if ((resource_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) {
allocation_desc.ExtraHeapFlags |= D3D12_HEAP_FLAG_ALLOW_SHADER_ATOMICS;
}
D3D12_RESOURCE_STATES initial_state = {};<--- initial_state is initialized
ID3D12Resource *texture = nullptr;
ComPtr<ID3D12Resource> main_texture;
ComPtr<D3D12MA::Allocation> allocation;
static const FLOAT black[4] = {};
D3D12_CLEAR_VALUE clear_value = CD3DX12_CLEAR_VALUE(RD_TO_D3D12_FORMAT[p_format.format].general_format, black);
D3D12_CLEAR_VALUE *clear_value_ptr = (resource_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) ? &clear_value : nullptr;
{
HRESULT res = E_FAIL;
if (barrier_capabilities.enhanced_barriers_supported || (cross_family_sharing && relaxed_casting_available)) {
// Create with undefined layout if enhanced barriers are supported. Leave as common otherwise for interop with legacy barriers.
D3D12_BARRIER_LAYOUT initial_layout = barrier_capabilities.enhanced_barriers_supported ? D3D12_BARRIER_LAYOUT_UNDEFINED : D3D12_BARRIER_LAYOUT_COMMON;
res = allocator->CreateResource3(
&allocation_desc,
&resource_desc,
initial_layout,
clear_value_ptr,
relaxed_casting_format_count,
relaxed_casting_formats,
allocation.GetAddressOf(),
IID_PPV_ARGS(main_texture.GetAddressOf()));
initial_state = D3D12_RESOURCE_STATE_COMMON;<--- initial_state is overwritten
} else {
res = allocator->CreateResource(
&allocation_desc,
(D3D12_RESOURCE_DESC *)&resource_desc,
D3D12_RESOURCE_STATE_COPY_DEST,
clear_value_ptr,
allocation.GetAddressOf(),
IID_PPV_ARGS(main_texture.GetAddressOf()));
initial_state = D3D12_RESOURCE_STATE_COPY_DEST;
}
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), TextureID(), "CreateResource failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
texture = main_texture.Get();
}
// Describe views.
D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};
{
srv_desc.Format = RD_TO_D3D12_FORMAT[p_view.format].general_format;
srv_desc.ViewDimension = p_format.samples == TEXTURE_SAMPLES_1 ? RD_TEXTURE_TYPE_TO_D3D12_VIEW_DIMENSION_FOR_SRV[p_format.texture_type] : RD_TEXTURE_TYPE_TO_D3D12_VIEW_DIMENSION_FOR_SRV_MS[p_format.texture_type];
srv_desc.Shader4ComponentMapping = _compute_component_mapping(p_view);
switch (srv_desc.ViewDimension) {
case D3D12_SRV_DIMENSION_TEXTURE1D: {
srv_desc.Texture1D.MipLevels = p_format.mipmaps;
} break;
case D3D12_SRV_DIMENSION_TEXTURE1DARRAY: {
srv_desc.Texture1DArray.MipLevels = p_format.mipmaps;
srv_desc.Texture1DArray.ArraySize = p_format.array_layers;
} break;
case D3D12_SRV_DIMENSION_TEXTURE2D: {
srv_desc.Texture2D.MipLevels = p_format.mipmaps;
} break;
case D3D12_SRV_DIMENSION_TEXTURE2DMS: {
} break;
case D3D12_SRV_DIMENSION_TEXTURE2DARRAY: {
srv_desc.Texture2DArray.MipLevels = p_format.mipmaps;
srv_desc.Texture2DArray.ArraySize = p_format.array_layers;
} break;
case D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY: {
srv_desc.Texture2DMSArray.ArraySize = p_format.array_layers;
} break;
case D3D12_SRV_DIMENSION_TEXTURECUBEARRAY: {
srv_desc.TextureCubeArray.MipLevels = p_format.mipmaps;
srv_desc.TextureCubeArray.NumCubes = p_format.array_layers / 6;
} break;
case D3D12_SRV_DIMENSION_TEXTURE3D: {
srv_desc.Texture3D.MipLevels = p_format.mipmaps;
} break;
case D3D12_SRV_DIMENSION_TEXTURECUBE: {
srv_desc.TextureCube.MipLevels = p_format.mipmaps;
} break;
default: {
}
}
}
D3D12_UNORDERED_ACCESS_VIEW_DESC main_uav_desc = {};
{
main_uav_desc.Format = RD_TO_D3D12_FORMAT[p_format.format].general_format;
main_uav_desc.ViewDimension = p_format.samples == TEXTURE_SAMPLES_1 ? RD_TEXTURE_TYPE_TO_D3D12_VIEW_DIMENSION_FOR_UAV[p_format.texture_type] : D3D12_UAV_DIMENSION_UNKNOWN;
switch (main_uav_desc.ViewDimension) {
case D3D12_UAV_DIMENSION_TEXTURE1DARRAY: {
main_uav_desc.Texture1DArray.ArraySize = p_format.array_layers;
} break;
case D3D12_UAV_DIMENSION_TEXTURE2DARRAY: {
// Either for an actual 2D texture array, cubemap or cubemap array.
main_uav_desc.Texture2DArray.ArraySize = p_format.array_layers;
} break;
case D3D12_UAV_DIMENSION_TEXTURE3D: {
main_uav_desc.Texture3D.WSize = p_format.depth;
} break;
default: {
}
}
}
D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = main_uav_desc;
uav_desc.Format = RD_TO_D3D12_FORMAT[p_view.format].general_format;
// Bookkeep.
TextureInfo *tex_info = VersatileResource::allocate<TextureInfo>(resources_allocator);
tex_info->resource = texture;
tex_info->owner_info.resource = main_texture;
tex_info->owner_info.allocation = allocation;
tex_info->owner_info.states.subresource_states.resize(p_format.mipmaps * p_format.array_layers);
for (uint32_t i = 0; i < tex_info->owner_info.states.subresource_states.size(); i++) {
tex_info->owner_info.states.subresource_states[i] = initial_state;
}
tex_info->states_ptr = &tex_info->owner_info.states;
tex_info->format = p_format.format;
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif
tex_info->desc = *(CD3DX12_RESOURCE_DESC *)&resource_desc;
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
tex_info->base_layer = 0;
tex_info->layers = resource_desc.ArraySize();
tex_info->base_mip = 0;
tex_info->mipmaps = resource_desc.MipLevels;
tex_info->view_descs.srv = srv_desc;
tex_info->view_descs.uav = uav_desc;
if (!barrier_capabilities.enhanced_barriers_supported && (p_format.usage_bits & (TEXTURE_USAGE_STORAGE_BIT | TEXTURE_USAGE_COLOR_ATTACHMENT_BIT))) {
// Fallback to clear resources when they're first used in a uniform set. Not necessary if enhanced barriers
// are supported, as the discard flag will be used instead when transitioning from an undefined layout.
textures_pending_clear.add(&tex_info->pending_clear);
}
return TextureID(tex_info);
}
RDD::TextureID RenderingDeviceDriverD3D12::texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil) {
ERR_FAIL_V_MSG(TextureID(), "Unimplemented!");
}
RDD::TextureID RenderingDeviceDriverD3D12::texture_create_shared(TextureID p_original_texture, const TextureView &p_view) {
return _texture_create_shared_from_slice(p_original_texture, p_view, (TextureSliceType)-1, 0, 0, 0, 0);
}
RDD::TextureID RenderingDeviceDriverD3D12::texture_create_shared_from_slice(TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps) {
return _texture_create_shared_from_slice(p_original_texture, p_view, p_slice_type, p_layer, p_layers, p_mipmap, p_mipmaps);
}
RDD::TextureID RenderingDeviceDriverD3D12::_texture_create_shared_from_slice(TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps) {
TextureInfo *owner_tex_info = (TextureInfo *)p_original_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
#ifdef DEBUG_ENABLED
ERR_FAIL_COND_V(!owner_tex_info->owner_info.allocation, TextureID());
#endif
ComPtr<ID3D12Resource> new_texture;
ComPtr<D3D12MA::Allocation> new_allocation;
ID3D12Resource *resource = owner_tex_info->resource;
CD3DX12_RESOURCE_DESC new_tex_resource_desc = owner_tex_info->desc;
// Describe views.
D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = owner_tex_info->view_descs.srv;
{
srv_desc.Format = RD_TO_D3D12_FORMAT[p_view.format].general_format;
srv_desc.Shader4ComponentMapping = _compute_component_mapping(p_view);
}
D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = owner_tex_info->view_descs.uav;
{
uav_desc.Format = RD_TO_D3D12_FORMAT[p_view.format].general_format;
}
if (p_slice_type != (TextureSliceType)-1) {
// Complete description with slicing.
switch (p_slice_type) {
case TEXTURE_SLICE_2D: {
if (srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE2D && p_layer == 0) {
srv_desc.Texture2D.MostDetailedMip = p_mipmap;
srv_desc.Texture2D.MipLevels = p_mipmaps;
DEV_ASSERT(uav_desc.ViewDimension == D3D12_UAV_DIMENSION_TEXTURE2D);
uav_desc.Texture1D.MipSlice = p_mipmap;
} else if (srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE2DMS && p_layer == 0) {
DEV_ASSERT(uav_desc.ViewDimension == D3D12_UAV_DIMENSION_UNKNOWN);
} else if ((srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE2DARRAY || (srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE2D && p_layer)) || srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURECUBE || srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURECUBEARRAY) {
srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;
srv_desc.Texture2DArray.MostDetailedMip = p_mipmap;
srv_desc.Texture2DArray.MipLevels = p_mipmaps;
srv_desc.Texture2DArray.FirstArraySlice = p_layer;
srv_desc.Texture2DArray.ArraySize = 1;
srv_desc.Texture2DArray.PlaneSlice = 0;
srv_desc.Texture2DArray.ResourceMinLODClamp = 0.0f;
uav_desc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY;
uav_desc.Texture2DArray.MipSlice = p_mipmap;
uav_desc.Texture2DArray.FirstArraySlice = p_layer;
uav_desc.Texture2DArray.ArraySize = 1;
uav_desc.Texture2DArray.PlaneSlice = 0;
} else if ((srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY || (srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE2DMS && p_layer))) {
srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;
srv_desc.Texture2DMSArray.FirstArraySlice = p_layer;
srv_desc.Texture2DMSArray.ArraySize = 1;
uav_desc.ViewDimension = D3D12_UAV_DIMENSION_UNKNOWN;
} else {
DEV_ASSERT(false);
}
} break;
case TEXTURE_SLICE_CUBEMAP: {
if (srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURECUBE || p_layer == 0) {<--- Assuming that condition 'p_layer==0' is not redundant
srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBE;
srv_desc.TextureCube.MostDetailedMip = p_mipmap;
srv_desc.TextureCube.MipLevels = p_mipmaps;
DEV_ASSERT(uav_desc.ViewDimension == D3D12_UAV_DIMENSION_TEXTURE2DARRAY);
uav_desc.Texture2DArray.MipSlice = p_mipmap;
uav_desc.Texture2DArray.FirstArraySlice = p_layer;
uav_desc.Texture2DArray.ArraySize = 6;
uav_desc.Texture2DArray.PlaneSlice = 0;
} else if (srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURECUBEARRAY || p_layer != 0) {<--- Condition 'p_layer!=0' is always true
srv_desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURECUBEARRAY;
srv_desc.TextureCubeArray.MostDetailedMip = p_mipmap;
srv_desc.TextureCubeArray.MipLevels = p_mipmaps;
srv_desc.TextureCubeArray.First2DArrayFace = p_layer;
srv_desc.TextureCubeArray.NumCubes = 1;
srv_desc.TextureCubeArray.ResourceMinLODClamp = 0.0f;
DEV_ASSERT(uav_desc.ViewDimension == D3D12_UAV_DIMENSION_TEXTURE2DARRAY);
uav_desc.Texture2DArray.MipSlice = p_mipmap;
uav_desc.Texture2DArray.FirstArraySlice = p_layer;
uav_desc.Texture2DArray.ArraySize = 6;
uav_desc.Texture2DArray.PlaneSlice = 0;
} else {
DEV_ASSERT(false);
}
} break;
case TEXTURE_SLICE_3D: {
DEV_ASSERT(srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE3D);
srv_desc.Texture3D.MostDetailedMip = p_mipmap;
srv_desc.Texture3D.MipLevels = p_mipmaps;
DEV_ASSERT(uav_desc.ViewDimension == D3D12_UAV_DIMENSION_TEXTURE3D);
uav_desc.Texture3D.MipSlice = p_mipmap;
uav_desc.Texture3D.WSize = -1;
} break;
case TEXTURE_SLICE_2D_ARRAY: {
DEV_ASSERT(srv_desc.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE2DARRAY);
srv_desc.Texture2DArray.MostDetailedMip = p_mipmap;
srv_desc.Texture2DArray.MipLevels = p_mipmaps;
srv_desc.Texture2DArray.FirstArraySlice = p_layer;
srv_desc.Texture2DArray.ArraySize = p_layers;
DEV_ASSERT(uav_desc.ViewDimension == D3D12_UAV_DIMENSION_TEXTURE2DARRAY);
uav_desc.Texture2DArray.MipSlice = p_mipmap;
uav_desc.Texture2DArray.FirstArraySlice = p_layer;
uav_desc.Texture2DArray.ArraySize = p_layers;
} break;
default:
break;
}
}
// Bookkeep.
TextureInfo *tex_info = VersatileResource::allocate<TextureInfo>(resources_allocator);
tex_info->resource = resource;
tex_info->states_ptr = owner_tex_info->states_ptr;
tex_info->format = p_view.format;
tex_info->desc = new_tex_resource_desc;
if (p_slice_type == (TextureSliceType)-1) {
tex_info->base_layer = owner_tex_info->base_layer;
tex_info->layers = owner_tex_info->layers;
tex_info->base_mip = owner_tex_info->base_mip;
tex_info->mipmaps = owner_tex_info->mipmaps;
} else {
tex_info->base_layer = p_layer;
tex_info->layers = p_layers;
tex_info->base_mip = p_mipmap;
tex_info->mipmaps = p_mipmaps;
}
tex_info->view_descs.srv = srv_desc;
tex_info->view_descs.uav = uav_desc;
tex_info->main_texture = owner_tex_info;
return TextureID(tex_info);
}
void RenderingDeviceDriverD3D12::texture_free(TextureID p_texture) {
TextureInfo *tex_info = (TextureInfo *)p_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
VersatileResource::free(resources_allocator, tex_info);
}
uint64_t RenderingDeviceDriverD3D12::texture_get_allocation_size(TextureID p_texture) {
const TextureInfo *tex_info = (const TextureInfo *)p_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
return tex_info->owner_info.allocation ? tex_info->owner_info.allocation->GetSize() : 0;
}
void RenderingDeviceDriverD3D12::texture_get_copyable_layout(TextureID p_texture, const TextureSubresource &p_subresource, TextureCopyableLayout *r_layout) {
TextureInfo *tex_info = (TextureInfo *)p_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
UINT subresource = tex_info->desc.CalcSubresource(p_subresource.mipmap, p_subresource.layer, 0);
D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint = {};
UINT64 subresource_total_size = 0;
device->GetCopyableFootprints(
&tex_info->desc,
subresource,
1,
0,
&footprint,
nullptr,
nullptr,
&subresource_total_size);
*r_layout = {};
r_layout->offset = footprint.Offset;
r_layout->size = subresource_total_size;
r_layout->row_pitch = footprint.Footprint.RowPitch;
r_layout->depth_pitch = subresource_total_size / tex_info->desc.Depth();
r_layout->layer_pitch = subresource_total_size / tex_info->desc.ArraySize();
}
uint8_t *RenderingDeviceDriverD3D12::texture_map(TextureID p_texture, const TextureSubresource &p_subresource) {
TextureInfo *tex_info = (TextureInfo *)p_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
#ifdef DEBUG_ENABLED
ERR_FAIL_COND_V(tex_info->mapped_subresource != UINT_MAX, nullptr);
#endif
UINT plane = _compute_plane_slice(tex_info->format, p_subresource.aspect);
UINT subresource = tex_info->desc.CalcSubresource(p_subresource.mipmap, p_subresource.layer, plane);
void *data_ptr = nullptr;
HRESULT res = tex_info->resource->Map(subresource, &VOID_RANGE, &data_ptr);
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), nullptr, "Map failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
tex_info->mapped_subresource = subresource;
return (uint8_t *)data_ptr;
}
void RenderingDeviceDriverD3D12::texture_unmap(TextureID p_texture) {
TextureInfo *tex_info = (TextureInfo *)p_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
#ifdef DEBUG_ENABLED
ERR_FAIL_COND(tex_info->mapped_subresource == UINT_MAX);
#endif
tex_info->resource->Unmap(tex_info->mapped_subresource, &VOID_RANGE);
tex_info->mapped_subresource = UINT_MAX;
}
BitField<RDD::TextureUsageBits> RenderingDeviceDriverD3D12::texture_get_usages_supported_by_format(DataFormat p_format, bool p_cpu_readable) {
D3D12_FEATURE_DATA_FORMAT_SUPPORT srv_rtv_support = {};
srv_rtv_support.Format = RD_TO_D3D12_FORMAT[p_format].general_format;
if (srv_rtv_support.Format != DXGI_FORMAT_UNKNOWN) { // Some implementations (i.e., vkd3d-proton) error out instead of returning empty.
HRESULT res = device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &srv_rtv_support, sizeof(srv_rtv_support));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), false, "CheckFeatureSupport failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
}
D3D12_FEATURE_DATA_FORMAT_SUPPORT &uav_support = srv_rtv_support; // Fine for now.
D3D12_FEATURE_DATA_FORMAT_SUPPORT dsv_support = {};
dsv_support.Format = RD_TO_D3D12_FORMAT[p_format].dsv_format;
if (dsv_support.Format != DXGI_FORMAT_UNKNOWN) { // See above.
HRESULT res = device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &dsv_support, sizeof(dsv_support));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), false, "CheckFeatureSupport failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
}
// Everything supported by default makes an all-or-nothing check easier for the caller.
BitField<RDD::TextureUsageBits> supported = INT64_MAX;
// Per https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_format_support1,
// as long as the resource can be used as a texture, Sample() will work with point filter at least.
// However, we've empirically found that checking for at least D3D12_FORMAT_SUPPORT1_SHADER_LOAD is needed.
// That's almost good for integer formats. The problem is that theoretically there may be
// float formats that support LOAD but not SAMPLE fully, so this check will not detect
// such a flaw in the format. Linearly interpolated sampling would just not work on them.
if (!(srv_rtv_support.Support1 & (D3D12_FORMAT_SUPPORT1_SHADER_LOAD | D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE)) ||
RD_TO_D3D12_FORMAT[p_format].general_format == DXGI_FORMAT_UNKNOWN) {
supported.clear_flag(TEXTURE_USAGE_SAMPLING_BIT);
}
if (!(srv_rtv_support.Support1 & D3D12_FORMAT_SUPPORT1_RENDER_TARGET)) {
supported.clear_flag(TEXTURE_USAGE_COLOR_ATTACHMENT_BIT);
}
if (!(dsv_support.Support1 & D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL)) {
supported.clear_flag(TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
}
if (!(uav_support.Support1 & D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW)) { // Maybe check LOAD/STORE, too?
supported.clear_flag(TEXTURE_USAGE_STORAGE_BIT);
}
if (!(uav_support.Support2 & D3D12_FORMAT_SUPPORT2_UAV_ATOMIC_ADD)) { // Check a basic atomic at least.
supported.clear_flag(TEXTURE_USAGE_STORAGE_ATOMIC_BIT);
}
if (RD_TO_D3D12_FORMAT[p_format].general_format != DXGI_FORMAT_R8_UINT) {
supported.clear_flag(TEXTURE_USAGE_VRS_ATTACHMENT_BIT);
}
return supported;
}
bool RenderingDeviceDriverD3D12::texture_can_make_shared_with_format(TextureID p_texture, DataFormat p_format, bool &r_raw_reinterpretation) {
r_raw_reinterpretation = false;
if (format_capabilities.relaxed_casting_supported) {
// Relaxed casting is supported, there should be no need to check for format family compatibility.
return true;
} else {
TextureInfo *tex_info = (TextureInfo *)p_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (tex_info->format == DATA_FORMAT_R16_UINT && p_format == DATA_FORMAT_R4G4B4A4_UNORM_PACK16) {
// Specific cases that require buffer reinterpretation.
r_raw_reinterpretation = true;
return false;
} else if (RD_TO_D3D12_FORMAT[tex_info->format].family != RD_TO_D3D12_FORMAT[p_format].family) {
// Format family is different but copying resources directly is possible.
return false;
} else {
// Format family is the same and the view can just cast the format.
return true;
}
}
}
/*****************/
/**** SAMPLER ****/
/*****************/
static const D3D12_TEXTURE_ADDRESS_MODE RD_REPEAT_MODE_TO_D3D12_ADDRES_MODE[RDD::SAMPLER_REPEAT_MODE_MAX] = {
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
D3D12_TEXTURE_ADDRESS_MODE_MIRROR,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
D3D12_TEXTURE_ADDRESS_MODE_BORDER,
D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE,
};
static const FLOAT RD_TO_D3D12_SAMPLER_BORDER_COLOR[RDD::SAMPLER_BORDER_COLOR_MAX][4] = {
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 1 },
{ 0, 0, 0, 1 },
{ 1, 1, 1, 1 },
{ 1, 1, 1, 1 },
};
RDD::SamplerID RenderingDeviceDriverD3D12::sampler_create(const SamplerState &p_state) {
uint32_t slot = UINT32_MAX;
if (samplers.is_empty()) {
// Adding a seemigly busy slot 0 makes things easier elsewhere.
samplers.push_back({});
samplers.push_back({});
slot = 1;
} else {
for (uint32_t i = 1; i < samplers.size(); i++) {
if ((int)samplers[i].Filter == INT_MAX) {
slot = i;
break;
}
}
if (slot == UINT32_MAX) {
slot = samplers.size();
samplers.push_back({});
}
}
D3D12_SAMPLER_DESC &sampler_desc = samplers[slot];
if (p_state.use_anisotropy) {
sampler_desc.Filter = D3D12_ENCODE_ANISOTROPIC_FILTER(D3D12_FILTER_REDUCTION_TYPE_STANDARD);
sampler_desc.MaxAnisotropy = p_state.anisotropy_max;
} else {
static const D3D12_FILTER_TYPE RD_FILTER_TYPE_TO_D3D12[] = {
D3D12_FILTER_TYPE_POINT, // SAMPLER_FILTER_NEAREST.
D3D12_FILTER_TYPE_LINEAR, // SAMPLER_FILTER_LINEAR.
};
sampler_desc.Filter = D3D12_ENCODE_BASIC_FILTER(
RD_FILTER_TYPE_TO_D3D12[p_state.min_filter],
RD_FILTER_TYPE_TO_D3D12[p_state.mag_filter],
RD_FILTER_TYPE_TO_D3D12[p_state.mip_filter],
p_state.enable_compare ? D3D12_FILTER_REDUCTION_TYPE_COMPARISON : D3D12_FILTER_REDUCTION_TYPE_STANDARD);
}
sampler_desc.AddressU = RD_REPEAT_MODE_TO_D3D12_ADDRES_MODE[p_state.repeat_u];
sampler_desc.AddressV = RD_REPEAT_MODE_TO_D3D12_ADDRES_MODE[p_state.repeat_v];
sampler_desc.AddressW = RD_REPEAT_MODE_TO_D3D12_ADDRES_MODE[p_state.repeat_w];
for (int i = 0; i < 4; i++) {
sampler_desc.BorderColor[i] = RD_TO_D3D12_SAMPLER_BORDER_COLOR[p_state.border_color][i];
}
sampler_desc.MinLOD = p_state.min_lod;
sampler_desc.MaxLOD = p_state.max_lod;
sampler_desc.MipLODBias = p_state.lod_bias;
sampler_desc.ComparisonFunc = p_state.enable_compare ? RD_TO_D3D12_COMPARE_OP[p_state.compare_op] : D3D12_COMPARISON_FUNC_NEVER;
// TODO: Emulate somehow?
if (p_state.unnormalized_uvw) {
WARN_PRINT("Creating a sampler with unnormalized UVW, which is not supported.");
}
return SamplerID(slot);
}
void RenderingDeviceDriverD3D12::sampler_free(SamplerID p_sampler) {
samplers[p_sampler.id].Filter = (D3D12_FILTER)INT_MAX;
}
bool RenderingDeviceDriverD3D12::sampler_is_format_supported_for_filter(DataFormat p_format, SamplerFilter p_filter) {
D3D12_FEATURE_DATA_FORMAT_SUPPORT srv_rtv_support = {};
srv_rtv_support.Format = RD_TO_D3D12_FORMAT[p_format].general_format;
HRESULT res = device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &srv_rtv_support, sizeof(srv_rtv_support));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), false, "CheckFeatureSupport failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
return (srv_rtv_support.Support1 & D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE);
}
/**********************/
/**** VERTEX ARRAY ****/
/**********************/
RDD::VertexFormatID RenderingDeviceDriverD3D12::vertex_format_create(VectorView<VertexAttribute> p_vertex_attribs) {
VertexFormatInfo *vf_info = VersatileResource::allocate<VertexFormatInfo>(resources_allocator);
vf_info->input_elem_descs.resize(p_vertex_attribs.size());
vf_info->vertex_buffer_strides.resize(p_vertex_attribs.size());
for (uint32_t i = 0; i < p_vertex_attribs.size(); i++) {
vf_info->input_elem_descs[i] = {};
vf_info->input_elem_descs[i].SemanticName = "TEXCOORD";
vf_info->input_elem_descs[i].SemanticIndex = p_vertex_attribs[i].location;
vf_info->input_elem_descs[i].Format = RD_TO_D3D12_FORMAT[p_vertex_attribs[i].format].general_format;
vf_info->input_elem_descs[i].InputSlot = i; // TODO: Can the same slot be used if data comes from the same buffer (regardless format)?
vf_info->input_elem_descs[i].AlignedByteOffset = p_vertex_attribs[i].offset;
if (p_vertex_attribs[i].frequency == VERTEX_FREQUENCY_INSTANCE) {
vf_info->input_elem_descs[i].InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA;
vf_info->input_elem_descs[i].InstanceDataStepRate = 1;
} else {
vf_info->input_elem_descs[i].InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
vf_info->input_elem_descs[i].InstanceDataStepRate = 0;
}
vf_info->vertex_buffer_strides[i] = p_vertex_attribs[i].stride;
}
return VertexFormatID(vf_info);
}
void RenderingDeviceDriverD3D12::vertex_format_free(VertexFormatID p_vertex_format) {
VertexFormatInfo *vf_info = (VertexFormatInfo *)p_vertex_format.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
VersatileResource::free(resources_allocator, vf_info);
}
/******************/
/**** BARRIERS ****/
/******************/
static D3D12_BARRIER_ACCESS _rd_texture_layout_access_mask(RDD::TextureLayout p_texture_layout) {
switch (p_texture_layout) {
case RDD::TEXTURE_LAYOUT_STORAGE_OPTIMAL:
return D3D12_BARRIER_ACCESS_UNORDERED_ACCESS;
case RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
return D3D12_BARRIER_ACCESS_RENDER_TARGET;
case RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
return D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ | D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE;
case RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
return D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ;
case RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
return D3D12_BARRIER_ACCESS_SHADER_RESOURCE;
case RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL:
return D3D12_BARRIER_ACCESS_COPY_SOURCE;
case RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL:
return D3D12_BARRIER_ACCESS_COPY_DEST;
case RDD::TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL:
return D3D12_BARRIER_ACCESS_RESOLVE_SOURCE;
case RDD::TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL:
return D3D12_BARRIER_ACCESS_RESOLVE_DEST;
case RDD::TEXTURE_LAYOUT_VRS_ATTACHMENT_OPTIMAL:
return D3D12_BARRIER_ACCESS_SHADING_RATE_SOURCE;
default:
return D3D12_BARRIER_ACCESS_NO_ACCESS;
}
}
static void _rd_access_to_d3d12_and_mask(BitField<RDD::BarrierAccessBits> p_access, RDD::TextureLayout p_texture_layout, D3D12_BARRIER_ACCESS &r_access, D3D12_BARRIER_SYNC &r_sync_mask) {
r_access = D3D12_BARRIER_ACCESS_COMMON;
r_sync_mask = D3D12_BARRIER_SYNC_NONE;
if (p_access.has_flag(RDD::BARRIER_ACCESS_INDIRECT_COMMAND_READ_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_INDIRECT_ARGUMENT;
r_sync_mask |= D3D12_BARRIER_SYNC_EXECUTE_INDIRECT;
}
if (p_access.has_flag(RDD::BARRIER_ACCESS_INDEX_READ_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_INDEX_BUFFER;
r_sync_mask |= D3D12_BARRIER_SYNC_INDEX_INPUT | D3D12_BARRIER_SYNC_DRAW;
}
if (p_access.has_flag(RDD::BARRIER_ACCESS_VERTEX_ATTRIBUTE_READ_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_VERTEX_BUFFER;
r_sync_mask |= D3D12_BARRIER_SYNC_VERTEX_SHADING | D3D12_BARRIER_SYNC_DRAW | D3D12_BARRIER_SYNC_ALL_SHADING;
}
if (p_access.has_flag(RDD::BARRIER_ACCESS_UNIFORM_READ_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_CONSTANT_BUFFER;
r_sync_mask |= D3D12_BARRIER_SYNC_VERTEX_SHADING | D3D12_BARRIER_SYNC_PIXEL_SHADING | D3D12_BARRIER_SYNC_COMPUTE_SHADING |
D3D12_BARRIER_SYNC_DRAW | D3D12_BARRIER_SYNC_ALL_SHADING;
}
if (p_access.has_flag(RDD::BARRIER_ACCESS_INPUT_ATTACHMENT_READ_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_RENDER_TARGET;
r_sync_mask |= D3D12_BARRIER_SYNC_DRAW | D3D12_BARRIER_SYNC_RENDER_TARGET;
}
if (p_access.has_flag(RDD::BARRIER_ACCESS_COPY_READ_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_COPY_SOURCE;
r_sync_mask |= D3D12_BARRIER_SYNC_COPY;
}
if (p_access.has_flag(RDD::BARRIER_ACCESS_COPY_WRITE_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_COPY_DEST;
r_sync_mask |= D3D12_BARRIER_SYNC_COPY;
}
if (p_access.has_flag(RDD::BARRIER_ACCESS_RESOLVE_READ_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_RESOLVE_SOURCE;
r_sync_mask |= D3D12_BARRIER_SYNC_RESOLVE;
}
if (p_access.has_flag(RDD::BARRIER_ACCESS_RESOLVE_WRITE_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_RESOLVE_DEST;
r_sync_mask |= D3D12_BARRIER_SYNC_RESOLVE;
}
if (p_access.has_flag(RDD::BARRIER_ACCESS_FRAGMENT_SHADING_RATE_ATTACHMENT_READ_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_SHADING_RATE_SOURCE;
r_sync_mask |= D3D12_BARRIER_SYNC_PIXEL_SHADING | D3D12_BARRIER_SYNC_ALL_SHADING;
}
const D3D12_BARRIER_SYNC unordered_access_mask = D3D12_BARRIER_SYNC_VERTEX_SHADING | D3D12_BARRIER_SYNC_PIXEL_SHADING | D3D12_BARRIER_SYNC_COMPUTE_SHADING |<--- Same expression on both sides of '|'. [+]Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
D3D12_BARRIER_SYNC_VERTEX_SHADING | D3D12_BARRIER_SYNC_DRAW | D3D12_BARRIER_SYNC_ALL_SHADING | D3D12_BARRIER_SYNC_CLEAR_UNORDERED_ACCESS_VIEW;
if (p_access.has_flag(RDD::BARRIER_ACCESS_STORAGE_CLEAR_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_UNORDERED_ACCESS;
r_sync_mask |= unordered_access_mask;
}
// These access bits only have compatibility with certain layouts unlike in Vulkan where they imply specific operations in the same layout.
if (p_access.has_flag(RDD::BARRIER_ACCESS_SHADER_WRITE_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_UNORDERED_ACCESS;
r_sync_mask |= unordered_access_mask;
} else if (p_access.has_flag(RDD::BARRIER_ACCESS_SHADER_READ_BIT)) {
if (p_texture_layout == RDD::TEXTURE_LAYOUT_STORAGE_OPTIMAL) {
// Unordered access must be enforced if the texture is using the storage layout.
r_access |= D3D12_BARRIER_ACCESS_UNORDERED_ACCESS;
r_sync_mask |= unordered_access_mask;
} else {
r_access |= D3D12_BARRIER_ACCESS_SHADER_RESOURCE;
r_sync_mask |= D3D12_BARRIER_SYNC_VERTEX_SHADING | D3D12_BARRIER_SYNC_PIXEL_SHADING | D3D12_BARRIER_SYNC_COMPUTE_SHADING | D3D12_BARRIER_SYNC_DRAW | D3D12_BARRIER_SYNC_ALL_SHADING;
}
}
if (p_access.has_flag(RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_WRITE_BIT) || p_access.has_flag(RDD::BARRIER_ACCESS_COLOR_ATTACHMENT_READ_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_RENDER_TARGET;
r_sync_mask |= D3D12_BARRIER_SYNC_DRAW | D3D12_BARRIER_SYNC_RENDER_TARGET;
}
if (p_access.has_flag(RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE;
r_sync_mask |= D3D12_BARRIER_SYNC_DRAW | D3D12_BARRIER_SYNC_DEPTH_STENCIL;
} else if (p_access.has_flag(RDD::BARRIER_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT)) {
r_access |= D3D12_BARRIER_ACCESS_DEPTH_STENCIL_READ;
r_sync_mask |= D3D12_BARRIER_SYNC_DRAW | D3D12_BARRIER_SYNC_DEPTH_STENCIL;
}
}
static void _rd_stages_to_d3d12(BitField<RDD::PipelineStageBits> p_stages, D3D12_BARRIER_SYNC &r_sync) {
if (p_stages.has_flag(RDD::PIPELINE_STAGE_ALL_COMMANDS_BIT)) {
r_sync = D3D12_BARRIER_SYNC_ALL;
} else {
if (p_stages.has_flag(RDD::PIPELINE_STAGE_DRAW_INDIRECT_BIT)) {
r_sync |= D3D12_BARRIER_SYNC_EXECUTE_INDIRECT;
}
if (p_stages.has_flag(RDD::PIPELINE_STAGE_VERTEX_INPUT_BIT)) {
r_sync |= D3D12_BARRIER_SYNC_INDEX_INPUT;
}
if (p_stages.has_flag(RDD::PIPELINE_STAGE_VERTEX_SHADER_BIT)) {
r_sync |= D3D12_BARRIER_SYNC_VERTEX_SHADING;
}
if (p_stages.has_flag(RDD::PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT) || p_stages.has_flag(RDD::PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT) || p_stages.has_flag(RDD::PIPELINE_STAGE_GEOMETRY_SHADER_BIT)) {
// There's no granularity for tessellation or geometry stages. The specification defines it as part of vertex shading.
r_sync |= D3D12_BARRIER_SYNC_VERTEX_SHADING;
}
if (p_stages.has_flag(RDD::PIPELINE_STAGE_FRAGMENT_SHADER_BIT)) {
r_sync |= D3D12_BARRIER_SYNC_PIXEL_SHADING;
}
if (p_stages.has_flag(RDD::PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT) || p_stages.has_flag(RDD::PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT)) {
// Covers both read and write operations for depth stencil.
r_sync |= D3D12_BARRIER_SYNC_DEPTH_STENCIL;
}
if (p_stages.has_flag(RDD::PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT)) {
r_sync |= D3D12_BARRIER_SYNC_RENDER_TARGET;
}
if (p_stages.has_flag(RDD::PIPELINE_STAGE_COMPUTE_SHADER_BIT)) {
r_sync |= D3D12_BARRIER_SYNC_COMPUTE_SHADING;
}
if (p_stages.has_flag(RDD::PIPELINE_STAGE_COPY_BIT)) {
r_sync |= D3D12_BARRIER_SYNC_COPY;
}
if (p_stages.has_flag(RDD::PIPELINE_STAGE_RESOLVE_BIT)) {
r_sync |= D3D12_BARRIER_SYNC_RESOLVE;
}
if (p_stages.has_flag(RDD::PIPELINE_STAGE_CLEAR_STORAGE_BIT)) {
r_sync |= D3D12_BARRIER_SYNC_CLEAR_UNORDERED_ACCESS_VIEW;
}
if (p_stages.has_flag(RDD::PIPELINE_STAGE_ALL_GRAPHICS_BIT)) {
r_sync |= D3D12_BARRIER_SYNC_DRAW;
}
}
}
static void _rd_stages_and_access_to_d3d12(BitField<RDD::PipelineStageBits> p_stages, RDD::TextureLayout p_texture_layout, BitField<RDD::BarrierAccessBits> p_access, D3D12_BARRIER_SYNC &r_sync, D3D12_BARRIER_ACCESS &r_access) {
D3D12_BARRIER_SYNC sync_mask;
r_sync = D3D12_BARRIER_SYNC_NONE;
if (p_texture_layout == RDD::TEXTURE_LAYOUT_UNDEFINED) {
// Undefined texture layouts are a special case where no access bits or synchronization scopes are allowed.
r_access = D3D12_BARRIER_ACCESS_NO_ACCESS;
return;
}
// Convert access bits to the D3D12 barrier access bits.
_rd_access_to_d3d12_and_mask(p_access, p_texture_layout, r_access, sync_mask);
if (p_texture_layout != RDD::TEXTURE_LAYOUT_MAX) {
// Only allow the access bits compatible with the texture layout.
r_access &= _rd_texture_layout_access_mask(p_texture_layout);
}
// Convert stage bits to the D3D12 synchronization scope bits.
_rd_stages_to_d3d12(p_stages, r_sync);
// Only enable synchronization stages compatible with the access bits that were used.
r_sync &= sync_mask;
if (r_sync == D3D12_BARRIER_SYNC_NONE) {
if (p_access.is_empty()) {
// No valid synchronization scope was defined and no access in particular is required.
r_access = D3D12_BARRIER_ACCESS_NO_ACCESS;
} else {
// Access is required but the synchronization scope wasn't compatible. We fall back to the global synchronization scope and access.
r_sync = D3D12_BARRIER_SYNC_ALL;
r_access = D3D12_BARRIER_ACCESS_COMMON;
}
}
}
static D3D12_BARRIER_LAYOUT _rd_texture_layout_to_d3d12_barrier_layout(RDD::TextureLayout p_texture_layout) {
switch (p_texture_layout) {
case RDD::TEXTURE_LAYOUT_UNDEFINED:
return D3D12_BARRIER_LAYOUT_UNDEFINED;
case RDD::TEXTURE_LAYOUT_GENERAL:
return D3D12_BARRIER_LAYOUT_COMMON;
case RDD::TEXTURE_LAYOUT_STORAGE_OPTIMAL:
return D3D12_BARRIER_LAYOUT_UNORDERED_ACCESS;
case RDD::TEXTURE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
return D3D12_BARRIER_LAYOUT_RENDER_TARGET;
case RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
return D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE;
case RDD::TEXTURE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
return D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_READ;
case RDD::TEXTURE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
return D3D12_BARRIER_LAYOUT_SHADER_RESOURCE;
case RDD::TEXTURE_LAYOUT_COPY_SRC_OPTIMAL:
return D3D12_BARRIER_LAYOUT_COPY_SOURCE;
case RDD::TEXTURE_LAYOUT_COPY_DST_OPTIMAL:
return D3D12_BARRIER_LAYOUT_COPY_DEST;
case RDD::TEXTURE_LAYOUT_RESOLVE_SRC_OPTIMAL:
return D3D12_BARRIER_LAYOUT_RESOLVE_SOURCE;
case RDD::TEXTURE_LAYOUT_RESOLVE_DST_OPTIMAL:
return D3D12_BARRIER_LAYOUT_RESOLVE_DEST;
case RDD::TEXTURE_LAYOUT_VRS_ATTACHMENT_OPTIMAL:
return D3D12_BARRIER_LAYOUT_SHADING_RATE_SOURCE;
default:
DEV_ASSERT(false && "Unknown texture layout.");
return D3D12_BARRIER_LAYOUT_UNDEFINED;
}
}
void RenderingDeviceDriverD3D12::command_pipeline_barrier(CommandBufferID p_cmd_buffer,
BitField<PipelineStageBits> p_src_stages,
BitField<PipelineStageBits> p_dst_stages,
VectorView<RDD::MemoryBarrier> p_memory_barriers,<--- Skipping configuration 'MemoryBarrier' since the value of 'MemoryBarrier' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
VectorView<RDD::BufferBarrier> p_buffer_barriers,
VectorView<RDD::TextureBarrier> p_texture_barriers) {
if (!barrier_capabilities.enhanced_barriers_supported) {
// Enhanced barriers are a requirement for this function.
return;
}
if (p_memory_barriers.size() == 0 && p_buffer_barriers.size() == 0 && p_texture_barriers.size() == 0) {
// At least one barrier must be present in the arguments.
return;
}
// The command list must support the required interface.
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)(p_cmd_buffer.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
ID3D12GraphicsCommandList7 *cmd_list_7 = nullptr;
HRESULT res = cmd_buf_info->cmd_list->QueryInterface(IID_PPV_ARGS(&cmd_list_7));
ERR_FAIL_COND(FAILED(res));
// Convert the RDD barriers to D3D12 enhanced barriers.
thread_local LocalVector<D3D12_GLOBAL_BARRIER> global_barriers;
thread_local LocalVector<D3D12_BUFFER_BARRIER> buffer_barriers;
thread_local LocalVector<D3D12_TEXTURE_BARRIER> texture_barriers;
global_barriers.clear();
buffer_barriers.clear();
texture_barriers.clear();
D3D12_GLOBAL_BARRIER global_barrier = {};
for (uint32_t i = 0; i < p_memory_barriers.size(); i++) {
const MemoryBarrier &memory_barrier = p_memory_barriers[i];<--- Skipping configuration 'MemoryBarrier' since the value of 'MemoryBarrier' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
_rd_stages_and_access_to_d3d12(p_src_stages, RDD::TEXTURE_LAYOUT_MAX, memory_barrier.src_access, global_barrier.SyncBefore, global_barrier.AccessBefore);
_rd_stages_and_access_to_d3d12(p_dst_stages, RDD::TEXTURE_LAYOUT_MAX, memory_barrier.dst_access, global_barrier.SyncAfter, global_barrier.AccessAfter);
global_barriers.push_back(global_barrier);
}
D3D12_BUFFER_BARRIER buffer_barrier_d3d12 = {};
buffer_barrier_d3d12.Offset = 0;
buffer_barrier_d3d12.Size = UINT64_MAX; // The specification says this must be the size of the buffer barrier.
for (uint32_t i = 0; i < p_buffer_barriers.size(); i++) {
const BufferBarrier &buffer_barrier_rd = p_buffer_barriers[i];
const BufferInfo *buffer_info = (const BufferInfo *)(buffer_barrier_rd.buffer.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_rd_stages_and_access_to_d3d12(p_src_stages, RDD::TEXTURE_LAYOUT_MAX, buffer_barrier_rd.src_access, buffer_barrier_d3d12.SyncBefore, buffer_barrier_d3d12.AccessBefore);
_rd_stages_and_access_to_d3d12(p_dst_stages, RDD::TEXTURE_LAYOUT_MAX, buffer_barrier_rd.dst_access, buffer_barrier_d3d12.SyncAfter, buffer_barrier_d3d12.AccessAfter);
buffer_barrier_d3d12.pResource = buffer_info->resource;
buffer_barriers.push_back(buffer_barrier_d3d12);
}
D3D12_TEXTURE_BARRIER texture_barrier_d3d12 = {};
for (uint32_t i = 0; i < p_texture_barriers.size(); i++) {
const TextureBarrier &texture_barrier_rd = p_texture_barriers[i];
const TextureInfo *texture_info = (const TextureInfo *)(texture_barrier_rd.texture.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (texture_info->main_texture) {
texture_info = texture_info->main_texture;
}
_rd_stages_and_access_to_d3d12(p_src_stages, texture_barrier_rd.prev_layout, texture_barrier_rd.src_access, texture_barrier_d3d12.SyncBefore, texture_barrier_d3d12.AccessBefore);
_rd_stages_and_access_to_d3d12(p_dst_stages, texture_barrier_rd.next_layout, texture_barrier_rd.dst_access, texture_barrier_d3d12.SyncAfter, texture_barrier_d3d12.AccessAfter);
texture_barrier_d3d12.LayoutBefore = _rd_texture_layout_to_d3d12_barrier_layout(texture_barrier_rd.prev_layout);
texture_barrier_d3d12.LayoutAfter = _rd_texture_layout_to_d3d12_barrier_layout(texture_barrier_rd.next_layout);
texture_barrier_d3d12.pResource = texture_info->resource;
if (texture_barrier_rd.subresources.mipmap_count == texture_info->mipmaps && texture_barrier_rd.subresources.layer_count == texture_info->layers) {
// So, all resources. Then, let's be explicit about it so D3D12 doesn't think
// we are dealing with a subset of subresources.
texture_barrier_d3d12.Subresources.IndexOrFirstMipLevel = 0xffffffff;
texture_barrier_d3d12.Subresources.NumMipLevels = 0;
// Because NumMipLevels == 0, all the other fields are ignored by D3D12.
} else {
texture_barrier_d3d12.Subresources.IndexOrFirstMipLevel = texture_barrier_rd.subresources.base_mipmap;
texture_barrier_d3d12.Subresources.NumMipLevels = texture_barrier_rd.subresources.mipmap_count;
texture_barrier_d3d12.Subresources.FirstArraySlice = texture_barrier_rd.subresources.base_layer;
texture_barrier_d3d12.Subresources.NumArraySlices = texture_barrier_rd.subresources.layer_count;
texture_barrier_d3d12.Subresources.FirstPlane = _compute_plane_slice(texture_info->format, texture_barrier_rd.subresources.aspect);
texture_barrier_d3d12.Subresources.NumPlanes = format_get_plane_count(texture_info->format);
}
texture_barrier_d3d12.Flags = (texture_barrier_rd.prev_layout == RDD::TEXTURE_LAYOUT_UNDEFINED) ? D3D12_TEXTURE_BARRIER_FLAG_DISCARD : D3D12_TEXTURE_BARRIER_FLAG_NONE;
texture_barriers.push_back(texture_barrier_d3d12);
}
// Define the barrier groups and execute.
D3D12_BARRIER_GROUP barrier_groups[3] = {};
uint32_t barrier_groups_count = 0;
if (!global_barriers.is_empty()) {
D3D12_BARRIER_GROUP &barrier_group = barrier_groups[barrier_groups_count++];
barrier_group.Type = D3D12_BARRIER_TYPE_GLOBAL;
barrier_group.NumBarriers = global_barriers.size();
barrier_group.pGlobalBarriers = global_barriers.ptr();
}
if (!buffer_barriers.is_empty()) {
D3D12_BARRIER_GROUP &barrier_group = barrier_groups[barrier_groups_count++];
barrier_group.Type = D3D12_BARRIER_TYPE_BUFFER;
barrier_group.NumBarriers = buffer_barriers.size();
barrier_group.pBufferBarriers = buffer_barriers.ptr();
}
if (!texture_barriers.is_empty()) {
D3D12_BARRIER_GROUP &barrier_group = barrier_groups[barrier_groups_count++];
barrier_group.Type = D3D12_BARRIER_TYPE_TEXTURE;
barrier_group.NumBarriers = texture_barriers.size();
barrier_group.pTextureBarriers = texture_barriers.ptr();
}
cmd_list_7->Barrier(barrier_groups_count, barrier_groups);
}
/****************/
/**** FENCES ****/
/****************/
RDD::FenceID RenderingDeviceDriverD3D12::fence_create() {
ComPtr<ID3D12Fence> d3d_fence;
HRESULT res = device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(d3d_fence.GetAddressOf()));
ERR_FAIL_COND_V(!SUCCEEDED(res), FenceID());
HANDLE event_handle = CreateEvent(nullptr, FALSE, FALSE, nullptr);
ERR_FAIL_NULL_V(event_handle, FenceID());
FenceInfo *fence = memnew(FenceInfo);
fence->d3d_fence = d3d_fence;
fence->event_handle = event_handle;
return FenceID(fence);
}
Error RenderingDeviceDriverD3D12::fence_wait(FenceID p_fence) {
FenceInfo *fence = (FenceInfo *)(p_fence.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
fence->d3d_fence->SetEventOnCompletion(fence->fence_value, fence->event_handle);
DWORD res = WaitForSingleObjectEx(fence->event_handle, INFINITE, FALSE);
#ifdef PIX_ENABLED
PIXNotifyWakeFromFenceSignal(fence->event_handle);
#endif
return (res == WAIT_FAILED) ? FAILED : OK;
}
void RenderingDeviceDriverD3D12::fence_free(FenceID p_fence) {
FenceInfo *fence = (FenceInfo *)(p_fence.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
CloseHandle(fence->event_handle);
memdelete(fence);
}
/********************/
/**** SEMAPHORES ****/
/********************/
RDD::SemaphoreID RenderingDeviceDriverD3D12::semaphore_create() {
ComPtr<ID3D12Fence> d3d_fence;
HRESULT res = device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(d3d_fence.GetAddressOf()));
ERR_FAIL_COND_V(!SUCCEEDED(res), SemaphoreID());
SemaphoreInfo *semaphore = memnew(SemaphoreInfo);
semaphore->d3d_fence = d3d_fence;
return SemaphoreID(semaphore);
}
void RenderingDeviceDriverD3D12::semaphore_free(SemaphoreID p_semaphore) {
SemaphoreInfo *semaphore = (SemaphoreInfo *)(p_semaphore.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
memdelete(semaphore);
}
/******************/
/**** COMMANDS ****/
/******************/
// ----- QUEUE FAMILY -----
RDD::CommandQueueFamilyID RenderingDeviceDriverD3D12::command_queue_family_get(BitField<CommandQueueFamilyBits> p_cmd_queue_family_bits, RenderingContextDriver::SurfaceID p_surface) {
// Return the command list type encoded plus one so zero is an invalid value.
// The only ones that support presenting to a surface are direct queues.
if (p_cmd_queue_family_bits.has_flag(COMMAND_QUEUE_FAMILY_GRAPHICS_BIT) || (p_surface != 0)) {
return CommandQueueFamilyID(D3D12_COMMAND_LIST_TYPE_DIRECT + 1);
} else if (p_cmd_queue_family_bits.has_flag(COMMAND_QUEUE_FAMILY_COMPUTE_BIT)) {
return CommandQueueFamilyID(D3D12_COMMAND_LIST_TYPE_COMPUTE + 1);
} else if (p_cmd_queue_family_bits.has_flag(COMMAND_QUEUE_FAMILY_TRANSFER_BIT)) {
return CommandQueueFamilyID(D3D12_COMMAND_LIST_TYPE_COPY + 1);
} else {
return CommandQueueFamilyID();
}
}
// ----- QUEUE -----
RDD::CommandQueueID RenderingDeviceDriverD3D12::command_queue_create(CommandQueueFamilyID p_cmd_queue_family, bool p_identify_as_main_queue) {
ComPtr<ID3D12CommandQueue> d3d_queue;
D3D12_COMMAND_QUEUE_DESC queue_desc = {};
queue_desc.Type = (D3D12_COMMAND_LIST_TYPE)(p_cmd_queue_family.id - 1);
HRESULT res = device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(d3d_queue.GetAddressOf()));
ERR_FAIL_COND_V(!SUCCEEDED(res), CommandQueueID());
CommandQueueInfo *command_queue = memnew(CommandQueueInfo);
command_queue->d3d_queue = d3d_queue;
return CommandQueueID(command_queue);
}
Error RenderingDeviceDriverD3D12::command_queue_execute_and_present(CommandQueueID p_cmd_queue, VectorView<SemaphoreID> p_wait_semaphores, VectorView<CommandBufferID> p_cmd_buffers, VectorView<SemaphoreID> p_cmd_semaphores, FenceID p_cmd_fence, VectorView<SwapChainID> p_swap_chains) {
CommandQueueInfo *command_queue = (CommandQueueInfo *)(p_cmd_queue.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
for (uint32_t i = 0; i < p_wait_semaphores.size(); i++) {
const SemaphoreInfo *semaphore = (const SemaphoreInfo *)(p_wait_semaphores[i].id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
command_queue->d3d_queue->Wait(semaphore->d3d_fence.Get(), semaphore->fence_value);
}
if (p_cmd_buffers.size() > 0) {
thread_local LocalVector<ID3D12CommandList *> command_lists;
command_lists.resize(p_cmd_buffers.size());
for (uint32_t i = 0; i < p_cmd_buffers.size(); i++) {
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)(p_cmd_buffers[i].id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
command_lists[i] = cmd_buf_info->cmd_list.Get();
}
command_queue->d3d_queue->ExecuteCommandLists(command_lists.size(), command_lists.ptr());
for (uint32_t i = 0; i < p_cmd_semaphores.size(); i++) {
SemaphoreInfo *semaphore = (SemaphoreInfo *)(p_cmd_semaphores[i].id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
semaphore->fence_value++;
command_queue->d3d_queue->Signal(semaphore->d3d_fence.Get(), semaphore->fence_value);
}
if (p_cmd_fence) {
FenceInfo *fence = (FenceInfo *)(p_cmd_fence.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
fence->fence_value++;
command_queue->d3d_queue->Signal(fence->d3d_fence.Get(), fence->fence_value);
}
}
HRESULT res;
bool any_present_failed = false;
for (uint32_t i = 0; i < p_swap_chains.size(); i++) {
SwapChain *swap_chain = (SwapChain *)(p_swap_chains[i].id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
res = swap_chain->d3d_swap_chain->Present(swap_chain->sync_interval, swap_chain->present_flags);
if (!SUCCEEDED(res)) {
print_verbose(vformat("D3D12: Presenting swapchain failed with error 0x%08ux.", (uint64_t)res));
any_present_failed = true;
}
}
return any_present_failed ? FAILED : OK;
}
void RenderingDeviceDriverD3D12::command_queue_free(CommandQueueID p_cmd_queue) {
CommandQueueInfo *command_queue = (CommandQueueInfo *)(p_cmd_queue.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
memdelete(command_queue);
}
// ----- POOL -----
RDD::CommandPoolID RenderingDeviceDriverD3D12::command_pool_create(CommandQueueFamilyID p_cmd_queue_family, CommandBufferType p_cmd_buffer_type) {
CommandPoolInfo *command_pool = memnew(CommandPoolInfo);
command_pool->queue_family = p_cmd_queue_family;
command_pool->buffer_type = p_cmd_buffer_type;
return CommandPoolID(command_pool);
}
bool RenderingDeviceDriverD3D12::command_pool_reset(CommandPoolID p_cmd_pool) {
return true;
}
void RenderingDeviceDriverD3D12::command_pool_free(CommandPoolID p_cmd_pool) {
CommandPoolInfo *command_pool = (CommandPoolInfo *)(p_cmd_pool.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
// Destroy all command buffers associated with this command pool, mirroring Vulkan's behavior.
SelfList<CommandBufferInfo> *cmd_buf_elem = command_pool->command_buffers.first();
while (cmd_buf_elem != nullptr) {
CommandBufferInfo *cmd_buf_info = cmd_buf_elem->self();
cmd_buf_elem = cmd_buf_elem->next();
cmd_buf_info->cmd_list.Reset();
cmd_buf_info->cmd_allocator.Reset();
VersatileResource::free(resources_allocator, cmd_buf_info);
}
memdelete(command_pool);
}
// ----- BUFFER -----
RDD::CommandBufferID RenderingDeviceDriverD3D12::command_buffer_create(CommandPoolID p_cmd_pool) {
DEV_ASSERT(p_cmd_pool);
CommandPoolInfo *command_pool = (CommandPoolInfo *)(p_cmd_pool.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
D3D12_COMMAND_LIST_TYPE list_type;
if (command_pool->buffer_type == COMMAND_BUFFER_TYPE_SECONDARY) {
list_type = D3D12_COMMAND_LIST_TYPE_BUNDLE;
} else {
list_type = D3D12_COMMAND_LIST_TYPE(command_pool->queue_family.id - 1);
}
ID3D12CommandAllocator *cmd_allocator = nullptr;
{
HRESULT res = device->CreateCommandAllocator(list_type, IID_PPV_ARGS(&cmd_allocator));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), CommandBufferID(), "CreateCommandAllocator failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
}
ID3D12GraphicsCommandList *cmd_list = nullptr;
{
ComPtr<ID3D12Device4> device_4;
device->QueryInterface(device_4.GetAddressOf());
HRESULT res = E_FAIL;
if (device_4) {
res = device_4->CreateCommandList1(0, list_type, D3D12_COMMAND_LIST_FLAG_NONE, IID_PPV_ARGS(&cmd_list));
} else {
res = device->CreateCommandList(0, list_type, cmd_allocator, nullptr, IID_PPV_ARGS(&cmd_list));
}
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), CommandBufferID(), "CreateCommandList failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
if (!device_4) {
cmd_list->Close();
}
}
// Bookkeep
CommandBufferInfo *cmd_buf_info = VersatileResource::allocate<CommandBufferInfo>(resources_allocator);
cmd_buf_info->cmd_allocator = cmd_allocator;
cmd_buf_info->cmd_list = cmd_list;
// Add this command buffer to the command pool's list of command buffers.
command_pool->command_buffers.add(&cmd_buf_info->command_buffer_info_elem);
return CommandBufferID(cmd_buf_info);
}
bool RenderingDeviceDriverD3D12::command_buffer_begin(CommandBufferID p_cmd_buffer) {
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
HRESULT res = cmd_buf_info->cmd_allocator->Reset();
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), false, "Reset failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
res = cmd_buf_info->cmd_list->Reset(cmd_buf_info->cmd_allocator.Get(), nullptr);
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), false, "Reset failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
return true;
}
bool RenderingDeviceDriverD3D12::command_buffer_begin_secondary(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, uint32_t p_subpass, FramebufferID p_framebuffer) {
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
HRESULT res = cmd_buf_info->cmd_allocator->Reset();
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), false, "Reset failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
res = cmd_buf_info->cmd_list->Reset(cmd_buf_info->cmd_allocator.Get(), nullptr);
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), false, "Reset failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
return true;
}
void RenderingDeviceDriverD3D12::command_buffer_end(CommandBufferID p_cmd_buffer) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
HRESULT res = cmd_buf_info->cmd_list->Close();
ERR_FAIL_COND_MSG(!SUCCEEDED(res), "Close failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
cmd_buf_info->graphics_pso = nullptr;
cmd_buf_info->graphics_root_signature_crc = 0;
cmd_buf_info->compute_pso = nullptr;
cmd_buf_info->compute_root_signature_crc = 0;
cmd_buf_info->descriptor_heaps_set = false;
}
void RenderingDeviceDriverD3D12::command_buffer_execute_secondary(CommandBufferID p_cmd_buffer, VectorView<CommandBufferID> p_secondary_cmd_buffers) {
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
for (uint32_t i = 0; i < p_secondary_cmd_buffers.size(); i++) {
const CommandBufferInfo *secondary_cb_info = (const CommandBufferInfo *)p_secondary_cmd_buffers[i].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
cmd_buf_info->cmd_list->ExecuteBundle(secondary_cb_info->cmd_list.Get());
}
}
/********************/
/**** SWAP CHAIN ****/
/********************/
void RenderingDeviceDriverD3D12::_swap_chain_release(SwapChain *p_swap_chain) {
_swap_chain_release_buffers(p_swap_chain);
p_swap_chain->d3d_swap_chain.Reset();
}
void RenderingDeviceDriverD3D12::_swap_chain_release_buffers(SwapChain *p_swap_chain) {
for (ID3D12Resource *render_target : p_swap_chain->render_targets) {
render_target->Release();
}
p_swap_chain->render_targets.clear();
p_swap_chain->render_targets_info.clear();
for (RDD::FramebufferID framebuffer : p_swap_chain->framebuffers) {
framebuffer_free(framebuffer);
}
p_swap_chain->framebuffers.clear();
}
RDD::SwapChainID RenderingDeviceDriverD3D12::swap_chain_create(RenderingContextDriver::SurfaceID p_surface) {
// Create the render pass that will be used to draw to the swap chain's framebuffers.
RDD::Attachment attachment;
attachment.format = DATA_FORMAT_R8G8B8A8_UNORM;
attachment.samples = RDD::TEXTURE_SAMPLES_1;
attachment.load_op = RDD::ATTACHMENT_LOAD_OP_CLEAR;
attachment.store_op = RDD::ATTACHMENT_STORE_OP_STORE;
RDD::Subpass subpass;
RDD::AttachmentReference color_ref;
color_ref.attachment = 0;
color_ref.aspect.set_flag(RDD::TEXTURE_ASPECT_COLOR_BIT);
subpass.color_references.push_back(color_ref);
RenderPassID render_pass = render_pass_create(attachment, subpass, {}, 1);
ERR_FAIL_COND_V(!render_pass, SwapChainID());
// Create the empty swap chain until it is resized.
SwapChain *swap_chain = memnew(SwapChain);
swap_chain->surface = p_surface;
swap_chain->data_format = attachment.format;
swap_chain->render_pass = render_pass;
return SwapChainID(swap_chain);
}
Error RenderingDeviceDriverD3D12::swap_chain_resize(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, uint32_t p_desired_framebuffer_count) {
DEV_ASSERT(p_cmd_queue.id != 0);
DEV_ASSERT(p_swap_chain.id != 0);
CommandQueueInfo *command_queue = (CommandQueueInfo *)(p_cmd_queue.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
SwapChain *swap_chain = (SwapChain *)(p_swap_chain.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
RenderingContextDriverD3D12::Surface *surface = (RenderingContextDriverD3D12::Surface *)(swap_chain->surface);
if (surface->width == 0 || surface->height == 0) {
// Very likely the window is minimized, don't create a swap chain.
return ERR_SKIP;
}
HRESULT res;
const bool is_tearing_supported = context_driver->get_tearing_supported();
UINT sync_interval = 0;
UINT present_flags = 0;
UINT creation_flags = 0;
switch (surface->vsync_mode) {
case DisplayServer::VSYNC_MAILBOX: {
sync_interval = 1;
present_flags = DXGI_PRESENT_RESTART;
} break;
case DisplayServer::VSYNC_ENABLED: {
sync_interval = 1;
present_flags = 0;
} break;
case DisplayServer::VSYNC_DISABLED: {
sync_interval = 0;
present_flags = is_tearing_supported ? DXGI_PRESENT_ALLOW_TEARING : 0;
creation_flags = is_tearing_supported ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0;
} break;
case DisplayServer::VSYNC_ADAPTIVE: // Unsupported.
default:
sync_interval = 1;
present_flags = 0;
break;
}
if (swap_chain->d3d_swap_chain != nullptr && creation_flags != swap_chain->creation_flags) {
// The swap chain must be recreated if the creation flags are different.
_swap_chain_release(swap_chain);
}
DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {};
if (swap_chain->d3d_swap_chain != nullptr) {
_swap_chain_release_buffers(swap_chain);
res = swap_chain->d3d_swap_chain->ResizeBuffers(p_desired_framebuffer_count, surface->width, surface->height, DXGI_FORMAT_UNKNOWN, creation_flags);
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_UNAVAILABLE);
} else {
swap_chain_desc.BufferCount = p_desired_framebuffer_count;
swap_chain_desc.Format = RD_TO_D3D12_FORMAT[swap_chain->data_format].general_format;
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
swap_chain_desc.SampleDesc.Count = 1;
swap_chain_desc.Flags = creation_flags;
swap_chain_desc.Scaling = DXGI_SCALING_STRETCH;
if (OS::get_singleton()->is_layered_allowed()) {
swap_chain_desc.AlphaMode = DXGI_ALPHA_MODE_PREMULTIPLIED;
has_comp_alpha[(uint64_t)p_cmd_queue.id] = true;
} else {
swap_chain_desc.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
has_comp_alpha[(uint64_t)p_cmd_queue.id] = false;
}
swap_chain_desc.Width = surface->width;
swap_chain_desc.Height = surface->height;
ComPtr<IDXGISwapChain1> swap_chain_1;
res = context_driver->dxgi_factory_get()->CreateSwapChainForComposition(command_queue->d3d_queue.Get(), &swap_chain_desc, nullptr, swap_chain_1.GetAddressOf());
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
swap_chain_1.As(&swap_chain->d3d_swap_chain);
ERR_FAIL_NULL_V(swap_chain->d3d_swap_chain, ERR_CANT_CREATE);
res = context_driver->dxgi_factory_get()->MakeWindowAssociation(surface->hwnd, DXGI_MWA_NO_ALT_ENTER | DXGI_MWA_NO_WINDOW_CHANGES);
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
}
if (surface->composition_device.Get() == nullptr) {
using PFN_DCompositionCreateDevice = HRESULT(WINAPI *)(IDXGIDevice *, REFIID, void **);
PFN_DCompositionCreateDevice pfn_DCompositionCreateDevice = (PFN_DCompositionCreateDevice)(void *)GetProcAddress(context_driver->lib_dcomp, "DCompositionCreateDevice");
ERR_FAIL_NULL_V(pfn_DCompositionCreateDevice, ERR_CANT_CREATE);
res = pfn_DCompositionCreateDevice(nullptr, IID_PPV_ARGS(surface->composition_device.GetAddressOf()));
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
res = surface->composition_device->CreateTargetForHwnd(surface->hwnd, TRUE, surface->composition_target.GetAddressOf());
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
res = surface->composition_device->CreateVisual(surface->composition_visual.GetAddressOf());
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
res = surface->composition_visual->SetContent(swap_chain->d3d_swap_chain.Get());
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
res = surface->composition_target->SetRoot(surface->composition_visual.Get());
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
res = surface->composition_device->Commit();
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
} else {
res = surface->composition_visual->SetContent(swap_chain->d3d_swap_chain.Get());
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
res = surface->composition_device->Commit();
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
}
res = swap_chain->d3d_swap_chain->GetDesc1(&swap_chain_desc);
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
ERR_FAIL_COND_V(swap_chain_desc.BufferCount == 0, ERR_CANT_CREATE);
surface->width = swap_chain_desc.Width;
surface->height = swap_chain_desc.Height;
swap_chain->creation_flags = creation_flags;
swap_chain->sync_interval = sync_interval;
swap_chain->present_flags = present_flags;
// Retrieve the render targets associated to the swap chain and recreate the framebuffers. The following code
// relies on the address of the elements remaining static when new elements are inserted, so the container must
// follow this restriction when reserving the right amount of elements beforehand.
swap_chain->render_targets.reserve(swap_chain_desc.BufferCount);
swap_chain->render_targets_info.reserve(swap_chain_desc.BufferCount);
swap_chain->framebuffers.reserve(swap_chain_desc.BufferCount);
for (uint32_t i = 0; i < swap_chain_desc.BufferCount; i++) {
// Retrieve the resource corresponding to the swap chain's buffer.
ID3D12Resource *render_target = nullptr;
res = swap_chain->d3d_swap_chain->GetBuffer(i, IID_PPV_ARGS(&render_target));
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
swap_chain->render_targets.push_back(render_target);
// Create texture information for the framebuffer to reference the resource. Since the states pointer must
// reference an address of the element itself, we must insert it first and then modify it.
swap_chain->render_targets_info.push_back(TextureInfo());
TextureInfo &texture_info = swap_chain->render_targets_info[i];
texture_info.owner_info.states.subresource_states.push_back(D3D12_RESOURCE_STATE_PRESENT);
texture_info.states_ptr = &texture_info.owner_info.states;
texture_info.format = swap_chain->data_format;
#if defined(_MSC_VER) || !defined(_WIN32)
texture_info.desc = CD3DX12_RESOURCE_DESC(render_target->GetDesc());
#else
render_target->GetDesc(&texture_info.desc);
#endif
texture_info.layers = 1;
texture_info.mipmaps = 1;
texture_info.resource = render_target;
texture_info.view_descs.srv.Format = texture_info.desc.Format;
texture_info.view_descs.srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
// Create the framebuffer for this buffer.
FramebufferID framebuffer = _framebuffer_create(swap_chain->render_pass, TextureID(&swap_chain->render_targets_info[i]), swap_chain_desc.Width, swap_chain_desc.Height, true);
ERR_FAIL_COND_V(!framebuffer, ERR_CANT_CREATE);
swap_chain->framebuffers.push_back(framebuffer);
}
// Once everything's been created correctly, indicate the surface no longer needs to be resized.
context_driver->surface_set_needs_resize(swap_chain->surface, false);
return OK;
}
RDD::FramebufferID RenderingDeviceDriverD3D12::swap_chain_acquire_framebuffer(CommandQueueID p_cmd_queue, SwapChainID p_swap_chain, bool &r_resize_required) {
DEV_ASSERT(p_swap_chain.id != 0);
const SwapChain *swap_chain = (const SwapChain *)(p_swap_chain.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (context_driver->surface_get_needs_resize(swap_chain->surface)) {
r_resize_required = true;
return FramebufferID();
}
const uint32_t buffer_index = swap_chain->d3d_swap_chain->GetCurrentBackBufferIndex();
DEV_ASSERT(buffer_index < swap_chain->framebuffers.size());
return swap_chain->framebuffers[buffer_index];
}
RDD::RenderPassID RenderingDeviceDriverD3D12::swap_chain_get_render_pass(SwapChainID p_swap_chain) {
const SwapChain *swap_chain = (const SwapChain *)(p_swap_chain.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
return swap_chain->render_pass;
}
RDD::DataFormat RenderingDeviceDriverD3D12::swap_chain_get_format(SwapChainID p_swap_chain) {
const SwapChain *swap_chain = (const SwapChain *)(p_swap_chain.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
return swap_chain->data_format;
}
void RenderingDeviceDriverD3D12::swap_chain_free(SwapChainID p_swap_chain) {
SwapChain *swap_chain = (SwapChain *)(p_swap_chain.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_swap_chain_release(swap_chain);
render_pass_free(swap_chain->render_pass);
memdelete(swap_chain);
}
/*********************/
/**** FRAMEBUFFER ****/
/*********************/
D3D12_RENDER_TARGET_VIEW_DESC RenderingDeviceDriverD3D12::_make_rtv_for_texture(const TextureInfo *p_texture_info, uint32_t p_mipmap_offset, uint32_t p_layer_offset, uint32_t p_layers, bool p_add_bases) {
D3D12_RENDER_TARGET_VIEW_DESC rtv_desc = {};
rtv_desc.Format = p_texture_info->view_descs.srv.Format;
switch (p_texture_info->view_descs.srv.ViewDimension) {
case D3D12_SRV_DIMENSION_TEXTURE1D: {
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE1D;
rtv_desc.Texture1D.MipSlice = p_texture_info->base_mip + p_mipmap_offset;
} break;
case D3D12_SRV_DIMENSION_TEXTURE1DARRAY: {
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE1DARRAY;
rtv_desc.Texture1DArray.MipSlice = (p_add_bases ? p_texture_info->base_mip : 0) + p_mipmap_offset;
rtv_desc.Texture1DArray.FirstArraySlice = (p_add_bases ? p_texture_info->base_layer : 0) + p_layer_offset;
rtv_desc.Texture1DArray.ArraySize = p_layers == UINT32_MAX ? p_texture_info->view_descs.srv.Texture1DArray.ArraySize : p_layers;
} break;
case D3D12_SRV_DIMENSION_TEXTURE2D: {
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
rtv_desc.Texture2D.MipSlice = (p_add_bases ? p_texture_info->base_mip : 0) + p_mipmap_offset;
rtv_desc.Texture2D.PlaneSlice = p_texture_info->view_descs.srv.Texture2D.PlaneSlice;
} break;
case D3D12_SRV_DIMENSION_TEXTURE2DARRAY: {
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DARRAY;
rtv_desc.Texture2DArray.MipSlice = (p_add_bases ? p_texture_info->base_mip : 0) + p_mipmap_offset;
rtv_desc.Texture2DArray.FirstArraySlice = (p_add_bases ? p_texture_info->base_layer : 0) + p_layer_offset;
rtv_desc.Texture2DArray.ArraySize = p_layers == UINT32_MAX ? p_texture_info->view_descs.srv.Texture2DArray.ArraySize : p_layers;
rtv_desc.Texture2DArray.PlaneSlice = p_texture_info->view_descs.srv.Texture2DArray.PlaneSlice;
} break;
case D3D12_SRV_DIMENSION_TEXTURE2DMS: {
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DMS;
} break;
case D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY: {
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY;
rtv_desc.Texture2DMSArray.FirstArraySlice = (p_add_bases ? p_texture_info->base_layer : 0) + p_layer_offset;
rtv_desc.Texture2DMSArray.ArraySize = p_layers == UINT32_MAX ? p_texture_info->view_descs.srv.Texture2DMSArray.ArraySize : p_layers;
} break;
case D3D12_SRV_DIMENSION_TEXTURE3D: {
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE3D;
rtv_desc.Texture3D.MipSlice = p_texture_info->view_descs.srv.Texture3D.MostDetailedMip + p_mipmap_offset;
rtv_desc.Texture3D.FirstWSlice = 0;
rtv_desc.Texture3D.WSize = -1;
} break;
case D3D12_SRV_DIMENSION_TEXTURECUBE:
case D3D12_SRV_DIMENSION_TEXTURECUBEARRAY: {
rtv_desc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DARRAY;
rtv_desc.Texture2DArray.MipSlice = (p_add_bases ? p_texture_info->base_mip : 0) + p_mipmap_offset;
rtv_desc.Texture2DArray.FirstArraySlice = (p_add_bases ? p_texture_info->base_layer : 0) + p_layer_offset;
rtv_desc.Texture2DArray.ArraySize = p_layers == UINT32_MAX ? p_texture_info->layers : p_layers;
rtv_desc.Texture2DArray.PlaneSlice = 0;
} break;
default: {
DEV_ASSERT(false);
}
}
return rtv_desc;
}
D3D12_UNORDERED_ACCESS_VIEW_DESC RenderingDeviceDriverD3D12::_make_ranged_uav_for_texture(const TextureInfo *p_texture_info, uint32_t p_mipmap_offset, uint32_t p_layer_offset, uint32_t p_layers, bool p_add_bases) {
D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = p_texture_info->view_descs.uav;
uint32_t mip = (p_add_bases ? p_texture_info->base_mip : 0) + p_mipmap_offset;
switch (p_texture_info->view_descs.uav.ViewDimension) {
case D3D12_UAV_DIMENSION_TEXTURE1D: {
uav_desc.Texture1DArray.MipSlice = mip;
} break;
case D3D12_UAV_DIMENSION_TEXTURE1DARRAY: {
uav_desc.Texture1DArray.MipSlice = mip;
uav_desc.Texture1DArray.FirstArraySlice = mip;
uav_desc.Texture1DArray.ArraySize = p_layers;
} break;
case D3D12_UAV_DIMENSION_TEXTURE2D: {
uav_desc.Texture2D.MipSlice = mip;
} break;
case D3D12_UAV_DIMENSION_TEXTURE2DARRAY: {
uav_desc.Texture2DArray.MipSlice = mip;
uav_desc.Texture2DArray.FirstArraySlice = (p_add_bases ? p_texture_info->base_layer : 0) + p_layer_offset;
uav_desc.Texture2DArray.ArraySize = p_layers;
} break;
case D3D12_UAV_DIMENSION_TEXTURE3D: {
uav_desc.Texture3D.MipSlice = mip;
uav_desc.Texture3D.WSize >>= p_mipmap_offset;
} break;
default:
break;
}
return uav_desc;
}
D3D12_DEPTH_STENCIL_VIEW_DESC RenderingDeviceDriverD3D12::_make_dsv_for_texture(const TextureInfo *p_texture_info) {
D3D12_DEPTH_STENCIL_VIEW_DESC dsv_desc = {};
dsv_desc.Format = RD_TO_D3D12_FORMAT[p_texture_info->format].dsv_format;
dsv_desc.Flags = D3D12_DSV_FLAG_NONE;
switch (p_texture_info->view_descs.srv.ViewDimension) {
case D3D12_SRV_DIMENSION_TEXTURE1D: {
dsv_desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE1D;
dsv_desc.Texture1D.MipSlice = p_texture_info->base_mip;
} break;
case D3D12_SRV_DIMENSION_TEXTURE1DARRAY: {
dsv_desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE1DARRAY;
dsv_desc.Texture1DArray.MipSlice = p_texture_info->base_mip;
dsv_desc.Texture1DArray.FirstArraySlice = p_texture_info->base_layer;
dsv_desc.Texture1DArray.ArraySize = p_texture_info->view_descs.srv.Texture1DArray.ArraySize;
} break;
case D3D12_SRV_DIMENSION_TEXTURE2D: {
dsv_desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
dsv_desc.Texture2D.MipSlice = p_texture_info->view_descs.srv.Texture2D.MostDetailedMip;
} break;
case D3D12_SRV_DIMENSION_TEXTURE2DARRAY: {
dsv_desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DARRAY;
dsv_desc.Texture2DArray.MipSlice = p_texture_info->base_mip;
dsv_desc.Texture2DArray.FirstArraySlice = p_texture_info->base_layer;
dsv_desc.Texture2DArray.ArraySize = p_texture_info->view_descs.srv.Texture2DArray.ArraySize;
} break;
case D3D12_SRV_DIMENSION_TEXTURE2DMS: {
dsv_desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DMS;
dsv_desc.Texture2DMS.UnusedField_NothingToDefine = p_texture_info->view_descs.srv.Texture2DMS.UnusedField_NothingToDefine;
} break;
case D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY: {
dsv_desc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DMSARRAY;
dsv_desc.Texture2DMSArray.FirstArraySlice = p_texture_info->base_layer;
dsv_desc.Texture2DMSArray.ArraySize = p_texture_info->view_descs.srv.Texture2DMSArray.ArraySize;
} break;
default: {
DEV_ASSERT(false);
}
}
return dsv_desc;
}
RDD::FramebufferID RenderingDeviceDriverD3D12::_framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height, bool p_is_screen) {
// Pre-bookkeep.
FramebufferInfo *fb_info = VersatileResource::allocate<FramebufferInfo>(resources_allocator);
fb_info->is_screen = p_is_screen;
const RenderPassInfo *pass_info = (const RenderPassInfo *)p_render_pass.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
uint32_t num_color = 0;
uint32_t num_depth_stencil = 0;
for (uint32_t i = 0; i < p_attachments.size(); i++) {
const TextureInfo *tex_info = (const TextureInfo *)p_attachments[i].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if ((tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) {
num_color++;
} else if ((tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) {
num_depth_stencil++;
}
}
uint32_t vrs_index = UINT32_MAX;
for (const Subpass &E : pass_info->subpasses) {
if (E.vrs_reference.attachment != AttachmentReference::UNUSED) {
vrs_index = E.vrs_reference.attachment;
}
}
if (num_color) {
Error err = fb_info->rtv_heap.allocate(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_RTV, num_color, false);
if (err) {
VersatileResource::free(resources_allocator, fb_info);
ERR_FAIL_V(FramebufferID());
}
}
DescriptorsHeap::Walker rtv_heap_walker = fb_info->rtv_heap.make_walker();
if (num_depth_stencil) {
Error err = fb_info->dsv_heap.allocate(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_DSV, num_depth_stencil, false);
if (err) {
VersatileResource::free(resources_allocator, fb_info);
ERR_FAIL_V(FramebufferID());
}
}
DescriptorsHeap::Walker dsv_heap_walker = fb_info->dsv_heap.make_walker();
fb_info->attachments_handle_inds.resize(p_attachments.size());
fb_info->attachments.reserve(num_color + num_depth_stencil);
uint32_t color_idx = 0;
uint32_t depth_stencil_idx = 0;
for (uint32_t i = 0; i < p_attachments.size(); i++) {
const TextureInfo *tex_info = (const TextureInfo *)p_attachments[i].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (fb_info->size.x == 0) {
fb_info->size = Size2i(tex_info->desc.Width, tex_info->desc.Height);
}
if ((tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) {
D3D12_RENDER_TARGET_VIEW_DESC rtv_desc = _make_rtv_for_texture(tex_info, 0, 0, UINT32_MAX);
device->CreateRenderTargetView(tex_info->resource, &rtv_desc, rtv_heap_walker.get_curr_cpu_handle());
rtv_heap_walker.advance();
fb_info->attachments_handle_inds[i] = color_idx;
fb_info->attachments.push_back(p_attachments[i]);
color_idx++;
} else if ((tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) {
D3D12_DEPTH_STENCIL_VIEW_DESC dsv_desc = _make_dsv_for_texture(tex_info);
device->CreateDepthStencilView(tex_info->resource, &dsv_desc, dsv_heap_walker.get_curr_cpu_handle());
dsv_heap_walker.advance();
fb_info->attachments_handle_inds[i] = depth_stencil_idx;
fb_info->attachments.push_back(p_attachments[i]);
depth_stencil_idx++;
} else if (i == vrs_index) {
fb_info->vrs_attachment = p_attachments[i];
} else {
DEV_ASSERT(false);
}
}
DEV_ASSERT(fb_info->attachments.size() == color_idx + depth_stencil_idx);
DEV_ASSERT((fb_info->vrs_attachment.id != 0) == (vrs_index != UINT32_MAX));
DEV_ASSERT(rtv_heap_walker.is_at_eof());
DEV_ASSERT(dsv_heap_walker.is_at_eof());
return FramebufferID(fb_info);
}
RDD::FramebufferID RenderingDeviceDriverD3D12::framebuffer_create(RenderPassID p_render_pass, VectorView<TextureID> p_attachments, uint32_t p_width, uint32_t p_height) {
return _framebuffer_create(p_render_pass, p_attachments, p_width, p_height, false);
}
void RenderingDeviceDriverD3D12::framebuffer_free(FramebufferID p_framebuffer) {
FramebufferInfo *fb_info = (FramebufferInfo *)p_framebuffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
VersatileResource::free(resources_allocator, fb_info);
}
/****************/
/**** SHADER ****/
/****************/
static uint32_t SHADER_STAGES_BIT_OFFSET_INDICES[RenderingDevice::SHADER_STAGE_MAX] = {
/* SHADER_STAGE_VERTEX */ 0,
/* SHADER_STAGE_FRAGMENT */ 1,
/* SHADER_STAGE_TESSELATION_CONTROL */ UINT32_MAX,
/* SHADER_STAGE_TESSELATION_EVALUATION */ UINT32_MAX,
/* SHADER_STAGE_COMPUTE */ 2,
};
uint32_t RenderingDeviceDriverD3D12::_shader_patch_dxil_specialization_constant(
PipelineSpecializationConstantType p_type,
const void *p_value,
const uint64_t (&p_stages_bit_offsets)[D3D12_BITCODE_OFFSETS_NUM_STAGES],
HashMap<ShaderStage, Vector<uint8_t>> &r_stages_bytecodes,
bool p_is_first_patch) {
uint32_t patch_val = 0;
switch (p_type) {
case PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT: {
uint32_t int_value = *((const int *)p_value);
ERR_FAIL_COND_V(int_value & (1 << 31), 0);<--- Integer overflow
patch_val = int_value;
} break;
case PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL: {
bool bool_value = *((const bool *)p_value);
patch_val = (uint32_t)bool_value;
} break;
case PIPELINE_SPECIALIZATION_CONSTANT_TYPE_FLOAT: {
uint32_t int_value = *((const int *)p_value);
ERR_FAIL_COND_V(int_value & (1 << 31), 0);<--- Integer overflow
patch_val = (int_value >> 1);
} break;
}
// For VBR encoding to encode the number of bits we expect (32), we need to set the MSB unconditionally.
// However, signed VBR moves the MSB to the LSB, so setting the MSB to 1 wouldn't help. Therefore,
// the bit we set to 1 is the one at index 30.
patch_val |= (1 << 30);
patch_val <<= 1; // What signed VBR does.
auto tamper_bits = [](uint8_t *p_start, uint64_t p_bit_offset, uint64_t p_tb_value) -> uint64_t {
uint64_t original = 0;
uint32_t curr_input_byte = p_bit_offset / 8;
uint8_t curr_input_bit = p_bit_offset % 8;
auto get_curr_input_bit = [&]() -> bool {
return ((p_start[curr_input_byte] >> curr_input_bit) & 1);
};
auto move_to_next_input_bit = [&]() {
if (curr_input_bit == 7) {
curr_input_bit = 0;
curr_input_byte++;
} else {
curr_input_bit++;
}
};
auto tamper_input_bit = [&](bool p_new_bit) {
p_start[curr_input_byte] &= ~((uint8_t)1 << curr_input_bit);
if (p_new_bit) {
p_start[curr_input_byte] |= (uint8_t)1 << curr_input_bit;
}
};
uint8_t value_bit_idx = 0;
for (uint32_t i = 0; i < 5; i++) { // 32 bits take 5 full bytes in VBR.
for (uint32_t j = 0; j < 7; j++) {
bool input_bit = get_curr_input_bit();
original |= (uint64_t)(input_bit ? 1 : 0) << value_bit_idx;
tamper_input_bit((p_tb_value >> value_bit_idx) & 1);
move_to_next_input_bit();
value_bit_idx++;
}
#ifdef DEV_ENABLED
bool input_bit = get_curr_input_bit();
DEV_ASSERT((i < 4 && input_bit) || (i == 4 && !input_bit));
#endif
move_to_next_input_bit();
}
return original;
};
uint32_t stages_patched_mask = 0;
for (int stage = 0; stage < SHADER_STAGE_MAX; stage++) {
if (!r_stages_bytecodes.has((ShaderStage)stage)) {
continue;
}
uint64_t offset = p_stages_bit_offsets[SHADER_STAGES_BIT_OFFSET_INDICES[stage]];
if (offset == 0) {
// This constant does not appear at this stage.
continue;
}
Vector<uint8_t> &bytecode = r_stages_bytecodes[(ShaderStage)stage];
#ifdef DEV_ENABLED
uint64_t orig_patch_val = tamper_bits(bytecode.ptrw(), offset, patch_val);
// Checking against the value the NIR patch should have set.
DEV_ASSERT(!p_is_first_patch || ((orig_patch_val >> 1) & GODOT_NIR_SC_SENTINEL_MAGIC_MASK) == GODOT_NIR_SC_SENTINEL_MAGIC);
uint64_t readback_patch_val = tamper_bits(bytecode.ptrw(), offset, patch_val);
DEV_ASSERT(readback_patch_val == patch_val);
#else
tamper_bits(bytecode.ptrw(), offset, patch_val);
#endif
stages_patched_mask |= (1 << stage);
}
return stages_patched_mask;
}
bool RenderingDeviceDriverD3D12::_shader_apply_specialization_constants(
const ShaderInfo *p_shader_info,
VectorView<PipelineSpecializationConstant> p_specialization_constants,
HashMap<ShaderStage, Vector<uint8_t>> &r_final_stages_bytecode) {
// If something needs to be patched, COW will do the trick.
r_final_stages_bytecode = p_shader_info->stages_bytecode;
uint32_t stages_re_sign_mask = 0;
for (uint32_t i = 0; i < p_specialization_constants.size(); i++) {
const PipelineSpecializationConstant &psc = p_specialization_constants[i];
if (!(p_shader_info->spirv_specialization_constants_ids_mask & (1 << psc.constant_id))) {
// This SC wasn't even in the original SPIR-V shader.
continue;
}
for (const ShaderInfo::SpecializationConstant &sc : p_shader_info->specialization_constants) {
if (psc.constant_id == sc.constant_id) {
if (psc.int_value != sc.int_value) {
stages_re_sign_mask |= _shader_patch_dxil_specialization_constant(psc.type, &psc.int_value, sc.stages_bit_offsets, r_final_stages_bytecode, false);
}
break;
}
}
}
// Re-sign patched stages.
for (KeyValue<ShaderStage, Vector<uint8_t>> &E : r_final_stages_bytecode) {
ShaderStage stage = E.key;
if ((stages_re_sign_mask & (1 << stage))) {
Vector<uint8_t> &bytecode = E.value;
_shader_sign_dxil_bytecode(stage, bytecode);
}
}
return true;
}
void RenderingDeviceDriverD3D12::_shader_sign_dxil_bytecode(ShaderStage p_stage, Vector<uint8_t> &r_dxil_blob) {
uint8_t *w = r_dxil_blob.ptrw();
compute_dxil_hash(w + 20, r_dxil_blob.size() - 20, w + 4);
}
String RenderingDeviceDriverD3D12::shader_get_binary_cache_key() {
return "D3D12-SV" + uitos(ShaderBinary::VERSION) + "-" + itos(shader_capabilities.shader_model);
}
Vector<uint8_t> RenderingDeviceDriverD3D12::shader_compile_binary_from_spirv(VectorView<ShaderStageSPIRVData> p_spirv, const String &p_shader_name) {
ShaderReflection shader_refl;
if (_reflect_spirv(p_spirv, shader_refl) != OK) {
return Vector<uint8_t>();
}
// Collect reflection data into binary data.
ShaderBinary::Data binary_data;
Vector<Vector<ShaderBinary::DataBinding>> sets_bindings;
Vector<ShaderBinary::SpecializationConstant> specialization_constants;
{
binary_data.vertex_input_mask = shader_refl.vertex_input_mask;
binary_data.fragment_output_mask = shader_refl.fragment_output_mask;
binary_data.specialization_constants_count = shader_refl.specialization_constants.size();
binary_data.is_compute = shader_refl.is_compute;
binary_data.compute_local_size[0] = shader_refl.compute_local_size[0];
binary_data.compute_local_size[1] = shader_refl.compute_local_size[1];
binary_data.compute_local_size[2] = shader_refl.compute_local_size[2];
binary_data.set_count = shader_refl.uniform_sets.size();
binary_data.push_constant_size = shader_refl.push_constant_size;
binary_data.nir_runtime_data_root_param_idx = UINT32_MAX;
binary_data.stage_count = p_spirv.size();
for (const Vector<ShaderUniform> &spirv_set : shader_refl.uniform_sets) {
Vector<ShaderBinary::DataBinding> bindings;
for (const ShaderUniform &spirv_uniform : spirv_set) {
ShaderBinary::DataBinding binding;
binding.type = (uint32_t)spirv_uniform.type;
binding.binding = spirv_uniform.binding;
binding.stages = (uint32_t)spirv_uniform.stages;
binding.length = spirv_uniform.length;
binding.writable = (uint32_t)spirv_uniform.writable;
bindings.push_back(binding);
}
sets_bindings.push_back(bindings);
}
for (const ShaderSpecializationConstant &spirv_sc : shader_refl.specialization_constants) {
ShaderBinary::SpecializationConstant spec_constant;
spec_constant.type = (uint32_t)spirv_sc.type;
spec_constant.constant_id = spirv_sc.constant_id;
spec_constant.int_value = spirv_sc.int_value;
spec_constant.stage_flags = spirv_sc.stages;
specialization_constants.push_back(spec_constant);
binary_data.spirv_specialization_constants_ids_mask |= (1 << spirv_sc.constant_id);
}
}
// Translate SPIR-V shaders to DXIL, and collect shader info from the new representation.
HashMap<ShaderStage, Vector<uint8_t>> dxil_blobs;
BitField<ShaderStage> stages_processed;
{
HashMap<int, nir_shader *> stages_nir_shaders;
auto free_nir_shaders = [&]() {
for (KeyValue<int, nir_shader *> &E : stages_nir_shaders) {
ralloc_free(E.value);
}
stages_nir_shaders.clear();
};
// This is based on spirv2dxil.c. May need updates when it changes.
// Also, this has to stay around until after linking.
nir_shader_compiler_options nir_options = *dxil_get_nir_compiler_options();
nir_options.lower_base_vertex = false;
dxil_spirv_runtime_conf dxil_runtime_conf = {};
dxil_runtime_conf.runtime_data_cbv.base_shader_register = RUNTIME_DATA_REGISTER;
dxil_runtime_conf.push_constant_cbv.base_shader_register = ROOT_CONSTANT_REGISTER;
dxil_runtime_conf.zero_based_vertex_instance_id = true;
dxil_runtime_conf.zero_based_compute_workgroup_id = true;
dxil_runtime_conf.declared_read_only_images_as_srvs = true;
// Making this explicit to let maintainers know that in practice this didn't improve performance,
// probably because data generated by one shader and consumed by another one forces the resource
// to transition from UAV to SRV, and back, instead of being an UAV all the time.
// In case someone wants to try, care must be taken so in case of incompatible bindings across stages
// happen as a result, all the stages are re-translated. That can happen if, for instance, a stage only
// uses an allegedly writable resource only for reading but the next stage doesn't.
dxil_runtime_conf.inferred_read_only_images_as_srvs = false;
// - Translate SPIR-V to NIR.
for (uint32_t i = 0; i < p_spirv.size(); i++) {
ShaderStage stage = (ShaderStage)p_spirv[i].shader_stage;
ShaderStage stage_flag = (ShaderStage)(1 << p_spirv[i].shader_stage);
stages_processed.set_flag(stage_flag);
{
const char *entry_point = "main";
static const gl_shader_stage SPIRV_TO_MESA_STAGES[SHADER_STAGE_MAX] = {
/* SHADER_STAGE_VERTEX */ MESA_SHADER_VERTEX,
/* SHADER_STAGE_FRAGMENT */ MESA_SHADER_FRAGMENT,
/* SHADER_STAGE_TESSELATION_CONTROL */ MESA_SHADER_TESS_CTRL,
/* SHADER_STAGE_TESSELATION_EVALUATION */ MESA_SHADER_TESS_EVAL,
/* SHADER_STAGE_COMPUTE */ MESA_SHADER_COMPUTE,
};
nir_shader *shader = spirv_to_nir(
(const uint32_t *)p_spirv[i].spirv.ptr(),
p_spirv[i].spirv.size() / sizeof(uint32_t),
nullptr,
0,
SPIRV_TO_MESA_STAGES[stage],
entry_point,
dxil_spirv_nir_get_spirv_options(), &nir_options);
if (!shader) {
free_nir_shaders();
ERR_FAIL_V_MSG(Vector<uint8_t>(), "Shader translation (step 1) at stage " + String(SHADER_STAGE_NAMES[stage]) + " failed.");
}
#ifdef DEV_ENABLED
nir_validate_shader(shader, "Validate before feeding NIR to the DXIL compiler");
#endif
if (stage == SHADER_STAGE_VERTEX) {
dxil_runtime_conf.yz_flip.y_mask = 0xffff;
dxil_runtime_conf.yz_flip.mode = DXIL_SPIRV_Y_FLIP_UNCONDITIONAL;
} else {
dxil_runtime_conf.yz_flip.y_mask = 0;
dxil_runtime_conf.yz_flip.mode = DXIL_SPIRV_YZ_FLIP_NONE;
}
// This is based on spirv2dxil.c. May need updates when it changes.
dxil_spirv_nir_prep(shader);
bool requires_runtime_data = {};
dxil_spirv_nir_passes(shader, &dxil_runtime_conf, &requires_runtime_data);
stages_nir_shaders[stage] = shader;
}
}
// - Link NIR shaders.
for (int i = SHADER_STAGE_MAX - 1; i >= 0; i--) {
if (!stages_nir_shaders.has(i)) {
continue;
}
nir_shader *shader = stages_nir_shaders[i];
nir_shader *prev_shader = nullptr;
for (int j = i - 1; j >= 0; j--) {
if (stages_nir_shaders.has(j)) {
prev_shader = stages_nir_shaders[j];
break;
}
}
if (prev_shader) {
bool requires_runtime_data = {};
dxil_spirv_nir_link(shader, prev_shader, &dxil_runtime_conf, &requires_runtime_data);
}
}
// - Translate NIR to DXIL.
for (uint32_t i = 0; i < p_spirv.size(); i++) {
ShaderStage stage = (ShaderStage)p_spirv[i].shader_stage;
struct ShaderData {
ShaderStage stage;
ShaderBinary::Data &binary_data;
Vector<Vector<ShaderBinary::DataBinding>> &sets_bindings;
Vector<ShaderBinary::SpecializationConstant> &specialization_constants;
} shader_data{ stage, binary_data, sets_bindings, specialization_constants };
GodotNirCallbacks godot_nir_callbacks = {};
godot_nir_callbacks.data = &shader_data;
godot_nir_callbacks.report_resource = [](uint32_t p_register, uint32_t p_space, uint32_t p_dxil_type, void *p_data) {
ShaderData &shader_data_in = *(ShaderData *)p_data;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
// Types based on Mesa's dxil_container.h.
static const uint32_t DXIL_RES_SAMPLER = 1;
static const ResourceClass DXIL_TYPE_TO_CLASS[] = {
/* DXIL_RES_INVALID */ RES_CLASS_INVALID,
/* DXIL_RES_SAMPLER */ RES_CLASS_INVALID, // Handling sampler as a flag.
/* DXIL_RES_CBV */ RES_CLASS_CBV,
/* DXIL_RES_SRV_TYPED */ RES_CLASS_SRV,
/* DXIL_RES_SRV_RAW */ RES_CLASS_SRV,
/* DXIL_RES_SRV_STRUCTURED */ RES_CLASS_SRV,
/* DXIL_RES_UAV_TYPED */ RES_CLASS_UAV,
/* DXIL_RES_UAV_RAW */ RES_CLASS_UAV,
/* DXIL_RES_UAV_STRUCTURED */ RES_CLASS_UAV,
/* DXIL_RES_UAV_STRUCTURED_WITH_COUNTER */ RES_CLASS_INVALID,
};
DEV_ASSERT(p_dxil_type < ARRAY_SIZE(DXIL_TYPE_TO_CLASS));
ResourceClass res_class = DXIL_TYPE_TO_CLASS[p_dxil_type];
if (p_register == ROOT_CONSTANT_REGISTER && p_space == 0) {
DEV_ASSERT(res_class == RES_CLASS_CBV);
shader_data_in.binary_data.dxil_push_constant_stages |= (1 << shader_data_in.stage);
} else if (p_register == RUNTIME_DATA_REGISTER && p_space == 0) {
DEV_ASSERT(res_class == RES_CLASS_CBV);
shader_data_in.binary_data.nir_runtime_data_root_param_idx = 1; // Temporary, to be determined later.
} else {
DEV_ASSERT(p_space == 0);
uint32_t set = p_register / GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER;
uint32_t binding = (p_register % GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER) / GODOT_NIR_BINDING_MULTIPLIER;
DEV_ASSERT(set < (uint32_t)shader_data_in.sets_bindings.size());
[[maybe_unused]] bool found = false;
for (int j = 0; j < shader_data_in.sets_bindings[set].size(); j++) {
if (shader_data_in.sets_bindings[set][j].binding != binding) {
continue;
}
ShaderBinary::DataBinding &binding_info = shader_data_in.sets_bindings.write[set].write[j];
binding_info.dxil_stages |= (1 << shader_data_in.stage);
if (res_class != RES_CLASS_INVALID) {
DEV_ASSERT(binding_info.res_class == (uint32_t)RES_CLASS_INVALID || binding_info.res_class == (uint32_t)res_class);
binding_info.res_class = res_class;
} else if (p_dxil_type == DXIL_RES_SAMPLER) {
binding_info.has_sampler = (uint32_t)true;
} else {
CRASH_NOW();
}
found = true;
break;
}
DEV_ASSERT(found);
}
};
godot_nir_callbacks.report_sc_bit_offset_fn = [](uint32_t p_sc_id, uint64_t p_bit_offset, void *p_data) {
ShaderData &shader_data_in = *(ShaderData *)p_data;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
[[maybe_unused]] bool found = false;
for (int j = 0; j < shader_data_in.specialization_constants.size(); j++) {
if (shader_data_in.specialization_constants[j].constant_id != p_sc_id) {
continue;
}
uint32_t offset_idx = SHADER_STAGES_BIT_OFFSET_INDICES[shader_data_in.stage];
DEV_ASSERT(shader_data_in.specialization_constants.write[j].stages_bit_offsets[offset_idx] == 0);
shader_data_in.specialization_constants.write[j].stages_bit_offsets[offset_idx] = p_bit_offset;
found = true;
break;
}
DEV_ASSERT(found);
};
godot_nir_callbacks.report_bitcode_bit_offset_fn = [](uint64_t p_bit_offset, void *p_data) {
DEV_ASSERT(p_bit_offset % 8 == 0);
ShaderData &shader_data_in = *(ShaderData *)p_data;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
uint32_t offset_idx = SHADER_STAGES_BIT_OFFSET_INDICES[shader_data_in.stage];
for (int j = 0; j < shader_data_in.specialization_constants.size(); j++) {
if (shader_data_in.specialization_constants.write[j].stages_bit_offsets[offset_idx] == 0) {
// This SC has been optimized out from this stage.
continue;
}
shader_data_in.specialization_constants.write[j].stages_bit_offsets[offset_idx] += p_bit_offset;
}
};
auto shader_model_d3d_to_dxil = [](D3D_SHADER_MODEL p_d3d_shader_model) -> dxil_shader_model {
static_assert(SHADER_MODEL_6_0 == 0x60000);
static_assert(SHADER_MODEL_6_3 == 0x60003);
static_assert(D3D_SHADER_MODEL_6_0 == 0x60);
static_assert(D3D_SHADER_MODEL_6_3 == 0x63);
return (dxil_shader_model)((p_d3d_shader_model >> 4) * 0x10000 + (p_d3d_shader_model & 0xf));
};
nir_to_dxil_options nir_to_dxil_options = {};
nir_to_dxil_options.environment = DXIL_ENVIRONMENT_VULKAN;
nir_to_dxil_options.shader_model_max = shader_model_d3d_to_dxil(shader_capabilities.shader_model);
nir_to_dxil_options.validator_version_max = NO_DXIL_VALIDATION;
nir_to_dxil_options.godot_nir_callbacks = &godot_nir_callbacks;
dxil_logger logger = {};
logger.log = [](void *p_priv, const char *p_msg) {
#ifdef DEBUG_ENABLED
print_verbose(p_msg);
#endif
};
blob dxil_blob = {};
bool ok = nir_to_dxil(stages_nir_shaders[stage], &nir_to_dxil_options, &logger, &dxil_blob);
ralloc_free(stages_nir_shaders[stage]);
stages_nir_shaders.erase(stage);
if (!ok) {
free_nir_shaders();
ERR_FAIL_V_MSG(Vector<uint8_t>(), "Shader translation at stage " + String(SHADER_STAGE_NAMES[stage]) + " failed.");
}
Vector<uint8_t> blob_copy;
blob_copy.resize(dxil_blob.size);
memcpy(blob_copy.ptrw(), dxil_blob.data, dxil_blob.size);
blob_finish(&dxil_blob);
dxil_blobs.insert(stage, blob_copy);
}
}
#if 0
if (dxil_blobs.has(SHADER_STAGE_FRAGMENT)) {
Ref<FileAccess> f = FileAccess::open("res://1.dxil", FileAccess::WRITE);
f->store_buffer(dxil_blobs[SHADER_STAGE_FRAGMENT].ptr(), dxil_blobs[SHADER_STAGE_FRAGMENT].size());
}
#endif
// Patch with default values of specialization constants.
if (specialization_constants.size()) {
for (const ShaderBinary::SpecializationConstant &sc : specialization_constants) {
_shader_patch_dxil_specialization_constant((PipelineSpecializationConstantType)sc.type, &sc.int_value, sc.stages_bit_offsets, dxil_blobs, true);
}
#if 0
if (dxil_blobs.has(SHADER_STAGE_FRAGMENT)) {
Ref<FileAccess> f = FileAccess::open("res://2.dxil", FileAccess::WRITE);
f->store_buffer(dxil_blobs[SHADER_STAGE_FRAGMENT].ptr(), dxil_blobs[SHADER_STAGE_FRAGMENT].size());
}
#endif
}
// Sign.
for (KeyValue<ShaderStage, Vector<uint8_t>> &E : dxil_blobs) {
ShaderStage stage = E.key;
Vector<uint8_t> &dxil_blob = E.value;
_shader_sign_dxil_bytecode(stage, dxil_blob);
}
// Build the root signature.
ComPtr<ID3DBlob> root_sig_blob;
{
auto stages_to_d3d12_visibility = [](uint32_t p_stages_mask) -> D3D12_SHADER_VISIBILITY {
switch (p_stages_mask) {
case SHADER_STAGE_VERTEX_BIT: {
return D3D12_SHADER_VISIBILITY_VERTEX;
}
case SHADER_STAGE_FRAGMENT_BIT: {
return D3D12_SHADER_VISIBILITY_PIXEL;
}
default: {
return D3D12_SHADER_VISIBILITY_ALL;
}
}
};
LocalVector<D3D12_ROOT_PARAMETER1> root_params;
// Root (push) constants.
if (binary_data.dxil_push_constant_stages) {
CD3DX12_ROOT_PARAMETER1 push_constant;
push_constant.InitAsConstants(
binary_data.push_constant_size / sizeof(uint32_t),
ROOT_CONSTANT_REGISTER,
0,
stages_to_d3d12_visibility(binary_data.dxil_push_constant_stages));
root_params.push_back(push_constant);
}
// NIR-DXIL runtime data.
if (binary_data.nir_runtime_data_root_param_idx == 1) { // Set above to 1 when discovering runtime data is needed.
DEV_ASSERT(!binary_data.is_compute); // Could be supported if needed, but it's pointless as of now.
binary_data.nir_runtime_data_root_param_idx = root_params.size();
CD3DX12_ROOT_PARAMETER1 nir_runtime_data;
nir_runtime_data.InitAsConstants(
sizeof(dxil_spirv_vertex_runtime_data) / sizeof(uint32_t),
RUNTIME_DATA_REGISTER,
0,
D3D12_SHADER_VISIBILITY_VERTEX);
root_params.push_back(nir_runtime_data);
}
// Descriptor tables (up to two per uniform set, for resources and/or samplers).
// These have to stay around until serialization!
struct TraceableDescriptorTable {
uint32_t stages_mask = {};
Vector<D3D12_DESCRIPTOR_RANGE1> ranges;
Vector<ShaderBinary::DataBinding::RootSignatureLocation *> root_sig_locations;
};
Vector<TraceableDescriptorTable> resource_tables_maps;
Vector<TraceableDescriptorTable> sampler_tables_maps;
for (int set = 0; set < sets_bindings.size(); set++) {
bool first_resource_in_set = true;
bool first_sampler_in_set = true;
sets_bindings.write[set].sort();
for (int i = 0; i < sets_bindings[set].size(); i++) {
const ShaderBinary::DataBinding &binding = sets_bindings[set][i];
bool really_used = binding.dxil_stages != 0;
#ifdef DEV_ENABLED
bool anybody_home = (ResourceClass)binding.res_class != RES_CLASS_INVALID || binding.has_sampler;
DEV_ASSERT(anybody_home == really_used);
#endif
if (!really_used) {
continue; // Existed in SPIR-V; went away in DXIL.
}
auto insert_range = [](D3D12_DESCRIPTOR_RANGE_TYPE p_range_type,
uint32_t p_num_descriptors,
uint32_t p_dxil_register,
uint32_t p_dxil_stages_mask,
ShaderBinary::DataBinding::RootSignatureLocation(&p_root_sig_locations),
Vector<TraceableDescriptorTable> &r_tables,
bool &r_first_in_set) {
if (r_first_in_set) {
r_tables.resize(r_tables.size() + 1);
r_first_in_set = false;
}
TraceableDescriptorTable &table = r_tables.write[r_tables.size() - 1];
table.stages_mask |= p_dxil_stages_mask;
CD3DX12_DESCRIPTOR_RANGE1 range;
// Due to the aliasing hack for SRV-UAV of different families,
// we can be causing an unintended change of data (sometimes the validation layers catch it).
D3D12_DESCRIPTOR_RANGE_FLAGS flags = D3D12_DESCRIPTOR_RANGE_FLAG_NONE;
if (p_range_type == D3D12_DESCRIPTOR_RANGE_TYPE_SRV || p_range_type == D3D12_DESCRIPTOR_RANGE_TYPE_UAV) {
flags = D3D12_DESCRIPTOR_RANGE_FLAG_DATA_VOLATILE;
} else if (p_range_type == D3D12_DESCRIPTOR_RANGE_TYPE_CBV) {
flags = D3D12_DESCRIPTOR_RANGE_FLAG_DATA_STATIC_WHILE_SET_AT_EXECUTE;
}
range.Init(p_range_type, p_num_descriptors, p_dxil_register, 0, flags);
table.ranges.push_back(range);
table.root_sig_locations.push_back(&p_root_sig_locations);
};
uint32_t num_descriptors = 1;
D3D12_DESCRIPTOR_RANGE_TYPE resource_range_type = {};
switch ((ResourceClass)binding.res_class) {
case RES_CLASS_INVALID: {
num_descriptors = binding.length;
DEV_ASSERT(binding.has_sampler);
} break;
case RES_CLASS_CBV: {
resource_range_type = D3D12_DESCRIPTOR_RANGE_TYPE_CBV;
DEV_ASSERT(!binding.has_sampler);
} break;
case RES_CLASS_SRV: {
resource_range_type = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
num_descriptors = MAX(1u, binding.length); // An unbound R/O buffer is reflected as zero-size.
} break;
case RES_CLASS_UAV: {
resource_range_type = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
num_descriptors = MAX(1u, binding.length); // An unbound R/W buffer is reflected as zero-size.
DEV_ASSERT(!binding.has_sampler);
} break;
}
uint32_t dxil_register = set * GODOT_NIR_DESCRIPTOR_SET_MULTIPLIER + binding.binding * GODOT_NIR_BINDING_MULTIPLIER;
if (binding.res_class != RES_CLASS_INVALID) {
insert_range(
resource_range_type,
num_descriptors,
dxil_register,
sets_bindings[set][i].dxil_stages,
sets_bindings.write[set].write[i].root_sig_locations[RS_LOC_TYPE_RESOURCE],
resource_tables_maps,
first_resource_in_set);
}
if (binding.has_sampler) {
insert_range(
D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER,
num_descriptors,
dxil_register,
sets_bindings[set][i].dxil_stages,
sets_bindings.write[set].write[i].root_sig_locations[RS_LOC_TYPE_SAMPLER],
sampler_tables_maps,
first_sampler_in_set);
}
}
}
auto make_descriptor_tables = [&root_params, &stages_to_d3d12_visibility](const Vector<TraceableDescriptorTable> &p_tables) {
for (const TraceableDescriptorTable &table : p_tables) {
D3D12_SHADER_VISIBILITY visibility = stages_to_d3d12_visibility(table.stages_mask);
DEV_ASSERT(table.ranges.size() == table.root_sig_locations.size());
for (int i = 0; i < table.ranges.size(); i++) {
// By now we know very well which root signature location corresponds to the pointed uniform.
table.root_sig_locations[i]->root_param_idx = root_params.size();
table.root_sig_locations[i]->range_idx = i;
}
CD3DX12_ROOT_PARAMETER1 root_table;
root_table.InitAsDescriptorTable(table.ranges.size(), table.ranges.ptr(), visibility);
root_params.push_back(root_table);
}
};
make_descriptor_tables(resource_tables_maps);
make_descriptor_tables(sampler_tables_maps);
CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC root_sig_desc = {};
D3D12_ROOT_SIGNATURE_FLAGS root_sig_flags =
D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS |
D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS |
D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS |
D3D12_ROOT_SIGNATURE_FLAG_DENY_AMPLIFICATION_SHADER_ROOT_ACCESS |
D3D12_ROOT_SIGNATURE_FLAG_DENY_MESH_SHADER_ROOT_ACCESS;
if (!stages_processed.has_flag(SHADER_STAGE_VERTEX_BIT)) {
root_sig_flags |= D3D12_ROOT_SIGNATURE_FLAG_DENY_VERTEX_SHADER_ROOT_ACCESS;
}
if (!stages_processed.has_flag(SHADER_STAGE_FRAGMENT_BIT)) {
root_sig_flags |= D3D12_ROOT_SIGNATURE_FLAG_DENY_PIXEL_SHADER_ROOT_ACCESS;
}
if (binary_data.vertex_input_mask) {
root_sig_flags |= D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
}
root_sig_desc.Init_1_1(root_params.size(), root_params.ptr(), 0, nullptr, root_sig_flags);
ComPtr<ID3DBlob> error_blob;
HRESULT res = D3DX12SerializeVersionedRootSignature(context_driver->lib_d3d12, &root_sig_desc, D3D_ROOT_SIGNATURE_VERSION_1_1, root_sig_blob.GetAddressOf(), error_blob.GetAddressOf());
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), Vector<uint8_t>(),
"Serialization of root signature failed with error " + vformat("0x%08ux", (uint64_t)res) + " and the following message:\n" + String((char *)error_blob->GetBufferPointer(), error_blob->GetBufferSize()));
binary_data.root_signature_crc = crc32(0, nullptr, 0);
binary_data.root_signature_crc = crc32(binary_data.root_signature_crc, (const Bytef *)root_sig_blob->GetBufferPointer(), root_sig_blob->GetBufferSize());
}
Vector<Vector<uint8_t>> compressed_stages;
Vector<uint32_t> zstd_size;
uint32_t stages_binary_size = 0;
for (uint32_t i = 0; i < p_spirv.size(); i++) {
Vector<uint8_t> zstd;
Vector<uint8_t> &dxil_blob = dxil_blobs[p_spirv[i].shader_stage];
zstd.resize(Compression::get_max_compressed_buffer_size(dxil_blob.size(), Compression::MODE_ZSTD));
int dst_size = Compression::compress(zstd.ptrw(), dxil_blob.ptr(), dxil_blob.size(), Compression::MODE_ZSTD);
zstd_size.push_back(dst_size);
zstd.resize(dst_size);
compressed_stages.push_back(zstd);
uint32_t s = compressed_stages[i].size();
stages_binary_size += STEPIFY(s, 4);
}
CharString shader_name_utf = p_shader_name.utf8();
binary_data.shader_name_len = shader_name_utf.length();
uint32_t total_size = sizeof(uint32_t) * 3; // Header + version + main datasize;.
total_size += sizeof(ShaderBinary::Data);
total_size += STEPIFY(binary_data.shader_name_len, 4);
for (int i = 0; i < sets_bindings.size(); i++) {
total_size += sizeof(uint32_t);
total_size += sets_bindings[i].size() * sizeof(ShaderBinary::DataBinding);
}
total_size += sizeof(ShaderBinary::SpecializationConstant) * specialization_constants.size();
total_size += compressed_stages.size() * sizeof(uint32_t) * 3; // Sizes.
total_size += stages_binary_size;
binary_data.root_signature_len = root_sig_blob->GetBufferSize();
total_size += binary_data.root_signature_len;
Vector<uint8_t> ret;
ret.resize(total_size);
{
uint32_t offset = 0;
uint8_t *binptr = ret.ptrw();
binptr[0] = 'G';
binptr[1] = 'S';
binptr[2] = 'B';
binptr[3] = 'D'; // Godot shader binary data.
offset += 4;
encode_uint32(ShaderBinary::VERSION, binptr + offset);
offset += sizeof(uint32_t);
encode_uint32(sizeof(ShaderBinary::Data), binptr + offset);
offset += sizeof(uint32_t);
memcpy(binptr + offset, &binary_data, sizeof(ShaderBinary::Data));
offset += sizeof(ShaderBinary::Data);
#define ADVANCE_OFFSET_WITH_ALIGNMENT(m_bytes) \
{ \
offset += m_bytes; \
uint32_t padding = STEPIFY(m_bytes, 4) - m_bytes; \
memset(binptr + offset, 0, padding); /* Avoid garbage data. */ \
offset += padding; \
}
if (binary_data.shader_name_len > 0) {
memcpy(binptr + offset, shader_name_utf.ptr(), binary_data.shader_name_len);
ADVANCE_OFFSET_WITH_ALIGNMENT(binary_data.shader_name_len);
}
for (int i = 0; i < sets_bindings.size(); i++) {
int count = sets_bindings[i].size();
encode_uint32(count, binptr + offset);
offset += sizeof(uint32_t);
if (count > 0) {
memcpy(binptr + offset, sets_bindings[i].ptr(), sizeof(ShaderBinary::DataBinding) * count);
offset += sizeof(ShaderBinary::DataBinding) * count;
}
}
if (specialization_constants.size()) {
memcpy(binptr + offset, specialization_constants.ptr(), sizeof(ShaderBinary::SpecializationConstant) * specialization_constants.size());
offset += sizeof(ShaderBinary::SpecializationConstant) * specialization_constants.size();
}
for (int i = 0; i < compressed_stages.size(); i++) {
encode_uint32(p_spirv[i].shader_stage, binptr + offset);
offset += sizeof(uint32_t);
encode_uint32(dxil_blobs[p_spirv[i].shader_stage].size(), binptr + offset);
offset += sizeof(uint32_t);
encode_uint32(zstd_size[i], binptr + offset);
offset += sizeof(uint32_t);
memcpy(binptr + offset, compressed_stages[i].ptr(), compressed_stages[i].size());
ADVANCE_OFFSET_WITH_ALIGNMENT(compressed_stages[i].size());
}
memcpy(binptr + offset, root_sig_blob->GetBufferPointer(), root_sig_blob->GetBufferSize());
offset += root_sig_blob->GetBufferSize();
ERR_FAIL_COND_V(offset != (uint32_t)ret.size(), Vector<uint8_t>());
}
return ret;
}
RDD::ShaderID RenderingDeviceDriverD3D12::shader_create_from_bytecode(const Vector<uint8_t> &p_shader_binary, ShaderDescription &r_shader_desc, String &r_name, const Vector<ImmutableSampler> &p_immutable_samplers) {
r_shader_desc = {}; // Driver-agnostic.
ShaderInfo shader_info_in; // Driver-specific.
const uint8_t *binptr = p_shader_binary.ptr();
uint32_t binsize = p_shader_binary.size();
uint32_t read_offset = 0;
// Consistency check.
ERR_FAIL_COND_V(binsize < sizeof(uint32_t) * 3 + sizeof(ShaderBinary::Data), ShaderID());
ERR_FAIL_COND_V(binptr[0] != 'G' || binptr[1] != 'S' || binptr[2] != 'B' || binptr[3] != 'D', ShaderID());
uint32_t bin_version = decode_uint32(binptr + 4);
ERR_FAIL_COND_V(bin_version != ShaderBinary::VERSION, ShaderID());
uint32_t bin_data_size = decode_uint32(binptr + 8);
const ShaderBinary::Data &binary_data = *(reinterpret_cast<const ShaderBinary::Data *>(binptr + 12));
r_shader_desc.push_constant_size = binary_data.push_constant_size;
shader_info_in.dxil_push_constant_size = binary_data.dxil_push_constant_stages ? binary_data.push_constant_size : 0;
shader_info_in.nir_runtime_data_root_param_idx = binary_data.nir_runtime_data_root_param_idx;
r_shader_desc.vertex_input_mask = binary_data.vertex_input_mask;
r_shader_desc.fragment_output_mask = binary_data.fragment_output_mask;
r_shader_desc.is_compute = binary_data.is_compute;
shader_info_in.is_compute = binary_data.is_compute;
r_shader_desc.compute_local_size[0] = binary_data.compute_local_size[0];
r_shader_desc.compute_local_size[1] = binary_data.compute_local_size[1];
r_shader_desc.compute_local_size[2] = binary_data.compute_local_size[2];
read_offset += sizeof(uint32_t) * 3 + bin_data_size;
if (binary_data.shader_name_len) {
r_name.parse_utf8((const char *)(binptr + read_offset), binary_data.shader_name_len);
read_offset += STEPIFY(binary_data.shader_name_len, 4);
}
r_shader_desc.uniform_sets.resize(binary_data.set_count);
shader_info_in.sets.resize(binary_data.set_count);
for (uint32_t i = 0; i < binary_data.set_count; i++) {
ERR_FAIL_COND_V(read_offset + sizeof(uint32_t) >= binsize, ShaderID());
uint32_t set_count = decode_uint32(binptr + read_offset);
read_offset += sizeof(uint32_t);
const ShaderBinary::DataBinding *set_ptr = reinterpret_cast<const ShaderBinary::DataBinding *>(binptr + read_offset);
uint32_t set_size = set_count * sizeof(ShaderBinary::DataBinding);
ERR_FAIL_COND_V(read_offset + set_size >= binsize, ShaderID());
shader_info_in.sets[i].bindings.reserve(set_count);
for (uint32_t j = 0; j < set_count; j++) {
ShaderUniform info;
info.type = UniformType(set_ptr[j].type);
info.writable = set_ptr[j].writable;
info.length = set_ptr[j].length;
info.binding = set_ptr[j].binding;
ShaderInfo::UniformBindingInfo binding;
binding.stages = set_ptr[j].dxil_stages;
binding.res_class = (ResourceClass)set_ptr[j].res_class;
binding.type = info.type;
binding.length = info.length;
#ifdef DEV_ENABLED
binding.writable = set_ptr[j].writable;
#endif
static_assert(sizeof(ShaderInfo::UniformBindingInfo::root_sig_locations) == sizeof(ShaderBinary::DataBinding::root_sig_locations));
memcpy((void *)&binding.root_sig_locations, (void *)&set_ptr[j].root_sig_locations, sizeof(ShaderInfo::UniformBindingInfo::root_sig_locations));
if (binding.root_sig_locations.resource.root_param_idx != UINT32_MAX) {
shader_info_in.sets[i].num_root_params.resources++;
}
if (binding.root_sig_locations.sampler.root_param_idx != UINT32_MAX) {
shader_info_in.sets[i].num_root_params.samplers++;
}
r_shader_desc.uniform_sets.write[i].push_back(info);
shader_info_in.sets[i].bindings.push_back(binding);
}
read_offset += set_size;
}
ERR_FAIL_COND_V(read_offset + binary_data.specialization_constants_count * sizeof(ShaderBinary::SpecializationConstant) >= binsize, ShaderID());
r_shader_desc.specialization_constants.resize(binary_data.specialization_constants_count);
shader_info_in.specialization_constants.resize(binary_data.specialization_constants_count);
for (uint32_t i = 0; i < binary_data.specialization_constants_count; i++) {
const ShaderBinary::SpecializationConstant &src_sc = *(reinterpret_cast<const ShaderBinary::SpecializationConstant *>(binptr + read_offset));
ShaderSpecializationConstant sc;
sc.type = PipelineSpecializationConstantType(src_sc.type);
sc.constant_id = src_sc.constant_id;
sc.int_value = src_sc.int_value;
sc.stages = src_sc.stage_flags;
r_shader_desc.specialization_constants.write[i] = sc;
ShaderInfo::SpecializationConstant ssc;
ssc.constant_id = src_sc.constant_id;
ssc.int_value = src_sc.int_value;
memcpy(ssc.stages_bit_offsets, src_sc.stages_bit_offsets, sizeof(ssc.stages_bit_offsets));
shader_info_in.specialization_constants[i] = ssc;
read_offset += sizeof(ShaderBinary::SpecializationConstant);
}
shader_info_in.spirv_specialization_constants_ids_mask = binary_data.spirv_specialization_constants_ids_mask;
for (uint32_t i = 0; i < binary_data.stage_count; i++) {
ERR_FAIL_COND_V(read_offset + sizeof(uint32_t) * 3 >= binsize, ShaderID());
uint32_t stage = decode_uint32(binptr + read_offset);
read_offset += sizeof(uint32_t);
uint32_t dxil_size = decode_uint32(binptr + read_offset);
read_offset += sizeof(uint32_t);
uint32_t zstd_size = decode_uint32(binptr + read_offset);
read_offset += sizeof(uint32_t);
// Decompress.
Vector<uint8_t> dxil;
dxil.resize(dxil_size);
int dec_dxil_size = Compression::decompress(dxil.ptrw(), dxil.size(), binptr + read_offset, zstd_size, Compression::MODE_ZSTD);
ERR_FAIL_COND_V(dec_dxil_size != (int32_t)dxil_size, ShaderID());
shader_info_in.stages_bytecode[ShaderStage(stage)] = dxil;
zstd_size = STEPIFY(zstd_size, 4);
read_offset += zstd_size;
ERR_FAIL_COND_V(read_offset > binsize, ShaderID());
r_shader_desc.stages.push_back(ShaderStage(stage));
}
const uint8_t *root_sig_data_ptr = binptr + read_offset;
PFN_D3D12_CREATE_ROOT_SIGNATURE_DESERIALIZER d3d_D3D12CreateRootSignatureDeserializer = (PFN_D3D12_CREATE_ROOT_SIGNATURE_DESERIALIZER)(void *)GetProcAddress(context_driver->lib_d3d12, "D3D12CreateRootSignatureDeserializer");
ERR_FAIL_NULL_V(d3d_D3D12CreateRootSignatureDeserializer, ShaderID());
HRESULT res = d3d_D3D12CreateRootSignatureDeserializer(root_sig_data_ptr, binary_data.root_signature_len, IID_PPV_ARGS(shader_info_in.root_signature_deserializer.GetAddressOf()));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), ShaderID(), "D3D12CreateRootSignatureDeserializer failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
read_offset += binary_data.root_signature_len;
ERR_FAIL_COND_V(read_offset != binsize, ShaderID());
ComPtr<ID3D12RootSignature> root_signature;
res = device->CreateRootSignature(0, root_sig_data_ptr, binary_data.root_signature_len, IID_PPV_ARGS(shader_info_in.root_signature.GetAddressOf()));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), ShaderID(), "CreateRootSignature failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
shader_info_in.root_signature_desc = shader_info_in.root_signature_deserializer->GetRootSignatureDesc();
shader_info_in.root_signature_crc = binary_data.root_signature_crc;
// Bookkeep.
ShaderInfo *shader_info_ptr = VersatileResource::allocate<ShaderInfo>(resources_allocator);
*shader_info_ptr = shader_info_in;
return ShaderID(shader_info_ptr);
}
uint32_t RenderingDeviceDriverD3D12::shader_get_layout_hash(ShaderID p_shader) {
const ShaderInfo *shader_info_in = (const ShaderInfo *)p_shader.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
return shader_info_in->root_signature_crc;
}
void RenderingDeviceDriverD3D12::shader_free(ShaderID p_shader) {
ShaderInfo *shader_info_in = (ShaderInfo *)p_shader.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
VersatileResource::free(resources_allocator, shader_info_in);
}
void RenderingDeviceDriverD3D12::shader_destroy_modules(ShaderID p_shader) {
ShaderInfo *shader_info_in = (ShaderInfo *)p_shader.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
shader_info_in->stages_bytecode.clear();
}
/*********************/
/**** UNIFORM SET ****/
/*********************/
static void _add_descriptor_count_for_uniform(RenderingDevice::UniformType p_type, uint32_t p_binding_length, bool p_dobule_srv_uav_ambiguous, uint32_t &r_num_resources, uint32_t &r_num_samplers, bool &r_srv_uav_ambiguity) {
r_srv_uav_ambiguity = false;
// Some resource types can be SRV or UAV, depending on what NIR-DXIL decided for a specific shader variant.
// The goal is to generate both SRV and UAV for the descriptor sets' heaps and copy only the relevant one
// to the frame descriptor heap at binding time.
// [[SRV_UAV_AMBIGUITY]]
switch (p_type) {
case RenderingDevice::UNIFORM_TYPE_SAMPLER: {
r_num_samplers += p_binding_length;
} break;
case RenderingDevice::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE:
case RenderingDevice::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE_BUFFER: {
r_num_resources += p_binding_length;
r_num_samplers += p_binding_length;
} break;
case RenderingDevice::UNIFORM_TYPE_UNIFORM_BUFFER: {
r_num_resources += 1;
} break;
case RenderingDevice::UNIFORM_TYPE_STORAGE_BUFFER: {
r_num_resources += p_dobule_srv_uav_ambiguous ? 2 : 1;
r_srv_uav_ambiguity = true;
} break;
case RenderingDevice::UNIFORM_TYPE_IMAGE: {
r_num_resources += p_binding_length * (p_dobule_srv_uav_ambiguous ? 2 : 1);
r_srv_uav_ambiguity = true;
} break;
default: {
r_num_resources += p_binding_length;
}
}
}
RDD::UniformSetID RenderingDeviceDriverD3D12::uniform_set_create(VectorView<BoundUniform> p_uniforms, ShaderID p_shader, uint32_t p_set_index, int p_linear_pool_index) {
//p_linear_pool_index = -1; // TODO:? Linear pools not implemented or not supported by API backend.
// Pre-bookkeep.
UniformSetInfo *uniform_set_info = VersatileResource::allocate<UniformSetInfo>(resources_allocator);
// Do a first pass to count resources and samplers.
uint32_t num_resource_descs = 0;
uint32_t num_sampler_descs = 0;
for (uint32_t i = 0; i < p_uniforms.size(); i++) {
const BoundUniform &uniform = p_uniforms[i];
// Since the uniform set may be created for a shader different than the one that will be actually bound,
// which may have a different set of uniforms optimized out, the stages mask we can check now is not reliable.
// Therefore, we can't make any assumptions here about descriptors that we may not need to create,
// pixel or vertex-only shader resource states, etc.
bool srv_uav_ambiguity = false;
uint32_t binding_length = uniform.ids.size();
if (uniform.type == UNIFORM_TYPE_SAMPLER_WITH_TEXTURE || uniform.type == UNIFORM_TYPE_SAMPLER_WITH_TEXTURE_BUFFER) {
binding_length /= 2;
}
_add_descriptor_count_for_uniform(uniform.type, binding_length, true, num_resource_descs, num_sampler_descs, srv_uav_ambiguity);
}
#ifdef DEV_ENABLED
uniform_set_info->resources_desc_info.reserve(num_resource_descs);
#endif
if (num_resource_descs) {
Error err = uniform_set_info->desc_heaps.resources.allocate(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, num_resource_descs, false);
if (err) {
VersatileResource::free(resources_allocator, uniform_set_info);
ERR_FAIL_V(UniformSetID());
}
}
if (num_sampler_descs) {
Error err = uniform_set_info->desc_heaps.samplers.allocate(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, num_sampler_descs, false);
if (err) {
VersatileResource::free(resources_allocator, uniform_set_info);
ERR_FAIL_V(UniformSetID());
}
}
struct {
DescriptorsHeap::Walker resources;
DescriptorsHeap::Walker samplers;
} desc_heap_walkers;
desc_heap_walkers.resources = uniform_set_info->desc_heaps.resources.make_walker();
desc_heap_walkers.samplers = uniform_set_info->desc_heaps.samplers.make_walker();
struct NeededState {
bool is_buffer = false;
uint64_t shader_uniform_idx_mask = 0;
D3D12_RESOURCE_STATES states = {};
};
HashMap<ResourceInfo *, NeededState> resource_states;
for (uint32_t i = 0; i < p_uniforms.size(); i++) {
const BoundUniform &uniform = p_uniforms[i];
#ifdef DEV_ENABLED
const ShaderInfo *shader_info_in = (const ShaderInfo *)p_shader.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
const ShaderInfo::UniformBindingInfo &shader_uniform = shader_info_in->sets[p_set_index].bindings[i];
bool is_compute = shader_info_in->stages_bytecode.has(SHADER_STAGE_COMPUTE);
DEV_ASSERT(!(is_compute && (shader_uniform.stages & (SHADER_STAGE_VERTEX_BIT | SHADER_STAGE_FRAGMENT_BIT))));
DEV_ASSERT(!(!is_compute && (shader_uniform.stages & SHADER_STAGE_COMPUTE_BIT)));
#endif
switch (uniform.type) {
case UNIFORM_TYPE_SAMPLER: {
for (uint32_t j = 0; j < uniform.ids.size(); j++) {
const D3D12_SAMPLER_DESC &sampler_desc = samplers[uniform.ids[j].id];
device->CreateSampler(&sampler_desc, desc_heap_walkers.samplers.get_curr_cpu_handle());
desc_heap_walkers.samplers.advance();
}
} break;
case UNIFORM_TYPE_SAMPLER_WITH_TEXTURE: {
for (uint32_t j = 0; j < uniform.ids.size(); j += 2) {
const D3D12_SAMPLER_DESC &sampler_desc = samplers[uniform.ids[j].id];
TextureInfo *texture_info = (TextureInfo *)uniform.ids[j + 1].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
device->CreateSampler(&sampler_desc, desc_heap_walkers.samplers.get_curr_cpu_handle());
desc_heap_walkers.samplers.advance();
device->CreateShaderResourceView(texture_info->resource, &texture_info->view_descs.srv, desc_heap_walkers.resources.get_curr_cpu_handle());
#ifdef DEV_ENABLED
uniform_set_info->resources_desc_info.push_back({ D3D12_DESCRIPTOR_RANGE_TYPE_SRV, texture_info->view_descs.srv.ViewDimension });
#endif
desc_heap_walkers.resources.advance();
NeededState &ns = resource_states[texture_info];
ns.shader_uniform_idx_mask |= ((uint64_t)1 << i);
ns.states |= D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE;
}
} break;
case UNIFORM_TYPE_TEXTURE: {
for (uint32_t j = 0; j < uniform.ids.size(); j++) {
TextureInfo *texture_info = (TextureInfo *)uniform.ids[j].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
device->CreateShaderResourceView(texture_info->resource, &texture_info->view_descs.srv, desc_heap_walkers.resources.get_curr_cpu_handle());
#ifdef DEV_ENABLED
uniform_set_info->resources_desc_info.push_back({ D3D12_DESCRIPTOR_RANGE_TYPE_SRV, texture_info->view_descs.srv.ViewDimension });
#endif
desc_heap_walkers.resources.advance();
NeededState &ns = resource_states[texture_info];
ns.shader_uniform_idx_mask |= ((uint64_t)1 << i);
ns.states |= D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE;
}
} break;
case UNIFORM_TYPE_IMAGE: {
for (uint32_t j = 0; j < uniform.ids.size(); j++) {
TextureInfo *texture_info = (TextureInfo *)uniform.ids[j].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
NeededState &ns = resource_states[texture_info];
ns.shader_uniform_idx_mask |= ((uint64_t)1 << i);
ns.states |= (D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
}
// SRVs first. [[SRV_UAV_AMBIGUITY]]
for (uint32_t j = 0; j < uniform.ids.size(); j++) {
TextureInfo *texture_info = (TextureInfo *)uniform.ids[j].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
device->CreateShaderResourceView(texture_info->resource, &texture_info->view_descs.srv, desc_heap_walkers.resources.get_curr_cpu_handle());
#ifdef DEV_ENABLED
uniform_set_info->resources_desc_info.push_back({ D3D12_DESCRIPTOR_RANGE_TYPE_SRV, texture_info->view_descs.srv.ViewDimension });
#endif
desc_heap_walkers.resources.advance();
}
// UAVs then. [[SRV_UAV_AMBIGUITY]]
for (uint32_t j = 0; j < uniform.ids.size(); j++) {
TextureInfo *texture_info = (TextureInfo *)uniform.ids[j].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
device->CreateUnorderedAccessView(texture_info->resource, nullptr, &texture_info->view_descs.uav, desc_heap_walkers.resources.get_curr_cpu_handle());
#ifdef DEV_ENABLED
uniform_set_info->resources_desc_info.push_back({ D3D12_DESCRIPTOR_RANGE_TYPE_UAV, {} });
#endif
desc_heap_walkers.resources.advance();
}
} break;
case UNIFORM_TYPE_TEXTURE_BUFFER:
case UNIFORM_TYPE_SAMPLER_WITH_TEXTURE_BUFFER: {
CRASH_NOW_MSG("Unimplemented!");
} break;
case UNIFORM_TYPE_IMAGE_BUFFER: {
CRASH_NOW_MSG("Unimplemented!");
} break;
case UNIFORM_TYPE_UNIFORM_BUFFER: {
BufferInfo *buf_info = (BufferInfo *)uniform.ids[0].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
D3D12_CONSTANT_BUFFER_VIEW_DESC cbv_desc = {};
cbv_desc.BufferLocation = buf_info->resource->GetGPUVirtualAddress();
cbv_desc.SizeInBytes = STEPIFY(buf_info->size, 256);
device->CreateConstantBufferView(&cbv_desc, desc_heap_walkers.resources.get_curr_cpu_handle());
desc_heap_walkers.resources.advance();
#ifdef DEV_ENABLED
uniform_set_info->resources_desc_info.push_back({ D3D12_DESCRIPTOR_RANGE_TYPE_CBV, {} });
#endif
NeededState &ns = resource_states[buf_info];
ns.is_buffer = true;
ns.shader_uniform_idx_mask |= ((uint64_t)1 << i);
ns.states |= D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER;
} break;
case UNIFORM_TYPE_STORAGE_BUFFER: {
BufferInfo *buf_info = (BufferInfo *)uniform.ids[0].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
// SRV first. [[SRV_UAV_AMBIGUITY]]
{
D3D12_SHADER_RESOURCE_VIEW_DESC srv_desc = {};
srv_desc.Format = DXGI_FORMAT_R32_TYPELESS;
srv_desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
srv_desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srv_desc.Buffer.FirstElement = 0;
srv_desc.Buffer.NumElements = (buf_info->size + 3) / 4;
srv_desc.Buffer.StructureByteStride = 0;
srv_desc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_RAW;
device->CreateShaderResourceView(buf_info->resource, &srv_desc, desc_heap_walkers.resources.get_curr_cpu_handle());
#ifdef DEV_ENABLED
uniform_set_info->resources_desc_info.push_back({ D3D12_DESCRIPTOR_RANGE_TYPE_SRV, srv_desc.ViewDimension });
#endif
desc_heap_walkers.resources.advance();
}
// UAV then. [[SRV_UAV_AMBIGUITY]]
{
if (buf_info->flags.usable_as_uav) {
D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {};
uav_desc.Format = DXGI_FORMAT_R32_TYPELESS;
uav_desc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
uav_desc.Buffer.FirstElement = 0;
uav_desc.Buffer.NumElements = (buf_info->size + 3) / 4;
uav_desc.Buffer.StructureByteStride = 0;
uav_desc.Buffer.CounterOffsetInBytes = 0;
uav_desc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW;
device->CreateUnorderedAccessView(buf_info->resource, nullptr, &uav_desc, desc_heap_walkers.resources.get_curr_cpu_handle());
#ifdef DEV_ENABLED
uniform_set_info->resources_desc_info.push_back({ D3D12_DESCRIPTOR_RANGE_TYPE_UAV, {} });
#endif
} else {
// If can't transition to UAV, leave this one empty since it won't be
// used, and trying to create an UAV view would trigger a validation error.
}
desc_heap_walkers.resources.advance();
}
NeededState &ns = resource_states[buf_info];
ns.shader_uniform_idx_mask |= ((uint64_t)1 << i);
ns.is_buffer = true;
ns.states |= (D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
} break;
case UNIFORM_TYPE_INPUT_ATTACHMENT: {
for (uint32_t j = 0; j < uniform.ids.size(); j++) {
TextureInfo *texture_info = (TextureInfo *)uniform.ids[j].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
device->CreateShaderResourceView(texture_info->resource, &texture_info->view_descs.srv, desc_heap_walkers.resources.get_curr_cpu_handle());
#ifdef DEV_ENABLED
uniform_set_info->resources_desc_info.push_back({ D3D12_DESCRIPTOR_RANGE_TYPE_SRV, texture_info->view_descs.srv.ViewDimension });
#endif
desc_heap_walkers.resources.advance();
NeededState &ns = resource_states[texture_info];
ns.shader_uniform_idx_mask |= ((uint64_t)1 << i);
ns.states |= D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
}
} break;
default: {
DEV_ASSERT(false);
}
}
}
DEV_ASSERT(desc_heap_walkers.resources.is_at_eof());
DEV_ASSERT(desc_heap_walkers.samplers.is_at_eof());
{
uniform_set_info->resource_states.reserve(resource_states.size());
for (const KeyValue<ResourceInfo *, NeededState> &E : resource_states) {
UniformSetInfo::StateRequirement sr;
sr.resource = E.key;
sr.is_buffer = E.value.is_buffer;
sr.states = E.value.states;
sr.shader_uniform_idx_mask = E.value.shader_uniform_idx_mask;
uniform_set_info->resource_states.push_back(sr);
}
}
return UniformSetID(uniform_set_info);
}
void RenderingDeviceDriverD3D12::uniform_set_free(UniformSetID p_uniform_set) {
UniformSetInfo *uniform_set_info = (UniformSetInfo *)p_uniform_set.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
VersatileResource::free(resources_allocator, uniform_set_info);
}
// ----- COMMANDS -----
void RenderingDeviceDriverD3D12::command_uniform_set_prepare_for_use(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) {
if (barrier_capabilities.enhanced_barriers_supported) {
return;
}
// Perform pending blackouts.
{
SelfList<TextureInfo> *E = textures_pending_clear.first();
while (E) {
TextureSubresourceRange subresources;
subresources.layer_count = E->self()->layers;
subresources.mipmap_count = E->self()->mipmaps;
command_clear_color_texture(p_cmd_buffer, TextureID(E->self()), TEXTURE_LAYOUT_UNDEFINED, Color(), subresources);
SelfList<TextureInfo> *next = E->next();
E->remove_from_list();
E = next;
}
}
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
const UniformSetInfo *uniform_set_info = (const UniformSetInfo *)p_uniform_set.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
const ShaderInfo *shader_info_in = (const ShaderInfo *)p_shader.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
const ShaderInfo::UniformSet &shader_set = shader_info_in->sets[p_set_index];
for (const UniformSetInfo::StateRequirement &sr : uniform_set_info->resource_states) {
#ifdef DEV_ENABLED
{
uint32_t stages = 0;
D3D12_RESOURCE_STATES wanted_state = {};
bool writable = false;
// Doing the full loop for debugging since the real one below may break early,
// but we want an exhaustive check
uint64_t inv_uniforms_mask = ~sr.shader_uniform_idx_mask; // Inverting the mask saves operations.
for (uint8_t bit = 0; inv_uniforms_mask != UINT64_MAX; bit++) {
uint64_t bit_mask = ((uint64_t)1 << bit);
if (likely((inv_uniforms_mask & bit_mask))) {
continue;
}
inv_uniforms_mask |= bit_mask;
const ShaderInfo::UniformBindingInfo &binding = shader_set.bindings[bit];
if (unlikely(!binding.stages)) {
continue;
}
D3D12_RESOURCE_STATES required_states = sr.states;
// Resolve a case of SRV/UAV ambiguity now. [[SRV_UAV_AMBIGUITY]]
if ((required_states & D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE) && (required_states & D3D12_RESOURCE_STATE_UNORDERED_ACCESS)) {
if (binding.res_class == RES_CLASS_SRV) {
required_states &= ~D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
} else {
required_states = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
}
}
if (stages) { // Second occurrence at least?
CRASH_COND_MSG(binding.writable != writable, "A resource is used in the same uniform set both as R/O and R/W. That's not supported and shouldn't happen.");
CRASH_COND_MSG(required_states != wanted_state, "A resource is used in the same uniform set with different resource states. The code needs to be enhanced to support that.");
} else {
wanted_state = required_states;
stages |= binding.stages;
writable = binding.writable;
}
DEV_ASSERT((wanted_state == D3D12_RESOURCE_STATE_UNORDERED_ACCESS) == (bool)(wanted_state & D3D12_RESOURCE_STATE_UNORDERED_ACCESS));
}
}
#endif
// We may have assumed D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE for a resource,
// because at uniform set creation time we couldn't know for sure which stages
// it would be used in (due to the fact that a set can be created against a different,
// albeit compatible, shader, which may make a different usage in the end).
// However, now we know and can exclude up to one unneeded states.
// TODO: If subresources involved already in the needed states, or scheduled for it,
// maybe it's more optimal not to do anything here
uint32_t stages = 0;
D3D12_RESOURCE_STATES wanted_state = {};
uint64_t inv_uniforms_mask = ~sr.shader_uniform_idx_mask; // Inverting the mask saves operations.
for (uint8_t bit = 0; inv_uniforms_mask != UINT64_MAX; bit++) {
uint64_t bit_mask = ((uint64_t)1 << bit);
if (likely((inv_uniforms_mask & bit_mask))) {
continue;
}
inv_uniforms_mask |= bit_mask;
const ShaderInfo::UniformBindingInfo &binding = shader_set.bindings[bit];
if (unlikely(!binding.stages)) {
continue;
}
if (!stages) {
D3D12_RESOURCE_STATES required_states = sr.states;
// Resolve a case of SRV/UAV ambiguity now. [[SRV_UAV_AMBIGUITY]]
if ((required_states & D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE) && (required_states & D3D12_RESOURCE_STATE_UNORDERED_ACCESS)) {
if (binding.res_class == RES_CLASS_SRV) {
required_states &= ~D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
} else {
required_states = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
}
}
wanted_state = required_states;
if (!(wanted_state & D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE)) {
// By now, we already know the resource is used, and with no PS/NON_PS disjuntive; no need to check further.
break;
}
}
stages |= binding.stages;
if (stages == (SHADER_STAGE_VERTEX_BIT | SHADER_STAGE_FRAGMENT_BIT) || stages == SHADER_STAGE_COMPUTE_BIT) {
// By now, we already know the resource is used, and as both PS/NON_PS; no need to check further.
break;
}
}
if (likely(wanted_state)) {
if ((wanted_state & D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE)) {
if (stages == SHADER_STAGE_VERTEX_BIT || stages == SHADER_STAGE_COMPUTE_BIT) {
D3D12_RESOURCE_STATES unneeded_states = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
wanted_state &= ~unneeded_states;
} else if (stages == SHADER_STAGE_FRAGMENT_BIT) {
D3D12_RESOURCE_STATES unneeded_states = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
wanted_state &= ~unneeded_states;
}
}
if (likely(wanted_state)) {
if (sr.is_buffer) {
_resource_transition_batch(cmd_buf_info, sr.resource, 0, 1, wanted_state);
} else {
TextureInfo *tex_info = (TextureInfo *)sr.resource;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
uint32_t planes = 1;
if ((tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) {
planes = format_get_plane_count(tex_info->format);
}
for (uint32_t i = 0; i < tex_info->layers; i++) {
for (uint32_t j = 0; j < tex_info->mipmaps; j++) {
uint32_t subresource = D3D12CalcSubresource(tex_info->base_mip + j, tex_info->base_layer + i, 0, tex_info->desc.MipLevels, tex_info->desc.ArraySize());
_resource_transition_batch(cmd_buf_info, tex_info, subresource, planes, wanted_state);
}
}
}
}
}
}
if (p_set_index == shader_info_in->sets.size() - 1) {
_resource_transitions_flush(cmd_buf_info);
}
}
void RenderingDeviceDriverD3D12::_command_check_descriptor_sets(CommandBufferID p_cmd_buffer) {
DEV_ASSERT(segment_begun && "Unable to use commands that rely on descriptors because a segment was never begun.");
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!cmd_buf_info->descriptor_heaps_set) {
// Set descriptor heaps for the command buffer if they haven't been set yet.
ID3D12DescriptorHeap *heaps[] = {
frames[frame_idx].desc_heaps.resources.get_heap(),
frames[frame_idx].desc_heaps.samplers.get_heap(),
};
cmd_buf_info->cmd_list->SetDescriptorHeaps(2, heaps);
cmd_buf_info->descriptor_heaps_set = true;
}
}
void RenderingDeviceDriverD3D12::_command_bind_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index, bool p_for_compute) {
_command_check_descriptor_sets(p_cmd_buffer);
UniformSetInfo *uniform_set_info = (UniformSetInfo *)p_uniform_set.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
const ShaderInfo *shader_info_in = (const ShaderInfo *)p_shader.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
const ShaderInfo::UniformSet &shader_set = shader_info_in->sets[p_set_index];
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
using SetRootDescriptorTableFn = void (STDMETHODCALLTYPE ID3D12GraphicsCommandList::*)(UINT, D3D12_GPU_DESCRIPTOR_HANDLE);
SetRootDescriptorTableFn set_root_desc_table_fn = p_for_compute ? &ID3D12GraphicsCommandList::SetComputeRootDescriptorTable : &ID3D12GraphicsCommandList1::SetGraphicsRootDescriptorTable;
// If this set's descriptors have already been set for the current execution and a compatible root signature, reuse!
uint32_t root_sig_crc = p_for_compute ? cmd_buf_info->compute_root_signature_crc : cmd_buf_info->graphics_root_signature_crc;
UniformSetInfo::RecentBind *last_bind = nullptr;<--- Assignment 'last_bind=nullptr', assigned value is 0<--- Assignment 'last_bind=nullptr', assigned value is 0<--- Assignment 'last_bind=nullptr', assigned value is 0<--- Assignment 'last_bind=nullptr', assigned value is 0<--- Assignment 'last_bind=nullptr', assigned value is 0
for (int i = 0; i < (int)ARRAY_SIZE(uniform_set_info->recent_binds); i++) {<--- Assuming condition is false<--- Assuming condition is false<--- Assuming condition is false<--- Assuming condition is false<--- Assuming condition is false
if (uniform_set_info->recent_binds[i].segment_serial == frames[frame_idx].segment_serial) {
if (uniform_set_info->recent_binds[i].root_signature_crc == root_sig_crc) {
for (const RootDescriptorTable &table : uniform_set_info->recent_binds[i].root_tables.resources) {
(cmd_buf_info->cmd_list.Get()->*set_root_desc_table_fn)(table.root_param_idx, table.start_gpu_handle);
}
for (const RootDescriptorTable &table : uniform_set_info->recent_binds[i].root_tables.samplers) {
(cmd_buf_info->cmd_list.Get()->*set_root_desc_table_fn)(table.root_param_idx, table.start_gpu_handle);
}
#ifdef DEV_ENABLED
uniform_set_info->recent_binds[i].uses++;
frames[frame_idx].uniform_set_reused++;
#endif
return;
} else {
if (!last_bind || uniform_set_info->recent_binds[i].uses < last_bind->uses) {
// Prefer this one since it's been used less or we still haven't a better option.
last_bind = &uniform_set_info->recent_binds[i];
}
}
} else {
// Prefer this one since it's unused.
last_bind = &uniform_set_info->recent_binds[i];
last_bind->uses = 0;
}
}
struct {
DescriptorsHeap::Walker *resources = nullptr;
DescriptorsHeap::Walker *samplers = nullptr;
} frame_heap_walkers;
frame_heap_walkers.resources = &frames[frame_idx].desc_heap_walkers.resources;
frame_heap_walkers.samplers = &frames[frame_idx].desc_heap_walkers.samplers;
struct {
DescriptorsHeap::Walker resources;
DescriptorsHeap::Walker samplers;
} set_heap_walkers;
set_heap_walkers.resources = uniform_set_info->desc_heaps.resources.make_walker();
set_heap_walkers.samplers = uniform_set_info->desc_heaps.samplers.make_walker();
#ifdef DEV_ENABLED
// Whether we have stages where the uniform is actually used should match
// whether we have any root signature locations for it.
for (uint32_t i = 0; i < shader_set.bindings.size(); i++) {
bool has_rs_locations = false;
if (shader_set.bindings[i].root_sig_locations.resource.root_param_idx != UINT32_MAX ||
shader_set.bindings[i].root_sig_locations.sampler.root_param_idx != UINT32_MAX) {
has_rs_locations = true;<--- Variable 'has_rs_locations' is assigned a value that is never used.
break;
}
bool has_stages = shader_set.bindings[i].stages;
DEV_ASSERT(has_rs_locations == has_stages);
}
#endif
last_bind->root_tables.resources.reserve(shader_set.num_root_params.resources);<--- Null pointer dereference
last_bind->root_tables.resources.clear();<--- Null pointer dereference
last_bind->root_tables.samplers.reserve(shader_set.num_root_params.samplers);<--- Null pointer dereference
last_bind->root_tables.samplers.clear();<--- Null pointer dereference
last_bind->uses++;<--- Null pointer dereference
struct {
RootDescriptorTable *resources = nullptr;
RootDescriptorTable *samplers = nullptr;
} tables;
for (uint32_t i = 0; i < shader_set.bindings.size(); i++) {
const ShaderInfo::UniformBindingInfo &binding = shader_set.bindings[i];
uint32_t num_resource_descs = 0;
uint32_t num_sampler_descs = 0;
bool srv_uav_ambiguity = false;
_add_descriptor_count_for_uniform(binding.type, binding.length, false, num_resource_descs, num_sampler_descs, srv_uav_ambiguity);
bool resource_used = false;
if (shader_set.bindings[i].stages) {
{
const ShaderInfo::UniformBindingInfo::RootSignatureLocation &rs_loc_resource = shader_set.bindings[i].root_sig_locations.resource;
if (rs_loc_resource.root_param_idx != UINT32_MAX) { // Location used?
DEV_ASSERT(num_resource_descs);
DEV_ASSERT(!(srv_uav_ambiguity && (shader_set.bindings[i].res_class != RES_CLASS_SRV && shader_set.bindings[i].res_class != RES_CLASS_UAV))); // [[SRV_UAV_AMBIGUITY]]
bool must_flush_table = tables.resources && rs_loc_resource.root_param_idx != tables.resources->root_param_idx;
if (must_flush_table) {
// Check the root signature data has been filled ordered.
DEV_ASSERT(rs_loc_resource.root_param_idx > tables.resources->root_param_idx);
(cmd_buf_info->cmd_list.Get()->*set_root_desc_table_fn)(tables.resources->root_param_idx, tables.resources->start_gpu_handle);
tables.resources = nullptr;
}
if (unlikely(frame_heap_walkers.resources->get_free_handles() < num_resource_descs)) {
if (!frames[frame_idx].desc_heaps_exhausted_reported.resources) {
frames[frame_idx].desc_heaps_exhausted_reported.resources = true;
ERR_FAIL_MSG("Cannot bind uniform set because there's no enough room in current frame's RESOURCES descriptor heap.\n"
"Please increase the value of the rendering/rendering_device/d3d12/max_resource_descriptors_per_frame project setting.");
} else {
return;
}
}
if (!tables.resources) {
DEV_ASSERT(last_bind->root_tables.resources.size() < last_bind->root_tables.resources.get_capacity());
last_bind->root_tables.resources.resize(last_bind->root_tables.resources.size() + 1);
tables.resources = &last_bind->root_tables.resources[last_bind->root_tables.resources.size() - 1];
tables.resources->root_param_idx = rs_loc_resource.root_param_idx;
tables.resources->start_gpu_handle = frame_heap_walkers.resources->get_curr_gpu_handle();
}
// If there is ambiguity and it didn't clarify as SRVs, skip them, which come first. [[SRV_UAV_AMBIGUITY]]
if (srv_uav_ambiguity && shader_set.bindings[i].res_class != RES_CLASS_SRV) {
set_heap_walkers.resources.advance(num_resource_descs);
}
// TODO: Batch to avoid multiple calls where possible (in any case, flush before setting root descriptor tables, or even batch that as well).
device->CopyDescriptorsSimple(
num_resource_descs,
frame_heap_walkers.resources->get_curr_cpu_handle(),
set_heap_walkers.resources.get_curr_cpu_handle(),
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
frame_heap_walkers.resources->advance(num_resource_descs);
// If there is ambiguity and it didn't clarify as UAVs, skip them, which come later. [[SRV_UAV_AMBIGUITY]]
if (srv_uav_ambiguity && shader_set.bindings[i].res_class != RES_CLASS_UAV) {
set_heap_walkers.resources.advance(num_resource_descs);
}
resource_used = true;
}
}
{
const ShaderInfo::UniformBindingInfo::RootSignatureLocation &rs_loc_sampler = shader_set.bindings[i].root_sig_locations.sampler;
if (rs_loc_sampler.root_param_idx != UINT32_MAX) { // Location used?
DEV_ASSERT(num_sampler_descs);
DEV_ASSERT(!srv_uav_ambiguity); // [[SRV_UAV_AMBIGUITY]]
bool must_flush_table = tables.samplers && rs_loc_sampler.root_param_idx != tables.samplers->root_param_idx;
if (must_flush_table) {
// Check the root signature data has been filled ordered.
DEV_ASSERT(rs_loc_sampler.root_param_idx > tables.samplers->root_param_idx);
(cmd_buf_info->cmd_list.Get()->*set_root_desc_table_fn)(tables.samplers->root_param_idx, tables.samplers->start_gpu_handle);
tables.samplers = nullptr;
}
if (unlikely(frame_heap_walkers.samplers->get_free_handles() < num_sampler_descs)) {
if (!frames[frame_idx].desc_heaps_exhausted_reported.samplers) {
frames[frame_idx].desc_heaps_exhausted_reported.samplers = true;
ERR_FAIL_MSG("Cannot bind uniform set because there's no enough room in current frame's SAMPLERS descriptors heap.\n"
"Please increase the value of the rendering/rendering_device/d3d12/max_sampler_descriptors_per_frame project setting.");
} else {
return;
}
}
if (!tables.samplers) {
DEV_ASSERT(last_bind->root_tables.samplers.size() < last_bind->root_tables.samplers.get_capacity());
last_bind->root_tables.samplers.resize(last_bind->root_tables.samplers.size() + 1);
tables.samplers = &last_bind->root_tables.samplers[last_bind->root_tables.samplers.size() - 1];
tables.samplers->root_param_idx = rs_loc_sampler.root_param_idx;
tables.samplers->start_gpu_handle = frame_heap_walkers.samplers->get_curr_gpu_handle();
}
// TODO: Batch to avoid multiple calls where possible (in any case, flush before setting root descriptor tables, or even batch that as well).
device->CopyDescriptorsSimple(
num_sampler_descs,
frame_heap_walkers.samplers->get_curr_cpu_handle(),
set_heap_walkers.samplers.get_curr_cpu_handle(),
D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
frame_heap_walkers.samplers->advance(num_sampler_descs);
}
}
}
// Uniform set descriptor heaps are always full (descriptors are created for every uniform in them) despite
// the shader variant a given set is created upon may not need all of them due to DXC optimizations.
// Therefore, at this point we have to advance through the descriptor set descriptor's heap unconditionally.
set_heap_walkers.resources.advance(num_resource_descs);
if (srv_uav_ambiguity) {
DEV_ASSERT(num_resource_descs);
if (!resource_used) {
set_heap_walkers.resources.advance(num_resource_descs); // Additional skip, since both SRVs and UAVs have to be bypassed.
}
}
set_heap_walkers.samplers.advance(num_sampler_descs);
}
DEV_ASSERT(set_heap_walkers.resources.is_at_eof());
DEV_ASSERT(set_heap_walkers.samplers.is_at_eof());
{
bool must_flush_table = tables.resources;
if (must_flush_table) {
(cmd_buf_info->cmd_list.Get()->*set_root_desc_table_fn)(tables.resources->root_param_idx, tables.resources->start_gpu_handle);
}
}
{
bool must_flush_table = tables.samplers;
if (must_flush_table) {
(cmd_buf_info->cmd_list.Get()->*set_root_desc_table_fn)(tables.samplers->root_param_idx, tables.samplers->start_gpu_handle);
}
}
last_bind->root_signature_crc = root_sig_crc;
last_bind->segment_serial = frames[frame_idx].segment_serial;
}
/******************/
/**** TRANSFER ****/
/******************/
void RenderingDeviceDriverD3D12::command_clear_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, uint64_t p_offset, uint64_t p_size) {
_command_check_descriptor_sets(p_cmd_buffer);
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
BufferInfo *buf_info = (BufferInfo *)p_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (frames[frame_idx].desc_heap_walkers.resources.is_at_eof()) {
if (!frames[frame_idx].desc_heaps_exhausted_reported.resources) {
frames[frame_idx].desc_heaps_exhausted_reported.resources = true;
ERR_FAIL_MSG(
"Cannot clear buffer because there's no enough room in current frame's RESOURCE descriptors heap.\n"
"Please increase the value of the rendering/rendering_device/d3d12/max_resource_descriptors_per_frame project setting.");
} else {
return;
}
}
if (frames[frame_idx].desc_heap_walkers.aux.is_at_eof()) {
if (!frames[frame_idx].desc_heaps_exhausted_reported.aux) {
frames[frame_idx].desc_heaps_exhausted_reported.aux = true;
ERR_FAIL_MSG(
"Cannot clear buffer because there's no enough room in current frame's AUX descriptors heap.\n"
"Please increase the value of the rendering/rendering_device/d3d12/max_misc_descriptors_per_frame project setting.");
} else {
return;
}
}
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, buf_info, 0, 1, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
_resource_transitions_flush(cmd_buf_info);
}
D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = {};
uav_desc.Format = DXGI_FORMAT_R32_TYPELESS;
uav_desc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
uav_desc.Buffer.FirstElement = 0;
uav_desc.Buffer.NumElements = (buf_info->size + 3) / 4;
uav_desc.Buffer.StructureByteStride = 0;
uav_desc.Buffer.CounterOffsetInBytes = 0;
uav_desc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW;
device->CreateUnorderedAccessView(
buf_info->resource,
nullptr,
&uav_desc,
frames[frame_idx].desc_heap_walkers.aux.get_curr_cpu_handle());
device->CopyDescriptorsSimple(
1,
frames[frame_idx].desc_heap_walkers.resources.get_curr_cpu_handle(),
frames[frame_idx].desc_heap_walkers.aux.get_curr_cpu_handle(),
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
static const UINT values[4] = {};
cmd_buf_info->cmd_list->ClearUnorderedAccessViewUint(
frames[frame_idx].desc_heap_walkers.resources.get_curr_gpu_handle(),
frames[frame_idx].desc_heap_walkers.aux.get_curr_cpu_handle(),
buf_info->resource,
values,
0,
nullptr);
frames[frame_idx].desc_heap_walkers.resources.advance();
frames[frame_idx].desc_heap_walkers.aux.advance();
}
void RenderingDeviceDriverD3D12::command_copy_buffer(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, BufferID p_buf_locfer, VectorView<BufferCopyRegion> p_regions) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
BufferInfo *src_buf_info = (BufferInfo *)p_src_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
BufferInfo *buf_loc_info = (BufferInfo *)p_buf_locfer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, src_buf_info, 0, 1, D3D12_RESOURCE_STATE_COPY_SOURCE);
_resource_transition_batch(cmd_buf_info, buf_loc_info, 0, 1, D3D12_RESOURCE_STATE_COPY_DEST);
_resource_transitions_flush(cmd_buf_info);
}
for (uint32_t i = 0; i < p_regions.size(); i++) {
cmd_buf_info->cmd_list->CopyBufferRegion(buf_loc_info->resource, p_regions[i].dst_offset, src_buf_info->resource, p_regions[i].src_offset, p_regions[i].size);
}
}
void RenderingDeviceDriverD3D12::command_copy_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<TextureCopyRegion> p_regions) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
TextureInfo *src_tex_info = (TextureInfo *)p_src_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
TextureInfo *dst_tex_info = (TextureInfo *)p_dst_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!barrier_capabilities.enhanced_barriers_supported) {
// Batch all barrier transitions for the textures before performing the copies.
for (uint32_t i = 0; i < p_regions.size(); i++) {
uint32_t layer_count = MIN(p_regions[i].src_subresources.layer_count, p_regions[i].dst_subresources.layer_count);
for (uint32_t j = 0; j < layer_count; j++) {
UINT src_subresource = _compute_subresource_from_layers(src_tex_info, p_regions[i].src_subresources, j);
UINT dst_subresource = _compute_subresource_from_layers(dst_tex_info, p_regions[i].dst_subresources, j);
_resource_transition_batch(cmd_buf_info, src_tex_info, src_subresource, 1, D3D12_RESOURCE_STATE_COPY_SOURCE);
_resource_transition_batch(cmd_buf_info, dst_tex_info, dst_subresource, 1, D3D12_RESOURCE_STATE_COPY_DEST);
}
}
_resource_transitions_flush(cmd_buf_info);
}
CD3DX12_BOX src_box;
for (uint32_t i = 0; i < p_regions.size(); i++) {
uint32_t layer_count = MIN(p_regions[i].src_subresources.layer_count, p_regions[i].dst_subresources.layer_count);
for (uint32_t j = 0; j < layer_count; j++) {
UINT src_subresource = _compute_subresource_from_layers(src_tex_info, p_regions[i].src_subresources, j);
UINT dst_subresource = _compute_subresource_from_layers(dst_tex_info, p_regions[i].dst_subresources, j);
CD3DX12_TEXTURE_COPY_LOCATION src_location(src_tex_info->resource, src_subresource);
CD3DX12_TEXTURE_COPY_LOCATION dst_location(dst_tex_info->resource, dst_subresource);
src_box.left = p_regions[i].src_offset.x;
src_box.top = p_regions[i].src_offset.y;
src_box.front = p_regions[i].src_offset.z;
src_box.right = p_regions[i].src_offset.x + p_regions[i].size.x;
src_box.bottom = p_regions[i].src_offset.y + p_regions[i].size.y;
src_box.back = p_regions[i].src_offset.z + p_regions[i].size.z;
cmd_buf_info->cmd_list->CopyTextureRegion(&dst_location, p_regions[i].dst_offset.x, p_regions[i].dst_offset.y, p_regions[i].dst_offset.z, &src_location, &src_box);
}
}
}
void RenderingDeviceDriverD3D12::command_resolve_texture(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, uint32_t p_src_layer, uint32_t p_src_mipmap, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, uint32_t p_dst_layer, uint32_t p_dst_mipmap) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
TextureInfo *src_tex_info = (TextureInfo *)p_src_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
TextureInfo *dst_tex_info = (TextureInfo *)p_dst_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
UINT src_subresource = D3D12CalcSubresource(p_src_mipmap, p_src_layer, 0, src_tex_info->desc.MipLevels, src_tex_info->desc.ArraySize());
UINT dst_subresource = D3D12CalcSubresource(p_dst_mipmap, p_dst_layer, 0, dst_tex_info->desc.MipLevels, dst_tex_info->desc.ArraySize());
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, src_tex_info, src_subresource, 1, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
_resource_transition_batch(cmd_buf_info, dst_tex_info, dst_subresource, 1, D3D12_RESOURCE_STATE_RESOLVE_DEST);
_resource_transitions_flush(cmd_buf_info);
}
cmd_buf_info->cmd_list->ResolveSubresource(dst_tex_info->resource, dst_subresource, src_tex_info->resource, src_subresource, RD_TO_D3D12_FORMAT[src_tex_info->format].general_format);
}
void RenderingDeviceDriverD3D12::command_clear_color_texture(CommandBufferID p_cmd_buffer, TextureID p_texture, TextureLayout p_texture_layout, const Color &p_color, const TextureSubresourceRange &p_subresources) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
TextureInfo *tex_info = (TextureInfo *)p_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (tex_info->main_texture) {
tex_info = tex_info->main_texture;
}
auto _transition_subresources = [&](D3D12_RESOURCE_STATES p_new_state) {
for (uint32_t i = 0; i < p_subresources.layer_count; i++) {
for (uint32_t j = 0; j < p_subresources.mipmap_count; j++) {
UINT subresource = D3D12CalcSubresource(
p_subresources.base_mipmap + j,
p_subresources.base_layer + i,
0,
tex_info->desc.MipLevels,
tex_info->desc.ArraySize());
_resource_transition_batch(cmd_buf_info, tex_info, subresource, 1, p_new_state);
}
}
_resource_transitions_flush(cmd_buf_info);
};
if ((tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) {
// Clear via RTV.
if (frames[frame_idx].desc_heap_walkers.rtv.get_free_handles() < p_subresources.mipmap_count) {
if (!frames[frame_idx].desc_heaps_exhausted_reported.rtv) {
frames[frame_idx].desc_heaps_exhausted_reported.rtv = true;
ERR_FAIL_MSG(
"Cannot clear texture because there's no enough room in current frame's RENDER TARGET descriptors heap.\n"
"Please increase the value of the rendering/rendering_device/d3d12/max_misc_descriptors_per_frame project setting.");
} else {
return;
}
}
if (!barrier_capabilities.enhanced_barriers_supported) {
_transition_subresources(D3D12_RESOURCE_STATE_RENDER_TARGET);
}
for (uint32_t i = 0; i < p_subresources.mipmap_count; i++) {
D3D12_RENDER_TARGET_VIEW_DESC rtv_desc = _make_rtv_for_texture(tex_info, p_subresources.base_mipmap + i, p_subresources.base_layer, p_subresources.layer_count, false);
rtv_desc.Format = tex_info->view_descs.uav.Format;
device->CreateRenderTargetView(
tex_info->resource,
&rtv_desc,
frames[frame_idx].desc_heap_walkers.rtv.get_curr_cpu_handle());
cmd_buf_info->cmd_list->ClearRenderTargetView(
frames[frame_idx].desc_heap_walkers.rtv.get_curr_cpu_handle(),
p_color.components,
0,
nullptr);
frames[frame_idx].desc_heap_walkers.rtv.advance();
}
} else if (tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS) {
// Clear via UAV.
_command_check_descriptor_sets(p_cmd_buffer);
if (frames[frame_idx].desc_heap_walkers.resources.get_free_handles() < p_subresources.mipmap_count) {
if (!frames[frame_idx].desc_heaps_exhausted_reported.resources) {
frames[frame_idx].desc_heaps_exhausted_reported.resources = true;
ERR_FAIL_MSG(
"Cannot clear texture because there's no enough room in current frame's RESOURCE descriptors heap.\n"
"Please increase the value of the rendering/rendering_device/d3d12/max_resource_descriptors_per_frame project setting.");
} else {
return;
}
}
if (frames[frame_idx].desc_heap_walkers.aux.get_free_handles() < p_subresources.mipmap_count) {
if (!frames[frame_idx].desc_heaps_exhausted_reported.aux) {
frames[frame_idx].desc_heaps_exhausted_reported.aux = true;
ERR_FAIL_MSG(
"Cannot clear texture because there's no enough room in current frame's AUX descriptors heap.\n"
"Please increase the value of the rendering/rendering_device/d3d12/max_misc_descriptors_per_frame project setting.");
} else {
return;
}
}
if (!barrier_capabilities.enhanced_barriers_supported) {
_transition_subresources(D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
}
for (uint32_t i = 0; i < p_subresources.mipmap_count; i++) {
D3D12_UNORDERED_ACCESS_VIEW_DESC uav_desc = _make_ranged_uav_for_texture(tex_info, p_subresources.base_mipmap + i, p_subresources.base_layer, p_subresources.layer_count, false);
device->CreateUnorderedAccessView(
tex_info->resource,
nullptr,
&uav_desc,
frames[frame_idx].desc_heap_walkers.aux.get_curr_cpu_handle());
device->CopyDescriptorsSimple(
1,
frames[frame_idx].desc_heap_walkers.resources.get_curr_cpu_handle(),
frames[frame_idx].desc_heap_walkers.aux.get_curr_cpu_handle(),
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
UINT values[4] = {
(UINT)p_color.get_r8(),
(UINT)p_color.get_g8(),
(UINT)p_color.get_b8(),
(UINT)p_color.get_a8(),
};
cmd_buf_info->cmd_list->ClearUnorderedAccessViewUint(
frames[frame_idx].desc_heap_walkers.resources.get_curr_gpu_handle(),
frames[frame_idx].desc_heap_walkers.aux.get_curr_cpu_handle(),
tex_info->resource,
values,
0,
nullptr);
frames[frame_idx].desc_heap_walkers.resources.advance();
frames[frame_idx].desc_heap_walkers.aux.advance();
}
} else {
ERR_FAIL_MSG("Cannot clear texture because its format does not support UAV writes. You'll need to update its contents through another method.");
}
}
void RenderingDeviceDriverD3D12::command_copy_buffer_to_texture(CommandBufferID p_cmd_buffer, BufferID p_src_buffer, TextureID p_dst_texture, TextureLayout p_dst_texture_layout, VectorView<BufferTextureCopyRegion> p_regions) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
BufferInfo *buf_info = (BufferInfo *)p_src_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
TextureInfo *tex_info = (TextureInfo *)p_dst_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, buf_info, 0, 1, D3D12_RESOURCE_STATE_COPY_SOURCE);
}
uint32_t pixel_size = get_image_format_pixel_size(tex_info->format);
uint32_t block_w = 0, block_h = 0;
get_compressed_image_format_block_dimensions(tex_info->format, block_w, block_h);
for (uint32_t i = 0; i < p_regions.size(); i++) {
uint32_t region_pitch = (p_regions[i].texture_region_size.x * pixel_size * block_w) >> get_compressed_image_format_pixel_rshift(tex_info->format);
region_pitch = STEPIFY(region_pitch, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
D3D12_PLACED_SUBRESOURCE_FOOTPRINT src_footprint = {};
src_footprint.Offset = p_regions[i].buffer_offset;
src_footprint.Footprint = CD3DX12_SUBRESOURCE_FOOTPRINT(
RD_TO_D3D12_FORMAT[tex_info->format].family,
STEPIFY(p_regions[i].texture_region_size.x, block_w),
STEPIFY(p_regions[i].texture_region_size.y, block_h),
p_regions[i].texture_region_size.z,
region_pitch);
CD3DX12_TEXTURE_COPY_LOCATION copy_src(buf_info->resource, src_footprint);
CD3DX12_BOX src_box(
0, 0, 0,
STEPIFY(p_regions[i].texture_region_size.x, block_w),
STEPIFY(p_regions[i].texture_region_size.y, block_h),
p_regions[i].texture_region_size.z);
if (!barrier_capabilities.enhanced_barriers_supported) {
for (uint32_t j = 0; j < p_regions[i].texture_subresources.layer_count; j++) {
UINT dst_subresource = D3D12CalcSubresource(
p_regions[i].texture_subresources.mipmap,
p_regions[i].texture_subresources.base_layer + j,
_compute_plane_slice(tex_info->format, p_regions[i].texture_subresources.aspect),
tex_info->desc.MipLevels,
tex_info->desc.ArraySize());
CD3DX12_TEXTURE_COPY_LOCATION copy_dst(tex_info->resource, dst_subresource);
_resource_transition_batch(cmd_buf_info, tex_info, dst_subresource, 1, D3D12_RESOURCE_STATE_COPY_DEST);
}
_resource_transitions_flush(cmd_buf_info);
}
for (uint32_t j = 0; j < p_regions[i].texture_subresources.layer_count; j++) {
UINT dst_subresource = D3D12CalcSubresource(
p_regions[i].texture_subresources.mipmap,
p_regions[i].texture_subresources.base_layer + j,
_compute_plane_slice(tex_info->format, p_regions[i].texture_subresources.aspect),
tex_info->desc.MipLevels,
tex_info->desc.ArraySize());
CD3DX12_TEXTURE_COPY_LOCATION copy_dst(tex_info->resource, dst_subresource);
cmd_buf_info->cmd_list->CopyTextureRegion(
©_dst,
p_regions[i].texture_offset.x,
p_regions[i].texture_offset.y,
p_regions[i].texture_offset.z,
©_src,
&src_box);
}
}
}
void RenderingDeviceDriverD3D12::command_copy_texture_to_buffer(CommandBufferID p_cmd_buffer, TextureID p_src_texture, TextureLayout p_src_texture_layout, BufferID p_buf_locfer, VectorView<BufferTextureCopyRegion> p_regions) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
TextureInfo *tex_info = (TextureInfo *)p_src_texture.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
BufferInfo *buf_info = (BufferInfo *)p_buf_locfer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, buf_info, 0, 1, D3D12_RESOURCE_STATE_COPY_DEST);
}
uint32_t block_w = 0, block_h = 0;
get_compressed_image_format_block_dimensions(tex_info->format, block_w, block_h);
for (uint32_t i = 0; i < p_regions.size(); i++) {
if (!barrier_capabilities.enhanced_barriers_supported) {
for (uint32_t j = 0; j < p_regions[i].texture_subresources.layer_count; j++) {
UINT src_subresource = D3D12CalcSubresource(
p_regions[i].texture_subresources.mipmap,
p_regions[i].texture_subresources.base_layer + j,
_compute_plane_slice(tex_info->format, p_regions[i].texture_subresources.aspect),
tex_info->desc.MipLevels,
tex_info->desc.ArraySize());
_resource_transition_batch(cmd_buf_info, tex_info, src_subresource, 1, D3D12_RESOURCE_STATE_COPY_SOURCE);
}
_resource_transitions_flush(cmd_buf_info);
}
for (uint32_t j = 0; j < p_regions[i].texture_subresources.layer_count; j++) {
UINT src_subresource = D3D12CalcSubresource(
p_regions[i].texture_subresources.mipmap,
p_regions[i].texture_subresources.base_layer + j,
_compute_plane_slice(tex_info->format, p_regions[i].texture_subresources.aspect),
tex_info->desc.MipLevels,
tex_info->desc.ArraySize());
CD3DX12_TEXTURE_COPY_LOCATION copy_src(tex_info->resource, src_subresource);
uint32_t computed_d = MAX(1, tex_info->desc.DepthOrArraySize >> p_regions[i].texture_subresources.mipmap);
uint32_t image_size = get_image_format_required_size(
tex_info->format,
MAX(1u, tex_info->desc.Width >> p_regions[i].texture_subresources.mipmap),
MAX(1u, tex_info->desc.Height >> p_regions[i].texture_subresources.mipmap),
computed_d,
1);
uint32_t row_pitch = image_size / (p_regions[i].texture_region_size.y * computed_d) * block_h;
row_pitch = STEPIFY(row_pitch, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
D3D12_PLACED_SUBRESOURCE_FOOTPRINT dst_footprint = {};
dst_footprint.Offset = p_regions[i].buffer_offset;
dst_footprint.Footprint.Width = STEPIFY(p_regions[i].texture_region_size.x, block_w);
dst_footprint.Footprint.Height = STEPIFY(p_regions[i].texture_region_size.y, block_h);
dst_footprint.Footprint.Depth = p_regions[i].texture_region_size.z;
dst_footprint.Footprint.RowPitch = row_pitch;
dst_footprint.Footprint.Format = RD_TO_D3D12_FORMAT[tex_info->format].family;
CD3DX12_TEXTURE_COPY_LOCATION copy_dst(buf_info->resource, dst_footprint);
cmd_buf_info->cmd_list->CopyTextureRegion(©_dst, 0, 0, 0, ©_src, nullptr);
}
}
}
/******************/
/**** PIPELINE ****/
/******************/
void RenderingDeviceDriverD3D12::pipeline_free(PipelineID p_pipeline) {
PipelineInfo *pipeline_info = (PipelineInfo *)(p_pipeline.id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
pipeline_info->pso->Release();
memdelete(pipeline_info);
}
// ----- BINDING -----
void RenderingDeviceDriverD3D12::command_bind_push_constants(CommandBufferID p_cmd_buffer, ShaderID p_shader, uint32_t p_dst_first_index, VectorView<uint32_t> p_data) {
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
const ShaderInfo *shader_info_in = (const ShaderInfo *)p_shader.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!shader_info_in->dxil_push_constant_size) {
return;
}
if (shader_info_in->is_compute) {
cmd_buf_info->cmd_list->SetComputeRoot32BitConstants(0, p_data.size(), p_data.ptr(), p_dst_first_index);
} else {
cmd_buf_info->cmd_list->SetGraphicsRoot32BitConstants(0, p_data.size(), p_data.ptr(), p_dst_first_index);
}
}
// ----- CACHE -----
bool RenderingDeviceDriverD3D12::pipeline_cache_create(const Vector<uint8_t> &p_data) {
WARN_PRINT("PSO caching is not implemented yet in the Direct3D 12 driver.");
return false;
}
void RenderingDeviceDriverD3D12::pipeline_cache_free() {
ERR_FAIL_MSG("Not implemented.");
}
size_t RenderingDeviceDriverD3D12::pipeline_cache_query_size() {
ERR_FAIL_V_MSG(0, "Not implemented.");
}
Vector<uint8_t> RenderingDeviceDriverD3D12::pipeline_cache_serialize() {
ERR_FAIL_V_MSG(Vector<uint8_t>(), "Not implemented.");
}
/*******************/
/**** RENDERING ****/
/*******************/
// ----- SUBPASS -----
RDD::RenderPassID RenderingDeviceDriverD3D12::render_pass_create(VectorView<Attachment> p_attachments, VectorView<Subpass> p_subpasses, VectorView<SubpassDependency> p_subpass_dependencies, uint32_t p_view_count) {
// Pre-bookkeep.
RenderPassInfo *pass_info = VersatileResource::allocate<RenderPassInfo>(resources_allocator);
pass_info->attachments.resize(p_attachments.size());
for (uint32_t i = 0; i < p_attachments.size(); i++) {
pass_info->attachments[i] = p_attachments[i];
}
pass_info->subpasses.resize(p_subpasses.size());
for (uint32_t i = 0; i < p_subpasses.size(); i++) {
pass_info->subpasses[i] = p_subpasses[i];
}
pass_info->view_count = p_view_count;
DXGI_FORMAT *formats = ALLOCA_ARRAY(DXGI_FORMAT, p_attachments.size());
for (uint32_t i = 0; i < p_attachments.size(); i++) {
const D3D12Format &format = RD_TO_D3D12_FORMAT[p_attachments[i].format];
if (format.dsv_format != DXGI_FORMAT_UNKNOWN) {
formats[i] = format.dsv_format;
} else {
formats[i] = format.general_format;
}
}
pass_info->max_supported_sample_count = _find_max_common_supported_sample_count(VectorView(formats, p_attachments.size()));
return RenderPassID(pass_info);
}
void RenderingDeviceDriverD3D12::render_pass_free(RenderPassID p_render_pass) {
RenderPassInfo *pass_info = (RenderPassInfo *)p_render_pass.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
VersatileResource::free(resources_allocator, pass_info);
}
// ----- COMMANDS -----
void RenderingDeviceDriverD3D12::command_begin_render_pass(CommandBufferID p_cmd_buffer, RenderPassID p_render_pass, FramebufferID p_framebuffer, CommandBufferType p_cmd_buffer_type, const Rect2i &p_rect, VectorView<RenderPassClearValue> p_attachment_clears) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
const RenderPassInfo *pass_info = (const RenderPassInfo *)p_render_pass.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
const FramebufferInfo *fb_info = (const FramebufferInfo *)p_framebuffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
DEV_ASSERT(cmd_buf_info->render_pass_state.current_subpass == UINT32_MAX);
auto _transition_subresources = [&](TextureInfo *p_texture_info, D3D12_RESOURCE_STATES p_states) {
uint32_t planes = 1;
if ((p_texture_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) {
planes = format_get_plane_count(p_texture_info->format);
}
for (uint32_t i = 0; i < p_texture_info->layers; i++) {
for (uint32_t j = 0; j < p_texture_info->mipmaps; j++) {
uint32_t subresource = D3D12CalcSubresource(
p_texture_info->base_mip + j,
p_texture_info->base_layer + i,
0,
p_texture_info->desc.MipLevels,
p_texture_info->desc.ArraySize());
_resource_transition_batch(cmd_buf_info, p_texture_info, subresource, planes, p_states);
}
}
};
if (fb_info->is_screen || !barrier_capabilities.enhanced_barriers_supported) {
// Screen framebuffers must perform this transition even if enhanced barriers are supported.
for (uint32_t i = 0; i < fb_info->attachments.size(); i++) {
TextureInfo *tex_info = (TextureInfo *)fb_info->attachments[i].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if ((tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) {
_transition_subresources(tex_info, D3D12_RESOURCE_STATE_RENDER_TARGET);
} else if ((tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) {
_transition_subresources(tex_info, D3D12_RESOURCE_STATE_DEPTH_WRITE);
} else {
DEV_ASSERT(false);
}
}
if (fb_info->vrs_attachment) {
TextureInfo *tex_info = (TextureInfo *)fb_info->vrs_attachment.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_transition_subresources(tex_info, D3D12_RESOURCE_STATE_SHADING_RATE_SOURCE);
}
_resource_transitions_flush(cmd_buf_info);
}
cmd_buf_info->render_pass_state.region_rect = CD3DX12_RECT(
p_rect.position.x,
p_rect.position.y,
p_rect.position.x + p_rect.size.x,
p_rect.position.y + p_rect.size.y);
cmd_buf_info->render_pass_state.region_is_all = !(
cmd_buf_info->render_pass_state.region_rect.left == 0 &&
cmd_buf_info->render_pass_state.region_rect.top == 0 &&
cmd_buf_info->render_pass_state.region_rect.right == fb_info->size.x &&
cmd_buf_info->render_pass_state.region_rect.bottom == fb_info->size.y);
for (uint32_t i = 0; i < pass_info->attachments.size(); i++) {
if (pass_info->attachments[i].load_op == ATTACHMENT_LOAD_OP_DONT_CARE) {
const TextureInfo *tex_info = (const TextureInfo *)fb_info->attachments[i].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_discard_texture_subresources(tex_info, cmd_buf_info);
}
}
if (fb_info->vrs_attachment && vrs_capabilities.ss_image_supported) {
ComPtr<ID3D12GraphicsCommandList5> cmd_list_5;
cmd_buf_info->cmd_list->QueryInterface(cmd_list_5.GetAddressOf());
if (cmd_list_5) {
static const D3D12_SHADING_RATE_COMBINER COMBINERS[D3D12_RS_SET_SHADING_RATE_COMBINER_COUNT] = {
D3D12_SHADING_RATE_COMBINER_PASSTHROUGH,
D3D12_SHADING_RATE_COMBINER_OVERRIDE,
};
cmd_list_5->RSSetShadingRate(D3D12_SHADING_RATE_1X1, COMBINERS);
}
}
cmd_buf_info->render_pass_state.current_subpass = UINT32_MAX;
cmd_buf_info->render_pass_state.fb_info = fb_info;
cmd_buf_info->render_pass_state.pass_info = pass_info;
command_next_render_subpass(p_cmd_buffer, p_cmd_buffer_type);
AttachmentClear *clears = ALLOCA_ARRAY(AttachmentClear, pass_info->attachments.size());
Rect2i *clear_rects = ALLOCA_ARRAY(Rect2i, pass_info->attachments.size());
uint32_t num_clears = 0;
for (uint32_t i = 0; i < pass_info->attachments.size(); i++) {
TextureInfo *tex_info = (TextureInfo *)fb_info->attachments[i].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!tex_info) {
continue;
}
AttachmentClear clear;
if ((tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) {
if (pass_info->attachments[i].load_op == ATTACHMENT_LOAD_OP_CLEAR) {
clear.aspect.set_flag(TEXTURE_ASPECT_COLOR_BIT);
clear.color_attachment = i;
tex_info->pending_clear.remove_from_list();
}
} else if ((tex_info->desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) {
if (pass_info->attachments[i].stencil_load_op == ATTACHMENT_LOAD_OP_CLEAR) {
clear.aspect.set_flag(TEXTURE_ASPECT_DEPTH_BIT);
}
}
if (!clear.aspect.is_empty()) {
clear.value = p_attachment_clears[i];
clears[num_clears] = clear;
clear_rects[num_clears] = p_rect;
num_clears++;
}
}
if (num_clears) {
command_render_clear_attachments(p_cmd_buffer, VectorView(clears, num_clears), VectorView(clear_rects, num_clears));
}
}
void RenderingDeviceDriverD3D12::_end_render_pass(CommandBufferID p_cmd_buffer) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
DEV_ASSERT(cmd_buf_info->render_pass_state.current_subpass != UINT32_MAX);
const FramebufferInfo *fb_info = cmd_buf_info->render_pass_state.fb_info;
const RenderPassInfo *pass_info = cmd_buf_info->render_pass_state.pass_info;
const Subpass &subpass = pass_info->subpasses[cmd_buf_info->render_pass_state.current_subpass];
if (fb_info->is_screen) {
// Screen framebuffers must transition back to present state when the render pass is finished.
for (uint32_t i = 0; i < fb_info->attachments.size(); i++) {
TextureInfo *src_tex_info = (TextureInfo *)(fb_info->attachments[i].id);<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
uint32_t src_subresource = D3D12CalcSubresource(src_tex_info->base_mip, src_tex_info->base_layer, 0, src_tex_info->desc.MipLevels, src_tex_info->desc.ArraySize());
_resource_transition_batch(cmd_buf_info, src_tex_info, src_subresource, 1, D3D12_RESOURCE_STATE_PRESENT);
}
}
struct Resolve {
ID3D12Resource *src_res = nullptr;
uint32_t src_subres = 0;
ID3D12Resource *dst_res = nullptr;
uint32_t dst_subres = 0;
DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
};
Resolve *resolves = ALLOCA_ARRAY(Resolve, subpass.resolve_references.size());
uint32_t num_resolves = 0;
for (uint32_t i = 0; i < subpass.resolve_references.size(); i++) {
uint32_t color_index = subpass.color_references[i].attachment;
uint32_t resolve_index = subpass.resolve_references[i].attachment;
DEV_ASSERT((color_index == AttachmentReference::UNUSED) == (resolve_index == AttachmentReference::UNUSED));
if (color_index == AttachmentReference::UNUSED || !fb_info->attachments[color_index]) {
continue;
}
TextureInfo *src_tex_info = (TextureInfo *)fb_info->attachments[color_index].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
uint32_t src_subresource = D3D12CalcSubresource(src_tex_info->base_mip, src_tex_info->base_layer, 0, src_tex_info->desc.MipLevels, src_tex_info->desc.ArraySize());
_resource_transition_batch(cmd_buf_info, src_tex_info, src_subresource, 1, D3D12_RESOURCE_STATE_RESOLVE_SOURCE);
TextureInfo *dst_tex_info = (TextureInfo *)fb_info->attachments[resolve_index].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
uint32_t dst_subresource = D3D12CalcSubresource(dst_tex_info->base_mip, dst_tex_info->base_layer, 0, dst_tex_info->desc.MipLevels, dst_tex_info->desc.ArraySize());
_resource_transition_batch(cmd_buf_info, dst_tex_info, dst_subresource, 1, D3D12_RESOURCE_STATE_RESOLVE_DEST);
resolves[num_resolves].src_res = src_tex_info->resource;
resolves[num_resolves].src_subres = src_subresource;
resolves[num_resolves].dst_res = dst_tex_info->resource;
resolves[num_resolves].dst_subres = dst_subresource;
resolves[num_resolves].format = RD_TO_D3D12_FORMAT[src_tex_info->format].general_format;
num_resolves++;
}
_resource_transitions_flush(cmd_buf_info);
for (uint32_t i = 0; i < num_resolves; i++) {
cmd_buf_info->cmd_list->ResolveSubresource(resolves[i].dst_res, resolves[i].dst_subres, resolves[i].src_res, resolves[i].src_subres, resolves[i].format);
}
}
void RenderingDeviceDriverD3D12::command_end_render_pass(CommandBufferID p_cmd_buffer) {
_end_render_pass(p_cmd_buffer);
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
DEV_ASSERT(cmd_buf_info->render_pass_state.current_subpass != UINT32_MAX);
const FramebufferInfo *fb_info = cmd_buf_info->render_pass_state.fb_info;
const RenderPassInfo *pass_info = cmd_buf_info->render_pass_state.pass_info;
if (vrs_capabilities.ss_image_supported) {
ComPtr<ID3D12GraphicsCommandList5> cmd_list_5;
cmd_buf_info->cmd_list->QueryInterface(cmd_list_5.GetAddressOf());
if (cmd_list_5) {
cmd_list_5->RSSetShadingRateImage(nullptr);
}
}
for (uint32_t i = 0; i < pass_info->attachments.size(); i++) {
if (pass_info->attachments[i].store_op == ATTACHMENT_STORE_OP_DONT_CARE) {
const TextureInfo *tex_info = (const TextureInfo *)fb_info->attachments[i].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_discard_texture_subresources(tex_info, cmd_buf_info);
}
}
cmd_buf_info->render_pass_state.current_subpass = UINT32_MAX;
}
void RenderingDeviceDriverD3D12::command_next_render_subpass(CommandBufferID p_cmd_buffer, CommandBufferType p_cmd_buffer_type) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (cmd_buf_info->render_pass_state.current_subpass == UINT32_MAX) {
cmd_buf_info->render_pass_state.current_subpass = 0;
} else {
_end_render_pass(p_cmd_buffer);
cmd_buf_info->render_pass_state.current_subpass++;
}
const FramebufferInfo *fb_info = cmd_buf_info->render_pass_state.fb_info;
const RenderPassInfo *pass_info = cmd_buf_info->render_pass_state.pass_info;
const Subpass &subpass = pass_info->subpasses[cmd_buf_info->render_pass_state.current_subpass];
D3D12_CPU_DESCRIPTOR_HANDLE *rtv_handles = ALLOCA_ARRAY(D3D12_CPU_DESCRIPTOR_HANDLE, subpass.color_references.size());
DescriptorsHeap::Walker rtv_heap_walker = fb_info->rtv_heap.make_walker();
for (uint32_t i = 0; i < subpass.color_references.size(); i++) {
uint32_t attachment = subpass.color_references[i].attachment;
if (attachment == AttachmentReference::UNUSED) {
if (!frames[frame_idx].null_rtv_handle.ptr) {
// No null descriptor-handle created for this frame yet.
if (frames[frame_idx].desc_heap_walkers.rtv.is_at_eof()) {
if (!frames[frame_idx].desc_heaps_exhausted_reported.rtv) {
frames[frame_idx].desc_heaps_exhausted_reported.rtv = true;
ERR_FAIL_MSG("Cannot begin subpass because there's no enough room in current frame's RENDER TARGET descriptors heap.\n"
"Please increase the value of the rendering/rendering_device/d3d12/max_misc_descriptors_per_frame project setting.");
} else {
return;
}
}
D3D12_RENDER_TARGET_VIEW_DESC rtv_desc_null = {};
rtv_desc_null.Format = DXGI_FORMAT_R8_UINT;
rtv_desc_null.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
frames[frame_idx].null_rtv_handle = frames[frame_idx].desc_heap_walkers.rtv.get_curr_cpu_handle();
device->CreateRenderTargetView(nullptr, &rtv_desc_null, frames[frame_idx].null_rtv_handle);
frames[frame_idx].desc_heap_walkers.rtv.advance();
}
rtv_handles[i] = frames[frame_idx].null_rtv_handle;
} else {
uint32_t rt_index = fb_info->attachments_handle_inds[attachment];
rtv_heap_walker.rewind();
rtv_heap_walker.advance(rt_index);
rtv_handles[i] = rtv_heap_walker.get_curr_cpu_handle();
}
}
D3D12_CPU_DESCRIPTOR_HANDLE dsv_handle = {};
{
DescriptorsHeap::Walker dsv_heap_walker = fb_info->dsv_heap.make_walker();
if (subpass.depth_stencil_reference.attachment != AttachmentReference::UNUSED) {
uint32_t ds_index = fb_info->attachments_handle_inds[subpass.depth_stencil_reference.attachment];
dsv_heap_walker.rewind();
dsv_heap_walker.advance(ds_index);
dsv_handle = dsv_heap_walker.get_curr_cpu_handle();
}
}
cmd_buf_info->cmd_list->OMSetRenderTargets(subpass.color_references.size(), rtv_handles, false, dsv_handle.ptr ? &dsv_handle : nullptr);
}
void RenderingDeviceDriverD3D12::command_render_set_viewport(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_viewports) {
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
D3D12_VIEWPORT *d3d12_viewports = ALLOCA_ARRAY(D3D12_VIEWPORT, p_viewports.size());
for (uint32_t i = 0; i < p_viewports.size(); i++) {
d3d12_viewports[i] = CD3DX12_VIEWPORT(
p_viewports[i].position.x,
p_viewports[i].position.y,
p_viewports[i].size.x,
p_viewports[i].size.y);
}
cmd_buf_info->cmd_list->RSSetViewports(p_viewports.size(), d3d12_viewports);
}
void RenderingDeviceDriverD3D12::command_render_set_scissor(CommandBufferID p_cmd_buffer, VectorView<Rect2i> p_scissors) {
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
D3D12_RECT *d3d12_scissors = ALLOCA_ARRAY(D3D12_RECT, p_scissors.size());
for (uint32_t i = 0; i < p_scissors.size(); i++) {
d3d12_scissors[i] = CD3DX12_RECT(
p_scissors[i].position.x,
p_scissors[i].position.y,
p_scissors[i].position.x + p_scissors[i].size.x,
p_scissors[i].position.y + p_scissors[i].size.y);
}
cmd_buf_info->cmd_list->RSSetScissorRects(p_scissors.size(), d3d12_scissors);
}
void RenderingDeviceDriverD3D12::command_render_clear_attachments(CommandBufferID p_cmd_buffer, VectorView<AttachmentClear> p_attachment_clears, VectorView<Rect2i> p_rects) {
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
DEV_ASSERT(cmd_buf_info->render_pass_state.current_subpass != UINT32_MAX);
const FramebufferInfo *fb_info = cmd_buf_info->render_pass_state.fb_info;
const RenderPassInfo *pass_info = cmd_buf_info->render_pass_state.pass_info;
DescriptorsHeap::Walker rtv_heap_walker = fb_info->rtv_heap.make_walker();
DescriptorsHeap::Walker dsv_heap_walker = fb_info->dsv_heap.make_walker();
for (uint32_t i = 0; i < p_attachment_clears.size(); i++) {
uint32_t attachment = UINT32_MAX;
bool is_render_target = false;
if (p_attachment_clears[i].aspect.has_flag(TEXTURE_ASPECT_COLOR_BIT)) {
attachment = p_attachment_clears[i].color_attachment;
is_render_target = true;
} else {
attachment = pass_info->subpasses[cmd_buf_info->render_pass_state.current_subpass].depth_stencil_reference.attachment;
}
for (uint32_t j = 0; j < p_rects.size(); j++) {
D3D12_RECT rect = CD3DX12_RECT(
p_rects[j].position.x,
p_rects[j].position.y,
p_rects[j].position.x + p_rects[j].size.x,
p_rects[j].position.y + p_rects[j].size.y);
const D3D12_RECT *rect_ptr = cmd_buf_info->render_pass_state.region_is_all ? nullptr : ▭
if (is_render_target) {
uint32_t color_idx = fb_info->attachments_handle_inds[attachment];
rtv_heap_walker.rewind();
rtv_heap_walker.advance(color_idx);
cmd_buf_info->cmd_list->ClearRenderTargetView(
rtv_heap_walker.get_curr_cpu_handle(),
p_attachment_clears[i].value.color.components,
rect_ptr ? 1 : 0,
rect_ptr);
} else {
uint32_t depth_stencil_idx = fb_info->attachments_handle_inds[attachment];
dsv_heap_walker.rewind();
dsv_heap_walker.advance(depth_stencil_idx);
D3D12_CLEAR_FLAGS flags = {};
if (p_attachment_clears[i].aspect.has_flag(TEXTURE_ASPECT_DEPTH_BIT)) {
flags |= D3D12_CLEAR_FLAG_DEPTH;
}
if (p_attachment_clears[i].aspect.has_flag(TEXTURE_ASPECT_STENCIL_BIT)) {
flags |= D3D12_CLEAR_FLAG_STENCIL;
}
cmd_buf_info->cmd_list->ClearDepthStencilView(
dsv_heap_walker.get_curr_cpu_handle(),
flags,
p_attachment_clears[i].value.depth,
p_attachment_clears[i].value.stencil,
rect_ptr ? 1 : 0,
rect_ptr);
}
}
}
}
void RenderingDeviceDriverD3D12::command_bind_render_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
const PipelineInfo *pipeline_info = (const PipelineInfo *)p_pipeline.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (cmd_buf_info->graphics_pso == pipeline_info->pso) {
return;
}
const ShaderInfo *shader_info_in = pipeline_info->shader_info;
const RenderPipelineInfo &render_info = pipeline_info->render_info;
cmd_buf_info->cmd_list->SetPipelineState(pipeline_info->pso);
if (cmd_buf_info->graphics_root_signature_crc != shader_info_in->root_signature_crc) {
cmd_buf_info->cmd_list->SetGraphicsRootSignature(shader_info_in->root_signature.Get());
cmd_buf_info->graphics_root_signature_crc = shader_info_in->root_signature_crc;
}
cmd_buf_info->cmd_list->IASetPrimitiveTopology(render_info.dyn_params.primitive_topology);
cmd_buf_info->cmd_list->OMSetBlendFactor(render_info.dyn_params.blend_constant.components);
cmd_buf_info->cmd_list->OMSetStencilRef(render_info.dyn_params.stencil_reference);
if (misc_features_support.depth_bounds_supported) {
ComPtr<ID3D12GraphicsCommandList1> command_list_1;
cmd_buf_info->cmd_list->QueryInterface(command_list_1.GetAddressOf());
if (command_list_1) {
command_list_1->OMSetDepthBounds(render_info.dyn_params.depth_bounds_min, render_info.dyn_params.depth_bounds_max);
}
}
cmd_buf_info->render_pass_state.vf_info = render_info.vf_info;
cmd_buf_info->graphics_pso = pipeline_info->pso;
cmd_buf_info->compute_pso = nullptr;
}
void RenderingDeviceDriverD3D12::command_bind_render_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) {
_command_bind_uniform_set(p_cmd_buffer, p_uniform_set, p_shader, p_set_index, false);
}
void RenderingDeviceDriverD3D12::command_bind_render_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count) {
for (uint32_t i = 0u; i < p_set_count; ++i) {
// TODO: _command_bind_uniform_set() does WAAAAY too much stuff. A lot of it should be already cached in UniformSetID when uniform_set_create() was called. Binding is supposed to be a cheap operation, ideally a memcpy.
_command_bind_uniform_set(p_cmd_buffer, p_uniform_sets[i], p_shader, p_first_set_index + i, false);
}
}
void RenderingDeviceDriverD3D12::command_render_draw(CommandBufferID p_cmd_buffer, uint32_t p_vertex_count, uint32_t p_instance_count, uint32_t p_base_vertex, uint32_t p_first_instance) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_bind_vertex_buffers(cmd_buf_info);
cmd_buf_info->cmd_list->DrawInstanced(p_vertex_count, p_instance_count, p_base_vertex, p_first_instance);
}
void RenderingDeviceDriverD3D12::command_render_draw_indexed(CommandBufferID p_cmd_buffer, uint32_t p_index_count, uint32_t p_instance_count, uint32_t p_first_index, int32_t p_vertex_offset, uint32_t p_first_instance) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_bind_vertex_buffers(cmd_buf_info);
cmd_buf_info->cmd_list->DrawIndexedInstanced(p_index_count, p_instance_count, p_first_index, p_vertex_offset, p_first_instance);
}
void RenderingDeviceDriverD3D12::command_render_draw_indexed_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_bind_vertex_buffers(cmd_buf_info);
BufferInfo *indirect_buf_info = (BufferInfo *)p_indirect_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, indirect_buf_info, 0, 1, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
_resource_transitions_flush(cmd_buf_info);
}
cmd_buf_info->cmd_list->ExecuteIndirect(indirect_cmd_signatures.draw_indexed.Get(), p_draw_count, indirect_buf_info->resource, p_offset, nullptr, 0);
}
void RenderingDeviceDriverD3D12::command_render_draw_indexed_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_bind_vertex_buffers(cmd_buf_info);
BufferInfo *indirect_buf_info = (BufferInfo *)p_indirect_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
BufferInfo *count_buf_info = (BufferInfo *)p_count_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, indirect_buf_info, 0, 1, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
_resource_transition_batch(cmd_buf_info, count_buf_info, 0, 1, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
_resource_transitions_flush(cmd_buf_info);
}
cmd_buf_info->cmd_list->ExecuteIndirect(indirect_cmd_signatures.draw_indexed.Get(), p_max_draw_count, indirect_buf_info->resource, p_offset, count_buf_info->resource, p_count_buffer_offset);
}
void RenderingDeviceDriverD3D12::command_render_draw_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, uint32_t p_draw_count, uint32_t p_stride) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_bind_vertex_buffers(cmd_buf_info);
BufferInfo *indirect_buf_info = (BufferInfo *)p_indirect_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, indirect_buf_info, 0, 1, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
_resource_transitions_flush(cmd_buf_info);
}
cmd_buf_info->cmd_list->ExecuteIndirect(indirect_cmd_signatures.draw.Get(), p_draw_count, indirect_buf_info->resource, p_offset, nullptr, 0);
}
void RenderingDeviceDriverD3D12::command_render_draw_indirect_count(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset, BufferID p_count_buffer, uint64_t p_count_buffer_offset, uint32_t p_max_draw_count, uint32_t p_stride) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_bind_vertex_buffers(cmd_buf_info);
BufferInfo *indirect_buf_info = (BufferInfo *)p_indirect_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
BufferInfo *count_buf_info = (BufferInfo *)p_count_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, indirect_buf_info, 0, 1, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
_resource_transition_batch(cmd_buf_info, count_buf_info, 0, 1, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
_resource_transitions_flush(cmd_buf_info);
}
cmd_buf_info->cmd_list->ExecuteIndirect(indirect_cmd_signatures.draw.Get(), p_max_draw_count, indirect_buf_info->resource, p_offset, count_buf_info->resource, p_count_buffer_offset);
}
void RenderingDeviceDriverD3D12::command_render_bind_vertex_buffers(CommandBufferID p_cmd_buffer, uint32_t p_binding_count, const BufferID *p_buffers, const uint64_t *p_offsets) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
DEV_ASSERT(cmd_buf_info->render_pass_state.current_subpass != UINT32_MAX);
// Vertex buffer views are set deferredly, to be sure we already know the strides by then,
// which is only true once the pipeline has been bound. Otherwise, we'd need that the pipeline
// is always bound first, which would be not kind of us. [[DEFERRED_VERTEX_BUFFERS]]
DEV_ASSERT(p_binding_count <= ARRAY_SIZE(cmd_buf_info->render_pass_state.vertex_buffer_views));
for (uint32_t i = 0; i < p_binding_count; i++) {
BufferInfo *buffer_info = (BufferInfo *)p_buffers[i].id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
cmd_buf_info->render_pass_state.vertex_buffer_views[i] = {};
cmd_buf_info->render_pass_state.vertex_buffer_views[i].BufferLocation = buffer_info->resource->GetGPUVirtualAddress() + p_offsets[i];
cmd_buf_info->render_pass_state.vertex_buffer_views[i].SizeInBytes = buffer_info->size - p_offsets[i];
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, buffer_info, 0, 1, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
}
}
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transitions_flush(cmd_buf_info);
}
cmd_buf_info->render_pass_state.vertex_buffer_count = p_binding_count;
}
void RenderingDeviceDriverD3D12::command_render_bind_index_buffer(CommandBufferID p_cmd_buffer, BufferID p_buffer, IndexBufferFormat p_format, uint64_t p_offset) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
BufferInfo *buffer_info = (BufferInfo *)p_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
D3D12_INDEX_BUFFER_VIEW d3d12_ib_view = {};
d3d12_ib_view.BufferLocation = buffer_info->resource->GetGPUVirtualAddress() + p_offset;
d3d12_ib_view.SizeInBytes = buffer_info->size - p_offset;
d3d12_ib_view.Format = p_format == INDEX_BUFFER_FORMAT_UINT16 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT;
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, buffer_info, 0, 1, D3D12_RESOURCE_STATE_INDEX_BUFFER);
_resource_transitions_flush(cmd_buf_info);
}
cmd_buf_info->cmd_list->IASetIndexBuffer(&d3d12_ib_view);
}
// [[DEFERRED_VERTEX_BUFFERS]]
void RenderingDeviceDriverD3D12::_bind_vertex_buffers(CommandBufferInfo *p_cmd_buf_info) {
RenderPassState &render_pass_state = p_cmd_buf_info->render_pass_state;
if (render_pass_state.vertex_buffer_count && render_pass_state.vf_info) {
for (uint32_t i = 0; i < render_pass_state.vertex_buffer_count; i++) {
render_pass_state.vertex_buffer_views[i].StrideInBytes = render_pass_state.vf_info->vertex_buffer_strides[i];
}
p_cmd_buf_info->cmd_list->IASetVertexBuffers(0, render_pass_state.vertex_buffer_count, render_pass_state.vertex_buffer_views);
render_pass_state.vertex_buffer_count = 0;
}
}
void RenderingDeviceDriverD3D12::command_render_set_blend_constants(CommandBufferID p_cmd_buffer, const Color &p_constants) {
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
cmd_buf_info->cmd_list->OMSetBlendFactor(p_constants.components);
}
void RenderingDeviceDriverD3D12::command_render_set_line_width(CommandBufferID p_cmd_buffer, float p_width) {
if (!Math::is_equal_approx(p_width, 1.0f)) {
ERR_FAIL_MSG("Setting line widths other than 1.0 is not supported by the Direct3D 12 rendering driver.");
}
}
// ----- PIPELINE -----
static const D3D12_PRIMITIVE_TOPOLOGY_TYPE RD_PRIMITIVE_TO_D3D12_TOPOLOGY_TYPE[RDD::RENDER_PRIMITIVE_MAX] = {
D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_PATCH,
};
static const D3D12_PRIMITIVE_TOPOLOGY RD_PRIMITIVE_TO_D3D12_TOPOLOGY[RDD::RENDER_PRIMITIVE_MAX] = {
D3D_PRIMITIVE_TOPOLOGY_POINTLIST,
D3D_PRIMITIVE_TOPOLOGY_LINELIST,
D3D_PRIMITIVE_TOPOLOGY_LINELIST_ADJ,
D3D_PRIMITIVE_TOPOLOGY_LINESTRIP,
D3D_PRIMITIVE_TOPOLOGY_LINESTRIP_ADJ,
D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST,
D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST_ADJ,
D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP,
D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP_ADJ,
D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP,
D3D_PRIMITIVE_TOPOLOGY_1_CONTROL_POINT_PATCHLIST,
};
static const D3D12_CULL_MODE RD_POLYGON_CULL_TO_D3D12_CULL_MODE[RDD::POLYGON_CULL_MAX] = {
D3D12_CULL_MODE_NONE,
D3D12_CULL_MODE_FRONT,
D3D12_CULL_MODE_BACK,
};
static const D3D12_STENCIL_OP RD_TO_D3D12_STENCIL_OP[RDD::STENCIL_OP_MAX] = {
D3D12_STENCIL_OP_KEEP,
D3D12_STENCIL_OP_ZERO,
D3D12_STENCIL_OP_REPLACE,
D3D12_STENCIL_OP_INCR_SAT,
D3D12_STENCIL_OP_DECR_SAT,
D3D12_STENCIL_OP_INVERT,
D3D12_STENCIL_OP_INCR,
D3D12_STENCIL_OP_DECR,
};
static const D3D12_LOGIC_OP RD_TO_D3D12_LOGIC_OP[RDD::LOGIC_OP_MAX] = {
D3D12_LOGIC_OP_CLEAR,
D3D12_LOGIC_OP_AND,
D3D12_LOGIC_OP_AND_REVERSE,
D3D12_LOGIC_OP_COPY,
D3D12_LOGIC_OP_AND_INVERTED,
D3D12_LOGIC_OP_NOOP,
D3D12_LOGIC_OP_XOR,
D3D12_LOGIC_OP_OR,
D3D12_LOGIC_OP_NOR,
D3D12_LOGIC_OP_EQUIV,
D3D12_LOGIC_OP_INVERT,
D3D12_LOGIC_OP_OR_REVERSE,
D3D12_LOGIC_OP_COPY_INVERTED,
D3D12_LOGIC_OP_OR_INVERTED,
D3D12_LOGIC_OP_NAND,
D3D12_LOGIC_OP_SET,
};
static const D3D12_BLEND RD_TO_D3D12_BLEND_FACTOR[RDD::BLEND_FACTOR_MAX] = {
D3D12_BLEND_ZERO,
D3D12_BLEND_ONE,
D3D12_BLEND_SRC_COLOR,
D3D12_BLEND_INV_SRC_COLOR,
D3D12_BLEND_DEST_COLOR,
D3D12_BLEND_INV_DEST_COLOR,
D3D12_BLEND_SRC_ALPHA,
D3D12_BLEND_INV_SRC_ALPHA,
D3D12_BLEND_DEST_ALPHA,
D3D12_BLEND_INV_DEST_ALPHA,
D3D12_BLEND_BLEND_FACTOR,
D3D12_BLEND_INV_BLEND_FACTOR,
D3D12_BLEND_BLEND_FACTOR,
D3D12_BLEND_INV_BLEND_FACTOR,
D3D12_BLEND_SRC_ALPHA_SAT,
D3D12_BLEND_SRC1_COLOR,
D3D12_BLEND_INV_SRC1_COLOR,
D3D12_BLEND_SRC1_ALPHA,
D3D12_BLEND_INV_SRC1_ALPHA,
};
static const D3D12_BLEND_OP RD_TO_D3D12_BLEND_OP[RDD::BLEND_OP_MAX] = {
D3D12_BLEND_OP_ADD,
D3D12_BLEND_OP_SUBTRACT,
D3D12_BLEND_OP_REV_SUBTRACT,
D3D12_BLEND_OP_MIN,
D3D12_BLEND_OP_MAX,
};
RDD::PipelineID RenderingDeviceDriverD3D12::render_pipeline_create(
ShaderID p_shader,
VertexFormatID p_vertex_format,
RenderPrimitive p_render_primitive,
PipelineRasterizationState p_rasterization_state,
PipelineMultisampleState p_multisample_state,
PipelineDepthStencilState p_depth_stencil_state,
PipelineColorBlendState p_blend_state,
VectorView<int32_t> p_color_attachments,
BitField<PipelineDynamicStateFlags> p_dynamic_state,
RenderPassID p_render_pass,
uint32_t p_render_subpass,
VectorView<PipelineSpecializationConstant> p_specialization_constants) {
const ShaderInfo *shader_info_in = (const ShaderInfo *)p_shader.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
CD3DX12_PIPELINE_STATE_STREAM pipeline_desc = {};
const RenderPassInfo *pass_info = (const RenderPassInfo *)p_render_pass.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
RenderPipelineInfo render_info;
// Attachments.
LocalVector<uint32_t> color_attachments;
{
const Subpass &subpass = pass_info->subpasses[p_render_subpass];
for (uint32_t i = 0; i < ARRAY_SIZE((&pipeline_desc.RTVFormats)->RTFormats); i++) {
(&pipeline_desc.RTVFormats)->RTFormats[i] = DXGI_FORMAT_UNKNOWN;
}
for (uint32_t i = 0; i < subpass.color_references.size(); i++) {
const AttachmentReference &ref = subpass.color_references[i];
if (ref.attachment != AttachmentReference::UNUSED) {
const Attachment &attachment = pass_info->attachments[ref.attachment];
DEV_ASSERT((&pipeline_desc.RTVFormats)->RTFormats[i] == DXGI_FORMAT_UNKNOWN);
(&pipeline_desc.RTVFormats)->RTFormats[i] = RD_TO_D3D12_FORMAT[attachment.format].general_format;
}
}
(&pipeline_desc.RTVFormats)->NumRenderTargets = p_color_attachments.size();
if (subpass.depth_stencil_reference.attachment != AttachmentReference::UNUSED) {
const Attachment &attachment = pass_info->attachments[subpass.depth_stencil_reference.attachment];
pipeline_desc.DSVFormat = RD_TO_D3D12_FORMAT[attachment.format].dsv_format;
} else {
pipeline_desc.DSVFormat = DXGI_FORMAT_UNKNOWN;
}
}
// Vertex.
if (p_vertex_format) {
const VertexFormatInfo *vf_info = (const VertexFormatInfo *)p_vertex_format.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
(&pipeline_desc.InputLayout)->pInputElementDescs = vf_info->input_elem_descs.ptr();
(&pipeline_desc.InputLayout)->NumElements = vf_info->input_elem_descs.size();
render_info.vf_info = vf_info;
}
// Input assembly & tessellation.
pipeline_desc.PrimitiveTopologyType = RD_PRIMITIVE_TO_D3D12_TOPOLOGY_TYPE[p_render_primitive];
if (p_render_primitive == RENDER_PRIMITIVE_TESSELATION_PATCH) {
// Is there any way to get the true point count limit?
ERR_FAIL_COND_V(p_rasterization_state.patch_control_points < 1 || p_rasterization_state.patch_control_points > 32, PipelineID());
render_info.dyn_params.primitive_topology = (D3D12_PRIMITIVE_TOPOLOGY)((int)D3D_PRIMITIVE_TOPOLOGY_1_CONTROL_POINT_PATCHLIST + p_rasterization_state.patch_control_points);
} else {
render_info.dyn_params.primitive_topology = RD_PRIMITIVE_TO_D3D12_TOPOLOGY[p_render_primitive];
}
if (p_render_primitive == RENDER_PRIMITIVE_TRIANGLE_STRIPS_WITH_RESTART_INDEX) {
// TODO: This is right for 16-bit indices; for 32-bit there's a different enum value to set, but we don't know at this point.
pipeline_desc.IBStripCutValue = D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFF;
} else {
pipeline_desc.IBStripCutValue = D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_DISABLED;
}
// Rasterization.
(&pipeline_desc.RasterizerState)->DepthClipEnable = !p_rasterization_state.enable_depth_clamp;
// In D3D12, discard can be supported with some extra effort (empty pixel shader + disable depth/stencil test); that said, unsupported by now.
ERR_FAIL_COND_V(p_rasterization_state.discard_primitives, PipelineID());
(&pipeline_desc.RasterizerState)->FillMode = p_rasterization_state.wireframe ? D3D12_FILL_MODE_WIREFRAME : D3D12_FILL_MODE_SOLID;
(&pipeline_desc.RasterizerState)->CullMode = RD_POLYGON_CULL_TO_D3D12_CULL_MODE[p_rasterization_state.cull_mode];
(&pipeline_desc.RasterizerState)->FrontCounterClockwise = p_rasterization_state.front_face == POLYGON_FRONT_FACE_COUNTER_CLOCKWISE;
// In D3D12, there's still a point in setting up depth bias with no depth buffer, but just zeroing (disabling) it all in such case is closer to Vulkan.
if (p_rasterization_state.depth_bias_enabled && pipeline_desc.DSVFormat != DXGI_FORMAT_UNKNOWN) {
(&pipeline_desc.RasterizerState)->DepthBias = p_rasterization_state.depth_bias_constant_factor;<--- (&pipeline_desc.RasterizerState)->DepthBias is assigned
(&pipeline_desc.RasterizerState)->DepthBiasClamp = p_rasterization_state.depth_bias_clamp;<--- (&pipeline_desc.RasterizerState)->DepthBiasClamp is assigned
(&pipeline_desc.RasterizerState)->SlopeScaledDepthBias = p_rasterization_state.depth_bias_slope_factor;<--- (&pipeline_desc.RasterizerState)->SlopeScaledDepthBias is assigned
(&pipeline_desc.RasterizerState)->DepthBias = 0;<--- (&pipeline_desc.RasterizerState)->DepthBias is overwritten
(&pipeline_desc.RasterizerState)->DepthBiasClamp = 0.0f;<--- (&pipeline_desc.RasterizerState)->DepthBiasClamp is overwritten
(&pipeline_desc.RasterizerState)->SlopeScaledDepthBias = 0.0f;<--- (&pipeline_desc.RasterizerState)->SlopeScaledDepthBias is overwritten
}
(&pipeline_desc.RasterizerState)->ForcedSampleCount = 0;
(&pipeline_desc.RasterizerState)->ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
(&pipeline_desc.RasterizerState)->MultisampleEnable = TEXTURE_SAMPLES_COUNT[p_multisample_state.sample_count] != 1;
(&pipeline_desc.RasterizerState)->AntialiasedLineEnable = true;
// In D3D12, there's no line width.
ERR_FAIL_COND_V(!Math::is_equal_approx(p_rasterization_state.line_width, 1.0f), PipelineID());
// Multisample.
ERR_FAIL_COND_V(p_multisample_state.enable_sample_shading, PipelineID()); // How one enables this in D3D12?
if ((&pipeline_desc.RTVFormats)->NumRenderTargets || pipeline_desc.DSVFormat != DXGI_FORMAT_UNKNOWN) {
uint32_t sample_count = MIN(
pass_info->max_supported_sample_count,
TEXTURE_SAMPLES_COUNT[p_multisample_state.sample_count]);
(&pipeline_desc.SampleDesc)->Count = sample_count;
} else {
(&pipeline_desc.SampleDesc)->Count = 1;
}
if ((&pipeline_desc.SampleDesc)->Count > 1) {
(&pipeline_desc.SampleDesc)->Quality = DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;
} else {
(&pipeline_desc.SampleDesc)->Quality = 0;
}
if (p_multisample_state.sample_mask.size()) {
for (int i = 1; i < p_multisample_state.sample_mask.size(); i++) {
// In D3D12 there's a single sample mask for every pixel.
ERR_FAIL_COND_V(p_multisample_state.sample_mask[i] != p_multisample_state.sample_mask[0], PipelineID());
}
pipeline_desc.SampleMask = p_multisample_state.sample_mask[0];
} else {
pipeline_desc.SampleMask = 0xffffffff;
}
// Depth stencil.
if (pipeline_desc.DSVFormat == DXGI_FORMAT_UNKNOWN) {
(&pipeline_desc.DepthStencilState)->DepthEnable = false;
(&pipeline_desc.DepthStencilState)->StencilEnable = false;
} else {
(&pipeline_desc.DepthStencilState)->DepthEnable = p_depth_stencil_state.enable_depth_test;
(&pipeline_desc.DepthStencilState)->DepthWriteMask = p_depth_stencil_state.enable_depth_write ? D3D12_DEPTH_WRITE_MASK_ALL : D3D12_DEPTH_WRITE_MASK_ZERO;
(&pipeline_desc.DepthStencilState)->DepthFunc = RD_TO_D3D12_COMPARE_OP[p_depth_stencil_state.depth_compare_operator];
(&pipeline_desc.DepthStencilState)->DepthBoundsTestEnable = p_depth_stencil_state.enable_depth_range;
(&pipeline_desc.DepthStencilState)->StencilEnable = p_depth_stencil_state.enable_stencil;
// In D3D12 some elements can't be different across front and back.
ERR_FAIL_COND_V(p_depth_stencil_state.front_op.compare_mask != p_depth_stencil_state.back_op.compare_mask, PipelineID());
ERR_FAIL_COND_V(p_depth_stencil_state.front_op.write_mask != p_depth_stencil_state.back_op.write_mask, PipelineID());
ERR_FAIL_COND_V(p_depth_stencil_state.front_op.reference != p_depth_stencil_state.back_op.reference, PipelineID());
(&pipeline_desc.DepthStencilState)->StencilReadMask = p_depth_stencil_state.front_op.compare_mask;
(&pipeline_desc.DepthStencilState)->StencilWriteMask = p_depth_stencil_state.front_op.write_mask;
(&pipeline_desc.DepthStencilState)->FrontFace.StencilFailOp = RD_TO_D3D12_STENCIL_OP[p_depth_stencil_state.front_op.fail];
(&pipeline_desc.DepthStencilState)->FrontFace.StencilPassOp = RD_TO_D3D12_STENCIL_OP[p_depth_stencil_state.front_op.pass];
(&pipeline_desc.DepthStencilState)->FrontFace.StencilDepthFailOp = RD_TO_D3D12_STENCIL_OP[p_depth_stencil_state.front_op.depth_fail];
(&pipeline_desc.DepthStencilState)->FrontFace.StencilFunc = RD_TO_D3D12_COMPARE_OP[p_depth_stencil_state.front_op.compare];
(&pipeline_desc.DepthStencilState)->BackFace.StencilFailOp = RD_TO_D3D12_STENCIL_OP[p_depth_stencil_state.back_op.fail];
(&pipeline_desc.DepthStencilState)->BackFace.StencilPassOp = RD_TO_D3D12_STENCIL_OP[p_depth_stencil_state.back_op.pass];
(&pipeline_desc.DepthStencilState)->BackFace.StencilDepthFailOp = RD_TO_D3D12_STENCIL_OP[p_depth_stencil_state.back_op.depth_fail];
(&pipeline_desc.DepthStencilState)->BackFace.StencilFunc = RD_TO_D3D12_COMPARE_OP[p_depth_stencil_state.back_op.compare];
if (misc_features_support.depth_bounds_supported) {
render_info.dyn_params.depth_bounds_min = p_depth_stencil_state.enable_depth_range ? p_depth_stencil_state.depth_range_min : 0.0f;
render_info.dyn_params.depth_bounds_max = p_depth_stencil_state.enable_depth_range ? p_depth_stencil_state.depth_range_max : 1.0f;
} else {
if (p_depth_stencil_state.enable_depth_range) {
WARN_PRINT_ONCE("Depth bounds test is not supported by the GPU driver.");
}
}
render_info.dyn_params.stencil_reference = p_depth_stencil_state.front_op.reference;
}
// Blend states.
(&pipeline_desc.BlendState)->AlphaToCoverageEnable = p_multisample_state.enable_alpha_to_coverage;
{
bool all_attachments_same_blend = true;
for (int i = 0; i < p_blend_state.attachments.size(); i++) {
const PipelineColorBlendState::Attachment &bs = p_blend_state.attachments[i];
D3D12_RENDER_TARGET_BLEND_DESC &bd = (&pipeline_desc.BlendState)->RenderTarget[i];
bd.BlendEnable = bs.enable_blend;
bd.LogicOpEnable = p_blend_state.enable_logic_op;
bd.LogicOp = RD_TO_D3D12_LOGIC_OP[p_blend_state.logic_op];
bd.SrcBlend = RD_TO_D3D12_BLEND_FACTOR[bs.src_color_blend_factor];
bd.DestBlend = RD_TO_D3D12_BLEND_FACTOR[bs.dst_color_blend_factor];
bd.BlendOp = RD_TO_D3D12_BLEND_OP[bs.color_blend_op];
bd.SrcBlendAlpha = RD_TO_D3D12_BLEND_FACTOR[bs.src_alpha_blend_factor];
bd.DestBlendAlpha = RD_TO_D3D12_BLEND_FACTOR[bs.dst_alpha_blend_factor];
bd.BlendOpAlpha = RD_TO_D3D12_BLEND_OP[bs.alpha_blend_op];
if (bs.write_r) {
bd.RenderTargetWriteMask |= D3D12_COLOR_WRITE_ENABLE_RED;
}
if (bs.write_g) {
bd.RenderTargetWriteMask |= D3D12_COLOR_WRITE_ENABLE_GREEN;
}
if (bs.write_b) {
bd.RenderTargetWriteMask |= D3D12_COLOR_WRITE_ENABLE_BLUE;
}
if (bs.write_a) {
bd.RenderTargetWriteMask |= D3D12_COLOR_WRITE_ENABLE_ALPHA;
}
if (i > 0 && all_attachments_same_blend) {
all_attachments_same_blend = &(&pipeline_desc.BlendState)->RenderTarget[i] == &(&pipeline_desc.BlendState)->RenderTarget[0];
}
}
// Per D3D12 docs, if logic op used, independent blending is not supported.
ERR_FAIL_COND_V(p_blend_state.enable_logic_op && !all_attachments_same_blend, PipelineID());
(&pipeline_desc.BlendState)->IndependentBlendEnable = !all_attachments_same_blend;
}
render_info.dyn_params.blend_constant = p_blend_state.blend_constant;
// Stages bytecodes + specialization constants.
pipeline_desc.pRootSignature = shader_info_in->root_signature.Get();
HashMap<ShaderStage, Vector<uint8_t>> final_stages_bytecode;
bool ok = _shader_apply_specialization_constants(shader_info_in, p_specialization_constants, final_stages_bytecode);
ERR_FAIL_COND_V(!ok, PipelineID());
pipeline_desc.VS = D3D12_SHADER_BYTECODE{
final_stages_bytecode[SHADER_STAGE_VERTEX].ptr(),
(SIZE_T)final_stages_bytecode[SHADER_STAGE_VERTEX].size()
};
pipeline_desc.PS = D3D12_SHADER_BYTECODE{
final_stages_bytecode[SHADER_STAGE_FRAGMENT].ptr(),
(SIZE_T)final_stages_bytecode[SHADER_STAGE_FRAGMENT].size()
};
ComPtr<ID3D12Device2> device_2;
device->QueryInterface(device_2.GetAddressOf());
ID3D12PipelineState *pso = nullptr;
HRESULT res = E_FAIL;
if (device_2) {
D3D12_PIPELINE_STATE_STREAM_DESC pssd = {};
pssd.pPipelineStateSubobjectStream = &pipeline_desc;
pssd.SizeInBytes = sizeof(pipeline_desc);
res = device_2->CreatePipelineState(&pssd, IID_PPV_ARGS(&pso));
} else {
D3D12_GRAPHICS_PIPELINE_STATE_DESC desc = pipeline_desc.GraphicsDescV0();
res = device->CreateGraphicsPipelineState(&desc, IID_PPV_ARGS(&pso));
}
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), PipelineID(), "Create(Graphics)PipelineState failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
PipelineInfo *pipeline_info = memnew(PipelineInfo);
pipeline_info->pso = pso;
pipeline_info->shader_info = shader_info_in;
pipeline_info->render_info = render_info;
return PipelineID(pipeline_info);
}
/*****************/
/**** COMPUTE ****/
/*****************/
// ----- COMMANDS -----
void RenderingDeviceDriverD3D12::command_bind_compute_pipeline(CommandBufferID p_cmd_buffer, PipelineID p_pipeline) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
const PipelineInfo *pipeline_info = (const PipelineInfo *)p_pipeline.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (cmd_buf_info->compute_pso == pipeline_info->pso) {
return;
}
const ShaderInfo *shader_info_in = pipeline_info->shader_info;
cmd_buf_info->cmd_list->SetPipelineState(pipeline_info->pso);
if (cmd_buf_info->compute_root_signature_crc != shader_info_in->root_signature_crc) {
cmd_buf_info->cmd_list->SetComputeRootSignature(shader_info_in->root_signature.Get());
cmd_buf_info->compute_root_signature_crc = shader_info_in->root_signature_crc;
}
cmd_buf_info->compute_pso = pipeline_info->pso;
cmd_buf_info->graphics_pso = nullptr;
}
void RenderingDeviceDriverD3D12::command_bind_compute_uniform_set(CommandBufferID p_cmd_buffer, UniformSetID p_uniform_set, ShaderID p_shader, uint32_t p_set_index) {
_command_bind_uniform_set(p_cmd_buffer, p_uniform_set, p_shader, p_set_index, true);
}
void RenderingDeviceDriverD3D12::command_bind_compute_uniform_sets(CommandBufferID p_cmd_buffer, VectorView<UniformSetID> p_uniform_sets, ShaderID p_shader, uint32_t p_first_set_index, uint32_t p_set_count) {
for (uint32_t i = 0u; i < p_set_count; ++i) {
// TODO: _command_bind_uniform_set() does WAAAAY too much stuff. A lot of it should be already cached in UniformSetID when uniform_set_create() was called. Binding is supposed to be a cheap operation, ideally a memcpy.
_command_bind_uniform_set(p_cmd_buffer, p_uniform_sets[i], p_shader, p_first_set_index + i, true);
}
}
void RenderingDeviceDriverD3D12::command_compute_dispatch(CommandBufferID p_cmd_buffer, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transitions_flush(cmd_buf_info);
}
cmd_buf_info->cmd_list->Dispatch(p_x_groups, p_y_groups, p_z_groups);
}
void RenderingDeviceDriverD3D12::command_compute_dispatch_indirect(CommandBufferID p_cmd_buffer, BufferID p_indirect_buffer, uint64_t p_offset) {
CommandBufferInfo *cmd_buf_info = (CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
BufferInfo *indirect_buf_info = (BufferInfo *)p_indirect_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (!barrier_capabilities.enhanced_barriers_supported) {
_resource_transition_batch(cmd_buf_info, indirect_buf_info, 0, 1, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
_resource_transitions_flush(cmd_buf_info);
}
cmd_buf_info->cmd_list->ExecuteIndirect(indirect_cmd_signatures.dispatch.Get(), 1, indirect_buf_info->resource, p_offset, nullptr, 0);
}
// ----- PIPELINE -----
RDD::PipelineID RenderingDeviceDriverD3D12::compute_pipeline_create(ShaderID p_shader, VectorView<PipelineSpecializationConstant> p_specialization_constants) {
const ShaderInfo *shader_info_in = (const ShaderInfo *)p_shader.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
CD3DX12_PIPELINE_STATE_STREAM pipeline_desc = {};
// Stages bytecodes + specialization constants.
pipeline_desc.pRootSignature = shader_info_in->root_signature.Get();
HashMap<ShaderStage, Vector<uint8_t>> final_stages_bytecode;
bool ok = _shader_apply_specialization_constants(shader_info_in, p_specialization_constants, final_stages_bytecode);
ERR_FAIL_COND_V(!ok, PipelineID());
pipeline_desc.CS = D3D12_SHADER_BYTECODE{
final_stages_bytecode[SHADER_STAGE_COMPUTE].ptr(),
(SIZE_T)final_stages_bytecode[SHADER_STAGE_COMPUTE].size()
};
ComPtr<ID3D12Device2> device_2;
device->QueryInterface(device_2.GetAddressOf());
ID3D12PipelineState *pso = nullptr;
HRESULT res = E_FAIL;
if (device_2) {
D3D12_PIPELINE_STATE_STREAM_DESC pssd = {};
pssd.pPipelineStateSubobjectStream = &pipeline_desc;
pssd.SizeInBytes = sizeof(pipeline_desc);
res = device_2->CreatePipelineState(&pssd, IID_PPV_ARGS(&pso));
} else {
D3D12_COMPUTE_PIPELINE_STATE_DESC desc = pipeline_desc.ComputeDescV0();
res = device->CreateComputePipelineState(&desc, IID_PPV_ARGS(&pso));
}
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), PipelineID(), "Create(Compute)PipelineState failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
PipelineInfo *pipeline_info = memnew(PipelineInfo);
pipeline_info->pso = pso;
pipeline_info->shader_info = shader_info_in;
return PipelineID(pipeline_info);
}
/*****************/
/**** QUERIES ****/
/*****************/
// ----- TIMESTAMP -----
RDD::QueryPoolID RenderingDeviceDriverD3D12::timestamp_query_pool_create(uint32_t p_query_count) {
ComPtr<ID3D12QueryHeap> query_heap;
{
D3D12_QUERY_HEAP_DESC qh_desc = {};
qh_desc.Type = D3D12_QUERY_HEAP_TYPE_TIMESTAMP;
qh_desc.Count = p_query_count;
qh_desc.NodeMask = 0;
HRESULT res = device->CreateQueryHeap(&qh_desc, IID_PPV_ARGS(query_heap.GetAddressOf()));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), QueryPoolID(), "CreateQueryHeap failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
}
ComPtr<D3D12MA::Allocation> results_buffer_allocation;
{
D3D12MA::ALLOCATION_DESC allocation_desc = {};
allocation_desc.HeapType = D3D12_HEAP_TYPE_READBACK;
CD3DX12_RESOURCE_DESC resource_desc = CD3DX12_RESOURCE_DESC::Buffer(sizeof(uint64_t) * p_query_count);
ComPtr<ID3D12Resource> results_buffer;
HRESULT res = allocator->CreateResource(
&allocation_desc,
&resource_desc,
D3D12_RESOURCE_STATE_COPY_DEST,
nullptr,
results_buffer_allocation.GetAddressOf(),
IID_PPV_ARGS(results_buffer.GetAddressOf()));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), QueryPoolID(), "D3D12MA::CreateResource failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
}
// Bookkeep.
TimestampQueryPoolInfo *tqp_info = VersatileResource::allocate<TimestampQueryPoolInfo>(resources_allocator);
tqp_info->query_heap = query_heap;
tqp_info->query_count = p_query_count;
tqp_info->results_buffer_allocation = results_buffer_allocation;
return RDD::QueryPoolID(tqp_info);
}
void RenderingDeviceDriverD3D12::timestamp_query_pool_free(QueryPoolID p_pool_id) {
TimestampQueryPoolInfo *tqp_info = (TimestampQueryPoolInfo *)p_pool_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
VersatileResource::free(resources_allocator, tqp_info);
}
void RenderingDeviceDriverD3D12::timestamp_query_pool_get_results(QueryPoolID p_pool_id, uint32_t p_query_count, uint64_t *r_results) {
TimestampQueryPoolInfo *tqp_info = (TimestampQueryPoolInfo *)p_pool_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
ID3D12Resource *results_buffer = tqp_info->results_buffer_allocation->GetResource();
void *results_buffer_data = nullptr;
results_buffer->Map(0, &VOID_RANGE, &results_buffer_data);
memcpy(r_results, results_buffer_data, sizeof(uint64_t) * p_query_count);
results_buffer->Unmap(0, &VOID_RANGE);
}
uint64_t RenderingDeviceDriverD3D12::timestamp_query_result_to_time(uint64_t p_result) {
return p_result / (double)device_limits.timestamp_frequency * 1000000000.0;
}
void RenderingDeviceDriverD3D12::command_timestamp_query_pool_reset(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_query_count) {
}
void RenderingDeviceDriverD3D12::command_timestamp_write(CommandBufferID p_cmd_buffer, QueryPoolID p_pool_id, uint32_t p_index) {
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
TimestampQueryPoolInfo *tqp_info = (TimestampQueryPoolInfo *)p_pool_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
ID3D12Resource *results_buffer = tqp_info->results_buffer_allocation->GetResource();
cmd_buf_info->cmd_list->EndQuery(tqp_info->query_heap.Get(), D3D12_QUERY_TYPE_TIMESTAMP, p_index);
cmd_buf_info->cmd_list->ResolveQueryData(tqp_info->query_heap.Get(), D3D12_QUERY_TYPE_TIMESTAMP, p_index, 1, results_buffer, p_index * sizeof(uint64_t));
}
void RenderingDeviceDriverD3D12::command_begin_label(CommandBufferID p_cmd_buffer, const char *p_label_name, const Color &p_color) {
#ifdef PIX_ENABLED
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
PIXBeginEvent(cmd_buf_info->cmd_list.Get(), p_color.to_argb32(), p_label_name);
#endif
}
void RenderingDeviceDriverD3D12::command_end_label(CommandBufferID p_cmd_buffer) {
#ifdef PIX_ENABLED
const CommandBufferInfo *cmd_buf_info = (const CommandBufferInfo *)p_cmd_buffer.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
PIXEndEvent(cmd_buf_info->cmd_list.Get());
#endif
}
void RenderingDeviceDriverD3D12::command_insert_breadcrumb(CommandBufferID p_cmd_buffer, uint32_t p_data) {
// TODO: Implement via DRED.
}
/********************/
/**** SUBMISSION ****/
/********************/
void RenderingDeviceDriverD3D12::begin_segment(uint32_t p_frame_index, uint32_t p_frames_drawn) {
frame_idx = p_frame_index;
frames_drawn = p_frames_drawn;
allocator->SetCurrentFrameIndex(p_frames_drawn);
frames[frame_idx].desc_heap_walkers.resources.rewind();
frames[frame_idx].desc_heap_walkers.samplers.rewind();
frames[frame_idx].desc_heap_walkers.aux.rewind();
frames[frame_idx].desc_heap_walkers.rtv.rewind();
frames[frame_idx].desc_heaps_exhausted_reported = {};
frames[frame_idx].null_rtv_handle = CD3DX12_CPU_DESCRIPTOR_HANDLE{};
frames[frame_idx].segment_serial = segment_serial;
segment_begun = true;
}
void RenderingDeviceDriverD3D12::end_segment() {
segment_serial++;
segment_begun = false;
}
/**************/
/**** MISC ****/
/**************/
void RenderingDeviceDriverD3D12::_set_object_name(ID3D12Object *p_object, String p_object_name) {
ERR_FAIL_NULL(p_object);
int name_len = p_object_name.size();
WCHAR *name_w = (WCHAR *)alloca(sizeof(WCHAR) * (name_len + 1));<--- Obsolete function 'alloca' called. [+]The obsolete function 'alloca' is called. In C++11 and later it is recommended to use std::array<> or a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons (http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca).
MultiByteToWideChar(CP_UTF8, 0, p_object_name.utf8().get_data(), -1, name_w, name_len);
p_object->SetName(name_w);
}
void RenderingDeviceDriverD3D12::set_object_name(ObjectType p_type, ID p_driver_id, const String &p_name) {
switch (p_type) {
case OBJECT_TYPE_TEXTURE: {
const TextureInfo *tex_info = (const TextureInfo *)p_driver_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (tex_info->owner_info.allocation) {
_set_object_name(tex_info->resource, p_name);
}
} break;
case OBJECT_TYPE_SAMPLER: {
} break;
case OBJECT_TYPE_BUFFER: {
const BufferInfo *buf_info = (const BufferInfo *)p_driver_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_set_object_name(buf_info->resource, p_name);
} break;
case OBJECT_TYPE_SHADER: {
const ShaderInfo *shader_info_in = (const ShaderInfo *)p_driver_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_set_object_name(shader_info_in->root_signature.Get(), p_name);
} break;
case OBJECT_TYPE_UNIFORM_SET: {
const UniformSetInfo *uniform_set_info = (const UniformSetInfo *)p_driver_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
if (uniform_set_info->desc_heaps.resources.get_heap()) {
_set_object_name(uniform_set_info->desc_heaps.resources.get_heap(), p_name + " resources heap");
}
if (uniform_set_info->desc_heaps.samplers.get_heap()) {
_set_object_name(uniform_set_info->desc_heaps.samplers.get_heap(), p_name + " samplers heap");
}
} break;
case OBJECT_TYPE_PIPELINE: {
const PipelineInfo *pipeline_info = (const PipelineInfo *)p_driver_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
_set_object_name(pipeline_info->pso, p_name);
} break;
default: {
DEV_ASSERT(false);
}
}
}
uint64_t RenderingDeviceDriverD3D12::get_resource_native_handle(DriverResource p_type, ID p_driver_id) {
switch (p_type) {
case DRIVER_RESOURCE_LOGICAL_DEVICE: {
return (uint64_t)device.Get();
}
case DRIVER_RESOURCE_PHYSICAL_DEVICE: {
return (uint64_t)adapter.Get();
}
case DRIVER_RESOURCE_TOPMOST_OBJECT: {
return 0;
}
case DRIVER_RESOURCE_COMMAND_QUEUE: {
return (uint64_t)p_driver_id.id;
}
case DRIVER_RESOURCE_QUEUE_FAMILY: {
return 0;
}
case DRIVER_RESOURCE_TEXTURE: {
const TextureInfo *tex_info = (const TextureInfo *)p_driver_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
return (uint64_t)tex_info->main_texture;
} break;
case DRIVER_RESOURCE_TEXTURE_VIEW: {
const TextureInfo *tex_info = (const TextureInfo *)p_driver_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
return (uint64_t)tex_info->resource;
}
case DRIVER_RESOURCE_TEXTURE_DATA_FORMAT: {
const TextureInfo *tex_info = (const TextureInfo *)p_driver_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
return (uint64_t)tex_info->desc.Format;
}
case DRIVER_RESOURCE_SAMPLER:
case DRIVER_RESOURCE_UNIFORM_SET:
return 0;
case DRIVER_RESOURCE_BUFFER: {
const TextureInfo *tex_info = (const TextureInfo *)p_driver_id.id;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts.
return (uint64_t)tex_info->resource;
} break;
case DRIVER_RESOURCE_COMPUTE_PIPELINE:
case DRIVER_RESOURCE_RENDER_PIPELINE: {
return p_driver_id.id;
}
default: {
return 0;
}
}
}
uint64_t RenderingDeviceDriverD3D12::get_total_memory_used() {
D3D12MA::TotalStatistics stats;
allocator->CalculateStatistics(&stats);
return stats.Total.Stats.BlockBytes;
}
uint64_t RenderingDeviceDriverD3D12::get_lazily_memory_used() {
return 0;
}
uint64_t RenderingDeviceDriverD3D12::limit_get(Limit p_limit) {
uint64_t safe_unbounded = ((uint64_t)1 << 30);
switch (p_limit) {
case LIMIT_MAX_BOUND_UNIFORM_SETS:
return safe_unbounded;
case LIMIT_MAX_TEXTURE_ARRAY_LAYERS:
return D3D12_REQ_TEXTURE2D_ARRAY_AXIS_DIMENSION;
case LIMIT_MAX_TEXTURE_SIZE_1D:
return D3D12_REQ_TEXTURE1D_U_DIMENSION;
case LIMIT_MAX_TEXTURE_SIZE_2D:
return D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION;
case LIMIT_MAX_TEXTURE_SIZE_3D:
return D3D12_REQ_TEXTURE3D_U_V_OR_W_DIMENSION;
case LIMIT_MAX_TEXTURE_SIZE_CUBE:
return D3D12_REQ_TEXTURECUBE_DIMENSION;
case LIMIT_MAX_TEXTURES_PER_SHADER_STAGE:
return device_limits.max_srvs_per_shader_stage;
case LIMIT_MAX_UNIFORM_BUFFER_SIZE:
return 65536;
case LIMIT_MAX_VIEWPORT_DIMENSIONS_X:
case LIMIT_MAX_VIEWPORT_DIMENSIONS_Y:
return 16384; // Based on max. texture size. Maybe not correct.
case LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_X:
return D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION;
case LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Y:
return D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION;
case LIMIT_MAX_COMPUTE_WORKGROUP_COUNT_Z:
return D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION;
case LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_X:
return D3D12_CS_THREAD_GROUP_MAX_X;
case LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_Y:
return D3D12_CS_THREAD_GROUP_MAX_Y;
case LIMIT_MAX_COMPUTE_WORKGROUP_SIZE_Z:
return D3D12_CS_THREAD_GROUP_MAX_Z;
case LIMIT_SUBGROUP_SIZE:
// Note in min/max. Shader model 6.6 supports it (see https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_WaveSize.html),
// but at this time I don't know the implications on the transpilation to DXIL, etc.
case LIMIT_SUBGROUP_MIN_SIZE:
case LIMIT_SUBGROUP_MAX_SIZE:
return subgroup_capabilities.size;
case LIMIT_SUBGROUP_IN_SHADERS:
return subgroup_capabilities.supported_stages_flags_rd();
case LIMIT_SUBGROUP_OPERATIONS:
return subgroup_capabilities.supported_operations_flags_rd();
case LIMIT_VRS_TEXEL_WIDTH:
case LIMIT_VRS_TEXEL_HEIGHT:
return vrs_capabilities.ss_image_tile_size;
case LIMIT_VRS_MAX_FRAGMENT_WIDTH:
case LIMIT_VRS_MAX_FRAGMENT_HEIGHT:
return vrs_capabilities.ss_max_fragment_size;
default: {
#ifdef DEV_ENABLED
WARN_PRINT("Returning maximum value for unknown limit " + itos(p_limit) + ".");
#endif
return safe_unbounded;
}
}
}
uint64_t RenderingDeviceDriverD3D12::api_trait_get(ApiTrait p_trait) {
switch (p_trait) {
case API_TRAIT_HONORS_PIPELINE_BARRIERS:
return barrier_capabilities.enhanced_barriers_supported;
case API_TRAIT_SHADER_CHANGE_INVALIDATION:
return (uint64_t)SHADER_CHANGE_INVALIDATION_ALL_OR_NONE_ACCORDING_TO_LAYOUT_HASH;
case API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT:
return D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT;
case API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP:
return D3D12_TEXTURE_DATA_PITCH_ALIGNMENT;
case API_TRAIT_SECONDARY_VIEWPORT_SCISSOR:
return false;
case API_TRAIT_CLEARS_WITH_COPY_ENGINE:
return false;
case API_TRAIT_USE_GENERAL_IN_COPY_QUEUES:
return true;
case API_TRAIT_BUFFERS_REQUIRE_TRANSITIONS:
return !barrier_capabilities.enhanced_barriers_supported;
default:
return RenderingDeviceDriver::api_trait_get(p_trait);
}
}
bool RenderingDeviceDriverD3D12::has_feature(Features p_feature) {
switch (p_feature) {
case SUPPORTS_MULTIVIEW:
return multiview_capabilities.is_supported && multiview_capabilities.max_view_count > 1;
case SUPPORTS_FSR_HALF_FLOAT:
return shader_capabilities.native_16bit_ops && storage_buffer_capabilities.storage_buffer_16_bit_access_is_supported;
case SUPPORTS_ATTACHMENT_VRS:
return vrs_capabilities.ss_image_supported;
case SUPPORTS_FRAGMENT_SHADER_WITH_ONLY_SIDE_EFFECTS:
return true;
default:
return false;
}
}
const RDD::MultiviewCapabilities &RenderingDeviceDriverD3D12::get_multiview_capabilities() {
return multiview_capabilities;
}
String RenderingDeviceDriverD3D12::get_api_name() const {
return "D3D12";
}
String RenderingDeviceDriverD3D12::get_api_version() const {
return vformat("%d_%d", feature_level / 10, feature_level % 10);
}
String RenderingDeviceDriverD3D12::get_pipeline_cache_uuid() const {
return pipeline_cache_id;
}
const RDD::Capabilities &RenderingDeviceDriverD3D12::get_capabilities() const {
return device_capabilities;
}
bool RenderingDeviceDriverD3D12::is_composite_alpha_supported(CommandQueueID p_queue) const {
if (has_comp_alpha.has((uint64_t)p_queue.id)) {
return has_comp_alpha[(uint64_t)p_queue.id];
}
return false;
}
/******************/
RenderingDeviceDriverD3D12::RenderingDeviceDriverD3D12(RenderingContextDriverD3D12 *p_context_driver) {
DEV_ASSERT(p_context_driver != nullptr);
this->context_driver = p_context_driver;
}
RenderingDeviceDriverD3D12::~RenderingDeviceDriverD3D12() {
glsl_type_singleton_decref();
}
bool RenderingDeviceDriverD3D12::is_in_developer_mode() {
HKEY hkey = nullptr;
LSTATUS result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AppModelUnlock", 0, KEY_READ, &hkey);
if (result != ERROR_SUCCESS) {
return false;
}
DWORD value = 0;
DWORD dword_size = sizeof(DWORD);
result = RegQueryValueExW(hkey, L"AllowDevelopmentWithoutDevLicense", nullptr, nullptr, (PBYTE)&value, &dword_size);
RegCloseKey(hkey);
if (result != ERROR_SUCCESS) {
return false;
}
return (value != 0);
}
Error RenderingDeviceDriverD3D12::_initialize_device() {
HRESULT res;
if (is_in_developer_mode()) {
typedef HRESULT(WINAPI * PFN_D3D12_ENABLE_EXPERIMENTAL_FEATURES)(_In_ UINT, _In_count_(NumFeatures) const IID *, _In_opt_count_(NumFeatures) void *, _In_opt_count_(NumFeatures) UINT *);
PFN_D3D12_ENABLE_EXPERIMENTAL_FEATURES d3d_D3D12EnableExperimentalFeatures = (PFN_D3D12_ENABLE_EXPERIMENTAL_FEATURES)(void *)GetProcAddress(context_driver->lib_d3d12, "D3D12EnableExperimentalFeatures");
ERR_FAIL_NULL_V(d3d_D3D12EnableExperimentalFeatures, ERR_CANT_CREATE);
UUID experimental_features[] = { D3D12ExperimentalShaderModels };
d3d_D3D12EnableExperimentalFeatures(1, experimental_features, nullptr, nullptr);
}
ID3D12DeviceFactory *device_factory = context_driver->device_factory_get();
if (device_factory != nullptr) {
res = device_factory->CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(device.GetAddressOf()));
} else {
PFN_D3D12_CREATE_DEVICE d3d_D3D12CreateDevice = (PFN_D3D12_CREATE_DEVICE)(void *)GetProcAddress(context_driver->lib_d3d12, "D3D12CreateDevice");
ERR_FAIL_NULL_V(d3d_D3D12CreateDevice, ERR_CANT_CREATE);
res = d3d_D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(device.GetAddressOf()));
}
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), ERR_CANT_CREATE, "D3D12CreateDevice failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
if (context_driver->use_validation_layers()) {
ComPtr<ID3D12InfoQueue> info_queue;
res = device.As(&info_queue);
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
#if CUSTOM_INFO_QUEUE_ENABLED
ComPtr<ID3D12InfoQueue1> info_queue_1;
device.As(&info_queue_1);
if (info_queue_1) {
// Custom printing supported (added in Windows 10 Release Preview build 20236). Even if the callback cookie is unused, it seems the
// argument is not optional and the function will fail if it's not specified.
DWORD callback_cookie;
info_queue_1->SetMuteDebugOutput(TRUE);
res = info_queue_1->RegisterMessageCallback(&_debug_message_func, D3D12_MESSAGE_CALLBACK_IGNORE_FILTERS, nullptr, &callback_cookie);
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
} else
#endif
{
// Rely on D3D12's own debug printing.
if (Engine::get_singleton()->is_abort_on_gpu_errors_enabled()) {
res = info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, TRUE);
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
res = info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
res = info_queue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
}
}
D3D12_MESSAGE_SEVERITY severities_to_mute[] = {
D3D12_MESSAGE_SEVERITY_INFO,
};
D3D12_MESSAGE_ID messages_to_mute[] = {
D3D12_MESSAGE_ID_CLEARRENDERTARGETVIEW_MISMATCHINGCLEARVALUE,
D3D12_MESSAGE_ID_CLEARDEPTHSTENCILVIEW_MISMATCHINGCLEARVALUE,
// These happen due to how D3D12MA manages buffers; seems benign.
D3D12_MESSAGE_ID_HEAP_ADDRESS_RANGE_HAS_NO_RESOURCE,
D3D12_MESSAGE_ID_HEAP_ADDRESS_RANGE_INTERSECTS_MULTIPLE_BUFFERS,
// Seemingly a false positive.
D3D12_MESSAGE_ID_DATA_STATIC_WHILE_SET_AT_EXECUTE_DESCRIPTOR_INVALID_DATA_CHANGE,
};
D3D12_INFO_QUEUE_FILTER filter = {};
filter.DenyList.NumSeverities = ARRAY_SIZE(severities_to_mute);
filter.DenyList.pSeverityList = severities_to_mute;
filter.DenyList.NumIDs = ARRAY_SIZE(messages_to_mute);
filter.DenyList.pIDList = messages_to_mute;
res = info_queue->PushStorageFilter(&filter);
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
}
return OK;
}
Error RenderingDeviceDriverD3D12::_check_capabilities() {
// Check feature levels.
const D3D_FEATURE_LEVEL FEATURE_LEVELS[] = {
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_12_0,
D3D_FEATURE_LEVEL_12_1,
D3D_FEATURE_LEVEL_12_2,
};
D3D12_FEATURE_DATA_FEATURE_LEVELS feat_levels = {};
feat_levels.NumFeatureLevels = ARRAY_SIZE(FEATURE_LEVELS);
feat_levels.pFeatureLevelsRequested = FEATURE_LEVELS;
HRESULT res = device->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS, &feat_levels, sizeof(feat_levels));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), ERR_UNAVAILABLE, "CheckFeatureSupport failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
// Example: D3D_FEATURE_LEVEL_12_1 = 0xc100.
uint32_t feat_level_major = feat_levels.MaxSupportedFeatureLevel >> 12;
uint32_t feat_level_minor = (feat_levels.MaxSupportedFeatureLevel >> 16) & 0xff;
feature_level = feat_level_major * 10 + feat_level_minor;
// Fill device capabilities.
device_capabilities.device_family = DEVICE_DIRECTX;
device_capabilities.version_major = feature_level / 10;
device_capabilities.version_minor = feature_level % 10;
// Assume not supported until proven otherwise.
vrs_capabilities.draw_call_supported = false;
vrs_capabilities.primitive_supported = false;
vrs_capabilities.primitive_in_multiviewport = false;
vrs_capabilities.ss_image_supported = false;
vrs_capabilities.ss_image_tile_size = 1;
vrs_capabilities.additional_rates_supported = false;
multiview_capabilities.is_supported = false;<--- multiview_capabilities.is_supported is assigned
multiview_capabilities.geometry_shader_is_supported = false;
multiview_capabilities.tessellation_shader_is_supported = false;
multiview_capabilities.max_view_count = 0;
multiview_capabilities.max_instance_count = 0;
multiview_capabilities.is_supported = false;<--- multiview_capabilities.is_supported is overwritten
subgroup_capabilities.size = 0;
subgroup_capabilities.wave_ops_supported = false;
shader_capabilities.shader_model = (D3D_SHADER_MODEL)0;
shader_capabilities.native_16bit_ops = false;
storage_buffer_capabilities.storage_buffer_16_bit_access_is_supported = false;
format_capabilities.relaxed_casting_supported = false;
{
static const D3D_SHADER_MODEL SMS_TO_CHECK[] = {
D3D_SHADER_MODEL_6_6,
D3D_SHADER_MODEL_6_5,
D3D_SHADER_MODEL_6_4,
D3D_SHADER_MODEL_6_3,
D3D_SHADER_MODEL_6_2,
D3D_SHADER_MODEL_6_1,
D3D_SHADER_MODEL_6_0, // Determined by NIR (dxil_min_shader_model).
};
D3D12_FEATURE_DATA_SHADER_MODEL shader_model = {};
for (uint32_t i = 0; i < ARRAY_SIZE(SMS_TO_CHECK); i++) {
shader_model.HighestShaderModel = SMS_TO_CHECK[i];
res = device->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &shader_model, sizeof(shader_model));
if (SUCCEEDED(res)) {
shader_capabilities.shader_model = shader_model.HighestShaderModel;
break;
}
if (res == E_INVALIDARG) {
continue; // Must assume the device doesn't know about the SM just checked.
}
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), ERR_CANT_CREATE, "CheckFeatureSupport failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
}
#define D3D_SHADER_MODEL_TO_STRING(m_sm) vformat("%d.%d", (m_sm >> 4), (m_sm & 0xf))
ERR_FAIL_COND_V_MSG(!shader_capabilities.shader_model, ERR_UNAVAILABLE,
vformat("No support for any of the suitable shader models (%s-%s) has been found.", D3D_SHADER_MODEL_TO_STRING(SMS_TO_CHECK[ARRAY_SIZE(SMS_TO_CHECK) - 1]), D3D_SHADER_MODEL_TO_STRING(SMS_TO_CHECK[0])));
print_verbose("- Shader:");
print_verbose(" model: " + D3D_SHADER_MODEL_TO_STRING(shader_capabilities.shader_model));
}
D3D12_FEATURE_DATA_D3D12_OPTIONS options = {};
res = device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options));
if (SUCCEEDED(res)) {
storage_buffer_capabilities.storage_buffer_16_bit_access_is_supported = options.TypedUAVLoadAdditionalFormats;
}
D3D12_FEATURE_DATA_D3D12_OPTIONS1 options1 = {};
res = device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &options1, sizeof(options1));
if (SUCCEEDED(res)) {
subgroup_capabilities.size = options1.WaveLaneCountMin;
subgroup_capabilities.wave_ops_supported = options1.WaveOps;
}
D3D12_FEATURE_DATA_D3D12_OPTIONS2 options2 = {};
res = device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &options2, sizeof(options2));
if (SUCCEEDED(res)) {
misc_features_support.depth_bounds_supported = options2.DepthBoundsTestSupported;
}
D3D12_FEATURE_DATA_D3D12_OPTIONS3 options3 = {};
res = device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS3, &options3, sizeof(options3));
if (SUCCEEDED(res)) {
// https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_view_instancing_tier
// https://microsoft.github.io/DirectX-Specs/d3d/ViewInstancing.html#sv_viewid
if (options3.ViewInstancingTier >= D3D12_VIEW_INSTANCING_TIER_1) {
multiview_capabilities.is_supported = true;
multiview_capabilities.geometry_shader_is_supported = options3.ViewInstancingTier >= D3D12_VIEW_INSTANCING_TIER_3;
multiview_capabilities.tessellation_shader_is_supported = options3.ViewInstancingTier >= D3D12_VIEW_INSTANCING_TIER_3;
multiview_capabilities.max_view_count = D3D12_MAX_VIEW_INSTANCE_COUNT;
multiview_capabilities.max_instance_count = UINT32_MAX;
}
}
D3D12_FEATURE_DATA_D3D12_OPTIONS4 options4 = {};
res = device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS4, &options4, sizeof(options4));
if (SUCCEEDED(res)) {
shader_capabilities.native_16bit_ops = options4.Native16BitShaderOpsSupported;
}
D3D12_FEATURE_DATA_D3D12_OPTIONS6 options6 = {};
res = device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS6, &options6, sizeof(options6));
if (SUCCEEDED(res)) {
if (options6.VariableShadingRateTier >= D3D12_VARIABLE_SHADING_RATE_TIER_1) {
vrs_capabilities.draw_call_supported = true;
if (options6.VariableShadingRateTier >= D3D12_VARIABLE_SHADING_RATE_TIER_2) {
vrs_capabilities.primitive_supported = true;
vrs_capabilities.primitive_in_multiviewport = options6.PerPrimitiveShadingRateSupportedWithViewportIndexing;
vrs_capabilities.ss_image_supported = true;
vrs_capabilities.ss_image_tile_size = options6.ShadingRateImageTileSize;
vrs_capabilities.ss_max_fragment_size = 8; // TODO figure out if this is supplied and/or needed
vrs_capabilities.additional_rates_supported = options6.AdditionalShadingRatesSupported;
}
}
}
D3D12_FEATURE_DATA_D3D12_OPTIONS12 options12 = {};
res = device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS12, &options12, sizeof(options12));
if (SUCCEEDED(res)) {
format_capabilities.relaxed_casting_supported = options12.RelaxedFormatCastingSupported;
barrier_capabilities.enhanced_barriers_supported = options12.EnhancedBarriersSupported;
}
if (vrs_capabilities.draw_call_supported || vrs_capabilities.primitive_supported || vrs_capabilities.ss_image_supported) {
print_verbose("- D3D12 Variable Rate Shading supported:");
if (vrs_capabilities.draw_call_supported) {
print_verbose(" Draw call");
}
if (vrs_capabilities.primitive_supported) {
print_verbose(String(" Per-primitive (multi-viewport: ") + (vrs_capabilities.primitive_in_multiviewport ? "yes" : "no") + ")");
}
if (vrs_capabilities.ss_image_supported) {
print_verbose(String(" Screen-space image (tile size: ") + itos(vrs_capabilities.ss_image_tile_size) + ")");
}
if (vrs_capabilities.additional_rates_supported) {
print_verbose(String(" Additional rates: ") + (vrs_capabilities.additional_rates_supported ? "yes" : "no"));
}
} else {
print_verbose("- D3D12 Variable Rate Shading not supported");
}
if (multiview_capabilities.is_supported) {
print_verbose("- D3D12 multiview supported:");
print_verbose(" max view count: " + itos(multiview_capabilities.max_view_count));
//print_verbose(" max instances: " + itos(multiview_capabilities.max_instance_count)); // Hardcoded; not very useful at the moment.
} else {
print_verbose("- D3D12 multiview not supported");
}
if (format_capabilities.relaxed_casting_supported) {
#if 0
print_verbose("- Relaxed casting supported");
#else
// Certain configurations (Windows 11 with an updated NVIDIA driver) crash when using relaxed casting.
// Therefore, we disable it temporarily until we can assure that it's reliable.
// There are fallbacks in place that work in every case, if less efficient.
format_capabilities.relaxed_casting_supported = false;
print_verbose("- Relaxed casting supported (but disabled for now)");
#endif
} else {
print_verbose("- Relaxed casting not supported");
}
print_verbose(String("- D3D12 16-bit ops supported: ") + (shader_capabilities.native_16bit_ops ? "yes" : "no"));
if (misc_features_support.depth_bounds_supported) {
print_verbose("- Depth bounds test supported");
} else {
print_verbose("- Depth bounds test not supported");
}
return OK;
}
Error RenderingDeviceDriverD3D12::_get_device_limits() {
D3D12_FEATURE_DATA_D3D12_OPTIONS options = {};
HRESULT res = device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), ERR_UNAVAILABLE, "CheckFeatureSupport failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
// https://docs.microsoft.com/en-us/windows/win32/direct3d12/hardware-support
device_limits.max_srvs_per_shader_stage = options.ResourceBindingTier == D3D12_RESOURCE_BINDING_TIER_1 ? 128 : UINT64_MAX;
device_limits.max_cbvs_per_shader_stage = options.ResourceBindingTier <= D3D12_RESOURCE_BINDING_TIER_2 ? 14 : UINT64_MAX;
device_limits.max_samplers_across_all_stages = options.ResourceBindingTier == D3D12_RESOURCE_BINDING_TIER_1 ? 16 : 2048;
if (options.ResourceBindingTier == D3D12_RESOURCE_BINDING_TIER_1) {
device_limits.max_uavs_across_all_stages = feature_level <= 110 ? 8 : 64;
} else if (options.ResourceBindingTier == D3D12_RESOURCE_BINDING_TIER_2) {
device_limits.max_uavs_across_all_stages = 64;
} else {
device_limits.max_uavs_across_all_stages = UINT64_MAX;
}
// Retrieving the timestamp frequency requires creating a command queue that will be discarded immediately.
ComPtr<ID3D12CommandQueue> unused_command_queue;
D3D12_COMMAND_QUEUE_DESC queue_desc = {};
queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
res = device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(unused_command_queue.GetAddressOf()));
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
res = unused_command_queue->GetTimestampFrequency(&device_limits.timestamp_frequency);
if (!SUCCEEDED(res)) {
print_verbose("D3D12: GetTimestampFrequency failed with error " + vformat("0x%08ux", (uint64_t)res) + ". Timestamps will be inaccurate.");
}
return OK;
}
Error RenderingDeviceDriverD3D12::_initialize_allocator() {
D3D12MA::ALLOCATOR_DESC allocator_desc = {};
allocator_desc.pDevice = device.Get();
allocator_desc.pAdapter = adapter.Get();
allocator_desc.Flags = D3D12MA::ALLOCATOR_FLAG_DEFAULT_POOLS_NOT_ZEROED;
HRESULT res = D3D12MA::CreateAllocator(&allocator_desc, &allocator);
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), ERR_CANT_CREATE, "D3D12MA::CreateAllocator failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
return OK;
}
static Error create_command_signature(ID3D12Device *device, D3D12_INDIRECT_ARGUMENT_TYPE p_type, uint32_t p_stride, ComPtr<ID3D12CommandSignature> *r_cmd_sig) {
D3D12_INDIRECT_ARGUMENT_DESC iarg_desc = {};
iarg_desc.Type = p_type;
D3D12_COMMAND_SIGNATURE_DESC cs_desc = {};
cs_desc.ByteStride = p_stride;
cs_desc.NumArgumentDescs = 1;
cs_desc.pArgumentDescs = &iarg_desc;
cs_desc.NodeMask = 0;
HRESULT res = device->CreateCommandSignature(&cs_desc, nullptr, IID_PPV_ARGS(r_cmd_sig->GetAddressOf()));
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), ERR_CANT_CREATE, "CreateCommandSignature failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
return OK;
}
Error RenderingDeviceDriverD3D12::_initialize_frames(uint32_t p_frame_count) {
Error err;
//CD3DX12_RESOURCE_DESC resource_desc = CD3DX12_RESOURCE_DESC::Buffer(D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
uint32_t resource_descriptors_per_frame = GLOBAL_GET("rendering/rendering_device/d3d12/max_resource_descriptors_per_frame");
uint32_t sampler_descriptors_per_frame = GLOBAL_GET("rendering/rendering_device/d3d12/max_sampler_descriptors_per_frame");
uint32_t misc_descriptors_per_frame = GLOBAL_GET("rendering/rendering_device/d3d12/max_misc_descriptors_per_frame");
frames.resize(p_frame_count);
for (uint32_t i = 0; i < frames.size(); i++) {
err = frames[i].desc_heaps.resources.allocate(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, resource_descriptors_per_frame, true);
ERR_FAIL_COND_V_MSG(err != OK, ERR_CANT_CREATE, "Creating the frame's RESOURCE descriptors heap failed.");
err = frames[i].desc_heaps.samplers.allocate(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, sampler_descriptors_per_frame, true);
ERR_FAIL_COND_V_MSG(err != OK, ERR_CANT_CREATE, "Creating the frame's SAMPLER descriptors heap failed.");
err = frames[i].desc_heaps.aux.allocate(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, misc_descriptors_per_frame, false);
ERR_FAIL_COND_V_MSG(err != OK, ERR_CANT_CREATE, "Creating the frame's AUX descriptors heap failed.");
err = frames[i].desc_heaps.rtv.allocate(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_RTV, misc_descriptors_per_frame, false);
ERR_FAIL_COND_V_MSG(err != OK, ERR_CANT_CREATE, "Creating the frame's RENDER TARGET descriptors heap failed.");
frames[i].desc_heap_walkers.resources = frames[i].desc_heaps.resources.make_walker();
frames[i].desc_heap_walkers.samplers = frames[i].desc_heaps.samplers.make_walker();
frames[i].desc_heap_walkers.aux = frames[i].desc_heaps.aux.make_walker();
frames[i].desc_heap_walkers.rtv = frames[i].desc_heaps.rtv.make_walker();
}
return OK;
}
Error RenderingDeviceDriverD3D12::_initialize_command_signatures() {
Error err = create_command_signature(device.Get(), D3D12_INDIRECT_ARGUMENT_TYPE_DRAW, sizeof(D3D12_DRAW_ARGUMENTS), &indirect_cmd_signatures.draw);
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
err = create_command_signature(device.Get(), D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED, sizeof(D3D12_DRAW_INDEXED_ARGUMENTS), &indirect_cmd_signatures.draw_indexed);
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
err = create_command_signature(device.Get(), D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH, sizeof(D3D12_DISPATCH_ARGUMENTS), &indirect_cmd_signatures.dispatch);
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
return OK;
}
Error RenderingDeviceDriverD3D12::initialize(uint32_t p_device_index, uint32_t p_frame_count) {
context_device = context_driver->device_get(p_device_index);
adapter = context_driver->create_adapter(p_device_index);
ERR_FAIL_NULL_V(adapter, ERR_CANT_CREATE);
HRESULT res = adapter->GetDesc(&adapter_desc);
ERR_FAIL_COND_V(!SUCCEEDED(res), ERR_CANT_CREATE);
// Set the pipeline cache ID based on the adapter information.
pipeline_cache_id = String::hex_encode_buffer((uint8_t *)&adapter_desc.AdapterLuid, sizeof(LUID));
pipeline_cache_id += "-driver-" + itos(adapter_desc.Revision);
Error err = _initialize_device();
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
err = _check_capabilities();
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
err = _get_device_limits();
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
err = _initialize_allocator();
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
err = _initialize_frames(p_frame_count);
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
err = _initialize_command_signatures();
ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
glsl_type_singleton_init_or_ref();
return OK;
}
|