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