[Vulkan教程]绘制一个三角形/设置/物理设备和队列簇(Physical devices and queue families)
选择一个物理设备
通过VkInstance初始化Vulkan库之后,我们需要在系统中查找和选择一个支持我们所需特性的显卡。我们可以选择任意数量的显卡同时使用,但在本教程中,我们只用一张。
我们创建一个函数pickPhysicalDevice,并在initVulkan函数中调用。
void initVulkan() {createInstance();setupDebugMessenger();pickPhysicalDevice();}void pickPhysicalDevice() {}123456789
创建一个新的VkPhysicalDevice类型的类成员来保存我们选择的显卡句柄。这个对象会在VkInstance销毁时隐式销毁,所以我们不需要在cleanup函数中做什么。
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;1
列出显卡与列出扩展非常相似,首先查询数量:
uint32_t deviceCount = 0;vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);12
如果支持Vulkan的设备数量为0,那就没有必要继续运行了:
if (deviceCount == 0) {throw std::runtime_error("failed to find GPUs with Vulkan support!");}123
如果有可用设备,我们先创建一个数组来保存所有VkPhysicalDevice:
std::vector<VkPhysicalDevice> devices(deviceCount);vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());12
并不是所有显卡都是一样的,所以我们现在需要评估它们中的每一个并检查它们是否适合我们想要执行的操作。为此,我们需要引入一个函数:
bool isDeviceSuitable(VkPhysicalDevice device) {return true;}123
我们找到一个符合要求的误了设备:
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!");}12345678910
下一节,我们会在isDeviceSuitable函数中检查第一个要求。之后还会用更多Vulkan功能,到时候我们再做更多检查。
基本的设备能力检查
我们先通过vkGetPhysicalDeviceProperties函数获取名称、类型和支持的Vulkan版本等细节信息:
VkPhysicalDeviceProperties deviceProperties;vkGetPhysicalDeviceProperties(device, &deviceProperties);12
通过vkGetPhysicalDeviceFeatures函数可以获取设备对纹理压缩、64位浮点数和多视口渲染(对VR有用)等可选功能的支持:
VkPhysicalDeviceFeatures deviceFeatures;vkGetPhysicalDeviceFeatures(device, &deviceFeatures);12
之后我们还会讨论关于设备内存和队列簇(下一章)等更多设备细节。
例如,假设我们的程序仅可用于支持几何着色器的专用显卡,那么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;}123456789
不仅仅是检查设备能力,我们可以给每个设备打个分,然后选择分最高的那个。当然,如果能力都达不到,就没必要评分了:
#include <map>...void pickPhysicalDevice() {...// Use an ordered map to automatically sort candidates by increasing scorestd::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 allif (candidates.rbegin()->first > 0) {physicalDevice = candidates.rbegin()->second;} else {throw std::runtime_error("failed to find a suitable GPU!");}}int rateDeviceSuitability(VkPhysicalDevice device) {...int score = 0;// Discrete GPUs have a significant performance advantageif (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {score += 1000;}// Maximum possible size of textures affects graphics qualityscore += deviceProperties.limits.maxImageDimension2D;// Application can't function without geometry shadersif (!deviceFeatures.geometryShader) {return 0;}return score;}12345678910111213141516171819202122232425262728293031323334353637383940414243
这里只是让你了解如何设计选择设备过程,你可以不像本教程一样实现。你也可以显示一个列表让用户来选择使用哪个设备。
我们才开始,支持Vulkan的显卡就够我们用了,所以:
bool isDeviceSuitable(VkPhysicalDevice device) {return true;}123
下一节,我们会讨论我们真正需要检查的特性。
队列簇
我们之前提到的很多内容,包括绘制和上传纹理等,都需要将命令提交到一个队列。有不同的队列簇和不同类型的队列,每个队列簇只允许一个命令子集。例如,有的队列簇只允许处理计算命令,有的队列只允许与内存传输相关的命令。
我们需要检查设备支持哪些队列簇,哪一个支持我们要用的命令。为此,我们添加一个新的函数findiQueueFamilies来查找我们需要的所有队列簇。
现在我们只要需要支持图形命令的队列,所以函数大概这样:
uint32_t findQueueFamilies(VkPhysicalDevice device) {// Logic to find graphics queue family}123
只后还要用到其它队列,这里准备一个结构体保存队列簇信息:
struct QueueFamilyIndices {uint32_t graphicsFamily;};QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {QueueFamilyIndices indices;// Logic to find queue family indices to populate struct withreturn indices;}123456789
如果队列簇不可用怎么办?我们可以在findQueueFamilies函数中抛出异常,但这里不是判断设备是否可用的最佳位置。例如,我们可能更倾向于使用拥有专用传输队列簇的设备,但它不是必要的。因此,我们需要用某种方式指示我们找到了特定队列簇。
译者认为不需要用std::optional,不需要整这些不是特别必要的东西,这里只是简单翻译一下吧
uint32_t表示索引,不太好用它表示队列簇是否存在。C++17中引入了一种数据结构可以用来区分值是否存在:
#include <optional>...std::optional<uint32_t> graphicsFamily;std::cout << std::boolalpha << graphicsFamily.has_value() << std::endl; // falsegraphicsFamily = 0;std::cout << std::boolalpha << graphicsFamily.has_value() << std::endl; // true1234567891011
std::optional是一个包装器,在赋值之前它不包含任何值。任何时候,你都可以用has_value()函数来判断它是否有值。我们可以把逻辑改成这样:
#include <optional>...struct QueueFamilyIndices {std::optional<uint32_t> graphicsFamily;};QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {QueueFamilyIndices indices;// Assign index to queue families that could be foundreturn indices;}12345678910111213
我们现在可以实际实现findQueueFamilies:
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {QueueFamilyIndices indices;...return indices;}1234567
检索队列簇列表的方法也比较简单,使用函数vkGetPhysicalDeviceQueueFamilyProperties就行:
uint32_t queueFamilyCount = 0;vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());12345
VkQueueFamilyProperties结构体保存了队列簇的一些详细信息,包括支持的操作类型和可以基于该簇创建的队列的数量。我们需要至少找到一个支持VK_QUQUE_GRAPHICS_BIT的队列簇。
int i = 0;for (const auto& queueFamily : queueFamilies) {if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {indices.graphicsFamily = i;}i++;}12345678
现在,我们有了一个奇特的队列簇查找函数,我们可以在isDeviceSuitable函数中使用它,确保设备可以处理我们要使用的命令:
bool isDeviceSuitable(VkPhysicalDevice device) {QueueFamilyIndices indices = findQueueFamilies(device);return indices.graphicsFamily.has_value();}12345
为了更方便一点,我们给结构体本身添加一个通用检查函数:
struct QueueFamilyIndices {std::optional<uint32_t> graphicsFamily;bool isComplete() {return graphicsFamily.has_value();}};...bool isDeviceSuitable(VkPhysicalDevice device) {QueueFamilyIndices indices = findQueueFamilies(device);return indices.isComplete();}123456789101112131415
我们可以通过上面的函数在findQueueFamilies中提前返回:
for (const auto& queueFamily : queueFamilies) {...if (indices.isComplete()) {break;}i++;}123456789
好的,这就是我们现在需要找到合适的物理设备的全部内容!下一步是创建一个逻辑设备来与之交互。
