blob: 727d68e5a4dde20743234068e3bac0241777d91b [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
Jamie Madill12222072018-07-11 14:59:48 -0400457 initFeatures();
458
Jamie Madill4d0bf552016-12-28 15:45:24 -0500459 // If only one queue family, go ahead and initialize the device. If there is more than one
460 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
461 if (graphicsQueueFamilyCount == 1)
462 {
463 ANGLE_TRY(initializeDevice(firstGraphicsQueueFamily));
464 }
465
Jamie Madill035fd6b2017-10-03 15:43:22 -0400466 // Store the physical device memory properties so we can find the right memory pools.
467 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500468
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500469 mGlslangWrapper = GlslangWrapper::GetReference();
470
Jamie Madill6a89d222017-11-02 11:59:51 -0400471 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500472 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
473 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400474
Jamie Madill327ba852016-11-30 12:38:28 -0500475 return vk::NoError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400476}
477
Jamie Madill4d0bf552016-12-28 15:45:24 -0500478vk::Error RendererVk::initializeDevice(uint32_t queueFamilyIndex)
479{
480 uint32_t deviceLayerCount = 0;
481 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
482
483 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
484 if (deviceLayerCount > 0)
485 {
486 ANGLE_VK_TRY(vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
487 deviceLayerProps.data()));
488 }
489
490 uint32_t deviceExtensionCount = 0;
491 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
492 &deviceExtensionCount, nullptr));
493
494 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
495 if (deviceExtensionCount > 0)
496 {
497 ANGLE_VK_TRY(vkEnumerateDeviceExtensionProperties(
498 mPhysicalDevice, nullptr, &deviceExtensionCount, deviceExtensionProps.data()));
499 }
500
Yuly Novikov199f4292018-01-19 19:04:05 -0500501 const char *const *enabledLayerNames = nullptr;
502 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500503 if (mEnableValidationLayers)
504 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500505 mEnableValidationLayers = GetAvailableValidationLayers(
506 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500507 }
508
509 std::vector<const char *> enabledDeviceExtensions;
510 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
511
Luc Ferronbf6dc372018-06-28 15:24:19 -0400512 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
513 if (getFeatures().flipViewportY)
514 {
515 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
516 }
517
Jamie Madill4d0bf552016-12-28 15:45:24 -0500518 ANGLE_VK_TRY(VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
519
520 VkDeviceQueueCreateInfo queueCreateInfo;
521
522 float zeroPriority = 0.0f;
523
524 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
525 queueCreateInfo.pNext = nullptr;
526 queueCreateInfo.flags = 0;
527 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
528 queueCreateInfo.queueCount = 1;
529 queueCreateInfo.pQueuePriorities = &zeroPriority;
530
531 // Initialize the device
532 VkDeviceCreateInfo createInfo;
533
Jamie Madill50cf2be2018-06-15 09:46:57 -0400534 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
535 createInfo.pNext = nullptr;
536 createInfo.flags = 0;
537 createInfo.queueCreateInfoCount = 1;
538 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500539 createInfo.enabledLayerCount = enabledLayerCount;
540 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500541 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
542 createInfo.ppEnabledExtensionNames =
543 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
544 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
545
546 ANGLE_VK_TRY(vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
547
548 mCurrentQueueFamilyIndex = queueFamilyIndex;
549
550 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
551
552 // Initialize the command pool now that we know the queue family index.
553 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500554 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
555 commandPoolInfo.pNext = nullptr;
556 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500557 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
558
Jamie Madill5deea722017-02-16 10:44:46 -0500559 ANGLE_TRY(mCommandPool.init(mDevice, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500560
Jamie Madill4d0bf552016-12-28 15:45:24 -0500561 return vk::NoError();
562}
563
Jamie Madill6cad7732018-07-11 09:01:17 -0400564vk::Error RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface, uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500565{
566 // We've already initialized a device, and can't re-create it unless it's never been used.
567 // TODO(jmadill): Handle the re-creation case if necessary.
568 if (mDevice != VK_NULL_HANDLE)
569 {
570 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
571
572 // Check if the current device supports present on this surface.
573 VkBool32 supportsPresent = VK_FALSE;
574 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
575 surface, &supportsPresent));
576
Jamie Madill6cad7732018-07-11 09:01:17 -0400577 if (supportsPresent == VK_TRUE)
578 {
579 *presentQueueOut = mCurrentQueueFamilyIndex;
580 return vk::NoError();
581 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500582 }
583
584 // Find a graphics and present queue.
585 Optional<uint32_t> newPresentQueue;
586 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
587 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
588 {
589 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
590 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
591 {
592 VkBool32 supportsPresent = VK_FALSE;
593 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
594 &supportsPresent));
595
596 if (supportsPresent == VK_TRUE)
597 {
598 newPresentQueue = queueIndex;
599 break;
600 }
601 }
602 }
603
604 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
605 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
606
Jamie Madill6cad7732018-07-11 09:01:17 -0400607 *presentQueueOut = newPresentQueue.value();
608 return vk::NoError();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500609}
610
611std::string RendererVk::getVendorString() const
612{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300613 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500614}
615
Jamie Madille09bd5d2016-11-29 16:20:35 -0500616std::string RendererVk::getRendererDescription() const
617{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500618 std::stringstream strstr;
619
620 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
621
622 strstr << "Vulkan ";
623 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
624 strstr << VK_VERSION_MINOR(apiVersion) << ".";
625 strstr << VK_VERSION_PATCH(apiVersion);
626
Olli Etuahoc6a06182018-04-13 14:11:46 +0300627 strstr << "(";
628
629 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
630 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
631 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
632 // driver detection.
633 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
634 {
635 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
636 }
637
638 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500639
640 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500641}
642
Jamie Madill12222072018-07-11 14:59:48 -0400643void RendererVk::initFeatures()
644{
645 // Use OpenGL line rasterization rules by default.
646 mFeatures.basicGLLineRasterization = true;
647
648 // For now, set this manually to true to enable viewport flipping. A couple of features are not
649 // working well like copyTexImage, copySubTexImage, blit, and probably some more. Until
650 // everything is fixed, we will keep the viewport flipping feature disabled.
651 mFeatures.flipViewportY = false;
652}
653
Jamie Madillacccc6c2016-05-03 17:22:10 -0400654void RendererVk::ensureCapsInitialized() const
655{
656 if (!mCapsInitialized)
657 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500658 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Jamie Madill12222072018-07-11 14:59:48 -0400659 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400660 mCapsInitialized = true;
661 }
662}
663
Jamie Madillacccc6c2016-05-03 17:22:10 -0400664const gl::Caps &RendererVk::getNativeCaps() const
665{
666 ensureCapsInitialized();
667 return mNativeCaps;
668}
669
670const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
671{
672 ensureCapsInitialized();
673 return mNativeTextureCaps;
674}
675
676const gl::Extensions &RendererVk::getNativeExtensions() const
677{
678 ensureCapsInitialized();
679 return mNativeExtensions;
680}
681
682const gl::Limitations &RendererVk::getNativeLimitations() const
683{
684 ensureCapsInitialized();
685 return mNativeLimitations;
686}
687
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400688uint32_t RendererVk::getMaxActiveTextures()
689{
690 // TODO(lucferron): expose this limitation to GL in Context Caps
691 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
692 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
693}
694
Jamie Madill49ac74b2017-12-21 14:42:33 -0500695const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500696{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500697 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500698}
699
Jamie Madill49ac74b2017-12-21 14:42:33 -0500700vk::Error RendererVk::finish(const gl::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500701{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500702 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500703 {
704 vk::CommandBuffer commandBatch;
705 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400706
Jamie Madill49ac74b2017-12-21 14:42:33 -0500707 VkSubmitInfo submitInfo;
708 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
709 submitInfo.pNext = nullptr;
710 submitInfo.waitSemaphoreCount = 0;
711 submitInfo.pWaitSemaphores = nullptr;
712 submitInfo.pWaitDstStageMask = nullptr;
713 submitInfo.commandBufferCount = 1;
714 submitInfo.pCommandBuffers = commandBatch.ptr();
715 submitInfo.signalSemaphoreCount = 0;
716 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500717
Jamie Madill49ac74b2017-12-21 14:42:33 -0500718 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
719 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500720
Jamie Madill4c26fc22017-02-24 11:04:10 -0500721 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500722 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400723 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500724 return vk::NoError();
725}
726
Jamie Madill0c0dc342017-03-24 14:18:51 -0400727void RendererVk::freeAllInFlightResources()
728{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500729 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400730 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500731 batch.fence.destroy(mDevice);
732 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400733 }
734 mInFlightCommands.clear();
735
736 for (auto &garbage : mGarbage)
737 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400738 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400739 }
740 mGarbage.clear();
741}
742
Jamie Madill4c26fc22017-02-24 11:04:10 -0500743vk::Error RendererVk::checkInFlightCommands()
744{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500745 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500746
Jamie Madill49ac74b2017-12-21 14:42:33 -0500747 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500748 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500749 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400750 if (result == VK_NOT_READY)
751 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500752
Jamie Madill0c0dc342017-03-24 14:18:51 -0400753 ANGLE_VK_TRY(result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500754 ASSERT(batch.serial > mLastCompletedQueueSerial);
755 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400756
Jamie Madill49ac74b2017-12-21 14:42:33 -0500757 batch.fence.destroy(mDevice);
758 batch.commandPool.destroy(mDevice);
759 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500760 }
761
Jamie Madill49ac74b2017-12-21 14:42:33 -0500762 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400763
764 size_t freeIndex = 0;
765 for (; freeIndex < mGarbage.size(); ++freeIndex)
766 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500767 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400768 break;
769 }
770
771 // Remove the entries from the garbage list - they should be ready to go.
772 if (freeIndex > 0)
773 {
774 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500775 }
776
Jamie Madill4c26fc22017-02-24 11:04:10 -0500777 return vk::NoError();
778}
779
Jamie Madill49ac74b2017-12-21 14:42:33 -0500780vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500781{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500782 VkFenceCreateInfo fenceInfo;
783 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
784 fenceInfo.pNext = nullptr;
785 fenceInfo.flags = 0;
786
Jamie Madillbea35a62018-07-05 11:54:10 -0400787 vk::Scoped<CommandBatch> scopedBatch(mDevice);
788 CommandBatch &batch = scopedBatch.get();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500789 ANGLE_TRY(batch.fence.init(mDevice, fenceInfo));
790
791 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500792
793 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500794 batch.commandPool = std::move(mCommandPool);
795 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500796
Jamie Madillbea35a62018-07-05 11:54:10 -0400797 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400798
799 // Sanity check.
800 ASSERT(mInFlightCommands.size() < 1000u);
801
802 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400803 // TODO(jmadill): Overflow check.
804 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400805
806 ANGLE_TRY(checkInFlightCommands());
807
Jamie Madill49ac74b2017-12-21 14:42:33 -0500808 // Simply null out the command buffer here - it was allocated using the command pool.
809 commandBuffer.releaseHandle();
810
811 // Reallocate the command pool for next frame.
812 // TODO(jmadill): Consider reusing command pools.
813 VkCommandPoolCreateInfo poolInfo;
814 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
815 poolInfo.pNext = nullptr;
816 poolInfo.flags = 0;
817 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
818
Jamie Madilleebe2192018-07-11 09:01:18 -0400819 ANGLE_TRY(mCommandPool.init(mDevice, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500820
Jamie Madill4c26fc22017-02-24 11:04:10 -0500821 return vk::NoError();
822}
823
Jamie Madillaaca96e2018-06-12 10:19:48 -0400824GlslangWrapper *RendererVk::getGlslangWrapper() const
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500825{
826 return mGlslangWrapper;
827}
828
Jamie Madill4c26fc22017-02-24 11:04:10 -0500829Serial RendererVk::getCurrentQueueSerial() const
830{
831 return mCurrentQueueSerial;
832}
833
Jamie Madillaaca96e2018-06-12 10:19:48 -0400834bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500835{
836 return serial > mLastCompletedQueueSerial;
837}
838
Jamie Madill9f2a8612017-11-30 12:43:09 -0500839vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc,
840 vk::RenderPass **renderPassOut)
841{
842 return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc,
843 renderPassOut);
844}
845
Jamie Madillbef918c2017-12-13 13:11:30 -0500846vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc,
847 const vk::AttachmentOpsArray &ops,
848 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500849{
Jamie Madillbef918c2017-12-13 13:11:30 -0500850 return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops,
851 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500852}
853
Jamie Madilla5e06072018-05-18 14:36:05 -0400854vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500855{
Jamie Madilla5e06072018-05-18 14:36:05 -0400856 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500857}
858
859vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch)
860{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500861 return mCommandGraph.submitCommands(mDevice, mCurrentQueueSerial, &mRenderPassCache,
862 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500863}
864
865vk::Error RendererVk::flush(const gl::Context *context,
866 const vk::Semaphore &waitSemaphore,
867 const vk::Semaphore &signalSemaphore)
868{
Jamie Madillbea35a62018-07-05 11:54:10 -0400869 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
870 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500871
872 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
873
874 VkSubmitInfo submitInfo;
875 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
876 submitInfo.pNext = nullptr;
877 submitInfo.waitSemaphoreCount = 1;
878 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
879 submitInfo.pWaitDstStageMask = &waitStageMask;
880 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400881 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500882 submitInfo.signalSemaphoreCount = 1;
883 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
884
Jamie Madillbea35a62018-07-05 11:54:10 -0400885 ANGLE_TRY(submitFrame(submitInfo, commandBatch.release()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500886 return vk::NoError();
887}
888
Jamie Madill78feddc2018-04-27 11:45:05 -0400889Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500890{
Jamie Madill78feddc2018-04-27 11:45:05 -0400891 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500892}
893
Jamie Madill9aef3672018-04-27 11:45:06 -0400894vk::Error RendererVk::getAppPipeline(const ProgramVk *programVk,
895 const vk::PipelineDesc &desc,
896 const gl::AttributesMask &activeAttribLocationsMask,
897 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500898{
Jamie Madill33318de2018-05-01 11:22:54 -0400899 ASSERT(programVk->getVertexModuleSerial() ==
900 desc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
901 ASSERT(programVk->getFragmentModuleSerial() ==
902 desc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500903
904 // Pull in a compatible RenderPass.
905 vk::RenderPass *compatibleRenderPass = nullptr;
906 ANGLE_TRY(getCompatibleRenderPass(desc.getRenderPassDesc(), &compatibleRenderPass));
907
Jamie Madill9b168d02018-06-13 13:25:32 -0400908 const vk::PipelineLayout &pipelineLayout = programVk->getPipelineLayout();
909
Jamie Madillc7918ce2018-06-13 13:25:31 -0400910 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, pipelineLayout,
Luc Ferronceb71902018-02-05 15:18:47 -0500911 activeAttribLocationsMask, programVk->getLinkedVertexModule(),
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500912 programVk->getLinkedFragmentModule(), desc, pipelineOut);
913}
914
Jamie Madill9aef3672018-04-27 11:45:06 -0400915vk::Error RendererVk::getInternalPipeline(const vk::ShaderAndSerial &vertexShader,
916 const vk::ShaderAndSerial &fragmentShader,
917 const vk::PipelineLayout &pipelineLayout,
918 const vk::PipelineDesc &pipelineDesc,
919 const gl::AttributesMask &activeAttribLocationsMask,
920 vk::PipelineAndSerial **pipelineOut)
921{
922 ASSERT(vertexShader.queueSerial() ==
923 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
924 ASSERT(fragmentShader.queueSerial() ==
925 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
926
927 // Pull in a compatible RenderPass.
928 vk::RenderPass *compatibleRenderPass = nullptr;
929 ANGLE_TRY(getCompatibleRenderPass(pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
930
931 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, pipelineLayout,
932 activeAttribLocationsMask, vertexShader.get(),
933 fragmentShader.get(), pipelineDesc, pipelineOut);
934}
935
Jamie Madill9b168d02018-06-13 13:25:32 -0400936vk::Error RendererVk::getDescriptorSetLayout(
937 const vk::DescriptorSetLayoutDesc &desc,
938 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
939{
940 return mDescriptorSetLayoutCache.getDescriptorSetLayout(mDevice, desc, descriptorSetLayoutOut);
941}
942
943vk::Error RendererVk::getPipelineLayout(
944 const vk::PipelineLayoutDesc &desc,
945 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
946 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
947{
948 return mPipelineLayoutCache.getPipelineLayout(mDevice, desc, descriptorSetLayouts,
949 pipelineLayoutOut);
950}
951
Jamie Madilld47044a2018-04-27 11:45:03 -0400952vk::ShaderLibrary *RendererVk::getShaderLibrary()
953{
954 return &mShaderLibrary;
955}
Luc Ferron90968362018-05-04 08:47:22 -0400956
Jamie Madillaaca96e2018-06-12 10:19:48 -0400957uint32_t GetUniformBufferDescriptorCount()
958{
959 return kUniformBufferDescriptorsPerDescriptorSet;
960}
961
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400962} // namespace rx