Vulkan extensions
The concept
Vulkan is designed to be platform-independent. By default, it does not know how to communicate with the window manager of our operating system (Windows, Linux, etc.). We therefore need to help it by providing extensions when creating the Vulkan instance.
When using the GLFW library, we can retrieve these extensions by calling the
glfwGetRequiredInstanceExtensions function:
std::vector<const char*> extensions;
uint32_t glfwExtensionCount = 0;
const char** glfwExt = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
extensions.insert(extensions.end(),
glfwExt,
glfwExt + glfwExtensionCount);We can also add VK_EXT_DEBUG_UTILS_EXTENSION_NAME for debug utils:
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);And on macOS we need another extension to communicate with the MoltenVK API:
#if defined(__APPLE__)
extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
#endifAs with validation layers, we also need to check
that our extensions are available on this machine. This is necessary because if
we try to use a Windows extension such as VK_KHR_win32_surface on Linux, it
will cause a crash because the extension is invalid. The same applies to GPUs:
if we use an NVIDIA extension on an AMD card, it will also crash.
bool checkExtensionsSupport(
const std::vector<const char*>& requiredExtensions,
const std::vector<vk::ExtensionProperties>& availableExtensions)
{
for (const auto& required : requiredExtensions) {
const bool found = std::ranges::any_of(
availableExtensions,
[required](const auto& prop) {
return strcmp(prop.extensionName, required) == 0;
});
if (!found) {
std::cerr << "[Error] Missing required extension: " << required
<< std::endl;
return false;
}
}
return true;
}