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