[译]Vulkan教程(07)物理设备和队列家族

Selecting a physical device 选择一个物理设备

After initializing the Vulkan library through a VkInstance we need to look for and select a graphics card in the system that supports the features we need. In fact we can select any number of graphics cards and use them simultaneously, but in this tutorial we'll stick to the first graphics card that suits our needs.

通过VkInstance 初始化了Vulkan库之后,我们需要在系统中查找和选择一个支持我们需要的特性的图形卡。实际上我们可以选择任意多个图形卡,同步地使用它们,但是本教程中我们只使用第一个满足我们需要的图形卡。

We'll add a function pickPhysicalDevice and add a call to it in the initVulkan function.

我们添加一个函数pickPhysicalDevice ,在initVulkan 函数中调用它。

 void initVulkan() {
createInstance();
setupDebugCallback();
pickPhysicalDevice();
} void pickPhysicalDevice() { }

The graphics card that we'll end up selecting will be stored in a VkPhysicalDevice handle that is added as a new class member. This object will be implicitly destroyed when the VkInstance is destroyed, so we won't need to do anything new in the cleanup function.

我们添加一个VkPhysicalDevice 成员,用于保存我们将选择的图形卡。这个对象会在VkInstance 被销毁时隐式地销毁,所以我们不需要在cleanup 函数中做什么。

VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;

Listing the graphics cards is very similar to listing extensions and starts with querying just the number.

列举图形卡,与列举扩展很相似,开始要查询数量。

 uint32_t deviceCount = ;
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);

If there are 0 devices with Vulkan support then there is no point going further.

如果支持Vulkan的设备数量为0,那么就没有必要继续了。

 if (deviceCount == ) {
throw std::runtime_error("failed to find GPUs with Vulkan support!");
}

Otherwise we can now allocate an array to hold all of the VkPhysicalDevice handles.

如果有,我们就申请一个数组来记录所有的VkPhysicalDevice 句柄。

 std::vector<VkPhysicalDevice> devices(deviceCount);
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());

Now we need to evaluate each of them and check if they are suitable for the operations we want to perform, because not all graphics cards are created equal. For that we'll introduce a new function:

现在我们需要评估它们,检查它们是不是适合我们需要施展的操作,因为不是所有的图形卡都一样。为此我们引入一个新的函数:

 bool isDeviceSuitable(VkPhysicalDevice device) {
return true;
}

And we'll check if any of the physical devices meet the requirements that we'll add to that function.

我们将检查设备是否符合我们在此函数中设定的要求。

 for (const auto& device : devices) {
if (isDeviceSuitable(device)) {
physicalDevice = device;
break;
}
} if (physicalDevice == VK_NULL_HANDLE) {
throw std::runtime_error("failed to find a suitable GPU!");
}

The next section will introduce the first requirements that we'll check for in the isDeviceSuitable function. As we'll start using more Vulkan features in the later chapters we will also extend this function to include more checks.

下一节将引入第一个要求,我们将在isDeviceSuitable 函数中检查它。随着我们将使用更多的Vulkan特性,我们还会逐步扩展这个函数,加入更多的检查。

Base device suitability checks 基础的设备适用性检查

To evaluate the suitability of a device we can start by querying for some details. Basic device properties like the name, type and supported Vulkan version can be queried using vkGetPhysicalDeviceProperties.

为了估计设备的适用性,我们可以从查询某些细节开始。基础的设备属性(例如名字、类型和支持的Vulkan版本)可以用vkGetPhysicalDeviceProperties查询。

 VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(device, &deviceProperties);

The support for optional features like texture compression, 64 bit floats and multi viewport rendering (useful for VR) can be queried using vkGetPhysicalDeviceFeatures:

对于可选特性(例如纹理压缩、64位浮点数和多视口渲染(用于VR))的支持,可以用vkGetPhysicalDeviceFeatures查询。

VkPhysicalDeviceFeatures deviceFeatures;
vkGetPhysicalDeviceFeatures(device, &deviceFeatures);

There are more details that can be queried from devices that we'll discuss later concerning device memory and queue families (see the next section).

关于设备内存和队列家族(见下一节)方面,设备还有更多细节可供查询,我们将在以后讨论之。

As an example, let's say we consider our application only usable for dedicated graphics cards that support geometry shaders. Then the isDeviceSuitable function would look like this:

举个例子,假设我们的app只使用支持几何shader的专用图形卡。那么isDeviceSuitable 函数应该是这样:

bool isDeviceSuitable(VkPhysicalDevice device) {
VkPhysicalDeviceProperties deviceProperties;
VkPhysicalDeviceFeatures deviceFeatures;
vkGetPhysicalDeviceProperties(device, &deviceProperties);
vkGetPhysicalDeviceFeatures(device, &deviceFeatures); return deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
deviceFeatures.geometryShader;
}

Instead of just checking if a device is suitable or not and going with the first one, you could also give each device a score and pick the highest one. That way you could favor a dedicated graphics card by giving it a higher score, but fall back to an integrated GPU if that's the only available one. You could implement something like that as follows:

