【Vulkan入门】03-创建Device

目录

先叨叨

上篇已经选择了一个合适的PhysicalDevice。

本篇要为这个PhysicalDevice创将一个Device。Device可以理解为APP与PhysicalDevice之间的代理。

所有APP与PhysicalDevice之间交互的资源都通过Device进行管理。当然APP与PhysicalDevice通信用的Queue也是挂在Device进行管理的。Vulkan的接口设计,也暗含了在创建Device时同时创建Device下的Queue.

git信息

关键代码

VulkanEnv::CreateDevice()

  1. 本方法的作用是为选中的PhysicalDeivce创建的Device对象。
  2. 由VkDeviceCreateInfo可知,创建Device时需要同时提供创建Queue的信息,因此需要填充VkDeviceQueueCreateInfo。
  3. 填充VkDeviceQueueCreateInfo,需要提供QueueFamily的index,因此本方法最开始的循环是为了查询出支持图形功能的QueueFamily的index。
  4. 创建完Device后,对应的Queue也被创建了,因此方法最后用**vkGetDeviceQueue()**接口获取已创建的Queue。
cpp 复制代码
void VulkanEnv::CreateDevice()
{
    std::vector<VkQueueFamilyProperties> queueFamilies = GetQueueFamiliesOfPhysicalDevice(m_selectedPhysicalDevice);
    for (uint32_t i = 0; i < queueFamilies.size(); ++i)
    {
        if (queueFamilies[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
        {
            m_graphicsQueueFamilyIndex = i;
            break;
        }
    }

    //https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#VkDeviceQueueCreateInfo
    float queuePriority = 1.0f;
    VkDeviceQueueCreateInfo queueCreateInfo {};
    queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
    queueCreateInfo.pNext = nullptr;
    queueCreateInfo.queueFamilyIndex = m_graphicsQueueFamilyIndex;
    queueCreateInfo.queueCount = 1;
    queueCreateInfo.pQueuePriorities = &queuePriority;


    //https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#VkDeviceCreateInfo
    VkDeviceCreateInfo createInfo{};
    createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
    createInfo.pNext = nullptr;
    createInfo.queueCreateInfoCount = 1;
    createInfo.pQueueCreateInfos = &queueCreateInfo;
    createInfo.enabledExtensionCount = 0;
    createInfo.ppEnabledExtensionNames = nullptr;
    createInfo.pEnabledFeatures = nullptr;

    
    //https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#vkCreateDevice
    if (VK_SUCCESS != vkCreateDevice(m_selectedPhysicalDevice, &createInfo, nullptr, &m_device))
    {
        throw std::runtime_error("To create device is failed");
    }

    //https://registry.khronos.org/vulkan/specs/latest/html/vkspec.html#vkGetDeviceQueue
    vkGetDeviceQueue(m_device, m_graphicsQueueFamilyIndex, 0, &m_graphicsQueue);
}

编译并运行程序

运行不报错就是最好的消息

题外话

实际上每个PhysicalDevice可以创建多个Device。每个Device又可以创建多个Queue。但我还是的初学者,为了简单起见我只创建一个Device和一个Queue

相关推荐
千里马-horse5 天前
Building a Simple Engine -- Advanced Topics--Planar reflections
rendering·vulkan·平面反射
千里马-horse8 天前
Building a Simple Engine -- Advanced Topics -- Mipmaps & LOD
rendering·vulkan·lod·mipmaps
千里马-horse10 天前
Building a Simple Engine -- Advanced Topics -- Forward+ rendering
rendering·vulkan·forward+
千里马-horse11 天前
Building a Simple Engine -- Mobile Development -- Conclusion
pipeline·shader·rendering·vulkan
千里马-horse11 天前
Building a Simple Engine -- Mobile Development -- Platform considerations
android·ios·rendering·vulkan
千里马-horse12 天前
Building a Simple Engine -- Mobile Development -- Performance optimizations
shader·内存优化·rendering·纹理优化·vulkan·压缩格式
千里马-horse14 天前
Building a Simple Engine -- Tooling -- Crash minidumps
rendering·vulkan·崩溃处理
千里马-horse15 天前
Building a Simple Engine -- Tooling -- Introduction
pipeline·shader·rendering·vulkan
千里马-horse1 个月前
Ray Tracing -- Ray query shadows
c++·rendering·vulkan
千里马-horse1 个月前
Multithreading with Vulkan
shader·rendering·vulkan·vertex·multithreaded