blob: 1475d31410759a1e0dad4a58b3c68aacef967462 [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";
36} // anonymous namespace
37
Jamie Madill9e54b5a2016-05-25 12:57:39 -040038namespace rx
39{
40
Jamie Madille09bd5d2016-11-29 16:20:35 -050041namespace
42{
Luc Ferrondaedf4d2018-03-16 09:28:53 -040043// We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and
44// one for the vertex shader.
45constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2;
Jamie Madille09bd5d2016-11-29 16:20:35 -050046
Omar El Sheikh26c61b22018-06-29 12:50:59 -060047bool ShouldEnableMockICD(const egl::AttributeMap &attribs)
48{
49#if !defined(ANGLE_PLATFORM_ANDROID)
50 // Mock ICD does not currently run on Android
51 return (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
52 EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) ==
53 EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE);
54#else
55 return false;
56#endif // !defined(ANGLE_PLATFORM_ANDROID)
57}
58
Jamie Madille09bd5d2016-11-29 16:20:35 -050059VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
60 const std::vector<const char *> &enabledExtensionNames)
61{
62 // Compile the extensions names into a set.
63 std::set<std::string> extensionNames;
64 for (const auto &extensionProp : extensionProps)
65 {
66 extensionNames.insert(extensionProp.extensionName);
67 }
68
Jamie Madillacf2f3a2017-11-21 19:22:44 -050069 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050070 {
71 if (extensionNames.count(extensionName) == 0)
72 {
73 return VK_ERROR_EXTENSION_NOT_PRESENT;
74 }
75 }
76
77 return VK_SUCCESS;
78}
79
Yuly Novikov199f4292018-01-19 19:04:05 -050080VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
81 VkDebugReportObjectTypeEXT objectType,
82 uint64_t object,
83 size_t location,
84 int32_t messageCode,
85 const char *layerPrefix,
86 const char *message,
87 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -050088{
89 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
90 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050091 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -050092#if !defined(NDEBUG)
93 // Abort the call in Debug builds.
94 return VK_TRUE;
95#endif
96 }
97 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
98 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -050099 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500100 }
101 else
102 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500103 // Uncomment this if you want Vulkan spam.
104 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500105 }
106
107 return VK_FALSE;
108}
109
Yuly Novikov199f4292018-01-19 19:04:05 -0500110// If we're loading the validation layers, we could be running from any random directory.
111// Change to the executable directory so we can find the layers, then change back to the
112// previous directory to be safe we don't disrupt the application.
113class ScopedVkLoaderEnvironment : angle::NonCopyable
114{
115 public:
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600116 ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD)
117 : mEnableValidationLayers(enableValidationLayers),
118 mEnableMockICD(enableMockICD),
119 mChangedCWD(false),
120 mChangedICDPath(false)
Yuly Novikov199f4292018-01-19 19:04:05 -0500121 {
122// Changing CWD and setting environment variables makes no sense on Android,
123// since this code is a part of Java application there.
124// Android Vulkan loader doesn't need this either.
125#if !defined(ANGLE_PLATFORM_ANDROID)
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600126 if (enableMockICD)
127 {
128 // Override environment variable to use built Mock ICD
129 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
130 mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv);
131 mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON);
132 if (!mChangedICDPath)
133 {
134 ERR() << "Error setting Path for Mock/Null Driver.";
135 mEnableMockICD = false;
136 }
137 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500138 if (mEnableValidationLayers)
139 {
140 const auto &cwd = angle::GetCWD();
141 if (!cwd.valid())
142 {
143 ERR() << "Error getting CWD for Vulkan layers init.";
144 mEnableValidationLayers = false;
145 }
146 else
147 {
148 mPreviousCWD = cwd.value();
149 const char *exeDir = angle::GetExecutableDirectory();
150 mChangedCWD = angle::SetCWD(exeDir);
151 if (!mChangedCWD)
152 {
153 ERR() << "Error setting CWD for Vulkan layers init.";
154 mEnableValidationLayers = false;
155 }
156 }
157 }
158
159 // Override environment variable to use the ANGLE layers.
160 if (mEnableValidationLayers)
161 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700162 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500163 {
164 ERR() << "Error setting environment for Vulkan layers init.";
165 mEnableValidationLayers = false;
166 }
167 }
168#endif // !defined(ANGLE_PLATFORM_ANDROID)
169 }
170
171 ~ScopedVkLoaderEnvironment()
172 {
173 if (mChangedCWD)
174 {
175#if !defined(ANGLE_PLATFORM_ANDROID)
176 ASSERT(mPreviousCWD.valid());
177 angle::SetCWD(mPreviousCWD.value().c_str());
178#endif // !defined(ANGLE_PLATFORM_ANDROID)
179 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600180 if (mChangedICDPath)
181 {
182 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
183 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500184 }
185
Jamie Madillaaca96e2018-06-12 10:19:48 -0400186 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500187
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600188 bool canEnableMockICD() const { return mEnableMockICD; }
189
Yuly Novikov199f4292018-01-19 19:04:05 -0500190 private:
191 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600192 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500193 bool mChangedCWD;
194 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600195 bool mChangedICDPath;
196 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500197};
198
Jamie Madill21061022018-07-12 23:56:30 -0400199void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
200 bool preferMockICD,
201 VkPhysicalDevice *physicalDeviceOut,
202 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
203{
204 ASSERT(!physicalDevices.empty());
205 if (preferMockICD)
206 {
207 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
208 {
209 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
210 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
211 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
212 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
213 {
214 *physicalDeviceOut = physicalDevice;
215 return;
216 }
217 }
218 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
219 "physicalDevice instead.";
220 }
221
222 // Fall back to first device.
223 *physicalDeviceOut = physicalDevices[0];
224 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
225}
Jamie Madille09bd5d2016-11-29 16:20:35 -0500226} // anonymous namespace
227
Jamie Madill49ac74b2017-12-21 14:42:33 -0500228// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400229RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500230
Jamie Madillaaca96e2018-06-12 10:19:48 -0400231RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500232
233RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
234 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
235{
236}
237
238RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
239{
240 std::swap(commandPool, other.commandPool);
241 std::swap(fence, other.fence);
242 std::swap(serial, other.serial);
243 return *this;
244}
245
Jamie Madillbea35a62018-07-05 11:54:10 -0400246void RendererVk::CommandBatch::destroy(VkDevice device)
247{
248 commandPool.destroy(device);
249 fence.destroy(device);
250}
251
Jamie Madill9f2a8612017-11-30 12:43:09 -0500252// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500253RendererVk::RendererVk()
254 : mCapsInitialized(false),
255 mInstance(VK_NULL_HANDLE),
256 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500257 mDebugReportCallback(VK_NULL_HANDLE),
258 mPhysicalDevice(VK_NULL_HANDLE),
259 mQueue(VK_NULL_HANDLE),
260 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
261 mDevice(VK_NULL_HANDLE),
Jamie Madill4c26fc22017-02-24 11:04:10 -0500262 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400263 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Jamie Madillaaca96e2018-06-12 10:19:48 -0400264 mCurrentQueueSerial(mQueueSerialFactory.generate())
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400265{
266}
267
268RendererVk::~RendererVk()
269{
Jamie Madill21061022018-07-12 23:56:30 -0400270}
271
272void RendererVk::onDestroy(vk::Context *context)
273{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500274 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500275 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500276 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400277 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500278 }
279
Jamie Madillc7918ce2018-06-13 13:25:31 -0400280 mPipelineLayoutCache.destroy(mDevice);
281 mDescriptorSetLayoutCache.destroy(mDevice);
282
Jamie Madill9f2a8612017-11-30 12:43:09 -0500283 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500284 mPipelineCache.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400285 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500286
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500287 if (mGlslangWrapper)
288 {
289 GlslangWrapper::ReleaseReference();
290 mGlslangWrapper = nullptr;
291 }
292
Jamie Madill5deea722017-02-16 10:44:46 -0500293 if (mCommandPool.valid())
294 {
295 mCommandPool.destroy(mDevice);
296 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500297
298 if (mDevice)
299 {
300 vkDestroyDevice(mDevice, nullptr);
301 mDevice = VK_NULL_HANDLE;
302 }
303
Jamie Madill0448ec82016-12-23 13:41:47 -0500304 if (mDebugReportCallback)
305 {
306 ASSERT(mInstance);
307 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
308 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
309 ASSERT(destroyDebugReportCallback);
310 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
311 }
312
Jamie Madill4d0bf552016-12-28 15:45:24 -0500313 if (mInstance)
314 {
315 vkDestroyInstance(mInstance, nullptr);
316 mInstance = VK_NULL_HANDLE;
317 }
318
319 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500320}
321
Jamie Madill21061022018-07-12 23:56:30 -0400322angle::Result RendererVk::initialize(vk::Context *context,
323 const egl::AttributeMap &attribs,
324 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500325{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600326 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
327 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500328 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600329 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500330
Jamie Madill0448ec82016-12-23 13:41:47 -0500331 // Gather global layer properties.
332 uint32_t instanceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400333 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500334
335 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
336 if (instanceLayerCount > 0)
337 {
Jamie Madill21061022018-07-12 23:56:30 -0400338 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
339 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500340 }
341
Jamie Madille09bd5d2016-11-29 16:20:35 -0500342 uint32_t instanceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400343 ANGLE_VK_TRY(context,
344 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500345
346 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
347 if (instanceExtensionCount > 0)
348 {
Jamie Madill21061022018-07-12 23:56:30 -0400349 ANGLE_VK_TRY(context, vkEnumerateInstanceExtensionProperties(
350 nullptr, &instanceExtensionCount, instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500351 }
352
Yuly Novikov199f4292018-01-19 19:04:05 -0500353 const char *const *enabledLayerNames = nullptr;
354 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500355 if (mEnableValidationLayers)
356 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500357 bool layersRequested =
358 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
359 mEnableValidationLayers = GetAvailableValidationLayers(
360 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500361 }
362
Jamie Madille09bd5d2016-11-29 16:20:35 -0500363 std::vector<const char *> enabledInstanceExtensions;
364 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500365 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500366
Jamie Madill0448ec82016-12-23 13:41:47 -0500367 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
368 if (mEnableValidationLayers)
369 {
370 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
371 }
372
Jamie Madille09bd5d2016-11-29 16:20:35 -0500373 // Verify the required extensions are in the extension names set. Fail if not.
Jamie Madill21061022018-07-12 23:56:30 -0400374 ANGLE_VK_TRY(context,
375 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500376
Jamie Madill327ba852016-11-30 12:38:28 -0500377 VkApplicationInfo applicationInfo;
378 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
379 applicationInfo.pNext = nullptr;
380 applicationInfo.pApplicationName = "ANGLE";
381 applicationInfo.applicationVersion = 1;
382 applicationInfo.pEngineName = "ANGLE";
383 applicationInfo.engineVersion = 1;
384 applicationInfo.apiVersion = VK_API_VERSION_1_0;
385
386 VkInstanceCreateInfo instanceInfo;
387 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
388 instanceInfo.pNext = nullptr;
389 instanceInfo.flags = 0;
390 instanceInfo.pApplicationInfo = &applicationInfo;
391
Jamie Madille09bd5d2016-11-29 16:20:35 -0500392 // Enable requested layers and extensions.
393 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
394 instanceInfo.ppEnabledExtensionNames =
395 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500396 instanceInfo.enabledLayerCount = enabledLayerCount;
397 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500398
Jamie Madill21061022018-07-12 23:56:30 -0400399 ANGLE_VK_TRY(context, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500400
Jamie Madill0448ec82016-12-23 13:41:47 -0500401 if (mEnableValidationLayers)
402 {
403 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
404
405 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
406 debugReportInfo.pNext = nullptr;
407 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
408 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
409 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
410 debugReportInfo.pfnCallback = &DebugReportCallback;
411 debugReportInfo.pUserData = this;
412
413 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
414 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
415 ASSERT(createDebugReportCallback);
Jamie Madill21061022018-07-12 23:56:30 -0400416 ANGLE_VK_TRY(context, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
417 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500418 }
419
Jamie Madill4d0bf552016-12-28 15:45:24 -0500420 uint32_t physicalDeviceCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400421 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
422 ANGLE_VK_CHECK(context, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500423
424 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700425 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Jamie Madill21061022018-07-12 23:56:30 -0400426 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
427 physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600428 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700429 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500430
431 // Ensure we can find a graphics queue family.
432 uint32_t queueCount = 0;
433 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
434
Jamie Madill21061022018-07-12 23:56:30 -0400435 ANGLE_VK_CHECK(context, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500436
437 mQueueFamilyProperties.resize(queueCount);
438 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
439 mQueueFamilyProperties.data());
440
441 size_t graphicsQueueFamilyCount = false;
442 uint32_t firstGraphicsQueueFamily = 0;
443 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
444 {
445 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
446 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
447 {
448 ASSERT(queueInfo.queueCount > 0);
449 graphicsQueueFamilyCount++;
450 if (firstGraphicsQueueFamily == 0)
451 {
452 firstGraphicsQueueFamily = familyIndex;
453 }
454 break;
455 }
456 }
457
Jamie Madill21061022018-07-12 23:56:30 -0400458 ANGLE_VK_CHECK(context, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500459
Jamie Madill12222072018-07-11 14:59:48 -0400460 initFeatures();
461
Jamie Madill4d0bf552016-12-28 15:45:24 -0500462 // If only one queue family, go ahead and initialize the device. If there is more than one
463 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
464 if (graphicsQueueFamilyCount == 1)
465 {
Jamie Madill21061022018-07-12 23:56:30 -0400466 ANGLE_TRY(initializeDevice(context, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500467 }
468
Jamie Madill035fd6b2017-10-03 15:43:22 -0400469 // Store the physical device memory properties so we can find the right memory pools.
470 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500471
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500472 mGlslangWrapper = GlslangWrapper::GetReference();
473
Jamie Madill6a89d222017-11-02 11:59:51 -0400474 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500475 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
476 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400477
Jamie Madill21061022018-07-12 23:56:30 -0400478 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400479}
480
Jamie Madill21061022018-07-12 23:56:30 -0400481angle::Result RendererVk::initializeDevice(vk::Context *context, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500482{
483 uint32_t deviceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400484 ANGLE_VK_TRY(context,
485 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500486
487 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
488 if (deviceLayerCount > 0)
489 {
Jamie Madill21061022018-07-12 23:56:30 -0400490 ANGLE_VK_TRY(context, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
491 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500492 }
493
494 uint32_t deviceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400495 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
496 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500497
498 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
499 if (deviceExtensionCount > 0)
500 {
Jamie Madill21061022018-07-12 23:56:30 -0400501 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
502 &deviceExtensionCount,
503 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500504 }
505
Yuly Novikov199f4292018-01-19 19:04:05 -0500506 const char *const *enabledLayerNames = nullptr;
507 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500508 if (mEnableValidationLayers)
509 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500510 mEnableValidationLayers = GetAvailableValidationLayers(
511 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500512 }
513
514 std::vector<const char *> enabledDeviceExtensions;
515 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
516
Luc Ferronbf6dc372018-06-28 15:24:19 -0400517 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
518 if (getFeatures().flipViewportY)
519 {
520 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
521 }
522
Jamie Madill21061022018-07-12 23:56:30 -0400523 ANGLE_VK_TRY(context, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500524
525 VkDeviceQueueCreateInfo queueCreateInfo;
526
527 float zeroPriority = 0.0f;
528
529 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
530 queueCreateInfo.pNext = nullptr;
531 queueCreateInfo.flags = 0;
532 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
533 queueCreateInfo.queueCount = 1;
534 queueCreateInfo.pQueuePriorities = &zeroPriority;
535
536 // Initialize the device
537 VkDeviceCreateInfo createInfo;
538
Jamie Madill50cf2be2018-06-15 09:46:57 -0400539 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
540 createInfo.pNext = nullptr;
541 createInfo.flags = 0;
542 createInfo.queueCreateInfoCount = 1;
543 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500544 createInfo.enabledLayerCount = enabledLayerCount;
545 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500546 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
547 createInfo.ppEnabledExtensionNames =
548 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
549 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
550
Jamie Madill21061022018-07-12 23:56:30 -0400551 ANGLE_VK_TRY(context, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500552
553 mCurrentQueueFamilyIndex = queueFamilyIndex;
554
555 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
556
557 // Initialize the command pool now that we know the queue family index.
558 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500559 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
560 commandPoolInfo.pNext = nullptr;
561 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500562 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
563
Jamie Madill21061022018-07-12 23:56:30 -0400564 ANGLE_TRY(mCommandPool.init(context, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500565
Jamie Madill21061022018-07-12 23:56:30 -0400566 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500567}
568
Jamie Madill21061022018-07-12 23:56:30 -0400569angle::Result RendererVk::selectPresentQueueForSurface(vk::Context *context,
570 VkSurfaceKHR surface,
571 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500572{
573 // We've already initialized a device, and can't re-create it unless it's never been used.
574 // TODO(jmadill): Handle the re-creation case if necessary.
575 if (mDevice != VK_NULL_HANDLE)
576 {
577 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
578
579 // Check if the current device supports present on this surface.
580 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400581 ANGLE_VK_TRY(context,
582 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500583 surface, &supportsPresent));
584
Jamie Madill6cad7732018-07-11 09:01:17 -0400585 if (supportsPresent == VK_TRUE)
586 {
587 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400588 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400589 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500590 }
591
592 // Find a graphics and present queue.
593 Optional<uint32_t> newPresentQueue;
594 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
595 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
596 {
597 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
598 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
599 {
600 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400601 ANGLE_VK_TRY(context, vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex,
602 surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500603
604 if (supportsPresent == VK_TRUE)
605 {
606 newPresentQueue = queueIndex;
607 break;
608 }
609 }
610 }
611
Jamie Madill21061022018-07-12 23:56:30 -0400612 ANGLE_VK_CHECK(context, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
613 ANGLE_TRY(initializeDevice(context, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500614
Jamie Madill6cad7732018-07-11 09:01:17 -0400615 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400616 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500617}
618
619std::string RendererVk::getVendorString() const
620{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300621 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500622}
623
Jamie Madille09bd5d2016-11-29 16:20:35 -0500624std::string RendererVk::getRendererDescription() const
625{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500626 std::stringstream strstr;
627
628 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
629
630 strstr << "Vulkan ";
631 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
632 strstr << VK_VERSION_MINOR(apiVersion) << ".";
633 strstr << VK_VERSION_PATCH(apiVersion);
634
Olli Etuahoc6a06182018-04-13 14:11:46 +0300635 strstr << "(";
636
637 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
638 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
639 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
640 // driver detection.
641 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
642 {
643 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
644 }
645
646 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500647
648 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500649}
650
Jamie Madill12222072018-07-11 14:59:48 -0400651void RendererVk::initFeatures()
652{
653 // Use OpenGL line rasterization rules by default.
654 mFeatures.basicGLLineRasterization = true;
655
656 // For now, set this manually to true to enable viewport flipping. A couple of features are not
657 // working well like copyTexImage, copySubTexImage, blit, and probably some more. Until
658 // everything is fixed, we will keep the viewport flipping feature disabled.
659 mFeatures.flipViewportY = false;
660}
661
Jamie Madillacccc6c2016-05-03 17:22:10 -0400662void RendererVk::ensureCapsInitialized() const
663{
664 if (!mCapsInitialized)
665 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500666 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Jamie Madill12222072018-07-11 14:59:48 -0400667 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400668 mCapsInitialized = true;
669 }
670}
671
Jamie Madillacccc6c2016-05-03 17:22:10 -0400672const gl::Caps &RendererVk::getNativeCaps() const
673{
674 ensureCapsInitialized();
675 return mNativeCaps;
676}
677
678const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
679{
680 ensureCapsInitialized();
681 return mNativeTextureCaps;
682}
683
684const gl::Extensions &RendererVk::getNativeExtensions() const
685{
686 ensureCapsInitialized();
687 return mNativeExtensions;
688}
689
690const gl::Limitations &RendererVk::getNativeLimitations() const
691{
692 ensureCapsInitialized();
693 return mNativeLimitations;
694}
695
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400696uint32_t RendererVk::getMaxActiveTextures()
697{
698 // TODO(lucferron): expose this limitation to GL in Context Caps
699 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
700 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
701}
702
Jamie Madill49ac74b2017-12-21 14:42:33 -0500703const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500704{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500705 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500706}
707
Jamie Madill21061022018-07-12 23:56:30 -0400708angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500709{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500710 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500711 {
Luc Ferron1617e692018-07-11 11:08:19 -0400712 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
713 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400714
Jamie Madill49ac74b2017-12-21 14:42:33 -0500715 VkSubmitInfo submitInfo;
716 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
717 submitInfo.pNext = nullptr;
718 submitInfo.waitSemaphoreCount = 0;
719 submitInfo.pWaitSemaphores = nullptr;
720 submitInfo.pWaitDstStageMask = nullptr;
721 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400722 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500723 submitInfo.signalSemaphoreCount = 0;
724 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500725
Jamie Madill21061022018-07-12 23:56:30 -0400726 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500727 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500728
Jamie Madill4c26fc22017-02-24 11:04:10 -0500729 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400730 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400731 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400732 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500733}
734
Jamie Madill0c0dc342017-03-24 14:18:51 -0400735void RendererVk::freeAllInFlightResources()
736{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500737 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400738 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500739 batch.fence.destroy(mDevice);
740 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400741 }
742 mInFlightCommands.clear();
743
744 for (auto &garbage : mGarbage)
745 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400746 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400747 }
748 mGarbage.clear();
749}
750
Jamie Madill21061022018-07-12 23:56:30 -0400751angle::Result RendererVk::checkInFlightCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500752{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500753 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500754
Jamie Madill49ac74b2017-12-21 14:42:33 -0500755 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500756 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500757 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400758 if (result == VK_NOT_READY)
759 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500760
Jamie Madill21061022018-07-12 23:56:30 -0400761 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500762 ASSERT(batch.serial > mLastCompletedQueueSerial);
763 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400764
Jamie Madill49ac74b2017-12-21 14:42:33 -0500765 batch.fence.destroy(mDevice);
766 batch.commandPool.destroy(mDevice);
767 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500768 }
769
Jamie Madill49ac74b2017-12-21 14:42:33 -0500770 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400771
772 size_t freeIndex = 0;
773 for (; freeIndex < mGarbage.size(); ++freeIndex)
774 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500775 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400776 break;
777 }
778
779 // Remove the entries from the garbage list - they should be ready to go.
780 if (freeIndex > 0)
781 {
782 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500783 }
784
Jamie Madill21061022018-07-12 23:56:30 -0400785 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500786}
787
Jamie Madill21061022018-07-12 23:56:30 -0400788angle::Result RendererVk::submitFrame(vk::Context *context,
789 const VkSubmitInfo &submitInfo,
790 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500791{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500792 VkFenceCreateInfo fenceInfo;
793 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
794 fenceInfo.pNext = nullptr;
795 fenceInfo.flags = 0;
796
Jamie Madillbea35a62018-07-05 11:54:10 -0400797 vk::Scoped<CommandBatch> scopedBatch(mDevice);
798 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400799 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500800
Jamie Madill21061022018-07-12 23:56:30 -0400801 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500802
803 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500804 batch.commandPool = std::move(mCommandPool);
805 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500806
Jamie Madillbea35a62018-07-05 11:54:10 -0400807 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400808
809 // Sanity check.
810 ASSERT(mInFlightCommands.size() < 1000u);
811
812 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400813 // TODO(jmadill): Overflow check.
814 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400815
Jamie Madill21061022018-07-12 23:56:30 -0400816 ANGLE_TRY(checkInFlightCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400817
Jamie Madill49ac74b2017-12-21 14:42:33 -0500818 // Simply null out the command buffer here - it was allocated using the command pool.
819 commandBuffer.releaseHandle();
820
821 // Reallocate the command pool for next frame.
822 // TODO(jmadill): Consider reusing command pools.
823 VkCommandPoolCreateInfo poolInfo;
824 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
825 poolInfo.pNext = nullptr;
826 poolInfo.flags = 0;
827 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
828
Jamie Madill21061022018-07-12 23:56:30 -0400829 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500830
Jamie Madill21061022018-07-12 23:56:30 -0400831 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500832}
833
Jamie Madillaaca96e2018-06-12 10:19:48 -0400834GlslangWrapper *RendererVk::getGlslangWrapper() const
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500835{
836 return mGlslangWrapper;
837}
838
Jamie Madill4c26fc22017-02-24 11:04:10 -0500839Serial RendererVk::getCurrentQueueSerial() const
840{
841 return mCurrentQueueSerial;
842}
843
Jamie Madillaaca96e2018-06-12 10:19:48 -0400844bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500845{
846 return serial > mLastCompletedQueueSerial;
847}
848
Jamie Madill21061022018-07-12 23:56:30 -0400849angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
850 const vk::RenderPassDesc &desc,
851 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500852{
Jamie Madill21061022018-07-12 23:56:30 -0400853 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -0500854 renderPassOut);
855}
856
Jamie Madill21061022018-07-12 23:56:30 -0400857angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
858 const vk::RenderPassDesc &desc,
859 const vk::AttachmentOpsArray &ops,
860 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500861{
Jamie Madill21061022018-07-12 23:56:30 -0400862 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -0500863 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500864}
865
Jamie Madilla5e06072018-05-18 14:36:05 -0400866vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500867{
Jamie Madilla5e06072018-05-18 14:36:05 -0400868 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500869}
870
Jamie Madill21061022018-07-12 23:56:30 -0400871angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500872{
Jamie Madill21061022018-07-12 23:56:30 -0400873 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500874 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500875}
876
Jamie Madill21061022018-07-12 23:56:30 -0400877angle::Result RendererVk::flush(vk::Context *context,
878 const vk::Semaphore &waitSemaphore,
879 const vk::Semaphore &signalSemaphore)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500880{
Jamie Madillbea35a62018-07-05 11:54:10 -0400881 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
882 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500883
884 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
885
886 VkSubmitInfo submitInfo;
887 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
888 submitInfo.pNext = nullptr;
889 submitInfo.waitSemaphoreCount = 1;
890 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
891 submitInfo.pWaitDstStageMask = &waitStageMask;
892 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400893 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500894 submitInfo.signalSemaphoreCount = 1;
895 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
896
Jamie Madill21061022018-07-12 23:56:30 -0400897 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
898 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500899}
900
Jamie Madill78feddc2018-04-27 11:45:05 -0400901Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500902{
Jamie Madill78feddc2018-04-27 11:45:05 -0400903 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500904}
905
Jamie Madill21061022018-07-12 23:56:30 -0400906angle::Result RendererVk::getAppPipeline(vk::Context *context,
907 const ProgramVk *programVk,
908 const vk::PipelineDesc &desc,
909 const gl::AttributesMask &activeAttribLocationsMask,
910 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500911{
Jamie Madill33318de2018-05-01 11:22:54 -0400912 ASSERT(programVk->getVertexModuleSerial() ==
913 desc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
914 ASSERT(programVk->getFragmentModuleSerial() ==
915 desc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500916
917 // Pull in a compatible RenderPass.
918 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400919 ANGLE_TRY(getCompatibleRenderPass(context, desc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500920
Jamie Madill9b168d02018-06-13 13:25:32 -0400921 const vk::PipelineLayout &pipelineLayout = programVk->getPipelineLayout();
922
Jamie Madill21061022018-07-12 23:56:30 -0400923 return mPipelineCache.getPipeline(context, *compatibleRenderPass, pipelineLayout,
Luc Ferronceb71902018-02-05 15:18:47 -0500924 activeAttribLocationsMask, programVk->getLinkedVertexModule(),
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500925 programVk->getLinkedFragmentModule(), desc, pipelineOut);
926}
927
Jamie Madill21061022018-07-12 23:56:30 -0400928angle::Result RendererVk::getInternalPipeline(vk::Context *context,
929 const vk::ShaderAndSerial &vertexShader,
930 const vk::ShaderAndSerial &fragmentShader,
931 const vk::PipelineLayout &pipelineLayout,
932 const vk::PipelineDesc &pipelineDesc,
933 const gl::AttributesMask &activeAttribLocationsMask,
934 vk::PipelineAndSerial **pipelineOut)
Jamie Madill9aef3672018-04-27 11:45:06 -0400935{
936 ASSERT(vertexShader.queueSerial() ==
937 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
938 ASSERT(fragmentShader.queueSerial() ==
939 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
940
941 // Pull in a compatible RenderPass.
942 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400943 ANGLE_TRY(
944 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -0400945
Jamie Madill21061022018-07-12 23:56:30 -0400946 return mPipelineCache.getPipeline(context, *compatibleRenderPass, pipelineLayout,
Jamie Madill9aef3672018-04-27 11:45:06 -0400947 activeAttribLocationsMask, vertexShader.get(),
948 fragmentShader.get(), pipelineDesc, pipelineOut);
949}
950
Jamie Madill21061022018-07-12 23:56:30 -0400951angle::Result RendererVk::getDescriptorSetLayout(
952 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400953 const vk::DescriptorSetLayoutDesc &desc,
954 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
955{
Jamie Madill21061022018-07-12 23:56:30 -0400956 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -0400957}
958
Jamie Madill21061022018-07-12 23:56:30 -0400959angle::Result RendererVk::getPipelineLayout(
960 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400961 const vk::PipelineLayoutDesc &desc,
962 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
963 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
964{
Jamie Madill21061022018-07-12 23:56:30 -0400965 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -0400966 pipelineLayoutOut);
967}
968
Jamie Madilld47044a2018-04-27 11:45:03 -0400969vk::ShaderLibrary *RendererVk::getShaderLibrary()
970{
971 return &mShaderLibrary;
972}
Luc Ferron90968362018-05-04 08:47:22 -0400973
Jamie Madillaaca96e2018-06-12 10:19:48 -0400974uint32_t GetUniformBufferDescriptorCount()
975{
976 return kUniformBufferDescriptorsPerDescriptorSet;
977}
978
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400979} // namespace rx