你可以检查一个设备适用与否,然后总选第一个合适的,你更可以给每个设备一个评分,选择最高分的。那样你就可以通过最高分来找到最专业的图形卡,而若只有1个适用的,你也可以选到那个。你可以实现一些这样的东西:

#include <map>

...

void pickPhysicalDevice() {
... // Use an ordered map to automatically sort candidates by increasing score
std::multimap<int, VkPhysicalDevice> candidates; for (const auto& device : devices) {
int score = rateDeviceSuitability(device);
candidates.insert(std::make_pair(score, device));
} // Check if the best candidate is suitable at all
if (candidates.rbegin()->first > ) {
physicalDevice = candidates.rbegin()->second;
} else {
throw std::runtime_error("failed to find a suitable GPU!");
}
} int rateDeviceSuitability(VkPhysicalDevice device) {
... int score = ; // Discrete GPUs have a significant performance advantage
if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {
score += ;
} // Maximum possible size of textures affects graphics quality
score += deviceProperties.limits.maxImageDimension2D; // Application can't function without geometry shaders
if (!deviceFeatures.geometryShader) {
return ;
} return score;
}

You don't need to implement all that for this tutorial, but it's to give you an idea of how you could design your device selection process. Of course you can also just display the names of the choices and allow the user to select.

在本教程中你不需要实现所有这些,但这些可以给你一点启示(关于如何设计你的设备选择流程)。当然你可以只显示选项名字,让用户来选择。

Because we're just starting out, Vulkan support is the only thing we need and therefore we'll settle for just any GPU:

因为我们是刚刚开始,Vulkan支持是我们唯一需要的,因此我们对任何GPU都能行:

bool isDeviceSuitable(VkPhysicalDevice device) {
return true;
}

In the next section we'll discuss the first real required feature to check for.

下一节我们将讨论要检查的第一个特性。

Queue families 队列家族

It has been briefly touched upon before that almost every operation in Vulkan, anything from drawing to uploading textures, requires commands to be submitted to a queue. There are different types of queues that originate from different queue families and each family of queues allows only a subset of commands. For example, there could be a queue family that only allows processing of compute commands or one that only allows memory transfer related commands.

之前简要提到过,Vulkan中的任何操作,从绘画到上传纹理,都需要将命令提交到一个队列。有多种类型的queue,它们起源于不同的队列家族,每个家族里的队列都只能做某一些特定的命令。例如,可能有一个队列家族,它只能处理计算命令,或者只能执行内存转移相关的命令。

We need to check which queue families are supported by the device and which one of these supports the commands that we want to use. For that purpose we'll add a new function findQueueFamilies that looks for all the queue families we need. Right now we'll only look for a queue that supports graphics commands, but we may extend this function to look for more at a later point in time.

我们需要检查,设备支持哪些队列家族,其中哪个支持我们想使用的命令。为此,我们添加新函数findQueueFamilies ,它查询我们需要的所有的队列家族。现在我们将只查找支持图形命令的队列,但是我们可能以后扩展这个函数,以查询更多东西。

This function will return the indices of the queue families that satisfy certain desired properties. The best way to do that is using a structure where we use std::optional to track if an index was found:

这个函数会返回满足给定属性的队列家族的索引。最好的方式是用一个struct(我们用std::optional )来追踪一个索引是否被找到了:

struct QueueFamilyIndices {
std::optional<uint32_t> graphicsFamily; bool isComplete() {
return graphicsFamily.has_value();
}
};

Note that this also requires including <optional>. We can now begin implementing findQueueFamilies:

注意这需要include一下<optional>。现在我们可以开始实现findQueueFamilies了:

QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
QueueFamilyIndices indices; ... return indices;
}

The process of retrieving the list of queue families is exactly what you expect and uses vkGetPhysicalDeviceQueueFamilyProperties:

如你所料,检索家族列表的过程,就是要用vkGetPhysicalDeviceQueueFamilyProperties

uint32_t queueFamilyCount = ;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr); std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());

The VkQueueFamilyProperties struct contains some details about the queue family, including the type of operations that are supported and the number of queues that can be created based on that family. We need to find at least one queue family that supports VK_QUEUE_GRAPHICS_BIT.

结构体VkQueueFamilyProperties 包含队列家族的一些细节,包括支持的操作类型,可以创建的队列数量。我们需要找到至少一个支持VK_QUEUE_GRAPHICS_BIT的队列家族。

int i = ;
for (const auto& queueFamily : queueFamilies) {
if (queueFamily.queueCount > && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
indices.graphicsFamily = i;
} if (indices.isComplete()) {
break;
} i++;
}

Now that we have this fancy queue family lookup function, we can use it as a check in the isDeviceSuitablefunction to ensure that the device can process the commands we want to use:

现在我们有了查询队列家族的函数,我们可以在isDeviceSuitable函数中用它做个判断,确保设备能处理我们想用的命令:

