-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
rendering_device_driver_d3d12.cpp
6725 lines (5812 loc) · 289 KB
/
rendering_device_driver_d3d12.cpp
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
/**************************************************************************/
/* 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;
}
}
/****************/
/**** MEMORY ****/
/****************/
static const uint32_t SMALL_ALLOCATION_MAX_SIZE = 4096;
#ifdef USE_SMALL_ALLOCS_POOL
D3D12MA::Pool *RenderingDeviceDriverD3D12::_find_or_create_small_allocs_pool(D3D12_HEAP_TYPE p_heap_type, D3D12_HEAP_FLAGS p_heap_flags) {
D3D12_HEAP_FLAGS effective_heap_flags = p_heap_flags;
if (allocator->GetD3D12Options().ResourceHeapTier != D3D12_RESOURCE_HEAP_TIER_1) {
// Heap tier 2 allows mixing resource types liberally.
effective_heap_flags &= ~(D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS | D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES | D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES);
}
AllocPoolKey pool_key;
pool_key.heap_type = p_heap_type;
pool_key.heap_flags = effective_heap_flags;
if (small_allocs_pools.has(pool_key.key)) {
return small_allocs_pools[pool_key.key].Get();
}
#ifdef DEV_ENABLED
print_verbose("Creating D3D12MA small objects pool for heap type " + itos(p_heap_type) + " and heap flags " + itos(p_heap_flags));
#endif
D3D12MA::POOL_DESC poolDesc = {};
poolDesc.HeapProperties.Type = p_heap_type;
poolDesc.HeapFlags = effective_heap_flags;
ComPtr<D3D12MA::Pool> pool;
HRESULT res = allocator->CreatePool(&poolDesc, pool.GetAddressOf());
small_allocs_pools[pool_key.key] = pool; // Don't try to create it again if failed the first time.
ERR_FAIL_COND_V_MSG(!SUCCEEDED(res), nullptr, "CreatePool failed with error " + vformat("0x%08ux", (uint64_t)res) + ".");
return pool.Get();
}
#endif
/******************/
/**** 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(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.
#ifdef DEBUG_COUNT_BARRIERS
uint64_t start = OS::get_singleton()->get_ticks_usec();
#endif
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 != res_barriers_batch;
if (needs_uav_barrier) {
if (res_barriers.size() < res_barriers_count + 1) {
res_barriers.resize(res_barriers_count + 1);
}
res_barriers[res_barriers_count] = CD3DX12_RESOURCE_BARRIER::UAV(p_resource->resource);
res_barriers_count++;
res_states->last_batch_with_uav_barrier = res_barriers_batch;
}
} else {
uint64_t subres_mask_piece = ((uint64_t)1 << (p_subresource & 0b111111));
uint8_t subres_qword = p_subresource >> 6;
if (res_barriers_requests.has(res_states)) {
BarrierRequest &br = 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 = {};
if (subres_already_there) {
final_states = br.groups[curr_group_idx].states;
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;
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 = 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;
}
}
#ifdef DEBUG_COUNT_BARRIERS
frame_barriers_cpu_time += OS::get_singleton()->get_ticks_usec() - start;
#endif
}
void RenderingDeviceDriverD3D12::_resource_transitions_flush(ID3D12GraphicsCommandList *p_cmd_list) {
#ifdef DEBUG_COUNT_BARRIERS
uint64_t start = OS::get_singleton()->get_ticks_usec();
#endif
for (const KeyValue<ResourceInfo::States *, BarrierRequest> &E : 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 != res_barriers_batch;
uint32_t needed_barriers = (needs_uav_barrier ? 1 : 0) + 1;
if (res_barriers.size() < res_barriers_count + needed_barriers) {
res_barriers.resize(res_barriers_count + needed_barriers);
}
if (needs_uav_barrier) {
res_barriers[res_barriers_count] = CD3DX12_RESOURCE_BARRIER::UAV(br.dx_resource);
res_barriers_count++;
res_states->last_batch_with_uav_barrier = res_barriers_batch;
}
if (res_states->subresource_states[0] != br.groups[0].states) {
res_barriers[res_barriers_count] = CD3DX12_RESOURCE_BARRIER::Transition(br.dx_resource, res_states->subresource_states[0], br.groups[0].states, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES);
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 != res_barriers_batch;
uint32_t needed_barriers = (needs_uav_barrier ? 1 : 0) + br.planes;
if (res_barriers.size() < res_barriers_count + needed_barriers) {
res_barriers.resize(res_barriers_count + needed_barriers);
}
if (needs_uav_barrier) {
res_barriers[res_barriers_count] = CD3DX12_RESOURCE_BARRIER::UAV(br.dx_resource);
res_barriers_count++;
res_states->last_batch_with_uav_barrier = res_barriers_batch;
}
if (*curr_state != g.states) {
for (uint8_t k = 0; k < br.planes; k++) {
res_barriers[res_barriers_count] = CD3DX12_RESOURCE_BARRIER::Transition(br.dx_resource, *curr_state, g.states, subresource + k * num_subresources);
res_barriers_count++;
}
}
*curr_state = g.states;
subresource++;
} while (subresource < num_subresources);
}
}
}
if (res_barriers_count) {
p_cmd_list->ResourceBarrier(res_barriers_count, res_barriers.ptr());
res_barriers_requests.clear();
}
#ifdef DEBUG_COUNT_BARRIERS
frame_barriers_count += res_barriers_count;
frame_barriers_batches_count++;
frame_barriers_cpu_time += OS::get_singleton()->get_ticks_usec() - start;
#endif
res_barriers_count = 0;
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: {
#ifdef USE_SMALL_ALLOCS_POOL
if (p_size <= SMALL_ALLOCATION_MAX_SIZE) {
allocation_desc.CustomPool = _find_or_create_small_allocs_pool(allocation_desc.HeapType, D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS);
}
#endif
} 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;
buf_info->texel_format = p_format;
return true;
}
void RenderingDeviceDriverD3D12::buffer_free(BufferID p_buffer) {
BufferInfo *buf_info = (BufferInfo *)p_buffer.id;
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;
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;
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;
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,
};