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