【Vulkan入门】01-列举物理设备

发布于:2024-12-06 ⋅ 阅读:(128) ⋅ 点赞:(0)

先叨叨

上一篇已经创建了VkInstance,本篇我们问问VkInstance,在当前平台上有多少个支持Vulkan的物理设备。

git信息

  • repository: https://gitee.com/J8_series/easy-car-ui
  • branch: master
  • tag: 01-EnumPhysicalDevices
  • url: https://gitee.com/J8_series/easy-car-ui/tree/01-EnumPhysicalDevices

主要逻辑

VulkanEnv

新增了两个方法EnumeratePhysicalDevices()PrintPhysicalDevices().

EnumeratePhysicalDevices()

本方作用是通过**vkEnumeratePhysicalDevices()**接口获取当前平台所有支持Vulkan的物理设备句柄

void VulkanEnv::EnumeratePhysicalDevices()
{
    //https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#vkEnumeratePhysicalDevices
    uint32_t physicalDeviceCount{0};
    vkEnumeratePhysicalDevices(m_vkInstance, &physicalDeviceCount, nullptr);

    if (0 == physicalDeviceCount)
    {
        throw std::runtime_error("There is no device support Vulkan!");
    }

    m_vkPhysicalDevices.resize(physicalDeviceCount);
    vkEnumeratePhysicalDevices(m_vkInstance, &physicalDeviceCount, m_vkPhysicalDevices.data());
}

在Vulkan中所有枚举方法的调用策略都是类似的,同一个方法需要调用两次。第一次调用获取对象数量,第二次调用对象列表。

PrintPhysicalDevices()

本方法的作用是获取所有物理设备的属性并打印。

void VulkanEnv::PrintPhysicalDevices()
{
    int index {0};
    for (auto physicalDevice : m_vkPhysicalDevices)
    {
        //https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#vkGetPhysicalDeviceProperties
        VkPhysicalDeviceProperties physicalDeviceProperties;
        vkGetPhysicalDeviceProperties(physicalDevice, &physicalDeviceProperties);
        std::cout << "Physical Device " << index << " is " << physicalDeviceProperties.deviceName <<
                    " Type is " << static_cast<int>(physicalDeviceProperties.deviceType) <<
                    " Support API Version is " << VK_VERSION_MAJOR(physicalDeviceProperties.apiVersion) << "." << 
                                                  VK_VERSION_MINOR(physicalDeviceProperties.apiVersion) << "." << 
                                                  VK_VERSION_PATCH(physicalDeviceProperties.apiVersion) << std::endl;
        ++index;
    }
}

编译并运行程序

我的实验电脑好像是8年前买的,型号是TinkPad E480. 系统是Ubuntu22.04.
共枚举出三个物理设备。

  1. AMD的独显,型号是Radeon 500系列
  2. llvmpipe,这是CPU模拟出来的虚拟显卡
  3. Intel的核显,UHD Graphics 620
    在这里插入图片描述
    关于显卡类型可以查看VkPhysicalDeviceType中的定义。
    在这里插入图片描述

网站公告

今日签到

点亮在社区的每一天
去签到