OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "gpu/vulkan/vulkan_device_queue.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "gpu/vulkan/vulkan_command_pool.h" |
| 10 #include "gpu/vulkan/vulkan_implementation.h" |
| 11 #include "gpu/vulkan/vulkan_platform.h" |
| 12 |
| 13 #if defined(VK_USE_PLATFORM_XLIB_KHR) |
| 14 #include "ui/gfx/x/x11_types.h" |
| 15 #endif // defined(VK_USE_PLATFORM_XLIB_KHR) |
| 16 |
| 17 namespace gpu { |
| 18 |
| 19 VulkanDeviceQueue::VulkanDeviceQueue() {} |
| 20 |
| 21 VulkanDeviceQueue::~VulkanDeviceQueue() { |
| 22 DCHECK_EQ(static_cast<VkPhysicalDevice>(VK_NULL_HANDLE), vk_physical_device_); |
| 23 DCHECK_EQ(static_cast<VkDevice>(VK_NULL_HANDLE), vk_device_); |
| 24 DCHECK_EQ(static_cast<VkQueue>(VK_NULL_HANDLE), vk_queue_); |
| 25 } |
| 26 |
| 27 bool VulkanDeviceQueue::Initialize(uint32_t options) { |
| 28 VkInstance vk_instance = gpu::GetVulkanInstance(); |
| 29 if (VK_NULL_HANDLE == vk_instance) |
| 30 return false; |
| 31 |
| 32 VkResult status = VK_SUCCESS; |
| 33 |
| 34 uint32_t device_count = 0; |
| 35 status = vkEnumeratePhysicalDevices(vk_instance, &device_count, nullptr); |
| 36 if (VK_SUCCESS != status || device_count == 0) |
| 37 return false; |
| 38 |
| 39 std::vector<VkPhysicalDevice> devices(device_count); |
| 40 status = |
| 41 vkEnumeratePhysicalDevices(vk_instance, &device_count, devices.data()); |
| 42 if (VK_SUCCESS != status) { |
| 43 DLOG(ERROR) << "vkEnumeratePhysicalDevices() failed: " << status; |
| 44 return false; |
| 45 } |
| 46 |
| 47 #if defined(VK_USE_PLATFORM_XLIB_KHR) |
| 48 Display* xdisplay = gfx::GetXDisplay(); |
| 49 VisualID visual_id = |
| 50 XVisualIDFromVisual(DefaultVisual(xdisplay, DefaultScreen(xdisplay))); |
| 51 #endif // defined(VK_USE_PLATFORM_XLIB_KHR) |
| 52 |
| 53 VkQueueFlags queue_flags = 0; |
| 54 if (options & DeviceQueueOption::GRAPHICS_QUEUE_FLAG) |
| 55 queue_flags |= VK_QUEUE_GRAPHICS_BIT; |
| 56 |
| 57 int device_index = -1; |
| 58 int queue_index = -1; |
| 59 for (size_t i = 0; i < devices.size(); ++i) { |
| 60 const VkPhysicalDevice& device = devices[i]; |
| 61 uint32_t queue_count = 0; |
| 62 vkGetPhysicalDeviceQueueFamilyProperties(device, &queue_count, nullptr); |
| 63 if (queue_count) { |
| 64 std::vector<VkQueueFamilyProperties> queue_properties(queue_count); |
| 65 vkGetPhysicalDeviceQueueFamilyProperties(device, &queue_count, |
| 66 queue_properties.data()); |
| 67 for (size_t n = 0; n < queue_properties.size(); ++n) { |
| 68 if ((queue_properties[n].queueFlags & queue_flags) != queue_flags) |
| 69 continue; |
| 70 |
| 71 #if defined(VK_USE_PLATFORM_XLIB_KHR) |
| 72 if (options & DeviceQueueOption::PRESENTATION_SUPPORT_QUEUE_FLAG && |
| 73 !vkGetPhysicalDeviceXlibPresentationSupportKHR(device, n, xdisplay, |
| 74 visual_id)) { |
| 75 continue; |
| 76 } |
| 77 #else |
| 78 #error Non-Supported Vulkan implementation. |
| 79 #endif |
| 80 |
| 81 queue_index = static_cast<int>(n); |
| 82 break; |
| 83 } |
| 84 |
| 85 if (-1 != queue_index) { |
| 86 device_index = static_cast<int>(i); |
| 87 break; |
| 88 } |
| 89 } |
| 90 } |
| 91 |
| 92 if (queue_index == -1) |
| 93 return false; |
| 94 |
| 95 vk_physical_device_ = devices[device_index]; |
| 96 vk_queue_index_ = queue_index; |
| 97 |
| 98 float queue_priority = 0.0f; |
| 99 VkDeviceQueueCreateInfo queue_create_info = {}; |
| 100 queue_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 101 queue_create_info.queueFamilyIndex = queue_index; |
| 102 queue_create_info.queueCount = 1; |
| 103 queue_create_info.pQueuePriorities = &queue_priority; |
| 104 |
| 105 const char* device_extensions[] = {VK_KHR_SWAPCHAIN_EXTENSION_NAME}; |
| 106 |
| 107 VkDeviceCreateInfo device_create_info = {}; |
| 108 device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; |
| 109 device_create_info.queueCreateInfoCount = 1; |
| 110 device_create_info.pQueueCreateInfos = &queue_create_info; |
| 111 device_create_info.enabledExtensionCount = arraysize(device_extensions); |
| 112 device_create_info.ppEnabledExtensionNames = device_extensions; |
| 113 |
| 114 status = vkCreateDevice(vk_physical_device_, &device_create_info, nullptr, |
| 115 &vk_device_); |
| 116 if (VK_SUCCESS != status) |
| 117 return false; |
| 118 |
| 119 vkGetDeviceQueue(vk_device_, queue_index, 0, &vk_queue_); |
| 120 |
| 121 return true; |
| 122 } |
| 123 |
| 124 void VulkanDeviceQueue::Destroy() { |
| 125 if (VK_NULL_HANDLE != vk_device_) { |
| 126 vkDestroyDevice(vk_device_, nullptr); |
| 127 vk_device_ = VK_NULL_HANDLE; |
| 128 } |
| 129 |
| 130 vk_queue_ = VK_NULL_HANDLE; |
| 131 vk_queue_index_ = 0; |
| 132 |
| 133 vk_physical_device_ = VK_NULL_HANDLE; |
| 134 } |
| 135 |
| 136 scoped_ptr<VulkanCommandPool> VulkanDeviceQueue::CreateCommandPool() { |
| 137 scoped_ptr<VulkanCommandPool> command_pool(new VulkanCommandPool(this)); |
| 138 if (!command_pool->Initialize()) |
| 139 return nullptr; |
| 140 |
| 141 return command_pool; |
| 142 } |
| 143 |
| 144 } // namespace gpu |
OLD | NEW |