dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 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 | |
dyen | 5bd6d4f | 2016-03-31 15:25:45 | [diff] [blame] | 7 | #include <unordered_set> |
Peng Huang | bcf31dc | 2019-03-12 00:35:40 | [diff] [blame] | 8 | #include <utility> |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Peng Huang | 386c31c | 2019-11-13 20:38:23 | [diff] [blame] | 11 | #include "gpu/config/vulkan_info.h" |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 12 | #include "gpu/vulkan/vulkan_command_pool.h" |
Eric Karl | f9ec4a50 | 2019-04-11 18:29:52 | [diff] [blame] | 13 | #include "gpu/vulkan/vulkan_fence_helper.h" |
Chris Blume | b9adeda8 | 2018-06-08 00:23:53 | [diff] [blame] | 14 | #include "gpu/vulkan/vulkan_function_pointers.h" |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 15 | |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 16 | namespace gpu { |
| 17 | |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 18 | VulkanDeviceQueue::VulkanDeviceQueue(VkInstance vk_instance, |
| 19 | bool enforce_protected_memory) |
| 20 | : vk_instance_(vk_instance), |
| 21 | enforce_protected_memory_(enforce_protected_memory) {} |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 22 | |
| 23 | VulkanDeviceQueue::~VulkanDeviceQueue() { |
| 24 | DCHECK_EQ(static_cast<VkPhysicalDevice>(VK_NULL_HANDLE), vk_physical_device_); |
| 25 | DCHECK_EQ(static_cast<VkDevice>(VK_NULL_HANDLE), vk_device_); |
| 26 | DCHECK_EQ(static_cast<VkQueue>(VK_NULL_HANDLE), vk_queue_); |
| 27 | } |
| 28 | |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 29 | bool VulkanDeviceQueue::Initialize( |
| 30 | uint32_t options, |
Peng Huang | c2d69cc | 2019-10-07 23:02:25 | [diff] [blame] | 31 | const VulkanInfo& info, |
Michael Spang | 4e46bc2 | 2018-06-21 23:07:51 | [diff] [blame] | 32 | const std::vector<const char*>& required_extensions, |
Peng Huang | 576415de | 2020-03-02 20:04:32 | [diff] [blame] | 33 | const std::vector<const char*>& optional_extensions, |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 34 | bool allow_protected_memory, |
Sergey Ulanov | e82b100 | 2019-07-22 22:23:39 | [diff] [blame] | 35 | const GetPresentationSupportCallback& get_presentation_support) { |
Peng Huang | bcf31dc | 2019-03-12 00:35:40 | [diff] [blame] | 36 | DCHECK_EQ(static_cast<VkPhysicalDevice>(VK_NULL_HANDLE), vk_physical_device_); |
| 37 | DCHECK_EQ(static_cast<VkDevice>(VK_NULL_HANDLE), owned_vk_device_); |
| 38 | DCHECK_EQ(static_cast<VkDevice>(VK_NULL_HANDLE), vk_device_); |
| 39 | DCHECK_EQ(static_cast<VkQueue>(VK_NULL_HANDLE), vk_queue_); |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 40 | DCHECK(!enforce_protected_memory_ || allow_protected_memory); |
Peng Huang | bcf31dc | 2019-03-12 00:35:40 | [diff] [blame] | 41 | |
suyambu.rm | f2fbf53 | 2017-12-04 08:38:15 | [diff] [blame] | 42 | if (VK_NULL_HANDLE == vk_instance_) |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 43 | return false; |
| 44 | |
dyen | 5bd6d4f | 2016-03-31 15:25:45 | [diff] [blame] | 45 | VkResult result = VK_SUCCESS; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 46 | |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 47 | VkQueueFlags queue_flags = 0; |
| 48 | if (options & DeviceQueueOption::GRAPHICS_QUEUE_FLAG) |
| 49 | queue_flags |= VK_QUEUE_GRAPHICS_BIT; |
| 50 | |
| 51 | int device_index = -1; |
| 52 | int queue_index = -1; |
Peng Huang | c2d69cc | 2019-10-07 23:02:25 | [diff] [blame] | 53 | for (size_t i = 0; i < info.physical_devices.size(); ++i) { |
| 54 | const auto& device_info = info.physical_devices[i]; |
| 55 | const VkPhysicalDevice& device = device_info.device; |
| 56 | for (size_t n = 0; n < device_info.queue_families.size(); ++n) { |
| 57 | if ((device_info.queue_families[n].queueFlags & queue_flags) != |
| 58 | queue_flags) |
| 59 | continue; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 60 | |
Peng Huang | c2d69cc | 2019-10-07 23:02:25 | [diff] [blame] | 61 | if (options & DeviceQueueOption::PRESENTATION_SUPPORT_QUEUE_FLAG && |
| 62 | !get_presentation_support.Run(device, device_info.queue_families, |
| 63 | n)) { |
| 64 | continue; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 65 | } |
| 66 | |
Peng Huang | c2d69cc | 2019-10-07 23:02:25 | [diff] [blame] | 67 | queue_index = static_cast<int>(n); |
| 68 | break; |
| 69 | } |
| 70 | if (-1 != queue_index) { |
| 71 | device_index = static_cast<int>(i); |
| 72 | break; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Peng Huang | 50f53ae8 | 2020-03-20 19:52:13 | [diff] [blame^] | 76 | if (queue_index == -1) { |
| 77 | DLOG(ERROR) << "Cannot find capable device queue."; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 78 | return false; |
Peng Huang | 50f53ae8 | 2020-03-20 19:52:13 | [diff] [blame^] | 79 | } |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 80 | |
Peng Huang | c2d69cc | 2019-10-07 23:02:25 | [diff] [blame] | 81 | const auto& physical_device_info = info.physical_devices[device_index]; |
| 82 | vk_physical_device_ = physical_device_info.device; |
| 83 | vk_physical_device_properties_ = physical_device_info.properties; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 84 | vk_queue_index_ = queue_index; |
| 85 | |
| 86 | float queue_priority = 0.0f; |
| 87 | VkDeviceQueueCreateInfo queue_create_info = {}; |
| 88 | queue_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 89 | queue_create_info.queueFamilyIndex = queue_index; |
| 90 | queue_create_info.queueCount = 1; |
| 91 | queue_create_info.pQueuePriorities = &queue_priority; |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 92 | queue_create_info.flags = |
| 93 | allow_protected_memory ? VK_DEVICE_QUEUE_CREATE_PROTECTED_BIT : 0; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 94 | |
Michael Spang | 4e46bc2 | 2018-06-21 23:07:51 | [diff] [blame] | 95 | std::vector<const char*> enabled_extensions; |
Peng Huang | 576415de | 2020-03-02 20:04:32 | [diff] [blame] | 96 | for (const char* extension : required_extensions) { |
| 97 | const auto it = |
| 98 | std::find_if(physical_device_info.extensions.begin(), |
| 99 | physical_device_info.extensions.end(), |
| 100 | [extension](const VkExtensionProperties& p) { |
| 101 | return std::strcmp(extension, p.extensionName) == 0; |
| 102 | }); |
| 103 | if (it == physical_device_info.extensions.end()) { |
| 104 | // On Fuchsia, some device extensions are provided by layers. |
| 105 | // TODO(penghuang): checking extensions against layer device extensions |
| 106 | // too. |
| 107 | #if !defined(OS_FUCHSIA) |
| 108 | DLOG(ERROR) << "Required Vulkan extension " << extension |
| 109 | << " is not supported."; |
| 110 | return false; |
| 111 | #endif |
| 112 | } |
| 113 | enabled_extensions.push_back(extension); |
| 114 | } |
| 115 | |
| 116 | for (const char* extension : optional_extensions) { |
| 117 | const auto it = |
| 118 | std::find_if(physical_device_info.extensions.begin(), |
| 119 | physical_device_info.extensions.end(), |
| 120 | [extension](const VkExtensionProperties& p) { |
| 121 | return std::strcmp(extension, p.extensionName) == 0; |
| 122 | }); |
| 123 | if (it == physical_device_info.extensions.end()) { |
| 124 | DLOG(ERROR) << "Optional Vulkan extension " << extension |
| 125 | << " is not supported."; |
| 126 | } else { |
| 127 | enabled_extensions.push_back(extension); |
| 128 | } |
| 129 | } |
Michael Spang | 4e46bc2 | 2018-06-21 23:07:51 | [diff] [blame] | 130 | |
Peng Huang | c2d69cc | 2019-10-07 23:02:25 | [diff] [blame] | 131 | uint32_t device_api_version = std::min( |
| 132 | info.used_api_version, vk_physical_device_properties_.apiVersion); |
| 133 | |
| 134 | // Disable all physical device features by default. |
| 135 | enabled_device_features_2_ = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 136 | |
Sergey Ulanov | 6ff66f2 | 2019-08-14 00:38:39 | [diff] [blame] | 137 | // Android and Fuchsia need YCbCr sampler support. |
| 138 | #if defined(OS_ANDROID) || defined(OS_FUCHSIA) |
Peng Huang | c2d69cc | 2019-10-07 23:02:25 | [diff] [blame] | 139 | if (!physical_device_info.feature_sampler_ycbcr_conversion) { |
Sergey Ulanov | 6ff66f2 | 2019-08-14 00:38:39 | [diff] [blame] | 140 | LOG(ERROR) << "samplerYcbcrConversion is not supported."; |
Vikas Soni | b08bd9b | 2019-06-13 05:49:47 | [diff] [blame] | 141 | return false; |
| 142 | } |
Peng Huang | c2d69cc | 2019-10-07 23:02:25 | [diff] [blame] | 143 | sampler_ycbcr_conversion_features_ = { |
| 144 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES}; |
| 145 | sampler_ycbcr_conversion_features_.samplerYcbcrConversion = VK_TRUE; |
Vikas Soni | b08bd9b | 2019-06-13 05:49:47 | [diff] [blame] | 146 | |
Sergey Ulanov | 6ff66f2 | 2019-08-14 00:38:39 | [diff] [blame] | 147 | // Add VkPhysicalDeviceSamplerYcbcrConversionFeatures struct to pNext chain |
| 148 | // of VkPhysicalDeviceFeatures2 to enable YCbCr sampler support. |
| 149 | sampler_ycbcr_conversion_features_.pNext = enabled_device_features_2_.pNext; |
| 150 | enabled_device_features_2_.pNext = &sampler_ycbcr_conversion_features_; |
| 151 | #endif // defined(OS_ANDROID) || defined(OS_FUCHSIA) |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 152 | |
| 153 | if (allow_protected_memory) { |
Peng Huang | c2d69cc | 2019-10-07 23:02:25 | [diff] [blame] | 154 | if (!physical_device_info.feature_protected_memory) { |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 155 | DLOG(ERROR) << "Protected memory is not supported"; |
| 156 | return false; |
| 157 | } |
Peng Huang | c2d69cc | 2019-10-07 23:02:25 | [diff] [blame] | 158 | protected_memory_features_ = { |
| 159 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES}; |
| 160 | protected_memory_features_.protectedMemory = VK_TRUE; |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 161 | |
Sergey Ulanov | 6ff66f2 | 2019-08-14 00:38:39 | [diff] [blame] | 162 | // Add VkPhysicalDeviceProtectedMemoryFeatures struct to pNext chain |
| 163 | // of VkPhysicalDeviceFeatures2 to enable YCbCr sampler support. |
| 164 | protected_memory_features_.pNext = enabled_device_features_2_.pNext; |
| 165 | enabled_device_features_2_.pNext = &protected_memory_features_; |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 166 | } |
Sergey Ulanov | 6ff66f2 | 2019-08-14 00:38:39 | [diff] [blame] | 167 | |
Peng Huang | c2d69cc | 2019-10-07 23:02:25 | [diff] [blame] | 168 | VkDeviceCreateInfo device_create_info = { |
| 169 | VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO}; |
Vikas Soni | b08bd9b | 2019-06-13 05:49:47 | [diff] [blame] | 170 | device_create_info.pNext = enabled_device_features_2_.pNext; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 171 | device_create_info.queueCreateInfoCount = 1; |
| 172 | device_create_info.pQueueCreateInfos = &queue_create_info; |
Michael Spang | 4e46bc2 | 2018-06-21 23:07:51 | [diff] [blame] | 173 | device_create_info.enabledExtensionCount = enabled_extensions.size(); |
| 174 | device_create_info.ppEnabledExtensionNames = enabled_extensions.data(); |
Vikas Soni | b08bd9b | 2019-06-13 05:49:47 | [diff] [blame] | 175 | device_create_info.pEnabledFeatures = &enabled_device_features_2_.features; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 176 | |
Chris Blume | f0fb42f | 2018-06-28 19:35:25 | [diff] [blame] | 177 | result = vkCreateDevice(vk_physical_device_, &device_create_info, nullptr, |
Peng Huang | bcf31dc | 2019-03-12 00:35:40 | [diff] [blame] | 178 | &owned_vk_device_); |
Peng Huang | 50f53ae8 | 2020-03-20 19:52:13 | [diff] [blame^] | 179 | if (VK_SUCCESS != result) { |
| 180 | DLOG(ERROR) << "vkCreateDevice failed. result:" << result; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 181 | return false; |
Peng Huang | 50f53ae8 | 2020-03-20 19:52:13 | [diff] [blame^] | 182 | } |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 183 | |
Michael Spang | 4e46bc2 | 2018-06-21 23:07:51 | [diff] [blame] | 184 | enabled_extensions_ = gfx::ExtensionSet(std::begin(enabled_extensions), |
| 185 | std::end(enabled_extensions)); |
| 186 | |
Michael Spang | 5086fb9 | 2019-06-11 21:13:59 | [diff] [blame] | 187 | if (!gpu::GetVulkanFunctionPointers()->BindDeviceFunctionPointers( |
Sergey Ulanov | e82b100 | 2019-07-22 22:23:39 | [diff] [blame] | 188 | owned_vk_device_, device_api_version, enabled_extensions_)) { |
Michael Spang | 5086fb9 | 2019-06-11 21:13:59 | [diff] [blame] | 189 | vkDestroyDevice(owned_vk_device_, nullptr); |
| 190 | owned_vk_device_ = VK_NULL_HANDLE; |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | vk_device_ = owned_vk_device_; |
Michael Spang | 4e46bc2 | 2018-06-21 23:07:51 | [diff] [blame] | 195 | |
Emircan Uysaler | ac88d87 | 2019-09-25 03:13:59 | [diff] [blame] | 196 | if (allow_protected_memory) { |
| 197 | VkDeviceQueueInfo2 queue_info2 = {}; |
| 198 | queue_info2.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2; |
| 199 | queue_info2.flags = VK_DEVICE_QUEUE_CREATE_PROTECTED_BIT; |
| 200 | queue_info2.queueFamilyIndex = queue_index; |
| 201 | queue_info2.queueIndex = 0; |
| 202 | vkGetDeviceQueue2(vk_device_, &queue_info2, &vk_queue_); |
| 203 | } else { |
| 204 | vkGetDeviceQueue(vk_device_, queue_index, 0, &vk_queue_); |
| 205 | } |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 206 | |
Eric Karl | f9ec4a50 | 2019-04-11 18:29:52 | [diff] [blame] | 207 | cleanup_helper_ = std::make_unique<VulkanFenceHelper>(this); |
| 208 | |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 209 | allow_protected_memory_ = allow_protected_memory; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
Shouqun Liu | 80cd8a8 | 2019-06-17 18:15:19 | [diff] [blame] | 213 | bool VulkanDeviceQueue::InitializeForWebView( |
Peng Huang | bcf31dc | 2019-03-12 00:35:40 | [diff] [blame] | 214 | VkPhysicalDevice vk_physical_device, |
| 215 | VkDevice vk_device, |
| 216 | VkQueue vk_queue, |
| 217 | uint32_t vk_queue_index, |
| 218 | gfx::ExtensionSet enabled_extensions) { |
| 219 | DCHECK_EQ(static_cast<VkPhysicalDevice>(VK_NULL_HANDLE), vk_physical_device_); |
| 220 | DCHECK_EQ(static_cast<VkDevice>(VK_NULL_HANDLE), owned_vk_device_); |
| 221 | DCHECK_EQ(static_cast<VkDevice>(VK_NULL_HANDLE), vk_device_); |
| 222 | DCHECK_EQ(static_cast<VkQueue>(VK_NULL_HANDLE), vk_queue_); |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 223 | |
Peng Huang | bcf31dc | 2019-03-12 00:35:40 | [diff] [blame] | 224 | vk_physical_device_ = vk_physical_device; |
| 225 | vk_device_ = vk_device; |
| 226 | vk_queue_ = vk_queue; |
| 227 | vk_queue_index_ = vk_queue_index; |
| 228 | enabled_extensions_ = std::move(enabled_extensions); |
Eric Karl | f9ec4a50 | 2019-04-11 18:29:52 | [diff] [blame] | 229 | |
| 230 | cleanup_helper_ = std::make_unique<VulkanFenceHelper>(this); |
Peng Huang | bcf31dc | 2019-03-12 00:35:40 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | |
| 234 | void VulkanDeviceQueue::Destroy() { |
Michael Spang | 5086fb9 | 2019-06-11 21:13:59 | [diff] [blame] | 235 | if (cleanup_helper_) { |
| 236 | cleanup_helper_->Destroy(); |
| 237 | cleanup_helper_.reset(); |
| 238 | } |
Eric Karl | f9ec4a50 | 2019-04-11 18:29:52 | [diff] [blame] | 239 | |
Peng Huang | bcf31dc | 2019-03-12 00:35:40 | [diff] [blame] | 240 | if (VK_NULL_HANDLE != owned_vk_device_) { |
| 241 | vkDestroyDevice(owned_vk_device_, nullptr); |
| 242 | owned_vk_device_ = VK_NULL_HANDLE; |
| 243 | } |
| 244 | vk_device_ = VK_NULL_HANDLE; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 245 | vk_queue_ = VK_NULL_HANDLE; |
| 246 | vk_queue_index_ = 0; |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 247 | vk_physical_device_ = VK_NULL_HANDLE; |
| 248 | } |
| 249 | |
mostynb | 6682b1c4 | 2016-04-19 10:17:30 | [diff] [blame] | 250 | std::unique_ptr<VulkanCommandPool> VulkanDeviceQueue::CreateCommandPool() { |
| 251 | std::unique_ptr<VulkanCommandPool> command_pool(new VulkanCommandPool(this)); |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 252 | if (!command_pool->Initialize(enforce_protected_memory_)) |
dyen | 8a145fb7 | 2016-03-31 00:37:51 | [diff] [blame] | 253 | return nullptr; |
| 254 | |
| 255 | return command_pool; |
| 256 | } |
| 257 | |
Peng Huang | 50f53ae8 | 2020-03-20 19:52:13 | [diff] [blame^] | 258 | } // namespace gpu |