blob: 2e7d7d69ad89646c695f42fc680479dc929be978 [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
Jamie Madill6cad7732018-07-11 09:01:17 -0400562vk::Error RendererVk::selectPresentQueueForSurface(VkSurfaceKHR surface, uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500563{
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
Jamie Madill6cad7732018-07-11 09:01:17 -0400575 if (supportsPresent == VK_TRUE)
576 {
577 *presentQueueOut = mCurrentQueueFamilyIndex;
578 return vk::NoError();
579 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500580 }
581
582 // Find a graphics and present queue.
583 Optional<uint32_t> newPresentQueue;
584 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
585 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
586 {
587 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
588 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
589 {
590 VkBool32 supportsPresent = VK_FALSE;
591 ANGLE_VK_TRY(vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex, surface,
592 &supportsPresent));
593
594 if (supportsPresent == VK_TRUE)
595 {
596 newPresentQueue = queueIndex;
597 break;
598 }
599 }
600 }
601
602 ANGLE_VK_CHECK(newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
603 ANGLE_TRY(initializeDevice(newPresentQueue.value()));
604
Jamie Madill6cad7732018-07-11 09:01:17 -0400605 *presentQueueOut = newPresentQueue.value();
606 return vk::NoError();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500607}
608
609std::string RendererVk::getVendorString() const
610{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300611 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500612}
613
Jamie Madille09bd5d2016-11-29 16:20:35 -0500614std::string RendererVk::getRendererDescription() const
615{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500616 std::stringstream strstr;
617
618 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
619
620 strstr << "Vulkan ";
621 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
622 strstr << VK_VERSION_MINOR(apiVersion) << ".";
623 strstr << VK_VERSION_PATCH(apiVersion);
624
Olli Etuahoc6a06182018-04-13 14:11:46 +0300625 strstr << "(";
626
627 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
628 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
629 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
630 // driver detection.
631 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
632 {
633 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
634 }
635
636 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500637
638 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500639}
640
Jamie Madillacccc6c2016-05-03 17:22:10 -0400641void RendererVk::ensureCapsInitialized() const
642{
643 if (!mCapsInitialized)
644 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500645 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Jamie Madill1266d202018-06-29 09:11:34 -0400646 &mNativeExtensions, &mNativeLimitations, &mFeatures);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400647 mCapsInitialized = true;
648 }
649}
650
Jamie Madillacccc6c2016-05-03 17:22:10 -0400651const gl::Caps &RendererVk::getNativeCaps() const
652{
653 ensureCapsInitialized();
654 return mNativeCaps;
655}
656
657const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
658{
659 ensureCapsInitialized();
660 return mNativeTextureCaps;
661}
662
663const gl::Extensions &RendererVk::getNativeExtensions() const
664{
665 ensureCapsInitialized();
666 return mNativeExtensions;
667}
668
669const gl::Limitations &RendererVk::getNativeLimitations() const
670{
671 ensureCapsInitialized();
672 return mNativeLimitations;
673}
674
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400675uint32_t RendererVk::getMaxActiveTextures()
676{
677 // TODO(lucferron): expose this limitation to GL in Context Caps
678 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
679 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
680}
681
Jamie Madill49ac74b2017-12-21 14:42:33 -0500682const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500683{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500684 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500685}
686
Jamie Madill49ac74b2017-12-21 14:42:33 -0500687vk::Error RendererVk::finish(const gl::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500688{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500689 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500690 {
691 vk::CommandBuffer commandBatch;
692 ANGLE_TRY(flushCommandGraph(context, &commandBatch));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400693
Jamie Madill49ac74b2017-12-21 14:42:33 -0500694 VkSubmitInfo submitInfo;
695 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
696 submitInfo.pNext = nullptr;
697 submitInfo.waitSemaphoreCount = 0;
698 submitInfo.pWaitSemaphores = nullptr;
699 submitInfo.pWaitDstStageMask = nullptr;
700 submitInfo.commandBufferCount = 1;
701 submitInfo.pCommandBuffers = commandBatch.ptr();
702 submitInfo.signalSemaphoreCount = 0;
703 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500704
Jamie Madill49ac74b2017-12-21 14:42:33 -0500705 ANGLE_TRY(submitFrame(submitInfo, std::move(commandBatch)));
706 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500707
Jamie Madill4c26fc22017-02-24 11:04:10 -0500708 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500709 ANGLE_VK_TRY(vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400710 freeAllInFlightResources();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500711 return vk::NoError();
712}
713
Jamie Madill0c0dc342017-03-24 14:18:51 -0400714void RendererVk::freeAllInFlightResources()
715{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500716 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400717 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500718 batch.fence.destroy(mDevice);
719 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400720 }
721 mInFlightCommands.clear();
722
723 for (auto &garbage : mGarbage)
724 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400725 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400726 }
727 mGarbage.clear();
728}
729
Jamie Madill4c26fc22017-02-24 11:04:10 -0500730vk::Error RendererVk::checkInFlightCommands()
731{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500732 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500733
Jamie Madill49ac74b2017-12-21 14:42:33 -0500734 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500735 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500736 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400737 if (result == VK_NOT_READY)
738 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500739
Jamie Madill0c0dc342017-03-24 14:18:51 -0400740 ANGLE_VK_TRY(result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500741 ASSERT(batch.serial > mLastCompletedQueueSerial);
742 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400743
Jamie Madill49ac74b2017-12-21 14:42:33 -0500744 batch.fence.destroy(mDevice);
745 batch.commandPool.destroy(mDevice);
746 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500747 }
748
Jamie Madill49ac74b2017-12-21 14:42:33 -0500749 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400750
751 size_t freeIndex = 0;
752 for (; freeIndex < mGarbage.size(); ++freeIndex)
753 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500754 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400755 break;
756 }
757
758 // Remove the entries from the garbage list - they should be ready to go.
759 if (freeIndex > 0)
760 {
761 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500762 }
763
Jamie Madill4c26fc22017-02-24 11:04:10 -0500764 return vk::NoError();
765}
766
Jamie Madill49ac74b2017-12-21 14:42:33 -0500767vk::Error RendererVk::submitFrame(const VkSubmitInfo &submitInfo, vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500768{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500769 VkFenceCreateInfo fenceInfo;
770 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
771 fenceInfo.pNext = nullptr;
772 fenceInfo.flags = 0;
773
Jamie Madillbea35a62018-07-05 11:54:10 -0400774 vk::Scoped<CommandBatch> scopedBatch(mDevice);
775 CommandBatch &batch = scopedBatch.get();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500776 ANGLE_TRY(batch.fence.init(mDevice, fenceInfo));
777
778 ANGLE_VK_TRY(vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500779
780 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500781 batch.commandPool = std::move(mCommandPool);
782 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500783
Jamie Madillbea35a62018-07-05 11:54:10 -0400784 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400785
786 // Sanity check.
787 ASSERT(mInFlightCommands.size() < 1000u);
788
789 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400790 // TODO(jmadill): Overflow check.
791 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400792
793 ANGLE_TRY(checkInFlightCommands());
794
Jamie Madill49ac74b2017-12-21 14:42:33 -0500795 // Simply null out the command buffer here - it was allocated using the command pool.
796 commandBuffer.releaseHandle();
797
798 // Reallocate the command pool for next frame.
799 // TODO(jmadill): Consider reusing command pools.
800 VkCommandPoolCreateInfo poolInfo;
801 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
802 poolInfo.pNext = nullptr;
803 poolInfo.flags = 0;
804 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
805
806 mCommandPool.init(mDevice, poolInfo);
807
Jamie Madill4c26fc22017-02-24 11:04:10 -0500808 return vk::NoError();
809}
810
Jamie Madillaaca96e2018-06-12 10:19:48 -0400811GlslangWrapper *RendererVk::getGlslangWrapper() const
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500812{
813 return mGlslangWrapper;
814}
815
Jamie Madill4c26fc22017-02-24 11:04:10 -0500816Serial RendererVk::getCurrentQueueSerial() const
817{
818 return mCurrentQueueSerial;
819}
820
Jamie Madillaaca96e2018-06-12 10:19:48 -0400821bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500822{
823 return serial > mLastCompletedQueueSerial;
824}
825
Jamie Madill9f2a8612017-11-30 12:43:09 -0500826vk::Error RendererVk::getCompatibleRenderPass(const vk::RenderPassDesc &desc,
827 vk::RenderPass **renderPassOut)
828{
829 return mRenderPassCache.getCompatibleRenderPass(mDevice, mCurrentQueueSerial, desc,
830 renderPassOut);
831}
832
Jamie Madillbef918c2017-12-13 13:11:30 -0500833vk::Error RendererVk::getRenderPassWithOps(const vk::RenderPassDesc &desc,
834 const vk::AttachmentOpsArray &ops,
835 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500836{
Jamie Madillbef918c2017-12-13 13:11:30 -0500837 return mRenderPassCache.getRenderPassWithOps(mDevice, mCurrentQueueSerial, desc, ops,
838 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500839}
840
Jamie Madilla5e06072018-05-18 14:36:05 -0400841vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500842{
Jamie Madilla5e06072018-05-18 14:36:05 -0400843 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500844}
845
846vk::Error RendererVk::flushCommandGraph(const gl::Context *context, vk::CommandBuffer *commandBatch)
847{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500848 return mCommandGraph.submitCommands(mDevice, mCurrentQueueSerial, &mRenderPassCache,
849 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500850}
851
852vk::Error RendererVk::flush(const gl::Context *context,
853 const vk::Semaphore &waitSemaphore,
854 const vk::Semaphore &signalSemaphore)
855{
Jamie Madillbea35a62018-07-05 11:54:10 -0400856 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
857 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500858
859 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
860
861 VkSubmitInfo submitInfo;
862 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
863 submitInfo.pNext = nullptr;
864 submitInfo.waitSemaphoreCount = 1;
865 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
866 submitInfo.pWaitDstStageMask = &waitStageMask;
867 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400868 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500869 submitInfo.signalSemaphoreCount = 1;
870 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
871
Jamie Madillbea35a62018-07-05 11:54:10 -0400872 ANGLE_TRY(submitFrame(submitInfo, commandBatch.release()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500873 return vk::NoError();
874}
875
Jamie Madill78feddc2018-04-27 11:45:05 -0400876Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500877{
Jamie Madill78feddc2018-04-27 11:45:05 -0400878 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500879}
880
Jamie Madill9aef3672018-04-27 11:45:06 -0400881vk::Error RendererVk::getAppPipeline(const ProgramVk *programVk,
882 const vk::PipelineDesc &desc,
883 const gl::AttributesMask &activeAttribLocationsMask,
884 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500885{
Jamie Madill33318de2018-05-01 11:22:54 -0400886 ASSERT(programVk->getVertexModuleSerial() ==
887 desc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
888 ASSERT(programVk->getFragmentModuleSerial() ==
889 desc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500890
891 // Pull in a compatible RenderPass.
892 vk::RenderPass *compatibleRenderPass = nullptr;
893 ANGLE_TRY(getCompatibleRenderPass(desc.getRenderPassDesc(), &compatibleRenderPass));
894
Jamie Madill9b168d02018-06-13 13:25:32 -0400895 const vk::PipelineLayout &pipelineLayout = programVk->getPipelineLayout();
896
Jamie Madillc7918ce2018-06-13 13:25:31 -0400897 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, pipelineLayout,
Luc Ferronceb71902018-02-05 15:18:47 -0500898 activeAttribLocationsMask, programVk->getLinkedVertexModule(),
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500899 programVk->getLinkedFragmentModule(), desc, pipelineOut);
900}
901
Jamie Madill9aef3672018-04-27 11:45:06 -0400902vk::Error RendererVk::getInternalPipeline(const vk::ShaderAndSerial &vertexShader,
903 const vk::ShaderAndSerial &fragmentShader,
904 const vk::PipelineLayout &pipelineLayout,
905 const vk::PipelineDesc &pipelineDesc,
906 const gl::AttributesMask &activeAttribLocationsMask,
907 vk::PipelineAndSerial **pipelineOut)
908{
909 ASSERT(vertexShader.queueSerial() ==
910 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
911 ASSERT(fragmentShader.queueSerial() ==
912 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
913
914 // Pull in a compatible RenderPass.
915 vk::RenderPass *compatibleRenderPass = nullptr;
916 ANGLE_TRY(getCompatibleRenderPass(pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
917
918 return mPipelineCache.getPipeline(mDevice, *compatibleRenderPass, pipelineLayout,
919 activeAttribLocationsMask, vertexShader.get(),
920 fragmentShader.get(), pipelineDesc, pipelineOut);
921}
922
Jamie Madill9b168d02018-06-13 13:25:32 -0400923vk::Error RendererVk::getDescriptorSetLayout(
924 const vk::DescriptorSetLayoutDesc &desc,
925 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
926{
927 return mDescriptorSetLayoutCache.getDescriptorSetLayout(mDevice, desc, descriptorSetLayoutOut);
928}
929
930vk::Error RendererVk::getPipelineLayout(
931 const vk::PipelineLayoutDesc &desc,
932 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
933 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
934{
935 return mPipelineLayoutCache.getPipelineLayout(mDevice, desc, descriptorSetLayouts,
936 pipelineLayoutOut);
937}
938
Jamie Madilld47044a2018-04-27 11:45:03 -0400939vk::ShaderLibrary *RendererVk::getShaderLibrary()
940{
941 return &mShaderLibrary;
942}
Luc Ferron90968362018-05-04 08:47:22 -0400943
Jamie Madillaaca96e2018-06-12 10:19:48 -0400944uint32_t GetUniformBufferDescriptorCount()
945{
946 return kUniformBufferDescriptorsPerDescriptorSet;
947}
948
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400949} // namespace rx