-
Notifications
You must be signed in to change notification settings - Fork 654
/
instance.cpp
551 lines (473 loc) · 19 KB
/
instance.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
/* Copyright (c) 2018-2024, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "physical_device.h"
#include "instance.h"
#include <algorithm>
#include <functional>
#if defined(VKB_DEBUG) || defined(VKB_VALIDATION_LAYERS)
# define USE_VALIDATION_LAYERS
#endif
#if defined(USE_VALIDATION_LAYERS) && (defined(VKB_VALIDATION_LAYERS_GPU_ASSISTED) || defined(VKB_VALIDATION_LAYERS_BEST_PRACTICES) || defined(VKB_VALIDATION_LAYERS_SYNCHRONIZATION))
# define USE_VALIDATION_LAYER_FEATURES
#endif
namespace vkb
{
namespace
{
#ifdef USE_VALIDATION_LAYERS
VKAPI_ATTR VkBool32 VKAPI_CALL debug_utils_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, VkDebugUtilsMessageTypeFlagsEXT message_type,
const VkDebugUtilsMessengerCallbackDataEXT *callback_data,
void *user_data)
{
// Log debug message
if (message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)
{
LOGW("{} - {}: {}", callback_data->messageIdNumber, callback_data->pMessageIdName, callback_data->pMessage);
}
else if (message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
{
LOGE("{} - {}: {}", callback_data->messageIdNumber, callback_data->pMessageIdName, callback_data->pMessage);
}
return VK_FALSE;
}
static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT /*type*/,
uint64_t /*object*/, size_t /*location*/, int32_t /*message_code*/,
const char *layer_prefix, const char *message, void * /*user_data*/)
{
if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT)
{
LOGE("{}: {}", layer_prefix, message);
}
else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT)
{
LOGW("{}: {}", layer_prefix, message);
}
else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT)
{
LOGW("{}: {}", layer_prefix, message);
}
else
{
LOGI("{}: {}", layer_prefix, message);
}
return VK_FALSE;
}
#endif
bool validate_layers(const std::vector<const char *> &required,
const std::vector<VkLayerProperties> &available)
{
for (auto layer : required)
{
bool found = false;
for (auto &available_layer : available)
{
if (strcmp(available_layer.layerName, layer) == 0)
{
found = true;
break;
}
}
if (!found)
{
LOGE("Validation Layer {} not found", layer);
return false;
}
}
return true;
}
} // namespace
std::vector<const char *> get_optimal_validation_layers(const std::vector<VkLayerProperties> &supported_instance_layers)
{
std::vector<std::vector<const char *>> validation_layer_priority_list =
{
// The preferred validation layer is "VK_LAYER_KHRONOS_validation"
{"VK_LAYER_KHRONOS_validation"},
// Otherwise we fallback to using the LunarG meta layer
{"VK_LAYER_LUNARG_standard_validation"},
// Otherwise we attempt to enable the individual layers that compose the LunarG meta layer since it doesn't exist
{
"VK_LAYER_GOOGLE_threading",
"VK_LAYER_LUNARG_parameter_validation",
"VK_LAYER_LUNARG_object_tracker",
"VK_LAYER_LUNARG_core_validation",
"VK_LAYER_GOOGLE_unique_objects",
},
// Otherwise as a last resort we fallback to attempting to enable the LunarG core layer
{"VK_LAYER_LUNARG_core_validation"}};
for (auto &validation_layers : validation_layer_priority_list)
{
if (validate_layers(validation_layers, supported_instance_layers))
{
return validation_layers;
}
LOGW("Couldn't enable validation layers (see log for error) - falling back");
}
// Else return nothing
return {};
}
Optional<uint32_t> Instance::selected_gpu_index;
namespace
{
bool enable_extension(const char *required_ext_name,
const std::vector<VkExtensionProperties> &available_exts,
std::vector<const char *> &enabled_extensions)
{
for (auto &avail_ext_it : available_exts)
{
if (strcmp(avail_ext_it.extensionName, required_ext_name) == 0)
{
auto it = std::find_if(enabled_extensions.begin(), enabled_extensions.end(),
[required_ext_name](const char *enabled_ext_name) {
return strcmp(enabled_ext_name, required_ext_name) == 0;
});
if (it != enabled_extensions.end())
{
// Extension is already enabled
}
else
{
LOGI("Extension {} found, enabling it", required_ext_name);
enabled_extensions.emplace_back(required_ext_name);
}
return true;
}
}
LOGI("Extension {} not found", required_ext_name);
return false;
}
bool enable_all_extensions(const std::vector<const char *> required_ext_names,
const std::vector<VkExtensionProperties> &available_exts,
std::vector<const char *> &enabled_extensions)
{
using std::placeholders::_1;
return std::all_of(required_ext_names.begin(), required_ext_names.end(),
std::bind(enable_extension, _1, available_exts, enabled_extensions));
}
} // namespace
Instance::Instance(const std::string &application_name,
const std::unordered_map<const char *, bool> &required_extensions,
const std::vector<const char *> &required_validation_layers,
const std::vector<VkLayerSettingEXT> &required_layer_settings,
uint32_t api_version)
{
uint32_t instance_extension_count;
VK_CHECK(vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, nullptr));
std::vector<VkExtensionProperties> available_instance_extensions(instance_extension_count);
VK_CHECK(vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, available_instance_extensions.data()));
#ifdef USE_VALIDATION_LAYERS
// Check if VK_EXT_debug_utils is supported, which supersedes VK_EXT_Debug_Report
const bool has_debug_utils = enable_extension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
available_instance_extensions, enabled_extensions);
bool has_debug_report = false;
if (!has_debug_utils)
{
has_debug_report = enable_extension(VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
available_instance_extensions, enabled_extensions);
if (!has_debug_report)
{
LOGW("Neither of {} or {} are available; disabling debug reporting",
VK_EXT_DEBUG_UTILS_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
}
}
#endif
#if (defined(VKB_ENABLE_PORTABILITY))
enable_extension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, available_instance_extensions, enabled_extensions);
bool portability_enumeration_available = enable_extension(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME, available_instance_extensions, enabled_extensions);
#endif
#ifdef USE_VALIDATION_LAYER_FEATURES
bool validation_features = false;
{
uint32_t layer_instance_extension_count;
VK_CHECK(vkEnumerateInstanceExtensionProperties("VK_LAYER_KHRONOS_validation", &layer_instance_extension_count, nullptr));
std::vector<VkExtensionProperties> available_layer_instance_extensions(layer_instance_extension_count);
VK_CHECK(vkEnumerateInstanceExtensionProperties("VK_LAYER_KHRONOS_validation", &layer_instance_extension_count, available_layer_instance_extensions.data()));
for (auto &available_extension : available_layer_instance_extensions)
{
if (strcmp(available_extension.extensionName, VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME) == 0)
{
validation_features = true;
LOGI("{} is available, enabling it", VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME);
enabled_extensions.push_back(VK_EXT_VALIDATION_FEATURES_EXTENSION_NAME);
}
}
}
#endif
// Specific surface extensions are obtained from Window::get_required_surface_extensions
// They are already added to required_extensions by VulkanSample::prepare
// Even for a headless surface a swapchain is still required
enabled_extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
// VK_KHR_get_physical_device_properties2 is a prerequisite of VK_KHR_performance_query
// which will be used for stats gathering where available.
enable_extension(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
available_instance_extensions, enabled_extensions);
auto extension_error = false;
for (auto extension : required_extensions)
{
auto extension_name = extension.first;
auto extension_is_optional = extension.second;
if (!enable_extension(extension_name, available_instance_extensions, enabled_extensions))
{
if (extension_is_optional)
{
LOGW("Optional instance extension {} not available, some features may be disabled", extension_name);
}
else
{
LOGE("Required instance extension {} not available, cannot run", extension_name);
extension_error = true;
}
extension_error = extension_error || !extension_is_optional;
}
}
if (extension_error)
{
throw std::runtime_error("Required instance extensions are missing.");
}
uint32_t instance_layer_count;
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr));
std::vector<VkLayerProperties> supported_validation_layers(instance_layer_count);
VK_CHECK(vkEnumerateInstanceLayerProperties(&instance_layer_count, supported_validation_layers.data()));
std::vector<const char *> requested_validation_layers(required_validation_layers);
#ifdef USE_VALIDATION_LAYERS
// Determine the optimal validation layers to enable that are necessary for useful debugging
std::vector<const char *> optimal_validation_layers = get_optimal_validation_layers(supported_validation_layers);
requested_validation_layers.insert(requested_validation_layers.end(), optimal_validation_layers.begin(), optimal_validation_layers.end());
#endif
if (validate_layers(requested_validation_layers, supported_validation_layers))
{
LOGI("Enabled Validation Layers:")
for (const auto &layer : requested_validation_layers)
{
LOGI(" \t{}", layer);
}
}
else
{
throw std::runtime_error("Required validation layers are missing.");
}
VkApplicationInfo app_info{VK_STRUCTURE_TYPE_APPLICATION_INFO};
app_info.pApplicationName = application_name.c_str();
app_info.applicationVersion = 0;
app_info.pEngineName = "Vulkan Samples";
app_info.engineVersion = 0;
app_info.apiVersion = api_version;
VkInstanceCreateInfo instance_info = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO};
instance_info.pApplicationInfo = &app_info;
instance_info.enabledExtensionCount = to_u32(enabled_extensions.size());
instance_info.ppEnabledExtensionNames = enabled_extensions.data();
instance_info.enabledLayerCount = to_u32(requested_validation_layers.size());
instance_info.ppEnabledLayerNames = requested_validation_layers.data();
#ifdef USE_VALIDATION_LAYERS
VkDebugUtilsMessengerCreateInfoEXT debug_utils_create_info = {VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT};
VkDebugReportCallbackCreateInfoEXT debug_report_create_info = {VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT};
if (has_debug_utils)
{
debug_utils_create_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT;
debug_utils_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
debug_utils_create_info.pfnUserCallback = debug_utils_messenger_callback;
instance_info.pNext = &debug_utils_create_info;
}
else if (has_debug_report)
{
debug_report_create_info.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
debug_report_create_info.pfnCallback = debug_callback;
instance_info.pNext = &debug_report_create_info;
}
#endif
#if (defined(VKB_ENABLE_PORTABILITY))
if (portability_enumeration_available)
{
instance_info.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
}
#endif
// Some of the specialized layers need to be enabled explicitly
#ifdef USE_VALIDATION_LAYER_FEATURES
VkValidationFeaturesEXT validation_features_info = {VK_STRUCTURE_TYPE_VALIDATION_FEATURES_EXT};
std::vector<VkValidationFeatureEnableEXT> enable_features{};
if (validation_features)
{
# if defined(VKB_VALIDATION_LAYERS_GPU_ASSISTED)
enable_features.push_back(VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_RESERVE_BINDING_SLOT_EXT);
enable_features.push_back(VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT);
# endif
# if defined(VKB_VALIDATION_LAYERS_BEST_PRACTICES)
enable_features.push_back(VK_VALIDATION_FEATURE_ENABLE_BEST_PRACTICES_EXT);
# endif
# if defined(VKB_VALIDATION_LAYERS_SYNCHRONIZATION)
enable_features.push_back(VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT);
# endif
validation_features_info.enabledValidationFeatureCount = static_cast<uint32_t>(enable_features.size());
validation_features_info.pEnabledValidationFeatures = enable_features.data();
validation_features_info.pNext = instance_info.pNext;
instance_info.pNext = &validation_features_info;
}
#endif
VkLayerSettingsCreateInfoEXT layerSettingsCreateInfo{VK_STRUCTURE_TYPE_LAYER_SETTINGS_CREATE_INFO_EXT};
// If layer settings extension enabled by sample, then activate layer settings during instance creation
if (std::find(enabled_extensions.begin(), enabled_extensions.end(), VK_EXT_LAYER_SETTINGS_EXTENSION_NAME) != enabled_extensions.end())
{
layerSettingsCreateInfo.settingCount = static_cast<uint32_t>(required_layer_settings.size());
layerSettingsCreateInfo.pSettings = required_layer_settings.data();
layerSettingsCreateInfo.pNext = instance_info.pNext;
instance_info.pNext = &layerSettingsCreateInfo;
}
// Create the Vulkan instance
VkResult result = vkCreateInstance(&instance_info, nullptr, &handle);
if (result != VK_SUCCESS)
{
throw VulkanException(result, "Could not create Vulkan instance");
}
volkLoadInstance(handle);
#ifdef USE_VALIDATION_LAYERS
if (has_debug_utils)
{
result = vkCreateDebugUtilsMessengerEXT(handle, &debug_utils_create_info, nullptr, &debug_utils_messenger);
if (result != VK_SUCCESS)
{
throw VulkanException(result, "Could not create debug utils messenger");
}
}
else if (has_debug_report)
{
result = vkCreateDebugReportCallbackEXT(handle, &debug_report_create_info, nullptr, &debug_report_callback);
if (result != VK_SUCCESS)
{
throw VulkanException(result, "Could not create debug report callback");
}
}
#endif
query_gpus();
}
Instance::Instance(VkInstance instance,
const std::vector<const char *> &externally_enabled_extensions) :
handle{instance}
{
// Some parts of the framework will check for certain extensions to be enabled
// To make those work we need to copy over externally enabled extensions into this class
for (auto extension : externally_enabled_extensions)
{
enabled_extensions.push_back(extension);
}
if (handle != VK_NULL_HANDLE)
{
query_gpus();
}
else
{
throw std::runtime_error("Instance not valid");
}
}
Instance::~Instance()
{
#ifdef USE_VALIDATION_LAYERS
if (debug_utils_messenger != VK_NULL_HANDLE)
{
vkDestroyDebugUtilsMessengerEXT(handle, debug_utils_messenger, nullptr);
}
if (debug_report_callback != VK_NULL_HANDLE)
{
vkDestroyDebugReportCallbackEXT(handle, debug_report_callback, nullptr);
}
#endif
if (handle != VK_NULL_HANDLE)
{
vkDestroyInstance(handle, nullptr);
}
}
void Instance::query_gpus()
{
// Querying valid physical devices on the machine
uint32_t physical_device_count{0};
VK_CHECK(vkEnumeratePhysicalDevices(handle, &physical_device_count, nullptr));
if (physical_device_count < 1)
{
throw std::runtime_error("Couldn't find a physical device that supports Vulkan.");
}
std::vector<VkPhysicalDevice> physical_devices;
physical_devices.resize(physical_device_count);
VK_CHECK(vkEnumeratePhysicalDevices(handle, &physical_device_count, physical_devices.data()));
// Create gpus wrapper objects from the VkPhysicalDevice's
for (auto &physical_device : physical_devices)
{
gpus.push_back(std::make_unique<PhysicalDevice>(*this, physical_device));
}
}
PhysicalDevice &Instance::get_first_gpu()
{
assert(!gpus.empty() && "No physical devices were found on the system.");
// Find a discrete GPU
for (auto &gpu : gpus)
{
if (gpu->get_properties().deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
{
return *gpu;
}
}
// Otherwise just pick the first one
LOGW("Couldn't find a discrete physical device, picking default GPU");
return *gpus[0];
}
PhysicalDevice &Instance::get_suitable_gpu(VkSurfaceKHR surface, bool headless_surface)
{
assert(!gpus.empty() && "No physical devices were found on the system.");
// A GPU can be explicitly selected via the command line (see plugins/gpu_selection.cpp), this overrides the below GPU selection algorithm
if (selected_gpu_index.has_value())
{
LOGI("Explicitly selecting GPU {}", selected_gpu_index.value());
if (selected_gpu_index.value() > gpus.size() - 1)
{
throw std::runtime_error("Selected GPU index is not within no. of available GPUs");
}
return *gpus[selected_gpu_index.value()];
}
if (headless_surface)
{
LOGW("Using headless surface with multiple GPUs. Considered explicitly selecting the target GPU.")
}
// Find a discrete GPU
for (auto &gpu : gpus)
{
if (gpu->get_properties().deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
{
// See if it work with the surface
size_t queue_count = gpu->get_queue_family_properties().size();
for (uint32_t queue_idx = 0; static_cast<size_t>(queue_idx) < queue_count; queue_idx++)
{
if (gpu->is_present_supported(surface, queue_idx))
{
return *gpu;
}
}
}
}
// Otherwise just pick the first one
LOGW("Couldn't find a discrete physical device, picking default GPU");
return *gpus[0];
}
bool Instance::is_enabled(const char *extension) const
{
return std::find_if(enabled_extensions.begin(), enabled_extensions.end(), [extension](const char *enabled_extension) { return strcmp(extension, enabled_extension) == 0; }) != enabled_extensions.end();
}
VkInstance Instance::get_handle() const
{
return handle;
}
const std::vector<const char *> &Instance::get_extensions()
{
return enabled_extensions;
}
} // namespace vkb