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