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