blob: 464daca086dbc9aa7c950e66c007dc8e8f59d64b [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 Madill4c26fc22017-02-24 11:04:10 -050096 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -040097 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
98 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Jamie Madill1b038242017-11-01 15:14:36 -040099 mInFlightCommands(),
100 mCurrentRenderPassFramebuffer(nullptr)
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();
Jamie Madillb8bbbf92017-09-19 00:24:59 -0400175 const char *exeDir = angle::GetExecutableDirectory();
176 if (!angle::SetCWD(exeDir))
177 {
178 ERR() << "Error setting CWD for Vulkan layers init.";
179 mEnableValidationLayers = false;
180 }
Jamie Madilla66779f2017-01-06 10:43:44 -0500181 }
Jamie Madillb8bbbf92017-09-19 00:24:59 -0400182 }
183
184 // Override environment variable to use the ANGLE layers.
185 if (mEnableValidationLayers)
186 {
187 if (!angle::SetEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_LAYERS_DIR))
188 {
189 ERR() << "Error setting environment for Vulkan layers init.";
190 mEnableValidationLayers = false;
191 }
Jamie Madilla66779f2017-01-06 10:43:44 -0500192 }
193
Jamie Madill0448ec82016-12-23 13:41:47 -0500194 // Gather global layer properties.
195 uint32_t instanceLayerCount = 0;
196 ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
197
198 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
199 if (instanceLayerCount > 0)
200 {
201 ANGLE_VK_TRY(
202 vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data()));
203 }
204
Jamie Madille09bd5d2016-11-29 16:20:35 -0500205 uint32_t instanceExtensionCount = 0;
206 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
207
208 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
209 if (instanceExtensionCount > 0)
210 {
211 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
212 instanceExtensionProps.data()));
213 }
214
Jamie Madill0448ec82016-12-23 13:41:47 -0500215 if (mEnableValidationLayers)
216 {
217 // Verify the standard validation layers are available.
218 if (!HasStandardValidationLayer(instanceLayerProps))
219 {
220 // Generate an error if the attribute was requested, warning otherwise.
Jamie Madill222c5172017-07-19 16:15:42 -0400221 if (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) ==
222 EGL_TRUE)
Jamie Madill0448ec82016-12-23 13:41:47 -0500223 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500224 ERR() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500225 }
226 else
227 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500228 WARN() << "Vulkan standard validation layers are missing.";
Jamie Madill0448ec82016-12-23 13:41:47 -0500229 }
230 mEnableValidationLayers = false;
231 }
232 }
233
Jamie Madille09bd5d2016-11-29 16:20:35 -0500234 std::vector<const char *> enabledInstanceExtensions;
235 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500236 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500237
Jamie Madill0448ec82016-12-23 13:41:47 -0500238 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
239 if (mEnableValidationLayers)
240 {
241 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
242 }
243
Jamie Madille09bd5d2016-11-29 16:20:35 -0500244 // Verify the required extensions are in the extension names set. Fail if not.
245 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
246
Jamie Madill327ba852016-11-30 12:38:28 -0500247 VkApplicationInfo applicationInfo;
248 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
249 applicationInfo.pNext = nullptr;
250 applicationInfo.pApplicationName = "ANGLE";
251 applicationInfo.applicationVersion = 1;
252 applicationInfo.pEngineName = "ANGLE";
253 applicationInfo.engineVersion = 1;
254 applicationInfo.apiVersion = VK_API_VERSION_1_0;
255
256 VkInstanceCreateInfo instanceInfo;
257 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
258 instanceInfo.pNext = nullptr;
259 instanceInfo.flags = 0;
260 instanceInfo.pApplicationInfo = &applicationInfo;
261
Jamie Madille09bd5d2016-11-29 16:20:35 -0500262 // Enable requested layers and extensions.
263 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
264 instanceInfo.ppEnabledExtensionNames =
265 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Jamie Madill0448ec82016-12-23 13:41:47 -0500266 instanceInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
267 instanceInfo.ppEnabledLayerNames =
268 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
Jamie Madill327ba852016-11-30 12:38:28 -0500269
270 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
271
Jamie Madill0448ec82016-12-23 13:41:47 -0500272 if (mEnableValidationLayers)
273 {
Jamie Madilla66779f2017-01-06 10:43:44 -0500274 // Change back to the previous working directory now that we've loaded the instance -
275 // the validation layers should be loaded at this point.
276 angle::SetCWD(previousCWD.c_str());
277
Jamie Madill0448ec82016-12-23 13:41:47 -0500278 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
279
280 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
281 debugReportInfo.pNext = nullptr;
282 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
283 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
284 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
285 debugReportInfo.pfnCallback = &DebugReportCallback;
286 debugReportInfo.pUserData = this;
287
288 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
289 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
290 ASSERT(createDebugReportCallback);
291 ANGLE_VK_TRY(
292 createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback));
293 }
294
Jamie Madill4d0bf552016-12-28 15:45:24 -0500295 uint32_t physicalDeviceCount = 0;
296 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
297 ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
298
299 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
300 physicalDeviceCount = 1;
301 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, &mPhysicalDevice));
302
303 vkGetPhysicalDeviceProperties(mPhysicalDevice, &mPhysicalDeviceProperties);
304
305 // Ensure we can find a graphics queue family.
306 uint32_t queueCount = 0;
307 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
308
309 ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
310
311 mQueueFamilyProperties.resize(queueCount);
312 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
313 mQueueFamilyProperties.data());
314
315 size_t graphicsQueueFamilyCount = false;
316 uint32_t firstGraphicsQueueFamily = 0;
317 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
318 {
319 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
320 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
321 {
322 ASSERT(queueInfo.queueCount > 0);
323 graphicsQueueFamilyCount++;
324 if (firstGraphicsQueueFamily == 0)
325 {
326 firstGraphicsQueueFamily = familyIndex;
327 }
328 break;
329 }
330 }
331
332 ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
333
334 // If only one queue family, go ahead and initialize the device. If there is more than one
335 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
336 if (graphicsQueueFamilyCount == 1)
337 {
338 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
339 }
340
Jamie Madill035fd6b2017-10-03 15:43:22 -0400341 // Store the physical device memory properties so we can find the right memory pools.
342 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500343
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500344 mGlslangWrapper = GlslangWrapper::GetReference();
345
Jamie Madill327ba852016-11-30 12:38:28 -0500346 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400347}
348
Jamie Madill4d0bf552016-12-28 15:45:24 -0500349vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
350{
351 uint32_t deviceLayerCount = 0;
352 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
353
354 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
355 if (deviceLayerCount > 0)
356 {
357 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
358 deviceLayerProps.data()));
359 }
360
361 uint32_t deviceExtensionCount = 0;
362 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
363 &deviceExtensionCount, nullptr));
364
365 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
366 if (deviceExtensionCount > 0)
367 {
368 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
369 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
370 }
371
372 if (mEnableValidationLayers)
373 {
374 if (!HasStandardValidationLayer(deviceLayerProps))
375 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500376 WARN() << "Vulkan standard validation layer is missing.";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500377 mEnableValidationLayers = false;
378 }
379 }
380
381 std::vector<const char *> enabledDeviceExtensions;
382 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
383
384 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
385
386 VkDeviceQueueCreateInfo queueCreateInfo;
387
388 float zeroPriority = 0.0f;
389
390 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
391 queueCreateInfo.pNext = nullptr;
392 queueCreateInfo.flags = 0;
393 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
394 queueCreateInfo.queueCount = 1;
395 queueCreateInfo.pQueuePriorities = &zeroPriority;
396
397 // Initialize the device
398 VkDeviceCreateInfo createInfo;
399
400 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
401 createInfo.pNext = nullptr;
402 createInfo.flags = 0;
403 createInfo.queueCreateInfoCount = 1;
404 createInfo.pQueueCreateInfos = &queueCreateInfo;
405 createInfo.enabledLayerCount = mEnableValidationLayers ? 1u : 0u;
406 createInfo.ppEnabledLayerNames =
407 mEnableValidationLayers ? &g_VkStdValidationLayerName : nullptr;
408 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
409 createInfo.ppEnabledExtensionNames =
410 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
411 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
412
413 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
414
415 mCurrentQueueFamilyIndex = queueFamilyIndex;
416
417 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
418
419 // Initialize the command pool now that we know the queue family index.
420 VkCommandPoolCreateInfo commandPoolInfo;
421 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
422 commandPoolInfo.pNext = nullptr;
423 // TODO(jmadill): Investigate transient command buffers.
424 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
425 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
426
Jamie Madill5deea722017-02-16 10:44:46 -0500427 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500428
Jamie Madill5deea722017-02-16 10:44:46 -0500429 mCommandBuffer.setCommandPool(&mCommandPool);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500430
431 return vk::NoError();
432}
433
434vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface)
435{
436 // We've already initialized a device, and can't re-create it unless it's never been used.
437 // TODO(jmadill): Handle the re-creation case if necessary.
438 if (mDevice != VK_NULL_HANDLE)
439 {
440 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
441
442 // Check if the current device supports present on this surface.
443 VkBool32 supportsPresent = VK_FALSE;
444 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
445 surface, &supportsPresent));
446
447 return (supportsPresent == VK_TRUE);
448 }
449
450 // Find a graphics and present queue.
451 Optional<uint32_t> newPresentQueue;
452 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
453 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
454 {
455 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
456 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
457 {
458 VkBool32 supportsPresent = VK_FALSE;
459 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
460 &supportsPresent));
461
462 if (supportsPresent == VK_TRUE)
463 {
464 newPresentQueue = queueIndex;
465 break;
466 }
467 }
468 }
469
470 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
471 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
472
473 return newPresentQueue.value();
474}
475
476std::string RendererVk::getVendorString() const
477{
478 switch (mPhysicalDeviceProperties.vendorID)
479 {
480 case VENDOR_ID_AMD:
481 return "Advanced Micro Devices";
482 case VENDOR_ID_NVIDIA:
483 return "NVIDIA";
484 case VENDOR_ID_INTEL:
485 return "Intel";
486 default:
487 {
488 // TODO(jmadill): More vendor IDs.
489 std::stringstream strstr;
490 strstr << "Vendor ID: " << mPhysicalDeviceProperties.vendorID;
491 return strstr.str();
492 }
493 }
494}
495
Jamie Madille09bd5d2016-11-29 16:20:35 -0500496std::string RendererVk::getRendererDescription() const
497{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500498 std::stringstream strstr;
499
500 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
501
502 strstr << "Vulkan ";
503 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
504 strstr << VK_VERSION_MINOR(apiVersion) << ".";
505 strstr << VK_VERSION_PATCH(apiVersion);
506
507 strstr << "(" << mPhysicalDeviceProperties.deviceName << ")";
508
509 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500510}
511
Jamie Madillacccc6c2016-05-03 17:22:10 -0400512void RendererVk::ensureCapsInitialized() const
513{
514 if (!mCapsInitialized)
515 {
516 generateCaps(&mNativeCaps, &mNativeTextureCaps, &mNativeExtensions, &mNativeLimitations);
517 mCapsInitialized = true;
518 }
519}
520
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500521void RendererVk::generateCaps(gl::Caps *outCaps,
Jamie Madillacccc6c2016-05-03 17:22:10 -0400522 gl::TextureCapsMap * /*outTextureCaps*/,
Jamie Madillb8353b02017-01-25 12:57:21 -0800523 gl::Extensions *outExtensions,
Jamie Madillacccc6c2016-05-03 17:22:10 -0400524 gl::Limitations * /* outLimitations */) const
525{
Jamie Madill327ba852016-11-30 12:38:28 -0500526 // TODO(jmadill): Caps.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500527 outCaps->maxDrawBuffers = 1;
Jiawei-Shao2597fb62016-12-09 16:38:02 +0800528 outCaps->maxVertexAttributes = gl::MAX_VERTEX_ATTRIBS;
529 outCaps->maxVertexAttribBindings = gl::MAX_VERTEX_ATTRIB_BINDINGS;
Jamie Madill035fd6b2017-10-03 15:43:22 -0400530 outCaps->maxVaryingVectors = 16;
531 outCaps->maxTextureImageUnits = 1;
532 outCaps->maxCombinedTextureImageUnits = 1;
533 outCaps->max2DTextureSize = 1024;
Jamie Madilld03a8492017-10-03 15:46:06 -0400534 outCaps->maxElementIndex = std::numeric_limits<GLuint>::max() - 1;
Jamie Madill6276b922017-09-25 02:35:57 -0400535 outCaps->maxFragmentUniformVectors = 8;
536 outCaps->maxVertexUniformVectors = 8;
Jamie Madillb79e7bb2017-10-24 13:55:50 -0400537 outCaps->maxColorAttachments = 1;
Jamie Madillb8353b02017-01-25 12:57:21 -0800538
539 // Enable this for simple buffer readback testing, but some functionality is missing.
540 // TODO(jmadill): Support full mapBufferRange extension.
541 outExtensions->mapBuffer = true;
542 outExtensions->mapBufferRange = true;
Jamie Madillacccc6c2016-05-03 17:22:10 -0400543}
544
545const gl::Caps &RendererVk::getNativeCaps() const
546{
547 ensureCapsInitialized();
548 return mNativeCaps;
549}
550
551const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
552{
553 ensureCapsInitialized();
554 return mNativeTextureCaps;
555}
556
557const gl::Extensions &RendererVk::getNativeExtensions() const
558{
559 ensureCapsInitialized();
560 return mNativeExtensions;
561}
562
563const gl::Limitations &RendererVk::getNativeLimitations() const
564{
565 ensureCapsInitialized();
566 return mNativeLimitations;
567}
568
Jamie Madill0c0dc342017-03-24 14:18:51 -0400569vk::Error RendererVk::getStartedCommandBuffer(vk::CommandBuffer **commandBufferOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500570{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400571 ANGLE_TRY(mCommandBuffer.begin(mDevice));
572 *commandBufferOut = &mCommandBuffer;
573 return vk::NoError();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500574}
575
Jamie Madill0c0dc342017-03-24 14:18:51 -0400576vk::Error RendererVk::submitCommandBuffer(vk::CommandBuffer *commandBuffer)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500577{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400578 ANGLE_TRY(commandBuffer->end());
579
Jamie Madill4d0bf552016-12-28 15:45:24 -0500580 VkFenceCreateInfo fenceInfo;
581 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
582 fenceInfo.pNext = nullptr;
583 fenceInfo.flags = 0;
584
Jamie Madill4d0bf552016-12-28 15:45:24 -0500585 VkSubmitInfo submitInfo;
586 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
587 submitInfo.pNext = nullptr;
588 submitInfo.waitSemaphoreCount = 0;
589 submitInfo.pWaitSemaphores = nullptr;
590 submitInfo.pWaitDstStageMask = nullptr;
591 submitInfo.commandBufferCount = 1;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400592 submitInfo.pCommandBuffers = commandBuffer->ptr();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500593 submitInfo.signalSemaphoreCount = 0;
594 submitInfo.pSignalSemaphores = nullptr;
595
596 // TODO(jmadill): Investigate how to properly submit command buffers.
Jamie Madill4c26fc22017-02-24 11:04:10 -0500597 ANGLE_TRY(submit(submitInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500598
Jamie Madillf651c772017-02-21 15:03:51 -0500599 return vk::NoError();
600}
601
Jamie Madill0c0dc342017-03-24 14:18:51 -0400602vk::Error RendererVk::submitAndFinishCommandBuffer(vk::CommandBuffer *commandBuffer)
Jamie Madillf651c772017-02-21 15:03:51 -0500603{
604 ANGLE_TRY(submitCommandBuffer(commandBuffer));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500605 ANGLE_TRY(finish());
Jamie Madill4d0bf552016-12-28 15:45:24 -0500606
607 return vk::NoError();
608}
609
Jamie Madill0c0dc342017-03-24 14:18:51 -0400610vk::Error RendererVk::submitCommandsWithSync(vk::CommandBuffer *commandBuffer,
Jamie Madille918de22017-04-12 10:21:11 -0400611 const vk::Semaphore &waitSemaphore,
612 const vk::Semaphore &signalSemaphore)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500613{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400614 ANGLE_TRY(commandBuffer->end());
615
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500616 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
617
618 VkSubmitInfo submitInfo;
619 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
620 submitInfo.pNext = nullptr;
621 submitInfo.waitSemaphoreCount = 1;
Jamie Madille918de22017-04-12 10:21:11 -0400622 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500623 submitInfo.pWaitDstStageMask = &waitStageMask;
624 submitInfo.commandBufferCount = 1;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400625 submitInfo.pCommandBuffers = commandBuffer->ptr();
Jamie Madille918de22017-04-12 10:21:11 -0400626 submitInfo.signalSemaphoreCount = 1;
627 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500628
629 // TODO(jmadill): Investigate how to properly queue command buffer work.
Jamie Madill0c0dc342017-03-24 14:18:51 -0400630 ANGLE_TRY(submitFrame(submitInfo));
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500631
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500632 return vk::NoError();
633}
634
Jamie Madill4c26fc22017-02-24 11:04:10 -0500635vk::Error RendererVk::finish()
636{
637 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500638 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400639 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500640 return vk::NoError();
641}
642
Jamie Madill0c0dc342017-03-24 14:18:51 -0400643void RendererVk::freeAllInFlightResources()
644{
645 for (auto &fence : mInFlightFences)
646 {
647 fence.destroy(mDevice);
648 }
649 mInFlightFences.clear();
650
651 for (auto &command : mInFlightCommands)
652 {
653 command.destroy(mDevice);
654 }
655 mInFlightCommands.clear();
656
657 for (auto &garbage : mGarbage)
658 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400659 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400660 }
661 mGarbage.clear();
662}
663
Jamie Madill4c26fc22017-02-24 11:04:10 -0500664vk::Error RendererVk::checkInFlightCommands()
665{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400666 size_t finishedIndex = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500667
Jamie Madill4c26fc22017-02-24 11:04:10 -0500668 // Check if any in-flight command buffers are finished.
Jamie Madill0c0dc342017-03-24 14:18:51 -0400669 for (size_t index = 0; index < mInFlightFences.size(); index++)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500670 {
Jamie Madill0c0dc342017-03-24 14:18:51 -0400671 auto *inFlightFence = &mInFlightFences[index];
Jamie Madill4c26fc22017-02-24 11:04:10 -0500672
Jamie Madill0c0dc342017-03-24 14:18:51 -0400673 VkResult result = inFlightFence->get().getStatus(mDevice);
674 if (result == VK_NOT_READY)
675 break;
676 ANGLE_VK_TRY(result);
677 finishedIndex = index + 1;
678
679 // Release the fence handle.
680 // TODO(jmadill): Re-use fences.
681 inFlightFence->destroy(mDevice);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500682 }
683
Jamie Madill0c0dc342017-03-24 14:18:51 -0400684 if (finishedIndex == 0)
685 return vk::NoError();
Jamie Madillf651c772017-02-21 15:03:51 -0500686
Jamie Madill0c0dc342017-03-24 14:18:51 -0400687 Serial finishedSerial = mInFlightFences[finishedIndex - 1].queueSerial();
688 mInFlightFences.erase(mInFlightFences.begin(), mInFlightFences.begin() + finishedIndex);
689
690 size_t completedCBIndex = 0;
691 for (size_t cbIndex = 0; cbIndex < mInFlightCommands.size(); ++cbIndex)
692 {
693 auto *inFlightCB = &mInFlightCommands[cbIndex];
694 if (inFlightCB->queueSerial() > finishedSerial)
695 break;
696
697 completedCBIndex = cbIndex + 1;
698 inFlightCB->destroy(mDevice);
699 }
700
701 if (completedCBIndex == 0)
702 return vk::NoError();
703
704 mInFlightCommands.erase(mInFlightCommands.begin(),
705 mInFlightCommands.begin() + completedCBIndex);
706
707 size_t freeIndex = 0;
708 for (; freeIndex < mGarbage.size(); ++freeIndex)
709 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400710 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, finishedSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400711 break;
712 }
713
714 // Remove the entries from the garbage list - they should be ready to go.
715 if (freeIndex > 0)
716 {
717 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500718 }
719
Jamie Madill4c26fc22017-02-24 11:04:10 -0500720 return vk::NoError();
721}
722
723vk::Error RendererVk::submit(const VkSubmitInfo &submitInfo)
724{
Jamie Madill0c0dc342017-03-24 14:18:51 -0400725 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, VK_NULL_HANDLE));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500726
727 // Store this command buffer in the in-flight list.
Jamie Madill0c0dc342017-03-24 14:18:51 -0400728 mInFlightCommands.emplace_back(std::move(mCommandBuffer), mCurrentQueueSerial);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500729
730 // Sanity check.
731 ASSERT(mInFlightCommands.size() < 1000u);
732
Jamie Madill0c0dc342017-03-24 14:18:51 -0400733 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400734 // TODO(jmadill): Overflow check.
735 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400736
737 return vk::NoError();
738}
739
740vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo)
741{
742 VkFenceCreateInfo createInfo;
743 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
744 createInfo.pNext = nullptr;
745 createInfo.flags = 0;
746
747 vk::Fence fence;
748 ANGLE_TRY(fence.init(mDevice, createInfo));
749
750 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, fence.getHandle()));
751
752 // Store this command buffer in the in-flight list.
753 mInFlightFences.emplace_back(std::move(fence), mCurrentQueueSerial);
754 mInFlightCommands.emplace_back(std::move(mCommandBuffer), mCurrentQueueSerial);
755
756 // Sanity check.
757 ASSERT(mInFlightCommands.size() < 1000u);
758
759 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400760 // TODO(jmadill): Overflow check.
761 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400762
763 ANGLE_TRY(checkInFlightCommands());
764
Jamie Madill4c26fc22017-02-24 11:04:10 -0500765 return vk::NoError();
766}
767
Jamie Madill5deea722017-02-16 10:44:46 -0500768vk::Error RendererVk::createStagingImage(TextureDimension dimension,
769 const vk::Format &format,
770 const gl::Extents &extent,
Jamie Madill035fd6b2017-10-03 15:43:22 -0400771 vk::StagingUsage usage,
Jamie Madill5deea722017-02-16 10:44:46 -0500772 vk::StagingImage *imageOut)
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500773{
Jamie Madill035fd6b2017-10-03 15:43:22 -0400774 ANGLE_TRY(imageOut->init(mDevice, mCurrentQueueFamilyIndex, mMemoryProperties, dimension,
775 format.native, extent, usage));
Jamie Madill5deea722017-02-16 10:44:46 -0500776 return vk::NoError();
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500777}
778
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500779GlslangWrapper *RendererVk::getGlslangWrapper()
780{
781 return mGlslangWrapper;
782}
783
Jamie Madill4c26fc22017-02-24 11:04:10 -0500784Serial RendererVk::getCurrentQueueSerial() const
785{
786 return mCurrentQueueSerial;
787}
788
Jamie Madill1b038242017-11-01 15:14:36 -0400789gl::Error RendererVk::ensureInRenderPass(const gl::Context *context, FramebufferVk *framebufferVk)
790{
791 if (mCurrentRenderPassFramebuffer == framebufferVk)
792 {
793 return gl::NoError();
794 }
795
796 if (mCurrentRenderPassFramebuffer)
797 {
798 endRenderPass();
799 }
800 ANGLE_TRY(
801 framebufferVk->beginRenderPass(context, mDevice, &mCommandBuffer, mCurrentQueueSerial));
802 mCurrentRenderPassFramebuffer = framebufferVk;
803 return gl::NoError();
804}
805
806void RendererVk::endRenderPass()
807{
808 if (mCurrentRenderPassFramebuffer)
809 {
810 ASSERT(mCommandBuffer.started());
811 mCommandBuffer.endRenderPass();
812 mCurrentRenderPassFramebuffer = nullptr;
813 }
814}
815
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400816} // namespace rx