blob: b3bda1694b24394581c74ccfc001378041c472ce [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 {
Omar El Sheikh80d4ef12018-07-13 17:08:19 -0600182 if (mPreviousICDPath.value().empty())
183 {
184 angle::UnsetEnvironmentVar(g_VkICDPathEnv);
185 }
186 else
187 {
188 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
189 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600190 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500191 }
192
Jamie Madillaaca96e2018-06-12 10:19:48 -0400193 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500194
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600195 bool canEnableMockICD() const { return mEnableMockICD; }
196
Yuly Novikov199f4292018-01-19 19:04:05 -0500197 private:
198 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600199 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500200 bool mChangedCWD;
201 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600202 bool mChangedICDPath;
203 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500204};
205
Jamie Madill21061022018-07-12 23:56:30 -0400206void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
207 bool preferMockICD,
208 VkPhysicalDevice *physicalDeviceOut,
209 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
210{
211 ASSERT(!physicalDevices.empty());
212 if (preferMockICD)
213 {
214 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
215 {
216 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
217 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
218 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
219 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
220 {
221 *physicalDeviceOut = physicalDevice;
222 return;
223 }
224 }
225 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
226 "physicalDevice instead.";
227 }
228
229 // Fall back to first device.
230 *physicalDeviceOut = physicalDevices[0];
231 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
232}
Jamie Madille09bd5d2016-11-29 16:20:35 -0500233} // anonymous namespace
234
Jamie Madill49ac74b2017-12-21 14:42:33 -0500235// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400236RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500237
Jamie Madillaaca96e2018-06-12 10:19:48 -0400238RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500239
240RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
241 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
242{
243}
244
245RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
246{
247 std::swap(commandPool, other.commandPool);
248 std::swap(fence, other.fence);
249 std::swap(serial, other.serial);
250 return *this;
251}
252
Jamie Madillbea35a62018-07-05 11:54:10 -0400253void RendererVk::CommandBatch::destroy(VkDevice device)
254{
255 commandPool.destroy(device);
256 fence.destroy(device);
257}
258
Jamie Madill9f2a8612017-11-30 12:43:09 -0500259// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500260RendererVk::RendererVk()
261 : mCapsInitialized(false),
262 mInstance(VK_NULL_HANDLE),
263 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500264 mDebugReportCallback(VK_NULL_HANDLE),
265 mPhysicalDevice(VK_NULL_HANDLE),
266 mQueue(VK_NULL_HANDLE),
267 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
268 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400269 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Jamie Madillaaca96e2018-06-12 10:19:48 -0400270 mCurrentQueueSerial(mQueueSerialFactory.generate())
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400271{
272}
273
274RendererVk::~RendererVk()
275{
Jamie Madill21061022018-07-12 23:56:30 -0400276}
277
278void RendererVk::onDestroy(vk::Context *context)
279{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500280 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500281 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500282 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400283 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500284 }
285
Jamie Madillc7918ce2018-06-13 13:25:31 -0400286 mPipelineLayoutCache.destroy(mDevice);
287 mDescriptorSetLayoutCache.destroy(mDevice);
288
Jamie Madill9f2a8612017-11-30 12:43:09 -0500289 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500290 mPipelineCache.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400291 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500292
Jamie Madill06ca6342018-07-12 15:56:53 -0400293 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500294
Jamie Madill5deea722017-02-16 10:44:46 -0500295 if (mCommandPool.valid())
296 {
297 mCommandPool.destroy(mDevice);
298 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500299
300 if (mDevice)
301 {
302 vkDestroyDevice(mDevice, nullptr);
303 mDevice = VK_NULL_HANDLE;
304 }
305
Jamie Madill0448ec82016-12-23 13:41:47 -0500306 if (mDebugReportCallback)
307 {
308 ASSERT(mInstance);
309 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
310 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
311 ASSERT(destroyDebugReportCallback);
312 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
313 }
314
Jamie Madill4d0bf552016-12-28 15:45:24 -0500315 if (mInstance)
316 {
317 vkDestroyInstance(mInstance, nullptr);
318 mInstance = VK_NULL_HANDLE;
319 }
320
321 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500322}
323
Jamie Madill21061022018-07-12 23:56:30 -0400324angle::Result RendererVk::initialize(vk::Context *context,
325 const egl::AttributeMap &attribs,
326 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500327{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600328 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
329 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500330 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600331 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500332
Jamie Madill0448ec82016-12-23 13:41:47 -0500333 // Gather global layer properties.
334 uint32_t instanceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400335 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500336
337 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
338 if (instanceLayerCount > 0)
339 {
Jamie Madill21061022018-07-12 23:56:30 -0400340 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
341 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500342 }
343
Jamie Madille09bd5d2016-11-29 16:20:35 -0500344 uint32_t instanceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400345 ANGLE_VK_TRY(context,
346 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500347
348 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
349 if (instanceExtensionCount > 0)
350 {
Jamie Madill21061022018-07-12 23:56:30 -0400351 ANGLE_VK_TRY(context, vkEnumerateInstanceExtensionProperties(
352 nullptr, &instanceExtensionCount, instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500353 }
354
Yuly Novikov199f4292018-01-19 19:04:05 -0500355 const char *const *enabledLayerNames = nullptr;
356 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500357 if (mEnableValidationLayers)
358 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500359 bool layersRequested =
360 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
361 mEnableValidationLayers = GetAvailableValidationLayers(
362 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500363 }
364
Jamie Madille09bd5d2016-11-29 16:20:35 -0500365 std::vector<const char *> enabledInstanceExtensions;
366 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500367 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500368
Jamie Madill0448ec82016-12-23 13:41:47 -0500369 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
370 if (mEnableValidationLayers)
371 {
372 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
373 }
374
Jamie Madille09bd5d2016-11-29 16:20:35 -0500375 // Verify the required extensions are in the extension names set. Fail if not.
Jamie Madill21061022018-07-12 23:56:30 -0400376 ANGLE_VK_TRY(context,
377 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500378
Jamie Madill327ba852016-11-30 12:38:28 -0500379 VkApplicationInfo applicationInfo;
380 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
381 applicationInfo.pNext = nullptr;
382 applicationInfo.pApplicationName = "ANGLE";
383 applicationInfo.applicationVersion = 1;
384 applicationInfo.pEngineName = "ANGLE";
385 applicationInfo.engineVersion = 1;
386 applicationInfo.apiVersion = VK_API_VERSION_1_0;
387
388 VkInstanceCreateInfo instanceInfo;
389 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
390 instanceInfo.pNext = nullptr;
391 instanceInfo.flags = 0;
392 instanceInfo.pApplicationInfo = &applicationInfo;
393
Jamie Madille09bd5d2016-11-29 16:20:35 -0500394 // Enable requested layers and extensions.
395 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
396 instanceInfo.ppEnabledExtensionNames =
397 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500398 instanceInfo.enabledLayerCount = enabledLayerCount;
399 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500400
Jamie Madill21061022018-07-12 23:56:30 -0400401 ANGLE_VK_TRY(context, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500402
Jamie Madill0448ec82016-12-23 13:41:47 -0500403 if (mEnableValidationLayers)
404 {
405 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
406
407 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
408 debugReportInfo.pNext = nullptr;
409 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
410 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
411 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
412 debugReportInfo.pfnCallback = &DebugReportCallback;
413 debugReportInfo.pUserData = this;
414
415 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
416 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
417 ASSERT(createDebugReportCallback);
Jamie Madill21061022018-07-12 23:56:30 -0400418 ANGLE_VK_TRY(context, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
419 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500420 }
421
Jamie Madill4d0bf552016-12-28 15:45:24 -0500422 uint32_t physicalDeviceCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400423 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
424 ANGLE_VK_CHECK(context, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500425
426 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700427 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Jamie Madill21061022018-07-12 23:56:30 -0400428 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
429 physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600430 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700431 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500432
433 // Ensure we can find a graphics queue family.
434 uint32_t queueCount = 0;
435 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
436
Jamie Madill21061022018-07-12 23:56:30 -0400437 ANGLE_VK_CHECK(context, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500438
439 mQueueFamilyProperties.resize(queueCount);
440 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
441 mQueueFamilyProperties.data());
442
443 size_t graphicsQueueFamilyCount = false;
444 uint32_t firstGraphicsQueueFamily = 0;
445 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
446 {
447 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
448 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
449 {
450 ASSERT(queueInfo.queueCount > 0);
451 graphicsQueueFamilyCount++;
452 if (firstGraphicsQueueFamily == 0)
453 {
454 firstGraphicsQueueFamily = familyIndex;
455 }
456 break;
457 }
458 }
459
Jamie Madill21061022018-07-12 23:56:30 -0400460 ANGLE_VK_CHECK(context, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500461
Jamie Madill12222072018-07-11 14:59:48 -0400462 initFeatures();
463
Jamie Madill4d0bf552016-12-28 15:45:24 -0500464 // If only one queue family, go ahead and initialize the device. If there is more than one
465 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
466 if (graphicsQueueFamilyCount == 1)
467 {
Jamie Madill21061022018-07-12 23:56:30 -0400468 ANGLE_TRY(initializeDevice(context, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500469 }
470
Jamie Madill035fd6b2017-10-03 15:43:22 -0400471 // Store the physical device memory properties so we can find the right memory pools.
472 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500473
Jamie Madill06ca6342018-07-12 15:56:53 -0400474 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500475
Jamie Madill6a89d222017-11-02 11:59:51 -0400476 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500477 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
478 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400479
Jamie Madill21061022018-07-12 23:56:30 -0400480 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400481}
482
Jamie Madill21061022018-07-12 23:56:30 -0400483angle::Result RendererVk::initializeDevice(vk::Context *context, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500484{
485 uint32_t deviceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400486 ANGLE_VK_TRY(context,
487 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500488
489 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
490 if (deviceLayerCount > 0)
491 {
Jamie Madill21061022018-07-12 23:56:30 -0400492 ANGLE_VK_TRY(context, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
493 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500494 }
495
496 uint32_t deviceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400497 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
498 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500499
500 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
501 if (deviceExtensionCount > 0)
502 {
Jamie Madill21061022018-07-12 23:56:30 -0400503 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
504 &deviceExtensionCount,
505 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500506 }
507
Yuly Novikov199f4292018-01-19 19:04:05 -0500508 const char *const *enabledLayerNames = nullptr;
509 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500510 if (mEnableValidationLayers)
511 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500512 mEnableValidationLayers = GetAvailableValidationLayers(
513 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500514 }
515
516 std::vector<const char *> enabledDeviceExtensions;
517 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
518
Luc Ferronbf6dc372018-06-28 15:24:19 -0400519 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
520 if (getFeatures().flipViewportY)
521 {
522 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
523 }
524
Jamie Madill21061022018-07-12 23:56:30 -0400525 ANGLE_VK_TRY(context, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500526
527 VkDeviceQueueCreateInfo queueCreateInfo;
528
529 float zeroPriority = 0.0f;
530
531 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
532 queueCreateInfo.pNext = nullptr;
533 queueCreateInfo.flags = 0;
534 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
535 queueCreateInfo.queueCount = 1;
536 queueCreateInfo.pQueuePriorities = &zeroPriority;
537
538 // Initialize the device
539 VkDeviceCreateInfo createInfo;
540
Jamie Madill50cf2be2018-06-15 09:46:57 -0400541 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
542 createInfo.pNext = nullptr;
543 createInfo.flags = 0;
544 createInfo.queueCreateInfoCount = 1;
545 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500546 createInfo.enabledLayerCount = enabledLayerCount;
547 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500548 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
549 createInfo.ppEnabledExtensionNames =
550 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
551 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
552
Jamie Madill21061022018-07-12 23:56:30 -0400553 ANGLE_VK_TRY(context, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500554
555 mCurrentQueueFamilyIndex = queueFamilyIndex;
556
557 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
558
559 // Initialize the command pool now that we know the queue family index.
560 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500561 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
562 commandPoolInfo.pNext = nullptr;
563 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500564 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
565
Jamie Madill21061022018-07-12 23:56:30 -0400566 ANGLE_TRY(mCommandPool.init(context, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500567
Jamie Madill21061022018-07-12 23:56:30 -0400568 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500569}
570
Jamie Madill21061022018-07-12 23:56:30 -0400571angle::Result RendererVk::selectPresentQueueForSurface(vk::Context *context,
572 VkSurfaceKHR surface,
573 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500574{
575 // We've already initialized a device, and can't re-create it unless it's never been used.
576 // TODO(jmadill): Handle the re-creation case if necessary.
577 if (mDevice != VK_NULL_HANDLE)
578 {
579 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
580
581 // Check if the current device supports present on this surface.
582 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400583 ANGLE_VK_TRY(context,
584 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500585 surface, &supportsPresent));
586
Jamie Madill6cad7732018-07-11 09:01:17 -0400587 if (supportsPresent == VK_TRUE)
588 {
589 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400590 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400591 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500592 }
593
594 // Find a graphics and present queue.
595 Optional<uint32_t> newPresentQueue;
596 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
597 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
598 {
599 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
600 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
601 {
602 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400603 ANGLE_VK_TRY(context, vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex,
604 surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500605
606 if (supportsPresent == VK_TRUE)
607 {
608 newPresentQueue = queueIndex;
609 break;
610 }
611 }
612 }
613
Jamie Madill21061022018-07-12 23:56:30 -0400614 ANGLE_VK_CHECK(context, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
615 ANGLE_TRY(initializeDevice(context, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500616
Jamie Madill6cad7732018-07-11 09:01:17 -0400617 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400618 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500619}
620
621std::string RendererVk::getVendorString() const
622{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300623 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500624}
625
Jamie Madille09bd5d2016-11-29 16:20:35 -0500626std::string RendererVk::getRendererDescription() const
627{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500628 std::stringstream strstr;
629
630 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
631
632 strstr << "Vulkan ";
633 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
634 strstr << VK_VERSION_MINOR(apiVersion) << ".";
635 strstr << VK_VERSION_PATCH(apiVersion);
636
Olli Etuahoc6a06182018-04-13 14:11:46 +0300637 strstr << "(";
638
639 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
640 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
641 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
642 // driver detection.
643 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
644 {
645 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
646 }
647
648 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500649
650 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500651}
652
Jamie Madill12222072018-07-11 14:59:48 -0400653void RendererVk::initFeatures()
654{
655 // Use OpenGL line rasterization rules by default.
656 mFeatures.basicGLLineRasterization = true;
657
Luc Ferronf786b702018-07-10 11:01:43 -0400658 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
659 // investigation. http://anglebug.com/2728
660 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Jamie Madill12222072018-07-11 14:59:48 -0400661}
662
Jamie Madillacccc6c2016-05-03 17:22:10 -0400663void RendererVk::ensureCapsInitialized() const
664{
665 if (!mCapsInitialized)
666 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500667 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Jamie Madill12222072018-07-11 14:59:48 -0400668 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400669 mCapsInitialized = true;
670 }
671}
672
Jamie Madillacccc6c2016-05-03 17:22:10 -0400673const gl::Caps &RendererVk::getNativeCaps() const
674{
675 ensureCapsInitialized();
676 return mNativeCaps;
677}
678
679const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
680{
681 ensureCapsInitialized();
682 return mNativeTextureCaps;
683}
684
685const gl::Extensions &RendererVk::getNativeExtensions() const
686{
687 ensureCapsInitialized();
688 return mNativeExtensions;
689}
690
691const gl::Limitations &RendererVk::getNativeLimitations() const
692{
693 ensureCapsInitialized();
694 return mNativeLimitations;
695}
696
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400697uint32_t RendererVk::getMaxActiveTextures()
698{
699 // TODO(lucferron): expose this limitation to GL in Context Caps
700 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
701 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
702}
703
Jamie Madill49ac74b2017-12-21 14:42:33 -0500704const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500705{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500706 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500707}
708
Jamie Madill21061022018-07-12 23:56:30 -0400709angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500710{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500711 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500712 {
Luc Ferron1617e692018-07-11 11:08:19 -0400713 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
714 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400715
Jamie Madill49ac74b2017-12-21 14:42:33 -0500716 VkSubmitInfo submitInfo;
717 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
718 submitInfo.pNext = nullptr;
719 submitInfo.waitSemaphoreCount = 0;
720 submitInfo.pWaitSemaphores = nullptr;
721 submitInfo.pWaitDstStageMask = nullptr;
722 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400723 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500724 submitInfo.signalSemaphoreCount = 0;
725 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500726
Jamie Madill21061022018-07-12 23:56:30 -0400727 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500728 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500729
Jamie Madill4c26fc22017-02-24 11:04:10 -0500730 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400731 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400732 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400733 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500734}
735
Jamie Madill0c0dc342017-03-24 14:18:51 -0400736void RendererVk::freeAllInFlightResources()
737{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500738 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400739 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500740 batch.fence.destroy(mDevice);
741 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400742 }
743 mInFlightCommands.clear();
744
745 for (auto &garbage : mGarbage)
746 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400747 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400748 }
749 mGarbage.clear();
750}
751
Jamie Madill21061022018-07-12 23:56:30 -0400752angle::Result RendererVk::checkInFlightCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500753{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500754 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500755
Jamie Madill49ac74b2017-12-21 14:42:33 -0500756 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500757 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500758 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400759 if (result == VK_NOT_READY)
760 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500761
Jamie Madill21061022018-07-12 23:56:30 -0400762 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500763 ASSERT(batch.serial > mLastCompletedQueueSerial);
764 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400765
Jamie Madill49ac74b2017-12-21 14:42:33 -0500766 batch.fence.destroy(mDevice);
767 batch.commandPool.destroy(mDevice);
768 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500769 }
770
Jamie Madill49ac74b2017-12-21 14:42:33 -0500771 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400772
773 size_t freeIndex = 0;
774 for (; freeIndex < mGarbage.size(); ++freeIndex)
775 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500776 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400777 break;
778 }
779
780 // Remove the entries from the garbage list - they should be ready to go.
781 if (freeIndex > 0)
782 {
783 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500784 }
785
Jamie Madill21061022018-07-12 23:56:30 -0400786 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500787}
788
Jamie Madill21061022018-07-12 23:56:30 -0400789angle::Result RendererVk::submitFrame(vk::Context *context,
790 const VkSubmitInfo &submitInfo,
791 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500792{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500793 VkFenceCreateInfo fenceInfo;
794 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
795 fenceInfo.pNext = nullptr;
796 fenceInfo.flags = 0;
797
Jamie Madillbea35a62018-07-05 11:54:10 -0400798 vk::Scoped<CommandBatch> scopedBatch(mDevice);
799 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400800 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500801
Jamie Madill21061022018-07-12 23:56:30 -0400802 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500803
804 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500805 batch.commandPool = std::move(mCommandPool);
806 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500807
Jamie Madillbea35a62018-07-05 11:54:10 -0400808 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400809
810 // Sanity check.
811 ASSERT(mInFlightCommands.size() < 1000u);
812
813 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400814 // TODO(jmadill): Overflow check.
815 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400816
Jamie Madill21061022018-07-12 23:56:30 -0400817 ANGLE_TRY(checkInFlightCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400818
Jamie Madill49ac74b2017-12-21 14:42:33 -0500819 // Simply null out the command buffer here - it was allocated using the command pool.
820 commandBuffer.releaseHandle();
821
822 // Reallocate the command pool for next frame.
823 // TODO(jmadill): Consider reusing command pools.
824 VkCommandPoolCreateInfo poolInfo;
825 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
826 poolInfo.pNext = nullptr;
827 poolInfo.flags = 0;
828 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
829
Jamie Madill21061022018-07-12 23:56:30 -0400830 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500831
Jamie Madill21061022018-07-12 23:56:30 -0400832 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500833}
834
Jamie Madill4c26fc22017-02-24 11:04:10 -0500835Serial RendererVk::getCurrentQueueSerial() const
836{
837 return mCurrentQueueSerial;
838}
839
Jamie Madillaaca96e2018-06-12 10:19:48 -0400840bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500841{
842 return serial > mLastCompletedQueueSerial;
843}
844
Jamie Madill21061022018-07-12 23:56:30 -0400845angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
846 const vk::RenderPassDesc &desc,
847 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500848{
Jamie Madill21061022018-07-12 23:56:30 -0400849 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -0500850 renderPassOut);
851}
852
Jamie Madill21061022018-07-12 23:56:30 -0400853angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
854 const vk::RenderPassDesc &desc,
855 const vk::AttachmentOpsArray &ops,
856 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500857{
Jamie Madill21061022018-07-12 23:56:30 -0400858 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -0500859 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500860}
861
Jamie Madilla5e06072018-05-18 14:36:05 -0400862vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500863{
Jamie Madilla5e06072018-05-18 14:36:05 -0400864 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500865}
866
Jamie Madill21061022018-07-12 23:56:30 -0400867angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500868{
Jamie Madill21061022018-07-12 23:56:30 -0400869 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500870 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500871}
872
Jamie Madill21061022018-07-12 23:56:30 -0400873angle::Result RendererVk::flush(vk::Context *context,
874 const vk::Semaphore &waitSemaphore,
875 const vk::Semaphore &signalSemaphore)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500876{
Jamie Madillbea35a62018-07-05 11:54:10 -0400877 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
878 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500879
880 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
881
882 VkSubmitInfo submitInfo;
883 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
884 submitInfo.pNext = nullptr;
885 submitInfo.waitSemaphoreCount = 1;
886 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
887 submitInfo.pWaitDstStageMask = &waitStageMask;
888 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400889 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500890 submitInfo.signalSemaphoreCount = 1;
891 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
892
Jamie Madill21061022018-07-12 23:56:30 -0400893 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
894 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500895}
896
Jamie Madill78feddc2018-04-27 11:45:05 -0400897Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500898{
Jamie Madill78feddc2018-04-27 11:45:05 -0400899 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500900}
901
Jamie Madill06ca6342018-07-12 15:56:53 -0400902angle::Result RendererVk::getPipeline(vk::Context *context,
903 const vk::ShaderAndSerial &vertexShader,
904 const vk::ShaderAndSerial &fragmentShader,
905 const vk::PipelineLayout &pipelineLayout,
906 const vk::PipelineDesc &pipelineDesc,
907 const gl::AttributesMask &activeAttribLocationsMask,
908 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500909{
Jamie Madill06ca6342018-07-12 15:56:53 -0400910 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400911 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -0400912 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400913 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
914
915 // Pull in a compatible RenderPass.
916 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400917 ANGLE_TRY(
918 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -0400919
Jamie Madill21061022018-07-12 23:56:30 -0400920 return mPipelineCache.getPipeline(context, *compatibleRenderPass, pipelineLayout,
Jamie Madill9aef3672018-04-27 11:45:06 -0400921 activeAttribLocationsMask, vertexShader.get(),
922 fragmentShader.get(), pipelineDesc, pipelineOut);
923}
924
Jamie Madill21061022018-07-12 23:56:30 -0400925angle::Result RendererVk::getDescriptorSetLayout(
926 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400927 const vk::DescriptorSetLayoutDesc &desc,
928 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
929{
Jamie Madill21061022018-07-12 23:56:30 -0400930 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -0400931}
932
Jamie Madill21061022018-07-12 23:56:30 -0400933angle::Result RendererVk::getPipelineLayout(
934 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400935 const vk::PipelineLayoutDesc &desc,
936 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
937 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
938{
Jamie Madill21061022018-07-12 23:56:30 -0400939 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -0400940 pipelineLayoutOut);
941}
942
Jamie Madilld47044a2018-04-27 11:45:03 -0400943vk::ShaderLibrary *RendererVk::getShaderLibrary()
944{
945 return &mShaderLibrary;
946}
Luc Ferron90968362018-05-04 08:47:22 -0400947
Jamie Madillaaca96e2018-06-12 10:19:48 -0400948uint32_t GetUniformBufferDescriptorCount()
949{
950 return kUniformBufferDescriptorsPerDescriptorSet;
951}
952
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400953} // namespace rx