-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSDL_gpu_d3d12.c
9090 lines (7744 loc) · 329 KB
/
SDL_gpu_d3d12.c
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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#ifdef SDL_GPU_D3D12
#include "../../core/windows/SDL_windows.h"
#include "../../video/directx/SDL_d3d12.h"
#include "../SDL_sysgpu.h"
#ifdef __IDXGIInfoQueue_INTERFACE_DEFINED__
#define HAVE_IDXGIINFOQUEUE
#endif
// Built-in shaders, compiled with compile_shaders.bat
#define g_FullscreenVert D3D12_FullscreenVert
#define g_BlitFrom2D D3D12_BlitFrom2D
#define g_BlitFrom2DArray D3D12_BlitFrom2DArray
#define g_BlitFrom3D D3D12_BlitFrom3D
#define g_BlitFromCube D3D12_BlitFromCube
#define g_BlitFromCubeArray D3D12_BlitFromCubeArray
#if defined(SDL_PLATFORM_XBOXSERIES)
#include "D3D12_Blit_Series.h"
#elif defined(SDL_PLATFORM_XBOXONE)
#include "D3D12_Blit_One.h"
#else
#include "D3D12_Blit.h"
#endif
#undef g_FullscreenVert
#undef g_BlitFrom2D
#undef g_BlitFrom2DArray
#undef g_BlitFrom3D
#undef g_BlitFromCube
#undef g_BlitFromCubeArray
// Macros
#define SET_ERROR(fmt, msg) \
do { \
if (renderer->debug_mode) { \
SDL_LogError(SDL_LOG_CATEGORY_GPU, fmt, msg); \
} \
SDL_SetError(fmt, msg); \
} while (0)
#define SET_ERROR_AND_RETURN(fmt, msg, ret) \
do { \
if (renderer->debug_mode) { \
SDL_LogError(SDL_LOG_CATEGORY_GPU, fmt, msg); \
} \
SDL_SetError(fmt, msg); \
return ret; \
} while (0)
#define SET_STRING_ERROR_AND_RETURN(msg, ret) SET_ERROR_AND_RETURN("%s", msg, ret)
#define CHECK_D3D12_ERROR_AND_RETURN(msg, ret) \
do { \
if (FAILED(res)) { \
D3D12_INTERNAL_SetError(renderer, msg, res); \
return (ret); \
} \
} while (0)
// Defines
#if defined(_WIN32)
#if defined(SDL_PLATFORM_XBOXSERIES)
#define D3D12_DLL "d3d12_xs.dll"
#elif defined(SDL_PLATFORM_XBOXONE)
#define D3D12_DLL "d3d12_x.dll"
#else
#define D3D12_DLL "d3d12.dll"
#endif
#define DXGI_DLL "dxgi.dll"
#define DXGIDEBUG_DLL "dxgidebug.dll"
#elif defined(__APPLE__)
#define D3D12_DLL "libdxvk_d3d12.dylib"
#define DXGI_DLL "libdxvk_dxgi.dylib"
#define DXGIDEBUG_DLL "libdxvk_dxgidebug.dylib"
#else
#define D3D12_DLL "libdxvk_d3d12.so"
#define DXGI_DLL "libdxvk_dxgi.so"
#define DXGIDEBUG_DLL "libdxvk_dxgidebug.so"
#endif
#define D3D12_CREATE_DEVICE_FUNC "D3D12CreateDevice"
#define D3D12_SERIALIZE_ROOT_SIGNATURE_FUNC "D3D12SerializeRootSignature"
#define CREATE_DXGI_FACTORY1_FUNC "CreateDXGIFactory1"
#define DXGI_GET_DEBUG_INTERFACE_FUNC "DXGIGetDebugInterface"
#define D3D12_GET_DEBUG_INTERFACE_FUNC "D3D12GetDebugInterface"
#define WINDOW_PROPERTY_DATA "SDL_GPUD3D12WindowPropertyData"
#define D3D_FEATURE_LEVEL_CHOICE D3D_FEATURE_LEVEL_11_1
#define D3D_FEATURE_LEVEL_CHOICE_STR "11_1"
#define MAX_ROOT_SIGNATURE_PARAMETERS 64
#define D3D12_FENCE_UNSIGNALED_VALUE 0
#define D3D12_FENCE_SIGNAL_VALUE 1
// TODO: do these need to be tuned?
#define VIEW_GPU_DESCRIPTOR_COUNT 65536
#define SAMPLER_GPU_DESCRIPTOR_COUNT 2048
#define STAGING_HEAP_DESCRIPTOR_COUNT 1024
#define SDL_GPU_SHADERSTAGE_COMPUTE (SDL_GPUShaderStage)2
#define EXPAND_ELEMENTS_IF_NEEDED(arr, initialValue, type) \
if (arr->count == arr->capacity) { \
if (arr->capacity == 0) { \
arr->capacity = initialValue; \
} else { \
arr->capacity *= 2; \
} \
arr->elements = (type *)SDL_realloc( \
arr->elements, \
arr->capacity * sizeof(type)); \
}
#ifdef _WIN32
#define HRESULT_FMT "(0x%08lX)"
#else
#define HRESULT_FMT "(0x%08X)"
#endif
// Function Pointer Signatures
typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY1)(const GUID *riid, void **ppFactory);
typedef HRESULT(WINAPI *PFN_DXGI_GET_DEBUG_INTERFACE)(const GUID *riid, void **ppDebug);
// IIDs (from https://www.magnumdb.com/)
static const IID D3D_IID_IDXGIFactory1 = { 0x770aae78, 0xf26f, 0x4dba, { 0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87 } };
static const IID D3D_IID_IDXGIFactory4 = { 0x1bc6ea02, 0xef36, 0x464f, { 0xbf, 0x0c, 0x21, 0xca, 0x39, 0xe5, 0x16, 0x8a } };
static const IID D3D_IID_IDXGIFactory5 = { 0x7632e1f5, 0xee65, 0x4dca, { 0x87, 0xfd, 0x84, 0xcd, 0x75, 0xf8, 0x83, 0x8d } };
static const IID D3D_IID_IDXGIFactory6 = { 0xc1b6694f, 0xff09, 0x44a9, { 0xb0, 0x3c, 0x77, 0x90, 0x0a, 0x0a, 0x1d, 0x17 } };
static const IID D3D_IID_IDXGIAdapter1 = { 0x29038f61, 0x3839, 0x4626, { 0x91, 0xfd, 0x08, 0x68, 0x79, 0x01, 0x1a, 0x05 } };
#if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
static const IID D3D_IID_IDXGIDevice1 = { 0x77db970f, 0x6276, 0x48ba, { 0xba, 0x28, 0x07, 0x01, 0x43, 0xb4, 0x39, 0x2c } };
#else
static const IID D3D_IID_IDXGIDevice = { 0x54ec77fa, 0x1377, 0x44e6, { 0x8c, 0x32, 0x88, 0xfd, 0x5f, 0x44, 0xc8, 0x4c } };
#endif
static const IID D3D_IID_IDXGISwapChain3 = { 0x94d99bdb, 0xf1f8, 0x4ab0, { 0xb2, 0x36, 0x7d, 0xa0, 0x17, 0x0e, 0xda, 0xb1 } };
#ifdef HAVE_IDXGIINFOQUEUE
static const IID D3D_IID_IDXGIDebug = { 0x119e7452, 0xde9e, 0x40fe, { 0x88, 0x06, 0x88, 0xf9, 0x0c, 0x12, 0xb4, 0x41 } };
static const IID D3D_IID_IDXGIInfoQueue = { 0xd67441c7, 0x672a, 0x476f, { 0x9e, 0x82, 0xcd, 0x55, 0xb4, 0x49, 0x49, 0xce } };
#endif
static const GUID D3D_IID_DXGI_DEBUG_ALL = { 0xe48ae283, 0xda80, 0x490b, { 0x87, 0xe6, 0x43, 0xe9, 0xa9, 0xcf, 0xda, 0x08 } };
static const GUID D3D_IID_D3DDebugObjectName = { 0x429b8c22, 0x9188, 0x4b0c, { 0x87, 0x42, 0xac, 0xb0, 0xbf, 0x85, 0xc2, 0x00 } };
static const IID D3D_IID_ID3D12Device = { 0x189819f1, 0x1db6, 0x4b57, { 0xbe, 0x54, 0x18, 0x21, 0x33, 0x9b, 0x85, 0xf7 } };
static const IID D3D_IID_ID3D12CommandQueue = { 0x0ec870a6, 0x5d7e, 0x4c22, { 0x8c, 0xfc, 0x5b, 0xaa, 0xe0, 0x76, 0x16, 0xed } };
static const IID D3D_IID_ID3D12DescriptorHeap = { 0x8efb471d, 0x616c, 0x4f49, { 0x90, 0xf7, 0x12, 0x7b, 0xb7, 0x63, 0xfa, 0x51 } };
static const IID D3D_IID_ID3D12Resource = { 0x696442be, 0xa72e, 0x4059, { 0xbc, 0x79, 0x5b, 0x5c, 0x98, 0x04, 0x0f, 0xad } };
static const IID D3D_IID_ID3D12CommandAllocator = { 0x6102dee4, 0xaf59, 0x4b09, { 0xb9, 0x99, 0xb4, 0x4d, 0x73, 0xf0, 0x9b, 0x24 } };
static const IID D3D_IID_ID3D12CommandList = { 0x7116d91c, 0xe7e4, 0x47ce, { 0xb8, 0xc6, 0xec, 0x81, 0x68, 0xf4, 0x37, 0xe5 } };
static const IID D3D_IID_ID3D12GraphicsCommandList = { 0x5b160d0f, 0xac1b, 0x4185, { 0x8b, 0xa8, 0xb3, 0xae, 0x42, 0xa5, 0xa4, 0x55 } };
static const IID D3D_IID_ID3D12Fence = { 0x0a753dcf, 0xc4d8, 0x4b91, { 0xad, 0xf6, 0xbe, 0x5a, 0x60, 0xd9, 0x5a, 0x76 } };
static const IID D3D_IID_ID3D12RootSignature = { 0xc54a6b66, 0x72df, 0x4ee8, { 0x8b, 0xe5, 0xa9, 0x46, 0xa1, 0x42, 0x92, 0x14 } };
static const IID D3D_IID_ID3D12CommandSignature = { 0xc36a797c, 0xec80, 0x4f0a, { 0x89, 0x85, 0xa7, 0xb2, 0x47, 0x50, 0x82, 0xd1 } };
static const IID D3D_IID_ID3D12PipelineState = { 0x765a30f3, 0xf624, 0x4c6f, { 0xa8, 0x28, 0xac, 0xe9, 0x48, 0x62, 0x24, 0x45 } };
static const IID D3D_IID_ID3D12Debug = { 0x344488b7, 0x6846, 0x474b, { 0xb9, 0x89, 0xf0, 0x27, 0x44, 0x82, 0x45, 0xe0 } };
static const IID D3D_IID_ID3D12InfoQueue = { 0x0742a90b, 0xc387, 0x483f, { 0xb9, 0x46, 0x30, 0xa7, 0xe4, 0xe6, 0x14, 0x58 } };
static const IID D3D_IID_ID3D12InfoQueue1 = { 0x2852dd88, 0xb484, 0x4c0c, { 0xb6, 0xb1, 0x67, 0x16, 0x85, 0x00, 0xe6, 0x00 } };
// Enums
typedef enum D3D12BufferType
{
D3D12_BUFFER_TYPE_GPU,
D3D12_BUFFER_TYPE_UNIFORM,
D3D12_BUFFER_TYPE_UPLOAD,
D3D12_BUFFER_TYPE_DOWNLOAD
} D3D12BufferType;
// Conversions
static SDL_GPUTextureFormat SwapchainCompositionToSDLTextureFormat[] = {
SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM, // SDR
SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB, // SDR_LINEAR
SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT, // HDR_EXTENDED_LINEAR
SDL_GPU_TEXTUREFORMAT_R10G10B10A2_UNORM, // HDR10_ST2084
};
static DXGI_FORMAT SwapchainCompositionToTextureFormat[] = {
DXGI_FORMAT_B8G8R8A8_UNORM, // SDR
DXGI_FORMAT_B8G8R8A8_UNORM, // SDR_LINEAR (NOTE: The RTV uses the sRGB format)
DXGI_FORMAT_R16G16B16A16_FLOAT, // HDR_EXTENDED_LINEAR
DXGI_FORMAT_R10G10B10A2_UNORM, // HDR10_ST2084
};
static DXGI_COLOR_SPACE_TYPE SwapchainCompositionToColorSpace[] = {
DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709, // SDR
DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709, // SDR_LINEAR
DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709, // HDR_EXTENDED_LINEAR
DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020 // HDR10_ST2084
};
static D3D12_BLEND SDLToD3D12_BlendFactor[] = {
D3D12_BLEND_ZERO, // INVALID
D3D12_BLEND_ZERO, // ZERO
D3D12_BLEND_ONE, // ONE
D3D12_BLEND_SRC_COLOR, // SRC_COLOR
D3D12_BLEND_INV_SRC_COLOR, // ONE_MINUS_SRC_COLOR
D3D12_BLEND_DEST_COLOR, // DST_COLOR
D3D12_BLEND_INV_DEST_COLOR, // ONE_MINUS_DST_COLOR
D3D12_BLEND_SRC_ALPHA, // SRC_ALPHA
D3D12_BLEND_INV_SRC_ALPHA, // ONE_MINUS_SRC_ALPHA
D3D12_BLEND_DEST_ALPHA, // DST_ALPHA
D3D12_BLEND_INV_DEST_ALPHA, // ONE_MINUS_DST_ALPHA
D3D12_BLEND_BLEND_FACTOR, // CONSTANT_COLOR
D3D12_BLEND_INV_BLEND_FACTOR, // ONE_MINUS_CONSTANT_COLOR
D3D12_BLEND_SRC_ALPHA_SAT, // SRC_ALPHA_SATURATE
};
SDL_COMPILE_TIME_ASSERT(SDLToD3D12_BlendFactor, SDL_arraysize(SDLToD3D12_BlendFactor) == SDL_GPU_BLENDFACTOR_MAX_ENUM_VALUE);
static D3D12_BLEND SDLToD3D12_BlendFactorAlpha[] = {
D3D12_BLEND_ZERO, // INVALID
D3D12_BLEND_ZERO, // ZERO
D3D12_BLEND_ONE, // ONE
D3D12_BLEND_SRC_ALPHA, // SRC_COLOR
D3D12_BLEND_INV_SRC_ALPHA, // ONE_MINUS_SRC_COLOR
D3D12_BLEND_DEST_ALPHA, // DST_COLOR
D3D12_BLEND_INV_DEST_ALPHA, // ONE_MINUS_DST_COLOR
D3D12_BLEND_SRC_ALPHA, // SRC_ALPHA
D3D12_BLEND_INV_SRC_ALPHA, // ONE_MINUS_SRC_ALPHA
D3D12_BLEND_DEST_ALPHA, // DST_ALPHA
D3D12_BLEND_INV_DEST_ALPHA, // ONE_MINUS_DST_ALPHA
D3D12_BLEND_BLEND_FACTOR, // CONSTANT_COLOR
D3D12_BLEND_INV_BLEND_FACTOR, // ONE_MINUS_CONSTANT_COLOR
D3D12_BLEND_SRC_ALPHA_SAT, // SRC_ALPHA_SATURATE
};
SDL_COMPILE_TIME_ASSERT(SDLToD3D12_BlendFactorAlpha, SDL_arraysize(SDLToD3D12_BlendFactorAlpha) == SDL_GPU_BLENDFACTOR_MAX_ENUM_VALUE);
static D3D12_BLEND_OP SDLToD3D12_BlendOp[] = {
D3D12_BLEND_OP_ADD, // INVALID
D3D12_BLEND_OP_ADD, // ADD
D3D12_BLEND_OP_SUBTRACT, // SUBTRACT
D3D12_BLEND_OP_REV_SUBTRACT, // REVERSE_SUBTRACT
D3D12_BLEND_OP_MIN, // MIN
D3D12_BLEND_OP_MAX // MAX
};
SDL_COMPILE_TIME_ASSERT(SDLToD3D12_BlendOp, SDL_arraysize(SDLToD3D12_BlendOp) == SDL_GPU_BLENDOP_MAX_ENUM_VALUE);
// These are actually color formats.
// For some genius reason, D3D12 splits format capabilites for depth-stencil views.
static DXGI_FORMAT SDLToD3D12_TextureFormat[] = {
DXGI_FORMAT_UNKNOWN, // INVALID
DXGI_FORMAT_A8_UNORM, // A8_UNORM
DXGI_FORMAT_R8_UNORM, // R8_UNORM
DXGI_FORMAT_R8G8_UNORM, // R8G8_UNORM
DXGI_FORMAT_R8G8B8A8_UNORM, // R8G8B8A8_UNORM
DXGI_FORMAT_R16_UNORM, // R16_UNORM
DXGI_FORMAT_R16G16_UNORM, // R16G16_UNORM
DXGI_FORMAT_R16G16B16A16_UNORM, // R16G16B16A16_UNORM
DXGI_FORMAT_R10G10B10A2_UNORM, // R10G10B10A2_UNORM
DXGI_FORMAT_B5G6R5_UNORM, // B5G6R5_UNORM
DXGI_FORMAT_B5G5R5A1_UNORM, // B5G5R5A1_UNORM
DXGI_FORMAT_B4G4R4A4_UNORM, // B4G4R4A4_UNORM
DXGI_FORMAT_B8G8R8A8_UNORM, // B8G8R8A8_UNORM
DXGI_FORMAT_BC1_UNORM, // BC1_UNORM
DXGI_FORMAT_BC2_UNORM, // BC2_UNORM
DXGI_FORMAT_BC3_UNORM, // BC3_UNORM
DXGI_FORMAT_BC4_UNORM, // BC4_UNORM
DXGI_FORMAT_BC5_UNORM, // BC5_UNORM
DXGI_FORMAT_BC7_UNORM, // BC7_UNORM
DXGI_FORMAT_BC6H_SF16, // BC6H_FLOAT
DXGI_FORMAT_BC6H_UF16, // BC6H_UFLOAT
DXGI_FORMAT_R8_SNORM, // R8_SNORM
DXGI_FORMAT_R8G8_SNORM, // R8G8_SNORM
DXGI_FORMAT_R8G8B8A8_SNORM, // R8G8B8A8_SNORM
DXGI_FORMAT_R16_SNORM, // R16_SNORM
DXGI_FORMAT_R16G16_SNORM, // R16G16_SNORM
DXGI_FORMAT_R16G16B16A16_SNORM, // R16G16B16A16_SNORM
DXGI_FORMAT_R16_FLOAT, // R16_FLOAT
DXGI_FORMAT_R16G16_FLOAT, // R16G16_FLOAT
DXGI_FORMAT_R16G16B16A16_FLOAT, // R16G16B16A16_FLOAT
DXGI_FORMAT_R32_FLOAT, // R32_FLOAT
DXGI_FORMAT_R32G32_FLOAT, // R32G32_FLOAT
DXGI_FORMAT_R32G32B32A32_FLOAT, // R32G32B32A32_FLOAT
DXGI_FORMAT_R11G11B10_FLOAT, // R11G11B10_UFLOAT
DXGI_FORMAT_R8_UINT, // R8_UINT
DXGI_FORMAT_R8G8_UINT, // R8G8_UINT
DXGI_FORMAT_R8G8B8A8_UINT, // R8G8B8A8_UINT
DXGI_FORMAT_R16_UINT, // R16_UINT
DXGI_FORMAT_R16G16_UINT, // R16G16_UINT
DXGI_FORMAT_R16G16B16A16_UINT, // R16G16B16A16_UINT
DXGI_FORMAT_R32_UINT, // R32_UINT
DXGI_FORMAT_R32G32_UINT, // R32G32_UINT
DXGI_FORMAT_R32G32B32A32_UINT, // R32G32B32A32_UINT
DXGI_FORMAT_R8_SINT, // R8_INT
DXGI_FORMAT_R8G8_SINT, // R8G8_INT
DXGI_FORMAT_R8G8B8A8_SINT, // R8G8B8A8_INT
DXGI_FORMAT_R16_SINT, // R16_INT
DXGI_FORMAT_R16G16_SINT, // R16G16_INT
DXGI_FORMAT_R16G16B16A16_SINT, // R16G16B16A16_INT
DXGI_FORMAT_R32_SINT, // R32_INT
DXGI_FORMAT_R32G32_SINT, // R32G32_INT
DXGI_FORMAT_R32G32B32A32_SINT, // R32G32B32A32_INT
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, // R8G8B8A8_UNORM_SRGB
DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, // B8G8R8A8_UNORM_SRGB
DXGI_FORMAT_BC1_UNORM_SRGB, // BC1_UNORM_SRGB
DXGI_FORMAT_BC2_UNORM_SRGB, // BC2_UNORM_SRGB
DXGI_FORMAT_BC3_UNORM_SRGB, // BC3_UNORM_SRGB
DXGI_FORMAT_BC7_UNORM_SRGB, // BC7_UNORM_SRGB
DXGI_FORMAT_R16_UNORM, // D16_UNORM
DXGI_FORMAT_R24_UNORM_X8_TYPELESS, // D24_UNORM
DXGI_FORMAT_R32_FLOAT, // D32_FLOAT
DXGI_FORMAT_R24_UNORM_X8_TYPELESS, // D24_UNORM_S8_UINT
DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS, // D32_FLOAT_S8_UINT
DXGI_FORMAT_UNKNOWN, // ASTC_4x4_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_5x4_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_5x5_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_6x5_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_6x6_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_8x5_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_8x6_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_8x8_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_10x5_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_10x6_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_10x8_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_10x10_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_12x10_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_12x12_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_4x4_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_5x4_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_5x5_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_6x5_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_6x6_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_8x5_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_8x6_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_8x8_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_10x5_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_10x6_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_10x8_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_10x10_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_12x10_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_12x12_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_4x4_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_5x4_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_5x5_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_6x5_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_6x6_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_8x5_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_8x6_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_8x8_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_10x5_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_10x6_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_10x8_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_10x10_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_12x10_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_12x12_FLOAT
};
SDL_COMPILE_TIME_ASSERT(SDLToD3D12_TextureFormat, SDL_arraysize(SDLToD3D12_TextureFormat) == SDL_GPU_TEXTUREFORMAT_MAX_ENUM_VALUE);
static DXGI_FORMAT SDLToD3D12_DepthFormat[] = {
DXGI_FORMAT_UNKNOWN, // INVALID
DXGI_FORMAT_UNKNOWN, // A8_UNORM
DXGI_FORMAT_UNKNOWN, // R8_UNORM
DXGI_FORMAT_UNKNOWN, // R8G8_UNORM
DXGI_FORMAT_UNKNOWN, // R8G8B8A8_UNORM
DXGI_FORMAT_UNKNOWN, // R16_UNORM
DXGI_FORMAT_UNKNOWN, // R16G16_UNORM
DXGI_FORMAT_UNKNOWN, // R16G16B16A16_UNORM
DXGI_FORMAT_UNKNOWN, // R10G10B10A2_UNORM
DXGI_FORMAT_UNKNOWN, // B5G6R5_UNORM
DXGI_FORMAT_UNKNOWN, // B5G5R5A1_UNORM
DXGI_FORMAT_UNKNOWN, // B4G4R4A4_UNORM
DXGI_FORMAT_UNKNOWN, // B8G8R8A8_UNORM
DXGI_FORMAT_UNKNOWN, // BC1_UNORM
DXGI_FORMAT_UNKNOWN, // BC2_UNORM
DXGI_FORMAT_UNKNOWN, // BC3_UNORM
DXGI_FORMAT_UNKNOWN, // BC4_UNORM
DXGI_FORMAT_UNKNOWN, // BC5_UNORM
DXGI_FORMAT_UNKNOWN, // BC7_UNORM
DXGI_FORMAT_UNKNOWN, // BC6H_FLOAT
DXGI_FORMAT_UNKNOWN, // BC6H_UFLOAT
DXGI_FORMAT_UNKNOWN, // R8_SNORM
DXGI_FORMAT_UNKNOWN, // R8G8_SNORM
DXGI_FORMAT_UNKNOWN, // R8G8B8A8_SNORM
DXGI_FORMAT_UNKNOWN, // R16_SNORM
DXGI_FORMAT_UNKNOWN, // R16G16_SNORM
DXGI_FORMAT_UNKNOWN, // R16G16B16A16_SNORM
DXGI_FORMAT_UNKNOWN, // R16_FLOAT
DXGI_FORMAT_UNKNOWN, // R16G16_FLOAT
DXGI_FORMAT_UNKNOWN, // R16G16B16A16_FLOAT
DXGI_FORMAT_UNKNOWN, // R32_FLOAT
DXGI_FORMAT_UNKNOWN, // R32G32_FLOAT
DXGI_FORMAT_UNKNOWN, // R32G32B32A32_FLOAT
DXGI_FORMAT_UNKNOWN, // R11G11B10_UFLOAT
DXGI_FORMAT_UNKNOWN, // R8_UINT
DXGI_FORMAT_UNKNOWN, // R8G8_UINT
DXGI_FORMAT_UNKNOWN, // R8G8B8A8_UINT
DXGI_FORMAT_UNKNOWN, // R16_UINT
DXGI_FORMAT_UNKNOWN, // R16G16_UINT
DXGI_FORMAT_UNKNOWN, // R16G16B16A16_UINT
DXGI_FORMAT_UNKNOWN, // R32_UINT
DXGI_FORMAT_UNKNOWN, // R32G32_UINT
DXGI_FORMAT_UNKNOWN, // R32G32B32A32_UINT
DXGI_FORMAT_UNKNOWN, // R8_INT
DXGI_FORMAT_UNKNOWN, // R8G8_INT
DXGI_FORMAT_UNKNOWN, // R8G8B8A8_INT
DXGI_FORMAT_UNKNOWN, // R16_INT
DXGI_FORMAT_UNKNOWN, // R16G16_INT
DXGI_FORMAT_UNKNOWN, // R16G16B16A16_INT
DXGI_FORMAT_UNKNOWN, // R32_INT
DXGI_FORMAT_UNKNOWN, // R32G32_INT
DXGI_FORMAT_UNKNOWN, // R32G32B32A32_INT
DXGI_FORMAT_UNKNOWN, // R8G8B8A8_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // B8G8R8A8_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // BC1_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // BC2_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // BC3_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // BC7_UNORM_SRGB
DXGI_FORMAT_D16_UNORM, // D16_UNORM
DXGI_FORMAT_D24_UNORM_S8_UINT, // D24_UNORM
DXGI_FORMAT_D32_FLOAT, // D32_FLOAT
DXGI_FORMAT_D24_UNORM_S8_UINT, // D24_UNORM_S8_UINT
DXGI_FORMAT_D32_FLOAT_S8X24_UINT, // D32_FLOAT_S8_UINT
DXGI_FORMAT_UNKNOWN, // ASTC_4x4_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_5x4_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_5x5_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_6x5_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_6x6_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_8x5_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_8x6_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_8x8_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_10x5_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_10x6_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_10x8_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_10x10_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_12x10_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_12x12_UNORM
DXGI_FORMAT_UNKNOWN, // ASTC_4x4_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_5x4_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_5x5_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_6x5_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_6x6_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_8x5_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_8x6_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_8x8_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_10x5_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_10x6_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_10x8_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_10x10_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_12x10_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_12x12_UNORM_SRGB
DXGI_FORMAT_UNKNOWN, // ASTC_4x4_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_5x4_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_5x5_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_6x5_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_6x6_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_8x5_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_8x6_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_8x8_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_10x5_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_10x6_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_10x8_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_10x10_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_12x10_FLOAT
DXGI_FORMAT_UNKNOWN, // ASTC_12x12_FLOAT
};
SDL_COMPILE_TIME_ASSERT(SDLToD3D12_DepthFormat, SDL_arraysize(SDLToD3D12_DepthFormat) == SDL_GPU_TEXTUREFORMAT_MAX_ENUM_VALUE);
static D3D12_COMPARISON_FUNC SDLToD3D12_CompareOp[] = {
D3D12_COMPARISON_FUNC_NEVER, // INVALID
D3D12_COMPARISON_FUNC_NEVER, // NEVER
D3D12_COMPARISON_FUNC_LESS, // LESS
D3D12_COMPARISON_FUNC_EQUAL, // EQUAL
D3D12_COMPARISON_FUNC_LESS_EQUAL, // LESS_OR_EQUAL
D3D12_COMPARISON_FUNC_GREATER, // GREATER
D3D12_COMPARISON_FUNC_NOT_EQUAL, // NOT_EQUAL
D3D12_COMPARISON_FUNC_GREATER_EQUAL, // GREATER_OR_EQUAL
D3D12_COMPARISON_FUNC_ALWAYS // ALWAYS
};
SDL_COMPILE_TIME_ASSERT(SDLToD3D12_CompareOp, SDL_arraysize(SDLToD3D12_CompareOp) == SDL_GPU_COMPAREOP_MAX_ENUM_VALUE);
static D3D12_STENCIL_OP SDLToD3D12_StencilOp[] = {
D3D12_STENCIL_OP_KEEP, // INVALID
D3D12_STENCIL_OP_KEEP, // KEEP
D3D12_STENCIL_OP_ZERO, // ZERO
D3D12_STENCIL_OP_REPLACE, // REPLACE
D3D12_STENCIL_OP_INCR_SAT, // INCREMENT_AND_CLAMP
D3D12_STENCIL_OP_DECR_SAT, // DECREMENT_AND_CLAMP
D3D12_STENCIL_OP_INVERT, // INVERT
D3D12_STENCIL_OP_INCR, // INCREMENT_AND_WRAP
D3D12_STENCIL_OP_DECR // DECREMENT_AND_WRAP
};
SDL_COMPILE_TIME_ASSERT(SDLToD3D12_StencilOp, SDL_arraysize(SDLToD3D12_StencilOp) == SDL_GPU_STENCILOP_MAX_ENUM_VALUE);
static D3D12_CULL_MODE SDLToD3D12_CullMode[] = {
D3D12_CULL_MODE_NONE, // NONE
D3D12_CULL_MODE_FRONT, // FRONT
D3D12_CULL_MODE_BACK // BACK
};
static D3D12_FILL_MODE SDLToD3D12_FillMode[] = {
D3D12_FILL_MODE_SOLID, // FILL
D3D12_FILL_MODE_WIREFRAME // LINE
};
static D3D12_INPUT_CLASSIFICATION SDLToD3D12_InputRate[] = {
D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, // VERTEX
D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA // INSTANCE
};
static DXGI_FORMAT SDLToD3D12_VertexFormat[] = {
DXGI_FORMAT_UNKNOWN, // UNKNOWN
DXGI_FORMAT_R32_SINT, // INT
DXGI_FORMAT_R32G32_SINT, // INT2
DXGI_FORMAT_R32G32B32_SINT, // INT3
DXGI_FORMAT_R32G32B32A32_SINT, // INT4
DXGI_FORMAT_R32_UINT, // UINT
DXGI_FORMAT_R32G32_UINT, // UINT2
DXGI_FORMAT_R32G32B32_UINT, // UINT3
DXGI_FORMAT_R32G32B32A32_UINT, // UINT4
DXGI_FORMAT_R32_FLOAT, // FLOAT
DXGI_FORMAT_R32G32_FLOAT, // FLOAT2
DXGI_FORMAT_R32G32B32_FLOAT, // FLOAT3
DXGI_FORMAT_R32G32B32A32_FLOAT, // FLOAT4
DXGI_FORMAT_R8G8_SINT, // BYTE2
DXGI_FORMAT_R8G8B8A8_SINT, // BYTE4
DXGI_FORMAT_R8G8_UINT, // UBYTE2
DXGI_FORMAT_R8G8B8A8_UINT, // UBYTE4
DXGI_FORMAT_R8G8_SNORM, // BYTE2_NORM
DXGI_FORMAT_R8G8B8A8_SNORM, // BYTE4_NORM
DXGI_FORMAT_R8G8_UNORM, // UBYTE2_NORM
DXGI_FORMAT_R8G8B8A8_UNORM, // UBYTE4_NORM
DXGI_FORMAT_R16G16_SINT, // SHORT2
DXGI_FORMAT_R16G16B16A16_SINT, // SHORT4
DXGI_FORMAT_R16G16_UINT, // USHORT2
DXGI_FORMAT_R16G16B16A16_UINT, // USHORT4
DXGI_FORMAT_R16G16_SNORM, // SHORT2_NORM
DXGI_FORMAT_R16G16B16A16_SNORM, // SHORT4_NORM
DXGI_FORMAT_R16G16_UNORM, // USHORT2_NORM
DXGI_FORMAT_R16G16B16A16_UNORM, // USHORT4_NORM
DXGI_FORMAT_R16G16_FLOAT, // HALF2
DXGI_FORMAT_R16G16B16A16_FLOAT // HALF4
};
SDL_COMPILE_TIME_ASSERT(SDLToD3D12_VertexFormat, SDL_arraysize(SDLToD3D12_VertexFormat) == SDL_GPU_VERTEXELEMENTFORMAT_MAX_ENUM_VALUE);
static Uint32 SDLToD3D12_SampleCount[] = {
1, // SDL_GPU_SAMPLECOUNT_1
2, // SDL_GPU_SAMPLECOUNT_2
4, // SDL_GPU_SAMPLECOUNT_4
8, // SDL_GPU_SAMPLECOUNT_8
};
static D3D12_PRIMITIVE_TOPOLOGY SDLToD3D12_PrimitiveType[] = {
D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST, // TRIANGLELIST
D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP, // TRIANGLESTRIP
D3D_PRIMITIVE_TOPOLOGY_LINELIST, // LINELIST
D3D_PRIMITIVE_TOPOLOGY_LINESTRIP, // LINESTRIP
D3D_PRIMITIVE_TOPOLOGY_POINTLIST // POINTLIST
};
static D3D12_PRIMITIVE_TOPOLOGY_TYPE SDLToD3D12_PrimitiveTopologyType[] = {
D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, // TRIANGLELIST
D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, // TRIANGLESTRIP
D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE, // LINELIST
D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE, // LINESTRIP
D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT // POINTLIST
};
static D3D12_TEXTURE_ADDRESS_MODE SDLToD3D12_SamplerAddressMode[] = {
D3D12_TEXTURE_ADDRESS_MODE_WRAP, // REPEAT
D3D12_TEXTURE_ADDRESS_MODE_MIRROR, // MIRRORED_REPEAT
D3D12_TEXTURE_ADDRESS_MODE_CLAMP // CLAMP_TO_EDGE
};
static D3D12_FILTER SDLToD3D12_Filter(
SDL_GPUFilter minFilter,
SDL_GPUFilter magFilter,
SDL_GPUSamplerMipmapMode mipmapMode,
bool comparisonEnabled,
bool anisotropyEnabled)
{
D3D12_FILTER result = D3D12_ENCODE_BASIC_FILTER(
(minFilter == SDL_GPU_FILTER_LINEAR) ? 1 : 0,
(magFilter == SDL_GPU_FILTER_LINEAR) ? 1 : 0,
(mipmapMode == SDL_GPU_SAMPLERMIPMAPMODE_LINEAR) ? 1 : 0,
comparisonEnabled ? 1 : 0);
if (anisotropyEnabled) {
result = (D3D12_FILTER)(result | D3D12_ANISOTROPIC_FILTERING_BIT);
}
return result;
}
// Structures
typedef struct D3D12Renderer D3D12Renderer;
typedef struct D3D12CommandBufferPool D3D12CommandBufferPool;
typedef struct D3D12CommandBuffer D3D12CommandBuffer;
typedef struct D3D12Texture D3D12Texture;
typedef struct D3D12Shader D3D12Shader;
typedef struct D3D12GraphicsPipeline D3D12GraphicsPipeline;
typedef struct D3D12ComputePipeline D3D12ComputePipeline;
typedef struct D3D12Buffer D3D12Buffer;
typedef struct D3D12BufferContainer D3D12BufferContainer;
typedef struct D3D12UniformBuffer D3D12UniformBuffer;
typedef struct D3D12DescriptorHeap D3D12DescriptorHeap;
typedef struct D3D12StagingDescriptor D3D12StagingDescriptor;
typedef struct D3D12TextureDownload D3D12TextureDownload;
typedef struct D3D12Fence
{
ID3D12Fence *handle;
HANDLE event; // used for blocking
SDL_AtomicInt referenceCount;
} D3D12Fence;
struct D3D12DescriptorHeap
{
ID3D12DescriptorHeap *handle;
D3D12_DESCRIPTOR_HEAP_TYPE heapType;
D3D12_CPU_DESCRIPTOR_HANDLE descriptorHeapCPUStart;
D3D12_GPU_DESCRIPTOR_HANDLE descriptorHeapGPUStart; // only used by GPU heaps
Uint32 maxDescriptors;
Uint32 descriptorSize;
bool staging;
Uint32 currentDescriptorIndex; // only used by GPU heaps
};
typedef struct D3D12GPUDescriptorHeapPool
{
Uint32 capacity;
Uint32 count;
D3D12DescriptorHeap **heaps;
SDL_Mutex *lock;
} D3D12GPUDescriptorHeapPool;
// The only thing we care about with staging descriptors is being able to grab a free descriptor.
typedef struct D3D12StagingDescriptorPool
{
Uint32 heapCount;
D3D12DescriptorHeap **heaps;
// Descriptor handles are owned by resources, so these can be thought of as descriptions of a free index within a heap.
Uint32 freeDescriptorCapacity;
Uint32 freeDescriptorCount;
D3D12StagingDescriptor *freeDescriptors;
SDL_Mutex *lock;
} D3D12StagingDescriptorPool;
struct D3D12StagingDescriptor
{
D3D12StagingDescriptorPool *pool;
D3D12DescriptorHeap *heap;
D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle;
Uint32 cpuHandleIndex;
};
typedef struct D3D12TextureContainer
{
TextureCommonHeader header;
D3D12Texture *activeTexture;
D3D12Texture **textures;
Uint32 textureCapacity;
Uint32 textureCount;
// Swapchain images cannot be cycled
bool canBeCycled;
char *debugName;
} D3D12TextureContainer;
// Null views represent by heap = NULL
typedef struct D3D12TextureSubresource
{
D3D12Texture *parent;
Uint32 layer;
Uint32 level;
Uint32 depth;
Uint32 index;
// One per depth slice
D3D12StagingDescriptor *rtvHandles; // NULL if not a color target
D3D12StagingDescriptor uavHandle; // NULL if not a compute storage write texture
D3D12StagingDescriptor dsvHandle; // NULL if not a depth stencil target
} D3D12TextureSubresource;
struct D3D12Texture
{
D3D12TextureContainer *container;
Uint32 containerIndex;
D3D12TextureSubresource *subresources;
Uint32 subresourceCount; /* layerCount * num_levels */
ID3D12Resource *resource;
D3D12StagingDescriptor srvHandle;
SDL_AtomicInt referenceCount;
};
typedef struct D3D12Sampler
{
SDL_GPUSamplerCreateInfo createInfo;
D3D12StagingDescriptor handle;
SDL_AtomicInt referenceCount;
} D3D12Sampler;
typedef struct D3D12WindowData
{
SDL_Window *window;
#if (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
D3D12XBOX_FRAME_PIPELINE_TOKEN frameToken;
#else
IDXGISwapChain3 *swapchain;
#endif
SDL_GPUPresentMode present_mode;
SDL_GPUSwapchainComposition swapchainComposition;
DXGI_COLOR_SPACE_TYPE swapchainColorSpace;
Uint32 frameCounter;
D3D12TextureContainer textureContainers[MAX_FRAMES_IN_FLIGHT];
Uint32 swapchainTextureCount;
SDL_GPUFence *inFlightFences[MAX_FRAMES_IN_FLIGHT];
Uint32 width;
Uint32 height;
bool needsSwapchainRecreate;
} D3D12WindowData;
typedef struct D3D12PresentData
{
D3D12WindowData *windowData;
Uint32 swapchainImageIndex;
} D3D12PresentData;
struct D3D12Renderer
{
// Reference to the parent device
SDL_GPUDevice *sdlGPUDevice;
#if !(defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES))
IDXGIDebug *dxgiDebug;
IDXGIFactory4 *factory;
#ifdef HAVE_IDXGIINFOQUEUE
IDXGIInfoQueue *dxgiInfoQueue;
#endif
IDXGIAdapter1 *adapter;
SDL_SharedObject *dxgi_dll;
SDL_SharedObject *dxgidebug_dll;
#endif
ID3D12Debug *d3d12Debug;
BOOL supportsTearing;
SDL_SharedObject *d3d12_dll;
ID3D12Device *device;
PFN_D3D12_SERIALIZE_ROOT_SIGNATURE D3D12SerializeRootSignature_func;
const char *semantic;
SDL_iconv_t iconv;
ID3D12CommandQueue *commandQueue;
bool debug_mode;
bool GPUUploadHeapSupported;
// FIXME: these might not be necessary since we're not using custom heaps
bool UMA;
bool UMACacheCoherent;
Uint32 allowedFramesInFlight;
// Indirect command signatures
ID3D12CommandSignature *indirectDrawCommandSignature;
ID3D12CommandSignature *indirectIndexedDrawCommandSignature;
ID3D12CommandSignature *indirectDispatchCommandSignature;
// Blit
SDL_GPUShader *blitVertexShader;
SDL_GPUShader *blitFrom2DShader;
SDL_GPUShader *blitFrom2DArrayShader;
SDL_GPUShader *blitFrom3DShader;
SDL_GPUShader *blitFromCubeShader;
SDL_GPUShader *blitFromCubeArrayShader;
SDL_GPUSampler *blitNearestSampler;
SDL_GPUSampler *blitLinearSampler;
BlitPipelineCacheEntry *blitPipelines;
Uint32 blitPipelineCount;
Uint32 blitPipelineCapacity;
// Resources
D3D12CommandBuffer **availableCommandBuffers;
Uint32 availableCommandBufferCount;
Uint32 availableCommandBufferCapacity;
D3D12CommandBuffer **submittedCommandBuffers;
Uint32 submittedCommandBufferCount;
Uint32 submittedCommandBufferCapacity;
D3D12UniformBuffer **uniformBufferPool;
Uint32 uniformBufferPoolCount;
Uint32 uniformBufferPoolCapacity;
D3D12WindowData **claimedWindows;
Uint32 claimedWindowCount;
Uint32 claimedWindowCapacity;
D3D12Fence **availableFences;
Uint32 availableFenceCount;
Uint32 availableFenceCapacity;
D3D12StagingDescriptorPool *stagingDescriptorPools[D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES];
D3D12GPUDescriptorHeapPool gpuDescriptorHeapPools[2];
// Deferred resource releasing
D3D12Buffer **buffersToDestroy;
Uint32 buffersToDestroyCount;
Uint32 buffersToDestroyCapacity;
D3D12Texture **texturesToDestroy;
Uint32 texturesToDestroyCount;
Uint32 texturesToDestroyCapacity;
D3D12Sampler **samplersToDestroy;
Uint32 samplersToDestroyCount;
Uint32 samplersToDestroyCapacity;
D3D12GraphicsPipeline **graphicsPipelinesToDestroy;
Uint32 graphicsPipelinesToDestroyCount;
Uint32 graphicsPipelinesToDestroyCapacity;
D3D12ComputePipeline **computePipelinesToDestroy;
Uint32 computePipelinesToDestroyCount;
Uint32 computePipelinesToDestroyCapacity;
// Locks
SDL_Mutex *acquireCommandBufferLock;
SDL_Mutex *acquireUniformBufferLock;
SDL_Mutex *submitLock;
SDL_Mutex *windowLock;
SDL_Mutex *fenceLock;
SDL_Mutex *disposeLock;
};
struct D3D12CommandBuffer
{
// reserved for SDL_gpu
CommandBufferCommonHeader common;
// non owning parent reference
D3D12Renderer *renderer;
ID3D12CommandAllocator *commandAllocator;
ID3D12GraphicsCommandList *graphicsCommandList;
D3D12Fence *inFlightFence;
bool autoReleaseFence;
// Presentation data
D3D12PresentData *presentDatas;
Uint32 presentDataCount;
Uint32 presentDataCapacity;
D3D12TextureSubresource *colorTargetSubresources[MAX_COLOR_TARGET_BINDINGS];
D3D12TextureSubresource *colorResolveSubresources[MAX_COLOR_TARGET_BINDINGS];
D3D12TextureSubresource *depthStencilTextureSubresource;
D3D12GraphicsPipeline *currentGraphicsPipeline;
D3D12ComputePipeline *currentComputePipeline;
// Set at acquire time
D3D12DescriptorHeap *gpuDescriptorHeaps[D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER + 1];
D3D12UniformBuffer **usedUniformBuffers;
Uint32 usedUniformBufferCount;
Uint32 usedUniformBufferCapacity;
// Resource slot state
bool needVertexBufferBind;
bool needVertexSamplerBind;
bool needVertexStorageTextureBind;
bool needVertexStorageBufferBind;
bool needVertexUniformBufferBind[MAX_UNIFORM_BUFFERS_PER_STAGE];
bool needFragmentSamplerBind;
bool needFragmentStorageTextureBind;
bool needFragmentStorageBufferBind;
bool needFragmentUniformBufferBind[MAX_UNIFORM_BUFFERS_PER_STAGE];
bool needComputeSamplerBind;
bool needComputeReadOnlyStorageTextureBind;
bool needComputeReadOnlyStorageBufferBind;
bool needComputeUniformBufferBind[MAX_UNIFORM_BUFFERS_PER_STAGE];
D3D12Buffer *vertexBuffers[MAX_VERTEX_BUFFERS];
Uint32 vertexBufferOffsets[MAX_VERTEX_BUFFERS];
Uint32 vertexBufferCount;
D3D12Texture *vertexSamplerTextures[MAX_TEXTURE_SAMPLERS_PER_STAGE];
D3D12Sampler *vertexSamplers[MAX_TEXTURE_SAMPLERS_PER_STAGE];
D3D12Texture *vertexStorageTextures[MAX_STORAGE_TEXTURES_PER_STAGE];
D3D12Buffer *vertexStorageBuffers[MAX_STORAGE_BUFFERS_PER_STAGE];
D3D12UniformBuffer *vertexUniformBuffers[MAX_UNIFORM_BUFFERS_PER_STAGE];
D3D12Texture *fragmentSamplerTextures[MAX_TEXTURE_SAMPLERS_PER_STAGE];
D3D12Sampler *fragmentSamplers[MAX_TEXTURE_SAMPLERS_PER_STAGE];
D3D12Texture *fragmentStorageTextures[MAX_STORAGE_TEXTURES_PER_STAGE];
D3D12Buffer *fragmentStorageBuffers[MAX_STORAGE_BUFFERS_PER_STAGE];
D3D12UniformBuffer *fragmentUniformBuffers[MAX_UNIFORM_BUFFERS_PER_STAGE];
D3D12Texture *computeSamplerTextures[MAX_TEXTURE_SAMPLERS_PER_STAGE];
D3D12Sampler *computeSamplers[MAX_TEXTURE_SAMPLERS_PER_STAGE];
D3D12Texture *computeReadOnlyStorageTextures[MAX_STORAGE_TEXTURES_PER_STAGE];
D3D12Buffer *computeReadOnlyStorageBuffers[MAX_STORAGE_BUFFERS_PER_STAGE];
D3D12TextureSubresource *computeReadWriteStorageTextureSubresources[MAX_COMPUTE_WRITE_TEXTURES];
Uint32 computeReadWriteStorageTextureSubresourceCount;
D3D12Buffer *computeReadWriteStorageBuffers[MAX_COMPUTE_WRITE_BUFFERS];
Uint32 computeReadWriteStorageBufferCount;
D3D12UniformBuffer *computeUniformBuffers[MAX_UNIFORM_BUFFERS_PER_STAGE];
// Resource tracking
D3D12Texture **usedTextures;
Uint32 usedTextureCount;
Uint32 usedTextureCapacity;
D3D12Buffer **usedBuffers;
Uint32 usedBufferCount;
Uint32 usedBufferCapacity;
D3D12Sampler **usedSamplers;
Uint32 usedSamplerCount;
Uint32 usedSamplerCapacity;
D3D12GraphicsPipeline **usedGraphicsPipelines;
Uint32 usedGraphicsPipelineCount;
Uint32 usedGraphicsPipelineCapacity;
D3D12ComputePipeline **usedComputePipelines;
Uint32 usedComputePipelineCount;
Uint32 usedComputePipelineCapacity;
// Used for texture pitch hack
D3D12TextureDownload **textureDownloads;
Uint32 textureDownloadCount;
Uint32 textureDownloadCapacity;
};
struct D3D12Shader
{
// todo cleanup
void *bytecode;
size_t bytecodeSize;
SDL_GPUShaderStage stage;
Uint32 num_samplers;
Uint32 numUniformBuffers;
Uint32 numStorageBuffers;
Uint32 numStorageTextures;
};
typedef struct D3D12GraphicsRootSignature
{
ID3D12RootSignature *handle;
Sint32 vertexSamplerRootIndex;
Sint32 vertexSamplerTextureRootIndex;
Sint32 vertexStorageTextureRootIndex;
Sint32 vertexStorageBufferRootIndex;
Sint32 vertexUniformBufferRootIndex[MAX_UNIFORM_BUFFERS_PER_STAGE];
Sint32 fragmentSamplerRootIndex;
Sint32 fragmentSamplerTextureRootIndex;
Sint32 fragmentStorageTextureRootIndex;
Sint32 fragmentStorageBufferRootIndex;
Sint32 fragmentUniformBufferRootIndex[MAX_UNIFORM_BUFFERS_PER_STAGE];
} D3D12GraphicsRootSignature;
struct D3D12GraphicsPipeline
{
ID3D12PipelineState *pipelineState;
D3D12GraphicsRootSignature *rootSignature;
SDL_GPUPrimitiveType primitiveType;
Uint32 vertexStrides[MAX_VERTEX_BUFFERS];
Uint32 vertexSamplerCount;
Uint32 vertexUniformBufferCount;