blob: d748b03e1be1e0a5d0fab90a831e30e409817639 [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 }
Jamie Madill46848422018-08-09 10:46:06 -0400139 if (mEnableValidationLayers || mEnableMockICD)
Yuly Novikov199f4292018-01-19 19:04:05 -0500140 {
141 const auto &cwd = angle::GetCWD();
142 if (!cwd.valid())
143 {
144 ERR() << "Error getting CWD for Vulkan layers init.";
145 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400146 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500147 }
148 else
149 {
150 mPreviousCWD = cwd.value();
151 const char *exeDir = angle::GetExecutableDirectory();
152 mChangedCWD = angle::SetCWD(exeDir);
153 if (!mChangedCWD)
154 {
155 ERR() << "Error setting CWD for Vulkan layers init.";
156 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400157 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500158 }
159 }
160 }
161
162 // Override environment variable to use the ANGLE layers.
163 if (mEnableValidationLayers)
164 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700165 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500166 {
167 ERR() << "Error setting environment for Vulkan layers init.";
168 mEnableValidationLayers = false;
169 }
170 }
171#endif // !defined(ANGLE_PLATFORM_ANDROID)
172 }
173
174 ~ScopedVkLoaderEnvironment()
175 {
176 if (mChangedCWD)
177 {
178#if !defined(ANGLE_PLATFORM_ANDROID)
179 ASSERT(mPreviousCWD.valid());
180 angle::SetCWD(mPreviousCWD.value().c_str());
181#endif // !defined(ANGLE_PLATFORM_ANDROID)
182 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600183 if (mChangedICDPath)
184 {
Omar El Sheikh80d4ef12018-07-13 17:08:19 -0600185 if (mPreviousICDPath.value().empty())
186 {
187 angle::UnsetEnvironmentVar(g_VkICDPathEnv);
188 }
189 else
190 {
191 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
192 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600193 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500194 }
195
Jamie Madillaaca96e2018-06-12 10:19:48 -0400196 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500197
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600198 bool canEnableMockICD() const { return mEnableMockICD; }
199
Yuly Novikov199f4292018-01-19 19:04:05 -0500200 private:
201 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600202 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500203 bool mChangedCWD;
204 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600205 bool mChangedICDPath;
206 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500207};
208
Jamie Madill21061022018-07-12 23:56:30 -0400209void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
210 bool preferMockICD,
211 VkPhysicalDevice *physicalDeviceOut,
212 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
213{
214 ASSERT(!physicalDevices.empty());
215 if (preferMockICD)
216 {
217 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
218 {
219 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
220 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
221 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
222 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
223 {
224 *physicalDeviceOut = physicalDevice;
225 return;
226 }
227 }
228 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
229 "physicalDevice instead.";
230 }
231
232 // Fall back to first device.
233 *physicalDeviceOut = physicalDevices[0];
234 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
235}
Jamie Madille09bd5d2016-11-29 16:20:35 -0500236} // anonymous namespace
237
Jamie Madill49ac74b2017-12-21 14:42:33 -0500238// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400239RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500240
Jamie Madillaaca96e2018-06-12 10:19:48 -0400241RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500242
243RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
244 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
245{
246}
247
248RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
249{
250 std::swap(commandPool, other.commandPool);
251 std::swap(fence, other.fence);
252 std::swap(serial, other.serial);
253 return *this;
254}
255
Jamie Madillbea35a62018-07-05 11:54:10 -0400256void RendererVk::CommandBatch::destroy(VkDevice device)
257{
258 commandPool.destroy(device);
259 fence.destroy(device);
260}
261
Jamie Madill9f2a8612017-11-30 12:43:09 -0500262// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500263RendererVk::RendererVk()
264 : mCapsInitialized(false),
265 mInstance(VK_NULL_HANDLE),
266 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500267 mDebugReportCallback(VK_NULL_HANDLE),
268 mPhysicalDevice(VK_NULL_HANDLE),
269 mQueue(VK_NULL_HANDLE),
270 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
271 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400272 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Jamie Madillaaca96e2018-06-12 10:19:48 -0400273 mCurrentQueueSerial(mQueueSerialFactory.generate())
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400274{
275}
276
277RendererVk::~RendererVk()
278{
Jamie Madill21061022018-07-12 23:56:30 -0400279}
280
281void RendererVk::onDestroy(vk::Context *context)
282{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500283 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500284 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500285 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400286 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500287 }
288
Jamie Madillc7918ce2018-06-13 13:25:31 -0400289 mPipelineLayoutCache.destroy(mDevice);
290 mDescriptorSetLayoutCache.destroy(mDevice);
291
Jamie Madill9f2a8612017-11-30 12:43:09 -0500292 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500293 mPipelineCache.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400294 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500295
Jamie Madill06ca6342018-07-12 15:56:53 -0400296 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500297
Jamie Madill5deea722017-02-16 10:44:46 -0500298 if (mCommandPool.valid())
299 {
300 mCommandPool.destroy(mDevice);
301 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500302
303 if (mDevice)
304 {
305 vkDestroyDevice(mDevice, nullptr);
306 mDevice = VK_NULL_HANDLE;
307 }
308
Jamie Madill0448ec82016-12-23 13:41:47 -0500309 if (mDebugReportCallback)
310 {
311 ASSERT(mInstance);
312 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
313 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
314 ASSERT(destroyDebugReportCallback);
315 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
316 }
317
Jamie Madill4d0bf552016-12-28 15:45:24 -0500318 if (mInstance)
319 {
320 vkDestroyInstance(mInstance, nullptr);
321 mInstance = VK_NULL_HANDLE;
322 }
323
Omar El Sheikheb4b8692018-07-17 10:55:40 -0600324 mMemoryProperties.destroy();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500325 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500326}
327
Jamie Madill21061022018-07-12 23:56:30 -0400328angle::Result RendererVk::initialize(vk::Context *context,
329 const egl::AttributeMap &attribs,
330 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500331{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600332 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
333 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500334 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600335 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500336
Jamie Madill0448ec82016-12-23 13:41:47 -0500337 // Gather global layer properties.
338 uint32_t instanceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400339 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500340
341 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
342 if (instanceLayerCount > 0)
343 {
Jamie Madill21061022018-07-12 23:56:30 -0400344 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
345 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500346 }
347
Jamie Madille09bd5d2016-11-29 16:20:35 -0500348 uint32_t instanceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400349 ANGLE_VK_TRY(context,
350 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500351
352 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
353 if (instanceExtensionCount > 0)
354 {
Jamie Madill21061022018-07-12 23:56:30 -0400355 ANGLE_VK_TRY(context, vkEnumerateInstanceExtensionProperties(
356 nullptr, &instanceExtensionCount, instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500357 }
358
Yuly Novikov199f4292018-01-19 19:04:05 -0500359 const char *const *enabledLayerNames = nullptr;
360 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500361 if (mEnableValidationLayers)
362 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500363 bool layersRequested =
364 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
365 mEnableValidationLayers = GetAvailableValidationLayers(
366 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500367 }
368
Jamie Madille09bd5d2016-11-29 16:20:35 -0500369 std::vector<const char *> enabledInstanceExtensions;
370 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500371 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500372
Jamie Madill0448ec82016-12-23 13:41:47 -0500373 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
374 if (mEnableValidationLayers)
375 {
376 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
377 }
378
Jamie Madille09bd5d2016-11-29 16:20:35 -0500379 // Verify the required extensions are in the extension names set. Fail if not.
Jamie Madill21061022018-07-12 23:56:30 -0400380 ANGLE_VK_TRY(context,
381 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500382
Jamie Madill327ba852016-11-30 12:38:28 -0500383 VkApplicationInfo applicationInfo;
384 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
385 applicationInfo.pNext = nullptr;
386 applicationInfo.pApplicationName = "ANGLE";
387 applicationInfo.applicationVersion = 1;
388 applicationInfo.pEngineName = "ANGLE";
389 applicationInfo.engineVersion = 1;
390 applicationInfo.apiVersion = VK_API_VERSION_1_0;
391
392 VkInstanceCreateInfo instanceInfo;
393 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
394 instanceInfo.pNext = nullptr;
395 instanceInfo.flags = 0;
396 instanceInfo.pApplicationInfo = &applicationInfo;
397
Jamie Madille09bd5d2016-11-29 16:20:35 -0500398 // Enable requested layers and extensions.
399 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
400 instanceInfo.ppEnabledExtensionNames =
401 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500402 instanceInfo.enabledLayerCount = enabledLayerCount;
403 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500404
Jamie Madill21061022018-07-12 23:56:30 -0400405 ANGLE_VK_TRY(context, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500406
Jamie Madill0448ec82016-12-23 13:41:47 -0500407 if (mEnableValidationLayers)
408 {
409 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
410
411 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
412 debugReportInfo.pNext = nullptr;
413 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
414 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
415 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
416 debugReportInfo.pfnCallback = &DebugReportCallback;
417 debugReportInfo.pUserData = this;
418
419 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
420 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
421 ASSERT(createDebugReportCallback);
Jamie Madill21061022018-07-12 23:56:30 -0400422 ANGLE_VK_TRY(context, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
423 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500424 }
425
Jamie Madill4d0bf552016-12-28 15:45:24 -0500426 uint32_t physicalDeviceCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400427 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
428 ANGLE_VK_CHECK(context, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500429
430 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700431 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Jamie Madill21061022018-07-12 23:56:30 -0400432 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
433 physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600434 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700435 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500436
437 // Ensure we can find a graphics queue family.
438 uint32_t queueCount = 0;
439 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
440
Jamie Madill21061022018-07-12 23:56:30 -0400441 ANGLE_VK_CHECK(context, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500442
443 mQueueFamilyProperties.resize(queueCount);
444 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
445 mQueueFamilyProperties.data());
446
447 size_t graphicsQueueFamilyCount = false;
448 uint32_t firstGraphicsQueueFamily = 0;
449 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
450 {
451 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
452 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
453 {
454 ASSERT(queueInfo.queueCount > 0);
455 graphicsQueueFamilyCount++;
456 if (firstGraphicsQueueFamily == 0)
457 {
458 firstGraphicsQueueFamily = familyIndex;
459 }
460 break;
461 }
462 }
463
Jamie Madill21061022018-07-12 23:56:30 -0400464 ANGLE_VK_CHECK(context, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500465
Jamie Madill12222072018-07-11 14:59:48 -0400466 initFeatures();
467
Jamie Madill4d0bf552016-12-28 15:45:24 -0500468 // If only one queue family, go ahead and initialize the device. If there is more than one
469 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
470 if (graphicsQueueFamilyCount == 1)
471 {
Jamie Madill21061022018-07-12 23:56:30 -0400472 ANGLE_TRY(initializeDevice(context, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500473 }
474
Jamie Madill035fd6b2017-10-03 15:43:22 -0400475 // Store the physical device memory properties so we can find the right memory pools.
476 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500477
Jamie Madill06ca6342018-07-12 15:56:53 -0400478 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500479
Jamie Madill6a89d222017-11-02 11:59:51 -0400480 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500481 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
482 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400483
Jamie Madill21061022018-07-12 23:56:30 -0400484 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400485}
486
Jamie Madill21061022018-07-12 23:56:30 -0400487angle::Result RendererVk::initializeDevice(vk::Context *context, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500488{
489 uint32_t deviceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400490 ANGLE_VK_TRY(context,
491 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500492
493 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
494 if (deviceLayerCount > 0)
495 {
Jamie Madill21061022018-07-12 23:56:30 -0400496 ANGLE_VK_TRY(context, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
497 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500498 }
499
500 uint32_t deviceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400501 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
502 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500503
504 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
505 if (deviceExtensionCount > 0)
506 {
Jamie Madill21061022018-07-12 23:56:30 -0400507 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
508 &deviceExtensionCount,
509 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500510 }
511
Yuly Novikov199f4292018-01-19 19:04:05 -0500512 const char *const *enabledLayerNames = nullptr;
513 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500514 if (mEnableValidationLayers)
515 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500516 mEnableValidationLayers = GetAvailableValidationLayers(
517 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500518 }
519
520 std::vector<const char *> enabledDeviceExtensions;
521 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
522
Luc Ferronbf6dc372018-06-28 15:24:19 -0400523 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
524 if (getFeatures().flipViewportY)
525 {
526 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
527 }
528
Jamie Madill21061022018-07-12 23:56:30 -0400529 ANGLE_VK_TRY(context, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500530
531 VkDeviceQueueCreateInfo queueCreateInfo;
532
533 float zeroPriority = 0.0f;
534
535 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
536 queueCreateInfo.pNext = nullptr;
537 queueCreateInfo.flags = 0;
538 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
539 queueCreateInfo.queueCount = 1;
540 queueCreateInfo.pQueuePriorities = &zeroPriority;
541
542 // Initialize the device
543 VkDeviceCreateInfo createInfo;
544
Jamie Madill50cf2be2018-06-15 09:46:57 -0400545 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
546 createInfo.pNext = nullptr;
547 createInfo.flags = 0;
548 createInfo.queueCreateInfoCount = 1;
549 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500550 createInfo.enabledLayerCount = enabledLayerCount;
551 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500552 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
553 createInfo.ppEnabledExtensionNames =
554 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
555 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
556
Jamie Madill21061022018-07-12 23:56:30 -0400557 ANGLE_VK_TRY(context, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500558
559 mCurrentQueueFamilyIndex = queueFamilyIndex;
560
561 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
562
563 // Initialize the command pool now that we know the queue family index.
564 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500565 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
566 commandPoolInfo.pNext = nullptr;
567 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500568 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
569
Jamie Madill21061022018-07-12 23:56:30 -0400570 ANGLE_TRY(mCommandPool.init(context, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500571
Jamie Madill21061022018-07-12 23:56:30 -0400572 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500573}
574
Jamie Madill21061022018-07-12 23:56:30 -0400575angle::Result RendererVk::selectPresentQueueForSurface(vk::Context *context,
576 VkSurfaceKHR surface,
577 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500578{
579 // We've already initialized a device, and can't re-create it unless it's never been used.
580 // TODO(jmadill): Handle the re-creation case if necessary.
581 if (mDevice != VK_NULL_HANDLE)
582 {
583 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
584
585 // Check if the current device supports present on this surface.
586 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400587 ANGLE_VK_TRY(context,
588 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500589 surface, &supportsPresent));
590
Jamie Madill6cad7732018-07-11 09:01:17 -0400591 if (supportsPresent == VK_TRUE)
592 {
593 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400594 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400595 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500596 }
597
598 // Find a graphics and present queue.
599 Optional<uint32_t> newPresentQueue;
600 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
601 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
602 {
603 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
604 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
605 {
606 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400607 ANGLE_VK_TRY(context, vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex,
608 surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500609
610 if (supportsPresent == VK_TRUE)
611 {
612 newPresentQueue = queueIndex;
613 break;
614 }
615 }
616 }
617
Jamie Madill21061022018-07-12 23:56:30 -0400618 ANGLE_VK_CHECK(context, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
619 ANGLE_TRY(initializeDevice(context, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500620
Jamie Madill6cad7732018-07-11 09:01:17 -0400621 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400622 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500623}
624
625std::string RendererVk::getVendorString() const
626{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300627 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500628}
629
Jamie Madille09bd5d2016-11-29 16:20:35 -0500630std::string RendererVk::getRendererDescription() const
631{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500632 std::stringstream strstr;
633
634 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
635
636 strstr << "Vulkan ";
637 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
638 strstr << VK_VERSION_MINOR(apiVersion) << ".";
639 strstr << VK_VERSION_PATCH(apiVersion);
640
Olli Etuahoc6a06182018-04-13 14:11:46 +0300641 strstr << "(";
642
643 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
644 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
645 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
646 // driver detection.
647 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
648 {
649 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
650 }
651
652 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500653
654 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500655}
656
Jamie Madill12222072018-07-11 14:59:48 -0400657void RendererVk::initFeatures()
658{
659 // Use OpenGL line rasterization rules by default.
660 mFeatures.basicGLLineRasterization = true;
661
Luc Ferronf786b702018-07-10 11:01:43 -0400662 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
663 // investigation. http://anglebug.com/2728
664 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Jamie Madill12222072018-07-11 14:59:48 -0400665}
666
Jamie Madillacccc6c2016-05-03 17:22:10 -0400667void RendererVk::ensureCapsInitialized() const
668{
669 if (!mCapsInitialized)
670 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500671 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Jamie Madill12222072018-07-11 14:59:48 -0400672 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400673 mCapsInitialized = true;
674 }
675}
676
Jamie Madillacccc6c2016-05-03 17:22:10 -0400677const gl::Caps &RendererVk::getNativeCaps() const
678{
679 ensureCapsInitialized();
680 return mNativeCaps;
681}
682
683const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
684{
685 ensureCapsInitialized();
686 return mNativeTextureCaps;
687}
688
689const gl::Extensions &RendererVk::getNativeExtensions() const
690{
691 ensureCapsInitialized();
692 return mNativeExtensions;
693}
694
695const gl::Limitations &RendererVk::getNativeLimitations() const
696{
697 ensureCapsInitialized();
698 return mNativeLimitations;
699}
700
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400701uint32_t RendererVk::getMaxActiveTextures()
702{
703 // TODO(lucferron): expose this limitation to GL in Context Caps
704 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
705 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
706}
707
Jamie Madill49ac74b2017-12-21 14:42:33 -0500708const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500709{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500710 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500711}
712
Jamie Madill21061022018-07-12 23:56:30 -0400713angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500714{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500715 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500716 {
Luc Ferron1617e692018-07-11 11:08:19 -0400717 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
718 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400719
Jamie Madill49ac74b2017-12-21 14:42:33 -0500720 VkSubmitInfo submitInfo;
721 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
722 submitInfo.pNext = nullptr;
723 submitInfo.waitSemaphoreCount = 0;
724 submitInfo.pWaitSemaphores = nullptr;
725 submitInfo.pWaitDstStageMask = nullptr;
726 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400727 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500728 submitInfo.signalSemaphoreCount = 0;
729 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500730
Jamie Madill21061022018-07-12 23:56:30 -0400731 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500732 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500733
Jamie Madill4c26fc22017-02-24 11:04:10 -0500734 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400735 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400736 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400737 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500738}
739
Jamie Madill0c0dc342017-03-24 14:18:51 -0400740void RendererVk::freeAllInFlightResources()
741{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500742 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400743 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500744 batch.fence.destroy(mDevice);
745 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400746 }
747 mInFlightCommands.clear();
748
749 for (auto &garbage : mGarbage)
750 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400751 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400752 }
753 mGarbage.clear();
754}
755
Jamie Madill21061022018-07-12 23:56:30 -0400756angle::Result RendererVk::checkInFlightCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500757{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500758 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500759
Jamie Madill49ac74b2017-12-21 14:42:33 -0500760 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500761 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500762 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400763 if (result == VK_NOT_READY)
764 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500765
Jamie Madill21061022018-07-12 23:56:30 -0400766 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500767 ASSERT(batch.serial > mLastCompletedQueueSerial);
768 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400769
Jamie Madill49ac74b2017-12-21 14:42:33 -0500770 batch.fence.destroy(mDevice);
771 batch.commandPool.destroy(mDevice);
772 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500773 }
774
Jamie Madill49ac74b2017-12-21 14:42:33 -0500775 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400776
777 size_t freeIndex = 0;
778 for (; freeIndex < mGarbage.size(); ++freeIndex)
779 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500780 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400781 break;
782 }
783
784 // Remove the entries from the garbage list - they should be ready to go.
785 if (freeIndex > 0)
786 {
787 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500788 }
789
Jamie Madill21061022018-07-12 23:56:30 -0400790 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500791}
792
Jamie Madill21061022018-07-12 23:56:30 -0400793angle::Result RendererVk::submitFrame(vk::Context *context,
794 const VkSubmitInfo &submitInfo,
795 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500796{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500797 VkFenceCreateInfo fenceInfo;
798 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
799 fenceInfo.pNext = nullptr;
800 fenceInfo.flags = 0;
801
Jamie Madillbea35a62018-07-05 11:54:10 -0400802 vk::Scoped<CommandBatch> scopedBatch(mDevice);
803 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400804 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500805
Jamie Madill21061022018-07-12 23:56:30 -0400806 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500807
808 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500809 batch.commandPool = std::move(mCommandPool);
810 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500811
Jamie Madillbea35a62018-07-05 11:54:10 -0400812 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400813
Omar El Sheikh0359b692018-07-23 17:22:00 -0600814 // Check that mInFlightCommands isn't growing too fast
815 // If it is, wait for the queue to complete work it has alread been assigned
816 if (mInFlightCommands.size() > kInFlightCommandsLimit)
817 {
818 vkQueueWaitIdle(mQueue);
819 }
Jamie Madill0c0dc342017-03-24 14:18:51 -0400820
821 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400822 // TODO(jmadill): Overflow check.
823 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400824
Jamie Madill21061022018-07-12 23:56:30 -0400825 ANGLE_TRY(checkInFlightCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400826
Jamie Madill49ac74b2017-12-21 14:42:33 -0500827 // Simply null out the command buffer here - it was allocated using the command pool.
828 commandBuffer.releaseHandle();
829
830 // Reallocate the command pool for next frame.
831 // TODO(jmadill): Consider reusing command pools.
832 VkCommandPoolCreateInfo poolInfo;
833 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
834 poolInfo.pNext = nullptr;
835 poolInfo.flags = 0;
836 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
837
Jamie Madill21061022018-07-12 23:56:30 -0400838 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500839
Jamie Madill21061022018-07-12 23:56:30 -0400840 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500841}
842
Jamie Madill4c26fc22017-02-24 11:04:10 -0500843Serial RendererVk::getCurrentQueueSerial() const
844{
845 return mCurrentQueueSerial;
846}
847
Jamie Madillaaca96e2018-06-12 10:19:48 -0400848bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500849{
850 return serial > mLastCompletedQueueSerial;
851}
852
Jamie Madill21061022018-07-12 23:56:30 -0400853angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
854 const vk::RenderPassDesc &desc,
855 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500856{
Jamie Madill21061022018-07-12 23:56:30 -0400857 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -0500858 renderPassOut);
859}
860
Jamie Madill21061022018-07-12 23:56:30 -0400861angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
862 const vk::RenderPassDesc &desc,
863 const vk::AttachmentOpsArray &ops,
864 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500865{
Jamie Madill21061022018-07-12 23:56:30 -0400866 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -0500867 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500868}
869
Jamie Madilla5e06072018-05-18 14:36:05 -0400870vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500871{
Jamie Madilla5e06072018-05-18 14:36:05 -0400872 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500873}
874
Jamie Madill21061022018-07-12 23:56:30 -0400875angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500876{
Jamie Madill21061022018-07-12 23:56:30 -0400877 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500878 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500879}
880
Jamie Madill21061022018-07-12 23:56:30 -0400881angle::Result RendererVk::flush(vk::Context *context,
882 const vk::Semaphore &waitSemaphore,
883 const vk::Semaphore &signalSemaphore)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500884{
Jamie Madillbea35a62018-07-05 11:54:10 -0400885 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
886 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500887
888 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
889
890 VkSubmitInfo submitInfo;
891 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
892 submitInfo.pNext = nullptr;
893 submitInfo.waitSemaphoreCount = 1;
894 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
895 submitInfo.pWaitDstStageMask = &waitStageMask;
896 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400897 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500898 submitInfo.signalSemaphoreCount = 1;
899 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
900
Jamie Madill21061022018-07-12 23:56:30 -0400901 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
902 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500903}
904
Jamie Madill78feddc2018-04-27 11:45:05 -0400905Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500906{
Jamie Madill78feddc2018-04-27 11:45:05 -0400907 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500908}
909
Jamie Madill06ca6342018-07-12 15:56:53 -0400910angle::Result RendererVk::getPipeline(vk::Context *context,
911 const vk::ShaderAndSerial &vertexShader,
912 const vk::ShaderAndSerial &fragmentShader,
913 const vk::PipelineLayout &pipelineLayout,
914 const vk::PipelineDesc &pipelineDesc,
915 const gl::AttributesMask &activeAttribLocationsMask,
916 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500917{
Jamie Madill06ca6342018-07-12 15:56:53 -0400918 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400919 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -0400920 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400921 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
922
923 // Pull in a compatible RenderPass.
924 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400925 ANGLE_TRY(
926 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -0400927
Jamie Madill21061022018-07-12 23:56:30 -0400928 return mPipelineCache.getPipeline(context, *compatibleRenderPass, pipelineLayout,
Jamie Madill9aef3672018-04-27 11:45:06 -0400929 activeAttribLocationsMask, vertexShader.get(),
930 fragmentShader.get(), pipelineDesc, pipelineOut);
931}
932
Jamie Madill21061022018-07-12 23:56:30 -0400933angle::Result RendererVk::getDescriptorSetLayout(
934 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400935 const vk::DescriptorSetLayoutDesc &desc,
936 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
937{
Jamie Madill21061022018-07-12 23:56:30 -0400938 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -0400939}
940
Jamie Madill21061022018-07-12 23:56:30 -0400941angle::Result RendererVk::getPipelineLayout(
942 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400943 const vk::PipelineLayoutDesc &desc,
944 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
945 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
946{
Jamie Madill21061022018-07-12 23:56:30 -0400947 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -0400948 pipelineLayoutOut);
949}
950
Jamie Madilld47044a2018-04-27 11:45:03 -0400951vk::ShaderLibrary *RendererVk::getShaderLibrary()
952{
953 return &mShaderLibrary;
954}
Luc Ferron90968362018-05-04 08:47:22 -0400955
Jamie Madillaaca96e2018-06-12 10:19:48 -0400956uint32_t GetUniformBufferDescriptorCount()
957{
958 return kUniformBufferDescriptorsPerDescriptorSet;
959}
960
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400961} // namespace rx