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