Debian Codegraph @

Source/Core/VideoBackends/Vulkan/VulkanContext.cpp

raw symbols SCIP
1 // Copyright 2016 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/Vulkan/VulkanContext.h"
5
6 #include <algorithm>
7 #include <array>
8 #include <cstring>
9
10 #include "Common/Assert.h"
11 #include "Common/Contains.h"
12 #include "Common/Logging/Log.h"
13 #include "Common/MsgHandler.h"
14
15 #include "VideoCommon/DriverDetails.h"
16 #include "VideoCommon/VideoCommon.h"
17
18 namespace Vulkan
19 {
20 static constexpr const char* VALIDATION_LAYER_NAME = "VK_LAYER_KHRONOS_validation";
21
22 std::unique_ptr<VulkanContext> g_vulkan_context;
23
24 template <typename Chain, typename Element>
25 static void InsertIntoChain(Chain* chain, Element* element)
26 {
27 element->pNext = chain->pNext;
28 chain->pNext = element;
29 }
30
31 VulkanContext::PhysicalDeviceInfo::PhysicalDeviceInfo(VkPhysicalDevice device)
32 {
33 VkPhysicalDeviceFeatures features;
34 VkPhysicalDeviceProperties2 properties2;
35 VkPhysicalDeviceProperties& properties = properties2.properties;
36
37 vkGetPhysicalDeviceProperties(device, &properties);
38 vkGetPhysicalDeviceFeatures(device, &features);
39 apiVersion = vkGetPhysicalDeviceProperties2 ? properties.apiVersion : VK_API_VERSION_1_0;
40
41 if (apiVersion >= VK_API_VERSION_1_1)
42 {
43 VkPhysicalDeviceSubgroupProperties properties_subgroup = {};
44 VkPhysicalDeviceVulkan12Properties properties_vk12 = {};
45 properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
46 properties2.pNext = nullptr;
47 properties_subgroup.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES;
48 InsertIntoChain(&properties2, &properties_subgroup);
49
50 if (apiVersion >= VK_API_VERSION_1_2)
51 {
52 properties_vk12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES;
53 InsertIntoChain(&properties2, &properties_vk12);
54 }
55
56 vkGetPhysicalDeviceProperties2(device, &properties2);
57
58 if (apiVersion >= VK_API_VERSION_1_2)
59 {
60 driverID = properties_vk12.driverID;
61 }
62
63 subgroupSize = properties_subgroup.subgroupSize;
64
65 // We require basic ops (for gl_SubgroupInvocationID), ballot (for subgroupBallot,
66 // subgroupBallotFindLSB), and arithmetic (for subgroupMin/subgroupMax).
67 // Shuffle is enabled as a workaround until SPIR-V >= 1.5 is enabled with broadcast(uniform)
68 // support.
69 constexpr VkSubgroupFeatureFlags required_operations =
70 VK_SUBGROUP_FEATURE_BASIC_BIT | VK_SUBGROUP_FEATURE_ARITHMETIC_BIT |
71 VK_SUBGROUP_FEATURE_BALLOT_BIT | VK_SUBGROUP_FEATURE_SHUFFLE_BIT;
72 shaderSubgroupOperations =
73 (properties_subgroup.supportedOperations & required_operations) == required_operations &&
74 properties_subgroup.supportedStages & VK_SHADER_STAGE_FRAGMENT_BIT;
75 }
76
77 memcpy(deviceName, properties.deviceName, sizeof(deviceName));
78 memcpy(pipelineCacheUUID, properties.pipelineCacheUUID, sizeof(pipelineCacheUUID));
79 vendorID = properties.vendorID;
80 deviceID = properties.deviceID;
81 minUniformBufferOffsetAlignment =
82 std::max<VkDeviceSize>(properties.limits.minUniformBufferOffsetAlignment, 1);
83 bufferImageGranularity = std::max<VkDeviceSize>(properties.limits.bufferImageGranularity, 1);
84 maxTexelBufferElements = properties.limits.maxTexelBufferElements;
85 maxImageDimension2D = properties.limits.maxImageDimension2D;
86 framebufferColorSampleCounts = properties.limits.framebufferColorSampleCounts;
87 framebufferDepthSampleCounts = properties.limits.framebufferDepthSampleCounts;
88 memcpy(pointSizeRange, properties.limits.pointSizeRange, sizeof(pointSizeRange));
89 maxSamplerAnisotropy = properties.limits.maxSamplerAnisotropy;
90
91 dualSrcBlend = features.dualSrcBlend != VK_FALSE;
92 geometryShader = features.geometryShader != VK_FALSE;
93 samplerAnisotropy = features.samplerAnisotropy != VK_FALSE;
94 logicOp = features.logicOp != VK_FALSE;
95 fragmentStoresAndAtomics = features.fragmentStoresAndAtomics != VK_FALSE;
96 sampleRateShading = features.sampleRateShading != VK_FALSE;
97 largePoints = features.largePoints != VK_FALSE;
98 shaderStorageImageMultisample = features.shaderStorageImageMultisample != VK_FALSE;
99 shaderTessellationAndGeometryPointSize =
100 features.shaderTessellationAndGeometryPointSize != VK_FALSE;
101 occlusionQueryPrecise = features.occlusionQueryPrecise != VK_FALSE;
102 shaderClipDistance = features.shaderClipDistance != VK_FALSE;
103 depthClamp = features.depthClamp != VK_FALSE;
104 textureCompressionBC = features.textureCompressionBC != VK_FALSE;
105 }
106
107 VkPhysicalDeviceFeatures VulkanContext::PhysicalDeviceInfo::features() const
108 {
109 VkPhysicalDeviceFeatures features;
110 memset(&features, 0, sizeof(features));
111 features.dualSrcBlend = dualSrcBlend ? VK_TRUE : VK_FALSE;
112 features.geometryShader = geometryShader ? VK_TRUE : VK_FALSE;
113 features.samplerAnisotropy = samplerAnisotropy ? VK_TRUE : VK_FALSE;
114 features.logicOp = logicOp ? VK_TRUE : VK_FALSE;
115 features.fragmentStoresAndAtomics = fragmentStoresAndAtomics ? VK_TRUE : VK_FALSE;
116 features.sampleRateShading = sampleRateShading ? VK_TRUE : VK_FALSE;
117 features.largePoints = largePoints ? VK_TRUE : VK_FALSE;
118 features.shaderStorageImageMultisample = shaderStorageImageMultisample ? VK_TRUE : VK_FALSE;
119 features.shaderTessellationAndGeometryPointSize =
120 shaderTessellationAndGeometryPointSize ? VK_TRUE : VK_FALSE;
121 features.occlusionQueryPrecise = occlusionQueryPrecise ? VK_TRUE : VK_FALSE;
122 features.shaderClipDistance = shaderClipDistance ? VK_TRUE : VK_FALSE;
123 features.depthClamp = depthClamp ? VK_TRUE : VK_FALSE;
124 features.textureCompressionBC = textureCompressionBC ? VK_TRUE : VK_FALSE;
125 return features;
126 }
127
128 VulkanContext::VulkanContext(VkInstance instance, VkPhysicalDevice physical_device)
129 : m_instance(instance), m_physical_device(physical_device), m_device_info(physical_device)
130 {
131 }
132
133 VulkanContext::~VulkanContext()
134 {
135 if (m_allocator != VK_NULL_HANDLE)
136 vmaDestroyAllocator(m_allocator);
137 if (m_device != VK_NULL_HANDLE)
138 vkDestroyDevice(m_device, nullptr);
139
140 if (m_debug_utils_messenger != VK_NULL_HANDLE)
141 DisableDebugUtils();
142
143 vkDestroyInstance(m_instance, nullptr);
144 }
145
146 bool VulkanContext::CheckValidationLayerAvailablility()
147 {
148 u32 extension_count = 0;
149 VkResult res = vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, nullptr);
150 if (res != VK_SUCCESS)
151 {
152 LOG_VULKAN_ERROR(res, "vkEnumerateInstanceExtensionProperties failed: ");
153 return false;
154 }
155
156 std::vector<VkExtensionProperties> extension_list(extension_count);
157 res = vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, extension_list.data());
158 ASSERT(res == VK_SUCCESS);
159
160 u32 layer_count = 0;
161 res = vkEnumerateInstanceLayerProperties(&layer_count, nullptr);
162 if (res != VK_SUCCESS)
163 {
164 LOG_VULKAN_ERROR(res, "vkEnumerateInstanceExtensionProperties failed: ");
165 return false;
166 }
167
168 std::vector<VkLayerProperties> layer_list(layer_count);
169 res = vkEnumerateInstanceLayerProperties(&layer_count, layer_list.data());
170 ASSERT(res == VK_SUCCESS);
171
172 bool supports_validation_layers = Common::Contains(
173 layer_list, std::string_view{VALIDATION_LAYER_NAME}, &VkLayerProperties::layerName);
174
175 bool supports_debug_utils =
176 Common::Contains(extension_list, std::string_view{VK_EXT_DEBUG_UTILS_EXTENSION_NAME},
177 &VkExtensionProperties::extensionName);
178
179 if (!supports_debug_utils && supports_validation_layers)
180 {
181 // If the instance doesn't support debug utils but we're using validation layers,
182 // try to use the implementation of the extension provided by the validation layers.
183 extension_count = 0;
184 res = vkEnumerateInstanceExtensionProperties(VALIDATION_LAYER_NAME, &extension_count, nullptr);
185 if (res != VK_SUCCESS)
186 {
187 LOG_VULKAN_ERROR(res, "vkEnumerateInstanceExtensionProperties failed: ");
188 return false;
189 }
190
191 extension_list.resize(extension_count);
192 res = vkEnumerateInstanceExtensionProperties(VALIDATION_LAYER_NAME, &extension_count,
193 extension_list.data());
194 ASSERT(res == VK_SUCCESS);
195 supports_debug_utils =
196 Common::Contains(extension_list, std::string_view{VK_EXT_DEBUG_UTILS_EXTENSION_NAME},
197 &VkExtensionProperties::extensionName);
198 }
199
200 // Check for both VK_EXT_debug_utils and VK_LAYER_KHRONOS_validation
201 return supports_debug_utils && supports_validation_layers;
202 }
203
204 static u32 getAPIVersion()
205 {
206 u32 supported_version;
207 u32 used_version = VK_API_VERSION_1_0;
208 if (vkEnumerateInstanceVersion && vkEnumerateInstanceVersion(&supported_version) == VK_SUCCESS)
209 {
210 // The device itself may not support 1.1, so we check that before using any 1.1 functionality.
211 if (supported_version >= VK_API_VERSION_1_2)
212 used_version = VK_API_VERSION_1_2;
213 else if (supported_version >= VK_API_VERSION_1_1)
214 used_version = VK_API_VERSION_1_1;
215 WARN_LOG_FMT(HOST_GPU, "Using Vulkan 1.{}, supported: {}.{}", VK_VERSION_MINOR(used_version),
216 VK_VERSION_MAJOR(supported_version), VK_VERSION_MINOR(supported_version));
217 }
218 else
219 {
220 WARN_LOG_FMT(HOST_GPU, "Using Vulkan 1.0");
221 }
222 return used_version;
223 }
224
225 VkInstance VulkanContext::CreateVulkanInstance(WindowSystemType wstype, bool enable_debug_utils,
226 bool enable_validation_layer,
227 u32* out_vk_api_version)
228 {
229 std::vector<const char*> enabled_extensions;
230 if (!SelectInstanceExtensions(&enabled_extensions, wstype, enable_debug_utils,
231 enable_validation_layer))
232 return VK_NULL_HANDLE;
233
234 VkApplicationInfo app_info = {};
235 app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
236 app_info.pNext = nullptr;
237 app_info.pApplicationName = "Dolphin Emulator";
238 app_info.applicationVersion = VK_MAKE_VERSION(5, 0, 0);
239 app_info.pEngineName = "Dolphin Emulator";
240 app_info.engineVersion = VK_MAKE_VERSION(5, 0, 0);
241 app_info.apiVersion = getAPIVersion();
242
243 *out_vk_api_version = app_info.apiVersion;
244
245 VkInstanceCreateInfo instance_create_info = {};
246 instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
247 instance_create_info.pNext = nullptr;
248 instance_create_info.flags = 0;
249 instance_create_info.pApplicationInfo = &app_info;
250 instance_create_info.enabledExtensionCount = static_cast<uint32_t>(enabled_extensions.size());
251 instance_create_info.ppEnabledExtensionNames = enabled_extensions.data();
252 instance_create_info.enabledLayerCount = 0;
253 instance_create_info.ppEnabledLayerNames = nullptr;
254
255 // Enable validation layer if the user enabled them in the settings
256 if (enable_validation_layer)
257 {
258 instance_create_info.enabledLayerCount = 1;
259 instance_create_info.ppEnabledLayerNames = &VALIDATION_LAYER_NAME;
260 }
261
262 VkInstance instance;
263 VkResult res = vkCreateInstance(&instance_create_info, nullptr, &instance);
264 if (res != VK_SUCCESS)
265 {
266 LOG_VULKAN_ERROR(res, "vkCreateInstance failed: ");
267 return nullptr;
268 }
269
270 return instance;
271 }
272
273 bool VulkanContext::SelectInstanceExtensions(std::vector<const char*>* extension_list,
274 WindowSystemType wstype, bool enable_debug_utils,
275 bool validation_layer_enabled)
276 {
277 u32 extension_count = 0;
278 VkResult res = vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, nullptr);
279 if (res != VK_SUCCESS)
280 {
281 LOG_VULKAN_ERROR(res, "vkEnumerateInstanceExtensionProperties failed: ");
282 return false;
283 }
284
285 if (extension_count == 0)
286 {
287 ERROR_LOG_FMT(VIDEO, "Vulkan: No extensions supported by instance.");
288 return false;
289 }
290
291 std::vector<VkExtensionProperties> available_extension_list(extension_count);
292 res = vkEnumerateInstanceExtensionProperties(nullptr, &extension_count,
293 available_extension_list.data());
294 ASSERT(res == VK_SUCCESS);
295
296 u32 validation_layer_extension_count = 0;
297 std::vector<VkExtensionProperties> validation_layer_extension_list;
298 if (validation_layer_enabled)
299 {
300 res = vkEnumerateInstanceExtensionProperties(VALIDATION_LAYER_NAME,
301 &validation_layer_extension_count, nullptr);
302 if (res != VK_SUCCESS)
303 {
304 LOG_VULKAN_ERROR(res,
305 "vkEnumerateInstanceExtensionProperties failed for validation layers: ");
306 }
307 else
308 {
309 validation_layer_extension_list.resize(validation_layer_extension_count);
310 res = vkEnumerateInstanceExtensionProperties(VALIDATION_LAYER_NAME,
311 &validation_layer_extension_count,
312 validation_layer_extension_list.data());
313 ASSERT(res == VK_SUCCESS);
314 }
315 }
316
317 for (const auto& extension_properties : available_extension_list)
318 INFO_LOG_FMT(VIDEO, "Available extension: {}", extension_properties.extensionName);
319
320 for (const auto& extension_properties : validation_layer_extension_list)
321 {
322 INFO_LOG_FMT(VIDEO, "Available extension in validation layer: {}",
323 extension_properties.extensionName);
324 }
325
326 auto AddExtension = [&](const char* name, bool required) {
327 bool extension_supported =
328 Common::Contains(available_extension_list, std::string_view{name},
329 &VkExtensionProperties::extensionName) ||
330 Common::Contains(validation_layer_extension_list, std::string_view{name},
331 &VkExtensionProperties::extensionName);
332
333 if (extension_supported)
334 {
335 INFO_LOG_FMT(VIDEO, "Enabling extension: {}", name);
336 extension_list->push_back(name);
337 return true;
338 }
339
340 if (required)
341 ERROR_LOG_FMT(VIDEO, "Vulkan: Missing required extension {}.", name);
342
343 return false;
344 };
345
346 // Common extensions
347 if (wstype != WindowSystemType::Headless && !AddExtension(VK_KHR_SURFACE_EXTENSION_NAME, true))
348 {
349 return false;
350 }
351
352 #if defined(VK_USE_PLATFORM_WIN32_KHR)
353 if (wstype == WindowSystemType::Windows &&
354 !AddExtension(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, true))
355 {
356 return false;
357 }
358 #endif
359 #if defined(VK_USE_PLATFORM_XLIB_KHR)
360 if (wstype == WindowSystemType::X11 && !AddExtension(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, true))
361 {
362 return false;
363 }
364 #endif
365 #if defined(VK_USE_PLATFORM_ANDROID_KHR)
366 if (wstype == WindowSystemType::Android &&
367 !AddExtension(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, true))
368 {
369 return false;
370 }
371 #endif
372 #if defined(VK_USE_PLATFORM_METAL_EXT)
373 if (wstype == WindowSystemType::MacOS && !AddExtension(VK_EXT_METAL_SURFACE_EXTENSION_NAME, true))
374 {
375 return false;
376 }
377 #endif
378
379 AddExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, false);
380 if (wstype != WindowSystemType::Headless)
381 {
382 AddExtension(VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME, false);
383 }
384
385 // VK_EXT_debug_utils
386 if (AddExtension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, false))
387 {
388 g_backend_info.bSupportsSettingObjectNames = true;
389 }
390 else if (enable_debug_utils)
391 {
392 WARN_LOG_FMT(VIDEO, "Vulkan: Debug utils requested, but extension is not available.");
393 }
394
395 return true;
396 }
397
398 VulkanContext::GPUList VulkanContext::EnumerateGPUs(VkInstance instance)
399 {
400 u32 gpu_count = 0;
401 VkResult res = vkEnumeratePhysicalDevices(instance, &gpu_count, nullptr);
402 if (res != VK_SUCCESS)
403 {
404 LOG_VULKAN_ERROR(res, "vkEnumeratePhysicalDevices failed: ");
405 return {};
406 }
407
408 GPUList gpus;
409 gpus.resize(gpu_count);
410
411 res = vkEnumeratePhysicalDevices(instance, &gpu_count, gpus.data());
412 if (res != VK_SUCCESS)
413 {
414 LOG_VULKAN_ERROR(res, "vkEnumeratePhysicalDevices failed: ");
415 return {};
416 }
417
418 return gpus;
419 }
420
421 void VulkanContext::PopulateBackendInfo(BackendInfo* backend_info)
422 {
423 backend_info->api_type = APIType::Vulkan;
424 backend_info->bSupports3DVision = false; // D3D-exclusive.
425 backend_info->bSupportsEarlyZ = true; // Assumed support.
426 backend_info->bSupportsPrimitiveRestart = true; // Assumed support.
427 backend_info->bSupportsBindingLayout = false; // Assumed support.
428 backend_info->bSupportsPaletteConversion = true; // Assumed support.
429 backend_info->bSupportsClipControl = true; // Assumed support.
430 backend_info->bSupportsMultithreading = true; // Assumed support.
431 backend_info->bSupportsComputeShaders = true; // Assumed support.
432 backend_info->bSupportsGPUTextureDecoding = true; // Assumed support.
433 backend_info->bSupportsBitfield = true; // Assumed support.
434 backend_info->bSupportsPartialDepthCopies = true; // Assumed support.
435 backend_info->bSupportsShaderBinaries = true; // Assumed support.
436 backend_info->bSupportsPipelineCacheData = false; // Handled via pipeline caches.
437 backend_info->bSupportsDynamicSamplerIndexing = true; // Assumed support.
438 backend_info->bSupportsPostProcessing = true; // Assumed support.
439 backend_info->bSupportsBackgroundCompiling = true; // Assumed support.
440 backend_info->bSupportsCopyToVram = true; // Assumed support.
441 backend_info->bSupportsReversedDepthRange = true; // Assumed support.
442 backend_info->bSupportsExclusiveFullscreen = false; // Dependent on OS and features.
443 backend_info->bSupportsDualSourceBlend = false; // Dependent on features.
444 backend_info->bSupportsGeometryShaders = false; // Dependent on features.
445 backend_info->bSupportsGSInstancing = false; // Dependent on features.
446 backend_info->bSupportsBBox = false; // Dependent on features.
447 backend_info->bSupportsFragmentStoresAndAtomics = false; // Dependent on features.
448 backend_info->bSupportsSSAA = false; // Dependent on features.
449 backend_info->bSupportsDepthClamp = false; // Dependent on features.
450 backend_info->bSupportsST3CTextures = false; // Dependent on features.
451 backend_info->bSupportsBPTCTextures = false; // Dependent on features.
452 backend_info->bSupportsLogicOp = false; // Dependent on features.
453 backend_info->bSupportsLargePoints = false; // Dependent on features.
454 backend_info->bSupportsCoarseDerivatives = true; // Assumed support.
455 backend_info->bSupportsTextureQueryLevels = true; // Assumed support.
456 backend_info->bSupportsLodBiasInSampler = false; // Dependent on OS.
457 backend_info->bSupportsSettingObjectNames = false; // Dependent on features.
458 backend_info->bSupportsPartialMultisampleResolve = true; // Assumed support.
459 backend_info->bSupportsDynamicVertexLoader = true; // Assumed support.
460 backend_info->bSupportsVSLinePointExpand = true; // Assumed support.
461 backend_info->bSupportsHDROutput = true; // Assumed support.
462 backend_info->bSupportsUnrestrictedDepthRange = false; // Dependent on features.
463 }
464
465 void VulkanContext::PopulateBackendInfoAdapters(BackendInfo* backend_info, const GPUList& gpu_list)
466 {
467 backend_info->Adapters.clear();
468 for (VkPhysicalDevice physical_device : gpu_list)
469 {
470 VkPhysicalDeviceProperties properties;
471 vkGetPhysicalDeviceProperties(physical_device, &properties);
472 backend_info->Adapters.push_back(properties.deviceName);
473 }
474 }
475
476 void VulkanContext::PopulateBackendInfoFeatures(BackendInfo* backend_info, VkPhysicalDevice gpu,
477 const PhysicalDeviceInfo& info)
478 {
479 backend_info->MaxTextureSize = info.maxImageDimension2D;
480 backend_info->bUsesLowerLeftOrigin = false;
481 backend_info->bSupportsDualSourceBlend = info.dualSrcBlend;
482 backend_info->bSupportsGeometryShaders = info.geometryShader;
483 backend_info->bSupportsGSInstancing = info.geometryShader;
484 backend_info->bSupportsBBox = backend_info->bSupportsFragmentStoresAndAtomics =
485 info.fragmentStoresAndAtomics;
486 backend_info->bSupportsSSAA = info.sampleRateShading;
487 backend_info->bSupportsLogicOp = info.logicOp;
488
489 // Metal doesn't support this.
490 backend_info->bSupportsLodBiasInSampler = info.driverID != VK_DRIVER_ID_MOLTENVK;
491
492 // Disable geometry shader when shaderTessellationAndGeometryPointSize is not supported.
493 // Seems this is needed for gl_Layer.
494 if (!info.shaderTessellationAndGeometryPointSize)
495 {
496 backend_info->bSupportsGeometryShaders = VK_FALSE;
497 backend_info->bSupportsGSInstancing = VK_FALSE;
498 }
499
500 // Depth clamping implies shaderClipDistance and depthClamp
501 backend_info->bSupportsDepthClamp = info.depthClamp && info.shaderClipDistance;
502
503 // textureCompressionBC implies BC1 through BC7, which is a superset of DXT1/3/5, which we need.
504 backend_info->bSupportsST3CTextures = info.textureCompressionBC;
505 backend_info->bSupportsBPTCTextures = info.textureCompressionBC;
506
507 // Some devices don't support point sizes >1 (e.g. Adreno).
508 // If we can't use a point size above our maximum IR, use triangles instead for EFB pokes.
509 // This means a 6x increase in the size of the vertices, though.
510 backend_info->bSupportsLargePoints =
511 info.largePoints && info.pointSizeRange[0] <= 1.0f && info.pointSizeRange[1] >= 16;
512
513 // Our usage of primitive restart appears to be broken on AMD's binary drivers.
514 // Seems to be fine on GCN Gen 1-2, unconfirmed on GCN Gen 3, causes driver resets on GCN Gen 4.
515 if (DriverDetails::HasBug(DriverDetails::BUG_PRIMITIVE_RESTART))
516 backend_info->bSupportsPrimitiveRestart = false;
517
518 // Reversed depth range is broken on some drivers, or is broken when used in combination
519 // with depth clamping. Fall back to inverted depth range for these.
520 if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_REVERSED_DEPTH_RANGE))
521 backend_info->bSupportsReversedDepthRange = false;
522
523 // Dynamic sampler indexing locks up Intel GPUs on MoltenVK/Metal
524 if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_DYNAMIC_SAMPLER_INDEXING))
525 backend_info->bSupportsDynamicSamplerIndexing = false;
526
527 if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_DISCARD_WITH_EARLY_Z))
528 {
529 PanicAlertFmtT(
530 "You are attempting to use the Vulkan backend on an unsupported operating system. "
531 "To prevent visual glitches and artifacts, please use the Metal backend or update "
532 "to macOS Sonoma 14 or newer.");
533 }
534 }
535
536 void VulkanContext::PopulateBackendInfoMultisampleModes(BackendInfo* backend_info,
537 VkPhysicalDevice gpu,
538 const PhysicalDeviceInfo& info)
539 {
540 // Query image support for the EFB texture formats.
541 VkImageFormatProperties efb_color_properties = {};
542 vkGetPhysicalDeviceImageFormatProperties(
543 gpu, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL,
544 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, 0, &efb_color_properties);
545 VkImageFormatProperties efb_depth_properties = {};
546 vkGetPhysicalDeviceImageFormatProperties(
547 gpu, VK_FORMAT_D32_SFLOAT, VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL,
548 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, 0, &efb_depth_properties);
549
550 // We can only support MSAA if it's supported on our render target formats.
551 VkSampleCountFlags supported_sample_counts =
552 info.framebufferColorSampleCounts & info.framebufferDepthSampleCounts &
553 efb_color_properties.sampleCounts & efb_depth_properties.sampleCounts;
554
555 // No AA
556 backend_info->AAModes.clear();
557 backend_info->AAModes.emplace_back(1);
558
559 // 2xMSAA/SSAA
560 if (supported_sample_counts & VK_SAMPLE_COUNT_2_BIT)
561 backend_info->AAModes.emplace_back(2);
562
563 // 4xMSAA/SSAA
564 if (supported_sample_counts & VK_SAMPLE_COUNT_4_BIT)
565 backend_info->AAModes.emplace_back(4);
566
567 // 8xMSAA/SSAA
568 if (supported_sample_counts & VK_SAMPLE_COUNT_8_BIT)
569 backend_info->AAModes.emplace_back(8);
570
571 // 16xMSAA/SSAA
572 if (supported_sample_counts & VK_SAMPLE_COUNT_16_BIT)
573 backend_info->AAModes.emplace_back(16);
574
575 // 32xMSAA/SSAA
576 if (supported_sample_counts & VK_SAMPLE_COUNT_32_BIT)
577 backend_info->AAModes.emplace_back(32);
578
579 // 64xMSAA/SSAA
580 if (supported_sample_counts & VK_SAMPLE_COUNT_64_BIT)
581 backend_info->AAModes.emplace_back(64);
582 }
583
584 std::unique_ptr<VulkanContext> VulkanContext::Create(VkInstance instance, VkPhysicalDevice gpu,
585 VkSurfaceKHR surface, bool enable_debug_utils,
586 bool enable_validation_layer,
587 u32 vk_api_version)
588 {
589 std::unique_ptr<VulkanContext> context = std::make_unique<VulkanContext>(instance, gpu);
590
591 // Initialize DriverDetails so that we can check for bugs to disable features if needed.
592 context->InitDriverDetails();
593
594 // Enable debug messages if the "Host GPU" log category is enabled.
595 if (enable_debug_utils)
596 context->EnableDebugUtils();
597
598 // Attempt to create the device.
599 if (!context->CreateDevice(surface, enable_validation_layer) ||
600 !context->CreateAllocator(vk_api_version))
601 {
602 // Since we are destroying the instance, we're also responsible for destroying the surface.
603 if (surface != VK_NULL_HANDLE)
604 vkDestroySurfaceKHR(instance, surface, nullptr);
605
606 return nullptr;
607 }
608
609 return context;
610 }
611
612 bool VulkanContext::SelectDeviceExtensions(bool enable_surface)
613 {
614 u32 extension_count = 0;
615 VkResult res =
616 vkEnumerateDeviceExtensionProperties(m_physical_device, nullptr, &extension_count, nullptr);
617 if (res != VK_SUCCESS)
618 {
619 LOG_VULKAN_ERROR(res, "vkEnumerateDeviceExtensionProperties failed: ");
620 return false;
621 }
622
623 if (extension_count == 0)
624 {
625 ERROR_LOG_FMT(VIDEO, "Vulkan: No extensions supported by device.");
626 return false;
627 }
628
629 std::vector<VkExtensionProperties> available_extension_list(extension_count);
630 res = vkEnumerateDeviceExtensionProperties(m_physical_device, nullptr, &extension_count,
631 available_extension_list.data());
632 ASSERT(res == VK_SUCCESS);
633
634 for (const auto& extension_properties : available_extension_list)
635 INFO_LOG_FMT(VIDEO, "Available extension: {}", extension_properties.extensionName);
636
637 auto AddExtension = [&](const char* name, bool required) {
638 if (Common::Contains(available_extension_list, std::string_view{name},
639 &VkExtensionProperties::extensionName))
640 {
641 INFO_LOG_FMT(VIDEO, "Enabling extension: {}", name);
642 m_device_extensions.push_back(name);
643 return true;
644 }
645
646 if (required)
647 ERROR_LOG_FMT(VIDEO, "Vulkan: Missing required extension {}.", name);
648
649 return false;
650 };
651
652 if (enable_surface && !AddExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, true))
653 return false;
654
655 #ifdef SUPPORTS_VULKAN_EXCLUSIVE_FULLSCREEN
656 // VK_EXT_full_screen_exclusive
657 if (AddExtension(VK_EXT_FULL_SCREEN_EXCLUSIVE_EXTENSION_NAME, true))
658 INFO_LOG_FMT(VIDEO, "Using VK_EXT_full_screen_exclusive for exclusive fullscreen.");
659 #endif
660
661 AddExtension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, false);
662 AddExtension(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME, false);
663
664 if (!DriverDetails::HasBug(DriverDetails::BUG_BROKEN_DEPTH_CLAMP_CONTROL))
665 {
666 // Unrestricted depth range is one of the few extensions that changes the behavior
667 // of Vulkan just by being enabled, so we rely on lazy evaluation to ensure it is
668 // not enabled unless depth clamp control is supported.
669 g_backend_info.bSupportsUnrestrictedDepthRange =
670 AddExtension(VK_EXT_DEPTH_CLAMP_CONTROL_EXTENSION_NAME, false) &&
671 AddExtension(VK_EXT_DEPTH_RANGE_UNRESTRICTED_EXTENSION_NAME, false);
672 }
673
674 return true;
675 }
676
677 void VulkanContext::WarnMissingDeviceFeatures()
678 {
679 if (!m_device_info.largePoints)
680 WARN_LOG_FMT(VIDEO, "Vulkan: Missing large points feature. CPU EFB writes will be slower.");
681 if (!m_device_info.occlusionQueryPrecise)
682 {
683 WARN_LOG_FMT(VIDEO,
684 "Vulkan: Missing precise occlusion queries. Perf queries will be inaccurate.");
685 }
686 }
687
688 bool VulkanContext::CreateDevice(VkSurfaceKHR surface, bool enable_validation_layer)
689 {
690 u32 queue_family_count;
691 vkGetPhysicalDeviceQueueFamilyProperties(m_physical_device, &queue_family_count, nullptr);
692 if (queue_family_count == 0)
693 {
694 ERROR_LOG_FMT(VIDEO, "No queue families found on specified vulkan physical device.");
695 return false;
696 }
697
698 std::vector<VkQueueFamilyProperties> queue_family_properties(queue_family_count);
699 vkGetPhysicalDeviceQueueFamilyProperties(m_physical_device, &queue_family_count,
700 queue_family_properties.data());
701 INFO_LOG_FMT(VIDEO, "{} vulkan queue families", queue_family_count);
702
703 // Find graphics and present queues.
704 m_graphics_queue_family_index = queue_family_count;
705 m_present_queue_family_index = queue_family_count;
706 for (uint32_t i = 0; i < queue_family_count; i++)
707 {
708 VkBool32 graphics_supported = queue_family_properties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT;
709 if (graphics_supported)
710 {
711 m_graphics_queue_family_index = i;
712 // Quit now, no need for a present queue.
713 if (!surface)
714 {
715 break;
716 }
717 }
718
719 if (surface)
720 {
721 VkBool32 present_supported;
722 VkResult res =
723 vkGetPhysicalDeviceSurfaceSupportKHR(m_physical_device, i, surface, &present_supported);
724 if (res != VK_SUCCESS)
725 {
726 LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceSupportKHR failed: ");
727 return false;
728 }
729
730 if (present_supported)
731 {
732 m_present_queue_family_index = i;
733 }
734
735 // Prefer one queue family index that does both graphics and present.
736 if (graphics_supported && present_supported)
737 {
738 break;
739 }
740 }
741 }
742 if (m_graphics_queue_family_index == queue_family_count)
743 {
744 ERROR_LOG_FMT(VIDEO, "Vulkan: Failed to find an acceptable graphics queue.");
745 return false;
746 }
747 if (surface && m_present_queue_family_index == queue_family_count)
748 {
749 ERROR_LOG_FMT(VIDEO, "Vulkan: Failed to find an acceptable present queue.");
750 return false;
751 }
752
753 VkDeviceCreateInfo device_info = {};
754 device_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
755 device_info.pNext = nullptr;
756 device_info.flags = 0;
757
758 static constexpr float queue_priorities[] = {1.0f};
759 VkDeviceQueueCreateInfo graphics_queue_info = {};
760 graphics_queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
761 graphics_queue_info.pNext = nullptr;
762 graphics_queue_info.flags = 0;
763 graphics_queue_info.queueFamilyIndex = m_graphics_queue_family_index;
764 graphics_queue_info.queueCount = 1;
765 graphics_queue_info.pQueuePriorities = queue_priorities;
766
767 VkDeviceQueueCreateInfo present_queue_info = {};
768 present_queue_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
769 present_queue_info.pNext = nullptr;
770 present_queue_info.flags = 0;
771 present_queue_info.queueFamilyIndex = m_present_queue_family_index;
772 present_queue_info.queueCount = 1;
773 present_queue_info.pQueuePriorities = queue_priorities;
774
775 std::array<VkDeviceQueueCreateInfo, 2> queue_infos = {{
776 graphics_queue_info,
777 present_queue_info,
778 }};
779
780 device_info.queueCreateInfoCount = 1;
781 if (m_graphics_queue_family_index != m_present_queue_family_index &&
782 m_present_queue_family_index != queue_family_count)
783 {
784 device_info.queueCreateInfoCount = 2;
785 }
786 device_info.pQueueCreateInfos = queue_infos.data();
787
788 if (!SelectDeviceExtensions(surface != VK_NULL_HANDLE))
789 return false;
790
791 // convert std::string list to a char pointer list which we can feed in
792 std::vector<const char*> extension_name_pointers;
793 for (const std::string& name : m_device_extensions)
794 extension_name_pointers.push_back(name.c_str());
795
796 device_info.enabledLayerCount = 0;
797 device_info.ppEnabledLayerNames = nullptr;
798 device_info.enabledExtensionCount = static_cast<uint32_t>(extension_name_pointers.size());
799 device_info.ppEnabledExtensionNames = extension_name_pointers.data();
800
801 WarnMissingDeviceFeatures();
802
803 VkPhysicalDeviceFeatures device_features = m_device_info.features();
804 device_info.pEnabledFeatures = &device_features;
805
806 // Enable debug layer on debug builds
807 if (enable_validation_layer)
808 {
809 device_info.enabledLayerCount = 1;
810 device_info.ppEnabledLayerNames = &VALIDATION_LAYER_NAME;
811 }
812
813 VkResult res = vkCreateDevice(m_physical_device, &device_info, nullptr, &m_device);
814 if (res != VK_SUCCESS)
815 {
816 LOG_VULKAN_ERROR(res, "vkCreateDevice failed: ");
817 return false;
818 }
819
820 // With the device created, we can fill the remaining entry points.
821 if (!LoadVulkanDeviceFunctions(m_device))
822 return false;
823
824 // Grab the graphics and present queues.
825 vkGetDeviceQueue(m_device, m_graphics_queue_family_index, 0, &m_graphics_queue);
826 if (surface)
827 {
828 vkGetDeviceQueue(m_device, m_present_queue_family_index, 0, &m_present_queue);
829 }
830 return true;
831 }
832
833 bool VulkanContext::CreateAllocator(u32 vk_api_version)
834 {
835 VmaAllocatorCreateInfo allocator_info = {};
836 allocator_info.flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT;
837 allocator_info.physicalDevice = m_physical_device;
838 allocator_info.device = m_device;
839 allocator_info.preferredLargeHeapBlockSize = 64 << 20;
840 allocator_info.pAllocationCallbacks = nullptr;
841 allocator_info.pDeviceMemoryCallbacks = nullptr;
842 allocator_info.pHeapSizeLimit = nullptr;
843 allocator_info.pVulkanFunctions = nullptr;
844 allocator_info.instance = m_instance;
845 allocator_info.vulkanApiVersion = vk_api_version;
846 allocator_info.pTypeExternalMemoryHandleTypes = nullptr;
847
848 if (SupportsDeviceExtension(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME))
849 allocator_info.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT;
850
851 VkResult res = vmaCreateAllocator(&allocator_info, &m_allocator);
852 if (res != VK_SUCCESS)
853 {
854 LOG_VULKAN_ERROR(res, "vmaCreateAllocator failed: ");
855 return false;
856 }
857
858 return true;
859 }
860
861 static VKAPI_ATTR VkBool32 VKAPI_CALL
862 DebugUtilsCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
863 VkDebugUtilsMessageTypeFlagsEXT messageTypes,
864 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData)
865 {
866 const std::string log_message = fmt::format("Vulkan debug message: {}", pCallbackData->pMessage);
867 if (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
868 ERROR_LOG_FMT(HOST_GPU, "{}", log_message);
869 else if (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)
870 WARN_LOG_FMT(HOST_GPU, "{}", log_message);
871 else if (messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT)
872 INFO_LOG_FMT(HOST_GPU, "{}", log_message);
873 else
874 DEBUG_LOG_FMT(HOST_GPU, "{}", log_message);
875
876 return VK_FALSE;
877 }
878
879 bool VulkanContext::EnableDebugUtils()
880 {
881 // Already enabled?
882 if (m_debug_utils_messenger != VK_NULL_HANDLE)
883 return true;
884
885 // Check for presence of the functions before calling
886 if (!vkCreateDebugUtilsMessengerEXT || !vkDestroyDebugUtilsMessengerEXT ||
887 !vkSubmitDebugUtilsMessageEXT)
888 {
889 return false;
890 }
891
892 VkDebugUtilsMessengerCreateInfoEXT messenger_info = {
893 VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
894 nullptr,
895 0,
896 VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT |
897 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
898 VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
899 VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT,
900 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
901 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT,
902 DebugUtilsCallback,
903 nullptr};
904
905 VkResult res = vkCreateDebugUtilsMessengerEXT(m_instance, &messenger_info, nullptr,
906 &m_debug_utils_messenger);
907 if (res != VK_SUCCESS)
908 {
909 LOG_VULKAN_ERROR(res, "vkCreateDebugUtilsMessengerEXT failed: ");
910 return false;
911 }
912
913 return true;
914 }
915
916 void VulkanContext::DisableDebugUtils()
917 {
918 if (m_debug_utils_messenger != VK_NULL_HANDLE)
919 {
920 vkDestroyDebugUtilsMessengerEXT(m_instance, m_debug_utils_messenger, nullptr);
921 m_debug_utils_messenger = VK_NULL_HANDLE;
922 }
923 }
924
925 bool VulkanContext::SupportsDeviceExtension(const char* name) const
926 {
927 return std::ranges::any_of(m_device_extensions,
928 [name](const std::string& extension) { return extension == name; });
929 }
930
931 static bool DriverIsMesa(VkDriverId driver_id)
932 {
933 switch (driver_id)
934 {
935 case VK_DRIVER_ID_MESA_RADV:
936 case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
937 case VK_DRIVER_ID_MESA_LLVMPIPE:
938 case VK_DRIVER_ID_MESA_TURNIP:
939 case VK_DRIVER_ID_MESA_V3DV:
940 case VK_DRIVER_ID_MESA_PANVK:
941 case VK_DRIVER_ID_MESA_VENUS:
942 case VK_DRIVER_ID_MESA_DOZEN:
943 case VK_DRIVER_ID_MESA_NVK:
944 case VK_DRIVER_ID_IMAGINATION_OPEN_SOURCE_MESA:
945 case VK_DRIVER_ID_MESA_HONEYKRISP:
946 return true;
947 default:
948 return false;
949 }
950 }
951
952 static DriverDetails::Driver GetMesaDriver(VkDriverId driver_id)
953 {
954 switch (driver_id)
955 {
956 // clang-format off
957 case VK_DRIVER_ID_MESA_RADV: return DriverDetails::DRIVER_R600;
958 case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA: return DriverDetails::DRIVER_I965;
959 case VK_DRIVER_ID_MESA_NVK: return DriverDetails::DRIVER_NOUVEAU;
960 case VK_DRIVER_ID_MESA_TURNIP: return DriverDetails::DRIVER_FREEDRENO;
961 default: return DriverDetails::DRIVER_UNKNOWN;
962 // clang-format on
963 }
964 }
965
966 void VulkanContext::InitDriverDetails()
967 {
968 DriverDetails::Vendor vendor;
969 DriverDetails::Driver driver;
970
971 // String comparisons aren't ideal, but there doesn't seem to be any other way to tell
972 // which vendor a driver is for. These names are based on the reports submitted to
973 // vulkan.gpuinfo.org, as of 19/09/2017.
974 std::string device_name = m_device_info.deviceName;
975 u32 vendor_id = m_device_info.vendorID;
976 // Note: driver_id may be 0 on vulkan < 1.2
977 VkDriverId driver_id = m_device_info.driverID;
978
979 if (DriverIsMesa(driver_id))
980 {
981 vendor = DriverDetails::VENDOR_MESA;
982 driver = GetMesaDriver(driver_id);
983 }
984 else if (vendor_id == 0x10DE)
985 {
986 // Currently, there is only the official NV binary driver.
987 // "NVIDIA" does not appear in the device name.
988 vendor = DriverDetails::VENDOR_NVIDIA;
989 driver = DriverDetails::DRIVER_NVIDIA;
990 }
991 else if (vendor_id == 0x1002 || vendor_id == 0x1022 || device_name.contains("AMD"))
992 {
993 // RADV always advertises its name in the device string.
994 // If not RADV, assume the AMD binary driver.
995 if (device_name.contains("RADV"))
996 {
997 vendor = DriverDetails::VENDOR_MESA;
998 driver = DriverDetails::DRIVER_R600;
999 }
1000 else
1001 {
1002 vendor = DriverDetails::VENDOR_ATI;
1003 driver = DriverDetails::DRIVER_ATI;
1004 }
1005 }
1006 else if (vendor_id == 0x8086 || vendor_id == 0x8087 || device_name.contains("Intel"))
1007 {
1008 // Apart from the driver version, Intel does not appear to provide a way to
1009 // differentiate between anv and the binary driver (Skylake+). Assume to be
1010 // using anv if we're not running on Windows or macOS.
1011 #if defined(WIN32) || defined(__APPLE__)
1012 vendor = DriverDetails::VENDOR_INTEL;
1013 driver = DriverDetails::DRIVER_INTEL;
1014 #else
1015 vendor = DriverDetails::VENDOR_MESA;
1016 driver = DriverDetails::DRIVER_I965;
1017 #endif
1018 }
1019 else if (vendor_id == 0x5143 || device_name.contains("Adreno"))
1020 {
1021 // Currently only the Qualcomm binary driver exists for Adreno.
1022 vendor = DriverDetails::VENDOR_QUALCOMM;
1023 driver = DriverDetails::DRIVER_QUALCOMM;
1024 }
1025 else if (vendor_id == 0x13B6 || device_name.contains("Mali"))
1026 {
1027 // Currently only the ARM binary driver exists for Mali.
1028 vendor = DriverDetails::VENDOR_ARM;
1029 driver = DriverDetails::DRIVER_ARM;
1030 }
1031 else if (vendor_id == 0x1010 || device_name.contains("PowerVR"))
1032 {
1033 // Currently only the binary driver exists for PowerVR.
1034 vendor = DriverDetails::VENDOR_IMGTEC;
1035 driver = DriverDetails::DRIVER_IMGTEC;
1036 }
1037 else if (device_name.contains("Apple"))
1038 {
1039 vendor = DriverDetails::VENDOR_APPLE;
1040 driver = DriverDetails::DRIVER_PORTABILITY;
1041 }
1042 else
1043 {
1044 WARN_LOG_FMT(VIDEO, "Unknown Vulkan driver vendor, please report it to us.");
1045 WARN_LOG_FMT(VIDEO, "Vendor ID: {:#X}, Device Name: {}", vendor_id, device_name);
1046 vendor = DriverDetails::VENDOR_UNKNOWN;
1047 driver = DriverDetails::DRIVER_UNKNOWN;
1048 }
1049
1050 // Vulkan on macOS goes through Metal, and is not susceptible to the same bugs
1051 // as the vendor's native Vulkan drivers. We use a different driver fields to
1052 // differentiate MoltenVK.
1053 if (driver_id == VK_DRIVER_ID_MOLTENVK)
1054 driver = DriverDetails::DRIVER_PORTABILITY;
1055
1056 DriverDetails::Init(DriverDetails::API_VULKAN, vendor, driver,
1057 static_cast<double>(m_device_info.driverVersion),
1058 DriverDetails::Family::UNKNOWN, std::move(device_name));
1059 }
1060
1061 bool VulkanContext::SupportsExclusiveFullscreen(const WindowSystemInfo& wsi, VkSurfaceKHR surface)
1062 {
1063 #ifdef SUPPORTS_VULKAN_EXCLUSIVE_FULLSCREEN
1064 if (!surface || !vkGetPhysicalDeviceSurfaceCapabilities2KHR ||
1065 !SupportsDeviceExtension(VK_EXT_FULL_SCREEN_EXCLUSIVE_EXTENSION_NAME))
1066 {
1067 return false;
1068 }
1069
1070 VkPhysicalDeviceSurfaceInfo2KHR si = {};
1071 si.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR;
1072 si.surface = surface;
1073
1074 auto platform_info = GetPlatformExclusiveFullscreenInfo(wsi);
1075 si.pNext = &platform_info;
1076
1077 VkSurfaceCapabilities2KHR caps = {};
1078 caps.sType = VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR;
1079
1080 VkSurfaceCapabilitiesFullScreenExclusiveEXT fullscreen_caps = {};
1081 fullscreen_caps.sType = VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_FULL_SCREEN_EXCLUSIVE_EXT;
1082 fullscreen_caps.fullScreenExclusiveSupported = VK_TRUE;
1083 caps.pNext = &fullscreen_caps;
1084
1085 VkResult res = vkGetPhysicalDeviceSurfaceCapabilities2KHR(m_physical_device, &si, &caps);
1086 if (res != VK_SUCCESS)
1087 {
1088 LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceCapabilities2KHR failed:");
1089 return false;
1090 }
1091
1092 return fullscreen_caps.fullScreenExclusiveSupported;
1093 #else
1094 return false;
1095 #endif
1096 }
1097
1098 #ifdef WIN32
1099 VkSurfaceFullScreenExclusiveWin32InfoEXT
1100 VulkanContext::GetPlatformExclusiveFullscreenInfo(const WindowSystemInfo& wsi)
1101 {
1102 VkSurfaceFullScreenExclusiveWin32InfoEXT info = {};
1103 info.sType = VK_STRUCTURE_TYPE_SURFACE_FULL_SCREEN_EXCLUSIVE_WIN32_INFO_EXT;
1104 info.hmonitor =
1105 MonitorFromWindow(static_cast<HWND>(wsi.render_surface), MONITOR_DEFAULTTOPRIMARY);
1106 return info;
1107 }
1108 #endif
1109
1110 } // namespace Vulkan