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