Create a Logical Device and Retrieve Queues in Vulkan
Create a VkDevice in C++ with queue create infos, Vulkan 1.3 features and the swapchain extension, then fetch graphics and present queues.

Choosing a physical device only tells you which GPU you want. The logical device, VkDevice, is your working connection to it: the object you create buffers, images, pipelines and command pools from, and the owner of the queues that run your commands. Creating it is the point where you commit to a set of features, extensions and queues, and Vulkan will not let you use anything you did not declare here. This post builds the device in C++17 from the physical device and the queue family indices of the previous post, turns on the Vulkan 1.3 features used by dynamic rendering, and retrieves the queue handles. It also explains the three mistakes that make vkCreateDevice fail or, worse, succeed and misbehave.
What the create call needs
vkCreateDevice takes a physical device and a VkDeviceCreateInfo. That struct holds four things:
Queue requests, an array of
VkDeviceQueueCreateInfo, one per queue family you want queues from, each saying how many queues and with what priority.Device extensions, a list of names such as
VK_KHR_swapchain.Features, the optional capabilities to switch on, passed through a chain of structs hanging off
pNext.Layers, which are obsolete for devices and ignored by modern loaders, so leave them out.
The sections below fill each in.
Request the queues
A queue family can appear only once in the request array, even if you want both graphics and presentation from it. That is the reason the previous post kept two indices that are often equal. Build the unique set, then one create info per member:
#include <vulkan/vulkan.h>
#include <set>
#include <vector>
std::vector<VkDeviceQueueCreateInfo> make_queue_infos(const QueueFamilyIndices& fam,
const float* priority) {
std::set<uint32_t> unique_families = { *fam.graphics, *fam.present };
std::vector<VkDeviceQueueCreateInfo> infos;
for (uint32_t family : unique_families) {
VkDeviceQueueCreateInfo info{};
info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
info.queueFamilyIndex = family;
info.queueCount = 1;
info.pQueuePriorities = priority;
infos.push_back(info);
}
return infos;
}A std::set removes the duplicate automatically, so on a GPU where graphics and present share family 0 the vector holds one entry. queueCount is how many queues to create from that family, and it must not exceed the queueCount the family reported. One queue is enough for a first renderer.
The priority is a float from 0.0 to 1.0, and it is required even when you create a single queue. It hints how the driver should divide GPU time between queues of the same device, with higher values favored. The pointer must stay valid until vkCreateDevice returns, which is why the caller owns the float and passes its address.
Enable the features
Features are off until requested. In Vulkan 1.3 the core ones are grouped in per-version structs. For dynamic rendering and the second synchronization API, which the rest of this series uses, chain the 1.3 struct:
VkPhysicalDeviceVulkan13Features features13{};
features13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES;
features13.dynamicRendering = VK_TRUE;
features13.synchronization2 = VK_TRUE;
VkPhysicalDeviceFeatures2 features2{};
features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
features2.pNext = &features13;
features2.features.samplerAnisotropy = VK_TRUE;There are two ways to enable the older 1.0 features. The pEnabledFeatures member of the create info takes a plain VkPhysicalDeviceFeatures, and the VkPhysicalDeviceFeatures2 struct chained through pNext carries them with the extension structs. Pick one. Supplying both, with pEnabledFeatures non-null and a Features2 in the chain, is invalid, and the validation layer reports it. The code above uses the chained form and leaves pEnabledFeatures null.
Every feature you set to true must have been reported as supported by the physical device. Enabling an unsupported one makes vkCreateDevice return VK_ERROR_FEATURE_NOT_PRESENT, which is why device selection checks the same features first.
Name the extensions
The swapchain is an extension, even though every desktop driver offers it:
const char* device_extensions[] = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
};If you add more later, such as VK_EXT_memory_budget, append names here, and check that the physical device supports each. On macOS the device must also enable VK_KHR_portability_subset when the physical device advertises it, as MoltenVK does, since the specification requires an application to enable that extension on any device that exposes it:
std::vector<const char*> extensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
#ifdef __APPLE__
extensions.push_back("VK_KHR_portability_subset");
#endifThere is no macro in the core headers for that name unless the beta headers are included, so the string is spelled out. Misspelling an extension name compiles and then fails with VK_ERROR_EXTENSION_NOT_PRESENT.
Create the device
Put the pieces together, with the features chain in pNext:
struct DeviceContext {
VkDevice device = VK_NULL_HANDLE;
VkQueue graphics_queue = VK_NULL_HANDLE;
VkQueue present_queue = VK_NULL_HANDLE;
};
DeviceContext create_logical_device(VkPhysicalDevice physical, const QueueFamilyIndices& fam) {
const float priority = 1.0f;
std::vector<VkDeviceQueueCreateInfo> queue_infos = make_queue_infos(fam, &priority);
VkPhysicalDeviceVulkan13Features features13{};
features13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES;
features13.dynamicRendering = VK_TRUE;
features13.synchronization2 = VK_TRUE;
VkPhysicalDeviceFeatures2 features2{};
features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
features2.pNext = &features13;
features2.features.samplerAnisotropy = VK_TRUE;
std::vector<const char*> extensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME };
#ifdef __APPLE__
extensions.push_back("VK_KHR_portability_subset");
#endif
VkDeviceCreateInfo ci{};
ci.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
ci.pNext = &features2;
ci.queueCreateInfoCount = static_cast<uint32_t>(queue_infos.size());
ci.pQueueCreateInfos = queue_infos.data();
ci.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
ci.ppEnabledExtensionNames = extensions.data();
ci.pEnabledFeatures = nullptr;
DeviceContext ctx;
VkResult r = vkCreateDevice(physical, &ci, nullptr, &ctx.device);
if (r != VK_SUCCESS)
throw std::runtime_error("vkCreateDevice failed, VkResult " + std::to_string(r));
vkGetDeviceQueue(ctx.device, *fam.graphics, 0, &ctx.graphics_queue);
vkGetDeviceQueue(ctx.device, *fam.present, 0, &ctx.present_queue);
return ctx;
}The create call returns the device but not the queues. Queues come into existence with the device, and you fetch the handles with vkGetDeviceQueue(device, family, index, &queue). The index is the queue's position within the family, 0 for the first, and it must be below the queueCount you requested. Queue handles are owned by the device, are never created or destroyed on their own, and become invalid when the device is destroyed.
When graphics and present share a family, both calls ask for family 0, index 0, and return the same handle. Code that treats them as separate objects still works, which is the reason to keep two variables.
You can check the result by printing the handles, or better, whether they are equal:
DeviceContext ctx = create_logical_device(gpu.device, gpu.families);
std::printf("graphics family %u, present family %u, same queue: %s\n",
*gpu.families.graphics, *gpu.families.present,
ctx.graphics_queue == ctx.present_queue ? "yes" : "no");graphics family 0, present family 0, same queue: yesDestroy it in order
The device is destroyed with vkDestroyDevice(device, nullptr), after every object created from it: pipelines, command pools, buffers, images, swapchains, semaphores and fences. Before the call, wait until the GPU has finished any work in flight, using vkDeviceWaitIdle:
vkDeviceWaitIdle(ctx.device);
// destroy swapchain, pipelines, pools, buffers ...
vkDestroyDevice(ctx.device, nullptr);Queues need no destruction. Destroying a device while work is still running is undefined behavior, and the validation layer reports leaked children by name, which makes it a useful checklist of what you forgot.
Three mistakes that look like driver bugs
The same family twice. Listing family 0 once for graphics and once for present in the queue create infos is invalid. The validation layer message mentions queueFamilyIndex appearing more than once. The set of unique indices prevents it.
**A feature structure with the wrong sType.** The chain is read by the sType of each struct, so a copy-paste error that leaves the 1.3 struct tagged as the 1.2 one makes the driver read the struct as the wrong type. Features silently stay off, and then a call such as vkCmdBeginRendering fails or crashes with a message that dynamic rendering was not enabled. Always set sType right after declaring a struct.
A missing priority. A null pQueuePriorities crashes inside the driver or triggers a validation error. Every queue create info needs a pointer to queueCount floats in the range 0.0 to 1.0.
A fourth, common on new machines: the code requests 1.3 features and the device supports only 1.2. Device selection should have excluded it, and a program that skipped the check gets VK_ERROR_FEATURE_NOT_PRESENT here.
Using more than one queue
A first renderer uses one queue per family. More queues in the same family let you submit from several threads at once, since each queue can be submitted to independently but a single queue needs external synchronization. A dedicated transfer family enables background uploads. To request two queues from a family, set queueCount = 2 and supply two priorities:
const float priorities[2] = { 1.0f, 0.5f };
VkDeviceQueueCreateInfo info{};
info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
info.queueFamilyIndex = family;
info.queueCount = 2;
info.pQueuePriorities = priorities;
// later: vkGetDeviceQueue(device, family, 0, &main_queue);
// vkGetDeviceQueue(device, family, 1, &upload_queue);The second queue has half the priority of the first, a hint that the uploads should not starve the main work. Whether the hint changes anything is the driver's decision, and some drivers ignore it.
What comes next
With a device and queues, the program can create a command pool, allocate command buffers, and submit work, which the next post does with the smallest possible command buffer. Swapchains, pipelines and memory all hang off the device too. Keep the DeviceContext accessible, or pass the VkDevice to everything that needs it, because nearly every later Vulkan call takes it as its first argument.
The structures and rules are specified in the Vulkan specification, Devices chapter.


