Checking available Instance extensions
Vulkan Instance gathers per application state and allows us to create a logical device on which almost all operations are performed. Before we can create an Instance object, we should think about the instance-level extensions we want to enable. An example of one of the most important instance-level extensions are swapchain related extensions, which are used to display images on screen.
Extensions in Vulkan, as opposed to OpenGL, are enabled explicitly. We can't create a Vulkan Instance and request extensions that are not supported, because the Instance creation operation will fail. That's why we need to check which extensions are supported on a given hardware platform.
How to do it...
- Prepare a variable of type
uint32_t
namedextensions_count
. - Call
vkEnumerateInstanceExtensionProperties( nullptr, &extensions_count, nullptr )
. All parameters should be set tonullptr
, except for the second parameter, which should point to theextensions_count
variable. - If a function call is successful, the total number of available instance-level extensions will be stored in the
extensions_count
variable. - Prepare a storage for the list of extension properties. It must contain elements of type
VkExtensionProperties
. The best solution is to use astd::vector
container. Call itavailable_extensions
. - Resize the vector to be able to hold at least the
extensions_count
elements.
- Call
vkEnumerateInstanceExtensionProperties( nullptr, &extensions_count, &available_extensions[0] )
. The first parameter is once again set tonullptr
; the second parameter should point to theextensions_count
variable; the third parameter must point to an array of at leastextensions_count
elements of typeVkExtensionProperties
. Here, in the third parameter, provide an address of the first element of theavailable_extensions
vector. - If the function returns successfully, the
available_extensions
vector variable will contain a list of all extensions supported on a given hardware platform.
How it works...
Code that acquires instance-level extensions can be divided into two stages. First we get the total number of available extensions as follows:
uint32_t extensions_count = 0; VkResult result = VK_SUCCESS; result = vkEnumerateInstanceExtensionProperties( nullptr, &extensions_count, nullptr ); if( (result != VK_SUCCESS) || (extensions_count == 0) ) { std::cout << "Could not get the number of Instance extensions." << std::endl; return false; }
When called with the last parameter set to nullptr
, the vkEnumerateInstanceExtensionProperties()
function stores the number of available extensions in the variable pointed to in the second parameter. This way, we know how many extensions are on a given platform and how much space we need to be able to store parameters for all of them.
When we are ready to acquire extensions' properties, we can call the same function once again. This time the last parameter should point to the prepared space (an array of VkExtensionProperties
elements, or a vector, in our case) in which these properties will be stored:
available_extensions.resize( extensions_count ); result = vkEnumerateInstanceExtensionProperties( nullptr, &extensions_count, &available_extensions[0] ); if( (result != VK_SUCCESS) || (extensions_count == 0) ) { std::cout << "Could not enumerate Instance extensions." << std::endl; return false; } return true;
Note
The pattern of calling the same function twice is common in Vulkan. There are multiple functions, which store the number of elements returned in the query when their last argument is set to nullptr
. When their last element points to an appropriate variable, they return the data itself.
Now that we have the list, we can look through it and check whether the extensions we would like to enable are available on a given platform.
See also
- The following recipes in this chapter:
- Checking available device extensions
- The following recipe in Chapter 2, Image Presentation:
- Creating a Vulkan Instance with WSI extensions enabled