blob: 91ecf3f34c5068273b517439a01d0f982ec899ee [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
Jamie Madill327ba852016-11-30 12:38:28 -0500429 VkApplicationInfo applicationInfo;
430 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
431 applicationInfo.pNext = nullptr;
432 applicationInfo.pApplicationName = "ANGLE";
433 applicationInfo.applicationVersion = 1;
434 applicationInfo.pEngineName = "ANGLE";
435 applicationInfo.engineVersion = 1;
436 applicationInfo.apiVersion = VK_API_VERSION_1_0;
437
438 VkInstanceCreateInfo instanceInfo;
439 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
440 instanceInfo.pNext = nullptr;
441 instanceInfo.flags = 0;
442 instanceInfo.pApplicationInfo = &applicationInfo;
443
Jamie Madille09bd5d2016-11-29 16:20:35 -0500444 // Enable requested layers and extensions.
445 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
446 instanceInfo.ppEnabledExtensionNames =
447 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500448 instanceInfo.enabledLayerCount = enabledLayerCount;
449 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500450
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400451 ANGLE_VK_TRY(displayVk, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500452
Jamie Madill0448ec82016-12-23 13:41:47 -0500453 if (mEnableValidationLayers)
454 {
455 VkDebugReportCallbackCreateInfoEXT debugReportInfo;
456
457 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
458 debugReportInfo.pNext = nullptr;
459 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
460 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
461 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
462 debugReportInfo.pfnCallback = &DebugReportCallback;
463 debugReportInfo.pUserData = this;
464
465 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
466 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
467 ASSERT(createDebugReportCallback);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400468 ANGLE_VK_TRY(displayVk, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
469 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500470 }
471
Jamie Madill4d0bf552016-12-28 15:45:24 -0500472 uint32_t physicalDeviceCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400473 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
474 ANGLE_VK_CHECK(displayVk, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500475
476 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700477 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400478 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
479 physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600480 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700481 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500482
Jamie Madill30b5d842018-08-31 17:19:12 -0400483 vkGetPhysicalDeviceFeatures(mPhysicalDevice, &mPhysicalDeviceFeatures);
484
Jamie Madill4d0bf552016-12-28 15:45:24 -0500485 // Ensure we can find a graphics queue family.
486 uint32_t queueCount = 0;
487 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
488
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400489 ANGLE_VK_CHECK(displayVk, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500490
491 mQueueFamilyProperties.resize(queueCount);
492 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
493 mQueueFamilyProperties.data());
494
495 size_t graphicsQueueFamilyCount = false;
496 uint32_t firstGraphicsQueueFamily = 0;
497 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
498 {
499 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
500 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
501 {
502 ASSERT(queueInfo.queueCount > 0);
503 graphicsQueueFamilyCount++;
504 if (firstGraphicsQueueFamily == 0)
505 {
506 firstGraphicsQueueFamily = familyIndex;
507 }
508 break;
509 }
510 }
511
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400512 ANGLE_VK_CHECK(displayVk, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500513
Jamie Madill12222072018-07-11 14:59:48 -0400514 initFeatures();
515
Jamie Madill4d0bf552016-12-28 15:45:24 -0500516 // If only one queue family, go ahead and initialize the device. If there is more than one
517 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
518 if (graphicsQueueFamilyCount == 1)
519 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400520 ANGLE_TRY(initializeDevice(displayVk, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500521 }
522
Jamie Madill035fd6b2017-10-03 15:43:22 -0400523 // Store the physical device memory properties so we can find the right memory pools.
524 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500525
Jamie Madill06ca6342018-07-12 15:56:53 -0400526 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500527
Jamie Madill6a89d222017-11-02 11:59:51 -0400528 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500529 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
530 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400531
Jamie Madill21061022018-07-12 23:56:30 -0400532 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400533}
534
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400535angle::Result RendererVk::initializeDevice(DisplayVk *displayVk, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500536{
537 uint32_t deviceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400538 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400539 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500540
541 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
542 if (deviceLayerCount > 0)
543 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400544 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
545 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500546 }
547
548 uint32_t deviceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400549 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
550 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500551
552 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
553 if (deviceExtensionCount > 0)
554 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400555 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
556 &deviceExtensionCount,
557 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500558 }
559
Yuly Novikov199f4292018-01-19 19:04:05 -0500560 const char *const *enabledLayerNames = nullptr;
561 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500562 if (mEnableValidationLayers)
563 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500564 mEnableValidationLayers = GetAvailableValidationLayers(
565 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500566 }
567
568 std::vector<const char *> enabledDeviceExtensions;
569 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
570
Luc Ferronbf6dc372018-06-28 15:24:19 -0400571 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
572 if (getFeatures().flipViewportY)
573 {
574 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
575 }
576
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400577 ANGLE_VK_TRY(displayVk, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500578
579 VkDeviceQueueCreateInfo queueCreateInfo;
580
581 float zeroPriority = 0.0f;
582
583 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
584 queueCreateInfo.pNext = nullptr;
585 queueCreateInfo.flags = 0;
586 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
587 queueCreateInfo.queueCount = 1;
588 queueCreateInfo.pQueuePriorities = &zeroPriority;
589
590 // Initialize the device
591 VkDeviceCreateInfo createInfo;
592
Jamie Madill50cf2be2018-06-15 09:46:57 -0400593 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
594 createInfo.pNext = nullptr;
595 createInfo.flags = 0;
596 createInfo.queueCreateInfoCount = 1;
597 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500598 createInfo.enabledLayerCount = enabledLayerCount;
599 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500600 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
601 createInfo.ppEnabledExtensionNames =
602 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
603 createInfo.pEnabledFeatures = nullptr; // TODO(jmadill): features
604
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400605 ANGLE_VK_TRY(displayVk, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500606
607 mCurrentQueueFamilyIndex = queueFamilyIndex;
608
609 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
610
611 // Initialize the command pool now that we know the queue family index.
612 VkCommandPoolCreateInfo commandPoolInfo;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500613 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
614 commandPoolInfo.pNext = nullptr;
615 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500616 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
617
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400618 ANGLE_TRY(mCommandPool.init(displayVk, commandPoolInfo));
619
620 // Initialize the vulkan pipeline cache
621 ANGLE_TRY(initPipelineCacheVk(displayVk));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500622
Jamie Madill21061022018-07-12 23:56:30 -0400623 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500624}
625
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400626angle::Result RendererVk::selectPresentQueueForSurface(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400627 VkSurfaceKHR surface,
628 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500629{
630 // We've already initialized a device, and can't re-create it unless it's never been used.
631 // TODO(jmadill): Handle the re-creation case if necessary.
632 if (mDevice != VK_NULL_HANDLE)
633 {
634 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
635
636 // Check if the current device supports present on this surface.
637 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400638 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400639 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500640 surface, &supportsPresent));
641
Jamie Madill6cad7732018-07-11 09:01:17 -0400642 if (supportsPresent == VK_TRUE)
643 {
644 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400645 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400646 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500647 }
648
649 // Find a graphics and present queue.
650 Optional<uint32_t> newPresentQueue;
651 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
652 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
653 {
654 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
655 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
656 {
657 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400658 ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceSupportKHR(
659 mPhysicalDevice, queueIndex, surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500660
661 if (supportsPresent == VK_TRUE)
662 {
663 newPresentQueue = queueIndex;
664 break;
665 }
666 }
667 }
668
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400669 ANGLE_VK_CHECK(displayVk, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
670 ANGLE_TRY(initializeDevice(displayVk, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500671
Jamie Madill6cad7732018-07-11 09:01:17 -0400672 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400673 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500674}
675
676std::string RendererVk::getVendorString() const
677{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300678 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500679}
680
Jamie Madille09bd5d2016-11-29 16:20:35 -0500681std::string RendererVk::getRendererDescription() const
682{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500683 std::stringstream strstr;
684
685 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
686
687 strstr << "Vulkan ";
688 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
689 strstr << VK_VERSION_MINOR(apiVersion) << ".";
690 strstr << VK_VERSION_PATCH(apiVersion);
691
Olli Etuahoc6a06182018-04-13 14:11:46 +0300692 strstr << "(";
693
694 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
695 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
696 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
697 // driver detection.
698 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
699 {
700 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
701 }
702
703 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500704
705 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500706}
707
Jamie Madill12222072018-07-11 14:59:48 -0400708void RendererVk::initFeatures()
709{
Jamie Madillb36a4812018-09-25 10:15:11 -0400710// Use OpenGL line rasterization rules by default.
711// TODO(jmadill): Fix Android support. http://anglebug.com/2830
712#if defined(ANGLE_PLATFORM_ANDROID)
713 mFeatures.basicGLLineRasterization = false;
714#else
Jamie Madill12222072018-07-11 14:59:48 -0400715 mFeatures.basicGLLineRasterization = true;
Jamie Madillb36a4812018-09-25 10:15:11 -0400716#endif // defined(ANGLE_PLATFORM_ANDROID)
Jamie Madill12222072018-07-11 14:59:48 -0400717
Luc Ferronf786b702018-07-10 11:01:43 -0400718 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
719 // investigation. http://anglebug.com/2728
720 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Frank Henigmanbeb669d2018-09-21 16:25:52 -0400721
722#ifdef ANGLE_PLATFORM_WINDOWS
723 // http://anglebug.com/2838
724 mFeatures.extraCopyBufferRegion = IsIntel(mPhysicalDeviceProperties.vendorID);
725#endif
Jamie Madill12222072018-07-11 14:59:48 -0400726}
727
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400728void RendererVk::initPipelineCacheVkKey()
729{
730 std::ostringstream hashStream("ANGLE Pipeline Cache: ", std::ios_base::ate);
731 // Add the pipeline cache UUID to make sure the blob cache always gives a compatible pipeline
732 // cache. It's not particularly necessary to write it as a hex number as done here, so long as
733 // there is no '\0' in the result.
734 for (const uint32_t c : mPhysicalDeviceProperties.pipelineCacheUUID)
735 {
736 hashStream << std::hex << c;
737 }
738 // Add the vendor and device id too for good measure.
739 hashStream << std::hex << mPhysicalDeviceProperties.vendorID;
740 hashStream << std::hex << mPhysicalDeviceProperties.deviceID;
741
742 const std::string &hashString = hashStream.str();
743 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(hashString.c_str()),
744 hashString.length(), mPipelineCacheVkBlobKey.data());
745}
746
747angle::Result RendererVk::initPipelineCacheVk(DisplayVk *display)
748{
749 initPipelineCacheVkKey();
750
751 egl::BlobCache::Value initialData;
752 bool success = display->getBlobCache()->get(display->getScratchBuffer(),
753 mPipelineCacheVkBlobKey, &initialData);
754
755 VkPipelineCacheCreateInfo pipelineCacheCreateInfo;
756
757 pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
758 pipelineCacheCreateInfo.pNext = nullptr;
759 pipelineCacheCreateInfo.flags = 0;
760 pipelineCacheCreateInfo.initialDataSize = success ? initialData.size() : 0;
761 pipelineCacheCreateInfo.pInitialData = success ? initialData.data() : nullptr;
762
763 ANGLE_TRY(mPipelineCacheVk.init(display, pipelineCacheCreateInfo));
764 return angle::Result::Continue();
765}
766
Jamie Madillacccc6c2016-05-03 17:22:10 -0400767void RendererVk::ensureCapsInitialized() const
768{
769 if (!mCapsInitialized)
770 {
Jamie Madill30b5d842018-08-31 17:19:12 -0400771 vk::GenerateCaps(mPhysicalDeviceProperties, mPhysicalDeviceFeatures, mNativeTextureCaps,
772 &mNativeCaps, &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400773 mCapsInitialized = true;
774 }
775}
776
Jamie Madillacccc6c2016-05-03 17:22:10 -0400777const gl::Caps &RendererVk::getNativeCaps() const
778{
779 ensureCapsInitialized();
780 return mNativeCaps;
781}
782
783const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
784{
785 ensureCapsInitialized();
786 return mNativeTextureCaps;
787}
788
789const gl::Extensions &RendererVk::getNativeExtensions() const
790{
791 ensureCapsInitialized();
792 return mNativeExtensions;
793}
794
795const gl::Limitations &RendererVk::getNativeLimitations() const
796{
797 ensureCapsInitialized();
798 return mNativeLimitations;
799}
800
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400801uint32_t RendererVk::getMaxActiveTextures()
802{
803 // TODO(lucferron): expose this limitation to GL in Context Caps
804 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
805 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
806}
807
Jamie Madill49ac74b2017-12-21 14:42:33 -0500808const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500809{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500810 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500811}
812
Jamie Madill21061022018-07-12 23:56:30 -0400813angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500814{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500815 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500816 {
Luc Ferron1617e692018-07-11 11:08:19 -0400817 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
818 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400819
Jamie Madill49ac74b2017-12-21 14:42:33 -0500820 VkSubmitInfo submitInfo;
821 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
822 submitInfo.pNext = nullptr;
823 submitInfo.waitSemaphoreCount = 0;
824 submitInfo.pWaitSemaphores = nullptr;
825 submitInfo.pWaitDstStageMask = nullptr;
826 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400827 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500828 submitInfo.signalSemaphoreCount = 0;
829 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500830
Jamie Madill21061022018-07-12 23:56:30 -0400831 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500832 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500833
Jamie Madill4c26fc22017-02-24 11:04:10 -0500834 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400835 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400836 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400837 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500838}
839
Jamie Madill0c0dc342017-03-24 14:18:51 -0400840void RendererVk::freeAllInFlightResources()
841{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500842 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400843 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500844 batch.fence.destroy(mDevice);
845 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400846 }
847 mInFlightCommands.clear();
848
849 for (auto &garbage : mGarbage)
850 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400851 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400852 }
853 mGarbage.clear();
854}
855
Jamie Madill21061022018-07-12 23:56:30 -0400856angle::Result RendererVk::checkInFlightCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500857{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500858 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500859
Jamie Madill49ac74b2017-12-21 14:42:33 -0500860 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500861 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500862 VkResult result = batch.fence.getStatus(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400863 if (result == VK_NOT_READY)
864 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500865
Jamie Madill21061022018-07-12 23:56:30 -0400866 ANGLE_VK_TRY(context, result);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500867 ASSERT(batch.serial > mLastCompletedQueueSerial);
868 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400869
Jamie Madill49ac74b2017-12-21 14:42:33 -0500870 batch.fence.destroy(mDevice);
871 batch.commandPool.destroy(mDevice);
872 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500873 }
874
Jamie Madill49ac74b2017-12-21 14:42:33 -0500875 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400876
877 size_t freeIndex = 0;
878 for (; freeIndex < mGarbage.size(); ++freeIndex)
879 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500880 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400881 break;
882 }
883
884 // Remove the entries from the garbage list - they should be ready to go.
885 if (freeIndex > 0)
886 {
887 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500888 }
889
Jamie Madill21061022018-07-12 23:56:30 -0400890 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500891}
892
Jamie Madill21061022018-07-12 23:56:30 -0400893angle::Result RendererVk::submitFrame(vk::Context *context,
894 const VkSubmitInfo &submitInfo,
895 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500896{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500897 VkFenceCreateInfo fenceInfo;
898 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
899 fenceInfo.pNext = nullptr;
900 fenceInfo.flags = 0;
901
Jamie Madillbea35a62018-07-05 11:54:10 -0400902 vk::Scoped<CommandBatch> scopedBatch(mDevice);
903 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400904 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500905
Jamie Madill21061022018-07-12 23:56:30 -0400906 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500907
908 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500909 batch.commandPool = std::move(mCommandPool);
910 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500911
Jamie Madillbea35a62018-07-05 11:54:10 -0400912 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400913
Omar El Sheikh0359b692018-07-23 17:22:00 -0600914 // Check that mInFlightCommands isn't growing too fast
915 // If it is, wait for the queue to complete work it has alread been assigned
916 if (mInFlightCommands.size() > kInFlightCommandsLimit)
917 {
918 vkQueueWaitIdle(mQueue);
919 }
Jamie Madill0c0dc342017-03-24 14:18:51 -0400920
921 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400922 // TODO(jmadill): Overflow check.
923 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400924
Jamie Madill21061022018-07-12 23:56:30 -0400925 ANGLE_TRY(checkInFlightCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400926
Jamie Madill49ac74b2017-12-21 14:42:33 -0500927 // Simply null out the command buffer here - it was allocated using the command pool.
928 commandBuffer.releaseHandle();
929
930 // Reallocate the command pool for next frame.
931 // TODO(jmadill): Consider reusing command pools.
932 VkCommandPoolCreateInfo poolInfo;
933 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
934 poolInfo.pNext = nullptr;
935 poolInfo.flags = 0;
936 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
937
Jamie Madill21061022018-07-12 23:56:30 -0400938 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500939
Jamie Madill21061022018-07-12 23:56:30 -0400940 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500941}
942
Jamie Madillaaca96e2018-06-12 10:19:48 -0400943bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500944{
945 return serial > mLastCompletedQueueSerial;
946}
947
Jamie Madill21061022018-07-12 23:56:30 -0400948angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
949 const vk::RenderPassDesc &desc,
950 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500951{
Jamie Madill21061022018-07-12 23:56:30 -0400952 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -0500953 renderPassOut);
954}
955
Jamie Madill21061022018-07-12 23:56:30 -0400956angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
957 const vk::RenderPassDesc &desc,
958 const vk::AttachmentOpsArray &ops,
959 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -0500960{
Jamie Madill21061022018-07-12 23:56:30 -0400961 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -0500962 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500963}
964
Jamie Madilla5e06072018-05-18 14:36:05 -0400965vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -0500966{
Jamie Madilla5e06072018-05-18 14:36:05 -0400967 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500968}
969
Jamie Madill21061022018-07-12 23:56:30 -0400970angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500971{
Jamie Madill21061022018-07-12 23:56:30 -0400972 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500973 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -0500974}
975
Jamie Madill21061022018-07-12 23:56:30 -0400976angle::Result RendererVk::flush(vk::Context *context,
977 const vk::Semaphore &waitSemaphore,
978 const vk::Semaphore &signalSemaphore)
Jamie Madill49ac74b2017-12-21 14:42:33 -0500979{
Jamie Madillbea35a62018-07-05 11:54:10 -0400980 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
981 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500982
983 VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
984
985 VkSubmitInfo submitInfo;
986 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
987 submitInfo.pNext = nullptr;
988 submitInfo.waitSemaphoreCount = 1;
989 submitInfo.pWaitSemaphores = waitSemaphore.ptr();
990 submitInfo.pWaitDstStageMask = &waitStageMask;
991 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -0400992 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500993 submitInfo.signalSemaphoreCount = 1;
994 submitInfo.pSignalSemaphores = signalSemaphore.ptr();
995
Jamie Madill21061022018-07-12 23:56:30 -0400996 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
997 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500998}
999
Jamie Madill78feddc2018-04-27 11:45:05 -04001000Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -05001001{
Jamie Madill78feddc2018-04-27 11:45:05 -04001002 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -05001003}
1004
Jamie Madill06ca6342018-07-12 15:56:53 -04001005angle::Result RendererVk::getPipeline(vk::Context *context,
1006 const vk::ShaderAndSerial &vertexShader,
1007 const vk::ShaderAndSerial &fragmentShader,
1008 const vk::PipelineLayout &pipelineLayout,
1009 const vk::PipelineDesc &pipelineDesc,
1010 const gl::AttributesMask &activeAttribLocationsMask,
1011 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -05001012{
Jamie Madill06ca6342018-07-12 15:56:53 -04001013 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -04001014 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -04001015 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -04001016 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
1017
1018 // Pull in a compatible RenderPass.
1019 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001020 ANGLE_TRY(
1021 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -04001022
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001023 return mPipelineCache.getPipeline(context, mPipelineCacheVk, *compatibleRenderPass,
1024 pipelineLayout, activeAttribLocationsMask, vertexShader.get(),
Jamie Madill9aef3672018-04-27 11:45:06 -04001025 fragmentShader.get(), pipelineDesc, pipelineOut);
1026}
1027
Jamie Madill21061022018-07-12 23:56:30 -04001028angle::Result RendererVk::getDescriptorSetLayout(
1029 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001030 const vk::DescriptorSetLayoutDesc &desc,
1031 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
1032{
Jamie Madill21061022018-07-12 23:56:30 -04001033 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -04001034}
1035
Jamie Madill21061022018-07-12 23:56:30 -04001036angle::Result RendererVk::getPipelineLayout(
1037 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001038 const vk::PipelineLayoutDesc &desc,
1039 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
1040 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
1041{
Jamie Madill21061022018-07-12 23:56:30 -04001042 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -04001043 pipelineLayoutOut);
1044}
1045
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001046angle::Result RendererVk::syncPipelineCacheVk(DisplayVk *displayVk)
1047{
1048 ASSERT(mPipelineCacheVk.valid());
1049
1050 if (--mPipelineCacheVkUpdateTimeout > 0)
1051 {
1052 return angle::Result::Continue();
1053 }
1054
1055 mPipelineCacheVkUpdateTimeout = kPipelineCacheVkUpdatePeriod;
1056
1057 // Get the size of the cache.
1058 size_t pipelineCacheSize = 0;
1059 ANGLE_TRY(mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, nullptr));
1060
1061 angle::MemoryBuffer *pipelineCacheData = nullptr;
1062 ANGLE_VK_CHECK_ALLOC(displayVk,
1063 displayVk->getScratchBuffer(pipelineCacheSize, &pipelineCacheData));
1064
1065 size_t originalPipelineCacheSize = pipelineCacheSize;
1066 angle::Result result =
1067 mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, pipelineCacheData->data());
1068 ANGLE_TRY(result);
1069
1070 // Note: currently we don't accept incomplete as we don't expect it (the full size of cache
1071 // was determined just above), so receiving it hints at an implementation bug we would want
1072 // to know about early.
1073 ASSERT(result != angle::Result::Incomplete());
1074
1075 // If vkGetPipelineCacheData ends up writing fewer bytes than requested, zero out the rest of
1076 // the buffer to avoid leaking garbage memory.
1077 ASSERT(pipelineCacheSize <= originalPipelineCacheSize);
1078 if (pipelineCacheSize < originalPipelineCacheSize)
1079 {
1080 memset(pipelineCacheData->data() + pipelineCacheSize, 0,
1081 originalPipelineCacheSize - pipelineCacheSize);
1082 }
1083
1084 displayVk->getBlobCache()->putApplication(mPipelineCacheVkBlobKey, *pipelineCacheData);
1085
1086 return angle::Result::Continue();
1087}
1088
Jamie Madilld47044a2018-04-27 11:45:03 -04001089vk::ShaderLibrary *RendererVk::getShaderLibrary()
1090{
1091 return &mShaderLibrary;
1092}
Luc Ferron90968362018-05-04 08:47:22 -04001093
Jamie Madillaaca96e2018-06-12 10:19:48 -04001094uint32_t GetUniformBufferDescriptorCount()
1095{
1096 return kUniformBufferDescriptorsPerDescriptorSet;
1097}
1098
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001099} // namespace rx