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 | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 20 | #include "libANGLE/renderer/vulkan/CommandBufferNode.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/TextureVk.h" |
| 26 | #include "libANGLE/renderer/vulkan/VertexArrayVk.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 | |
| 30 | namespace rx |
| 31 | { |
| 32 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 33 | namespace |
| 34 | { |
| 35 | |
| 36 | VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps, |
| 37 | const std::vector<const char *> &enabledExtensionNames) |
| 38 | { |
| 39 | // Compile the extensions names into a set. |
| 40 | std::set<std::string> extensionNames; |
| 41 | for (const auto &extensionProp : extensionProps) |
| 42 | { |
| 43 | extensionNames.insert(extensionProp.extensionName); |
| 44 | } |
| 45 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 46 | for (const char *extensionName : enabledExtensionNames) |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 47 | { |
| 48 | if (extensionNames.count(extensionName) == 0) |
| 49 | { |
| 50 | return VK_ERROR_EXTENSION_NOT_PRESENT; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return VK_SUCCESS; |
| 55 | } |
| 56 | |
Yuly Novikov | 2e551f6 | 2018-01-24 21:45:34 -0500 | [diff] [blame] | 57 | VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags, |
| 58 | VkDebugReportObjectTypeEXT objectType, |
| 59 | uint64_t object, |
| 60 | size_t location, |
| 61 | int32_t messageCode, |
| 62 | const char *layerPrefix, |
| 63 | const char *message, |
| 64 | void *userData) |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 65 | { |
| 66 | if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0) |
| 67 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 68 | ERR() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 69 | #if !defined(NDEBUG) |
| 70 | // Abort the call in Debug builds. |
| 71 | return VK_TRUE; |
| 72 | #endif |
| 73 | } |
| 74 | else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0) |
| 75 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 76 | WARN() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 77 | } |
| 78 | else |
| 79 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 80 | // Uncomment this if you want Vulkan spam. |
| 81 | // WARN() << message; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | return VK_FALSE; |
| 85 | } |
| 86 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 87 | } // anonymous namespace |
| 88 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 89 | // CommandBatch implementation. |
| 90 | RendererVk::CommandBatch::CommandBatch() |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | RendererVk::CommandBatch::~CommandBatch() |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | RendererVk::CommandBatch::CommandBatch(CommandBatch &&other) |
| 99 | : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial) |
| 100 | { |
| 101 | } |
| 102 | |
| 103 | RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other) |
| 104 | { |
| 105 | std::swap(commandPool, other.commandPool); |
| 106 | std::swap(fence, other.fence); |
| 107 | std::swap(serial, other.serial); |
| 108 | return *this; |
| 109 | } |
| 110 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 111 | // RendererVk implementation. |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 112 | RendererVk::RendererVk() |
| 113 | : mCapsInitialized(false), |
| 114 | mInstance(VK_NULL_HANDLE), |
| 115 | mEnableValidationLayers(false), |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 116 | mDebugReportCallback(VK_NULL_HANDLE), |
| 117 | mPhysicalDevice(VK_NULL_HANDLE), |
| 118 | mQueue(VK_NULL_HANDLE), |
| 119 | mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()), |
| 120 | mDevice(VK_NULL_HANDLE), |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 121 | mGlslangWrapper(nullptr), |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 122 | mLastCompletedQueueSerial(mQueueSerialFactory.generate()), |
| 123 | mCurrentQueueSerial(mQueueSerialFactory.generate()), |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 124 | mInFlightCommands() |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 125 | { |
| 126 | } |
| 127 | |
| 128 | RendererVk::~RendererVk() |
| 129 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 130 | if (!mInFlightCommands.empty() || !mGarbage.empty()) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 131 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 132 | // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem. |
| 133 | vk::Error error = finish(nullptr); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 134 | if (error.isError()) |
| 135 | { |
| 136 | ERR() << "Error during VK shutdown: " << error; |
| 137 | } |
| 138 | } |
| 139 | |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 140 | for (auto &descriptorSetLayout : mGraphicsDescriptorSetLayouts) |
| 141 | { |
| 142 | descriptorSetLayout.destroy(mDevice); |
| 143 | } |
| 144 | |
| 145 | mGraphicsPipelineLayout.destroy(mDevice); |
| 146 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 147 | mRenderPassCache.destroy(mDevice); |
Jamie Madill | ffa4cbb | 2018-01-23 13:04:07 -0500 | [diff] [blame^] | 148 | mPipelineCache.destroy(mDevice); |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 149 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 150 | if (mGlslangWrapper) |
| 151 | { |
| 152 | GlslangWrapper::ReleaseReference(); |
| 153 | mGlslangWrapper = nullptr; |
| 154 | } |
| 155 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 156 | if (mCommandPool.valid()) |
| 157 | { |
| 158 | mCommandPool.destroy(mDevice); |
| 159 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 160 | |
| 161 | if (mDevice) |
| 162 | { |
| 163 | vkDestroyDevice(mDevice, nullptr); |
| 164 | mDevice = VK_NULL_HANDLE; |
| 165 | } |
| 166 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 167 | if (mDebugReportCallback) |
| 168 | { |
| 169 | ASSERT(mInstance); |
| 170 | auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( |
| 171 | vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT")); |
| 172 | ASSERT(destroyDebugReportCallback); |
| 173 | destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr); |
| 174 | } |
| 175 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 176 | if (mInstance) |
| 177 | { |
| 178 | vkDestroyInstance(mInstance, nullptr); |
| 179 | mInstance = VK_NULL_HANDLE; |
| 180 | } |
| 181 | |
| 182 | mPhysicalDevice = VK_NULL_HANDLE; |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 183 | } |
| 184 | |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 185 | vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName) |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 186 | { |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 187 | mEnableValidationLayers = ShouldUseDebugLayers(attribs); |
Jamie Madill | a66779f | 2017-01-06 10:43:44 -0500 | [diff] [blame] | 188 | |
| 189 | // If we're loading the validation layers, we could be running from any random directory. |
| 190 | // Change to the executable directory so we can find the layers, then change back to the |
| 191 | // previous directory to be safe we don't disrupt the application. |
| 192 | std::string previousCWD; |
| 193 | |
| 194 | if (mEnableValidationLayers) |
| 195 | { |
| 196 | const auto &cwd = angle::GetCWD(); |
| 197 | if (!cwd.valid()) |
| 198 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 199 | ERR() << "Error getting CWD for Vulkan layers init."; |
Jamie Madill | a66779f | 2017-01-06 10:43:44 -0500 | [diff] [blame] | 200 | mEnableValidationLayers = false; |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | previousCWD = cwd.value(); |
Jamie Madill | b8bbbf9 | 2017-09-19 00:24:59 -0400 | [diff] [blame] | 205 | const char *exeDir = angle::GetExecutableDirectory(); |
| 206 | if (!angle::SetCWD(exeDir)) |
| 207 | { |
| 208 | ERR() << "Error setting CWD for Vulkan layers init."; |
| 209 | mEnableValidationLayers = false; |
| 210 | } |
Jamie Madill | a66779f | 2017-01-06 10:43:44 -0500 | [diff] [blame] | 211 | } |
Jamie Madill | b8bbbf9 | 2017-09-19 00:24:59 -0400 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // Override environment variable to use the ANGLE layers. |
| 215 | if (mEnableValidationLayers) |
| 216 | { |
| 217 | if (!angle::SetEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_LAYERS_DIR)) |
| 218 | { |
| 219 | ERR() << "Error setting environment for Vulkan layers init."; |
| 220 | mEnableValidationLayers = false; |
| 221 | } |
Jamie Madill | a66779f | 2017-01-06 10:43:44 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 224 | // Gather global layer properties. |
| 225 | uint32_t instanceLayerCount = 0; |
| 226 | ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr)); |
| 227 | |
| 228 | std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount); |
| 229 | if (instanceLayerCount > 0) |
| 230 | { |
| 231 | ANGLE_VK_TRY( |
| 232 | vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data())); |
| 233 | } |
| 234 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 235 | uint32_t instanceExtensionCount = 0; |
| 236 | ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr)); |
| 237 | |
| 238 | std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount); |
| 239 | if (instanceExtensionCount > 0) |
| 240 | { |
| 241 | ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, |
| 242 | instanceExtensionProps.data())); |
| 243 | } |
| 244 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 245 | if (mEnableValidationLayers) |
| 246 | { |
| 247 | // Verify the standard validation layers are available. |
| 248 | if (!HasStandardValidationLayer(instanceLayerProps)) |
| 249 | { |
| 250 | // Generate an error if the attribute was requested, warning otherwise. |
Jamie Madill | 222c517 | 2017-07-19 16:15:42 -0400 | [diff] [blame] | 251 | if (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == |
| 252 | EGL_TRUE) |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 253 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 254 | ERR() << "Vulkan standard validation layers are missing."; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 255 | } |
| 256 | else |
| 257 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 258 | WARN() << "Vulkan standard validation layers are missing."; |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 259 | } |
| 260 | mEnableValidationLayers = false; |
| 261 | } |
| 262 | } |
| 263 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 264 | std::vector<const char *> enabledInstanceExtensions; |
| 265 | enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); |
Frank Henigman | 29f148b | 2016-11-23 21:05:36 -0500 | [diff] [blame] | 266 | enabledInstanceExtensions.push_back(wsiName); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 267 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 268 | // TODO(jmadill): Should be able to continue initialization if debug report ext missing. |
| 269 | if (mEnableValidationLayers) |
| 270 | { |
| 271 | enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); |
| 272 | } |
| 273 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 274 | // Verify the required extensions are in the extension names set. Fail if not. |
| 275 | ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions)); |
| 276 | |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 277 | VkApplicationInfo applicationInfo; |
| 278 | applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; |
| 279 | applicationInfo.pNext = nullptr; |
| 280 | applicationInfo.pApplicationName = "ANGLE"; |
| 281 | applicationInfo.applicationVersion = 1; |
| 282 | applicationInfo.pEngineName = "ANGLE"; |
| 283 | applicationInfo.engineVersion = 1; |
| 284 | applicationInfo.apiVersion = VK_API_VERSION_1_0; |
| 285 | |
| 286 | VkInstanceCreateInfo instanceInfo; |
| 287 | instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; |
| 288 | instanceInfo.pNext = nullptr; |
| 289 | instanceInfo.flags = 0; |
| 290 | instanceInfo.pApplicationInfo = &applicationInfo; |
| 291 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 292 | // Enable requested layers and extensions. |
| 293 | instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size()); |
| 294 | instanceInfo.ppEnabledExtensionNames = |
| 295 | enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data(); |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 296 | instanceInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u; |
| 297 | instanceInfo.ppEnabledLayerNames = |
| 298 | mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr; |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 299 | |
| 300 | ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance)); |
| 301 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 302 | if (mEnableValidationLayers) |
| 303 | { |
Jamie Madill | a66779f | 2017-01-06 10:43:44 -0500 | [diff] [blame] | 304 | // Change back to the previous working directory now that we've loaded the instance - |
| 305 | // the validation layers should be loaded at this point. |
| 306 | angle::SetCWD(previousCWD.c_str()); |
| 307 | |
Jamie Madill | 0448ec8 | 2016-12-23 13:41:47 -0500 | [diff] [blame] | 308 | VkDebugReportCallbackCreateInfoEXT debugReportInfo; |
| 309 | |
| 310 | debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
| 311 | debugReportInfo.pNext = nullptr; |
| 312 | debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | |
| 313 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT | |
| 314 | VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT; |
| 315 | debugReportInfo.pfnCallback = &DebugReportCallback; |
| 316 | debugReportInfo.pUserData = this; |
| 317 | |
| 318 | auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>( |
| 319 | vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT")); |
| 320 | ASSERT(createDebugReportCallback); |
| 321 | ANGLE_VK_TRY( |
| 322 | createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback)); |
| 323 | } |
| 324 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 325 | uint32_t physicalDeviceCount = 0; |
| 326 | ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr)); |
| 327 | ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
| 328 | |
| 329 | // TODO(jmadill): Handle multiple physical devices. For now, use the first device. |
| 330 | physicalDeviceCount = 1; |
| 331 | ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, &mPhysicalDevice)); |
| 332 | |
| 333 | vkGetPhysicalDeviceProperties(mPhysicalDevice, &mPhysicalDeviceProperties); |
| 334 | |
| 335 | // Ensure we can find a graphics queue family. |
| 336 | uint32_t queueCount = 0; |
| 337 | vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr); |
| 338 | |
| 339 | ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
| 340 | |
| 341 | mQueueFamilyProperties.resize(queueCount); |
| 342 | vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, |
| 343 | mQueueFamilyProperties.data()); |
| 344 | |
| 345 | size_t graphicsQueueFamilyCount = false; |
| 346 | uint32_t firstGraphicsQueueFamily = 0; |
| 347 | for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex) |
| 348 | { |
| 349 | const auto &queueInfo = mQueueFamilyProperties[familyIndex]; |
| 350 | if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) |
| 351 | { |
| 352 | ASSERT(queueInfo.queueCount > 0); |
| 353 | graphicsQueueFamilyCount++; |
| 354 | if (firstGraphicsQueueFamily == 0) |
| 355 | { |
| 356 | firstGraphicsQueueFamily = familyIndex; |
| 357 | } |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED); |
| 363 | |
| 364 | // If only one queue family, go ahead and initialize the device. If there is more than one |
| 365 | // queue, we'll have to wait until we see a WindowSurface to know which supports present. |
| 366 | if (graphicsQueueFamilyCount == 1) |
| 367 | { |
| 368 | ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily)); |
| 369 | } |
| 370 | |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 371 | // Store the physical device memory properties so we can find the right memory pools. |
| 372 | mMemoryProperties.init(mPhysicalDevice); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 373 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 374 | mGlslangWrapper = GlslangWrapper::GetReference(); |
| 375 | |
Jamie Madill | 6a89d22 | 2017-11-02 11:59:51 -0400 | [diff] [blame] | 376 | // Initialize the format table. |
| 377 | mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps); |
| 378 | |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 379 | // Initialize the pipeline layout for GL programs. |
| 380 | ANGLE_TRY(initGraphicsPipelineLayout()); |
| 381 | |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 382 | return vk::NoError(); |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 383 | } |
| 384 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 385 | vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex) |
| 386 | { |
| 387 | uint32_t deviceLayerCount = 0; |
| 388 | ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr)); |
| 389 | |
| 390 | std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount); |
| 391 | if (deviceLayerCount > 0) |
| 392 | { |
| 393 | ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, |
| 394 | deviceLayerProps.data())); |
| 395 | } |
| 396 | |
| 397 | uint32_t deviceExtensionCount = 0; |
| 398 | ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr, |
| 399 | &deviceExtensionCount, nullptr)); |
| 400 | |
| 401 | std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount); |
| 402 | if (deviceExtensionCount > 0) |
| 403 | { |
| 404 | ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties( |
| 405 | mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data())); |
| 406 | } |
| 407 | |
| 408 | if (mEnableValidationLayers) |
| 409 | { |
| 410 | if (!HasStandardValidationLayer(deviceLayerProps)) |
| 411 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 412 | WARN() << "Vulkan standard validation layer is missing."; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 413 | mEnableValidationLayers = false; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | std::vector<const char *> enabledDeviceExtensions; |
| 418 | enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); |
| 419 | |
| 420 | ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions)); |
| 421 | |
| 422 | VkDeviceQueueCreateInfo queueCreateInfo; |
| 423 | |
| 424 | float zeroPriority = 0.0f; |
| 425 | |
| 426 | queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; |
| 427 | queueCreateInfo.pNext = nullptr; |
| 428 | queueCreateInfo.flags = 0; |
| 429 | queueCreateInfo.queueFamilyIndex = queueFamilyIndex; |
| 430 | queueCreateInfo.queueCount = 1; |
| 431 | queueCreateInfo.pQueuePriorities = &zeroPriority; |
| 432 | |
| 433 | // Initialize the device |
| 434 | VkDeviceCreateInfo createInfo; |
| 435 | |
| 436 | createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; |
| 437 | createInfo.pNext = nullptr; |
| 438 | createInfo.flags = 0; |
| 439 | createInfo.queueCreateInfoCount = 1; |
| 440 | createInfo.pQueueCreateInfos = &queueCreateInfo; |
| 441 | createInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u; |
| 442 | createInfo.ppEnabledLayerNames = |
| 443 | mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr; |
| 444 | createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size()); |
| 445 | createInfo.ppEnabledExtensionNames = |
| 446 | enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data(); |
| 447 | createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features |
| 448 | |
| 449 | ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice)); |
| 450 | |
| 451 | mCurrentQueueFamilyIndex = queueFamilyIndex; |
| 452 | |
| 453 | vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue); |
| 454 | |
| 455 | // Initialize the command pool now that we know the queue family index. |
| 456 | VkCommandPoolCreateInfo commandPoolInfo; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 457 | commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 458 | commandPoolInfo.pNext = nullptr; |
| 459 | commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 460 | commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex; |
| 461 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 462 | ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo)); |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 463 | |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 464 | return vk::NoError(); |
| 465 | } |
| 466 | |
| 467 | vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface) |
| 468 | { |
| 469 | // We've already initialized a device, and can't re-create it unless it's never been used. |
| 470 | // TODO(jmadill): Handle the re-creation case if necessary. |
| 471 | if (mDevice != VK_NULL_HANDLE) |
| 472 | { |
| 473 | ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max()); |
| 474 | |
| 475 | // Check if the current device supports present on this surface. |
| 476 | VkBool32 supportsPresent = VK_FALSE; |
| 477 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex, |
| 478 | surface, &supportsPresent)); |
| 479 | |
| 480 | return (supportsPresent == VK_TRUE); |
| 481 | } |
| 482 | |
| 483 | // Find a graphics and present queue. |
| 484 | Optional<uint32_t> newPresentQueue; |
| 485 | uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size()); |
| 486 | for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex) |
| 487 | { |
| 488 | const auto &queueInfo = mQueueFamilyProperties[queueIndex]; |
| 489 | if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) |
| 490 | { |
| 491 | VkBool32 supportsPresent = VK_FALSE; |
| 492 | ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface, |
| 493 | &supportsPresent)); |
| 494 | |
| 495 | if (supportsPresent == VK_TRUE) |
| 496 | { |
| 497 | newPresentQueue = queueIndex; |
| 498 | break; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED); |
| 504 | ANGLE_TRY(initializeDevice(newPresentQueue.value())); |
| 505 | |
| 506 | return newPresentQueue.value(); |
| 507 | } |
| 508 | |
| 509 | std::string RendererVk::getVendorString() const |
| 510 | { |
| 511 | switch (mPhysicalDeviceProperties.vendorID) |
| 512 | { |
| 513 | case VENDOR_ID_AMD: |
| 514 | return "Advanced Micro Devices"; |
| 515 | case VENDOR_ID_NVIDIA: |
| 516 | return "NVIDIA"; |
| 517 | case VENDOR_ID_INTEL: |
| 518 | return "Intel"; |
| 519 | default: |
| 520 | { |
| 521 | // TODO(jmadill): More vendor IDs. |
| 522 | std::stringstream strstr; |
| 523 | strstr << "Vendor ID: " << mPhysicalDeviceProperties.vendorID; |
| 524 | return strstr.str(); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 529 | std::string RendererVk::getRendererDescription() const |
| 530 | { |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 531 | std::stringstream strstr; |
| 532 | |
| 533 | uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion; |
| 534 | |
| 535 | strstr << "Vulkan "; |
| 536 | strstr << VK_VERSION_MAJOR(apiVersion) << "."; |
| 537 | strstr << VK_VERSION_MINOR(apiVersion) << "."; |
| 538 | strstr << VK_VERSION_PATCH(apiVersion); |
| 539 | |
| 540 | strstr << "(" << mPhysicalDeviceProperties.deviceName << ")"; |
| 541 | |
| 542 | return strstr.str(); |
Jamie Madill | e09bd5d | 2016-11-29 16:20:35 -0500 | [diff] [blame] | 543 | } |
| 544 | |
Jamie Madill | acccc6c | 2016-05-03 17:22:10 -0400 | [diff] [blame] | 545 | void RendererVk::ensureCapsInitialized() const |
| 546 | { |
| 547 | if (!mCapsInitialized) |
| 548 | { |
| 549 | generateCaps(&mNativeCaps, &mNativeTextureCaps, &mNativeExtensions, &mNativeLimitations); |
| 550 | mCapsInitialized = true; |
| 551 | } |
| 552 | } |
| 553 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 554 | void RendererVk::generateCaps(gl::Caps *outCaps, |
Jamie Madill | acccc6c | 2016-05-03 17:22:10 -0400 | [diff] [blame] | 555 | gl::TextureCapsMap * /*outTextureCaps*/, |
Jamie Madill | b8353b0 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 556 | gl::Extensions *outExtensions, |
Jamie Madill | acccc6c | 2016-05-03 17:22:10 -0400 | [diff] [blame] | 557 | gl::Limitations * /* outLimitations */) const |
| 558 | { |
Jamie Madill | 327ba85 | 2016-11-30 12:38:28 -0500 | [diff] [blame] | 559 | // TODO(jmadill): Caps. |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 560 | outCaps->maxDrawBuffers = 1; |
Jiawei-Shao | 2597fb6 | 2016-12-09 16:38:02 +0800 | [diff] [blame] | 561 | outCaps->maxVertexAttributes = gl::MAX_VERTEX_ATTRIBS; |
| 562 | outCaps->maxVertexAttribBindings = gl::MAX_VERTEX_ATTRIB_BINDINGS; |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 563 | outCaps->maxVaryingVectors = 16; |
| 564 | outCaps->maxTextureImageUnits = 1; |
| 565 | outCaps->maxCombinedTextureImageUnits = 1; |
| 566 | outCaps->max2DTextureSize = 1024; |
Jamie Madill | d03a849 | 2017-10-03 15:46:06 -0400 | [diff] [blame] | 567 | outCaps->maxElementIndex = std::numeric_limits<GLuint>::max() - 1; |
Jamie Madill | 6276b92 | 2017-09-25 02:35:57 -0400 | [diff] [blame] | 568 | outCaps->maxFragmentUniformVectors = 8; |
| 569 | outCaps->maxVertexUniformVectors = 8; |
Jamie Madill | b79e7bb | 2017-10-24 13:55:50 -0400 | [diff] [blame] | 570 | outCaps->maxColorAttachments = 1; |
Jamie Madill | b8353b0 | 2017-01-25 12:57:21 -0800 | [diff] [blame] | 571 | |
| 572 | // Enable this for simple buffer readback testing, but some functionality is missing. |
| 573 | // TODO(jmadill): Support full mapBufferRange extension. |
| 574 | outExtensions->mapBuffer = true; |
| 575 | outExtensions->mapBufferRange = true; |
Jamie Madill | acccc6c | 2016-05-03 17:22:10 -0400 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | const gl::Caps &RendererVk::getNativeCaps() const |
| 579 | { |
| 580 | ensureCapsInitialized(); |
| 581 | return mNativeCaps; |
| 582 | } |
| 583 | |
| 584 | const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const |
| 585 | { |
| 586 | ensureCapsInitialized(); |
| 587 | return mNativeTextureCaps; |
| 588 | } |
| 589 | |
| 590 | const gl::Extensions &RendererVk::getNativeExtensions() const |
| 591 | { |
| 592 | ensureCapsInitialized(); |
| 593 | return mNativeExtensions; |
| 594 | } |
| 595 | |
| 596 | const gl::Limitations &RendererVk::getNativeLimitations() const |
| 597 | { |
| 598 | ensureCapsInitialized(); |
| 599 | return mNativeLimitations; |
| 600 | } |
| 601 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 602 | const vk::CommandPool &RendererVk::getCommandPool() const |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 603 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 604 | return mCommandPool; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 605 | } |
| 606 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 607 | vk::Error RendererVk::finish(const gl::Context *context) |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 608 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 609 | if (!mOpenCommandGraph.empty()) |
| 610 | { |
| 611 | vk::CommandBuffer commandBatch; |
| 612 | ANGLE_TRY(flushCommandGraph(context, &commandBatch)); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 613 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 614 | VkSubmitInfo submitInfo; |
| 615 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 616 | submitInfo.pNext = nullptr; |
| 617 | submitInfo.waitSemaphoreCount = 0; |
| 618 | submitInfo.pWaitSemaphores = nullptr; |
| 619 | submitInfo.pWaitDstStageMask = nullptr; |
| 620 | submitInfo.commandBufferCount = 1; |
| 621 | submitInfo.pCommandBuffers = commandBatch.ptr(); |
| 622 | submitInfo.signalSemaphoreCount = 0; |
| 623 | submitInfo.pSignalSemaphores = nullptr; |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 624 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 625 | ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch))); |
| 626 | } |
Jamie Madill | 4d0bf55 | 2016-12-28 15:45:24 -0500 | [diff] [blame] | 627 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 628 | ASSERT(mQueue != VK_NULL_HANDLE); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 629 | ANGLE_VK_TRY(vkQueueWaitIdle(mQueue)); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 630 | freeAllInFlightResources(); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 631 | return vk::NoError(); |
| 632 | } |
| 633 | |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 634 | void RendererVk::freeAllInFlightResources() |
| 635 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 636 | for (CommandBatch &batch : mInFlightCommands) |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 637 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 638 | batch.fence.destroy(mDevice); |
| 639 | batch.commandPool.destroy(mDevice); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 640 | } |
| 641 | mInFlightCommands.clear(); |
| 642 | |
| 643 | for (auto &garbage : mGarbage) |
| 644 | { |
Jamie Madill | e88ec8e | 2017-10-31 17:18:14 -0400 | [diff] [blame] | 645 | garbage.destroy(mDevice); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 646 | } |
| 647 | mGarbage.clear(); |
| 648 | } |
| 649 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 650 | vk::Error RendererVk::checkInFlightCommands() |
| 651 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 652 | int finishedCount = 0; |
Jamie Madill | f651c77 | 2017-02-21 15:03:51 -0500 | [diff] [blame] | 653 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 654 | for (CommandBatch &batch : mInFlightCommands) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 655 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 656 | VkResult result = batch.fence.getStatus(mDevice); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 657 | if (result == VK_NOT_READY) |
| 658 | break; |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 659 | |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 660 | ANGLE_VK_TRY(result); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 661 | ASSERT(batch.serial > mLastCompletedQueueSerial); |
| 662 | mLastCompletedQueueSerial = batch.serial; |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 663 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 664 | batch.fence.destroy(mDevice); |
| 665 | batch.commandPool.destroy(mDevice); |
| 666 | ++finishedCount; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 667 | } |
| 668 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 669 | mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 670 | |
| 671 | size_t freeIndex = 0; |
| 672 | for (; freeIndex < mGarbage.size(); ++freeIndex) |
| 673 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 674 | if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial)) |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 675 | break; |
| 676 | } |
| 677 | |
| 678 | // Remove the entries from the garbage list - they should be ready to go. |
| 679 | if (freeIndex > 0) |
| 680 | { |
| 681 | mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex); |
Jamie Madill | f651c77 | 2017-02-21 15:03:51 -0500 | [diff] [blame] | 682 | } |
| 683 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 684 | return vk::NoError(); |
| 685 | } |
| 686 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 687 | vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer) |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 688 | { |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 689 | VkFenceCreateInfo fenceInfo; |
| 690 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 691 | fenceInfo.pNext = nullptr; |
| 692 | fenceInfo.flags = 0; |
| 693 | |
| 694 | CommandBatch batch; |
| 695 | ANGLE_TRY(batch.fence.init(mDevice, fenceInfo)); |
| 696 | |
| 697 | ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle())); |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 698 | |
| 699 | // Store this command buffer in the in-flight list. |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 700 | batch.commandPool = std::move(mCommandPool); |
| 701 | batch.serial = mCurrentQueueSerial; |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 702 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 703 | mInFlightCommands.emplace_back(std::move(batch)); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 704 | |
| 705 | // Sanity check. |
| 706 | ASSERT(mInFlightCommands.size() < 1000u); |
| 707 | |
| 708 | // Increment the queue serial. If this fails, we should restart ANGLE. |
Jamie Madill | fb05bcb | 2017-06-07 15:43:18 -0400 | [diff] [blame] | 709 | // TODO(jmadill): Overflow check. |
| 710 | mCurrentQueueSerial = mQueueSerialFactory.generate(); |
Jamie Madill | 0c0dc34 | 2017-03-24 14:18:51 -0400 | [diff] [blame] | 711 | |
| 712 | ANGLE_TRY(checkInFlightCommands()); |
| 713 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 714 | // Simply null out the command buffer here - it was allocated using the command pool. |
| 715 | commandBuffer.releaseHandle(); |
| 716 | |
| 717 | // Reallocate the command pool for next frame. |
| 718 | // TODO(jmadill): Consider reusing command pools. |
| 719 | VkCommandPoolCreateInfo poolInfo; |
| 720 | poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 721 | poolInfo.pNext = nullptr; |
| 722 | poolInfo.flags = 0; |
| 723 | poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex; |
| 724 | |
| 725 | mCommandPool.init(mDevice, poolInfo); |
| 726 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 727 | return vk::NoError(); |
| 728 | } |
| 729 | |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 730 | vk::Error RendererVk::createStagingImage(TextureDimension dimension, |
| 731 | const vk::Format &format, |
| 732 | const gl::Extents &extent, |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 733 | vk::StagingUsage usage, |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 734 | vk::StagingImage *imageOut) |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 735 | { |
Jamie Madill | 035fd6b | 2017-10-03 15:43:22 -0400 | [diff] [blame] | 736 | ANGLE_TRY(imageOut->init(mDevice, mCurrentQueueFamilyIndex, mMemoryProperties, dimension, |
Jamie Madill | 1d7be50 | 2017-10-29 18:06:50 -0400 | [diff] [blame] | 737 | format.vkTextureFormat, extent, usage)); |
Jamie Madill | 5deea72 | 2017-02-16 10:44:46 -0500 | [diff] [blame] | 738 | return vk::NoError(); |
Jamie Madill | 7b57b9d | 2017-01-13 09:33:38 -0500 | [diff] [blame] | 739 | } |
| 740 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 741 | GlslangWrapper *RendererVk::getGlslangWrapper() |
| 742 | { |
| 743 | return mGlslangWrapper; |
| 744 | } |
| 745 | |
Jamie Madill | 4c26fc2 | 2017-02-24 11:04:10 -0500 | [diff] [blame] | 746 | Serial RendererVk::getCurrentQueueSerial() const |
| 747 | { |
| 748 | return mCurrentQueueSerial; |
| 749 | } |
| 750 | |
Jamie Madill | 9776035 | 2017-11-09 13:08:29 -0500 | [diff] [blame] | 751 | bool RendererVk::isResourceInUse(const ResourceVk &resource) |
| 752 | { |
| 753 | return isSerialInUse(resource.getQueueSerial()); |
| 754 | } |
| 755 | |
| 756 | bool RendererVk::isSerialInUse(Serial serial) |
| 757 | { |
| 758 | return serial > mLastCompletedQueueSerial; |
| 759 | } |
| 760 | |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 761 | vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc, |
| 762 | vk::RenderPass **renderPassOut) |
| 763 | { |
| 764 | return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc, |
| 765 | renderPassOut); |
| 766 | } |
| 767 | |
Jamie Madill | bef918c | 2017-12-13 13:11:30 -0500 | [diff] [blame] | 768 | vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc, |
| 769 | const vk::AttachmentOpsArray &ops, |
| 770 | vk::RenderPass **renderPassOut) |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 771 | { |
Jamie Madill | bef918c | 2017-12-13 13:11:30 -0500 | [diff] [blame] | 772 | return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops, |
| 773 | renderPassOut); |
Jamie Madill | 9f2a861 | 2017-11-30 12:43:09 -0500 | [diff] [blame] | 774 | } |
| 775 | |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 776 | vk::CommandBufferNode *RendererVk::allocateCommandNode() |
| 777 | { |
| 778 | // TODO(jmadill): Use a pool allocator for the CPU node allocations. |
| 779 | vk::CommandBufferNode *newCommands = new vk::CommandBufferNode(); |
| 780 | mOpenCommandGraph.emplace_back(newCommands); |
| 781 | return newCommands; |
| 782 | } |
| 783 | |
| 784 | vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch) |
| 785 | { |
| 786 | VkCommandBufferAllocateInfo primaryInfo; |
| 787 | primaryInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 788 | primaryInfo.pNext = nullptr; |
| 789 | primaryInfo.commandPool = mCommandPool.getHandle(); |
| 790 | primaryInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 791 | primaryInfo.commandBufferCount = 1; |
| 792 | |
| 793 | ANGLE_TRY(commandBatch->init(mDevice, primaryInfo)); |
| 794 | |
| 795 | if (mOpenCommandGraph.empty()) |
| 796 | { |
| 797 | return vk::NoError(); |
| 798 | } |
| 799 | |
| 800 | std::vector<vk::CommandBufferNode *> nodeStack; |
| 801 | |
| 802 | VkCommandBufferBeginInfo beginInfo; |
| 803 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 804 | beginInfo.pNext = nullptr; |
| 805 | beginInfo.flags = 0; |
| 806 | beginInfo.pInheritanceInfo = nullptr; |
| 807 | |
| 808 | ANGLE_TRY(commandBatch->begin(beginInfo)); |
| 809 | |
| 810 | for (vk::CommandBufferNode *topLevelNode : mOpenCommandGraph) |
| 811 | { |
| 812 | // Only process commands that don't have child commands. The others will be pulled in |
| 813 | // automatically. Also skip commands that have already been visited. |
| 814 | if (topLevelNode->isDependency() || |
| 815 | topLevelNode->visitedState() != vk::VisitedState::Unvisited) |
| 816 | continue; |
| 817 | |
| 818 | nodeStack.push_back(topLevelNode); |
| 819 | |
| 820 | while (!nodeStack.empty()) |
| 821 | { |
| 822 | vk::CommandBufferNode *node = nodeStack.back(); |
| 823 | |
| 824 | switch (node->visitedState()) |
| 825 | { |
| 826 | case vk::VisitedState::Unvisited: |
| 827 | node->visitDependencies(&nodeStack); |
| 828 | break; |
| 829 | case vk::VisitedState::Ready: |
| 830 | ANGLE_TRY(node->visitAndExecute(this, commandBatch)); |
| 831 | nodeStack.pop_back(); |
| 832 | break; |
| 833 | case vk::VisitedState::Visited: |
| 834 | nodeStack.pop_back(); |
| 835 | break; |
| 836 | default: |
| 837 | UNREACHABLE(); |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | ANGLE_TRY(commandBatch->end()); |
Jamie Madill | 97f39b3 | 2018-01-05 13:14:29 -0500 | [diff] [blame] | 844 | resetCommandGraph(); |
Jamie Madill | 49ac74b | 2017-12-21 14:42:33 -0500 | [diff] [blame] | 845 | return vk::NoError(); |
| 846 | } |
| 847 | |
| 848 | void RendererVk::resetCommandGraph() |
| 849 | { |
| 850 | // TODO(jmadill): Use pool allocation so we don't need to deallocate command graph. |
| 851 | for (vk::CommandBufferNode *node : mOpenCommandGraph) |
| 852 | { |
| 853 | delete node; |
| 854 | } |
| 855 | mOpenCommandGraph.clear(); |
| 856 | } |
| 857 | |
| 858 | vk::Error RendererVk::flush(const gl::Context *context, |
| 859 | const vk::Semaphore &waitSemaphore, |
| 860 | const vk::Semaphore &signalSemaphore) |
| 861 | { |
| 862 | vk::CommandBuffer commandBatch; |
| 863 | ANGLE_TRY(flushCommandGraph(context, &commandBatch)); |
| 864 | |
| 865 | VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 866 | |
| 867 | VkSubmitInfo submitInfo; |
| 868 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 869 | submitInfo.pNext = nullptr; |
| 870 | submitInfo.waitSemaphoreCount = 1; |
| 871 | submitInfo.pWaitSemaphores = waitSemaphore.ptr(); |
| 872 | submitInfo.pWaitDstStageMask = &waitStageMask; |
| 873 | submitInfo.commandBufferCount = 1; |
| 874 | submitInfo.pCommandBuffers = commandBatch.ptr(); |
| 875 | submitInfo.signalSemaphoreCount = 1; |
| 876 | submitInfo.pSignalSemaphores = signalSemaphore.ptr(); |
| 877 | |
| 878 | ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch))); |
| 879 | return vk::NoError(); |
| 880 | } |
| 881 | |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 882 | const vk::PipelineLayout &RendererVk::getGraphicsPipelineLayout() const |
| 883 | { |
| 884 | return mGraphicsPipelineLayout; |
| 885 | } |
| 886 | |
| 887 | const std::vector<vk::DescriptorSetLayout> &RendererVk::getGraphicsDescriptorSetLayouts() const |
| 888 | { |
| 889 | return mGraphicsDescriptorSetLayouts; |
| 890 | } |
| 891 | |
| 892 | vk::Error RendererVk::initGraphicsPipelineLayout() |
| 893 | { |
| 894 | ASSERT(!mGraphicsPipelineLayout.valid()); |
| 895 | |
| 896 | // Create two descriptor set layouts: one for default uniform info, and one for textures. |
| 897 | // Skip one or both if there are no uniforms. |
| 898 | VkDescriptorSetLayoutBinding uniformBindings[2]; |
| 899 | uint32_t blockCount = 0; |
| 900 | |
| 901 | { |
| 902 | auto &layoutBinding = uniformBindings[blockCount]; |
| 903 | |
| 904 | layoutBinding.binding = blockCount; |
| 905 | layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 906 | layoutBinding.descriptorCount = 1; |
| 907 | layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; |
| 908 | layoutBinding.pImmutableSamplers = nullptr; |
| 909 | |
| 910 | blockCount++; |
| 911 | } |
| 912 | |
| 913 | { |
| 914 | auto &layoutBinding = uniformBindings[blockCount]; |
| 915 | |
| 916 | layoutBinding.binding = blockCount; |
| 917 | layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 918 | layoutBinding.descriptorCount = 1; |
| 919 | layoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; |
| 920 | layoutBinding.pImmutableSamplers = nullptr; |
| 921 | |
| 922 | blockCount++; |
| 923 | } |
| 924 | |
| 925 | { |
| 926 | VkDescriptorSetLayoutCreateInfo uniformInfo; |
| 927 | uniformInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
| 928 | uniformInfo.pNext = nullptr; |
| 929 | uniformInfo.flags = 0; |
| 930 | uniformInfo.bindingCount = blockCount; |
| 931 | uniformInfo.pBindings = uniformBindings; |
| 932 | |
| 933 | vk::DescriptorSetLayout uniformLayout; |
| 934 | ANGLE_TRY(uniformLayout.init(mDevice, uniformInfo)); |
| 935 | mGraphicsDescriptorSetLayouts.push_back(std::move(uniformLayout)); |
| 936 | } |
| 937 | |
Yuly Novikov | 3796813 | 2018-01-23 18:19:29 -0500 | [diff] [blame] | 938 | // TODO(lucferron): expose this limitation to GL in Context Caps |
| 939 | std::vector<VkDescriptorSetLayoutBinding> textureBindings( |
| 940 | std::min<size_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers, |
| 941 | gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES)); |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 942 | |
| 943 | // TODO(jmadill): This approach might not work well for texture arrays. |
Yuly Novikov | 3796813 | 2018-01-23 18:19:29 -0500 | [diff] [blame] | 944 | for (uint32_t textureIndex = 0; textureIndex < textureBindings.size(); ++textureIndex) |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 945 | { |
| 946 | VkDescriptorSetLayoutBinding &layoutBinding = textureBindings[textureIndex]; |
| 947 | |
| 948 | layoutBinding.binding = textureIndex; |
| 949 | layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 950 | layoutBinding.descriptorCount = 1; |
| 951 | layoutBinding.stageFlags = (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT); |
| 952 | layoutBinding.pImmutableSamplers = nullptr; |
| 953 | } |
| 954 | |
| 955 | { |
| 956 | VkDescriptorSetLayoutCreateInfo textureInfo; |
| 957 | textureInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; |
| 958 | textureInfo.pNext = nullptr; |
| 959 | textureInfo.flags = 0; |
| 960 | textureInfo.bindingCount = static_cast<uint32_t>(textureBindings.size()); |
| 961 | textureInfo.pBindings = textureBindings.data(); |
| 962 | |
| 963 | vk::DescriptorSetLayout textureLayout; |
| 964 | ANGLE_TRY(textureLayout.init(mDevice, textureInfo)); |
| 965 | mGraphicsDescriptorSetLayouts.push_back(std::move(textureLayout)); |
| 966 | } |
| 967 | |
| 968 | VkPipelineLayoutCreateInfo createInfo; |
| 969 | createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; |
| 970 | createInfo.pNext = nullptr; |
| 971 | createInfo.flags = 0; |
| 972 | createInfo.setLayoutCount = static_cast<uint32_t>(mGraphicsDescriptorSetLayouts.size()); |
| 973 | createInfo.pSetLayouts = mGraphicsDescriptorSetLayouts[0].ptr(); |
| 974 | createInfo.pushConstantRangeCount = 0; |
| 975 | createInfo.pPushConstantRanges = nullptr; |
| 976 | |
| 977 | ANGLE_TRY(mGraphicsPipelineLayout.init(mDevice, createInfo)); |
| 978 | |
| 979 | return vk::NoError(); |
| 980 | } |
Jamie Madill | f2f6d37 | 2018-01-10 21:37:23 -0500 | [diff] [blame] | 981 | |
| 982 | Serial RendererVk::issueProgramSerial() |
| 983 | { |
| 984 | return mProgramSerialFactory.generate(); |
| 985 | } |
| 986 | |
Jamie Madill | ffa4cbb | 2018-01-23 13:04:07 -0500 | [diff] [blame^] | 987 | vk::Error RendererVk::getPipeline(const ProgramVk *programVk, |
| 988 | const vk::PipelineDesc &desc, |
| 989 | vk::PipelineAndSerial **pipelineOut) |
| 990 | { |
| 991 | ASSERT(programVk->getVertexModuleSerial() == desc.getShaderStageInfo()[0].moduleSerial); |
| 992 | ASSERT(programVk->getFragmentModuleSerial() == desc.getShaderStageInfo()[1].moduleSerial); |
| 993 | |
| 994 | // Pull in a compatible RenderPass. |
| 995 | vk::RenderPass *compatibleRenderPass = nullptr; |
| 996 | ANGLE_TRY(getCompatibleRenderPass(desc.getRenderPassDesc(), &compatibleRenderPass)); |
| 997 | |
| 998 | return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, mGraphicsPipelineLayout, |
| 999 | programVk->getLinkedVertexModule(), |
| 1000 | programVk->getLinkedFragmentModule(), desc, pipelineOut); |
| 1001 | } |
| 1002 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1003 | } // namespace rx |