Some Vulkan features we would like to use, require us to explicitly enable certain extensions (contrary to OpenGL, in which extensions were automatically/implicitly enabled). There are two kinds, or two levels, of extensions: Instance-level and device-level. Like Instance extensions, device extensions are enabled during logical device creation. We can't ask for a device extension if it is not supported by a given physical device or we won't be able to create a logical device for it. So, before we start creating a logical device, we need to make sure that all requested extensions are supported by a given physical device, or we need to search for another device that supports them all.
Checking available device extensions
How to do it...
- Take one of the physical device handles returned by the vkEnumeratePhysicalDevices() function and store it in a variable of type VkPhysicalDevice called physical_device.
- Prepare a variable of type uint32_t named extensions_count.
- Call vkEnumerateDeviceExtensionProperties( physical_device, nullptr, &extensions_count, nullptr ). In the first parameter, provide the handle of a physical device available on a given hardware platform: the physical_device variable; the second and last parameters should be set to nullptr, and the third parameter should point to the extensions_count variable.
- If a function call is successful, the extensions_count variable will contain the total number of available device-level extensions.
- Prepare the storage for the list of extension properties. The best solution is to use a variable of type std::vector with elements of type VkExtensionProperties. Call it available_extensions.
- Resize the vector to be able to hold at least the extensions_count elements.
- Call vkEnumerateDeviceExtensionProperties( physical_device, nullptr, &extensions_count, &available_extensions[0] ). However, this time, replace the last parameter with a pointer to the first element of an array with elements of type VkExtensionProperties. This array must have enough space to contain at least extensions_count elements. Here, provide a pointer to the first element of the available_extensions variable.
- If the function returns successfully, the available_extensions vector will contain a list of all extensions supported by a given physical device.
How it works...
The process of acquiring the list of supported device-level extensions can be divided into two stages: Firstly, we check how many extensions are supported by a given physical device. This is done by calling a function named vkEnumerateDeviceExtensionProperties() and setting its last parameter to nullptr as follows:
uint32_t extensions_count = 0; VkResult result = VK_SUCCESS; result = vkEnumerateDeviceExtensionProperties( physical_device, nullptr, &extensions_count, nullptr ); if( (result != VK_SUCCESS) || (extensions_count == 0) ) { std::cout << "Could not get the number of device extensions." << std::endl; return false; }
Secondly, we need to prepare an array that will be able to store enough elements of type VkExtensionProperties. In the example, we create a vector variable and resize it so it has the extensions_count number of elements. In the second vkEnumerateDeviceExtensionProperties() function call, we provide an address of the first element of the available_extensions variable. When the call is successful, the variable will be filled with properties (names and versions) of all extensions supported by a given physical device.
available_extensions.resize( extensions_count ); result = vkEnumerateDeviceExtensionProperties( physical_device, nullptr, &extensions_count, &available_extensions[0] ); if( (result != VK_SUCCESS) || (extensions_count == 0) ) { std::cout << "Could not enumerate device extensions." << std::endl; return false; } return true;
Once again, we can see the pattern of calling the same function twice: The first call (with the last parameter set to nullptr) informs us of the number of elements returned by the second call. The second call (with the last parameter pointing to an array of VkExtensionProperties elements) returns the requested data, in this case device extensions, which we can iterate over and check whether the extensions we are interested in are available on a given physical device.
See also
- The following recipes in this chapter:
- Checking available Instance extensions
- Enumerating available physical devices
- The following recipe in Chapter 2, Image Presentation:
- Creating a logical device with WSI extensions enabled