blob: c1486d28dba994f7a23739609630b0f53cb3bde3 [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 Madill0da73fe2018-10-02 09:31:39 -0400264
265// Initially dumping the command graphs is disabled.
266constexpr bool kEnableCommandGraphDiagnostics = false;
Jamie Madille09bd5d2016-11-29 16:20:35 -0500267} // anonymous namespace
268
Jamie Madill49ac74b2017-12-21 14:42:33 -0500269// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400270RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500271
Jamie Madillaaca96e2018-06-12 10:19:48 -0400272RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500273
274RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
275 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
276{
277}
278
279RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
280{
281 std::swap(commandPool, other.commandPool);
282 std::swap(fence, other.fence);
283 std::swap(serial, other.serial);
284 return *this;
285}
286
Jamie Madillbea35a62018-07-05 11:54:10 -0400287void RendererVk::CommandBatch::destroy(VkDevice device)
288{
289 commandPool.destroy(device);
290 fence.destroy(device);
291}
292
Jamie Madill9f2a8612017-11-30 12:43:09 -0500293// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500294RendererVk::RendererVk()
295 : mCapsInitialized(false),
296 mInstance(VK_NULL_HANDLE),
297 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500298 mDebugReportCallback(VK_NULL_HANDLE),
299 mPhysicalDevice(VK_NULL_HANDLE),
300 mQueue(VK_NULL_HANDLE),
301 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
302 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400303 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400304 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400305 mDeviceLost(false),
Jamie Madill0da73fe2018-10-02 09:31:39 -0400306 mPipelineCacheVkUpdateTimeout(kPipelineCacheVkUpdatePeriod),
307 mCommandGraph(kEnableCommandGraphDiagnostics)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400308{
309}
310
311RendererVk::~RendererVk()
312{
Jamie Madill21061022018-07-12 23:56:30 -0400313}
314
315void RendererVk::onDestroy(vk::Context *context)
316{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500317 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500318 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500319 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400320 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500321 }
322
Jamie Madillc7918ce2018-06-13 13:25:31 -0400323 mPipelineLayoutCache.destroy(mDevice);
324 mDescriptorSetLayoutCache.destroy(mDevice);
325
Jamie Madill9f2a8612017-11-30 12:43:09 -0500326 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500327 mPipelineCache.destroy(mDevice);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400328 mPipelineCacheVk.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400329 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500330
Jamie Madill06ca6342018-07-12 15:56:53 -0400331 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500332
Jamie Madill5deea722017-02-16 10:44:46 -0500333 if (mCommandPool.valid())
334 {
335 mCommandPool.destroy(mDevice);
336 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500337
338 if (mDevice)
339 {
340 vkDestroyDevice(mDevice, nullptr);
341 mDevice = VK_NULL_HANDLE;
342 }
343
Jamie Madill0448ec82016-12-23 13:41:47 -0500344 if (mDebugReportCallback)
345 {
346 ASSERT(mInstance);
347 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
348 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
349 ASSERT(destroyDebugReportCallback);
350 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
351 }
352
Jamie Madill4d0bf552016-12-28 15:45:24 -0500353 if (mInstance)
354 {
355 vkDestroyInstance(mInstance, nullptr);
356 mInstance = VK_NULL_HANDLE;
357 }
358
Omar El Sheikheb4b8692018-07-17 10:55:40 -0600359 mMemoryProperties.destroy();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500360 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500361}
362
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400363void RendererVk::markDeviceLost()
364{
365 mDeviceLost = true;
366}
367
368bool RendererVk::isDeviceLost() const
369{
370 return mDeviceLost;
371}
372
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400373angle::Result RendererVk::initialize(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400374 const egl::AttributeMap &attribs,
375 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500376{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600377 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
378 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500379 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600380 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500381
Jamie Madill0448ec82016-12-23 13:41:47 -0500382 // Gather global layer properties.
383 uint32_t instanceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400384 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500385
386 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
387 if (instanceLayerCount > 0)
388 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400389 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
390 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500391 }
392
Jamie Madille09bd5d2016-11-29 16:20:35 -0500393 uint32_t instanceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400394 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400395 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500396
397 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
398 if (instanceExtensionCount > 0)
399 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400400 ANGLE_VK_TRY(displayVk,
401 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
402 instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500403 }
404
Yuly Novikov199f4292018-01-19 19:04:05 -0500405 const char *const *enabledLayerNames = nullptr;
406 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500407 if (mEnableValidationLayers)
408 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500409 bool layersRequested =
410 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
411 mEnableValidationLayers = GetAvailableValidationLayers(
412 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500413 }
414
Jamie Madille09bd5d2016-11-29 16:20:35 -0500415 std::vector<const char *> enabledInstanceExtensions;
416 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500417 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500418
Jamie Madill0448ec82016-12-23 13:41:47 -0500419 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
420 if (mEnableValidationLayers)
421 {
422 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
423 }
424
Jamie Madille09bd5d2016-11-29 16:20:35 -0500425 // Verify the required extensions are in the extension names set. Fail if not.
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400426 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400427 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500428
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400429 VkApplicationInfo applicationInfo = {};
Jamie Madill327ba852016-11-30 12:38:28 -0500430 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
Jamie Madill327ba852016-11-30 12:38:28 -0500431 applicationInfo.pApplicationName = "ANGLE";
432 applicationInfo.applicationVersion = 1;
433 applicationInfo.pEngineName = "ANGLE";
434 applicationInfo.engineVersion = 1;
435 applicationInfo.apiVersion = VK_API_VERSION_1_0;
436
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400437 VkInstanceCreateInfo instanceInfo = {};
Jamie Madill327ba852016-11-30 12:38:28 -0500438 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jamie Madill327ba852016-11-30 12:38:28 -0500439 instanceInfo.flags = 0;
440 instanceInfo.pApplicationInfo = &applicationInfo;
441
Jamie Madille09bd5d2016-11-29 16:20:35 -0500442 // Enable requested layers and extensions.
443 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
444 instanceInfo.ppEnabledExtensionNames =
445 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500446 instanceInfo.enabledLayerCount = enabledLayerCount;
447 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500448
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400449 ANGLE_VK_TRY(displayVk, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500450
Jamie Madill0448ec82016-12-23 13:41:47 -0500451 if (mEnableValidationLayers)
452 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400453 VkDebugReportCallbackCreateInfoEXT debugReportInfo = {};
Jamie Madill0448ec82016-12-23 13:41:47 -0500454
455 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Jamie Madill0448ec82016-12-23 13:41:47 -0500456 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
457 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
458 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
459 debugReportInfo.pfnCallback = &DebugReportCallback;
460 debugReportInfo.pUserData = this;
461
462 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
463 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
464 ASSERT(createDebugReportCallback);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400465 ANGLE_VK_TRY(displayVk, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
466 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500467 }
468
Jamie Madill4d0bf552016-12-28 15:45:24 -0500469 uint32_t physicalDeviceCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400470 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
471 ANGLE_VK_CHECK(displayVk, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500472
473 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700474 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400475 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
476 physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600477 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700478 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500479
Jamie Madill30b5d842018-08-31 17:19:12 -0400480 vkGetPhysicalDeviceFeatures(mPhysicalDevice, &mPhysicalDeviceFeatures);
481
Jamie Madill4d0bf552016-12-28 15:45:24 -0500482 // Ensure we can find a graphics queue family.
483 uint32_t queueCount = 0;
484 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
485
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400486 ANGLE_VK_CHECK(displayVk, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500487
488 mQueueFamilyProperties.resize(queueCount);
489 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
490 mQueueFamilyProperties.data());
491
492 size_t graphicsQueueFamilyCount = false;
493 uint32_t firstGraphicsQueueFamily = 0;
494 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
495 {
496 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
497 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
498 {
499 ASSERT(queueInfo.queueCount > 0);
500 graphicsQueueFamilyCount++;
501 if (firstGraphicsQueueFamily == 0)
502 {
503 firstGraphicsQueueFamily = familyIndex;
504 }
505 break;
506 }
507 }
508
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400509 ANGLE_VK_CHECK(displayVk, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500510
Jamie Madill12222072018-07-11 14:59:48 -0400511 initFeatures();
512
Jamie Madill4d0bf552016-12-28 15:45:24 -0500513 // If only one queue family, go ahead and initialize the device. If there is more than one
514 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
515 if (graphicsQueueFamilyCount == 1)
516 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400517 ANGLE_TRY(initializeDevice(displayVk, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500518 }
519
Jamie Madill035fd6b2017-10-03 15:43:22 -0400520 // Store the physical device memory properties so we can find the right memory pools.
521 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500522
Jamie Madill06ca6342018-07-12 15:56:53 -0400523 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500524
Jamie Madill6a89d222017-11-02 11:59:51 -0400525 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500526 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
527 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400528
Jamie Madill21061022018-07-12 23:56:30 -0400529 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400530}
531
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400532angle::Result RendererVk::initializeDevice(DisplayVk *displayVk, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500533{
534 uint32_t deviceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400535 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400536 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500537
538 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
539 if (deviceLayerCount > 0)
540 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400541 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
542 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500543 }
544
545 uint32_t deviceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400546 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
547 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500548
549 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
550 if (deviceExtensionCount > 0)
551 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400552 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
553 &deviceExtensionCount,
554 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500555 }
556
Yuly Novikov199f4292018-01-19 19:04:05 -0500557 const char *const *enabledLayerNames = nullptr;
558 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500559 if (mEnableValidationLayers)
560 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500561 mEnableValidationLayers = GetAvailableValidationLayers(
562 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500563 }
564
565 std::vector<const char *> enabledDeviceExtensions;
566 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
567
Luc Ferronbf6dc372018-06-28 15:24:19 -0400568 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
569 if (getFeatures().flipViewportY)
570 {
571 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
572 }
573
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400574 ANGLE_VK_TRY(displayVk, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500575
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400576 // Select additional features to be enabled
577 VkPhysicalDeviceFeatures enabledFeatures = {};
578 enabledFeatures.inheritedQueries = mPhysicalDeviceFeatures.inheritedQueries;
579
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400580 VkDeviceQueueCreateInfo queueCreateInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500581
582 float zeroPriority = 0.0f;
583
584 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500585 queueCreateInfo.flags = 0;
586 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
587 queueCreateInfo.queueCount = 1;
588 queueCreateInfo.pQueuePriorities = &zeroPriority;
589
590 // Initialize the device
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400591 VkDeviceCreateInfo createInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500592
Jamie Madill50cf2be2018-06-15 09:46:57 -0400593 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400594 createInfo.flags = 0;
595 createInfo.queueCreateInfoCount = 1;
596 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500597 createInfo.enabledLayerCount = enabledLayerCount;
598 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500599 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
600 createInfo.ppEnabledExtensionNames =
601 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400602 createInfo.pEnabledFeatures = &enabledFeatures;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500603
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400604 ANGLE_VK_TRY(displayVk, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500605
606 mCurrentQueueFamilyIndex = queueFamilyIndex;
607
608 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
609
610 // Initialize the command pool now that we know the queue family index.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400611 VkCommandPoolCreateInfo commandPoolInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500612 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500613 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500614 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
615
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400616 ANGLE_TRY(mCommandPool.init(displayVk, commandPoolInfo));
617
618 // Initialize the vulkan pipeline cache
619 ANGLE_TRY(initPipelineCacheVk(displayVk));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500620
Jamie Madill21061022018-07-12 23:56:30 -0400621 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500622}
623
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400624angle::Result RendererVk::selectPresentQueueForSurface(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400625 VkSurfaceKHR surface,
626 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500627{
628 // We've already initialized a device, and can't re-create it unless it's never been used.
629 // TODO(jmadill): Handle the re-creation case if necessary.
630 if (mDevice != VK_NULL_HANDLE)
631 {
632 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
633
634 // Check if the current device supports present on this surface.
635 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400636 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400637 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500638 surface, &supportsPresent));
639
Jamie Madill6cad7732018-07-11 09:01:17 -0400640 if (supportsPresent == VK_TRUE)
641 {
642 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400643 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400644 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500645 }
646
647 // Find a graphics and present queue.
648 Optional<uint32_t> newPresentQueue;
649 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
650 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
651 {
652 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
653 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
654 {
655 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400656 ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceSupportKHR(
657 mPhysicalDevice, queueIndex, surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500658
659 if (supportsPresent == VK_TRUE)
660 {
661 newPresentQueue = queueIndex;
662 break;
663 }
664 }
665 }
666
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400667 ANGLE_VK_CHECK(displayVk, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
668 ANGLE_TRY(initializeDevice(displayVk, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500669
Jamie Madill6cad7732018-07-11 09:01:17 -0400670 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400671 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500672}
673
674std::string RendererVk::getVendorString() const
675{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300676 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500677}
678
Jamie Madille09bd5d2016-11-29 16:20:35 -0500679std::string RendererVk::getRendererDescription() const
680{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500681 std::stringstream strstr;
682
683 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
684
685 strstr << "Vulkan ";
686 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
687 strstr << VK_VERSION_MINOR(apiVersion) << ".";
688 strstr << VK_VERSION_PATCH(apiVersion);
689
Olli Etuahoc6a06182018-04-13 14:11:46 +0300690 strstr << "(";
691
692 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
693 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
694 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
695 // driver detection.
696 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
697 {
698 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
699 }
700
701 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500702
703 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500704}
705
Jamie Madill12222072018-07-11 14:59:48 -0400706void RendererVk::initFeatures()
707{
Jamie Madillb36a4812018-09-25 10:15:11 -0400708// Use OpenGL line rasterization rules by default.
709// TODO(jmadill): Fix Android support. http://anglebug.com/2830
710#if defined(ANGLE_PLATFORM_ANDROID)
711 mFeatures.basicGLLineRasterization = false;
712#else
Jamie Madill12222072018-07-11 14:59:48 -0400713 mFeatures.basicGLLineRasterization = true;
Jamie Madillb36a4812018-09-25 10:15:11 -0400714#endif // defined(ANGLE_PLATFORM_ANDROID)
Jamie Madill12222072018-07-11 14:59:48 -0400715
Luc Ferronf786b702018-07-10 11:01:43 -0400716 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
717 // investigation. http://anglebug.com/2728
718 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Frank Henigmanbeb669d2018-09-21 16:25:52 -0400719
720#ifdef ANGLE_PLATFORM_WINDOWS
721 // http://anglebug.com/2838
722 mFeatures.extraCopyBufferRegion = IsIntel(mPhysicalDeviceProperties.vendorID);
723#endif
Jamie Madill12222072018-07-11 14:59:48 -0400724}
725
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400726void RendererVk::initPipelineCacheVkKey()
727{
728 std::ostringstream hashStream("ANGLE Pipeline Cache: ", std::ios_base::ate);
729 // Add the pipeline cache UUID to make sure the blob cache always gives a compatible pipeline
730 // cache. It's not particularly necessary to write it as a hex number as done here, so long as
731 // there is no '\0' in the result.
732 for (const uint32_t c : mPhysicalDeviceProperties.pipelineCacheUUID)
733 {
734 hashStream << std::hex << c;
735 }
736 // Add the vendor and device id too for good measure.
737 hashStream << std::hex << mPhysicalDeviceProperties.vendorID;
738 hashStream << std::hex << mPhysicalDeviceProperties.deviceID;
739
740 const std::string &hashString = hashStream.str();
741 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(hashString.c_str()),
742 hashString.length(), mPipelineCacheVkBlobKey.data());
743}
744
745angle::Result RendererVk::initPipelineCacheVk(DisplayVk *display)
746{
747 initPipelineCacheVkKey();
748
749 egl::BlobCache::Value initialData;
750 bool success = display->getBlobCache()->get(display->getScratchBuffer(),
751 mPipelineCacheVkBlobKey, &initialData);
752
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400753 VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {};
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400754
755 pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400756 pipelineCacheCreateInfo.flags = 0;
757 pipelineCacheCreateInfo.initialDataSize = success ? initialData.size() : 0;
758 pipelineCacheCreateInfo.pInitialData = success ? initialData.data() : nullptr;
759
760 ANGLE_TRY(mPipelineCacheVk.init(display, pipelineCacheCreateInfo));
761 return angle::Result::Continue();
762}
763
Jamie Madillacccc6c2016-05-03 17:22:10 -0400764void RendererVk::ensureCapsInitialized() const
765{
766 if (!mCapsInitialized)
767 {
Jamie Madill30b5d842018-08-31 17:19:12 -0400768 vk::GenerateCaps(mPhysicalDeviceProperties, mPhysicalDeviceFeatures, mNativeTextureCaps,
769 &mNativeCaps, &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400770 mCapsInitialized = true;
771 }
772}
773
Jamie Madillacccc6c2016-05-03 17:22:10 -0400774const gl::Caps &RendererVk::getNativeCaps() const
775{
776 ensureCapsInitialized();
777 return mNativeCaps;
778}
779
780const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
781{
782 ensureCapsInitialized();
783 return mNativeTextureCaps;
784}
785
786const gl::Extensions &RendererVk::getNativeExtensions() const
787{
788 ensureCapsInitialized();
789 return mNativeExtensions;
790}
791
792const gl::Limitations &RendererVk::getNativeLimitations() const
793{
794 ensureCapsInitialized();
795 return mNativeLimitations;
796}
797
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400798uint32_t RendererVk::getMaxActiveTextures()
799{
800 // TODO(lucferron): expose this limitation to GL in Context Caps
801 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
802 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
803}
804
Jamie Madill49ac74b2017-12-21 14:42:33 -0500805const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500806{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500807 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500808}
809
Jamie Madill21061022018-07-12 23:56:30 -0400810angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500811{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500812 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500813 {
Luc Ferron1617e692018-07-11 11:08:19 -0400814 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
815 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400816
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400817 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500818 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500819 submitInfo.waitSemaphoreCount = 0;
820 submitInfo.pWaitSemaphores = nullptr;
821 submitInfo.pWaitDstStageMask = nullptr;
822 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400823 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500824 submitInfo.signalSemaphoreCount = 0;
825 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500826
Jamie Madill21061022018-07-12 23:56:30 -0400827 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500828 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500829
Jamie Madill4c26fc22017-02-24 11:04:10 -0500830 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400831 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400832 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400833 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500834}
835
Jamie Madill0c0dc342017-03-24 14:18:51 -0400836void RendererVk::freeAllInFlightResources()
837{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500838 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400839 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500840 batch.fence.destroy(mDevice);
841 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400842 }
843 mInFlightCommands.clear();
844
845 for (auto &garbage : mGarbage)
846 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400847 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400848 }
849 mGarbage.clear();
850}
851
Jamie Madill21061022018-07-12 23:56:30 -0400852angle::Result RendererVk::checkInFlightCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500853{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500854 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500855
Jamie Madill49ac74b2017-12-21 14:42:33 -0500856 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500857 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500858 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400859 if (result == VK_NOT_READY)
860 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500861
Jamie Madill21061022018-07-12 23:56:30 -0400862 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500863 ASSERT(batch.serial > mLastCompletedQueueSerial);
864 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400865
Jamie Madill49ac74b2017-12-21 14:42:33 -0500866 batch.fence.destroy(mDevice);
867 batch.commandPool.destroy(mDevice);
868 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500869 }
870
Jamie Madill49ac74b2017-12-21 14:42:33 -0500871 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400872
873 size_t freeIndex = 0;
874 for (; freeIndex < mGarbage.size(); ++freeIndex)
875 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500876 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400877 break;
878 }
879
880 // Remove the entries from the garbage list - they should be ready to go.
881 if (freeIndex > 0)
882 {
883 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500884 }
885
Jamie Madill21061022018-07-12 23:56:30 -0400886 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500887}
888
Jamie Madill21061022018-07-12 23:56:30 -0400889angle::Result RendererVk::submitFrame(vk::Context *context,
890 const VkSubmitInfo &submitInfo,
891 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500892{
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400893 VkFenceCreateInfo fenceInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500894 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500895 fenceInfo.flags = 0;
896
Jamie Madillbea35a62018-07-05 11:54:10 -0400897 vk::Scoped<CommandBatch> scopedBatch(mDevice);
898 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400899 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500900
Jamie Madill21061022018-07-12 23:56:30 -0400901 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500902
903 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500904 batch.commandPool = std::move(mCommandPool);
905 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500906
Jamie Madillbea35a62018-07-05 11:54:10 -0400907 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400908
Omar El Sheikh0359b692018-07-23 17:22:00 -0600909 // Check that mInFlightCommands isn't growing too fast
910 // If it is, wait for the queue to complete work it has alread been assigned
911 if (mInFlightCommands.size() > kInFlightCommandsLimit)
912 {
913 vkQueueWaitIdle(mQueue);
914 }
Jamie Madill0c0dc342017-03-24 14:18:51 -0400915
916 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400917 // TODO(jmadill): Overflow check.
918 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400919
Jamie Madill21061022018-07-12 23:56:30 -0400920 ANGLE_TRY(checkInFlightCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400921
Jamie Madill49ac74b2017-12-21 14:42:33 -0500922 // Simply null out the command buffer here - it was allocated using the command pool.
923 commandBuffer.releaseHandle();
924
925 // Reallocate the command pool for next frame.
926 // TODO(jmadill): Consider reusing command pools.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400927 VkCommandPoolCreateInfo poolInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500928 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500929 poolInfo.flags = 0;
930 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
931
Jamie Madill21061022018-07-12 23:56:30 -0400932 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500933
Jamie Madill21061022018-07-12 23:56:30 -0400934 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500935}
936
Jamie Madillaaca96e2018-06-12 10:19:48 -0400937bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500938{
939 return serial > mLastCompletedQueueSerial;
940}
941
Jamie Madill21061022018-07-12 23:56:30 -0400942angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
943 const vk::RenderPassDesc &desc,
944 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500945{
Jamie Madill21061022018-07-12 23:56:30 -0400946 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -0500947 renderPassOut);
948}
949
Jamie Madill21061022018-07-12 23:56:30 -0400950angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
951 const vk::RenderPassDesc &desc,
952 const vk::AttachmentOpsArray &ops,
953 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500954{
Jamie Madill21061022018-07-12 23:56:30 -0400955 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -0500956 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500957}
958
Jamie Madilla5e06072018-05-18 14:36:05 -0400959vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500960{
Jamie Madilla5e06072018-05-18 14:36:05 -0400961 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500962}
963
Jamie Madill21061022018-07-12 23:56:30 -0400964angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500965{
Jamie Madill21061022018-07-12 23:56:30 -0400966 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500967 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500968}
969
Jamie Madill21061022018-07-12 23:56:30 -0400970angle::Result RendererVk::flush(vk::Context *context,
971 const vk::Semaphore &waitSemaphore,
972 const vk::Semaphore &signalSemaphore)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500973{
Jamie Madillbea35a62018-07-05 11:54:10 -0400974 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
975 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500976
977 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
978
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400979 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500980 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500981 submitInfo.waitSemaphoreCount = 1;
982 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
983 submitInfo.pWaitDstStageMask = &waitStageMask;
984 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400985 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500986 submitInfo.signalSemaphoreCount = 1;
987 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
988
Jamie Madill21061022018-07-12 23:56:30 -0400989 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
990 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500991}
992
Jamie Madill78feddc2018-04-27 11:45:05 -0400993Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -0500994{
Jamie Madill78feddc2018-04-27 11:45:05 -0400995 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500996}
997
Jamie Madill06ca6342018-07-12 15:56:53 -0400998angle::Result RendererVk::getPipeline(vk::Context *context,
999 const vk::ShaderAndSerial &vertexShader,
1000 const vk::ShaderAndSerial &fragmentShader,
1001 const vk::PipelineLayout &pipelineLayout,
1002 const vk::PipelineDesc &pipelineDesc,
1003 const gl::AttributesMask &activeAttribLocationsMask,
1004 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -05001005{
Jamie Madill06ca6342018-07-12 15:56:53 -04001006 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -04001007 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -04001008 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -04001009 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
1010
1011 // Pull in a compatible RenderPass.
1012 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001013 ANGLE_TRY(
1014 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -04001015
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001016 return mPipelineCache.getPipeline(context, mPipelineCacheVk, *compatibleRenderPass,
1017 pipelineLayout, activeAttribLocationsMask, vertexShader.get(),
Jamie Madill9aef3672018-04-27 11:45:06 -04001018 fragmentShader.get(), pipelineDesc, pipelineOut);
1019}
1020
Jamie Madill21061022018-07-12 23:56:30 -04001021angle::Result RendererVk::getDescriptorSetLayout(
1022 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001023 const vk::DescriptorSetLayoutDesc &desc,
1024 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
1025{
Jamie Madill21061022018-07-12 23:56:30 -04001026 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -04001027}
1028
Jamie Madill21061022018-07-12 23:56:30 -04001029angle::Result RendererVk::getPipelineLayout(
1030 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001031 const vk::PipelineLayoutDesc &desc,
1032 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
1033 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
1034{
Jamie Madill21061022018-07-12 23:56:30 -04001035 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -04001036 pipelineLayoutOut);
1037}
1038
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001039angle::Result RendererVk::syncPipelineCacheVk(DisplayVk *displayVk)
1040{
1041 ASSERT(mPipelineCacheVk.valid());
1042
1043 if (--mPipelineCacheVkUpdateTimeout > 0)
1044 {
1045 return angle::Result::Continue();
1046 }
1047
1048 mPipelineCacheVkUpdateTimeout = kPipelineCacheVkUpdatePeriod;
1049
1050 // Get the size of the cache.
1051 size_t pipelineCacheSize = 0;
1052 ANGLE_TRY(mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, nullptr));
1053
1054 angle::MemoryBuffer *pipelineCacheData = nullptr;
1055 ANGLE_VK_CHECK_ALLOC(displayVk,
1056 displayVk->getScratchBuffer(pipelineCacheSize, &pipelineCacheData));
1057
1058 size_t originalPipelineCacheSize = pipelineCacheSize;
1059 angle::Result result =
1060 mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, pipelineCacheData->data());
1061 ANGLE_TRY(result);
1062
1063 // Note: currently we don't accept incomplete as we don't expect it (the full size of cache
1064 // was determined just above), so receiving it hints at an implementation bug we would want
1065 // to know about early.
1066 ASSERT(result != angle::Result::Incomplete());
1067
1068 // If vkGetPipelineCacheData ends up writing fewer bytes than requested, zero out the rest of
1069 // the buffer to avoid leaking garbage memory.
1070 ASSERT(pipelineCacheSize <= originalPipelineCacheSize);
1071 if (pipelineCacheSize < originalPipelineCacheSize)
1072 {
1073 memset(pipelineCacheData->data() + pipelineCacheSize, 0,
1074 originalPipelineCacheSize - pipelineCacheSize);
1075 }
1076
1077 displayVk->getBlobCache()->putApplication(mPipelineCacheVkBlobKey, *pipelineCacheData);
1078
1079 return angle::Result::Continue();
1080}
1081
Jamie Madilld47044a2018-04-27 11:45:03 -04001082vk::ShaderLibrary *RendererVk::getShaderLibrary()
1083{
1084 return &mShaderLibrary;
1085}
Luc Ferron90968362018-05-04 08:47:22 -04001086
Jamie Madillaaca96e2018-06-12 10:19:48 -04001087uint32_t GetUniformBufferDescriptorCount()
1088{
1089 return kUniformBufferDescriptorsPerDescriptorSet;
1090}
1091
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001092} // namespace rx