-
Notifications
You must be signed in to change notification settings - Fork 24
/
DeviceVK.cpp
2100 lines (1709 loc) · 82.9 KB
/
DeviceVK.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
// © 2021 NVIDIA Corporation
#include "SharedVK.h"
#include "CommandQueueVK.h"
#include "CommandAllocatorVK.h"
#include "CommandBufferVK.h"
#include "TextureVK.h"
#include "BufferVK.h"
#include "DescriptorVK.h"
#include "FenceVK.h"
#include "SwapChainVK.h"
#include "QueryPoolVK.h"
#include "DescriptorPoolVK.h"
#include "DescriptorSetVK.h"
#include "PipelineLayoutVK.h"
#include "PipelineVK.h"
#include "AccelerationStructureVK.h"
#include "MemoryVK.h"
constexpr uint32_t INVALID_FAMILY_INDEX = uint32_t(-1);
#ifdef _WIN32
#include <dxgi1_4.h>
#endif
using namespace nri;
Result CreateDeviceVK(const DeviceCreationDesc& deviceCreationDesc, DeviceBase*& device)
{
StdAllocator<uint8_t> allocator(deviceCreationDesc.memoryAllocatorInterface);
DeviceVK* implementation = Allocate<DeviceVK>(allocator, deviceCreationDesc.callbackInterface, allocator);
const Result res = implementation->Create(deviceCreationDesc);
if (res == Result::SUCCESS)
{
device = implementation;
return Result::SUCCESS;
}
Deallocate(allocator, implementation);
return res;
}
Result CreateDeviceVK(const DeviceCreationVKDesc& deviceCreationDesc, DeviceBase*& device)
{
StdAllocator<uint8_t> allocator(deviceCreationDesc.memoryAllocatorInterface);
DeviceVK* implementation = Allocate<DeviceVK>(allocator, deviceCreationDesc.callbackInterface, allocator);
const Result res = implementation->Create(deviceCreationDesc);
if (res == Result::SUCCESS)
{
device = implementation;
return Result::SUCCESS;
}
Deallocate(allocator, implementation);
return res;
}
inline bool IsExtensionSupported(const char* ext, const Vector<VkExtensionProperties>& list)
{
for (auto& e : list)
{
if (!strcmp(ext, e.extensionName))
return true;
}
return false;
}
void* VKAPI_PTR vkAllocateHostMemory(void* pUserData, size_t size, size_t alignment, VkSystemAllocationScope allocationScope)
{
MaybeUnused(allocationScope);
StdAllocator<uint8_t>& stdAllocator = *(StdAllocator<uint8_t>*)pUserData;
const auto& lowLevelAllocator = stdAllocator.GetInterface();
return lowLevelAllocator.Allocate(lowLevelAllocator.userArg, size, alignment);
}
void* VKAPI_PTR vkReallocateHostMemory(void* pUserData, void* pOriginal, size_t size, size_t alignment, VkSystemAllocationScope allocationScope)
{
MaybeUnused(allocationScope);
StdAllocator<uint8_t>& stdAllocator = *(StdAllocator<uint8_t>*)pUserData;
const auto& lowLevelAllocator = stdAllocator.GetInterface();
return lowLevelAllocator.Reallocate(lowLevelAllocator.userArg, pOriginal, size, alignment);
}
void VKAPI_PTR vkFreeHostMemory(void* pUserData, void* pMemory)
{
StdAllocator<uint8_t>& stdAllocator = *(StdAllocator<uint8_t>*)pUserData;
const auto& lowLevelAllocator = stdAllocator.GetInterface();
return lowLevelAllocator.Free(lowLevelAllocator.userArg, pMemory);
}
void VKAPI_PTR vkHostMemoryInternalAllocationNotification(void* pUserData, size_t size, VkInternalAllocationType allocationType,
VkSystemAllocationScope allocationScope)
{
MaybeUnused(pUserData);
MaybeUnused(size);
MaybeUnused(allocationType);
MaybeUnused(allocationScope);
}
void VKAPI_PTR vkHostMemoryInternalFreeNotification(void* pUserData, size_t size, VkInternalAllocationType allocationType,
VkSystemAllocationScope allocationScope)
{
MaybeUnused(pUserData);
MaybeUnused(size);
MaybeUnused(allocationType);
MaybeUnused(allocationScope);
}
template< typename Implementation, typename Interface, typename ... Args >
Result DeviceVK::CreateImplementation(Interface*& entity, const Args&... args)
{
Implementation* implementation = Allocate<Implementation>(GetStdAllocator(), *this);
const Result result = implementation->Create(args...);
if (result == Result::SUCCESS)
{
entity = (Interface*)implementation;
return Result::SUCCESS;
}
Deallocate(GetStdAllocator(), implementation);
return result;
}
DeviceVK::DeviceVK(const CallbackInterface& callbacks, const StdAllocator<uint8_t>& stdAllocator) :
DeviceBase(callbacks, stdAllocator),
m_PhysicalDevices(GetStdAllocator()),
m_PhysicalDeviceIndices(GetStdAllocator()),
m_ConcurrentSharingModeQueueIndices(GetStdAllocator())
{
m_Desc.graphicsAPI = GraphicsAPI::VULKAN;
m_Desc.nriVersionMajor = NRI_VERSION_MAJOR;
m_Desc.nriVersionMinor = NRI_VERSION_MINOR;
}
DeviceVK::~DeviceVK()
{
if (m_Device == VK_NULL_HANDLE)
return;
for (uint32_t i = 0; i < m_Queues.size(); i++)
Deallocate(GetStdAllocator(), m_Queues[i]);
if (m_Messenger)
{
typedef PFN_vkDestroyDebugUtilsMessengerEXT Func;
Func destroyCallback = (Func)m_VK.GetInstanceProcAddr(m_Instance, "vkDestroyDebugUtilsMessengerEXT");
destroyCallback(m_Instance, m_Messenger, m_AllocationCallbackPtr);
}
if (m_OwnsNativeObjects)
{
m_VK.DestroyDevice(m_Device, m_AllocationCallbackPtr);
m_VK.DestroyInstance(m_Instance, m_AllocationCallbackPtr);
}
if (m_Loader)
UnloadSharedLibrary(*m_Loader);
}
void DeviceVK::GetAdapterDesc()
{
VkPhysicalDeviceIDProperties deviceIDProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES };
VkPhysicalDeviceProperties2 props = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &deviceIDProps };
m_VK.GetPhysicalDeviceProperties2(m_PhysicalDevices.front(), &props);
#ifdef _WIN32
static_assert(sizeof(LUID) == VK_LUID_SIZE, "invalid sizeof");
ComPtr<IDXGIFactory4> dxgiFactory;
HRESULT hr = CreateDXGIFactory2(0, IID_PPV_ARGS(&dxgiFactory));
if (FAILED(hr))
REPORT_WARNING(this, "CreateDXGIFactory2() failed, result = 0x%08X!", hr);
ComPtr<IDXGIAdapter> adapter;
LUID luid = *(LUID*)&deviceIDProps.deviceLUID[0];
hr = dxgiFactory->EnumAdapterByLuid(luid, IID_PPV_ARGS(&adapter));
if (FAILED(hr))
REPORT_WARNING(this, "IDXGIFactory4::EnumAdapterByLuid() failed, result = 0x%08X!", hr);
DXGI_ADAPTER_DESC desc = {};
hr = adapter->GetDesc(&desc);
if (FAILED(hr))
REPORT_WARNING(this, "IDXGIAdapter::GetDesc() failed, result = 0x%08X!", hr);
else
{
wcstombs(m_Desc.adapterDesc.description, desc.Description, GetCountOf(m_Desc.adapterDesc.description) - 1);
m_Desc.adapterDesc.luid = *(uint64_t*)&desc.AdapterLuid;
m_Desc.adapterDesc.videoMemorySize = desc.DedicatedVideoMemory;
m_Desc.adapterDesc.systemMemorySize = desc.DedicatedSystemMemory + desc.SharedSystemMemory;
m_Desc.adapterDesc.deviceId = desc.DeviceId;
m_Desc.adapterDesc.vendor = GetVendorFromID(desc.VendorId);
}
#else
strncpy(m_Desc.adapterDesc.description, props.properties.deviceName, sizeof(m_Desc.adapterDesc.description));
m_Desc.adapterDesc.luid = *(uint64_t*)&deviceIDProps.deviceLUID[0];
m_Desc.adapterDesc.deviceId = props.properties.deviceID;
m_Desc.adapterDesc.vendor = GetVendorFromID(props.properties.vendorID);
/* THIS IS AWFUL!
https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPhysicalDeviceMemoryProperties.html
In a unified memory architecture (UMA) system there is often only a single memory heap which is considered to
be equally "local" to the host and to the device, and such an implementation must advertise the heap as device-local. */
for (uint32_t k = 0; k < m_MemoryProps.memoryHeapCount; k++)
{
if (m_MemoryProps.memoryHeaps[k].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)
m_Desc.adapterDesc.videoMemorySize += m_MemoryProps.memoryHeaps[k].size;
else
m_Desc.adapterDesc.systemMemorySize += m_MemoryProps.memoryHeaps[k].size;
}
#endif
}
Result DeviceVK::Create(const DeviceCreationVKDesc& deviceCreationVKDesc)
{
m_OwnsNativeObjects = false;
m_SPIRVBindingOffsets = deviceCreationVKDesc.spirvBindingOffsets;
const char* loaderPath = deviceCreationVKDesc.vulkanLoaderPath ? deviceCreationVKDesc.vulkanLoaderPath : VULKAN_LOADER_NAME;
m_Loader = LoadSharedLibrary(loaderPath);
if (!m_Loader)
{
REPORT_ERROR(this, "Failed to load Vulkan loader: '%s'.", loaderPath);
return Result::UNSUPPORTED;
}
// Create instance
Result res = ResolvePreInstanceDispatchTable();
if (res != Result::SUCCESS)
return res;
m_Instance = (VkInstance)deviceCreationVKDesc.vkInstance;
res = ResolveInstanceDispatchTable();
if (res != Result::SUCCESS)
return res;
// Find physical device
const VkPhysicalDevice* physicalDevices = (VkPhysicalDevice*)deviceCreationVKDesc.vkPhysicalDevices; // TODO: physical device indices?
m_PhysicalDevices.insert(m_PhysicalDevices.begin(), physicalDevices, physicalDevices + deviceCreationVKDesc.deviceGroupSize);
m_VK.GetPhysicalDeviceMemoryProperties(m_PhysicalDevices.front(), &m_MemoryProps);
FillFamilyIndices(true, deviceCreationVKDesc.queueFamilyIndices, deviceCreationVKDesc.queueFamilyIndexNum);
// Get adapter description as early as possible for meaningful error reporting
GetAdapterDesc();
// Create device
m_Device = (VkDevice)deviceCreationVKDesc.vkDevice;
res = ResolveDispatchTable();
if (res != Result::SUCCESS)
return res;
{ // Instance extensions
uint32_t extensionNum = 0;
m_VK.EnumerateInstanceExtensionProperties(nullptr, &extensionNum, nullptr);
Vector<VkExtensionProperties> supportedExts(extensionNum, GetStdAllocator());
m_VK.EnumerateInstanceExtensionProperties(nullptr, &extensionNum, supportedExts.data());
if (IsExtensionSupported(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, supportedExts))
supportedFeatures.debugUtils = true;
}
Vector<const char*> desiredExts(GetStdAllocator());
ProcessDeviceExtensions(desiredExts, false);
// Finalize
CreateCommandQueues();
FillDesc(false);
return FillFunctionTable(m_CoreInterface);
}
Result DeviceVK::Create(const DeviceCreationDesc& deviceCreationDesc)
{
m_OwnsNativeObjects = true;
m_SPIRVBindingOffsets = deviceCreationDesc.spirvBindingOffsets;
m_AllocationCallbacks.pUserData = &GetStdAllocator();
m_AllocationCallbacks.pfnAllocation = vkAllocateHostMemory;
m_AllocationCallbacks.pfnReallocation = vkReallocateHostMemory;
m_AllocationCallbacks.pfnFree = vkFreeHostMemory;
m_AllocationCallbacks.pfnInternalAllocation = vkHostMemoryInternalAllocationNotification;
m_AllocationCallbacks.pfnInternalFree = vkHostMemoryInternalFreeNotification;
if (deviceCreationDesc.enableAPIValidation)
m_AllocationCallbackPtr = &m_AllocationCallbacks;
m_Loader = LoadSharedLibrary(VULKAN_LOADER_NAME);
if (!m_Loader)
{
REPORT_ERROR(this, "Failed to load Vulkan loader: '%s'.", VULKAN_LOADER_NAME);
return Result::UNSUPPORTED;
}
// Create instance
Result res = ResolvePreInstanceDispatchTable();
if (res != Result::SUCCESS)
return res;
res = CreateInstance(deviceCreationDesc);
if (res != Result::SUCCESS)
return res;
res = ResolveInstanceDispatchTable();
if (res != Result::SUCCESS)
return res;
// Find physical device
res = FindPhysicalDeviceGroup(deviceCreationDesc.adapterDesc, deviceCreationDesc.enableMGPU);
if (res != Result::SUCCESS)
return res;
m_VK.GetPhysicalDeviceMemoryProperties(m_PhysicalDevices.front(), &m_MemoryProps);
FillFamilyIndices(false, nullptr, 0);
// Get adapter description as early as possible for meaningful error reporting
GetAdapterDesc();
// Create device
res = CreateLogicalDevice(deviceCreationDesc);
if (res != Result::SUCCESS)
return res;
res = ResolveDispatchTable();
if (res != Result::SUCCESS)
return res;
const uint32_t groupSize = m_Desc.nodeNum;
m_PhysicalDeviceIndices.resize(groupSize * groupSize);
const auto begin = m_PhysicalDeviceIndices.begin();
for (uint32_t i = 0; i < groupSize; i++)
std::fill(begin + i * groupSize, begin + (i + 1) * groupSize, i);
// Finalize
CreateCommandQueues();
FillDesc(deviceCreationDesc.enableAPIValidation);
if (deviceCreationDesc.enableAPIValidation)
ReportDeviceGroupInfo();
return FillFunctionTable(m_CoreInterface);
}
void DeviceVK::ProcessDeviceExtensions(Vector<const char*>& desiredExts, bool disableRayTracing)
{
// Query extensions
uint32_t extensionNum = 0;
m_VK.EnumerateDeviceExtensionProperties(m_PhysicalDevices.front(), nullptr, &extensionNum, nullptr);
Vector<VkExtensionProperties> supportedExts(extensionNum, GetStdAllocator());
m_VK.EnumerateDeviceExtensionProperties(m_PhysicalDevices.front(), nullptr, &extensionNum, supportedExts.data());
// Mandatory
desiredExts.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); // TODO: move to supportedFeatures?
desiredExts.push_back(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME);
desiredExts.push_back(VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME);
desiredExts.push_back(VK_KHR_SHADER_NON_SEMANTIC_INFO_EXTENSION_NAME); // at least for "printf"
#ifdef __APPLE__
desiredExts.push_back(VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME);
desiredExts.push_back(VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME);
#endif
// Optional
if (IsExtensionSupported(VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME, supportedExts))
{
desiredExts.push_back(VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME);
supportedFeatures.sampleLocations = true;
}
if (IsExtensionSupported(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME, supportedExts))
{
desiredExts.push_back(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME);
supportedFeatures.conservativeRaster = true;
}
if (IsExtensionSupported(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME, supportedExts))
{
desiredExts.push_back(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
supportedFeatures.transformFeedback = true;
}
if (IsExtensionSupported(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME, supportedExts))
{
desiredExts.push_back(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME);
supportedFeatures.shadingRate = true;
}
if (IsExtensionSupported(VK_EXT_MESH_SHADER_EXTENSION_NAME, supportedExts))
{
desiredExts.push_back(VK_EXT_MESH_SHADER_EXTENSION_NAME);
supportedFeatures.meshShader = true;
}
if (IsExtensionSupported(VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME, supportedExts))
desiredExts.push_back(VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME);
// RT consumes memory: enable if supported and not disabled, store availability
if (!disableRayTracing)
{
bool isRayTracingSupported = true;
// Mandatory
if (IsExtensionSupported(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME, supportedExts))
desiredExts.push_back(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME);
else
isRayTracingSupported = false;
if (IsExtensionSupported(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME, supportedExts))
desiredExts.push_back(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME);
else
isRayTracingSupported = false;
supportedFeatures.rayTracing = isRayTracingSupported;
// Optional
if (IsExtensionSupported(VK_KHR_RAY_QUERY_EXTENSION_NAME, supportedExts))
desiredExts.push_back(VK_KHR_RAY_QUERY_EXTENSION_NAME);
}
// Dependent stuff
if (supportedFeatures.rayTracing && IsExtensionSupported(VK_EXT_OPACITY_MICROMAP_EXTENSION_NAME, supportedExts))
{
desiredExts.push_back(VK_EXT_OPACITY_MICROMAP_EXTENSION_NAME);
supportedFeatures.opacityMicroMap = true;
}
}
Result DeviceVK::CreateLogicalDevice(const DeviceCreationDesc& deviceCreationDesc)
{
// Process extensions
Vector<const char*> desiredExts(GetStdAllocator());
for (uint32_t i = 0; i < deviceCreationDesc.vulkanExtensions.deviceExtensionNum; i++)
desiredExts.push_back(deviceCreationDesc.vulkanExtensions.deviceExtensions[i]);
ProcessDeviceExtensions(desiredExts, deviceCreationDesc.disableVulkanRayTracing);
// Chain extensions
#define APPEND_EXT(desc) *tail = &desc; tail = &desc.pNext
VkPhysicalDeviceFeatures2 features2 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};
void** tail = &features2.pNext;
VkPhysicalDeviceVulkan11Features features11 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES};
APPEND_EXT(features11);
VkPhysicalDeviceVulkan12Features features12 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES};
APPEND_EXT(features12);
VkPhysicalDeviceVulkan13Features features13 = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES};
APPEND_EXT(features13);
VkPhysicalDeviceTransformFeedbackFeaturesEXT transformFeedbackFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT};
if (supportedFeatures.transformFeedback)
{
APPEND_EXT(transformFeedbackFeatures);
}
VkPhysicalDeviceFragmentShadingRateFeaturesKHR shadingRateFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_FEATURES_KHR};
if (supportedFeatures.shadingRate)
{
APPEND_EXT(shadingRateFeatures);
}
VkPhysicalDeviceRayTracingPipelineFeaturesKHR rayTracingFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR};
VkPhysicalDeviceAccelerationStructureFeaturesKHR accelerationStructureFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR};
VkPhysicalDeviceRayQueryFeaturesKHR rayQueryFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_QUERY_FEATURES_KHR};
if (supportedFeatures.rayTracing)
{
APPEND_EXT(rayTracingFeatures);
APPEND_EXT(accelerationStructureFeatures);
APPEND_EXT(rayQueryFeatures);
}
VkPhysicalDeviceOpacityMicromapFeaturesEXT micromapFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_OPACITY_MICROMAP_FEATURES_EXT};
if (supportedFeatures.opacityMicroMap)
{
APPEND_EXT(micromapFeatures);
}
VkPhysicalDeviceMeshShaderFeaturesEXT meshShaderFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT};
if (supportedFeatures.meshShader)
{
APPEND_EXT(meshShaderFeatures);
}
#undef APPEND_EXT
// Query extensions availability
m_VK.GetPhysicalDeviceFeatures2(m_PhysicalDevices.front(), &features2);
// Disable features here
// ...
// Create device
Vector<VkDeviceQueueCreateInfo> queues(GetStdAllocator());
const float priorities = 1.0f;
for (size_t i = 0; i < m_FamilyIndices.size(); i++)
{
if (m_FamilyIndices[i] == INVALID_FAMILY_INDEX)
continue;
VkDeviceQueueCreateInfo info = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO };
info.queueCount = 1;
info.queueFamilyIndex = m_FamilyIndices[i];
info.pQueuePriorities = &priorities;
queues.push_back(info);
}
VkDeviceCreateInfo deviceCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO };
deviceCreateInfo.pNext = &features2;
deviceCreateInfo.queueCreateInfoCount = (uint32_t)queues.size();
deviceCreateInfo.pQueueCreateInfos = queues.data();
deviceCreateInfo.enabledExtensionCount = (uint32_t)desiredExts.size();
deviceCreateInfo.ppEnabledExtensionNames = desiredExts.data();
VkDeviceGroupDeviceCreateInfo deviceGroupInfo = { VK_STRUCTURE_TYPE_DEVICE_GROUP_DEVICE_CREATE_INFO };
if (m_PhysicalDevices.size() > 1)
{
deviceGroupInfo.pNext = deviceCreateInfo.pNext;
deviceGroupInfo.physicalDeviceCount = (uint32_t)m_PhysicalDevices.size();
deviceGroupInfo.pPhysicalDevices = m_PhysicalDevices.data();
deviceCreateInfo.pNext = &deviceGroupInfo;
}
const VkResult result = m_VK.CreateDevice(m_PhysicalDevices.front(), &deviceCreateInfo, m_AllocationCallbackPtr, &m_Device);
RETURN_ON_FAILURE(this, result == VK_SUCCESS, GetReturnCode(result), "Can't create a device: "
"vkCreateDevice returned %d.", (int32_t)result);
return Result::SUCCESS;
}
bool DeviceVK::GetMemoryType(MemoryLocation memoryLocation, uint32_t memoryTypeMask, MemoryTypeInfo& memoryTypeInfo) const
{
VkMemoryPropertyFlags neededFlags = 0; // must have
VkMemoryPropertyFlags undesiredFlags = 0; // have higher priority than desired
VkMemoryPropertyFlags desiredFlags = 0; // nice to have
if (memoryLocation == MemoryLocation::DEVICE)
{
neededFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
undesiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
}
else if (memoryLocation == MemoryLocation::DEVICE_UPLOAD)
{
neededFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
desiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
undesiredFlags = VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
}
else
{
neededFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
undesiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
desiredFlags = memoryLocation == MemoryLocation::HOST_READBACK ? VK_MEMORY_PROPERTY_HOST_CACHED_BIT : 0;
}
// Phase 1: needed, undesired and desired
for (uint32_t i = 0; i < m_MemoryProps.memoryTypeCount; i++)
{
bool isSupported = memoryTypeMask & (1 << i);
bool hasNeededFlags = (m_MemoryProps.memoryTypes[i].propertyFlags & neededFlags) == neededFlags;
bool hasUndesiredFlags = undesiredFlags == 0 ? false : (m_MemoryProps.memoryTypes[i].propertyFlags & undesiredFlags) == undesiredFlags;
bool hasDesiredFlags = (m_MemoryProps.memoryTypes[i].propertyFlags & desiredFlags) == desiredFlags;
if (isSupported && hasNeededFlags && !hasUndesiredFlags && hasDesiredFlags)
{
memoryTypeInfo.memoryTypeIndex = (MemoryTypeIndexType)i;
memoryTypeInfo.memoryLocation = memoryLocation;
return true;
}
}
// Phase 2: needed and undesired
for (uint32_t i = 0; i < m_MemoryProps.memoryTypeCount; i++)
{
bool isSupported = memoryTypeMask & (1 << i);
bool hasNeededFlags = (m_MemoryProps.memoryTypes[i].propertyFlags & neededFlags) == neededFlags;
bool hasUndesiredFlags = undesiredFlags == 0 ? false : (m_MemoryProps.memoryTypes[i].propertyFlags & undesiredFlags) == undesiredFlags;
if (isSupported && hasNeededFlags && !hasUndesiredFlags)
{
memoryTypeInfo.memoryTypeIndex = (MemoryTypeIndexType)i;
memoryTypeInfo.memoryLocation = memoryLocation;
return true;
}
}
// Phase 3: needed and desired
for (uint32_t i = 0; i < m_MemoryProps.memoryTypeCount; i++)
{
bool isSupported = memoryTypeMask & (1 << i);
bool hasNeededFlags = (m_MemoryProps.memoryTypes[i].propertyFlags & neededFlags) == neededFlags;
bool hasDesiredFlags = (m_MemoryProps.memoryTypes[i].propertyFlags & desiredFlags) == desiredFlags;
if (isSupported && hasNeededFlags && hasDesiredFlags)
{
memoryTypeInfo.memoryTypeIndex = (MemoryTypeIndexType)i;
memoryTypeInfo.memoryLocation = memoryLocation;
return true;
}
}
// Phase 4: only needed
for (uint32_t i = 0; i < m_MemoryProps.memoryTypeCount; i++)
{
bool isSupported = memoryTypeMask & (1 << i);
bool hasNeededFlags = (m_MemoryProps.memoryTypes[i].propertyFlags & neededFlags) == neededFlags;
if (isSupported && hasNeededFlags)
{
memoryTypeInfo.memoryTypeIndex = (MemoryTypeIndexType)i;
memoryTypeInfo.memoryLocation = memoryLocation;
return true;
}
}
return false;
}
bool DeviceVK::GetMemoryTypeByIndex(uint32_t index, MemoryTypeInfo& memoryTypeInfo) const
{
if (index >= m_MemoryProps.memoryTypeCount)
return false;
const VkMemoryType& memoryType = m_MemoryProps.memoryTypes[index];
bool isHostVisible = memoryType.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
bool isDevice = memoryType.propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
memoryTypeInfo.memoryTypeIndex = (MemoryTypeIndexType)index;
if (isDevice)
memoryTypeInfo.memoryLocation = isHostVisible ? MemoryLocation::DEVICE_UPLOAD : MemoryLocation::DEVICE;
else
memoryTypeInfo.memoryLocation = MemoryLocation::HOST_UPLOAD;
return true;
}
const char* GetObjectTypeName(VkObjectType objectType)
{
switch(objectType)
{
case VK_OBJECT_TYPE_INSTANCE:
return "VkInstance";
case VK_OBJECT_TYPE_PHYSICAL_DEVICE:
return "VkPhysicalDevice";
case VK_OBJECT_TYPE_DEVICE:
return "VkDevice";
case VK_OBJECT_TYPE_QUEUE:
return "VkQueue";
case VK_OBJECT_TYPE_SEMAPHORE:
return "VkSemaphore";
case VK_OBJECT_TYPE_COMMAND_BUFFER:
return "VkCommandBuffer";
case VK_OBJECT_TYPE_FENCE:
return "VkFence";
case VK_OBJECT_TYPE_DEVICE_MEMORY:
return "VkDeviceMemory";
case VK_OBJECT_TYPE_BUFFER:
return "VkBuffer";
case VK_OBJECT_TYPE_IMAGE:
return "VkImage";
case VK_OBJECT_TYPE_EVENT:
return "VkEvent";
case VK_OBJECT_TYPE_QUERY_POOL:
return "VkQueryPool";
case VK_OBJECT_TYPE_BUFFER_VIEW:
return "VkBufferView";
case VK_OBJECT_TYPE_IMAGE_VIEW:
return "VkImageView";
case VK_OBJECT_TYPE_SHADER_MODULE:
return "VkShaderModule";
case VK_OBJECT_TYPE_PIPELINE_CACHE:
return "VkPipelineCache";
case VK_OBJECT_TYPE_PIPELINE_LAYOUT:
return "VkPipelineLayout";
case VK_OBJECT_TYPE_RENDER_PASS:
return "VkRenderPass";
case VK_OBJECT_TYPE_PIPELINE:
return "VkPipeline";
case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT:
return "VkDescriptorSetLayout";
case VK_OBJECT_TYPE_SAMPLER:
return "VkSampler";
case VK_OBJECT_TYPE_DESCRIPTOR_POOL:
return "VkDescriptorPool";
case VK_OBJECT_TYPE_DESCRIPTOR_SET:
return "VkDescriptorSet";
case VK_OBJECT_TYPE_FRAMEBUFFER:
return "VkFramebuffer";
case VK_OBJECT_TYPE_COMMAND_POOL:
return "VkCommandPool";
case VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION:
return "VkSamplerYcbcrConversion";
case VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE:
return "VkDescriptorUpdateTemplate";
case VK_OBJECT_TYPE_SURFACE_KHR:
return "VkSurfaceKHR";
case VK_OBJECT_TYPE_SWAPCHAIN_KHR:
return "VkSwapchainKHR";
case VK_OBJECT_TYPE_DISPLAY_KHR:
return "VkDisplayKHR";
case VK_OBJECT_TYPE_DISPLAY_MODE_KHR:
return "VkDisplayModeKHR";
case VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT:
return "VkDebugReportCallbackEXT";
case VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT:
return "VkDebugUtilsMessengerEXT";
case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR:
return "VkAccelerationStructureKHR";
case VK_OBJECT_TYPE_VALIDATION_CACHE_EXT:
return "VkValidationCacheEXT";
case VK_OBJECT_TYPE_DEFERRED_OPERATION_KHR:
return "VkDeferredOperationKHR";
default:
return "unknown";
}
}
VkBool32 VKAPI_PTR DebugUtilsMessenger(
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT* callbackData,
void* userData)
{
MaybeUnused(messageType);
/*
// TODO: some messages can be muted here
if (callbackData->messageIdNumber == XXX)
return VK_FALSE;
*/
Message severity = Message::TYPE_INFO;
if (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
severity = Message::TYPE_ERROR;
else if (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)
severity = Message::TYPE_WARNING;
DeviceVK& device = *(DeviceVK*)userData;
device.ReportMessage(severity, __FILE__, __LINE__, "%s", callbackData->pMessage);
return VK_FALSE;
}
void DeviceVK::FilterInstanceLayers(Vector<const char*>& layers)
{
uint32_t layerNum = 0;
m_VK.EnumerateInstanceLayerProperties(&layerNum, nullptr);
Vector<VkLayerProperties> supportedLayers(layerNum, GetStdAllocator());
m_VK.EnumerateInstanceLayerProperties(&layerNum, supportedLayers.data());
for (size_t i = 0; i < layers.size(); i++)
{
bool found = false;
for (uint32_t j = 0; j < layerNum && !found; j++)
{
if (strcmp(supportedLayers[j].layerName, layers[i]) == 0)
found = true;
}
if (!found)
layers.erase(layers.begin() + i--);
}
}
Result DeviceVK::CreateInstance(const DeviceCreationDesc& deviceCreationDesc)
{
Vector<const char*> layers(GetStdAllocator());
if (deviceCreationDesc.enableAPIValidation)
layers.push_back("VK_LAYER_KHRONOS_validation");
FilterInstanceLayers(layers);
uint32_t extensionNum = 0;
m_VK.EnumerateInstanceExtensionProperties(nullptr, &extensionNum, nullptr);
Vector<VkExtensionProperties> supportedExts(extensionNum, GetStdAllocator());
m_VK.EnumerateInstanceExtensionProperties(nullptr, &extensionNum, supportedExts.data());
Vector<const char*> extensions(GetStdAllocator());
for (uint32_t i = 0; i < deviceCreationDesc.vulkanExtensions.instanceExtensionNum; i++)
extensions.push_back(deviceCreationDesc.vulkanExtensions.instanceExtensions[i]);
extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
extensions.push_back(VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME);
#ifdef VK_USE_PLATFORM_WIN32_KHR
extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_METAL_EXT
extensions.push_back(VK_EXT_METAL_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_XLIB_KHR
extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
#endif
#ifdef VK_USE_PLATFORM_WAYLAND_KHR
extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
#endif
#ifdef __APPLE__
extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
#endif
if (IsExtensionSupported(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, supportedExts))
{
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
supportedFeatures.debugUtils = true;
}
const VkApplicationInfo appInfo = {
VK_STRUCTURE_TYPE_APPLICATION_INFO,
nullptr,
nullptr,
0,
nullptr,
0,
VK_API_VERSION_1_3
};
const VkValidationFeatureEnableEXT enabledValidationFeatures[] = {
VK_VALIDATION_FEATURE_ENABLE_DEBUG_PRINTF_EXT,
};
const VkValidationFeaturesEXT validationFeatures = {
VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT,
nullptr,
GetCountOf(enabledValidationFeatures),
enabledValidationFeatures,
0,
nullptr,
};
const VkInstanceCreateInfo info = {
VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
deviceCreationDesc.enableAPIValidation ? &validationFeatures : nullptr,
#ifdef __APPLE__
(VkInstanceCreateFlags)VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR,
#else
(VkInstanceCreateFlags)0,
#endif
&appInfo,
(uint32_t)layers.size(),
layers.data(),
(uint32_t)extensions.size(),
extensions.data(),
};
VkResult result = m_VK.CreateInstance(&info, m_AllocationCallbackPtr, &m_Instance);
RETURN_ON_FAILURE(this, result == VK_SUCCESS, GetReturnCode(result),
"Can't create a VkInstance: vkCreateInstance returned %d.", (int32_t)result);
if (deviceCreationDesc.enableAPIValidation)
{
typedef PFN_vkCreateDebugUtilsMessengerEXT Func;
Func vkCreateDebugUtilsMessengerEXT = nullptr;
vkCreateDebugUtilsMessengerEXT = (Func)m_VK.GetInstanceProcAddr(m_Instance, "vkCreateDebugUtilsMessengerEXT");
VkDebugUtilsMessengerCreateInfoEXT createInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT };
createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT;
createInfo.messageSeverity |= VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
createInfo.messageType |= VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
createInfo.pUserData = this;
createInfo.pfnUserCallback = DebugUtilsMessenger;
result = vkCreateDebugUtilsMessengerEXT(m_Instance, &createInfo, m_AllocationCallbackPtr, &m_Messenger);
RETURN_ON_FAILURE(this, result == VK_SUCCESS, GetReturnCode(result),
"Can't create a debug utils messenger callback: vkCreateDebugUtilsMessengerEXT returned %d.", (int32_t)result);
}
return Result::SUCCESS;
}
Result DeviceVK::FindPhysicalDeviceGroup(const AdapterDesc* adapterDesc, bool enableMGPU)
{
uint32_t deviceGroupNum = 0;
m_VK.EnumeratePhysicalDeviceGroups(m_Instance, &deviceGroupNum, nullptr);
VkPhysicalDeviceGroupProperties* deviceGroups = STACK_ALLOC(VkPhysicalDeviceGroupProperties, deviceGroupNum);
deviceGroups->sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_GROUP_PROPERTIES;
VkResult result = m_VK.EnumeratePhysicalDeviceGroups(m_Instance, &deviceGroupNum, deviceGroups);
RETURN_ON_FAILURE(this, result == VK_SUCCESS, GetReturnCode(result),
"Can't enumerate physical devices: vkEnumeratePhysicalDevices returned %d.", (int32_t)result);
VkPhysicalDeviceIDProperties idProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES };
VkPhysicalDeviceProperties2 props = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 };
props.pNext = &idProps;
uint32_t i = 0;
for (; i < deviceGroupNum && m_PhysicalDevices.empty(); i++)
{
const VkPhysicalDeviceGroupProperties& group = deviceGroups[i];
m_VK.GetPhysicalDeviceProperties2(group.physicalDevices[0], &props);
uint32_t majorVersion = VK_VERSION_MAJOR(props.properties.apiVersion);
uint32_t minorVersion = VK_VERSION_MINOR(props.properties.apiVersion);
bool isSupported = majorVersion * 10 + minorVersion >= 12;
if (adapterDesc)
{
const uint64_t luid = *(uint64_t*)idProps.deviceLUID;
if (luid == adapterDesc->luid)
{
RETURN_ON_FAILURE(this, isSupported, Result::UNSUPPORTED, "Can't create a device: the specified physical device does not support Vulkan 1.2!");
break;
}
}
else if (isSupported)
break;
}
RETURN_ON_FAILURE(this, i != deviceGroupNum, Result::UNSUPPORTED,
"Can't create a device: physical device not found.");
const VkPhysicalDeviceGroupProperties& group = deviceGroups[i];
supportedFeatures.subsetAllocation = true;
if (group.subsetAllocation == VK_FALSE && group.physicalDeviceCount > 1)
{
supportedFeatures.subsetAllocation = false;
REPORT_WARNING(this, "The device group does not support memory allocation on a subset of the physical devices.");
}
m_PhysicalDevices.insert(m_PhysicalDevices.begin(), group.physicalDevices, group.physicalDevices + group.physicalDeviceCount);
if (!enableMGPU)
m_PhysicalDevices.resize(1);
return Result::SUCCESS;
}
void DeviceVK::FillDesc(bool enableValidation)
{
uint32_t familyNum = 0;
m_VK.GetPhysicalDeviceQueueFamilyProperties(m_PhysicalDevices.front(), &familyNum, nullptr);
Vector<VkQueueFamilyProperties> familyProperties(familyNum, m_StdAllocator);
m_VK.GetPhysicalDeviceQueueFamilyProperties(m_PhysicalDevices.front(), &familyNum, familyProperties.data());
uint32_t copyQueueTimestampValidBits = 0;
const uint32_t copyQueueFamilyIndex = m_FamilyIndices[(uint32_t)CommandQueueType::COPY];
if (copyQueueFamilyIndex != INVALID_FAMILY_INDEX)
copyQueueTimestampValidBits = familyProperties[copyQueueFamilyIndex].timestampValidBits;
VkPhysicalDeviceIDProperties deviceIDProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES };
VkPhysicalDeviceProperties2 props = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &deviceIDProps };
m_VK.GetPhysicalDeviceProperties2(m_PhysicalDevices.front(), &props);
VkPhysicalDeviceFeatures features = {};
m_VK.GetPhysicalDeviceFeatures(m_PhysicalDevices.front(), &features);
VkPhysicalDeviceVulkan12Features features12 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES };
VkPhysicalDeviceFeatures2 features2 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &features12 };
m_VK.GetPhysicalDeviceFeatures2(m_PhysicalDevices.front(), &features2);
supportedFeatures.descriptorIndexing = features12.descriptorIndexing ? true : false;
supportedFeatures.bufferDeviceAddress = features12.bufferDeviceAddress ? true : false;
static_assert(VK_LUID_SIZE == sizeof(uint64_t), "invalid sizeof");
const VkPhysicalDeviceLimits& limits = props.properties.limits;
m_Desc.viewportMaxNum = limits.maxViewports;
m_Desc.viewportSubPixelBits = limits.viewportSubPixelBits;
m_Desc.viewportBoundsRange[0] = int32_t(limits.viewportBoundsRange[0]);
m_Desc.viewportBoundsRange[1] = int32_t(limits.viewportBoundsRange[1]);
m_Desc.attachmentMaxDim = (Dim_t)std::min(limits.maxFramebufferWidth, limits.maxFramebufferHeight);
m_Desc.attachmentLayerMaxNum = (Dim_t)limits.maxFramebufferLayers;
m_Desc.colorAttachmentMaxNum = (Dim_t)limits.maxColorAttachments;
m_Desc.colorSampleMaxNum = (Sample_t)limits.framebufferColorSampleCounts;
m_Desc.depthSampleMaxNum = (Sample_t)limits.framebufferDepthSampleCounts;
m_Desc.stencilSampleMaxNum = (Sample_t)limits.framebufferStencilSampleCounts;
m_Desc.zeroAttachmentsSampleMaxNum = (Sample_t)limits.framebufferNoAttachmentsSampleCounts;
m_Desc.textureColorSampleMaxNum = (Sample_t)limits.sampledImageColorSampleCounts;