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