blob: 9ac899bcda06b6be360030294b5d2d7e38d41eb5 [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
Jamie Madill30b5d842018-08-31 17:19:12 -0400462 vkGetPhysicalDeviceFeatures(mPhysicalDevice, &mPhysicalDeviceFeatures);
463
Jamie Madill4d0bf552016-12-28 15:45:24 -0500464 // Ensure we can find a graphics queue family.
465 uint32_t queueCount = 0;
466 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
467
Jamie Madill21061022018-07-12 23:56:30 -0400468 ANGLE_VK_CHECK(context, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500469
470 mQueueFamilyProperties.resize(queueCount);
471 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
472 mQueueFamilyProperties.data());
473
474 size_t graphicsQueueFamilyCount = false;
475 uint32_t firstGraphicsQueueFamily = 0;
476 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
477 {
478 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
479 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
480 {
481 ASSERT(queueInfo.queueCount > 0);
482 graphicsQueueFamilyCount++;
483 if (firstGraphicsQueueFamily == 0)
484 {
485 firstGraphicsQueueFamily = familyIndex;
486 }
487 break;
488 }
489 }
490
Jamie Madill21061022018-07-12 23:56:30 -0400491 ANGLE_VK_CHECK(context, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500492
Jamie Madill12222072018-07-11 14:59:48 -0400493 initFeatures();
494
Jamie Madill4d0bf552016-12-28 15:45:24 -0500495 // If only one queue family, go ahead and initialize the device. If there is more than one
496 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
497 if (graphicsQueueFamilyCount == 1)
498 {
Jamie Madill21061022018-07-12 23:56:30 -0400499 ANGLE_TRY(initializeDevice(context, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500500 }
501
Jamie Madill035fd6b2017-10-03 15:43:22 -0400502 // Store the physical device memory properties so we can find the right memory pools.
503 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500504
Jamie Madill06ca6342018-07-12 15:56:53 -0400505 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500506
Jamie Madill6a89d222017-11-02 11:59:51 -0400507 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500508 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
509 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400510
Jamie Madill21061022018-07-12 23:56:30 -0400511 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400512}
513
Jamie Madill21061022018-07-12 23:56:30 -0400514angle::Result RendererVk::initializeDevice(vk::Context *context, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500515{
516 uint32_t deviceLayerCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400517 ANGLE_VK_TRY(context,
518 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500519
520 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
521 if (deviceLayerCount > 0)
522 {
Jamie Madill21061022018-07-12 23:56:30 -0400523 ANGLE_VK_TRY(context, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
524 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500525 }
526
527 uint32_t deviceExtensionCount = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400528 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
529 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500530
531 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
532 if (deviceExtensionCount > 0)
533 {
Jamie Madill21061022018-07-12 23:56:30 -0400534 ANGLE_VK_TRY(context, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
535 &deviceExtensionCount,
536 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500537 }
538
Yuly Novikov199f4292018-01-19 19:04:05 -0500539 const char *const *enabledLayerNames = nullptr;
540 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500541 if (mEnableValidationLayers)
542 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500543 mEnableValidationLayers = GetAvailableValidationLayers(
544 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500545 }
546
547 std::vector<const char *> enabledDeviceExtensions;
548 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
549
Luc Ferronbf6dc372018-06-28 15:24:19 -0400550 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
551 if (getFeatures().flipViewportY)
552 {
553 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
554 }
555
Jamie Madill21061022018-07-12 23:56:30 -0400556 ANGLE_VK_TRY(context, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500557
558 VkDeviceQueueCreateInfo queueCreateInfo;
559
560 float zeroPriority = 0.0f;
561
562 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
563 queueCreateInfo.pNext = nullptr;
564 queueCreateInfo.flags = 0;
565 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
566 queueCreateInfo.queueCount = 1;
567 queueCreateInfo.pQueuePriorities = &zeroPriority;
568
569 // Initialize the device
570 VkDeviceCreateInfo createInfo;
571
Jamie Madill50cf2be2018-06-15 09:46:57 -0400572 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
573 createInfo.pNext = nullptr;
574 createInfo.flags = 0;
575 createInfo.queueCreateInfoCount = 1;
576 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500577 createInfo.enabledLayerCount = enabledLayerCount;
578 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500579 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
580 createInfo.ppEnabledExtensionNames =
581 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
582 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
583
Jamie Madill21061022018-07-12 23:56:30 -0400584 ANGLE_VK_TRY(context, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500585
586 mCurrentQueueFamilyIndex = queueFamilyIndex;
587
588 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
589
590 // Initialize the command pool now that we know the queue family index.
591 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500592 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
593 commandPoolInfo.pNext = nullptr;
594 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500595 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
596
Jamie Madill21061022018-07-12 23:56:30 -0400597 ANGLE_TRY(mCommandPool.init(context, commandPoolInfo));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500598
Jamie Madill21061022018-07-12 23:56:30 -0400599 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500600}
601
Jamie Madill21061022018-07-12 23:56:30 -0400602angle::Result RendererVk::selectPresentQueueForSurface(vk::Context *context,
603 VkSurfaceKHR surface,
604 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500605{
606 // We've already initialized a device, and can't re-create it unless it's never been used.
607 // TODO(jmadill): Handle the re-creation case if necessary.
608 if (mDevice != VK_NULL_HANDLE)
609 {
610 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
611
612 // Check if the current device supports present on this surface.
613 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400614 ANGLE_VK_TRY(context,
615 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500616 surface, &supportsPresent));
617
Jamie Madill6cad7732018-07-11 09:01:17 -0400618 if (supportsPresent == VK_TRUE)
619 {
620 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400621 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400622 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500623 }
624
625 // Find a graphics and present queue.
626 Optional<uint32_t> newPresentQueue;
627 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
628 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
629 {
630 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
631 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
632 {
633 VkBool32 supportsPresent = VK_FALSE;
Jamie Madill21061022018-07-12 23:56:30 -0400634 ANGLE_VK_TRY(context, vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, queueIndex,
635 surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500636
637 if (supportsPresent == VK_TRUE)
638 {
639 newPresentQueue = queueIndex;
640 break;
641 }
642 }
643 }
644
Jamie Madill21061022018-07-12 23:56:30 -0400645 ANGLE_VK_CHECK(context, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
646 ANGLE_TRY(initializeDevice(context, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500647
Jamie Madill6cad7732018-07-11 09:01:17 -0400648 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400649 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500650}
651
652std::string RendererVk::getVendorString() const
653{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300654 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500655}
656
Jamie Madille09bd5d2016-11-29 16:20:35 -0500657std::string RendererVk::getRendererDescription() const
658{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500659 std::stringstream strstr;
660
661 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
662
663 strstr << "Vulkan ";
664 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
665 strstr << VK_VERSION_MINOR(apiVersion) << ".";
666 strstr << VK_VERSION_PATCH(apiVersion);
667
Olli Etuahoc6a06182018-04-13 14:11:46 +0300668 strstr << "(";
669
670 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
671 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
672 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
673 // driver detection.
674 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
675 {
676 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
677 }
678
679 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500680
681 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500682}
683
Jamie Madill12222072018-07-11 14:59:48 -0400684void RendererVk::initFeatures()
685{
686 // Use OpenGL line rasterization rules by default.
687 mFeatures.basicGLLineRasterization = true;
688
Luc Ferronf786b702018-07-10 11:01:43 -0400689 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
690 // investigation. http://anglebug.com/2728
691 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Jamie Madill12222072018-07-11 14:59:48 -0400692}
693
Jamie Madillacccc6c2016-05-03 17:22:10 -0400694void RendererVk::ensureCapsInitialized() const
695{
696 if (!mCapsInitialized)
697 {
Jamie Madill30b5d842018-08-31 17:19:12 -0400698 vk::GenerateCaps(mPhysicalDeviceProperties, mPhysicalDeviceFeatures, mNativeTextureCaps,
699 &mNativeCaps, &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400700 mCapsInitialized = true;
701 }
702}
703
Jamie Madillacccc6c2016-05-03 17:22:10 -0400704const gl::Caps &RendererVk::getNativeCaps() const
705{
706 ensureCapsInitialized();
707 return mNativeCaps;
708}
709
710const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
711{
712 ensureCapsInitialized();
713 return mNativeTextureCaps;
714}
715
716const gl::Extensions &RendererVk::getNativeExtensions() const
717{
718 ensureCapsInitialized();
719 return mNativeExtensions;
720}
721
722const gl::Limitations &RendererVk::getNativeLimitations() const
723{
724 ensureCapsInitialized();
725 return mNativeLimitations;
726}
727
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400728uint32_t RendererVk::getMaxActiveTextures()
729{
730 // TODO(lucferron): expose this limitation to GL in Context Caps
731 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
732 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
733}
734
Jamie Madill49ac74b2017-12-21 14:42:33 -0500735const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500736{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500737 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500738}
739
Jamie Madill21061022018-07-12 23:56:30 -0400740angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500741{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500742 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500743 {
Luc Ferron1617e692018-07-11 11:08:19 -0400744 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
745 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400746
Jamie Madill49ac74b2017-12-21 14:42:33 -0500747 VkSubmitInfo submitInfo;
748 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
749 submitInfo.pNext = nullptr;
750 submitInfo.waitSemaphoreCount = 0;
751 submitInfo.pWaitSemaphores = nullptr;
752 submitInfo.pWaitDstStageMask = nullptr;
753 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400754 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500755 submitInfo.signalSemaphoreCount = 0;
756 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500757
Jamie Madill21061022018-07-12 23:56:30 -0400758 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500759 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500760
Jamie Madill4c26fc22017-02-24 11:04:10 -0500761 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400762 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400763 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400764 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500765}
766
Jamie Madill0c0dc342017-03-24 14:18:51 -0400767void RendererVk::freeAllInFlightResources()
768{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500769 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400770 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500771 batch.fence.destroy(mDevice);
772 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400773 }
774 mInFlightCommands.clear();
775
776 for (auto &garbage : mGarbage)
777 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400778 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400779 }
780 mGarbage.clear();
781}
782
Jamie Madill21061022018-07-12 23:56:30 -0400783angle::Result RendererVk::checkInFlightCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500784{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500785 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500786
Jamie Madill49ac74b2017-12-21 14:42:33 -0500787 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500788 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500789 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400790 if (result == VK_NOT_READY)
791 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500792
Jamie Madill21061022018-07-12 23:56:30 -0400793 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500794 ASSERT(batch.serial > mLastCompletedQueueSerial);
795 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400796
Jamie Madill49ac74b2017-12-21 14:42:33 -0500797 batch.fence.destroy(mDevice);
798 batch.commandPool.destroy(mDevice);
799 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500800 }
801
Jamie Madill49ac74b2017-12-21 14:42:33 -0500802 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400803
804 size_t freeIndex = 0;
805 for (; freeIndex < mGarbage.size(); ++freeIndex)
806 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500807 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400808 break;
809 }
810
811 // Remove the entries from the garbage list - they should be ready to go.
812 if (freeIndex > 0)
813 {
814 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500815 }
816
Jamie Madill21061022018-07-12 23:56:30 -0400817 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500818}
819
Jamie Madill21061022018-07-12 23:56:30 -0400820angle::Result RendererVk::submitFrame(vk::Context *context,
821 const VkSubmitInfo &submitInfo,
822 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500823{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500824 VkFenceCreateInfo fenceInfo;
825 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
826 fenceInfo.pNext = nullptr;
827 fenceInfo.flags = 0;
828
Jamie Madillbea35a62018-07-05 11:54:10 -0400829 vk::Scoped<CommandBatch> scopedBatch(mDevice);
830 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400831 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500832
Jamie Madill21061022018-07-12 23:56:30 -0400833 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500834
835 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500836 batch.commandPool = std::move(mCommandPool);
837 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500838
Jamie Madillbea35a62018-07-05 11:54:10 -0400839 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400840
Omar El Sheikh0359b692018-07-23 17:22:00 -0600841 // Check that mInFlightCommands isn't growing too fast
842 // If it is, wait for the queue to complete work it has alread been assigned
843 if (mInFlightCommands.size() > kInFlightCommandsLimit)
844 {
845 vkQueueWaitIdle(mQueue);
846 }
Jamie Madill0c0dc342017-03-24 14:18:51 -0400847
848 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400849 // TODO(jmadill): Overflow check.
850 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400851
Jamie Madill21061022018-07-12 23:56:30 -0400852 ANGLE_TRY(checkInFlightCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400853
Jamie Madill49ac74b2017-12-21 14:42:33 -0500854 // Simply null out the command buffer here - it was allocated using the command pool.
855 commandBuffer.releaseHandle();
856
857 // Reallocate the command pool for next frame.
858 // TODO(jmadill): Consider reusing command pools.
859 VkCommandPoolCreateInfo poolInfo;
860 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
861 poolInfo.pNext = nullptr;
862 poolInfo.flags = 0;
863 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
864
Jamie Madill21061022018-07-12 23:56:30 -0400865 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500866
Jamie Madill21061022018-07-12 23:56:30 -0400867 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500868}
869
Jamie Madillaaca96e2018-06-12 10:19:48 -0400870bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500871{
872 return serial > mLastCompletedQueueSerial;
873}
874
Jamie Madill21061022018-07-12 23:56:30 -0400875angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
876 const vk::RenderPassDesc &desc,
877 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500878{
Jamie Madill21061022018-07-12 23:56:30 -0400879 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -0500880 renderPassOut);
881}
882
Jamie Madill21061022018-07-12 23:56:30 -0400883angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
884 const vk::RenderPassDesc &desc,
885 const vk::AttachmentOpsArray &ops,
886 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500887{
Jamie Madill21061022018-07-12 23:56:30 -0400888 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -0500889 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500890}
891
Jamie Madilla5e06072018-05-18 14:36:05 -0400892vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500893{
Jamie Madilla5e06072018-05-18 14:36:05 -0400894 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500895}
896
Jamie Madill21061022018-07-12 23:56:30 -0400897angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500898{
Jamie Madill21061022018-07-12 23:56:30 -0400899 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500900 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500901}
902
Jamie Madill21061022018-07-12 23:56:30 -0400903angle::Result RendererVk::flush(vk::Context *context,
904 const vk::Semaphore &waitSemaphore,
905 const vk::Semaphore &signalSemaphore)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500906{
Jamie Madillbea35a62018-07-05 11:54:10 -0400907 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
908 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500909
910 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
911
912 VkSubmitInfo submitInfo;
913 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
914 submitInfo.pNext = nullptr;
915 submitInfo.waitSemaphoreCount = 1;
916 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
917 submitInfo.pWaitDstStageMask = &waitStageMask;
918 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400919 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500920 submitInfo.signalSemaphoreCount = 1;
921 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
922
Jamie Madill21061022018-07-12 23:56:30 -0400923 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
924 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500925}
926
Jamie Madill78feddc2018-04-27 11:45:05 -0400927Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500928{
Jamie Madill78feddc2018-04-27 11:45:05 -0400929 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500930}
931
Jamie Madill06ca6342018-07-12 15:56:53 -0400932angle::Result RendererVk::getPipeline(vk::Context *context,
933 const vk::ShaderAndSerial &vertexShader,
934 const vk::ShaderAndSerial &fragmentShader,
935 const vk::PipelineLayout &pipelineLayout,
936 const vk::PipelineDesc &pipelineDesc,
937 const gl::AttributesMask &activeAttribLocationsMask,
938 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500939{
Jamie Madill06ca6342018-07-12 15:56:53 -0400940 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400941 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -0400942 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400943 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
944
945 // Pull in a compatible RenderPass.
946 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -0400947 ANGLE_TRY(
948 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -0400949
Jamie Madill21061022018-07-12 23:56:30 -0400950 return mPipelineCache.getPipeline(context, *compatibleRenderPass, pipelineLayout,
Jamie Madill9aef3672018-04-27 11:45:06 -0400951 activeAttribLocationsMask, vertexShader.get(),
952 fragmentShader.get(), pipelineDesc, pipelineOut);
953}
954
Jamie Madill21061022018-07-12 23:56:30 -0400955angle::Result RendererVk::getDescriptorSetLayout(
956 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400957 const vk::DescriptorSetLayoutDesc &desc,
958 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
959{
Jamie Madill21061022018-07-12 23:56:30 -0400960 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -0400961}
962
Jamie Madill21061022018-07-12 23:56:30 -0400963angle::Result RendererVk::getPipelineLayout(
964 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -0400965 const vk::PipelineLayoutDesc &desc,
966 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
967 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
968{
Jamie Madill21061022018-07-12 23:56:30 -0400969 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -0400970 pipelineLayoutOut);
971}
972
Jamie Madilld47044a2018-04-27 11:45:03 -0400973vk::ShaderLibrary *RendererVk::getShaderLibrary()
974{
975 return &mShaderLibrary;
976}
Luc Ferron90968362018-05-04 08:47:22 -0400977
Jamie Madillaaca96e2018-06-12 10:19:48 -0400978uint32_t GetUniformBufferDescriptorCount()
979{
980 return kUniformBufferDescriptorsPerDescriptorSet;
981}
982
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400983} // namespace rx