[Translated] Vulkan Guide (08) and the logical device queue

[Translated] Vulkan Guide (08) and the logical device queue

Introduction Getting Started

After selecting a physical device to use we need to set up a logical device to interface with it. The logical device creation process is similar to the instance creation process and describes the features we want to use. We also need to specify which queues to create now that we've queried which queue families are available. You can even create multiple logical devices from the same physical device if you have varying requirements.

After selecting the physical device to use, we need to set up a logical device to interact with it. The creation of logical devices and the creation of a similar instance, it describes the characteristics we want to use. Now that we have a query queue which family available, we also need to indicate which want to create a queue. If you turn a variety of needs, you can also create multiple logical devices from the same physical device.

Start by adding a new class member to store the logical device handle in.

Start, add a new member to handle logical storage devices.

VkDevice device;

 

Next, add a createLogicalDevice function that is called from initVulkan.

Next, add createLogicalDevice a function, in initVulkancalling it in.

 1 void initVulkan() {
 2     createInstance();
 3     setupDebugCallback();
 4     pickPhysicalDevice();
 5     createLogicalDevice();
 6 }
 7  
 8 void createLogicalDevice() {
 9  
10 }

 

Specifying the queues to be created to create a queue marked

The creation of a logical device involves specifying a bunch of details in structs again, of which the first one will be VkDeviceQueueCreateInfo. This structure describes the number of queues we want for a single queue family. Right now we're only interested in a queue with graphics capabilities.

Creating logical device, many details need to be marked in a number of struct, where the first one is VkDeviceQueueCreateInfo. This struct describes the number of queues we want to get from one queue family. Now we just have to queue interested in graphics capabilities.

1 QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
2  
3 VkDeviceQueueCreateInfo queueCreateInfo = {};
4 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
5 queueCreateInfo.queueFamilyIndex = indices.graphicsFamily.value();
6 queueCreateInfo.queueCount = 1;

 

The currently available drivers will only allow you to create a small number of queues for each queue family and you don't really need more than one. That's because you can create all of the command buffers on multiple threads and then submit them all at once on the main thread with a single low-overhead call.

Currently available driver allows you to create only a small amount of each queue queue from the family, but you do not need to create more than one. This is because you can create multiple threads on all orders cache, and then submit them all at once in the main thread, which only a low-overhead call.

Vulkan lets you assign priorities to queues to influence the scheduling of command buffer execution using floating point numbers between 0.0 and 1.0. This is required even if there is only a single queue:

Vulkan let you queue given priority ( 0.0to 1.0floating point), to influence the command cache implementation arrangements. Even if only one queue, which is necessary:

1 float queuePriority = 1.0f;
2 queueCreateInfo.pQueuePriorities = &queuePriority;

 

Specifying used device features indicate the use of the device characteristics

The next information to specify is the set of device features that we'll be using. These are the features that we queried support for with vkGetPhysicalDeviceFeatures in the previous chapter, like geometry shaders. Right now we don't need anything special, so we can simply define it and leave everything to VK_FALSE. We'll come back to this structure once we're about to start doing more interesting things with Vulkan.

The next information to be marked, that we have to use the device characteristics. They are used in the previous chapter we vkGetPhysicalDeviceFeatures inquired of the function of those features are supported. Now we do not need any special things, so we can simply define it, let everything remain VK_FALSE. And so we want to start doing more with Vulkan are interested in something, we will come back to this struct.

VkPhysicalDeviceFeatures deviceFeatures = {};

 

Creating the logical device to create a logic device

With the previous two structures in place, we can start filling in the main VkDeviceCreateInfo structure.

After the first 2 struct ready, we can start to fill in the VkDeviceCreateInfo structure of the body.

1 VkDeviceCreateInfo createInfo = {};
2 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;

 

First add pointers to the queue creation info and device features structs:

First, create a pointer to add information and equipment characteristics of a struct:

1 createInfo.pQueueCreateInfos = &queueCreateInfo;
2 createInfo.queueCreateInfoCount = 1;
3  
4 createInfo.pEnabledFeatures = &deviceFeatures;

 

The remainder of the information bears a resemblance to the VkInstanceCreateInfo struct and requires you to specify extensions and validation layers. The difference is that these are device specific this time.、

