-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathrenderer_vk.cpp
9529 lines (7953 loc) · 298 KB
/
renderer_vk.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
/*
* Copyright 2011-2025 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
*/
#include "bgfx_p.h"
#if BGFX_CONFIG_RENDERER_VULKAN
# include <bx/pixelformat.h>
# include "renderer_vk.h"
# include "shader_spirv.h"
#if BX_PLATFORM_OSX
# import <Cocoa/Cocoa.h>
# import <Foundation/Foundation.h>
# import <QuartzCore/QuartzCore.h>
# import <Metal/Metal.h>
#endif // BX_PLATFORM_OSX
namespace bgfx { namespace vk
{
static char s_viewName[BGFX_CONFIG_MAX_VIEWS][BGFX_CONFIG_MAX_VIEW_NAME];
inline void setViewType(ViewId _view, const bx::StringView _str)
{
if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION || BGFX_CONFIG_PROFILER) )
{
bx::memCopy(&s_viewName[_view][3], _str.getPtr(), _str.getLength() );
}
}
struct PrimInfo
{
VkPrimitiveTopology m_topology;
uint32_t m_min;
uint32_t m_div;
uint32_t m_sub;
};
static const PrimInfo s_primInfo[] =
{
{ VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 3, 3, 0 },
{ VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, 3, 1, 2 },
{ VK_PRIMITIVE_TOPOLOGY_LINE_LIST, 2, 2, 0 },
{ VK_PRIMITIVE_TOPOLOGY_LINE_STRIP, 2, 1, 1 },
{ VK_PRIMITIVE_TOPOLOGY_POINT_LIST, 1, 1, 0 },
{ VK_PRIMITIVE_TOPOLOGY_MAX_ENUM, 0, 0, 0 },
};
static_assert(Topology::Count == BX_COUNTOF(s_primInfo)-1);
static MsaaSamplerVK s_msaa[] =
{
{ 1, VK_SAMPLE_COUNT_1_BIT },
{ 2, VK_SAMPLE_COUNT_2_BIT },
{ 4, VK_SAMPLE_COUNT_4_BIT },
{ 8, VK_SAMPLE_COUNT_8_BIT },
{ 16, VK_SAMPLE_COUNT_16_BIT },
};
static const VkBlendFactor s_blendFactor[][2] =
{
{ VkBlendFactor(0), VkBlendFactor(0) }, // ignored
{ VK_BLEND_FACTOR_ZERO, VK_BLEND_FACTOR_ZERO }, // ZERO
{ VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_ONE }, // ONE
{ VK_BLEND_FACTOR_SRC_COLOR, VK_BLEND_FACTOR_SRC_ALPHA }, // SRC_COLOR
{ VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR, VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA }, // INV_SRC_COLOR
{ VK_BLEND_FACTOR_SRC_ALPHA, VK_BLEND_FACTOR_SRC_ALPHA }, // SRC_ALPHA
{ VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA }, // INV_SRC_ALPHA
{ VK_BLEND_FACTOR_DST_ALPHA, VK_BLEND_FACTOR_DST_ALPHA }, // DST_ALPHA
{ VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA }, // INV_DST_ALPHA
{ VK_BLEND_FACTOR_DST_COLOR, VK_BLEND_FACTOR_DST_ALPHA }, // DST_COLOR
{ VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR, VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA }, // INV_DST_COLOR
{ VK_BLEND_FACTOR_SRC_ALPHA, VK_BLEND_FACTOR_ONE }, // SRC_ALPHA_SAT
{ VK_BLEND_FACTOR_CONSTANT_COLOR, VK_BLEND_FACTOR_CONSTANT_COLOR }, // FACTOR
{ VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR }, // INV_FACTOR
};
static const VkBlendOp s_blendEquation[] =
{
VK_BLEND_OP_ADD,
VK_BLEND_OP_SUBTRACT,
VK_BLEND_OP_REVERSE_SUBTRACT,
VK_BLEND_OP_MIN,
VK_BLEND_OP_MAX,
};
static const VkCompareOp s_cmpFunc[] =
{
VkCompareOp(0), // ignored
VK_COMPARE_OP_LESS,
VK_COMPARE_OP_LESS_OR_EQUAL,
VK_COMPARE_OP_EQUAL,
VK_COMPARE_OP_GREATER_OR_EQUAL,
VK_COMPARE_OP_GREATER,
VK_COMPARE_OP_NOT_EQUAL,
VK_COMPARE_OP_NEVER,
VK_COMPARE_OP_ALWAYS,
};
static const VkStencilOp s_stencilOp[] =
{
VK_STENCIL_OP_ZERO,
VK_STENCIL_OP_KEEP,
VK_STENCIL_OP_REPLACE,
VK_STENCIL_OP_INCREMENT_AND_WRAP,
VK_STENCIL_OP_INCREMENT_AND_CLAMP,
VK_STENCIL_OP_DECREMENT_AND_WRAP,
VK_STENCIL_OP_DECREMENT_AND_CLAMP,
VK_STENCIL_OP_INVERT,
};
static const VkCullModeFlagBits s_cullMode[] =
{
VK_CULL_MODE_NONE,
VK_CULL_MODE_FRONT_BIT,
VK_CULL_MODE_BACK_BIT,
};
static const VkSamplerAddressMode s_textureAddress[] =
{
VK_SAMPLER_ADDRESS_MODE_REPEAT,
VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT,
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
};
struct PresentMode
{
VkPresentModeKHR mode;
bool vsync;
const char* name;
};
static const PresentMode s_presentMode[] =
{
{ VK_PRESENT_MODE_FIFO_KHR, true, "VK_PRESENT_MODE_FIFO_KHR" },
{ VK_PRESENT_MODE_FIFO_RELAXED_KHR, true, "VK_PRESENT_MODE_FIFO_RELAXED_KHR" },
{ VK_PRESENT_MODE_MAILBOX_KHR, true, "VK_PRESENT_MODE_MAILBOX_KHR" },
{ VK_PRESENT_MODE_IMMEDIATE_KHR, false, "VK_PRESENT_MODE_IMMEDIATE_KHR" },
};
#define VK_IMPORT_FUNC(_optional, _func) PFN_##_func _func
#define VK_IMPORT_INSTANCE_FUNC VK_IMPORT_FUNC
#define VK_IMPORT_DEVICE_FUNC VK_IMPORT_FUNC
VK_IMPORT
VK_IMPORT_INSTANCE
VK_IMPORT_DEVICE
#undef VK_IMPORT_DEVICE_FUNC
#undef VK_IMPORT_INSTANCE_FUNC
#undef VK_IMPORT_FUNC
struct TextureFormatInfo
{
VkFormat m_fmt;
VkFormat m_fmtSrv;
VkFormat m_fmtDsv;
VkFormat m_fmtSrgb;
VkComponentMapping m_mapping;
};
static const TextureFormatInfo s_textureFormat[] =
{
#define $_ VK_COMPONENT_SWIZZLE_IDENTITY
#define $0 VK_COMPONENT_SWIZZLE_ZERO
#define $1 VK_COMPONENT_SWIZZLE_ONE
#define $R VK_COMPONENT_SWIZZLE_R
#define $G VK_COMPONENT_SWIZZLE_G
#define $B VK_COMPONENT_SWIZZLE_B
#define $A VK_COMPONENT_SWIZZLE_A
{ VK_FORMAT_BC1_RGB_UNORM_BLOCK, VK_FORMAT_BC1_RGB_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_BC1_RGB_SRGB_BLOCK, { $_, $_, $_, $_ } }, // BC1
{ VK_FORMAT_BC2_UNORM_BLOCK, VK_FORMAT_BC2_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_BC2_SRGB_BLOCK, { $_, $_, $_, $_ } }, // BC2
{ VK_FORMAT_BC3_UNORM_BLOCK, VK_FORMAT_BC3_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_BC3_SRGB_BLOCK, { $_, $_, $_, $_ } }, // BC3
{ VK_FORMAT_BC4_UNORM_BLOCK, VK_FORMAT_BC4_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // BC4
{ VK_FORMAT_BC5_UNORM_BLOCK, VK_FORMAT_BC5_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // BC5
{ VK_FORMAT_BC6H_SFLOAT_BLOCK, VK_FORMAT_BC6H_SFLOAT_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // BC6H
{ VK_FORMAT_BC7_UNORM_BLOCK, VK_FORMAT_BC7_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_BC7_SRGB_BLOCK, { $_, $_, $_, $_ } }, // BC7
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // ETC1
{ VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ETC2
{ VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ETC2A
{ VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ETC2A1
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // PTC12
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // PTC14
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // PTC12A
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // PTC14A
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // PTC22
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // PTC24
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // ATC
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // ATCE
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // ATCI
{ VK_FORMAT_ASTC_4x4_UNORM_BLOCK, VK_FORMAT_ASTC_4x4_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_4x4_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC4x4
{ VK_FORMAT_ASTC_5x4_UNORM_BLOCK, VK_FORMAT_ASTC_5x4_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_5x4_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC5x4
{ VK_FORMAT_ASTC_5x5_UNORM_BLOCK, VK_FORMAT_ASTC_5x5_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_5x5_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC5x5
{ VK_FORMAT_ASTC_6x5_UNORM_BLOCK, VK_FORMAT_ASTC_6x5_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_6x5_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC6x5
{ VK_FORMAT_ASTC_6x6_UNORM_BLOCK, VK_FORMAT_ASTC_6x6_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_6x6_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC6x6
{ VK_FORMAT_ASTC_8x5_UNORM_BLOCK, VK_FORMAT_ASTC_8x5_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_8x5_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC8x5
{ VK_FORMAT_ASTC_8x6_UNORM_BLOCK, VK_FORMAT_ASTC_8x6_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_8x6_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC8x6
{ VK_FORMAT_ASTC_8x8_UNORM_BLOCK, VK_FORMAT_ASTC_8x8_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_8x8_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC8x8
{ VK_FORMAT_ASTC_10x5_UNORM_BLOCK, VK_FORMAT_ASTC_10x5_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_10x5_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC10x5
{ VK_FORMAT_ASTC_10x6_UNORM_BLOCK, VK_FORMAT_ASTC_10x6_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_10x6_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC10x6
{ VK_FORMAT_ASTC_10x8_UNORM_BLOCK, VK_FORMAT_ASTC_10x8_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_10x8_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC10x8
{ VK_FORMAT_ASTC_10x10_UNORM_BLOCK, VK_FORMAT_ASTC_10x10_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_10x10_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC10x10
{ VK_FORMAT_ASTC_12x10_UNORM_BLOCK, VK_FORMAT_ASTC_12x10_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_12x10_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC12x10
{ VK_FORMAT_ASTC_12x12_UNORM_BLOCK, VK_FORMAT_ASTC_12x12_UNORM_BLOCK, VK_FORMAT_UNDEFINED, VK_FORMAT_ASTC_12x12_SRGB_BLOCK, { $_, $_, $_, $_ } }, // ASTC12x12
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // Unknown
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R1
{ VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $0, $0, $0, $R } }, // A8
{ VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_R8_SRGB, { $_, $_, $_, $_ } }, // R8
{ VK_FORMAT_R8_SINT, VK_FORMAT_R8_SINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R8I
{ VK_FORMAT_R8_UINT, VK_FORMAT_R8_UINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R8U
{ VK_FORMAT_R8_SNORM, VK_FORMAT_R8_SNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R8S
{ VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R16
{ VK_FORMAT_R16_SINT, VK_FORMAT_R16_SINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R16I
{ VK_FORMAT_R16_UINT, VK_FORMAT_R16_UINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R16U
{ VK_FORMAT_R16_SFLOAT, VK_FORMAT_R16_SFLOAT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R16F
{ VK_FORMAT_R16_SNORM, VK_FORMAT_R16_SNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R16S
{ VK_FORMAT_R32_SINT, VK_FORMAT_R32_SINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R32I
{ VK_FORMAT_R32_UINT, VK_FORMAT_R32_UINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R32U
{ VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R32F
{ VK_FORMAT_R8G8_UNORM, VK_FORMAT_R8G8_UNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_R8G8_SRGB, { $_, $_, $_, $_ } }, // RG8
{ VK_FORMAT_R8G8_SINT, VK_FORMAT_R8G8_SINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG8I
{ VK_FORMAT_R8G8_UINT, VK_FORMAT_R8G8_UINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG8U
{ VK_FORMAT_R8G8_SNORM, VK_FORMAT_R8G8_SNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG8S
{ VK_FORMAT_R16G16_UNORM, VK_FORMAT_R16G16_UNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG16
{ VK_FORMAT_R16G16_SINT, VK_FORMAT_R16G16_SINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG16I
{ VK_FORMAT_R16G16_UINT, VK_FORMAT_R16G16_UINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG16U
{ VK_FORMAT_R16G16_SFLOAT, VK_FORMAT_R16G16_SFLOAT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG16F
{ VK_FORMAT_R16G16_SNORM, VK_FORMAT_R16G16_SNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG16S
{ VK_FORMAT_R32G32_SINT, VK_FORMAT_R32G32_SINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG32I
{ VK_FORMAT_R32G32_UINT, VK_FORMAT_R32G32_UINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG32U
{ VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG32F
{ VK_FORMAT_R8G8B8_UNORM, VK_FORMAT_R8G8B8_UNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_R8G8B8_SRGB, { $_, $_, $_, $_ } }, // RGB8
{ VK_FORMAT_R8G8B8_SINT, VK_FORMAT_R8G8B8_SINT, VK_FORMAT_UNDEFINED, VK_FORMAT_R8G8B8_SRGB, { $_, $_, $_, $_ } }, // RGB8I
{ VK_FORMAT_R8G8B8_UINT, VK_FORMAT_R8G8B8_UINT, VK_FORMAT_UNDEFINED, VK_FORMAT_R8G8B8_SRGB, { $_, $_, $_, $_ } }, // RGB8U
{ VK_FORMAT_R8G8B8_SNORM, VK_FORMAT_R8G8B8_SNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RGB8S
{ VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RGB9E5F
{ VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_B8G8R8A8_SRGB, { $_, $_, $_, $_ } }, // BGRA8
{ VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_R8G8B8A8_SRGB, { $_, $_, $_, $_ } }, // RGBA8
{ VK_FORMAT_R8G8B8A8_SINT, VK_FORMAT_R8G8B8A8_SINT, VK_FORMAT_UNDEFINED, VK_FORMAT_R8G8B8A8_SRGB, { $_, $_, $_, $_ } }, // RGBA8I
{ VK_FORMAT_R8G8B8A8_UINT, VK_FORMAT_R8G8B8A8_UINT, VK_FORMAT_UNDEFINED, VK_FORMAT_R8G8B8A8_SRGB, { $_, $_, $_, $_ } }, // RGBA8U
{ VK_FORMAT_R8G8B8A8_SNORM, VK_FORMAT_R8G8B8A8_SNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RGBA8S
{ VK_FORMAT_R16G16B16A16_UNORM, VK_FORMAT_R16G16B16A16_UNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RGBA16
{ VK_FORMAT_R16G16B16A16_SINT, VK_FORMAT_R16G16B16A16_SINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RGBA16I
{ VK_FORMAT_R16G16B16A16_UINT, VK_FORMAT_R16G16B16A16_UINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RGBA16U
{ VK_FORMAT_R16G16B16A16_SFLOAT, VK_FORMAT_R16G16B16A16_SFLOAT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RGBA16F
{ VK_FORMAT_R16G16B16A16_SNORM, VK_FORMAT_R16G16B16A16_SNORM, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RGBA16S
{ VK_FORMAT_R32G32B32A32_SINT, VK_FORMAT_R32G32B32A32_SINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RGBA32I
{ VK_FORMAT_R32G32B32A32_UINT, VK_FORMAT_R32G32B32A32_UINT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RGBA32U
{ VK_FORMAT_R32G32B32A32_SFLOAT, VK_FORMAT_R32G32B32A32_SFLOAT, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RGBA32F
{ VK_FORMAT_R5G6B5_UNORM_PACK16, VK_FORMAT_R5G6B5_UNORM_PACK16, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // B5G6R5
{ VK_FORMAT_B5G6R5_UNORM_PACK16, VK_FORMAT_B5G6R5_UNORM_PACK16, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // R5G6B5
{ VK_FORMAT_B4G4R4A4_UNORM_PACK16, VK_FORMAT_B4G4R4A4_UNORM_PACK16, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $G, $R, $A, $B } }, // BGRA4
{ VK_FORMAT_R4G4B4A4_UNORM_PACK16, VK_FORMAT_R4G4B4A4_UNORM_PACK16, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $A, $B, $G, $R } }, // RGBA4
{ VK_FORMAT_A1R5G5B5_UNORM_PACK16, VK_FORMAT_A1R5G5B5_UNORM_PACK16, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // BGR5A1
{ VK_FORMAT_A1R5G5B5_UNORM_PACK16, VK_FORMAT_A1R5G5B5_UNORM_PACK16, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $B, $G, $R, $A } }, // RGB5A1
{ VK_FORMAT_A2R10G10B10_UNORM_PACK32, VK_FORMAT_A2R10G10B10_UNORM_PACK32, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $B, $G, $R, $A } }, // RGB10A2
{ VK_FORMAT_B10G11R11_UFLOAT_PACK32, VK_FORMAT_B10G11R11_UFLOAT_PACK32, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // RG11B10F
{ VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // UnknownDepth
{ VK_FORMAT_UNDEFINED, VK_FORMAT_R16_UNORM, VK_FORMAT_D16_UNORM, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // D16
{ VK_FORMAT_UNDEFINED, VK_FORMAT_R32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // D24
{ VK_FORMAT_UNDEFINED, VK_FORMAT_R32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // D24S8
{ VK_FORMAT_UNDEFINED, VK_FORMAT_R32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // D32
{ VK_FORMAT_UNDEFINED, VK_FORMAT_R32_SFLOAT, VK_FORMAT_D32_SFLOAT, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // D16F
{ VK_FORMAT_UNDEFINED, VK_FORMAT_R32_SFLOAT, VK_FORMAT_D32_SFLOAT, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // D24F
{ VK_FORMAT_UNDEFINED, VK_FORMAT_R32_SFLOAT, VK_FORMAT_D32_SFLOAT, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // D32F
{ VK_FORMAT_UNDEFINED, VK_FORMAT_R8_UINT, VK_FORMAT_S8_UINT, VK_FORMAT_UNDEFINED, { $_, $_, $_, $_ } }, // D0S8
#undef $_
#undef $0
#undef $1
#undef $R
#undef $G
#undef $B
#undef $A
};
static_assert(TextureFormat::Count == BX_COUNTOF(s_textureFormat) );
struct ImageTest
{
VkImageType type;
VkImageUsageFlags usage;
VkImageCreateFlags flags;
uint32_t formatCaps[2];
};
static const ImageTest s_imageTest[] =
{
{ VK_IMAGE_TYPE_2D, VK_IMAGE_USAGE_SAMPLED_BIT, 0, { BGFX_CAPS_FORMAT_TEXTURE_2D, BGFX_CAPS_FORMAT_TEXTURE_2D_SRGB } },
{ VK_IMAGE_TYPE_3D, VK_IMAGE_USAGE_SAMPLED_BIT, 0, { BGFX_CAPS_FORMAT_TEXTURE_3D, BGFX_CAPS_FORMAT_TEXTURE_3D_SRGB } },
{ VK_IMAGE_TYPE_2D, VK_IMAGE_USAGE_SAMPLED_BIT, VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT, { BGFX_CAPS_FORMAT_TEXTURE_CUBE, BGFX_CAPS_FORMAT_TEXTURE_CUBE_SRGB } },
{ VK_IMAGE_TYPE_2D, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, 0, { BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER, 0 } },
{ VK_IMAGE_TYPE_2D, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, 0, { BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER, 0 } },
{ VK_IMAGE_TYPE_2D, VK_IMAGE_USAGE_STORAGE_BIT, 0, { BGFX_CAPS_FORMAT_TEXTURE_IMAGE_READ, 0 } },
{ VK_IMAGE_TYPE_2D, VK_IMAGE_USAGE_STORAGE_BIT, 0, { BGFX_CAPS_FORMAT_TEXTURE_IMAGE_WRITE, 0 } },
};
struct LayerInfo
{
bool m_supported;
bool m_initialize;
};
struct Layer
{
enum Enum
{
VK_LAYER_KHRONOS_validation,
VK_LAYER_LUNARG_standard_validation,
Count
};
const char* m_name;
uint32_t m_minVersion;
LayerInfo m_instance;
LayerInfo m_device;
};
// Layer registry
//
static Layer s_layer[] =
{
{ "VK_LAYER_KHRONOS_validation", 1, { false, false }, { false, false } },
{ "VK_LAYER_LUNARG_standard_validation", 1, { false, false }, { false, false } },
{ "", 0, { false, false }, { false, false } },
};
static_assert(Layer::Count == BX_COUNTOF(s_layer)-1);
void updateLayer(const char* _name, uint32_t _version, bool _instanceLayer)
{
bx::StringView layerName(_name);
for (uint32_t ii = 0; ii < Layer::Count; ++ii)
{
Layer& layer = s_layer[ii];
LayerInfo& layerInfo = _instanceLayer
? layer.m_instance
: layer.m_device
;
if (!layerInfo.m_supported && layerInfo.m_initialize)
{
if ( 0 == bx::strCmp(layerName, layer.m_name)
&& _version >= layer.m_minVersion)
{
layerInfo.m_supported = true;
break;
}
}
}
}
struct Extension
{
enum Enum
{
EXT_conservative_rasterization,
EXT_custom_border_color,
EXT_debug_report,
EXT_debug_utils,
EXT_line_rasterization,
EXT_memory_budget,
EXT_shader_viewport_index_layer,
KHR_draw_indirect_count,
KHR_get_physical_device_properties2,
# if BX_PLATFORM_ANDROID
KHR_android_surface,
# elif BX_PLATFORM_LINUX
KHR_wayland_surface,
KHR_xlib_surface,
KHR_xcb_surface,
# elif BX_PLATFORM_WINDOWS
KHR_win32_surface,
# elif BX_PLATFORM_OSX
MVK_macos_surface,
# elif BX_PLATFORM_NX
NN_vi_surface,
# endif
Count
};
const char* m_name;
uint32_t m_minVersion;
bool m_instanceExt;
bool m_supported;
bool m_initialize;
Layer::Enum m_layer;
};
// Extension registry
//
static Extension s_extension[] =
{
{ "VK_EXT_conservative_rasterization", 1, false, false, true, Layer::Count },
{ "VK_EXT_custom_border_color", 1, false, false, true, Layer::Count },
{ "VK_EXT_debug_report", 1, false, false, false, Layer::Count },
{ "VK_EXT_debug_utils", 1, false, false, BGFX_CONFIG_DEBUG_OBJECT_NAME || BGFX_CONFIG_DEBUG_ANNOTATION, Layer::Count },
{ "VK_EXT_line_rasterization", 1, false, false, true, Layer::Count },
{ "VK_EXT_memory_budget", 1, false, false, true, Layer::Count },
{ "VK_EXT_shader_viewport_index_layer", 1, false, false, true, Layer::Count },
{ "VK_KHR_draw_indirect_count", 1, false, false, true, Layer::Count },
{ "VK_KHR_get_physical_device_properties2", 1, false, false, true, Layer::Count },
# if BX_PLATFORM_ANDROID
{ VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, 1, false, false, true, Layer::Count },
# elif BX_PLATFORM_LINUX
{ VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, 1, false, false, true, Layer::Count },
{ VK_KHR_XLIB_SURFACE_EXTENSION_NAME, 1, false, false, true, Layer::Count },
{ VK_KHR_XCB_SURFACE_EXTENSION_NAME, 1, false, false, true, Layer::Count },
# elif BX_PLATFORM_WINDOWS
{ VK_KHR_WIN32_SURFACE_EXTENSION_NAME, 1, false, false, true, Layer::Count },
# elif BX_PLATFORM_OSX
{ VK_MVK_MACOS_SURFACE_EXTENSION_NAME, 1, false, false, true, Layer::Count },
# elif BX_PLATFORM_NX
{ VK_NN_VI_SURFACE_EXTENSION_NAME, 1, false, false, true, Layer::Count },
# endif
};
static_assert(Extension::Count == BX_COUNTOF(s_extension) );
bool updateExtension(const char* _name, uint32_t _version, bool _instanceExt, Extension _extensions[Extension::Count])
{
bool supported = false;
if (BX_ENABLED(BGFX_CONFIG_RENDERER_USE_EXTENSIONS) )
{
const bx::StringView ext(_name);
for (uint32_t ii = 0; ii < Extension::Count; ++ii)
{
Extension& extension = _extensions[ii];
const LayerInfo& layerInfo = _instanceExt
? s_layer[extension.m_layer].m_instance
: s_layer[extension.m_layer].m_device
;
if (!extension.m_supported
&& extension.m_initialize
&& (extension.m_layer == Layer::Count || layerInfo.m_supported) )
{
if ( 0 == bx::strCmp(ext, extension.m_name)
&& _version >= extension.m_minVersion)
{
extension.m_supported = true;
extension.m_instanceExt = _instanceExt;
supported = true;
break;
}
}
}
}
return supported;
}
static const VkFormat s_attribType[][4][2] =
{
{ // Uint8
{ VK_FORMAT_R8_UINT, VK_FORMAT_R8_UNORM },
{ VK_FORMAT_R8G8_UINT, VK_FORMAT_R8G8_UNORM },
{ VK_FORMAT_R8G8B8A8_UINT, VK_FORMAT_R8G8B8A8_UNORM },
{ VK_FORMAT_R8G8B8A8_UINT, VK_FORMAT_R8G8B8A8_UNORM },
},
{ // Uint10
{ VK_FORMAT_A2R10G10B10_UINT_PACK32, VK_FORMAT_A2R10G10B10_UNORM_PACK32 },
{ VK_FORMAT_A2R10G10B10_UINT_PACK32, VK_FORMAT_A2R10G10B10_UNORM_PACK32 },
{ VK_FORMAT_A2R10G10B10_UINT_PACK32, VK_FORMAT_A2R10G10B10_UNORM_PACK32 },
{ VK_FORMAT_A2R10G10B10_UINT_PACK32, VK_FORMAT_A2R10G10B10_UNORM_PACK32 },
},
{ // Int16
{ VK_FORMAT_R16_SINT, VK_FORMAT_R16_SNORM },
{ VK_FORMAT_R16G16_SINT, VK_FORMAT_R16G16_SNORM },
{ VK_FORMAT_R16G16B16_SINT, VK_FORMAT_R16G16B16_SNORM },
{ VK_FORMAT_R16G16B16A16_SINT, VK_FORMAT_R16G16B16A16_SNORM },
},
{ // Half
{ VK_FORMAT_R16_SFLOAT, VK_FORMAT_R16_SFLOAT },
{ VK_FORMAT_R16G16_SFLOAT, VK_FORMAT_R16G16_SFLOAT },
{ VK_FORMAT_R16G16B16_SFLOAT, VK_FORMAT_R16G16B16_SFLOAT },
{ VK_FORMAT_R16G16B16A16_SFLOAT, VK_FORMAT_R16G16B16A16_SFLOAT },
},
{ // Float
{ VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT },
{ VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32_SFLOAT },
{ VK_FORMAT_R32G32B32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT },
{ VK_FORMAT_R32G32B32A32_SFLOAT, VK_FORMAT_R32G32B32A32_SFLOAT },
},
};
static_assert(AttribType::Count == BX_COUNTOF(s_attribType) );
void fillVertexLayout(const ShaderVK* _vsh, VkPipelineVertexInputStateCreateInfo& _vertexInputState, const VertexLayout& _layout)
{
uint32_t numBindings = _vertexInputState.vertexBindingDescriptionCount;
uint32_t numAttribs = _vertexInputState.vertexAttributeDescriptionCount;
VkVertexInputBindingDescription* inputBinding = const_cast<VkVertexInputBindingDescription*>(_vertexInputState.pVertexBindingDescriptions + numBindings);
VkVertexInputAttributeDescription* inputAttrib = const_cast<VkVertexInputAttributeDescription*>(_vertexInputState.pVertexAttributeDescriptions + numAttribs);
inputBinding->binding = numBindings;
inputBinding->stride = _layout.m_stride;
inputBinding->inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
for (uint32_t attr = 0; attr < Attrib::Count; ++attr)
{
if (UINT16_MAX != _layout.m_attributes[attr])
{
inputAttrib->location = _vsh->m_attrRemap[attr];
inputAttrib->binding = numBindings;
uint8_t num;
AttribType::Enum type;
bool normalized;
bool asInt;
_layout.decode(Attrib::Enum(attr), num, type, normalized, asInt);
inputAttrib->format = s_attribType[type][num-1][normalized];
inputAttrib->offset = _layout.m_offset[attr];
++inputAttrib;
++numAttribs;
}
}
_vertexInputState.vertexBindingDescriptionCount = numBindings + 1;
_vertexInputState.vertexAttributeDescriptionCount = numAttribs;
}
void fillInstanceBinding(const ShaderVK* _vsh, VkPipelineVertexInputStateCreateInfo& _vertexInputState, uint32_t _numInstanceData)
{
BX_UNUSED(_vsh);
uint32_t numBindings = _vertexInputState.vertexBindingDescriptionCount;
uint32_t numAttribs = _vertexInputState.vertexAttributeDescriptionCount;
VkVertexInputBindingDescription* inputBinding = const_cast<VkVertexInputBindingDescription*>(_vertexInputState.pVertexBindingDescriptions + numBindings);
VkVertexInputAttributeDescription* inputAttrib = const_cast<VkVertexInputAttributeDescription*>(_vertexInputState.pVertexAttributeDescriptions + numAttribs);
inputBinding->binding = numBindings;
inputBinding->stride = _numInstanceData * 16;
inputBinding->inputRate = VK_VERTEX_INPUT_RATE_INSTANCE;
for (uint32_t inst = 0; inst < _numInstanceData; ++inst)
{
inputAttrib->location = numAttribs;
inputAttrib->binding = numBindings;
inputAttrib->format = VK_FORMAT_R32G32B32A32_SFLOAT;
inputAttrib->offset = inst * 16;
++numAttribs;
++inputAttrib;
}
_vertexInputState.vertexBindingDescriptionCount = numBindings + 1;
_vertexInputState.vertexAttributeDescriptionCount = numAttribs;
}
static const char* s_deviceTypeName[] =
{
"Other",
"Integrated GPU",
"Discrete GPU",
"Virtual GPU",
"CPU",
"Unknown?!"
};
const char* getName(VkPhysicalDeviceType _type)
{
return s_deviceTypeName[bx::min<int32_t>(_type, BX_COUNTOF(s_deviceTypeName)-1 )];
}
static const char* s_allocScopeName[] =
{
"vkCommand",
"vkObject",
"vkCache",
"vkDevice",
"vkInstance",
};
static_assert(VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE == BX_COUNTOF(s_allocScopeName)-1);
constexpr size_t kMinAlignment = 16;
static void* VKAPI_PTR allocationFunction(void* _userData, size_t _size, size_t _alignment, VkSystemAllocationScope _allocationScope)
{
bx::AllocatorI* allocator = (bx::AllocatorI*)_userData;
return bx::alignedAlloc(allocator, _size, bx::max(kMinAlignment, _alignment), bx::Location(s_allocScopeName[_allocationScope], 0) );
}
static void* VKAPI_PTR reallocationFunction(void* _userData, void* _ptr, size_t _size, size_t _alignment, VkSystemAllocationScope _allocationScope)
{
bx::AllocatorI* allocator = (bx::AllocatorI*)_userData;
BX_UNUSED(_userData);
if (0 == _size)
{
bx::alignedFree(allocator, _ptr, 0);
return NULL;
}
return bx::alignedRealloc(allocator, _ptr, _size, bx::max(kMinAlignment, _alignment), bx::Location(s_allocScopeName[_allocationScope], 0) );
}
static void VKAPI_PTR freeFunction(void* _userData, void* _ptr)
{
if (NULL == _ptr)
{
return;
}
bx::AllocatorI* allocator = (bx::AllocatorI*)_userData;
bx::alignedFree(allocator, _ptr, kMinAlignment);
}
static void VKAPI_PTR internalAllocationNotification(void* _userData, size_t _size, VkInternalAllocationType _allocationType, VkSystemAllocationScope _allocationScope)
{
BX_UNUSED(_userData, _size, _allocationType, _allocationScope);
}
static void VKAPI_PTR internalFreeNotification(void* _userData, size_t _size, VkInternalAllocationType _allocationType, VkSystemAllocationScope _allocationScope)
{
BX_UNUSED(_userData, _size, _allocationType, _allocationScope);
}
static VkAllocationCallbacks s_allocationCb =
{
NULL,
allocationFunction,
reallocationFunction,
freeFunction,
internalAllocationNotification,
internalFreeNotification,
};
VkResult VKAPI_PTR stubSetDebugUtilsObjectNameEXT(VkDevice _device, const VkDebugUtilsObjectNameInfoEXT* _nameInfo)
{
BX_UNUSED(_device, _nameInfo);
return VK_SUCCESS;
}
void VKAPI_PTR stubCmdInsertDebugUtilsLabelEXT(VkCommandBuffer _commandBuffer, const VkDebugUtilsLabelEXT* _labelInfo)
{
BX_UNUSED(_commandBuffer, _labelInfo);
}
void VKAPI_PTR stubCmdBeginDebugUtilsLabelEXT(VkCommandBuffer _commandBuffer, const VkDebugUtilsLabelEXT* _labelInfo)
{
BX_UNUSED(_commandBuffer, _labelInfo);
}
void VKAPI_PTR stubCmdEndDebugUtilsLabelEXT(VkCommandBuffer _commandBuffer)
{
BX_UNUSED(_commandBuffer);
}
static const char* s_debugReportObjectType[] =
{
"Unknown",
"Instance",
"PhysicalDevice",
"Device",
"Queue",
"Semaphore",
"CommandBuffer",
"Fence",
"DeviceMemory",
"Buffer",
"Image",
"Event",
"QueryPool",
"BufferView",
"ImageView",
"ShaderModule",
"PipelineCache",
"PipelineLayout",
"RenderPass",
"Pipeline",
"DescriptorSetLayout",
"Sampler",
"DescriptorPool",
"DescriptorSet",
"Framebuffer",
"CommandPool",
"SurfaceKHR",
"SwapchainKHR",
"DebugReport",
};
VkBool32 VKAPI_PTR debugReportCb(
VkDebugReportFlagsEXT _flags
, VkDebugReportObjectTypeEXT _objectType
, uint64_t _object
, size_t _location
, int32_t _messageCode
, const char* _layerPrefix
, const char* _message
, void* _userData
)
{
BX_UNUSED(_flags, _objectType, _object, _location, _messageCode, _layerPrefix, _message, _userData, s_debugReportObjectType);
// For more info about 'VUID-VkSwapchainCreateInfoKHR-imageExtent-01274'
// check https://github.com/KhronosGroup/Vulkan-Docs/issues/1144
if (!bx::strFind(_message, "PointSizeMissing").isEmpty()
|| !bx::strFind(_message, "SwapchainTooManyImages").isEmpty()
|| !bx::strFind(_message, "SwapchainImageNotAcquired").isEmpty()
|| !bx::strFind(_message, "VUID-VkSwapchainCreateInfoKHR-imageExtent-01274").isEmpty() )
{
return VK_FALSE;
}
BX_TRACE("%c%c%c%c%c %19s, %s, %d: %s"
, 0 != (_flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT ) ? 'I' : '-'
, 0 != (_flags & VK_DEBUG_REPORT_WARNING_BIT_EXT ) ? 'W' : '-'
, 0 != (_flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) ? 'P' : '-'
, 0 != (_flags & VK_DEBUG_REPORT_ERROR_BIT_EXT ) ? 'E' : '-'
, 0 != (_flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT ) ? 'D' : '-'
, s_debugReportObjectType[_objectType]
, _layerPrefix
, _messageCode
, _message
);
return VK_FALSE;
}
VkResult enumerateLayerProperties(VkPhysicalDevice _physicalDevice, uint32_t* _propertyCount, VkLayerProperties* _properties)
{
return (VK_NULL_HANDLE == _physicalDevice)
? vkEnumerateInstanceLayerProperties(_propertyCount, _properties)
: vkEnumerateDeviceLayerProperties(_physicalDevice, _propertyCount, _properties)
;
}
VkResult enumerateExtensionProperties(VkPhysicalDevice _physicalDevice, const char* _layerName, uint32_t* _propertyCount, VkExtensionProperties* _properties)
{
return (VK_NULL_HANDLE == _physicalDevice)
? vkEnumerateInstanceExtensionProperties(_layerName, _propertyCount, _properties)
: vkEnumerateDeviceExtensionProperties(_physicalDevice, _layerName, _propertyCount, _properties)
;
}
void dumpExtensions(VkPhysicalDevice _physicalDevice, Extension _extensions[Extension::Count])
{
{ // Global extensions.
uint32_t numExtensionProperties;
VkResult result = enumerateExtensionProperties(_physicalDevice
, NULL
, &numExtensionProperties
, NULL
);
if (VK_SUCCESS == result
&& 0 < numExtensionProperties)
{
VkExtensionProperties* extensionProperties = (VkExtensionProperties*)bx::alloc(g_allocator, numExtensionProperties * sizeof(VkExtensionProperties) );
result = enumerateExtensionProperties(_physicalDevice
, NULL
, &numExtensionProperties
, extensionProperties
);
BX_TRACE("Global extensions (%d):"
, numExtensionProperties
);
for (uint32_t extension = 0; extension < numExtensionProperties; ++extension)
{
bool supported = updateExtension(
extensionProperties[extension].extensionName
, extensionProperties[extension].specVersion
, VK_NULL_HANDLE == _physicalDevice
, _extensions
);
BX_TRACE("\tv%-3d %s%s"
, extensionProperties[extension].specVersion
, extensionProperties[extension].extensionName
, supported ? " (supported)" : "", extensionProperties[extension].extensionName
);
BX_UNUSED(supported);
}
bx::free(g_allocator, extensionProperties);
}
}
// Layer extensions.
uint32_t numLayerProperties;
VkResult result = enumerateLayerProperties(_physicalDevice, &numLayerProperties, NULL);
if (VK_SUCCESS == result
&& 0 < numLayerProperties)
{
VkLayerProperties* layerProperties = (VkLayerProperties*)bx::alloc(g_allocator, numLayerProperties * sizeof(VkLayerProperties) );
result = enumerateLayerProperties(_physicalDevice, &numLayerProperties, layerProperties);
char indent = VK_NULL_HANDLE == _physicalDevice ? '\0' : '\t';
BX_UNUSED(indent);
BX_TRACE("%cLayer extensions (%d):"
, indent
, numLayerProperties
);
for (uint32_t layer = 0; layer < numLayerProperties; ++layer)
{
updateLayer(
layerProperties[layer].layerName
, layerProperties[layer].implementationVersion
, VK_NULL_HANDLE == _physicalDevice
);
BX_TRACE("%c\t%s (s: 0x%08x, i: 0x%08x), %s"
, indent
, layerProperties[layer].layerName
, layerProperties[layer].specVersion
, layerProperties[layer].implementationVersion
, layerProperties[layer].description
);
uint32_t numExtensionProperties;
result = enumerateExtensionProperties(_physicalDevice
, layerProperties[layer].layerName
, &numExtensionProperties
, NULL
);
if (VK_SUCCESS == result
&& 0 < numExtensionProperties)
{
VkExtensionProperties* extensionProperties = (VkExtensionProperties*)bx::alloc(g_allocator, numExtensionProperties * sizeof(VkExtensionProperties) );
result = enumerateExtensionProperties(_physicalDevice
, layerProperties[layer].layerName
, &numExtensionProperties
, extensionProperties
);
for (uint32_t extension = 0; extension < numExtensionProperties; ++extension)
{
const bool supported = updateExtension(
extensionProperties[extension].extensionName
, extensionProperties[extension].specVersion
, VK_NULL_HANDLE == _physicalDevice
, _extensions
);
BX_TRACE("%c\t\t%s (s: 0x%08x)"
, indent
, extensionProperties[extension].extensionName
, extensionProperties[extension].specVersion
, supported ? " (supported)" : "", extensionProperties[extension].extensionName
);
BX_UNUSED(supported);
}
bx::free(g_allocator, extensionProperties);
}
}
bx::free(g_allocator, layerProperties);
}
}
const char* getName(VkResult _result)
{
switch (_result)
{
#define VKENUM(_ty) case _ty: return #_ty
VKENUM(VK_SUCCESS);
VKENUM(VK_NOT_READY);
VKENUM(VK_TIMEOUT);
VKENUM(VK_EVENT_SET);
VKENUM(VK_EVENT_RESET);
VKENUM(VK_INCOMPLETE);
VKENUM(VK_ERROR_OUT_OF_HOST_MEMORY);
VKENUM(VK_ERROR_OUT_OF_DEVICE_MEMORY);
VKENUM(VK_ERROR_OUT_OF_POOL_MEMORY);
VKENUM(VK_ERROR_FRAGMENTED_POOL);
VKENUM(VK_ERROR_INITIALIZATION_FAILED);
VKENUM(VK_ERROR_DEVICE_LOST);
VKENUM(VK_ERROR_MEMORY_MAP_FAILED);
VKENUM(VK_ERROR_LAYER_NOT_PRESENT);
VKENUM(VK_ERROR_EXTENSION_NOT_PRESENT);
VKENUM(VK_ERROR_FEATURE_NOT_PRESENT);
VKENUM(VK_ERROR_INCOMPATIBLE_DRIVER);
VKENUM(VK_ERROR_TOO_MANY_OBJECTS);
VKENUM(VK_ERROR_FORMAT_NOT_SUPPORTED);
VKENUM(VK_ERROR_SURFACE_LOST_KHR);
VKENUM(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
VKENUM(VK_SUBOPTIMAL_KHR);
VKENUM(VK_ERROR_OUT_OF_DATE_KHR);
VKENUM(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR);
VKENUM(VK_ERROR_VALIDATION_FAILED_EXT);
#undef VKENUM
default: break;
}
BX_WARN(false, "Unknown VkResult? %x", _result);
return "<VkResult?>";
}
template<typename Ty>
constexpr VkObjectType getType();
template<> VkObjectType getType<VkBuffer >() { return VK_OBJECT_TYPE_BUFFER; }
template<> VkObjectType getType<VkCommandPool >() { return VK_OBJECT_TYPE_COMMAND_POOL; }
template<> VkObjectType getType<VkDescriptorPool >() { return VK_OBJECT_TYPE_DESCRIPTOR_POOL; }
template<> VkObjectType getType<VkDescriptorSet >() { return VK_OBJECT_TYPE_DESCRIPTOR_SET; }
template<> VkObjectType getType<VkDescriptorSetLayout>() { return VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT; }
template<> VkObjectType getType<VkDeviceMemory >() { return VK_OBJECT_TYPE_DEVICE_MEMORY; }
template<> VkObjectType getType<VkFence >() { return VK_OBJECT_TYPE_FENCE; }
template<> VkObjectType getType<VkFramebuffer >() { return VK_OBJECT_TYPE_FRAMEBUFFER; }
template<> VkObjectType getType<VkImage >() { return VK_OBJECT_TYPE_IMAGE; }
template<> VkObjectType getType<VkImageView >() { return VK_OBJECT_TYPE_IMAGE_VIEW; }
template<> VkObjectType getType<VkPipeline >() { return VK_OBJECT_TYPE_PIPELINE; }
template<> VkObjectType getType<VkPipelineCache >() { return VK_OBJECT_TYPE_PIPELINE_CACHE; }
template<> VkObjectType getType<VkPipelineLayout >() { return VK_OBJECT_TYPE_PIPELINE_LAYOUT; }
template<> VkObjectType getType<VkQueryPool >() { return VK_OBJECT_TYPE_QUERY_POOL; }
template<> VkObjectType getType<VkRenderPass >() { return VK_OBJECT_TYPE_RENDER_PASS; }
template<> VkObjectType getType<VkSampler >() { return VK_OBJECT_TYPE_SAMPLER; }
template<> VkObjectType getType<VkSemaphore >() { return VK_OBJECT_TYPE_SEMAPHORE; }
template<> VkObjectType getType<VkShaderModule >() { return VK_OBJECT_TYPE_SHADER_MODULE; }
template<> VkObjectType getType<VkSurfaceKHR >() { return VK_OBJECT_TYPE_SURFACE_KHR; }
template<> VkObjectType getType<VkSwapchainKHR >() { return VK_OBJECT_TYPE_SWAPCHAIN_KHR; }
template<typename Ty>
static BX_NO_INLINE void setDebugObjectName(VkDevice _device, Ty _object, const char* _format, ...)
{
if (BX_ENABLED(BGFX_CONFIG_DEBUG_OBJECT_NAME)
&& s_extension[Extension::EXT_debug_utils].m_supported)
{
char temp[2048];
va_list argList;
va_start(argList, _format);
int32_t size = bx::min<int32_t>(sizeof(temp)-1, bx::vsnprintf(temp, sizeof(temp), _format, argList) );
va_end(argList);
temp[size] = '\0';
VkDebugUtilsObjectNameInfoEXT ni;
ni.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
ni.pNext = NULL;
ni.objectType = getType<Ty>();
ni.objectHandle = uint64_t(_object.vk);
ni.pObjectName = temp;
VK_CHECK(vkSetDebugUtilsObjectNameEXT(_device, &ni) );
}
}
void setMemoryBarrier(
VkCommandBuffer _commandBuffer
, VkPipelineStageFlags _srcStages
, VkPipelineStageFlags _dstStages
)
{
VkMemoryBarrier mb;
mb.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
mb.pNext = NULL;
mb.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT;
mb.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
vkCmdPipelineBarrier(
_commandBuffer
, _srcStages
, _dstStages
, 0
, 1
, &mb
, 0
, NULL
, 0
, NULL
);
}
void setImageMemoryBarrier(
VkCommandBuffer _commandBuffer
, VkImage _image
, VkImageAspectFlags _aspectMask
, VkImageLayout _oldLayout
, VkImageLayout _newLayout
, uint32_t _baseMipLevel = 0
, uint32_t _levelCount = VK_REMAINING_MIP_LEVELS
, uint32_t _baseArrayLayer = 0
, uint32_t _layerCount = VK_REMAINING_ARRAY_LAYERS
)
{
BX_ASSERT(true
&& _newLayout != VK_IMAGE_LAYOUT_UNDEFINED
&& _newLayout != VK_IMAGE_LAYOUT_PREINITIALIZED
, "_newLayout cannot use VK_IMAGE_LAYOUT_UNDEFINED or VK_IMAGE_LAYOUT_PREINITIALIZED."
);
constexpr VkPipelineStageFlags depthStageMask = 0
| VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT
| VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT
;
constexpr VkPipelineStageFlags sampledStageMask = 0
| VK_PIPELINE_STAGE_VERTEX_SHADER_BIT
| VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
| VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT
;