blob: affb372bd6b27224e8b1236248d222f5e6668d2d [file] [log] [blame]
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001//
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 Madill4d0bf552016-12-28 15:45:24 -050012// Placing this first seems to solve an intellisense bug.
13#include "libANGLE/renderer/vulkan/renderervk_utils.h"
14
Jamie Madille09bd5d2016-11-29 16:20:35 -050015#include <EGL/eglext.h>
16
Jamie Madill9e54b5a2016-05-25 12:57:39 -040017#include "common/debug.h"
Jamie Madilla66779f2017-01-06 10:43:44 -050018#include "common/system_utils.h"
Jamie Madill4d0bf552016-12-28 15:45:24 -050019#include "libANGLE/renderer/driver_utils.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050020#include "libANGLE/renderer/vulkan/CompilerVk.h"
21#include "libANGLE/renderer/vulkan/FramebufferVk.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050022#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050023#include "libANGLE/renderer/vulkan/TextureVk.h"
24#include "libANGLE/renderer/vulkan/VertexArrayVk.h"
Jamie Madill7b57b9d2017-01-13 09:33:38 -050025#include "libANGLE/renderer/vulkan/formatutilsvk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050026#include "platform/Platform.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040027
28namespace rx
29{
30
Jamie Madille09bd5d2016-11-29 16:20:35 -050031namespace
32{
33
34VkResult 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 Madillacf2f3a2017-11-21 19:22:44 -050044 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050045 {
46 if (extensionNames.count(extensionName) == 0)
47 {
48 return VK_ERROR_EXTENSION_NOT_PRESENT;
49 }
50 }
51
52 return VK_SUCCESS;
53}
54
Jamie Madill0448ec82016-12-23 13:41:47 -050055VkBool32 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 Novikovbcb3f9b2017-01-27 22:45:18 -050066 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050067#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 Novikovbcb3f9b2017-01-27 22:45:18 -050074 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050075 }
76 else
77 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050078 // Uncomment this if you want Vulkan spam.
79 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050080 }
81
82 return VK_FALSE;
83}
84
Jamie Madille09bd5d2016-11-29 16:20:35 -050085} // anonymous namespace
86
Jamie Madill9f2a8612017-11-30 12:43:09 -050087// RenderPassCache implementation.
88RenderPassCache::RenderPassCache()
89{
90}
91
92RenderPassCache::~RenderPassCache()
93{
94 ASSERT(mPayload.empty());
95}
96
97void RenderPassCache::destroy(VkDevice device)
98{
99 for (auto &renderPassIt : mPayload)
100 {
101 renderPassIt.second.get().destroy(device);
102 }
103 mPayload.clear();
104}
105
106vk::Error RenderPassCache::getCompatibleRenderPass(VkDevice device,
107 Serial serial,
108 const vk::RenderPassDesc &desc,
109 vk::RenderPass **renderPassOut)
110{
111 // TODO(jmadill): Return compatible RenderPass when possible.
112 return getMatchingRenderPass(device, serial, desc, renderPassOut);
113}
114
115vk::Error RenderPassCache::getMatchingRenderPass(VkDevice device,
116 Serial serial,
117 const vk::RenderPassDesc &desc,
118 vk::RenderPass **renderPassOut)
119{
120 auto it = mPayload.find(desc);
121 if (it != mPayload.end())
122 {
123 // Update the serial before we return.
124 // TODO(jmadill): Could possibly use an MRU cache here.
125 it->second.updateSerial(serial);
126
127 *renderPassOut = &it->second.get();
128 return vk::NoError();
129 }
130
131 vk::RenderPass newRenderPass;
132 ANGLE_TRY(vk::InitializeRenderPassFromDesc(device, desc, &newRenderPass));
133
134 vk::RenderPassAndSerial withSerial(std::move(newRenderPass), serial);
135
136 auto insertPos = mPayload.emplace(desc, std::move(withSerial));
137 *renderPassOut = &insertPos.first->second.get();
138
139 // TODO(jmadill): Trim cache, and pre-populate with the most common RPs on startup.
140 return vk::NoError();
141}
142
143// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500144RendererVk::RendererVk()
145 : mCapsInitialized(false),
146 mInstance(VK_NULL_HANDLE),
147 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500148 mDebugReportCallback(VK_NULL_HANDLE),
149 mPhysicalDevice(VK_NULL_HANDLE),
150 mQueue(VK_NULL_HANDLE),
151 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
152 mDevice(VK_NULL_HANDLE),
Jamie Madill4c26fc22017-02-24 11:04:10 -0500153 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400154 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
155 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Jamie Madill1b038242017-11-01 15:14:36 -0400156 mInFlightCommands(),
157 mCurrentRenderPassFramebuffer(nullptr)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400158{
159}
160
161RendererVk::~RendererVk()
162{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400163 if (!mInFlightCommands.empty() || !mInFlightFences.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500164 {
165 vk::Error error = finish();
166 if (error.isError())
167 {
168 ERR() << "Error during VK shutdown: " << error;
169 }
170 }
171
Jamie Madill9f2a8612017-11-30 12:43:09 -0500172 mRenderPassCache.destroy(mDevice);
173
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500174 if (mGlslangWrapper)
175 {
176 GlslangWrapper::ReleaseReference();
177 mGlslangWrapper = nullptr;
178 }
179
Jamie Madill5deea722017-02-16 10:44:46 -0500180 if (mCommandBuffer.valid())
181 {
Jamie Madill7f738d42017-11-20 17:06:27 -0500182 mCommandBuffer.destroy(mDevice, mCommandPool);
Jamie Madill5deea722017-02-16 10:44:46 -0500183 }
184
185 if (mCommandPool.valid())
186 {
187 mCommandPool.destroy(mDevice);
188 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500189
190 if (mDevice)
191 {
192 vkDestroyDevice(mDevice, nullptr);
193 mDevice = VK_NULL_HANDLE;
194 }
195
Jamie Madill0448ec82016-12-23 13:41:47 -0500196 if (mDebugReportCallback)
197 {
198 ASSERT(mInstance);
199 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
200 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
201 ASSERT(destroyDebugReportCallback);
202 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
203 }
204
Jamie Madill4d0bf552016-12-28 15:45:24 -0500205 if (mInstance)
206 {
207 vkDestroyInstance(mInstance, nullptr);
208 mInstance = VK_NULL_HANDLE;
209 }
210
211 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500212}
213
Frank Henigman29f148b2016-11-23 21:05:36 -0500214vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500215{
Jamie Madill222c5172017-07-19 16:15:42 -0400216 mEnableValidationLayers = ShouldUseDebugLayers(attribs);
Jamie Madilla66779f2017-01-06 10:43:44 -0500217
218 // If we're loading the validation layers, we could be running from any random directory.
219 // Change to the executable directory so we can find the layers, then change back to the
220 // previous directory to be safe we don't disrupt the application.
221 std::string previousCWD;
222
223 if (mEnableValidationLayers)
224 {
225 const auto &cwd = angle::GetCWD();
226 if (!cwd.valid())
227 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500228 ERR() << "Error getting CWD for Vulkan layers init.";
Jamie Madilla66779f2017-01-06 10:43:44 -0500229 mEnableValidationLayers = false;
230 }
231 else
232 {
233 previousCWD = cwd.value();
Jamie Madillb8bbbf92017-09-19 00:24:59 -0400234 const char *exeDir = angle::GetExecutableDirectory();
235 if (!angle::SetCWD(exeDir))
236 {
237 ERR() << "Error setting CWD for Vulkan layers init.";
238 mEnableValidationLayers = false;
239 }
Jamie Madilla66779f2017-01-06 10:43:44 -0500240 }
Jamie Madillb8bbbf92017-09-19 00:24:59 -0400241 }
242
243 // Override environment variable to use the ANGLE layers.
244 if (mEnableValidationLayers)
245 {
246 if (!angle::SetEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_LAYERS_DIR))
247 {
248 ERR() << "Error setting environment for Vulkan layers init.";
249 mEnableValidationLayers = false;
250 }
Jamie Madilla66779f2017-01-06 10:43:44 -0500251 }
252
Jamie Madill0448ec82016-12-23 13:41:47 -0500253 // Gather global layer properties.
254 uint32_t instanceLayerCount = 0;
255 ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
256
257 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
258 if (instanceLayerCount > 0)
259 {
260 ANGLE_VK_TRY(
261 vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data()));
262 }
263
Jamie Madille09bd5d2016-11-29 16:20:35 -0500264 uint32_t instanceExtensionCount = 0;
265 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
266
267 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
268 if (instanceExtensionCount > 0)
269 {
270 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
271 instanceExtensionProps.data()));
272 }
273
Jamie Madill0448ec82016-12-23 13:41:47 -0500274 if (mEnableValidationLayers)
275 {
276 // Verify the standard validation layers are available.
277 if (!HasStandardValidationLayer(instanceLayerProps))
278 {
279 // Generate an error if the attribute was requested, warning otherwise.
Jamie Madill222c5172017-07-19 16:15:42 -0400280 if (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) ==
281 EGL_TRUE)
Jamie Madill0448ec82016-12-23 13:41:47 -0500282 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500283 ERR() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500284 }
285 else
286 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500287 WARN() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500288 }
289 mEnableValidationLayers = false;
290 }
291 }
292
Jamie Madille09bd5d2016-11-29 16:20:35 -0500293 std::vector<const char *> enabledInstanceExtensions;
294 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500295 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500296
Jamie Madill0448ec82016-12-23 13:41:47 -0500297 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
298 if (mEnableValidationLayers)
299 {
300 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
301 }
302
Jamie Madille09bd5d2016-11-29 16:20:35 -0500303 // Verify the required extensions are in the extension names set. Fail if not.
304 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
305
Jamie Madill327ba852016-11-30 12:38:28 -0500306 VkApplicationInfo applicationInfo;
307 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
308 applicationInfo.pNext = nullptr;
309 applicationInfo.pApplicationName = "ANGLE";
310 applicationInfo.applicationVersion = 1;
311 applicationInfo.pEngineName = "ANGLE";
312 applicationInfo.engineVersion = 1;
313 applicationInfo.apiVersion = VK_API_VERSION_1_0;
314
315 VkInstanceCreateInfo instanceInfo;
316 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
317 instanceInfo.pNext = nullptr;
318 instanceInfo.flags = 0;
319 instanceInfo.pApplicationInfo = &applicationInfo;
320
Jamie Madille09bd5d2016-11-29 16:20:35 -0500321 // Enable requested layers and extensions.
322 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
323 instanceInfo.ppEnabledExtensionNames =
324 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Jamie Madill0448ec82016-12-23 13:41:47 -0500325 instanceInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
326 instanceInfo.ppEnabledLayerNames =
327 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
Jamie Madill327ba852016-11-30 12:38:28 -0500328
329 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
330
Jamie Madill0448ec82016-12-23 13:41:47 -0500331 if (mEnableValidationLayers)
332 {
Jamie Madilla66779f2017-01-06 10:43:44 -0500333 // Change back to the previous working directory now that we've loaded the instance -
334 // the validation layers should be loaded at this point.
335 angle::SetCWD(previousCWD.c_str());
336
Jamie Madill0448ec82016-12-23 13:41:47 -0500337 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
338
339 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
340 debugReportInfo.pNext = nullptr;
341 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
342 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
343 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
344 debugReportInfo.pfnCallback = &DebugReportCallback;
345 debugReportInfo.pUserData = this;
346
347 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
348 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
349 ASSERT(createDebugReportCallback);
350 ANGLE_VK_TRY(
351 createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback));
352 }
353
Jamie Madill4d0bf552016-12-28 15:45:24 -0500354 uint32_t physicalDeviceCount = 0;
355 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
356 ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
357
358 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
359 physicalDeviceCount = 1;
360 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, &mPhysicalDevice));
361
362 vkGetPhysicalDeviceProperties(mPhysicalDevice, &mPhysicalDeviceProperties);
363
364 // Ensure we can find a graphics queue family.
365 uint32_t queueCount = 0;
366 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
367
368 ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
369
370 mQueueFamilyProperties.resize(queueCount);
371 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
372 mQueueFamilyProperties.data());
373
374 size_t graphicsQueueFamilyCount = false;
375 uint32_t firstGraphicsQueueFamily = 0;
376 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
377 {
378 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
379 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
380 {
381 ASSERT(queueInfo.queueCount > 0);
382 graphicsQueueFamilyCount++;
383 if (firstGraphicsQueueFamily == 0)
384 {
385 firstGraphicsQueueFamily = familyIndex;
386 }
387 break;
388 }
389 }
390
391 ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
392
393 // If only one queue family, go ahead and initialize the device. If there is more than one
394 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
395 if (graphicsQueueFamilyCount == 1)
396 {
397 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
398 }
399
Jamie Madill035fd6b2017-10-03 15:43:22 -0400400 // Store the physical device memory properties so we can find the right memory pools.
401 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500402
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500403 mGlslangWrapper = GlslangWrapper::GetReference();
404
Jamie Madill6a89d222017-11-02 11:59:51 -0400405 // Initialize the format table.
406 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps);
407
Jamie Madill327ba852016-11-30 12:38:28 -0500408 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400409}
410
Jamie Madill4d0bf552016-12-28 15:45:24 -0500411vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
412{
413 uint32_t deviceLayerCount = 0;
414 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
415
416 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
417 if (deviceLayerCount > 0)
418 {
419 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
420 deviceLayerProps.data()));
421 }
422
423 uint32_t deviceExtensionCount = 0;
424 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
425 &deviceExtensionCount, nullptr));
426
427 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
428 if (deviceExtensionCount > 0)
429 {
430 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
431 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
432 }
433
434 if (mEnableValidationLayers)
435 {
436 if (!HasStandardValidationLayer(deviceLayerProps))
437 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500438 WARN() << "Vulkan standard validation layer is missing.";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500439 mEnableValidationLayers = false;
440 }
441 }
442
443 std::vector<const char *> enabledDeviceExtensions;
444 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
445
446 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
447
448 VkDeviceQueueCreateInfo queueCreateInfo;
449
450 float zeroPriority = 0.0f;
451
452 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
453 queueCreateInfo.pNext = nullptr;
454 queueCreateInfo.flags = 0;
455 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
456 queueCreateInfo.queueCount = 1;
457 queueCreateInfo.pQueuePriorities = &zeroPriority;
458
459 // Initialize the device
460 VkDeviceCreateInfo createInfo;
461
462 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
463 createInfo.pNext = nullptr;
464 createInfo.flags = 0;
465 createInfo.queueCreateInfoCount = 1;
466 createInfo.pQueueCreateInfos = &queueCreateInfo;
467 createInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
468 createInfo.ppEnabledLayerNames =
469 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
470 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
471 createInfo.ppEnabledExtensionNames =
472 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
473 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
474
475 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
476
477 mCurrentQueueFamilyIndex = queueFamilyIndex;
478
479 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
480
481 // Initialize the command pool now that we know the queue family index.
482 VkCommandPoolCreateInfo commandPoolInfo;
483 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
484 commandPoolInfo.pNext = nullptr;
485 // TODO(jmadill): Investigate transient command buffers.
486 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
487 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
488
Jamie Madill5deea722017-02-16 10:44:46 -0500489 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500490
Jamie Madill4d0bf552016-12-28 15:45:24 -0500491 return vk::NoError();
492}
493
494vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface)
495{
496 // We've already initialized a device, and can't re-create it unless it's never been used.
497 // TODO(jmadill): Handle the re-creation case if necessary.
498 if (mDevice != VK_NULL_HANDLE)
499 {
500 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
501
502 // Check if the current device supports present on this surface.
503 VkBool32 supportsPresent = VK_FALSE;
504 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
505 surface, &supportsPresent));
506
507 return (supportsPresent == VK_TRUE);
508 }
509
510 // Find a graphics and present queue.
511 Optional<uint32_t> newPresentQueue;
512 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
513 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
514 {
515 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
516 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
517 {
518 VkBool32 supportsPresent = VK_FALSE;
519 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
520 &supportsPresent));
521
522 if (supportsPresent == VK_TRUE)
523 {
524 newPresentQueue = queueIndex;
525 break;
526 }
527 }
528 }
529
530 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
531 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
532
533 return newPresentQueue.value();
534}
535
536std::string RendererVk::getVendorString() const
537{
538 switch (mPhysicalDeviceProperties.vendorID)
539 {
540 case VENDOR_ID_AMD:
541 return "Advanced Micro Devices";
542 case VENDOR_ID_NVIDIA:
543 return "NVIDIA";
544 case VENDOR_ID_INTEL:
545 return "Intel";
546 default:
547 {
548 // TODO(jmadill): More vendor IDs.
549 std::stringstream strstr;
550 strstr << "Vendor ID: " << mPhysicalDeviceProperties.vendorID;
551 return strstr.str();
552 }
553 }
554}
555
Jamie Madille09bd5d2016-11-29 16:20:35 -0500556std::string RendererVk::getRendererDescription() const
557{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500558 std::stringstream strstr;
559
560 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
561
562 strstr << "Vulkan ";
563 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
564 strstr << VK_VERSION_MINOR(apiVersion) << ".";
565 strstr << VK_VERSION_PATCH(apiVersion);
566
567 strstr << "(" << mPhysicalDeviceProperties.deviceName << ")";
568
569 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500570}
571
Jamie Madillacccc6c2016-05-03 17:22:10 -0400572void RendererVk::ensureCapsInitialized() const
573{
574 if (!mCapsInitialized)
575 {
576 generateCaps(&mNativeCaps, &mNativeTextureCaps, &mNativeExtensions, &mNativeLimitations);
577 mCapsInitialized = true;
578 }
579}
580
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500581void RendererVk::generateCaps(gl::Caps *outCaps,
Jamie Madillacccc6c2016-05-03 17:22:10 -0400582 gl::TextureCapsMap * /*outTextureCaps*/,
Jamie Madillb8353b02017-01-25 12:57:21 -0800583 gl::Extensions *outExtensions,
Jamie Madillacccc6c2016-05-03 17:22:10 -0400584 gl::Limitations * /* outLimitations */) const
585{
Jamie Madill327ba852016-11-30 12:38:28 -0500586 // TODO(jmadill): Caps.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500587 outCaps->maxDrawBuffers = 1;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800588 outCaps->maxVertexAttributes = gl::MAX_VERTEX_ATTRIBS;
589 outCaps->maxVertexAttribBindings = gl::MAX_VERTEX_ATTRIB_BINDINGS;
Jamie Madill035fd6b2017-10-03 15:43:22 -0400590 outCaps->maxVaryingVectors = 16;
591 outCaps->maxTextureImageUnits = 1;
592 outCaps->maxCombinedTextureImageUnits = 1;
593 outCaps->max2DTextureSize = 1024;
Jamie Madilld03a8492017-10-03 15:46:06 -0400594 outCaps->maxElementIndex = std::numeric_limits<GLuint>::max() - 1;
Jamie Madill6276b922017-09-25 02:35:57 -0400595 outCaps->maxFragmentUniformVectors = 8;
596 outCaps->maxVertexUniformVectors = 8;
Jamie Madillb79e7bb2017-10-24 13:55:50 -0400597 outCaps->maxColorAttachments = 1;
Jamie Madillb8353b02017-01-25 12:57:21 -0800598
599 // Enable this for simple buffer readback testing, but some functionality is missing.
600 // TODO(jmadill): Support full mapBufferRange extension.
601 outExtensions->mapBuffer = true;
602 outExtensions->mapBufferRange = true;
Jamie Madillacccc6c2016-05-03 17:22:10 -0400603}
604
605const gl::Caps &RendererVk::getNativeCaps() const
606{
607 ensureCapsInitialized();
608 return mNativeCaps;
609}
610
611const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
612{
613 ensureCapsInitialized();
614 return mNativeTextureCaps;
615}
616
617const gl::Extensions &RendererVk::getNativeExtensions() const
618{
619 ensureCapsInitialized();
620 return mNativeExtensions;
621}
622
623const gl::Limitations &RendererVk::getNativeLimitations() const
624{
625 ensureCapsInitialized();
626 return mNativeLimitations;
627}
628
Jamie Madill7f738d42017-11-20 17:06:27 -0500629vk::Error RendererVk::getStartedCommandBuffer(vk::CommandBufferAndState **commandBufferOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500630{
Jamie Madill7f738d42017-11-20 17:06:27 -0500631 ANGLE_TRY(mCommandBuffer.ensureStarted(mDevice, mCommandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400632 *commandBufferOut = &mCommandBuffer;
633 return vk::NoError();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500634}
635
Jamie Madill7f738d42017-11-20 17:06:27 -0500636vk::Error RendererVk::submitCommandBuffer(vk::CommandBufferAndState *commandBuffer)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500637{
Jamie Madill7f738d42017-11-20 17:06:27 -0500638 ANGLE_TRY(commandBuffer->ensureFinished());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400639
Jamie Madill4d0bf552016-12-28 15:45:24 -0500640 VkFenceCreateInfo fenceInfo;
641 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
642 fenceInfo.pNext = nullptr;
643 fenceInfo.flags = 0;
644
Jamie Madill4d0bf552016-12-28 15:45:24 -0500645 VkSubmitInfo submitInfo;
646 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
647 submitInfo.pNext = nullptr;
648 submitInfo.waitSemaphoreCount = 0;
649 submitInfo.pWaitSemaphores = nullptr;
650 submitInfo.pWaitDstStageMask = nullptr;
651 submitInfo.commandBufferCount = 1;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400652 submitInfo.pCommandBuffers = commandBuffer->ptr();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500653 submitInfo.signalSemaphoreCount = 0;
654 submitInfo.pSignalSemaphores = nullptr;
655
656 // TODO(jmadill): Investigate how to properly submit command buffers.
Jamie Madill4c26fc22017-02-24 11:04:10 -0500657 ANGLE_TRY(submit(submitInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500658
Jamie Madillf651c772017-02-21 15:03:51 -0500659 return vk::NoError();
660}
661
Jamie Madill7f738d42017-11-20 17:06:27 -0500662vk::Error RendererVk::submitAndFinishCommandBuffer(vk::CommandBufferAndState *commandBuffer)
Jamie Madillf651c772017-02-21 15:03:51 -0500663{
664 ANGLE_TRY(submitCommandBuffer(commandBuffer));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500665 ANGLE_TRY(finish());
Jamie Madill4d0bf552016-12-28 15:45:24 -0500666
667 return vk::NoError();
668}
669
Jamie Madill7f738d42017-11-20 17:06:27 -0500670vk::Error RendererVk::submitCommandsWithSync(vk::CommandBufferAndState *commandBuffer,
Jamie Madille918de22017-04-12 10:21:11 -0400671 const vk::Semaphore &waitSemaphore,
672 const vk::Semaphore &signalSemaphore)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500673{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400674 ANGLE_TRY(commandBuffer->end());
675
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500676 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
677
678 VkSubmitInfo submitInfo;
679 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
680 submitInfo.pNext = nullptr;
681 submitInfo.waitSemaphoreCount = 1;
Jamie Madille918de22017-04-12 10:21:11 -0400682 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500683 submitInfo.pWaitDstStageMask = &waitStageMask;
684 submitInfo.commandBufferCount = 1;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400685 submitInfo.pCommandBuffers = commandBuffer->ptr();
Jamie Madille918de22017-04-12 10:21:11 -0400686 submitInfo.signalSemaphoreCount = 1;
687 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500688
689 // TODO(jmadill): Investigate how to properly queue command buffer work.
Jamie Madill0c0dc342017-03-24 14:18:51 -0400690 ANGLE_TRY(submitFrame(submitInfo));
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500691
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500692 return vk::NoError();
693}
694
Jamie Madill4c26fc22017-02-24 11:04:10 -0500695vk::Error RendererVk::finish()
696{
697 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500698 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400699 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500700 return vk::NoError();
701}
702
Jamie Madill0c0dc342017-03-24 14:18:51 -0400703void RendererVk::freeAllInFlightResources()
704{
705 for (auto &fence : mInFlightFences)
706 {
Jamie Madill7f738d42017-11-20 17:06:27 -0500707 fence.get().destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400708 }
709 mInFlightFences.clear();
710
711 for (auto &command : mInFlightCommands)
712 {
Jamie Madill7f738d42017-11-20 17:06:27 -0500713 command.get().destroy(mDevice, mCommandPool);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400714 }
715 mInFlightCommands.clear();
716
717 for (auto &garbage : mGarbage)
718 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400719 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400720 }
721 mGarbage.clear();
722}
723
Jamie Madill4c26fc22017-02-24 11:04:10 -0500724vk::Error RendererVk::checkInFlightCommands()
725{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400726 size_t finishedIndex = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500727
Jamie Madill4c26fc22017-02-24 11:04:10 -0500728 // Check if any in-flight command buffers are finished.
Jamie Madill0c0dc342017-03-24 14:18:51 -0400729 for (size_t index = 0; index < mInFlightFences.size(); index++)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500730 {
Jamie Madill0c0dc342017-03-24 14:18:51 -0400731 auto *inFlightFence = &mInFlightFences[index];
Jamie Madill4c26fc22017-02-24 11:04:10 -0500732
Jamie Madill0c0dc342017-03-24 14:18:51 -0400733 VkResult result = inFlightFence->get().getStatus(mDevice);
734 if (result == VK_NOT_READY)
735 break;
736 ANGLE_VK_TRY(result);
737 finishedIndex = index + 1;
738
739 // Release the fence handle.
740 // TODO(jmadill): Re-use fences.
Jamie Madill7f738d42017-11-20 17:06:27 -0500741 inFlightFence->get().destroy(mDevice);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500742 }
743
Jamie Madill0c0dc342017-03-24 14:18:51 -0400744 if (finishedIndex == 0)
745 return vk::NoError();
Jamie Madillf651c772017-02-21 15:03:51 -0500746
Jamie Madill0c0dc342017-03-24 14:18:51 -0400747 Serial finishedSerial = mInFlightFences[finishedIndex - 1].queueSerial();
748 mInFlightFences.erase(mInFlightFences.begin(), mInFlightFences.begin() + finishedIndex);
749
750 size_t completedCBIndex = 0;
751 for (size_t cbIndex = 0; cbIndex < mInFlightCommands.size(); ++cbIndex)
752 {
753 auto *inFlightCB = &mInFlightCommands[cbIndex];
754 if (inFlightCB->queueSerial() > finishedSerial)
755 break;
756
757 completedCBIndex = cbIndex + 1;
Jamie Madill7f738d42017-11-20 17:06:27 -0500758 inFlightCB->get().destroy(mDevice, mCommandPool);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400759 }
760
761 if (completedCBIndex == 0)
762 return vk::NoError();
763
764 mInFlightCommands.erase(mInFlightCommands.begin(),
765 mInFlightCommands.begin() + completedCBIndex);
766
767 size_t freeIndex = 0;
768 for (; freeIndex < mGarbage.size(); ++freeIndex)
769 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400770 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, finishedSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400771 break;
772 }
773
774 // Remove the entries from the garbage list - they should be ready to go.
775 if (freeIndex > 0)
776 {
777 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500778 }
779
Jamie Madill4c26fc22017-02-24 11:04:10 -0500780 return vk::NoError();
781}
782
783vk::Error RendererVk::submit(const VkSubmitInfo &submitInfo)
784{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400785 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, VK_NULL_HANDLE));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500786
787 // Store this command buffer in the in-flight list.
Jamie Madill0c0dc342017-03-24 14:18:51 -0400788 mInFlightCommands.emplace_back(std::move(mCommandBuffer), mCurrentQueueSerial);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500789
790 // Sanity check.
791 ASSERT(mInFlightCommands.size() < 1000u);
792
Jamie Madill0c0dc342017-03-24 14:18:51 -0400793 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400794 // TODO(jmadill): Overflow check.
795 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400796
797 return vk::NoError();
798}
799
800vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo)
801{
802 VkFenceCreateInfo createInfo;
803 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
804 createInfo.pNext = nullptr;
805 createInfo.flags = 0;
806
807 vk::Fence fence;
808 ANGLE_TRY(fence.init(mDevice, createInfo));
809
810 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, fence.getHandle()));
811
812 // Store this command buffer in the in-flight list.
813 mInFlightFences.emplace_back(std::move(fence), mCurrentQueueSerial);
814 mInFlightCommands.emplace_back(std::move(mCommandBuffer), mCurrentQueueSerial);
815
816 // Sanity check.
817 ASSERT(mInFlightCommands.size() < 1000u);
818
819 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400820 // TODO(jmadill): Overflow check.
821 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400822
823 ANGLE_TRY(checkInFlightCommands());
824
Jamie Madill4c26fc22017-02-24 11:04:10 -0500825 return vk::NoError();
826}
827
Jamie Madill5deea722017-02-16 10:44:46 -0500828vk::Error RendererVk::createStagingImage(TextureDimension dimension,
829 const vk::Format &format,
830 const gl::Extents &extent,
Jamie Madill035fd6b2017-10-03 15:43:22 -0400831 vk::StagingUsage usage,
Jamie Madill5deea722017-02-16 10:44:46 -0500832 vk::StagingImage *imageOut)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500833{
Jamie Madill035fd6b2017-10-03 15:43:22 -0400834 ANGLE_TRY(imageOut->init(mDevice, mCurrentQueueFamilyIndex, mMemoryProperties, dimension,
Jamie Madill1d7be502017-10-29 18:06:50 -0400835 format.vkTextureFormat, extent, usage));
Jamie Madill5deea722017-02-16 10:44:46 -0500836 return vk::NoError();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500837}
838
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500839GlslangWrapper *RendererVk::getGlslangWrapper()
840{
841 return mGlslangWrapper;
842}
843
Jamie Madill4c26fc22017-02-24 11:04:10 -0500844Serial RendererVk::getCurrentQueueSerial() const
845{
846 return mCurrentQueueSerial;
847}
848
Jamie Madill1b038242017-11-01 15:14:36 -0400849gl::Error RendererVk::ensureInRenderPass(const gl::Context *context, FramebufferVk *framebufferVk)
850{
851 if (mCurrentRenderPassFramebuffer == framebufferVk)
852 {
853 return gl::NoError();
854 }
855
856 if (mCurrentRenderPassFramebuffer)
857 {
858 endRenderPass();
859 }
Jamie Madill9f2a8612017-11-30 12:43:09 -0500860 ANGLE_TRY(framebufferVk->beginRenderPass(context, this, &mCommandBuffer, mCurrentQueueSerial));
Jamie Madill1b038242017-11-01 15:14:36 -0400861 mCurrentRenderPassFramebuffer = framebufferVk;
862 return gl::NoError();
863}
864
865void RendererVk::endRenderPass()
866{
867 if (mCurrentRenderPassFramebuffer)
868 {
869 ASSERT(mCommandBuffer.started());
870 mCommandBuffer.endRenderPass();
871 mCurrentRenderPassFramebuffer = nullptr;
872 }
873}
874
Jamie Madill7bd16662017-10-28 19:40:50 -0400875void RendererVk::onReleaseRenderPass(const FramebufferVk *framebufferVk)
876{
877 if (mCurrentRenderPassFramebuffer == framebufferVk)
878 {
879 endRenderPass();
880 }
881}
882
Jamie Madill97760352017-11-09 13:08:29 -0500883bool RendererVk::isResourceInUse(const ResourceVk &resource)
884{
885 return isSerialInUse(resource.getQueueSerial());
886}
887
888bool RendererVk::isSerialInUse(Serial serial)
889{
890 return serial > mLastCompletedQueueSerial;
891}
892
Jamie Madill9f2a8612017-11-30 12:43:09 -0500893vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc,
894 vk::RenderPass **renderPassOut)
895{
896 return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc,
897 renderPassOut);
898}
899
900vk::Error RendererVk::getMatchingRenderPass(const vk::RenderPassDesc &desc,
901 vk::RenderPass **renderPassOut)
902{
903 return mRenderPassCache.getMatchingRenderPass(mDevice, mCurrentQueueSerial, desc,
904 renderPassOut);
905}
906
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400907} // namespace rx