欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > 【Vulkan入门】03-创建Device

【Vulkan入门】03-创建Device

2025/4/26 23:36:48 来源:https://blog.csdn.net/patronwa/article/details/144233195  浏览:    关键词:【Vulkan入门】03-创建Device

目录

  • 先叨叨
  • git信息
  • 关键代码
    • VulkanEnv::CreateDevice()
  • 编译并运行程序
  • 题外话

先叨叨

在上篇已经选择了一个合适的PhysicalDevice。
本篇要为这个PhysicalDevice创将一个Device。Device可以理解为APP与PhysicalDevice之间的代理。
所有APP与PhysicalDevice之间交互的资源都通过Device进行管理。当然APP与PhysicalDevice通信用的Queue也是挂在Device进行管理的。Vulkan的接口设计,也暗含了在创建Device时同时创建Device下的Queue.

git信息

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

关键代码

VulkanEnv::CreateDevice()

  1. 本方法的作用是为选中的PhysicalDeivce创建的Device对象。
  2. 由VkDeviceCreateInfo可知,创建Device时需要同时提供创建Queue的信息,因此需要填充VkDeviceQueueCreateInfo。
  3. 填充VkDeviceQueueCreateInfo,需要提供QueueFamily的index,因此本方法最开始的循环是为了查询出支持图形功能的QueueFamily的index。
  4. 创建完Device后,对应的Queue也被创建了,因此方法最后用**vkGetDeviceQueue()**接口获取已创建的Queue。
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#VkDeviceQueueCreateInfofloat 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#VkDeviceCreateInfoVkDeviceCreateInfo 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#vkCreateDeviceif (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#vkGetDeviceQueuevkGetDeviceQueue(m_device, m_graphicsQueueFamilyIndex, 0, &m_graphicsQueue);
}

编译并运行程序

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

题外话

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词