The rest of the information VkInstanceCreateInfo is similar, asking you to indicate expansion and authentication layer. The difference is, this time for a specific device.

An example of a device specific extension is VK_KHR_swapchain, which allows you to present rendered images from that device to windows. It is possible that there are Vulkan devices in the system that lack this ability, for example because they only support compute operations. We will come back to this extension in the swap chain chapter.

An example of expansion for a particular device is that VK_KHR_swapchainit allows you to present images from the device to the window. There may be a lack of function of this apparatus Vulkan system, such as computing device only support operations. We will talk about this again in a swap chain extension section.

Previous implementations of Vulkan made a distinction between instance and device specific validation layers, but this is no longer the case. That means that the enabledLayerCount and ppEnabledLayerNames fields of VkDeviceCreateInfo are ignored by up-to-date implementations. However, it is still a good idea to set them anyway to be compatible with older implementations:

Vulkan achieved earlier distinguished instance validation layer and related equipment, but now no longer the case . This means that, VkDeviceCreateInfo in enabledLayerCount and ppEnabledLayerNames fields are ignored implement the new version. However, to achieve compatibility with older, setting them is still a good idea:

1 createInfo.enabledExtensionCount = 0;
2  
3 if (enableValidationLayers) {
4     createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
5     createInfo.ppEnabledLayerNames = validationLayers.data();
6 } else {
7     createInfo.enabledLayerCount = 0;
8 }

 

We won't need any device specific extensions for now.

Now we do not need any equipment related to expanded.

That's it, we're now ready to instantiate the logical device with a call to the appropriately named vkCreateDevicefunction.

In this way, we are now ready for the initialization logic device (with vkCreateDevicefunction function).

1 if (vkCreateDevice(physicalDevice, &createInfo, nullptr, &device) != VK_SUCCESS) {
2     throw std::runtime_error("failed to create logical device!");
3 }

 

The parameters are the physical device to interface with, the queue and usage info we just specified, the optional allocation callbacks pointer and a pointer to a variable to store the logical device handle in. Similarly to the instance creation function, this call can return errors based on enabling non-existent extensions or specifying the desired usage of unsupported features.

These parameters are, the physical address of the device interact, and their usage queue (we have labeled), optional callback function pointer, a logical storage device handle variables. Function and create a similar instance, the call will be extended due to missing or marked unsupported features and returns an error code.

The device should be destroyed in cleanup with the vkDestroyDevice function:

Equipment should cleanup function using vkDestroyDevice function to destroy:

1 void cleanup() {
2     vkDestroyDevice(device, nullptr);
3     ...
4 }

 

Logical devices don't interact directly with instances, which is why it's not included as a parameter.

Logic devices do not interact directly with the instance, which is why it has not been passed as a parameter to the device.

Retrieving queue handles retrieve the handle of the queue

The queues are automatically created along with the logical device, but we don't have a handle to interface with them yet. First add a class member to store a handle to the graphics queue:

With the creation of logical device queue is automatically created, but we did not interact with the handle. First, add a member to hold the handle of the graphics queue:

VkQueue graphicsQueue;

 

Device queues are implicitly cleaned up when the device is destroyed, so we don't need to do anything in cleanup.

The device will automatically queue implicitly destroyed when the equipment is destroyed, so we do not need to cleanupdo anything in.

We can use the vkGetDeviceQueue function to retrieve queue handles for each queue family. The parameters are the logical device, queue family, queue index and a pointer to the variable to store the queue handle in. Because we're only creating a single queue from this family, we'll simply use index 0.

We can vkGetDeviceQueue retrieve the queue handle each queue family of functions. Its parameters are, the pointer variable logic device, family queue, and queue index storage queue handle. Because we only create a queue, we use an index 0can be.

vkGetDeviceQueue(device, indices.graphicsFamily.value(), 0, &graphicsQueue);

 

With the logical device and queue handles we can now actually start using the graphics card to do things! In the next few chapters we'll set up the resources to present results to the window system.

With logical device and queue handle, and now we can really do something with the graphics card! The next few chapters, we will set up resources in order to present the results to the window system.

++ code C C ++ Code

 

 

Guess you like

Origin www.cnblogs.com/bitzhuwei/p/Vulkan-Tutorial-08-Logical-device-and-queue.html