Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // RendererVk.cpp: |
| 7 | // Implements the class methods for RendererVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
| 11 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 12 | // Placing this first seems to solve an intellisense bug. |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 13 | #include "libANGLE/renderer/vulkan/vk_utils.h" |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 14 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 15 | #include <EGL/eglext.h> |
| 16 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 17 | #include "common/debug.h" |
Yuly Novikov | 5fe7c5b | 2019-01-17 12:16:34 -0500 | [diff] [blame] | 18 | #include "common/platform.h" |
Jamie Madill | a66779f | 2017-01-06 10:43:44 -0500 | [diff] [blame] | 19 | #include "common/system_utils.h" |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 20 | #include "libANGLE/Context.h" |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 21 | #include "libANGLE/Display.h" |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 22 | #include "libANGLE/renderer/driver_utils.h" |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 23 | #include "libANGLE/renderer/vulkan/CommandGraph.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 24 | #include "libANGLE/renderer/vulkan/CompilerVk.h" |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 25 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 26 | #include "libANGLE/renderer/vulkan/DisplayVk.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 27 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 28 | #include "libANGLE/renderer/vulkan/GlslangWrapper.h" |
Jamie Madill | ffa4cbb | 2018-01-23 13:04:07 -0500 | [diff] [blame] | 29 | #include "libANGLE/renderer/vulkan/ProgramVk.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 30 | #include "libANGLE/renderer/vulkan/VertexArrayVk.h" |
Luc Ferron | e4741fd | 2018-01-25 13:25:27 -0500 | [diff] [blame] | 31 | #include "libANGLE/renderer/vulkan/vk_caps_utils.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 32 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 33 | #include "platform/Platform.h" |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 34 | #include "third_party/trace_event/trace_event.h" |
| 35 | |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 36 | // Consts |
| 37 | namespace |
| 38 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 39 | const uint32_t kMockVendorID = 0xba5eba11; |
| 40 | const uint32_t kMockDeviceID = 0xf005ba11; |
| 41 | constexpr char kMockDeviceName[] = "Vulkan Mock Device"; |
| 42 | constexpr size_t kInFlightCommandsLimit = 100u; |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 43 | constexpr VkFormatFeatureFlags kInvalidFormatFeatureFlags = static_cast<VkFormatFeatureFlags>(-1); |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 44 | } // anonymous namespace |
| 45 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 46 | namespace rx |
| 47 | { |
| 48 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 49 | namespace |
| 50 | { |
Luc Ferron | daedf4d | 2018-03-16 09:28:53 -0400 | [diff] [blame] | 51 | // We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and |
| 52 | // one for the vertex shader. |
| 53 | constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 54 | // Update the pipeline cache every this many swaps (if 60fps, this means every 10 minutes) |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 55 | constexpr uint32_t kPipelineCacheVkUpdatePeriod = 10 * 60 * 60; |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 56 | // Wait a maximum of 10s. If that times out, we declare it a failure. |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 57 | constexpr uint64_t kMaxFenceWaitTimeNs = 10'000'000'000llu; |
Ian Elliott | 356d26c | 2019-02-11 11:27:01 -0700 | [diff] [blame] | 58 | // Per the Vulkan specification, as long as Vulkan 1.1+ is returned by vkEnumerateInstanceVersion, |
| 59 | // ANGLE must indicate the highest version of Vulkan functionality that it uses. The Vulkan |
| 60 | // validation layers will issue messages for any core functionality that requires a higher version. |
| 61 | // This value must be increased whenever ANGLE starts using functionality from a newer core |
| 62 | // version of Vulkan. |
| 63 | constexpr uint32_t kPreferredVulkanAPIVersion = VK_API_VERSION_1_1; |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 64 | |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 65 | bool ShouldEnableMockICD(const egl::AttributeMap &attribs) |
| 66 | { |
| 67 | #if !defined(ANGLE_PLATFORM_ANDROID) |
| 68 | // Mock ICD does not currently run on Android |
| 69 | return (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, |
| 70 | EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) == |
| 71 | EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE); |
| 72 | #else |
| 73 | return false; |
| 74 | #endif // !defined(ANGLE_PLATFORM_ANDROID) |
| 75 | } |
| 76 | |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 77 | bool StrLess(const char *a, const char *b) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 78 | { |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 79 | return strcmp(a, b) < 0; |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 82 | bool ExtensionFound(const char *needle, const RendererVk::ExtensionNameList &haystack) |
| 83 | { |
| 84 | // NOTE: The list must be sorted. |
| 85 | return std::binary_search(haystack.begin(), haystack.end(), needle, StrLess); |
Tobin Ehlis | 3a181e3 | 2018-08-29 15:17:05 -0600 | [diff] [blame] | 86 | } |
| 87 | |
Yuly Novikov | dbbdf56 | 2019-02-27 19:48:16 -0500 | [diff] [blame] | 88 | VkResult VerifyExtensionsPresent(const RendererVk::ExtensionNameList &haystack, |
| 89 | const RendererVk::ExtensionNameList &needles) |
| 90 | { |
| 91 | // NOTE: The lists must be sorted. |
| 92 | if (std::includes(haystack.begin(), haystack.end(), needles.begin(), needles.end(), StrLess)) |
| 93 | { |
| 94 | return VK_SUCCESS; |
| 95 | } |
| 96 | for (const char *needle : needles) |
| 97 | { |
| 98 | if (!ExtensionFound(needle, haystack)) |
| 99 | { |
| 100 | ERR() << "Extension not supported: " << needle; |
| 101 | } |
| 102 | } |
| 103 | return VK_ERROR_EXTENSION_NOT_PRESENT; |
| 104 | } |
| 105 | |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 106 | // Array of Validation error/warning messages that will be ignored, should include bugID |
Jamie Madill | 00f43c9 | 2019-02-09 11:41:12 -0500 | [diff] [blame] | 107 | constexpr const char *kSkippedMessages[] = { |
| 108 | // http://anglebug.com/2866 |
| 109 | "UNASSIGNED-CoreValidation-Shader-OutputNotConsumed", |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 110 | // http://anglebug.com/2796 |
Jamie Madill | 00f43c9 | 2019-02-09 11:41:12 -0500 | [diff] [blame] | 111 | "UNASSIGNED-CoreValidation-Shader-PointSizeMissing", |
| 112 | }; |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 113 | |
| 114 | // Suppress validation errors that are known |
| 115 | // return "true" if given code/prefix/message is known, else return "false" |
| 116 | bool IsIgnoredDebugMessage(const char *message) |
| 117 | { |
Michael Spang | 2583980 | 2019-01-30 18:02:51 -0500 | [diff] [blame] | 118 | if (!message) |
| 119 | { |
| 120 | return false; |
| 121 | } |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 122 | for (const char *msg : kSkippedMessages) |
| 123 | { |
| 124 | if (strstr(message, msg) != nullptr) |
| 125 | { |
| 126 | return true; |
| 127 | } |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | const char *GetVkObjectTypeName(VkObjectType type) |
| 133 | { |
| 134 | switch (type) |
| 135 | { |
| 136 | case VK_OBJECT_TYPE_UNKNOWN: |
| 137 | return "Unknown"; |
| 138 | case VK_OBJECT_TYPE_INSTANCE: |
| 139 | return "Instance"; |
| 140 | case VK_OBJECT_TYPE_PHYSICAL_DEVICE: |
| 141 | return "Physical Device"; |
| 142 | case VK_OBJECT_TYPE_DEVICE: |
| 143 | return "Device"; |
| 144 | case VK_OBJECT_TYPE_QUEUE: |
| 145 | return "Queue"; |
| 146 | case VK_OBJECT_TYPE_SEMAPHORE: |
| 147 | return "Semaphore"; |
| 148 | case VK_OBJECT_TYPE_COMMAND_BUFFER: |
| 149 | return "Command Buffer"; |
| 150 | case VK_OBJECT_TYPE_FENCE: |
| 151 | return "Fence"; |
| 152 | case VK_OBJECT_TYPE_DEVICE_MEMORY: |
| 153 | return "Device Memory"; |
| 154 | case VK_OBJECT_TYPE_BUFFER: |
| 155 | return "Buffer"; |
| 156 | case VK_OBJECT_TYPE_IMAGE: |
| 157 | return "Image"; |
| 158 | case VK_OBJECT_TYPE_EVENT: |
| 159 | return "Event"; |
| 160 | case VK_OBJECT_TYPE_QUERY_POOL: |
| 161 | return "Query Pool"; |
| 162 | case VK_OBJECT_TYPE_BUFFER_VIEW: |
| 163 | return "Buffer View"; |
| 164 | case VK_OBJECT_TYPE_IMAGE_VIEW: |
| 165 | return "Image View"; |
| 166 | case VK_OBJECT_TYPE_SHADER_MODULE: |
| 167 | return "Shader Module"; |
| 168 | case VK_OBJECT_TYPE_PIPELINE_CACHE: |
| 169 | return "Pipeline Cache"; |
| 170 | case VK_OBJECT_TYPE_PIPELINE_LAYOUT: |
| 171 | return "Pipeline Layout"; |
| 172 | case VK_OBJECT_TYPE_RENDER_PASS: |
| 173 | return "Render Pass"; |
| 174 | case VK_OBJECT_TYPE_PIPELINE: |
| 175 | return "Pipeline"; |
| 176 | case VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT: |
| 177 | return "Descriptor Set Layout"; |
| 178 | case VK_OBJECT_TYPE_SAMPLER: |
| 179 | return "Sampler"; |
| 180 | case VK_OBJECT_TYPE_DESCRIPTOR_POOL: |
| 181 | return "Descriptor Pool"; |
| 182 | case VK_OBJECT_TYPE_DESCRIPTOR_SET: |
| 183 | return "Descriptor Set"; |
| 184 | case VK_OBJECT_TYPE_FRAMEBUFFER: |
| 185 | return "Framebuffer"; |
| 186 | case VK_OBJECT_TYPE_COMMAND_POOL: |
| 187 | return "Command Pool"; |
| 188 | case VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION: |
| 189 | return "Sampler YCbCr Conversion"; |
| 190 | case VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE: |
| 191 | return "Descriptor Update Template"; |
| 192 | case VK_OBJECT_TYPE_SURFACE_KHR: |
| 193 | return "Surface"; |
| 194 | case VK_OBJECT_TYPE_SWAPCHAIN_KHR: |
| 195 | return "Swapchain"; |
| 196 | case VK_OBJECT_TYPE_DISPLAY_KHR: |
| 197 | return "Display"; |
| 198 | case VK_OBJECT_TYPE_DISPLAY_MODE_KHR: |
| 199 | return "Display Mode"; |
| 200 | case VK_OBJECT_TYPE_DEBUG_REPORT_CALLBACK_EXT: |
| 201 | return "Debug Report Callback"; |
| 202 | case VK_OBJECT_TYPE_OBJECT_TABLE_NVX: |
| 203 | return "Object Table"; |
| 204 | case VK_OBJECT_TYPE_INDIRECT_COMMANDS_LAYOUT_NVX: |
| 205 | return "Indirect Commands Layout"; |
| 206 | case VK_OBJECT_TYPE_DEBUG_UTILS_MESSENGER_EXT: |
| 207 | return "Debug Utils Messenger"; |
| 208 | case VK_OBJECT_TYPE_VALIDATION_CACHE_EXT: |
| 209 | return "Validation Cache"; |
Shahbaz Youssefi | 3805122 | 2019-02-13 21:21:13 +0000 | [diff] [blame] | 210 | case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_NVX: |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 211 | return "Acceleration Structure"; |
| 212 | default: |
| 213 | return "<Unrecognized>"; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | VKAPI_ATTR VkBool32 VKAPI_CALL |
| 218 | DebugUtilsMessenger(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, |
| 219 | VkDebugUtilsMessageTypeFlagsEXT messageTypes, |
| 220 | const VkDebugUtilsMessengerCallbackDataEXT *callbackData, |
| 221 | void *userData) |
| 222 | { |
Shahbaz Youssefi | 0c01e36 | 2019-02-13 21:27:23 +0000 | [diff] [blame] | 223 | constexpr VkDebugUtilsMessageSeverityFlagsEXT kSeveritiesToLog = |
| 224 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | |
| 225 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; |
| 226 | |
| 227 | // Check if we even care about this message. |
| 228 | if ((messageSeverity & kSeveritiesToLog) == 0) |
| 229 | { |
| 230 | return VK_FALSE; |
| 231 | } |
| 232 | |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 233 | // See if it's an issue we are aware of and don't want to be spammed about. |
| 234 | if (IsIgnoredDebugMessage(callbackData->pMessageIdName)) |
| 235 | { |
| 236 | return VK_FALSE; |
| 237 | } |
| 238 | |
| 239 | std::ostringstream log; |
Michael Spang | 2583980 | 2019-01-30 18:02:51 -0500 | [diff] [blame] | 240 | if (callbackData->pMessageIdName) |
| 241 | { |
| 242 | log << "[ " << callbackData->pMessageIdName << " ] "; |
| 243 | } |
| 244 | log << callbackData->pMessage << std::endl; |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 245 | |
| 246 | // Aesthetic value based on length of the function name, line number, etc. |
| 247 | constexpr size_t kStartIndent = 28; |
| 248 | |
| 249 | // Output the debug marker hierarchy under which this error has occured. |
| 250 | size_t indent = kStartIndent; |
| 251 | if (callbackData->queueLabelCount > 0) |
| 252 | { |
| 253 | log << std::string(indent++, ' ') << "<Queue Label Hierarchy:>" << std::endl; |
| 254 | for (uint32_t i = 0; i < callbackData->queueLabelCount; ++i) |
| 255 | { |
| 256 | log << std::string(indent++, ' ') << callbackData->pQueueLabels[i].pLabelName |
| 257 | << std::endl; |
| 258 | } |
| 259 | } |
| 260 | if (callbackData->cmdBufLabelCount > 0) |
| 261 | { |
| 262 | log << std::string(indent++, ' ') << "<Command Buffer Label Hierarchy:>" << std::endl; |
| 263 | for (uint32_t i = 0; i < callbackData->cmdBufLabelCount; ++i) |
| 264 | { |
| 265 | log << std::string(indent++, ' ') << callbackData->pCmdBufLabels[i].pLabelName |
| 266 | << std::endl; |
| 267 | } |
| 268 | } |
| 269 | // Output the objects involved in this error message. |
| 270 | if (callbackData->objectCount > 0) |
| 271 | { |
| 272 | for (uint32_t i = 0; i < callbackData->objectCount; ++i) |
| 273 | { |
| 274 | const char *objectName = callbackData->pObjects[i].pObjectName; |
| 275 | const char *objectType = GetVkObjectTypeName(callbackData->pObjects[i].objectType); |
| 276 | uint64_t objectHandle = callbackData->pObjects[i].objectHandle; |
| 277 | log << std::string(indent, ' ') << "Object: "; |
| 278 | if (objectHandle == 0) |
| 279 | { |
| 280 | log << "VK_NULL_HANDLE"; |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | log << "0x" << std::hex << objectHandle << std::dec; |
| 285 | } |
| 286 | log << " (type = " << objectType << "(" << callbackData->pObjects[i].objectType << "))"; |
| 287 | if (objectName) |
| 288 | { |
| 289 | log << " [" << objectName << "]"; |
| 290 | } |
| 291 | log << std::endl; |
| 292 | } |
| 293 | } |
| 294 | |
Jamie Madill | 54ed8f0 | 2019-02-11 12:32:04 -0500 | [diff] [blame] | 295 | bool isError = (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) != 0; |
| 296 | std::string msg = log.str(); |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 297 | |
| 298 | if (isError) |
| 299 | { |
Jamie Madill | 54ed8f0 | 2019-02-11 12:32:04 -0500 | [diff] [blame] | 300 | ERR() << msg; |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 301 | } |
| 302 | else |
| 303 | { |
Jamie Madill | 54ed8f0 | 2019-02-11 12:32:04 -0500 | [diff] [blame] | 304 | WARN() << msg; |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | return VK_FALSE; |
| 308 | } |
| 309 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 310 | VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags, |
| 311 | VkDebugReportObjectTypeEXT objectType, |
| 312 | uint64_t object, |
| 313 | size_t location, |
| 314 | int32_t messageCode, |
| 315 | const char *layerPrefix, |
| 316 | const char *message, |
| 317 | void *userData) |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 318 | { |
Tobin Ehlis | 3a181e3 | 2018-08-29 15:17:05 -0600 | [diff] [blame] | 319 | if (IsIgnoredDebugMessage(message)) |
| 320 | { |
| 321 | return VK_FALSE; |
| 322 | } |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 323 | if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0) |
| 324 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 325 | ERR() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 326 | #if !defined(NDEBUG) |
| 327 | // Abort the call in Debug builds. |
| 328 | return VK_TRUE; |
| 329 | #endif |
| 330 | } |
| 331 | else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0) |
| 332 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 333 | WARN() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 334 | } |
| 335 | else |
| 336 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 337 | // Uncomment this if you want Vulkan spam. |
| 338 | // WARN() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | return VK_FALSE; |
| 342 | } |
| 343 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 344 | // If we're loading the validation layers, we could be running from any random directory. |
| 345 | // Change to the executable directory so we can find the layers, then change back to the |
| 346 | // previous directory to be safe we don't disrupt the application. |
| 347 | class ScopedVkLoaderEnvironment : angle::NonCopyable |
| 348 | { |
| 349 | public: |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 350 | ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD) |
| 351 | : mEnableValidationLayers(enableValidationLayers), |
| 352 | mEnableMockICD(enableMockICD), |
| 353 | mChangedCWD(false), |
| 354 | mChangedICDPath(false) |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 355 | { |
| 356 | // Changing CWD and setting environment variables makes no sense on Android, |
| 357 | // since this code is a part of Java application there. |
| 358 | // Android Vulkan loader doesn't need this either. |
Michael Spang | 229fc83 | 2019-01-21 18:09:15 -0500 | [diff] [blame] | 359 | #if !defined(ANGLE_PLATFORM_ANDROID) && !defined(ANGLE_PLATFORM_FUCHSIA) |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 360 | if (enableMockICD) |
| 361 | { |
| 362 | // Override environment variable to use built Mock ICD |
| 363 | // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn |
| 364 | mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv); |
| 365 | mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON); |
| 366 | if (!mChangedICDPath) |
| 367 | { |
| 368 | ERR() << "Error setting Path for Mock/Null Driver."; |
| 369 | mEnableMockICD = false; |
| 370 | } |
| 371 | } |
Jamie Madill | 4684842 | 2018-08-09 10:46:06 -0400 | [diff] [blame] | 372 | if (mEnableValidationLayers || mEnableMockICD) |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 373 | { |
| 374 | const auto &cwd = angle::GetCWD(); |
| 375 | if (!cwd.valid()) |
| 376 | { |
| 377 | ERR() << "Error getting CWD for Vulkan layers init."; |
| 378 | mEnableValidationLayers = false; |
Jamie Madill | 4684842 | 2018-08-09 10:46:06 -0400 | [diff] [blame] | 379 | mEnableMockICD = false; |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 380 | } |
| 381 | else |
| 382 | { |
| 383 | mPreviousCWD = cwd.value(); |
Jamie Madill | bab0302 | 2019-01-16 14:12:28 -0500 | [diff] [blame] | 384 | std::string exeDir = angle::GetExecutableDirectory(); |
| 385 | mChangedCWD = angle::SetCWD(exeDir.c_str()); |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 386 | if (!mChangedCWD) |
| 387 | { |
| 388 | ERR() << "Error setting CWD for Vulkan layers init."; |
| 389 | mEnableValidationLayers = false; |
Jamie Madill | 4684842 | 2018-08-09 10:46:06 -0400 | [diff] [blame] | 390 | mEnableMockICD = false; |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | // Override environment variable to use the ANGLE layers. |
| 396 | if (mEnableValidationLayers) |
| 397 | { |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 398 | if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR)) |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 399 | { |
| 400 | ERR() << "Error setting environment for Vulkan layers init."; |
| 401 | mEnableValidationLayers = false; |
| 402 | } |
| 403 | } |
| 404 | #endif // !defined(ANGLE_PLATFORM_ANDROID) |
| 405 | } |
| 406 | |
| 407 | ~ScopedVkLoaderEnvironment() |
| 408 | { |
| 409 | if (mChangedCWD) |
| 410 | { |
| 411 | #if !defined(ANGLE_PLATFORM_ANDROID) |
| 412 | ASSERT(mPreviousCWD.valid()); |
| 413 | angle::SetCWD(mPreviousCWD.value().c_str()); |
| 414 | #endif // !defined(ANGLE_PLATFORM_ANDROID) |
| 415 | } |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 416 | if (mChangedICDPath) |
| 417 | { |
Omar El Sheikh | 80d4ef1 | 2018-07-13 17:08:19 -0600 | [diff] [blame] | 418 | if (mPreviousICDPath.value().empty()) |
| 419 | { |
| 420 | angle::UnsetEnvironmentVar(g_VkICDPathEnv); |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str()); |
| 425 | } |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 426 | } |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 429 | bool canEnableValidationLayers() const { return mEnableValidationLayers; } |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 430 | |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 431 | bool canEnableMockICD() const { return mEnableMockICD; } |
| 432 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 433 | private: |
| 434 | bool mEnableValidationLayers; |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 435 | bool mEnableMockICD; |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 436 | bool mChangedCWD; |
| 437 | Optional<std::string> mPreviousCWD; |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 438 | bool mChangedICDPath; |
| 439 | Optional<std::string> mPreviousICDPath; |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 440 | }; |
| 441 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 442 | void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices, |
| 443 | bool preferMockICD, |
| 444 | VkPhysicalDevice *physicalDeviceOut, |
| 445 | VkPhysicalDeviceProperties *physicalDevicePropertiesOut) |
| 446 | { |
| 447 | ASSERT(!physicalDevices.empty()); |
| 448 | if (preferMockICD) |
| 449 | { |
| 450 | for (const VkPhysicalDevice &physicalDevice : physicalDevices) |
| 451 | { |
| 452 | vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut); |
| 453 | if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) && |
| 454 | (kMockDeviceID == physicalDevicePropertiesOut->deviceID) && |
| 455 | (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0)) |
| 456 | { |
| 457 | *physicalDeviceOut = physicalDevice; |
| 458 | return; |
| 459 | } |
| 460 | } |
| 461 | WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default " |
| 462 | "physicalDevice instead."; |
| 463 | } |
| 464 | |
| 465 | // Fall back to first device. |
| 466 | *physicalDeviceOut = physicalDevices[0]; |
| 467 | vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut); |
| 468 | } |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 469 | |
| 470 | // Initially dumping the command graphs is disabled. |
| 471 | constexpr bool kEnableCommandGraphDiagnostics = false; |
Ian Elliott | bcb7890 | 2018-12-19 11:46:29 -0700 | [diff] [blame] | 472 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 473 | } // anonymous namespace |
| 474 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 475 | // CommandBatch implementation. |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 476 | RendererVk::CommandBatch::CommandBatch() = default; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 477 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 478 | RendererVk::CommandBatch::~CommandBatch() = default; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 479 | |
| 480 | RendererVk::CommandBatch::CommandBatch(CommandBatch &&other) |
Tobin Ehlis | 47ca1b2 | 2019-01-23 16:11:41 +0000 | [diff] [blame] | 481 | : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 482 | {} |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 483 | |
| 484 | RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other) |
| 485 | { |
| 486 | std::swap(commandPool, other.commandPool); |
| 487 | std::swap(fence, other.fence); |
| 488 | std::swap(serial, other.serial); |
| 489 | return *this; |
| 490 | } |
| 491 | |
Tobin Ehlis | 47ca1b2 | 2019-01-23 16:11:41 +0000 | [diff] [blame] | 492 | void RendererVk::CommandBatch::destroy(VkDevice device) |
Jamie Madill | bea35a6 | 2018-07-05 11:54:10 -0400 | [diff] [blame] | 493 | { |
Tobin Ehlis | 47ca1b2 | 2019-01-23 16:11:41 +0000 | [diff] [blame] | 494 | commandPool.destroy(device); |
Jamie Madill | bea35a6 | 2018-07-05 11:54:10 -0400 | [diff] [blame] | 495 | fence.destroy(device); |
| 496 | } |
| 497 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 498 | // RendererVk implementation. |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 499 | RendererVk::RendererVk() |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 500 | : mDisplay(nullptr), |
| 501 | mCapsInitialized(false), |
Ian Elliott | bcb7890 | 2018-12-19 11:46:29 -0700 | [diff] [blame] | 502 | mFeaturesInitialized(false), |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 503 | mInstance(VK_NULL_HANDLE), |
| 504 | mEnableValidationLayers(false), |
Jamie Madill | 0ea9621 | 2018-10-30 15:14:51 -0400 | [diff] [blame] | 505 | mEnableMockICD(false), |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 506 | mDebugUtilsMessenger(VK_NULL_HANDLE), |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 507 | mDebugReportCallback(VK_NULL_HANDLE), |
| 508 | mPhysicalDevice(VK_NULL_HANDLE), |
| 509 | mQueue(VK_NULL_HANDLE), |
| 510 | mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()), |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 511 | mMaxVertexAttribDivisor(1), |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 512 | mDevice(VK_NULL_HANDLE), |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 513 | mLastCompletedQueueSerial(mQueueSerialFactory.generate()), |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 514 | mCurrentQueueSerial(mQueueSerialFactory.generate()), |
Geoff Lang | 2fe5e1d | 2018-08-28 14:00:24 -0400 | [diff] [blame] | 515 | mDeviceLost(false), |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 516 | mPipelineCacheVkUpdateTimeout(kPipelineCacheVkUpdatePeriod), |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 517 | mCommandGraph(kEnableCommandGraphDiagnostics), |
| 518 | mGpuEventsEnabled(false), |
| 519 | mGpuClockSync{std::numeric_limits<double>::max(), std::numeric_limits<double>::max()}, |
| 520 | mGpuEventTimestampOrigin(0) |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 521 | { |
| 522 | VkFormatProperties invalid = {0, 0, kInvalidFormatFeatureFlags}; |
| 523 | mFormatProperties.fill(invalid); |
| 524 | } |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 525 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 526 | RendererVk::~RendererVk() {} |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 527 | |
| 528 | void RendererVk::onDestroy(vk::Context *context) |
| 529 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 530 | if (!mInFlightCommands.empty() || !mGarbage.empty()) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 531 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 532 | // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem. |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 533 | (void)finish(context); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 534 | } |
| 535 | |
Shahbaz Youssefi | e321940 | 2018-12-08 16:54:14 +0100 | [diff] [blame] | 536 | mUtils.destroy(mDevice); |
Shahbaz Youssefi | 8f1b7a6 | 2018-11-14 16:02:54 -0500 | [diff] [blame] | 537 | |
Jamie Madill | c7918ce | 2018-06-13 13:25:31 -0400 | [diff] [blame] | 538 | mPipelineLayoutCache.destroy(mDevice); |
| 539 | mDescriptorSetLayoutCache.destroy(mDevice); |
| 540 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 541 | mRenderPassCache.destroy(mDevice); |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 542 | mPipelineCache.destroy(mDevice); |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 543 | mSubmitSemaphorePool.destroy(mDevice); |
Jamie Madill | d47044a | 2018-04-27 11:45:03 -0400 | [diff] [blame] | 544 | mShaderLibrary.destroy(mDevice); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 545 | mGpuEventQueryPool.destroy(mDevice); |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 546 | |
Jamie Madill | 06ca634 | 2018-07-12 15:56:53 -0400 | [diff] [blame] | 547 | GlslangWrapper::Release(); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 548 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 549 | if (mCommandPool.valid()) |
| 550 | { |
Tobin Ehlis | 47ca1b2 | 2019-01-23 16:11:41 +0000 | [diff] [blame] | 551 | mCommandPool.destroy(mDevice); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 552 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 553 | |
| 554 | if (mDevice) |
| 555 | { |
| 556 | vkDestroyDevice(mDevice, nullptr); |
| 557 | mDevice = VK_NULL_HANDLE; |
| 558 | } |
| 559 | |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 560 | if (mDebugUtilsMessenger) |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 561 | { |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 562 | ASSERT(mInstance && vkDestroyDebugUtilsMessengerEXT); |
| 563 | vkDestroyDebugUtilsMessengerEXT(mInstance, mDebugUtilsMessenger, nullptr); |
| 564 | |
| 565 | ASSERT(mDebugReportCallback == VK_NULL_HANDLE); |
| 566 | } |
| 567 | else if (mDebugReportCallback) |
| 568 | { |
| 569 | ASSERT(mInstance && vkDestroyDebugReportCallbackEXT); |
| 570 | vkDestroyDebugReportCallbackEXT(mInstance, mDebugReportCallback, nullptr); |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 571 | } |
| 572 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 573 | if (mInstance) |
| 574 | { |
| 575 | vkDestroyInstance(mInstance, nullptr); |
| 576 | mInstance = VK_NULL_HANDLE; |
| 577 | } |
| 578 | |
Omar El Sheikh | eb4b869 | 2018-07-17 10:55:40 -0600 | [diff] [blame] | 579 | mMemoryProperties.destroy(); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 580 | mPhysicalDevice = VK_NULL_HANDLE; |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 581 | } |
| 582 | |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 583 | void RendererVk::notifyDeviceLost() |
Geoff Lang | 2fe5e1d | 2018-08-28 14:00:24 -0400 | [diff] [blame] | 584 | { |
| 585 | mDeviceLost = true; |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 586 | |
| 587 | mCommandGraph.clear(); |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 588 | nextSerial(); |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 589 | freeAllInFlightResources(); |
| 590 | |
| 591 | mDisplay->notifyDeviceLost(); |
Geoff Lang | 2fe5e1d | 2018-08-28 14:00:24 -0400 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | bool RendererVk::isDeviceLost() const |
| 595 | { |
| 596 | return mDeviceLost; |
| 597 | } |
| 598 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 599 | angle::Result RendererVk::initialize(DisplayVk *displayVk, |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 600 | egl::Display *display, |
Michael Spang | 740db7f | 2019-02-06 09:40:13 -0500 | [diff] [blame] | 601 | const char *wsiExtension, |
| 602 | const char *wsiLayer) |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 603 | { |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 604 | mDisplay = display; |
| 605 | const egl::AttributeMap &attribs = mDisplay->getAttributeMap(); |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 606 | ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs), |
| 607 | ShouldEnableMockICD(attribs)); |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 608 | mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers(); |
Jamie Madill | 0ea9621 | 2018-10-30 15:14:51 -0400 | [diff] [blame] | 609 | mEnableMockICD = scopedEnvironment.canEnableMockICD(); |
Jamie Madill | a66779f | 2017-01-06 10:43:44 -0500 | [diff] [blame] | 610 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 611 | // Gather global layer properties. |
| 612 | uint32_t instanceLayerCount = 0; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 613 | ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr)); |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 614 | |
| 615 | std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount); |
| 616 | if (instanceLayerCount > 0) |
| 617 | { |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 618 | ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, |
| 619 | instanceLayerProps.data())); |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 620 | } |
| 621 | |
Michael Spang | 6c13c70 | 2019-02-06 15:59:44 -0500 | [diff] [blame] | 622 | VulkanLayerVector enabledInstanceLayerNames; |
| 623 | if (mEnableValidationLayers) |
| 624 | { |
| 625 | bool layersRequested = |
| 626 | (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE); |
| 627 | mEnableValidationLayers = GetAvailableValidationLayers(instanceLayerProps, layersRequested, |
| 628 | &enabledInstanceLayerNames); |
| 629 | } |
| 630 | |
| 631 | if (wsiLayer) |
| 632 | { |
| 633 | enabledInstanceLayerNames.push_back(wsiLayer); |
| 634 | } |
| 635 | |
| 636 | // Enumerate instance extensions that are provided by the vulkan |
| 637 | // implementation and implicit layers. |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 638 | uint32_t instanceExtensionCount = 0; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 639 | ANGLE_VK_TRY(displayVk, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 640 | vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr)); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 641 | |
| 642 | std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount); |
| 643 | if (instanceExtensionCount > 0) |
| 644 | { |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 645 | ANGLE_VK_TRY(displayVk, |
| 646 | vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, |
| 647 | instanceExtensionProps.data())); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 648 | } |
| 649 | |
Michael Spang | 6c13c70 | 2019-02-06 15:59:44 -0500 | [diff] [blame] | 650 | // Enumerate instance extensions that are provided by explicit layers. |
| 651 | for (const char *layerName : enabledInstanceLayerNames) |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 652 | { |
Michael Spang | 6c13c70 | 2019-02-06 15:59:44 -0500 | [diff] [blame] | 653 | uint32_t previousExtensionCount = instanceExtensionProps.size(); |
| 654 | uint32_t instanceLayerExtensionCount = 0; |
| 655 | ANGLE_VK_TRY(displayVk, vkEnumerateInstanceExtensionProperties( |
| 656 | layerName, &instanceLayerExtensionCount, nullptr)); |
| 657 | instanceExtensionProps.resize(previousExtensionCount + instanceLayerExtensionCount); |
| 658 | ANGLE_VK_TRY(displayVk, vkEnumerateInstanceExtensionProperties( |
| 659 | layerName, &instanceLayerExtensionCount, |
| 660 | instanceExtensionProps.data() + previousExtensionCount)); |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 661 | } |
| 662 | |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 663 | ExtensionNameList instanceExtensionNames; |
| 664 | if (!instanceExtensionProps.empty()) |
| 665 | { |
| 666 | for (const VkExtensionProperties &i : instanceExtensionProps) |
| 667 | { |
| 668 | instanceExtensionNames.push_back(i.extensionName); |
| 669 | } |
| 670 | std::sort(instanceExtensionNames.begin(), instanceExtensionNames.end(), StrLess); |
| 671 | } |
| 672 | |
| 673 | ExtensionNameList enabledInstanceExtensions; |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 674 | enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); |
Michael Spang | 740db7f | 2019-02-06 09:40:13 -0500 | [diff] [blame] | 675 | enabledInstanceExtensions.push_back(wsiExtension); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 676 | |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 677 | bool enableDebugUtils = |
| 678 | mEnableValidationLayers && |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 679 | ExtensionFound(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instanceExtensionNames); |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 680 | bool enableDebugReport = |
| 681 | mEnableValidationLayers && !enableDebugUtils && |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 682 | ExtensionFound(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instanceExtensionNames); |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 683 | |
| 684 | if (enableDebugUtils) |
| 685 | { |
| 686 | enabledInstanceExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); |
| 687 | } |
| 688 | else if (enableDebugReport) |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 689 | { |
| 690 | enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); |
| 691 | } |
| 692 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 693 | // Verify the required extensions are in the extension names set. Fail if not. |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 694 | std::sort(enabledInstanceExtensions.begin(), enabledInstanceExtensions.end(), StrLess); |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 695 | ANGLE_VK_TRY(displayVk, |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 696 | VerifyExtensionsPresent(instanceExtensionNames, enabledInstanceExtensions)); |
| 697 | |
| 698 | // Enable VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME if available. |
| 699 | if (ExtensionFound(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, |
| 700 | instanceExtensionNames)) |
| 701 | { |
| 702 | enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); |
| 703 | } |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 704 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 705 | VkApplicationInfo applicationInfo = {}; |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 706 | applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 707 | applicationInfo.pApplicationName = "ANGLE"; |
| 708 | applicationInfo.applicationVersion = 1; |
| 709 | applicationInfo.pEngineName = "ANGLE"; |
| 710 | applicationInfo.engineVersion = 1; |
Ian Elliott | 899c5d2 | 2018-12-21 13:12:50 -0700 | [diff] [blame] | 711 | |
| 712 | auto enumerateInstanceVersion = reinterpret_cast<PFN_vkEnumerateInstanceVersion>( |
| 713 | vkGetInstanceProcAddr(mInstance, "vkEnumerateInstanceVersion")); |
| 714 | if (!enumerateInstanceVersion) |
| 715 | { |
| 716 | applicationInfo.apiVersion = VK_API_VERSION_1_0; |
| 717 | } |
| 718 | else |
| 719 | { |
| 720 | uint32_t apiVersion = VK_API_VERSION_1_0; |
| 721 | ANGLE_VK_TRY(displayVk, enumerateInstanceVersion(&apiVersion)); |
| 722 | if ((VK_VERSION_MAJOR(apiVersion) > 1) || (VK_VERSION_MINOR(apiVersion) >= 1)) |
| 723 | { |
Ian Elliott | 356d26c | 2019-02-11 11:27:01 -0700 | [diff] [blame] | 724 | // This is the highest version of core Vulkan functionality that ANGLE uses. |
| 725 | applicationInfo.apiVersion = kPreferredVulkanAPIVersion; |
Ian Elliott | 899c5d2 | 2018-12-21 13:12:50 -0700 | [diff] [blame] | 726 | } |
| 727 | else |
| 728 | { |
Ian Elliott | 356d26c | 2019-02-11 11:27:01 -0700 | [diff] [blame] | 729 | // Since only 1.0 instance-level functionality is available, this must set to 1.0. |
Ian Elliott | 899c5d2 | 2018-12-21 13:12:50 -0700 | [diff] [blame] | 730 | applicationInfo.apiVersion = VK_API_VERSION_1_0; |
| 731 | } |
| 732 | } |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 733 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 734 | VkInstanceCreateInfo instanceInfo = {}; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 735 | instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; |
| 736 | instanceInfo.flags = 0; |
| 737 | instanceInfo.pApplicationInfo = &applicationInfo; |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 738 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 739 | // Enable requested layers and extensions. |
| 740 | instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size()); |
| 741 | instanceInfo.ppEnabledExtensionNames = |
| 742 | enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data(); |
Michael Spang | 6c13c70 | 2019-02-06 15:59:44 -0500 | [diff] [blame] | 743 | instanceInfo.enabledLayerCount = enabledInstanceLayerNames.size(); |
| 744 | instanceInfo.ppEnabledLayerNames = enabledInstanceLayerNames.data(); |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 745 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 746 | ANGLE_VK_TRY(displayVk, vkCreateInstance(&instanceInfo, nullptr, &mInstance)); |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 747 | |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 748 | if (enableDebugUtils) |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 749 | { |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 750 | // Try to use the newer EXT_debug_utils if it exists. |
| 751 | InitDebugUtilsEXTFunctions(mInstance); |
| 752 | |
| 753 | // Create the messenger callback. |
| 754 | VkDebugUtilsMessengerCreateInfoEXT messengerInfo = {}; |
| 755 | |
| 756 | messengerInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; |
Shahbaz Youssefi | 0c01e36 | 2019-02-13 21:27:23 +0000 | [diff] [blame] | 757 | messengerInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | |
| 758 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; |
| 759 | messengerInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | |
| 760 | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | |
| 761 | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 762 | messengerInfo.pfnUserCallback = &DebugUtilsMessenger; |
| 763 | messengerInfo.pUserData = this; |
| 764 | |
| 765 | ANGLE_VK_TRY(displayVk, vkCreateDebugUtilsMessengerEXT(mInstance, &messengerInfo, nullptr, |
| 766 | &mDebugUtilsMessenger)); |
| 767 | } |
| 768 | else if (enableDebugReport) |
| 769 | { |
| 770 | // Fallback to EXT_debug_report. |
| 771 | InitDebugReportEXTFunctions(mInstance); |
| 772 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 773 | VkDebugReportCallbackCreateInfoEXT debugReportInfo = {}; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 774 | |
| 775 | debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 776 | debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 777 | debugReportInfo.pfnCallback = &DebugReportCallback; |
| 778 | debugReportInfo.pUserData = this; |
| 779 | |
Shahbaz Youssefi | 5bca4fe | 2019-01-09 17:07:06 -0500 | [diff] [blame] | 780 | ANGLE_VK_TRY(displayVk, vkCreateDebugReportCallbackEXT(mInstance, &debugReportInfo, nullptr, |
| 781 | &mDebugReportCallback)); |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 782 | } |
| 783 | |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 784 | if (std::find(enabledInstanceExtensions.begin(), enabledInstanceExtensions.end(), |
| 785 | VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) != |
| 786 | enabledInstanceExtensions.end()) |
| 787 | { |
| 788 | InitGetPhysicalDeviceProperties2KHRFunctions(mInstance); |
| 789 | ASSERT(vkGetPhysicalDeviceProperties2KHR); |
| 790 | } |
| 791 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 792 | uint32_t physicalDeviceCount = 0; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 793 | ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr)); |
| 794 | ANGLE_VK_CHECK(displayVk, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 795 | |
| 796 | // TODO(jmadill): Handle multiple physical devices. For now, use the first device. |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 797 | std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount); |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 798 | ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, |
| 799 | physicalDevices.data())); |
Jamie Madill | 0ea9621 | 2018-10-30 15:14:51 -0400 | [diff] [blame] | 800 | ChoosePhysicalDevice(physicalDevices, mEnableMockICD, &mPhysicalDevice, |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 801 | &mPhysicalDeviceProperties); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 802 | |
Jamie Madill | 30b5d84 | 2018-08-31 17:19:12 -0400 | [diff] [blame] | 803 | vkGetPhysicalDeviceFeatures(mPhysicalDevice, &mPhysicalDeviceFeatures); |
| 804 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 805 | // Ensure we can find a graphics queue family. |
| 806 | uint32_t queueCount = 0; |
| 807 | vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr); |
| 808 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 809 | ANGLE_VK_CHECK(displayVk, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 810 | |
| 811 | mQueueFamilyProperties.resize(queueCount); |
| 812 | vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, |
| 813 | mQueueFamilyProperties.data()); |
| 814 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 815 | size_t graphicsQueueFamilyCount = false; |
| 816 | uint32_t firstGraphicsQueueFamily = 0; |
Shahbaz Youssefi | 823d897 | 2018-11-13 10:52:40 -0500 | [diff] [blame] | 817 | constexpr VkQueueFlags kGraphicsAndCompute = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 818 | for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex) |
| 819 | { |
| 820 | const auto &queueInfo = mQueueFamilyProperties[familyIndex]; |
Shahbaz Youssefi | 823d897 | 2018-11-13 10:52:40 -0500 | [diff] [blame] | 821 | if ((queueInfo.queueFlags & kGraphicsAndCompute) == kGraphicsAndCompute) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 822 | { |
| 823 | ASSERT(queueInfo.queueCount > 0); |
| 824 | graphicsQueueFamilyCount++; |
| 825 | if (firstGraphicsQueueFamily == 0) |
| 826 | { |
| 827 | firstGraphicsQueueFamily = familyIndex; |
| 828 | } |
| 829 | break; |
| 830 | } |
| 831 | } |
| 832 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 833 | ANGLE_VK_CHECK(displayVk, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 834 | |
| 835 | // If only one queue family, go ahead and initialize the device. If there is more than one |
| 836 | // queue, we'll have to wait until we see a WindowSurface to know which supports present. |
| 837 | if (graphicsQueueFamilyCount == 1) |
| 838 | { |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 839 | ANGLE_TRY(initializeDevice(displayVk, firstGraphicsQueueFamily)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 840 | } |
| 841 | |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 842 | // Store the physical device memory properties so we can find the right memory pools. |
| 843 | mMemoryProperties.init(mPhysicalDevice); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 844 | |
Jamie Madill | 06ca634 | 2018-07-12 15:56:53 -0400 | [diff] [blame] | 845 | GlslangWrapper::Initialize(); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 846 | |
Jamie Madill | 6a89d22 | 2017-11-02 11:59:51 -0400 | [diff] [blame] | 847 | // Initialize the format table. |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 848 | mFormatTable.initialize(this, &mNativeTextureCaps, &mNativeCaps.compressedTextureFormats); |
Jamie Madill | 6a89d22 | 2017-11-02 11:59:51 -0400 | [diff] [blame] | 849 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 850 | return angle::Result::Continue; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 851 | } |
| 852 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 853 | angle::Result RendererVk::initializeDevice(DisplayVk *displayVk, uint32_t queueFamilyIndex) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 854 | { |
| 855 | uint32_t deviceLayerCount = 0; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 856 | ANGLE_VK_TRY(displayVk, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 857 | vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 858 | |
| 859 | std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount); |
| 860 | if (deviceLayerCount > 0) |
| 861 | { |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 862 | ANGLE_VK_TRY(displayVk, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, |
| 863 | deviceLayerProps.data())); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 864 | } |
| 865 | |
Michael Spang | 6c13c70 | 2019-02-06 15:59:44 -0500 | [diff] [blame] | 866 | VulkanLayerVector enabledDeviceLayerNames; |
| 867 | if (mEnableValidationLayers) |
| 868 | { |
| 869 | mEnableValidationLayers = |
| 870 | GetAvailableValidationLayers(deviceLayerProps, false, &enabledDeviceLayerNames); |
| 871 | } |
| 872 | |
| 873 | const char *wsiLayer = displayVk->getWSILayer(); |
| 874 | if (wsiLayer) |
| 875 | { |
| 876 | enabledDeviceLayerNames.push_back(wsiLayer); |
| 877 | } |
| 878 | |
| 879 | // Enumerate device extensions that are provided by the vulkan |
| 880 | // implementation and implicit layers. |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 881 | uint32_t deviceExtensionCount = 0; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 882 | ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, |
| 883 | &deviceExtensionCount, nullptr)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 884 | |
| 885 | std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount); |
| 886 | if (deviceExtensionCount > 0) |
| 887 | { |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 888 | ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, |
| 889 | &deviceExtensionCount, |
| 890 | deviceExtensionProps.data())); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 891 | } |
| 892 | |
Michael Spang | 6c13c70 | 2019-02-06 15:59:44 -0500 | [diff] [blame] | 893 | // Enumerate device extensions that are provided by explicit layers. |
| 894 | for (const char *layerName : enabledDeviceLayerNames) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 895 | { |
Michael Spang | 6c13c70 | 2019-02-06 15:59:44 -0500 | [diff] [blame] | 896 | uint32_t previousExtensionCount = deviceExtensionProps.size(); |
| 897 | uint32_t deviceLayerExtensionCount = 0; |
| 898 | ANGLE_VK_TRY(displayVk, |
| 899 | vkEnumerateDeviceExtensionProperties(mPhysicalDevice, layerName, |
| 900 | &deviceLayerExtensionCount, nullptr)); |
| 901 | deviceExtensionProps.resize(previousExtensionCount + deviceLayerExtensionCount); |
| 902 | ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties( |
| 903 | mPhysicalDevice, layerName, &deviceLayerExtensionCount, |
| 904 | deviceExtensionProps.data() + previousExtensionCount)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 905 | } |
| 906 | |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 907 | ExtensionNameList deviceExtensionNames; |
| 908 | if (!deviceExtensionProps.empty()) |
| 909 | { |
| 910 | ASSERT(deviceExtensionNames.size() <= deviceExtensionProps.size()); |
| 911 | for (const VkExtensionProperties &prop : deviceExtensionProps) |
| 912 | { |
| 913 | deviceExtensionNames.push_back(prop.extensionName); |
| 914 | } |
| 915 | std::sort(deviceExtensionNames.begin(), deviceExtensionNames.end(), StrLess); |
| 916 | } |
| 917 | |
| 918 | ExtensionNameList enabledDeviceExtensions; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 919 | enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); |
| 920 | |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 921 | initFeatures(deviceExtensionNames); |
Ian Elliott | bcb7890 | 2018-12-19 11:46:29 -0700 | [diff] [blame] | 922 | mFeaturesInitialized = true; |
| 923 | |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 924 | // Selectively enable KHR_MAINTENANCE1 to support viewport flipping. |
Ian Elliott | 52f5da4 | 2018-12-21 09:02:09 -0700 | [diff] [blame] | 925 | if ((getFeatures().flipViewportY) && |
| 926 | (mPhysicalDeviceProperties.apiVersion < VK_MAKE_VERSION(1, 1, 0))) |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 927 | { |
| 928 | enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME); |
| 929 | } |
Ian Elliott | bcb7890 | 2018-12-19 11:46:29 -0700 | [diff] [blame] | 930 | if (getFeatures().supportsIncrementalPresent) |
| 931 | { |
| 932 | enabledDeviceExtensions.push_back(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME); |
| 933 | } |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 934 | |
Geoff Lang | 009696c | 2019-01-31 14:47:07 -0500 | [diff] [blame] | 935 | #if defined(ANGLE_PLATFORM_ANDROID) |
| 936 | if (getFeatures().supportsAndroidHardwareBuffer) |
| 937 | { |
| 938 | enabledDeviceExtensions.push_back(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME); |
| 939 | enabledDeviceExtensions.push_back( |
| 940 | VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME); |
| 941 | InitExternalMemoryHardwareBufferANDROIDFunctions(mInstance); |
| 942 | } |
| 943 | #else |
| 944 | ASSERT(!getFeatures().supportsAndroidHardwareBuffer); |
| 945 | #endif |
| 946 | |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 947 | std::sort(enabledDeviceExtensions.begin(), enabledDeviceExtensions.end(), StrLess); |
| 948 | ANGLE_VK_TRY(displayVk, VerifyExtensionsPresent(deviceExtensionNames, enabledDeviceExtensions)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 949 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 950 | // Select additional features to be enabled |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 951 | VkPhysicalDeviceFeatures2KHR enabledFeatures = {}; |
| 952 | enabledFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; |
| 953 | enabledFeatures.features.inheritedQueries = mPhysicalDeviceFeatures.inheritedQueries; |
| 954 | enabledFeatures.features.robustBufferAccess = mPhysicalDeviceFeatures.robustBufferAccess; |
Shahbaz Youssefi | 962c222 | 2019-02-20 15:43:41 -0500 | [diff] [blame] | 955 | enabledFeatures.features.samplerAnisotropy = mPhysicalDeviceFeatures.samplerAnisotropy; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 956 | |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 957 | VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT divisorFeatures = {}; |
| 958 | divisorFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT; |
| 959 | divisorFeatures.vertexAttributeInstanceRateDivisor = true; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 960 | |
Shahbaz Youssefi | 76bd848 | 2019-02-13 13:00:44 -0500 | [diff] [blame] | 961 | float zeroPriority = 0.0f; |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 962 | VkDeviceQueueCreateInfo queueCreateInfo = {}; |
Shahbaz Youssefi | 76bd848 | 2019-02-13 13:00:44 -0500 | [diff] [blame] | 963 | queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 964 | queueCreateInfo.flags = 0; |
| 965 | queueCreateInfo.queueFamilyIndex = queueFamilyIndex; |
| 966 | queueCreateInfo.queueCount = 1; |
| 967 | queueCreateInfo.pQueuePriorities = &zeroPriority; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 968 | |
| 969 | // Initialize the device |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 970 | VkDeviceCreateInfo createInfo = {}; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 971 | |
Shahbaz Youssefi | 76bd848 | 2019-02-13 13:00:44 -0500 | [diff] [blame] | 972 | createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; |
| 973 | createInfo.flags = 0; |
| 974 | createInfo.queueCreateInfoCount = 1; |
| 975 | createInfo.pQueueCreateInfos = &queueCreateInfo; |
| 976 | createInfo.enabledLayerCount = enabledDeviceLayerNames.size(); |
| 977 | createInfo.ppEnabledLayerNames = enabledDeviceLayerNames.data(); |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 978 | |
| 979 | if (vkGetPhysicalDeviceProperties2KHR && |
| 980 | ExtensionFound(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME, deviceExtensionNames)) |
| 981 | { |
| 982 | enabledDeviceExtensions.push_back(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME); |
| 983 | enabledFeatures.pNext = &divisorFeatures; |
| 984 | |
| 985 | VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT divisorProperties = {}; |
| 986 | divisorProperties.sType = |
| 987 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT; |
| 988 | |
| 989 | VkPhysicalDeviceProperties2 deviceProperties = {}; |
| 990 | deviceProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2; |
| 991 | deviceProperties.pNext = &divisorProperties; |
| 992 | |
| 993 | vkGetPhysicalDeviceProperties2KHR(mPhysicalDevice, &deviceProperties); |
| 994 | mMaxVertexAttribDivisor = divisorProperties.maxVertexAttribDivisor; |
| 995 | |
| 996 | createInfo.pNext = &enabledFeatures; |
| 997 | } |
| 998 | else |
| 999 | { |
| 1000 | createInfo.pEnabledFeatures = &enabledFeatures.features; |
| 1001 | } |
| 1002 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1003 | createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size()); |
| 1004 | createInfo.ppEnabledExtensionNames = |
| 1005 | enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data(); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1006 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1007 | ANGLE_VK_TRY(displayVk, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1008 | |
| 1009 | mCurrentQueueFamilyIndex = queueFamilyIndex; |
| 1010 | |
| 1011 | vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue); |
| 1012 | |
| 1013 | // Initialize the command pool now that we know the queue family index. |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 1014 | VkCommandPoolCreateInfo commandPoolInfo = {}; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1015 | commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 1016 | commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT; |
| 1017 | commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1018 | |
Tobin Ehlis | 47ca1b2 | 2019-01-23 16:11:41 +0000 | [diff] [blame] | 1019 | ANGLE_VK_TRY(displayVk, mCommandPool.init(mDevice, commandPoolInfo)); |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1020 | |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1021 | // Initialize the vulkan pipeline cache. |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 1022 | ANGLE_TRY(initPipelineCache(displayVk)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1023 | |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1024 | // Initialize the submission semaphore pool. |
| 1025 | ANGLE_TRY(mSubmitSemaphorePool.init(displayVk, vk::kDefaultSemaphorePoolSize)); |
| 1026 | |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1027 | #if ANGLE_ENABLE_VULKAN_GPU_TRACE_EVENTS |
| 1028 | angle::PlatformMethods *platform = ANGLEPlatformCurrent(); |
| 1029 | ASSERT(platform); |
| 1030 | |
| 1031 | // GPU tracing workaround for anglebug.com/2927. The renderer should not emit gpu events during |
| 1032 | // platform discovery. |
| 1033 | const unsigned char *gpuEventsEnabled = |
| 1034 | platform->getTraceCategoryEnabledFlag(platform, "gpu.angle.gpu"); |
| 1035 | mGpuEventsEnabled = gpuEventsEnabled && *gpuEventsEnabled; |
| 1036 | #endif |
| 1037 | |
| 1038 | if (mGpuEventsEnabled) |
| 1039 | { |
| 1040 | // Calculate the difference between CPU and GPU clocks for GPU event reporting. |
| 1041 | ANGLE_TRY(mGpuEventQueryPool.init(displayVk, VK_QUERY_TYPE_TIMESTAMP, |
| 1042 | vk::kDefaultTimestampQueryPoolSize)); |
| 1043 | ANGLE_TRY(synchronizeCpuGpuTime(displayVk)); |
| 1044 | } |
| 1045 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1046 | return angle::Result::Continue; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1047 | } |
| 1048 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1049 | angle::Result RendererVk::selectPresentQueueForSurface(DisplayVk *displayVk, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1050 | VkSurfaceKHR surface, |
| 1051 | uint32_t *presentQueueOut) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1052 | { |
| 1053 | // We've already initialized a device, and can't re-create it unless it's never been used. |
| 1054 | // TODO(jmadill): Handle the re-creation case if necessary. |
| 1055 | if (mDevice != VK_NULL_HANDLE) |
| 1056 | { |
| 1057 | ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max()); |
| 1058 | |
| 1059 | // Check if the current device supports present on this surface. |
| 1060 | VkBool32 supportsPresent = VK_FALSE; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1061 | ANGLE_VK_TRY(displayVk, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1062 | vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex, |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1063 | surface, &supportsPresent)); |
| 1064 | |
Jamie Madill | 6cad773 | 2018-07-11 09:01:17 -0400 | [diff] [blame] | 1065 | if (supportsPresent == VK_TRUE) |
| 1066 | { |
| 1067 | *presentQueueOut = mCurrentQueueFamilyIndex; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1068 | return angle::Result::Continue; |
Jamie Madill | 6cad773 | 2018-07-11 09:01:17 -0400 | [diff] [blame] | 1069 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | // Find a graphics and present queue. |
| 1073 | Optional<uint32_t> newPresentQueue; |
| 1074 | uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size()); |
Shahbaz Youssefi | 823d897 | 2018-11-13 10:52:40 -0500 | [diff] [blame] | 1075 | constexpr VkQueueFlags kGraphicsAndCompute = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1076 | for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex) |
| 1077 | { |
| 1078 | const auto &queueInfo = mQueueFamilyProperties[queueIndex]; |
Shahbaz Youssefi | 823d897 | 2018-11-13 10:52:40 -0500 | [diff] [blame] | 1079 | if ((queueInfo.queueFlags & kGraphicsAndCompute) == kGraphicsAndCompute) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1080 | { |
| 1081 | VkBool32 supportsPresent = VK_FALSE; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1082 | ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceSupportKHR( |
| 1083 | mPhysicalDevice, queueIndex, surface, &supportsPresent)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1084 | |
| 1085 | if (supportsPresent == VK_TRUE) |
| 1086 | { |
| 1087 | newPresentQueue = queueIndex; |
| 1088 | break; |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1093 | ANGLE_VK_CHECK(displayVk, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED); |
| 1094 | ANGLE_TRY(initializeDevice(displayVk, newPresentQueue.value())); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1095 | |
Jamie Madill | 6cad773 | 2018-07-11 09:01:17 -0400 | [diff] [blame] | 1096 | *presentQueueOut = newPresentQueue.value(); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1097 | return angle::Result::Continue; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | std::string RendererVk::getVendorString() const |
| 1101 | { |
Olli Etuaho | c6a0618 | 2018-04-13 14:11:46 +0300 | [diff] [blame] | 1102 | return GetVendorString(mPhysicalDeviceProperties.vendorID); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1103 | } |
| 1104 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 1105 | std::string RendererVk::getRendererDescription() const |
| 1106 | { |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1107 | std::stringstream strstr; |
| 1108 | |
| 1109 | uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion; |
| 1110 | |
| 1111 | strstr << "Vulkan "; |
| 1112 | strstr << VK_VERSION_MAJOR(apiVersion) << "."; |
| 1113 | strstr << VK_VERSION_MINOR(apiVersion) << "."; |
| 1114 | strstr << VK_VERSION_PATCH(apiVersion); |
| 1115 | |
Olli Etuaho | c6a0618 | 2018-04-13 14:11:46 +0300 | [diff] [blame] | 1116 | strstr << "("; |
| 1117 | |
| 1118 | // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that |
| 1119 | // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be |
| 1120 | // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated |
| 1121 | // driver detection. |
| 1122 | if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA) |
| 1123 | { |
| 1124 | strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " "; |
| 1125 | } |
| 1126 | |
Geoff Lang | a7af56b | 2018-12-14 14:20:28 -0500 | [diff] [blame] | 1127 | strstr << mPhysicalDeviceProperties.deviceName; |
| 1128 | strstr << " (" << gl::FmtHex(mPhysicalDeviceProperties.deviceID) << ")"; |
| 1129 | |
| 1130 | strstr << ")"; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1131 | |
| 1132 | return strstr.str(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 1133 | } |
| 1134 | |
Shahbaz Youssefi | 092481a | 2018-11-08 00:25:50 -0500 | [diff] [blame] | 1135 | gl::Version RendererVk::getMaxSupportedESVersion() const |
| 1136 | { |
Geoff Lang | 0c2c923 | 2019-01-14 16:01:38 -0500 | [diff] [blame] | 1137 | // Current highest supported version |
Shahbaz Youssefi | 9dc8eaf | 2019-02-11 11:04:54 -0500 | [diff] [blame] | 1138 | gl::Version maxVersion = gl::Version(3, 0); |
| 1139 | |
| 1140 | #if ANGLE_VULKAN_CONFORMANT_CONFIGS_ONLY |
| 1141 | // TODO: Disallow ES 3.0 until supported. http://crbug.com/angleproject/2950 |
| 1142 | maxVersion = gl::Version(2, 0); |
| 1143 | #endif |
Shahbaz Youssefi | 092481a | 2018-11-08 00:25:50 -0500 | [diff] [blame] | 1144 | |
Geoff Lang | 0c2c923 | 2019-01-14 16:01:38 -0500 | [diff] [blame] | 1145 | // Vulkan inherited queries are required to support any GL query type |
| 1146 | if (!mPhysicalDeviceFeatures.inheritedQueries) |
Shahbaz Youssefi | 092481a | 2018-11-08 00:25:50 -0500 | [diff] [blame] | 1147 | { |
Geoff Lang | 0c2c923 | 2019-01-14 16:01:38 -0500 | [diff] [blame] | 1148 | maxVersion = std::max(maxVersion, gl::Version(2, 0)); |
Shahbaz Youssefi | 092481a | 2018-11-08 00:25:50 -0500 | [diff] [blame] | 1149 | } |
| 1150 | |
Geoff Lang | 0c2c923 | 2019-01-14 16:01:38 -0500 | [diff] [blame] | 1151 | return maxVersion; |
Shahbaz Youssefi | 092481a | 2018-11-08 00:25:50 -0500 | [diff] [blame] | 1152 | } |
| 1153 | |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 1154 | void RendererVk::initFeatures(const ExtensionNameList &deviceExtensionNames) |
Jamie Madill | 1222207 | 2018-07-11 14:59:48 -0400 | [diff] [blame] | 1155 | { |
Jamie Madill | b36a481 | 2018-09-25 10:15:11 -0400 | [diff] [blame] | 1156 | // Use OpenGL line rasterization rules by default. |
| 1157 | // TODO(jmadill): Fix Android support. http://anglebug.com/2830 |
| 1158 | #if defined(ANGLE_PLATFORM_ANDROID) |
| 1159 | mFeatures.basicGLLineRasterization = false; |
| 1160 | #else |
Jamie Madill | 1222207 | 2018-07-11 14:59:48 -0400 | [diff] [blame] | 1161 | mFeatures.basicGLLineRasterization = true; |
Jamie Madill | b36a481 | 2018-09-25 10:15:11 -0400 | [diff] [blame] | 1162 | #endif // defined(ANGLE_PLATFORM_ANDROID) |
Jamie Madill | 1222207 | 2018-07-11 14:59:48 -0400 | [diff] [blame] | 1163 | |
Ian Elliott | 52f5da4 | 2018-12-21 09:02:09 -0700 | [diff] [blame] | 1164 | if ((mPhysicalDeviceProperties.apiVersion >= VK_MAKE_VERSION(1, 1, 0)) || |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 1165 | ExtensionFound(VK_KHR_MAINTENANCE1_EXTENSION_NAME, deviceExtensionNames)) |
Ian Elliott | d50521f | 2018-12-20 12:05:14 -0700 | [diff] [blame] | 1166 | { |
| 1167 | // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need |
| 1168 | // investigation. http://anglebug.com/2728 |
| 1169 | mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID); |
| 1170 | } |
Frank Henigman | beb669d | 2018-09-21 16:25:52 -0400 | [diff] [blame] | 1171 | |
| 1172 | #ifdef ANGLE_PLATFORM_WINDOWS |
| 1173 | // http://anglebug.com/2838 |
| 1174 | mFeatures.extraCopyBufferRegion = IsIntel(mPhysicalDeviceProperties.vendorID); |
Shahbaz Youssefi | 4f3b207 | 2019-01-01 14:48:25 -0500 | [diff] [blame] | 1175 | |
| 1176 | // http://anglebug.com/3055 |
| 1177 | mFeatures.forceCpuPathForCubeMapCopy = IsIntel(mPhysicalDeviceProperties.vendorID); |
Frank Henigman | beb669d | 2018-09-21 16:25:52 -0400 | [diff] [blame] | 1178 | #endif |
Shahbaz Youssefi | d856ca4 | 2018-10-31 16:55:12 -0400 | [diff] [blame] | 1179 | |
| 1180 | angle::PlatformMethods *platform = ANGLEPlatformCurrent(); |
| 1181 | platform->overrideFeaturesVk(platform, &mFeatures); |
Jamie Madill | fde74c0 | 2018-11-18 16:12:02 -0500 | [diff] [blame] | 1182 | |
| 1183 | // Work around incorrect NVIDIA point size range clamping. |
| 1184 | // TODO(jmadill): Narrow driver range once fixed. http://anglebug.com/2970 |
| 1185 | if (IsNvidia(mPhysicalDeviceProperties.vendorID)) |
| 1186 | { |
| 1187 | mFeatures.clampPointSize = true; |
| 1188 | } |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 1189 | |
Jamie Madill | fa7ca18 | 2019-01-15 11:20:58 -0500 | [diff] [blame] | 1190 | // We also need to clamp point size on several Android drivers. |
| 1191 | // TODO(jmadill): Remove suppression once fixed. http://anglebug.com/2599 |
| 1192 | if (IsAndroid()) |
| 1193 | { |
| 1194 | mFeatures.clampPointSize = true; |
| 1195 | } |
| 1196 | |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 1197 | #if defined(ANGLE_PLATFORM_ANDROID) |
Shahbaz Youssefi | b08457d | 2018-12-11 15:13:54 -0500 | [diff] [blame] | 1198 | // Work around ineffective compute-graphics barriers on Nexus 5X. |
| 1199 | // TODO(syoussefi): Figure out which other vendors and driver versions are affected. |
| 1200 | // http://anglebug.com/3019 |
| 1201 | mFeatures.flushAfterVertexConversion = |
| 1202 | IsNexus5X(mPhysicalDeviceProperties.vendorID, mPhysicalDeviceProperties.deviceID); |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 1203 | #endif |
Ian Elliott | bcb7890 | 2018-12-19 11:46:29 -0700 | [diff] [blame] | 1204 | |
Frank Henigman | 52047de | 2018-11-13 17:22:36 -0500 | [diff] [blame] | 1205 | if (ExtensionFound(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, deviceExtensionNames)) |
Ian Elliott | bcb7890 | 2018-12-19 11:46:29 -0700 | [diff] [blame] | 1206 | { |
| 1207 | mFeatures.supportsIncrementalPresent = true; |
| 1208 | } |
Geoff Lang | 009696c | 2019-01-31 14:47:07 -0500 | [diff] [blame] | 1209 | |
| 1210 | #if defined(ANGLE_PLATFORM_ANDROID) |
Yuly Novikov | dbbdf56 | 2019-02-27 19:48:16 -0500 | [diff] [blame] | 1211 | mFeatures.supportsAndroidHardwareBuffer = |
| 1212 | ExtensionFound(VK_ANDROID_EXTERNAL_MEMORY_ANDROID_HARDWARE_BUFFER_EXTENSION_NAME, |
| 1213 | deviceExtensionNames) && |
| 1214 | ExtensionFound(VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME, deviceExtensionNames); |
Geoff Lang | 009696c | 2019-01-31 14:47:07 -0500 | [diff] [blame] | 1215 | #endif |
Yuly Novikov | 1581ff0 | 2019-02-28 20:59:26 -0500 | [diff] [blame] | 1216 | |
| 1217 | if (IsLinux() && IsIntel(mPhysicalDeviceProperties.vendorID)) |
| 1218 | { |
| 1219 | mFeatures.disableFifoPresentMode = true; |
| 1220 | } |
Jamie Madill | 1222207 | 2018-07-11 14:59:48 -0400 | [diff] [blame] | 1221 | } |
| 1222 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1223 | void RendererVk::initPipelineCacheVkKey() |
| 1224 | { |
| 1225 | std::ostringstream hashStream("ANGLE Pipeline Cache: ", std::ios_base::ate); |
| 1226 | // Add the pipeline cache UUID to make sure the blob cache always gives a compatible pipeline |
| 1227 | // cache. It's not particularly necessary to write it as a hex number as done here, so long as |
| 1228 | // there is no '\0' in the result. |
| 1229 | for (const uint32_t c : mPhysicalDeviceProperties.pipelineCacheUUID) |
| 1230 | { |
| 1231 | hashStream << std::hex << c; |
| 1232 | } |
| 1233 | // Add the vendor and device id too for good measure. |
| 1234 | hashStream << std::hex << mPhysicalDeviceProperties.vendorID; |
| 1235 | hashStream << std::hex << mPhysicalDeviceProperties.deviceID; |
| 1236 | |
| 1237 | const std::string &hashString = hashStream.str(); |
| 1238 | angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(hashString.c_str()), |
| 1239 | hashString.length(), mPipelineCacheVkBlobKey.data()); |
| 1240 | } |
| 1241 | |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 1242 | angle::Result RendererVk::initPipelineCache(DisplayVk *display) |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1243 | { |
| 1244 | initPipelineCacheVkKey(); |
| 1245 | |
| 1246 | egl::BlobCache::Value initialData; |
| 1247 | bool success = display->getBlobCache()->get(display->getScratchBuffer(), |
| 1248 | mPipelineCacheVkBlobKey, &initialData); |
| 1249 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 1250 | VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {}; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1251 | |
| 1252 | pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1253 | pipelineCacheCreateInfo.flags = 0; |
| 1254 | pipelineCacheCreateInfo.initialDataSize = success ? initialData.size() : 0; |
| 1255 | pipelineCacheCreateInfo.pInitialData = success ? initialData.data() : nullptr; |
| 1256 | |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 1257 | ANGLE_VK_TRY(display, mPipelineCache.init(mDevice, pipelineCacheCreateInfo)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1258 | return angle::Result::Continue; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1259 | } |
| 1260 | |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1261 | void RendererVk::getSubmitWaitSemaphores( |
| 1262 | vk::Context *context, |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1263 | angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> *waitSemaphores, |
| 1264 | angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> *waitStageMasks) |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1265 | { |
| 1266 | if (mSubmitLastSignaledSemaphore.getSemaphore()) |
| 1267 | { |
| 1268 | waitSemaphores->push_back(mSubmitLastSignaledSemaphore.getSemaphore()->getHandle()); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1269 | waitStageMasks->push_back(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT); |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1270 | |
| 1271 | // Return the semaphore to the pool (which will remain valid and unused until the |
| 1272 | // queue it's about to be waited on has finished execution). |
| 1273 | mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore); |
| 1274 | } |
| 1275 | |
| 1276 | for (vk::SemaphoreHelper &semaphore : mSubmitWaitSemaphores) |
| 1277 | { |
| 1278 | waitSemaphores->push_back(semaphore.getSemaphore()->getHandle()); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1279 | waitStageMasks->push_back(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT); |
| 1280 | |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1281 | mSubmitSemaphorePool.freeSemaphore(context, &semaphore); |
| 1282 | } |
| 1283 | mSubmitWaitSemaphores.clear(); |
| 1284 | } |
| 1285 | |
Jamie Madill | acccc6c | 2016-05-03 17:22:10 -0400 | [diff] [blame] | 1286 | const gl::Caps &RendererVk::getNativeCaps() const |
| 1287 | { |
| 1288 | ensureCapsInitialized(); |
| 1289 | return mNativeCaps; |
| 1290 | } |
| 1291 | |
| 1292 | const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const |
| 1293 | { |
| 1294 | ensureCapsInitialized(); |
| 1295 | return mNativeTextureCaps; |
| 1296 | } |
| 1297 | |
| 1298 | const gl::Extensions &RendererVk::getNativeExtensions() const |
| 1299 | { |
| 1300 | ensureCapsInitialized(); |
| 1301 | return mNativeExtensions; |
| 1302 | } |
| 1303 | |
| 1304 | const gl::Limitations &RendererVk::getNativeLimitations() const |
| 1305 | { |
| 1306 | ensureCapsInitialized(); |
| 1307 | return mNativeLimitations; |
| 1308 | } |
| 1309 | |
Luc Ferron | daedf4d | 2018-03-16 09:28:53 -0400 | [diff] [blame] | 1310 | uint32_t RendererVk::getMaxActiveTextures() |
| 1311 | { |
| 1312 | // TODO(lucferron): expose this limitation to GL in Context Caps |
| 1313 | return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers, |
| 1314 | gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES); |
| 1315 | } |
| 1316 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1317 | const vk::CommandPool &RendererVk::getCommandPool() const |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1318 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1319 | return mCommandPool; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1320 | } |
| 1321 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1322 | angle::Result RendererVk::finish(vk::Context *context) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1323 | { |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 1324 | if (!mCommandGraph.empty()) |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1325 | { |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 1326 | TRACE_EVENT0("gpu.angle", "RendererVk::finish"); |
| 1327 | |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1328 | vk::Scoped<vk::CommandBuffer> commandBatch(mDevice); |
| 1329 | ANGLE_TRY(flushCommandGraph(context, &commandBatch.get())); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1330 | |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1331 | angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores; |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1332 | angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks; |
| 1333 | getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks); |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1334 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 1335 | VkSubmitInfo submitInfo = {}; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1336 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1337 | submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size()); |
| 1338 | submitInfo.pWaitSemaphores = waitSemaphores.data(); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1339 | submitInfo.pWaitDstStageMask = waitStageMasks.data(); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1340 | submitInfo.commandBufferCount = 1; |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 1341 | submitInfo.pCommandBuffers = commandBatch.get().ptr(); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1342 | submitInfo.signalSemaphoreCount = 0; |
| 1343 | submitInfo.pSignalSemaphores = nullptr; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1344 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1345 | ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get()))); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1346 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 1347 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1348 | ASSERT(mQueue != VK_NULL_HANDLE); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1349 | ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue)); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1350 | freeAllInFlightResources(); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1351 | |
| 1352 | if (mGpuEventsEnabled) |
| 1353 | { |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1354 | // This loop should in practice execute once since the queue is already idle. |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1355 | while (mInFlightGpuEventQueries.size() > 0) |
| 1356 | { |
| 1357 | ANGLE_TRY(checkCompletedGpuEvents(context)); |
| 1358 | } |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1359 | // Recalculate the CPU/GPU time difference to account for clock drifting. Avoid unnecessary |
| 1360 | // synchronization if there is no event to be adjusted (happens when finish() gets called |
| 1361 | // multiple times towards the end of the application). |
| 1362 | if (mGpuEvents.size() > 0) |
| 1363 | { |
| 1364 | ANGLE_TRY(synchronizeCpuGpuTime(context)); |
| 1365 | } |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1366 | } |
| 1367 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1368 | return angle::Result::Continue; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1369 | } |
| 1370 | |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1371 | void RendererVk::freeAllInFlightResources() |
| 1372 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1373 | for (CommandBatch &batch : mInFlightCommands) |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1374 | { |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 1375 | // On device loss we need to wait for fence to be signaled before destroying it |
| 1376 | if (mDeviceLost) |
| 1377 | { |
| 1378 | VkResult status = batch.fence.wait(mDevice, kMaxFenceWaitTimeNs); |
| 1379 | // If wait times out, it is probably not possible to recover from lost device |
| 1380 | ASSERT(status == VK_SUCCESS || status == VK_ERROR_DEVICE_LOST); |
| 1381 | } |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1382 | batch.fence.destroy(mDevice); |
Tobin Ehlis | 47ca1b2 | 2019-01-23 16:11:41 +0000 | [diff] [blame] | 1383 | batch.commandPool.destroy(mDevice); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1384 | } |
| 1385 | mInFlightCommands.clear(); |
| 1386 | |
| 1387 | for (auto &garbage : mGarbage) |
| 1388 | { |
Jamie Madill | e88ec8e | 2017-10-31 17:18:14 -0400 | [diff] [blame] | 1389 | garbage.destroy(mDevice); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1390 | } |
| 1391 | mGarbage.clear(); |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 1392 | |
| 1393 | mLastCompletedQueueSerial = mLastSubmittedQueueSerial; |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1394 | } |
| 1395 | |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 1396 | angle::Result RendererVk::checkCompletedCommands(vk::Context *context) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1397 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1398 | int finishedCount = 0; |
Jamie Madill | f651c77 | 2017-02-21 15:03:51 -0500 | [diff] [blame] | 1399 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1400 | for (CommandBatch &batch : mInFlightCommands) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1401 | { |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1402 | VkResult result = batch.fence.getStatus(mDevice); |
| 1403 | if (result == VK_NOT_READY) |
| 1404 | { |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1405 | break; |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1406 | } |
| 1407 | ANGLE_VK_TRY(context, result); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1408 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1409 | ASSERT(batch.serial > mLastCompletedQueueSerial); |
| 1410 | mLastCompletedQueueSerial = batch.serial; |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1411 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1412 | batch.fence.destroy(mDevice); |
Tobin Ehlis | 4a41914 | 2019-02-05 08:50:30 -0700 | [diff] [blame] | 1413 | TRACE_EVENT0("gpu.angle", "commandPool.destroy"); |
Tobin Ehlis | 47ca1b2 | 2019-01-23 16:11:41 +0000 | [diff] [blame] | 1414 | batch.commandPool.destroy(mDevice); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1415 | ++finishedCount; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1416 | } |
| 1417 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1418 | mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1419 | |
| 1420 | size_t freeIndex = 0; |
| 1421 | for (; freeIndex < mGarbage.size(); ++freeIndex) |
| 1422 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1423 | if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial)) |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1424 | break; |
| 1425 | } |
| 1426 | |
| 1427 | // Remove the entries from the garbage list - they should be ready to go. |
| 1428 | if (freeIndex > 0) |
| 1429 | { |
| 1430 | mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex); |
Jamie Madill | f651c77 | 2017-02-21 15:03:51 -0500 | [diff] [blame] | 1431 | } |
| 1432 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1433 | return angle::Result::Continue; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1434 | } |
| 1435 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1436 | angle::Result RendererVk::submitFrame(vk::Context *context, |
| 1437 | const VkSubmitInfo &submitInfo, |
| 1438 | vk::CommandBuffer &&commandBuffer) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1439 | { |
Tobin Ehlis | 573f76b | 2018-05-03 11:10:44 -0600 | [diff] [blame] | 1440 | TRACE_EVENT0("gpu.angle", "RendererVk::submitFrame"); |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 1441 | VkFenceCreateInfo fenceInfo = {}; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1442 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 1443 | fenceInfo.flags = 0; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1444 | |
Tobin Ehlis | 47ca1b2 | 2019-01-23 16:11:41 +0000 | [diff] [blame] | 1445 | vk::Scoped<CommandBatch> scopedBatch(mDevice); |
Jamie Madill | bea35a6 | 2018-07-05 11:54:10 -0400 | [diff] [blame] | 1446 | CommandBatch &batch = scopedBatch.get(); |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1447 | ANGLE_VK_TRY(context, batch.fence.init(mDevice, fenceInfo)); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1448 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1449 | ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle())); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1450 | |
| 1451 | // Store this command buffer in the in-flight list. |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1452 | batch.commandPool = std::move(mCommandPool); |
| 1453 | batch.serial = mCurrentQueueSerial; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1454 | |
Jamie Madill | bea35a6 | 2018-07-05 11:54:10 -0400 | [diff] [blame] | 1455 | mInFlightCommands.emplace_back(scopedBatch.release()); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1456 | |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 1457 | // CPU should be throttled to avoid mInFlightCommands from growing too fast. That is done on |
| 1458 | // swap() though, and there could be multiple submissions in between (through glFlush() calls), |
Shahbaz Youssefi | 611bbaa | 2018-12-06 01:59:53 +0100 | [diff] [blame] | 1459 | // so the limit is larger than the expected number of images. The |
| 1460 | // InterleavedAttributeDataBenchmark perf test for example issues a large number of flushes. |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 1461 | ASSERT(mInFlightCommands.size() <= kInFlightCommandsLimit); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1462 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 1463 | nextSerial(); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1464 | |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 1465 | ANGLE_TRY(checkCompletedCommands(context)); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 1466 | |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1467 | if (mGpuEventsEnabled) |
| 1468 | { |
| 1469 | ANGLE_TRY(checkCompletedGpuEvents(context)); |
| 1470 | } |
| 1471 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1472 | // Simply null out the command buffer here - it was allocated using the command pool. |
| 1473 | commandBuffer.releaseHandle(); |
| 1474 | |
| 1475 | // Reallocate the command pool for next frame. |
| 1476 | // TODO(jmadill): Consider reusing command pools. |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 1477 | VkCommandPoolCreateInfo poolInfo = {}; |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1478 | poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1479 | poolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT; |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1480 | poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1481 | |
Tobin Ehlis | 47ca1b2 | 2019-01-23 16:11:41 +0000 | [diff] [blame] | 1482 | ANGLE_VK_TRY(context, mCommandPool.init(mDevice, poolInfo)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1483 | return angle::Result::Continue; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 1484 | } |
| 1485 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 1486 | void RendererVk::nextSerial() |
| 1487 | { |
| 1488 | // Increment the queue serial. If this fails, we should restart ANGLE. |
| 1489 | mLastSubmittedQueueSerial = mCurrentQueueSerial; |
| 1490 | mCurrentQueueSerial = mQueueSerialFactory.generate(); |
| 1491 | |
| 1492 | // Notify the Contexts that they should be starting new command buffers. |
| 1493 | // We use one command pool per serial/submit associated with this VkQueue. We can also |
| 1494 | // have multiple Contexts sharing one VkQueue. In ContextVk::setupDraw we don't explicitly |
| 1495 | // check for a new serial when starting a new command buffer. We just check that the current |
| 1496 | // recording command buffer is valid. Thus we need to explicitly notify every other Context |
| 1497 | // using this VkQueue that they their current command buffer is no longer valid. |
| 1498 | for (gl::Context *context : mDisplay->getContextSet()) |
| 1499 | { |
| 1500 | ContextVk *contextVk = vk::GetImpl(context); |
| 1501 | contextVk->onCommandBufferFinished(); |
| 1502 | } |
| 1503 | } |
| 1504 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 1505 | bool RendererVk::isSerialInUse(Serial serial) const |
Jamie Madill | 9776035 | 2017-11-09 13:08:29 -0500 | [diff] [blame] | 1506 | { |
| 1507 | return serial > mLastCompletedQueueSerial; |
| 1508 | } |
| 1509 | |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 1510 | angle::Result RendererVk::finishToSerial(vk::Context *context, Serial serial) |
| 1511 | { |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 1512 | bool timedOut = false; |
| 1513 | angle::Result result = finishToSerialOrTimeout(context, serial, kMaxFenceWaitTimeNs, &timedOut); |
| 1514 | |
| 1515 | // Don't tolerate timeout. If such a large wait time results in timeout, something's wrong. |
| 1516 | if (timedOut) |
| 1517 | { |
| 1518 | result = angle::Result::Stop; |
| 1519 | } |
| 1520 | return result; |
| 1521 | } |
| 1522 | |
| 1523 | angle::Result RendererVk::finishToSerialOrTimeout(vk::Context *context, |
| 1524 | Serial serial, |
| 1525 | uint64_t timeout, |
| 1526 | bool *outTimedOut) |
| 1527 | { |
| 1528 | *outTimedOut = false; |
| 1529 | |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 1530 | if (!isSerialInUse(serial) || mInFlightCommands.empty()) |
| 1531 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1532 | return angle::Result::Continue; |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | // Find the first batch with serial equal to or bigger than given serial (note that |
| 1536 | // the batch serials are unique, otherwise upper-bound would have been necessary). |
| 1537 | size_t batchIndex = mInFlightCommands.size() - 1; |
| 1538 | for (size_t i = 0; i < mInFlightCommands.size(); ++i) |
| 1539 | { |
| 1540 | if (mInFlightCommands[i].serial >= serial) |
| 1541 | { |
| 1542 | batchIndex = i; |
| 1543 | break; |
| 1544 | } |
| 1545 | } |
| 1546 | const CommandBatch &batch = mInFlightCommands[batchIndex]; |
| 1547 | |
| 1548 | // Wait for it finish |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 1549 | VkResult status = batch.fence.wait(mDevice, kMaxFenceWaitTimeNs); |
| 1550 | |
| 1551 | // If timed out, report it as such. |
| 1552 | if (status == VK_TIMEOUT) |
| 1553 | { |
| 1554 | *outTimedOut = true; |
| 1555 | return angle::Result::Continue; |
| 1556 | } |
| 1557 | |
| 1558 | ANGLE_VK_TRY(context, status); |
Shahbaz Youssefi | c4765aa | 2018-10-12 14:40:29 -0400 | [diff] [blame] | 1559 | |
| 1560 | // Clean up finished batches. |
| 1561 | return checkCompletedCommands(context); |
| 1562 | } |
| 1563 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1564 | angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context, |
| 1565 | const vk::RenderPassDesc &desc, |
| 1566 | vk::RenderPass **renderPassOut) |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 1567 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1568 | return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc, |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 1569 | renderPassOut); |
| 1570 | } |
| 1571 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1572 | angle::Result RendererVk::getRenderPassWithOps(vk::Context *context, |
| 1573 | const vk::RenderPassDesc &desc, |
| 1574 | const vk::AttachmentOpsArray &ops, |
| 1575 | vk::RenderPass **renderPassOut) |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 1576 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1577 | return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops, |
Jamie Madill | bef918c | 2017-12-13 13:11:30 -0500 | [diff] [blame] | 1578 | renderPassOut); |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 1579 | } |
| 1580 | |
Jamie Madill | a5e0607 | 2018-05-18 14:36:05 -0400 | [diff] [blame] | 1581 | vk::CommandGraph *RendererVk::getCommandGraph() |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1582 | { |
Jamie Madill | a5e0607 | 2018-05-18 14:36:05 -0400 | [diff] [blame] | 1583 | return &mCommandGraph; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1584 | } |
| 1585 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1586 | angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch) |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1587 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1588 | return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache, |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 1589 | &mCommandPool, commandBatch); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1590 | } |
| 1591 | |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1592 | angle::Result RendererVk::flush(vk::Context *context) |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1593 | { |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1594 | if (mCommandGraph.empty()) |
| 1595 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1596 | return angle::Result::Continue; |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1597 | } |
| 1598 | |
Shahbaz Youssefi | 6165602 | 2018-10-24 15:00:50 -0400 | [diff] [blame] | 1599 | TRACE_EVENT0("gpu.angle", "RendererVk::flush"); |
| 1600 | |
Jamie Madill | bea35a6 | 2018-07-05 11:54:10 -0400 | [diff] [blame] | 1601 | vk::Scoped<vk::CommandBuffer> commandBatch(mDevice); |
| 1602 | ANGLE_TRY(flushCommandGraph(context, &commandBatch.get())); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1603 | |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1604 | angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores; |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1605 | angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks; |
| 1606 | getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks); |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1607 | |
| 1608 | // On every flush, create a semaphore to be signaled. On the next submission, this semaphore |
| 1609 | // will be waited on. |
| 1610 | ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &mSubmitLastSignaledSemaphore)); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1611 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 1612 | VkSubmitInfo submitInfo = {}; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1613 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1614 | submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size()); |
| 1615 | submitInfo.pWaitSemaphores = waitSemaphores.data(); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1616 | submitInfo.pWaitDstStageMask = waitStageMasks.data(); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1617 | submitInfo.commandBufferCount = 1; |
Jamie Madill | bea35a6 | 2018-07-05 11:54:10 -0400 | [diff] [blame] | 1618 | submitInfo.pCommandBuffers = commandBatch.get().ptr(); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1619 | submitInfo.signalSemaphoreCount = 1; |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1620 | submitInfo.pSignalSemaphores = mSubmitLastSignaledSemaphore.getSemaphore()->ptr(); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1621 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1622 | ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release())); |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1623 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1624 | return angle::Result::Continue; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 1625 | } |
| 1626 | |
Jamie Madill | 78feddc | 2018-04-27 11:45:05 -0400 | [diff] [blame] | 1627 | Serial RendererVk::issueShaderSerial() |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 1628 | { |
Jamie Madill | 78feddc | 2018-04-27 11:45:05 -0400 | [diff] [blame] | 1629 | return mShaderSerialFactory.generate(); |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 1630 | } |
| 1631 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1632 | angle::Result RendererVk::getDescriptorSetLayout( |
| 1633 | vk::Context *context, |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 1634 | const vk::DescriptorSetLayoutDesc &desc, |
| 1635 | vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut) |
| 1636 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1637 | return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut); |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 1638 | } |
| 1639 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1640 | angle::Result RendererVk::getPipelineLayout( |
| 1641 | vk::Context *context, |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 1642 | const vk::PipelineLayoutDesc &desc, |
| 1643 | const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts, |
| 1644 | vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut) |
| 1645 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1646 | return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts, |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 1647 | pipelineLayoutOut); |
| 1648 | } |
| 1649 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1650 | angle::Result RendererVk::syncPipelineCacheVk(DisplayVk *displayVk) |
| 1651 | { |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 1652 | ASSERT(mPipelineCache.valid()); |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1653 | |
| 1654 | if (--mPipelineCacheVkUpdateTimeout > 0) |
| 1655 | { |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1656 | return angle::Result::Continue; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | mPipelineCacheVkUpdateTimeout = kPipelineCacheVkUpdatePeriod; |
| 1660 | |
| 1661 | // Get the size of the cache. |
| 1662 | size_t pipelineCacheSize = 0; |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 1663 | VkResult result = mPipelineCache.getCacheData(mDevice, &pipelineCacheSize, nullptr); |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1664 | if (result != VK_INCOMPLETE) |
| 1665 | { |
| 1666 | ANGLE_VK_TRY(displayVk, result); |
| 1667 | } |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1668 | |
| 1669 | angle::MemoryBuffer *pipelineCacheData = nullptr; |
| 1670 | ANGLE_VK_CHECK_ALLOC(displayVk, |
| 1671 | displayVk->getScratchBuffer(pipelineCacheSize, &pipelineCacheData)); |
| 1672 | |
| 1673 | size_t originalPipelineCacheSize = pipelineCacheSize; |
Jamie Madill | dc65c5b | 2018-11-21 11:07:26 -0500 | [diff] [blame] | 1674 | result = mPipelineCache.getCacheData(mDevice, &pipelineCacheSize, pipelineCacheData->data()); |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1675 | // Note: currently we don't accept incomplete as we don't expect it (the full size of cache |
| 1676 | // was determined just above), so receiving it hints at an implementation bug we would want |
| 1677 | // to know about early. |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1678 | ASSERT(result != VK_INCOMPLETE); |
| 1679 | ANGLE_VK_TRY(displayVk, result); |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1680 | |
| 1681 | // If vkGetPipelineCacheData ends up writing fewer bytes than requested, zero out the rest of |
| 1682 | // the buffer to avoid leaking garbage memory. |
| 1683 | ASSERT(pipelineCacheSize <= originalPipelineCacheSize); |
| 1684 | if (pipelineCacheSize < originalPipelineCacheSize) |
| 1685 | { |
| 1686 | memset(pipelineCacheData->data() + pipelineCacheSize, 0, |
| 1687 | originalPipelineCacheSize - pipelineCacheSize); |
| 1688 | } |
| 1689 | |
| 1690 | displayVk->getBlobCache()->putApplication(mPipelineCacheVkBlobKey, *pipelineCacheData); |
| 1691 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1692 | return angle::Result::Continue; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame] | 1693 | } |
| 1694 | |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1695 | angle::Result RendererVk::allocateSubmitWaitSemaphore(vk::Context *context, |
| 1696 | const vk::Semaphore **outSemaphore) |
| 1697 | { |
| 1698 | ASSERT(mSubmitWaitSemaphores.size() < mSubmitWaitSemaphores.max_size()); |
| 1699 | |
| 1700 | vk::SemaphoreHelper semaphore; |
| 1701 | ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &semaphore)); |
| 1702 | |
| 1703 | mSubmitWaitSemaphores.push_back(std::move(semaphore)); |
| 1704 | *outSemaphore = mSubmitWaitSemaphores.back().getSemaphore(); |
| 1705 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1706 | return angle::Result::Continue; |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | const vk::Semaphore *RendererVk::getSubmitLastSignaledSemaphore(vk::Context *context) |
| 1710 | { |
| 1711 | const vk::Semaphore *semaphore = mSubmitLastSignaledSemaphore.getSemaphore(); |
| 1712 | |
| 1713 | // Return the semaphore to the pool (which will remain valid and unused until the |
| 1714 | // queue it's about to be waited on has finished execution). The caller is about |
| 1715 | // to wait on it. |
| 1716 | mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore); |
| 1717 | |
| 1718 | return semaphore; |
| 1719 | } |
| 1720 | |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1721 | angle::Result RendererVk::getTimestamp(vk::Context *context, uint64_t *timestampOut) |
| 1722 | { |
| 1723 | // The intent of this function is to query the timestamp without stalling the GPU. Currently, |
| 1724 | // that seems impossible, so instead, we are going to make a small submission with just a |
| 1725 | // timestamp query. First, the disjoint timer query extension says: |
| 1726 | // |
| 1727 | // > This will return the GL time after all previous commands have reached the GL server but |
| 1728 | // have not yet necessarily executed. |
| 1729 | // |
| 1730 | // The previous commands are stored in the command graph at the moment and are not yet flushed. |
| 1731 | // The wording allows us to make a submission to get the timestamp without performing a flush. |
| 1732 | // |
| 1733 | // Second: |
| 1734 | // |
| 1735 | // > By using a combination of this synchronous get command and the asynchronous timestamp query |
| 1736 | // object target, applications can measure the latency between when commands reach the GL server |
| 1737 | // and when they are realized in the framebuffer. |
| 1738 | // |
| 1739 | // This fits with the above strategy as well, although inevitably we are possibly introducing a |
| 1740 | // GPU bubble. This function directly generates a command buffer and submits it instead of |
| 1741 | // using the other member functions. This is to avoid changing any state, such as the queue |
| 1742 | // serial. |
| 1743 | |
| 1744 | // Create a query used to receive the GPU timestamp |
| 1745 | vk::Scoped<vk::DynamicQueryPool> timestampQueryPool(mDevice); |
| 1746 | vk::QueryHelper timestampQuery; |
| 1747 | ANGLE_TRY(timestampQueryPool.get().init(context, VK_QUERY_TYPE_TIMESTAMP, 1)); |
| 1748 | ANGLE_TRY(timestampQueryPool.get().allocateQuery(context, ×tampQuery)); |
| 1749 | |
| 1750 | // Record the command buffer |
| 1751 | vk::Scoped<vk::CommandBuffer> commandBatch(mDevice); |
| 1752 | vk::CommandBuffer &commandBuffer = commandBatch.get(); |
| 1753 | |
| 1754 | VkCommandBufferAllocateInfo commandBufferInfo = {}; |
| 1755 | commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 1756 | commandBufferInfo.commandPool = mCommandPool.getHandle(); |
| 1757 | commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 1758 | commandBufferInfo.commandBufferCount = 1; |
| 1759 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1760 | ANGLE_VK_TRY(context, commandBuffer.init(mDevice, commandBufferInfo)); |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1761 | |
| 1762 | VkCommandBufferBeginInfo beginInfo = {}; |
| 1763 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 1764 | beginInfo.flags = 0; |
| 1765 | beginInfo.pInheritanceInfo = nullptr; |
| 1766 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1767 | ANGLE_VK_TRY(context, commandBuffer.begin(beginInfo)); |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1768 | |
| 1769 | commandBuffer.resetQueryPool(timestampQuery.getQueryPool()->getHandle(), |
| 1770 | timestampQuery.getQuery(), 1); |
| 1771 | commandBuffer.writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, |
| 1772 | timestampQuery.getQueryPool()->getHandle(), |
| 1773 | timestampQuery.getQuery()); |
| 1774 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1775 | ANGLE_VK_TRY(context, commandBuffer.end()); |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1776 | |
| 1777 | // Create fence for the submission |
| 1778 | VkFenceCreateInfo fenceInfo = {}; |
| 1779 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 1780 | fenceInfo.flags = 0; |
| 1781 | |
| 1782 | vk::Scoped<vk::Fence> fence(mDevice); |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1783 | ANGLE_VK_TRY(context, fence.get().init(mDevice, fenceInfo)); |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1784 | |
| 1785 | // Submit the command buffer |
| 1786 | VkSubmitInfo submitInfo = {}; |
| 1787 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 1788 | submitInfo.waitSemaphoreCount = 0; |
| 1789 | submitInfo.pWaitSemaphores = nullptr; |
| 1790 | submitInfo.pWaitDstStageMask = nullptr; |
| 1791 | submitInfo.commandBufferCount = 1; |
| 1792 | submitInfo.pCommandBuffers = commandBuffer.ptr(); |
| 1793 | submitInfo.signalSemaphoreCount = 0; |
| 1794 | submitInfo.pSignalSemaphores = nullptr; |
| 1795 | |
| 1796 | ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, fence.get().getHandle())); |
| 1797 | |
| 1798 | // Wait for the submission to finish. Given no semaphores, there is hope that it would execute |
| 1799 | // in parallel with what's already running on the GPU. |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1800 | ANGLE_VK_TRY(context, fence.get().wait(mDevice, kMaxFenceWaitTimeNs)); |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1801 | |
| 1802 | // Get the query results |
| 1803 | constexpr VkQueryResultFlags queryFlags = VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT; |
| 1804 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1805 | ANGLE_VK_TRY(context, timestampQuery.getQueryPool()->getResults( |
| 1806 | mDevice, timestampQuery.getQuery(), 1, sizeof(*timestampOut), |
| 1807 | timestampOut, sizeof(*timestampOut), queryFlags)); |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1808 | |
| 1809 | timestampQueryPool.get().freeQuery(context, ×tampQuery); |
| 1810 | |
Shahbaz Youssefi | 5904ee3 | 2019-01-25 11:15:16 -0500 | [diff] [blame] | 1811 | // Convert results to nanoseconds. |
| 1812 | *timestampOut = static_cast<uint64_t>( |
| 1813 | *timestampOut * static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod)); |
| 1814 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 1815 | return angle::Result::Continue; |
Shahbaz Youssefi | 749589f | 2018-10-25 12:48:49 -0400 | [diff] [blame] | 1816 | } |
| 1817 | |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 1818 | // These functions look at the mandatory format for support, and fallback to querying the device (if |
| 1819 | // necessary) to test the availability of the bits. |
| 1820 | bool RendererVk::hasLinearTextureFormatFeatureBits(VkFormat format, |
| 1821 | const VkFormatFeatureFlags featureBits) |
| 1822 | { |
| 1823 | return hasFormatFeatureBits<&VkFormatProperties::linearTilingFeatures>(format, featureBits); |
| 1824 | } |
| 1825 | |
| 1826 | bool RendererVk::hasTextureFormatFeatureBits(VkFormat format, |
| 1827 | const VkFormatFeatureFlags featureBits) |
| 1828 | { |
| 1829 | return hasFormatFeatureBits<&VkFormatProperties::optimalTilingFeatures>(format, featureBits); |
| 1830 | } |
| 1831 | |
| 1832 | bool RendererVk::hasBufferFormatFeatureBits(VkFormat format, const VkFormatFeatureFlags featureBits) |
| 1833 | { |
| 1834 | return hasFormatFeatureBits<&VkFormatProperties::bufferFeatures>(format, featureBits); |
| 1835 | } |
| 1836 | |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1837 | angle::Result RendererVk::synchronizeCpuGpuTime(vk::Context *context) |
| 1838 | { |
| 1839 | ASSERT(mGpuEventsEnabled); |
| 1840 | |
| 1841 | angle::PlatformMethods *platform = ANGLEPlatformCurrent(); |
| 1842 | ASSERT(platform); |
| 1843 | |
| 1844 | // To synchronize CPU and GPU times, we need to get the CPU timestamp as close as possible to |
| 1845 | // the GPU timestamp. The process of getting the GPU timestamp is as follows: |
| 1846 | // |
| 1847 | // CPU GPU |
| 1848 | // |
| 1849 | // Record command buffer |
| 1850 | // with timestamp query |
| 1851 | // |
| 1852 | // Submit command buffer |
| 1853 | // |
| 1854 | // Post-submission work Begin execution |
| 1855 | // |
Tobin Ehlis | 5546fb4 | 2019-01-17 12:25:54 -0500 | [diff] [blame] | 1856 | // ???? Write timestamp Tgpu |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1857 | // |
| 1858 | // ???? End execution |
| 1859 | // |
| 1860 | // ???? Return query results |
| 1861 | // |
| 1862 | // ???? |
| 1863 | // |
| 1864 | // Get query results |
| 1865 | // |
| 1866 | // The areas of unknown work (????) on the CPU indicate that the CPU may or may not have |
| 1867 | // finished post-submission work while the GPU is executing in parallel. With no further work, |
| 1868 | // querying CPU timestamps before submission and after getting query results give the bounds to |
| 1869 | // Tgpu, which could be quite large. |
| 1870 | // |
| 1871 | // Using VkEvents, the GPU can be made to wait for the CPU and vice versa, in an effort to |
| 1872 | // reduce this range. This function implements the following procedure: |
| 1873 | // |
| 1874 | // CPU GPU |
| 1875 | // |
| 1876 | // Record command buffer |
| 1877 | // with timestamp query |
| 1878 | // |
| 1879 | // Submit command buffer |
| 1880 | // |
| 1881 | // Post-submission work Begin execution |
| 1882 | // |
| 1883 | // ???? Set Event GPUReady |
| 1884 | // |
| 1885 | // Wait on Event GPUReady Wait on Event CPUReady |
| 1886 | // |
| 1887 | // Get CPU Time Ts Wait on Event CPUReady |
| 1888 | // |
| 1889 | // Set Event CPUReady Wait on Event CPUReady |
| 1890 | // |
| 1891 | // Get CPU Time Tcpu Get GPU Time Tgpu |
| 1892 | // |
| 1893 | // Wait on Event GPUDone Set Event GPUDone |
| 1894 | // |
| 1895 | // Get CPU Time Te End Execution |
| 1896 | // |
| 1897 | // Idle Return query results |
| 1898 | // |
| 1899 | // Get query results |
| 1900 | // |
| 1901 | // If Te-Ts > epsilon, a GPU or CPU interruption can be assumed and the operation can be |
| 1902 | // retried. Once Te-Ts < epsilon, Tcpu can be taken to presumably match Tgpu. Finding an |
| 1903 | // epsilon that's valid for all devices may be difficult, so the loop can be performed only a |
| 1904 | // limited number of times and the Tcpu,Tgpu pair corresponding to smallest Te-Ts used for |
| 1905 | // calibration. |
| 1906 | // |
| 1907 | // Note: Once VK_EXT_calibrated_timestamps is ubiquitous, this should be redone. |
| 1908 | |
| 1909 | // Make sure nothing is running |
| 1910 | ASSERT(mCommandGraph.empty()); |
| 1911 | |
| 1912 | TRACE_EVENT0("gpu.angle", "RendererVk::synchronizeCpuGpuTime"); |
| 1913 | |
| 1914 | // Create a query used to receive the GPU timestamp |
| 1915 | vk::QueryHelper timestampQuery; |
| 1916 | ANGLE_TRY(mGpuEventQueryPool.allocateQuery(context, ×tampQuery)); |
| 1917 | |
| 1918 | // Create the three events |
| 1919 | VkEventCreateInfo eventCreateInfo = {}; |
| 1920 | eventCreateInfo.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO; |
| 1921 | eventCreateInfo.flags = 0; |
| 1922 | |
| 1923 | vk::Scoped<vk::Event> cpuReady(mDevice), gpuReady(mDevice), gpuDone(mDevice); |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1924 | ANGLE_VK_TRY(context, cpuReady.get().init(mDevice, eventCreateInfo)); |
| 1925 | ANGLE_VK_TRY(context, gpuReady.get().init(mDevice, eventCreateInfo)); |
| 1926 | ANGLE_VK_TRY(context, gpuDone.get().init(mDevice, eventCreateInfo)); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1927 | |
| 1928 | constexpr uint32_t kRetries = 10; |
| 1929 | |
| 1930 | // Time suffixes used are S for seconds and Cycles for cycles |
| 1931 | double tightestRangeS = 1e6f; |
| 1932 | double TcpuS = 0; |
| 1933 | uint64_t TgpuCycles = 0; |
| 1934 | for (uint32_t i = 0; i < kRetries; ++i) |
| 1935 | { |
| 1936 | // Reset the events |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1937 | ANGLE_VK_TRY(context, cpuReady.get().reset(mDevice)); |
| 1938 | ANGLE_VK_TRY(context, gpuReady.get().reset(mDevice)); |
| 1939 | ANGLE_VK_TRY(context, gpuDone.get().reset(mDevice)); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1940 | |
| 1941 | // Record the command buffer |
| 1942 | vk::Scoped<vk::CommandBuffer> commandBatch(mDevice); |
| 1943 | vk::CommandBuffer &commandBuffer = commandBatch.get(); |
| 1944 | |
| 1945 | VkCommandBufferAllocateInfo commandBufferInfo = {}; |
| 1946 | commandBufferInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 1947 | commandBufferInfo.commandPool = mCommandPool.getHandle(); |
| 1948 | commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 1949 | commandBufferInfo.commandBufferCount = 1; |
| 1950 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1951 | ANGLE_VK_TRY(context, commandBuffer.init(mDevice, commandBufferInfo)); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1952 | |
| 1953 | VkCommandBufferBeginInfo beginInfo = {}; |
| 1954 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 1955 | beginInfo.flags = 0; |
| 1956 | beginInfo.pInheritanceInfo = nullptr; |
| 1957 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1958 | ANGLE_VK_TRY(context, commandBuffer.begin(beginInfo)); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1959 | |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 1960 | commandBuffer.setEvent(gpuReady.get().getHandle(), VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1961 | commandBuffer.waitEvents(1, cpuReady.get().ptr(), VK_PIPELINE_STAGE_HOST_BIT, |
| 1962 | VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, 0, nullptr, 0, nullptr, 0, |
| 1963 | nullptr); |
| 1964 | |
| 1965 | commandBuffer.resetQueryPool(timestampQuery.getQueryPool()->getHandle(), |
| 1966 | timestampQuery.getQuery(), 1); |
| 1967 | commandBuffer.writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, |
| 1968 | timestampQuery.getQueryPool()->getHandle(), |
| 1969 | timestampQuery.getQuery()); |
| 1970 | |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 1971 | commandBuffer.setEvent(gpuDone.get().getHandle(), VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1972 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1973 | ANGLE_VK_TRY(context, commandBuffer.end()); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1974 | |
| 1975 | // Submit the command buffer |
| 1976 | angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores; |
| 1977 | angle::FixedVector<VkPipelineStageFlags, kMaxWaitSemaphores> waitStageMasks; |
| 1978 | getSubmitWaitSemaphores(context, &waitSemaphores, &waitStageMasks); |
| 1979 | |
| 1980 | VkSubmitInfo submitInfo = {}; |
| 1981 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 1982 | submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size()); |
| 1983 | submitInfo.pWaitSemaphores = waitSemaphores.data(); |
| 1984 | submitInfo.pWaitDstStageMask = waitStageMasks.data(); |
| 1985 | submitInfo.commandBufferCount = 1; |
| 1986 | submitInfo.pCommandBuffers = commandBuffer.ptr(); |
| 1987 | submitInfo.signalSemaphoreCount = 0; |
| 1988 | submitInfo.pSignalSemaphores = nullptr; |
| 1989 | |
| 1990 | ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBuffer))); |
| 1991 | |
| 1992 | // Wait for GPU to be ready. This is a short busy wait. |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1993 | VkResult result = VK_EVENT_RESET; |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 1994 | do |
| 1995 | { |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 1996 | result = gpuReady.get().getStatus(mDevice); |
| 1997 | if (result != VK_EVENT_SET && result != VK_EVENT_RESET) |
| 1998 | { |
| 1999 | ANGLE_VK_TRY(context, result); |
| 2000 | } |
| 2001 | } while (result == VK_EVENT_RESET); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 2002 | |
| 2003 | double TsS = platform->monotonicallyIncreasingTime(platform); |
| 2004 | |
| 2005 | // Tell the GPU to go ahead with the timestamp query. |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 2006 | ANGLE_VK_TRY(context, cpuReady.get().set(mDevice)); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 2007 | double cpuTimestampS = platform->monotonicallyIncreasingTime(platform); |
| 2008 | |
| 2009 | // Wait for GPU to be done. Another short busy wait. |
| 2010 | do |
| 2011 | { |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 2012 | result = gpuDone.get().getStatus(mDevice); |
| 2013 | if (result != VK_EVENT_SET && result != VK_EVENT_RESET) |
| 2014 | { |
| 2015 | ANGLE_VK_TRY(context, result); |
| 2016 | } |
| 2017 | } while (result == VK_EVENT_RESET); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 2018 | |
| 2019 | double TeS = platform->monotonicallyIncreasingTime(platform); |
| 2020 | |
| 2021 | // Get the query results |
| 2022 | ANGLE_TRY(finishToSerial(context, getLastSubmittedQueueSerial())); |
| 2023 | |
| 2024 | constexpr VkQueryResultFlags queryFlags = VK_QUERY_RESULT_WAIT_BIT | VK_QUERY_RESULT_64_BIT; |
| 2025 | |
| 2026 | uint64_t gpuTimestampCycles = 0; |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 2027 | ANGLE_VK_TRY(context, timestampQuery.getQueryPool()->getResults( |
| 2028 | mDevice, timestampQuery.getQuery(), 1, sizeof(gpuTimestampCycles), |
| 2029 | &gpuTimestampCycles, sizeof(gpuTimestampCycles), queryFlags)); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 2030 | |
| 2031 | // Use the first timestamp queried as origin. |
| 2032 | if (mGpuEventTimestampOrigin == 0) |
| 2033 | { |
| 2034 | mGpuEventTimestampOrigin = gpuTimestampCycles; |
| 2035 | } |
| 2036 | |
| 2037 | // Take these CPU and GPU timestamps if there is better confidence. |
| 2038 | double confidenceRangeS = TeS - TsS; |
| 2039 | if (confidenceRangeS < tightestRangeS) |
| 2040 | { |
| 2041 | tightestRangeS = confidenceRangeS; |
| 2042 | TcpuS = cpuTimestampS; |
| 2043 | TgpuCycles = gpuTimestampCycles; |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | mGpuEventQueryPool.freeQuery(context, ×tampQuery); |
| 2048 | |
| 2049 | // timestampPeriod gives nanoseconds/cycle. |
| 2050 | double TgpuS = (TgpuCycles - mGpuEventTimestampOrigin) * |
| 2051 | static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod) / |
| 2052 | 1'000'000'000.0; |
| 2053 | |
| 2054 | flushGpuEvents(TgpuS, TcpuS); |
| 2055 | |
| 2056 | mGpuClockSync.gpuTimestampS = TgpuS; |
| 2057 | mGpuClockSync.cpuTimestampS = TcpuS; |
| 2058 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 2059 | return angle::Result::Continue; |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | angle::Result RendererVk::traceGpuEventImpl(vk::Context *context, |
| 2063 | vk::CommandBuffer *commandBuffer, |
| 2064 | char phase, |
| 2065 | const char *name) |
| 2066 | { |
| 2067 | ASSERT(mGpuEventsEnabled); |
| 2068 | |
| 2069 | GpuEventQuery event; |
| 2070 | |
| 2071 | event.name = name; |
| 2072 | event.phase = phase; |
| 2073 | event.serial = mCurrentQueueSerial; |
| 2074 | |
| 2075 | ANGLE_TRY(mGpuEventQueryPool.allocateQuery(context, &event.queryPoolIndex, &event.queryIndex)); |
| 2076 | |
| 2077 | commandBuffer->resetQueryPool( |
| 2078 | mGpuEventQueryPool.getQueryPool(event.queryPoolIndex)->getHandle(), event.queryIndex, 1); |
| 2079 | commandBuffer->writeTimestamp( |
| 2080 | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, |
| 2081 | mGpuEventQueryPool.getQueryPool(event.queryPoolIndex)->getHandle(), event.queryIndex); |
| 2082 | |
| 2083 | mInFlightGpuEventQueries.push_back(std::move(event)); |
| 2084 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 2085 | return angle::Result::Continue; |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | angle::Result RendererVk::checkCompletedGpuEvents(vk::Context *context) |
| 2089 | { |
| 2090 | ASSERT(mGpuEventsEnabled); |
| 2091 | |
| 2092 | angle::PlatformMethods *platform = ANGLEPlatformCurrent(); |
| 2093 | ASSERT(platform); |
| 2094 | |
| 2095 | int finishedCount = 0; |
| 2096 | |
| 2097 | for (GpuEventQuery &eventQuery : mInFlightGpuEventQueries) |
| 2098 | { |
| 2099 | // Only check the timestamp query if the submission has finished. |
| 2100 | if (eventQuery.serial > mLastCompletedQueueSerial) |
| 2101 | { |
| 2102 | break; |
| 2103 | } |
| 2104 | |
| 2105 | // See if the results are available. |
| 2106 | uint64_t gpuTimestampCycles = 0; |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 2107 | VkResult result = mGpuEventQueryPool.getQueryPool(eventQuery.queryPoolIndex) |
| 2108 | ->getResults(mDevice, eventQuery.queryIndex, 1, |
| 2109 | sizeof(gpuTimestampCycles), &gpuTimestampCycles, |
| 2110 | sizeof(gpuTimestampCycles), VK_QUERY_RESULT_64_BIT); |
| 2111 | if (result == VK_NOT_READY) |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 2112 | { |
| 2113 | break; |
| 2114 | } |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 2115 | ANGLE_VK_TRY(context, result); |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 2116 | |
| 2117 | mGpuEventQueryPool.freeQuery(context, eventQuery.queryPoolIndex, eventQuery.queryIndex); |
| 2118 | |
| 2119 | GpuEvent event; |
| 2120 | event.gpuTimestampCycles = gpuTimestampCycles; |
| 2121 | event.name = eventQuery.name; |
| 2122 | event.phase = eventQuery.phase; |
| 2123 | |
| 2124 | mGpuEvents.emplace_back(event); |
| 2125 | |
| 2126 | ++finishedCount; |
| 2127 | } |
| 2128 | |
| 2129 | mInFlightGpuEventQueries.erase(mInFlightGpuEventQueries.begin(), |
| 2130 | mInFlightGpuEventQueries.begin() + finishedCount); |
| 2131 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 2132 | return angle::Result::Continue; |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | void RendererVk::flushGpuEvents(double nextSyncGpuTimestampS, double nextSyncCpuTimestampS) |
| 2136 | { |
| 2137 | if (mGpuEvents.size() == 0) |
| 2138 | { |
| 2139 | return; |
| 2140 | } |
| 2141 | |
| 2142 | angle::PlatformMethods *platform = ANGLEPlatformCurrent(); |
| 2143 | ASSERT(platform); |
| 2144 | |
| 2145 | // Find the slope of the clock drift for adjustment |
| 2146 | double lastGpuSyncTimeS = mGpuClockSync.gpuTimestampS; |
| 2147 | double lastGpuSyncDiffS = mGpuClockSync.cpuTimestampS - mGpuClockSync.gpuTimestampS; |
| 2148 | double gpuSyncDriftSlope = 0; |
| 2149 | |
| 2150 | double nextGpuSyncTimeS = nextSyncGpuTimestampS; |
| 2151 | double nextGpuSyncDiffS = nextSyncCpuTimestampS - nextSyncGpuTimestampS; |
| 2152 | |
| 2153 | // No gpu trace events should have been generated before the clock sync, so if there is no |
| 2154 | // "previous" clock sync, there should be no gpu events (i.e. the function early-outs above). |
| 2155 | ASSERT(mGpuClockSync.gpuTimestampS != std::numeric_limits<double>::max() && |
| 2156 | mGpuClockSync.cpuTimestampS != std::numeric_limits<double>::max()); |
| 2157 | |
| 2158 | gpuSyncDriftSlope = |
| 2159 | (nextGpuSyncDiffS - lastGpuSyncDiffS) / (nextGpuSyncTimeS - lastGpuSyncTimeS); |
| 2160 | |
| 2161 | for (const GpuEvent &event : mGpuEvents) |
| 2162 | { |
| 2163 | double gpuTimestampS = |
| 2164 | (event.gpuTimestampCycles - mGpuEventTimestampOrigin) * |
| 2165 | static_cast<double>(mPhysicalDeviceProperties.limits.timestampPeriod) * 1e-9; |
| 2166 | |
| 2167 | // Account for clock drift. |
| 2168 | gpuTimestampS += lastGpuSyncDiffS + gpuSyncDriftSlope * (gpuTimestampS - lastGpuSyncTimeS); |
| 2169 | |
| 2170 | // Generate the trace now that the GPU timestamp is available and clock drifts are accounted |
| 2171 | // for. |
| 2172 | static long long eventId = 1; |
| 2173 | static const unsigned char *categoryEnabled = |
| 2174 | TRACE_EVENT_API_GET_CATEGORY_ENABLED("gpu.angle.gpu"); |
| 2175 | platform->addTraceEvent(platform, event.phase, categoryEnabled, event.name, eventId++, |
| 2176 | gpuTimestampS, 0, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_NONE); |
| 2177 | } |
| 2178 | |
| 2179 | mGpuEvents.clear(); |
| 2180 | } |
| 2181 | |
Shahbaz Youssefi | 96bd8fd | 2018-11-30 14:30:18 -0500 | [diff] [blame] | 2182 | template <VkFormatFeatureFlags VkFormatProperties::*features> |
| 2183 | bool RendererVk::hasFormatFeatureBits(VkFormat format, const VkFormatFeatureFlags featureBits) |
| 2184 | { |
| 2185 | ASSERT(static_cast<uint32_t>(format) < vk::kNumVkFormats); |
| 2186 | VkFormatProperties &deviceProperties = mFormatProperties[format]; |
| 2187 | |
| 2188 | if (deviceProperties.bufferFeatures == kInvalidFormatFeatureFlags) |
| 2189 | { |
| 2190 | // If we don't have the actual device features, see if the requested features are mandatory. |
| 2191 | // If so, there's no need to query the device. |
| 2192 | const VkFormatProperties &mandatoryProperties = vk::GetMandatoryFormatSupport(format); |
| 2193 | if (IsMaskFlagSet(mandatoryProperties.*features, featureBits)) |
| 2194 | { |
| 2195 | return true; |
| 2196 | } |
| 2197 | |
| 2198 | // Otherwise query the format features and cache it. |
| 2199 | vkGetPhysicalDeviceFormatProperties(mPhysicalDevice, format, &deviceProperties); |
| 2200 | } |
| 2201 | |
| 2202 | return IsMaskFlagSet(deviceProperties.*features, featureBits); |
| 2203 | } |
| 2204 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 2205 | uint32_t GetUniformBufferDescriptorCount() |
| 2206 | { |
| 2207 | return kUniformBufferDescriptorsPerDescriptorSet; |
| 2208 | } |
| 2209 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 2210 | } // namespace rx |