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