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