bool isDeviceSuitable(VkPhysicalDevice device) {
QueueFamilyIndices indices = findQueueFamilies(device); return indices.isComplete();
}

Great, that's all we need for now to find the right physical device! The next step is to create a logical device to interface with it.

好极了,这就是我们目前需要的用以找到正确的物理设备的所有东西!下一步是创建逻辑设备,然后与之交互。

C++ code C++代码

[译]Vulkan教程(07)物理设备和队列家族的更多相关文章

  1. [译]Vulkan教程(08)逻辑设备和队列

    [译]Vulkan教程(08)逻辑设备和队列 Introduction 入门 After selecting a physical device to use we need to set up a  ...

  2. [译]Vulkan教程(23)暂存buffer

    [译]Vulkan教程(23)暂存buffer Staging buffer 暂存buffer Introduction 入门 The vertex buffer we have right now ...

  3. [译]Vulkan教程(09)窗口表面

    [译]Vulkan教程(09)窗口表面 Since Vulkan is a platform agnostic API, it can not interface directly with the ...

  4. [译]Vulkan教程(27)Image

    [译]Vulkan教程(27)Image Images Introduction 入门 The geometry has been colored using per-vertex colors so ...

  5. [译]Vulkan教程(22)创建顶点buffer

    [译]Vulkan教程(22)创建顶点buffer Vertex buffer creation 创建顶点buffer Introduction 入门 Buffers in Vulkan are re ...

  6. [译]Vulkan教程(10)交换链

    [译]Vulkan教程(10)交换链 Vulkan does not have the concept of a "default framebuffer", hence it r ...

  7. [译]Vulkan教程(02)概况

    [译]Vulkan教程(02)概况 这是我翻译(https://vulkan-tutorial.com)上的Vulkan教程的第2篇. This chapter will start off with ...

  8. [译]Vulkan教程(33)多重采样

    [译]Vulkan教程(33)多重采样 Multisampling 多重采样 Introduction 入门 Our program can now load multiple levels of d ...

  9. [译]Vulkan教程(19)渲染和呈现

    [译]Vulkan教程(19)渲染和呈现 Rendering and presentation 渲染和呈现 Setup 设置 This is the chapter where everything ...

随机推荐

  1. django----csrf跨站请求伪造 auth组件 settings源码 importlib模块

    目录 importlib模块 csrf跨站请求伪造 form表单发送 ajax发送 csrf装饰器 auth模块 如何创建超级用户(root) 创建用户 校验用户名和密码是否正确 保存用户登录状态 判 ...

  2. NodeJS4-9静态资源服务器实战_发到npm上

    登录->publish一下 ->上npm官网查看 -> 安装全局 //登录 npm login //推上去npm npm publish //全局安装 npm i -g 你的文件名

  3. django路由匹配层

    目录 orm表关系如何建立 一对多 多对多 一对一 django请求生命周期流程图 路由层 路由的简单配置 Django路由匹配规律 分组 无名分组 有名分组 反向解析 路由分发 名称空间 伪静态 虚 ...

  4. HTML语法简要总结

    HTML基本语法 认识网页 网页主要由文字.图像和超链接等元素构成.当然,除了这些元素,网页中还可以包含音频.视频以及Flash等. 常见浏览器内核介绍 浏览器是网页运行的平台,常用的浏览器有IE.火 ...

  5. asp.net core 3.0 MVC JSON 全局配置

    asp.net core 3.0 MVC JSON 全局配置 System.Text.Json(default) startup配置代码如下: using System.Text.Encodings. ...

  6. 3. abp依赖注入的分析.md

    abp依赖注入的原理剖析 请先移步参考 [Abp vNext 源码分析] - 3. 依赖注入与拦截器 本文此篇文章的补充和完善. abp的依赖注入最后是通过IConventionalRegister接 ...

  7. 【机器学习实战】计算两个矩阵的成对距离(pair-wise distances)

    矩阵中每一行是一个样本,计算两个矩阵样本之间的距离,即成对距离(pair-wise distances),可以采用 sklearn 或 scipy 中的函数,方便计算. sklearn: sklear ...

  8. LICEcap 动画屏幕录制软件

    下载地址    https://licecap.en.softonic.com/ LICEcap捕捉屏幕的区域并保存为gif动画或lcf格式 效果请看下面的链接 https://www.cnblogs ...

  9. python--推倒式(列表、字典、集合)

    python的各种推导式(列表推导式.字典推导式.集合推导式) 推导式comprehensions(又称解析式),是Python的一种独有特性.推导式是可以从一个数据序列构建另一个新的数据序列的结构体 ...

  10. C语言搬书学习第一记 —— 认识一个简单程序的细节

    #include<stdio.h> /*告诉编译器把stdio.h 中的内容包含在当前程序中,stdio.h是C编译器软件包的标准部分,它提供键盘输入和 屏幕输入的支持studio.h文件 ...