blob: abd9f6f6c1dadaf62915e1ed68535516e957c7df [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.
Jamie Madill3c424b42018-01-19 12:35:09 -050013#include "libANGLE/renderer/vulkan/vk_utils.h"
Jamie Madill4d0bf552016-12-28 15:45:24 -050014
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 Madill1f46bc12018-02-20 16:09:43 -050020#include "libANGLE/renderer/vulkan/CommandGraph.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050021#include "libANGLE/renderer/vulkan/CompilerVk.h"
22#include "libANGLE/renderer/vulkan/FramebufferVk.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050023#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
Jamie Madillffa4cbb2018-01-23 13:04:07 -050024#include "libANGLE/renderer/vulkan/ProgramVk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050025#include "libANGLE/renderer/vulkan/VertexArrayVk.h"
Luc Ferrone4741fd2018-01-25 13:25:27 -050026#include "libANGLE/renderer/vulkan/vk_caps_utils.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050027#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050028#include "platform/Platform.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040029
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070030// Consts
31namespace
32{
33const uint32_t kMockVendorID = 0xba5eba11;
34const uint32_t kMockDeviceID = 0xf005ba11;
35constexpr char kMockDeviceName[] = "Vulkan Mock Device";
Omar El Sheikh0359b692018-07-23 17:22:00 -060036constexpr size_t kInFlightCommandsLimit = 50000u;
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070037} // anonymous namespace
38
Jamie Madill9e54b5a2016-05-25 12:57:39 -040039namespace rx
40{
41
Jamie Madille09bd5d2016-11-29 16:20:35 -050042namespace
43{
Luc Ferrondaedf4d2018-03-16 09:28:53 -040044// We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and
45// one for the vertex shader.
46constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2;
Jamie Madille09bd5d2016-11-29 16:20:35 -050047
Omar El Sheikh26c61b22018-06-29 12:50:59 -060048bool ShouldEnableMockICD(const egl::AttributeMap &attribs)
49{
50#if !defined(ANGLE_PLATFORM_ANDROID)
51 // Mock ICD does not currently run on Android
52 return (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
53 EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) ==
54 EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE);
55#else
56 return false;
57#endif // !defined(ANGLE_PLATFORM_ANDROID)
58}
59
Jamie Madille09bd5d2016-11-29 16:20:35 -050060VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
61 const std::vector<const char *> &enabledExtensionNames)
62{
63 // Compile the extensions names into a set.
64 std::set<std::string> extensionNames;
65 for (const auto &extensionProp : extensionProps)
66 {
67 extensionNames.insert(extensionProp.extensionName);
68 }
69
Jamie Madillacf2f3a2017-11-21 19:22:44 -050070 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050071 {
72 if (extensionNames.count(extensionName) == 0)
73 {
74 return VK_ERROR_EXTENSION_NOT_PRESENT;
75 }
76 }
77
78 return VK_SUCCESS;
79}
80
Yuly Novikov199f4292018-01-19 19:04:05 -050081VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
82 VkDebugReportObjectTypeEXT objectType,
83 uint64_t object,
84 size_t location,
85 int32_t messageCode,
86 const char *layerPrefix,
87 const char *message,
88 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -050089{
90 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
91 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050092 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050093#if !defined(NDEBUG)
94 // Abort the call in Debug builds.
95 return VK_TRUE;
96#endif
97 }
98 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
99 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500100 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500101 }
102 else
103 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500104 // Uncomment this if you want Vulkan spam.
105 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500106 }
107
108 return VK_FALSE;
109}
110
Yuly Novikov199f4292018-01-19 19:04:05 -0500111// If we're loading the validation layers, we could be running from any random directory.
112// Change to the executable directory so we can find the layers, then change back to the
113// previous directory to be safe we don't disrupt the application.
114class ScopedVkLoaderEnvironment : angle::NonCopyable
115{
116 public:
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600117 ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD)
118 : mEnableValidationLayers(enableValidationLayers),
119 mEnableMockICD(enableMockICD),
120 mChangedCWD(false),
121 mChangedICDPath(false)
Yuly Novikov199f4292018-01-19 19:04:05 -0500122 {
123// Changing CWD and setting environment variables makes no sense on Android,
124// since this code is a part of Java application there.
125// Android Vulkan loader doesn't need this either.
126#if !defined(ANGLE_PLATFORM_ANDROID)
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600127 if (enableMockICD)
128 {
129 // Override environment variable to use built Mock ICD
130 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
131 mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv);
132 mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON);
133 if (!mChangedICDPath)
134 {
135 ERR() << "Error setting Path for Mock/Null Driver.";
136 mEnableMockICD = false;
137 }
138 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500139 if (mEnableValidationLayers)
140 {
141 const auto &cwd = angle::GetCWD();
142 if (!cwd.valid())
143 {
144 ERR() << "Error getting CWD for Vulkan layers init.";
145 mEnableValidationLayers = false;
146 }
147 else
148 {
149 mPreviousCWD = cwd.value();
150 const char *exeDir = angle::GetExecutableDirectory();
151 mChangedCWD = angle::SetCWD(exeDir);
152 if (!mChangedCWD)
153 {
154 ERR() << "Error setting CWD for Vulkan layers init.";
155 mEnableValidationLayers = false;
156 }
157 }
158 }
159
160 // Override environment variable to use the ANGLE layers.
161 if (mEnableValidationLayers)
162 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700163 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500164 {
165 ERR() << "Error setting environment for Vulkan layers init.";
166 mEnableValidationLayers = false;
167 }
168 }
169#endif // !defined(ANGLE_PLATFORM_ANDROID)
170 }
171
172 ~ScopedVkLoaderEnvironment()
173 {
174 if (mChangedCWD)
175 {
176#if !defined(ANGLE_PLATFORM_ANDROID)
177 ASSERT(mPreviousCWD.valid());
178 angle::SetCWD(mPreviousCWD.value().c_str());
179#endif // !defined(ANGLE_PLATFORM_ANDROID)
180 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600181 if (mChangedICDPath)
182 {
Omar El Sheikh80d4ef12018-07-13 17:08:19 -0600183 if (mPreviousICDPath.value().empty())
184 {
185 angle::UnsetEnvironmentVar(g_VkICDPathEnv);
186 }
187 else
188 {
189 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
190 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600191 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500192 }
193
Jamie Madillaaca96e2018-06-12 10:19:48 -0400194 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500195
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600196 bool canEnableMockICD() const { return mEnableMockICD; }
197
Yuly Novikov199f4292018-01-19 19:04:05 -0500198 private:
199 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600200 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500201 bool mChangedCWD;
202 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600203 bool mChangedICDPath;
204 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500205};
206
Jamie Madill21061022018-07-12 23:56:30 -0400207void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
208 bool preferMockICD,
209 VkPhysicalDevice *physicalDeviceOut,
210 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
211{
212 ASSERT(!physicalDevices.empty());
213 if (preferMockICD)
214 {
215 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
216 {
217 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
218 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
219 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
220 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
221 {
222 *physicalDeviceOut = physicalDevice;
223 return;
224 }
225 }
226 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
227 "physicalDevice instead.";
228 }
229
230 // Fall back to first device.
231 *physicalDeviceOut = physicalDevices[0];
232 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
233}
Jamie Madille09bd5d2016-11-29 16:20:35 -0500234} // anonymous namespace
235
Jamie Madill49ac74b2017-12-21 14:42:33 -0500236// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400237RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500238
Jamie Madillaaca96e2018-06-12 10:19:48 -0400239RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500240
241RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
242 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
243{
244}
245
246RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
247{
248 std::swap(commandPool, other.commandPool);
249 std::swap(fence, other.fence);
250 std::swap(serial, other.serial);
251 return *this;
252}
253
Jamie Madillbea35a62018-07-05 11:54:10 -0400254void RendererVk::CommandBatch::destroy(VkDevice device)
255{
256 commandPool.destroy(device);
257 fence.destroy(device);
258}
259
Jamie Madill9f2a8612017-11-30 12:43:09 -0500260// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500261RendererVk::RendererVk()
262 : mCapsInitialized(false),
263 mInstance(VK_NULL_HANDLE),
264 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500265 mDebugReportCallback(VK_NULL_HANDLE),
266 mPhysicalDevice(VK_NULL_HANDLE),
267 mQueue(VK_NULL_HANDLE),
268 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
269 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400270 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Jamie Madillaaca96e2018-06-12 10:19:48 -0400271 mCurrentQueueSerial(mQueueSerialFactory.generate())
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400272{
273}
274
275RendererVk::~RendererVk()
276{
Jamie Madill21061022018-07-12 23:56:30 -0400277}
278
279void RendererVk::onDestroy(vk::Context *context)
280{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500281 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500282 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500283 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400284 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500285 }
286
Jamie Madillc7918ce2018-06-13 13:25:31 -0400287 mPipelineLayoutCache.destroy(mDevice);
288 mDescriptorSetLayoutCache.destroy(mDevice);
289
Jamie Madill9f2a8612017-11-30 12:43:09 -0500290 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500291 mPipelineCache.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400292 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500293
Jamie Madill06ca6342018-07-12 15:56:53 -0400294 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500295
Jamie Madill5deea722017-02-16 10:44:46 -0500296 if (mCommandPool.valid())
297 {
298 mCommandPool.destroy(mDevice);
299 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500300
301 if (mDevice)
302 {
303 vkDestroyDevice(mDevice, nullptr);
304 mDevice = VK_NULL_HANDLE;
305 }
306
Jamie Madill0448ec82016-12-23 13:41:47 -0500307 if (mDebugReportCallback)
308 {
309 ASSERT(mInstance);
310 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
311 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
312 ASSERT(destroyDebugReportCallback);
313 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
314 }
315
Jamie Madill4d0bf552016-12-28 15:45:24 -0500316 if (mInstance)
317 {
318 vkDestroyInstance(mInstance, nullptr);
319 mInstance = VK_NULL_HANDLE;
320 }
321
Omar El Sheikheb4b8692018-07-17 10:55:40 -0600322 mMemoryProperties.destroy();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500323 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500324}
325
Jamie Madill21061022018-07-12 23:56:30 -0400326angle::Result RendererVk::initialize(vk::Context *context,
327 const egl::AttributeMap &attribs,
328 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500329{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600330 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
331 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500332 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600333 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500334
Jamie Madill0448ec82016-12-23 13:41:47 -0500335 // Gather global layer properties.
336 uint32_t instanceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400337 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500338
339 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
340 if (instanceLayerCount > 0)
341 {
Jamie Madill21061022018-07-12 23:56:30 -0400342 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
343 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500344 }
345
Jamie Madille09bd5d2016-11-29 16:20:35 -0500346 uint32_t instanceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400347 ANGLE_VK_TRY(context,
348 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500349
350 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
351 if (instanceExtensionCount > 0)
352 {
Jamie Madill21061022018-07-12 23:56:30 -0400353 ANGLE_VK_TRY(context, vkEnumerateInstanceExtensionProperties(
354 nullptr, &instanceExtensionCount, instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500355 }
356
Yuly Novikov199f4292018-01-19 19:04:05 -0500357 const char *const *enabledLayerNames = nullptr;
358 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500359 if (mEnableValidationLayers)
360 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500361 bool layersRequested =
362 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
363 mEnableValidationLayers = GetAvailableValidationLayers(
364 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500365 }
366
Jamie Madille09bd5d2016-11-29 16:20:35 -0500367 std::vector<const char *> enabledInstanceExtensions;
368 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500369 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500370
Jamie Madill0448ec82016-12-23 13:41:47 -0500371 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
372 if (mEnableValidationLayers)
373 {
374 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
375 }
376
Jamie Madille09bd5d2016-11-29 16:20:35 -0500377 // Verify the required extensions are in the extension names set. Fail if not.
Jamie Madill21061022018-07-12 23:56:30 -0400378 ANGLE_VK_TRY(context,
379 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500380
Jamie Madill327ba852016-11-30 12:38:28 -0500381 VkApplicationInfo applicationInfo;
382 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
383 applicationInfo.pNext = nullptr;
384 applicationInfo.pApplicationName = "ANGLE";
385 applicationInfo.applicationVersion = 1;
386 applicationInfo.pEngineName = "ANGLE";
387 applicationInfo.engineVersion = 1;
388 applicationInfo.apiVersion = VK_API_VERSION_1_0;
389
390 VkInstanceCreateInfo instanceInfo;
391 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
392 instanceInfo.pNext = nullptr;
393 instanceInfo.flags = 0;
394 instanceInfo.pApplicationInfo = &applicationInfo;
395
Jamie Madille09bd5d2016-11-29 16:20:35 -0500396 // Enable requested layers and extensions.
397 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
398 instanceInfo.ppEnabledExtensionNames =
399 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500400 instanceInfo.enabledLayerCount = enabledLayerCount;
401 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500402
Jamie Madill21061022018-07-12 23:56:30 -0400403 ANGLE_VK_TRY(context, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500404
Jamie Madill0448ec82016-12-23 13:41:47 -0500405 if (mEnableValidationLayers)
406 {
407 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
408
409 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
410 debugReportInfo.pNext = nullptr;
411 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
412 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
413 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
414 debugReportInfo.pfnCallback = &DebugReportCallback;
415 debugReportInfo.pUserData = this;
416
417 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
418 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
419 ASSERT(createDebugReportCallback);
Jamie Madill21061022018-07-12 23:56:30 -0400420 ANGLE_VK_TRY(context, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
421 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500422 }
423
Jamie Madill4d0bf552016-12-28 15:45:24 -0500424 uint32_t physicalDeviceCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400425 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
426 ANGLE_VK_CHECK(context, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500427
428 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700429 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Jamie Madill21061022018-07-12 23:56:30 -0400430 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
431 physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600432 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700433 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500434
435 // Ensure we can find a graphics queue family.
436 uint32_t queueCount = 0;
437 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
438
Jamie Madill21061022018-07-12 23:56:30 -0400439 ANGLE_VK_CHECK(context, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500440
441 mQueueFamilyProperties.resize(queueCount);
442 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
443 mQueueFamilyProperties.data());
444
445 size_t graphicsQueueFamilyCount = false;
446 uint32_t firstGraphicsQueueFamily = 0;
447 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
448 {
449 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
450 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
451 {
452 ASSERT(queueInfo.queueCount > 0);
453 graphicsQueueFamilyCount++;
454 if (firstGraphicsQueueFamily == 0)
455 {
456 firstGraphicsQueueFamily = familyIndex;
457 }
458 break;
459 }
460 }
461
Jamie Madill21061022018-07-12 23:56:30 -0400462 ANGLE_VK_CHECK(context, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500463
Jamie Madill12222072018-07-11 14:59:48 -0400464 initFeatures();
465
Jamie Madill4d0bf552016-12-28 15:45:24 -0500466 // If only one queue family, go ahead and initialize the device. If there is more than one
467 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
468 if (graphicsQueueFamilyCount == 1)
469 {
Jamie Madill21061022018-07-12 23:56:30 -0400470 ANGLE_TRY(initializeDevice(context, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500471 }
472
Jamie Madill035fd6b2017-10-03 15:43:22 -0400473 // Store the physical device memory properties so we can find the right memory pools.
474 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500475
Jamie Madill06ca6342018-07-12 15:56:53 -0400476 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500477
Jamie Madill6a89d222017-11-02 11:59:51 -0400478 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500479 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
480 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400481
Jamie Madill21061022018-07-12 23:56:30 -0400482 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400483}
484
Jamie Madill21061022018-07-12 23:56:30 -0400485angle::Result RendererVk::initializeDevice(vk::Context *context, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500486{
487 uint32_t deviceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400488 ANGLE_VK_TRY(context,
489 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500490
491 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
492 if (deviceLayerCount > 0)
493 {
Jamie Madill21061022018-07-12 23:56:30 -0400494 ANGLE_VK_TRY(context, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
495 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500496 }
497
498 uint32_t deviceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400499 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
500 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500501
502 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
503 if (deviceExtensionCount > 0)
504 {
Jamie Madill21061022018-07-12 23:56:30 -0400505 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
506 &deviceExtensionCount,
507 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500508 }
509
Yuly Novikov199f4292018-01-19 19:04:05 -0500510 const char *const *enabledLayerNames = nullptr;
511 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500512 if (mEnableValidationLayers)
513 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500514 mEnableValidationLayers = GetAvailableValidationLayers(
515 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500516 }
517
518 std::vector<const char *> enabledDeviceExtensions;
519 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
520
Luc Ferronbf6dc372018-06-28 15:24:19 -0400521 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
522 if (getFeatures().flipViewportY)
523 {
524 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
525 }
526
Jamie Madill21061022018-07-12 23:56:30 -0400527 ANGLE_VK_TRY(context, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500528
529 VkDeviceQueueCreateInfo queueCreateInfo;
530
531 float zeroPriority = 0.0f;
532
533 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
534 queueCreateInfo.pNext = nullptr;
535 queueCreateInfo.flags = 0;
536 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
537 queueCreateInfo.queueCount = 1;
538 queueCreateInfo.pQueuePriorities = &zeroPriority;
539
540 // Initialize the device
541 VkDeviceCreateInfo createInfo;
542
Jamie Madill50cf2be2018-06-15 09:46:57 -0400543 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
544 createInfo.pNext = nullptr;
545 createInfo.flags = 0;
546 createInfo.queueCreateInfoCount = 1;
547 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500548 createInfo.enabledLayerCount = enabledLayerCount;
549 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500550 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
551 createInfo.ppEnabledExtensionNames =
552 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
553 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
554
Jamie Madill21061022018-07-12 23:56:30 -0400555 ANGLE_VK_TRY(context, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500556
557 mCurrentQueueFamilyIndex = queueFamilyIndex;
558
559 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
560
561 // Initialize the command pool now that we know the queue family index.
562 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500563 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
564 commandPoolInfo.pNext = nullptr;
565 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500566 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
567
Jamie Madill21061022018-07-12 23:56:30 -0400568 ANGLE_TRY(mCommandPool.init(context, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500569
Jamie Madill21061022018-07-12 23:56:30 -0400570 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500571}
572
Jamie Madill21061022018-07-12 23:56:30 -0400573angle::Result RendererVk::selectPresentQueueForSurface(vk::Context *context,
574 VkSurfaceKHR surface,
575 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500576{
577 // We've already initialized a device, and can't re-create it unless it's never been used.
578 // TODO(jmadill): Handle the re-creation case if necessary.
579 if (mDevice != VK_NULL_HANDLE)
580 {
581 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
582
583 // Check if the current device supports present on this surface.
584 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400585 ANGLE_VK_TRY(context,
586 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500587 surface, &supportsPresent));
588
Jamie Madill6cad7732018-07-11 09:01:17 -0400589 if (supportsPresent == VK_TRUE)
590 {
591 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400592 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400593 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500594 }
595
596 // Find a graphics and present queue.
597 Optional<uint32_t> newPresentQueue;
598 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
599 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
600 {
601 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
602 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
603 {
604 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400605 ANGLE_VK_TRY(context, vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex,
606 surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500607
608 if (supportsPresent == VK_TRUE)
609 {
610 newPresentQueue = queueIndex;
611 break;
612 }
613 }
614 }
615
Jamie Madill21061022018-07-12 23:56:30 -0400616 ANGLE_VK_CHECK(context, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
617 ANGLE_TRY(initializeDevice(context, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500618
Jamie Madill6cad7732018-07-11 09:01:17 -0400619 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400620 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500621}
622
623std::string RendererVk::getVendorString() const
624{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300625 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500626}
627
Jamie Madille09bd5d2016-11-29 16:20:35 -0500628std::string RendererVk::getRendererDescription() const
629{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500630 std::stringstream strstr;
631
632 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
633
634 strstr << "Vulkan ";
635 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
636 strstr << VK_VERSION_MINOR(apiVersion) << ".";
637 strstr << VK_VERSION_PATCH(apiVersion);
638
Olli Etuahoc6a06182018-04-13 14:11:46 +0300639 strstr << "(";
640
641 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
642 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
643 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
644 // driver detection.
645 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
646 {
647 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
648 }
649
650 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500651
652 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500653}
654
Jamie Madill12222072018-07-11 14:59:48 -0400655void RendererVk::initFeatures()
656{
657 // Use OpenGL line rasterization rules by default.
658 mFeatures.basicGLLineRasterization = true;
659
Luc Ferronf786b702018-07-10 11:01:43 -0400660 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
661 // investigation. http://anglebug.com/2728
662 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Jamie Madill12222072018-07-11 14:59:48 -0400663}
664
Jamie Madillacccc6c2016-05-03 17:22:10 -0400665void RendererVk::ensureCapsInitialized() const
666{
667 if (!mCapsInitialized)
668 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500669 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Jamie Madill12222072018-07-11 14:59:48 -0400670 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400671 mCapsInitialized = true;
672 }
673}
674
Jamie Madillacccc6c2016-05-03 17:22:10 -0400675const gl::Caps &RendererVk::getNativeCaps() const
676{
677 ensureCapsInitialized();
678 return mNativeCaps;
679}
680
681const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
682{
683 ensureCapsInitialized();
684 return mNativeTextureCaps;
685}
686
687const gl::Extensions &RendererVk::getNativeExtensions() const
688{
689 ensureCapsInitialized();
690 return mNativeExtensions;
691}
692
693const gl::Limitations &RendererVk::getNativeLimitations() const
694{
695 ensureCapsInitialized();
696 return mNativeLimitations;
697}
698
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400699uint32_t RendererVk::getMaxActiveTextures()
700{
701 // TODO(lucferron): expose this limitation to GL in Context Caps
702 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
703 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
704}
705
Jamie Madill49ac74b2017-12-21 14:42:33 -0500706const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500707{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500708 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500709}
710
Jamie Madill21061022018-07-12 23:56:30 -0400711angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500712{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500713 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500714 {
Luc Ferron1617e692018-07-11 11:08:19 -0400715 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
716 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400717
Jamie Madill49ac74b2017-12-21 14:42:33 -0500718 VkSubmitInfo submitInfo;
719 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
720 submitInfo.pNext = nullptr;
721 submitInfo.waitSemaphoreCount = 0;
722 submitInfo.pWaitSemaphores = nullptr;
723 submitInfo.pWaitDstStageMask = nullptr;
724 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400725 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500726 submitInfo.signalSemaphoreCount = 0;
727 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500728
Jamie Madill21061022018-07-12 23:56:30 -0400729 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500730 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500731
Jamie Madill4c26fc22017-02-24 11:04:10 -0500732 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400733 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400734 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400735 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500736}
737
Jamie Madill0c0dc342017-03-24 14:18:51 -0400738void RendererVk::freeAllInFlightResources()
739{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500740 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400741 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500742 batch.fence.destroy(mDevice);
743 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400744 }
745 mInFlightCommands.clear();
746
747 for (auto &garbage : mGarbage)
748 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400749 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400750 }
751 mGarbage.clear();
752}
753
Jamie Madill21061022018-07-12 23:56:30 -0400754angle::Result RendererVk::checkInFlightCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500755{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500756 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500757
Jamie Madill49ac74b2017-12-21 14:42:33 -0500758 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500759 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500760 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400761 if (result == VK_NOT_READY)
762 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500763
Jamie Madill21061022018-07-12 23:56:30 -0400764 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500765 ASSERT(batch.serial > mLastCompletedQueueSerial);
766 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400767
Jamie Madill49ac74b2017-12-21 14:42:33 -0500768 batch.fence.destroy(mDevice);
769 batch.commandPool.destroy(mDevice);
770 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500771 }
772
Jamie Madill49ac74b2017-12-21 14:42:33 -0500773 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400774
775 size_t freeIndex = 0;
776 for (; freeIndex < mGarbage.size(); ++freeIndex)
777 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500778 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400779 break;
780 }
781
782 // Remove the entries from the garbage list - they should be ready to go.
783 if (freeIndex > 0)
784 {
785 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500786 }
787
Jamie Madill21061022018-07-12 23:56:30 -0400788 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500789}
790
Jamie Madill21061022018-07-12 23:56:30 -0400791angle::Result RendererVk::submitFrame(vk::Context *context,
792 const VkSubmitInfo &submitInfo,
793 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500794{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500795 VkFenceCreateInfo fenceInfo;
796 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
797 fenceInfo.pNext = nullptr;
798 fenceInfo.flags = 0;
799
Jamie Madillbea35a62018-07-05 11:54:10 -0400800 vk::Scoped<CommandBatch> scopedBatch(mDevice);
801 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400802 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500803
Jamie Madill21061022018-07-12 23:56:30 -0400804 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500805
806 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500807 batch.commandPool = std::move(mCommandPool);
808 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500809
Jamie Madillbea35a62018-07-05 11:54:10 -0400810 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400811
Omar El Sheikh0359b692018-07-23 17:22:00 -0600812 // Check that mInFlightCommands isn't growing too fast
813 // If it is, wait for the queue to complete work it has alread been assigned
814 if (mInFlightCommands.size() > kInFlightCommandsLimit)
815 {
816 vkQueueWaitIdle(mQueue);
817 }
Jamie Madill0c0dc342017-03-24 14:18:51 -0400818
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
Jamie Madill21061022018-07-12 23:56:30 -0400823 ANGLE_TRY(checkInFlightCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400824
Jamie Madill49ac74b2017-12-21 14:42:33 -0500825 // Simply null out the command buffer here - it was allocated using the command pool.
826 commandBuffer.releaseHandle();
827
828 // Reallocate the command pool for next frame.
829 // TODO(jmadill): Consider reusing command pools.
830 VkCommandPoolCreateInfo poolInfo;
831 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
832 poolInfo.pNext = nullptr;
833 poolInfo.flags = 0;
834 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
835
Jamie Madill21061022018-07-12 23:56:30 -0400836 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500837
Jamie Madill21061022018-07-12 23:56:30 -0400838 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500839}
840
Jamie Madill4c26fc22017-02-24 11:04:10 -0500841Serial RendererVk::getCurrentQueueSerial() const
842{
843 return mCurrentQueueSerial;
844}
845
Jamie Madillaaca96e2018-06-12 10:19:48 -0400846bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500847{
848 return serial > mLastCompletedQueueSerial;
849}
850
Jamie Madill21061022018-07-12 23:56:30 -0400851angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
852 const vk::RenderPassDesc &desc,
853 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500854{
Jamie Madill21061022018-07-12 23:56:30 -0400855 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -0500856 renderPassOut);
857}
858
Jamie Madill21061022018-07-12 23:56:30 -0400859angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
860 const vk::RenderPassDesc &desc,
861 const vk::AttachmentOpsArray &ops,
862 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500863{
Jamie Madill21061022018-07-12 23:56:30 -0400864 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -0500865 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500866}
867
Jamie Madilla5e06072018-05-18 14:36:05 -0400868vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500869{
Jamie Madilla5e06072018-05-18 14:36:05 -0400870 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500871}
872
Jamie Madill21061022018-07-12 23:56:30 -0400873angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500874{
Jamie Madill21061022018-07-12 23:56:30 -0400875 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500876 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500877}
878
Jamie Madill21061022018-07-12 23:56:30 -0400879angle::Result RendererVk::flush(vk::Context *context,
880 const vk::Semaphore &waitSemaphore,
881 const vk::Semaphore &signalSemaphore)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500882{
Jamie Madillbea35a62018-07-05 11:54:10 -0400883 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
884 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500885
886 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
887
888 VkSubmitInfo submitInfo;
889 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
890 submitInfo.pNext = nullptr;
891 submitInfo.waitSemaphoreCount = 1;
892 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
893 submitInfo.pWaitDstStageMask = &waitStageMask;
894 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400895 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500896 submitInfo.signalSemaphoreCount = 1;
897 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
898
Jamie Madill21061022018-07-12 23:56:30 -0400899 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
900 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500901}
902
Jamie Madill78feddc2018-04-27 11:45:05 -0400903Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500904{
Jamie Madill78feddc2018-04-27 11:45:05 -0400905 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500906}
907
Jamie Madill06ca6342018-07-12 15:56:53 -0400908angle::Result RendererVk::getPipeline(vk::Context *context,
909 const vk::ShaderAndSerial &vertexShader,
910 const vk::ShaderAndSerial &fragmentShader,
911 const vk::PipelineLayout &pipelineLayout,
912 const vk::PipelineDesc &pipelineDesc,
913 const gl::AttributesMask &activeAttribLocationsMask,
914 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500915{
Jamie Madill06ca6342018-07-12 15:56:53 -0400916 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400917 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -0400918 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400919 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
920
921 // Pull in a compatible RenderPass.
922 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400923 ANGLE_TRY(
924 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -0400925
Jamie Madill21061022018-07-12 23:56:30 -0400926 return mPipelineCache.getPipeline(context, *compatibleRenderPass, pipelineLayout,
Jamie Madill9aef3672018-04-27 11:45:06 -0400927 activeAttribLocationsMask, vertexShader.get(),
928 fragmentShader.get(), pipelineDesc, pipelineOut);
929}
930
Jamie Madill21061022018-07-12 23:56:30 -0400931angle::Result RendererVk::getDescriptorSetLayout(
932 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400933 const vk::DescriptorSetLayoutDesc &desc,
934 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
935{
Jamie Madill21061022018-07-12 23:56:30 -0400936 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -0400937}
938
Jamie Madill21061022018-07-12 23:56:30 -0400939angle::Result RendererVk::getPipelineLayout(
940 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400941 const vk::PipelineLayoutDesc &desc,
942 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
943 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
944{
Jamie Madill21061022018-07-12 23:56:30 -0400945 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -0400946 pipelineLayoutOut);
947}
948
Jamie Madilld47044a2018-04-27 11:45:03 -0400949vk::ShaderLibrary *RendererVk::getShaderLibrary()
950{
951 return &mShaderLibrary;
952}
Luc Ferron90968362018-05-04 08:47:22 -0400953
Jamie Madillaaca96e2018-06-12 10:19:48 -0400954uint32_t GetUniformBufferDescriptorCount()
955{
956 return kUniformBufferDescriptorsPerDescriptorSet;
957}
958
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400959} // namespace rx