blob: cd474fd9955b800a716efd9f47546eb372b49090 [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"
Shahbaz Youssefi996628a2018-09-24 16:39:26 -040022#include "libANGLE/renderer/vulkan/DisplayVk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050023#include "libANGLE/renderer/vulkan/FramebufferVk.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050024#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
Jamie Madillffa4cbb2018-01-23 13:04:07 -050025#include "libANGLE/renderer/vulkan/ProgramVk.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050026#include "libANGLE/renderer/vulkan/VertexArrayVk.h"
Luc Ferrone4741fd2018-01-25 13:25:27 -050027#include "libANGLE/renderer/vulkan/vk_caps_utils.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050028#include "libANGLE/renderer/vulkan/vk_format_utils.h"
Jamie Madille09bd5d2016-11-29 16:20:35 -050029#include "platform/Platform.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040030
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070031// Consts
32namespace
33{
34const uint32_t kMockVendorID = 0xba5eba11;
35const uint32_t kMockDeviceID = 0xf005ba11;
36constexpr char kMockDeviceName[] = "Vulkan Mock Device";
Omar El Sheikh0359b692018-07-23 17:22:00 -060037constexpr size_t kInFlightCommandsLimit = 50000u;
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070038} // anonymous namespace
39
Jamie Madill9e54b5a2016-05-25 12:57:39 -040040namespace rx
41{
42
Jamie Madille09bd5d2016-11-29 16:20:35 -050043namespace
44{
Luc Ferrondaedf4d2018-03-16 09:28:53 -040045// We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and
46// one for the vertex shader.
47constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -040048// Update the pipeline cache every this many swaps (if 60fps, this means every 10 minutes)
49static constexpr uint32_t kPipelineCacheVkUpdatePeriod = 10 * 60 * 60;
Jamie Madille09bd5d2016-11-29 16:20:35 -050050
Omar El Sheikh26c61b22018-06-29 12:50:59 -060051bool ShouldEnableMockICD(const egl::AttributeMap &attribs)
52{
53#if !defined(ANGLE_PLATFORM_ANDROID)
54 // Mock ICD does not currently run on Android
55 return (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
56 EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) ==
57 EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE);
58#else
59 return false;
60#endif // !defined(ANGLE_PLATFORM_ANDROID)
61}
62
Jamie Madille09bd5d2016-11-29 16:20:35 -050063VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
64 const std::vector<const char *> &enabledExtensionNames)
65{
66 // Compile the extensions names into a set.
67 std::set<std::string> extensionNames;
68 for (const auto &extensionProp : extensionProps)
69 {
70 extensionNames.insert(extensionProp.extensionName);
71 }
72
Jamie Madillacf2f3a2017-11-21 19:22:44 -050073 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050074 {
75 if (extensionNames.count(extensionName) == 0)
76 {
77 return VK_ERROR_EXTENSION_NOT_PRESENT;
78 }
79 }
80
81 return VK_SUCCESS;
82}
83
Tobin Ehlis3a181e32018-08-29 15:17:05 -060084// Array of Validation error/warning messages that will be ignored, should include bugID
85constexpr std::array<const char *, 1> kSkippedMessages = {
86 // http://anglebug.com/2796
87 " [ UNASSIGNED-CoreValidation-Shader-PointSizeMissing ] Object: VK_NULL_HANDLE (Type = 19) "
88 "| Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader "
89 "corresponding to VK_SHADER_STAGE_VERTEX_BIT."};
90
91// Suppress validation errors that are known
92// return "true" if given code/prefix/message is known, else return "false"
93bool IsIgnoredDebugMessage(const char *message)
94{
95 for (const auto &msg : kSkippedMessages)
96 {
97 if (strcmp(msg, message) == 0)
98 {
99 return true;
100 }
101 }
102 return false;
103}
104
Yuly Novikov199f4292018-01-19 19:04:05 -0500105VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
106 VkDebugReportObjectTypeEXT objectType,
107 uint64_t object,
108 size_t location,
109 int32_t messageCode,
110 const char *layerPrefix,
111 const char *message,
112 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -0500113{
Tobin Ehlis3a181e32018-08-29 15:17:05 -0600114 if (IsIgnoredDebugMessage(message))
115 {
116 return VK_FALSE;
117 }
Jamie Madill0448ec82016-12-23 13:41:47 -0500118 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
119 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500120 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500121#if !defined(NDEBUG)
122 // Abort the call in Debug builds.
123 return VK_TRUE;
124#endif
125 }
126 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
127 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500128 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500129 }
130 else
131 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500132 // Uncomment this if you want Vulkan spam.
133 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500134 }
135
136 return VK_FALSE;
137}
138
Yuly Novikov199f4292018-01-19 19:04:05 -0500139// If we're loading the validation layers, we could be running from any random directory.
140// Change to the executable directory so we can find the layers, then change back to the
141// previous directory to be safe we don't disrupt the application.
142class ScopedVkLoaderEnvironment : angle::NonCopyable
143{
144 public:
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600145 ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD)
146 : mEnableValidationLayers(enableValidationLayers),
147 mEnableMockICD(enableMockICD),
148 mChangedCWD(false),
149 mChangedICDPath(false)
Yuly Novikov199f4292018-01-19 19:04:05 -0500150 {
151// Changing CWD and setting environment variables makes no sense on Android,
152// since this code is a part of Java application there.
153// Android Vulkan loader doesn't need this either.
154#if !defined(ANGLE_PLATFORM_ANDROID)
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600155 if (enableMockICD)
156 {
157 // Override environment variable to use built Mock ICD
158 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
159 mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv);
160 mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON);
161 if (!mChangedICDPath)
162 {
163 ERR() << "Error setting Path for Mock/Null Driver.";
164 mEnableMockICD = false;
165 }
166 }
Jamie Madill46848422018-08-09 10:46:06 -0400167 if (mEnableValidationLayers || mEnableMockICD)
Yuly Novikov199f4292018-01-19 19:04:05 -0500168 {
169 const auto &cwd = angle::GetCWD();
170 if (!cwd.valid())
171 {
172 ERR() << "Error getting CWD for Vulkan layers init.";
173 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400174 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500175 }
176 else
177 {
178 mPreviousCWD = cwd.value();
179 const char *exeDir = angle::GetExecutableDirectory();
180 mChangedCWD = angle::SetCWD(exeDir);
181 if (!mChangedCWD)
182 {
183 ERR() << "Error setting CWD for Vulkan layers init.";
184 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400185 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500186 }
187 }
188 }
189
190 // Override environment variable to use the ANGLE layers.
191 if (mEnableValidationLayers)
192 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700193 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500194 {
195 ERR() << "Error setting environment for Vulkan layers init.";
196 mEnableValidationLayers = false;
197 }
198 }
199#endif // !defined(ANGLE_PLATFORM_ANDROID)
200 }
201
202 ~ScopedVkLoaderEnvironment()
203 {
204 if (mChangedCWD)
205 {
206#if !defined(ANGLE_PLATFORM_ANDROID)
207 ASSERT(mPreviousCWD.valid());
208 angle::SetCWD(mPreviousCWD.value().c_str());
209#endif // !defined(ANGLE_PLATFORM_ANDROID)
210 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600211 if (mChangedICDPath)
212 {
Omar El Sheikh80d4ef12018-07-13 17:08:19 -0600213 if (mPreviousICDPath.value().empty())
214 {
215 angle::UnsetEnvironmentVar(g_VkICDPathEnv);
216 }
217 else
218 {
219 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
220 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600221 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500222 }
223
Jamie Madillaaca96e2018-06-12 10:19:48 -0400224 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500225
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600226 bool canEnableMockICD() const { return mEnableMockICD; }
227
Yuly Novikov199f4292018-01-19 19:04:05 -0500228 private:
229 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600230 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500231 bool mChangedCWD;
232 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600233 bool mChangedICDPath;
234 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500235};
236
Jamie Madill21061022018-07-12 23:56:30 -0400237void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
238 bool preferMockICD,
239 VkPhysicalDevice *physicalDeviceOut,
240 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
241{
242 ASSERT(!physicalDevices.empty());
243 if (preferMockICD)
244 {
245 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
246 {
247 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
248 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
249 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
250 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
251 {
252 *physicalDeviceOut = physicalDevice;
253 return;
254 }
255 }
256 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
257 "physicalDevice instead.";
258 }
259
260 // Fall back to first device.
261 *physicalDeviceOut = physicalDevices[0];
262 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
263}
Jamie Madille09bd5d2016-11-29 16:20:35 -0500264} // anonymous namespace
265
Jamie Madill49ac74b2017-12-21 14:42:33 -0500266// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400267RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500268
Jamie Madillaaca96e2018-06-12 10:19:48 -0400269RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500270
271RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
272 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
273{
274}
275
276RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
277{
278 std::swap(commandPool, other.commandPool);
279 std::swap(fence, other.fence);
280 std::swap(serial, other.serial);
281 return *this;
282}
283
Jamie Madillbea35a62018-07-05 11:54:10 -0400284void RendererVk::CommandBatch::destroy(VkDevice device)
285{
286 commandPool.destroy(device);
287 fence.destroy(device);
288}
289
Jamie Madill9f2a8612017-11-30 12:43:09 -0500290// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500291RendererVk::RendererVk()
292 : mCapsInitialized(false),
293 mInstance(VK_NULL_HANDLE),
294 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500295 mDebugReportCallback(VK_NULL_HANDLE),
296 mPhysicalDevice(VK_NULL_HANDLE),
297 mQueue(VK_NULL_HANDLE),
298 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
299 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400300 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400301 mCurrentQueueSerial(mQueueSerialFactory.generate()),
302 mPipelineCacheVkUpdateTimeout(kPipelineCacheVkUpdatePeriod)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400303{
304}
305
306RendererVk::~RendererVk()
307{
Jamie Madill21061022018-07-12 23:56:30 -0400308}
309
310void RendererVk::onDestroy(vk::Context *context)
311{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500312 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500313 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500314 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400315 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500316 }
317
Jamie Madillc7918ce2018-06-13 13:25:31 -0400318 mPipelineLayoutCache.destroy(mDevice);
319 mDescriptorSetLayoutCache.destroy(mDevice);
320
Jamie Madill9f2a8612017-11-30 12:43:09 -0500321 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500322 mPipelineCache.destroy(mDevice);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400323 mPipelineCacheVk.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400324 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500325
Jamie Madill06ca6342018-07-12 15:56:53 -0400326 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500327
Jamie Madill5deea722017-02-16 10:44:46 -0500328 if (mCommandPool.valid())
329 {
330 mCommandPool.destroy(mDevice);
331 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500332
333 if (mDevice)
334 {
335 vkDestroyDevice(mDevice, nullptr);
336 mDevice = VK_NULL_HANDLE;
337 }
338
Jamie Madill0448ec82016-12-23 13:41:47 -0500339 if (mDebugReportCallback)
340 {
341 ASSERT(mInstance);
342 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
343 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
344 ASSERT(destroyDebugReportCallback);
345 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
346 }
347
Jamie Madill4d0bf552016-12-28 15:45:24 -0500348 if (mInstance)
349 {
350 vkDestroyInstance(mInstance, nullptr);
351 mInstance = VK_NULL_HANDLE;
352 }
353
Omar El Sheikheb4b8692018-07-17 10:55:40 -0600354 mMemoryProperties.destroy();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500355 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500356}
357
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400358angle::Result RendererVk::initialize(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400359 const egl::AttributeMap &attribs,
360 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500361{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600362 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
363 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500364 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600365 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500366
Jamie Madill0448ec82016-12-23 13:41:47 -0500367 // Gather global layer properties.
368 uint32_t instanceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400369 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500370
371 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
372 if (instanceLayerCount > 0)
373 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400374 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
375 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500376 }
377
Jamie Madille09bd5d2016-11-29 16:20:35 -0500378 uint32_t instanceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400379 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400380 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500381
382 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
383 if (instanceExtensionCount > 0)
384 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400385 ANGLE_VK_TRY(displayVk,
386 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
387 instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500388 }
389
Yuly Novikov199f4292018-01-19 19:04:05 -0500390 const char *const *enabledLayerNames = nullptr;
391 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500392 if (mEnableValidationLayers)
393 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500394 bool layersRequested =
395 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
396 mEnableValidationLayers = GetAvailableValidationLayers(
397 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500398 }
399
Jamie Madille09bd5d2016-11-29 16:20:35 -0500400 std::vector<const char *> enabledInstanceExtensions;
401 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500402 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500403
Jamie Madill0448ec82016-12-23 13:41:47 -0500404 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
405 if (mEnableValidationLayers)
406 {
407 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
408 }
409
Jamie Madille09bd5d2016-11-29 16:20:35 -0500410 // Verify the required extensions are in the extension names set. Fail if not.
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400411 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400412 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500413
Jamie Madill327ba852016-11-30 12:38:28 -0500414 VkApplicationInfo applicationInfo;
415 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
416 applicationInfo.pNext = nullptr;
417 applicationInfo.pApplicationName = "ANGLE";
418 applicationInfo.applicationVersion = 1;
419 applicationInfo.pEngineName = "ANGLE";
420 applicationInfo.engineVersion = 1;
421 applicationInfo.apiVersion = VK_API_VERSION_1_0;
422
423 VkInstanceCreateInfo instanceInfo;
424 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
425 instanceInfo.pNext = nullptr;
426 instanceInfo.flags = 0;
427 instanceInfo.pApplicationInfo = &applicationInfo;
428
Jamie Madille09bd5d2016-11-29 16:20:35 -0500429 // Enable requested layers and extensions.
430 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
431 instanceInfo.ppEnabledExtensionNames =
432 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500433 instanceInfo.enabledLayerCount = enabledLayerCount;
434 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500435
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400436 ANGLE_VK_TRY(displayVk, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500437
Jamie Madill0448ec82016-12-23 13:41:47 -0500438 if (mEnableValidationLayers)
439 {
440 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
441
442 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
443 debugReportInfo.pNext = nullptr;
444 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
445 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
446 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
447 debugReportInfo.pfnCallback = &DebugReportCallback;
448 debugReportInfo.pUserData = this;
449
450 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
451 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
452 ASSERT(createDebugReportCallback);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400453 ANGLE_VK_TRY(displayVk, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
454 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500455 }
456
Jamie Madill4d0bf552016-12-28 15:45:24 -0500457 uint32_t physicalDeviceCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400458 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
459 ANGLE_VK_CHECK(displayVk, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500460
461 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700462 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400463 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
464 physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600465 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700466 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500467
Jamie Madill30b5d842018-08-31 17:19:12 -0400468 vkGetPhysicalDeviceFeatures(mPhysicalDevice, &mPhysicalDeviceFeatures);
469
Jamie Madill4d0bf552016-12-28 15:45:24 -0500470 // Ensure we can find a graphics queue family.
471 uint32_t queueCount = 0;
472 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
473
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400474 ANGLE_VK_CHECK(displayVk, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500475
476 mQueueFamilyProperties.resize(queueCount);
477 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
478 mQueueFamilyProperties.data());
479
480 size_t graphicsQueueFamilyCount = false;
481 uint32_t firstGraphicsQueueFamily = 0;
482 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
483 {
484 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
485 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
486 {
487 ASSERT(queueInfo.queueCount > 0);
488 graphicsQueueFamilyCount++;
489 if (firstGraphicsQueueFamily == 0)
490 {
491 firstGraphicsQueueFamily = familyIndex;
492 }
493 break;
494 }
495 }
496
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400497 ANGLE_VK_CHECK(displayVk, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500498
Jamie Madill12222072018-07-11 14:59:48 -0400499 initFeatures();
500
Jamie Madill4d0bf552016-12-28 15:45:24 -0500501 // If only one queue family, go ahead and initialize the device. If there is more than one
502 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
503 if (graphicsQueueFamilyCount == 1)
504 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400505 ANGLE_TRY(initializeDevice(displayVk, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500506 }
507
Jamie Madill035fd6b2017-10-03 15:43:22 -0400508 // Store the physical device memory properties so we can find the right memory pools.
509 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500510
Jamie Madill06ca6342018-07-12 15:56:53 -0400511 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500512
Jamie Madill6a89d222017-11-02 11:59:51 -0400513 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500514 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
515 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400516
Jamie Madill21061022018-07-12 23:56:30 -0400517 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400518}
519
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400520angle::Result RendererVk::initializeDevice(DisplayVk *displayVk, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500521{
522 uint32_t deviceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400523 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400524 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500525
526 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
527 if (deviceLayerCount > 0)
528 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400529 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
530 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500531 }
532
533 uint32_t deviceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400534 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
535 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500536
537 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
538 if (deviceExtensionCount > 0)
539 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400540 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
541 &deviceExtensionCount,
542 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500543 }
544
Yuly Novikov199f4292018-01-19 19:04:05 -0500545 const char *const *enabledLayerNames = nullptr;
546 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500547 if (mEnableValidationLayers)
548 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500549 mEnableValidationLayers = GetAvailableValidationLayers(
550 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500551 }
552
553 std::vector<const char *> enabledDeviceExtensions;
554 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
555
Luc Ferronbf6dc372018-06-28 15:24:19 -0400556 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
557 if (getFeatures().flipViewportY)
558 {
559 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
560 }
561
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400562 ANGLE_VK_TRY(displayVk, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500563
564 VkDeviceQueueCreateInfo queueCreateInfo;
565
566 float zeroPriority = 0.0f;
567
568 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
569 queueCreateInfo.pNext = nullptr;
570 queueCreateInfo.flags = 0;
571 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
572 queueCreateInfo.queueCount = 1;
573 queueCreateInfo.pQueuePriorities = &zeroPriority;
574
575 // Initialize the device
576 VkDeviceCreateInfo createInfo;
577
Jamie Madill50cf2be2018-06-15 09:46:57 -0400578 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
579 createInfo.pNext = nullptr;
580 createInfo.flags = 0;
581 createInfo.queueCreateInfoCount = 1;
582 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500583 createInfo.enabledLayerCount = enabledLayerCount;
584 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500585 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
586 createInfo.ppEnabledExtensionNames =
587 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
588 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
589
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400590 ANGLE_VK_TRY(displayVk, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500591
592 mCurrentQueueFamilyIndex = queueFamilyIndex;
593
594 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
595
596 // Initialize the command pool now that we know the queue family index.
597 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500598 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
599 commandPoolInfo.pNext = nullptr;
600 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500601 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
602
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400603 ANGLE_TRY(mCommandPool.init(displayVk, commandPoolInfo));
604
605 // Initialize the vulkan pipeline cache
606 ANGLE_TRY(initPipelineCacheVk(displayVk));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500607
Jamie Madill21061022018-07-12 23:56:30 -0400608 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500609}
610
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400611angle::Result RendererVk::selectPresentQueueForSurface(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400612 VkSurfaceKHR surface,
613 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500614{
615 // We've already initialized a device, and can't re-create it unless it's never been used.
616 // TODO(jmadill): Handle the re-creation case if necessary.
617 if (mDevice != VK_NULL_HANDLE)
618 {
619 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
620
621 // Check if the current device supports present on this surface.
622 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400623 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400624 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500625 surface, &supportsPresent));
626
Jamie Madill6cad7732018-07-11 09:01:17 -0400627 if (supportsPresent == VK_TRUE)
628 {
629 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400630 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400631 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500632 }
633
634 // Find a graphics and present queue.
635 Optional<uint32_t> newPresentQueue;
636 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
637 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
638 {
639 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
640 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
641 {
642 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400643 ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceSupportKHR(
644 mPhysicalDevice, queueIndex, surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500645
646 if (supportsPresent == VK_TRUE)
647 {
648 newPresentQueue = queueIndex;
649 break;
650 }
651 }
652 }
653
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400654 ANGLE_VK_CHECK(displayVk, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
655 ANGLE_TRY(initializeDevice(displayVk, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500656
Jamie Madill6cad7732018-07-11 09:01:17 -0400657 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400658 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500659}
660
661std::string RendererVk::getVendorString() const
662{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300663 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500664}
665
Jamie Madille09bd5d2016-11-29 16:20:35 -0500666std::string RendererVk::getRendererDescription() const
667{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500668 std::stringstream strstr;
669
670 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
671
672 strstr << "Vulkan ";
673 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
674 strstr << VK_VERSION_MINOR(apiVersion) << ".";
675 strstr << VK_VERSION_PATCH(apiVersion);
676
Olli Etuahoc6a06182018-04-13 14:11:46 +0300677 strstr << "(";
678
679 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
680 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
681 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
682 // driver detection.
683 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
684 {
685 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
686 }
687
688 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500689
690 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500691}
692
Jamie Madill12222072018-07-11 14:59:48 -0400693void RendererVk::initFeatures()
694{
Jamie Madillb36a4812018-09-25 10:15:11 -0400695// Use OpenGL line rasterization rules by default.
696// TODO(jmadill): Fix Android support. http://anglebug.com/2830
697#if defined(ANGLE_PLATFORM_ANDROID)
698 mFeatures.basicGLLineRasterization = false;
699#else
Jamie Madill12222072018-07-11 14:59:48 -0400700 mFeatures.basicGLLineRasterization = true;
Jamie Madillb36a4812018-09-25 10:15:11 -0400701#endif // defined(ANGLE_PLATFORM_ANDROID)
Jamie Madill12222072018-07-11 14:59:48 -0400702
Luc Ferronf786b702018-07-10 11:01:43 -0400703 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
704 // investigation. http://anglebug.com/2728
705 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Frank Henigmanbeb669d2018-09-21 16:25:52 -0400706
707#ifdef ANGLE_PLATFORM_WINDOWS
708 // http://anglebug.com/2838
709 mFeatures.extraCopyBufferRegion = IsIntel(mPhysicalDeviceProperties.vendorID);
710#endif
Jamie Madill12222072018-07-11 14:59:48 -0400711}
712
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400713void RendererVk::initPipelineCacheVkKey()
714{
715 std::ostringstream hashStream("ANGLE Pipeline Cache: ", std::ios_base::ate);
716 // Add the pipeline cache UUID to make sure the blob cache always gives a compatible pipeline
717 // cache. It's not particularly necessary to write it as a hex number as done here, so long as
718 // there is no '\0' in the result.
719 for (const uint32_t c : mPhysicalDeviceProperties.pipelineCacheUUID)
720 {
721 hashStream << std::hex << c;
722 }
723 // Add the vendor and device id too for good measure.
724 hashStream << std::hex << mPhysicalDeviceProperties.vendorID;
725 hashStream << std::hex << mPhysicalDeviceProperties.deviceID;
726
727 const std::string &hashString = hashStream.str();
728 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(hashString.c_str()),
729 hashString.length(), mPipelineCacheVkBlobKey.data());
730}
731
732angle::Result RendererVk::initPipelineCacheVk(DisplayVk *display)
733{
734 initPipelineCacheVkKey();
735
736 egl::BlobCache::Value initialData;
737 bool success = display->getBlobCache()->get(display->getScratchBuffer(),
738 mPipelineCacheVkBlobKey, &initialData);
739
740 VkPipelineCacheCreateInfo pipelineCacheCreateInfo;
741
742 pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
743 pipelineCacheCreateInfo.pNext = nullptr;
744 pipelineCacheCreateInfo.flags = 0;
745 pipelineCacheCreateInfo.initialDataSize = success ? initialData.size() : 0;
746 pipelineCacheCreateInfo.pInitialData = success ? initialData.data() : nullptr;
747
748 ANGLE_TRY(mPipelineCacheVk.init(display, pipelineCacheCreateInfo));
749 return angle::Result::Continue();
750}
751
Jamie Madillacccc6c2016-05-03 17:22:10 -0400752void RendererVk::ensureCapsInitialized() const
753{
754 if (!mCapsInitialized)
755 {
Jamie Madill30b5d842018-08-31 17:19:12 -0400756 vk::GenerateCaps(mPhysicalDeviceProperties, mPhysicalDeviceFeatures, mNativeTextureCaps,
757 &mNativeCaps, &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400758 mCapsInitialized = true;
759 }
760}
761
Jamie Madillacccc6c2016-05-03 17:22:10 -0400762const gl::Caps &RendererVk::getNativeCaps() const
763{
764 ensureCapsInitialized();
765 return mNativeCaps;
766}
767
768const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
769{
770 ensureCapsInitialized();
771 return mNativeTextureCaps;
772}
773
774const gl::Extensions &RendererVk::getNativeExtensions() const
775{
776 ensureCapsInitialized();
777 return mNativeExtensions;
778}
779
780const gl::Limitations &RendererVk::getNativeLimitations() const
781{
782 ensureCapsInitialized();
783 return mNativeLimitations;
784}
785
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400786uint32_t RendererVk::getMaxActiveTextures()
787{
788 // TODO(lucferron): expose this limitation to GL in Context Caps
789 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
790 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
791}
792
Jamie Madill49ac74b2017-12-21 14:42:33 -0500793const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500794{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500795 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500796}
797
Jamie Madill21061022018-07-12 23:56:30 -0400798angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500799{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500800 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500801 {
Luc Ferron1617e692018-07-11 11:08:19 -0400802 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
803 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400804
Jamie Madill49ac74b2017-12-21 14:42:33 -0500805 VkSubmitInfo submitInfo;
806 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
807 submitInfo.pNext = nullptr;
808 submitInfo.waitSemaphoreCount = 0;
809 submitInfo.pWaitSemaphores = nullptr;
810 submitInfo.pWaitDstStageMask = nullptr;
811 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400812 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500813 submitInfo.signalSemaphoreCount = 0;
814 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500815
Jamie Madill21061022018-07-12 23:56:30 -0400816 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500817 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500818
Jamie Madill4c26fc22017-02-24 11:04:10 -0500819 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400820 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400821 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400822 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500823}
824
Jamie Madill0c0dc342017-03-24 14:18:51 -0400825void RendererVk::freeAllInFlightResources()
826{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500827 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400828 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500829 batch.fence.destroy(mDevice);
830 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400831 }
832 mInFlightCommands.clear();
833
834 for (auto &garbage : mGarbage)
835 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400836 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400837 }
838 mGarbage.clear();
839}
840
Jamie Madill21061022018-07-12 23:56:30 -0400841angle::Result RendererVk::checkInFlightCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500842{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500843 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500844
Jamie Madill49ac74b2017-12-21 14:42:33 -0500845 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500846 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500847 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400848 if (result == VK_NOT_READY)
849 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500850
Jamie Madill21061022018-07-12 23:56:30 -0400851 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500852 ASSERT(batch.serial > mLastCompletedQueueSerial);
853 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400854
Jamie Madill49ac74b2017-12-21 14:42:33 -0500855 batch.fence.destroy(mDevice);
856 batch.commandPool.destroy(mDevice);
857 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500858 }
859
Jamie Madill49ac74b2017-12-21 14:42:33 -0500860 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400861
862 size_t freeIndex = 0;
863 for (; freeIndex < mGarbage.size(); ++freeIndex)
864 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500865 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400866 break;
867 }
868
869 // Remove the entries from the garbage list - they should be ready to go.
870 if (freeIndex > 0)
871 {
872 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500873 }
874
Jamie Madill21061022018-07-12 23:56:30 -0400875 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500876}
877
Jamie Madill21061022018-07-12 23:56:30 -0400878angle::Result RendererVk::submitFrame(vk::Context *context,
879 const VkSubmitInfo &submitInfo,
880 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500881{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500882 VkFenceCreateInfo fenceInfo;
883 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
884 fenceInfo.pNext = nullptr;
885 fenceInfo.flags = 0;
886
Jamie Madillbea35a62018-07-05 11:54:10 -0400887 vk::Scoped<CommandBatch> scopedBatch(mDevice);
888 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400889 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500890
Jamie Madill21061022018-07-12 23:56:30 -0400891 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500892
893 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500894 batch.commandPool = std::move(mCommandPool);
895 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500896
Jamie Madillbea35a62018-07-05 11:54:10 -0400897 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400898
Omar El Sheikh0359b692018-07-23 17:22:00 -0600899 // Check that mInFlightCommands isn't growing too fast
900 // If it is, wait for the queue to complete work it has alread been assigned
901 if (mInFlightCommands.size() > kInFlightCommandsLimit)
902 {
903 vkQueueWaitIdle(mQueue);
904 }
Jamie Madill0c0dc342017-03-24 14:18:51 -0400905
906 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400907 // TODO(jmadill): Overflow check.
908 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400909
Jamie Madill21061022018-07-12 23:56:30 -0400910 ANGLE_TRY(checkInFlightCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400911
Jamie Madill49ac74b2017-12-21 14:42:33 -0500912 // Simply null out the command buffer here - it was allocated using the command pool.
913 commandBuffer.releaseHandle();
914
915 // Reallocate the command pool for next frame.
916 // TODO(jmadill): Consider reusing command pools.
917 VkCommandPoolCreateInfo poolInfo;
918 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
919 poolInfo.pNext = nullptr;
920 poolInfo.flags = 0;
921 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
922
Jamie Madill21061022018-07-12 23:56:30 -0400923 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500924
Jamie Madill21061022018-07-12 23:56:30 -0400925 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500926}
927
Jamie Madillaaca96e2018-06-12 10:19:48 -0400928bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500929{
930 return serial > mLastCompletedQueueSerial;
931}
932
Jamie Madill21061022018-07-12 23:56:30 -0400933angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
934 const vk::RenderPassDesc &desc,
935 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500936{
Jamie Madill21061022018-07-12 23:56:30 -0400937 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -0500938 renderPassOut);
939}
940
Jamie Madill21061022018-07-12 23:56:30 -0400941angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
942 const vk::RenderPassDesc &desc,
943 const vk::AttachmentOpsArray &ops,
944 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500945{
Jamie Madill21061022018-07-12 23:56:30 -0400946 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -0500947 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500948}
949
Jamie Madilla5e06072018-05-18 14:36:05 -0400950vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500951{
Jamie Madilla5e06072018-05-18 14:36:05 -0400952 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500953}
954
Jamie Madill21061022018-07-12 23:56:30 -0400955angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500956{
Jamie Madill21061022018-07-12 23:56:30 -0400957 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500958 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500959}
960
Jamie Madill21061022018-07-12 23:56:30 -0400961angle::Result RendererVk::flush(vk::Context *context,
962 const vk::Semaphore &waitSemaphore,
963 const vk::Semaphore &signalSemaphore)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500964{
Jamie Madillbea35a62018-07-05 11:54:10 -0400965 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
966 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500967
968 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
969
970 VkSubmitInfo submitInfo;
971 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
972 submitInfo.pNext = nullptr;
973 submitInfo.waitSemaphoreCount = 1;
974 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
975 submitInfo.pWaitDstStageMask = &waitStageMask;
976 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400977 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500978 submitInfo.signalSemaphoreCount = 1;
979 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
980
Jamie Madill21061022018-07-12 23:56:30 -0400981 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
982 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500983}
984
Jamie Madill78feddc2018-04-27 11:45:05 -0400985Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500986{
Jamie Madill78feddc2018-04-27 11:45:05 -0400987 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500988}
989
Jamie Madill06ca6342018-07-12 15:56:53 -0400990angle::Result RendererVk::getPipeline(vk::Context *context,
991 const vk::ShaderAndSerial &vertexShader,
992 const vk::ShaderAndSerial &fragmentShader,
993 const vk::PipelineLayout &pipelineLayout,
994 const vk::PipelineDesc &pipelineDesc,
995 const gl::AttributesMask &activeAttribLocationsMask,
996 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500997{
Jamie Madill06ca6342018-07-12 15:56:53 -0400998 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -0400999 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -04001000 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -04001001 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
1002
1003 // Pull in a compatible RenderPass.
1004 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001005 ANGLE_TRY(
1006 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -04001007
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001008 return mPipelineCache.getPipeline(context, mPipelineCacheVk, *compatibleRenderPass,
1009 pipelineLayout, activeAttribLocationsMask, vertexShader.get(),
Jamie Madill9aef3672018-04-27 11:45:06 -04001010 fragmentShader.get(), pipelineDesc, pipelineOut);
1011}
1012
Jamie Madill21061022018-07-12 23:56:30 -04001013angle::Result RendererVk::getDescriptorSetLayout(
1014 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001015 const vk::DescriptorSetLayoutDesc &desc,
1016 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
1017{
Jamie Madill21061022018-07-12 23:56:30 -04001018 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -04001019}
1020
Jamie Madill21061022018-07-12 23:56:30 -04001021angle::Result RendererVk::getPipelineLayout(
1022 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001023 const vk::PipelineLayoutDesc &desc,
1024 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
1025 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
1026{
Jamie Madill21061022018-07-12 23:56:30 -04001027 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -04001028 pipelineLayoutOut);
1029}
1030
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001031angle::Result RendererVk::syncPipelineCacheVk(DisplayVk *displayVk)
1032{
1033 ASSERT(mPipelineCacheVk.valid());
1034
1035 if (--mPipelineCacheVkUpdateTimeout > 0)
1036 {
1037 return angle::Result::Continue();
1038 }
1039
1040 mPipelineCacheVkUpdateTimeout = kPipelineCacheVkUpdatePeriod;
1041
1042 // Get the size of the cache.
1043 size_t pipelineCacheSize = 0;
1044 ANGLE_TRY(mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, nullptr));
1045
1046 angle::MemoryBuffer *pipelineCacheData = nullptr;
1047 ANGLE_VK_CHECK_ALLOC(displayVk,
1048 displayVk->getScratchBuffer(pipelineCacheSize, &pipelineCacheData));
1049
1050 size_t originalPipelineCacheSize = pipelineCacheSize;
1051 angle::Result result =
1052 mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, pipelineCacheData->data());
1053 ANGLE_TRY(result);
1054
1055 // Note: currently we don't accept incomplete as we don't expect it (the full size of cache
1056 // was determined just above), so receiving it hints at an implementation bug we would want
1057 // to know about early.
1058 ASSERT(result != angle::Result::Incomplete());
1059
1060 // If vkGetPipelineCacheData ends up writing fewer bytes than requested, zero out the rest of
1061 // the buffer to avoid leaking garbage memory.
1062 ASSERT(pipelineCacheSize <= originalPipelineCacheSize);
1063 if (pipelineCacheSize < originalPipelineCacheSize)
1064 {
1065 memset(pipelineCacheData->data() + pipelineCacheSize, 0,
1066 originalPipelineCacheSize - pipelineCacheSize);
1067 }
1068
1069 displayVk->getBlobCache()->putApplication(mPipelineCacheVkBlobKey, *pipelineCacheData);
1070
1071 return angle::Result::Continue();
1072}
1073
Jamie Madilld47044a2018-04-27 11:45:03 -04001074vk::ShaderLibrary *RendererVk::getShaderLibrary()
1075{
1076 return &mShaderLibrary;
1077}
Luc Ferron90968362018-05-04 08:47:22 -04001078
Jamie Madillaaca96e2018-06-12 10:19:48 -04001079uint32_t GetUniformBufferDescriptorCount()
1080{
1081 return kUniformBufferDescriptorsPerDescriptorSet;
1082}
1083
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001084} // namespace rx