blob: 14d422d6ebd2c5cddbe5bad189c8dbe903fd37e2 [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";
Omar El Sheikh0359b692018-07-23 17:22:00 -060036constexpr size_t kInFlightCommandsLimit = 50000u;
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070037} // anonymous namespace
38
Jamie Madill9e54b5a2016-05-25 12:57:39 -040039namespace rx
40{
41
Jamie Madille09bd5d2016-11-29 16:20:35 -050042namespace
43{
Luc Ferrondaedf4d2018-03-16 09:28:53 -040044// We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and
45// one for the vertex shader.
46constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2;
Jamie Madille09bd5d2016-11-29 16:20:35 -050047
Omar El Sheikh26c61b22018-06-29 12:50:59 -060048bool ShouldEnableMockICD(const egl::AttributeMap &attribs)
49{
50#if !defined(ANGLE_PLATFORM_ANDROID)
51 // Mock ICD does not currently run on Android
52 return (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
53 EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) ==
54 EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE);
55#else
56 return false;
57#endif // !defined(ANGLE_PLATFORM_ANDROID)
58}
59
Jamie Madille09bd5d2016-11-29 16:20:35 -050060VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
61 const std::vector<const char *> &enabledExtensionNames)
62{
63 // Compile the extensions names into a set.
64 std::set<std::string> extensionNames;
65 for (const auto &extensionProp : extensionProps)
66 {
67 extensionNames.insert(extensionProp.extensionName);
68 }
69
Jamie Madillacf2f3a2017-11-21 19:22:44 -050070 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050071 {
72 if (extensionNames.count(extensionName) == 0)
73 {
74 return VK_ERROR_EXTENSION_NOT_PRESENT;
75 }
76 }
77
78 return VK_SUCCESS;
79}
80
Tobin Ehlis3a181e32018-08-29 15:17:05 -060081// Array of Validation error/warning messages that will be ignored, should include bugID
82constexpr std::array<const char *, 1> kSkippedMessages = {
83 // http://anglebug.com/2796
84 " [ UNASSIGNED-CoreValidation-Shader-PointSizeMissing ] Object: VK_NULL_HANDLE (Type = 19) "
85 "| Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader "
86 "corresponding to VK_SHADER_STAGE_VERTEX_BIT."};
87
88// Suppress validation errors that are known
89// return "true" if given code/prefix/message is known, else return "false"
90bool IsIgnoredDebugMessage(const char *message)
91{
92 for (const auto &msg : kSkippedMessages)
93 {
94 if (strcmp(msg, message) == 0)
95 {
96 return true;
97 }
98 }
99 return false;
100}
101
Yuly Novikov199f4292018-01-19 19:04:05 -0500102VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
103 VkDebugReportObjectTypeEXT objectType,
104 uint64_t object,
105 size_t location,
106 int32_t messageCode,
107 const char *layerPrefix,
108 const char *message,
109 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -0500110{
Tobin Ehlis3a181e32018-08-29 15:17:05 -0600111 if (IsIgnoredDebugMessage(message))
112 {
113 return VK_FALSE;
114 }
Jamie Madill0448ec82016-12-23 13:41:47 -0500115 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
116 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500117 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500118#if !defined(NDEBUG)
119 // Abort the call in Debug builds.
120 return VK_TRUE;
121#endif
122 }
123 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
124 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500125 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500126 }
127 else
128 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500129 // Uncomment this if you want Vulkan spam.
130 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500131 }
132
133 return VK_FALSE;
134}
135
Yuly Novikov199f4292018-01-19 19:04:05 -0500136// If we're loading the validation layers, we could be running from any random directory.
137// Change to the executable directory so we can find the layers, then change back to the
138// previous directory to be safe we don't disrupt the application.
139class ScopedVkLoaderEnvironment : angle::NonCopyable
140{
141 public:
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600142 ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD)
143 : mEnableValidationLayers(enableValidationLayers),
144 mEnableMockICD(enableMockICD),
145 mChangedCWD(false),
146 mChangedICDPath(false)
Yuly Novikov199f4292018-01-19 19:04:05 -0500147 {
148// Changing CWD and setting environment variables makes no sense on Android,
149// since this code is a part of Java application there.
150// Android Vulkan loader doesn't need this either.
151#if !defined(ANGLE_PLATFORM_ANDROID)
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600152 if (enableMockICD)
153 {
154 // Override environment variable to use built Mock ICD
155 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
156 mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv);
157 mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON);
158 if (!mChangedICDPath)
159 {
160 ERR() << "Error setting Path for Mock/Null Driver.";
161 mEnableMockICD = false;
162 }
163 }
Jamie Madill46848422018-08-09 10:46:06 -0400164 if (mEnableValidationLayers || mEnableMockICD)
Yuly Novikov199f4292018-01-19 19:04:05 -0500165 {
166 const auto &cwd = angle::GetCWD();
167 if (!cwd.valid())
168 {
169 ERR() << "Error getting CWD for Vulkan layers init.";
170 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400171 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500172 }
173 else
174 {
175 mPreviousCWD = cwd.value();
176 const char *exeDir = angle::GetExecutableDirectory();
177 mChangedCWD = angle::SetCWD(exeDir);
178 if (!mChangedCWD)
179 {
180 ERR() << "Error setting CWD for Vulkan layers init.";
181 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400182 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500183 }
184 }
185 }
186
187 // Override environment variable to use the ANGLE layers.
188 if (mEnableValidationLayers)
189 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700190 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500191 {
192 ERR() << "Error setting environment for Vulkan layers init.";
193 mEnableValidationLayers = false;
194 }
195 }
196#endif // !defined(ANGLE_PLATFORM_ANDROID)
197 }
198
199 ~ScopedVkLoaderEnvironment()
200 {
201 if (mChangedCWD)
202 {
203#if !defined(ANGLE_PLATFORM_ANDROID)
204 ASSERT(mPreviousCWD.valid());
205 angle::SetCWD(mPreviousCWD.value().c_str());
206#endif // !defined(ANGLE_PLATFORM_ANDROID)
207 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600208 if (mChangedICDPath)
209 {
Omar El Sheikh80d4ef12018-07-13 17:08:19 -0600210 if (mPreviousICDPath.value().empty())
211 {
212 angle::UnsetEnvironmentVar(g_VkICDPathEnv);
213 }
214 else
215 {
216 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
217 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600218 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500219 }
220
Jamie Madillaaca96e2018-06-12 10:19:48 -0400221 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500222
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600223 bool canEnableMockICD() const { return mEnableMockICD; }
224
Yuly Novikov199f4292018-01-19 19:04:05 -0500225 private:
226 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600227 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500228 bool mChangedCWD;
229 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600230 bool mChangedICDPath;
231 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500232};
233
Jamie Madill21061022018-07-12 23:56:30 -0400234void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
235 bool preferMockICD,
236 VkPhysicalDevice *physicalDeviceOut,
237 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
238{
239 ASSERT(!physicalDevices.empty());
240 if (preferMockICD)
241 {
242 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
243 {
244 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
245 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
246 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
247 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
248 {
249 *physicalDeviceOut = physicalDevice;
250 return;
251 }
252 }
253 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
254 "physicalDevice instead.";
255 }
256
257 // Fall back to first device.
258 *physicalDeviceOut = physicalDevices[0];
259 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
260}
Jamie Madille09bd5d2016-11-29 16:20:35 -0500261} // anonymous namespace
262
Jamie Madill49ac74b2017-12-21 14:42:33 -0500263// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400264RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500265
Jamie Madillaaca96e2018-06-12 10:19:48 -0400266RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500267
268RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
269 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
270{
271}
272
273RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
274{
275 std::swap(commandPool, other.commandPool);
276 std::swap(fence, other.fence);
277 std::swap(serial, other.serial);
278 return *this;
279}
280
Jamie Madillbea35a62018-07-05 11:54:10 -0400281void RendererVk::CommandBatch::destroy(VkDevice device)
282{
283 commandPool.destroy(device);
284 fence.destroy(device);
285}
286
Jamie Madill9f2a8612017-11-30 12:43:09 -0500287// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500288RendererVk::RendererVk()
289 : mCapsInitialized(false),
290 mInstance(VK_NULL_HANDLE),
291 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500292 mDebugReportCallback(VK_NULL_HANDLE),
293 mPhysicalDevice(VK_NULL_HANDLE),
294 mQueue(VK_NULL_HANDLE),
295 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
296 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400297 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Jamie Madillaaca96e2018-06-12 10:19:48 -0400298 mCurrentQueueSerial(mQueueSerialFactory.generate())
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400299{
300}
301
302RendererVk::~RendererVk()
303{
Jamie Madill21061022018-07-12 23:56:30 -0400304}
305
306void RendererVk::onDestroy(vk::Context *context)
307{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500308 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500309 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500310 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400311 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500312 }
313
Jamie Madillc7918ce2018-06-13 13:25:31 -0400314 mPipelineLayoutCache.destroy(mDevice);
315 mDescriptorSetLayoutCache.destroy(mDevice);
316
Jamie Madill9f2a8612017-11-30 12:43:09 -0500317 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500318 mPipelineCache.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400319 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500320
Jamie Madill06ca6342018-07-12 15:56:53 -0400321 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500322
Jamie Madill5deea722017-02-16 10:44:46 -0500323 if (mCommandPool.valid())
324 {
325 mCommandPool.destroy(mDevice);
326 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500327
328 if (mDevice)
329 {
330 vkDestroyDevice(mDevice, nullptr);
331 mDevice = VK_NULL_HANDLE;
332 }
333
Jamie Madill0448ec82016-12-23 13:41:47 -0500334 if (mDebugReportCallback)
335 {
336 ASSERT(mInstance);
337 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
338 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
339 ASSERT(destroyDebugReportCallback);
340 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
341 }
342
Jamie Madill4d0bf552016-12-28 15:45:24 -0500343 if (mInstance)
344 {
345 vkDestroyInstance(mInstance, nullptr);
346 mInstance = VK_NULL_HANDLE;
347 }
348
Omar El Sheikheb4b8692018-07-17 10:55:40 -0600349 mMemoryProperties.destroy();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500350 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500351}
352
Jamie Madill21061022018-07-12 23:56:30 -0400353angle::Result RendererVk::initialize(vk::Context *context,
354 const egl::AttributeMap &attribs,
355 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500356{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600357 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
358 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500359 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600360 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500361
Jamie Madill0448ec82016-12-23 13:41:47 -0500362 // Gather global layer properties.
363 uint32_t instanceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400364 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500365
366 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
367 if (instanceLayerCount > 0)
368 {
Jamie Madill21061022018-07-12 23:56:30 -0400369 ANGLE_VK_TRY(context, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
370 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500371 }
372
Jamie Madille09bd5d2016-11-29 16:20:35 -0500373 uint32_t instanceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400374 ANGLE_VK_TRY(context,
375 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500376
377 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
378 if (instanceExtensionCount > 0)
379 {
Jamie Madill21061022018-07-12 23:56:30 -0400380 ANGLE_VK_TRY(context, vkEnumerateInstanceExtensionProperties(
381 nullptr, &instanceExtensionCount, instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500382 }
383
Yuly Novikov199f4292018-01-19 19:04:05 -0500384 const char *const *enabledLayerNames = nullptr;
385 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500386 if (mEnableValidationLayers)
387 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500388 bool layersRequested =
389 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
390 mEnableValidationLayers = GetAvailableValidationLayers(
391 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500392 }
393
Jamie Madille09bd5d2016-11-29 16:20:35 -0500394 std::vector<const char *> enabledInstanceExtensions;
395 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500396 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500397
Jamie Madill0448ec82016-12-23 13:41:47 -0500398 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
399 if (mEnableValidationLayers)
400 {
401 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
402 }
403
Jamie Madille09bd5d2016-11-29 16:20:35 -0500404 // Verify the required extensions are in the extension names set. Fail if not.
Jamie Madill21061022018-07-12 23:56:30 -0400405 ANGLE_VK_TRY(context,
406 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500407
Jamie Madill327ba852016-11-30 12:38:28 -0500408 VkApplicationInfo applicationInfo;
409 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
410 applicationInfo.pNext = nullptr;
411 applicationInfo.pApplicationName = "ANGLE";
412 applicationInfo.applicationVersion = 1;
413 applicationInfo.pEngineName = "ANGLE";
414 applicationInfo.engineVersion = 1;
415 applicationInfo.apiVersion = VK_API_VERSION_1_0;
416
417 VkInstanceCreateInfo instanceInfo;
418 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
419 instanceInfo.pNext = nullptr;
420 instanceInfo.flags = 0;
421 instanceInfo.pApplicationInfo = &applicationInfo;
422
Jamie Madille09bd5d2016-11-29 16:20:35 -0500423 // Enable requested layers and extensions.
424 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
425 instanceInfo.ppEnabledExtensionNames =
426 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500427 instanceInfo.enabledLayerCount = enabledLayerCount;
428 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500429
Jamie Madill21061022018-07-12 23:56:30 -0400430 ANGLE_VK_TRY(context, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500431
Jamie Madill0448ec82016-12-23 13:41:47 -0500432 if (mEnableValidationLayers)
433 {
434 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
435
436 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
437 debugReportInfo.pNext = nullptr;
438 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
439 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
440 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
441 debugReportInfo.pfnCallback = &DebugReportCallback;
442 debugReportInfo.pUserData = this;
443
444 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
445 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
446 ASSERT(createDebugReportCallback);
Jamie Madill21061022018-07-12 23:56:30 -0400447 ANGLE_VK_TRY(context, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
448 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500449 }
450
Jamie Madill4d0bf552016-12-28 15:45:24 -0500451 uint32_t physicalDeviceCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400452 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
453 ANGLE_VK_CHECK(context, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500454
455 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700456 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Jamie Madill21061022018-07-12 23:56:30 -0400457 ANGLE_VK_TRY(context, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
458 physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600459 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700460 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500461
462 // Ensure we can find a graphics queue family.
463 uint32_t queueCount = 0;
464 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
465
Jamie Madill21061022018-07-12 23:56:30 -0400466 ANGLE_VK_CHECK(context, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500467
468 mQueueFamilyProperties.resize(queueCount);
469 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
470 mQueueFamilyProperties.data());
471
472 size_t graphicsQueueFamilyCount = false;
473 uint32_t firstGraphicsQueueFamily = 0;
474 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
475 {
476 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
477 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
478 {
479 ASSERT(queueInfo.queueCount > 0);
480 graphicsQueueFamilyCount++;
481 if (firstGraphicsQueueFamily == 0)
482 {
483 firstGraphicsQueueFamily = familyIndex;
484 }
485 break;
486 }
487 }
488
Jamie Madill21061022018-07-12 23:56:30 -0400489 ANGLE_VK_CHECK(context, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500490
Jamie Madill12222072018-07-11 14:59:48 -0400491 initFeatures();
492
Jamie Madill4d0bf552016-12-28 15:45:24 -0500493 // If only one queue family, go ahead and initialize the device. If there is more than one
494 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
495 if (graphicsQueueFamilyCount == 1)
496 {
Jamie Madill21061022018-07-12 23:56:30 -0400497 ANGLE_TRY(initializeDevice(context, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500498 }
499
Jamie Madill035fd6b2017-10-03 15:43:22 -0400500 // Store the physical device memory properties so we can find the right memory pools.
501 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500502
Jamie Madill06ca6342018-07-12 15:56:53 -0400503 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500504
Jamie Madill6a89d222017-11-02 11:59:51 -0400505 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500506 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
507 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400508
Jamie Madill21061022018-07-12 23:56:30 -0400509 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400510}
511
Jamie Madill21061022018-07-12 23:56:30 -0400512angle::Result RendererVk::initializeDevice(vk::Context *context, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500513{
514 uint32_t deviceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400515 ANGLE_VK_TRY(context,
516 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500517
518 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
519 if (deviceLayerCount > 0)
520 {
Jamie Madill21061022018-07-12 23:56:30 -0400521 ANGLE_VK_TRY(context, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
522 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500523 }
524
525 uint32_t deviceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400526 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
527 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500528
529 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
530 if (deviceExtensionCount > 0)
531 {
Jamie Madill21061022018-07-12 23:56:30 -0400532 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
533 &deviceExtensionCount,
534 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500535 }
536
Yuly Novikov199f4292018-01-19 19:04:05 -0500537 const char *const *enabledLayerNames = nullptr;
538 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500539 if (mEnableValidationLayers)
540 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500541 mEnableValidationLayers = GetAvailableValidationLayers(
542 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500543 }
544
545 std::vector<const char *> enabledDeviceExtensions;
546 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
547
Luc Ferronbf6dc372018-06-28 15:24:19 -0400548 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
549 if (getFeatures().flipViewportY)
550 {
551 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
552 }
553
Jamie Madill21061022018-07-12 23:56:30 -0400554 ANGLE_VK_TRY(context, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500555
556 VkDeviceQueueCreateInfo queueCreateInfo;
557
558 float zeroPriority = 0.0f;
559
560 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
561 queueCreateInfo.pNext = nullptr;
562 queueCreateInfo.flags = 0;
563 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
564 queueCreateInfo.queueCount = 1;
565 queueCreateInfo.pQueuePriorities = &zeroPriority;
566
567 // Initialize the device
568 VkDeviceCreateInfo createInfo;
569
Jamie Madill50cf2be2018-06-15 09:46:57 -0400570 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
571 createInfo.pNext = nullptr;
572 createInfo.flags = 0;
573 createInfo.queueCreateInfoCount = 1;
574 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500575 createInfo.enabledLayerCount = enabledLayerCount;
576 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500577 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
578 createInfo.ppEnabledExtensionNames =
579 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
580 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
581
Jamie Madill21061022018-07-12 23:56:30 -0400582 ANGLE_VK_TRY(context, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500583
584 mCurrentQueueFamilyIndex = queueFamilyIndex;
585
586 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
587
588 // Initialize the command pool now that we know the queue family index.
589 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500590 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
591 commandPoolInfo.pNext = nullptr;
592 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500593 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
594
Jamie Madill21061022018-07-12 23:56:30 -0400595 ANGLE_TRY(mCommandPool.init(context, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500596
Jamie Madill21061022018-07-12 23:56:30 -0400597 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500598}
599
Jamie Madill21061022018-07-12 23:56:30 -0400600angle::Result RendererVk::selectPresentQueueForSurface(vk::Context *context,
601 VkSurfaceKHR surface,
602 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500603{
604 // We've already initialized a device, and can't re-create it unless it's never been used.
605 // TODO(jmadill): Handle the re-creation case if necessary.
606 if (mDevice != VK_NULL_HANDLE)
607 {
608 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
609
610 // Check if the current device supports present on this surface.
611 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400612 ANGLE_VK_TRY(context,
613 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500614 surface, &supportsPresent));
615
Jamie Madill6cad7732018-07-11 09:01:17 -0400616 if (supportsPresent == VK_TRUE)
617 {
618 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400619 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400620 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500621 }
622
623 // Find a graphics and present queue.
624 Optional<uint32_t> newPresentQueue;
625 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
626 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
627 {
628 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
629 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
630 {
631 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400632 ANGLE_VK_TRY(context, vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex,
633 surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500634
635 if (supportsPresent == VK_TRUE)
636 {
637 newPresentQueue = queueIndex;
638 break;
639 }
640 }
641 }
642
Jamie Madill21061022018-07-12 23:56:30 -0400643 ANGLE_VK_CHECK(context, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
644 ANGLE_TRY(initializeDevice(context, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500645
Jamie Madill6cad7732018-07-11 09:01:17 -0400646 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400647 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500648}
649
650std::string RendererVk::getVendorString() const
651{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300652 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500653}
654
Jamie Madille09bd5d2016-11-29 16:20:35 -0500655std::string RendererVk::getRendererDescription() const
656{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500657 std::stringstream strstr;
658
659 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
660
661 strstr << "Vulkan ";
662 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
663 strstr << VK_VERSION_MINOR(apiVersion) << ".";
664 strstr << VK_VERSION_PATCH(apiVersion);
665
Olli Etuahoc6a06182018-04-13 14:11:46 +0300666 strstr << "(";
667
668 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
669 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
670 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
671 // driver detection.
672 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
673 {
674 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
675 }
676
677 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500678
679 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500680}
681
Jamie Madill12222072018-07-11 14:59:48 -0400682void RendererVk::initFeatures()
683{
684 // Use OpenGL line rasterization rules by default.
685 mFeatures.basicGLLineRasterization = true;
686
Luc Ferronf786b702018-07-10 11:01:43 -0400687 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
688 // investigation. http://anglebug.com/2728
689 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Jamie Madill12222072018-07-11 14:59:48 -0400690}
691
Jamie Madillacccc6c2016-05-03 17:22:10 -0400692void RendererVk::ensureCapsInitialized() const
693{
694 if (!mCapsInitialized)
695 {
Luc Ferrond50537a2018-02-07 17:02:08 -0500696 vk::GenerateCaps(mPhysicalDeviceProperties, mNativeTextureCaps, &mNativeCaps,
Jamie Madill12222072018-07-11 14:59:48 -0400697 &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400698 mCapsInitialized = true;
699 }
700}
701
Jamie Madillacccc6c2016-05-03 17:22:10 -0400702const gl::Caps &RendererVk::getNativeCaps() const
703{
704 ensureCapsInitialized();
705 return mNativeCaps;
706}
707
708const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
709{
710 ensureCapsInitialized();
711 return mNativeTextureCaps;
712}
713
714const gl::Extensions &RendererVk::getNativeExtensions() const
715{
716 ensureCapsInitialized();
717 return mNativeExtensions;
718}
719
720const gl::Limitations &RendererVk::getNativeLimitations() const
721{
722 ensureCapsInitialized();
723 return mNativeLimitations;
724}
725
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400726uint32_t RendererVk::getMaxActiveTextures()
727{
728 // TODO(lucferron): expose this limitation to GL in Context Caps
729 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
730 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
731}
732
Jamie Madill49ac74b2017-12-21 14:42:33 -0500733const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500734{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500735 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500736}
737
Jamie Madill21061022018-07-12 23:56:30 -0400738angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500739{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500740 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500741 {
Luc Ferron1617e692018-07-11 11:08:19 -0400742 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
743 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400744
Jamie Madill49ac74b2017-12-21 14:42:33 -0500745 VkSubmitInfo submitInfo;
746 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
747 submitInfo.pNext = nullptr;
748 submitInfo.waitSemaphoreCount = 0;
749 submitInfo.pWaitSemaphores = nullptr;
750 submitInfo.pWaitDstStageMask = nullptr;
751 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400752 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500753 submitInfo.signalSemaphoreCount = 0;
754 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500755
Jamie Madill21061022018-07-12 23:56:30 -0400756 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500757 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500758
Jamie Madill4c26fc22017-02-24 11:04:10 -0500759 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400760 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400761 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400762 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500763}
764
Jamie Madill0c0dc342017-03-24 14:18:51 -0400765void RendererVk::freeAllInFlightResources()
766{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500767 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400768 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500769 batch.fence.destroy(mDevice);
770 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400771 }
772 mInFlightCommands.clear();
773
774 for (auto &garbage : mGarbage)
775 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400776 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400777 }
778 mGarbage.clear();
779}
780
Jamie Madill21061022018-07-12 23:56:30 -0400781angle::Result RendererVk::checkInFlightCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500782{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500783 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500784
Jamie Madill49ac74b2017-12-21 14:42:33 -0500785 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500786 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500787 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400788 if (result == VK_NOT_READY)
789 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500790
Jamie Madill21061022018-07-12 23:56:30 -0400791 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500792 ASSERT(batch.serial > mLastCompletedQueueSerial);
793 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400794
Jamie Madill49ac74b2017-12-21 14:42:33 -0500795 batch.fence.destroy(mDevice);
796 batch.commandPool.destroy(mDevice);
797 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500798 }
799
Jamie Madill49ac74b2017-12-21 14:42:33 -0500800 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400801
802 size_t freeIndex = 0;
803 for (; freeIndex < mGarbage.size(); ++freeIndex)
804 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500805 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400806 break;
807 }
808
809 // Remove the entries from the garbage list - they should be ready to go.
810 if (freeIndex > 0)
811 {
812 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500813 }
814
Jamie Madill21061022018-07-12 23:56:30 -0400815 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500816}
817
Jamie Madill21061022018-07-12 23:56:30 -0400818angle::Result RendererVk::submitFrame(vk::Context *context,
819 const VkSubmitInfo &submitInfo,
820 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500821{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500822 VkFenceCreateInfo fenceInfo;
823 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
824 fenceInfo.pNext = nullptr;
825 fenceInfo.flags = 0;
826
Jamie Madillbea35a62018-07-05 11:54:10 -0400827 vk::Scoped<CommandBatch> scopedBatch(mDevice);
828 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400829 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500830
Jamie Madill21061022018-07-12 23:56:30 -0400831 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500832
833 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500834 batch.commandPool = std::move(mCommandPool);
835 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500836
Jamie Madillbea35a62018-07-05 11:54:10 -0400837 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400838
Omar El Sheikh0359b692018-07-23 17:22:00 -0600839 // Check that mInFlightCommands isn't growing too fast
840 // If it is, wait for the queue to complete work it has alread been assigned
841 if (mInFlightCommands.size() > kInFlightCommandsLimit)
842 {
843 vkQueueWaitIdle(mQueue);
844 }
Jamie Madill0c0dc342017-03-24 14:18:51 -0400845
846 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400847 // TODO(jmadill): Overflow check.
848 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400849
Jamie Madill21061022018-07-12 23:56:30 -0400850 ANGLE_TRY(checkInFlightCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400851
Jamie Madill49ac74b2017-12-21 14:42:33 -0500852 // Simply null out the command buffer here - it was allocated using the command pool.
853 commandBuffer.releaseHandle();
854
855 // Reallocate the command pool for next frame.
856 // TODO(jmadill): Consider reusing command pools.
857 VkCommandPoolCreateInfo poolInfo;
858 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
859 poolInfo.pNext = nullptr;
860 poolInfo.flags = 0;
861 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
862
Jamie Madill21061022018-07-12 23:56:30 -0400863 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500864
Jamie Madill21061022018-07-12 23:56:30 -0400865 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500866}
867
Jamie Madill4c26fc22017-02-24 11:04:10 -0500868Serial RendererVk::getCurrentQueueSerial() const
869{
870 return mCurrentQueueSerial;
871}
872
Jamie Madillaaca96e2018-06-12 10:19:48 -0400873bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500874{
875 return serial > mLastCompletedQueueSerial;
876}
877
Jamie Madill21061022018-07-12 23:56:30 -0400878angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
879 const vk::RenderPassDesc &desc,
880 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500881{
Jamie Madill21061022018-07-12 23:56:30 -0400882 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -0500883 renderPassOut);
884}
885
Jamie Madill21061022018-07-12 23:56:30 -0400886angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
887 const vk::RenderPassDesc &desc,
888 const vk::AttachmentOpsArray &ops,
889 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500890{
Jamie Madill21061022018-07-12 23:56:30 -0400891 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -0500892 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500893}
894
Jamie Madilla5e06072018-05-18 14:36:05 -0400895vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500896{
Jamie Madilla5e06072018-05-18 14:36:05 -0400897 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500898}
899
Jamie Madill21061022018-07-12 23:56:30 -0400900angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500901{
Jamie Madill21061022018-07-12 23:56:30 -0400902 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500903 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500904}
905
Jamie Madill21061022018-07-12 23:56:30 -0400906angle::Result RendererVk::flush(vk::Context *context,
907 const vk::Semaphore &waitSemaphore,
908 const vk::Semaphore &signalSemaphore)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500909{
Jamie Madillbea35a62018-07-05 11:54:10 -0400910 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
911 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500912
913 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
914
915 VkSubmitInfo submitInfo;
916 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
917 submitInfo.pNext = nullptr;
918 submitInfo.waitSemaphoreCount = 1;
919 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
920 submitInfo.pWaitDstStageMask = &waitStageMask;
921 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400922 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500923 submitInfo.signalSemaphoreCount = 1;
924 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
925
Jamie Madill21061022018-07-12 23:56:30 -0400926 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
927 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500928}
929
Jamie Madill78feddc2018-04-27 11:45:05 -0400930Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500931{
Jamie Madill78feddc2018-04-27 11:45:05 -0400932 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500933}
934
Jamie Madill06ca6342018-07-12 15:56:53 -0400935angle::Result RendererVk::getPipeline(vk::Context *context,
936 const vk::ShaderAndSerial &vertexShader,
937 const vk::ShaderAndSerial &fragmentShader,
938 const vk::PipelineLayout &pipelineLayout,
939 const vk::PipelineDesc &pipelineDesc,
940 const gl::AttributesMask &activeAttribLocationsMask,
941 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500942{
Jamie Madill06ca6342018-07-12 15:56:53 -0400943 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400944 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -0400945 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400946 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
947
948 // Pull in a compatible RenderPass.
949 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400950 ANGLE_TRY(
951 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -0400952
Jamie Madill21061022018-07-12 23:56:30 -0400953 return mPipelineCache.getPipeline(context, *compatibleRenderPass, pipelineLayout,
Jamie Madill9aef3672018-04-27 11:45:06 -0400954 activeAttribLocationsMask, vertexShader.get(),
955 fragmentShader.get(), pipelineDesc, pipelineOut);
956}
957
Jamie Madill21061022018-07-12 23:56:30 -0400958angle::Result RendererVk::getDescriptorSetLayout(
959 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400960 const vk::DescriptorSetLayoutDesc &desc,
961 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
962{
Jamie Madill21061022018-07-12 23:56:30 -0400963 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -0400964}
965
Jamie Madill21061022018-07-12 23:56:30 -0400966angle::Result RendererVk::getPipelineLayout(
967 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400968 const vk::PipelineLayoutDesc &desc,
969 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
970 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
971{
Jamie Madill21061022018-07-12 23:56:30 -0400972 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -0400973 pipelineLayoutOut);
974}
975
Jamie Madilld47044a2018-04-27 11:45:03 -0400976vk::ShaderLibrary *RendererVk::getShaderLibrary()
977{
978 return &mShaderLibrary;
979}
Luc Ferron90968362018-05-04 08:47:22 -0400980
Jamie Madillaaca96e2018-06-12 10:19:48 -0400981uint32_t GetUniformBufferDescriptorCount()
982{
983 return kUniformBufferDescriptorsPerDescriptorSet;
984}
985
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400986} // namespace rx