Selecting the index of a queue family with the desired capabilities
Before we can create a logical device, we need to think about what operations we want to perform on it, because this will affect our choice of a queue family (or families) from which we want to request queues.
For simple use cases, a single queue from a family that supports graphics operations should be enough. More advanced scenarios will require graphics and compute operations to be supported, or even an additional transfer queue for very fast memory copying.
In this recipe, we will look at how to search for a queue family that supports the desired type of operations.
How to do it...
- Take one of the physical device handles returned by the
vkEnumeratePhysicalDevices()function and store it in a variable of typeVkPhysicalDevicecalledphysical_device. - Prepare a variable of type
uint32_tnamedqueue_family_index. In it, we will store an index of a queue family that supports selected types of operations. - Create a bit field variable of type
VkQueueFlagsnameddesired_capabilities. Store the desired types of operations in thedesired_capabilitiesvariables--it can be a logicalORoperation of any of theVK_QUEUE_GRAPHICS_BIT,VK_QUEUE_COMPUTE_BIT,VK_QUEUE_TRANSFER_BITorVK_QUEUE_SPARSE_BINDING_BITvalues. - Create a variable of type
std::vectorwithVkQueueFamilyPropertieselements namedqueue_families. - Check the number of available queue families and acquire their properties as described in the Checking available queue families and their properties recipe. Store the results of this operation in the
queue_familiesvariable. - Loop over all elements of the
queue_familiesvector using a variable of typeuint32_tnamedindex. - For each element of the
queue_familiesvariable:- Check if the number of queues (indicated by the
queueCountmember) in the current element is greater than zero. - Check if the logical
ANDoperation of thedesired_capabilitiesvariable and thequeueFlagsmember of the currently iterated element is not equal to zero. - If both checks are positive, store the value of an
indexvariable (current loop iteration) in thequeue_family_indexvariable, and finish iterating.
- Check if the number of queues (indicated by the
- Repeat steps from 7.1 to 7.3 until all elements of the
queue_familiesvector are viewed.
How it works...
First, we acquire the properties of queue families available on a given physical device. This is the operation described in the Checking available queue families and their properties recipe. We store the results of the query in the queue_families variable, which is of std::vector type with VkQueueFamilyProperties elements:
std::vector<VkQueueFamilyProperties> queue_families;
if( !CheckAvailableQueueFamiliesAndTheirProperties( physical_device, queue_families ) ) {
return false;
}Next, we start inspecting all elements of a queue_families vector:
for( uint32_t index = 0; index < static_cast<uint32_t>(queue_families.size()); ++index ) {
if( (queue_families[index].queueCount > 0) &&
(queue_families[index].queueFlags & desired_capabilities ) ) {
queue_family_index = index;
return true;
}
}
return false;Each element of the queue_families vector represents a separate queue family. Its queueCount member contains the number of queues available in a given family. The queueFlags member is a bit field, in which each bit represents a different type of operation. If a given bit is set, it means that the corresponding type of operation is supported by the given queue family. We can check for any combination of supported operations, but we may need to search for separate queues for every type of operation. This solely depends on the hardware support and the Vulkan API driver.
To be sure that the data we have acquired is correct, we also check if each family exposes at least one queue.
More advanced real-life scenarios would require us to store the total number of queues exposed in each family. This is because we may want to request more than one queue, but we can't request more queues than are available in a given family. In simple use cases, one queue from a given family is enough.
See also
- The following recipes in this chapter:
- Checking available queue families and their properties
- Creating a logical device
- Getting a device queue
- Creating a logical device with geometry shader, graphics, and compute queues
- The following recipe in Chapter 2, Image Presentation:
- Selecting a queue family that supports the presentation to a given surface