blob: 9421154d863988e21b1db93b5cf037535008c28d [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 Madille09bd5d2016-11-29 16:20:35 -0500199} // anonymous namespace
200
Jamie Madill49ac74b2017-12-21 14:42:33 -0500201// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400202RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500203
Jamie Madillaaca96e2018-06-12 10:19:48 -0400204RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500205
206RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
207 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
208{
209}
210
211RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
212{
213 std::swap(commandPool, other.commandPool);
214 std::swap(fence, other.fence);
215 std::swap(serial, other.serial);
216 return *this;
217}
218
Jamie Madillbea35a62018-07-05 11:54:10 -0400219void RendererVk::CommandBatch::destroy(VkDevice device)
220{
221 commandPool.destroy(device);
222 fence.destroy(device);
223}
224
Jamie Madill9f2a8612017-11-30 12:43:09 -0500225// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500226RendererVk::RendererVk()
227 : mCapsInitialized(false),
228 mInstance(VK_NULL_HANDLE),
229 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500230 mDebugReportCallback(VK_NULL_HANDLE),
231 mPhysicalDevice(VK_NULL_HANDLE),
232 mQueue(VK_NULL_HANDLE),
233 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
234 mDevice(VK_NULL_HANDLE),
Jamie Madill4c26fc22017-02-24 11:04:10 -0500235 mGlslangWrapper(nullptr),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400236 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Jamie Madillaaca96e2018-06-12 10:19:48 -0400237 mCurrentQueueSerial(mQueueSerialFactory.generate())
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400238{
239}
240
241RendererVk::~RendererVk()
242{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500243 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500244 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500245 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
246 vk::Error error = finish(nullptr);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500247 if (error.isError())
248 {
249 ERR() << "Error during VK shutdown: " << error;
250 }
251 }
252
Jamie Madillc7918ce2018-06-13 13:25:31 -0400253 mPipelineLayoutCache.destroy(mDevice);
254 mDescriptorSetLayoutCache.destroy(mDevice);
255
Jamie Madill9f2a8612017-11-30 12:43:09 -0500256 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500257 mPipelineCache.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400258 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500259
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500260 if (mGlslangWrapper)
261 {
262 GlslangWrapper::ReleaseReference();
263 mGlslangWrapper = nullptr;
264 }
265
Jamie Madill5deea722017-02-16 10:44:46 -0500266 if (mCommandPool.valid())
267 {
268 mCommandPool.destroy(mDevice);
269 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500270
271 if (mDevice)
272 {
273 vkDestroyDevice(mDevice, nullptr);
274 mDevice = VK_NULL_HANDLE;
275 }
276
Jamie Madill0448ec82016-12-23 13:41:47 -0500277 if (mDebugReportCallback)
278 {
279 ASSERT(mInstance);
280 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
281 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
282 ASSERT(destroyDebugReportCallback);
283 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
284 }
285
Jamie Madill4d0bf552016-12-28 15:45:24 -0500286 if (mInstance)
287 {
288 vkDestroyInstance(mInstance, nullptr);
289 mInstance = VK_NULL_HANDLE;
290 }
291
292 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500293}
294
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700295void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
296 bool preferMockICD,
297 VkPhysicalDevice *physicalDeviceOut,
298 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
299{
300 ASSERT(!physicalDevices.empty());
301 if (preferMockICD)
302 {
303 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
304 {
305 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
306 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
307 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
308 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
309 {
310 *physicalDeviceOut = physicalDevice;
311 return;
312 }
313 }
314 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
315 "physicalDevice instead.";
316 }
317
318 // Fall back to first device.
319 *physicalDeviceOut = physicalDevices[0];
320 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
321}
322
Frank Henigman29f148b2016-11-23 21:05:36 -0500323vk::Error RendererVk::initialize(const egl::AttributeMap &attribs, const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500324{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600325 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
326 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500327 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600328 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500329
Jamie Madill0448ec82016-12-23 13:41:47 -0500330 // Gather global layer properties.
331 uint32_t instanceLayerCount = 0;
332 ANGLE_VK_TRY(vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
333
334 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
335 if (instanceLayerCount > 0)
336 {
337 ANGLE_VK_TRY(
338 vkEnumerateInstanceLayerProperties(&instanceLayerCount, instanceLayerProps.data()));
339 }
340
Jamie Madille09bd5d2016-11-29 16:20:35 -0500341 uint32_t instanceExtensionCount = 0;
342 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
343
344 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
345 if (instanceExtensionCount > 0)
346 {
347 ANGLE_VK_TRY(vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
348 instanceExtensionProps.data()));
349 }
350
Yuly Novikov199f4292018-01-19 19:04:05 -0500351 const char *const *enabledLayerNames = nullptr;
352 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500353 if (mEnableValidationLayers)
354 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500355 bool layersRequested =
356 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
357 mEnableValidationLayers = GetAvailableValidationLayers(
358 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500359 }
360
Jamie Madille09bd5d2016-11-29 16:20:35 -0500361 std::vector<const char *> enabledInstanceExtensions;
362 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500363 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500364
Jamie Madill0448ec82016-12-23 13:41:47 -0500365 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
366 if (mEnableValidationLayers)
367 {
368 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
369 }
370
Jamie Madille09bd5d2016-11-29 16:20:35 -0500371 // Verify the required extensions are in the extension names set. Fail if not.
372 ANGLE_VK_TRY(VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
373
Jamie Madill327ba852016-11-30 12:38:28 -0500374 VkApplicationInfo applicationInfo;
375 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
376 applicationInfo.pNext = nullptr;
377 applicationInfo.pApplicationName = "ANGLE";
378 applicationInfo.applicationVersion = 1;
379 applicationInfo.pEngineName = "ANGLE";
380 applicationInfo.engineVersion = 1;
381 applicationInfo.apiVersion = VK_API_VERSION_1_0;
382
383 VkInstanceCreateInfo instanceInfo;
384 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
385 instanceInfo.pNext = nullptr;
386 instanceInfo.flags = 0;
387 instanceInfo.pApplicationInfo = &applicationInfo;
388
Jamie Madille09bd5d2016-11-29 16:20:35 -0500389 // Enable requested layers and extensions.
390 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
391 instanceInfo.ppEnabledExtensionNames =
392 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500393 instanceInfo.enabledLayerCount = enabledLayerCount;
394 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500395
396 ANGLE_VK_TRY(vkCreateInstance(&instanceInfo, nullptr, &mInstance));
397
Jamie Madill0448ec82016-12-23 13:41:47 -0500398 if (mEnableValidationLayers)
399 {
400 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
401
402 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
403 debugReportInfo.pNext = nullptr;
404 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
405 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
406 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
407 debugReportInfo.pfnCallback = &DebugReportCallback;
408 debugReportInfo.pUserData = this;
409
410 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
411 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
412 ASSERT(createDebugReportCallback);
413 ANGLE_VK_TRY(
414 createDebugReportCallback(mInstance, &debugReportInfo, nullptr, &mDebugReportCallback));
415 }
416
Jamie Madill4d0bf552016-12-28 15:45:24 -0500417 uint32_t physicalDeviceCount = 0;
418 ANGLE_VK_TRY(vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
419 ANGLE_VK_CHECK(physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
420
421 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700422 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
423 ANGLE_VK_TRY(
424 vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600425 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700426 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500427
428 // Ensure we can find a graphics queue family.
429 uint32_t queueCount = 0;
430 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
431
432 ANGLE_VK_CHECK(queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
433
434 mQueueFamilyProperties.resize(queueCount);
435 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
436 mQueueFamilyProperties.data());
437
438 size_t graphicsQueueFamilyCount = false;
439 uint32_t firstGraphicsQueueFamily = 0;
440 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
441 {
442 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
443 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
444 {
445 ASSERT(queueInfo.queueCount > 0);
446 graphicsQueueFamilyCount++;
447 if (firstGraphicsQueueFamily == 0)
448 {
449 firstGraphicsQueueFamily = familyIndex;
450 }
451 break;
452 }
453 }
454
455 ANGLE_VK_CHECK(graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
456
457 // If only one queue family, go ahead and initialize the device. If there is more than one
458 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
459 if (graphicsQueueFamilyCount == 1)
460 {
461 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
462 }
463
Jamie Madill035fd6b2017-10-03 15:43:22 -0400464 // Store the physical device memory properties so we can find the right memory pools.
465 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500466
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500467 mGlslangWrapper = GlslangWrapper::GetReference();
468
Jamie Madill6a89d222017-11-02 11:59:51 -0400469 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500470 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
471 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400472
Jamie Madill327ba852016-11-30 12:38:28 -0500473 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400474}
475
Jamie Madill4d0bf552016-12-28 15:45:24 -0500476vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
477{
478 uint32_t deviceLayerCount = 0;
479 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
480
481 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
482 if (deviceLayerCount > 0)
483 {
484 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
485 deviceLayerProps.data()));
486 }
487
488 uint32_t deviceExtensionCount = 0;
489 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
490 &deviceExtensionCount, nullptr));
491
492 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
493 if (deviceExtensionCount > 0)
494 {
495 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
496 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
497 }
498
Yuly Novikov199f4292018-01-19 19:04:05 -0500499 const char *const *enabledLayerNames = nullptr;
500 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500501 if (mEnableValidationLayers)
502 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500503 mEnableValidationLayers = GetAvailableValidationLayers(
504 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500505 }
506
507 std::vector<const char *> enabledDeviceExtensions;
508 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
509
Luc Ferronbf6dc372018-06-28 15:24:19 -0400510 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
511 if (getFeatures().flipViewportY)
512 {
513 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
514 }
515
Jamie Madill4d0bf552016-12-28 15:45:24 -0500516 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
517
518 VkDeviceQueueCreateInfo queueCreateInfo;
519
520 float zeroPriority = 0.0f;
521
522 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
523 queueCreateInfo.pNext = nullptr;
524 queueCreateInfo.flags = 0;
525 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
526 queueCreateInfo.queueCount = 1;
527 queueCreateInfo.pQueuePriorities = &zeroPriority;
528
529 // Initialize the device
530 VkDeviceCreateInfo createInfo;
531
Jamie Madill50cf2be2018-06-15 09:46:57 -0400532 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
533 createInfo.pNext = nullptr;
534 createInfo.flags = 0;
535 createInfo.queueCreateInfoCount = 1;
536 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500537 createInfo.enabledLayerCount = enabledLayerCount;
538 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500539 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
540 createInfo.ppEnabledExtensionNames =
541 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
542 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
543
544 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
545
546 mCurrentQueueFamilyIndex = queueFamilyIndex;
547
548 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
549
550 // Initialize the command pool now that we know the queue family index.
551 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500552 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
553 commandPoolInfo.pNext = nullptr;
554 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500555 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
556
Jamie Madill5deea722017-02-16 10:44:46 -0500557 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500558
Jamie Madill4d0bf552016-12-28 15:45:24 -0500559 return vk::NoError();
560}
561
562vk::ErrorOrResult<uint32_t> RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface)
563{
564 // We've already initialized a device, and can't re-create it unless it's never been used.
565 // TODO(jmadill): Handle the re-creation case if necessary.
566 if (mDevice != VK_NULL_HANDLE)
567 {
568 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
569
570 // Check if the current device supports present on this surface.
571 VkBool32 supportsPresent = VK_FALSE;
572 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
573 surface, &supportsPresent));
574
575 return (supportsPresent == VK_TRUE);
576 }
577
578 // Find a graphics and present queue.
579 Optional<uint32_t> newPresentQueue;
580 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
581 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
582 {
583 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
584 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
585 {
586 VkBool32 supportsPresent = VK_FALSE;
587 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
588 &supportsPresent));
589
590 if (supportsPresent == VK_TRUE)
591 {
592 newPresentQueue = queueIndex;
593 break;
594 }
595 }
596 }
597
598 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
599 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
600
601 return newPresentQueue.value();
602}
603
604std::string RendererVk::getVendorString() const
605{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300606 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500607}
608
Jamie Madille09bd5d2016-11-29 16:20:35 -0500609std::string RendererVk::getRendererDescription() const
610{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500611 std::stringstream strstr;
612
613 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
614
615 strstr << "Vulkan ";
616 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
617 strstr << VK_VERSION_MINOR(apiVersion) << ".";
618 strstr << VK_VERSION_PATCH(apiVersion);
619
Olli Etuahoc6a06182018-04-13 14:11:46 +0300620 strstr << "(";
621
622 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
623 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
624 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
625 // driver detection.
626 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
627 {
628 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
629 }
630
631 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500632
633 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500634}
635
Jamie Madillacccc6c2016-05-03 17:22:10 -0400636void RendererVk::ensureCapsInitialized() const
637{
638 if (!mCapsInitialized)
639 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500640 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Jamie Madill1266d202018-06-29 09:11:34 -0400641 &mNativeExtensions, &mNativeLimitations, &mFeatures);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400642 mCapsInitialized = true;
643 }
644}
645
Jamie Madillacccc6c2016-05-03 17:22:10 -0400646const gl::Caps &RendererVk::getNativeCaps() const
647{
648 ensureCapsInitialized();
649 return mNativeCaps;
650}
651
652const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
653{
654 ensureCapsInitialized();
655 return mNativeTextureCaps;
656}
657
658const gl::Extensions &RendererVk::getNativeExtensions() const
659{
660 ensureCapsInitialized();
661 return mNativeExtensions;
662}
663
664const gl::Limitations &RendererVk::getNativeLimitations() const
665{
666 ensureCapsInitialized();
667 return mNativeLimitations;
668}
669
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400670uint32_t RendererVk::getMaxActiveTextures()
671{
672 // TODO(lucferron): expose this limitation to GL in Context Caps
673 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
674 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
675}
676
Jamie Madill49ac74b2017-12-21 14:42:33 -0500677const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500678{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500679 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500680}
681
Jamie Madill49ac74b2017-12-21 14:42:33 -0500682vk::Error RendererVk::finish(const gl::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500683{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500684 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500685 {
686 vk::CommandBuffer commandBatch;
687 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400688
Jamie Madill49ac74b2017-12-21 14:42:33 -0500689 VkSubmitInfo submitInfo;
690 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
691 submitInfo.pNext = nullptr;
692 submitInfo.waitSemaphoreCount = 0;
693 submitInfo.pWaitSemaphores = nullptr;
694 submitInfo.pWaitDstStageMask = nullptr;
695 submitInfo.commandBufferCount = 1;
696 submitInfo.pCommandBuffers = commandBatch.ptr();
697 submitInfo.signalSemaphoreCount = 0;
698 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500699
Jamie Madill49ac74b2017-12-21 14:42:33 -0500700 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
701 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500702
Jamie Madill4c26fc22017-02-24 11:04:10 -0500703 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500704 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400705 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500706 return vk::NoError();
707}
708
Jamie Madill0c0dc342017-03-24 14:18:51 -0400709void RendererVk::freeAllInFlightResources()
710{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500711 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400712 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500713 batch.fence.destroy(mDevice);
714 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400715 }
716 mInFlightCommands.clear();
717
718 for (auto &garbage : mGarbage)
719 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400720 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400721 }
722 mGarbage.clear();
723}
724
Jamie Madill4c26fc22017-02-24 11:04:10 -0500725vk::Error RendererVk::checkInFlightCommands()
726{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500727 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500728
Jamie Madill49ac74b2017-12-21 14:42:33 -0500729 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500730 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500731 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400732 if (result == VK_NOT_READY)
733 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500734
Jamie Madill0c0dc342017-03-24 14:18:51 -0400735 ANGLE_VK_TRY(result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500736 ASSERT(batch.serial > mLastCompletedQueueSerial);
737 mLastCompletedQueueSerial = batch.serial;
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);
741 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500742 }
743
Jamie Madill49ac74b2017-12-21 14:42:33 -0500744 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400745
746 size_t freeIndex = 0;
747 for (; freeIndex < mGarbage.size(); ++freeIndex)
748 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500749 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400750 break;
751 }
752
753 // Remove the entries from the garbage list - they should be ready to go.
754 if (freeIndex > 0)
755 {
756 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500757 }
758
Jamie Madill4c26fc22017-02-24 11:04:10 -0500759 return vk::NoError();
760}
761
Jamie Madill49ac74b2017-12-21 14:42:33 -0500762vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500763{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500764 VkFenceCreateInfo fenceInfo;
765 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
766 fenceInfo.pNext = nullptr;
767 fenceInfo.flags = 0;
768
Jamie Madillbea35a62018-07-05 11:54:10 -0400769 vk::Scoped<CommandBatch> scopedBatch(mDevice);
770 CommandBatch &batch = scopedBatch.get();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500771 ANGLE_TRY(batch.fence.init(mDevice, fenceInfo));
772
773 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500774
775 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500776 batch.commandPool = std::move(mCommandPool);
777 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500778
Jamie Madillbea35a62018-07-05 11:54:10 -0400779 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400780
781 // Sanity check.
782 ASSERT(mInFlightCommands.size() < 1000u);
783
784 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400785 // TODO(jmadill): Overflow check.
786 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400787
788 ANGLE_TRY(checkInFlightCommands());
789
Jamie Madill49ac74b2017-12-21 14:42:33 -0500790 // Simply null out the command buffer here - it was allocated using the command pool.
791 commandBuffer.releaseHandle();
792
793 // Reallocate the command pool for next frame.
794 // TODO(jmadill): Consider reusing command pools.
795 VkCommandPoolCreateInfo poolInfo;
796 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
797 poolInfo.pNext = nullptr;
798 poolInfo.flags = 0;
799 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
800
801 mCommandPool.init(mDevice, poolInfo);
802
Jamie Madill4c26fc22017-02-24 11:04:10 -0500803 return vk::NoError();
804}
805
Jamie Madillaaca96e2018-06-12 10:19:48 -0400806GlslangWrapper *RendererVk::getGlslangWrapper() const
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500807{
808 return mGlslangWrapper;
809}
810
Jamie Madill4c26fc22017-02-24 11:04:10 -0500811Serial RendererVk::getCurrentQueueSerial() const
812{
813 return mCurrentQueueSerial;
814}
815
Jamie Madillaaca96e2018-06-12 10:19:48 -0400816bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500817{
818 return serial > mLastCompletedQueueSerial;
819}
820
Jamie Madill9f2a8612017-11-30 12:43:09 -0500821vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc,
822 vk::RenderPass **renderPassOut)
823{
824 return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc,
825 renderPassOut);
826}
827
Jamie Madillbef918c2017-12-13 13:11:30 -0500828vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc,
829 const vk::AttachmentOpsArray &ops,
830 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500831{
Jamie Madillbef918c2017-12-13 13:11:30 -0500832 return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops,
833 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500834}
835
Jamie Madilla5e06072018-05-18 14:36:05 -0400836vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500837{
Jamie Madilla5e06072018-05-18 14:36:05 -0400838 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500839}
840
841vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch)
842{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500843 return mCommandGraph.submitCommands(mDevice, mCurrentQueueSerial, &mRenderPassCache,
844 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500845}
846
847vk::Error RendererVk::flush(const gl::Context *context,
848 const vk::Semaphore &waitSemaphore,
849 const vk::Semaphore &signalSemaphore)
850{
Jamie Madillbea35a62018-07-05 11:54:10 -0400851 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
852 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500853
854 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
855
856 VkSubmitInfo submitInfo;
857 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
858 submitInfo.pNext = nullptr;
859 submitInfo.waitSemaphoreCount = 1;
860 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
861 submitInfo.pWaitDstStageMask = &waitStageMask;
862 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400863 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500864 submitInfo.signalSemaphoreCount = 1;
865 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
866
Jamie Madillbea35a62018-07-05 11:54:10 -0400867 ANGLE_TRY(submitFrame(submitInfo, commandBatch.release()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500868 return vk::NoError();
869}
870
Jamie Madill78feddc2018-04-27 11:45:05 -0400871Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500872{
Jamie Madill78feddc2018-04-27 11:45:05 -0400873 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500874}
875
Jamie Madill9aef3672018-04-27 11:45:06 -0400876vk::Error RendererVk::getAppPipeline(const ProgramVk *programVk,
877 const vk::PipelineDesc &desc,
878 const gl::AttributesMask &activeAttribLocationsMask,
879 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500880{
Jamie Madill33318de2018-05-01 11:22:54 -0400881 ASSERT(programVk->getVertexModuleSerial() ==
882 desc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
883 ASSERT(programVk->getFragmentModuleSerial() ==
884 desc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500885
886 // Pull in a compatible RenderPass.
887 vk::RenderPass *compatibleRenderPass = nullptr;
888 ANGLE_TRY(getCompatibleRenderPass(desc.getRenderPassDesc(), &compatibleRenderPass));
889
Jamie Madill9b168d02018-06-13 13:25:32 -0400890 const vk::PipelineLayout &pipelineLayout = programVk->getPipelineLayout();
891
Jamie Madillc7918ce2018-06-13 13:25:31 -0400892 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, pipelineLayout,
Luc Ferronceb71902018-02-05 15:18:47 -0500893 activeAttribLocationsMask, programVk->getLinkedVertexModule(),
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500894 programVk->getLinkedFragmentModule(), desc, pipelineOut);
895}
896
Jamie Madill9aef3672018-04-27 11:45:06 -0400897vk::Error RendererVk::getInternalPipeline(const vk::ShaderAndSerial &vertexShader,
898 const vk::ShaderAndSerial &fragmentShader,
899 const vk::PipelineLayout &pipelineLayout,
900 const vk::PipelineDesc &pipelineDesc,
901 const gl::AttributesMask &activeAttribLocationsMask,
902 vk::PipelineAndSerial **pipelineOut)
903{
904 ASSERT(vertexShader.queueSerial() ==
905 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
906 ASSERT(fragmentShader.queueSerial() ==
907 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
908
909 // Pull in a compatible RenderPass.
910 vk::RenderPass *compatibleRenderPass = nullptr;
911 ANGLE_TRY(getCompatibleRenderPass(pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
912
913 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, pipelineLayout,
914 activeAttribLocationsMask, vertexShader.get(),
915 fragmentShader.get(), pipelineDesc, pipelineOut);
916}
917
Jamie Madill9b168d02018-06-13 13:25:32 -0400918vk::Error RendererVk::getDescriptorSetLayout(
919 const vk::DescriptorSetLayoutDesc &desc,
920 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
921{
922 return mDescriptorSetLayoutCache.getDescriptorSetLayout(mDevice, desc, descriptorSetLayoutOut);
923}
924
925vk::Error RendererVk::getPipelineLayout(
926 const vk::PipelineLayoutDesc &desc,
927 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
928 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
929{
930 return mPipelineLayoutCache.getPipelineLayout(mDevice, desc, descriptorSetLayouts,
931 pipelineLayoutOut);
932}
933
Jamie Madilld47044a2018-04-27 11:45:03 -0400934vk::ShaderLibrary *RendererVk::getShaderLibrary()
935{
936 return &mShaderLibrary;
937}
Luc Ferron90968362018-05-04 08:47:22 -0400938
Jamie Madillaaca96e2018-06-12 10:19:48 -0400939uint32_t GetUniformBufferDescriptorCount()
940{
941 return kUniformBufferDescriptorsPerDescriptorSet;
942}
943
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400944} // namespace rx