Vulkan:物理设备与队列族

大家好,接下来将为大家介绍Vulkan 物理设备与队列族。

创建完了instance后,loader知道了你有多少个物理设备(显卡)是可用的,但是application是不知道的。application可以通过Vulkan的API来获得可用的物理设备列表。

物理设备与实例相关,如上图所示。

一、从Vulkan获取对象列表:

获取对象列表在Vulkan中是经常性的操作,获取各种各样对象的API都是相似的。这些API函数都会使用参数返回对象的数量及指向这些对象的指针。使用一个指向整形的指针作为参数传入到API函数中,然后这个指针所指向的内存就会被API改写成对象的数量。

VkResult vkEnumerateObjects( ObjectPointer*, int* count, void**)
  • 将上述函数原型的第二个参数设置为指向整型的指针,第三个参数的值为NULL
  • 执行这个函数结束后,我们就能通过第二个参数获取对象的数量。
  • Application 知道了对象的数量,然后会分配这些对象的空间来存储对象。
  • 再次调用上述函数,将第三个参数换为指向在第三步中分配的空间。
vkEnumeratePhysicalDevices 函数

函数vkEnumeratePhysicalDevices会返回在系统中物理设备的列表。一个物理设备可能是电脑中的显卡,SoC中的GPU core等等。如果有多个可用的物理设备(GPU),application需要决定用哪个物理设备。
查看物理设备的示例代码如下:

// Get the number of devices (GPUs) available.
int gpu_count = 0;
VkResult res = vkEnumeratePhysicalDevices(info.inst, &gpu_count, NULL);
// Allocate space and get the list of devices.
info.gpus.resize(gpu_count);
res = vkEnumeratePhysicalDevices(info.inst, &gpu_count, info.gpus.data());

注意:变量info.gpusVkPhysicalDevice类型的vector,VkPhysicalDevice是一个handle。这个函数所作的事情就是获取所有在系统中可用的物理设备的handle。

info 结构体:

你应该注意到了上述代码中的变量info。经常会使用全局的info变量来跟踪Vulkan的信息和application状态。

init_instance(info, "vulkansamples_enumerate");

init_instance() 创建了instance 并将handle存储在了info中. 然后vkEnumeratePhysicalDevices() 使用了 info.inst
现在,你有了设备(GPUs)的列表, 接下来需要做的是挑选一个GPU,然后创建Vulkan的逻辑设备,这样,你就可以开始使用GPU进行工作了。

二、使用详解

1、添加函数pickPhysicalDevice并在initVulkan函数中调用:

VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;

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

void pickPhysicalDevice() {

}

最终我们选择的图形显卡存储在类成员VkPhysicalDevice句柄中。当VkInstance销毁时,这个对象将会被隐式销毁,所以我们并不需要在cleanup函数中做任何操作。

2、获取图形卡列表的方式:

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

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

如果Vulkan支持的设备数为0,那么没有任何意义进行下一步,我们选择抛出异常。

否则我们分配数组存储所有VkPhysicalDevice的句柄。

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

3、现在我们需要对它们进行评估,检查它们是否适合我们要执行的操作,因为并不是所有的显卡功能一致。为此我们添加一个新的函数:

bool isDeviceSuitable(VkPhysicalDevice device) {
    return true;
}

检查是否有任何物理设备符合我们的功能需求。

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!");
}

4、评估合适的设备

评估合适的设备我们可以通过遍历一些细节来完成。基本的设备属性像name, type以及Vulkan版本都可以通过vkGetPhysicalDeviceProperties来遍历得到。

VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(device, &deviceProperties);

可以使用vkGetPhysicalDeviceFeatures查询对纹理压缩,64位浮点数和多视图渲染(VR非常有用)等可选功能的支持:

VkPhysicalDeviceFeatures deviceFeatures;
vkGetPhysicalDeviceFeatures(device, &deviceFeatures);

例如,我们假设我们的应用程序仅适用于支持geometry shaders的专用显卡。那么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;
}

为了避免纯粹的单一的判断一个设备是否合适,尤其是当你发现多个设备都合适的条件下,你也可以给每一个设备做权值,选择最高的一个。这样,可以通过给予更高权值获取定制化的图形设备,但如果没有一个可用的设备,可以回滚到集成图形设备。你可以按照如下方式实现:

#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 > 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 advantage
    if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {
        score += 1000;
    }

    // Maximum possible size of textures affects graphics quality
    score += deviceProperties.limits.maxImageDimension2D;

    // Application can't function without geometry shaders
    if (!deviceFeatures.geometryShader) {
        return 0;
    }

    return score;
}

三、队列族

之前已经简要的介绍过,几乎所有的Vulkan操作,从绘图到上传纹理,都需要将命令提交到队列中。有不同类型的队列来源于不同的队列簇,每个队列簇只允许部分commands。例如,可以有一个队列簇,只允许处理计算commands或者只允许内存传输commands:

我们需要检测设备中支持的队列簇,其中哪一个队列簇支持我们想要的commands。为此我们添加一个新的函数findQueueFamilies来查找我们需要的队列簇。现在我们只会寻找一个支持图形commands队列簇,但是我们可以在稍后的小节中扩展更多的内容。

此函数返回满足某个属性的队列簇索引。定义结构体,其中索引-1表示"未找到":

struct QueueFamilyIndices {
    int graphicsFamily = -1;

    bool isComplete() {
        return graphicsFamily >= 0;
    }
};

现在我们实现findQueueFamilies函数:

QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
    QueueFamilyIndices indices;

    ...

    return indices;
}

获取队列簇的列表函数为vkGetPhysicalDeviceQueueFamilyProperties:

uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);

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

有关队列簇,结构体VkQueueFamilyProperties包含了具体信息,包括支持的操作类型和基于当前队列簇可以创建的有效队列数。我们至少需要找到一个支持VK_QUEUE_GRAPHICS_BIT的队列簇。

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

    if (indices.isComplete()) {
        break;
    }

    i++;
}

现在我们有了比较理想的队列簇查询功能,我们可以在isDeviceSuitable函数中使用,确保物理设备可以处理我们需要的命令:

bool isDeviceSuitable(VkPhysicalDevice device) {
    QueueFamilyIndices indices = findQueueFamilies(device);

    return indices.isComplete();
}
发布了78 篇原创文章 · 获赞 85 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/u010281924/article/details/105360310