blob: e14b8779ca27bd2521df625c4510e44a6d288ed4 [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);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400329 mSubmitSemaphorePool.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400330 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500331
Jamie Madill06ca6342018-07-12 15:56:53 -0400332 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500333
Jamie Madill5deea722017-02-16 10:44:46 -0500334 if (mCommandPool.valid())
335 {
336 mCommandPool.destroy(mDevice);
337 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500338
339 if (mDevice)
340 {
341 vkDestroyDevice(mDevice, nullptr);
342 mDevice = VK_NULL_HANDLE;
343 }
344
Jamie Madill0448ec82016-12-23 13:41:47 -0500345 if (mDebugReportCallback)
346 {
347 ASSERT(mInstance);
348 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
349 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
350 ASSERT(destroyDebugReportCallback);
351 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
352 }
353
Jamie Madill4d0bf552016-12-28 15:45:24 -0500354 if (mInstance)
355 {
356 vkDestroyInstance(mInstance, nullptr);
357 mInstance = VK_NULL_HANDLE;
358 }
359
Omar El Sheikheb4b8692018-07-17 10:55:40 -0600360 mMemoryProperties.destroy();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500361 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500362}
363
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400364void RendererVk::markDeviceLost()
365{
366 mDeviceLost = true;
367}
368
369bool RendererVk::isDeviceLost() const
370{
371 return mDeviceLost;
372}
373
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400374angle::Result RendererVk::initialize(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400375 const egl::AttributeMap &attribs,
376 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500377{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600378 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
379 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500380 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600381 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500382
Jamie Madill0448ec82016-12-23 13:41:47 -0500383 // Gather global layer properties.
384 uint32_t instanceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400385 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500386
387 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
388 if (instanceLayerCount > 0)
389 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400390 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
391 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500392 }
393
Jamie Madille09bd5d2016-11-29 16:20:35 -0500394 uint32_t instanceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400395 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400396 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500397
398 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
399 if (instanceExtensionCount > 0)
400 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400401 ANGLE_VK_TRY(displayVk,
402 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
403 instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500404 }
405
Yuly Novikov199f4292018-01-19 19:04:05 -0500406 const char *const *enabledLayerNames = nullptr;
407 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500408 if (mEnableValidationLayers)
409 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500410 bool layersRequested =
411 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
412 mEnableValidationLayers = GetAvailableValidationLayers(
413 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500414 }
415
Jamie Madille09bd5d2016-11-29 16:20:35 -0500416 std::vector<const char *> enabledInstanceExtensions;
417 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500418 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500419
Jamie Madill0448ec82016-12-23 13:41:47 -0500420 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
421 if (mEnableValidationLayers)
422 {
423 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
424 }
425
Jamie Madille09bd5d2016-11-29 16:20:35 -0500426 // Verify the required extensions are in the extension names set. Fail if not.
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400427 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400428 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500429
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400430 VkApplicationInfo applicationInfo = {};
Jamie Madill327ba852016-11-30 12:38:28 -0500431 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
Jamie Madill327ba852016-11-30 12:38:28 -0500432 applicationInfo.pApplicationName = "ANGLE";
433 applicationInfo.applicationVersion = 1;
434 applicationInfo.pEngineName = "ANGLE";
435 applicationInfo.engineVersion = 1;
436 applicationInfo.apiVersion = VK_API_VERSION_1_0;
437
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400438 VkInstanceCreateInfo instanceInfo = {};
Jamie Madill327ba852016-11-30 12:38:28 -0500439 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jamie Madill327ba852016-11-30 12:38:28 -0500440 instanceInfo.flags = 0;
441 instanceInfo.pApplicationInfo = &applicationInfo;
442
Jamie Madille09bd5d2016-11-29 16:20:35 -0500443 // Enable requested layers and extensions.
444 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
445 instanceInfo.ppEnabledExtensionNames =
446 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500447 instanceInfo.enabledLayerCount = enabledLayerCount;
448 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500449
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400450 ANGLE_VK_TRY(displayVk, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500451
Jamie Madill0448ec82016-12-23 13:41:47 -0500452 if (mEnableValidationLayers)
453 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400454 VkDebugReportCallbackCreateInfoEXT debugReportInfo = {};
Jamie Madill0448ec82016-12-23 13:41:47 -0500455
456 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Jamie Madill0448ec82016-12-23 13:41:47 -0500457 debugReportInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT |
458 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT |
459 VK_DEBUG_REPORT_INFORMATION_BIT_EXT | VK_DEBUG_REPORT_DEBUG_BIT_EXT;
460 debugReportInfo.pfnCallback = &DebugReportCallback;
461 debugReportInfo.pUserData = this;
462
463 auto createDebugReportCallback = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
464 vkGetInstanceProcAddr(mInstance, "vkCreateDebugReportCallbackEXT"));
465 ASSERT(createDebugReportCallback);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400466 ANGLE_VK_TRY(displayVk, createDebugReportCallback(mInstance, &debugReportInfo, nullptr,
467 &mDebugReportCallback));
Jamie Madill0448ec82016-12-23 13:41:47 -0500468 }
469
Jamie Madill4d0bf552016-12-28 15:45:24 -0500470 uint32_t physicalDeviceCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400471 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount, nullptr));
472 ANGLE_VK_CHECK(displayVk, physicalDeviceCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500473
474 // TODO(jmadill): Handle multiple physical devices. For now, use the first device.
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700475 std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400476 ANGLE_VK_TRY(displayVk, vkEnumeratePhysicalDevices(mInstance, &physicalDeviceCount,
477 physicalDevices.data()));
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600478 ChoosePhysicalDevice(physicalDevices, enableMockICD, &mPhysicalDevice,
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700479 &mPhysicalDeviceProperties);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500480
Jamie Madill30b5d842018-08-31 17:19:12 -0400481 vkGetPhysicalDeviceFeatures(mPhysicalDevice, &mPhysicalDeviceFeatures);
482
Jamie Madill4d0bf552016-12-28 15:45:24 -0500483 // Ensure we can find a graphics queue family.
484 uint32_t queueCount = 0;
485 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount, nullptr);
486
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400487 ANGLE_VK_CHECK(displayVk, queueCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500488
489 mQueueFamilyProperties.resize(queueCount);
490 vkGetPhysicalDeviceQueueFamilyProperties(mPhysicalDevice, &queueCount,
491 mQueueFamilyProperties.data());
492
493 size_t graphicsQueueFamilyCount = false;
494 uint32_t firstGraphicsQueueFamily = 0;
495 for (uint32_t familyIndex = 0; familyIndex < queueCount; ++familyIndex)
496 {
497 const auto &queueInfo = mQueueFamilyProperties[familyIndex];
498 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
499 {
500 ASSERT(queueInfo.queueCount > 0);
501 graphicsQueueFamilyCount++;
502 if (firstGraphicsQueueFamily == 0)
503 {
504 firstGraphicsQueueFamily = familyIndex;
505 }
506 break;
507 }
508 }
509
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400510 ANGLE_VK_CHECK(displayVk, graphicsQueueFamilyCount > 0, VK_ERROR_INITIALIZATION_FAILED);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500511
Jamie Madill12222072018-07-11 14:59:48 -0400512 initFeatures();
513
Jamie Madill4d0bf552016-12-28 15:45:24 -0500514 // If only one queue family, go ahead and initialize the device. If there is more than one
515 // queue, we'll have to wait until we see a WindowSurface to know which supports present.
516 if (graphicsQueueFamilyCount == 1)
517 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400518 ANGLE_TRY(initializeDevice(displayVk, firstGraphicsQueueFamily));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500519 }
520
Jamie Madill035fd6b2017-10-03 15:43:22 -0400521 // Store the physical device memory properties so we can find the right memory pools.
522 mMemoryProperties.init(mPhysicalDevice);
Jamie Madill7b57b9d2017-01-13 09:33:38 -0500523
Jamie Madill06ca6342018-07-12 15:56:53 -0400524 GlslangWrapper::Initialize();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500525
Jamie Madill6a89d222017-11-02 11:59:51 -0400526 // Initialize the format table.
Luc Ferrond50537a2018-02-07 17:02:08 -0500527 mFormatTable.initialize(mPhysicalDevice, &mNativeTextureCaps,
528 &mNativeCaps.compressedTextureFormats);
Jamie Madill6a89d222017-11-02 11:59:51 -0400529
Jamie Madill21061022018-07-12 23:56:30 -0400530 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400531}
532
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400533angle::Result RendererVk::initializeDevice(DisplayVk *displayVk, uint32_t queueFamilyIndex)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500534{
535 uint32_t deviceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400536 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400537 vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500538
539 std::vector<VkLayerProperties> deviceLayerProps(deviceLayerCount);
540 if (deviceLayerCount > 0)
541 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400542 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceLayerProperties(mPhysicalDevice, &deviceLayerCount,
543 deviceLayerProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500544 }
545
546 uint32_t deviceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400547 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
548 &deviceExtensionCount, nullptr));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500549
550 std::vector<VkExtensionProperties> deviceExtensionProps(deviceExtensionCount);
551 if (deviceExtensionCount > 0)
552 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400553 ANGLE_VK_TRY(displayVk, vkEnumerateDeviceExtensionProperties(mPhysicalDevice, nullptr,
554 &deviceExtensionCount,
555 deviceExtensionProps.data()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500556 }
557
Yuly Novikov199f4292018-01-19 19:04:05 -0500558 const char *const *enabledLayerNames = nullptr;
559 uint32_t enabledLayerCount = 0;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500560 if (mEnableValidationLayers)
561 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500562 mEnableValidationLayers = GetAvailableValidationLayers(
563 deviceLayerProps, false, &enabledLayerNames, &enabledLayerCount);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500564 }
565
566 std::vector<const char *> enabledDeviceExtensions;
567 enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
568
Luc Ferronbf6dc372018-06-28 15:24:19 -0400569 // Selectively enable KHR_MAINTENANCE1 to support viewport flipping.
570 if (getFeatures().flipViewportY)
571 {
572 enabledDeviceExtensions.push_back(VK_KHR_MAINTENANCE1_EXTENSION_NAME);
573 }
574
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400575 ANGLE_VK_TRY(displayVk, VerifyExtensionsPresent(deviceExtensionProps, enabledDeviceExtensions));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500576
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400577 // Select additional features to be enabled
578 VkPhysicalDeviceFeatures enabledFeatures = {};
579 enabledFeatures.inheritedQueries = mPhysicalDeviceFeatures.inheritedQueries;
580
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400581 VkDeviceQueueCreateInfo queueCreateInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500582
583 float zeroPriority = 0.0f;
584
585 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500586 queueCreateInfo.flags = 0;
587 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
588 queueCreateInfo.queueCount = 1;
589 queueCreateInfo.pQueuePriorities = &zeroPriority;
590
591 // Initialize the device
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400592 VkDeviceCreateInfo createInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500593
Jamie Madill50cf2be2018-06-15 09:46:57 -0400594 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400595 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();
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400603 createInfo.pEnabledFeatures = &enabledFeatures;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500604
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.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400612 VkCommandPoolCreateInfo commandPoolInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500613 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500614 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500615 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
616
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400617 ANGLE_TRY(mCommandPool.init(displayVk, commandPoolInfo));
618
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400619 // Initialize the vulkan pipeline cache.
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400620 ANGLE_TRY(initPipelineCacheVk(displayVk));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500621
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400622 // Initialize the submission semaphore pool.
623 ANGLE_TRY(mSubmitSemaphorePool.init(displayVk, vk::kDefaultSemaphorePoolSize));
624
Jamie Madill21061022018-07-12 23:56:30 -0400625 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500626}
627
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400628angle::Result RendererVk::selectPresentQueueForSurface(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400629 VkSurfaceKHR surface,
630 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500631{
632 // We've already initialized a device, and can't re-create it unless it's never been used.
633 // TODO(jmadill): Handle the re-creation case if necessary.
634 if (mDevice != VK_NULL_HANDLE)
635 {
636 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
637
638 // Check if the current device supports present on this surface.
639 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400640 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400641 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500642 surface, &supportsPresent));
643
Jamie Madill6cad7732018-07-11 09:01:17 -0400644 if (supportsPresent == VK_TRUE)
645 {
646 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400647 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400648 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500649 }
650
651 // Find a graphics and present queue.
652 Optional<uint32_t> newPresentQueue;
653 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
654 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
655 {
656 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
657 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
658 {
659 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400660 ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceSupportKHR(
661 mPhysicalDevice, queueIndex, surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500662
663 if (supportsPresent == VK_TRUE)
664 {
665 newPresentQueue = queueIndex;
666 break;
667 }
668 }
669 }
670
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400671 ANGLE_VK_CHECK(displayVk, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
672 ANGLE_TRY(initializeDevice(displayVk, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500673
Jamie Madill6cad7732018-07-11 09:01:17 -0400674 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400675 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500676}
677
678std::string RendererVk::getVendorString() const
679{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300680 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500681}
682
Jamie Madille09bd5d2016-11-29 16:20:35 -0500683std::string RendererVk::getRendererDescription() const
684{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500685 std::stringstream strstr;
686
687 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
688
689 strstr << "Vulkan ";
690 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
691 strstr << VK_VERSION_MINOR(apiVersion) << ".";
692 strstr << VK_VERSION_PATCH(apiVersion);
693
Olli Etuahoc6a06182018-04-13 14:11:46 +0300694 strstr << "(";
695
696 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
697 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
698 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
699 // driver detection.
700 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
701 {
702 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
703 }
704
705 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500706
707 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500708}
709
Jamie Madill12222072018-07-11 14:59:48 -0400710void RendererVk::initFeatures()
711{
Jamie Madillb36a4812018-09-25 10:15:11 -0400712// Use OpenGL line rasterization rules by default.
713// TODO(jmadill): Fix Android support. http://anglebug.com/2830
714#if defined(ANGLE_PLATFORM_ANDROID)
715 mFeatures.basicGLLineRasterization = false;
716#else
Jamie Madill12222072018-07-11 14:59:48 -0400717 mFeatures.basicGLLineRasterization = true;
Jamie Madillb36a4812018-09-25 10:15:11 -0400718#endif // defined(ANGLE_PLATFORM_ANDROID)
Jamie Madill12222072018-07-11 14:59:48 -0400719
Luc Ferronf786b702018-07-10 11:01:43 -0400720 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
721 // investigation. http://anglebug.com/2728
722 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Frank Henigmanbeb669d2018-09-21 16:25:52 -0400723
724#ifdef ANGLE_PLATFORM_WINDOWS
725 // http://anglebug.com/2838
726 mFeatures.extraCopyBufferRegion = IsIntel(mPhysicalDeviceProperties.vendorID);
727#endif
Jamie Madill12222072018-07-11 14:59:48 -0400728}
729
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400730void RendererVk::initPipelineCacheVkKey()
731{
732 std::ostringstream hashStream("ANGLE Pipeline Cache: ", std::ios_base::ate);
733 // Add the pipeline cache UUID to make sure the blob cache always gives a compatible pipeline
734 // cache. It's not particularly necessary to write it as a hex number as done here, so long as
735 // there is no '\0' in the result.
736 for (const uint32_t c : mPhysicalDeviceProperties.pipelineCacheUUID)
737 {
738 hashStream << std::hex << c;
739 }
740 // Add the vendor and device id too for good measure.
741 hashStream << std::hex << mPhysicalDeviceProperties.vendorID;
742 hashStream << std::hex << mPhysicalDeviceProperties.deviceID;
743
744 const std::string &hashString = hashStream.str();
745 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(hashString.c_str()),
746 hashString.length(), mPipelineCacheVkBlobKey.data());
747}
748
749angle::Result RendererVk::initPipelineCacheVk(DisplayVk *display)
750{
751 initPipelineCacheVkKey();
752
753 egl::BlobCache::Value initialData;
754 bool success = display->getBlobCache()->get(display->getScratchBuffer(),
755 mPipelineCacheVkBlobKey, &initialData);
756
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400757 VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {};
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400758
759 pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400760 pipelineCacheCreateInfo.flags = 0;
761 pipelineCacheCreateInfo.initialDataSize = success ? initialData.size() : 0;
762 pipelineCacheCreateInfo.pInitialData = success ? initialData.data() : nullptr;
763
764 ANGLE_TRY(mPipelineCacheVk.init(display, pipelineCacheCreateInfo));
765 return angle::Result::Continue();
766}
767
Jamie Madillacccc6c2016-05-03 17:22:10 -0400768void RendererVk::ensureCapsInitialized() const
769{
770 if (!mCapsInitialized)
771 {
Jamie Madill30b5d842018-08-31 17:19:12 -0400772 vk::GenerateCaps(mPhysicalDeviceProperties, mPhysicalDeviceFeatures, mNativeTextureCaps,
773 &mNativeCaps, &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400774 mCapsInitialized = true;
775 }
776}
777
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400778void RendererVk::getSubmitWaitSemaphores(
779 vk::Context *context,
780 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> *waitSemaphores)
781{
782 if (mSubmitLastSignaledSemaphore.getSemaphore())
783 {
784 waitSemaphores->push_back(mSubmitLastSignaledSemaphore.getSemaphore()->getHandle());
785
786 // Return the semaphore to the pool (which will remain valid and unused until the
787 // queue it's about to be waited on has finished execution).
788 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
789 }
790
791 for (vk::SemaphoreHelper &semaphore : mSubmitWaitSemaphores)
792 {
793 waitSemaphores->push_back(semaphore.getSemaphore()->getHandle());
794 mSubmitSemaphorePool.freeSemaphore(context, &semaphore);
795 }
796 mSubmitWaitSemaphores.clear();
797}
798
Jamie Madillacccc6c2016-05-03 17:22:10 -0400799const gl::Caps &RendererVk::getNativeCaps() const
800{
801 ensureCapsInitialized();
802 return mNativeCaps;
803}
804
805const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
806{
807 ensureCapsInitialized();
808 return mNativeTextureCaps;
809}
810
811const gl::Extensions &RendererVk::getNativeExtensions() const
812{
813 ensureCapsInitialized();
814 return mNativeExtensions;
815}
816
817const gl::Limitations &RendererVk::getNativeLimitations() const
818{
819 ensureCapsInitialized();
820 return mNativeLimitations;
821}
822
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400823uint32_t RendererVk::getMaxActiveTextures()
824{
825 // TODO(lucferron): expose this limitation to GL in Context Caps
826 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
827 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
828}
829
Jamie Madill49ac74b2017-12-21 14:42:33 -0500830const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500831{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500832 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500833}
834
Jamie Madill21061022018-07-12 23:56:30 -0400835angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500836{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500837 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500838 {
Luc Ferron1617e692018-07-11 11:08:19 -0400839 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
840 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400841
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400842 VkPipelineStageFlags waitStageMask[2] = {
843 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
844 };
845
846 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
847 getSubmitWaitSemaphores(context, &waitSemaphores);
848
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400849 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500850 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400851 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
852 submitInfo.pWaitSemaphores = waitSemaphores.data();
853 submitInfo.pWaitDstStageMask = waitStageMask;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500854 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400855 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500856 submitInfo.signalSemaphoreCount = 0;
857 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500858
Jamie Madill21061022018-07-12 23:56:30 -0400859 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500860 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500861
Jamie Madill4c26fc22017-02-24 11:04:10 -0500862 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400863 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400864 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400865 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500866}
867
Jamie Madill0c0dc342017-03-24 14:18:51 -0400868void RendererVk::freeAllInFlightResources()
869{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500870 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400871 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500872 batch.fence.destroy(mDevice);
873 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400874 }
875 mInFlightCommands.clear();
876
877 for (auto &garbage : mGarbage)
878 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400879 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400880 }
881 mGarbage.clear();
882}
883
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400884angle::Result RendererVk::checkCompletedCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500885{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500886 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500887
Jamie Madill49ac74b2017-12-21 14:42:33 -0500888 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500889 {
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400890 angle::Result result = batch.fence.getStatus(context);
891 ANGLE_TRY(result);
892 if (result == angle::Result::Incomplete())
Jamie Madill0c0dc342017-03-24 14:18:51 -0400893 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500894
Jamie Madill49ac74b2017-12-21 14:42:33 -0500895 ASSERT(batch.serial > mLastCompletedQueueSerial);
896 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400897
Jamie Madill49ac74b2017-12-21 14:42:33 -0500898 batch.fence.destroy(mDevice);
899 batch.commandPool.destroy(mDevice);
900 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500901 }
902
Jamie Madill49ac74b2017-12-21 14:42:33 -0500903 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400904
905 size_t freeIndex = 0;
906 for (; freeIndex < mGarbage.size(); ++freeIndex)
907 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500908 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400909 break;
910 }
911
912 // Remove the entries from the garbage list - they should be ready to go.
913 if (freeIndex > 0)
914 {
915 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500916 }
917
Jamie Madill21061022018-07-12 23:56:30 -0400918 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500919}
920
Jamie Madill21061022018-07-12 23:56:30 -0400921angle::Result RendererVk::submitFrame(vk::Context *context,
922 const VkSubmitInfo &submitInfo,
923 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500924{
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400925 VkFenceCreateInfo fenceInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500926 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500927 fenceInfo.flags = 0;
928
Jamie Madillbea35a62018-07-05 11:54:10 -0400929 vk::Scoped<CommandBatch> scopedBatch(mDevice);
930 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400931 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500932
Jamie Madill21061022018-07-12 23:56:30 -0400933 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500934
935 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500936 batch.commandPool = std::move(mCommandPool);
937 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500938
Jamie Madillbea35a62018-07-05 11:54:10 -0400939 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400940
Omar El Sheikh0359b692018-07-23 17:22:00 -0600941 // Check that mInFlightCommands isn't growing too fast
942 // If it is, wait for the queue to complete work it has alread been assigned
943 if (mInFlightCommands.size() > kInFlightCommandsLimit)
944 {
945 vkQueueWaitIdle(mQueue);
946 }
Jamie Madill0c0dc342017-03-24 14:18:51 -0400947
948 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400949 // TODO(jmadill): Overflow check.
950 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400951
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400952 ANGLE_TRY(checkCompletedCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400953
Jamie Madill49ac74b2017-12-21 14:42:33 -0500954 // Simply null out the command buffer here - it was allocated using the command pool.
955 commandBuffer.releaseHandle();
956
957 // Reallocate the command pool for next frame.
958 // TODO(jmadill): Consider reusing command pools.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400959 VkCommandPoolCreateInfo poolInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500960 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500961 poolInfo.flags = 0;
962 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
963
Jamie Madill21061022018-07-12 23:56:30 -0400964 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500965
Jamie Madill21061022018-07-12 23:56:30 -0400966 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500967}
968
Jamie Madillaaca96e2018-06-12 10:19:48 -0400969bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500970{
971 return serial > mLastCompletedQueueSerial;
972}
973
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400974angle::Result RendererVk::finishToSerial(vk::Context *context, Serial serial)
975{
976 if (!isSerialInUse(serial) || mInFlightCommands.empty())
977 {
978 return angle::Result::Continue();
979 }
980
981 // Find the first batch with serial equal to or bigger than given serial (note that
982 // the batch serials are unique, otherwise upper-bound would have been necessary).
983 size_t batchIndex = mInFlightCommands.size() - 1;
984 for (size_t i = 0; i < mInFlightCommands.size(); ++i)
985 {
986 if (mInFlightCommands[i].serial >= serial)
987 {
988 batchIndex = i;
989 break;
990 }
991 }
992 const CommandBatch &batch = mInFlightCommands[batchIndex];
993
994 // Wait for it finish
995 constexpr uint64_t kMaxFenceWaitTimeNs = 10'000'000'000llu;
996 angle::Result result = batch.fence.wait(context, kMaxFenceWaitTimeNs);
997 if (result == angle::Result::Incomplete())
998 {
999 // Wait a maximum of 10s. If that times out, we declare it a failure.
1000 result = angle::Result::Stop();
1001 }
1002 ANGLE_TRY(result);
1003
1004 // Clean up finished batches.
1005 return checkCompletedCommands(context);
1006}
1007
Jamie Madill21061022018-07-12 23:56:30 -04001008angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
1009 const vk::RenderPassDesc &desc,
1010 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001011{
Jamie Madill21061022018-07-12 23:56:30 -04001012 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -05001013 renderPassOut);
1014}
1015
Jamie Madill21061022018-07-12 23:56:30 -04001016angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
1017 const vk::RenderPassDesc &desc,
1018 const vk::AttachmentOpsArray &ops,
1019 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001020{
Jamie Madill21061022018-07-12 23:56:30 -04001021 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -05001022 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -05001023}
1024
Jamie Madilla5e06072018-05-18 14:36:05 -04001025vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -05001026{
Jamie Madilla5e06072018-05-18 14:36:05 -04001027 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001028}
1029
Jamie Madill21061022018-07-12 23:56:30 -04001030angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001031{
Jamie Madill21061022018-07-12 23:56:30 -04001032 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -05001033 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001034}
1035
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001036angle::Result RendererVk::flush(vk::Context *context)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001037{
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001038 if (mCommandGraph.empty())
1039 {
1040 return angle::Result::Continue();
1041 }
1042
Jamie Madillbea35a62018-07-05 11:54:10 -04001043 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1044 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001045
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001046 VkPipelineStageFlags waitStageMask[2] = {
1047 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1048 };
1049
1050 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
1051 getSubmitWaitSemaphores(context, &waitSemaphores);
1052
1053 // On every flush, create a semaphore to be signaled. On the next submission, this semaphore
1054 // will be waited on.
1055 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &mSubmitLastSignaledSemaphore));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001056
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001057 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -05001058 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001059 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1060 submitInfo.pWaitSemaphores = waitSemaphores.data();
1061 submitInfo.pWaitDstStageMask = waitStageMask;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001062 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -04001063 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001064 submitInfo.signalSemaphoreCount = 1;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001065 submitInfo.pSignalSemaphores = mSubmitLastSignaledSemaphore.getSemaphore()->ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001066
Jamie Madill21061022018-07-12 23:56:30 -04001067 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001068
Jamie Madill21061022018-07-12 23:56:30 -04001069 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001070}
1071
Jamie Madill78feddc2018-04-27 11:45:05 -04001072Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -05001073{
Jamie Madill78feddc2018-04-27 11:45:05 -04001074 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -05001075}
1076
Jamie Madill06ca6342018-07-12 15:56:53 -04001077angle::Result RendererVk::getPipeline(vk::Context *context,
1078 const vk::ShaderAndSerial &vertexShader,
1079 const vk::ShaderAndSerial &fragmentShader,
1080 const vk::PipelineLayout &pipelineLayout,
1081 const vk::PipelineDesc &pipelineDesc,
1082 const gl::AttributesMask &activeAttribLocationsMask,
1083 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -05001084{
Jamie Madill06ca6342018-07-12 15:56:53 -04001085 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -04001086 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -04001087 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -04001088 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
1089
1090 // Pull in a compatible RenderPass.
1091 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001092 ANGLE_TRY(
1093 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -04001094
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001095 return mPipelineCache.getPipeline(context, mPipelineCacheVk, *compatibleRenderPass,
1096 pipelineLayout, activeAttribLocationsMask, vertexShader.get(),
Jamie Madill9aef3672018-04-27 11:45:06 -04001097 fragmentShader.get(), pipelineDesc, pipelineOut);
1098}
1099
Jamie Madill21061022018-07-12 23:56:30 -04001100angle::Result RendererVk::getDescriptorSetLayout(
1101 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001102 const vk::DescriptorSetLayoutDesc &desc,
1103 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
1104{
Jamie Madill21061022018-07-12 23:56:30 -04001105 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -04001106}
1107
Jamie Madill21061022018-07-12 23:56:30 -04001108angle::Result RendererVk::getPipelineLayout(
1109 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001110 const vk::PipelineLayoutDesc &desc,
1111 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
1112 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
1113{
Jamie Madill21061022018-07-12 23:56:30 -04001114 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -04001115 pipelineLayoutOut);
1116}
1117
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001118angle::Result RendererVk::syncPipelineCacheVk(DisplayVk *displayVk)
1119{
1120 ASSERT(mPipelineCacheVk.valid());
1121
1122 if (--mPipelineCacheVkUpdateTimeout > 0)
1123 {
1124 return angle::Result::Continue();
1125 }
1126
1127 mPipelineCacheVkUpdateTimeout = kPipelineCacheVkUpdatePeriod;
1128
1129 // Get the size of the cache.
1130 size_t pipelineCacheSize = 0;
1131 ANGLE_TRY(mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, nullptr));
1132
1133 angle::MemoryBuffer *pipelineCacheData = nullptr;
1134 ANGLE_VK_CHECK_ALLOC(displayVk,
1135 displayVk->getScratchBuffer(pipelineCacheSize, &pipelineCacheData));
1136
1137 size_t originalPipelineCacheSize = pipelineCacheSize;
1138 angle::Result result =
1139 mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, pipelineCacheData->data());
1140 ANGLE_TRY(result);
1141
1142 // Note: currently we don't accept incomplete as we don't expect it (the full size of cache
1143 // was determined just above), so receiving it hints at an implementation bug we would want
1144 // to know about early.
1145 ASSERT(result != angle::Result::Incomplete());
1146
1147 // If vkGetPipelineCacheData ends up writing fewer bytes than requested, zero out the rest of
1148 // the buffer to avoid leaking garbage memory.
1149 ASSERT(pipelineCacheSize <= originalPipelineCacheSize);
1150 if (pipelineCacheSize < originalPipelineCacheSize)
1151 {
1152 memset(pipelineCacheData->data() + pipelineCacheSize, 0,
1153 originalPipelineCacheSize - pipelineCacheSize);
1154 }
1155
1156 displayVk->getBlobCache()->putApplication(mPipelineCacheVkBlobKey, *pipelineCacheData);
1157
1158 return angle::Result::Continue();
1159}
1160
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001161angle::Result RendererVk::allocateSubmitWaitSemaphore(vk::Context *context,
1162 const vk::Semaphore **outSemaphore)
1163{
1164 ASSERT(mSubmitWaitSemaphores.size() < mSubmitWaitSemaphores.max_size());
1165
1166 vk::SemaphoreHelper semaphore;
1167 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &semaphore));
1168
1169 mSubmitWaitSemaphores.push_back(std::move(semaphore));
1170 *outSemaphore = mSubmitWaitSemaphores.back().getSemaphore();
1171
1172 return angle::Result::Continue();
1173}
1174
1175const vk::Semaphore *RendererVk::getSubmitLastSignaledSemaphore(vk::Context *context)
1176{
1177 const vk::Semaphore *semaphore = mSubmitLastSignaledSemaphore.getSemaphore();
1178
1179 // Return the semaphore to the pool (which will remain valid and unused until the
1180 // queue it's about to be waited on has finished execution). The caller is about
1181 // to wait on it.
1182 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
1183
1184 return semaphore;
1185}
1186
Jamie Madilld47044a2018-04-27 11:45:03 -04001187vk::ShaderLibrary *RendererVk::getShaderLibrary()
1188{
1189 return &mShaderLibrary;
1190}
Luc Ferron90968362018-05-04 08:47:22 -04001191
Jamie Madillaaca96e2018-06-12 10:19:48 -04001192uint32_t GetUniformBufferDescriptorCount()
1193{
1194 return kUniformBufferDescriptorsPerDescriptorSet;
1195}
1196
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001197} // namespace rx