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 typeVkPhysicalDevice
calledphysical_device
. - Prepare a variable of type
uint32_t
namedqueue_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
VkQueueFlags
nameddesired_capabilities
. Store the desired types of operations in thedesired_capabilities
variables--it can be a logicalOR
operation of any of theVK_QUEUE_GRAPHICS_BIT
,VK_QUEUE_COMPUTE_BIT
,VK_QUEUE_TRANSFER_BIT
orVK_QUEUE_SPARSE_BINDING_BIT
values. - Create a variable of type
std::vector
withVkQueueFamilyProperties
elements 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_families
variable. - Loop over all elements of the
queue_families
vector using a variable of typeuint32_t
namedindex
. - For each element of the
queue_families
variable:- Check if the number of queues (indicated by the
queueCount
member) in the current element is greater than zero. - Check if the logical
AND
operation of thedesired_capabilities
variable and thequeueFlags
member of the currently iterated element is not equal to zero. - If both checks are positive, store the value of an
index
variable (current loop iteration) in thequeue_family_index
variable, 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_families
vector 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