-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
vulkan_context.cpp
2928 lines (2552 loc) · 114 KB
/
vulkan_context.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
/**************************************************************************/
/* vulkan_context.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 "vulkan_context.h"
#include "core/config/engine.h"
#include "core/config/project_settings.h"
#include "core/string/ustring.h"
#include "core/templates/local_vector.h"
#include "core/version.h"
#include "servers/rendering/rendering_device.h"
#include "vk_enum_string_helper.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define APP_SHORT_NAME "GodotEngine"
VulkanHooks *VulkanContext::vulkan_hooks = nullptr;
Vector<VkAttachmentReference> VulkanContext::_convert_VkAttachmentReference2(uint32_t p_count, const VkAttachmentReference2 *p_refs) {
Vector<VkAttachmentReference> att_refs;
if (p_refs != nullptr) {
for (uint32_t i = 0; i < p_count; i++) {
// We lose aspectMask in this conversion but we don't use it currently.
VkAttachmentReference ref = {
p_refs[i].attachment, /* attachment */
p_refs[i].layout /* layout */
};
att_refs.push_back(ref);
}
}
return att_refs;
}
VkResult VulkanContext::vkCreateRenderPass2KHR(VkDevice p_device, const VkRenderPassCreateInfo2 *p_create_info, const VkAllocationCallbacks *p_allocator, VkRenderPass *p_render_pass) {
if (is_device_extension_enabled(VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME)) {
if (fpCreateRenderPass2KHR == nullptr) {
fpCreateRenderPass2KHR = (PFN_vkCreateRenderPass2KHR)vkGetDeviceProcAddr(p_device, "vkCreateRenderPass2KHR");
}
if (fpCreateRenderPass2KHR == nullptr) {
return VK_ERROR_EXTENSION_NOT_PRESENT;
} else {
return (fpCreateRenderPass2KHR)(p_device, p_create_info, p_allocator, p_render_pass);
}
} else {
// need to fall back on vkCreateRenderPass
const void *next = p_create_info->pNext; // ATM we only support multiview which should work if supported.
Vector<VkAttachmentDescription> attachments;
for (uint32_t i = 0; i < p_create_info->attachmentCount; i++) {
// Basically the old layout just misses type and next.
VkAttachmentDescription att = {
p_create_info->pAttachments[i].flags, /* flags */
p_create_info->pAttachments[i].format, /* format */
p_create_info->pAttachments[i].samples, /* samples */
p_create_info->pAttachments[i].loadOp, /* loadOp */
p_create_info->pAttachments[i].storeOp, /* storeOp */
p_create_info->pAttachments[i].stencilLoadOp, /* stencilLoadOp */
p_create_info->pAttachments[i].stencilStoreOp, /* stencilStoreOp */
p_create_info->pAttachments[i].initialLayout, /* initialLayout */
p_create_info->pAttachments[i].finalLayout /* finalLayout */
};
attachments.push_back(att);
}
Vector<Vector<VkAttachmentReference>> attachment_references;
Vector<VkSubpassDescription> subpasses;
for (uint32_t i = 0; i < p_create_info->subpassCount; i++) {
// Here we need to do more, again it's just stripping out type and next
// but we have VkAttachmentReference2 to convert to VkAttachmentReference.
// Also viewmask is not supported but we don't use it outside of multiview.
Vector<VkAttachmentReference> input_attachments = _convert_VkAttachmentReference2(p_create_info->pSubpasses[i].inputAttachmentCount, p_create_info->pSubpasses[i].pInputAttachments);
Vector<VkAttachmentReference> color_attachments = _convert_VkAttachmentReference2(p_create_info->pSubpasses[i].colorAttachmentCount, p_create_info->pSubpasses[i].pColorAttachments);
Vector<VkAttachmentReference> resolve_attachments = _convert_VkAttachmentReference2(p_create_info->pSubpasses[i].colorAttachmentCount, p_create_info->pSubpasses[i].pResolveAttachments);
Vector<VkAttachmentReference> depth_attachments = _convert_VkAttachmentReference2(p_create_info->pSubpasses[i].colorAttachmentCount, p_create_info->pSubpasses[i].pDepthStencilAttachment);
VkSubpassDescription subpass = {
p_create_info->pSubpasses[i].flags, /* flags */
p_create_info->pSubpasses[i].pipelineBindPoint, /* pipelineBindPoint */
p_create_info->pSubpasses[i].inputAttachmentCount, /* inputAttachmentCount */
input_attachments.size() == 0 ? nullptr : input_attachments.ptr(), /* pInputAttachments */
p_create_info->pSubpasses[i].colorAttachmentCount, /* colorAttachmentCount */
color_attachments.size() == 0 ? nullptr : color_attachments.ptr(), /* pColorAttachments */
resolve_attachments.size() == 0 ? nullptr : resolve_attachments.ptr(), /* pResolveAttachments */
depth_attachments.size() == 0 ? nullptr : depth_attachments.ptr(), /* pDepthStencilAttachment */
p_create_info->pSubpasses[i].preserveAttachmentCount, /* preserveAttachmentCount */
p_create_info->pSubpasses[i].pPreserveAttachments /* pPreserveAttachments */
};
attachment_references.push_back(input_attachments);
attachment_references.push_back(color_attachments);
attachment_references.push_back(resolve_attachments);
attachment_references.push_back(depth_attachments);
subpasses.push_back(subpass);
}
Vector<VkSubpassDependency> dependencies;
for (uint32_t i = 0; i < p_create_info->dependencyCount; i++) {
// We lose viewOffset here but again I don't believe we use this anywhere.
VkSubpassDependency dep = {
p_create_info->pDependencies[i].srcSubpass, /* srcSubpass */
p_create_info->pDependencies[i].dstSubpass, /* dstSubpass */
p_create_info->pDependencies[i].srcStageMask, /* srcStageMask */
p_create_info->pDependencies[i].dstStageMask, /* dstStageMask */
p_create_info->pDependencies[i].srcAccessMask, /* srcAccessMask */
p_create_info->pDependencies[i].dstAccessMask, /* dstAccessMask */
p_create_info->pDependencies[i].dependencyFlags, /* dependencyFlags */
};
dependencies.push_back(dep);
}
// CorrelatedViewMask is not supported in vkCreateRenderPass but we
// currently only use this for multiview.
// We'll need to look into this.
VkRenderPassCreateInfo create_info = {
VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, /* sType */
next, /* pNext*/
p_create_info->flags, /* flags */
(uint32_t)attachments.size(), /* attachmentCount */
attachments.ptr(), /* pAttachments */
(uint32_t)subpasses.size(), /* subpassCount */
subpasses.ptr(), /* pSubpasses */
(uint32_t)dependencies.size(), /* */
dependencies.ptr(), /* */
};
return vkCreateRenderPass(device, &create_info, p_allocator, p_render_pass);
}
}
VKAPI_ATTR VkBool32 VKAPI_CALL VulkanContext::_debug_messenger_callback(
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
void *pUserData) {
// This error needs to be ignored because the AMD allocator will mix up memory types on IGP processors.
if (strstr(pCallbackData->pMessage, "Mapping an image with layout") != nullptr &&
strstr(pCallbackData->pMessage, "can result in undefined behavior if this memory is used by the device") != nullptr) {
return VK_FALSE;
}
// This needs to be ignored because Validator is wrong here.
if (strstr(pCallbackData->pMessage, "Invalid SPIR-V binary version 1.3") != nullptr) {
return VK_FALSE;
}
// This needs to be ignored because Validator is wrong here.
if (strstr(pCallbackData->pMessage, "Shader requires flag") != nullptr) {
return VK_FALSE;
}
// This needs to be ignored because Validator is wrong here.
if (strstr(pCallbackData->pMessage, "SPIR-V module not valid: Pointer operand") != nullptr &&
strstr(pCallbackData->pMessage, "must be a memory object") != nullptr) {
return VK_FALSE;
}
if (pCallbackData->pMessageIdName && strstr(pCallbackData->pMessageIdName, "UNASSIGNED-CoreValidation-DrawState-ClearCmdBeforeDraw") != nullptr) {
return VK_FALSE;
}
String type_string;
switch (messageType) {
case (VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT):
type_string = "GENERAL";
break;
case (VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT):
type_string = "VALIDATION";
break;
case (VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT):
type_string = "PERFORMANCE";
break;
case (VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT):
type_string = "VALIDATION|PERFORMANCE";
break;
}
String objects_string;
if (pCallbackData->objectCount > 0) {
objects_string = "\n\tObjects - " + String::num_int64(pCallbackData->objectCount);
for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) {
objects_string +=
"\n\t\tObject[" + String::num_int64(object) + "]" +
" - " + string_VkObjectType(pCallbackData->pObjects[object].objectType) +
", Handle " + String::num_int64(pCallbackData->pObjects[object].objectHandle);
if (nullptr != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) {
objects_string += ", Name \"" + String(pCallbackData->pObjects[object].pObjectName) + "\"";
}
}
}
String labels_string;
if (pCallbackData->cmdBufLabelCount > 0) {
labels_string = "\n\tCommand Buffer Labels - " + String::num_int64(pCallbackData->cmdBufLabelCount);
for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) {
labels_string +=
"\n\t\tLabel[" + String::num_int64(cmd_buf_label) + "]" +
" - " + pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName +
"{ ";
for (int color_idx = 0; color_idx < 4; ++color_idx) {
labels_string += String::num(pCallbackData->pCmdBufLabels[cmd_buf_label].color[color_idx]);
if (color_idx < 3) {
labels_string += ", ";
}
}
labels_string += " }";
}
}
String error_message(type_string +
" - Message Id Number: " + String::num_int64(pCallbackData->messageIdNumber) +
" | Message Id Name: " + pCallbackData->pMessageIdName +
"\n\t" + pCallbackData->pMessage +
objects_string + labels_string);
// Convert VK severity to our own log macros.
switch (messageSeverity) {
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT:
print_verbose(error_message);
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT:
print_line(error_message);
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT:
WARN_PRINT(error_message);
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT:
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;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_FLAG_BITS_MAX_ENUM_EXT:
break; // Shouldn't happen, only handling to make compilers happy.
}
return VK_FALSE;
}
VKAPI_ATTR VkBool32 VKAPI_CALL VulkanContext::_debug_report_callback(
VkDebugReportFlagsEXT flags,
VkDebugReportObjectTypeEXT objectType,
uint64_t object,
size_t location,
int32_t messageCode,
const char *pLayerPrefix,
const char *pMessage,
void *pUserData) {
String debugMessage = String("Vulkan Debug Report: object - ") +
String::num_int64(object) + "\n" + pMessage;
switch (flags) {
case VK_DEBUG_REPORT_DEBUG_BIT_EXT:
case VK_DEBUG_REPORT_INFORMATION_BIT_EXT:
print_line(debugMessage);
break;
case VK_DEBUG_REPORT_WARNING_BIT_EXT:
case VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT:
WARN_PRINT(debugMessage);
break;
case VK_DEBUG_REPORT_ERROR_BIT_EXT:
ERR_PRINT(debugMessage);
break;
}
return VK_FALSE;
}
VkBool32 VulkanContext::_check_layers(uint32_t check_count, const char *const *check_names, uint32_t layer_count, VkLayerProperties *layers) {
for (uint32_t i = 0; i < check_count; i++) {
VkBool32 found = 0;
for (uint32_t j = 0; j < layer_count; j++) {
if (!strcmp(check_names[i], layers[j].layerName)) {
found = 1;
break;
}
}
if (!found) {
WARN_PRINT("Can't find layer: " + String(check_names[i]));
return 0;
}
}
return 1;
}
Error VulkanContext::_get_preferred_validation_layers(uint32_t *count, const char *const **names) {
static const LocalVector<LocalVector<const char *>> instance_validation_layers_alt{
// Preferred set of validation layers.
{ "VK_LAYER_KHRONOS_validation" },
// Alternative (deprecated, removed in SDK 1.1.126.0) set of validation layers.
{ "VK_LAYER_LUNARG_standard_validation" },
// Alternative (deprecated, removed in SDK 1.1.121.1) set of validation layers.
{ "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation", "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation", "VK_LAYER_GOOGLE_unique_objects" }
};
// Clear out-arguments.
*count = 0;
if (names != nullptr) {
*names = nullptr;
}
VkResult err;
uint32_t instance_layer_count;
err = vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr);
if (err) {
ERR_FAIL_V(ERR_CANT_CREATE);
}
if (instance_layer_count < 1) {
return OK;
}
VkLayerProperties *instance_layers = (VkLayerProperties *)malloc(sizeof(VkLayerProperties) * instance_layer_count);
err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
if (err) {
free(instance_layers);
ERR_FAIL_V(ERR_CANT_CREATE);
}
for (const LocalVector<const char *> &layer : instance_validation_layers_alt) {
if (_check_layers(layer.size(), layer.ptr(), instance_layer_count, instance_layers)) {
*count = layer.size();
if (names != nullptr) {
*names = layer.ptr();
}
break;
}
}
free(instance_layers);
return OK;
}
typedef VkResult(VKAPI_PTR *_vkEnumerateInstanceVersion)(uint32_t *);
Error VulkanContext::_obtain_vulkan_version() {
// https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkApplicationInfo.html#_description
// For Vulkan 1.0 vkEnumerateInstanceVersion is not available, including not in the loader we compile against on Android.
_vkEnumerateInstanceVersion func = (_vkEnumerateInstanceVersion)vkGetInstanceProcAddr(nullptr, "vkEnumerateInstanceVersion");
if (func != nullptr) {
uint32_t api_version;
VkResult res = func(&api_version);
if (res == VK_SUCCESS) {
instance_api_version = api_version;
} else {
// According to the documentation this shouldn't fail with anything except a memory allocation error
// in which case we're in deep trouble anyway.
ERR_FAIL_V(ERR_CANT_CREATE);
}
} else {
print_line("vkEnumerateInstanceVersion not available, assuming Vulkan 1.0.");
instance_api_version = VK_API_VERSION_1_0;
}
return OK;
}
bool VulkanContext::instance_extensions_initialized = false;
HashMap<CharString, bool> VulkanContext::requested_instance_extensions;
void VulkanContext::register_requested_instance_extension(const CharString &extension_name, bool p_required) {
ERR_FAIL_COND_MSG(instance_extensions_initialized, "You can only registered extensions before the Vulkan instance is created");
ERR_FAIL_COND(requested_instance_extensions.has(extension_name));
requested_instance_extensions[extension_name] = p_required;
}
Error VulkanContext::_initialize_instance_extensions() {
enabled_instance_extension_names.clear();
// Make sure our core extensions are here
register_requested_instance_extension(VK_KHR_SURFACE_EXTENSION_NAME, true);
register_requested_instance_extension(_get_platform_surface_extension(), true);
if (_use_validation_layers()) {
register_requested_instance_extension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, false);
}
// This extension allows us to use the properties2 features to query additional device capabilities
register_requested_instance_extension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, false);
// Only enable debug utils in verbose mode or DEV_ENABLED.
// End users would get spammed with messages of varying verbosity due to the
// mess that thirdparty layers/extensions and drivers seem to leave in their
// wake, making the Windows registry a bottomless pit of broken layer JSON.
#ifdef DEV_ENABLED
bool want_debug_utils = true;
#else
bool want_debug_utils = OS::get_singleton()->is_stdout_verbose();
#endif
if (want_debug_utils) {
register_requested_instance_extension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, false);
}
// Load instance extensions that are available...
uint32_t instance_extension_count = 0;
VkResult err = vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, nullptr);
ERR_FAIL_COND_V(err != VK_SUCCESS && err != VK_INCOMPLETE, ERR_CANT_CREATE);
ERR_FAIL_COND_V_MSG(instance_extension_count == 0, ERR_CANT_CREATE, "No instance extensions found, is a driver installed?");
VkExtensionProperties *instance_extensions = (VkExtensionProperties *)malloc(sizeof(VkExtensionProperties) * instance_extension_count);
err = vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, instance_extensions);
if (err != VK_SUCCESS && err != VK_INCOMPLETE) {
free(instance_extensions);
ERR_FAIL_V(ERR_CANT_CREATE);
}
#ifdef DEV_ENABLED
for (uint32_t i = 0; i < instance_extension_count; i++) {
print_verbose(String("VULKAN: Found instance extension ") + String::utf8(instance_extensions[i].extensionName));
}
#endif
// Enable all extensions that are supported and requested
for (uint32_t i = 0; i < instance_extension_count; i++) {
CharString extension_name(instance_extensions[i].extensionName);
if (requested_instance_extensions.has(extension_name)) {
enabled_instance_extension_names.insert(extension_name);
}
}
// Now check our requested extensions
for (KeyValue<CharString, bool> &requested_extension : requested_instance_extensions) {
if (!enabled_instance_extension_names.has(requested_extension.key)) {
if (requested_extension.value) {
free(instance_extensions);
ERR_FAIL_V_MSG(ERR_BUG, String("Required extension ") + String::utf8(requested_extension.key) + String(" not found, is a driver installed?"));
} else {
print_verbose(String("Optional extension ") + String::utf8(requested_extension.key) + String(" not found"));
}
}
}
free(instance_extensions);
instance_extensions_initialized = true;
return OK;
}
bool VulkanContext::device_extensions_initialized = false;
HashMap<CharString, bool> VulkanContext::requested_device_extensions;
void VulkanContext::register_requested_device_extension(const CharString &extension_name, bool p_required) {
ERR_FAIL_COND_MSG(device_extensions_initialized, "You can only registered extensions before the Vulkan instance is created");
ERR_FAIL_COND(requested_device_extensions.has(extension_name));
requested_device_extensions[extension_name] = p_required;
}
Error VulkanContext::_initialize_device_extensions() {
// Look for device extensions.
enabled_device_extension_names.clear();
// Make sure our core extensions are here
register_requested_device_extension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, true);
register_requested_device_extension(VK_KHR_MULTIVIEW_EXTENSION_NAME, false);
register_requested_device_extension(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME, false);
register_requested_device_extension(VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME, false);
register_requested_device_extension(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME, false);
register_requested_device_extension(VK_KHR_STORAGE_BUFFER_STORAGE_CLASS_EXTENSION_NAME, false);
register_requested_device_extension(VK_KHR_16BIT_STORAGE_EXTENSION_NAME, false);
register_requested_device_extension(VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME, false);
register_requested_device_extension(VK_KHR_MAINTENANCE_2_EXTENSION_NAME, false);
register_requested_device_extension(VK_EXT_PIPELINE_CREATION_CACHE_CONTROL_EXTENSION_NAME, false);
register_requested_device_extension(VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME, false);
if (Engine::get_singleton()->is_generate_spirv_debug_info_enabled()) {
register_requested_device_extension(VK_KHR_SHADER_NON_SEMANTIC_INFO_EXTENSION_NAME, true);
}
// TODO consider the following extensions:
// - VK_KHR_spirv_1_4
// - VK_KHR_swapchain_mutable_format
// - VK_EXT_full_screen_exclusive
// - VK_EXT_hdr_metadata
// - VK_KHR_depth_stencil_resolve
// Even though the user "enabled" the extension via the command
// line, we must make sure that it's enumerated for use with the
// device. Therefore, disable it here, and re-enable it again if
// enumerated.
if (VK_KHR_incremental_present_enabled) {
register_requested_device_extension(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, false);
}
if (VK_GOOGLE_display_timing_enabled) {
register_requested_device_extension(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, false);
}
// obtain available device extensions
uint32_t device_extension_count = 0;
VkResult err = vkEnumerateDeviceExtensionProperties(gpu, nullptr, &device_extension_count, nullptr);
ERR_FAIL_COND_V(err, ERR_CANT_CREATE);
ERR_FAIL_COND_V_MSG(device_extension_count == 0, ERR_CANT_CREATE,
"vkEnumerateDeviceExtensionProperties failed to find any extensions\n\n"
"Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
"vkCreateInstance Failure");
VkExtensionProperties *device_extensions = (VkExtensionProperties *)malloc(sizeof(VkExtensionProperties) * device_extension_count);
err = vkEnumerateDeviceExtensionProperties(gpu, nullptr, &device_extension_count, device_extensions);
if (err) {
free(device_extensions);
ERR_FAIL_V(ERR_CANT_CREATE);
}
#ifdef DEV_ENABLED
for (uint32_t i = 0; i < device_extension_count; i++) {
print_verbose(String("VULKAN: Found device extension ") + String::utf8(device_extensions[i].extensionName));
}
#endif
// Enable all extensions that are supported and requested
for (uint32_t i = 0; i < device_extension_count; i++) {
CharString extension_name(device_extensions[i].extensionName);
if (requested_device_extensions.has(extension_name)) {
enabled_device_extension_names.insert(extension_name);
}
}
// Now check our requested extensions
for (KeyValue<CharString, bool> &requested_extension : requested_device_extensions) {
if (!enabled_device_extension_names.has(requested_extension.key)) {
if (requested_extension.value) {
free(device_extensions);
ERR_FAIL_V_MSG(ERR_BUG,
String("vkEnumerateDeviceExtensionProperties failed to find the ") + String::utf8(requested_extension.key) + String(" extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\nvkCreateInstance Failure"));
} else {
print_verbose(String("Optional extension ") + String::utf8(requested_extension.key) + String(" not found"));
}
}
}
free(device_extensions);
device_extensions_initialized = true;
return OK;
}
uint32_t VulkanContext::SubgroupCapabilities::supported_stages_flags_rd() const {
uint32_t flags = 0;
if (supportedStages & VK_SHADER_STAGE_VERTEX_BIT) {
flags += RenderingDevice::ShaderStage::SHADER_STAGE_VERTEX_BIT;
}
if (supportedStages & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT) {
flags += RenderingDevice::ShaderStage::SHADER_STAGE_TESSELATION_CONTROL_BIT;
}
if (supportedStages & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
flags += RenderingDevice::ShaderStage::SHADER_STAGE_TESSELATION_EVALUATION_BIT;
}
// if (supportedStages & VK_SHADER_STAGE_GEOMETRY_BIT) {
// flags += RenderingDevice::ShaderStage::SHADER_STAGE_GEOMETRY_BIT;
// }
if (supportedStages & VK_SHADER_STAGE_FRAGMENT_BIT) {
flags += RenderingDevice::ShaderStage::SHADER_STAGE_FRAGMENT_BIT;
}
if (supportedStages & VK_SHADER_STAGE_COMPUTE_BIT) {
flags += RenderingDevice::ShaderStage::SHADER_STAGE_COMPUTE_BIT;
}
return flags;
}
String VulkanContext::SubgroupCapabilities::supported_stages_desc() const {
String res;
if (supportedStages & VK_SHADER_STAGE_VERTEX_BIT) {
res += ", STAGE_VERTEX";
}
if (supportedStages & VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT) {
res += ", STAGE_TESSELLATION_CONTROL";
}
if (supportedStages & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
res += ", STAGE_TESSELLATION_EVALUATION";
}
if (supportedStages & VK_SHADER_STAGE_GEOMETRY_BIT) {
res += ", STAGE_GEOMETRY";
}
if (supportedStages & VK_SHADER_STAGE_FRAGMENT_BIT) {
res += ", STAGE_FRAGMENT";
}
if (supportedStages & VK_SHADER_STAGE_COMPUTE_BIT) {
res += ", STAGE_COMPUTE";
}
// These are not defined on Android GRMBL.
if (supportedStages & 0x00000100 /* VK_SHADER_STAGE_RAYGEN_BIT_KHR */) {
res += ", STAGE_RAYGEN_KHR";
}
if (supportedStages & 0x00000200 /* VK_SHADER_STAGE_ANY_HIT_BIT_KHR */) {
res += ", STAGE_ANY_HIT_KHR";
}
if (supportedStages & 0x00000400 /* VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR */) {
res += ", STAGE_CLOSEST_HIT_KHR";
}
if (supportedStages & 0x00000800 /* VK_SHADER_STAGE_MISS_BIT_KHR */) {
res += ", STAGE_MISS_KHR";
}
if (supportedStages & 0x00001000 /* VK_SHADER_STAGE_INTERSECTION_BIT_KHR */) {
res += ", STAGE_INTERSECTION_KHR";
}
if (supportedStages & 0x00002000 /* VK_SHADER_STAGE_CALLABLE_BIT_KHR */) {
res += ", STAGE_CALLABLE_KHR";
}
if (supportedStages & 0x00000040 /* VK_SHADER_STAGE_TASK_BIT_NV */) {
res += ", STAGE_TASK_NV";
}
if (supportedStages & 0x00000080 /* VK_SHADER_STAGE_MESH_BIT_NV */) {
res += ", STAGE_MESH_NV";
}
return res.substr(2); // Remove first ", ".
}
uint32_t VulkanContext::SubgroupCapabilities::supported_operations_flags_rd() const {
uint32_t flags = 0;
if (supportedOperations & VK_SUBGROUP_FEATURE_BASIC_BIT) {
flags += RenderingDevice::SubgroupOperations::SUBGROUP_BASIC_BIT;
}
if (supportedOperations & VK_SUBGROUP_FEATURE_VOTE_BIT) {
flags += RenderingDevice::SubgroupOperations::SUBGROUP_VOTE_BIT;
}
if (supportedOperations & VK_SUBGROUP_FEATURE_ARITHMETIC_BIT) {
flags += RenderingDevice::SubgroupOperations::SUBGROUP_ARITHMETIC_BIT;
}
if (supportedOperations & VK_SUBGROUP_FEATURE_BALLOT_BIT) {
flags += RenderingDevice::SubgroupOperations::SUBGROUP_BALLOT_BIT;
}
if (supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_BIT) {
flags += RenderingDevice::SubgroupOperations::SUBGROUP_SHUFFLE_BIT;
}
if (supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT) {
flags += RenderingDevice::SubgroupOperations::SUBGROUP_SHUFFLE_RELATIVE_BIT;
}
if (supportedOperations & VK_SUBGROUP_FEATURE_CLUSTERED_BIT) {
flags += RenderingDevice::SubgroupOperations::SUBGROUP_CLUSTERED_BIT;
}
if (supportedOperations & VK_SUBGROUP_FEATURE_QUAD_BIT) {
flags += RenderingDevice::SubgroupOperations::SUBGROUP_QUAD_BIT;
}
return flags;
}
String VulkanContext::SubgroupCapabilities::supported_operations_desc() const {
String res;
if (supportedOperations & VK_SUBGROUP_FEATURE_BASIC_BIT) {
res += ", FEATURE_BASIC";
}
if (supportedOperations & VK_SUBGROUP_FEATURE_VOTE_BIT) {
res += ", FEATURE_VOTE";
}
if (supportedOperations & VK_SUBGROUP_FEATURE_ARITHMETIC_BIT) {
res += ", FEATURE_ARITHMETIC";
}
if (supportedOperations & VK_SUBGROUP_FEATURE_BALLOT_BIT) {
res += ", FEATURE_BALLOT";
}
if (supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_BIT) {
res += ", FEATURE_SHUFFLE";
}
if (supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT) {
res += ", FEATURE_SHUFFLE_RELATIVE";
}
if (supportedOperations & VK_SUBGROUP_FEATURE_CLUSTERED_BIT) {
res += ", FEATURE_CLUSTERED";
}
if (supportedOperations & VK_SUBGROUP_FEATURE_QUAD_BIT) {
res += ", FEATURE_QUAD";
}
if (supportedOperations & VK_SUBGROUP_FEATURE_PARTITIONED_BIT_NV) {
res += ", FEATURE_PARTITIONED_NV";
}
return res.substr(2); // Remove first ", ".
}
Error VulkanContext::_check_capabilities() {
// https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VK_KHR_multiview.html
// https://www.khronos.org/blog/vulkan-subgroup-tutorial
// For Vulkan 1.0 vkGetPhysicalDeviceProperties2 is not available, including not in the loader we compile against on Android.
// So we check if the functions are accessible by getting their function pointers and skipping if not
// (note that the desktop loader does a better job here but the android loader doesn't.)
// Assume not supported until proven otherwise.
vrs_capabilities.pipeline_vrs_supported = false;
vrs_capabilities.primitive_vrs_supported = false;
vrs_capabilities.attachment_vrs_supported = false;
vrs_capabilities.min_texel_size = Size2i();
vrs_capabilities.max_texel_size = Size2i();
vrs_capabilities.texel_size = Size2i();
multiview_capabilities.is_supported = false;
multiview_capabilities.geometry_shader_is_supported = false;
multiview_capabilities.tessellation_shader_is_supported = false;
multiview_capabilities.max_view_count = 0;
multiview_capabilities.max_instance_count = 0;
subgroup_capabilities.size = 0;
subgroup_capabilities.min_size = 0;
subgroup_capabilities.max_size = 0;
subgroup_capabilities.supportedStages = 0;
subgroup_capabilities.supportedOperations = 0;
subgroup_capabilities.quadOperationsInAllStages = false;
subgroup_capabilities.size_control_is_supported = false;
shader_capabilities.shader_float16_is_supported = false;
shader_capabilities.shader_int8_is_supported = false;
storage_buffer_capabilities.storage_buffer_16_bit_access_is_supported = false;
storage_buffer_capabilities.uniform_and_storage_buffer_16_bit_access_is_supported = false;
storage_buffer_capabilities.storage_push_constant_16_is_supported = false;
storage_buffer_capabilities.storage_input_output_16 = false;
if (is_instance_extension_enabled(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME)) {
// Check for extended features.
PFN_vkGetPhysicalDeviceFeatures2 vkGetPhysicalDeviceFeatures2_func = (PFN_vkGetPhysicalDeviceFeatures2)vkGetInstanceProcAddr(inst, "vkGetPhysicalDeviceFeatures2");
if (vkGetPhysicalDeviceFeatures2_func == nullptr) {
// In Vulkan 1.0 might be accessible under its original extension name.
vkGetPhysicalDeviceFeatures2_func = (PFN_vkGetPhysicalDeviceFeatures2)vkGetInstanceProcAddr(inst, "vkGetPhysicalDeviceFeatures2KHR");
}
if (vkGetPhysicalDeviceFeatures2_func != nullptr) {
// Check our extended features.
void *next = nullptr;
// We must check that the relative extension is present before assuming a
// feature as enabled.
// See also: https://github.com/godotengine/godot/issues/65409
VkPhysicalDeviceVulkan12Features device_features_vk12 = {};
VkPhysicalDeviceShaderFloat16Int8FeaturesKHR shader_features = {};
VkPhysicalDeviceFragmentShadingRateFeaturesKHR vrs_features = {};
VkPhysicalDevice16BitStorageFeaturesKHR storage_feature = {};
VkPhysicalDeviceMultiviewFeatures multiview_features = {};
VkPhysicalDevicePipelineCreationCacheControlFeatures pipeline_cache_control_features = {};
if (device_api_version >= VK_API_VERSION_1_2) {
device_features_vk12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES;
device_features_vk12.pNext = next;
next = &device_features_vk12;
} else {
if (is_device_extension_enabled(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME)) {
shader_features = {
/*sType*/ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES_KHR,
/*pNext*/ next,
/*shaderFloat16*/ false,
/*shaderInt8*/ false,
};
next = &shader_features;
}
}
if (is_device_extension_enabled(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)) {
vrs_features = {
/*sType*/ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_FEATURES_KHR,
/*pNext*/ next,
/*pipelineFragmentShadingRate*/ false,
/*primitiveFragmentShadingRate*/ false,
/*attachmentFragmentShadingRate*/ false,
};
next = &vrs_features;
}
if (is_device_extension_enabled(VK_KHR_16BIT_STORAGE_EXTENSION_NAME)) {
storage_feature = {
/*sType*/ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES_KHR,
/*pNext*/ next,
/*storageBuffer16BitAccess*/ false,
/*uniformAndStorageBuffer16BitAccess*/ false,
/*storagePushConstant16*/ false,
/*storageInputOutput16*/ false,
};
next = &storage_feature;
}
if (is_device_extension_enabled(VK_KHR_MULTIVIEW_EXTENSION_NAME)) {
multiview_features = {
/*sType*/ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES,
/*pNext*/ next,
/*multiview*/ false,
/*multiviewGeometryShader*/ false,
/*multiviewTessellationShader*/ false,
};
next = &multiview_features;
}
if (is_device_extension_enabled(VK_EXT_PIPELINE_CREATION_CACHE_CONTROL_EXTENSION_NAME)) {
pipeline_cache_control_features = {
/*sType*/ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_CREATION_CACHE_CONTROL_FEATURES,
/*pNext*/ next,
/*pipelineCreationCacheControl*/ false,
};
next = &pipeline_cache_control_features;
}
VkPhysicalDeviceFeatures2 device_features;
device_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
device_features.pNext = next;
vkGetPhysicalDeviceFeatures2_func(gpu, &device_features);
if (device_api_version >= VK_API_VERSION_1_2) {
#ifdef MACOS_ENABLED
ERR_FAIL_COND_V_MSG(!device_features_vk12.shaderSampledImageArrayNonUniformIndexing, ERR_CANT_CREATE, "Your GPU doesn't support shaderSampledImageArrayNonUniformIndexing which is required to use the Vulkan-based renderers in Godot.");
#endif
if (is_device_extension_enabled(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME)) {
shader_capabilities.shader_float16_is_supported = device_features_vk12.shaderFloat16;
shader_capabilities.shader_int8_is_supported = device_features_vk12.shaderInt8;
}
} else {
if (is_device_extension_enabled(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME)) {
shader_capabilities.shader_float16_is_supported = shader_features.shaderFloat16;
shader_capabilities.shader_int8_is_supported = shader_features.shaderInt8;
}
}
if (is_device_extension_enabled(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)) {
vrs_capabilities.pipeline_vrs_supported = vrs_features.pipelineFragmentShadingRate;
vrs_capabilities.primitive_vrs_supported = vrs_features.primitiveFragmentShadingRate;
vrs_capabilities.attachment_vrs_supported = vrs_features.attachmentFragmentShadingRate;
}
if (is_device_extension_enabled(VK_KHR_MULTIVIEW_EXTENSION_NAME)) {
multiview_capabilities.is_supported = multiview_features.multiview;
multiview_capabilities.geometry_shader_is_supported = multiview_features.multiviewGeometryShader;
multiview_capabilities.tessellation_shader_is_supported = multiview_features.multiviewTessellationShader;
}
if (is_device_extension_enabled(VK_KHR_16BIT_STORAGE_EXTENSION_NAME)) {
storage_buffer_capabilities.storage_buffer_16_bit_access_is_supported = storage_feature.storageBuffer16BitAccess;
storage_buffer_capabilities.uniform_and_storage_buffer_16_bit_access_is_supported = storage_feature.uniformAndStorageBuffer16BitAccess;
storage_buffer_capabilities.storage_push_constant_16_is_supported = storage_feature.storagePushConstant16;
storage_buffer_capabilities.storage_input_output_16 = storage_feature.storageInputOutput16;
}
if (is_device_extension_enabled(VK_EXT_PIPELINE_CREATION_CACHE_CONTROL_EXTENSION_NAME)) {
pipeline_cache_control_support = pipeline_cache_control_features.pipelineCreationCacheControl;
}
}
// Check extended properties.
PFN_vkGetPhysicalDeviceProperties2 device_properties_func = (PFN_vkGetPhysicalDeviceProperties2)vkGetInstanceProcAddr(inst, "vkGetPhysicalDeviceProperties2");
if (device_properties_func == nullptr) {
// In Vulkan 1.0 might be accessible under its original extension name.
device_properties_func = (PFN_vkGetPhysicalDeviceProperties2)vkGetInstanceProcAddr(inst, "vkGetPhysicalDeviceProperties2KHR");
}
if (device_properties_func != nullptr) {
VkPhysicalDeviceFragmentShadingRatePropertiesKHR vrsProperties{};
VkPhysicalDeviceMultiviewProperties multiviewProperties{};
VkPhysicalDeviceSubgroupProperties subgroupProperties{};
VkPhysicalDeviceSubgroupSizeControlProperties subgroupSizeControlProperties = {};
VkPhysicalDeviceProperties2 physicalDeviceProperties{};
void *nextptr = nullptr;
if (device_api_version >= VK_API_VERSION_1_1) { // Vulkan 1.1 or higher
subgroupProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES;
subgroupProperties.pNext = nextptr;
nextptr = &subgroupProperties;
subgroup_capabilities.size_control_is_supported = is_device_extension_enabled(VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME);
if (subgroup_capabilities.size_control_is_supported) {
subgroupSizeControlProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES;
subgroupSizeControlProperties.pNext = nextptr;
nextptr = &subgroupSizeControlProperties;
}
}
if (multiview_capabilities.is_supported) {
multiviewProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES;
multiviewProperties.pNext = nextptr;
nextptr = &multiviewProperties;
}
if (vrs_capabilities.attachment_vrs_supported) {
vrsProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_PROPERTIES_KHR;
vrsProperties.pNext = nextptr;
nextptr = &vrsProperties;
}
physicalDeviceProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
physicalDeviceProperties.pNext = nextptr;
device_properties_func(gpu, &physicalDeviceProperties);
subgroup_capabilities.size = subgroupProperties.subgroupSize;
subgroup_capabilities.min_size = subgroupProperties.subgroupSize;
subgroup_capabilities.max_size = subgroupProperties.subgroupSize;
subgroup_capabilities.supportedStages = subgroupProperties.supportedStages;
subgroup_capabilities.supportedOperations = subgroupProperties.supportedOperations;
// Note: quadOperationsInAllStages will be true if:
// - supportedStages has VK_SHADER_STAGE_ALL_GRAPHICS + VK_SHADER_STAGE_COMPUTE_BIT.
// - supportedOperations has VK_SUBGROUP_FEATURE_QUAD_BIT.
subgroup_capabilities.quadOperationsInAllStages = subgroupProperties.quadOperationsInAllStages;
if (subgroup_capabilities.size_control_is_supported && (subgroupSizeControlProperties.requiredSubgroupSizeStages & VK_SHADER_STAGE_COMPUTE_BIT)) {
subgroup_capabilities.min_size = subgroupSizeControlProperties.minSubgroupSize;
subgroup_capabilities.max_size = subgroupSizeControlProperties.maxSubgroupSize;
}
if (vrs_capabilities.pipeline_vrs_supported || vrs_capabilities.primitive_vrs_supported || vrs_capabilities.attachment_vrs_supported) {
print_verbose("- Vulkan Variable Rate Shading supported:");
if (vrs_capabilities.pipeline_vrs_supported) {
print_verbose(" Pipeline fragment shading rate");
}
if (vrs_capabilities.primitive_vrs_supported) {
print_verbose(" Primitive fragment shading rate");
}
if (vrs_capabilities.attachment_vrs_supported) {
// TODO expose these somehow to the end user.
vrs_capabilities.min_texel_size.x = vrsProperties.minFragmentShadingRateAttachmentTexelSize.width;
vrs_capabilities.min_texel_size.y = vrsProperties.minFragmentShadingRateAttachmentTexelSize.height;
vrs_capabilities.max_texel_size.x = vrsProperties.maxFragmentShadingRateAttachmentTexelSize.width;
vrs_capabilities.max_texel_size.y = vrsProperties.maxFragmentShadingRateAttachmentTexelSize.height;
// We'll attempt to default to a texel size of 16x16
vrs_capabilities.texel_size.x = CLAMP(16, vrs_capabilities.min_texel_size.x, vrs_capabilities.max_texel_size.x);
vrs_capabilities.texel_size.y = CLAMP(16, vrs_capabilities.min_texel_size.y, vrs_capabilities.max_texel_size.y);
print_verbose(String(" Attachment fragment shading rate") + String(", min texel size: (") + itos(vrs_capabilities.min_texel_size.x) + String(", ") + itos(vrs_capabilities.min_texel_size.y) + String(")") + String(", max texel size: (") + itos(vrs_capabilities.max_texel_size.x) + String(", ") + itos(vrs_capabilities.max_texel_size.y) + String(")"));
}
} else {
print_verbose("- Vulkan Variable Rate Shading not supported");
}
if (multiview_capabilities.is_supported) {
multiview_capabilities.max_view_count = multiviewProperties.maxMultiviewViewCount;
multiview_capabilities.max_instance_count = multiviewProperties.maxMultiviewInstanceIndex;
print_verbose("- Vulkan multiview supported:");
print_verbose(" max view count: " + itos(multiview_capabilities.max_view_count));
print_verbose(" max instances: " + itos(multiview_capabilities.max_instance_count));
} else {
print_verbose("- Vulkan multiview not supported");
}
print_verbose("- Vulkan subgroup:");
print_verbose(" size: " + itos(subgroup_capabilities.size));
print_verbose(" min size: " + itos(subgroup_capabilities.min_size));
print_verbose(" max size: " + itos(subgroup_capabilities.max_size));
print_verbose(" stages: " + subgroup_capabilities.supported_stages_desc());
print_verbose(" supported ops: " + subgroup_capabilities.supported_operations_desc());
if (subgroup_capabilities.quadOperationsInAllStages) {
print_verbose(" quad operations in all stages");
}
} else {
print_verbose("- Couldn't call vkGetPhysicalDeviceProperties2");
}
}
return OK;
}