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