blob: e02cd2555a636bd30c4e4f65351d08c4180178b7 [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
Shahbaz Youssefi61656022018-10-24 15:00:50 -040031#include "third_party/trace_event/trace_event.h"
32
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070033// Consts
34namespace
35{
36const uint32_t kMockVendorID = 0xba5eba11;
37const uint32_t kMockDeviceID = 0xf005ba11;
38constexpr char kMockDeviceName[] = "Vulkan Mock Device";
Shahbaz Youssefi61656022018-10-24 15:00:50 -040039constexpr size_t kInFlightCommandsLimit = 100u;
Tobin Ehlisa3b220f2018-03-06 16:22:13 -070040} // anonymous namespace
41
Jamie Madill9e54b5a2016-05-25 12:57:39 -040042namespace rx
43{
44
Jamie Madille09bd5d2016-11-29 16:20:35 -050045namespace
46{
Luc Ferrondaedf4d2018-03-16 09:28:53 -040047// We currently only allocate 2 uniform buffer per descriptor set, one for the fragment shader and
48// one for the vertex shader.
49constexpr size_t kUniformBufferDescriptorsPerDescriptorSet = 2;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -040050// Update the pipeline cache every this many swaps (if 60fps, this means every 10 minutes)
51static constexpr uint32_t kPipelineCacheVkUpdatePeriod = 10 * 60 * 60;
Jamie Madille09bd5d2016-11-29 16:20:35 -050052
Omar El Sheikh26c61b22018-06-29 12:50:59 -060053bool ShouldEnableMockICD(const egl::AttributeMap &attribs)
54{
55#if !defined(ANGLE_PLATFORM_ANDROID)
56 // Mock ICD does not currently run on Android
57 return (attribs.get(EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE,
58 EGL_PLATFORM_ANGLE_DEVICE_TYPE_HARDWARE_ANGLE) ==
59 EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE);
60#else
61 return false;
62#endif // !defined(ANGLE_PLATFORM_ANDROID)
63}
64
Jamie Madille09bd5d2016-11-29 16:20:35 -050065VkResult VerifyExtensionsPresent(const std::vector<VkExtensionProperties> &extensionProps,
66 const std::vector<const char *> &enabledExtensionNames)
67{
68 // Compile the extensions names into a set.
69 std::set<std::string> extensionNames;
70 for (const auto &extensionProp : extensionProps)
71 {
72 extensionNames.insert(extensionProp.extensionName);
73 }
74
Jamie Madillacf2f3a2017-11-21 19:22:44 -050075 for (const char *extensionName : enabledExtensionNames)
Jamie Madille09bd5d2016-11-29 16:20:35 -050076 {
77 if (extensionNames.count(extensionName) == 0)
78 {
79 return VK_ERROR_EXTENSION_NOT_PRESENT;
80 }
81 }
82
83 return VK_SUCCESS;
84}
85
Tobin Ehlis3a181e32018-08-29 15:17:05 -060086// Array of Validation error/warning messages that will be ignored, should include bugID
87constexpr std::array<const char *, 1> kSkippedMessages = {
88 // http://anglebug.com/2796
89 " [ UNASSIGNED-CoreValidation-Shader-PointSizeMissing ] Object: VK_NULL_HANDLE (Type = 19) "
90 "| Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader "
91 "corresponding to VK_SHADER_STAGE_VERTEX_BIT."};
92
93// Suppress validation errors that are known
94// return "true" if given code/prefix/message is known, else return "false"
95bool IsIgnoredDebugMessage(const char *message)
96{
97 for (const auto &msg : kSkippedMessages)
98 {
99 if (strcmp(msg, message) == 0)
100 {
101 return true;
102 }
103 }
104 return false;
105}
106
Yuly Novikov199f4292018-01-19 19:04:05 -0500107VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(VkDebugReportFlagsEXT flags,
108 VkDebugReportObjectTypeEXT objectType,
109 uint64_t object,
110 size_t location,
111 int32_t messageCode,
112 const char *layerPrefix,
113 const char *message,
114 void *userData)
Jamie Madill0448ec82016-12-23 13:41:47 -0500115{
Tobin Ehlis3a181e32018-08-29 15:17:05 -0600116 if (IsIgnoredDebugMessage(message))
117 {
118 return VK_FALSE;
119 }
Jamie Madill0448ec82016-12-23 13:41:47 -0500120 if ((flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) != 0)
121 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500122 ERR() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500123#if !defined(NDEBUG)
124 // Abort the call in Debug builds.
125 return VK_TRUE;
126#endif
127 }
128 else if ((flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) != 0)
129 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500130 WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500131 }
132 else
133 {
Yuly Novikovbcb3f9b2017-01-27 22:45:18 -0500134 // Uncomment this if you want Vulkan spam.
135 // WARN() << message;
Jamie Madill0448ec82016-12-23 13:41:47 -0500136 }
137
138 return VK_FALSE;
139}
140
Yuly Novikov199f4292018-01-19 19:04:05 -0500141// If we're loading the validation layers, we could be running from any random directory.
142// Change to the executable directory so we can find the layers, then change back to the
143// previous directory to be safe we don't disrupt the application.
144class ScopedVkLoaderEnvironment : angle::NonCopyable
145{
146 public:
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600147 ScopedVkLoaderEnvironment(bool enableValidationLayers, bool enableMockICD)
148 : mEnableValidationLayers(enableValidationLayers),
149 mEnableMockICD(enableMockICD),
150 mChangedCWD(false),
151 mChangedICDPath(false)
Yuly Novikov199f4292018-01-19 19:04:05 -0500152 {
153// Changing CWD and setting environment variables makes no sense on Android,
154// since this code is a part of Java application there.
155// Android Vulkan loader doesn't need this either.
156#if !defined(ANGLE_PLATFORM_ANDROID)
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600157 if (enableMockICD)
158 {
159 // Override environment variable to use built Mock ICD
160 // ANGLE_VK_ICD_JSON gets set to the built mock ICD in BUILD.gn
161 mPreviousICDPath = angle::GetEnvironmentVar(g_VkICDPathEnv);
162 mChangedICDPath = angle::SetEnvironmentVar(g_VkICDPathEnv, ANGLE_VK_ICD_JSON);
163 if (!mChangedICDPath)
164 {
165 ERR() << "Error setting Path for Mock/Null Driver.";
166 mEnableMockICD = false;
167 }
168 }
Jamie Madill46848422018-08-09 10:46:06 -0400169 if (mEnableValidationLayers || mEnableMockICD)
Yuly Novikov199f4292018-01-19 19:04:05 -0500170 {
171 const auto &cwd = angle::GetCWD();
172 if (!cwd.valid())
173 {
174 ERR() << "Error getting CWD for Vulkan layers init.";
175 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400176 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500177 }
178 else
179 {
180 mPreviousCWD = cwd.value();
181 const char *exeDir = angle::GetExecutableDirectory();
182 mChangedCWD = angle::SetCWD(exeDir);
183 if (!mChangedCWD)
184 {
185 ERR() << "Error setting CWD for Vulkan layers init.";
186 mEnableValidationLayers = false;
Jamie Madill46848422018-08-09 10:46:06 -0400187 mEnableMockICD = false;
Yuly Novikov199f4292018-01-19 19:04:05 -0500188 }
189 }
190 }
191
192 // Override environment variable to use the ANGLE layers.
193 if (mEnableValidationLayers)
194 {
Tobin Ehlisa3b220f2018-03-06 16:22:13 -0700195 if (!angle::PrependPathToEnvironmentVar(g_VkLoaderLayersPathEnv, ANGLE_VK_DATA_DIR))
Yuly Novikov199f4292018-01-19 19:04:05 -0500196 {
197 ERR() << "Error setting environment for Vulkan layers init.";
198 mEnableValidationLayers = false;
199 }
200 }
201#endif // !defined(ANGLE_PLATFORM_ANDROID)
202 }
203
204 ~ScopedVkLoaderEnvironment()
205 {
206 if (mChangedCWD)
207 {
208#if !defined(ANGLE_PLATFORM_ANDROID)
209 ASSERT(mPreviousCWD.valid());
210 angle::SetCWD(mPreviousCWD.value().c_str());
211#endif // !defined(ANGLE_PLATFORM_ANDROID)
212 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600213 if (mChangedICDPath)
214 {
Omar El Sheikh80d4ef12018-07-13 17:08:19 -0600215 if (mPreviousICDPath.value().empty())
216 {
217 angle::UnsetEnvironmentVar(g_VkICDPathEnv);
218 }
219 else
220 {
221 angle::SetEnvironmentVar(g_VkICDPathEnv, mPreviousICDPath.value().c_str());
222 }
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600223 }
Yuly Novikov199f4292018-01-19 19:04:05 -0500224 }
225
Jamie Madillaaca96e2018-06-12 10:19:48 -0400226 bool canEnableValidationLayers() const { return mEnableValidationLayers; }
Yuly Novikov199f4292018-01-19 19:04:05 -0500227
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600228 bool canEnableMockICD() const { return mEnableMockICD; }
229
Yuly Novikov199f4292018-01-19 19:04:05 -0500230 private:
231 bool mEnableValidationLayers;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600232 bool mEnableMockICD;
Yuly Novikov199f4292018-01-19 19:04:05 -0500233 bool mChangedCWD;
234 Optional<std::string> mPreviousCWD;
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600235 bool mChangedICDPath;
236 Optional<std::string> mPreviousICDPath;
Yuly Novikov199f4292018-01-19 19:04:05 -0500237};
238
Jamie Madill21061022018-07-12 23:56:30 -0400239void ChoosePhysicalDevice(const std::vector<VkPhysicalDevice> &physicalDevices,
240 bool preferMockICD,
241 VkPhysicalDevice *physicalDeviceOut,
242 VkPhysicalDeviceProperties *physicalDevicePropertiesOut)
243{
244 ASSERT(!physicalDevices.empty());
245 if (preferMockICD)
246 {
247 for (const VkPhysicalDevice &physicalDevice : physicalDevices)
248 {
249 vkGetPhysicalDeviceProperties(physicalDevice, physicalDevicePropertiesOut);
250 if ((kMockVendorID == physicalDevicePropertiesOut->vendorID) &&
251 (kMockDeviceID == physicalDevicePropertiesOut->deviceID) &&
252 (strcmp(kMockDeviceName, physicalDevicePropertiesOut->deviceName) == 0))
253 {
254 *physicalDeviceOut = physicalDevice;
255 return;
256 }
257 }
258 WARN() << "Vulkan Mock Driver was requested but Mock Device was not found. Using default "
259 "physicalDevice instead.";
260 }
261
262 // Fall back to first device.
263 *physicalDeviceOut = physicalDevices[0];
264 vkGetPhysicalDeviceProperties(*physicalDeviceOut, physicalDevicePropertiesOut);
265}
Jamie Madill0da73fe2018-10-02 09:31:39 -0400266
267// Initially dumping the command graphs is disabled.
268constexpr bool kEnableCommandGraphDiagnostics = false;
Jamie Madille09bd5d2016-11-29 16:20:35 -0500269} // anonymous namespace
270
Jamie Madill49ac74b2017-12-21 14:42:33 -0500271// CommandBatch implementation.
Jamie Madillaaca96e2018-06-12 10:19:48 -0400272RendererVk::CommandBatch::CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500273
Jamie Madillaaca96e2018-06-12 10:19:48 -0400274RendererVk::CommandBatch::~CommandBatch() = default;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500275
276RendererVk::CommandBatch::CommandBatch(CommandBatch &&other)
277 : commandPool(std::move(other.commandPool)), fence(std::move(other.fence)), serial(other.serial)
278{
279}
280
281RendererVk::CommandBatch &RendererVk::CommandBatch::operator=(CommandBatch &&other)
282{
283 std::swap(commandPool, other.commandPool);
284 std::swap(fence, other.fence);
285 std::swap(serial, other.serial);
286 return *this;
287}
288
Jamie Madillbea35a62018-07-05 11:54:10 -0400289void RendererVk::CommandBatch::destroy(VkDevice device)
290{
291 commandPool.destroy(device);
292 fence.destroy(device);
293}
294
Jamie Madill9f2a8612017-11-30 12:43:09 -0500295// RendererVk implementation.
Jamie Madill0448ec82016-12-23 13:41:47 -0500296RendererVk::RendererVk()
297 : mCapsInitialized(false),
298 mInstance(VK_NULL_HANDLE),
299 mEnableValidationLayers(false),
Jamie Madill4d0bf552016-12-28 15:45:24 -0500300 mDebugReportCallback(VK_NULL_HANDLE),
301 mPhysicalDevice(VK_NULL_HANDLE),
302 mQueue(VK_NULL_HANDLE),
303 mCurrentQueueFamilyIndex(std::numeric_limits<uint32_t>::max()),
304 mDevice(VK_NULL_HANDLE),
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400305 mLastCompletedQueueSerial(mQueueSerialFactory.generate()),
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400306 mCurrentQueueSerial(mQueueSerialFactory.generate()),
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400307 mDeviceLost(false),
Jamie Madill0da73fe2018-10-02 09:31:39 -0400308 mPipelineCacheVkUpdateTimeout(kPipelineCacheVkUpdatePeriod),
309 mCommandGraph(kEnableCommandGraphDiagnostics)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400310{
311}
312
313RendererVk::~RendererVk()
314{
Jamie Madill21061022018-07-12 23:56:30 -0400315}
316
317void RendererVk::onDestroy(vk::Context *context)
318{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500319 if (!mInFlightCommands.empty() || !mGarbage.empty())
Jamie Madill4c26fc22017-02-24 11:04:10 -0500320 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500321 // TODO(jmadill): Not nice to pass nullptr here, but shouldn't be a problem.
Jamie Madill21061022018-07-12 23:56:30 -0400322 (void)finish(context);
Jamie Madill4c26fc22017-02-24 11:04:10 -0500323 }
324
Jamie Madillc7918ce2018-06-13 13:25:31 -0400325 mPipelineLayoutCache.destroy(mDevice);
326 mDescriptorSetLayoutCache.destroy(mDevice);
327
Jamie Madill9f2a8612017-11-30 12:43:09 -0500328 mRenderPassCache.destroy(mDevice);
Jamie Madillffa4cbb2018-01-23 13:04:07 -0500329 mPipelineCache.destroy(mDevice);
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400330 mPipelineCacheVk.destroy(mDevice);
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400331 mSubmitSemaphorePool.destroy(mDevice);
Jamie Madilld47044a2018-04-27 11:45:03 -0400332 mShaderLibrary.destroy(mDevice);
Jamie Madill9f2a8612017-11-30 12:43:09 -0500333
Jamie Madill06ca6342018-07-12 15:56:53 -0400334 GlslangWrapper::Release();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500335
Jamie Madill5deea722017-02-16 10:44:46 -0500336 if (mCommandPool.valid())
337 {
338 mCommandPool.destroy(mDevice);
339 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500340
341 if (mDevice)
342 {
343 vkDestroyDevice(mDevice, nullptr);
344 mDevice = VK_NULL_HANDLE;
345 }
346
Jamie Madill0448ec82016-12-23 13:41:47 -0500347 if (mDebugReportCallback)
348 {
349 ASSERT(mInstance);
350 auto destroyDebugReportCallback = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
351 vkGetInstanceProcAddr(mInstance, "vkDestroyDebugReportCallbackEXT"));
352 ASSERT(destroyDebugReportCallback);
353 destroyDebugReportCallback(mInstance, mDebugReportCallback, nullptr);
354 }
355
Jamie Madill4d0bf552016-12-28 15:45:24 -0500356 if (mInstance)
357 {
358 vkDestroyInstance(mInstance, nullptr);
359 mInstance = VK_NULL_HANDLE;
360 }
361
Omar El Sheikheb4b8692018-07-17 10:55:40 -0600362 mMemoryProperties.destroy();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500363 mPhysicalDevice = VK_NULL_HANDLE;
Jamie Madill327ba852016-11-30 12:38:28 -0500364}
365
Geoff Lang2fe5e1d2018-08-28 14:00:24 -0400366void RendererVk::markDeviceLost()
367{
368 mDeviceLost = true;
369}
370
371bool RendererVk::isDeviceLost() const
372{
373 return mDeviceLost;
374}
375
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400376angle::Result RendererVk::initialize(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400377 const egl::AttributeMap &attribs,
378 const char *wsiName)
Jamie Madill327ba852016-11-30 12:38:28 -0500379{
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600380 ScopedVkLoaderEnvironment scopedEnvironment(ShouldUseDebugLayers(attribs),
381 ShouldEnableMockICD(attribs));
Yuly Novikov199f4292018-01-19 19:04:05 -0500382 mEnableValidationLayers = scopedEnvironment.canEnableValidationLayers();
Omar El Sheikh26c61b22018-06-29 12:50:59 -0600383 bool enableMockICD = scopedEnvironment.canEnableMockICD();
Jamie Madilla66779f2017-01-06 10:43:44 -0500384
Jamie Madill0448ec82016-12-23 13:41:47 -0500385 // Gather global layer properties.
386 uint32_t instanceLayerCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400387 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount, nullptr));
Jamie Madill0448ec82016-12-23 13:41:47 -0500388
389 std::vector<VkLayerProperties> instanceLayerProps(instanceLayerCount);
390 if (instanceLayerCount > 0)
391 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400392 ANGLE_VK_TRY(displayVk, vkEnumerateInstanceLayerProperties(&instanceLayerCount,
393 instanceLayerProps.data()));
Jamie Madill0448ec82016-12-23 13:41:47 -0500394 }
395
Jamie Madille09bd5d2016-11-29 16:20:35 -0500396 uint32_t instanceExtensionCount = 0;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400397 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400398 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount, nullptr));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500399
400 std::vector<VkExtensionProperties> instanceExtensionProps(instanceExtensionCount);
401 if (instanceExtensionCount > 0)
402 {
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400403 ANGLE_VK_TRY(displayVk,
404 vkEnumerateInstanceExtensionProperties(nullptr, &instanceExtensionCount,
405 instanceExtensionProps.data()));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500406 }
407
Yuly Novikov199f4292018-01-19 19:04:05 -0500408 const char *const *enabledLayerNames = nullptr;
409 uint32_t enabledLayerCount = 0;
Jamie Madill0448ec82016-12-23 13:41:47 -0500410 if (mEnableValidationLayers)
411 {
Yuly Novikov199f4292018-01-19 19:04:05 -0500412 bool layersRequested =
413 (attribs.get(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE, EGL_DONT_CARE) == EGL_TRUE);
414 mEnableValidationLayers = GetAvailableValidationLayers(
415 instanceLayerProps, layersRequested, &enabledLayerNames, &enabledLayerCount);
Jamie Madill0448ec82016-12-23 13:41:47 -0500416 }
417
Jamie Madille09bd5d2016-11-29 16:20:35 -0500418 std::vector<const char *> enabledInstanceExtensions;
419 enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
Frank Henigman29f148b2016-11-23 21:05:36 -0500420 enabledInstanceExtensions.push_back(wsiName);
Jamie Madille09bd5d2016-11-29 16:20:35 -0500421
Jamie Madill0448ec82016-12-23 13:41:47 -0500422 // TODO(jmadill): Should be able to continue initialization if debug report ext missing.
423 if (mEnableValidationLayers)
424 {
425 enabledInstanceExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
426 }
427
Jamie Madille09bd5d2016-11-29 16:20:35 -0500428 // Verify the required extensions are in the extension names set. Fail if not.
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400429 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400430 VerifyExtensionsPresent(instanceExtensionProps, enabledInstanceExtensions));
Jamie Madille09bd5d2016-11-29 16:20:35 -0500431
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400432 VkApplicationInfo applicationInfo = {};
Jamie Madill327ba852016-11-30 12:38:28 -0500433 applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
Jamie Madill327ba852016-11-30 12:38:28 -0500434 applicationInfo.pApplicationName = "ANGLE";
435 applicationInfo.applicationVersion = 1;
436 applicationInfo.pEngineName = "ANGLE";
437 applicationInfo.engineVersion = 1;
438 applicationInfo.apiVersion = VK_API_VERSION_1_0;
439
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400440 VkInstanceCreateInfo instanceInfo = {};
Jamie Madill327ba852016-11-30 12:38:28 -0500441 instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
Jamie Madill327ba852016-11-30 12:38:28 -0500442 instanceInfo.flags = 0;
443 instanceInfo.pApplicationInfo = &applicationInfo;
444
Jamie Madille09bd5d2016-11-29 16:20:35 -0500445 // Enable requested layers and extensions.
446 instanceInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
447 instanceInfo.ppEnabledExtensionNames =
448 enabledInstanceExtensions.empty() ? nullptr : enabledInstanceExtensions.data();
Yuly Novikov199f4292018-01-19 19:04:05 -0500449 instanceInfo.enabledLayerCount = enabledLayerCount;
450 instanceInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill327ba852016-11-30 12:38:28 -0500451
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400452 ANGLE_VK_TRY(displayVk, vkCreateInstance(&instanceInfo, nullptr, &mInstance));
Jamie Madill327ba852016-11-30 12:38:28 -0500453
Jamie Madill0448ec82016-12-23 13:41:47 -0500454 if (mEnableValidationLayers)
455 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400456 VkDebugReportCallbackCreateInfoEXT debugReportInfo = {};
Jamie Madill0448ec82016-12-23 13:41:47 -0500457
458 debugReportInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
Jamie Madill0448ec82016-12-23 13:41:47 -0500459 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
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400579 // Select additional features to be enabled
580 VkPhysicalDeviceFeatures enabledFeatures = {};
581 enabledFeatures.inheritedQueries = mPhysicalDeviceFeatures.inheritedQueries;
582
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400583 VkDeviceQueueCreateInfo queueCreateInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500584
585 float zeroPriority = 0.0f;
586
587 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500588 queueCreateInfo.flags = 0;
589 queueCreateInfo.queueFamilyIndex = queueFamilyIndex;
590 queueCreateInfo.queueCount = 1;
591 queueCreateInfo.pQueuePriorities = &zeroPriority;
592
593 // Initialize the device
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400594 VkDeviceCreateInfo createInfo = {};
Jamie Madill4d0bf552016-12-28 15:45:24 -0500595
Jamie Madill50cf2be2018-06-15 09:46:57 -0400596 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400597 createInfo.flags = 0;
598 createInfo.queueCreateInfoCount = 1;
599 createInfo.pQueueCreateInfos = &queueCreateInfo;
Yuly Novikov199f4292018-01-19 19:04:05 -0500600 createInfo.enabledLayerCount = enabledLayerCount;
601 createInfo.ppEnabledLayerNames = enabledLayerNames;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500602 createInfo.enabledExtensionCount = static_cast<uint32_t>(enabledDeviceExtensions.size());
603 createInfo.ppEnabledExtensionNames =
604 enabledDeviceExtensions.empty() ? nullptr : enabledDeviceExtensions.data();
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400605 createInfo.pEnabledFeatures = &enabledFeatures;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500606
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400607 ANGLE_VK_TRY(displayVk, vkCreateDevice(mPhysicalDevice, &createInfo, nullptr, &mDevice));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500608
609 mCurrentQueueFamilyIndex = queueFamilyIndex;
610
611 vkGetDeviceQueue(mDevice, mCurrentQueueFamilyIndex, 0, &mQueue);
612
613 // Initialize the command pool now that we know the queue family index.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400614 VkCommandPoolCreateInfo commandPoolInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500615 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500616 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500617 commandPoolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
618
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400619 ANGLE_TRY(mCommandPool.init(displayVk, commandPoolInfo));
620
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400621 // Initialize the vulkan pipeline cache.
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400622 ANGLE_TRY(initPipelineCacheVk(displayVk));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500623
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400624 // Initialize the submission semaphore pool.
625 ANGLE_TRY(mSubmitSemaphorePool.init(displayVk, vk::kDefaultSemaphorePoolSize));
626
Jamie Madill21061022018-07-12 23:56:30 -0400627 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500628}
629
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400630angle::Result RendererVk::selectPresentQueueForSurface(DisplayVk *displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400631 VkSurfaceKHR surface,
632 uint32_t *presentQueueOut)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500633{
634 // We've already initialized a device, and can't re-create it unless it's never been used.
635 // TODO(jmadill): Handle the re-creation case if necessary.
636 if (mDevice != VK_NULL_HANDLE)
637 {
638 ASSERT(mCurrentQueueFamilyIndex != std::numeric_limits<uint32_t>::max());
639
640 // Check if the current device supports present on this surface.
641 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400642 ANGLE_VK_TRY(displayVk,
Jamie Madill21061022018-07-12 23:56:30 -0400643 vkGetPhysicalDeviceSurfaceSupportKHR(mPhysicalDevice, mCurrentQueueFamilyIndex,
Jamie Madill4d0bf552016-12-28 15:45:24 -0500644 surface, &supportsPresent));
645
Jamie Madill6cad7732018-07-11 09:01:17 -0400646 if (supportsPresent == VK_TRUE)
647 {
648 *presentQueueOut = mCurrentQueueFamilyIndex;
Jamie Madill21061022018-07-12 23:56:30 -0400649 return angle::Result::Continue();
Jamie Madill6cad7732018-07-11 09:01:17 -0400650 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500651 }
652
653 // Find a graphics and present queue.
654 Optional<uint32_t> newPresentQueue;
655 uint32_t queueCount = static_cast<uint32_t>(mQueueFamilyProperties.size());
656 for (uint32_t queueIndex = 0; queueIndex < queueCount; ++queueIndex)
657 {
658 const auto &queueInfo = mQueueFamilyProperties[queueIndex];
659 if ((queueInfo.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
660 {
661 VkBool32 supportsPresent = VK_FALSE;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400662 ANGLE_VK_TRY(displayVk, vkGetPhysicalDeviceSurfaceSupportKHR(
663 mPhysicalDevice, queueIndex, surface, &supportsPresent));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500664
665 if (supportsPresent == VK_TRUE)
666 {
667 newPresentQueue = queueIndex;
668 break;
669 }
670 }
671 }
672
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400673 ANGLE_VK_CHECK(displayVk, newPresentQueue.valid(), VK_ERROR_INITIALIZATION_FAILED);
674 ANGLE_TRY(initializeDevice(displayVk, newPresentQueue.value()));
Jamie Madill4d0bf552016-12-28 15:45:24 -0500675
Jamie Madill6cad7732018-07-11 09:01:17 -0400676 *presentQueueOut = newPresentQueue.value();
Jamie Madill21061022018-07-12 23:56:30 -0400677 return angle::Result::Continue();
Jamie Madill4d0bf552016-12-28 15:45:24 -0500678}
679
680std::string RendererVk::getVendorString() const
681{
Olli Etuahoc6a06182018-04-13 14:11:46 +0300682 return GetVendorString(mPhysicalDeviceProperties.vendorID);
Jamie Madill4d0bf552016-12-28 15:45:24 -0500683}
684
Jamie Madille09bd5d2016-11-29 16:20:35 -0500685std::string RendererVk::getRendererDescription() const
686{
Jamie Madill4d0bf552016-12-28 15:45:24 -0500687 std::stringstream strstr;
688
689 uint32_t apiVersion = mPhysicalDeviceProperties.apiVersion;
690
691 strstr << "Vulkan ";
692 strstr << VK_VERSION_MAJOR(apiVersion) << ".";
693 strstr << VK_VERSION_MINOR(apiVersion) << ".";
694 strstr << VK_VERSION_PATCH(apiVersion);
695
Olli Etuahoc6a06182018-04-13 14:11:46 +0300696 strstr << "(";
697
698 // In the case of NVIDIA, deviceName does not necessarily contain "NVIDIA". Add "NVIDIA" so that
699 // Vulkan end2end tests can be selectively disabled on NVIDIA. TODO(jmadill): should not be
700 // needed after http://anglebug.com/1874 is fixed and end2end_tests use more sophisticated
701 // driver detection.
702 if (mPhysicalDeviceProperties.vendorID == VENDOR_ID_NVIDIA)
703 {
704 strstr << GetVendorString(mPhysicalDeviceProperties.vendorID) << " ";
705 }
706
707 strstr << mPhysicalDeviceProperties.deviceName << ")";
Jamie Madill4d0bf552016-12-28 15:45:24 -0500708
709 return strstr.str();
Jamie Madille09bd5d2016-11-29 16:20:35 -0500710}
711
Jamie Madill12222072018-07-11 14:59:48 -0400712void RendererVk::initFeatures()
713{
Jamie Madillb36a4812018-09-25 10:15:11 -0400714// Use OpenGL line rasterization rules by default.
715// TODO(jmadill): Fix Android support. http://anglebug.com/2830
716#if defined(ANGLE_PLATFORM_ANDROID)
717 mFeatures.basicGLLineRasterization = false;
718#else
Jamie Madill12222072018-07-11 14:59:48 -0400719 mFeatures.basicGLLineRasterization = true;
Jamie Madillb36a4812018-09-25 10:15:11 -0400720#endif // defined(ANGLE_PLATFORM_ANDROID)
Jamie Madill12222072018-07-11 14:59:48 -0400721
Luc Ferronf786b702018-07-10 11:01:43 -0400722 // TODO(lucferron): Currently disabled on Intel only since many tests are failing and need
723 // investigation. http://anglebug.com/2728
724 mFeatures.flipViewportY = !IsIntel(mPhysicalDeviceProperties.vendorID);
Frank Henigmanbeb669d2018-09-21 16:25:52 -0400725
726#ifdef ANGLE_PLATFORM_WINDOWS
727 // http://anglebug.com/2838
728 mFeatures.extraCopyBufferRegion = IsIntel(mPhysicalDeviceProperties.vendorID);
729#endif
Jamie Madill12222072018-07-11 14:59:48 -0400730}
731
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400732void RendererVk::initPipelineCacheVkKey()
733{
734 std::ostringstream hashStream("ANGLE Pipeline Cache: ", std::ios_base::ate);
735 // Add the pipeline cache UUID to make sure the blob cache always gives a compatible pipeline
736 // cache. It's not particularly necessary to write it as a hex number as done here, so long as
737 // there is no '\0' in the result.
738 for (const uint32_t c : mPhysicalDeviceProperties.pipelineCacheUUID)
739 {
740 hashStream << std::hex << c;
741 }
742 // Add the vendor and device id too for good measure.
743 hashStream << std::hex << mPhysicalDeviceProperties.vendorID;
744 hashStream << std::hex << mPhysicalDeviceProperties.deviceID;
745
746 const std::string &hashString = hashStream.str();
747 angle::base::SHA1HashBytes(reinterpret_cast<const unsigned char *>(hashString.c_str()),
748 hashString.length(), mPipelineCacheVkBlobKey.data());
749}
750
751angle::Result RendererVk::initPipelineCacheVk(DisplayVk *display)
752{
753 initPipelineCacheVkKey();
754
755 egl::BlobCache::Value initialData;
756 bool success = display->getBlobCache()->get(display->getScratchBuffer(),
757 mPipelineCacheVkBlobKey, &initialData);
758
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400759 VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {};
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400760
761 pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Shahbaz Youssefi996628a2018-09-24 16:39:26 -0400762 pipelineCacheCreateInfo.flags = 0;
763 pipelineCacheCreateInfo.initialDataSize = success ? initialData.size() : 0;
764 pipelineCacheCreateInfo.pInitialData = success ? initialData.data() : nullptr;
765
766 ANGLE_TRY(mPipelineCacheVk.init(display, pipelineCacheCreateInfo));
767 return angle::Result::Continue();
768}
769
Jamie Madillacccc6c2016-05-03 17:22:10 -0400770void RendererVk::ensureCapsInitialized() const
771{
772 if (!mCapsInitialized)
773 {
Shahbaz Youssefic2b576d2018-10-12 14:45:34 -0400774 ASSERT(mCurrentQueueFamilyIndex < mQueueFamilyProperties.size());
775 vk::GenerateCaps(mPhysicalDeviceProperties, mPhysicalDeviceFeatures,
776 mQueueFamilyProperties[mCurrentQueueFamilyIndex], mNativeTextureCaps,
Jamie Madill30b5d842018-08-31 17:19:12 -0400777 &mNativeCaps, &mNativeExtensions, &mNativeLimitations);
Jamie Madillacccc6c2016-05-03 17:22:10 -0400778 mCapsInitialized = true;
779 }
780}
781
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400782void RendererVk::getSubmitWaitSemaphores(
783 vk::Context *context,
784 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> *waitSemaphores)
785{
786 if (mSubmitLastSignaledSemaphore.getSemaphore())
787 {
788 waitSemaphores->push_back(mSubmitLastSignaledSemaphore.getSemaphore()->getHandle());
789
790 // Return the semaphore to the pool (which will remain valid and unused until the
791 // queue it's about to be waited on has finished execution).
792 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
793 }
794
795 for (vk::SemaphoreHelper &semaphore : mSubmitWaitSemaphores)
796 {
797 waitSemaphores->push_back(semaphore.getSemaphore()->getHandle());
798 mSubmitSemaphorePool.freeSemaphore(context, &semaphore);
799 }
800 mSubmitWaitSemaphores.clear();
801}
802
Jamie Madillacccc6c2016-05-03 17:22:10 -0400803const gl::Caps &RendererVk::getNativeCaps() const
804{
805 ensureCapsInitialized();
806 return mNativeCaps;
807}
808
809const gl::TextureCapsMap &RendererVk::getNativeTextureCaps() const
810{
811 ensureCapsInitialized();
812 return mNativeTextureCaps;
813}
814
815const gl::Extensions &RendererVk::getNativeExtensions() const
816{
817 ensureCapsInitialized();
818 return mNativeExtensions;
819}
820
821const gl::Limitations &RendererVk::getNativeLimitations() const
822{
823 ensureCapsInitialized();
824 return mNativeLimitations;
825}
826
Luc Ferrondaedf4d2018-03-16 09:28:53 -0400827uint32_t RendererVk::getMaxActiveTextures()
828{
829 // TODO(lucferron): expose this limitation to GL in Context Caps
830 return std::min<uint32_t>(mPhysicalDeviceProperties.limits.maxPerStageDescriptorSamplers,
831 gl::IMPLEMENTATION_MAX_ACTIVE_TEXTURES);
832}
833
Jamie Madill49ac74b2017-12-21 14:42:33 -0500834const vk::CommandPool &RendererVk::getCommandPool() const
Jamie Madill4d0bf552016-12-28 15:45:24 -0500835{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500836 return mCommandPool;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500837}
838
Jamie Madill21061022018-07-12 23:56:30 -0400839angle::Result RendererVk::finish(vk::Context *context)
Jamie Madill4d0bf552016-12-28 15:45:24 -0500840{
Jamie Madill1f46bc12018-02-20 16:09:43 -0500841 if (!mCommandGraph.empty())
Jamie Madill49ac74b2017-12-21 14:42:33 -0500842 {
Shahbaz Youssefi61656022018-10-24 15:00:50 -0400843 TRACE_EVENT0("gpu.angle", "RendererVk::finish");
844
Luc Ferron1617e692018-07-11 11:08:19 -0400845 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
846 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400847
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400848 VkPipelineStageFlags waitStageMask[2] = {
849 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
850 };
851
852 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
853 getSubmitWaitSemaphores(context, &waitSemaphores);
854
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400855 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500856 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -0400857 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
858 submitInfo.pWaitSemaphores = waitSemaphores.data();
859 submitInfo.pWaitDstStageMask = waitStageMask;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500860 submitInfo.commandBufferCount = 1;
Luc Ferron1617e692018-07-11 11:08:19 -0400861 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -0500862 submitInfo.signalSemaphoreCount = 0;
863 submitInfo.pSignalSemaphores = nullptr;
Jamie Madill4d0bf552016-12-28 15:45:24 -0500864
Jamie Madill21061022018-07-12 23:56:30 -0400865 ANGLE_TRY(submitFrame(context, submitInfo, std::move(commandBatch.get())));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500866 }
Jamie Madill4d0bf552016-12-28 15:45:24 -0500867
Jamie Madill4c26fc22017-02-24 11:04:10 -0500868 ASSERT(mQueue != VK_NULL_HANDLE);
Jamie Madill21061022018-07-12 23:56:30 -0400869 ANGLE_VK_TRY(context, vkQueueWaitIdle(mQueue));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400870 freeAllInFlightResources();
Jamie Madill21061022018-07-12 23:56:30 -0400871 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500872}
873
Jamie Madill0c0dc342017-03-24 14:18:51 -0400874void RendererVk::freeAllInFlightResources()
875{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500876 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill0c0dc342017-03-24 14:18:51 -0400877 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500878 batch.fence.destroy(mDevice);
879 batch.commandPool.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400880 }
881 mInFlightCommands.clear();
882
883 for (auto &garbage : mGarbage)
884 {
Jamie Madille88ec8e2017-10-31 17:18:14 -0400885 garbage.destroy(mDevice);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400886 }
887 mGarbage.clear();
Shahbaz Youssefi61656022018-10-24 15:00:50 -0400888
889 mLastCompletedQueueSerial = mLastSubmittedQueueSerial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400890}
891
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400892angle::Result RendererVk::checkCompletedCommands(vk::Context *context)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500893{
Jamie Madill49ac74b2017-12-21 14:42:33 -0500894 int finishedCount = 0;
Jamie Madillf651c772017-02-21 15:03:51 -0500895
Jamie Madill49ac74b2017-12-21 14:42:33 -0500896 for (CommandBatch &batch : mInFlightCommands)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500897 {
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400898 angle::Result result = batch.fence.getStatus(context);
899 ANGLE_TRY(result);
900 if (result == angle::Result::Incomplete())
Jamie Madill0c0dc342017-03-24 14:18:51 -0400901 break;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500902
Jamie Madill49ac74b2017-12-21 14:42:33 -0500903 ASSERT(batch.serial > mLastCompletedQueueSerial);
904 mLastCompletedQueueSerial = batch.serial;
Jamie Madill0c0dc342017-03-24 14:18:51 -0400905
Jamie Madill49ac74b2017-12-21 14:42:33 -0500906 batch.fence.destroy(mDevice);
907 batch.commandPool.destroy(mDevice);
908 ++finishedCount;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500909 }
910
Jamie Madill49ac74b2017-12-21 14:42:33 -0500911 mInFlightCommands.erase(mInFlightCommands.begin(), mInFlightCommands.begin() + finishedCount);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400912
913 size_t freeIndex = 0;
914 for (; freeIndex < mGarbage.size(); ++freeIndex)
915 {
Jamie Madill49ac74b2017-12-21 14:42:33 -0500916 if (!mGarbage[freeIndex].destroyIfComplete(mDevice, mLastCompletedQueueSerial))
Jamie Madill0c0dc342017-03-24 14:18:51 -0400917 break;
918 }
919
920 // Remove the entries from the garbage list - they should be ready to go.
921 if (freeIndex > 0)
922 {
923 mGarbage.erase(mGarbage.begin(), mGarbage.begin() + freeIndex);
Jamie Madillf651c772017-02-21 15:03:51 -0500924 }
925
Jamie Madill21061022018-07-12 23:56:30 -0400926 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500927}
928
Jamie Madill21061022018-07-12 23:56:30 -0400929angle::Result RendererVk::submitFrame(vk::Context *context,
930 const VkSubmitInfo &submitInfo,
931 vk::CommandBuffer &&commandBuffer)
Jamie Madill4c26fc22017-02-24 11:04:10 -0500932{
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400933 VkFenceCreateInfo fenceInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500934 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500935 fenceInfo.flags = 0;
936
Jamie Madillbea35a62018-07-05 11:54:10 -0400937 vk::Scoped<CommandBatch> scopedBatch(mDevice);
938 CommandBatch &batch = scopedBatch.get();
Jamie Madill21061022018-07-12 23:56:30 -0400939 ANGLE_TRY(batch.fence.init(context, fenceInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500940
Jamie Madill21061022018-07-12 23:56:30 -0400941 ANGLE_VK_TRY(context, vkQueueSubmit(mQueue, 1, &submitInfo, batch.fence.getHandle()));
Jamie Madill4c26fc22017-02-24 11:04:10 -0500942
943 // Store this command buffer in the in-flight list.
Jamie Madill49ac74b2017-12-21 14:42:33 -0500944 batch.commandPool = std::move(mCommandPool);
945 batch.serial = mCurrentQueueSerial;
Jamie Madill4c26fc22017-02-24 11:04:10 -0500946
Jamie Madillbea35a62018-07-05 11:54:10 -0400947 mInFlightCommands.emplace_back(scopedBatch.release());
Jamie Madill0c0dc342017-03-24 14:18:51 -0400948
Shahbaz Youssefi61656022018-10-24 15:00:50 -0400949 // CPU should be throttled to avoid mInFlightCommands from growing too fast. That is done on
950 // swap() though, and there could be multiple submissions in between (through glFlush() calls),
951 // so the limit is larger than the expected number of images.
952 ASSERT(mInFlightCommands.size() <= kInFlightCommandsLimit);
Jamie Madill0c0dc342017-03-24 14:18:51 -0400953
954 // Increment the queue serial. If this fails, we should restart ANGLE.
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400955 // TODO(jmadill): Overflow check.
Shahbaz Youssefi61656022018-10-24 15:00:50 -0400956 mLastSubmittedQueueSerial = mCurrentQueueSerial;
Jamie Madillfb05bcb2017-06-07 15:43:18 -0400957 mCurrentQueueSerial = mQueueSerialFactory.generate();
Jamie Madill0c0dc342017-03-24 14:18:51 -0400958
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400959 ANGLE_TRY(checkCompletedCommands(context));
Jamie Madill0c0dc342017-03-24 14:18:51 -0400960
Jamie Madill49ac74b2017-12-21 14:42:33 -0500961 // Simply null out the command buffer here - it was allocated using the command pool.
962 commandBuffer.releaseHandle();
963
964 // Reallocate the command pool for next frame.
965 // TODO(jmadill): Consider reusing command pools.
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400966 VkCommandPoolCreateInfo poolInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -0500967 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
Jamie Madill49ac74b2017-12-21 14:42:33 -0500968 poolInfo.flags = 0;
969 poolInfo.queueFamilyIndex = mCurrentQueueFamilyIndex;
970
Jamie Madill21061022018-07-12 23:56:30 -0400971 ANGLE_TRY(mCommandPool.init(context, poolInfo));
Jamie Madill49ac74b2017-12-21 14:42:33 -0500972
Jamie Madill21061022018-07-12 23:56:30 -0400973 return angle::Result::Continue();
Jamie Madill4c26fc22017-02-24 11:04:10 -0500974}
975
Jamie Madillaaca96e2018-06-12 10:19:48 -0400976bool RendererVk::isSerialInUse(Serial serial) const
Jamie Madill97760352017-11-09 13:08:29 -0500977{
978 return serial > mLastCompletedQueueSerial;
979}
980
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400981angle::Result RendererVk::finishToSerial(vk::Context *context, Serial serial)
982{
983 if (!isSerialInUse(serial) || mInFlightCommands.empty())
984 {
985 return angle::Result::Continue();
986 }
987
988 // Find the first batch with serial equal to or bigger than given serial (note that
989 // the batch serials are unique, otherwise upper-bound would have been necessary).
990 size_t batchIndex = mInFlightCommands.size() - 1;
991 for (size_t i = 0; i < mInFlightCommands.size(); ++i)
992 {
993 if (mInFlightCommands[i].serial >= serial)
994 {
995 batchIndex = i;
996 break;
997 }
998 }
999 const CommandBatch &batch = mInFlightCommands[batchIndex];
1000
1001 // Wait for it finish
1002 constexpr uint64_t kMaxFenceWaitTimeNs = 10'000'000'000llu;
1003 angle::Result result = batch.fence.wait(context, kMaxFenceWaitTimeNs);
1004 if (result == angle::Result::Incomplete())
1005 {
1006 // Wait a maximum of 10s. If that times out, we declare it a failure.
1007 result = angle::Result::Stop();
1008 }
1009 ANGLE_TRY(result);
1010
1011 // Clean up finished batches.
1012 return checkCompletedCommands(context);
1013}
1014
Jamie Madill21061022018-07-12 23:56:30 -04001015angle::Result RendererVk::getCompatibleRenderPass(vk::Context *context,
1016 const vk::RenderPassDesc &desc,
1017 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001018{
Jamie Madill21061022018-07-12 23:56:30 -04001019 return mRenderPassCache.getCompatibleRenderPass(context, mCurrentQueueSerial, desc,
Jamie Madill9f2a8612017-11-30 12:43:09 -05001020 renderPassOut);
1021}
1022
Jamie Madill21061022018-07-12 23:56:30 -04001023angle::Result RendererVk::getRenderPassWithOps(vk::Context *context,
1024 const vk::RenderPassDesc &desc,
1025 const vk::AttachmentOpsArray &ops,
1026 vk::RenderPass **renderPassOut)
Jamie Madill9f2a8612017-11-30 12:43:09 -05001027{
Jamie Madill21061022018-07-12 23:56:30 -04001028 return mRenderPassCache.getRenderPassWithOps(context, mCurrentQueueSerial, desc, ops,
Jamie Madillbef918c2017-12-13 13:11:30 -05001029 renderPassOut);
Jamie Madill9f2a8612017-11-30 12:43:09 -05001030}
1031
Jamie Madilla5e06072018-05-18 14:36:05 -04001032vk::CommandGraph *RendererVk::getCommandGraph()
Jamie Madill49ac74b2017-12-21 14:42:33 -05001033{
Jamie Madilla5e06072018-05-18 14:36:05 -04001034 return &mCommandGraph;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001035}
1036
Jamie Madill21061022018-07-12 23:56:30 -04001037angle::Result RendererVk::flushCommandGraph(vk::Context *context, vk::CommandBuffer *commandBatch)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001038{
Jamie Madill21061022018-07-12 23:56:30 -04001039 return mCommandGraph.submitCommands(context, mCurrentQueueSerial, &mRenderPassCache,
Jamie Madill1f46bc12018-02-20 16:09:43 -05001040 &mCommandPool, commandBatch);
Jamie Madill49ac74b2017-12-21 14:42:33 -05001041}
1042
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001043angle::Result RendererVk::flush(vk::Context *context)
Jamie Madill49ac74b2017-12-21 14:42:33 -05001044{
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001045 if (mCommandGraph.empty())
1046 {
1047 return angle::Result::Continue();
1048 }
1049
Shahbaz Youssefi61656022018-10-24 15:00:50 -04001050 TRACE_EVENT0("gpu.angle", "RendererVk::flush");
1051
Jamie Madillbea35a62018-07-05 11:54:10 -04001052 vk::Scoped<vk::CommandBuffer> commandBatch(mDevice);
1053 ANGLE_TRY(flushCommandGraph(context, &commandBatch.get()));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001054
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001055 VkPipelineStageFlags waitStageMask[2] = {
1056 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
1057 };
1058
1059 angle::FixedVector<VkSemaphore, kMaxWaitSemaphores> waitSemaphores;
1060 getSubmitWaitSemaphores(context, &waitSemaphores);
1061
1062 // On every flush, create a semaphore to be signaled. On the next submission, this semaphore
1063 // will be waited on.
1064 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &mSubmitLastSignaledSemaphore));
Jamie Madill49ac74b2017-12-21 14:42:33 -05001065
Shahbaz Youssefi06270c92018-10-03 17:00:25 -04001066 VkSubmitInfo submitInfo = {};
Jamie Madill49ac74b2017-12-21 14:42:33 -05001067 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001068 submitInfo.waitSemaphoreCount = static_cast<uint32_t>(waitSemaphores.size());
1069 submitInfo.pWaitSemaphores = waitSemaphores.data();
1070 submitInfo.pWaitDstStageMask = waitStageMask;
Jamie Madill49ac74b2017-12-21 14:42:33 -05001071 submitInfo.commandBufferCount = 1;
Jamie Madillbea35a62018-07-05 11:54:10 -04001072 submitInfo.pCommandBuffers = commandBatch.get().ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001073 submitInfo.signalSemaphoreCount = 1;
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001074 submitInfo.pSignalSemaphores = mSubmitLastSignaledSemaphore.getSemaphore()->ptr();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001075
Jamie Madill21061022018-07-12 23:56:30 -04001076 ANGLE_TRY(submitFrame(context, submitInfo, commandBatch.release()));
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001077
Jamie Madill21061022018-07-12 23:56:30 -04001078 return angle::Result::Continue();
Jamie Madill49ac74b2017-12-21 14:42:33 -05001079}
1080
Jamie Madill78feddc2018-04-27 11:45:05 -04001081Serial RendererVk::issueShaderSerial()
Jamie Madillf2f6d372018-01-10 21:37:23 -05001082{
Jamie Madill78feddc2018-04-27 11:45:05 -04001083 return mShaderSerialFactory.generate();
Jamie Madillf2f6d372018-01-10 21:37:23 -05001084}
1085
Jamie Madill06ca6342018-07-12 15:56:53 -04001086angle::Result RendererVk::getPipeline(vk::Context *context,
1087 const vk::ShaderAndSerial &vertexShader,
1088 const vk::ShaderAndSerial &fragmentShader,
1089 const vk::PipelineLayout &pipelineLayout,
1090 const vk::PipelineDesc &pipelineDesc,
1091 const gl::AttributesMask &activeAttribLocationsMask,
1092 vk::PipelineAndSerial **pipelineOut)
Jamie Madillffa4cbb2018-01-23 13:04:07 -05001093{
Jamie Madill06ca6342018-07-12 15:56:53 -04001094 ASSERT(vertexShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -04001095 pipelineDesc.getShaderStageInfo()[vk::ShaderType::VertexShader].moduleSerial);
Jamie Madill06ca6342018-07-12 15:56:53 -04001096 ASSERT(fragmentShader.getSerial() ==
Jamie Madill9aef3672018-04-27 11:45:06 -04001097 pipelineDesc.getShaderStageInfo()[vk::ShaderType::FragmentShader].moduleSerial);
1098
1099 // Pull in a compatible RenderPass.
1100 vk::RenderPass *compatibleRenderPass = nullptr;
Jamie Madill21061022018-07-12 23:56:30 -04001101 ANGLE_TRY(
1102 getCompatibleRenderPass(context, pipelineDesc.getRenderPassDesc(), &compatibleRenderPass));
Jamie Madill9aef3672018-04-27 11:45:06 -04001103
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001104 return mPipelineCache.getPipeline(context, mPipelineCacheVk, *compatibleRenderPass,
1105 pipelineLayout, activeAttribLocationsMask, vertexShader.get(),
Jamie Madill9aef3672018-04-27 11:45:06 -04001106 fragmentShader.get(), pipelineDesc, pipelineOut);
1107}
1108
Jamie Madill21061022018-07-12 23:56:30 -04001109angle::Result RendererVk::getDescriptorSetLayout(
1110 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001111 const vk::DescriptorSetLayoutDesc &desc,
1112 vk::BindingPointer<vk::DescriptorSetLayout> *descriptorSetLayoutOut)
1113{
Jamie Madill21061022018-07-12 23:56:30 -04001114 return mDescriptorSetLayoutCache.getDescriptorSetLayout(context, desc, descriptorSetLayoutOut);
Jamie Madill9b168d02018-06-13 13:25:32 -04001115}
1116
Jamie Madill21061022018-07-12 23:56:30 -04001117angle::Result RendererVk::getPipelineLayout(
1118 vk::Context *context,
Jamie Madill9b168d02018-06-13 13:25:32 -04001119 const vk::PipelineLayoutDesc &desc,
1120 const vk::DescriptorSetLayoutPointerArray &descriptorSetLayouts,
1121 vk::BindingPointer<vk::PipelineLayout> *pipelineLayoutOut)
1122{
Jamie Madill21061022018-07-12 23:56:30 -04001123 return mPipelineLayoutCache.getPipelineLayout(context, desc, descriptorSetLayouts,
Jamie Madill9b168d02018-06-13 13:25:32 -04001124 pipelineLayoutOut);
1125}
1126
Shahbaz Youssefi996628a2018-09-24 16:39:26 -04001127angle::Result RendererVk::syncPipelineCacheVk(DisplayVk *displayVk)
1128{
1129 ASSERT(mPipelineCacheVk.valid());
1130
1131 if (--mPipelineCacheVkUpdateTimeout > 0)
1132 {
1133 return angle::Result::Continue();
1134 }
1135
1136 mPipelineCacheVkUpdateTimeout = kPipelineCacheVkUpdatePeriod;
1137
1138 // Get the size of the cache.
1139 size_t pipelineCacheSize = 0;
1140 ANGLE_TRY(mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, nullptr));
1141
1142 angle::MemoryBuffer *pipelineCacheData = nullptr;
1143 ANGLE_VK_CHECK_ALLOC(displayVk,
1144 displayVk->getScratchBuffer(pipelineCacheSize, &pipelineCacheData));
1145
1146 size_t originalPipelineCacheSize = pipelineCacheSize;
1147 angle::Result result =
1148 mPipelineCacheVk.getCacheData(displayVk, &pipelineCacheSize, pipelineCacheData->data());
1149 ANGLE_TRY(result);
1150
1151 // Note: currently we don't accept incomplete as we don't expect it (the full size of cache
1152 // was determined just above), so receiving it hints at an implementation bug we would want
1153 // to know about early.
1154 ASSERT(result != angle::Result::Incomplete());
1155
1156 // If vkGetPipelineCacheData ends up writing fewer bytes than requested, zero out the rest of
1157 // the buffer to avoid leaking garbage memory.
1158 ASSERT(pipelineCacheSize <= originalPipelineCacheSize);
1159 if (pipelineCacheSize < originalPipelineCacheSize)
1160 {
1161 memset(pipelineCacheData->data() + pipelineCacheSize, 0,
1162 originalPipelineCacheSize - pipelineCacheSize);
1163 }
1164
1165 displayVk->getBlobCache()->putApplication(mPipelineCacheVkBlobKey, *pipelineCacheData);
1166
1167 return angle::Result::Continue();
1168}
1169
Shahbaz Youssefi3a482172018-10-11 10:34:44 -04001170angle::Result RendererVk::allocateSubmitWaitSemaphore(vk::Context *context,
1171 const vk::Semaphore **outSemaphore)
1172{
1173 ASSERT(mSubmitWaitSemaphores.size() < mSubmitWaitSemaphores.max_size());
1174
1175 vk::SemaphoreHelper semaphore;
1176 ANGLE_TRY(mSubmitSemaphorePool.allocateSemaphore(context, &semaphore));
1177
1178 mSubmitWaitSemaphores.push_back(std::move(semaphore));
1179 *outSemaphore = mSubmitWaitSemaphores.back().getSemaphore();
1180
1181 return angle::Result::Continue();
1182}
1183
1184const vk::Semaphore *RendererVk::getSubmitLastSignaledSemaphore(vk::Context *context)
1185{
1186 const vk::Semaphore *semaphore = mSubmitLastSignaledSemaphore.getSemaphore();
1187
1188 // Return the semaphore to the pool (which will remain valid and unused until the
1189 // queue it's about to be waited on has finished execution). The caller is about
1190 // to wait on it.
1191 mSubmitSemaphorePool.freeSemaphore(context, &mSubmitLastSignaledSemaphore);
1192
1193 return semaphore;
1194}
1195
Jamie Madilld47044a2018-04-27 11:45:03 -04001196vk::ShaderLibrary *RendererVk::getShaderLibrary()
1197{
1198 return &mShaderLibrary;
1199}
Luc Ferron90968362018-05-04 08:47:22 -04001200
Jamie Madillaaca96e2018-06-12 10:19:48 -04001201uint32_t GetUniformBufferDescriptorCount()
1202{
1203 return kUniformBufferDescriptorsPerDescriptorSet;
1204}
1205
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001206} // namespace rx