blob: bc090b03a591ef85be6e5f5a548e03cd792d11d8 [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
44 for (const auto &extensionName : enabledExtensionNames)
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 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 Madill0448ec82016-12-23 13:41:47 -050087RendererVk::RendererVk()
88 : mCapsInitialized(false),
89 mInstance(VK_NULL_HANDLE),
90 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -050091 mDebugReportCallback(VK_NULL_HANDLE),
92 mPhysicalDevice(VK_NULL_HANDLE),
93 mQueue(VK_NULL_HANDLE),
94 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
95 mDevice(VK_NULL_HANDLE),
Jamie Madill8ecf7f92017-01-13 17:29:52 -050096 mHostVisibleMemoryIndex(std::numeric_limits<uint32_t>::max()),
Jamie Madill4c26fc22017-02-24 11:04:10 -050097 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -040098 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
99 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Jamie Madill4c26fc22017-02-24 11:04:10 -0500100 mInFlightCommands()
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400101{
102}
103
104RendererVk::~RendererVk()
105{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400106 if (!mInFlightCommands.empty() || !mInFlightFences.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500107 {
108 vk::Error error = finish();
109 if (error.isError())
110 {
111 ERR() << "Error during VK shutdown: " << error;
112 }
113 }
114
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500115 if (mGlslangWrapper)
116 {
117 GlslangWrapper::ReleaseReference();
118 mGlslangWrapper = nullptr;
119 }
120
Jamie Madill5deea722017-02-16 10:44:46 -0500121 if (mCommandBuffer.valid())
122 {
123 mCommandBuffer.destroy(mDevice);
124 }
125
126 if (mCommandPool.valid())
127 {
128 mCommandPool.destroy(mDevice);
129 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500130
131 if (mDevice)
132 {
133 vkDestroyDevice(mDevice, nullptr);
134 mDevice = VK_NULL_HANDLE;
135 }
136
Jamie Madill0448ec82016-12-23 13:41:47 -0500137 if (mDebugReportCallback)
138 {
139 ASSERT(mInstance);
140 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
141 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
142 ASSERT(destroyDebugReportCallback);
143 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
144 }
145
Jamie Madill4d0bf552016-12-28 15:45:24 -0500146 if (mInstance)
147 {
148 vkDestroyInstance(mInstance, nullptr);
149 mInstance = VK_NULL_HANDLE;
150 }
151
152 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500153}
154
Frank Henigman29f148b2016-11-23 21:05:36 -0500155vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500156{
Jamie Madill222c5172017-07-19 16:15:42 -0400157 mEnableValidationLayers = ShouldUseDebugLayers(attribs);
Jamie Madilla66779f2017-01-06 10:43:44 -0500158
159 // If we're loading the validation layers, we could be running from any random directory.
160 // Change to the executable directory so we can find the layers, then change back to the
161 // previous directory to be safe we don't disrupt the application.
162 std::string previousCWD;
163
164 if (mEnableValidationLayers)
165 {
166 const auto &cwd = angle::GetCWD();
167 if (!cwd.valid())
168 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500169 ERR() << "Error getting CWD for Vulkan layers init.";
Jamie Madilla66779f2017-01-06 10:43:44 -0500170 mEnableValidationLayers = false;
171 }
172 else
173 {
174 previousCWD = cwd.value();
175 }
176 const char *exeDir = angle::GetExecutableDirectory();
177 angle::SetCWD(exeDir);
178 }
179
Jamie Madill0448ec82016-12-23 13:41:47 -0500180 // Gather global layer properties.
181 uint32_t instanceLayerCount = 0;
182 ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
183
184 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
185 if (instanceLayerCount > 0)
186 {
187 ANGLE_VK_TRY(
188 vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data()));
189 }
190
Jamie Madille09bd5d2016-11-29 16:20:35 -0500191 uint32_t instanceExtensionCount = 0;
192 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
193
194 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
195 if (instanceExtensionCount > 0)
196 {
197 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
198 instanceExtensionProps.data()));
199 }
200
Jamie Madill0448ec82016-12-23 13:41:47 -0500201 if (mEnableValidationLayers)
202 {
203 // Verify the standard validation layers are available.
204 if (!HasStandardValidationLayer(instanceLayerProps))
205 {
206 // Generate an error if the attribute was requested, warning otherwise.
Jamie Madill222c5172017-07-19 16:15:42 -0400207 if (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) ==
208 EGL_TRUE)
Jamie Madill0448ec82016-12-23 13:41:47 -0500209 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500210 ERR() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500211 }
212 else
213 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500214 WARN() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500215 }
216 mEnableValidationLayers = false;
217 }
218 }
219
Jamie Madille09bd5d2016-11-29 16:20:35 -0500220 std::vector<const char *> enabledInstanceExtensions;
221 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500222 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500223
Jamie Madill0448ec82016-12-23 13:41:47 -0500224 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
225 if (mEnableValidationLayers)
226 {
227 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
228 }
229
Jamie Madille09bd5d2016-11-29 16:20:35 -0500230 // Verify the required extensions are in the extension names set. Fail if not.
231 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
232
Jamie Madill327ba852016-11-30 12:38:28 -0500233 VkApplicationInfo applicationInfo;
234 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
235 applicationInfo.pNext = nullptr;
236 applicationInfo.pApplicationName = "ANGLE";
237 applicationInfo.applicationVersion = 1;
238 applicationInfo.pEngineName = "ANGLE";
239 applicationInfo.engineVersion = 1;
240 applicationInfo.apiVersion = VK_API_VERSION_1_0;
241
242 VkInstanceCreateInfo instanceInfo;
243 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
244 instanceInfo.pNext = nullptr;
245 instanceInfo.flags = 0;
246 instanceInfo.pApplicationInfo = &applicationInfo;
247
Jamie Madille09bd5d2016-11-29 16:20:35 -0500248 // Enable requested layers and extensions.
249 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
250 instanceInfo.ppEnabledExtensionNames =
251 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Jamie Madill0448ec82016-12-23 13:41:47 -0500252 instanceInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
253 instanceInfo.ppEnabledLayerNames =
254 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
Jamie Madill327ba852016-11-30 12:38:28 -0500255
256 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
257
Jamie Madill0448ec82016-12-23 13:41:47 -0500258 if (mEnableValidationLayers)
259 {
Jamie Madilla66779f2017-01-06 10:43:44 -0500260 // Change back to the previous working directory now that we've loaded the instance -
261 // the validation layers should be loaded at this point.
262 angle::SetCWD(previousCWD.c_str());
263
Jamie Madill0448ec82016-12-23 13:41:47 -0500264 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
265
266 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
267 debugReportInfo.pNext = nullptr;
268 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
269 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
270 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
271 debugReportInfo.pfnCallback = &DebugReportCallback;
272 debugReportInfo.pUserData = this;
273
274 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
275 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
276 ASSERT(createDebugReportCallback);
277 ANGLE_VK_TRY(
278 createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback));
279 }
280
Jamie Madill4d0bf552016-12-28 15:45:24 -0500281 uint32_t physicalDeviceCount = 0;
282 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
283 ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
284
285 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
286 physicalDeviceCount = 1;
287 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, &mPhysicalDevice));
288
289 vkGetPhysicalDeviceProperties(mPhysicalDevice, &mPhysicalDeviceProperties);
290
291 // Ensure we can find a graphics queue family.
292 uint32_t queueCount = 0;
293 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
294
295 ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
296
297 mQueueFamilyProperties.resize(queueCount);
298 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
299 mQueueFamilyProperties.data());
300
301 size_t graphicsQueueFamilyCount = false;
302 uint32_t firstGraphicsQueueFamily = 0;
303 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
304 {
305 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
306 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
307 {
308 ASSERT(queueInfo.queueCount > 0);
309 graphicsQueueFamilyCount++;
310 if (firstGraphicsQueueFamily == 0)
311 {
312 firstGraphicsQueueFamily = familyIndex;
313 }
314 break;
315 }
316 }
317
318 ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
319
320 // If only one queue family, go ahead and initialize the device. If there is more than one
321 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
322 if (graphicsQueueFamilyCount == 1)
323 {
324 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
325 }
326
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500327 VkPhysicalDeviceMemoryProperties memoryProperties;
328 vkGetPhysicalDeviceMemoryProperties(mPhysicalDevice, &memoryProperties);
329
330 for (uint32_t memoryIndex = 0; memoryIndex < memoryProperties.memoryTypeCount; ++memoryIndex)
331 {
332 if ((memoryProperties.memoryTypes[memoryIndex].propertyFlags &
333 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0)
334 {
335 mHostVisibleMemoryIndex = memoryIndex;
336 break;
337 }
338 }
339
340 ANGLE_VK_CHECK(mHostVisibleMemoryIndex < std::numeric_limits<uint32_t>::max(),
341 VK_ERROR_INITIALIZATION_FAILED);
342
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500343 mGlslangWrapper = GlslangWrapper::GetReference();
344
Jamie Madill327ba852016-11-30 12:38:28 -0500345 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400346}
347
Jamie Madill4d0bf552016-12-28 15:45:24 -0500348vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
349{
350 uint32_t deviceLayerCount = 0;
351 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
352
353 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
354 if (deviceLayerCount > 0)
355 {
356 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
357 deviceLayerProps.data()));
358 }
359
360 uint32_t deviceExtensionCount = 0;
361 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
362 &deviceExtensionCount, nullptr));
363
364 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
365 if (deviceExtensionCount > 0)
366 {
367 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
368 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
369 }
370
371 if (mEnableValidationLayers)
372 {
373 if (!HasStandardValidationLayer(deviceLayerProps))
374 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500375 WARN() << "Vulkan standard validation layer is missing.";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500376 mEnableValidationLayers = false;
377 }
378 }
379
380 std::vector<const char *> enabledDeviceExtensions;
381 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
382
383 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
384
385 VkDeviceQueueCreateInfo queueCreateInfo;
386
387 float zeroPriority = 0.0f;
388
389 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
390 queueCreateInfo.pNext = nullptr;
391 queueCreateInfo.flags = 0;
392 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
393 queueCreateInfo.queueCount = 1;
394 queueCreateInfo.pQueuePriorities = &zeroPriority;
395
396 // Initialize the device
397 VkDeviceCreateInfo createInfo;
398
399 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
400 createInfo.pNext = nullptr;
401 createInfo.flags = 0;
402 createInfo.queueCreateInfoCount = 1;
403 createInfo.pQueueCreateInfos = &queueCreateInfo;
404 createInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
405 createInfo.ppEnabledLayerNames =
406 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
407 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
408 createInfo.ppEnabledExtensionNames =
409 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
410 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
411
412 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
413
414 mCurrentQueueFamilyIndex = queueFamilyIndex;
415
416 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
417
418 // Initialize the command pool now that we know the queue family index.
419 VkCommandPoolCreateInfo commandPoolInfo;
420 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
421 commandPoolInfo.pNext = nullptr;
422 // TODO(jmadill): Investigate transient command buffers.
423 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
424 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
425
Jamie Madill5deea722017-02-16 10:44:46 -0500426 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500427
Jamie Madill5deea722017-02-16 10:44:46 -0500428 mCommandBuffer.setCommandPool(&mCommandPool);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500429
430 return vk::NoError();
431}
432
433vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface)
434{
435 // We've already initialized a device, and can't re-create it unless it's never been used.
436 // TODO(jmadill): Handle the re-creation case if necessary.
437 if (mDevice != VK_NULL_HANDLE)
438 {
439 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
440
441 // Check if the current device supports present on this surface.
442 VkBool32 supportsPresent = VK_FALSE;
443 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
444 surface, &supportsPresent));
445
446 return (supportsPresent == VK_TRUE);
447 }
448
449 // Find a graphics and present queue.
450 Optional<uint32_t> newPresentQueue;
451 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
452 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
453 {
454 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
455 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
456 {
457 VkBool32 supportsPresent = VK_FALSE;
458 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
459 &supportsPresent));
460
461 if (supportsPresent == VK_TRUE)
462 {
463 newPresentQueue = queueIndex;
464 break;
465 }
466 }
467 }
468
469 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
470 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
471
472 return newPresentQueue.value();
473}
474
475std::string RendererVk::getVendorString() const
476{
477 switch (mPhysicalDeviceProperties.vendorID)
478 {
479 case VENDOR_ID_AMD:
480 return "Advanced Micro Devices";
481 case VENDOR_ID_NVIDIA:
482 return "NVIDIA";
483 case VENDOR_ID_INTEL:
484 return "Intel";
485 default:
486 {
487 // TODO(jmadill): More vendor IDs.
488 std::stringstream strstr;
489 strstr << "Vendor ID: " << mPhysicalDeviceProperties.vendorID;
490 return strstr.str();
491 }
492 }
493}
494
Jamie Madille09bd5d2016-11-29 16:20:35 -0500495std::string RendererVk::getRendererDescription() const
496{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500497 std::stringstream strstr;
498
499 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
500
501 strstr << "Vulkan ";
502 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
503 strstr << VK_VERSION_MINOR(apiVersion) << ".";
504 strstr << VK_VERSION_PATCH(apiVersion);
505
506 strstr << "(" << mPhysicalDeviceProperties.deviceName << ")";
507
508 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500509}
510
Jamie Madillacccc6c2016-05-03 17:22:10 -0400511void RendererVk::ensureCapsInitialized() const
512{
513 if (!mCapsInitialized)
514 {
515 generateCaps(&mNativeCaps, &mNativeTextureCaps, &mNativeExtensions, &mNativeLimitations);
516 mCapsInitialized = true;
517 }
518}
519
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500520void RendererVk::generateCaps(gl::Caps *outCaps,
Jamie Madillacccc6c2016-05-03 17:22:10 -0400521 gl::TextureCapsMap * /*outTextureCaps*/,
Jamie Madillb8353b02017-01-25 12:57:21 -0800522 gl::Extensions *outExtensions,
Jamie Madillacccc6c2016-05-03 17:22:10 -0400523 gl::Limitations * /* outLimitations */) const
524{
Jamie Madill327ba852016-11-30 12:38:28 -0500525 // TODO(jmadill): Caps.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500526 outCaps->maxDrawBuffers = 1;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800527 outCaps->maxVertexAttributes = gl::MAX_VERTEX_ATTRIBS;
528 outCaps->maxVertexAttribBindings = gl::MAX_VERTEX_ATTRIB_BINDINGS;
Jamie Madillb8353b02017-01-25 12:57:21 -0800529
530 // Enable this for simple buffer readback testing, but some functionality is missing.
531 // TODO(jmadill): Support full mapBufferRange extension.
532 outExtensions->mapBuffer = true;
533 outExtensions->mapBufferRange = true;
Jamie Madillacccc6c2016-05-03 17:22:10 -0400534}
535
536const gl::Caps &RendererVk::getNativeCaps() const
537{
538 ensureCapsInitialized();
539 return mNativeCaps;
540}
541
542const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
543{
544 ensureCapsInitialized();
545 return mNativeTextureCaps;
546}
547
548const gl::Extensions &RendererVk::getNativeExtensions() const
549{
550 ensureCapsInitialized();
551 return mNativeExtensions;
552}
553
554const gl::Limitations &RendererVk::getNativeLimitations() const
555{
556 ensureCapsInitialized();
557 return mNativeLimitations;
558}
559
Jamie Madill0c0dc342017-03-24 14:18:51 -0400560vk::Error RendererVk::getStartedCommandBuffer(vk::CommandBuffer **commandBufferOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500561{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400562 ANGLE_TRY(mCommandBuffer.begin(mDevice));
563 *commandBufferOut = &mCommandBuffer;
564 return vk::NoError();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500565}
566
Jamie Madill0c0dc342017-03-24 14:18:51 -0400567vk::Error RendererVk::submitCommandBuffer(vk::CommandBuffer *commandBuffer)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500568{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400569 ANGLE_TRY(commandBuffer->end());
570
Jamie Madill4d0bf552016-12-28 15:45:24 -0500571 VkFenceCreateInfo fenceInfo;
572 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
573 fenceInfo.pNext = nullptr;
574 fenceInfo.flags = 0;
575
Jamie Madill4d0bf552016-12-28 15:45:24 -0500576 VkSubmitInfo submitInfo;
577 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
578 submitInfo.pNext = nullptr;
579 submitInfo.waitSemaphoreCount = 0;
580 submitInfo.pWaitSemaphores = nullptr;
581 submitInfo.pWaitDstStageMask = nullptr;
582 submitInfo.commandBufferCount = 1;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400583 submitInfo.pCommandBuffers = commandBuffer->ptr();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500584 submitInfo.signalSemaphoreCount = 0;
585 submitInfo.pSignalSemaphores = nullptr;
586
587 // TODO(jmadill): Investigate how to properly submit command buffers.
Jamie Madill4c26fc22017-02-24 11:04:10 -0500588 ANGLE_TRY(submit(submitInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500589
Jamie Madillf651c772017-02-21 15:03:51 -0500590 return vk::NoError();
591}
592
Jamie Madill0c0dc342017-03-24 14:18:51 -0400593vk::Error RendererVk::submitAndFinishCommandBuffer(vk::CommandBuffer *commandBuffer)
Jamie Madillf651c772017-02-21 15:03:51 -0500594{
595 ANGLE_TRY(submitCommandBuffer(commandBuffer));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500596 ANGLE_TRY(finish());
Jamie Madill4d0bf552016-12-28 15:45:24 -0500597
598 return vk::NoError();
599}
600
Jamie Madill0c0dc342017-03-24 14:18:51 -0400601vk::Error RendererVk::submitCommandsWithSync(vk::CommandBuffer *commandBuffer,
Jamie Madille918de22017-04-12 10:21:11 -0400602 const vk::Semaphore &waitSemaphore,
603 const vk::Semaphore &signalSemaphore)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500604{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400605 ANGLE_TRY(commandBuffer->end());
606
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500607 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
608
609 VkSubmitInfo submitInfo;
610 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
611 submitInfo.pNext = nullptr;
612 submitInfo.waitSemaphoreCount = 1;
Jamie Madille918de22017-04-12 10:21:11 -0400613 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500614 submitInfo.pWaitDstStageMask = &waitStageMask;
615 submitInfo.commandBufferCount = 1;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400616 submitInfo.pCommandBuffers = commandBuffer->ptr();
Jamie Madille918de22017-04-12 10:21:11 -0400617 submitInfo.signalSemaphoreCount = 1;
618 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500619
620 // TODO(jmadill): Investigate how to properly queue command buffer work.
Jamie Madill0c0dc342017-03-24 14:18:51 -0400621 ANGLE_TRY(submitFrame(submitInfo));
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500622
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500623 return vk::NoError();
624}
625
Jamie Madill4c26fc22017-02-24 11:04:10 -0500626vk::Error RendererVk::finish()
627{
628 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500629 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400630 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500631 return vk::NoError();
632}
633
Jamie Madill0c0dc342017-03-24 14:18:51 -0400634void RendererVk::freeAllInFlightResources()
635{
636 for (auto &fence : mInFlightFences)
637 {
638 fence.destroy(mDevice);
639 }
640 mInFlightFences.clear();
641
642 for (auto &command : mInFlightCommands)
643 {
644 command.destroy(mDevice);
645 }
646 mInFlightCommands.clear();
647
648 for (auto &garbage : mGarbage)
649 {
650 garbage->destroy(mDevice);
651 }
652 mGarbage.clear();
653}
654
Jamie Madill4c26fc22017-02-24 11:04:10 -0500655vk::Error RendererVk::checkInFlightCommands()
656{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400657 size_t finishedIndex = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500658
Jamie Madill4c26fc22017-02-24 11:04:10 -0500659 // Check if any in-flight command buffers are finished.
Jamie Madill0c0dc342017-03-24 14:18:51 -0400660 for (size_t index = 0; index < mInFlightFences.size(); index++)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500661 {
Jamie Madill0c0dc342017-03-24 14:18:51 -0400662 auto *inFlightFence = &mInFlightFences[index];
Jamie Madill4c26fc22017-02-24 11:04:10 -0500663
Jamie Madill0c0dc342017-03-24 14:18:51 -0400664 VkResult result = inFlightFence->get().getStatus(mDevice);
665 if (result == VK_NOT_READY)
666 break;
667 ANGLE_VK_TRY(result);
668 finishedIndex = index + 1;
669
670 // Release the fence handle.
671 // TODO(jmadill): Re-use fences.
672 inFlightFence->destroy(mDevice);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500673 }
674
Jamie Madill0c0dc342017-03-24 14:18:51 -0400675 if (finishedIndex == 0)
676 return vk::NoError();
Jamie Madillf651c772017-02-21 15:03:51 -0500677
Jamie Madill0c0dc342017-03-24 14:18:51 -0400678 Serial finishedSerial = mInFlightFences[finishedIndex - 1].queueSerial();
679 mInFlightFences.erase(mInFlightFences.begin(), mInFlightFences.begin() + finishedIndex);
680
681 size_t completedCBIndex = 0;
682 for (size_t cbIndex = 0; cbIndex < mInFlightCommands.size(); ++cbIndex)
683 {
684 auto *inFlightCB = &mInFlightCommands[cbIndex];
685 if (inFlightCB->queueSerial() > finishedSerial)
686 break;
687
688 completedCBIndex = cbIndex + 1;
689 inFlightCB->destroy(mDevice);
690 }
691
692 if (completedCBIndex == 0)
693 return vk::NoError();
694
695 mInFlightCommands.erase(mInFlightCommands.begin(),
696 mInFlightCommands.begin() + completedCBIndex);
697
698 size_t freeIndex = 0;
699 for (; freeIndex < mGarbage.size(); ++freeIndex)
700 {
701 if (!mGarbage[freeIndex]->destroyIfComplete(mDevice, finishedSerial))
702 break;
703 }
704
705 // Remove the entries from the garbage list - they should be ready to go.
706 if (freeIndex > 0)
707 {
708 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500709 }
710
Jamie Madill4c26fc22017-02-24 11:04:10 -0500711 return vk::NoError();
712}
713
714vk::Error RendererVk::submit(const VkSubmitInfo &submitInfo)
715{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400716 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, VK_NULL_HANDLE));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500717
718 // Store this command buffer in the in-flight list.
Jamie Madill0c0dc342017-03-24 14:18:51 -0400719 mInFlightCommands.emplace_back(std::move(mCommandBuffer), mCurrentQueueSerial);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500720
721 // Sanity check.
722 ASSERT(mInFlightCommands.size() < 1000u);
723
Jamie Madill0c0dc342017-03-24 14:18:51 -0400724 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400725 // TODO(jmadill): Overflow check.
726 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400727
728 return vk::NoError();
729}
730
731vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo)
732{
733 VkFenceCreateInfo createInfo;
734 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
735 createInfo.pNext = nullptr;
736 createInfo.flags = 0;
737
738 vk::Fence fence;
739 ANGLE_TRY(fence.init(mDevice, createInfo));
740
741 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, fence.getHandle()));
742
743 // Store this command buffer in the in-flight list.
744 mInFlightFences.emplace_back(std::move(fence), mCurrentQueueSerial);
745 mInFlightCommands.emplace_back(std::move(mCommandBuffer), mCurrentQueueSerial);
746
747 // Sanity check.
748 ASSERT(mInFlightCommands.size() < 1000u);
749
750 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400751 // TODO(jmadill): Overflow check.
752 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400753
754 ANGLE_TRY(checkInFlightCommands());
755
Jamie Madill4c26fc22017-02-24 11:04:10 -0500756 return vk::NoError();
757}
758
Jamie Madill5deea722017-02-16 10:44:46 -0500759vk::Error RendererVk::createStagingImage(TextureDimension dimension,
760 const vk::Format &format,
761 const gl::Extents &extent,
762 vk::StagingImage *imageOut)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500763{
764 ASSERT(mHostVisibleMemoryIndex != std::numeric_limits<uint32_t>::max());
765
Jamie Madill5deea722017-02-16 10:44:46 -0500766 ANGLE_TRY(imageOut->init(mDevice, mCurrentQueueFamilyIndex, mHostVisibleMemoryIndex, dimension,
767 format.native, extent));
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500768
Jamie Madill5deea722017-02-16 10:44:46 -0500769 return vk::NoError();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500770}
771
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500772GlslangWrapper *RendererVk::getGlslangWrapper()
773{
774 return mGlslangWrapper;
775}
776
Jamie Madill4c26fc22017-02-24 11:04:10 -0500777Serial RendererVk::getCurrentQueueSerial() const
778{
779 return mCurrentQueueSerial;
780}
781
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400782} // namespace rx