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" |
| 22 | #include "libANGLE/renderer/vulkan/FramebufferVk.h" |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 23 | #include "libANGLE/renderer/vulkan/GlslangWrapper.h" |
Jamie Madill | ffa4cbb | 2018-01-23 13:04:07 -0500 | [diff] [blame] | 24 | #include "libANGLE/renderer/vulkan/ProgramVk.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 25 | #include "libANGLE/renderer/vulkan/VertexArrayVk.h" |
Luc Ferron | e4741fd | 2018-01-25 13:25:27 -0500 | [diff] [blame] | 26 | #include "libANGLE/renderer/vulkan/vk_caps_utils.h" |
Jamie Madill | 3c424b4 | 2018-01-19 12:35:09 -0500 | [diff] [blame] | 27 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 28 | #include "platform/Platform.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 29 | |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 30 | // Consts |
| 31 | namespace |
| 32 | { |
| 33 | const uint32_t kMockVendorID = 0xba5eba11; |
| 34 | const uint32_t kMockDeviceID = 0xf005ba11; |
| 35 | constexpr char kMockDeviceName[] = "Vulkan Mock Device"; |
| 36 | } // anonymous namespace |
| 37 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 38 | namespace rx |
| 39 | { |
| 40 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 41 | namespace |
| 42 | { |
Luc Ferron | daedf4d | 2018-03-16 09:28:53 -0400 | [diff] [blame] | 43 | // We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and |
| 44 | // one for the vertex shader. |
| 45 | constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2; |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 46 | |
| 47 | VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps, |
| 48 | const std::vector<const char *> &enabledExtensionNames) |
| 49 | { |
| 50 | // Compile the extensions names into a set. |
| 51 | std::set<std::string> extensionNames; |
| 52 | for (const auto &extensionProp : extensionProps) |
| 53 | { |
| 54 | extensionNames.insert(extensionProp.extensionName); |
| 55 | } |
| 56 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 57 | for (const char *extensionName : enabledExtensionNames) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 58 | { |
| 59 | if (extensionNames.count(extensionName) == 0) |
| 60 | { |
| 61 | return VK_ERROR_EXTENSION_NOT_PRESENT; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return VK_SUCCESS; |
| 66 | } |
| 67 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 68 | VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags, |
| 69 | VkDebugReportObjectTypeEXT objectType, |
| 70 | uint64_t object, |
| 71 | size_t location, |
| 72 | int32_t messageCode, |
| 73 | const char *layerPrefix, |
| 74 | const char *message, |
| 75 | void *userData) |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 76 | { |
| 77 | if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0) |
| 78 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 79 | ERR() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 80 | #if !defined(NDEBUG) |
| 81 | // Abort the call in Debug builds. |
| 82 | return VK_TRUE; |
| 83 | #endif |
| 84 | } |
| 85 | else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0) |
| 86 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 87 | WARN() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 88 | } |
| 89 | else |
| 90 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 91 | // Uncomment this if you want Vulkan spam. |
| 92 | // WARN() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return VK_FALSE; |
| 96 | } |
| 97 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 98 | // If we're loading the validation layers, we could be running from any random directory. |
| 99 | // Change to the executable directory so we can find the layers, then change back to the |
| 100 | // previous directory to be safe we don't disrupt the application. |
| 101 | class ScopedVkLoaderEnvironment : angle::NonCopyable |
| 102 | { |
| 103 | public: |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 104 | explicit ScopedVkLoaderEnvironment(bool enableValidationLayers) |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 105 | : mEnableValidationLayers(enableValidationLayers), mChangedCWD(false) |
| 106 | { |
| 107 | // Changing CWD and setting environment variables makes no sense on Android, |
| 108 | // since this code is a part of Java application there. |
| 109 | // Android Vulkan loader doesn't need this either. |
| 110 | #if !defined(ANGLE_PLATFORM_ANDROID) |
| 111 | if (mEnableValidationLayers) |
| 112 | { |
| 113 | const auto &cwd = angle::GetCWD(); |
| 114 | if (!cwd.valid()) |
| 115 | { |
| 116 | ERR() << "Error getting CWD for Vulkan layers init."; |
| 117 | mEnableValidationLayers = false; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | mPreviousCWD = cwd.value(); |
| 122 | const char *exeDir = angle::GetExecutableDirectory(); |
| 123 | mChangedCWD = angle::SetCWD(exeDir); |
| 124 | if (!mChangedCWD) |
| 125 | { |
| 126 | ERR() << "Error setting CWD for Vulkan layers init."; |
| 127 | mEnableValidationLayers = false; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Override environment variable to use the ANGLE layers. |
| 133 | if (mEnableValidationLayers) |
| 134 | { |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 135 | if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR)) |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 136 | { |
| 137 | ERR() << "Error setting environment for Vulkan layers init."; |
| 138 | mEnableValidationLayers = false; |
| 139 | } |
| 140 | } |
| 141 | #endif // !defined(ANGLE_PLATFORM_ANDROID) |
| 142 | } |
| 143 | |
| 144 | ~ScopedVkLoaderEnvironment() |
| 145 | { |
| 146 | if (mChangedCWD) |
| 147 | { |
| 148 | #if !defined(ANGLE_PLATFORM_ANDROID) |
| 149 | ASSERT(mPreviousCWD.valid()); |
| 150 | angle::SetCWD(mPreviousCWD.value().c_str()); |
| 151 | #endif // !defined(ANGLE_PLATFORM_ANDROID) |
| 152 | } |
| 153 | } |
| 154 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 155 | bool canEnableValidationLayers() const { return mEnableValidationLayers; } |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 156 | |
| 157 | private: |
| 158 | bool mEnableValidationLayers; |
| 159 | bool mChangedCWD; |
| 160 | Optional<std::string> mPreviousCWD; |
| 161 | }; |
| 162 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 163 | } // anonymous namespace |
| 164 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 165 | // CommandBatch implementation. |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 166 | RendererVk::CommandBatch::CommandBatch() = default; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 167 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 168 | RendererVk::CommandBatch::~CommandBatch() = default; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 169 | |
| 170 | RendererVk::CommandBatch::CommandBatch(CommandBatch &&other) |
| 171 | : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial) |
| 172 | { |
| 173 | } |
| 174 | |
| 175 | RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other) |
| 176 | { |
| 177 | std::swap(commandPool, other.commandPool); |
| 178 | std::swap(fence, other.fence); |
| 179 | std::swap(serial, other.serial); |
| 180 | return *this; |
| 181 | } |
| 182 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 183 | // RendererVk implementation. |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 184 | RendererVk::RendererVk() |
| 185 | : mCapsInitialized(false), |
| 186 | mInstance(VK_NULL_HANDLE), |
| 187 | mEnableValidationLayers(false), |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 188 | mDebugReportCallback(VK_NULL_HANDLE), |
| 189 | mPhysicalDevice(VK_NULL_HANDLE), |
| 190 | mQueue(VK_NULL_HANDLE), |
| 191 | mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()), |
| 192 | mDevice(VK_NULL_HANDLE), |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 193 | mGlslangWrapper(nullptr), |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 194 | mLastCompletedQueueSerial(mQueueSerialFactory.generate()), |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 195 | mCurrentQueueSerial(mQueueSerialFactory.generate()) |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 196 | { |
| 197 | } |
| 198 | |
| 199 | RendererVk::~RendererVk() |
| 200 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 201 | if (!mInFlightCommands.empty() || !mGarbage.empty()) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 202 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 203 | // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem. |
| 204 | vk::Error error = finish(nullptr); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 205 | if (error.isError()) |
| 206 | { |
| 207 | ERR() << "Error during VK shutdown: " << error; |
| 208 | } |
| 209 | } |
| 210 | |
Jamie Madill | c7918ce | 2018-06-13 13:25:31 -0400 | [diff] [blame] | 211 | mPipelineLayoutCache.destroy(mDevice); |
| 212 | mDescriptorSetLayoutCache.destroy(mDevice); |
| 213 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 214 | mRenderPassCache.destroy(mDevice); |
Jamie Madill | ffa4cbb | 2018-01-23 13:04:07 -0500 | [diff] [blame] | 215 | mPipelineCache.destroy(mDevice); |
Jamie Madill | d47044a | 2018-04-27 11:45:03 -0400 | [diff] [blame] | 216 | mShaderLibrary.destroy(mDevice); |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 217 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 218 | if (mGlslangWrapper) |
| 219 | { |
| 220 | GlslangWrapper::ReleaseReference(); |
| 221 | mGlslangWrapper = nullptr; |
| 222 | } |
| 223 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 224 | if (mCommandPool.valid()) |
| 225 | { |
| 226 | mCommandPool.destroy(mDevice); |
| 227 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 228 | |
| 229 | if (mDevice) |
| 230 | { |
| 231 | vkDestroyDevice(mDevice, nullptr); |
| 232 | mDevice = VK_NULL_HANDLE; |
| 233 | } |
| 234 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 235 | if (mDebugReportCallback) |
| 236 | { |
| 237 | ASSERT(mInstance); |
| 238 | auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( |
| 239 | vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT")); |
| 240 | ASSERT(destroyDebugReportCallback); |
| 241 | destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr); |
| 242 | } |
| 243 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 244 | if (mInstance) |
| 245 | { |
| 246 | vkDestroyInstance(mInstance, nullptr); |
| 247 | mInstance = VK_NULL_HANDLE; |
| 248 | } |
| 249 | |
| 250 | mPhysicalDevice = VK_NULL_HANDLE; |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 251 | } |
| 252 | |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 253 | void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices, |
| 254 | bool preferMockICD, |
| 255 | VkPhysicalDevice *physicalDeviceOut, |
| 256 | VkPhysicalDeviceProperties *physicalDevicePropertiesOut) |
| 257 | { |
| 258 | ASSERT(!physicalDevices.empty()); |
| 259 | if (preferMockICD) |
| 260 | { |
| 261 | for (const VkPhysicalDevice &physicalDevice : physicalDevices) |
| 262 | { |
| 263 | vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut); |
| 264 | if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) && |
| 265 | (kMockDeviceID == physicalDevicePropertiesOut->deviceID) && |
| 266 | (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0)) |
| 267 | { |
| 268 | *physicalDeviceOut = physicalDevice; |
| 269 | return; |
| 270 | } |
| 271 | } |
| 272 | WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default " |
| 273 | "physicalDevice instead."; |
| 274 | } |
| 275 | |
| 276 | // Fall back to first device. |
| 277 | *physicalDeviceOut = physicalDevices[0]; |
| 278 | vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut); |
| 279 | } |
| 280 | |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 281 | vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName) |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 282 | { |
Jamie Madill | df9ad2b | 2018-02-02 12:40:01 -0500 | [diff] [blame] | 283 | ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs)); |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 284 | mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers(); |
Jamie Madill | a66779f | 2017-01-06 10:43:44 -0500 | [diff] [blame] | 285 | |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 286 | #if !defined(ANGLE_PLATFORM_ANDROID) |
| 287 | // Mock ICD does not currently run on Android |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 288 | bool enableNullDriver = (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, |
| 289 | EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) == |
| 290 | EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE); |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 291 | if (enableNullDriver) |
| 292 | { |
| 293 | // Override environment variable to use built Mock ICD |
| 294 | // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn |
| 295 | ANGLE_VK_CHECK(angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON), |
| 296 | VK_ERROR_INITIALIZATION_FAILED); |
| 297 | } |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 298 | #else |
| 299 | constexpr bool enableNullDriver = false; |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 300 | #endif // !defined(ANGLE_PLATFORM_ANDROID) |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 301 | // Gather global layer properties. |
| 302 | uint32_t instanceLayerCount = 0; |
| 303 | ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr)); |
| 304 | |
| 305 | std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount); |
| 306 | if (instanceLayerCount > 0) |
| 307 | { |
| 308 | ANGLE_VK_TRY( |
| 309 | vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data())); |
| 310 | } |
| 311 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 312 | uint32_t instanceExtensionCount = 0; |
| 313 | ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr)); |
| 314 | |
| 315 | std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount); |
| 316 | if (instanceExtensionCount > 0) |
| 317 | { |
| 318 | ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, |
| 319 | instanceExtensionProps.data())); |
| 320 | } |
| 321 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 322 | const char *const *enabledLayerNames = nullptr; |
| 323 | uint32_t enabledLayerCount = 0; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 324 | if (mEnableValidationLayers) |
| 325 | { |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 326 | bool layersRequested = |
| 327 | (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE); |
| 328 | mEnableValidationLayers = GetAvailableValidationLayers( |
| 329 | instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount); |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 330 | } |
| 331 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 332 | std::vector<const char *> enabledInstanceExtensions; |
| 333 | enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 334 | enabledInstanceExtensions.push_back(wsiName); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 335 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 336 | // TODO(jmadill): Should be able to continue initialization if debug report ext missing. |
| 337 | if (mEnableValidationLayers) |
| 338 | { |
| 339 | enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); |
| 340 | } |
| 341 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 342 | // Verify the required extensions are in the extension names set. Fail if not. |
| 343 | ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions)); |
| 344 | |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 345 | VkApplicationInfo applicationInfo; |
| 346 | applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; |
| 347 | applicationInfo.pNext = nullptr; |
| 348 | applicationInfo.pApplicationName = "ANGLE"; |
| 349 | applicationInfo.applicationVersion = 1; |
| 350 | applicationInfo.pEngineName = "ANGLE"; |
| 351 | applicationInfo.engineVersion = 1; |
| 352 | applicationInfo.apiVersion = VK_API_VERSION_1_0; |
| 353 | |
| 354 | VkInstanceCreateInfo instanceInfo; |
| 355 | instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; |
| 356 | instanceInfo.pNext = nullptr; |
| 357 | instanceInfo.flags = 0; |
| 358 | instanceInfo.pApplicationInfo = &applicationInfo; |
| 359 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 360 | // Enable requested layers and extensions. |
| 361 | instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size()); |
| 362 | instanceInfo.ppEnabledExtensionNames = |
| 363 | enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data(); |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 364 | instanceInfo.enabledLayerCount = enabledLayerCount; |
| 365 | instanceInfo.ppEnabledLayerNames = enabledLayerNames; |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 366 | |
| 367 | ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance)); |
| 368 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 369 | if (mEnableValidationLayers) |
| 370 | { |
| 371 | VkDebugReportCallbackCreateInfoEXT debugReportInfo; |
| 372 | |
| 373 | debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
| 374 | debugReportInfo.pNext = nullptr; |
| 375 | debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | |
| 376 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT | |
| 377 | VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT; |
| 378 | debugReportInfo.pfnCallback = &DebugReportCallback; |
| 379 | debugReportInfo.pUserData = this; |
| 380 | |
| 381 | auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>( |
| 382 | vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT")); |
| 383 | ASSERT(createDebugReportCallback); |
| 384 | ANGLE_VK_TRY( |
| 385 | createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback)); |
| 386 | } |
| 387 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 388 | uint32_t physicalDeviceCount = 0; |
| 389 | ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr)); |
| 390 | ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
| 391 | |
| 392 | // TODO(jmadill): Handle multiple physical devices. For now, use the first device. |
Tobin Ehlis | a3b220f | 2018-03-06 16:22:13 -0700 | [diff] [blame] | 393 | std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount); |
| 394 | ANGLE_VK_TRY( |
| 395 | vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, physicalDevices.data())); |
| 396 | ChoosePhysicalDevice(physicalDevices, enableNullDriver, &mPhysicalDevice, |
| 397 | &mPhysicalDeviceProperties); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 398 | |
| 399 | // Ensure we can find a graphics queue family. |
| 400 | uint32_t queueCount = 0; |
| 401 | vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr); |
| 402 | |
| 403 | ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
| 404 | |
| 405 | mQueueFamilyProperties.resize(queueCount); |
| 406 | vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, |
| 407 | mQueueFamilyProperties.data()); |
| 408 | |
| 409 | size_t graphicsQueueFamilyCount = false; |
| 410 | uint32_t firstGraphicsQueueFamily = 0; |
| 411 | for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex) |
| 412 | { |
| 413 | const auto &queueInfo = mQueueFamilyProperties[familyIndex]; |
| 414 | if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) |
| 415 | { |
| 416 | ASSERT(queueInfo.queueCount > 0); |
| 417 | graphicsQueueFamilyCount++; |
| 418 | if (firstGraphicsQueueFamily == 0) |
| 419 | { |
| 420 | firstGraphicsQueueFamily = familyIndex; |
| 421 | } |
| 422 | break; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
| 427 | |
| 428 | // If only one queue family, go ahead and initialize the device. If there is more than one |
| 429 | // queue, we'll have to wait until we see a WindowSurface to know which supports present. |
| 430 | if (graphicsQueueFamilyCount == 1) |
| 431 | { |
| 432 | ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily)); |
| 433 | } |
| 434 | |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 435 | // Store the physical device memory properties so we can find the right memory pools. |
| 436 | mMemoryProperties.init(mPhysicalDevice); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 437 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 438 | mGlslangWrapper = GlslangWrapper::GetReference(); |
| 439 | |
Jamie Madill | 6a89d22 | 2017-11-02 11:59:51 -0400 | [diff] [blame] | 440 | // Initialize the format table. |
Luc Ferron | d50537a | 2018-02-07 17:02:08 -0500 | [diff] [blame] | 441 | mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps, |
| 442 | &mNativeCaps.compressedTextureFormats); |
Jamie Madill | 6a89d22 | 2017-11-02 11:59:51 -0400 | [diff] [blame] | 443 | |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 444 | return vk::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 447 | vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex) |
| 448 | { |
| 449 | uint32_t deviceLayerCount = 0; |
| 450 | ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr)); |
| 451 | |
| 452 | std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount); |
| 453 | if (deviceLayerCount > 0) |
| 454 | { |
| 455 | ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, |
| 456 | deviceLayerProps.data())); |
| 457 | } |
| 458 | |
| 459 | uint32_t deviceExtensionCount = 0; |
| 460 | ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, |
| 461 | &deviceExtensionCount, nullptr)); |
| 462 | |
| 463 | std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount); |
| 464 | if (deviceExtensionCount > 0) |
| 465 | { |
| 466 | ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties( |
| 467 | mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data())); |
| 468 | } |
| 469 | |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 470 | const char *const *enabledLayerNames = nullptr; |
| 471 | uint32_t enabledLayerCount = 0; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 472 | if (mEnableValidationLayers) |
| 473 | { |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 474 | mEnableValidationLayers = GetAvailableValidationLayers( |
| 475 | deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | std::vector<const char *> enabledDeviceExtensions; |
| 479 | enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); |
| 480 | |
| 481 | ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions)); |
| 482 | |
| 483 | VkDeviceQueueCreateInfo queueCreateInfo; |
| 484 | |
| 485 | float zeroPriority = 0.0f; |
| 486 | |
| 487 | queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 488 | queueCreateInfo.pNext = nullptr; |
| 489 | queueCreateInfo.flags = 0; |
| 490 | queueCreateInfo.queueFamilyIndex = queueFamilyIndex; |
| 491 | queueCreateInfo.queueCount = 1; |
| 492 | queueCreateInfo.pQueuePriorities = &zeroPriority; |
| 493 | |
| 494 | // Initialize the device |
| 495 | VkDeviceCreateInfo createInfo; |
| 496 | |
Jamie Madill | 50cf2be | 2018-06-15 09:46:57 -0400 | [diff] [blame] | 497 | createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; |
| 498 | createInfo.pNext = nullptr; |
| 499 | createInfo.flags = 0; |
| 500 | createInfo.queueCreateInfoCount = 1; |
| 501 | createInfo.pQueueCreateInfos = &queueCreateInfo; |
Yuly Novikov | 199f429 | 2018-01-19 19:04:05 -0500 | [diff] [blame] | 502 | createInfo.enabledLayerCount = enabledLayerCount; |
| 503 | createInfo.ppEnabledLayerNames = enabledLayerNames; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 504 | createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size()); |
| 505 | createInfo.ppEnabledExtensionNames = |
| 506 | enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data(); |
| 507 | createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features |
| 508 | |
| 509 | ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice)); |
| 510 | |
| 511 | mCurrentQueueFamilyIndex = queueFamilyIndex; |
| 512 | |
| 513 | vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue); |
| 514 | |
| 515 | // Initialize the command pool now that we know the queue family index. |
| 516 | VkCommandPoolCreateInfo commandPoolInfo; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 517 | commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 518 | commandPoolInfo.pNext = nullptr; |
| 519 | commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 520 | commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex; |
| 521 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 522 | ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 523 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 524 | return vk::NoError(); |
| 525 | } |
| 526 | |
| 527 | vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface) |
| 528 | { |
| 529 | // We've already initialized a device, and can't re-create it unless it's never been used. |
| 530 | // TODO(jmadill): Handle the re-creation case if necessary. |
| 531 | if (mDevice != VK_NULL_HANDLE) |
| 532 | { |
| 533 | ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max()); |
| 534 | |
| 535 | // Check if the current device supports present on this surface. |
| 536 | VkBool32 supportsPresent = VK_FALSE; |
| 537 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex, |
| 538 | surface, &supportsPresent)); |
| 539 | |
| 540 | return (supportsPresent == VK_TRUE); |
| 541 | } |
| 542 | |
| 543 | // Find a graphics and present queue. |
| 544 | Optional<uint32_t> newPresentQueue; |
| 545 | uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size()); |
| 546 | for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex) |
| 547 | { |
| 548 | const auto &queueInfo = mQueueFamilyProperties[queueIndex]; |
| 549 | if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) |
| 550 | { |
| 551 | VkBool32 supportsPresent = VK_FALSE; |
| 552 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface, |
| 553 | &supportsPresent)); |
| 554 | |
| 555 | if (supportsPresent == VK_TRUE) |
| 556 | { |
| 557 | newPresentQueue = queueIndex; |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED); |
| 564 | ANGLE_TRY(initializeDevice(newPresentQueue.value())); |
| 565 | |
| 566 | return newPresentQueue.value(); |
| 567 | } |
| 568 | |
| 569 | std::string RendererVk::getVendorString() const |
| 570 | { |
Olli Etuaho | c6a0618 | 2018-04-13 14:11:46 +0300 | [diff] [blame] | 571 | return GetVendorString(mPhysicalDeviceProperties.vendorID); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 572 | } |
| 573 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 574 | std::string RendererVk::getRendererDescription() const |
| 575 | { |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 576 | std::stringstream strstr; |
| 577 | |
| 578 | uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion; |
| 579 | |
| 580 | strstr << "Vulkan "; |
| 581 | strstr << VK_VERSION_MAJOR(apiVersion) << "."; |
| 582 | strstr << VK_VERSION_MINOR(apiVersion) << "."; |
| 583 | strstr << VK_VERSION_PATCH(apiVersion); |
| 584 | |
Olli Etuaho | c6a0618 | 2018-04-13 14:11:46 +0300 | [diff] [blame] | 585 | strstr << "("; |
| 586 | |
| 587 | // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that |
| 588 | // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be |
| 589 | // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated |
| 590 | // driver detection. |
| 591 | if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA) |
| 592 | { |
| 593 | strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " "; |
| 594 | } |
| 595 | |
| 596 | strstr << mPhysicalDeviceProperties.deviceName << ")"; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 597 | |
| 598 | return strstr.str(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 599 | } |
| 600 | |
Jamie Madill | acccc6c | 2016-05-03 17:22:10 -0400 | [diff] [blame] | 601 | void RendererVk::ensureCapsInitialized() const |
| 602 | { |
| 603 | if (!mCapsInitialized) |
| 604 | { |
Luc Ferron | d50537a | 2018-02-07 17:02:08 -0500 | [diff] [blame] | 605 | vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps, |
Luc Ferron | e4741fd | 2018-01-25 13:25:27 -0500 | [diff] [blame] | 606 | &mNativeExtensions, &mNativeLimitations); |
Jamie Madill | acccc6c | 2016-05-03 17:22:10 -0400 | [diff] [blame] | 607 | mCapsInitialized = true; |
| 608 | } |
| 609 | } |
| 610 | |
Jamie Madill | acccc6c | 2016-05-03 17:22:10 -0400 | [diff] [blame] | 611 | const gl::Caps &RendererVk::getNativeCaps() const |
| 612 | { |
| 613 | ensureCapsInitialized(); |
| 614 | return mNativeCaps; |
| 615 | } |
| 616 | |
| 617 | const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const |
| 618 | { |
| 619 | ensureCapsInitialized(); |
| 620 | return mNativeTextureCaps; |
| 621 | } |
| 622 | |
| 623 | const gl::Extensions &RendererVk::getNativeExtensions() const |
| 624 | { |
| 625 | ensureCapsInitialized(); |
| 626 | return mNativeExtensions; |
| 627 | } |
| 628 | |
| 629 | const gl::Limitations &RendererVk::getNativeLimitations() const |
| 630 | { |
| 631 | ensureCapsInitialized(); |
| 632 | return mNativeLimitations; |
| 633 | } |
| 634 | |
Luc Ferron | daedf4d | 2018-03-16 09:28:53 -0400 | [diff] [blame] | 635 | uint32_t RendererVk::getMaxActiveTextures() |
| 636 | { |
| 637 | // TODO(lucferron): expose this limitation to GL in Context Caps |
| 638 | return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers, |
| 639 | gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES); |
| 640 | } |
| 641 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 642 | const vk::CommandPool &RendererVk::getCommandPool() const |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 643 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 644 | return mCommandPool; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 645 | } |
| 646 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 647 | vk::Error RendererVk::finish(const gl::Context *context) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 648 | { |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 649 | if (!mCommandGraph.empty()) |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 650 | { |
| 651 | vk::CommandBuffer commandBatch; |
| 652 | ANGLE_TRY(flushCommandGraph(context, &commandBatch)); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 653 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 654 | VkSubmitInfo submitInfo; |
| 655 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 656 | submitInfo.pNext = nullptr; |
| 657 | submitInfo.waitSemaphoreCount = 0; |
| 658 | submitInfo.pWaitSemaphores = nullptr; |
| 659 | submitInfo.pWaitDstStageMask = nullptr; |
| 660 | submitInfo.commandBufferCount = 1; |
| 661 | submitInfo.pCommandBuffers = commandBatch.ptr(); |
| 662 | submitInfo.signalSemaphoreCount = 0; |
| 663 | submitInfo.pSignalSemaphores = nullptr; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 664 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 665 | ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch))); |
| 666 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 667 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 668 | ASSERT(mQueue != VK_NULL_HANDLE); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 669 | ANGLE_VK_TRY(vkQueueWaitIdle(mQueue)); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 670 | freeAllInFlightResources(); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 671 | return vk::NoError(); |
| 672 | } |
| 673 | |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 674 | void RendererVk::freeAllInFlightResources() |
| 675 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 676 | for (CommandBatch &batch : mInFlightCommands) |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 677 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 678 | batch.fence.destroy(mDevice); |
| 679 | batch.commandPool.destroy(mDevice); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 680 | } |
| 681 | mInFlightCommands.clear(); |
| 682 | |
| 683 | for (auto &garbage : mGarbage) |
| 684 | { |
Jamie Madill | e88ec8e | 2017-10-31 17:18:14 -0400 | [diff] [blame] | 685 | garbage.destroy(mDevice); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 686 | } |
| 687 | mGarbage.clear(); |
| 688 | } |
| 689 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 690 | vk::Error RendererVk::checkInFlightCommands() |
| 691 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 692 | int finishedCount = 0; |
Jamie Madill | f651c77 | 2017-02-21 15:03:51 -0500 | [diff] [blame] | 693 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 694 | for (CommandBatch &batch : mInFlightCommands) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 695 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 696 | VkResult result = batch.fence.getStatus(mDevice); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 697 | if (result == VK_NOT_READY) |
| 698 | break; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 699 | |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 700 | ANGLE_VK_TRY(result); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 701 | ASSERT(batch.serial > mLastCompletedQueueSerial); |
| 702 | mLastCompletedQueueSerial = batch.serial; |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 703 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 704 | batch.fence.destroy(mDevice); |
| 705 | batch.commandPool.destroy(mDevice); |
| 706 | ++finishedCount; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 707 | } |
| 708 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 709 | mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 710 | |
| 711 | size_t freeIndex = 0; |
| 712 | for (; freeIndex < mGarbage.size(); ++freeIndex) |
| 713 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 714 | if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial)) |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 715 | break; |
| 716 | } |
| 717 | |
| 718 | // Remove the entries from the garbage list - they should be ready to go. |
| 719 | if (freeIndex > 0) |
| 720 | { |
| 721 | mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex); |
Jamie Madill | f651c77 | 2017-02-21 15:03:51 -0500 | [diff] [blame] | 722 | } |
| 723 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 724 | return vk::NoError(); |
| 725 | } |
| 726 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 727 | vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 728 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 729 | VkFenceCreateInfo fenceInfo; |
| 730 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 731 | fenceInfo.pNext = nullptr; |
| 732 | fenceInfo.flags = 0; |
| 733 | |
| 734 | CommandBatch batch; |
| 735 | ANGLE_TRY(batch.fence.init(mDevice, fenceInfo)); |
| 736 | |
| 737 | ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle())); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 738 | |
| 739 | // Store this command buffer in the in-flight list. |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 740 | batch.commandPool = std::move(mCommandPool); |
| 741 | batch.serial = mCurrentQueueSerial; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 742 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 743 | mInFlightCommands.emplace_back(std::move(batch)); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 744 | |
| 745 | // Sanity check. |
| 746 | ASSERT(mInFlightCommands.size() < 1000u); |
| 747 | |
| 748 | // Increment the queue serial. If this fails, we should restart ANGLE. |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 749 | // TODO(jmadill): Overflow check. |
| 750 | mCurrentQueueSerial = mQueueSerialFactory.generate(); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 751 | |
| 752 | ANGLE_TRY(checkInFlightCommands()); |
| 753 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 754 | // Simply null out the command buffer here - it was allocated using the command pool. |
| 755 | commandBuffer.releaseHandle(); |
| 756 | |
| 757 | // Reallocate the command pool for next frame. |
| 758 | // TODO(jmadill): Consider reusing command pools. |
| 759 | VkCommandPoolCreateInfo poolInfo; |
| 760 | poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 761 | poolInfo.pNext = nullptr; |
| 762 | poolInfo.flags = 0; |
| 763 | poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex; |
| 764 | |
| 765 | mCommandPool.init(mDevice, poolInfo); |
| 766 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 767 | return vk::NoError(); |
| 768 | } |
| 769 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 770 | GlslangWrapper *RendererVk::getGlslangWrapper() const |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 771 | { |
| 772 | return mGlslangWrapper; |
| 773 | } |
| 774 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 775 | Serial RendererVk::getCurrentQueueSerial() const |
| 776 | { |
| 777 | return mCurrentQueueSerial; |
| 778 | } |
| 779 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 780 | bool RendererVk::isSerialInUse(Serial serial) const |
Jamie Madill | 9776035 | 2017-11-09 13:08:29 -0500 | [diff] [blame] | 781 | { |
| 782 | return serial > mLastCompletedQueueSerial; |
| 783 | } |
| 784 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 785 | vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc, |
| 786 | vk::RenderPass **renderPassOut) |
| 787 | { |
| 788 | return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc, |
| 789 | renderPassOut); |
| 790 | } |
| 791 | |
Jamie Madill | bef918c | 2017-12-13 13:11:30 -0500 | [diff] [blame] | 792 | vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc, |
| 793 | const vk::AttachmentOpsArray &ops, |
| 794 | vk::RenderPass **renderPassOut) |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 795 | { |
Jamie Madill | bef918c | 2017-12-13 13:11:30 -0500 | [diff] [blame] | 796 | return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops, |
| 797 | renderPassOut); |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 798 | } |
| 799 | |
Jamie Madill | a5e0607 | 2018-05-18 14:36:05 -0400 | [diff] [blame] | 800 | vk::CommandGraph *RendererVk::getCommandGraph() |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 801 | { |
Jamie Madill | a5e0607 | 2018-05-18 14:36:05 -0400 | [diff] [blame] | 802 | return &mCommandGraph; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch) |
| 806 | { |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 807 | return mCommandGraph.submitCommands(mDevice, mCurrentQueueSerial, &mRenderPassCache, |
| 808 | &mCommandPool, commandBatch); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | vk::Error RendererVk::flush(const gl::Context *context, |
| 812 | const vk::Semaphore &waitSemaphore, |
| 813 | const vk::Semaphore &signalSemaphore) |
| 814 | { |
| 815 | vk::CommandBuffer commandBatch; |
| 816 | ANGLE_TRY(flushCommandGraph(context, &commandBatch)); |
| 817 | |
| 818 | VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 819 | |
| 820 | VkSubmitInfo submitInfo; |
| 821 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 822 | submitInfo.pNext = nullptr; |
| 823 | submitInfo.waitSemaphoreCount = 1; |
| 824 | submitInfo.pWaitSemaphores = waitSemaphore.ptr(); |
| 825 | submitInfo.pWaitDstStageMask = &waitStageMask; |
| 826 | submitInfo.commandBufferCount = 1; |
| 827 | submitInfo.pCommandBuffers = commandBatch.ptr(); |
| 828 | submitInfo.signalSemaphoreCount = 1; |
| 829 | submitInfo.pSignalSemaphores = signalSemaphore.ptr(); |
| 830 | |
| 831 | ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch))); |
| 832 | return vk::NoError(); |
| 833 | } |
| 834 | |
Jamie Madill | 78feddc | 2018-04-27 11:45:05 -0400 | [diff] [blame] | 835 | Serial RendererVk::issueShaderSerial() |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 836 | { |
Jamie Madill | 78feddc | 2018-04-27 11:45:05 -0400 | [diff] [blame] | 837 | return mShaderSerialFactory.generate(); |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 838 | } |
| 839 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 840 | vk::Error RendererVk::getAppPipeline(const ProgramVk *programVk, |
| 841 | const vk::PipelineDesc &desc, |
| 842 | const gl::AttributesMask &activeAttribLocationsMask, |
| 843 | vk::PipelineAndSerial **pipelineOut) |
Jamie Madill | ffa4cbb | 2018-01-23 13:04:07 -0500 | [diff] [blame] | 844 | { |
Jamie Madill | 33318de | 2018-05-01 11:22:54 -0400 | [diff] [blame] | 845 | ASSERT(programVk->getVertexModuleSerial() == |
| 846 | desc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial); |
| 847 | ASSERT(programVk->getFragmentModuleSerial() == |
| 848 | desc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial); |
Jamie Madill | ffa4cbb | 2018-01-23 13:04:07 -0500 | [diff] [blame] | 849 | |
| 850 | // Pull in a compatible RenderPass. |
| 851 | vk::RenderPass *compatibleRenderPass = nullptr; |
| 852 | ANGLE_TRY(getCompatibleRenderPass(desc.getRenderPassDesc(), &compatibleRenderPass)); |
| 853 | |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 854 | const vk::PipelineLayout &pipelineLayout = programVk->getPipelineLayout(); |
| 855 | |
Jamie Madill | c7918ce | 2018-06-13 13:25:31 -0400 | [diff] [blame] | 856 | return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, pipelineLayout, |
Luc Ferron | ceb7190 | 2018-02-05 15:18:47 -0500 | [diff] [blame] | 857 | activeAttribLocationsMask, programVk->getLinkedVertexModule(), |
Jamie Madill | ffa4cbb | 2018-01-23 13:04:07 -0500 | [diff] [blame] | 858 | programVk->getLinkedFragmentModule(), desc, pipelineOut); |
| 859 | } |
| 860 | |
Jamie Madill | 9aef367 | 2018-04-27 11:45:06 -0400 | [diff] [blame] | 861 | vk::Error RendererVk::getInternalPipeline(const vk::ShaderAndSerial &vertexShader, |
| 862 | const vk::ShaderAndSerial &fragmentShader, |
| 863 | const vk::PipelineLayout &pipelineLayout, |
| 864 | const vk::PipelineDesc &pipelineDesc, |
| 865 | const gl::AttributesMask &activeAttribLocationsMask, |
| 866 | vk::PipelineAndSerial **pipelineOut) |
| 867 | { |
| 868 | ASSERT(vertexShader.queueSerial() == |
| 869 | pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial); |
| 870 | ASSERT(fragmentShader.queueSerial() == |
| 871 | pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial); |
| 872 | |
| 873 | // Pull in a compatible RenderPass. |
| 874 | vk::RenderPass *compatibleRenderPass = nullptr; |
| 875 | ANGLE_TRY(getCompatibleRenderPass(pipelineDesc.getRenderPassDesc(), &compatibleRenderPass)); |
| 876 | |
| 877 | return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, pipelineLayout, |
| 878 | activeAttribLocationsMask, vertexShader.get(), |
| 879 | fragmentShader.get(), pipelineDesc, pipelineOut); |
| 880 | } |
| 881 | |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 882 | vk::Error RendererVk::getDescriptorSetLayout( |
| 883 | const vk::DescriptorSetLayoutDesc &desc, |
| 884 | vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut) |
| 885 | { |
| 886 | return mDescriptorSetLayoutCache.getDescriptorSetLayout(mDevice, desc, descriptorSetLayoutOut); |
| 887 | } |
| 888 | |
| 889 | vk::Error RendererVk::getPipelineLayout( |
| 890 | const vk::PipelineLayoutDesc &desc, |
| 891 | const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts, |
| 892 | vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut) |
| 893 | { |
| 894 | return mPipelineLayoutCache.getPipelineLayout(mDevice, desc, descriptorSetLayouts, |
| 895 | pipelineLayoutOut); |
| 896 | } |
| 897 | |
Jamie Madill | d47044a | 2018-04-27 11:45:03 -0400 | [diff] [blame] | 898 | vk::ShaderLibrary *RendererVk::getShaderLibrary() |
| 899 | { |
| 900 | return &mShaderLibrary; |
| 901 | } |
Luc Ferron | 9096836 | 2018-05-04 08:47:22 -0400 | [diff] [blame] | 902 | |
Jamie Madill | aaca96e | 2018-06-12 10:19:48 -0400 | [diff] [blame] | 903 | uint32_t GetUniformBufferDescriptorCount() |
| 904 | { |
| 905 | return kUniformBufferDescriptorsPerDescriptorSet; |
| 906 | } |
| 907 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 908 | } // namespace rx |