Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | // RendererVk.cpp: |
| 7 | // Implements the class methods for RendererVk. |
| 8 | // |
| 9 | |
| 10 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
| 11 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 12 | // Placing this first seems to solve an intellisense bug. |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 13 | #include "libANGLE/renderer/vulkan/vk_utils.h" |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 14 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 15 | #include <EGL/eglext.h> |
| 16 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 17 | #include "common/debug.h" |
Jamie Madill | a66779f | 2017-01-06 10:43:44 -0500 | [diff] [blame] | 18 | #include "common/system_utils.h" |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 19 | #include "libANGLE/renderer/driver_utils.h" |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 20 | #include "libANGLE/renderer/vulkan/CommandGraph.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 21 | #include "libANGLE/renderer/vulkan/CompilerVk.h" |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 22 | #include "libANGLE/renderer/vulkan/DisplayVk.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 23 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 24 | #include "libANGLE/renderer/vulkan/GlslangWrapper.h" |
Jamie Madill | ffa4cbb | 2018-01-23 13:04:07 -0500 | [diff] [blame] | 25 | #include "libANGLE/renderer/vulkan/ProgramVk.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 26 | #include "libANGLE/renderer/vulkan/VertexArrayVk.h" |
Luc Ferron | e4741fd | 2018-01-25 13:25:27 -0500 | [diff] [blame] | 27 | #include "libANGLE/renderer/vulkan/vk_caps_utils.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 28 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 29 | #include "platform/Platform.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 30 | |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 31 | // Consts |
| 32 | namespace |
| 33 | { |
| 34 | const uint32_t kMockVendorID = 0xba5eba11; |
| 35 | const uint32_t kMockDeviceID = 0xf005ba11; |
| 36 | constexpr char kMockDeviceName[] = "Vulkan Mock Device"; |
Omar El Sheikh | 0359b69 | 2018-07-23 17:22:00 -0600 | [diff] [blame] | 37 | constexpr size_t kInFlightCommandsLimit = 50000u; |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 38 | } // anonymous namespace |
| 39 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 40 | namespace rx |
| 41 | { |
| 42 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 43 | namespace |
| 44 | { |
Luc Ferron | daedf4d | 2018-03-16 09:28:53 -0400 | [diff] [blame] | 45 | // We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and |
| 46 | // one for the vertex shader. |
| 47 | constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 48 | // Update the pipeline cache every this many swaps (if 60fps, this means every 10 minutes) |
| 49 | static constexpr uint32_t kPipelineCacheVkUpdatePeriod = 10 * 60 * 60; |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 50 | |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 51 | bool ShouldEnableMockICD(const egl::AttributeMap &attribs) |
| 52 | { |
| 53 | #if !defined(ANGLE_PLATFORM_ANDROID) |
| 54 | // Mock ICD does not currently run on Android |
| 55 | return (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, |
| 56 | EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) == |
| 57 | EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE); |
| 58 | #else |
| 59 | return false; |
| 60 | #endif // !defined(ANGLE_PLATFORM_ANDROID) |
| 61 | } |
| 62 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 63 | VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps, |
| 64 | const std::vector<const char *> &enabledExtensionNames) |
| 65 | { |
| 66 | // Compile the extensions names into a set. |
| 67 | std::set<std::string> extensionNames; |
| 68 | for (const auto &extensionProp : extensionProps) |
| 69 | { |
| 70 | extensionNames.insert(extensionProp.extensionName); |
| 71 | } |
| 72 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 73 | for (const char *extensionName : enabledExtensionNames) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 74 | { |
| 75 | if (extensionNames.count(extensionName) == 0) |
| 76 | { |
| 77 | return VK_ERROR_EXTENSION_NOT_PRESENT; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return VK_SUCCESS; |
| 82 | } |
| 83 | |
Tobin Ehlis | 3a181e3 | 2018-08-29 15:17:05 -0600 | [diff] [blame] | 84 | // Array of Validation error/warning messages that will be ignored, should include bugID |
| 85 | constexpr std::array<const char *, 1> kSkippedMessages = { |
| 86 | // http://anglebug.com/2796 |
| 87 | " [ UNASSIGNED-CoreValidation-Shader-PointSizeMissing ] Object: VK_NULL_HANDLE (Type = 19) " |
| 88 | "| Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader " |
| 89 | "corresponding to VK_SHADER_STAGE_VERTEX_BIT."}; |
| 90 | |
| 91 | // Suppress validation errors that are known |
| 92 | // return "true" if given code/prefix/message is known, else return "false" |
| 93 | bool IsIgnoredDebugMessage(const char *message) |
| 94 | { |
| 95 | for (const auto &msg : kSkippedMessages) |
| 96 | { |
| 97 | if (strcmp(msg, message) == 0) |
| 98 | { |
| 99 | return true; |
| 100 | } |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 105 | VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags, |
| 106 | VkDebugReportObjectTypeEXT objectType, |
| 107 | uint64_t object, |
| 108 | size_t location, |
| 109 | int32_t messageCode, |
| 110 | const char *layerPrefix, |
| 111 | const char *message, |
| 112 | void *userData) |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 113 | { |
Tobin Ehlis | 3a181e3 | 2018-08-29 15:17:05 -0600 | [diff] [blame] | 114 | if (IsIgnoredDebugMessage(message)) |
| 115 | { |
| 116 | return VK_FALSE; |
| 117 | } |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 118 | if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0) |
| 119 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 120 | ERR() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 121 | #if !defined(NDEBUG) |
| 122 | // Abort the call in Debug builds. |
| 123 | return VK_TRUE; |
| 124 | #endif |
| 125 | } |
| 126 | else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0) |
| 127 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 128 | WARN() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 129 | } |
| 130 | else |
| 131 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 132 | // Uncomment this if you want Vulkan spam. |
| 133 | // WARN() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | return VK_FALSE; |
| 137 | } |
| 138 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 139 | // If we're loading the validation layers, we could be running from any random directory. |
| 140 | // Change to the executable directory so we can find the layers, then change back to the |
| 141 | // previous directory to be safe we don't disrupt the application. |
| 142 | class ScopedVkLoaderEnvironment : angle::NonCopyable |
| 143 | { |
| 144 | public: |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 145 | ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD) |
| 146 | : mEnableValidationLayers(enableValidationLayers), |
| 147 | mEnableMockICD(enableMockICD), |
| 148 | mChangedCWD(false), |
| 149 | mChangedICDPath(false) |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 150 | { |
| 151 | // Changing CWD and setting environment variables makes no sense on Android, |
| 152 | // since this code is a part of Java application there. |
| 153 | // Android Vulkan loader doesn't need this either. |
| 154 | #if !defined(ANGLE_PLATFORM_ANDROID) |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 155 | if (enableMockICD) |
| 156 | { |
| 157 | // Override environment variable to use built Mock ICD |
| 158 | // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn |
| 159 | mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv); |
| 160 | mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON); |
| 161 | if (!mChangedICDPath) |
| 162 | { |
| 163 | ERR() << "Error setting Path for Mock/Null Driver."; |
| 164 | mEnableMockICD = false; |
| 165 | } |
| 166 | } |
Jamie Madill | 4684842 | 2018-08-09 10:46:06 -0400 | [diff] [blame] | 167 | if (mEnableValidationLayers || mEnableMockICD) |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 168 | { |
| 169 | const auto &cwd = angle::GetCWD(); |
| 170 | if (!cwd.valid()) |
| 171 | { |
| 172 | ERR() << "Error getting CWD for Vulkan layers init."; |
| 173 | mEnableValidationLayers = false; |
Jamie Madill | 4684842 | 2018-08-09 10:46:06 -0400 | [diff] [blame] | 174 | mEnableMockICD = false; |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 175 | } |
| 176 | else |
| 177 | { |
| 178 | mPreviousCWD = cwd.value(); |
| 179 | const char *exeDir = angle::GetExecutableDirectory(); |
| 180 | mChangedCWD = angle::SetCWD(exeDir); |
| 181 | if (!mChangedCWD) |
| 182 | { |
| 183 | ERR() << "Error setting CWD for Vulkan layers init."; |
| 184 | mEnableValidationLayers = false; |
Jamie Madill | 4684842 | 2018-08-09 10:46:06 -0400 | [diff] [blame] | 185 | mEnableMockICD = false; |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Override environment variable to use the ANGLE layers. |
| 191 | if (mEnableValidationLayers) |
| 192 | { |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 193 | if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR)) |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 194 | { |
| 195 | ERR() << "Error setting environment for Vulkan layers init."; |
| 196 | mEnableValidationLayers = false; |
| 197 | } |
| 198 | } |
| 199 | #endif // !defined(ANGLE_PLATFORM_ANDROID) |
| 200 | } |
| 201 | |
| 202 | ~ScopedVkLoaderEnvironment() |
| 203 | { |
| 204 | if (mChangedCWD) |
| 205 | { |
| 206 | #if !defined(ANGLE_PLATFORM_ANDROID) |
| 207 | ASSERT(mPreviousCWD.valid()); |
| 208 | angle::SetCWD(mPreviousCWD.value().c_str()); |
| 209 | #endif // !defined(ANGLE_PLATFORM_ANDROID) |
| 210 | } |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 211 | if (mChangedICDPath) |
| 212 | { |
Omar El Sheikh | 80d4ef1 | 2018-07-13 17:08:19 -0600 | [diff] [blame] | 213 | if (mPreviousICDPath.value().empty()) |
| 214 | { |
| 215 | angle::UnsetEnvironmentVar(g_VkICDPathEnv); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str()); |
| 220 | } |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 221 | } |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 224 | bool canEnableValidationLayers() const { return mEnableValidationLayers; } |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 225 | |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 226 | bool canEnableMockICD() const { return mEnableMockICD; } |
| 227 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 228 | private: |
| 229 | bool mEnableValidationLayers; |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 230 | bool mEnableMockICD; |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 231 | bool mChangedCWD; |
| 232 | Optional<std::string> mPreviousCWD; |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 233 | bool mChangedICDPath; |
| 234 | Optional<std::string> mPreviousICDPath; |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 235 | }; |
| 236 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 237 | void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices, |
| 238 | bool preferMockICD, |
| 239 | VkPhysicalDevice *physicalDeviceOut, |
| 240 | VkPhysicalDeviceProperties *physicalDevicePropertiesOut) |
| 241 | { |
| 242 | ASSERT(!physicalDevices.empty()); |
| 243 | if (preferMockICD) |
| 244 | { |
| 245 | for (const VkPhysicalDevice &physicalDevice : physicalDevices) |
| 246 | { |
| 247 | vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut); |
| 248 | if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) && |
| 249 | (kMockDeviceID == physicalDevicePropertiesOut->deviceID) && |
| 250 | (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0)) |
| 251 | { |
| 252 | *physicalDeviceOut = physicalDevice; |
| 253 | return; |
| 254 | } |
| 255 | } |
| 256 | WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default " |
| 257 | "physicalDevice instead."; |
| 258 | } |
| 259 | |
| 260 | // Fall back to first device. |
| 261 | *physicalDeviceOut = physicalDevices[0]; |
| 262 | vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut); |
| 263 | } |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 264 | } // anonymous namespace |
| 265 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 266 | // CommandBatch implementation. |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 267 | RendererVk::CommandBatch::CommandBatch() = default; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 268 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 269 | RendererVk::CommandBatch::~CommandBatch() = default; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 270 | |
| 271 | RendererVk::CommandBatch::CommandBatch(CommandBatch &&other) |
| 272 | : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial) |
| 273 | { |
| 274 | } |
| 275 | |
| 276 | RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other) |
| 277 | { |
| 278 | std::swap(commandPool, other.commandPool); |
| 279 | std::swap(fence, other.fence); |
| 280 | std::swap(serial, other.serial); |
| 281 | return *this; |
| 282 | } |
| 283 | |
Jamie Madill | bea35a6 | 2018-07-05 11:54:10 -0400 | [diff] [blame] | 284 | void RendererVk::CommandBatch::destroy(VkDevice device) |
| 285 | { |
| 286 | commandPool.destroy(device); |
| 287 | fence.destroy(device); |
| 288 | } |
| 289 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 290 | // RendererVk implementation. |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 291 | RendererVk::RendererVk() |
| 292 | : mCapsInitialized(false), |
| 293 | mInstance(VK_NULL_HANDLE), |
| 294 | mEnableValidationLayers(false), |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 295 | mDebugReportCallback(VK_NULL_HANDLE), |
| 296 | mPhysicalDevice(VK_NULL_HANDLE), |
| 297 | mQueue(VK_NULL_HANDLE), |
| 298 | mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()), |
| 299 | mDevice(VK_NULL_HANDLE), |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 300 | mLastCompletedQueueSerial(mQueueSerialFactory.generate()), |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 301 | mCurrentQueueSerial(mQueueSerialFactory.generate()), |
| 302 | mPipelineCacheVkUpdateTimeout(kPipelineCacheVkUpdatePeriod) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 303 | { |
| 304 | } |
| 305 | |
| 306 | RendererVk::~RendererVk() |
| 307 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void RendererVk::onDestroy(vk::Context *context) |
| 311 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 312 | if (!mInFlightCommands.empty() || !mGarbage.empty()) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 313 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 314 | // 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] | 315 | (void)finish(context); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 316 | } |
| 317 | |
Jamie Madill | c7918ce | 2018-06-13 13:25:31 -0400 | [diff] [blame] | 318 | mPipelineLayoutCache.destroy(mDevice); |
| 319 | mDescriptorSetLayoutCache.destroy(mDevice); |
| 320 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 321 | mRenderPassCache.destroy(mDevice); |
Jamie Madill | ffa4cbb | 2018-01-23 13:04:07 -0500 | [diff] [blame] | 322 | mPipelineCache.destroy(mDevice); |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 323 | mPipelineCacheVk.destroy(mDevice); |
Jamie Madill | d47044a | 2018-04-27 11:45:03 -0400 | [diff] [blame] | 324 | mShaderLibrary.destroy(mDevice); |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 325 | |
Jamie Madill | 06ca634 | 2018-07-12 15:56:53 -0400 | [diff] [blame] | 326 | GlslangWrapper::Release(); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 327 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 328 | if (mCommandPool.valid()) |
| 329 | { |
| 330 | mCommandPool.destroy(mDevice); |
| 331 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 332 | |
| 333 | if (mDevice) |
| 334 | { |
| 335 | vkDestroyDevice(mDevice, nullptr); |
| 336 | mDevice = VK_NULL_HANDLE; |
| 337 | } |
| 338 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 339 | if (mDebugReportCallback) |
| 340 | { |
| 341 | ASSERT(mInstance); |
| 342 | auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( |
| 343 | vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT")); |
| 344 | ASSERT(destroyDebugReportCallback); |
| 345 | destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr); |
| 346 | } |
| 347 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 348 | if (mInstance) |
| 349 | { |
| 350 | vkDestroyInstance(mInstance, nullptr); |
| 351 | mInstance = VK_NULL_HANDLE; |
| 352 | } |
| 353 | |
Omar El Sheikh | eb4b869 | 2018-07-17 10:55:40 -0600 | [diff] [blame] | 354 | mMemoryProperties.destroy(); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 355 | mPhysicalDevice = VK_NULL_HANDLE; |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 356 | } |
| 357 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 358 | angle::Result RendererVk::initialize(DisplayVk *displayVk, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 359 | const egl::AttributeMap &attribs, |
| 360 | const char *wsiName) |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 361 | { |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 362 | ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs), |
| 363 | ShouldEnableMockICD(attribs)); |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 364 | mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers(); |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 365 | bool enableMockICD = scopedEnvironment.canEnableMockICD(); |
Jamie Madill | a66779f | 2017-01-06 10:43:44 -0500 | [diff] [blame] | 366 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 367 | // Gather global layer properties. |
| 368 | uint32_t instanceLayerCount = 0; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 369 | ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr)); |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 370 | |
| 371 | std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount); |
| 372 | if (instanceLayerCount > 0) |
| 373 | { |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 374 | ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, |
| 375 | instanceLayerProps.data())); |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 376 | } |
| 377 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 378 | uint32_t instanceExtensionCount = 0; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 379 | ANGLE_VK_TRY(displayVk, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 380 | vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr)); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 381 | |
| 382 | std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount); |
| 383 | if (instanceExtensionCount > 0) |
| 384 | { |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 385 | ANGLE_VK_TRY(displayVk, |
| 386 | vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, |
| 387 | instanceExtensionProps.data())); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 388 | } |
| 389 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 390 | const char *const *enabledLayerNames = nullptr; |
| 391 | uint32_t enabledLayerCount = 0; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 392 | if (mEnableValidationLayers) |
| 393 | { |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 394 | bool layersRequested = |
| 395 | (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE); |
| 396 | mEnableValidationLayers = GetAvailableValidationLayers( |
| 397 | instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount); |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 398 | } |
| 399 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 400 | std::vector<const char *> enabledInstanceExtensions; |
| 401 | enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 402 | enabledInstanceExtensions.push_back(wsiName); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 403 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 404 | // TODO(jmadill): Should be able to continue initialization if debug report ext missing. |
| 405 | if (mEnableValidationLayers) |
| 406 | { |
| 407 | enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); |
| 408 | } |
| 409 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 410 | // 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^] | 411 | ANGLE_VK_TRY(displayVk, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 412 | VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions)); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 413 | |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 414 | VkApplicationInfo applicationInfo; |
| 415 | applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; |
| 416 | applicationInfo.pNext = nullptr; |
| 417 | applicationInfo.pApplicationName = "ANGLE"; |
| 418 | applicationInfo.applicationVersion = 1; |
| 419 | applicationInfo.pEngineName = "ANGLE"; |
| 420 | applicationInfo.engineVersion = 1; |
| 421 | applicationInfo.apiVersion = VK_API_VERSION_1_0; |
| 422 | |
| 423 | VkInstanceCreateInfo instanceInfo; |
| 424 | instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; |
| 425 | instanceInfo.pNext = nullptr; |
| 426 | instanceInfo.flags = 0; |
| 427 | instanceInfo.pApplicationInfo = &applicationInfo; |
| 428 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 429 | // Enable requested layers and extensions. |
| 430 | instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size()); |
| 431 | instanceInfo.ppEnabledExtensionNames = |
| 432 | enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data(); |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 433 | instanceInfo.enabledLayerCount = enabledLayerCount; |
| 434 | instanceInfo.ppEnabledLayerNames = enabledLayerNames; |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 435 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 436 | ANGLE_VK_TRY(displayVk, vkCreateInstance(&instanceInfo, nullptr, &mInstance)); |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 437 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 438 | if (mEnableValidationLayers) |
| 439 | { |
| 440 | VkDebugReportCallbackCreateInfoEXT debugReportInfo; |
| 441 | |
| 442 | debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
| 443 | debugReportInfo.pNext = nullptr; |
| 444 | debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | |
| 445 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT | |
| 446 | VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT; |
| 447 | debugReportInfo.pfnCallback = &DebugReportCallback; |
| 448 | debugReportInfo.pUserData = this; |
| 449 | |
| 450 | auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>( |
| 451 | vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT")); |
| 452 | ASSERT(createDebugReportCallback); |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 453 | ANGLE_VK_TRY(displayVk, createDebugReportCallback(mInstance, &debugReportInfo, nullptr, |
| 454 | &mDebugReportCallback)); |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 455 | } |
| 456 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 457 | uint32_t physicalDeviceCount = 0; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 458 | ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr)); |
| 459 | ANGLE_VK_CHECK(displayVk, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 460 | |
| 461 | // TODO(jmadill): Handle multiple physical devices. For now, use the first device. |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 462 | std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount); |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 463 | ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, |
| 464 | physicalDevices.data())); |
Omar El Sheikh | 26c61b2 | 2018-06-29 12:50:59 -0600 | [diff] [blame] | 465 | ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice, |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 466 | &mPhysicalDeviceProperties); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 467 | |
Jamie Madill | 30b5d84 | 2018-08-31 17:19:12 -0400 | [diff] [blame] | 468 | vkGetPhysicalDeviceFeatures(mPhysicalDevice, &mPhysicalDeviceFeatures); |
| 469 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 470 | // Ensure we can find a graphics queue family. |
| 471 | uint32_t queueCount = 0; |
| 472 | vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr); |
| 473 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 474 | ANGLE_VK_CHECK(displayVk, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 475 | |
| 476 | mQueueFamilyProperties.resize(queueCount); |
| 477 | vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, |
| 478 | mQueueFamilyProperties.data()); |
| 479 | |
| 480 | size_t graphicsQueueFamilyCount = false; |
| 481 | uint32_t firstGraphicsQueueFamily = 0; |
| 482 | for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex) |
| 483 | { |
| 484 | const auto &queueInfo = mQueueFamilyProperties[familyIndex]; |
| 485 | if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) |
| 486 | { |
| 487 | ASSERT(queueInfo.queueCount > 0); |
| 488 | graphicsQueueFamilyCount++; |
| 489 | if (firstGraphicsQueueFamily == 0) |
| 490 | { |
| 491 | firstGraphicsQueueFamily = familyIndex; |
| 492 | } |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 497 | ANGLE_VK_CHECK(displayVk, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 498 | |
Jamie Madill | 1222207 | 2018-07-11 14:59:48 -0400 | [diff] [blame] | 499 | initFeatures(); |
| 500 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 501 | // If only one queue family, go ahead and initialize the device. If there is more than one |
| 502 | // queue, we'll have to wait until we see a WindowSurface to know which supports present. |
| 503 | if (graphicsQueueFamilyCount == 1) |
| 504 | { |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 505 | ANGLE_TRY(initializeDevice(displayVk, firstGraphicsQueueFamily)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 506 | } |
| 507 | |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 508 | // Store the physical device memory properties so we can find the right memory pools. |
| 509 | mMemoryProperties.init(mPhysicalDevice); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 510 | |
Jamie Madill | 06ca634 | 2018-07-12 15:56:53 -0400 | [diff] [blame] | 511 | GlslangWrapper::Initialize(); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 512 | |
Jamie Madill | 6a89d22 | 2017-11-02 11:59:51 -0400 | [diff] [blame] | 513 | // Initialize the format table. |
Luc Ferron | d50537a | 2018-02-07 17:02:08 -0500 | [diff] [blame] | 514 | mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps, |
| 515 | &mNativeCaps.compressedTextureFormats); |
Jamie Madill | 6a89d22 | 2017-11-02 11:59:51 -0400 | [diff] [blame] | 516 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 517 | return angle::Result::Continue(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 520 | angle::Result RendererVk::initializeDevice(DisplayVk *displayVk, uint32_t queueFamilyIndex) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 521 | { |
| 522 | uint32_t deviceLayerCount = 0; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 523 | ANGLE_VK_TRY(displayVk, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 524 | vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 525 | |
| 526 | std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount); |
| 527 | if (deviceLayerCount > 0) |
| 528 | { |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 529 | ANGLE_VK_TRY(displayVk, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, |
| 530 | deviceLayerProps.data())); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | uint32_t deviceExtensionCount = 0; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 534 | ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, |
| 535 | &deviceExtensionCount, nullptr)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 536 | |
| 537 | std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount); |
| 538 | if (deviceExtensionCount > 0) |
| 539 | { |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 540 | ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, |
| 541 | &deviceExtensionCount, |
| 542 | deviceExtensionProps.data())); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 543 | } |
| 544 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 545 | const char *const *enabledLayerNames = nullptr; |
| 546 | uint32_t enabledLayerCount = 0; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 547 | if (mEnableValidationLayers) |
| 548 | { |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 549 | mEnableValidationLayers = GetAvailableValidationLayers( |
| 550 | deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | std::vector<const char *> enabledDeviceExtensions; |
| 554 | enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); |
| 555 | |
Luc Ferron | bf6dc37 | 2018-06-28 15:24:19 -0400 | [diff] [blame] | 556 | // Selectively enable KHR_MAINTENANCE1 to support viewport flipping. |
| 557 | if (getFeatures().flipViewportY) |
| 558 | { |
| 559 | enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME); |
| 560 | } |
| 561 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 562 | ANGLE_VK_TRY(displayVk, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 563 | |
| 564 | VkDeviceQueueCreateInfo queueCreateInfo; |
| 565 | |
| 566 | float zeroPriority = 0.0f; |
| 567 | |
| 568 | queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 569 | queueCreateInfo.pNext = nullptr; |
| 570 | queueCreateInfo.flags = 0; |
| 571 | queueCreateInfo.queueFamilyIndex = queueFamilyIndex; |
| 572 | queueCreateInfo.queueCount = 1; |
| 573 | queueCreateInfo.pQueuePriorities = &zeroPriority; |
| 574 | |
| 575 | // Initialize the device |
| 576 | VkDeviceCreateInfo createInfo; |
| 577 | |
Jamie Madill | 50cf2be | 2018-06-15 09:46:57 -0400 | [diff] [blame] | 578 | createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; |
| 579 | createInfo.pNext = nullptr; |
| 580 | createInfo.flags = 0; |
| 581 | createInfo.queueCreateInfoCount = 1; |
| 582 | createInfo.pQueueCreateInfos = &queueCreateInfo; |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 583 | createInfo.enabledLayerCount = enabledLayerCount; |
| 584 | createInfo.ppEnabledLayerNames = enabledLayerNames; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 585 | createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size()); |
| 586 | createInfo.ppEnabledExtensionNames = |
| 587 | enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data(); |
| 588 | createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features |
| 589 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 590 | ANGLE_VK_TRY(displayVk, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 591 | |
| 592 | mCurrentQueueFamilyIndex = queueFamilyIndex; |
| 593 | |
| 594 | vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue); |
| 595 | |
| 596 | // Initialize the command pool now that we know the queue family index. |
| 597 | VkCommandPoolCreateInfo commandPoolInfo; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 598 | commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 599 | commandPoolInfo.pNext = nullptr; |
| 600 | commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 601 | commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex; |
| 602 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 603 | ANGLE_TRY(mCommandPool.init(displayVk, commandPoolInfo)); |
| 604 | |
| 605 | // Initialize the vulkan pipeline cache |
| 606 | ANGLE_TRY(initPipelineCacheVk(displayVk)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 607 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 608 | return angle::Result::Continue(); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 609 | } |
| 610 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 611 | angle::Result RendererVk::selectPresentQueueForSurface(DisplayVk *displayVk, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 612 | VkSurfaceKHR surface, |
| 613 | uint32_t *presentQueueOut) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 614 | { |
| 615 | // We've already initialized a device, and can't re-create it unless it's never been used. |
| 616 | // TODO(jmadill): Handle the re-creation case if necessary. |
| 617 | if (mDevice != VK_NULL_HANDLE) |
| 618 | { |
| 619 | ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max()); |
| 620 | |
| 621 | // Check if the current device supports present on this surface. |
| 622 | VkBool32 supportsPresent = VK_FALSE; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 623 | ANGLE_VK_TRY(displayVk, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 624 | vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex, |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 625 | surface, &supportsPresent)); |
| 626 | |
Jamie Madill | 6cad773 | 2018-07-11 09:01:17 -0400 | [diff] [blame] | 627 | if (supportsPresent == VK_TRUE) |
| 628 | { |
| 629 | *presentQueueOut = mCurrentQueueFamilyIndex; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 630 | return angle::Result::Continue(); |
Jamie Madill | 6cad773 | 2018-07-11 09:01:17 -0400 | [diff] [blame] | 631 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // Find a graphics and present queue. |
| 635 | Optional<uint32_t> newPresentQueue; |
| 636 | uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size()); |
| 637 | for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex) |
| 638 | { |
| 639 | const auto &queueInfo = mQueueFamilyProperties[queueIndex]; |
| 640 | if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) |
| 641 | { |
| 642 | VkBool32 supportsPresent = VK_FALSE; |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 643 | ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceSupportKHR( |
| 644 | mPhysicalDevice, queueIndex, surface, &supportsPresent)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 645 | |
| 646 | if (supportsPresent == VK_TRUE) |
| 647 | { |
| 648 | newPresentQueue = queueIndex; |
| 649 | break; |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 654 | ANGLE_VK_CHECK(displayVk, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED); |
| 655 | ANGLE_TRY(initializeDevice(displayVk, newPresentQueue.value())); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 656 | |
Jamie Madill | 6cad773 | 2018-07-11 09:01:17 -0400 | [diff] [blame] | 657 | *presentQueueOut = newPresentQueue.value(); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 658 | return angle::Result::Continue(); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | std::string RendererVk::getVendorString() const |
| 662 | { |
Olli Etuaho | c6a0618 | 2018-04-13 14:11:46 +0300 | [diff] [blame] | 663 | return GetVendorString(mPhysicalDeviceProperties.vendorID); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 664 | } |
| 665 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 666 | std::string RendererVk::getRendererDescription() const |
| 667 | { |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 668 | std::stringstream strstr; |
| 669 | |
| 670 | uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion; |
| 671 | |
| 672 | strstr << "Vulkan "; |
| 673 | strstr << VK_VERSION_MAJOR(apiVersion) << "."; |
| 674 | strstr << VK_VERSION_MINOR(apiVersion) << "."; |
| 675 | strstr << VK_VERSION_PATCH(apiVersion); |
| 676 | |
Olli Etuaho | c6a0618 | 2018-04-13 14:11:46 +0300 | [diff] [blame] | 677 | strstr << "("; |
| 678 | |
| 679 | // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that |
| 680 | // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be |
| 681 | // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated |
| 682 | // driver detection. |
| 683 | if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA) |
| 684 | { |
| 685 | strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " "; |
| 686 | } |
| 687 | |
| 688 | strstr << mPhysicalDeviceProperties.deviceName << ")"; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 689 | |
| 690 | return strstr.str(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 691 | } |
| 692 | |
Jamie Madill | 1222207 | 2018-07-11 14:59:48 -0400 | [diff] [blame] | 693 | void RendererVk::initFeatures() |
| 694 | { |
Jamie Madill | b36a481 | 2018-09-25 10:15:11 -0400 | [diff] [blame] | 695 | // Use OpenGL line rasterization rules by default. |
| 696 | // TODO(jmadill): Fix Android support. http://anglebug.com/2830 |
| 697 | #if defined(ANGLE_PLATFORM_ANDROID) |
| 698 | mFeatures.basicGLLineRasterization = false; |
| 699 | #else |
Jamie Madill | 1222207 | 2018-07-11 14:59:48 -0400 | [diff] [blame] | 700 | mFeatures.basicGLLineRasterization = true; |
Jamie Madill | b36a481 | 2018-09-25 10:15:11 -0400 | [diff] [blame] | 701 | #endif // defined(ANGLE_PLATFORM_ANDROID) |
Jamie Madill | 1222207 | 2018-07-11 14:59:48 -0400 | [diff] [blame] | 702 | |
Luc Ferron | f786b70 | 2018-07-10 11:01:43 -0400 | [diff] [blame] | 703 | // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need |
| 704 | // investigation. http://anglebug.com/2728 |
| 705 | mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID); |
Frank Henigman | beb669d | 2018-09-21 16:25:52 -0400 | [diff] [blame] | 706 | |
| 707 | #ifdef ANGLE_PLATFORM_WINDOWS |
| 708 | // http://anglebug.com/2838 |
| 709 | mFeatures.extraCopyBufferRegion = IsIntel(mPhysicalDeviceProperties.vendorID); |
| 710 | #endif |
Jamie Madill | 1222207 | 2018-07-11 14:59:48 -0400 | [diff] [blame] | 711 | } |
| 712 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 713 | void RendererVk::initPipelineCacheVkKey() |
| 714 | { |
| 715 | std::ostringstream hashStream("ANGLE Pipeline Cache: ", std::ios_base::ate); |
| 716 | // Add the pipeline cache UUID to make sure the blob cache always gives a compatible pipeline |
| 717 | // cache. It's not particularly necessary to write it as a hex number as done here, so long as |
| 718 | // there is no '\0' in the result. |
| 719 | for (const uint32_t c : mPhysicalDeviceProperties.pipelineCacheUUID) |
| 720 | { |
| 721 | hashStream << std::hex << c; |
| 722 | } |
| 723 | // Add the vendor and device id too for good measure. |
| 724 | hashStream << std::hex << mPhysicalDeviceProperties.vendorID; |
| 725 | hashStream << std::hex << mPhysicalDeviceProperties.deviceID; |
| 726 | |
| 727 | const std::string &hashString = hashStream.str(); |
| 728 | angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(hashString.c_str()), |
| 729 | hashString.length(), mPipelineCacheVkBlobKey.data()); |
| 730 | } |
| 731 | |
| 732 | angle::Result RendererVk::initPipelineCacheVk(DisplayVk *display) |
| 733 | { |
| 734 | initPipelineCacheVkKey(); |
| 735 | |
| 736 | egl::BlobCache::Value initialData; |
| 737 | bool success = display->getBlobCache()->get(display->getScratchBuffer(), |
| 738 | mPipelineCacheVkBlobKey, &initialData); |
| 739 | |
| 740 | VkPipelineCacheCreateInfo pipelineCacheCreateInfo; |
| 741 | |
| 742 | pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
| 743 | pipelineCacheCreateInfo.pNext = nullptr; |
| 744 | pipelineCacheCreateInfo.flags = 0; |
| 745 | pipelineCacheCreateInfo.initialDataSize = success ? initialData.size() : 0; |
| 746 | pipelineCacheCreateInfo.pInitialData = success ? initialData.data() : nullptr; |
| 747 | |
| 748 | ANGLE_TRY(mPipelineCacheVk.init(display, pipelineCacheCreateInfo)); |
| 749 | return angle::Result::Continue(); |
| 750 | } |
| 751 | |
Jamie Madill | acccc6c | 2016-05-03 17:22:10 -0400 | [diff] [blame] | 752 | void RendererVk::ensureCapsInitialized() const |
| 753 | { |
| 754 | if (!mCapsInitialized) |
| 755 | { |
Jamie Madill | 30b5d84 | 2018-08-31 17:19:12 -0400 | [diff] [blame] | 756 | vk::GenerateCaps(mPhysicalDeviceProperties, mPhysicalDeviceFeatures, mNativeTextureCaps, |
| 757 | &mNativeCaps, &mNativeExtensions, &mNativeLimitations); |
Jamie Madill | acccc6c | 2016-05-03 17:22:10 -0400 | [diff] [blame] | 758 | mCapsInitialized = true; |
| 759 | } |
| 760 | } |
| 761 | |
Jamie Madill | acccc6c | 2016-05-03 17:22:10 -0400 | [diff] [blame] | 762 | const gl::Caps &RendererVk::getNativeCaps() const |
| 763 | { |
| 764 | ensureCapsInitialized(); |
| 765 | return mNativeCaps; |
| 766 | } |
| 767 | |
| 768 | const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const |
| 769 | { |
| 770 | ensureCapsInitialized(); |
| 771 | return mNativeTextureCaps; |
| 772 | } |
| 773 | |
| 774 | const gl::Extensions &RendererVk::getNativeExtensions() const |
| 775 | { |
| 776 | ensureCapsInitialized(); |
| 777 | return mNativeExtensions; |
| 778 | } |
| 779 | |
| 780 | const gl::Limitations &RendererVk::getNativeLimitations() const |
| 781 | { |
| 782 | ensureCapsInitialized(); |
| 783 | return mNativeLimitations; |
| 784 | } |
| 785 | |
Luc Ferron | daedf4d | 2018-03-16 09:28:53 -0400 | [diff] [blame] | 786 | uint32_t RendererVk::getMaxActiveTextures() |
| 787 | { |
| 788 | // TODO(lucferron): expose this limitation to GL in Context Caps |
| 789 | return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers, |
| 790 | gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES); |
| 791 | } |
| 792 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 793 | const vk::CommandPool &RendererVk::getCommandPool() const |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 794 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 795 | return mCommandPool; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 796 | } |
| 797 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 798 | angle::Result RendererVk::finish(vk::Context *context) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 799 | { |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 800 | if (!mCommandGraph.empty()) |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 801 | { |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 802 | vk::Scoped<vk::CommandBuffer> commandBatch(mDevice); |
| 803 | ANGLE_TRY(flushCommandGraph(context, &commandBatch.get())); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 804 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 805 | VkSubmitInfo submitInfo; |
| 806 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 807 | submitInfo.pNext = nullptr; |
| 808 | submitInfo.waitSemaphoreCount = 0; |
| 809 | submitInfo.pWaitSemaphores = nullptr; |
| 810 | submitInfo.pWaitDstStageMask = nullptr; |
| 811 | submitInfo.commandBufferCount = 1; |
Luc Ferron | 1617e69 | 2018-07-11 11:08:19 -0400 | [diff] [blame] | 812 | submitInfo.pCommandBuffers = commandBatch.get().ptr(); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 813 | submitInfo.signalSemaphoreCount = 0; |
| 814 | submitInfo.pSignalSemaphores = nullptr; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 815 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 816 | ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get()))); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 817 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 818 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 819 | ASSERT(mQueue != VK_NULL_HANDLE); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 820 | ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue)); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 821 | freeAllInFlightResources(); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 822 | return angle::Result::Continue(); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 823 | } |
| 824 | |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 825 | void RendererVk::freeAllInFlightResources() |
| 826 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 827 | for (CommandBatch &batch : mInFlightCommands) |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 828 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 829 | batch.fence.destroy(mDevice); |
| 830 | batch.commandPool.destroy(mDevice); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 831 | } |
| 832 | mInFlightCommands.clear(); |
| 833 | |
| 834 | for (auto &garbage : mGarbage) |
| 835 | { |
Jamie Madill | e88ec8e | 2017-10-31 17:18:14 -0400 | [diff] [blame] | 836 | garbage.destroy(mDevice); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 837 | } |
| 838 | mGarbage.clear(); |
| 839 | } |
| 840 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 841 | angle::Result RendererVk::checkInFlightCommands(vk::Context *context) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 842 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 843 | int finishedCount = 0; |
Jamie Madill | f651c77 | 2017-02-21 15:03:51 -0500 | [diff] [blame] | 844 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 845 | for (CommandBatch &batch : mInFlightCommands) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 846 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 847 | VkResult result = batch.fence.getStatus(mDevice); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 848 | if (result == VK_NOT_READY) |
| 849 | break; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 850 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 851 | ANGLE_VK_TRY(context, result); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 852 | ASSERT(batch.serial > mLastCompletedQueueSerial); |
| 853 | mLastCompletedQueueSerial = batch.serial; |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 854 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 855 | batch.fence.destroy(mDevice); |
| 856 | batch.commandPool.destroy(mDevice); |
| 857 | ++finishedCount; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 858 | } |
| 859 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 860 | mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 861 | |
| 862 | size_t freeIndex = 0; |
| 863 | for (; freeIndex < mGarbage.size(); ++freeIndex) |
| 864 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 865 | if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial)) |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 866 | break; |
| 867 | } |
| 868 | |
| 869 | // Remove the entries from the garbage list - they should be ready to go. |
| 870 | if (freeIndex > 0) |
| 871 | { |
| 872 | mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex); |
Jamie Madill | f651c77 | 2017-02-21 15:03:51 -0500 | [diff] [blame] | 873 | } |
| 874 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 875 | return angle::Result::Continue(); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 876 | } |
| 877 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 878 | angle::Result RendererVk::submitFrame(vk::Context *context, |
| 879 | const VkSubmitInfo &submitInfo, |
| 880 | vk::CommandBuffer &&commandBuffer) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 881 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 882 | VkFenceCreateInfo fenceInfo; |
| 883 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 884 | fenceInfo.pNext = nullptr; |
| 885 | fenceInfo.flags = 0; |
| 886 | |
Jamie Madill | bea35a6 | 2018-07-05 11:54:10 -0400 | [diff] [blame] | 887 | vk::Scoped<CommandBatch> scopedBatch(mDevice); |
| 888 | CommandBatch &batch = scopedBatch.get(); |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 889 | ANGLE_TRY(batch.fence.init(context, fenceInfo)); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 890 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 891 | ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle())); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 892 | |
| 893 | // Store this command buffer in the in-flight list. |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 894 | batch.commandPool = std::move(mCommandPool); |
| 895 | batch.serial = mCurrentQueueSerial; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 896 | |
Jamie Madill | bea35a6 | 2018-07-05 11:54:10 -0400 | [diff] [blame] | 897 | mInFlightCommands.emplace_back(scopedBatch.release()); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 898 | |
Omar El Sheikh | 0359b69 | 2018-07-23 17:22:00 -0600 | [diff] [blame] | 899 | // Check that mInFlightCommands isn't growing too fast |
| 900 | // If it is, wait for the queue to complete work it has alread been assigned |
| 901 | if (mInFlightCommands.size() > kInFlightCommandsLimit) |
| 902 | { |
| 903 | vkQueueWaitIdle(mQueue); |
| 904 | } |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 905 | |
| 906 | // Increment the queue serial. If this fails, we should restart ANGLE. |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 907 | // TODO(jmadill): Overflow check. |
| 908 | mCurrentQueueSerial = mQueueSerialFactory.generate(); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 909 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 910 | ANGLE_TRY(checkInFlightCommands(context)); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 911 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 912 | // Simply null out the command buffer here - it was allocated using the command pool. |
| 913 | commandBuffer.releaseHandle(); |
| 914 | |
| 915 | // Reallocate the command pool for next frame. |
| 916 | // TODO(jmadill): Consider reusing command pools. |
| 917 | VkCommandPoolCreateInfo poolInfo; |
| 918 | poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 919 | poolInfo.pNext = nullptr; |
| 920 | poolInfo.flags = 0; |
| 921 | poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex; |
| 922 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 923 | ANGLE_TRY(mCommandPool.init(context, poolInfo)); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 924 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 925 | return angle::Result::Continue(); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 926 | } |
| 927 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 928 | bool RendererVk::isSerialInUse(Serial serial) const |
Jamie Madill | 9776035 | 2017-11-09 13:08:29 -0500 | [diff] [blame] | 929 | { |
| 930 | return serial > mLastCompletedQueueSerial; |
| 931 | } |
| 932 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 933 | angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context, |
| 934 | const vk::RenderPassDesc &desc, |
| 935 | vk::RenderPass **renderPassOut) |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 936 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 937 | return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc, |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 938 | renderPassOut); |
| 939 | } |
| 940 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 941 | angle::Result RendererVk::getRenderPassWithOps(vk::Context *context, |
| 942 | const vk::RenderPassDesc &desc, |
| 943 | const vk::AttachmentOpsArray &ops, |
| 944 | vk::RenderPass **renderPassOut) |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 945 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 946 | return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops, |
Jamie Madill | bef918c | 2017-12-13 13:11:30 -0500 | [diff] [blame] | 947 | renderPassOut); |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 948 | } |
| 949 | |
Jamie Madill | a5e0607 | 2018-05-18 14:36:05 -0400 | [diff] [blame] | 950 | vk::CommandGraph *RendererVk::getCommandGraph() |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 951 | { |
Jamie Madill | a5e0607 | 2018-05-18 14:36:05 -0400 | [diff] [blame] | 952 | return &mCommandGraph; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 953 | } |
| 954 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 955 | angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch) |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 956 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 957 | return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache, |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 958 | &mCommandPool, commandBatch); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 959 | } |
| 960 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 961 | angle::Result RendererVk::flush(vk::Context *context, |
| 962 | const vk::Semaphore &waitSemaphore, |
| 963 | const vk::Semaphore &signalSemaphore) |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 964 | { |
Jamie Madill | bea35a6 | 2018-07-05 11:54:10 -0400 | [diff] [blame] | 965 | vk::Scoped<vk::CommandBuffer> commandBatch(mDevice); |
| 966 | ANGLE_TRY(flushCommandGraph(context, &commandBatch.get())); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 967 | |
| 968 | VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 969 | |
| 970 | VkSubmitInfo submitInfo; |
| 971 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 972 | submitInfo.pNext = nullptr; |
| 973 | submitInfo.waitSemaphoreCount = 1; |
| 974 | submitInfo.pWaitSemaphores = waitSemaphore.ptr(); |
| 975 | submitInfo.pWaitDstStageMask = &waitStageMask; |
| 976 | submitInfo.commandBufferCount = 1; |
Jamie Madill | bea35a6 | 2018-07-05 11:54:10 -0400 | [diff] [blame] | 977 | submitInfo.pCommandBuffers = commandBatch.get().ptr(); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 978 | submitInfo.signalSemaphoreCount = 1; |
| 979 | submitInfo.pSignalSemaphores = signalSemaphore.ptr(); |
| 980 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 981 | ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release())); |
| 982 | return angle::Result::Continue(); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 983 | } |
| 984 | |
Jamie Madill | 78feddc | 2018-04-27 11:45:05 -0400 | [diff] [blame] | 985 | Serial RendererVk::issueShaderSerial() |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 986 | { |
Jamie Madill | 78feddc | 2018-04-27 11:45:05 -0400 | [diff] [blame] | 987 | return mShaderSerialFactory.generate(); |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 988 | } |
| 989 | |
Jamie Madill | 06ca634 | 2018-07-12 15:56:53 -0400 | [diff] [blame] | 990 | angle::Result RendererVk::getPipeline(vk::Context *context, |
| 991 | const vk::ShaderAndSerial &vertexShader, |
| 992 | const vk::ShaderAndSerial &fragmentShader, |
| 993 | const vk::PipelineLayout &pipelineLayout, |
| 994 | const vk::PipelineDesc &pipelineDesc, |
| 995 | const gl::AttributesMask &activeAttribLocationsMask, |
| 996 | vk::PipelineAndSerial **pipelineOut) |
Jamie Madill | ffa4cbb | 2018-01-23 13:04:07 -0500 | [diff] [blame] | 997 | { |
Jamie Madill | 06ca634 | 2018-07-12 15:56:53 -0400 | [diff] [blame] | 998 | ASSERT(vertexShader.getSerial() == |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 999 | pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial); |
Jamie Madill | 06ca634 | 2018-07-12 15:56:53 -0400 | [diff] [blame] | 1000 | ASSERT(fragmentShader.getSerial() == |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1001 | pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial); |
| 1002 | |
| 1003 | // Pull in a compatible RenderPass. |
| 1004 | vk::RenderPass *compatibleRenderPass = nullptr; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1005 | ANGLE_TRY( |
| 1006 | getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass)); |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1007 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 1008 | return mPipelineCache.getPipeline(context, mPipelineCacheVk, *compatibleRenderPass, |
| 1009 | pipelineLayout, activeAttribLocationsMask, vertexShader.get(), |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 1010 | fragmentShader.get(), pipelineDesc, pipelineOut); |
| 1011 | } |
| 1012 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1013 | angle::Result RendererVk::getDescriptorSetLayout( |
| 1014 | vk::Context *context, |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 1015 | const vk::DescriptorSetLayoutDesc &desc, |
| 1016 | vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut) |
| 1017 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1018 | return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut); |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 1019 | } |
| 1020 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1021 | angle::Result RendererVk::getPipelineLayout( |
| 1022 | vk::Context *context, |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 1023 | const vk::PipelineLayoutDesc &desc, |
| 1024 | const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts, |
| 1025 | vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut) |
| 1026 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 1027 | return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts, |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 1028 | pipelineLayoutOut); |
| 1029 | } |
| 1030 | |
Shahbaz Youssefi | 996628a | 2018-09-24 16:39:26 -0400 | [diff] [blame^] | 1031 | angle::Result RendererVk::syncPipelineCacheVk(DisplayVk *displayVk) |
| 1032 | { |
| 1033 | ASSERT(mPipelineCacheVk.valid()); |
| 1034 | |
| 1035 | if (--mPipelineCacheVkUpdateTimeout > 0) |
| 1036 | { |
| 1037 | return angle::Result::Continue(); |
| 1038 | } |
| 1039 | |
| 1040 | mPipelineCacheVkUpdateTimeout = kPipelineCacheVkUpdatePeriod; |
| 1041 | |
| 1042 | // Get the size of the cache. |
| 1043 | size_t pipelineCacheSize = 0; |
| 1044 | ANGLE_TRY(mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, nullptr)); |
| 1045 | |
| 1046 | angle::MemoryBuffer *pipelineCacheData = nullptr; |
| 1047 | ANGLE_VK_CHECK_ALLOC(displayVk, |
| 1048 | displayVk->getScratchBuffer(pipelineCacheSize, &pipelineCacheData)); |
| 1049 | |
| 1050 | size_t originalPipelineCacheSize = pipelineCacheSize; |
| 1051 | angle::Result result = |
| 1052 | mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, pipelineCacheData->data()); |
| 1053 | ANGLE_TRY(result); |
| 1054 | |
| 1055 | // Note: currently we don't accept incomplete as we don't expect it (the full size of cache |
| 1056 | // was determined just above), so receiving it hints at an implementation bug we would want |
| 1057 | // to know about early. |
| 1058 | ASSERT(result != angle::Result::Incomplete()); |
| 1059 | |
| 1060 | // If vkGetPipelineCacheData ends up writing fewer bytes than requested, zero out the rest of |
| 1061 | // the buffer to avoid leaking garbage memory. |
| 1062 | ASSERT(pipelineCacheSize <= originalPipelineCacheSize); |
| 1063 | if (pipelineCacheSize < originalPipelineCacheSize) |
| 1064 | { |
| 1065 | memset(pipelineCacheData->data() + pipelineCacheSize, 0, |
| 1066 | originalPipelineCacheSize - pipelineCacheSize); |
| 1067 | } |
| 1068 | |
| 1069 | displayVk->getBlobCache()->putApplication(mPipelineCacheVkBlobKey, *pipelineCacheData); |
| 1070 | |
| 1071 | return angle::Result::Continue(); |
| 1072 | } |
| 1073 | |
Jamie Madill | d47044a | 2018-04-27 11:45:03 -0400 | [diff] [blame] | 1074 | vk::ShaderLibrary *RendererVk::getShaderLibrary() |
| 1075 | { |
| 1076 | return &mShaderLibrary; |
| 1077 | } |
Luc Ferron | 9096836 | 2018-05-04 08:47:22 -0400 | [diff] [blame] | 1078 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 1079 | uint32_t GetUniformBufferDescriptorCount() |
| 1080 | { |
| 1081 | return kUniformBufferDescriptorsPerDescriptorSet; |
| 1082 | } |
| 1083 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1084 | } // namespace rx |